@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.js CHANGED
@@ -20,12 +20,1073 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ GenAIOpenTelemetryIntegration: () => GenAIOpenTelemetryIntegration,
23
24
  OpenTelemetryIntegration: () => OpenTelemetryIntegration
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
28
+ // src/gen-ai-open-telemetry-integration.ts
29
+ var import_api = require("@opentelemetry/api");
30
+
31
+ // src/gen-ai-format-messages.ts
32
+ var import_ai = require("ai");
33
+ function mapProviderName(provider) {
34
+ const lower = provider.toLowerCase();
35
+ const wellKnownPrefixes = [
36
+ ["google.vertex", "gcp.vertex_ai"],
37
+ ["google.generative-ai", "gcp.gemini"],
38
+ ["google-vertex", "gcp.vertex_ai"],
39
+ ["amazon-bedrock", "aws.bedrock"],
40
+ ["azure-openai", "azure.ai.openai"],
41
+ ["anthropic", "anthropic"],
42
+ ["openai", "openai"],
43
+ ["azure", "azure.ai.inference"],
44
+ ["google", "gcp.gemini"],
45
+ ["mistral", "mistral_ai"],
46
+ ["cohere", "cohere"],
47
+ ["bedrock", "aws.bedrock"],
48
+ ["groq", "groq"],
49
+ ["deepseek", "deepseek"],
50
+ ["perplexity", "perplexity"],
51
+ ["xai", "x_ai"]
52
+ ];
53
+ for (const [prefix, mapped] of wellKnownPrefixes) {
54
+ if (lower === prefix || lower.startsWith(prefix + ".") || lower.startsWith(prefix + "-")) {
55
+ return mapped;
56
+ }
57
+ }
58
+ return provider;
59
+ }
60
+ function mapOperationName(operationId) {
61
+ var _a;
62
+ const mapping = {
63
+ "ai.generateText": "invoke_agent",
64
+ "ai.streamText": "invoke_agent",
65
+ "ai.generateObject": "invoke_agent",
66
+ "ai.streamObject": "invoke_agent",
67
+ "ai.embed": "embeddings",
68
+ "ai.embedMany": "embeddings",
69
+ "ai.rerank": "rerank"
70
+ };
71
+ return (_a = mapping[operationId]) != null ? _a : operationId;
72
+ }
73
+ function formatSystemInstructions(system) {
74
+ if (typeof system === "string") {
75
+ return [{ type: "text", content: system }];
76
+ }
77
+ if (Array.isArray(system)) {
78
+ return system.map((msg) => ({ type: "text", content: msg.content }));
79
+ }
80
+ return [{ type: "text", content: system.content }];
81
+ }
82
+ function convertMessagePartToSemConv(part) {
83
+ var _a, _b, _c, _d;
84
+ switch (part.type) {
85
+ case "text":
86
+ return { type: "text", content: part.text };
87
+ case "reasoning":
88
+ return { type: "reasoning", content: part.text };
89
+ case "tool-call":
90
+ return {
91
+ type: "tool_call",
92
+ id: (_a = part.toolCallId) != null ? _a : null,
93
+ name: part.toolName,
94
+ arguments: part.input
95
+ };
96
+ case "tool-result": {
97
+ const output = part.output;
98
+ let response;
99
+ if (output) {
100
+ if (output.type === "text" || output.type === "error-text") {
101
+ response = output.value;
102
+ } else if (output.type === "json" || output.type === "error-json") {
103
+ response = output.value;
104
+ } else if (output.type === "execution-denied") {
105
+ response = { denied: true, reason: output.reason };
106
+ } else {
107
+ response = output;
108
+ }
109
+ }
110
+ return {
111
+ type: "tool_call_response",
112
+ id: (_b = part.toolCallId) != null ? _b : null,
113
+ response
114
+ };
115
+ }
116
+ case "file": {
117
+ const data = part.data;
118
+ let content;
119
+ if (data instanceof Uint8Array) {
120
+ content = (0, import_ai.convertDataContentToBase64String)(data);
121
+ } else if (typeof data === "string") {
122
+ if (data.startsWith("http://") || data.startsWith("https://")) {
123
+ return {
124
+ type: "uri",
125
+ modality: getModality(part.mediaType),
126
+ mime_type: (_c = part.mediaType) != null ? _c : null,
127
+ uri: data
128
+ };
129
+ }
130
+ content = data;
131
+ } else {
132
+ content = String(data);
133
+ }
134
+ return {
135
+ type: "blob",
136
+ modality: getModality(part.mediaType),
137
+ mime_type: (_d = part.mediaType) != null ? _d : null,
138
+ content
139
+ };
140
+ }
141
+ case "tool-approval-response":
142
+ return {
143
+ type: "tool_approval_response",
144
+ approval_id: part.approvalId,
145
+ approved: part.approved,
146
+ reason: part.reason
147
+ };
148
+ case "custom":
149
+ return { type: "custom", kind: part.kind };
150
+ case "reasoning-file":
151
+ return { type: String(part.type) };
152
+ default: {
153
+ const _exhaustive = part;
154
+ return { type: String(_exhaustive.type) };
155
+ }
156
+ }
157
+ }
158
+ function getModality(mediaType) {
159
+ if (!mediaType) return "image";
160
+ if (mediaType.startsWith("image/")) return "image";
161
+ if (mediaType.startsWith("video/")) return "video";
162
+ if (mediaType.startsWith("audio/")) return "audio";
163
+ return "image";
164
+ }
165
+ function formatInputMessages(prompt) {
166
+ return prompt.filter((msg) => msg.role !== "system").map((message) => {
167
+ if (message.role === "system") {
168
+ return {
169
+ role: "system",
170
+ parts: [{ type: "text", content: message.content }]
171
+ };
172
+ }
173
+ const parts = message.content.map(convertMessagePartToSemConv);
174
+ return { role: message.role, parts };
175
+ });
176
+ }
177
+ function formatModelMessages({
178
+ prompt,
179
+ messages
180
+ }) {
181
+ const result = [];
182
+ if (typeof prompt === "string") {
183
+ result.push({
184
+ role: "user",
185
+ parts: [{ type: "text", content: prompt }]
186
+ });
187
+ } else if (Array.isArray(prompt)) {
188
+ for (const msg of prompt) {
189
+ const converted = convertModelMessageToSemConv(msg);
190
+ if (converted) result.push(converted);
191
+ }
192
+ }
193
+ if (messages) {
194
+ for (const msg of messages) {
195
+ const converted = convertModelMessageToSemConv(msg);
196
+ if (converted) result.push(converted);
197
+ }
198
+ }
199
+ return result;
200
+ }
201
+ function convertModelMessageToSemConv(msg) {
202
+ if (msg.role === "system") return void 0;
203
+ if (msg.role === "user") {
204
+ if (typeof msg.content === "string") {
205
+ return {
206
+ role: "user",
207
+ parts: [{ type: "text", content: msg.content }]
208
+ };
209
+ }
210
+ const parts = msg.content.map((part) => {
211
+ var _a, _b, _c, _d, _e, _f, _g, _h;
212
+ switch (part.type) {
213
+ case "text":
214
+ return { type: "text", content: part.text };
215
+ case "image": {
216
+ const data = part.image;
217
+ if (data instanceof URL) {
218
+ return {
219
+ type: "uri",
220
+ modality: "image",
221
+ mime_type: (_a = part.mediaType) != null ? _a : null,
222
+ uri: data.toString()
223
+ };
224
+ }
225
+ if (typeof data === "string") {
226
+ if (data.startsWith("http://") || data.startsWith("https://")) {
227
+ return {
228
+ type: "uri",
229
+ modality: "image",
230
+ mime_type: (_b = part.mediaType) != null ? _b : null,
231
+ uri: data
232
+ };
233
+ }
234
+ return {
235
+ type: "blob",
236
+ modality: "image",
237
+ mime_type: (_c = part.mediaType) != null ? _c : null,
238
+ content: data
239
+ };
240
+ }
241
+ return {
242
+ type: "blob",
243
+ modality: "image",
244
+ mime_type: (_d = part.mediaType) != null ? _d : null,
245
+ content: (0, import_ai.convertDataContentToBase64String)(data)
246
+ };
247
+ }
248
+ case "file": {
249
+ const data = part.data;
250
+ if (data instanceof URL) {
251
+ return {
252
+ type: "uri",
253
+ modality: getModality(part.mediaType),
254
+ mime_type: (_e = part.mediaType) != null ? _e : null,
255
+ uri: data.toString()
256
+ };
257
+ }
258
+ if (typeof data === "string") {
259
+ if (data.startsWith("http://") || data.startsWith("https://")) {
260
+ return {
261
+ type: "uri",
262
+ modality: getModality(part.mediaType),
263
+ mime_type: (_f = part.mediaType) != null ? _f : null,
264
+ uri: data
265
+ };
266
+ }
267
+ return {
268
+ type: "blob",
269
+ modality: getModality(part.mediaType),
270
+ mime_type: (_g = part.mediaType) != null ? _g : null,
271
+ content: data
272
+ };
273
+ }
274
+ return {
275
+ type: "blob",
276
+ modality: getModality(part.mediaType),
277
+ mime_type: (_h = part.mediaType) != null ? _h : null,
278
+ content: (0, import_ai.convertDataContentToBase64String)(data)
279
+ };
280
+ }
281
+ default:
282
+ return { type: String(part.type) };
283
+ }
284
+ });
285
+ return { role: "user", parts };
286
+ }
287
+ if (msg.role === "assistant") {
288
+ if (typeof msg.content === "string") {
289
+ return {
290
+ role: "assistant",
291
+ parts: [{ type: "text", content: msg.content }]
292
+ };
293
+ }
294
+ const parts = msg.content.map((part) => {
295
+ var _a, _b;
296
+ switch (part.type) {
297
+ case "text":
298
+ return { type: "text", content: part.text };
299
+ case "reasoning":
300
+ return { type: "reasoning", content: part.text };
301
+ case "tool-call":
302
+ return {
303
+ type: "tool_call",
304
+ id: (_a = part.toolCallId) != null ? _a : null,
305
+ name: part.toolName,
306
+ arguments: part.input
307
+ };
308
+ case "tool-result": {
309
+ const output = part.output;
310
+ let response;
311
+ if (output) {
312
+ if (output.type === "text" || output.type === "error-text") {
313
+ response = output.value;
314
+ } else if (output.type === "json" || output.type === "error-json") {
315
+ response = output.value;
316
+ } else if (output.type === "execution-denied") {
317
+ response = { denied: true, reason: output.reason };
318
+ } else {
319
+ response = output;
320
+ }
321
+ }
322
+ return {
323
+ type: "tool_call_response",
324
+ id: (_b = part.toolCallId) != null ? _b : null,
325
+ response
326
+ };
327
+ }
328
+ default:
329
+ return { type: String(part.type) };
330
+ }
331
+ });
332
+ return { role: "assistant", parts };
333
+ }
334
+ if (msg.role === "tool") {
335
+ const parts = msg.content.map((part) => {
336
+ var _a;
337
+ if (part.type === "tool-result") {
338
+ const output = part.output;
339
+ let response;
340
+ if (output) {
341
+ if (output.type === "text" || output.type === "error-text") {
342
+ response = output.value;
343
+ } else if (output.type === "json" || output.type === "error-json") {
344
+ response = output.value;
345
+ } else if (output.type === "execution-denied") {
346
+ response = { denied: true, reason: output.reason };
347
+ } else {
348
+ response = output;
349
+ }
350
+ }
351
+ return {
352
+ type: "tool_call_response",
353
+ id: (_a = part.toolCallId) != null ? _a : null,
354
+ response
355
+ };
356
+ }
357
+ return { type: String(part.type) };
358
+ });
359
+ return { role: "tool", parts };
360
+ }
361
+ return void 0;
362
+ }
363
+ function formatOutputMessages({
364
+ text,
365
+ reasoning,
366
+ toolCalls,
367
+ files,
368
+ finishReason
369
+ }) {
370
+ const parts = [];
371
+ if (reasoning) {
372
+ for (const r of reasoning) {
373
+ if ("text" in r && r.text) {
374
+ parts.push({ type: "reasoning", content: r.text });
375
+ }
376
+ }
377
+ }
378
+ if (text != null && text.length > 0) {
379
+ parts.push({ type: "text", content: text });
380
+ }
381
+ if (toolCalls) {
382
+ for (const tc of toolCalls) {
383
+ parts.push({
384
+ type: "tool_call",
385
+ id: tc.toolCallId,
386
+ name: tc.toolName,
387
+ arguments: tc.input
388
+ });
389
+ }
390
+ }
391
+ if (files) {
392
+ for (const file of files) {
393
+ parts.push({
394
+ type: "blob",
395
+ modality: getModality(file.mediaType),
396
+ mime_type: file.mediaType,
397
+ content: file.base64
398
+ });
399
+ }
400
+ }
401
+ return [
402
+ {
403
+ role: "assistant",
404
+ parts,
405
+ finish_reason: mapFinishReason(finishReason)
406
+ }
407
+ ];
408
+ }
409
+ function formatObjectOutputMessages({
410
+ objectText,
411
+ finishReason
412
+ }) {
413
+ return [
414
+ {
415
+ role: "assistant",
416
+ parts: [{ type: "text", content: objectText }],
417
+ finish_reason: mapFinishReason(finishReason)
418
+ }
419
+ ];
420
+ }
421
+ function mapFinishReason(reason) {
422
+ var _a;
423
+ const mapping = {
424
+ stop: "stop",
425
+ length: "length",
426
+ "content-filter": "content_filter",
427
+ "tool-calls": "tool_call",
428
+ error: "error",
429
+ other: "stop",
430
+ unknown: "stop"
431
+ };
432
+ return (_a = mapping[reason]) != null ? _a : reason;
433
+ }
434
+
435
+ // src/gen-ai-open-telemetry-integration.ts
436
+ function recordSpanError(span, error) {
437
+ if (error instanceof Error) {
438
+ span.recordException({
439
+ name: error.name,
440
+ message: error.message,
441
+ stack: error.stack
442
+ });
443
+ span.setStatus({
444
+ code: import_api.SpanStatusCode.ERROR,
445
+ message: error.message
446
+ });
447
+ } else {
448
+ span.setStatus({ code: import_api.SpanStatusCode.ERROR });
449
+ }
450
+ }
451
+ function shouldRecord(telemetry) {
452
+ return (telemetry == null ? void 0 : telemetry.isEnabled) === true;
453
+ }
454
+ function selectAttributes(telemetry, attributes) {
455
+ if (!shouldRecord(telemetry)) {
456
+ return {};
457
+ }
458
+ const result = {};
459
+ for (const [key, value] of Object.entries(attributes)) {
460
+ if (value == null) continue;
461
+ if (typeof value === "object" && "input" in value && typeof value.input === "function") {
462
+ if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
463
+ const resolved = value.input();
464
+ if (resolved != null) result[key] = resolved;
465
+ continue;
466
+ }
467
+ if (typeof value === "object" && "output" in value && typeof value.output === "function") {
468
+ if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
469
+ const resolved = value.output();
470
+ if (resolved != null) result[key] = resolved;
471
+ continue;
472
+ }
473
+ result[key] = value;
474
+ }
475
+ return result;
476
+ }
477
+ var GenAIOpenTelemetryIntegration = class {
478
+ constructor(options = {}) {
479
+ this.callStates = /* @__PURE__ */ new Map();
480
+ var _a;
481
+ this.tracer = (_a = options.tracer) != null ? _a : import_api.trace.getTracer("gen_ai");
482
+ }
483
+ getCallState(callId) {
484
+ return this.callStates.get(callId);
485
+ }
486
+ cleanupCallState(callId) {
487
+ this.callStates.delete(callId);
488
+ }
489
+ executeTool({
490
+ callId,
491
+ toolCallId,
492
+ execute
493
+ }) {
494
+ var _a;
495
+ const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
496
+ if (toolSpanEntry == null) {
497
+ return execute();
498
+ }
499
+ return import_api.context.with(toolSpanEntry.context, execute);
500
+ }
501
+ onStart(event) {
502
+ if (event.isEnabled !== true) return;
503
+ if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
504
+ this.onEmbedOperationStart(event);
505
+ return;
506
+ }
507
+ if (event.operationId === "ai.rerank") {
508
+ this.onRerankOperationStart(event);
509
+ return;
510
+ }
511
+ if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
512
+ this.onObjectOperationStart(event);
513
+ return;
514
+ }
515
+ this.onGenerateStart(event);
516
+ }
517
+ onGenerateStart(event) {
518
+ var _a;
519
+ const telemetry = {
520
+ isEnabled: event.isEnabled,
521
+ recordInputs: event.recordInputs,
522
+ recordOutputs: event.recordOutputs,
523
+ functionId: event.functionId,
524
+ metadata: event.metadata
525
+ };
526
+ const settings = {
527
+ maxOutputTokens: event.maxOutputTokens,
528
+ temperature: event.temperature,
529
+ topP: event.topP,
530
+ topK: event.topK,
531
+ presencePenalty: event.presencePenalty,
532
+ frequencyPenalty: event.frequencyPenalty,
533
+ stopSequences: event.stopSequences,
534
+ seed: event.seed,
535
+ maxRetries: event.maxRetries
536
+ };
537
+ const providerName = mapProviderName(event.provider);
538
+ const operationName = mapOperationName(event.operationId);
539
+ const attributes = selectAttributes(telemetry, {
540
+ "gen_ai.operation.name": operationName,
541
+ "gen_ai.provider.name": providerName,
542
+ "gen_ai.request.model": event.modelId,
543
+ "gen_ai.agent.name": telemetry.functionId,
544
+ "gen_ai.request.frequency_penalty": event.frequencyPenalty,
545
+ "gen_ai.request.max_tokens": event.maxOutputTokens,
546
+ "gen_ai.request.presence_penalty": event.presencePenalty,
547
+ "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
548
+ "gen_ai.request.top_k": event.topK,
549
+ "gen_ai.request.top_p": event.topP,
550
+ "gen_ai.request.stop_sequences": event.stopSequences,
551
+ "gen_ai.request.seed": event.seed,
552
+ "gen_ai.system_instructions": event.system ? {
553
+ input: () => JSON.stringify(formatSystemInstructions(event.system))
554
+ } : void 0,
555
+ "gen_ai.input.messages": {
556
+ input: () => JSON.stringify(
557
+ formatModelMessages({
558
+ prompt: event.prompt,
559
+ messages: event.messages
560
+ })
561
+ )
562
+ }
563
+ });
564
+ const spanName = `${operationName} ${event.modelId}`;
565
+ const rootSpan = this.tracer.startSpan(spanName, {
566
+ attributes,
567
+ kind: import_api.SpanKind.INTERNAL
568
+ });
569
+ const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
570
+ this.callStates.set(event.callId, {
571
+ operationId: event.operationId,
572
+ telemetry,
573
+ rootSpan,
574
+ rootContext,
575
+ stepSpan: void 0,
576
+ stepContext: void 0,
577
+ embedSpans: /* @__PURE__ */ new Map(),
578
+ rerankSpan: void 0,
579
+ toolSpans: /* @__PURE__ */ new Map(),
580
+ settings,
581
+ provider: event.provider,
582
+ modelId: event.modelId
583
+ });
584
+ }
585
+ onObjectOperationStart(event) {
586
+ var _a;
587
+ const telemetry = {
588
+ isEnabled: event.isEnabled,
589
+ recordInputs: event.recordInputs,
590
+ recordOutputs: event.recordOutputs,
591
+ functionId: event.functionId,
592
+ metadata: event.metadata
593
+ };
594
+ const settings = {
595
+ maxOutputTokens: event.maxOutputTokens,
596
+ temperature: event.temperature,
597
+ topP: event.topP,
598
+ topK: event.topK,
599
+ presencePenalty: event.presencePenalty,
600
+ frequencyPenalty: event.frequencyPenalty,
601
+ seed: event.seed,
602
+ maxRetries: event.maxRetries
603
+ };
604
+ const providerName = mapProviderName(event.provider);
605
+ const operationName = mapOperationName(event.operationId);
606
+ const attributes = selectAttributes(telemetry, {
607
+ "gen_ai.operation.name": operationName,
608
+ "gen_ai.provider.name": providerName,
609
+ "gen_ai.request.model": event.modelId,
610
+ "gen_ai.agent.name": telemetry.functionId,
611
+ "gen_ai.output.type": "json",
612
+ "gen_ai.request.frequency_penalty": event.frequencyPenalty,
613
+ "gen_ai.request.max_tokens": event.maxOutputTokens,
614
+ "gen_ai.request.presence_penalty": event.presencePenalty,
615
+ "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
616
+ "gen_ai.request.top_k": event.topK,
617
+ "gen_ai.request.top_p": event.topP,
618
+ "gen_ai.request.seed": event.seed,
619
+ "gen_ai.system_instructions": event.system ? {
620
+ input: () => JSON.stringify(formatSystemInstructions(event.system))
621
+ } : void 0,
622
+ "gen_ai.input.messages": {
623
+ input: () => JSON.stringify(
624
+ formatModelMessages({
625
+ prompt: event.prompt,
626
+ messages: event.messages
627
+ })
628
+ )
629
+ }
630
+ });
631
+ const spanName = `${operationName} ${event.modelId}`;
632
+ const rootSpan = this.tracer.startSpan(spanName, {
633
+ attributes,
634
+ kind: import_api.SpanKind.INTERNAL
635
+ });
636
+ const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
637
+ this.callStates.set(event.callId, {
638
+ operationId: event.operationId,
639
+ telemetry,
640
+ rootSpan,
641
+ rootContext,
642
+ stepSpan: void 0,
643
+ stepContext: void 0,
644
+ embedSpans: /* @__PURE__ */ new Map(),
645
+ rerankSpan: void 0,
646
+ toolSpans: /* @__PURE__ */ new Map(),
647
+ settings,
648
+ provider: event.provider,
649
+ modelId: event.modelId
650
+ });
651
+ }
652
+ /** @deprecated */
653
+ onObjectStepStart(event) {
654
+ var _a;
655
+ const state = this.getCallState(event.callId);
656
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
657
+ const { telemetry } = state;
658
+ const providerName = mapProviderName(event.provider);
659
+ const attributes = selectAttributes(telemetry, {
660
+ "gen_ai.operation.name": "chat",
661
+ "gen_ai.provider.name": providerName,
662
+ "gen_ai.request.model": event.modelId,
663
+ "gen_ai.output.type": "json",
664
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
665
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
666
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
667
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
668
+ "gen_ai.request.top_k": state.settings.topK,
669
+ "gen_ai.request.top_p": state.settings.topP,
670
+ "gen_ai.input.messages": {
671
+ input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
672
+ }
673
+ });
674
+ const spanName = `chat ${event.modelId}`;
675
+ state.stepSpan = this.tracer.startSpan(
676
+ spanName,
677
+ { attributes, kind: import_api.SpanKind.CLIENT },
678
+ state.rootContext
679
+ );
680
+ state.stepContext = import_api.trace.setSpan(state.rootContext, state.stepSpan);
681
+ }
682
+ /** @deprecated */
683
+ onObjectStepFinish(event) {
684
+ const state = this.getCallState(event.callId);
685
+ if (!(state == null ? void 0 : state.stepSpan)) return;
686
+ const { telemetry } = state;
687
+ state.stepSpan.setAttributes(
688
+ selectAttributes(telemetry, {
689
+ "gen_ai.response.finish_reasons": [event.finishReason],
690
+ "gen_ai.response.id": event.response.id,
691
+ "gen_ai.response.model": event.response.modelId,
692
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
693
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
694
+ "gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
695
+ "gen_ai.output.messages": {
696
+ output: () => {
697
+ try {
698
+ return JSON.stringify(
699
+ formatObjectOutputMessages({
700
+ objectText: event.objectText,
701
+ finishReason: event.finishReason
702
+ })
703
+ );
704
+ } catch (e) {
705
+ return event.objectText;
706
+ }
707
+ }
708
+ }
709
+ })
710
+ );
711
+ state.stepSpan.end();
712
+ state.stepSpan = void 0;
713
+ state.stepContext = void 0;
714
+ }
715
+ onEmbedOperationStart(event) {
716
+ const telemetry = {
717
+ isEnabled: event.isEnabled,
718
+ recordInputs: event.recordInputs,
719
+ recordOutputs: event.recordOutputs,
720
+ functionId: event.functionId,
721
+ metadata: event.metadata
722
+ };
723
+ const settings = {
724
+ maxRetries: event.maxRetries
725
+ };
726
+ const providerName = mapProviderName(event.provider);
727
+ const attributes = selectAttributes(telemetry, {
728
+ "gen_ai.operation.name": "embeddings",
729
+ "gen_ai.provider.name": providerName,
730
+ "gen_ai.request.model": event.modelId
731
+ });
732
+ const spanName = `embeddings ${event.modelId}`;
733
+ const rootSpan = this.tracer.startSpan(spanName, {
734
+ attributes,
735
+ kind: import_api.SpanKind.CLIENT
736
+ });
737
+ const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
738
+ this.callStates.set(event.callId, {
739
+ operationId: event.operationId,
740
+ telemetry,
741
+ rootSpan,
742
+ rootContext,
743
+ stepSpan: void 0,
744
+ stepContext: void 0,
745
+ embedSpans: /* @__PURE__ */ new Map(),
746
+ rerankSpan: void 0,
747
+ toolSpans: /* @__PURE__ */ new Map(),
748
+ settings,
749
+ provider: event.provider,
750
+ modelId: event.modelId
751
+ });
752
+ }
753
+ onStepStart(event) {
754
+ var _a;
755
+ const state = this.getCallState(event.callId);
756
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
757
+ const { telemetry } = state;
758
+ const providerName = mapProviderName(event.provider);
759
+ const attributes = selectAttributes(telemetry, {
760
+ "gen_ai.operation.name": "chat",
761
+ "gen_ai.provider.name": providerName,
762
+ "gen_ai.request.model": event.modelId,
763
+ "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
764
+ "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
765
+ "gen_ai.request.presence_penalty": state.settings.presencePenalty,
766
+ "gen_ai.request.stop_sequences": state.settings.stopSequences,
767
+ "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
768
+ "gen_ai.request.top_k": state.settings.topK,
769
+ "gen_ai.request.top_p": state.settings.topP,
770
+ "gen_ai.input.messages": {
771
+ input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
772
+ },
773
+ "gen_ai.tool.definitions": {
774
+ input: () => event.stepTools ? JSON.stringify(event.stepTools) : void 0
775
+ }
776
+ });
777
+ const spanName = `chat ${event.modelId}`;
778
+ state.stepSpan = this.tracer.startSpan(
779
+ spanName,
780
+ { attributes, kind: import_api.SpanKind.CLIENT },
781
+ state.rootContext
782
+ );
783
+ state.stepContext = import_api.trace.setSpan(state.rootContext, state.stepSpan);
784
+ }
785
+ onToolCallStart(event) {
786
+ const state = this.getCallState(event.callId);
787
+ if (!(state == null ? void 0 : state.stepContext)) return;
788
+ const { telemetry } = state;
789
+ const { toolCall } = event;
790
+ const attributes = selectAttributes(telemetry, {
791
+ "gen_ai.operation.name": "execute_tool",
792
+ "gen_ai.tool.name": toolCall.toolName,
793
+ "gen_ai.tool.call.id": toolCall.toolCallId,
794
+ "gen_ai.tool.type": "function",
795
+ "gen_ai.tool.call.arguments": {
796
+ input: () => JSON.stringify(toolCall.input)
797
+ }
798
+ });
799
+ const spanName = `execute_tool ${toolCall.toolName}`;
800
+ const toolSpan = this.tracer.startSpan(
801
+ spanName,
802
+ { attributes, kind: import_api.SpanKind.INTERNAL },
803
+ state.stepContext
804
+ );
805
+ const toolContext = import_api.trace.setSpan(state.stepContext, toolSpan);
806
+ state.toolSpans.set(toolCall.toolCallId, {
807
+ span: toolSpan,
808
+ context: toolContext
809
+ });
810
+ }
811
+ onToolCallFinish(event) {
812
+ const state = this.getCallState(event.callId);
813
+ if (!state) return;
814
+ const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
815
+ if (!toolSpanEntry) return;
816
+ const { span } = toolSpanEntry;
817
+ const { telemetry } = state;
818
+ if (event.success) {
819
+ try {
820
+ span.setAttributes(
821
+ selectAttributes(telemetry, {
822
+ "gen_ai.tool.call.result": {
823
+ output: () => JSON.stringify(event.output)
824
+ }
825
+ })
826
+ );
827
+ } catch (e) {
828
+ }
829
+ } else {
830
+ recordSpanError(span, event.error);
831
+ }
832
+ span.end();
833
+ state.toolSpans.delete(event.toolCall.toolCallId);
834
+ }
835
+ onStepFinish(event) {
836
+ var _a, _b, _c;
837
+ const state = this.getCallState(event.callId);
838
+ if (!(state == null ? void 0 : state.stepSpan)) return;
839
+ const { telemetry } = state;
840
+ state.stepSpan.setAttributes(
841
+ selectAttributes(telemetry, {
842
+ "gen_ai.response.finish_reasons": [event.finishReason],
843
+ "gen_ai.response.id": event.response.id,
844
+ "gen_ai.response.model": event.response.modelId,
845
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
846
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
847
+ "gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.usage.cachedInputTokens,
848
+ "gen_ai.usage.cache_creation.input_tokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
849
+ "gen_ai.output.messages": {
850
+ output: () => {
851
+ var _a2;
852
+ return JSON.stringify(
853
+ formatOutputMessages({
854
+ text: (_a2 = event.text) != null ? _a2 : void 0,
855
+ reasoning: event.reasoning,
856
+ toolCalls: event.toolCalls,
857
+ files: event.files,
858
+ finishReason: event.finishReason
859
+ })
860
+ );
861
+ }
862
+ }
863
+ })
864
+ );
865
+ state.stepSpan.end();
866
+ state.stepSpan = void 0;
867
+ state.stepContext = void 0;
868
+ }
869
+ onFinish(event) {
870
+ const state = this.getCallState(event.callId);
871
+ if (!(state == null ? void 0 : state.rootSpan)) return;
872
+ if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
873
+ this.onEmbedOperationFinish(event);
874
+ return;
875
+ }
876
+ if (state.operationId === "ai.rerank") {
877
+ this.onRerankOperationFinish(event);
878
+ return;
879
+ }
880
+ if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
881
+ this.onObjectOperationFinish(event);
882
+ return;
883
+ }
884
+ this.onGenerateFinish(event);
885
+ }
886
+ onGenerateFinish(event) {
887
+ var _a, _b, _c;
888
+ const state = this.getCallState(event.callId);
889
+ if (!(state == null ? void 0 : state.rootSpan)) return;
890
+ const { telemetry } = state;
891
+ state.rootSpan.setAttributes(
892
+ selectAttributes(telemetry, {
893
+ "gen_ai.response.finish_reasons": [event.finishReason],
894
+ "gen_ai.usage.input_tokens": event.totalUsage.inputTokens,
895
+ "gen_ai.usage.output_tokens": event.totalUsage.outputTokens,
896
+ "gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.totalUsage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.totalUsage.cachedInputTokens,
897
+ "gen_ai.usage.cache_creation.input_tokens": (_c = event.totalUsage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
898
+ "gen_ai.output.messages": {
899
+ output: () => {
900
+ var _a2;
901
+ return JSON.stringify(
902
+ formatOutputMessages({
903
+ text: (_a2 = event.text) != null ? _a2 : void 0,
904
+ reasoning: event.reasoning,
905
+ toolCalls: event.toolCalls,
906
+ files: event.files,
907
+ finishReason: event.finishReason
908
+ })
909
+ );
910
+ }
911
+ }
912
+ })
913
+ );
914
+ state.rootSpan.end();
915
+ this.cleanupCallState(event.callId);
916
+ }
917
+ onObjectOperationFinish(event) {
918
+ const state = this.getCallState(event.callId);
919
+ if (!(state == null ? void 0 : state.rootSpan)) return;
920
+ const { telemetry } = state;
921
+ state.rootSpan.setAttributes(
922
+ selectAttributes(telemetry, {
923
+ "gen_ai.response.finish_reasons": [event.finishReason],
924
+ "gen_ai.usage.input_tokens": event.usage.inputTokens,
925
+ "gen_ai.usage.output_tokens": event.usage.outputTokens,
926
+ "gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
927
+ "gen_ai.output.messages": {
928
+ output: () => event.object != null ? JSON.stringify(
929
+ formatObjectOutputMessages({
930
+ objectText: JSON.stringify(event.object),
931
+ finishReason: event.finishReason
932
+ })
933
+ ) : void 0
934
+ }
935
+ })
936
+ );
937
+ state.rootSpan.end();
938
+ this.cleanupCallState(event.callId);
939
+ }
940
+ onEmbedOperationFinish(event) {
941
+ const state = this.getCallState(event.callId);
942
+ if (!(state == null ? void 0 : state.rootSpan)) return;
943
+ const { telemetry } = state;
944
+ state.rootSpan.setAttributes(
945
+ selectAttributes(telemetry, {
946
+ "gen_ai.usage.input_tokens": event.usage.tokens
947
+ })
948
+ );
949
+ state.rootSpan.end();
950
+ this.cleanupCallState(event.callId);
951
+ }
952
+ onEmbedStart(event) {
953
+ const state = this.getCallState(event.callId);
954
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
955
+ const { telemetry } = state;
956
+ const providerName = mapProviderName(state.provider);
957
+ const attributes = selectAttributes(telemetry, {
958
+ "gen_ai.operation.name": "embeddings",
959
+ "gen_ai.provider.name": providerName,
960
+ "gen_ai.request.model": state.modelId
961
+ });
962
+ const spanName = `embeddings ${state.modelId}`;
963
+ const embedSpan = this.tracer.startSpan(
964
+ spanName,
965
+ { attributes, kind: import_api.SpanKind.CLIENT },
966
+ state.rootContext
967
+ );
968
+ const embedContext = import_api.trace.setSpan(state.rootContext, embedSpan);
969
+ state.embedSpans.set(event.embedCallId, {
970
+ span: embedSpan,
971
+ context: embedContext
972
+ });
973
+ }
974
+ onEmbedFinish(event) {
975
+ const state = this.getCallState(event.callId);
976
+ if (!state) return;
977
+ const embedSpanEntry = state.embedSpans.get(event.embedCallId);
978
+ if (!embedSpanEntry) return;
979
+ const { span } = embedSpanEntry;
980
+ const { telemetry } = state;
981
+ span.setAttributes(
982
+ selectAttributes(telemetry, {
983
+ "gen_ai.usage.input_tokens": event.usage.tokens
984
+ })
985
+ );
986
+ span.end();
987
+ state.embedSpans.delete(event.embedCallId);
988
+ }
989
+ onRerankOperationStart(event) {
990
+ const telemetry = {
991
+ isEnabled: event.isEnabled,
992
+ recordInputs: event.recordInputs,
993
+ recordOutputs: event.recordOutputs,
994
+ functionId: event.functionId,
995
+ metadata: event.metadata
996
+ };
997
+ const settings = {
998
+ maxRetries: event.maxRetries
999
+ };
1000
+ const providerName = mapProviderName(event.provider);
1001
+ const attributes = selectAttributes(telemetry, {
1002
+ "gen_ai.operation.name": "rerank",
1003
+ "gen_ai.provider.name": providerName,
1004
+ "gen_ai.request.model": event.modelId
1005
+ });
1006
+ const spanName = `rerank ${event.modelId}`;
1007
+ const rootSpan = this.tracer.startSpan(spanName, {
1008
+ attributes,
1009
+ kind: import_api.SpanKind.CLIENT
1010
+ });
1011
+ const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1012
+ this.callStates.set(event.callId, {
1013
+ operationId: event.operationId,
1014
+ telemetry,
1015
+ rootSpan,
1016
+ rootContext,
1017
+ stepSpan: void 0,
1018
+ stepContext: void 0,
1019
+ embedSpans: /* @__PURE__ */ new Map(),
1020
+ rerankSpan: void 0,
1021
+ toolSpans: /* @__PURE__ */ new Map(),
1022
+ settings,
1023
+ provider: event.provider,
1024
+ modelId: event.modelId
1025
+ });
1026
+ }
1027
+ onRerankOperationFinish(event) {
1028
+ const state = this.getCallState(event.callId);
1029
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1030
+ state.rootSpan.end();
1031
+ this.cleanupCallState(event.callId);
1032
+ }
1033
+ onRerankStart(event) {
1034
+ const state = this.getCallState(event.callId);
1035
+ if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1036
+ const { telemetry } = state;
1037
+ const providerName = mapProviderName(state.provider);
1038
+ const attributes = selectAttributes(telemetry, {
1039
+ "gen_ai.operation.name": "rerank",
1040
+ "gen_ai.provider.name": providerName,
1041
+ "gen_ai.request.model": state.modelId
1042
+ });
1043
+ const spanName = `rerank ${state.modelId}`;
1044
+ const rerankSpan = this.tracer.startSpan(
1045
+ spanName,
1046
+ { attributes, kind: import_api.SpanKind.CLIENT },
1047
+ state.rootContext
1048
+ );
1049
+ const rerankContext = import_api.trace.setSpan(state.rootContext, rerankSpan);
1050
+ state.rerankSpan = { span: rerankSpan, context: rerankContext };
1051
+ }
1052
+ onRerankFinish(event) {
1053
+ const state = this.getCallState(event.callId);
1054
+ if (!(state == null ? void 0 : state.rerankSpan)) return;
1055
+ const { span } = state.rerankSpan;
1056
+ span.end();
1057
+ state.rerankSpan = void 0;
1058
+ }
1059
+ onChunk(_event) {
1060
+ }
1061
+ onError(error) {
1062
+ var _a;
1063
+ const event = error;
1064
+ if (!(event == null ? void 0 : event.callId)) return;
1065
+ const state = this.getCallState(event.callId);
1066
+ if (!(state == null ? void 0 : state.rootSpan)) return;
1067
+ const actualError = (_a = event.error) != null ? _a : error;
1068
+ if (state.stepSpan) {
1069
+ recordSpanError(state.stepSpan, actualError);
1070
+ state.stepSpan.end();
1071
+ }
1072
+ for (const { span: embedSpan } of state.embedSpans.values()) {
1073
+ recordSpanError(embedSpan, actualError);
1074
+ embedSpan.end();
1075
+ }
1076
+ state.embedSpans.clear();
1077
+ if (state.rerankSpan) {
1078
+ recordSpanError(state.rerankSpan.span, actualError);
1079
+ state.rerankSpan.span.end();
1080
+ state.rerankSpan = void 0;
1081
+ }
1082
+ recordSpanError(state.rootSpan, actualError);
1083
+ state.rootSpan.end();
1084
+ this.cleanupCallState(event.callId);
1085
+ }
1086
+ };
1087
+
27
1088
  // src/open-telemetry-integration.ts
28
- var import_api = require("@opentelemetry/api");
1089
+ var import_api2 = require("@opentelemetry/api");
29
1090
 
30
1091
  // src/assemble-operation-name.ts
31
1092
  function assembleOperationName({
@@ -79,7 +1140,7 @@ function getBaseTelemetryAttributes({
79
1140
  }
80
1141
 
81
1142
  // src/stringify-for-telemetry.ts
82
- var import_ai = require("ai");
1143
+ var import_ai2 = require("ai");
83
1144
  function stringifyForTelemetry(prompt) {
84
1145
  return JSON.stringify(
85
1146
  prompt.map((message) => ({
@@ -87,7 +1148,7 @@ function stringifyForTelemetry(prompt) {
87
1148
  content: typeof message.content === "string" ? message.content : message.content.map(
88
1149
  (part) => part.type === "file" ? {
89
1150
  ...part,
90
- data: part.data instanceof Uint8Array ? (0, import_ai.convertDataContentToBase64String)(part.data) : part.data
1151
+ data: part.data instanceof Uint8Array ? (0, import_ai2.convertDataContentToBase64String)(part.data) : part.data
91
1152
  } : part
92
1153
  )
93
1154
  }))
@@ -95,7 +1156,7 @@ function stringifyForTelemetry(prompt) {
95
1156
  }
96
1157
 
97
1158
  // src/open-telemetry-integration.ts
98
- function recordSpanError(span, error) {
1159
+ function recordSpanError2(span, error) {
99
1160
  if (error instanceof Error) {
100
1161
  span.recordException({
101
1162
  name: error.name,
@@ -103,18 +1164,18 @@ function recordSpanError(span, error) {
103
1164
  stack: error.stack
104
1165
  });
105
1166
  span.setStatus({
106
- code: import_api.SpanStatusCode.ERROR,
1167
+ code: import_api2.SpanStatusCode.ERROR,
107
1168
  message: error.message
108
1169
  });
109
1170
  } else {
110
- span.setStatus({ code: import_api.SpanStatusCode.ERROR });
1171
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
111
1172
  }
112
1173
  }
113
- function shouldRecord(telemetry) {
1174
+ function shouldRecord2(telemetry) {
114
1175
  return (telemetry == null ? void 0 : telemetry.isEnabled) === true;
115
1176
  }
116
- function selectAttributes(telemetry, attributes) {
117
- if (!shouldRecord(telemetry)) {
1177
+ function selectAttributes2(telemetry, attributes) {
1178
+ if (!shouldRecord2(telemetry)) {
118
1179
  return {};
119
1180
  }
120
1181
  const result = {};
@@ -140,7 +1201,7 @@ var OpenTelemetryIntegration = class {
140
1201
  constructor(options = {}) {
141
1202
  this.callStates = /* @__PURE__ */ new Map();
142
1203
  var _a;
143
- this.tracer = (_a = options.tracer) != null ? _a : import_api.trace.getTracer("ai");
1204
+ this.tracer = (_a = options.tracer) != null ? _a : import_api2.trace.getTracer("ai");
144
1205
  }
145
1206
  getCallState(callId) {
146
1207
  return this.callStates.get(callId);
@@ -158,7 +1219,7 @@ var OpenTelemetryIntegration = class {
158
1219
  if (toolSpanEntry == null) {
159
1220
  return execute();
160
1221
  }
161
- return import_api.context.with(toolSpanEntry.context, execute);
1222
+ return import_api2.context.with(toolSpanEntry.context, execute);
162
1223
  }
163
1224
  onStart(event) {
164
1225
  if (event.isEnabled !== true) return;
@@ -201,7 +1262,7 @@ var OpenTelemetryIntegration = class {
201
1262
  headers: event.headers,
202
1263
  settings
203
1264
  });
204
- const attributes = selectAttributes(telemetry, {
1265
+ const attributes = selectAttributes2(telemetry, {
205
1266
  ...assembleOperationName({
206
1267
  operationId: event.operationId,
207
1268
  telemetry
@@ -218,7 +1279,7 @@ var OpenTelemetryIntegration = class {
218
1279
  }
219
1280
  });
220
1281
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
221
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1282
+ const rootContext = import_api2.trace.setSpan(import_api2.context.active(), rootSpan);
222
1283
  this.callStates.set(event.callId, {
223
1284
  operationId: event.operationId,
224
1285
  telemetry,
@@ -257,7 +1318,7 @@ var OpenTelemetryIntegration = class {
257
1318
  headers: event.headers,
258
1319
  settings
259
1320
  });
260
- const attributes = selectAttributes(telemetry, {
1321
+ const attributes = selectAttributes2(telemetry, {
261
1322
  ...assembleOperationName({
262
1323
  operationId: event.operationId,
263
1324
  telemetry
@@ -276,7 +1337,7 @@ var OpenTelemetryIntegration = class {
276
1337
  "ai.settings.output": event.output
277
1338
  });
278
1339
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
279
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1340
+ const rootContext = import_api2.trace.setSpan(import_api2.context.active(), rootSpan);
280
1341
  this.callStates.set(event.callId, {
281
1342
  operationId: event.operationId,
282
1343
  telemetry,
@@ -298,7 +1359,7 @@ var OpenTelemetryIntegration = class {
298
1359
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
299
1360
  const { telemetry } = state;
300
1361
  const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
301
- const attributes = selectAttributes(telemetry, {
1362
+ const attributes = selectAttributes2(telemetry, {
302
1363
  ...assembleOperationName({
303
1364
  operationId: stepOperationId,
304
1365
  telemetry
@@ -321,7 +1382,7 @@ var OpenTelemetryIntegration = class {
321
1382
  { attributes },
322
1383
  state.rootContext
323
1384
  );
324
- state.stepContext = import_api.trace.setSpan(state.rootContext, state.stepSpan);
1385
+ state.stepContext = import_api2.trace.setSpan(state.rootContext, state.stepSpan);
325
1386
  }
326
1387
  /** @deprecated */
327
1388
  onObjectStepFinish(event) {
@@ -329,7 +1390,7 @@ var OpenTelemetryIntegration = class {
329
1390
  if (!(state == null ? void 0 : state.stepSpan)) return;
330
1391
  const { telemetry } = state;
331
1392
  state.stepSpan.setAttributes(
332
- selectAttributes(telemetry, {
1393
+ selectAttributes2(telemetry, {
333
1394
  "ai.response.finishReason": event.finishReason,
334
1395
  "ai.response.object": {
335
1396
  output: () => {
@@ -387,7 +1448,7 @@ var OpenTelemetryIntegration = class {
387
1448
  });
388
1449
  const value = event.value;
389
1450
  const isMany = event.operationId === "ai.embedMany";
390
- const attributes = selectAttributes(telemetry, {
1451
+ const attributes = selectAttributes2(telemetry, {
391
1452
  ...assembleOperationName({
392
1453
  operationId: event.operationId,
393
1454
  telemetry
@@ -404,7 +1465,7 @@ var OpenTelemetryIntegration = class {
404
1465
  }
405
1466
  });
406
1467
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
407
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1468
+ const rootContext = import_api2.trace.setSpan(import_api2.context.active(), rootSpan);
408
1469
  this.callStates.set(event.callId, {
409
1470
  operationId: event.operationId,
410
1471
  telemetry,
@@ -425,7 +1486,7 @@ var OpenTelemetryIntegration = class {
425
1486
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
426
1487
  const { telemetry } = state;
427
1488
  const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
428
- const attributes = selectAttributes(telemetry, {
1489
+ const attributes = selectAttributes2(telemetry, {
429
1490
  ...assembleOperationName({
430
1491
  operationId: stepOperationId,
431
1492
  telemetry
@@ -460,14 +1521,14 @@ var OpenTelemetryIntegration = class {
460
1521
  { attributes },
461
1522
  state.rootContext
462
1523
  );
463
- state.stepContext = import_api.trace.setSpan(state.rootContext, state.stepSpan);
1524
+ state.stepContext = import_api2.trace.setSpan(state.rootContext, state.stepSpan);
464
1525
  }
465
1526
  onToolCallStart(event) {
466
1527
  const state = this.getCallState(event.callId);
467
1528
  if (!(state == null ? void 0 : state.stepContext)) return;
468
1529
  const { telemetry } = state;
469
1530
  const { toolCall } = event;
470
- const attributes = selectAttributes(telemetry, {
1531
+ const attributes = selectAttributes2(telemetry, {
471
1532
  ...assembleOperationName({
472
1533
  operationId: "ai.toolCall",
473
1534
  telemetry
@@ -483,7 +1544,7 @@ var OpenTelemetryIntegration = class {
483
1544
  { attributes },
484
1545
  state.stepContext
485
1546
  );
486
- const toolContext = import_api.trace.setSpan(state.stepContext, toolSpan);
1547
+ const toolContext = import_api2.trace.setSpan(state.stepContext, toolSpan);
487
1548
  state.toolSpans.set(toolCall.toolCallId, {
488
1549
  span: toolSpan,
489
1550
  context: toolContext
@@ -499,16 +1560,16 @@ var OpenTelemetryIntegration = class {
499
1560
  if (event.success) {
500
1561
  try {
501
1562
  span.setAttributes(
502
- selectAttributes(telemetry, {
1563
+ selectAttributes2(telemetry, {
503
1564
  "ai.toolCall.result": {
504
1565
  output: () => JSON.stringify(event.output)
505
1566
  }
506
1567
  })
507
1568
  );
508
- } catch (_ignored) {
1569
+ } catch (e) {
509
1570
  }
510
1571
  } else {
511
- recordSpanError(span, event.error);
1572
+ recordSpanError2(span, event.error);
512
1573
  }
513
1574
  span.end();
514
1575
  state.toolSpans.delete(event.toolCall.toolCallId);
@@ -519,7 +1580,7 @@ var OpenTelemetryIntegration = class {
519
1580
  if (!(state == null ? void 0 : state.stepSpan)) return;
520
1581
  const { telemetry } = state;
521
1582
  state.stepSpan.setAttributes(
522
- selectAttributes(telemetry, {
1583
+ selectAttributes2(telemetry, {
523
1584
  "ai.response.finishReason": event.finishReason,
524
1585
  "ai.response.text": {
525
1586
  output: () => {
@@ -596,7 +1657,7 @@ var OpenTelemetryIntegration = class {
596
1657
  if (!(state == null ? void 0 : state.rootSpan)) return;
597
1658
  const { telemetry } = state;
598
1659
  state.rootSpan.setAttributes(
599
- selectAttributes(telemetry, {
1660
+ selectAttributes2(telemetry, {
600
1661
  "ai.response.finishReason": event.finishReason,
601
1662
  "ai.response.text": {
602
1663
  output: () => {
@@ -646,7 +1707,7 @@ var OpenTelemetryIntegration = class {
646
1707
  if (!(state == null ? void 0 : state.rootSpan)) return;
647
1708
  const { telemetry } = state;
648
1709
  state.rootSpan.setAttributes(
649
- selectAttributes(telemetry, {
1710
+ selectAttributes2(telemetry, {
650
1711
  "ai.response.finishReason": event.finishReason,
651
1712
  "ai.response.object": {
652
1713
  output: () => event.object != null ? JSON.stringify(event.object) : void 0
@@ -668,7 +1729,7 @@ var OpenTelemetryIntegration = class {
668
1729
  const { telemetry } = state;
669
1730
  const isMany = state.operationId === "ai.embedMany";
670
1731
  state.rootSpan.setAttributes(
671
- selectAttributes(telemetry, {
1732
+ selectAttributes2(telemetry, {
672
1733
  ...isMany ? {
673
1734
  "ai.embeddings": {
674
1735
  output: () => event.embedding.map((e) => JSON.stringify(e))
@@ -688,7 +1749,7 @@ var OpenTelemetryIntegration = class {
688
1749
  const state = this.getCallState(event.callId);
689
1750
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
690
1751
  const { telemetry } = state;
691
- const attributes = selectAttributes(telemetry, {
1752
+ const attributes = selectAttributes2(telemetry, {
692
1753
  ...assembleOperationName({
693
1754
  operationId: event.operationId,
694
1755
  telemetry
@@ -703,7 +1764,7 @@ var OpenTelemetryIntegration = class {
703
1764
  { attributes },
704
1765
  state.rootContext
705
1766
  );
706
- const embedContext = import_api.trace.setSpan(state.rootContext, embedSpan);
1767
+ const embedContext = import_api2.trace.setSpan(state.rootContext, embedSpan);
707
1768
  state.embedSpans.set(event.embedCallId, {
708
1769
  span: embedSpan,
709
1770
  context: embedContext
@@ -717,7 +1778,7 @@ var OpenTelemetryIntegration = class {
717
1778
  const { span } = embedSpanEntry;
718
1779
  const { telemetry } = state;
719
1780
  span.setAttributes(
720
- selectAttributes(telemetry, {
1781
+ selectAttributes2(telemetry, {
721
1782
  "ai.embeddings": {
722
1783
  output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
723
1784
  },
@@ -744,7 +1805,7 @@ var OpenTelemetryIntegration = class {
744
1805
  headers: event.headers,
745
1806
  settings
746
1807
  });
747
- const attributes = selectAttributes(telemetry, {
1808
+ const attributes = selectAttributes2(telemetry, {
748
1809
  ...assembleOperationName({
749
1810
  operationId: event.operationId,
750
1811
  telemetry
@@ -755,7 +1816,7 @@ var OpenTelemetryIntegration = class {
755
1816
  }
756
1817
  });
757
1818
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
758
- const rootContext = import_api.trace.setSpan(import_api.context.active(), rootSpan);
1819
+ const rootContext = import_api2.trace.setSpan(import_api2.context.active(), rootSpan);
759
1820
  this.callStates.set(event.callId, {
760
1821
  operationId: event.operationId,
761
1822
  telemetry,
@@ -780,7 +1841,7 @@ var OpenTelemetryIntegration = class {
780
1841
  const state = this.getCallState(event.callId);
781
1842
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
782
1843
  const { telemetry } = state;
783
- const attributes = selectAttributes(telemetry, {
1844
+ const attributes = selectAttributes2(telemetry, {
784
1845
  ...assembleOperationName({
785
1846
  operationId: event.operationId,
786
1847
  telemetry
@@ -795,7 +1856,7 @@ var OpenTelemetryIntegration = class {
795
1856
  { attributes },
796
1857
  state.rootContext
797
1858
  );
798
- const rerankContext = import_api.trace.setSpan(state.rootContext, rerankSpan);
1859
+ const rerankContext = import_api2.trace.setSpan(state.rootContext, rerankSpan);
799
1860
  state.rerankSpan = { span: rerankSpan, context: rerankContext };
800
1861
  }
801
1862
  onRerankFinish(event) {
@@ -804,7 +1865,7 @@ var OpenTelemetryIntegration = class {
804
1865
  const { span } = state.rerankSpan;
805
1866
  const { telemetry } = state;
806
1867
  span.setAttributes(
807
- selectAttributes(telemetry, {
1868
+ selectAttributes2(telemetry, {
808
1869
  "ai.ranking.type": event.documentsType,
809
1870
  "ai.ranking": {
810
1871
  output: () => event.ranking.map((r) => JSON.stringify(r))
@@ -843,26 +1904,27 @@ var OpenTelemetryIntegration = class {
843
1904
  if (!(state == null ? void 0 : state.rootSpan)) return;
844
1905
  const actualError = (_a = event.error) != null ? _a : error;
845
1906
  if (state.stepSpan) {
846
- recordSpanError(state.stepSpan, actualError);
1907
+ recordSpanError2(state.stepSpan, actualError);
847
1908
  state.stepSpan.end();
848
1909
  }
849
1910
  for (const { span: embedSpan } of state.embedSpans.values()) {
850
- recordSpanError(embedSpan, actualError);
1911
+ recordSpanError2(embedSpan, actualError);
851
1912
  embedSpan.end();
852
1913
  }
853
1914
  state.embedSpans.clear();
854
1915
  if (state.rerankSpan) {
855
- recordSpanError(state.rerankSpan.span, actualError);
1916
+ recordSpanError2(state.rerankSpan.span, actualError);
856
1917
  state.rerankSpan.span.end();
857
1918
  state.rerankSpan = void 0;
858
1919
  }
859
- recordSpanError(state.rootSpan, actualError);
1920
+ recordSpanError2(state.rootSpan, actualError);
860
1921
  state.rootSpan.end();
861
1922
  this.cleanupCallState(event.callId);
862
1923
  }
863
1924
  };
864
1925
  // Annotate the CommonJS export names for ESM import in node:
865
1926
  0 && (module.exports = {
1927
+ GenAIOpenTelemetryIntegration,
866
1928
  OpenTelemetryIntegration
867
1929
  });
868
1930
  //# sourceMappingURL=index.js.map