@deepagents/context 0.30.0 → 0.32.0

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 (41) hide show
  1. package/dist/browser.js +41 -7
  2. package/dist/browser.js.map +2 -2
  3. package/dist/index.d.ts +2 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +478 -73
  6. package/dist/index.js.map +4 -4
  7. package/dist/lib/engine.d.ts +0 -6
  8. package/dist/lib/engine.d.ts.map +1 -1
  9. package/dist/lib/fragments/message/environment-reminder.d.ts +26 -0
  10. package/dist/lib/fragments/message/environment-reminder.d.ts.map +1 -0
  11. package/dist/lib/fragments/message/user.d.ts +15 -1
  12. package/dist/lib/fragments/message/user.d.ts.map +1 -1
  13. package/dist/lib/fragments/socratic.d.ts +21 -0
  14. package/dist/lib/fragments/socratic.d.ts.map +1 -0
  15. package/dist/lib/sandbox/agent-os-sandbox.d.ts +84 -0
  16. package/dist/lib/sandbox/agent-os-sandbox.d.ts.map +1 -0
  17. package/dist/lib/sandbox/container-tool.d.ts +3 -1
  18. package/dist/lib/sandbox/container-tool.d.ts.map +1 -1
  19. package/dist/lib/sandbox/docker-sandbox.d.ts +8 -3
  20. package/dist/lib/sandbox/docker-sandbox.d.ts.map +1 -1
  21. package/dist/lib/sandbox/index.d.ts +2 -1
  22. package/dist/lib/sandbox/index.d.ts.map +1 -1
  23. package/dist/lib/tracing/batch-processor.d.ts +19 -0
  24. package/dist/lib/tracing/batch-processor.d.ts.map +1 -0
  25. package/dist/lib/tracing/exporter.d.ts +22 -0
  26. package/dist/lib/tracing/exporter.d.ts.map +1 -0
  27. package/dist/lib/tracing/ids.d.ts +4 -0
  28. package/dist/lib/tracing/ids.d.ts.map +1 -0
  29. package/dist/lib/tracing/index.d.ts +7 -0
  30. package/dist/lib/tracing/index.d.ts.map +1 -0
  31. package/dist/lib/tracing/index.js +661 -0
  32. package/dist/lib/tracing/index.js.map +7 -0
  33. package/dist/lib/tracing/openai-traces-integration.d.ts +20 -0
  34. package/dist/lib/tracing/openai-traces-integration.d.ts.map +1 -0
  35. package/dist/lib/tracing/processor.d.ts +26 -0
  36. package/dist/lib/tracing/processor.d.ts.map +1 -0
  37. package/dist/lib/tracing/serialization.d.ts +7 -0
  38. package/dist/lib/tracing/serialization.d.ts.map +1 -0
  39. package/dist/lib/tracing/types.d.ts +90 -0
  40. package/dist/lib/tracing/types.d.ts.map +1 -0
  41. package/package.json +18 -5
@@ -0,0 +1,661 @@
1
+ // packages/context/src/lib/tracing/batch-processor.ts
2
+ var BatchTraceProcessor = class {
3
+ #exporter;
4
+ #maxQueueSize;
5
+ #maxBatchSize;
6
+ #scheduleDelayMs;
7
+ #exportTimeoutMs;
8
+ #flushThreshold;
9
+ #queue = [];
10
+ #flushTimer;
11
+ #flushPromise;
12
+ #shutdown = false;
13
+ constructor(exporter, options = {}) {
14
+ this.#exporter = exporter;
15
+ this.#maxQueueSize = options.maxQueueSize ?? 8192;
16
+ this.#maxBatchSize = options.maxBatchSize ?? 128;
17
+ this.#scheduleDelayMs = options.scheduleDelayMs ?? 5e3;
18
+ this.#exportTimeoutMs = options.exportTimeoutMs ?? 3e4;
19
+ this.#flushThreshold = Math.max(
20
+ 1,
21
+ Math.floor(this.#maxQueueSize * (options.exportTriggerRatio ?? 0.7))
22
+ );
23
+ }
24
+ start() {
25
+ if (this.#shutdown || this.#flushTimer != null) {
26
+ return;
27
+ }
28
+ this.#flushTimer = setInterval(() => {
29
+ void this.forceFlush();
30
+ }, this.#scheduleDelayMs);
31
+ this.#flushTimer.unref?.();
32
+ }
33
+ onTraceEnd(trace) {
34
+ this.#enqueue(trace);
35
+ }
36
+ onSpanEnd(span) {
37
+ this.#enqueue(span);
38
+ }
39
+ async forceFlush() {
40
+ if (this.#flushPromise != null) {
41
+ await this.#flushPromise;
42
+ return;
43
+ }
44
+ if (this.#queue.length === 0) {
45
+ return;
46
+ }
47
+ this.#flushPromise = this.#flush();
48
+ try {
49
+ await this.#flushPromise;
50
+ } finally {
51
+ this.#flushPromise = void 0;
52
+ }
53
+ }
54
+ async shutdown() {
55
+ this.#shutdown = true;
56
+ if (this.#flushTimer != null) {
57
+ clearInterval(this.#flushTimer);
58
+ this.#flushTimer = void 0;
59
+ }
60
+ await this.forceFlush();
61
+ }
62
+ #enqueue(item) {
63
+ if (this.#queue.length >= this.#maxQueueSize) {
64
+ return;
65
+ }
66
+ this.#queue.push(item);
67
+ if (this.#queue.length >= this.#flushThreshold) {
68
+ void this.forceFlush();
69
+ }
70
+ }
71
+ async #flush() {
72
+ while (this.#queue.length > 0) {
73
+ const batch = this.#queue.slice(0, this.#maxBatchSize);
74
+ await this.#exporter.export(
75
+ batch,
76
+ AbortSignal.timeout(this.#exportTimeoutMs)
77
+ );
78
+ this.#queue.splice(0, batch.length);
79
+ }
80
+ }
81
+ };
82
+
83
+ // packages/context/src/lib/tracing/exporter.ts
84
+ var OpenAIExportError = class extends Error {
85
+ status;
86
+ constructor(status, body) {
87
+ super(`OpenAI traces export failed (${status}): ${body}`);
88
+ this.name = "OpenAIExportError";
89
+ this.status = status;
90
+ }
91
+ };
92
+ var OpenAITracesExporter = class {
93
+ #apiKey;
94
+ #endpoint;
95
+ #organization;
96
+ #project;
97
+ #maxRetries;
98
+ #baseDelayMs;
99
+ #maxDelayMs;
100
+ constructor(options = {}) {
101
+ this.#apiKey = options.apiKey ?? process.env.OPENAI_API_KEY ?? "";
102
+ this.#endpoint = options.endpoint ?? `${options.baseURL ?? "https://api.openai.com"}/v1/traces/ingest`;
103
+ this.#organization = options.organization;
104
+ this.#project = options.project;
105
+ this.#maxRetries = options.maxRetries ?? 3;
106
+ this.#baseDelayMs = options.baseDelayMs ?? 1e3;
107
+ this.#maxDelayMs = options.maxDelayMs ?? 3e4;
108
+ }
109
+ async export(items, signal) {
110
+ if (items.length === 0) return;
111
+ const apiKey = await this.#resolveApiKey();
112
+ if (!apiKey) {
113
+ throw new Error(
114
+ "OpenAI API key is required. Set OPENAI_API_KEY or pass apiKey option."
115
+ );
116
+ }
117
+ const headers = {
118
+ "Content-Type": "application/json",
119
+ Authorization: `Bearer ${apiKey}`,
120
+ "OpenAI-Beta": "traces=v1"
121
+ };
122
+ if (this.#organization) {
123
+ headers["OpenAI-Organization"] = this.#organization;
124
+ }
125
+ if (this.#project) {
126
+ headers["OpenAI-Project"] = this.#project;
127
+ }
128
+ const body = JSON.stringify({ data: items });
129
+ await this.#fetchWithRetry(this.#endpoint, {
130
+ method: "POST",
131
+ headers,
132
+ body,
133
+ signal
134
+ });
135
+ }
136
+ async #resolveApiKey() {
137
+ return typeof this.#apiKey === "function" ? await this.#apiKey() : this.#apiKey;
138
+ }
139
+ async #fetchWithRetry(url, init) {
140
+ let lastError;
141
+ for (let attempt = 0; attempt < this.#maxRetries; attempt++) {
142
+ try {
143
+ const response = await fetch(url, init);
144
+ if (response.ok) return response;
145
+ if (response.status >= 400 && response.status < 500) {
146
+ const text = await response.text().catch(() => "");
147
+ throw new OpenAIExportError(response.status, text);
148
+ }
149
+ lastError = new Error(`Server error ${response.status}`);
150
+ } catch (error) {
151
+ if (error instanceof OpenAIExportError) {
152
+ throw error;
153
+ }
154
+ lastError = error;
155
+ }
156
+ if (attempt < this.#maxRetries - 1) {
157
+ const delay = Math.min(
158
+ this.#baseDelayMs * 2 ** attempt,
159
+ this.#maxDelayMs
160
+ );
161
+ const jitter = delay * (0.9 + Math.random() * 0.2);
162
+ await new Promise((r) => setTimeout(r, jitter));
163
+ }
164
+ }
165
+ throw lastError;
166
+ }
167
+ };
168
+
169
+ // packages/context/src/lib/tracing/ids.ts
170
+ import { randomUUID } from "node:crypto";
171
+ function traceId() {
172
+ return `trace_${randomUUID().replace(/-/g, "")}`;
173
+ }
174
+ function spanId() {
175
+ return `span_${randomUUID().replace(/-/g, "").slice(0, 24)}`;
176
+ }
177
+ function groupId() {
178
+ return `group_${randomUUID().replace(/-/g, "").slice(0, 24)}`;
179
+ }
180
+
181
+ // packages/context/src/lib/tracing/processor.ts
182
+ var CompositeTraceProcessor = class {
183
+ #processors;
184
+ constructor(processors) {
185
+ this.#processors = processors.filter(Boolean);
186
+ }
187
+ async start() {
188
+ for (const processor of this.#processors) {
189
+ await processor.start?.();
190
+ }
191
+ }
192
+ async onTraceStart(trace) {
193
+ for (const processor of this.#processors) {
194
+ await processor.onTraceStart?.(trace);
195
+ }
196
+ }
197
+ async onTraceEnd(trace) {
198
+ for (const processor of this.#processors) {
199
+ await processor.onTraceEnd?.(trace);
200
+ }
201
+ }
202
+ async onSpanStart(span) {
203
+ for (const processor of this.#processors) {
204
+ await processor.onSpanStart?.(span);
205
+ }
206
+ }
207
+ async onSpanEnd(span) {
208
+ for (const processor of this.#processors) {
209
+ await processor.onSpanEnd?.(span);
210
+ }
211
+ }
212
+ async forceFlush() {
213
+ for (const processor of this.#processors) {
214
+ await processor.forceFlush?.();
215
+ }
216
+ }
217
+ async shutdown() {
218
+ for (const processor of this.#processors) {
219
+ await processor.shutdown?.();
220
+ }
221
+ }
222
+ };
223
+ function createTracingProcessor(processors) {
224
+ if (processors == null) {
225
+ return void 0;
226
+ }
227
+ const list = Array.isArray(processors) ? processors : [processors];
228
+ if (list.length === 0) {
229
+ return void 0;
230
+ }
231
+ return list.length === 1 ? list[0] : new CompositeTraceProcessor(list);
232
+ }
233
+
234
+ // packages/context/src/lib/tracing/serialization.ts
235
+ function normalizeForJson(value) {
236
+ if (value == null) {
237
+ return value;
238
+ }
239
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
240
+ return value;
241
+ }
242
+ if (typeof value === "bigint") {
243
+ return value.toString();
244
+ }
245
+ if (value instanceof Date) {
246
+ return value.toISOString();
247
+ }
248
+ if (value instanceof Error) {
249
+ return errorToSpanError(value);
250
+ }
251
+ if (Array.isArray(value)) {
252
+ return value.map((item) => normalizeForJson(item)).filter((item) => item !== void 0);
253
+ }
254
+ if (typeof value === "object") {
255
+ const normalized = {};
256
+ for (const [key, entry] of Object.entries(
257
+ value
258
+ )) {
259
+ const result = normalizeForJson(entry);
260
+ if (result !== void 0) {
261
+ normalized[key] = result;
262
+ }
263
+ }
264
+ return normalized;
265
+ }
266
+ return String(value);
267
+ }
268
+ function normalizeRecordArray(value) {
269
+ const normalized = normalizeForJson(value);
270
+ if (!Array.isArray(normalized)) {
271
+ return void 0;
272
+ }
273
+ return normalized.map(
274
+ (item) => typeof item === "object" && item != null ? item : { value: item }
275
+ );
276
+ }
277
+ function normalizeUsage(usage) {
278
+ if (usage == null) {
279
+ return void 0;
280
+ }
281
+ const details = {};
282
+ if (hasDefinedValue(usage.inputTokenDetails)) {
283
+ details.input_token_details = normalizeForJson({
284
+ no_cache_tokens: usage.inputTokenDetails.noCacheTokens,
285
+ cache_read_tokens: usage.inputTokenDetails.cacheReadTokens,
286
+ cache_write_tokens: usage.inputTokenDetails.cacheWriteTokens
287
+ });
288
+ }
289
+ if (hasDefinedValue(usage.outputTokenDetails)) {
290
+ details.output_token_details = normalizeForJson({
291
+ text_tokens: usage.outputTokenDetails.textTokens,
292
+ reasoning_tokens: usage.outputTokenDetails.reasoningTokens
293
+ });
294
+ }
295
+ if (usage.raw != null) {
296
+ details.raw = normalizeForJson(usage.raw);
297
+ }
298
+ if (usage.reasoningTokens !== void 0) {
299
+ details.reasoning_tokens = usage.reasoningTokens;
300
+ }
301
+ if (usage.cachedInputTokens !== void 0) {
302
+ details.cached_input_tokens = usage.cachedInputTokens;
303
+ }
304
+ return {
305
+ input_tokens: usage.inputTokens,
306
+ output_tokens: usage.outputTokens,
307
+ total_tokens: usage.totalTokens,
308
+ ...Object.keys(details).length > 0 ? { details } : {}
309
+ };
310
+ }
311
+ function errorToSpanError(error) {
312
+ if (error instanceof Error) {
313
+ const data = normalizeForJson({
314
+ name: error.name,
315
+ stack: error.stack,
316
+ cause: error.cause
317
+ });
318
+ return {
319
+ message: error.message,
320
+ ...Object.keys(data).length > 0 ? { data } : {}
321
+ };
322
+ }
323
+ return {
324
+ message: String(error)
325
+ };
326
+ }
327
+ function hasDefinedValue(value) {
328
+ if (value == null) {
329
+ return false;
330
+ }
331
+ return Object.values(value).some((entry) => entry !== void 0);
332
+ }
333
+
334
+ // packages/context/src/lib/tracing/openai-traces-integration.ts
335
+ function createOpenAITracesIntegration(options = {}) {
336
+ if (process.env.OPENAI_AGENTS_DISABLE_TRACING === "1") {
337
+ return {};
338
+ }
339
+ const exporter = options.exporter ?? new OpenAITracesExporter({
340
+ apiKey: options.apiKey,
341
+ baseURL: options.baseURL,
342
+ endpoint: options.endpoint,
343
+ organization: options.organization,
344
+ project: options.project
345
+ });
346
+ const processor = createTracingProcessor(options.processor) ?? new BatchTraceProcessor(exporter, options.batch);
347
+ const includeSensitive = options.includeSensitiveData ?? process.env.OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA !== "0";
348
+ const openRuns = [];
349
+ const responseIdToRun = /* @__PURE__ */ new Map();
350
+ const toolCallIdToRun = /* @__PURE__ */ new Map();
351
+ void processor.start?.();
352
+ return {
353
+ onStart: async (event) => {
354
+ const trace = {
355
+ object: "trace",
356
+ id: traceId(),
357
+ workflow_name: options.workflowName ?? event.functionId ?? "ai-sdk-workflow",
358
+ group_id: options.groupId ?? null,
359
+ metadata: normalizeMetadata({
360
+ ...options.metadata,
361
+ ...event.metadata
362
+ })
363
+ };
364
+ const rootSpan = {
365
+ object: "trace.span",
366
+ id: spanId(),
367
+ trace_id: trace.id,
368
+ parent_id: null,
369
+ started_at: now(),
370
+ span_data: {
371
+ type: "agent",
372
+ name: trace.workflow_name,
373
+ tools: event.tools ? Object.keys(event.tools) : void 0,
374
+ output_type: getOutputType(event.output)
375
+ }
376
+ };
377
+ const state = {
378
+ trace,
379
+ rootSpan,
380
+ functionId: event.functionId,
381
+ modelKey: toModelKey(event.model),
382
+ inputFingerprint: fingerprintInput({
383
+ system: event.system,
384
+ prompt: event.prompt,
385
+ messages: event.messages
386
+ }),
387
+ metadataRef: asObjectRef(event.metadata),
388
+ contextRef: asObjectRef(event.experimental_context),
389
+ abortSignal: event.abortSignal,
390
+ responseIds: /* @__PURE__ */ new Set(),
391
+ stepSpans: [],
392
+ spansById: /* @__PURE__ */ new Map([[rootSpan.id, rootSpan]]),
393
+ toolCallSpans: /* @__PURE__ */ new Map()
394
+ };
395
+ openRuns.push(state);
396
+ await processor.onTraceStart?.(trace);
397
+ await processor.onSpanStart?.(rootSpan);
398
+ },
399
+ onStepStart: async (event) => {
400
+ const state = resolveRunState(event);
401
+ if (state == null) {
402
+ return;
403
+ }
404
+ const span = {
405
+ object: "trace.span",
406
+ id: spanId(),
407
+ trace_id: state.trace.id,
408
+ parent_id: currentParentId(state),
409
+ started_at: now(),
410
+ span_data: {
411
+ type: "generation",
412
+ model: event.model.modelId,
413
+ model_config: normalizeMetadata({
414
+ provider: event.model.provider,
415
+ tool_choice: event.toolChoice,
416
+ active_tools: event.activeTools,
417
+ provider_options: event.providerOptions
418
+ }),
419
+ ...includeSensitive ? { input: normalizeRecordArray(event.messages) } : {}
420
+ }
421
+ };
422
+ state.stepSpans.push(span.id);
423
+ state.spansById.set(span.id, span);
424
+ await processor.onSpanStart?.(span);
425
+ },
426
+ onToolCallStart: async (event) => {
427
+ const state = resolveRunState(event);
428
+ if (state == null) {
429
+ return;
430
+ }
431
+ const span = {
432
+ object: "trace.span",
433
+ id: spanId(),
434
+ trace_id: state.trace.id,
435
+ parent_id: currentParentId(state),
436
+ started_at: now(),
437
+ span_data: {
438
+ type: "function",
439
+ name: event.toolCall.toolName,
440
+ ...includeSensitive ? {
441
+ input: normalizeForJson(event.toolCall.input)
442
+ } : {}
443
+ }
444
+ };
445
+ state.toolCallSpans.set(event.toolCall.toolCallId, span.id);
446
+ toolCallIdToRun.set(event.toolCall.toolCallId, state);
447
+ state.spansById.set(span.id, span);
448
+ await processor.onSpanStart?.(span);
449
+ },
450
+ onToolCallFinish: async (event) => {
451
+ const state = toolCallIdToRun.get(event.toolCall.toolCallId) ?? resolveRunState(event);
452
+ if (state == null) {
453
+ return;
454
+ }
455
+ const id = state.toolCallSpans.get(event.toolCall.toolCallId);
456
+ if (id == null) {
457
+ return;
458
+ }
459
+ state.toolCallSpans.delete(event.toolCall.toolCallId);
460
+ toolCallIdToRun.delete(event.toolCall.toolCallId);
461
+ const span = state.spansById.get(id);
462
+ if (span == null) {
463
+ return;
464
+ }
465
+ span.ended_at = now();
466
+ const data = span.span_data;
467
+ if (includeSensitive && event.success) {
468
+ data.output = normalizeForJson(event.output);
469
+ }
470
+ if (!event.success) {
471
+ span.error = errorToSpanError(event.error);
472
+ }
473
+ await processor.onSpanEnd?.(span);
474
+ },
475
+ onStepFinish: async (event) => {
476
+ const state = resolveRunState(event);
477
+ if (state == null) {
478
+ return;
479
+ }
480
+ const id = state.stepSpans.pop();
481
+ if (id == null) {
482
+ return;
483
+ }
484
+ const span = state.spansById.get(id);
485
+ if (span == null) {
486
+ return;
487
+ }
488
+ span.ended_at = now();
489
+ const data = span.span_data;
490
+ if (includeSensitive) {
491
+ data.output = normalizeRecordArray(event.response.messages);
492
+ }
493
+ data.usage = normalizeUsage(event.usage);
494
+ state.responseIds.add(event.response.id);
495
+ responseIdToRun.set(event.response.id, state);
496
+ await processor.onSpanEnd?.(span);
497
+ },
498
+ onFinish: async (event) => {
499
+ const state = resolveRunState(event);
500
+ if (state == null) {
501
+ return;
502
+ }
503
+ try {
504
+ while (state.stepSpans.length > 0) {
505
+ const openStepId = state.stepSpans.pop();
506
+ if (openStepId == null) {
507
+ continue;
508
+ }
509
+ const openStep = state.spansById.get(openStepId);
510
+ if (openStep == null || openStep.ended_at != null) {
511
+ continue;
512
+ }
513
+ openStep.ended_at = now();
514
+ await processor.onSpanEnd?.(openStep);
515
+ }
516
+ state.rootSpan.ended_at = now();
517
+ state.trace.metadata = normalizeMetadata({
518
+ ...state.trace.metadata,
519
+ total_usage: normalizeUsage(event.totalUsage),
520
+ steps: event.steps.length,
521
+ finish_reason: event.finishReason
522
+ });
523
+ await processor.onSpanEnd?.(state.rootSpan);
524
+ await processor.onTraceEnd?.(state.trace);
525
+ await processor.forceFlush?.();
526
+ } finally {
527
+ closeRunState(state);
528
+ }
529
+ }
530
+ };
531
+ function resolveRunState(event) {
532
+ if (event.response?.id != null) {
533
+ const state = responseIdToRun.get(event.response.id);
534
+ if (state != null) {
535
+ return state;
536
+ }
537
+ }
538
+ if (event.toolCall?.toolCallId != null) {
539
+ const state = toolCallIdToRun.get(event.toolCall.toolCallId);
540
+ if (state != null) {
541
+ return state;
542
+ }
543
+ }
544
+ if (openRuns.length === 0) {
545
+ return void 0;
546
+ }
547
+ if (openRuns.length === 1) {
548
+ return openRuns[0];
549
+ }
550
+ let bestState;
551
+ let bestScore = 0;
552
+ const eventMetadataRef = asObjectRef(event.metadata);
553
+ const eventContextRef = asObjectRef(event.experimental_context);
554
+ const eventModelKey = event.model != null ? toModelKey(event.model) : void 0;
555
+ const eventFingerprint = fingerprintInput({
556
+ system: event.system,
557
+ prompt: event.prompt,
558
+ messages: event.messages
559
+ });
560
+ for (const state of openRuns) {
561
+ let score = 0;
562
+ if (eventContextRef != null && state.contextRef === eventContextRef) {
563
+ score += 1e3;
564
+ }
565
+ if (eventMetadataRef != null && state.metadataRef === eventMetadataRef) {
566
+ score += 800;
567
+ }
568
+ if (event.abortSignal != null && state.abortSignal != null && state.abortSignal === event.abortSignal) {
569
+ score += 600;
570
+ }
571
+ if (event.functionId != null && state.functionId === event.functionId) {
572
+ score += 300;
573
+ }
574
+ if (eventModelKey != null && state.modelKey === eventModelKey) {
575
+ score += 100;
576
+ }
577
+ if (eventFingerprint != null && state.inputFingerprint != null && state.inputFingerprint === eventFingerprint) {
578
+ score += 200;
579
+ }
580
+ if (score > bestScore) {
581
+ bestScore = score;
582
+ bestState = state;
583
+ }
584
+ }
585
+ return bestState;
586
+ }
587
+ function closeRunState(state) {
588
+ const index = openRuns.indexOf(state);
589
+ if (index >= 0) {
590
+ openRuns.splice(index, 1);
591
+ }
592
+ for (const responseId of state.responseIds) {
593
+ responseIdToRun.delete(responseId);
594
+ }
595
+ for (const toolCallId of state.toolCallSpans.keys()) {
596
+ toolCallIdToRun.delete(toolCallId);
597
+ }
598
+ }
599
+ }
600
+ function currentParentId(state) {
601
+ return state.stepSpans[state.stepSpans.length - 1] ?? state.rootSpan.id;
602
+ }
603
+ function normalizeMetadata(metadata) {
604
+ if (metadata == null) {
605
+ return void 0;
606
+ }
607
+ const normalized = normalizeForJson(metadata);
608
+ if (normalized == null || typeof normalized !== "object" || Array.isArray(normalized)) {
609
+ return void 0;
610
+ }
611
+ return Object.keys(normalized).length > 0 ? normalized : void 0;
612
+ }
613
+ function getOutputType(output) {
614
+ if (output == null) {
615
+ return void 0;
616
+ }
617
+ if (typeof output === "object" && "type" in output && typeof output.type === "string") {
618
+ return output.type;
619
+ }
620
+ if (typeof output === "object" && output.constructor?.name != null) {
621
+ return output.constructor.name;
622
+ }
623
+ return typeof output;
624
+ }
625
+ function now() {
626
+ return (/* @__PURE__ */ new Date()).toISOString();
627
+ }
628
+ function toModelKey(model) {
629
+ return `${model.provider}:${model.modelId}`;
630
+ }
631
+ function fingerprintInput(input) {
632
+ const normalizedMessages = input.messages ?? (typeof input.prompt === "string" ? [
633
+ {
634
+ role: "user",
635
+ content: [{ type: "text", text: input.prompt }]
636
+ }
637
+ ] : input.prompt);
638
+ const normalized = normalizeForJson({
639
+ system: input.system,
640
+ messages: normalizedMessages
641
+ });
642
+ if (normalized == null) {
643
+ return void 0;
644
+ }
645
+ return JSON.stringify(normalized);
646
+ }
647
+ function asObjectRef(value) {
648
+ return value != null && typeof value === "object" ? value : void 0;
649
+ }
650
+ export {
651
+ BatchTraceProcessor,
652
+ CompositeTraceProcessor,
653
+ OpenAIExportError,
654
+ OpenAITracesExporter,
655
+ createOpenAITracesIntegration,
656
+ createTracingProcessor,
657
+ groupId,
658
+ spanId,
659
+ traceId
660
+ };
661
+ //# sourceMappingURL=index.js.map