@ai-sdk/otel 1.0.0-beta.59 → 1.0.0-beta.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  // src/open-telemetry.ts
2
2
  import {
3
- context,
3
+ context as context2,
4
4
  SpanKind,
5
- SpanStatusCode,
6
5
  trace
7
6
  } from "@opentelemetry/api";
8
7
 
@@ -58,7 +57,7 @@ function formatSystemInstructions(system) {
58
57
  return [{ type: "text", content: system.content }];
59
58
  }
60
59
  function convertMessagePartToSemConv(part) {
61
- var _a, _b, _c, _d;
60
+ var _a, _b, _c, _d, _e;
62
61
  switch (part.type) {
63
62
  case "text":
64
63
  return { type: "text", content: part.text };
@@ -92,7 +91,22 @@ function convertMessagePartToSemConv(part) {
92
91
  };
93
92
  }
94
93
  case "file": {
95
- const data = part.data;
94
+ const rawData = part.data;
95
+ const data = (() => {
96
+ if (typeof rawData === "object" && rawData !== null && !(rawData instanceof URL) && !(rawData instanceof Uint8Array) && !(rawData instanceof ArrayBuffer) && "type" in rawData) {
97
+ switch (rawData.type) {
98
+ case "data":
99
+ return rawData.data;
100
+ case "url":
101
+ return rawData.url;
102
+ case "text":
103
+ return rawData.text;
104
+ default:
105
+ return "";
106
+ }
107
+ }
108
+ return rawData;
109
+ })();
96
110
  let content;
97
111
  if (data instanceof Uint8Array) {
98
112
  content = convertDataContentToBase64String(data);
@@ -106,13 +120,20 @@ function convertMessagePartToSemConv(part) {
106
120
  };
107
121
  }
108
122
  content = data;
123
+ } else if (data instanceof URL) {
124
+ return {
125
+ type: "uri",
126
+ modality: getModality(part.mediaType),
127
+ mime_type: (_d = part.mediaType) != null ? _d : null,
128
+ uri: data.toString()
129
+ };
109
130
  } else {
110
131
  content = String(data);
111
132
  }
112
133
  return {
113
134
  type: "blob",
114
135
  modality: getModality(part.mediaType),
115
- mime_type: (_d = part.mediaType) != null ? _d : null,
136
+ mime_type: (_e = part.mediaType) != null ? _e : null,
116
137
  content
117
138
  };
118
139
  }
@@ -224,7 +245,22 @@ function convertModelMessageToSemConv(msg) {
224
245
  };
225
246
  }
226
247
  case "file": {
227
- const data = part.data;
248
+ const rawData = part.data;
249
+ const data = (() => {
250
+ if (typeof rawData === "object" && rawData !== null && !(rawData instanceof URL) && !(rawData instanceof Uint8Array) && !(rawData instanceof ArrayBuffer) && "type" in rawData) {
251
+ switch (rawData.type) {
252
+ case "data":
253
+ return rawData.data;
254
+ case "url":
255
+ return rawData.url;
256
+ case "text":
257
+ return rawData.text;
258
+ default:
259
+ return "";
260
+ }
261
+ }
262
+ return rawData;
263
+ })();
228
264
  if (data instanceof URL) {
229
265
  return {
230
266
  type: "uri",
@@ -410,8 +446,12 @@ function mapFinishReason(reason) {
410
446
  return (_a = mapping[reason]) != null ? _a : reason;
411
447
  }
412
448
 
413
- // src/open-telemetry.ts
414
- function recordSpanError(span, error) {
449
+ // src/record-span.ts
450
+ import {
451
+ SpanStatusCode,
452
+ context
453
+ } from "@opentelemetry/api";
454
+ function recordErrorOnSpan(span, error) {
415
455
  if (error instanceof Error) {
416
456
  span.recordException({
417
457
  name: error.name,
@@ -426,6 +466,8 @@ function recordSpanError(span, error) {
426
466
  span.setStatus({ code: SpanStatusCode.ERROR });
427
467
  }
428
468
  }
469
+
470
+ // src/select-attributes.ts
429
471
  function shouldRecord(telemetry) {
430
472
  return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
431
473
  }
@@ -452,11 +494,68 @@ function selectAttributes(telemetry, attributes) {
452
494
  }
453
495
  return result;
454
496
  }
497
+
498
+ // src/supplemental-attributes.ts
499
+ var disabledSupplementalAttributes = {
500
+ usage: false,
501
+ providerMetadata: false,
502
+ embedding: false,
503
+ reranking: false,
504
+ runtimeContext: false,
505
+ headers: false,
506
+ toolChoice: false,
507
+ schema: false
508
+ };
509
+ function normalizeSupplementalAttributes(options) {
510
+ var _a, _b, _c, _d, _e, _f, _g, _h;
511
+ return {
512
+ ...disabledSupplementalAttributes,
513
+ usage: (_a = options.usage) != null ? _a : false,
514
+ providerMetadata: (_b = options.providerMetadata) != null ? _b : false,
515
+ embedding: (_c = options.embedding) != null ? _c : false,
516
+ reranking: (_d = options.reranking) != null ? _d : false,
517
+ runtimeContext: (_e = options.runtimeContext) != null ? _e : false,
518
+ headers: (_f = options.headers) != null ? _f : false,
519
+ toolChoice: (_g = options.toolChoice) != null ? _g : false,
520
+ schema: (_h = options.schema) != null ? _h : false
521
+ };
522
+ }
523
+ function getRuntimeContextAttributes(context4) {
524
+ return Object.fromEntries(
525
+ Object.entries(context4 != null ? context4 : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.settings.context.${key}`, value])
526
+ );
527
+ }
528
+ function getHeaderAttributes(headers) {
529
+ return Object.fromEntries(
530
+ Object.entries(headers != null ? headers : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.request.headers.${key}`, value])
531
+ );
532
+ }
533
+ function getDetailedUsageAttributes(usage) {
534
+ var _a, _b, _c;
535
+ return {
536
+ "ai.usage.inputTokenDetails.noCacheTokens": (_a = usage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
537
+ "ai.usage.outputTokenDetails.textTokens": (_b = usage.outputTokenDetails) == null ? void 0 : _b.textTokens,
538
+ "ai.usage.outputTokenDetails.reasoningTokens": (_c = usage.outputTokenDetails) == null ? void 0 : _c.reasoningTokens
539
+ };
540
+ }
541
+ function selectSupplementalAttributes(telemetry, enabledAttributes, attributes) {
542
+ const result = {};
543
+ for (const [key, value] of Object.entries(attributes)) {
544
+ if (!enabledAttributes[key] || value == null) {
545
+ continue;
546
+ }
547
+ Object.assign(result, selectAttributes(telemetry, value));
548
+ }
549
+ return result;
550
+ }
551
+
552
+ // src/open-telemetry.ts
455
553
  var OpenTelemetry = class {
456
554
  constructor(options = {}) {
457
555
  this.callStates = /* @__PURE__ */ new Map();
458
556
  var _a;
459
557
  this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
558
+ this.supplementalAttributes = normalizeSupplementalAttributes(options);
460
559
  }
461
560
  getCallState(callId) {
462
561
  return this.callStates.get(callId);
@@ -474,7 +573,7 @@ var OpenTelemetry = class {
474
573
  if (toolSpanEntry == null) {
475
574
  return execute();
476
575
  }
477
- return context.with(toolSpanEntry.context, execute);
576
+ return context2.with(toolSpanEntry.context, execute);
478
577
  }
479
578
  onStart(event) {
480
579
  if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
@@ -515,6 +614,16 @@ var OpenTelemetry = class {
515
614
  };
516
615
  const providerName = mapProviderName(event.provider);
517
616
  const operationName = mapOperationName(event.operationId);
617
+ const baseSupplementalAttributes = selectSupplementalAttributes(
618
+ telemetry,
619
+ this.supplementalAttributes,
620
+ {
621
+ runtimeContext: getRuntimeContextAttributes(
622
+ event.runtimeContext
623
+ ),
624
+ headers: getHeaderAttributes(event.headers)
625
+ }
626
+ );
518
627
  const attributes = selectAttributes(telemetry, {
519
628
  "gen_ai.operation.name": operationName,
520
629
  "gen_ai.provider.name": providerName,
@@ -538,14 +647,15 @@ var OpenTelemetry = class {
538
647
  messages: event.messages
539
648
  })
540
649
  )
541
- }
650
+ },
651
+ ...baseSupplementalAttributes
542
652
  });
543
653
  const spanName = `${operationName} ${event.modelId}`;
544
654
  const rootSpan = this.tracer.startSpan(spanName, {
545
655
  attributes,
546
656
  kind: SpanKind.INTERNAL
547
657
  });
548
- const rootContext = trace.setSpan(context.active(), rootSpan);
658
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
549
659
  this.callStates.set(event.callId, {
550
660
  operationId: event.operationId,
551
661
  telemetry,
@@ -560,7 +670,8 @@ var OpenTelemetry = class {
560
670
  toolSpans: /* @__PURE__ */ new Map(),
561
671
  settings,
562
672
  provider: event.provider,
563
- modelId: event.modelId
673
+ modelId: event.modelId,
674
+ baseSupplementalAttributes
564
675
  });
565
676
  }
566
677
  onObjectOperationStart(event) {
@@ -582,6 +693,13 @@ var OpenTelemetry = class {
582
693
  };
583
694
  const providerName = mapProviderName(event.provider);
584
695
  const operationName = mapOperationName(event.operationId);
696
+ const baseSupplementalAttributes = selectSupplementalAttributes(
697
+ telemetry,
698
+ this.supplementalAttributes,
699
+ {
700
+ headers: getHeaderAttributes(event.headers)
701
+ }
702
+ );
585
703
  const attributes = selectAttributes(telemetry, {
586
704
  "gen_ai.operation.name": operationName,
587
705
  "gen_ai.provider.name": providerName,
@@ -605,14 +723,23 @@ var OpenTelemetry = class {
605
723
  messages: event.messages
606
724
  })
607
725
  )
608
- }
726
+ },
727
+ ...baseSupplementalAttributes,
728
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
729
+ schema: {
730
+ "ai.schema": event.schema ? { input: () => JSON.stringify(event.schema) } : void 0,
731
+ "ai.schema.name": event.schemaName,
732
+ "ai.schema.description": event.schemaDescription,
733
+ "ai.settings.output": event.output
734
+ }
735
+ })
609
736
  });
610
737
  const spanName = `${operationName} ${event.modelId}`;
611
738
  const rootSpan = this.tracer.startSpan(spanName, {
612
739
  attributes,
613
740
  kind: SpanKind.INTERNAL
614
741
  });
615
- const rootContext = trace.setSpan(context.active(), rootSpan);
742
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
616
743
  this.callStates.set(event.callId, {
617
744
  operationId: event.operationId,
618
745
  telemetry,
@@ -627,7 +754,8 @@ var OpenTelemetry = class {
627
754
  toolSpans: /* @__PURE__ */ new Map(),
628
755
  settings,
629
756
  provider: event.provider,
630
- modelId: event.modelId
757
+ modelId: event.modelId,
758
+ baseSupplementalAttributes
631
759
  });
632
760
  }
633
761
  /** @deprecated */
@@ -650,7 +778,8 @@ var OpenTelemetry = class {
650
778
  "gen_ai.request.top_p": state.settings.topP,
651
779
  "gen_ai.input.messages": {
652
780
  input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
653
- }
781
+ },
782
+ ...state.baseSupplementalAttributes
654
783
  });
655
784
  const spanName = `chat ${event.modelId}`;
656
785
  state.inferenceSpan = this.tracer.startSpan(
@@ -689,7 +818,17 @@ var OpenTelemetry = class {
689
818
  return event.objectText;
690
819
  }
691
820
  }
692
- }
821
+ },
822
+ ...selectSupplementalAttributes(
823
+ telemetry,
824
+ this.supplementalAttributes,
825
+ {
826
+ providerMetadata: {
827
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
828
+ },
829
+ usage: getDetailedUsageAttributes(event.usage)
830
+ }
831
+ )
693
832
  })
694
833
  );
695
834
  state.inferenceSpan.end();
@@ -702,21 +841,39 @@ var OpenTelemetry = class {
702
841
  recordOutputs: event.recordOutputs,
703
842
  functionId: event.functionId
704
843
  };
705
- const settings = {
706
- maxRetries: event.maxRetries
707
- };
708
844
  const providerName = mapProviderName(event.provider);
845
+ const baseSupplementalAttributes = selectSupplementalAttributes(
846
+ telemetry,
847
+ this.supplementalAttributes,
848
+ {
849
+ headers: getHeaderAttributes(event.headers)
850
+ }
851
+ );
852
+ const value = event.value;
853
+ const isMany = event.operationId === "ai.embedMany";
709
854
  const attributes = selectAttributes(telemetry, {
710
855
  "gen_ai.operation.name": "embeddings",
711
856
  "gen_ai.provider.name": providerName,
712
- "gen_ai.request.model": event.modelId
857
+ "gen_ai.request.model": event.modelId,
858
+ ...baseSupplementalAttributes,
859
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
860
+ embedding: isMany ? {
861
+ "ai.values": {
862
+ input: () => value.map((v) => JSON.stringify(v))
863
+ }
864
+ } : {
865
+ "ai.value": {
866
+ input: () => JSON.stringify(value)
867
+ }
868
+ }
869
+ })
713
870
  });
714
871
  const spanName = `embeddings ${event.modelId}`;
715
872
  const rootSpan = this.tracer.startSpan(spanName, {
716
873
  attributes,
717
874
  kind: SpanKind.CLIENT
718
875
  });
719
- const rootContext = trace.setSpan(context.active(), rootSpan);
876
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
720
877
  this.callStates.set(event.callId, {
721
878
  operationId: event.operationId,
722
879
  telemetry,
@@ -729,9 +886,10 @@ var OpenTelemetry = class {
729
886
  embedSpans: /* @__PURE__ */ new Map(),
730
887
  rerankSpan: void 0,
731
888
  toolSpans: /* @__PURE__ */ new Map(),
732
- settings,
889
+ settings: { maxRetries: event.maxRetries },
733
890
  provider: event.provider,
734
- modelId: event.modelId
891
+ modelId: event.modelId,
892
+ baseSupplementalAttributes
735
893
  });
736
894
  }
737
895
  onStepStart(event) {
@@ -739,7 +897,15 @@ var OpenTelemetry = class {
739
897
  if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
740
898
  const { telemetry } = state;
741
899
  const stepAttributes = selectAttributes(telemetry, {
742
- "gen_ai.operation.name": "agent_step"
900
+ "gen_ai.operation.name": "agent_step",
901
+ ...state.baseSupplementalAttributes,
902
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
903
+ toolChoice: {
904
+ "ai.prompt.toolChoice": {
905
+ input: () => event.stepToolChoice != null ? JSON.stringify(event.stepToolChoice) : void 0
906
+ }
907
+ }
908
+ })
743
909
  });
744
910
  state.stepSpan = this.tracer.startSpan(
745
911
  `step ${event.steps.length + 1}`,
@@ -811,7 +977,14 @@ var OpenTelemetry = class {
811
977
  finishReason: event.finishReason
812
978
  })
813
979
  )
814
- }
980
+ },
981
+ ...selectSupplementalAttributes(
982
+ telemetry,
983
+ this.supplementalAttributes,
984
+ {
985
+ usage: getDetailedUsageAttributes(event.usage)
986
+ }
987
+ )
815
988
  })
816
989
  );
817
990
  state.inferenceSpan.end();
@@ -864,7 +1037,7 @@ var OpenTelemetry = class {
864
1037
  } catch (e) {
865
1038
  }
866
1039
  } else {
867
- recordSpanError(span, toolOutput.error);
1040
+ recordErrorOnSpan(span, toolOutput.error);
868
1041
  }
869
1042
  span.end();
870
1043
  state.toolSpans.delete(event.toolCall.toolCallId);
@@ -872,6 +1045,15 @@ var OpenTelemetry = class {
872
1045
  onStepFinish(event) {
873
1046
  const state = this.getCallState(event.callId);
874
1047
  if (!(state == null ? void 0 : state.stepSpan)) return;
1048
+ const { telemetry } = state;
1049
+ state.stepSpan.setAttributes(
1050
+ selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1051
+ providerMetadata: {
1052
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
1053
+ },
1054
+ usage: getDetailedUsageAttributes(event.usage)
1055
+ })
1056
+ );
875
1057
  state.stepSpan.end();
876
1058
  state.stepSpan = void 0;
877
1059
  state.stepContext = void 0;
@@ -918,7 +1100,17 @@ var OpenTelemetry = class {
918
1100
  })
919
1101
  );
920
1102
  }
921
- }
1103
+ },
1104
+ ...selectSupplementalAttributes(
1105
+ telemetry,
1106
+ this.supplementalAttributes,
1107
+ {
1108
+ providerMetadata: {
1109
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
1110
+ },
1111
+ usage: getDetailedUsageAttributes(event.totalUsage)
1112
+ }
1113
+ )
922
1114
  })
923
1115
  );
924
1116
  state.rootSpan.end();
@@ -941,7 +1133,17 @@ var OpenTelemetry = class {
941
1133
  finishReason: event.finishReason
942
1134
  })
943
1135
  ) : void 0
944
- }
1136
+ },
1137
+ ...selectSupplementalAttributes(
1138
+ telemetry,
1139
+ this.supplementalAttributes,
1140
+ {
1141
+ providerMetadata: {
1142
+ "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
1143
+ },
1144
+ usage: getDetailedUsageAttributes(event.usage)
1145
+ }
1146
+ )
945
1147
  })
946
1148
  );
947
1149
  state.rootSpan.end();
@@ -951,9 +1153,27 @@ var OpenTelemetry = class {
951
1153
  const state = this.getCallState(event.callId);
952
1154
  if (!(state == null ? void 0 : state.rootSpan)) return;
953
1155
  const { telemetry } = state;
1156
+ const isMany = state.operationId === "ai.embedMany";
954
1157
  state.rootSpan.setAttributes(
955
1158
  selectAttributes(telemetry, {
956
- "gen_ai.usage.input_tokens": event.usage.tokens
1159
+ "gen_ai.usage.input_tokens": event.usage.tokens,
1160
+ ...selectSupplementalAttributes(
1161
+ telemetry,
1162
+ this.supplementalAttributes,
1163
+ {
1164
+ embedding: isMany ? {
1165
+ "ai.embeddings": {
1166
+ output: () => event.embedding.map(
1167
+ (e) => JSON.stringify(e)
1168
+ )
1169
+ }
1170
+ } : {
1171
+ "ai.embedding": {
1172
+ output: () => JSON.stringify(event.embedding)
1173
+ }
1174
+ }
1175
+ }
1176
+ )
957
1177
  })
958
1178
  );
959
1179
  state.rootSpan.end();
@@ -967,7 +1187,15 @@ var OpenTelemetry = class {
967
1187
  const attributes = selectAttributes(telemetry, {
968
1188
  "gen_ai.operation.name": "embeddings",
969
1189
  "gen_ai.provider.name": providerName,
970
- "gen_ai.request.model": state.modelId
1190
+ "gen_ai.request.model": state.modelId,
1191
+ ...state.baseSupplementalAttributes,
1192
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1193
+ embedding: {
1194
+ "ai.values": {
1195
+ input: () => event.values.map((v) => JSON.stringify(v))
1196
+ }
1197
+ }
1198
+ })
971
1199
  });
972
1200
  const spanName = `embeddings ${state.modelId}`;
973
1201
  const embedSpan = this.tracer.startSpan(
@@ -990,7 +1218,18 @@ var OpenTelemetry = class {
990
1218
  const { telemetry } = state;
991
1219
  span.setAttributes(
992
1220
  selectAttributes(telemetry, {
993
- "gen_ai.usage.input_tokens": event.usage.tokens
1221
+ "gen_ai.usage.input_tokens": event.usage.tokens,
1222
+ ...selectSupplementalAttributes(
1223
+ telemetry,
1224
+ this.supplementalAttributes,
1225
+ {
1226
+ embedding: {
1227
+ "ai.embeddings": {
1228
+ output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
1229
+ }
1230
+ }
1231
+ }
1232
+ )
994
1233
  })
995
1234
  );
996
1235
  span.end();
@@ -1002,21 +1241,33 @@ var OpenTelemetry = class {
1002
1241
  recordOutputs: event.recordOutputs,
1003
1242
  functionId: event.functionId
1004
1243
  };
1005
- const settings = {
1006
- maxRetries: event.maxRetries
1007
- };
1008
1244
  const providerName = mapProviderName(event.provider);
1245
+ const baseSupplementalAttributes = selectSupplementalAttributes(
1246
+ telemetry,
1247
+ this.supplementalAttributes,
1248
+ {
1249
+ headers: getHeaderAttributes(event.headers)
1250
+ }
1251
+ );
1009
1252
  const attributes = selectAttributes(telemetry, {
1010
1253
  "gen_ai.operation.name": "rerank",
1011
1254
  "gen_ai.provider.name": providerName,
1012
- "gen_ai.request.model": event.modelId
1255
+ "gen_ai.request.model": event.modelId,
1256
+ ...baseSupplementalAttributes,
1257
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1258
+ reranking: {
1259
+ "ai.documents": {
1260
+ input: () => event.documents.map((d) => JSON.stringify(d))
1261
+ }
1262
+ }
1263
+ })
1013
1264
  });
1014
1265
  const spanName = `rerank ${event.modelId}`;
1015
1266
  const rootSpan = this.tracer.startSpan(spanName, {
1016
1267
  attributes,
1017
1268
  kind: SpanKind.CLIENT
1018
1269
  });
1019
- const rootContext = trace.setSpan(context.active(), rootSpan);
1270
+ const rootContext = trace.setSpan(context2.active(), rootSpan);
1020
1271
  this.callStates.set(event.callId, {
1021
1272
  operationId: event.operationId,
1022
1273
  telemetry,
@@ -1029,9 +1280,10 @@ var OpenTelemetry = class {
1029
1280
  embedSpans: /* @__PURE__ */ new Map(),
1030
1281
  rerankSpan: void 0,
1031
1282
  toolSpans: /* @__PURE__ */ new Map(),
1032
- settings,
1283
+ settings: { maxRetries: event.maxRetries },
1033
1284
  provider: event.provider,
1034
- modelId: event.modelId
1285
+ modelId: event.modelId,
1286
+ baseSupplementalAttributes
1035
1287
  });
1036
1288
  }
1037
1289
  onRerankOperationFinish(event) {
@@ -1048,7 +1300,15 @@ var OpenTelemetry = class {
1048
1300
  const attributes = selectAttributes(telemetry, {
1049
1301
  "gen_ai.operation.name": "rerank",
1050
1302
  "gen_ai.provider.name": providerName,
1051
- "gen_ai.request.model": state.modelId
1303
+ "gen_ai.request.model": state.modelId,
1304
+ ...state.baseSupplementalAttributes,
1305
+ ...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1306
+ reranking: {
1307
+ "ai.documents": {
1308
+ input: () => event.documents.map((d) => JSON.stringify(d))
1309
+ }
1310
+ }
1311
+ })
1052
1312
  });
1053
1313
  const spanName = `rerank ${state.modelId}`;
1054
1314
  const rerankSpan = this.tracer.startSpan(
@@ -1063,6 +1323,17 @@ var OpenTelemetry = class {
1063
1323
  const state = this.getCallState(event.callId);
1064
1324
  if (!(state == null ? void 0 : state.rerankSpan)) return;
1065
1325
  const { span } = state.rerankSpan;
1326
+ const { telemetry } = state;
1327
+ span.setAttributes(
1328
+ selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
1329
+ reranking: {
1330
+ "ai.ranking.type": event.documentsType,
1331
+ "ai.ranking": {
1332
+ output: () => event.ranking.map((r) => JSON.stringify(r))
1333
+ }
1334
+ }
1335
+ })
1336
+ );
1066
1337
  span.end();
1067
1338
  state.rerankSpan = void 0;
1068
1339
  }
@@ -1076,33 +1347,33 @@ var OpenTelemetry = class {
1076
1347
  if (!(state == null ? void 0 : state.rootSpan)) return;
1077
1348
  const actualError = (_a = event.error) != null ? _a : error;
1078
1349
  for (const { span: toolSpan } of state.toolSpans.values()) {
1079
- recordSpanError(toolSpan, actualError);
1350
+ recordErrorOnSpan(toolSpan, actualError);
1080
1351
  toolSpan.end();
1081
1352
  }
1082
1353
  state.toolSpans.clear();
1083
1354
  if (state.inferenceSpan) {
1084
- recordSpanError(state.inferenceSpan, actualError);
1355
+ recordErrorOnSpan(state.inferenceSpan, actualError);
1085
1356
  state.inferenceSpan.end();
1086
1357
  state.inferenceSpan = void 0;
1087
1358
  state.inferenceContext = void 0;
1088
1359
  }
1089
1360
  if (state.stepSpan) {
1090
- recordSpanError(state.stepSpan, actualError);
1361
+ recordErrorOnSpan(state.stepSpan, actualError);
1091
1362
  state.stepSpan.end();
1092
1363
  state.stepSpan = void 0;
1093
1364
  state.stepContext = void 0;
1094
1365
  }
1095
1366
  for (const { span: embedSpan } of state.embedSpans.values()) {
1096
- recordSpanError(embedSpan, actualError);
1367
+ recordErrorOnSpan(embedSpan, actualError);
1097
1368
  embedSpan.end();
1098
1369
  }
1099
1370
  state.embedSpans.clear();
1100
1371
  if (state.rerankSpan) {
1101
- recordSpanError(state.rerankSpan.span, actualError);
1372
+ recordErrorOnSpan(state.rerankSpan.span, actualError);
1102
1373
  state.rerankSpan.span.end();
1103
1374
  state.rerankSpan = void 0;
1104
1375
  }
1105
- recordSpanError(state.rootSpan, actualError);
1376
+ recordErrorOnSpan(state.rootSpan, actualError);
1106
1377
  state.rootSpan.end();
1107
1378
  this.cleanupCallState(event.callId);
1108
1379
  }
@@ -1110,7 +1381,7 @@ var OpenTelemetry = class {
1110
1381
 
1111
1382
  // src/legacy-open-telemetry.ts
1112
1383
  import {
1113
- context as context2,
1384
+ context as context3,
1114
1385
  SpanStatusCode as SpanStatusCode2,
1115
1386
  trace as trace2
1116
1387
  } from "@opentelemetry/api";
@@ -1135,7 +1406,7 @@ function getBaseTelemetryAttributes({
1135
1406
  model,
1136
1407
  settings,
1137
1408
  headers,
1138
- context: context3
1409
+ context: context4
1139
1410
  }) {
1140
1411
  return {
1141
1412
  "ai.model.provider": model.provider,
@@ -1146,7 +1417,7 @@ function getBaseTelemetryAttributes({
1146
1417
  return attributes;
1147
1418
  }, {}),
1148
1419
  // add context as attributes:
1149
- ...Object.entries(context3 != null ? context3 : {}).reduce((attributes, [key, value]) => {
1420
+ ...Object.entries(context4 != null ? context4 : {}).reduce((attributes, [key, value]) => {
1150
1421
  if (value != void 0) {
1151
1422
  attributes[`ai.settings.context.${key}`] = value;
1152
1423
  }
@@ -1171,15 +1442,27 @@ function stringifyForTelemetry(prompt) {
1171
1442
  content: typeof message.content === "string" ? message.content : message.content.map(
1172
1443
  (part) => part.type === "file" ? {
1173
1444
  ...part,
1174
- data: part.data instanceof Uint8Array ? convertDataContentToBase64String2(part.data) : part.data
1445
+ data: serializeFileData(part.data)
1175
1446
  } : part
1176
1447
  )
1177
1448
  }))
1178
1449
  );
1179
1450
  }
1451
+ function serializeFileData(data) {
1452
+ switch (data.type) {
1453
+ case "data":
1454
+ return data.data instanceof Uint8Array ? convertDataContentToBase64String2(data.data) : data.data;
1455
+ case "url":
1456
+ return data.url.toString();
1457
+ case "reference":
1458
+ return data.reference;
1459
+ case "text":
1460
+ return data.text;
1461
+ }
1462
+ }
1180
1463
 
1181
1464
  // src/legacy-open-telemetry.ts
1182
- function recordSpanError2(span, error) {
1465
+ function recordSpanError(span, error) {
1183
1466
  if (error instanceof Error) {
1184
1467
  span.recordException({
1185
1468
  name: error.name,
@@ -1242,7 +1525,7 @@ var LegacyOpenTelemetry = class {
1242
1525
  if (toolSpanEntry == null) {
1243
1526
  return execute();
1244
1527
  }
1245
- return context2.with(toolSpanEntry.context, execute);
1528
+ return context3.with(toolSpanEntry.context, execute);
1246
1529
  }
1247
1530
  onStart(event) {
1248
1531
  if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
@@ -1302,7 +1585,7 @@ var LegacyOpenTelemetry = class {
1302
1585
  }
1303
1586
  });
1304
1587
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
1305
- const rootContext = trace2.setSpan(context2.active(), rootSpan);
1588
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
1306
1589
  this.callStates.set(event.callId, {
1307
1590
  operationId: event.operationId,
1308
1591
  telemetry,
@@ -1358,7 +1641,7 @@ var LegacyOpenTelemetry = class {
1358
1641
  "ai.settings.output": event.output
1359
1642
  });
1360
1643
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
1361
- const rootContext = trace2.setSpan(context2.active(), rootSpan);
1644
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
1362
1645
  this.callStates.set(event.callId, {
1363
1646
  operationId: event.operationId,
1364
1647
  telemetry,
@@ -1484,7 +1767,7 @@ var LegacyOpenTelemetry = class {
1484
1767
  }
1485
1768
  });
1486
1769
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
1487
- const rootContext = trace2.setSpan(context2.active(), rootSpan);
1770
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
1488
1771
  this.callStates.set(event.callId, {
1489
1772
  operationId: event.operationId,
1490
1773
  telemetry,
@@ -1589,7 +1872,7 @@ var LegacyOpenTelemetry = class {
1589
1872
  } catch (e) {
1590
1873
  }
1591
1874
  } else {
1592
- recordSpanError2(span, toolOutput.error);
1875
+ recordSpanError(span, toolOutput.error);
1593
1876
  }
1594
1877
  span.end();
1595
1878
  state.toolSpans.delete(event.toolCall.toolCallId);
@@ -1834,7 +2117,7 @@ var LegacyOpenTelemetry = class {
1834
2117
  }
1835
2118
  });
1836
2119
  const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
1837
- const rootContext = trace2.setSpan(context2.active(), rootSpan);
2120
+ const rootContext = trace2.setSpan(context3.active(), rootSpan);
1838
2121
  this.callStates.set(event.callId, {
1839
2122
  operationId: event.operationId,
1840
2123
  telemetry,
@@ -1922,20 +2205,20 @@ var LegacyOpenTelemetry = class {
1922
2205
  if (!(state == null ? void 0 : state.rootSpan)) return;
1923
2206
  const actualError = (_a = event.error) != null ? _a : error;
1924
2207
  if (state.stepSpan) {
1925
- recordSpanError2(state.stepSpan, actualError);
2208
+ recordSpanError(state.stepSpan, actualError);
1926
2209
  state.stepSpan.end();
1927
2210
  }
1928
2211
  for (const { span: embedSpan } of state.embedSpans.values()) {
1929
- recordSpanError2(embedSpan, actualError);
2212
+ recordSpanError(embedSpan, actualError);
1930
2213
  embedSpan.end();
1931
2214
  }
1932
2215
  state.embedSpans.clear();
1933
2216
  if (state.rerankSpan) {
1934
- recordSpanError2(state.rerankSpan.span, actualError);
2217
+ recordSpanError(state.rerankSpan.span, actualError);
1935
2218
  state.rerankSpan.span.end();
1936
2219
  state.rerankSpan = void 0;
1937
2220
  }
1938
- recordSpanError2(state.rootSpan, actualError);
2221
+ recordSpanError(state.rootSpan, actualError);
1939
2222
  state.rootSpan.end();
1940
2223
  this.cleanupCallState(event.callId);
1941
2224
  }