braintrust 3.28.0 → 3.29.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 (50) hide show
  1. package/README.md +1 -1
  2. package/dev/dist/index.d.mts +356 -7
  3. package/dev/dist/index.d.ts +356 -7
  4. package/dev/dist/index.js +1815 -1548
  5. package/dev/dist/index.mjs +1054 -787
  6. package/dist/apply-auto-instrumentation.js +297 -264
  7. package/dist/apply-auto-instrumentation.mjs +84 -51
  8. package/dist/auto-instrumentations/bundler/esbuild.cjs +43 -5
  9. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  10. package/dist/auto-instrumentations/bundler/next.cjs +43 -5
  11. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  12. package/dist/auto-instrumentations/bundler/rollup.cjs +43 -5
  13. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  14. package/dist/auto-instrumentations/bundler/vite.cjs +43 -5
  15. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  16. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +43 -5
  17. package/dist/auto-instrumentations/bundler/webpack.cjs +43 -5
  18. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  19. package/dist/auto-instrumentations/{chunk-NP7V4XB2.mjs → chunk-AOIYCVEL.mjs} +32 -2
  20. package/dist/auto-instrumentations/{chunk-26PKVUKB.mjs → chunk-DKTGDNA7.mjs} +12 -4
  21. package/dist/auto-instrumentations/{chunk-HD35AM3M.mjs → chunk-OMCZ3MV2.mjs} +1 -1
  22. package/dist/auto-instrumentations/hook.mjs +952 -133
  23. package/dist/auto-instrumentations/index.cjs +32 -2
  24. package/dist/auto-instrumentations/index.mjs +1 -1
  25. package/dist/browser.d.mts +760 -22
  26. package/dist/browser.d.ts +760 -22
  27. package/dist/browser.js +1661 -848
  28. package/dist/browser.mjs +1661 -848
  29. package/dist/{chunk-BBE7SNRV.js → chunk-6Z5S7VOU.js} +143 -23
  30. package/dist/{chunk-ZHUHZWFY.mjs → chunk-7FA6VP2S.mjs} +140 -20
  31. package/dist/{chunk-UPFNQCGB.mjs → chunk-M6XPNJC4.mjs} +1144 -931
  32. package/dist/{chunk-OBBWQW6K.js → chunk-XLLGRXGR.js} +2335 -2122
  33. package/dist/cli.js +5134 -1090
  34. package/dist/edge-light.js +1661 -848
  35. package/dist/edge-light.mjs +1661 -848
  36. package/dist/index.d.mts +760 -22
  37. package/dist/index.d.ts +760 -22
  38. package/dist/index.js +1054 -567
  39. package/dist/index.mjs +514 -27
  40. package/dist/instrumentation/index.d.mts +548 -9
  41. package/dist/instrumentation/index.d.ts +548 -9
  42. package/dist/instrumentation/index.js +1975 -765
  43. package/dist/instrumentation/index.mjs +1975 -765
  44. package/dist/vitest-evals-reporter.js +16 -16
  45. package/dist/vitest-evals-reporter.mjs +2 -2
  46. package/dist/workerd.js +1661 -848
  47. package/dist/workerd.mjs +1661 -848
  48. package/package.json +1 -1
  49. package/util/dist/index.d.mts +42 -0
  50. package/util/dist/index.d.ts +42 -0
@@ -659,8 +659,6 @@ function newGlobalTracingChannel(nameOrChannels) {
659
659
  var DefaultAsyncLocalStorage = class {
660
660
  constructor() {
661
661
  }
662
- enterWith(_) {
663
- }
664
662
  run(_, callback) {
665
663
  return callback();
666
664
  }
@@ -2477,7 +2475,9 @@ var AclObjectType = z6.union([
2477
2475
  "project_log",
2478
2476
  "org_project",
2479
2477
  "org_audit_logs",
2480
- "project_group"
2478
+ "project_group",
2479
+ "ai_secret",
2480
+ "org_ai_secret"
2481
2481
  ]),
2482
2482
  z6.null()
2483
2483
  ]);
@@ -3310,7 +3310,7 @@ var PromptParserNullish = z6.union([
3310
3310
  }),
3311
3311
  z6.null()
3312
3312
  ]);
3313
- var PreprocessorSavedFunctionId = z6.union([
3313
+ var PreprocessorId = z6.union([
3314
3314
  z6.object({
3315
3315
  type: z6.literal("function"),
3316
3316
  id: z6.string(),
@@ -3321,6 +3321,7 @@ var PreprocessorSavedFunctionId = z6.union([
3321
3321
  name: z6.string(),
3322
3322
  function_type: z6.literal("preprocessor").optional().default("preprocessor")
3323
3323
  }),
3324
+ z6.object({ type: z6.literal("inline"), code: z6.string().min(1) }),
3324
3325
  z6.null()
3325
3326
  ]);
3326
3327
  var PromptDataNullish = z6.union([
@@ -3328,7 +3329,7 @@ var PromptDataNullish = z6.union([
3328
3329
  prompt: PromptBlockDataNullish,
3329
3330
  options: PromptOptionsNullish,
3330
3331
  parser: PromptParserNullish,
3331
- preprocessor: PreprocessorSavedFunctionId,
3332
+ preprocessor: PreprocessorId,
3332
3333
  tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
3333
3334
  template_format: z6.union([
3334
3335
  z6.enum(["mustache", "nunjucks", "none"]),
@@ -3530,7 +3531,7 @@ var PromptData = z6.object({
3530
3531
  prompt: PromptBlockDataNullish,
3531
3532
  options: PromptOptionsNullish,
3532
3533
  parser: PromptParserNullish,
3533
- preprocessor: PreprocessorSavedFunctionId,
3534
+ preprocessor: PreprocessorId,
3534
3535
  tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
3535
3536
  template_format: z6.union([
3536
3537
  z6.enum(["mustache", "nunjucks", "none"]),
@@ -4474,6 +4475,7 @@ var View = z6.object({
4474
4475
  ]),
4475
4476
  name: z6.string(),
4476
4477
  description: z6.union([z6.string(), z6.null()]).optional(),
4478
+ starred: z6.boolean().optional(),
4477
4479
  created: z6.union([z6.string(), z6.null()]).optional(),
4478
4480
  updated_at: z6.union([z6.string(), z6.null()]).optional(),
4479
4481
  view_data: ViewData.optional(),
@@ -5177,44 +5179,72 @@ function createCacheLayers({
5177
5179
  }
5178
5180
 
5179
5181
  // src/prompt-cache/prompt-cache.ts
5180
- function createCacheKey(key) {
5182
+ function createCacheKey(key, namespace) {
5183
+ let cacheKey;
5181
5184
  if (key.id) {
5182
- return `id:${key.id}`;
5183
- }
5184
- const prefix = key.projectId ?? key.projectName;
5185
- if (!prefix) {
5186
- throw new Error("Either projectId or projectName must be provided");
5187
- }
5188
- if (!key.slug) {
5189
- throw new Error("Slug must be provided when not using ID");
5185
+ cacheKey = `id:${key.id}`;
5186
+ } else {
5187
+ const prefix = key.projectId ?? key.projectName;
5188
+ if (!prefix) {
5189
+ throw new Error("Either projectId or projectName must be provided");
5190
+ }
5191
+ if (!key.slug) {
5192
+ throw new Error("Slug must be provided when not using ID");
5193
+ }
5194
+ cacheKey = `${prefix}:${key.slug}:${key.version ?? "latest"}`;
5190
5195
  }
5191
- return `${prefix}:${key.slug}:${key.version ?? "latest"}`;
5196
+ return namespace === void 0 ? cacheKey : `${namespace.length}:${namespace}:${cacheKey}`;
5192
5197
  }
5193
- var PromptCache = class {
5198
+ var PromptCache = class _PromptCache {
5194
5199
  memoryCache;
5195
5200
  diskCache;
5201
+ namespace;
5202
+ expectedResolvedOrgIdentity;
5196
5203
  constructor(options) {
5197
5204
  this.memoryCache = options.memoryCache;
5198
5205
  this.diskCache = options.diskCache;
5206
+ this.namespace = options.namespace;
5207
+ this.expectedResolvedOrgIdentity = options.expectedResolvedOrgIdentity;
5208
+ }
5209
+ /**
5210
+ * Returns a cache view that shares the same storage layers but isolates all
5211
+ * entries under the provided namespace.
5212
+ */
5213
+ withNamespace(namespace, expectedResolvedOrgIdentity) {
5214
+ return new _PromptCache({
5215
+ memoryCache: this.memoryCache,
5216
+ diskCache: this.diskCache,
5217
+ namespace,
5218
+ expectedResolvedOrgIdentity
5219
+ });
5199
5220
  }
5200
5221
  /**
5201
5222
  * Retrieves a prompt from the cache.
5202
5223
  * First checks the in-memory LRU cache, then falls back to checking the disk cache if available.
5203
5224
  */
5204
5225
  async get(key) {
5205
- const cacheKey = createCacheKey(key);
5226
+ const cacheKey = createCacheKey(key, this.namespace);
5206
5227
  if (this.memoryCache) {
5207
- const memoryPrompt = this.memoryCache.get(cacheKey);
5208
- if (memoryPrompt !== void 0) {
5209
- return memoryPrompt;
5228
+ const memoryEntry = this.memoryCache.get(cacheKey);
5229
+ if (memoryEntry !== void 0 && (this.expectedResolvedOrgIdentity === void 0 || memoryEntry.resolvedOrgIdentity === this.expectedResolvedOrgIdentity)) {
5230
+ return memoryEntry.value;
5210
5231
  }
5211
5232
  }
5212
5233
  if (this.diskCache) {
5213
- const diskPrompt = await this.diskCache.get(cacheKey);
5214
- if (!diskPrompt) {
5234
+ const diskEntry = await this.diskCache.get(cacheKey);
5235
+ if (!diskEntry || this.expectedResolvedOrgIdentity !== void 0 && diskEntry.resolvedOrgIdentity !== this.expectedResolvedOrgIdentity) {
5215
5236
  return void 0;
5216
5237
  }
5217
- this.memoryCache?.set(cacheKey, diskPrompt);
5238
+ const serializedPrompt = diskEntry.value;
5239
+ const diskPrompt = new Prompt2(
5240
+ serializedPrompt.metadata,
5241
+ serializedPrompt.defaults,
5242
+ serializedPrompt.noTrace
5243
+ );
5244
+ this.memoryCache?.set(cacheKey, {
5245
+ value: diskPrompt,
5246
+ resolvedOrgIdentity: diskEntry.resolvedOrgIdentity
5247
+ });
5218
5248
  return diskPrompt;
5219
5249
  }
5220
5250
  return void 0;
@@ -5228,58 +5258,91 @@ var PromptCache = class {
5228
5258
  * @throws If there is an error writing to the disk cache.
5229
5259
  */
5230
5260
  async set(key, value) {
5231
- const cacheKey = createCacheKey(key);
5232
- this.memoryCache?.set(cacheKey, value);
5261
+ const cacheKey = createCacheKey(key, this.namespace);
5262
+ const memoryEntry = {
5263
+ value,
5264
+ resolvedOrgIdentity: this.expectedResolvedOrgIdentity
5265
+ };
5266
+ this.memoryCache?.set(cacheKey, memoryEntry);
5233
5267
  if (this.diskCache) {
5234
- await this.diskCache.set(cacheKey, value);
5268
+ await this.diskCache.set(cacheKey, {
5269
+ value: value._internalSerializeForCache(),
5270
+ resolvedOrgIdentity: this.expectedResolvedOrgIdentity
5271
+ });
5235
5272
  }
5236
5273
  }
5237
5274
  };
5238
5275
 
5239
5276
  // src/prompt-cache/parameters-cache.ts
5240
- function createCacheKey2(key) {
5277
+ function createCacheKey2(key, namespace) {
5278
+ let cacheKey;
5241
5279
  if (key.id) {
5242
- return `parameters:id:${key.id}`;
5243
- }
5244
- const prefix = key.projectId ?? key.projectName;
5245
- if (!prefix) {
5246
- throw new Error("Either projectId or projectName must be provided");
5247
- }
5248
- if (!key.slug) {
5249
- throw new Error("Slug must be provided when not using ID");
5280
+ cacheKey = `parameters:id:${key.id}`;
5281
+ } else {
5282
+ const prefix = key.projectId ?? key.projectName;
5283
+ if (!prefix) {
5284
+ throw new Error("Either projectId or projectName must be provided");
5285
+ }
5286
+ if (!key.slug) {
5287
+ throw new Error("Slug must be provided when not using ID");
5288
+ }
5289
+ cacheKey = `parameters:${prefix}:${key.slug}:${key.version ?? "latest"}`;
5250
5290
  }
5251
- return `parameters:${prefix}:${key.slug}:${key.version ?? "latest"}`;
5291
+ return namespace === void 0 ? cacheKey : `${namespace.length}:${namespace}:${cacheKey}`;
5252
5292
  }
5253
- var ParametersCache = class {
5293
+ var ParametersCache = class _ParametersCache {
5254
5294
  memoryCache;
5255
5295
  diskCache;
5296
+ namespace;
5297
+ expectedResolvedOrgIdentity;
5256
5298
  constructor(options) {
5257
5299
  this.memoryCache = options.memoryCache;
5258
5300
  this.diskCache = options.diskCache;
5301
+ this.namespace = options.namespace;
5302
+ this.expectedResolvedOrgIdentity = options.expectedResolvedOrgIdentity;
5303
+ }
5304
+ withNamespace(namespace, expectedResolvedOrgIdentity) {
5305
+ return new _ParametersCache({
5306
+ memoryCache: this.memoryCache,
5307
+ diskCache: this.diskCache,
5308
+ namespace,
5309
+ expectedResolvedOrgIdentity
5310
+ });
5259
5311
  }
5260
5312
  async get(key) {
5261
- const cacheKey = createCacheKey2(key);
5313
+ const cacheKey = createCacheKey2(key, this.namespace);
5262
5314
  if (this.memoryCache) {
5263
- const memoryParams = this.memoryCache.get(cacheKey);
5264
- if (memoryParams !== void 0) {
5265
- return memoryParams;
5315
+ const memoryEntry = this.memoryCache.get(cacheKey);
5316
+ if (memoryEntry !== void 0 && (this.expectedResolvedOrgIdentity === void 0 || memoryEntry.resolvedOrgIdentity === this.expectedResolvedOrgIdentity)) {
5317
+ return memoryEntry.value;
5266
5318
  }
5267
5319
  }
5268
5320
  if (this.diskCache) {
5269
- const diskParams = await this.diskCache.get(cacheKey);
5270
- if (!diskParams) {
5321
+ const diskEntry = await this.diskCache.get(cacheKey);
5322
+ if (!diskEntry || this.expectedResolvedOrgIdentity !== void 0 && diskEntry.resolvedOrgIdentity !== this.expectedResolvedOrgIdentity) {
5271
5323
  return void 0;
5272
5324
  }
5273
- this.memoryCache?.set(cacheKey, diskParams);
5274
- return diskParams;
5325
+ const diskParameters = new RemoteEvalParameters(diskEntry.value.metadata);
5326
+ this.memoryCache?.set(cacheKey, {
5327
+ value: diskParameters,
5328
+ resolvedOrgIdentity: diskEntry.resolvedOrgIdentity
5329
+ });
5330
+ return diskParameters;
5275
5331
  }
5276
5332
  return void 0;
5277
5333
  }
5278
5334
  async set(key, value) {
5279
- const cacheKey = createCacheKey2(key);
5280
- this.memoryCache?.set(cacheKey, value);
5335
+ const cacheKey = createCacheKey2(key, this.namespace);
5336
+ const memoryEntry = {
5337
+ value,
5338
+ resolvedOrgIdentity: this.expectedResolvedOrgIdentity
5339
+ };
5340
+ this.memoryCache?.set(cacheKey, memoryEntry);
5281
5341
  if (this.diskCache) {
5282
- await this.diskCache.set(cacheKey, value);
5342
+ await this.diskCache.set(cacheKey, {
5343
+ value: value._internalSerializeForCache(),
5344
+ resolvedOrgIdentity: this.expectedResolvedOrgIdentity
5345
+ });
5283
5346
  }
5284
5347
  }
5285
5348
  };
@@ -5583,6 +5646,7 @@ var INSTRUMENTATION_NAMES = {
5583
5646
  CLOUDFLARE_THINK: "cloudflare-think",
5584
5647
  COHERE: "cohere",
5585
5648
  CURSOR_SDK: "cursor-sdk",
5649
+ DEEPSEEK_HARNESS: "deepseek-harness",
5586
5650
  EVE: "eve",
5587
5651
  FLUE: "flue",
5588
5652
  GENKIT: "genkit",
@@ -5608,7 +5672,7 @@ var INSTRUMENTATION_NAMES = {
5608
5672
  var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
5609
5673
  "braintrust.spanInstrumentationName"
5610
5674
  );
5611
- var SDK_VERSION = true ? "3.28.0" : "0.0.0";
5675
+ var SDK_VERSION = true ? "3.29.0" : "0.0.0";
5612
5676
  function withSpanInstrumentationName(args, instrumentationName) {
5613
5677
  return {
5614
5678
  ...args,
@@ -5727,6 +5791,16 @@ var datasetSnapshotRegisterResponseSchema = z8.object({
5727
5791
  dataset_snapshot: DatasetSnapshot,
5728
5792
  found_existing: z8.boolean().optional()
5729
5793
  });
5794
+ var datasetObjectInfoSchema = z8.object({
5795
+ object_id: z8.string(),
5796
+ object_name: z8.string(),
5797
+ parent_cols: z8.object({
5798
+ project: z8.object({
5799
+ id: z8.string(),
5800
+ name: z8.string()
5801
+ })
5802
+ })
5803
+ });
5730
5804
  var datasetRestorePreviewResultSchema = z8.object({
5731
5805
  rows_to_restore: z8.number(),
5732
5806
  rows_to_delete: z8.number()
@@ -5940,12 +6014,53 @@ var loginSchema = z8.strictObject({
5940
6014
  });
5941
6015
  var stateNonce = 0;
5942
6016
  var V1_PROXY_SUFFIX = "/v1/proxy";
6017
+ var LOADER_LOGIN_CACHE_MAX = 16;
5943
6018
  function normalizeProxyConnUrl(proxyUrl) {
5944
6019
  return proxyUrl.endsWith(V1_PROXY_SUFFIX) ? proxyUrl.slice(0, proxyUrl.length - V1_PROXY_SUFFIX.length) : proxyUrl;
5945
6020
  }
5946
6021
  var BraintrustState = class _BraintrustState {
6022
+ id;
6023
+ currentExperiment;
6024
+ // Note: the value of IsAsyncFlush doesn't really matter here, since we
6025
+ // (safely) dynamically cast it whenever retrieving the logger.
6026
+ currentLogger;
6027
+ currentParent;
6028
+ currentSpan;
6029
+ // Any time we re-log in, we directly update the apiConn inside the logger.
6030
+ // This is preferable to replacing the whole logger, which would create the
6031
+ // possibility of multiple loggers floating around, which may not log in a
6032
+ // deterministic order.
6033
+ _bgLogger;
6034
+ _overrideBgLogger = null;
6035
+ appUrl = null;
6036
+ appPublicUrl = null;
6037
+ loginToken = null;
6038
+ orgId = null;
6039
+ orgName = null;
6040
+ apiUrl = null;
6041
+ proxyUrl = null;
6042
+ loggedIn = false;
6043
+ gitMetadataSettings;
6044
+ debugLogLevel;
6045
+ debugLogLevelConfigured = false;
6046
+ fetch = globalThis.fetch;
6047
+ _appConn = null;
6048
+ _apiConn = null;
6049
+ _proxyConn = null;
6050
+ promptCache;
6051
+ parametersCache;
6052
+ spanCache;
6053
+ _idGenerator = null;
6054
+ _contextManager = null;
6055
+ _otelFlushCallback = null;
6056
+ spanOriginEnvironment;
6057
+ traceContextSigningSecret;
6058
+ loaderLoginCache = /* @__PURE__ */ new WeakMap();
6059
+ loginParams;
6060
+ activeLoginOrgNameSelector;
5947
6061
  constructor(loginParams) {
5948
- this.loginParams = loginParams;
6062
+ this.loginParams = { ...loginParams };
6063
+ this.activeLoginOrgNameSelector = loginParams.orgName ?? isomorph_default.getEnv("BRAINTRUST_ORG_NAME");
5949
6064
  this.id = `${(/* @__PURE__ */ new Date()).toLocaleString()}-${stateNonce++}`;
5950
6065
  this.currentExperiment = void 0;
5951
6066
  this.currentLogger = void 0;
@@ -5982,12 +6097,14 @@ var BraintrustState = class _BraintrustState {
5982
6097
  const {
5983
6098
  memoryCache: parametersMemoryCache,
5984
6099
  diskCache: parametersDiskCache
5985
- } = createCacheLayers({
5986
- memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
5987
- diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
5988
- diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
5989
- getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
5990
- });
6100
+ } = createCacheLayers(
6101
+ {
6102
+ memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
6103
+ diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
6104
+ diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
6105
+ getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
6106
+ }
6107
+ );
5991
6108
  this.parametersCache = new ParametersCache({
5992
6109
  memoryCache: parametersMemoryCache,
5993
6110
  diskCache: parametersDiskCache
@@ -5996,43 +6113,6 @@ var BraintrustState = class _BraintrustState {
5996
6113
  this.spanOriginEnvironment = detectSpanOriginEnvironment();
5997
6114
  this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5998
6115
  }
5999
- loginParams;
6000
- id;
6001
- currentExperiment;
6002
- // Note: the value of IsAsyncFlush doesn't really matter here, since we
6003
- // (safely) dynamically cast it whenever retrieving the logger.
6004
- currentLogger;
6005
- currentParent;
6006
- currentSpan;
6007
- // Any time we re-log in, we directly update the apiConn inside the logger.
6008
- // This is preferable to replacing the whole logger, which would create the
6009
- // possibility of multiple loggers floating around, which may not log in a
6010
- // deterministic order.
6011
- _bgLogger;
6012
- _overrideBgLogger = null;
6013
- appUrl = null;
6014
- appPublicUrl = null;
6015
- loginToken = null;
6016
- orgId = null;
6017
- orgName = null;
6018
- apiUrl = null;
6019
- proxyUrl = null;
6020
- loggedIn = false;
6021
- gitMetadataSettings;
6022
- debugLogLevel;
6023
- debugLogLevelConfigured = false;
6024
- fetch = globalThis.fetch;
6025
- _appConn = null;
6026
- _apiConn = null;
6027
- _proxyConn = null;
6028
- promptCache;
6029
- parametersCache;
6030
- spanCache;
6031
- _idGenerator = null;
6032
- _contextManager = null;
6033
- _otelFlushCallback = null;
6034
- spanOriginEnvironment;
6035
- traceContextSigningSecret;
6036
6116
  /** @internal */
6037
6117
  _internalSetTraceContextSigningSecret(secret) {
6038
6118
  const normalizedSecret = secret?.trim();
@@ -6057,6 +6137,101 @@ var BraintrustState = class _BraintrustState {
6057
6137
  this._appConn = null;
6058
6138
  this._apiConn = null;
6059
6139
  this._proxyConn = null;
6140
+ this.loaderLoginCache = /* @__PURE__ */ new WeakMap();
6141
+ }
6142
+ /** @internal */
6143
+ async _internalResolveLoaderLoginOptions({
6144
+ apiKey,
6145
+ appUrl,
6146
+ orgName,
6147
+ fetch: fetch2,
6148
+ forceLogin
6149
+ }) {
6150
+ const resolvedAppUrl = appUrl ?? (this.loggedIn ? this.appUrl ?? void 0 : void 0) ?? this.loginParams.appUrl ?? isomorph_default.getEnv("BRAINTRUST_APP_URL") ?? "https://www.braintrust.dev";
6151
+ const resolvedApiKey = apiKey ?? (this.loggedIn ? this.loginToken ?? void 0 : void 0) ?? this.loginParams.apiKey ?? await isomorph_default.getBraintrustApiKey();
6152
+ if (!resolvedApiKey) {
6153
+ throw new Error(
6154
+ "Please specify an api key (e.g. by setting BRAINTRUST_API_KEY)."
6155
+ );
6156
+ }
6157
+ const normalizedApiKey = HTTPConnection.sanitize_token(resolvedApiKey);
6158
+ const usesActiveCredential = this.loggedIn && normalizedApiKey === this.loginToken;
6159
+ const requestedOrgName = orgName ?? (usesActiveCredential ? this.activeLoginOrgNameSelector : void 0) ?? this.loginParams.orgName ?? isomorph_default.getEnv("BRAINTRUST_ORG_NAME");
6160
+ const resolvedOrgName = orgName ?? (usesActiveCredential ? this.orgName ?? void 0 : void 0) ?? requestedOrgName;
6161
+ const resolvedFetch = fetch2 ?? (this.loggedIn ? this.fetch : void 0) ?? this.loginParams.fetch ?? globalThis.fetch;
6162
+ const credentialCacheNamespace = JSON.stringify([
6163
+ "loader-credential",
6164
+ resolvedAppUrl,
6165
+ requestedOrgName,
6166
+ normalizedApiKey
6167
+ ]);
6168
+ return {
6169
+ apiKey: normalizedApiKey,
6170
+ appUrl: resolvedAppUrl,
6171
+ orgName: resolvedOrgName,
6172
+ fetch: resolvedFetch,
6173
+ forceLogin,
6174
+ credentialCacheNamespace,
6175
+ existingState: !forceLogin && usesActiveCredential && resolvedAppUrl === this.appUrl && resolvedOrgName === this.orgName && resolvedFetch === this.fetch ? this : void 0
6176
+ };
6177
+ }
6178
+ /** @internal */
6179
+ _internalGetLoaderCacheViews(loginOptions, requestState) {
6180
+ const expectedResolvedOrgIdentity = requestState?.orgId && requestState.appUrl ? JSON.stringify([
6181
+ "loader-org",
6182
+ requestState.appUrl,
6183
+ requestState.orgId
6184
+ ]) : void 0;
6185
+ return {
6186
+ promptCache: this.promptCache.withNamespace(
6187
+ loginOptions.credentialCacheNamespace,
6188
+ expectedResolvedOrgIdentity
6189
+ ),
6190
+ parametersCache: this.parametersCache.withNamespace(
6191
+ loginOptions.credentialCacheNamespace,
6192
+ expectedResolvedOrgIdentity
6193
+ )
6194
+ };
6195
+ }
6196
+ /** @internal */
6197
+ async _internalGetLoaderState({
6198
+ apiKey,
6199
+ appUrl,
6200
+ orgName,
6201
+ fetch: fetch2,
6202
+ forceLogin,
6203
+ existingState
6204
+ }) {
6205
+ if (existingState) {
6206
+ return existingState;
6207
+ }
6208
+ let cache = this.loaderLoginCache.get(fetch2);
6209
+ if (!cache) {
6210
+ cache = new LRUCache({ max: LOADER_LOGIN_CACHE_MAX });
6211
+ this.loaderLoginCache.set(fetch2, cache);
6212
+ }
6213
+ const cacheKey = JSON.stringify([appUrl, orgName, apiKey]);
6214
+ if (!forceLogin) {
6215
+ const cachedState = cache.get(cacheKey);
6216
+ if (cachedState) {
6217
+ return cachedState;
6218
+ }
6219
+ }
6220
+ const statePromise = loginToLoaderRequestState({
6221
+ orgName,
6222
+ apiKey,
6223
+ appUrl,
6224
+ fetch: fetch2
6225
+ });
6226
+ cache.set(cacheKey, statePromise);
6227
+ try {
6228
+ return await statePromise;
6229
+ } catch (error) {
6230
+ if (cache.get(cacheKey) === statePromise) {
6231
+ cache.delete(cacheKey);
6232
+ }
6233
+ throw error;
6234
+ }
6060
6235
  }
6061
6236
  resetIdGenState() {
6062
6237
  this._idGenerator = null;
@@ -6105,6 +6280,8 @@ var BraintrustState = class _BraintrustState {
6105
6280
  this.debugLogLevel = other.debugLogLevel;
6106
6281
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
6107
6282
  this.traceContextSigningSecret = other.traceContextSigningSecret;
6283
+ this.fetch = other.fetch;
6284
+ this.activeLoginOrgNameSelector = other.activeLoginOrgNameSelector;
6108
6285
  setGlobalDebugLogLevel(
6109
6286
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
6110
6287
  );
@@ -6344,36 +6521,85 @@ var FailedHTTPResponse = class extends Error {
6344
6521
  status;
6345
6522
  text;
6346
6523
  data;
6347
- constructor(status, text, data) {
6524
+ cause;
6525
+ constructor(status, text, data, cause) {
6348
6526
  super(`${status}: ${text} (${data})`);
6349
6527
  this.status = status;
6350
6528
  this.text = text;
6351
6529
  this.data = data;
6530
+ this.cause = cause;
6531
+ }
6532
+ };
6533
+ var HTTPTransportError = class extends Error {
6534
+ cause;
6535
+ constructor(cause) {
6536
+ super(cause instanceof Error ? cause.message : String(cause));
6537
+ this.name = "HTTPTransportError";
6538
+ this.cause = cause;
6352
6539
  }
6353
6540
  };
6541
+ var httpTransportErrorCauses = /* @__PURE__ */ new WeakSet();
6542
+ function recordHTTPTransportError(error) {
6543
+ if (typeof error === "object" && error !== null || typeof error === "function") {
6544
+ httpTransportErrorCauses.add(error);
6545
+ }
6546
+ }
6547
+ function rethrowHTTPTransportError(error, classifyTransportErrors) {
6548
+ if (classifyTransportErrors) {
6549
+ throw new HTTPTransportError(error);
6550
+ }
6551
+ recordHTTPTransportError(error);
6552
+ throw error;
6553
+ }
6554
+ function isLoaderCacheFallbackError(error) {
6555
+ if (error instanceof FailedHTTPResponse) {
6556
+ return error.status === 408 || error.status === 429 || error.status >= 500;
6557
+ }
6558
+ if (error instanceof HTTPTransportError) {
6559
+ return true;
6560
+ }
6561
+ return (typeof error === "object" && error !== null || typeof error === "function") && httpTransportErrorCauses.has(error);
6562
+ }
6563
+ async function readJSONResponse(response, classifyTransportErrors = false) {
6564
+ let data;
6565
+ try {
6566
+ data = await response.text();
6567
+ } catch (error) {
6568
+ rethrowHTTPTransportError(error, classifyTransportErrors);
6569
+ }
6570
+ return JSON.parse(data);
6571
+ }
6354
6572
  async function checkResponse(resp) {
6355
6573
  if (resp.ok) {
6356
6574
  return resp;
6357
- } else {
6575
+ }
6576
+ let data;
6577
+ try {
6578
+ data = await resp.text();
6579
+ } catch (error) {
6358
6580
  throw new FailedHTTPResponse(
6359
6581
  resp.status,
6360
6582
  resp.statusText,
6361
- await resp.text()
6583
+ "Unable to read response body",
6584
+ error
6362
6585
  );
6363
6586
  }
6587
+ throw new FailedHTTPResponse(resp.status, resp.statusText, data);
6364
6588
  }
6365
6589
  var HTTPConnection = class _HTTPConnection {
6366
- base_url;
6367
- token;
6368
- headers;
6369
- fetch;
6370
- constructor(base_url, fetch2) {
6590
+ constructor(base_url, fetch2, classifyTransportErrors = false) {
6591
+ this.classifyTransportErrors = classifyTransportErrors;
6371
6592
  this.base_url = base_url;
6372
6593
  this.token = null;
6373
6594
  this.headers = {};
6374
6595
  this._reset();
6375
6596
  this.fetch = fetch2;
6376
6597
  }
6598
+ classifyTransportErrors;
6599
+ base_url;
6600
+ token;
6601
+ headers;
6602
+ fetch;
6377
6603
  setFetch(fetch2) {
6378
6604
  this.fetch = fetch2;
6379
6605
  }
@@ -6413,9 +6639,9 @@ var HTTPConnection = class _HTTPConnection {
6413
6639
  ).toString();
6414
6640
  const this_fetch = this.fetch;
6415
6641
  const this_headers = this.headers;
6416
- return await checkResponse(
6417
- // Using toString() here makes it work with isomorphic fetch
6418
- await this_fetch(url.toString(), {
6642
+ let response;
6643
+ try {
6644
+ response = await this_fetch(url.toString(), {
6419
6645
  headers: {
6420
6646
  Accept: "application/json",
6421
6647
  ...this_headers,
@@ -6423,8 +6649,14 @@ var HTTPConnection = class _HTTPConnection {
6423
6649
  },
6424
6650
  keepalive: true,
6425
6651
  ...rest
6426
- })
6427
- );
6652
+ });
6653
+ } catch (error) {
6654
+ if (config?.signal?.aborted) {
6655
+ throw getAbortReason(config.signal);
6656
+ }
6657
+ rethrowHTTPTransportError(error, this.classifyTransportErrors);
6658
+ }
6659
+ return await checkResponse(response);
6428
6660
  }
6429
6661
  async post(path, params, config, retries = 0) {
6430
6662
  const { headers, ...rest } = config || {};
@@ -6434,8 +6666,9 @@ var HTTPConnection = class _HTTPConnection {
6434
6666
  const tries = retries + 1;
6435
6667
  for (let i = 0; i < tries; i++) {
6436
6668
  try {
6437
- return await checkResponse(
6438
- await this_fetch(_urljoin(this_base_url, path), {
6669
+ let response;
6670
+ try {
6671
+ response = await this_fetch(_urljoin(this_base_url, path), {
6439
6672
  method: "POST",
6440
6673
  headers: {
6441
6674
  Accept: "application/json",
@@ -6446,8 +6679,14 @@ var HTTPConnection = class _HTTPConnection {
6446
6679
  body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
6447
6680
  keepalive: true,
6448
6681
  ...rest
6449
- })
6450
- );
6682
+ });
6683
+ } catch (error) {
6684
+ if (config?.signal?.aborted) {
6685
+ throw getAbortReason(config.signal);
6686
+ }
6687
+ rethrowHTTPTransportError(error, this.classifyTransportErrors);
6688
+ }
6689
+ return await checkResponse(response);
6451
6690
  } catch (error) {
6452
6691
  if (config?.signal?.aborted) {
6453
6692
  throw getAbortReason(config.signal);
@@ -6472,7 +6711,7 @@ var HTTPConnection = class _HTTPConnection {
6472
6711
  for (let i = 0; i < tries; i++) {
6473
6712
  try {
6474
6713
  const resp = await this.get(`${object_type}`, args);
6475
- return await resp.json();
6714
+ return await readJSONResponse(resp, this.classifyTransportErrors);
6476
6715
  } catch (e) {
6477
6716
  if (i < tries - 1) {
6478
6717
  debugLogger.debug(
@@ -6495,7 +6734,7 @@ var HTTPConnection = class _HTTPConnection {
6495
6734
  const resp = await this.post(`${object_type}`, args, {
6496
6735
  headers: { "Content-Type": "application/json" }
6497
6736
  });
6498
- return await resp.json();
6737
+ return await readJSONResponse(resp, this.classifyTransportErrors);
6499
6738
  }
6500
6739
  // Custom inspect for Node.js console.log
6501
6740
  [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
@@ -8543,6 +8782,7 @@ function initDataset(projectOrOptions, optionalOptions) {
8543
8782
  const {
8544
8783
  project,
8545
8784
  dataset,
8785
+ datasetId,
8546
8786
  description,
8547
8787
  version,
8548
8788
  snapshotName,
@@ -8558,6 +8798,11 @@ function initDataset(projectOrOptions, optionalOptions) {
8558
8798
  state: stateArg,
8559
8799
  _internal_btql
8560
8800
  } = options;
8801
+ if (datasetId !== void 0 && (description !== void 0 || metadata !== void 0)) {
8802
+ throw new Error(
8803
+ "Cannot specify description or metadata when datasetId is provided"
8804
+ );
8805
+ }
8561
8806
  const selection = normalizeDatasetSelection({
8562
8807
  version,
8563
8808
  environment,
@@ -8578,6 +8823,35 @@ function initDataset(projectOrOptions, optionalOptions) {
8578
8823
  fetch: fetch2,
8579
8824
  forceLogin
8580
8825
  });
8826
+ if (datasetId !== void 0) {
8827
+ const objectInfo = datasetObjectInfoSchema.array().parse(
8828
+ await state.appConn().post_json("api/self/get_object_info", {
8829
+ object_type: "dataset",
8830
+ object_ids: [datasetId]
8831
+ })
8832
+ );
8833
+ if (objectInfo.length === 0) {
8834
+ throw new Error(`Dataset with ID ${datasetId} not found`);
8835
+ }
8836
+ if (objectInfo.length !== 1) {
8837
+ throw new Error(
8838
+ `Expected exactly one dataset with ID ${datasetId}, but found ${objectInfo.length}`
8839
+ );
8840
+ }
8841
+ const datasetInfo = objectInfo[0];
8842
+ return {
8843
+ project: {
8844
+ id: datasetInfo.parent_cols.project.id,
8845
+ name: datasetInfo.parent_cols.project.name,
8846
+ fullInfo: datasetInfo.parent_cols.project
8847
+ },
8848
+ dataset: {
8849
+ id: datasetInfo.object_id,
8850
+ name: datasetInfo.object_name,
8851
+ fullInfo: datasetInfo
8852
+ }
8853
+ };
8854
+ }
8581
8855
  const args = {
8582
8856
  org_id: state.orgId,
8583
8857
  project_name: project,
@@ -8731,6 +9005,24 @@ function initLogger(options = {}) {
8731
9005
  }
8732
9006
  return ret;
8733
9007
  }
9008
+ async function runCredentialScopedLoaderRequest(state, loginOptions, request) {
9009
+ const resolvedLoginOptions = await state._internalResolveLoaderLoginOptions(loginOptions);
9010
+ let cacheViews = state._internalGetLoaderCacheViews(resolvedLoginOptions);
9011
+ try {
9012
+ const requestState = await state._internalGetLoaderState(resolvedLoginOptions);
9013
+ cacheViews = state._internalGetLoaderCacheViews(
9014
+ resolvedLoginOptions,
9015
+ requestState
9016
+ );
9017
+ return {
9018
+ ok: true,
9019
+ response: await request(requestState),
9020
+ ...cacheViews
9021
+ };
9022
+ } catch (error) {
9023
+ return { ok: false, error, ...cacheViews };
9024
+ }
9025
+ }
8734
9026
  async function loadPrompt({
8735
9027
  projectName,
8736
9028
  projectId,
@@ -8755,43 +9047,47 @@ async function loadPrompt({
8755
9047
  throw new Error("Must specify slug");
8756
9048
  }
8757
9049
  const state = stateArg ?? _globalState;
8758
- let response;
8759
- try {
8760
- await state.login({
8761
- orgName,
8762
- apiKey,
8763
- appUrl,
8764
- fetch: fetch2,
8765
- forceLogin
8766
- });
8767
- if (id) {
8768
- response = await state.apiConn().get_json(`v1/prompt/${id}`, versionOrEnvironment);
8769
- if (response) {
8770
- response = { objects: [response] };
9050
+ const result = await runCredentialScopedLoaderRequest(
9051
+ state,
9052
+ { orgName, apiKey, appUrl, fetch: fetch2, forceLogin },
9053
+ async (requestState) => {
9054
+ let response2;
9055
+ if (id) {
9056
+ response2 = await requestState.apiConn().get_json(`v1/prompt/${id}`, versionOrEnvironment);
9057
+ if (response2) {
9058
+ response2 = { objects: [response2] };
9059
+ }
9060
+ } else {
9061
+ response2 = await requestState.apiConn().get_json("v1/prompt", {
9062
+ project_name: projectName,
9063
+ project_id: projectId,
9064
+ slug,
9065
+ ...versionOrEnvironment
9066
+ });
8771
9067
  }
8772
- } else {
8773
- response = await state.apiConn().get_json("v1/prompt", {
8774
- project_name: projectName,
8775
- project_id: projectId,
8776
- slug,
8777
- ...versionOrEnvironment
8778
- });
9068
+ return response2;
9069
+ }
9070
+ );
9071
+ const { promptCache } = result;
9072
+ if (!result.ok) {
9073
+ const e = result.error;
9074
+ if (!isLoaderCacheFallbackError(e)) {
9075
+ throw e;
8779
9076
  }
8780
- } catch (e) {
8781
9077
  if (version || environment) {
8782
9078
  throw new Error(`Prompt not found with specified parameters: ${e}`);
8783
9079
  }
8784
9080
  debugLogger.forState(state).warn("Failed to load prompt, attempting to fall back to cache:", e);
8785
9081
  let prompt2;
8786
9082
  if (id) {
8787
- prompt2 = await state.promptCache.get({ id });
9083
+ prompt2 = await promptCache.get({ id });
8788
9084
  if (!prompt2) {
8789
9085
  throw new Error(
8790
9086
  `Prompt with id ${id} not found (not found on server or in local cache): ${e}`
8791
9087
  );
8792
9088
  }
8793
9089
  } else {
8794
- prompt2 = await state.promptCache.get({
9090
+ prompt2 = await promptCache.get({
8795
9091
  slug,
8796
9092
  projectId,
8797
9093
  projectName,
@@ -8807,6 +9103,7 @@ async function loadPrompt({
8807
9103
  }
8808
9104
  return prompt2;
8809
9105
  }
9106
+ const { response } = result;
8810
9107
  if (!("objects" in response) || response.objects.length === 0) {
8811
9108
  if (id) {
8812
9109
  throw new Error(`Prompt with id ${id} not found.`);
@@ -8830,9 +9127,9 @@ async function loadPrompt({
8830
9127
  const prompt = new Prompt2(metadata, defaults || {}, noTrace);
8831
9128
  try {
8832
9129
  if (id) {
8833
- await state.promptCache.set({ id }, prompt);
9130
+ await promptCache.set({ id }, prompt);
8834
9131
  } else if (slug) {
8835
- await state.promptCache.set(
9132
+ await promptCache.set(
8836
9133
  { slug, projectId, projectName, version: version ?? "latest" },
8837
9134
  prompt
8838
9135
  );
@@ -8864,46 +9161,50 @@ async function loadParameters({
8864
9161
  throw new Error("Must specify slug");
8865
9162
  }
8866
9163
  const state = stateArg ?? _globalState;
8867
- let response;
8868
- try {
8869
- await state.login({
8870
- orgName,
8871
- apiKey,
8872
- appUrl,
8873
- fetch: fetch2,
8874
- forceLogin
8875
- });
8876
- if (id) {
8877
- response = await state.apiConn().get_json(`v1/function/${id}`, {
8878
- ...versionOrEnvironment
8879
- });
8880
- if (response) {
8881
- response = { objects: [response] };
9164
+ const result = await runCredentialScopedLoaderRequest(
9165
+ state,
9166
+ { orgName, apiKey, appUrl, fetch: fetch2, forceLogin },
9167
+ async (requestState) => {
9168
+ let response2;
9169
+ if (id) {
9170
+ response2 = await requestState.apiConn().get_json(`v1/function/${id}`, {
9171
+ ...versionOrEnvironment
9172
+ });
9173
+ if (response2) {
9174
+ response2 = { objects: [response2] };
9175
+ }
9176
+ } else {
9177
+ response2 = await requestState.apiConn().get_json("v1/function", {
9178
+ project_name: projectName,
9179
+ project_id: projectId,
9180
+ slug,
9181
+ function_type: "parameters",
9182
+ ...versionOrEnvironment
9183
+ });
8882
9184
  }
8883
- } else {
8884
- response = await state.apiConn().get_json("v1/function", {
8885
- project_name: projectName,
8886
- project_id: projectId,
8887
- slug,
8888
- function_type: "parameters",
8889
- ...versionOrEnvironment
8890
- });
9185
+ return response2;
9186
+ }
9187
+ );
9188
+ const { parametersCache } = result;
9189
+ if (!result.ok) {
9190
+ const e = result.error;
9191
+ if (!isLoaderCacheFallbackError(e)) {
9192
+ throw e;
8891
9193
  }
8892
- } catch (e) {
8893
9194
  if (version || environment) {
8894
9195
  throw new Error(`Parameters not found with specified parameters: ${e}`);
8895
9196
  }
8896
9197
  debugLogger.forState(state).warn("Failed to load parameters, attempting to fall back to cache:", e);
8897
9198
  let parameters2;
8898
9199
  if (id) {
8899
- parameters2 = await state.parametersCache.get({ id });
9200
+ parameters2 = await parametersCache.get({ id });
8900
9201
  if (!parameters2) {
8901
9202
  throw new Error(
8902
9203
  `Parameters with id ${id} not found (not found on server or in local cache): ${e}`
8903
9204
  );
8904
9205
  }
8905
9206
  } else {
8906
- parameters2 = await state.parametersCache.get({
9207
+ parameters2 = await parametersCache.get({
8907
9208
  slug,
8908
9209
  projectId,
8909
9210
  projectName,
@@ -8919,6 +9220,7 @@ async function loadParameters({
8919
9220
  }
8920
9221
  return parameters2;
8921
9222
  }
9223
+ const { response } = result;
8922
9224
  if (!("objects" in response) || response.objects.length === 0) {
8923
9225
  if (id) {
8924
9226
  throw new Error(`Parameters with id ${id} not found.`);
@@ -8942,9 +9244,9 @@ async function loadParameters({
8942
9244
  const parameters = new RemoteEvalParameters(metadata);
8943
9245
  try {
8944
9246
  if (id) {
8945
- await state.parametersCache.set({ id }, parameters);
9247
+ await parametersCache.set({ id }, parameters);
8946
9248
  } else if (slug) {
8947
- await state.parametersCache.set(
9249
+ await parametersCache.set(
8948
9250
  { slug, projectId, projectName, version: version ?? "latest" },
8949
9251
  parameters
8950
9252
  );
@@ -8985,6 +9287,52 @@ async function login(options = {}) {
8985
9287
  await state.login(options);
8986
9288
  return state;
8987
9289
  }
9290
+ async function loginToLoaderRequestState({
9291
+ appUrl,
9292
+ apiKey,
9293
+ orgName,
9294
+ fetch: fetch2
9295
+ }) {
9296
+ let orgId;
9297
+ let apiUrl;
9298
+ if (apiKey === TEST_API_KEY) {
9299
+ orgId = "test-org-id";
9300
+ apiUrl = "https://braintrust.dev/fake-api-url";
9301
+ } else {
9302
+ let loginResponse;
9303
+ try {
9304
+ loginResponse = await fetch2(_urljoin(appUrl, `/api/apikey/login`), {
9305
+ method: "POST",
9306
+ headers: {
9307
+ "Content-Type": "application/json",
9308
+ Authorization: `Bearer ${apiKey}`
9309
+ }
9310
+ });
9311
+ } catch (error) {
9312
+ throw new HTTPTransportError(error);
9313
+ }
9314
+ const info = await readJSONResponse(
9315
+ await checkResponse(loginResponse),
9316
+ true
9317
+ );
9318
+ const org = selectLoginOrg(info.org_info, orgName);
9319
+ orgId = org.id;
9320
+ apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
9321
+ if (!apiUrl) {
9322
+ throw new Error(
9323
+ orgName ? `Unable to log into organization '${orgName}'. Are you sure this credential is scoped to the organization?` : "Unable to log into any organization with the provided credential."
9324
+ );
9325
+ }
9326
+ }
9327
+ const apiConnection = new HTTPConnection(apiUrl, fetch2, true);
9328
+ apiConnection.set_token(apiKey);
9329
+ apiConnection.make_long_lived();
9330
+ return {
9331
+ appUrl,
9332
+ orgId,
9333
+ apiConn: () => apiConnection
9334
+ };
9335
+ }
8988
9336
  async function loginToState(options = {}) {
8989
9337
  const {
8990
9338
  appUrl = isomorph_default.getEnv("BRAINTRUST_APP_URL") || "https://www.braintrust.dev",
@@ -9016,16 +9364,18 @@ async function loginToState(options = {}) {
9016
9364
  _saveOrgInfo(state, testOrgInfo, testOrgInfo[0].name);
9017
9365
  return state;
9018
9366
  } else {
9019
- const resp = await checkResponse(
9020
- await fetch2(_urljoin(state.appUrl, `/api/apikey/login`), {
9367
+ const loginResponse = await fetch2(
9368
+ _urljoin(state.appUrl, `/api/apikey/login`),
9369
+ {
9021
9370
  method: "POST",
9022
9371
  headers: {
9023
9372
  "Content-Type": "application/json",
9024
9373
  Authorization: `Bearer ${apiKey}`
9025
9374
  }
9026
- })
9375
+ }
9027
9376
  );
9028
- const info = await resp.json();
9377
+ const resp = await checkResponse(loginResponse);
9378
+ const info = await readJSONResponse(resp);
9029
9379
  _saveOrgInfo(state, info.org_info, orgName);
9030
9380
  if (!state.apiUrl) {
9031
9381
  if (orgName) {
@@ -9528,11 +9878,11 @@ function wrapTraced(fn, args) {
9528
9878
  }
9529
9879
  if (args?.asyncFlush) {
9530
9880
  return ((...fnArgs) => traced((span) => {
9531
- if (!hasExplicitInput) {
9881
+ if (!args?.noTraceIO && !hasExplicitInput) {
9532
9882
  span.log({ input: fnArgs });
9533
9883
  }
9534
9884
  const output = fn(...fnArgs);
9535
- if (!hasExplicitOutput) {
9885
+ if (!args?.noTraceIO && !hasExplicitOutput) {
9536
9886
  if (output instanceof Promise) {
9537
9887
  return (async () => {
9538
9888
  const result = await output;
@@ -9547,12 +9897,12 @@ function wrapTraced(fn, args) {
9547
9897
  }, spanArgs));
9548
9898
  } else {
9549
9899
  return ((...fnArgs) => traced(async (span) => {
9550
- if (!hasExplicitInput) {
9900
+ if (!args?.noTraceIO && !hasExplicitInput) {
9551
9901
  span.log({ input: fnArgs });
9552
9902
  }
9553
9903
  const outputResult = fn(...fnArgs);
9554
9904
  const output = await outputResult;
9555
- if (!hasExplicitOutput) {
9905
+ if (!args?.noTraceIO && !hasExplicitOutput) {
9556
9906
  span.log({ output });
9557
9907
  }
9558
9908
  return output;
@@ -9678,27 +10028,28 @@ async function* asyncGeneratorWithCurrent(span, gen, state = void 0) {
9678
10028
  function withParent(parent, callback, state = void 0) {
9679
10029
  return (state ?? _globalState).currentParent.run(parent, () => callback());
9680
10030
  }
9681
- function _saveOrgInfo(state, org_info, org_name) {
9682
- if (org_info.length === 0) {
10031
+ function _saveOrgInfo(state, orgInfo, orgName) {
10032
+ const org = selectLoginOrg(orgInfo, orgName);
10033
+ state.orgId = org.id;
10034
+ state.orgName = org.name;
10035
+ state.apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
10036
+ state.proxyUrl = isomorph_default.getEnv("BRAINTRUST_PROXY_URL") ?? org.proxy_url;
10037
+ state.gitMetadataSettings = org.git_metadata || void 0;
10038
+ }
10039
+ function selectLoginOrg(orgInfo, orgName) {
10040
+ if (orgInfo.length === 0) {
9683
10041
  throw new LoginInvalidOrgError(
9684
10042
  "This user is not part of any organizations."
9685
10043
  );
9686
10044
  }
9687
- for (const org of org_info) {
9688
- if (org_name === void 0 || org.name === org_name) {
9689
- state.orgId = org.id;
9690
- state.orgName = org.name;
9691
- state.apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
9692
- state.proxyUrl = isomorph_default.getEnv("BRAINTRUST_PROXY_URL") ?? org.proxy_url;
9693
- state.gitMetadataSettings = org.git_metadata || void 0;
9694
- break;
10045
+ for (const org of orgInfo) {
10046
+ if (orgName === void 0 || org.name === orgName) {
10047
+ return org;
9695
10048
  }
9696
10049
  }
9697
- if (state.orgId === void 0) {
9698
- throw new LoginInvalidOrgError(
9699
- `Organization ${org_name} not found. Must be one of ${org_info.map((x) => x.name).join(", ")}`
9700
- );
9701
- }
10050
+ throw new LoginInvalidOrgError(
10051
+ `Organization ${orgName} not found. Must be one of ${orgInfo.map((org) => org.name).join(", ")}`
10052
+ );
9702
10053
  }
9703
10054
  function validateTags(tags) {
9704
10055
  const seen = /* @__PURE__ */ new Set();
@@ -11604,6 +11955,14 @@ var Prompt2 = class _Prompt {
11604
11955
  static isPrompt(data) {
11605
11956
  return typeof data === "object" && data !== null && "__braintrust_prompt_marker" in data;
11606
11957
  }
11958
+ /** @internal */
11959
+ _internalSerializeForCache() {
11960
+ return {
11961
+ metadata: this.metadata,
11962
+ defaults: this.defaults,
11963
+ noTrace: this.noTrace
11964
+ };
11965
+ }
11607
11966
  static fromPromptData(name, promptData) {
11608
11967
  return new _Prompt(
11609
11968
  {
@@ -11644,6 +12003,10 @@ var RemoteEvalParameters = class {
11644
12003
  get data() {
11645
12004
  return this.metadata.function_data.data ?? {};
11646
12005
  }
12006
+ /** @internal */
12007
+ _internalSerializeForCache() {
12008
+ return { metadata: this.metadata };
12009
+ }
11647
12010
  validate(data) {
11648
12011
  if (typeof data !== "object" || data === null) {
11649
12012
  return false;
@@ -12464,56 +12827,14 @@ function suppressionStore() {
12464
12827
  autoInstrumentationSuppressionStore ??= isomorph_default.newAsyncLocalStorage();
12465
12828
  return autoInstrumentationSuppressionStore;
12466
12829
  }
12467
- function currentFrames() {
12468
- return suppressionStore().getStore()?.frames ?? [];
12469
- }
12470
12830
  function isAutoInstrumentationSuppressed() {
12471
- const frames = currentFrames();
12472
- return frames[frames.length - 1]?.mode === "suppress";
12831
+ return suppressionStore().getStore() === true;
12473
12832
  }
12474
12833
  function runWithAutoInstrumentationSuppressed(callback) {
12475
- const frame = {
12476
- id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-suppress"),
12477
- mode: "suppress"
12478
- };
12479
- return suppressionStore().run(
12480
- { frames: [...currentFrames(), frame] },
12481
- callback
12482
- );
12834
+ return suppressionStore().run(true, callback);
12483
12835
  }
12484
- function bindAutoInstrumentationSuppressionToStart(tracingChannel) {
12485
- const startChannel = tracingChannel.start;
12486
- if (!startChannel) {
12487
- return void 0;
12488
- }
12489
- const store = suppressionStore();
12490
- startChannel.bindStore(store, () => ({
12491
- frames: [
12492
- ...currentFrames(),
12493
- {
12494
- id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-suppress"),
12495
- mode: "suppress"
12496
- }
12497
- ]
12498
- }));
12499
- return () => {
12500
- startChannel.unbindStore(store);
12501
- };
12502
- }
12503
- function enterAutoInstrumentationAllowed() {
12504
- const frame = {
12505
- id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-allow"),
12506
- mode: "allow"
12507
- };
12508
- suppressionStore().enterWith({
12509
- frames: [...currentFrames(), frame]
12510
- });
12511
- return () => {
12512
- const frames = currentFrames().filter(
12513
- (candidate) => candidate.id !== frame.id
12514
- );
12515
- suppressionStore().enterWith(frames.length > 0 ? { frames } : void 0);
12516
- };
12836
+ function runWithAutoInstrumentationAllowed(callback) {
12837
+ return suppressionStore().run(void 0, callback);
12517
12838
  }
12518
12839
 
12519
12840
  // src/instrumentation/core/channel-tracing.ts
@@ -13802,13 +14123,33 @@ function aggregateChatLogprobs(existing, incoming) {
13802
14123
  }
13803
14124
  return aggregated;
13804
14125
  }
14126
+ function createAggregatedChatChoice(index) {
14127
+ return {
14128
+ index,
14129
+ role: void 0,
14130
+ content: void 0,
14131
+ refusal: void 0,
14132
+ toolCallsByIndex: /* @__PURE__ */ new Map(),
14133
+ logprobs: void 0,
14134
+ finish_reason: void 0
14135
+ };
14136
+ }
14137
+ function toChatChoice(choice) {
14138
+ const toolCalls2 = Array.from(choice.toolCallsByIndex.entries()).sort(([left], [right]) => left - right).map(([, toolCall]) => toolCall);
14139
+ return {
14140
+ index: choice.index,
14141
+ message: {
14142
+ role: choice.role,
14143
+ content: choice.content,
14144
+ ...choice.refusal !== void 0 ? { refusal: choice.refusal } : {},
14145
+ tool_calls: toolCalls2.length > 0 ? toolCalls2 : void 0
14146
+ },
14147
+ logprobs: choice.logprobs ?? null,
14148
+ finish_reason: choice.finish_reason
14149
+ };
14150
+ }
13805
14151
  function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
13806
- let role = void 0;
13807
- let content = void 0;
13808
- let refusal = void 0;
13809
- let tool_calls = void 0;
13810
- let logprobs = void 0;
13811
- let finish_reason = void 0;
14152
+ const choicesByIndex = /* @__PURE__ */ new Map();
13812
14153
  let metrics = {};
13813
14154
  for (const chunk of chunks) {
13814
14155
  if (chunk.usage) {
@@ -13817,62 +14158,75 @@ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
13817
14158
  ...parseMetricsFromUsage(chunk.usage)
13818
14159
  };
13819
14160
  }
13820
- const choice = chunk.choices?.[0];
13821
- if (!choice) {
13822
- continue;
13823
- }
13824
- if (choice.finish_reason) {
13825
- finish_reason = choice.finish_reason;
13826
- }
13827
- logprobs = aggregateChatLogprobs(logprobs, choice.logprobs);
13828
- const delta = choice.delta;
13829
- if (!delta) {
14161
+ const choices = chunk.choices;
14162
+ if (!choices?.length) {
13830
14163
  continue;
13831
14164
  }
13832
- if (delta.finish_reason) {
13833
- finish_reason = delta.finish_reason;
13834
- }
13835
- if (!role && delta.role) {
13836
- role = delta.role;
13837
- }
13838
- if (delta.content) {
13839
- content = (content || "") + delta.content;
13840
- }
13841
- if (delta.refusal) {
13842
- refusal = (refusal || "") + delta.refusal;
13843
- }
13844
- if (delta.tool_calls) {
13845
- const toolDelta = delta.tool_calls[0];
13846
- if (!tool_calls || toolDelta.id && tool_calls[tool_calls.length - 1].id !== toolDelta.id) {
13847
- tool_calls = [
13848
- ...tool_calls || [],
13849
- {
13850
- id: toolDelta.id,
13851
- type: toolDelta.type,
13852
- function: toolDelta.function
14165
+ for (const choice of choices) {
14166
+ const choiceIndex = choice.index;
14167
+ let aggregatedChoice = choicesByIndex.get(choiceIndex);
14168
+ if (!aggregatedChoice) {
14169
+ aggregatedChoice = createAggregatedChatChoice(choiceIndex);
14170
+ choicesByIndex.set(choiceIndex, aggregatedChoice);
14171
+ }
14172
+ if (choice.finish_reason) {
14173
+ aggregatedChoice.finish_reason = choice.finish_reason;
14174
+ }
14175
+ aggregatedChoice.logprobs = aggregateChatLogprobs(
14176
+ aggregatedChoice.logprobs,
14177
+ choice.logprobs
14178
+ );
14179
+ const delta = choice.delta;
14180
+ if (!delta) {
14181
+ continue;
14182
+ }
14183
+ if (delta.finish_reason) {
14184
+ aggregatedChoice.finish_reason = delta.finish_reason;
14185
+ }
14186
+ if (!aggregatedChoice.role && delta.role) {
14187
+ aggregatedChoice.role = delta.role;
14188
+ }
14189
+ if (delta.content) {
14190
+ aggregatedChoice.content = (aggregatedChoice.content || "") + delta.content;
14191
+ }
14192
+ if (delta.refusal) {
14193
+ aggregatedChoice.refusal = (aggregatedChoice.refusal || "") + delta.refusal;
14194
+ }
14195
+ if (delta.tool_calls) {
14196
+ for (const toolDelta of delta.tool_calls) {
14197
+ let aggregatedToolCall = aggregatedChoice.toolCallsByIndex.get(
14198
+ toolDelta.index
14199
+ );
14200
+ if (!aggregatedToolCall) {
14201
+ aggregatedToolCall = {
14202
+ function: { arguments: "" }
14203
+ };
14204
+ aggregatedChoice.toolCallsByIndex.set(
14205
+ toolDelta.index,
14206
+ aggregatedToolCall
14207
+ );
13853
14208
  }
13854
- ];
13855
- } else {
13856
- tool_calls[tool_calls.length - 1].function.arguments += toolDelta.function.arguments;
14209
+ if (toolDelta.id !== void 0) {
14210
+ aggregatedToolCall.id = toolDelta.id;
14211
+ }
14212
+ if (toolDelta.type !== void 0) {
14213
+ aggregatedToolCall.type = toolDelta.type;
14214
+ }
14215
+ if (toolDelta.function?.name !== void 0) {
14216
+ aggregatedToolCall.function.name = toolDelta.function.name;
14217
+ }
14218
+ if (toolDelta.function?.arguments !== void 0) {
14219
+ aggregatedToolCall.function.arguments += toolDelta.function.arguments;
14220
+ }
14221
+ }
13857
14222
  }
13858
14223
  }
13859
14224
  }
13860
14225
  metrics = withCachedMetric(metrics, streamResult, endEvent);
14226
+ const output = Array.from(choicesByIndex.values()).sort((left, right) => left.index - right.index).map(toChatChoice);
13861
14227
  return {
13862
14228
  metrics,
13863
- output: [
13864
- {
13865
- index: 0,
13866
- message: {
13867
- role,
13868
- content,
13869
- ...refusal !== void 0 ? { refusal } : {},
13870
- tool_calls
13871
- },
13872
- logprobs: logprobs ?? null,
13873
- finish_reason
13874
- }
13875
- ]
14229
+ output: output.length > 0 ? output : [toChatChoice(createAggregatedChatChoice(0))]
13876
14230
  };
13877
14231
  }
13878
14232
  function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
@@ -15208,6 +15562,12 @@ function parseMetricsFromUsage2(usage) {
15208
15562
  }
15209
15563
  }
15210
15564
  }
15565
+ if (isObject(usage.output_tokens_details)) {
15566
+ const thinkingTokens = usage.output_tokens_details.thinking_tokens;
15567
+ if (typeof thinkingTokens === "number") {
15568
+ metrics.completion_reasoning_tokens = thinkingTokens;
15569
+ }
15570
+ }
15211
15571
  if (isObject(usage.server_tool_use)) {
15212
15572
  for (const [name, value] of Object.entries(usage.server_tool_use)) {
15213
15573
  if (typeof value === "number") {
@@ -16164,7 +16524,6 @@ function endHarnessTurn(parent) {
16164
16524
  function braintrustAISDKTelemetry() {
16165
16525
  const operations = /* @__PURE__ */ new Map();
16166
16526
  const operationKeysByCallId = /* @__PURE__ */ new Map();
16167
- const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
16168
16527
  const modelSpans = /* @__PURE__ */ new Map();
16169
16528
  const objectSpans = /* @__PURE__ */ new Map();
16170
16529
  const embedSpans = /* @__PURE__ */ new Map();
@@ -16207,9 +16566,6 @@ function braintrustAISDKTelemetry() {
16207
16566
  return;
16208
16567
  }
16209
16568
  operations.delete(operationKey);
16210
- if (workflowOperationKeyStore.getStore() === operationKey) {
16211
- workflowOperationKeyStore.enterWith(void 0);
16212
- }
16213
16569
  const keys = operationKeysByCallId.get(state.callId);
16214
16570
  if (!keys) {
16215
16571
  return;
@@ -16257,14 +16613,7 @@ function braintrustAISDKTelemetry() {
16257
16613
  return key;
16258
16614
  }
16259
16615
  }
16260
- const workflowOperationKey = workflowOperationKeyStore.getStore();
16261
- if (workflowOperationKey && keys.includes(workflowOperationKey)) {
16262
- return workflowOperationKey;
16263
- }
16264
- if (callId === "workflow-agent") {
16265
- return void 0;
16266
- }
16267
- return mode === "finish" ? keys[0] : keys[keys.length - 1];
16616
+ return callId === "workflow-agent" || mode === "active" ? keys[keys.length - 1] : keys[0];
16268
16617
  };
16269
16618
  const operationKeyFromEvent = (event, mode = "active") => {
16270
16619
  const explicit = explicitOperationKey(event);
@@ -16278,17 +16627,13 @@ function braintrustAISDKTelemetry() {
16278
16627
  if (operationKey) {
16279
16628
  return operationKey;
16280
16629
  }
16281
- const workflowOperationKey2 = workflowOperationKeyStore.getStore();
16282
- if (workflowOperationKey2 && operations.has(workflowOperationKey2)) {
16283
- return workflowOperationKey2;
16630
+ const workflowAgentKeys2 = operationKeysByCallId.get("workflow-agent");
16631
+ if (workflowAgentKeys2?.length) {
16632
+ return workflowAgentKeys2[workflowAgentKeys2.length - 1];
16284
16633
  }
16285
16634
  return callId === "workflow-agent" ? void 0 : callId;
16286
16635
  }
16287
16636
  }
16288
- const workflowOperationKey = workflowOperationKeyStore.getStore();
16289
- if (workflowOperationKey && operations.has(workflowOperationKey)) {
16290
- return workflowOperationKey;
16291
- }
16292
16637
  const wrapperSpan = currentWorkflowAgentWrapperSpan();
16293
16638
  if (wrapperSpan?.spanId) {
16294
16639
  for (const [operationKey, state] of operations) {
@@ -16298,8 +16643,8 @@ function braintrustAISDKTelemetry() {
16298
16643
  }
16299
16644
  }
16300
16645
  const workflowAgentKeys = operationKeysByCallId.get("workflow-agent");
16301
- if (workflowAgentKeys?.length === 1) {
16302
- return workflowAgentKeys[0];
16646
+ if (workflowAgentKeys?.length) {
16647
+ return workflowAgentKeys[workflowAgentKeys.length - 1];
16303
16648
  }
16304
16649
  if (operations.size === 1) {
16305
16650
  return operations.keys().next().value;
@@ -16502,9 +16847,6 @@ function braintrustAISDKTelemetry() {
16502
16847
  if (!ownsSpan) {
16503
16848
  return;
16504
16849
  }
16505
- if (workflowAgent) {
16506
- workflowOperationKeyStore.enterWith(operationKey);
16507
- }
16508
16850
  let metadata = metadataFromEvent(event);
16509
16851
  const logPayload = { metadata };
16510
16852
  const workflowAgentCallInput = workflowAgent ? operationInput(event, operationName) : void 0;
@@ -16976,6 +17318,10 @@ var aiSDKChannels = defineChannels(
16976
17318
  channelName: "generateText",
16977
17319
  kind: "async"
16978
17320
  }),
17321
+ generateImage: channel({
17322
+ channelName: "generateImage",
17323
+ kind: "async"
17324
+ }),
16979
17325
  streamText: channel({
16980
17326
  channelName: "streamText",
16981
17327
  kind: "async"
@@ -17163,7 +17509,7 @@ var AISDKPlugin = class extends BasePlugin {
17163
17509
  }
17164
17510
  subscribeToAISDK() {
17165
17511
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
17166
- this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
17512
+ this.unsubscribers.push(interceptAISDKV7TelemetryDispatcher());
17167
17513
  this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
17168
17514
  this.unsubscribers.push(
17169
17515
  subscribeToHarnessContinuation(
@@ -17191,6 +17537,18 @@ var AISDKPlugin = class extends BasePlugin {
17191
17537
  aggregateChunks: aggregateAISDKChunks
17192
17538
  })
17193
17539
  );
17540
+ this.unsubscribers.push(
17541
+ traceAsyncChannel(aiSDKChannels.generateImage, {
17542
+ name: "generateImage",
17543
+ type: "llm" /* LLM */,
17544
+ extractInput: ([params], event) => prepareAISDKGenerateImageInput(params, event.self),
17545
+ extractOutput: (result, endEvent) => processAISDKGenerateImageOutput(
17546
+ result,
17547
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
17548
+ ),
17549
+ extractMetrics: (result) => extractTokenMetrics(result)
17550
+ })
17551
+ );
17194
17552
  this.unsubscribers.push(
17195
17553
  traceStreamingChannel(aiSDKChannels.streamText, {
17196
17554
  name: "streamText",
@@ -17680,26 +18038,29 @@ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPa
17680
18038
  channel2.unsubscribe(handlers);
17681
18039
  };
17682
18040
  }
17683
- function subscribeToAISDKV7TelemetryDispatcher() {
17684
- const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
18041
+ function interceptAISDKV7TelemetryDispatcher() {
17685
18042
  const telemetry = braintrustAISDKTelemetry();
17686
- const handlers = {
17687
- end: (event) => {
17688
- const telemetryOptions = event.arguments?.[0]?.telemetry;
17689
- if (telemetryOptions?.isEnabled === false) {
17690
- return;
18043
+ return aiSDKChannels.v7CreateTelemetryDispatcher.intercept(
18044
+ (target, thisArg, args) => {
18045
+ const dispatcher = Reflect.apply(target, thisArg, args);
18046
+ const telemetryOptions = args[0]?.telemetry;
18047
+ if (telemetryOptions?.isEnabled !== false) {
18048
+ try {
18049
+ patchAISDKV7TelemetryDispatcher(
18050
+ dispatcher,
18051
+ telemetry,
18052
+ telemetryOptions
18053
+ );
18054
+ } catch (error) {
18055
+ debugLogger.error(
18056
+ "Error instrumenting AI SDK v7 telemetry dispatcher:",
18057
+ error
18058
+ );
18059
+ }
17691
18060
  }
17692
- patchAISDKV7TelemetryDispatcher(
17693
- event.result,
17694
- telemetry,
17695
- telemetryOptions
17696
- );
18061
+ return dispatcher;
17697
18062
  }
17698
- };
17699
- channel2.subscribe(handlers);
17700
- return () => {
17701
- channel2.unsubscribe(handlers);
17702
- };
18063
+ );
17703
18064
  }
17704
18065
  function patchAISDKV7TelemetryDispatcher(dispatcher, telemetry, telemetryOptions) {
17705
18066
  if (!isObject(dispatcher)) {
@@ -18024,16 +18385,10 @@ var convertImageToAttachment = (image, explicitMimeType) => {
18024
18385
  }
18025
18386
  }
18026
18387
  if (explicitMimeType) {
18027
- if (image instanceof Uint8Array) {
18028
- return new Attachment({
18029
- data: new Blob([image], { type: explicitMimeType }),
18030
- filename: `image.${getExtensionFromMediaType(explicitMimeType)}`,
18031
- contentType: explicitMimeType
18032
- });
18033
- }
18034
- if (typeof Buffer !== "undefined" && Buffer.isBuffer(image)) {
18388
+ const blob = convertDataToBlob(image, explicitMimeType);
18389
+ if (blob) {
18035
18390
  return new Attachment({
18036
- data: new Blob([image], { type: explicitMimeType }),
18391
+ data: blob,
18037
18392
  filename: `image.${getExtensionFromMediaType(explicitMimeType)}`,
18038
18393
  contentType: explicitMimeType
18039
18394
  });
@@ -18087,6 +18442,25 @@ var convertDataToAttachment = (data, mimeType, filename) => {
18087
18442
  function processAISDKCallInput(params) {
18088
18443
  return processInputAttachmentsSync(params);
18089
18444
  }
18445
+ function processAISDKGenerateImageInput(params) {
18446
+ const prompt = params.prompt;
18447
+ if (!isObject(prompt) || Array.isArray(prompt)) {
18448
+ return processAISDKCallInput(params);
18449
+ }
18450
+ const processedPrompt = { ...prompt };
18451
+ if (Array.isArray(prompt.images)) {
18452
+ processedPrompt.images = prompt.images.map(
18453
+ (image) => convertImageToAttachment(image, "image/png") ?? image
18454
+ );
18455
+ }
18456
+ if (prompt.mask !== void 0) {
18457
+ processedPrompt.mask = convertImageToAttachment(prompt.mask, "image/png") ?? prompt.mask;
18458
+ }
18459
+ return processAISDKCallInput({
18460
+ ...params,
18461
+ prompt: processedPrompt
18462
+ });
18463
+ }
18090
18464
  function processAISDKWorkflowAgentCallInput(params) {
18091
18465
  const processed = processAISDKCallInput(params);
18092
18466
  return {
@@ -18225,6 +18599,12 @@ function prepareAISDKEmbedInput(params, self) {
18225
18599
  metadata: extractMetadataFromEmbedParams(params, self)
18226
18600
  };
18227
18601
  }
18602
+ function prepareAISDKGenerateImageInput(params, self) {
18603
+ return {
18604
+ input: processAISDKGenerateImageInput(params).input,
18605
+ metadata: extractMetadataFromCallParams(params, self)
18606
+ };
18607
+ }
18228
18608
  function prepareAISDKRerankInput(params, self) {
18229
18609
  const { documents, query } = params;
18230
18610
  return {
@@ -19600,6 +19980,57 @@ function processAISDKOutput(output, denyOutputPaths) {
19600
19980
  }
19601
19981
  return normalizeAISDKLoggedOutput(sanitized);
19602
19982
  }
19983
+ function processAISDKGenerateImageOutput(output, denyOutputPaths) {
19984
+ if (!output || typeof output !== "object") {
19985
+ return output;
19986
+ }
19987
+ const summarized = {};
19988
+ for (const field of [
19989
+ "usage",
19990
+ "warnings",
19991
+ "providerMetadata",
19992
+ "experimental_providerMetadata",
19993
+ "responses"
19994
+ ]) {
19995
+ const value = safeSerializableFieldRead(output, field);
19996
+ if (value !== void 0 && isSerializableOutputValue(value)) {
19997
+ summarized[field] = value;
19998
+ }
19999
+ }
20000
+ const images = safeSerializableFieldRead(output, "images");
20001
+ const image = safeSerializableFieldRead(output, "image");
20002
+ const generatedFiles = Array.isArray(images) && images.length > 0 ? images : image !== void 0 ? [image] : [];
20003
+ const loggedOutput = normalizeAISDKLoggedOutput(
20004
+ omit(summarized, denyOutputPaths)
20005
+ );
20006
+ if (generatedFiles.length > 0) {
20007
+ loggedOutput.images = generatedFiles.map(
20008
+ (file, index) => convertAISDKGeneratedFileToAttachment(file, index)
20009
+ );
20010
+ }
20011
+ return loggedOutput;
20012
+ }
20013
+ function convertAISDKGeneratedFileToAttachment(file, index) {
20014
+ if (!file || typeof file !== "object") {
20015
+ return file;
20016
+ }
20017
+ const generatedFile = file;
20018
+ const generatedMediaType = safeSerializableFieldRead(
20019
+ generatedFile,
20020
+ "mediaType"
20021
+ );
20022
+ const mediaType = typeof generatedMediaType === "string" ? generatedMediaType : "application/octet-stream";
20023
+ const data = safeSerializableFieldRead(generatedFile, "base64") ?? safeSerializableFieldRead(generatedFile, "uint8Array");
20024
+ const blob = convertDataToBlob(data, mediaType);
20025
+ if (blob) {
20026
+ return new Attachment({
20027
+ data: blob,
20028
+ filename: `generated_image_${index}.${getExtensionFromMediaType(mediaType)}`,
20029
+ contentType: mediaType
20030
+ });
20031
+ }
20032
+ return file;
20033
+ }
19603
20034
  function processAISDKEmbeddingOutput(output, denyOutputPaths) {
19604
20035
  if (!output || typeof output !== "object") {
19605
20036
  return output;
@@ -20100,118 +20531,22 @@ var claudeAgentSDKChannels = defineChannels(
20100
20531
  var CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION = "__braintrust_skip_local_tool_hooks";
20101
20532
 
20102
20533
  // src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts
20103
- var LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED = /* @__PURE__ */ Symbol.for(
20104
- "braintrust.claude_agent_sdk.local_tool_context_async_iterator_patched"
20105
- );
20106
- function createLocalToolContextStore() {
20107
- const maybeIsoWithAsyncLocalStorage = isomorph_default;
20108
- if (typeof maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage === "function") {
20109
- return maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage();
20110
- }
20111
- let currentStore;
20112
- return {
20113
- enterWith(store) {
20114
- currentStore = store;
20115
- },
20116
- getStore() {
20117
- return currentStore;
20118
- },
20119
- run(store, callback) {
20120
- const previousStore = currentStore;
20121
- currentStore = store;
20122
- try {
20123
- return callback();
20124
- } finally {
20125
- currentStore = previousStore;
20126
- }
20127
- }
20128
- };
20129
- }
20130
- var localToolContextStore = createLocalToolContextStore();
20131
- var fallbackLocalToolParentResolver;
20132
- function createClaudeLocalToolContext() {
20133
- return {};
20134
- }
20135
- function runWithClaudeLocalToolContext(callback, context) {
20136
- return localToolContextStore.run(
20137
- context ?? createClaudeLocalToolContext(),
20138
- callback
20139
- );
20140
- }
20141
- function ensureClaudeLocalToolContext() {
20142
- const existing = localToolContextStore.getStore();
20143
- if (existing) {
20144
- return existing;
20145
- }
20146
- const created = {};
20147
- localToolContextStore.enterWith(created);
20148
- return created;
20149
- }
20150
- function setClaudeLocalToolParentResolver(resolver) {
20151
- fallbackLocalToolParentResolver = resolver;
20152
- const context = ensureClaudeLocalToolContext();
20153
- if (!context) {
20154
- return;
20155
- }
20156
- context.resolveLocalToolParent = resolver;
20157
- }
20158
- function getClaudeLocalToolParentResolver() {
20159
- return localToolContextStore.getStore()?.resolveLocalToolParent ?? fallbackLocalToolParentResolver;
20534
+ var localToolContextStore = isomorph_default.newAsyncLocalStorage();
20535
+ var localToolParentResolversByToolUseId = /* @__PURE__ */ new Map();
20536
+ function runWithClaudeLocalToolContext(callback, resolver) {
20537
+ return localToolContextStore.run(resolver, callback);
20160
20538
  }
20161
- function isAsyncIterable3(value) {
20162
- return value !== null && typeof value === "object" && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
20539
+ function registerClaudeLocalToolParentResolver(toolUseId, resolver) {
20540
+ localToolParentResolversByToolUseId.set(toolUseId, resolver);
20163
20541
  }
20164
- function bindClaudeLocalToolContextToAsyncIterable(result, localToolContext) {
20165
- if (!isAsyncIterable3(result) || Object.isFrozen(result) || Object.isSealed(result)) {
20166
- return result;
20167
- }
20168
- const stream = result;
20169
- const originalAsyncIterator = stream[Symbol.asyncIterator];
20170
- if (originalAsyncIterator[LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED]) {
20171
- return result;
20542
+ function getClaudeLocalToolParentResolver(toolUseId) {
20543
+ const currentResolver = localToolContextStore.getStore();
20544
+ if (!toolUseId) {
20545
+ return currentResolver;
20172
20546
  }
20173
- const patchedAsyncIterator = function() {
20174
- return runWithClaudeLocalToolContext(() => {
20175
- const iterator = Reflect.apply(originalAsyncIterator, this, []);
20176
- if (!iterator || typeof iterator !== "object") {
20177
- return iterator;
20178
- }
20179
- const patchMethod = (methodName) => {
20180
- const originalMethod = Reflect.get(iterator, methodName);
20181
- if (typeof originalMethod !== "function") {
20182
- return;
20183
- }
20184
- Reflect.set(
20185
- iterator,
20186
- methodName,
20187
- (...args) => runWithClaudeLocalToolContext(
20188
- () => Reflect.apply(
20189
- originalMethod,
20190
- iterator,
20191
- args
20192
- ),
20193
- localToolContext
20194
- )
20195
- );
20196
- };
20197
- patchMethod("next");
20198
- patchMethod("return");
20199
- patchMethod("throw");
20200
- return iterator;
20201
- }, localToolContext);
20202
- };
20203
- Object.defineProperty(
20204
- patchedAsyncIterator,
20205
- LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED,
20206
- {
20207
- configurable: false,
20208
- enumerable: false,
20209
- value: true,
20210
- writable: false
20211
- }
20212
- );
20213
- Reflect.set(stream, Symbol.asyncIterator, patchedAsyncIterator);
20214
- return result;
20547
+ const registeredResolver = localToolParentResolversByToolUseId.get(toolUseId);
20548
+ localToolParentResolversByToolUseId.delete(toolUseId);
20549
+ return currentResolver ?? registeredResolver;
20215
20550
  }
20216
20551
 
20217
20552
  // src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts
@@ -20237,7 +20572,7 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
20237
20572
  const metadata = getMetadata();
20238
20573
  const rawToolName = metadata.serverName ? `mcp__${metadata.serverName}__${metadata.toolName}` : metadata.toolName;
20239
20574
  const toolUseId = getToolUseIdFromExtra(handlerArgs[1]);
20240
- const localToolParentResolver = getClaudeLocalToolParentResolver();
20575
+ const localToolParentResolver = getClaudeLocalToolParentResolver(toolUseId);
20241
20576
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
20242
20577
  const runWithResolvedParent = async () => {
20243
20578
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
@@ -20764,6 +21099,7 @@ function createToolTracingHooks(resolveParentSpan, taskIdToToolUseId, toolUseToP
20764
21099
  }
20765
21100
  }
20766
21101
  if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
21102
+ registerClaudeLocalToolParentResolver(toolUseID, resolveParentSpan);
20767
21103
  return {};
20768
21104
  }
20769
21105
  const parsed = parseToolName(input.tool_name);
@@ -21474,7 +21810,7 @@ async function finalizeQuerySpan(state) {
21474
21810
  }
21475
21811
  var ClaudeAgentSDKPlugin = class extends BasePlugin {
21476
21812
  onEnable() {
21477
- this.subscribeToQuery();
21813
+ this.interceptQuery();
21478
21814
  }
21479
21815
  onDisable() {
21480
21816
  for (const unsubscribe of this.unsubscribers) {
@@ -21482,211 +21818,218 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
21482
21818
  }
21483
21819
  this.unsubscribers = [];
21484
21820
  }
21485
- subscribeToQuery() {
21486
- const channel2 = claudeAgentSDKChannels.query.tracingChannel();
21487
- const spans = /* @__PURE__ */ new WeakMap();
21488
- const handlers = {
21489
- start: (event) => {
21490
- const params = event.arguments[0] ?? {};
21491
- const originalPrompt = params.prompt;
21492
- const options = params.options ?? {};
21493
- const promptIsAsyncIterable = isAsyncIterable(originalPrompt);
21494
- let promptStarted = false;
21495
- let capturedPromptMessages;
21496
- let resolvePromptDone;
21497
- const promptDone = new Promise((resolve) => {
21498
- resolvePromptDone = resolve;
21499
- });
21500
- if (promptIsAsyncIterable) {
21501
- capturedPromptMessages = [];
21502
- const promptStream = originalPrompt;
21503
- params.prompt = (async function* () {
21504
- promptStarted = true;
21505
- try {
21506
- for await (const message of promptStream) {
21507
- capturedPromptMessages.push(message);
21508
- yield message;
21509
- }
21510
- } finally {
21511
- resolvePromptDone?.();
21821
+ interceptQuery() {
21822
+ const startQuery = (params) => {
21823
+ const originalPrompt = params.prompt;
21824
+ const options = params.options ?? {};
21825
+ const promptIsAsyncIterable = isAsyncIterable(originalPrompt);
21826
+ let promptStarted = false;
21827
+ let capturedPromptMessages;
21828
+ let resolvePromptDone;
21829
+ const promptDone = new Promise((resolve) => {
21830
+ resolvePromptDone = resolve;
21831
+ });
21832
+ if (promptIsAsyncIterable) {
21833
+ capturedPromptMessages = [];
21834
+ const promptStream = originalPrompt;
21835
+ params.prompt = (async function* () {
21836
+ promptStarted = true;
21837
+ try {
21838
+ for await (const message of promptStream) {
21839
+ capturedPromptMessages.push(message);
21840
+ yield message;
21512
21841
  }
21513
- })();
21514
- }
21515
- const span = startSpan(
21516
- withSpanInstrumentationName(
21517
- {
21518
- name: "Claude Agent",
21519
- spanAttributes: {
21520
- type: "task" /* TASK */
21521
- }
21522
- },
21523
- INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
21524
- )
21525
- );
21526
- const startTime = getCurrentUnixTimestamp();
21527
- try {
21528
- span.log({
21529
- input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
21530
- metadata: filterSerializableOptions(options)
21531
- });
21532
- } catch (error) {
21533
- console.error("Error extracting input for Claude Agent SDK:", error);
21534
- }
21535
- const activeToolSpans = /* @__PURE__ */ new Map();
21536
- const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
21537
- const conversationHistoryByParentKey = /* @__PURE__ */ new Map();
21538
- const subAgentSpans = /* @__PURE__ */ new Map();
21539
- const endedSubAgentSpans = /* @__PURE__ */ new Set();
21540
- const toolUseToParent = /* @__PURE__ */ new Map();
21541
- const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
21542
- const latestRootLlmParentRef = {
21543
- value: void 0
21544
- };
21545
- const subAgentDetailsByToolUseId = /* @__PURE__ */ new Map();
21546
- const taskIdToToolUseId = /* @__PURE__ */ new Map();
21547
- const promptMessagesByParentKey = /* @__PURE__ */ new Map();
21548
- const promptSourcePriorityByParentKey = /* @__PURE__ */ new Map();
21549
- const localToolContext = createClaudeLocalToolContext();
21550
- const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
21551
- const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
21552
- const resolveToolUseParentSpan = async (toolUseID, context) => {
21553
- const trackedParentToolUseId = toolUseToParent.get(toolUseID);
21554
- const parentToolUseId = trackedParentToolUseId ?? (context?.agentId ? taskIdToToolUseId.get(context.agentId) ?? null : null);
21555
- const parentKey = llmParentKey(parentToolUseId);
21556
- const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
21557
- const latestLlmParent = parentToolUseId ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value;
21558
- if (!activeLlmSpan && !latestLlmParent) {
21559
- await ensureActiveLlmSpanForParentToolUse(
21560
- span,
21561
- activeLlmSpansByParentToolUse,
21562
- subAgentDetailsByToolUseId,
21563
- activeToolSpans,
21564
- subAgentSpans,
21565
- parentToolUseId,
21566
- getCurrentUnixTimestamp()
21567
- );
21568
- }
21569
- if (parentToolUseId) {
21570
- const subAgentSpan = await ensureSubAgentSpan(
21571
- subAgentDetailsByToolUseId,
21572
- span,
21573
- activeToolSpans,
21574
- subAgentSpans,
21575
- parentToolUseId
21576
- );
21577
- return subAgentSpan.export();
21842
+ } finally {
21843
+ resolvePromptDone?.();
21578
21844
  }
21579
- return span.export();
21580
- };
21581
- localToolContext.resolveLocalToolParent = resolveToolUseParentSpan;
21582
- setClaudeLocalToolParentResolver(resolveToolUseParentSpan);
21583
- const optionsWithHooks = injectTracingHooks(
21584
- options,
21585
- resolveToolUseParentSpan,
21586
- taskIdToToolUseId,
21587
- toolUseToParent,
21588
- activeToolSpans,
21589
- localToolHookNames,
21590
- skipLocalToolHooks,
21591
- subAgentDetailsByToolUseId,
21592
- subAgentSpans,
21593
- endedSubAgentSpans
21594
- );
21595
- params.options = optionsWithHooks;
21596
- event.arguments[0] = params;
21597
- spans.set(event, {
21598
- activeLlmSpansByParentToolUse,
21599
- activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
21600
- activeToolSpans,
21601
- conversationHistoryByParentKey,
21602
- capturedPromptMessages,
21603
- currentMessageId: void 0,
21604
- currentMessageStartTime: startTime,
21605
- currentMessages: [],
21606
- endedSubAgentSpans,
21607
- finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
21608
- finalResults: [],
21609
- options: optionsWithHooks,
21610
- originalPrompt,
21611
- processing: Promise.resolve(),
21612
- promptDone,
21613
- promptMessagesByParentKey,
21614
- promptStarted: () => promptStarted,
21615
- promptSourcePriorityByParentKey,
21616
- span,
21617
- subAgentDetailsByToolUseId,
21618
- subAgentSpans,
21619
- taskIdToToolUseId,
21620
- latestLlmParentBySubAgentToolUse,
21621
- latestRootLlmParentRef,
21622
- toolUseToParent,
21623
- usageByMessageId: /* @__PURE__ */ new Map(),
21624
- localToolContext
21845
+ })();
21846
+ }
21847
+ const span = startSpan(
21848
+ withSpanInstrumentationName(
21849
+ {
21850
+ name: "Claude Agent",
21851
+ spanAttributes: {
21852
+ type: "task" /* TASK */
21853
+ }
21854
+ },
21855
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
21856
+ )
21857
+ );
21858
+ const startTime = getCurrentUnixTimestamp();
21859
+ try {
21860
+ span.log({
21861
+ input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
21862
+ metadata: filterSerializableOptions(options)
21625
21863
  });
21626
- },
21627
- end: (event) => {
21628
- const state = spans.get(event);
21629
- if (!state) {
21630
- return;
21631
- }
21632
- const eventResult = bindClaudeLocalToolContextToAsyncIterable(
21633
- event.result,
21634
- state.localToolContext
21635
- );
21636
- if (eventResult === void 0) {
21637
- state.span.end();
21638
- spans.delete(event);
21639
- return;
21640
- }
21641
- if (isAsyncIterable(eventResult)) {
21642
- patchStreamIfNeeded(eventResult, {
21643
- onChunk: (message) => {
21644
- maybeTrackToolUseContext(state, message);
21645
- state.processing = state.processing.then(() => handleStreamMessage(state, message)).catch((error) => {
21646
- console.error(
21647
- "Error processing Claude Agent SDK stream chunk:",
21648
- error
21649
- );
21650
- });
21651
- },
21652
- onComplete: () => state.processing.then(() => finalizeQuerySpan(state)).finally(() => {
21653
- spans.delete(event);
21654
- }),
21655
- onError: (error) => state.processing.then(() => {
21656
- state.span.log({
21657
- error: error.message
21658
- });
21659
- }).then(() => finalizeQuerySpan(state)).finally(() => {
21660
- spans.delete(event);
21661
- })
21662
- });
21663
- return;
21664
- }
21665
- try {
21666
- state.span.log({ output: eventResult });
21667
- } catch (error) {
21668
- console.error("Error extracting output for Claude Agent SDK:", error);
21669
- } finally {
21670
- state.span.end();
21671
- spans.delete(event);
21864
+ } catch (error) {
21865
+ console.error("Error extracting input for Claude Agent SDK:", error);
21866
+ }
21867
+ const activeToolSpans = /* @__PURE__ */ new Map();
21868
+ const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
21869
+ const conversationHistoryByParentKey = /* @__PURE__ */ new Map();
21870
+ const subAgentSpans = /* @__PURE__ */ new Map();
21871
+ const endedSubAgentSpans = /* @__PURE__ */ new Set();
21872
+ const toolUseToParent = /* @__PURE__ */ new Map();
21873
+ const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
21874
+ const latestRootLlmParentRef = {
21875
+ value: void 0
21876
+ };
21877
+ const subAgentDetailsByToolUseId = /* @__PURE__ */ new Map();
21878
+ const taskIdToToolUseId = /* @__PURE__ */ new Map();
21879
+ const promptMessagesByParentKey = /* @__PURE__ */ new Map();
21880
+ const promptSourcePriorityByParentKey = /* @__PURE__ */ new Map();
21881
+ const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
21882
+ const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
21883
+ const resolveToolUseParentSpan = async (toolUseID, context) => {
21884
+ const trackedParentToolUseId = toolUseToParent.get(toolUseID);
21885
+ const parentToolUseId = trackedParentToolUseId ?? (context?.agentId ? taskIdToToolUseId.get(context.agentId) ?? null : null);
21886
+ const parentKey = llmParentKey(parentToolUseId);
21887
+ const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
21888
+ const latestLlmParent = parentToolUseId ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value;
21889
+ if (!activeLlmSpan && !latestLlmParent) {
21890
+ await ensureActiveLlmSpanForParentToolUse(
21891
+ span,
21892
+ activeLlmSpansByParentToolUse,
21893
+ subAgentDetailsByToolUseId,
21894
+ activeToolSpans,
21895
+ subAgentSpans,
21896
+ parentToolUseId,
21897
+ getCurrentUnixTimestamp()
21898
+ );
21672
21899
  }
21673
- },
21674
- error: (event) => {
21675
- const state = spans.get(event);
21676
- if (!state || !event.error) {
21677
- return;
21900
+ if (parentToolUseId) {
21901
+ const subAgentSpan = await ensureSubAgentSpan(
21902
+ subAgentDetailsByToolUseId,
21903
+ span,
21904
+ activeToolSpans,
21905
+ subAgentSpans,
21906
+ parentToolUseId
21907
+ );
21908
+ return subAgentSpan.export();
21678
21909
  }
21679
- state.span.log({
21680
- error: event.error.message
21910
+ return span.export();
21911
+ };
21912
+ const optionsWithHooks = injectTracingHooks(
21913
+ options,
21914
+ resolveToolUseParentSpan,
21915
+ taskIdToToolUseId,
21916
+ toolUseToParent,
21917
+ activeToolSpans,
21918
+ localToolHookNames,
21919
+ skipLocalToolHooks,
21920
+ subAgentDetailsByToolUseId,
21921
+ subAgentSpans,
21922
+ endedSubAgentSpans
21923
+ );
21924
+ params.options = optionsWithHooks;
21925
+ return {
21926
+ activeLlmSpansByParentToolUse,
21927
+ activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
21928
+ activeToolSpans,
21929
+ conversationHistoryByParentKey,
21930
+ capturedPromptMessages,
21931
+ currentMessageId: void 0,
21932
+ currentMessageStartTime: startTime,
21933
+ currentMessages: [],
21934
+ endedSubAgentSpans,
21935
+ finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
21936
+ finalResults: [],
21937
+ options: optionsWithHooks,
21938
+ originalPrompt,
21939
+ processing: Promise.resolve(),
21940
+ promptDone,
21941
+ promptMessagesByParentKey,
21942
+ promptStarted: () => promptStarted,
21943
+ promptSourcePriorityByParentKey,
21944
+ span,
21945
+ subAgentDetailsByToolUseId,
21946
+ subAgentSpans,
21947
+ taskIdToToolUseId,
21948
+ latestLlmParentBySubAgentToolUse,
21949
+ latestRootLlmParentRef,
21950
+ toolUseToParent,
21951
+ usageByMessageId: /* @__PURE__ */ new Map(),
21952
+ localToolParentResolver: resolveToolUseParentSpan
21953
+ };
21954
+ };
21955
+ const finishQuery = (state, result) => {
21956
+ if (isAsyncIterable(result)) {
21957
+ patchStreamIfNeeded(result, {
21958
+ aroundNext: (callback) => runWithClaudeLocalToolContext(
21959
+ callback,
21960
+ state.localToolParentResolver
21961
+ ),
21962
+ onChunk: (message) => {
21963
+ maybeTrackToolUseContext(state, message);
21964
+ state.processing = state.processing.then(() => handleStreamMessage(state, message)).catch((error) => {
21965
+ console.error(
21966
+ "Error processing Claude Agent SDK stream chunk:",
21967
+ error
21968
+ );
21969
+ });
21970
+ },
21971
+ onComplete: () => state.processing.then(() => finalizeQuerySpan(state)),
21972
+ onError: (error) => state.processing.then(() => {
21973
+ state.span.log({ error: error.message });
21974
+ }).then(() => finalizeQuerySpan(state))
21681
21975
  });
21976
+ return;
21977
+ }
21978
+ try {
21979
+ state.span.log({ output: result });
21980
+ } catch (error) {
21981
+ console.error("Error extracting output for Claude Agent SDK:", error);
21982
+ } finally {
21682
21983
  state.span.end();
21683
- spans.delete(event);
21684
21984
  }
21685
21985
  };
21686
- channel2.subscribe(handlers);
21687
- this.unsubscribers.push(() => {
21688
- channel2.unsubscribe(handlers);
21689
- });
21986
+ this.unsubscribers.push(
21987
+ claudeAgentSDKChannels.query.intercept((target, thisArg, args) => {
21988
+ let state;
21989
+ try {
21990
+ args[0] ??= {};
21991
+ state = startQuery(args[0]);
21992
+ } catch (error) {
21993
+ debugLogger.error(
21994
+ "Error starting Claude Agent SDK instrumentation:",
21995
+ error
21996
+ );
21997
+ }
21998
+ const invokeTarget = () => Reflect.apply(target, thisArg, args);
21999
+ try {
22000
+ const result = state ? runWithClaudeLocalToolContext(
22001
+ invokeTarget,
22002
+ state.localToolParentResolver
22003
+ ) : invokeTarget();
22004
+ if (state) {
22005
+ try {
22006
+ finishQuery(state, result);
22007
+ } catch (error) {
22008
+ debugLogger.error(
22009
+ "Error finalizing Claude Agent SDK instrumentation:",
22010
+ error
22011
+ );
22012
+ }
22013
+ }
22014
+ return result;
22015
+ } catch (error) {
22016
+ if (state) {
22017
+ try {
22018
+ state.span.log({
22019
+ error: error instanceof Error ? error.message : String(error)
22020
+ });
22021
+ state.span.end();
22022
+ } catch (instrumentationError) {
22023
+ debugLogger.error(
22024
+ "Error handling Claude Agent SDK instrumentation failure:",
22025
+ instrumentationError
22026
+ );
22027
+ }
22028
+ }
22029
+ throw error;
22030
+ }
22031
+ })
22032
+ );
21690
22033
  }
21691
22034
  };
21692
22035
 
@@ -25800,7 +26143,7 @@ function patchOpenRouterCallModelResult(args) {
25800
26143
  span,
25801
26144
  () => originalMethod.apply(resultLike, args2)
25802
26145
  );
25803
- if (!isAsyncIterable4(stream)) {
26146
+ if (!isAsyncIterable3(stream)) {
25804
26147
  return stream;
25805
26148
  }
25806
26149
  return wrapAsyncIterableWithSpan({
@@ -25995,7 +26338,7 @@ function wrapAsyncIterableWithSpan(args) {
25995
26338
  }
25996
26339
  };
25997
26340
  }
25998
- function isAsyncIterable4(value) {
26341
+ function isAsyncIterable3(value) {
25999
26342
  return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
26000
26343
  }
26001
26344
  function normalizeError(error) {
@@ -26873,7 +27216,7 @@ function patchOpenRouterCallModelResult2(args) {
26873
27216
  span,
26874
27217
  () => originalMethod.apply(resultLike, args2)
26875
27218
  );
26876
- if (!isAsyncIterable5(stream)) {
27219
+ if (!isAsyncIterable4(stream)) {
26877
27220
  return stream;
26878
27221
  }
26879
27222
  return wrapAsyncIterableWithSpan2({
@@ -27068,7 +27411,7 @@ function wrapAsyncIterableWithSpan2(args) {
27068
27411
  }
27069
27412
  };
27070
27413
  }
27071
- function isAsyncIterable5(value) {
27414
+ function isAsyncIterable4(value) {
27072
27415
  return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
27073
27416
  }
27074
27417
  function normalizeError2(error) {
@@ -33060,12 +33403,14 @@ function getMetricsFromResponse(response) {
33060
33403
  continue;
33061
33404
  }
33062
33405
  const inputTokenDetails = usageMetadata.input_token_details;
33406
+ const outputTokenDetails = usageMetadata.output_token_details;
33063
33407
  return normalizeTokenMetrics({
33064
33408
  total_tokens: usageMetadata.total_tokens,
33065
33409
  prompt_tokens: usageMetadata.input_tokens,
33066
33410
  completion_tokens: usageMetadata.output_tokens,
33067
33411
  prompt_cache_creation_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_creation : void 0,
33068
- prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0
33412
+ prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0,
33413
+ completion_reasoning_tokens: isRecord(outputTokenDetails) ? outputTokenDetails.reasoning : void 0
33069
33414
  });
33070
33415
  }
33071
33416
  const llmOutput = response.llmOutput || {};
@@ -33652,6 +33997,9 @@ var piCodingAgentChannels = defineChannels(
33652
33997
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
33653
33998
  var piAgentPatchStates = /* @__PURE__ */ new WeakMap();
33654
33999
  var piAgentEventSubscriptions = /* @__PURE__ */ new WeakSet();
34000
+ var PI_TOOL_EXECUTE_WRAPPED = /* @__PURE__ */ Symbol.for(
34001
+ "braintrust.pi_coding_agent.tool_execute_wrapped"
34002
+ );
33655
34003
  var piPromptContextStore;
33656
34004
  var PiCodingAgentPlugin = class extends BasePlugin {
33657
34005
  activePromptStates = /* @__PURE__ */ new Set();
@@ -33732,6 +34080,7 @@ function startPiPromptRun(event, onFinalize) {
33732
34080
  return void 0;
33733
34081
  }
33734
34082
  installPiAgentInstrumentation(agent);
34083
+ wrapPiToolExecutors(agent.state?.tools);
33735
34084
  const metadata = {
33736
34085
  ...extractSessionMetadata(session),
33737
34086
  ...extractPromptOptionsMetadata(event.arguments[1]),
@@ -33828,6 +34177,7 @@ function makeInstrumentedStreamFn(agent, originalStreamFn) {
33828
34177
  if (!state || state.agent !== agent || state.finalized) {
33829
34178
  return invokeOriginal();
33830
34179
  }
34180
+ wrapPiToolExecutors(context.tools);
33831
34181
  const llmState = await startPiLlmSpan(state, model, context, options);
33832
34182
  try {
33833
34183
  const stream = await runWithAutoInstrumentationSuppressed(invokeOriginal);
@@ -33838,6 +34188,33 @@ function makeInstrumentedStreamFn(agent, originalStreamFn) {
33838
34188
  }
33839
34189
  };
33840
34190
  }
34191
+ function wrapPiToolExecutors(tools) {
34192
+ if (!tools) {
34193
+ return;
34194
+ }
34195
+ for (const tool of tools) {
34196
+ try {
34197
+ const execute = tool.execute;
34198
+ if (typeof execute !== "function" || execute[PI_TOOL_EXECUTE_WRAPPED]) {
34199
+ continue;
34200
+ }
34201
+ const wrappedExecute = function(...args) {
34202
+ return runWithAutoInstrumentationAllowed(
34203
+ () => Reflect.apply(execute, this, args)
34204
+ );
34205
+ };
34206
+ Object.defineProperty(wrappedExecute, PI_TOOL_EXECUTE_WRAPPED, {
34207
+ configurable: false,
34208
+ enumerable: false,
34209
+ value: true,
34210
+ writable: false
34211
+ });
34212
+ tool.execute = wrappedExecute;
34213
+ } catch (error) {
34214
+ logInstrumentationError4("Pi Coding Agent tool wrapping", error);
34215
+ }
34216
+ }
34217
+ }
33841
34218
  async function startPiLlmSpan(state, model, context, options) {
33842
34219
  const metadata = {
33843
34220
  ...extractModelMetadata2(model),
@@ -34009,35 +34386,26 @@ async function startPiToolSpan(state, event) {
34009
34386
  if (!event.toolCallId || state.activeToolSpans.has(event.toolCallId)) {
34010
34387
  return;
34011
34388
  }
34012
- const restoreAutoInstrumentation = enterAutoInstrumentationAllowed();
34013
34389
  const metadata = {
34014
34390
  "gen_ai.tool.call.id": event.toolCallId,
34015
34391
  "gen_ai.tool.name": event.toolName,
34016
34392
  "pi_coding_agent.tool.name": event.toolName
34017
34393
  };
34018
- try {
34019
- const span = startSpan(
34020
- withSpanInstrumentationName(
34021
- {
34022
- event: {
34023
- input: event.args,
34024
- metadata
34025
- },
34026
- name: event.toolName || "tool",
34027
- parent: await state.span.export(),
34028
- spanAttributes: { type: "tool" /* TOOL */ }
34394
+ const span = startSpan(
34395
+ withSpanInstrumentationName(
34396
+ {
34397
+ event: {
34398
+ input: event.args,
34399
+ metadata
34029
34400
  },
34030
- INSTRUMENTATION_NAMES.PI_CODING_AGENT
34031
- )
34032
- );
34033
- state.activeToolSpans.set(event.toolCallId, {
34034
- restoreAutoInstrumentation,
34035
- span
34036
- });
34037
- } catch (error) {
34038
- restoreAutoInstrumentation();
34039
- throw error;
34040
- }
34401
+ name: event.toolName || "tool",
34402
+ parent: await state.span.export(),
34403
+ spanAttributes: { type: "tool" /* TOOL */ }
34404
+ },
34405
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
34406
+ )
34407
+ );
34408
+ state.activeToolSpans.set(event.toolCallId, { span });
34041
34409
  }
34042
34410
  function finishPiToolSpan(state, event) {
34043
34411
  const toolState = state.activeToolSpans.get(event.toolCallId);
@@ -34058,11 +34426,7 @@ function finishPiToolSpan(state, event) {
34058
34426
  output: event.result
34059
34427
  });
34060
34428
  } finally {
34061
- try {
34062
- toolState.span.end();
34063
- } finally {
34064
- toolState.restoreAutoInstrumentation?.();
34065
- }
34429
+ toolState.span.end();
34066
34430
  }
34067
34431
  }
34068
34432
  function finishPiPromptRun(state, error) {
@@ -34112,10 +34476,10 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
34112
34476
  ...cleanMetrics5(llmState.metrics),
34113
34477
  ...buildDurationMetrics3(llmState.startTime)
34114
34478
  };
34115
- const usageMetrics = extractUsageMetrics2(message?.usage);
34116
- if (Object.keys(usageMetrics).length > 0) {
34479
+ const usageMetrics2 = extractUsageMetrics2(message?.usage);
34480
+ if (Object.keys(usageMetrics2).length > 0) {
34117
34481
  promptState.collectedLlmUsageMetrics = true;
34118
- addMetrics(promptState.metrics, usageMetrics);
34482
+ addMetrics(promptState.metrics, usageMetrics2);
34119
34483
  }
34120
34484
  try {
34121
34485
  safeLog4(llmState.span, {
@@ -34133,14 +34497,10 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
34133
34497
  }
34134
34498
  function finishOpenToolSpans(state, error) {
34135
34499
  for (const [, toolState] of state.activeToolSpans) {
34136
- try {
34137
- safeLog4(toolState.span, {
34138
- error: error ? toLoggedError(error) : "Pi tool did not complete"
34139
- });
34140
- toolState.span.end();
34141
- } finally {
34142
- toolState.restoreAutoInstrumentation?.();
34143
- }
34500
+ safeLog4(toolState.span, {
34501
+ error: error ? toLoggedError(error) : "Pi tool did not complete"
34502
+ });
34503
+ toolState.span.end();
34144
34504
  }
34145
34505
  state.activeToolSpans.clear();
34146
34506
  }
@@ -34459,12 +34819,12 @@ var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
34459
34819
  var StrandsAgentSDKPlugin = class extends BasePlugin {
34460
34820
  activeChildParents = /* @__PURE__ */ new WeakMap();
34461
34821
  onEnable() {
34462
- this.subscribeToAgentStream();
34463
- this.subscribeToMultiAgentStream(
34822
+ this.interceptAgentStream();
34823
+ this.interceptMultiAgentStream(
34464
34824
  strandsAgentSDKChannels.graphStream,
34465
34825
  "Graph.stream"
34466
34826
  );
34467
- this.subscribeToMultiAgentStream(
34827
+ this.interceptMultiAgentStream(
34468
34828
  strandsAgentSDKChannels.swarmStream,
34469
34829
  "Swarm.stream"
34470
34830
  );
@@ -34475,122 +34835,92 @@ var StrandsAgentSDKPlugin = class extends BasePlugin {
34475
34835
  }
34476
34836
  this.unsubscribers = [];
34477
34837
  }
34478
- subscribeToAgentStream() {
34479
- const channel2 = strandsAgentSDKChannels.agentStream.tracingChannel();
34480
- const states = /* @__PURE__ */ new WeakMap();
34481
- const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(channel2);
34482
- const handlers = {
34483
- start: (event) => {
34484
- const state = startAgentStream(event, this.activeChildParents);
34485
- if (state) {
34486
- states.set(event, state);
34487
- }
34488
- },
34489
- end: (event) => {
34490
- const state = states.get(event);
34491
- if (!state) {
34492
- return;
34493
- }
34494
- const result = event.result;
34495
- if (isAsyncIterable(result)) {
34496
- patchStreamIfNeeded(result, {
34497
- aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
34498
- onChunk: (chunk) => handleAgentStreamEvent(state, chunk),
34499
- onComplete: () => {
34500
- finalizeAgentStream(state);
34501
- states.delete(event);
34502
- },
34503
- onError: (error) => {
34504
- finalizeAgentStream(state, error);
34505
- states.delete(event);
34506
- }
34507
- });
34508
- return;
34509
- }
34510
- finalizeAgentStream(state, void 0, result);
34511
- states.delete(event);
34512
- },
34513
- error: (event) => {
34514
- const state = states.get(event);
34515
- if (!state || !event.error) {
34516
- return;
34517
- }
34518
- finalizeAgentStream(state, event.error);
34519
- states.delete(event);
34520
- }
34521
- };
34522
- channel2.subscribe(handlers);
34523
- this.unsubscribers.push(() => {
34524
- unbindAutoInstrumentationSuppression?.();
34525
- channel2.unsubscribe(handlers);
34526
- });
34838
+ interceptAgentStream() {
34839
+ this.unsubscribers.push(
34840
+ strandsAgentSDKChannels.agentStream.intercept(
34841
+ (target, thisArg, args, additional) => instrumentStrandsStreamInvocation({
34842
+ finalize: finalizeAgentStream,
34843
+ handleChunk: handleAgentStreamEvent,
34844
+ invoke: () => Reflect.apply(target, thisArg, args),
34845
+ name: "Strands Agent SDK",
34846
+ start: () => startAgentStream(
34847
+ args[0],
34848
+ extractAgent(additional.agent, thisArg),
34849
+ this.activeChildParents
34850
+ )
34851
+ })
34852
+ )
34853
+ );
34527
34854
  }
34528
- subscribeToMultiAgentStream(channel2, operation) {
34529
- const tracingChannel = channel2.tracingChannel();
34530
- const states = /* @__PURE__ */ new WeakMap();
34531
- const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(tracingChannel);
34532
- const handlers = {
34533
- start: (event) => {
34534
- const state = startMultiAgentStream(
34535
- event,
34536
- operation,
34537
- this.activeChildParents
34538
- );
34539
- if (state) {
34540
- states.set(event, state);
34541
- }
34542
- },
34543
- end: (event) => {
34544
- const state = states.get(event);
34545
- if (!state) {
34546
- return;
34547
- }
34548
- const result = event.result;
34549
- if (isAsyncIterable(result)) {
34550
- patchStreamIfNeeded(result, {
34551
- aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
34552
- onChunk: (chunk) => handleMultiAgentStreamEvent(
34553
- state,
34554
- chunk,
34555
- this.activeChildParents
34556
- ),
34557
- onComplete: () => {
34558
- finalizeMultiAgentStream(state, this.activeChildParents);
34559
- states.delete(event);
34560
- },
34561
- onError: (error) => {
34562
- finalizeMultiAgentStream(state, this.activeChildParents, error);
34563
- states.delete(event);
34564
- }
34565
- });
34566
- return;
34567
- }
34568
- finalizeMultiAgentStream(
34569
- state,
34570
- this.activeChildParents,
34571
- void 0,
34572
- result
34855
+ interceptMultiAgentStream(channel2, operation) {
34856
+ this.unsubscribers.push(
34857
+ channel2.intercept(
34858
+ (target, thisArg, args, additional) => instrumentStrandsStreamInvocation({
34859
+ finalize: (state, error, output) => finalizeMultiAgentStream(
34860
+ state,
34861
+ this.activeChildParents,
34862
+ error,
34863
+ output
34864
+ ),
34865
+ handleChunk: (state, chunk) => handleMultiAgentStreamEvent(state, chunk, this.activeChildParents),
34866
+ invoke: () => Reflect.apply(target, thisArg, args),
34867
+ name: "Strands multi-agent",
34868
+ start: () => startMultiAgentStream(
34869
+ args[0],
34870
+ extractOrchestrator(additional.orchestrator, thisArg),
34871
+ operation,
34872
+ this.activeChildParents
34873
+ )
34874
+ })
34875
+ )
34876
+ );
34877
+ }
34878
+ };
34879
+ function instrumentStrandsStreamInvocation(options) {
34880
+ let state;
34881
+ try {
34882
+ state = options.start();
34883
+ } catch (error) {
34884
+ debugLogger.error(`Error starting ${options.name} instrumentation:`, error);
34885
+ }
34886
+ let result;
34887
+ try {
34888
+ result = runWithAutoInstrumentationSuppressed(options.invoke);
34889
+ } catch (error) {
34890
+ if (state) {
34891
+ try {
34892
+ options.finalize(state, error);
34893
+ } catch (instrumentationError) {
34894
+ debugLogger.error(
34895
+ `Error handling ${options.name} instrumentation failure:`,
34896
+ instrumentationError
34573
34897
  );
34574
- states.delete(event);
34575
- },
34576
- error: (event) => {
34577
- const state = states.get(event);
34578
- if (!state || !event.error) {
34579
- return;
34580
- }
34581
- finalizeMultiAgentStream(state, this.activeChildParents, event.error);
34582
- states.delete(event);
34583
34898
  }
34584
- };
34585
- tracingChannel.subscribe(handlers);
34586
- this.unsubscribers.push(() => {
34587
- unbindAutoInstrumentationSuppression?.();
34588
- tracingChannel.unsubscribe(handlers);
34589
- });
34899
+ }
34900
+ throw error;
34590
34901
  }
34591
- };
34592
- function startAgentStream(event, activeChildParents) {
34593
- const agent = extractAgent(event);
34902
+ if (state) {
34903
+ try {
34904
+ if (isAsyncIterable(result)) {
34905
+ patchStreamIfNeeded(result, {
34906
+ aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
34907
+ onChunk: (chunk) => options.handleChunk(state, chunk),
34908
+ onComplete: () => options.finalize(state),
34909
+ onError: (error) => options.finalize(state, error)
34910
+ });
34911
+ } else {
34912
+ options.finalize(state, void 0, result);
34913
+ }
34914
+ } catch (error) {
34915
+ debugLogger.error(
34916
+ `Error finalizing ${options.name} instrumentation:`,
34917
+ error
34918
+ );
34919
+ }
34920
+ }
34921
+ return result;
34922
+ }
34923
+ function startAgentStream(input, agent, activeChildParents) {
34594
34924
  const model = agent?.model;
34595
34925
  const metadata = {
34596
34926
  ...extractAgentMetadata2(agent),
@@ -34600,17 +34930,14 @@ function startAgentStream(event, activeChildParents) {
34600
34930
  };
34601
34931
  const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
34602
34932
  const attachmentCache = createStrandsAttachmentCache();
34603
- const input = processStrandsInputAttachments(
34604
- event.arguments[0],
34605
- attachmentCache
34606
- );
34933
+ const processedInput = processStrandsInputAttachments(input, attachmentCache);
34607
34934
  const span = parentSpan ? withCurrent(
34608
34935
  parentSpan,
34609
34936
  () => startSpan(
34610
34937
  withSpanInstrumentationName(
34611
34938
  {
34612
34939
  event: {
34613
- input,
34940
+ input: processedInput,
34614
34941
  metadata
34615
34942
  },
34616
34943
  name: formatAgentSpanName(agent),
@@ -34623,7 +34950,7 @@ function startAgentStream(event, activeChildParents) {
34623
34950
  withSpanInstrumentationName(
34624
34951
  {
34625
34952
  event: {
34626
- input,
34953
+ input: processedInput,
34627
34954
  metadata
34628
34955
  },
34629
34956
  name: formatAgentSpanName(agent),
@@ -34641,22 +34968,21 @@ function startAgentStream(event, activeChildParents) {
34641
34968
  startTime: getCurrentUnixTimestamp()
34642
34969
  };
34643
34970
  }
34644
- function startMultiAgentStream(event, operation, activeChildParents) {
34645
- const orchestrator = extractOrchestrator(event);
34971
+ function startMultiAgentStream(input, orchestrator, operation, activeChildParents) {
34646
34972
  const metadata = {
34647
34973
  "strands.operation": operation,
34648
34974
  provider: "strands",
34649
34975
  ...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {}
34650
34976
  };
34651
34977
  const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
34652
- const input = processStrandsInputAttachments(event.arguments[0]);
34978
+ const processedInput = processStrandsInputAttachments(input);
34653
34979
  const span = parentSpan ? withCurrent(
34654
34980
  parentSpan,
34655
34981
  () => startSpan(
34656
34982
  withSpanInstrumentationName(
34657
34983
  {
34658
34984
  event: {
34659
- input,
34985
+ input: processedInput,
34660
34986
  metadata
34661
34987
  },
34662
34988
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -34669,7 +34995,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
34669
34995
  withSpanInstrumentationName(
34670
34996
  {
34671
34997
  event: {
34672
- input,
34998
+ input: processedInput,
34673
34999
  metadata
34674
35000
  },
34675
35001
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -35040,12 +35366,12 @@ function finalizeMultiAgentStream(state, activeChildParents, error, output) {
35040
35366
  });
35041
35367
  state.span.end();
35042
35368
  }
35043
- function extractAgent(event) {
35044
- const candidate = event.agent ?? event.self;
35369
+ function extractAgent(agent, self) {
35370
+ const candidate = agent ?? self;
35045
35371
  return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
35046
35372
  }
35047
- function extractOrchestrator(event) {
35048
- const candidate = event.orchestrator ?? event.self;
35373
+ function extractOrchestrator(orchestrator, self) {
35374
+ const candidate = orchestrator ?? self;
35049
35375
  return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
35050
35376
  }
35051
35377
  function extractAgentMetadata2(agent) {
@@ -37559,6 +37885,13 @@ function wrapAISDK(aiSDK, options = {}) {
37559
37885
  switch (prop) {
37560
37886
  case "generateText":
37561
37887
  return wrapGenerateText(typedAISDK.generateText, options, typedAISDK);
37888
+ case "generateImage":
37889
+ case "experimental_generateImage":
37890
+ return typeof original === "function" ? wrapGenerateImage(
37891
+ original,
37892
+ options,
37893
+ typedAISDK
37894
+ ) : original;
37562
37895
  case "streamText":
37563
37896
  return wrapStreamText(typedAISDK.streamText, options, typedAISDK);
37564
37897
  case "generateObject":
@@ -37795,6 +38128,32 @@ var wrapGenerateObject = (generateObject, options = {}, aiSDK) => {
37795
38128
  options
37796
38129
  );
37797
38130
  };
38131
+ var wrapGenerateImage = (generateImage, options = {}, aiSDK) => {
38132
+ return makeGenerateImageWrapper(generateImage, { aiSDK }, options);
38133
+ };
38134
+ var makeGenerateImageWrapper = (generateImage, contextOptions = {}, options = {}) => {
38135
+ const wrapper = async function(allParams) {
38136
+ const { span_info, ...params } = allParams;
38137
+ const tracedParams = { ...params };
38138
+ return aiSDKChannels.generateImage.tracePromise(
38139
+ () => generateImage(tracedParams),
38140
+ createAISDKChannelContext(tracedParams, {
38141
+ aiSDK: contextOptions.aiSDK,
38142
+ denyOutputPaths: options.denyOutputPaths,
38143
+ self: contextOptions.self,
38144
+ span_info: mergeSpanInfo(span_info, {
38145
+ name: "generateImage",
38146
+ spanType: contextOptions.spanType
38147
+ })
38148
+ })
38149
+ );
38150
+ };
38151
+ Object.defineProperty(wrapper, "name", {
38152
+ value: "generateImage",
38153
+ writable: false
38154
+ });
38155
+ return wrapper;
38156
+ };
37798
38157
  var makeEmbedWrapper = (channel2, name, embed, contextOptions = {}, options = {}) => {
37799
38158
  const wrapper = async function(allParams) {
37800
38159
  const { span_info, ...params } = allParams;
@@ -38545,7 +38904,7 @@ function braintrustEveHook(options) {
38545
38904
  }
38546
38905
  };
38547
38906
  }
38548
- function braintrustEveInstrumentation(options) {
38907
+ function createLegacyEveInstrumentation(options) {
38549
38908
  const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
38550
38909
  return {
38551
38910
  events: {
@@ -39933,8 +40292,10 @@ function capturedModelInput(modelInput) {
39933
40292
  const value = [];
39934
40293
  if (typeof instructions === "string") {
39935
40294
  value.push({ content: instructions, role: "system" });
39936
- } else if (instructions) {
40295
+ } else if (Array.isArray(instructions)) {
39937
40296
  value.push(...instructions.map(capturedEveModelMessage));
40297
+ } else if (instructions) {
40298
+ value.push(capturedEveModelMessage(instructions));
39938
40299
  }
39939
40300
  value.push(...messages.map(capturedEveModelMessage));
39940
40301
  try {
@@ -40178,6 +40539,464 @@ async function deterministicEveId(...parts) {
40178
40539
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
40179
40540
  }
40180
40541
 
40542
+ // src/instrumentation/plugins/eve-provider.ts
40543
+ var MAX_EVE_PROVIDER_CACHE_ENTRIES = 1e4;
40544
+ function createEveInstrumentationProvider(options = {}) {
40545
+ const bridge = new EveProviderBridge(options.metadata);
40546
+ return {
40547
+ capture: "content",
40548
+ events: {
40549
+ "action.completed": (event, context) => bridge.handleActionTerminal(event, context),
40550
+ "action.failed": (event, context) => bridge.handleActionTerminal(event, context),
40551
+ "action.started": (event, context) => bridge.handleActionStarted(event, context),
40552
+ "model.call.completed": (event, context) => bridge.handleModelTerminal(event, context),
40553
+ "model.call.failed": (event, context) => bridge.handleModelTerminal(event, context),
40554
+ "model.call.started": (event, context) => bridge.handleModelStarted(event, context),
40555
+ "step.attempt.completed": (event) => bridge.handleStepAttemptTerminal(event),
40556
+ "step.attempt.failed": (event) => bridge.handleStepAttemptTerminal(event),
40557
+ "turn.cancelled": (event, context) => bridge.handleTurnTerminal(event, context),
40558
+ "turn.completed": (event, context) => bridge.handleTurnTerminal(event, context),
40559
+ "turn.failed": (event, context) => bridge.handleTurnTerminal(event, context),
40560
+ "turn.started": (event, context) => bridge.handleTurnStarted(event, context)
40561
+ },
40562
+ flush,
40563
+ setup: options.setup
40564
+ };
40565
+ }
40566
+ var EveProviderBridge = class {
40567
+ constructor(metadata) {
40568
+ this.metadata = metadata;
40569
+ }
40570
+ metadata;
40571
+ activeActions = /* @__PURE__ */ new Map();
40572
+ activeModels = /* @__PURE__ */ new Map();
40573
+ modelsByAttempt = /* @__PURE__ */ new Map();
40574
+ settledOperations = new LRUCache({
40575
+ max: MAX_EVE_PROVIDER_CACHE_ENTRIES
40576
+ });
40577
+ turns = new LRUCache({
40578
+ max: MAX_EVE_PROVIDER_CACHE_ENTRIES
40579
+ });
40580
+ async handleTurnStarted(event, context) {
40581
+ await this.contain("turn start", async () => {
40582
+ if (this.settledOperations.has(event.idempotencyKey)) {
40583
+ context.state.set({ skip: true });
40584
+ return;
40585
+ }
40586
+ const { rowId, spanId } = await generateEveIds(
40587
+ "turn",
40588
+ event.idempotencyKey
40589
+ );
40590
+ const key = turnKey3(event.sessionId, event.turnId);
40591
+ const stored = storedSpan(context);
40592
+ if (stored && "exported" in stored && stored.rootSpanId) {
40593
+ this.turns.set(key, { rootSpanId: stored.rootSpanId, spanId });
40594
+ return;
40595
+ }
40596
+ let rootSpanId = spanId;
40597
+ let parentSpanId;
40598
+ if (event.parentLineage) {
40599
+ const parentTurnKey = turnKey3(
40600
+ event.parentLineage.sessionId,
40601
+ event.parentLineage.turnId
40602
+ );
40603
+ const parentTurn = this.turns.get(parentTurnKey);
40604
+ rootSpanId = parentTurn?.rootSpanId ?? // Eve propagates the root session through every nested subagent. Use
40605
+ // it instead of the immediate parent session when no ancestor ran
40606
+ // in-process.
40607
+ (await generateEveIds(
40608
+ "turn",
40609
+ turnIdempotencyKey(
40610
+ event.rootSessionId,
40611
+ event.parentLineage.turnId
40612
+ )
40613
+ )).spanId;
40614
+ parentSpanId = (await generateEveIds(
40615
+ "subagent",
40616
+ actionIdempotencyKey(
40617
+ event.parentLineage.sessionId,
40618
+ event.parentLineage.turnId,
40619
+ event.parentLineage.callId
40620
+ )
40621
+ )).spanId;
40622
+ }
40623
+ const metadata = this.spanMetadata(event.sessionId);
40624
+ const span = await this.startSpan(
40625
+ context,
40626
+ {
40627
+ event: { id: rowId, metadata },
40628
+ name: "eve.turn",
40629
+ parentSpanIds: parentSpanId ? { rootSpanId, spanId: parentSpanId } : { parentSpanIds: [], rootSpanId },
40630
+ spanAttributes: { type: "task" /* TASK */ },
40631
+ spanId
40632
+ },
40633
+ rootSpanId
40634
+ );
40635
+ span?.log({ metadata });
40636
+ this.turns.set(key, {
40637
+ rootSpanId,
40638
+ spanId
40639
+ });
40640
+ });
40641
+ }
40642
+ async handleTurnTerminal(event, context) {
40643
+ await this.contain("turn terminal", async () => {
40644
+ if (this.settledOperations.has(event.idempotencyKey)) {
40645
+ context.state.set(void 0);
40646
+ return;
40647
+ }
40648
+ const error = event.type === "turn.failed" ? event.error : void 0;
40649
+ this.drainActionsForTurn(event.sessionId, event.turnId, error);
40650
+ const stored = storedSpan(context);
40651
+ if (stored && "exported" in stored) {
40652
+ updateSpan({
40653
+ exported: stored.exported,
40654
+ ...error !== void 0 ? { error } : {},
40655
+ metrics: { end: Date.now() / 1e3 }
40656
+ });
40657
+ }
40658
+ this.settledOperations.set(event.idempotencyKey, true);
40659
+ context.state.set(void 0);
40660
+ this.turns.delete(turnKey3(event.sessionId, event.turnId));
40661
+ });
40662
+ }
40663
+ async handleModelStarted(event, context) {
40664
+ await this.contain("model start", async () => {
40665
+ if (this.settledOperations.has(event.idempotencyKey)) {
40666
+ context.state.set({ skip: true });
40667
+ return;
40668
+ }
40669
+ const parent = await this.parentForScope(event.scope);
40670
+ const { rowId, spanId } = await generateEveIds(
40671
+ "step",
40672
+ event.idempotencyKey
40673
+ );
40674
+ const input = event.input ? capturedModelInput(event.input) : void 0;
40675
+ const metadata = {
40676
+ ...this.spanMetadata(event.scope.sessionId),
40677
+ model: event.model.modelId,
40678
+ provider: event.model.provider
40679
+ };
40680
+ const span = await this.startSpan(context, {
40681
+ event: {
40682
+ id: rowId,
40683
+ ...input !== void 0 ? { input } : {},
40684
+ metadata
40685
+ },
40686
+ name: "eve.step",
40687
+ parentSpanIds: parent,
40688
+ spanAttributes: { type: "llm" /* LLM */ },
40689
+ spanId
40690
+ });
40691
+ if (!span) return;
40692
+ span.log({ ...input !== void 0 ? { input } : {}, metadata });
40693
+ this.activeModels.set(event.idempotencyKey, {
40694
+ span,
40695
+ turnKey: turnKey3(event.scope.sessionId, event.scope.turnId)
40696
+ });
40697
+ const keys = this.modelsByAttempt.get(event.scope.attemptId) ?? /* @__PURE__ */ new Set();
40698
+ keys.add(event.idempotencyKey);
40699
+ this.modelsByAttempt.set(event.scope.attemptId, keys);
40700
+ });
40701
+ }
40702
+ async handleModelTerminal(event, context) {
40703
+ await this.contain("model terminal", async () => {
40704
+ if (this.settledOperations.has(event.idempotencyKey)) {
40705
+ context.state.set(void 0);
40706
+ return;
40707
+ }
40708
+ const active = this.activeModels.get(event.idempotencyKey);
40709
+ if (active) {
40710
+ const span = active.span;
40711
+ if (event.type === "model.call.failed") {
40712
+ if (event.error !== void 0) span.log({ error: event.error });
40713
+ } else {
40714
+ span.log({
40715
+ metrics: usageMetrics(event.usage),
40716
+ output: modelOutput(event)
40717
+ });
40718
+ }
40719
+ span.end();
40720
+ } else {
40721
+ const stored = storedSpan(context);
40722
+ if (stored && "exported" in stored) {
40723
+ updateSpan({
40724
+ exported: stored.exported,
40725
+ ...event.type === "model.call.failed" ? event.error !== void 0 ? { error: event.error } : {} : { output: modelOutput(event) },
40726
+ metrics: {
40727
+ ...event.type === "model.call.completed" ? usageMetrics(event.usage) : {},
40728
+ end: Date.now() / 1e3
40729
+ }
40730
+ });
40731
+ }
40732
+ }
40733
+ this.settledOperations.set(event.idempotencyKey, true);
40734
+ this.forgetModel(event.scope.attemptId, event.idempotencyKey);
40735
+ context.state.set(void 0);
40736
+ });
40737
+ }
40738
+ async handleActionStarted(event, context) {
40739
+ await this.contain("action start", async () => {
40740
+ if (event.kind === "load-skill" || this.settledOperations.has(event.idempotencyKey)) {
40741
+ context.state.set({ skip: true });
40742
+ return;
40743
+ }
40744
+ const parent = await this.parentForScope(event.scope);
40745
+ const { rowId, spanId } = await generateEveIds(
40746
+ event.kind === "subagent-call" ? "subagent" : "tool",
40747
+ event.idempotencyKey
40748
+ );
40749
+ const metadata = {
40750
+ ...this.spanMetadata(event.scope.sessionId),
40751
+ "eve.action_kind": event.kind
40752
+ };
40753
+ const span = await this.startSpan(context, {
40754
+ event: {
40755
+ id: rowId,
40756
+ ...event.input !== void 0 ? { input: event.input } : {},
40757
+ metadata
40758
+ },
40759
+ name: event.name,
40760
+ parentSpanIds: parent,
40761
+ spanAttributes: { type: "tool" /* TOOL */ },
40762
+ spanId
40763
+ });
40764
+ if (!span) return;
40765
+ span.log({
40766
+ ...event.input !== void 0 ? { input: event.input } : {},
40767
+ metadata
40768
+ });
40769
+ this.activeActions.set(event.idempotencyKey, {
40770
+ span,
40771
+ turnKey: turnKey3(event.scope.sessionId, event.scope.turnId)
40772
+ });
40773
+ });
40774
+ }
40775
+ async handleActionTerminal(event, context) {
40776
+ await this.contain("action terminal", async () => {
40777
+ if (this.settledOperations.has(event.idempotencyKey)) {
40778
+ context.state.set(void 0);
40779
+ return;
40780
+ }
40781
+ const stored = storedSpan(context);
40782
+ if (stored?.skip) {
40783
+ context.state.set(void 0);
40784
+ return;
40785
+ }
40786
+ const active = this.activeActions.get(event.idempotencyKey);
40787
+ const end = finiteTimestamp(event.acceptedAtMs) ?? Date.now();
40788
+ if (active) {
40789
+ const span = active.span;
40790
+ if (event.type === "action.failed") {
40791
+ span.log({
40792
+ error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
40793
+ });
40794
+ } else if (event.output.type === "error") {
40795
+ span.log({
40796
+ error: event.output.error ?? new Error("Eve action returned an error")
40797
+ });
40798
+ } else {
40799
+ span.log({ output: event.output.output });
40800
+ }
40801
+ span.end({ endTime: end / 1e3 });
40802
+ } else {
40803
+ const stored2 = storedSpan(context);
40804
+ if (stored2 && "exported" in stored2) {
40805
+ updateSpan({
40806
+ exported: stored2.exported,
40807
+ ...event.type === "action.failed" ? {
40808
+ error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
40809
+ } : event.output.type === "error" ? {
40810
+ error: event.output.error ?? new Error("Eve action returned an error")
40811
+ } : { output: event.output.output },
40812
+ metrics: { end: end / 1e3 }
40813
+ });
40814
+ }
40815
+ }
40816
+ this.settledOperations.set(event.idempotencyKey, true);
40817
+ this.activeActions.delete(event.idempotencyKey);
40818
+ context.state.set(void 0);
40819
+ });
40820
+ }
40821
+ handleStepAttemptTerminal(event) {
40822
+ void this.contain("step attempt terminal", () => {
40823
+ const keys = this.modelsByAttempt.get(event.scope.attemptId);
40824
+ if (!keys) return;
40825
+ for (const key of keys) {
40826
+ const active = this.activeModels.get(key);
40827
+ if (!active) continue;
40828
+ if (event.type === "step.attempt.failed" && event.error !== void 0) {
40829
+ active.span.log({ error: event.error });
40830
+ }
40831
+ active.span.end();
40832
+ this.activeModels.delete(key);
40833
+ }
40834
+ this.modelsByAttempt.delete(event.scope.attemptId);
40835
+ });
40836
+ }
40837
+ async parentForScope(scope) {
40838
+ const key = turnKey3(scope.sessionId, scope.turnId);
40839
+ const known = this.turns.get(key);
40840
+ if (known) {
40841
+ return { rootSpanId: known.rootSpanId, spanId: known.spanId };
40842
+ }
40843
+ const [{ spanId }, { spanId: rootSpanId }] = await Promise.all([
40844
+ generateEveIds("turn", turnIdempotencyKey(scope.sessionId, scope.turnId)),
40845
+ generateEveIds(
40846
+ "turn",
40847
+ turnIdempotencyKey(
40848
+ scope.rootSessionId ?? scope.sessionId,
40849
+ scope.turnId
40850
+ )
40851
+ )
40852
+ ]);
40853
+ return { rootSpanId, spanId };
40854
+ }
40855
+ async startSpan(context, args, rootSpanId) {
40856
+ const span = withCurrent(
40857
+ NOOP_SPAN,
40858
+ () => _internalStartSpanWithInitialMerge(
40859
+ withSpanInstrumentationName(args ?? {}, INSTRUMENTATION_NAMES.EVE)
40860
+ )
40861
+ );
40862
+ try {
40863
+ context.state.set({
40864
+ exported: await span.export(),
40865
+ ...rootSpanId ? { rootSpanId } : {}
40866
+ });
40867
+ } catch (error) {
40868
+ debugLogger.warn("Error exporting Eve provider span:", error);
40869
+ }
40870
+ return span;
40871
+ }
40872
+ drainActionsForTurn(sessionId, turnId, error) {
40873
+ const key = turnKey3(sessionId, turnId);
40874
+ for (const [idempotencyKey, active] of this.activeActions) {
40875
+ if (active.turnKey !== key) continue;
40876
+ if (error !== void 0) active.span.log({ error });
40877
+ active.span.end();
40878
+ this.activeActions.delete(idempotencyKey);
40879
+ }
40880
+ }
40881
+ forgetModel(attemptId, idempotencyKey) {
40882
+ this.activeModels.delete(idempotencyKey);
40883
+ const keys = this.modelsByAttempt.get(attemptId);
40884
+ keys?.delete(idempotencyKey);
40885
+ if (keys?.size === 0) this.modelsByAttempt.delete(attemptId);
40886
+ }
40887
+ spanMetadata(sessionId) {
40888
+ return {
40889
+ ...this.metadata ?? {},
40890
+ "eve.session_id": sessionId
40891
+ };
40892
+ }
40893
+ async contain(operation, fn) {
40894
+ try {
40895
+ await fn();
40896
+ } catch (error) {
40897
+ debugLogger.warn(`Error in Eve provider ${operation}:`, error);
40898
+ }
40899
+ }
40900
+ };
40901
+ function storedSpan(context) {
40902
+ const value = context.state.get();
40903
+ if (!isObject(value)) return void 0;
40904
+ if (value["skip"] === true) return { skip: true };
40905
+ return typeof value["exported"] === "string" ? {
40906
+ exported: value["exported"],
40907
+ ...typeof value["rootSpanId"] === "string" && value["rootSpanId"].length > 0 ? { rootSpanId: value["rootSpanId"] } : {}
40908
+ } : void 0;
40909
+ }
40910
+ function modelOutput(event) {
40911
+ const content = event.content ?? [];
40912
+ let text = "";
40913
+ const reasoning = [];
40914
+ const toolCalls2 = [];
40915
+ for (const part of content) {
40916
+ if (part.type === "text") {
40917
+ text += part.text;
40918
+ } else if (part.type === "reasoning" && part.text.trim().length > 0) {
40919
+ reasoning.push({ content: part.text });
40920
+ } else if (part.type === "tool-call") {
40921
+ toolCalls2.push({
40922
+ function: {
40923
+ arguments: safeJsonStringify(part.input),
40924
+ name: part.toolName
40925
+ },
40926
+ id: part.callId,
40927
+ type: "function"
40928
+ });
40929
+ }
40930
+ }
40931
+ return [
40932
+ {
40933
+ finish_reason: normalizeFinishReason3(event.finishReason),
40934
+ index: 0,
40935
+ message: {
40936
+ content: text || null,
40937
+ ...reasoning.length > 0 ? { reasoning } : {},
40938
+ role: "assistant",
40939
+ ...toolCalls2.length > 0 ? { tool_calls: toolCalls2 } : {}
40940
+ }
40941
+ }
40942
+ ];
40943
+ }
40944
+ function usageMetrics(usage) {
40945
+ const promptTokens = nonNegativeNumber(usage.inputTokens);
40946
+ const completionTokens = nonNegativeNumber(usage.outputTokens);
40947
+ const cachedTokens = nonNegativeNumber(
40948
+ usage.inputTokenDetails?.cacheReadTokens
40949
+ );
40950
+ const cacheCreationTokens = nonNegativeNumber(
40951
+ usage.inputTokenDetails?.cacheWriteTokens
40952
+ );
40953
+ return {
40954
+ ...promptTokens !== void 0 ? { prompt_tokens: promptTokens } : {},
40955
+ ...completionTokens !== void 0 ? { completion_tokens: completionTokens } : {},
40956
+ ...promptTokens !== void 0 && completionTokens !== void 0 ? { tokens: promptTokens + completionTokens } : {},
40957
+ ...cachedTokens !== void 0 ? { prompt_cached_tokens: cachedTokens } : {},
40958
+ ...cacheCreationTokens !== void 0 ? { prompt_cache_creation_tokens: cacheCreationTokens } : {}
40959
+ };
40960
+ }
40961
+ function nonNegativeNumber(value) {
40962
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
40963
+ }
40964
+ function finiteTimestamp(value) {
40965
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
40966
+ }
40967
+ function normalizeFinishReason3(value) {
40968
+ if (value === "content-filter") return "content_filter";
40969
+ if (value === "tool-calls") return "tool_calls";
40970
+ return value;
40971
+ }
40972
+ function safeJsonStringify(value) {
40973
+ try {
40974
+ return JSON.stringify(value) ?? "null";
40975
+ } catch {
40976
+ return "null";
40977
+ }
40978
+ }
40979
+ function turnKey3(sessionId, turnId) {
40980
+ return `${sessionId}:${turnId}`;
40981
+ }
40982
+ function turnIdempotencyKey(sessionId, turnId) {
40983
+ return `turn:${sessionId}:${turnId}`;
40984
+ }
40985
+ function actionIdempotencyKey(sessionId, turnId, callId) {
40986
+ return `action:${sessionId}:${turnId}:${callId}`;
40987
+ }
40988
+
40989
+ // src/instrumentation/plugins/eve-instrumentation.ts
40990
+ var EVE_INSTRUMENTATION_PROVIDER = /* @__PURE__ */ Symbol.for("eve.instrumentation.provider");
40991
+ function braintrustEveInstrumentation(options) {
40992
+ const definition = "defineState" in options ? createLegacyEveInstrumentation(options) : createEveInstrumentationProvider(options);
40993
+ const declaration = {
40994
+ ...definition,
40995
+ [EVE_INSTRUMENTATION_PROVIDER]: true
40996
+ };
40997
+ return declaration;
40998
+ }
40999
+
40181
41000
  // src/typed-instrumentation-helpers.ts
40182
41001
  var TypedApplyProxy = Proxy;
40183
41002
 
@@ -40579,11 +41398,11 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
40579
41398
  }
40580
41399
  };
40581
41400
  const invocationTarget = thisArg === proxy || thisArg === void 0 ? defaultThis ?? thisArg : thisArg;
40582
- return claudeAgentSDKChannels.query.traceSync(
40583
- () => Reflect.apply(target, invocationTarget, [wrappedParams]),
40584
- // The channel carries no extra context fields, but the generated
40585
- // StartOf<> type for Record<string, never> is overly strict here.
40586
- { arguments: [wrappedParams] }
41401
+ return claudeAgentSDKChannels.query.invoke(
41402
+ target,
41403
+ invocationTarget,
41404
+ [wrappedParams],
41405
+ {}
40587
41406
  );
40588
41407
  }
40589
41408
  });
@@ -41198,13 +42017,11 @@ function wrapAgentInstance(agent) {
41198
42017
  if (prop === "stream" && typeof value === "function") {
41199
42018
  return function(args, options) {
41200
42019
  const callArgs = [args, options];
41201
- return strandsAgentSDKChannels.agentStream.traceSync(
41202
- () => Reflect.apply(value, target, callArgs),
41203
- {
41204
- agent: proxy,
41205
- arguments: callArgs,
41206
- self: proxy
41207
- }
42020
+ return strandsAgentSDKChannels.agentStream.invoke(
42021
+ value,
42022
+ target,
42023
+ callArgs,
42024
+ { agent: proxy }
41208
42025
  );
41209
42026
  };
41210
42027
  }
@@ -41235,12 +42052,12 @@ function wrapMultiAgentInstance(orchestrator, kind) {
41235
42052
  return function(input, options) {
41236
42053
  const callArgs = [input, options];
41237
42054
  const channel2 = kind === "graph" ? strandsAgentSDKChannels.graphStream : strandsAgentSDKChannels.swarmStream;
41238
- return channel2.traceSync(
41239
- () => Reflect.apply(value, target, callArgs),
42055
+ return channel2.invoke(
42056
+ value,
42057
+ target,
42058
+ callArgs,
41240
42059
  {
41241
- arguments: callArgs,
41242
- orchestrator: proxy,
41243
- self: proxy
42060
+ orchestrator: proxy
41244
42061
  }
41245
42062
  );
41246
42063
  };
@@ -43192,9 +44009,6 @@ var VitestContextManager = class {
43192
44009
  getCurrentContext() {
43193
44010
  return this.contextStorage.getStore();
43194
44011
  }
43195
- setContext(context) {
43196
- this.contextStorage.enterWith(context);
43197
- }
43198
44012
  runInContext(context, callback) {
43199
44013
  return this.contextStorage.run(context, callback);
43200
44014
  }
@@ -43580,8 +44394,7 @@ function wrapDescribe(originalDescribe, config, afterAll) {
43580
44394
  if (config.onProgress) {
43581
44395
  config.onProgress({ type: "suite_start", suiteName });
43582
44396
  }
43583
- contextManager.setContext(lazyContext);
43584
- factory();
44397
+ contextManager.runInContext(lazyContext, factory);
43585
44398
  if (afterAll) {
43586
44399
  afterAll(async () => {
43587
44400
  await flushExperimentWithSync(context, config);
@@ -44160,7 +44973,7 @@ function isAsync(fn) {
44160
44973
  function isAsyncGenerator2(fn) {
44161
44974
  return fn[Symbol.toStringTag] === "AsyncGenerator";
44162
44975
  }
44163
- function isAsyncIterable6(obj) {
44976
+ function isAsyncIterable5(obj) {
44164
44977
  return typeof obj[Symbol.asyncIterator] === "function";
44165
44978
  }
44166
44979
  function wrapAsync(asyncFn) {
@@ -44332,7 +45145,7 @@ var eachOfLimit$2 = (limit) => {
44332
45145
  if (isAsyncGenerator2(obj)) {
44333
45146
  return asyncEachOfLimit(obj, limit, iteratee, callback);
44334
45147
  }
44335
- if (isAsyncIterable6(obj)) {
45148
+ if (isAsyncIterable5(obj)) {
44336
45149
  return asyncEachOfLimit(obj[Symbol.asyncIterator](), limit, iteratee, callback);
44337
45150
  }
44338
45151
  var nextElem = createIterator(obj);
@@ -45687,7 +46500,7 @@ function callEvaluatorData(data) {
45687
46500
  baseExperiment
45688
46501
  };
45689
46502
  }
45690
- function isAsyncIterable7(value) {
46503
+ function isAsyncIterable6(value) {
45691
46504
  return typeof value === "object" && value !== null && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
45692
46505
  }
45693
46506
  function isIterable(value) {
@@ -45722,7 +46535,7 @@ async function _internalResolveEvaluatorData(evaluator, experiment) {
45722
46535
  }).asDataset();
45723
46536
  }
45724
46537
  const resolvedDataResult = dataResult instanceof Promise ? await dataResult : dataResult;
45725
- if (isAsyncIterable7(resolvedDataResult)) {
46538
+ if (isAsyncIterable6(resolvedDataResult)) {
45726
46539
  return resolvedDataResult;
45727
46540
  }
45728
46541
  if (Array.isArray(resolvedDataResult) || isIterable(resolvedDataResult)) {