@ai-sdk/otel 0.0.0-6b196531-20260710185421

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 ADDED
@@ -0,0 +1,2500 @@
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
548
+ };
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);
1609
+ }
1610
+ };
1611
+
1612
+ // src/legacy-open-telemetry.ts
1613
+ import {
1614
+ context as context3,
1615
+ SpanStatusCode as SpanStatusCode2,
1616
+ trace as trace2
1617
+ } from "@opentelemetry/api";
1618
+
1619
+ // src/assemble-operation-name.ts
1620
+ function assembleOperationName({
1621
+ operationId,
1622
+ telemetry
1623
+ }) {
1624
+ return {
1625
+ // standardized operation and resource name:
1626
+ "operation.name": `${operationId}${(telemetry == null ? void 0 : telemetry.functionId) != null ? ` ${telemetry.functionId}` : ""}`,
1627
+ "resource.name": telemetry == null ? void 0 : telemetry.functionId,
1628
+ // detailed, AI SDK specific data:
1629
+ "ai.operationId": operationId,
1630
+ "ai.telemetry.functionId": telemetry == null ? void 0 : telemetry.functionId
1631
+ };
1632
+ }
1633
+
1634
+ // src/get-base-telemetry-attributes.ts
1635
+ function getBaseTelemetryAttributes({
1636
+ model,
1637
+ settings,
1638
+ headers,
1639
+ context: context4
1640
+ }) {
1641
+ return {
1642
+ "ai.model.provider": model.provider,
1643
+ "ai.model.id": model.modelId,
1644
+ // settings:
1645
+ ...Object.entries(settings).reduce((attributes, [key, value]) => {
1646
+ attributes[`ai.settings.${key}`] = value;
1647
+ return attributes;
1648
+ }, {}),
1649
+ // add context as attributes:
1650
+ ...getRuntimeContextAttributes(context4),
1651
+ // request headers
1652
+ ...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
1653
+ if (value !== void 0) {
1654
+ attributes[`ai.request.headers.${key}`] = value;
1655
+ }
1656
+ return attributes;
1657
+ }, {})
1658
+ };
1659
+ }
1660
+
1661
+ // src/stringify-for-telemetry.ts
1662
+ import { convertDataContentToBase64String as convertDataContentToBase64String2 } from "ai";
1663
+ function stringifyForTelemetry(prompt) {
1664
+ return JSON.stringify(
1665
+ prompt.map((message) => ({
1666
+ ...message,
1667
+ content: typeof message.content === "string" ? message.content : message.content.map(
1668
+ (part) => part.type === "file" ? {
1669
+ ...part,
1670
+ data: serializeFileData(part.data)
1671
+ } : part
1672
+ )
1673
+ }))
1674
+ );
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
+ }
1688
+
1689
+ // src/legacy-open-telemetry.ts
1690
+ function recordSpanError(span, error) {
1691
+ if (error instanceof Error) {
1692
+ span.recordException({
1693
+ name: error.name,
1694
+ message: error.message,
1695
+ stack: error.stack
1696
+ });
1697
+ span.setStatus({
1698
+ code: SpanStatusCode2.ERROR,
1699
+ message: error.message
1700
+ });
1701
+ } else {
1702
+ span.setStatus({ code: SpanStatusCode2.ERROR });
1703
+ }
1704
+ }
1705
+ function shouldRecord2(telemetry) {
1706
+ return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
1707
+ }
1708
+ function selectAttributes2(telemetry, attributes) {
1709
+ if (!shouldRecord2(telemetry)) {
1710
+ return {};
1711
+ }
1712
+ const result = {};
1713
+ for (const [key, value] of Object.entries(attributes)) {
1714
+ if (value == null) continue;
1715
+ if (typeof value === "object" && "input" in value && typeof value.input === "function") {
1716
+ if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
1717
+ const resolved = value.input();
1718
+ if (resolved != null) {
1719
+ const sanitized2 = sanitizeAttributeValue(resolved);
1720
+ if (sanitized2 != null) result[key] = sanitized2;
1721
+ }
1722
+ continue;
1723
+ }
1724
+ if (typeof value === "object" && "output" in value && typeof value.output === "function") {
1725
+ if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
1726
+ const resolved = value.output();
1727
+ if (resolved != null) {
1728
+ const sanitized2 = sanitizeAttributeValue(resolved);
1729
+ if (sanitized2 != null) result[key] = sanitized2;
1730
+ }
1731
+ continue;
1732
+ }
1733
+ const sanitized = sanitizeAttributeValue(value);
1734
+ if (sanitized != null) result[key] = sanitized;
1735
+ }
1736
+ return result;
1737
+ }
1738
+ var LegacyOpenTelemetry = class {
1739
+ constructor(options = {}) {
1740
+ this.callStates = /* @__PURE__ */ new Map();
1741
+ var _a;
1742
+ this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
1743
+ }
1744
+ getCallState(callId) {
1745
+ return this.callStates.get(callId);
1746
+ }
1747
+ cleanupCallState(callId) {
1748
+ this.callStates.delete(callId);
1749
+ }
1750
+ executeTool({
1751
+ callId,
1752
+ toolCallId,
1753
+ execute
1754
+ }) {
1755
+ var _a;
1756
+ const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
1757
+ if (toolSpanEntry == null) {
1758
+ return execute();
1759
+ }
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);
1776
+ }
1777
+ onStart(event) {
1778
+ if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
1779
+ this.onEmbedOperationStart(event);
1780
+ return;
1781
+ }
1782
+ if (event.operationId === "ai.rerank") {
1783
+ this.onRerankOperationStart(
1784
+ event
1785
+ );
1786
+ return;
1787
+ }
1788
+ if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
1789
+ this.onObjectOperationStart(
1790
+ event
1791
+ );
1792
+ return;
1793
+ }
1794
+ this.onGenerateStart(event);
1795
+ }
1796
+ onGenerateStart(event) {
1797
+ const telemetry = {
1798
+ recordInputs: event.recordInputs,
1799
+ recordOutputs: event.recordOutputs,
1800
+ functionId: event.functionId
1801
+ };
1802
+ const settings = {
1803
+ maxOutputTokens: event.maxOutputTokens,
1804
+ temperature: event.temperature,
1805
+ topP: event.topP,
1806
+ topK: event.topK,
1807
+ presencePenalty: event.presencePenalty,
1808
+ frequencyPenalty: event.frequencyPenalty,
1809
+ stopSequences: event.stopSequences,
1810
+ seed: event.seed,
1811
+ maxRetries: event.maxRetries
1812
+ };
1813
+ const baseTelemetryAttributes = getBaseTelemetryAttributes({
1814
+ model: { provider: event.provider, modelId: event.modelId },
1815
+ headers: event.headers,
1816
+ settings,
1817
+ context: event.runtimeContext
1818
+ });
1819
+ const attributes = selectAttributes2(telemetry, {
1820
+ ...assembleOperationName({
1821
+ operationId: event.operationId,
1822
+ telemetry
1823
+ }),
1824
+ ...baseTelemetryAttributes,
1825
+ "ai.model.provider": event.provider,
1826
+ "ai.model.id": event.modelId,
1827
+ "ai.prompt": {
1828
+ input: () => JSON.stringify({
1829
+ system: event.instructions,
1830
+ messages: event.messages
1831
+ })
1832
+ }
1833
+ });
1834
+ const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
1835
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
1836
+ this.callStates.set(event.callId, {
1837
+ operationId: event.operationId,
1838
+ telemetry,
1839
+ rootSpan,
1840
+ rootContext,
1841
+ stepSpan: void 0,
1842
+ stepContext: void 0,
1843
+ embedSpans: /* @__PURE__ */ new Map(),
1844
+ rerankSpan: void 0,
1845
+ toolSpans: /* @__PURE__ */ new Map(),
1846
+ baseTelemetryAttributes,
1847
+ settings
1848
+ });
1849
+ }
1850
+ onObjectOperationStart(event) {
1851
+ const telemetry = {
1852
+ recordInputs: event.recordInputs,
1853
+ recordOutputs: event.recordOutputs,
1854
+ functionId: event.functionId
1855
+ };
1856
+ const settings = {
1857
+ maxOutputTokens: event.maxOutputTokens,
1858
+ temperature: event.temperature,
1859
+ topP: event.topP,
1860
+ topK: event.topK,
1861
+ presencePenalty: event.presencePenalty,
1862
+ frequencyPenalty: event.frequencyPenalty,
1863
+ seed: event.seed,
1864
+ maxRetries: event.maxRetries
1865
+ };
1866
+ const baseTelemetryAttributes = getBaseTelemetryAttributes({
1867
+ model: { provider: event.provider, modelId: event.modelId },
1868
+ headers: event.headers,
1869
+ settings,
1870
+ context: void 0
1871
+ });
1872
+ const attributes = selectAttributes2(telemetry, {
1873
+ ...assembleOperationName({
1874
+ operationId: event.operationId,
1875
+ telemetry
1876
+ }),
1877
+ ...baseTelemetryAttributes,
1878
+ "ai.prompt": {
1879
+ input: () => JSON.stringify({
1880
+ system: event.system,
1881
+ prompt: event.prompt,
1882
+ messages: event.messages
1883
+ })
1884
+ },
1885
+ "ai.schema": event.schema ? { input: () => JSON.stringify(event.schema) } : void 0,
1886
+ "ai.schema.name": event.schemaName,
1887
+ "ai.schema.description": event.schemaDescription,
1888
+ "ai.settings.output": event.output
1889
+ });
1890
+ const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
1891
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
1892
+ this.callStates.set(event.callId, {
1893
+ operationId: event.operationId,
1894
+ telemetry,
1895
+ rootSpan,
1896
+ rootContext,
1897
+ stepSpan: void 0,
1898
+ stepContext: void 0,
1899
+ embedSpans: /* @__PURE__ */ new Map(),
1900
+ rerankSpan: void 0,
1901
+ toolSpans: /* @__PURE__ */ new Map(),
1902
+ baseTelemetryAttributes,
1903
+ settings
1904
+ });
1905
+ }
1906
+ /** @deprecated */
1907
+ onObjectStepStart(event) {
1908
+ var _a;
1909
+ const state = this.getCallState(event.callId);
1910
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1911
+ const { telemetry } = state;
1912
+ const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
1913
+ const attributes = selectAttributes2(telemetry, {
1914
+ ...assembleOperationName({
1915
+ operationId: stepOperationId,
1916
+ telemetry
1917
+ }),
1918
+ ...state.baseTelemetryAttributes,
1919
+ "ai.prompt.messages": {
1920
+ input: () => event.promptMessages ? stringifyForTelemetry(event.promptMessages) : void 0
1921
+ },
1922
+ "gen_ai.system": event.provider,
1923
+ "gen_ai.request.model": event.modelId,
1924
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
1925
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
1926
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
1927
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
1928
+ "gen_ai.request.top_k": state.settings.topK,
1929
+ "gen_ai.request.top_p": state.settings.topP
1930
+ });
1931
+ state.stepSpan = this.tracer.startSpan(
1932
+ stepOperationId,
1933
+ { attributes },
1934
+ state.rootContext
1935
+ );
1936
+ state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
1937
+ }
1938
+ /** @deprecated */
1939
+ onObjectStepEnd(event) {
1940
+ var _a, _b;
1941
+ const state = this.getCallState(event.callId);
1942
+ if (!(state == null ? void 0 : state.stepSpan)) return;
1943
+ const { telemetry } = state;
1944
+ state.stepSpan.setAttributes(
1945
+ selectAttributes2(telemetry, {
1946
+ "ai.response.finishReason": event.finishReason,
1947
+ "ai.response.object": {
1948
+ output: () => {
1949
+ try {
1950
+ return JSON.stringify(JSON.parse(event.objectText));
1951
+ } catch (e) {
1952
+ return event.objectText;
1953
+ }
1954
+ }
1955
+ },
1956
+ "ai.response.id": event.response.id,
1957
+ "ai.response.model": event.response.modelId,
1958
+ "ai.response.timestamp": event.response.timestamp.toISOString(),
1959
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0,
1960
+ "ai.usage.inputTokens": event.usage.inputTokens,
1961
+ "ai.usage.outputTokens": event.usage.outputTokens,
1962
+ "ai.usage.totalTokens": event.usage.totalTokens,
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,
1965
+ "gen_ai.response.finish_reasons": [event.finishReason],
1966
+ "gen_ai.response.id": event.response.id,
1967
+ "gen_ai.response.model": event.response.modelId,
1968
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
1969
+ "gen_ai.usage.output_tokens": event.usage.outputTokens
1970
+ })
1971
+ );
1972
+ if (event.msToFirstChunk != null) {
1973
+ state.stepSpan.addEvent("ai.stream.firstChunk", {
1974
+ "ai.stream.msToFirstChunk": event.msToFirstChunk
1975
+ });
1976
+ state.stepSpan.setAttributes({
1977
+ "ai.stream.msToFirstChunk": event.msToFirstChunk
1978
+ });
1979
+ }
1980
+ state.stepSpan.end();
1981
+ state.stepSpan = void 0;
1982
+ state.stepContext = void 0;
1983
+ }
1984
+ onEmbedOperationStart(event) {
1985
+ const telemetry = {
1986
+ recordInputs: event.recordInputs,
1987
+ recordOutputs: event.recordOutputs,
1988
+ functionId: event.functionId
1989
+ };
1990
+ const settings = {
1991
+ maxRetries: event.maxRetries
1992
+ };
1993
+ const baseTelemetryAttributes = getBaseTelemetryAttributes({
1994
+ model: { provider: event.provider, modelId: event.modelId },
1995
+ headers: event.headers,
1996
+ settings,
1997
+ context: void 0
1998
+ });
1999
+ const value = event.value;
2000
+ const isMany = event.operationId === "ai.embedMany";
2001
+ const attributes = selectAttributes2(telemetry, {
2002
+ ...assembleOperationName({
2003
+ operationId: event.operationId,
2004
+ telemetry
2005
+ }),
2006
+ ...baseTelemetryAttributes,
2007
+ ...isMany ? {
2008
+ "ai.values": {
2009
+ input: () => value.map((v) => JSON.stringify(v))
2010
+ }
2011
+ } : {
2012
+ "ai.value": {
2013
+ input: () => JSON.stringify(value)
2014
+ }
2015
+ }
2016
+ });
2017
+ const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
2018
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
2019
+ this.callStates.set(event.callId, {
2020
+ operationId: event.operationId,
2021
+ telemetry,
2022
+ rootSpan,
2023
+ rootContext,
2024
+ stepSpan: void 0,
2025
+ stepContext: void 0,
2026
+ embedSpans: /* @__PURE__ */ new Map(),
2027
+ rerankSpan: void 0,
2028
+ toolSpans: /* @__PURE__ */ new Map(),
2029
+ baseTelemetryAttributes,
2030
+ settings
2031
+ });
2032
+ }
2033
+ onStepStart(event) {
2034
+ var _a;
2035
+ const state = this.getCallState(event.callId);
2036
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2037
+ const { telemetry } = state;
2038
+ const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
2039
+ const attributes = selectAttributes2(telemetry, {
2040
+ ...assembleOperationName({
2041
+ operationId: stepOperationId,
2042
+ telemetry
2043
+ }),
2044
+ ...state.baseTelemetryAttributes,
2045
+ "ai.model.provider": event.provider,
2046
+ "ai.model.id": event.modelId,
2047
+ "ai.prompt.messages": {
2048
+ input: () => event.promptMessages ? stringifyForTelemetry(event.promptMessages) : void 0
2049
+ },
2050
+ "ai.prompt.tools": {
2051
+ input: () => {
2052
+ var _a2;
2053
+ return (_a2 = event.stepTools) == null ? void 0 : _a2.map((tool) => JSON.stringify(tool));
2054
+ }
2055
+ },
2056
+ "ai.prompt.toolChoice": {
2057
+ input: () => event.stepToolChoice != null ? JSON.stringify(event.stepToolChoice) : void 0
2058
+ },
2059
+ "gen_ai.system": event.provider,
2060
+ "gen_ai.request.model": event.modelId,
2061
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
2062
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
2063
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
2064
+ "gen_ai.request.stop_sequences": state.settings.stopSequences,
2065
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
2066
+ "gen_ai.request.top_k": state.settings.topK,
2067
+ "gen_ai.request.top_p": state.settings.topP
2068
+ });
2069
+ state.stepSpan = this.tracer.startSpan(
2070
+ stepOperationId,
2071
+ { attributes },
2072
+ state.rootContext
2073
+ );
2074
+ state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
2075
+ }
2076
+ onToolExecutionStart(event) {
2077
+ const state = this.getCallState(event.callId);
2078
+ if (!(state == null ? void 0 : state.stepContext)) return;
2079
+ const { telemetry } = state;
2080
+ const { toolCall } = event;
2081
+ const attributes = selectAttributes2(telemetry, {
2082
+ ...assembleOperationName({
2083
+ operationId: "ai.toolCall",
2084
+ telemetry
2085
+ }),
2086
+ "ai.toolCall.name": toolCall.toolName,
2087
+ "ai.toolCall.id": toolCall.toolCallId,
2088
+ "ai.toolCall.args": {
2089
+ output: () => JSON.stringify(toolCall.input)
2090
+ }
2091
+ });
2092
+ const toolSpan = this.tracer.startSpan(
2093
+ "ai.toolCall",
2094
+ { attributes },
2095
+ state.stepContext
2096
+ );
2097
+ const toolContext = trace2.setSpan(state.stepContext, toolSpan);
2098
+ state.toolSpans.set(toolCall.toolCallId, {
2099
+ span: toolSpan,
2100
+ context: toolContext
2101
+ });
2102
+ }
2103
+ onToolExecutionEnd(event) {
2104
+ const state = this.getCallState(event.callId);
2105
+ if (!state) return;
2106
+ const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
2107
+ if (!toolSpanEntry) return;
2108
+ const { span } = toolSpanEntry;
2109
+ const { telemetry } = state;
2110
+ const { toolOutput } = event;
2111
+ if (toolOutput.type === "tool-result") {
2112
+ try {
2113
+ span.setAttributes(
2114
+ selectAttributes2(telemetry, {
2115
+ "ai.toolCall.result": {
2116
+ output: () => JSON.stringify(toolOutput.output)
2117
+ }
2118
+ })
2119
+ );
2120
+ } catch (e) {
2121
+ }
2122
+ } else {
2123
+ recordSpanError(span, toolOutput.error);
2124
+ }
2125
+ span.end();
2126
+ state.toolSpans.delete(event.toolCall.toolCallId);
2127
+ }
2128
+ onStepEnd(event) {
2129
+ var _a, _b, _c, _d, _e, _f, _g;
2130
+ const state = this.getCallState(event.callId);
2131
+ if (!(state == null ? void 0 : state.stepSpan)) return;
2132
+ const { telemetry } = state;
2133
+ const isStreamText = state.operationId === "ai.streamText";
2134
+ state.stepSpan.setAttributes(
2135
+ selectAttributes2(telemetry, {
2136
+ "ai.response.finishReason": event.finishReason,
2137
+ "ai.response.text": {
2138
+ output: () => {
2139
+ var _a2;
2140
+ return (_a2 = event.text) != null ? _a2 : void 0;
2141
+ }
2142
+ },
2143
+ "ai.response.reasoning": {
2144
+ output: () => event.reasoning.length > 0 ? event.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
2145
+ },
2146
+ "ai.response.toolCalls": {
2147
+ output: () => event.toolCalls.length > 0 ? JSON.stringify(
2148
+ event.toolCalls.map((toolCall) => ({
2149
+ toolCallId: toolCall.toolCallId,
2150
+ toolName: toolCall.toolName,
2151
+ input: toolCall.input
2152
+ }))
2153
+ ) : void 0
2154
+ },
2155
+ "ai.response.files": {
2156
+ output: () => event.files.length > 0 ? JSON.stringify(
2157
+ event.files.map((file) => ({
2158
+ type: "file",
2159
+ mediaType: file.mediaType,
2160
+ data: file.base64
2161
+ }))
2162
+ ) : void 0
2163
+ },
2164
+ "ai.response.id": event.response.id,
2165
+ "ai.response.model": event.response.modelId,
2166
+ "ai.response.timestamp": event.response.timestamp.toISOString(),
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,
2171
+ "ai.usage.inputTokens": event.usage.inputTokens,
2172
+ "ai.usage.outputTokens": event.usage.outputTokens,
2173
+ "ai.usage.totalTokens": event.usage.totalTokens,
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,
2181
+ "gen_ai.response.finish_reasons": [event.finishReason],
2182
+ "gen_ai.response.id": event.response.id,
2183
+ "gen_ai.response.model": event.response.modelId,
2184
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
2185
+ "gen_ai.usage.output_tokens": event.usage.outputTokens
2186
+ })
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
+ }
2199
+ state.stepSpan.end();
2200
+ state.stepSpan = void 0;
2201
+ state.stepContext = void 0;
2202
+ }
2203
+ /** @deprecated Use `onStepEnd` instead. */
2204
+ onStepFinish(event) {
2205
+ this.onStepEnd(event);
2206
+ }
2207
+ onEnd(event) {
2208
+ const state = this.getCallState(event.callId);
2209
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2210
+ if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
2211
+ this.onEmbedOperationEnd(event);
2212
+ return;
2213
+ }
2214
+ if (state.operationId === "ai.rerank") {
2215
+ this.onRerankOperationEnd(event);
2216
+ return;
2217
+ }
2218
+ if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
2219
+ this.onObjectOperationEnd(event);
2220
+ return;
2221
+ }
2222
+ this.onGenerateEnd(event);
2223
+ }
2224
+ onGenerateEnd(event) {
2225
+ var _a, _b, _c, _d, _e, _f, _g;
2226
+ const state = this.getCallState(event.callId);
2227
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2228
+ const { telemetry } = state;
2229
+ state.rootSpan.setAttributes(
2230
+ selectAttributes2(telemetry, {
2231
+ "ai.response.finishReason": event.finishReason,
2232
+ "ai.response.text": {
2233
+ output: () => {
2234
+ var _a2;
2235
+ return (_a2 = event.text) != null ? _a2 : void 0;
2236
+ }
2237
+ },
2238
+ "ai.response.reasoning": {
2239
+ output: () => event.finalStep.reasoning.length > 0 ? event.finalStep.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
2240
+ },
2241
+ "ai.response.toolCalls": {
2242
+ output: () => event.toolCalls.length > 0 ? JSON.stringify(
2243
+ event.toolCalls.map((toolCall) => ({
2244
+ toolCallId: toolCall.toolCallId,
2245
+ toolName: toolCall.toolName,
2246
+ input: toolCall.input
2247
+ }))
2248
+ ) : void 0
2249
+ },
2250
+ "ai.response.files": {
2251
+ output: () => event.files.length > 0 ? JSON.stringify(
2252
+ event.files.map((file) => ({
2253
+ type: "file",
2254
+ mediaType: file.mediaType,
2255
+ data: file.base64
2256
+ }))
2257
+ ) : void 0
2258
+ },
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
2270
+ })
2271
+ );
2272
+ state.rootSpan.end();
2273
+ this.cleanupCallState(event.callId);
2274
+ }
2275
+ onObjectOperationEnd(event) {
2276
+ var _a, _b;
2277
+ const state = this.getCallState(event.callId);
2278
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2279
+ const { telemetry } = state;
2280
+ state.rootSpan.setAttributes(
2281
+ selectAttributes2(telemetry, {
2282
+ "ai.response.finishReason": event.finishReason,
2283
+ "ai.response.object": {
2284
+ output: () => event.object != null ? JSON.stringify(event.object) : void 0
2285
+ },
2286
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0,
2287
+ "ai.usage.inputTokens": event.usage.inputTokens,
2288
+ "ai.usage.outputTokens": event.usage.outputTokens,
2289
+ "ai.usage.totalTokens": event.usage.totalTokens,
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
2292
+ })
2293
+ );
2294
+ state.rootSpan.end();
2295
+ this.cleanupCallState(event.callId);
2296
+ }
2297
+ onEmbedOperationEnd(event) {
2298
+ const state = this.getCallState(event.callId);
2299
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2300
+ const { telemetry } = state;
2301
+ const isMany = state.operationId === "ai.embedMany";
2302
+ state.rootSpan.setAttributes(
2303
+ selectAttributes2(telemetry, {
2304
+ ...isMany ? {
2305
+ "ai.embeddings": {
2306
+ output: () => event.embedding.map((e) => JSON.stringify(e))
2307
+ }
2308
+ } : {
2309
+ "ai.embedding": {
2310
+ output: () => JSON.stringify(event.embedding)
2311
+ }
2312
+ },
2313
+ "ai.usage.tokens": event.usage.tokens
2314
+ })
2315
+ );
2316
+ state.rootSpan.end();
2317
+ this.cleanupCallState(event.callId);
2318
+ }
2319
+ onEmbedStart(event) {
2320
+ const state = this.getCallState(event.callId);
2321
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2322
+ const { telemetry } = state;
2323
+ const attributes = selectAttributes2(telemetry, {
2324
+ ...assembleOperationName({
2325
+ operationId: event.operationId,
2326
+ telemetry
2327
+ }),
2328
+ ...state.baseTelemetryAttributes,
2329
+ "ai.values": {
2330
+ input: () => event.values.map((v) => JSON.stringify(v))
2331
+ }
2332
+ });
2333
+ const embedSpan = this.tracer.startSpan(
2334
+ event.operationId,
2335
+ { attributes },
2336
+ state.rootContext
2337
+ );
2338
+ const embedContext = trace2.setSpan(state.rootContext, embedSpan);
2339
+ state.embedSpans.set(event.embedCallId, {
2340
+ span: embedSpan,
2341
+ context: embedContext
2342
+ });
2343
+ }
2344
+ onEmbedEnd(event) {
2345
+ const state = this.getCallState(event.callId);
2346
+ if (!state) return;
2347
+ const embedSpanEntry = state.embedSpans.get(event.embedCallId);
2348
+ if (!embedSpanEntry) return;
2349
+ const { span } = embedSpanEntry;
2350
+ const { telemetry } = state;
2351
+ span.setAttributes(
2352
+ selectAttributes2(telemetry, {
2353
+ "ai.embeddings": {
2354
+ output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
2355
+ },
2356
+ "ai.usage.tokens": event.usage.tokens
2357
+ })
2358
+ );
2359
+ span.end();
2360
+ state.embedSpans.delete(event.embedCallId);
2361
+ }
2362
+ onRerankOperationStart(event) {
2363
+ const telemetry = {
2364
+ recordInputs: event.recordInputs,
2365
+ recordOutputs: event.recordOutputs,
2366
+ functionId: event.functionId
2367
+ };
2368
+ const settings = {
2369
+ maxRetries: event.maxRetries
2370
+ };
2371
+ const baseTelemetryAttributes = getBaseTelemetryAttributes({
2372
+ model: { provider: event.provider, modelId: event.modelId },
2373
+ headers: event.headers,
2374
+ settings,
2375
+ context: void 0
2376
+ });
2377
+ const attributes = selectAttributes2(telemetry, {
2378
+ ...assembleOperationName({
2379
+ operationId: event.operationId,
2380
+ telemetry
2381
+ }),
2382
+ ...baseTelemetryAttributes,
2383
+ "ai.documents": {
2384
+ input: () => event.documents.map((d) => JSON.stringify(d))
2385
+ }
2386
+ });
2387
+ const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
2388
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
2389
+ this.callStates.set(event.callId, {
2390
+ operationId: event.operationId,
2391
+ telemetry,
2392
+ rootSpan,
2393
+ rootContext,
2394
+ stepSpan: void 0,
2395
+ stepContext: void 0,
2396
+ embedSpans: /* @__PURE__ */ new Map(),
2397
+ rerankSpan: void 0,
2398
+ toolSpans: /* @__PURE__ */ new Map(),
2399
+ baseTelemetryAttributes,
2400
+ settings
2401
+ });
2402
+ }
2403
+ onRerankOperationEnd(event) {
2404
+ const state = this.getCallState(event.callId);
2405
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2406
+ state.rootSpan.end();
2407
+ this.cleanupCallState(event.callId);
2408
+ }
2409
+ onRerankStart(event) {
2410
+ const state = this.getCallState(event.callId);
2411
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2412
+ const { telemetry } = state;
2413
+ const attributes = selectAttributes2(telemetry, {
2414
+ ...assembleOperationName({
2415
+ operationId: event.operationId,
2416
+ telemetry
2417
+ }),
2418
+ ...state.baseTelemetryAttributes,
2419
+ "ai.documents": {
2420
+ input: () => event.documents.map((d) => JSON.stringify(d))
2421
+ }
2422
+ });
2423
+ const rerankSpan = this.tracer.startSpan(
2424
+ event.operationId,
2425
+ { attributes },
2426
+ state.rootContext
2427
+ );
2428
+ const rerankContext = trace2.setSpan(state.rootContext, rerankSpan);
2429
+ state.rerankSpan = { span: rerankSpan, context: rerankContext };
2430
+ }
2431
+ onRerankEnd(event) {
2432
+ const state = this.getCallState(event.callId);
2433
+ if (!(state == null ? void 0 : state.rerankSpan)) return;
2434
+ const { span } = state.rerankSpan;
2435
+ const { telemetry } = state;
2436
+ span.setAttributes(
2437
+ selectAttributes2(telemetry, {
2438
+ "ai.ranking.type": event.documentsType,
2439
+ "ai.ranking": {
2440
+ output: () => event.ranking.map((r) => JSON.stringify(r))
2441
+ }
2442
+ })
2443
+ );
2444
+ span.end();
2445
+ state.rerankSpan = void 0;
2446
+ }
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();
2452
+ }
2453
+ state.toolSpans.clear();
2454
+ if (state.stepSpan) {
2455
+ state.stepSpan.end();
2456
+ state.stepSpan = void 0;
2457
+ state.stepContext = void 0;
2458
+ }
2459
+ for (const { span: embedSpan } of state.embedSpans.values()) {
2460
+ embedSpan.end();
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);
2469
+ }
2470
+ onError(error) {
2471
+ var _a;
2472
+ const event = error;
2473
+ if (!(event == null ? void 0 : event.callId)) return;
2474
+ const state = this.getCallState(event.callId);
2475
+ if (!(state == null ? void 0 : state.rootSpan)) return;
2476
+ const actualError = (_a = event.error) != null ? _a : error;
2477
+ if (state.stepSpan) {
2478
+ recordSpanError(state.stepSpan, actualError);
2479
+ state.stepSpan.end();
2480
+ }
2481
+ for (const { span: embedSpan } of state.embedSpans.values()) {
2482
+ recordSpanError(embedSpan, actualError);
2483
+ embedSpan.end();
2484
+ }
2485
+ state.embedSpans.clear();
2486
+ if (state.rerankSpan) {
2487
+ recordSpanError(state.rerankSpan.span, actualError);
2488
+ state.rerankSpan.span.end();
2489
+ state.rerankSpan = void 0;
2490
+ }
2491
+ recordSpanError(state.rootSpan, actualError);
2492
+ state.rootSpan.end();
2493
+ this.cleanupCallState(event.callId);
2494
+ }
2495
+ };
2496
+ export {
2497
+ LegacyOpenTelemetry,
2498
+ OpenTelemetry
2499
+ };
2500
+ //# sourceMappingURL=index.js.map