braintrust 3.13.0 → 3.15.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 (50) hide show
  1. package/dev/dist/index.d.mts +6 -8
  2. package/dev/dist/index.d.ts +6 -8
  3. package/dev/dist/index.js +1399 -1466
  4. package/dev/dist/index.mjs +971 -1038
  5. package/dist/apply-auto-instrumentation.js +208 -188
  6. package/dist/apply-auto-instrumentation.mjs +40 -20
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +230 -40
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -1
  9. package/dist/auto-instrumentations/bundler/next.cjs +230 -40
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -2
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +230 -40
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -1
  13. package/dist/auto-instrumentations/bundler/vite.cjs +230 -40
  14. package/dist/auto-instrumentations/bundler/vite.mjs +2 -1
  15. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +13 -39
  16. package/dist/auto-instrumentations/bundler/webpack.cjs +230 -40
  17. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -2
  18. package/dist/auto-instrumentations/{chunk-WFEUJACP.mjs → chunk-CNQ7BUKN.mjs} +1 -1
  19. package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +208 -0
  20. package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +280 -0
  21. package/dist/auto-instrumentations/{chunk-GJOO4ESL.mjs → chunk-VXJONZVX.mjs} +28 -40
  22. package/dist/auto-instrumentations/hook.mjs +7985 -46
  23. package/dist/auto-instrumentations/loader/cjs-patch.cjs +194 -4
  24. package/dist/auto-instrumentations/loader/cjs-patch.mjs +13 -27
  25. package/dist/auto-instrumentations/loader/esm-hook.mjs +24 -10
  26. package/dist/browser.d.mts +138 -26
  27. package/dist/browser.d.ts +138 -26
  28. package/dist/browser.js +898 -1063
  29. package/dist/browser.mjs +898 -1063
  30. package/dist/{chunk-75IQCUB2.mjs → chunk-O4ZIWXO3.mjs} +179 -25
  31. package/dist/{chunk-26JGOELH.js → chunk-VMBQETG3.js} +179 -25
  32. package/dist/cli.js +990 -1077
  33. package/dist/edge-light.d.mts +1 -1
  34. package/dist/edge-light.d.ts +1 -1
  35. package/dist/edge-light.js +898 -1063
  36. package/dist/edge-light.mjs +898 -1063
  37. package/dist/index.d.mts +138 -26
  38. package/dist/index.d.ts +138 -26
  39. package/dist/index.js +1690 -1825
  40. package/dist/index.mjs +918 -1053
  41. package/dist/instrumentation/index.d.mts +18 -9
  42. package/dist/instrumentation/index.d.ts +18 -9
  43. package/dist/instrumentation/index.js +711 -1038
  44. package/dist/instrumentation/index.mjs +710 -1038
  45. package/dist/workerd.d.mts +1 -1
  46. package/dist/workerd.d.ts +1 -1
  47. package/dist/workerd.js +898 -1063
  48. package/dist/workerd.mjs +898 -1063
  49. package/package.json +1 -7
  50. package/dist/auto-instrumentations/chunk-MWZXZQUO.mjs +0 -81
@@ -63,6 +63,7 @@ var envIntegrationAliases = {
63
63
  cursorsdk: "cursorSDK",
64
64
  flue: "flue",
65
65
  "flue-runtime": "flue",
66
+ mastra: "mastra",
66
67
  "openai-agents": "openAIAgents",
67
68
  openaiagents: "openAIAgents",
68
69
  "openai-agents-core": "openAIAgents",
@@ -1129,35 +1130,11 @@ var flueChannels = defineChannels("@flue/runtime", {
1129
1130
  createContext: channel({
1130
1131
  channelName: "createFlueContext",
1131
1132
  kind: "sync-stream"
1132
- }),
1133
- openSession: channel({
1134
- channelName: "Harness.openSession",
1135
- kind: "async"
1136
- }),
1137
- contextEvent: channel({
1138
- channelName: "context.event",
1139
- kind: "sync-stream"
1140
- }),
1141
- prompt: channel({
1142
- channelName: "session.prompt",
1143
- kind: "async"
1144
- }),
1145
- skill: channel({
1146
- channelName: "session.skill",
1147
- kind: "async"
1148
- }),
1149
- task: channel({
1150
- channelName: "session.task",
1151
- kind: "async"
1152
- }),
1153
- compact: channel({
1154
- channelName: "session.compact",
1155
- kind: "async"
1156
1133
  })
1157
1134
  });
1158
1135
 
1159
1136
  // src/auto-instrumentations/configs/flue.ts
1160
- var flueVersionRange = ">=0.7.0 <0.8.0";
1137
+ var flueVersionRange = ">=0.8.0";
1161
1138
  var flueConfigs = [
1162
1139
  {
1163
1140
  channelName: flueChannels.createContext.channelName,
@@ -1170,19 +1147,6 @@ var flueConfigs = [
1170
1147
  functionName: "createFlueContext",
1171
1148
  kind: "Sync"
1172
1149
  }
1173
- },
1174
- {
1175
- channelName: flueChannels.openSession.channelName,
1176
- module: {
1177
- name: "@flue/runtime",
1178
- versionRange: flueVersionRange,
1179
- filePath: "dist/internal.mjs"
1180
- },
1181
- functionQuery: {
1182
- className: "Harness",
1183
- methodName: "openSession",
1184
- kind: "Async"
1185
- }
1186
1150
  }
1187
1151
  ];
1188
1152
 
@@ -2959,7 +2923,6 @@ var defaultInstrumentationConfigGroups = [
2959
2923
  configs: claudeAgentSDKConfigs
2960
2924
  },
2961
2925
  { integrations: ["cursor", "cursorSDK"], configs: cursorSDKConfigs },
2962
- { integrations: ["flue"], configs: flueConfigs },
2963
2926
  {
2964
2927
  integrations: ["openAIAgents"],
2965
2928
  configs: openAIAgentsCoreConfigs
@@ -2989,7 +2952,18 @@ var defaultInstrumentationConfigGroups = [
2989
2952
  {
2990
2953
  integrations: ["gitHubCopilot"],
2991
2954
  configs: gitHubCopilotConfigs
2955
+ },
2956
+ {
2957
+ integrations: ["flue"],
2958
+ configs: flueConfigs
2992
2959
  }
2960
+ // Note: `@mastra/core` is not listed here because its instrumentation
2961
+ // doesn't go through the AST `code-transformer` matcher — Mastra's
2962
+ // content-hashed chunks make `filePath`-based matching too brittle.
2963
+ // Instead it's handled by the source-replacement entry in
2964
+ // `loader/special-case-patches.ts`, which both the runtime loader
2965
+ // (`hook.mjs` → `cjs-patch.ts`/`esm-hook.mts`) and the bundler plugin
2966
+ // (`bundler/plugin.ts`) call. The `mastra` env-var disable still works.
2993
2967
  ];
2994
2968
  function getDefaultInstrumentationConfigs({
2995
2969
  additionalInstrumentations,
@@ -3007,6 +2981,211 @@ function getDefaultInstrumentationConfigs({
3007
2981
  ];
3008
2982
  }
3009
2983
 
2984
+ // src/auto-instrumentations/loader/mastra-observability-patch.ts
2985
+ var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
2986
+ var MASTRA_CORE_PACKAGE = "@mastra/core";
2987
+ var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
2988
+ var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
2989
+ "dist/index.js",
2990
+ "dist/index.cjs",
2991
+ "dist/mastra/index.js",
2992
+ "dist/mastra/index.cjs"
2993
+ ]);
2994
+ var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
2995
+ "dist/index.js",
2996
+ "dist/index.cjs"
2997
+ ]);
2998
+ function classifyMastraTarget(packageName, modulePath) {
2999
+ if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
3000
+ return "core";
3001
+ }
3002
+ if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
3003
+ return "observability";
3004
+ }
3005
+ return null;
3006
+ }
3007
+ function extractChunkPath(source) {
3008
+ const esmMatch = source.match(
3009
+ /export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
3010
+ );
3011
+ if (esmMatch) return esmMatch[1];
3012
+ const cjsMatch = source.match(
3013
+ /require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
3014
+ );
3015
+ if (cjsMatch) return cjsMatch[1];
3016
+ return null;
3017
+ }
3018
+ var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
3019
+ var MASTRA_PROXY_HANDLER_BODY = `
3020
+ {
3021
+ construct(target, args, newTarget) {
3022
+ var firstArg = args[0];
3023
+ if (
3024
+ (!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
3025
+ __braintrustObservabilityClass
3026
+ ) {
3027
+ try {
3028
+ // serviceName is required by Mastra's Observability validator; pass
3029
+ // something sensible by default. Users who want a different name
3030
+ // should construct Observability themselves.
3031
+ var observability = new __braintrustObservabilityClass({
3032
+ configs: { default: { serviceName: "mastra" } },
3033
+ });
3034
+ args = args.slice();
3035
+ args[0] = Object.assign({}, firstArg, { observability: observability });
3036
+ } catch (e) {
3037
+ // Fall through. Mastra will use its own NoOp; user code still works,
3038
+ // just without auto-instrumentation.
3039
+ }
3040
+ }
3041
+ return Reflect.construct(target, args, newTarget);
3042
+ },
3043
+ }`;
3044
+ function buildMastraEsmWrapper(chunkPath) {
3045
+ const chunk = JSON.stringify(chunkPath);
3046
+ return `import { Mastra as __braintrustOrigMastra } from ${chunk};
3047
+ import { createRequire as __braintrustCreateRequire } from "node:module";
3048
+
3049
+ let __braintrustObservabilityClass = null;
3050
+ try {
3051
+ // Resolve @mastra/observability relative to this module (the Mastra entry),
3052
+ // so it's looked up from the user's node_modules tree.
3053
+ const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
3054
+ __braintrustObservabilityClass =
3055
+ __braintrustRequire("@mastra/observability").Observability;
3056
+ } catch (e) {
3057
+ // @mastra/observability isn't installed; the construct trap will skip the
3058
+ // auto-construct branch and Mastra falls back to its own NoOp.
3059
+ }
3060
+ const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
3061
+ export { Mastra };
3062
+ `;
3063
+ }
3064
+ function buildMastraCjsWrapper(chunkPath) {
3065
+ const chunk = JSON.stringify(chunkPath);
3066
+ return `'use strict';
3067
+ const __braintrustChunk = require(${chunk});
3068
+
3069
+ let __braintrustObservabilityClass = null;
3070
+ try {
3071
+ __braintrustObservabilityClass = require("@mastra/observability").Observability;
3072
+ } catch (e) {
3073
+ // @mastra/observability isn't installed; same fallback as the ESM wrapper.
3074
+ }
3075
+
3076
+ const __braintrustWrappedMastra = new Proxy(
3077
+ __braintrustChunk.Mastra,
3078
+ ${MASTRA_PROXY_HANDLER_BODY},
3079
+ );
3080
+ Object.defineProperty(exports, "Mastra", {
3081
+ enumerable: true,
3082
+ configurable: true,
3083
+ get: function () { return __braintrustWrappedMastra; }
3084
+ });
3085
+ `;
3086
+ }
3087
+ var OBSERVABILITY_APPEND_BODY = `
3088
+ ;(function __braintrustWrapObservability() {
3089
+ // Top-level so we can both read and reassign the var binding the original
3090
+ // entry declared.
3091
+ if (typeof Observability === "undefined") return;
3092
+ if (Observability.__braintrustWrapped) return;
3093
+ function __braintrustEnsureExporter(rawConfig) {
3094
+ try {
3095
+ var factory = globalThis[${EXPORTER_FACTORY_KEY}];
3096
+ if (typeof factory !== "function") return rawConfig;
3097
+ var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
3098
+ var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
3099
+ var configsOut = {};
3100
+ var hadEntries = false;
3101
+ if (configsIn) {
3102
+ for (var name in configsIn) {
3103
+ if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
3104
+ hadEntries = true;
3105
+ var inst = configsIn[name] || {};
3106
+ var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
3107
+ var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
3108
+ configsOut[name] = Object.assign({}, inst, {
3109
+ exporters: hasOurs ? existing : existing.concat([factory()]),
3110
+ });
3111
+ }
3112
+ }
3113
+ if (!hadEntries) {
3114
+ configsOut.default = {
3115
+ serviceName: "mastra",
3116
+ exporters: [factory()],
3117
+ };
3118
+ }
3119
+ return Object.assign({}, config, { configs: configsOut });
3120
+ } catch (e) {
3121
+ return rawConfig;
3122
+ }
3123
+ }
3124
+ var __OriginalObservability = Observability;
3125
+ Observability = new Proxy(__OriginalObservability, {
3126
+ construct: function (target, args, newTarget) {
3127
+ var nextArgs = args.slice();
3128
+ nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
3129
+ return Reflect.construct(target, nextArgs, newTarget);
3130
+ },
3131
+ });
3132
+ Observability.__braintrustWrapped = true;
3133
+ if (typeof exports !== "undefined" && exports && typeof exports === "object") {
3134
+ try {
3135
+ Object.defineProperty(exports, "Observability", {
3136
+ enumerable: true,
3137
+ configurable: true,
3138
+ get: function () { return Observability; },
3139
+ });
3140
+ } catch (e) {}
3141
+ }
3142
+ })();
3143
+ `;
3144
+ function patchMastraSource(source, target, format) {
3145
+ if (target === "core") {
3146
+ const chunkPath = extractChunkPath(source);
3147
+ if (!chunkPath) return source;
3148
+ return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
3149
+ }
3150
+ return source + OBSERVABILITY_APPEND_BODY;
3151
+ }
3152
+
3153
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
3154
+ var OPENAI_API_PROMISE_PATCH = `
3155
+ ;(function __btPatchAPIPromise() {
3156
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
3157
+ APIPromise.prototype.__btParsePatched = true;
3158
+ var _origThen = APIPromise.prototype.then;
3159
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
3160
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
3161
+ this.__btParseWrapped = true;
3162
+ var _origParse = this.parseResponse;
3163
+ var _cached;
3164
+ this.parseResponse = function() {
3165
+ if (!_cached) _cached = _origParse.apply(this, arguments);
3166
+ return _cached;
3167
+ };
3168
+ }
3169
+ return _origThen.call(this, onfulfilled, onrejected);
3170
+ };
3171
+ })();
3172
+ `;
3173
+
3174
+ // src/auto-instrumentations/loader/special-case-patches.ts
3175
+ function applySpecialCasePatch(input) {
3176
+ if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
3177
+ return input.source + OPENAI_API_PROMISE_PATCH;
3178
+ }
3179
+ const mastraTarget = classifyMastraTarget(
3180
+ input.packageName,
3181
+ input.modulePath
3182
+ );
3183
+ if (mastraTarget) {
3184
+ return patchMastraSource(input.source, mastraTarget, input.format);
3185
+ }
3186
+ return null;
3187
+ }
3188
+
3010
3189
  // src/auto-instrumentations/bundler/plugin.ts
3011
3190
  function getModuleVersion(basedir) {
3012
3191
  try {
@@ -3045,6 +3224,18 @@ var unplugin = (0, import_unplugin.createUnplugin)(
3045
3224
  return null;
3046
3225
  }
3047
3226
  const moduleName = moduleDetails.name;
3227
+ const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
3228
+ if (options.browser !== true) {
3229
+ const patched = applySpecialCasePatch({
3230
+ packageName: moduleName,
3231
+ modulePath: normalizedModulePath,
3232
+ source: code,
3233
+ format: isModule ? "esm" : "cjs"
3234
+ });
3235
+ if (patched !== null) {
3236
+ return { code: patched, map: null };
3237
+ }
3238
+ }
3048
3239
  const moduleVersion = getModuleVersion(moduleDetails.basedir);
3049
3240
  if (!moduleVersion) {
3050
3241
  console.warn(
@@ -3052,7 +3243,6 @@ var unplugin = (0, import_unplugin.createUnplugin)(
3052
3243
  );
3053
3244
  return null;
3054
3245
  }
3055
- const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
3056
3246
  const transformer = instrumentationMatcher.getTransformer(
3057
3247
  moduleName,
3058
3248
  moduleVersion,
@@ -1,9 +1,10 @@
1
1
  import {
2
2
  braintrustWebpackPlugin,
3
3
  webpackPlugin
4
- } from "../chunk-WFEUJACP.mjs";
5
- import "../chunk-GJOO4ESL.mjs";
4
+ } from "../chunk-CNQ7BUKN.mjs";
5
+ import "../chunk-VXJONZVX.mjs";
6
6
  import "../chunk-E5DUYJWK.mjs";
7
+ import "../chunk-J57YF7WS.mjs";
7
8
  export {
8
9
  braintrustWebpackPlugin,
9
10
  webpackPlugin
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unplugin
3
- } from "./chunk-GJOO4ESL.mjs";
3
+ } from "./chunk-VXJONZVX.mjs";
4
4
 
5
5
  // src/auto-instrumentations/bundler/webpack.ts
6
6
  function braintrustWebpackPlugin(options = {}) {
@@ -0,0 +1,208 @@
1
+ // src/auto-instrumentations/loader/mastra-observability-patch.ts
2
+ var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
3
+ var MASTRA_CORE_PACKAGE = "@mastra/core";
4
+ var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
5
+ var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
6
+ "dist/index.js",
7
+ "dist/index.cjs",
8
+ "dist/mastra/index.js",
9
+ "dist/mastra/index.cjs"
10
+ ]);
11
+ var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
12
+ "dist/index.js",
13
+ "dist/index.cjs"
14
+ ]);
15
+ function classifyMastraTarget(packageName, modulePath) {
16
+ if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
17
+ return "core";
18
+ }
19
+ if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
20
+ return "observability";
21
+ }
22
+ return null;
23
+ }
24
+ function extractChunkPath(source) {
25
+ const esmMatch = source.match(
26
+ /export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
27
+ );
28
+ if (esmMatch) return esmMatch[1];
29
+ const cjsMatch = source.match(
30
+ /require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
31
+ );
32
+ if (cjsMatch) return cjsMatch[1];
33
+ return null;
34
+ }
35
+ var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
36
+ var MASTRA_PROXY_HANDLER_BODY = `
37
+ {
38
+ construct(target, args, newTarget) {
39
+ var firstArg = args[0];
40
+ if (
41
+ (!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
42
+ __braintrustObservabilityClass
43
+ ) {
44
+ try {
45
+ // serviceName is required by Mastra's Observability validator; pass
46
+ // something sensible by default. Users who want a different name
47
+ // should construct Observability themselves.
48
+ var observability = new __braintrustObservabilityClass({
49
+ configs: { default: { serviceName: "mastra" } },
50
+ });
51
+ args = args.slice();
52
+ args[0] = Object.assign({}, firstArg, { observability: observability });
53
+ } catch (e) {
54
+ // Fall through. Mastra will use its own NoOp; user code still works,
55
+ // just without auto-instrumentation.
56
+ }
57
+ }
58
+ return Reflect.construct(target, args, newTarget);
59
+ },
60
+ }`;
61
+ function buildMastraEsmWrapper(chunkPath) {
62
+ const chunk = JSON.stringify(chunkPath);
63
+ return `import { Mastra as __braintrustOrigMastra } from ${chunk};
64
+ import { createRequire as __braintrustCreateRequire } from "node:module";
65
+
66
+ let __braintrustObservabilityClass = null;
67
+ try {
68
+ // Resolve @mastra/observability relative to this module (the Mastra entry),
69
+ // so it's looked up from the user's node_modules tree.
70
+ const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
71
+ __braintrustObservabilityClass =
72
+ __braintrustRequire("@mastra/observability").Observability;
73
+ } catch (e) {
74
+ // @mastra/observability isn't installed; the construct trap will skip the
75
+ // auto-construct branch and Mastra falls back to its own NoOp.
76
+ }
77
+ const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
78
+ export { Mastra };
79
+ `;
80
+ }
81
+ function buildMastraCjsWrapper(chunkPath) {
82
+ const chunk = JSON.stringify(chunkPath);
83
+ return `'use strict';
84
+ const __braintrustChunk = require(${chunk});
85
+
86
+ let __braintrustObservabilityClass = null;
87
+ try {
88
+ __braintrustObservabilityClass = require("@mastra/observability").Observability;
89
+ } catch (e) {
90
+ // @mastra/observability isn't installed; same fallback as the ESM wrapper.
91
+ }
92
+
93
+ const __braintrustWrappedMastra = new Proxy(
94
+ __braintrustChunk.Mastra,
95
+ ${MASTRA_PROXY_HANDLER_BODY},
96
+ );
97
+ Object.defineProperty(exports, "Mastra", {
98
+ enumerable: true,
99
+ configurable: true,
100
+ get: function () { return __braintrustWrappedMastra; }
101
+ });
102
+ `;
103
+ }
104
+ var OBSERVABILITY_APPEND_BODY = `
105
+ ;(function __braintrustWrapObservability() {
106
+ // Top-level so we can both read and reassign the var binding the original
107
+ // entry declared.
108
+ if (typeof Observability === "undefined") return;
109
+ if (Observability.__braintrustWrapped) return;
110
+ function __braintrustEnsureExporter(rawConfig) {
111
+ try {
112
+ var factory = globalThis[${EXPORTER_FACTORY_KEY}];
113
+ if (typeof factory !== "function") return rawConfig;
114
+ var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
115
+ var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
116
+ var configsOut = {};
117
+ var hadEntries = false;
118
+ if (configsIn) {
119
+ for (var name in configsIn) {
120
+ if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
121
+ hadEntries = true;
122
+ var inst = configsIn[name] || {};
123
+ var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
124
+ var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
125
+ configsOut[name] = Object.assign({}, inst, {
126
+ exporters: hasOurs ? existing : existing.concat([factory()]),
127
+ });
128
+ }
129
+ }
130
+ if (!hadEntries) {
131
+ configsOut.default = {
132
+ serviceName: "mastra",
133
+ exporters: [factory()],
134
+ };
135
+ }
136
+ return Object.assign({}, config, { configs: configsOut });
137
+ } catch (e) {
138
+ return rawConfig;
139
+ }
140
+ }
141
+ var __OriginalObservability = Observability;
142
+ Observability = new Proxy(__OriginalObservability, {
143
+ construct: function (target, args, newTarget) {
144
+ var nextArgs = args.slice();
145
+ nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
146
+ return Reflect.construct(target, nextArgs, newTarget);
147
+ },
148
+ });
149
+ Observability.__braintrustWrapped = true;
150
+ if (typeof exports !== "undefined" && exports && typeof exports === "object") {
151
+ try {
152
+ Object.defineProperty(exports, "Observability", {
153
+ enumerable: true,
154
+ configurable: true,
155
+ get: function () { return Observability; },
156
+ });
157
+ } catch (e) {}
158
+ }
159
+ })();
160
+ `;
161
+ function patchMastraSource(source, target, format) {
162
+ if (target === "core") {
163
+ const chunkPath = extractChunkPath(source);
164
+ if (!chunkPath) return source;
165
+ return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
166
+ }
167
+ return source + OBSERVABILITY_APPEND_BODY;
168
+ }
169
+
170
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
171
+ var OPENAI_API_PROMISE_PATCH = `
172
+ ;(function __btPatchAPIPromise() {
173
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
174
+ APIPromise.prototype.__btParsePatched = true;
175
+ var _origThen = APIPromise.prototype.then;
176
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
177
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
178
+ this.__btParseWrapped = true;
179
+ var _origParse = this.parseResponse;
180
+ var _cached;
181
+ this.parseResponse = function() {
182
+ if (!_cached) _cached = _origParse.apply(this, arguments);
183
+ return _cached;
184
+ };
185
+ }
186
+ return _origThen.call(this, onfulfilled, onrejected);
187
+ };
188
+ })();
189
+ `;
190
+
191
+ // src/auto-instrumentations/loader/special-case-patches.ts
192
+ function applySpecialCasePatch(input) {
193
+ if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
194
+ return input.source + OPENAI_API_PROMISE_PATCH;
195
+ }
196
+ const mastraTarget = classifyMastraTarget(
197
+ input.packageName,
198
+ input.modulePath
199
+ );
200
+ if (mastraTarget) {
201
+ return patchMastraSource(input.source, mastraTarget, input.format);
202
+ }
203
+ return null;
204
+ }
205
+
206
+ export {
207
+ applySpecialCasePatch
208
+ };