braintrust 3.12.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.
- package/dev/dist/index.d.mts +31 -14
- package/dev/dist/index.d.ts +31 -14
- package/dev/dist/index.js +919 -485
- package/dev/dist/index.mjs +480 -46
- package/dist/apply-auto-instrumentation.js +204 -174
- package/dist/apply-auto-instrumentation.mjs +35 -5
- package/dist/auto-instrumentations/bundler/esbuild.cjs +226 -1
- package/dist/auto-instrumentations/bundler/esbuild.mjs +3 -2
- package/dist/auto-instrumentations/bundler/next.cjs +226 -1
- package/dist/auto-instrumentations/bundler/next.mjs +4 -3
- package/dist/auto-instrumentations/bundler/rollup.cjs +226 -1
- package/dist/auto-instrumentations/bundler/rollup.mjs +3 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +226 -1
- package/dist/auto-instrumentations/bundler/vite.mjs +3 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +9 -0
- package/dist/auto-instrumentations/bundler/webpack.cjs +226 -1
- package/dist/auto-instrumentations/bundler/webpack.mjs +4 -3
- package/dist/auto-instrumentations/{chunk-2DPA74KK.mjs → chunk-E5DUYJWK.mjs} +1 -0
- package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +208 -0
- package/dist/auto-instrumentations/{chunk-AFXRW7I7.mjs → chunk-OTUQ7KH5.mjs} +1 -1
- package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +280 -0
- package/dist/auto-instrumentations/{chunk-73BZUKVI.mjs → chunk-XKAAVWT6.mjs} +24 -2
- package/dist/auto-instrumentations/hook.mjs +7981 -7
- package/dist/auto-instrumentations/index.cjs +1 -0
- package/dist/auto-instrumentations/index.mjs +1 -1
- package/dist/auto-instrumentations/loader/cjs-patch.cjs +194 -4
- package/dist/auto-instrumentations/loader/cjs-patch.mjs +13 -27
- package/dist/auto-instrumentations/loader/esm-hook.mjs +24 -10
- package/dist/browser.d.mts +274 -30
- package/dist/browser.d.ts +274 -30
- package/dist/browser.js +407 -48
- package/dist/browser.mjs +407 -48
- package/dist/{chunk-BW4DF4CY.js → chunk-NKD77KGB.js} +180 -1
- package/dist/{chunk-MSLBGITU.mjs → chunk-NU2GSPHX.mjs} +180 -1
- package/dist/cli.js +494 -94
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +407 -48
- package/dist/edge-light.mjs +407 -48
- package/dist/index.d.mts +274 -30
- package/dist/index.d.ts +274 -30
- package/dist/index.js +1267 -857
- package/dist/index.mjs +465 -55
- package/dist/instrumentation/index.d.mts +47 -11
- package/dist/instrumentation/index.d.ts +47 -11
- package/dist/instrumentation/index.js +116 -26
- package/dist/instrumentation/index.mjs +116 -26
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +407 -48
- package/dist/workerd.mjs +407 -48
- package/package.json +3 -23
- package/util/dist/index.d.mts +3 -1
- package/util/dist/index.d.ts +3 -1
- 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
|
+
};
|
|
@@ -19,7 +19,10 @@ import {
|
|
|
19
19
|
openRouterAgentConfigs,
|
|
20
20
|
openRouterConfigs,
|
|
21
21
|
openaiConfigs
|
|
22
|
-
} from "./chunk-
|
|
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",
|
|
@@ -204,6 +208,13 @@ var defaultInstrumentationConfigGroups = [
|
|
|
204
208
|
integrations: ["gitHubCopilot"],
|
|
205
209
|
configs: gitHubCopilotConfigs
|
|
206
210
|
}
|
|
211
|
+
// Note: `@mastra/core` is not listed here because its instrumentation
|
|
212
|
+
// doesn't go through the AST `code-transformer` matcher — Mastra's
|
|
213
|
+
// content-hashed chunks make `filePath`-based matching too brittle.
|
|
214
|
+
// Instead it's handled by the source-replacement entry in
|
|
215
|
+
// `loader/special-case-patches.ts`, which both the runtime loader
|
|
216
|
+
// (`hook.mjs` → `cjs-patch.ts`/`esm-hook.mts`) and the bundler plugin
|
|
217
|
+
// (`bundler/plugin.ts`) call. The `mastra` env-var disable still works.
|
|
207
218
|
];
|
|
208
219
|
function getDefaultInstrumentationConfigs({
|
|
209
220
|
additionalInstrumentations,
|
|
@@ -259,6 +270,18 @@ var unplugin = createUnplugin(
|
|
|
259
270
|
return null;
|
|
260
271
|
}
|
|
261
272
|
const moduleName = moduleDetails.name;
|
|
273
|
+
const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
|
|
274
|
+
if (options.browser !== true) {
|
|
275
|
+
const patched = applySpecialCasePatch({
|
|
276
|
+
packageName: moduleName,
|
|
277
|
+
modulePath: normalizedModulePath,
|
|
278
|
+
source: code,
|
|
279
|
+
format: isModule ? "esm" : "cjs"
|
|
280
|
+
});
|
|
281
|
+
if (patched !== null) {
|
|
282
|
+
return { code: patched, map: null };
|
|
283
|
+
}
|
|
284
|
+
}
|
|
262
285
|
const moduleVersion = getModuleVersion(moduleDetails.basedir);
|
|
263
286
|
if (!moduleVersion) {
|
|
264
287
|
console.warn(
|
|
@@ -266,7 +289,6 @@ var unplugin = createUnplugin(
|
|
|
266
289
|
);
|
|
267
290
|
return null;
|
|
268
291
|
}
|
|
269
|
-
const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
|
|
270
292
|
const transformer = instrumentationMatcher.getTransformer(
|
|
271
293
|
moduleName,
|
|
272
294
|
moduleVersion,
|