braintrust 3.25.0 → 3.26.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 (78) hide show
  1. package/NOTICE +6 -0
  2. package/dev/dist/index.d.mts +130 -2
  3. package/dev/dist/index.d.ts +130 -2
  4. package/dev/dist/index.js +2455 -1116
  5. package/dev/dist/index.mjs +1599 -260
  6. package/dist/apply-auto-instrumentation.js +439 -398
  7. package/dist/apply-auto-instrumentation.mjs +242 -201
  8. package/dist/auto-instrumentations/bundler/esbuild.cjs +853 -337
  9. package/dist/auto-instrumentations/bundler/esbuild.d.mts +4 -4
  10. package/dist/auto-instrumentations/bundler/esbuild.d.ts +4 -4
  11. package/dist/auto-instrumentations/bundler/esbuild.mjs +5 -8
  12. package/dist/auto-instrumentations/bundler/next.cjs +852 -356
  13. package/dist/auto-instrumentations/bundler/next.mjs +6 -29
  14. package/dist/auto-instrumentations/bundler/rollup.cjs +853 -337
  15. package/dist/auto-instrumentations/bundler/rollup.d.mts +4 -4
  16. package/dist/auto-instrumentations/bundler/rollup.d.ts +4 -4
  17. package/dist/auto-instrumentations/bundler/rollup.mjs +5 -8
  18. package/dist/auto-instrumentations/bundler/vite.cjs +853 -337
  19. package/dist/auto-instrumentations/bundler/vite.d.mts +4 -4
  20. package/dist/auto-instrumentations/bundler/vite.d.ts +4 -4
  21. package/dist/auto-instrumentations/bundler/vite.mjs +5 -8
  22. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1006 -266
  23. package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +20 -12
  24. package/dist/auto-instrumentations/bundler/webpack.cjs +853 -337
  25. package/dist/auto-instrumentations/bundler/webpack.d.mts +4 -4
  26. package/dist/auto-instrumentations/bundler/webpack.d.ts +4 -4
  27. package/dist/auto-instrumentations/bundler/webpack.mjs +5 -4
  28. package/dist/auto-instrumentations/{chunk-JPVCUKTY.mjs → chunk-6E22MYSW.mjs} +165 -73
  29. package/dist/auto-instrumentations/{chunk-VT6DDNKM.mjs → chunk-AKKPLXTP.mjs} +631 -194
  30. package/dist/auto-instrumentations/chunk-NRE4QUQR.mjs +509 -0
  31. package/dist/auto-instrumentations/{chunk-7P6563SW.mjs → chunk-V5LEODRX.mjs} +2 -6
  32. package/dist/auto-instrumentations/{chunk-M6DLIJ2Z.mjs → chunk-XYN63IG5.mjs} +124 -194
  33. package/dist/auto-instrumentations/{chunk-CZ24KNHT.mjs → chunk-XZHHE4Y3.mjs} +77 -68
  34. package/dist/auto-instrumentations/hook.mjs +247 -182
  35. package/dist/auto-instrumentations/index.cjs +557 -48
  36. package/dist/auto-instrumentations/index.d.mts +6 -2
  37. package/dist/auto-instrumentations/index.d.ts +6 -2
  38. package/dist/auto-instrumentations/index.mjs +6 -1
  39. package/dist/auto-instrumentations/loader/cjs-patch.cjs +492 -195
  40. package/dist/auto-instrumentations/loader/cjs-patch.d.mts +2 -2
  41. package/dist/auto-instrumentations/loader/cjs-patch.d.ts +2 -2
  42. package/dist/auto-instrumentations/loader/cjs-patch.mjs +3 -2
  43. package/dist/auto-instrumentations/loader/esm-hook.mjs +1 -1
  44. package/dist/auto-instrumentations/plugin-B-aZh7EP.d.ts +30 -0
  45. package/dist/auto-instrumentations/plugin-Bnj4E_6B.d.mts +30 -0
  46. package/dist/auto-instrumentations/{types-RNPaKi9o.d.mts → types-C1fVeiCp.d.mts} +1 -1
  47. package/dist/auto-instrumentations/{types-RNPaKi9o.d.ts → types-C1fVeiCp.d.ts} +1 -1
  48. package/dist/browser.d.mts +817 -197
  49. package/dist/browser.d.ts +817 -197
  50. package/dist/browser.js +1883 -265
  51. package/dist/browser.mjs +1883 -265
  52. package/dist/{chunk-ABR2QWDP.mjs → chunk-PN7EWK66.mjs} +581 -133
  53. package/dist/{chunk-GSIDVFE6.js → chunk-VRZZQPAA.js} +1670 -736
  54. package/dist/{chunk-P25IOOU4.js → chunk-VYNNEKSA.js} +586 -138
  55. package/dist/{chunk-2SANLSWX.mjs → chunk-XIZOM2HO.mjs} +1052 -118
  56. package/dist/cli.js +1601 -263
  57. package/dist/edge-light.d.mts +1 -1
  58. package/dist/edge-light.d.ts +1 -1
  59. package/dist/edge-light.js +1883 -268
  60. package/dist/edge-light.mjs +1883 -268
  61. package/dist/index.d.mts +817 -197
  62. package/dist/index.d.ts +817 -197
  63. package/dist/index.js +695 -435
  64. package/dist/index.mjs +264 -4
  65. package/dist/instrumentation/index.d.mts +130 -6
  66. package/dist/instrumentation/index.d.ts +130 -6
  67. package/dist/instrumentation/index.js +1510 -80
  68. package/dist/instrumentation/index.mjs +1510 -80
  69. package/dist/vitest-evals-reporter.js +16 -16
  70. package/dist/vitest-evals-reporter.mjs +2 -2
  71. package/dist/workerd.d.mts +1 -1
  72. package/dist/workerd.d.ts +1 -1
  73. package/dist/workerd.js +1883 -268
  74. package/dist/workerd.mjs +1883 -268
  75. package/licenses/node-diagnostics-channel/LICENSE +19 -0
  76. package/package.json +34 -35
  77. package/dist/auto-instrumentations/plugin-D0KHwSJv.d.mts +0 -44
  78. package/dist/auto-instrumentations/plugin-G6fgxk1b.d.ts +0 -44
@@ -1,9 +1,12 @@
1
1
  import {
2
+ GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
2
3
  INSTRUMENTATION_NAMES,
3
4
  aiSDKChannels,
4
5
  anthropicChannels,
5
6
  bedrockRuntimeChannels,
6
7
  claudeAgentSDKChannels,
8
+ cloudflareAIChatChannels,
9
+ cloudflareAgentsChannels,
7
10
  cohereChannels,
8
11
  cursorSDKChannels,
9
12
  detectSpanOriginEnvironment,
@@ -11,6 +14,7 @@ import {
11
14
  genkitChannels,
12
15
  genkitCoreChannels,
13
16
  getDefaultInstrumentationIntegrations,
17
+ getHuggingFaceTransformersPipelineInfo,
14
18
  getSpanInstrumentationName,
15
19
  gitHubCopilotChannels,
16
20
  googleADKChannels,
@@ -18,8 +22,10 @@ import {
18
22
  groqChannels,
19
23
  harnessAgentChannels,
20
24
  huggingFaceChannels,
25
+ huggingFaceTransformersChannels,
21
26
  installMastraExporterFactory,
22
27
  isInstrumentationIntegrationDisabled,
28
+ isSupportedHuggingFaceTransformersTask,
23
29
  isomorph_default,
24
30
  langChainChannels,
25
31
  langSmithChannels,
@@ -30,14 +36,15 @@ import {
30
36
  openAICodexChannels,
31
37
  openRouterAgentChannels,
32
38
  openRouterChannels,
33
- patchTracingChannel,
34
39
  piCodingAgentChannels,
35
40
  readDisabledInstrumentationEnvConfig,
41
+ registerHuggingFaceTransformersPipeline,
42
+ setGlobalHookErrorReporter,
36
43
  smithyClientChannels,
37
44
  smithyCoreChannels,
38
45
  strandsAgentSDKChannels,
39
46
  withSpanInstrumentationName
40
- } from "./chunk-ABR2QWDP.mjs";
47
+ } from "./chunk-PN7EWK66.mjs";
41
48
 
42
49
  // src/id-gen.ts
43
50
  import { v4 as uuidv4 } from "uuid";
@@ -150,6 +157,9 @@ var debugLogger = {
150
157
  return createDebugLogger(state);
151
158
  }
152
159
  };
160
+ setGlobalHookErrorReporter((error) => {
161
+ debugLogger.error("Global instrumentation hook error:", error);
162
+ });
153
163
 
154
164
  // src/id-gen.ts
155
165
  var IDGenerator = class {
@@ -1877,7 +1887,8 @@ var ApiKey = z6.object({
1877
1887
  user_email: z6.union([z6.string(), z6.null()]).optional(),
1878
1888
  user_given_name: z6.union([z6.string(), z6.null()]).optional(),
1879
1889
  user_family_name: z6.union([z6.string(), z6.null()]).optional(),
1880
- org_id: z6.union([z6.string(), z6.null()]).optional()
1890
+ org_id: z6.union([z6.string(), z6.null()]).optional(),
1891
+ expires_at: z6.union([z6.string(), z6.null()]).optional()
1881
1892
  });
1882
1893
  var TriggeredFunctionState = z6.object({
1883
1894
  triggered_xact_id: z6.string(),
@@ -1997,6 +2008,7 @@ var TopicMapData = z6.object({
1997
2008
  topic_names: z6.record(z6.string()).optional(),
1998
2009
  generation_settings: TopicMapGenerationSettings.optional(),
1999
2010
  disable_reconciliation: z6.boolean().optional(),
2011
+ reconcile_mode: z6.enum(["evolve", "names_only"]).optional(),
2000
2012
  distance_threshold: z6.number().optional(),
2001
2013
  btql_filter: z6.string().optional(),
2002
2014
  automation_btql_filter: z6.string().optional()
@@ -2577,7 +2589,12 @@ var ModelParams = z6.union([
2577
2589
  }).partial().passthrough()
2578
2590
  ]);
2579
2591
  var PromptOptionsNullish = z6.union([
2580
- z6.object({ model: z6.string(), params: ModelParams, position: z6.string() }).partial(),
2592
+ z6.object({
2593
+ model: z6.string(),
2594
+ params: ModelParams,
2595
+ position: z6.string(),
2596
+ endpoint_name: z6.union([z6.string(), z6.null()])
2597
+ }).partial(),
2581
2598
  z6.null()
2582
2599
  ]);
2583
2600
  var PromptParserNullish = z6.union([
@@ -2591,11 +2608,25 @@ var PromptParserNullish = z6.union([
2591
2608
  }),
2592
2609
  z6.null()
2593
2610
  ]);
2611
+ var PreprocessorSavedFunctionId = z6.union([
2612
+ z6.object({
2613
+ type: z6.literal("function"),
2614
+ id: z6.string(),
2615
+ version: z6.string().optional()
2616
+ }),
2617
+ z6.object({
2618
+ type: z6.literal("global"),
2619
+ name: z6.string(),
2620
+ function_type: z6.literal("preprocessor").optional().default("preprocessor")
2621
+ }),
2622
+ z6.null()
2623
+ ]);
2594
2624
  var PromptDataNullish = z6.union([
2595
2625
  z6.object({
2596
2626
  prompt: PromptBlockDataNullish,
2597
2627
  options: PromptOptionsNullish,
2598
2628
  parser: PromptParserNullish,
2629
+ preprocessor: PreprocessorSavedFunctionId,
2599
2630
  tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
2600
2631
  template_format: z6.union([
2601
2632
  z6.enum(["mustache", "nunjucks", "none"]),
@@ -2797,6 +2828,7 @@ var PromptData = z6.object({
2797
2828
  prompt: PromptBlockDataNullish,
2798
2829
  options: PromptOptionsNullish,
2799
2830
  parser: PromptParserNullish,
2831
+ preprocessor: PreprocessorSavedFunctionId,
2800
2832
  tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
2801
2833
  template_format: z6.union([
2802
2834
  z6.enum(["mustache", "nunjucks", "none"]),
@@ -2959,7 +2991,8 @@ var InvokeFunction = FunctionId.and(
2959
2991
  mode: StreamingMode,
2960
2992
  strict: z6.union([z6.boolean(), z6.null()]),
2961
2993
  mcp_auth: z6.record(z6.object({ oauth_token: z6.string() }).partial()),
2962
- overrides: z6.union([z6.object({}).partial().passthrough(), z6.null()])
2994
+ overrides: z6.union([z6.object({}).partial().passthrough(), z6.null()]),
2995
+ endpoint_name: z6.union([z6.string(), z6.null()])
2963
2996
  }).partial()
2964
2997
  );
2965
2998
  var MCPServer = z6.object({
@@ -3023,6 +3056,24 @@ var Organization = z6.object({
3023
3056
  created: z6.union([z6.string(), z6.null()]).optional(),
3024
3057
  image_rendering_mode: ImageRenderingMode.optional()
3025
3058
  });
3059
+ var RetentionObjectType = z6.enum([
3060
+ "project_logs",
3061
+ "experiment",
3062
+ "dataset"
3063
+ ]);
3064
+ var OrgAutomation = z6.object({
3065
+ id: z6.string().uuid(),
3066
+ org_id: z6.string().uuid(),
3067
+ user_id: z6.union([z6.string(), z6.null()]).optional(),
3068
+ created: z6.union([z6.string(), z6.null()]).optional(),
3069
+ name: z6.string(),
3070
+ description: z6.union([z6.string(), z6.null()]).optional(),
3071
+ config: z6.object({
3072
+ event_type: z6.literal("retention"),
3073
+ object_type: RetentionObjectType,
3074
+ retention_days: z6.number().int().gte(0)
3075
+ })
3076
+ });
3026
3077
  var ProjectSettings = z6.union([
3027
3078
  z6.object({
3028
3079
  comparison_key: z6.union([z6.string(), z6.null()]),
@@ -3049,6 +3100,7 @@ var ProjectSettings = z6.union([
3049
3100
  z6.null()
3050
3101
  ]),
3051
3102
  disable_realtime_queries: z6.union([z6.boolean(), z6.null()]),
3103
+ monitor_charts_use_metrics_start: z6.union([z6.boolean(), z6.null()]),
3052
3104
  default_preprocessor: NullableSavedFunctionId
3053
3105
  }).partial(),
3054
3106
  z6.null()
@@ -3063,11 +3115,6 @@ var Project = z6.object({
3063
3115
  user_id: z6.union([z6.string(), z6.null()]).optional(),
3064
3116
  settings: ProjectSettings.optional()
3065
3117
  });
3066
- var RetentionObjectType = z6.enum([
3067
- "project_logs",
3068
- "experiment",
3069
- "dataset"
3070
- ]);
3071
3118
  var TopicAutomationFacetModel = z6.union([
3072
3119
  z6.enum(["brain-facet-latest", "brain-facet-1", "brain-facet-2"]),
3073
3120
  z6.null()
@@ -3178,7 +3225,7 @@ var ProjectAutomation = z6.object({
3178
3225
  z6.object({
3179
3226
  event_type: z6.literal("retention"),
3180
3227
  object_type: RetentionObjectType,
3181
- retention_days: z6.number().gte(0)
3228
+ retention_days: z6.number().int().gte(0)
3182
3229
  }),
3183
3230
  z6.object({
3184
3231
  event_type: z6.literal("environment_update"),
@@ -3350,7 +3397,12 @@ var Prompt = z6.object({
3350
3397
  metadata: z6.union([z6.object({}).partial().passthrough(), z6.null()]).optional(),
3351
3398
  function_type: FunctionTypeEnumNullish.optional()
3352
3399
  });
3353
- var PromptOptions = z6.object({ model: z6.string(), params: ModelParams, position: z6.string() }).partial();
3400
+ var PromptOptions = z6.object({
3401
+ model: z6.string(),
3402
+ params: ModelParams,
3403
+ position: z6.string(),
3404
+ endpoint_name: z6.union([z6.string(), z6.null()])
3405
+ }).partial();
3354
3406
  var PromptSessionEvent = z6.object({
3355
3407
  id: z6.string(),
3356
3408
  _xact_id: z6.string(),
@@ -3433,7 +3485,8 @@ var RunEval = z6.object({
3433
3485
  stop_token: z6.union([z6.string(), z6.null()]).optional(),
3434
3486
  extra_messages: z6.string().optional(),
3435
3487
  tags: z6.array(z6.string()).optional(),
3436
- mcp_auth: z6.record(z6.object({ oauth_token: z6.string() }).partial()).optional()
3488
+ mcp_auth: z6.record(z6.object({ oauth_token: z6.string() }).partial()).optional(),
3489
+ endpoint_name: z6.union([z6.string(), z6.null()]).optional()
3437
3490
  });
3438
3491
  var ServiceToken = z6.object({
3439
3492
  id: z6.string().uuid(),
@@ -3443,7 +3496,8 @@ var ServiceToken = z6.object({
3443
3496
  service_account_id: z6.union([z6.string(), z6.null()]).optional(),
3444
3497
  service_account_email: z6.union([z6.string(), z6.null()]).optional(),
3445
3498
  service_account_name: z6.union([z6.string(), z6.null()]).optional(),
3446
- org_id: z6.union([z6.string(), z6.null()]).optional()
3499
+ org_id: z6.union([z6.string(), z6.null()]).optional(),
3500
+ expires_at: z6.union([z6.string(), z6.null()]).optional()
3447
3501
  });
3448
3502
  var SpanIFrame = z6.object({
3449
3503
  id: z6.string().uuid(),
@@ -3565,6 +3619,13 @@ var ViewOptions = z6.union([
3565
3619
  }),
3566
3620
  z6.null()
3567
3621
  ]),
3622
+ pointSizeMetric: z6.union([
3623
+ z6.object({
3624
+ type: z6.enum(["none", "score", "metric", "metadata"]),
3625
+ value: z6.string()
3626
+ }),
3627
+ z6.null()
3628
+ ]),
3568
3629
  xAxisAggregation: z6.union([z6.string(), z6.null()]),
3569
3630
  chartAnnotations: z6.union([
3570
3631
  z6.array(z6.object({ id: z6.string(), text: z6.string() })),
@@ -4847,12 +4908,13 @@ function applyMaskingToField(maskingFunction, data, fieldName) {
4847
4908
  var INITIAL_SPAN_WRITE_AS_MERGE = /* @__PURE__ */ Symbol(
4848
4909
  "braintrust.initial-span-write-as-merge"
4849
4910
  );
4911
+ var INTERNAL_SPAN_CONTEXT = /* @__PURE__ */ Symbol("braintrust.internal-span-context");
4850
4912
  var BRAINTRUST_CURRENT_SPAN_STORE = /* @__PURE__ */ Symbol.for(
4851
4913
  "braintrust.currentSpanStore"
4852
4914
  );
4853
4915
  var ContextManager = class {
4854
4916
  /**
4855
- * Returns the value to store in the ALS bound to a TracingChannel's start event.
4917
+ * Returns the value to store in the ALS bound to a global hook's start event.
4856
4918
  * In default mode this is the Span itself; in OTEL mode it is the OTEL Context
4857
4919
  * containing the span so that OTEL's own ALS stores a proper Context object.
4858
4920
  */
@@ -8585,6 +8647,12 @@ function _internalStartSpanWithInitialMerge(args) {
8585
8647
  [INITIAL_SPAN_WRITE_AS_MERGE]: true
8586
8648
  }).span;
8587
8649
  }
8650
+ function _internalStartSpanWithContext(args, context) {
8651
+ return startSpanAndIsLogger({
8652
+ ...args,
8653
+ [INTERNAL_SPAN_CONTEXT]: context
8654
+ }).span;
8655
+ }
8588
8656
  async function flush(options) {
8589
8657
  const state = options?.state ?? _globalState;
8590
8658
  return await state.bgLogger().flush();
@@ -9486,7 +9554,7 @@ var SpanImpl = class _SpanImpl {
9486
9554
  start: args.startTime ?? getCurrentUnixTimestamp()
9487
9555
  },
9488
9556
  context: mergeSpanOriginContext(
9489
- { ...callerLocation },
9557
+ { ...callerLocation, ...args[INTERNAL_SPAN_CONTEXT] },
9490
9558
  instrumentationName,
9491
9559
  this._state.spanOriginEnvironment
9492
9560
  ),
@@ -11481,8 +11549,8 @@ function runWithAutoInstrumentationSuppressed(callback) {
11481
11549
  callback
11482
11550
  );
11483
11551
  }
11484
- function bindAutoInstrumentationSuppressionToStart(tracingChannel2) {
11485
- const startChannel = tracingChannel2.start;
11552
+ function bindAutoInstrumentationSuppressionToStart(tracingChannel) {
11553
+ const startChannel = tracingChannel.start;
11486
11554
  if (!startChannel) {
11487
11555
  return void 0;
11488
11556
  }
@@ -11593,9 +11661,9 @@ function ensureSpanStateForEvent(states, config, event, channelName, instrumenta
11593
11661
  states.set(key, created);
11594
11662
  return created;
11595
11663
  }
11596
- function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName, instrumentationName) {
11664
+ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName, instrumentationName) {
11597
11665
  const state = _internalGetGlobalState();
11598
- const startChannel = tracingChannel2.start;
11666
+ const startChannel = tracingChannel.start;
11599
11667
  const contextManager = state?.contextManager;
11600
11668
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
11601
11669
  if (!currentSpanStore || !startChannel) {
@@ -11678,11 +11746,11 @@ function runStreamingErrorHook(args) {
11678
11746
  }
11679
11747
  }
11680
11748
  function traceAsyncChannel(channel, config) {
11681
- const tracingChannel2 = channel.tracingChannel();
11749
+ const tracingChannel = channel.tracingChannel();
11682
11750
  const states = /* @__PURE__ */ new WeakMap();
11683
11751
  const channelName = channel.channelName;
11684
11752
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart(
11685
- tracingChannel2,
11753
+ tracingChannel,
11686
11754
  states,
11687
11755
  config,
11688
11756
  channelName,
@@ -11738,18 +11806,18 @@ function traceAsyncChannel(channel, config) {
11738
11806
  logErrorAndEnd(states, event, channelName);
11739
11807
  }
11740
11808
  };
11741
- tracingChannel2.subscribe(handlers);
11809
+ tracingChannel.subscribe(handlers);
11742
11810
  return () => {
11743
11811
  unbindCurrentSpanStore?.();
11744
- tracingChannel2.unsubscribe(handlers);
11812
+ tracingChannel.unsubscribe(handlers);
11745
11813
  };
11746
11814
  }
11747
11815
  function traceStreamingChannel(channel, config) {
11748
- const tracingChannel2 = channel.tracingChannel();
11816
+ const tracingChannel = channel.tracingChannel();
11749
11817
  const states = /* @__PURE__ */ new WeakMap();
11750
11818
  const channelName = channel.channelName;
11751
11819
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart(
11752
- tracingChannel2,
11820
+ tracingChannel,
11753
11821
  states,
11754
11822
  config,
11755
11823
  channelName,
@@ -11960,18 +12028,18 @@ function traceStreamingChannel(channel, config) {
11960
12028
  }
11961
12029
  }
11962
12030
  };
11963
- tracingChannel2.subscribe(handlers);
12031
+ tracingChannel.subscribe(handlers);
11964
12032
  return () => {
11965
12033
  unbindCurrentSpanStore?.();
11966
- tracingChannel2.unsubscribe(handlers);
12034
+ tracingChannel.unsubscribe(handlers);
11967
12035
  };
11968
12036
  }
11969
12037
  function traceSyncStreamChannel(channel, config) {
11970
- const tracingChannel2 = channel.tracingChannel();
12038
+ const tracingChannel = channel.tracingChannel();
11971
12039
  const states = /* @__PURE__ */ new WeakMap();
11972
12040
  const channelName = channel.channelName;
11973
12041
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart(
11974
- tracingChannel2,
12042
+ tracingChannel,
11975
12043
  states,
11976
12044
  config,
11977
12045
  channelName,
@@ -12084,10 +12152,10 @@ function traceSyncStreamChannel(channel, config) {
12084
12152
  logErrorAndEnd(states, event, channelName);
12085
12153
  }
12086
12154
  };
12087
- tracingChannel2.subscribe(handlers);
12155
+ tracingChannel.subscribe(handlers);
12088
12156
  return () => {
12089
12157
  unbindCurrentSpanStore?.();
12090
- tracingChannel2.unsubscribe(handlers);
12158
+ tracingChannel.unsubscribe(handlers);
12091
12159
  };
12092
12160
  }
12093
12161
  function unsubscribeAll(unsubscribers) {
@@ -12560,8 +12628,8 @@ function registerHarnessSessionParent(session, parent) {
12560
12628
  function currentHarnessTurnParent() {
12561
12629
  return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
12562
12630
  }
12563
- function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
12564
- const startChannel = tracingChannel2.start;
12631
+ function bindHarnessTurnParentToStart(tracingChannel, parentFromEvent) {
12632
+ const startChannel = tracingChannel.start;
12565
12633
  if (!startChannel) {
12566
12634
  return () => {
12567
12635
  };
@@ -19024,7 +19092,7 @@ var AnthropicPlugin = class extends BasePlugin {
19024
19092
  );
19025
19093
  }
19026
19094
  subscribeToAnthropicToolRunner() {
19027
- const tracingChannel2 = anthropicChannels.betaMessagesToolRunner.tracingChannel();
19095
+ const tracingChannel = anthropicChannels.betaMessagesToolRunner.tracingChannel();
19028
19096
  const states = /* @__PURE__ */ new WeakMap();
19029
19097
  const handlers = {
19030
19098
  start: (event) => {
@@ -19081,9 +19149,9 @@ var AnthropicPlugin = class extends BasePlugin {
19081
19149
  states.delete(event);
19082
19150
  }
19083
19151
  };
19084
- tracingChannel2.subscribe(handlers);
19152
+ tracingChannel.subscribe(handlers);
19085
19153
  this.unsubscribers.push(() => {
19086
- tracingChannel2.unsubscribe(handlers);
19154
+ tracingChannel.unsubscribe(handlers);
19087
19155
  });
19088
19156
  }
19089
19157
  };
@@ -21168,7 +21236,7 @@ var CursorSDKPlugin = class extends BasePlugin {
21168
21236
  this.subscribeToAgentFactory(cursorSDKChannels.resume);
21169
21237
  }
21170
21238
  subscribeToAgentFactory(channel) {
21171
- const tracingChannel2 = channel.tracingChannel();
21239
+ const tracingChannel = channel.tracingChannel();
21172
21240
  const handlers = {
21173
21241
  asyncEnd: (event) => {
21174
21242
  patchCursorAgentInPlace(event.result);
@@ -21176,9 +21244,9 @@ var CursorSDKPlugin = class extends BasePlugin {
21176
21244
  error: () => {
21177
21245
  }
21178
21246
  };
21179
- tracingChannel2.subscribe(handlers);
21247
+ tracingChannel.subscribe(handlers);
21180
21248
  this.unsubscribers.push(() => {
21181
- tracingChannel2.unsubscribe(handlers);
21249
+ tracingChannel.unsubscribe(handlers);
21182
21250
  });
21183
21251
  }
21184
21252
  subscribeToPrompt() {
@@ -22486,10 +22554,10 @@ var GoogleGenAIPlugin = class extends BasePlugin {
22486
22554
  this.subscribeToInteractionsCreateChannel();
22487
22555
  }
22488
22556
  subscribeToGenerateContentChannel() {
22489
- const tracingChannel2 = googleGenAIChannels.generateContent.tracingChannel();
22557
+ const tracingChannel = googleGenAIChannels.generateContent.tracingChannel();
22490
22558
  const states = /* @__PURE__ */ new WeakMap();
22491
22559
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart2(
22492
- tracingChannel2,
22560
+ tracingChannel,
22493
22561
  states,
22494
22562
  (event) => {
22495
22563
  const params = event.arguments[0];
@@ -22563,14 +22631,14 @@ var GoogleGenAIPlugin = class extends BasePlugin {
22563
22631
  logErrorAndEndSpan(states, event);
22564
22632
  }
22565
22633
  };
22566
- tracingChannel2.subscribe(handlers);
22634
+ tracingChannel.subscribe(handlers);
22567
22635
  this.unsubscribers.push(() => {
22568
22636
  unbindCurrentSpanStore?.();
22569
- tracingChannel2.unsubscribe(handlers);
22637
+ tracingChannel.unsubscribe(handlers);
22570
22638
  });
22571
22639
  }
22572
22640
  subscribeToGenerateContentStreamChannel() {
22573
- const tracingChannel2 = googleGenAIChannels.generateContentStream.tracingChannel();
22641
+ const tracingChannel = googleGenAIChannels.generateContentStream.tracingChannel();
22574
22642
  const handlers = {
22575
22643
  start: (event) => {
22576
22644
  const streamEvent = event;
@@ -22591,16 +22659,16 @@ var GoogleGenAIPlugin = class extends BasePlugin {
22591
22659
  error: () => {
22592
22660
  }
22593
22661
  };
22594
- tracingChannel2.subscribe(handlers);
22662
+ tracingChannel.subscribe(handlers);
22595
22663
  this.unsubscribers.push(() => {
22596
- tracingChannel2.unsubscribe(handlers);
22664
+ tracingChannel.unsubscribe(handlers);
22597
22665
  });
22598
22666
  }
22599
22667
  subscribeToEmbedContentChannel() {
22600
- const tracingChannel2 = googleGenAIChannels.embedContent.tracingChannel();
22668
+ const tracingChannel = googleGenAIChannels.embedContent.tracingChannel();
22601
22669
  const states = /* @__PURE__ */ new WeakMap();
22602
22670
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart2(
22603
- tracingChannel2,
22671
+ tracingChannel,
22604
22672
  states,
22605
22673
  (event) => {
22606
22674
  const params = event.arguments[0];
@@ -22670,10 +22738,10 @@ var GoogleGenAIPlugin = class extends BasePlugin {
22670
22738
  logErrorAndEndSpan(states, event);
22671
22739
  }
22672
22740
  };
22673
- tracingChannel2.subscribe(handlers);
22741
+ tracingChannel.subscribe(handlers);
22674
22742
  this.unsubscribers.push(() => {
22675
22743
  unbindCurrentSpanStore?.();
22676
- tracingChannel2.unsubscribe(handlers);
22744
+ tracingChannel.unsubscribe(handlers);
22677
22745
  });
22678
22746
  }
22679
22747
  subscribeToInteractionsCreateChannel() {
@@ -22709,10 +22777,10 @@ function ensureSpanState(states, event, create) {
22709
22777
  states.set(event, created);
22710
22778
  return created;
22711
22779
  }
22712
- function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
22780
+ function bindCurrentSpanStoreToStart2(tracingChannel, states, create) {
22713
22781
  const state = _internalGetGlobalState();
22714
22782
  const contextManager = state?.contextManager;
22715
- const startChannel = tracingChannel2.start;
22783
+ const startChannel = tracingChannel.start;
22716
22784
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
22717
22785
  if (!startChannel?.bindStore || !currentSpanStore) {
22718
22786
  return void 0;
@@ -23215,11 +23283,11 @@ function summarizeEmbedContentOutput(response) {
23215
23283
  };
23216
23284
  }
23217
23285
  function populateUsageMetrics(metrics, usage) {
23218
- if (usage.promptTokenCount !== void 0) {
23219
- metrics.prompt_tokens = usage.promptTokenCount;
23286
+ if (usage.promptTokenCount !== void 0 || usage.toolUsePromptTokenCount !== void 0) {
23287
+ metrics.prompt_tokens = (usage.promptTokenCount ?? 0) + (usage.toolUsePromptTokenCount ?? 0);
23220
23288
  }
23221
- if (usage.candidatesTokenCount !== void 0) {
23222
- metrics.completion_tokens = usage.candidatesTokenCount;
23289
+ if (usage.candidatesTokenCount !== void 0 || usage.thoughtsTokenCount !== void 0) {
23290
+ metrics.completion_tokens = (usage.candidatesTokenCount ?? 0) + (usage.thoughtsTokenCount ?? 0);
23223
23291
  }
23224
23292
  if (usage.totalTokenCount !== void 0) {
23225
23293
  metrics.tokens = usage.totalTokenCount;
@@ -23230,13 +23298,26 @@ function populateUsageMetrics(metrics, usage) {
23230
23298
  if (usage.thoughtsTokenCount !== void 0) {
23231
23299
  metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
23232
23300
  }
23301
+ for (const detail of usage.promptTokensDetails ?? []) {
23302
+ if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
23303
+ metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokenCount;
23304
+ }
23305
+ }
23306
+ for (const detail of usage.candidatesTokensDetails ?? []) {
23307
+ if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
23308
+ metrics.completion_audio_tokens = (metrics.completion_audio_tokens ?? 0) + detail.tokenCount;
23309
+ }
23310
+ if (detail.modality === "IMAGE" && detail.tokenCount !== void 0) {
23311
+ metrics.completion_image_tokens = (metrics.completion_image_tokens ?? 0) + detail.tokenCount;
23312
+ }
23313
+ }
23233
23314
  }
23234
23315
  function populateInteractionUsageMetrics(metrics, usage) {
23235
23316
  if (typeof usage.total_input_tokens === "number") {
23236
23317
  metrics.prompt_tokens = usage.total_input_tokens;
23237
23318
  }
23238
23319
  if (typeof usage.total_output_tokens === "number") {
23239
- metrics.completion_tokens = usage.total_output_tokens;
23320
+ metrics.completion_tokens = usage.total_output_tokens + (typeof usage.total_thought_tokens === "number" ? usage.total_thought_tokens : 0);
23240
23321
  }
23241
23322
  if (typeof usage.total_tokens === "number") {
23242
23323
  metrics.tokens = usage.total_tokens;
@@ -23247,6 +23328,24 @@ function populateInteractionUsageMetrics(metrics, usage) {
23247
23328
  if (typeof usage.total_thought_tokens === "number") {
23248
23329
  metrics.completion_reasoning_tokens = usage.total_thought_tokens;
23249
23330
  }
23331
+ for (const detail of Array.isArray(usage.input_tokens_by_modality) ? usage.input_tokens_by_modality : []) {
23332
+ if (typeof detail?.modality === "string" && detail.modality.toLowerCase() === "audio" && typeof detail.tokens === "number") {
23333
+ metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokens;
23334
+ }
23335
+ }
23336
+ for (const detail of Array.isArray(usage.output_tokens_by_modality) ? usage.output_tokens_by_modality : []) {
23337
+ if (typeof detail?.modality !== "string" || typeof detail.tokens !== "number") {
23338
+ continue;
23339
+ }
23340
+ switch (detail.modality.toLowerCase()) {
23341
+ case "audio":
23342
+ metrics.completion_audio_tokens = (metrics.completion_audio_tokens ?? 0) + detail.tokens;
23343
+ break;
23344
+ case "image":
23345
+ metrics.completion_image_tokens = (metrics.completion_image_tokens ?? 0) + detail.tokens;
23346
+ break;
23347
+ }
23348
+ }
23250
23349
  }
23251
23350
  function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
23252
23351
  const end = getCurrentUnixTimestamp();
@@ -23957,6 +24056,259 @@ function extractTextGenerationStreamMetadata(chunks) {
23957
24056
  return void 0;
23958
24057
  }
23959
24058
 
24059
+ // src/instrumentation/plugins/huggingface-transformers-plugin.ts
24060
+ var REQUEST_METADATA_KEYS = [
24061
+ "temperature",
24062
+ "top_p",
24063
+ "max_tokens",
24064
+ "stop"
24065
+ ];
24066
+ var HuggingFaceTransformersPlugin = class extends BasePlugin {
24067
+ onEnable() {
24068
+ this.subscribeToPipelineFactory();
24069
+ this.unsubscribers.push(
24070
+ traceAsyncChannel(huggingFaceTransformersChannels.pipelineCall, {
24071
+ name: (_args, event) => {
24072
+ const task = getTask(event);
24073
+ const operation = task?.replaceAll("-", "_") ?? "unknown";
24074
+ return `huggingface.transformers.${operation}`;
24075
+ },
24076
+ type: "llm" /* LLM */,
24077
+ shouldTrace: (_args, event) => isSupportedHuggingFaceTransformersTask(
24078
+ getTask(event)
24079
+ ),
24080
+ extractInput: (args, event) => ({
24081
+ input: extractInput(
24082
+ getTask(event),
24083
+ args
24084
+ ),
24085
+ metadata: extractMetadata(
24086
+ event,
24087
+ args
24088
+ )
24089
+ }),
24090
+ extractOutput: (result, event) => extractOutput(
24091
+ getTask(event),
24092
+ result
24093
+ ),
24094
+ extractMetrics: () => ({})
24095
+ })
24096
+ );
24097
+ }
24098
+ onDisable() {
24099
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
24100
+ }
24101
+ subscribeToPipelineFactory() {
24102
+ const channel = huggingFaceTransformersChannels.pipeline.tracingChannel();
24103
+ const handlers = {
24104
+ asyncEnd: (event) => {
24105
+ if (typeof event.result !== "function") {
24106
+ return;
24107
+ }
24108
+ registerHuggingFaceTransformersPipeline(
24109
+ event.result,
24110
+ event.arguments?.[0],
24111
+ event.arguments?.[1]
24112
+ );
24113
+ }
24114
+ };
24115
+ channel.subscribe(handlers);
24116
+ this.unsubscribers.push(() => channel.unsubscribe(handlers));
24117
+ }
24118
+ };
24119
+ function getTask(event) {
24120
+ const self = event.self;
24121
+ const registeredTask = getHuggingFaceTransformersPipelineInfo(self)?.task;
24122
+ if (registeredTask !== void 0) {
24123
+ return registeredTask;
24124
+ }
24125
+ if (typeof self?.task === "string") {
24126
+ return self.task;
24127
+ }
24128
+ return void 0;
24129
+ }
24130
+ function extractMetadata(event, args) {
24131
+ const metadata = {
24132
+ provider: "huggingface"
24133
+ };
24134
+ const registeredModel = getHuggingFaceTransformersPipelineInfo(
24135
+ event.self
24136
+ )?.model;
24137
+ const model = registeredModel ?? modelIdentifier(event.self);
24138
+ if (model) {
24139
+ metadata.model = model;
24140
+ }
24141
+ const task = getTask(event);
24142
+ const options = task === "question-answering" ? args[2] : args[1];
24143
+ if (isObject(options)) {
24144
+ for (const key of REQUEST_METADATA_KEYS) {
24145
+ if (options[key] !== void 0) {
24146
+ metadata[key] = options[key];
24147
+ }
24148
+ }
24149
+ }
24150
+ return metadata;
24151
+ }
24152
+ function modelIdentifier(pipeline) {
24153
+ const model = pipeline?.model;
24154
+ if (!isObject(model)) {
24155
+ return void 0;
24156
+ }
24157
+ const config = isObject(model.config) ? model.config : void 0;
24158
+ for (const value of [
24159
+ config?._name_or_path,
24160
+ config?.name_or_path,
24161
+ config?.model_id,
24162
+ config?.modelId,
24163
+ model.name
24164
+ ]) {
24165
+ if (typeof value === "string" && value.length > 0) {
24166
+ return value;
24167
+ }
24168
+ }
24169
+ return void 0;
24170
+ }
24171
+ function extractInput(task, args) {
24172
+ switch (task) {
24173
+ case "feature-extraction":
24174
+ return args[0];
24175
+ case "question-answering": {
24176
+ const question = args[0];
24177
+ const context = args[1];
24178
+ if (Array.isArray(question) && Array.isArray(context) && question.every((value) => typeof value === "string") && context.every((value) => typeof value === "string")) {
24179
+ return question.map((value, index) => [
24180
+ {
24181
+ role: "user",
24182
+ content: `Context:
24183
+ ${context[index] ?? ""}
24184
+
24185
+ Question:
24186
+ ${value}`
24187
+ }
24188
+ ]);
24189
+ }
24190
+ if (typeof question === "string" && typeof context === "string") {
24191
+ return [
24192
+ {
24193
+ role: "user",
24194
+ content: `Context:
24195
+ ${context}
24196
+
24197
+ Question:
24198
+ ${question}`
24199
+ }
24200
+ ];
24201
+ }
24202
+ return { context, question };
24203
+ }
24204
+ }
24205
+ const input = args[0];
24206
+ if (task === "text-generation" && isChat(input)) {
24207
+ return input;
24208
+ }
24209
+ if (task === "text-generation" && Array.isArray(input) && input.every(isChat)) {
24210
+ return input;
24211
+ }
24212
+ if (typeof input === "string") {
24213
+ return [{ role: "user", content: input }];
24214
+ }
24215
+ if (Array.isArray(input) && input.every((value) => typeof value === "string")) {
24216
+ return input.map((value) => [
24217
+ {
24218
+ role: "user",
24219
+ content: value
24220
+ }
24221
+ ]);
24222
+ }
24223
+ return input;
24224
+ }
24225
+ function isChat(value) {
24226
+ return Array.isArray(value) && value.length > 0 && value.every(
24227
+ (message) => isObject(message) && typeof message.role === "string" && "content" in message
24228
+ );
24229
+ }
24230
+ function extractOutput(task, result) {
24231
+ switch (task) {
24232
+ case "feature-extraction":
24233
+ return summarizeEmbedding(result);
24234
+ case "question-answering":
24235
+ return choicesFromAnswers(result);
24236
+ default:
24237
+ return choicesFromGenerations(result);
24238
+ }
24239
+ }
24240
+ function summarizeEmbedding(result) {
24241
+ if (!isObject(result) || !Array.isArray(result.dims)) {
24242
+ return void 0;
24243
+ }
24244
+ const { dims } = result;
24245
+ if (dims.length === 0 || !dims.every((dimension) => typeof dimension === "number")) {
24246
+ return void 0;
24247
+ }
24248
+ if (dims.length === 1) {
24249
+ return { embedding_length: dims[0] };
24250
+ }
24251
+ if (dims.length === 2) {
24252
+ return {
24253
+ embedding_count: dims[0],
24254
+ embedding_length: dims[1]
24255
+ };
24256
+ }
24257
+ return {
24258
+ embedding_batch_count: dims[0],
24259
+ embedding_count: dims[1],
24260
+ embedding_length: dims.at(-1) ?? 0
24261
+ };
24262
+ }
24263
+ function choicesFromAnswers(result) {
24264
+ const answers = Array.isArray(result) ? result.flat() : [result];
24265
+ const choices = answers.flatMap((answer, index) => {
24266
+ if (!isObject(answer) || typeof answer.answer !== "string") {
24267
+ return [];
24268
+ }
24269
+ return [
24270
+ {
24271
+ index,
24272
+ finish_reason: "stop",
24273
+ message: {
24274
+ role: "assistant",
24275
+ content: answer.answer
24276
+ }
24277
+ }
24278
+ ];
24279
+ });
24280
+ return choices.length > 0 ? choices : void 0;
24281
+ }
24282
+ function choicesFromGenerations(result) {
24283
+ const generations = Array.isArray(result) ? result.flat() : [result];
24284
+ const choices = generations.flatMap((generation, index) => {
24285
+ if (!isObject(generation)) {
24286
+ return [];
24287
+ }
24288
+ const generated = generation.generated_text ?? generation.summary_text;
24289
+ let content;
24290
+ if (typeof generated === "string") {
24291
+ content = generated;
24292
+ } else if (isChat(generated)) {
24293
+ content = generated.at(-1)?.content;
24294
+ }
24295
+ if (typeof content !== "string") {
24296
+ return [];
24297
+ }
24298
+ return [
24299
+ {
24300
+ index,
24301
+ finish_reason: "stop",
24302
+ message: {
24303
+ role: "assistant",
24304
+ content
24305
+ }
24306
+ }
24307
+ ];
24308
+ });
24309
+ return choices.length > 0 ? choices : void 0;
24310
+ }
24311
+
23960
24312
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
23961
24313
  var OpenRouterAgentPlugin = class extends BasePlugin {
23962
24314
  onEnable() {
@@ -24336,7 +24688,7 @@ function isWrappedTool(tool) {
24336
24688
  return Boolean(tool[OPENROUTER_WRAPPED_TOOL]);
24337
24689
  }
24338
24690
  function traceToolExecution(args) {
24339
- const tracingChannel2 = openRouterAgentChannels.toolExecute.tracingChannel();
24691
+ const tracingChannel = openRouterAgentChannels.toolExecute.tracingChannel();
24340
24692
  const input = args.args.length > 0 ? args.args[0] : void 0;
24341
24693
  const event = {
24342
24694
  arguments: [input],
@@ -24346,33 +24698,33 @@ function traceToolExecution(args) {
24346
24698
  toolCallId: args.toolCallId,
24347
24699
  toolName: args.toolName
24348
24700
  };
24349
- tracingChannel2.start.publish(event);
24701
+ tracingChannel.start.publish(event);
24350
24702
  try {
24351
24703
  const result = args.execute();
24352
- return publishToolResult(tracingChannel2, event, result);
24704
+ return publishToolResult(tracingChannel, event, result);
24353
24705
  } catch (error) {
24354
24706
  event.error = normalizeError(error);
24355
- tracingChannel2.error.publish(event);
24707
+ tracingChannel.error.publish(event);
24356
24708
  throw error;
24357
24709
  }
24358
24710
  }
24359
- function publishToolResult(tracingChannel2, event, result) {
24711
+ function publishToolResult(tracingChannel, event, result) {
24360
24712
  if (isPromiseLike(result)) {
24361
24713
  return result.then(
24362
24714
  (resolved) => {
24363
24715
  event.result = resolved;
24364
- tracingChannel2.asyncEnd.publish(event);
24716
+ tracingChannel.asyncEnd.publish(event);
24365
24717
  return resolved;
24366
24718
  },
24367
24719
  (error) => {
24368
24720
  event.error = normalizeError(error);
24369
- tracingChannel2.error.publish(event);
24721
+ tracingChannel.error.publish(event);
24370
24722
  throw error;
24371
24723
  }
24372
24724
  );
24373
24725
  }
24374
24726
  event.result = result;
24375
- tracingChannel2.asyncEnd.publish(event);
24727
+ tracingChannel.asyncEnd.publish(event);
24376
24728
  return result;
24377
24729
  }
24378
24730
  function getToolCallId(context) {
@@ -25242,7 +25594,7 @@ function isWrappedTool2(tool) {
25242
25594
  return Boolean(tool[OPENROUTER_WRAPPED_TOOL2]);
25243
25595
  }
25244
25596
  function traceToolExecution2(args) {
25245
- const tracingChannel2 = openRouterChannels.toolExecute.tracingChannel();
25597
+ const tracingChannel = openRouterChannels.toolExecute.tracingChannel();
25246
25598
  const input = args.args.length > 0 ? args.args[0] : void 0;
25247
25599
  const event = {
25248
25600
  arguments: [input],
@@ -25252,33 +25604,33 @@ function traceToolExecution2(args) {
25252
25604
  toolCallId: args.toolCallId,
25253
25605
  toolName: args.toolName
25254
25606
  };
25255
- tracingChannel2.start.publish(event);
25607
+ tracingChannel.start.publish(event);
25256
25608
  try {
25257
25609
  const result = args.execute();
25258
- return publishToolResult2(tracingChannel2, event, result);
25610
+ return publishToolResult2(tracingChannel, event, result);
25259
25611
  } catch (error) {
25260
25612
  event.error = normalizeError2(error);
25261
- tracingChannel2.error.publish(event);
25613
+ tracingChannel.error.publish(event);
25262
25614
  throw error;
25263
25615
  }
25264
25616
  }
25265
- function publishToolResult2(tracingChannel2, event, result) {
25617
+ function publishToolResult2(tracingChannel, event, result) {
25266
25618
  if (isPromiseLike(result)) {
25267
25619
  return result.then(
25268
25620
  (resolved) => {
25269
25621
  event.result = resolved;
25270
- tracingChannel2.asyncEnd.publish(event);
25622
+ tracingChannel.asyncEnd.publish(event);
25271
25623
  return resolved;
25272
25624
  },
25273
25625
  (error) => {
25274
25626
  event.error = normalizeError2(error);
25275
- tracingChannel2.error.publish(event);
25627
+ tracingChannel.error.publish(event);
25276
25628
  throw error;
25277
25629
  }
25278
25630
  );
25279
25631
  }
25280
25632
  event.result = result;
25281
- tracingChannel2.asyncEnd.publish(event);
25633
+ tracingChannel.asyncEnd.publish(event);
25282
25634
  return result;
25283
25635
  }
25284
25636
  function getToolCallId2(context) {
@@ -26396,7 +26748,7 @@ var GoogleADKPlugin = class extends BasePlugin {
26396
26748
  this.activeAgentSpans.clear();
26397
26749
  }
26398
26750
  subscribeToRunnerRunAsync() {
26399
- const tracingChannel2 = googleADKChannels.runnerRunAsync.tracingChannel();
26751
+ const tracingChannel = googleADKChannels.runnerRunAsync.tracingChannel();
26400
26752
  const states = /* @__PURE__ */ new WeakMap();
26401
26753
  const createState = (event) => {
26402
26754
  const params = event.arguments[0] ?? {};
@@ -26427,7 +26779,7 @@ var GoogleADKPlugin = class extends BasePlugin {
26427
26779
  return { span, startTime, events: [], contextKey };
26428
26780
  };
26429
26781
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart3(
26430
- tracingChannel2,
26782
+ tracingChannel,
26431
26783
  states,
26432
26784
  createState
26433
26785
  );
@@ -26479,14 +26831,14 @@ var GoogleADKPlugin = class extends BasePlugin {
26479
26831
  states.delete(event);
26480
26832
  }
26481
26833
  };
26482
- tracingChannel2.subscribe(handlers);
26834
+ tracingChannel.subscribe(handlers);
26483
26835
  this.unsubscribers.push(() => {
26484
26836
  unbindCurrentSpanStore?.();
26485
- tracingChannel2.unsubscribe(handlers);
26837
+ tracingChannel.unsubscribe(handlers);
26486
26838
  });
26487
26839
  }
26488
26840
  subscribeToAgentRunAsync() {
26489
- const tracingChannel2 = googleADKChannels.agentRunAsync.tracingChannel();
26841
+ const tracingChannel = googleADKChannels.agentRunAsync.tracingChannel();
26490
26842
  const states = /* @__PURE__ */ new WeakMap();
26491
26843
  const createState = (event) => {
26492
26844
  const parentContext = event.arguments[0];
@@ -26535,7 +26887,7 @@ var GoogleADKPlugin = class extends BasePlugin {
26535
26887
  return { span, startTime, events: [], contextKey, name: agentName };
26536
26888
  };
26537
26889
  const unbindCurrentSpanStore = bindCurrentSpanStoreToStart3(
26538
- tracingChannel2,
26890
+ tracingChannel,
26539
26891
  states,
26540
26892
  createState
26541
26893
  );
@@ -26587,14 +26939,14 @@ var GoogleADKPlugin = class extends BasePlugin {
26587
26939
  states.delete(event);
26588
26940
  }
26589
26941
  };
26590
- tracingChannel2.subscribe(handlers);
26942
+ tracingChannel.subscribe(handlers);
26591
26943
  this.unsubscribers.push(() => {
26592
26944
  unbindCurrentSpanStore?.();
26593
- tracingChannel2.unsubscribe(handlers);
26945
+ tracingChannel.unsubscribe(handlers);
26594
26946
  });
26595
26947
  }
26596
26948
  subscribeToToolRunAsync() {
26597
- const tracingChannel2 = googleADKChannels.toolRunAsync.tracingChannel();
26949
+ const tracingChannel = googleADKChannels.toolRunAsync.tracingChannel();
26598
26950
  const states = /* @__PURE__ */ new WeakMap();
26599
26951
  const handlers = {
26600
26952
  start: (event) => {
@@ -26661,9 +27013,9 @@ var GoogleADKPlugin = class extends BasePlugin {
26661
27013
  states.delete(event);
26662
27014
  }
26663
27015
  };
26664
- tracingChannel2.subscribe(handlers);
27016
+ tracingChannel.subscribe(handlers);
26665
27017
  this.unsubscribers.push(() => {
26666
- tracingChannel2.unsubscribe(handlers);
27018
+ tracingChannel.unsubscribe(handlers);
26667
27019
  });
26668
27020
  }
26669
27021
  };
@@ -26739,10 +27091,10 @@ function bindAsyncIterableToCurrentSpan(stream, span) {
26739
27091
  }
26740
27092
  return stream;
26741
27093
  }
26742
- function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
27094
+ function bindCurrentSpanStoreToStart3(tracingChannel, states, create) {
26743
27095
  const state = _internalGetGlobalState();
26744
27096
  const contextManager = state?.contextManager;
26745
- const startChannel = tracingChannel2.start;
27097
+ const startChannel = tracingChannel.start;
26746
27098
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
26747
27099
  if (!startChannel?.bindStore || !currentSpanStore) {
26748
27100
  return void 0;
@@ -26994,15 +27346,32 @@ function aggregateUsageFromEvents(events) {
26994
27346
  if (usage.thoughtsTokenCount !== void 0) {
26995
27347
  aggregated.thoughtsTokenCount = (aggregated.thoughtsTokenCount ?? 0) + usage.thoughtsTokenCount;
26996
27348
  }
27349
+ if (usage.toolUsePromptTokenCount !== void 0) {
27350
+ aggregated.toolUsePromptTokenCount = (aggregated.toolUsePromptTokenCount ?? 0) + usage.toolUsePromptTokenCount;
27351
+ }
27352
+ for (const detailsField of [
27353
+ "promptTokensDetails",
27354
+ "cacheTokensDetails",
27355
+ "candidatesTokensDetails",
27356
+ "toolUsePromptTokensDetails"
27357
+ ]) {
27358
+ const details = usage[detailsField];
27359
+ if (details !== void 0) {
27360
+ aggregated[detailsField] = [
27361
+ ...aggregated[detailsField] ?? [],
27362
+ ...details
27363
+ ];
27364
+ }
27365
+ }
26997
27366
  }
26998
27367
  return hasUsage ? aggregated : void 0;
26999
27368
  }
27000
27369
  function populateUsageMetrics2(metrics, usage) {
27001
- if (usage.promptTokenCount !== void 0) {
27002
- metrics.prompt_tokens = usage.promptTokenCount;
27370
+ if (usage.promptTokenCount !== void 0 || usage.toolUsePromptTokenCount !== void 0) {
27371
+ metrics.prompt_tokens = (usage.promptTokenCount ?? 0) + (usage.toolUsePromptTokenCount ?? 0);
27003
27372
  }
27004
- if (usage.candidatesTokenCount !== void 0) {
27005
- metrics.completion_tokens = usage.candidatesTokenCount;
27373
+ if (usage.candidatesTokenCount !== void 0 || usage.thoughtsTokenCount !== void 0) {
27374
+ metrics.completion_tokens = (usage.candidatesTokenCount ?? 0) + (usage.thoughtsTokenCount ?? 0);
27006
27375
  }
27007
27376
  if (usage.totalTokenCount !== void 0) {
27008
27377
  metrics.tokens = usage.totalTokenCount;
@@ -27013,6 +27382,19 @@ function populateUsageMetrics2(metrics, usage) {
27013
27382
  if (usage.thoughtsTokenCount !== void 0) {
27014
27383
  metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
27015
27384
  }
27385
+ for (const detail of usage.promptTokensDetails ?? []) {
27386
+ if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
27387
+ metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokenCount;
27388
+ }
27389
+ }
27390
+ for (const detail of usage.candidatesTokensDetails ?? []) {
27391
+ if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
27392
+ metrics.completion_audio_tokens = (metrics.completion_audio_tokens ?? 0) + detail.tokenCount;
27393
+ }
27394
+ if (detail.modality === "IMAGE" && detail.tokenCount !== void 0) {
27395
+ metrics.completion_image_tokens = (metrics.completion_image_tokens ?? 0) + detail.tokenCount;
27396
+ }
27397
+ }
27016
27398
  }
27017
27399
  function cleanMetrics4(metrics) {
27018
27400
  const cleaned = {};
@@ -28469,10 +28851,10 @@ var GenkitPlugin = class extends BasePlugin {
28469
28851
  this.subscribeToActionStream();
28470
28852
  }
28471
28853
  subscribeToActionRun() {
28472
- const tracingChannel2 = genkitChannels.actionRun.tracingChannel();
28854
+ const tracingChannel = genkitChannels.actionRun.tracingChannel();
28473
28855
  const states = /* @__PURE__ */ new WeakMap();
28474
28856
  const unbindCurrentSpanStore = bindActionCurrentSpanStoreToStart(
28475
- tracingChannel2,
28857
+ tracingChannel,
28476
28858
  states,
28477
28859
  (event) => startActionRunSpan(event)
28478
28860
  );
@@ -28509,17 +28891,17 @@ var GenkitPlugin = class extends BasePlugin {
28509
28891
  states.delete(event);
28510
28892
  }
28511
28893
  };
28512
- tracingChannel2.subscribe(handlers);
28894
+ tracingChannel.subscribe(handlers);
28513
28895
  this.unsubscribers.push(() => {
28514
28896
  unbindCurrentSpanStore?.();
28515
- tracingChannel2.unsubscribe(handlers);
28897
+ tracingChannel.unsubscribe(handlers);
28516
28898
  });
28517
28899
  }
28518
28900
  subscribeToActionSpan() {
28519
- const tracingChannel2 = genkitCoreChannels.actionSpan.tracingChannel();
28901
+ const tracingChannel = genkitCoreChannels.actionSpan.tracingChannel();
28520
28902
  const states = /* @__PURE__ */ new WeakMap();
28521
28903
  const unbindCurrentSpanStore = bindActionCurrentSpanStoreToStart(
28522
- tracingChannel2,
28904
+ tracingChannel,
28523
28905
  states,
28524
28906
  (event) => startActionSpan(event)
28525
28907
  );
@@ -28557,10 +28939,10 @@ var GenkitPlugin = class extends BasePlugin {
28557
28939
  states.delete(event);
28558
28940
  }
28559
28941
  };
28560
- tracingChannel2.subscribe(handlers);
28942
+ tracingChannel.subscribe(handlers);
28561
28943
  this.unsubscribers.push(() => {
28562
28944
  unbindCurrentSpanStore?.();
28563
- tracingChannel2.unsubscribe(handlers);
28945
+ tracingChannel.unsubscribe(handlers);
28564
28946
  });
28565
28947
  }
28566
28948
  subscribeToActionStream() {
@@ -28629,10 +29011,10 @@ function ensureActionSpanState(states, event, create) {
28629
29011
  }
28630
29012
  return created;
28631
29013
  }
28632
- function bindActionCurrentSpanStoreToStart(tracingChannel2, states, create) {
29014
+ function bindActionCurrentSpanStoreToStart(tracingChannel, states, create) {
28633
29015
  const state = _internalGetGlobalState();
28634
29016
  const contextManager = state?.contextManager;
28635
- const startChannel = tracingChannel2.start;
29017
+ const startChannel = tracingChannel.start;
28636
29018
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
28637
29019
  if (!startChannel?.bindStore || !currentSpanStore) {
28638
29020
  return void 0;
@@ -29524,7 +29906,7 @@ var LangChainPlugin = class extends BasePlugin {
29524
29906
  this.injectedManagers = /* @__PURE__ */ new WeakSet();
29525
29907
  }
29526
29908
  subscribeToConfigure(channel) {
29527
- const tracingChannel2 = channel.tracingChannel();
29909
+ const tracingChannel = channel.tracingChannel();
29528
29910
  const handlers = {
29529
29911
  start: (event) => {
29530
29912
  injectHandlerIntoArguments(event.arguments);
@@ -29533,9 +29915,9 @@ var LangChainPlugin = class extends BasePlugin {
29533
29915
  this.injectHandler(event.result);
29534
29916
  }
29535
29917
  };
29536
- tracingChannel2.subscribe(handlers);
29918
+ tracingChannel.subscribe(handlers);
29537
29919
  this.unsubscribers.push(() => {
29538
- tracingChannel2.unsubscribe(handlers);
29920
+ tracingChannel.unsubscribe(handlers);
29539
29921
  });
29540
29922
  }
29541
29923
  injectHandler(result) {
@@ -29779,7 +30161,7 @@ var LangSmithPlugin = class extends BasePlugin {
29779
30161
  const inputs = preferOwnValue(run, previous, "inputs");
29780
30162
  const outputs = preferOwnValue(run, previous, "outputs");
29781
30163
  const error = errorMessage(preferOwnValue(run, previous, "error"));
29782
- const metadata = extractMetadata(run, previous);
30164
+ const metadata = extractMetadata2(run, previous);
29783
30165
  const tags = extractTags(preferOwnValue(run, previous, "tags"));
29784
30166
  const metrics = extractMetrics(run, previous);
29785
30167
  span.log({
@@ -29884,7 +30266,7 @@ function mapRunType(runType) {
29884
30266
  return "task" /* TASK */;
29885
30267
  }
29886
30268
  }
29887
- function extractMetadata(run, previous) {
30269
+ function extractMetadata2(run, previous) {
29888
30270
  const extra = preferOwnValue(run, previous, "extra");
29889
30271
  const rawMetadata = ownValue(extra, "metadata");
29890
30272
  if (!isRecord2(rawMetadata)) {
@@ -30983,9 +31365,9 @@ var StrandsAgentSDKPlugin = class extends BasePlugin {
30983
31365
  });
30984
31366
  }
30985
31367
  subscribeToMultiAgentStream(channel, operation) {
30986
- const tracingChannel2 = channel.tracingChannel();
31368
+ const tracingChannel = channel.tracingChannel();
30987
31369
  const states = /* @__PURE__ */ new WeakMap();
30988
- const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(tracingChannel2);
31370
+ const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(tracingChannel);
30989
31371
  const handlers = {
30990
31372
  start: (event) => {
30991
31373
  const state = startMultiAgentStream(
@@ -31039,10 +31421,10 @@ var StrandsAgentSDKPlugin = class extends BasePlugin {
31039
31421
  states.delete(event);
31040
31422
  }
31041
31423
  };
31042
- tracingChannel2.subscribe(handlers);
31424
+ tracingChannel.subscribe(handlers);
31043
31425
  this.unsubscribers.push(() => {
31044
31426
  unbindAutoInstrumentationSuppression?.();
31045
- tracingChannel2.unsubscribe(handlers);
31427
+ tracingChannel.unsubscribe(handlers);
31046
31428
  });
31047
31429
  }
31048
31430
  };
@@ -31225,6 +31607,15 @@ function handleMultiAgentStreamEvent(state, event, activeChildParents) {
31225
31607
  logInstrumentationError5("Strands Agent SDK multi-agent event", error);
31226
31608
  }
31227
31609
  }
31610
+ function buildModelSpanInput(event, attachmentCache) {
31611
+ const systemPrompt = typeof event.agent?.systemPrompt === "string" && event.agent.systemPrompt.length > 0 ? event.agent.systemPrompt : void 0;
31612
+ const processedMessages = Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(event.agent.messages, attachmentCache) : void 0;
31613
+ if (!systemPrompt) {
31614
+ return processedMessages;
31615
+ }
31616
+ const systemMessage = { role: "system", content: systemPrompt };
31617
+ return Array.isArray(processedMessages) ? [systemMessage, ...processedMessages] : [systemMessage];
31618
+ }
31228
31619
  function startModelSpan(state, event) {
31229
31620
  if (state.activeModel) {
31230
31621
  finalizeModelSpan(state);
@@ -31242,10 +31633,7 @@ function startModelSpan(state, event) {
31242
31633
  withSpanInstrumentationName(
31243
31634
  {
31244
31635
  event: {
31245
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
31246
- event.agent.messages,
31247
- state.attachmentCache
31248
- ) : void 0,
31636
+ input: buildModelSpanInput(event, state.attachmentCache),
31249
31637
  metadata
31250
31638
  },
31251
31639
  name: formatModelSpanName(model),
@@ -31885,6 +32273,527 @@ function logInstrumentationError5(context, error) {
31885
32273
  debugLogger.debug(`${context}:`, error);
31886
32274
  }
31887
32275
 
32276
+ // src/instrumentation/plugins/cloudflare-ai-chat-instrumentation.ts
32277
+ var wrappedTurnRunners = /* @__PURE__ */ new WeakSet();
32278
+ var wrappedResponseHooks = /* @__PURE__ */ new WeakSet();
32279
+ function instrumentCloudflareAIChatAgent(agent) {
32280
+ try {
32281
+ const original = Reflect.get(agent, "_runExclusiveChatTurn");
32282
+ if (typeof original !== "function" || wrappedTurnRunners.has(original)) {
32283
+ return agent;
32284
+ }
32285
+ const wrapped = async function(...args) {
32286
+ return cloudflareAIChatChannels.runExclusiveChatTurn.tracePromise(
32287
+ () => Reflect.apply(original, this, args),
32288
+ {
32289
+ arguments: args,
32290
+ self: this
32291
+ }
32292
+ );
32293
+ };
32294
+ wrappedTurnRunners.add(wrapped);
32295
+ defineWrappedMethod(agent, "_runExclusiveChatTurn", original, wrapped);
32296
+ } catch (error) {
32297
+ debugLogger.debug("Failed to wrap @cloudflare/ai-chat turn runner:", error);
32298
+ }
32299
+ return agent;
32300
+ }
32301
+ function instrumentCloudflareAIChatResponseHook(agent) {
32302
+ try {
32303
+ const original = Reflect.get(agent, "onChatResponse");
32304
+ if (typeof original !== "function" || wrappedResponseHooks.has(original)) {
32305
+ return;
32306
+ }
32307
+ const wrapped = function(result) {
32308
+ const args = [result];
32309
+ return cloudflareAIChatChannels.onChatResponse.traceSync(
32310
+ () => Reflect.apply(original, this, args),
32311
+ {
32312
+ arguments: args,
32313
+ self: this
32314
+ }
32315
+ );
32316
+ };
32317
+ wrappedResponseHooks.add(wrapped);
32318
+ defineWrappedMethod(agent, "onChatResponse", original, wrapped);
32319
+ } catch (error) {
32320
+ debugLogger.debug(
32321
+ "Failed to wrap @cloudflare/ai-chat response hook:",
32322
+ error
32323
+ );
32324
+ }
32325
+ }
32326
+ function defineWrappedMethod(target, property, original, wrapped) {
32327
+ const ownDescriptor = Object.getOwnPropertyDescriptor(target, property);
32328
+ Object.defineProperty(target, property, {
32329
+ configurable: ownDescriptor?.configurable ?? true,
32330
+ enumerable: ownDescriptor?.enumerable ?? false,
32331
+ value: wrapped,
32332
+ writable: ownDescriptor && "writable" in ownDescriptor ? ownDescriptor.writable : true
32333
+ });
32334
+ try {
32335
+ Object.defineProperty(wrapped, "name", {
32336
+ configurable: true,
32337
+ value: original.name
32338
+ });
32339
+ } catch {
32340
+ }
32341
+ }
32342
+
32343
+ // src/instrumentation/plugins/cloudflare-ai-chat-plugin.ts
32344
+ var BLOCKED_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
32345
+ var RESPONSE_STATE_RETENTION_MS = 6e4;
32346
+ var CloudflareAIChatPlugin = class extends BasePlugin {
32347
+ activeStates = /* @__PURE__ */ new Set();
32348
+ activeTurns = /* @__PURE__ */ new WeakMap();
32349
+ eventStates = /* @__PURE__ */ new WeakMap();
32350
+ onEnable() {
32351
+ this.subscribeToResponseHook();
32352
+ this.subscribeToTurnRunner();
32353
+ }
32354
+ onDisable() {
32355
+ for (const unsubscribe of this.unsubscribers) {
32356
+ unsubscribe();
32357
+ }
32358
+ this.unsubscribers = [];
32359
+ for (const state of this.activeStates) {
32360
+ this.cleanupState(state);
32361
+ }
32362
+ this.activeStates.clear();
32363
+ }
32364
+ subscribeToTurnRunner() {
32365
+ const tracingChannel = cloudflareAIChatChannels.runExclusiveChatTurn.tracingChannel();
32366
+ const unbindCurrentSpanStore = this.bindCurrentSpanStore(tracingChannel);
32367
+ const handlers = {
32368
+ start: (event) => {
32369
+ this.ensureEventState(event);
32370
+ },
32371
+ asyncEnd: (event) => {
32372
+ this.finishEvent(event);
32373
+ },
32374
+ error: (event) => {
32375
+ this.finishEvent(event, event.error);
32376
+ }
32377
+ };
32378
+ tracingChannel.subscribe(handlers);
32379
+ this.unsubscribers.push(() => {
32380
+ unbindCurrentSpanStore?.();
32381
+ tracingChannel.unsubscribe(handlers);
32382
+ });
32383
+ }
32384
+ subscribeToResponseHook() {
32385
+ const tracingChannel = cloudflareAIChatChannels.onChatResponse.tracingChannel();
32386
+ const handlers = {
32387
+ start: (event) => {
32388
+ let state;
32389
+ try {
32390
+ const agent = asObject(event.self);
32391
+ const result = event.arguments[0];
32392
+ state = agent ? this.findResponseState(
32393
+ agent,
32394
+ stringValue3(ownValue2(result, "requestId"))
32395
+ ) : void 0;
32396
+ if (!state) {
32397
+ return;
32398
+ }
32399
+ state.responseObserved = true;
32400
+ const output = serializeMessage(ownValue2(result, "message"));
32401
+ const status = stringValue3(ownValue2(result, "status"));
32402
+ const error = ownValue2(result, "error");
32403
+ const input = ownValue2(result, "continuation") === true ? state.input : serializeMessages(readProperty(agent, "messages"))?.filter(
32404
+ (message) => typeof output?.id !== "string" || message.id !== output.id
32405
+ );
32406
+ state.span.log({
32407
+ ...input !== void 0 ? { input } : {},
32408
+ ...output !== void 0 ? { output } : {},
32409
+ ...status === "error" && error !== void 0 ? { error } : {}
32410
+ });
32411
+ } catch (error) {
32412
+ debugLogger.debug(
32413
+ "Failed to process @cloudflare/ai-chat response hook:",
32414
+ error
32415
+ );
32416
+ } finally {
32417
+ if (state?.settled) {
32418
+ this.cleanupState(state);
32419
+ }
32420
+ }
32421
+ }
32422
+ };
32423
+ tracingChannel.subscribe(handlers);
32424
+ this.unsubscribers.push(() => tracingChannel.unsubscribe(handlers));
32425
+ }
32426
+ bindCurrentSpanStore(tracingChannel) {
32427
+ const globalState = _internalGetGlobalState();
32428
+ const contextManager = globalState?.contextManager;
32429
+ const startChannel = tracingChannel.start;
32430
+ const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
32431
+ if (!startChannel || !currentSpanStore || !contextManager) {
32432
+ return void 0;
32433
+ }
32434
+ startChannel.bindStore(currentSpanStore, (event) => {
32435
+ const state = this.ensureEventState(event);
32436
+ return state ? contextManager.wrapSpanForStore(state.span) : currentSpanStore.getStore();
32437
+ });
32438
+ return () => startChannel.unbindStore(currentSpanStore);
32439
+ }
32440
+ ensureEventState(event) {
32441
+ const eventKey = event;
32442
+ const known = this.eventStates.get(eventKey);
32443
+ if (known) {
32444
+ return known;
32445
+ }
32446
+ try {
32447
+ const agent = asObject(event.self);
32448
+ const requestId = stringValue3(event.arguments[0]);
32449
+ const key = requestId ?? /* @__PURE__ */ Symbol("cloudflare-ai-chat-turn");
32450
+ const activeForAgent = agent ? this.getActiveTurns(agent) : void 0;
32451
+ const existing = activeForAgent?.get(key);
32452
+ const state = existing && !existing.settled ? existing : this.createTurnState(agent, key);
32453
+ if (existing && !existing.settled) {
32454
+ state.depth += 1;
32455
+ }
32456
+ this.eventStates.set(eventKey, state);
32457
+ this.bindTurnCallback(event, agent, state);
32458
+ if (agent) {
32459
+ instrumentCloudflareAIChatResponseHook(agent);
32460
+ }
32461
+ return state;
32462
+ } catch (error) {
32463
+ debugLogger.debug(
32464
+ "Failed to start @cloudflare/ai-chat instrumentation:",
32465
+ error
32466
+ );
32467
+ return void 0;
32468
+ }
32469
+ }
32470
+ createTurnState(agent, key) {
32471
+ const span = startSpan(
32472
+ withSpanInstrumentationName(
32473
+ {
32474
+ name: "AIChatAgent.onChatMessage",
32475
+ spanAttributes: { type: "task" /* TASK */ }
32476
+ },
32477
+ INSTRUMENTATION_NAMES.CLOUDFLARE_AI_CHAT
32478
+ )
32479
+ );
32480
+ const state = {
32481
+ agent,
32482
+ depth: 1,
32483
+ ended: false,
32484
+ key,
32485
+ responseObserved: false,
32486
+ settled: false,
32487
+ span
32488
+ };
32489
+ this.activeStates.add(state);
32490
+ if (agent) {
32491
+ this.getActiveTurns(agent).set(key, state);
32492
+ }
32493
+ return state;
32494
+ }
32495
+ bindTurnCallback(event, agent, state) {
32496
+ const callback = event.arguments[1];
32497
+ if (typeof callback !== "function") {
32498
+ return;
32499
+ }
32500
+ try {
32501
+ event.arguments[1] = function(...args) {
32502
+ return withCurrent(state.span, () => {
32503
+ try {
32504
+ const input = serializeMessages(readProperty(agent, "messages"));
32505
+ if (input !== void 0 && state.input === void 0) {
32506
+ state.input = input;
32507
+ state.span.log({ input });
32508
+ }
32509
+ } catch (error) {
32510
+ debugLogger.debug(
32511
+ "Failed to capture @cloudflare/ai-chat messages:",
32512
+ error
32513
+ );
32514
+ }
32515
+ return Reflect.apply(callback, this, args);
32516
+ });
32517
+ };
32518
+ } catch (error) {
32519
+ debugLogger.debug(
32520
+ "Failed to bind @cloudflare/ai-chat turn callback context:",
32521
+ error
32522
+ );
32523
+ }
32524
+ }
32525
+ finishEvent(event, error) {
32526
+ const eventKey = event;
32527
+ const state = this.eventStates.get(eventKey);
32528
+ if (!state || state.ended) {
32529
+ return;
32530
+ }
32531
+ this.eventStates.delete(eventKey);
32532
+ if (error !== void 0 && state.pendingError === void 0) {
32533
+ state.pendingError = error;
32534
+ }
32535
+ state.depth -= 1;
32536
+ if (state.depth === 0) {
32537
+ state.settled = true;
32538
+ this.endState(state);
32539
+ if (state.responseObserved || error !== void 0) {
32540
+ this.cleanupState(state);
32541
+ } else {
32542
+ state.cleanupTimer = setTimeout(
32543
+ () => this.cleanupState(state),
32544
+ RESPONSE_STATE_RETENTION_MS
32545
+ );
32546
+ if (typeof state.cleanupTimer === "object" && state.cleanupTimer !== null && "unref" in state.cleanupTimer && typeof state.cleanupTimer.unref === "function") {
32547
+ state.cleanupTimer.unref();
32548
+ }
32549
+ }
32550
+ }
32551
+ }
32552
+ endState(state) {
32553
+ if (state.ended) {
32554
+ return;
32555
+ }
32556
+ state.ended = true;
32557
+ try {
32558
+ if (state.pendingError !== void 0) {
32559
+ state.span.log({ error: state.pendingError });
32560
+ }
32561
+ } finally {
32562
+ state.span.end();
32563
+ }
32564
+ }
32565
+ cleanupState(state) {
32566
+ if (state.cleanupTimer !== void 0) {
32567
+ clearTimeout(state.cleanupTimer);
32568
+ state.cleanupTimer = void 0;
32569
+ }
32570
+ try {
32571
+ this.endState(state);
32572
+ } finally {
32573
+ this.activeStates.delete(state);
32574
+ if (state.agent) {
32575
+ const activeForAgent = this.activeTurns.get(state.agent);
32576
+ if (activeForAgent?.get(state.key) === state) {
32577
+ activeForAgent.delete(state.key);
32578
+ }
32579
+ }
32580
+ }
32581
+ }
32582
+ getActiveTurns(agent) {
32583
+ let turns = this.activeTurns.get(agent);
32584
+ if (!turns) {
32585
+ turns = /* @__PURE__ */ new Map();
32586
+ this.activeTurns.set(agent, turns);
32587
+ }
32588
+ return turns;
32589
+ }
32590
+ findResponseState(agent, requestId) {
32591
+ const turns = this.activeTurns.get(agent);
32592
+ if (!turns) {
32593
+ return void 0;
32594
+ }
32595
+ if (requestId !== void 0) {
32596
+ return turns.get(requestId);
32597
+ }
32598
+ const active = [...turns.values()];
32599
+ return active.length === 1 ? active[0] : void 0;
32600
+ }
32601
+ };
32602
+ function asObject(value) {
32603
+ return value !== null && (typeof value === "object" || typeof value === "function") ? value : void 0;
32604
+ }
32605
+ function readProperty(value, key) {
32606
+ const object = asObject(value);
32607
+ return object ? Reflect.get(object, key) : void 0;
32608
+ }
32609
+ function ownValue2(value, key) {
32610
+ const object = asObject(value);
32611
+ if (!object) {
32612
+ return void 0;
32613
+ }
32614
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
32615
+ return descriptor && "value" in descriptor ? descriptor.value : void 0;
32616
+ }
32617
+ function stringValue3(value) {
32618
+ return typeof value === "string" ? value : void 0;
32619
+ }
32620
+ function serializeMessages(value) {
32621
+ if (!Array.isArray(value)) {
32622
+ return void 0;
32623
+ }
32624
+ return value.flatMap((message) => {
32625
+ const serialized = serializeMessage(message);
32626
+ return serialized === void 0 ? [] : [serialized];
32627
+ });
32628
+ }
32629
+ function serializeMessage(value) {
32630
+ const message = asObject(value);
32631
+ if (!message) {
32632
+ return void 0;
32633
+ }
32634
+ const serialized = [];
32635
+ const id = ownValue2(message, "id");
32636
+ const role = ownValue2(message, "role");
32637
+ const parts = ownValue2(message, "parts");
32638
+ if (typeof id === "string") {
32639
+ serialized.push(["id", id]);
32640
+ }
32641
+ if (typeof role === "string") {
32642
+ serialized.push(["role", role]);
32643
+ }
32644
+ const sanitizedParts = sanitizeLoggedValue2(parts);
32645
+ if (sanitizedParts !== void 0) {
32646
+ serialized.push(["parts", sanitizedParts]);
32647
+ }
32648
+ return serialized.length > 0 ? Object.fromEntries(serialized) : void 0;
32649
+ }
32650
+ function sanitizeLoggedValue2(value, seen = /* @__PURE__ */ new WeakSet(), depth = 0) {
32651
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
32652
+ return value;
32653
+ }
32654
+ if (typeof value === "number") {
32655
+ return Number.isFinite(value) ? value : void 0;
32656
+ }
32657
+ const object = asObject(value);
32658
+ if (!object || depth >= 20 || typeof value === "function") {
32659
+ return void 0;
32660
+ }
32661
+ if (seen.has(object)) {
32662
+ return "[Circular]";
32663
+ }
32664
+ seen.add(object);
32665
+ if (Array.isArray(value)) {
32666
+ return value.map((entry) => sanitizeLoggedValue2(entry, seen, depth + 1));
32667
+ }
32668
+ const entries = [];
32669
+ for (const [key, descriptor] of Object.entries(
32670
+ Object.getOwnPropertyDescriptors(object)
32671
+ )) {
32672
+ if (BLOCKED_KEYS.has(key) || !descriptor.enumerable || !("value" in descriptor)) {
32673
+ continue;
32674
+ }
32675
+ const sanitized = sanitizeLoggedValue2(descriptor.value, seen, depth + 1);
32676
+ if (sanitized !== void 0) {
32677
+ entries.push([key, sanitized]);
32678
+ }
32679
+ }
32680
+ return Object.fromEntries(entries);
32681
+ }
32682
+
32683
+ // src/instrumentation/plugins/cloudflare-agents-plugin.ts
32684
+ var CLOUDFLARE_WORKERS_CONTEXT = {
32685
+ span_origin: {
32686
+ environment: { type: "server", name: "cloudflare_workers" }
32687
+ }
32688
+ };
32689
+ var CloudflareAgentsPlugin = class extends BasePlugin {
32690
+ onEnable() {
32691
+ const channel = cloudflareAgentsChannels.runAgentTool.tracingChannel();
32692
+ const spans = /* @__PURE__ */ new WeakMap();
32693
+ const handlers = {
32694
+ start: (event) => {
32695
+ try {
32696
+ const agentClass = event.arguments[0];
32697
+ const options = event.arguments[1];
32698
+ if (ownValue3(options, "detached")) {
32699
+ return;
32700
+ }
32701
+ const name = ownValue3(agentClass, "name");
32702
+ if (typeof name !== "string" || name.length === 0) {
32703
+ debugLogger.warn(
32704
+ "Skipping Cloudflare Agents runAgentTool span because the child agent class has no name."
32705
+ );
32706
+ return;
32707
+ }
32708
+ const span = _internalStartSpanWithContext(
32709
+ withSpanInstrumentationName(
32710
+ {
32711
+ name,
32712
+ spanAttributes: { type: "tool" /* TOOL */ },
32713
+ event: {
32714
+ input: ownValue3(options, "input")
32715
+ }
32716
+ },
32717
+ INSTRUMENTATION_NAMES.CLOUDFLARE_AGENTS
32718
+ ),
32719
+ CLOUDFLARE_WORKERS_CONTEXT
32720
+ );
32721
+ spans.set(event, span);
32722
+ } catch (error) {
32723
+ logInstrumentationError6("start", error);
32724
+ }
32725
+ },
32726
+ asyncEnd: (event) => {
32727
+ const span = spans.get(event);
32728
+ if (!span) {
32729
+ return;
32730
+ }
32731
+ spans.delete(event);
32732
+ try {
32733
+ const status = ownValue3(event.result, "status");
32734
+ if (status === "completed") {
32735
+ span.log({ output: ownValue3(event.result, "output") });
32736
+ } else {
32737
+ const error = ownValue3(event.result, "error");
32738
+ if (typeof error === "string") {
32739
+ span.log({ error });
32740
+ }
32741
+ }
32742
+ } catch (error) {
32743
+ logInstrumentationError6("completion", error);
32744
+ } finally {
32745
+ safelyEndSpan(span);
32746
+ }
32747
+ },
32748
+ error: (event) => {
32749
+ const span = spans.get(event);
32750
+ if (!span) {
32751
+ return;
32752
+ }
32753
+ spans.delete(event);
32754
+ try {
32755
+ span.log({ error: event.error });
32756
+ } catch (error) {
32757
+ logInstrumentationError6("rejection", error);
32758
+ } finally {
32759
+ safelyEndSpan(span);
32760
+ }
32761
+ }
32762
+ };
32763
+ channel.subscribe(handlers);
32764
+ this.unsubscribers.push(() => channel.unsubscribe(handlers));
32765
+ }
32766
+ onDisable() {
32767
+ for (const unsubscribe of this.unsubscribers) {
32768
+ unsubscribe();
32769
+ }
32770
+ this.unsubscribers = [];
32771
+ }
32772
+ };
32773
+ function ownValue3(value, key) {
32774
+ if (!isObjectLike2(value)) {
32775
+ return void 0;
32776
+ }
32777
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
32778
+ return descriptor && "value" in descriptor ? descriptor.value : void 0;
32779
+ }
32780
+ function isObjectLike2(value) {
32781
+ return typeof value === "object" && value !== null || typeof value === "function";
32782
+ }
32783
+ function safelyEndSpan(span) {
32784
+ try {
32785
+ span.end();
32786
+ } catch (error) {
32787
+ logInstrumentationError6("span end", error);
32788
+ }
32789
+ }
32790
+ function logInstrumentationError6(operation, error) {
32791
+ debugLogger.error(
32792
+ `Failed to process Cloudflare Agents ${operation} instrumentation:`,
32793
+ error
32794
+ );
32795
+ }
32796
+
31888
32797
  // src/instrumentation/braintrust-plugin.ts
31889
32798
  var BraintrustPlugin = class extends BasePlugin {
31890
32799
  config;
@@ -31897,6 +32806,7 @@ var BraintrustPlugin = class extends BasePlugin {
31897
32806
  openAIAgentsPlugin = null;
31898
32807
  googleGenAIPlugin = null;
31899
32808
  huggingFacePlugin = null;
32809
+ huggingFaceTransformersPlugin = null;
31900
32810
  openRouterPlugin = null;
31901
32811
  openRouterAgentPlugin = null;
31902
32812
  mistralPlugin = null;
@@ -31911,6 +32821,8 @@ var BraintrustPlugin = class extends BasePlugin {
31911
32821
  langSmithPlugin = null;
31912
32822
  piCodingAgentPlugin = null;
31913
32823
  strandsAgentSDKPlugin = null;
32824
+ cloudflareAIChatPlugin = null;
32825
+ cloudflareAgentsPlugin = null;
31914
32826
  constructor(config = {}) {
31915
32827
  super();
31916
32828
  this.config = config;
@@ -31952,6 +32864,8 @@ var BraintrustPlugin = class extends BasePlugin {
31952
32864
  if (integrations.huggingface !== false) {
31953
32865
  this.huggingFacePlugin = new HuggingFacePlugin();
31954
32866
  this.huggingFacePlugin.enable();
32867
+ this.huggingFaceTransformersPlugin = new HuggingFaceTransformersPlugin();
32868
+ this.huggingFaceTransformersPlugin.enable();
31955
32869
  }
31956
32870
  if (integrations.openrouter !== false) {
31957
32871
  this.openRouterPlugin = new OpenRouterPlugin();
@@ -31997,6 +32911,14 @@ var BraintrustPlugin = class extends BasePlugin {
31997
32911
  this.strandsAgentSDKPlugin = new StrandsAgentSDKPlugin();
31998
32912
  this.strandsAgentSDKPlugin.enable();
31999
32913
  }
32914
+ if (integrations.cloudflareAIChat !== false) {
32915
+ this.cloudflareAIChatPlugin = new CloudflareAIChatPlugin();
32916
+ this.cloudflareAIChatPlugin.enable();
32917
+ }
32918
+ if (integrations.cloudflareAgents !== false) {
32919
+ this.cloudflareAgentsPlugin = new CloudflareAgentsPlugin();
32920
+ this.cloudflareAgentsPlugin.enable();
32921
+ }
32000
32922
  if (integrations.flue !== false) {
32001
32923
  this.fluePlugin = new FluePlugin();
32002
32924
  this.fluePlugin.enable();
@@ -32049,6 +32971,10 @@ var BraintrustPlugin = class extends BasePlugin {
32049
32971
  this.huggingFacePlugin.disable();
32050
32972
  this.huggingFacePlugin = null;
32051
32973
  }
32974
+ if (this.huggingFaceTransformersPlugin) {
32975
+ this.huggingFaceTransformersPlugin.disable();
32976
+ this.huggingFaceTransformersPlugin = null;
32977
+ }
32052
32978
  if (this.openRouterPlugin) {
32053
32979
  this.openRouterPlugin.disable();
32054
32980
  this.openRouterPlugin = null;
@@ -32093,6 +33019,14 @@ var BraintrustPlugin = class extends BasePlugin {
32093
33019
  this.strandsAgentSDKPlugin.disable();
32094
33020
  this.strandsAgentSDKPlugin = null;
32095
33021
  }
33022
+ if (this.cloudflareAIChatPlugin) {
33023
+ this.cloudflareAIChatPlugin.disable();
33024
+ this.cloudflareAIChatPlugin = null;
33025
+ }
33026
+ if (this.cloudflareAgentsPlugin) {
33027
+ this.cloudflareAgentsPlugin.disable();
33028
+ this.cloudflareAgentsPlugin = null;
33029
+ }
32096
33030
  if (this.fluePlugin) {
32097
33031
  this.fluePlugin.disable();
32098
33032
  this.fluePlugin = null;
@@ -32109,7 +33043,9 @@ var BraintrustPlugin = class extends BasePlugin {
32109
33043
  };
32110
33044
 
32111
33045
  // src/instrumentation/registry.ts
32112
- var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for("braintrust.registry");
33046
+ var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for(
33047
+ `braintrust.registry.global-hooks.v${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION}`
33048
+ );
32113
33049
  function getSharedState() {
32114
33050
  const state = globalThis[/* @__PURE__ */ Symbol.for("braintrust-state")];
32115
33051
  return state && typeof state === "object" ? state : void 0;
@@ -32425,7 +33361,6 @@ function wrapMastraAgent(agent, _options) {
32425
33361
 
32426
33362
  // src/node/config.ts
32427
33363
  import { AsyncLocalStorage } from "node:async_hooks";
32428
- import * as diagnostics_channel from "node:diagnostics_channel";
32429
33364
  import * as path from "node:path";
32430
33365
  import * as fs from "node:fs/promises";
32431
33366
  import * as os from "node:os";
@@ -32712,8 +33647,6 @@ function configureNode() {
32712
33647
  };
32713
33648
  isomorph_default.getCallerLocation = getCallerLocation;
32714
33649
  isomorph_default.newAsyncLocalStorage = () => new AsyncLocalStorage();
32715
- isomorph_default.newTracingChannel = (nameOrChannels) => diagnostics_channel.tracingChannel(nameOrChannels);
32716
- patchTracingChannel(diagnostics_channel.tracingChannel);
32717
33650
  isomorph_default.processOn = (event, handler) => {
32718
33651
  process.on(event, handler);
32719
33652
  };
@@ -32950,6 +33883,7 @@ export {
32950
33883
  braintrustFlueObserver,
32951
33884
  BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME,
32952
33885
  BraintrustLangChainCallbackHandler,
33886
+ instrumentCloudflareAIChatAgent,
32953
33887
  configureInstrumentation,
32954
33888
  BraintrustObservabilityExporter,
32955
33889
  wrapMastraAgent,