braintrust 3.27.0 → 3.28.0

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.
Files changed (53) hide show
  1. package/dev/dist/index.d.mts +519 -186
  2. package/dev/dist/index.d.ts +519 -186
  3. package/dev/dist/index.js +1837 -1009
  4. package/dev/dist/index.mjs +1172 -344
  5. package/dist/apply-auto-instrumentation.js +262 -210
  6. package/dist/apply-auto-instrumentation.mjs +54 -2
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +81 -2
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  9. package/dist/auto-instrumentations/bundler/next.cjs +81 -2
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +81 -2
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  13. package/dist/auto-instrumentations/bundler/vite.cjs +81 -2
  14. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  15. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +81 -2
  16. package/dist/auto-instrumentations/bundler/webpack.cjs +81 -2
  17. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  18. package/dist/auto-instrumentations/{chunk-XEYKUBLY.mjs → chunk-26PKVUKB.mjs} +80 -2
  19. package/dist/auto-instrumentations/{chunk-BW33ULMW.mjs → chunk-HD35AM3M.mjs} +1 -1
  20. package/dist/auto-instrumentations/{chunk-ZNHTSSGI.mjs → chunk-NP7V4XB2.mjs} +2 -1
  21. package/dist/auto-instrumentations/hook.mjs +236 -30
  22. package/dist/auto-instrumentations/index.cjs +2 -1
  23. package/dist/auto-instrumentations/index.mjs +1 -1
  24. package/dist/browser.d.mts +628 -53
  25. package/dist/browser.d.ts +628 -53
  26. package/dist/browser.js +2301 -284
  27. package/dist/browser.mjs +2301 -284
  28. package/dist/{chunk-MF7NU6BT.js → chunk-BBE7SNRV.js} +34 -4
  29. package/dist/{chunk-QRHGVBKU.js → chunk-OBBWQW6K.js} +1799 -1023
  30. package/dist/{chunk-YKD22IMR.mjs → chunk-UPFNQCGB.mjs} +966 -190
  31. package/dist/{chunk-CZM5JIQL.mjs → chunk-ZHUHZWFY.mjs} +33 -3
  32. package/dist/cli.js +1224 -398
  33. package/dist/edge-light.d.mts +1 -1
  34. package/dist/edge-light.d.ts +1 -1
  35. package/dist/edge-light.js +2301 -284
  36. package/dist/edge-light.mjs +2301 -284
  37. package/dist/index.d.mts +1212 -637
  38. package/dist/index.d.ts +1212 -637
  39. package/dist/index.js +1880 -590
  40. package/dist/index.mjs +1450 -160
  41. package/dist/instrumentation/index.d.mts +190 -6
  42. package/dist/instrumentation/index.d.ts +190 -6
  43. package/dist/instrumentation/index.js +823 -116
  44. package/dist/instrumentation/index.mjs +823 -116
  45. package/dist/vitest-evals-reporter.js +16 -16
  46. package/dist/vitest-evals-reporter.mjs +2 -2
  47. package/dist/workerd.d.mts +1 -1
  48. package/dist/workerd.d.ts +1 -1
  49. package/dist/workerd.js +2301 -284
  50. package/dist/workerd.mjs +2301 -284
  51. package/package.json +2 -3
  52. package/util/dist/index.d.mts +1545 -100
  53. package/util/dist/index.d.ts +1545 -100
@@ -2621,7 +2621,8 @@ var AclObjectType = import_v36.z.union([
2621
2621
  "org_member",
2622
2622
  "project_log",
2623
2623
  "org_project",
2624
- "org_audit_logs"
2624
+ "org_audit_logs",
2625
+ "project_group"
2625
2626
  ]),
2626
2627
  import_v36.z.null()
2627
2628
  ]);
@@ -2750,7 +2751,8 @@ var AsyncScoringState = import_v36.z.union([
2750
2751
  token: import_v36.z.string(),
2751
2752
  function_ids: import_v36.z.array(import_v36.z.unknown()),
2752
2753
  skip_logging: import_v36.z.union([import_v36.z.boolean(), import_v36.z.null()]).optional(),
2753
- triggered_functions: import_v36.z.union([import_v36.z.record(TriggeredFunctionState), import_v36.z.null()]).optional()
2754
+ triggered_functions: import_v36.z.union([import_v36.z.record(TriggeredFunctionState), import_v36.z.null()]).optional(),
2755
+ last_triggered_xact_id: import_v36.z.union([import_v36.z.string(), import_v36.z.number(), import_v36.z.null()]).optional()
2754
2756
  }),
2755
2757
  import_v36.z.object({ status: import_v36.z.literal("disabled") }),
2756
2758
  import_v36.z.null(),
@@ -2819,7 +2821,7 @@ var FunctionTypeEnum = import_v36.z.enum([
2819
2821
  "parameters",
2820
2822
  "sandbox"
2821
2823
  ]);
2822
- var NullableSavedFunctionId = import_v36.z.union([
2824
+ var FacetPreprocessorId = import_v36.z.union([
2823
2825
  import_v36.z.object({
2824
2826
  type: import_v36.z.literal("function"),
2825
2827
  id: import_v36.z.string(),
@@ -2830,10 +2832,23 @@ var NullableSavedFunctionId = import_v36.z.union([
2830
2832
  name: import_v36.z.string(),
2831
2833
  function_type: FunctionTypeEnum.optional().default("scorer")
2832
2834
  }),
2835
+ import_v36.z.object({ type: import_v36.z.literal("inline"), code: import_v36.z.string().min(1) }),
2833
2836
  import_v36.z.null()
2834
2837
  ]);
2838
+ var SavedFunctionId = import_v36.z.union([
2839
+ import_v36.z.object({
2840
+ type: import_v36.z.literal("function"),
2841
+ id: import_v36.z.string(),
2842
+ version: import_v36.z.string().optional()
2843
+ }),
2844
+ import_v36.z.object({
2845
+ type: import_v36.z.literal("global"),
2846
+ name: import_v36.z.string(),
2847
+ function_type: FunctionTypeEnum.optional().default("scorer")
2848
+ })
2849
+ ]);
2835
2850
  var TopicMapGenerationSettings = import_v36.z.object({
2836
- algorithm: import_v36.z.enum(["hdbscan", "kmeans"]),
2851
+ algorithm: import_v36.z.enum(["hdbscan", "kmeans", "community"]),
2837
2852
  dimension_reduction: import_v36.z.enum(["umap", "pca", "none"]),
2838
2853
  sample_size: import_v36.z.number().int().gt(0).optional(),
2839
2854
  n_clusters: import_v36.z.number().int().gt(0).optional(),
@@ -2845,6 +2860,7 @@ var TopicMapGenerationSettings = import_v36.z.object({
2845
2860
  var TopicMapData = import_v36.z.object({
2846
2861
  type: import_v36.z.literal("topic_map"),
2847
2862
  source_facet: import_v36.z.string(),
2863
+ source_facet_function: SavedFunctionId.and(import_v36.z.unknown()).optional(),
2848
2864
  embedding_model: import_v36.z.string(),
2849
2865
  bundle_key: import_v36.z.string().optional(),
2850
2866
  report_key: import_v36.z.string().optional(),
@@ -2858,7 +2874,7 @@ var TopicMapData = import_v36.z.object({
2858
2874
  });
2859
2875
  var BatchedFacetData = import_v36.z.object({
2860
2876
  type: import_v36.z.literal("batched_facet"),
2861
- preprocessor: NullableSavedFunctionId.and(import_v36.z.unknown()).optional(),
2877
+ preprocessor: FacetPreprocessorId.optional(),
2862
2878
  facets: import_v36.z.array(
2863
2879
  import_v36.z.object({
2864
2880
  name: import_v36.z.string(),
@@ -3119,18 +3135,6 @@ var ObjectReferenceNullish = import_v36.z.union([
3119
3135
  }),
3120
3136
  import_v36.z.null()
3121
3137
  ]);
3122
- var SavedFunctionId = import_v36.z.union([
3123
- import_v36.z.object({
3124
- type: import_v36.z.literal("function"),
3125
- id: import_v36.z.string(),
3126
- version: import_v36.z.string().optional()
3127
- }),
3128
- import_v36.z.object({
3129
- type: import_v36.z.literal("global"),
3130
- name: import_v36.z.string(),
3131
- function_type: FunctionTypeEnum.optional().default("scorer")
3132
- })
3133
- ]);
3134
3138
  var DatasetEvent = import_v36.z.object({
3135
3139
  id: import_v36.z.string(),
3136
3140
  _xact_id: import_v36.z.string(),
@@ -3352,7 +3356,7 @@ var ExtendedSavedFunctionId = import_v36.z.union([
3352
3356
  ]);
3353
3357
  var FacetData = import_v36.z.object({
3354
3358
  type: import_v36.z.literal("facet"),
3355
- preprocessor: NullableSavedFunctionId.and(import_v36.z.unknown()).optional(),
3359
+ preprocessor: FacetPreprocessorId.optional(),
3356
3360
  prompt: import_v36.z.string(),
3357
3361
  model: import_v36.z.string().optional(),
3358
3362
  embedding_model: import_v36.z.string().optional(),
@@ -3857,6 +3861,19 @@ var MessageRole = import_v36.z.enum([
3857
3861
  "model",
3858
3862
  "developer"
3859
3863
  ]);
3864
+ var NullableSavedFunctionId = import_v36.z.union([
3865
+ import_v36.z.object({
3866
+ type: import_v36.z.literal("function"),
3867
+ id: import_v36.z.string(),
3868
+ version: import_v36.z.string().optional()
3869
+ }),
3870
+ import_v36.z.object({
3871
+ type: import_v36.z.literal("global"),
3872
+ name: import_v36.z.string(),
3873
+ function_type: FunctionTypeEnum.optional().default("scorer")
3874
+ }),
3875
+ import_v36.z.null()
3876
+ ]);
3860
3877
  var ObjectReference = import_v36.z.object({
3861
3878
  object_type: import_v36.z.enum([
3862
3879
  "project_logs",
@@ -3878,6 +3895,7 @@ var TraceScope = import_v36.z.object({
3878
3895
  });
3879
3896
  var OnlineScoreConfig = import_v36.z.union([
3880
3897
  import_v36.z.object({
3898
+ status: AutomationStatus.optional(),
3881
3899
  sampling_rate: import_v36.z.number().gte(0).lte(1),
3882
3900
  scorers: import_v36.z.array(SavedFunctionId),
3883
3901
  btql_filter: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
@@ -3958,6 +3976,72 @@ var Project = import_v36.z.object({
3958
3976
  user_id: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
3959
3977
  settings: ProjectSettings.optional()
3960
3978
  });
3979
+ var WindowedAutomationConfig = import_v36.z.object({
3980
+ event_type: import_v36.z.literal("windowed"),
3981
+ product_origin: import_v36.z.union([import_v36.z.literal("patterns"), import_v36.z.null()]).optional(),
3982
+ status: AutomationStatus.optional(),
3983
+ threshold: import_v36.z.object({
3984
+ calculation: import_v36.z.object({
3985
+ type: import_v36.z.literal("btql"),
3986
+ btql_query: import_v36.z.string().min(1),
3987
+ output: import_v36.z.object({
3988
+ type: import_v36.z.literal("scalar"),
3989
+ value_column: import_v36.z.string().min(1)
3990
+ })
3991
+ }),
3992
+ policy: import_v36.z.object({
3993
+ condition: import_v36.z.object({
3994
+ type: import_v36.z.literal("threshold"),
3995
+ operator: import_v36.z.enum(["lt", "lte", "gt", "gte", "eq", "neq"]),
3996
+ threshold: import_v36.z.number()
3997
+ }),
3998
+ pending_seconds: import_v36.z.number().int().gte(0).lte(2592e3),
3999
+ no_data_behavior: import_v36.z.enum(["keep_last", "resolve", "alert"]),
4000
+ renotify_interval_seconds: import_v36.z.union([import_v36.z.number(), import_v36.z.null()]).optional(),
4001
+ notify_on_recovery: import_v36.z.boolean().optional().default(true)
4002
+ })
4003
+ }).optional(),
4004
+ window: import_v36.z.object({
4005
+ window_seconds: import_v36.z.number().int().gte(1).lte(2592e3),
4006
+ schedule: import_v36.z.union([
4007
+ import_v36.z.object({
4008
+ type: import_v36.z.literal("interval"),
4009
+ evaluation_interval_seconds: import_v36.z.number().int().gte(1).lte(2592e3)
4010
+ }),
4011
+ import_v36.z.object({
4012
+ type: import_v36.z.literal("cron"),
4013
+ cron_expression: import_v36.z.string().min(1),
4014
+ timezone: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional()
4015
+ })
4016
+ ]),
4017
+ evaluation_delay_seconds: import_v36.z.number().int().gte(0).lte(2592e3)
4018
+ }),
4019
+ loop: import_v36.z.object({
4020
+ prompt: import_v36.z.string().min(1).max(1e4),
4021
+ include_trigger_input: import_v36.z.boolean().optional().default(false),
4022
+ agent_slug: import_v36.z.string().min(1),
4023
+ auto_approve_tools: import_v36.z.array(import_v36.z.string().min(1)).optional().default([]),
4024
+ harness: import_v36.z.enum(["native", "codex", "claude-code"]).optional(),
4025
+ model: import_v36.z.string().min(1).optional(),
4026
+ reasoning_effort: import_v36.z.enum(["none", "minimal", "low", "medium", "high", "xhigh", "max"]).optional()
4027
+ }).optional(),
4028
+ actions: import_v36.z.array(
4029
+ import_v36.z.union([
4030
+ import_v36.z.object({
4031
+ type: import_v36.z.literal("webhook"),
4032
+ url: import_v36.z.string(),
4033
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4034
+ }),
4035
+ import_v36.z.object({
4036
+ type: import_v36.z.literal("slack"),
4037
+ workspace_id: import_v36.z.string(),
4038
+ channel: import_v36.z.string(),
4039
+ message_template: import_v36.z.string().optional(),
4040
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4041
+ })
4042
+ ])
4043
+ ).max(20).optional().default([])
4044
+ });
3961
4045
  var TopicAutomationFacetModel = import_v36.z.union([
3962
4046
  import_v36.z.enum(["brain-facet-latest", "brain-facet-1", "brain-facet-2"]),
3963
4047
  import_v36.z.null()
@@ -3999,7 +4083,8 @@ var TopicDigestAutomationConfig = import_v36.z.object({
3999
4083
  type: import_v36.z.literal("slack"),
4000
4084
  workspace_id: import_v36.z.string(),
4001
4085
  channel: import_v36.z.string(),
4002
- message_template: import_v36.z.string().optional()
4086
+ message_template: import_v36.z.string().optional(),
4087
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4003
4088
  }),
4004
4089
  topic_map_function_ids: import_v36.z.array(import_v36.z.string()).max(10).optional()
4005
4090
  });
@@ -4013,15 +4098,21 @@ var ProjectAutomation = import_v36.z.object({
4013
4098
  config: import_v36.z.union([
4014
4099
  import_v36.z.object({
4015
4100
  event_type: import_v36.z.literal("logs"),
4101
+ status: AutomationStatus.optional(),
4016
4102
  btql_filter: import_v36.z.string(),
4017
4103
  interval_seconds: import_v36.z.number().gte(1).lte(2592e3),
4018
4104
  action: import_v36.z.union([
4019
- import_v36.z.object({ type: import_v36.z.literal("webhook"), url: import_v36.z.string() }),
4105
+ import_v36.z.object({
4106
+ type: import_v36.z.literal("webhook"),
4107
+ url: import_v36.z.string(),
4108
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4109
+ }),
4020
4110
  import_v36.z.object({
4021
4111
  type: import_v36.z.literal("slack"),
4022
4112
  workspace_id: import_v36.z.string(),
4023
4113
  channel: import_v36.z.string(),
4024
- message_template: import_v36.z.string().optional()
4114
+ message_template: import_v36.z.string().optional(),
4115
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4025
4116
  })
4026
4117
  ])
4027
4118
  }),
@@ -4072,21 +4163,38 @@ var ProjectAutomation = import_v36.z.object({
4072
4163
  }),
4073
4164
  import_v36.z.object({
4074
4165
  event_type: import_v36.z.literal("environment_update"),
4166
+ status: AutomationStatus.optional(),
4075
4167
  environment_filter: import_v36.z.array(import_v36.z.string()).optional(),
4076
4168
  action: import_v36.z.union([
4077
- import_v36.z.object({ type: import_v36.z.literal("webhook"), url: import_v36.z.string() }),
4169
+ import_v36.z.object({
4170
+ type: import_v36.z.literal("webhook"),
4171
+ url: import_v36.z.string(),
4172
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4173
+ }),
4078
4174
  import_v36.z.object({
4079
4175
  type: import_v36.z.literal("slack"),
4080
4176
  workspace_id: import_v36.z.string(),
4081
4177
  channel: import_v36.z.string(),
4082
- message_template: import_v36.z.string().optional()
4178
+ message_template: import_v36.z.string().optional(),
4179
+ formatting_prompt: import_v36.z.string().min(1).max(1e4).optional()
4083
4180
  })
4084
4181
  ])
4085
4182
  }),
4183
+ WindowedAutomationConfig,
4086
4184
  TopicAutomationConfig,
4087
4185
  TopicDigestAutomationConfig
4088
4186
  ])
4089
4187
  });
4188
+ var ProjectGroup = import_v36.z.object({
4189
+ id: import_v36.z.string().uuid(),
4190
+ org_id: import_v36.z.string().uuid(),
4191
+ user_id: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4192
+ created: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4193
+ name: import_v36.z.string(),
4194
+ description: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4195
+ deleted_at: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4196
+ member_projects: import_v36.z.array(import_v36.z.string().uuid()).max(1e4)
4197
+ });
4090
4198
  var ProjectLogsEvent = import_v36.z.object({
4091
4199
  id: import_v36.z.string(),
4092
4200
  _xact_id: import_v36.z.string(),
@@ -4304,7 +4412,8 @@ var RunEval = import_v36.z.object({
4304
4412
  dataset_environment: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4305
4413
  _internal_btql: import_v36.z.union([import_v36.z.object({}).partial().passthrough(), import_v36.z.null()]).optional()
4306
4414
  }),
4307
- import_v36.z.object({ data: import_v36.z.array(import_v36.z.unknown()) })
4415
+ import_v36.z.object({ data: import_v36.z.array(import_v36.z.unknown()) }),
4416
+ import_v36.z.object({ experiment_name: import_v36.z.string() })
4308
4417
  ]),
4309
4418
  name: import_v36.z.string().optional(),
4310
4419
  parameters: import_v36.z.object({}).partial().passthrough().optional(),
@@ -4509,7 +4618,9 @@ var View = import_v36.z.object({
4509
4618
  "for_review_datasets"
4510
4619
  ]),
4511
4620
  name: import_v36.z.string(),
4621
+ description: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4512
4622
  created: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4623
+ updated_at: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
4513
4624
  view_data: ViewData.optional(),
4514
4625
  options: ViewOptions.optional(),
4515
4626
  user_id: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
@@ -5606,12 +5717,13 @@ var INSTRUMENTATION_NAMES = {
5606
5717
  OPENROUTER: "openrouter",
5607
5718
  OPENROUTER_AGENT: "openrouter-agent",
5608
5719
  PI_CODING_AGENT: "pi-coding-agent",
5609
- STRANDS_AGENT_SDK: "strands-agent-sdk"
5720
+ STRANDS_AGENT_SDK: "strands-agent-sdk",
5721
+ VOYAGEAI: "voyageai"
5610
5722
  };
5611
5723
  var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
5612
5724
  "braintrust.spanInstrumentationName"
5613
5725
  );
5614
- var SDK_VERSION = true ? "3.27.0" : "0.0.0";
5726
+ var SDK_VERSION = true ? "3.28.0" : "0.0.0";
5615
5727
  function withSpanInstrumentationName(args, instrumentationName) {
5616
5728
  return {
5617
5729
  ...args,
@@ -5806,6 +5918,9 @@ function applyMaskingToField(maskingFunction, data, fieldName) {
5806
5918
  var INITIAL_SPAN_WRITE_AS_MERGE = /* @__PURE__ */ Symbol(
5807
5919
  "braintrust.initial-span-write-as-merge"
5808
5920
  );
5921
+ var RESUME_SPAN_WITHOUT_INITIAL_WRITE = /* @__PURE__ */ Symbol(
5922
+ "braintrust.resume-span-without-initial-write"
5923
+ );
5809
5924
  var INTERNAL_SPAN_CONTEXT = /* @__PURE__ */ Symbol("braintrust.internal-span-context");
5810
5925
  var BRAINTRUST_CURRENT_SPAN_STORE = /* @__PURE__ */ Symbol.for(
5811
5926
  "braintrust.currentSpanStore"
@@ -8655,7 +8770,7 @@ var ObjectFetcher = class {
8655
8770
  const objectId = await this.id;
8656
8771
  const batchLimit = batchSize ?? DEFAULT_FETCH_BATCH_SIZE;
8657
8772
  const internalLimit = getInternalBtqlLimit(this._internal_btql);
8658
- const limit = batchSize !== void 0 ? batchSize : internalLimit ?? batchLimit;
8773
+ let remainingLimit = internalLimit;
8659
8774
  const internalBtqlWithoutReservedQueryKeys = Object.fromEntries(
8660
8775
  Object.entries(this._internal_btql ?? {}).filter(
8661
8776
  ([key]) => key !== "cursor" && key !== "limit" && key !== "select" && key !== "from"
@@ -8664,6 +8779,10 @@ var ObjectFetcher = class {
8664
8779
  let cursor = void 0;
8665
8780
  let iterations = 0;
8666
8781
  while (true) {
8782
+ if (remainingLimit !== void 0 && remainingLimit <= 0) {
8783
+ return;
8784
+ }
8785
+ const limit = remainingLimit === void 0 ? batchLimit : Math.min(batchLimit, remainingLimit);
8667
8786
  const resp = await state.apiConn().post(
8668
8787
  `btql`,
8669
8788
  {
@@ -8703,7 +8822,14 @@ var ObjectFetcher = class {
8703
8822
  const respJson = await resp.json();
8704
8823
  const mutate = this.mutateRecord;
8705
8824
  for (const record of respJson.data ?? []) {
8706
- yield mutate ? mutate(record) : record;
8825
+ if (remainingLimit !== void 0 && remainingLimit <= 0) {
8826
+ return;
8827
+ }
8828
+ const mutatedRecord = mutate ? mutate(record) : record;
8829
+ if (remainingLimit !== void 0) {
8830
+ remainingLimit--;
8831
+ }
8832
+ yield mutatedRecord;
8707
8833
  }
8708
8834
  if (!respJson.cursor) {
8709
8835
  break;
@@ -9180,7 +9306,9 @@ var SpanImpl = class _SpanImpl {
9180
9306
  this._rootSpanId = resolvedIds.rootSpanId;
9181
9307
  this._spanParents = resolvedIds.spanParents;
9182
9308
  this.isMerge = args[INITIAL_SPAN_WRITE_AS_MERGE] === true;
9183
- this.logInternal({ event, internalData });
9309
+ if (!args[RESUME_SPAN_WITHOUT_INITIAL_WRITE]) {
9310
+ this.logInternal({ event, internalData });
9311
+ }
9184
9312
  this.isMerge = true;
9185
9313
  }
9186
9314
  getParentInfo() {
@@ -11059,6 +11187,22 @@ function processInputAttachments(input) {
11059
11187
  };
11060
11188
  }
11061
11189
  }
11190
+ const voyageBase64Key = node.type === "image_base64" ? Object.hasOwn(node, "imageBase64") ? "imageBase64" : "image_base64" : node.type === "video_base64" ? Object.hasOwn(node, "videoBase64") ? "videoBase64" : "video_base64" : void 0;
11191
+ const voyageBase64Value = voyageBase64Key ? node[voyageBase64Key] : void 0;
11192
+ if (voyageBase64Key && typeof voyageBase64Value === "string" && voyageBase64Value.startsWith("data:")) {
11193
+ const mediaType = inferMediaTypeFromDataUrl(
11194
+ voyageBase64Value,
11195
+ node.type === "video_base64" ? "video/mp4" : "image/png"
11196
+ );
11197
+ const filename = `${node.type === "video_base64" ? "video" : "image"}.${getExtensionFromMediaType(mediaType)}`;
11198
+ const attachment = toAttachment(voyageBase64Value, mediaType, filename);
11199
+ if (attachment) {
11200
+ return {
11201
+ ...node,
11202
+ [voyageBase64Key]: attachment
11203
+ };
11204
+ }
11205
+ }
11062
11206
  if (node.type === "file" && node.file && typeof node.file === "object" && typeof node.file.file_data === "string" && node.file.file_data.startsWith("data:")) {
11063
11207
  const mediaType = inferMediaTypeFromDataUrl(
11064
11208
  node.file.file_data,
@@ -12406,12 +12550,30 @@ function logInstrumentationError(context, error) {
12406
12550
 
12407
12551
  // src/wrappers/anthropic-tokens-util.ts
12408
12552
  function finalizeAnthropicTokens(metrics) {
12409
- const prompt_tokens = (metrics.prompt_tokens || 0) + (metrics.prompt_cached_tokens || 0) + (metrics.prompt_cache_creation_tokens || 0);
12410
- return {
12553
+ const hasSplitCacheCreationTokens = metrics.prompt_cache_creation_5m_tokens !== void 0 || metrics.prompt_cache_creation_1h_tokens !== void 0;
12554
+ const splitCacheCreationTokens = (metrics.prompt_cache_creation_5m_tokens || 0) + (metrics.prompt_cache_creation_1h_tokens || 0);
12555
+ const aggregateCacheCreationTokens = metrics.prompt_cache_creation_tokens || 0;
12556
+ const effectiveCacheCreationTokens = Math.max(
12557
+ aggregateCacheCreationTokens,
12558
+ splitCacheCreationTokens
12559
+ );
12560
+ const prompt_tokens = (metrics.prompt_tokens || 0) + (metrics.prompt_cached_tokens || 0) + effectiveCacheCreationTokens;
12561
+ const finalized = {
12411
12562
  ...metrics,
12412
12563
  prompt_tokens,
12413
12564
  tokens: prompt_tokens + (metrics.completion_tokens || 0)
12414
12565
  };
12566
+ if (hasSplitCacheCreationTokens && splitCacheCreationTokens >= aggregateCacheCreationTokens) {
12567
+ delete finalized.prompt_cache_creation_tokens;
12568
+ }
12569
+ return finalized;
12570
+ }
12571
+ function toNumericMetrics(metrics) {
12572
+ return Object.fromEntries(
12573
+ Object.entries(metrics).filter(
12574
+ (entry) => entry[1] !== void 0
12575
+ )
12576
+ );
12415
12577
  }
12416
12578
  function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens = 0) {
12417
12579
  const cacheTokens = {};
@@ -13004,6 +13166,18 @@ function parseMetricsFromUsage2(usage) {
13004
13166
  saveIfExistsTo("output_tokens", "completion_tokens");
13005
13167
  saveIfExistsTo("cache_read_input_tokens", "prompt_cached_tokens");
13006
13168
  saveIfExistsTo("cache_creation_input_tokens", "prompt_cache_creation_tokens");
13169
+ if (isObject(usage.cache_creation)) {
13170
+ const cacheCreation = usage.cache_creation;
13171
+ for (const [source, target] of [
13172
+ ["ephemeral_5m_input_tokens", "prompt_cache_creation_5m_tokens"],
13173
+ ["ephemeral_1h_input_tokens", "prompt_cache_creation_1h_tokens"]
13174
+ ]) {
13175
+ const value = cacheCreation[source];
13176
+ if (typeof value === "number") {
13177
+ metrics[target] = value;
13178
+ }
13179
+ }
13180
+ }
13007
13181
  if (isObject(usage.server_tool_use)) {
13008
13182
  for (const [name, value] of Object.entries(usage.server_tool_use)) {
13009
13183
  if (typeof value === "number") {
@@ -18302,37 +18476,97 @@ function seedTaskToolUseIdMapping(taskIdToToolUseId, message) {
18302
18476
  taskIdToToolUseId.set(message.task_id, message.tool_use_id);
18303
18477
  }
18304
18478
  }
18305
- function extractUsageFromMessage(message) {
18306
- const metrics = {};
18307
- let usage;
18308
- if (message.type === "assistant") {
18309
- usage = message.message?.usage;
18310
- } else if (message.type === "result") {
18311
- usage = message.usage;
18312
- }
18479
+ function tokenCount(value) {
18480
+ return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value) && value >= 0 ? value : void 0;
18481
+ }
18482
+ function copyUsage(usage) {
18313
18483
  if (!usage || typeof usage !== "object") {
18314
- return metrics;
18484
+ return void 0;
18485
+ }
18486
+ const copy = {};
18487
+ for (const key of [
18488
+ "input_tokens",
18489
+ "output_tokens",
18490
+ "cache_read_input_tokens",
18491
+ "cache_creation_input_tokens"
18492
+ ]) {
18493
+ const value = tokenCount(Reflect.get(usage, key));
18494
+ if (value !== void 0) {
18495
+ copy[key] = value;
18496
+ }
18497
+ }
18498
+ const cacheCreation = Reflect.get(usage, "cache_creation");
18499
+ if (cacheCreation && typeof cacheCreation === "object") {
18500
+ const cacheCreationCopy = {};
18501
+ for (const key of [
18502
+ "ephemeral_5m_input_tokens",
18503
+ "ephemeral_1h_input_tokens"
18504
+ ]) {
18505
+ const value = tokenCount(Reflect.get(cacheCreation, key));
18506
+ if (value !== void 0) {
18507
+ cacheCreationCopy[key] = value;
18508
+ }
18509
+ }
18510
+ if (Object.keys(cacheCreationCopy).length > 0) {
18511
+ copy.cache_creation = cacheCreationCopy;
18512
+ }
18513
+ }
18514
+ return Object.keys(copy).length > 0 ? copy : void 0;
18515
+ }
18516
+ function mergeUsage(base, override) {
18517
+ if (!base || !override) {
18518
+ return override ?? base;
18519
+ }
18520
+ const cacheCreation = base.cache_creation || override.cache_creation ? { ...base.cache_creation, ...override.cache_creation } : void 0;
18521
+ return {
18522
+ ...base,
18523
+ ...override,
18524
+ ...cacheCreation && { cache_creation: cacheCreation }
18525
+ };
18526
+ }
18527
+ function extractUsage(usage, includeOutput) {
18528
+ const metrics = {};
18529
+ if (!usage) {
18530
+ return {};
18315
18531
  }
18316
18532
  const inputTokens = getNumberProperty(usage, "input_tokens");
18317
18533
  if (inputTokens !== void 0) {
18318
18534
  metrics.prompt_tokens = inputTokens;
18319
18535
  }
18320
- const outputTokens = getNumberProperty(usage, "output_tokens");
18321
- if (outputTokens !== void 0) {
18322
- metrics.completion_tokens = outputTokens;
18536
+ if (includeOutput) {
18537
+ const outputTokens = getNumberProperty(usage, "output_tokens");
18538
+ if (outputTokens !== void 0) {
18539
+ metrics.completion_tokens = outputTokens;
18540
+ }
18323
18541
  }
18324
18542
  const cacheReadTokens = getNumberProperty(usage, "cache_read_input_tokens") || 0;
18325
18543
  const cacheCreationTokens = getNumberProperty(usage, "cache_creation_input_tokens") || 0;
18326
- if (cacheReadTokens > 0 || cacheCreationTokens > 0) {
18327
- Object.assign(
18328
- metrics,
18329
- extractAnthropicCacheTokens(cacheReadTokens, cacheCreationTokens)
18330
- );
18544
+ Object.assign(
18545
+ metrics,
18546
+ extractAnthropicCacheTokens(cacheReadTokens, cacheCreationTokens)
18547
+ );
18548
+ const cacheCreation5mTokens = getNumberProperty(
18549
+ usage.cache_creation,
18550
+ "ephemeral_5m_input_tokens"
18551
+ );
18552
+ const cacheCreation1hTokens = getNumberProperty(
18553
+ usage.cache_creation,
18554
+ "ephemeral_1h_input_tokens"
18555
+ );
18556
+ if (cacheCreation5mTokens !== void 0) {
18557
+ metrics.prompt_cache_creation_5m_tokens = cacheCreation5mTokens;
18331
18558
  }
18332
- if (Object.keys(metrics).length > 0) {
18333
- Object.assign(metrics, finalizeAnthropicTokens(metrics));
18559
+ if (cacheCreation1hTokens !== void 0) {
18560
+ metrics.prompt_cache_creation_1h_tokens = cacheCreation1hTokens;
18334
18561
  }
18335
- return metrics;
18562
+ if (Object.keys(metrics).length === 0) {
18563
+ return {};
18564
+ }
18565
+ const finalized = finalizeAnthropicTokens(metrics);
18566
+ if (metrics.completion_tokens === void 0) {
18567
+ delete finalized.tokens;
18568
+ }
18569
+ return toNumericMetrics(finalized);
18336
18570
  }
18337
18571
  function buildLLMInput(promptMessages, conversationHistory) {
18338
18572
  const inputParts = [...promptMessages, ...conversationHistory];
@@ -18366,16 +18600,16 @@ function buildRootPromptMessages(prompt, capturedPromptMessages) {
18366
18600
  function formatCapturedMessages(messages) {
18367
18601
  return messages.length > 0 ? messages : [];
18368
18602
  }
18369
- async function createLLMSpanForMessages(messages, promptMessages, conversationHistory, options, startTime, parentSpan, existingSpan) {
18603
+ async function createLLMSpanForMessages(messages, promptMessages, conversationHistory, options, startTime, parentSpan, usage, hasFinalOutputUsage, existingSpan) {
18370
18604
  if (messages.length === 0) {
18371
18605
  return void 0;
18372
18606
  }
18373
18607
  const lastMessage = messages[messages.length - 1];
18374
- if (lastMessage.type !== "assistant" || !lastMessage.message?.usage) {
18608
+ if (lastMessage.type !== "assistant") {
18375
18609
  return void 0;
18376
18610
  }
18377
- const model = lastMessage.message.model || options.model;
18378
- const usage = extractUsageFromMessage(lastMessage);
18611
+ const model = lastMessage.message?.model || options.model;
18612
+ const metrics = options.includePartialMessages ? extractUsage(usage, hasFinalOutputUsage) : {};
18379
18613
  const input = buildLLMInput(promptMessages, conversationHistory);
18380
18614
  const outputs = messages.map(
18381
18615
  (m) => m.message?.content && m.message?.role ? { content: m.message.content, role: m.message.role } : void 0
@@ -18397,8 +18631,8 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
18397
18631
  );
18398
18632
  span.log({
18399
18633
  input,
18400
- metadata: model ? { model } : void 0,
18401
- metrics: usage,
18634
+ metadata: { ...model && { model }, provider: "anthropic" },
18635
+ ...Object.keys(metrics).length > 0 ? { metrics } : {},
18402
18636
  output: outputs
18403
18637
  });
18404
18638
  const spanExport = await span.export();
@@ -18488,11 +18722,17 @@ function prepareLocalToolHandlersInMcpServers(mcpServers) {
18488
18722
  }
18489
18723
  return { hasLocalToolHandlers, localToolHookNames };
18490
18724
  }
18491
- function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
18725
+ function createToolTracingHooks(resolveParentSpan, taskIdToToolUseId, toolUseToParent, activeToolSpans, mcpServers, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
18492
18726
  const preToolUse = async (input, toolUseID) => {
18493
18727
  if (input.hook_event_name !== "PreToolUse" || !toolUseID) {
18494
18728
  return {};
18495
18729
  }
18730
+ if (!toolUseToParent.has(toolUseID) && input.agent_id) {
18731
+ const parentToolUseId = taskIdToToolUseId.get(input.agent_id);
18732
+ if (parentToolUseId) {
18733
+ toolUseToParent.set(toolUseID, parentToolUseId);
18734
+ }
18735
+ }
18496
18736
  if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
18497
18737
  return {};
18498
18738
  }
@@ -18688,9 +18928,6 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
18688
18928
  }
18689
18929
  const metadata = {
18690
18930
  ...subAgentDetailsToMetadata(details),
18691
- ...input.agent_transcript_path && {
18692
- "claude_agent_sdk.agent_transcript_path": input.agent_transcript_path
18693
- },
18694
18931
  "claude_agent_sdk.stop_hook_active": input.stop_hook_active
18695
18932
  };
18696
18933
  try {
@@ -18712,7 +18949,7 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
18712
18949
  subagentStop
18713
18950
  };
18714
18951
  }
18715
- function injectTracingHooks(options, resolveParentSpan, activeToolSpans, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
18952
+ function injectTracingHooks(options, resolveParentSpan, taskIdToToolUseId, toolUseToParent, activeToolSpans, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
18716
18953
  const {
18717
18954
  preToolUse,
18718
18955
  postToolUse,
@@ -18721,6 +18958,8 @@ function injectTracingHooks(options, resolveParentSpan, activeToolSpans, localTo
18721
18958
  subagentStop
18722
18959
  } = createToolTracingHooks(
18723
18960
  resolveParentSpan,
18961
+ taskIdToToolUseId,
18962
+ toolUseToParent,
18724
18963
  activeToolSpans,
18725
18964
  options.mcpServers,
18726
18965
  localToolHookNames,
@@ -18802,6 +19041,13 @@ async function finalizeCurrentMessageGroup(state) {
18802
19041
  }
18803
19042
  }
18804
19043
  const existingLlmSpan = state.activeLlmSpansByParentToolUse.get(parentKey);
19044
+ const lastMessage = state.currentMessages[state.currentMessages.length - 1];
19045
+ const messageId = lastMessage?.message?.id;
19046
+ const usage = state.options.includePartialMessages ? mergeUsage(
19047
+ copyUsage(lastMessage?.message?.usage),
19048
+ messageId ? state.usageByMessageId.get(messageId) : void 0
19049
+ ) : void 0;
19050
+ const hasFinalOutputUsage = messageId !== void 0 && state.finalOutputUsageMessageIds.has(messageId);
18805
19051
  const llmSpanResult = await createLLMSpanForMessages(
18806
19052
  state.currentMessages,
18807
19053
  promptMessages,
@@ -18809,6 +19055,8 @@ async function finalizeCurrentMessageGroup(state) {
18809
19055
  state.options,
18810
19056
  state.currentMessageStartTime,
18811
19057
  parentSpan,
19058
+ usage,
19059
+ hasFinalOutputUsage,
18812
19060
  existingLlmSpan
18813
19061
  );
18814
19062
  if (llmSpanResult) {
@@ -18826,9 +19074,17 @@ async function finalizeCurrentMessageGroup(state) {
18826
19074
  }
18827
19075
  }
18828
19076
  state.activeLlmSpansByParentToolUse.delete(parentKey);
18829
- const lastMessage = state.currentMessages[state.currentMessages.length - 1];
18830
- if (lastMessage?.message?.usage) {
18831
- state.accumulatedOutputTokens += getNumberProperty(lastMessage.message.usage, "output_tokens") || 0;
19077
+ if (messageId) {
19078
+ state.usageByMessageId.delete(messageId);
19079
+ state.finalOutputUsageMessageIds.delete(messageId);
19080
+ for (const [
19081
+ parent,
19082
+ activeMessageId
19083
+ ] of state.activePartialMessageIdByParentKey) {
19084
+ if (activeMessageId === messageId) {
19085
+ state.activePartialMessageIdByParentKey.delete(parent);
19086
+ }
19087
+ }
18832
19088
  }
18833
19089
  state.currentMessages.length = 0;
18834
19090
  }
@@ -18919,6 +19175,10 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
18919
19175
  );
18920
19176
  llmParentSpan = await subAgentSpan.export();
18921
19177
  }
19178
+ const racedLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
19179
+ if (racedLlmSpan) {
19180
+ return racedLlmSpan;
19181
+ }
18922
19182
  const llmSpan = startSpan(
18923
19183
  withSpanInstrumentationName(
18924
19184
  {
@@ -19038,7 +19298,49 @@ async function maybeHandleTaskLifecycleMessage(state, message) {
19038
19298
  }
19039
19299
  return true;
19040
19300
  }
19301
+ function handlePartialUsageMessage(state, message) {
19302
+ if (message.type !== "stream_event") {
19303
+ return false;
19304
+ }
19305
+ const event = message.event;
19306
+ if (!event || typeof event !== "object") {
19307
+ return true;
19308
+ }
19309
+ const parentKey = llmParentKey(message.parent_tool_use_id ?? null);
19310
+ if (event.type === "message_start") {
19311
+ const messageId2 = event.message?.id;
19312
+ const usage = copyUsage(event.message?.usage);
19313
+ if (messageId2) {
19314
+ state.activePartialMessageIdByParentKey.set(parentKey, messageId2);
19315
+ if (usage) {
19316
+ state.usageByMessageId.set(messageId2, usage);
19317
+ }
19318
+ }
19319
+ return true;
19320
+ }
19321
+ const messageId = state.activePartialMessageIdByParentKey.get(parentKey);
19322
+ if (!messageId) {
19323
+ return true;
19324
+ }
19325
+ if (event.type === "message_delta") {
19326
+ const update = copyUsage(event.usage);
19327
+ if (update) {
19328
+ const usage = state.usageByMessageId.get(messageId) ?? {};
19329
+ Object.assign(usage, update);
19330
+ state.usageByMessageId.set(messageId, usage);
19331
+ if (update.output_tokens !== void 0) {
19332
+ state.finalOutputUsageMessageIds.add(messageId);
19333
+ }
19334
+ }
19335
+ } else if (event.type === "message_stop") {
19336
+ state.activePartialMessageIdByParentKey.delete(parentKey);
19337
+ }
19338
+ return true;
19339
+ }
19041
19340
  async function handleStreamMessage(state, message) {
19341
+ if (handlePartialUsageMessage(state, message)) {
19342
+ return;
19343
+ }
19042
19344
  maybeTrackToolUseContext(state, message);
19043
19345
  if (await maybeHandleTaskLifecycleMessage(state, message)) {
19044
19346
  return;
@@ -19085,36 +19387,9 @@ async function handleStreamMessage(state, message) {
19085
19387
  );
19086
19388
  state.currentMessages.push(message);
19087
19389
  }
19088
- if (message.type !== "result" || !message.usage) {
19390
+ if (message.type !== "result") {
19089
19391
  return;
19090
19392
  }
19091
- const finalUsageMetrics = extractUsageFromMessage(message);
19092
- if (state.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
19093
- const lastMessage = state.currentMessages[state.currentMessages.length - 1];
19094
- if (lastMessage?.message?.usage) {
19095
- const adjustedTokens = finalUsageMetrics.completion_tokens - state.accumulatedOutputTokens;
19096
- if (adjustedTokens >= 0) {
19097
- lastMessage.message.usage.output_tokens = adjustedTokens;
19098
- }
19099
- const resultUsage = message.usage;
19100
- if (resultUsage && typeof resultUsage === "object") {
19101
- const cacheReadTokens = getNumberProperty(
19102
- resultUsage,
19103
- "cache_read_input_tokens"
19104
- );
19105
- if (cacheReadTokens !== void 0) {
19106
- lastMessage.message.usage.cache_read_input_tokens = cacheReadTokens;
19107
- }
19108
- const cacheCreationTokens = getNumberProperty(
19109
- resultUsage,
19110
- "cache_creation_input_tokens"
19111
- );
19112
- if (cacheCreationTokens !== void 0) {
19113
- lastMessage.message.usage.cache_creation_input_tokens = cacheCreationTokens;
19114
- }
19115
- }
19116
- }
19117
- }
19118
19393
  const metadata = {};
19119
19394
  if (message.num_turns !== void 0) {
19120
19395
  metadata.num_turns = message.num_turns;
@@ -19122,8 +19397,12 @@ async function handleStreamMessage(state, message) {
19122
19397
  if (message.session_id !== void 0) {
19123
19398
  metadata.session_id = message.session_id;
19124
19399
  }
19125
- if (Object.keys(metadata).length > 0) {
19126
- state.span.log({ metadata });
19400
+ const metrics = state.options.includePartialMessages ? {} : extractUsage(copyUsage(message.usage), true);
19401
+ if (Object.keys(metadata).length > 0 || Object.keys(metrics).length > 0) {
19402
+ state.span.log({
19403
+ ...Object.keys(metadata).length > 0 ? { metadata } : {},
19404
+ ...Object.keys(metrics).length > 0 ? { metrics } : {}
19405
+ });
19127
19406
  }
19128
19407
  }
19129
19408
  async function finalizeQuerySpan(state) {
@@ -19147,6 +19426,9 @@ async function finalizeQuerySpan(state) {
19147
19426
  llmSpan.end();
19148
19427
  }
19149
19428
  state.activeLlmSpansByParentToolUse.clear();
19429
+ state.activePartialMessageIdByParentKey.clear();
19430
+ state.finalOutputUsageMessageIds.clear();
19431
+ state.usageByMessageId.clear();
19150
19432
  for (const toolSpan of state.activeToolSpans.values()) {
19151
19433
  toolSpan.end();
19152
19434
  }
@@ -19271,6 +19553,8 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19271
19553
  const optionsWithHooks = injectTracingHooks(
19272
19554
  options,
19273
19555
  resolveToolUseParentSpan,
19556
+ taskIdToToolUseId,
19557
+ toolUseToParent,
19274
19558
  activeToolSpans,
19275
19559
  localToolHookNames,
19276
19560
  skipLocalToolHooks,
@@ -19281,8 +19565,8 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19281
19565
  params.options = optionsWithHooks;
19282
19566
  event.arguments[0] = params;
19283
19567
  spans.set(event, {
19284
- accumulatedOutputTokens: 0,
19285
19568
  activeLlmSpansByParentToolUse,
19569
+ activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
19286
19570
  activeToolSpans,
19287
19571
  conversationHistoryByParentKey,
19288
19572
  capturedPromptMessages,
@@ -19290,6 +19574,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19290
19574
  currentMessageStartTime: startTime,
19291
19575
  currentMessages: [],
19292
19576
  endedSubAgentSpans,
19577
+ finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
19293
19578
  finalResults: [],
19294
19579
  options: optionsWithHooks,
19295
19580
  originalPrompt,
@@ -19305,6 +19590,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19305
19590
  latestLlmParentBySubAgentToolUse,
19306
19591
  latestRootLlmParentRef,
19307
19592
  toolUseToParent,
19593
+ usageByMessageId: /* @__PURE__ */ new Map(),
19308
19594
  localToolContext
19309
19595
  });
19310
19596
  },
@@ -21779,9 +22065,9 @@ function extractEmbedPromptTokenCount(response) {
21779
22065
  let sawAny = false;
21780
22066
  for (const embedding of embeddings) {
21781
22067
  const embeddingStats = tryToDict(tryToDict(embedding)?.statistics);
21782
- const tokenCount = embeddingStats?.tokenCount;
21783
- if (typeof tokenCount === "number" && Number.isFinite(tokenCount)) {
21784
- total += tokenCount;
22068
+ const tokenCount2 = embeddingStats?.tokenCount;
22069
+ if (typeof tokenCount2 === "number" && Number.isFinite(tokenCount2)) {
22070
+ total += tokenCount2;
21785
22071
  sawAny = true;
21786
22072
  }
21787
22073
  }
@@ -28676,7 +28962,7 @@ function getStringProperty2(obj, key) {
28676
28962
  return typeof value === "string" ? value : void 0;
28677
28963
  }
28678
28964
  function extractMetricsFromUsage(usage) {
28679
- const metrics = {
28965
+ const rawMetrics = {
28680
28966
  prompt_tokens: usage.inputTokens,
28681
28967
  completion_tokens: usage.outputTokens,
28682
28968
  ...extractAnthropicCacheTokens(
@@ -28685,10 +28971,10 @@ function extractMetricsFromUsage(usage) {
28685
28971
  )
28686
28972
  };
28687
28973
  if (usage.reasoningTokens !== void 0) {
28688
- metrics.completion_reasoning_tokens = usage.reasoningTokens;
28689
- metrics.reasoning_tokens = usage.reasoningTokens;
28974
+ rawMetrics.completion_reasoning_tokens = usage.reasoningTokens;
28975
+ rawMetrics.reasoning_tokens = usage.reasoningTokens;
28690
28976
  }
28691
- Object.assign(metrics, finalizeAnthropicTokens(metrics));
28977
+ const metrics = finalizeAnthropicTokens(rawMetrics);
28692
28978
  const metadata = {
28693
28979
  model: usage.model
28694
28980
  };
@@ -29627,17 +29913,20 @@ var FlueObserveBridge = class {
29627
29913
  return;
29628
29914
  }
29629
29915
  const metadata = {
29916
+ ...event.runId ? this.runsById.get(event.runId)?.metadata : {},
29630
29917
  ...extractEventMetadata(event),
29631
29918
  "flue.operation": event.operationKind,
29632
29919
  provider: "flue"
29633
29920
  };
29634
29921
  const parent = this.parentSpanForEvent(event);
29635
- const span = startFlueSpan(parent, {
29922
+ const args = {
29636
29923
  name: `flue.${event.operationKind}`,
29637
29924
  spanAttributes: { type: "task" /* TASK */ },
29638
29925
  startTime: eventTime(event.timestamp),
29639
29926
  event: { metadata }
29640
- });
29927
+ };
29928
+ const runSpan = event.runId ? this.runsById.get(event.runId)?.span : void 0;
29929
+ const span = event.operationKind === "prompt" && (!parent || parent === runSpan) ? startFlueRootSpan(args) : startFlueSpan(parent, args);
29641
29930
  this.operationsById.set(event.operationId, { metadata, span });
29642
29931
  }
29643
29932
  handleOperation(event) {
@@ -29652,6 +29941,11 @@ var FlueObserveBridge = class {
29652
29941
  ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {},
29653
29942
  ...event.usage ? { "flue.usage": event.usage } : {}
29654
29943
  };
29944
+ const input = flueOperationInput(event);
29945
+ if (!state.loggedInput && input !== void 0) {
29946
+ safeLog3(state.span, { input });
29947
+ state.loggedInput = true;
29948
+ }
29655
29949
  this.finishPendingChildrenForOperation(event, output);
29656
29950
  safeLog3(state.span, {
29657
29951
  ...event.isError ? { error: toLoggedError(event.errorInfo ?? event.error) } : {},
@@ -29668,6 +29962,8 @@ var FlueObserveBridge = class {
29668
29962
  return;
29669
29963
  }
29670
29964
  const input = flueTurnRequestInput(event);
29965
+ const operation = event.operationId ? this.operationsById.get(event.operationId) : void 0;
29966
+ const turnInput = prepareFlueTurnInput(event, input, operation);
29671
29967
  const model = flueTurnRequestModel(event);
29672
29968
  const provider = flueTurnRequestProvider(event);
29673
29969
  const api = flueTurnRequestApi(event);
@@ -29680,8 +29976,7 @@ var FlueObserveBridge = class {
29680
29976
  ...provider ? { "flue.provider": provider } : {},
29681
29977
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {},
29682
29978
  ...reasoning ? { reasoning } : {},
29683
- ...input?.systemPrompt ? { "flue.system_prompt": input.systemPrompt } : {},
29684
- ...input?.tools ? { tools: input.tools } : {}
29979
+ ...turnInput.metadata
29685
29980
  };
29686
29981
  const parent = this.parentSpanForTurn(event);
29687
29982
  const span = startFlueSpan(parent, {
@@ -29689,11 +29984,14 @@ var FlueObserveBridge = class {
29689
29984
  spanAttributes: { type: "llm" /* LLM */ },
29690
29985
  startTime: eventTime(event.timestamp),
29691
29986
  event: {
29692
- input: input?.messages,
29987
+ input: turnInput.messages,
29693
29988
  metadata
29694
29989
  }
29695
29990
  });
29696
- this.logOperationInput(event.operationId, input?.messages ?? input);
29991
+ this.logOperationInput(
29992
+ event.operationId,
29993
+ latestUserMessageInput(input?.messages)
29994
+ );
29697
29995
  this.turnsByKey.set(key, { metadata, span });
29698
29996
  }
29699
29997
  handleTurn(event) {
@@ -29940,16 +30238,20 @@ var FlueObserveBridge = class {
29940
30238
  }
29941
30239
  startSyntheticOperation(event) {
29942
30240
  const metadata = {
30241
+ ...event.runId ? this.runsById.get(event.runId)?.metadata : {},
29943
30242
  ...extractEventMetadata(event),
29944
30243
  "flue.operation": event.operationKind,
29945
30244
  provider: "flue"
29946
30245
  };
29947
- const span = startFlueSpan(this.parentSpanForEvent(event), {
30246
+ const args = {
29948
30247
  name: `flue.${event.operationKind}`,
29949
30248
  spanAttributes: { type: "task" /* TASK */ },
29950
30249
  startTime: eventTime(event.timestamp),
29951
30250
  event: { metadata }
29952
- });
30251
+ };
30252
+ const parent = this.parentSpanForEvent(event);
30253
+ const runSpan = event.runId ? this.runsById.get(event.runId)?.span : void 0;
30254
+ const span = event.operationKind === "prompt" && (!parent || parent === runSpan) ? startFlueRootSpan(args) : startFlueSpan(parent, args);
29953
30255
  return { metadata, span };
29954
30256
  }
29955
30257
  startSyntheticTurn(event) {
@@ -30128,6 +30430,71 @@ function flueRunInput(event) {
30128
30430
  function flueTurnRequestInput(event) {
30129
30431
  return event.request?.input ?? event.input;
30130
30432
  }
30433
+ function prepareFlueTurnInput(event, input, operation) {
30434
+ const messages = input?.messages;
30435
+ const tracksUserTurn = event.purpose === "agent" && operation?.metadata["flue.operation"] === "prompt" && Array.isArray(messages);
30436
+ if (!tracksUserTurn) {
30437
+ return {
30438
+ messages,
30439
+ metadata: {
30440
+ ...input?.systemPrompt ? { "flue.system_prompt": input.systemPrompt } : {},
30441
+ ...input?.tools ? { tools: input.tools } : {}
30442
+ }
30443
+ };
30444
+ }
30445
+ const previous = operation.turnInputState;
30446
+ const previousMessageCount = previous?.messageCount ?? 0;
30447
+ const boundaryFingerprint = messages.length > 0 ? fingerprintJsonValue(messages[messages.length - 1]) : void 0;
30448
+ const continuesPreviousInput = previous !== void 0 && messages.length >= previousMessageCount && (previousMessageCount === 0 || previous.boundaryFingerprint !== void 0 && previous.boundaryFingerprint === fingerprintJsonValue(messages[previousMessageCount - 1]));
30449
+ const inputMode = previous === void 0 ? "full" : continuesPreviousInput ? "delta" : "reset";
30450
+ const systemPromptFingerprint = fingerprintJsonValue(input?.systemPrompt);
30451
+ const toolsFingerprint = fingerprintJsonValue(input?.tools);
30452
+ operation.turnInputState = {
30453
+ ...boundaryFingerprint !== void 0 ? { boundaryFingerprint } : {},
30454
+ messageCount: messages.length,
30455
+ ...systemPromptFingerprint !== void 0 ? { systemPromptFingerprint } : {},
30456
+ ...toolsFingerprint !== void 0 ? { toolsFingerprint } : {}
30457
+ };
30458
+ return {
30459
+ messages: continuesPreviousInput ? messages.slice(previousMessageCount) : messages,
30460
+ metadata: {
30461
+ "flue.input_mode": inputMode,
30462
+ ...continuesPreviousInput ? { "flue.input_message_offset": previousMessageCount } : {},
30463
+ ...input?.systemPrompt && (!continuesPreviousInput || systemPromptFingerprint !== previous?.systemPromptFingerprint) ? { "flue.system_prompt": input.systemPrompt } : {},
30464
+ ...input?.tools && (!continuesPreviousInput || toolsFingerprint !== previous?.toolsFingerprint) ? { tools: input.tools } : {}
30465
+ }
30466
+ };
30467
+ }
30468
+ function fingerprintJsonValue(value) {
30469
+ try {
30470
+ const serialized = JSON.stringify(value);
30471
+ if (serialized === void 0) {
30472
+ return void 0;
30473
+ }
30474
+ let hash = 2166136261;
30475
+ for (let i = 0; i < serialized.length; i++) {
30476
+ hash = Math.imul(hash ^ serialized.charCodeAt(i), 16777619);
30477
+ }
30478
+ return `${serialized.length}:${hash >>> 0}`;
30479
+ } catch {
30480
+ return void 0;
30481
+ }
30482
+ }
30483
+ function latestUserMessageInput(messages) {
30484
+ if (!messages) {
30485
+ return void 0;
30486
+ }
30487
+ for (let i = messages.length - 1; i >= 0; i--) {
30488
+ const message = messages[i];
30489
+ if (isObjectLike(message) && Reflect.get(message, "role") === "user") {
30490
+ return [message];
30491
+ }
30492
+ }
30493
+ return void 0;
30494
+ }
30495
+ function flueOperationInput(event) {
30496
+ return typeof event.agentInput?.text === "string" ? [{ content: event.agentInput.text, role: "user" }] : void 0;
30497
+ }
30131
30498
  function flueTurnRequestModel(event) {
30132
30499
  return event.request?.requestedModel ?? event.request?.model ?? event.model;
30133
30500
  }
@@ -30285,6 +30652,21 @@ function startFlueSpan(parent, args) {
30285
30652
  withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30286
30653
  );
30287
30654
  }
30655
+ function startFlueRootSpan(args) {
30656
+ const state = _internalGetGlobalState();
30657
+ const spanId = state.idGenerator.getSpanId();
30658
+ const rootSpanId = state.idGenerator.shareRootSpanId() ? spanId : state.idGenerator.getTraceId();
30659
+ return withCurrent(
30660
+ NOOP_SPAN,
30661
+ () => startSpan({
30662
+ ...withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE),
30663
+ parentSpanIds: { parentSpanIds: [], rootSpanId },
30664
+ spanId,
30665
+ state
30666
+ }),
30667
+ state
30668
+ );
30669
+ }
30288
30670
  function runWithCurrentSpanStore(span, next) {
30289
30671
  const state = _internalGetGlobalState();
30290
30672
  const contextManager = state?.contextManager;
@@ -32184,8 +32566,7 @@ function startAgentStream(event, activeChildParents) {
32184
32566
  ...extractAgentMetadata2(agent),
32185
32567
  ...extractModelMetadata3(model),
32186
32568
  "strands.operation": "Agent.stream",
32187
- provider: extractProvider(model),
32188
- ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
32569
+ provider: extractProvider(model)
32189
32570
  };
32190
32571
  const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
32191
32572
  const attachmentCache = createStrandsAttachmentCache();
@@ -32235,8 +32616,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
32235
32616
  const metadata = {
32236
32617
  "strands.operation": operation,
32237
32618
  provider: "strands",
32238
- ...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {},
32239
- ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
32619
+ ...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {}
32240
32620
  };
32241
32621
  const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
32242
32622
  const input = processStrandsInputAttachments(event.arguments[0]);
@@ -33022,6 +33402,320 @@ function logInstrumentationError5(context, error) {
33022
33402
  debugLogger.debug(`${context}:`, error);
33023
33403
  }
33024
33404
 
33405
+ // src/instrumentation/plugins/voyageai-channels.ts
33406
+ var voyageAIChannels = defineChannels(
33407
+ "voyageai",
33408
+ {
33409
+ embed: channel({
33410
+ channelName: "embed",
33411
+ kind: "async"
33412
+ }),
33413
+ multimodalEmbed: channel({
33414
+ channelName: "multimodalEmbed",
33415
+ kind: "async"
33416
+ }),
33417
+ rerank: channel({
33418
+ channelName: "rerank",
33419
+ kind: "async"
33420
+ }),
33421
+ contextualizedEmbed: channel({
33422
+ channelName: "contextualizedEmbed",
33423
+ kind: "async"
33424
+ })
33425
+ },
33426
+ { instrumentationName: INSTRUMENTATION_NAMES.VOYAGEAI }
33427
+ );
33428
+
33429
+ // src/instrumentation/plugins/voyageai-plugin.ts
33430
+ var RERANK_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
33431
+ "model",
33432
+ "returnDocuments",
33433
+ "topK",
33434
+ "truncation"
33435
+ ]);
33436
+ var VoyageAIPlugin = class extends BasePlugin {
33437
+ onEnable() {
33438
+ this.unsubscribers.push(
33439
+ interceptVoyageAICall(
33440
+ voyageAIChannels.embed,
33441
+ "voyageai.embed",
33442
+ extractTextEmbeddingInput,
33443
+ summarizeEmbeddingOutput,
33444
+ extractEmbeddingUsageMetrics
33445
+ ),
33446
+ interceptVoyageAICall(
33447
+ voyageAIChannels.multimodalEmbed,
33448
+ "voyageai.multimodalEmbed",
33449
+ extractMultimodalEmbeddingInput,
33450
+ summarizeEmbeddingOutput,
33451
+ extractEmbeddingUsageMetrics
33452
+ ),
33453
+ interceptVoyageAICall(
33454
+ voyageAIChannels.rerank,
33455
+ "voyageai.rerank",
33456
+ extractRerankInput,
33457
+ summarizeRerankOutput
33458
+ ),
33459
+ interceptVoyageAICall(
33460
+ voyageAIChannels.contextualizedEmbed,
33461
+ "voyageai.contextualizedEmbed",
33462
+ extractContextualizedEmbeddingInput,
33463
+ summarizeContextualizedEmbeddingOutput,
33464
+ extractEmbeddingUsageMetrics
33465
+ )
33466
+ );
33467
+ }
33468
+ onDisable() {
33469
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
33470
+ }
33471
+ };
33472
+ function interceptVoyageAICall(channel2, name, extractInput2, extractOutput2, extractMetrics2 = extractUsageMetrics3) {
33473
+ return channel2.intercept((target, thisArg, args) => {
33474
+ const invokeTarget = () => Reflect.apply(target, thisArg, args);
33475
+ if (isAutoInstrumentationSuppressed()) {
33476
+ return invokeTarget();
33477
+ }
33478
+ let span;
33479
+ try {
33480
+ const { input, metadata } = extractInput2(args);
33481
+ span = startSpan(
33482
+ withSpanInstrumentationName(
33483
+ {
33484
+ event: { input, metadata },
33485
+ name,
33486
+ spanAttributes: { type: "llm" /* LLM */ }
33487
+ },
33488
+ INSTRUMENTATION_NAMES.VOYAGEAI
33489
+ )
33490
+ );
33491
+ } catch (error) {
33492
+ debugLogger.error(`Error starting span for ${name}:`, error);
33493
+ return invokeTarget();
33494
+ }
33495
+ let result;
33496
+ try {
33497
+ result = withCurrent(
33498
+ span,
33499
+ () => runWithAutoInstrumentationSuppressed(invokeTarget)
33500
+ );
33501
+ } catch (error) {
33502
+ finishVoyageAISpan(span, name, () => span.log({ error }));
33503
+ throw error;
33504
+ }
33505
+ void Promise.resolve(result).then(
33506
+ (value) => finishVoyageAISpan(span, name, () => {
33507
+ const metadata = extractResponseMetadata3(value);
33508
+ span.log({
33509
+ output: extractOutput2(value),
33510
+ ...metadata ? { metadata } : {},
33511
+ metrics: extractMetrics2(value)
33512
+ });
33513
+ }),
33514
+ (error) => finishVoyageAISpan(span, name, () => span.log({ error }))
33515
+ );
33516
+ return result;
33517
+ });
33518
+ }
33519
+ function finishVoyageAISpan(span, name, log) {
33520
+ try {
33521
+ log();
33522
+ } catch (error) {
33523
+ debugLogger.error(`Error logging span for ${name}:`, error);
33524
+ }
33525
+ try {
33526
+ span.end();
33527
+ } catch (error) {
33528
+ debugLogger.error(`Error ending span for ${name}:`, error);
33529
+ }
33530
+ }
33531
+ function getRequestArg2(args) {
33532
+ if (Array.isArray(args)) {
33533
+ return isObject(args[0]) ? args[0] : void 0;
33534
+ }
33535
+ if (!isObject(args)) {
33536
+ return void 0;
33537
+ }
33538
+ const firstArg = Reflect.get(args, "0");
33539
+ return isObject(firstArg) ? firstArg : void 0;
33540
+ }
33541
+ function pickMetadata(request, allowlist) {
33542
+ const metadata = {};
33543
+ if (request) {
33544
+ for (const key of allowlist) {
33545
+ if (!Object.hasOwn(request, key)) {
33546
+ continue;
33547
+ }
33548
+ const value = request[key];
33549
+ if (value !== void 0) {
33550
+ metadata[key] = value;
33551
+ }
33552
+ }
33553
+ }
33554
+ return {
33555
+ ...metadata,
33556
+ provider: "voyage"
33557
+ };
33558
+ }
33559
+ function buildEmbeddingInput(inputs, request) {
33560
+ const outputDimensions = request?.outputDimension;
33561
+ return {
33562
+ inputs,
33563
+ ...typeof outputDimensions === "number" && Number.isFinite(outputDimensions) ? { output_dimensions: outputDimensions } : {}
33564
+ };
33565
+ }
33566
+ function embeddingMetadata(request) {
33567
+ return {
33568
+ ...typeof request?.model === "string" ? { model: request.model } : {},
33569
+ provider: "voyage"
33570
+ };
33571
+ }
33572
+ function extractTextEmbeddingInput(args) {
33573
+ const request = getRequestArg2(args);
33574
+ const rawInput = request?.input;
33575
+ const values = Array.isArray(rawInput) ? rawInput : [rawInput];
33576
+ return {
33577
+ input: buildEmbeddingInput(
33578
+ values.flatMap(
33579
+ (value) => typeof value === "string" ? [{ content: value }] : []
33580
+ ),
33581
+ request
33582
+ ),
33583
+ metadata: embeddingMetadata(request)
33584
+ };
33585
+ }
33586
+ function extractContextualizedEmbeddingInput(args) {
33587
+ const request = getRequestArg2(args);
33588
+ const rawInputs = request?.inputs;
33589
+ const values = Array.isArray(rawInputs) ? rawInputs.flatMap((value) => Array.isArray(value) ? value : [value]) : [];
33590
+ return {
33591
+ input: buildEmbeddingInput(
33592
+ values.flatMap(
33593
+ (value) => typeof value === "string" ? [{ content: value }] : []
33594
+ ),
33595
+ request
33596
+ ),
33597
+ metadata: embeddingMetadata(request)
33598
+ };
33599
+ }
33600
+ function extractMultimodalEmbeddingInput(args) {
33601
+ const request = getRequestArg2(args);
33602
+ const rawInputs = request?.inputs;
33603
+ const inputs = Array.isArray(rawInputs) ? rawInputs.map((rawInput) => {
33604
+ const rawContent = isObject(rawInput) ? rawInput.content : void 0;
33605
+ return {
33606
+ content: Array.isArray(rawContent) ? rawContent.flatMap(normalizeMultimodalContentPart) : []
33607
+ };
33608
+ }) : [];
33609
+ const input = buildEmbeddingInput(inputs, request);
33610
+ const processedInput = processInputAttachments(input);
33611
+ return {
33612
+ input: hasInlineEmbeddingMedia(processedInput) ? input : processedInput,
33613
+ metadata: embeddingMetadata(request)
33614
+ };
33615
+ }
33616
+ function normalizeMultimodalContentPart(part) {
33617
+ if (!isObject(part) || typeof part.type !== "string") {
33618
+ return [];
33619
+ }
33620
+ if (part.type === "text") {
33621
+ return typeof part.text === "string" ? [{ type: "text", text: part.text }] : [];
33622
+ }
33623
+ const camelCaseField = {
33624
+ image_base64: "imageBase64",
33625
+ image_url: "imageUrl",
33626
+ video_base64: "videoBase64",
33627
+ video_url: "videoUrl"
33628
+ };
33629
+ const field = camelCaseField[part.type];
33630
+ if (!field) {
33631
+ return [];
33632
+ }
33633
+ const data = typeof part[field] === "string" ? part[field] : part[part.type];
33634
+ if (typeof data !== "string") {
33635
+ return [];
33636
+ }
33637
+ return part.type.startsWith("image_") ? [{ type: "image_url", image_url: { url: data } }] : [{ type: "file", file: { file_data: data } }];
33638
+ }
33639
+ function hasInlineEmbeddingMedia(input) {
33640
+ return input.inputs.some(
33641
+ ({ content }) => Array.isArray(content) ? content.some((part) => {
33642
+ const value = part.type === "image_url" ? part.image_url.url : part.type === "file" ? part.file.file_data : void 0;
33643
+ return typeof value === "string" && value.startsWith("data:");
33644
+ }) : false
33645
+ );
33646
+ }
33647
+ function extractRerankInput(args) {
33648
+ const request = getRequestArg2(args);
33649
+ const documents = request?.documents;
33650
+ return {
33651
+ input: {
33652
+ documents,
33653
+ query: request?.query
33654
+ },
33655
+ metadata: {
33656
+ ...pickMetadata(request, RERANK_METADATA_ALLOWLIST),
33657
+ ...Array.isArray(documents) ? { document_count: documents.length } : {}
33658
+ }
33659
+ };
33660
+ }
33661
+ function extractResponseMetadata3(result) {
33662
+ if (!isObject(result)) {
33663
+ return void 0;
33664
+ }
33665
+ const rawResponse = isObject(result.rawResponse) ? result.rawResponse : void 0;
33666
+ const model = typeof result.model === "string" ? result.model : typeof rawResponse?.model === "string" ? rawResponse.model : void 0;
33667
+ return model ? { model } : void 0;
33668
+ }
33669
+ function summarizeEmbeddingOutput(result) {
33670
+ return {
33671
+ count: isObject(result) && Array.isArray(result.data) ? result.data.length : 0
33672
+ };
33673
+ }
33674
+ function summarizeRerankOutput(result) {
33675
+ if (!isObject(result) || !Array.isArray(result.data)) {
33676
+ return void 0;
33677
+ }
33678
+ return result.data.slice(0, 100).map((item) => ({
33679
+ index: isObject(item) ? item.index : void 0,
33680
+ relevance_score: isObject(item) ? (typeof item.relevanceScore === "number" ? item.relevanceScore : item.relevance_score) ?? null : null
33681
+ }));
33682
+ }
33683
+ function summarizeContextualizedEmbeddingOutput(result) {
33684
+ if (!isObject(result)) {
33685
+ return { count: 0 };
33686
+ }
33687
+ if (Array.isArray(result.results)) {
33688
+ return {
33689
+ count: result.results.reduce(
33690
+ (count, item) => count + (isObject(item) && Array.isArray(item.embeddings) ? item.embeddings.length : 0),
33691
+ 0
33692
+ )
33693
+ };
33694
+ }
33695
+ if (!Array.isArray(result.data)) {
33696
+ return { count: 0 };
33697
+ }
33698
+ return {
33699
+ count: result.data.reduce(
33700
+ (count, item) => count + (isObject(item) && Array.isArray(item.data) ? item.data.length : 0),
33701
+ 0
33702
+ )
33703
+ };
33704
+ }
33705
+ function extractEmbeddingUsageMetrics(result) {
33706
+ const metrics = extractUsageMetrics3(result);
33707
+ return typeof metrics.tokens === "number" ? { prompt_tokens: metrics.tokens, tokens: metrics.tokens } : {};
33708
+ }
33709
+ function extractUsageMetrics3(result) {
33710
+ if (!isObject(result)) {
33711
+ return {};
33712
+ }
33713
+ const rawResponse = isObject(result.rawResponse) ? result.rawResponse : void 0;
33714
+ const usage = isObject(result.usage) ? result.usage : isObject(rawResponse?.usage) ? rawResponse.usage : void 0;
33715
+ const tokens = typeof result.totalTokens === "number" ? result.totalTokens : usage?.totalTokens ?? usage?.total_tokens;
33716
+ return typeof tokens === "number" && Number.isFinite(tokens) && tokens >= 0 ? { tokens } : {};
33717
+ }
33718
+
33025
33719
  // src/instrumentation/plugins/cloudflare-ai-chat-channels.ts
33026
33720
  var cloudflareAIChatChannels = defineChannels(
33027
33721
  "@cloudflare/ai-chat",
@@ -33577,6 +34271,7 @@ var BraintrustPlugin = class extends BasePlugin {
33577
34271
  langSmithPlugin = null;
33578
34272
  piCodingAgentPlugin = null;
33579
34273
  strandsAgentSDKPlugin = null;
34274
+ voyageAIPlugin = null;
33580
34275
  cloudflareAIChatPlugin = null;
33581
34276
  cloudflareAgentsPlugin = null;
33582
34277
  constructor(config = {}) {
@@ -33651,6 +34346,10 @@ var BraintrustPlugin = class extends BasePlugin {
33651
34346
  this.coherePlugin = new CoherePlugin();
33652
34347
  this.coherePlugin.enable();
33653
34348
  }
34349
+ if (integrations.voyageai !== false) {
34350
+ this.voyageAIPlugin = new VoyageAIPlugin();
34351
+ this.voyageAIPlugin.enable();
34352
+ }
33654
34353
  if (integrations.groq !== false) {
33655
34354
  this.groqPlugin = new GroqPlugin();
33656
34355
  this.groqPlugin.enable();
@@ -33767,6 +34466,10 @@ var BraintrustPlugin = class extends BasePlugin {
33767
34466
  this.coherePlugin.disable();
33768
34467
  this.coherePlugin = null;
33769
34468
  }
34469
+ if (this.voyageAIPlugin) {
34470
+ this.voyageAIPlugin.disable();
34471
+ this.voyageAIPlugin = null;
34472
+ }
33770
34473
  if (this.groqPlugin) {
33771
34474
  this.groqPlugin.disable();
33772
34475
  this.groqPlugin = null;
@@ -35537,7 +36240,10 @@ var envIntegrationAliases = {
35537
36240
  "langchain-js": "langchain",
35538
36241
  "@langchain": "langchain",
35539
36242
  langgraph: "langgraph",
35540
- langsmith: "langsmith"
36243
+ langsmith: "langsmith",
36244
+ voyage: "voyageai",
36245
+ "voyage-ai": "voyageai",
36246
+ voyageai: "voyageai"
35541
36247
  };
35542
36248
  function getDefaultInstrumentationIntegrations() {
35543
36249
  return {
@@ -35572,6 +36278,7 @@ function getDefaultInstrumentationIntegrations() {
35572
36278
  langchain: true,
35573
36279
  langgraph: true,
35574
36280
  langsmith: true,
36281
+ voyageai: true,
35575
36282
  piCodingAgent: true,
35576
36283
  strandsAgentSDK: true,
35577
36284
  cloudflareAgents: true