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/workerd.mjs
CHANGED
|
@@ -3935,6 +3935,76 @@ var LRUCache = class {
|
|
|
3935
3935
|
}
|
|
3936
3936
|
};
|
|
3937
3937
|
|
|
3938
|
+
// src/prompt-cache/cache-config.ts
|
|
3939
|
+
var CACHE_LOCATION_ENV_VAR = "BRAINTRUST_CACHE_LOCATION";
|
|
3940
|
+
var DEFAULT_CACHE_MEMORY_MAX = 1 << 10;
|
|
3941
|
+
var DEFAULT_CACHE_DISK_MAX = 1 << 20;
|
|
3942
|
+
var warnedInvalidCacheModeEnvValue = false;
|
|
3943
|
+
var warnedUnavailableDiskCacheMode = false;
|
|
3944
|
+
function warnInvalidCacheMode(value) {
|
|
3945
|
+
if (warnedInvalidCacheModeEnvValue) {
|
|
3946
|
+
return;
|
|
3947
|
+
}
|
|
3948
|
+
warnedInvalidCacheModeEnvValue = true;
|
|
3949
|
+
debugLogger.warn(
|
|
3950
|
+
`Invalid ${CACHE_LOCATION_ENV_VAR} value "${value}". Expected "mixed", "memory", "disk", or "none". Falling back to "mixed".`
|
|
3951
|
+
);
|
|
3952
|
+
}
|
|
3953
|
+
function warnUnavailableDiskCache() {
|
|
3954
|
+
if (warnedUnavailableDiskCacheMode) {
|
|
3955
|
+
return;
|
|
3956
|
+
}
|
|
3957
|
+
warnedUnavailableDiskCacheMode = true;
|
|
3958
|
+
debugLogger.warn(
|
|
3959
|
+
`Disk cache is not supported on this platform, so ${CACHE_LOCATION_ENV_VAR}="disk" disables prompt and parameters caching.`
|
|
3960
|
+
);
|
|
3961
|
+
}
|
|
3962
|
+
function parseCacheMode() {
|
|
3963
|
+
const value = isomorph_default.getEnv(CACHE_LOCATION_ENV_VAR);
|
|
3964
|
+
const normalized = value?.trim().toLowerCase();
|
|
3965
|
+
if (!normalized) {
|
|
3966
|
+
return "mixed";
|
|
3967
|
+
}
|
|
3968
|
+
if (normalized === "mixed" || normalized === "memory" || normalized === "disk" || normalized === "none") {
|
|
3969
|
+
return normalized;
|
|
3970
|
+
}
|
|
3971
|
+
warnInvalidCacheMode(value ?? "");
|
|
3972
|
+
return "mixed";
|
|
3973
|
+
}
|
|
3974
|
+
function parsePositiveIntegerEnv(envVar, defaultValue) {
|
|
3975
|
+
const value = Number(isomorph_default.getEnv(envVar));
|
|
3976
|
+
return Number.isInteger(value) && value > 0 ? value : defaultValue;
|
|
3977
|
+
}
|
|
3978
|
+
function createCacheLayers({
|
|
3979
|
+
memoryMaxEnvVar,
|
|
3980
|
+
diskCacheDirEnvVar,
|
|
3981
|
+
diskMaxEnvVar,
|
|
3982
|
+
getDefaultDiskCacheDir
|
|
3983
|
+
}) {
|
|
3984
|
+
const mode = parseCacheMode();
|
|
3985
|
+
const memoryCache = mode === "mixed" || mode === "memory" ? new LRUCache({
|
|
3986
|
+
max: parsePositiveIntegerEnv(
|
|
3987
|
+
memoryMaxEnvVar,
|
|
3988
|
+
DEFAULT_CACHE_MEMORY_MAX
|
|
3989
|
+
)
|
|
3990
|
+
}) : void 0;
|
|
3991
|
+
let diskCache;
|
|
3992
|
+
if (mode === "mixed" || mode === "disk") {
|
|
3993
|
+
if (canUseDiskCache()) {
|
|
3994
|
+
diskCache = new DiskCache({
|
|
3995
|
+
cacheDir: isomorph_default.getEnv(diskCacheDirEnvVar) ?? getDefaultDiskCacheDir(),
|
|
3996
|
+
max: parsePositiveIntegerEnv(diskMaxEnvVar, DEFAULT_CACHE_DISK_MAX)
|
|
3997
|
+
});
|
|
3998
|
+
} else if (mode === "disk") {
|
|
3999
|
+
warnUnavailableDiskCache();
|
|
4000
|
+
}
|
|
4001
|
+
}
|
|
4002
|
+
if (diskCache) {
|
|
4003
|
+
return { memoryCache, diskCache };
|
|
4004
|
+
}
|
|
4005
|
+
return { memoryCache };
|
|
4006
|
+
}
|
|
4007
|
+
|
|
3938
4008
|
// src/prompt-cache/prompt-cache.ts
|
|
3939
4009
|
function createCacheKey(key) {
|
|
3940
4010
|
if (key.id) {
|
|
@@ -3962,16 +4032,18 @@ var PromptCache = class {
|
|
|
3962
4032
|
*/
|
|
3963
4033
|
async get(key) {
|
|
3964
4034
|
const cacheKey = createCacheKey(key);
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
4035
|
+
if (this.memoryCache) {
|
|
4036
|
+
const memoryPrompt = this.memoryCache.get(cacheKey);
|
|
4037
|
+
if (memoryPrompt !== void 0) {
|
|
4038
|
+
return memoryPrompt;
|
|
4039
|
+
}
|
|
3968
4040
|
}
|
|
3969
4041
|
if (this.diskCache) {
|
|
3970
4042
|
const diskPrompt = await this.diskCache.get(cacheKey);
|
|
3971
4043
|
if (!diskPrompt) {
|
|
3972
4044
|
return void 0;
|
|
3973
4045
|
}
|
|
3974
|
-
this.memoryCache
|
|
4046
|
+
this.memoryCache?.set(cacheKey, diskPrompt);
|
|
3975
4047
|
return diskPrompt;
|
|
3976
4048
|
}
|
|
3977
4049
|
return void 0;
|
|
@@ -3986,7 +4058,7 @@ var PromptCache = class {
|
|
|
3986
4058
|
*/
|
|
3987
4059
|
async set(key, value) {
|
|
3988
4060
|
const cacheKey = createCacheKey(key);
|
|
3989
|
-
this.memoryCache
|
|
4061
|
+
this.memoryCache?.set(cacheKey, value);
|
|
3990
4062
|
if (this.diskCache) {
|
|
3991
4063
|
await this.diskCache.set(cacheKey, value);
|
|
3992
4064
|
}
|
|
@@ -4016,23 +4088,25 @@ var ParametersCache = class {
|
|
|
4016
4088
|
}
|
|
4017
4089
|
async get(key) {
|
|
4018
4090
|
const cacheKey = createCacheKey2(key);
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4091
|
+
if (this.memoryCache) {
|
|
4092
|
+
const memoryParams = this.memoryCache.get(cacheKey);
|
|
4093
|
+
if (memoryParams !== void 0) {
|
|
4094
|
+
return memoryParams;
|
|
4095
|
+
}
|
|
4022
4096
|
}
|
|
4023
4097
|
if (this.diskCache) {
|
|
4024
4098
|
const diskParams = await this.diskCache.get(cacheKey);
|
|
4025
4099
|
if (!diskParams) {
|
|
4026
4100
|
return void 0;
|
|
4027
4101
|
}
|
|
4028
|
-
this.memoryCache
|
|
4102
|
+
this.memoryCache?.set(cacheKey, diskParams);
|
|
4029
4103
|
return diskParams;
|
|
4030
4104
|
}
|
|
4031
4105
|
return void 0;
|
|
4032
4106
|
}
|
|
4033
4107
|
async set(key, value) {
|
|
4034
4108
|
const cacheKey = createCacheKey2(key);
|
|
4035
|
-
this.memoryCache
|
|
4109
|
+
this.memoryCache?.set(cacheKey, value);
|
|
4036
4110
|
if (this.diskCache) {
|
|
4037
4111
|
await this.diskCache.set(cacheKey, value);
|
|
4038
4112
|
}
|
|
@@ -4564,21 +4638,22 @@ var BraintrustState = class _BraintrustState {
|
|
|
4564
4638
|
setGlobalDebugLogLevel(void 0);
|
|
4565
4639
|
}
|
|
4566
4640
|
this.resetLoginInfo();
|
|
4567
|
-
const memoryCache =
|
|
4568
|
-
|
|
4641
|
+
const { memoryCache, diskCache } = createCacheLayers({
|
|
4642
|
+
memoryMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_MEMORY_MAX",
|
|
4643
|
+
diskCacheDirEnvVar: "BRAINTRUST_PROMPT_CACHE_DIR",
|
|
4644
|
+
diskMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_DISK_MAX",
|
|
4645
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`
|
|
4569
4646
|
});
|
|
4570
|
-
const diskCache = canUseDiskCache() ? new DiskCache({
|
|
4571
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`,
|
|
4572
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4573
|
-
}) : void 0;
|
|
4574
4647
|
this.promptCache = new PromptCache({ memoryCache, diskCache });
|
|
4575
|
-
const
|
|
4576
|
-
|
|
4648
|
+
const {
|
|
4649
|
+
memoryCache: parametersMemoryCache,
|
|
4650
|
+
diskCache: parametersDiskCache
|
|
4651
|
+
} = createCacheLayers({
|
|
4652
|
+
memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
|
|
4653
|
+
diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
|
|
4654
|
+
diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
|
|
4655
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
|
|
4577
4656
|
});
|
|
4578
|
-
const parametersDiskCache = canUseDiskCache() ? new DiskCache({
|
|
4579
|
-
cacheDir: isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`,
|
|
4580
|
-
max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")) ?? 1 << 20
|
|
4581
|
-
}) : void 0;
|
|
4582
4657
|
this.parametersCache = new ParametersCache({
|
|
4583
4658
|
memoryCache: parametersMemoryCache,
|
|
4584
4659
|
diskCache: parametersDiskCache
|
|
@@ -25718,7 +25793,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25718
25793
|
this.config = config;
|
|
25719
25794
|
}
|
|
25720
25795
|
onEnable() {
|
|
25721
|
-
const integrations = this.config.integrations
|
|
25796
|
+
const integrations = this.config.integrations ?? {};
|
|
25722
25797
|
if (integrations.openai !== false) {
|
|
25723
25798
|
this.openaiPlugin = new OpenAIPlugin();
|
|
25724
25799
|
this.openaiPlugin.enable();
|
|
@@ -25783,7 +25858,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
25783
25858
|
this.genkitPlugin = new GenkitPlugin();
|
|
25784
25859
|
this.genkitPlugin.enable();
|
|
25785
25860
|
}
|
|
25786
|
-
if (
|
|
25861
|
+
if (integrations.gitHubCopilot !== false) {
|
|
25787
25862
|
this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
|
|
25788
25863
|
this.gitHubCopilotPlugin.enable();
|
|
25789
25864
|
}
|
|
@@ -25896,6 +25971,7 @@ var envIntegrationAliases = {
|
|
|
25896
25971
|
cursorsdk: "cursorSDK",
|
|
25897
25972
|
flue: "flue",
|
|
25898
25973
|
"flue-runtime": "flue",
|
|
25974
|
+
mastra: "mastra",
|
|
25899
25975
|
"openai-agents": "openAIAgents",
|
|
25900
25976
|
openaiagents: "openAIAgents",
|
|
25901
25977
|
"openai-agents-core": "openAIAgents",
|
|
@@ -25938,6 +26014,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
25938
26014
|
cursor: true,
|
|
25939
26015
|
cursorSDK: true,
|
|
25940
26016
|
flue: true,
|
|
26017
|
+
mastra: true,
|
|
25941
26018
|
openAIAgents: true,
|
|
25942
26019
|
openrouter: true,
|
|
25943
26020
|
openrouterAgent: true,
|
|
@@ -26110,6 +26187,7 @@ __export(exports_exports, {
|
|
|
26110
26187
|
BaseExperiment: () => BaseExperiment,
|
|
26111
26188
|
BraintrustLangChainCallbackHandler: () => BraintrustLangChainCallbackHandler,
|
|
26112
26189
|
BraintrustMiddleware: () => BraintrustMiddleware,
|
|
26190
|
+
BraintrustObservabilityExporter: () => BraintrustObservabilityExporter,
|
|
26113
26191
|
BraintrustState: () => BraintrustState,
|
|
26114
26192
|
BraintrustStream: () => BraintrustStream,
|
|
26115
26193
|
CachedSpanFetcher: () => CachedSpanFetcher,
|
|
@@ -27726,6 +27804,196 @@ function toolRunnerProxy(toolRunner, anthropic, channel2) {
|
|
|
27726
27804
|
}
|
|
27727
27805
|
|
|
27728
27806
|
// src/wrappers/mastra.ts
|
|
27807
|
+
var MASTRA_BRAINTRUST_EXPORTER_NAME = "braintrust";
|
|
27808
|
+
var SPAN_TYPE_MAP = {
|
|
27809
|
+
agent_run: "task" /* TASK */,
|
|
27810
|
+
model_generation: "llm" /* LLM */,
|
|
27811
|
+
model_step: "llm" /* LLM */,
|
|
27812
|
+
model_chunk: "llm" /* LLM */,
|
|
27813
|
+
tool_call: "tool" /* TOOL */,
|
|
27814
|
+
mcp_tool_call: "tool" /* TOOL */,
|
|
27815
|
+
workflow_run: "task" /* TASK */,
|
|
27816
|
+
workflow_step: "function" /* FUNCTION */,
|
|
27817
|
+
workflow_conditional: "function" /* FUNCTION */,
|
|
27818
|
+
workflow_conditional_eval: "function" /* FUNCTION */,
|
|
27819
|
+
workflow_parallel: "function" /* FUNCTION */,
|
|
27820
|
+
workflow_loop: "function" /* FUNCTION */,
|
|
27821
|
+
workflow_sleep: "function" /* FUNCTION */,
|
|
27822
|
+
workflow_wait_event: "function" /* FUNCTION */,
|
|
27823
|
+
memory_operation: "function" /* FUNCTION */,
|
|
27824
|
+
workspace_action: "function" /* FUNCTION */,
|
|
27825
|
+
rag_ingestion: "task" /* TASK */,
|
|
27826
|
+
rag_embedding: "llm" /* LLM */,
|
|
27827
|
+
rag_vector_operation: "function" /* FUNCTION */,
|
|
27828
|
+
rag_action: "function" /* FUNCTION */,
|
|
27829
|
+
graph_action: "function" /* FUNCTION */,
|
|
27830
|
+
scorer_run: "score" /* SCORE */,
|
|
27831
|
+
scorer_step: "score" /* SCORE */,
|
|
27832
|
+
processor_run: "function" /* FUNCTION */,
|
|
27833
|
+
generic: "function" /* FUNCTION */
|
|
27834
|
+
};
|
|
27835
|
+
function spanTypeFor(mastraType) {
|
|
27836
|
+
return SPAN_TYPE_MAP[mastraType] ?? "function" /* FUNCTION */;
|
|
27837
|
+
}
|
|
27838
|
+
function epochSeconds(value) {
|
|
27839
|
+
if (value === void 0) return void 0;
|
|
27840
|
+
const ms = value instanceof Date ? value.getTime() : typeof value === "number" ? value : Date.parse(value);
|
|
27841
|
+
return Number.isFinite(ms) ? ms / 1e3 : void 0;
|
|
27842
|
+
}
|
|
27843
|
+
function modelMetrics(attributes) {
|
|
27844
|
+
if (!isObject(attributes)) return void 0;
|
|
27845
|
+
const usage = isObject(attributes.usage) ? attributes.usage : void 0;
|
|
27846
|
+
if (!usage) return void 0;
|
|
27847
|
+
const out = {};
|
|
27848
|
+
if (typeof usage.inputTokens === "number")
|
|
27849
|
+
out.prompt_tokens = usage.inputTokens;
|
|
27850
|
+
if (typeof usage.outputTokens === "number")
|
|
27851
|
+
out.completion_tokens = usage.outputTokens;
|
|
27852
|
+
if (typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
|
|
27853
|
+
out.tokens = usage.inputTokens + usage.outputTokens;
|
|
27854
|
+
}
|
|
27855
|
+
const inputDetails = isObject(usage.inputDetails) ? usage.inputDetails : void 0;
|
|
27856
|
+
const outputDetails = isObject(usage.outputDetails) ? usage.outputDetails : void 0;
|
|
27857
|
+
if (inputDetails && typeof inputDetails.cacheRead === "number") {
|
|
27858
|
+
out.prompt_cached_tokens = inputDetails.cacheRead;
|
|
27859
|
+
}
|
|
27860
|
+
if (inputDetails && typeof inputDetails.cacheWrite === "number") {
|
|
27861
|
+
out.prompt_cache_creation_tokens = inputDetails.cacheWrite;
|
|
27862
|
+
}
|
|
27863
|
+
if (outputDetails && typeof outputDetails.reasoning === "number") {
|
|
27864
|
+
out.completion_reasoning_tokens = outputDetails.reasoning;
|
|
27865
|
+
}
|
|
27866
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
27867
|
+
}
|
|
27868
|
+
function buildMetadata(exported) {
|
|
27869
|
+
const out = {};
|
|
27870
|
+
if (exported.entityId !== void 0) out.entity_id = exported.entityId;
|
|
27871
|
+
if (exported.entityName !== void 0) out.entity_name = exported.entityName;
|
|
27872
|
+
if (exported.entityType !== void 0) out.entity_type = exported.entityType;
|
|
27873
|
+
if (exported.metadata && isObject(exported.metadata)) {
|
|
27874
|
+
Object.assign(out, exported.metadata);
|
|
27875
|
+
}
|
|
27876
|
+
if (exported.attributes && isObject(exported.attributes)) {
|
|
27877
|
+
for (const [key, value] of Object.entries(exported.attributes)) {
|
|
27878
|
+
if (key === "usage") continue;
|
|
27879
|
+
if (value !== void 0) out[key] = value;
|
|
27880
|
+
}
|
|
27881
|
+
}
|
|
27882
|
+
if (exported.tags && exported.tags.length > 0) {
|
|
27883
|
+
out.tags = exported.tags;
|
|
27884
|
+
}
|
|
27885
|
+
if (exported.requestContext && isObject(exported.requestContext)) {
|
|
27886
|
+
out.request_context = exported.requestContext;
|
|
27887
|
+
}
|
|
27888
|
+
return out;
|
|
27889
|
+
}
|
|
27890
|
+
var BraintrustObservabilityExporter = class {
|
|
27891
|
+
name = MASTRA_BRAINTRUST_EXPORTER_NAME;
|
|
27892
|
+
spans = /* @__PURE__ */ new Map();
|
|
27893
|
+
// Captured at the first SPAN_STARTED event. Mastra's observability bus may
|
|
27894
|
+
// dispatch later events outside the user's AsyncLocalStorage context, where
|
|
27895
|
+
// `currentSpan()` returns NOOP_SPAN — which would make our `startSpan()`
|
|
27896
|
+
// calls go to a no-op logger and silently drop. Anchoring on the parent
|
|
27897
|
+
// we observe while still in-context keeps the whole Mastra subtree under
|
|
27898
|
+
// the user's traced scenario.
|
|
27899
|
+
capturedParent;
|
|
27900
|
+
constructor() {
|
|
27901
|
+
_internalSetInitialState();
|
|
27902
|
+
}
|
|
27903
|
+
async exportTracingEvent(event) {
|
|
27904
|
+
const exported = event.exportedSpan;
|
|
27905
|
+
if (exported.isInternal === true) return;
|
|
27906
|
+
try {
|
|
27907
|
+
switch (event.type) {
|
|
27908
|
+
case "span_started":
|
|
27909
|
+
this.onStart(exported);
|
|
27910
|
+
break;
|
|
27911
|
+
case "span_updated":
|
|
27912
|
+
this.onUpdate(exported);
|
|
27913
|
+
break;
|
|
27914
|
+
case "span_ended":
|
|
27915
|
+
this.onEnd(exported);
|
|
27916
|
+
break;
|
|
27917
|
+
}
|
|
27918
|
+
} catch (err) {
|
|
27919
|
+
logExporterError(err);
|
|
27920
|
+
}
|
|
27921
|
+
}
|
|
27922
|
+
async flush() {
|
|
27923
|
+
const state = _internalGetGlobalState();
|
|
27924
|
+
if (state) {
|
|
27925
|
+
await state.bgLogger().flush();
|
|
27926
|
+
}
|
|
27927
|
+
}
|
|
27928
|
+
async shutdown() {
|
|
27929
|
+
await this.flush();
|
|
27930
|
+
this.spans.clear();
|
|
27931
|
+
}
|
|
27932
|
+
onStart(exported) {
|
|
27933
|
+
if (this.spans.has(exported.id)) return;
|
|
27934
|
+
const args = {
|
|
27935
|
+
name: exported.name,
|
|
27936
|
+
spanAttributes: { type: spanTypeFor(exported.type) },
|
|
27937
|
+
startTime: epochSeconds(exported.startTime)
|
|
27938
|
+
};
|
|
27939
|
+
const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
|
|
27940
|
+
if (!this.capturedParent) {
|
|
27941
|
+
const probe = currentSpan();
|
|
27942
|
+
if (probe && probe.spanId) {
|
|
27943
|
+
this.capturedParent = probe;
|
|
27944
|
+
}
|
|
27945
|
+
}
|
|
27946
|
+
const span = parentRecord ? parentRecord.span.startSpan(args) : this.capturedParent ? this.capturedParent.startSpan(args) : startSpan(args);
|
|
27947
|
+
const record = { span, hasLoggedInput: false };
|
|
27948
|
+
this.logPayload(record, exported);
|
|
27949
|
+
this.spans.set(exported.id, record);
|
|
27950
|
+
if (exported.isEvent === true) {
|
|
27951
|
+
span.end({ endTime: args.startTime });
|
|
27952
|
+
this.spans.delete(exported.id);
|
|
27953
|
+
}
|
|
27954
|
+
}
|
|
27955
|
+
onUpdate(exported) {
|
|
27956
|
+
const record = this.spans.get(exported.id);
|
|
27957
|
+
if (!record) return;
|
|
27958
|
+
this.logPayload(record, exported);
|
|
27959
|
+
}
|
|
27960
|
+
onEnd(exported) {
|
|
27961
|
+
const record = this.spans.get(exported.id);
|
|
27962
|
+
if (!record) return;
|
|
27963
|
+
this.logPayload(record, exported);
|
|
27964
|
+
if (exported.errorInfo) {
|
|
27965
|
+
record.span.log({
|
|
27966
|
+
error: exported.errorInfo.message || exported.errorInfo.name || "Unknown Mastra error"
|
|
27967
|
+
});
|
|
27968
|
+
}
|
|
27969
|
+
record.span.end({ endTime: epochSeconds(exported.endTime) });
|
|
27970
|
+
this.spans.delete(exported.id);
|
|
27971
|
+
}
|
|
27972
|
+
logPayload(record, exported) {
|
|
27973
|
+
const event = {};
|
|
27974
|
+
if (exported.input !== void 0) {
|
|
27975
|
+
event.input = exported.input;
|
|
27976
|
+
record.hasLoggedInput = true;
|
|
27977
|
+
}
|
|
27978
|
+
if (exported.output !== void 0) {
|
|
27979
|
+
event.output = exported.output;
|
|
27980
|
+
}
|
|
27981
|
+
const metadata = buildMetadata(exported);
|
|
27982
|
+
if (Object.keys(metadata).length > 0) {
|
|
27983
|
+
event.metadata = metadata;
|
|
27984
|
+
}
|
|
27985
|
+
const metrics = modelMetrics(exported.attributes);
|
|
27986
|
+
if (metrics) {
|
|
27987
|
+
event.metrics = metrics;
|
|
27988
|
+
}
|
|
27989
|
+
if (Object.keys(event).length > 0) {
|
|
27990
|
+
record.span.log(event);
|
|
27991
|
+
}
|
|
27992
|
+
}
|
|
27993
|
+
};
|
|
27994
|
+
function logExporterError(err) {
|
|
27995
|
+
debugLogger.warn("Mastra exporter failure:", err);
|
|
27996
|
+
}
|
|
27729
27997
|
function wrapMastraAgent(agent, _options) {
|
|
27730
27998
|
return agent;
|
|
27731
27999
|
}
|
|
@@ -33224,6 +33492,7 @@ export {
|
|
|
33224
33492
|
BaseExperiment,
|
|
33225
33493
|
BraintrustLangChainCallbackHandler,
|
|
33226
33494
|
BraintrustMiddleware,
|
|
33495
|
+
BraintrustObservabilityExporter,
|
|
33227
33496
|
BraintrustState,
|
|
33228
33497
|
BraintrustStream,
|
|
33229
33498
|
CachedSpanFetcher,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "braintrust",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.0",
|
|
4
4
|
"description": "SDK for integrating Braintrust",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -162,17 +162,13 @@
|
|
|
162
162
|
"author": "",
|
|
163
163
|
"license": "MIT",
|
|
164
164
|
"devDependencies": {
|
|
165
|
-
"@ai-sdk/anthropic": "2.0.37",
|
|
166
165
|
"@anthropic-ai/sdk": "^0.60.0",
|
|
167
|
-
"@google/adk": "^0.6.1",
|
|
168
|
-
"@google/genai": "^1.25.0",
|
|
169
166
|
"@nodelib/fs.walk": "^1.2.8",
|
|
170
167
|
"@types/argparse": "^2.0.14",
|
|
171
168
|
"@types/async": "^3.2.24",
|
|
172
169
|
"@types/cli-progress": "^3.11.5",
|
|
173
170
|
"@types/cors": "^2.8.17",
|
|
174
171
|
"@types/express": "^5.0.0",
|
|
175
|
-
"@types/graceful-fs": "^4.1.9",
|
|
176
172
|
"@types/http-errors": "^2.0.4",
|
|
177
173
|
"@types/mustache": "^4.2.5",
|
|
178
174
|
"@types/node": "^20.10.5",
|
|
@@ -182,7 +178,6 @@
|
|
|
182
178
|
"@typescript-eslint/parser": "^8.49.0",
|
|
183
179
|
"ai": "^6.0.0",
|
|
184
180
|
"async": "^3.2.5",
|
|
185
|
-
"autoevals": "^0.0.131",
|
|
186
181
|
"cross-env": "^7.0.3",
|
|
187
182
|
"eslint-plugin-node-import": "^1.0.5",
|
|
188
183
|
"openai": "6.25.0",
|
|
@@ -214,7 +209,6 @@
|
|
|
214
209
|
"esbuild": "0.28.0",
|
|
215
210
|
"eventsource-parser": "^1.1.2",
|
|
216
211
|
"express": "^5.2.1",
|
|
217
|
-
"graceful-fs": "^4.2.11",
|
|
218
212
|
"http-errors": "^2.0.0",
|
|
219
213
|
"minimatch": "^10.2.5",
|
|
220
214
|
"module-details-from-path": "^1.0.4",
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
// src/auto-instrumentations/loader/get-package-version.ts
|
|
2
|
-
import { readFileSync, realpathSync } from "fs";
|
|
3
|
-
import { join } from "path";
|
|
4
|
-
var packageVersions = /* @__PURE__ */ new Map();
|
|
5
|
-
var packageNames = /* @__PURE__ */ new Map();
|
|
6
|
-
function readPackageJson(baseDir) {
|
|
7
|
-
try {
|
|
8
|
-
const packageJsonPath = join(baseDir, "package.json");
|
|
9
|
-
const jsonFile = readFileSync(packageJsonPath, "utf8");
|
|
10
|
-
return JSON.parse(jsonFile);
|
|
11
|
-
} catch {
|
|
12
|
-
return void 0;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function resolvePackageBaseDir(baseDir) {
|
|
16
|
-
try {
|
|
17
|
-
return realpathSync(baseDir);
|
|
18
|
-
} catch {
|
|
19
|
-
return baseDir;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
function readPackageJsonWithFallback(baseDir) {
|
|
23
|
-
const packageJson = readPackageJson(baseDir);
|
|
24
|
-
if (packageJson) {
|
|
25
|
-
return packageJson;
|
|
26
|
-
}
|
|
27
|
-
const resolvedBaseDir = resolvePackageBaseDir(baseDir);
|
|
28
|
-
if (resolvedBaseDir === baseDir) {
|
|
29
|
-
return void 0;
|
|
30
|
-
}
|
|
31
|
-
return readPackageJson(resolvedBaseDir);
|
|
32
|
-
}
|
|
33
|
-
function getPackageVersion(baseDir) {
|
|
34
|
-
if (packageVersions.has(baseDir)) {
|
|
35
|
-
return packageVersions.get(baseDir);
|
|
36
|
-
}
|
|
37
|
-
const packageJson = readPackageJsonWithFallback(baseDir);
|
|
38
|
-
if (typeof packageJson?.version === "string") {
|
|
39
|
-
packageVersions.set(baseDir, packageJson.version);
|
|
40
|
-
return packageJson.version;
|
|
41
|
-
}
|
|
42
|
-
return process.version.slice(1);
|
|
43
|
-
}
|
|
44
|
-
function getPackageName(baseDir) {
|
|
45
|
-
if (packageNames.has(baseDir)) {
|
|
46
|
-
return packageNames.get(baseDir);
|
|
47
|
-
}
|
|
48
|
-
const packageJson = readPackageJsonWithFallback(baseDir);
|
|
49
|
-
if (typeof packageJson?.name === "string") {
|
|
50
|
-
packageNames.set(baseDir, packageJson.name);
|
|
51
|
-
return packageJson.name;
|
|
52
|
-
}
|
|
53
|
-
return void 0;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// src/auto-instrumentations/loader/openai-api-promise-patch.ts
|
|
57
|
-
var OPENAI_API_PROMISE_PATCH = `
|
|
58
|
-
;(function __btPatchAPIPromise() {
|
|
59
|
-
if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
|
|
60
|
-
APIPromise.prototype.__btParsePatched = true;
|
|
61
|
-
var _origThen = APIPromise.prototype.then;
|
|
62
|
-
APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
|
|
63
|
-
if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
|
|
64
|
-
this.__btParseWrapped = true;
|
|
65
|
-
var _origParse = this.parseResponse;
|
|
66
|
-
var _cached;
|
|
67
|
-
this.parseResponse = function() {
|
|
68
|
-
if (!_cached) _cached = _origParse.apply(this, arguments);
|
|
69
|
-
return _cached;
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
return _origThen.call(this, onfulfilled, onrejected);
|
|
73
|
-
};
|
|
74
|
-
})();
|
|
75
|
-
`;
|
|
76
|
-
|
|
77
|
-
export {
|
|
78
|
-
getPackageVersion,
|
|
79
|
-
getPackageName,
|
|
80
|
-
OPENAI_API_PROMISE_PATCH
|
|
81
|
-
};
|