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
@@ -0,0 +1,280 @@
1
+ // src/auto-instrumentations/loader/mastra-observability-patch.ts
2
+ var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
3
+ function installMastraExporterFactory(factory) {
4
+ const globals = globalThis;
5
+ globals[MASTRA_EXPORTER_FACTORY_GLOBAL] ??= factory;
6
+ }
7
+ var MASTRA_CORE_PACKAGE = "@mastra/core";
8
+ var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
9
+ var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
10
+ "dist/index.js",
11
+ "dist/index.cjs",
12
+ "dist/mastra/index.js",
13
+ "dist/mastra/index.cjs"
14
+ ]);
15
+ var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
16
+ "dist/index.js",
17
+ "dist/index.cjs"
18
+ ]);
19
+ function classifyMastraTarget(packageName, modulePath) {
20
+ if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
21
+ return "core";
22
+ }
23
+ if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
24
+ return "observability";
25
+ }
26
+ return null;
27
+ }
28
+ function extractChunkPath(source) {
29
+ const esmMatch = source.match(
30
+ /export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
31
+ );
32
+ if (esmMatch) return esmMatch[1];
33
+ const cjsMatch = source.match(
34
+ /require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
35
+ );
36
+ if (cjsMatch) return cjsMatch[1];
37
+ return null;
38
+ }
39
+ var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
40
+ var MASTRA_PROXY_HANDLER_BODY = `
41
+ {
42
+ construct(target, args, newTarget) {
43
+ var firstArg = args[0];
44
+ if (
45
+ (!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
46
+ __braintrustObservabilityClass
47
+ ) {
48
+ try {
49
+ // serviceName is required by Mastra's Observability validator; pass
50
+ // something sensible by default. Users who want a different name
51
+ // should construct Observability themselves.
52
+ var observability = new __braintrustObservabilityClass({
53
+ configs: { default: { serviceName: "mastra" } },
54
+ });
55
+ args = args.slice();
56
+ args[0] = Object.assign({}, firstArg, { observability: observability });
57
+ } catch (e) {
58
+ // Fall through. Mastra will use its own NoOp; user code still works,
59
+ // just without auto-instrumentation.
60
+ }
61
+ }
62
+ return Reflect.construct(target, args, newTarget);
63
+ },
64
+ }`;
65
+ function buildMastraEsmWrapper(chunkPath) {
66
+ const chunk = JSON.stringify(chunkPath);
67
+ return `import { Mastra as __braintrustOrigMastra } from ${chunk};
68
+ import { createRequire as __braintrustCreateRequire } from "node:module";
69
+
70
+ let __braintrustObservabilityClass = null;
71
+ try {
72
+ // Resolve @mastra/observability relative to this module (the Mastra entry),
73
+ // so it's looked up from the user's node_modules tree.
74
+ const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
75
+ __braintrustObservabilityClass =
76
+ __braintrustRequire("@mastra/observability").Observability;
77
+ } catch (e) {
78
+ // @mastra/observability isn't installed; the construct trap will skip the
79
+ // auto-construct branch and Mastra falls back to its own NoOp.
80
+ }
81
+ const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
82
+ export { Mastra };
83
+ `;
84
+ }
85
+ function buildMastraCjsWrapper(chunkPath) {
86
+ const chunk = JSON.stringify(chunkPath);
87
+ return `'use strict';
88
+ const __braintrustChunk = require(${chunk});
89
+
90
+ let __braintrustObservabilityClass = null;
91
+ try {
92
+ __braintrustObservabilityClass = require("@mastra/observability").Observability;
93
+ } catch (e) {
94
+ // @mastra/observability isn't installed; same fallback as the ESM wrapper.
95
+ }
96
+
97
+ const __braintrustWrappedMastra = new Proxy(
98
+ __braintrustChunk.Mastra,
99
+ ${MASTRA_PROXY_HANDLER_BODY},
100
+ );
101
+ Object.defineProperty(exports, "Mastra", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ get: function () { return __braintrustWrappedMastra; }
105
+ });
106
+ `;
107
+ }
108
+ var OBSERVABILITY_APPEND_BODY = `
109
+ ;(function __braintrustWrapObservability() {
110
+ // Top-level so we can both read and reassign the var binding the original
111
+ // entry declared.
112
+ if (typeof Observability === "undefined") return;
113
+ if (Observability.__braintrustWrapped) return;
114
+ function __braintrustEnsureExporter(rawConfig) {
115
+ try {
116
+ var factory = globalThis[${EXPORTER_FACTORY_KEY}];
117
+ if (typeof factory !== "function") return rawConfig;
118
+ var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
119
+ var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
120
+ var configsOut = {};
121
+ var hadEntries = false;
122
+ if (configsIn) {
123
+ for (var name in configsIn) {
124
+ if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
125
+ hadEntries = true;
126
+ var inst = configsIn[name] || {};
127
+ var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
128
+ var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
129
+ configsOut[name] = Object.assign({}, inst, {
130
+ exporters: hasOurs ? existing : existing.concat([factory()]),
131
+ });
132
+ }
133
+ }
134
+ if (!hadEntries) {
135
+ configsOut.default = {
136
+ serviceName: "mastra",
137
+ exporters: [factory()],
138
+ };
139
+ }
140
+ return Object.assign({}, config, { configs: configsOut });
141
+ } catch (e) {
142
+ return rawConfig;
143
+ }
144
+ }
145
+ var __OriginalObservability = Observability;
146
+ Observability = new Proxy(__OriginalObservability, {
147
+ construct: function (target, args, newTarget) {
148
+ var nextArgs = args.slice();
149
+ nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
150
+ return Reflect.construct(target, nextArgs, newTarget);
151
+ },
152
+ });
153
+ Observability.__braintrustWrapped = true;
154
+ if (typeof exports !== "undefined" && exports && typeof exports === "object") {
155
+ try {
156
+ Object.defineProperty(exports, "Observability", {
157
+ enumerable: true,
158
+ configurable: true,
159
+ get: function () { return Observability; },
160
+ });
161
+ } catch (e) {}
162
+ }
163
+ })();
164
+ `;
165
+ function patchMastraSource(source, target, format) {
166
+ if (target === "core") {
167
+ const chunkPath = extractChunkPath(source);
168
+ if (!chunkPath) return source;
169
+ return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
170
+ }
171
+ return source + OBSERVABILITY_APPEND_BODY;
172
+ }
173
+
174
+ // src/auto-instrumentations/loader/get-package-version.ts
175
+ import { readFileSync, realpathSync } from "fs";
176
+ import { join } from "path";
177
+ var packageVersions = /* @__PURE__ */ new Map();
178
+ var packageNames = /* @__PURE__ */ new Map();
179
+ function readPackageJson(baseDir) {
180
+ try {
181
+ const packageJsonPath = join(baseDir, "package.json");
182
+ const jsonFile = readFileSync(packageJsonPath, "utf8");
183
+ return JSON.parse(jsonFile);
184
+ } catch {
185
+ return void 0;
186
+ }
187
+ }
188
+ function resolvePackageBaseDir(baseDir) {
189
+ try {
190
+ return realpathSync(baseDir);
191
+ } catch {
192
+ return baseDir;
193
+ }
194
+ }
195
+ function readPackageJsonWithFallback(baseDir) {
196
+ const packageJson = readPackageJson(baseDir);
197
+ if (packageJson) {
198
+ return packageJson;
199
+ }
200
+ const resolvedBaseDir = resolvePackageBaseDir(baseDir);
201
+ if (resolvedBaseDir === baseDir) {
202
+ return void 0;
203
+ }
204
+ return readPackageJson(resolvedBaseDir);
205
+ }
206
+ function getPackageVersion(baseDir) {
207
+ if (packageVersions.has(baseDir)) {
208
+ return packageVersions.get(baseDir);
209
+ }
210
+ const packageJson = readPackageJsonWithFallback(baseDir);
211
+ if (typeof packageJson?.version === "string") {
212
+ packageVersions.set(baseDir, packageJson.version);
213
+ return packageJson.version;
214
+ }
215
+ return process.version.slice(1);
216
+ }
217
+ function getPackageName(baseDir) {
218
+ if (packageNames.has(baseDir)) {
219
+ return packageNames.get(baseDir);
220
+ }
221
+ const packageJson = readPackageJsonWithFallback(baseDir);
222
+ if (typeof packageJson?.name === "string") {
223
+ packageNames.set(baseDir, packageJson.name);
224
+ return packageJson.name;
225
+ }
226
+ return void 0;
227
+ }
228
+
229
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
230
+ var OPENAI_API_PROMISE_PATCH = `
231
+ ;(function __btPatchAPIPromise() {
232
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
233
+ APIPromise.prototype.__btParsePatched = true;
234
+ var _origThen = APIPromise.prototype.then;
235
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
236
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
237
+ this.__btParseWrapped = true;
238
+ var _origParse = this.parseResponse;
239
+ var _cached;
240
+ this.parseResponse = function() {
241
+ if (!_cached) _cached = _origParse.apply(this, arguments);
242
+ return _cached;
243
+ };
244
+ }
245
+ return _origThen.call(this, onfulfilled, onrejected);
246
+ };
247
+ })();
248
+ `;
249
+
250
+ // src/auto-instrumentations/loader/special-case-patches.ts
251
+ function applySpecialCasePatch(input) {
252
+ if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
253
+ return input.source + OPENAI_API_PROMISE_PATCH;
254
+ }
255
+ const mastraTarget = classifyMastraTarget(
256
+ input.packageName,
257
+ input.modulePath
258
+ );
259
+ if (mastraTarget) {
260
+ return patchMastraSource(input.source, mastraTarget, input.format);
261
+ }
262
+ return null;
263
+ }
264
+ function isSpecialCaseTarget(packageName, modulePath) {
265
+ if (packageName === "openai" && modulePath.includes("api-promise")) {
266
+ return true;
267
+ }
268
+ if (classifyMastraTarget(packageName, modulePath) !== null) {
269
+ return true;
270
+ }
271
+ return false;
272
+ }
273
+
274
+ export {
275
+ installMastraExporterFactory,
276
+ getPackageVersion,
277
+ getPackageName,
278
+ applySpecialCasePatch,
279
+ isSpecialCaseTarget
280
+ };
@@ -20,6 +20,9 @@ import {
20
20
  openRouterConfigs,
21
21
  openaiConfigs
22
22
  } from "./chunk-E5DUYJWK.mjs";
23
+ import {
24
+ applySpecialCasePatch
25
+ } from "./chunk-J57YF7WS.mjs";
23
26
 
24
27
  // src/auto-instrumentations/bundler/plugin.ts
25
28
  import { createUnplugin } from "unplugin";
@@ -51,6 +54,7 @@ var envIntegrationAliases = {
51
54
  cursorsdk: "cursorSDK",
52
55
  flue: "flue",
53
56
  "flue-runtime": "flue",
57
+ mastra: "mastra",
54
58
  "openai-agents": "openAIAgents",
55
59
  openaiagents: "openAIAgents",
56
60
  "openai-agents-core": "openAIAgents",
@@ -99,35 +103,11 @@ var flueChannels = defineChannels("@flue/runtime", {
99
103
  createContext: channel({
100
104
  channelName: "createFlueContext",
101
105
  kind: "sync-stream"
102
- }),
103
- openSession: channel({
104
- channelName: "Harness.openSession",
105
- kind: "async"
106
- }),
107
- contextEvent: channel({
108
- channelName: "context.event",
109
- kind: "sync-stream"
110
- }),
111
- prompt: channel({
112
- channelName: "session.prompt",
113
- kind: "async"
114
- }),
115
- skill: channel({
116
- channelName: "session.skill",
117
- kind: "async"
118
- }),
119
- task: channel({
120
- channelName: "session.task",
121
- kind: "async"
122
- }),
123
- compact: channel({
124
- channelName: "session.compact",
125
- kind: "async"
126
106
  })
127
107
  });
128
108
 
129
109
  // src/auto-instrumentations/configs/flue.ts
130
- var flueVersionRange = ">=0.7.0 <0.8.0";
110
+ var flueVersionRange = ">=0.8.0";
131
111
  var flueConfigs = [
132
112
  {
133
113
  channelName: flueChannels.createContext.channelName,
@@ -140,19 +120,6 @@ var flueConfigs = [
140
120
  functionName: "createFlueContext",
141
121
  kind: "Sync"
142
122
  }
143
- },
144
- {
145
- channelName: flueChannels.openSession.channelName,
146
- module: {
147
- name: "@flue/runtime",
148
- versionRange: flueVersionRange,
149
- filePath: "dist/internal.mjs"
150
- },
151
- functionQuery: {
152
- className: "Harness",
153
- methodName: "openSession",
154
- kind: "Async"
155
- }
156
123
  }
157
124
  ];
158
125
 
@@ -173,7 +140,6 @@ var defaultInstrumentationConfigGroups = [
173
140
  configs: claudeAgentSDKConfigs
174
141
  },
175
142
  { integrations: ["cursor", "cursorSDK"], configs: cursorSDKConfigs },
176
- { integrations: ["flue"], configs: flueConfigs },
177
143
  {
178
144
  integrations: ["openAIAgents"],
179
145
  configs: openAIAgentsCoreConfigs
@@ -203,7 +169,18 @@ var defaultInstrumentationConfigGroups = [
203
169
  {
204
170
  integrations: ["gitHubCopilot"],
205
171
  configs: gitHubCopilotConfigs
172
+ },
173
+ {
174
+ integrations: ["flue"],
175
+ configs: flueConfigs
206
176
  }
177
+ // Note: `@mastra/core` is not listed here because its instrumentation
178
+ // doesn't go through the AST `code-transformer` matcher — Mastra's
179
+ // content-hashed chunks make `filePath`-based matching too brittle.
180
+ // Instead it's handled by the source-replacement entry in
181
+ // `loader/special-case-patches.ts`, which both the runtime loader
182
+ // (`hook.mjs` → `cjs-patch.ts`/`esm-hook.mts`) and the bundler plugin
183
+ // (`bundler/plugin.ts`) call. The `mastra` env-var disable still works.
207
184
  ];
208
185
  function getDefaultInstrumentationConfigs({
209
186
  additionalInstrumentations,
@@ -259,6 +236,18 @@ var unplugin = createUnplugin(
259
236
  return null;
260
237
  }
261
238
  const moduleName = moduleDetails.name;
239
+ const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
240
+ if (options.browser !== true) {
241
+ const patched = applySpecialCasePatch({
242
+ packageName: moduleName,
243
+ modulePath: normalizedModulePath,
244
+ source: code,
245
+ format: isModule ? "esm" : "cjs"
246
+ });
247
+ if (patched !== null) {
248
+ return { code: patched, map: null };
249
+ }
250
+ }
262
251
  const moduleVersion = getModuleVersion(moduleDetails.basedir);
263
252
  if (!moduleVersion) {
264
253
  console.warn(
@@ -266,7 +255,6 @@ var unplugin = createUnplugin(
266
255
  );
267
256
  return null;
268
257
  }
269
- const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
270
258
  const transformer = instrumentationMatcher.getTransformer(
271
259
  moduleName,
272
260
  moduleVersion,