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/browser.js
CHANGED
|
@@ -38,6 +38,7 @@ __export(browser_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,
|
|
@@ -4115,6 +4116,76 @@ var LRUCache = class {
|
|
|
4115
4116
|
}
|
|
4116
4117
|
};
|
|
4117
4118
|
|
|
4119
|
+
// src/prompt-cache/cache-config.ts
|
|
4120
|
+
var CACHE_LOCATION_ENV_VAR = "BRAINTRUST_CACHE_LOCATION";
|
|
4121
|
+
var DEFAULT_CACHE_MEMORY_MAX = 1 << 10;
|
|
4122
|
+
var DEFAULT_CACHE_DISK_MAX = 1 << 20;
|
|
4123
|
+
var warnedInvalidCacheModeEnvValue = false;
|
|
4124
|
+
var warnedUnavailableDiskCacheMode = false;
|
|
4125
|
+
function warnInvalidCacheMode(value) {
|
|
4126
|
+
if (warnedInvalidCacheModeEnvValue) {
|
|
4127
|
+
return;
|
|
4128
|
+
}
|
|
4129
|
+
warnedInvalidCacheModeEnvValue = true;
|
|
4130
|
+
debugLogger.warn(
|
|
4131
|
+
`Invalid ${CACHE_LOCATION_ENV_VAR} value "${value}". Expected "mixed", "memory", "disk", or "none". Falling back to "mixed".`
|
|
4132
|
+
);
|
|
4133
|
+
}
|
|
4134
|
+
function warnUnavailableDiskCache() {
|
|
4135
|
+
if (warnedUnavailableDiskCacheMode) {
|
|
4136
|
+
return;
|
|
4137
|
+
}
|
|
4138
|
+
warnedUnavailableDiskCacheMode = true;
|
|
4139
|
+
debugLogger.warn(
|
|
4140
|
+
`Disk cache is not supported on this platform, so ${CACHE_LOCATION_ENV_VAR}="disk" disables prompt and parameters caching.`
|
|
4141
|
+
);
|
|
4142
|
+
}
|
|
4143
|
+
function parseCacheMode() {
|
|
4144
|
+
const value = isomorph_default.getEnv(CACHE_LOCATION_ENV_VAR);
|
|
4145
|
+
const normalized = value?.trim().toLowerCase();
|
|
4146
|
+
if (!normalized) {
|
|
4147
|
+
return "mixed";
|
|
4148
|
+
}
|
|
4149
|
+
if (normalized === "mixed" || normalized === "memory" || normalized === "disk" || normalized === "none") {
|
|
4150
|
+
return normalized;
|
|
4151
|
+
}
|
|
4152
|
+
warnInvalidCacheMode(value ?? "");
|
|
4153
|
+
return "mixed";
|
|
4154
|
+
}
|
|
4155
|
+
function parsePositiveIntegerEnv(envVar, defaultValue) {
|
|
4156
|
+
const value = Number(isomorph_default.getEnv(envVar));
|
|
4157
|
+
return Number.isInteger(value) && value > 0 ? value : defaultValue;
|
|
4158
|
+
}
|
|
4159
|
+
function createCacheLayers({
|
|
4160
|
+
memoryMaxEnvVar,
|
|
4161
|
+
diskCacheDirEnvVar,
|
|
4162
|
+
diskMaxEnvVar,
|
|
4163
|
+
getDefaultDiskCacheDir
|
|
4164
|
+
}) {
|
|
4165
|
+
const mode = parseCacheMode();
|
|
4166
|
+
const memoryCache = mode === "mixed" || mode === "memory" ? new LRUCache({
|
|
4167
|
+
max: parsePositiveIntegerEnv(
|
|
4168
|
+
memoryMaxEnvVar,
|
|
4169
|
+
DEFAULT_CACHE_MEMORY_MAX
|
|
4170
|
+
)
|
|
4171
|
+
}) : void 0;
|
|
4172
|
+
let diskCache;
|
|
4173
|
+
if (mode === "mixed" || mode === "disk") {
|
|
4174
|
+
if (canUseDiskCache()) {
|
|
4175
|
+
diskCache = new DiskCache({
|
|
4176
|
+
cacheDir: isomorph_default.getEnv(diskCacheDirEnvVar) ?? getDefaultDiskCacheDir(),
|
|
4177
|
+
max: parsePositiveIntegerEnv(diskMaxEnvVar, DEFAULT_CACHE_DISK_MAX)
|
|
4178
|
+
});
|
|
4179
|
+
} else if (mode === "disk") {
|
|
4180
|
+
warnUnavailableDiskCache();
|
|
4181
|
+
}
|
|
4182
|
+
}
|
|
4183
|
+
if (diskCache) {
|
|
4184
|
+
return { memoryCache, diskCache };
|
|
4185
|
+
}
|
|
4186
|
+
return { memoryCache };
|
|
4187
|
+
}
|
|
4188
|
+
|
|
4118
4189
|
// src/prompt-cache/prompt-cache.ts
|
|
4119
4190
|
function createCacheKey(key) {
|
|
4120
4191
|
if (key.id) {
|
|
@@ -4142,16 +4213,18 @@ var PromptCache = class {
|
|
|
4142
4213
|
*/
|
|
4143
4214
|
async get(key) {
|
|
4144
4215
|
const cacheKey = createCacheKey(key);
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4216
|
+
if (this.memoryCache) {
|
|
4217
|
+
const memoryPrompt = this.memoryCache.get(cacheKey);
|
|
4218
|
+
if (memoryPrompt !== void 0) {
|
|
4219
|
+
return memoryPrompt;
|
|
4220
|
+
}
|
|
4148
4221
|
}
|
|
4149
4222
|
if (this.diskCache) {
|
|
4150
4223
|
const diskPrompt = await this.diskCache.get(cacheKey);
|
|
4151
4224
|
if (!diskPrompt) {
|
|
4152
4225
|
return void 0;
|
|
4153
4226
|
}
|
|
4154
|
-
this.memoryCache
|
|
4227
|
+
this.memoryCache?.set(cacheKey, diskPrompt);
|
|
4155
4228
|
return diskPrompt;
|
|
4156
4229
|
}
|
|
4157
4230
|
return void 0;
|
|
@@ -4166,7 +4239,7 @@ var PromptCache = class {
|
|
|
4166
4239
|
*/
|
|
4167
4240
|
async set(key, value) {
|
|
4168
4241
|
const cacheKey = createCacheKey(key);
|
|
4169
|
-
this.memoryCache
|
|
4242
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4170
4243
|
if (this.diskCache) {
|
|
4171
4244
|
await this.diskCache.set(cacheKey, value);
|
|
4172
4245
|
}
|
|
@@ -4196,23 +4269,25 @@ var ParametersCache = class {
|
|
|
4196
4269
|
}
|
|
4197
4270
|
async get(key) {
|
|
4198
4271
|
const cacheKey = createCacheKey2(key);
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4272
|
+
if (this.memoryCache) {
|
|
4273
|
+
const memoryParams = this.memoryCache.get(cacheKey);
|
|
4274
|
+
if (memoryParams !== void 0) {
|
|
4275
|
+
return memoryParams;
|
|
4276
|
+
}
|
|
4202
4277
|
}
|
|
4203
4278
|
if (this.diskCache) {
|
|
4204
4279
|
const diskParams = await this.diskCache.get(cacheKey);
|
|
4205
4280
|
if (!diskParams) {
|
|
4206
4281
|
return void 0;
|
|
4207
4282
|
}
|
|
4208
|
-
this.memoryCache
|
|
4283
|
+
this.memoryCache?.set(cacheKey, diskParams);
|
|
4209
4284
|
return diskParams;
|
|
4210
4285
|
}
|
|
4211
4286
|
return void 0;
|
|
4212
4287
|
}
|
|
4213
4288
|
async set(key, value) {
|
|
4214
4289
|
const cacheKey = createCacheKey2(key);
|
|
4215
|
-
this.memoryCache
|
|
4290
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4216
4291
|
if (this.diskCache) {
|
|
4217
4292
|
await this.diskCache.set(cacheKey, value);
|
|
4218
4293
|
}
|
|
@@ -4744,21 +4819,22 @@ var BraintrustState = class _BraintrustState {
|
|
|
4744
4819
|
setGlobalDebugLogLevel(void 0);
|
|
4745
4820
|
}
|
|
4746
4821
|
this.resetLoginInfo();
|
|
4747
|
-
const memoryCache =
|
|
4748
|
-
|
|
4822
|
+
const { memoryCache, diskCache } = createCacheLayers({
|
|
4823
|
+
memoryMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_MEMORY_MAX",
|
|
4824
|
+
diskCacheDirEnvVar: "BRAINTRUST_PROMPT_CACHE_DIR",
|
|
4825
|
+
diskMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_DISK_MAX",
|
|
4826
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`
|
|
4749
4827
|
});
|
|
4750
|
-
const diskCache = canUseDiskCache() ? new DiskCache({
|
|
4751
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`,
|
|
4752
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4753
|
-
}) : void 0;
|
|
4754
4828
|
this.promptCache = new PromptCache({ memoryCache, diskCache });
|
|
4755
|
-
const
|
|
4756
|
-
|
|
4829
|
+
const {
|
|
4830
|
+
memoryCache: parametersMemoryCache,
|
|
4831
|
+
diskCache: parametersDiskCache
|
|
4832
|
+
} = createCacheLayers({
|
|
4833
|
+
memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
|
|
4834
|
+
diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
|
|
4835
|
+
diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
|
|
4836
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
|
|
4757
4837
|
});
|
|
4758
|
-
const parametersDiskCache = canUseDiskCache() ? new DiskCache({
|
|
4759
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`,
|
|
4760
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4761
|
-
}) : void 0;
|
|
4762
4838
|
this.parametersCache = new ParametersCache({
|
|
4763
4839
|
memoryCache: parametersMemoryCache,
|
|
4764
4840
|
diskCache: parametersDiskCache
|
|
@@ -25775,7 +25851,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25775
25851
|
this.config = config;
|
|
25776
25852
|
}
|
|
25777
25853
|
onEnable() {
|
|
25778
|
-
const integrations = this.config.integrations
|
|
25854
|
+
const integrations = this.config.integrations ?? {};
|
|
25779
25855
|
if (integrations.openai !== false) {
|
|
25780
25856
|
this.openaiPlugin = new OpenAIPlugin();
|
|
25781
25857
|
this.openaiPlugin.enable();
|
|
@@ -25840,7 +25916,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25840
25916
|
this.genkitPlugin = new GenkitPlugin();
|
|
25841
25917
|
this.genkitPlugin.enable();
|
|
25842
25918
|
}
|
|
25843
|
-
if (
|
|
25919
|
+
if (integrations.gitHubCopilot !== false) {
|
|
25844
25920
|
this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
|
|
25845
25921
|
this.gitHubCopilotPlugin.enable();
|
|
25846
25922
|
}
|
|
@@ -25953,6 +26029,7 @@ var envIntegrationAliases = {
|
|
|
25953
26029
|
cursorsdk: "cursorSDK",
|
|
25954
26030
|
flue: "flue",
|
|
25955
26031
|
"flue-runtime": "flue",
|
|
26032
|
+
mastra: "mastra",
|
|
25956
26033
|
"openai-agents": "openAIAgents",
|
|
25957
26034
|
openaiagents: "openAIAgents",
|
|
25958
26035
|
"openai-agents-core": "openAIAgents",
|
|
@@ -25995,6 +26072,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
25995
26072
|
cursor: true,
|
|
25996
26073
|
cursorSDK: true,
|
|
25997
26074
|
flue: true,
|
|
26075
|
+
mastra: true,
|
|
25998
26076
|
openAIAgents: true,
|
|
25999
26077
|
openrouter: true,
|
|
26000
26078
|
openrouterAgent: true,
|
|
@@ -26252,6 +26330,7 @@ __export(exports_exports, {
|
|
|
26252
26330
|
BaseExperiment: () => BaseExperiment,
|
|
26253
26331
|
BraintrustLangChainCallbackHandler: () => BraintrustLangChainCallbackHandler,
|
|
26254
26332
|
BraintrustMiddleware: () => BraintrustMiddleware,
|
|
26333
|
+
BraintrustObservabilityExporter: () => BraintrustObservabilityExporter,
|
|
26255
26334
|
BraintrustState: () => BraintrustState,
|
|
26256
26335
|
BraintrustStream: () => BraintrustStream,
|
|
26257
26336
|
CachedSpanFetcher: () => CachedSpanFetcher,
|
|
@@ -27868,6 +27947,196 @@ function toolRunnerProxy(toolRunner, anthropic, channel2) {
|
|
|
27868
27947
|
}
|
|
27869
27948
|
|
|
27870
27949
|
// src/wrappers/mastra.ts
|
|
27950
|
+
var MASTRA_BRAINTRUST_EXPORTER_NAME = "braintrust";
|
|
27951
|
+
var SPAN_TYPE_MAP = {
|
|
27952
|
+
agent_run: "task" /* TASK */,
|
|
27953
|
+
model_generation: "llm" /* LLM */,
|
|
27954
|
+
model_step: "llm" /* LLM */,
|
|
27955
|
+
model_chunk: "llm" /* LLM */,
|
|
27956
|
+
tool_call: "tool" /* TOOL */,
|
|
27957
|
+
mcp_tool_call: "tool" /* TOOL */,
|
|
27958
|
+
workflow_run: "task" /* TASK */,
|
|
27959
|
+
workflow_step: "function" /* FUNCTION */,
|
|
27960
|
+
workflow_conditional: "function" /* FUNCTION */,
|
|
27961
|
+
workflow_conditional_eval: "function" /* FUNCTION */,
|
|
27962
|
+
workflow_parallel: "function" /* FUNCTION */,
|
|
27963
|
+
workflow_loop: "function" /* FUNCTION */,
|
|
27964
|
+
workflow_sleep: "function" /* FUNCTION */,
|
|
27965
|
+
workflow_wait_event: "function" /* FUNCTION */,
|
|
27966
|
+
memory_operation: "function" /* FUNCTION */,
|
|
27967
|
+
workspace_action: "function" /* FUNCTION */,
|
|
27968
|
+
rag_ingestion: "task" /* TASK */,
|
|
27969
|
+
rag_embedding: "llm" /* LLM */,
|
|
27970
|
+
rag_vector_operation: "function" /* FUNCTION */,
|
|
27971
|
+
rag_action: "function" /* FUNCTION */,
|
|
27972
|
+
graph_action: "function" /* FUNCTION */,
|
|
27973
|
+
scorer_run: "score" /* SCORE */,
|
|
27974
|
+
scorer_step: "score" /* SCORE */,
|
|
27975
|
+
processor_run: "function" /* FUNCTION */,
|
|
27976
|
+
generic: "function" /* FUNCTION */
|
|
27977
|
+
};
|
|
27978
|
+
function spanTypeFor(mastraType) {
|
|
27979
|
+
return SPAN_TYPE_MAP[mastraType] ?? "function" /* FUNCTION */;
|
|
27980
|
+
}
|
|
27981
|
+
function epochSeconds(value) {
|
|
27982
|
+
if (value === void 0) return void 0;
|
|
27983
|
+
const ms = value instanceof Date ? value.getTime() : typeof value === "number" ? value : Date.parse(value);
|
|
27984
|
+
return Number.isFinite(ms) ? ms / 1e3 : void 0;
|
|
27985
|
+
}
|
|
27986
|
+
function modelMetrics(attributes) {
|
|
27987
|
+
if (!isObject(attributes)) return void 0;
|
|
27988
|
+
const usage = isObject(attributes.usage) ? attributes.usage : void 0;
|
|
27989
|
+
if (!usage) return void 0;
|
|
27990
|
+
const out = {};
|
|
27991
|
+
if (typeof usage.inputTokens === "number")
|
|
27992
|
+
out.prompt_tokens = usage.inputTokens;
|
|
27993
|
+
if (typeof usage.outputTokens === "number")
|
|
27994
|
+
out.completion_tokens = usage.outputTokens;
|
|
27995
|
+
if (typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
|
|
27996
|
+
out.tokens = usage.inputTokens + usage.outputTokens;
|
|
27997
|
+
}
|
|
27998
|
+
const inputDetails = isObject(usage.inputDetails) ? usage.inputDetails : void 0;
|
|
27999
|
+
const outputDetails = isObject(usage.outputDetails) ? usage.outputDetails : void 0;
|
|
28000
|
+
if (inputDetails && typeof inputDetails.cacheRead === "number") {
|
|
28001
|
+
out.prompt_cached_tokens = inputDetails.cacheRead;
|
|
28002
|
+
}
|
|
28003
|
+
if (inputDetails && typeof inputDetails.cacheWrite === "number") {
|
|
28004
|
+
out.prompt_cache_creation_tokens = inputDetails.cacheWrite;
|
|
28005
|
+
}
|
|
28006
|
+
if (outputDetails && typeof outputDetails.reasoning === "number") {
|
|
28007
|
+
out.completion_reasoning_tokens = outputDetails.reasoning;
|
|
28008
|
+
}
|
|
28009
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
28010
|
+
}
|
|
28011
|
+
function buildMetadata(exported) {
|
|
28012
|
+
const out = {};
|
|
28013
|
+
if (exported.entityId !== void 0) out.entity_id = exported.entityId;
|
|
28014
|
+
if (exported.entityName !== void 0) out.entity_name = exported.entityName;
|
|
28015
|
+
if (exported.entityType !== void 0) out.entity_type = exported.entityType;
|
|
28016
|
+
if (exported.metadata && isObject(exported.metadata)) {
|
|
28017
|
+
Object.assign(out, exported.metadata);
|
|
28018
|
+
}
|
|
28019
|
+
if (exported.attributes && isObject(exported.attributes)) {
|
|
28020
|
+
for (const [key, value] of Object.entries(exported.attributes)) {
|
|
28021
|
+
if (key === "usage") continue;
|
|
28022
|
+
if (value !== void 0) out[key] = value;
|
|
28023
|
+
}
|
|
28024
|
+
}
|
|
28025
|
+
if (exported.tags && exported.tags.length > 0) {
|
|
28026
|
+
out.tags = exported.tags;
|
|
28027
|
+
}
|
|
28028
|
+
if (exported.requestContext && isObject(exported.requestContext)) {
|
|
28029
|
+
out.request_context = exported.requestContext;
|
|
28030
|
+
}
|
|
28031
|
+
return out;
|
|
28032
|
+
}
|
|
28033
|
+
var BraintrustObservabilityExporter = class {
|
|
28034
|
+
name = MASTRA_BRAINTRUST_EXPORTER_NAME;
|
|
28035
|
+
spans = /* @__PURE__ */ new Map();
|
|
28036
|
+
// Captured at the first SPAN_STARTED event. Mastra's observability bus may
|
|
28037
|
+
// dispatch later events outside the user's AsyncLocalStorage context, where
|
|
28038
|
+
// `currentSpan()` returns NOOP_SPAN — which would make our `startSpan()`
|
|
28039
|
+
// calls go to a no-op logger and silently drop. Anchoring on the parent
|
|
28040
|
+
// we observe while still in-context keeps the whole Mastra subtree under
|
|
28041
|
+
// the user's traced scenario.
|
|
28042
|
+
capturedParent;
|
|
28043
|
+
constructor() {
|
|
28044
|
+
_internalSetInitialState();
|
|
28045
|
+
}
|
|
28046
|
+
async exportTracingEvent(event) {
|
|
28047
|
+
const exported = event.exportedSpan;
|
|
28048
|
+
if (exported.isInternal === true) return;
|
|
28049
|
+
try {
|
|
28050
|
+
switch (event.type) {
|
|
28051
|
+
case "span_started":
|
|
28052
|
+
this.onStart(exported);
|
|
28053
|
+
break;
|
|
28054
|
+
case "span_updated":
|
|
28055
|
+
this.onUpdate(exported);
|
|
28056
|
+
break;
|
|
28057
|
+
case "span_ended":
|
|
28058
|
+
this.onEnd(exported);
|
|
28059
|
+
break;
|
|
28060
|
+
}
|
|
28061
|
+
} catch (err) {
|
|
28062
|
+
logExporterError(err);
|
|
28063
|
+
}
|
|
28064
|
+
}
|
|
28065
|
+
async flush() {
|
|
28066
|
+
const state = _internalGetGlobalState();
|
|
28067
|
+
if (state) {
|
|
28068
|
+
await state.bgLogger().flush();
|
|
28069
|
+
}
|
|
28070
|
+
}
|
|
28071
|
+
async shutdown() {
|
|
28072
|
+
await this.flush();
|
|
28073
|
+
this.spans.clear();
|
|
28074
|
+
}
|
|
28075
|
+
onStart(exported) {
|
|
28076
|
+
if (this.spans.has(exported.id)) return;
|
|
28077
|
+
const args = {
|
|
28078
|
+
name: exported.name,
|
|
28079
|
+
spanAttributes: { type: spanTypeFor(exported.type) },
|
|
28080
|
+
startTime: epochSeconds(exported.startTime)
|
|
28081
|
+
};
|
|
28082
|
+
const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
|
|
28083
|
+
if (!this.capturedParent) {
|
|
28084
|
+
const probe = currentSpan();
|
|
28085
|
+
if (probe && probe.spanId) {
|
|
28086
|
+
this.capturedParent = probe;
|
|
28087
|
+
}
|
|
28088
|
+
}
|
|
28089
|
+
const span = parentRecord ? parentRecord.span.startSpan(args) : this.capturedParent ? this.capturedParent.startSpan(args) : startSpan(args);
|
|
28090
|
+
const record = { span, hasLoggedInput: false };
|
|
28091
|
+
this.logPayload(record, exported);
|
|
28092
|
+
this.spans.set(exported.id, record);
|
|
28093
|
+
if (exported.isEvent === true) {
|
|
28094
|
+
span.end({ endTime: args.startTime });
|
|
28095
|
+
this.spans.delete(exported.id);
|
|
28096
|
+
}
|
|
28097
|
+
}
|
|
28098
|
+
onUpdate(exported) {
|
|
28099
|
+
const record = this.spans.get(exported.id);
|
|
28100
|
+
if (!record) return;
|
|
28101
|
+
this.logPayload(record, exported);
|
|
28102
|
+
}
|
|
28103
|
+
onEnd(exported) {
|
|
28104
|
+
const record = this.spans.get(exported.id);
|
|
28105
|
+
if (!record) return;
|
|
28106
|
+
this.logPayload(record, exported);
|
|
28107
|
+
if (exported.errorInfo) {
|
|
28108
|
+
record.span.log({
|
|
28109
|
+
error: exported.errorInfo.message || exported.errorInfo.name || "Unknown Mastra error"
|
|
28110
|
+
});
|
|
28111
|
+
}
|
|
28112
|
+
record.span.end({ endTime: epochSeconds(exported.endTime) });
|
|
28113
|
+
this.spans.delete(exported.id);
|
|
28114
|
+
}
|
|
28115
|
+
logPayload(record, exported) {
|
|
28116
|
+
const event = {};
|
|
28117
|
+
if (exported.input !== void 0) {
|
|
28118
|
+
event.input = exported.input;
|
|
28119
|
+
record.hasLoggedInput = true;
|
|
28120
|
+
}
|
|
28121
|
+
if (exported.output !== void 0) {
|
|
28122
|
+
event.output = exported.output;
|
|
28123
|
+
}
|
|
28124
|
+
const metadata = buildMetadata(exported);
|
|
28125
|
+
if (Object.keys(metadata).length > 0) {
|
|
28126
|
+
event.metadata = metadata;
|
|
28127
|
+
}
|
|
28128
|
+
const metrics = modelMetrics(exported.attributes);
|
|
28129
|
+
if (metrics) {
|
|
28130
|
+
event.metrics = metrics;
|
|
28131
|
+
}
|
|
28132
|
+
if (Object.keys(event).length > 0) {
|
|
28133
|
+
record.span.log(event);
|
|
28134
|
+
}
|
|
28135
|
+
}
|
|
28136
|
+
};
|
|
28137
|
+
function logExporterError(err) {
|
|
28138
|
+
debugLogger.warn("Mastra exporter failure:", err);
|
|
28139
|
+
}
|
|
27871
28140
|
function wrapMastraAgent(agent, _options) {
|
|
27872
28141
|
return agent;
|
|
27873
28142
|
}
|