@ai-sdk/otel 1.0.0-beta.13 → 1.0.0-beta.131

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.
package/dist/index.js CHANGED
@@ -1,31 +1,1620 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ // src/open-telemetry.ts
2
+ import {
3
+ context as context2,
4
+ SpanKind,
5
+ trace
6
+ } from "@opentelemetry/api";
7
+
8
+ // src/gen-ai-format-messages.ts
9
+ import { convertDataContentToBase64String } from "ai";
10
+ function mapProviderName(provider) {
11
+ const lower = provider.toLowerCase();
12
+ const wellKnownPrefixes = [
13
+ ["google.vertex", "gcp.vertex_ai"],
14
+ ["google.generative-ai", "gcp.gemini"],
15
+ ["google-vertex", "gcp.vertex_ai"],
16
+ ["amazon-bedrock", "aws.bedrock"],
17
+ ["azure-openai", "azure.ai.openai"],
18
+ ["anthropic", "anthropic"],
19
+ ["openai", "openai"],
20
+ ["azure", "azure.ai.inference"],
21
+ ["google", "gcp.gemini"],
22
+ ["mistral", "mistral_ai"],
23
+ ["cohere", "cohere"],
24
+ ["bedrock", "aws.bedrock"],
25
+ ["groq", "groq"],
26
+ ["deepseek", "deepseek"],
27
+ ["perplexity", "perplexity"],
28
+ ["xai", "x_ai"]
29
+ ];
30
+ for (const [prefix, mapped] of wellKnownPrefixes) {
31
+ if (lower === prefix || lower.startsWith(prefix + ".") || lower.startsWith(prefix + "-")) {
32
+ return mapped;
33
+ }
34
+ }
35
+ return provider;
36
+ }
37
+ function mapOperationName(operationId) {
38
+ var _a;
39
+ const mapping = {
40
+ "ai.generateText": "invoke_agent",
41
+ "ai.streamText": "invoke_agent",
42
+ "ai.generateObject": "invoke_agent",
43
+ "ai.streamObject": "invoke_agent",
44
+ "ai.embed": "embeddings",
45
+ "ai.embedMany": "embeddings",
46
+ "ai.rerank": "rerank"
47
+ };
48
+ return (_a = mapping[operationId]) != null ? _a : operationId;
49
+ }
50
+ function formatSystemInstructions(system) {
51
+ if (typeof system === "string") {
52
+ return [{ type: "text", content: system }];
53
+ }
54
+ if (Array.isArray(system)) {
55
+ return system.map((msg) => ({ type: "text", content: msg.content }));
56
+ }
57
+ return [{ type: "text", content: system.content }];
58
+ }
59
+ function convertMessagePartToSemConv(part) {
60
+ var _a, _b, _c, _d, _e;
61
+ switch (part.type) {
62
+ case "text":
63
+ return { type: "text", content: part.text };
64
+ case "reasoning":
65
+ return { type: "reasoning", content: part.text };
66
+ case "tool-call":
67
+ return {
68
+ type: "tool_call",
69
+ id: (_a = part.toolCallId) != null ? _a : null,
70
+ name: part.toolName,
71
+ arguments: part.input
72
+ };
73
+ case "tool-result": {
74
+ const output = part.output;
75
+ let response;
76
+ if (output) {
77
+ if (output.type === "text" || output.type === "error-text") {
78
+ response = output.value;
79
+ } else if (output.type === "json" || output.type === "error-json") {
80
+ response = output.value;
81
+ } else if (output.type === "execution-denied") {
82
+ response = { denied: true, reason: output.reason };
83
+ } else {
84
+ response = output;
85
+ }
86
+ }
87
+ return {
88
+ type: "tool_call_response",
89
+ id: (_b = part.toolCallId) != null ? _b : null,
90
+ response
91
+ };
92
+ }
93
+ case "file": {
94
+ const rawData = part.data;
95
+ const data = (() => {
96
+ if (typeof rawData === "object" && rawData !== null && !(rawData instanceof URL) && !(rawData instanceof Uint8Array) && !(rawData instanceof ArrayBuffer) && "type" in rawData) {
97
+ switch (rawData.type) {
98
+ case "data":
99
+ return rawData.data;
100
+ case "url":
101
+ return rawData.url;
102
+ case "text":
103
+ return rawData.text;
104
+ default:
105
+ return "";
106
+ }
107
+ }
108
+ return rawData;
109
+ })();
110
+ let content;
111
+ if (data instanceof Uint8Array) {
112
+ content = convertDataContentToBase64String(data);
113
+ } else if (typeof data === "string") {
114
+ if (data.startsWith("http://") || data.startsWith("https://")) {
115
+ return {
116
+ type: "uri",
117
+ modality: getModality(part.mediaType),
118
+ mime_type: (_c = part.mediaType) != null ? _c : null,
119
+ uri: data
120
+ };
121
+ }
122
+ content = data;
123
+ } else if (data instanceof URL) {
124
+ return {
125
+ type: "uri",
126
+ modality: getModality(part.mediaType),
127
+ mime_type: (_d = part.mediaType) != null ? _d : null,
128
+ uri: data.toString()
129
+ };
130
+ } else {
131
+ content = String(data);
132
+ }
133
+ return {
134
+ type: "blob",
135
+ modality: getModality(part.mediaType),
136
+ mime_type: (_e = part.mediaType) != null ? _e : null,
137
+ content
138
+ };
139
+ }
140
+ case "tool-approval-response":
141
+ return {
142
+ type: "tool_approval_response",
143
+ approval_id: part.approvalId,
144
+ approved: part.approved,
145
+ reason: part.reason
146
+ };
147
+ case "custom":
148
+ return { type: "custom", kind: part.kind };
149
+ case "reasoning-file":
150
+ return { type: String(part.type) };
151
+ default: {
152
+ const _exhaustive = part;
153
+ return { type: String(_exhaustive.type) };
154
+ }
155
+ }
156
+ }
157
+ function getModality(mediaType) {
158
+ if (!mediaType) return "image";
159
+ if (mediaType.startsWith("image/")) return "image";
160
+ if (mediaType.startsWith("video/")) return "video";
161
+ if (mediaType.startsWith("audio/")) return "audio";
162
+ return "image";
163
+ }
164
+ function formatInputMessages(prompt) {
165
+ return prompt.filter((msg) => msg.role !== "system").map((message) => {
166
+ if (message.role === "system") {
167
+ return {
168
+ role: "system",
169
+ parts: [{ type: "text", content: message.content }]
170
+ };
171
+ }
172
+ const parts = message.content.map(convertMessagePartToSemConv);
173
+ return { role: message.role, parts };
174
+ });
175
+ }
176
+ function formatModelMessages({
177
+ prompt,
178
+ messages
179
+ }) {
180
+ const result = [];
181
+ if (typeof prompt === "string") {
182
+ result.push({
183
+ role: "user",
184
+ parts: [{ type: "text", content: prompt }]
185
+ });
186
+ } else if (Array.isArray(prompt)) {
187
+ for (const msg of prompt) {
188
+ const converted = convertModelMessageToSemConv(msg);
189
+ if (converted) result.push(converted);
190
+ }
191
+ }
192
+ if (messages) {
193
+ for (const msg of messages) {
194
+ const converted = convertModelMessageToSemConv(msg);
195
+ if (converted) result.push(converted);
196
+ }
197
+ }
198
+ return result;
199
+ }
200
+ function convertModelMessageToSemConv(msg) {
201
+ if (msg.role === "system") return void 0;
202
+ if (msg.role === "user") {
203
+ if (typeof msg.content === "string") {
204
+ return {
205
+ role: "user",
206
+ parts: [{ type: "text", content: msg.content }]
207
+ };
208
+ }
209
+ const parts = msg.content.map((part) => {
210
+ var _a, _b, _c, _d, _e, _f, _g, _h;
211
+ switch (part.type) {
212
+ case "text":
213
+ return { type: "text", content: part.text };
214
+ case "image": {
215
+ const data = part.image;
216
+ if (data instanceof URL) {
217
+ return {
218
+ type: "uri",
219
+ modality: "image",
220
+ mime_type: (_a = part.mediaType) != null ? _a : null,
221
+ uri: data.toString()
222
+ };
223
+ }
224
+ if (typeof data === "string") {
225
+ if (data.startsWith("http://") || data.startsWith("https://")) {
226
+ return {
227
+ type: "uri",
228
+ modality: "image",
229
+ mime_type: (_b = part.mediaType) != null ? _b : null,
230
+ uri: data
231
+ };
232
+ }
233
+ return {
234
+ type: "blob",
235
+ modality: "image",
236
+ mime_type: (_c = part.mediaType) != null ? _c : null,
237
+ content: data
238
+ };
239
+ }
240
+ return {
241
+ type: "blob",
242
+ modality: "image",
243
+ mime_type: (_d = part.mediaType) != null ? _d : null,
244
+ content: convertDataContentToBase64String(data)
245
+ };
246
+ }
247
+ case "file": {
248
+ const rawData = part.data;
249
+ const data = (() => {
250
+ if (typeof rawData === "object" && rawData !== null && !(rawData instanceof URL) && !(rawData instanceof Uint8Array) && !(rawData instanceof ArrayBuffer) && "type" in rawData) {
251
+ switch (rawData.type) {
252
+ case "data":
253
+ return rawData.data;
254
+ case "url":
255
+ return rawData.url;
256
+ case "text":
257
+ return rawData.text;
258
+ default:
259
+ return "";
260
+ }
261
+ }
262
+ return rawData;
263
+ })();
264
+ if (data instanceof URL) {
265
+ return {
266
+ type: "uri",
267
+ modality: getModality(part.mediaType),
268
+ mime_type: (_e = part.mediaType) != null ? _e : null,
269
+ uri: data.toString()
270
+ };
271
+ }
272
+ if (typeof data === "string") {
273
+ if (data.startsWith("http://") || data.startsWith("https://")) {
274
+ return {
275
+ type: "uri",
276
+ modality: getModality(part.mediaType),
277
+ mime_type: (_f = part.mediaType) != null ? _f : null,
278
+ uri: data
279
+ };
280
+ }
281
+ return {
282
+ type: "blob",
283
+ modality: getModality(part.mediaType),
284
+ mime_type: (_g = part.mediaType) != null ? _g : null,
285
+ content: data
286
+ };
287
+ }
288
+ return {
289
+ type: "blob",
290
+ modality: getModality(part.mediaType),
291
+ mime_type: (_h = part.mediaType) != null ? _h : null,
292
+ content: convertDataContentToBase64String(data)
293
+ };
294
+ }
295
+ default:
296
+ return { type: String(part.type) };
297
+ }
298
+ });
299
+ return { role: "user", parts };
300
+ }
301
+ if (msg.role === "assistant") {
302
+ if (typeof msg.content === "string") {
303
+ return {
304
+ role: "assistant",
305
+ parts: [{ type: "text", content: msg.content }]
306
+ };
307
+ }
308
+ const parts = msg.content.map((part) => {
309
+ var _a, _b;
310
+ switch (part.type) {
311
+ case "text":
312
+ return { type: "text", content: part.text };
313
+ case "reasoning":
314
+ return { type: "reasoning", content: part.text };
315
+ case "tool-call":
316
+ return {
317
+ type: "tool_call",
318
+ id: (_a = part.toolCallId) != null ? _a : null,
319
+ name: part.toolName,
320
+ arguments: part.input
321
+ };
322
+ case "tool-result": {
323
+ const output = part.output;
324
+ let response;
325
+ if (output) {
326
+ if (output.type === "text" || output.type === "error-text") {
327
+ response = output.value;
328
+ } else if (output.type === "json" || output.type === "error-json") {
329
+ response = output.value;
330
+ } else if (output.type === "execution-denied") {
331
+ response = { denied: true, reason: output.reason };
332
+ } else {
333
+ response = output;
334
+ }
335
+ }
336
+ return {
337
+ type: "tool_call_response",
338
+ id: (_b = part.toolCallId) != null ? _b : null,
339
+ response
340
+ };
341
+ }
342
+ default:
343
+ return { type: String(part.type) };
344
+ }
345
+ });
346
+ return { role: "assistant", parts };
347
+ }
348
+ if (msg.role === "tool") {
349
+ const parts = msg.content.map((part) => {
350
+ var _a;
351
+ if (part.type === "tool-result") {
352
+ const output = part.output;
353
+ let response;
354
+ if (output) {
355
+ if (output.type === "text" || output.type === "error-text") {
356
+ response = output.value;
357
+ } else if (output.type === "json" || output.type === "error-json") {
358
+ response = output.value;
359
+ } else if (output.type === "execution-denied") {
360
+ response = { denied: true, reason: output.reason };
361
+ } else {
362
+ response = output;
363
+ }
364
+ }
365
+ return {
366
+ type: "tool_call_response",
367
+ id: (_a = part.toolCallId) != null ? _a : null,
368
+ response
369
+ };
370
+ }
371
+ return { type: String(part.type) };
372
+ });
373
+ return { role: "tool", parts };
374
+ }
375
+ return void 0;
376
+ }
377
+ function formatOutputMessages({
378
+ text,
379
+ reasoning,
380
+ toolCalls,
381
+ files,
382
+ finishReason
383
+ }) {
384
+ const parts = [];
385
+ if (reasoning) {
386
+ for (const r of reasoning) {
387
+ if ("text" in r && r.text) {
388
+ parts.push({ type: "reasoning", content: r.text });
389
+ }
390
+ }
391
+ }
392
+ if (text != null && text.length > 0) {
393
+ parts.push({ type: "text", content: text });
394
+ }
395
+ if (toolCalls) {
396
+ for (const tc of toolCalls) {
397
+ parts.push({
398
+ type: "tool_call",
399
+ id: tc.toolCallId,
400
+ name: tc.toolName,
401
+ arguments: tc.input
402
+ });
403
+ }
404
+ }
405
+ if (files) {
406
+ for (const file of files) {
407
+ parts.push({
408
+ type: "blob",
409
+ modality: getModality(file.mediaType),
410
+ mime_type: file.mediaType,
411
+ content: file.base64
412
+ });
413
+ }
414
+ }
415
+ return [
416
+ {
417
+ role: "assistant",
418
+ parts,
419
+ finish_reason: mapFinishReason(finishReason)
420
+ }
421
+ ];
422
+ }
423
+ function formatObjectOutputMessages({
424
+ objectText,
425
+ finishReason
426
+ }) {
427
+ return [
428
+ {
429
+ role: "assistant",
430
+ parts: [{ type: "text", content: objectText }],
431
+ finish_reason: mapFinishReason(finishReason)
432
+ }
433
+ ];
434
+ }
435
+ function mapFinishReason(reason) {
436
+ var _a;
437
+ const mapping = {
438
+ stop: "stop",
439
+ length: "length",
440
+ "content-filter": "content_filter",
441
+ "tool-calls": "tool_call",
442
+ error: "error",
443
+ other: "stop",
444
+ unknown: "stop"
445
+ };
446
+ return (_a = mapping[reason]) != null ? _a : reason;
447
+ }
448
+
449
+ // src/record-span.ts
450
+ import {
451
+ SpanStatusCode,
452
+ context
453
+ } from "@opentelemetry/api";
454
+ function recordErrorOnSpan(span, error) {
455
+ if (error instanceof Error) {
456
+ span.recordException({
457
+ name: error.name,
458
+ message: error.message,
459
+ stack: error.stack
460
+ });
461
+ span.setStatus({
462
+ code: SpanStatusCode.ERROR,
463
+ message: error.message
464
+ });
465
+ } else {
466
+ span.setStatus({ code: SpanStatusCode.ERROR });
467
+ }
468
+ }
469
+
470
+ // src/sanitize-attribute-value.ts
471
+ function isPrimitiveAttributeValue(value) {
472
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
473
+ }
474
+ function sanitizeAttributeValue(value) {
475
+ if (!Array.isArray(value)) {
476
+ return value;
477
+ }
478
+ const primitiveTypes = new Set(
479
+ value.filter(isPrimitiveAttributeValue).map((item) => typeof item)
480
+ );
481
+ if (primitiveTypes.size !== 1) {
482
+ return void 0;
483
+ }
484
+ const [primitiveType] = primitiveTypes;
485
+ if (primitiveType === "string") {
486
+ return value.filter((item) => typeof item === "string");
487
+ }
488
+ if (primitiveType === "number") {
489
+ return value.filter((item) => typeof item === "number");
490
+ }
491
+ return value.filter((item) => typeof item === "boolean");
492
+ }
493
+ function sanitizeAttributes(attributes) {
494
+ const result = {};
495
+ for (const [key, value] of Object.entries(attributes != null ? attributes : {})) {
496
+ if (value == null) continue;
497
+ const sanitized = sanitizeAttributeValue(value);
498
+ if (sanitized != null) result[key] = sanitized;
499
+ }
500
+ return result;
501
+ }
502
+
503
+ // src/select-attributes.ts
504
+ function shouldRecord(telemetry) {
505
+ return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
506
+ }
507
+ function selectAttributes(telemetry, attributes) {
508
+ if (!shouldRecord(telemetry)) {
509
+ return {};
510
+ }
511
+ const result = {};
512
+ for (const [key, value] of Object.entries(attributes)) {
513
+ if (value == null) continue;
514
+ if (typeof value === "object" && "input" in value && typeof value.input === "function") {
515
+ if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
516
+ const resolved = value.input();
517
+ if (resolved != null) {
518
+ const sanitized2 = sanitizeAttributeValue(resolved);
519
+ if (sanitized2 != null) result[key] = sanitized2;
520
+ }
521
+ continue;
522
+ }
523
+ if (typeof value === "object" && "output" in value && typeof value.output === "function") {
524
+ if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
525
+ const resolved = value.output();
526
+ if (resolved != null) {
527
+ const sanitized2 = sanitizeAttributeValue(resolved);
528
+ if (sanitized2 != null) result[key] = sanitized2;
529
+ }
530
+ continue;
531
+ }
532
+ const sanitized = sanitizeAttributeValue(value);
533
+ if (sanitized != null) result[key] = sanitized;
534
+ }
535
+ return result;
536
+ }
537
+
538
+ // src/supplemental-attributes.ts
539
+ var disabledSupplementalAttributes = {
540
+ usage: false,
541
+ providerMetadata: false,
542
+ embedding: false,
543
+ reranking: false,
544
+ runtimeContext: false,
545
+ headers: false,
546
+ toolChoice: false,
547
+ schema: false
9
548
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
549
+ function normalizeSupplementalAttributes(options) {
550
+ var _a, _b, _c, _d, _e, _f, _g, _h;
551
+ return {
552
+ ...disabledSupplementalAttributes,
553
+ usage: (_a = options.usage) != null ? _a : false,
554
+ providerMetadata: (_b = options.providerMetadata) != null ? _b : false,
555
+ embedding: (_c = options.embedding) != null ? _c : false,
556
+ reranking: (_d = options.reranking) != null ? _d : false,
557
+ runtimeContext: (_e = options.runtimeContext) != null ? _e : false,
558
+ headers: (_f = options.headers) != null ? _f : false,
559
+ toolChoice: (_g = options.toolChoice) != null ? _g : false,
560
+ schema: (_h = options.schema) != null ? _h : false
561
+ };
562
+ }
563
+ function getRuntimeContextAttributes(context4) {
564
+ const attributes = {};
565
+ for (const [key, value] of Object.entries(context4 != null ? context4 : {})) {
566
+ addRuntimeContextAttribute(attributes, `ai.settings.context.${key}`, value);
567
+ }
568
+ return attributes;
569
+ }
570
+ function addRuntimeContextAttribute(attributes, key, value) {
571
+ if (value == null) {
572
+ return;
573
+ }
574
+ if (Array.isArray(value) || typeof value !== "object") {
575
+ attributes[key] = value;
576
+ return;
577
+ }
578
+ for (const [nestedKey, nestedValue] of Object.entries(value)) {
579
+ addRuntimeContextAttribute(attributes, `${key}.${nestedKey}`, nestedValue);
580
+ }
581
+ }
582
+ function getHeaderAttributes(headers) {
583
+ return Object.fromEntries(
584
+ Object.entries(headers != null ? headers : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.request.headers.${key}`, value])
585
+ );
586
+ }
587
+ function getDetailedUsageAttributes(usage) {
588
+ var _a, _b, _c;
589
+ return {
590
+ "ai.usage.inputTokenDetails.noCacheTokens": (_a = usage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
591
+ "ai.usage.outputTokenDetails.textTokens": (_b = usage.outputTokenDetails) == null ? void 0 : _b.textTokens,
592
+ "ai.usage.outputTokenDetails.reasoningTokens": (_c = usage.outputTokenDetails) == null ? void 0 : _c.reasoningTokens
593
+ };
594
+ }
595
+ function selectSupplementalAttributes(telemetry, enabledAttributes, attributes) {
596
+ const result = {};
597
+ for (const [key, value] of Object.entries(attributes)) {
598
+ if (!enabledAttributes[key] || value == null) {
599
+ continue;
600
+ }
601
+ Object.assign(result, selectAttributes(telemetry, value));
602
+ }
603
+ return result;
604
+ }
605
+
606
+ // src/open-telemetry.ts
607
+ function msToSeconds(durationMs) {
608
+ return durationMs == null ? void 0 : durationMs / 1e3;
609
+ }
610
+ function getGenAIClientPerformanceAttributes(performance) {
611
+ var _a;
612
+ return {
613
+ "gen_ai.client.operation.duration": msToSeconds(performance.responseTimeMs),
614
+ "gen_ai.client.operation.time_to_first_chunk": msToSeconds(
615
+ performance.timeToFirstOutputMs
616
+ ),
617
+ "gen_ai.client.operation.time_per_output_chunk": msToSeconds(
618
+ (_a = performance.timeBetweenOutputChunksMs) == null ? void 0 : _a.avg
619
+ )
620
+ };
621
+ }
622
+ var OpenTelemetry = class {
623
+ constructor(options = {}) {
624
+ this.callStates = /* @__PURE__ */ new Map();
625
+ var _a;
626
+ this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
627
+ this.supplementalAttributes = normalizeSupplementalAttributes(options);
628
+ this.enrichSpan = options.enrichSpan;
629
+ }
630
+ getCallState(callId) {
631
+ return this.callStates.get(callId);
632
+ }
633
+ cleanupCallState(callId) {
634
+ this.callStates.delete(callId);
635
+ }
636
+ getSpanAttributes({
637
+ attributes,
638
+ spanType,
639
+ operationId,
640
+ callId,
641
+ runtimeContext
642
+ }) {
643
+ var _a;
644
+ let customAttributes;
645
+ try {
646
+ customAttributes = (_a = this.enrichSpan) == null ? void 0 : _a.call(this, {
647
+ spanType,
648
+ operationId,
649
+ callId,
650
+ runtimeContext
651
+ });
652
+ } catch (e) {
653
+ customAttributes = void 0;
654
+ }
655
+ return {
656
+ ...sanitizeAttributes(customAttributes),
657
+ ...attributes
658
+ };
659
+ }
660
+ executeTool({
661
+ callId,
662
+ toolCallId,
663
+ execute
664
+ }) {
665
+ var _a;
666
+ const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
667
+ if (toolSpanEntry == null) {
668
+ return execute();
669
+ }
670
+ return context2.with(toolSpanEntry.context, execute);
671
+ }
672
+ /**
673
+ * Runs the provider `doGenerate`/`doStream` call with the active model-call
674
+ * context.
675
+ */
676
+ executeLanguageModelCall({
677
+ callId,
678
+ execute
679
+ }) {
680
+ var _a;
681
+ const state = this.getCallState(callId);
682
+ const modelCallContext = (_a = state == null ? void 0 : state.inferenceContext) != null ? _a : state == null ? void 0 : state.stepContext;
683
+ if (modelCallContext == null) {
684
+ return execute();
685
+ }
686
+ return context2.with(modelCallContext, execute);
687
+ }
688
+ onStart(event) {
689
+ if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
690
+ this.onEmbedOperationStart(event);
691
+ return;
692
+ }
693
+ if (event.operationId === "ai.rerank") {
694
+ this.onRerankOperationStart(
695
+ event
696
+ );
697
+ return;
698
+ }
699
+ if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
700
+ this.onObjectOperationStart(
701
+ event
702
+ );
703
+ return;
704
+ }
705
+ this.onGenerateStart(event);
706
+ }
707
+ onGenerateStart(event) {
708
+ var _a;
709
+ const telemetry = {
710
+ recordInputs: event.recordInputs,
711
+ recordOutputs: event.recordOutputs,
712
+ functionId: event.functionId
713
+ };
714
+ const settings = {
715
+ maxOutputTokens: event.maxOutputTokens,
716
+ temperature: event.temperature,
717
+ topP: event.topP,
718
+ topK: event.topK,
719
+ presencePenalty: event.presencePenalty,
720
+ frequencyPenalty: event.frequencyPenalty,
721
+ stopSequences: event.stopSequences,
722
+ seed: event.seed,
723
+ maxRetries: event.maxRetries
724
+ };
725
+ const providerName = mapProviderName(event.provider);
726
+ const operationName = mapOperationName(event.operationId);
727
+ const runtimeContext = event.runtimeContext;
728
+ const baseSupplementalAttributes = selectSupplementalAttributes(
729
+ telemetry,
730
+ this.supplementalAttributes,
731
+ {
732
+ runtimeContext: getRuntimeContextAttributes(runtimeContext),
733
+ headers: getHeaderAttributes(event.headers)
734
+ }
735
+ );
736
+ const attributes = selectAttributes(telemetry, {
737
+ "gen_ai.operation.name": operationName,
738
+ "gen_ai.provider.name": providerName,
739
+ "gen_ai.request.model": event.modelId,
740
+ "gen_ai.agent.name": telemetry.functionId,
741
+ "gen_ai.request.frequency_penalty": event.frequencyPenalty,
742
+ "gen_ai.request.max_tokens": event.maxOutputTokens,
743
+ "gen_ai.request.presence_penalty": event.presencePenalty,
744
+ "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
745
+ "gen_ai.request.top_k": event.topK,
746
+ "gen_ai.request.top_p": event.topP,
747
+ "gen_ai.request.stop_sequences": event.stopSequences,
748
+ "gen_ai.request.seed": event.seed,
749
+ "gen_ai.system_instructions": event.instructions ? {
750
+ input: () => JSON.stringify(formatSystemInstructions(event.instructions))
751
+ } : void 0,
752
+ "gen_ai.input.messages": {
753
+ input: () => JSON.stringify(
754
+ formatModelMessages({
755
+ prompt: void 0,
756
+ messages: event.messages
757
+ })
758
+ )
759
+ },
760
+ ...baseSupplementalAttributes
761
+ });
762
+ const spanName = `${operationName} ${event.modelId}`;
763
+ const rootSpan = this.tracer.startSpan(spanName, {
764
+ attributes: this.getSpanAttributes({
765
+ attributes,
766
+ spanType: "operation",
767
+ operationId: event.operationId,
768
+ callId: event.callId,
769
+ runtimeContext
770
+ }),
771
+ kind: SpanKind.INTERNAL
772
+ });
773
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
774
+ this.callStates.set(event.callId, {
775
+ operationId: event.operationId,
776
+ telemetry,
777
+ rootSpan,
778
+ rootContext,
779
+ stepSpan: void 0,
780
+ stepContext: void 0,
781
+ inferenceSpan: void 0,
782
+ inferenceContext: void 0,
783
+ embedSpans: /* @__PURE__ */ new Map(),
784
+ rerankSpan: void 0,
785
+ toolSpans: /* @__PURE__ */ new Map(),
786
+ settings,
787
+ provider: event.provider,
788
+ modelId: event.modelId,
789
+ runtimeContext,
790
+ baseSupplementalAttributes
791
+ });
792
+ }
793
+ onObjectOperationStart(event) {
794
+ var _a;
795
+ const telemetry = {
796
+ recordInputs: event.recordInputs,
797
+ recordOutputs: event.recordOutputs,
798
+ functionId: event.functionId
799
+ };
800
+ const settings = {
801
+ maxOutputTokens: event.maxOutputTokens,
802
+ temperature: event.temperature,
803
+ topP: event.topP,
804
+ topK: event.topK,
805
+ presencePenalty: event.presencePenalty,
806
+ frequencyPenalty: event.frequencyPenalty,
807
+ seed: event.seed,
808
+ maxRetries: event.maxRetries
809
+ };
810
+ const providerName = mapProviderName(event.provider);
811
+ const operationName = mapOperationName(event.operationId);
812
+ const baseSupplementalAttributes = selectSupplementalAttributes(
813
+ telemetry,
814
+ this.supplementalAttributes,
815
+ {
816
+ headers: getHeaderAttributes(event.headers)
817
+ }
818
+ );
819
+ const attributes = selectAttributes(telemetry, {
820
+ "gen_ai.operation.name": operationName,
821
+ "gen_ai.provider.name": providerName,
822
+ "gen_ai.request.model": event.modelId,
823
+ "gen_ai.agent.name": telemetry.functionId,
824
+ "gen_ai.output.type": "json",
825
+ "gen_ai.request.frequency_penalty": event.frequencyPenalty,
826
+ "gen_ai.request.max_tokens": event.maxOutputTokens,
827
+ "gen_ai.request.presence_penalty": event.presencePenalty,
828
+ "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
829
+ "gen_ai.request.top_k": event.topK,
830
+ "gen_ai.request.top_p": event.topP,
831
+ "gen_ai.request.seed": event.seed,
832
+ "gen_ai.system_instructions": event.system ? {
833
+ input: () => JSON.stringify(formatSystemInstructions(event.system))
834
+ } : void 0,
835
+ "gen_ai.input.messages": {
836
+ input: () => JSON.stringify(
837
+ formatModelMessages({
838
+ prompt: event.prompt,
839
+ messages: event.messages
840
+ })
841
+ )
842
+ },
843
+ ...baseSupplementalAttributes,
844
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
845
+ schema: {
846
+ "ai.schema": event.schema ? { input: () => JSON.stringify(event.schema) } : void 0,
847
+ "ai.schema.name": event.schemaName,
848
+ "ai.schema.description": event.schemaDescription,
849
+ "ai.settings.output": event.output
850
+ }
851
+ })
852
+ });
853
+ const spanName = `${operationName} ${event.modelId}`;
854
+ const rootSpan = this.tracer.startSpan(spanName, {
855
+ attributes: this.getSpanAttributes({
856
+ attributes,
857
+ spanType: "operation",
858
+ operationId: event.operationId,
859
+ callId: event.callId,
860
+ runtimeContext: void 0
861
+ }),
862
+ kind: SpanKind.INTERNAL
863
+ });
864
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
865
+ this.callStates.set(event.callId, {
866
+ operationId: event.operationId,
867
+ telemetry,
868
+ rootSpan,
869
+ rootContext,
870
+ stepSpan: void 0,
871
+ stepContext: void 0,
872
+ inferenceSpan: void 0,
873
+ inferenceContext: void 0,
874
+ embedSpans: /* @__PURE__ */ new Map(),
875
+ rerankSpan: void 0,
876
+ toolSpans: /* @__PURE__ */ new Map(),
877
+ settings,
878
+ provider: event.provider,
879
+ modelId: event.modelId,
880
+ runtimeContext: void 0,
881
+ baseSupplementalAttributes
882
+ });
883
+ }
884
+ /** @deprecated */
885
+ onObjectStepStart(event) {
886
+ var _a;
887
+ const state = this.getCallState(event.callId);
888
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
889
+ const { telemetry } = state;
890
+ const providerName = mapProviderName(event.provider);
891
+ const attributes = selectAttributes(telemetry, {
892
+ "gen_ai.operation.name": "chat",
893
+ "gen_ai.provider.name": providerName,
894
+ "gen_ai.request.model": event.modelId,
895
+ "gen_ai.output.type": "json",
896
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
897
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
898
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
899
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
900
+ "gen_ai.request.top_k": state.settings.topK,
901
+ "gen_ai.request.top_p": state.settings.topP,
902
+ "gen_ai.input.messages": {
903
+ input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
904
+ },
905
+ ...state.baseSupplementalAttributes
906
+ });
907
+ const spanName = `chat ${event.modelId}`;
908
+ state.inferenceSpan = this.tracer.startSpan(
909
+ spanName,
910
+ {
911
+ attributes: this.getSpanAttributes({
912
+ attributes,
913
+ spanType: "languageModel",
914
+ operationId: state.operationId,
915
+ callId: event.callId,
916
+ runtimeContext: state.runtimeContext
917
+ }),
918
+ kind: SpanKind.CLIENT
919
+ },
920
+ state.rootContext
921
+ );
922
+ state.inferenceContext = trace.setSpan(
923
+ state.rootContext,
924
+ state.inferenceSpan
925
+ );
926
+ }
927
+ /** @deprecated */
928
+ onObjectStepEnd(event) {
929
+ var _a;
930
+ const state = this.getCallState(event.callId);
931
+ if (!(state == null ? void 0 : state.inferenceSpan)) return;
932
+ const { telemetry } = state;
933
+ state.inferenceSpan.setAttributes(
934
+ selectAttributes(telemetry, {
935
+ "gen_ai.response.finish_reasons": [event.finishReason],
936
+ "gen_ai.response.id": event.response.id,
937
+ "gen_ai.response.model": event.response.modelId,
938
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
939
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
940
+ "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
941
+ "gen_ai.output.messages": {
942
+ output: () => {
943
+ try {
944
+ return JSON.stringify(
945
+ formatObjectOutputMessages({
946
+ objectText: event.objectText,
947
+ finishReason: event.finishReason
948
+ })
949
+ );
950
+ } catch (e) {
951
+ return event.objectText;
952
+ }
953
+ }
954
+ },
955
+ ...selectSupplementalAttributes(
956
+ telemetry,
957
+ this.supplementalAttributes,
958
+ {
959
+ providerMetadata: {
960
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
961
+ },
962
+ usage: getDetailedUsageAttributes(event.usage)
963
+ }
964
+ )
965
+ })
966
+ );
967
+ state.inferenceSpan.end();
968
+ state.inferenceSpan = void 0;
969
+ state.inferenceContext = void 0;
970
+ }
971
+ onEmbedOperationStart(event) {
972
+ const telemetry = {
973
+ recordInputs: event.recordInputs,
974
+ recordOutputs: event.recordOutputs,
975
+ functionId: event.functionId
976
+ };
977
+ const providerName = mapProviderName(event.provider);
978
+ const baseSupplementalAttributes = selectSupplementalAttributes(
979
+ telemetry,
980
+ this.supplementalAttributes,
981
+ {
982
+ headers: getHeaderAttributes(event.headers)
983
+ }
984
+ );
985
+ const value = event.value;
986
+ const isMany = event.operationId === "ai.embedMany";
987
+ const attributes = selectAttributes(telemetry, {
988
+ "gen_ai.operation.name": "embeddings",
989
+ "gen_ai.provider.name": providerName,
990
+ "gen_ai.request.model": event.modelId,
991
+ ...baseSupplementalAttributes,
992
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
993
+ embedding: isMany ? {
994
+ "ai.values": {
995
+ input: () => value.map((v) => JSON.stringify(v))
996
+ }
997
+ } : {
998
+ "ai.value": {
999
+ input: () => JSON.stringify(value)
1000
+ }
1001
+ }
1002
+ })
1003
+ });
1004
+ const spanName = `embeddings ${event.modelId}`;
1005
+ const rootSpan = this.tracer.startSpan(spanName, {
1006
+ attributes: this.getSpanAttributes({
1007
+ attributes,
1008
+ spanType: "operation",
1009
+ operationId: event.operationId,
1010
+ callId: event.callId,
1011
+ runtimeContext: void 0
1012
+ }),
1013
+ kind: SpanKind.CLIENT
1014
+ });
1015
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
1016
+ this.callStates.set(event.callId, {
1017
+ operationId: event.operationId,
1018
+ telemetry,
1019
+ rootSpan,
1020
+ rootContext,
1021
+ stepSpan: void 0,
1022
+ stepContext: void 0,
1023
+ inferenceSpan: void 0,
1024
+ inferenceContext: void 0,
1025
+ embedSpans: /* @__PURE__ */ new Map(),
1026
+ rerankSpan: void 0,
1027
+ toolSpans: /* @__PURE__ */ new Map(),
1028
+ settings: { maxRetries: event.maxRetries },
1029
+ provider: event.provider,
1030
+ modelId: event.modelId,
1031
+ runtimeContext: void 0,
1032
+ baseSupplementalAttributes
1033
+ });
1034
+ }
1035
+ onStepStart(event) {
1036
+ const state = this.getCallState(event.callId);
1037
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1038
+ const { telemetry } = state;
1039
+ state.runtimeContext = event.runtimeContext;
1040
+ const stepAttributes = selectAttributes(telemetry, {
1041
+ "gen_ai.operation.name": "agent_step",
1042
+ ...state.baseSupplementalAttributes,
1043
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1044
+ toolChoice: {
1045
+ "ai.prompt.toolChoice": {
1046
+ input: () => event.stepToolChoice != null ? JSON.stringify(event.stepToolChoice) : void 0
1047
+ }
1048
+ }
1049
+ })
1050
+ });
1051
+ state.stepSpan = this.tracer.startSpan(
1052
+ `step ${event.steps.length + 1}`,
1053
+ {
1054
+ attributes: this.getSpanAttributes({
1055
+ attributes: stepAttributes,
1056
+ spanType: "step",
1057
+ operationId: state.operationId,
1058
+ callId: event.callId,
1059
+ runtimeContext: state.runtimeContext
1060
+ }),
1061
+ kind: SpanKind.INTERNAL
1062
+ },
1063
+ state.rootContext
1064
+ );
1065
+ state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
1066
+ }
1067
+ onLanguageModelCallStart(event) {
1068
+ var _a;
1069
+ const state = this.getCallState(event.callId);
1070
+ if (!(state == null ? void 0 : state.stepContext)) return;
1071
+ const { telemetry } = state;
1072
+ const providerName = mapProviderName(event.provider);
1073
+ const inferenceAttributes = selectAttributes(telemetry, {
1074
+ "gen_ai.operation.name": "chat",
1075
+ "gen_ai.provider.name": providerName,
1076
+ "gen_ai.request.model": event.modelId,
1077
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
1078
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
1079
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
1080
+ "gen_ai.request.stop_sequences": state.settings.stopSequences,
1081
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
1082
+ "gen_ai.request.top_k": state.settings.topK,
1083
+ "gen_ai.request.top_p": state.settings.topP,
1084
+ "gen_ai.input.messages": {
1085
+ input: () => {
1086
+ const formattedMessages = formatModelMessages({
1087
+ prompt: void 0,
1088
+ messages: event.messages
1089
+ });
1090
+ return formattedMessages.length > 0 ? JSON.stringify(formattedMessages) : void 0;
1091
+ }
1092
+ },
1093
+ "gen_ai.tool.definitions": {
1094
+ input: () => event.tools ? JSON.stringify(event.tools) : void 0
1095
+ }
1096
+ });
1097
+ state.inferenceSpan = this.tracer.startSpan(
1098
+ `chat ${event.modelId}`,
1099
+ {
1100
+ attributes: this.getSpanAttributes({
1101
+ attributes: inferenceAttributes,
1102
+ spanType: "languageModel",
1103
+ operationId: state.operationId,
1104
+ callId: event.callId,
1105
+ runtimeContext: state.runtimeContext
1106
+ }),
1107
+ kind: SpanKind.CLIENT
1108
+ },
1109
+ state.stepContext
1110
+ );
1111
+ state.inferenceContext = trace.setSpan(
1112
+ state.stepContext,
1113
+ state.inferenceSpan
1114
+ );
1115
+ }
1116
+ onLanguageModelCallEnd(event) {
1117
+ var _a, _b;
1118
+ const state = this.getCallState(event.callId);
1119
+ if (!(state == null ? void 0 : state.inferenceSpan)) return;
1120
+ const { telemetry } = state;
1121
+ state.inferenceSpan.setAttributes(
1122
+ selectAttributes(telemetry, {
1123
+ ...getGenAIClientPerformanceAttributes(event.performance),
1124
+ "gen_ai.response.finish_reasons": [event.finishReason],
1125
+ "gen_ai.response.id": event.responseId,
1126
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
1127
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
1128
+ "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
1129
+ "gen_ai.usage.cache_creation.input_tokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheWriteTokens,
1130
+ "gen_ai.output.messages": {
1131
+ output: () => JSON.stringify(
1132
+ formatOutputMessages({
1133
+ text: event.content.filter((p) => p.type === "text").map((p) => p.text).join("") || void 0,
1134
+ reasoning: event.content.filter((p) => p.type === "reasoning"),
1135
+ toolCalls: event.content.filter((p) => p.type === "tool-call"),
1136
+ files: event.content.filter((p) => p.type === "file").map((p) => p.file),
1137
+ finishReason: event.finishReason
1138
+ })
1139
+ )
1140
+ },
1141
+ ...selectSupplementalAttributes(
1142
+ telemetry,
1143
+ this.supplementalAttributes,
1144
+ {
1145
+ usage: getDetailedUsageAttributes(event.usage)
1146
+ }
1147
+ )
1148
+ })
1149
+ );
1150
+ state.inferenceSpan.end();
1151
+ state.inferenceSpan = void 0;
1152
+ state.inferenceContext = void 0;
1153
+ }
1154
+ onToolExecutionStart(event) {
1155
+ const state = this.getCallState(event.callId);
1156
+ if (!(state == null ? void 0 : state.stepContext)) return;
1157
+ const { telemetry } = state;
1158
+ const { toolCall } = event;
1159
+ const attributes = selectAttributes(telemetry, {
1160
+ "gen_ai.operation.name": "execute_tool",
1161
+ "gen_ai.tool.name": toolCall.toolName,
1162
+ "gen_ai.tool.call.id": toolCall.toolCallId,
1163
+ "gen_ai.tool.type": "function",
1164
+ "gen_ai.tool.call.arguments": {
1165
+ input: () => JSON.stringify(toolCall.input)
1166
+ }
1167
+ });
1168
+ const spanName = `execute_tool ${toolCall.toolName}`;
1169
+ const toolSpan = this.tracer.startSpan(
1170
+ spanName,
1171
+ {
1172
+ attributes: this.getSpanAttributes({
1173
+ attributes,
1174
+ spanType: "tool",
1175
+ operationId: state.operationId,
1176
+ callId: event.callId,
1177
+ runtimeContext: state.runtimeContext
1178
+ }),
1179
+ kind: SpanKind.INTERNAL
1180
+ },
1181
+ state.stepContext
1182
+ );
1183
+ const toolContext = trace.setSpan(state.stepContext, toolSpan);
1184
+ state.toolSpans.set(toolCall.toolCallId, {
1185
+ span: toolSpan,
1186
+ context: toolContext
1187
+ });
1188
+ }
1189
+ onToolExecutionEnd(event) {
1190
+ const state = this.getCallState(event.callId);
1191
+ if (!state) return;
1192
+ const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
1193
+ if (!toolSpanEntry) return;
1194
+ const { span } = toolSpanEntry;
1195
+ const { telemetry } = state;
1196
+ const { toolOutput } = event;
1197
+ span.setAttributes(
1198
+ selectAttributes(telemetry, {
1199
+ "gen_ai.execute_tool.duration": msToSeconds(event.toolExecutionMs)
1200
+ })
1201
+ );
1202
+ if (toolOutput.type === "tool-result") {
1203
+ try {
1204
+ span.setAttributes(
1205
+ selectAttributes(telemetry, {
1206
+ "gen_ai.tool.call.result": {
1207
+ output: () => JSON.stringify(toolOutput.output)
1208
+ }
1209
+ })
1210
+ );
1211
+ } catch (e) {
1212
+ }
1213
+ } else {
1214
+ recordErrorOnSpan(span, toolOutput.error);
1215
+ }
1216
+ span.end();
1217
+ state.toolSpans.delete(event.toolCall.toolCallId);
1218
+ }
1219
+ onStepEnd(event) {
1220
+ const state = this.getCallState(event.callId);
1221
+ if (!(state == null ? void 0 : state.stepSpan)) return;
1222
+ const { telemetry } = state;
1223
+ state.stepSpan.setAttributes(
1224
+ selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1225
+ providerMetadata: {
1226
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
1227
+ },
1228
+ usage: getDetailedUsageAttributes(event.usage)
1229
+ })
1230
+ );
1231
+ state.stepSpan.end();
1232
+ state.stepSpan = void 0;
1233
+ state.stepContext = void 0;
1234
+ }
1235
+ /** @deprecated Use `onStepEnd` instead. */
1236
+ onStepFinish(event) {
1237
+ this.onStepEnd(event);
1238
+ }
1239
+ onEnd(event) {
1240
+ const state = this.getCallState(event.callId);
1241
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1242
+ if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
1243
+ this.onEmbedOperationEnd(event);
1244
+ return;
1245
+ }
1246
+ if (state.operationId === "ai.rerank") {
1247
+ this.onRerankOperationEnd(event);
1248
+ return;
1249
+ }
1250
+ if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
1251
+ this.onObjectOperationEnd(event);
1252
+ return;
1253
+ }
1254
+ this.onGenerateEnd(event);
1255
+ }
1256
+ onGenerateEnd(event) {
1257
+ var _a, _b;
1258
+ const state = this.getCallState(event.callId);
1259
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1260
+ const { telemetry } = state;
1261
+ state.rootSpan.setAttributes(
1262
+ selectAttributes(telemetry, {
1263
+ "gen_ai.response.finish_reasons": [event.finishReason],
1264
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
1265
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
1266
+ "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
1267
+ "gen_ai.usage.cache_creation.input_tokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheWriteTokens,
1268
+ "gen_ai.output.messages": {
1269
+ output: () => {
1270
+ var _a2;
1271
+ return JSON.stringify(
1272
+ formatOutputMessages({
1273
+ text: (_a2 = event.text) != null ? _a2 : void 0,
1274
+ reasoning: event.finalStep.reasoning,
1275
+ toolCalls: event.toolCalls,
1276
+ files: event.files,
1277
+ finishReason: event.finishReason
1278
+ })
1279
+ );
1280
+ }
1281
+ },
1282
+ ...selectSupplementalAttributes(
1283
+ telemetry,
1284
+ this.supplementalAttributes,
1285
+ {
1286
+ providerMetadata: {
1287
+ "ai.response.providerMetadata": event.finalStep.providerMetadata ? JSON.stringify(event.finalStep.providerMetadata) : void 0
1288
+ },
1289
+ usage: getDetailedUsageAttributes(event.usage)
1290
+ }
1291
+ )
1292
+ })
1293
+ );
1294
+ state.rootSpan.end();
1295
+ this.cleanupCallState(event.callId);
1296
+ }
1297
+ onObjectOperationEnd(event) {
1298
+ var _a;
1299
+ const state = this.getCallState(event.callId);
1300
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1301
+ const { telemetry } = state;
1302
+ state.rootSpan.setAttributes(
1303
+ selectAttributes(telemetry, {
1304
+ "gen_ai.response.finish_reasons": [event.finishReason],
1305
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
1306
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
1307
+ "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
1308
+ "gen_ai.output.messages": {
1309
+ output: () => event.object != null ? JSON.stringify(
1310
+ formatObjectOutputMessages({
1311
+ objectText: JSON.stringify(event.object),
1312
+ finishReason: event.finishReason
1313
+ })
1314
+ ) : void 0
1315
+ },
1316
+ ...selectSupplementalAttributes(
1317
+ telemetry,
1318
+ this.supplementalAttributes,
1319
+ {
1320
+ providerMetadata: {
1321
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
1322
+ },
1323
+ usage: getDetailedUsageAttributes(event.usage)
1324
+ }
1325
+ )
1326
+ })
1327
+ );
1328
+ state.rootSpan.end();
1329
+ this.cleanupCallState(event.callId);
1330
+ }
1331
+ onEmbedOperationEnd(event) {
1332
+ const state = this.getCallState(event.callId);
1333
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1334
+ const { telemetry } = state;
1335
+ const isMany = state.operationId === "ai.embedMany";
1336
+ state.rootSpan.setAttributes(
1337
+ selectAttributes(telemetry, {
1338
+ "gen_ai.usage.input_tokens": event.usage.tokens,
1339
+ ...selectSupplementalAttributes(
1340
+ telemetry,
1341
+ this.supplementalAttributes,
1342
+ {
1343
+ embedding: isMany ? {
1344
+ "ai.embeddings": {
1345
+ output: () => event.embedding.map(
1346
+ (e) => JSON.stringify(e)
1347
+ )
1348
+ }
1349
+ } : {
1350
+ "ai.embedding": {
1351
+ output: () => JSON.stringify(event.embedding)
1352
+ }
1353
+ }
1354
+ }
1355
+ )
1356
+ })
1357
+ );
1358
+ state.rootSpan.end();
1359
+ this.cleanupCallState(event.callId);
1360
+ }
1361
+ onEmbedStart(event) {
1362
+ const state = this.getCallState(event.callId);
1363
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1364
+ const { telemetry } = state;
1365
+ const providerName = mapProviderName(state.provider);
1366
+ const attributes = selectAttributes(telemetry, {
1367
+ "gen_ai.operation.name": "embeddings",
1368
+ "gen_ai.provider.name": providerName,
1369
+ "gen_ai.request.model": state.modelId,
1370
+ ...state.baseSupplementalAttributes,
1371
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1372
+ embedding: {
1373
+ "ai.values": {
1374
+ input: () => event.values.map((v) => JSON.stringify(v))
1375
+ }
1376
+ }
1377
+ })
1378
+ });
1379
+ const spanName = `embeddings ${state.modelId}`;
1380
+ const embedSpan = this.tracer.startSpan(
1381
+ spanName,
1382
+ {
1383
+ attributes: this.getSpanAttributes({
1384
+ attributes,
1385
+ spanType: "embedding",
1386
+ operationId: state.operationId,
1387
+ callId: event.callId,
1388
+ runtimeContext: state.runtimeContext
1389
+ }),
1390
+ kind: SpanKind.CLIENT
1391
+ },
1392
+ state.rootContext
1393
+ );
1394
+ const embedContext = trace.setSpan(state.rootContext, embedSpan);
1395
+ state.embedSpans.set(event.embedCallId, {
1396
+ span: embedSpan,
1397
+ context: embedContext
1398
+ });
1399
+ }
1400
+ onEmbedEnd(event) {
1401
+ const state = this.getCallState(event.callId);
1402
+ if (!state) return;
1403
+ const embedSpanEntry = state.embedSpans.get(event.embedCallId);
1404
+ if (!embedSpanEntry) return;
1405
+ const { span } = embedSpanEntry;
1406
+ const { telemetry } = state;
1407
+ span.setAttributes(
1408
+ selectAttributes(telemetry, {
1409
+ "gen_ai.usage.input_tokens": event.usage.tokens,
1410
+ ...selectSupplementalAttributes(
1411
+ telemetry,
1412
+ this.supplementalAttributes,
1413
+ {
1414
+ embedding: {
1415
+ "ai.embeddings": {
1416
+ output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
1417
+ }
1418
+ }
1419
+ }
1420
+ )
1421
+ })
1422
+ );
1423
+ span.end();
1424
+ state.embedSpans.delete(event.embedCallId);
1425
+ }
1426
+ onRerankOperationStart(event) {
1427
+ const telemetry = {
1428
+ recordInputs: event.recordInputs,
1429
+ recordOutputs: event.recordOutputs,
1430
+ functionId: event.functionId
1431
+ };
1432
+ const providerName = mapProviderName(event.provider);
1433
+ const baseSupplementalAttributes = selectSupplementalAttributes(
1434
+ telemetry,
1435
+ this.supplementalAttributes,
1436
+ {
1437
+ headers: getHeaderAttributes(event.headers)
1438
+ }
1439
+ );
1440
+ const attributes = selectAttributes(telemetry, {
1441
+ "gen_ai.operation.name": "rerank",
1442
+ "gen_ai.provider.name": providerName,
1443
+ "gen_ai.request.model": event.modelId,
1444
+ ...baseSupplementalAttributes,
1445
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1446
+ reranking: {
1447
+ "ai.documents": {
1448
+ input: () => event.documents.map((d) => JSON.stringify(d))
1449
+ }
1450
+ }
1451
+ })
1452
+ });
1453
+ const spanName = `rerank ${event.modelId}`;
1454
+ const rootSpan = this.tracer.startSpan(spanName, {
1455
+ attributes: this.getSpanAttributes({
1456
+ attributes,
1457
+ spanType: "operation",
1458
+ operationId: event.operationId,
1459
+ callId: event.callId,
1460
+ runtimeContext: void 0
1461
+ }),
1462
+ kind: SpanKind.CLIENT
1463
+ });
1464
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
1465
+ this.callStates.set(event.callId, {
1466
+ operationId: event.operationId,
1467
+ telemetry,
1468
+ rootSpan,
1469
+ rootContext,
1470
+ stepSpan: void 0,
1471
+ stepContext: void 0,
1472
+ inferenceSpan: void 0,
1473
+ inferenceContext: void 0,
1474
+ embedSpans: /* @__PURE__ */ new Map(),
1475
+ rerankSpan: void 0,
1476
+ toolSpans: /* @__PURE__ */ new Map(),
1477
+ settings: { maxRetries: event.maxRetries },
1478
+ provider: event.provider,
1479
+ modelId: event.modelId,
1480
+ runtimeContext: void 0,
1481
+ baseSupplementalAttributes
1482
+ });
1483
+ }
1484
+ onRerankOperationEnd(event) {
1485
+ const state = this.getCallState(event.callId);
1486
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1487
+ state.rootSpan.end();
1488
+ this.cleanupCallState(event.callId);
1489
+ }
1490
+ onRerankStart(event) {
1491
+ const state = this.getCallState(event.callId);
1492
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1493
+ const { telemetry } = state;
1494
+ const providerName = mapProviderName(state.provider);
1495
+ const attributes = selectAttributes(telemetry, {
1496
+ "gen_ai.operation.name": "rerank",
1497
+ "gen_ai.provider.name": providerName,
1498
+ "gen_ai.request.model": state.modelId,
1499
+ ...state.baseSupplementalAttributes,
1500
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1501
+ reranking: {
1502
+ "ai.documents": {
1503
+ input: () => event.documents.map((d) => JSON.stringify(d))
1504
+ }
1505
+ }
1506
+ })
1507
+ });
1508
+ const spanName = `rerank ${state.modelId}`;
1509
+ const rerankSpan = this.tracer.startSpan(
1510
+ spanName,
1511
+ {
1512
+ attributes: this.getSpanAttributes({
1513
+ attributes,
1514
+ spanType: "reranking",
1515
+ operationId: state.operationId,
1516
+ callId: event.callId,
1517
+ runtimeContext: state.runtimeContext
1518
+ }),
1519
+ kind: SpanKind.CLIENT
1520
+ },
1521
+ state.rootContext
1522
+ );
1523
+ const rerankContext = trace.setSpan(state.rootContext, rerankSpan);
1524
+ state.rerankSpan = { span: rerankSpan, context: rerankContext };
1525
+ }
1526
+ onRerankEnd(event) {
1527
+ const state = this.getCallState(event.callId);
1528
+ if (!(state == null ? void 0 : state.rerankSpan)) return;
1529
+ const { span } = state.rerankSpan;
1530
+ const { telemetry } = state;
1531
+ span.setAttributes(
1532
+ selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1533
+ reranking: {
1534
+ "ai.ranking.type": event.documentsType,
1535
+ "ai.ranking": {
1536
+ output: () => event.ranking.map((r) => JSON.stringify(r))
1537
+ }
1538
+ }
1539
+ })
1540
+ );
1541
+ span.end();
1542
+ state.rerankSpan = void 0;
1543
+ }
1544
+ onAbort(event) {
1545
+ const state = this.getCallState(event.callId);
1546
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1547
+ for (const { span: toolSpan } of state.toolSpans.values()) {
1548
+ toolSpan.end();
1549
+ }
1550
+ state.toolSpans.clear();
1551
+ if (state.inferenceSpan) {
1552
+ state.inferenceSpan.end();
1553
+ state.inferenceSpan = void 0;
1554
+ state.inferenceContext = void 0;
1555
+ }
1556
+ if (state.stepSpan) {
1557
+ state.stepSpan.end();
1558
+ state.stepSpan = void 0;
1559
+ state.stepContext = void 0;
1560
+ }
1561
+ for (const { span: embedSpan } of state.embedSpans.values()) {
1562
+ embedSpan.end();
1563
+ }
1564
+ state.embedSpans.clear();
1565
+ if (state.rerankSpan) {
1566
+ state.rerankSpan.span.end();
1567
+ state.rerankSpan = void 0;
1568
+ }
1569
+ state.rootSpan.end();
1570
+ this.cleanupCallState(event.callId);
1571
+ }
1572
+ onError(error) {
1573
+ var _a;
1574
+ const event = error;
1575
+ if (!(event == null ? void 0 : event.callId)) return;
1576
+ const state = this.getCallState(event.callId);
1577
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1578
+ const actualError = (_a = event.error) != null ? _a : error;
1579
+ for (const { span: toolSpan } of state.toolSpans.values()) {
1580
+ recordErrorOnSpan(toolSpan, actualError);
1581
+ toolSpan.end();
1582
+ }
1583
+ state.toolSpans.clear();
1584
+ if (state.inferenceSpan) {
1585
+ recordErrorOnSpan(state.inferenceSpan, actualError);
1586
+ state.inferenceSpan.end();
1587
+ state.inferenceSpan = void 0;
1588
+ state.inferenceContext = void 0;
1589
+ }
1590
+ if (state.stepSpan) {
1591
+ recordErrorOnSpan(state.stepSpan, actualError);
1592
+ state.stepSpan.end();
1593
+ state.stepSpan = void 0;
1594
+ state.stepContext = void 0;
1595
+ }
1596
+ for (const { span: embedSpan } of state.embedSpans.values()) {
1597
+ recordErrorOnSpan(embedSpan, actualError);
1598
+ embedSpan.end();
1599
+ }
1600
+ state.embedSpans.clear();
1601
+ if (state.rerankSpan) {
1602
+ recordErrorOnSpan(state.rerankSpan.span, actualError);
1603
+ state.rerankSpan.span.end();
1604
+ state.rerankSpan = void 0;
1605
+ }
1606
+ recordErrorOnSpan(state.rootSpan, actualError);
1607
+ state.rootSpan.end();
1608
+ this.cleanupCallState(event.callId);
15
1609
  }
16
- return to;
17
1610
  };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
1611
 
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- OpenTelemetryIntegration: () => OpenTelemetryIntegration
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
-
27
- // src/open-telemetry-integration.ts
28
- var import_api = require("@opentelemetry/api");
1612
+ // src/legacy-open-telemetry.ts
1613
+ import {
1614
+ context as context3,
1615
+ SpanStatusCode as SpanStatusCode2,
1616
+ trace as trace2
1617
+ } from "@opentelemetry/api";
29
1618
 
30
1619
  // src/assemble-operation-name.ts
31
1620
  function assembleOperationName({
@@ -46,10 +1635,9 @@ function assembleOperationName({
46
1635
  function getBaseTelemetryAttributes({
47
1636
  model,
48
1637
  settings,
49
- telemetry,
50
- headers
1638
+ headers,
1639
+ context: context4
51
1640
  }) {
52
- var _a;
53
1641
  return {
54
1642
  "ai.model.provider": model.provider,
55
1643
  "ai.model.id": model.modelId,
@@ -58,16 +1646,8 @@ function getBaseTelemetryAttributes({
58
1646
  attributes[`ai.settings.${key}`] = value;
59
1647
  return attributes;
60
1648
  }, {}),
61
- // add metadata as attributes:
62
- ...Object.entries((_a = telemetry == null ? void 0 : telemetry.metadata) != null ? _a : {}).reduce(
63
- (attributes, [key, value]) => {
64
- if (value != void 0) {
65
- attributes[`ai.telemetry.metadata.${key}`] = value;
66
- }
67
- return attributes;
68
- },
69
- {}
70
- ),
1649
+ // add context as attributes:
1650
+ ...getRuntimeContextAttributes(context4),
71
1651
  // request headers
72
1652
  ...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
73
1653
  if (value !== void 0) {
@@ -79,7 +1659,7 @@ function getBaseTelemetryAttributes({
79
1659
  }
80
1660
 
81
1661
  // src/stringify-for-telemetry.ts
82
- var import_ai = require("ai");
1662
+ import { convertDataContentToBase64String as convertDataContentToBase64String2 } from "ai";
83
1663
  function stringifyForTelemetry(prompt) {
84
1664
  return JSON.stringify(
85
1665
  prompt.map((message) => ({
@@ -87,14 +1667,26 @@ function stringifyForTelemetry(prompt) {
87
1667
  content: typeof message.content === "string" ? message.content : message.content.map(
88
1668
  (part) => part.type === "file" ? {
89
1669
  ...part,
90
- data: part.data instanceof Uint8Array ? (0, import_ai.convertDataContentToBase64String)(part.data) : part.data
1670
+ data: serializeFileData(part.data)
91
1671
  } : part
92
1672
  )
93
1673
  }))
94
1674
  );
95
1675
  }
1676
+ function serializeFileData(data) {
1677
+ switch (data.type) {
1678
+ case "data":
1679
+ return data.data instanceof Uint8Array ? convertDataContentToBase64String2(data.data) : data.data;
1680
+ case "url":
1681
+ return data.url.toString();
1682
+ case "reference":
1683
+ return data.reference;
1684
+ case "text":
1685
+ return data.text;
1686
+ }
1687
+ }
96
1688
 
97
- // src/open-telemetry-integration.ts
1689
+ // src/legacy-open-telemetry.ts
98
1690
  function recordSpanError(span, error) {
99
1691
  if (error instanceof Error) {
100
1692
  span.recordException({
@@ -103,18 +1695,18 @@ function recordSpanError(span, error) {
103
1695
  stack: error.stack
104
1696
  });
105
1697
  span.setStatus({
106
- code: import_api.SpanStatusCode.ERROR,
1698
+ code: SpanStatusCode2.ERROR,
107
1699
  message: error.message
108
1700
  });
109
1701
  } else {
110
- span.setStatus({ code: import_api.SpanStatusCode.ERROR });
1702
+ span.setStatus({ code: SpanStatusCode2.ERROR });
111
1703
  }
112
1704
  }
113
- function shouldRecord(telemetry) {
114
- return (telemetry == null ? void 0 : telemetry.isEnabled) === true;
1705
+ function shouldRecord2(telemetry) {
1706
+ return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
115
1707
  }
116
- function selectAttributes(telemetry, attributes) {
117
- if (!shouldRecord(telemetry)) {
1708
+ function selectAttributes2(telemetry, attributes) {
1709
+ if (!shouldRecord2(telemetry)) {
118
1710
  return {};
119
1711
  }
120
1712
  const result = {};
@@ -123,24 +1715,31 @@ function selectAttributes(telemetry, attributes) {
123
1715
  if (typeof value === "object" && "input" in value && typeof value.input === "function") {
124
1716
  if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
125
1717
  const resolved = value.input();
126
- if (resolved != null) result[key] = resolved;
1718
+ if (resolved != null) {
1719
+ const sanitized2 = sanitizeAttributeValue(resolved);
1720
+ if (sanitized2 != null) result[key] = sanitized2;
1721
+ }
127
1722
  continue;
128
1723
  }
129
1724
  if (typeof value === "object" && "output" in value && typeof value.output === "function") {
130
1725
  if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
131
1726
  const resolved = value.output();
132
- if (resolved != null) result[key] = resolved;
1727
+ if (resolved != null) {
1728
+ const sanitized2 = sanitizeAttributeValue(resolved);
1729
+ if (sanitized2 != null) result[key] = sanitized2;
1730
+ }
133
1731
  continue;
134
1732
  }
135
- result[key] = value;
1733
+ const sanitized = sanitizeAttributeValue(value);
1734
+ if (sanitized != null) result[key] = sanitized;
136
1735
  }
137
1736
  return result;
138
1737
  }
139
- var OpenTelemetryIntegration = class {
1738
+ var LegacyOpenTelemetry = class {
140
1739
  constructor(options = {}) {
141
1740
  this.callStates = /* @__PURE__ */ new Map();
142
1741
  var _a;
143
- this.tracer = (_a = options.tracer) != null ? _a : import_api.trace.getTracer("ai");
1742
+ this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
144
1743
  }
145
1744
  getCallState(callId) {
146
1745
  return this.callStates.get(callId);
@@ -158,31 +1757,47 @@ var OpenTelemetryIntegration = class {
158
1757
  if (toolSpanEntry == null) {
159
1758
  return execute();
160
1759
  }
161
- return import_api.context.with(toolSpanEntry.context, execute);
1760
+ return context3.with(toolSpanEntry.context, execute);
1761
+ }
1762
+ /**
1763
+ * Runs the provider `doGenerate`/`doStream` call with the active legacy
1764
+ * model-call context.
1765
+ */
1766
+ executeLanguageModelCall({
1767
+ callId,
1768
+ execute
1769
+ }) {
1770
+ var _a;
1771
+ const stepContext = (_a = this.getCallState(callId)) == null ? void 0 : _a.stepContext;
1772
+ if (stepContext == null) {
1773
+ return execute();
1774
+ }
1775
+ return context3.with(stepContext, execute);
162
1776
  }
163
1777
  onStart(event) {
164
- if (event.isEnabled !== true) return;
165
1778
  if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
166
1779
  this.onEmbedOperationStart(event);
167
1780
  return;
168
1781
  }
169
1782
  if (event.operationId === "ai.rerank") {
170
- this.onRerankOperationStart(event);
1783
+ this.onRerankOperationStart(
1784
+ event
1785
+ );
171
1786
  return;
172
1787
  }
173
1788
  if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
174
- this.onObjectOperationStart(event);
1789
+ this.onObjectOperationStart(
1790
+ event
1791
+ );
175
1792
  return;
176
1793
  }
177
1794
  this.onGenerateStart(event);
178
1795
  }
179
1796
  onGenerateStart(event) {
180
1797
  const telemetry = {
181
- isEnabled: event.isEnabled,
182
1798
  recordInputs: event.recordInputs,
183
1799
  recordOutputs: event.recordOutputs,
184
- functionId: event.functionId,
185
- metadata: event.metadata
1800
+ functionId: event.functionId
186
1801
  };
187
1802
  const settings = {
188
1803
  maxOutputTokens: event.maxOutputTokens,
@@ -197,11 +1812,11 @@ var OpenTelemetryIntegration = class {
197
1812
  };
198
1813
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
199
1814
  model: { provider: event.provider, modelId: event.modelId },
200
- telemetry,
201
1815
  headers: event.headers,
202
- settings
1816
+ settings,
1817
+ context: event.runtimeContext
203
1818
  });
204
- const attributes = selectAttributes(telemetry, {
1819
+ const attributes = selectAttributes2(telemetry, {
205
1820
  ...assembleOperationName({
206
1821
  operationId: event.operationId,
207
1822
  telemetry
@@ -211,14 +1826,13 @@ var OpenTelemetryIntegration = class {
211
1826
  "ai.model.id": event.modelId,
212
1827
  "ai.prompt": {
213
1828
  input: () => JSON.stringify({
214
- system: event.system,
215
- prompt: event.prompt,
1829
+ system: event.instructions,
216
1830
  messages: event.messages
217
1831
  })
218
1832
  }
219
1833
  });
220
1834
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
221
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1835
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
222
1836
  this.callStates.set(event.callId, {
223
1837
  operationId: event.operationId,
224
1838
  telemetry,
@@ -235,11 +1849,9 @@ var OpenTelemetryIntegration = class {
235
1849
  }
236
1850
  onObjectOperationStart(event) {
237
1851
  const telemetry = {
238
- isEnabled: event.isEnabled,
239
1852
  recordInputs: event.recordInputs,
240
1853
  recordOutputs: event.recordOutputs,
241
- functionId: event.functionId,
242
- metadata: event.metadata
1854
+ functionId: event.functionId
243
1855
  };
244
1856
  const settings = {
245
1857
  maxOutputTokens: event.maxOutputTokens,
@@ -253,11 +1865,11 @@ var OpenTelemetryIntegration = class {
253
1865
  };
254
1866
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
255
1867
  model: { provider: event.provider, modelId: event.modelId },
256
- telemetry,
257
1868
  headers: event.headers,
258
- settings
1869
+ settings,
1870
+ context: void 0
259
1871
  });
260
- const attributes = selectAttributes(telemetry, {
1872
+ const attributes = selectAttributes2(telemetry, {
261
1873
  ...assembleOperationName({
262
1874
  operationId: event.operationId,
263
1875
  telemetry
@@ -276,7 +1888,7 @@ var OpenTelemetryIntegration = class {
276
1888
  "ai.settings.output": event.output
277
1889
  });
278
1890
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
279
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1891
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
280
1892
  this.callStates.set(event.callId, {
281
1893
  operationId: event.operationId,
282
1894
  telemetry,
@@ -298,7 +1910,7 @@ var OpenTelemetryIntegration = class {
298
1910
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
299
1911
  const { telemetry } = state;
300
1912
  const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
301
- const attributes = selectAttributes(telemetry, {
1913
+ const attributes = selectAttributes2(telemetry, {
302
1914
  ...assembleOperationName({
303
1915
  operationId: stepOperationId,
304
1916
  telemetry
@@ -321,15 +1933,16 @@ var OpenTelemetryIntegration = class {
321
1933
  { attributes },
322
1934
  state.rootContext
323
1935
  );
324
- state.stepContext = import_api.trace.setSpan(state.rootContext, state.stepSpan);
1936
+ state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
325
1937
  }
326
1938
  /** @deprecated */
327
- onObjectStepFinish(event) {
1939
+ onObjectStepEnd(event) {
1940
+ var _a, _b;
328
1941
  const state = this.getCallState(event.callId);
329
1942
  if (!(state == null ? void 0 : state.stepSpan)) return;
330
1943
  const { telemetry } = state;
331
1944
  state.stepSpan.setAttributes(
332
- selectAttributes(telemetry, {
1945
+ selectAttributes2(telemetry, {
333
1946
  "ai.response.finishReason": event.finishReason,
334
1947
  "ai.response.object": {
335
1948
  output: () => {
@@ -347,8 +1960,8 @@ var OpenTelemetryIntegration = class {
347
1960
  "ai.usage.inputTokens": event.usage.inputTokens,
348
1961
  "ai.usage.outputTokens": event.usage.outputTokens,
349
1962
  "ai.usage.totalTokens": event.usage.totalTokens,
350
- "ai.usage.reasoningTokens": event.usage.reasoningTokens,
351
- "ai.usage.cachedInputTokens": event.usage.cachedInputTokens,
1963
+ "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
1964
+ "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
352
1965
  "gen_ai.response.finish_reasons": [event.finishReason],
353
1966
  "gen_ai.response.id": event.response.id,
354
1967
  "gen_ai.response.model": event.response.modelId,
@@ -370,24 +1983,22 @@ var OpenTelemetryIntegration = class {
370
1983
  }
371
1984
  onEmbedOperationStart(event) {
372
1985
  const telemetry = {
373
- isEnabled: event.isEnabled,
374
1986
  recordInputs: event.recordInputs,
375
1987
  recordOutputs: event.recordOutputs,
376
- functionId: event.functionId,
377
- metadata: event.metadata
1988
+ functionId: event.functionId
378
1989
  };
379
1990
  const settings = {
380
1991
  maxRetries: event.maxRetries
381
1992
  };
382
1993
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
383
1994
  model: { provider: event.provider, modelId: event.modelId },
384
- telemetry,
385
1995
  headers: event.headers,
386
- settings
1996
+ settings,
1997
+ context: void 0
387
1998
  });
388
1999
  const value = event.value;
389
2000
  const isMany = event.operationId === "ai.embedMany";
390
- const attributes = selectAttributes(telemetry, {
2001
+ const attributes = selectAttributes2(telemetry, {
391
2002
  ...assembleOperationName({
392
2003
  operationId: event.operationId,
393
2004
  telemetry
@@ -404,7 +2015,7 @@ var OpenTelemetryIntegration = class {
404
2015
  }
405
2016
  });
406
2017
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
407
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
2018
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
408
2019
  this.callStates.set(event.callId, {
409
2020
  operationId: event.operationId,
410
2021
  telemetry,
@@ -425,7 +2036,7 @@ var OpenTelemetryIntegration = class {
425
2036
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
426
2037
  const { telemetry } = state;
427
2038
  const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
428
- const attributes = selectAttributes(telemetry, {
2039
+ const attributes = selectAttributes2(telemetry, {
429
2040
  ...assembleOperationName({
430
2041
  operationId: stepOperationId,
431
2042
  telemetry
@@ -460,14 +2071,14 @@ var OpenTelemetryIntegration = class {
460
2071
  { attributes },
461
2072
  state.rootContext
462
2073
  );
463
- state.stepContext = import_api.trace.setSpan(state.rootContext, state.stepSpan);
2074
+ state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
464
2075
  }
465
- onToolCallStart(event) {
2076
+ onToolExecutionStart(event) {
466
2077
  const state = this.getCallState(event.callId);
467
2078
  if (!(state == null ? void 0 : state.stepContext)) return;
468
2079
  const { telemetry } = state;
469
2080
  const { toolCall } = event;
470
- const attributes = selectAttributes(telemetry, {
2081
+ const attributes = selectAttributes2(telemetry, {
471
2082
  ...assembleOperationName({
472
2083
  operationId: "ai.toolCall",
473
2084
  telemetry
@@ -483,43 +2094,45 @@ var OpenTelemetryIntegration = class {
483
2094
  { attributes },
484
2095
  state.stepContext
485
2096
  );
486
- const toolContext = import_api.trace.setSpan(state.stepContext, toolSpan);
2097
+ const toolContext = trace2.setSpan(state.stepContext, toolSpan);
487
2098
  state.toolSpans.set(toolCall.toolCallId, {
488
2099
  span: toolSpan,
489
2100
  context: toolContext
490
2101
  });
491
2102
  }
492
- onToolCallFinish(event) {
2103
+ onToolExecutionEnd(event) {
493
2104
  const state = this.getCallState(event.callId);
494
2105
  if (!state) return;
495
2106
  const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
496
2107
  if (!toolSpanEntry) return;
497
2108
  const { span } = toolSpanEntry;
498
2109
  const { telemetry } = state;
499
- if (event.success) {
2110
+ const { toolOutput } = event;
2111
+ if (toolOutput.type === "tool-result") {
500
2112
  try {
501
2113
  span.setAttributes(
502
- selectAttributes(telemetry, {
2114
+ selectAttributes2(telemetry, {
503
2115
  "ai.toolCall.result": {
504
- output: () => JSON.stringify(event.output)
2116
+ output: () => JSON.stringify(toolOutput.output)
505
2117
  }
506
2118
  })
507
2119
  );
508
- } catch (_ignored) {
2120
+ } catch (e) {
509
2121
  }
510
2122
  } else {
511
- recordSpanError(span, event.error);
2123
+ recordSpanError(span, toolOutput.error);
512
2124
  }
513
2125
  span.end();
514
2126
  state.toolSpans.delete(event.toolCall.toolCallId);
515
2127
  }
516
- onStepFinish(event) {
517
- var _a, _b, _c, _d, _e;
2128
+ onStepEnd(event) {
2129
+ var _a, _b, _c, _d, _e, _f, _g;
518
2130
  const state = this.getCallState(event.callId);
519
2131
  if (!(state == null ? void 0 : state.stepSpan)) return;
520
2132
  const { telemetry } = state;
2133
+ const isStreamText = state.operationId === "ai.streamText";
521
2134
  state.stepSpan.setAttributes(
522
- selectAttributes(telemetry, {
2135
+ selectAttributes2(telemetry, {
523
2136
  "ai.response.finishReason": event.finishReason,
524
2137
  "ai.response.text": {
525
2138
  output: () => {
@@ -552,16 +2165,19 @@ var OpenTelemetryIntegration = class {
552
2165
  "ai.response.model": event.response.modelId,
553
2166
  "ai.response.timestamp": event.response.timestamp.toISOString(),
554
2167
  "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0,
2168
+ "ai.response.msToFirstChunk": isStreamText ? event.performance.timeToFirstOutputMs : void 0,
2169
+ "ai.response.msToFinish": isStreamText ? event.performance.responseTimeMs : void 0,
2170
+ "ai.response.avgOutputTokensPerSecond": isStreamText ? event.performance.effectiveOutputTokensPerSecond : void 0,
555
2171
  "ai.usage.inputTokens": event.usage.inputTokens,
556
2172
  "ai.usage.outputTokens": event.usage.outputTokens,
557
2173
  "ai.usage.totalTokens": event.usage.totalTokens,
558
- "ai.usage.reasoningTokens": event.usage.reasoningTokens,
559
- "ai.usage.cachedInputTokens": event.usage.cachedInputTokens,
560
- "ai.usage.inputTokenDetails.noCacheTokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
561
- "ai.usage.inputTokenDetails.cacheReadTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
562
- "ai.usage.inputTokenDetails.cacheWriteTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
563
- "ai.usage.outputTokenDetails.textTokens": (_d = event.usage.outputTokenDetails) == null ? void 0 : _d.textTokens,
564
- "ai.usage.outputTokenDetails.reasoningTokens": (_e = event.usage.outputTokenDetails) == null ? void 0 : _e.reasoningTokens,
2174
+ "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2175
+ "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
2176
+ "ai.usage.inputTokenDetails.noCacheTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.noCacheTokens,
2177
+ "ai.usage.inputTokenDetails.cacheReadTokens": (_d = event.usage.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens,
2178
+ "ai.usage.inputTokenDetails.cacheWriteTokens": (_e = event.usage.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
2179
+ "ai.usage.outputTokenDetails.textTokens": (_f = event.usage.outputTokenDetails) == null ? void 0 : _f.textTokens,
2180
+ "ai.usage.outputTokenDetails.reasoningTokens": (_g = event.usage.outputTokenDetails) == null ? void 0 : _g.reasoningTokens,
565
2181
  "gen_ai.response.finish_reasons": [event.finishReason],
566
2182
  "gen_ai.response.id": event.response.id,
567
2183
  "gen_ai.response.model": event.response.modelId,
@@ -569,34 +2185,49 @@ var OpenTelemetryIntegration = class {
569
2185
  "gen_ai.usage.output_tokens": event.usage.outputTokens
570
2186
  })
571
2187
  );
2188
+ if (isStreamText && event.performance.timeToFirstOutputMs != null) {
2189
+ state.stepSpan.addEvent("ai.stream.firstChunk", {
2190
+ "ai.response.msToFirstChunk": event.performance.timeToFirstOutputMs
2191
+ });
2192
+ }
2193
+ if (isStreamText) {
2194
+ state.stepSpan.addEvent("ai.stream.finish", {
2195
+ "ai.response.msToFinish": event.performance.responseTimeMs,
2196
+ "ai.response.avgOutputTokensPerSecond": event.performance.effectiveOutputTokensPerSecond
2197
+ });
2198
+ }
572
2199
  state.stepSpan.end();
573
2200
  state.stepSpan = void 0;
574
2201
  state.stepContext = void 0;
575
2202
  }
576
- onFinish(event) {
2203
+ /** @deprecated Use `onStepEnd` instead. */
2204
+ onStepFinish(event) {
2205
+ this.onStepEnd(event);
2206
+ }
2207
+ onEnd(event) {
577
2208
  const state = this.getCallState(event.callId);
578
2209
  if (!(state == null ? void 0 : state.rootSpan)) return;
579
2210
  if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
580
- this.onEmbedOperationFinish(event);
2211
+ this.onEmbedOperationEnd(event);
581
2212
  return;
582
2213
  }
583
2214
  if (state.operationId === "ai.rerank") {
584
- this.onRerankOperationFinish(event);
2215
+ this.onRerankOperationEnd(event);
585
2216
  return;
586
2217
  }
587
2218
  if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
588
- this.onObjectOperationFinish(event);
2219
+ this.onObjectOperationEnd(event);
589
2220
  return;
590
2221
  }
591
- this.onGenerateFinish(event);
2222
+ this.onGenerateEnd(event);
592
2223
  }
593
- onGenerateFinish(event) {
594
- var _a, _b, _c, _d, _e;
2224
+ onGenerateEnd(event) {
2225
+ var _a, _b, _c, _d, _e, _f, _g;
595
2226
  const state = this.getCallState(event.callId);
596
2227
  if (!(state == null ? void 0 : state.rootSpan)) return;
597
2228
  const { telemetry } = state;
598
2229
  state.rootSpan.setAttributes(
599
- selectAttributes(telemetry, {
2230
+ selectAttributes2(telemetry, {
600
2231
  "ai.response.finishReason": event.finishReason,
601
2232
  "ai.response.text": {
602
2233
  output: () => {
@@ -605,7 +2236,7 @@ var OpenTelemetryIntegration = class {
605
2236
  }
606
2237
  },
607
2238
  "ai.response.reasoning": {
608
- output: () => event.reasoning.length > 0 ? event.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
2239
+ output: () => event.finalStep.reasoning.length > 0 ? event.finalStep.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
609
2240
  },
610
2241
  "ai.response.toolCalls": {
611
2242
  output: () => event.toolCalls.length > 0 ? JSON.stringify(
@@ -625,28 +2256,29 @@ var OpenTelemetryIntegration = class {
625
2256
  }))
626
2257
  ) : void 0
627
2258
  },
628
- "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0,
629
- "ai.usage.inputTokens": event.totalUsage.inputTokens,
630
- "ai.usage.outputTokens": event.totalUsage.outputTokens,
631
- "ai.usage.totalTokens": event.totalUsage.totalTokens,
632
- "ai.usage.reasoningTokens": event.totalUsage.reasoningTokens,
633
- "ai.usage.cachedInputTokens": event.totalUsage.cachedInputTokens,
634
- "ai.usage.inputTokenDetails.noCacheTokens": (_a = event.totalUsage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
635
- "ai.usage.inputTokenDetails.cacheReadTokens": (_b = event.totalUsage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
636
- "ai.usage.inputTokenDetails.cacheWriteTokens": (_c = event.totalUsage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
637
- "ai.usage.outputTokenDetails.textTokens": (_d = event.totalUsage.outputTokenDetails) == null ? void 0 : _d.textTokens,
638
- "ai.usage.outputTokenDetails.reasoningTokens": (_e = event.totalUsage.outputTokenDetails) == null ? void 0 : _e.reasoningTokens
2259
+ "ai.response.providerMetadata": event.finalStep.providerMetadata ? JSON.stringify(event.finalStep.providerMetadata) : void 0,
2260
+ "ai.usage.inputTokens": event.usage.inputTokens,
2261
+ "ai.usage.outputTokens": event.usage.outputTokens,
2262
+ "ai.usage.totalTokens": event.usage.totalTokens,
2263
+ "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2264
+ "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
2265
+ "ai.usage.inputTokenDetails.noCacheTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.noCacheTokens,
2266
+ "ai.usage.inputTokenDetails.cacheReadTokens": (_d = event.usage.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens,
2267
+ "ai.usage.inputTokenDetails.cacheWriteTokens": (_e = event.usage.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
2268
+ "ai.usage.outputTokenDetails.textTokens": (_f = event.usage.outputTokenDetails) == null ? void 0 : _f.textTokens,
2269
+ "ai.usage.outputTokenDetails.reasoningTokens": (_g = event.usage.outputTokenDetails) == null ? void 0 : _g.reasoningTokens
639
2270
  })
640
2271
  );
641
2272
  state.rootSpan.end();
642
2273
  this.cleanupCallState(event.callId);
643
2274
  }
644
- onObjectOperationFinish(event) {
2275
+ onObjectOperationEnd(event) {
2276
+ var _a, _b;
645
2277
  const state = this.getCallState(event.callId);
646
2278
  if (!(state == null ? void 0 : state.rootSpan)) return;
647
2279
  const { telemetry } = state;
648
2280
  state.rootSpan.setAttributes(
649
- selectAttributes(telemetry, {
2281
+ selectAttributes2(telemetry, {
650
2282
  "ai.response.finishReason": event.finishReason,
651
2283
  "ai.response.object": {
652
2284
  output: () => event.object != null ? JSON.stringify(event.object) : void 0
@@ -655,20 +2287,20 @@ var OpenTelemetryIntegration = class {
655
2287
  "ai.usage.inputTokens": event.usage.inputTokens,
656
2288
  "ai.usage.outputTokens": event.usage.outputTokens,
657
2289
  "ai.usage.totalTokens": event.usage.totalTokens,
658
- "ai.usage.reasoningTokens": event.usage.reasoningTokens,
659
- "ai.usage.cachedInputTokens": event.usage.cachedInputTokens
2290
+ "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2291
+ "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens
660
2292
  })
661
2293
  );
662
2294
  state.rootSpan.end();
663
2295
  this.cleanupCallState(event.callId);
664
2296
  }
665
- onEmbedOperationFinish(event) {
2297
+ onEmbedOperationEnd(event) {
666
2298
  const state = this.getCallState(event.callId);
667
2299
  if (!(state == null ? void 0 : state.rootSpan)) return;
668
2300
  const { telemetry } = state;
669
2301
  const isMany = state.operationId === "ai.embedMany";
670
2302
  state.rootSpan.setAttributes(
671
- selectAttributes(telemetry, {
2303
+ selectAttributes2(telemetry, {
672
2304
  ...isMany ? {
673
2305
  "ai.embeddings": {
674
2306
  output: () => event.embedding.map((e) => JSON.stringify(e))
@@ -688,7 +2320,7 @@ var OpenTelemetryIntegration = class {
688
2320
  const state = this.getCallState(event.callId);
689
2321
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
690
2322
  const { telemetry } = state;
691
- const attributes = selectAttributes(telemetry, {
2323
+ const attributes = selectAttributes2(telemetry, {
692
2324
  ...assembleOperationName({
693
2325
  operationId: event.operationId,
694
2326
  telemetry
@@ -703,13 +2335,13 @@ var OpenTelemetryIntegration = class {
703
2335
  { attributes },
704
2336
  state.rootContext
705
2337
  );
706
- const embedContext = import_api.trace.setSpan(state.rootContext, embedSpan);
2338
+ const embedContext = trace2.setSpan(state.rootContext, embedSpan);
707
2339
  state.embedSpans.set(event.embedCallId, {
708
2340
  span: embedSpan,
709
2341
  context: embedContext
710
2342
  });
711
2343
  }
712
- onEmbedFinish(event) {
2344
+ onEmbedEnd(event) {
713
2345
  const state = this.getCallState(event.callId);
714
2346
  if (!state) return;
715
2347
  const embedSpanEntry = state.embedSpans.get(event.embedCallId);
@@ -717,7 +2349,7 @@ var OpenTelemetryIntegration = class {
717
2349
  const { span } = embedSpanEntry;
718
2350
  const { telemetry } = state;
719
2351
  span.setAttributes(
720
- selectAttributes(telemetry, {
2352
+ selectAttributes2(telemetry, {
721
2353
  "ai.embeddings": {
722
2354
  output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
723
2355
  },
@@ -729,22 +2361,20 @@ var OpenTelemetryIntegration = class {
729
2361
  }
730
2362
  onRerankOperationStart(event) {
731
2363
  const telemetry = {
732
- isEnabled: event.isEnabled,
733
2364
  recordInputs: event.recordInputs,
734
2365
  recordOutputs: event.recordOutputs,
735
- functionId: event.functionId,
736
- metadata: event.metadata
2366
+ functionId: event.functionId
737
2367
  };
738
2368
  const settings = {
739
2369
  maxRetries: event.maxRetries
740
2370
  };
741
2371
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
742
2372
  model: { provider: event.provider, modelId: event.modelId },
743
- telemetry,
744
2373
  headers: event.headers,
745
- settings
2374
+ settings,
2375
+ context: void 0
746
2376
  });
747
- const attributes = selectAttributes(telemetry, {
2377
+ const attributes = selectAttributes2(telemetry, {
748
2378
  ...assembleOperationName({
749
2379
  operationId: event.operationId,
750
2380
  telemetry
@@ -755,7 +2385,7 @@ var OpenTelemetryIntegration = class {
755
2385
  }
756
2386
  });
757
2387
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
758
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
2388
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
759
2389
  this.callStates.set(event.callId, {
760
2390
  operationId: event.operationId,
761
2391
  telemetry,
@@ -770,7 +2400,7 @@ var OpenTelemetryIntegration = class {
770
2400
  settings
771
2401
  });
772
2402
  }
773
- onRerankOperationFinish(event) {
2403
+ onRerankOperationEnd(event) {
774
2404
  const state = this.getCallState(event.callId);
775
2405
  if (!(state == null ? void 0 : state.rootSpan)) return;
776
2406
  state.rootSpan.end();
@@ -780,7 +2410,7 @@ var OpenTelemetryIntegration = class {
780
2410
  const state = this.getCallState(event.callId);
781
2411
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
782
2412
  const { telemetry } = state;
783
- const attributes = selectAttributes(telemetry, {
2413
+ const attributes = selectAttributes2(telemetry, {
784
2414
  ...assembleOperationName({
785
2415
  operationId: event.operationId,
786
2416
  telemetry
@@ -795,16 +2425,16 @@ var OpenTelemetryIntegration = class {
795
2425
  { attributes },
796
2426
  state.rootContext
797
2427
  );
798
- const rerankContext = import_api.trace.setSpan(state.rootContext, rerankSpan);
2428
+ const rerankContext = trace2.setSpan(state.rootContext, rerankSpan);
799
2429
  state.rerankSpan = { span: rerankSpan, context: rerankContext };
800
2430
  }
801
- onRerankFinish(event) {
2431
+ onRerankEnd(event) {
802
2432
  const state = this.getCallState(event.callId);
803
2433
  if (!(state == null ? void 0 : state.rerankSpan)) return;
804
2434
  const { span } = state.rerankSpan;
805
2435
  const { telemetry } = state;
806
2436
  span.setAttributes(
807
- selectAttributes(telemetry, {
2437
+ selectAttributes2(telemetry, {
808
2438
  "ai.ranking.type": event.documentsType,
809
2439
  "ai.ranking": {
810
2440
  output: () => event.ranking.map((r) => JSON.stringify(r))
@@ -814,26 +2444,28 @@ var OpenTelemetryIntegration = class {
814
2444
  span.end();
815
2445
  state.rerankSpan = void 0;
816
2446
  }
817
- onChunk(event) {
818
- var _a;
819
- const chunk = event.chunk;
820
- if (typeof chunk.callId !== "string") {
821
- return;
2447
+ onAbort(event) {
2448
+ const state = this.getCallState(event.callId);
2449
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2450
+ for (const { span: toolSpan } of state.toolSpans.values()) {
2451
+ toolSpan.end();
822
2452
  }
823
- if (chunk.type !== "ai.stream.firstChunk" && chunk.type !== "ai.stream.finish") {
824
- return;
2453
+ state.toolSpans.clear();
2454
+ if (state.stepSpan) {
2455
+ state.stepSpan.end();
2456
+ state.stepSpan = void 0;
2457
+ state.stepContext = void 0;
825
2458
  }
826
- const state = this.getCallState(chunk.callId);
827
- if (!(state == null ? void 0 : state.stepSpan)) return;
828
- const attributes = Object.fromEntries(
829
- Object.entries(
830
- (_a = chunk.attributes) != null ? _a : {}
831
- ).filter(([, value]) => value != null)
832
- );
833
- state.stepSpan.addEvent(chunk.type, attributes);
834
- if (Object.keys(attributes).length > 0) {
835
- state.stepSpan.setAttributes(attributes);
2459
+ for (const { span: embedSpan } of state.embedSpans.values()) {
2460
+ embedSpan.end();
836
2461
  }
2462
+ state.embedSpans.clear();
2463
+ if (state.rerankSpan) {
2464
+ state.rerankSpan.span.end();
2465
+ state.rerankSpan = void 0;
2466
+ }
2467
+ state.rootSpan.end();
2468
+ this.cleanupCallState(event.callId);
837
2469
  }
838
2470
  onError(error) {
839
2471
  var _a;
@@ -861,8 +2493,8 @@ var OpenTelemetryIntegration = class {
861
2493
  this.cleanupCallState(event.callId);
862
2494
  }
863
2495
  };
864
- // Annotate the CommonJS export names for ESM import in node:
865
- 0 && (module.exports = {
866
- OpenTelemetryIntegration
867
- });
2496
+ export {
2497
+ LegacyOpenTelemetry,
2498
+ OpenTelemetry
2499
+ };
868
2500
  //# sourceMappingURL=index.js.map