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
|
@@ -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",
|
|
@@ -191,6 +192,7 @@ var iso = {
|
|
|
191
192
|
getRepoInfo: async (_settings) => void 0,
|
|
192
193
|
getPastNAncestors: async () => [],
|
|
193
194
|
getEnv: (_name) => void 0,
|
|
195
|
+
getBraintrustApiKey: async () => void 0,
|
|
194
196
|
getCallerLocation: () => void 0,
|
|
195
197
|
newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
|
|
196
198
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -2989,6 +2991,13 @@ var defaultInstrumentationConfigGroups = [
|
|
|
2989
2991
|
integrations: ["gitHubCopilot"],
|
|
2990
2992
|
configs: gitHubCopilotConfigs
|
|
2991
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.
|
|
2992
3001
|
];
|
|
2993
3002
|
function getDefaultInstrumentationConfigs({
|
|
2994
3003
|
additionalInstrumentations,
|
|
@@ -3006,6 +3015,211 @@ function getDefaultInstrumentationConfigs({
|
|
|
3006
3015
|
];
|
|
3007
3016
|
}
|
|
3008
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
|
+
|
|
3009
3223
|
// src/auto-instrumentations/bundler/plugin.ts
|
|
3010
3224
|
function getModuleVersion(basedir) {
|
|
3011
3225
|
try {
|
|
@@ -3044,6 +3258,18 @@ var unplugin = (0, import_unplugin.createUnplugin)(
|
|
|
3044
3258
|
return null;
|
|
3045
3259
|
}
|
|
3046
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
|
+
}
|
|
3047
3273
|
const moduleVersion = getModuleVersion(moduleDetails.basedir);
|
|
3048
3274
|
if (!moduleVersion) {
|
|
3049
3275
|
console.warn(
|
|
@@ -3051,7 +3277,6 @@ var unplugin = (0, import_unplugin.createUnplugin)(
|
|
|
3051
3277
|
);
|
|
3052
3278
|
return null;
|
|
3053
3279
|
}
|
|
3054
|
-
const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
|
|
3055
3280
|
const transformer = instrumentationMatcher.getTransformer(
|
|
3056
3281
|
moduleName,
|
|
3057
3282
|
moduleVersion,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
braintrustWebpackPlugin,
|
|
3
3
|
webpackPlugin
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-OTUQ7KH5.mjs";
|
|
5
|
+
import "../chunk-XKAAVWT6.mjs";
|
|
6
|
+
import "../chunk-E5DUYJWK.mjs";
|
|
7
|
+
import "../chunk-J57YF7WS.mjs";
|
|
7
8
|
export {
|
|
8
9
|
braintrustWebpackPlugin,
|
|
9
10
|
webpackPlugin
|
|
@@ -83,6 +83,7 @@ var iso = {
|
|
|
83
83
|
getRepoInfo: async (_settings) => void 0,
|
|
84
84
|
getPastNAncestors: async () => [],
|
|
85
85
|
getEnv: (_name) => void 0,
|
|
86
|
+
getBraintrustApiKey: async () => void 0,
|
|
86
87
|
getCallerLocation: () => void 0,
|
|
87
88
|
newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
|
|
88
89
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -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
|
+
};
|