@ai-sdk/otel 1.0.0-beta.3 → 1.0.0-beta.30

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.mjs CHANGED
@@ -1,8 +1,1073 @@
1
+ // src/gen-ai-open-telemetry-integration.ts
2
+ import {
3
+ context,
4
+ SpanKind,
5
+ SpanStatusCode,
6
+ trace
7
+ } from "@opentelemetry/api";
8
+
9
+ // src/gen-ai-format-messages.ts
10
+ import { convertDataContentToBase64String } from "ai";
11
+ function mapProviderName(provider) {
12
+ const lower = provider.toLowerCase();
13
+ const wellKnownPrefixes = [
14
+ ["google.vertex", "gcp.vertex_ai"],
15
+ ["google.generative-ai", "gcp.gemini"],
16
+ ["google-vertex", "gcp.vertex_ai"],
17
+ ["amazon-bedrock", "aws.bedrock"],
18
+ ["azure-openai", "azure.ai.openai"],
19
+ ["anthropic", "anthropic"],
20
+ ["openai", "openai"],
21
+ ["azure", "azure.ai.inference"],
22
+ ["google", "gcp.gemini"],
23
+ ["mistral", "mistral_ai"],
24
+ ["cohere", "cohere"],
25
+ ["bedrock", "aws.bedrock"],
26
+ ["groq", "groq"],
27
+ ["deepseek", "deepseek"],
28
+ ["perplexity", "perplexity"],
29
+ ["xai", "x_ai"]
30
+ ];
31
+ for (const [prefix, mapped] of wellKnownPrefixes) {
32
+ if (lower === prefix || lower.startsWith(prefix + ".") || lower.startsWith(prefix + "-")) {
33
+ return mapped;
34
+ }
35
+ }
36
+ return provider;
37
+ }
38
+ function mapOperationName(operationId) {
39
+ var _a;
40
+ const mapping = {
41
+ "ai.generateText": "invoke_agent",
42
+ "ai.streamText": "invoke_agent",
43
+ "ai.generateObject": "invoke_agent",
44
+ "ai.streamObject": "invoke_agent",
45
+ "ai.embed": "embeddings",
46
+ "ai.embedMany": "embeddings",
47
+ "ai.rerank": "rerank"
48
+ };
49
+ return (_a = mapping[operationId]) != null ? _a : operationId;
50
+ }
51
+ function formatSystemInstructions(system) {
52
+ if (typeof system === "string") {
53
+ return [{ type: "text", content: system }];
54
+ }
55
+ if (Array.isArray(system)) {
56
+ return system.map((msg) => ({ type: "text", content: msg.content }));
57
+ }
58
+ return [{ type: "text", content: system.content }];
59
+ }
60
+ function convertMessagePartToSemConv(part) {
61
+ var _a, _b, _c, _d;
62
+ switch (part.type) {
63
+ case "text":
64
+ return { type: "text", content: part.text };
65
+ case "reasoning":
66
+ return { type: "reasoning", content: part.text };
67
+ case "tool-call":
68
+ return {
69
+ type: "tool_call",
70
+ id: (_a = part.toolCallId) != null ? _a : null,
71
+ name: part.toolName,
72
+ arguments: part.input
73
+ };
74
+ case "tool-result": {
75
+ const output = part.output;
76
+ let response;
77
+ if (output) {
78
+ if (output.type === "text" || output.type === "error-text") {
79
+ response = output.value;
80
+ } else if (output.type === "json" || output.type === "error-json") {
81
+ response = output.value;
82
+ } else if (output.type === "execution-denied") {
83
+ response = { denied: true, reason: output.reason };
84
+ } else {
85
+ response = output;
86
+ }
87
+ }
88
+ return {
89
+ type: "tool_call_response",
90
+ id: (_b = part.toolCallId) != null ? _b : null,
91
+ response
92
+ };
93
+ }
94
+ case "file": {
95
+ const data = part.data;
96
+ let content;
97
+ if (data instanceof Uint8Array) {
98
+ content = convertDataContentToBase64String(data);
99
+ } else if (typeof data === "string") {
100
+ if (data.startsWith("http://") || data.startsWith("https://")) {
101
+ return {
102
+ type: "uri",
103
+ modality: getModality(part.mediaType),
104
+ mime_type: (_c = part.mediaType) != null ? _c : null,
105
+ uri: data
106
+ };
107
+ }
108
+ content = data;
109
+ } else {
110
+ content = String(data);
111
+ }
112
+ return {
113
+ type: "blob",
114
+ modality: getModality(part.mediaType),
115
+ mime_type: (_d = part.mediaType) != null ? _d : null,
116
+ content
117
+ };
118
+ }
119
+ case "tool-approval-response":
120
+ return {
121
+ type: "tool_approval_response",
122
+ approval_id: part.approvalId,
123
+ approved: part.approved,
124
+ reason: part.reason
125
+ };
126
+ case "custom":
127
+ return { type: "custom", kind: part.kind };
128
+ case "reasoning-file":
129
+ return { type: String(part.type) };
130
+ default: {
131
+ const _exhaustive = part;
132
+ return { type: String(_exhaustive.type) };
133
+ }
134
+ }
135
+ }
136
+ function getModality(mediaType) {
137
+ if (!mediaType) return "image";
138
+ if (mediaType.startsWith("image/")) return "image";
139
+ if (mediaType.startsWith("video/")) return "video";
140
+ if (mediaType.startsWith("audio/")) return "audio";
141
+ return "image";
142
+ }
143
+ function formatInputMessages(prompt) {
144
+ return prompt.filter((msg) => msg.role !== "system").map((message) => {
145
+ if (message.role === "system") {
146
+ return {
147
+ role: "system",
148
+ parts: [{ type: "text", content: message.content }]
149
+ };
150
+ }
151
+ const parts = message.content.map(convertMessagePartToSemConv);
152
+ return { role: message.role, parts };
153
+ });
154
+ }
155
+ function formatModelMessages({
156
+ prompt,
157
+ messages
158
+ }) {
159
+ const result = [];
160
+ if (typeof prompt === "string") {
161
+ result.push({
162
+ role: "user",
163
+ parts: [{ type: "text", content: prompt }]
164
+ });
165
+ } else if (Array.isArray(prompt)) {
166
+ for (const msg of prompt) {
167
+ const converted = convertModelMessageToSemConv(msg);
168
+ if (converted) result.push(converted);
169
+ }
170
+ }
171
+ if (messages) {
172
+ for (const msg of messages) {
173
+ const converted = convertModelMessageToSemConv(msg);
174
+ if (converted) result.push(converted);
175
+ }
176
+ }
177
+ return result;
178
+ }
179
+ function convertModelMessageToSemConv(msg) {
180
+ if (msg.role === "system") return void 0;
181
+ if (msg.role === "user") {
182
+ if (typeof msg.content === "string") {
183
+ return {
184
+ role: "user",
185
+ parts: [{ type: "text", content: msg.content }]
186
+ };
187
+ }
188
+ const parts = msg.content.map((part) => {
189
+ var _a, _b, _c, _d, _e, _f, _g, _h;
190
+ switch (part.type) {
191
+ case "text":
192
+ return { type: "text", content: part.text };
193
+ case "image": {
194
+ const data = part.image;
195
+ if (data instanceof URL) {
196
+ return {
197
+ type: "uri",
198
+ modality: "image",
199
+ mime_type: (_a = part.mediaType) != null ? _a : null,
200
+ uri: data.toString()
201
+ };
202
+ }
203
+ if (typeof data === "string") {
204
+ if (data.startsWith("http://") || data.startsWith("https://")) {
205
+ return {
206
+ type: "uri",
207
+ modality: "image",
208
+ mime_type: (_b = part.mediaType) != null ? _b : null,
209
+ uri: data
210
+ };
211
+ }
212
+ return {
213
+ type: "blob",
214
+ modality: "image",
215
+ mime_type: (_c = part.mediaType) != null ? _c : null,
216
+ content: data
217
+ };
218
+ }
219
+ return {
220
+ type: "blob",
221
+ modality: "image",
222
+ mime_type: (_d = part.mediaType) != null ? _d : null,
223
+ content: convertDataContentToBase64String(data)
224
+ };
225
+ }
226
+ case "file": {
227
+ const data = part.data;
228
+ if (data instanceof URL) {
229
+ return {
230
+ type: "uri",
231
+ modality: getModality(part.mediaType),
232
+ mime_type: (_e = part.mediaType) != null ? _e : null,
233
+ uri: data.toString()
234
+ };
235
+ }
236
+ if (typeof data === "string") {
237
+ if (data.startsWith("http://") || data.startsWith("https://")) {
238
+ return {
239
+ type: "uri",
240
+ modality: getModality(part.mediaType),
241
+ mime_type: (_f = part.mediaType) != null ? _f : null,
242
+ uri: data
243
+ };
244
+ }
245
+ return {
246
+ type: "blob",
247
+ modality: getModality(part.mediaType),
248
+ mime_type: (_g = part.mediaType) != null ? _g : null,
249
+ content: data
250
+ };
251
+ }
252
+ return {
253
+ type: "blob",
254
+ modality: getModality(part.mediaType),
255
+ mime_type: (_h = part.mediaType) != null ? _h : null,
256
+ content: convertDataContentToBase64String(data)
257
+ };
258
+ }
259
+ default:
260
+ return { type: String(part.type) };
261
+ }
262
+ });
263
+ return { role: "user", parts };
264
+ }
265
+ if (msg.role === "assistant") {
266
+ if (typeof msg.content === "string") {
267
+ return {
268
+ role: "assistant",
269
+ parts: [{ type: "text", content: msg.content }]
270
+ };
271
+ }
272
+ const parts = msg.content.map((part) => {
273
+ var _a, _b;
274
+ switch (part.type) {
275
+ case "text":
276
+ return { type: "text", content: part.text };
277
+ case "reasoning":
278
+ return { type: "reasoning", content: part.text };
279
+ case "tool-call":
280
+ return {
281
+ type: "tool_call",
282
+ id: (_a = part.toolCallId) != null ? _a : null,
283
+ name: part.toolName,
284
+ arguments: part.input
285
+ };
286
+ case "tool-result": {
287
+ const output = part.output;
288
+ let response;
289
+ if (output) {
290
+ if (output.type === "text" || output.type === "error-text") {
291
+ response = output.value;
292
+ } else if (output.type === "json" || output.type === "error-json") {
293
+ response = output.value;
294
+ } else if (output.type === "execution-denied") {
295
+ response = { denied: true, reason: output.reason };
296
+ } else {
297
+ response = output;
298
+ }
299
+ }
300
+ return {
301
+ type: "tool_call_response",
302
+ id: (_b = part.toolCallId) != null ? _b : null,
303
+ response
304
+ };
305
+ }
306
+ default:
307
+ return { type: String(part.type) };
308
+ }
309
+ });
310
+ return { role: "assistant", parts };
311
+ }
312
+ if (msg.role === "tool") {
313
+ const parts = msg.content.map((part) => {
314
+ var _a;
315
+ if (part.type === "tool-result") {
316
+ const output = part.output;
317
+ let response;
318
+ if (output) {
319
+ if (output.type === "text" || output.type === "error-text") {
320
+ response = output.value;
321
+ } else if (output.type === "json" || output.type === "error-json") {
322
+ response = output.value;
323
+ } else if (output.type === "execution-denied") {
324
+ response = { denied: true, reason: output.reason };
325
+ } else {
326
+ response = output;
327
+ }
328
+ }
329
+ return {
330
+ type: "tool_call_response",
331
+ id: (_a = part.toolCallId) != null ? _a : null,
332
+ response
333
+ };
334
+ }
335
+ return { type: String(part.type) };
336
+ });
337
+ return { role: "tool", parts };
338
+ }
339
+ return void 0;
340
+ }
341
+ function formatOutputMessages({
342
+ text,
343
+ reasoning,
344
+ toolCalls,
345
+ files,
346
+ finishReason
347
+ }) {
348
+ const parts = [];
349
+ if (reasoning) {
350
+ for (const r of reasoning) {
351
+ if ("text" in r && r.text) {
352
+ parts.push({ type: "reasoning", content: r.text });
353
+ }
354
+ }
355
+ }
356
+ if (text != null && text.length > 0) {
357
+ parts.push({ type: "text", content: text });
358
+ }
359
+ if (toolCalls) {
360
+ for (const tc of toolCalls) {
361
+ parts.push({
362
+ type: "tool_call",
363
+ id: tc.toolCallId,
364
+ name: tc.toolName,
365
+ arguments: tc.input
366
+ });
367
+ }
368
+ }
369
+ if (files) {
370
+ for (const file of files) {
371
+ parts.push({
372
+ type: "blob",
373
+ modality: getModality(file.mediaType),
374
+ mime_type: file.mediaType,
375
+ content: file.base64
376
+ });
377
+ }
378
+ }
379
+ return [
380
+ {
381
+ role: "assistant",
382
+ parts,
383
+ finish_reason: mapFinishReason(finishReason)
384
+ }
385
+ ];
386
+ }
387
+ function formatObjectOutputMessages({
388
+ objectText,
389
+ finishReason
390
+ }) {
391
+ return [
392
+ {
393
+ role: "assistant",
394
+ parts: [{ type: "text", content: objectText }],
395
+ finish_reason: mapFinishReason(finishReason)
396
+ }
397
+ ];
398
+ }
399
+ function mapFinishReason(reason) {
400
+ var _a;
401
+ const mapping = {
402
+ stop: "stop",
403
+ length: "length",
404
+ "content-filter": "content_filter",
405
+ "tool-calls": "tool_call",
406
+ error: "error",
407
+ other: "stop",
408
+ unknown: "stop"
409
+ };
410
+ return (_a = mapping[reason]) != null ? _a : reason;
411
+ }
412
+
413
+ // src/gen-ai-open-telemetry-integration.ts
414
+ function recordSpanError(span, error) {
415
+ if (error instanceof Error) {
416
+ span.recordException({
417
+ name: error.name,
418
+ message: error.message,
419
+ stack: error.stack
420
+ });
421
+ span.setStatus({
422
+ code: SpanStatusCode.ERROR,
423
+ message: error.message
424
+ });
425
+ } else {
426
+ span.setStatus({ code: SpanStatusCode.ERROR });
427
+ }
428
+ }
429
+ function shouldRecord(telemetry) {
430
+ return (telemetry == null ? void 0 : telemetry.isEnabled) === true;
431
+ }
432
+ function selectAttributes(telemetry, attributes) {
433
+ if (!shouldRecord(telemetry)) {
434
+ return {};
435
+ }
436
+ const result = {};
437
+ for (const [key, value] of Object.entries(attributes)) {
438
+ if (value == null) continue;
439
+ if (typeof value === "object" && "input" in value && typeof value.input === "function") {
440
+ if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
441
+ const resolved = value.input();
442
+ if (resolved != null) result[key] = resolved;
443
+ continue;
444
+ }
445
+ if (typeof value === "object" && "output" in value && typeof value.output === "function") {
446
+ if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
447
+ const resolved = value.output();
448
+ if (resolved != null) result[key] = resolved;
449
+ continue;
450
+ }
451
+ result[key] = value;
452
+ }
453
+ return result;
454
+ }
455
+ var GenAIOpenTelemetryIntegration = class {
456
+ constructor(options = {}) {
457
+ this.callStates = /* @__PURE__ */ new Map();
458
+ var _a;
459
+ this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
460
+ }
461
+ getCallState(callId) {
462
+ return this.callStates.get(callId);
463
+ }
464
+ cleanupCallState(callId) {
465
+ this.callStates.delete(callId);
466
+ }
467
+ executeTool({
468
+ callId,
469
+ toolCallId,
470
+ execute
471
+ }) {
472
+ var _a;
473
+ const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
474
+ if (toolSpanEntry == null) {
475
+ return execute();
476
+ }
477
+ return context.with(toolSpanEntry.context, execute);
478
+ }
479
+ onStart(event) {
480
+ if (event.isEnabled !== true) return;
481
+ if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
482
+ this.onEmbedOperationStart(event);
483
+ return;
484
+ }
485
+ if (event.operationId === "ai.rerank") {
486
+ this.onRerankOperationStart(event);
487
+ return;
488
+ }
489
+ if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
490
+ this.onObjectOperationStart(event);
491
+ return;
492
+ }
493
+ this.onGenerateStart(event);
494
+ }
495
+ onGenerateStart(event) {
496
+ var _a;
497
+ const telemetry = {
498
+ isEnabled: event.isEnabled,
499
+ recordInputs: event.recordInputs,
500
+ recordOutputs: event.recordOutputs,
501
+ functionId: event.functionId,
502
+ metadata: event.metadata
503
+ };
504
+ const settings = {
505
+ maxOutputTokens: event.maxOutputTokens,
506
+ temperature: event.temperature,
507
+ topP: event.topP,
508
+ topK: event.topK,
509
+ presencePenalty: event.presencePenalty,
510
+ frequencyPenalty: event.frequencyPenalty,
511
+ stopSequences: event.stopSequences,
512
+ seed: event.seed,
513
+ maxRetries: event.maxRetries
514
+ };
515
+ const providerName = mapProviderName(event.provider);
516
+ const operationName = mapOperationName(event.operationId);
517
+ const attributes = selectAttributes(telemetry, {
518
+ "gen_ai.operation.name": operationName,
519
+ "gen_ai.provider.name": providerName,
520
+ "gen_ai.request.model": event.modelId,
521
+ "gen_ai.agent.name": telemetry.functionId,
522
+ "gen_ai.request.frequency_penalty": event.frequencyPenalty,
523
+ "gen_ai.request.max_tokens": event.maxOutputTokens,
524
+ "gen_ai.request.presence_penalty": event.presencePenalty,
525
+ "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
526
+ "gen_ai.request.top_k": event.topK,
527
+ "gen_ai.request.top_p": event.topP,
528
+ "gen_ai.request.stop_sequences": event.stopSequences,
529
+ "gen_ai.request.seed": event.seed,
530
+ "gen_ai.system_instructions": event.system ? {
531
+ input: () => JSON.stringify(formatSystemInstructions(event.system))
532
+ } : void 0,
533
+ "gen_ai.input.messages": {
534
+ input: () => JSON.stringify(
535
+ formatModelMessages({
536
+ prompt: event.prompt,
537
+ messages: event.messages
538
+ })
539
+ )
540
+ }
541
+ });
542
+ const spanName = `${operationName} ${event.modelId}`;
543
+ const rootSpan = this.tracer.startSpan(spanName, {
544
+ attributes,
545
+ kind: SpanKind.INTERNAL
546
+ });
547
+ const rootContext = trace.setSpan(context.active(), rootSpan);
548
+ this.callStates.set(event.callId, {
549
+ operationId: event.operationId,
550
+ telemetry,
551
+ rootSpan,
552
+ rootContext,
553
+ stepSpan: void 0,
554
+ stepContext: void 0,
555
+ embedSpans: /* @__PURE__ */ new Map(),
556
+ rerankSpan: void 0,
557
+ toolSpans: /* @__PURE__ */ new Map(),
558
+ settings,
559
+ provider: event.provider,
560
+ modelId: event.modelId
561
+ });
562
+ }
563
+ onObjectOperationStart(event) {
564
+ var _a;
565
+ const telemetry = {
566
+ isEnabled: event.isEnabled,
567
+ recordInputs: event.recordInputs,
568
+ recordOutputs: event.recordOutputs,
569
+ functionId: event.functionId,
570
+ metadata: event.metadata
571
+ };
572
+ const settings = {
573
+ maxOutputTokens: event.maxOutputTokens,
574
+ temperature: event.temperature,
575
+ topP: event.topP,
576
+ topK: event.topK,
577
+ presencePenalty: event.presencePenalty,
578
+ frequencyPenalty: event.frequencyPenalty,
579
+ seed: event.seed,
580
+ maxRetries: event.maxRetries
581
+ };
582
+ const providerName = mapProviderName(event.provider);
583
+ const operationName = mapOperationName(event.operationId);
584
+ const attributes = selectAttributes(telemetry, {
585
+ "gen_ai.operation.name": operationName,
586
+ "gen_ai.provider.name": providerName,
587
+ "gen_ai.request.model": event.modelId,
588
+ "gen_ai.agent.name": telemetry.functionId,
589
+ "gen_ai.output.type": "json",
590
+ "gen_ai.request.frequency_penalty": event.frequencyPenalty,
591
+ "gen_ai.request.max_tokens": event.maxOutputTokens,
592
+ "gen_ai.request.presence_penalty": event.presencePenalty,
593
+ "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
594
+ "gen_ai.request.top_k": event.topK,
595
+ "gen_ai.request.top_p": event.topP,
596
+ "gen_ai.request.seed": event.seed,
597
+ "gen_ai.system_instructions": event.system ? {
598
+ input: () => JSON.stringify(formatSystemInstructions(event.system))
599
+ } : void 0,
600
+ "gen_ai.input.messages": {
601
+ input: () => JSON.stringify(
602
+ formatModelMessages({
603
+ prompt: event.prompt,
604
+ messages: event.messages
605
+ })
606
+ )
607
+ }
608
+ });
609
+ const spanName = `${operationName} ${event.modelId}`;
610
+ const rootSpan = this.tracer.startSpan(spanName, {
611
+ attributes,
612
+ kind: SpanKind.INTERNAL
613
+ });
614
+ const rootContext = trace.setSpan(context.active(), rootSpan);
615
+ this.callStates.set(event.callId, {
616
+ operationId: event.operationId,
617
+ telemetry,
618
+ rootSpan,
619
+ rootContext,
620
+ stepSpan: void 0,
621
+ stepContext: void 0,
622
+ embedSpans: /* @__PURE__ */ new Map(),
623
+ rerankSpan: void 0,
624
+ toolSpans: /* @__PURE__ */ new Map(),
625
+ settings,
626
+ provider: event.provider,
627
+ modelId: event.modelId
628
+ });
629
+ }
630
+ /** @deprecated */
631
+ onObjectStepStart(event) {
632
+ var _a;
633
+ const state = this.getCallState(event.callId);
634
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
635
+ const { telemetry } = state;
636
+ const providerName = mapProviderName(event.provider);
637
+ const attributes = selectAttributes(telemetry, {
638
+ "gen_ai.operation.name": "chat",
639
+ "gen_ai.provider.name": providerName,
640
+ "gen_ai.request.model": event.modelId,
641
+ "gen_ai.output.type": "json",
642
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
643
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
644
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
645
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
646
+ "gen_ai.request.top_k": state.settings.topK,
647
+ "gen_ai.request.top_p": state.settings.topP,
648
+ "gen_ai.input.messages": {
649
+ input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
650
+ }
651
+ });
652
+ const spanName = `chat ${event.modelId}`;
653
+ state.stepSpan = this.tracer.startSpan(
654
+ spanName,
655
+ { attributes, kind: SpanKind.CLIENT },
656
+ state.rootContext
657
+ );
658
+ state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
659
+ }
660
+ /** @deprecated */
661
+ onObjectStepFinish(event) {
662
+ const state = this.getCallState(event.callId);
663
+ if (!(state == null ? void 0 : state.stepSpan)) return;
664
+ const { telemetry } = state;
665
+ state.stepSpan.setAttributes(
666
+ selectAttributes(telemetry, {
667
+ "gen_ai.response.finish_reasons": [event.finishReason],
668
+ "gen_ai.response.id": event.response.id,
669
+ "gen_ai.response.model": event.response.modelId,
670
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
671
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
672
+ "gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
673
+ "gen_ai.output.messages": {
674
+ output: () => {
675
+ try {
676
+ return JSON.stringify(
677
+ formatObjectOutputMessages({
678
+ objectText: event.objectText,
679
+ finishReason: event.finishReason
680
+ })
681
+ );
682
+ } catch (e) {
683
+ return event.objectText;
684
+ }
685
+ }
686
+ }
687
+ })
688
+ );
689
+ state.stepSpan.end();
690
+ state.stepSpan = void 0;
691
+ state.stepContext = void 0;
692
+ }
693
+ onEmbedOperationStart(event) {
694
+ const telemetry = {
695
+ isEnabled: event.isEnabled,
696
+ recordInputs: event.recordInputs,
697
+ recordOutputs: event.recordOutputs,
698
+ functionId: event.functionId,
699
+ metadata: event.metadata
700
+ };
701
+ const settings = {
702
+ maxRetries: event.maxRetries
703
+ };
704
+ const providerName = mapProviderName(event.provider);
705
+ const attributes = selectAttributes(telemetry, {
706
+ "gen_ai.operation.name": "embeddings",
707
+ "gen_ai.provider.name": providerName,
708
+ "gen_ai.request.model": event.modelId
709
+ });
710
+ const spanName = `embeddings ${event.modelId}`;
711
+ const rootSpan = this.tracer.startSpan(spanName, {
712
+ attributes,
713
+ kind: SpanKind.CLIENT
714
+ });
715
+ const rootContext = trace.setSpan(context.active(), rootSpan);
716
+ this.callStates.set(event.callId, {
717
+ operationId: event.operationId,
718
+ telemetry,
719
+ rootSpan,
720
+ rootContext,
721
+ stepSpan: void 0,
722
+ stepContext: void 0,
723
+ embedSpans: /* @__PURE__ */ new Map(),
724
+ rerankSpan: void 0,
725
+ toolSpans: /* @__PURE__ */ new Map(),
726
+ settings,
727
+ provider: event.provider,
728
+ modelId: event.modelId
729
+ });
730
+ }
731
+ onStepStart(event) {
732
+ var _a;
733
+ const state = this.getCallState(event.callId);
734
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
735
+ const { telemetry } = state;
736
+ const providerName = mapProviderName(event.provider);
737
+ const attributes = selectAttributes(telemetry, {
738
+ "gen_ai.operation.name": "chat",
739
+ "gen_ai.provider.name": providerName,
740
+ "gen_ai.request.model": event.modelId,
741
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
742
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
743
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
744
+ "gen_ai.request.stop_sequences": state.settings.stopSequences,
745
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
746
+ "gen_ai.request.top_k": state.settings.topK,
747
+ "gen_ai.request.top_p": state.settings.topP,
748
+ "gen_ai.input.messages": {
749
+ input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
750
+ },
751
+ "gen_ai.tool.definitions": {
752
+ input: () => event.stepTools ? JSON.stringify(event.stepTools) : void 0
753
+ }
754
+ });
755
+ const spanName = `chat ${event.modelId}`;
756
+ state.stepSpan = this.tracer.startSpan(
757
+ spanName,
758
+ { attributes, kind: SpanKind.CLIENT },
759
+ state.rootContext
760
+ );
761
+ state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
762
+ }
763
+ onToolCallStart(event) {
764
+ const state = this.getCallState(event.callId);
765
+ if (!(state == null ? void 0 : state.stepContext)) return;
766
+ const { telemetry } = state;
767
+ const { toolCall } = event;
768
+ const attributes = selectAttributes(telemetry, {
769
+ "gen_ai.operation.name": "execute_tool",
770
+ "gen_ai.tool.name": toolCall.toolName,
771
+ "gen_ai.tool.call.id": toolCall.toolCallId,
772
+ "gen_ai.tool.type": "function",
773
+ "gen_ai.tool.call.arguments": {
774
+ input: () => JSON.stringify(toolCall.input)
775
+ }
776
+ });
777
+ const spanName = `execute_tool ${toolCall.toolName}`;
778
+ const toolSpan = this.tracer.startSpan(
779
+ spanName,
780
+ { attributes, kind: SpanKind.INTERNAL },
781
+ state.stepContext
782
+ );
783
+ const toolContext = trace.setSpan(state.stepContext, toolSpan);
784
+ state.toolSpans.set(toolCall.toolCallId, {
785
+ span: toolSpan,
786
+ context: toolContext
787
+ });
788
+ }
789
+ onToolCallFinish(event) {
790
+ const state = this.getCallState(event.callId);
791
+ if (!state) return;
792
+ const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
793
+ if (!toolSpanEntry) return;
794
+ const { span } = toolSpanEntry;
795
+ const { telemetry } = state;
796
+ if (event.success) {
797
+ try {
798
+ span.setAttributes(
799
+ selectAttributes(telemetry, {
800
+ "gen_ai.tool.call.result": {
801
+ output: () => JSON.stringify(event.output)
802
+ }
803
+ })
804
+ );
805
+ } catch (e) {
806
+ }
807
+ } else {
808
+ recordSpanError(span, event.error);
809
+ }
810
+ span.end();
811
+ state.toolSpans.delete(event.toolCall.toolCallId);
812
+ }
813
+ onStepFinish(event) {
814
+ var _a, _b, _c;
815
+ const state = this.getCallState(event.callId);
816
+ if (!(state == null ? void 0 : state.stepSpan)) return;
817
+ const { telemetry } = state;
818
+ state.stepSpan.setAttributes(
819
+ selectAttributes(telemetry, {
820
+ "gen_ai.response.finish_reasons": [event.finishReason],
821
+ "gen_ai.response.id": event.response.id,
822
+ "gen_ai.response.model": event.response.modelId,
823
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
824
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
825
+ "gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.usage.cachedInputTokens,
826
+ "gen_ai.usage.cache_creation.input_tokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
827
+ "gen_ai.output.messages": {
828
+ output: () => {
829
+ var _a2;
830
+ return JSON.stringify(
831
+ formatOutputMessages({
832
+ text: (_a2 = event.text) != null ? _a2 : void 0,
833
+ reasoning: event.reasoning,
834
+ toolCalls: event.toolCalls,
835
+ files: event.files,
836
+ finishReason: event.finishReason
837
+ })
838
+ );
839
+ }
840
+ }
841
+ })
842
+ );
843
+ state.stepSpan.end();
844
+ state.stepSpan = void 0;
845
+ state.stepContext = void 0;
846
+ }
847
+ onFinish(event) {
848
+ const state = this.getCallState(event.callId);
849
+ if (!(state == null ? void 0 : state.rootSpan)) return;
850
+ if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
851
+ this.onEmbedOperationFinish(event);
852
+ return;
853
+ }
854
+ if (state.operationId === "ai.rerank") {
855
+ this.onRerankOperationFinish(event);
856
+ return;
857
+ }
858
+ if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
859
+ this.onObjectOperationFinish(event);
860
+ return;
861
+ }
862
+ this.onGenerateFinish(event);
863
+ }
864
+ onGenerateFinish(event) {
865
+ var _a, _b, _c;
866
+ const state = this.getCallState(event.callId);
867
+ if (!(state == null ? void 0 : state.rootSpan)) return;
868
+ const { telemetry } = state;
869
+ state.rootSpan.setAttributes(
870
+ selectAttributes(telemetry, {
871
+ "gen_ai.response.finish_reasons": [event.finishReason],
872
+ "gen_ai.usage.input_tokens": event.totalUsage.inputTokens,
873
+ "gen_ai.usage.output_tokens": event.totalUsage.outputTokens,
874
+ "gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.totalUsage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.totalUsage.cachedInputTokens,
875
+ "gen_ai.usage.cache_creation.input_tokens": (_c = event.totalUsage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
876
+ "gen_ai.output.messages": {
877
+ output: () => {
878
+ var _a2;
879
+ return JSON.stringify(
880
+ formatOutputMessages({
881
+ text: (_a2 = event.text) != null ? _a2 : void 0,
882
+ reasoning: event.reasoning,
883
+ toolCalls: event.toolCalls,
884
+ files: event.files,
885
+ finishReason: event.finishReason
886
+ })
887
+ );
888
+ }
889
+ }
890
+ })
891
+ );
892
+ state.rootSpan.end();
893
+ this.cleanupCallState(event.callId);
894
+ }
895
+ onObjectOperationFinish(event) {
896
+ const state = this.getCallState(event.callId);
897
+ if (!(state == null ? void 0 : state.rootSpan)) return;
898
+ const { telemetry } = state;
899
+ state.rootSpan.setAttributes(
900
+ selectAttributes(telemetry, {
901
+ "gen_ai.response.finish_reasons": [event.finishReason],
902
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
903
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
904
+ "gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
905
+ "gen_ai.output.messages": {
906
+ output: () => event.object != null ? JSON.stringify(
907
+ formatObjectOutputMessages({
908
+ objectText: JSON.stringify(event.object),
909
+ finishReason: event.finishReason
910
+ })
911
+ ) : void 0
912
+ }
913
+ })
914
+ );
915
+ state.rootSpan.end();
916
+ this.cleanupCallState(event.callId);
917
+ }
918
+ onEmbedOperationFinish(event) {
919
+ const state = this.getCallState(event.callId);
920
+ if (!(state == null ? void 0 : state.rootSpan)) return;
921
+ const { telemetry } = state;
922
+ state.rootSpan.setAttributes(
923
+ selectAttributes(telemetry, {
924
+ "gen_ai.usage.input_tokens": event.usage.tokens
925
+ })
926
+ );
927
+ state.rootSpan.end();
928
+ this.cleanupCallState(event.callId);
929
+ }
930
+ onEmbedStart(event) {
931
+ const state = this.getCallState(event.callId);
932
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
933
+ const { telemetry } = state;
934
+ const providerName = mapProviderName(state.provider);
935
+ const attributes = selectAttributes(telemetry, {
936
+ "gen_ai.operation.name": "embeddings",
937
+ "gen_ai.provider.name": providerName,
938
+ "gen_ai.request.model": state.modelId
939
+ });
940
+ const spanName = `embeddings ${state.modelId}`;
941
+ const embedSpan = this.tracer.startSpan(
942
+ spanName,
943
+ { attributes, kind: SpanKind.CLIENT },
944
+ state.rootContext
945
+ );
946
+ const embedContext = trace.setSpan(state.rootContext, embedSpan);
947
+ state.embedSpans.set(event.embedCallId, {
948
+ span: embedSpan,
949
+ context: embedContext
950
+ });
951
+ }
952
+ onEmbedFinish(event) {
953
+ const state = this.getCallState(event.callId);
954
+ if (!state) return;
955
+ const embedSpanEntry = state.embedSpans.get(event.embedCallId);
956
+ if (!embedSpanEntry) return;
957
+ const { span } = embedSpanEntry;
958
+ const { telemetry } = state;
959
+ span.setAttributes(
960
+ selectAttributes(telemetry, {
961
+ "gen_ai.usage.input_tokens": event.usage.tokens
962
+ })
963
+ );
964
+ span.end();
965
+ state.embedSpans.delete(event.embedCallId);
966
+ }
967
+ onRerankOperationStart(event) {
968
+ const telemetry = {
969
+ isEnabled: event.isEnabled,
970
+ recordInputs: event.recordInputs,
971
+ recordOutputs: event.recordOutputs,
972
+ functionId: event.functionId,
973
+ metadata: event.metadata
974
+ };
975
+ const settings = {
976
+ maxRetries: event.maxRetries
977
+ };
978
+ const providerName = mapProviderName(event.provider);
979
+ const attributes = selectAttributes(telemetry, {
980
+ "gen_ai.operation.name": "rerank",
981
+ "gen_ai.provider.name": providerName,
982
+ "gen_ai.request.model": event.modelId
983
+ });
984
+ const spanName = `rerank ${event.modelId}`;
985
+ const rootSpan = this.tracer.startSpan(spanName, {
986
+ attributes,
987
+ kind: SpanKind.CLIENT
988
+ });
989
+ const rootContext = trace.setSpan(context.active(), rootSpan);
990
+ this.callStates.set(event.callId, {
991
+ operationId: event.operationId,
992
+ telemetry,
993
+ rootSpan,
994
+ rootContext,
995
+ stepSpan: void 0,
996
+ stepContext: void 0,
997
+ embedSpans: /* @__PURE__ */ new Map(),
998
+ rerankSpan: void 0,
999
+ toolSpans: /* @__PURE__ */ new Map(),
1000
+ settings,
1001
+ provider: event.provider,
1002
+ modelId: event.modelId
1003
+ });
1004
+ }
1005
+ onRerankOperationFinish(event) {
1006
+ const state = this.getCallState(event.callId);
1007
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1008
+ state.rootSpan.end();
1009
+ this.cleanupCallState(event.callId);
1010
+ }
1011
+ onRerankStart(event) {
1012
+ const state = this.getCallState(event.callId);
1013
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1014
+ const { telemetry } = state;
1015
+ const providerName = mapProviderName(state.provider);
1016
+ const attributes = selectAttributes(telemetry, {
1017
+ "gen_ai.operation.name": "rerank",
1018
+ "gen_ai.provider.name": providerName,
1019
+ "gen_ai.request.model": state.modelId
1020
+ });
1021
+ const spanName = `rerank ${state.modelId}`;
1022
+ const rerankSpan = this.tracer.startSpan(
1023
+ spanName,
1024
+ { attributes, kind: SpanKind.CLIENT },
1025
+ state.rootContext
1026
+ );
1027
+ const rerankContext = trace.setSpan(state.rootContext, rerankSpan);
1028
+ state.rerankSpan = { span: rerankSpan, context: rerankContext };
1029
+ }
1030
+ onRerankFinish(event) {
1031
+ const state = this.getCallState(event.callId);
1032
+ if (!(state == null ? void 0 : state.rerankSpan)) return;
1033
+ const { span } = state.rerankSpan;
1034
+ span.end();
1035
+ state.rerankSpan = void 0;
1036
+ }
1037
+ onChunk(_event) {
1038
+ }
1039
+ onError(error) {
1040
+ var _a;
1041
+ const event = error;
1042
+ if (!(event == null ? void 0 : event.callId)) return;
1043
+ const state = this.getCallState(event.callId);
1044
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1045
+ const actualError = (_a = event.error) != null ? _a : error;
1046
+ if (state.stepSpan) {
1047
+ recordSpanError(state.stepSpan, actualError);
1048
+ state.stepSpan.end();
1049
+ }
1050
+ for (const { span: embedSpan } of state.embedSpans.values()) {
1051
+ recordSpanError(embedSpan, actualError);
1052
+ embedSpan.end();
1053
+ }
1054
+ state.embedSpans.clear();
1055
+ if (state.rerankSpan) {
1056
+ recordSpanError(state.rerankSpan.span, actualError);
1057
+ state.rerankSpan.span.end();
1058
+ state.rerankSpan = void 0;
1059
+ }
1060
+ recordSpanError(state.rootSpan, actualError);
1061
+ state.rootSpan.end();
1062
+ this.cleanupCallState(event.callId);
1063
+ }
1064
+ };
1065
+
1
1066
  // src/open-telemetry-integration.ts
2
1067
  import {
3
- context,
4
- SpanStatusCode,
5
- trace
1068
+ context as context2,
1069
+ SpanStatusCode as SpanStatusCode2,
1070
+ trace as trace2
6
1071
  } from "@opentelemetry/api";
7
1072
 
8
1073
  // src/assemble-operation-name.ts
@@ -57,7 +1122,7 @@ function getBaseTelemetryAttributes({
57
1122
  }
58
1123
 
59
1124
  // src/stringify-for-telemetry.ts
60
- import { convertDataContentToBase64String } from "ai";
1125
+ import { convertDataContentToBase64String as convertDataContentToBase64String2 } from "ai";
61
1126
  function stringifyForTelemetry(prompt) {
62
1127
  return JSON.stringify(
63
1128
  prompt.map((message) => ({
@@ -65,7 +1130,7 @@ function stringifyForTelemetry(prompt) {
65
1130
  content: typeof message.content === "string" ? message.content : message.content.map(
66
1131
  (part) => part.type === "file" ? {
67
1132
  ...part,
68
- data: part.data instanceof Uint8Array ? convertDataContentToBase64String(part.data) : part.data
1133
+ data: part.data instanceof Uint8Array ? convertDataContentToBase64String2(part.data) : part.data
69
1134
  } : part
70
1135
  )
71
1136
  }))
@@ -73,7 +1138,7 @@ function stringifyForTelemetry(prompt) {
73
1138
  }
74
1139
 
75
1140
  // src/open-telemetry-integration.ts
76
- function recordSpanError(span, error) {
1141
+ function recordSpanError2(span, error) {
77
1142
  if (error instanceof Error) {
78
1143
  span.recordException({
79
1144
  name: error.name,
@@ -81,18 +1146,18 @@ function recordSpanError(span, error) {
81
1146
  stack: error.stack
82
1147
  });
83
1148
  span.setStatus({
84
- code: SpanStatusCode.ERROR,
1149
+ code: SpanStatusCode2.ERROR,
85
1150
  message: error.message
86
1151
  });
87
1152
  } else {
88
- span.setStatus({ code: SpanStatusCode.ERROR });
1153
+ span.setStatus({ code: SpanStatusCode2.ERROR });
89
1154
  }
90
1155
  }
91
- function shouldRecord(telemetry) {
1156
+ function shouldRecord2(telemetry) {
92
1157
  return (telemetry == null ? void 0 : telemetry.isEnabled) === true;
93
1158
  }
94
- function selectAttributes(telemetry, attributes) {
95
- if (!shouldRecord(telemetry)) {
1159
+ function selectAttributes2(telemetry, attributes) {
1160
+ if (!shouldRecord2(telemetry)) {
96
1161
  return {};
97
1162
  }
98
1163
  const result = {};
@@ -118,7 +1183,7 @@ var OpenTelemetryIntegration = class {
118
1183
  constructor(options = {}) {
119
1184
  this.callStates = /* @__PURE__ */ new Map();
120
1185
  var _a;
121
- this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("ai");
1186
+ this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
122
1187
  }
123
1188
  getCallState(callId) {
124
1189
  return this.callStates.get(callId);
@@ -136,7 +1201,7 @@ var OpenTelemetryIntegration = class {
136
1201
  if (toolSpanEntry == null) {
137
1202
  return execute();
138
1203
  }
139
- return context.with(toolSpanEntry.context, execute);
1204
+ return context2.with(toolSpanEntry.context, execute);
140
1205
  }
141
1206
  onStart(event) {
142
1207
  if (event.isEnabled !== true) return;
@@ -179,7 +1244,7 @@ var OpenTelemetryIntegration = class {
179
1244
  headers: event.headers,
180
1245
  settings
181
1246
  });
182
- const attributes = selectAttributes(telemetry, {
1247
+ const attributes = selectAttributes2(telemetry, {
183
1248
  ...assembleOperationName({
184
1249
  operationId: event.operationId,
185
1250
  telemetry
@@ -196,7 +1261,7 @@ var OpenTelemetryIntegration = class {
196
1261
  }
197
1262
  });
198
1263
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
199
- const rootContext = trace.setSpan(context.active(), rootSpan);
1264
+ const rootContext = trace2.setSpan(context2.active(), rootSpan);
200
1265
  this.callStates.set(event.callId, {
201
1266
  operationId: event.operationId,
202
1267
  telemetry,
@@ -235,7 +1300,7 @@ var OpenTelemetryIntegration = class {
235
1300
  headers: event.headers,
236
1301
  settings
237
1302
  });
238
- const attributes = selectAttributes(telemetry, {
1303
+ const attributes = selectAttributes2(telemetry, {
239
1304
  ...assembleOperationName({
240
1305
  operationId: event.operationId,
241
1306
  telemetry
@@ -254,7 +1319,7 @@ var OpenTelemetryIntegration = class {
254
1319
  "ai.settings.output": event.output
255
1320
  });
256
1321
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
257
- const rootContext = trace.setSpan(context.active(), rootSpan);
1322
+ const rootContext = trace2.setSpan(context2.active(), rootSpan);
258
1323
  this.callStates.set(event.callId, {
259
1324
  operationId: event.operationId,
260
1325
  telemetry,
@@ -276,7 +1341,7 @@ var OpenTelemetryIntegration = class {
276
1341
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
277
1342
  const { telemetry } = state;
278
1343
  const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
279
- const attributes = selectAttributes(telemetry, {
1344
+ const attributes = selectAttributes2(telemetry, {
280
1345
  ...assembleOperationName({
281
1346
  operationId: stepOperationId,
282
1347
  telemetry
@@ -299,7 +1364,7 @@ var OpenTelemetryIntegration = class {
299
1364
  { attributes },
300
1365
  state.rootContext
301
1366
  );
302
- state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
1367
+ state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
303
1368
  }
304
1369
  /** @deprecated */
305
1370
  onObjectStepFinish(event) {
@@ -307,7 +1372,7 @@ var OpenTelemetryIntegration = class {
307
1372
  if (!(state == null ? void 0 : state.stepSpan)) return;
308
1373
  const { telemetry } = state;
309
1374
  state.stepSpan.setAttributes(
310
- selectAttributes(telemetry, {
1375
+ selectAttributes2(telemetry, {
311
1376
  "ai.response.finishReason": event.finishReason,
312
1377
  "ai.response.object": {
313
1378
  output: () => {
@@ -365,7 +1430,7 @@ var OpenTelemetryIntegration = class {
365
1430
  });
366
1431
  const value = event.value;
367
1432
  const isMany = event.operationId === "ai.embedMany";
368
- const attributes = selectAttributes(telemetry, {
1433
+ const attributes = selectAttributes2(telemetry, {
369
1434
  ...assembleOperationName({
370
1435
  operationId: event.operationId,
371
1436
  telemetry
@@ -382,7 +1447,7 @@ var OpenTelemetryIntegration = class {
382
1447
  }
383
1448
  });
384
1449
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
385
- const rootContext = trace.setSpan(context.active(), rootSpan);
1450
+ const rootContext = trace2.setSpan(context2.active(), rootSpan);
386
1451
  this.callStates.set(event.callId, {
387
1452
  operationId: event.operationId,
388
1453
  telemetry,
@@ -403,7 +1468,7 @@ var OpenTelemetryIntegration = class {
403
1468
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
404
1469
  const { telemetry } = state;
405
1470
  const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
406
- const attributes = selectAttributes(telemetry, {
1471
+ const attributes = selectAttributes2(telemetry, {
407
1472
  ...assembleOperationName({
408
1473
  operationId: stepOperationId,
409
1474
  telemetry
@@ -438,14 +1503,14 @@ var OpenTelemetryIntegration = class {
438
1503
  { attributes },
439
1504
  state.rootContext
440
1505
  );
441
- state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
1506
+ state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
442
1507
  }
443
1508
  onToolCallStart(event) {
444
1509
  const state = this.getCallState(event.callId);
445
1510
  if (!(state == null ? void 0 : state.stepContext)) return;
446
1511
  const { telemetry } = state;
447
1512
  const { toolCall } = event;
448
- const attributes = selectAttributes(telemetry, {
1513
+ const attributes = selectAttributes2(telemetry, {
449
1514
  ...assembleOperationName({
450
1515
  operationId: "ai.toolCall",
451
1516
  telemetry
@@ -461,7 +1526,7 @@ var OpenTelemetryIntegration = class {
461
1526
  { attributes },
462
1527
  state.stepContext
463
1528
  );
464
- const toolContext = trace.setSpan(state.stepContext, toolSpan);
1529
+ const toolContext = trace2.setSpan(state.stepContext, toolSpan);
465
1530
  state.toolSpans.set(toolCall.toolCallId, {
466
1531
  span: toolSpan,
467
1532
  context: toolContext
@@ -477,16 +1542,16 @@ var OpenTelemetryIntegration = class {
477
1542
  if (event.success) {
478
1543
  try {
479
1544
  span.setAttributes(
480
- selectAttributes(telemetry, {
1545
+ selectAttributes2(telemetry, {
481
1546
  "ai.toolCall.result": {
482
1547
  output: () => JSON.stringify(event.output)
483
1548
  }
484
1549
  })
485
1550
  );
486
- } catch (_ignored) {
1551
+ } catch (e) {
487
1552
  }
488
1553
  } else {
489
- recordSpanError(span, event.error);
1554
+ recordSpanError2(span, event.error);
490
1555
  }
491
1556
  span.end();
492
1557
  state.toolSpans.delete(event.toolCall.toolCallId);
@@ -497,7 +1562,7 @@ var OpenTelemetryIntegration = class {
497
1562
  if (!(state == null ? void 0 : state.stepSpan)) return;
498
1563
  const { telemetry } = state;
499
1564
  state.stepSpan.setAttributes(
500
- selectAttributes(telemetry, {
1565
+ selectAttributes2(telemetry, {
501
1566
  "ai.response.finishReason": event.finishReason,
502
1567
  "ai.response.text": {
503
1568
  output: () => {
@@ -574,7 +1639,7 @@ var OpenTelemetryIntegration = class {
574
1639
  if (!(state == null ? void 0 : state.rootSpan)) return;
575
1640
  const { telemetry } = state;
576
1641
  state.rootSpan.setAttributes(
577
- selectAttributes(telemetry, {
1642
+ selectAttributes2(telemetry, {
578
1643
  "ai.response.finishReason": event.finishReason,
579
1644
  "ai.response.text": {
580
1645
  output: () => {
@@ -624,7 +1689,7 @@ var OpenTelemetryIntegration = class {
624
1689
  if (!(state == null ? void 0 : state.rootSpan)) return;
625
1690
  const { telemetry } = state;
626
1691
  state.rootSpan.setAttributes(
627
- selectAttributes(telemetry, {
1692
+ selectAttributes2(telemetry, {
628
1693
  "ai.response.finishReason": event.finishReason,
629
1694
  "ai.response.object": {
630
1695
  output: () => event.object != null ? JSON.stringify(event.object) : void 0
@@ -646,7 +1711,7 @@ var OpenTelemetryIntegration = class {
646
1711
  const { telemetry } = state;
647
1712
  const isMany = state.operationId === "ai.embedMany";
648
1713
  state.rootSpan.setAttributes(
649
- selectAttributes(telemetry, {
1714
+ selectAttributes2(telemetry, {
650
1715
  ...isMany ? {
651
1716
  "ai.embeddings": {
652
1717
  output: () => event.embedding.map((e) => JSON.stringify(e))
@@ -666,7 +1731,7 @@ var OpenTelemetryIntegration = class {
666
1731
  const state = this.getCallState(event.callId);
667
1732
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
668
1733
  const { telemetry } = state;
669
- const attributes = selectAttributes(telemetry, {
1734
+ const attributes = selectAttributes2(telemetry, {
670
1735
  ...assembleOperationName({
671
1736
  operationId: event.operationId,
672
1737
  telemetry
@@ -681,7 +1746,7 @@ var OpenTelemetryIntegration = class {
681
1746
  { attributes },
682
1747
  state.rootContext
683
1748
  );
684
- const embedContext = trace.setSpan(state.rootContext, embedSpan);
1749
+ const embedContext = trace2.setSpan(state.rootContext, embedSpan);
685
1750
  state.embedSpans.set(event.embedCallId, {
686
1751
  span: embedSpan,
687
1752
  context: embedContext
@@ -695,7 +1760,7 @@ var OpenTelemetryIntegration = class {
695
1760
  const { span } = embedSpanEntry;
696
1761
  const { telemetry } = state;
697
1762
  span.setAttributes(
698
- selectAttributes(telemetry, {
1763
+ selectAttributes2(telemetry, {
699
1764
  "ai.embeddings": {
700
1765
  output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
701
1766
  },
@@ -722,7 +1787,7 @@ var OpenTelemetryIntegration = class {
722
1787
  headers: event.headers,
723
1788
  settings
724
1789
  });
725
- const attributes = selectAttributes(telemetry, {
1790
+ const attributes = selectAttributes2(telemetry, {
726
1791
  ...assembleOperationName({
727
1792
  operationId: event.operationId,
728
1793
  telemetry
@@ -733,7 +1798,7 @@ var OpenTelemetryIntegration = class {
733
1798
  }
734
1799
  });
735
1800
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
736
- const rootContext = trace.setSpan(context.active(), rootSpan);
1801
+ const rootContext = trace2.setSpan(context2.active(), rootSpan);
737
1802
  this.callStates.set(event.callId, {
738
1803
  operationId: event.operationId,
739
1804
  telemetry,
@@ -758,7 +1823,7 @@ var OpenTelemetryIntegration = class {
758
1823
  const state = this.getCallState(event.callId);
759
1824
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
760
1825
  const { telemetry } = state;
761
- const attributes = selectAttributes(telemetry, {
1826
+ const attributes = selectAttributes2(telemetry, {
762
1827
  ...assembleOperationName({
763
1828
  operationId: event.operationId,
764
1829
  telemetry
@@ -773,7 +1838,7 @@ var OpenTelemetryIntegration = class {
773
1838
  { attributes },
774
1839
  state.rootContext
775
1840
  );
776
- const rerankContext = trace.setSpan(state.rootContext, rerankSpan);
1841
+ const rerankContext = trace2.setSpan(state.rootContext, rerankSpan);
777
1842
  state.rerankSpan = { span: rerankSpan, context: rerankContext };
778
1843
  }
779
1844
  onRerankFinish(event) {
@@ -782,7 +1847,7 @@ var OpenTelemetryIntegration = class {
782
1847
  const { span } = state.rerankSpan;
783
1848
  const { telemetry } = state;
784
1849
  span.setAttributes(
785
- selectAttributes(telemetry, {
1850
+ selectAttributes2(telemetry, {
786
1851
  "ai.ranking.type": event.documentsType,
787
1852
  "ai.ranking": {
788
1853
  output: () => event.ranking.map((r) => JSON.stringify(r))
@@ -821,25 +1886,26 @@ var OpenTelemetryIntegration = class {
821
1886
  if (!(state == null ? void 0 : state.rootSpan)) return;
822
1887
  const actualError = (_a = event.error) != null ? _a : error;
823
1888
  if (state.stepSpan) {
824
- recordSpanError(state.stepSpan, actualError);
1889
+ recordSpanError2(state.stepSpan, actualError);
825
1890
  state.stepSpan.end();
826
1891
  }
827
1892
  for (const { span: embedSpan } of state.embedSpans.values()) {
828
- recordSpanError(embedSpan, actualError);
1893
+ recordSpanError2(embedSpan, actualError);
829
1894
  embedSpan.end();
830
1895
  }
831
1896
  state.embedSpans.clear();
832
1897
  if (state.rerankSpan) {
833
- recordSpanError(state.rerankSpan.span, actualError);
1898
+ recordSpanError2(state.rerankSpan.span, actualError);
834
1899
  state.rerankSpan.span.end();
835
1900
  state.rerankSpan = void 0;
836
1901
  }
837
- recordSpanError(state.rootSpan, actualError);
1902
+ recordSpanError2(state.rootSpan, actualError);
838
1903
  state.rootSpan.end();
839
1904
  this.cleanupCallState(event.callId);
840
1905
  }
841
1906
  };
842
1907
  export {
1908
+ GenAIOpenTelemetryIntegration,
843
1909
  OpenTelemetryIntegration
844
1910
  };
845
1911
  //# sourceMappingURL=index.mjs.map