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
@@ -3487,6 +3487,26 @@ declare const GitMetadataSettings: z.ZodObject<{
3487
3487
  fields?: ("dirty" | "tag" | "commit" | "branch" | "author_name" | "author_email" | "commit_message" | "commit_time" | "git_diff")[] | undefined;
3488
3488
  }>;
3489
3489
  type GitMetadataSettingsType = z.infer<typeof GitMetadataSettings>;
3490
+ declare const ObjectReference$1: z.ZodObject<{
3491
+ object_type: z.ZodEnum<["project_logs", "experiment", "dataset", "prompt", "function", "prompt_session"]>;
3492
+ object_id: z.ZodString;
3493
+ id: z.ZodString;
3494
+ _xact_id: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
3495
+ created: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
3496
+ }, "strip", z.ZodTypeAny, {
3497
+ id: string;
3498
+ object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
3499
+ object_id: string;
3500
+ created?: string | null | undefined;
3501
+ _xact_id?: string | null | undefined;
3502
+ }, {
3503
+ id: string;
3504
+ object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
3505
+ object_id: string;
3506
+ created?: string | null | undefined;
3507
+ _xact_id?: string | null | undefined;
3508
+ }>;
3509
+ type ObjectReferenceType$1 = z.infer<typeof ObjectReference$1>;
3490
3510
  declare const Prompt$1: z.ZodObject<{
3491
3511
  id: z.ZodString;
3492
3512
  _xact_id: z.ZodString;
@@ -6117,6 +6137,7 @@ interface InstrumentationIntegrationsConfig {
6117
6137
  cursor?: boolean;
6118
6138
  cursorSDK?: boolean;
6119
6139
  flue?: boolean;
6140
+ mastra?: boolean;
6120
6141
  openAIAgents?: boolean;
6121
6142
  openrouter?: boolean;
6122
6143
  openrouterAgent?: boolean;
@@ -6322,6 +6343,7 @@ declare const ObjectReference: z.ZodObject<{
6322
6343
  created?: string | null | undefined;
6323
6344
  _xact_id?: string | null | undefined;
6324
6345
  }>;
6346
+ type ObjectReferenceType = z.infer<typeof ObjectReference>;
6325
6347
 
6326
6348
  type IdField = {
6327
6349
  id: string;
@@ -6342,7 +6364,7 @@ type OtherExperimentLogFields = {
6342
6364
  metadata: Record<string, unknown>;
6343
6365
  metrics: Record<string, unknown>;
6344
6366
  datasetRecordId: string;
6345
- origin: z.infer<typeof ObjectReference>;
6367
+ origin: ObjectReferenceType;
6346
6368
  span_attributes: Record<string, unknown>;
6347
6369
  [ASYNC_SCORING_CONTROL_FIELD]: AsyncScoringControlType;
6348
6370
  [MERGE_PATHS_FIELD]: string[][];
@@ -6384,6 +6406,7 @@ type DatasetEvent = {
6384
6406
  tags?: string[];
6385
6407
  metadata?: unknown;
6386
6408
  created?: string;
6409
+ origin?: ObjectReferenceType;
6387
6410
  id: string;
6388
6411
  dataset_id: string;
6389
6412
  } & ({
@@ -6715,17 +6738,15 @@ interface PromptKey {
6715
6738
  id?: string;
6716
6739
  }
6717
6740
  /**
6718
- * A two-layer cache for Braintrust prompts with both in-memory and filesystem storage.
6741
+ * A configurable cache for Braintrust prompts with optional in-memory and filesystem storage.
6719
6742
  *
6720
- * This cache implements either a one or two-layer caching strategy:
6721
- * 1. A fast in-memory LRU cache for frequently accessed prompts.
6722
- * 2. An optional persistent filesystem-based cache that serves as a backing store.
6743
+ * This cache can use either layer independently, both layers together, or no layers.
6723
6744
  */
6724
6745
  declare class PromptCache {
6725
- private readonly memoryCache;
6746
+ private readonly memoryCache?;
6726
6747
  private readonly diskCache?;
6727
6748
  constructor(options: {
6728
- memoryCache: LRUCache<string, Prompt>;
6749
+ memoryCache?: LRUCache<string, Prompt>;
6729
6750
  diskCache?: DiskCache<Prompt>;
6730
6751
  });
6731
6752
  /**
@@ -6752,10 +6773,10 @@ interface ParametersKey {
6752
6773
  id?: string;
6753
6774
  }
6754
6775
  declare class ParametersCache {
6755
- private readonly memoryCache;
6776
+ private readonly memoryCache?;
6756
6777
  private readonly diskCache?;
6757
6778
  constructor(options: {
6758
- memoryCache: LRUCache<string, RemoteEvalParameters>;
6779
+ memoryCache?: LRUCache<string, RemoteEvalParameters>;
6759
6780
  diskCache?: DiskCache<RemoteEvalParameters>;
6760
6781
  });
6761
6782
  get(key: ParametersKey): Promise<RemoteEvalParameters | undefined>;
@@ -6775,9 +6796,15 @@ declare class ParametersCache {
6775
6796
  interface CachedSpan {
6776
6797
  input?: unknown;
6777
6798
  output?: unknown;
6799
+ expected?: unknown;
6800
+ error?: unknown;
6801
+ scores?: Record<string, unknown>;
6802
+ metrics?: Record<string, unknown>;
6778
6803
  metadata?: Record<string, unknown>;
6804
+ tags?: string[];
6779
6805
  span_id: string;
6780
6806
  span_parents?: string[];
6807
+ is_root?: boolean | null;
6781
6808
  span_attributes?: {
6782
6809
  name?: string;
6783
6810
  type?: string;
@@ -7310,6 +7337,13 @@ interface LogOptions<IsAsyncFlush> {
7310
7337
  linkArgs?: LinkArgs;
7311
7338
  }
7312
7339
  type PromiseUnless<B, R> = B extends true ? R : Promise<Awaited<R>>;
7340
+ type DatasetPipelineDeferredJSONAttachmentHook = (data: unknown, options?: {
7341
+ filename?: string;
7342
+ pretty?: boolean;
7343
+ }) => object;
7344
+ declare global {
7345
+ var __BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__: DatasetPipelineDeferredJSONAttachmentHook | undefined;
7346
+ }
7313
7347
  interface ParentSpanIds {
7314
7348
  spanId: string;
7315
7349
  rootSpanId: string;
@@ -7480,7 +7514,7 @@ interface LoginOptions {
7480
7514
  */
7481
7515
  appUrl?: string;
7482
7516
  /**
7483
- * The API key to use. If the parameter is not specified, will try to use the `BRAINTRUST_API_KEY` environment variable.
7517
+ * The API key to use. If the parameter is not specified, will try to use the `BRAINTRUST_API_KEY` environment variable. In Node.js, if that is unset, will try the nearest `.env.braintrust` file in the current working directory or parent directories.
7484
7518
  */
7485
7519
  apiKey?: string;
7486
7520
  /**
@@ -7719,17 +7753,19 @@ declare class Dataset<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY
7719
7753
  * about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the
7720
7754
  * `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any
7721
7755
  * JSON-serializable type, but its keys must be strings.
7756
+ * @param event.origin (Optional) a reference to the source object this dataset record was derived from.
7722
7757
  * @param event.id (Optional) a unique identifier for the event. If you don't provide one, Braintrust will generate one for you.
7723
7758
  * @param event.output: (Deprecated) The output of your application. Use `expected` instead.
7724
7759
  * @returns The `id` of the logged record.
7725
7760
  */
7726
- insert({ input, expected, metadata, tags, id, output, }: {
7761
+ insert({ input, expected, metadata, tags, id, output, origin, }: {
7727
7762
  readonly input?: unknown;
7728
7763
  readonly expected?: unknown;
7729
7764
  readonly tags?: string[];
7730
7765
  readonly metadata?: Record<string, unknown>;
7731
7766
  readonly id?: string;
7732
7767
  readonly output?: unknown;
7768
+ readonly origin?: ObjectReferenceType$1;
7733
7769
  }): string;
7734
7770
  /**
7735
7771
  * Update fields of a single record in the dataset. The updated fields will be batched and uploaded behind the scenes.
@@ -3487,6 +3487,26 @@ declare const GitMetadataSettings: z.ZodObject<{
3487
3487
  fields?: ("dirty" | "tag" | "commit" | "branch" | "author_name" | "author_email" | "commit_message" | "commit_time" | "git_diff")[] | undefined;
3488
3488
  }>;
3489
3489
  type GitMetadataSettingsType = z.infer<typeof GitMetadataSettings>;
3490
+ declare const ObjectReference$1: z.ZodObject<{
3491
+ object_type: z.ZodEnum<["project_logs", "experiment", "dataset", "prompt", "function", "prompt_session"]>;
3492
+ object_id: z.ZodString;
3493
+ id: z.ZodString;
3494
+ _xact_id: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
3495
+ created: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
3496
+ }, "strip", z.ZodTypeAny, {
3497
+ id: string;
3498
+ object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
3499
+ object_id: string;
3500
+ created?: string | null | undefined;
3501
+ _xact_id?: string | null | undefined;
3502
+ }, {
3503
+ id: string;
3504
+ object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
3505
+ object_id: string;
3506
+ created?: string | null | undefined;
3507
+ _xact_id?: string | null | undefined;
3508
+ }>;
3509
+ type ObjectReferenceType$1 = z.infer<typeof ObjectReference$1>;
3490
3510
  declare const Prompt$1: z.ZodObject<{
3491
3511
  id: z.ZodString;
3492
3512
  _xact_id: z.ZodString;
@@ -6117,6 +6137,7 @@ interface InstrumentationIntegrationsConfig {
6117
6137
  cursor?: boolean;
6118
6138
  cursorSDK?: boolean;
6119
6139
  flue?: boolean;
6140
+ mastra?: boolean;
6120
6141
  openAIAgents?: boolean;
6121
6142
  openrouter?: boolean;
6122
6143
  openrouterAgent?: boolean;
@@ -6322,6 +6343,7 @@ declare const ObjectReference: z.ZodObject<{
6322
6343
  created?: string | null | undefined;
6323
6344
  _xact_id?: string | null | undefined;
6324
6345
  }>;
6346
+ type ObjectReferenceType = z.infer<typeof ObjectReference>;
6325
6347
 
6326
6348
  type IdField = {
6327
6349
  id: string;
@@ -6342,7 +6364,7 @@ type OtherExperimentLogFields = {
6342
6364
  metadata: Record<string, unknown>;
6343
6365
  metrics: Record<string, unknown>;
6344
6366
  datasetRecordId: string;
6345
- origin: z.infer<typeof ObjectReference>;
6367
+ origin: ObjectReferenceType;
6346
6368
  span_attributes: Record<string, unknown>;
6347
6369
  [ASYNC_SCORING_CONTROL_FIELD]: AsyncScoringControlType;
6348
6370
  [MERGE_PATHS_FIELD]: string[][];
@@ -6384,6 +6406,7 @@ type DatasetEvent = {
6384
6406
  tags?: string[];
6385
6407
  metadata?: unknown;
6386
6408
  created?: string;
6409
+ origin?: ObjectReferenceType;
6387
6410
  id: string;
6388
6411
  dataset_id: string;
6389
6412
  } & ({
@@ -6715,17 +6738,15 @@ interface PromptKey {
6715
6738
  id?: string;
6716
6739
  }
6717
6740
  /**
6718
- * A two-layer cache for Braintrust prompts with both in-memory and filesystem storage.
6741
+ * A configurable cache for Braintrust prompts with optional in-memory and filesystem storage.
6719
6742
  *
6720
- * This cache implements either a one or two-layer caching strategy:
6721
- * 1. A fast in-memory LRU cache for frequently accessed prompts.
6722
- * 2. An optional persistent filesystem-based cache that serves as a backing store.
6743
+ * This cache can use either layer independently, both layers together, or no layers.
6723
6744
  */
6724
6745
  declare class PromptCache {
6725
- private readonly memoryCache;
6746
+ private readonly memoryCache?;
6726
6747
  private readonly diskCache?;
6727
6748
  constructor(options: {
6728
- memoryCache: LRUCache<string, Prompt>;
6749
+ memoryCache?: LRUCache<string, Prompt>;
6729
6750
  diskCache?: DiskCache<Prompt>;
6730
6751
  });
6731
6752
  /**
@@ -6752,10 +6773,10 @@ interface ParametersKey {
6752
6773
  id?: string;
6753
6774
  }
6754
6775
  declare class ParametersCache {
6755
- private readonly memoryCache;
6776
+ private readonly memoryCache?;
6756
6777
  private readonly diskCache?;
6757
6778
  constructor(options: {
6758
- memoryCache: LRUCache<string, RemoteEvalParameters>;
6779
+ memoryCache?: LRUCache<string, RemoteEvalParameters>;
6759
6780
  diskCache?: DiskCache<RemoteEvalParameters>;
6760
6781
  });
6761
6782
  get(key: ParametersKey): Promise<RemoteEvalParameters | undefined>;
@@ -6775,9 +6796,15 @@ declare class ParametersCache {
6775
6796
  interface CachedSpan {
6776
6797
  input?: unknown;
6777
6798
  output?: unknown;
6799
+ expected?: unknown;
6800
+ error?: unknown;
6801
+ scores?: Record<string, unknown>;
6802
+ metrics?: Record<string, unknown>;
6778
6803
  metadata?: Record<string, unknown>;
6804
+ tags?: string[];
6779
6805
  span_id: string;
6780
6806
  span_parents?: string[];
6807
+ is_root?: boolean | null;
6781
6808
  span_attributes?: {
6782
6809
  name?: string;
6783
6810
  type?: string;
@@ -7310,6 +7337,13 @@ interface LogOptions<IsAsyncFlush> {
7310
7337
  linkArgs?: LinkArgs;
7311
7338
  }
7312
7339
  type PromiseUnless<B, R> = B extends true ? R : Promise<Awaited<R>>;
7340
+ type DatasetPipelineDeferredJSONAttachmentHook = (data: unknown, options?: {
7341
+ filename?: string;
7342
+ pretty?: boolean;
7343
+ }) => object;
7344
+ declare global {
7345
+ var __BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__: DatasetPipelineDeferredJSONAttachmentHook | undefined;
7346
+ }
7313
7347
  interface ParentSpanIds {
7314
7348
  spanId: string;
7315
7349
  rootSpanId: string;
@@ -7480,7 +7514,7 @@ interface LoginOptions {
7480
7514
  */
7481
7515
  appUrl?: string;
7482
7516
  /**
7483
- * The API key to use. If the parameter is not specified, will try to use the `BRAINTRUST_API_KEY` environment variable.
7517
+ * The API key to use. If the parameter is not specified, will try to use the `BRAINTRUST_API_KEY` environment variable. In Node.js, if that is unset, will try the nearest `.env.braintrust` file in the current working directory or parent directories.
7484
7518
  */
7485
7519
  apiKey?: string;
7486
7520
  /**
@@ -7719,17 +7753,19 @@ declare class Dataset<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY
7719
7753
  * about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the
7720
7754
  * `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any
7721
7755
  * JSON-serializable type, but its keys must be strings.
7756
+ * @param event.origin (Optional) a reference to the source object this dataset record was derived from.
7722
7757
  * @param event.id (Optional) a unique identifier for the event. If you don't provide one, Braintrust will generate one for you.
7723
7758
  * @param event.output: (Deprecated) The output of your application. Use `expected` instead.
7724
7759
  * @returns The `id` of the logged record.
7725
7760
  */
7726
- insert({ input, expected, metadata, tags, id, output, }: {
7761
+ insert({ input, expected, metadata, tags, id, output, origin, }: {
7727
7762
  readonly input?: unknown;
7728
7763
  readonly expected?: unknown;
7729
7764
  readonly tags?: string[];
7730
7765
  readonly metadata?: Record<string, unknown>;
7731
7766
  readonly id?: string;
7732
7767
  readonly output?: unknown;
7768
+ readonly origin?: ObjectReferenceType$1;
7733
7769
  }): string;
7734
7770
  /**
7735
7771
  * Update fields of a single record in the dataset. The updated fields will be batched and uploaded behind the scenes.
@@ -125,6 +125,7 @@ var iso = {
125
125
  getRepoInfo: async (_settings) => void 0,
126
126
  getPastNAncestors: async () => [],
127
127
  getEnv: (_name) => void 0,
128
+ getBraintrustApiKey: async () => void 0,
128
129
  getCallerLocation: () => void 0,
129
130
  newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
130
131
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -4018,6 +4019,76 @@ var LRUCache = class {
4018
4019
  }
4019
4020
  };
4020
4021
 
4022
+ // src/prompt-cache/cache-config.ts
4023
+ var CACHE_LOCATION_ENV_VAR = "BRAINTRUST_CACHE_LOCATION";
4024
+ var DEFAULT_CACHE_MEMORY_MAX = 1 << 10;
4025
+ var DEFAULT_CACHE_DISK_MAX = 1 << 20;
4026
+ var warnedInvalidCacheModeEnvValue = false;
4027
+ var warnedUnavailableDiskCacheMode = false;
4028
+ function warnInvalidCacheMode(value) {
4029
+ if (warnedInvalidCacheModeEnvValue) {
4030
+ return;
4031
+ }
4032
+ warnedInvalidCacheModeEnvValue = true;
4033
+ debugLogger.warn(
4034
+ `Invalid ${CACHE_LOCATION_ENV_VAR} value "${value}". Expected "mixed", "memory", "disk", or "none". Falling back to "mixed".`
4035
+ );
4036
+ }
4037
+ function warnUnavailableDiskCache() {
4038
+ if (warnedUnavailableDiskCacheMode) {
4039
+ return;
4040
+ }
4041
+ warnedUnavailableDiskCacheMode = true;
4042
+ debugLogger.warn(
4043
+ `Disk cache is not supported on this platform, so ${CACHE_LOCATION_ENV_VAR}="disk" disables prompt and parameters caching.`
4044
+ );
4045
+ }
4046
+ function parseCacheMode() {
4047
+ const value = isomorph_default.getEnv(CACHE_LOCATION_ENV_VAR);
4048
+ const normalized = value?.trim().toLowerCase();
4049
+ if (!normalized) {
4050
+ return "mixed";
4051
+ }
4052
+ if (normalized === "mixed" || normalized === "memory" || normalized === "disk" || normalized === "none") {
4053
+ return normalized;
4054
+ }
4055
+ warnInvalidCacheMode(value ?? "");
4056
+ return "mixed";
4057
+ }
4058
+ function parsePositiveIntegerEnv(envVar, defaultValue) {
4059
+ const value = Number(isomorph_default.getEnv(envVar));
4060
+ return Number.isInteger(value) && value > 0 ? value : defaultValue;
4061
+ }
4062
+ function createCacheLayers({
4063
+ memoryMaxEnvVar,
4064
+ diskCacheDirEnvVar,
4065
+ diskMaxEnvVar,
4066
+ getDefaultDiskCacheDir
4067
+ }) {
4068
+ const mode = parseCacheMode();
4069
+ const memoryCache = mode === "mixed" || mode === "memory" ? new LRUCache({
4070
+ max: parsePositiveIntegerEnv(
4071
+ memoryMaxEnvVar,
4072
+ DEFAULT_CACHE_MEMORY_MAX
4073
+ )
4074
+ }) : void 0;
4075
+ let diskCache;
4076
+ if (mode === "mixed" || mode === "disk") {
4077
+ if (canUseDiskCache()) {
4078
+ diskCache = new DiskCache({
4079
+ cacheDir: isomorph_default.getEnv(diskCacheDirEnvVar) ?? getDefaultDiskCacheDir(),
4080
+ max: parsePositiveIntegerEnv(diskMaxEnvVar, DEFAULT_CACHE_DISK_MAX)
4081
+ });
4082
+ } else if (mode === "disk") {
4083
+ warnUnavailableDiskCache();
4084
+ }
4085
+ }
4086
+ if (diskCache) {
4087
+ return { memoryCache, diskCache };
4088
+ }
4089
+ return { memoryCache };
4090
+ }
4091
+
4021
4092
  // src/prompt-cache/prompt-cache.ts
4022
4093
  function createCacheKey(key) {
4023
4094
  if (key.id) {
@@ -4045,16 +4116,18 @@ var PromptCache = class {
4045
4116
  */
4046
4117
  async get(key) {
4047
4118
  const cacheKey = createCacheKey(key);
4048
- const memoryPrompt = this.memoryCache.get(cacheKey);
4049
- if (memoryPrompt !== void 0) {
4050
- return memoryPrompt;
4119
+ if (this.memoryCache) {
4120
+ const memoryPrompt = this.memoryCache.get(cacheKey);
4121
+ if (memoryPrompt !== void 0) {
4122
+ return memoryPrompt;
4123
+ }
4051
4124
  }
4052
4125
  if (this.diskCache) {
4053
4126
  const diskPrompt = await this.diskCache.get(cacheKey);
4054
4127
  if (!diskPrompt) {
4055
4128
  return void 0;
4056
4129
  }
4057
- this.memoryCache.set(cacheKey, diskPrompt);
4130
+ this.memoryCache?.set(cacheKey, diskPrompt);
4058
4131
  return diskPrompt;
4059
4132
  }
4060
4133
  return void 0;
@@ -4069,7 +4142,7 @@ var PromptCache = class {
4069
4142
  */
4070
4143
  async set(key, value) {
4071
4144
  const cacheKey = createCacheKey(key);
4072
- this.memoryCache.set(cacheKey, value);
4145
+ this.memoryCache?.set(cacheKey, value);
4073
4146
  if (this.diskCache) {
4074
4147
  await this.diskCache.set(cacheKey, value);
4075
4148
  }
@@ -4099,23 +4172,25 @@ var ParametersCache = class {
4099
4172
  }
4100
4173
  async get(key) {
4101
4174
  const cacheKey = createCacheKey2(key);
4102
- const memoryParams = this.memoryCache.get(cacheKey);
4103
- if (memoryParams !== void 0) {
4104
- return memoryParams;
4175
+ if (this.memoryCache) {
4176
+ const memoryParams = this.memoryCache.get(cacheKey);
4177
+ if (memoryParams !== void 0) {
4178
+ return memoryParams;
4179
+ }
4105
4180
  }
4106
4181
  if (this.diskCache) {
4107
4182
  const diskParams = await this.diskCache.get(cacheKey);
4108
4183
  if (!diskParams) {
4109
4184
  return void 0;
4110
4185
  }
4111
- this.memoryCache.set(cacheKey, diskParams);
4186
+ this.memoryCache?.set(cacheKey, diskParams);
4112
4187
  return diskParams;
4113
4188
  }
4114
4189
  return void 0;
4115
4190
  }
4116
4191
  async set(key, value) {
4117
4192
  const cacheKey = createCacheKey2(key);
4118
- this.memoryCache.set(cacheKey, value);
4193
+ this.memoryCache?.set(cacheKey, value);
4119
4194
  if (this.diskCache) {
4120
4195
  await this.diskCache.set(cacheKey, value);
4121
4196
  }
@@ -4647,21 +4722,22 @@ var BraintrustState = class _BraintrustState {
4647
4722
  setGlobalDebugLogLevel(void 0);
4648
4723
  }
4649
4724
  this.resetLoginInfo();
4650
- const memoryCache = new LRUCache({
4651
- max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_MEMORY_MAX")) ?? 1 << 10
4725
+ const { memoryCache, diskCache } = createCacheLayers({
4726
+ memoryMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_MEMORY_MAX",
4727
+ diskCacheDirEnvVar: "BRAINTRUST_PROMPT_CACHE_DIR",
4728
+ diskMaxEnvVar: "BRAINTRUST_PROMPT_CACHE_DISK_MAX",
4729
+ getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`
4652
4730
  });
4653
- const diskCache = canUseDiskCache() ? new DiskCache({
4654
- cacheDir: isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/prompt_cache`,
4655
- max: Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")) ?? 1 << 20
4656
- }) : void 0;
4657
4731
  this.promptCache = new PromptCache({ memoryCache, diskCache });
4658
- const parametersMemoryCache = new LRUCache({
4659
- max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX")) ?? 1 << 10
4732
+ const {
4733
+ memoryCache: parametersMemoryCache,
4734
+ diskCache: parametersDiskCache
4735
+ } = createCacheLayers({
4736
+ memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
4737
+ diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
4738
+ diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
4739
+ getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
4660
4740
  });
4661
- const parametersDiskCache = canUseDiskCache() ? new DiskCache({
4662
- cacheDir: isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR") ?? `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`,
4663
- max: Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")) ?? 1 << 20
4664
- }) : void 0;
4665
4741
  this.parametersCache = new ParametersCache({
4666
4742
  memoryCache: parametersMemoryCache,
4667
4743
  diskCache: parametersDiskCache
@@ -6548,10 +6624,11 @@ function initLogger(options = {}) {
6548
6624
  async function loginToState(options = {}) {
6549
6625
  const {
6550
6626
  appUrl = isomorph_default.getEnv("BRAINTRUST_APP_URL") || "https://www.braintrust.dev",
6551
- apiKey = isomorph_default.getEnv("BRAINTRUST_API_KEY"),
6627
+ apiKey: apiKeyArg,
6552
6628
  orgName = isomorph_default.getEnv("BRAINTRUST_ORG_NAME"),
6553
6629
  fetch: fetch2 = globalThis.fetch
6554
6630
  } = options || {};
6631
+ const apiKey = apiKeyArg !== void 0 ? apiKeyArg : await isomorph_default.getBraintrustApiKey();
6555
6632
  const appPublicUrl = isomorph_default.getEnv("BRAINTRUST_APP_PUBLIC_URL") || appUrl;
6556
6633
  const state = new BraintrustState(options);
6557
6634
  state.resetLoginInfo();
@@ -7482,9 +7559,15 @@ var SpanImpl = class _SpanImpl {
7482
7559
  const cachedSpan = {
7483
7560
  input: partialRecord.input,
7484
7561
  output: partialRecord.output,
7562
+ expected: partialRecord.expected,
7563
+ error: partialRecord.error,
7564
+ scores: partialRecord.scores,
7565
+ metrics: partialRecord.metrics,
7485
7566
  metadata: partialRecord.metadata,
7567
+ tags: partialRecord.tags,
7486
7568
  span_id: this._spanId,
7487
7569
  span_parents: this._spanParents,
7570
+ is_root: this._spanId === this._rootSpanId,
7488
7571
  span_attributes: partialRecord.span_attributes
7489
7572
  };
7490
7573
  this._state.spanCache.queueWrite(
@@ -7820,6 +7903,7 @@ var Dataset2 = class extends ObjectFetcher {
7820
7903
  metadata,
7821
7904
  tags,
7822
7905
  output,
7906
+ origin,
7823
7907
  isMerge
7824
7908
  }) {
7825
7909
  return new LazyValue(async () => {
@@ -7834,6 +7918,7 @@ var Dataset2 = class extends ObjectFetcher {
7834
7918
  created: !isMerge ? (/* @__PURE__ */ new Date()).toISOString() : void 0,
7835
7919
  //if we're merging/updating an event we will not add this ts
7836
7920
  metadata,
7921
+ origin,
7837
7922
  ...!!isMerge ? {
7838
7923
  [IS_MERGE_FIELD]: true
7839
7924
  } : {}
@@ -7853,6 +7938,7 @@ var Dataset2 = class extends ObjectFetcher {
7853
7938
  * about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the
7854
7939
  * `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any
7855
7940
  * JSON-serializable type, but its keys must be strings.
7941
+ * @param event.origin (Optional) a reference to the source object this dataset record was derived from.
7856
7942
  * @param event.id (Optional) a unique identifier for the event. If you don't provide one, Braintrust will generate one for you.
7857
7943
  * @param event.output: (Deprecated) The output of your application. Use `expected` instead.
7858
7944
  * @returns The `id` of the logged record.
@@ -7863,7 +7949,8 @@ var Dataset2 = class extends ObjectFetcher {
7863
7949
  metadata,
7864
7950
  tags,
7865
7951
  id,
7866
- output
7952
+ output,
7953
+ origin
7867
7954
  }) {
7868
7955
  this.validateEvent({ metadata, expected, output, tags });
7869
7956
  const rowId = id || (0, import_uuid2.v4)();
@@ -7875,6 +7962,7 @@ var Dataset2 = class extends ObjectFetcher {
7875
7962
  metadata,
7876
7963
  tags,
7877
7964
  output,
7965
+ origin,
7878
7966
  isMerge: false
7879
7967
  })
7880
7968
  );
@@ -23621,7 +23709,7 @@ var BraintrustPlugin = class extends BasePlugin {
23621
23709
  this.config = config;
23622
23710
  }
23623
23711
  onEnable() {
23624
- const integrations = this.config.integrations || {};
23712
+ const integrations = this.config.integrations ?? {};
23625
23713
  if (integrations.openai !== false) {
23626
23714
  this.openaiPlugin = new OpenAIPlugin();
23627
23715
  this.openaiPlugin.enable();
@@ -23686,7 +23774,7 @@ var BraintrustPlugin = class extends BasePlugin {
23686
23774
  this.genkitPlugin = new GenkitPlugin();
23687
23775
  this.genkitPlugin.enable();
23688
23776
  }
23689
- if (getIntegrationConfig(integrations, "gitHubCopilot") !== false) {
23777
+ if (integrations.gitHubCopilot !== false) {
23690
23778
  this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
23691
23779
  this.gitHubCopilotPlugin.enable();
23692
23780
  }
@@ -23799,6 +23887,7 @@ var envIntegrationAliases = {
23799
23887
  cursorsdk: "cursorSDK",
23800
23888
  flue: "flue",
23801
23889
  "flue-runtime": "flue",
23890
+ mastra: "mastra",
23802
23891
  "openai-agents": "openAIAgents",
23803
23892
  openaiagents: "openAIAgents",
23804
23893
  "openai-agents-core": "openAIAgents",
@@ -23841,6 +23930,7 @@ function getDefaultInstrumentationIntegrations() {
23841
23930
  cursor: true,
23842
23931
  cursorSDK: true,
23843
23932
  flue: true,
23933
+ mastra: true,
23844
23934
  openAIAgents: true,
23845
23935
  openrouter: true,
23846
23936
  openrouterAgent: true,