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.
- package/dev/dist/index.d.mts +6 -8
- package/dev/dist/index.d.ts +6 -8
- package/dev/dist/index.js +809 -466
- package/dev/dist/index.mjs +367 -24
- package/dist/apply-auto-instrumentation.js +204 -174
- package/dist/apply-auto-instrumentation.mjs +35 -5
- package/dist/auto-instrumentations/bundler/esbuild.cjs +225 -1
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -1
- package/dist/auto-instrumentations/bundler/next.cjs +225 -1
- package/dist/auto-instrumentations/bundler/next.mjs +3 -2
- package/dist/auto-instrumentations/bundler/rollup.cjs +225 -1
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -1
- package/dist/auto-instrumentations/bundler/vite.cjs +225 -1
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -1
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +8 -0
- package/dist/auto-instrumentations/bundler/webpack.cjs +225 -1
- package/dist/auto-instrumentations/bundler/webpack.mjs +3 -2
- package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +208 -0
- package/dist/auto-instrumentations/{chunk-WFEUJACP.mjs → chunk-OTUQ7KH5.mjs} +1 -1
- package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +280 -0
- package/dist/auto-instrumentations/{chunk-GJOO4ESL.mjs → chunk-XKAAVWT6.mjs} +23 -1
- package/dist/auto-instrumentations/hook.mjs +7980 -7
- 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 +127 -11
- package/dist/browser.d.ts +127 -11
- package/dist/browser.js +293 -24
- package/dist/browser.mjs +293 -24
- package/dist/{chunk-26JGOELH.js → chunk-NKD77KGB.js} +179 -1
- package/dist/{chunk-75IQCUB2.mjs → chunk-NU2GSPHX.mjs} +179 -1
- package/dist/cli.js +374 -51
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +293 -24
- package/dist/edge-light.mjs +293 -24
- package/dist/index.d.mts +127 -11
- package/dist/index.d.ts +127 -11
- package/dist/index.js +1113 -838
- package/dist/index.mjs +305 -30
- package/dist/instrumentation/index.d.mts +7 -8
- package/dist/instrumentation/index.d.ts +7 -8
- package/dist/instrumentation/index.js +101 -24
- package/dist/instrumentation/index.mjs +101 -24
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +293 -24
- package/dist/workerd.mjs +293 -24
- package/package.json +1 -7
- package/dist/auto-instrumentations/chunk-MWZXZQUO.mjs +0 -81
package/dev/dist/index.mjs
CHANGED
|
@@ -4254,6 +4254,76 @@ var LRUCache = class {
|
|
|
4254
4254
|
}
|
|
4255
4255
|
};
|
|
4256
4256
|
|
|
4257
|
+
// src/prompt-cache/cache-config.ts
|
|
4258
|
+
var CACHE_LOCATION_ENV_VAR = "BRAINTRUST_CACHE_LOCATION";
|
|
4259
|
+
var DEFAULT_CACHE_MEMORY_MAX = 1 << 10;
|
|
4260
|
+
var DEFAULT_CACHE_DISK_MAX = 1 << 20;
|
|
4261
|
+
var warnedInvalidCacheModeEnvValue = false;
|
|
4262
|
+
var warnedUnavailableDiskCacheMode = false;
|
|
4263
|
+
function warnInvalidCacheMode(value) {
|
|
4264
|
+
if (warnedInvalidCacheModeEnvValue) {
|
|
4265
|
+
return;
|
|
4266
|
+
}
|
|
4267
|
+
warnedInvalidCacheModeEnvValue = true;
|
|
4268
|
+
debugLogger.warn(
|
|
4269
|
+
`Invalid ${CACHE_LOCATION_ENV_VAR} value "${value}". Expected "mixed", "memory", "disk", or "none". Falling back to "mixed".`
|
|
4270
|
+
);
|
|
4271
|
+
}
|
|
4272
|
+
function warnUnavailableDiskCache() {
|
|
4273
|
+
if (warnedUnavailableDiskCacheMode) {
|
|
4274
|
+
return;
|
|
4275
|
+
}
|
|
4276
|
+
warnedUnavailableDiskCacheMode = true;
|
|
4277
|
+
debugLogger.warn(
|
|
4278
|
+
`Disk cache is not supported on this platform, so ${CACHE_LOCATION_ENV_VAR}="disk" disables prompt and parameters caching.`
|
|
4279
|
+
);
|
|
4280
|
+
}
|
|
4281
|
+
function parseCacheMode() {
|
|
4282
|
+
const value = isomorph_default.getEnv(CACHE_LOCATION_ENV_VAR);
|
|
4283
|
+
const normalized = value?.trim().toLowerCase();
|
|
4284
|
+
if (!normalized) {
|
|
4285
|
+
return "mixed";
|
|
4286
|
+
}
|
|
4287
|
+
if (normalized === "mixed" || normalized === "memory" || normalized === "disk" || normalized === "none") {
|
|
4288
|
+
return normalized;
|
|
4289
|
+
}
|
|
4290
|
+
warnInvalidCacheMode(value ?? "");
|
|
4291
|
+
return "mixed";
|
|
4292
|
+
}
|
|
4293
|
+
function parsePositiveIntegerEnv(envVar, defaultValue) {
|
|
4294
|
+
const value = Number(isomorph_default.getEnv(envVar));
|
|
4295
|
+
return Number.isInteger(value) && value > 0 ? value : defaultValue;
|
|
4296
|
+
}
|
|
4297
|
+
function createCacheLayers({
|
|
4298
|
+
memoryMaxEnvVar,
|
|
4299
|
+
diskCacheDirEnvVar,
|
|
4300
|
+
diskMaxEnvVar,
|
|
4301
|
+
getDefaultDiskCacheDir
|
|
4302
|
+
}) {
|
|
4303
|
+
const mode = parseCacheMode();
|
|
4304
|
+
const memoryCache = mode === "mixed" || mode === "memory" ? new LRUCache({
|
|
4305
|
+
max: parsePositiveIntegerEnv(
|
|
4306
|
+
memoryMaxEnvVar,
|
|
4307
|
+
DEFAULT_CACHE_MEMORY_MAX
|
|
4308
|
+
)
|
|
4309
|
+
}) : void 0;
|
|
4310
|
+
let diskCache;
|
|
4311
|
+
if (mode === "mixed" || mode === "disk") {
|
|
4312
|
+
if (canUseDiskCache()) {
|
|
4313
|
+
diskCache = new DiskCache({
|
|
4314
|
+
cacheDir: isomorph_default.getEnv(diskCacheDirEnvVar) ?? getDefaultDiskCacheDir(),
|
|
4315
|
+
max: parsePositiveIntegerEnv(diskMaxEnvVar, DEFAULT_CACHE_DISK_MAX)
|
|
4316
|
+
});
|
|
4317
|
+
} else if (mode === "disk") {
|
|
4318
|
+
warnUnavailableDiskCache();
|
|
4319
|
+
}
|
|
4320
|
+
}
|
|
4321
|
+
if (diskCache) {
|
|
4322
|
+
return { memoryCache, diskCache };
|
|
4323
|
+
}
|
|
4324
|
+
return { memoryCache };
|
|
4325
|
+
}
|
|
4326
|
+
|
|
4257
4327
|
// src/prompt-cache/prompt-cache.ts
|
|
4258
4328
|
function createCacheKey(key) {
|
|
4259
4329
|
if (key.id) {
|
|
@@ -4281,16 +4351,18 @@ var PromptCache = class {
|
|
|
4281
4351
|
*/
|
|
4282
4352
|
async get(key) {
|
|
4283
4353
|
const cacheKey = createCacheKey(key);
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4354
|
+
if (this.memoryCache) {
|
|
4355
|
+
const memoryPrompt = this.memoryCache.get(cacheKey);
|
|
4356
|
+
if (memoryPrompt !== void 0) {
|
|
4357
|
+
return memoryPrompt;
|
|
4358
|
+
}
|
|
4287
4359
|
}
|
|
4288
4360
|
if (this.diskCache) {
|
|
4289
4361
|
const diskPrompt = await this.diskCache.get(cacheKey);
|
|
4290
4362
|
if (!diskPrompt) {
|
|
4291
4363
|
return void 0;
|
|
4292
4364
|
}
|
|
4293
|
-
this.memoryCache
|
|
4365
|
+
this.memoryCache?.set(cacheKey, diskPrompt);
|
|
4294
4366
|
return diskPrompt;
|
|
4295
4367
|
}
|
|
4296
4368
|
return void 0;
|
|
@@ -4305,7 +4377,7 @@ var PromptCache = class {
|
|
|
4305
4377
|
*/
|
|
4306
4378
|
async set(key, value) {
|
|
4307
4379
|
const cacheKey = createCacheKey(key);
|
|
4308
|
-
this.memoryCache
|
|
4380
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4309
4381
|
if (this.diskCache) {
|
|
4310
4382
|
await this.diskCache.set(cacheKey, value);
|
|
4311
4383
|
}
|
|
@@ -4335,23 +4407,25 @@ var ParametersCache = class {
|
|
|
4335
4407
|
}
|
|
4336
4408
|
async get(key) {
|
|
4337
4409
|
const cacheKey = createCacheKey2(key);
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4410
|
+
if (this.memoryCache) {
|
|
4411
|
+
const memoryParams = this.memoryCache.get(cacheKey);
|
|
4412
|
+
if (memoryParams !== void 0) {
|
|
4413
|
+
return memoryParams;
|
|
4414
|
+
}
|
|
4341
4415
|
}
|
|
4342
4416
|
if (this.diskCache) {
|
|
4343
4417
|
const diskParams = await this.diskCache.get(cacheKey);
|
|
4344
4418
|
if (!diskParams) {
|
|
4345
4419
|
return void 0;
|
|
4346
4420
|
}
|
|
4347
|
-
this.memoryCache
|
|
4421
|
+
this.memoryCache?.set(cacheKey, diskParams);
|
|
4348
4422
|
return diskParams;
|
|
4349
4423
|
}
|
|
4350
4424
|
return void 0;
|
|
4351
4425
|
}
|
|
4352
4426
|
async set(key, value) {
|
|
4353
4427
|
const cacheKey = createCacheKey2(key);
|
|
4354
|
-
this.memoryCache
|
|
4428
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4355
4429
|
if (this.diskCache) {
|
|
4356
4430
|
await this.diskCache.set(cacheKey, value);
|
|
4357
4431
|
}
|
|
@@ -4883,21 +4957,22 @@ var BraintrustState = class _BraintrustState {
|
|
|
4883
4957
|
setGlobalDebugLogLevel(void 0);
|
|
4884
4958
|
}
|
|
4885
4959
|
this.resetLoginInfo();
|
|
4886
|
-
const memoryCache =
|
|
4887
|
-
|
|
4960
|
+
const { memoryCache, diskCache } = createCacheLayers({
|
|
4961
|
+
memoryMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_MEMORY_MAX",
|
|
4962
|
+
diskCacheDirEnvVar: "BRAINTRUST_PROMPT_CACHE_DIR",
|
|
4963
|
+
diskMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_DISK_MAX",
|
|
4964
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`
|
|
4888
4965
|
});
|
|
4889
|
-
const diskCache = canUseDiskCache() ? new DiskCache({
|
|
4890
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`,
|
|
4891
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4892
|
-
}) : void 0;
|
|
4893
4966
|
this.promptCache = new PromptCache({ memoryCache, diskCache });
|
|
4894
|
-
const
|
|
4895
|
-
|
|
4967
|
+
const {
|
|
4968
|
+
memoryCache: parametersMemoryCache,
|
|
4969
|
+
diskCache: parametersDiskCache
|
|
4970
|
+
} = createCacheLayers({
|
|
4971
|
+
memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
|
|
4972
|
+
diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
|
|
4973
|
+
diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
|
|
4974
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
|
|
4896
4975
|
});
|
|
4897
|
-
const parametersDiskCache = canUseDiskCache() ? new DiskCache({
|
|
4898
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`,
|
|
4899
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4900
|
-
}) : void 0;
|
|
4901
4976
|
this.parametersCache = new ParametersCache({
|
|
4902
4977
|
memoryCache: parametersMemoryCache,
|
|
4903
4978
|
diskCache: parametersDiskCache
|
|
@@ -25087,7 +25162,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25087
25162
|
this.config = config;
|
|
25088
25163
|
}
|
|
25089
25164
|
onEnable() {
|
|
25090
|
-
const integrations = this.config.integrations
|
|
25165
|
+
const integrations = this.config.integrations ?? {};
|
|
25091
25166
|
if (integrations.openai !== false) {
|
|
25092
25167
|
this.openaiPlugin = new OpenAIPlugin();
|
|
25093
25168
|
this.openaiPlugin.enable();
|
|
@@ -25152,7 +25227,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25152
25227
|
this.genkitPlugin = new GenkitPlugin();
|
|
25153
25228
|
this.genkitPlugin.enable();
|
|
25154
25229
|
}
|
|
25155
|
-
if (
|
|
25230
|
+
if (integrations.gitHubCopilot !== false) {
|
|
25156
25231
|
this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
|
|
25157
25232
|
this.gitHubCopilotPlugin.enable();
|
|
25158
25233
|
}
|
|
@@ -25265,6 +25340,7 @@ var envIntegrationAliases = {
|
|
|
25265
25340
|
cursorsdk: "cursorSDK",
|
|
25266
25341
|
flue: "flue",
|
|
25267
25342
|
"flue-runtime": "flue",
|
|
25343
|
+
mastra: "mastra",
|
|
25268
25344
|
"openai-agents": "openAIAgents",
|
|
25269
25345
|
openaiagents: "openAIAgents",
|
|
25270
25346
|
"openai-agents-core": "openAIAgents",
|
|
@@ -25307,6 +25383,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
25307
25383
|
cursor: true,
|
|
25308
25384
|
cursorSDK: true,
|
|
25309
25385
|
flue: true,
|
|
25386
|
+
mastra: true,
|
|
25310
25387
|
openAIAgents: true,
|
|
25311
25388
|
openrouter: true,
|
|
25312
25389
|
openrouterAgent: true,
|
|
@@ -25331,6 +25408,9 @@ function readDisabledInstrumentationEnvConfig(disabledList) {
|
|
|
25331
25408
|
}
|
|
25332
25409
|
return { integrations };
|
|
25333
25410
|
}
|
|
25411
|
+
function isInstrumentationIntegrationDisabled(integrations, ...names) {
|
|
25412
|
+
return names.some((name) => integrations?.[name] === false);
|
|
25413
|
+
}
|
|
25334
25414
|
|
|
25335
25415
|
// src/instrumentation/registry.ts
|
|
25336
25416
|
var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for("braintrust.registry");
|
|
@@ -25424,6 +25504,263 @@ var PluginRegistry = class {
|
|
|
25424
25504
|
};
|
|
25425
25505
|
var registry = new PluginRegistry();
|
|
25426
25506
|
|
|
25507
|
+
// src/auto-instrumentations/loader/mastra-observability-patch.ts
|
|
25508
|
+
var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
|
|
25509
|
+
function installMastraExporterFactory(factory) {
|
|
25510
|
+
const globals = globalThis;
|
|
25511
|
+
globals[MASTRA_EXPORTER_FACTORY_GLOBAL] ??= factory;
|
|
25512
|
+
}
|
|
25513
|
+
var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
|
|
25514
|
+
var OBSERVABILITY_APPEND_BODY = `
|
|
25515
|
+
;(function __braintrustWrapObservability() {
|
|
25516
|
+
// Top-level so we can both read and reassign the var binding the original
|
|
25517
|
+
// entry declared.
|
|
25518
|
+
if (typeof Observability === "undefined") return;
|
|
25519
|
+
if (Observability.__braintrustWrapped) return;
|
|
25520
|
+
function __braintrustEnsureExporter(rawConfig) {
|
|
25521
|
+
try {
|
|
25522
|
+
var factory = globalThis[${EXPORTER_FACTORY_KEY}];
|
|
25523
|
+
if (typeof factory !== "function") return rawConfig;
|
|
25524
|
+
var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
|
|
25525
|
+
var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
|
|
25526
|
+
var configsOut = {};
|
|
25527
|
+
var hadEntries = false;
|
|
25528
|
+
if (configsIn) {
|
|
25529
|
+
for (var name in configsIn) {
|
|
25530
|
+
if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
|
|
25531
|
+
hadEntries = true;
|
|
25532
|
+
var inst = configsIn[name] || {};
|
|
25533
|
+
var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
|
|
25534
|
+
var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
|
|
25535
|
+
configsOut[name] = Object.assign({}, inst, {
|
|
25536
|
+
exporters: hasOurs ? existing : existing.concat([factory()]),
|
|
25537
|
+
});
|
|
25538
|
+
}
|
|
25539
|
+
}
|
|
25540
|
+
if (!hadEntries) {
|
|
25541
|
+
configsOut.default = {
|
|
25542
|
+
serviceName: "mastra",
|
|
25543
|
+
exporters: [factory()],
|
|
25544
|
+
};
|
|
25545
|
+
}
|
|
25546
|
+
return Object.assign({}, config, { configs: configsOut });
|
|
25547
|
+
} catch (e) {
|
|
25548
|
+
return rawConfig;
|
|
25549
|
+
}
|
|
25550
|
+
}
|
|
25551
|
+
var __OriginalObservability = Observability;
|
|
25552
|
+
Observability = new Proxy(__OriginalObservability, {
|
|
25553
|
+
construct: function (target, args, newTarget) {
|
|
25554
|
+
var nextArgs = args.slice();
|
|
25555
|
+
nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
|
|
25556
|
+
return Reflect.construct(target, nextArgs, newTarget);
|
|
25557
|
+
},
|
|
25558
|
+
});
|
|
25559
|
+
Observability.__braintrustWrapped = true;
|
|
25560
|
+
if (typeof exports !== "undefined" && exports && typeof exports === "object") {
|
|
25561
|
+
try {
|
|
25562
|
+
Object.defineProperty(exports, "Observability", {
|
|
25563
|
+
enumerable: true,
|
|
25564
|
+
configurable: true,
|
|
25565
|
+
get: function () { return Observability; },
|
|
25566
|
+
});
|
|
25567
|
+
} catch (e) {}
|
|
25568
|
+
}
|
|
25569
|
+
})();
|
|
25570
|
+
`;
|
|
25571
|
+
|
|
25572
|
+
// src/wrappers/mastra.ts
|
|
25573
|
+
var MASTRA_BRAINTRUST_EXPORTER_NAME = "braintrust";
|
|
25574
|
+
var SPAN_TYPE_MAP = {
|
|
25575
|
+
agent_run: "task" /* TASK */,
|
|
25576
|
+
model_generation: "llm" /* LLM */,
|
|
25577
|
+
model_step: "llm" /* LLM */,
|
|
25578
|
+
model_chunk: "llm" /* LLM */,
|
|
25579
|
+
tool_call: "tool" /* TOOL */,
|
|
25580
|
+
mcp_tool_call: "tool" /* TOOL */,
|
|
25581
|
+
workflow_run: "task" /* TASK */,
|
|
25582
|
+
workflow_step: "function" /* FUNCTION */,
|
|
25583
|
+
workflow_conditional: "function" /* FUNCTION */,
|
|
25584
|
+
workflow_conditional_eval: "function" /* FUNCTION */,
|
|
25585
|
+
workflow_parallel: "function" /* FUNCTION */,
|
|
25586
|
+
workflow_loop: "function" /* FUNCTION */,
|
|
25587
|
+
workflow_sleep: "function" /* FUNCTION */,
|
|
25588
|
+
workflow_wait_event: "function" /* FUNCTION */,
|
|
25589
|
+
memory_operation: "function" /* FUNCTION */,
|
|
25590
|
+
workspace_action: "function" /* FUNCTION */,
|
|
25591
|
+
rag_ingestion: "task" /* TASK */,
|
|
25592
|
+
rag_embedding: "llm" /* LLM */,
|
|
25593
|
+
rag_vector_operation: "function" /* FUNCTION */,
|
|
25594
|
+
rag_action: "function" /* FUNCTION */,
|
|
25595
|
+
graph_action: "function" /* FUNCTION */,
|
|
25596
|
+
scorer_run: "score" /* SCORE */,
|
|
25597
|
+
scorer_step: "score" /* SCORE */,
|
|
25598
|
+
processor_run: "function" /* FUNCTION */,
|
|
25599
|
+
generic: "function" /* FUNCTION */
|
|
25600
|
+
};
|
|
25601
|
+
function spanTypeFor(mastraType) {
|
|
25602
|
+
return SPAN_TYPE_MAP[mastraType] ?? "function" /* FUNCTION */;
|
|
25603
|
+
}
|
|
25604
|
+
function epochSeconds(value) {
|
|
25605
|
+
if (value === void 0) return void 0;
|
|
25606
|
+
const ms = value instanceof Date ? value.getTime() : typeof value === "number" ? value : Date.parse(value);
|
|
25607
|
+
return Number.isFinite(ms) ? ms / 1e3 : void 0;
|
|
25608
|
+
}
|
|
25609
|
+
function modelMetrics(attributes) {
|
|
25610
|
+
if (!isObject(attributes)) return void 0;
|
|
25611
|
+
const usage = isObject(attributes.usage) ? attributes.usage : void 0;
|
|
25612
|
+
if (!usage) return void 0;
|
|
25613
|
+
const out = {};
|
|
25614
|
+
if (typeof usage.inputTokens === "number")
|
|
25615
|
+
out.prompt_tokens = usage.inputTokens;
|
|
25616
|
+
if (typeof usage.outputTokens === "number")
|
|
25617
|
+
out.completion_tokens = usage.outputTokens;
|
|
25618
|
+
if (typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
|
|
25619
|
+
out.tokens = usage.inputTokens + usage.outputTokens;
|
|
25620
|
+
}
|
|
25621
|
+
const inputDetails = isObject(usage.inputDetails) ? usage.inputDetails : void 0;
|
|
25622
|
+
const outputDetails = isObject(usage.outputDetails) ? usage.outputDetails : void 0;
|
|
25623
|
+
if (inputDetails && typeof inputDetails.cacheRead === "number") {
|
|
25624
|
+
out.prompt_cached_tokens = inputDetails.cacheRead;
|
|
25625
|
+
}
|
|
25626
|
+
if (inputDetails && typeof inputDetails.cacheWrite === "number") {
|
|
25627
|
+
out.prompt_cache_creation_tokens = inputDetails.cacheWrite;
|
|
25628
|
+
}
|
|
25629
|
+
if (outputDetails && typeof outputDetails.reasoning === "number") {
|
|
25630
|
+
out.completion_reasoning_tokens = outputDetails.reasoning;
|
|
25631
|
+
}
|
|
25632
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
25633
|
+
}
|
|
25634
|
+
function buildMetadata(exported) {
|
|
25635
|
+
const out = {};
|
|
25636
|
+
if (exported.entityId !== void 0) out.entity_id = exported.entityId;
|
|
25637
|
+
if (exported.entityName !== void 0) out.entity_name = exported.entityName;
|
|
25638
|
+
if (exported.entityType !== void 0) out.entity_type = exported.entityType;
|
|
25639
|
+
if (exported.metadata && isObject(exported.metadata)) {
|
|
25640
|
+
Object.assign(out, exported.metadata);
|
|
25641
|
+
}
|
|
25642
|
+
if (exported.attributes && isObject(exported.attributes)) {
|
|
25643
|
+
for (const [key, value] of Object.entries(exported.attributes)) {
|
|
25644
|
+
if (key === "usage") continue;
|
|
25645
|
+
if (value !== void 0) out[key] = value;
|
|
25646
|
+
}
|
|
25647
|
+
}
|
|
25648
|
+
if (exported.tags && exported.tags.length > 0) {
|
|
25649
|
+
out.tags = exported.tags;
|
|
25650
|
+
}
|
|
25651
|
+
if (exported.requestContext && isObject(exported.requestContext)) {
|
|
25652
|
+
out.request_context = exported.requestContext;
|
|
25653
|
+
}
|
|
25654
|
+
return out;
|
|
25655
|
+
}
|
|
25656
|
+
var BraintrustObservabilityExporter = class {
|
|
25657
|
+
name = MASTRA_BRAINTRUST_EXPORTER_NAME;
|
|
25658
|
+
spans = /* @__PURE__ */ new Map();
|
|
25659
|
+
// Captured at the first SPAN_STARTED event. Mastra's observability bus may
|
|
25660
|
+
// dispatch later events outside the user's AsyncLocalStorage context, where
|
|
25661
|
+
// `currentSpan()` returns NOOP_SPAN — which would make our `startSpan()`
|
|
25662
|
+
// calls go to a no-op logger and silently drop. Anchoring on the parent
|
|
25663
|
+
// we observe while still in-context keeps the whole Mastra subtree under
|
|
25664
|
+
// the user's traced scenario.
|
|
25665
|
+
capturedParent;
|
|
25666
|
+
constructor() {
|
|
25667
|
+
_internalSetInitialState();
|
|
25668
|
+
}
|
|
25669
|
+
async exportTracingEvent(event) {
|
|
25670
|
+
const exported = event.exportedSpan;
|
|
25671
|
+
if (exported.isInternal === true) return;
|
|
25672
|
+
try {
|
|
25673
|
+
switch (event.type) {
|
|
25674
|
+
case "span_started":
|
|
25675
|
+
this.onStart(exported);
|
|
25676
|
+
break;
|
|
25677
|
+
case "span_updated":
|
|
25678
|
+
this.onUpdate(exported);
|
|
25679
|
+
break;
|
|
25680
|
+
case "span_ended":
|
|
25681
|
+
this.onEnd(exported);
|
|
25682
|
+
break;
|
|
25683
|
+
}
|
|
25684
|
+
} catch (err) {
|
|
25685
|
+
logExporterError(err);
|
|
25686
|
+
}
|
|
25687
|
+
}
|
|
25688
|
+
async flush() {
|
|
25689
|
+
const state = _internalGetGlobalState();
|
|
25690
|
+
if (state) {
|
|
25691
|
+
await state.bgLogger().flush();
|
|
25692
|
+
}
|
|
25693
|
+
}
|
|
25694
|
+
async shutdown() {
|
|
25695
|
+
await this.flush();
|
|
25696
|
+
this.spans.clear();
|
|
25697
|
+
}
|
|
25698
|
+
onStart(exported) {
|
|
25699
|
+
if (this.spans.has(exported.id)) return;
|
|
25700
|
+
const args = {
|
|
25701
|
+
name: exported.name,
|
|
25702
|
+
spanAttributes: { type: spanTypeFor(exported.type) },
|
|
25703
|
+
startTime: epochSeconds(exported.startTime)
|
|
25704
|
+
};
|
|
25705
|
+
const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
|
|
25706
|
+
if (!this.capturedParent) {
|
|
25707
|
+
const probe = currentSpan();
|
|
25708
|
+
if (probe && probe.spanId) {
|
|
25709
|
+
this.capturedParent = probe;
|
|
25710
|
+
}
|
|
25711
|
+
}
|
|
25712
|
+
const span = parentRecord ? parentRecord.span.startSpan(args) : this.capturedParent ? this.capturedParent.startSpan(args) : startSpan(args);
|
|
25713
|
+
const record = { span, hasLoggedInput: false };
|
|
25714
|
+
this.logPayload(record, exported);
|
|
25715
|
+
this.spans.set(exported.id, record);
|
|
25716
|
+
if (exported.isEvent === true) {
|
|
25717
|
+
span.end({ endTime: args.startTime });
|
|
25718
|
+
this.spans.delete(exported.id);
|
|
25719
|
+
}
|
|
25720
|
+
}
|
|
25721
|
+
onUpdate(exported) {
|
|
25722
|
+
const record = this.spans.get(exported.id);
|
|
25723
|
+
if (!record) return;
|
|
25724
|
+
this.logPayload(record, exported);
|
|
25725
|
+
}
|
|
25726
|
+
onEnd(exported) {
|
|
25727
|
+
const record = this.spans.get(exported.id);
|
|
25728
|
+
if (!record) return;
|
|
25729
|
+
this.logPayload(record, exported);
|
|
25730
|
+
if (exported.errorInfo) {
|
|
25731
|
+
record.span.log({
|
|
25732
|
+
error: exported.errorInfo.message || exported.errorInfo.name || "Unknown Mastra error"
|
|
25733
|
+
});
|
|
25734
|
+
}
|
|
25735
|
+
record.span.end({ endTime: epochSeconds(exported.endTime) });
|
|
25736
|
+
this.spans.delete(exported.id);
|
|
25737
|
+
}
|
|
25738
|
+
logPayload(record, exported) {
|
|
25739
|
+
const event = {};
|
|
25740
|
+
if (exported.input !== void 0) {
|
|
25741
|
+
event.input = exported.input;
|
|
25742
|
+
record.hasLoggedInput = true;
|
|
25743
|
+
}
|
|
25744
|
+
if (exported.output !== void 0) {
|
|
25745
|
+
event.output = exported.output;
|
|
25746
|
+
}
|
|
25747
|
+
const metadata = buildMetadata(exported);
|
|
25748
|
+
if (Object.keys(metadata).length > 0) {
|
|
25749
|
+
event.metadata = metadata;
|
|
25750
|
+
}
|
|
25751
|
+
const metrics = modelMetrics(exported.attributes);
|
|
25752
|
+
if (metrics) {
|
|
25753
|
+
event.metrics = metrics;
|
|
25754
|
+
}
|
|
25755
|
+
if (Object.keys(event).length > 0) {
|
|
25756
|
+
record.span.log(event);
|
|
25757
|
+
}
|
|
25758
|
+
}
|
|
25759
|
+
};
|
|
25760
|
+
function logExporterError(err) {
|
|
25761
|
+
debugLogger.warn("Mastra exporter failure:", err);
|
|
25762
|
+
}
|
|
25763
|
+
|
|
25427
25764
|
// src/node/config.ts
|
|
25428
25765
|
var BRAINTRUST_ENV_SEARCH_PARENT_LIMIT = 64;
|
|
25429
25766
|
function configureNode() {
|
|
@@ -25509,6 +25846,12 @@ function configureNode() {
|
|
|
25509
25846
|
isomorph_default.gunzip = promisify(zlib.gunzip);
|
|
25510
25847
|
isomorph_default.hash = (data) => crypto.createHash("sha256").update(data).digest("hex");
|
|
25511
25848
|
_internalSetInitialState();
|
|
25849
|
+
const disabled = readDisabledInstrumentationEnvConfig(
|
|
25850
|
+
isomorph_default.getEnv("BRAINTRUST_DISABLE_INSTRUMENTATION")
|
|
25851
|
+
).integrations;
|
|
25852
|
+
if (!isInstrumentationIntegrationDisabled(disabled, "mastra")) {
|
|
25853
|
+
installMastraExporterFactory(() => new BraintrustObservabilityExporter());
|
|
25854
|
+
}
|
|
25512
25855
|
registry.enable();
|
|
25513
25856
|
}
|
|
25514
25857
|
|