@aexhq/sdk 0.13.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +160 -0
  3. package/dist/_contracts/connection-ticket.d.ts +21 -0
  4. package/dist/_contracts/connection-ticket.js +49 -0
  5. package/dist/_contracts/event-envelope.d.ts +276 -0
  6. package/dist/_contracts/event-envelope.js +324 -0
  7. package/dist/_contracts/event-stream-client.d.ts +47 -0
  8. package/dist/_contracts/event-stream-client.js +141 -0
  9. package/dist/_contracts/http.d.ts +35 -0
  10. package/dist/_contracts/http.js +114 -0
  11. package/dist/_contracts/index.d.ts +28 -0
  12. package/dist/_contracts/index.js +29 -0
  13. package/dist/_contracts/managed-key.d.ts +74 -0
  14. package/dist/_contracts/managed-key.js +110 -0
  15. package/dist/_contracts/operations.d.ts +237 -0
  16. package/dist/_contracts/operations.js +632 -0
  17. package/dist/_contracts/provider-support.d.ts +220 -0
  18. package/dist/_contracts/provider-support.js +90 -0
  19. package/dist/_contracts/proxy-protocol.d.ts +257 -0
  20. package/dist/_contracts/proxy-protocol.js +234 -0
  21. package/dist/_contracts/proxy-validation.d.ts +19 -0
  22. package/dist/_contracts/proxy-validation.js +51 -0
  23. package/dist/_contracts/run-artifacts.d.ts +47 -0
  24. package/dist/_contracts/run-artifacts.js +101 -0
  25. package/dist/_contracts/run-config.d.ts +304 -0
  26. package/dist/_contracts/run-config.js +659 -0
  27. package/dist/_contracts/run-cost.d.ts +125 -0
  28. package/dist/_contracts/run-cost.js +616 -0
  29. package/dist/_contracts/run-custody.d.ts +226 -0
  30. package/dist/_contracts/run-custody.js +465 -0
  31. package/dist/_contracts/run-record.d.ts +127 -0
  32. package/dist/_contracts/run-record.js +177 -0
  33. package/dist/_contracts/run-retention.d.ts +213 -0
  34. package/dist/_contracts/run-retention.js +484 -0
  35. package/dist/_contracts/run-unit.d.ts +194 -0
  36. package/dist/_contracts/run-unit.js +215 -0
  37. package/dist/_contracts/runner-event.d.ts +114 -0
  38. package/dist/_contracts/runner-event.js +187 -0
  39. package/dist/_contracts/runtime-manifest.d.ts +106 -0
  40. package/dist/_contracts/runtime-manifest.js +98 -0
  41. package/dist/_contracts/runtime-security-profile.d.ts +27 -0
  42. package/dist/_contracts/runtime-security-profile.js +82 -0
  43. package/dist/_contracts/runtime-sizes.d.ts +144 -0
  44. package/dist/_contracts/runtime-sizes.js +136 -0
  45. package/dist/_contracts/runtime-types.d.ts +212 -0
  46. package/dist/_contracts/runtime-types.js +2 -0
  47. package/dist/_contracts/sdk-errors.d.ts +34 -0
  48. package/dist/_contracts/sdk-errors.js +52 -0
  49. package/dist/_contracts/sdk-secrets.d.ts +31 -0
  50. package/dist/_contracts/sdk-secrets.js +220 -0
  51. package/dist/_contracts/side-effect-audit.d.ts +129 -0
  52. package/dist/_contracts/side-effect-audit.js +494 -0
  53. package/dist/_contracts/sse.d.ts +74 -0
  54. package/dist/_contracts/sse.js +0 -0
  55. package/dist/_contracts/stable.d.ts +26 -0
  56. package/dist/_contracts/stable.js +44 -0
  57. package/dist/_contracts/status.d.ts +19 -0
  58. package/dist/_contracts/status.js +61 -0
  59. package/dist/_contracts/submission.d.ts +383 -0
  60. package/dist/_contracts/submission.js +1380 -0
  61. package/dist/agents-md.d.ts +46 -0
  62. package/dist/agents-md.js +83 -0
  63. package/dist/agents-md.js.map +1 -0
  64. package/dist/asset-upload.d.ts +66 -0
  65. package/dist/asset-upload.js +168 -0
  66. package/dist/asset-upload.js.map +1 -0
  67. package/dist/bundle.d.ts +33 -0
  68. package/dist/bundle.js +89 -0
  69. package/dist/bundle.js.map +1 -0
  70. package/dist/cli.mjs +4140 -0
  71. package/dist/cli.mjs.sha256 +1 -0
  72. package/dist/client.d.ts +460 -0
  73. package/dist/client.js +857 -0
  74. package/dist/client.js.map +1 -0
  75. package/dist/fetch-archive.d.ts +16 -0
  76. package/dist/fetch-archive.js +170 -0
  77. package/dist/fetch-archive.js.map +1 -0
  78. package/dist/file.d.ts +57 -0
  79. package/dist/file.js +153 -0
  80. package/dist/file.js.map +1 -0
  81. package/dist/index.d.ts +30 -0
  82. package/dist/index.js +34 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/mcp-server.d.ts +84 -0
  85. package/dist/mcp-server.js +114 -0
  86. package/dist/mcp-server.js.map +1 -0
  87. package/dist/node-fs.d.ts +12 -0
  88. package/dist/node-fs.js +44 -0
  89. package/dist/node-fs.js.map +1 -0
  90. package/dist/proxy-endpoint.d.ts +131 -0
  91. package/dist/proxy-endpoint.js +147 -0
  92. package/dist/proxy-endpoint.js.map +1 -0
  93. package/dist/skill.d.ts +117 -0
  94. package/dist/skill.js +169 -0
  95. package/dist/skill.js.map +1 -0
  96. package/dist/version.d.ts +9 -0
  97. package/dist/version.js +10 -0
  98. package/dist/version.js.map +1 -0
  99. package/docs/cleanup.md +38 -0
  100. package/docs/credentials.md +153 -0
  101. package/docs/events.md +76 -0
  102. package/docs/mcp.md +47 -0
  103. package/docs/outputs.md +157 -0
  104. package/docs/product-boundaries.md +57 -0
  105. package/docs/provider-runtime-capabilities.md +103 -0
  106. package/docs/quickstart.md +110 -0
  107. package/docs/release.md +99 -0
  108. package/docs/run-config.md +53 -0
  109. package/docs/run-record.md +39 -0
  110. package/docs/skills.md +139 -0
  111. package/docs/testing.md +29 -0
  112. package/package.json +47 -0
@@ -0,0 +1,616 @@
1
+ export const RUN_COST_TELEMETRY_SCHEMA_VERSION = 1;
2
+ export const RUN_USAGE_SAMPLE_SCHEMA_VERSION = 1;
3
+ export const RUN_COST_SUMMARY_STATUSES = [
4
+ "pending",
5
+ "partial",
6
+ "complete",
7
+ "unavailable",
8
+ "not_applicable"
9
+ ];
10
+ export const RUN_USAGE_SAMPLE_UNITS = [
11
+ "token",
12
+ "millisecond",
13
+ "byte",
14
+ "byte_millisecond",
15
+ "count",
16
+ "file",
17
+ "credit_unit"
18
+ ];
19
+ export const RUN_USAGE_SAMPLE_SOURCE_TYPES = [
20
+ "coordinator-event",
21
+ "run-event",
22
+ "usage-ledger",
23
+ "output-object",
24
+ "proxy-call",
25
+ "runtime-job",
26
+ "provider-session",
27
+ "storage-accrual",
28
+ "billing-reservation",
29
+ "billing-settlement",
30
+ "billing-release",
31
+ "manual-adjustment"
32
+ ];
33
+ export const RUN_USAGE_SAMPLE_METRICS = [
34
+ "provider.input_tokens",
35
+ "provider.output_tokens",
36
+ "provider.cache_read_input_tokens",
37
+ "provider.cache_creation_input_tokens",
38
+ "provider.total_tokens",
39
+ "runtime.queued_ms",
40
+ "runtime.active_ms",
41
+ "runtime.output_capture_ms",
42
+ "runtime.cleanup_ms",
43
+ "run.total_ms",
44
+ "output.discovered_files",
45
+ "output.captured_files",
46
+ "output.failed_files",
47
+ "output.captured_bytes",
48
+ "retry.runtime_attempts",
49
+ "retry.provider_poll",
50
+ "retry.output_capture",
51
+ "retry.output_upload",
52
+ "capture.uploaded_files",
53
+ "capture.failed_files",
54
+ "capture.total_bytes",
55
+ "storage.current_bytes",
56
+ "storage.byte_milliseconds",
57
+ "proxy.call_count",
58
+ "proxy.failed_call_count",
59
+ "proxy.request_bytes",
60
+ "proxy.response_bytes",
61
+ "proxy.duration_ms",
62
+ "managed_key.reserved_credit_units",
63
+ "managed_key.charged_credit_units",
64
+ "managed_key.released_credit_units"
65
+ ];
66
+ const RUN_USAGE_SAMPLE_METRIC_UNITS = {
67
+ "provider.input_tokens": "token",
68
+ "provider.output_tokens": "token",
69
+ "provider.cache_read_input_tokens": "token",
70
+ "provider.cache_creation_input_tokens": "token",
71
+ "provider.total_tokens": "token",
72
+ "runtime.queued_ms": "millisecond",
73
+ "runtime.active_ms": "millisecond",
74
+ "runtime.output_capture_ms": "millisecond",
75
+ "runtime.cleanup_ms": "millisecond",
76
+ "run.total_ms": "millisecond",
77
+ "output.discovered_files": "file",
78
+ "output.captured_files": "file",
79
+ "output.failed_files": "file",
80
+ "output.captured_bytes": "byte",
81
+ "retry.runtime_attempts": "count",
82
+ "retry.provider_poll": "count",
83
+ "retry.output_capture": "count",
84
+ "retry.output_upload": "count",
85
+ "capture.uploaded_files": "file",
86
+ "capture.failed_files": "file",
87
+ "capture.total_bytes": "byte",
88
+ "storage.current_bytes": "byte",
89
+ "storage.byte_milliseconds": "byte_millisecond",
90
+ "proxy.call_count": "count",
91
+ "proxy.failed_call_count": "count",
92
+ "proxy.request_bytes": "byte",
93
+ "proxy.response_bytes": "byte",
94
+ "proxy.duration_ms": "millisecond",
95
+ "managed_key.reserved_credit_units": "credit_unit",
96
+ "managed_key.charged_credit_units": "credit_unit",
97
+ "managed_key.released_credit_units": "credit_unit"
98
+ };
99
+ export function buildRunUsageSample(input) {
100
+ const metric = normalizeUsageSampleMetric(input.metric);
101
+ const expectedUnit = RUN_USAGE_SAMPLE_METRIC_UNITS[metric];
102
+ const unit = input.unit ? normalizeUsageSampleUnit(input.unit) : expectedUnit;
103
+ if (unit !== expectedUnit) {
104
+ throw new Error(`run usage sample ${metric} must use ${expectedUnit} units`);
105
+ }
106
+ return Object.freeze({
107
+ schemaVersion: RUN_USAGE_SAMPLE_SCHEMA_VERSION,
108
+ ...(input.sampleId ? { sampleId: nonEmptyString(input.sampleId, "sampleId") } : {}),
109
+ ...(input.runId ? { runId: input.runId } : {}),
110
+ metric,
111
+ unit,
112
+ quantity: nonNegativeFinite(input.quantity, "quantity"),
113
+ source: normalizeUsageSampleSource(input.source),
114
+ ...(input.provider ? { provider: input.provider } : {}),
115
+ ...(input.runtime ? { runtime: input.runtime } : {}),
116
+ ...(input.model ? { model: input.model } : {}),
117
+ ...(input.credentialMode ? { credentialMode: input.credentialMode } : {}),
118
+ ...(input.recordedAt ? { recordedAt: input.recordedAt } : {})
119
+ });
120
+ }
121
+ export function buildRunCostTelemetry(input) {
122
+ return freezeTelemetry({
123
+ schemaVersion: RUN_COST_TELEMETRY_SCHEMA_VERSION,
124
+ ...(input.runId ? { runId: input.runId } : {}),
125
+ ...(input.provider ? { provider: input.provider } : {}),
126
+ ...(input.runtime ? { runtime: input.runtime } : {}),
127
+ ...(input.recordedAt ? { recordedAt: input.recordedAt } : {}),
128
+ ...(input.status ? { status: normalizeSummaryStatus(input.status) } : {}),
129
+ ...(input.sourceSummary ? { sourceSummary: normalizeSourceSummary(input.sourceSummary) } : {}),
130
+ ...(input.durations ? { durations: normalizeDurations(input.durations) } : {}),
131
+ ...(input.outputs ? { outputs: normalizeOutputs(input.outputs) } : {}),
132
+ ...(input.retries ? { retries: normalizeRetries(input.retries) } : {}),
133
+ ...(input.capture ? { capture: normalizeCapture(input.capture) } : {}),
134
+ ...(input.providerUsage ? { providerUsage: input.providerUsage.map(normalizeProviderUsage) } : {}),
135
+ ...(input.storage ? { storage: normalizeStorage(input.storage) } : {}),
136
+ ...(input.proxy ? { proxy: normalizeProxy(input.proxy) } : {}),
137
+ ...(input.managedKey ? { managedKey: normalizeManagedKey(input.managedKey) } : {})
138
+ });
139
+ }
140
+ export function buildRunCostTelemetryFromUsageSamples(input) {
141
+ const samples = input.samples.map(buildRunUsageSample);
142
+ const telemetry = {
143
+ sourceSummary: summarizeUsageSamples(samples)
144
+ };
145
+ if (input.runId)
146
+ telemetry.runId = input.runId;
147
+ if (input.provider)
148
+ telemetry.provider = input.provider;
149
+ if (input.runtime)
150
+ telemetry.runtime = input.runtime;
151
+ if (input.recordedAt)
152
+ telemetry.recordedAt = input.recordedAt;
153
+ if (input.status)
154
+ telemetry.status = input.status;
155
+ const durations = {};
156
+ const outputs = {};
157
+ const retries = {};
158
+ const capture = {};
159
+ const storage = {};
160
+ const proxy = {};
161
+ const managedKey = {};
162
+ const providerUsage = new Map();
163
+ let captureAttempted = false;
164
+ let managedKeyCredentialMode;
165
+ for (const sample of samples) {
166
+ switch (sample.metric) {
167
+ case "provider.input_tokens":
168
+ addProviderUsage(providerUsage, input, sample, "inputTokens");
169
+ break;
170
+ case "provider.output_tokens":
171
+ addProviderUsage(providerUsage, input, sample, "outputTokens");
172
+ break;
173
+ case "provider.cache_read_input_tokens":
174
+ addProviderUsage(providerUsage, input, sample, "cacheReadInputTokens");
175
+ break;
176
+ case "provider.cache_creation_input_tokens":
177
+ addProviderUsage(providerUsage, input, sample, "cacheCreationInputTokens");
178
+ break;
179
+ case "provider.total_tokens":
180
+ addProviderUsage(providerUsage, input, sample, "totalTokens");
181
+ break;
182
+ case "runtime.queued_ms":
183
+ addDraftNumber(durations, "queuedMs", sample.quantity);
184
+ break;
185
+ case "runtime.active_ms":
186
+ addDraftNumber(durations, "runtimeMs", sample.quantity);
187
+ break;
188
+ case "runtime.output_capture_ms":
189
+ addDraftNumber(durations, "outputCaptureMs", sample.quantity);
190
+ break;
191
+ case "runtime.cleanup_ms":
192
+ addDraftNumber(durations, "cleanupMs", sample.quantity);
193
+ break;
194
+ case "run.total_ms":
195
+ addDraftNumber(durations, "totalMs", sample.quantity);
196
+ break;
197
+ case "output.discovered_files":
198
+ addDraftNumber(outputs, "discoveredFiles", sample.quantity);
199
+ break;
200
+ case "output.captured_files":
201
+ addDraftNumber(outputs, "capturedFiles", sample.quantity);
202
+ break;
203
+ case "output.failed_files":
204
+ addDraftNumber(outputs, "failedFiles", sample.quantity);
205
+ break;
206
+ case "output.captured_bytes":
207
+ addDraftNumber(outputs, "capturedBytes", sample.quantity);
208
+ break;
209
+ case "retry.runtime_attempts":
210
+ addDraftNumber(retries, "runtimeAttempts", sample.quantity);
211
+ break;
212
+ case "retry.provider_poll":
213
+ addDraftNumber(retries, "providerPollRetries", sample.quantity);
214
+ break;
215
+ case "retry.output_capture":
216
+ addDraftNumber(retries, "outputCaptureRetries", sample.quantity);
217
+ break;
218
+ case "retry.output_upload":
219
+ addDraftNumber(retries, "outputUploadRetries", sample.quantity);
220
+ break;
221
+ case "capture.uploaded_files":
222
+ captureAttempted = true;
223
+ addDraftNumber(capture, "uploadedFiles", sample.quantity);
224
+ break;
225
+ case "capture.failed_files":
226
+ captureAttempted = true;
227
+ addDraftNumber(capture, "failedFiles", sample.quantity);
228
+ break;
229
+ case "capture.total_bytes":
230
+ captureAttempted = true;
231
+ addDraftNumber(capture, "totalBytes", sample.quantity);
232
+ break;
233
+ case "storage.current_bytes":
234
+ addDraftNumber(storage, "storedBytes", sample.quantity);
235
+ break;
236
+ case "storage.byte_milliseconds":
237
+ addDraftNumber(storage, "byteMilliseconds", sample.quantity);
238
+ break;
239
+ case "proxy.call_count":
240
+ addDraftNumber(proxy, "calls", sample.quantity);
241
+ break;
242
+ case "proxy.failed_call_count":
243
+ addDraftNumber(proxy, "failedCalls", sample.quantity);
244
+ break;
245
+ case "proxy.request_bytes":
246
+ addDraftNumber(proxy, "requestBytes", sample.quantity);
247
+ break;
248
+ case "proxy.response_bytes":
249
+ addDraftNumber(proxy, "responseBytes", sample.quantity);
250
+ break;
251
+ case "proxy.duration_ms":
252
+ addDraftNumber(proxy, "durationMs", sample.quantity);
253
+ break;
254
+ case "managed_key.reserved_credit_units":
255
+ managedKeyCredentialMode = sample.credentialMode ?? managedKeyCredentialMode;
256
+ addDraftNumber(managedKey, "reservedCreditUnits", sample.quantity);
257
+ break;
258
+ case "managed_key.charged_credit_units":
259
+ managedKeyCredentialMode = sample.credentialMode ?? managedKeyCredentialMode;
260
+ addDraftNumber(managedKey, "chargedCreditUnits", sample.quantity);
261
+ break;
262
+ case "managed_key.released_credit_units":
263
+ managedKeyCredentialMode = sample.credentialMode ?? managedKeyCredentialMode;
264
+ addDraftNumber(managedKey, "releasedCreditUnits", sample.quantity);
265
+ break;
266
+ }
267
+ }
268
+ if (Object.keys(durations).length > 0)
269
+ telemetry.durations = durations;
270
+ if (Object.keys(outputs).length > 0)
271
+ telemetry.outputs = outputs;
272
+ if (Object.keys(retries).length > 0)
273
+ telemetry.retries = retries;
274
+ if (captureAttempted || Object.keys(capture).length > 0) {
275
+ telemetry.capture = { attempted: true, ...capture };
276
+ }
277
+ if (providerUsage.size > 0) {
278
+ telemetry.providerUsage = Array.from(providerUsage.values()).map(publicProviderUsage);
279
+ }
280
+ if (Object.keys(storage).length > 0)
281
+ telemetry.storage = storage;
282
+ if (Object.keys(proxy).length > 0)
283
+ telemetry.proxy = proxy;
284
+ if (Object.keys(managedKey).length > 0 || managedKeyCredentialMode) {
285
+ telemetry.managedKey = {
286
+ ...(managedKeyCredentialMode ? { credentialMode: managedKeyCredentialMode } : {}),
287
+ ...managedKey
288
+ };
289
+ }
290
+ return buildRunCostTelemetry(telemetry);
291
+ }
292
+ export function summarizeUsageSamples(samples) {
293
+ const normalized = samples.map(buildRunUsageSample);
294
+ const metrics = unique(normalized.map((sample) => sample.metric));
295
+ const sourceTypes = unique(normalized.map((sample) => sample.source.type));
296
+ const sourceSampleIds = unique(normalized.flatMap((sample) => (sample.sampleId ? [sample.sampleId] : [])));
297
+ return normalizeSourceSummary({
298
+ sampleCount: normalized.length,
299
+ ...(metrics.length > 0 ? { metrics } : {}),
300
+ ...(sourceTypes.length > 0 ? { sourceTypes } : {}),
301
+ ...(sourceSampleIds.length > 0 ? { sourceSampleIds } : {})
302
+ });
303
+ }
304
+ export function mergeRunCostTelemetry(base, next) {
305
+ const patch = buildRunCostTelemetry(next);
306
+ const merged = {};
307
+ const runId = patch.runId ?? base.runId;
308
+ const provider = patch.provider ?? base.provider;
309
+ const runtime = patch.runtime ?? base.runtime;
310
+ const recordedAt = patch.recordedAt ?? base.recordedAt;
311
+ const status = patch.status ?? base.status;
312
+ const sourceSummary = mergeSourceSummary(base.sourceSummary, patch.sourceSummary);
313
+ const durations = sumDurations(base.durations, patch.durations);
314
+ const outputs = sumOutputs(base.outputs, patch.outputs);
315
+ const retries = sumRetries(base.retries, patch.retries);
316
+ const capture = mergeCapture(base.capture, patch.capture);
317
+ const providerUsage = [...(base.providerUsage ?? []), ...(patch.providerUsage ?? [])];
318
+ const storage = sumStorage(base.storage, patch.storage);
319
+ const proxy = sumProxy(base.proxy, patch.proxy);
320
+ const managedKey = mergeManagedKey(base.managedKey, patch.managedKey);
321
+ if (runId)
322
+ merged.runId = runId;
323
+ if (provider)
324
+ merged.provider = provider;
325
+ if (runtime)
326
+ merged.runtime = runtime;
327
+ if (recordedAt)
328
+ merged.recordedAt = recordedAt;
329
+ if (status)
330
+ merged.status = status;
331
+ if (sourceSummary)
332
+ merged.sourceSummary = sourceSummary;
333
+ if (durations)
334
+ merged.durations = durations;
335
+ if (outputs)
336
+ merged.outputs = outputs;
337
+ if (retries)
338
+ merged.retries = retries;
339
+ if (capture)
340
+ merged.capture = capture;
341
+ if (providerUsage.length > 0)
342
+ merged.providerUsage = providerUsage;
343
+ if (storage)
344
+ merged.storage = storage;
345
+ if (proxy)
346
+ merged.proxy = proxy;
347
+ if (managedKey)
348
+ merged.managedKey = managedKey;
349
+ return buildRunCostTelemetry(merged);
350
+ }
351
+ function normalizeDurations(input) {
352
+ return freezeOptionalNumbers({
353
+ queuedMs: input.queuedMs,
354
+ runtimeMs: input.runtimeMs,
355
+ outputCaptureMs: input.outputCaptureMs,
356
+ cleanupMs: input.cleanupMs,
357
+ totalMs: input.totalMs
358
+ });
359
+ }
360
+ function normalizeOutputs(input) {
361
+ return freezeOptionalNumbers({
362
+ discoveredFiles: input.discoveredFiles,
363
+ capturedFiles: input.capturedFiles,
364
+ failedFiles: input.failedFiles,
365
+ capturedBytes: input.capturedBytes
366
+ });
367
+ }
368
+ function normalizeRetries(input) {
369
+ return freezeOptionalNumbers({
370
+ runtimeAttempts: input.runtimeAttempts,
371
+ providerPollRetries: input.providerPollRetries,
372
+ outputCaptureRetries: input.outputCaptureRetries,
373
+ outputUploadRetries: input.outputUploadRetries
374
+ });
375
+ }
376
+ function normalizeCapture(input) {
377
+ return Object.freeze({
378
+ attempted: input.attempted,
379
+ ...freezeOptionalNumbers({
380
+ uploadedFiles: input.uploadedFiles,
381
+ failedFiles: input.failedFiles,
382
+ totalBytes: input.totalBytes
383
+ }),
384
+ ...(input.failureReasons ? { failureReasons: Object.freeze([...input.failureReasons]) } : {})
385
+ });
386
+ }
387
+ function normalizeProviderUsage(input) {
388
+ return Object.freeze({
389
+ provider: input.provider,
390
+ ...(input.model ? { model: input.model } : {}),
391
+ ...freezeOptionalNumbers({
392
+ inputTokens: input.inputTokens,
393
+ outputTokens: input.outputTokens,
394
+ cacheReadInputTokens: input.cacheReadInputTokens,
395
+ cacheCreationInputTokens: input.cacheCreationInputTokens,
396
+ totalTokens: input.totalTokens
397
+ }),
398
+ ...(input.sourceEventId ? { sourceEventId: input.sourceEventId } : {}),
399
+ ...(input.sourceSampleIds ? { sourceSampleIds: Object.freeze([...input.sourceSampleIds]) } : {})
400
+ });
401
+ }
402
+ function normalizeStorage(input) {
403
+ return freezeOptionalNumbers({
404
+ storedBytes: input.storedBytes,
405
+ byteMilliseconds: input.byteMilliseconds
406
+ });
407
+ }
408
+ function normalizeProxy(input) {
409
+ return freezeOptionalNumbers({
410
+ calls: input.calls,
411
+ failedCalls: input.failedCalls,
412
+ requestBytes: input.requestBytes,
413
+ responseBytes: input.responseBytes,
414
+ durationMs: input.durationMs
415
+ });
416
+ }
417
+ function normalizeManagedKey(input) {
418
+ return Object.freeze({
419
+ ...(input.credentialMode ? { credentialMode: input.credentialMode } : {}),
420
+ ...freezeOptionalNumbers({
421
+ reservedCreditUnits: input.reservedCreditUnits,
422
+ chargedCreditUnits: input.chargedCreditUnits,
423
+ releasedCreditUnits: input.releasedCreditUnits
424
+ })
425
+ });
426
+ }
427
+ function normalizeSourceSummary(input) {
428
+ return Object.freeze({
429
+ sampleCount: nonNegativeFinite(input.sampleCount, "sourceSummary.sampleCount"),
430
+ ...(input.metrics ? { metrics: Object.freeze(input.metrics.map(normalizeUsageSampleMetric)) } : {}),
431
+ ...(input.sourceTypes
432
+ ? { sourceTypes: Object.freeze(input.sourceTypes.map(normalizeUsageSampleSourceType)) }
433
+ : {}),
434
+ ...(input.sourceSampleIds
435
+ ? { sourceSampleIds: Object.freeze(input.sourceSampleIds.map((id) => nonEmptyString(id, "sourceSampleIds"))) }
436
+ : {})
437
+ });
438
+ }
439
+ function normalizeUsageSampleSource(input) {
440
+ if (!input || typeof input !== "object") {
441
+ throw new Error("run usage sample source must be an object");
442
+ }
443
+ return Object.freeze({
444
+ type: normalizeUsageSampleSourceType(input.type),
445
+ id: nonEmptyString(input.id, "source.id"),
446
+ ...(input.observedAt ? { observedAt: input.observedAt } : {})
447
+ });
448
+ }
449
+ function normalizeUsageSampleMetric(input) {
450
+ if (!isStringIn(input, RUN_USAGE_SAMPLE_METRICS)) {
451
+ throw new Error(`run usage sample metric ${String(input)} is not supported`);
452
+ }
453
+ return input;
454
+ }
455
+ function normalizeUsageSampleUnit(input) {
456
+ if (!isStringIn(input, RUN_USAGE_SAMPLE_UNITS)) {
457
+ throw new Error(`run usage sample unit ${String(input)} is not supported`);
458
+ }
459
+ return input;
460
+ }
461
+ function normalizeUsageSampleSourceType(input) {
462
+ if (!isStringIn(input, RUN_USAGE_SAMPLE_SOURCE_TYPES)) {
463
+ throw new Error(`run usage sample source type ${String(input)} is not supported`);
464
+ }
465
+ return input;
466
+ }
467
+ function normalizeSummaryStatus(input) {
468
+ if (!isStringIn(input, RUN_COST_SUMMARY_STATUSES)) {
469
+ throw new Error(`run cost telemetry status ${String(input)} is not supported`);
470
+ }
471
+ return input;
472
+ }
473
+ function addProviderUsage(usageByKey, input, sample, field) {
474
+ const provider = sample.provider ?? input.provider;
475
+ if (!provider) {
476
+ throw new Error(`run usage sample ${sample.metric} requires a provider`);
477
+ }
478
+ const key = `${provider}\u0000${sample.model ?? ""}`;
479
+ const existing = usageByKey.get(key);
480
+ const usage = existing ?? {
481
+ provider,
482
+ ...(sample.model ? { model: sample.model } : {}),
483
+ sourceSampleIds: []
484
+ };
485
+ usage[field] = (usage[field] ?? 0) + sample.quantity;
486
+ if (!usage.sourceEventId && (sample.source.type === "coordinator-event" || sample.source.type === "run-event")) {
487
+ usage.sourceEventId = sample.source.id;
488
+ }
489
+ if (sample.sampleId) {
490
+ usage.sourceSampleIds.push(sample.sampleId);
491
+ }
492
+ usageByKey.set(key, usage);
493
+ }
494
+ function publicProviderUsage(input) {
495
+ return {
496
+ provider: input.provider,
497
+ ...(input.model ? { model: input.model } : {}),
498
+ ...(input.inputTokens !== undefined ? { inputTokens: input.inputTokens } : {}),
499
+ ...(input.outputTokens !== undefined ? { outputTokens: input.outputTokens } : {}),
500
+ ...(input.cacheReadInputTokens !== undefined ? { cacheReadInputTokens: input.cacheReadInputTokens } : {}),
501
+ ...(input.cacheCreationInputTokens !== undefined
502
+ ? { cacheCreationInputTokens: input.cacheCreationInputTokens }
503
+ : {}),
504
+ ...(input.totalTokens !== undefined ? { totalTokens: input.totalTokens } : {}),
505
+ ...(input.sourceEventId ? { sourceEventId: input.sourceEventId } : {}),
506
+ ...(input.sourceSampleIds.length > 0 ? { sourceSampleIds: input.sourceSampleIds } : {})
507
+ };
508
+ }
509
+ function addDraftNumber(target, key, quantity) {
510
+ target[key] = (target[key] ?? 0) + quantity;
511
+ }
512
+ function freezeOptionalNumbers(input) {
513
+ const out = {};
514
+ for (const key of Object.keys(input)) {
515
+ const value = input[key];
516
+ if (value === undefined) {
517
+ continue;
518
+ }
519
+ out[key] = nonNegativeFinite(value, key);
520
+ }
521
+ return Object.freeze(out);
522
+ }
523
+ function sumDurations(base, next) {
524
+ return sumNumberFields(["queuedMs", "runtimeMs", "outputCaptureMs", "cleanupMs", "totalMs"], base, next);
525
+ }
526
+ function sumOutputs(base, next) {
527
+ return sumNumberFields(["discoveredFiles", "capturedFiles", "failedFiles", "capturedBytes"], base, next);
528
+ }
529
+ function sumRetries(base, next) {
530
+ return sumNumberFields(["runtimeAttempts", "providerPollRetries", "outputCaptureRetries", "outputUploadRetries"], base, next);
531
+ }
532
+ function sumStorage(base, next) {
533
+ return sumNumberFields(["storedBytes", "byteMilliseconds"], base, next);
534
+ }
535
+ function sumProxy(base, next) {
536
+ return sumNumberFields(["calls", "failedCalls", "requestBytes", "responseBytes", "durationMs"], base, next);
537
+ }
538
+ function sumNumberFields(keys, base, next) {
539
+ if (!base && !next) {
540
+ return undefined;
541
+ }
542
+ const out = {};
543
+ for (const key of keys) {
544
+ for (const source of [base, next]) {
545
+ const value = source?.[key];
546
+ if (value !== undefined) {
547
+ out[key] = (out[key] ?? 0) + value;
548
+ }
549
+ }
550
+ }
551
+ return Object.keys(out).length > 0 ? Object.freeze(out) : undefined;
552
+ }
553
+ function mergeCapture(base, next) {
554
+ if (!base && !next) {
555
+ return undefined;
556
+ }
557
+ const failureReasons = [
558
+ ...(base?.failureReasons ?? []),
559
+ ...(next?.failureReasons ?? [])
560
+ ];
561
+ return normalizeCapture({
562
+ attempted: Boolean(base?.attempted || next?.attempted),
563
+ uploadedFiles: (base?.uploadedFiles ?? 0) + (next?.uploadedFiles ?? 0),
564
+ failedFiles: (base?.failedFiles ?? 0) + (next?.failedFiles ?? 0),
565
+ totalBytes: (base?.totalBytes ?? 0) + (next?.totalBytes ?? 0),
566
+ ...(failureReasons.length > 0 ? { failureReasons } : {})
567
+ });
568
+ }
569
+ function mergeManagedKey(base, next) {
570
+ if (!base && !next) {
571
+ return undefined;
572
+ }
573
+ const credentialMode = next?.credentialMode ?? base?.credentialMode;
574
+ return normalizeManagedKey({
575
+ ...(credentialMode ? { credentialMode } : {}),
576
+ reservedCreditUnits: (base?.reservedCreditUnits ?? 0) + (next?.reservedCreditUnits ?? 0),
577
+ chargedCreditUnits: (base?.chargedCreditUnits ?? 0) + (next?.chargedCreditUnits ?? 0),
578
+ releasedCreditUnits: (base?.releasedCreditUnits ?? 0) + (next?.releasedCreditUnits ?? 0)
579
+ });
580
+ }
581
+ function mergeSourceSummary(base, next) {
582
+ if (!base && !next) {
583
+ return undefined;
584
+ }
585
+ return normalizeSourceSummary({
586
+ sampleCount: (base?.sampleCount ?? 0) + (next?.sampleCount ?? 0),
587
+ metrics: unique([...(base?.metrics ?? []), ...(next?.metrics ?? [])]),
588
+ sourceTypes: unique([...(base?.sourceTypes ?? []), ...(next?.sourceTypes ?? [])]),
589
+ sourceSampleIds: unique([...(base?.sourceSampleIds ?? []), ...(next?.sourceSampleIds ?? [])])
590
+ });
591
+ }
592
+ function freezeTelemetry(input) {
593
+ return Object.freeze({
594
+ ...input,
595
+ ...(input.providerUsage ? { providerUsage: Object.freeze([...input.providerUsage]) } : {})
596
+ });
597
+ }
598
+ function unique(input) {
599
+ return Object.freeze([...new Set(input)]);
600
+ }
601
+ function nonEmptyString(value, field) {
602
+ if (typeof value !== "string" || value.trim().length === 0) {
603
+ throw new Error(`run cost telemetry ${field} must be a non-empty string`);
604
+ }
605
+ return value;
606
+ }
607
+ function nonNegativeFinite(value, field) {
608
+ if (!Number.isFinite(value) || value < 0) {
609
+ throw new Error(`run cost telemetry ${field} must be a non-negative finite number`);
610
+ }
611
+ return value;
612
+ }
613
+ function isStringIn(value, allowed) {
614
+ return typeof value === "string" && allowed.includes(value);
615
+ }
616
+ //# sourceMappingURL=run-cost.js.map