braintrust 3.3.0 → 3.4.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 (44) hide show
  1. package/README.md +52 -67
  2. package/dev/dist/index.d.mts +53 -9
  3. package/dev/dist/index.d.ts +53 -9
  4. package/dev/dist/index.js +1839 -1298
  5. package/dev/dist/index.mjs +1503 -962
  6. package/dist/auto-instrumentations/bundler/esbuild.cjs +270 -23
  7. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  8. package/dist/auto-instrumentations/bundler/rollup.cjs +270 -23
  9. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  10. package/dist/auto-instrumentations/bundler/vite.cjs +270 -23
  11. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  12. package/dist/auto-instrumentations/bundler/webpack.cjs +270 -23
  13. package/dist/auto-instrumentations/bundler/webpack.mjs +2 -2
  14. package/dist/auto-instrumentations/{chunk-OLOPGWTJ.mjs → chunk-D5ZPIUEL.mjs} +1 -1
  15. package/dist/auto-instrumentations/chunk-LVWWLUMN.mjs +535 -0
  16. package/dist/auto-instrumentations/hook.mjs +306 -23
  17. package/dist/auto-instrumentations/index.cjs +270 -23
  18. package/dist/auto-instrumentations/index.d.mts +5 -5
  19. package/dist/auto-instrumentations/index.d.ts +5 -5
  20. package/dist/auto-instrumentations/index.mjs +1 -1
  21. package/dist/auto-instrumentations/loader/esm-hook.mjs +7 -8
  22. package/dist/browser.d.mts +474 -47
  23. package/dist/browser.d.ts +474 -47
  24. package/dist/browser.js +2258 -2095
  25. package/dist/browser.mjs +2258 -2095
  26. package/dist/cli.js +1817 -1232
  27. package/dist/edge-light.d.mts +1 -1
  28. package/dist/edge-light.d.ts +1 -1
  29. package/dist/edge-light.js +2188 -2027
  30. package/dist/edge-light.mjs +2188 -2027
  31. package/dist/index.d.mts +474 -47
  32. package/dist/index.d.ts +474 -47
  33. package/dist/index.js +2576 -2415
  34. package/dist/index.mjs +2259 -2098
  35. package/dist/instrumentation/index.d.mts +16 -22
  36. package/dist/instrumentation/index.d.ts +16 -22
  37. package/dist/instrumentation/index.js +1558 -1068
  38. package/dist/instrumentation/index.mjs +1558 -1068
  39. package/dist/workerd.d.mts +1 -1
  40. package/dist/workerd.d.ts +1 -1
  41. package/dist/workerd.js +2188 -2027
  42. package/dist/workerd.mjs +2188 -2027
  43. package/package.json +6 -3
  44. package/dist/auto-instrumentations/chunk-KVX7OFPD.mjs +0 -288
package/dev/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27; var _class28;// src/node/config.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25;// src/node/config.ts
2
2
  var _nodeasync_hooks = require('node:async_hooks');
3
+ var _nodediagnostics_channel = require('node:diagnostics_channel'); var diagnostics_channel = _interopRequireWildcard(_nodediagnostics_channel);
3
4
  var _nodepath = require('node:path'); var path = _interopRequireWildcard(_nodepath);
4
5
  var _promises = require('node:fs/promises'); var fs = _interopRequireWildcard(_promises);
5
6
  var _nodeos = require('node:os'); var os = _interopRequireWildcard(_nodeos);
@@ -21,6 +22,26 @@ var DefaultAsyncLocalStorage = class {
21
22
  return void 0;
22
23
  }
23
24
  };
25
+ var DefaultTracingChannel = (_class = class {constructor() { _class.prototype.__init.call(this); }
26
+ __init() {this.hasSubscribers = false}
27
+ subscribe(_handlers) {
28
+ }
29
+ unsubscribe(_handlers) {
30
+ return false;
31
+ }
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ traceSync(fn, _message, thisArg, ...args) {
34
+ return fn.apply(thisArg, args);
35
+ }
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ tracePromise(fn, _message, thisArg, ...args) {
38
+ return Promise.resolve(fn.apply(thisArg, args));
39
+ }
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ traceCallback(fn, _position, _message, thisArg, ...args) {
42
+ return fn.apply(thisArg, args);
43
+ }
44
+ }, _class);
24
45
  var iso = {
25
46
  buildType: "unknown",
26
47
  // Will be set by configureBrowser() or configureNode()
@@ -29,6 +50,8 @@ var iso = {
29
50
  getEnv: (_name) => void 0,
30
51
  getCallerLocation: () => void 0,
31
52
  newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
53
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
+ newTracingChannel: (_nameOrChannels) => new DefaultTracingChannel(),
32
55
  processOn: (_0, _1) => {
33
56
  },
34
57
  basename: (filepath) => filepath.split(/[\\/]/).pop() || filepath,
@@ -36,6 +59,111 @@ var iso = {
36
59
  };
37
60
  var isomorph_default = iso;
38
61
 
62
+ // src/debug-logger.ts
63
+ var PREFIX = "[braintrust]";
64
+ var DEBUG_LOG_LEVEL_SYMBOL = Symbol.for("braintrust-debug-log-level");
65
+ var LOG_LEVEL_PRIORITY = {
66
+ error: 0,
67
+ warn: 1,
68
+ info: 2,
69
+ debug: 3
70
+ };
71
+ var hasWarnedAboutInvalidEnvValue = false;
72
+ var debugLogStateResolver = void 0;
73
+ function warnInvalidEnvValue(value) {
74
+ if (hasWarnedAboutInvalidEnvValue) {
75
+ return;
76
+ }
77
+ hasWarnedAboutInvalidEnvValue = true;
78
+ console.warn(
79
+ PREFIX,
80
+ `Invalid BRAINTRUST_DEBUG_LOG_LEVEL value "${value}". Expected "error", "warn", "info", or "debug".`
81
+ );
82
+ }
83
+ function normalizeDebugLogLevelOption(option) {
84
+ if (option === false) {
85
+ return void 0;
86
+ }
87
+ if (option === "error" || option === "warn" || option === "info" || option === "debug") {
88
+ return option;
89
+ }
90
+ throw new Error(
91
+ `Invalid debugLogLevel value "${option}". Expected false, "error", "warn", "info", or "debug".`
92
+ );
93
+ }
94
+ function parseDebugLogLevelEnv(value) {
95
+ if (!value) {
96
+ return void 0;
97
+ }
98
+ if (value === "error" || value === "warn" || value === "info" || value === "debug") {
99
+ return value;
100
+ }
101
+ warnInvalidEnvValue(value);
102
+ return void 0;
103
+ }
104
+ function getEnvDebugLogLevel() {
105
+ return parseDebugLogLevelEnv(isomorph_default.getEnv("BRAINTRUST_DEBUG_LOG_LEVEL"));
106
+ }
107
+ function setGlobalDebugLogLevel(level) {
108
+ globalThis[DEBUG_LOG_LEVEL_SYMBOL] = level;
109
+ }
110
+ function setDebugLogStateResolver(resolver) {
111
+ debugLogStateResolver = resolver;
112
+ }
113
+ function resolveDebugLogLevel(state) {
114
+ const stateLevel = _optionalChain([state, 'optionalAccess', _2 => _2.getDebugLogLevel, 'optionalCall', _3 => _3()]);
115
+ const hasStateOverride = _nullishCoalesce(_optionalChain([state, 'optionalAccess', _4 => _4.hasDebugLogLevelOverride, 'optionalCall', _5 => _5()]), () => ( false));
116
+ if (hasStateOverride) {
117
+ return stateLevel;
118
+ }
119
+ const globalLevel = (
120
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
121
+ globalThis[DEBUG_LOG_LEVEL_SYMBOL]
122
+ );
123
+ if (globalLevel !== void 0) {
124
+ return globalLevel === false ? void 0 : globalLevel;
125
+ }
126
+ return getEnvDebugLogLevel();
127
+ }
128
+ function emit(method, state, args) {
129
+ const level = resolveDebugLogLevel(state);
130
+ if (!level || LOG_LEVEL_PRIORITY[method] > LOG_LEVEL_PRIORITY[level]) {
131
+ return;
132
+ }
133
+ if (method === "info") {
134
+ console.log(PREFIX, ...args);
135
+ } else if (method === "debug") {
136
+ console.debug(PREFIX, ...args);
137
+ } else if (method === "warn") {
138
+ console.warn(PREFIX, ...args);
139
+ } else {
140
+ console.error(PREFIX, ...args);
141
+ }
142
+ }
143
+ function createDebugLogger(state) {
144
+ const resolveState = () => _nullishCoalesce(state, () => ( _optionalChain([debugLogStateResolver, 'optionalCall', _6 => _6()])));
145
+ return {
146
+ info(...args) {
147
+ emit("info", resolveState(), args);
148
+ },
149
+ debug(...args) {
150
+ emit("debug", resolveState(), args);
151
+ },
152
+ warn(...args) {
153
+ emit("warn", resolveState(), args);
154
+ },
155
+ error(...args) {
156
+ emit("error", resolveState(), args);
157
+ }
158
+ };
159
+ }
160
+ var debugLogger = {
161
+ ...createDebugLogger(),
162
+ forState(state) {
163
+ return createDebugLogger(state);
164
+ }
165
+ };
166
+
39
167
  // src/gitutil.ts
40
168
  var _simplegit = require('simple-git');
41
169
  var COMMON_BASE_BRANCHES = ["main", "master", "develop"];
@@ -58,7 +186,7 @@ async function getBaseBranch(remote = void 0) {
58
186
  if (git === null) {
59
187
  throw new Error("Not in a git repo");
60
188
  }
61
- const remoteName = await _asyncNullishCoalesce(remote, async () => ( await _asyncOptionalChain([(await git.getRemotes()), 'access', async _2 => _2[0], 'optionalAccess', async _3 => _3.name])));
189
+ const remoteName = await _asyncNullishCoalesce(remote, async () => ( await _asyncOptionalChain([(await git.getRemotes()), 'access', async _7 => _7[0], 'optionalAccess', async _8 => _8.name])));
62
190
  if (!remoteName) {
63
191
  throw new Error("No remote found");
64
192
  }
@@ -116,7 +244,7 @@ async function getPastNAncestors(n = 1e3, remote = void 0) {
116
244
  try {
117
245
  ancestor = await getBaseBranchAncestor(remote);
118
246
  } catch (e) {
119
- console.warn(
247
+ debugLogger.warn(
120
248
  "Skipping git metadata. This is likely because the repository has not been published to a remote yet.",
121
249
  `${e}`
122
250
  );
@@ -151,7 +279,7 @@ async function getRepoInfo(settings) {
151
279
  return repo;
152
280
  }
153
281
  let sanitized = {};
154
- _optionalChain([settings, 'access', _4 => _4.fields, 'optionalAccess', _5 => _5.forEach, 'call', _6 => _6((field) => {
282
+ _optionalChain([settings, 'access', _9 => _9.fields, 'optionalAccess', _10 => _10.forEach, 'call', _11 => _11((field) => {
155
283
  sanitized = { ...sanitized, [field]: repo[field] };
156
284
  })]);
157
285
  return sanitized;
@@ -237,9 +365,9 @@ function getCallerLocation() {
237
365
  const entries = getStackTrace();
238
366
  for (const frame of entries) {
239
367
  if (thisDir === void 0) {
240
- thisDir = _optionalChain([isomorph_default, 'access', _7 => _7.pathDirname, 'optionalCall', _8 => _8(frame.fileName)]);
368
+ thisDir = _optionalChain([isomorph_default, 'access', _12 => _12.pathDirname, 'optionalCall', _13 => _13(frame.fileName)]);
241
369
  }
242
- if (_optionalChain([isomorph_default, 'access', _9 => _9.pathDirname, 'optionalCall', _10 => _10(frame.fileName)]) !== thisDir) {
370
+ if (_optionalChain([isomorph_default, 'access', _14 => _14.pathDirname, 'optionalCall', _15 => _15(frame.fileName)]) !== thisDir) {
243
371
  return {
244
372
  caller_functionname: frame.functionName,
245
373
  caller_filename: frame.fileName,
@@ -255,13 +383,13 @@ var _uuid = require('uuid'); var uuid3 = _interopRequireWildcard(_uuid); var uui
255
383
 
256
384
  // src/queue.ts
257
385
  var DEFAULT_QUEUE_SIZE = 15e3;
258
- var Queue = (_class = class {
259
- __init() {this.items = []}
386
+ var Queue = (_class2 = class {
387
+ __init2() {this.items = []}
260
388
 
261
- __init2() {this.enforceSizeLimit = false}
262
- constructor(maxSize) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);
389
+ __init3() {this.enforceSizeLimit = false}
390
+ constructor(maxSize) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);
263
391
  if (maxSize < 1) {
264
- console.warn(
392
+ debugLogger.warn(
265
393
  `maxSize ${maxSize} is <1, using default ${DEFAULT_QUEUE_SIZE}`
266
394
  );
267
395
  maxSize = DEFAULT_QUEUE_SIZE;
@@ -307,7 +435,7 @@ var Queue = (_class = class {
307
435
  get capacity() {
308
436
  return this.maxSize;
309
437
  }
310
- }, _class);
438
+ }, _class2);
311
439
 
312
440
  // src/id-gen.ts
313
441
 
@@ -486,7 +614,7 @@ var SpanComponentsV1 = class _SpanComponentsV1 {
486
614
  return {
487
615
  objectType: this.objectType,
488
616
  objectId: this.objectId,
489
- rowIds: _optionalChain([this, 'access', _11 => _11.rowIds, 'optionalAccess', _12 => _12.toObject, 'call', _13 => _13()])
617
+ rowIds: _optionalChain([this, 'access', _16 => _16.rowIds, 'optionalAccess', _17 => _17.toObject, 'call', _18 => _18()])
490
618
  };
491
619
  }
492
620
  };
@@ -700,7 +828,7 @@ var SpanComponentsV2 = class _SpanComponentsV2 {
700
828
  objectType: this.objectType,
701
829
  objectId: this.objectId,
702
830
  computeObjectMetadataArgs: this.computeObjectMetadataArgs,
703
- rowIds: _optionalChain([this, 'access', _14 => _14.rowIds, 'optionalAccess', _15 => _15.toObject, 'call', _16 => _16()])
831
+ rowIds: _optionalChain([this, 'access', _19 => _19.rowIds, 'optionalAccess', _20 => _20.toObject, 'call', _21 => _21()])
704
832
  };
705
833
  }
706
834
  };
@@ -1240,20 +1368,20 @@ function slugify(text, options) {
1240
1368
  \u00FF: "y"
1241
1369
  };
1242
1370
  const replacement = "-";
1243
- const trim = _optionalChain([options, 'optionalAccess', _17 => _17.trim]) !== false;
1371
+ const trim = _optionalChain([options, 'optionalAccess', _22 => _22.trim]) !== false;
1244
1372
  let slug = text.normalize().split("").reduce((result, ch) => {
1245
1373
  const mapped = charMap[ch] || ch;
1246
1374
  const appendChar = mapped === replacement ? " " : mapped;
1247
1375
  return result + appendChar.replace(/[^\w\s$*_+~.()'"!\-:@]+/g, "");
1248
1376
  }, "");
1249
- if (_optionalChain([options, 'optionalAccess', _18 => _18.strict])) {
1377
+ if (_optionalChain([options, 'optionalAccess', _23 => _23.strict])) {
1250
1378
  slug = slug.replace(/[^A-Za-z0-9\s]/g, "");
1251
1379
  }
1252
1380
  if (trim) {
1253
1381
  slug = slug.trim();
1254
1382
  }
1255
1383
  slug = slug.replace(/\s+/g, replacement);
1256
- if (_optionalChain([options, 'optionalAccess', _19 => _19.lower])) {
1384
+ if (_optionalChain([options, 'optionalAccess', _24 => _24.lower])) {
1257
1385
  slug = slug.toLowerCase();
1258
1386
  }
1259
1387
  return slug;
@@ -1555,13 +1683,13 @@ function getCurrentUnixTimestamp() {
1555
1683
  function isEmpty2(a) {
1556
1684
  return a === void 0 || a === null;
1557
1685
  }
1558
- var LazyValue = (_class2 = class {
1686
+ var LazyValue = (_class3 = class {
1559
1687
 
1560
- __init3() {this.resolvedValue = void 0}
1561
- __init4() {this.value = {
1688
+ __init4() {this.resolvedValue = void 0}
1689
+ __init5() {this.value = {
1562
1690
  computedState: "uninitialized"
1563
1691
  }}
1564
- constructor(callable) {;_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);
1692
+ constructor(callable) {;_class3.prototype.__init4.call(this);_class3.prototype.__init5.call(this);
1565
1693
  this.callable = callable;
1566
1694
  }
1567
1695
  get() {
@@ -1589,13 +1717,13 @@ var LazyValue = (_class2 = class {
1589
1717
  get hasSucceeded() {
1590
1718
  return this.value.computedState === "succeeded";
1591
1719
  }
1592
- }, _class2);
1593
- var SyncLazyValue = (_class3 = class {
1720
+ }, _class3);
1721
+ var SyncLazyValue = (_class4 = class {
1594
1722
 
1595
- __init5() {this.value = {
1723
+ __init6() {this.value = {
1596
1724
  computedState: "uninitialized"
1597
1725
  }}
1598
- constructor(callable) {;_class3.prototype.__init5.call(this);
1726
+ constructor(callable) {;_class4.prototype.__init6.call(this);
1599
1727
  this.callable = callable;
1600
1728
  }
1601
1729
  get() {
@@ -1611,7 +1739,7 @@ var SyncLazyValue = (_class3 = class {
1611
1739
  get hasSucceeded() {
1612
1740
  return this.value.computedState === "succeeded";
1613
1741
  }
1614
- }, _class3);
1742
+ }, _class4);
1615
1743
  function addAzureBlobHeaders(headers, url) {
1616
1744
  if (url.includes("blob.core.windows.net")) {
1617
1745
  headers["x-ms-blob-type"] = "BlockBlob";
@@ -3380,8 +3508,8 @@ var mustachePlugin = {
3380
3508
  defaultOptions: { strict: true, escape: jsonEscape },
3381
3509
  createRenderer() {
3382
3510
  const opts = _nullishCoalesce(this.defaultOptions, () => ( {}));
3383
- const escapeFn = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _20 => _20.escape]), () => ( jsonEscape));
3384
- const strictDefault = typeof _optionalChain([opts, 'optionalAccess', _21 => _21.strict]) === "boolean" ? opts.strict : true;
3511
+ const escapeFn = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _25 => _25.escape]), () => ( jsonEscape));
3512
+ const strictDefault = typeof _optionalChain([opts, 'optionalAccess', _26 => _26.strict]) === "boolean" ? opts.strict : true;
3385
3513
  return {
3386
3514
  render(template, variables, escape, strict) {
3387
3515
  const esc = _nullishCoalesce(escape, () => ( escapeFn));
@@ -3397,8 +3525,8 @@ var mustachePlugin = {
3397
3525
  };
3398
3526
 
3399
3527
  // src/template/registry.ts
3400
- var TemplatePluginRegistry = (_class4 = class {constructor() { _class4.prototype.__init6.call(this); }
3401
- __init6() {this.plugins = /* @__PURE__ */ new Map()}
3528
+ var TemplatePluginRegistry = (_class5 = class {constructor() { _class5.prototype.__init7.call(this); }
3529
+ __init7() {this.plugins = /* @__PURE__ */ new Map()}
3402
3530
  register(plugin) {
3403
3531
  if (this.plugins.has(plugin.name)) {
3404
3532
  console.warn(
@@ -3415,12 +3543,12 @@ var TemplatePluginRegistry = (_class4 = class {constructor() { _class4.prototype
3415
3543
  return Array.from(this.plugins.keys());
3416
3544
  }
3417
3545
  get(name) {
3418
- return _optionalChain([this, 'access', _22 => _22.plugins, 'access', _23 => _23.get, 'call', _24 => _24(name), 'optionalAccess', _25 => _25.renderer]);
3546
+ return _optionalChain([this, 'access', _27 => _27.plugins, 'access', _28 => _28.get, 'call', _29 => _29(name), 'optionalAccess', _30 => _30.renderer]);
3419
3547
  }
3420
3548
  isRegistered(name) {
3421
3549
  return this.plugins.has(name);
3422
3550
  }
3423
- }, _class4);
3551
+ }, _class5);
3424
3552
  var templateRegistry = new TemplatePluginRegistry();
3425
3553
  var registerTemplatePlugin = templateRegistry.register.bind(templateRegistry);
3426
3554
  var getTemplateRenderer = templateRegistry.get.bind(templateRegistry);
@@ -4034,22 +4162,22 @@ var exitHandlersRegistered = false;
4034
4162
  function canUseSpanCache() {
4035
4163
  return !!(isomorph_default.pathJoin && isomorph_default.tmpdir && isomorph_default.writeFileSync && isomorph_default.appendFileSync && isomorph_default.readFileSync && isomorph_default.unlinkSync && isomorph_default.openFile);
4036
4164
  }
4037
- var SpanCache = (_class5 = class {
4038
- __init7() {this.cacheFilePath = null}
4039
- __init8() {this.fileHandle = null}
4165
+ var SpanCache = (_class6 = class {
4166
+ __init8() {this.cacheFilePath = null}
4167
+ __init9() {this.fileHandle = null}
4040
4168
  // type-erased fs.promises.FileHandle
4041
- __init9() {this.initialized = false}
4042
- __init10() {this.initPromise = null}
4169
+ __init10() {this.initialized = false}
4170
+ __init11() {this.initPromise = null}
4043
4171
  // Tracks whether the cache was explicitly disabled (via constructor or disable())
4044
4172
 
4045
4173
  // Tracks whether the cache has been enabled (for evals only)
4046
- __init11() {this._enabled = false}
4174
+ __init12() {this._enabled = false}
4047
4175
  // Reference count of active evals using this cache
4048
- __init12() {this._activeEvalCount = 0}
4176
+ __init13() {this._activeEvalCount = 0}
4049
4177
  // Small in-memory index tracking which rootSpanIds have data
4050
- __init13() {this.rootSpanIndex = /* @__PURE__ */ new Set()}
4051
- constructor(options) {;_class5.prototype.__init7.call(this);_class5.prototype.__init8.call(this);_class5.prototype.__init9.call(this);_class5.prototype.__init10.call(this);_class5.prototype.__init11.call(this);_class5.prototype.__init12.call(this);_class5.prototype.__init13.call(this);_class5.prototype.__init14.call(this);_class5.prototype.__init15.call(this);_class5.prototype.__init16.call(this);
4052
- this._explicitlyDisabled = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _26 => _26.disabled]), () => ( false));
4178
+ __init14() {this.rootSpanIndex = /* @__PURE__ */ new Set()}
4179
+ constructor(options) {;_class6.prototype.__init8.call(this);_class6.prototype.__init9.call(this);_class6.prototype.__init10.call(this);_class6.prototype.__init11.call(this);_class6.prototype.__init12.call(this);_class6.prototype.__init13.call(this);_class6.prototype.__init14.call(this);_class6.prototype.__init15.call(this);_class6.prototype.__init16.call(this);_class6.prototype.__init17.call(this);
4180
+ this._explicitlyDisabled = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _31 => _31.disabled]), () => ( false));
4053
4181
  }
4054
4182
  /**
4055
4183
  * Disable the cache at runtime. This is called automatically when
@@ -4145,9 +4273,9 @@ var SpanCache = (_class5 = class {
4145
4273
  }
4146
4274
  }
4147
4275
  // Buffer for pending writes - flushed asynchronously
4148
- __init14() {this.writeBuffer = []}
4149
- __init15() {this.flushScheduled = false}
4150
- __init16() {this.flushPromise = null}
4276
+ __init15() {this.writeBuffer = []}
4277
+ __init16() {this.flushScheduled = false}
4278
+ __init17() {this.flushPromise = null}
4151
4279
  /**
4152
4280
  * Queue a span write for async flushing.
4153
4281
  * This is non-blocking - writes are buffered in memory and flushed
@@ -4313,7 +4441,7 @@ var SpanCache = (_class5 = class {
4313
4441
  this.initPromise = null;
4314
4442
  this.rootSpanIndex.clear();
4315
4443
  }
4316
- }, _class5);
4444
+ }, _class6);
4317
4445
 
4318
4446
  // src/logger.ts
4319
4447
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
@@ -4407,13 +4535,13 @@ function getSpanComponentsClass() {
4407
4535
  function getContextManager() {
4408
4536
  return globalThis.BRAINTRUST_CONTEXT_MANAGER ? new globalThis.BRAINTRUST_CONTEXT_MANAGER() : new BraintrustContextManager();
4409
4537
  }
4410
- var NoopSpan = (_class6 = class {
4538
+ var NoopSpan = (_class7 = class {
4411
4539
 
4412
4540
 
4413
4541
 
4414
4542
 
4415
- __init17() {this.kind = "span"}
4416
- constructor() {;_class6.prototype.__init17.call(this);
4543
+ __init18() {this.kind = "span"}
4544
+ constructor() {;_class7.prototype.__init18.call(this);
4417
4545
  this.id = "";
4418
4546
  this.spanId = "";
4419
4547
  this.rootSpanId = "";
@@ -4433,7 +4561,7 @@ var NoopSpan = (_class6 = class {
4433
4561
  return this;
4434
4562
  }
4435
4563
  end(args) {
4436
- return _nullishCoalesce(_optionalChain([args, 'optionalAccess', _27 => _27.endTime]), () => ( getCurrentUnixTimestamp()));
4564
+ return _nullishCoalesce(_optionalChain([args, 'optionalAccess', _32 => _32.endTime]), () => ( getCurrentUnixTimestamp()));
4437
4565
  }
4438
4566
  async export() {
4439
4567
  return "";
@@ -4471,7 +4599,7 @@ var NoopSpan = (_class6 = class {
4471
4599
  toString() {
4472
4600
  return `NoopSpan(id=${this.id}, spanId=${this.spanId})`;
4473
4601
  }
4474
- }, _class6);
4602
+ }, _class7);
4475
4603
  var NOOP_SPAN = new NoopSpan();
4476
4604
  var NOOP_SPAN_PERMALINK = "https://braintrust.dev/noop-span";
4477
4605
  var loginSchema = _v3.z.strictObject({
@@ -4482,11 +4610,14 @@ var loginSchema = _v3.z.strictObject({
4482
4610
  proxyUrl: _v3.z.string(),
4483
4611
  loginToken: _v3.z.string(),
4484
4612
  orgId: _v3.z.string().nullish(),
4485
- gitMetadataSettings: GitMetadataSettings.nullish()
4613
+ gitMetadataSettings: GitMetadataSettings.nullish(),
4614
+ debugLogLevel: _v3.z.enum(["error", "warn", "info", "debug"]).optional(),
4615
+ // Distinguishes explicit false from unset so env fallback stays disabled after deserialization.
4616
+ debugLogLevelDisabled: _v3.z.boolean().optional()
4486
4617
  });
4487
4618
  var stateNonce = 0;
4488
- var BraintrustState = (_class7 = class _BraintrustState {
4489
- constructor(loginParams) {;_class7.prototype.__init18.call(this);_class7.prototype.__init19.call(this);_class7.prototype.__init20.call(this);_class7.prototype.__init21.call(this);_class7.prototype.__init22.call(this);_class7.prototype.__init23.call(this);_class7.prototype.__init24.call(this);_class7.prototype.__init25.call(this);_class7.prototype.__init26.call(this);_class7.prototype.__init27.call(this);_class7.prototype.__init28.call(this);_class7.prototype.__init29.call(this);_class7.prototype.__init30.call(this);_class7.prototype.__init31.call(this);_class7.prototype.__init32.call(this);_class7.prototype.__init33.call(this);
4619
+ var BraintrustState = (_class8 = class _BraintrustState {
4620
+ constructor(loginParams) {;_class8.prototype.__init19.call(this);_class8.prototype.__init20.call(this);_class8.prototype.__init21.call(this);_class8.prototype.__init22.call(this);_class8.prototype.__init23.call(this);_class8.prototype.__init24.call(this);_class8.prototype.__init25.call(this);_class8.prototype.__init26.call(this);_class8.prototype.__init27.call(this);_class8.prototype.__init28.call(this);_class8.prototype.__init29.call(this);_class8.prototype.__init30.call(this);_class8.prototype.__init31.call(this);_class8.prototype.__init32.call(this);_class8.prototype.__init33.call(this);_class8.prototype.__init34.call(this);_class8.prototype.__init35.call(this);
4490
4621
  this.loginParams = loginParams;
4491
4622
  this.id = `${(/* @__PURE__ */ new Date()).toLocaleString()}-${stateNonce++}`;
4492
4623
  this.currentExperiment = void 0;
@@ -4503,6 +4634,16 @@ var BraintrustState = (_class7 = class _BraintrustState {
4503
4634
  this._bgLogger = new SyncLazyValue(
4504
4635
  () => new HTTPBackgroundLogger(new LazyValue(defaultGetLogConn), loginParams)
4505
4636
  );
4637
+ if (loginParams.debugLogLevel !== void 0) {
4638
+ this.debugLogLevelConfigured = true;
4639
+ this.debugLogLevel = normalizeDebugLogLevelOption(
4640
+ loginParams.debugLogLevel
4641
+ );
4642
+ setGlobalDebugLogLevel(_nullishCoalesce(this.debugLogLevel, () => ( false)));
4643
+ } else {
4644
+ this.debugLogLevel = getEnvDebugLogLevel();
4645
+ setGlobalDebugLogLevel(void 0);
4646
+ }
4506
4647
  this.resetLoginInfo();
4507
4648
  const memoryCache = new LRUCache({
4508
4649
  max: _nullishCoalesce(Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_MEMORY_MAX")), () => ( 1 << 10))
@@ -4537,26 +4678,28 @@ var BraintrustState = (_class7 = class _BraintrustState {
4537
4678
  // possibility of multiple loggers floating around, which may not log in a
4538
4679
  // deterministic order.
4539
4680
 
4540
- __init18() {this._overrideBgLogger = null}
4541
- __init19() {this.appUrl = null}
4542
- __init20() {this.appPublicUrl = null}
4543
- __init21() {this.loginToken = null}
4544
- __init22() {this.orgId = null}
4545
- __init23() {this.orgName = null}
4546
- __init24() {this.apiUrl = null}
4547
- __init25() {this.proxyUrl = null}
4548
- __init26() {this.loggedIn = false}
4681
+ __init19() {this._overrideBgLogger = null}
4682
+ __init20() {this.appUrl = null}
4683
+ __init21() {this.appPublicUrl = null}
4684
+ __init22() {this.loginToken = null}
4685
+ __init23() {this.orgId = null}
4686
+ __init24() {this.orgName = null}
4687
+ __init25() {this.apiUrl = null}
4688
+ __init26() {this.proxyUrl = null}
4689
+ __init27() {this.loggedIn = false}
4690
+
4549
4691
 
4550
- __init27() {this.fetch = globalThis.fetch}
4551
- __init28() {this._appConn = null}
4552
- __init29() {this._apiConn = null}
4553
- __init30() {this._proxyConn = null}
4692
+ __init28() {this.debugLogLevelConfigured = false}
4693
+ __init29() {this.fetch = globalThis.fetch}
4694
+ __init30() {this._appConn = null}
4695
+ __init31() {this._apiConn = null}
4696
+ __init32() {this._proxyConn = null}
4554
4697
 
4555
4698
 
4556
4699
 
4557
- __init31() {this._idGenerator = null}
4558
- __init32() {this._contextManager = null}
4559
- __init33() {this._otelFlushCallback = null}
4700
+ __init33() {this._idGenerator = null}
4701
+ __init34() {this._contextManager = null}
4702
+ __init35() {this._otelFlushCallback = null}
4560
4703
  resetLoginInfo() {
4561
4704
  this.appUrl = null;
4562
4705
  this.appPublicUrl = null;
@@ -4612,6 +4755,11 @@ var BraintrustState = (_class7 = class _BraintrustState {
4612
4755
  this.proxyUrl = other.proxyUrl;
4613
4756
  this.loggedIn = other.loggedIn;
4614
4757
  this.gitMetadataSettings = other.gitMetadataSettings;
4758
+ this.debugLogLevel = other.debugLogLevel;
4759
+ this.debugLogLevelConfigured = other.debugLogLevelConfigured;
4760
+ setGlobalDebugLogLevel(
4761
+ this.debugLogLevelConfigured ? _nullishCoalesce(this.debugLogLevel, () => ( false)) : void 0
4762
+ );
4615
4763
  this._appConn = other._appConn;
4616
4764
  this._apiConn = other._apiConn;
4617
4765
  this.loginReplaceApiConn(this.apiConn());
@@ -4636,7 +4784,9 @@ var BraintrustState = (_class7 = class _BraintrustState {
4636
4784
  orgName: this.orgName,
4637
4785
  apiUrl: this.apiUrl,
4638
4786
  proxyUrl: this.proxyUrl,
4639
- gitMetadataSettings: this.gitMetadataSettings
4787
+ gitMetadataSettings: this.gitMetadataSettings,
4788
+ ...this.debugLogLevel ? { debugLogLevel: this.debugLogLevel } : {},
4789
+ ...this.debugLogLevelConfigured && !this.debugLogLevel ? { debugLogLevelDisabled: true } : {}
4640
4790
  };
4641
4791
  }
4642
4792
  static deserialize(serialized, opts) {
@@ -4663,19 +4813,38 @@ var BraintrustState = (_class7 = class _BraintrustState {
4663
4813
  state.proxyConn().set_token(state.loginToken);
4664
4814
  }
4665
4815
  state.loggedIn = true;
4816
+ state.debugLogLevelConfigured = "debugLogLevel" in serializedParsed.data || !!serializedParsed.data.debugLogLevelDisabled;
4817
+ setGlobalDebugLogLevel(
4818
+ state.debugLogLevelConfigured ? _nullishCoalesce(state.debugLogLevel, () => ( false)) : void 0
4819
+ );
4666
4820
  state.loginReplaceApiConn(state.apiConn());
4667
4821
  return state;
4668
4822
  }
4669
4823
  setFetch(fetch2) {
4670
4824
  this.loginParams.fetch = fetch2;
4671
4825
  this.fetch = fetch2;
4672
- _optionalChain([this, 'access', _28 => _28._apiConn, 'optionalAccess', _29 => _29.setFetch, 'call', _30 => _30(fetch2)]);
4673
- _optionalChain([this, 'access', _31 => _31._appConn, 'optionalAccess', _32 => _32.setFetch, 'call', _33 => _33(fetch2)]);
4826
+ _optionalChain([this, 'access', _33 => _33._apiConn, 'optionalAccess', _34 => _34.setFetch, 'call', _35 => _35(fetch2)]);
4827
+ _optionalChain([this, 'access', _36 => _36._appConn, 'optionalAccess', _37 => _37.setFetch, 'call', _38 => _38(fetch2)]);
4674
4828
  }
4675
4829
  setMaskingFunction(maskingFunction) {
4676
4830
  this.bgLogger().setMaskingFunction(maskingFunction);
4677
4831
  }
4832
+ setDebugLogLevel(option) {
4833
+ if (option === void 0) {
4834
+ return;
4835
+ }
4836
+ this.debugLogLevelConfigured = true;
4837
+ this.debugLogLevel = normalizeDebugLogLevelOption(option);
4838
+ setGlobalDebugLogLevel(_nullishCoalesce(this.debugLogLevel, () => ( false)));
4839
+ }
4840
+ getDebugLogLevel() {
4841
+ return this.debugLogLevel;
4842
+ }
4843
+ hasDebugLogLevelOverride() {
4844
+ return this.debugLogLevelConfigured;
4845
+ }
4678
4846
  async login(loginParams) {
4847
+ this.setDebugLogLevel(loginParams.debugLogLevel);
4679
4848
  if (this.apiUrl && !loginParams.forceLogin) {
4680
4849
  return;
4681
4850
  }
@@ -4770,7 +4939,7 @@ var BraintrustState = (_class7 = class _BraintrustState {
4770
4939
  toString() {
4771
4940
  return `BraintrustState(id=${this.id}, org=${this.orgName || "none"}, loggedIn=${this.loggedIn})`;
4772
4941
  }
4773
- }, _class7);
4942
+ }, _class8);
4774
4943
  var _globalState;
4775
4944
  function _internalSetInitialState() {
4776
4945
  if (_globalState) {
@@ -4786,6 +4955,7 @@ function _internalSetInitialState() {
4786
4955
  _globalState = existing;
4787
4956
  }
4788
4957
  var _internalGetGlobalState = () => _globalState;
4958
+ setDebugLogStateResolver(() => _internalGetGlobalState());
4789
4959
  var FailedHTTPResponse = class extends Error {
4790
4960
 
4791
4961
 
@@ -4900,7 +5070,7 @@ var HTTPConnection = class _HTTPConnection {
4900
5070
  return await resp.json();
4901
5071
  } catch (e) {
4902
5072
  if (i < tries - 1) {
4903
- console.log(
5073
+ debugLogger.debug(
4904
5074
  `Retrying API request ${object_type} ${JSON.stringify(args)} ${e.status} ${e.text}`
4905
5075
  );
4906
5076
  continue;
@@ -5112,7 +5282,7 @@ with a Blob/ArrayBuffer, or run the program on Node.js.`
5112
5282
  try {
5113
5283
  statSync2(data);
5114
5284
  } catch (e) {
5115
- console.warn(`Failed to read file: ${e}`);
5285
+ debugLogger.warn(`Failed to read file: ${e}`);
5116
5286
  }
5117
5287
  }
5118
5288
  };
@@ -5356,8 +5526,8 @@ function _getOrgName(orgName) {
5356
5526
  return orgName || isomorph_default.getEnv("BRAINTRUST_ORG_NAME") || void 0;
5357
5527
  }
5358
5528
  function _getLinkBaseUrl(state, linkArgs) {
5359
- const appUrl = _getAppUrl(state.appUrl || _optionalChain([linkArgs, 'optionalAccess', _34 => _34.app_url]));
5360
- const orgName = _getOrgName(state.orgName || _optionalChain([linkArgs, 'optionalAccess', _35 => _35.org_name]));
5529
+ const appUrl = _getAppUrl(state.appUrl || _optionalChain([linkArgs, 'optionalAccess', _39 => _39.app_url]));
5530
+ const orgName = _getOrgName(state.orgName || _optionalChain([linkArgs, 'optionalAccess', _40 => _40.org_name]));
5361
5531
  if (!orgName) {
5362
5532
  return null;
5363
5533
  }
@@ -5367,9 +5537,9 @@ async function permalink(slug, opts) {
5367
5537
  if (slug === "") {
5368
5538
  return NOOP_SPAN_PERMALINK;
5369
5539
  }
5370
- const state = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _36 => _36.state]), () => ( _globalState));
5540
+ const state = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _41 => _41.state]), () => ( _globalState));
5371
5541
  const getOrgName = async () => {
5372
- if (_optionalChain([opts, 'optionalAccess', _37 => _37.orgName])) {
5542
+ if (_optionalChain([opts, 'optionalAccess', _42 => _42.orgName])) {
5373
5543
  return opts.orgName;
5374
5544
  }
5375
5545
  await state.login({});
@@ -5379,7 +5549,7 @@ async function permalink(slug, opts) {
5379
5549
  return state.orgName;
5380
5550
  };
5381
5551
  const getAppUrl = async () => {
5382
- if (_optionalChain([opts, 'optionalAccess', _38 => _38.appUrl])) {
5552
+ if (_optionalChain([opts, 'optionalAccess', _43 => _43.appUrl])) {
5383
5553
  return opts.appUrl;
5384
5554
  }
5385
5555
  await state.login({});
@@ -5457,7 +5627,7 @@ function startSpanParentArgs(args) {
5457
5627
  propagatedEvent: argPropagatedEvent
5458
5628
  };
5459
5629
  }
5460
- var Logger = (_class8 = class {
5630
+ var Logger = (_class9 = class {
5461
5631
 
5462
5632
 
5463
5633
 
@@ -5467,8 +5637,8 @@ var Logger = (_class8 = class {
5467
5637
 
5468
5638
 
5469
5639
  // For type identification.
5470
- __init34() {this.kind = "logger"}
5471
- constructor(state, lazyMetadata, logOptions = {}) {;_class8.prototype.__init34.call(this);
5640
+ __init36() {this.kind = "logger"}
5641
+ constructor(state, lazyMetadata, logOptions = {}) {;_class9.prototype.__init36.call(this);
5472
5642
  this.lazyMetadata = lazyMetadata;
5473
5643
  this._asyncFlush = logOptions.asyncFlush;
5474
5644
  this.computeMetadataArgs = logOptions.computeMetadataArgs;
@@ -5514,7 +5684,7 @@ var Logger = (_class8 = class {
5514
5684
  * @returns The `id` of the logged event.
5515
5685
  */
5516
5686
  log(event, options) {
5517
- if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _39 => _39.allowConcurrentWithSpans])) {
5687
+ if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _44 => _44.allowConcurrentWithSpans])) {
5518
5688
  throw new Error(
5519
5689
  "Cannot run toplevel `log` method while using spans. To log to the span, call `logger.traced` and then log with `span.log`"
5520
5690
  );
@@ -5582,12 +5752,12 @@ var Logger = (_class8 = class {
5582
5752
  state: this.state,
5583
5753
  ...startSpanParentArgs({
5584
5754
  state: this.state,
5585
- parent: _optionalChain([args, 'optionalAccess', _40 => _40.parent]),
5755
+ parent: _optionalChain([args, 'optionalAccess', _45 => _45.parent]),
5586
5756
  parentObjectType: this.parentObjectType(),
5587
5757
  parentObjectId: this.lazyId,
5588
5758
  parentComputeObjectMetadataArgs: this.computeMetadataArgs,
5589
- parentSpanIds: _optionalChain([args, 'optionalAccess', _41 => _41.parentSpanIds]),
5590
- propagatedEvent: _optionalChain([args, 'optionalAccess', _42 => _42.propagatedEvent])
5759
+ parentSpanIds: _optionalChain([args, 'optionalAccess', _46 => _46.parentSpanIds]),
5760
+ propagatedEvent: _optionalChain([args, 'optionalAccess', _47 => _47.propagatedEvent])
5591
5761
  }),
5592
5762
  defaultRootType: "task" /* TASK */
5593
5763
  });
@@ -5655,7 +5825,7 @@ var Logger = (_class8 = class {
5655
5825
  _getLinkBaseUrl() {
5656
5826
  return _getLinkBaseUrl(this.state, this._linkArgs);
5657
5827
  }
5658
- }, _class8);
5828
+ }, _class9);
5659
5829
  function castLogger(logger, asyncFlush) {
5660
5830
  if (logger === void 0) return void 0;
5661
5831
  if (asyncFlush !== void 0 && !!asyncFlush !== !!logger.asyncFlush) {
@@ -5765,31 +5935,31 @@ function now() {
5765
5935
  }
5766
5936
  var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5767
5937
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
5768
- var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5769
-
5770
-
5771
- __init35() {this.activeFlush = Promise.resolve()}
5772
- __init36() {this.activeFlushResolved = true}
5773
- __init37() {this.activeFlushError = void 0}
5774
-
5775
- __init38() {this.maskingFunction = null}
5776
- __init39() {this.syncFlush = false}
5777
- __init40() {this.maxRequestSizeOverride = null}
5778
- __init41() {this._maxRequestSizePromise = null}
5779
- __init42() {this.defaultBatchSize = 100}
5780
- __init43() {this.numTries = 3}
5781
- __init44() {this.queueDropExceedingMaxsize = DEFAULT_QUEUE_SIZE}
5782
- __init45() {this.queueDropLoggingPeriod = 60}
5783
- __init46() {this.failedPublishPayloadsDir = void 0}
5784
- __init47() {this.allPublishPayloadsDir = void 0}
5785
- __init48() {this._flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES}
5786
- __init49() {this._pendingBytes = 0}
5787
- __init50() {this._disabled = false}
5788
- __init51() {this.queueDropLoggingState = {
5938
+ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
5939
+
5940
+
5941
+ __init37() {this.activeFlush = Promise.resolve()}
5942
+ __init38() {this.activeFlushResolved = true}
5943
+ __init39() {this.activeFlushError = void 0}
5944
+
5945
+ __init40() {this.maskingFunction = null}
5946
+ __init41() {this.syncFlush = false}
5947
+ __init42() {this.maxRequestSizeOverride = null}
5948
+ __init43() {this._maxRequestSizePromise = null}
5949
+ __init44() {this.defaultBatchSize = 100}
5950
+ __init45() {this.numTries = 3}
5951
+ __init46() {this.queueDropExceedingMaxsize = DEFAULT_QUEUE_SIZE}
5952
+ __init47() {this.queueDropLoggingPeriod = 60}
5953
+ __init48() {this.failedPublishPayloadsDir = void 0}
5954
+ __init49() {this.allPublishPayloadsDir = void 0}
5955
+ __init50() {this._flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES}
5956
+ __init51() {this._pendingBytes = 0}
5957
+ __init52() {this._disabled = false}
5958
+ __init53() {this.queueDropLoggingState = {
5789
5959
  numDropped: 0,
5790
5960
  lastLoggedTimestamp: 0
5791
5961
  }}
5792
- constructor(apiConn, opts) {;_class9.prototype.__init35.call(this);_class9.prototype.__init36.call(this);_class9.prototype.__init37.call(this);_class9.prototype.__init38.call(this);_class9.prototype.__init39.call(this);_class9.prototype.__init40.call(this);_class9.prototype.__init41.call(this);_class9.prototype.__init42.call(this);_class9.prototype.__init43.call(this);_class9.prototype.__init44.call(this);_class9.prototype.__init45.call(this);_class9.prototype.__init46.call(this);_class9.prototype.__init47.call(this);_class9.prototype.__init48.call(this);_class9.prototype.__init49.call(this);_class9.prototype.__init50.call(this);_class9.prototype.__init51.call(this);
5962
+ constructor(apiConn, opts) {;_class10.prototype.__init37.call(this);_class10.prototype.__init38.call(this);_class10.prototype.__init39.call(this);_class10.prototype.__init40.call(this);_class10.prototype.__init41.call(this);_class10.prototype.__init42.call(this);_class10.prototype.__init43.call(this);_class10.prototype.__init44.call(this);_class10.prototype.__init45.call(this);_class10.prototype.__init46.call(this);_class10.prototype.__init47.call(this);_class10.prototype.__init48.call(this);_class10.prototype.__init49.call(this);_class10.prototype.__init50.call(this);_class10.prototype.__init51.call(this);_class10.prototype.__init52.call(this);_class10.prototype.__init53.call(this);
5793
5963
  opts = _nullishCoalesce(opts, () => ( {}));
5794
5964
  this.apiConn = apiConn;
5795
5965
  const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
@@ -5824,7 +5994,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5824
5994
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5825
5995
  }
5826
5996
  if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5827
- console.warn(
5997
+ debugLogger.warn(
5828
5998
  "BRAINTRUST_LOG_FLUSH_CHUNK_SIZE is deprecated and no longer has any effect. Log flushing now sends all items at once and batches them automatically. This environment variable will be removed in a future major release."
5829
5999
  );
5830
6000
  }
@@ -5886,7 +6056,10 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5886
6056
  const versionInfo = await conn.get_json("version");
5887
6057
  serverLimit = _nullishCoalesce(_v3.z.object({ logs3_payload_max_bytes: _v3.z.number().nullish() }).parse(versionInfo).logs3_payload_max_bytes, () => ( null));
5888
6058
  } catch (e) {
5889
- console.warn("Failed to fetch version info for payload limit:", e);
6059
+ debugLogger.warn(
6060
+ "Failed to fetch version info for payload limit:",
6061
+ e
6062
+ );
5890
6063
  }
5891
6064
  const validServerLimit = serverLimit !== null && serverLimit > 0 ? serverLimit : null;
5892
6065
  const canUseOverflow = validServerLimit !== null;
@@ -5919,7 +6092,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5919
6092
  this.queue.clear();
5920
6093
  return;
5921
6094
  }
5922
- const batchSize = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _43 => _43.batchSize]), () => ( this.defaultBatchSize));
6095
+ const batchSize = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _48 => _48.batchSize]), () => ( this.defaultBatchSize));
5923
6096
  const wrappedItems = this.queue.drain();
5924
6097
  if (wrappedItems.length === 0) {
5925
6098
  return;
@@ -6030,16 +6203,16 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
6030
6203
  if (isRetrying) {
6031
6204
  errmsg += ". Retrying";
6032
6205
  }
6033
- console.warn(errmsg);
6206
+ debugLogger.warn(errmsg);
6034
6207
  if (!isRetrying) {
6035
- console.warn(
6208
+ debugLogger.warn(
6036
6209
  `Failed to construct log records to flush after ${this.numTries} attempts. Dropping batch`
6037
6210
  );
6038
6211
  throw e;
6039
6212
  } else {
6040
- console.warn(e);
6213
+ debugLogger.warn(e);
6041
6214
  const sleepTimeS = BACKGROUND_LOGGER_BASE_SLEEP_TIME_S * 2 ** i;
6042
- console.info(`Sleeping for ${sleepTimeS}s`);
6215
+ debugLogger.info(`Sleeping for ${sleepTimeS}s`);
6043
6216
  await new Promise(
6044
6217
  (resolve) => setTimeout(resolve, sleepTimeS * 1e3)
6045
6218
  );
@@ -6140,15 +6313,15 @@ Error: ${errorText}`;
6140
6313
  this.logFailedPayloadsDir();
6141
6314
  }
6142
6315
  if (!isRetrying) {
6143
- console.warn(
6316
+ debugLogger.warn(
6144
6317
  `log request failed after ${this.numTries} retries. Dropping batch`
6145
6318
  );
6146
6319
  throw new Error(errMsg);
6147
6320
  } else {
6148
- console.warn(errMsg);
6321
+ debugLogger.warn(errMsg);
6149
6322
  if (isRetrying) {
6150
6323
  const sleepTimeS = BACKGROUND_LOGGER_BASE_SLEEP_TIME_S * 2 ** i;
6151
- console.info(`Sleeping for ${sleepTimeS}s`);
6324
+ debugLogger.info(`Sleeping for ${sleepTimeS}s`);
6152
6325
  await new Promise(
6153
6326
  (resolve) => setTimeout(resolve, sleepTimeS * 1e3)
6154
6327
  );
@@ -6163,7 +6336,7 @@ Error: ${errorText}`;
6163
6336
  this.queueDropLoggingState.numDropped += numItems;
6164
6337
  const timeNow = getCurrentUnixTimestamp();
6165
6338
  if (timeNow - this.queueDropLoggingState.lastLoggedTimestamp > this.queueDropLoggingPeriod) {
6166
- console.warn(
6339
+ debugLogger.warn(
6167
6340
  `Dropped ${this.queueDropLoggingState.numDropped} elements due to full queue`
6168
6341
  );
6169
6342
  if (this.failedPublishPayloadsDir) {
@@ -6195,7 +6368,7 @@ Error: ${errorText}`;
6195
6368
  await _HTTPBackgroundLogger.writePayloadToDir({ payloadDir, payload });
6196
6369
  }
6197
6370
  } catch (e) {
6198
- console.error(e);
6371
+ debugLogger.error(e);
6199
6372
  }
6200
6373
  }
6201
6374
  static async writePayloadToDir({
@@ -6203,7 +6376,7 @@ Error: ${errorText}`;
6203
6376
  payload
6204
6377
  }) {
6205
6378
  if (!(isomorph_default.pathJoin && isomorph_default.mkdir && isomorph_default.writeFile)) {
6206
- console.warn(
6379
+ debugLogger.warn(
6207
6380
  "Cannot dump payloads: filesystem-operations not supported on this platform"
6208
6381
  );
6209
6382
  return;
@@ -6216,7 +6389,7 @@ Error: ${errorText}`;
6216
6389
  await isomorph_default.mkdir(payloadDir, { recursive: true });
6217
6390
  await isomorph_default.writeFile(payloadFile, payload);
6218
6391
  } catch (e) {
6219
- console.error(
6392
+ debugLogger.error(
6220
6393
  `Failed to write failed payload to output file ${payloadFile}:
6221
6394
  `,
6222
6395
  e
@@ -6233,10 +6406,10 @@ Error: ${errorText}`;
6233
6406
  } catch (err) {
6234
6407
  if (err instanceof AggregateError) {
6235
6408
  for (const e of err.errors) {
6236
- _optionalChain([this, 'access', _44 => _44.onFlushError, 'optionalCall', _45 => _45(e)]);
6409
+ _optionalChain([this, 'access', _49 => _49.onFlushError, 'optionalCall', _50 => _50(e)]);
6237
6410
  }
6238
6411
  } else {
6239
- _optionalChain([this, 'access', _46 => _46.onFlushError, 'optionalCall', _47 => _47(err)]);
6412
+ _optionalChain([this, 'access', _51 => _51.onFlushError, 'optionalCall', _52 => _52(err)]);
6240
6413
  }
6241
6414
  this.activeFlushError = err;
6242
6415
  } finally {
@@ -6247,7 +6420,9 @@ Error: ${errorText}`;
6247
6420
  }
6248
6421
  }
6249
6422
  logFailedPayloadsDir() {
6250
- console.warn(`Logging failed payloads to ${this.failedPublishPayloadsDir}`);
6423
+ debugLogger.warn(
6424
+ `Logging failed payloads to ${this.failedPublishPayloadsDir}`
6425
+ );
6251
6426
  }
6252
6427
  // Should only be called by BraintrustState.
6253
6428
  internalReplaceApiConn(apiConn) {
@@ -6259,7 +6434,7 @@ Error: ${errorText}`;
6259
6434
  enforceQueueSizeLimit(enforce) {
6260
6435
  this.queue.enforceQueueSizeLimit(enforce);
6261
6436
  }
6262
- }, _class9);
6437
+ }, _class10);
6263
6438
  function init(projectOrOptions, optionalOptions) {
6264
6439
  const options = (() => {
6265
6440
  if (typeof projectOrOptions === "string") {
@@ -6412,9 +6587,7 @@ function init(projectOrOptions, optionalOptions) {
6412
6587
  break;
6413
6588
  } catch (e) {
6414
6589
  if (args["base_experiment"] && `${"data" in e && e.data}`.includes("base experiment")) {
6415
- console.warn(
6416
- `Base experiment ${args["base_experiment"]} not found.`
6417
- );
6590
+ debugLogger.forState(state).warn(`Base experiment ${args["base_experiment"]} not found.`);
6418
6591
  delete args["base_experiment"];
6419
6592
  } else {
6420
6593
  throw e;
@@ -6556,7 +6729,11 @@ async function computeLoggerMetadata(state, {
6556
6729
  }
6557
6730
  async function login(options = {}) {
6558
6731
  const { forceLogin = false } = options || {};
6732
+ if (!_internalGetGlobalState()) {
6733
+ _internalSetInitialState();
6734
+ }
6559
6735
  const state = _internalGetGlobalState();
6736
+ state.setDebugLogLevel(options.debugLogLevel);
6560
6737
  if (state.loggedIn && !forceLogin) {
6561
6738
  let checkUpdatedParam2 = function(varname, arg, orig) {
6562
6739
  if (!isEmpty2(arg) && !isEmpty2(orig) && arg !== orig) {
@@ -6575,9 +6752,6 @@ async function login(options = {}) {
6575
6752
  checkUpdatedParam2("orgName", options.orgName, state.orgName);
6576
6753
  return state;
6577
6754
  }
6578
- if (!state) {
6579
- _internalSetInitialState();
6580
- }
6581
6755
  await state.login(options);
6582
6756
  return state;
6583
6757
  }
@@ -6650,24 +6824,24 @@ async function loginToState(options = {}) {
6650
6824
  return state;
6651
6825
  }
6652
6826
  function currentExperiment(options) {
6653
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _48 => _48.state]), () => ( _globalState));
6827
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _53 => _53.state]), () => ( _globalState));
6654
6828
  return state.currentExperiment;
6655
6829
  }
6656
6830
  function currentLogger(options) {
6657
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _49 => _49.state]), () => ( _globalState));
6658
- return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _50 => _50.asyncFlush]));
6831
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _54 => _54.state]), () => ( _globalState));
6832
+ return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _55 => _55.asyncFlush]));
6659
6833
  }
6660
6834
  function currentSpan(options) {
6661
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _51 => _51.state]), () => ( _globalState));
6835
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.state]), () => ( _globalState));
6662
6836
  return _nullishCoalesce(state.contextManager.getCurrentSpan(), () => ( NOOP_SPAN));
6663
6837
  }
6664
6838
  function getSpanParentObject(options) {
6665
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _52 => _52.state]), () => ( _globalState));
6839
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _57 => _57.state]), () => ( _globalState));
6666
6840
  const parentSpan = currentSpan({ state });
6667
6841
  if (!Object.is(parentSpan, NOOP_SPAN)) {
6668
6842
  return parentSpan;
6669
6843
  }
6670
- const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _53 => _53.parent]), () => ( state.currentParent.getStore()));
6844
+ const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _58 => _58.parent]), () => ( state.currentParent.getStore()));
6671
6845
  if (parentStr) return getSpanComponentsClass().fromStr(parentStr);
6672
6846
  const experiment = currentExperiment();
6673
6847
  if (experiment) {
@@ -6696,7 +6870,7 @@ function traced(callback, args) {
6696
6870
  const { span, isSyncFlushLogger } = startSpanAndIsLogger(args);
6697
6871
  const ret = runCatchFinally(
6698
6872
  () => {
6699
- if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _54 => _54.setCurrent]), () => ( true))) {
6873
+ if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _59 => _59.setCurrent]), () => ( true))) {
6700
6874
  return withCurrent(span, callback);
6701
6875
  } else {
6702
6876
  return callback(span);
@@ -6708,7 +6882,7 @@ function traced(callback, args) {
6708
6882
  },
6709
6883
  () => span.end()
6710
6884
  );
6711
- if (_optionalChain([args, 'optionalAccess', _55 => _55.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _56 => _56.asyncFlush])) {
6885
+ if (_optionalChain([args, 'optionalAccess', _60 => _60.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _61 => _61.asyncFlush])) {
6712
6886
  return ret;
6713
6887
  } else {
6714
6888
  return (async () => {
@@ -6724,14 +6898,14 @@ function startSpan(args) {
6724
6898
  return startSpanAndIsLogger(args).span;
6725
6899
  }
6726
6900
  async function flush(options) {
6727
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _57 => _57.state]), () => ( _globalState));
6901
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _62 => _62.state]), () => ( _globalState));
6728
6902
  return await state.bgLogger().flush();
6729
6903
  }
6730
6904
  function startSpanAndIsLogger(args) {
6731
- const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _58 => _58.state]), () => ( _globalState));
6905
+ const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _63 => _63.state]), () => ( _globalState));
6732
6906
  const parentObject = getSpanParentObject({
6733
- asyncFlush: _optionalChain([args, 'optionalAccess', _59 => _59.asyncFlush]),
6734
- parent: _optionalChain([args, 'optionalAccess', _60 => _60.parent]),
6907
+ asyncFlush: _optionalChain([args, 'optionalAccess', _64 => _64.asyncFlush]),
6908
+ parent: _optionalChain([args, 'optionalAccess', _65 => _65.parent]),
6735
6909
  state
6736
6910
  });
6737
6911
  if (parentObject instanceof SpanComponentsV3 || parentObject instanceof SpanComponentsV4) {
@@ -6748,14 +6922,14 @@ function startSpanAndIsLogger(args) {
6748
6922
  ),
6749
6923
  parentComputeObjectMetadataArgs: _nullishCoalesce(parentObject.data.compute_object_metadata_args, () => ( void 0)),
6750
6924
  parentSpanIds,
6751
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _61 => _61.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6925
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _66 => _66.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6752
6926
  (_nullishCoalesce(parentObject.data.propagated_event, () => ( void 0)))))
6753
6927
  });
6754
6928
  return {
6755
6929
  span,
6756
6930
  isSyncFlushLogger: parentObject.data.object_type === 2 /* PROJECT_LOGS */ && // Since there's no parent logger here, we're free to choose the async flush
6757
6931
  // behavior, and therefore propagate along whatever we get from the arguments
6758
- _optionalChain([args, 'optionalAccess', _62 => _62.asyncFlush]) === false
6932
+ _optionalChain([args, 'optionalAccess', _67 => _67.asyncFlush]) === false
6759
6933
  };
6760
6934
  } else {
6761
6935
  const span = parentObject.startSpan(args);
@@ -6903,10 +7077,10 @@ function extractAttachments(event, attachments) {
6903
7077
  event[key] = value.reference;
6904
7078
  continue;
6905
7079
  }
6906
- if (_optionalChain([value, 'optionalAccess', _63 => _63.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
7080
+ if (_optionalChain([value, 'optionalAccess', _68 => _68.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
6907
7081
  continue;
6908
7082
  }
6909
- if (_optionalChain([value, 'optionalAccess', _64 => _64.reference, 'optionalAccess', _65 => _65.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _66 => _66.uploader])) {
7083
+ if (_optionalChain([value, 'optionalAccess', _69 => _69.reference, 'optionalAccess', _70 => _70.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _71 => _71.uploader])) {
6910
7084
  const attachment = new Attachment({
6911
7085
  data: value.dataDebugString,
6912
7086
  filename: value.reference.filename,
@@ -6972,14 +7146,14 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
6972
7146
  }
6973
7147
  var DEFAULT_FETCH_BATCH_SIZE = 1e3;
6974
7148
  var MAX_BTQL_ITERATIONS = 1e4;
6975
- var ObjectFetcher = (_class10 = class {
6976
- constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class10.prototype.__init52.call(this);
7149
+ var ObjectFetcher = (_class11 = class {
7150
+ constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class11.prototype.__init54.call(this);
6977
7151
  this.objectType = objectType;
6978
7152
  this.pinnedVersion = pinnedVersion;
6979
7153
  this.mutateRecord = mutateRecord;
6980
7154
  this._internal_btql = _internal_btql;
6981
7155
  }
6982
- __init52() {this._fetchedData = void 0}
7156
+ __init54() {this._fetchedData = void 0}
6983
7157
  get id() {
6984
7158
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
6985
7159
  }
@@ -6989,7 +7163,14 @@ var ObjectFetcher = (_class10 = class {
6989
7163
  async *fetchRecordsFromApi(batchSize) {
6990
7164
  const state = await this.getState();
6991
7165
  const objectId = await this.id;
6992
- const limit = _nullishCoalesce(batchSize, () => ( DEFAULT_FETCH_BATCH_SIZE));
7166
+ const batchLimit = _nullishCoalesce(batchSize, () => ( DEFAULT_FETCH_BATCH_SIZE));
7167
+ const internalLimit = _optionalChain([this, 'access', _72 => _72._internal_btql, 'optionalAccess', _73 => _73.limit]);
7168
+ const limit = batchSize !== void 0 ? batchSize : _nullishCoalesce(internalLimit, () => ( batchLimit));
7169
+ const internalBtqlWithoutReservedQueryKeys = Object.fromEntries(
7170
+ Object.entries(_nullishCoalesce(this._internal_btql, () => ( {}))).filter(
7171
+ ([key]) => key !== "cursor" && key !== "limit" && key !== "select" && key !== "from"
7172
+ )
7173
+ );
6993
7174
  let cursor = void 0;
6994
7175
  let iterations = 0;
6995
7176
  while (true) {
@@ -6997,7 +7178,6 @@ var ObjectFetcher = (_class10 = class {
6997
7178
  `btql`,
6998
7179
  {
6999
7180
  query: {
7000
- ...this._internal_btql,
7001
7181
  select: [
7002
7182
  {
7003
7183
  op: "star"
@@ -7017,7 +7197,8 @@ var ObjectFetcher = (_class10 = class {
7017
7197
  ]
7018
7198
  },
7019
7199
  cursor,
7020
- limit
7200
+ limit,
7201
+ ...internalBtqlWithoutReservedQueryKeys
7021
7202
  },
7022
7203
  use_columnstore: false,
7023
7204
  brainstore_realtime: true,
@@ -7057,7 +7238,7 @@ var ObjectFetcher = (_class10 = class {
7057
7238
  }
7058
7239
  return;
7059
7240
  }
7060
- for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _67 => _67.batchSize]))) {
7241
+ for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _74 => _74.batchSize]))) {
7061
7242
  yield record;
7062
7243
  }
7063
7244
  }
@@ -7067,7 +7248,7 @@ var ObjectFetcher = (_class10 = class {
7067
7248
  async fetchedData(options) {
7068
7249
  if (this._fetchedData === void 0) {
7069
7250
  const data = [];
7070
- for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _68 => _68.batchSize]))) {
7251
+ for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _75 => _75.batchSize]))) {
7071
7252
  data.push(record);
7072
7253
  }
7073
7254
  this._fetchedData = data;
@@ -7091,8 +7272,8 @@ var ObjectFetcher = (_class10 = class {
7091
7272
  return maxVersion;
7092
7273
  }
7093
7274
  }
7094
- }, _class10);
7095
- var Experiment2 = (_class11 = class extends ObjectFetcher {
7275
+ }, _class11);
7276
+ var Experiment2 = (_class12 = class extends ObjectFetcher {
7096
7277
 
7097
7278
 
7098
7279
 
@@ -7100,9 +7281,9 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7100
7281
 
7101
7282
 
7102
7283
  // For type identification.
7103
- __init53() {this.kind = "experiment"}
7284
+ __init55() {this.kind = "experiment"}
7104
7285
  constructor(state, lazyMetadata, dataset) {
7105
- super("experiment", void 0, (r) => enrichAttachments(r, state));_class11.prototype.__init53.call(this);;
7286
+ super("experiment", void 0, (r) => enrichAttachments(r, state));_class12.prototype.__init55.call(this);;
7106
7287
  this.lazyMetadata = lazyMetadata;
7107
7288
  this.dataset = dataset;
7108
7289
  this.lastStartTime = getCurrentUnixTimestamp();
@@ -7162,7 +7343,7 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7162
7343
  * @returns The `id` of the logged event.
7163
7344
  */
7164
7345
  log(event, options) {
7165
- if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _69 => _69.allowConcurrentWithSpans])) {
7346
+ if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _76 => _76.allowConcurrentWithSpans])) {
7166
7347
  throw new Error(
7167
7348
  "Cannot run toplevel `log` method while using spans. To log to the span, call `experiment.traced` and then log with `span.log`"
7168
7349
  );
@@ -7215,12 +7396,12 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7215
7396
  state: this.state,
7216
7397
  ...startSpanParentArgs({
7217
7398
  state: this.state,
7218
- parent: _optionalChain([args, 'optionalAccess', _70 => _70.parent]),
7399
+ parent: _optionalChain([args, 'optionalAccess', _77 => _77.parent]),
7219
7400
  parentObjectType: this.parentObjectType(),
7220
7401
  parentObjectId: this.lazyId,
7221
7402
  parentComputeObjectMetadataArgs: void 0,
7222
7403
  parentSpanIds: void 0,
7223
- propagatedEvent: _optionalChain([args, 'optionalAccess', _71 => _71.propagatedEvent])
7404
+ propagatedEvent: _optionalChain([args, 'optionalAccess', _78 => _78.propagatedEvent])
7224
7405
  }),
7225
7406
  defaultRootType: "eval" /* EVAL */
7226
7407
  });
@@ -7290,7 +7471,7 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7290
7471
  scores = results["scores"];
7291
7472
  metrics = results["metrics"];
7292
7473
  } catch (e) {
7293
- console.warn(
7474
+ debugLogger.forState(state).warn(
7294
7475
  `Failed to fetch experiment scores and metrics: ${e}
7295
7476
 
7296
7477
  View complete results in Braintrust or run experiment.summarize() again.`
@@ -7367,12 +7548,12 @@ View complete results in Braintrust or run experiment.summarize() again.`
7367
7548
  * @deprecated This function is deprecated. You can simply remove it from your code.
7368
7549
  */
7369
7550
  async close() {
7370
- console.warn(
7551
+ debugLogger.forState(this.state).warn(
7371
7552
  "close is deprecated and will be removed in a future version of braintrust. It is now a no-op and can be removed"
7372
7553
  );
7373
7554
  return this.id;
7374
7555
  }
7375
- }, _class11);
7556
+ }, _class12);
7376
7557
  var ReadonlyExperiment = class extends ObjectFetcher {
7377
7558
  constructor(state, lazyMetadata) {
7378
7559
  super("experiment", void 0, (r) => enrichAttachments(r, state));
@@ -7448,7 +7629,7 @@ function _resolveSpanIds(spanId, parentSpanIds, lookupSpanParent, idGenerator, c
7448
7629
  spanParents: void 0
7449
7630
  };
7450
7631
  }
7451
- var SpanImpl = (_class12 = class _SpanImpl {
7632
+ var SpanImpl = (_class13 = class _SpanImpl {
7452
7633
 
7453
7634
 
7454
7635
 
@@ -7461,8 +7642,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7461
7642
 
7462
7643
 
7463
7644
 
7464
- __init54() {this.kind = "span"}
7465
- constructor(args) {;_class12.prototype.__init54.call(this);
7645
+ __init56() {this.kind = "span"}
7646
+ constructor(args) {;_class13.prototype.__init56.call(this);
7466
7647
  this._state = args.state;
7467
7648
  const spanAttributes = _nullishCoalesce(args.spanAttributes, () => ( {}));
7468
7649
  const rawEvent = _nullishCoalesce(args.event, () => ( {}));
@@ -7564,8 +7745,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7564
7745
  ...serializableInternalData,
7565
7746
  [IS_MERGE_FIELD]: this.isMerge
7566
7747
  });
7567
- if (_optionalChain([partialRecord, 'access', _72 => _72.metrics, 'optionalAccess', _73 => _73.end])) {
7568
- this.loggedEndTime = _optionalChain([partialRecord, 'access', _74 => _74.metrics, 'optionalAccess', _75 => _75.end]);
7748
+ if (typeof _optionalChain([partialRecord, 'access', _79 => _79.metrics, 'optionalAccess', _80 => _80.end]) === "number") {
7749
+ this.loggedEndTime = partialRecord.metrics.end;
7569
7750
  }
7570
7751
  if (this.parentObjectType === 1 /* EXPERIMENT */) {
7571
7752
  const cachedSpan = {
@@ -7624,18 +7805,18 @@ var SpanImpl = (_class12 = class _SpanImpl {
7624
7805
  );
7625
7806
  }
7626
7807
  startSpan(args) {
7627
- const parentSpanIds = _optionalChain([args, 'optionalAccess', _76 => _76.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
7808
+ const parentSpanIds = _optionalChain([args, 'optionalAccess', _81 => _81.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
7628
7809
  return new _SpanImpl({
7629
7810
  state: this._state,
7630
7811
  ...args,
7631
7812
  ...startSpanParentArgs({
7632
7813
  state: this._state,
7633
- parent: _optionalChain([args, 'optionalAccess', _77 => _77.parent]),
7814
+ parent: _optionalChain([args, 'optionalAccess', _82 => _82.parent]),
7634
7815
  parentObjectType: this.parentObjectType,
7635
7816
  parentObjectId: this.parentObjectId,
7636
7817
  parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
7637
7818
  parentSpanIds,
7638
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _78 => _78.propagatedEvent]), () => ( this.propagatedEvent))
7819
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _83 => _83.propagatedEvent]), () => ( this.propagatedEvent))
7639
7820
  })
7640
7821
  });
7641
7822
  }
@@ -7649,12 +7830,12 @@ var SpanImpl = (_class12 = class _SpanImpl {
7649
7830
  ...args,
7650
7831
  ...startSpanParentArgs({
7651
7832
  state: this._state,
7652
- parent: _optionalChain([args, 'optionalAccess', _79 => _79.parent]),
7833
+ parent: _optionalChain([args, 'optionalAccess', _84 => _84.parent]),
7653
7834
  parentObjectType: this.parentObjectType,
7654
7835
  parentObjectId: this.parentObjectId,
7655
7836
  parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
7656
7837
  parentSpanIds,
7657
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _80 => _80.propagatedEvent]), () => ( this.propagatedEvent))
7838
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _85 => _85.propagatedEvent]), () => ( this.propagatedEvent))
7658
7839
  }),
7659
7840
  spanId
7660
7841
  });
@@ -7663,7 +7844,7 @@ var SpanImpl = (_class12 = class _SpanImpl {
7663
7844
  let endTime;
7664
7845
  let internalData = {};
7665
7846
  if (!this.loggedEndTime) {
7666
- endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _81 => _81.endTime]), () => ( getCurrentUnixTimestamp()));
7847
+ endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _86 => _86.endTime]), () => ( getCurrentUnixTimestamp()));
7667
7848
  internalData = { metrics: { end: endTime } };
7668
7849
  } else {
7669
7850
  endTime = this.loggedEndTime;
@@ -7714,8 +7895,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7714
7895
  const args = this.parentComputeObjectMetadataArgs;
7715
7896
  switch (this.parentObjectType) {
7716
7897
  case 2 /* PROJECT_LOGS */: {
7717
- const projectID = _optionalChain([args, 'optionalAccess', _82 => _82.project_id]) || this.parentObjectId.getSync().value;
7718
- const projectName = _optionalChain([args, 'optionalAccess', _83 => _83.project_name]);
7898
+ const projectID = _optionalChain([args, 'optionalAccess', _87 => _87.project_id]) || this.parentObjectId.getSync().value;
7899
+ const projectName = _optionalChain([args, 'optionalAccess', _88 => _88.project_name]);
7719
7900
  if (projectID) {
7720
7901
  return `${baseUrl}/object?object_type=project_logs&object_id=${projectID}&id=${this._id}`;
7721
7902
  } else if (projectName) {
@@ -7725,7 +7906,7 @@ var SpanImpl = (_class12 = class _SpanImpl {
7725
7906
  }
7726
7907
  }
7727
7908
  case 1 /* EXPERIMENT */: {
7728
- const expID = _optionalChain([args, 'optionalAccess', _84 => _84.experiment_id]) || _optionalChain([this, 'access', _85 => _85.parentObjectId, 'optionalAccess', _86 => _86.getSync, 'call', _87 => _87(), 'optionalAccess', _88 => _88.value]);
7909
+ const expID = _optionalChain([args, 'optionalAccess', _89 => _89.experiment_id]) || _optionalChain([this, 'access', _90 => _90.parentObjectId, 'optionalAccess', _91 => _91.getSync, 'call', _92 => _92(), 'optionalAccess', _93 => _93.value]);
7729
7910
  if (!expID) {
7730
7911
  return getErrPermlink("provide-experiment-id");
7731
7912
  } else {
@@ -7765,7 +7946,7 @@ var SpanImpl = (_class12 = class _SpanImpl {
7765
7946
  toString() {
7766
7947
  return `SpanImpl(id=${this.id}, spanId=${this.spanId})`;
7767
7948
  }
7768
- }, _class12);
7949
+ }, _class13);
7769
7950
  function splitLoggingData({
7770
7951
  event,
7771
7952
  internalData
@@ -7796,11 +7977,11 @@ function splitLoggingData({
7796
7977
  }
7797
7978
  return [serializableInternalData, lazyInternalData];
7798
7979
  }
7799
- var Dataset2 = (_class13 = class extends ObjectFetcher {
7980
+ var Dataset2 = (_class14 = class extends ObjectFetcher {
7800
7981
  constructor(state, lazyMetadata, pinnedVersion, legacy, _internal_btql) {
7801
7982
  const isLegacyDataset = _nullishCoalesce(legacy, () => ( DEFAULT_IS_LEGACY_DATASET));
7802
7983
  if (isLegacyDataset) {
7803
- console.warn(
7984
+ debugLogger.forState(state).warn(
7804
7985
  `Records will be fetched from this dataset in the legacy format, with the "expected" field renamed to "output". Please update your code to use "expected", and use \`braintrust.initDataset()\` with \`{ useOutput: false }\`, which will become the default in a future version of Braintrust.`
7805
7986
  );
7806
7987
  }
@@ -7815,13 +7996,13 @@ var Dataset2 = (_class13 = class extends ObjectFetcher {
7815
7996
  )
7816
7997
  ),
7817
7998
  _internal_btql
7818
- );_class13.prototype.__init55.call(this);_class13.prototype.__init56.call(this);;
7999
+ );_class14.prototype.__init57.call(this);_class14.prototype.__init58.call(this);;
7819
8000
  this.state = state;
7820
8001
  this.lazyMetadata = lazyMetadata;
7821
8002
  }
7822
8003
 
7823
- __init55() {this.__braintrust_dataset_marker = true}
7824
- __init56() {this.newRecords = 0}
8004
+ __init57() {this.__braintrust_dataset_marker = true}
8005
+ __init58() {this.newRecords = 0}
7825
8006
  get id() {
7826
8007
  return (async () => {
7827
8008
  return (await this.lazyMetadata.get()).dataset.id;
@@ -8031,7 +8212,7 @@ var Dataset2 = (_class13 = class extends ObjectFetcher {
8031
8212
  * @deprecated This function is deprecated. You can simply remove it from your code.
8032
8213
  */
8033
8214
  async close() {
8034
- console.warn(
8215
+ debugLogger.forState(this.state).warn(
8035
8216
  "close is deprecated and will be removed in a future version of braintrust. It is now a no-op and can be removed"
8036
8217
  );
8037
8218
  return this.id;
@@ -8039,7 +8220,7 @@ var Dataset2 = (_class13 = class extends ObjectFetcher {
8039
8220
  static isDataset(data) {
8040
8221
  return typeof data === "object" && data !== null && "__braintrust_dataset_marker" in data;
8041
8222
  }
8042
- }, _class13);
8223
+ }, _class14);
8043
8224
  function renderMessage(render, message) {
8044
8225
  return {
8045
8226
  ...message,
@@ -8161,15 +8342,15 @@ function renderPromptParams(params, args, options = {}) {
8161
8342
  }
8162
8343
  return params;
8163
8344
  }
8164
- var Prompt2 = (_class14 = class _Prompt {
8165
- constructor(metadata, defaults, noTrace) {;_class14.prototype.__init57.call(this);_class14.prototype.__init58.call(this);
8345
+ var Prompt2 = (_class15 = class _Prompt {
8346
+ constructor(metadata, defaults, noTrace) {;_class15.prototype.__init59.call(this);_class15.prototype.__init60.call(this);
8166
8347
  this.metadata = metadata;
8167
8348
  this.defaults = defaults;
8168
8349
  this.noTrace = noTrace;
8169
8350
  }
8170
8351
 
8171
- __init57() {this.hasParsedPromptData = false}
8172
- __init58() {this.__braintrust_prompt_marker = true}
8352
+ __init59() {this.hasParsedPromptData = false}
8353
+ __init60() {this.__braintrust_prompt_marker = true}
8173
8354
  get id() {
8174
8355
  return this.metadata.id;
8175
8356
  }
@@ -8183,16 +8364,16 @@ var Prompt2 = (_class14 = class _Prompt {
8183
8364
  return "slug" in this.metadata ? this.metadata.slug : this.metadata.id;
8184
8365
  }
8185
8366
  get prompt() {
8186
- return _optionalChain([this, 'access', _89 => _89.getParsedPromptData, 'call', _90 => _90(), 'optionalAccess', _91 => _91.prompt]);
8367
+ return _optionalChain([this, 'access', _94 => _94.getParsedPromptData, 'call', _95 => _95(), 'optionalAccess', _96 => _96.prompt]);
8187
8368
  }
8188
8369
  get version() {
8189
8370
  return this.metadata[TRANSACTION_ID_FIELD];
8190
8371
  }
8191
8372
  get options() {
8192
- return _optionalChain([this, 'access', _92 => _92.getParsedPromptData, 'call', _93 => _93(), 'optionalAccess', _94 => _94.options]) || {};
8373
+ return _optionalChain([this, 'access', _97 => _97.getParsedPromptData, 'call', _98 => _98(), 'optionalAccess', _99 => _99.options]) || {};
8193
8374
  }
8194
8375
  get templateFormat() {
8195
- return _optionalChain([this, 'access', _95 => _95.getParsedPromptData, 'call', _96 => _96(), 'optionalAccess', _97 => _97.template_format]);
8376
+ return _optionalChain([this, 'access', _100 => _100.getParsedPromptData, 'call', _101 => _101(), 'optionalAccess', _102 => _102.template_format]);
8196
8377
  }
8197
8378
  get promptData() {
8198
8379
  return this.getParsedPromptData();
@@ -8352,7 +8533,7 @@ var Prompt2 = (_class14 = class _Prompt {
8352
8533
  return {
8353
8534
  type: "chat",
8354
8535
  messages,
8355
- ..._optionalChain([prompt, 'access', _98 => _98.tools, 'optionalAccess', _99 => _99.trim, 'call', _100 => _100()]) ? {
8536
+ ..._optionalChain([prompt, 'access', _103 => _103.tools, 'optionalAccess', _104 => _104.trim, 'call', _105 => _105()]) ? {
8356
8537
  tools: render(prompt.tools)
8357
8538
  } : void 0
8358
8539
  };
@@ -8396,12 +8577,12 @@ var Prompt2 = (_class14 = class _Prompt {
8396
8577
  false
8397
8578
  );
8398
8579
  }
8399
- }, _class14);
8400
- var RemoteEvalParameters = (_class15 = class {
8401
- constructor(metadata) {;_class15.prototype.__init59.call(this);
8580
+ }, _class15);
8581
+ var RemoteEvalParameters = (_class16 = class {
8582
+ constructor(metadata) {;_class16.prototype.__init61.call(this);
8402
8583
  this.metadata = metadata;
8403
8584
  }
8404
- __init59() {this.__braintrust_parameters_marker = true}
8585
+ __init61() {this.__braintrust_parameters_marker = true}
8405
8586
  get id() {
8406
8587
  return this.metadata.id;
8407
8588
  }
@@ -8444,12 +8625,9 @@ var RemoteEvalParameters = (_class15 = class {
8444
8625
  static isParameters(x) {
8445
8626
  return typeof x === "object" && x !== null && "__braintrust_parameters_marker" in x && x.__braintrust_parameters_marker === true;
8446
8627
  }
8447
- }, _class15);
8628
+ }, _class16);
8448
8629
  var TEST_API_KEY = "___TEST_API_KEY__THIS_IS_NOT_REAL___";
8449
8630
 
8450
- // src/instrumentation/core/plugin.ts
8451
- var _dcbrowser = require('dc-browser');
8452
-
8453
8631
  // src/instrumentation/core/stream-patcher.ts
8454
8632
  function isAsyncIterable(value) {
8455
8633
  return value !== null && typeof value === "object" && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
@@ -8465,7 +8643,7 @@ function patchStreamIfNeeded(stream, options) {
8465
8643
  return stream;
8466
8644
  }
8467
8645
  const originalIteratorFn = stream[Symbol.asyncIterator];
8468
- if (originalIteratorFn.__braintrust_patched) {
8646
+ if ("__braintrust_patched" in originalIteratorFn && originalIteratorFn["__braintrust_patched"]) {
8469
8647
  return stream;
8470
8648
  }
8471
8649
  try {
@@ -8506,7 +8684,10 @@ function patchStreamIfNeeded(stream, options) {
8506
8684
  completed = true;
8507
8685
  if (options.onError) {
8508
8686
  try {
8509
- options.onError(error, chunks);
8687
+ options.onError(
8688
+ error instanceof Error ? error : new Error(String(error)),
8689
+ chunks
8690
+ );
8510
8691
  } catch (handlerError) {
8511
8692
  console.error("Error in stream onError handler:", handlerError);
8512
8693
  }
@@ -8534,7 +8715,8 @@ function patchStreamIfNeeded(stream, options) {
8534
8715
  iterator.throw = async function(...args) {
8535
8716
  if (!completed) {
8536
8717
  completed = true;
8537
- const error = args[0];
8718
+ const rawError = args[0];
8719
+ const error = rawError instanceof Error ? rawError : new Error(String(rawError));
8538
8720
  if (options.onError) {
8539
8721
  try {
8540
8722
  options.onError(error, chunks);
@@ -8548,7 +8730,9 @@ function patchStreamIfNeeded(stream, options) {
8548
8730
  }
8549
8731
  return iterator;
8550
8732
  };
8551
- patchedIteratorFn.__braintrust_patched = true;
8733
+ Object.defineProperty(patchedIteratorFn, "__braintrust_patched", {
8734
+ value: true
8735
+ });
8552
8736
  stream[Symbol.asyncIterator] = patchedIteratorFn;
8553
8737
  return stream;
8554
8738
  } catch (error) {
@@ -8557,10 +8741,53 @@ function patchStreamIfNeeded(stream, options) {
8557
8741
  }
8558
8742
  }
8559
8743
 
8744
+ // src/instrumentation/core/channel-tracing-utils.ts
8745
+ function hasChannelSpanInfo(value) {
8746
+ return isObject(value) && isObject(value.span_info);
8747
+ }
8748
+ function getChannelSpanInfo(event) {
8749
+ if (isObject(event.span_info)) {
8750
+ return event.span_info;
8751
+ }
8752
+ const firstArg = _optionalChain([event, 'access', _106 => _106.arguments, 'optionalAccess', _107 => _107[0]]);
8753
+ if (hasChannelSpanInfo(firstArg)) {
8754
+ return firstArg.span_info;
8755
+ }
8756
+ return void 0;
8757
+ }
8758
+ function buildStartSpanArgs(config, event) {
8759
+ const spanInfo = getChannelSpanInfo(event);
8760
+ const spanAttributes = {
8761
+ type: config.type
8762
+ };
8763
+ if (isObject(_optionalChain([spanInfo, 'optionalAccess', _108 => _108.spanAttributes]))) {
8764
+ mergeDicts(spanAttributes, spanInfo.spanAttributes);
8765
+ }
8766
+ return {
8767
+ name: typeof _optionalChain([spanInfo, 'optionalAccess', _109 => _109.name]) === "string" && spanInfo.name ? spanInfo.name : config.name,
8768
+ spanAttributes,
8769
+ spanInfoMetadata: isObject(_optionalChain([spanInfo, 'optionalAccess', _110 => _110.metadata])) ? spanInfo.metadata : void 0
8770
+ };
8771
+ }
8772
+ function mergeInputMetadata(metadata, spanInfoMetadata) {
8773
+ if (!spanInfoMetadata) {
8774
+ return isObject(metadata) ? (
8775
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
8776
+ metadata
8777
+ ) : void 0;
8778
+ }
8779
+ const mergedMetadata = {};
8780
+ mergeDicts(mergedMetadata, spanInfoMetadata);
8781
+ if (isObject(metadata)) {
8782
+ mergeDicts(mergedMetadata, metadata);
8783
+ }
8784
+ return mergedMetadata;
8785
+ }
8786
+
8560
8787
  // src/instrumentation/core/plugin.ts
8561
- var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.call(this);_class16.prototype.__init61.call(this); }
8562
- __init60() {this.enabled = false}
8563
- __init61() {this.unsubscribers = []}
8788
+ var BasePlugin = (_class17 = class {constructor() { _class17.prototype.__init62.call(this);_class17.prototype.__init63.call(this); }
8789
+ __init62() {this.enabled = false}
8790
+ __init63() {this.unsubscribers = []}
8564
8791
  /**
8565
8792
  * Enables the plugin. Must be called before the plugin will receive events.
8566
8793
  */
@@ -8588,23 +8815,25 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8588
8815
  * @param handlers - Event handlers
8589
8816
  */
8590
8817
  subscribe(channelName, handlers) {
8591
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
8592
- channel.subscribe(handlers);
8818
+ const channel2 = isomorph_default.newTracingChannel(channelName);
8819
+ channel2.subscribe(handlers);
8593
8820
  }
8594
8821
  /**
8595
8822
  * Subscribe to a channel for async methods (non-streaming).
8596
8823
  * Creates a span and logs input/output/metrics.
8597
8824
  */
8598
8825
  subscribeToChannel(channelName, config) {
8599
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
8826
+ const channel2 = isomorph_default.newTracingChannel(channelName);
8600
8827
  const spans = /* @__PURE__ */ new WeakMap();
8601
8828
  const handlers = {
8602
8829
  start: (event) => {
8830
+ const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
8831
+ config,
8832
+ event
8833
+ );
8603
8834
  const span = startSpan({
8604
- name: config.name,
8605
- spanAttributes: {
8606
- type: config.type
8607
- }
8835
+ name,
8836
+ spanAttributes
8608
8837
  });
8609
8838
  const startTime = getCurrentUnixTimestamp();
8610
8839
  spans.set(event, { span, startTime });
@@ -8612,7 +8841,7 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8612
8841
  const { input, metadata } = config.extractInput(event.arguments);
8613
8842
  span.log({
8614
8843
  input,
8615
- metadata
8844
+ metadata: mergeInputMetadata(metadata, spanInfoMetadata)
8616
8845
  });
8617
8846
  } catch (error) {
8618
8847
  console.error(`Error extracting input for ${channelName}:`, error);
@@ -8625,10 +8854,12 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8625
8854
  }
8626
8855
  const { span, startTime } = spanData;
8627
8856
  try {
8628
- const output = config.extractOutput(event.result);
8629
- const metrics = config.extractMetrics(event.result, startTime);
8857
+ const output = config.extractOutput(event.result, event);
8858
+ const metrics = config.extractMetrics(event.result, startTime, event);
8859
+ const metadata = _optionalChain([config, 'access', _111 => _111.extractMetadata, 'optionalCall', _112 => _112(event.result, event)]);
8630
8860
  span.log({
8631
8861
  output,
8862
+ ...metadata !== void 0 ? { metadata } : {},
8632
8863
  metrics
8633
8864
  });
8634
8865
  } catch (error) {
@@ -8651,9 +8882,9 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8651
8882
  spans.delete(event);
8652
8883
  }
8653
8884
  };
8654
- channel.subscribe(handlers);
8885
+ channel2.subscribe(handlers);
8655
8886
  this.unsubscribers.push(() => {
8656
- channel.unsubscribe(handlers);
8887
+ channel2.unsubscribe(handlers);
8657
8888
  });
8658
8889
  }
8659
8890
  /**
@@ -8661,15 +8892,17 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8661
8892
  * Handles both streaming and non-streaming responses.
8662
8893
  */
8663
8894
  subscribeToStreamingChannel(channelName, config) {
8664
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
8895
+ const channel2 = isomorph_default.newTracingChannel(channelName);
8665
8896
  const spans = /* @__PURE__ */ new WeakMap();
8666
8897
  const handlers = {
8667
8898
  start: (event) => {
8899
+ const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
8900
+ config,
8901
+ event
8902
+ );
8668
8903
  const span = startSpan({
8669
- name: config.name,
8670
- spanAttributes: {
8671
- type: config.type
8672
- }
8904
+ name,
8905
+ spanAttributes
8673
8906
  });
8674
8907
  const startTime = getCurrentUnixTimestamp();
8675
8908
  spans.set(event, { span, startTime });
@@ -8677,7 +8910,7 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8677
8910
  const { input, metadata } = config.extractInput(event.arguments);
8678
8911
  span.log({
8679
8912
  input,
8680
- metadata
8913
+ metadata: mergeInputMetadata(metadata, spanInfoMetadata)
8681
8914
  });
8682
8915
  } catch (error) {
8683
8916
  console.error(`Error extracting input for ${channelName}:`, error);
@@ -8690,24 +8923,39 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8690
8923
  }
8691
8924
  const { span, startTime } = spanData;
8692
8925
  if (isAsyncIterable(event.result)) {
8926
+ let firstChunkTime;
8693
8927
  patchStreamIfNeeded(event.result, {
8928
+ onChunk: () => {
8929
+ if (firstChunkTime === void 0) {
8930
+ firstChunkTime = getCurrentUnixTimestamp();
8931
+ }
8932
+ },
8694
8933
  onComplete: (chunks) => {
8695
8934
  try {
8696
8935
  let output;
8697
8936
  let metrics;
8937
+ let metadata;
8698
8938
  if (config.aggregateChunks) {
8699
- const aggregated = config.aggregateChunks(chunks);
8939
+ const aggregated = config.aggregateChunks(
8940
+ chunks,
8941
+ event.result,
8942
+ event
8943
+ );
8700
8944
  output = aggregated.output;
8701
8945
  metrics = aggregated.metrics;
8946
+ metadata = aggregated.metadata;
8702
8947
  } else {
8703
- output = config.extractOutput(chunks);
8704
- metrics = config.extractMetrics(chunks, startTime);
8948
+ output = config.extractOutput(chunks, event);
8949
+ metrics = config.extractMetrics(chunks, startTime, event);
8705
8950
  }
8706
- if (!metrics.time_to_first_token && chunks.length > 0) {
8951
+ if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
8952
+ metrics.time_to_first_token = firstChunkTime - startTime;
8953
+ } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
8707
8954
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
8708
8955
  }
8709
8956
  span.log({
8710
8957
  output,
8958
+ ...metadata !== void 0 ? { metadata } : {},
8711
8959
  metrics
8712
8960
  });
8713
8961
  } catch (error) {
@@ -8728,10 +8976,16 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8728
8976
  });
8729
8977
  } else {
8730
8978
  try {
8731
- const output = config.extractOutput(event.result);
8732
- const metrics = config.extractMetrics(event.result, startTime);
8979
+ const output = config.extractOutput(event.result, event);
8980
+ const metadata = config.extractMetadata ? config.extractMetadata(event.result, event) : void 0;
8981
+ const metrics = config.extractMetrics(
8982
+ event.result,
8983
+ startTime,
8984
+ event
8985
+ );
8733
8986
  span.log({
8734
8987
  output,
8988
+ ...metadata !== void 0 ? { metadata } : {},
8735
8989
  metrics
8736
8990
  });
8737
8991
  } catch (error) {
@@ -8755,9 +9009,9 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8755
9009
  spans.delete(event);
8756
9010
  }
8757
9011
  };
8758
- channel.subscribe(handlers);
9012
+ channel2.subscribe(handlers);
8759
9013
  this.unsubscribers.push(() => {
8760
- channel.unsubscribe(handlers);
9014
+ channel2.unsubscribe(handlers);
8761
9015
  });
8762
9016
  }
8763
9017
  /**
@@ -8765,15 +9019,17 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8765
9019
  * Used for methods like beta.chat.completions.stream() and responses.stream().
8766
9020
  */
8767
9021
  subscribeToSyncStreamChannel(channelName, config) {
8768
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
9022
+ const channel2 = isomorph_default.newTracingChannel(channelName);
8769
9023
  const spans = /* @__PURE__ */ new WeakMap();
8770
9024
  const handlers = {
8771
9025
  start: (event) => {
9026
+ const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
9027
+ config,
9028
+ event
9029
+ );
8772
9030
  const span = startSpan({
8773
- name: config.name,
8774
- spanAttributes: {
8775
- type: config.type
8776
- }
9031
+ name,
9032
+ spanAttributes
8777
9033
  });
8778
9034
  const startTime = getCurrentUnixTimestamp();
8779
9035
  spans.set(event, { span, startTime });
@@ -8781,7 +9037,7 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8781
9037
  const { input, metadata } = config.extractInput(event.arguments);
8782
9038
  span.log({
8783
9039
  input,
8784
- metadata
9040
+ metadata: mergeInputMetadata(metadata, spanInfoMetadata)
8785
9041
  });
8786
9042
  } catch (error) {
8787
9043
  console.error(`Error extracting input for ${channelName}:`, error);
@@ -8865,156 +9121,642 @@ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.
8865
9121
  spans.delete(event);
8866
9122
  }
8867
9123
  };
8868
- channel.subscribe(handlers);
9124
+ channel2.subscribe(handlers);
8869
9125
  this.unsubscribers.push(() => {
8870
- channel.unsubscribe(handlers);
9126
+ channel2.unsubscribe(handlers);
8871
9127
  });
8872
9128
  }
8873
- }, _class16);
9129
+ }, _class17);
8874
9130
 
8875
- // src/wrappers/attachment-utils.ts
8876
- function getExtensionFromMediaType(mediaType) {
8877
- const extensionMap = {
8878
- "image/png": "png",
8879
- "image/jpeg": "jpg",
8880
- "image/gif": "gif",
8881
- "image/webp": "webp",
8882
- "image/svg+xml": "svg",
8883
- "audio/mpeg": "mp3",
8884
- "audio/wav": "wav",
8885
- "audio/ogg": "ogg",
8886
- "video/mp4": "mp4",
8887
- "video/webm": "webm",
8888
- "application/pdf": "pdf",
8889
- "application/json": "json",
8890
- "text/plain": "txt",
8891
- "text/html": "html",
8892
- "text/csv": "csv"
8893
- };
8894
- return extensionMap[mediaType] || "bin";
9131
+ // src/instrumentation/core/channel-tracing.ts
9132
+ function isSyncStreamLike(value) {
9133
+ return !!value && typeof value === "object" && typeof value.on === "function";
8895
9134
  }
8896
- function convertDataToBlob(data, mediaType) {
9135
+ function hasChoices(value) {
9136
+ return !!value && typeof value === "object" && "choices" in value;
9137
+ }
9138
+ function normalizeMetadata(metadata) {
9139
+ return isObject(metadata) ? metadata : void 0;
9140
+ }
9141
+ function startSpanForEvent(config, event, channelName) {
9142
+ const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
9143
+ config,
9144
+ event
9145
+ );
9146
+ const span = startSpan({
9147
+ name,
9148
+ spanAttributes
9149
+ });
9150
+ const startTime = getCurrentUnixTimestamp();
8897
9151
  try {
8898
- if (typeof data === "string") {
8899
- if (data.startsWith("data:")) {
8900
- const base64Match = data.match(/^data:[^;]+;base64,(.+)$/);
8901
- if (base64Match) {
8902
- const base64 = base64Match[1];
8903
- const binaryString = atob(base64);
8904
- const bytes = new Uint8Array(binaryString.length);
8905
- for (let i = 0; i < binaryString.length; i++) {
8906
- bytes[i] = binaryString.charCodeAt(i);
8907
- }
8908
- return new Blob([bytes], { type: mediaType });
8909
- }
8910
- } else if (data.startsWith("http://") || data.startsWith("https://")) {
8911
- return null;
8912
- } else {
8913
- const binaryString = atob(data);
8914
- const bytes = new Uint8Array(binaryString.length);
8915
- for (let i = 0; i < binaryString.length; i++) {
8916
- bytes[i] = binaryString.charCodeAt(i);
8917
- }
8918
- return new Blob([bytes], { type: mediaType });
8919
- }
8920
- } else if (data instanceof Uint8Array) {
8921
- return new Blob([data], { type: mediaType });
8922
- } else if (data instanceof ArrayBuffer) {
8923
- return new Blob([data], { type: mediaType });
8924
- } else if (typeof Buffer !== "undefined" && data instanceof Buffer) {
8925
- return new Blob([data], { type: mediaType });
8926
- }
8927
- } catch (e18) {
8928
- return null;
9152
+ const { input, metadata } = config.extractInput(event.arguments);
9153
+ span.log({
9154
+ input,
9155
+ metadata: mergeInputMetadata(metadata, spanInfoMetadata)
9156
+ });
9157
+ } catch (error) {
9158
+ console.error(`Error extracting input for ${channelName}:`, error);
8929
9159
  }
8930
- return null;
9160
+ return { span, startTime };
8931
9161
  }
8932
- function processInputAttachments(input) {
8933
- if (!input) {
8934
- return input;
9162
+ function logErrorAndEnd(states, event) {
9163
+ const spanData = states.get(event);
9164
+ if (!spanData) {
9165
+ return;
8935
9166
  }
8936
- let attachmentIndex = 0;
8937
- const processContentPart = (part) => {
8938
- if (!part || typeof part !== "object") {
8939
- return part;
8940
- }
8941
- if (part.type === "image" && part.image) {
8942
- let mediaType = "image/png";
8943
- if (typeof part.image === "string" && part.image.startsWith("data:")) {
8944
- const mediaTypeMatch = part.image.match(/^data:([^;]+);/);
8945
- if (mediaTypeMatch) {
8946
- mediaType = mediaTypeMatch[1];
8947
- }
8948
- } else if (part.mediaType) {
8949
- mediaType = part.mediaType;
8950
- }
8951
- const blob = convertDataToBlob(part.image, mediaType);
8952
- if (blob) {
8953
- const filename = `input_image_${attachmentIndex}.${getExtensionFromMediaType(mediaType)}`;
8954
- attachmentIndex++;
8955
- const attachment = new Attachment({
8956
- data: blob,
8957
- filename,
8958
- contentType: mediaType
8959
- });
8960
- return {
8961
- ...part,
8962
- image: attachment
8963
- };
9167
+ spanData.span.log({
9168
+ error: event.error.message
9169
+ });
9170
+ spanData.span.end();
9171
+ states.delete(event);
9172
+ }
9173
+ function traceAsyncChannel(channel2, config) {
9174
+ const tracingChannel2 = channel2.tracingChannel();
9175
+ const states = /* @__PURE__ */ new WeakMap();
9176
+ const channelName = channel2.channelName;
9177
+ const handlers = {
9178
+ start: (event) => {
9179
+ states.set(
9180
+ event,
9181
+ startSpanForEvent(
9182
+ config,
9183
+ event,
9184
+ channelName
9185
+ )
9186
+ );
9187
+ },
9188
+ asyncEnd: (event) => {
9189
+ const spanData = states.get(event);
9190
+ if (!spanData) {
9191
+ return;
8964
9192
  }
8965
- }
8966
- if (part.type === "file" && part.data) {
8967
- const mediaType = part.mediaType || "application/octet-stream";
8968
- const blob = convertDataToBlob(part.data, mediaType);
8969
- if (blob) {
8970
- const filename = part.filename || `input_file_${attachmentIndex}.${getExtensionFromMediaType(mediaType)}`;
8971
- attachmentIndex++;
8972
- const attachment = new Attachment({
8973
- data: blob,
8974
- filename,
8975
- contentType: mediaType
9193
+ const asyncEndEvent = event;
9194
+ const { span, startTime } = spanData;
9195
+ try {
9196
+ const output = config.extractOutput(
9197
+ asyncEndEvent.result,
9198
+ asyncEndEvent
9199
+ );
9200
+ const metrics = config.extractMetrics(
9201
+ asyncEndEvent.result,
9202
+ startTime,
9203
+ asyncEndEvent
9204
+ );
9205
+ const metadata = _optionalChain([config, 'access', _113 => _113.extractMetadata, 'optionalCall', _114 => _114(
9206
+ asyncEndEvent.result,
9207
+ asyncEndEvent
9208
+ )]);
9209
+ span.log({
9210
+ output,
9211
+ ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
9212
+ metrics
8976
9213
  });
8977
- return {
8978
- ...part,
8979
- data: attachment
8980
- };
9214
+ } catch (error) {
9215
+ console.error(`Error extracting output for ${channelName}:`, error);
9216
+ } finally {
9217
+ span.end();
9218
+ states.delete(event);
8981
9219
  }
9220
+ },
9221
+ error: (event) => {
9222
+ logErrorAndEnd(states, event);
8982
9223
  }
8983
- return part;
8984
9224
  };
8985
- const processMessage = (message) => {
8986
- if (!message || typeof message !== "object") {
8987
- return message;
8988
- }
8989
- if (Array.isArray(message.content)) {
8990
- return {
8991
- ...message,
8992
- content: message.content.map(processContentPart)
8993
- };
8994
- }
8995
- return message;
9225
+ tracingChannel2.subscribe(handlers);
9226
+ return () => {
9227
+ tracingChannel2.unsubscribe(handlers);
8996
9228
  };
8997
- if (Array.isArray(input)) {
8998
- return input.map(processMessage);
8999
- } else if (typeof input === "object" && input.content) {
9000
- return processMessage(input);
9001
- }
9002
- return input;
9003
9229
  }
9004
-
9005
- // src/instrumentation/plugins/openai-plugin.ts
9006
- var OpenAIPlugin = class extends BasePlugin {
9007
- constructor() {
9230
+ function traceStreamingChannel(channel2, config) {
9231
+ const tracingChannel2 = channel2.tracingChannel();
9232
+ const states = /* @__PURE__ */ new WeakMap();
9233
+ const channelName = channel2.channelName;
9234
+ const handlers = {
9235
+ start: (event) => {
9236
+ states.set(
9237
+ event,
9238
+ startSpanForEvent(
9239
+ config,
9240
+ event,
9241
+ channelName
9242
+ )
9243
+ );
9244
+ },
9245
+ asyncEnd: (event) => {
9246
+ const spanData = states.get(event);
9247
+ if (!spanData) {
9248
+ return;
9249
+ }
9250
+ const asyncEndEvent = event;
9251
+ const { span, startTime } = spanData;
9252
+ if (isAsyncIterable(asyncEndEvent.result)) {
9253
+ let firstChunkTime;
9254
+ patchStreamIfNeeded(asyncEndEvent.result, {
9255
+ onChunk: () => {
9256
+ if (firstChunkTime === void 0) {
9257
+ firstChunkTime = getCurrentUnixTimestamp();
9258
+ }
9259
+ },
9260
+ onComplete: (chunks) => {
9261
+ try {
9262
+ let output;
9263
+ let metrics;
9264
+ let metadata;
9265
+ if (config.aggregateChunks) {
9266
+ const aggregated = config.aggregateChunks(
9267
+ chunks,
9268
+ asyncEndEvent.result,
9269
+ asyncEndEvent,
9270
+ startTime
9271
+ );
9272
+ output = aggregated.output;
9273
+ metrics = aggregated.metrics;
9274
+ metadata = aggregated.metadata;
9275
+ } else {
9276
+ output = config.extractOutput(
9277
+ chunks,
9278
+ asyncEndEvent
9279
+ );
9280
+ metrics = config.extractMetrics(
9281
+ chunks,
9282
+ startTime,
9283
+ asyncEndEvent
9284
+ );
9285
+ }
9286
+ if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
9287
+ metrics.time_to_first_token = firstChunkTime - startTime;
9288
+ } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
9289
+ metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9290
+ }
9291
+ span.log({
9292
+ output,
9293
+ ...metadata !== void 0 ? { metadata } : {},
9294
+ metrics
9295
+ });
9296
+ } catch (error) {
9297
+ console.error(
9298
+ `Error extracting output for ${channelName}:`,
9299
+ error
9300
+ );
9301
+ } finally {
9302
+ span.end();
9303
+ states.delete(event);
9304
+ }
9305
+ },
9306
+ onError: (error) => {
9307
+ span.log({
9308
+ error: error.message
9309
+ });
9310
+ span.end();
9311
+ states.delete(event);
9312
+ }
9313
+ });
9314
+ return;
9315
+ }
9316
+ try {
9317
+ const output = config.extractOutput(
9318
+ asyncEndEvent.result,
9319
+ asyncEndEvent
9320
+ );
9321
+ const metrics = config.extractMetrics(
9322
+ asyncEndEvent.result,
9323
+ startTime,
9324
+ asyncEndEvent
9325
+ );
9326
+ const metadata = _optionalChain([config, 'access', _115 => _115.extractMetadata, 'optionalCall', _116 => _116(
9327
+ asyncEndEvent.result,
9328
+ asyncEndEvent
9329
+ )]);
9330
+ span.log({
9331
+ output,
9332
+ ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
9333
+ metrics
9334
+ });
9335
+ } catch (error) {
9336
+ console.error(`Error extracting output for ${channelName}:`, error);
9337
+ } finally {
9338
+ span.end();
9339
+ states.delete(event);
9340
+ }
9341
+ },
9342
+ error: (event) => {
9343
+ logErrorAndEnd(states, event);
9344
+ }
9345
+ };
9346
+ tracingChannel2.subscribe(handlers);
9347
+ return () => {
9348
+ tracingChannel2.unsubscribe(handlers);
9349
+ };
9350
+ }
9351
+ function traceSyncStreamChannel(channel2, config) {
9352
+ const tracingChannel2 = channel2.tracingChannel();
9353
+ const states = /* @__PURE__ */ new WeakMap();
9354
+ const channelName = channel2.channelName;
9355
+ const handlers = {
9356
+ start: (event) => {
9357
+ states.set(
9358
+ event,
9359
+ startSpanForEvent(
9360
+ config,
9361
+ event,
9362
+ channelName
9363
+ )
9364
+ );
9365
+ },
9366
+ end: (event) => {
9367
+ const spanData = states.get(event);
9368
+ if (!spanData) {
9369
+ return;
9370
+ }
9371
+ const { span, startTime } = spanData;
9372
+ const resultEvent = event;
9373
+ const stream = resultEvent.result;
9374
+ if (!isSyncStreamLike(stream)) {
9375
+ span.end();
9376
+ states.delete(event);
9377
+ return;
9378
+ }
9379
+ let first = true;
9380
+ stream.on("chunk", () => {
9381
+ if (first) {
9382
+ span.log({
9383
+ metrics: {
9384
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
9385
+ }
9386
+ });
9387
+ first = false;
9388
+ }
9389
+ });
9390
+ stream.on("chatCompletion", (completion) => {
9391
+ try {
9392
+ if (hasChoices(completion)) {
9393
+ span.log({
9394
+ output: completion.choices
9395
+ });
9396
+ }
9397
+ } catch (error) {
9398
+ console.error(
9399
+ `Error extracting chatCompletion for ${channelName}:`,
9400
+ error
9401
+ );
9402
+ }
9403
+ });
9404
+ stream.on("event", (streamEvent) => {
9405
+ if (!config.extractFromEvent) {
9406
+ return;
9407
+ }
9408
+ try {
9409
+ if (first) {
9410
+ span.log({
9411
+ metrics: {
9412
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
9413
+ }
9414
+ });
9415
+ first = false;
9416
+ }
9417
+ const extracted = config.extractFromEvent(streamEvent);
9418
+ if (extracted && Object.keys(extracted).length > 0) {
9419
+ span.log(extracted);
9420
+ }
9421
+ } catch (error) {
9422
+ console.error(`Error extracting event for ${channelName}:`, error);
9423
+ }
9424
+ });
9425
+ stream.on("end", () => {
9426
+ span.end();
9427
+ states.delete(event);
9428
+ });
9429
+ stream.on("error", (error) => {
9430
+ span.log({
9431
+ error: error.message
9432
+ });
9433
+ span.end();
9434
+ states.delete(event);
9435
+ });
9436
+ },
9437
+ error: (event) => {
9438
+ logErrorAndEnd(states, event);
9439
+ }
9440
+ };
9441
+ tracingChannel2.subscribe(handlers);
9442
+ return () => {
9443
+ tracingChannel2.unsubscribe(handlers);
9444
+ };
9445
+ }
9446
+ function unsubscribeAll(unsubscribers) {
9447
+ for (const unsubscribe of unsubscribers) {
9448
+ unsubscribe();
9449
+ }
9450
+ return [];
9451
+ }
9452
+
9453
+ // src/wrappers/attachment-utils.ts
9454
+ function getExtensionFromMediaType(mediaType) {
9455
+ const extensionMap = {
9456
+ "image/png": "png",
9457
+ "image/jpeg": "jpg",
9458
+ "image/gif": "gif",
9459
+ "image/webp": "webp",
9460
+ "image/svg+xml": "svg",
9461
+ "audio/mpeg": "mp3",
9462
+ "audio/wav": "wav",
9463
+ "audio/ogg": "ogg",
9464
+ "video/mp4": "mp4",
9465
+ "video/webm": "webm",
9466
+ "application/pdf": "pdf",
9467
+ "application/json": "json",
9468
+ "text/plain": "txt",
9469
+ "text/html": "html",
9470
+ "text/csv": "csv"
9471
+ };
9472
+ return extensionMap[mediaType] || "bin";
9473
+ }
9474
+ function convertDataToBlob(data, mediaType) {
9475
+ try {
9476
+ if (typeof data === "string") {
9477
+ if (data.startsWith("data:")) {
9478
+ const base64Match = data.match(/^data:[^;]+;base64,(.+)$/);
9479
+ if (base64Match) {
9480
+ const base64 = base64Match[1];
9481
+ const binaryString = atob(base64);
9482
+ const bytes = new Uint8Array(binaryString.length);
9483
+ for (let i = 0; i < binaryString.length; i++) {
9484
+ bytes[i] = binaryString.charCodeAt(i);
9485
+ }
9486
+ return new Blob([bytes], { type: mediaType });
9487
+ }
9488
+ } else if (data.startsWith("http://") || data.startsWith("https://")) {
9489
+ return null;
9490
+ } else {
9491
+ const binaryString = atob(data);
9492
+ const bytes = new Uint8Array(binaryString.length);
9493
+ for (let i = 0; i < binaryString.length; i++) {
9494
+ bytes[i] = binaryString.charCodeAt(i);
9495
+ }
9496
+ return new Blob([bytes], { type: mediaType });
9497
+ }
9498
+ } else if (data instanceof Uint8Array) {
9499
+ return new Blob([data], { type: mediaType });
9500
+ } else if (data instanceof ArrayBuffer) {
9501
+ return new Blob([data], { type: mediaType });
9502
+ } else if (typeof Buffer !== "undefined" && data instanceof Buffer) {
9503
+ return new Blob([data], { type: mediaType });
9504
+ }
9505
+ } catch (e18) {
9506
+ return null;
9507
+ }
9508
+ return null;
9509
+ }
9510
+ function processInputAttachments(input) {
9511
+ if (!input) {
9512
+ return input;
9513
+ }
9514
+ let attachmentIndex = 0;
9515
+ const inferMediaTypeFromDataUrl = (value, fallback2) => {
9516
+ const mediaTypeMatch = value.match(/^data:([^;]+);/);
9517
+ return _optionalChain([mediaTypeMatch, 'optionalAccess', _117 => _117[1]]) || fallback2;
9518
+ };
9519
+ const toAttachment = (value, mediaType, filename) => {
9520
+ const blob = convertDataToBlob(value, mediaType);
9521
+ if (!blob) {
9522
+ return null;
9523
+ }
9524
+ return new Attachment({
9525
+ data: blob,
9526
+ filename,
9527
+ contentType: mediaType
9528
+ });
9529
+ };
9530
+ const processNode = (node) => {
9531
+ if (Array.isArray(node)) {
9532
+ return node.map(processNode);
9533
+ }
9534
+ if (!node || typeof node !== "object") {
9535
+ return node;
9536
+ }
9537
+ if (node.type === "image_url" && node.image_url && typeof node.image_url === "object" && typeof node.image_url.url === "string" && node.image_url.url.startsWith("data:")) {
9538
+ const mediaType = inferMediaTypeFromDataUrl(
9539
+ node.image_url.url,
9540
+ "image/png"
9541
+ );
9542
+ const filename = `image.${getExtensionFromMediaType(mediaType)}`;
9543
+ const attachment = toAttachment(node.image_url.url, mediaType, filename);
9544
+ if (attachment) {
9545
+ return {
9546
+ ...node,
9547
+ image_url: {
9548
+ ...node.image_url,
9549
+ url: attachment
9550
+ }
9551
+ };
9552
+ }
9553
+ }
9554
+ if (node.type === "file" && node.file && typeof node.file === "object" && typeof node.file.file_data === "string" && node.file.file_data.startsWith("data:")) {
9555
+ const mediaType = inferMediaTypeFromDataUrl(
9556
+ node.file.file_data,
9557
+ "application/octet-stream"
9558
+ );
9559
+ const filename = typeof node.file.filename === "string" && node.file.filename ? node.file.filename : `document.${getExtensionFromMediaType(mediaType)}`;
9560
+ const attachment = toAttachment(node.file.file_data, mediaType, filename);
9561
+ if (attachment) {
9562
+ return {
9563
+ ...node,
9564
+ file: {
9565
+ ...node.file,
9566
+ file_data: attachment
9567
+ }
9568
+ };
9569
+ }
9570
+ }
9571
+ if (node.type === "image" && node.image) {
9572
+ let mediaType = "image/png";
9573
+ if (typeof node.image === "string" && node.image.startsWith("data:")) {
9574
+ mediaType = inferMediaTypeFromDataUrl(node.image, mediaType);
9575
+ } else if (node.mediaType) {
9576
+ mediaType = node.mediaType;
9577
+ }
9578
+ const filename = `input_image_${attachmentIndex}.${getExtensionFromMediaType(mediaType)}`;
9579
+ const attachment = toAttachment(node.image, mediaType, filename);
9580
+ if (attachment) {
9581
+ attachmentIndex++;
9582
+ return {
9583
+ ...node,
9584
+ image: attachment
9585
+ };
9586
+ }
9587
+ }
9588
+ if (node.type === "file" && node.data) {
9589
+ const mediaType = node.mediaType || "application/octet-stream";
9590
+ const filename = node.filename || `input_file_${attachmentIndex}.${getExtensionFromMediaType(mediaType)}`;
9591
+ const attachment = toAttachment(node.data, mediaType, filename);
9592
+ if (attachment) {
9593
+ attachmentIndex++;
9594
+ return {
9595
+ ...node,
9596
+ data: attachment
9597
+ };
9598
+ }
9599
+ }
9600
+ const processed = {};
9601
+ for (const [key, value] of Object.entries(node)) {
9602
+ processed[key] = processNode(value);
9603
+ }
9604
+ return processed;
9605
+ };
9606
+ if (Array.isArray(input)) {
9607
+ return input.map(processNode);
9608
+ }
9609
+ return processNode(input);
9610
+ }
9611
+
9612
+ // src/instrumentation/core/channel-definitions.ts
9613
+ function channel(spec) {
9614
+ return spec;
9615
+ }
9616
+ function defineChannels(pkg, channels) {
9617
+ return Object.fromEntries(
9618
+ Object.entries(channels).map(([key, spec]) => {
9619
+ const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
9620
+ if (spec.kind === "async") {
9621
+ const asyncSpec = spec;
9622
+ const tracingChannel3 = () => isomorph_default.newTracingChannel(
9623
+ fullChannelName
9624
+ );
9625
+ return [
9626
+ key,
9627
+ {
9628
+ ...asyncSpec,
9629
+ tracingChannel: tracingChannel3,
9630
+ tracePromise: (fn, context) => tracingChannel3().tracePromise(
9631
+ fn,
9632
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
9633
+ context
9634
+ )
9635
+ }
9636
+ ];
9637
+ }
9638
+ const syncSpec = spec;
9639
+ const tracingChannel2 = () => isomorph_default.newTracingChannel(
9640
+ fullChannelName
9641
+ );
9642
+ return [
9643
+ key,
9644
+ {
9645
+ ...syncSpec,
9646
+ tracingChannel: tracingChannel2,
9647
+ traceSync: (fn, context) => tracingChannel2().traceSync(
9648
+ fn,
9649
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
9650
+ context
9651
+ )
9652
+ }
9653
+ ];
9654
+ })
9655
+ );
9656
+ }
9657
+
9658
+ // src/instrumentation/plugins/openai-channels.ts
9659
+ var openAIChannels = defineChannels("openai", {
9660
+ chatCompletionsCreate: channel({
9661
+ channelName: "chat.completions.create",
9662
+ kind: "async"
9663
+ }),
9664
+ embeddingsCreate: channel({
9665
+ channelName: "embeddings.create",
9666
+ kind: "async"
9667
+ }),
9668
+ betaChatCompletionsParse: channel({
9669
+ channelName: "beta.chat.completions.parse",
9670
+ kind: "async"
9671
+ }),
9672
+ betaChatCompletionsStream: channel({
9673
+ channelName: "beta.chat.completions.stream",
9674
+ kind: "sync-stream"
9675
+ }),
9676
+ moderationsCreate: channel({
9677
+ channelName: "moderations.create",
9678
+ kind: "async"
9679
+ }),
9680
+ responsesCreate: channel({
9681
+ channelName: "responses.create",
9682
+ kind: "async"
9683
+ }),
9684
+ responsesStream: channel({
9685
+ channelName: "responses.stream",
9686
+ kind: "sync-stream"
9687
+ }),
9688
+ responsesParse: channel({
9689
+ channelName: "responses.parse",
9690
+ kind: "async"
9691
+ })
9692
+ });
9693
+
9694
+ // src/openai-utils.ts
9695
+ var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
9696
+ var LEGACY_CACHED_HEADER = "x-cached";
9697
+ var X_CACHED_HEADER = "x-bt-cached";
9698
+ var TOKEN_NAME_MAP = {
9699
+ input_tokens: "prompt_tokens",
9700
+ output_tokens: "completion_tokens",
9701
+ total_tokens: "tokens"
9702
+ };
9703
+ var TOKEN_PREFIX_MAP = {
9704
+ input: "prompt",
9705
+ output: "completion"
9706
+ };
9707
+ function parseMetricsFromUsage(usage) {
9708
+ if (!usage) {
9709
+ return {};
9710
+ }
9711
+ const metrics = {};
9712
+ for (const [oaiName, value] of Object.entries(usage)) {
9713
+ if (typeof value === "number") {
9714
+ const metricName = TOKEN_NAME_MAP[oaiName] || oaiName;
9715
+ metrics[metricName] = value;
9716
+ continue;
9717
+ }
9718
+ if (!oaiName.endsWith("_tokens_details") || !isObject(value)) {
9719
+ continue;
9720
+ }
9721
+ const rawPrefix = oaiName.slice(0, -"_tokens_details".length);
9722
+ const prefix = TOKEN_PREFIX_MAP[rawPrefix] || rawPrefix;
9723
+ for (const [key, nestedValue] of Object.entries(value)) {
9724
+ if (typeof nestedValue !== "number") {
9725
+ continue;
9726
+ }
9727
+ metrics[`${prefix}_${key}`] = nestedValue;
9728
+ }
9729
+ }
9730
+ return metrics;
9731
+ }
9732
+ function parseCachedHeader(value) {
9733
+ if (!value) {
9734
+ return void 0;
9735
+ }
9736
+ return ["true", "hit"].includes(value.toLowerCase()) ? 1 : 0;
9737
+ }
9738
+ function getCachedMetricFromHeaders(headers) {
9739
+ if (!headers || typeof headers.get !== "function") {
9740
+ return void 0;
9741
+ }
9742
+ const cachedHeader = headers.get(X_CACHED_HEADER);
9743
+ if (cachedHeader) {
9744
+ return parseCachedHeader(cachedHeader);
9745
+ }
9746
+ return parseCachedHeader(headers.get(LEGACY_CACHED_HEADER));
9747
+ }
9748
+
9749
+ // src/instrumentation/plugins/openai-plugin.ts
9750
+ var OpenAIPlugin = class extends BasePlugin {
9751
+ constructor() {
9008
9752
  super();
9009
9753
  }
9010
9754
  onEnable() {
9011
- this.subscribeToStreamingChannel(
9012
- "orchestrion:openai:chat.completions.create",
9013
- {
9755
+ this.unsubscribers.push(
9756
+ traceStreamingChannel(openAIChannels.chatCompletionsCreate, {
9014
9757
  name: "Chat Completion",
9015
9758
  type: "llm" /* LLM */,
9016
- extractInput: (args) => {
9017
- const params = args[0] || {};
9759
+ extractInput: ([params]) => {
9018
9760
  const { messages, ...metadata } = params;
9019
9761
  return {
9020
9762
  input: processInputAttachments(messages),
@@ -9022,43 +9764,51 @@ var OpenAIPlugin = class extends BasePlugin {
9022
9764
  };
9023
9765
  },
9024
9766
  extractOutput: (result) => {
9025
- return _optionalChain([result, 'optionalAccess', _101 => _101.choices]);
9767
+ return _optionalChain([result, 'optionalAccess', _118 => _118.choices]);
9026
9768
  },
9027
- extractMetrics: (result, startTime) => {
9028
- const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _102 => _102.usage]));
9769
+ extractMetrics: (result, startTime, endEvent) => {
9770
+ const metrics = withCachedMetric(
9771
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _119 => _119.usage])),
9772
+ result,
9773
+ endEvent
9774
+ );
9029
9775
  if (startTime) {
9030
9776
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9031
9777
  }
9032
9778
  return metrics;
9033
9779
  },
9034
9780
  aggregateChunks: aggregateChatCompletionChunks
9035
- }
9781
+ })
9036
9782
  );
9037
- this.subscribeToChannel("orchestrion:openai:embeddings.create", {
9038
- name: "Embedding",
9039
- type: "llm" /* LLM */,
9040
- extractInput: (args) => {
9041
- const params = args[0] || {};
9042
- const { input, ...metadata } = params;
9043
- return {
9044
- input,
9045
- metadata: { ...metadata, provider: "openai" }
9046
- };
9047
- },
9048
- extractOutput: (result) => {
9049
- return _optionalChain([result, 'optionalAccess', _103 => _103.data, 'optionalAccess', _104 => _104.map, 'call', _105 => _105((d) => d.embedding)]);
9050
- },
9051
- extractMetrics: (result) => {
9052
- return parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _106 => _106.usage]));
9053
- }
9054
- });
9055
- this.subscribeToStreamingChannel(
9056
- "orchestrion:openai:beta.chat.completions.parse",
9057
- {
9783
+ this.unsubscribers.push(
9784
+ traceAsyncChannel(openAIChannels.embeddingsCreate, {
9785
+ name: "Embedding",
9786
+ type: "llm" /* LLM */,
9787
+ extractInput: ([params]) => {
9788
+ const { input, ...metadata } = params;
9789
+ return {
9790
+ input,
9791
+ metadata: { ...metadata, provider: "openai" }
9792
+ };
9793
+ },
9794
+ extractOutput: (result) => {
9795
+ const embedding = _optionalChain([result, 'optionalAccess', _120 => _120.data, 'optionalAccess', _121 => _121[0], 'optionalAccess', _122 => _122.embedding]);
9796
+ return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
9797
+ },
9798
+ extractMetrics: (result, _startTime, endEvent) => {
9799
+ return withCachedMetric(
9800
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _123 => _123.usage])),
9801
+ result,
9802
+ endEvent
9803
+ );
9804
+ }
9805
+ })
9806
+ );
9807
+ this.unsubscribers.push(
9808
+ traceStreamingChannel(openAIChannels.betaChatCompletionsParse, {
9058
9809
  name: "Chat Completion",
9059
9810
  type: "llm" /* LLM */,
9060
- extractInput: (args) => {
9061
- const params = args[0] || {};
9811
+ extractInput: ([params]) => {
9062
9812
  const { messages, ...metadata } = params;
9063
9813
  return {
9064
9814
  input: processInputAttachments(messages),
@@ -9066,166 +9816,198 @@ var OpenAIPlugin = class extends BasePlugin {
9066
9816
  };
9067
9817
  },
9068
9818
  extractOutput: (result) => {
9069
- return _optionalChain([result, 'optionalAccess', _107 => _107.choices]);
9819
+ return _optionalChain([result, 'optionalAccess', _124 => _124.choices]);
9820
+ },
9821
+ extractMetrics: (result, startTime, endEvent) => {
9822
+ const metrics = withCachedMetric(
9823
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _125 => _125.usage])),
9824
+ result,
9825
+ endEvent
9826
+ );
9827
+ if (startTime) {
9828
+ metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9829
+ }
9830
+ return metrics;
9831
+ },
9832
+ aggregateChunks: aggregateChatCompletionChunks
9833
+ })
9834
+ );
9835
+ this.unsubscribers.push(
9836
+ traceSyncStreamChannel(openAIChannels.betaChatCompletionsStream, {
9837
+ name: "Chat Completion",
9838
+ type: "llm" /* LLM */,
9839
+ extractInput: ([params]) => {
9840
+ const { messages, ...metadata } = params;
9841
+ return {
9842
+ input: processInputAttachments(messages),
9843
+ metadata: { ...metadata, provider: "openai" }
9844
+ };
9845
+ }
9846
+ })
9847
+ );
9848
+ this.unsubscribers.push(
9849
+ traceAsyncChannel(openAIChannels.moderationsCreate, {
9850
+ name: "Moderation",
9851
+ type: "llm" /* LLM */,
9852
+ extractInput: ([params]) => {
9853
+ const { input, ...metadata } = params;
9854
+ return {
9855
+ input,
9856
+ metadata: { ...metadata, provider: "openai" }
9857
+ };
9858
+ },
9859
+ extractOutput: (result) => {
9860
+ return _optionalChain([result, 'optionalAccess', _126 => _126.results]);
9861
+ },
9862
+ extractMetrics: (result, _startTime, endEvent) => {
9863
+ return withCachedMetric(
9864
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _127 => _127.usage])),
9865
+ result,
9866
+ endEvent
9867
+ );
9868
+ }
9869
+ })
9870
+ );
9871
+ this.unsubscribers.push(
9872
+ traceStreamingChannel(openAIChannels.responsesCreate, {
9873
+ name: "openai.responses.create",
9874
+ type: "llm" /* LLM */,
9875
+ extractInput: ([params]) => {
9876
+ const { input, ...metadata } = params;
9877
+ return {
9878
+ input: processInputAttachments(input),
9879
+ metadata: { ...metadata, provider: "openai" }
9880
+ };
9881
+ },
9882
+ extractOutput: (result) => {
9883
+ return processImagesInOutput(_optionalChain([result, 'optionalAccess', _128 => _128.output]));
9884
+ },
9885
+ extractMetadata: (result) => {
9886
+ if (!result) {
9887
+ return void 0;
9888
+ }
9889
+ const { output: _output, usage: _usage, ...metadata } = result;
9890
+ return Object.keys(metadata).length > 0 ? metadata : void 0;
9070
9891
  },
9071
- extractMetrics: (result, startTime) => {
9072
- const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _108 => _108.usage]));
9892
+ extractMetrics: (result, startTime, endEvent) => {
9893
+ const metrics = withCachedMetric(
9894
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _129 => _129.usage])),
9895
+ result,
9896
+ endEvent
9897
+ );
9073
9898
  if (startTime) {
9074
9899
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9075
9900
  }
9076
9901
  return metrics;
9077
9902
  },
9078
- aggregateChunks: aggregateChatCompletionChunks
9079
- }
9903
+ aggregateChunks: aggregateResponseStreamEvents
9904
+ })
9080
9905
  );
9081
- this.subscribeToSyncStreamChannel(
9082
- "orchestrion:openai:beta.chat.completions.stream",
9083
- {
9084
- name: "Chat Completion",
9906
+ this.unsubscribers.push(
9907
+ traceSyncStreamChannel(openAIChannels.responsesStream, {
9908
+ name: "openai.responses.create",
9085
9909
  type: "llm" /* LLM */,
9086
- extractInput: (args) => {
9087
- const params = args[0] || {};
9088
- const { messages, ...metadata } = params;
9910
+ extractInput: ([params]) => {
9911
+ const { input, ...metadata } = params;
9089
9912
  return {
9090
- input: processInputAttachments(messages),
9913
+ input: processInputAttachments(input),
9091
9914
  metadata: { ...metadata, provider: "openai" }
9092
9915
  };
9093
- }
9094
- }
9095
- );
9096
- this.subscribeToChannel("orchestrion:openai:moderations.create", {
9097
- name: "Moderation",
9098
- type: "llm" /* LLM */,
9099
- extractInput: (args) => {
9100
- const params = args[0] || {};
9101
- const { input, ...metadata } = params;
9102
- return {
9103
- input,
9104
- metadata: { ...metadata, provider: "openai" }
9105
- };
9106
- },
9107
- extractOutput: (result) => {
9108
- return _optionalChain([result, 'optionalAccess', _109 => _109.results]);
9109
- },
9110
- extractMetrics: () => {
9111
- return {};
9112
- }
9113
- });
9114
- this.subscribeToStreamingChannel("orchestrion:openai:responses.create", {
9115
- name: "openai.responses.create",
9116
- type: "llm" /* LLM */,
9117
- extractInput: (args) => {
9118
- const params = args[0] || {};
9119
- const { input, ...metadata } = params;
9120
- return {
9121
- input: processInputAttachments(input),
9122
- metadata: { ...metadata, provider: "openai" }
9123
- };
9124
- },
9125
- extractOutput: (result) => {
9126
- return processImagesInOutput(_optionalChain([result, 'optionalAccess', _110 => _110.output]));
9127
- },
9128
- extractMetrics: (result, startTime) => {
9129
- const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _111 => _111.usage]));
9130
- if (startTime) {
9131
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9132
- }
9133
- return metrics;
9134
- }
9135
- });
9136
- this.subscribeToSyncStreamChannel("orchestrion:openai:responses.stream", {
9137
- name: "openai.responses.stream",
9138
- type: "llm" /* LLM */,
9139
- extractInput: (args) => {
9140
- const params = args[0] || {};
9141
- const { input, ...metadata } = params;
9142
- return {
9143
- input: processInputAttachments(input),
9144
- metadata: { ...metadata, provider: "openai" }
9145
- };
9146
- },
9147
- extractFromEvent: (event) => {
9148
- if (!event || !event.type || !event.response) {
9149
- return {};
9150
- }
9151
- const response = event.response;
9152
- if (event.type === "response.completed") {
9916
+ },
9917
+ extractFromEvent: (event) => {
9918
+ if (event.type !== "response.completed" || !event.response) {
9919
+ return {};
9920
+ }
9921
+ const response = event.response;
9153
9922
  const data = {};
9154
- if (_optionalChain([response, 'optionalAccess', _112 => _112.output]) !== void 0) {
9923
+ if (response.output !== void 0) {
9155
9924
  data.output = processImagesInOutput(response.output);
9156
9925
  }
9157
- if (response) {
9158
- const { usage: _usage, output: _output, ...metadata } = response;
9159
- if (Object.keys(metadata).length > 0) {
9160
- data.metadata = metadata;
9161
- }
9926
+ const { usage: _usage, output: _output, ...metadata } = response;
9927
+ if (Object.keys(metadata).length > 0) {
9928
+ data.metadata = metadata;
9162
9929
  }
9163
- data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _113 => _113.usage]));
9930
+ data.metrics = parseMetricsFromUsage(response.usage);
9164
9931
  return data;
9165
9932
  }
9166
- return {};
9167
- }
9168
- });
9169
- this.subscribeToStreamingChannel("orchestrion:openai:responses.parse", {
9170
- name: "openai.responses.parse",
9171
- type: "llm" /* LLM */,
9172
- extractInput: (args) => {
9173
- const params = args[0] || {};
9174
- const { input, ...metadata } = params;
9175
- return {
9176
- input: processInputAttachments(input),
9177
- metadata: { ...metadata, provider: "openai" }
9178
- };
9179
- },
9180
- extractOutput: (result) => {
9181
- return processImagesInOutput(_optionalChain([result, 'optionalAccess', _114 => _114.output]));
9182
- },
9183
- extractMetrics: (result, startTime) => {
9184
- const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _115 => _115.usage]));
9185
- if (startTime) {
9186
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9187
- }
9188
- return metrics;
9189
- }
9190
- });
9933
+ })
9934
+ );
9935
+ this.unsubscribers.push(
9936
+ traceStreamingChannel(openAIChannels.responsesParse, {
9937
+ name: "openai.responses.parse",
9938
+ type: "llm" /* LLM */,
9939
+ extractInput: ([params]) => {
9940
+ const { input, ...metadata } = params;
9941
+ return {
9942
+ input: processInputAttachments(input),
9943
+ metadata: { ...metadata, provider: "openai" }
9944
+ };
9945
+ },
9946
+ extractOutput: (result) => {
9947
+ return processImagesInOutput(_optionalChain([result, 'optionalAccess', _130 => _130.output]));
9948
+ },
9949
+ extractMetadata: (result) => {
9950
+ if (!result) {
9951
+ return void 0;
9952
+ }
9953
+ const { output: _output, usage: _usage, ...metadata } = result;
9954
+ return Object.keys(metadata).length > 0 ? metadata : void 0;
9955
+ },
9956
+ extractMetrics: (result, startTime, endEvent) => {
9957
+ const metrics = withCachedMetric(
9958
+ parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _131 => _131.usage])),
9959
+ result,
9960
+ endEvent
9961
+ );
9962
+ if (startTime) {
9963
+ metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9964
+ }
9965
+ return metrics;
9966
+ },
9967
+ aggregateChunks: aggregateResponseStreamEvents
9968
+ })
9969
+ );
9191
9970
  }
9192
9971
  onDisable() {
9972
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
9193
9973
  }
9194
9974
  };
9195
- var TOKEN_NAME_MAP = {
9196
- input_tokens: "prompt_tokens",
9197
- output_tokens: "completion_tokens",
9198
- total_tokens: "tokens"
9199
- };
9200
- var TOKEN_PREFIX_MAP = {
9201
- input: "prompt",
9202
- output: "completion"
9203
- };
9204
- function parseMetricsFromUsage(usage) {
9205
- if (!usage) {
9206
- return {};
9975
+ function getCachedMetricFromEndEvent(endEvent) {
9976
+ if (!isObject(endEvent)) {
9977
+ return void 0;
9207
9978
  }
9208
- const metrics = {};
9209
- for (const [oai_name, value] of Object.entries(usage)) {
9210
- if (typeof value === "number") {
9211
- const metricName = TOKEN_NAME_MAP[oai_name] || oai_name;
9212
- metrics[metricName] = value;
9213
- } else if (oai_name.endsWith("_tokens_details")) {
9214
- if (!isObject(value)) {
9215
- continue;
9216
- }
9217
- const rawPrefix = oai_name.slice(0, -"_tokens_details".length);
9218
- const prefix = TOKEN_PREFIX_MAP[rawPrefix] || rawPrefix;
9219
- for (const [key, n] of Object.entries(value)) {
9220
- if (typeof n !== "number") {
9221
- continue;
9222
- }
9223
- const metricName = `${prefix}_${key}`;
9224
- metrics[metricName] = n;
9225
- }
9226
- }
9979
+ const response = endEvent.response;
9980
+ if (!isObject(response)) {
9981
+ return void 0;
9227
9982
  }
9228
- return metrics;
9983
+ const headers = response.headers;
9984
+ if (!headers || typeof headers.get !== "function") {
9985
+ return void 0;
9986
+ }
9987
+ return getCachedMetricFromHeaders(headers);
9988
+ }
9989
+ function withCachedMetric(metrics, result, endEvent) {
9990
+ if (metrics.cached !== void 0) {
9991
+ return metrics;
9992
+ }
9993
+ const cachedFromEvent = getCachedMetricFromEndEvent(endEvent);
9994
+ if (cachedFromEvent !== void 0) {
9995
+ return {
9996
+ ...metrics,
9997
+ cached: cachedFromEvent
9998
+ };
9999
+ }
10000
+ if (!isObject(result)) {
10001
+ return metrics;
10002
+ }
10003
+ const cached = result[BRAINTRUST_CACHED_STREAM_METRIC];
10004
+ if (typeof cached !== "number") {
10005
+ return metrics;
10006
+ }
10007
+ return {
10008
+ ...metrics,
10009
+ cached
10010
+ };
9229
10011
  }
9230
10012
  function processImagesInOutput(output) {
9231
10013
  if (Array.isArray(output)) {
@@ -9256,7 +10038,7 @@ function processImagesInOutput(output) {
9256
10038
  }
9257
10039
  return output;
9258
10040
  }
9259
- function aggregateChatCompletionChunks(chunks) {
10041
+ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
9260
10042
  let role = void 0;
9261
10043
  let content = void 0;
9262
10044
  let tool_calls = void 0;
@@ -9269,7 +10051,7 @@ function aggregateChatCompletionChunks(chunks) {
9269
10051
  ...parseMetricsFromUsage(chunk.usage)
9270
10052
  };
9271
10053
  }
9272
- const delta = _optionalChain([chunk, 'access', _116 => _116.choices, 'optionalAccess', _117 => _117[0], 'optionalAccess', _118 => _118.delta]);
10054
+ const delta = _optionalChain([chunk, 'access', _132 => _132.choices, 'optionalAccess', _133 => _133[0], 'optionalAccess', _134 => _134.delta]);
9273
10055
  if (!delta) {
9274
10056
  continue;
9275
10057
  }
@@ -9298,6 +10080,7 @@ function aggregateChatCompletionChunks(chunks) {
9298
10080
  }
9299
10081
  }
9300
10082
  }
10083
+ metrics = withCachedMetric(metrics, streamResult, endEvent);
9301
10084
  return {
9302
10085
  metrics,
9303
10086
  output: [
@@ -9314,9 +10097,33 @@ function aggregateChatCompletionChunks(chunks) {
9314
10097
  ]
9315
10098
  };
9316
10099
  }
9317
-
9318
- // src/instrumentation/plugins/anthropic-plugin.ts
9319
-
10100
+ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
10101
+ let output = void 0;
10102
+ let metrics = {};
10103
+ let metadata = void 0;
10104
+ for (const chunk of chunks) {
10105
+ if (!chunk || !chunk.type || !chunk.response) {
10106
+ continue;
10107
+ }
10108
+ if (chunk.type !== "response.completed") {
10109
+ continue;
10110
+ }
10111
+ const response = chunk.response;
10112
+ if (_optionalChain([response, 'optionalAccess', _135 => _135.output]) !== void 0) {
10113
+ output = processImagesInOutput(response.output);
10114
+ }
10115
+ const { usage: _usage, output: _output, ...rest } = response || {};
10116
+ if (Object.keys(rest).length > 0) {
10117
+ metadata = rest;
10118
+ }
10119
+ metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _136 => _136.usage]));
10120
+ }
10121
+ return {
10122
+ output,
10123
+ metrics: withCachedMetric(metrics, void 0, endEvent),
10124
+ ...metadata !== void 0 ? { metadata } : {}
10125
+ };
10126
+ }
9320
10127
 
9321
10128
  // src/wrappers/anthropic-tokens-util.ts
9322
10129
  function finalizeAnthropicTokens(metrics) {
@@ -9338,20 +10145,28 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
9338
10145
  return cacheTokens;
9339
10146
  }
9340
10147
 
10148
+ // src/instrumentation/plugins/anthropic-channels.ts
10149
+ var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
10150
+ messagesCreate: channel({
10151
+ channelName: "messages.create",
10152
+ kind: "async"
10153
+ }),
10154
+ betaMessagesCreate: channel({
10155
+ channelName: "beta.messages.create",
10156
+ kind: "async"
10157
+ })
10158
+ });
10159
+
9341
10160
  // src/instrumentation/plugins/anthropic-plugin.ts
9342
- var AnthropicPlugin = (_class17 = class extends BasePlugin {constructor(...args2) { super(...args2); _class17.prototype.__init62.call(this); }
9343
- __init62() {this.unsubscribers = []}
10161
+ var AnthropicPlugin = class extends BasePlugin {
9344
10162
  onEnable() {
9345
10163
  this.subscribeToAnthropicChannels();
9346
10164
  }
9347
10165
  onDisable() {
9348
- for (const unsubscribe of this.unsubscribers) {
9349
- unsubscribe();
9350
- }
9351
- this.unsubscribers = [];
10166
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
9352
10167
  }
9353
10168
  subscribeToAnthropicChannels() {
9354
- this.subscribeToStreamingChannel("orchestrion:anthropic:messages.create", {
10169
+ const anthropicConfig = {
9355
10170
  name: "anthropic.messages.create",
9356
10171
  type: "llm" /* LLM */,
9357
10172
  extractInput: (args) => {
@@ -9363,192 +10178,44 @@ var AnthropicPlugin = (_class17 = class extends BasePlugin {constructor(...args2
9363
10178
  metadata: { ...metadata, provider: "anthropic" }
9364
10179
  };
9365
10180
  },
9366
- extractOutput: (result) => {
9367
- return result ? { role: result.role, content: result.content } : null;
10181
+ extractOutput: (message) => {
10182
+ return message ? { role: message.role, content: message.content } : null;
9368
10183
  },
9369
- extractMetrics: (result, startTime) => {
9370
- const metrics = parseMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _119 => _119.usage]));
10184
+ extractMetrics: (message, startTime) => {
10185
+ const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _137 => _137.usage]));
9371
10186
  if (startTime) {
9372
10187
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9373
10188
  }
9374
10189
  const finalized = finalizeAnthropicTokens(metrics);
9375
10190
  return Object.fromEntries(
9376
- Object.entries(finalized).filter(([, v]) => v !== void 0)
10191
+ Object.entries(finalized).filter(
10192
+ (entry) => entry[1] !== void 0
10193
+ )
9377
10194
  );
9378
10195
  },
9379
- extractMetadata: (result) => {
10196
+ extractMetadata: (message) => {
9380
10197
  const metadata = {};
9381
10198
  const metas = ["stop_reason", "stop_sequence"];
9382
10199
  for (const m of metas) {
9383
- if (_optionalChain([result, 'optionalAccess', _120 => _120[m]]) !== void 0) {
9384
- metadata[m] = result[m];
10200
+ if (_optionalChain([message, 'optionalAccess', _138 => _138[m]]) !== void 0) {
10201
+ metadata[m] = message[m];
9385
10202
  }
9386
10203
  }
9387
10204
  return metadata;
9388
10205
  },
9389
- aggregateChunks: aggregateAnthropicStreamChunks,
9390
- isStreaming: (args) => {
9391
- return _optionalChain([args, 'access', _121 => _121[0], 'optionalAccess', _122 => _122.stream]) === true;
9392
- }
9393
- });
9394
- this.subscribeToStreamingChannel(
9395
- "orchestrion:anthropic:beta.messages.create",
9396
- {
9397
- name: "anthropic.beta.messages.create",
9398
- type: "llm" /* LLM */,
9399
- extractInput: (args) => {
9400
- const params = args[0] || {};
9401
- const input = coalesceInput(params.messages || [], params.system);
9402
- const metadata = filterFrom(params, ["messages", "system"]);
9403
- return {
9404
- input: processAttachmentsInInput(input),
9405
- metadata: { ...metadata, provider: "anthropic" }
9406
- };
9407
- },
9408
- extractOutput: (result) => {
9409
- return result ? { role: result.role, content: result.content } : null;
9410
- },
9411
- extractMetrics: (result, startTime) => {
9412
- const metrics = parseMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _123 => _123.usage]));
9413
- if (startTime) {
9414
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9415
- }
9416
- const finalized = finalizeAnthropicTokens(metrics);
9417
- return Object.fromEntries(
9418
- Object.entries(finalized).filter(([, v]) => v !== void 0)
9419
- );
9420
- },
9421
- extractMetadata: (result) => {
9422
- const metadata = {};
9423
- const metas = ["stop_reason", "stop_sequence"];
9424
- for (const m of metas) {
9425
- if (_optionalChain([result, 'optionalAccess', _124 => _124[m]]) !== void 0) {
9426
- metadata[m] = result[m];
9427
- }
9428
- }
9429
- return metadata;
9430
- },
9431
- aggregateChunks: aggregateAnthropicStreamChunks,
9432
- isStreaming: (args) => {
9433
- return _optionalChain([args, 'access', _125 => _125[0], 'optionalAccess', _126 => _126.stream]) === true;
9434
- }
9435
- }
9436
- );
9437
- }
9438
- /**
9439
- * Subscribe to a channel for async methods that may return streams.
9440
- * Handles both streaming and non-streaming responses based on the stream parameter.
9441
- */
9442
- subscribeToStreamingChannel(channelName, config) {
9443
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
9444
- const spans = /* @__PURE__ */ new WeakMap();
9445
- const handlers = {
9446
- start: (event) => {
9447
- const span = startSpan({
9448
- name: config.name,
9449
- spanAttributes: {
9450
- type: config.type
9451
- }
9452
- });
9453
- const startTime = getCurrentUnixTimestamp();
9454
- spans.set(event, { span, startTime });
9455
- try {
9456
- const { input, metadata } = config.extractInput(event.arguments);
9457
- span.log({
9458
- input,
9459
- metadata
9460
- });
9461
- } catch (error) {
9462
- console.error(`Error extracting input for ${channelName}:`, error);
9463
- }
9464
- },
9465
- asyncEnd: (event) => {
9466
- const spanData = spans.get(event);
9467
- if (!spanData) {
9468
- return;
9469
- }
9470
- const { span, startTime } = spanData;
9471
- const isStreaming = config.isStreaming ? config.isStreaming(event.arguments) : isAsyncIterable(event.result);
9472
- if (isStreaming && isAsyncIterable(event.result)) {
9473
- patchStreamIfNeeded(event.result, {
9474
- onComplete: (chunks) => {
9475
- try {
9476
- let output;
9477
- let metrics;
9478
- let metadata = {};
9479
- if (config.aggregateChunks) {
9480
- const aggregated = config.aggregateChunks(chunks);
9481
- output = aggregated.output;
9482
- metrics = aggregated.metrics;
9483
- metadata = aggregated.metadata || {};
9484
- } else {
9485
- output = config.extractOutput(chunks);
9486
- metrics = config.extractMetrics(chunks, startTime);
9487
- if (config.extractMetadata) {
9488
- metadata = config.extractMetadata(chunks);
9489
- }
9490
- }
9491
- if (!metrics.time_to_first_token && chunks.length > 0) {
9492
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9493
- }
9494
- span.log({
9495
- output,
9496
- metrics,
9497
- metadata
9498
- });
9499
- } catch (error) {
9500
- console.error(
9501
- `Error extracting output for ${channelName}:`,
9502
- error
9503
- );
9504
- } finally {
9505
- span.end();
9506
- }
9507
- },
9508
- onError: (error) => {
9509
- span.log({
9510
- error: error.message
9511
- });
9512
- span.end();
9513
- }
9514
- });
9515
- } else {
9516
- try {
9517
- const output = config.extractOutput(event.result);
9518
- const metrics = config.extractMetrics(event.result, startTime);
9519
- const metadata = config.extractMetadata ? config.extractMetadata(event.result) : {};
9520
- span.log({
9521
- output,
9522
- metrics,
9523
- metadata
9524
- });
9525
- } catch (error) {
9526
- console.error(`Error extracting output for ${channelName}:`, error);
9527
- } finally {
9528
- span.end();
9529
- spans.delete(event);
9530
- }
9531
- }
9532
- },
9533
- error: (event) => {
9534
- const spanData = spans.get(event);
9535
- if (!spanData) {
9536
- return;
9537
- }
9538
- const { span } = spanData;
9539
- span.log({
9540
- error: event.error.message
9541
- });
9542
- span.end();
9543
- spans.delete(event);
9544
- }
10206
+ aggregateChunks: (chunks) => aggregateAnthropicStreamChunks(chunks)
9545
10207
  };
9546
- channel.subscribe(handlers);
9547
- this.unsubscribers.push(() => {
9548
- channel.unsubscribe(handlers);
9549
- });
10208
+ this.unsubscribers.push(
10209
+ traceStreamingChannel(anthropicChannels.messagesCreate, anthropicConfig)
10210
+ );
10211
+ this.unsubscribers.push(
10212
+ traceStreamingChannel(anthropicChannels.betaMessagesCreate, {
10213
+ ...anthropicConfig,
10214
+ name: "anthropic.beta.messages.create"
10215
+ })
10216
+ );
9550
10217
  }
9551
- }, _class17);
10218
+ };
9552
10219
  function parseMetricsFromUsage2(usage) {
9553
10220
  if (!usage) {
9554
10221
  return {};
@@ -9570,29 +10237,29 @@ function aggregateAnthropicStreamChunks(chunks) {
9570
10237
  const deltas = [];
9571
10238
  let metrics = {};
9572
10239
  let metadata = {};
9573
- for (const chunk of chunks) {
9574
- switch (_optionalChain([chunk, 'optionalAccess', _127 => _127.type])) {
10240
+ for (const event of chunks) {
10241
+ switch (_optionalChain([event, 'optionalAccess', _139 => _139.type])) {
9575
10242
  case "message_start":
9576
- if (_optionalChain([chunk, 'access', _128 => _128.message, 'optionalAccess', _129 => _129.usage])) {
9577
- const initialMetrics = parseMetricsFromUsage2(chunk.message.usage);
10243
+ if (_optionalChain([event, 'access', _140 => _140.message, 'optionalAccess', _141 => _141.usage])) {
10244
+ const initialMetrics = parseMetricsFromUsage2(event.message.usage);
9578
10245
  metrics = { ...metrics, ...initialMetrics };
9579
10246
  }
9580
10247
  break;
9581
10248
  case "content_block_delta":
9582
- if (_optionalChain([chunk, 'access', _130 => _130.delta, 'optionalAccess', _131 => _131.type]) === "text_delta") {
9583
- const text = _optionalChain([chunk, 'access', _132 => _132.delta, 'optionalAccess', _133 => _133.text]);
10249
+ if (_optionalChain([event, 'access', _142 => _142.delta, 'optionalAccess', _143 => _143.type]) === "text_delta") {
10250
+ const text = event.delta.text;
9584
10251
  if (text) {
9585
10252
  deltas.push(text);
9586
10253
  }
9587
10254
  }
9588
10255
  break;
9589
10256
  case "message_delta":
9590
- if (chunk.usage) {
9591
- const finalMetrics = parseMetricsFromUsage2(chunk.usage);
10257
+ if (event.usage) {
10258
+ const finalMetrics = parseMetricsFromUsage2(event.usage);
9592
10259
  metrics = { ...metrics, ...finalMetrics };
9593
10260
  }
9594
- if (chunk.delta) {
9595
- metadata = { ...metadata, ...chunk.delta };
10261
+ if (event.delta) {
10262
+ metadata = { ...metadata, ...event.delta };
9596
10263
  }
9597
10264
  break;
9598
10265
  }
@@ -9600,7 +10267,9 @@ function aggregateAnthropicStreamChunks(chunks) {
9600
10267
  const output = deltas.join("");
9601
10268
  const finalized = finalizeAnthropicTokens(metrics);
9602
10269
  const filteredMetrics = Object.fromEntries(
9603
- Object.entries(finalized).filter(([, v]) => v !== void 0)
10270
+ Object.entries(finalized).filter(
10271
+ (entry) => entry[1] !== void 0
10272
+ )
9604
10273
  );
9605
10274
  return {
9606
10275
  output,
@@ -9608,6 +10277,9 @@ function aggregateAnthropicStreamChunks(chunks) {
9608
10277
  metadata
9609
10278
  };
9610
10279
  }
10280
+ function isAnthropicBase64ContentBlock(input) {
10281
+ return (input.type === "image" || input.type === "document") && isObject(input.source) && input.source.type === "base64";
10282
+ }
9611
10283
  function convertBase64ToAttachment(source, contentType) {
9612
10284
  const mediaType = typeof source.media_type === "string" ? source.media_type : "image/png";
9613
10285
  const base64Data = source.data;
@@ -9631,14 +10303,14 @@ function convertBase64ToAttachment(source, contentType) {
9631
10303
  data: attachment
9632
10304
  };
9633
10305
  }
9634
- return source;
10306
+ return { ...source };
9635
10307
  }
9636
10308
  function processAttachmentsInInput(input) {
9637
10309
  if (Array.isArray(input)) {
9638
10310
  return input.map(processAttachmentsInInput);
9639
10311
  }
9640
10312
  if (isObject(input)) {
9641
- if ((input.type === "image" || input.type === "document") && isObject(input.source) && input.source.type === "base64") {
10313
+ if (isAnthropicBase64ContentBlock(input)) {
9642
10314
  return {
9643
10315
  ...input,
9644
10316
  source: convertBase64ToAttachment(input.source, input.type)
@@ -9669,8 +10341,35 @@ function filterFrom(obj, fieldsToRemove) {
9669
10341
  return result;
9670
10342
  }
9671
10343
 
9672
- // src/instrumentation/plugins/ai-sdk-plugin.ts
10344
+ // src/instrumentation/plugins/ai-sdk-channels.ts
10345
+ var aiSDKChannels = defineChannels("ai", {
10346
+ generateText: channel({
10347
+ channelName: "generateText",
10348
+ kind: "async"
10349
+ }),
10350
+ streamText: channel({
10351
+ channelName: "streamText",
10352
+ kind: "async"
10353
+ }),
10354
+ generateObject: channel({
10355
+ channelName: "generateObject",
10356
+ kind: "async"
10357
+ }),
10358
+ streamObject: channel({
10359
+ channelName: "streamObject",
10360
+ kind: "async"
10361
+ }),
10362
+ agentGenerate: channel({
10363
+ channelName: "Agent.generate",
10364
+ kind: "async"
10365
+ }),
10366
+ agentStream: channel({
10367
+ channelName: "Agent.stream",
10368
+ kind: "async"
10369
+ })
10370
+ });
9673
10371
 
10372
+ // src/instrumentation/plugins/ai-sdk-plugin.ts
9674
10373
  var DEFAULT_DENY_OUTPUT_PATHS = [
9675
10374
  // v3
9676
10375
  "roundtrips[].request.body",
@@ -9685,262 +10384,160 @@ var DEFAULT_DENY_OUTPUT_PATHS = [
9685
10384
  "steps[].response.body",
9686
10385
  "steps[].response.headers"
9687
10386
  ];
9688
- var AISDKPlugin = (_class18 = class extends BasePlugin {
9689
- __init63() {this.unsubscribers = []}
10387
+ var AISDKPlugin = class extends BasePlugin {
9690
10388
 
9691
10389
  constructor(config = {}) {
9692
- super();_class18.prototype.__init63.call(this);;
10390
+ super();
9693
10391
  this.config = config;
9694
10392
  }
9695
10393
  onEnable() {
9696
10394
  this.subscribeToAISDK();
9697
10395
  }
9698
10396
  onDisable() {
9699
- for (const unsubscribe of this.unsubscribers) {
9700
- unsubscribe();
9701
- }
9702
- this.unsubscribers = [];
10397
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
9703
10398
  }
9704
10399
  subscribeToAISDK() {
9705
10400
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
9706
- this.subscribeToStreamingChannel("orchestrion:ai-sdk:generateText", {
9707
- name: "generateText",
9708
- type: "llm" /* LLM */,
9709
- extractInput: (args) => {
9710
- const params = args[0] || {};
9711
- return {
9712
- input: processAISDKInput(params),
9713
- metadata: extractMetadataFromParams(params)
9714
- };
9715
- },
9716
- extractOutput: (result) => {
9717
- return processAISDKOutput(result, denyOutputPaths);
9718
- },
9719
- extractMetrics: (result, startTime) => {
9720
- const metrics = extractTokenMetrics(result);
9721
- if (startTime) {
9722
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9723
- }
9724
- return metrics;
9725
- },
9726
- aggregateChunks: aggregateAISDKChunks
9727
- });
9728
- this.subscribeToStreamingChannel("orchestrion:ai-sdk:streamText", {
9729
- name: "streamText",
9730
- type: "llm" /* LLM */,
9731
- extractInput: (args) => {
9732
- const params = args[0] || {};
9733
- return {
9734
- input: processAISDKInput(params),
9735
- metadata: extractMetadataFromParams(params)
9736
- };
9737
- },
9738
- extractOutput: (result) => {
9739
- return processAISDKOutput(result, denyOutputPaths);
9740
- },
9741
- extractMetrics: (result, startTime) => {
9742
- const metrics = extractTokenMetrics(result);
9743
- if (startTime) {
9744
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9745
- }
9746
- return metrics;
9747
- },
9748
- aggregateChunks: aggregateAISDKChunks
9749
- });
9750
- this.subscribeToStreamingChannel("orchestrion:ai-sdk:generateObject", {
9751
- name: "generateObject",
9752
- type: "llm" /* LLM */,
9753
- extractInput: (args) => {
9754
- const params = args[0] || {};
9755
- return {
9756
- input: processAISDKInput(params),
9757
- metadata: extractMetadataFromParams(params)
9758
- };
9759
- },
9760
- extractOutput: (result) => {
9761
- return processAISDKOutput(result, denyOutputPaths);
9762
- },
9763
- extractMetrics: (result, startTime) => {
9764
- const metrics = extractTokenMetrics(result);
9765
- if (startTime) {
9766
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9767
- }
9768
- return metrics;
9769
- },
9770
- aggregateChunks: aggregateAISDKChunks
9771
- });
9772
- this.subscribeToStreamingChannel("orchestrion:ai-sdk:streamObject", {
9773
- name: "streamObject",
9774
- type: "llm" /* LLM */,
9775
- extractInput: (args) => {
9776
- const params = args[0] || {};
9777
- return {
9778
- input: processAISDKInput(params),
9779
- metadata: extractMetadataFromParams(params)
9780
- };
9781
- },
9782
- extractOutput: (result) => {
9783
- return processAISDKOutput(result, denyOutputPaths);
9784
- },
9785
- extractMetrics: (result, startTime) => {
9786
- const metrics = extractTokenMetrics(result);
9787
- if (startTime) {
9788
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9789
- }
9790
- return metrics;
9791
- },
9792
- aggregateChunks: aggregateAISDKChunks
9793
- });
9794
- this.subscribeToStreamingChannel("orchestrion:ai-sdk:Agent.generate", {
9795
- name: "Agent.generate",
9796
- type: "llm" /* LLM */,
9797
- extractInput: (args) => {
9798
- const params = args[0] || {};
9799
- return {
9800
- input: processAISDKInput(params),
9801
- metadata: extractMetadataFromParams(params)
9802
- };
9803
- },
9804
- extractOutput: (result) => {
9805
- return processAISDKOutput(result, denyOutputPaths);
9806
- },
9807
- extractMetrics: (result, startTime) => {
9808
- const metrics = extractTokenMetrics(result);
9809
- if (startTime) {
9810
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9811
- }
9812
- return metrics;
9813
- },
9814
- aggregateChunks: aggregateAISDKChunks
9815
- });
9816
- this.subscribeToStreamingChannel("orchestrion:ai-sdk:Agent.stream", {
9817
- name: "Agent.stream",
9818
- type: "llm" /* LLM */,
9819
- extractInput: (args) => {
9820
- const params = args[0] || {};
9821
- return {
9822
- input: processAISDKInput(params),
9823
- metadata: extractMetadataFromParams(params)
9824
- };
9825
- },
9826
- extractOutput: (result) => {
9827
- return processAISDKOutput(result, denyOutputPaths);
9828
- },
9829
- extractMetrics: (result, startTime) => {
9830
- const metrics = extractTokenMetrics(result);
9831
- if (startTime) {
9832
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9833
- }
9834
- return metrics;
9835
- },
9836
- aggregateChunks: aggregateAISDKChunks
9837
- });
9838
- }
9839
- /**
9840
- * Subscribe to a channel for async methods that may return streams.
9841
- * Handles both streaming and non-streaming responses.
9842
- */
9843
- subscribeToStreamingChannel(channelName, config) {
9844
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
9845
- const spans = /* @__PURE__ */ new WeakMap();
9846
- const handlers = {
9847
- start: (event) => {
9848
- const span = startSpan({
9849
- name: config.name,
9850
- spanAttributes: {
9851
- type: config.type
10401
+ this.unsubscribers.push(
10402
+ traceStreamingChannel(aiSDKChannels.generateText, {
10403
+ name: "generateText",
10404
+ type: "llm" /* LLM */,
10405
+ extractInput: ([params]) => {
10406
+ return {
10407
+ input: processAISDKInput(params),
10408
+ metadata: extractMetadataFromParams(params)
10409
+ };
10410
+ },
10411
+ extractOutput: (result) => {
10412
+ return processAISDKOutput(result, denyOutputPaths);
10413
+ },
10414
+ extractMetrics: (result, startTime) => {
10415
+ const metrics = extractTokenMetrics(result);
10416
+ if (startTime) {
10417
+ metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9852
10418
  }
9853
- });
9854
- const startTime = getCurrentUnixTimestamp();
9855
- spans.set(event, { span, startTime });
9856
- try {
9857
- const { input, metadata } = config.extractInput(event.arguments);
9858
- span.log({
9859
- input,
9860
- metadata
9861
- });
9862
- } catch (error) {
9863
- console.error(`Error extracting input for ${channelName}:`, error);
9864
- }
9865
- },
9866
- asyncEnd: (event) => {
9867
- const spanData = spans.get(event);
9868
- if (!spanData) {
9869
- return;
9870
- }
9871
- const { span, startTime } = spanData;
9872
- if (isAsyncIterable(event.result)) {
9873
- patchStreamIfNeeded(event.result, {
9874
- onComplete: (chunks) => {
9875
- try {
9876
- let output;
9877
- let metrics;
9878
- if (config.aggregateChunks) {
9879
- const aggregated = config.aggregateChunks(chunks);
9880
- output = aggregated.output;
9881
- metrics = aggregated.metrics;
9882
- } else {
9883
- output = config.extractOutput(chunks);
9884
- metrics = config.extractMetrics(chunks, startTime);
9885
- }
9886
- if (!metrics.time_to_first_token && chunks.length > 0) {
9887
- metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9888
- }
9889
- span.log({
9890
- output,
9891
- metrics
9892
- });
9893
- } catch (error) {
9894
- console.error(
9895
- `Error extracting output for ${channelName}:`,
9896
- error
9897
- );
9898
- } finally {
9899
- span.end();
9900
- }
9901
- },
9902
- onError: (error) => {
9903
- span.log({
9904
- error: error.message
9905
- });
9906
- span.end();
9907
- }
9908
- });
9909
- } else {
9910
- try {
9911
- const output = config.extractOutput(event.result);
9912
- const metrics = config.extractMetrics(event.result, startTime);
9913
- span.log({
9914
- output,
9915
- metrics
9916
- });
9917
- } catch (error) {
9918
- console.error(`Error extracting output for ${channelName}:`, error);
9919
- } finally {
9920
- span.end();
9921
- spans.delete(event);
10419
+ return metrics;
10420
+ },
10421
+ aggregateChunks: aggregateAISDKChunks
10422
+ })
10423
+ );
10424
+ this.unsubscribers.push(
10425
+ traceStreamingChannel(aiSDKChannels.streamText, {
10426
+ name: "streamText",
10427
+ 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;
9922
10441
  }
9923
- }
9924
- },
9925
- error: (event) => {
9926
- const spanData = spans.get(event);
9927
- if (!spanData) {
9928
- return;
9929
- }
9930
- const { span } = spanData;
9931
- span.log({
9932
- error: event.error.message
9933
- });
9934
- span.end();
9935
- spans.delete(event);
9936
- }
9937
- };
9938
- channel.subscribe(handlers);
9939
- this.unsubscribers.push(() => {
9940
- channel.unsubscribe(handlers);
9941
- });
10442
+ return metrics;
10443
+ },
10444
+ aggregateChunks: aggregateAISDKChunks
10445
+ })
10446
+ );
10447
+ this.unsubscribers.push(
10448
+ traceStreamingChannel(aiSDKChannels.generateObject, {
10449
+ name: "generateObject",
10450
+ type: "llm" /* LLM */,
10451
+ extractInput: ([params]) => {
10452
+ return {
10453
+ input: processAISDKInput(params),
10454
+ metadata: extractMetadataFromParams(params)
10455
+ };
10456
+ },
10457
+ extractOutput: (result) => {
10458
+ return processAISDKOutput(result, denyOutputPaths);
10459
+ },
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
+ },
10467
+ aggregateChunks: aggregateAISDKChunks
10468
+ })
10469
+ );
10470
+ this.unsubscribers.push(
10471
+ traceStreamingChannel(aiSDKChannels.streamObject, {
10472
+ name: "streamObject",
10473
+ 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
10491
+ })
10492
+ );
10493
+ this.unsubscribers.push(
10494
+ traceStreamingChannel(aiSDKChannels.agentGenerate, {
10495
+ name: "Agent.generate",
10496
+ type: "llm" /* LLM */,
10497
+ extractInput: ([params]) => {
10498
+ return {
10499
+ input: processAISDKInput(params),
10500
+ metadata: extractMetadataFromParams(params)
10501
+ };
10502
+ },
10503
+ extractOutput: (result) => {
10504
+ return processAISDKOutput(result, denyOutputPaths);
10505
+ },
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
+ },
10513
+ aggregateChunks: aggregateAISDKChunks
10514
+ })
10515
+ );
10516
+ this.unsubscribers.push(
10517
+ traceStreamingChannel(aiSDKChannels.agentStream, {
10518
+ name: "Agent.stream",
10519
+ type: "llm" /* LLM */,
10520
+ extractInput: ([params]) => {
10521
+ return {
10522
+ input: processAISDKInput(params),
10523
+ metadata: extractMetadataFromParams(params)
10524
+ };
10525
+ },
10526
+ extractOutput: (result) => {
10527
+ return processAISDKOutput(result, denyOutputPaths);
10528
+ },
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
+ },
10536
+ aggregateChunks: aggregateAISDKChunks
10537
+ })
10538
+ );
9942
10539
  }
9943
- }, _class18);
10540
+ };
9944
10541
  function processAISDKInput(params) {
9945
10542
  if (!params) return params;
9946
10543
  return processInputAttachments(params);
@@ -9969,7 +10566,7 @@ function processAISDKOutput(output, denyOutputPaths) {
9969
10566
  }
9970
10567
  function extractTokenMetrics(result) {
9971
10568
  const metrics = {};
9972
- let usage = _optionalChain([result, 'optionalAccess', _134 => _134.totalUsage]) || _optionalChain([result, 'optionalAccess', _135 => _135.usage]);
10569
+ let usage = _optionalChain([result, 'optionalAccess', _144 => _144.totalUsage]) || _optionalChain([result, 'optionalAccess', _145 => _145.usage]);
9973
10570
  if (!usage && result) {
9974
10571
  try {
9975
10572
  if ("totalUsage" in result && typeof result.totalUsage !== "function") {
@@ -9984,7 +10581,7 @@ function extractTokenMetrics(result) {
9984
10581
  return metrics;
9985
10582
  }
9986
10583
  const promptTokens = firstNumber(
9987
- _optionalChain([usage, 'access', _136 => _136.inputTokens, 'optionalAccess', _137 => _137.total]),
10584
+ _optionalChain([usage, 'access', _146 => _146.inputTokens, 'optionalAccess', _147 => _147.total]),
9988
10585
  usage.inputTokens,
9989
10586
  usage.promptTokens,
9990
10587
  usage.prompt_tokens
@@ -9993,7 +10590,7 @@ function extractTokenMetrics(result) {
9993
10590
  metrics.prompt_tokens = promptTokens;
9994
10591
  }
9995
10592
  const completionTokens = firstNumber(
9996
- _optionalChain([usage, 'access', _138 => _138.outputTokens, 'optionalAccess', _139 => _139.total]),
10593
+ _optionalChain([usage, 'access', _148 => _148.outputTokens, 'optionalAccess', _149 => _149.total]),
9997
10594
  usage.outputTokens,
9998
10595
  usage.completionTokens,
9999
10596
  usage.completion_tokens
@@ -10063,8 +10660,8 @@ function extractGetterValues(obj) {
10063
10660
  return getterValues;
10064
10661
  }
10065
10662
  function serializeModelWithProvider(model) {
10066
- const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _140 => _140.modelId]);
10067
- const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _141 => _141.provider]) : void 0;
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;
10068
10665
  if (!modelId) {
10069
10666
  return { model: modelId, provider: explicitProvider };
10070
10667
  }
@@ -10088,12 +10685,12 @@ function parseGatewayModelString(modelString) {
10088
10685
  return { model: modelString };
10089
10686
  }
10090
10687
  function extractCostFromResult(result) {
10091
- if (_optionalChain([result, 'optionalAccess', _142 => _142.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
10688
+ if (_optionalChain([result, 'optionalAccess', _152 => _152.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
10092
10689
  let totalCost = 0;
10093
10690
  let foundCost = false;
10094
10691
  for (const step of result.steps) {
10095
- const gateway2 = _optionalChain([step, 'optionalAccess', _143 => _143.providerMetadata, 'optionalAccess', _144 => _144.gateway]);
10096
- const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _145 => _145.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _146 => _146.marketCost]));
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]));
10097
10694
  if (stepCost !== void 0 && stepCost > 0) {
10098
10695
  totalCost += stepCost;
10099
10696
  foundCost = true;
@@ -10103,8 +10700,8 @@ function extractCostFromResult(result) {
10103
10700
  return totalCost;
10104
10701
  }
10105
10702
  }
10106
- const gateway = _optionalChain([result, 'optionalAccess', _147 => _147.providerMetadata, 'optionalAccess', _148 => _148.gateway]);
10107
- const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _149 => _149.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _150 => _150.marketCost]));
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]));
10108
10705
  if (directCost !== void 0 && directCost > 0) {
10109
10706
  return directCost;
10110
10707
  }
@@ -10180,7 +10777,10 @@ function omitAtPath(obj, keys) {
10180
10777
  if (Array.isArray(obj)) {
10181
10778
  obj.forEach((item) => {
10182
10779
  if (remainingKeys.length > 0) {
10183
- omitAtPath(item, remainingKeys);
10780
+ omitAtPath(
10781
+ item,
10782
+ remainingKeys
10783
+ );
10184
10784
  }
10185
10785
  });
10186
10786
  }
@@ -10190,7 +10790,10 @@ function omitAtPath(obj, keys) {
10190
10790
  }
10191
10791
  } else {
10192
10792
  if (obj && typeof obj === "object" && firstKey in obj) {
10193
- omitAtPath(obj[firstKey], remainingKeys);
10793
+ omitAtPath(
10794
+ obj[firstKey],
10795
+ remainingKeys
10796
+ );
10194
10797
  }
10195
10798
  }
10196
10799
  }
@@ -10203,8 +10806,18 @@ function omit(obj, paths) {
10203
10806
  return result;
10204
10807
  }
10205
10808
 
10206
- // src/instrumentation/plugins/claude-agent-sdk-plugin.ts
10809
+ // src/instrumentation/plugins/claude-agent-sdk-channels.ts
10810
+ var claudeAgentSDKChannels = defineChannels(
10811
+ "@anthropic-ai/claude-agent-sdk",
10812
+ {
10813
+ query: channel({
10814
+ channelName: "query",
10815
+ kind: "async"
10816
+ })
10817
+ }
10818
+ );
10207
10819
 
10820
+ // src/instrumentation/plugins/claude-agent-sdk-plugin.ts
10208
10821
  function filterSerializableOptions(options) {
10209
10822
  const allowedKeys = [
10210
10823
  "model",
@@ -10240,7 +10853,7 @@ function extractUsageFromMessage(message) {
10240
10853
  const metrics = {};
10241
10854
  let usage;
10242
10855
  if (message.type === "assistant") {
10243
- usage = _optionalChain([message, 'access', _151 => _151.message, 'optionalAccess', _152 => _152.usage]);
10856
+ usage = _optionalChain([message, 'access', _161 => _161.message, 'optionalAccess', _162 => _162.usage]);
10244
10857
  } else if (message.type === "result") {
10245
10858
  usage = message.usage;
10246
10859
  }
@@ -10280,15 +10893,17 @@ function buildLLMInput(prompt, conversationHistory) {
10280
10893
  async function createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, parentSpan) {
10281
10894
  if (messages.length === 0) return void 0;
10282
10895
  const lastMessage = messages[messages.length - 1];
10283
- if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _153 => _153.message, 'optionalAccess', _154 => _154.usage])) {
10896
+ if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _163 => _163.message, 'optionalAccess', _164 => _164.usage])) {
10284
10897
  return void 0;
10285
10898
  }
10286
10899
  const model = lastMessage.message.model || options.model;
10287
10900
  const usage = extractUsageFromMessage(lastMessage);
10288
10901
  const input = buildLLMInput(prompt, conversationHistory);
10289
10902
  const outputs = messages.map(
10290
- (m) => _optionalChain([m, 'access', _155 => _155.message, 'optionalAccess', _156 => _156.content]) && _optionalChain([m, 'access', _157 => _157.message, 'optionalAccess', _158 => _158.role]) ? { content: m.message.content, role: m.message.role } : void 0
10291
- ).filter((c) => c !== void 0);
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
10904
+ ).filter(
10905
+ (c) => c !== void 0
10906
+ );
10292
10907
  const span = startSpan({
10293
10908
  name: "anthropic.messages.create",
10294
10909
  spanAttributes: {
@@ -10304,10 +10919,9 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
10304
10919
  metrics: usage
10305
10920
  });
10306
10921
  await span.end();
10307
- return _optionalChain([lastMessage, 'access', _159 => _159.message, 'optionalAccess', _160 => _160.content]) && _optionalChain([lastMessage, 'access', _161 => _161.message, 'optionalAccess', _162 => _162.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
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;
10308
10923
  }
10309
- var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...args3) { super(...args3); _class19.prototype.__init64.call(this); }
10310
- __init64() {this.unsubscribers = []}
10924
+ var ClaudeAgentSDKPlugin = class extends BasePlugin {
10311
10925
  onEnable() {
10312
10926
  this.subscribeToQuery();
10313
10927
  }
@@ -10323,12 +10937,13 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10323
10937
  * and individual LLM calls.
10324
10938
  */
10325
10939
  subscribeToQuery() {
10326
- const channel = _dcbrowser.tracingChannel.call(void 0, "orchestrion:claude-agent-sdk:query");
10940
+ const channel2 = claudeAgentSDKChannels.query.tracingChannel();
10327
10941
  const spans = /* @__PURE__ */ new WeakMap();
10328
10942
  const handlers = {
10329
10943
  start: (event) => {
10330
- const params = _nullishCoalesce(event.arguments[0], () => ( {}));
10331
- const { prompt, options = {} } = params;
10944
+ const params = event.arguments[0];
10945
+ const prompt = _optionalChain([params, 'optionalAccess', _173 => _173.prompt]);
10946
+ const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _174 => _174.options]), () => ( {}));
10332
10947
  const span = startSpan({
10333
10948
  name: "Claude Agent",
10334
10949
  spanAttributes: {
@@ -10340,7 +10955,7 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10340
10955
  span.log({
10341
10956
  input: typeof prompt === "string" ? prompt : {
10342
10957
  type: "streaming",
10343
- description: "AsyncIterable<SDKMessage>"
10958
+ description: "AsyncIterable<ClaudeAgentSDKMessage>"
10344
10959
  },
10345
10960
  metadata: filterSerializableOptions(options)
10346
10961
  });
@@ -10362,13 +10977,20 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10362
10977
  if (!spanData) {
10363
10978
  return;
10364
10979
  }
10365
- if (isAsyncIterable(event.result)) {
10366
- patchStreamIfNeeded(event.result, {
10980
+ const eventResult = event.result;
10981
+ if (eventResult === void 0) {
10982
+ spanData.span.end();
10983
+ spans.delete(event);
10984
+ return;
10985
+ }
10986
+ if (isAsyncIterable(eventResult)) {
10987
+ patchStreamIfNeeded(eventResult, {
10367
10988
  onChunk: async (message) => {
10368
10989
  const currentTime = getCurrentUnixTimestamp();
10369
10990
  const params = event.arguments[0];
10370
- const { prompt, options = {} } = params;
10371
- const messageId = _optionalChain([message, 'access', _163 => _163.message, 'optionalAccess', _164 => _164.id]);
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]);
10372
10994
  if (messageId && messageId !== spanData.currentMessageId) {
10373
10995
  if (spanData.currentMessages.length > 0) {
10374
10996
  const finalMessage = await createLLMSpanForMessages(
@@ -10383,7 +11005,7 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10383
11005
  spanData.conversationHistory.push(finalMessage);
10384
11006
  }
10385
11007
  const lastMessage = spanData.currentMessages[spanData.currentMessages.length - 1];
10386
- if (_optionalChain([lastMessage, 'optionalAccess', _165 => _165.message, 'optionalAccess', _166 => _166.usage])) {
11008
+ if (_optionalChain([lastMessage, 'optionalAccess', _179 => _179.message, 'optionalAccess', _180 => _180.usage])) {
10387
11009
  const outputTokens = getNumberProperty(
10388
11010
  lastMessage.message.usage,
10389
11011
  "output_tokens"
@@ -10395,14 +11017,14 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10395
11017
  spanData.currentMessageId = messageId;
10396
11018
  spanData.currentMessageStartTime = currentTime;
10397
11019
  }
10398
- if (message.type === "assistant" && _optionalChain([message, 'access', _167 => _167.message, 'optionalAccess', _168 => _168.usage])) {
11020
+ if (message.type === "assistant" && _optionalChain([message, 'access', _181 => _181.message, 'optionalAccess', _182 => _182.usage])) {
10399
11021
  spanData.currentMessages.push(message);
10400
11022
  }
10401
11023
  if (message.type === "result" && message.usage) {
10402
11024
  const finalUsageMetrics = extractUsageFromMessage(message);
10403
11025
  if (spanData.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
10404
11026
  const lastMessage = spanData.currentMessages[spanData.currentMessages.length - 1];
10405
- if (_optionalChain([lastMessage, 'optionalAccess', _169 => _169.message, 'optionalAccess', _170 => _170.usage])) {
11027
+ if (_optionalChain([lastMessage, 'optionalAccess', _183 => _183.message, 'optionalAccess', _184 => _184.usage])) {
10406
11028
  const adjustedTokens = finalUsageMetrics.completion_tokens - spanData.accumulatedOutputTokens;
10407
11029
  if (adjustedTokens >= 0) {
10408
11030
  lastMessage.message.usage.output_tokens = adjustedTokens;
@@ -10426,7 +11048,8 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10426
11048
  onComplete: async () => {
10427
11049
  try {
10428
11050
  const params = event.arguments[0];
10429
- const { prompt, options = {} } = params;
11051
+ const prompt = _optionalChain([params, 'optionalAccess', _185 => _185.prompt]);
11052
+ const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _186 => _186.options]), () => ( {}));
10430
11053
  if (spanData.currentMessages.length > 0) {
10431
11054
  const finalMessage = await createLLMSpanForMessages(
10432
11055
  spanData.currentMessages,
@@ -10464,7 +11087,7 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10464
11087
  } else {
10465
11088
  try {
10466
11089
  spanData.span.log({
10467
- output: event.result
11090
+ output: eventResult
10468
11091
  });
10469
11092
  } catch (error) {
10470
11093
  console.error(
@@ -10479,7 +11102,7 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10479
11102
  },
10480
11103
  error: (event) => {
10481
11104
  const spanData = spans.get(event);
10482
- if (!spanData) {
11105
+ if (!spanData || !event.error) {
10483
11106
  return;
10484
11107
  }
10485
11108
  const { span } = spanData;
@@ -10490,53 +11113,39 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10490
11113
  spans.delete(event);
10491
11114
  }
10492
11115
  };
10493
- channel.subscribe(handlers);
11116
+ channel2.subscribe(handlers);
10494
11117
  this.unsubscribers.push(() => {
10495
- channel.unsubscribe(handlers);
11118
+ channel2.unsubscribe(handlers);
10496
11119
  });
10497
11120
  }
10498
- }, _class19);
11121
+ };
10499
11122
 
10500
- // src/instrumentation/plugins/google-genai-plugin.ts
11123
+ // src/instrumentation/plugins/google-genai-channels.ts
11124
+ var googleGenAIChannels = defineChannels("@google/genai", {
11125
+ generateContent: channel({
11126
+ channelName: "models.generateContent",
11127
+ kind: "async"
11128
+ }),
11129
+ generateContentStream: channel({
11130
+ channelName: "models.generateContentStream",
11131
+ kind: "async"
11132
+ })
11133
+ });
10501
11134
 
10502
- var GoogleGenAIPlugin = (_class20 = class extends BasePlugin {constructor(...args4) { super(...args4); _class20.prototype.__init65.call(this); }
10503
- __init65() {this.unsubscribers = []}
11135
+ // src/instrumentation/plugins/google-genai-plugin.ts
11136
+ var GoogleGenAIPlugin = class extends BasePlugin {
10504
11137
  onEnable() {
10505
11138
  this.subscribeToGoogleGenAIChannels();
10506
11139
  }
10507
11140
  onDisable() {
10508
- for (const unsubscribe of this.unsubscribers) {
10509
- unsubscribe();
10510
- }
10511
- this.unsubscribers = [];
11141
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
10512
11142
  }
10513
11143
  subscribeToGoogleGenAIChannels() {
10514
- this.subscribeToChannel("orchestrion:google-genai:models.generateContent", {
10515
- name: "google-genai.generateContent",
10516
- type: "llm" /* LLM */,
10517
- extractInput: (args) => {
10518
- const params = args[0] || {};
10519
- const input = serializeInput(params);
10520
- const metadata = extractMetadata(params);
10521
- return {
10522
- input,
10523
- metadata: { ...metadata, provider: "google-genai" }
10524
- };
10525
- },
10526
- extractOutput: (result) => {
10527
- return result;
10528
- },
10529
- extractMetrics: (result, startTime) => {
10530
- return extractGenerateContentMetrics(result, startTime);
10531
- }
10532
- });
10533
- this.subscribeToGoogleStreamingChannel(
10534
- "orchestrion:google-genai:models.generateContentStream",
10535
- {
10536
- name: "google-genai.generateContentStream",
11144
+ this.unsubscribers.push(
11145
+ traceAsyncChannel(googleGenAIChannels.generateContent, {
11146
+ name: "google-genai.generateContent",
10537
11147
  type: "llm" /* LLM */,
10538
- extractInput: (args) => {
10539
- const params = args[0] || {};
11148
+ extractInput: ([params]) => {
10540
11149
  const input = serializeInput(params);
10541
11150
  const metadata = extractMetadata(params);
10542
11151
  return {
@@ -10544,152 +11153,39 @@ var GoogleGenAIPlugin = (_class20 = class extends BasePlugin {constructor(...arg
10544
11153
  metadata: { ...metadata, provider: "google-genai" }
10545
11154
  };
10546
11155
  },
10547
- aggregateChunks: aggregateGenerateContentChunks
10548
- }
10549
- );
10550
- }
10551
- subscribeToChannel(channelName, config) {
10552
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
10553
- const spans = /* @__PURE__ */ new WeakMap();
10554
- const handlers = {
10555
- start: (event) => {
10556
- const span = startSpan({
10557
- name: config.name,
10558
- spanAttributes: {
10559
- type: config.type
10560
- }
10561
- });
10562
- const startTime = getCurrentUnixTimestamp();
10563
- spans.set(event, { span, startTime });
10564
- try {
10565
- const { input, metadata } = config.extractInput(event.arguments);
10566
- span.log({
10567
- input,
10568
- metadata
10569
- });
10570
- } catch (error) {
10571
- console.error(`Error extracting input for ${channelName}:`, error);
10572
- }
10573
- },
10574
- asyncEnd: (event) => {
10575
- const spanData = spans.get(event);
10576
- if (!spanData) {
10577
- return;
10578
- }
10579
- const { span, startTime } = spanData;
10580
- try {
10581
- const output = config.extractOutput(event.result);
10582
- const metrics = config.extractMetrics(event.result, startTime);
10583
- span.log({
10584
- output,
10585
- metrics
10586
- });
10587
- } catch (error) {
10588
- console.error(`Error extracting output for ${channelName}:`, error);
10589
- } finally {
10590
- span.end();
10591
- spans.delete(event);
10592
- }
10593
- },
10594
- error: (event) => {
10595
- const spanData = spans.get(event);
10596
- if (!spanData) {
10597
- return;
11156
+ extractOutput: (result) => {
11157
+ return result;
11158
+ },
11159
+ extractMetrics: (result, startTime) => {
11160
+ return extractGenerateContentMetrics(result, startTime);
10598
11161
  }
10599
- const { span } = spanData;
10600
- span.log({
10601
- error: event.error.message
10602
- });
10603
- span.end();
10604
- spans.delete(event);
10605
- }
10606
- };
10607
- channel.subscribe(handlers);
10608
- this.unsubscribers.push(() => {
10609
- channel.unsubscribe(handlers);
10610
- });
10611
- }
10612
- subscribeToGoogleStreamingChannel(channelName, config) {
10613
- const channel = _dcbrowser.tracingChannel.call(void 0, channelName);
10614
- const spans = /* @__PURE__ */ new WeakMap();
10615
- const handlers = {
10616
- start: (event) => {
10617
- const span = startSpan({
10618
- name: config.name,
10619
- spanAttributes: {
10620
- type: config.type
10621
- }
10622
- });
10623
- const startTime = getCurrentUnixTimestamp();
10624
- spans.set(event, { span, startTime });
10625
- try {
10626
- const { input, metadata } = config.extractInput(event.arguments);
10627
- span.log({
11162
+ })
11163
+ );
11164
+ this.unsubscribers.push(
11165
+ traceStreamingChannel(googleGenAIChannels.generateContentStream, {
11166
+ name: "google-genai.generateContentStream",
11167
+ type: "llm" /* LLM */,
11168
+ extractInput: ([params]) => {
11169
+ const input = serializeInput(params);
11170
+ const metadata = extractMetadata(params);
11171
+ return {
10628
11172
  input,
10629
- metadata
10630
- });
10631
- } catch (error) {
10632
- console.error(`Error extracting input for ${channelName}:`, error);
10633
- }
10634
- },
10635
- asyncEnd: (event) => {
10636
- const spanData = spans.get(event);
10637
- if (!spanData) {
10638
- return;
10639
- }
10640
- const { span, startTime } = spanData;
10641
- if (isAsyncIterable(event.result)) {
10642
- patchStreamIfNeeded(event.result, {
10643
- onComplete: (chunks) => {
10644
- try {
10645
- const { output, metrics } = config.aggregateChunks(
10646
- chunks,
10647
- startTime
10648
- );
10649
- span.log({
10650
- output,
10651
- metrics
10652
- });
10653
- } catch (error) {
10654
- console.error(
10655
- `Error extracting output for ${channelName}:`,
10656
- error
10657
- );
10658
- } finally {
10659
- span.end();
10660
- }
10661
- },
10662
- onError: (error) => {
10663
- span.log({
10664
- error: error.message
10665
- });
10666
- span.end();
10667
- }
10668
- });
10669
- } else {
10670
- span.end();
10671
- spans.delete(event);
10672
- }
10673
- },
10674
- error: (event) => {
10675
- const spanData = spans.get(event);
10676
- if (!spanData) {
10677
- return;
11173
+ metadata: { ...metadata, provider: "google-genai" }
11174
+ };
11175
+ },
11176
+ extractOutput: (result) => {
11177
+ return result;
11178
+ },
11179
+ extractMetrics: () => {
11180
+ return {};
11181
+ },
11182
+ aggregateChunks: (chunks, _result, _endEvent, startTime) => {
11183
+ return aggregateGenerateContentChunks(chunks, startTime);
10678
11184
  }
10679
- const { span } = spanData;
10680
- span.log({
10681
- error: event.error.message
10682
- });
10683
- span.end();
10684
- spans.delete(event);
10685
- }
10686
- };
10687
- channel.subscribe(handlers);
10688
- this.unsubscribers.push(() => {
10689
- channel.unsubscribe(handlers);
10690
- });
11185
+ })
11186
+ );
10691
11187
  }
10692
- }, _class20);
11188
+ };
10693
11189
  function serializeInput(params) {
10694
11190
  const input = {
10695
11191
  model: params.model,
@@ -10743,8 +11239,12 @@ function serializePart(part) {
10743
11239
  const buffer = typeof data === "string" ? typeof Buffer !== "undefined" ? Buffer.from(data, "base64") : new Uint8Array(
10744
11240
  atob(data).split("").map((c) => c.charCodeAt(0))
10745
11241
  ) : typeof Buffer !== "undefined" ? Buffer.from(data) : new Uint8Array(data);
11242
+ const arrayBuffer = buffer instanceof Uint8Array ? buffer.buffer.slice(
11243
+ buffer.byteOffset,
11244
+ buffer.byteOffset + buffer.byteLength
11245
+ ) : buffer;
10746
11246
  const attachment = new Attachment({
10747
- data: buffer,
11247
+ data: arrayBuffer,
10748
11248
  filename,
10749
11249
  contentType: mimeType || "application/octet-stream"
10750
11250
  });
@@ -10756,7 +11256,7 @@ function serializePart(part) {
10756
11256
  return part;
10757
11257
  }
10758
11258
  function serializeTools(params) {
10759
- if (!_optionalChain([params, 'access', _171 => _171.config, 'optionalAccess', _172 => _172.tools])) {
11259
+ if (!_optionalChain([params, 'access', _187 => _187.config, 'optionalAccess', _188 => _188.tools])) {
10760
11260
  return null;
10761
11261
  }
10762
11262
  try {
@@ -10793,33 +11293,36 @@ function extractGenerateContentMetrics(response, startTime) {
10793
11293
  const end = getCurrentUnixTimestamp();
10794
11294
  metrics.duration = end - startTime;
10795
11295
  }
10796
- if (response.usageMetadata) {
10797
- const usage = response.usageMetadata;
10798
- if (usage.promptTokenCount !== void 0) {
10799
- metrics.prompt_tokens = usage.promptTokenCount;
10800
- }
10801
- if (usage.candidatesTokenCount !== void 0) {
10802
- metrics.completion_tokens = usage.candidatesTokenCount;
10803
- }
10804
- if (usage.totalTokenCount !== void 0) {
10805
- metrics.tokens = usage.totalTokenCount;
10806
- }
10807
- if (usage.cachedContentTokenCount !== void 0) {
10808
- metrics.prompt_cached_tokens = usage.cachedContentTokenCount;
10809
- }
10810
- if (usage.thoughtsTokenCount !== void 0) {
10811
- metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
10812
- }
11296
+ if (_optionalChain([response, 'optionalAccess', _189 => _189.usageMetadata])) {
11297
+ populateUsageMetrics(metrics, response.usageMetadata);
10813
11298
  }
10814
11299
  return metrics;
10815
11300
  }
11301
+ function populateUsageMetrics(metrics, usage) {
11302
+ if (usage.promptTokenCount !== void 0) {
11303
+ metrics.prompt_tokens = usage.promptTokenCount;
11304
+ }
11305
+ if (usage.candidatesTokenCount !== void 0) {
11306
+ metrics.completion_tokens = usage.candidatesTokenCount;
11307
+ }
11308
+ if (usage.totalTokenCount !== void 0) {
11309
+ metrics.tokens = usage.totalTokenCount;
11310
+ }
11311
+ if (usage.cachedContentTokenCount !== void 0) {
11312
+ metrics.prompt_cached_tokens = usage.cachedContentTokenCount;
11313
+ }
11314
+ if (usage.thoughtsTokenCount !== void 0) {
11315
+ metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
11316
+ }
11317
+ }
10816
11318
  function aggregateGenerateContentChunks(chunks, startTime) {
10817
- const end = getCurrentUnixTimestamp();
10818
- const metrics = {
10819
- duration: end - startTime
10820
- };
11319
+ const metrics = {};
11320
+ if (startTime !== void 0) {
11321
+ const end = getCurrentUnixTimestamp();
11322
+ metrics.duration = end - startTime;
11323
+ }
10821
11324
  let firstTokenTime = null;
10822
- if (chunks.length > 0 && firstTokenTime === null) {
11325
+ if (chunks.length > 0 && firstTokenTime === null && startTime !== void 0) {
10823
11326
  firstTokenTime = getCurrentUnixTimestamp();
10824
11327
  metrics.time_to_first_token = firstTokenTime - startTime;
10825
11328
  }
@@ -10838,7 +11341,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
10838
11341
  }
10839
11342
  if (chunk.candidates && Array.isArray(chunk.candidates)) {
10840
11343
  for (const candidate of chunk.candidates) {
10841
- if (_optionalChain([candidate, 'access', _173 => _173.content, 'optionalAccess', _174 => _174.parts])) {
11344
+ if (_optionalChain([candidate, 'access', _190 => _190.content, 'optionalAccess', _191 => _191.parts])) {
10842
11345
  for (const part of candidate.content.parts) {
10843
11346
  if (part.text !== void 0) {
10844
11347
  if (part.thought) {
@@ -10869,7 +11372,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
10869
11372
  parts.push({ text });
10870
11373
  }
10871
11374
  parts.push(...otherParts);
10872
- if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _175 => _175.candidates])) {
11375
+ if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _192 => _192.candidates])) {
10873
11376
  const candidates = [];
10874
11377
  for (const candidate of lastResponse.candidates) {
10875
11378
  const candidateDict = {
@@ -10890,21 +11393,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
10890
11393
  }
10891
11394
  if (usageMetadata) {
10892
11395
  output.usageMetadata = usageMetadata;
10893
- if (usageMetadata.promptTokenCount !== void 0) {
10894
- metrics.prompt_tokens = usageMetadata.promptTokenCount;
10895
- }
10896
- if (usageMetadata.candidatesTokenCount !== void 0) {
10897
- metrics.completion_tokens = usageMetadata.candidatesTokenCount;
10898
- }
10899
- if (usageMetadata.totalTokenCount !== void 0) {
10900
- metrics.tokens = usageMetadata.totalTokenCount;
10901
- }
10902
- if (usageMetadata.cachedContentTokenCount !== void 0) {
10903
- metrics.prompt_cached_tokens = usageMetadata.cachedContentTokenCount;
10904
- }
10905
- if (usageMetadata.thoughtsTokenCount !== void 0) {
10906
- metrics.completion_reasoning_tokens = usageMetadata.thoughtsTokenCount;
10907
- }
11396
+ populateUsageMetrics(metrics, usageMetadata);
10908
11397
  }
10909
11398
  if (text) {
10910
11399
  output.text = text;
@@ -10916,7 +11405,8 @@ function tryToDict(obj) {
10916
11405
  return null;
10917
11406
  }
10918
11407
  if (typeof obj === "object") {
10919
- if (typeof obj.toJSON === "function") {
11408
+ if ("toJSON" in obj && // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
11409
+ typeof obj.toJSON === "function") {
10920
11410
  return obj.toJSON();
10921
11411
  }
10922
11412
  return obj;
@@ -10925,15 +11415,15 @@ function tryToDict(obj) {
10925
11415
  }
10926
11416
 
10927
11417
  // src/instrumentation/braintrust-plugin.ts
10928
- var BraintrustPlugin = (_class21 = class extends BasePlugin {
11418
+ var BraintrustPlugin = (_class18 = class extends BasePlugin {
10929
11419
 
10930
- __init66() {this.openaiPlugin = null}
10931
- __init67() {this.anthropicPlugin = null}
10932
- __init68() {this.aiSDKPlugin = null}
10933
- __init69() {this.claudeAgentSDKPlugin = null}
10934
- __init70() {this.googleGenAIPlugin = null}
11420
+ __init64() {this.openaiPlugin = null}
11421
+ __init65() {this.anthropicPlugin = null}
11422
+ __init66() {this.aiSDKPlugin = null}
11423
+ __init67() {this.claudeAgentSDKPlugin = null}
11424
+ __init68() {this.googleGenAIPlugin = null}
10935
11425
  constructor(config = {}) {
10936
- super();_class21.prototype.__init66.call(this);_class21.prototype.__init67.call(this);_class21.prototype.__init68.call(this);_class21.prototype.__init69.call(this);_class21.prototype.__init70.call(this);;
11426
+ super();_class18.prototype.__init64.call(this);_class18.prototype.__init65.call(this);_class18.prototype.__init66.call(this);_class18.prototype.__init67.call(this);_class18.prototype.__init68.call(this);;
10937
11427
  this.config = config;
10938
11428
  }
10939
11429
  onEnable() {
@@ -10981,13 +11471,13 @@ var BraintrustPlugin = (_class21 = class extends BasePlugin {
10981
11471
  this.googleGenAIPlugin = null;
10982
11472
  }
10983
11473
  }
10984
- }, _class21);
11474
+ }, _class18);
10985
11475
 
10986
11476
  // src/instrumentation/registry.ts
10987
- var PluginRegistry = (_class22 = class {constructor() { _class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this);_class22.prototype.__init73.call(this); }
10988
- __init71() {this.braintrustPlugin = null}
10989
- __init72() {this.config = {}}
10990
- __init73() {this.enabled = false}
11477
+ var PluginRegistry = (_class19 = class {constructor() { _class19.prototype.__init69.call(this);_class19.prototype.__init70.call(this);_class19.prototype.__init71.call(this); }
11478
+ __init69() {this.braintrustPlugin = null}
11479
+ __init70() {this.config = {}}
11480
+ __init71() {this.enabled = false}
10991
11481
  /**
10992
11482
  * Configure which integrations should be enabled.
10993
11483
  * This must be called before any SDK imports to take effect.
@@ -11069,7 +11559,7 @@ var PluginRegistry = (_class22 = class {constructor() { _class22.prototype.__ini
11069
11559
  }
11070
11560
  return { integrations };
11071
11561
  }
11072
- }, _class22);
11562
+ }, _class19);
11073
11563
  var registry = new PluginRegistry();
11074
11564
 
11075
11565
  // src/node/config.ts
@@ -11080,6 +11570,7 @@ function configureNode() {
11080
11570
  isomorph_default.getEnv = (name) => process.env[name];
11081
11571
  isomorph_default.getCallerLocation = getCallerLocation;
11082
11572
  isomorph_default.newAsyncLocalStorage = () => new (0, _nodeasync_hooks.AsyncLocalStorage)();
11573
+ isomorph_default.newTracingChannel = (nameOrChannels) => diagnostics_channel.tracingChannel(nameOrChannels);
11083
11574
  isomorph_default.processOn = (event, handler) => {
11084
11575
  process.on(event, handler);
11085
11576
  };
@@ -12283,11 +12774,11 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
12283
12774
  return this._state;
12284
12775
  }
12285
12776
  };
12286
- var CachedSpanFetcher = (_class23 = class {
12287
- __init74() {this.spanCache = /* @__PURE__ */ new Map()}
12288
- __init75() {this.allFetched = false}
12777
+ var CachedSpanFetcher = (_class20 = class {
12778
+ __init72() {this.spanCache = /* @__PURE__ */ new Map()}
12779
+ __init73() {this.allFetched = false}
12289
12780
 
12290
- constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class23.prototype.__init74.call(this);_class23.prototype.__init75.call(this);
12781
+ constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class20.prototype.__init72.call(this);_class20.prototype.__init73.call(this);
12291
12782
  if (typeof objectTypeOrFetchFn === "function") {
12292
12783
  this.fetchFn = objectTypeOrFetchFn;
12293
12784
  } else {
@@ -12302,7 +12793,7 @@ var CachedSpanFetcher = (_class23 = class {
12302
12793
  spanType
12303
12794
  );
12304
12795
  const rows = await fetcher.fetchedData();
12305
- return rows.filter((row) => _optionalChain([row, 'access', _176 => _176.span_attributes, 'optionalAccess', _177 => _177.purpose]) !== "scorer").map((row) => ({
12796
+ return rows.filter((row) => _optionalChain([row, 'access', _193 => _193.span_attributes, 'optionalAccess', _194 => _194.purpose]) !== "scorer").map((row) => ({
12306
12797
  input: row.input,
12307
12798
  output: row.output,
12308
12799
  metadata: row.metadata,
@@ -12336,7 +12827,7 @@ var CachedSpanFetcher = (_class23 = class {
12336
12827
  async fetchSpans(spanType) {
12337
12828
  const spans = await this.fetchFn(spanType);
12338
12829
  for (const span of spans) {
12339
- const type = _nullishCoalesce(_optionalChain([span, 'access', _178 => _178.span_attributes, 'optionalAccess', _179 => _179.type]), () => ( ""));
12830
+ const type = _nullishCoalesce(_optionalChain([span, 'access', _195 => _195.span_attributes, 'optionalAccess', _196 => _196.type]), () => ( ""));
12340
12831
  const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
12341
12832
  existing.push(span);
12342
12833
  this.spanCache.set(type, existing);
@@ -12353,24 +12844,24 @@ var CachedSpanFetcher = (_class23 = class {
12353
12844
  }
12354
12845
  return result;
12355
12846
  }
12356
- }, _class23);
12357
- var LocalTrace = (_class24 = class {
12847
+ }, _class20);
12848
+ var LocalTrace = (_class21 = class {
12358
12849
 
12359
12850
 
12360
12851
 
12361
12852
 
12362
12853
 
12363
- __init76() {this.spansFlushed = false}
12364
- __init77() {this.spansFlushPromise = null}
12854
+ __init74() {this.spansFlushed = false}
12855
+ __init75() {this.spansFlushPromise = null}
12365
12856
 
12366
- __init78() {this.threadCache = /* @__PURE__ */ new Map()}
12857
+ __init76() {this.threadCache = /* @__PURE__ */ new Map()}
12367
12858
  constructor({
12368
12859
  objectType,
12369
12860
  objectId,
12370
12861
  rootSpanId,
12371
12862
  ensureSpansFlushed,
12372
12863
  state
12373
- }) {;_class24.prototype.__init76.call(this);_class24.prototype.__init77.call(this);_class24.prototype.__init78.call(this);
12864
+ }) {;_class21.prototype.__init74.call(this);_class21.prototype.__init75.call(this);_class21.prototype.__init76.call(this);
12374
12865
  this.objectType = objectType;
12375
12866
  this.objectId = objectId;
12376
12867
  this.rootSpanId = rootSpanId;
@@ -12416,11 +12907,11 @@ var LocalTrace = (_class24 = class {
12416
12907
  const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
12417
12908
  if (cachedSpans && cachedSpans.length > 0) {
12418
12909
  let spans = cachedSpans.filter(
12419
- (span) => _optionalChain([span, 'access', _180 => _180.span_attributes, 'optionalAccess', _181 => _181.purpose]) !== "scorer"
12910
+ (span) => _optionalChain([span, 'access', _197 => _197.span_attributes, 'optionalAccess', _198 => _198.purpose]) !== "scorer"
12420
12911
  );
12421
12912
  if (spanType && spanType.length > 0) {
12422
12913
  spans = spans.filter(
12423
- (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _182 => _182.span_attributes, 'optionalAccess', _183 => _183.type]), () => ( "")))
12914
+ (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _199 => _199.span_attributes, 'optionalAccess', _200 => _200.type]), () => ( "")))
12424
12915
  );
12425
12916
  }
12426
12917
  return spans.map((span) => ({
@@ -12439,7 +12930,7 @@ var LocalTrace = (_class24 = class {
12439
12930
  * Calls the API with the project_default preprocessor (which falls back to "thread").
12440
12931
  */
12441
12932
  async getThread(options) {
12442
- const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _184 => _184.preprocessor]), () => ( "project_default"));
12933
+ const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _201 => _201.preprocessor]), () => ( "project_default"));
12443
12934
  if (!this.threadCache.has(cacheKey)) {
12444
12935
  const promise = this.fetchThread(options);
12445
12936
  this.threadCache.set(cacheKey, promise);
@@ -12450,7 +12941,7 @@ var LocalTrace = (_class24 = class {
12450
12941
  await this.ensureSpansReady();
12451
12942
  await this.state.login({});
12452
12943
  const result = await invoke({
12453
- globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _185 => _185.preprocessor]), () => ( "project_default")),
12944
+ globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _202 => _202.preprocessor]), () => ( "project_default")),
12454
12945
  functionType: "preprocessor",
12455
12946
  input: {
12456
12947
  trace_ref: {
@@ -12481,7 +12972,7 @@ var LocalTrace = (_class24 = class {
12481
12972
  }
12482
12973
  await this.spansFlushPromise;
12483
12974
  }
12484
- }, _class24);
12975
+ }, _class21);
12485
12976
 
12486
12977
  // src/reporters/progress.ts
12487
12978
  var SimpleProgressReporter = class {
@@ -12550,6 +13041,11 @@ var evalParametersSchema = _v3.z.record(
12550
13041
  default: promptDefinitionWithToolsSchema.optional(),
12551
13042
  description: _v3.z.string().optional()
12552
13043
  }),
13044
+ _v3.z.object({
13045
+ type: _v3.z.literal("model"),
13046
+ default: _v3.z.string().optional(),
13047
+ description: _v3.z.string().optional()
13048
+ }),
12553
13049
  _v3.z.instanceof(_v3.z.ZodType)
12554
13050
  // For Zod schemas
12555
13051
  ])
@@ -12592,6 +13088,17 @@ function validateParametersWithZod(parameters, parameterSchema) {
12592
13088
  throw new Error(`Parameter '${name}' is required`);
12593
13089
  }
12594
13090
  return [name, Prompt2.fromPromptData(name, promptData)];
13091
+ } else if ("type" in schema && schema.type === "model") {
13092
+ const model = _nullishCoalesce(value, () => ( schema.default));
13093
+ if (model === void 0) {
13094
+ throw new Error(`Parameter '${name}' is required`);
13095
+ }
13096
+ if (typeof model !== "string") {
13097
+ throw new Error(
13098
+ `Parameter '${name}' must be a string model identifier`
13099
+ );
13100
+ }
13101
+ return [name, model];
12595
13102
  } else {
12596
13103
  const schemaCasted = schema;
12597
13104
  return [name, schemaCasted.parse(value)];
@@ -12609,10 +13116,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
12609
13116
  const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
12610
13117
  const validate = ajv.compile(schema);
12611
13118
  if (!validate(parameters)) {
12612
- const errorMessages = _optionalChain([validate, 'access', _186 => _186.errors, 'optionalAccess', _187 => _187.map, 'call', _188 => _188((err) => {
13119
+ const errorMessages = _optionalChain([validate, 'access', _203 => _203.errors, 'optionalAccess', _204 => _204.map, 'call', _205 => _205((err) => {
12613
13120
  const path2 = err.instancePath || "root";
12614
13121
  return `${path2}: ${err.message}`;
12615
- }), 'access', _189 => _189.join, 'call', _190 => _190(", ")]);
13122
+ }), 'access', _206 => _206.join, 'call', _207 => _207(", ")]);
12616
13123
  throw Error(`Invalid parameters: ${errorMessages}`);
12617
13124
  }
12618
13125
  return parameters;
@@ -12666,10 +13173,10 @@ function callEvaluatorData(data) {
12666
13173
  };
12667
13174
  }
12668
13175
  function isAsyncIterable3(value) {
12669
- return typeof value === "object" && value !== null && typeof value[Symbol.asyncIterator] === "function";
13176
+ return typeof value === "object" && value !== null && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
12670
13177
  }
12671
13178
  function isIterable(value) {
12672
- return typeof value === "object" && value !== null && typeof value[Symbol.iterator] === "function";
13179
+ return typeof value === "object" && value !== null && Symbol.iterator in value && typeof value[Symbol.iterator] === "function";
12673
13180
  }
12674
13181
  globalThis._evals = {
12675
13182
  functions: [],
@@ -12832,7 +13339,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
12832
13339
  }
12833
13340
  async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
12834
13341
  if (enableCache) {
12835
- _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _191 => _191.spanCache, 'optionalAccess', _192 => _192.start, 'call', _193 => _193()]);
13342
+ _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _208 => _208.spanCache, 'optionalAccess', _209 => _209.start, 'call', _210 => _210()]);
12836
13343
  }
12837
13344
  try {
12838
13345
  if (typeof evaluator.data === "string") {
@@ -12943,7 +13450,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
12943
13450
  objectType: parentComponents ? spanObjectTypeV3ToTypedString(
12944
13451
  parentComponents.data.object_type
12945
13452
  ) : "experiment",
12946
- objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _194 => _194.data, 'access', async _195 => _195.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
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 () => ( "")) : ""))),
12947
13454
  rootSpanId: rootSpan.rootSpanId,
12948
13455
  ensureSpansFlushed,
12949
13456
  state
@@ -12969,10 +13476,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
12969
13476
  span,
12970
13477
  parameters: _nullishCoalesce(parameters, () => ( {})),
12971
13478
  reportProgress: (event) => {
12972
- _optionalChain([stream, 'optionalCall', _196 => _196({
13479
+ _optionalChain([stream, 'optionalCall', _213 => _213({
12973
13480
  ...event,
12974
13481
  id: rootSpan.id,
12975
- origin: _optionalChain([baseEvent, 'access', _197 => _197.event, 'optionalAccess', _198 => _198.origin]),
13482
+ origin: _optionalChain([baseEvent, 'access', _214 => _214.event, 'optionalAccess', _215 => _215.origin]),
12976
13483
  name: evaluator.evalName,
12977
13484
  object_type: "task"
12978
13485
  })]);
@@ -13105,7 +13612,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13105
13612
  const names = Object.keys(scorerErrors).join(", ");
13106
13613
  const errors = failingScorersAndResults.map((item) => item.error);
13107
13614
  unhandledScores = Object.keys(scorerErrors);
13108
- console.warn(
13615
+ debugLogger.forState(evaluator.state).warn(
13109
13616
  `Found exceptions for the following scorers: ${names}`,
13110
13617
  errors
13111
13618
  );
@@ -13136,7 +13643,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13136
13643
  metadata,
13137
13644
  scores: mergedScores,
13138
13645
  error,
13139
- origin: _optionalChain([baseEvent, 'access', _199 => _199.event, 'optionalAccess', _200 => _200.origin])
13646
+ origin: _optionalChain([baseEvent, 'access', _216 => _216.event, 'optionalAccess', _217 => _217.origin])
13140
13647
  });
13141
13648
  }
13142
13649
  };
@@ -13170,7 +13677,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13170
13677
  break;
13171
13678
  }
13172
13679
  scheduledTrials++;
13173
- _optionalChain([progressReporter, 'access', _201 => _201.setTotal, 'optionalCall', _202 => _202(evaluator.evalName, scheduledTrials)]);
13680
+ _optionalChain([progressReporter, 'access', _218 => _218.setTotal, 'optionalCall', _219 => _219(evaluator.evalName, scheduledTrials)]);
13174
13681
  q.push({ datum, trialIndex });
13175
13682
  }
13176
13683
  }
@@ -13223,7 +13730,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13223
13730
  q.kill();
13224
13731
  if (e instanceof InternalAbortError) {
13225
13732
  if (isomorph_default.getEnv("BRAINTRUST_VERBOSE")) {
13226
- console.warn("Evaluator cancelled:", e.message);
13733
+ debugLogger.forState(evaluator.state).warn("Evaluator cancelled:", e.message);
13227
13734
  }
13228
13735
  }
13229
13736
  throw e;
@@ -13245,9 +13752,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13245
13752
  );
13246
13753
  } finally {
13247
13754
  if (enableCache) {
13248
- const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _203 => _203.spanCache]);
13249
- _optionalChain([spanCache, 'optionalAccess', _204 => _204.dispose, 'call', _205 => _205()]);
13250
- _optionalChain([spanCache, 'optionalAccess', _206 => _206.stop, 'call', _207 => _207()]);
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()]);
13251
13758
  }
13252
13759
  }
13253
13760
  }
@@ -13318,7 +13825,11 @@ function reportFailures(evaluator, failingResults, { verbose, jsonl }) {
13318
13825
  }
13319
13826
  }
13320
13827
  if (!verbose && !jsonl) {
13321
- console.error(warning("Add --verbose to see full stack traces."));
13828
+ console.error(
13829
+ warning(
13830
+ "Use --debug-logging debug to see full stack traces and troubleshooting details."
13831
+ )
13832
+ );
13322
13833
  }
13323
13834
  }
13324
13835
  }
@@ -13480,7 +13991,7 @@ async function cachedLogin(options) {
13480
13991
  }
13481
13992
  function makeCheckAuthorized(allowedOrgName) {
13482
13993
  return async (req, _res, next) => {
13483
- if (!_optionalChain([req, 'access', _208 => _208.ctx, 'optionalAccess', _209 => _209.token])) {
13994
+ if (!_optionalChain([req, 'access', _225 => _225.ctx, 'optionalAccess', _226 => _226.token])) {
13484
13995
  return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
13485
13996
  }
13486
13997
  try {
@@ -13493,7 +14004,7 @@ function makeCheckAuthorized(allowedOrgName) {
13493
14004
  return next(_httperrors2.default.call(void 0, 403, errorMessage));
13494
14005
  }
13495
14006
  const state = await cachedLogin({
13496
- apiKey: _optionalChain([req, 'access', _210 => _210.ctx, 'optionalAccess', _211 => _211.token]),
14007
+ apiKey: _optionalChain([req, 'access', _227 => _227.ctx, 'optionalAccess', _228 => _228.token]),
13497
14008
  orgName
13498
14009
  });
13499
14010
  req.ctx.state = state;
@@ -13619,6 +14130,11 @@ var staticParametersSchema = _v3.z.record(
13619
14130
  default: PromptData.optional(),
13620
14131
  description: _v3.z.string().optional()
13621
14132
  }),
14133
+ _v3.z.object({
14134
+ type: _v3.z.literal("model"),
14135
+ default: _v3.z.string().optional(),
14136
+ description: _v3.z.string().optional()
14137
+ }),
13622
14138
  _v3.z.object({
13623
14139
  type: _v3.z.literal("data"),
13624
14140
  schema: _v3.z.record(_v3.z.unknown()),
@@ -13695,17 +14211,17 @@ var ProjectBuilder = class {
13695
14211
  }
13696
14212
  };
13697
14213
  var projects = new ProjectBuilder();
13698
- var Project2 = (_class25 = class {
14214
+ var Project2 = (_class22 = class {
13699
14215
 
13700
14216
 
13701
14217
 
13702
14218
 
13703
14219
 
13704
14220
 
13705
- __init79() {this._publishableCodeFunctions = []}
13706
- __init80() {this._publishablePrompts = []}
13707
- __init81() {this._publishableParameters = []}
13708
- constructor(args) {;_class25.prototype.__init79.call(this);_class25.prototype.__init80.call(this);_class25.prototype.__init81.call(this);
14221
+ __init77() {this._publishableCodeFunctions = []}
14222
+ __init78() {this._publishablePrompts = []}
14223
+ __init79() {this._publishableParameters = []}
14224
+ constructor(args) {;_class22.prototype.__init77.call(this);_class22.prototype.__init78.call(this);_class22.prototype.__init79.call(this);
13709
14225
  _initializeSpanContext();
13710
14226
  this.name = "name" in args ? args.name : void 0;
13711
14227
  this.id = "id" in args ? args.id : void 0;
@@ -13757,12 +14273,12 @@ var Project2 = (_class25 = class {
13757
14273
  functions: functionDefinitions
13758
14274
  });
13759
14275
  }
13760
- }, _class25);
13761
- var ToolBuilder = (_class26 = class {
13762
- constructor(project) {;_class26.prototype.__init82.call(this);
14276
+ }, _class22);
14277
+ var ToolBuilder = (_class23 = class {
14278
+ constructor(project) {;_class23.prototype.__init80.call(this);
13763
14279
  this.project = project;
13764
14280
  }
13765
- __init82() {this.taskCounter = 0}
14281
+ __init80() {this.taskCounter = 0}
13766
14282
  // This type definition is just a catch all so that the implementation can be
13767
14283
  // less specific than the two more specific declarations above.
13768
14284
  create(opts) {
@@ -13787,12 +14303,12 @@ var ToolBuilder = (_class26 = class {
13787
14303
  this.project.addCodeFunction(tool);
13788
14304
  return tool;
13789
14305
  }
13790
- }, _class26);
13791
- var ScorerBuilder = (_class27 = class {
13792
- constructor(project) {;_class27.prototype.__init83.call(this);
14306
+ }, _class23);
14307
+ var ScorerBuilder = (_class24 = class {
14308
+ constructor(project) {;_class24.prototype.__init81.call(this);
13793
14309
  this.project = project;
13794
14310
  }
13795
- __init83() {this.taskCounter = 0}
14311
+ __init81() {this.taskCounter = 0}
13796
14312
  create(opts) {
13797
14313
  this.taskCounter++;
13798
14314
  let resolvedName = opts.name;
@@ -13845,7 +14361,7 @@ var ScorerBuilder = (_class27 = class {
13845
14361
  this.project.addPrompt(codePrompt);
13846
14362
  }
13847
14363
  }
13848
- }, _class27);
14364
+ }, _class24);
13849
14365
  var CodeFunction = class {
13850
14366
  constructor(project, opts) {
13851
14367
  this.project = project;
@@ -13855,6 +14371,7 @@ var CodeFunction = class {
13855
14371
  this.description = opts.description;
13856
14372
  this.type = opts.type;
13857
14373
  this.ifExists = opts.ifExists;
14374
+ this.tags = opts.tags;
13858
14375
  this.metadata = opts.metadata;
13859
14376
  this.parameters = opts.parameters;
13860
14377
  this.returns = opts.returns;
@@ -13871,6 +14388,7 @@ var CodeFunction = class {
13871
14388
 
13872
14389
 
13873
14390
 
14391
+
13874
14392
  key() {
13875
14393
  return JSON.stringify([
13876
14394
  _nullishCoalesce(this.project.id, () => ( "")),
@@ -13890,6 +14408,7 @@ var CodePrompt = class {
13890
14408
 
13891
14409
 
13892
14410
 
14411
+
13893
14412
  constructor(project, prompt, toolFunctions, opts, functionType) {
13894
14413
  this.project = project;
13895
14414
  this.name = opts.name;
@@ -13900,6 +14419,7 @@ var CodePrompt = class {
13900
14419
  this.description = opts.description;
13901
14420
  this.id = opts.id;
13902
14421
  this.functionType = functionType;
14422
+ this.tags = opts.tags;
13903
14423
  this.metadata = opts.metadata;
13904
14424
  }
13905
14425
  async toFunctionDefinition(projectNameToId) {
@@ -13934,6 +14454,7 @@ var CodePrompt = class {
13934
14454
  function_type: this.functionType,
13935
14455
  prompt_data,
13936
14456
  if_exists: this.ifExists,
14457
+ tags: this.tags,
13937
14458
  metadata: this.metadata
13938
14459
  };
13939
14460
  }
@@ -13962,6 +14483,7 @@ var PromptBuilder = class {
13962
14483
  name: opts.name,
13963
14484
  slug,
13964
14485
  prompt_data: promptData,
14486
+ tags: opts.tags,
13965
14487
  ...this.project.id !== void 0 ? { project_id: this.project.id } : {}
13966
14488
  };
13967
14489
  const prompt = new Prompt2(
@@ -14043,6 +14565,15 @@ function serializeEvalParametersToStaticParametersSchema(parameters) {
14043
14565
  description: value.description
14044
14566
  }
14045
14567
  ];
14568
+ } else if ("type" in value && value.type === "model") {
14569
+ return [
14570
+ name,
14571
+ {
14572
+ type: "model",
14573
+ default: value.default,
14574
+ description: value.description
14575
+ }
14576
+ ];
14046
14577
  } else {
14047
14578
  const schemaObj = zodToJsonSchema(value);
14048
14579
  return [
@@ -14073,6 +14604,16 @@ function serializeEvalParameterstoParametersSchema(parameters) {
14073
14604
  if (!defaultPromptData) {
14074
14605
  required.push(name);
14075
14606
  }
14607
+ } else if ("type" in value && value.type === "model") {
14608
+ properties[name] = {
14609
+ type: "string",
14610
+ "x-bt-type": "model",
14611
+ ...value.description ? { description: value.description } : {},
14612
+ ..."default" in value ? { default: value.default } : {}
14613
+ };
14614
+ if (!("default" in value)) {
14615
+ required.push(name);
14616
+ }
14076
14617
  } else {
14077
14618
  const schemaObj = zodToJsonSchema(value);
14078
14619
  properties[name] = schemaObj;
@@ -14119,9 +14660,9 @@ function serializeRemoteEvalParametersContainer(parameters) {
14119
14660
  source: null
14120
14661
  };
14121
14662
  }
14122
- var ProjectNameIdMap = (_class28 = class {constructor() { _class28.prototype.__init84.call(this);_class28.prototype.__init85.call(this); }
14123
- __init84() {this.nameToId = {}}
14124
- __init85() {this.idToName = {}}
14663
+ var ProjectNameIdMap = (_class25 = class {constructor() { _class25.prototype.__init82.call(this);_class25.prototype.__init83.call(this); }
14664
+ __init82() {this.nameToId = {}}
14665
+ __init83() {this.idToName = {}}
14125
14666
  async getId(projectName) {
14126
14667
  if (!(projectName in this.nameToId)) {
14127
14668
  const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
@@ -14154,7 +14695,7 @@ var ProjectNameIdMap = (_class28 = class {constructor() { _class28.prototype.__i
14154
14695
  }
14155
14696
  return this.getId(project.name);
14156
14697
  }
14157
- }, _class28);
14698
+ }, _class25);
14158
14699
 
14159
14700
  // dev/server.ts
14160
14701
  function runDevServer(evaluators, opts) {
@@ -14226,7 +14767,7 @@ function runDevServer(evaluators, opts) {
14226
14767
  scores,
14227
14768
  stream
14228
14769
  } = evalBodySchema.parse(req.body);
14229
- if (!_optionalChain([req, 'access', _212 => _212.ctx, 'optionalAccess', _213 => _213.state])) {
14770
+ if (!_optionalChain([req, 'access', _229 => _229.ctx, 'optionalAccess', _230 => _230.state])) {
14230
14771
  res.status(500).json({ error: "Braintrust state not initialized in request" });
14231
14772
  return;
14232
14773
  }
@@ -14277,12 +14818,12 @@ function runDevServer(evaluators, opts) {
14277
14818
  ...evaluator,
14278
14819
  data: evalData.data,
14279
14820
  scores: evaluator.scores.concat(
14280
- _nullishCoalesce(_optionalChain([scores, 'optionalAccess', _214 => _214.map, 'call', _215 => _215(
14821
+ _nullishCoalesce(_optionalChain([scores, 'optionalAccess', _231 => _231.map, 'call', _232 => _232(
14281
14822
  (score) => makeScorer(
14282
14823
  state,
14283
14824
  score.name,
14284
14825
  score.function_id,
14285
- _optionalChain([req, 'access', _216 => _216.ctx, 'optionalAccess', _217 => _217.projectId])
14826
+ _optionalChain([req, 'access', _233 => _233.ctx, 'optionalAccess', _234 => _234.projectId])
14286
14827
  )
14287
14828
  )]), () => ( []))
14288
14829
  ),