braintrust 3.4.0 → 3.5.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/dev/dist/index.d.mts +45 -5
  2. package/dev/dist/index.d.ts +45 -5
  3. package/dev/dist/index.js +992 -245
  4. package/dev/dist/index.mjs +914 -167
  5. package/dist/auto-instrumentations/bundler/esbuild.cjs +208 -10
  6. package/dist/auto-instrumentations/bundler/esbuild.d.mts +2 -2
  7. package/dist/auto-instrumentations/bundler/esbuild.d.ts +2 -2
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  9. package/dist/auto-instrumentations/bundler/rollup.cjs +208 -10
  10. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  11. package/dist/auto-instrumentations/bundler/vite.cjs +208 -10
  12. package/dist/auto-instrumentations/bundler/vite.d.mts +2 -2
  13. package/dist/auto-instrumentations/bundler/vite.d.ts +2 -2
  14. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  15. package/dist/auto-instrumentations/bundler/webpack.cjs +208 -10
  16. package/dist/auto-instrumentations/bundler/webpack.d.mts +2 -2
  17. package/dist/auto-instrumentations/bundler/webpack.d.ts +2 -2
  18. package/dist/auto-instrumentations/bundler/webpack.mjs +2 -2
  19. package/dist/auto-instrumentations/{chunk-LVWWLUMN.mjs → chunk-DQTPSXJB.mjs} +208 -10
  20. package/dist/auto-instrumentations/chunk-EVUKFMHG.mjs +41 -0
  21. package/dist/auto-instrumentations/{chunk-D5ZPIUEL.mjs → chunk-F3TJZ3Z2.mjs} +1 -1
  22. package/dist/auto-instrumentations/chunk-VLEJ5AEK.mjs +41 -0
  23. package/dist/auto-instrumentations/hook.mjs +238 -18
  24. package/dist/auto-instrumentations/index.cjs +208 -10
  25. package/dist/auto-instrumentations/index.mjs +1 -1
  26. package/dist/auto-instrumentations/loader/cjs-patch.cjs +32 -10
  27. package/dist/auto-instrumentations/loader/cjs-patch.mjs +10 -5
  28. package/dist/auto-instrumentations/loader/esm-hook.mjs +4 -4
  29. package/dist/auto-instrumentations/loader/get-package-version.cjs +28 -8
  30. package/dist/auto-instrumentations/loader/get-package-version.d.mts +2 -1
  31. package/dist/auto-instrumentations/loader/get-package-version.d.ts +2 -1
  32. package/dist/auto-instrumentations/loader/get-package-version.mjs +3 -1
  33. package/dist/browser.d.mts +342 -269
  34. package/dist/browser.d.ts +342 -269
  35. package/dist/browser.js +996 -241
  36. package/dist/browser.mjs +996 -241
  37. package/dist/cli.js +1029 -289
  38. package/dist/edge-light.js +1007 -220
  39. package/dist/edge-light.mjs +1007 -220
  40. package/dist/index.d.mts +342 -269
  41. package/dist/index.d.ts +342 -269
  42. package/dist/index.js +1182 -427
  43. package/dist/index.mjs +996 -241
  44. package/dist/instrumentation/index.js +781 -107
  45. package/dist/instrumentation/index.mjs +781 -107
  46. package/dist/workerd.js +1007 -220
  47. package/dist/workerd.mjs +1007 -220
  48. package/package.json +22 -6
  49. package/dist/auto-instrumentations/chunk-XDBPUTVE.mjs +0 -22
  50. package/dist/auto-instrumentations/chunk-ZEC7BCL4.mjs +0 -22
package/dev/dist/index.js CHANGED
@@ -22,8 +22,54 @@ var DefaultAsyncLocalStorage = class {
22
22
  return void 0;
23
23
  }
24
24
  };
25
- var DefaultTracingChannel = (_class = class {constructor() { _class.prototype.__init.call(this); }
25
+ var DefaultChannel = (_class = class {
26
+ constructor(name) {;_class.prototype.__init.call(this);
27
+ this.name = name;
28
+ }
26
29
  __init() {this.hasSubscribers = false}
30
+ subscribe(_subscription) {
31
+ }
32
+ unsubscribe(_subscription) {
33
+ return false;
34
+ }
35
+ bindStore(_store, _transform) {
36
+ }
37
+ unbindStore(_store) {
38
+ return false;
39
+ }
40
+ publish(_message) {
41
+ }
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ runStores(_message, fn, thisArg, ...args) {
44
+ return fn.apply(thisArg, args);
45
+ }
46
+ }, _class);
47
+ var DefaultTracingChannel = class {
48
+
49
+
50
+
51
+
52
+
53
+ constructor(nameOrChannels) {
54
+ if (typeof nameOrChannels === "string") {
55
+ this.start = new DefaultChannel(`tracing:${nameOrChannels}:start`);
56
+ this.end = new DefaultChannel(`tracing:${nameOrChannels}:end`);
57
+ this.asyncStart = new DefaultChannel(
58
+ `tracing:${nameOrChannels}:asyncStart`
59
+ );
60
+ this.asyncEnd = new DefaultChannel(`tracing:${nameOrChannels}:asyncEnd`);
61
+ this.error = new DefaultChannel(`tracing:${nameOrChannels}:error`);
62
+ return;
63
+ }
64
+ this.start = _nullishCoalesce(nameOrChannels.start, () => ( new DefaultChannel("tracing:start")));
65
+ this.end = _nullishCoalesce(nameOrChannels.end, () => ( new DefaultChannel("tracing:end")));
66
+ this.asyncStart = _nullishCoalesce(nameOrChannels.asyncStart, () => ( new DefaultChannel("tracing:asyncStart")));
67
+ this.asyncEnd = _nullishCoalesce(nameOrChannels.asyncEnd, () => ( new DefaultChannel("tracing:asyncEnd")));
68
+ this.error = _nullishCoalesce(nameOrChannels.error, () => ( new DefaultChannel("tracing:error")));
69
+ }
70
+ get hasSubscribers() {
71
+ return this.start.hasSubscribers || this.end.hasSubscribers || this.asyncStart.hasSubscribers || this.asyncEnd.hasSubscribers || this.error.hasSubscribers;
72
+ }
27
73
  subscribe(_handlers) {
28
74
  }
29
75
  unsubscribe(_handlers) {
@@ -41,7 +87,7 @@ var DefaultTracingChannel = (_class = class {constructor() { _class.prototype.__
41
87
  traceCallback(fn, _position, _message, thisArg, ...args) {
42
88
  return fn.apply(thisArg, args);
43
89
  }
44
- }, _class);
90
+ };
45
91
  var iso = {
46
92
  buildType: "unknown",
47
93
  // Will be set by configureBrowser() or configureNode()
@@ -51,7 +97,7 @@ var iso = {
51
97
  getCallerLocation: () => void 0,
52
98
  newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
53
99
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
- newTracingChannel: (_nameOrChannels) => new DefaultTracingChannel(),
100
+ newTracingChannel: (nameOrChannels) => new DefaultTracingChannel(nameOrChannels),
55
101
  processOn: (_0, _1) => {
56
102
  },
57
103
  basename: (filepath) => filepath.split(/[\\/]/).pop() || filepath,
@@ -2334,6 +2380,8 @@ var Experiment = _v3.z.object({
2334
2380
  deleted_at: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2335
2381
  dataset_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2336
2382
  dataset_version: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2383
+ parameters_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2384
+ parameters_version: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2337
2385
  public: _v3.z.boolean(),
2338
2386
  user_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2339
2387
  metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
@@ -2356,7 +2404,11 @@ var SpanType = _v3.z.union([
2356
2404
  _v3.z.null()
2357
2405
  ]);
2358
2406
  var SpanAttributes = _v3.z.union([
2359
- _v3.z.object({ name: _v3.z.union([_v3.z.string(), _v3.z.null()]), type: SpanType }).partial().passthrough(),
2407
+ _v3.z.object({
2408
+ name: _v3.z.union([_v3.z.string(), _v3.z.null()]),
2409
+ type: SpanType,
2410
+ purpose: _v3.z.union([_v3.z.literal("scorer"), _v3.z.null()])
2411
+ }).partial().passthrough(),
2360
2412
  _v3.z.null()
2361
2413
  ]);
2362
2414
  var ExperimentEvent = _v3.z.object({
@@ -2796,6 +2848,7 @@ var FunctionId = _v3.z.union([
2796
2848
  version: _v3.z.string()
2797
2849
  }),
2798
2850
  code: _v3.z.string(),
2851
+ function_type: FunctionTypeEnum.and(_v3.z.unknown()).optional(),
2799
2852
  name: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional()
2800
2853
  }),
2801
2854
  _v3.z.object({
@@ -3025,7 +3078,12 @@ var TopicAutomationConfig = _v3.z.object({
3025
3078
  topic_map_functions: _v3.z.array(TopicMapFunctionAutomation),
3026
3079
  scope: _v3.z.union([SpanScope, TraceScope, GroupScope, _v3.z.null()]).optional(),
3027
3080
  data_scope: TopicAutomationDataScope.optional(),
3028
- btql_filter: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional()
3081
+ btql_filter: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
3082
+ backfill_time_range: _v3.z.union([
3083
+ _v3.z.string(),
3084
+ _v3.z.object({ from: _v3.z.string(), to: _v3.z.string() }),
3085
+ _v3.z.null()
3086
+ ]).optional()
3029
3087
  });
3030
3088
  var ProjectAutomation = _v3.z.object({
3031
3089
  id: _v3.z.string().uuid(),
@@ -4464,6 +4522,12 @@ var parametersRowSchema = _v3.z.object({
4464
4522
  }),
4465
4523
  metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional()
4466
4524
  });
4525
+ var InlineAttachmentReferenceSchema = _v3.z.object({
4526
+ type: _v3.z.literal("inline_attachment"),
4527
+ src: _v3.z.string().min(1),
4528
+ content_type: _v3.z.string().optional(),
4529
+ filename: _v3.z.string().optional()
4530
+ });
4467
4531
  var LoginInvalidOrgError = class extends Error {
4468
4532
  constructor(message) {
4469
4533
  super(message);
@@ -6453,6 +6517,7 @@ function init(projectOrOptions, optionalOptions) {
6453
6517
  experiment,
6454
6518
  description,
6455
6519
  dataset,
6520
+ parameters,
6456
6521
  baseExperiment,
6457
6522
  isPublic,
6458
6523
  open: open2,
@@ -6570,6 +6635,17 @@ function init(projectOrOptions, optionalOptions) {
6570
6635
  args["dataset_version"] = await dataset.version();
6571
6636
  }
6572
6637
  }
6638
+ if (parameters !== void 0) {
6639
+ if (RemoteEvalParameters.isParameters(parameters)) {
6640
+ args["parameters_id"] = parameters.id;
6641
+ args["parameters_version"] = parameters.version;
6642
+ } else {
6643
+ args["parameters_id"] = parameters.id;
6644
+ if (parameters.version !== void 0) {
6645
+ args["parameters_version"] = parameters.version;
6646
+ }
6647
+ }
6648
+ }
6573
6649
  if (isPublic !== void 0) {
6574
6650
  args["public"] = isPublic;
6575
6651
  }
@@ -8221,40 +8297,82 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8221
8297
  return typeof data === "object" && data !== null && "__braintrust_dataset_marker" in data;
8222
8298
  }
8223
8299
  }, _class14);
8224
- function renderMessage(render, message) {
8300
+ function isAttachmentObject(value) {
8301
+ return BraintrustAttachmentReference.safeParse(value).success || InlineAttachmentReferenceSchema.safeParse(value).success || ExternalAttachmentReference.safeParse(value).success;
8302
+ }
8303
+ function isURL(url) {
8304
+ try {
8305
+ const parsedUrl = new URL(url.trim());
8306
+ return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
8307
+ } catch (e18) {
8308
+ return false;
8309
+ }
8310
+ }
8311
+ function expandAttachmentArrayPreTemplate(content, variables) {
8312
+ if (typeof content !== "string") return null;
8313
+ const match = content.match(/^\{\{\s*([\w.]+)\s*\}\}$/);
8314
+ if (!match) return null;
8315
+ const varPath = match[1];
8316
+ const value = varPath.includes(".") ? getObjValueByPath(variables, varPath.split(".")) : variables[varPath];
8317
+ if (!Array.isArray(value)) return null;
8318
+ const allValid = value.every(
8319
+ (v) => isAttachmentObject(v) || typeof v === "string" && isURL(v)
8320
+ );
8321
+ if (!allValid) return null;
8322
+ return value.map((item) => ({
8323
+ type: "image_url",
8324
+ image_url: { url: item }
8325
+ }));
8326
+ }
8327
+ function renderMessageImpl(render, message, variables) {
8225
8328
  return {
8226
8329
  ...message,
8227
8330
  ..."content" in message ? {
8228
- content: isEmpty2(message.content) ? void 0 : typeof message.content === "string" ? render(message.content) : message.content.map((c) => {
8331
+ content: isEmpty2(message.content) ? void 0 : typeof message.content === "string" ? render(message.content) : message.content.flatMap((c) => {
8229
8332
  switch (c.type) {
8230
8333
  case "text":
8231
- return { ...c, text: render(c.text) };
8334
+ return [{ ...c, text: render(c.text) }];
8232
8335
  case "image_url":
8233
8336
  if (isObject(c.image_url.url)) {
8234
8337
  throw new Error(
8235
8338
  "Attachments must be replaced with URLs before calling `build()`"
8236
8339
  );
8237
8340
  }
8238
- return {
8239
- ...c,
8240
- image_url: {
8241
- ...c.image_url,
8242
- url: render(c.image_url.url)
8341
+ if (variables) {
8342
+ const expanded = expandAttachmentArrayPreTemplate(
8343
+ c.image_url.url,
8344
+ variables
8345
+ );
8346
+ if (expanded) {
8347
+ return expanded;
8243
8348
  }
8244
- };
8349
+ }
8350
+ return [
8351
+ {
8352
+ ...c,
8353
+ image_url: {
8354
+ ...c.image_url,
8355
+ url: render(c.image_url.url)
8356
+ }
8357
+ }
8358
+ ];
8245
8359
  case "file":
8246
- return {
8247
- ...c,
8248
- file: {
8249
- file_data: render(c.file.file_data || ""),
8250
- ...c.file.file_id && {
8251
- file_id: render(c.file.file_id)
8252
- },
8253
- ...c.file.filename && {
8254
- filename: render(c.file.filename)
8360
+ return [
8361
+ {
8362
+ ...c,
8363
+ file: {
8364
+ ...c.file.file_data && {
8365
+ file_data: render(c.file.file_data)
8366
+ },
8367
+ ...c.file.file_id && {
8368
+ file_id: render(c.file.file_id)
8369
+ },
8370
+ ...c.file.filename && {
8371
+ filename: render(c.file.filename)
8372
+ }
8255
8373
  }
8256
8374
  }
8257
- };
8375
+ ];
8258
8376
  default:
8259
8377
  const _exhaustiveCheck = c;
8260
8378
  return _exhaustiveCheck;
@@ -8411,17 +8529,19 @@ var Prompt2 = (_class15 = class _Prompt {
8411
8529
  }
8412
8530
  runBuild(buildArgs, options) {
8413
8531
  const { flavor } = options;
8414
- const params = {
8415
- ...this.defaults,
8416
- ...Object.fromEntries(
8417
- Object.entries(this.options.params || {}).filter(
8418
- ([k, _v]) => !BRAINTRUST_PARAMS.includes(k)
8419
- )
8420
- ),
8421
- ...!isEmpty2(this.options.model) ? {
8422
- model: this.options.model
8423
- } : {}
8424
- };
8532
+ const params = Object.fromEntries(
8533
+ Object.entries({
8534
+ ...this.defaults,
8535
+ ...Object.fromEntries(
8536
+ Object.entries(this.options.params || {}).filter(
8537
+ ([k, _v]) => !BRAINTRUST_PARAMS.includes(k)
8538
+ )
8539
+ ),
8540
+ ...!isEmpty2(this.options.model) ? {
8541
+ model: this.options.model
8542
+ } : {}
8543
+ }).filter(([key, value]) => key !== "response_format" || value !== null)
8544
+ );
8425
8545
  if (!("model" in params) || isEmpty2(params.model)) {
8426
8546
  throw new Error(
8427
8547
  "No model specified. Either specify it in the prompt or as a default"
@@ -8521,7 +8641,7 @@ var Prompt2 = (_class15 = class _Prompt {
8521
8641
  templateFormat
8522
8642
  });
8523
8643
  const baseMessages = (prompt.messages || []).map(
8524
- (m) => renderMessage(render, m)
8644
+ (m) => renderMessageImpl(render, m, variables)
8525
8645
  );
8526
8646
  const hasSystemPrompt = baseMessages.some((m) => m.role === "system");
8527
8647
  const messages = [
@@ -9149,7 +9269,11 @@ function startSpanForEvent(config, event, channelName) {
9149
9269
  });
9150
9270
  const startTime = getCurrentUnixTimestamp();
9151
9271
  try {
9152
- const { input, metadata } = config.extractInput(event.arguments);
9272
+ const { input, metadata } = config.extractInput(
9273
+ event.arguments,
9274
+ event,
9275
+ span
9276
+ );
9153
9277
  span.log({
9154
9278
  input,
9155
9279
  metadata: mergeInputMetadata(metadata, spanInfoMetadata)
@@ -9313,6 +9437,16 @@ function traceStreamingChannel(channel2, config) {
9313
9437
  });
9314
9438
  return;
9315
9439
  }
9440
+ if (_optionalChain([config, 'access', _115 => _115.patchResult, 'optionalCall', _116 => _116({
9441
+ channelName,
9442
+ endEvent: asyncEndEvent,
9443
+ result: asyncEndEvent.result,
9444
+ span,
9445
+ startTime
9446
+ })])) {
9447
+ states.delete(event);
9448
+ return;
9449
+ }
9316
9450
  try {
9317
9451
  const output = config.extractOutput(
9318
9452
  asyncEndEvent.result,
@@ -9323,7 +9457,7 @@ function traceStreamingChannel(channel2, config) {
9323
9457
  startTime,
9324
9458
  asyncEndEvent
9325
9459
  );
9326
- const metadata = _optionalChain([config, 'access', _115 => _115.extractMetadata, 'optionalCall', _116 => _116(
9460
+ const metadata = _optionalChain([config, 'access', _117 => _117.extractMetadata, 'optionalCall', _118 => _118(
9327
9461
  asyncEndEvent.result,
9328
9462
  asyncEndEvent
9329
9463
  )]);
@@ -9369,8 +9503,17 @@ function traceSyncStreamChannel(channel2, config) {
9369
9503
  return;
9370
9504
  }
9371
9505
  const { span, startTime } = spanData;
9372
- const resultEvent = event;
9373
- const stream = resultEvent.result;
9506
+ const endEvent = event;
9507
+ if (_optionalChain([config, 'access', _119 => _119.patchResult, 'optionalCall', _120 => _120({
9508
+ channelName,
9509
+ endEvent,
9510
+ result: endEvent.result,
9511
+ span,
9512
+ startTime
9513
+ })])) {
9514
+ return;
9515
+ }
9516
+ const stream = endEvent.result;
9374
9517
  if (!isSyncStreamLike(stream)) {
9375
9518
  span.end();
9376
9519
  states.delete(event);
@@ -9502,7 +9645,7 @@ function convertDataToBlob(data, mediaType) {
9502
9645
  } else if (typeof Buffer !== "undefined" && data instanceof Buffer) {
9503
9646
  return new Blob([data], { type: mediaType });
9504
9647
  }
9505
- } catch (e18) {
9648
+ } catch (e19) {
9506
9649
  return null;
9507
9650
  }
9508
9651
  return null;
@@ -9514,7 +9657,7 @@ function processInputAttachments(input) {
9514
9657
  let attachmentIndex = 0;
9515
9658
  const inferMediaTypeFromDataUrl = (value, fallback2) => {
9516
9659
  const mediaTypeMatch = value.match(/^data:([^;]+);/);
9517
- return _optionalChain([mediaTypeMatch, 'optionalAccess', _117 => _117[1]]) || fallback2;
9660
+ return _optionalChain([mediaTypeMatch, 'optionalAccess', _121 => _121[1]]) || fallback2;
9518
9661
  };
9519
9662
  const toAttachment = (value, mediaType, filename) => {
9520
9663
  const blob = convertDataToBlob(value, mediaType);
@@ -9764,11 +9907,11 @@ var OpenAIPlugin = class extends BasePlugin {
9764
9907
  };
9765
9908
  },
9766
9909
  extractOutput: (result) => {
9767
- return _optionalChain([result, 'optionalAccess', _118 => _118.choices]);
9910
+ return _optionalChain([result, 'optionalAccess', _122 => _122.choices]);
9768
9911
  },
9769
9912
  extractMetrics: (result, startTime, endEvent) => {
9770
9913
  const metrics = withCachedMetric(
9771
- parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _119 => _119.usage])),
9914
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _123 => _123.usage])),
9772
9915
  result,
9773
9916
  endEvent
9774
9917
  );
@@ -9792,12 +9935,12 @@ var OpenAIPlugin = class extends BasePlugin {
9792
9935
  };
9793
9936
  },
9794
9937
  extractOutput: (result) => {
9795
- const embedding = _optionalChain([result, 'optionalAccess', _120 => _120.data, 'optionalAccess', _121 => _121[0], 'optionalAccess', _122 => _122.embedding]);
9938
+ const embedding = _optionalChain([result, 'optionalAccess', _124 => _124.data, 'optionalAccess', _125 => _125[0], 'optionalAccess', _126 => _126.embedding]);
9796
9939
  return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
9797
9940
  },
9798
9941
  extractMetrics: (result, _startTime, endEvent) => {
9799
9942
  return withCachedMetric(
9800
- parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _123 => _123.usage])),
9943
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _127 => _127.usage])),
9801
9944
  result,
9802
9945
  endEvent
9803
9946
  );
@@ -9816,11 +9959,11 @@ var OpenAIPlugin = class extends BasePlugin {
9816
9959
  };
9817
9960
  },
9818
9961
  extractOutput: (result) => {
9819
- return _optionalChain([result, 'optionalAccess', _124 => _124.choices]);
9962
+ return _optionalChain([result, 'optionalAccess', _128 => _128.choices]);
9820
9963
  },
9821
9964
  extractMetrics: (result, startTime, endEvent) => {
9822
9965
  const metrics = withCachedMetric(
9823
- parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _125 => _125.usage])),
9966
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _129 => _129.usage])),
9824
9967
  result,
9825
9968
  endEvent
9826
9969
  );
@@ -9857,11 +10000,11 @@ var OpenAIPlugin = class extends BasePlugin {
9857
10000
  };
9858
10001
  },
9859
10002
  extractOutput: (result) => {
9860
- return _optionalChain([result, 'optionalAccess', _126 => _126.results]);
10003
+ return _optionalChain([result, 'optionalAccess', _130 => _130.results]);
9861
10004
  },
9862
10005
  extractMetrics: (result, _startTime, endEvent) => {
9863
10006
  return withCachedMetric(
9864
- parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _127 => _127.usage])),
10007
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _131 => _131.usage])),
9865
10008
  result,
9866
10009
  endEvent
9867
10010
  );
@@ -9880,7 +10023,7 @@ var OpenAIPlugin = class extends BasePlugin {
9880
10023
  };
9881
10024
  },
9882
10025
  extractOutput: (result) => {
9883
- return processImagesInOutput(_optionalChain([result, 'optionalAccess', _128 => _128.output]));
10026
+ return processImagesInOutput(_optionalChain([result, 'optionalAccess', _132 => _132.output]));
9884
10027
  },
9885
10028
  extractMetadata: (result) => {
9886
10029
  if (!result) {
@@ -9891,7 +10034,7 @@ var OpenAIPlugin = class extends BasePlugin {
9891
10034
  },
9892
10035
  extractMetrics: (result, startTime, endEvent) => {
9893
10036
  const metrics = withCachedMetric(
9894
- parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _129 => _129.usage])),
10037
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _133 => _133.usage])),
9895
10038
  result,
9896
10039
  endEvent
9897
10040
  );
@@ -9944,7 +10087,7 @@ var OpenAIPlugin = class extends BasePlugin {
9944
10087
  };
9945
10088
  },
9946
10089
  extractOutput: (result) => {
9947
- return processImagesInOutput(_optionalChain([result, 'optionalAccess', _130 => _130.output]));
10090
+ return processImagesInOutput(_optionalChain([result, 'optionalAccess', _134 => _134.output]));
9948
10091
  },
9949
10092
  extractMetadata: (result) => {
9950
10093
  if (!result) {
@@ -9955,7 +10098,7 @@ var OpenAIPlugin = class extends BasePlugin {
9955
10098
  },
9956
10099
  extractMetrics: (result, startTime, endEvent) => {
9957
10100
  const metrics = withCachedMetric(
9958
- parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _131 => _131.usage])),
10101
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _135 => _135.usage])),
9959
10102
  result,
9960
10103
  endEvent
9961
10104
  );
@@ -10051,7 +10194,7 @@ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
10051
10194
  ...parseMetricsFromUsage(chunk.usage)
10052
10195
  };
10053
10196
  }
10054
- const delta = _optionalChain([chunk, 'access', _132 => _132.choices, 'optionalAccess', _133 => _133[0], 'optionalAccess', _134 => _134.delta]);
10197
+ const delta = _optionalChain([chunk, 'access', _136 => _136.choices, 'optionalAccess', _137 => _137[0], 'optionalAccess', _138 => _138.delta]);
10055
10198
  if (!delta) {
10056
10199
  continue;
10057
10200
  }
@@ -10109,14 +10252,14 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
10109
10252
  continue;
10110
10253
  }
10111
10254
  const response = chunk.response;
10112
- if (_optionalChain([response, 'optionalAccess', _135 => _135.output]) !== void 0) {
10255
+ if (_optionalChain([response, 'optionalAccess', _139 => _139.output]) !== void 0) {
10113
10256
  output = processImagesInOutput(response.output);
10114
10257
  }
10115
10258
  const { usage: _usage, output: _output, ...rest } = response || {};
10116
10259
  if (Object.keys(rest).length > 0) {
10117
10260
  metadata = rest;
10118
10261
  }
10119
- metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _136 => _136.usage]));
10262
+ metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _140 => _140.usage]));
10120
10263
  }
10121
10264
  return {
10122
10265
  output,
@@ -10182,7 +10325,7 @@ var AnthropicPlugin = class extends BasePlugin {
10182
10325
  return message ? { role: message.role, content: message.content } : null;
10183
10326
  },
10184
10327
  extractMetrics: (message, startTime) => {
10185
- const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _137 => _137.usage]));
10328
+ const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _141 => _141.usage]));
10186
10329
  if (startTime) {
10187
10330
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10188
10331
  }
@@ -10197,7 +10340,7 @@ var AnthropicPlugin = class extends BasePlugin {
10197
10340
  const metadata = {};
10198
10341
  const metas = ["stop_reason", "stop_sequence"];
10199
10342
  for (const m of metas) {
10200
- if (_optionalChain([message, 'optionalAccess', _138 => _138[m]]) !== void 0) {
10343
+ if (_optionalChain([message, 'optionalAccess', _142 => _142[m]]) !== void 0) {
10201
10344
  metadata[m] = message[m];
10202
10345
  }
10203
10346
  }
@@ -10238,15 +10381,15 @@ function aggregateAnthropicStreamChunks(chunks) {
10238
10381
  let metrics = {};
10239
10382
  let metadata = {};
10240
10383
  for (const event of chunks) {
10241
- switch (_optionalChain([event, 'optionalAccess', _139 => _139.type])) {
10384
+ switch (_optionalChain([event, 'optionalAccess', _143 => _143.type])) {
10242
10385
  case "message_start":
10243
- if (_optionalChain([event, 'access', _140 => _140.message, 'optionalAccess', _141 => _141.usage])) {
10386
+ if (_optionalChain([event, 'access', _144 => _144.message, 'optionalAccess', _145 => _145.usage])) {
10244
10387
  const initialMetrics = parseMetricsFromUsage2(event.message.usage);
10245
10388
  metrics = { ...metrics, ...initialMetrics };
10246
10389
  }
10247
10390
  break;
10248
10391
  case "content_block_delta":
10249
- if (_optionalChain([event, 'access', _142 => _142.delta, 'optionalAccess', _143 => _143.type]) === "text_delta") {
10392
+ if (_optionalChain([event, 'access', _146 => _146.delta, 'optionalAccess', _147 => _147.type]) === "text_delta") {
10250
10393
  const text = event.delta.text;
10251
10394
  if (text) {
10252
10395
  deltas.push(text);
@@ -10341,6 +10484,108 @@ function filterFrom(obj, fieldsToRemove) {
10341
10484
  return result;
10342
10485
  }
10343
10486
 
10487
+ // src/wrappers/ai-sdk/normalize-logged-output.ts
10488
+ var REMOVE_NORMALIZED_VALUE = Symbol("braintrust.ai-sdk.remove-normalized");
10489
+ function normalizeAISDKLoggedOutput(value) {
10490
+ const normalized = normalizeAISDKLoggedValue(value);
10491
+ return normalized === REMOVE_NORMALIZED_VALUE ? {} : normalized;
10492
+ }
10493
+ function normalizeAISDKLoggedValue(value, context = {}) {
10494
+ if (Array.isArray(value)) {
10495
+ return value.map((entry) => normalizeAISDKLoggedValue(entry, context)).filter((entry) => entry !== REMOVE_NORMALIZED_VALUE);
10496
+ }
10497
+ if (!value || typeof value !== "object") {
10498
+ return value;
10499
+ }
10500
+ const nextInProviderMetadata = context.inProviderMetadata || context.parentKey === "providerMetadata" || context.parentKey === "experimental_providerMetadata";
10501
+ const normalizedEntries = [];
10502
+ for (const [key, entry] of Object.entries(value)) {
10503
+ if (key === "cachedPromptTokens" && entry === 0) {
10504
+ continue;
10505
+ }
10506
+ if (context.parentKey === "request" && key === "body" && entry === "<omitted>") {
10507
+ continue;
10508
+ }
10509
+ const normalizedEntry = normalizeAISDKLoggedValue(entry, {
10510
+ inProviderMetadata: nextInProviderMetadata,
10511
+ parentKey: key
10512
+ });
10513
+ if (normalizedEntry === REMOVE_NORMALIZED_VALUE) {
10514
+ continue;
10515
+ }
10516
+ normalizedEntries.push([key, normalizedEntry]);
10517
+ }
10518
+ if (normalizedEntries.length === 0) {
10519
+ if (context.parentKey === "request" || nextInProviderMetadata) {
10520
+ return REMOVE_NORMALIZED_VALUE;
10521
+ }
10522
+ return {};
10523
+ }
10524
+ return Object.fromEntries(normalizedEntries);
10525
+ }
10526
+
10527
+ // src/zod/utils.ts
10528
+ var _zodtojsonschema = require('zod-to-json-schema');
10529
+ var _v4 = require('zod/v4'); var z42 = _interopRequireWildcard(_v4);
10530
+ function isZodV4(zodObject) {
10531
+ return typeof zodObject === "object" && zodObject !== null && "_zod" in zodObject && zodObject._zod !== void 0;
10532
+ }
10533
+ function zodToJsonSchema(schema) {
10534
+ if (isZodV4(schema)) {
10535
+ return z42.toJSONSchema(schema, {
10536
+ target: "draft-7"
10537
+ });
10538
+ }
10539
+ return _zodtojsonschema.zodToJsonSchema.call(void 0, schema);
10540
+ }
10541
+
10542
+ // src/wrappers/ai-sdk/tool-serialization.ts
10543
+ function isZodSchema(value) {
10544
+ return value != null && typeof value === "object" && "_def" in value && typeof value._def === "object";
10545
+ }
10546
+ function serializeZodSchema(schema) {
10547
+ try {
10548
+ return zodToJsonSchema(schema);
10549
+ } catch (e20) {
10550
+ return {
10551
+ type: "object",
10552
+ description: "Zod schema (conversion failed)"
10553
+ };
10554
+ }
10555
+ }
10556
+ function serializeTool(tool) {
10557
+ if (!tool || typeof tool !== "object") {
10558
+ return tool;
10559
+ }
10560
+ const serialized = { ...tool };
10561
+ if (isZodSchema(serialized.inputSchema)) {
10562
+ serialized.inputSchema = serializeZodSchema(serialized.inputSchema);
10563
+ }
10564
+ if (isZodSchema(serialized.parameters)) {
10565
+ serialized.parameters = serializeZodSchema(serialized.parameters);
10566
+ }
10567
+ if ("execute" in serialized) {
10568
+ delete serialized.execute;
10569
+ }
10570
+ if ("render" in serialized) {
10571
+ delete serialized.render;
10572
+ }
10573
+ return serialized;
10574
+ }
10575
+ function serializeAISDKToolsForLogging(tools) {
10576
+ if (!tools || typeof tools !== "object") {
10577
+ return tools;
10578
+ }
10579
+ if (Array.isArray(tools)) {
10580
+ return tools.map(serializeTool);
10581
+ }
10582
+ const serialized = {};
10583
+ for (const [key, tool] of Object.entries(tools)) {
10584
+ serialized[key] = serializeTool(tool);
10585
+ }
10586
+ return serialized;
10587
+ }
10588
+
10344
10589
  // src/instrumentation/plugins/ai-sdk-channels.ts
10345
10590
  var aiSDKChannels = defineChannels("ai", {
10346
10591
  generateText: channel({
@@ -10351,6 +10596,10 @@ var aiSDKChannels = defineChannels("ai", {
10351
10596
  channelName: "streamText",
10352
10597
  kind: "async"
10353
10598
  }),
10599
+ streamTextSync: channel({
10600
+ channelName: "streamText.sync",
10601
+ kind: "sync-stream"
10602
+ }),
10354
10603
  generateObject: channel({
10355
10604
  channelName: "generateObject",
10356
10605
  kind: "async"
@@ -10359,6 +10608,10 @@ var aiSDKChannels = defineChannels("ai", {
10359
10608
  channelName: "streamObject",
10360
10609
  kind: "async"
10361
10610
  }),
10611
+ streamObjectSync: channel({
10612
+ channelName: "streamObject.sync",
10613
+ kind: "sync-stream"
10614
+ }),
10362
10615
  agentGenerate: channel({
10363
10616
  channelName: "Agent.generate",
10364
10617
  kind: "async"
@@ -10366,6 +10619,14 @@ var aiSDKChannels = defineChannels("ai", {
10366
10619
  agentStream: channel({
10367
10620
  channelName: "Agent.stream",
10368
10621
  kind: "async"
10622
+ }),
10623
+ toolLoopAgentGenerate: channel({
10624
+ channelName: "ToolLoopAgent.generate",
10625
+ kind: "async"
10626
+ }),
10627
+ toolLoopAgentStream: channel({
10628
+ channelName: "ToolLoopAgent.stream",
10629
+ kind: "async"
10369
10630
  })
10370
10631
  });
10371
10632
 
@@ -10384,6 +10645,8 @@ var DEFAULT_DENY_OUTPUT_PATHS = [
10384
10645
  "steps[].response.body",
10385
10646
  "steps[].response.headers"
10386
10647
  ];
10648
+ var AUTO_PATCHED_MODEL = Symbol.for("braintrust.ai-sdk.auto-patched-model");
10649
+ var AUTO_PATCHED_TOOL = Symbol.for("braintrust.ai-sdk.auto-patched-tool");
10387
10650
  var AISDKPlugin = class extends BasePlugin {
10388
10651
 
10389
10652
  constructor(config = {}) {
@@ -10402,22 +10665,12 @@ var AISDKPlugin = class extends BasePlugin {
10402
10665
  traceStreamingChannel(aiSDKChannels.generateText, {
10403
10666
  name: "generateText",
10404
10667
  type: "llm" /* LLM */,
10405
- extractInput: ([params]) => {
10406
- return {
10407
- input: processAISDKInput(params),
10408
- metadata: extractMetadataFromParams(params)
10409
- };
10410
- },
10411
- extractOutput: (result) => {
10668
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10669
+ extractOutput: (result, endEvent) => {
10670
+ finalizeAISDKChildTracing(endEvent);
10412
10671
  return processAISDKOutput(result, denyOutputPaths);
10413
10672
  },
10414
- extractMetrics: (result, startTime) => {
10415
- const metrics = extractTokenMetrics(result);
10416
- if (startTime) {
10417
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10418
- }
10419
- return metrics;
10420
- },
10673
+ extractMetrics: (result, _startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent),
10421
10674
  aggregateChunks: aggregateAISDKChunks
10422
10675
  })
10423
10676
  );
@@ -10425,45 +10678,43 @@ var AISDKPlugin = class extends BasePlugin {
10425
10678
  traceStreamingChannel(aiSDKChannels.streamText, {
10426
10679
  name: "streamText",
10427
10680
  type: "llm" /* LLM */,
10428
- extractInput: ([params]) => {
10429
- return {
10430
- input: processAISDKInput(params),
10431
- metadata: extractMetadataFromParams(params)
10432
- };
10433
- },
10434
- extractOutput: (result) => {
10435
- return processAISDKOutput(result, denyOutputPaths);
10436
- },
10437
- extractMetrics: (result, startTime) => {
10438
- const metrics = extractTokenMetrics(result);
10439
- if (startTime) {
10440
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10441
- }
10442
- return metrics;
10443
- },
10444
- aggregateChunks: aggregateAISDKChunks
10681
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10682
+ extractOutput: (result) => processAISDKOutput(result, denyOutputPaths),
10683
+ extractMetrics: (result, startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent, startTime),
10684
+ aggregateChunks: aggregateAISDKChunks,
10685
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
10686
+ denyOutputPaths,
10687
+ endEvent,
10688
+ result,
10689
+ span,
10690
+ startTime
10691
+ })
10692
+ })
10693
+ );
10694
+ this.unsubscribers.push(
10695
+ traceSyncStreamChannel(aiSDKChannels.streamTextSync, {
10696
+ name: "streamText",
10697
+ type: "llm" /* LLM */,
10698
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10699
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
10700
+ denyOutputPaths,
10701
+ endEvent,
10702
+ result,
10703
+ span,
10704
+ startTime
10705
+ })
10445
10706
  })
10446
10707
  );
10447
10708
  this.unsubscribers.push(
10448
10709
  traceStreamingChannel(aiSDKChannels.generateObject, {
10449
10710
  name: "generateObject",
10450
10711
  type: "llm" /* LLM */,
10451
- extractInput: ([params]) => {
10452
- return {
10453
- input: processAISDKInput(params),
10454
- metadata: extractMetadataFromParams(params)
10455
- };
10456
- },
10457
- extractOutput: (result) => {
10712
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10713
+ extractOutput: (result, endEvent) => {
10714
+ finalizeAISDKChildTracing(endEvent);
10458
10715
  return processAISDKOutput(result, denyOutputPaths);
10459
10716
  },
10460
- extractMetrics: (result, startTime) => {
10461
- const metrics = extractTokenMetrics(result);
10462
- if (startTime) {
10463
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10464
- }
10465
- return metrics;
10466
- },
10717
+ extractMetrics: (result, _startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent),
10467
10718
  aggregateChunks: aggregateAISDKChunks
10468
10719
  })
10469
10720
  );
@@ -10471,45 +10722,43 @@ var AISDKPlugin = class extends BasePlugin {
10471
10722
  traceStreamingChannel(aiSDKChannels.streamObject, {
10472
10723
  name: "streamObject",
10473
10724
  type: "llm" /* LLM */,
10474
- extractInput: ([params]) => {
10475
- return {
10476
- input: processAISDKInput(params),
10477
- metadata: extractMetadataFromParams(params)
10478
- };
10479
- },
10480
- extractOutput: (result) => {
10481
- return processAISDKOutput(result, denyOutputPaths);
10482
- },
10483
- extractMetrics: (result, startTime) => {
10484
- const metrics = extractTokenMetrics(result);
10485
- if (startTime) {
10486
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10487
- }
10488
- return metrics;
10489
- },
10490
- aggregateChunks: aggregateAISDKChunks
10725
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10726
+ extractOutput: (result) => processAISDKOutput(result, denyOutputPaths),
10727
+ extractMetrics: (result, startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent, startTime),
10728
+ aggregateChunks: aggregateAISDKChunks,
10729
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
10730
+ denyOutputPaths,
10731
+ endEvent,
10732
+ result,
10733
+ span,
10734
+ startTime
10735
+ })
10736
+ })
10737
+ );
10738
+ this.unsubscribers.push(
10739
+ traceSyncStreamChannel(aiSDKChannels.streamObjectSync, {
10740
+ name: "streamObject",
10741
+ type: "llm" /* LLM */,
10742
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10743
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
10744
+ denyOutputPaths,
10745
+ endEvent,
10746
+ result,
10747
+ span,
10748
+ startTime
10749
+ })
10491
10750
  })
10492
10751
  );
10493
10752
  this.unsubscribers.push(
10494
10753
  traceStreamingChannel(aiSDKChannels.agentGenerate, {
10495
10754
  name: "Agent.generate",
10496
10755
  type: "llm" /* LLM */,
10497
- extractInput: ([params]) => {
10498
- return {
10499
- input: processAISDKInput(params),
10500
- metadata: extractMetadataFromParams(params)
10501
- };
10502
- },
10503
- extractOutput: (result) => {
10756
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10757
+ extractOutput: (result, endEvent) => {
10758
+ finalizeAISDKChildTracing(endEvent);
10504
10759
  return processAISDKOutput(result, denyOutputPaths);
10505
10760
  },
10506
- extractMetrics: (result, startTime) => {
10507
- const metrics = extractTokenMetrics(result);
10508
- if (startTime) {
10509
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10510
- }
10511
- return metrics;
10512
- },
10761
+ extractMetrics: (result, _startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent),
10513
10762
  aggregateChunks: aggregateAISDKChunks
10514
10763
  })
10515
10764
  );
@@ -10517,56 +10766,474 @@ var AISDKPlugin = class extends BasePlugin {
10517
10766
  traceStreamingChannel(aiSDKChannels.agentStream, {
10518
10767
  name: "Agent.stream",
10519
10768
  type: "llm" /* LLM */,
10520
- extractInput: ([params]) => {
10521
- return {
10522
- input: processAISDKInput(params),
10523
- metadata: extractMetadataFromParams(params)
10524
- };
10525
- },
10526
- extractOutput: (result) => {
10769
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10770
+ extractOutput: (result) => processAISDKOutput(result, denyOutputPaths),
10771
+ extractMetrics: (result, startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent, startTime),
10772
+ aggregateChunks: aggregateAISDKChunks,
10773
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
10774
+ denyOutputPaths,
10775
+ endEvent,
10776
+ result,
10777
+ span,
10778
+ startTime
10779
+ })
10780
+ })
10781
+ );
10782
+ this.unsubscribers.push(
10783
+ traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
10784
+ name: "ToolLoopAgent.generate",
10785
+ type: "llm" /* LLM */,
10786
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10787
+ extractOutput: (result, endEvent) => {
10788
+ finalizeAISDKChildTracing(endEvent);
10527
10789
  return processAISDKOutput(result, denyOutputPaths);
10528
10790
  },
10529
- extractMetrics: (result, startTime) => {
10530
- const metrics = extractTokenMetrics(result);
10531
- if (startTime) {
10532
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10533
- }
10534
- return metrics;
10535
- },
10791
+ extractMetrics: (result, _startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent),
10536
10792
  aggregateChunks: aggregateAISDKChunks
10537
10793
  })
10538
10794
  );
10795
+ this.unsubscribers.push(
10796
+ traceStreamingChannel(aiSDKChannels.toolLoopAgentStream, {
10797
+ name: "ToolLoopAgent.stream",
10798
+ type: "llm" /* LLM */,
10799
+ extractInput: ([params], event, span) => prepareAISDKInput(params, event, span, denyOutputPaths),
10800
+ extractOutput: (result) => processAISDKOutput(result, denyOutputPaths),
10801
+ extractMetrics: (result, startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent, startTime),
10802
+ aggregateChunks: aggregateAISDKChunks,
10803
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
10804
+ denyOutputPaths,
10805
+ endEvent,
10806
+ result,
10807
+ span,
10808
+ startTime
10809
+ })
10810
+ })
10811
+ );
10539
10812
  }
10540
10813
  };
10541
10814
  function processAISDKInput(params) {
10542
10815
  if (!params) return params;
10543
- return processInputAttachments(params);
10816
+ const input = processInputAttachments(params);
10817
+ if (!input || typeof input !== "object" || Array.isArray(input)) {
10818
+ return input;
10819
+ }
10820
+ const { tools: _tools, ...rest } = input;
10821
+ return rest;
10822
+ }
10823
+ function prepareAISDKInput(params, event, span, denyOutputPaths) {
10824
+ const input = processAISDKInput(params);
10825
+ const metadata = extractMetadataFromParams(params, event.self);
10826
+ const childTracing = prepareAISDKChildTracing(
10827
+ params,
10828
+ event.self,
10829
+ span,
10830
+ denyOutputPaths
10831
+ );
10832
+ event.__braintrust_ai_sdk_model_wrapped = childTracing.modelWrapped;
10833
+ if (childTracing.cleanup) {
10834
+ event.__braintrust_ai_sdk_cleanup = childTracing.cleanup;
10835
+ }
10836
+ return {
10837
+ input,
10838
+ metadata
10839
+ };
10840
+ }
10841
+ function extractTopLevelAISDKMetrics(result, event, startTime) {
10842
+ const metrics = hasModelChildTracing(event) ? {} : extractTokenMetrics(result);
10843
+ if (startTime) {
10844
+ metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
10845
+ }
10846
+ return metrics;
10544
10847
  }
10545
- function extractMetadataFromParams(params) {
10848
+ function hasModelChildTracing(event) {
10849
+ return _optionalChain([event, 'optionalAccess', _148 => _148.__braintrust_ai_sdk_model_wrapped]) === true;
10850
+ }
10851
+ function extractMetadataFromParams(params, self) {
10546
10852
  const metadata = {
10547
10853
  braintrust: {
10548
10854
  integration_name: "ai-sdk",
10549
10855
  sdk_language: "typescript"
10550
10856
  }
10551
10857
  };
10552
- const { model, provider } = serializeModelWithProvider(params.model);
10858
+ const agentModel = self && typeof self === "object" && "model" in self && self.model ? self.model : self && typeof self === "object" && "settings" in self && _optionalChain([self, 'access', _149 => _149.settings, 'optionalAccess', _150 => _150.model]) ? _optionalChain([self, 'access', _151 => _151.settings, 'optionalAccess', _152 => _152.model]) : void 0;
10859
+ const { model, provider } = serializeModelWithProvider(
10860
+ _nullishCoalesce(params.model, () => ( agentModel))
10861
+ );
10553
10862
  if (model) {
10554
10863
  metadata.model = model;
10555
10864
  }
10556
10865
  if (provider) {
10557
10866
  metadata.provider = provider;
10558
10867
  }
10868
+ const tools = serializeAISDKToolsForLogging(params.tools);
10869
+ if (tools) {
10870
+ metadata.tools = tools;
10871
+ }
10559
10872
  return metadata;
10560
10873
  }
10874
+ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths) {
10875
+ const cleanup = [];
10876
+ const patchedModels = /* @__PURE__ */ new WeakSet();
10877
+ const patchedTools = /* @__PURE__ */ new WeakSet();
10878
+ let modelWrapped = false;
10879
+ const patchModel = (model) => {
10880
+ const resolvedModel = resolveAISDKModel(model);
10881
+ if (!resolvedModel || typeof resolvedModel !== "object" || typeof resolvedModel.doGenerate !== "function" || patchedModels.has(resolvedModel) || resolvedModel[AUTO_PATCHED_MODEL]) {
10882
+ return;
10883
+ }
10884
+ patchedModels.add(resolvedModel);
10885
+ resolvedModel[AUTO_PATCHED_MODEL] = true;
10886
+ modelWrapped = true;
10887
+ const originalDoGenerate = resolvedModel.doGenerate;
10888
+ const originalDoStream = resolvedModel.doStream;
10889
+ const baseMetadata = buildAISDKChildMetadata(resolvedModel);
10890
+ resolvedModel.doGenerate = async function doGeneratePatched(options) {
10891
+ return parentSpan.traced(
10892
+ async (span) => {
10893
+ const result = await Reflect.apply(
10894
+ originalDoGenerate,
10895
+ resolvedModel,
10896
+ [options]
10897
+ );
10898
+ span.log({
10899
+ output: processAISDKOutput(result, denyOutputPaths),
10900
+ metrics: extractTokenMetrics(result),
10901
+ ...buildResolvedMetadataPayload(result)
10902
+ });
10903
+ return result;
10904
+ },
10905
+ {
10906
+ name: "doGenerate",
10907
+ spanAttributes: {
10908
+ type: "llm" /* LLM */
10909
+ },
10910
+ event: {
10911
+ input: processAISDKInput(options),
10912
+ metadata: baseMetadata
10913
+ }
10914
+ }
10915
+ );
10916
+ };
10917
+ if (originalDoStream) {
10918
+ resolvedModel.doStream = async function doStreamPatched(options) {
10919
+ const span = parentSpan.startSpan({
10920
+ name: "doStream",
10921
+ spanAttributes: {
10922
+ type: "llm" /* LLM */
10923
+ },
10924
+ event: {
10925
+ input: processAISDKInput(options),
10926
+ metadata: baseMetadata
10927
+ }
10928
+ });
10929
+ const result = await withCurrent(
10930
+ span,
10931
+ () => Reflect.apply(originalDoStream, resolvedModel, [options])
10932
+ );
10933
+ const output = {};
10934
+ let text = "";
10935
+ let reasoning = "";
10936
+ const toolCalls = [];
10937
+ let object = void 0;
10938
+ const transformStream = new TransformStream({
10939
+ transform(chunk, controller) {
10940
+ switch (chunk.type) {
10941
+ case "text-delta":
10942
+ text += extractTextDelta(chunk);
10943
+ break;
10944
+ case "reasoning-delta":
10945
+ if (chunk.delta) {
10946
+ reasoning += chunk.delta;
10947
+ } else if (chunk.text) {
10948
+ reasoning += chunk.text;
10949
+ }
10950
+ break;
10951
+ case "tool-call":
10952
+ toolCalls.push(chunk);
10953
+ break;
10954
+ case "object":
10955
+ object = chunk.object;
10956
+ break;
10957
+ case "raw":
10958
+ if (chunk.rawValue) {
10959
+ const rawVal = chunk.rawValue;
10960
+ if (_optionalChain([rawVal, 'access', _153 => _153.delta, 'optionalAccess', _154 => _154.content])) {
10961
+ text += rawVal.delta.content;
10962
+ } else if (_optionalChain([rawVal, 'access', _155 => _155.choices, 'optionalAccess', _156 => _156[0], 'optionalAccess', _157 => _157.delta, 'optionalAccess', _158 => _158.content])) {
10963
+ text += rawVal.choices[0].delta.content;
10964
+ } else if (typeof rawVal.text === "string") {
10965
+ text += rawVal.text;
10966
+ } else if (typeof rawVal.content === "string") {
10967
+ text += rawVal.content;
10968
+ }
10969
+ }
10970
+ break;
10971
+ case "finish":
10972
+ output.text = text;
10973
+ output.reasoning = reasoning;
10974
+ output.toolCalls = toolCalls;
10975
+ output.finishReason = chunk.finishReason;
10976
+ output.usage = chunk.usage;
10977
+ if (object !== void 0) {
10978
+ output.object = object;
10979
+ }
10980
+ span.log({
10981
+ output: processAISDKOutput(
10982
+ output,
10983
+ denyOutputPaths
10984
+ ),
10985
+ metrics: extractTokenMetrics(output),
10986
+ ...buildResolvedMetadataPayload(output)
10987
+ });
10988
+ span.end();
10989
+ break;
10990
+ }
10991
+ controller.enqueue(chunk);
10992
+ }
10993
+ });
10994
+ return {
10995
+ ...result,
10996
+ stream: result.stream.pipeThrough(transformStream)
10997
+ };
10998
+ };
10999
+ }
11000
+ cleanup.push(() => {
11001
+ resolvedModel.doGenerate = originalDoGenerate;
11002
+ if (originalDoStream) {
11003
+ resolvedModel.doStream = originalDoStream;
11004
+ }
11005
+ delete resolvedModel[AUTO_PATCHED_MODEL];
11006
+ });
11007
+ };
11008
+ const patchTool = (tool, name) => {
11009
+ if (tool == null || typeof tool !== "object" || !("execute" in tool) || typeof tool.execute !== "function" || patchedTools.has(tool) || tool[AUTO_PATCHED_TOOL]) {
11010
+ return;
11011
+ }
11012
+ patchedTools.add(tool);
11013
+ tool[AUTO_PATCHED_TOOL] = true;
11014
+ const originalExecute = tool.execute;
11015
+ tool.execute = function executePatched(...args) {
11016
+ const result = Reflect.apply(originalExecute, this, args);
11017
+ if (isAsyncGenerator(result)) {
11018
+ return (async function* () {
11019
+ const span = parentSpan.startSpan({
11020
+ name,
11021
+ spanAttributes: {
11022
+ type: "tool" /* TOOL */
11023
+ }
11024
+ });
11025
+ span.log({ input: args.length === 1 ? args[0] : args });
11026
+ try {
11027
+ let lastValue;
11028
+ for await (const value of result) {
11029
+ lastValue = value;
11030
+ yield value;
11031
+ }
11032
+ span.log({ output: lastValue });
11033
+ } catch (error) {
11034
+ span.log({
11035
+ error: error instanceof Error ? error.message : String(error)
11036
+ });
11037
+ throw error;
11038
+ } finally {
11039
+ span.end();
11040
+ }
11041
+ })();
11042
+ }
11043
+ return parentSpan.traced(
11044
+ async (span) => {
11045
+ span.log({ input: args.length === 1 ? args[0] : args });
11046
+ const awaitedResult = await result;
11047
+ span.log({ output: awaitedResult });
11048
+ return awaitedResult;
11049
+ },
11050
+ {
11051
+ name,
11052
+ spanAttributes: {
11053
+ type: "tool" /* TOOL */
11054
+ }
11055
+ }
11056
+ );
11057
+ };
11058
+ cleanup.push(() => {
11059
+ tool.execute = originalExecute;
11060
+ delete tool[AUTO_PATCHED_TOOL];
11061
+ });
11062
+ };
11063
+ const patchTools = (tools) => {
11064
+ if (!tools) {
11065
+ return;
11066
+ }
11067
+ const inferName = (tool, fallback2) => tool && (tool.name || tool.toolName || tool.id) || fallback2;
11068
+ if (Array.isArray(tools)) {
11069
+ tools.forEach(
11070
+ (tool, index) => patchTool(tool, inferName(tool, `tool[${index}]`))
11071
+ );
11072
+ return;
11073
+ }
11074
+ for (const [key, tool] of Object.entries(tools)) {
11075
+ patchTool(tool, key);
11076
+ }
11077
+ };
11078
+ if (params && typeof params === "object") {
11079
+ patchModel(params.model);
11080
+ patchTools(params.tools);
11081
+ }
11082
+ if (self && typeof self === "object") {
11083
+ const selfRecord = self;
11084
+ if (selfRecord.model !== void 0) {
11085
+ patchModel(selfRecord.model);
11086
+ }
11087
+ if (selfRecord.settings && typeof selfRecord.settings === "object") {
11088
+ if (selfRecord.settings.model !== void 0) {
11089
+ patchModel(selfRecord.settings.model);
11090
+ }
11091
+ if (selfRecord.settings.tools !== void 0) {
11092
+ patchTools(selfRecord.settings.tools);
11093
+ }
11094
+ }
11095
+ }
11096
+ return {
11097
+ cleanup: cleanup.length > 0 ? () => {
11098
+ while (cleanup.length > 0) {
11099
+ _optionalChain([cleanup, 'access', _159 => _159.pop, 'call', _160 => _160(), 'optionalCall', _161 => _161()]);
11100
+ }
11101
+ } : void 0,
11102
+ modelWrapped
11103
+ };
11104
+ }
11105
+ function finalizeAISDKChildTracing(event) {
11106
+ const cleanup = _optionalChain([event, 'optionalAccess', _162 => _162.__braintrust_ai_sdk_cleanup]);
11107
+ if (event && typeof cleanup === "function") {
11108
+ cleanup();
11109
+ delete event.__braintrust_ai_sdk_cleanup;
11110
+ }
11111
+ }
11112
+ function patchAISDKStreamingResult(args) {
11113
+ const { denyOutputPaths, endEvent, result, span, startTime } = args;
11114
+ if (!result || typeof result !== "object") {
11115
+ return false;
11116
+ }
11117
+ const resultRecord = result;
11118
+ if (!isReadableStreamLike(resultRecord.baseStream)) {
11119
+ return false;
11120
+ }
11121
+ let firstChunkTime;
11122
+ const wrappedBaseStream = resultRecord.baseStream.pipeThrough(
11123
+ new TransformStream({
11124
+ transform(chunk, controller) {
11125
+ if (firstChunkTime === void 0) {
11126
+ firstChunkTime = getCurrentUnixTimestamp();
11127
+ }
11128
+ controller.enqueue(chunk);
11129
+ },
11130
+ async flush() {
11131
+ const metrics = extractTopLevelAISDKMetrics(result, endEvent);
11132
+ if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
11133
+ metrics.time_to_first_token = firstChunkTime - startTime;
11134
+ }
11135
+ const output = await processAISDKStreamingOutput(
11136
+ result,
11137
+ denyOutputPaths
11138
+ );
11139
+ const metadata = buildResolvedMetadataPayload(result).metadata;
11140
+ span.log({
11141
+ output,
11142
+ ...metadata ? { metadata } : {},
11143
+ metrics
11144
+ });
11145
+ finalizeAISDKChildTracing(endEvent);
11146
+ span.end();
11147
+ }
11148
+ })
11149
+ );
11150
+ Object.defineProperty(resultRecord, "baseStream", {
11151
+ configurable: true,
11152
+ enumerable: true,
11153
+ value: wrappedBaseStream,
11154
+ writable: true
11155
+ });
11156
+ return true;
11157
+ }
11158
+ function isReadableStreamLike(value) {
11159
+ return value != null && typeof value === "object" && typeof value.pipeThrough === "function";
11160
+ }
11161
+ async function processAISDKStreamingOutput(result, denyOutputPaths) {
11162
+ const output = processAISDKOutput(result, denyOutputPaths);
11163
+ if (!output || typeof output !== "object") {
11164
+ return output;
11165
+ }
11166
+ const outputRecord = output;
11167
+ try {
11168
+ if ("text" in result && typeof result.text === "string") {
11169
+ outputRecord.text = result.text;
11170
+ }
11171
+ } catch (e21) {
11172
+ }
11173
+ try {
11174
+ if ("object" in result) {
11175
+ const resolvedObject = await Promise.resolve(result.object);
11176
+ if (resolvedObject !== void 0) {
11177
+ outputRecord.object = resolvedObject;
11178
+ }
11179
+ }
11180
+ } catch (e22) {
11181
+ }
11182
+ return outputRecord;
11183
+ }
11184
+ function buildAISDKChildMetadata(model) {
11185
+ const { model: modelId, provider } = serializeModelWithProvider(model);
11186
+ return {
11187
+ ...modelId ? { model: modelId } : {},
11188
+ ...provider ? { provider } : {},
11189
+ braintrust: {
11190
+ integration_name: "ai-sdk",
11191
+ sdk_language: "typescript"
11192
+ }
11193
+ };
11194
+ }
11195
+ function buildResolvedMetadataPayload(result) {
11196
+ const gatewayInfo = extractGatewayRoutingInfo(result);
11197
+ const metadata = {};
11198
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _163 => _163.provider])) {
11199
+ metadata.provider = gatewayInfo.provider;
11200
+ }
11201
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _164 => _164.model])) {
11202
+ metadata.model = gatewayInfo.model;
11203
+ }
11204
+ if (result.finishReason !== void 0) {
11205
+ metadata.finish_reason = result.finishReason;
11206
+ }
11207
+ return Object.keys(metadata).length > 0 ? { metadata } : {};
11208
+ }
11209
+ function resolveAISDKModel(model) {
11210
+ if (typeof model !== "string") {
11211
+ return model;
11212
+ }
11213
+ const provider = _nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( null));
11214
+ if (provider && typeof provider.languageModel === "function") {
11215
+ return provider.languageModel(model);
11216
+ }
11217
+ return model;
11218
+ }
11219
+ function extractTextDelta(chunk) {
11220
+ if (typeof chunk.textDelta === "string") return chunk.textDelta;
11221
+ if (typeof chunk.delta === "string") return chunk.delta;
11222
+ if (typeof chunk.text === "string") return chunk.text;
11223
+ if (typeof chunk.content === "string") return chunk.content;
11224
+ return "";
11225
+ }
11226
+ function isAsyncGenerator(value) {
11227
+ return value != null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function" && typeof value.next === "function" && typeof value.return === "function" && typeof value.throw === "function";
11228
+ }
10561
11229
  function processAISDKOutput(output, denyOutputPaths) {
10562
11230
  if (!output) return output;
10563
- const getterValues = extractGetterValues(output);
10564
- const merged = { ...output, ...getterValues };
10565
- return omit(merged, denyOutputPaths);
11231
+ const merged = extractSerializableOutputFields(output);
11232
+ return normalizeAISDKLoggedOutput(omit(merged, denyOutputPaths));
10566
11233
  }
10567
11234
  function extractTokenMetrics(result) {
10568
11235
  const metrics = {};
10569
- let usage = _optionalChain([result, 'optionalAccess', _144 => _144.totalUsage]) || _optionalChain([result, 'optionalAccess', _145 => _145.usage]);
11236
+ let usage = _optionalChain([result, 'optionalAccess', _165 => _165.totalUsage]) || _optionalChain([result, 'optionalAccess', _166 => _166.usage]);
10570
11237
  if (!usage && result) {
10571
11238
  try {
10572
11239
  if ("totalUsage" in result && typeof result.totalUsage !== "function") {
@@ -10574,14 +11241,14 @@ function extractTokenMetrics(result) {
10574
11241
  } else if ("usage" in result && typeof result.usage !== "function") {
10575
11242
  usage = result.usage;
10576
11243
  }
10577
- } catch (e19) {
11244
+ } catch (e23) {
10578
11245
  }
10579
11246
  }
10580
11247
  if (!usage) {
10581
11248
  return metrics;
10582
11249
  }
10583
11250
  const promptTokens = firstNumber(
10584
- _optionalChain([usage, 'access', _146 => _146.inputTokens, 'optionalAccess', _147 => _147.total]),
11251
+ _optionalChain([usage, 'access', _167 => _167.inputTokens, 'optionalAccess', _168 => _168.total]),
10585
11252
  usage.inputTokens,
10586
11253
  usage.promptTokens,
10587
11254
  usage.prompt_tokens
@@ -10590,7 +11257,7 @@ function extractTokenMetrics(result) {
10590
11257
  metrics.prompt_tokens = promptTokens;
10591
11258
  }
10592
11259
  const completionTokens = firstNumber(
10593
- _optionalChain([usage, 'access', _148 => _148.outputTokens, 'optionalAccess', _149 => _149.total]),
11260
+ _optionalChain([usage, 'access', _169 => _169.outputTokens, 'optionalAccess', _170 => _170.total]),
10594
11261
  usage.outputTokens,
10595
11262
  usage.completionTokens,
10596
11263
  usage.completion_tokens
@@ -10612,12 +11279,14 @@ function extractTokenMetrics(result) {
10612
11279
  }
10613
11280
  return metrics;
10614
11281
  }
10615
- function aggregateAISDKChunks(chunks) {
11282
+ function aggregateAISDKChunks(chunks, _result, endEvent) {
10616
11283
  const lastChunk = chunks[chunks.length - 1];
10617
11284
  const output = {};
10618
11285
  let metrics = {};
11286
+ let metadata;
10619
11287
  if (lastChunk) {
10620
- metrics = extractTokenMetrics(lastChunk);
11288
+ metrics = hasModelChildTracing(endEvent) ? {} : extractTokenMetrics(lastChunk);
11289
+ metadata = buildResolvedMetadataPayload(lastChunk).metadata;
10621
11290
  if (lastChunk.text !== void 0) {
10622
11291
  output.text = lastChunk.text;
10623
11292
  }
@@ -10631,7 +11300,8 @@ function aggregateAISDKChunks(chunks) {
10631
11300
  output.toolCalls = lastChunk.toolCalls;
10632
11301
  }
10633
11302
  }
10634
- return { output, metrics };
11303
+ finalizeAISDKChildTracing(endEvent);
11304
+ return { output, metrics, metadata };
10635
11305
  }
10636
11306
  function extractGetterValues(obj) {
10637
11307
  const getterValues = {};
@@ -10651,17 +11321,58 @@ function extractGetterValues(obj) {
10651
11321
  ];
10652
11322
  for (const name of getterNames) {
10653
11323
  try {
10654
- if (obj && name in obj && typeof obj[name] !== "function") {
11324
+ if (obj && name in obj && isSerializableOutputValue(obj[name])) {
10655
11325
  getterValues[name] = obj[name];
10656
11326
  }
10657
- } catch (e20) {
11327
+ } catch (e24) {
10658
11328
  }
10659
11329
  }
10660
11330
  return getterValues;
10661
11331
  }
11332
+ function extractSerializableOutputFields(output) {
11333
+ const serialized = {};
11334
+ const directFieldNames = [
11335
+ "steps",
11336
+ "request",
11337
+ "responseMessages",
11338
+ "warnings",
11339
+ "rawResponse",
11340
+ "response",
11341
+ "providerMetadata",
11342
+ "experimental_providerMetadata"
11343
+ ];
11344
+ for (const name of directFieldNames) {
11345
+ try {
11346
+ const value = _optionalChain([output, 'optionalAccess', _171 => _171[name]]);
11347
+ if (isSerializableOutputValue(value)) {
11348
+ serialized[name] = value;
11349
+ }
11350
+ } catch (e25) {
11351
+ }
11352
+ }
11353
+ return {
11354
+ ...serialized,
11355
+ ...extractGetterValues(output)
11356
+ };
11357
+ }
11358
+ function isSerializableOutputValue(value) {
11359
+ if (typeof value === "function") {
11360
+ return false;
11361
+ }
11362
+ if (value && typeof value === "object" && typeof value.then === "function") {
11363
+ return false;
11364
+ }
11365
+ if (value && typeof value === "object" && typeof value.getReader === "function") {
11366
+ return false;
11367
+ }
11368
+ if (value && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function") {
11369
+ return false;
11370
+ }
11371
+ return true;
11372
+ }
10662
11373
  function serializeModelWithProvider(model) {
10663
- const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _150 => _150.modelId]);
10664
- const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _151 => _151.provider]) : void 0;
11374
+ const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _172 => _172.modelId]);
11375
+ const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _173 => _173.provider]) : void 0;
10665
11376
  if (!modelId) {
10666
11377
  return { model: modelId, provider: explicitProvider };
10667
11378
  }
@@ -10684,13 +11395,32 @@ function parseGatewayModelString(modelString) {
10684
11395
  }
10685
11396
  return { model: modelString };
10686
11397
  }
11398
+ function extractGatewayRoutingInfo(result) {
11399
+ if (_optionalChain([result, 'optionalAccess', _174 => _174.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
11400
+ const routing2 = _optionalChain([result, 'access', _175 => _175.steps, 'access', _176 => _176[0], 'optionalAccess', _177 => _177.providerMetadata, 'optionalAccess', _178 => _178.gateway, 'optionalAccess', _179 => _179.routing]);
11401
+ if (routing2) {
11402
+ return {
11403
+ provider: routing2.resolvedProvider || routing2.finalProvider,
11404
+ model: routing2.resolvedProviderApiModelId
11405
+ };
11406
+ }
11407
+ }
11408
+ const routing = _optionalChain([result, 'optionalAccess', _180 => _180.providerMetadata, 'optionalAccess', _181 => _181.gateway, 'optionalAccess', _182 => _182.routing]);
11409
+ if (routing) {
11410
+ return {
11411
+ provider: routing.resolvedProvider || routing.finalProvider,
11412
+ model: routing.resolvedProviderApiModelId
11413
+ };
11414
+ }
11415
+ return null;
11416
+ }
10687
11417
  function extractCostFromResult(result) {
10688
- if (_optionalChain([result, 'optionalAccess', _152 => _152.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
11418
+ if (_optionalChain([result, 'optionalAccess', _183 => _183.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
10689
11419
  let totalCost = 0;
10690
11420
  let foundCost = false;
10691
11421
  for (const step of result.steps) {
10692
- const gateway2 = _optionalChain([step, 'optionalAccess', _153 => _153.providerMetadata, 'optionalAccess', _154 => _154.gateway]);
10693
- const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _155 => _155.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _156 => _156.marketCost]));
11422
+ const gateway2 = _optionalChain([step, 'optionalAccess', _184 => _184.providerMetadata, 'optionalAccess', _185 => _185.gateway]);
11423
+ const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _186 => _186.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _187 => _187.marketCost]));
10694
11424
  if (stepCost !== void 0 && stepCost > 0) {
10695
11425
  totalCost += stepCost;
10696
11426
  foundCost = true;
@@ -10700,8 +11430,8 @@ function extractCostFromResult(result) {
10700
11430
  return totalCost;
10701
11431
  }
10702
11432
  }
10703
- const gateway = _optionalChain([result, 'optionalAccess', _157 => _157.providerMetadata, 'optionalAccess', _158 => _158.gateway]);
10704
- const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _159 => _159.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _160 => _160.marketCost]));
11433
+ const gateway = _optionalChain([result, 'optionalAccess', _188 => _188.providerMetadata, 'optionalAccess', _189 => _189.gateway]);
11434
+ const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _190 => _190.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _191 => _191.marketCost]));
10705
11435
  if (directCost !== void 0 && directCost > 0) {
10706
11436
  return directCost;
10707
11437
  }
@@ -10853,7 +11583,7 @@ function extractUsageFromMessage(message) {
10853
11583
  const metrics = {};
10854
11584
  let usage;
10855
11585
  if (message.type === "assistant") {
10856
- usage = _optionalChain([message, 'access', _161 => _161.message, 'optionalAccess', _162 => _162.usage]);
11586
+ usage = _optionalChain([message, 'access', _192 => _192.message, 'optionalAccess', _193 => _193.usage]);
10857
11587
  } else if (message.type === "result") {
10858
11588
  usage = message.usage;
10859
11589
  }
@@ -10893,14 +11623,14 @@ function buildLLMInput(prompt, conversationHistory) {
10893
11623
  async function createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, parentSpan) {
10894
11624
  if (messages.length === 0) return void 0;
10895
11625
  const lastMessage = messages[messages.length - 1];
10896
- if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _163 => _163.message, 'optionalAccess', _164 => _164.usage])) {
11626
+ if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _194 => _194.message, 'optionalAccess', _195 => _195.usage])) {
10897
11627
  return void 0;
10898
11628
  }
10899
11629
  const model = lastMessage.message.model || options.model;
10900
11630
  const usage = extractUsageFromMessage(lastMessage);
10901
11631
  const input = buildLLMInput(prompt, conversationHistory);
10902
11632
  const outputs = messages.map(
10903
- (m) => _optionalChain([m, 'access', _165 => _165.message, 'optionalAccess', _166 => _166.content]) && _optionalChain([m, 'access', _167 => _167.message, 'optionalAccess', _168 => _168.role]) ? { content: m.message.content, role: m.message.role } : void 0
11633
+ (m) => _optionalChain([m, 'access', _196 => _196.message, 'optionalAccess', _197 => _197.content]) && _optionalChain([m, 'access', _198 => _198.message, 'optionalAccess', _199 => _199.role]) ? { content: m.message.content, role: m.message.role } : void 0
10904
11634
  ).filter(
10905
11635
  (c) => c !== void 0
10906
11636
  );
@@ -10919,7 +11649,7 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
10919
11649
  metrics: usage
10920
11650
  });
10921
11651
  await span.end();
10922
- return _optionalChain([lastMessage, 'access', _169 => _169.message, 'optionalAccess', _170 => _170.content]) && _optionalChain([lastMessage, 'access', _171 => _171.message, 'optionalAccess', _172 => _172.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
11652
+ return _optionalChain([lastMessage, 'access', _200 => _200.message, 'optionalAccess', _201 => _201.content]) && _optionalChain([lastMessage, 'access', _202 => _202.message, 'optionalAccess', _203 => _203.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
10923
11653
  }
10924
11654
  var ClaudeAgentSDKPlugin = class extends BasePlugin {
10925
11655
  onEnable() {
@@ -10942,8 +11672,8 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
10942
11672
  const handlers = {
10943
11673
  start: (event) => {
10944
11674
  const params = event.arguments[0];
10945
- const prompt = _optionalChain([params, 'optionalAccess', _173 => _173.prompt]);
10946
- const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _174 => _174.options]), () => ( {}));
11675
+ const prompt = _optionalChain([params, 'optionalAccess', _204 => _204.prompt]);
11676
+ const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _205 => _205.options]), () => ( {}));
10947
11677
  const span = startSpan({
10948
11678
  name: "Claude Agent",
10949
11679
  spanAttributes: {
@@ -10988,9 +11718,9 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
10988
11718
  onChunk: async (message) => {
10989
11719
  const currentTime = getCurrentUnixTimestamp();
10990
11720
  const params = event.arguments[0];
10991
- const prompt = _optionalChain([params, 'optionalAccess', _175 => _175.prompt]);
10992
- const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _176 => _176.options]), () => ( {}));
10993
- const messageId = _optionalChain([message, 'access', _177 => _177.message, 'optionalAccess', _178 => _178.id]);
11721
+ const prompt = _optionalChain([params, 'optionalAccess', _206 => _206.prompt]);
11722
+ const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _207 => _207.options]), () => ( {}));
11723
+ const messageId = _optionalChain([message, 'access', _208 => _208.message, 'optionalAccess', _209 => _209.id]);
10994
11724
  if (messageId && messageId !== spanData.currentMessageId) {
10995
11725
  if (spanData.currentMessages.length > 0) {
10996
11726
  const finalMessage = await createLLMSpanForMessages(
@@ -11005,7 +11735,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
11005
11735
  spanData.conversationHistory.push(finalMessage);
11006
11736
  }
11007
11737
  const lastMessage = spanData.currentMessages[spanData.currentMessages.length - 1];
11008
- if (_optionalChain([lastMessage, 'optionalAccess', _179 => _179.message, 'optionalAccess', _180 => _180.usage])) {
11738
+ if (_optionalChain([lastMessage, 'optionalAccess', _210 => _210.message, 'optionalAccess', _211 => _211.usage])) {
11009
11739
  const outputTokens = getNumberProperty(
11010
11740
  lastMessage.message.usage,
11011
11741
  "output_tokens"
@@ -11017,14 +11747,14 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
11017
11747
  spanData.currentMessageId = messageId;
11018
11748
  spanData.currentMessageStartTime = currentTime;
11019
11749
  }
11020
- if (message.type === "assistant" && _optionalChain([message, 'access', _181 => _181.message, 'optionalAccess', _182 => _182.usage])) {
11750
+ if (message.type === "assistant" && _optionalChain([message, 'access', _212 => _212.message, 'optionalAccess', _213 => _213.usage])) {
11021
11751
  spanData.currentMessages.push(message);
11022
11752
  }
11023
11753
  if (message.type === "result" && message.usage) {
11024
11754
  const finalUsageMetrics = extractUsageFromMessage(message);
11025
11755
  if (spanData.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
11026
11756
  const lastMessage = spanData.currentMessages[spanData.currentMessages.length - 1];
11027
- if (_optionalChain([lastMessage, 'optionalAccess', _183 => _183.message, 'optionalAccess', _184 => _184.usage])) {
11757
+ if (_optionalChain([lastMessage, 'optionalAccess', _214 => _214.message, 'optionalAccess', _215 => _215.usage])) {
11028
11758
  const adjustedTokens = finalUsageMetrics.completion_tokens - spanData.accumulatedOutputTokens;
11029
11759
  if (adjustedTokens >= 0) {
11030
11760
  lastMessage.message.usage.output_tokens = adjustedTokens;
@@ -11048,8 +11778,8 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
11048
11778
  onComplete: async () => {
11049
11779
  try {
11050
11780
  const params = event.arguments[0];
11051
- const prompt = _optionalChain([params, 'optionalAccess', _185 => _185.prompt]);
11052
- const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _186 => _186.options]), () => ( {}));
11781
+ const prompt = _optionalChain([params, 'optionalAccess', _216 => _216.prompt]);
11782
+ const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _217 => _217.options]), () => ( {}));
11053
11783
  if (spanData.currentMessages.length > 0) {
11054
11784
  const finalMessage = await createLLMSpanForMessages(
11055
11785
  spanData.currentMessages,
@@ -11256,7 +11986,7 @@ function serializePart(part) {
11256
11986
  return part;
11257
11987
  }
11258
11988
  function serializeTools(params) {
11259
- if (!_optionalChain([params, 'access', _187 => _187.config, 'optionalAccess', _188 => _188.tools])) {
11989
+ if (!_optionalChain([params, 'access', _218 => _218.config, 'optionalAccess', _219 => _219.tools])) {
11260
11990
  return null;
11261
11991
  }
11262
11992
  try {
@@ -11266,7 +11996,7 @@ function serializeTools(params) {
11266
11996
  }
11267
11997
  return tool;
11268
11998
  });
11269
- } catch (e21) {
11999
+ } catch (e26) {
11270
12000
  return null;
11271
12001
  }
11272
12002
  }
@@ -11293,7 +12023,7 @@ function extractGenerateContentMetrics(response, startTime) {
11293
12023
  const end = getCurrentUnixTimestamp();
11294
12024
  metrics.duration = end - startTime;
11295
12025
  }
11296
- if (_optionalChain([response, 'optionalAccess', _189 => _189.usageMetadata])) {
12026
+ if (_optionalChain([response, 'optionalAccess', _220 => _220.usageMetadata])) {
11297
12027
  populateUsageMetrics(metrics, response.usageMetadata);
11298
12028
  }
11299
12029
  return metrics;
@@ -11341,7 +12071,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
11341
12071
  }
11342
12072
  if (chunk.candidates && Array.isArray(chunk.candidates)) {
11343
12073
  for (const candidate of chunk.candidates) {
11344
- if (_optionalChain([candidate, 'access', _190 => _190.content, 'optionalAccess', _191 => _191.parts])) {
12074
+ if (_optionalChain([candidate, 'access', _221 => _221.content, 'optionalAccess', _222 => _222.parts])) {
11345
12075
  for (const part of candidate.content.parts) {
11346
12076
  if (part.text !== void 0) {
11347
12077
  if (part.thought) {
@@ -11372,7 +12102,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
11372
12102
  parts.push({ text });
11373
12103
  }
11374
12104
  parts.push(...otherParts);
11375
- if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _192 => _192.candidates])) {
12105
+ if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _223 => _223.candidates])) {
11376
12106
  const candidates = [];
11377
12107
  for (const candidate of lastResponse.candidates) {
11378
12108
  const candidateDict = {
@@ -11604,7 +12334,7 @@ function configureNode() {
11604
12334
  var _express = require('express'); var _express2 = _interopRequireDefault(_express);
11605
12335
  var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors);
11606
12336
 
11607
- // ../node_modules/async/dist/async.mjs
12337
+ // ../node_modules/.pnpm/async@3.2.5/node_modules/async/dist/async.mjs
11608
12338
  function initialParams(fn) {
11609
12339
  return function(...args) {
11610
12340
  var callback = args.pop();
@@ -11672,7 +12402,7 @@ function invokeCallback(callback, error, value) {
11672
12402
  function isAsync(fn) {
11673
12403
  return fn[Symbol.toStringTag] === "AsyncFunction";
11674
12404
  }
11675
- function isAsyncGenerator(fn) {
12405
+ function isAsyncGenerator2(fn) {
11676
12406
  return fn[Symbol.toStringTag] === "AsyncGenerator";
11677
12407
  }
11678
12408
  function isAsyncIterable2(obj) {
@@ -11729,6 +12459,7 @@ function isArrayLike(value) {
11729
12459
  return value && typeof value.length === "number" && value.length >= 0 && value.length % 1 === 0;
11730
12460
  }
11731
12461
  var breakLoop = {};
12462
+ var breakLoop$1 = breakLoop;
11732
12463
  function once(fn) {
11733
12464
  function wrapper(...args) {
11734
12465
  if (fn === null) return;
@@ -11820,7 +12551,7 @@ function asyncEachOfLimit(generator, limit, iteratee, callback) {
11820
12551
  canceled = true;
11821
12552
  return;
11822
12553
  }
11823
- if (result === breakLoop || done && running <= 0) {
12554
+ if (result === breakLoop$1 || done && running <= 0) {
11824
12555
  done = true;
11825
12556
  return callback(null);
11826
12557
  }
@@ -11843,7 +12574,7 @@ var eachOfLimit$2 = (limit) => {
11843
12574
  if (!obj) {
11844
12575
  return callback(null);
11845
12576
  }
11846
- if (isAsyncGenerator(obj)) {
12577
+ if (isAsyncGenerator2(obj)) {
11847
12578
  return asyncEachOfLimit(obj, limit, iteratee, callback);
11848
12579
  }
11849
12580
  if (isAsyncIterable2(obj)) {
@@ -11863,7 +12594,7 @@ var eachOfLimit$2 = (limit) => {
11863
12594
  } else if (err === false) {
11864
12595
  done = true;
11865
12596
  canceled = true;
11866
- } else if (value === breakLoop || done && running <= 0) {
12597
+ } else if (value === breakLoop$1 || done && running <= 0) {
11867
12598
  done = true;
11868
12599
  return callback(null);
11869
12600
  } else if (!looping) {
@@ -11906,7 +12637,7 @@ function eachOfArrayLike(coll, iteratee, callback) {
11906
12637
  if (canceled === true) return;
11907
12638
  if (err) {
11908
12639
  callback(err);
11909
- } else if (++completed === length || value === breakLoop) {
12640
+ } else if (++completed === length || value === breakLoop$1) {
11910
12641
  callback(null);
11911
12642
  }
11912
12643
  }
@@ -12302,7 +13033,7 @@ function _createTester(check, getResult) {
12302
13033
  if (check(result) && !testResult) {
12303
13034
  testPassed = true;
12304
13035
  testResult = getResult(true, value);
12305
- return callback(null, breakLoop);
13036
+ return callback(null, breakLoop$1);
12306
13037
  }
12307
13038
  callback();
12308
13039
  });
@@ -12793,7 +13524,7 @@ var CachedSpanFetcher = (_class20 = class {
12793
13524
  spanType
12794
13525
  );
12795
13526
  const rows = await fetcher.fetchedData();
12796
- return rows.filter((row) => _optionalChain([row, 'access', _193 => _193.span_attributes, 'optionalAccess', _194 => _194.purpose]) !== "scorer").map((row) => ({
13527
+ return rows.filter((row) => _optionalChain([row, 'access', _224 => _224.span_attributes, 'optionalAccess', _225 => _225.purpose]) !== "scorer").map((row) => ({
12797
13528
  input: row.input,
12798
13529
  output: row.output,
12799
13530
  metadata: row.metadata,
@@ -12827,7 +13558,7 @@ var CachedSpanFetcher = (_class20 = class {
12827
13558
  async fetchSpans(spanType) {
12828
13559
  const spans = await this.fetchFn(spanType);
12829
13560
  for (const span of spans) {
12830
- const type = _nullishCoalesce(_optionalChain([span, 'access', _195 => _195.span_attributes, 'optionalAccess', _196 => _196.type]), () => ( ""));
13561
+ const type = _nullishCoalesce(_optionalChain([span, 'access', _226 => _226.span_attributes, 'optionalAccess', _227 => _227.type]), () => ( ""));
12831
13562
  const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
12832
13563
  existing.push(span);
12833
13564
  this.spanCache.set(type, existing);
@@ -12907,11 +13638,11 @@ var LocalTrace = (_class21 = class {
12907
13638
  const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
12908
13639
  if (cachedSpans && cachedSpans.length > 0) {
12909
13640
  let spans = cachedSpans.filter(
12910
- (span) => _optionalChain([span, 'access', _197 => _197.span_attributes, 'optionalAccess', _198 => _198.purpose]) !== "scorer"
13641
+ (span) => _optionalChain([span, 'access', _228 => _228.span_attributes, 'optionalAccess', _229 => _229.purpose]) !== "scorer"
12911
13642
  );
12912
13643
  if (spanType && spanType.length > 0) {
12913
13644
  spans = spans.filter(
12914
- (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _199 => _199.span_attributes, 'optionalAccess', _200 => _200.type]), () => ( "")))
13645
+ (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _230 => _230.span_attributes, 'optionalAccess', _231 => _231.type]), () => ( "")))
12915
13646
  );
12916
13647
  }
12917
13648
  return spans.map((span) => ({
@@ -12930,7 +13661,7 @@ var LocalTrace = (_class21 = class {
12930
13661
  * Calls the API with the project_default preprocessor (which falls back to "thread").
12931
13662
  */
12932
13663
  async getThread(options) {
12933
- const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _201 => _201.preprocessor]), () => ( "project_default"));
13664
+ const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _232 => _232.preprocessor]), () => ( "project_default"));
12934
13665
  if (!this.threadCache.has(cacheKey)) {
12935
13666
  const promise = this.fetchThread(options);
12936
13667
  this.threadCache.set(cacheKey, promise);
@@ -12941,7 +13672,7 @@ var LocalTrace = (_class21 = class {
12941
13672
  await this.ensureSpansReady();
12942
13673
  await this.state.login({});
12943
13674
  const result = await invoke({
12944
- globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _202 => _202.preprocessor]), () => ( "project_default")),
13675
+ globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _233 => _233.preprocessor]), () => ( "project_default")),
12945
13676
  functionType: "preprocessor",
12946
13677
  input: {
12947
13678
  trace_ref: {
@@ -13005,7 +13736,8 @@ var promptDefinitionSchema = promptContentsSchema.and(
13005
13736
  _v3.z.object({
13006
13737
  model: _v3.z.string(),
13007
13738
  params: ModelParams.optional(),
13008
- templateFormat: _v3.z.enum(["mustache", "nunjucks", "none"]).optional()
13739
+ templateFormat: _v3.z.enum(["mustache", "nunjucks", "none"]).optional(),
13740
+ environments: _v3.z.array(_v3.z.string()).optional()
13009
13741
  })
13010
13742
  );
13011
13743
  var promptDefinitionWithToolsSchema = promptDefinitionSchema.and(
@@ -13116,10 +13848,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
13116
13848
  const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
13117
13849
  const validate = ajv.compile(schema);
13118
13850
  if (!validate(parameters)) {
13119
- const errorMessages = _optionalChain([validate, 'access', _203 => _203.errors, 'optionalAccess', _204 => _204.map, 'call', _205 => _205((err) => {
13851
+ const errorMessages = _optionalChain([validate, 'access', _234 => _234.errors, 'optionalAccess', _235 => _235.map, 'call', _236 => _236((err) => {
13120
13852
  const path2 = err.instancePath || "root";
13121
13853
  return `${path2}: ${err.message}`;
13122
- }), 'access', _206 => _206.join, 'call', _207 => _207(", ")]);
13854
+ }), 'access', _237 => _237.join, 'call', _238 => _238(", ")]);
13123
13855
  throw Error(`Invalid parameters: ${errorMessages}`);
13124
13856
  }
13125
13857
  return parameters;
@@ -13161,6 +13893,22 @@ function initExperiment(state, options = {}) {
13161
13893
  setCurrent: false
13162
13894
  });
13163
13895
  }
13896
+ async function getExperimentParametersRef(parameters) {
13897
+ if (!parameters) {
13898
+ return void 0;
13899
+ }
13900
+ const resolvedParameters = parameters instanceof Promise ? await parameters : parameters;
13901
+ if (!RemoteEvalParameters.isParameters(resolvedParameters)) {
13902
+ return void 0;
13903
+ }
13904
+ if (resolvedParameters.id === void 0) {
13905
+ return void 0;
13906
+ }
13907
+ return {
13908
+ id: resolvedParameters.id,
13909
+ version: resolvedParameters.version
13910
+ };
13911
+ }
13164
13912
  function callEvaluatorData(data) {
13165
13913
  const dataResult = typeof data === "function" ? data() : data;
13166
13914
  let baseExperiment = void 0;
@@ -13227,6 +13975,7 @@ async function Eval(name, evaluator, reporterOrOpts) {
13227
13975
  const { data, baseExperiment: defaultBaseExperiment } = callEvaluatorData(
13228
13976
  evaluator.data
13229
13977
  );
13978
+ const parameters = await getExperimentParametersRef(evaluator.parameters);
13230
13979
  const experiment = options.parent || options.noSendLogs ? null : initExperiment(evaluator.state, {
13231
13980
  ...evaluator.projectId ? { projectId: evaluator.projectId } : { project: name },
13232
13981
  experiment: evaluator.experimentName,
@@ -13239,7 +13988,8 @@ async function Eval(name, evaluator, reporterOrOpts) {
13239
13988
  baseExperimentId: evaluator.baseExperimentId,
13240
13989
  gitMetadataSettings: evaluator.gitMetadataSettings,
13241
13990
  repoInfo: evaluator.repoInfo,
13242
- dataset: Dataset2.isDataset(data) ? data : void 0
13991
+ dataset: Dataset2.isDataset(data) ? data : void 0,
13992
+ parameters
13243
13993
  });
13244
13994
  if (experiment && typeof process !== "undefined" && globalThis.BRAINTRUST_CONTEXT_MANAGER !== void 0) {
13245
13995
  await experiment._waitForId();
@@ -13294,7 +14044,7 @@ async function Eval(name, evaluator, reporterOrOpts) {
13294
14044
  if (experiment) {
13295
14045
  await experiment.flush().catch(console.error);
13296
14046
  } else if (options.parent) {
13297
- await flush().catch(console.error);
14047
+ await flush({ state: evaluator.state }).catch(console.error);
13298
14048
  }
13299
14049
  }
13300
14050
  } finally {
@@ -13339,7 +14089,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
13339
14089
  }
13340
14090
  async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
13341
14091
  if (enableCache) {
13342
- _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _208 => _208.spanCache, 'optionalAccess', _209 => _209.start, 'call', _210 => _210()]);
14092
+ _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _239 => _239.spanCache, 'optionalAccess', _240 => _240.start, 'call', _241 => _241()]);
13343
14093
  }
13344
14094
  try {
13345
14095
  if (typeof evaluator.data === "string") {
@@ -13394,7 +14144,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13394
14144
  const experimentIdPromise = experiment ? (async () => {
13395
14145
  try {
13396
14146
  return await experiment.id;
13397
- } catch (e22) {
14147
+ } catch (e27) {
13398
14148
  return void 0;
13399
14149
  }
13400
14150
  })() : void 0;
@@ -13450,7 +14200,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13450
14200
  objectType: parentComponents ? spanObjectTypeV3ToTypedString(
13451
14201
  parentComponents.data.object_type
13452
14202
  ) : "experiment",
13453
- objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _211 => _211.data, 'access', async _212 => _212.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
14203
+ objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _242 => _242.data, 'access', async _243 => _243.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
13454
14204
  rootSpanId: rootSpan.rootSpanId,
13455
14205
  ensureSpansFlushed,
13456
14206
  state
@@ -13476,10 +14226,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13476
14226
  span,
13477
14227
  parameters: _nullishCoalesce(parameters, () => ( {})),
13478
14228
  reportProgress: (event) => {
13479
- _optionalChain([stream, 'optionalCall', _213 => _213({
14229
+ _optionalChain([stream, 'optionalCall', _244 => _244({
13480
14230
  ...event,
13481
14231
  id: rootSpan.id,
13482
- origin: _optionalChain([baseEvent, 'access', _214 => _214.event, 'optionalAccess', _215 => _215.origin]),
14232
+ origin: _optionalChain([baseEvent, 'access', _245 => _245.event, 'optionalAccess', _246 => _246.origin]),
13483
14233
  name: evaluator.evalName,
13484
14234
  object_type: "task"
13485
14235
  })]);
@@ -13643,7 +14393,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13643
14393
  metadata,
13644
14394
  scores: mergedScores,
13645
14395
  error,
13646
- origin: _optionalChain([baseEvent, 'access', _216 => _216.event, 'optionalAccess', _217 => _217.origin])
14396
+ origin: _optionalChain([baseEvent, 'access', _247 => _247.event, 'optionalAccess', _248 => _248.origin])
13647
14397
  });
13648
14398
  }
13649
14399
  };
@@ -13663,6 +14413,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13663
14413
  },
13664
14414
  Math.max(_nullishCoalesce(evaluator.maxConcurrency, () => ( Number.MAX_SAFE_INTEGER)), 1)
13665
14415
  );
14416
+ const queueErrors = [];
13666
14417
  const enqueuePromise = (async () => {
13667
14418
  for await (const datum of dataIterable) {
13668
14419
  if (cancelled) {
@@ -13677,8 +14428,12 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13677
14428
  break;
13678
14429
  }
13679
14430
  scheduledTrials++;
13680
- _optionalChain([progressReporter, 'access', _218 => _218.setTotal, 'optionalCall', _219 => _219(evaluator.evalName, scheduledTrials)]);
13681
- q.push({ datum, trialIndex });
14431
+ _optionalChain([progressReporter, 'access', _249 => _249.setTotal, 'optionalCall', _250 => _250(evaluator.evalName, scheduledTrials)]);
14432
+ q.pushAsync({ datum, trialIndex }).catch((e) => {
14433
+ if (queueErrors.length < 5) {
14434
+ queueErrors.push(e);
14435
+ }
14436
+ });
13682
14437
  }
13683
14438
  }
13684
14439
  })();
@@ -13726,6 +14481,12 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13726
14481
  })();
13727
14482
  try {
13728
14483
  await Promise.race([waitForQueue, cancel()]);
14484
+ if (queueErrors.length > 0) {
14485
+ throw new AggregateError(
14486
+ queueErrors,
14487
+ `Encountered ${queueErrors.length} unhandled task errors`
14488
+ );
14489
+ }
13729
14490
  } catch (e) {
13730
14491
  q.kill();
13731
14492
  if (e instanceof InternalAbortError) {
@@ -13752,9 +14513,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13752
14513
  );
13753
14514
  } finally {
13754
14515
  if (enableCache) {
13755
- const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _220 => _220.spanCache]);
13756
- _optionalChain([spanCache, 'optionalAccess', _221 => _221.dispose, 'call', _222 => _222()]);
13757
- _optionalChain([spanCache, 'optionalAccess', _223 => _223.stop, 'call', _224 => _224()]);
14516
+ const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _251 => _251.spanCache]);
14517
+ _optionalChain([spanCache, 'optionalAccess', _252 => _252.dispose, 'call', _253 => _253()]);
14518
+ _optionalChain([spanCache, 'optionalAccess', _254 => _254.stop, 'call', _255 => _255()]);
13758
14519
  }
13759
14520
  }
13760
14521
  }
@@ -13991,7 +14752,7 @@ async function cachedLogin(options) {
13991
14752
  }
13992
14753
  function makeCheckAuthorized(allowedOrgName) {
13993
14754
  return async (req, _res, next) => {
13994
- if (!_optionalChain([req, 'access', _225 => _225.ctx, 'optionalAccess', _226 => _226.token])) {
14755
+ if (!_optionalChain([req, 'access', _256 => _256.ctx, 'optionalAccess', _257 => _257.token])) {
13995
14756
  return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
13996
14757
  }
13997
14758
  try {
@@ -14004,7 +14765,7 @@ function makeCheckAuthorized(allowedOrgName) {
14004
14765
  return next(_httperrors2.default.call(void 0, 403, errorMessage));
14005
14766
  }
14006
14767
  const state = await cachedLogin({
14007
- apiKey: _optionalChain([req, 'access', _227 => _227.ctx, 'optionalAccess', _228 => _228.token]),
14768
+ apiKey: _optionalChain([req, 'access', _258 => _258.ctx, 'optionalAccess', _259 => _259.token]),
14008
14769
  orgName
14009
14770
  });
14010
14771
  req.ctx.state = state;
@@ -14187,23 +14948,6 @@ var evaluatorDefinitionsSchema = _v3.z.record(
14187
14948
 
14188
14949
  // src/framework2.ts
14189
14950
 
14190
-
14191
- // src/zod/utils.ts
14192
- var _zodtojsonschema = require('zod-to-json-schema');
14193
- var _v4 = require('zod/v4'); var z42 = _interopRequireWildcard(_v4);
14194
- function isZodV4(zodObject) {
14195
- return typeof zodObject === "object" && zodObject !== null && "_zod" in zodObject && zodObject._zod !== void 0;
14196
- }
14197
- function zodToJsonSchema(schema) {
14198
- if (isZodV4(schema)) {
14199
- return z42.toJSONSchema(schema, {
14200
- target: "draft-7"
14201
- });
14202
- }
14203
- return _zodtojsonschema.zodToJsonSchema.call(void 0, schema);
14204
- }
14205
-
14206
- // src/framework2.ts
14207
14951
  var currentFilename = typeof __filename !== "undefined" ? __filename : "unknown";
14208
14952
  var ProjectBuilder = class {
14209
14953
  create(opts) {
@@ -14409,6 +15153,7 @@ var CodePrompt = class {
14409
15153
 
14410
15154
 
14411
15155
 
15156
+
14412
15157
  constructor(project, prompt, toolFunctions, opts, functionType) {
14413
15158
  this.project = project;
14414
15159
  this.name = opts.name;
@@ -14421,6 +15166,7 @@ var CodePrompt = class {
14421
15166
  this.functionType = functionType;
14422
15167
  this.tags = opts.tags;
14423
15168
  this.metadata = opts.metadata;
15169
+ this.environmentSlugs = opts.environments;
14424
15170
  }
14425
15171
  async toFunctionDefinition(projectNameToId) {
14426
15172
  const prompt_data = {
@@ -14455,7 +15201,8 @@ var CodePrompt = class {
14455
15201
  prompt_data,
14456
15202
  if_exists: this.ifExists,
14457
15203
  tags: this.tags,
14458
- metadata: this.metadata
15204
+ metadata: this.metadata,
15205
+ environments: this.environmentSlugs && this.environmentSlugs.length > 0 ? this.environmentSlugs.map((slug) => ({ slug })) : void 0
14459
15206
  };
14460
15207
  }
14461
15208
  };
@@ -14767,7 +15514,7 @@ function runDevServer(evaluators, opts) {
14767
15514
  scores,
14768
15515
  stream
14769
15516
  } = evalBodySchema.parse(req.body);
14770
- if (!_optionalChain([req, 'access', _229 => _229.ctx, 'optionalAccess', _230 => _230.state])) {
15517
+ if (!_optionalChain([req, 'access', _260 => _260.ctx, 'optionalAccess', _261 => _261.state])) {
14771
15518
  res.status(500).json({ error: "Braintrust state not initialized in request" });
14772
15519
  return;
14773
15520
  }
@@ -14818,12 +15565,12 @@ function runDevServer(evaluators, opts) {
14818
15565
  ...evaluator,
14819
15566
  data: evalData.data,
14820
15567
  scores: evaluator.scores.concat(
14821
- _nullishCoalesce(_optionalChain([scores, 'optionalAccess', _231 => _231.map, 'call', _232 => _232(
15568
+ _nullishCoalesce(_optionalChain([scores, 'optionalAccess', _262 => _262.map, 'call', _263 => _263(
14822
15569
  (score) => makeScorer(
14823
15570
  state,
14824
15571
  score.name,
14825
15572
  score.function_id,
14826
- _optionalChain([req, 'access', _233 => _233.ctx, 'optionalAccess', _234 => _234.projectId])
15573
+ _optionalChain([req, 'access', _264 => _264.ctx, 'optionalAccess', _265 => _265.projectId])
14827
15574
  )
14828
15575
  )]), () => ( []))
14829
15576
  ),