@ai-sdk/otel 1.0.114 → 1.0.117

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
@@ -35,7 +35,6 @@ function mapProviderName(provider) {
35
35
  return provider;
36
36
  }
37
37
  function mapOperationName(operationId) {
38
- var _a;
39
38
  const mapping = {
40
39
  "ai.generateText": "invoke_agent",
41
40
  "ai.streamText": "invoke_agent",
@@ -45,7 +44,7 @@ function mapOperationName(operationId) {
45
44
  "ai.embedMany": "embeddings",
46
45
  "ai.rerank": "rerank"
47
46
  };
48
- return (_a = mapping[operationId]) != null ? _a : operationId;
47
+ return mapping[operationId] ?? operationId;
49
48
  }
50
49
  function formatSystemInstructions(system) {
51
50
  if (typeof system === "string") {
@@ -57,7 +56,6 @@ function formatSystemInstructions(system) {
57
56
  return [{ type: "text", content: system.content }];
58
57
  }
59
58
  function convertMessagePartToSemConv(part) {
60
- var _a, _b, _c, _d, _e;
61
59
  switch (part.type) {
62
60
  case "text":
63
61
  return { type: "text", content: part.text };
@@ -66,7 +64,7 @@ function convertMessagePartToSemConv(part) {
66
64
  case "tool-call":
67
65
  return {
68
66
  type: "tool_call",
69
- id: (_a = part.toolCallId) != null ? _a : null,
67
+ id: part.toolCallId ?? null,
70
68
  name: part.toolName,
71
69
  arguments: part.input
72
70
  };
@@ -86,7 +84,7 @@ function convertMessagePartToSemConv(part) {
86
84
  }
87
85
  return {
88
86
  type: "tool_call_response",
89
- id: (_b = part.toolCallId) != null ? _b : null,
87
+ id: part.toolCallId ?? null,
90
88
  response
91
89
  };
92
90
  }
@@ -115,7 +113,7 @@ function convertMessagePartToSemConv(part) {
115
113
  return {
116
114
  type: "uri",
117
115
  modality: getModality(part.mediaType),
118
- mime_type: (_c = part.mediaType) != null ? _c : null,
116
+ mime_type: part.mediaType ?? null,
119
117
  uri: data
120
118
  };
121
119
  }
@@ -124,7 +122,7 @@ function convertMessagePartToSemConv(part) {
124
122
  return {
125
123
  type: "uri",
126
124
  modality: getModality(part.mediaType),
127
- mime_type: (_d = part.mediaType) != null ? _d : null,
125
+ mime_type: part.mediaType ?? null,
128
126
  uri: data.toString()
129
127
  };
130
128
  } else {
@@ -133,7 +131,7 @@ function convertMessagePartToSemConv(part) {
133
131
  return {
134
132
  type: "blob",
135
133
  modality: getModality(part.mediaType),
136
- mime_type: (_e = part.mediaType) != null ? _e : null,
134
+ mime_type: part.mediaType ?? null,
137
135
  content
138
136
  };
139
137
  }
@@ -212,7 +210,6 @@ function convertModelMessageToSemConv(msg) {
212
210
  };
213
211
  }
214
212
  const parts = msg.content.map((part) => {
215
- var _a, _b, _c, _d, _e, _f, _g, _h;
216
213
  switch (part.type) {
217
214
  case "text":
218
215
  return { type: "text", content: part.text };
@@ -222,7 +219,7 @@ function convertModelMessageToSemConv(msg) {
222
219
  return {
223
220
  type: "uri",
224
221
  modality: "image",
225
- mime_type: (_a = part.mediaType) != null ? _a : null,
222
+ mime_type: part.mediaType ?? null,
226
223
  uri: data.toString()
227
224
  };
228
225
  }
@@ -231,21 +228,21 @@ function convertModelMessageToSemConv(msg) {
231
228
  return {
232
229
  type: "uri",
233
230
  modality: "image",
234
- mime_type: (_b = part.mediaType) != null ? _b : null,
231
+ mime_type: part.mediaType ?? null,
235
232
  uri: data
236
233
  };
237
234
  }
238
235
  return {
239
236
  type: "blob",
240
237
  modality: "image",
241
- mime_type: (_c = part.mediaType) != null ? _c : null,
238
+ mime_type: part.mediaType ?? null,
242
239
  content: data
243
240
  };
244
241
  }
245
242
  return {
246
243
  type: "blob",
247
244
  modality: "image",
248
- mime_type: (_d = part.mediaType) != null ? _d : null,
245
+ mime_type: part.mediaType ?? null,
249
246
  content: convertDataContentToBase64String(data)
250
247
  };
251
248
  }
@@ -270,7 +267,7 @@ function convertModelMessageToSemConv(msg) {
270
267
  return {
271
268
  type: "uri",
272
269
  modality: getModality(part.mediaType),
273
- mime_type: (_e = part.mediaType) != null ? _e : null,
270
+ mime_type: part.mediaType ?? null,
274
271
  uri: data.toString()
275
272
  };
276
273
  }
@@ -279,21 +276,21 @@ function convertModelMessageToSemConv(msg) {
279
276
  return {
280
277
  type: "uri",
281
278
  modality: getModality(part.mediaType),
282
- mime_type: (_f = part.mediaType) != null ? _f : null,
279
+ mime_type: part.mediaType ?? null,
283
280
  uri: data
284
281
  };
285
282
  }
286
283
  return {
287
284
  type: "blob",
288
285
  modality: getModality(part.mediaType),
289
- mime_type: (_g = part.mediaType) != null ? _g : null,
286
+ mime_type: part.mediaType ?? null,
290
287
  content: data
291
288
  };
292
289
  }
293
290
  return {
294
291
  type: "blob",
295
292
  modality: getModality(part.mediaType),
296
- mime_type: (_h = part.mediaType) != null ? _h : null,
293
+ mime_type: part.mediaType ?? null,
297
294
  content: convertDataContentToBase64String(data)
298
295
  };
299
296
  }
@@ -311,7 +308,6 @@ function convertModelMessageToSemConv(msg) {
311
308
  };
312
309
  }
313
310
  const parts = msg.content.map((part) => {
314
- var _a, _b;
315
311
  switch (part.type) {
316
312
  case "text":
317
313
  return { type: "text", content: part.text };
@@ -320,7 +316,7 @@ function convertModelMessageToSemConv(msg) {
320
316
  case "tool-call":
321
317
  return {
322
318
  type: "tool_call",
323
- id: (_a = part.toolCallId) != null ? _a : null,
319
+ id: part.toolCallId ?? null,
324
320
  name: part.toolName,
325
321
  arguments: part.input
326
322
  };
@@ -340,7 +336,7 @@ function convertModelMessageToSemConv(msg) {
340
336
  }
341
337
  return {
342
338
  type: "tool_call_response",
343
- id: (_b = part.toolCallId) != null ? _b : null,
339
+ id: part.toolCallId ?? null,
344
340
  response
345
341
  };
346
342
  }
@@ -352,7 +348,6 @@ function convertModelMessageToSemConv(msg) {
352
348
  }
353
349
  if (msg.role === "tool") {
354
350
  const parts = msg.content.map((part) => {
355
- var _a;
356
351
  if (part.type === "tool-result") {
357
352
  const output = part.output;
358
353
  let response;
@@ -369,7 +364,7 @@ function convertModelMessageToSemConv(msg) {
369
364
  }
370
365
  return {
371
366
  type: "tool_call_response",
372
- id: (_a = part.toolCallId) != null ? _a : null,
367
+ id: part.toolCallId ?? null,
373
368
  response
374
369
  };
375
370
  }
@@ -449,7 +444,6 @@ function formatObjectOutputMessages({
449
444
  ];
450
445
  }
451
446
  function mapFinishReason(reason) {
452
- var _a;
453
447
  const mapping = {
454
448
  stop: "stop",
455
449
  length: "length",
@@ -459,7 +453,7 @@ function mapFinishReason(reason) {
459
453
  other: "stop",
460
454
  unknown: "stop"
461
455
  };
462
- return (_a = mapping[reason]) != null ? _a : reason;
456
+ return mapping[reason] ?? reason;
463
457
  }
464
458
 
465
459
  // src/record-span.ts
@@ -520,7 +514,7 @@ function sanitizeAttributeValue(value) {
520
514
  }
521
515
  function sanitizeAttributes(attributes) {
522
516
  const result = {};
523
- for (const [key, value] of Object.entries(attributes != null ? attributes : {})) {
517
+ for (const [key, value] of Object.entries(attributes ?? {})) {
524
518
  if (value == null) continue;
525
519
  const sanitized = sanitizeAttributeValue(value);
526
520
  if (sanitized != null) result[key] = sanitized;
@@ -530,7 +524,7 @@ function sanitizeAttributes(attributes) {
530
524
 
531
525
  // src/select-attributes.ts
532
526
  function shouldRecord(telemetry) {
533
- return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
527
+ return telemetry?.isEnabled !== false;
534
528
  }
535
529
  function selectAttributes(telemetry, attributes) {
536
530
  if (!shouldRecord(telemetry)) {
@@ -540,7 +534,7 @@ function selectAttributes(telemetry, attributes) {
540
534
  for (const [key, value] of Object.entries(attributes)) {
541
535
  if (value == null) continue;
542
536
  if (typeof value === "object" && "input" in value && typeof value.input === "function") {
543
- if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
537
+ if (telemetry?.recordInputs === false) continue;
544
538
  const resolved = value.input();
545
539
  if (resolved != null) {
546
540
  const sanitized2 = sanitizeAttributeValue(resolved);
@@ -549,7 +543,7 @@ function selectAttributes(telemetry, attributes) {
549
543
  continue;
550
544
  }
551
545
  if (typeof value === "object" && "output" in value && typeof value.output === "function") {
552
- if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
546
+ if (telemetry?.recordOutputs === false) continue;
553
547
  const resolved = value.output();
554
548
  if (resolved != null) {
555
549
  const sanitized2 = sanitizeAttributeValue(resolved);
@@ -576,23 +570,22 @@ var disabledSupplementalAttributes = {
576
570
  schema: false
577
571
  };
578
572
  function normalizeSupplementalAttributes(options) {
579
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
580
573
  return {
581
574
  ...disabledSupplementalAttributes,
582
- usage: (_a = options.usage) != null ? _a : false,
583
- providerMetadata: (_b = options.providerMetadata) != null ? _b : false,
584
- embedding: (_c = options.embedding) != null ? _c : false,
585
- reranking: (_d = options.reranking) != null ? _d : false,
586
- experimental_evaluation: (_e = options.experimental_evaluation) != null ? _e : false,
587
- runtimeContext: (_f = options.runtimeContext) != null ? _f : false,
588
- headers: (_g = options.headers) != null ? _g : false,
589
- toolChoice: (_h = options.toolChoice) != null ? _h : false,
590
- schema: (_i = options.schema) != null ? _i : false
575
+ usage: options.usage ?? false,
576
+ providerMetadata: options.providerMetadata ?? false,
577
+ embedding: options.embedding ?? false,
578
+ reranking: options.reranking ?? false,
579
+ experimental_evaluation: options.experimental_evaluation ?? false,
580
+ runtimeContext: options.runtimeContext ?? false,
581
+ headers: options.headers ?? false,
582
+ toolChoice: options.toolChoice ?? false,
583
+ schema: options.schema ?? false
591
584
  };
592
585
  }
593
586
  function getRuntimeContextAttributes(context4) {
594
587
  const attributes = {};
595
- for (const [key, value] of Object.entries(context4 != null ? context4 : {})) {
588
+ for (const [key, value] of Object.entries(context4 ?? {})) {
596
589
  addRuntimeContextAttribute(attributes, `ai.settings.context.${key}`, value);
597
590
  }
598
591
  return attributes;
@@ -611,15 +604,14 @@ function addRuntimeContextAttribute(attributes, key, value) {
611
604
  }
612
605
  function getHeaderAttributes(headers) {
613
606
  return Object.fromEntries(
614
- Object.entries(headers != null ? headers : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.request.headers.${key}`, value])
607
+ Object.entries(headers ?? {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.request.headers.${key}`, value])
615
608
  );
616
609
  }
617
610
  function getDetailedUsageAttributes(usage) {
618
- var _a, _b, _c;
619
611
  return {
620
- "ai.usage.inputTokenDetails.noCacheTokens": (_a = usage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
621
- "ai.usage.outputTokenDetails.textTokens": (_b = usage.outputTokenDetails) == null ? void 0 : _b.textTokens,
622
- "ai.usage.outputTokenDetails.reasoningTokens": (_c = usage.outputTokenDetails) == null ? void 0 : _c.reasoningTokens
612
+ "ai.usage.inputTokenDetails.noCacheTokens": usage.inputTokenDetails?.noCacheTokens,
613
+ "ai.usage.outputTokenDetails.textTokens": usage.outputTokenDetails?.textTokens,
614
+ "ai.usage.outputTokenDetails.reasoningTokens": usage.outputTokenDetails?.reasoningTokens
623
615
  };
624
616
  }
625
617
  function selectSupplementalAttributes(telemetry, enabledAttributes, attributes) {
@@ -638,22 +630,20 @@ function msToSeconds(durationMs) {
638
630
  return durationMs == null ? void 0 : durationMs / 1e3;
639
631
  }
640
632
  function getGenAIClientPerformanceAttributes(performance) {
641
- var _a;
642
633
  return {
643
634
  "gen_ai.client.operation.duration": msToSeconds(performance.responseTimeMs),
644
635
  "gen_ai.client.operation.time_to_first_chunk": msToSeconds(
645
636
  performance.timeToFirstOutputMs
646
637
  ),
647
638
  "gen_ai.client.operation.time_per_output_chunk": msToSeconds(
648
- (_a = performance.timeBetweenOutputChunksMs) == null ? void 0 : _a.avg
639
+ performance.timeBetweenOutputChunksMs?.avg
649
640
  )
650
641
  };
651
642
  }
652
643
  var OpenTelemetry = class {
653
644
  constructor(options = {}) {
654
645
  this.callStates = /* @__PURE__ */ new Map();
655
- var _a;
656
- this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
646
+ this.tracer = options.tracer ?? trace.getTracer("gen_ai");
657
647
  this.supplementalAttributes = normalizeSupplementalAttributes(options);
658
648
  this.enrichSpan = options.enrichSpan;
659
649
  }
@@ -670,16 +660,15 @@ var OpenTelemetry = class {
670
660
  callId,
671
661
  runtimeContext
672
662
  }) {
673
- var _a;
674
663
  let customAttributes;
675
664
  try {
676
- customAttributes = (_a = this.enrichSpan) == null ? void 0 : _a.call(this, {
665
+ customAttributes = this.enrichSpan?.({
677
666
  spanType,
678
667
  operationId,
679
668
  callId,
680
669
  runtimeContext
681
670
  });
682
- } catch (e) {
671
+ } catch {
683
672
  customAttributes = void 0;
684
673
  }
685
674
  return {
@@ -692,8 +681,7 @@ var OpenTelemetry = class {
692
681
  toolCallId,
693
682
  execute
694
683
  }) {
695
- var _a;
696
- const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
684
+ const toolSpanEntry = this.getCallState(callId)?.toolSpans.get(toolCallId);
697
685
  if (toolSpanEntry == null) {
698
686
  return execute();
699
687
  }
@@ -707,9 +695,8 @@ var OpenTelemetry = class {
707
695
  callId,
708
696
  execute
709
697
  }) {
710
- var _a;
711
698
  const state = this.getCallState(callId);
712
- const modelCallContext = (_a = state == null ? void 0 : state.inferenceContext) != null ? _a : state == null ? void 0 : state.stepContext;
699
+ const modelCallContext = state?.inferenceContext ?? state?.stepContext;
713
700
  if (modelCallContext == null) {
714
701
  return execute();
715
702
  }
@@ -735,7 +722,6 @@ var OpenTelemetry = class {
735
722
  this.onGenerateStart(event);
736
723
  }
737
724
  onGenerateStart(event) {
738
- var _a;
739
725
  const telemetry = {
740
726
  recordInputs: event.recordInputs,
741
727
  recordOutputs: event.recordOutputs,
@@ -771,7 +757,7 @@ var OpenTelemetry = class {
771
757
  "gen_ai.request.frequency_penalty": event.frequencyPenalty,
772
758
  "gen_ai.request.max_tokens": event.maxOutputTokens,
773
759
  "gen_ai.request.presence_penalty": event.presencePenalty,
774
- "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
760
+ "gen_ai.request.temperature": event.temperature ?? void 0,
775
761
  "gen_ai.request.top_k": event.topK,
776
762
  "gen_ai.request.top_p": event.topP,
777
763
  "gen_ai.request.stop_sequences": event.stopSequences,
@@ -822,7 +808,6 @@ var OpenTelemetry = class {
822
808
  });
823
809
  }
824
810
  onObjectOperationStart(event) {
825
- var _a;
826
811
  const telemetry = {
827
812
  recordInputs: event.recordInputs,
828
813
  recordOutputs: event.recordOutputs,
@@ -856,7 +841,7 @@ var OpenTelemetry = class {
856
841
  "gen_ai.request.frequency_penalty": event.frequencyPenalty,
857
842
  "gen_ai.request.max_tokens": event.maxOutputTokens,
858
843
  "gen_ai.request.presence_penalty": event.presencePenalty,
859
- "gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
844
+ "gen_ai.request.temperature": event.temperature ?? void 0,
860
845
  "gen_ai.request.top_k": event.topK,
861
846
  "gen_ai.request.top_p": event.topP,
862
847
  "gen_ai.request.seed": event.seed,
@@ -916,13 +901,12 @@ var OpenTelemetry = class {
916
901
  }
917
902
  /** @deprecated */
918
903
  onObjectStepStart(event) {
919
- var _a, _b;
920
904
  const state = this.getCallState(event.callId);
921
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
905
+ if (!state?.rootSpan || !state.rootContext) return;
922
906
  const { telemetry } = state;
923
907
  const providerName = mapProviderName(event.provider);
924
908
  const systemInstructionCount = state.objectSystemInstructions == null ? 0 : formatSystemInstructions(state.objectSystemInstructions).length;
925
- const inputMessages = (_a = event.promptMessages) == null ? void 0 : _a.slice(systemInstructionCount);
909
+ const inputMessages = event.promptMessages?.slice(systemInstructionCount);
926
910
  const attributes = selectAttributes(telemetry, {
927
911
  "gen_ai.operation.name": "chat",
928
912
  "gen_ai.provider.name": providerName,
@@ -931,7 +915,7 @@ var OpenTelemetry = class {
931
915
  "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
932
916
  "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
933
917
  "gen_ai.request.presence_penalty": state.settings.presencePenalty,
934
- "gen_ai.request.temperature": (_b = state.settings.temperature) != null ? _b : void 0,
918
+ "gen_ai.request.temperature": state.settings.temperature ?? void 0,
935
919
  "gen_ai.request.top_k": state.settings.topK,
936
920
  "gen_ai.request.top_p": state.settings.topP,
937
921
  "gen_ai.system_instructions": state.objectSystemInstructions != null ? {
@@ -966,9 +950,8 @@ var OpenTelemetry = class {
966
950
  }
967
951
  /** @deprecated */
968
952
  onObjectStepEnd(event) {
969
- var _a;
970
953
  const state = this.getCallState(event.callId);
971
- if (!(state == null ? void 0 : state.inferenceSpan)) return;
954
+ if (!state?.inferenceSpan) return;
972
955
  const { telemetry } = state;
973
956
  state.inferenceSpan.setAttributes(
974
957
  selectAttributes(telemetry, {
@@ -977,7 +960,7 @@ var OpenTelemetry = class {
977
960
  "gen_ai.response.model": event.response.modelId,
978
961
  "gen_ai.usage.input_tokens": event.usage.inputTokens,
979
962
  "gen_ai.usage.output_tokens": event.usage.outputTokens,
980
- "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
963
+ "gen_ai.usage.cache_read.input_tokens": event.usage.inputTokenDetails?.cacheReadTokens,
981
964
  "gen_ai.output.messages": {
982
965
  output: () => {
983
966
  try {
@@ -987,7 +970,7 @@ var OpenTelemetry = class {
987
970
  finishReason: event.finishReason
988
971
  })
989
972
  );
990
- } catch (e) {
973
+ } catch {
991
974
  return event.objectText;
992
975
  }
993
976
  }
@@ -1078,7 +1061,7 @@ var OpenTelemetry = class {
1078
1061
  }
1079
1062
  onStepStart(event) {
1080
1063
  const state = this.getCallState(event.callId);
1081
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1064
+ if (!state?.rootSpan || !state.rootContext) return;
1082
1065
  const { telemetry } = state;
1083
1066
  state.runtimeContext = event.runtimeContext;
1084
1067
  const stepAttributes = selectAttributes(telemetry, {
@@ -1109,9 +1092,8 @@ var OpenTelemetry = class {
1109
1092
  state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
1110
1093
  }
1111
1094
  onLanguageModelCallStart(event) {
1112
- var _a;
1113
1095
  const state = this.getCallState(event.callId);
1114
- if (!(state == null ? void 0 : state.stepContext)) return;
1096
+ if (!state?.stepContext) return;
1115
1097
  const { telemetry } = state;
1116
1098
  const providerName = mapProviderName(event.provider);
1117
1099
  state.inferenceToolDefinitions = event.tools;
@@ -1123,7 +1105,7 @@ var OpenTelemetry = class {
1123
1105
  "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
1124
1106
  "gen_ai.request.presence_penalty": state.settings.presencePenalty,
1125
1107
  "gen_ai.request.stop_sequences": state.settings.stopSequences,
1126
- "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
1108
+ "gen_ai.request.temperature": state.settings.temperature ?? void 0,
1127
1109
  "gen_ai.request.top_k": state.settings.topK,
1128
1110
  "gen_ai.request.top_p": state.settings.topP,
1129
1111
  "gen_ai.system_instructions": event.instructions != null ? {
@@ -1162,12 +1144,11 @@ var OpenTelemetry = class {
1162
1144
  );
1163
1145
  }
1164
1146
  onLanguageModelCallEnd(event) {
1165
- var _a, _b, _c;
1166
1147
  const state = this.getCallState(event.callId);
1167
- if (!(state == null ? void 0 : state.inferenceSpan)) return;
1148
+ if (!state?.inferenceSpan) return;
1168
1149
  const { telemetry } = state;
1169
1150
  const providerName = mapProviderName(event.provider);
1170
- const toolDefinitions = [...(_a = state.inferenceToolDefinitions) != null ? _a : []];
1151
+ const toolDefinitions = [...state.inferenceToolDefinitions ?? []];
1171
1152
  const definedToolNames = new Set(
1172
1153
  toolDefinitions.flatMap(
1173
1154
  (tool) => typeof tool.name === "string" ? [tool.name] : []
@@ -1194,8 +1175,8 @@ var OpenTelemetry = class {
1194
1175
  "gen_ai.response.model": event.modelId,
1195
1176
  "gen_ai.usage.input_tokens": event.usage.inputTokens,
1196
1177
  "gen_ai.usage.output_tokens": event.usage.outputTokens,
1197
- "gen_ai.usage.cache_read.input_tokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
1198
- "gen_ai.usage.cache_creation.input_tokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
1178
+ "gen_ai.usage.cache_read.input_tokens": event.usage.inputTokenDetails?.cacheReadTokens,
1179
+ "gen_ai.usage.cache_creation.input_tokens": event.usage.inputTokenDetails?.cacheWriteTokens,
1199
1180
  "gen_ai.output.messages": {
1200
1181
  output: () => JSON.stringify(
1201
1182
  formatOutputMessages({
@@ -1260,7 +1241,7 @@ var OpenTelemetry = class {
1260
1241
  state.inferenceContext
1261
1242
  );
1262
1243
  const toolOutput = finalToolOutputs.get(part.toolCallId);
1263
- if ((toolOutput == null ? void 0 : toolOutput.type) === "tool-result") {
1244
+ if (toolOutput?.type === "tool-result") {
1264
1245
  try {
1265
1246
  toolSpan.setAttributes(
1266
1247
  selectAttributes(telemetry, {
@@ -1269,9 +1250,9 @@ var OpenTelemetry = class {
1269
1250
  }
1270
1251
  })
1271
1252
  );
1272
- } catch (e) {
1253
+ } catch {
1273
1254
  }
1274
- } else if ((toolOutput == null ? void 0 : toolOutput.type) === "tool-error") {
1255
+ } else if (toolOutput?.type === "tool-error") {
1275
1256
  recordErrorOnSpan(toolSpan, toolOutput.error);
1276
1257
  }
1277
1258
  toolSpan.end();
@@ -1282,9 +1263,8 @@ var OpenTelemetry = class {
1282
1263
  state.inferenceToolDefinitions = void 0;
1283
1264
  }
1284
1265
  onToolExecutionStart(event) {
1285
- var _a;
1286
1266
  const state = this.getCallState(event.callId);
1287
- const parentContext = (_a = state == null ? void 0 : state.stepContext) != null ? _a : state == null ? void 0 : state.rootContext;
1267
+ const parentContext = state?.stepContext ?? state?.rootContext;
1288
1268
  if (!state || !parentContext) return;
1289
1269
  const { telemetry } = state;
1290
1270
  const { toolCall } = event;
@@ -1340,7 +1320,7 @@ var OpenTelemetry = class {
1340
1320
  }
1341
1321
  })
1342
1322
  );
1343
- } catch (e) {
1323
+ } catch {
1344
1324
  }
1345
1325
  } else {
1346
1326
  recordErrorOnSpan(span, toolOutput.error);
@@ -1350,7 +1330,7 @@ var OpenTelemetry = class {
1350
1330
  }
1351
1331
  onStepEnd(event) {
1352
1332
  const state = this.getCallState(event.callId);
1353
- if (!(state == null ? void 0 : state.stepSpan)) return;
1333
+ if (!state?.stepSpan) return;
1354
1334
  const { telemetry } = state;
1355
1335
  state.stepSpan.setAttributes(
1356
1336
  selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
@@ -1370,7 +1350,7 @@ var OpenTelemetry = class {
1370
1350
  }
1371
1351
  onEnd(event) {
1372
1352
  const state = this.getCallState(event.callId);
1373
- if (!(state == null ? void 0 : state.rootSpan)) return;
1353
+ if (!state?.rootSpan) return;
1374
1354
  if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
1375
1355
  this.onEmbedOperationEnd(event);
1376
1356
  return;
@@ -1386,31 +1366,27 @@ var OpenTelemetry = class {
1386
1366
  this.onGenerateEnd(event);
1387
1367
  }
1388
1368
  onGenerateEnd(event) {
1389
- var _a, _b;
1390
1369
  const state = this.getCallState(event.callId);
1391
- if (!(state == null ? void 0 : state.rootSpan)) return;
1370
+ if (!state?.rootSpan) return;
1392
1371
  const { telemetry } = state;
1393
1372
  state.rootSpan.setAttributes(
1394
1373
  selectAttributes(telemetry, {
1395
1374
  "gen_ai.response.finish_reasons": [event.finishReason],
1396
1375
  "gen_ai.usage.input_tokens": event.usage.inputTokens,
1397
1376
  "gen_ai.usage.output_tokens": event.usage.outputTokens,
1398
- "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
1399
- "gen_ai.usage.cache_creation.input_tokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheWriteTokens,
1377
+ "gen_ai.usage.cache_read.input_tokens": event.usage.inputTokenDetails?.cacheReadTokens,
1378
+ "gen_ai.usage.cache_creation.input_tokens": event.usage.inputTokenDetails?.cacheWriteTokens,
1400
1379
  "gen_ai.output.messages": {
1401
- output: () => {
1402
- var _a2;
1403
- return JSON.stringify(
1404
- formatOutputMessages({
1405
- text: (_a2 = event.text) != null ? _a2 : void 0,
1406
- reasoning: event.finalStep.reasoning,
1407
- toolCalls: event.toolCalls,
1408
- toolResults: event.toolResults,
1409
- files: event.files,
1410
- finishReason: event.finishReason
1411
- })
1412
- );
1413
- }
1380
+ output: () => JSON.stringify(
1381
+ formatOutputMessages({
1382
+ text: event.text ?? void 0,
1383
+ reasoning: event.finalStep.reasoning,
1384
+ toolCalls: event.toolCalls,
1385
+ toolResults: event.toolResults,
1386
+ files: event.files,
1387
+ finishReason: event.finishReason
1388
+ })
1389
+ )
1414
1390
  },
1415
1391
  ...selectSupplementalAttributes(
1416
1392
  telemetry,
@@ -1428,16 +1404,15 @@ var OpenTelemetry = class {
1428
1404
  this.cleanupCallState(event.callId);
1429
1405
  }
1430
1406
  onObjectOperationEnd(event) {
1431
- var _a;
1432
1407
  const state = this.getCallState(event.callId);
1433
- if (!(state == null ? void 0 : state.rootSpan)) return;
1408
+ if (!state?.rootSpan) return;
1434
1409
  const { telemetry } = state;
1435
1410
  state.rootSpan.setAttributes(
1436
1411
  selectAttributes(telemetry, {
1437
1412
  "gen_ai.response.finish_reasons": [event.finishReason],
1438
1413
  "gen_ai.usage.input_tokens": event.usage.inputTokens,
1439
1414
  "gen_ai.usage.output_tokens": event.usage.outputTokens,
1440
- "gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
1415
+ "gen_ai.usage.cache_read.input_tokens": event.usage.inputTokenDetails?.cacheReadTokens,
1441
1416
  "gen_ai.output.messages": {
1442
1417
  output: () => event.object != null ? JSON.stringify(
1443
1418
  formatObjectOutputMessages({
@@ -1463,7 +1438,7 @@ var OpenTelemetry = class {
1463
1438
  }
1464
1439
  onEmbedOperationEnd(event) {
1465
1440
  const state = this.getCallState(event.callId);
1466
- if (!(state == null ? void 0 : state.rootSpan)) return;
1441
+ if (!state?.rootSpan) return;
1467
1442
  const { telemetry } = state;
1468
1443
  const isMany = state.operationId === "ai.embedMany";
1469
1444
  state.rootSpan.setAttributes(
@@ -1492,7 +1467,7 @@ var OpenTelemetry = class {
1492
1467
  }
1493
1468
  onEmbedStart(event) {
1494
1469
  const state = this.getCallState(event.callId);
1495
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1470
+ if (!state?.rootSpan || !state.rootContext) return;
1496
1471
  const { telemetry } = state;
1497
1472
  const providerName = mapProviderName(state.provider);
1498
1473
  const attributes = selectAttributes(telemetry, {
@@ -1618,13 +1593,13 @@ var OpenTelemetry = class {
1618
1593
  }
1619
1594
  onRerankOperationEnd(event) {
1620
1595
  const state = this.getCallState(event.callId);
1621
- if (!(state == null ? void 0 : state.rootSpan)) return;
1596
+ if (!state?.rootSpan) return;
1622
1597
  state.rootSpan.end();
1623
1598
  this.cleanupCallState(event.callId);
1624
1599
  }
1625
1600
  onRerankStart(event) {
1626
1601
  const state = this.getCallState(event.callId);
1627
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1602
+ if (!state?.rootSpan || !state.rootContext) return;
1628
1603
  const { telemetry } = state;
1629
1604
  const providerName = mapProviderName(state.provider);
1630
1605
  const attributes = selectAttributes(telemetry, {
@@ -1660,7 +1635,7 @@ var OpenTelemetry = class {
1660
1635
  }
1661
1636
  onRerankEnd(event) {
1662
1637
  const state = this.getCallState(event.callId);
1663
- if (!(state == null ? void 0 : state.rerankSpan)) return;
1638
+ if (!state?.rerankSpan) return;
1664
1639
  const { span } = state.rerankSpan;
1665
1640
  const { telemetry } = state;
1666
1641
  span.setAttributes(
@@ -1740,7 +1715,7 @@ var OpenTelemetry = class {
1740
1715
  }
1741
1716
  onEvaluateOperationEnd(event) {
1742
1717
  const state = this.getCallState(event.callId);
1743
- if (!(state == null ? void 0 : state.rootSpan)) return;
1718
+ if (!state?.rootSpan) return;
1744
1719
  state.rootSpan.setAttributes(
1745
1720
  selectSupplementalAttributes(
1746
1721
  state.telemetry,
@@ -1765,7 +1740,7 @@ var OpenTelemetry = class {
1765
1740
  }
1766
1741
  experimental_onEvaluationModelCallStart(event) {
1767
1742
  const state = this.getCallState(event.callId);
1768
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
1743
+ if (!state?.rootSpan || !state.rootContext) return;
1769
1744
  const attributes = selectAttributes(state.telemetry, {
1770
1745
  "gen_ai.operation.name": "evaluate",
1771
1746
  "gen_ai.provider.name": mapProviderName(state.provider),
@@ -1806,13 +1781,12 @@ var OpenTelemetry = class {
1806
1781
  };
1807
1782
  }
1808
1783
  experimental_onEvaluationModelCallEnd(event) {
1809
- var _a, _b;
1810
1784
  const state = this.getCallState(event.callId);
1811
- if (!(state == null ? void 0 : state.evaluationSpan)) return;
1785
+ if (!state?.evaluationSpan) return;
1812
1786
  state.evaluationSpan.span.setAttributes(
1813
1787
  selectAttributes(state.telemetry, {
1814
- "gen_ai.usage.input_tokens": (_a = event.usage) == null ? void 0 : _a.inputTokens,
1815
- "gen_ai.usage.output_tokens": (_b = event.usage) == null ? void 0 : _b.outputTokens,
1788
+ "gen_ai.usage.input_tokens": event.usage?.inputTokens,
1789
+ "gen_ai.usage.output_tokens": event.usage?.outputTokens,
1816
1790
  ...selectSupplementalAttributes(
1817
1791
  state.telemetry,
1818
1792
  this.supplementalAttributes,
@@ -1834,7 +1808,7 @@ var OpenTelemetry = class {
1834
1808
  }
1835
1809
  onAbort(event) {
1836
1810
  const state = this.getCallState(event.callId);
1837
- if (!(state == null ? void 0 : state.rootSpan)) return;
1811
+ if (!state?.rootSpan) return;
1838
1812
  for (const { span: toolSpan } of state.toolSpans.values()) {
1839
1813
  toolSpan.end();
1840
1814
  }
@@ -1865,12 +1839,11 @@ var OpenTelemetry = class {
1865
1839
  this.cleanupCallState(event.callId);
1866
1840
  }
1867
1841
  onError(error) {
1868
- var _a;
1869
1842
  const event = error;
1870
- if (!(event == null ? void 0 : event.callId)) return;
1843
+ if (!event?.callId) return;
1871
1844
  const state = this.getCallState(event.callId);
1872
- if (!(state == null ? void 0 : state.rootSpan)) return;
1873
- const actualError = (_a = event.error) != null ? _a : error;
1845
+ if (!state?.rootSpan) return;
1846
+ const actualError = event.error ?? error;
1874
1847
  for (const { span: toolSpan } of state.toolSpans.values()) {
1875
1848
  recordErrorOnSpan(toolSpan, actualError);
1876
1849
  toolSpan.end();
@@ -1923,11 +1896,11 @@ function assembleOperationName({
1923
1896
  }) {
1924
1897
  return {
1925
1898
  // standardized operation and resource name:
1926
- "operation.name": `${operationId}${(telemetry == null ? void 0 : telemetry.functionId) != null ? ` ${telemetry.functionId}` : ""}`,
1927
- "resource.name": telemetry == null ? void 0 : telemetry.functionId,
1899
+ "operation.name": `${operationId}${telemetry?.functionId != null ? ` ${telemetry.functionId}` : ""}`,
1900
+ "resource.name": telemetry?.functionId,
1928
1901
  // detailed, AI SDK specific data:
1929
1902
  "ai.operationId": operationId,
1930
- "ai.telemetry.functionId": telemetry == null ? void 0 : telemetry.functionId
1903
+ "ai.telemetry.functionId": telemetry?.functionId
1931
1904
  };
1932
1905
  }
1933
1906
 
@@ -1949,7 +1922,7 @@ function getBaseTelemetryAttributes({
1949
1922
  // add context as attributes:
1950
1923
  ...getRuntimeContextAttributes(context4),
1951
1924
  // request headers
1952
- ...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
1925
+ ...Object.entries(headers ?? {}).reduce((attributes, [key, value]) => {
1953
1926
  if (value !== void 0) {
1954
1927
  attributes[`ai.request.headers.${key}`] = value;
1955
1928
  }
@@ -2003,7 +1976,7 @@ function recordSpanError(span, error) {
2003
1976
  }
2004
1977
  }
2005
1978
  function shouldRecord2(telemetry) {
2006
- return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
1979
+ return telemetry?.isEnabled !== false;
2007
1980
  }
2008
1981
  function selectAttributes2(telemetry, attributes) {
2009
1982
  if (!shouldRecord2(telemetry)) {
@@ -2013,7 +1986,7 @@ function selectAttributes2(telemetry, attributes) {
2013
1986
  for (const [key, value] of Object.entries(attributes)) {
2014
1987
  if (value == null) continue;
2015
1988
  if (typeof value === "object" && "input" in value && typeof value.input === "function") {
2016
- if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
1989
+ if (telemetry?.recordInputs === false) continue;
2017
1990
  const resolved = value.input();
2018
1991
  if (resolved != null) {
2019
1992
  const sanitized2 = sanitizeAttributeValue(resolved);
@@ -2022,7 +1995,7 @@ function selectAttributes2(telemetry, attributes) {
2022
1995
  continue;
2023
1996
  }
2024
1997
  if (typeof value === "object" && "output" in value && typeof value.output === "function") {
2025
- if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
1998
+ if (telemetry?.recordOutputs === false) continue;
2026
1999
  const resolved = value.output();
2027
2000
  if (resolved != null) {
2028
2001
  const sanitized2 = sanitizeAttributeValue(resolved);
@@ -2038,8 +2011,7 @@ function selectAttributes2(telemetry, attributes) {
2038
2011
  var LegacyOpenTelemetry = class {
2039
2012
  constructor(options = {}) {
2040
2013
  this.callStates = /* @__PURE__ */ new Map();
2041
- var _a;
2042
- this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
2014
+ this.tracer = options.tracer ?? trace2.getTracer("ai");
2043
2015
  }
2044
2016
  getCallState(callId) {
2045
2017
  return this.callStates.get(callId);
@@ -2052,8 +2024,7 @@ var LegacyOpenTelemetry = class {
2052
2024
  toolCallId,
2053
2025
  execute
2054
2026
  }) {
2055
- var _a;
2056
- const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
2027
+ const toolSpanEntry = this.getCallState(callId)?.toolSpans.get(toolCallId);
2057
2028
  if (toolSpanEntry == null) {
2058
2029
  return execute();
2059
2030
  }
@@ -2067,8 +2038,7 @@ var LegacyOpenTelemetry = class {
2067
2038
  callId,
2068
2039
  execute
2069
2040
  }) {
2070
- var _a;
2071
- const stepContext = (_a = this.getCallState(callId)) == null ? void 0 : _a.stepContext;
2041
+ const stepContext = this.getCallState(callId)?.stepContext;
2072
2042
  if (stepContext == null) {
2073
2043
  return execute();
2074
2044
  }
@@ -2207,9 +2177,8 @@ var LegacyOpenTelemetry = class {
2207
2177
  }
2208
2178
  /** @deprecated */
2209
2179
  onObjectStepStart(event) {
2210
- var _a;
2211
2180
  const state = this.getCallState(event.callId);
2212
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2181
+ if (!state?.rootSpan || !state.rootContext) return;
2213
2182
  const { telemetry } = state;
2214
2183
  const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
2215
2184
  const attributes = selectAttributes2(telemetry, {
@@ -2226,7 +2195,7 @@ var LegacyOpenTelemetry = class {
2226
2195
  "gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
2227
2196
  "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
2228
2197
  "gen_ai.request.presence_penalty": state.settings.presencePenalty,
2229
- "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
2198
+ "gen_ai.request.temperature": state.settings.temperature ?? void 0,
2230
2199
  "gen_ai.request.top_k": state.settings.topK,
2231
2200
  "gen_ai.request.top_p": state.settings.topP
2232
2201
  });
@@ -2239,9 +2208,8 @@ var LegacyOpenTelemetry = class {
2239
2208
  }
2240
2209
  /** @deprecated */
2241
2210
  onObjectStepEnd(event) {
2242
- var _a, _b;
2243
2211
  const state = this.getCallState(event.callId);
2244
- if (!(state == null ? void 0 : state.stepSpan)) return;
2212
+ if (!state?.stepSpan) return;
2245
2213
  const { telemetry } = state;
2246
2214
  state.stepSpan.setAttributes(
2247
2215
  selectAttributes2(telemetry, {
@@ -2250,7 +2218,7 @@ var LegacyOpenTelemetry = class {
2250
2218
  output: () => {
2251
2219
  try {
2252
2220
  return JSON.stringify(JSON.parse(event.objectText));
2253
- } catch (e) {
2221
+ } catch {
2254
2222
  return event.objectText;
2255
2223
  }
2256
2224
  }
@@ -2262,8 +2230,8 @@ var LegacyOpenTelemetry = class {
2262
2230
  "ai.usage.inputTokens": event.usage.inputTokens,
2263
2231
  "ai.usage.outputTokens": event.usage.outputTokens,
2264
2232
  "ai.usage.totalTokens": event.usage.totalTokens,
2265
- "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2266
- "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
2233
+ "ai.usage.reasoningTokens": event.usage.outputTokenDetails?.reasoningTokens,
2234
+ "ai.usage.cachedInputTokens": event.usage.inputTokenDetails?.cacheReadTokens,
2267
2235
  "gen_ai.response.finish_reasons": [event.finishReason],
2268
2236
  "gen_ai.response.id": event.response.id,
2269
2237
  "gen_ai.response.model": event.response.modelId,
@@ -2334,9 +2302,8 @@ var LegacyOpenTelemetry = class {
2334
2302
  });
2335
2303
  }
2336
2304
  onStepStart(event) {
2337
- var _a;
2338
2305
  const state = this.getCallState(event.callId);
2339
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2306
+ if (!state?.rootSpan || !state.rootContext) return;
2340
2307
  const { telemetry } = state;
2341
2308
  const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
2342
2309
  const attributes = selectAttributes2(telemetry, {
@@ -2351,10 +2318,7 @@ var LegacyOpenTelemetry = class {
2351
2318
  input: () => event.promptMessages ? stringifyForTelemetry(event.promptMessages) : void 0
2352
2319
  },
2353
2320
  "ai.prompt.tools": {
2354
- input: () => {
2355
- var _a2;
2356
- return (_a2 = event.stepTools) == null ? void 0 : _a2.map((tool) => JSON.stringify(tool));
2357
- }
2321
+ input: () => event.stepTools?.map((tool) => JSON.stringify(tool))
2358
2322
  },
2359
2323
  "ai.prompt.toolChoice": {
2360
2324
  input: () => event.stepToolChoice != null ? JSON.stringify(event.stepToolChoice) : void 0
@@ -2365,7 +2329,7 @@ var LegacyOpenTelemetry = class {
2365
2329
  "gen_ai.request.max_tokens": state.settings.maxOutputTokens,
2366
2330
  "gen_ai.request.presence_penalty": state.settings.presencePenalty,
2367
2331
  "gen_ai.request.stop_sequences": state.settings.stopSequences,
2368
- "gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
2332
+ "gen_ai.request.temperature": state.settings.temperature ?? void 0,
2369
2333
  "gen_ai.request.top_k": state.settings.topK,
2370
2334
  "gen_ai.request.top_p": state.settings.topP
2371
2335
  });
@@ -2378,7 +2342,7 @@ var LegacyOpenTelemetry = class {
2378
2342
  }
2379
2343
  onToolExecutionStart(event) {
2380
2344
  const state = this.getCallState(event.callId);
2381
- if (!(state == null ? void 0 : state.stepContext)) return;
2345
+ if (!state?.stepContext) return;
2382
2346
  const { telemetry } = state;
2383
2347
  const { toolCall } = event;
2384
2348
  const attributes = selectAttributes2(telemetry, {
@@ -2425,7 +2389,7 @@ var LegacyOpenTelemetry = class {
2425
2389
  }
2426
2390
  })
2427
2391
  );
2428
- } catch (e) {
2392
+ } catch {
2429
2393
  }
2430
2394
  } else {
2431
2395
  recordSpanError(span, toolOutput.error);
@@ -2434,19 +2398,15 @@ var LegacyOpenTelemetry = class {
2434
2398
  state.toolSpans.delete(event.toolCall.toolCallId);
2435
2399
  }
2436
2400
  onStepEnd(event) {
2437
- var _a, _b, _c, _d, _e, _f, _g;
2438
2401
  const state = this.getCallState(event.callId);
2439
- if (!(state == null ? void 0 : state.stepSpan)) return;
2402
+ if (!state?.stepSpan) return;
2440
2403
  const { telemetry } = state;
2441
2404
  const isStreamText = state.operationId === "ai.streamText";
2442
2405
  state.stepSpan.setAttributes(
2443
2406
  selectAttributes2(telemetry, {
2444
2407
  "ai.response.finishReason": event.finishReason,
2445
2408
  "ai.response.text": {
2446
- output: () => {
2447
- var _a2;
2448
- return (_a2 = event.text) != null ? _a2 : void 0;
2449
- }
2409
+ output: () => event.text ?? void 0
2450
2410
  },
2451
2411
  "ai.response.reasoning": {
2452
2412
  output: () => event.reasoning.length > 0 ? event.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
@@ -2479,13 +2439,13 @@ var LegacyOpenTelemetry = class {
2479
2439
  "ai.usage.inputTokens": event.usage.inputTokens,
2480
2440
  "ai.usage.outputTokens": event.usage.outputTokens,
2481
2441
  "ai.usage.totalTokens": event.usage.totalTokens,
2482
- "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2483
- "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
2484
- "ai.usage.inputTokenDetails.noCacheTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.noCacheTokens,
2485
- "ai.usage.inputTokenDetails.cacheReadTokens": (_d = event.usage.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens,
2486
- "ai.usage.inputTokenDetails.cacheWriteTokens": (_e = event.usage.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
2487
- "ai.usage.outputTokenDetails.textTokens": (_f = event.usage.outputTokenDetails) == null ? void 0 : _f.textTokens,
2488
- "ai.usage.outputTokenDetails.reasoningTokens": (_g = event.usage.outputTokenDetails) == null ? void 0 : _g.reasoningTokens,
2442
+ "ai.usage.reasoningTokens": event.usage.outputTokenDetails?.reasoningTokens,
2443
+ "ai.usage.cachedInputTokens": event.usage.inputTokenDetails?.cacheReadTokens,
2444
+ "ai.usage.inputTokenDetails.noCacheTokens": event.usage.inputTokenDetails?.noCacheTokens,
2445
+ "ai.usage.inputTokenDetails.cacheReadTokens": event.usage.inputTokenDetails?.cacheReadTokens,
2446
+ "ai.usage.inputTokenDetails.cacheWriteTokens": event.usage.inputTokenDetails?.cacheWriteTokens,
2447
+ "ai.usage.outputTokenDetails.textTokens": event.usage.outputTokenDetails?.textTokens,
2448
+ "ai.usage.outputTokenDetails.reasoningTokens": event.usage.outputTokenDetails?.reasoningTokens,
2489
2449
  "gen_ai.response.finish_reasons": [event.finishReason],
2490
2450
  "gen_ai.response.id": event.response.id,
2491
2451
  "gen_ai.response.model": event.response.modelId,
@@ -2514,7 +2474,7 @@ var LegacyOpenTelemetry = class {
2514
2474
  }
2515
2475
  onEnd(event) {
2516
2476
  const state = this.getCallState(event.callId);
2517
- if (!(state == null ? void 0 : state.rootSpan)) return;
2477
+ if (!state?.rootSpan) return;
2518
2478
  if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
2519
2479
  this.onEmbedOperationEnd(event);
2520
2480
  return;
@@ -2530,18 +2490,14 @@ var LegacyOpenTelemetry = class {
2530
2490
  this.onGenerateEnd(event);
2531
2491
  }
2532
2492
  onGenerateEnd(event) {
2533
- var _a, _b, _c, _d, _e, _f, _g;
2534
2493
  const state = this.getCallState(event.callId);
2535
- if (!(state == null ? void 0 : state.rootSpan)) return;
2494
+ if (!state?.rootSpan) return;
2536
2495
  const { telemetry } = state;
2537
2496
  state.rootSpan.setAttributes(
2538
2497
  selectAttributes2(telemetry, {
2539
2498
  "ai.response.finishReason": event.finishReason,
2540
2499
  "ai.response.text": {
2541
- output: () => {
2542
- var _a2;
2543
- return (_a2 = event.text) != null ? _a2 : void 0;
2544
- }
2500
+ output: () => event.text ?? void 0
2545
2501
  },
2546
2502
  "ai.response.reasoning": {
2547
2503
  output: () => event.finalStep.reasoning.length > 0 ? event.finalStep.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
@@ -2568,22 +2524,21 @@ var LegacyOpenTelemetry = class {
2568
2524
  "ai.usage.inputTokens": event.usage.inputTokens,
2569
2525
  "ai.usage.outputTokens": event.usage.outputTokens,
2570
2526
  "ai.usage.totalTokens": event.usage.totalTokens,
2571
- "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2572
- "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
2573
- "ai.usage.inputTokenDetails.noCacheTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.noCacheTokens,
2574
- "ai.usage.inputTokenDetails.cacheReadTokens": (_d = event.usage.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens,
2575
- "ai.usage.inputTokenDetails.cacheWriteTokens": (_e = event.usage.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
2576
- "ai.usage.outputTokenDetails.textTokens": (_f = event.usage.outputTokenDetails) == null ? void 0 : _f.textTokens,
2577
- "ai.usage.outputTokenDetails.reasoningTokens": (_g = event.usage.outputTokenDetails) == null ? void 0 : _g.reasoningTokens
2527
+ "ai.usage.reasoningTokens": event.usage.outputTokenDetails?.reasoningTokens,
2528
+ "ai.usage.cachedInputTokens": event.usage.inputTokenDetails?.cacheReadTokens,
2529
+ "ai.usage.inputTokenDetails.noCacheTokens": event.usage.inputTokenDetails?.noCacheTokens,
2530
+ "ai.usage.inputTokenDetails.cacheReadTokens": event.usage.inputTokenDetails?.cacheReadTokens,
2531
+ "ai.usage.inputTokenDetails.cacheWriteTokens": event.usage.inputTokenDetails?.cacheWriteTokens,
2532
+ "ai.usage.outputTokenDetails.textTokens": event.usage.outputTokenDetails?.textTokens,
2533
+ "ai.usage.outputTokenDetails.reasoningTokens": event.usage.outputTokenDetails?.reasoningTokens
2578
2534
  })
2579
2535
  );
2580
2536
  state.rootSpan.end();
2581
2537
  this.cleanupCallState(event.callId);
2582
2538
  }
2583
2539
  onObjectOperationEnd(event) {
2584
- var _a, _b;
2585
2540
  const state = this.getCallState(event.callId);
2586
- if (!(state == null ? void 0 : state.rootSpan)) return;
2541
+ if (!state?.rootSpan) return;
2587
2542
  const { telemetry } = state;
2588
2543
  state.rootSpan.setAttributes(
2589
2544
  selectAttributes2(telemetry, {
@@ -2595,8 +2550,8 @@ var LegacyOpenTelemetry = class {
2595
2550
  "ai.usage.inputTokens": event.usage.inputTokens,
2596
2551
  "ai.usage.outputTokens": event.usage.outputTokens,
2597
2552
  "ai.usage.totalTokens": event.usage.totalTokens,
2598
- "ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
2599
- "ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens
2553
+ "ai.usage.reasoningTokens": event.usage.outputTokenDetails?.reasoningTokens,
2554
+ "ai.usage.cachedInputTokens": event.usage.inputTokenDetails?.cacheReadTokens
2600
2555
  })
2601
2556
  );
2602
2557
  state.rootSpan.end();
@@ -2604,7 +2559,7 @@ var LegacyOpenTelemetry = class {
2604
2559
  }
2605
2560
  onEmbedOperationEnd(event) {
2606
2561
  const state = this.getCallState(event.callId);
2607
- if (!(state == null ? void 0 : state.rootSpan)) return;
2562
+ if (!state?.rootSpan) return;
2608
2563
  const { telemetry } = state;
2609
2564
  const isMany = state.operationId === "ai.embedMany";
2610
2565
  state.rootSpan.setAttributes(
@@ -2626,7 +2581,7 @@ var LegacyOpenTelemetry = class {
2626
2581
  }
2627
2582
  onEmbedStart(event) {
2628
2583
  const state = this.getCallState(event.callId);
2629
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2584
+ if (!state?.rootSpan || !state.rootContext) return;
2630
2585
  const { telemetry } = state;
2631
2586
  const attributes = selectAttributes2(telemetry, {
2632
2587
  ...assembleOperationName({
@@ -2711,13 +2666,13 @@ var LegacyOpenTelemetry = class {
2711
2666
  }
2712
2667
  onRerankOperationEnd(event) {
2713
2668
  const state = this.getCallState(event.callId);
2714
- if (!(state == null ? void 0 : state.rootSpan)) return;
2669
+ if (!state?.rootSpan) return;
2715
2670
  state.rootSpan.end();
2716
2671
  this.cleanupCallState(event.callId);
2717
2672
  }
2718
2673
  onRerankStart(event) {
2719
2674
  const state = this.getCallState(event.callId);
2720
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2675
+ if (!state?.rootSpan || !state.rootContext) return;
2721
2676
  const { telemetry } = state;
2722
2677
  const attributes = selectAttributes2(telemetry, {
2723
2678
  ...assembleOperationName({
@@ -2739,7 +2694,7 @@ var LegacyOpenTelemetry = class {
2739
2694
  }
2740
2695
  onRerankEnd(event) {
2741
2696
  const state = this.getCallState(event.callId);
2742
- if (!(state == null ? void 0 : state.rerankSpan)) return;
2697
+ if (!state?.rerankSpan) return;
2743
2698
  const { span } = state.rerankSpan;
2744
2699
  const { telemetry } = state;
2745
2700
  span.setAttributes(
@@ -2797,7 +2752,7 @@ var LegacyOpenTelemetry = class {
2797
2752
  }
2798
2753
  onEvaluateOperationEnd(event) {
2799
2754
  const state = this.getCallState(event.callId);
2800
- if (!(state == null ? void 0 : state.rootSpan)) return;
2755
+ if (!state?.rootSpan) return;
2801
2756
  state.rootSpan.setAttributes(
2802
2757
  selectAttributes2(state.telemetry, {
2803
2758
  "ai.evaluation.answers": {
@@ -2816,7 +2771,7 @@ var LegacyOpenTelemetry = class {
2816
2771
  }
2817
2772
  experimental_onEvaluationModelCallStart(event) {
2818
2773
  const state = this.getCallState(event.callId);
2819
- if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
2774
+ if (!state?.rootSpan || !state.rootContext) return;
2820
2775
  const attributes = selectAttributes2(state.telemetry, {
2821
2776
  ...assembleOperationName({
2822
2777
  operationId: event.operationId,
@@ -2841,16 +2796,15 @@ var LegacyOpenTelemetry = class {
2841
2796
  };
2842
2797
  }
2843
2798
  experimental_onEvaluationModelCallEnd(event) {
2844
- var _a, _b;
2845
2799
  const state = this.getCallState(event.callId);
2846
- if (!(state == null ? void 0 : state.evaluationSpan)) return;
2800
+ if (!state?.evaluationSpan) return;
2847
2801
  state.evaluationSpan.span.setAttributes(
2848
2802
  selectAttributes2(state.telemetry, {
2849
2803
  "ai.evaluation.answers": {
2850
2804
  output: () => JSON.stringify(event.answers)
2851
2805
  },
2852
- "ai.usage.inputTokens": (_a = event.usage) == null ? void 0 : _a.inputTokens,
2853
- "ai.usage.outputTokens": (_b = event.usage) == null ? void 0 : _b.outputTokens,
2806
+ "ai.usage.inputTokens": event.usage?.inputTokens,
2807
+ "ai.usage.outputTokens": event.usage?.outputTokens,
2854
2808
  "ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
2855
2809
  })
2856
2810
  );
@@ -2859,7 +2813,7 @@ var LegacyOpenTelemetry = class {
2859
2813
  }
2860
2814
  onAbort(event) {
2861
2815
  const state = this.getCallState(event.callId);
2862
- if (!(state == null ? void 0 : state.rootSpan)) return;
2816
+ if (!state?.rootSpan) return;
2863
2817
  for (const { span: toolSpan } of state.toolSpans.values()) {
2864
2818
  toolSpan.end();
2865
2819
  }
@@ -2885,12 +2839,11 @@ var LegacyOpenTelemetry = class {
2885
2839
  this.cleanupCallState(event.callId);
2886
2840
  }
2887
2841
  onError(error) {
2888
- var _a;
2889
2842
  const event = error;
2890
- if (!(event == null ? void 0 : event.callId)) return;
2843
+ if (!event?.callId) return;
2891
2844
  const state = this.getCallState(event.callId);
2892
- if (!(state == null ? void 0 : state.rootSpan)) return;
2893
- const actualError = (_a = event.error) != null ? _a : error;
2845
+ if (!state?.rootSpan) return;
2846
+ const actualError = event.error ?? error;
2894
2847
  if (state.stepSpan) {
2895
2848
  recordSpanError(state.stepSpan, actualError);
2896
2849
  state.stepSpan.end();