braintrust 3.13.0 → 3.14.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 +809 -466
  4. package/dev/dist/index.mjs +367 -24
  5. package/dist/apply-auto-instrumentation.js +204 -174
  6. package/dist/apply-auto-instrumentation.mjs +35 -5
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +225 -1
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -1
  9. package/dist/auto-instrumentations/bundler/next.cjs +225 -1
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -2
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +225 -1
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -1
  13. package/dist/auto-instrumentations/bundler/vite.cjs +225 -1
  14. package/dist/auto-instrumentations/bundler/vite.mjs +2 -1
  15. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +8 -0
  16. package/dist/auto-instrumentations/bundler/webpack.cjs +225 -1
  17. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -2
  18. package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +208 -0
  19. package/dist/auto-instrumentations/{chunk-WFEUJACP.mjs → chunk-OTUQ7KH5.mjs} +1 -1
  20. package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +280 -0
  21. package/dist/auto-instrumentations/{chunk-GJOO4ESL.mjs → chunk-XKAAVWT6.mjs} +23 -1
  22. package/dist/auto-instrumentations/hook.mjs +7980 -7
  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 +127 -11
  27. package/dist/browser.d.ts +127 -11
  28. package/dist/browser.js +293 -24
  29. package/dist/browser.mjs +293 -24
  30. package/dist/{chunk-26JGOELH.js → chunk-NKD77KGB.js} +179 -1
  31. package/dist/{chunk-75IQCUB2.mjs → chunk-NU2GSPHX.mjs} +179 -1
  32. package/dist/cli.js +374 -51
  33. package/dist/edge-light.d.mts +1 -1
  34. package/dist/edge-light.d.ts +1 -1
  35. package/dist/edge-light.js +293 -24
  36. package/dist/edge-light.mjs +293 -24
  37. package/dist/index.d.mts +127 -11
  38. package/dist/index.d.ts +127 -11
  39. package/dist/index.js +1113 -838
  40. package/dist/index.mjs +305 -30
  41. package/dist/instrumentation/index.d.mts +7 -8
  42. package/dist/instrumentation/index.d.ts +7 -8
  43. package/dist/instrumentation/index.js +101 -24
  44. package/dist/instrumentation/index.mjs +101 -24
  45. package/dist/workerd.d.mts +1 -1
  46. package/dist/workerd.d.ts +1 -1
  47. package/dist/workerd.js +293 -24
  48. package/dist/workerd.mjs +293 -24
  49. package/package.json +1 -7
  50. package/dist/auto-instrumentations/chunk-MWZXZQUO.mjs +0 -81
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  aiSDKChannels,
3
3
  anthropicChannels,
4
+ classifyMastraTarget,
4
5
  claudeAgentSDKChannels,
5
6
  cohereChannels,
6
7
  cursorSDKChannels,
@@ -20,9 +21,10 @@ import {
20
21
  openAICodexChannels,
21
22
  openRouterAgentChannels,
22
23
  openRouterChannels,
24
+ patchMastraSource,
23
25
  patchTracingChannel,
24
26
  readDisabledInstrumentationEnvConfig
25
- } from "./chunk-75IQCUB2.mjs";
27
+ } from "./chunk-NU2GSPHX.mjs";
26
28
 
27
29
  // src/node/apply-auto-instrumentation-entry.ts
28
30
  import * as diagnostics_channel from "node:diagnostics_channel";
@@ -2306,6 +2308,13 @@ var defaultInstrumentationConfigGroups = [
2306
2308
  integrations: ["gitHubCopilot"],
2307
2309
  configs: gitHubCopilotConfigs
2308
2310
  }
2311
+ // Note: `@mastra/core` is not listed here because its instrumentation
2312
+ // doesn't go through the AST `code-transformer` matcher — Mastra's
2313
+ // content-hashed chunks make `filePath`-based matching too brittle.
2314
+ // Instead it's handled by the source-replacement entry in
2315
+ // `loader/special-case-patches.ts`, which both the runtime loader
2316
+ // (`hook.mjs` → `cjs-patch.ts`/`esm-hook.mts`) and the bundler plugin
2317
+ // (`bundler/plugin.ts`) call. The `mastra` env-var disable still works.
2309
2318
  ];
2310
2319
  function getDefaultInstrumentationConfigs({
2311
2320
  additionalInstrumentations,
@@ -2414,6 +2423,21 @@ var OPENAI_API_PROMISE_PATCH = `
2414
2423
  })();
2415
2424
  `;
2416
2425
 
2426
+ // src/auto-instrumentations/loader/special-case-patches.ts
2427
+ function applySpecialCasePatch(input) {
2428
+ if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
2429
+ return input.source + OPENAI_API_PROMISE_PATCH;
2430
+ }
2431
+ const mastraTarget = classifyMastraTarget(
2432
+ input.packageName,
2433
+ input.modulePath
2434
+ );
2435
+ if (mastraTarget) {
2436
+ return patchMastraSource(input.source, mastraTarget, input.format);
2437
+ }
2438
+ return null;
2439
+ }
2440
+
2417
2441
  // src/auto-instrumentations/loader/cjs-patch.ts
2418
2442
  var ModulePatch = class {
2419
2443
  packages;
@@ -2442,12 +2466,18 @@ var ModulePatch = class {
2442
2466
  const resolvedModule = moduleDetailsFromPath(normalizedForPlatform);
2443
2467
  if (resolvedModule) {
2444
2468
  const packageName = getPackageName(resolvedModule.basedir) ?? resolvedModule.name;
2445
- if (!self.packages.has(packageName)) {
2469
+ const normalizedModulePath = resolvedModule.path.replace(/\\/g, "/");
2470
+ const patched = applySpecialCasePatch({
2471
+ packageName,
2472
+ modulePath: normalizedModulePath,
2473
+ source: String(content),
2474
+ format: "cjs"
2475
+ });
2476
+ if (patched !== null) {
2477
+ args[0] = patched;
2446
2478
  return self.originalCompile.apply(this, args);
2447
2479
  }
2448
- const normalizedModulePath = resolvedModule.path.replace(/\\/g, "/");
2449
- if (packageName === "openai" && normalizedModulePath.includes("api-promise")) {
2450
- args[0] = content + OPENAI_API_PROMISE_PATCH;
2480
+ if (!self.packages.has(packageName)) {
2451
2481
  return self.originalCompile.apply(this, args);
2452
2482
  }
2453
2483
  const version = getPackageVersion(resolvedModule.basedir);
@@ -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",
@@ -2990,6 +2991,13 @@ var defaultInstrumentationConfigGroups = [
2990
2991
  integrations: ["gitHubCopilot"],
2991
2992
  configs: gitHubCopilotConfigs
2992
2993
  }
2994
+ // Note: `@mastra/core` is not listed here because its instrumentation
2995
+ // doesn't go through the AST `code-transformer` matcher — Mastra's
2996
+ // content-hashed chunks make `filePath`-based matching too brittle.
2997
+ // Instead it's handled by the source-replacement entry in
2998
+ // `loader/special-case-patches.ts`, which both the runtime loader
2999
+ // (`hook.mjs` → `cjs-patch.ts`/`esm-hook.mts`) and the bundler plugin
3000
+ // (`bundler/plugin.ts`) call. The `mastra` env-var disable still works.
2993
3001
  ];
2994
3002
  function getDefaultInstrumentationConfigs({
2995
3003
  additionalInstrumentations,
@@ -3007,6 +3015,211 @@ function getDefaultInstrumentationConfigs({
3007
3015
  ];
3008
3016
  }
3009
3017
 
3018
+ // src/auto-instrumentations/loader/mastra-observability-patch.ts
3019
+ var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
3020
+ var MASTRA_CORE_PACKAGE = "@mastra/core";
3021
+ var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
3022
+ var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
3023
+ "dist/index.js",
3024
+ "dist/index.cjs",
3025
+ "dist/mastra/index.js",
3026
+ "dist/mastra/index.cjs"
3027
+ ]);
3028
+ var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
3029
+ "dist/index.js",
3030
+ "dist/index.cjs"
3031
+ ]);
3032
+ function classifyMastraTarget(packageName, modulePath) {
3033
+ if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
3034
+ return "core";
3035
+ }
3036
+ if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
3037
+ return "observability";
3038
+ }
3039
+ return null;
3040
+ }
3041
+ function extractChunkPath(source) {
3042
+ const esmMatch = source.match(
3043
+ /export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
3044
+ );
3045
+ if (esmMatch) return esmMatch[1];
3046
+ const cjsMatch = source.match(
3047
+ /require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
3048
+ );
3049
+ if (cjsMatch) return cjsMatch[1];
3050
+ return null;
3051
+ }
3052
+ var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
3053
+ var MASTRA_PROXY_HANDLER_BODY = `
3054
+ {
3055
+ construct(target, args, newTarget) {
3056
+ var firstArg = args[0];
3057
+ if (
3058
+ (!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
3059
+ __braintrustObservabilityClass
3060
+ ) {
3061
+ try {
3062
+ // serviceName is required by Mastra's Observability validator; pass
3063
+ // something sensible by default. Users who want a different name
3064
+ // should construct Observability themselves.
3065
+ var observability = new __braintrustObservabilityClass({
3066
+ configs: { default: { serviceName: "mastra" } },
3067
+ });
3068
+ args = args.slice();
3069
+ args[0] = Object.assign({}, firstArg, { observability: observability });
3070
+ } catch (e) {
3071
+ // Fall through. Mastra will use its own NoOp; user code still works,
3072
+ // just without auto-instrumentation.
3073
+ }
3074
+ }
3075
+ return Reflect.construct(target, args, newTarget);
3076
+ },
3077
+ }`;
3078
+ function buildMastraEsmWrapper(chunkPath) {
3079
+ const chunk = JSON.stringify(chunkPath);
3080
+ return `import { Mastra as __braintrustOrigMastra } from ${chunk};
3081
+ import { createRequire as __braintrustCreateRequire } from "node:module";
3082
+
3083
+ let __braintrustObservabilityClass = null;
3084
+ try {
3085
+ // Resolve @mastra/observability relative to this module (the Mastra entry),
3086
+ // so it's looked up from the user's node_modules tree.
3087
+ const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
3088
+ __braintrustObservabilityClass =
3089
+ __braintrustRequire("@mastra/observability").Observability;
3090
+ } catch (e) {
3091
+ // @mastra/observability isn't installed; the construct trap will skip the
3092
+ // auto-construct branch and Mastra falls back to its own NoOp.
3093
+ }
3094
+ const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
3095
+ export { Mastra };
3096
+ `;
3097
+ }
3098
+ function buildMastraCjsWrapper(chunkPath) {
3099
+ const chunk = JSON.stringify(chunkPath);
3100
+ return `'use strict';
3101
+ const __braintrustChunk = require(${chunk});
3102
+
3103
+ let __braintrustObservabilityClass = null;
3104
+ try {
3105
+ __braintrustObservabilityClass = require("@mastra/observability").Observability;
3106
+ } catch (e) {
3107
+ // @mastra/observability isn't installed; same fallback as the ESM wrapper.
3108
+ }
3109
+
3110
+ const __braintrustWrappedMastra = new Proxy(
3111
+ __braintrustChunk.Mastra,
3112
+ ${MASTRA_PROXY_HANDLER_BODY},
3113
+ );
3114
+ Object.defineProperty(exports, "Mastra", {
3115
+ enumerable: true,
3116
+ configurable: true,
3117
+ get: function () { return __braintrustWrappedMastra; }
3118
+ });
3119
+ `;
3120
+ }
3121
+ var OBSERVABILITY_APPEND_BODY = `
3122
+ ;(function __braintrustWrapObservability() {
3123
+ // Top-level so we can both read and reassign the var binding the original
3124
+ // entry declared.
3125
+ if (typeof Observability === "undefined") return;
3126
+ if (Observability.__braintrustWrapped) return;
3127
+ function __braintrustEnsureExporter(rawConfig) {
3128
+ try {
3129
+ var factory = globalThis[${EXPORTER_FACTORY_KEY}];
3130
+ if (typeof factory !== "function") return rawConfig;
3131
+ var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
3132
+ var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
3133
+ var configsOut = {};
3134
+ var hadEntries = false;
3135
+ if (configsIn) {
3136
+ for (var name in configsIn) {
3137
+ if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
3138
+ hadEntries = true;
3139
+ var inst = configsIn[name] || {};
3140
+ var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
3141
+ var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
3142
+ configsOut[name] = Object.assign({}, inst, {
3143
+ exporters: hasOurs ? existing : existing.concat([factory()]),
3144
+ });
3145
+ }
3146
+ }
3147
+ if (!hadEntries) {
3148
+ configsOut.default = {
3149
+ serviceName: "mastra",
3150
+ exporters: [factory()],
3151
+ };
3152
+ }
3153
+ return Object.assign({}, config, { configs: configsOut });
3154
+ } catch (e) {
3155
+ return rawConfig;
3156
+ }
3157
+ }
3158
+ var __OriginalObservability = Observability;
3159
+ Observability = new Proxy(__OriginalObservability, {
3160
+ construct: function (target, args, newTarget) {
3161
+ var nextArgs = args.slice();
3162
+ nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
3163
+ return Reflect.construct(target, nextArgs, newTarget);
3164
+ },
3165
+ });
3166
+ Observability.__braintrustWrapped = true;
3167
+ if (typeof exports !== "undefined" && exports && typeof exports === "object") {
3168
+ try {
3169
+ Object.defineProperty(exports, "Observability", {
3170
+ enumerable: true,
3171
+ configurable: true,
3172
+ get: function () { return Observability; },
3173
+ });
3174
+ } catch (e) {}
3175
+ }
3176
+ })();
3177
+ `;
3178
+ function patchMastraSource(source, target, format) {
3179
+ if (target === "core") {
3180
+ const chunkPath = extractChunkPath(source);
3181
+ if (!chunkPath) return source;
3182
+ return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
3183
+ }
3184
+ return source + OBSERVABILITY_APPEND_BODY;
3185
+ }
3186
+
3187
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
3188
+ var OPENAI_API_PROMISE_PATCH = `
3189
+ ;(function __btPatchAPIPromise() {
3190
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
3191
+ APIPromise.prototype.__btParsePatched = true;
3192
+ var _origThen = APIPromise.prototype.then;
3193
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
3194
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
3195
+ this.__btParseWrapped = true;
3196
+ var _origParse = this.parseResponse;
3197
+ var _cached;
3198
+ this.parseResponse = function() {
3199
+ if (!_cached) _cached = _origParse.apply(this, arguments);
3200
+ return _cached;
3201
+ };
3202
+ }
3203
+ return _origThen.call(this, onfulfilled, onrejected);
3204
+ };
3205
+ })();
3206
+ `;
3207
+
3208
+ // src/auto-instrumentations/loader/special-case-patches.ts
3209
+ function applySpecialCasePatch(input) {
3210
+ if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
3211
+ return input.source + OPENAI_API_PROMISE_PATCH;
3212
+ }
3213
+ const mastraTarget = classifyMastraTarget(
3214
+ input.packageName,
3215
+ input.modulePath
3216
+ );
3217
+ if (mastraTarget) {
3218
+ return patchMastraSource(input.source, mastraTarget, input.format);
3219
+ }
3220
+ return null;
3221
+ }
3222
+
3010
3223
  // src/auto-instrumentations/bundler/plugin.ts
3011
3224
  function getModuleVersion(basedir) {
3012
3225
  try {
@@ -3045,6 +3258,18 @@ var unplugin = (0, import_unplugin.createUnplugin)(
3045
3258
  return null;
3046
3259
  }
3047
3260
  const moduleName = moduleDetails.name;
3261
+ const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
3262
+ if (options.browser !== true) {
3263
+ const patched = applySpecialCasePatch({
3264
+ packageName: moduleName,
3265
+ modulePath: normalizedModulePath,
3266
+ source: code,
3267
+ format: isModule ? "esm" : "cjs"
3268
+ });
3269
+ if (patched !== null) {
3270
+ return { code: patched, map: null };
3271
+ }
3272
+ }
3048
3273
  const moduleVersion = getModuleVersion(moduleDetails.basedir);
3049
3274
  if (!moduleVersion) {
3050
3275
  console.warn(
@@ -3052,7 +3277,6 @@ var unplugin = (0, import_unplugin.createUnplugin)(
3052
3277
  );
3053
3278
  return null;
3054
3279
  }
3055
- const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
3056
3280
  const transformer = instrumentationMatcher.getTransformer(
3057
3281
  moduleName,
3058
3282
  moduleVersion,
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  unplugin
3
- } from "../chunk-GJOO4ESL.mjs";
3
+ } from "../chunk-XKAAVWT6.mjs";
4
4
  import "../chunk-E5DUYJWK.mjs";
5
+ import "../chunk-J57YF7WS.mjs";
5
6
 
6
7
  // src/auto-instrumentations/bundler/esbuild.ts
7
8
  function braintrustEsbuildPlugin(options = {}) {
@@ -64,6 +64,7 @@ var envIntegrationAliases = {
64
64
  cursorsdk: "cursorSDK",
65
65
  flue: "flue",
66
66
  "flue-runtime": "flue",
67
+ mastra: "mastra",
67
68
  "openai-agents": "openAIAgents",
68
69
  openaiagents: "openAIAgents",
69
70
  "openai-agents-core": "openAIAgents",
@@ -2991,6 +2992,13 @@ var defaultInstrumentationConfigGroups = [
2991
2992
  integrations: ["gitHubCopilot"],
2992
2993
  configs: gitHubCopilotConfigs
2993
2994
  }
2995
+ // Note: `@mastra/core` is not listed here because its instrumentation
2996
+ // doesn't go through the AST `code-transformer` matcher — Mastra's
2997
+ // content-hashed chunks make `filePath`-based matching too brittle.
2998
+ // Instead it's handled by the source-replacement entry in
2999
+ // `loader/special-case-patches.ts`, which both the runtime loader
3000
+ // (`hook.mjs` → `cjs-patch.ts`/`esm-hook.mts`) and the bundler plugin
3001
+ // (`bundler/plugin.ts`) call. The `mastra` env-var disable still works.
2994
3002
  ];
2995
3003
  function getDefaultInstrumentationConfigs({
2996
3004
  additionalInstrumentations,
@@ -3008,6 +3016,211 @@ function getDefaultInstrumentationConfigs({
3008
3016
  ];
3009
3017
  }
3010
3018
 
3019
+ // src/auto-instrumentations/loader/mastra-observability-patch.ts
3020
+ var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
3021
+ var MASTRA_CORE_PACKAGE = "@mastra/core";
3022
+ var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
3023
+ var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
3024
+ "dist/index.js",
3025
+ "dist/index.cjs",
3026
+ "dist/mastra/index.js",
3027
+ "dist/mastra/index.cjs"
3028
+ ]);
3029
+ var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
3030
+ "dist/index.js",
3031
+ "dist/index.cjs"
3032
+ ]);
3033
+ function classifyMastraTarget(packageName, modulePath) {
3034
+ if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
3035
+ return "core";
3036
+ }
3037
+ if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
3038
+ return "observability";
3039
+ }
3040
+ return null;
3041
+ }
3042
+ function extractChunkPath(source) {
3043
+ const esmMatch = source.match(
3044
+ /export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
3045
+ );
3046
+ if (esmMatch) return esmMatch[1];
3047
+ const cjsMatch = source.match(
3048
+ /require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
3049
+ );
3050
+ if (cjsMatch) return cjsMatch[1];
3051
+ return null;
3052
+ }
3053
+ var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
3054
+ var MASTRA_PROXY_HANDLER_BODY = `
3055
+ {
3056
+ construct(target, args, newTarget) {
3057
+ var firstArg = args[0];
3058
+ if (
3059
+ (!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
3060
+ __braintrustObservabilityClass
3061
+ ) {
3062
+ try {
3063
+ // serviceName is required by Mastra's Observability validator; pass
3064
+ // something sensible by default. Users who want a different name
3065
+ // should construct Observability themselves.
3066
+ var observability = new __braintrustObservabilityClass({
3067
+ configs: { default: { serviceName: "mastra" } },
3068
+ });
3069
+ args = args.slice();
3070
+ args[0] = Object.assign({}, firstArg, { observability: observability });
3071
+ } catch (e) {
3072
+ // Fall through. Mastra will use its own NoOp; user code still works,
3073
+ // just without auto-instrumentation.
3074
+ }
3075
+ }
3076
+ return Reflect.construct(target, args, newTarget);
3077
+ },
3078
+ }`;
3079
+ function buildMastraEsmWrapper(chunkPath) {
3080
+ const chunk = JSON.stringify(chunkPath);
3081
+ return `import { Mastra as __braintrustOrigMastra } from ${chunk};
3082
+ import { createRequire as __braintrustCreateRequire } from "node:module";
3083
+
3084
+ let __braintrustObservabilityClass = null;
3085
+ try {
3086
+ // Resolve @mastra/observability relative to this module (the Mastra entry),
3087
+ // so it's looked up from the user's node_modules tree.
3088
+ const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
3089
+ __braintrustObservabilityClass =
3090
+ __braintrustRequire("@mastra/observability").Observability;
3091
+ } catch (e) {
3092
+ // @mastra/observability isn't installed; the construct trap will skip the
3093
+ // auto-construct branch and Mastra falls back to its own NoOp.
3094
+ }
3095
+ const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
3096
+ export { Mastra };
3097
+ `;
3098
+ }
3099
+ function buildMastraCjsWrapper(chunkPath) {
3100
+ const chunk = JSON.stringify(chunkPath);
3101
+ return `'use strict';
3102
+ const __braintrustChunk = require(${chunk});
3103
+
3104
+ let __braintrustObservabilityClass = null;
3105
+ try {
3106
+ __braintrustObservabilityClass = require("@mastra/observability").Observability;
3107
+ } catch (e) {
3108
+ // @mastra/observability isn't installed; same fallback as the ESM wrapper.
3109
+ }
3110
+
3111
+ const __braintrustWrappedMastra = new Proxy(
3112
+ __braintrustChunk.Mastra,
3113
+ ${MASTRA_PROXY_HANDLER_BODY},
3114
+ );
3115
+ Object.defineProperty(exports, "Mastra", {
3116
+ enumerable: true,
3117
+ configurable: true,
3118
+ get: function () { return __braintrustWrappedMastra; }
3119
+ });
3120
+ `;
3121
+ }
3122
+ var OBSERVABILITY_APPEND_BODY = `
3123
+ ;(function __braintrustWrapObservability() {
3124
+ // Top-level so we can both read and reassign the var binding the original
3125
+ // entry declared.
3126
+ if (typeof Observability === "undefined") return;
3127
+ if (Observability.__braintrustWrapped) return;
3128
+ function __braintrustEnsureExporter(rawConfig) {
3129
+ try {
3130
+ var factory = globalThis[${EXPORTER_FACTORY_KEY}];
3131
+ if (typeof factory !== "function") return rawConfig;
3132
+ var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
3133
+ var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
3134
+ var configsOut = {};
3135
+ var hadEntries = false;
3136
+ if (configsIn) {
3137
+ for (var name in configsIn) {
3138
+ if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
3139
+ hadEntries = true;
3140
+ var inst = configsIn[name] || {};
3141
+ var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
3142
+ var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
3143
+ configsOut[name] = Object.assign({}, inst, {
3144
+ exporters: hasOurs ? existing : existing.concat([factory()]),
3145
+ });
3146
+ }
3147
+ }
3148
+ if (!hadEntries) {
3149
+ configsOut.default = {
3150
+ serviceName: "mastra",
3151
+ exporters: [factory()],
3152
+ };
3153
+ }
3154
+ return Object.assign({}, config, { configs: configsOut });
3155
+ } catch (e) {
3156
+ return rawConfig;
3157
+ }
3158
+ }
3159
+ var __OriginalObservability = Observability;
3160
+ Observability = new Proxy(__OriginalObservability, {
3161
+ construct: function (target, args, newTarget) {
3162
+ var nextArgs = args.slice();
3163
+ nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
3164
+ return Reflect.construct(target, nextArgs, newTarget);
3165
+ },
3166
+ });
3167
+ Observability.__braintrustWrapped = true;
3168
+ if (typeof exports !== "undefined" && exports && typeof exports === "object") {
3169
+ try {
3170
+ Object.defineProperty(exports, "Observability", {
3171
+ enumerable: true,
3172
+ configurable: true,
3173
+ get: function () { return Observability; },
3174
+ });
3175
+ } catch (e) {}
3176
+ }
3177
+ })();
3178
+ `;
3179
+ function patchMastraSource(source, target, format) {
3180
+ if (target === "core") {
3181
+ const chunkPath = extractChunkPath(source);
3182
+ if (!chunkPath) return source;
3183
+ return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
3184
+ }
3185
+ return source + OBSERVABILITY_APPEND_BODY;
3186
+ }
3187
+
3188
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
3189
+ var OPENAI_API_PROMISE_PATCH = `
3190
+ ;(function __btPatchAPIPromise() {
3191
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
3192
+ APIPromise.prototype.__btParsePatched = true;
3193
+ var _origThen = APIPromise.prototype.then;
3194
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
3195
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
3196
+ this.__btParseWrapped = true;
3197
+ var _origParse = this.parseResponse;
3198
+ var _cached;
3199
+ this.parseResponse = function() {
3200
+ if (!_cached) _cached = _origParse.apply(this, arguments);
3201
+ return _cached;
3202
+ };
3203
+ }
3204
+ return _origThen.call(this, onfulfilled, onrejected);
3205
+ };
3206
+ })();
3207
+ `;
3208
+
3209
+ // src/auto-instrumentations/loader/special-case-patches.ts
3210
+ function applySpecialCasePatch(input) {
3211
+ if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
3212
+ return input.source + OPENAI_API_PROMISE_PATCH;
3213
+ }
3214
+ const mastraTarget = classifyMastraTarget(
3215
+ input.packageName,
3216
+ input.modulePath
3217
+ );
3218
+ if (mastraTarget) {
3219
+ return patchMastraSource(input.source, mastraTarget, input.format);
3220
+ }
3221
+ return null;
3222
+ }
3223
+
3011
3224
  // src/auto-instrumentations/bundler/plugin.ts
3012
3225
  function getModuleVersion(basedir) {
3013
3226
  try {
@@ -3046,6 +3259,18 @@ var unplugin = (0, import_unplugin.createUnplugin)(
3046
3259
  return null;
3047
3260
  }
3048
3261
  const moduleName = moduleDetails.name;
3262
+ const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
3263
+ if (options.browser !== true) {
3264
+ const patched = applySpecialCasePatch({
3265
+ packageName: moduleName,
3266
+ modulePath: normalizedModulePath,
3267
+ source: code,
3268
+ format: isModule ? "esm" : "cjs"
3269
+ });
3270
+ if (patched !== null) {
3271
+ return { code: patched, map: null };
3272
+ }
3273
+ }
3049
3274
  const moduleVersion = getModuleVersion(moduleDetails.basedir);
3050
3275
  if (!moduleVersion) {
3051
3276
  console.warn(
@@ -3053,7 +3278,6 @@ var unplugin = (0, import_unplugin.createUnplugin)(
3053
3278
  );
3054
3279
  return null;
3055
3280
  }
3056
- const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
3057
3281
  const transformer = instrumentationMatcher.getTransformer(
3058
3282
  moduleName,
3059
3283
  moduleVersion,
@@ -1,8 +1,9 @@
1
1
  import {
2
2
  webpackPlugin
3
- } from "../chunk-WFEUJACP.mjs";
4
- import "../chunk-GJOO4ESL.mjs";
3
+ } from "../chunk-OTUQ7KH5.mjs";
4
+ import "../chunk-XKAAVWT6.mjs";
5
5
  import "../chunk-E5DUYJWK.mjs";
6
+ import "../chunk-J57YF7WS.mjs";
6
7
 
7
8
  // src/auto-instrumentations/bundler/next.ts
8
9
  import { createRequire } from "module";