@ai-sdk/otel 1.0.173 → 1.0.175

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/otel",
3
- "version": "1.0.173",
3
+ "version": "1.0.175",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -26,12 +26,12 @@
26
26
  }
27
27
  },
28
28
  "dependencies": {
29
- "@ai-sdk/provider": "4.0.38",
29
+ "@ai-sdk/provider": "4.0.39",
30
30
  "@opentelemetry/api": "1.9.1",
31
- "ai": "7.0.173"
31
+ "ai": "7.0.175"
32
32
  },
33
33
  "devDependencies": {
34
- "@ai-sdk/provider-utils": "5.0.81",
34
+ "@ai-sdk/provider-utils": "5.0.82",
35
35
  "@opentelemetry/sdk-metrics": "2.8.0",
36
36
  "@opentelemetry/sdk-trace-base": "2.7.1",
37
37
  "@types/node": "22.19.19",
@@ -1,17 +1,22 @@
1
- import type {
2
- LanguageModelV4CustomPart,
3
- LanguageModelV4FilePart,
4
- LanguageModelV4Message,
5
- LanguageModelV4Prompt,
6
- LanguageModelV4ReasoningFilePart,
7
- LanguageModelV4ReasoningPart,
8
- LanguageModelV4TextPart,
9
- LanguageModelV4ToolApprovalResponsePart,
10
- LanguageModelV4ToolCallPart,
11
- LanguageModelV4ToolResultPart,
1
+ import {
2
+ getErrorMessage,
3
+ type LanguageModelV4CustomPart,
4
+ type LanguageModelV4FilePart,
5
+ type LanguageModelV4Message,
6
+ type LanguageModelV4Prompt,
7
+ type LanguageModelV4ReasoningFilePart,
8
+ type LanguageModelV4ReasoningPart,
9
+ type LanguageModelV4TextPart,
10
+ type LanguageModelV4ToolApprovalResponsePart,
11
+ type LanguageModelV4ToolCallPart,
12
+ type LanguageModelV4ToolResultPart,
12
13
  } from '@ai-sdk/provider';
13
14
  import type { ModelMessage } from '@ai-sdk/provider-utils';
14
- import { convertDataContentToBase64String } from 'ai';
15
+ import {
16
+ convertDataContentToBase64String,
17
+ type ContentPart,
18
+ type ToolSet,
19
+ } from 'ai';
15
20
 
16
21
  type LanguageModelV4ContentPart =
17
22
  | LanguageModelV4TextPart
@@ -166,91 +171,19 @@ function convertMessagePartToSemConv(
166
171
  arguments: part.input,
167
172
  };
168
173
 
169
- case 'tool-result': {
170
- const output = part.output;
171
- let response: unknown;
172
- if (output) {
173
- if (output.type === 'text' || output.type === 'error-text') {
174
- response = output.value;
175
- } else if (output.type === 'json' || output.type === 'error-json') {
176
- response = output.value;
177
- } else if (output.type === 'execution-denied') {
178
- response = { denied: true, reason: output.reason };
179
- } else {
180
- response = output;
181
- }
182
- }
174
+ case 'tool-result':
183
175
  return {
184
176
  type: 'tool_call_response',
185
177
  id: part.toolCallId ?? null,
186
- response,
178
+ response: convertToolResultOutputToResponse(part.output),
187
179
  };
188
- }
189
-
190
- case 'file': {
191
- const rawData = part.data as
192
- | string
193
- | Uint8Array
194
- | URL
195
- | ArrayBuffer
196
- | { type: 'data'; data: string | Uint8Array | ArrayBuffer }
197
- | { type: 'url'; url: URL }
198
- | { type: 'reference'; reference: Record<string, string> }
199
- | { type: 'text'; text: string };
200
180
 
201
- const data: string | Uint8Array | URL | ArrayBuffer = (() => {
202
- if (
203
- typeof rawData === 'object' &&
204
- rawData !== null &&
205
- !(rawData instanceof URL) &&
206
- !(rawData instanceof Uint8Array) &&
207
- !(rawData instanceof ArrayBuffer) &&
208
- 'type' in rawData
209
- ) {
210
- switch (rawData.type) {
211
- case 'data':
212
- return rawData.data;
213
- case 'url':
214
- return rawData.url;
215
- case 'text':
216
- return rawData.text;
217
- default:
218
- return '';
219
- }
220
- }
221
- return rawData as string | Uint8Array | URL | ArrayBuffer;
222
- })();
223
-
224
- let content: string;
225
- if (data instanceof Uint8Array) {
226
- content = convertDataContentToBase64String(data);
227
- } else if (typeof data === 'string') {
228
- if (data.startsWith('http://') || data.startsWith('https://')) {
229
- return {
230
- type: 'uri',
231
- modality: getModality(part.mediaType),
232
- mime_type: part.mediaType ?? null,
233
- uri: data,
234
- };
235
- }
236
- content = data;
237
- } else if (data instanceof URL) {
238
- return {
239
- type: 'uri',
240
- modality: getModality(part.mediaType),
241
- mime_type: part.mediaType ?? null,
242
- uri: data.toString(),
243
- };
244
- } else {
245
- content = String(data);
246
- }
247
- return {
248
- type: 'blob',
249
- modality: getModality(part.mediaType),
250
- mime_type: part.mediaType ?? null,
251
- content,
252
- };
253
- }
181
+ case 'file':
182
+ case 'reasoning-file':
183
+ return convertFileDataToSemConv({
184
+ data: part.data,
185
+ mediaType: part.mediaType,
186
+ });
254
187
 
255
188
  case 'tool-approval-response':
256
189
  return {
@@ -263,9 +196,6 @@ function convertMessagePartToSemConv(
263
196
  case 'custom':
264
197
  return { type: 'custom', kind: part.kind };
265
198
 
266
- case 'reasoning-file':
267
- return { type: String(part.type) };
268
-
269
199
  default: {
270
200
  const _exhaustive: never = part;
271
201
  return { type: String((_exhaustive as { type: string }).type) };
@@ -273,6 +203,87 @@ function convertMessagePartToSemConv(
273
203
  }
274
204
  }
275
205
 
206
+ function convertToolResultOutputToResponse(output: unknown): unknown {
207
+ if (output == null || typeof output !== 'object' || !('type' in output)) {
208
+ return output;
209
+ }
210
+
211
+ const typedOutput = output as {
212
+ type: string;
213
+ value?: unknown;
214
+ reason?: string;
215
+ };
216
+
217
+ switch (typedOutput.type) {
218
+ case 'text':
219
+ case 'error-text':
220
+ case 'json':
221
+ case 'error-json':
222
+ return typedOutput.value;
223
+ case 'execution-denied':
224
+ return { denied: true, reason: typedOutput.reason };
225
+ default:
226
+ return output;
227
+ }
228
+ }
229
+
230
+ function convertFileDataToSemConv({
231
+ data: rawData,
232
+ mediaType,
233
+ }: {
234
+ data: unknown;
235
+ mediaType: string | undefined;
236
+ }): SemConvPart {
237
+ const data: string | Uint8Array | URL | ArrayBuffer = (() => {
238
+ if (
239
+ typeof rawData === 'object' &&
240
+ rawData !== null &&
241
+ !(rawData instanceof URL) &&
242
+ !(rawData instanceof Uint8Array) &&
243
+ !(rawData instanceof ArrayBuffer) &&
244
+ 'type' in rawData
245
+ ) {
246
+ const fileData = rawData as
247
+ | { type: 'data'; data: string | Uint8Array | ArrayBuffer }
248
+ | { type: 'url'; url: URL }
249
+ | { type: 'reference'; reference: Record<string, string> }
250
+ | { type: 'text'; text: string };
251
+
252
+ switch (fileData.type) {
253
+ case 'data':
254
+ return fileData.data;
255
+ case 'url':
256
+ return fileData.url;
257
+ case 'text':
258
+ return fileData.text;
259
+ default:
260
+ return '';
261
+ }
262
+ }
263
+ return rawData as string | Uint8Array | URL | ArrayBuffer;
264
+ })();
265
+
266
+ if (
267
+ data instanceof URL ||
268
+ (typeof data === 'string' &&
269
+ (data.startsWith('http://') || data.startsWith('https://')))
270
+ ) {
271
+ return {
272
+ type: 'uri',
273
+ modality: getModality(mediaType),
274
+ mime_type: mediaType ?? null,
275
+ uri: data.toString(),
276
+ };
277
+ }
278
+
279
+ return {
280
+ type: 'blob',
281
+ modality: getModality(mediaType),
282
+ mime_type: mediaType ?? null,
283
+ content: convertDataContentToBase64String(data),
284
+ };
285
+ }
286
+
276
287
  function getModality(mediaType: string | undefined): string {
277
288
  if (!mediaType) return 'image';
278
289
  if (mediaType.startsWith('image/')) return 'image';
@@ -388,71 +399,11 @@ function convertModelMessageToSemConv(msg: ModelMessage): SemConvInputMessage {
388
399
  content: convertDataContentToBase64String(data as Uint8Array),
389
400
  };
390
401
  }
391
- case 'file': {
392
- const rawData = part.data as
393
- | string
394
- | Uint8Array
395
- | URL
396
- | ArrayBuffer
397
- | { type: 'data'; data: string | Uint8Array | ArrayBuffer }
398
- | { type: 'url'; url: URL }
399
- | { type: 'reference'; reference: Record<string, string> }
400
- | { type: 'text'; text: string };
401
-
402
- const data: string | Uint8Array | URL | ArrayBuffer = (() => {
403
- if (
404
- typeof rawData === 'object' &&
405
- rawData !== null &&
406
- !(rawData instanceof URL) &&
407
- !(rawData instanceof Uint8Array) &&
408
- !(rawData instanceof ArrayBuffer) &&
409
- 'type' in rawData
410
- ) {
411
- switch (rawData.type) {
412
- case 'data':
413
- return rawData.data;
414
- case 'url':
415
- return rawData.url;
416
- case 'text':
417
- return rawData.text;
418
- default:
419
- return '';
420
- }
421
- }
422
- return rawData as string | Uint8Array | URL | ArrayBuffer;
423
- })();
424
-
425
- if (data instanceof URL) {
426
- return {
427
- type: 'uri' as const,
428
- modality: getModality(part.mediaType),
429
- mime_type: part.mediaType ?? null,
430
- uri: data.toString(),
431
- };
432
- }
433
- if (typeof data === 'string') {
434
- if (data.startsWith('http://') || data.startsWith('https://')) {
435
- return {
436
- type: 'uri' as const,
437
- modality: getModality(part.mediaType),
438
- mime_type: part.mediaType ?? null,
439
- uri: data,
440
- };
441
- }
442
- return {
443
- type: 'blob' as const,
444
- modality: getModality(part.mediaType),
445
- mime_type: part.mediaType ?? null,
446
- content: data,
447
- };
448
- }
449
- return {
450
- type: 'blob' as const,
451
- modality: getModality(part.mediaType),
452
- mime_type: part.mediaType ?? null,
453
- content: convertDataContentToBase64String(data as Uint8Array),
454
- };
455
- }
402
+ case 'file':
403
+ return convertFileDataToSemConv({
404
+ data: part.data,
405
+ mediaType: part.mediaType,
406
+ });
456
407
  default:
457
408
  return { type: String((part as { type: string }).type) };
458
409
  }
@@ -473,6 +424,14 @@ function convertModelMessageToSemConv(msg: ModelMessage): SemConvInputMessage {
473
424
  return { type: 'text' as const, content: part.text };
474
425
  case 'reasoning':
475
426
  return { type: 'reasoning' as const, content: part.text };
427
+ case 'file':
428
+ case 'reasoning-file':
429
+ return convertFileDataToSemConv({
430
+ data: part.data,
431
+ mediaType: part.mediaType,
432
+ });
433
+ case 'custom':
434
+ return { type: 'custom', kind: part.kind };
476
435
  case 'tool-call':
477
436
  return {
478
437
  type: 'tool_call' as const,
@@ -480,26 +439,19 @@ function convertModelMessageToSemConv(msg: ModelMessage): SemConvInputMessage {
480
439
  name: part.toolName,
481
440
  arguments: part.input,
482
441
  };
483
- case 'tool-result': {
484
- const output = part.output;
485
- let response: unknown;
486
- if (output) {
487
- if (output.type === 'text' || output.type === 'error-text') {
488
- response = output.value;
489
- } else if (output.type === 'json' || output.type === 'error-json') {
490
- response = output.value;
491
- } else if (output.type === 'execution-denied') {
492
- response = { denied: true, reason: output.reason };
493
- } else {
494
- response = output;
495
- }
496
- }
442
+ case 'tool-result':
497
443
  return {
498
444
  type: 'tool_call_response' as const,
499
445
  id: part.toolCallId ?? null,
500
- response,
446
+ response: convertToolResultOutputToResponse(part.output),
447
+ };
448
+ case 'tool-approval-request':
449
+ return {
450
+ type: 'tool_approval_request',
451
+ approval_id: part.approvalId,
452
+ tool_call_id: part.toolCallId,
453
+ reason: part.reason,
501
454
  };
502
- }
503
455
  default:
504
456
  return { type: String((part as { type: string }).type) };
505
457
  }
@@ -509,27 +461,23 @@ function convertModelMessageToSemConv(msg: ModelMessage): SemConvInputMessage {
509
461
 
510
462
  if (msg.role === 'tool') {
511
463
  const parts: SemConvPart[] = msg.content.map(part => {
512
- if (part.type === 'tool-result') {
513
- const output = part.output;
514
- let response: unknown;
515
- if (output) {
516
- if (output.type === 'text' || output.type === 'error-text') {
517
- response = output.value;
518
- } else if (output.type === 'json' || output.type === 'error-json') {
519
- response = output.value;
520
- } else if (output.type === 'execution-denied') {
521
- response = { denied: true, reason: output.reason };
522
- } else {
523
- response = output;
524
- }
525
- }
526
- return {
527
- type: 'tool_call_response' as const,
528
- id: part.toolCallId ?? null,
529
- response,
530
- };
464
+ switch (part.type) {
465
+ case 'tool-result':
466
+ return {
467
+ type: 'tool_call_response' as const,
468
+ id: part.toolCallId ?? null,
469
+ response: convertToolResultOutputToResponse(part.output),
470
+ };
471
+ case 'tool-approval-response':
472
+ return {
473
+ type: 'tool_approval_response',
474
+ approval_id: part.approvalId,
475
+ approved: part.approved,
476
+ reason: part.reason,
477
+ };
478
+ default:
479
+ return { type: String((part as { type: string }).type) };
531
480
  }
532
- return { type: String((part as { type: string }).type) };
533
481
  });
534
482
  return { role: 'tool', parts };
535
483
  }
@@ -550,8 +498,117 @@ export function extractSystemFromPrompt(
550
498
  }
551
499
  }
552
500
 
501
+ /**
502
+ * Converts content parts produced by a model call or step to the
503
+ * gen_ai.output.messages SemConv format.
504
+ * Parts are preserved in the order they were produced.
505
+ */
506
+ export function formatOutputContent({
507
+ content,
508
+ finishReason,
509
+ }: {
510
+ content: ReadonlyArray<ContentPart<ToolSet>>;
511
+ finishReason: string;
512
+ }): SemConvOutputMessage[] {
513
+ const parts: SemConvPart[] = [];
514
+
515
+ for (const part of content) {
516
+ switch (part.type) {
517
+ case 'text':
518
+ // skip empty text without provider metadata (same as response messages):
519
+ if (part.text.length > 0 || part.providerMetadata != null) {
520
+ parts.push({ type: 'text', content: part.text });
521
+ }
522
+ break;
523
+
524
+ case 'reasoning':
525
+ parts.push({ type: 'reasoning', content: part.text });
526
+ break;
527
+
528
+ case 'file':
529
+ case 'reasoning-file':
530
+ parts.push(
531
+ convertFileDataToSemConv({
532
+ data: part.file.base64,
533
+ mediaType: part.file.mediaType,
534
+ }),
535
+ );
536
+ break;
537
+
538
+ case 'custom':
539
+ parts.push({ type: 'custom', kind: part.kind });
540
+ break;
541
+
542
+ case 'tool-call':
543
+ parts.push({
544
+ type: 'tool_call',
545
+ id: part.toolCallId,
546
+ name: part.toolName,
547
+ arguments: part.input,
548
+ });
549
+ break;
550
+
551
+ case 'tool-result':
552
+ parts.push({
553
+ type: 'tool_call_response',
554
+ id: part.toolCallId,
555
+ response: part.output,
556
+ });
557
+ break;
558
+
559
+ case 'tool-error':
560
+ parts.push({
561
+ type: 'tool_call_response',
562
+ id: part.toolCallId,
563
+ response: part.providerExecuted
564
+ ? part.error
565
+ : getErrorMessage(part.error),
566
+ });
567
+ break;
568
+
569
+ case 'tool-approval-request':
570
+ parts.push({
571
+ type: 'tool_approval_request',
572
+ approval_id: part.approvalId,
573
+ tool_call_id: part.toolCall.toolCallId,
574
+ reason: part.reason,
575
+ });
576
+ break;
577
+
578
+ case 'tool-approval-response':
579
+ parts.push({
580
+ type: 'tool_approval_response',
581
+ approval_id: part.approvalId,
582
+ approved: part.approved,
583
+ reason: part.reason,
584
+ });
585
+ break;
586
+
587
+ case 'source':
588
+ // sources are response metadata, not message parts:
589
+ break;
590
+
591
+ default: {
592
+ const _exhaustive: never = part;
593
+ parts.push({ type: String((_exhaustive as { type: string }).type) });
594
+ }
595
+ }
596
+ }
597
+
598
+ return [
599
+ {
600
+ role: 'assistant',
601
+ parts,
602
+ finish_reason: mapFinishReason(finishReason),
603
+ },
604
+ ];
605
+ }
606
+
553
607
  /**
554
608
  * Converts step result data to the gen_ai.output.messages SemConv format.
609
+ *
610
+ * @deprecated Use `formatOutputContent` instead. This function groups parts
611
+ * by type and does not preserve the order in which they were produced.
555
612
  */
556
613
  export function formatOutputMessages({
557
614
  text,
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export {
3
3
  formatInputMessages,
4
4
  formatModelMessages,
5
5
  formatObjectOutputMessages,
6
+ formatOutputContent,
6
7
  formatOutputMessages,
7
8
  formatSystemInstructions,
8
9
  } from './gen-ai-format-messages';
@@ -54,7 +54,7 @@ import {
54
54
  formatInputMessages,
55
55
  formatModelMessages,
56
56
  formatObjectOutputMessages,
57
- formatOutputMessages,
57
+ formatOutputContent,
58
58
  formatSystemInstructions,
59
59
  mapOperationName,
60
60
  mapProviderName,
@@ -1226,20 +1226,8 @@ export class OpenTelemetry implements Telemetry {
1226
1226
  'gen_ai.output.messages': {
1227
1227
  output: () =>
1228
1228
  JSON.stringify(
1229
- formatOutputMessages({
1230
- text:
1231
- event.content
1232
- .filter(p => p.type === 'text')
1233
- .map(p => p.text)
1234
- .join('') || undefined,
1235
- reasoning: event.content.filter(p => p.type === 'reasoning'),
1236
- toolCalls: event.content.filter(p => p.type === 'tool-call'),
1237
- toolResults: event.content.filter(
1238
- p => p.type === 'tool-result',
1239
- ),
1240
- files: event.content
1241
- .filter(p => p.type === 'file')
1242
- .map(p => p.file),
1229
+ formatOutputContent({
1230
+ content: event.content,
1243
1231
  finishReason: event.finishReason,
1244
1232
  }),
1245
1233
  ),
@@ -1742,14 +1730,8 @@ export class OpenTelemetry implements Telemetry {
1742
1730
  'gen_ai.output.messages': {
1743
1731
  output: () =>
1744
1732
  JSON.stringify(
1745
- formatOutputMessages({
1746
- text: event.text ?? undefined,
1747
- reasoning: event.finalStep.reasoning as ReadonlyArray<{
1748
- text?: string;
1749
- }>,
1750
- toolCalls: event.toolCalls,
1751
- toolResults: event.toolResults,
1752
- files: event.files,
1733
+ formatOutputContent({
1734
+ content: event.content,
1753
1735
  finishReason: event.finishReason,
1754
1736
  }),
1755
1737
  ),
@@ -30,6 +30,20 @@ export function redactToolDefinition(
30
30
  };
31
31
  }
32
32
 
33
+ if (tool.id === 'google.mcp_server') {
34
+ if (!isRecord(args.headers)) return tool;
35
+
36
+ return {
37
+ ...tool,
38
+ args: {
39
+ ...args,
40
+ headers: Object.fromEntries(
41
+ Object.keys(args.headers).map(name => [name, '[redacted]']),
42
+ ),
43
+ },
44
+ };
45
+ }
46
+
33
47
  const containerKey =
34
48
  tool.id === 'openai.code_interpreter'
35
49
  ? 'container'