braintrust 3.24.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 (80) hide show
  1. package/NOTICE +6 -0
  2. package/dev/dist/index.d.mts +135 -2
  3. package/dev/dist/index.d.ts +135 -2
  4. package/dev/dist/index.js +4426 -2040
  5. package/dev/dist/index.mjs +3594 -1208
  6. package/dist/apply-auto-instrumentation.js +445 -403
  7. package/dist/apply-auto-instrumentation.mjs +249 -207
  8. package/dist/auto-instrumentations/bundler/esbuild.cjs +1449 -799
  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 +1448 -818
  13. package/dist/auto-instrumentations/bundler/next.mjs +6 -29
  14. package/dist/auto-instrumentations/bundler/rollup.cjs +1449 -799
  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 +1449 -799
  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 +1602 -728
  23. package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +20 -12
  24. package/dist/auto-instrumentations/bundler/webpack.cjs +1449 -799
  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-GSZHTAQW.mjs → chunk-6E22MYSW.mjs} +194 -93
  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-BURMPO7L.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-F43DNLPD.mjs → chunk-XZHHE4Y3.mjs} +646 -510
  34. package/dist/auto-instrumentations/hook.mjs +894 -657
  35. package/dist/auto-instrumentations/index.cjs +1125 -490
  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 +824 -197
  49. package/dist/browser.d.ts +824 -197
  50. package/dist/browser.js +3897 -1228
  51. package/dist/browser.mjs +3897 -1228
  52. package/dist/chunk-PN7EWK66.mjs +1784 -0
  53. package/dist/{chunk-SU6EHKJV.js → chunk-VRZZQPAA.js} +3289 -1577
  54. package/dist/chunk-VYNNEKSA.js +1784 -0
  55. package/dist/{chunk-XE5FS7QY.mjs → chunk-XIZOM2HO.mjs} +2470 -758
  56. package/dist/cli.js +3596 -1211
  57. package/dist/edge-light.d.mts +1 -1
  58. package/dist/edge-light.d.ts +1 -1
  59. package/dist/edge-light.js +3897 -1231
  60. package/dist/edge-light.mjs +3897 -1231
  61. package/dist/index.d.mts +824 -197
  62. package/dist/index.d.ts +824 -197
  63. package/dist/index.js +750 -445
  64. package/dist/index.mjs +334 -29
  65. package/dist/instrumentation/index.d.mts +135 -6
  66. package/dist/instrumentation/index.d.ts +135 -6
  67. package/dist/instrumentation/index.js +3696 -1257
  68. package/dist/instrumentation/index.mjs +3696 -1257
  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 +3897 -1231
  74. package/dist/workerd.mjs +3897 -1231
  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
  79. package/dist/chunk-7AUY2XWX.js +0 -1090
  80. package/dist/chunk-7F6GCRHH.mjs +0 -1090
@@ -1,8 +1,14 @@
1
1
  import {
2
+ GLOBAL_INSTRUMENTATION_HOOKS_KEY,
3
+ GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
4
+ GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND,
5
+ GLOBAL_INSTRUMENTATION_HOOK_BRAND,
2
6
  aiSDKChannels,
3
7
  anthropicChannels,
4
8
  classifyMastraTarget,
5
9
  claudeAgentSDKChannels,
10
+ cloudflareAIChatChannels,
11
+ cloudflareAgentsChannels,
6
12
  cohereChannels,
7
13
  cursorSDKChannels,
8
14
  flueChannels,
@@ -14,6 +20,7 @@ import {
14
20
  groqChannels,
15
21
  harnessAgentChannels,
16
22
  huggingFaceChannels,
23
+ huggingFaceTransformersChannels,
17
24
  isInstrumentationIntegrationDisabled,
18
25
  langChainChannels,
19
26
  langSmithChannels,
@@ -24,16 +31,14 @@ import {
24
31
  openRouterAgentChannels,
25
32
  openRouterChannels,
26
33
  patchMastraSource,
27
- patchTracingChannel,
28
34
  piCodingAgentChannels,
29
35
  readDisabledInstrumentationEnvConfig,
30
36
  smithyClientChannels,
31
37
  smithyCoreChannels,
32
38
  strandsAgentSDKChannels
33
- } from "./chunk-7F6GCRHH.mjs";
39
+ } from "./chunk-PN7EWK66.mjs";
34
40
 
35
41
  // src/node/apply-auto-instrumentation-entry.ts
36
- import * as diagnostics_channel from "node:diagnostics_channel";
37
42
  import { register } from "node:module";
38
43
  import { pathToFileURL } from "node:url";
39
44
 
@@ -43,6 +48,7 @@ var aiSDKConfigs = [
43
48
  // `./agent` entrypoint. The compiled class expression is anonymous, so match
44
49
  // the first async method with each public name instead of a class name.
45
50
  ...[
51
+ ["createSession", harnessAgentChannels.createSession.channelName],
46
52
  ["generate", harnessAgentChannels.generate.channelName],
47
53
  ["stream", harnessAgentChannels.stream.channelName],
48
54
  ["continueGenerate", harnessAgentChannels.continueGenerate.channelName],
@@ -633,6 +639,46 @@ var claudeAgentSDKConfigs = [
633
639
  }
634
640
  ];
635
641
 
642
+ // src/auto-instrumentations/configs/cloudflare-ai-chat.ts
643
+ var cloudflareAIChatVersionRange = ">=0.9.0 <0.10.0";
644
+ var cloudflareAIChatConfigs = [
645
+ {
646
+ // AIChatAgent subclasses replace onChatMessage, so there is no stable
647
+ // public implementation for the transformer to target. This runner is
648
+ // the narrowest shared boundary around every chat turn; keep the version
649
+ // range tight because it is an internal method.
650
+ channelName: cloudflareAIChatChannels.runExclusiveChatTurn.channelName,
651
+ module: {
652
+ name: "@cloudflare/ai-chat",
653
+ versionRange: cloudflareAIChatVersionRange,
654
+ filePath: "dist/index.js"
655
+ },
656
+ functionQuery: {
657
+ className: "AIChatAgent",
658
+ methodName: "_runExclusiveChatTurn",
659
+ kind: "Async"
660
+ }
661
+ }
662
+ ];
663
+
664
+ // src/auto-instrumentations/configs/cloudflare-agents.ts
665
+ var cloudflareAgentsVersionRange = ">=0.17.0 <0.18.0";
666
+ var cloudflareAgentsConfigs = [
667
+ {
668
+ channelName: cloudflareAgentsChannels.runAgentTool.channelName,
669
+ module: {
670
+ name: "agents",
671
+ versionRange: cloudflareAgentsVersionRange,
672
+ filePath: "dist/index.js"
673
+ },
674
+ functionQuery: {
675
+ className: "Agent",
676
+ methodName: "runAgentTool",
677
+ kind: "Async"
678
+ }
679
+ }
680
+ ];
681
+
636
682
  // src/auto-instrumentations/configs/cohere.ts
637
683
  var cohereConfigs = [
638
684
  {
@@ -1785,6 +1831,67 @@ var huggingFaceConfigs = [
1785
1831
  }
1786
1832
  ];
1787
1833
 
1834
+ // src/auto-instrumentations/configs/huggingface-transformers.ts
1835
+ var moduleFiles = [
1836
+ {
1837
+ filePath: "dist/transformers.mjs",
1838
+ versionRange: ">=3.0.0 <3.4.0"
1839
+ },
1840
+ {
1841
+ filePath: "dist/transformers.cjs",
1842
+ versionRange: ">=3.0.0 <3.4.0"
1843
+ },
1844
+ {
1845
+ filePath: "dist/transformers.js",
1846
+ versionRange: ">=3.0.0 <3.4.0"
1847
+ },
1848
+ {
1849
+ filePath: "dist/transformers.node.mjs",
1850
+ versionRange: ">=3.4.0 <5.0.0"
1851
+ },
1852
+ {
1853
+ filePath: "dist/transformers.node.cjs",
1854
+ versionRange: ">=3.4.0 <5.0.0"
1855
+ },
1856
+ {
1857
+ filePath: "dist/transformers.web.js",
1858
+ versionRange: ">=3.4.0 <5.0.0"
1859
+ }
1860
+ ];
1861
+ var pipelineClasses = [
1862
+ "TextGenerationPipeline",
1863
+ "Text2TextGenerationPipeline",
1864
+ "FeatureExtractionPipeline",
1865
+ "QuestionAnsweringPipeline"
1866
+ ];
1867
+ var huggingFaceTransformersConfigs = moduleFiles.flatMap(({ filePath, versionRange: versionRange2 }) => [
1868
+ {
1869
+ channelName: huggingFaceTransformersChannels.pipeline.channelName,
1870
+ module: {
1871
+ name: "@huggingface/transformers",
1872
+ versionRange: versionRange2,
1873
+ filePath
1874
+ },
1875
+ functionQuery: {
1876
+ functionName: "pipeline",
1877
+ kind: "Async"
1878
+ }
1879
+ },
1880
+ ...pipelineClasses.map((className) => ({
1881
+ channelName: huggingFaceTransformersChannels.pipelineCall.channelName,
1882
+ module: {
1883
+ name: "@huggingface/transformers",
1884
+ versionRange: versionRange2,
1885
+ filePath
1886
+ },
1887
+ functionQuery: {
1888
+ className,
1889
+ methodName: "_call",
1890
+ kind: "Async"
1891
+ }
1892
+ }))
1893
+ ]);
1894
+
1788
1895
  // src/auto-instrumentations/configs/langchain.ts
1789
1896
  var langChainCoreVersionRange = ">=0.3.42";
1790
1897
  var langChainCallbackManagerFilePath = "dist/callbacks/manager.js";
@@ -2382,7 +2489,7 @@ var openRouterConfigs = [
2382
2489
  channelName: openRouterChannels.chatSend.channelName,
2383
2490
  module: {
2384
2491
  name: "@openrouter/sdk",
2385
- versionRange: ">=0.9.11 <1.0.0",
2492
+ versionRange: ">=0.9.11 <2.0.0",
2386
2493
  filePath: "esm/sdk/chat.js"
2387
2494
  },
2388
2495
  functionQuery: {
@@ -2395,7 +2502,7 @@ var openRouterConfigs = [
2395
2502
  channelName: openRouterChannels.embeddingsGenerate.channelName,
2396
2503
  module: {
2397
2504
  name: "@openrouter/sdk",
2398
- versionRange: ">=0.9.11 <1.0.0",
2505
+ versionRange: ">=0.9.11 <2.0.0",
2399
2506
  filePath: "esm/sdk/embeddings.js"
2400
2507
  },
2401
2508
  functionQuery: {
@@ -2408,7 +2515,7 @@ var openRouterConfigs = [
2408
2515
  channelName: openRouterChannels.rerankRerank.channelName,
2409
2516
  module: {
2410
2517
  name: "@openrouter/sdk",
2411
- versionRange: ">=0.12.0 <1.0.0",
2518
+ versionRange: ">=0.12.0 <2.0.0",
2412
2519
  filePath: "esm/sdk/rerank.js"
2413
2520
  },
2414
2521
  functionQuery: {
@@ -2421,7 +2528,7 @@ var openRouterConfigs = [
2421
2528
  channelName: openRouterChannels.betaResponsesSend.channelName,
2422
2529
  module: {
2423
2530
  name: "@openrouter/sdk",
2424
- versionRange: ">=0.9.11 <1.0.0",
2531
+ versionRange: ">=0.9.11 <2.0.0",
2425
2532
  filePath: "esm/sdk/responses.js"
2426
2533
  },
2427
2534
  functionQuery: {
@@ -2434,7 +2541,7 @@ var openRouterConfigs = [
2434
2541
  channelName: openRouterChannels.callModel.channelName,
2435
2542
  module: {
2436
2543
  name: "@openrouter/sdk",
2437
- versionRange: ">=0.9.11 <1.0.0",
2544
+ versionRange: ">=0.9.11 <2.0.0",
2438
2545
  filePath: "esm/sdk/sdk.js"
2439
2546
  },
2440
2547
  functionQuery: {
@@ -2462,7 +2569,7 @@ var openRouterAgentConfigs = [
2462
2569
  ];
2463
2570
 
2464
2571
  // src/auto-instrumentations/configs/pi-coding-agent.ts
2465
- var piCodingAgentVersionRange = ">=0.79.0 <0.80.0";
2572
+ var piCodingAgentVersionRange = ">=0.79.0 <0.82.0";
2466
2573
  var piCodingAgentConfigs = [
2467
2574
  {
2468
2575
  channelName: piCodingAgentChannels.prompt.channelName,
@@ -2543,6 +2650,14 @@ var defaultInstrumentationConfigGroups = [
2543
2650
  integrations: ["claudeAgentSDK"],
2544
2651
  configs: claudeAgentSDKConfigs
2545
2652
  },
2653
+ {
2654
+ integrations: ["cloudflareAIChat"],
2655
+ configs: cloudflareAIChatConfigs
2656
+ },
2657
+ {
2658
+ integrations: ["cloudflareAgents"],
2659
+ configs: cloudflareAgentsConfigs
2660
+ },
2546
2661
  { integrations: ["cursor", "cursorSDK"], configs: cursorSDKConfigs },
2547
2662
  {
2548
2663
  integrations: ["openAIAgents"],
@@ -2552,7 +2667,10 @@ var defaultInstrumentationConfigGroups = [
2552
2667
  integrations: ["google", "googleGenAI"],
2553
2668
  configs: googleGenAIConfigs
2554
2669
  },
2555
- { integrations: ["huggingface"], configs: huggingFaceConfigs },
2670
+ {
2671
+ integrations: ["huggingface"],
2672
+ configs: [...huggingFaceConfigs, ...huggingFaceTransformersConfigs]
2673
+ },
2556
2674
  {
2557
2675
  integrations: ["langchain", "langgraph"],
2558
2676
  configs: langchainConfigs
@@ -2630,52 +2748,79 @@ import { SourceMapGenerator } from "source-map";
2630
2748
  // src/auto-instrumentations/orchestrion-js/transforms.ts
2631
2749
  import esquery from "esquery";
2632
2750
  import { parse } from "meriyah";
2633
- var tracingChannelPredicate = (node) => node.declarations?.[0]?.id?.properties?.[0]?.value?.name === "tr_ch_apm_tracingChannel";
2634
2751
  var CHANNEL_REGEX = /[^\w]/g;
2752
+ var SHARED_HOOK_LOOKUP = "tr_ch_bt$get_hook";
2635
2753
  function formatChannelVariable(channelName) {
2636
- return `tr_ch_apm$${channelName.replace(CHANNEL_REGEX, "_")}`;
2754
+ return `tr_ch_bt$${channelName.replace(CHANNEL_REGEX, "_")}`;
2755
+ }
2756
+ function formatChannelGetter(channelName) {
2757
+ return `tr_ch_bt$get_${channelName.replace(CHANNEL_REGEX, "_")}`;
2637
2758
  }
2638
2759
  var transforms = {
2639
- tracingChannelImport({ dcModule, moduleType }, node) {
2640
- if (node.body.some(tracingChannelPredicate)) {
2641
- return;
2642
- }
2643
- const options = { module: moduleType === "esm" };
2644
- const index = node.body.findIndex(
2645
- (child) => child.directive === "use strict"
2646
- );
2647
- const dc = moduleType === "esm" ? `import tr_ch_apm_dc from "${dcModule}"` : `const tr_ch_apm_dc = ${"require"}("${dcModule}")`;
2648
- const tracingChannel2 = "const { tracingChannel: tr_ch_apm_tracingChannel } = tr_ch_apm_dc";
2649
- const hasSubscribers = `const tr_ch_apm_hasSubscribers = ch => ch.start.hasSubscribers
2650
- || ch.end.hasSubscribers
2651
- || ch.asyncStart.hasSubscribers
2652
- || ch.asyncEnd.hasSubscribers
2653
- || ch.error.hasSubscribers`;
2654
- node.body.splice(
2655
- index + 1,
2656
- 0,
2657
- parse(dc, options).body[0],
2658
- parse(tracingChannel2, options).body[0],
2659
- parse(hasSubscribers, options).body[0]
2660
- );
2661
- },
2662
- tracingChannelDeclaration(state2, node) {
2760
+ tracingHookDeclaration(state2, node) {
2663
2761
  const {
2664
2762
  channelName,
2665
- module: { name }
2763
+ module: { name },
2764
+ operator
2666
2765
  } = state2;
2667
2766
  const channelVariable = formatChannelVariable(channelName);
2767
+ const channelGetter = formatChannelGetter(channelName);
2768
+ const hasSharedHookLookup = node.body.some(
2769
+ (child) => child.declarations?.[0]?.id?.name === SHARED_HOOK_LOOKUP
2770
+ );
2668
2771
  if (node.body.some(
2669
- (child) => child.declarations?.[0]?.id?.name === channelVariable
2772
+ (child) => child.declarations?.[0]?.id?.name === channelGetter
2670
2773
  )) {
2671
2774
  return;
2672
2775
  }
2673
- transforms.tracingChannelImport(state2, node, null, []);
2674
- const index = node.body.findIndex(tracingChannelPredicate);
2776
+ const sharedHookLookup = hasSharedHookLookup ? "" : `
2777
+ const ${SHARED_HOOK_LOOKUP} = (__bt$hookName, __bt$operator) => {
2778
+ try {
2779
+ const __bt$hooks = globalThis[${JSON.stringify(
2780
+ GLOBAL_INSTRUMENTATION_HOOKS_KEY
2781
+ )}];
2782
+ if (
2783
+ !(__bt$hooks instanceof Map) ||
2784
+ __bt$hooks[Symbol.for(${JSON.stringify(
2785
+ GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND
2786
+ )})] !== ${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION}
2787
+ ) return undefined;
2788
+ const __bt$hook = Map.prototype.get.call(
2789
+ __bt$hooks,
2790
+ __bt$hookName
2791
+ );
2792
+ if (
2793
+ (__bt$hook === null ||
2794
+ (typeof __bt$hook !== "object" &&
2795
+ typeof __bt$hook !== "function")) ||
2796
+ __bt$hook[Symbol.for(${JSON.stringify(
2797
+ GLOBAL_INSTRUMENTATION_HOOK_BRAND
2798
+ )})] !== ${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION} ||
2799
+ typeof __bt$hook.hasSubscribers !== "boolean" ||
2800
+ typeof __bt$hook[__bt$operator] !== "function"
2801
+ ) return undefined;
2802
+ return __bt$hook;
2803
+ } catch {
2804
+ return undefined;
2805
+ }
2806
+ };
2807
+ `;
2675
2808
  const code = `
2676
- const ${channelVariable} = tr_ch_apm_tracingChannel("orchestrion:${name}:${channelName}")
2809
+ ${sharedHookLookup}
2810
+ let ${channelVariable};
2811
+ const ${channelGetter} = () =>
2812
+ ${channelVariable} ??= ${SHARED_HOOK_LOOKUP}(
2813
+ ${JSON.stringify(`orchestrion:${name}:${channelName}`)},
2814
+ ${JSON.stringify(operator)}
2815
+ );
2677
2816
  `;
2678
- node.body.splice(index + 1, 0, parse(code).body[0]);
2817
+ const sharedHookLookupIndex = node.body.findIndex(
2818
+ (child) => child.declarations?.[0]?.id?.name === SHARED_HOOK_LOOKUP
2819
+ );
2820
+ const index = sharedHookLookupIndex === -1 ? node.body.findIndex(
2821
+ (child) => child.directive === "use strict"
2822
+ ) : sharedHookLookupIndex;
2823
+ node.body.splice(index + 1, 0, ...parse(code).body);
2679
2824
  },
2680
2825
  traceCallback: traceAny,
2681
2826
  tracePromise: traceAny,
@@ -2690,25 +2835,21 @@ function traceAny(state2, node, _parent, ancestry) {
2690
2835
  }
2691
2836
  }
2692
2837
  function traceFunction(state2, node, program) {
2693
- transforms.tracingChannelDeclaration(state2, program, null, []);
2838
+ transforms.tracingHookDeclaration(state2, program, null, []);
2694
2839
  const { functionQuery } = state2;
2695
2840
  const methodName = "methodName" in functionQuery ? functionQuery.methodName : void 0;
2696
2841
  const privateMethodName = "privateMethodName" in functionQuery ? functionQuery.privateMethodName : void 0;
2697
2842
  const functionName = "functionName" in functionQuery ? functionQuery.functionName : void 0;
2698
2843
  const isConstructor = methodName === "constructor" || !methodName && !privateMethodName && !functionName;
2699
2844
  const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression";
2700
- node.body = wrap(
2701
- state2,
2702
- {
2703
- type,
2704
- params: node.params,
2705
- body: node.body,
2706
- async: node.async,
2707
- expression: false,
2708
- generator: node.generator
2709
- },
2710
- program
2711
- );
2845
+ node.body = wrap(state2, {
2846
+ type,
2847
+ params: node.params,
2848
+ body: node.body,
2849
+ async: node.async,
2850
+ expression: false,
2851
+ generator: node.generator
2852
+ });
2712
2853
  node.generator = false;
2713
2854
  node.async = false;
2714
2855
  wrapSuper(node);
@@ -2720,11 +2861,11 @@ function traceInstanceMethod(state2, node, program) {
2720
2861
  return;
2721
2862
  }
2722
2863
  const classBody = node.body;
2723
- if (classBody.body.some(({ key }) => key.name === methodName)) {
2864
+ if (classBody.body.some(({ key }) => key?.name === methodName)) {
2724
2865
  return;
2725
2866
  }
2726
2867
  let ctor = classBody.body.find(({ kind }) => kind === "constructor");
2727
- transforms.tracingChannelDeclaration(state2, program, null, []);
2868
+ transforms.tracingHookDeclaration(state2, program, null, []);
2728
2869
  if (!ctor) {
2729
2870
  ctor = parse(
2730
2871
  node.superClass ? "class A extends Object { constructor (...args) { super(...args) } }" : "class A { constructor () {} }"
@@ -2732,47 +2873,46 @@ function traceInstanceMethod(state2, node, program) {
2732
2873
  classBody.body.unshift(ctor);
2733
2874
  }
2734
2875
  const ctorBody = parse(`
2735
- const __apm$${methodName} = this["${methodName}"]
2876
+ const __bt$${methodName} = this["${methodName}"]
2736
2877
  this["${methodName}"] = function () {}
2737
2878
  `).body;
2738
2879
  const fn = ctorBody[1].expression.right;
2739
2880
  fn.async = operator === "tracePromise";
2740
- fn.body = wrap(
2741
- state2,
2742
- { type: "Identifier", name: `__apm$${methodName}` },
2743
- program
2744
- );
2881
+ fn.body = wrap(state2, {
2882
+ type: "Identifier",
2883
+ name: `__bt$${methodName}`
2884
+ });
2745
2885
  wrapSuper(fn);
2746
2886
  ctor.value.body.body.push(...ctorBody);
2747
2887
  }
2748
- function wrap(state2, node, program) {
2888
+ function wrap(state2, node) {
2749
2889
  const { operator, moduleVersion } = state2;
2750
2890
  const wrapper = operator === "traceCallback" ? wrapCallback(state2) : operator === "tracePromise" ? wrapPromise(state2) : wrapSync(state2);
2751
2891
  const block = wrapper.body[0].body;
2752
2892
  const common = parse(
2753
2893
  node.type === "ArrowFunctionExpression" ? `
2754
- const __apm$ctx = {
2894
+ const __bt$ctx = {
2755
2895
  arguments,
2756
2896
  moduleVersion: ${JSON.stringify(moduleVersion)}
2757
2897
  };
2758
- const __apm$traced = () => {
2759
- const __apm$wrapped = () => {};
2760
- return __apm$wrapped(...arguments);
2898
+ const __bt$traced = () => {
2899
+ const __bt$wrapped = () => {};
2900
+ return __bt$wrapped(...arguments);
2761
2901
  };
2762
2902
  ` : `
2763
- const __apm$ctx = {
2903
+ const __bt$ctx = {
2764
2904
  arguments,
2765
2905
  self: this,
2766
2906
  moduleVersion: ${JSON.stringify(moduleVersion)}
2767
2907
  };
2768
- const __apm$traced = () => {
2769
- const __apm$wrapped = () => {};
2770
- return __apm$wrapped.apply(this, arguments);
2908
+ const __bt$traced = () => {
2909
+ const __bt$wrapped = () => {};
2910
+ return __bt$wrapped.apply(this, arguments);
2771
2911
  };
2772
2912
  `
2773
2913
  ).body;
2774
2914
  block.body.unshift(...common);
2775
- esquery.query(block, "[id.name=__apm$wrapped]")[0].init = node;
2915
+ esquery.query(block, "[id.name=__bt$wrapped]")[0].init = node;
2776
2916
  return block;
2777
2917
  }
2778
2918
  function wrapSuper(node) {
@@ -2784,11 +2924,11 @@ function wrapSuper(node) {
2784
2924
  const { name } = node2.property;
2785
2925
  let child;
2786
2926
  if (parent.callee) {
2787
- const { expression } = parse(`__apm$super['${name}'].call(this)`).body[0];
2927
+ const { expression } = parse(`__bt$super['${name}'].call(this)`).body[0];
2788
2928
  parent.callee = child = expression.callee;
2789
2929
  parent.arguments.unshift(...expression.arguments);
2790
2930
  } else {
2791
- parent.expression = child = parse(`__apm$super['${name}']`).body[0];
2931
+ parent.expression = child = parse(`__bt$super['${name}']`).body[0];
2792
2932
  }
2793
2933
  child.computed = parent.callee.computed;
2794
2934
  child.optional = parent.callee.optional;
@@ -2799,14 +2939,14 @@ function wrapSuper(node) {
2799
2939
  const member = parse(`
2800
2940
  class Wrapper {
2801
2941
  wrapper () {
2802
- __apm$super['${name}'] = super['${name}']
2942
+ __bt$super['${name}'] = super['${name}']
2803
2943
  }
2804
2944
  }
2805
2945
  `).body[0].body.body[0].value.body.body[0];
2806
2946
  node.body.body.unshift(member);
2807
2947
  }
2808
2948
  if (members.size > 0) {
2809
- node.body.body.unshift(parse("const __apm$super = {}").body[0]);
2949
+ node.body.body.unshift(parse("const __bt$super = {}").body[0]);
2810
2950
  }
2811
2951
  }
2812
2952
  function wrapCallback(state2) {
@@ -2814,136 +2954,41 @@ function wrapCallback(state2) {
2814
2954
  channelName,
2815
2955
  functionQuery: { callbackIndex = -1 }
2816
2956
  } = state2;
2817
- const channelVariable = formatChannelVariable(channelName);
2957
+ const channelGetter = formatChannelGetter(channelName);
2818
2958
  return parse(`
2819
2959
  function wrapper () {
2820
- const __apm$cb = Array.prototype.at.call(arguments, ${callbackIndex});
2821
-
2822
- if (!${channelVariable}.start.hasSubscribers) return __apm$traced();
2823
-
2824
- function __apm$wrappedCb(err, res) {
2825
- if (err) {
2826
- __apm$ctx.error = err;
2827
- ${channelVariable}.error.publish(__apm$ctx);
2828
- } else {
2829
- __apm$ctx.result = res;
2830
- }
2831
-
2832
- ${channelVariable}.asyncStart.runStores(__apm$ctx, () => {
2833
- try {
2834
- if (__apm$cb) {
2835
- return __apm$cb.apply(this, arguments);
2836
- }
2837
- } finally {
2838
- ${channelVariable}.asyncEnd.publish(__apm$ctx);
2839
- }
2840
- });
2841
- }
2842
-
2843
- if (typeof __apm$cb !== 'function') {
2844
- return __apm$traced();
2845
- }
2846
- Array.prototype.splice.call(arguments, ${callbackIndex}, 1, __apm$wrappedCb);
2847
-
2848
- return ${channelVariable}.start.runStores(__apm$ctx, () => {
2849
- try {
2850
- return __apm$traced();
2851
- } catch (err) {
2852
- __apm$ctx.error = err;
2853
- ${channelVariable}.error.publish(__apm$ctx);
2854
- throw err;
2855
- } finally {
2856
- __apm$ctx.self ??= this;
2857
- ${channelVariable}.end.publish(__apm$ctx);
2858
- }
2859
- });
2960
+ const __bt$hook = ${channelGetter}();
2961
+ if (!__bt$hook?.hasSubscribers) return __bt$traced();
2962
+ __bt$ctx.self ??= this;
2963
+ return __bt$hook.traceCallback(
2964
+ __bt$traced,
2965
+ ${callbackIndex},
2966
+ __bt$ctx
2967
+ );
2860
2968
  }
2861
2969
  `);
2862
2970
  }
2863
2971
  function wrapPromise(state2) {
2864
2972
  const { channelName } = state2;
2865
- const channelVariable = formatChannelVariable(channelName);
2973
+ const channelGetter = formatChannelGetter(channelName);
2866
2974
  return parse(`
2867
2975
  function wrapper () {
2868
- if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
2869
-
2870
- return ${channelVariable}.start.runStores(__apm$ctx, () => {
2871
- try {
2872
- let promise = __apm$traced();
2873
- if (typeof promise?.then !== 'function') {
2874
- __apm$ctx.result = promise;
2875
- return promise;
2876
- }
2877
- // Mirror Node.js core diagnostics_channel behaviour: for native Promise
2878
- // instances, chain normally (safe since there is no subclass API to
2879
- // preserve). For Promise subclasses and other thenables, side-chain the
2880
- // callbacks for event publishing and return the original so that any
2881
- // subclass-specific methods (e.g. APIPromise.withResponse()) remain
2882
- // accessible to the caller.
2883
- if (promise instanceof Promise && promise.constructor === Promise) {
2884
- return promise.then(
2885
- result => {
2886
- __apm$ctx.result = result;
2887
- ${channelVariable}.asyncStart.publish(__apm$ctx);
2888
- ${channelVariable}.asyncEnd.publish(__apm$ctx);
2889
- return result;
2890
- },
2891
- err => {
2892
- __apm$ctx.error = err;
2893
- ${channelVariable}.error.publish(__apm$ctx);
2894
- ${channelVariable}.asyncStart.publish(__apm$ctx);
2895
- ${channelVariable}.asyncEnd.publish(__apm$ctx);
2896
- throw err;
2897
- }
2898
- );
2899
- }
2900
- promise.then(
2901
- result => {
2902
- __apm$ctx.result = result;
2903
- ${channelVariable}.asyncStart.publish(__apm$ctx);
2904
- ${channelVariable}.asyncEnd.publish(__apm$ctx);
2905
- },
2906
- err => {
2907
- __apm$ctx.error = err;
2908
- ${channelVariable}.error.publish(__apm$ctx);
2909
- ${channelVariable}.asyncStart.publish(__apm$ctx);
2910
- ${channelVariable}.asyncEnd.publish(__apm$ctx);
2911
- }
2912
- );
2913
- return promise;
2914
- } catch (err) {
2915
- __apm$ctx.error = err;
2916
- ${channelVariable}.error.publish(__apm$ctx);
2917
- throw err;
2918
- } finally {
2919
- __apm$ctx.self ??= this;
2920
- ${channelVariable}.end.publish(__apm$ctx);
2921
- }
2922
- });
2976
+ const __bt$hook = ${channelGetter}();
2977
+ if (!__bt$hook?.hasSubscribers) return __bt$traced();
2978
+ __bt$ctx.self ??= this;
2979
+ return __bt$hook.tracePromise(__bt$traced, __bt$ctx);
2923
2980
  }
2924
2981
  `);
2925
2982
  }
2926
2983
  function wrapSync(state2) {
2927
2984
  const { channelName } = state2;
2928
- const channelVariable = formatChannelVariable(channelName);
2985
+ const channelGetter = formatChannelGetter(channelName);
2929
2986
  return parse(`
2930
2987
  function wrapper () {
2931
- if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
2932
-
2933
- return ${channelVariable}.start.runStores(__apm$ctx, () => {
2934
- try {
2935
- const result = __apm$traced();
2936
- __apm$ctx.result = result;
2937
- return result;
2938
- } catch (err) {
2939
- __apm$ctx.error = err;
2940
- ${channelVariable}.error.publish(__apm$ctx);
2941
- throw err;
2942
- } finally {
2943
- __apm$ctx.self ??= this;
2944
- ${channelVariable}.end.publish(__apm$ctx);
2945
- }
2946
- });
2988
+ const __bt$hook = ${channelGetter}();
2989
+ if (!__bt$hook?.hasSubscribers) return __bt$traced();
2990
+ __bt$ctx.self ??= this;
2991
+ return __bt$hook.traceSync(__bt$traced, __bt$ctx);
2947
2992
  }
2948
2993
  `);
2949
2994
  }
@@ -2954,16 +2999,14 @@ var Transformer = class {
2954
2999
  version;
2955
3000
  filePath;
2956
3001
  configs = [];
2957
- dcModule;
2958
- constructor(moduleName, version, filePath, configs, dcModule) {
3002
+ constructor(moduleName, version, filePath, configs) {
2959
3003
  this.moduleName = moduleName;
2960
3004
  this.version = version;
2961
3005
  this.filePath = filePath;
2962
3006
  this.configs = configs;
2963
- this.dcModule = dcModule;
2964
3007
  }
2965
3008
  /**
2966
- * Instruments `code` by injecting diagnostics_channel tracing around the
3009
+ * Instruments `code` by injecting global tracing hooks around the
2967
3010
  * target functions defined by this transformer's configs.
2968
3011
  */
2969
3012
  transform(code, moduleType) {
@@ -3001,8 +3044,6 @@ var Transformer = class {
3001
3044
  const query = astQuery || this.fromFunctionQuery(resolvedFunctionQuery);
3002
3045
  const state2 = {
3003
3046
  ...config,
3004
- dcModule: this.dcModule,
3005
- moduleType,
3006
3047
  moduleVersion: this.version,
3007
3048
  functionQuery: resolvedFunctionQuery,
3008
3049
  operator: this.getOperator(resolvedFunctionQuery.kind)
@@ -3152,11 +3193,9 @@ var Transformer = class {
3152
3193
  // src/auto-instrumentations/orchestrion-js/matcher.ts
3153
3194
  var InstrumentationMatcher = class {
3154
3195
  configs = [];
3155
- dcModule;
3156
3196
  transformers = {};
3157
- constructor(configs, dcModule) {
3197
+ constructor(configs) {
3158
3198
  this.configs = configs;
3159
- this.dcModule = dcModule || "diagnostics_channel";
3160
3199
  }
3161
3200
  /**
3162
3201
  * Returns a Transformer for the given module/file/version, or undefined if no
@@ -3178,8 +3217,7 @@ var InstrumentationMatcher = class {
3178
3217
  moduleName,
3179
3218
  version,
3180
3219
  filePath,
3181
- configs,
3182
- this.dcModule
3220
+ configs
3183
3221
  );
3184
3222
  return this.transformers[id];
3185
3223
  }
@@ -3189,8 +3227,8 @@ var InstrumentationMatcher = class {
3189
3227
  };
3190
3228
 
3191
3229
  // src/auto-instrumentations/orchestrion-js/index.ts
3192
- function create(configs, dcModule) {
3193
- return new InstrumentationMatcher(configs, dcModule);
3230
+ function create(configs) {
3231
+ return new InstrumentationMatcher(configs);
3194
3232
  }
3195
3233
 
3196
3234
  // src/auto-instrumentations/loader/cjs-patch.ts
@@ -3279,6 +3317,9 @@ function applySpecialCasePatch(input) {
3279
3317
  if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
3280
3318
  return input.source + OPENAI_API_PROMISE_PATCH;
3281
3319
  }
3320
+ if (input.browser) {
3321
+ return null;
3322
+ }
3282
3323
  const mastraTarget = classifyMastraTarget(
3283
3324
  input.packageName,
3284
3325
  input.modulePath
@@ -3307,7 +3348,7 @@ var ModulePatch = class {
3307
3348
  /**
3308
3349
  * Patches the Node.js module class method that is responsible for compiling code.
3309
3350
  * If a module is found that has an instrumentator, it will transform the code before compiling it
3310
- * with tracing channel methods.
3351
+ * with global hook calls.
3311
3352
  */
3312
3353
  patch() {
3313
3354
  const self = this;
@@ -3366,7 +3407,9 @@ function resolveModulePrototype() {
3366
3407
  }
3367
3408
 
3368
3409
  // src/node/apply-auto-instrumentation-entry.ts
3369
- var stateKey = /* @__PURE__ */ Symbol.for("braintrust.applyAutoInstrumentation");
3410
+ var stateKey = /* @__PURE__ */ Symbol.for(
3411
+ `braintrust.applyAutoInstrumentation.global-hooks.v${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION}`
3412
+ );
3370
3413
  var existingState = Object.getOwnPropertyDescriptor(
3371
3414
  globalThis,
3372
3415
  stateKey
@@ -3383,7 +3426,6 @@ if (state !== existingState) {
3383
3426
  });
3384
3427
  }
3385
3428
  if (!state.applied) {
3386
- patchTracingChannel(diagnostics_channel.tracingChannel);
3387
3429
  const allConfigs = getDefaultAutoInstrumentationConfigs();
3388
3430
  const currentModuleUrl = getCurrentModuleUrl();
3389
3431
  register("./auto-instrumentations/loader/esm-hook.mjs", {