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/dist/edge-light.js
CHANGED
|
@@ -38,6 +38,7 @@ __export(edge_light_exports, {
|
|
|
38
38
|
BaseExperiment: () => BaseExperiment,
|
|
39
39
|
BraintrustLangChainCallbackHandler: () => BraintrustLangChainCallbackHandler,
|
|
40
40
|
BraintrustMiddleware: () => BraintrustMiddleware,
|
|
41
|
+
BraintrustObservabilityExporter: () => BraintrustObservabilityExporter,
|
|
41
42
|
BraintrustState: () => BraintrustState,
|
|
42
43
|
BraintrustStream: () => BraintrustStream,
|
|
43
44
|
CachedSpanFetcher: () => CachedSpanFetcher,
|
|
@@ -4112,6 +4113,76 @@ var LRUCache = class {
|
|
|
4112
4113
|
}
|
|
4113
4114
|
};
|
|
4114
4115
|
|
|
4116
|
+
// src/prompt-cache/cache-config.ts
|
|
4117
|
+
var CACHE_LOCATION_ENV_VAR = "BRAINTRUST_CACHE_LOCATION";
|
|
4118
|
+
var DEFAULT_CACHE_MEMORY_MAX = 1 << 10;
|
|
4119
|
+
var DEFAULT_CACHE_DISK_MAX = 1 << 20;
|
|
4120
|
+
var warnedInvalidCacheModeEnvValue = false;
|
|
4121
|
+
var warnedUnavailableDiskCacheMode = false;
|
|
4122
|
+
function warnInvalidCacheMode(value) {
|
|
4123
|
+
if (warnedInvalidCacheModeEnvValue) {
|
|
4124
|
+
return;
|
|
4125
|
+
}
|
|
4126
|
+
warnedInvalidCacheModeEnvValue = true;
|
|
4127
|
+
debugLogger.warn(
|
|
4128
|
+
`Invalid ${CACHE_LOCATION_ENV_VAR} value "${value}". Expected "mixed", "memory", "disk", or "none". Falling back to "mixed".`
|
|
4129
|
+
);
|
|
4130
|
+
}
|
|
4131
|
+
function warnUnavailableDiskCache() {
|
|
4132
|
+
if (warnedUnavailableDiskCacheMode) {
|
|
4133
|
+
return;
|
|
4134
|
+
}
|
|
4135
|
+
warnedUnavailableDiskCacheMode = true;
|
|
4136
|
+
debugLogger.warn(
|
|
4137
|
+
`Disk cache is not supported on this platform, so ${CACHE_LOCATION_ENV_VAR}="disk" disables prompt and parameters caching.`
|
|
4138
|
+
);
|
|
4139
|
+
}
|
|
4140
|
+
function parseCacheMode() {
|
|
4141
|
+
const value = isomorph_default.getEnv(CACHE_LOCATION_ENV_VAR);
|
|
4142
|
+
const normalized = value?.trim().toLowerCase();
|
|
4143
|
+
if (!normalized) {
|
|
4144
|
+
return "mixed";
|
|
4145
|
+
}
|
|
4146
|
+
if (normalized === "mixed" || normalized === "memory" || normalized === "disk" || normalized === "none") {
|
|
4147
|
+
return normalized;
|
|
4148
|
+
}
|
|
4149
|
+
warnInvalidCacheMode(value ?? "");
|
|
4150
|
+
return "mixed";
|
|
4151
|
+
}
|
|
4152
|
+
function parsePositiveIntegerEnv(envVar, defaultValue) {
|
|
4153
|
+
const value = Number(isomorph_default.getEnv(envVar));
|
|
4154
|
+
return Number.isInteger(value) && value > 0 ? value : defaultValue;
|
|
4155
|
+
}
|
|
4156
|
+
function createCacheLayers({
|
|
4157
|
+
memoryMaxEnvVar,
|
|
4158
|
+
diskCacheDirEnvVar,
|
|
4159
|
+
diskMaxEnvVar,
|
|
4160
|
+
getDefaultDiskCacheDir
|
|
4161
|
+
}) {
|
|
4162
|
+
const mode = parseCacheMode();
|
|
4163
|
+
const memoryCache = mode === "mixed" || mode === "memory" ? new LRUCache({
|
|
4164
|
+
max: parsePositiveIntegerEnv(
|
|
4165
|
+
memoryMaxEnvVar,
|
|
4166
|
+
DEFAULT_CACHE_MEMORY_MAX
|
|
4167
|
+
)
|
|
4168
|
+
}) : void 0;
|
|
4169
|
+
let diskCache;
|
|
4170
|
+
if (mode === "mixed" || mode === "disk") {
|
|
4171
|
+
if (canUseDiskCache()) {
|
|
4172
|
+
diskCache = new DiskCache({
|
|
4173
|
+
cacheDir: isomorph_default.getEnv(diskCacheDirEnvVar) ?? getDefaultDiskCacheDir(),
|
|
4174
|
+
max: parsePositiveIntegerEnv(diskMaxEnvVar, DEFAULT_CACHE_DISK_MAX)
|
|
4175
|
+
});
|
|
4176
|
+
} else if (mode === "disk") {
|
|
4177
|
+
warnUnavailableDiskCache();
|
|
4178
|
+
}
|
|
4179
|
+
}
|
|
4180
|
+
if (diskCache) {
|
|
4181
|
+
return { memoryCache, diskCache };
|
|
4182
|
+
}
|
|
4183
|
+
return { memoryCache };
|
|
4184
|
+
}
|
|
4185
|
+
|
|
4115
4186
|
// src/prompt-cache/prompt-cache.ts
|
|
4116
4187
|
function createCacheKey(key) {
|
|
4117
4188
|
if (key.id) {
|
|
@@ -4139,16 +4210,18 @@ var PromptCache = class {
|
|
|
4139
4210
|
*/
|
|
4140
4211
|
async get(key) {
|
|
4141
4212
|
const cacheKey = createCacheKey(key);
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4213
|
+
if (this.memoryCache) {
|
|
4214
|
+
const memoryPrompt = this.memoryCache.get(cacheKey);
|
|
4215
|
+
if (memoryPrompt !== void 0) {
|
|
4216
|
+
return memoryPrompt;
|
|
4217
|
+
}
|
|
4145
4218
|
}
|
|
4146
4219
|
if (this.diskCache) {
|
|
4147
4220
|
const diskPrompt = await this.diskCache.get(cacheKey);
|
|
4148
4221
|
if (!diskPrompt) {
|
|
4149
4222
|
return void 0;
|
|
4150
4223
|
}
|
|
4151
|
-
this.memoryCache
|
|
4224
|
+
this.memoryCache?.set(cacheKey, diskPrompt);
|
|
4152
4225
|
return diskPrompt;
|
|
4153
4226
|
}
|
|
4154
4227
|
return void 0;
|
|
@@ -4163,7 +4236,7 @@ var PromptCache = class {
|
|
|
4163
4236
|
*/
|
|
4164
4237
|
async set(key, value) {
|
|
4165
4238
|
const cacheKey = createCacheKey(key);
|
|
4166
|
-
this.memoryCache
|
|
4239
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4167
4240
|
if (this.diskCache) {
|
|
4168
4241
|
await this.diskCache.set(cacheKey, value);
|
|
4169
4242
|
}
|
|
@@ -4193,23 +4266,25 @@ var ParametersCache = class {
|
|
|
4193
4266
|
}
|
|
4194
4267
|
async get(key) {
|
|
4195
4268
|
const cacheKey = createCacheKey2(key);
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4269
|
+
if (this.memoryCache) {
|
|
4270
|
+
const memoryParams = this.memoryCache.get(cacheKey);
|
|
4271
|
+
if (memoryParams !== void 0) {
|
|
4272
|
+
return memoryParams;
|
|
4273
|
+
}
|
|
4199
4274
|
}
|
|
4200
4275
|
if (this.diskCache) {
|
|
4201
4276
|
const diskParams = await this.diskCache.get(cacheKey);
|
|
4202
4277
|
if (!diskParams) {
|
|
4203
4278
|
return void 0;
|
|
4204
4279
|
}
|
|
4205
|
-
this.memoryCache
|
|
4280
|
+
this.memoryCache?.set(cacheKey, diskParams);
|
|
4206
4281
|
return diskParams;
|
|
4207
4282
|
}
|
|
4208
4283
|
return void 0;
|
|
4209
4284
|
}
|
|
4210
4285
|
async set(key, value) {
|
|
4211
4286
|
const cacheKey = createCacheKey2(key);
|
|
4212
|
-
this.memoryCache
|
|
4287
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4213
4288
|
if (this.diskCache) {
|
|
4214
4289
|
await this.diskCache.set(cacheKey, value);
|
|
4215
4290
|
}
|
|
@@ -4741,21 +4816,22 @@ var BraintrustState = class _BraintrustState {
|
|
|
4741
4816
|
setGlobalDebugLogLevel(void 0);
|
|
4742
4817
|
}
|
|
4743
4818
|
this.resetLoginInfo();
|
|
4744
|
-
const memoryCache =
|
|
4745
|
-
|
|
4819
|
+
const { memoryCache, diskCache } = createCacheLayers({
|
|
4820
|
+
memoryMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_MEMORY_MAX",
|
|
4821
|
+
diskCacheDirEnvVar: "BRAINTRUST_PROMPT_CACHE_DIR",
|
|
4822
|
+
diskMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_DISK_MAX",
|
|
4823
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`
|
|
4746
4824
|
});
|
|
4747
|
-
const diskCache = canUseDiskCache() ? new DiskCache({
|
|
4748
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`,
|
|
4749
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4750
|
-
}) : void 0;
|
|
4751
4825
|
this.promptCache = new PromptCache({ memoryCache, diskCache });
|
|
4752
|
-
const
|
|
4753
|
-
|
|
4826
|
+
const {
|
|
4827
|
+
memoryCache: parametersMemoryCache,
|
|
4828
|
+
diskCache: parametersDiskCache
|
|
4829
|
+
} = createCacheLayers({
|
|
4830
|
+
memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
|
|
4831
|
+
diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
|
|
4832
|
+
diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
|
|
4833
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
|
|
4754
4834
|
});
|
|
4755
|
-
const parametersDiskCache = canUseDiskCache() ? new DiskCache({
|
|
4756
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`,
|
|
4757
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4758
|
-
}) : void 0;
|
|
4759
4835
|
this.parametersCache = new ParametersCache({
|
|
4760
4836
|
memoryCache: parametersMemoryCache,
|
|
4761
4837
|
diskCache: parametersDiskCache
|
|
@@ -25895,7 +25971,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25895
25971
|
this.config = config;
|
|
25896
25972
|
}
|
|
25897
25973
|
onEnable() {
|
|
25898
|
-
const integrations = this.config.integrations
|
|
25974
|
+
const integrations = this.config.integrations ?? {};
|
|
25899
25975
|
if (integrations.openai !== false) {
|
|
25900
25976
|
this.openaiPlugin = new OpenAIPlugin();
|
|
25901
25977
|
this.openaiPlugin.enable();
|
|
@@ -25960,7 +26036,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25960
26036
|
this.genkitPlugin = new GenkitPlugin();
|
|
25961
26037
|
this.genkitPlugin.enable();
|
|
25962
26038
|
}
|
|
25963
|
-
if (
|
|
26039
|
+
if (integrations.gitHubCopilot !== false) {
|
|
25964
26040
|
this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
|
|
25965
26041
|
this.gitHubCopilotPlugin.enable();
|
|
25966
26042
|
}
|
|
@@ -26073,6 +26149,7 @@ var envIntegrationAliases = {
|
|
|
26073
26149
|
cursorsdk: "cursorSDK",
|
|
26074
26150
|
flue: "flue",
|
|
26075
26151
|
"flue-runtime": "flue",
|
|
26152
|
+
mastra: "mastra",
|
|
26076
26153
|
"openai-agents": "openAIAgents",
|
|
26077
26154
|
openaiagents: "openAIAgents",
|
|
26078
26155
|
"openai-agents-core": "openAIAgents",
|
|
@@ -26115,6 +26192,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
26115
26192
|
cursor: true,
|
|
26116
26193
|
cursorSDK: true,
|
|
26117
26194
|
flue: true,
|
|
26195
|
+
mastra: true,
|
|
26118
26196
|
openAIAgents: true,
|
|
26119
26197
|
openrouter: true,
|
|
26120
26198
|
openrouterAgent: true,
|
|
@@ -26287,6 +26365,7 @@ __export(exports_exports, {
|
|
|
26287
26365
|
BaseExperiment: () => BaseExperiment,
|
|
26288
26366
|
BraintrustLangChainCallbackHandler: () => BraintrustLangChainCallbackHandler,
|
|
26289
26367
|
BraintrustMiddleware: () => BraintrustMiddleware,
|
|
26368
|
+
BraintrustObservabilityExporter: () => BraintrustObservabilityExporter,
|
|
26290
26369
|
BraintrustState: () => BraintrustState,
|
|
26291
26370
|
BraintrustStream: () => BraintrustStream,
|
|
26292
26371
|
CachedSpanFetcher: () => CachedSpanFetcher,
|
|
@@ -27903,6 +27982,196 @@ function toolRunnerProxy(toolRunner, anthropic, channel2) {
|
|
|
27903
27982
|
}
|
|
27904
27983
|
|
|
27905
27984
|
// src/wrappers/mastra.ts
|
|
27985
|
+
var MASTRA_BRAINTRUST_EXPORTER_NAME = "braintrust";
|
|
27986
|
+
var SPAN_TYPE_MAP = {
|
|
27987
|
+
agent_run: "task" /* TASK */,
|
|
27988
|
+
model_generation: "llm" /* LLM */,
|
|
27989
|
+
model_step: "llm" /* LLM */,
|
|
27990
|
+
model_chunk: "llm" /* LLM */,
|
|
27991
|
+
tool_call: "tool" /* TOOL */,
|
|
27992
|
+
mcp_tool_call: "tool" /* TOOL */,
|
|
27993
|
+
workflow_run: "task" /* TASK */,
|
|
27994
|
+
workflow_step: "function" /* FUNCTION */,
|
|
27995
|
+
workflow_conditional: "function" /* FUNCTION */,
|
|
27996
|
+
workflow_conditional_eval: "function" /* FUNCTION */,
|
|
27997
|
+
workflow_parallel: "function" /* FUNCTION */,
|
|
27998
|
+
workflow_loop: "function" /* FUNCTION */,
|
|
27999
|
+
workflow_sleep: "function" /* FUNCTION */,
|
|
28000
|
+
workflow_wait_event: "function" /* FUNCTION */,
|
|
28001
|
+
memory_operation: "function" /* FUNCTION */,
|
|
28002
|
+
workspace_action: "function" /* FUNCTION */,
|
|
28003
|
+
rag_ingestion: "task" /* TASK */,
|
|
28004
|
+
rag_embedding: "llm" /* LLM */,
|
|
28005
|
+
rag_vector_operation: "function" /* FUNCTION */,
|
|
28006
|
+
rag_action: "function" /* FUNCTION */,
|
|
28007
|
+
graph_action: "function" /* FUNCTION */,
|
|
28008
|
+
scorer_run: "score" /* SCORE */,
|
|
28009
|
+
scorer_step: "score" /* SCORE */,
|
|
28010
|
+
processor_run: "function" /* FUNCTION */,
|
|
28011
|
+
generic: "function" /* FUNCTION */
|
|
28012
|
+
};
|
|
28013
|
+
function spanTypeFor(mastraType) {
|
|
28014
|
+
return SPAN_TYPE_MAP[mastraType] ?? "function" /* FUNCTION */;
|
|
28015
|
+
}
|
|
28016
|
+
function epochSeconds(value) {
|
|
28017
|
+
if (value === void 0) return void 0;
|
|
28018
|
+
const ms = value instanceof Date ? value.getTime() : typeof value === "number" ? value : Date.parse(value);
|
|
28019
|
+
return Number.isFinite(ms) ? ms / 1e3 : void 0;
|
|
28020
|
+
}
|
|
28021
|
+
function modelMetrics(attributes) {
|
|
28022
|
+
if (!isObject(attributes)) return void 0;
|
|
28023
|
+
const usage = isObject(attributes.usage) ? attributes.usage : void 0;
|
|
28024
|
+
if (!usage) return void 0;
|
|
28025
|
+
const out = {};
|
|
28026
|
+
if (typeof usage.inputTokens === "number")
|
|
28027
|
+
out.prompt_tokens = usage.inputTokens;
|
|
28028
|
+
if (typeof usage.outputTokens === "number")
|
|
28029
|
+
out.completion_tokens = usage.outputTokens;
|
|
28030
|
+
if (typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
|
|
28031
|
+
out.tokens = usage.inputTokens + usage.outputTokens;
|
|
28032
|
+
}
|
|
28033
|
+
const inputDetails = isObject(usage.inputDetails) ? usage.inputDetails : void 0;
|
|
28034
|
+
const outputDetails = isObject(usage.outputDetails) ? usage.outputDetails : void 0;
|
|
28035
|
+
if (inputDetails && typeof inputDetails.cacheRead === "number") {
|
|
28036
|
+
out.prompt_cached_tokens = inputDetails.cacheRead;
|
|
28037
|
+
}
|
|
28038
|
+
if (inputDetails && typeof inputDetails.cacheWrite === "number") {
|
|
28039
|
+
out.prompt_cache_creation_tokens = inputDetails.cacheWrite;
|
|
28040
|
+
}
|
|
28041
|
+
if (outputDetails && typeof outputDetails.reasoning === "number") {
|
|
28042
|
+
out.completion_reasoning_tokens = outputDetails.reasoning;
|
|
28043
|
+
}
|
|
28044
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
28045
|
+
}
|
|
28046
|
+
function buildMetadata(exported) {
|
|
28047
|
+
const out = {};
|
|
28048
|
+
if (exported.entityId !== void 0) out.entity_id = exported.entityId;
|
|
28049
|
+
if (exported.entityName !== void 0) out.entity_name = exported.entityName;
|
|
28050
|
+
if (exported.entityType !== void 0) out.entity_type = exported.entityType;
|
|
28051
|
+
if (exported.metadata && isObject(exported.metadata)) {
|
|
28052
|
+
Object.assign(out, exported.metadata);
|
|
28053
|
+
}
|
|
28054
|
+
if (exported.attributes && isObject(exported.attributes)) {
|
|
28055
|
+
for (const [key, value] of Object.entries(exported.attributes)) {
|
|
28056
|
+
if (key === "usage") continue;
|
|
28057
|
+
if (value !== void 0) out[key] = value;
|
|
28058
|
+
}
|
|
28059
|
+
}
|
|
28060
|
+
if (exported.tags && exported.tags.length > 0) {
|
|
28061
|
+
out.tags = exported.tags;
|
|
28062
|
+
}
|
|
28063
|
+
if (exported.requestContext && isObject(exported.requestContext)) {
|
|
28064
|
+
out.request_context = exported.requestContext;
|
|
28065
|
+
}
|
|
28066
|
+
return out;
|
|
28067
|
+
}
|
|
28068
|
+
var BraintrustObservabilityExporter = class {
|
|
28069
|
+
name = MASTRA_BRAINTRUST_EXPORTER_NAME;
|
|
28070
|
+
spans = /* @__PURE__ */ new Map();
|
|
28071
|
+
// Captured at the first SPAN_STARTED event. Mastra's observability bus may
|
|
28072
|
+
// dispatch later events outside the user's AsyncLocalStorage context, where
|
|
28073
|
+
// `currentSpan()` returns NOOP_SPAN — which would make our `startSpan()`
|
|
28074
|
+
// calls go to a no-op logger and silently drop. Anchoring on the parent
|
|
28075
|
+
// we observe while still in-context keeps the whole Mastra subtree under
|
|
28076
|
+
// the user's traced scenario.
|
|
28077
|
+
capturedParent;
|
|
28078
|
+
constructor() {
|
|
28079
|
+
_internalSetInitialState();
|
|
28080
|
+
}
|
|
28081
|
+
async exportTracingEvent(event) {
|
|
28082
|
+
const exported = event.exportedSpan;
|
|
28083
|
+
if (exported.isInternal === true) return;
|
|
28084
|
+
try {
|
|
28085
|
+
switch (event.type) {
|
|
28086
|
+
case "span_started":
|
|
28087
|
+
this.onStart(exported);
|
|
28088
|
+
break;
|
|
28089
|
+
case "span_updated":
|
|
28090
|
+
this.onUpdate(exported);
|
|
28091
|
+
break;
|
|
28092
|
+
case "span_ended":
|
|
28093
|
+
this.onEnd(exported);
|
|
28094
|
+
break;
|
|
28095
|
+
}
|
|
28096
|
+
} catch (err) {
|
|
28097
|
+
logExporterError(err);
|
|
28098
|
+
}
|
|
28099
|
+
}
|
|
28100
|
+
async flush() {
|
|
28101
|
+
const state = _internalGetGlobalState();
|
|
28102
|
+
if (state) {
|
|
28103
|
+
await state.bgLogger().flush();
|
|
28104
|
+
}
|
|
28105
|
+
}
|
|
28106
|
+
async shutdown() {
|
|
28107
|
+
await this.flush();
|
|
28108
|
+
this.spans.clear();
|
|
28109
|
+
}
|
|
28110
|
+
onStart(exported) {
|
|
28111
|
+
if (this.spans.has(exported.id)) return;
|
|
28112
|
+
const args = {
|
|
28113
|
+
name: exported.name,
|
|
28114
|
+
spanAttributes: { type: spanTypeFor(exported.type) },
|
|
28115
|
+
startTime: epochSeconds(exported.startTime)
|
|
28116
|
+
};
|
|
28117
|
+
const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
|
|
28118
|
+
if (!this.capturedParent) {
|
|
28119
|
+
const probe = currentSpan();
|
|
28120
|
+
if (probe && probe.spanId) {
|
|
28121
|
+
this.capturedParent = probe;
|
|
28122
|
+
}
|
|
28123
|
+
}
|
|
28124
|
+
const span = parentRecord ? parentRecord.span.startSpan(args) : this.capturedParent ? this.capturedParent.startSpan(args) : startSpan(args);
|
|
28125
|
+
const record = { span, hasLoggedInput: false };
|
|
28126
|
+
this.logPayload(record, exported);
|
|
28127
|
+
this.spans.set(exported.id, record);
|
|
28128
|
+
if (exported.isEvent === true) {
|
|
28129
|
+
span.end({ endTime: args.startTime });
|
|
28130
|
+
this.spans.delete(exported.id);
|
|
28131
|
+
}
|
|
28132
|
+
}
|
|
28133
|
+
onUpdate(exported) {
|
|
28134
|
+
const record = this.spans.get(exported.id);
|
|
28135
|
+
if (!record) return;
|
|
28136
|
+
this.logPayload(record, exported);
|
|
28137
|
+
}
|
|
28138
|
+
onEnd(exported) {
|
|
28139
|
+
const record = this.spans.get(exported.id);
|
|
28140
|
+
if (!record) return;
|
|
28141
|
+
this.logPayload(record, exported);
|
|
28142
|
+
if (exported.errorInfo) {
|
|
28143
|
+
record.span.log({
|
|
28144
|
+
error: exported.errorInfo.message || exported.errorInfo.name || "Unknown Mastra error"
|
|
28145
|
+
});
|
|
28146
|
+
}
|
|
28147
|
+
record.span.end({ endTime: epochSeconds(exported.endTime) });
|
|
28148
|
+
this.spans.delete(exported.id);
|
|
28149
|
+
}
|
|
28150
|
+
logPayload(record, exported) {
|
|
28151
|
+
const event = {};
|
|
28152
|
+
if (exported.input !== void 0) {
|
|
28153
|
+
event.input = exported.input;
|
|
28154
|
+
record.hasLoggedInput = true;
|
|
28155
|
+
}
|
|
28156
|
+
if (exported.output !== void 0) {
|
|
28157
|
+
event.output = exported.output;
|
|
28158
|
+
}
|
|
28159
|
+
const metadata = buildMetadata(exported);
|
|
28160
|
+
if (Object.keys(metadata).length > 0) {
|
|
28161
|
+
event.metadata = metadata;
|
|
28162
|
+
}
|
|
28163
|
+
const metrics = modelMetrics(exported.attributes);
|
|
28164
|
+
if (metrics) {
|
|
28165
|
+
event.metrics = metrics;
|
|
28166
|
+
}
|
|
28167
|
+
if (Object.keys(event).length > 0) {
|
|
28168
|
+
record.span.log(event);
|
|
28169
|
+
}
|
|
28170
|
+
}
|
|
28171
|
+
};
|
|
28172
|
+
function logExporterError(err) {
|
|
28173
|
+
debugLogger.warn("Mastra exporter failure:", err);
|
|
28174
|
+
}
|
|
27906
28175
|
function wrapMastraAgent(agent, _options) {
|
|
27907
28176
|
return agent;
|
|
27908
28177
|
}
|