braintrust 3.12.0 → 3.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dev/dist/index.d.mts +31 -14
  2. package/dev/dist/index.d.ts +31 -14
  3. package/dev/dist/index.js +919 -485
  4. package/dev/dist/index.mjs +480 -46
  5. package/dist/apply-auto-instrumentation.js +204 -174
  6. package/dist/apply-auto-instrumentation.mjs +35 -5
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +226 -1
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +3 -2
  9. package/dist/auto-instrumentations/bundler/next.cjs +226 -1
  10. package/dist/auto-instrumentations/bundler/next.mjs +4 -3
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +226 -1
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +3 -2
  13. package/dist/auto-instrumentations/bundler/vite.cjs +226 -1
  14. package/dist/auto-instrumentations/bundler/vite.mjs +3 -2
  15. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +9 -0
  16. package/dist/auto-instrumentations/bundler/webpack.cjs +226 -1
  17. package/dist/auto-instrumentations/bundler/webpack.mjs +4 -3
  18. package/dist/auto-instrumentations/{chunk-2DPA74KK.mjs → chunk-E5DUYJWK.mjs} +1 -0
  19. package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +208 -0
  20. package/dist/auto-instrumentations/{chunk-AFXRW7I7.mjs → chunk-OTUQ7KH5.mjs} +1 -1
  21. package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +280 -0
  22. package/dist/auto-instrumentations/{chunk-73BZUKVI.mjs → chunk-XKAAVWT6.mjs} +24 -2
  23. package/dist/auto-instrumentations/hook.mjs +7981 -7
  24. package/dist/auto-instrumentations/index.cjs +1 -0
  25. package/dist/auto-instrumentations/index.mjs +1 -1
  26. package/dist/auto-instrumentations/loader/cjs-patch.cjs +194 -4
  27. package/dist/auto-instrumentations/loader/cjs-patch.mjs +13 -27
  28. package/dist/auto-instrumentations/loader/esm-hook.mjs +24 -10
  29. package/dist/browser.d.mts +274 -30
  30. package/dist/browser.d.ts +274 -30
  31. package/dist/browser.js +407 -48
  32. package/dist/browser.mjs +407 -48
  33. package/dist/{chunk-BW4DF4CY.js → chunk-NKD77KGB.js} +180 -1
  34. package/dist/{chunk-MSLBGITU.mjs → chunk-NU2GSPHX.mjs} +180 -1
  35. package/dist/cli.js +494 -94
  36. package/dist/edge-light.d.mts +1 -1
  37. package/dist/edge-light.d.ts +1 -1
  38. package/dist/edge-light.js +407 -48
  39. package/dist/edge-light.mjs +407 -48
  40. package/dist/index.d.mts +274 -30
  41. package/dist/index.d.ts +274 -30
  42. package/dist/index.js +1267 -857
  43. package/dist/index.mjs +465 -55
  44. package/dist/instrumentation/index.d.mts +47 -11
  45. package/dist/instrumentation/index.d.ts +47 -11
  46. package/dist/instrumentation/index.js +116 -26
  47. package/dist/instrumentation/index.mjs +116 -26
  48. package/dist/workerd.d.mts +1 -1
  49. package/dist/workerd.d.ts +1 -1
  50. package/dist/workerd.js +407 -48
  51. package/dist/workerd.mjs +407 -48
  52. package/package.json +3 -23
  53. package/util/dist/index.d.mts +3 -1
  54. package/util/dist/index.d.ts +3 -1
  55. 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,
@@ -47,6 +48,7 @@ __export(browser_exports, {
47
48
  DEFAULT_FETCH_BATCH_SIZE: () => DEFAULT_FETCH_BATCH_SIZE,
48
49
  DEFAULT_MAX_REQUEST_SIZE: () => DEFAULT_MAX_REQUEST_SIZE,
49
50
  Dataset: () => Dataset2,
51
+ DatasetPipeline: () => DatasetPipeline,
50
52
  ERR_PERMALINK: () => ERR_PERMALINK,
51
53
  Eval: () => Eval,
52
54
  EvalResultWithSummary: () => EvalResultWithSummary,
@@ -270,6 +272,7 @@ var iso = {
270
272
  getRepoInfo: async (_settings) => void 0,
271
273
  getPastNAncestors: async () => [],
272
274
  getEnv: (_name) => void 0,
275
+ getBraintrustApiKey: async () => void 0,
273
276
  getCallerLocation: () => void 0,
274
277
  newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
275
278
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -4113,6 +4116,76 @@ var LRUCache = class {
4113
4116
  }
4114
4117
  };
4115
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
+
4116
4189
  // src/prompt-cache/prompt-cache.ts
4117
4190
  function createCacheKey(key) {
4118
4191
  if (key.id) {
@@ -4140,16 +4213,18 @@ var PromptCache = class {
4140
4213
  */
4141
4214
  async get(key) {
4142
4215
  const cacheKey = createCacheKey(key);
4143
- const memoryPrompt = this.memoryCache.get(cacheKey);
4144
- if (memoryPrompt !== void 0) {
4145
- return memoryPrompt;
4216
+ if (this.memoryCache) {
4217
+ const memoryPrompt = this.memoryCache.get(cacheKey);
4218
+ if (memoryPrompt !== void 0) {
4219
+ return memoryPrompt;
4220
+ }
4146
4221
  }
4147
4222
  if (this.diskCache) {
4148
4223
  const diskPrompt = await this.diskCache.get(cacheKey);
4149
4224
  if (!diskPrompt) {
4150
4225
  return void 0;
4151
4226
  }
4152
- this.memoryCache.set(cacheKey, diskPrompt);
4227
+ this.memoryCache?.set(cacheKey, diskPrompt);
4153
4228
  return diskPrompt;
4154
4229
  }
4155
4230
  return void 0;
@@ -4164,7 +4239,7 @@ var PromptCache = class {
4164
4239
  */
4165
4240
  async set(key, value) {
4166
4241
  const cacheKey = createCacheKey(key);
4167
- this.memoryCache.set(cacheKey, value);
4242
+ this.memoryCache?.set(cacheKey, value);
4168
4243
  if (this.diskCache) {
4169
4244
  await this.diskCache.set(cacheKey, value);
4170
4245
  }
@@ -4194,23 +4269,25 @@ var ParametersCache = class {
4194
4269
  }
4195
4270
  async get(key) {
4196
4271
  const cacheKey = createCacheKey2(key);
4197
- const memoryParams = this.memoryCache.get(cacheKey);
4198
- if (memoryParams !== void 0) {
4199
- return memoryParams;
4272
+ if (this.memoryCache) {
4273
+ const memoryParams = this.memoryCache.get(cacheKey);
4274
+ if (memoryParams !== void 0) {
4275
+ return memoryParams;
4276
+ }
4200
4277
  }
4201
4278
  if (this.diskCache) {
4202
4279
  const diskParams = await this.diskCache.get(cacheKey);
4203
4280
  if (!diskParams) {
4204
4281
  return void 0;
4205
4282
  }
4206
- this.memoryCache.set(cacheKey, diskParams);
4283
+ this.memoryCache?.set(cacheKey, diskParams);
4207
4284
  return diskParams;
4208
4285
  }
4209
4286
  return void 0;
4210
4287
  }
4211
4288
  async set(key, value) {
4212
4289
  const cacheKey = createCacheKey2(key);
4213
- this.memoryCache.set(cacheKey, value);
4290
+ this.memoryCache?.set(cacheKey, value);
4214
4291
  if (this.diskCache) {
4215
4292
  await this.diskCache.set(cacheKey, value);
4216
4293
  }
@@ -4742,21 +4819,22 @@ var BraintrustState = class _BraintrustState {
4742
4819
  setGlobalDebugLogLevel(void 0);
4743
4820
  }
4744
4821
  this.resetLoginInfo();
4745
- const memoryCache = new LRUCache({
4746
- max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_MEMORY_MAX")) ?? 1 << 10
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`
4747
4827
  });
4748
- const diskCache = canUseDiskCache() ? new DiskCache({
4749
- cacheDir: isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`,
4750
- max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")) ?? 1 << 20
4751
- }) : void 0;
4752
4828
  this.promptCache = new PromptCache({ memoryCache, diskCache });
4753
- const parametersMemoryCache = new LRUCache({
4754
- max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX")) ?? 1 << 10
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`
4755
4837
  });
4756
- const parametersDiskCache = canUseDiskCache() ? new DiskCache({
4757
- cacheDir: isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`,
4758
- max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")) ?? 1 << 20
4759
- }) : void 0;
4760
4838
  this.parametersCache = new ParametersCache({
4761
4839
  memoryCache: parametersMemoryCache,
4762
4840
  diskCache: parametersDiskCache
@@ -5609,6 +5687,19 @@ var JSONAttachment = class extends Attachment {
5609
5687
  */
5610
5688
  constructor(data, options) {
5611
5689
  const { filename = "data.json", pretty = false, state } = options ?? {};
5690
+ const deferredJsonAttachment = globalThis.__BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__;
5691
+ if (deferredJsonAttachment) {
5692
+ super({
5693
+ data: new Blob([]),
5694
+ filename,
5695
+ contentType: "application/json",
5696
+ state
5697
+ });
5698
+ return deferredJsonAttachment(data, {
5699
+ filename,
5700
+ pretty
5701
+ });
5702
+ }
5612
5703
  const jsonString = pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
5613
5704
  const blob = new Blob([jsonString], { type: "application/json" });
5614
5705
  super({
@@ -7601,10 +7692,11 @@ async function login(options = {}) {
7601
7692
  async function loginToState(options = {}) {
7602
7693
  const {
7603
7694
  appUrl = isomorph_default.getEnv("BRAINTRUST_APP_URL") || "https://www.braintrust.dev",
7604
- apiKey = isomorph_default.getEnv("BRAINTRUST_API_KEY"),
7695
+ apiKey: apiKeyArg,
7605
7696
  orgName = isomorph_default.getEnv("BRAINTRUST_ORG_NAME"),
7606
7697
  fetch: fetch2 = globalThis.fetch
7607
7698
  } = options || {};
7699
+ const apiKey = apiKeyArg !== void 0 ? apiKeyArg : await isomorph_default.getBraintrustApiKey();
7608
7700
  const appPublicUrl = isomorph_default.getEnv("BRAINTRUST_APP_PUBLIC_URL") || appUrl;
7609
7701
  const state = new BraintrustState(options);
7610
7702
  state.resetLoginInfo();
@@ -8845,9 +8937,15 @@ var SpanImpl = class _SpanImpl {
8845
8937
  const cachedSpan = {
8846
8938
  input: partialRecord.input,
8847
8939
  output: partialRecord.output,
8940
+ expected: partialRecord.expected,
8941
+ error: partialRecord.error,
8942
+ scores: partialRecord.scores,
8943
+ metrics: partialRecord.metrics,
8848
8944
  metadata: partialRecord.metadata,
8945
+ tags: partialRecord.tags,
8849
8946
  span_id: this._spanId,
8850
8947
  span_parents: this._spanParents,
8948
+ is_root: this._spanId === this._rootSpanId,
8851
8949
  span_attributes: partialRecord.span_attributes
8852
8950
  };
8853
8951
  this._state.spanCache.queueWrite(
@@ -9183,6 +9281,7 @@ var Dataset2 = class extends ObjectFetcher {
9183
9281
  metadata,
9184
9282
  tags,
9185
9283
  output,
9284
+ origin,
9186
9285
  isMerge
9187
9286
  }) {
9188
9287
  return new LazyValue(async () => {
@@ -9197,6 +9296,7 @@ var Dataset2 = class extends ObjectFetcher {
9197
9296
  created: !isMerge ? (/* @__PURE__ */ new Date()).toISOString() : void 0,
9198
9297
  //if we're merging/updating an event we will not add this ts
9199
9298
  metadata,
9299
+ origin,
9200
9300
  ...!!isMerge ? {
9201
9301
  [IS_MERGE_FIELD]: true
9202
9302
  } : {}
@@ -9216,6 +9316,7 @@ var Dataset2 = class extends ObjectFetcher {
9216
9316
  * about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the
9217
9317
  * `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any
9218
9318
  * JSON-serializable type, but its keys must be strings.
9319
+ * @param event.origin (Optional) a reference to the source object this dataset record was derived from.
9219
9320
  * @param event.id (Optional) a unique identifier for the event. If you don't provide one, Braintrust will generate one for you.
9220
9321
  * @param event.output: (Deprecated) The output of your application. Use `expected` instead.
9221
9322
  * @returns The `id` of the logged record.
@@ -9226,7 +9327,8 @@ var Dataset2 = class extends ObjectFetcher {
9226
9327
  metadata,
9227
9328
  tags,
9228
9329
  id,
9229
- output
9330
+ output,
9331
+ origin
9230
9332
  }) {
9231
9333
  this.validateEvent({ metadata, expected, output, tags });
9232
9334
  const rowId = id || (0, import_uuid2.v4)();
@@ -9238,6 +9340,7 @@ var Dataset2 = class extends ObjectFetcher {
9238
9340
  metadata,
9239
9341
  tags,
9240
9342
  output,
9343
+ origin,
9241
9344
  isMerge: false
9242
9345
  })
9243
9346
  );
@@ -25748,7 +25851,7 @@ var BraintrustPlugin = class extends BasePlugin {
25748
25851
  this.config = config;
25749
25852
  }
25750
25853
  onEnable() {
25751
- const integrations = this.config.integrations || {};
25854
+ const integrations = this.config.integrations ?? {};
25752
25855
  if (integrations.openai !== false) {
25753
25856
  this.openaiPlugin = new OpenAIPlugin();
25754
25857
  this.openaiPlugin.enable();
@@ -25813,7 +25916,7 @@ var BraintrustPlugin = class extends BasePlugin {
25813
25916
  this.genkitPlugin = new GenkitPlugin();
25814
25917
  this.genkitPlugin.enable();
25815
25918
  }
25816
- if (getIntegrationConfig(integrations, "gitHubCopilot") !== false) {
25919
+ if (integrations.gitHubCopilot !== false) {
25817
25920
  this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
25818
25921
  this.gitHubCopilotPlugin.enable();
25819
25922
  }
@@ -25926,6 +26029,7 @@ var envIntegrationAliases = {
25926
26029
  cursorsdk: "cursorSDK",
25927
26030
  flue: "flue",
25928
26031
  "flue-runtime": "flue",
26032
+ mastra: "mastra",
25929
26033
  "openai-agents": "openAIAgents",
25930
26034
  openaiagents: "openAIAgents",
25931
26035
  "openai-agents-core": "openAIAgents",
@@ -25968,6 +26072,7 @@ function getDefaultInstrumentationIntegrations() {
25968
26072
  cursor: true,
25969
26073
  cursorSDK: true,
25970
26074
  flue: true,
26075
+ mastra: true,
25971
26076
  openAIAgents: true,
25972
26077
  openrouter: true,
25973
26078
  openrouterAgent: true,
@@ -26195,6 +26300,10 @@ function configureBrowser() {
26195
26300
  }
26196
26301
  return process.env[name];
26197
26302
  };
26303
+ isomorph_default.getBraintrustApiKey = async () => {
26304
+ const value = isomorph_default.getEnv("BRAINTRUST_API_KEY");
26305
+ return value?.trim() ? value : void 0;
26306
+ };
26198
26307
  isomorph_default.hash = (data) => {
26199
26308
  let hash = 0;
26200
26309
  for (let i = 0; i < data.length; i++) {
@@ -26221,6 +26330,7 @@ __export(exports_exports, {
26221
26330
  BaseExperiment: () => BaseExperiment,
26222
26331
  BraintrustLangChainCallbackHandler: () => BraintrustLangChainCallbackHandler,
26223
26332
  BraintrustMiddleware: () => BraintrustMiddleware,
26333
+ BraintrustObservabilityExporter: () => BraintrustObservabilityExporter,
26224
26334
  BraintrustState: () => BraintrustState,
26225
26335
  BraintrustStream: () => BraintrustStream,
26226
26336
  CachedSpanFetcher: () => CachedSpanFetcher,
@@ -26230,6 +26340,7 @@ __export(exports_exports, {
26230
26340
  DEFAULT_FETCH_BATCH_SIZE: () => DEFAULT_FETCH_BATCH_SIZE,
26231
26341
  DEFAULT_MAX_REQUEST_SIZE: () => DEFAULT_MAX_REQUEST_SIZE,
26232
26342
  Dataset: () => Dataset2,
26343
+ DatasetPipeline: () => DatasetPipeline,
26233
26344
  ERR_PERMALINK: () => ERR_PERMALINK,
26234
26345
  Eval: () => Eval,
26235
26346
  EvalResultWithSummary: () => EvalResultWithSummary,
@@ -27836,6 +27947,196 @@ function toolRunnerProxy(toolRunner, anthropic, channel2) {
27836
27947
  }
27837
27948
 
27838
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
+ }
27839
28140
  function wrapMastraAgent(agent, _options) {
27840
28141
  return agent;
27841
28142
  }
@@ -29488,10 +29789,12 @@ function formatExperimentSummary(summary) {
29488
29789
  // src/wrappers/shared/flush.ts
29489
29790
  async function summarizeAndFlush(experiment, options) {
29490
29791
  const shouldDisplay = options.displaySummary ?? true;
29491
- const summary = await experiment.summarize();
29492
- if (shouldDisplay) {
29493
- console.log(formatExperimentSummary(summary));
29792
+ if (!shouldDisplay) {
29793
+ await experiment.flush();
29794
+ return;
29494
29795
  }
29796
+ const summary = await experiment.summarize();
29797
+ console.log(formatExperimentSummary(summary));
29495
29798
  }
29496
29799
 
29497
29800
  // src/wrappers/vitest/flush-manager.ts
@@ -31355,8 +31658,12 @@ var waterfall$1 = awaitify(waterfall);
31355
31658
 
31356
31659
  // src/trace.ts
31357
31660
  var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
31358
- constructor(objectType, _objectId, rootSpanId, _state, spanTypeFilter) {
31359
- const filterExpr = _SpanFetcher.buildFilter(rootSpanId, spanTypeFilter);
31661
+ constructor(objectType, _objectId, rootSpanId, _state, spanTypeFilter, includeScorers = false) {
31662
+ const filterExpr = _SpanFetcher.buildFilter(
31663
+ rootSpanId,
31664
+ spanTypeFilter,
31665
+ includeScorers
31666
+ );
31360
31667
  super(objectType, void 0, void 0, {
31361
31668
  filter: filterExpr
31362
31669
  });
@@ -31365,16 +31672,17 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
31365
31672
  this._state = _state;
31366
31673
  this.spanTypeFilter = spanTypeFilter;
31367
31674
  }
31368
- static buildFilter(rootSpanId, spanTypeFilter) {
31675
+ static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
31369
31676
  const children = [
31370
31677
  // Base filter: root_span_id = 'value'
31371
31678
  {
31372
31679
  op: "eq",
31373
31680
  left: { op: "ident", name: ["root_span_id"] },
31374
31681
  right: { op: "literal", value: rootSpanId }
31375
- },
31376
- // Exclude span_attributes.purpose = 'score'
31377
- {
31682
+ }
31683
+ ];
31684
+ if (!includeScorers) {
31685
+ children.push({
31378
31686
  op: "or",
31379
31687
  children: [
31380
31688
  {
@@ -31387,8 +31695,8 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
31387
31695
  right: { op: "literal", value: "scorer" }
31388
31696
  }
31389
31697
  ]
31390
- }
31391
- ];
31698
+ });
31699
+ }
31392
31700
  if (spanTypeFilter && spanTypeFilter.length > 0) {
31393
31701
  children.push({
31394
31702
  op: "in",
@@ -31414,35 +31722,49 @@ var CachedSpanFetcher = class {
31414
31722
  fetchFn;
31415
31723
  constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {
31416
31724
  if (typeof objectTypeOrFetchFn === "function") {
31417
- this.fetchFn = objectTypeOrFetchFn;
31725
+ this.fetchFn = (spanType) => objectTypeOrFetchFn(spanType);
31418
31726
  } else {
31419
31727
  const objectType = objectTypeOrFetchFn;
31420
- this.fetchFn = async (spanType) => {
31728
+ this.fetchFn = async (spanType, includeScorers) => {
31421
31729
  const state = await getState();
31422
31730
  const fetcher = new SpanFetcher(
31423
31731
  objectType,
31424
31732
  objectId,
31425
31733
  rootSpanId,
31426
31734
  state,
31427
- spanType
31735
+ spanType,
31736
+ includeScorers
31428
31737
  );
31429
31738
  const rows = await fetcher.fetchedData();
31430
- return rows.filter((row) => row.span_attributes?.purpose !== "scorer").map((row) => ({
31739
+ return rows.map((row) => ({
31431
31740
  input: row.input,
31432
31741
  output: row.output,
31742
+ expected: row.expected,
31743
+ error: row.error,
31744
+ scores: row.scores,
31745
+ metrics: row.metrics,
31433
31746
  metadata: row.metadata,
31434
31747
  span_id: row.span_id,
31435
31748
  span_parents: row.span_parents,
31749
+ is_root: row.is_root,
31436
31750
  span_attributes: row.span_attributes,
31437
31751
  id: row.id,
31438
31752
  _xact_id: row._xact_id,
31439
31753
  _pagination_key: row._pagination_key,
31440
- root_span_id: row.root_span_id
31754
+ root_span_id: row.root_span_id,
31755
+ created: row.created,
31756
+ tags: row.tags
31441
31757
  }));
31442
31758
  };
31443
31759
  }
31444
31760
  }
31445
- async getSpans({ spanType } = {}) {
31761
+ async getSpans({
31762
+ spanType,
31763
+ includeScorers = false
31764
+ } = {}) {
31765
+ if (includeScorers) {
31766
+ return this.fetchFn(spanType, true);
31767
+ }
31446
31768
  if (this.allFetched) {
31447
31769
  return this.getFromCache(spanType);
31448
31770
  }
@@ -31459,7 +31781,7 @@ var CachedSpanFetcher = class {
31459
31781
  return this.getFromCache(spanType);
31460
31782
  }
31461
31783
  async fetchSpans(spanType) {
31462
- const spans = await this.fetchFn(spanType);
31784
+ const spans = await this.fetchFn(spanType, false);
31463
31785
  for (const span of spans) {
31464
31786
  const type = span.span_attributes?.type ?? "";
31465
31787
  const existing = this.spanCache.get(type) ?? [];
@@ -31537,10 +31859,13 @@ var LocalTrace = class {
31537
31859
  * First checks the local span cache for recently logged spans, then falls
31538
31860
  * back to CachedSpanFetcher which handles BTQL fetching and caching.
31539
31861
  */
31540
- async getSpans({ spanType } = {}) {
31862
+ async getSpans({
31863
+ spanType,
31864
+ includeScorers = false
31865
+ } = {}) {
31541
31866
  const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
31542
31867
  if (cachedSpans && cachedSpans.length > 0) {
31543
- let spans = cachedSpans.filter(
31868
+ let spans = includeScorers ? cachedSpans : cachedSpans.filter(
31544
31869
  (span) => span.span_attributes?.purpose !== "scorer"
31545
31870
  );
31546
31871
  if (spanType && spanType.length > 0) {
@@ -31551,13 +31876,19 @@ var LocalTrace = class {
31551
31876
  return spans.map((span) => ({
31552
31877
  input: span.input,
31553
31878
  output: span.output,
31879
+ expected: span.expected,
31880
+ error: span.error,
31881
+ scores: span.scores,
31882
+ metrics: span.metrics,
31554
31883
  metadata: span.metadata,
31555
31884
  span_id: span.span_id,
31556
31885
  span_parents: span.span_parents,
31557
- span_attributes: span.span_attributes
31886
+ is_root: span.is_root,
31887
+ span_attributes: span.span_attributes,
31888
+ tags: span.tags
31558
31889
  }));
31559
31890
  }
31560
- return this.cachedFetcher.getSpans({ spanType });
31891
+ return this.cachedFetcher.getSpans({ spanType, includeScorers });
31561
31892
  }
31562
31893
  /**
31563
31894
  * Get the thread (preprocessed messages) for this trace.
@@ -32746,6 +33077,34 @@ var defaultReporter = {
32746
33077
  }
32747
33078
  };
32748
33079
 
33080
+ // src/dataset-pipeline.ts
33081
+ function DatasetPipeline(definition) {
33082
+ if (!globalThis.__braintrust_dataset_pipelines) {
33083
+ globalThis.__braintrust_dataset_pipelines = [];
33084
+ }
33085
+ const storedDefinition = {
33086
+ name: definition.name,
33087
+ source: {
33088
+ projectId: definition.source.projectId,
33089
+ projectName: definition.source.projectName,
33090
+ orgName: definition.source.orgName,
33091
+ filter: definition.source.filter,
33092
+ scope: definition.source.scope ?? "span"
33093
+ },
33094
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
33095
+ transform: definition.transform,
33096
+ target: {
33097
+ projectId: definition.target.projectId,
33098
+ projectName: definition.target.projectName,
33099
+ orgName: definition.target.orgName,
33100
+ datasetName: definition.target.datasetName,
33101
+ description: definition.target.description,
33102
+ metadata: definition.target.metadata
33103
+ }
33104
+ };
33105
+ globalThis.__braintrust_dataset_pipelines.push(storedDefinition);
33106
+ }
33107
+
32749
33108
  // src/framework2.ts
32750
33109
  var import_v312 = require("zod/v3");
32751
33110
  var currentFilename = typeof __filename !== "undefined" ? __filename : "unknown";