braintrust 3.11.0 → 3.13.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 (77) hide show
  1. package/README.md +8 -8
  2. package/dev/dist/index.d.mts +26 -7
  3. package/dev/dist/index.d.ts +26 -7
  4. package/dev/dist/index.js +2717 -335
  5. package/dev/dist/index.mjs +2499 -117
  6. package/dist/apply-auto-instrumentation.browser.d.mts +2 -0
  7. package/dist/apply-auto-instrumentation.browser.d.ts +2 -0
  8. package/dist/apply-auto-instrumentation.browser.js +18 -0
  9. package/dist/apply-auto-instrumentation.browser.mjs +0 -0
  10. package/dist/apply-auto-instrumentation.d.mts +2 -0
  11. package/dist/apply-auto-instrumentation.d.ts +2 -0
  12. package/dist/apply-auto-instrumentation.js +2534 -0
  13. package/dist/apply-auto-instrumentation.mjs +2534 -0
  14. package/dist/auto-instrumentations/bundler/esbuild.cjs +1803 -1283
  15. package/dist/auto-instrumentations/bundler/esbuild.d.mts +9 -5
  16. package/dist/auto-instrumentations/bundler/esbuild.d.ts +9 -5
  17. package/dist/auto-instrumentations/bundler/esbuild.mjs +10 -2
  18. package/dist/auto-instrumentations/bundler/next.cjs +3269 -0
  19. package/dist/auto-instrumentations/bundler/next.d.mts +3 -0
  20. package/dist/auto-instrumentations/bundler/next.d.ts +3 -0
  21. package/dist/auto-instrumentations/bundler/next.mjs +189 -0
  22. package/dist/auto-instrumentations/bundler/rollup.cjs +1803 -1283
  23. package/dist/auto-instrumentations/bundler/rollup.d.mts +9 -5
  24. package/dist/auto-instrumentations/bundler/rollup.d.ts +9 -5
  25. package/dist/auto-instrumentations/bundler/rollup.mjs +10 -2
  26. package/dist/auto-instrumentations/bundler/vite.cjs +1803 -1283
  27. package/dist/auto-instrumentations/bundler/vite.d.mts +9 -5
  28. package/dist/auto-instrumentations/bundler/vite.d.ts +9 -5
  29. package/dist/auto-instrumentations/bundler/vite.mjs +10 -2
  30. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1861 -1308
  31. package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +3 -3
  32. package/dist/auto-instrumentations/bundler/webpack.cjs +1803 -1283
  33. package/dist/auto-instrumentations/bundler/webpack.d.mts +9 -5
  34. package/dist/auto-instrumentations/bundler/webpack.d.ts +9 -5
  35. package/dist/auto-instrumentations/bundler/webpack.mjs +6 -6
  36. package/dist/auto-instrumentations/{chunk-DIV5TO4S.mjs → chunk-E5DUYJWK.mjs} +338 -1
  37. package/dist/auto-instrumentations/chunk-GJOO4ESL.mjs +300 -0
  38. package/dist/auto-instrumentations/chunk-WFEUJACP.mjs +18 -0
  39. package/dist/auto-instrumentations/hook.mjs +1713 -1460
  40. package/dist/auto-instrumentations/index.cjs +94 -0
  41. package/dist/auto-instrumentations/index.d.mts +5 -1
  42. package/dist/auto-instrumentations/index.d.ts +5 -1
  43. package/dist/auto-instrumentations/index.mjs +6 -247
  44. package/dist/auto-instrumentations/loader/esm-hook.mjs +19 -2
  45. package/dist/auto-instrumentations/plugin-D7nDswtC.d.mts +44 -0
  46. package/dist/auto-instrumentations/plugin-D7nDswtC.d.ts +44 -0
  47. package/dist/browser.d.mts +264 -47
  48. package/dist/browser.d.ts +264 -47
  49. package/dist/browser.js +2521 -159
  50. package/dist/browser.mjs +2521 -159
  51. package/dist/chunk-26JGOELH.js +817 -0
  52. package/dist/chunk-75IQCUB2.mjs +817 -0
  53. package/dist/cli.js +2510 -122
  54. package/dist/edge-light.d.mts +1 -1
  55. package/dist/edge-light.d.ts +1 -1
  56. package/dist/edge-light.js +2521 -159
  57. package/dist/edge-light.mjs +2521 -159
  58. package/dist/index.d.mts +264 -47
  59. package/dist/index.d.ts +264 -47
  60. package/dist/index.js +3498 -1850
  61. package/dist/index.mjs +2635 -987
  62. package/dist/instrumentation/index.d.mts +7897 -48
  63. package/dist/instrumentation/index.d.ts +7897 -48
  64. package/dist/instrumentation/index.js +2408 -95
  65. package/dist/instrumentation/index.mjs +2407 -95
  66. package/dist/workerd.d.mts +1 -1
  67. package/dist/workerd.d.ts +1 -1
  68. package/dist/workerd.js +2521 -159
  69. package/dist/workerd.mjs +2521 -159
  70. package/package.json +23 -17
  71. package/util/dist/index.d.mts +3 -1
  72. package/util/dist/index.d.ts +3 -1
  73. package/util/dist/index.js +6 -0
  74. package/util/dist/index.mjs +6 -0
  75. package/dist/auto-instrumentations/chunk-G6ZWXGZB.mjs +0 -116
  76. package/dist/auto-instrumentations/plugin-Df3qKIl2.d.mts +0 -22
  77. package/dist/auto-instrumentations/plugin-Df3qKIl2.d.ts +0 -22
package/dev/dist/index.js CHANGED
@@ -1,4 +1,4 @@
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;// 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; var _class26; var _class27; var _class28; var _class29; var _class30; var _class31; var _class32;// src/node/config.ts
2
2
  var _nodeasync_hooks = require('node:async_hooks');
3
3
  var _nodediagnostics_channel = require('node:diagnostics_channel'); var diagnostics_channel = _interopRequireWildcard(_nodediagnostics_channel);
4
4
  var _nodepath = require('node:path'); var path = _interopRequireWildcard(_nodepath);
@@ -96,6 +96,7 @@ var _nodefs = require('node:fs'); var fsSync = _interopRequireWildcard(_nodefs);
96
96
  var _nodecrypto = require('node:crypto'); var crypto = _interopRequireWildcard(_nodecrypto);
97
97
  var _nodeutil = require('node:util');
98
98
  var _nodezlib = require('node:zlib'); var zlib = _interopRequireWildcard(_nodezlib);
99
+ var _dotenv = require('dotenv'); var dotenv = _interopRequireWildcard(_dotenv);
99
100
 
100
101
  // src/isomorph.ts
101
102
  var DefaultAsyncLocalStorage = class {
@@ -182,6 +183,7 @@ var iso = {
182
183
  getRepoInfo: async (_settings) => void 0,
183
184
  getPastNAncestors: async () => [],
184
185
  getEnv: (_name) => void 0,
186
+ getBraintrustApiKey: async () => void 0,
185
187
  getCallerLocation: () => void 0,
186
188
  newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
187
189
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -406,11 +408,11 @@ function truncateToByteLimit(s, byteLimit = 65536) {
406
408
  return new TextDecoder().decode(truncated);
407
409
  }
408
410
  async function getRepoInfo(settings) {
409
- if (settings && settings.collect === "none") {
411
+ if (!settings || settings.collect === "none") {
410
412
  return void 0;
411
413
  }
412
414
  const repo = await repoInfo();
413
- if (!repo || !settings || settings.collect === "all") {
415
+ if (!repo || settings.collect === "all") {
414
416
  return repo;
415
417
  }
416
418
  let sanitized = {};
@@ -1228,6 +1230,11 @@ function isPromiseLike(value) {
1228
1230
 
1229
1231
  // util/object_util.ts
1230
1232
  var SET_UNION_FIELDS = /* @__PURE__ */ new Set(["tags"]);
1233
+ var FORBIDDEN_MERGE_KEYS = /* @__PURE__ */ new Set([
1234
+ "__proto__",
1235
+ "constructor",
1236
+ "prototype"
1237
+ ]);
1231
1238
  function mergeDictsWithPaths({
1232
1239
  mergeInto,
1233
1240
  mergeFrom,
@@ -1250,6 +1257,7 @@ function mergeDictsWithPathsHelper({
1250
1257
  mergePaths
1251
1258
  }) {
1252
1259
  Object.entries(mergeFrom).forEach(([k, mergeFromV]) => {
1260
+ if (FORBIDDEN_MERGE_KEYS.has(k)) return;
1253
1261
  const fullPath = path2.concat([k]);
1254
1262
  const fullPathSerialized = JSON.stringify(fullPath);
1255
1263
  const mergeIntoV = recordFind(mergeInto, k);
@@ -5306,6 +5314,13 @@ var HTTPConnection = class _HTTPConnection {
5306
5314
  debugLogger.debug(
5307
5315
  `Retrying API request ${object_type} ${JSON.stringify(args)} ${e.status} ${e.text}`
5308
5316
  );
5317
+ const sleepTimeS = HTTP_RETRY_BASE_SLEEP_TIME_S * 2 ** i;
5318
+ debugLogger.info(
5319
+ `Sleeping for ${sleepTimeS}s before retrying API request`
5320
+ );
5321
+ await new Promise(
5322
+ (resolve) => setTimeout(resolve, sleepTimeS * 1e3)
5323
+ );
5309
5324
  continue;
5310
5325
  }
5311
5326
  throw e;
@@ -5828,20 +5843,7 @@ function startSpanParentArgs(args) {
5828
5843
  `Mismatch between expected span parent object type ${args.parentObjectType} and provided type ${parentComponents.data.object_type}`
5829
5844
  );
5830
5845
  }
5831
- const parentComponentsObjectIdLambda = spanComponentsToObjectIdLambda(
5832
- args.state,
5833
- parentComponents
5834
- );
5835
- const computeParentObjectId = async () => {
5836
- const parentComponentsObjectId = await parentComponentsObjectIdLambda();
5837
- if (await args.parentObjectId.get() !== parentComponentsObjectId) {
5838
- throw new Error(
5839
- `Mismatch between expected span parent object id ${await args.parentObjectId.get()} and provided id ${parentComponentsObjectId}`
5840
- );
5841
- }
5842
- return await args.parentObjectId.get();
5843
- };
5844
- argParentObjectId = new LazyValue(computeParentObjectId);
5846
+ argParentObjectId = args.parentObjectId;
5845
5847
  if (parentComponents.data.row_id) {
5846
5848
  argParentSpanIds = {
5847
5849
  spanId: parentComponents.data.span_id,
@@ -6170,6 +6172,7 @@ function now() {
6170
6172
  }
6171
6173
  var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
6172
6174
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
6175
+ var HTTP_RETRY_BASE_SLEEP_TIME_S = 1;
6173
6176
  var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
6174
6177
 
6175
6178
 
@@ -6800,17 +6803,10 @@ function init(projectOrOptions, optionalOptions) {
6800
6803
  if (repoInfo2) {
6801
6804
  return repoInfo2;
6802
6805
  }
6803
- let mergedGitMetadataSettings = {
6804
- ...state.gitMetadataSettings || {
6805
- collect: "all"
6806
- }
6807
- };
6808
- if (gitMetadataSettings) {
6809
- mergedGitMetadataSettings = mergeGitMetadataSettings(
6810
- mergedGitMetadataSettings,
6811
- gitMetadataSettings
6812
- );
6813
- }
6806
+ const mergedGitMetadataSettings = state.gitMetadataSettings == null ? _nullishCoalesce(gitMetadataSettings, () => ( { collect: "none" })) : mergeGitMetadataSettings(
6807
+ state.gitMetadataSettings,
6808
+ _nullishCoalesce(gitMetadataSettings, () => ( { collect: "all" }))
6809
+ );
6814
6810
  return await isomorph_default.getRepoInfo(mergedGitMetadataSettings);
6815
6811
  })();
6816
6812
  if (repoInfoArg) {
@@ -7192,6 +7188,55 @@ async function computeLoggerMetadata(state, {
7192
7188
  };
7193
7189
  }
7194
7190
  }
7191
+ function initLogger(options = {}) {
7192
+ const {
7193
+ projectName,
7194
+ projectId,
7195
+ asyncFlush: asyncFlushArg,
7196
+ appUrl,
7197
+ apiKey,
7198
+ orgName,
7199
+ forceLogin,
7200
+ debugLogLevel,
7201
+ fetch: fetch2,
7202
+ state: stateArg
7203
+ } = options || {};
7204
+ const asyncFlush = asyncFlushArg === void 0 ? true : asyncFlushArg;
7205
+ const computeMetadataArgs = {
7206
+ project_name: projectName,
7207
+ project_id: projectId
7208
+ };
7209
+ const linkArgs = {
7210
+ org_name: orgName,
7211
+ app_url: appUrl,
7212
+ project_name: projectName,
7213
+ project_id: projectId
7214
+ };
7215
+ const state = _nullishCoalesce(stateArg, () => ( _globalState));
7216
+ state.setDebugLogLevel(debugLogLevel);
7217
+ state.enforceQueueSizeLimit(true);
7218
+ const lazyMetadata = new LazyValue(
7219
+ async () => {
7220
+ await state.login({
7221
+ orgName,
7222
+ apiKey,
7223
+ appUrl,
7224
+ forceLogin,
7225
+ fetch: fetch2
7226
+ });
7227
+ return computeLoggerMetadata(state, computeMetadataArgs);
7228
+ }
7229
+ );
7230
+ const ret = new Logger(state, lazyMetadata, {
7231
+ asyncFlush,
7232
+ computeMetadataArgs,
7233
+ linkArgs
7234
+ });
7235
+ if (_nullishCoalesce(options.setCurrent, () => ( true))) {
7236
+ state.currentLogger = ret;
7237
+ }
7238
+ return ret;
7239
+ }
7195
7240
  async function login(options = {}) {
7196
7241
  const { forceLogin = false } = options || {};
7197
7242
  if (!_internalGetGlobalState()) {
@@ -7223,10 +7268,11 @@ async function login(options = {}) {
7223
7268
  async function loginToState(options = {}) {
7224
7269
  const {
7225
7270
  appUrl = isomorph_default.getEnv("BRAINTRUST_APP_URL") || "https://www.braintrust.dev",
7226
- apiKey = isomorph_default.getEnv("BRAINTRUST_API_KEY"),
7271
+ apiKey: apiKeyArg,
7227
7272
  orgName = isomorph_default.getEnv("BRAINTRUST_ORG_NAME"),
7228
7273
  fetch: fetch2 = globalThis.fetch
7229
7274
  } = options || {};
7275
+ const apiKey = apiKeyArg !== void 0 ? apiKeyArg : await isomorph_default.getBraintrustApiKey();
7230
7276
  const appPublicUrl = isomorph_default.getEnv("BRAINTRUST_APP_PUBLIC_URL") || appUrl;
7231
7277
  const state = new BraintrustState(options);
7232
7278
  state.resetLoginInfo();
@@ -8245,9 +8291,15 @@ var SpanImpl = (_class13 = class _SpanImpl {
8245
8291
  const cachedSpan = {
8246
8292
  input: partialRecord.input,
8247
8293
  output: partialRecord.output,
8294
+ expected: partialRecord.expected,
8295
+ error: partialRecord.error,
8296
+ scores: partialRecord.scores,
8297
+ metrics: partialRecord.metrics,
8248
8298
  metadata: partialRecord.metadata,
8299
+ tags: partialRecord.tags,
8249
8300
  span_id: this._spanId,
8250
8301
  span_parents: this._spanParents,
8302
+ is_root: this._spanId === this._rootSpanId,
8251
8303
  span_attributes: partialRecord.span_attributes
8252
8304
  };
8253
8305
  this._state.spanCache.queueWrite(
@@ -8583,6 +8635,7 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8583
8635
  metadata,
8584
8636
  tags,
8585
8637
  output,
8638
+ origin,
8586
8639
  isMerge
8587
8640
  }) {
8588
8641
  return new LazyValue(async () => {
@@ -8597,6 +8650,7 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8597
8650
  created: !isMerge ? (/* @__PURE__ */ new Date()).toISOString() : void 0,
8598
8651
  //if we're merging/updating an event we will not add this ts
8599
8652
  metadata,
8653
+ origin,
8600
8654
  ...!!isMerge ? {
8601
8655
  [IS_MERGE_FIELD]: true
8602
8656
  } : {}
@@ -8616,6 +8670,7 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8616
8670
  * about anything else that's relevant, that you can use to help find and analyze examples later. For example, you could log the
8617
8671
  * `prompt`, example's `id`, or anything else that would be useful to slice/dice later. The values in `metadata` can be any
8618
8672
  * JSON-serializable type, but its keys must be strings.
8673
+ * @param event.origin (Optional) a reference to the source object this dataset record was derived from.
8619
8674
  * @param event.id (Optional) a unique identifier for the event. If you don't provide one, Braintrust will generate one for you.
8620
8675
  * @param event.output: (Deprecated) The output of your application. Use `expected` instead.
8621
8676
  * @returns The `id` of the logged record.
@@ -8626,7 +8681,8 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8626
8681
  metadata,
8627
8682
  tags,
8628
8683
  id,
8629
- output
8684
+ output,
8685
+ origin
8630
8686
  }) {
8631
8687
  this.validateEvent({ metadata, expected, output, tags });
8632
8688
  const rowId = id || _uuid.v4.call(void 0, );
@@ -8638,6 +8694,7 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8638
8694
  metadata,
8639
8695
  tags,
8640
8696
  output,
8697
+ origin,
8641
8698
  isMerge: false
8642
8699
  })
8643
8700
  );
@@ -12877,11 +12934,11 @@ function resolveDenyOutputPaths(event, defaultDenyOutputPaths) {
12877
12934
  if (Array.isArray(_optionalChain([event, 'optionalAccess', _212 => _212.denyOutputPaths]))) {
12878
12935
  return event.denyOutputPaths;
12879
12936
  }
12880
- const firstArgument = _optionalChain([event, 'optionalAccess', _213 => _213.arguments]) && event.arguments.length > 0 ? event.arguments[0] : void 0;
12881
- if (!firstArgument || typeof firstArgument !== "object") {
12937
+ const firstArgument2 = _optionalChain([event, 'optionalAccess', _213 => _213.arguments]) && event.arguments.length > 0 ? event.arguments[0] : void 0;
12938
+ if (!firstArgument2 || typeof firstArgument2 !== "object") {
12882
12939
  return defaultDenyOutputPaths;
12883
12940
  }
12884
- const runtimeDenyOutputPaths = firstArgument[RUNTIME_DENY_OUTPUT_PATHS];
12941
+ const runtimeDenyOutputPaths = firstArgument2[RUNTIME_DENY_OUTPUT_PATHS];
12885
12942
  if (Array.isArray(runtimeDenyOutputPaths) && runtimeDenyOutputPaths.every((path2) => typeof path2 === "string")) {
12886
12943
  return runtimeDenyOutputPaths;
12887
12944
  }
@@ -16540,6 +16597,467 @@ function cleanMetrics2(metrics) {
16540
16597
  return cleaned;
16541
16598
  }
16542
16599
 
16600
+ // src/instrumentation/plugins/openai-agents-channels.ts
16601
+ var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
16602
+ onTraceStart: channel({
16603
+ channelName: "tracing.processor.onTraceStart",
16604
+ kind: "async"
16605
+ }),
16606
+ onTraceEnd: channel({
16607
+ channelName: "tracing.processor.onTraceEnd",
16608
+ kind: "async"
16609
+ }),
16610
+ onSpanStart: channel({
16611
+ channelName: "tracing.processor.onSpanStart",
16612
+ kind: "async"
16613
+ }),
16614
+ onSpanEnd: channel({
16615
+ channelName: "tracing.processor.onSpanEnd",
16616
+ kind: "async"
16617
+ })
16618
+ });
16619
+
16620
+ // src/instrumentation/plugins/openai-agents-trace-processor.ts
16621
+ function isSpanData(spanData, type) {
16622
+ return spanData.type === type;
16623
+ }
16624
+ function spanTypeFromAgents(span) {
16625
+ const spanType = span.spanData.type;
16626
+ if (spanType === "function" || spanType === "guardrail" || spanType === "mcp_tools") {
16627
+ return "tool" /* TOOL */;
16628
+ }
16629
+ if (spanType === "generation" || spanType === "response" || spanType === "transcription" || spanType === "speech") {
16630
+ return "llm" /* LLM */;
16631
+ }
16632
+ return "task" /* TASK */;
16633
+ }
16634
+ function spanNameFromAgents(span) {
16635
+ const spanData = span.spanData;
16636
+ if ("name" in spanData && spanData.name) {
16637
+ return spanData.name;
16638
+ }
16639
+ switch (spanData.type) {
16640
+ case "generation":
16641
+ return "Generation";
16642
+ case "response":
16643
+ return "Response";
16644
+ case "handoff":
16645
+ return "Handoff";
16646
+ case "mcp_tools":
16647
+ return isSpanData(spanData, "mcp_tools") && spanData.server ? `List Tools (${spanData.server})` : "MCP List Tools";
16648
+ case "transcription":
16649
+ return "Transcription";
16650
+ case "speech":
16651
+ return "Speech";
16652
+ case "speech_group":
16653
+ return "Speech Group";
16654
+ default:
16655
+ return "Unknown";
16656
+ }
16657
+ }
16658
+ function getTimeElapsed(end, start) {
16659
+ if (!start || !end) {
16660
+ return void 0;
16661
+ }
16662
+ const startTime = new Date(start).getTime();
16663
+ const endTime = new Date(end).getTime();
16664
+ if (Number.isNaN(startTime) || Number.isNaN(endTime)) {
16665
+ return void 0;
16666
+ }
16667
+ return (endTime - startTime) / 1e3;
16668
+ }
16669
+ function getNumberProperty2(obj, key) {
16670
+ if (!isObject(obj) || !(key in obj)) {
16671
+ return void 0;
16672
+ }
16673
+ const value = obj[key];
16674
+ return typeof value === "number" ? value : void 0;
16675
+ }
16676
+ function parseUsageMetrics(usage) {
16677
+ const metrics = {};
16678
+ if (!isObject(usage)) {
16679
+ return metrics;
16680
+ }
16681
+ const promptTokens = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(getNumberProperty2(usage, "prompt_tokens"), () => ( getNumberProperty2(usage, "input_tokens"))), () => ( getNumberProperty2(usage, "promptTokens"))), () => ( getNumberProperty2(usage, "inputTokens")));
16682
+ const completionTokens = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(getNumberProperty2(usage, "completion_tokens"), () => ( getNumberProperty2(usage, "output_tokens"))), () => ( getNumberProperty2(usage, "completionTokens"))), () => ( getNumberProperty2(usage, "outputTokens")));
16683
+ const totalTokens = _nullishCoalesce(getNumberProperty2(usage, "total_tokens"), () => ( getNumberProperty2(usage, "totalTokens")));
16684
+ if (promptTokens !== void 0) {
16685
+ metrics.prompt_tokens = promptTokens;
16686
+ }
16687
+ if (completionTokens !== void 0) {
16688
+ metrics.completion_tokens = completionTokens;
16689
+ }
16690
+ if (totalTokens !== void 0) {
16691
+ metrics.tokens = totalTokens;
16692
+ } else if (promptTokens !== void 0 && completionTokens !== void 0) {
16693
+ metrics.tokens = promptTokens + completionTokens;
16694
+ }
16695
+ const inputDetails = usage.input_tokens_details;
16696
+ const cachedTokens = getNumberProperty2(inputDetails, "cached_tokens");
16697
+ const cacheWriteTokens = getNumberProperty2(
16698
+ inputDetails,
16699
+ "cache_write_tokens"
16700
+ );
16701
+ if (cachedTokens !== void 0) {
16702
+ metrics.prompt_cached_tokens = cachedTokens;
16703
+ }
16704
+ if (cacheWriteTokens !== void 0) {
16705
+ metrics.prompt_cache_creation_tokens = cacheWriteTokens;
16706
+ }
16707
+ return metrics;
16708
+ }
16709
+ var OpenAIAgentsTraceProcessor = (_class19 = class _OpenAIAgentsTraceProcessor {
16710
+ static __initStatic() {this.DEFAULT_MAX_TRACES = 1e4}
16711
+
16712
+
16713
+ __init65() {this.traceSpans = /* @__PURE__ */ new Map()}
16714
+ __init66() {this.traceOrder = []}
16715
+ __init67() {this._traceSpans = this.traceSpans}
16716
+ constructor(options = {}) {;_class19.prototype.__init65.call(this);_class19.prototype.__init66.call(this);_class19.prototype.__init67.call(this);
16717
+ this.logger = options.logger;
16718
+ this.maxTraces = _nullishCoalesce(options.maxTraces, () => ( _OpenAIAgentsTraceProcessor.DEFAULT_MAX_TRACES));
16719
+ }
16720
+ evictOldestTrace() {
16721
+ const oldestTraceId = this.traceOrder.shift();
16722
+ if (oldestTraceId) {
16723
+ this.traceSpans.delete(oldestTraceId);
16724
+ }
16725
+ }
16726
+ onTraceStart(trace) {
16727
+ if (!_optionalChain([trace, 'optionalAccess', _368 => _368.traceId])) {
16728
+ return Promise.resolve();
16729
+ }
16730
+ if (this.traceOrder.length >= this.maxTraces) {
16731
+ this.evictOldestTrace();
16732
+ }
16733
+ const current = currentSpan();
16734
+ const span = current && current !== NOOP_SPAN ? current.startSpan({
16735
+ name: trace.name,
16736
+ type: "task" /* TASK */
16737
+ }) : this.logger ? this.logger.startSpan({
16738
+ name: trace.name,
16739
+ type: "task" /* TASK */
16740
+ }) : startSpan({
16741
+ name: trace.name,
16742
+ type: "task" /* TASK */
16743
+ });
16744
+ span.log({
16745
+ input: "Agent workflow started",
16746
+ metadata: {
16747
+ group_id: trace.groupId,
16748
+ ...trace.metadata || {}
16749
+ }
16750
+ });
16751
+ this.traceSpans.set(trace.traceId, {
16752
+ rootSpan: span,
16753
+ childSpans: /* @__PURE__ */ new Map(),
16754
+ metadata: {
16755
+ firstInput: null,
16756
+ lastOutput: null
16757
+ }
16758
+ });
16759
+ this.traceOrder.push(trace.traceId);
16760
+ return Promise.resolve();
16761
+ }
16762
+ async onTraceEnd(trace) {
16763
+ const traceData = this.traceSpans.get(_optionalChain([trace, 'optionalAccess', _369 => _369.traceId]));
16764
+ if (!traceData) {
16765
+ return;
16766
+ }
16767
+ try {
16768
+ traceData.rootSpan.log({
16769
+ input: traceData.metadata.firstInput,
16770
+ output: traceData.metadata.lastOutput
16771
+ });
16772
+ traceData.rootSpan.end();
16773
+ await traceData.rootSpan.flush();
16774
+ } finally {
16775
+ this.traceSpans.delete(trace.traceId);
16776
+ const orderIndex = this.traceOrder.indexOf(trace.traceId);
16777
+ if (orderIndex > -1) {
16778
+ this.traceOrder.splice(orderIndex, 1);
16779
+ }
16780
+ }
16781
+ }
16782
+ onSpanStart(span) {
16783
+ if (!_optionalChain([span, 'optionalAccess', _370 => _370.spanId]) || !span.traceId) {
16784
+ return Promise.resolve();
16785
+ }
16786
+ const traceData = this.traceSpans.get(span.traceId);
16787
+ if (!traceData) {
16788
+ return Promise.resolve();
16789
+ }
16790
+ const parentSpan = span.parentId ? traceData.childSpans.get(span.parentId) : traceData.rootSpan;
16791
+ if (!parentSpan) {
16792
+ return Promise.resolve();
16793
+ }
16794
+ const childSpan = parentSpan.startSpan({
16795
+ name: spanNameFromAgents(span),
16796
+ type: spanTypeFromAgents(span)
16797
+ });
16798
+ traceData.childSpans.set(span.spanId, childSpan);
16799
+ return Promise.resolve();
16800
+ }
16801
+ onSpanEnd(span) {
16802
+ if (!_optionalChain([span, 'optionalAccess', _371 => _371.spanId]) || !span.traceId) {
16803
+ return Promise.resolve();
16804
+ }
16805
+ const traceData = this.traceSpans.get(span.traceId);
16806
+ if (!traceData) {
16807
+ return Promise.resolve();
16808
+ }
16809
+ const braintrustSpan = traceData.childSpans.get(span.spanId);
16810
+ if (!braintrustSpan) {
16811
+ return Promise.resolve();
16812
+ }
16813
+ const logData = this.extractLogData(span);
16814
+ braintrustSpan.log({
16815
+ error: span.error,
16816
+ ...logData
16817
+ });
16818
+ braintrustSpan.end();
16819
+ traceData.childSpans.delete(span.spanId);
16820
+ const input = logData.input;
16821
+ const output = logData.output;
16822
+ if (traceData.metadata.firstInput === null && input != null) {
16823
+ traceData.metadata.firstInput = input;
16824
+ }
16825
+ if (output != null) {
16826
+ traceData.metadata.lastOutput = output;
16827
+ }
16828
+ return Promise.resolve();
16829
+ }
16830
+ async shutdown() {
16831
+ if (this.logger && typeof this.logger.flush === "function") {
16832
+ await this.logger.flush();
16833
+ }
16834
+ }
16835
+ async forceFlush() {
16836
+ if (this.logger && typeof this.logger.flush === "function") {
16837
+ await this.logger.flush();
16838
+ }
16839
+ }
16840
+ extractLogData(span) {
16841
+ const spanData = span.spanData;
16842
+ switch (spanData.type) {
16843
+ case "agent":
16844
+ return this.extractAgentLogData(spanData);
16845
+ case "response":
16846
+ return this.extractResponseLogData(spanData, span);
16847
+ case "function":
16848
+ return this.extractFunctionLogData(spanData);
16849
+ case "handoff":
16850
+ return this.extractHandoffLogData(spanData);
16851
+ case "guardrail":
16852
+ return this.extractGuardrailLogData(spanData);
16853
+ case "generation":
16854
+ return this.extractGenerationLogData(spanData, span);
16855
+ case "custom":
16856
+ return this.extractCustomLogData(spanData);
16857
+ case "mcp_tools":
16858
+ return this.extractMCPListToolsLogData(spanData);
16859
+ case "transcription":
16860
+ return this.extractTranscriptionLogData(spanData);
16861
+ case "speech":
16862
+ return this.extractSpeechLogData(spanData);
16863
+ case "speech_group":
16864
+ return this.extractSpeechGroupLogData(spanData);
16865
+ default:
16866
+ return {};
16867
+ }
16868
+ }
16869
+ extractAgentLogData(spanData) {
16870
+ return {
16871
+ metadata: {
16872
+ tools: spanData.tools,
16873
+ handoffs: spanData.handoffs,
16874
+ output_type: spanData.output_type
16875
+ }
16876
+ };
16877
+ }
16878
+ extractResponseLogData(spanData, span) {
16879
+ const response = spanData._response;
16880
+ const output = isObject(response) ? response.output : void 0;
16881
+ const usage = isObject(response) ? response.usage : void 0;
16882
+ const metrics = {
16883
+ ...this.extractTimingMetrics(span),
16884
+ ...parseUsageMetrics(usage)
16885
+ };
16886
+ return {
16887
+ input: spanData._input,
16888
+ output,
16889
+ metadata: isObject(response) ? this.omitKeys(response, ["output", "usage"]) : {},
16890
+ metrics
16891
+ };
16892
+ }
16893
+ extractFunctionLogData(spanData) {
16894
+ return {
16895
+ input: spanData.input,
16896
+ output: spanData.output
16897
+ };
16898
+ }
16899
+ extractHandoffLogData(spanData) {
16900
+ return {
16901
+ metadata: {
16902
+ from_agent: spanData.from_agent,
16903
+ to_agent: spanData.to_agent
16904
+ }
16905
+ };
16906
+ }
16907
+ extractGuardrailLogData(spanData) {
16908
+ return {
16909
+ metadata: {
16910
+ triggered: spanData.triggered
16911
+ }
16912
+ };
16913
+ }
16914
+ extractGenerationLogData(spanData, span) {
16915
+ return {
16916
+ input: spanData.input,
16917
+ output: spanData.output,
16918
+ metadata: {
16919
+ model: spanData.model,
16920
+ model_config: spanData.model_config
16921
+ },
16922
+ metrics: {
16923
+ ...this.extractTimingMetrics(span),
16924
+ ...parseUsageMetrics(spanData.usage)
16925
+ }
16926
+ };
16927
+ }
16928
+ extractCustomLogData(spanData) {
16929
+ return spanData.data || {};
16930
+ }
16931
+ extractMCPListToolsLogData(spanData) {
16932
+ return {
16933
+ output: spanData.result,
16934
+ metadata: {
16935
+ server: spanData.server
16936
+ }
16937
+ };
16938
+ }
16939
+ extractTranscriptionLogData(spanData) {
16940
+ return {
16941
+ input: spanData.input,
16942
+ output: spanData.output,
16943
+ metadata: {
16944
+ model: spanData.model,
16945
+ model_config: spanData.model_config
16946
+ }
16947
+ };
16948
+ }
16949
+ extractSpeechLogData(spanData) {
16950
+ return {
16951
+ input: spanData.input,
16952
+ output: spanData.output,
16953
+ metadata: {
16954
+ model: spanData.model,
16955
+ model_config: spanData.model_config
16956
+ }
16957
+ };
16958
+ }
16959
+ extractSpeechGroupLogData(spanData) {
16960
+ return {
16961
+ input: spanData.input
16962
+ };
16963
+ }
16964
+ extractTimingMetrics(span) {
16965
+ const timeToFirstToken = getTimeElapsed(
16966
+ _nullishCoalesce(span.endedAt, () => ( void 0)),
16967
+ _nullishCoalesce(span.startedAt, () => ( void 0))
16968
+ );
16969
+ return timeToFirstToken === void 0 ? {} : { time_to_first_token: timeToFirstToken };
16970
+ }
16971
+ omitKeys(value, keys) {
16972
+ const result = {};
16973
+ for (const [key, fieldValue] of Object.entries(value)) {
16974
+ if (!keys.includes(key)) {
16975
+ result[key] = fieldValue;
16976
+ }
16977
+ }
16978
+ return result;
16979
+ }
16980
+ }, _class19.__initStatic(), _class19);
16981
+
16982
+ // src/instrumentation/plugins/openai-agents-plugin.ts
16983
+ function firstArgument(args) {
16984
+ if (Array.isArray(args)) {
16985
+ return args[0];
16986
+ }
16987
+ if (isObject(args) && "length" in args && typeof args.length === "number" && Number.isInteger(args.length) && args.length >= 0) {
16988
+ return Array.from(args)[0];
16989
+ }
16990
+ return void 0;
16991
+ }
16992
+ function isOpenAIAgentsTrace(value) {
16993
+ return isObject(value) && value.type === "trace" && typeof value.traceId === "string";
16994
+ }
16995
+ function isOpenAIAgentsSpan(value) {
16996
+ return isObject(value) && value.type === "trace.span" && typeof value.traceId === "string" && typeof value.spanId === "string";
16997
+ }
16998
+ var OpenAIAgentsPlugin = (_class20 = class extends BasePlugin {constructor(...args4) { super(...args4); _class20.prototype.__init68.call(this); }
16999
+ __init68() {this.processor = new OpenAIAgentsTraceProcessor()}
17000
+ onEnable() {
17001
+ this.subscribeToTraceLifecycle();
17002
+ }
17003
+ onDisable() {
17004
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
17005
+ void this.processor.shutdown();
17006
+ }
17007
+ subscribeToTraceLifecycle() {
17008
+ const traceStartChannel = openAIAgentsCoreChannels.onTraceStart.tracingChannel();
17009
+ const traceStartHandlers = {
17010
+ start: (event) => {
17011
+ const trace = firstArgument(event.arguments);
17012
+ if (isOpenAIAgentsTrace(trace)) {
17013
+ void this.processor.onTraceStart(trace);
17014
+ }
17015
+ }
17016
+ };
17017
+ traceStartChannel.subscribe(traceStartHandlers);
17018
+ this.unsubscribers.push(
17019
+ () => traceStartChannel.unsubscribe(traceStartHandlers)
17020
+ );
17021
+ const traceEndChannel = openAIAgentsCoreChannels.onTraceEnd.tracingChannel();
17022
+ const traceEndHandlers = {
17023
+ start: (event) => {
17024
+ const trace = firstArgument(event.arguments);
17025
+ if (isOpenAIAgentsTrace(trace)) {
17026
+ void this.processor.onTraceEnd(trace);
17027
+ }
17028
+ }
17029
+ };
17030
+ traceEndChannel.subscribe(traceEndHandlers);
17031
+ this.unsubscribers.push(
17032
+ () => traceEndChannel.unsubscribe(traceEndHandlers)
17033
+ );
17034
+ const spanStartChannel = openAIAgentsCoreChannels.onSpanStart.tracingChannel();
17035
+ const spanStartHandlers = {
17036
+ start: (event) => {
17037
+ const span = firstArgument(event.arguments);
17038
+ if (isOpenAIAgentsSpan(span)) {
17039
+ void this.processor.onSpanStart(span);
17040
+ }
17041
+ }
17042
+ };
17043
+ spanStartChannel.subscribe(spanStartHandlers);
17044
+ this.unsubscribers.push(
17045
+ () => spanStartChannel.unsubscribe(spanStartHandlers)
17046
+ );
17047
+ const spanEndChannel = openAIAgentsCoreChannels.onSpanEnd.tracingChannel();
17048
+ const spanEndHandlers = {
17049
+ start: (event) => {
17050
+ const span = firstArgument(event.arguments);
17051
+ if (isOpenAIAgentsSpan(span)) {
17052
+ void this.processor.onSpanEnd(span);
17053
+ }
17054
+ }
17055
+ };
17056
+ spanEndChannel.subscribe(spanEndHandlers);
17057
+ this.unsubscribers.push(() => spanEndChannel.unsubscribe(spanEndHandlers));
17058
+ }
17059
+ }, _class20);
17060
+
16543
17061
  // src/instrumentation/plugins/google-genai-channels.ts
16544
17062
  var googleGenAIChannels = defineChannels("@google/genai", {
16545
17063
  generateContent: channel({
@@ -16651,7 +17169,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
16651
17169
  };
16652
17170
  tracingChannel2.subscribe(handlers);
16653
17171
  this.unsubscribers.push(() => {
16654
- _optionalChain([unbindCurrentSpanStore, 'optionalCall', _368 => _368()]);
17172
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _372 => _372()]);
16655
17173
  tracingChannel2.unsubscribe(handlers);
16656
17174
  });
16657
17175
  }
@@ -16748,7 +17266,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
16748
17266
  };
16749
17267
  tracingChannel2.subscribe(handlers);
16750
17268
  this.unsubscribers.push(() => {
16751
- _optionalChain([unbindCurrentSpanStore, 'optionalCall', _369 => _369()]);
17269
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _373 => _373()]);
16752
17270
  tracingChannel2.unsubscribe(handlers);
16753
17271
  });
16754
17272
  }
@@ -16764,10 +17282,10 @@ function ensureSpanState(states, event, create) {
16764
17282
  }
16765
17283
  function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
16766
17284
  const state = _internalGetGlobalState();
16767
- const contextManager = _optionalChain([state, 'optionalAccess', _370 => _370.contextManager]);
17285
+ const contextManager = _optionalChain([state, 'optionalAccess', _374 => _374.contextManager]);
16768
17286
  const startChannel = tracingChannel2.start;
16769
17287
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
16770
- if (!_optionalChain([startChannel, 'optionalAccess', _371 => _371.bindStore]) || !currentSpanStore) {
17288
+ if (!_optionalChain([startChannel, 'optionalAccess', _375 => _375.bindStore]) || !currentSpanStore) {
16771
17289
  return void 0;
16772
17290
  }
16773
17291
  startChannel.bindStore(currentSpanStore, (event) => {
@@ -16779,7 +17297,7 @@ function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
16779
17297
  return contextManager.wrapSpanForStore(span);
16780
17298
  });
16781
17299
  return () => {
16782
- _optionalChain([startChannel, 'access', _372 => _372.unbindStore, 'optionalCall', _373 => _373(currentSpanStore)]);
17300
+ _optionalChain([startChannel, 'access', _376 => _376.unbindStore, 'optionalCall', _377 => _377(currentSpanStore)]);
16783
17301
  };
16784
17302
  }
16785
17303
  function logErrorAndEndSpan(states, event) {
@@ -17014,7 +17532,7 @@ function serializePart(part) {
17014
17532
  }
17015
17533
  function serializeGenerateContentTools(params) {
17016
17534
  const config = params.config ? tryToDict(params.config) : null;
17017
- const tools = _optionalChain([config, 'optionalAccess', _374 => _374.tools]);
17535
+ const tools = _optionalChain([config, 'optionalAccess', _378 => _378.tools]);
17018
17536
  if (!Array.isArray(tools)) {
17019
17537
  return null;
17020
17538
  }
@@ -17073,7 +17591,7 @@ function extractGenerateContentMetrics(response, startTime) {
17073
17591
  metrics.end = end;
17074
17592
  metrics.duration = end - startTime;
17075
17593
  }
17076
- if (_optionalChain([response, 'optionalAccess', _375 => _375.usageMetadata])) {
17594
+ if (_optionalChain([response, 'optionalAccess', _379 => _379.usageMetadata])) {
17077
17595
  populateUsageMetrics(metrics, response.usageMetadata);
17078
17596
  }
17079
17597
  return metrics;
@@ -17086,7 +17604,7 @@ function extractEmbedContentMetrics(response, startTime) {
17086
17604
  metrics.end = end;
17087
17605
  metrics.duration = end - startTime;
17088
17606
  }
17089
- if (_optionalChain([response, 'optionalAccess', _376 => _376.usageMetadata])) {
17607
+ if (_optionalChain([response, 'optionalAccess', _380 => _380.usageMetadata])) {
17090
17608
  populateUsageMetrics(metrics, response.usageMetadata);
17091
17609
  }
17092
17610
  const embeddingTokenCount = extractEmbedPromptTokenCount(response);
@@ -17100,11 +17618,11 @@ function extractEmbedPromptTokenCount(response) {
17100
17618
  if (!response) {
17101
17619
  return void 0;
17102
17620
  }
17103
- const usagePromptTokens = _optionalChain([response, 'access', _377 => _377.usageMetadata, 'optionalAccess', _378 => _378.promptTokenCount]);
17621
+ const usagePromptTokens = _optionalChain([response, 'access', _381 => _381.usageMetadata, 'optionalAccess', _382 => _382.promptTokenCount]);
17104
17622
  if (typeof usagePromptTokens === "number" && Number.isFinite(usagePromptTokens)) {
17105
17623
  return usagePromptTokens;
17106
17624
  }
17107
- const usageTotalTokens = _optionalChain([response, 'access', _379 => _379.usageMetadata, 'optionalAccess', _380 => _380.totalTokenCount]);
17625
+ const usageTotalTokens = _optionalChain([response, 'access', _383 => _383.usageMetadata, 'optionalAccess', _384 => _384.totalTokenCount]);
17108
17626
  if (typeof usageTotalTokens === "number" && Number.isFinite(usageTotalTokens)) {
17109
17627
  return usageTotalTokens;
17110
17628
  }
@@ -17115,8 +17633,8 @@ function extractEmbedPromptTokenCount(response) {
17115
17633
  let total = 0;
17116
17634
  let sawAny = false;
17117
17635
  for (const embedding of embeddings) {
17118
- const embeddingStats = tryToDict(_optionalChain([tryToDict, 'call', _381 => _381(embedding), 'optionalAccess', _382 => _382.statistics]));
17119
- const tokenCount = _optionalChain([embeddingStats, 'optionalAccess', _383 => _383.tokenCount]);
17636
+ const embeddingStats = tryToDict(_optionalChain([tryToDict, 'call', _385 => _385(embedding), 'optionalAccess', _386 => _386.statistics]));
17637
+ const tokenCount = _optionalChain([embeddingStats, 'optionalAccess', _387 => _387.tokenCount]);
17120
17638
  if (typeof tokenCount === "number" && Number.isFinite(tokenCount)) {
17121
17639
  total += tokenCount;
17122
17640
  sawAny = true;
@@ -17132,7 +17650,7 @@ function summarizeEmbedContentOutput(response) {
17132
17650
  if (embeddings.length === 0) {
17133
17651
  return void 0;
17134
17652
  }
17135
- const firstValues = _optionalChain([embeddings, 'access', _384 => _384[0], 'optionalAccess', _385 => _385.values]);
17653
+ const firstValues = _optionalChain([embeddings, 'access', _388 => _388[0], 'optionalAccess', _389 => _389.values]);
17136
17654
  if (!Array.isArray(firstValues)) {
17137
17655
  return void 0;
17138
17656
  }
@@ -17187,7 +17705,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
17187
17705
  }
17188
17706
  if (chunk.candidates && Array.isArray(chunk.candidates)) {
17189
17707
  for (const candidate of chunk.candidates) {
17190
- if (_optionalChain([candidate, 'access', _386 => _386.content, 'optionalAccess', _387 => _387.parts])) {
17708
+ if (_optionalChain([candidate, 'access', _390 => _390.content, 'optionalAccess', _391 => _391.parts])) {
17191
17709
  for (const part of candidate.content.parts) {
17192
17710
  if (part.text !== void 0) {
17193
17711
  if (part.thought) {
@@ -17218,7 +17736,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
17218
17736
  parts.push({ text });
17219
17737
  }
17220
17738
  parts.push(...otherParts);
17221
- if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _388 => _388.candidates])) {
17739
+ if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _392 => _392.candidates])) {
17222
17740
  const candidates = [];
17223
17741
  for (const candidate of lastResponse.candidates) {
17224
17742
  const candidateDict = {
@@ -17275,7 +17793,7 @@ function extractResponseMetadata(response) {
17275
17793
  if (Array.isArray(responseDict.candidates)) {
17276
17794
  for (const candidate of responseDict.candidates) {
17277
17795
  const candidateDict = tryToDict(candidate);
17278
- if (_optionalChain([candidateDict, 'optionalAccess', _389 => _389.groundingMetadata]) !== void 0) {
17796
+ if (_optionalChain([candidateDict, 'optionalAccess', _393 => _393.groundingMetadata]) !== void 0) {
17279
17797
  candidateGroundingMetadata.push(candidateDict.groundingMetadata);
17280
17798
  }
17281
17799
  }
@@ -17356,9 +17874,9 @@ var HuggingFacePlugin = class extends BasePlugin {
17356
17874
  name: "huggingface.chat_completion",
17357
17875
  type: "llm" /* LLM */,
17358
17876
  extractInput: extractChatInputWithMetadata,
17359
- extractOutput: (result) => _optionalChain([result, 'optionalAccess', _390 => _390.choices]),
17877
+ extractOutput: (result) => _optionalChain([result, 'optionalAccess', _394 => _394.choices]),
17360
17878
  extractMetadata: (result) => extractResponseMetadata2(result),
17361
- extractMetrics: (result) => parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _391 => _391.usage]))
17879
+ extractMetrics: (result) => parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _395 => _395.usage]))
17362
17880
  }),
17363
17881
  traceSyncStreamChannel(huggingFaceChannels.chatCompletionStream, {
17364
17882
  name: "huggingface.chat_completion_stream",
@@ -17376,7 +17894,7 @@ var HuggingFacePlugin = class extends BasePlugin {
17376
17894
  extractInput: extractTextGenerationInputWithMetadata,
17377
17895
  extractOutput: (result) => isObject(result) ? { generated_text: result.generated_text } : result,
17378
17896
  extractMetadata: extractTextGenerationMetadata,
17379
- extractMetrics: (result) => extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _392 => _392.details]), () => ( null)))
17897
+ extractMetrics: (result) => extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _396 => _396.details]), () => ( null)))
17380
17898
  }),
17381
17899
  traceSyncStreamChannel(huggingFaceChannels.textGenerationStream, {
17382
17900
  name: "huggingface.text_generation_stream",
@@ -17495,7 +18013,7 @@ function extractTextGenerationMetrics(details) {
17495
18013
  return metrics;
17496
18014
  }
17497
18015
  function extractTextGenerationMetadata(result) {
17498
- if (!isObject(_optionalChain([result, 'optionalAccess', _393 => _393.details]))) {
18016
+ if (!isObject(_optionalChain([result, 'optionalAccess', _397 => _397.details]))) {
17499
18017
  return void 0;
17500
18018
  }
17501
18019
  return typeof result.details.finish_reason === "string" ? {
@@ -17541,7 +18059,7 @@ function patchChatCompletionStream(args) {
17541
18059
  const lastChunk = chunks.at(-1);
17542
18060
  const responseMetadata = extractResponseMetadata2(lastChunk);
17543
18061
  const metrics = {
17544
- ...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _394 => _394.usage])),
18062
+ ...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _398 => _398.usage])),
17545
18063
  ...firstChunkTime !== void 0 ? { time_to_first_token: firstChunkTime - startTime } : {}
17546
18064
  };
17547
18065
  span.log({
@@ -17579,8 +18097,8 @@ function patchTextGenerationStream(args) {
17579
18097
  output: aggregateTextGenerationStreamChunks(chunks),
17580
18098
  ...streamMetadata ? { metadata: streamMetadata } : {},
17581
18099
  metrics: {
17582
- ...extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([lastChunk, 'optionalAccess', _395 => _395.details]), () => ( null))),
17583
- ...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _396 => _396.usage])),
18100
+ ...extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([lastChunk, 'optionalAccess', _399 => _399.details]), () => ( null))),
18101
+ ...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _400 => _400.usage])),
17584
18102
  ...firstChunkTime !== void 0 ? { time_to_first_token: firstChunkTime - startTime } : {}
17585
18103
  }
17586
18104
  });
@@ -17609,14 +18127,14 @@ function aggregateChatCompletionChunks2(chunks) {
17609
18127
  }));
17610
18128
  const delta = isObject(choice.delta) ? choice.delta : void 0;
17611
18129
  const message = isObject(choice.message) ? choice.message : void 0;
17612
- if (typeof _optionalChain([delta, 'optionalAccess', _397 => _397.content]) === "string") {
18130
+ if (typeof _optionalChain([delta, 'optionalAccess', _401 => _401.content]) === "string") {
17613
18131
  existing.content += delta.content;
17614
- } else if (typeof _optionalChain([message, 'optionalAccess', _398 => _398.content]) === "string") {
18132
+ } else if (typeof _optionalChain([message, 'optionalAccess', _402 => _402.content]) === "string") {
17615
18133
  existing.content = message.content;
17616
18134
  }
17617
- if (typeof _optionalChain([delta, 'optionalAccess', _399 => _399.role]) === "string") {
18135
+ if (typeof _optionalChain([delta, 'optionalAccess', _403 => _403.role]) === "string") {
17618
18136
  existing.role = delta.role;
17619
- } else if (typeof _optionalChain([message, 'optionalAccess', _400 => _400.role]) === "string") {
18137
+ } else if (typeof _optionalChain([message, 'optionalAccess', _404 => _404.role]) === "string") {
17620
18138
  existing.role = message.role;
17621
18139
  }
17622
18140
  if (choice.finish_reason !== void 0) {
@@ -17644,7 +18162,7 @@ function aggregateChatCompletionChunks2(chunks) {
17644
18162
  };
17645
18163
  }
17646
18164
  function getChatToolCallDeltas(value) {
17647
- if (!Array.isArray(_optionalChain([value, 'optionalAccess', _401 => _401.tool_calls]))) {
18165
+ if (!Array.isArray(_optionalChain([value, 'optionalAccess', _405 => _405.tool_calls]))) {
17648
18166
  return void 0;
17649
18167
  }
17650
18168
  const toolCalls = value.tool_calls.filter((toolCall) => isObject(toolCall));
@@ -17709,7 +18227,7 @@ function aggregateTextGenerationStreamChunks(chunks) {
17709
18227
  for (const chunk of chunks) {
17710
18228
  if (typeof chunk.generated_text === "string") {
17711
18229
  generatedText = chunk.generated_text;
17712
- } else if (typeof _optionalChain([chunk, 'access', _402 => _402.token, 'optionalAccess', _403 => _403.text]) === "string" && !chunk.token.special) {
18230
+ } else if (typeof _optionalChain([chunk, 'access', _406 => _406.token, 'optionalAccess', _407 => _407.text]) === "string" && !chunk.token.special) {
17713
18231
  generatedText += chunk.token.text;
17714
18232
  } else if (Array.isArray(chunk.choices)) {
17715
18233
  for (const choice of chunk.choices) {
@@ -17733,17 +18251,17 @@ function aggregateTextGenerationStreamChunks(chunks) {
17733
18251
  function extractTextGenerationStreamMetadata(chunks) {
17734
18252
  for (let index = chunks.length - 1; index >= 0; index--) {
17735
18253
  const chunk = chunks[index];
17736
- if (isObject(_optionalChain([chunk, 'optionalAccess', _404 => _404.details])) && typeof chunk.details.finish_reason === "string") {
18254
+ if (isObject(_optionalChain([chunk, 'optionalAccess', _408 => _408.details])) && typeof chunk.details.finish_reason === "string") {
17737
18255
  return {
17738
18256
  finish_reason: chunk.details.finish_reason
17739
18257
  };
17740
18258
  }
17741
- if (!Array.isArray(_optionalChain([chunk, 'optionalAccess', _405 => _405.choices]))) {
18259
+ if (!Array.isArray(_optionalChain([chunk, 'optionalAccess', _409 => _409.choices]))) {
17742
18260
  continue;
17743
18261
  }
17744
18262
  for (let choiceIndex = chunk.choices.length - 1; choiceIndex >= 0; choiceIndex--) {
17745
18263
  const choice = chunk.choices[choiceIndex];
17746
- if (_optionalChain([choice, 'optionalAccess', _406 => _406.finish_reason]) !== void 0) {
18264
+ if (_optionalChain([choice, 'optionalAccess', _410 => _410.finish_reason]) !== void 0) {
17747
18265
  return { finish_reason: choice.finish_reason };
17748
18266
  }
17749
18267
  }
@@ -17819,14 +18337,14 @@ var OpenRouterAgentPlugin = class extends BasePlugin {
17819
18337
  extractMetadata: (result, event) => {
17820
18338
  if (!isObject(result)) {
17821
18339
  return {
17822
- step: _optionalChain([event, 'optionalAccess', _407 => _407.step]),
17823
- step_type: _optionalChain([event, 'optionalAccess', _408 => _408.stepType])
18340
+ step: _optionalChain([event, 'optionalAccess', _411 => _411.step]),
18341
+ step_type: _optionalChain([event, 'optionalAccess', _412 => _412.stepType])
17824
18342
  };
17825
18343
  }
17826
18344
  return {
17827
18345
  ...extractOpenRouterResponseMetadata(result) || {},
17828
- ..._optionalChain([event, 'optionalAccess', _409 => _409.step]) !== void 0 ? { step: event.step } : {},
17829
- ..._optionalChain([event, 'optionalAccess', _410 => _410.stepType]) ? { step_type: event.stepType } : {}
18346
+ ..._optionalChain([event, 'optionalAccess', _413 => _413.step]) !== void 0 ? { step: event.step } : {},
18347
+ ..._optionalChain([event, 'optionalAccess', _414 => _414.stepType]) ? { step_type: event.stepType } : {}
17830
18348
  };
17831
18349
  },
17832
18350
  extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage(result.usage) : {}
@@ -18187,7 +18705,7 @@ function publishToolResult(tracingChannel2, event, result) {
18187
18705
  }
18188
18706
  function getToolCallId(context) {
18189
18707
  const toolContext = context;
18190
- return typeof _optionalChain([toolContext, 'optionalAccess', _411 => _411.toolCall, 'optionalAccess', _412 => _412.id]) === "string" ? toolContext.toolCall.id : void 0;
18708
+ return typeof _optionalChain([toolContext, 'optionalAccess', _415 => _415.toolCall, 'optionalAccess', _416 => _416.id]) === "string" ? toolContext.toolCall.id : void 0;
18191
18709
  }
18192
18710
  var OPENROUTER_WRAPPED_CALL_MODEL_RESULT = /* @__PURE__ */ Symbol(
18193
18711
  "braintrust.openrouter.wrappedCallModelResult"
@@ -18585,9 +19103,9 @@ var OpenRouterPlugin = class extends BasePlugin {
18585
19103
  type: "llm" /* LLM */,
18586
19104
  extractInput: (args) => {
18587
19105
  const request = getOpenRouterRequestArg(args);
18588
- const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _413 => _413.chatGenerationParams])) ? request.chatGenerationParams : {};
18589
- const httpReferer = _optionalChain([request, 'optionalAccess', _414 => _414.httpReferer]);
18590
- const xTitle = _optionalChain([request, 'optionalAccess', _415 => _415.xTitle]);
19106
+ const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _417 => _417.chatGenerationParams])) ? request.chatGenerationParams : {};
19107
+ const httpReferer = _optionalChain([request, 'optionalAccess', _418 => _418.httpReferer]);
19108
+ const xTitle = _optionalChain([request, 'optionalAccess', _419 => _419.xTitle]);
18591
19109
  const { messages, ...metadata } = chatGenerationParams;
18592
19110
  return {
18593
19111
  input: messages,
@@ -18598,7 +19116,7 @@ var OpenRouterPlugin = class extends BasePlugin {
18598
19116
  return isObject(result) ? result.choices : void 0;
18599
19117
  },
18600
19118
  extractMetrics: (result, startTime) => {
18601
- const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _416 => _416.usage]));
19119
+ const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _420 => _420.usage]));
18602
19120
  if (startTime) {
18603
19121
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
18604
19122
  }
@@ -18613,9 +19131,9 @@ var OpenRouterPlugin = class extends BasePlugin {
18613
19131
  type: "llm" /* LLM */,
18614
19132
  extractInput: (args) => {
18615
19133
  const request = getOpenRouterRequestArg(args);
18616
- const requestBody = isObject(_optionalChain([request, 'optionalAccess', _417 => _417.requestBody])) ? request.requestBody : {};
18617
- const httpReferer = _optionalChain([request, 'optionalAccess', _418 => _418.httpReferer]);
18618
- const xTitle = _optionalChain([request, 'optionalAccess', _419 => _419.xTitle]);
19134
+ const requestBody = isObject(_optionalChain([request, 'optionalAccess', _421 => _421.requestBody])) ? request.requestBody : {};
19135
+ const httpReferer = _optionalChain([request, 'optionalAccess', _422 => _422.httpReferer]);
19136
+ const xTitle = _optionalChain([request, 'optionalAccess', _423 => _423.xTitle]);
18619
19137
  const { input, ...metadata } = requestBody;
18620
19138
  return {
18621
19139
  input,
@@ -18630,7 +19148,7 @@ var OpenRouterPlugin = class extends BasePlugin {
18630
19148
  if (!isObject(result)) {
18631
19149
  return void 0;
18632
19150
  }
18633
- const embedding = _optionalChain([result, 'access', _420 => _420.data, 'optionalAccess', _421 => _421[0], 'optionalAccess', _422 => _422.embedding]);
19151
+ const embedding = _optionalChain([result, 'access', _424 => _424.data, 'optionalAccess', _425 => _425[0], 'optionalAccess', _426 => _426.embedding]);
18634
19152
  return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
18635
19153
  },
18636
19154
  extractMetadata: (result) => {
@@ -18650,9 +19168,9 @@ var OpenRouterPlugin = class extends BasePlugin {
18650
19168
  type: "llm" /* LLM */,
18651
19169
  extractInput: (args) => {
18652
19170
  const request = getOpenRouterRequestArg(args);
18653
- const requestBody = isObject(_optionalChain([request, 'optionalAccess', _423 => _423.requestBody])) ? request.requestBody : {};
18654
- const httpReferer = _optionalChain([request, 'optionalAccess', _424 => _424.httpReferer]);
18655
- const xTitle = _nullishCoalesce(_optionalChain([request, 'optionalAccess', _425 => _425.xTitle]), () => ( _optionalChain([request, 'optionalAccess', _426 => _426.appTitle])));
19171
+ const requestBody = isObject(_optionalChain([request, 'optionalAccess', _427 => _427.requestBody])) ? request.requestBody : {};
19172
+ const httpReferer = _optionalChain([request, 'optionalAccess', _428 => _428.httpReferer]);
19173
+ const xTitle = _nullishCoalesce(_optionalChain([request, 'optionalAccess', _429 => _429.xTitle]), () => ( _optionalChain([request, 'optionalAccess', _430 => _430.appTitle])));
18656
19174
  const { documents, query, ...metadata } = requestBody;
18657
19175
  return {
18658
19176
  input: {
@@ -18678,9 +19196,9 @@ var OpenRouterPlugin = class extends BasePlugin {
18678
19196
  type: "llm" /* LLM */,
18679
19197
  extractInput: (args) => {
18680
19198
  const request = getOpenRouterRequestArg(args);
18681
- const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess', _427 => _427.openResponsesRequest])) ? request.openResponsesRequest : {};
18682
- const httpReferer = _optionalChain([request, 'optionalAccess', _428 => _428.httpReferer]);
18683
- const xTitle = _optionalChain([request, 'optionalAccess', _429 => _429.xTitle]);
19199
+ const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess', _431 => _431.openResponsesRequest])) ? request.openResponsesRequest : {};
19200
+ const httpReferer = _optionalChain([request, 'optionalAccess', _432 => _432.httpReferer]);
19201
+ const xTitle = _optionalChain([request, 'optionalAccess', _433 => _433.xTitle]);
18684
19202
  const { input, ...metadata } = openResponsesRequest;
18685
19203
  return {
18686
19204
  input,
@@ -18690,7 +19208,7 @@ var OpenRouterPlugin = class extends BasePlugin {
18690
19208
  extractOutput: (result) => extractOpenRouterResponseOutput2(result),
18691
19209
  extractMetadata: (result) => extractOpenRouterResponseMetadata2(result),
18692
19210
  extractMetrics: (result, startTime) => {
18693
- const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _430 => _430.usage]));
19211
+ const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _434 => _434.usage]));
18694
19212
  if (startTime) {
18695
19213
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
18696
19214
  }
@@ -18742,14 +19260,14 @@ var OpenRouterPlugin = class extends BasePlugin {
18742
19260
  extractMetadata: (result, event) => {
18743
19261
  if (!isObject(result)) {
18744
19262
  return {
18745
- step: _optionalChain([event, 'optionalAccess', _431 => _431.step]),
18746
- step_type: _optionalChain([event, 'optionalAccess', _432 => _432.stepType])
19263
+ step: _optionalChain([event, 'optionalAccess', _435 => _435.step]),
19264
+ step_type: _optionalChain([event, 'optionalAccess', _436 => _436.stepType])
18747
19265
  };
18748
19266
  }
18749
19267
  return {
18750
19268
  ...extractOpenRouterResponseMetadata2(result) || {},
18751
- ..._optionalChain([event, 'optionalAccess', _433 => _433.step]) !== void 0 ? { step: event.step } : {},
18752
- ..._optionalChain([event, 'optionalAccess', _434 => _434.stepType]) ? { step_type: event.stepType } : {}
19269
+ ..._optionalChain([event, 'optionalAccess', _437 => _437.step]) !== void 0 ? { step: event.step } : {},
19270
+ ..._optionalChain([event, 'optionalAccess', _438 => _438.stepType]) ? { step_type: event.stepType } : {}
18753
19271
  };
18754
19272
  },
18755
19273
  extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage2(result.usage) : {}
@@ -19129,7 +19647,7 @@ function publishToolResult2(tracingChannel2, event, result) {
19129
19647
  }
19130
19648
  function getToolCallId2(context) {
19131
19649
  const toolContext = context;
19132
- return typeof _optionalChain([toolContext, 'optionalAccess', _435 => _435.toolCall, 'optionalAccess', _436 => _436.id]) === "string" ? toolContext.toolCall.id : void 0;
19650
+ return typeof _optionalChain([toolContext, 'optionalAccess', _439 => _439.toolCall, 'optionalAccess', _440 => _440.id]) === "string" ? toolContext.toolCall.id : void 0;
19133
19651
  }
19134
19652
  function aggregateOpenRouterChatChunks(chunks) {
19135
19653
  let role;
@@ -19145,12 +19663,12 @@ function aggregateOpenRouterChatChunks(chunks) {
19145
19663
  for (const chunk of chunks) {
19146
19664
  metrics = {
19147
19665
  ...metrics,
19148
- ...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess', _437 => _437.usage]))
19666
+ ...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess', _441 => _441.usage]))
19149
19667
  };
19150
- const choice = _optionalChain([chunk, 'optionalAccess', _438 => _438.choices, 'optionalAccess', _439 => _439[0]]);
19151
- const delta = _optionalChain([choice, 'optionalAccess', _440 => _440.delta]);
19668
+ const choice = _optionalChain([chunk, 'optionalAccess', _442 => _442.choices, 'optionalAccess', _443 => _443[0]]);
19669
+ const delta = _optionalChain([choice, 'optionalAccess', _444 => _444.delta]);
19152
19670
  if (!delta) {
19153
- if (_optionalChain([choice, 'optionalAccess', _441 => _441.finish_reason]) !== void 0) {
19671
+ if (_optionalChain([choice, 'optionalAccess', _445 => _445.finish_reason]) !== void 0) {
19154
19672
  finishReason = choice.finish_reason;
19155
19673
  }
19156
19674
  continue;
@@ -19176,7 +19694,7 @@ function aggregateOpenRouterChatChunks(chunks) {
19176
19694
  ...delta.reasoning_details
19177
19695
  ];
19178
19696
  }
19179
- const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess', _442 => _442.finishReason]), () => ( _optionalChain([choice, 'optionalAccess', _443 => _443.finish_reason]))), () => ( void 0));
19697
+ const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess', _446 => _446.finishReason]), () => ( _optionalChain([choice, 'optionalAccess', _447 => _447.finish_reason]))), () => ( void 0));
19180
19698
  const deltaFinishReason = _nullishCoalesce(_nullishCoalesce(delta.finishReason, () => ( delta.finish_reason)), () => ( void 0));
19181
19699
  if (choiceFinishReason !== void 0) {
19182
19700
  finishReason = choiceFinishReason;
@@ -19188,11 +19706,11 @@ function aggregateOpenRouterChatChunks(chunks) {
19188
19706
  continue;
19189
19707
  }
19190
19708
  for (const toolDelta of toolCallDeltas) {
19191
- if (!_optionalChain([toolDelta, 'optionalAccess', _444 => _444.function])) {
19709
+ if (!_optionalChain([toolDelta, 'optionalAccess', _448 => _448.function])) {
19192
19710
  continue;
19193
19711
  }
19194
19712
  const toolIndex = _nullishCoalesce(toolDelta.index, () => ( 0));
19195
- const existingToolCall = _optionalChain([toolCalls, 'optionalAccess', _445 => _445[toolIndex]]);
19713
+ const existingToolCall = _optionalChain([toolCalls, 'optionalAccess', _449 => _449[toolIndex]]);
19196
19714
  if (!existingToolCall || toolDelta.id && existingToolCall.id !== void 0 && existingToolCall.id !== toolDelta.id) {
19197
19715
  const nextToolCalls = [...toolCalls || []];
19198
19716
  nextToolCalls[toolIndex] = {
@@ -19242,7 +19760,7 @@ function aggregateOpenRouterChatChunks(chunks) {
19242
19760
  function aggregateOpenRouterResponseStreamEvents(chunks) {
19243
19761
  let finalResponse;
19244
19762
  for (const chunk of chunks) {
19245
- const response = _optionalChain([chunk, 'optionalAccess', _446 => _446.response]);
19763
+ const response = _optionalChain([chunk, 'optionalAccess', _450 => _450.response]);
19246
19764
  if (!response) {
19247
19765
  continue;
19248
19766
  }
@@ -19672,10 +20190,10 @@ var MistralPlugin = class extends BasePlugin {
19672
20190
  type: "llm" /* LLM */,
19673
20191
  extractInput: extractMessagesInputWithMetadata,
19674
20192
  extractOutput: (result) => {
19675
- return _optionalChain([result, 'optionalAccess', _447 => _447.choices]);
20193
+ return _optionalChain([result, 'optionalAccess', _451 => _451.choices]);
19676
20194
  },
19677
20195
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19678
- extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _448 => _448.usage]), startTime)
20196
+ extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _452 => _452.usage]), startTime)
19679
20197
  })
19680
20198
  );
19681
20199
  this.unsubscribers.push(
@@ -19695,11 +20213,11 @@ var MistralPlugin = class extends BasePlugin {
19695
20213
  type: "llm" /* LLM */,
19696
20214
  extractInput: extractEmbeddingInputWithMetadata,
19697
20215
  extractOutput: (result) => {
19698
- const embedding = _optionalChain([result, 'optionalAccess', _449 => _449.data, 'optionalAccess', _450 => _450[0], 'optionalAccess', _451 => _451.embedding]);
20216
+ const embedding = _optionalChain([result, 'optionalAccess', _453 => _453.data, 'optionalAccess', _454 => _454[0], 'optionalAccess', _455 => _455.embedding]);
19699
20217
  return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
19700
20218
  },
19701
20219
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19702
- extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _452 => _452.usage]))
20220
+ extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _456 => _456.usage]))
19703
20221
  })
19704
20222
  );
19705
20223
  this.unsubscribers.push(
@@ -19709,7 +20227,7 @@ var MistralPlugin = class extends BasePlugin {
19709
20227
  extractInput: extractClassifierInputWithMetadata,
19710
20228
  extractOutput: extractClassifierOutput,
19711
20229
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19712
- extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _453 => _453.usage]))
20230
+ extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _457 => _457.usage]))
19713
20231
  })
19714
20232
  );
19715
20233
  this.unsubscribers.push(
@@ -19719,7 +20237,7 @@ var MistralPlugin = class extends BasePlugin {
19719
20237
  extractInput: extractClassifierInputWithMetadata,
19720
20238
  extractOutput: extractClassifierOutput,
19721
20239
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19722
- extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _454 => _454.usage]))
20240
+ extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _458 => _458.usage]))
19723
20241
  })
19724
20242
  );
19725
20243
  this.unsubscribers.push(
@@ -19729,7 +20247,7 @@ var MistralPlugin = class extends BasePlugin {
19729
20247
  extractInput: extractClassifierInputWithMetadata,
19730
20248
  extractOutput: extractClassifierOutput,
19731
20249
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19732
- extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _455 => _455.usage]))
20250
+ extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _459 => _459.usage]))
19733
20251
  })
19734
20252
  );
19735
20253
  this.unsubscribers.push(
@@ -19739,7 +20257,7 @@ var MistralPlugin = class extends BasePlugin {
19739
20257
  extractInput: extractClassifierInputWithMetadata,
19740
20258
  extractOutput: extractClassifierOutput,
19741
20259
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19742
- extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _456 => _456.usage]))
20260
+ extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _460 => _460.usage]))
19743
20261
  })
19744
20262
  );
19745
20263
  this.unsubscribers.push(
@@ -19748,10 +20266,10 @@ var MistralPlugin = class extends BasePlugin {
19748
20266
  type: "llm" /* LLM */,
19749
20267
  extractInput: extractPromptInputWithMetadata,
19750
20268
  extractOutput: (result) => {
19751
- return _optionalChain([result, 'optionalAccess', _457 => _457.choices]);
20269
+ return _optionalChain([result, 'optionalAccess', _461 => _461.choices]);
19752
20270
  },
19753
20271
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19754
- extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _458 => _458.usage]), startTime)
20272
+ extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _462 => _462.usage]), startTime)
19755
20273
  })
19756
20274
  );
19757
20275
  this.unsubscribers.push(
@@ -19771,10 +20289,10 @@ var MistralPlugin = class extends BasePlugin {
19771
20289
  type: "llm" /* LLM */,
19772
20290
  extractInput: extractMessagesInputWithMetadata,
19773
20291
  extractOutput: (result) => {
19774
- return _optionalChain([result, 'optionalAccess', _459 => _459.choices]);
20292
+ return _optionalChain([result, 'optionalAccess', _463 => _463.choices]);
19775
20293
  },
19776
20294
  extractMetadata: (result) => extractMistralResponseMetadata(result),
19777
- extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _460 => _460.usage]), startTime)
20295
+ extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _464 => _464.usage]), startTime)
19778
20296
  })
19779
20297
  );
19780
20298
  this.unsubscribers.push(
@@ -20029,11 +20547,11 @@ function mergeMistralContentParts(left, right) {
20029
20547
  const merged = [...(left || []).map((part) => structuredClone(part))];
20030
20548
  for (const part of right) {
20031
20549
  const lastPart = merged[merged.length - 1];
20032
- if (part.type === "text" && _optionalChain([lastPart, 'optionalAccess', _461 => _461.type]) === "text" && typeof lastPart.text === "string" && typeof part.text === "string") {
20550
+ if (part.type === "text" && _optionalChain([lastPart, 'optionalAccess', _465 => _465.type]) === "text" && typeof lastPart.text === "string" && typeof part.text === "string") {
20033
20551
  lastPart.text += part.text;
20034
20552
  continue;
20035
20553
  }
20036
- if (part.type === "thinking" && _optionalChain([lastPart, 'optionalAccess', _462 => _462.type]) === "thinking" && Array.isArray(lastPart.thinking) && Array.isArray(part.thinking)) {
20554
+ if (part.type === "thinking" && _optionalChain([lastPart, 'optionalAccess', _466 => _466.type]) === "thinking" && Array.isArray(lastPart.thinking) && Array.isArray(part.thinking)) {
20037
20555
  lastPart.thinking = mergeMistralTextSegments(
20038
20556
  lastPart.thinking,
20039
20557
  part.thinking
@@ -20088,13 +20606,13 @@ function createMergedToolCallDelta(delta) {
20088
20606
  ...delta,
20089
20607
  function: {
20090
20608
  ...delta.function,
20091
- arguments: typeof _optionalChain([delta, 'access', _463 => _463.function, 'optionalAccess', _464 => _464.arguments]) === "string" ? delta.function.arguments : ""
20609
+ arguments: typeof _optionalChain([delta, 'access', _467 => _467.function, 'optionalAccess', _468 => _468.arguments]) === "string" ? delta.function.arguments : ""
20092
20610
  }
20093
20611
  };
20094
20612
  }
20095
20613
  function mergeToolCallDeltaPair(current, delta) {
20096
- const currentArguments = typeof _optionalChain([current, 'access', _465 => _465.function, 'optionalAccess', _466 => _466.arguments]) === "string" ? current.function.arguments : "";
20097
- const deltaArguments = typeof _optionalChain([delta, 'access', _467 => _467.function, 'optionalAccess', _468 => _468.arguments]) === "string" ? delta.function.arguments : "";
20614
+ const currentArguments = typeof _optionalChain([current, 'access', _469 => _469.function, 'optionalAccess', _470 => _470.arguments]) === "string" ? current.function.arguments : "";
20615
+ const deltaArguments = typeof _optionalChain([delta, 'access', _471 => _471.function, 'optionalAccess', _472 => _472.arguments]) === "string" ? delta.function.arguments : "";
20098
20616
  return {
20099
20617
  ...current,
20100
20618
  ...delta,
@@ -20197,7 +20715,7 @@ function aggregateMistralStreamChunks(chunks) {
20197
20715
  let metrics = {};
20198
20716
  let metadata;
20199
20717
  for (const event of chunks) {
20200
- const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess', _469 => _469.data])) ? event.data : void 0;
20718
+ const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess', _473 => _473.data])) ? event.data : void 0;
20201
20719
  if (!chunk) {
20202
20720
  continue;
20203
20721
  }
@@ -20291,9 +20809,9 @@ var googleADKChannels = defineChannels("@google/adk", {
20291
20809
  });
20292
20810
 
20293
20811
  // src/instrumentation/plugins/google-adk-plugin.ts
20294
- var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4) { super(...args4); _class19.prototype.__init65.call(this);_class19.prototype.__init66.call(this); }
20295
- __init65() {this.activeRunnerSpans = /* @__PURE__ */ new Map()}
20296
- __init66() {this.activeAgentSpans = /* @__PURE__ */ new Map()}
20812
+ var GoogleADKPlugin = (_class21 = class extends BasePlugin {constructor(...args5) { super(...args5); _class21.prototype.__init69.call(this);_class21.prototype.__init70.call(this); }
20813
+ __init69() {this.activeRunnerSpans = /* @__PURE__ */ new Map()}
20814
+ __init70() {this.activeAgentSpans = /* @__PURE__ */ new Map()}
20297
20815
  onEnable() {
20298
20816
  this.subscribeToRunnerRunAsync();
20299
20817
  this.subscribeToAgentRunAsync();
@@ -20388,7 +20906,7 @@ var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4
20388
20906
  };
20389
20907
  tracingChannel2.subscribe(handlers);
20390
20908
  this.unsubscribers.push(() => {
20391
- _optionalChain([unbindCurrentSpanStore, 'optionalCall', _470 => _470()]);
20909
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _474 => _474()]);
20392
20910
  tracingChannel2.unsubscribe(handlers);
20393
20911
  });
20394
20912
  }
@@ -20491,7 +21009,7 @@ var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4
20491
21009
  };
20492
21010
  tracingChannel2.subscribe(handlers);
20493
21011
  this.unsubscribers.push(() => {
20494
- _optionalChain([unbindCurrentSpanStore, 'optionalCall', _471 => _471()]);
21012
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _475 => _475()]);
20495
21013
  tracingChannel2.unsubscribe(handlers);
20496
21014
  });
20497
21015
  }
@@ -20563,7 +21081,7 @@ var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4
20563
21081
  tracingChannel2.unsubscribe(handlers);
20564
21082
  });
20565
21083
  }
20566
- }, _class19);
21084
+ }, _class21);
20567
21085
  function ensureState(states, event, create) {
20568
21086
  const existing = states.get(event);
20569
21087
  if (existing) {
@@ -20638,10 +21156,10 @@ function bindAsyncIterableToCurrentSpan(stream, span) {
20638
21156
  }
20639
21157
  function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
20640
21158
  const state = _internalGetGlobalState();
20641
- const contextManager = _optionalChain([state, 'optionalAccess', _472 => _472.contextManager]);
21159
+ const contextManager = _optionalChain([state, 'optionalAccess', _476 => _476.contextManager]);
20642
21160
  const startChannel = tracingChannel2.start;
20643
21161
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
20644
- if (!_optionalChain([startChannel, 'optionalAccess', _473 => _473.bindStore]) || !currentSpanStore) {
21162
+ if (!_optionalChain([startChannel, 'optionalAccess', _477 => _477.bindStore]) || !currentSpanStore) {
20645
21163
  return void 0;
20646
21164
  }
20647
21165
  startChannel.bindStore(currentSpanStore, (event) => {
@@ -20653,7 +21171,7 @@ function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
20653
21171
  return contextManager.wrapSpanForStore(span);
20654
21172
  });
20655
21173
  return () => {
20656
- _optionalChain([startChannel, 'access', _474 => _474.unbindStore, 'optionalCall', _475 => _475(currentSpanStore)]);
21174
+ _optionalChain([startChannel, 'access', _478 => _478.unbindStore, 'optionalCall', _479 => _479(currentSpanStore)]);
20657
21175
  };
20658
21176
  }
20659
21177
  function extractRunnerContextKey(paramsOrContext) {
@@ -20666,9 +21184,9 @@ function extractRunnerContextKey(paramsOrContext) {
20666
21184
  return extractInvocationContextKey(invocationContext);
20667
21185
  }
20668
21186
  function extractInvocationContextKey(parentContext) {
20669
- const session = _optionalChain([parentContext, 'optionalAccess', _476 => _476.session]);
20670
- const userId = _optionalChain([session, 'optionalAccess', _477 => _477.userId]);
20671
- const sessionId = _optionalChain([session, 'optionalAccess', _478 => _478.id]);
21187
+ const session = _optionalChain([parentContext, 'optionalAccess', _480 => _480.session]);
21188
+ const userId = _optionalChain([session, 'optionalAccess', _481 => _481.userId]);
21189
+ const sessionId = _optionalChain([session, 'optionalAccess', _482 => _482.id]);
20672
21190
  if (typeof userId !== "string" || typeof sessionId !== "string") {
20673
21191
  return void 0;
20674
21192
  }
@@ -20725,27 +21243,27 @@ function extractRunnerMetadata(paramsOrContext) {
20725
21243
  metadata["google_adk.session_id"] = directSessionId;
20726
21244
  }
20727
21245
  const session = "session" in paramsOrContext ? paramsOrContext.session : void 0;
20728
- if (metadata["google_adk.user_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _479 => _479.userId]) === "string") {
21246
+ if (metadata["google_adk.user_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _483 => _483.userId]) === "string") {
20729
21247
  metadata["google_adk.user_id"] = session.userId;
20730
21248
  }
20731
- if (metadata["google_adk.session_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _480 => _480.id]) === "string") {
21249
+ if (metadata["google_adk.session_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _484 => _484.id]) === "string") {
20732
21250
  metadata["google_adk.session_id"] = session.id;
20733
21251
  }
20734
21252
  return metadata;
20735
21253
  }
20736
21254
  function extractAgentName(agent, parentContext) {
20737
- if (typeof _optionalChain([agent, 'optionalAccess', _481 => _481.name]) === "string" && agent.name.length > 0) {
21255
+ if (typeof _optionalChain([agent, 'optionalAccess', _485 => _485.name]) === "string" && agent.name.length > 0) {
20738
21256
  return agent.name;
20739
21257
  }
20740
21258
  if (!parentContext) {
20741
21259
  return void 0;
20742
21260
  }
20743
21261
  const contextAgent = parentContext.agent;
20744
- return typeof _optionalChain([contextAgent, 'optionalAccess', _482 => _482.name]) === "string" && contextAgent.name.length > 0 ? contextAgent.name : void 0;
21262
+ return typeof _optionalChain([contextAgent, 'optionalAccess', _486 => _486.name]) === "string" && contextAgent.name.length > 0 ? contextAgent.name : void 0;
20745
21263
  }
20746
21264
  function extractModelName(agent, parentContext) {
20747
- const modelAgent = _nullishCoalesce(agent, () => ( _optionalChain([parentContext, 'optionalAccess', _483 => _483.agent])));
20748
- if (!_optionalChain([modelAgent, 'optionalAccess', _484 => _484.model])) {
21265
+ const modelAgent = _nullishCoalesce(agent, () => ( _optionalChain([parentContext, 'optionalAccess', _487 => _487.agent])));
21266
+ if (!_optionalChain([modelAgent, 'optionalAccess', _488 => _488.model])) {
20749
21267
  return;
20750
21268
  }
20751
21269
  if (typeof modelAgent.model === "string") {
@@ -20758,30 +21276,30 @@ function extractModelName(agent, parentContext) {
20758
21276
  }
20759
21277
  function extractToolCallId(req) {
20760
21278
  const toolContext = req.toolContext;
20761
- return _optionalChain([toolContext, 'optionalAccess', _485 => _485.functionCallId]);
21279
+ return _optionalChain([toolContext, 'optionalAccess', _489 => _489.functionCallId]);
20762
21280
  }
20763
21281
  function extractToolName2(req, tool) {
20764
- if (typeof _optionalChain([tool, 'optionalAccess', _486 => _486.name]) === "string" && tool.name.length > 0) {
21282
+ if (typeof _optionalChain([tool, 'optionalAccess', _490 => _490.name]) === "string" && tool.name.length > 0) {
20765
21283
  return tool.name;
20766
21284
  }
20767
21285
  const toolContext = req.toolContext;
20768
- const invocationContext = _optionalChain([toolContext, 'optionalAccess', _487 => _487.invocationContext]);
20769
- const invocationTool = _optionalChain([invocationContext, 'optionalAccess', _488 => _488.tool]);
20770
- const toolName = _optionalChain([invocationTool, 'optionalAccess', _489 => _489.name]);
21286
+ const invocationContext = _optionalChain([toolContext, 'optionalAccess', _491 => _491.invocationContext]);
21287
+ const invocationTool = _optionalChain([invocationContext, 'optionalAccess', _492 => _492.tool]);
21288
+ const toolName = _optionalChain([invocationTool, 'optionalAccess', _493 => _493.name]);
20771
21289
  return typeof toolName === "string" && toolName.length > 0 ? toolName : void 0;
20772
21290
  }
20773
21291
  function extractToolAgentName(req) {
20774
21292
  const toolContext = req.toolContext;
20775
- const directName = _optionalChain([toolContext, 'optionalAccess', _490 => _490.agentName]);
21293
+ const directName = _optionalChain([toolContext, 'optionalAccess', _494 => _494.agentName]);
20776
21294
  if (typeof directName === "string" && directName.length > 0) {
20777
21295
  return directName;
20778
21296
  }
20779
- const invocationContext = _optionalChain([toolContext, 'optionalAccess', _491 => _491.invocationContext]);
21297
+ const invocationContext = _optionalChain([toolContext, 'optionalAccess', _495 => _495.invocationContext]);
20780
21298
  return extractAgentName(void 0, invocationContext);
20781
21299
  }
20782
21300
  function findToolParentSpan(req, activeAgentSpans, activeRunnerSpans) {
20783
21301
  const toolContext = req.toolContext;
20784
- const invocationContext = _optionalChain([toolContext, 'optionalAccess', _492 => _492.invocationContext]);
21302
+ const invocationContext = _optionalChain([toolContext, 'optionalAccess', _496 => _496.invocationContext]);
20785
21303
  const contextKey = extractInvocationContextKey(invocationContext);
20786
21304
  const agentName = extractToolAgentName(req);
20787
21305
  if (contextKey && agentName) {
@@ -21163,7 +21681,7 @@ function extractCohereResponseMetadata(result) {
21163
21681
  RESPONSE_METADATA_ALLOWLIST2
21164
21682
  );
21165
21683
  const meta = isObject(result.meta) ? result.meta : void 0;
21166
- const apiVersion = isObject(_optionalChain([meta, 'optionalAccess', _493 => _493.apiVersion])) && typeof meta.apiVersion.version === "string" && meta.apiVersion.version || isObject(_optionalChain([meta, 'optionalAccess', _494 => _494.api_version])) && typeof meta.api_version.version === "string" && meta.api_version.version;
21684
+ const apiVersion = isObject(_optionalChain([meta, 'optionalAccess', _497 => _497.apiVersion])) && typeof meta.apiVersion.version === "string" && meta.apiVersion.version || isObject(_optionalChain([meta, 'optionalAccess', _498 => _498.api_version])) && typeof meta.api_version.version === "string" && meta.api_version.version;
21167
21685
  const metadata = {
21168
21686
  ...responseMetadata,
21169
21687
  ...apiVersion ? { api_version: apiVersion } : {}
@@ -21324,13 +21842,13 @@ function getToolCallIndex2(toolCall, fallbackIndex) {
21324
21842
  return typeof toolCall.index === "number" && Number.isInteger(toolCall.index) ? toolCall.index : fallbackIndex;
21325
21843
  }
21326
21844
  function appendToolCallDelta(existing, incoming) {
21327
- const currentArguments = isObject(_optionalChain([existing, 'optionalAccess', _495 => _495.function])) && typeof existing.function.arguments === "string" ? existing.function.arguments : "";
21845
+ const currentArguments = isObject(_optionalChain([existing, 'optionalAccess', _499 => _499.function])) && typeof existing.function.arguments === "string" ? existing.function.arguments : "";
21328
21846
  const incomingArguments = isObject(incoming.function) && typeof incoming.function.arguments === "string" ? incoming.function.arguments : "";
21329
21847
  return {
21330
21848
  ...existing,
21331
21849
  ...incoming,
21332
21850
  function: {
21333
- ...isObject(_optionalChain([existing, 'optionalAccess', _496 => _496.function])) ? existing.function : {},
21851
+ ...isObject(_optionalChain([existing, 'optionalAccess', _500 => _500.function])) ? existing.function : {},
21334
21852
  ...isObject(incoming.function) ? incoming.function : {},
21335
21853
  ...incomingArguments ? { arguments: `${currentArguments}${incomingArguments}` } : {}
21336
21854
  }
@@ -21586,7 +22104,7 @@ function aggregateCohereChatStreamChunks(chunks) {
21586
22104
  ...mergedToolCalls.length > 0 ? { toolCalls: mergedToolCalls } : {}
21587
22105
  };
21588
22106
  } else if (mergedToolCalls.length > 0 || role || mergedText.length > 0 || aggregatedContentBlocks.length > 0) {
21589
- const textContent = mergedText.length > 0 ? mergedText : _optionalChain([aggregatedContentBlocks, 'access', _497 => _497[0], 'optionalAccess', _498 => _498.type]) === "text" ? aggregatedContentBlocks[0].text : void 0;
22107
+ const textContent = mergedText.length > 0 ? mergedText : _optionalChain([aggregatedContentBlocks, 'access', _501 => _501[0], 'optionalAccess', _502 => _502.type]) === "text" ? aggregatedContentBlocks[0].text : void 0;
21590
22108
  output = {
21591
22109
  ...role ? { role } : {},
21592
22110
  ...textContent ? { content: textContent } : {},
@@ -21635,7 +22153,7 @@ var GroqPlugin = class extends BasePlugin {
21635
22153
  metadata: { ...metadata, provider: "groq" }
21636
22154
  };
21637
22155
  },
21638
- extractOutput: (result) => _optionalChain([result, 'optionalAccess', _499 => _499.choices]),
22156
+ extractOutput: (result) => _optionalChain([result, 'optionalAccess', _503 => _503.choices]),
21639
22157
  extractMetrics: (result, startTime) => {
21640
22158
  const metrics = parseGroqMetrics(result);
21641
22159
  if (startTime) {
@@ -21658,7 +22176,7 @@ var GroqPlugin = class extends BasePlugin {
21658
22176
  };
21659
22177
  },
21660
22178
  extractOutput: (result) => {
21661
- const embedding = _optionalChain([result, 'optionalAccess', _500 => _500.data, 'optionalAccess', _501 => _501[0], 'optionalAccess', _502 => _502.embedding]);
22179
+ const embedding = _optionalChain([result, 'optionalAccess', _504 => _504.data, 'optionalAccess', _505 => _505[0], 'optionalAccess', _506 => _506.embedding]);
21662
22180
  return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
21663
22181
  },
21664
22182
  extractMetrics: (result) => parseGroqMetrics(result)
@@ -21670,8 +22188,8 @@ var GroqPlugin = class extends BasePlugin {
21670
22188
  }
21671
22189
  };
21672
22190
  function parseGroqMetrics(result) {
21673
- const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _503 => _503.usage]));
21674
- const xGroq = _optionalChain([result, 'optionalAccess', _504 => _504.x_groq]);
22191
+ const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _507 => _507.usage]));
22192
+ const xGroq = _optionalChain([result, 'optionalAccess', _508 => _508.x_groq]);
21675
22193
  if (!xGroq || typeof xGroq !== "object") {
21676
22194
  return metrics;
21677
22195
  }
@@ -21695,7 +22213,7 @@ function aggregateGroqChatCompletionChunks(chunks, streamResult, endEvent) {
21695
22213
  );
21696
22214
  const reasoning = aggregateGroqReasoning(chunks);
21697
22215
  if (reasoning !== void 0) {
21698
- const message = _optionalChain([aggregated, 'access', _505 => _505.output, 'access', _506 => _506[0], 'optionalAccess', _507 => _507.message]);
22216
+ const message = _optionalChain([aggregated, 'access', _509 => _509.output, 'access', _510 => _510[0], 'optionalAccess', _511 => _511.message]);
21699
22217
  if (message) {
21700
22218
  message.reasoning = reasoning;
21701
22219
  }
@@ -21708,8 +22226,8 @@ function aggregateGroqChatCompletionChunks(chunks, streamResult, endEvent) {
21708
22226
  function aggregateGroqReasoning(chunks) {
21709
22227
  let reasoning = "";
21710
22228
  for (const chunk of chunks) {
21711
- const delta = _optionalChain([chunk, 'access', _508 => _508.choices, 'optionalAccess', _509 => _509[0], 'optionalAccess', _510 => _510.delta]);
21712
- const deltaReasoning = _optionalChain([delta, 'optionalAccess', _511 => _511.reasoning]);
22229
+ const delta = _optionalChain([chunk, 'access', _512 => _512.choices, 'optionalAccess', _513 => _513[0], 'optionalAccess', _514 => _514.delta]);
22230
+ const deltaReasoning = _optionalChain([delta, 'optionalAccess', _515 => _515.reasoning]);
21713
22231
  if (typeof deltaReasoning === "string") {
21714
22232
  reasoning += deltaReasoning;
21715
22233
  }
@@ -21766,8 +22284,8 @@ var GenkitPlugin = class extends BasePlugin {
21766
22284
  type: "llm" /* LLM */,
21767
22285
  extractInput: ([input]) => extractGenerateInput(input),
21768
22286
  extractOutput: extractGenerateOutput,
21769
- extractMetadata: (result, event) => extractGenerateResponseMetadata(result, _optionalChain([event, 'optionalAccess', _512 => _512.arguments, 'optionalAccess', _513 => _513[0]])),
21770
- extractMetrics: (result) => parseGenkitUsageMetrics(_optionalChain([result, 'optionalAccess', _514 => _514.usage]))
22287
+ extractMetadata: (result, event) => extractGenerateResponseMetadata(result, _optionalChain([event, 'optionalAccess', _516 => _516.arguments, 'optionalAccess', _517 => _517[0]])),
22288
+ extractMetrics: (result) => parseGenkitUsageMetrics(_optionalChain([result, 'optionalAccess', _518 => _518.usage]))
21771
22289
  })
21772
22290
  );
21773
22291
  this.unsubscribers.push(
@@ -21784,7 +22302,7 @@ var GenkitPlugin = class extends BasePlugin {
21784
22302
  type: "function" /* FUNCTION */,
21785
22303
  extractInput: ([params]) => extractEmbedInput(params),
21786
22304
  extractOutput: (result) => summarizeEmbeddingResult(result),
21787
- extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess', _515 => _515.arguments, 'optionalAccess', _516 => _516[0]])),
22305
+ extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess', _519 => _519.arguments, 'optionalAccess', _520 => _520[0]])),
21788
22306
  extractMetrics: () => ({})
21789
22307
  })
21790
22308
  );
@@ -21794,7 +22312,7 @@ var GenkitPlugin = class extends BasePlugin {
21794
22312
  type: "function" /* FUNCTION */,
21795
22313
  extractInput: ([params]) => extractEmbedManyInput(params),
21796
22314
  extractOutput: summarizeEmbeddingResult,
21797
- extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess', _517 => _517.arguments, 'optionalAccess', _518 => _518[0]])),
22315
+ extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess', _521 => _521.arguments, 'optionalAccess', _522 => _522[0]])),
21798
22316
  extractMetrics: () => ({})
21799
22317
  })
21800
22318
  );
@@ -21845,7 +22363,7 @@ var GenkitPlugin = class extends BasePlugin {
21845
22363
  };
21846
22364
  tracingChannel2.subscribe(handlers);
21847
22365
  this.unsubscribers.push(() => {
21848
- _optionalChain([unbindCurrentSpanStore, 'optionalCall', _519 => _519()]);
22366
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _523 => _523()]);
21849
22367
  tracingChannel2.unsubscribe(handlers);
21850
22368
  });
21851
22369
  }
@@ -21893,7 +22411,7 @@ var GenkitPlugin = class extends BasePlugin {
21893
22411
  };
21894
22412
  tracingChannel2.subscribe(handlers);
21895
22413
  this.unsubscribers.push(() => {
21896
- _optionalChain([unbindCurrentSpanStore, 'optionalCall', _520 => _520()]);
22414
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _524 => _524()]);
21897
22415
  tracingChannel2.unsubscribe(handlers);
21898
22416
  });
21899
22417
  }
@@ -21960,10 +22478,10 @@ function ensureActionSpanState(states, event, create) {
21960
22478
  }
21961
22479
  function bindActionCurrentSpanStoreToStart(tracingChannel2, states, create) {
21962
22480
  const state = _internalGetGlobalState();
21963
- const contextManager = _optionalChain([state, 'optionalAccess', _521 => _521.contextManager]);
22481
+ const contextManager = _optionalChain([state, 'optionalAccess', _525 => _525.contextManager]);
21964
22482
  const startChannel = tracingChannel2.start;
21965
22483
  const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
21966
- if (!_optionalChain([startChannel, 'optionalAccess', _522 => _522.bindStore]) || !currentSpanStore) {
22484
+ if (!_optionalChain([startChannel, 'optionalAccess', _526 => _526.bindStore]) || !currentSpanStore) {
21967
22485
  return void 0;
21968
22486
  }
21969
22487
  startChannel.bindStore(currentSpanStore, (event) => {
@@ -21975,7 +22493,7 @@ function bindActionCurrentSpanStoreToStart(tracingChannel2, states, create) {
21975
22493
  return state2 ? contextManager.wrapSpanForStore(state2.span) : currentSpanStore.getStore();
21976
22494
  });
21977
22495
  return () => {
21978
- _optionalChain([startChannel, 'access', _523 => _523.unbindStore, 'optionalCall', _524 => _524(currentSpanStore)]);
22496
+ _optionalChain([startChannel, 'access', _527 => _527.unbindStore, 'optionalCall', _528 => _528(currentSpanStore)]);
21979
22497
  };
21980
22498
  }
21981
22499
  function normalizeInput(input) {
@@ -22022,25 +22540,25 @@ function extractGenerateOutput(result) {
22022
22540
  }
22023
22541
  function extractGenerateResponseMetadata(result, input) {
22024
22542
  const normalized = input ? normalizeInput(input) : void 0;
22025
- const request = isObject(_optionalChain([result, 'optionalAccess', _525 => _525.request])) ? _optionalChain([result, 'optionalAccess', _526 => _526.request]) : isObject(normalized) ? normalized : void 0;
22543
+ const request = isObject(_optionalChain([result, 'optionalAccess', _529 => _529.request])) ? _optionalChain([result, 'optionalAccess', _530 => _530.request]) : isObject(normalized) ? normalized : void 0;
22026
22544
  return {
22027
22545
  ...genkitProviderMetadata(),
22028
22546
  ...pickDefined({
22029
- model: modelName(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _527 => _527.model]), () => ( _optionalChain([request, 'optionalAccess', _528 => _528.model])))),
22030
- finishReason: _optionalChain([result, 'optionalAccess', _529 => _529.finishReason]),
22031
- finishMessage: _optionalChain([result, 'optionalAccess', _530 => _530.finishMessage])
22547
+ model: modelName(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _531 => _531.model]), () => ( _optionalChain([request, 'optionalAccess', _532 => _532.model])))),
22548
+ finishReason: _optionalChain([result, 'optionalAccess', _533 => _533.finishReason]),
22549
+ finishMessage: _optionalChain([result, 'optionalAccess', _534 => _534.finishMessage])
22032
22550
  })
22033
22551
  };
22034
22552
  }
22035
22553
  function extractEmbedInput(params) {
22036
22554
  return {
22037
- input: processInputAttachments(_optionalChain([params, 'optionalAccess', _531 => _531.content])),
22555
+ input: processInputAttachments(_optionalChain([params, 'optionalAccess', _535 => _535.content])),
22038
22556
  metadata: extractEmbedMetadata(params)
22039
22557
  };
22040
22558
  }
22041
22559
  function extractEmbedManyInput(params) {
22042
22560
  return {
22043
- input: processInputAttachments(_optionalChain([params, 'optionalAccess', _532 => _532.content])),
22561
+ input: processInputAttachments(_optionalChain([params, 'optionalAccess', _536 => _536.content])),
22044
22562
  metadata: extractEmbedMetadata(params)
22045
22563
  };
22046
22564
  }
@@ -22048,7 +22566,7 @@ function extractEmbedMetadata(params) {
22048
22566
  return {
22049
22567
  ...genkitProviderMetadata(),
22050
22568
  ...pickDefined({
22051
- model: modelName(_optionalChain([params, 'optionalAccess', _533 => _533.embedder]))
22569
+ model: modelName(_optionalChain([params, 'optionalAccess', _537 => _537.embedder]))
22052
22570
  })
22053
22571
  };
22054
22572
  }
@@ -22056,7 +22574,7 @@ function summarizeEmbeddingResult(result) {
22056
22574
  if (Array.isArray(result)) {
22057
22575
  return {
22058
22576
  embedding_count: result.length,
22059
- dimensions: Array.isArray(result[0]) || Array.isArray(_optionalChain([result, 'access', _534 => _534[0], 'optionalAccess', _535 => _535.embedding])) ? result[0].length || _optionalChain([result, 'access', _536 => _536[0], 'optionalAccess', _537 => _537.embedding, 'optionalAccess', _538 => _538.length]) : void 0
22577
+ dimensions: Array.isArray(result[0]) || Array.isArray(_optionalChain([result, 'access', _538 => _538[0], 'optionalAccess', _539 => _539.embedding])) ? result[0].length || _optionalChain([result, 'access', _540 => _540[0], 'optionalAccess', _541 => _541.embedding, 'optionalAccess', _542 => _542.length]) : void 0
22060
22578
  };
22061
22579
  }
22062
22580
  if (isObject(result) && Array.isArray(result.embeddings)) {
@@ -22083,7 +22601,7 @@ function patchGenerateStreamResult(result, span, startTime) {
22083
22601
  finishSpan(async () => {
22084
22602
  const streamedText = chunks.map((chunk) => safeGet(chunk, "text")).join("");
22085
22603
  const response = await result.response;
22086
- const metrics = parseGenkitUsageMetrics(_optionalChain([response, 'optionalAccess', _539 => _539.usage]));
22604
+ const metrics = parseGenkitUsageMetrics(_optionalChain([response, 'optionalAccess', _543 => _543.usage]));
22087
22605
  if (firstChunkTime !== void 0) {
22088
22606
  metrics.time_to_first_token = firstChunkTime - startTime;
22089
22607
  }
@@ -22176,22 +22694,22 @@ function extractActionMetadata(self) {
22176
22694
  }
22177
22695
  function extractActionSpanMetadata(args) {
22178
22696
  const options = extractRunInNewSpanOptions(args);
22179
- const labels = isObject(_optionalChain([options, 'optionalAccess', _540 => _540.labels])) ? options.labels : void 0;
22180
- const metadata = isObject(_optionalChain([options, 'optionalAccess', _541 => _541.metadata])) ? options.metadata : void 0;
22181
- const actionType = stringValue(_optionalChain([labels, 'optionalAccess', _542 => _542["genkit:metadata:subtype"]]));
22182
- const name = stringValue(_optionalChain([metadata, 'optionalAccess', _543 => _543.name]));
22697
+ const labels = isObject(_optionalChain([options, 'optionalAccess', _544 => _544.labels])) ? options.labels : void 0;
22698
+ const metadata = isObject(_optionalChain([options, 'optionalAccess', _545 => _545.metadata])) ? options.metadata : void 0;
22699
+ const actionType = stringValue(_optionalChain([labels, 'optionalAccess', _546 => _546["genkit:metadata:subtype"]]));
22700
+ const name = stringValue(_optionalChain([metadata, 'optionalAccess', _547 => _547.name]));
22183
22701
  if (!actionType || !name) {
22184
22702
  return void 0;
22185
22703
  }
22186
22704
  return {
22187
22705
  actionType,
22188
- key: stringValue(_optionalChain([labels, 'optionalAccess', _544 => _544["genkit:key"]])),
22706
+ key: stringValue(_optionalChain([labels, 'optionalAccess', _548 => _548["genkit:key"]])),
22189
22707
  name
22190
22708
  };
22191
22709
  }
22192
22710
  function extractActionSpanInput(args) {
22193
22711
  const options = extractRunInNewSpanOptions(args);
22194
- if (!isObject(_optionalChain([options, 'optionalAccess', _545 => _545.metadata]))) {
22712
+ if (!isObject(_optionalChain([options, 'optionalAccess', _549 => _549.metadata]))) {
22195
22713
  return void 0;
22196
22714
  }
22197
22715
  return options.metadata.input;
@@ -22204,7 +22722,7 @@ function shouldTraceAction(metadata, runStepName) {
22204
22722
  if (runStepName) {
22205
22723
  return true;
22206
22724
  }
22207
- switch (_optionalChain([metadata, 'optionalAccess', _546 => _546.actionType])) {
22725
+ switch (_optionalChain([metadata, 'optionalAccess', _550 => _550.actionType])) {
22208
22726
  case "model":
22209
22727
  case "background-model":
22210
22728
  case "embedder":
@@ -22214,8 +22732,8 @@ function shouldTraceAction(metadata, runStepName) {
22214
22732
  }
22215
22733
  }
22216
22734
  function actionSpanName(metadata, runStepName) {
22217
- const actionType = _optionalChain([metadata, 'optionalAccess', _547 => _547.actionType]);
22218
- const name = _optionalChain([metadata, 'optionalAccess', _548 => _548.name]);
22735
+ const actionType = _optionalChain([metadata, 'optionalAccess', _551 => _551.actionType]);
22736
+ const name = _optionalChain([metadata, 'optionalAccess', _552 => _552.name]);
22219
22737
  if (actionType && name) {
22220
22738
  return `genkit.${actionType}: ${name}`;
22221
22739
  }
@@ -22228,7 +22746,7 @@ function actionSpanName(metadata, runStepName) {
22228
22746
  return "genkit.action";
22229
22747
  }
22230
22748
  function actionSpanType(metadata) {
22231
- switch (_optionalChain([metadata, 'optionalAccess', _549 => _549.actionType])) {
22749
+ switch (_optionalChain([metadata, 'optionalAccess', _553 => _553.actionType])) {
22232
22750
  case "tool":
22233
22751
  case "tool.v2":
22234
22752
  return "tool" /* TOOL */;
@@ -22243,9 +22761,9 @@ function actionMetadataForLog(metadata, runStepName) {
22243
22761
  return {
22244
22762
  ...genkitProviderMetadata(),
22245
22763
  ...pickDefined({
22246
- "genkit.action_type": _optionalChain([metadata, 'optionalAccess', _550 => _550.actionType]),
22247
- "genkit.action_name": _optionalChain([metadata, 'optionalAccess', _551 => _551.name]),
22248
- "genkit.action_key": _optionalChain([metadata, 'optionalAccess', _552 => _552.key]),
22764
+ "genkit.action_type": _optionalChain([metadata, 'optionalAccess', _554 => _554.actionType]),
22765
+ "genkit.action_name": _optionalChain([metadata, 'optionalAccess', _555 => _555.name]),
22766
+ "genkit.action_key": _optionalChain([metadata, 'optionalAccess', _556 => _556.key]),
22249
22767
  "genkit.run_name": runStepName
22250
22768
  })
22251
22769
  };
@@ -22708,10 +23226,10 @@ function injectTracingHooks2(config, state) {
22708
23226
  const existingHooks = _nullishCoalesce(config.hooks, () => ( {}));
22709
23227
  const onSessionEnd = async (input, invocation) => {
22710
23228
  try {
22711
- await _optionalChain([existingHooks, 'access', _553 => _553.onSessionEnd, 'optionalCall', _554 => _554(input, invocation)]);
23229
+ await _optionalChain([existingHooks, 'access', _557 => _557.onSessionEnd, 'optionalCall', _558 => _558(input, invocation)]);
22712
23230
  } finally {
22713
23231
  handleSessionEnd(state, input.reason, input.error);
22714
- _optionalChain([state, 'access', _555 => _555.unsubscribeEvents, 'optionalCall', _556 => _556()]);
23232
+ _optionalChain([state, 'access', _559 => _559.unsubscribeEvents, 'optionalCall', _560 => _560()]);
22715
23233
  }
22716
23234
  };
22717
23235
  config.hooks = {
@@ -22750,7 +23268,7 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
22750
23268
  if (config.model) {
22751
23269
  metadata["github_copilot.model"] = config.model;
22752
23270
  }
22753
- if (includeProviderMetadata && _optionalChain([config, 'access', _557 => _557.provider, 'optionalAccess', _558 => _558.type])) {
23271
+ if (includeProviderMetadata && _optionalChain([config, 'access', _561 => _561.provider, 'optionalAccess', _562 => _562.type])) {
22754
23272
  metadata["github_copilot.provider_type"] = config.provider.type;
22755
23273
  }
22756
23274
  if (Object.keys(metadata).length > 0) {
@@ -22832,43 +23350,1753 @@ var GitHubCopilotPlugin = class extends BasePlugin {
22832
23350
  }
22833
23351
  };
22834
23352
 
22835
- // src/instrumentation/braintrust-plugin.ts
22836
- function getIntegrationConfig(integrations, key) {
22837
- return integrations[key];
23353
+ // src/instrumentation/plugins/flue-channels.ts
23354
+ var flueChannels = defineChannels("@flue/runtime", {
23355
+ createContext: channel({
23356
+ channelName: "createFlueContext",
23357
+ kind: "sync-stream"
23358
+ }),
23359
+ openSession: channel({
23360
+ channelName: "Harness.openSession",
23361
+ kind: "async"
23362
+ }),
23363
+ contextEvent: channel({
23364
+ channelName: "context.event",
23365
+ kind: "sync-stream"
23366
+ }),
23367
+ prompt: channel({
23368
+ channelName: "session.prompt",
23369
+ kind: "async"
23370
+ }),
23371
+ skill: channel({
23372
+ channelName: "session.skill",
23373
+ kind: "async"
23374
+ }),
23375
+ task: channel({
23376
+ channelName: "session.task",
23377
+ kind: "async"
23378
+ }),
23379
+ compact: channel({
23380
+ channelName: "session.compact",
23381
+ kind: "async"
23382
+ })
23383
+ });
23384
+
23385
+ // src/wrappers/flue.ts
23386
+ var WRAPPED_FLUE_CONTEXT = /* @__PURE__ */ Symbol.for("braintrust.flue.wrapped-context");
23387
+ var WRAPPED_FLUE_HARNESS = /* @__PURE__ */ Symbol.for("braintrust.flue.wrapped-harness");
23388
+ var WRAPPED_FLUE_SESSION = /* @__PURE__ */ Symbol.for("braintrust.flue.wrapped-session");
23389
+ var SUBSCRIBED_FLUE_CONTEXT_EVENTS = /* @__PURE__ */ Symbol.for(
23390
+ "braintrust.flue.subscribed-context-events"
23391
+ );
23392
+ function patchFlueContextInPlace(ctx) {
23393
+ const context = ctx;
23394
+ if (context[WRAPPED_FLUE_CONTEXT]) {
23395
+ return ctx;
23396
+ }
23397
+ const originalInit = context.init.bind(context);
23398
+ try {
23399
+ Object.defineProperty(context, WRAPPED_FLUE_CONTEXT, {
23400
+ configurable: false,
23401
+ enumerable: false,
23402
+ value: true
23403
+ });
23404
+ Object.defineProperty(context, "init", {
23405
+ configurable: true,
23406
+ value: async function wrappedFlueInit(options) {
23407
+ const harness = await originalInit(options);
23408
+ return wrapFlueHarness(harness);
23409
+ },
23410
+ writable: true
23411
+ });
23412
+ } catch (e50) {
23413
+ }
23414
+ return ctx;
22838
23415
  }
22839
- var BraintrustPlugin = (_class20 = class extends BasePlugin {
22840
-
22841
- __init67() {this.openaiPlugin = null}
22842
- __init68() {this.openAICodexPlugin = null}
22843
- __init69() {this.anthropicPlugin = null}
22844
- __init70() {this.aiSDKPlugin = null}
22845
- __init71() {this.claudeAgentSDKPlugin = null}
22846
- __init72() {this.cursorSDKPlugin = null}
22847
- __init73() {this.googleGenAIPlugin = null}
22848
- __init74() {this.huggingFacePlugin = null}
22849
- __init75() {this.openRouterPlugin = null}
22850
- __init76() {this.openRouterAgentPlugin = null}
22851
- __init77() {this.mistralPlugin = null}
22852
- __init78() {this.googleADKPlugin = null}
22853
- __init79() {this.coherePlugin = null}
22854
- __init80() {this.groqPlugin = null}
22855
- __init81() {this.genkitPlugin = null}
22856
- __init82() {this.gitHubCopilotPlugin = null}
22857
- constructor(config = {}) {
22858
- super();_class20.prototype.__init67.call(this);_class20.prototype.__init68.call(this);_class20.prototype.__init69.call(this);_class20.prototype.__init70.call(this);_class20.prototype.__init71.call(this);_class20.prototype.__init72.call(this);_class20.prototype.__init73.call(this);_class20.prototype.__init74.call(this);_class20.prototype.__init75.call(this);_class20.prototype.__init76.call(this);_class20.prototype.__init77.call(this);_class20.prototype.__init78.call(this);_class20.prototype.__init79.call(this);_class20.prototype.__init80.call(this);_class20.prototype.__init81.call(this);_class20.prototype.__init82.call(this);;
22859
- this.config = config;
23416
+ function subscribeFlueContextEvents(ctx, options = {}) {
23417
+ if (!ctx || typeof ctx !== "object" || typeof ctx.subscribeEvent !== "function") {
23418
+ return void 0;
22860
23419
  }
22861
- onEnable() {
22862
- const integrations = this.config.integrations || {};
22863
- if (integrations.openai !== false) {
22864
- this.openaiPlugin = new OpenAIPlugin();
22865
- this.openaiPlugin.enable();
23420
+ const context = ctx;
23421
+ const captureTurnSpans = _nullishCoalesce(options.captureTurnSpans, () => ( true));
23422
+ const existingSubscription = context[SUBSCRIBED_FLUE_CONTEXT_EVENTS];
23423
+ if (existingSubscription) {
23424
+ if (existingSubscription.captureTurnSpans || !captureTurnSpans) {
23425
+ return void 0;
22866
23426
  }
22867
- if (integrations.openaiCodexSDK !== false) {
22868
- this.openAICodexPlugin = new OpenAICodexPlugin();
22869
- this.openAICodexPlugin.enable();
23427
+ try {
23428
+ existingSubscription.unsubscribe();
23429
+ } catch (e51) {
22870
23430
  }
22871
- if (integrations.anthropic !== false) {
23431
+ }
23432
+ try {
23433
+ const unsubscribe = ctx.subscribeEvent((event) => {
23434
+ flueChannels.contextEvent.traceSync(() => void 0, {
23435
+ arguments: [event],
23436
+ captureTurnSpans,
23437
+ context: ctx
23438
+ });
23439
+ });
23440
+ if (existingSubscription) {
23441
+ existingSubscription.captureTurnSpans = captureTurnSpans;
23442
+ existingSubscription.unsubscribe = unsubscribe;
23443
+ } else {
23444
+ Object.defineProperty(context, SUBSCRIBED_FLUE_CONTEXT_EVENTS, {
23445
+ configurable: false,
23446
+ enumerable: false,
23447
+ value: {
23448
+ captureTurnSpans,
23449
+ unsubscribe
23450
+ }
23451
+ });
23452
+ }
23453
+ return unsubscribe;
23454
+ } catch (e52) {
23455
+ return void 0;
23456
+ }
23457
+ }
23458
+ function wrapFlueHarness(harness) {
23459
+ if (!isPlausibleFlueHarness(harness)) {
23460
+ return harness;
23461
+ }
23462
+ const target = harness;
23463
+ if (target[WRAPPED_FLUE_HARNESS]) {
23464
+ return harness;
23465
+ }
23466
+ const originalSession = target.session.bind(target);
23467
+ try {
23468
+ Object.defineProperty(target, WRAPPED_FLUE_HARNESS, {
23469
+ configurable: false,
23470
+ enumerable: false,
23471
+ value: true
23472
+ });
23473
+ Object.defineProperty(target, "session", {
23474
+ configurable: true,
23475
+ value: async function wrappedFlueHarnessSession(name, options) {
23476
+ const session = await originalSession(name, options);
23477
+ return patchFlueSessionInPlace(session);
23478
+ },
23479
+ writable: true
23480
+ });
23481
+ const sessions = target.sessions;
23482
+ if (sessions && typeof sessions === "object") {
23483
+ patchFlueSessionFactory(sessions, "get");
23484
+ patchFlueSessionFactory(sessions, "create");
23485
+ }
23486
+ } catch (e53) {
23487
+ }
23488
+ return harness;
23489
+ }
23490
+ function patchFlueSessionInPlace(session) {
23491
+ if (session[WRAPPED_FLUE_SESSION]) {
23492
+ return session;
23493
+ }
23494
+ try {
23495
+ Object.defineProperty(session, WRAPPED_FLUE_SESSION, {
23496
+ configurable: false,
23497
+ enumerable: false,
23498
+ value: true
23499
+ });
23500
+ patchCallHandleMethod(session, "prompt", flueChannels.prompt);
23501
+ patchCallHandleMethod(session, "skill", flueChannels.skill);
23502
+ patchCallHandleMethod(session, "task", flueChannels.task);
23503
+ patchCompact(session);
23504
+ } catch (e54) {
23505
+ }
23506
+ return session;
23507
+ }
23508
+ function patchFlueSessionFactory(sessions, method) {
23509
+ const original = sessions[method];
23510
+ if (typeof original !== "function") {
23511
+ return;
23512
+ }
23513
+ const bound = original.bind(sessions);
23514
+ Object.defineProperty(sessions, method, {
23515
+ configurable: true,
23516
+ value: async function wrappedFlueSessionFactory(name, options) {
23517
+ const session = await bound(name, options);
23518
+ return patchFlueSessionInPlace(session);
23519
+ },
23520
+ writable: true
23521
+ });
23522
+ }
23523
+ function patchCallHandleMethod(session, method, channel2) {
23524
+ const original = session[method];
23525
+ if (typeof original !== "function") {
23526
+ return;
23527
+ }
23528
+ const bound = original.bind(session);
23529
+ Object.defineProperty(session, method, {
23530
+ configurable: true,
23531
+ value(input, options) {
23532
+ const args = [input, options];
23533
+ const { originalResult, traced: traced2 } = traceFlueOperation(channel2, {
23534
+ context: {
23535
+ arguments: args,
23536
+ operation: method,
23537
+ session
23538
+ },
23539
+ run: () => bound(input, options)
23540
+ });
23541
+ return preserveCallHandle(originalResult, traced2);
23542
+ },
23543
+ writable: true
23544
+ });
23545
+ }
23546
+ function patchCompact(session) {
23547
+ const original = session.compact;
23548
+ if (typeof original !== "function") {
23549
+ return;
23550
+ }
23551
+ const bound = original.bind(session);
23552
+ Object.defineProperty(session, "compact", {
23553
+ configurable: true,
23554
+ value() {
23555
+ const context = {
23556
+ arguments: [],
23557
+ operation: "compact",
23558
+ session
23559
+ };
23560
+ return flueChannels.compact.tracePromise(() => bound(), context);
23561
+ },
23562
+ writable: true
23563
+ });
23564
+ }
23565
+ function traceFlueOperation(channel2, args) {
23566
+ const tracingChannel2 = channel2.tracingChannel();
23567
+ const context = args.context;
23568
+ let originalResult;
23569
+ let traced2;
23570
+ const run = () => {
23571
+ try {
23572
+ originalResult = args.run();
23573
+ _optionalChain([tracingChannel2, 'access', _563 => _563.end, 'optionalAccess', _564 => _564.publish, 'call', _565 => _565(context)]);
23574
+ } catch (error) {
23575
+ context.error = normalizeError3(error);
23576
+ _optionalChain([tracingChannel2, 'access', _566 => _566.error, 'optionalAccess', _567 => _567.publish, 'call', _568 => _568(context)]);
23577
+ _optionalChain([tracingChannel2, 'access', _569 => _569.end, 'optionalAccess', _570 => _570.publish, 'call', _571 => _571(context)]);
23578
+ throw error;
23579
+ }
23580
+ traced2 = Promise.resolve(originalResult).then(
23581
+ (result) => {
23582
+ context.result = result;
23583
+ _optionalChain([tracingChannel2, 'access', _572 => _572.asyncStart, 'optionalAccess', _573 => _573.publish, 'call', _574 => _574(context)]);
23584
+ _optionalChain([tracingChannel2, 'access', _575 => _575.asyncEnd, 'optionalAccess', _576 => _576.publish, 'call', _577 => _577(context)]);
23585
+ return result;
23586
+ },
23587
+ (error) => {
23588
+ context.error = normalizeError3(error);
23589
+ _optionalChain([tracingChannel2, 'access', _578 => _578.error, 'optionalAccess', _579 => _579.publish, 'call', _580 => _580(context)]);
23590
+ _optionalChain([tracingChannel2, 'access', _581 => _581.asyncStart, 'optionalAccess', _582 => _582.publish, 'call', _583 => _583(context)]);
23591
+ _optionalChain([tracingChannel2, 'access', _584 => _584.asyncEnd, 'optionalAccess', _585 => _585.publish, 'call', _586 => _586(context)]);
23592
+ throw error;
23593
+ }
23594
+ );
23595
+ };
23596
+ if (_optionalChain([tracingChannel2, 'access', _587 => _587.start, 'optionalAccess', _588 => _588.runStores])) {
23597
+ tracingChannel2.start.runStores(context, run);
23598
+ } else {
23599
+ _optionalChain([tracingChannel2, 'access', _589 => _589.start, 'optionalAccess', _590 => _590.publish, 'call', _591 => _591(context)]);
23600
+ run();
23601
+ }
23602
+ return { originalResult, traced: traced2 };
23603
+ }
23604
+ function normalizeError3(error) {
23605
+ return error instanceof Error ? error : new Error(String(error));
23606
+ }
23607
+ function preserveCallHandle(originalHandle, traced2) {
23608
+ if (!isFlueCallHandle(originalHandle)) {
23609
+ return traced2;
23610
+ }
23611
+ const handle = originalHandle;
23612
+ const wrapped = {
23613
+ get signal() {
23614
+ return handle.signal;
23615
+ },
23616
+ abort(reason) {
23617
+ return handle.abort(reason);
23618
+ },
23619
+ then(onfulfilled, onrejected) {
23620
+ return traced2.then(onfulfilled, onrejected);
23621
+ }
23622
+ };
23623
+ return wrapped;
23624
+ }
23625
+ function isPlausibleFlueHarness(value) {
23626
+ return !!value && typeof value === "object" && typeof value.session === "function";
23627
+ }
23628
+ function isFlueCallHandle(value) {
23629
+ return !!value && typeof value === "object" && typeof value.then === "function" && typeof value.abort === "function" && "signal" in value;
23630
+ }
23631
+
23632
+ // src/instrumentation/plugins/flue-plugin.ts
23633
+ var FluePlugin = (_class22 = class extends BasePlugin {constructor(...args6) { super(...args6); _class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this);_class22.prototype.__init73.call(this);_class22.prototype.__init74.call(this);_class22.prototype.__init75.call(this);_class22.prototype.__init76.call(this);_class22.prototype.__init77.call(this); }
23634
+ __init71() {this.activeOperationsById = /* @__PURE__ */ new Map()}
23635
+ __init72() {this.activeOperationsByScope = /* @__PURE__ */ new Map()}
23636
+ __init73() {this.compactionsByScope = /* @__PURE__ */ new Map()}
23637
+ __init74() {this.pendingOperationsByKey = /* @__PURE__ */ new Map()}
23638
+ __init75() {this.tasksById = /* @__PURE__ */ new Map()}
23639
+ __init76() {this.toolsById = /* @__PURE__ */ new Map()}
23640
+ __init77() {this.turnsByScope = /* @__PURE__ */ new Map()}
23641
+ onEnable() {
23642
+ this.subscribeToContextCreation();
23643
+ this.subscribeToSessionCreation();
23644
+ this.subscribeToContextEvents();
23645
+ this.subscribeToSessionOperations();
23646
+ }
23647
+ onDisable() {
23648
+ for (const unsubscribe of this.unsubscribers) {
23649
+ unsubscribe();
23650
+ }
23651
+ this.unsubscribers = [];
23652
+ this.activeOperationsById.clear();
23653
+ this.activeOperationsByScope.clear();
23654
+ this.compactionsByScope.clear();
23655
+ this.pendingOperationsByKey.clear();
23656
+ this.tasksById.clear();
23657
+ this.toolsById.clear();
23658
+ this.turnsByScope.clear();
23659
+ }
23660
+ subscribeToContextCreation() {
23661
+ const channel2 = flueChannels.createContext.tracingChannel();
23662
+ const handlers = {
23663
+ end: (event) => {
23664
+ const ctx = event.result;
23665
+ if (!ctx) {
23666
+ return;
23667
+ }
23668
+ subscribeFlueContextEvents(ctx, { captureTurnSpans: false });
23669
+ patchFlueContextInPlace(ctx);
23670
+ },
23671
+ error: () => {
23672
+ }
23673
+ };
23674
+ channel2.subscribe(handlers);
23675
+ this.unsubscribers.push(() => {
23676
+ channel2.unsubscribe(handlers);
23677
+ });
23678
+ }
23679
+ subscribeToSessionCreation() {
23680
+ const channel2 = flueChannels.openSession.tracingChannel();
23681
+ const handlers = {
23682
+ asyncEnd: (event) => {
23683
+ if (event.result) {
23684
+ patchFlueSessionInPlace(
23685
+ event.result
23686
+ );
23687
+ }
23688
+ if (event.harness) {
23689
+ wrapFlueHarness(event.harness);
23690
+ }
23691
+ },
23692
+ error: () => {
23693
+ }
23694
+ };
23695
+ channel2.subscribe(handlers);
23696
+ this.unsubscribers.push(() => {
23697
+ channel2.unsubscribe(handlers);
23698
+ });
23699
+ }
23700
+ subscribeToSessionOperations() {
23701
+ this.subscribeToSessionOperation(flueChannels.prompt);
23702
+ this.subscribeToSessionOperation(flueChannels.skill);
23703
+ this.subscribeToSessionOperation(flueChannels.task);
23704
+ this.subscribeToCompact();
23705
+ }
23706
+ subscribeToSessionOperation(channel2) {
23707
+ const tracingChannel2 = channel2.tracingChannel();
23708
+ const states = /* @__PURE__ */ new WeakMap();
23709
+ const ensureState2 = (event) => {
23710
+ const existing = states.get(event);
23711
+ if (existing) {
23712
+ return existing;
23713
+ }
23714
+ const state = this.startOperationState({
23715
+ args: event.arguments,
23716
+ moduleVersion: typeof event.moduleVersion === "string" ? event.moduleVersion : void 0,
23717
+ operation: event.operation,
23718
+ session: event.session
23719
+ });
23720
+ states.set(event, state);
23721
+ return state;
23722
+ };
23723
+ const unbindCurrentSpanStore = this.bindCurrentSpanStoreToOperationStart(
23724
+ tracingChannel2,
23725
+ ensureState2
23726
+ );
23727
+ const handlers = {
23728
+ start: (event) => {
23729
+ ensureState2(event);
23730
+ },
23731
+ asyncEnd: (event) => {
23732
+ this.endOperationState(states.get(event), event.result);
23733
+ states.delete(event);
23734
+ },
23735
+ error: (event) => {
23736
+ const state = states.get(event);
23737
+ if (state && event.error) {
23738
+ safeLog3(state.span, { error: errorToString(event.error) });
23739
+ this.finishOperationState(state);
23740
+ }
23741
+ states.delete(event);
23742
+ }
23743
+ };
23744
+ tracingChannel2.subscribe(handlers);
23745
+ this.unsubscribers.push(() => {
23746
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _592 => _592()]);
23747
+ tracingChannel2.unsubscribe(handlers);
23748
+ });
23749
+ }
23750
+ subscribeToCompact() {
23751
+ const tracingChannel2 = flueChannels.compact.tracingChannel();
23752
+ const states = /* @__PURE__ */ new WeakMap();
23753
+ const ensureState2 = (event) => {
23754
+ const existing = states.get(event);
23755
+ if (existing) {
23756
+ return existing;
23757
+ }
23758
+ const state = this.startOperationState({
23759
+ args: [],
23760
+ moduleVersion: typeof event.moduleVersion === "string" ? event.moduleVersion : void 0,
23761
+ operation: event.operation,
23762
+ session: event.session
23763
+ });
23764
+ states.set(event, state);
23765
+ return state;
23766
+ };
23767
+ const unbindCurrentSpanStore = this.bindCurrentSpanStoreToOperationStart(
23768
+ tracingChannel2,
23769
+ ensureState2
23770
+ );
23771
+ const handlers = {
23772
+ start: (event) => {
23773
+ ensureState2(event);
23774
+ },
23775
+ asyncEnd: (event) => {
23776
+ this.endOperationState(states.get(event), void 0);
23777
+ states.delete(event);
23778
+ },
23779
+ error: (event) => {
23780
+ const state = states.get(event);
23781
+ if (state && event.error) {
23782
+ safeLog3(state.span, { error: errorToString(event.error) });
23783
+ this.finishOperationState(state);
23784
+ }
23785
+ states.delete(event);
23786
+ }
23787
+ };
23788
+ tracingChannel2.subscribe(handlers);
23789
+ this.unsubscribers.push(() => {
23790
+ _optionalChain([unbindCurrentSpanStore, 'optionalCall', _593 => _593()]);
23791
+ tracingChannel2.unsubscribe(handlers);
23792
+ });
23793
+ }
23794
+ subscribeToContextEvents() {
23795
+ const channel2 = flueChannels.contextEvent.tracingChannel();
23796
+ const handlers = {
23797
+ start: (event) => {
23798
+ const flueEvent = event.arguments[0];
23799
+ if (!flueEvent) {
23800
+ return;
23801
+ }
23802
+ try {
23803
+ this.handleFlueEvent(flueEvent, {
23804
+ captureTurnSpans: event.captureTurnSpans !== false
23805
+ });
23806
+ } catch (error) {
23807
+ logInstrumentationError3("Flue event", error);
23808
+ }
23809
+ },
23810
+ error: () => {
23811
+ }
23812
+ };
23813
+ channel2.subscribe(handlers);
23814
+ this.unsubscribers.push(() => {
23815
+ channel2.unsubscribe(handlers);
23816
+ });
23817
+ }
23818
+ bindCurrentSpanStoreToOperationStart(tracingChannel2, ensureState2) {
23819
+ const state = _internalGetGlobalState();
23820
+ const startChannel = tracingChannel2.start;
23821
+ const contextManager = _optionalChain([state, 'optionalAccess', _594 => _594.contextManager]);
23822
+ const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
23823
+ if (!currentSpanStore || !startChannel) {
23824
+ return void 0;
23825
+ }
23826
+ startChannel.bindStore(currentSpanStore, (event) => {
23827
+ const operationState = ensureState2(event);
23828
+ return contextManager.wrapSpanForStore(operationState.span);
23829
+ });
23830
+ return () => {
23831
+ startChannel.unbindStore(currentSpanStore);
23832
+ };
23833
+ }
23834
+ startOperationState(args) {
23835
+ const sessionName = getSessionName(args.session);
23836
+ const metadata = {
23837
+ ...extractOperationInputMetadata(args.operation, args.args),
23838
+ ...extractSessionMetadata(args.session),
23839
+ "flue.operation": args.operation,
23840
+ provider: "flue",
23841
+ ...args.moduleVersion ? { "flue.version": args.moduleVersion } : {}
23842
+ };
23843
+ const span = startSpan({
23844
+ name: `flue.session.${args.operation}`,
23845
+ spanAttributes: { type: "task" /* TASK */ }
23846
+ });
23847
+ const state = {
23848
+ metadata,
23849
+ operation: args.operation,
23850
+ sessionName,
23851
+ span,
23852
+ startTime: getCurrentUnixTimestamp()
23853
+ };
23854
+ safeLog3(span, {
23855
+ input: extractOperationInput(args.operation, args.args),
23856
+ metadata
23857
+ });
23858
+ this.pendingOperationQueue(operationKey(sessionName, args.operation)).push(
23859
+ state
23860
+ );
23861
+ addOperationToScope(
23862
+ this.activeOperationsByScope,
23863
+ _nullishCoalesce(sessionName, () => ( "unknown")),
23864
+ state
23865
+ );
23866
+ return state;
23867
+ }
23868
+ endOperationState(state, result) {
23869
+ if (!state) {
23870
+ return;
23871
+ }
23872
+ const metadata = {
23873
+ ...state.metadata,
23874
+ ...extractPromptResponseMetadata(result)
23875
+ };
23876
+ const metrics = {
23877
+ ...buildDurationMetrics3(state.startTime),
23878
+ ...metricsFromUsage(_optionalChain([result, 'optionalAccess', _595 => _595.usage]))
23879
+ };
23880
+ safeLog3(state.span, {
23881
+ metadata,
23882
+ metrics,
23883
+ output: extractOperationOutput(result)
23884
+ });
23885
+ this.finishCompactionsForOperation(state);
23886
+ this.finishOperationState(state);
23887
+ }
23888
+ finishOperationState(state) {
23889
+ removePendingOperation(this.pendingOperationsByKey, state);
23890
+ if (state.operationId) {
23891
+ this.activeOperationsById.delete(state.operationId);
23892
+ }
23893
+ removeScopedOperation(this.activeOperationsByScope, state);
23894
+ state.span.end();
23895
+ }
23896
+ handleFlueEvent(event, options) {
23897
+ switch (event.type) {
23898
+ case "operation_start":
23899
+ this.handleOperationStart(event);
23900
+ return;
23901
+ case "operation":
23902
+ this.handleOperation(event);
23903
+ return;
23904
+ case "text_delta":
23905
+ if (!options.captureTurnSpans) {
23906
+ return;
23907
+ }
23908
+ this.ensureTurnState(event).text.push(
23909
+ typeof event.text === "string" ? event.text : ""
23910
+ );
23911
+ return;
23912
+ case "thinking_start":
23913
+ if (!options.captureTurnSpans) {
23914
+ return;
23915
+ }
23916
+ this.handleThinkingStart(event);
23917
+ return;
23918
+ case "thinking_delta":
23919
+ if (!options.captureTurnSpans) {
23920
+ return;
23921
+ }
23922
+ this.handleThinkingDelta(event);
23923
+ return;
23924
+ case "thinking_end":
23925
+ if (!options.captureTurnSpans) {
23926
+ return;
23927
+ }
23928
+ this.handleThinkingEnd(event);
23929
+ return;
23930
+ case "turn":
23931
+ if (!options.captureTurnSpans) {
23932
+ return;
23933
+ }
23934
+ this.handleTurn(event);
23935
+ return;
23936
+ case "tool_start":
23937
+ this.handleToolStart(event, options);
23938
+ return;
23939
+ case "tool_call":
23940
+ this.handleToolCall(event);
23941
+ return;
23942
+ case "task_start":
23943
+ this.handleTaskStart(event);
23944
+ return;
23945
+ case "task":
23946
+ this.handleTask(event);
23947
+ return;
23948
+ case "compaction_start":
23949
+ this.handleCompactionStart(event);
23950
+ return;
23951
+ case "compaction":
23952
+ this.handleCompaction(event);
23953
+ return;
23954
+ default:
23955
+ return;
23956
+ }
23957
+ }
23958
+ handleOperationStart(event) {
23959
+ if (!isInstrumentedOperation(event.operationKind)) {
23960
+ return;
23961
+ }
23962
+ const state = this.takePendingOperationForEvent(event);
23963
+ if (!state) {
23964
+ return;
23965
+ }
23966
+ state.operationId = event.operationId;
23967
+ this.activeOperationsById.set(event.operationId, state);
23968
+ addScopedOperation(this.activeOperationsByScope, event, state);
23969
+ state.metadata = {
23970
+ ...state.metadata,
23971
+ ...extractEventMetadata(event),
23972
+ "flue.operation_id": event.operationId
23973
+ };
23974
+ safeLog3(state.span, { metadata: state.metadata });
23975
+ }
23976
+ handleOperation(event) {
23977
+ const state = event.operationId ? this.activeOperationsById.get(event.operationId) : void 0;
23978
+ if (!state) {
23979
+ return;
23980
+ }
23981
+ const metadata = {
23982
+ ...state.metadata,
23983
+ ...extractEventMetadata(event),
23984
+ ...typeof event.durationMs === "number" ? { "flue.duration_ms": event.durationMs } : {},
23985
+ ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
23986
+ };
23987
+ const metrics = metricsFromUsage(event.usage);
23988
+ safeLog3(state.span, {
23989
+ ...event.error ? { error: errorToString(event.error) } : {},
23990
+ metadata,
23991
+ ...Object.keys(metrics).length ? { metrics } : {}
23992
+ });
23993
+ }
23994
+ ensureTurnState(event) {
23995
+ const scope = scopeKey(event);
23996
+ const existing = this.turnsByScope.get(scope);
23997
+ if (existing) {
23998
+ return existing;
23999
+ }
24000
+ const parent = this.parentSpanForEvent(event);
24001
+ const metadata = {
24002
+ ...extractEventMetadata(event),
24003
+ provider: "flue"
24004
+ };
24005
+ const span = startFlueSpan(parent, {
24006
+ name: "flue.turn",
24007
+ spanAttributes: { type: "llm" /* LLM */ }
24008
+ });
24009
+ const state = {
24010
+ metadata,
24011
+ span,
24012
+ hasThinking: false,
24013
+ startTime: getCurrentUnixTimestamp(),
24014
+ text: [],
24015
+ thinking: [],
24016
+ toolCalls: []
24017
+ };
24018
+ safeLog3(span, { metadata });
24019
+ this.turnsByScope.set(scope, state);
24020
+ return state;
24021
+ }
24022
+ handleTurn(event) {
24023
+ const scope = scopeKey(event);
24024
+ const state = this.ensureTurnState(event);
24025
+ const text = state.text.join("");
24026
+ const reasoning = _nullishCoalesce(state.finalThinking, () => ( state.thinking.join("")));
24027
+ const outputReasoning = reasoning || (state.hasThinking ? "[reasoning stream present; content unavailable]" : void 0);
24028
+ const metadata = {
24029
+ ...state.metadata,
24030
+ ...extractEventMetadata(event),
24031
+ ...event.model ? { model: event.model, "flue.model": event.model } : {},
24032
+ ...event.stopReason ? { "flue.stop_reason": event.stopReason } : {},
24033
+ ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {},
24034
+ provider: "flue"
24035
+ };
24036
+ safeLog3(state.span, {
24037
+ ...event.error ? { error: errorToString(event.error) } : {},
24038
+ metadata,
24039
+ metrics: {
24040
+ ...durationMsMetrics(event.durationMs),
24041
+ ...metricsFromUsage(event.usage)
24042
+ },
24043
+ output: toAssistantOutput(
24044
+ text,
24045
+ event.stopReason,
24046
+ outputReasoning,
24047
+ state.toolCalls
24048
+ )
24049
+ });
24050
+ state.span.end();
24051
+ this.turnsByScope.delete(scope);
24052
+ }
24053
+ handleThinkingDelta(event) {
24054
+ const delta = event.delta;
24055
+ if (typeof delta !== "string" || !delta) {
24056
+ return;
24057
+ }
24058
+ const state = this.ensureTurnState(event);
24059
+ state.hasThinking = true;
24060
+ state.metadata["flue.thinking"] = true;
24061
+ state.thinking.push(delta);
24062
+ }
24063
+ handleThinkingStart(event) {
24064
+ const state = this.ensureTurnState(event);
24065
+ state.hasThinking = true;
24066
+ state.metadata["flue.thinking"] = true;
24067
+ }
24068
+ handleThinkingEnd(event) {
24069
+ const state = this.ensureTurnState(event);
24070
+ state.hasThinking = true;
24071
+ state.metadata["flue.thinking"] = true;
24072
+ if (typeof event.content === "string" && event.content) {
24073
+ state.finalThinking = event.content;
24074
+ }
24075
+ }
24076
+ handleToolStart(event, options) {
24077
+ const toolCallId = event.toolCallId;
24078
+ if (!toolCallId) {
24079
+ return;
24080
+ }
24081
+ const parent = this.parentSpanForEvent(event);
24082
+ const scope = scopeKey(event);
24083
+ let turnState = this.turnsByScope.get(scope);
24084
+ if (!turnState && parent && options.captureTurnSpans) {
24085
+ turnState = this.ensureTurnState(event);
24086
+ }
24087
+ const metadata = {
24088
+ ...extractEventMetadata(event),
24089
+ ...event.toolName ? { "flue.tool_name": event.toolName } : {},
24090
+ "flue.tool_call_id": toolCallId,
24091
+ provider: "flue"
24092
+ };
24093
+ const span = startFlueSpan(parent, {
24094
+ name: `tool: ${_nullishCoalesce(event.toolName, () => ( "unknown"))}`,
24095
+ spanAttributes: { type: "tool" /* TOOL */ }
24096
+ });
24097
+ if (turnState) {
24098
+ turnState.toolCalls.push({
24099
+ args: event.args,
24100
+ toolCallId,
24101
+ toolName: event.toolName
24102
+ });
24103
+ }
24104
+ safeLog3(span, {
24105
+ input: event.args,
24106
+ metadata
24107
+ });
24108
+ this.toolsById.set(toolKey(event), {
24109
+ metadata,
24110
+ span,
24111
+ startTime: getCurrentUnixTimestamp()
24112
+ });
24113
+ }
24114
+ handleToolCall(event) {
24115
+ const key = toolKey(event);
24116
+ const state = _nullishCoalesce(this.toolsById.get(key), () => ( this.startSyntheticToolState(event, _nullishCoalesce(event.toolName, () => ( "unknown")))));
24117
+ const metadata = {
24118
+ ...state.metadata,
24119
+ ...extractEventMetadata(event),
24120
+ ...event.toolName ? { "flue.tool_name": event.toolName } : {},
24121
+ ...event.toolCallId ? { "flue.tool_call_id": event.toolCallId } : {},
24122
+ ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
24123
+ };
24124
+ safeLog3(state.span, {
24125
+ ...event.isError ? { error: errorToString(event.result) } : {},
24126
+ metadata,
24127
+ metrics: durationMsMetrics(event.durationMs),
24128
+ output: event.result
24129
+ });
24130
+ state.span.end();
24131
+ this.toolsById.delete(key);
24132
+ }
24133
+ handleTaskStart(event) {
24134
+ const parent = this.parentSpanForEvent(event);
24135
+ const metadata = {
24136
+ ...extractEventMetadata(event),
24137
+ ...event.role ? { "flue.role": event.role } : {},
24138
+ ...event.cwd ? { "flue.cwd": event.cwd } : {},
24139
+ "flue.task_id": event.taskId,
24140
+ provider: "flue"
24141
+ };
24142
+ const span = startFlueSpan(parent, {
24143
+ name: "flue.task",
24144
+ spanAttributes: { type: "task" /* TASK */ }
24145
+ });
24146
+ safeLog3(span, {
24147
+ input: event.prompt,
24148
+ metadata
24149
+ });
24150
+ this.tasksById.set(event.taskId, {
24151
+ metadata,
24152
+ span,
24153
+ startTime: getCurrentUnixTimestamp()
24154
+ });
24155
+ }
24156
+ handleTask(event) {
24157
+ const state = this.tasksById.get(event.taskId);
24158
+ if (!state) {
24159
+ return;
24160
+ }
24161
+ safeLog3(state.span, {
24162
+ ...event.isError ? { error: errorToString(event.result) } : {},
24163
+ metadata: {
24164
+ ...state.metadata,
24165
+ ...extractEventMetadata(event),
24166
+ ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
24167
+ },
24168
+ metrics: durationMsMetrics(event.durationMs),
24169
+ output: event.result
24170
+ });
24171
+ state.span.end();
24172
+ this.tasksById.delete(event.taskId);
24173
+ }
24174
+ handleCompactionStart(event) {
24175
+ const operationState = this.operationStateForEvent(event);
24176
+ const parent = _nullishCoalesce(_optionalChain([operationState, 'optionalAccess', _596 => _596.span]), () => ( this.parentSpanForEvent(event)));
24177
+ const metadata = {
24178
+ ...extractEventMetadata(event),
24179
+ ...event.reason ? { "flue.compaction_reason": event.reason } : {},
24180
+ provider: "flue"
24181
+ };
24182
+ const input = {
24183
+ ...typeof event.estimatedTokens === "number" ? { estimatedTokens: event.estimatedTokens } : {},
24184
+ ...event.reason ? { reason: event.reason } : {}
24185
+ };
24186
+ const span = startFlueSpan(parent, {
24187
+ name: "flue.compaction",
24188
+ spanAttributes: { type: "task" /* TASK */ }
24189
+ });
24190
+ safeLog3(span, {
24191
+ input,
24192
+ metadata
24193
+ });
24194
+ this.compactionsByScope.set(scopeKey(event), {
24195
+ input,
24196
+ metadata,
24197
+ operationState,
24198
+ span,
24199
+ startTime: getCurrentUnixTimestamp()
24200
+ });
24201
+ }
24202
+ handleCompaction(event) {
24203
+ const key = scopeKey(event);
24204
+ const state = _nullishCoalesce(this.compactionsByScope.get(key), () => ( this.findCompactionState(event)));
24205
+ if (!state) {
24206
+ return;
24207
+ }
24208
+ safeLog3(state.span, {
24209
+ metadata: {
24210
+ ...state.metadata,
24211
+ ...extractEventMetadata(event),
24212
+ ...typeof event.messagesBefore === "number" ? { "flue.messages_before": event.messagesBefore } : {},
24213
+ ...typeof event.messagesAfter === "number" ? { "flue.messages_after": event.messagesAfter } : {}
24214
+ },
24215
+ metrics: {
24216
+ ...durationMsMetrics(event.durationMs),
24217
+ ...metricsFromUsage(event.usage)
24218
+ },
24219
+ output: {
24220
+ messagesAfter: event.messagesAfter,
24221
+ messagesBefore: event.messagesBefore
24222
+ }
24223
+ });
24224
+ state.span.end();
24225
+ this.deleteCompactionState(state);
24226
+ }
24227
+ findCompactionState(event) {
24228
+ const operationState = this.operationStateForEvent(event);
24229
+ for (const state of this.compactionsByScope.values()) {
24230
+ if (operationState && state.operationState === operationState) {
24231
+ return state;
24232
+ }
24233
+ }
24234
+ return void 0;
24235
+ }
24236
+ finishCompactionsForOperation(operationState) {
24237
+ for (const state of [...this.compactionsByScope.values()]) {
24238
+ if (state.operationState !== operationState) {
24239
+ continue;
24240
+ }
24241
+ safeLog3(state.span, {
24242
+ input: state.input,
24243
+ metadata: state.metadata,
24244
+ metrics: {
24245
+ ...buildDurationMetrics3(state.startTime)
24246
+ },
24247
+ output: { completed: true }
24248
+ });
24249
+ state.span.end();
24250
+ this.deleteCompactionState(state);
24251
+ }
24252
+ }
24253
+ deleteCompactionState(state) {
24254
+ for (const [key, candidate] of this.compactionsByScope) {
24255
+ if (candidate !== state) {
24256
+ continue;
24257
+ }
24258
+ this.compactionsByScope.delete(key);
24259
+ return;
24260
+ }
24261
+ }
24262
+ startSyntheticToolState(event, toolName) {
24263
+ const parent = this.parentSpanForEvent(event);
24264
+ const metadata = {
24265
+ ...extractEventMetadata(event),
24266
+ ...event.toolCallId ? { "flue.tool_call_id": event.toolCallId } : {},
24267
+ "flue.tool_name": toolName,
24268
+ provider: "flue"
24269
+ };
24270
+ const span = startFlueSpan(parent, {
24271
+ name: `tool: ${toolName}`,
24272
+ spanAttributes: { type: "tool" /* TOOL */ }
24273
+ });
24274
+ safeLog3(span, { metadata });
24275
+ return { metadata, span, startTime: getCurrentUnixTimestamp() };
24276
+ }
24277
+ operationStateForEvent(event) {
24278
+ if (event.operationId) {
24279
+ const operation = _nullishCoalesce(this.activeOperationsById.get(event.operationId), () => ( this.promotePendingOperationForEvent(event)));
24280
+ if (operation) {
24281
+ return operation;
24282
+ }
24283
+ }
24284
+ return _nullishCoalesce(this.activeOperationForEventScope(event), () => ( this.pendingOperationForEventScope(event)));
24285
+ }
24286
+ parentSpanForEvent(event) {
24287
+ if (event.operationId) {
24288
+ const operation = this.operationStateForEvent(event);
24289
+ if (operation) {
24290
+ return operation.span;
24291
+ }
24292
+ }
24293
+ if (event.taskId) {
24294
+ return _optionalChain([this, 'access', _597 => _597.tasksById, 'access', _598 => _598.get, 'call', _599 => _599(event.taskId), 'optionalAccess', _600 => _600.span]);
24295
+ }
24296
+ return _optionalChain([this, 'access', _601 => _601.operationStateForEvent, 'call', _602 => _602(event), 'optionalAccess', _603 => _603.span]);
24297
+ }
24298
+ promotePendingOperationForEvent(event) {
24299
+ if (!event.operationId) {
24300
+ return void 0;
24301
+ }
24302
+ const scopePrefixes = operationScopePrefixes(event);
24303
+ for (const [candidateKey, candidateQueue] of this.pendingOperationsByKey) {
24304
+ if (!candidateQueue.length || !operationKeyMatchesScopes(candidateKey, scopePrefixes)) {
24305
+ continue;
24306
+ }
24307
+ const state = candidateQueue.shift();
24308
+ if (!state) {
24309
+ return void 0;
24310
+ }
24311
+ state.operationId = event.operationId;
24312
+ this.activeOperationsById.set(event.operationId, state);
24313
+ addScopedOperation(this.activeOperationsByScope, event, state);
24314
+ state.metadata = {
24315
+ ...state.metadata,
24316
+ ...extractEventMetadata(event),
24317
+ "flue.operation_id": event.operationId
24318
+ };
24319
+ safeLog3(state.span, { metadata: state.metadata });
24320
+ return state;
24321
+ }
24322
+ return void 0;
24323
+ }
24324
+ activeOperationForEventScope(event) {
24325
+ for (const scope of operationScopeNames(event)) {
24326
+ const operations = this.activeOperationsByScope.get(scope);
24327
+ if (_optionalChain([operations, 'optionalAccess', _604 => _604.length])) {
24328
+ return operations[operations.length - 1];
24329
+ }
24330
+ }
24331
+ return void 0;
24332
+ }
24333
+ pendingOperationForEventScope(event) {
24334
+ const scopePrefixes = operationScopePrefixes(event);
24335
+ for (const [candidateKey, candidateQueue] of this.pendingOperationsByKey) {
24336
+ if (!candidateQueue.length || !operationKeyMatchesScopes(candidateKey, scopePrefixes)) {
24337
+ continue;
24338
+ }
24339
+ return candidateQueue[0];
24340
+ }
24341
+ return void 0;
24342
+ }
24343
+ takePendingOperationForEvent(event) {
24344
+ const key = operationKey(event.session, event.operationKind);
24345
+ const queue2 = this.pendingOperationsByKey.get(key);
24346
+ if (_optionalChain([queue2, 'optionalAccess', _605 => _605.length])) {
24347
+ return queue2.shift();
24348
+ }
24349
+ for (const [candidateKey, candidateQueue] of this.pendingOperationsByKey) {
24350
+ if (candidateKey.endsWith(`::${event.operationKind}`) && candidateQueue.length) {
24351
+ return candidateQueue.shift();
24352
+ }
24353
+ }
24354
+ return void 0;
24355
+ }
24356
+ pendingOperationQueue(key) {
24357
+ const existing = this.pendingOperationsByKey.get(key);
24358
+ if (existing) {
24359
+ return existing;
24360
+ }
24361
+ const queue2 = [];
24362
+ this.pendingOperationsByKey.set(key, queue2);
24363
+ return queue2;
24364
+ }
24365
+ }, _class22);
24366
+ function isInstrumentedOperation(operation) {
24367
+ return operation === "prompt" || operation === "skill" || operation === "task" || operation === "compact";
24368
+ }
24369
+ function getSessionName(session) {
24370
+ return typeof _optionalChain([session, 'optionalAccess', _606 => _606.name]) === "string" ? session.name : void 0;
24371
+ }
24372
+ function operationKey(sessionName, operation) {
24373
+ return `${_nullishCoalesce(sessionName, () => ( "unknown"))}::${operation}`;
24374
+ }
24375
+ function operationScopePrefixes(event) {
24376
+ const scopes = /* @__PURE__ */ new Set();
24377
+ for (const scope of operationScopeNames(event)) {
24378
+ scopes.add(`${scope}::`);
24379
+ }
24380
+ return scopes;
24381
+ }
24382
+ function operationKeyMatchesScopes(key, scopes) {
24383
+ for (const scope of scopes) {
24384
+ if (key.startsWith(scope)) {
24385
+ return true;
24386
+ }
24387
+ }
24388
+ return false;
24389
+ }
24390
+ function operationScopeNames(event) {
24391
+ const scopes = /* @__PURE__ */ new Set();
24392
+ if (event.session) {
24393
+ scopes.add(event.session);
24394
+ }
24395
+ if (event.parentSession) {
24396
+ scopes.add(event.parentSession);
24397
+ }
24398
+ if (!scopes.size) {
24399
+ scopes.add("unknown");
24400
+ }
24401
+ return scopes;
24402
+ }
24403
+ function addScopedOperation(operationsByScope, event, state) {
24404
+ for (const scope of operationScopeNames(event)) {
24405
+ addOperationToScope(operationsByScope, scope, state);
24406
+ }
24407
+ }
24408
+ function addOperationToScope(operationsByScope, scope, state) {
24409
+ const operations = operationsByScope.get(scope);
24410
+ if (operations) {
24411
+ if (!operations.includes(state)) {
24412
+ operations.push(state);
24413
+ }
24414
+ } else {
24415
+ operationsByScope.set(scope, [state]);
24416
+ }
24417
+ }
24418
+ function removeScopedOperation(operationsByScope, state) {
24419
+ for (const [scope, operations] of operationsByScope) {
24420
+ const index = operations.indexOf(state);
24421
+ if (index === -1) {
24422
+ continue;
24423
+ }
24424
+ operations.splice(index, 1);
24425
+ if (operations.length === 0) {
24426
+ operationsByScope.delete(scope);
24427
+ }
24428
+ }
24429
+ }
24430
+ function removePendingOperation(pendingOperationsByKey, state) {
24431
+ for (const [key, queue2] of pendingOperationsByKey) {
24432
+ const index = queue2.indexOf(state);
24433
+ if (index === -1) {
24434
+ continue;
24435
+ }
24436
+ queue2.splice(index, 1);
24437
+ if (queue2.length === 0) {
24438
+ pendingOperationsByKey.delete(key);
24439
+ }
24440
+ return;
24441
+ }
24442
+ }
24443
+ function extractSessionMetadata(session) {
24444
+ const sessionName = getSessionName(session);
24445
+ return sessionName ? { "flue.session": sessionName } : {};
24446
+ }
24447
+ function extractEventMetadata(event) {
24448
+ return {
24449
+ ...event.runId ? { "flue.run_id": event.runId } : {},
24450
+ ...typeof event.eventIndex === "number" ? { "flue.event_index": event.eventIndex } : {},
24451
+ ...event.session ? { "flue.session": event.session } : {},
24452
+ ...event.parentSession ? { "flue.parent_session": event.parentSession } : {},
24453
+ ...event.harness ? { "flue.harness": event.harness } : {},
24454
+ ...event.taskId ? { "flue.task_id": event.taskId } : {},
24455
+ ...event.operationId ? { "flue.operation_id": event.operationId } : {}
24456
+ };
24457
+ }
24458
+ function extractOperationInput(operation, args) {
24459
+ switch (operation) {
24460
+ case "prompt":
24461
+ case "task":
24462
+ return args[0];
24463
+ case "skill":
24464
+ return {
24465
+ args: _optionalChain([getOptionObject, 'call', _607 => _607(args[1]), 'optionalAccess', _608 => _608.args]),
24466
+ name: args[0]
24467
+ };
24468
+ case "compact":
24469
+ return void 0;
24470
+ }
24471
+ }
24472
+ function extractOperationInputMetadata(operation, args) {
24473
+ const options = getOptionObject(args[1]);
24474
+ return {
24475
+ ...operation === "skill" && typeof args[0] === "string" ? { "flue.skill_name": args[0] } : {},
24476
+ ..._optionalChain([options, 'optionalAccess', _609 => _609.model]) ? { model: options.model, "flue.model": options.model } : {},
24477
+ ..._optionalChain([options, 'optionalAccess', _610 => _610.role]) ? { "flue.role": options.role } : {},
24478
+ ..._optionalChain([options, 'optionalAccess', _611 => _611.thinkingLevel]) ? { "flue.thinking_level": options.thinkingLevel } : {},
24479
+ ...typeof _optionalChain([options, 'optionalAccess', _612 => _612.cwd]) === "string" ? { "flue.cwd": options.cwd } : {},
24480
+ ...Array.isArray(_optionalChain([options, 'optionalAccess', _613 => _613.tools])) ? {
24481
+ "flue.tools_count": options.tools.length,
24482
+ tools: summarizeTools(options.tools)
24483
+ } : {},
24484
+ ...Array.isArray(_optionalChain([options, 'optionalAccess', _614 => _614.images])) ? { "flue.images_count": options.images.length } : {},
24485
+ ..._optionalChain([options, 'optionalAccess', _615 => _615.result]) || _optionalChain([options, 'optionalAccess', _616 => _616.schema]) ? { "flue.result_schema": true } : {}
24486
+ };
24487
+ }
24488
+ function getOptionObject(value) {
24489
+ return isObject(value) ? value : void 0;
24490
+ }
24491
+ function summarizeTools(tools) {
24492
+ return tools.flatMap((tool) => {
24493
+ if (!isObject(tool)) {
24494
+ return [];
24495
+ }
24496
+ const name = typeof tool.name === "string" ? tool.name : void 0;
24497
+ if (!name) {
24498
+ return [];
24499
+ }
24500
+ return [
24501
+ {
24502
+ function: {
24503
+ description: typeof tool.description === "string" ? tool.description : void 0,
24504
+ name,
24505
+ parameters: tool.parameters
24506
+ },
24507
+ type: "function"
24508
+ }
24509
+ ];
24510
+ });
24511
+ }
24512
+ function extractPromptResponseMetadata(result) {
24513
+ const modelId = _optionalChain([result, 'optionalAccess', _617 => _617.model]) && typeof result.model.id === "string" ? result.model.id : void 0;
24514
+ return modelId ? {
24515
+ model: modelId,
24516
+ "flue.model": modelId
24517
+ } : {};
24518
+ }
24519
+ function extractOperationOutput(result) {
24520
+ if (!result) {
24521
+ return void 0;
24522
+ }
24523
+ if ("data" in result) {
24524
+ return result.data;
24525
+ }
24526
+ if ("text" in result) {
24527
+ return result.text;
24528
+ }
24529
+ return result;
24530
+ }
24531
+ function metricsFromUsage(usage) {
24532
+ return {
24533
+ ...typeof _optionalChain([usage, 'optionalAccess', _618 => _618.input]) === "number" ? { prompt_tokens: usage.input } : {},
24534
+ ...typeof _optionalChain([usage, 'optionalAccess', _619 => _619.output]) === "number" ? { completion_tokens: usage.output } : {},
24535
+ ...typeof _optionalChain([usage, 'optionalAccess', _620 => _620.cacheRead]) === "number" ? { prompt_cached_tokens: usage.cacheRead } : {},
24536
+ ...typeof _optionalChain([usage, 'optionalAccess', _621 => _621.cacheWrite]) === "number" ? { prompt_cache_creation_tokens: usage.cacheWrite } : {},
24537
+ ...typeof _optionalChain([usage, 'optionalAccess', _622 => _622.totalTokens]) === "number" ? { tokens: usage.totalTokens } : {},
24538
+ ...typeof _optionalChain([usage, 'optionalAccess', _623 => _623.cost, 'optionalAccess', _624 => _624.total]) === "number" ? { estimated_cost: usage.cost.total } : {}
24539
+ };
24540
+ }
24541
+ function buildDurationMetrics3(startTime) {
24542
+ return {
24543
+ duration_ms: Math.max(0, (getCurrentUnixTimestamp() - startTime) * 1e3)
24544
+ };
24545
+ }
24546
+ function durationMsMetrics(durationMs) {
24547
+ return typeof durationMs === "number" ? { duration_ms: durationMs } : {};
24548
+ }
24549
+ function scopeKey(event) {
24550
+ if (event.operationId) {
24551
+ return `operation:${event.operationId}`;
24552
+ }
24553
+ if (event.taskId) {
24554
+ return `task:${event.taskId}`;
24555
+ }
24556
+ if (event.session) {
24557
+ return `session:${event.session}`;
24558
+ }
24559
+ return "flue:unknown";
24560
+ }
24561
+ function toolKey(event) {
24562
+ return `${scopeKey(event)}::tool:${_nullishCoalesce(event.toolCallId, () => ( "unknown"))}`;
24563
+ }
24564
+ function toAssistantOutput(text, finishReason, reasoning, toolCalls) {
24565
+ return [
24566
+ {
24567
+ finish_reason: _nullishCoalesce(finishReason, () => ( "stop")),
24568
+ index: 0,
24569
+ message: {
24570
+ content: text,
24571
+ ...reasoning ? { reasoning } : {},
24572
+ role: "assistant",
24573
+ ..._optionalChain([toolCalls, 'optionalAccess', _625 => _625.length]) ? {
24574
+ tool_calls: toolCalls.map((toolCall) => ({
24575
+ function: {
24576
+ arguments: toolCall.args === void 0 ? "{}" : JSON.stringify(toolCall.args),
24577
+ name: _nullishCoalesce(toolCall.toolName, () => ( "unknown"))
24578
+ },
24579
+ ...toolCall.toolCallId ? { id: toolCall.toolCallId } : {},
24580
+ type: "function"
24581
+ }))
24582
+ } : {}
24583
+ }
24584
+ }
24585
+ ];
24586
+ }
24587
+ function startFlueSpan(parent, args) {
24588
+ return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
24589
+ }
24590
+ function safeLog3(span, event) {
24591
+ try {
24592
+ span.log(event);
24593
+ } catch (error) {
24594
+ logInstrumentationError3("Flue span log", error);
24595
+ }
24596
+ }
24597
+ function errorToString(error) {
24598
+ if (error instanceof Error) {
24599
+ return error.message;
24600
+ }
24601
+ if (typeof error === "string") {
24602
+ return error;
24603
+ }
24604
+ try {
24605
+ return JSON.stringify(error);
24606
+ } catch (e55) {
24607
+ return String(error);
24608
+ }
24609
+ }
24610
+ function logInstrumentationError3(label, error) {
24611
+ console.error(`Error in ${label} instrumentation:`, error);
24612
+ }
24613
+
24614
+ // src/wrappers/langchain/callback-handler.ts
24615
+ var BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME = "BraintrustCallbackHandler";
24616
+ var BraintrustLangChainCallbackHandler = (_class23 = class {
24617
+ __init78() {this.name = BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME}
24618
+ __init79() {this.spans = /* @__PURE__ */ new Map()}
24619
+ __init80() {this.skippedRuns = /* @__PURE__ */ new Set()}
24620
+
24621
+
24622
+
24623
+ __init81() {this.startTimes = /* @__PURE__ */ new Map()}
24624
+ __init82() {this.firstTokenTimes = /* @__PURE__ */ new Map()}
24625
+ __init83() {this.ttftMs = /* @__PURE__ */ new Map()}
24626
+ constructor(options) {;_class23.prototype.__init78.call(this);_class23.prototype.__init79.call(this);_class23.prototype.__init80.call(this);_class23.prototype.__init81.call(this);_class23.prototype.__init82.call(this);_class23.prototype.__init83.call(this);
24627
+ this.parent = _optionalChain([options, 'optionalAccess', _626 => _626.parent]);
24628
+ this.options = {
24629
+ debug: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _627 => _627.debug]), () => ( false)),
24630
+ excludeMetadataProps: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _628 => _628.excludeMetadataProps]), () => ( /^(l[sc]_|langgraph_|__pregel_|checkpoint_ns)/)),
24631
+ logger: _optionalChain([options, 'optionalAccess', _629 => _629.logger])
24632
+ };
24633
+ }
24634
+ startSpan({
24635
+ runId,
24636
+ parentRunId,
24637
+ ...args
24638
+ }) {
24639
+ if (this.spans.has(runId)) {
24640
+ return;
24641
+ }
24642
+ if (!parentRunId) {
24643
+ this.rootRunId = runId;
24644
+ }
24645
+ const tags = _optionalChain([args, 'access', _630 => _630.event, 'optionalAccess', _631 => _631.tags]);
24646
+ const spanAttributes = args.spanAttributes || {};
24647
+ spanAttributes.type = args.type || spanAttributes.type || "task";
24648
+ args.type = spanAttributes.type;
24649
+ const currentParent = _nullishCoalesce((typeof this.parent === "function" ? this.parent() : this.parent), () => ( currentSpan()));
24650
+ let parentSpan;
24651
+ if (parentRunId && this.spans.has(parentRunId)) {
24652
+ parentSpan = this.spans.get(parentRunId);
24653
+ } else if (!Object.is(currentParent, NOOP_SPAN)) {
24654
+ parentSpan = currentParent;
24655
+ } else if (this.options.logger) {
24656
+ parentSpan = this.options.logger;
24657
+ } else {
24658
+ parentSpan = { startSpan };
24659
+ }
24660
+ args.event = {
24661
+ ...args.event,
24662
+ tags: void 0,
24663
+ metadata: {
24664
+ ...tags ? { tags } : {},
24665
+ ..._optionalChain([args, 'access', _632 => _632.event, 'optionalAccess', _633 => _633.metadata]),
24666
+ braintrust: {
24667
+ integration_name: "langchain-js",
24668
+ sdk_language: "javascript"
24669
+ },
24670
+ run_id: runId,
24671
+ parent_run_id: parentRunId,
24672
+ ...this.options.debug ? { runId, parentRunId } : {}
24673
+ }
24674
+ };
24675
+ let span = parentSpan.startSpan(args);
24676
+ if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
24677
+ span = initLogger().startSpan(args);
24678
+ }
24679
+ this.spans.set(runId, span);
24680
+ }
24681
+ endSpan({
24682
+ runId,
24683
+ parentRunId,
24684
+ tags,
24685
+ metadata,
24686
+ ...args
24687
+ }) {
24688
+ if (!this.spans.has(runId)) {
24689
+ return;
24690
+ }
24691
+ if (this.skippedRuns.has(runId)) {
24692
+ this.skippedRuns.delete(runId);
24693
+ return;
24694
+ }
24695
+ const span = this.spans.get(runId);
24696
+ this.spans.delete(runId);
24697
+ if (runId === this.rootRunId) {
24698
+ this.rootRunId = void 0;
24699
+ }
24700
+ span.log({ ...args, metadata: { tags, ...metadata } });
24701
+ span.end();
24702
+ }
24703
+ async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, runName) {
24704
+ this.startSpan({
24705
+ runId,
24706
+ parentRunId,
24707
+ name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(llm))), () => ( "LLM")),
24708
+ type: "llm",
24709
+ event: {
24710
+ input: prompts,
24711
+ tags,
24712
+ metadata: {
24713
+ serialized: llm,
24714
+ name: runName,
24715
+ metadata,
24716
+ ...extraParams
24717
+ }
24718
+ }
24719
+ });
24720
+ }
24721
+ async handleLLMError(err, runId, parentRunId, tags) {
24722
+ this.endSpan({ runId, parentRunId, error: err, tags });
24723
+ }
24724
+ async handleLLMEnd(output, runId, parentRunId, tags) {
24725
+ const metrics = getMetricsFromResponse(output);
24726
+ const modelName2 = getModelNameFromResponse(output);
24727
+ const ttft = this.ttftMs.get(runId);
24728
+ if (ttft !== void 0) {
24729
+ metrics.time_to_first_token = ttft;
24730
+ }
24731
+ this.startTimes.delete(runId);
24732
+ this.firstTokenTimes.delete(runId);
24733
+ this.ttftMs.delete(runId);
24734
+ this.endSpan({
24735
+ runId,
24736
+ parentRunId,
24737
+ output,
24738
+ metrics,
24739
+ tags,
24740
+ metadata: {
24741
+ model: modelName2
24742
+ }
24743
+ });
24744
+ }
24745
+ async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, runName) {
24746
+ this.startTimes.set(runId, Date.now());
24747
+ this.firstTokenTimes.delete(runId);
24748
+ this.ttftMs.delete(runId);
24749
+ this.startSpan({
24750
+ runId,
24751
+ parentRunId,
24752
+ name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(llm))), () => ( "Chat Model")),
24753
+ type: "llm",
24754
+ event: {
24755
+ input: messages,
24756
+ tags,
24757
+ metadata: {
24758
+ serialized: llm,
24759
+ name: runName,
24760
+ metadata,
24761
+ ...extraParams
24762
+ }
24763
+ }
24764
+ });
24765
+ }
24766
+ async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
24767
+ if (_optionalChain([tags, 'optionalAccess', _634 => _634.includes, 'call', _635 => _635("langsmith:hidden")])) {
24768
+ this.skippedRuns.add(runId);
24769
+ return;
24770
+ }
24771
+ this.startSpan({
24772
+ runId,
24773
+ parentRunId,
24774
+ name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(chain))), () => ( "Chain")),
24775
+ event: {
24776
+ input: inputs,
24777
+ tags,
24778
+ metadata: {
24779
+ serialized: chain,
24780
+ name: runName,
24781
+ metadata,
24782
+ run_type: runType
24783
+ }
24784
+ }
24785
+ });
24786
+ }
24787
+ async handleChainError(err, runId, parentRunId, tags, kwargs) {
24788
+ this.endSpan({ runId, parentRunId, error: err, tags, metadata: kwargs });
24789
+ }
24790
+ async handleChainEnd(outputs, runId, parentRunId, tags, kwargs) {
24791
+ this.endSpan({
24792
+ runId,
24793
+ parentRunId,
24794
+ tags,
24795
+ output: outputs,
24796
+ metadata: { ...kwargs }
24797
+ });
24798
+ }
24799
+ async handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
24800
+ this.startSpan({
24801
+ runId,
24802
+ parentRunId,
24803
+ name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(tool))), () => ( "Tool")),
24804
+ type: "llm",
24805
+ event: {
24806
+ input: safeJsonParse(input),
24807
+ tags,
24808
+ metadata: {
24809
+ metadata,
24810
+ serialized: tool,
24811
+ input_str: input,
24812
+ input: safeJsonParse(input),
24813
+ name: runName
24814
+ }
24815
+ }
24816
+ });
24817
+ }
24818
+ async handleToolError(err, runId, parentRunId, tags) {
24819
+ this.endSpan({ runId, parentRunId, error: err, tags });
24820
+ }
24821
+ async handleToolEnd(output, runId, parentRunId, tags) {
24822
+ this.endSpan({ runId, parentRunId, output, tags });
24823
+ }
24824
+ async handleAgentAction(action, runId, parentRunId, tags) {
24825
+ this.startSpan({
24826
+ runId,
24827
+ parentRunId,
24828
+ type: "llm",
24829
+ name: typeof action.tool === "string" ? action.tool : "Agent",
24830
+ event: {
24831
+ input: action,
24832
+ tags
24833
+ }
24834
+ });
24835
+ }
24836
+ async handleAgentEnd(action, runId, parentRunId, tags) {
24837
+ this.endSpan({ runId, parentRunId, output: action, tags });
24838
+ }
24839
+ async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
24840
+ this.startSpan({
24841
+ runId,
24842
+ parentRunId,
24843
+ name: _nullishCoalesce(_nullishCoalesce(name, () => ( getSerializedName(retriever))), () => ( "Retriever")),
24844
+ type: "function",
24845
+ event: {
24846
+ input: query,
24847
+ tags,
24848
+ metadata: {
24849
+ serialized: retriever,
24850
+ metadata,
24851
+ name
24852
+ }
24853
+ }
24854
+ });
24855
+ }
24856
+ async handleRetrieverEnd(documents, runId, parentRunId, tags) {
24857
+ this.endSpan({ runId, parentRunId, output: documents, tags });
24858
+ }
24859
+ async handleRetrieverError(err, runId, parentRunId, tags) {
24860
+ this.endSpan({ runId, parentRunId, error: err, tags });
24861
+ }
24862
+ async handleLLMNewToken(_token, _idx, runId, _parentRunId, _tags) {
24863
+ if (!this.firstTokenTimes.has(runId)) {
24864
+ const now2 = Date.now();
24865
+ this.firstTokenTimes.set(runId, now2);
24866
+ const start = this.startTimes.get(runId);
24867
+ if (start !== void 0) {
24868
+ this.ttftMs.set(runId, (now2 - start) / 1e3);
24869
+ }
24870
+ }
24871
+ }
24872
+ }, _class23);
24873
+ function getSerializedName(serialized) {
24874
+ if (typeof serialized.name === "string") {
24875
+ return serialized.name;
24876
+ }
24877
+ const lastIdPart = _optionalChain([serialized, 'access', _636 => _636.id, 'optionalAccess', _637 => _637.at, 'call', _638 => _638(-1)]);
24878
+ return typeof lastIdPart === "string" ? lastIdPart : void 0;
24879
+ }
24880
+ function cleanObject(obj) {
24881
+ return Object.fromEntries(
24882
+ Object.entries(obj).filter(([, value]) => {
24883
+ if (typeof value !== "number") {
24884
+ return false;
24885
+ }
24886
+ return Number.isFinite(value);
24887
+ })
24888
+ );
24889
+ }
24890
+ function walkGenerations(response) {
24891
+ const result = [];
24892
+ const generations = response.generations || [];
24893
+ for (const batch of generations) {
24894
+ if (Array.isArray(batch)) {
24895
+ for (const generation of batch) {
24896
+ if (isRecord(generation)) {
24897
+ result.push(generation);
24898
+ }
24899
+ }
24900
+ } else if (isRecord(batch)) {
24901
+ result.push(batch);
24902
+ }
24903
+ }
24904
+ return result;
24905
+ }
24906
+ function getModelNameFromResponse(response) {
24907
+ for (const generation of walkGenerations(response)) {
24908
+ const message = generation.message;
24909
+ if (!isRecord(message)) {
24910
+ continue;
24911
+ }
24912
+ const responseMetadata = message.response_metadata;
24913
+ if (!isRecord(responseMetadata)) {
24914
+ continue;
24915
+ }
24916
+ const modelName3 = _nullishCoalesce(responseMetadata.model_name, () => ( responseMetadata.model));
24917
+ if (typeof modelName3 === "string") {
24918
+ return modelName3;
24919
+ }
24920
+ }
24921
+ const llmOutput = response.llmOutput || {};
24922
+ const modelName2 = _nullishCoalesce(llmOutput.model_name, () => ( llmOutput.model));
24923
+ return typeof modelName2 === "string" ? modelName2 : void 0;
24924
+ }
24925
+ function getMetricsFromResponse(response) {
24926
+ for (const generation of walkGenerations(response)) {
24927
+ const message = generation.message;
24928
+ if (!isRecord(message)) {
24929
+ continue;
24930
+ }
24931
+ const usageMetadata = message.usage_metadata;
24932
+ if (!isRecord(usageMetadata)) {
24933
+ continue;
24934
+ }
24935
+ const inputTokenDetails = usageMetadata.input_token_details;
24936
+ return cleanObject({
24937
+ total_tokens: usageMetadata.total_tokens,
24938
+ prompt_tokens: usageMetadata.input_tokens,
24939
+ completion_tokens: usageMetadata.output_tokens,
24940
+ prompt_cache_creation_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_creation : void 0,
24941
+ prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0
24942
+ });
24943
+ }
24944
+ const llmOutput = response.llmOutput || {};
24945
+ const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
24946
+ return cleanObject({
24947
+ total_tokens: tokenUsage.totalTokens,
24948
+ prompt_tokens: tokenUsage.promptTokens,
24949
+ completion_tokens: tokenUsage.completionTokens
24950
+ });
24951
+ }
24952
+ function safeJsonParse(input) {
24953
+ try {
24954
+ return JSON.parse(input);
24955
+ } catch (e56) {
24956
+ return input;
24957
+ }
24958
+ }
24959
+ function isRecord(value) {
24960
+ return typeof value === "object" && value !== null && !Array.isArray(value);
24961
+ }
24962
+
24963
+ // src/instrumentation/plugins/langchain-channels.ts
24964
+ var langChainChannels = defineChannels("@langchain/core", {
24965
+ configure: channel({
24966
+ channelName: "CallbackManager.configure",
24967
+ kind: "sync-stream"
24968
+ }),
24969
+ configureSync: channel({
24970
+ channelName: "CallbackManager._configureSync",
24971
+ kind: "sync-stream"
24972
+ })
24973
+ });
24974
+
24975
+ // src/instrumentation/plugins/langchain-plugin.ts
24976
+ var LangChainPlugin = (_class24 = class extends BasePlugin {constructor(...args7) { super(...args7); _class24.prototype.__init84.call(this); }
24977
+ __init84() {this.injectedManagers = /* @__PURE__ */ new WeakSet()}
24978
+ onEnable() {
24979
+ this.subscribeToConfigure(langChainChannels.configure);
24980
+ this.subscribeToConfigure(langChainChannels.configureSync);
24981
+ }
24982
+ onDisable() {
24983
+ for (const unsubscribe of this.unsubscribers) {
24984
+ unsubscribe();
24985
+ }
24986
+ this.unsubscribers = [];
24987
+ this.injectedManagers = /* @__PURE__ */ new WeakSet();
24988
+ }
24989
+ subscribeToConfigure(channel2) {
24990
+ const tracingChannel2 = channel2.tracingChannel();
24991
+ const handlers = {
24992
+ start: (event) => {
24993
+ injectHandlerIntoArguments(event.arguments);
24994
+ },
24995
+ end: (event) => {
24996
+ this.injectHandler(event.result);
24997
+ }
24998
+ };
24999
+ tracingChannel2.subscribe(handlers);
25000
+ this.unsubscribers.push(() => {
25001
+ tracingChannel2.unsubscribe(handlers);
25002
+ });
25003
+ }
25004
+ injectHandler(result) {
25005
+ if (!isCallbackManager(result)) {
25006
+ return;
25007
+ }
25008
+ if (this.injectedManagers.has(result) || hasBraintrustHandler(result)) {
25009
+ return;
25010
+ }
25011
+ try {
25012
+ result.addHandler(new BraintrustLangChainCallbackHandler(), true);
25013
+ this.injectedManagers.add(result);
25014
+ } catch (e57) {
25015
+ }
25016
+ }
25017
+ }, _class24);
25018
+ function isCallbackManager(value) {
25019
+ if (typeof value !== "object" || value === null) {
25020
+ return false;
25021
+ }
25022
+ const maybeManager = value;
25023
+ return typeof maybeManager.addHandler === "function";
25024
+ }
25025
+ function hasBraintrustHandler(manager) {
25026
+ return _nullishCoalesce(_optionalChain([manager, 'access', _639 => _639.handlers, 'optionalAccess', _640 => _640.some, 'call', _641 => _641((handler) => {
25027
+ if (typeof handler !== "object" || handler === null) {
25028
+ return false;
25029
+ }
25030
+ const name = Reflect.get(handler, "name");
25031
+ return name === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
25032
+ })]), () => ( false));
25033
+ }
25034
+ function injectHandlerIntoArguments(args) {
25035
+ if (!isWritableArgumentsObject(args)) {
25036
+ return;
25037
+ }
25038
+ const inheritedHandlers = Reflect.get(args, "0");
25039
+ const handler = new BraintrustLangChainCallbackHandler();
25040
+ if (inheritedHandlers === void 0 || inheritedHandlers === null) {
25041
+ Reflect.set(args, "0", [handler]);
25042
+ return;
25043
+ }
25044
+ if (Array.isArray(inheritedHandlers)) {
25045
+ if (!inheritedHandlers.some(isBraintrustHandler)) {
25046
+ inheritedHandlers.push(handler);
25047
+ }
25048
+ }
25049
+ }
25050
+ function isWritableArgumentsObject(args) {
25051
+ return typeof args === "object" && args !== null;
25052
+ }
25053
+ function isBraintrustHandler(handler) {
25054
+ if (typeof handler !== "object" || handler === null) {
25055
+ return false;
25056
+ }
25057
+ return Reflect.get(handler, "name") === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
25058
+ }
25059
+
25060
+ // src/instrumentation/braintrust-plugin.ts
25061
+ function getIntegrationConfig(integrations, key) {
25062
+ return integrations[key];
25063
+ }
25064
+ var BraintrustPlugin = (_class25 = class extends BasePlugin {
25065
+
25066
+ __init85() {this.openaiPlugin = null}
25067
+ __init86() {this.openAICodexPlugin = null}
25068
+ __init87() {this.anthropicPlugin = null}
25069
+ __init88() {this.aiSDKPlugin = null}
25070
+ __init89() {this.claudeAgentSDKPlugin = null}
25071
+ __init90() {this.cursorSDKPlugin = null}
25072
+ __init91() {this.openAIAgentsPlugin = null}
25073
+ __init92() {this.googleGenAIPlugin = null}
25074
+ __init93() {this.huggingFacePlugin = null}
25075
+ __init94() {this.openRouterPlugin = null}
25076
+ __init95() {this.openRouterAgentPlugin = null}
25077
+ __init96() {this.mistralPlugin = null}
25078
+ __init97() {this.googleADKPlugin = null}
25079
+ __init98() {this.coherePlugin = null}
25080
+ __init99() {this.groqPlugin = null}
25081
+ __init100() {this.genkitPlugin = null}
25082
+ __init101() {this.gitHubCopilotPlugin = null}
25083
+ __init102() {this.fluePlugin = null}
25084
+ __init103() {this.langChainPlugin = null}
25085
+ constructor(config = {}) {
25086
+ super();_class25.prototype.__init85.call(this);_class25.prototype.__init86.call(this);_class25.prototype.__init87.call(this);_class25.prototype.__init88.call(this);_class25.prototype.__init89.call(this);_class25.prototype.__init90.call(this);_class25.prototype.__init91.call(this);_class25.prototype.__init92.call(this);_class25.prototype.__init93.call(this);_class25.prototype.__init94.call(this);_class25.prototype.__init95.call(this);_class25.prototype.__init96.call(this);_class25.prototype.__init97.call(this);_class25.prototype.__init98.call(this);_class25.prototype.__init99.call(this);_class25.prototype.__init100.call(this);_class25.prototype.__init101.call(this);_class25.prototype.__init102.call(this);_class25.prototype.__init103.call(this);;
25087
+ this.config = config;
25088
+ }
25089
+ onEnable() {
25090
+ const integrations = this.config.integrations || {};
25091
+ if (integrations.openai !== false) {
25092
+ this.openaiPlugin = new OpenAIPlugin();
25093
+ this.openaiPlugin.enable();
25094
+ }
25095
+ if (integrations.openaiCodexSDK !== false) {
25096
+ this.openAICodexPlugin = new OpenAICodexPlugin();
25097
+ this.openAICodexPlugin.enable();
25098
+ }
25099
+ if (integrations.anthropic !== false) {
22872
25100
  this.anthropicPlugin = new AnthropicPlugin();
22873
25101
  this.anthropicPlugin.enable();
22874
25102
  }
@@ -22884,6 +25112,10 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
22884
25112
  this.cursorSDKPlugin = new CursorSDKPlugin();
22885
25113
  this.cursorSDKPlugin.enable();
22886
25114
  }
25115
+ if (integrations.openAIAgents !== false) {
25116
+ this.openAIAgentsPlugin = new OpenAIAgentsPlugin();
25117
+ this.openAIAgentsPlugin.enable();
25118
+ }
22887
25119
  if (integrations.googleGenAI !== false && integrations.google !== false) {
22888
25120
  this.googleGenAIPlugin = new GoogleGenAIPlugin();
22889
25121
  this.googleGenAIPlugin.enable();
@@ -22924,6 +25156,14 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
22924
25156
  this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
22925
25157
  this.gitHubCopilotPlugin.enable();
22926
25158
  }
25159
+ if (getIntegrationConfig(integrations, "flue") !== false) {
25160
+ this.fluePlugin = new FluePlugin();
25161
+ this.fluePlugin.enable();
25162
+ }
25163
+ if (integrations.langchain !== false && integrations.langgraph !== false) {
25164
+ this.langChainPlugin = new LangChainPlugin();
25165
+ this.langChainPlugin.enable();
25166
+ }
22927
25167
  }
22928
25168
  onDisable() {
22929
25169
  if (this.openaiPlugin) {
@@ -22950,6 +25190,10 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
22950
25190
  this.cursorSDKPlugin.disable();
22951
25191
  this.cursorSDKPlugin = null;
22952
25192
  }
25193
+ if (this.openAIAgentsPlugin) {
25194
+ this.openAIAgentsPlugin.disable();
25195
+ this.openAIAgentsPlugin = null;
25196
+ }
22953
25197
  if (this.googleGenAIPlugin) {
22954
25198
  this.googleGenAIPlugin.disable();
22955
25199
  this.googleGenAIPlugin = null;
@@ -22990,8 +25234,103 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
22990
25234
  this.gitHubCopilotPlugin.disable();
22991
25235
  this.gitHubCopilotPlugin = null;
22992
25236
  }
25237
+ if (this.fluePlugin) {
25238
+ this.fluePlugin.disable();
25239
+ this.fluePlugin = null;
25240
+ }
25241
+ if (this.langChainPlugin) {
25242
+ this.langChainPlugin.disable();
25243
+ this.langChainPlugin = null;
25244
+ }
22993
25245
  }
22994
- }, _class20);
25246
+ }, _class25);
25247
+
25248
+ // src/instrumentation/config.ts
25249
+ var envIntegrationAliases = {
25250
+ openai: "openai",
25251
+ "openai-codex": "openaiCodexSDK",
25252
+ "openai-codex-sdk": "openaiCodexSDK",
25253
+ openaicodexsdk: "openaiCodexSDK",
25254
+ codex: "openaiCodexSDK",
25255
+ "codex-sdk": "openaiCodexSDK",
25256
+ anthropic: "anthropic",
25257
+ aisdk: "aisdk",
25258
+ "ai-sdk": "aisdk",
25259
+ "vercel-ai": "aisdk",
25260
+ vercel: "vercel",
25261
+ claudeagentsdk: "claudeAgentSDK",
25262
+ "claude-agent-sdk": "claudeAgentSDK",
25263
+ cursor: "cursor",
25264
+ "cursor-sdk": "cursorSDK",
25265
+ cursorsdk: "cursorSDK",
25266
+ flue: "flue",
25267
+ "flue-runtime": "flue",
25268
+ "openai-agents": "openAIAgents",
25269
+ openaiagents: "openAIAgents",
25270
+ "openai-agents-core": "openAIAgents",
25271
+ openaiagentscore: "openAIAgents",
25272
+ google: "google",
25273
+ "google-genai": "googleGenAI",
25274
+ googlegenai: "googleGenAI",
25275
+ huggingface: "huggingface",
25276
+ openrouter: "openrouter",
25277
+ openrouteragent: "openrouterAgent",
25278
+ "openrouter-agent": "openrouterAgent",
25279
+ mistral: "mistral",
25280
+ googleadk: "googleADK",
25281
+ "google-adk": "googleADK",
25282
+ cohere: "cohere",
25283
+ groq: "groq",
25284
+ "groq-sdk": "groq",
25285
+ genkit: "genkit",
25286
+ "firebase-genkit": "genkit",
25287
+ githubcopilot: "gitHubCopilot",
25288
+ "github-copilot": "gitHubCopilot",
25289
+ "copilot-sdk": "gitHubCopilot",
25290
+ langchain: "langchain",
25291
+ "langchain-js": "langchain",
25292
+ "@langchain": "langchain",
25293
+ langgraph: "langgraph"
25294
+ };
25295
+ function getDefaultInstrumentationIntegrations() {
25296
+ return {
25297
+ openai: true,
25298
+ openaiCodexSDK: true,
25299
+ anthropic: true,
25300
+ vercel: true,
25301
+ aisdk: true,
25302
+ google: true,
25303
+ googleGenAI: true,
25304
+ googleADK: true,
25305
+ huggingface: true,
25306
+ claudeAgentSDK: true,
25307
+ cursor: true,
25308
+ cursorSDK: true,
25309
+ flue: true,
25310
+ openAIAgents: true,
25311
+ openrouter: true,
25312
+ openrouterAgent: true,
25313
+ mistral: true,
25314
+ cohere: true,
25315
+ groq: true,
25316
+ genkit: true,
25317
+ gitHubCopilot: true,
25318
+ langchain: true,
25319
+ langgraph: true
25320
+ };
25321
+ }
25322
+ function readDisabledInstrumentationEnvConfig(disabledList) {
25323
+ const integrations = {};
25324
+ if (disabledList) {
25325
+ for (const value of disabledList.split(",")) {
25326
+ const sdk = value.trim().toLowerCase();
25327
+ if (sdk.length > 0) {
25328
+ integrations[_nullishCoalesce(envIntegrationAliases[sdk], () => ( sdk))] = false;
25329
+ }
25330
+ }
25331
+ }
25332
+ return { integrations };
25333
+ }
22995
25334
 
22996
25335
  // src/instrumentation/registry.ts
22997
25336
  var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for("braintrust.registry");
@@ -22999,10 +25338,10 @@ function getSharedState() {
22999
25338
  const state = globalThis[/* @__PURE__ */ Symbol.for("braintrust-state")];
23000
25339
  return state && typeof state === "object" ? state : void 0;
23001
25340
  }
23002
- var PluginRegistry = (_class21 = class {constructor() { _class21.prototype.__init83.call(this);_class21.prototype.__init84.call(this);_class21.prototype.__init85.call(this); }
23003
- __init83() {this.braintrustPlugin = null}
23004
- __init84() {this.config = {}}
23005
- __init85() {this.enabled = false}
25341
+ var PluginRegistry = (_class26 = class {constructor() { _class26.prototype.__init104.call(this);_class26.prototype.__init105.call(this);_class26.prototype.__init106.call(this); }
25342
+ __init104() {this.braintrustPlugin = null}
25343
+ __init105() {this.config = {}}
25344
+ __init106() {this.enabled = false}
23006
25345
  /**
23007
25346
  * Configure which integrations should be enabled.
23008
25347
  * This must be called before any SDK imports to take effect.
@@ -23071,60 +25410,75 @@ var PluginRegistry = (_class21 = class {constructor() { _class21.prototype.__ini
23071
25410
  * Get default configuration (all integrations enabled).
23072
25411
  */
23073
25412
  getDefaultConfig() {
23074
- return {
23075
- openai: true,
23076
- openaiCodexSDK: true,
23077
- anthropic: true,
23078
- vercel: true,
23079
- aisdk: true,
23080
- google: true,
23081
- googleGenAI: true,
23082
- googleADK: true,
23083
- huggingface: true,
23084
- claudeAgentSDK: true,
23085
- cursor: true,
23086
- cursorSDK: true,
23087
- openrouter: true,
23088
- openrouterAgent: true,
23089
- mistral: true,
23090
- cohere: true,
23091
- groq: true,
23092
- genkit: true,
23093
- gitHubCopilot: true
23094
- };
25413
+ return getDefaultInstrumentationIntegrations();
23095
25414
  }
23096
25415
  /**
23097
25416
  * Read configuration from environment variables.
23098
25417
  * Supports: BRAINTRUST_DISABLE_INSTRUMENTATION=openai,anthropic,...
23099
25418
  */
23100
25419
  readEnvConfig() {
23101
- const integrations = {};
23102
- const disabledList = isomorph_default.getEnv("BRAINTRUST_DISABLE_INSTRUMENTATION");
23103
- if (disabledList) {
23104
- const disabled = disabledList.split(",").map((s) => s.trim().toLowerCase()).filter((s) => s.length > 0);
23105
- for (const sdk of disabled) {
23106
- if (sdk === "cursor-sdk") {
23107
- integrations.cursorSDK = false;
23108
- } else if (sdk === "githubcopilot" || sdk === "github-copilot" || sdk === "copilot-sdk") {
23109
- integrations.gitHubCopilot = false;
23110
- } else if (sdk === "openai-codex-sdk") {
23111
- integrations.openaiCodexSDK = false;
23112
- } else {
23113
- integrations[sdk] = false;
23114
- }
23115
- }
23116
- }
23117
- return { integrations };
25420
+ return readDisabledInstrumentationEnvConfig(
25421
+ isomorph_default.getEnv("BRAINTRUST_DISABLE_INSTRUMENTATION")
25422
+ );
23118
25423
  }
23119
- }, _class21);
25424
+ }, _class26);
23120
25425
  var registry = new PluginRegistry();
23121
25426
 
23122
25427
  // src/node/config.ts
25428
+ var BRAINTRUST_ENV_SEARCH_PARENT_LIMIT = 64;
23123
25429
  function configureNode() {
23124
25430
  isomorph_default.buildType = "node";
23125
25431
  isomorph_default.getRepoInfo = getRepoInfo;
23126
25432
  isomorph_default.getPastNAncestors = getPastNAncestors;
23127
- isomorph_default.getEnv = (name) => process.env[name];
25433
+ isomorph_default.getEnv = (name) => {
25434
+ const value = process.env[name];
25435
+ return name === "BRAINTRUST_API_KEY" && !_optionalChain([value, 'optionalAccess', _642 => _642.trim, 'call', _643 => _643()]) ? void 0 : value;
25436
+ };
25437
+ isomorph_default.getBraintrustApiKey = async () => {
25438
+ const value = process.env.BRAINTRUST_API_KEY;
25439
+ if (_optionalChain([value, 'optionalAccess', _644 => _644.trim, 'call', _645 => _645()])) {
25440
+ return value;
25441
+ }
25442
+ const envPaths = [];
25443
+ for (let dir2 = process.cwd(), depth = 0; depth <= BRAINTRUST_ENV_SEARCH_PARENT_LIMIT; dir2 = path.dirname(dir2), depth++) {
25444
+ envPaths.push(path.join(dir2, ".env.braintrust"));
25445
+ if (path.dirname(dir2) === dir2) {
25446
+ break;
25447
+ }
25448
+ }
25449
+ const pending = /* @__PURE__ */ new Map();
25450
+ envPaths.forEach((envPath, index) => {
25451
+ pending.set(
25452
+ index,
25453
+ fs.readFile(envPath, "utf8").then(
25454
+ (contents) => ({ contents, envPath, index }),
25455
+ (error) => ({ error, envPath, index })
25456
+ )
25457
+ );
25458
+ });
25459
+ const results = [];
25460
+ let nearestUnresolvedIndex = 0;
25461
+ while (pending.size > 0) {
25462
+ const result = await Promise.race(pending.values());
25463
+ pending.delete(result.index);
25464
+ results[result.index] = result;
25465
+ while (results[nearestUnresolvedIndex]) {
25466
+ const nearestResult = results[nearestUnresolvedIndex];
25467
+ if ("contents" in nearestResult) {
25468
+ const parsed = dotenv.parse(nearestResult.contents);
25469
+ const apiKey = parsed.BRAINTRUST_API_KEY;
25470
+ return _optionalChain([apiKey, 'optionalAccess', _646 => _646.trim, 'call', _647 => _647()]) ? apiKey : void 0;
25471
+ }
25472
+ const e = nearestResult.error;
25473
+ if (typeof e === "object" && e !== null && "code" in e && e.code === "ENOENT") {
25474
+ nearestUnresolvedIndex++;
25475
+ continue;
25476
+ }
25477
+ return void 0;
25478
+ }
25479
+ }
25480
+ return void 0;
25481
+ };
23128
25482
  isomorph_default.getCallerLocation = getCallerLocation;
23129
25483
  isomorph_default.newAsyncLocalStorage = () => new (0, _nodeasync_hooks.AsyncLocalStorage)();
23130
25484
  isomorph_default.newTracingChannel = (nameOrChannels) => diagnostics_channel.tracingChannel(nameOrChannels);
@@ -24279,8 +26633,12 @@ async function invoke(args) {
24279
26633
 
24280
26634
  // src/trace.ts
24281
26635
  var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
24282
- constructor(objectType, _objectId, rootSpanId, _state, spanTypeFilter) {
24283
- const filterExpr = _SpanFetcher.buildFilter(rootSpanId, spanTypeFilter);
26636
+ constructor(objectType, _objectId, rootSpanId, _state, spanTypeFilter, includeScorers = false) {
26637
+ const filterExpr = _SpanFetcher.buildFilter(
26638
+ rootSpanId,
26639
+ spanTypeFilter,
26640
+ includeScorers
26641
+ );
24284
26642
  super(objectType, void 0, void 0, {
24285
26643
  filter: filterExpr
24286
26644
  });
@@ -24289,16 +26647,17 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
24289
26647
  this._state = _state;
24290
26648
  this.spanTypeFilter = spanTypeFilter;
24291
26649
  }
24292
- static buildFilter(rootSpanId, spanTypeFilter) {
26650
+ static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
24293
26651
  const children = [
24294
26652
  // Base filter: root_span_id = 'value'
24295
26653
  {
24296
26654
  op: "eq",
24297
26655
  left: { op: "ident", name: ["root_span_id"] },
24298
26656
  right: { op: "literal", value: rootSpanId }
24299
- },
24300
- // Exclude span_attributes.purpose = 'score'
24301
- {
26657
+ }
26658
+ ];
26659
+ if (!includeScorers) {
26660
+ children.push({
24302
26661
  op: "or",
24303
26662
  children: [
24304
26663
  {
@@ -24311,8 +26670,8 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
24311
26670
  right: { op: "literal", value: "scorer" }
24312
26671
  }
24313
26672
  ]
24314
- }
24315
- ];
26673
+ });
26674
+ }
24316
26675
  if (spanTypeFilter && spanTypeFilter.length > 0) {
24317
26676
  children.push({
24318
26677
  op: "in",
@@ -24332,41 +26691,55 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
24332
26691
  return this._state;
24333
26692
  }
24334
26693
  };
24335
- var CachedSpanFetcher = (_class22 = class {
24336
- __init86() {this.spanCache = /* @__PURE__ */ new Map()}
24337
- __init87() {this.allFetched = false}
26694
+ var CachedSpanFetcher = (_class27 = class {
26695
+ __init107() {this.spanCache = /* @__PURE__ */ new Map()}
26696
+ __init108() {this.allFetched = false}
24338
26697
 
24339
- constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class22.prototype.__init86.call(this);_class22.prototype.__init87.call(this);
26698
+ constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class27.prototype.__init107.call(this);_class27.prototype.__init108.call(this);
24340
26699
  if (typeof objectTypeOrFetchFn === "function") {
24341
- this.fetchFn = objectTypeOrFetchFn;
26700
+ this.fetchFn = (spanType) => objectTypeOrFetchFn(spanType);
24342
26701
  } else {
24343
26702
  const objectType = objectTypeOrFetchFn;
24344
- this.fetchFn = async (spanType) => {
26703
+ this.fetchFn = async (spanType, includeScorers) => {
24345
26704
  const state = await getState();
24346
26705
  const fetcher = new SpanFetcher(
24347
26706
  objectType,
24348
26707
  objectId,
24349
26708
  rootSpanId,
24350
26709
  state,
24351
- spanType
26710
+ spanType,
26711
+ includeScorers
24352
26712
  );
24353
26713
  const rows = await fetcher.fetchedData();
24354
- return rows.filter((row) => _optionalChain([row, 'access', _559 => _559.span_attributes, 'optionalAccess', _560 => _560.purpose]) !== "scorer").map((row) => ({
26714
+ return rows.map((row) => ({
24355
26715
  input: row.input,
24356
26716
  output: row.output,
26717
+ expected: row.expected,
26718
+ error: row.error,
26719
+ scores: row.scores,
26720
+ metrics: row.metrics,
24357
26721
  metadata: row.metadata,
24358
26722
  span_id: row.span_id,
24359
26723
  span_parents: row.span_parents,
26724
+ is_root: row.is_root,
24360
26725
  span_attributes: row.span_attributes,
24361
26726
  id: row.id,
24362
26727
  _xact_id: row._xact_id,
24363
26728
  _pagination_key: row._pagination_key,
24364
- root_span_id: row.root_span_id
26729
+ root_span_id: row.root_span_id,
26730
+ created: row.created,
26731
+ tags: row.tags
24365
26732
  }));
24366
26733
  };
24367
26734
  }
24368
26735
  }
24369
- async getSpans({ spanType } = {}) {
26736
+ async getSpans({
26737
+ spanType,
26738
+ includeScorers = false
26739
+ } = {}) {
26740
+ if (includeScorers) {
26741
+ return this.fetchFn(spanType, true);
26742
+ }
24370
26743
  if (this.allFetched) {
24371
26744
  return this.getFromCache(spanType);
24372
26745
  }
@@ -24383,9 +26756,9 @@ var CachedSpanFetcher = (_class22 = class {
24383
26756
  return this.getFromCache(spanType);
24384
26757
  }
24385
26758
  async fetchSpans(spanType) {
24386
- const spans = await this.fetchFn(spanType);
26759
+ const spans = await this.fetchFn(spanType, false);
24387
26760
  for (const span of spans) {
24388
- const type = _nullishCoalesce(_optionalChain([span, 'access', _561 => _561.span_attributes, 'optionalAccess', _562 => _562.type]), () => ( ""));
26761
+ const type = _nullishCoalesce(_optionalChain([span, 'access', _648 => _648.span_attributes, 'optionalAccess', _649 => _649.type]), () => ( ""));
24389
26762
  const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
24390
26763
  existing.push(span);
24391
26764
  this.spanCache.set(type, existing);
@@ -24402,24 +26775,24 @@ var CachedSpanFetcher = (_class22 = class {
24402
26775
  }
24403
26776
  return result;
24404
26777
  }
24405
- }, _class22);
24406
- var LocalTrace = (_class23 = class {
26778
+ }, _class27);
26779
+ var LocalTrace = (_class28 = class {
24407
26780
 
24408
26781
 
24409
26782
 
24410
26783
 
24411
26784
 
24412
- __init88() {this.spansFlushed = false}
24413
- __init89() {this.spansFlushPromise = null}
26785
+ __init109() {this.spansFlushed = false}
26786
+ __init110() {this.spansFlushPromise = null}
24414
26787
 
24415
- __init90() {this.threadCache = /* @__PURE__ */ new Map()}
26788
+ __init111() {this.threadCache = /* @__PURE__ */ new Map()}
24416
26789
  constructor({
24417
26790
  objectType,
24418
26791
  objectId,
24419
26792
  rootSpanId,
24420
26793
  ensureSpansFlushed,
24421
26794
  state
24422
- }) {;_class23.prototype.__init88.call(this);_class23.prototype.__init89.call(this);_class23.prototype.__init90.call(this);
26795
+ }) {;_class28.prototype.__init109.call(this);_class28.prototype.__init110.call(this);_class28.prototype.__init111.call(this);
24423
26796
  this.objectType = objectType;
24424
26797
  this.objectId = objectId;
24425
26798
  this.rootSpanId = rootSpanId;
@@ -24461,34 +26834,43 @@ var LocalTrace = (_class23 = class {
24461
26834
  * First checks the local span cache for recently logged spans, then falls
24462
26835
  * back to CachedSpanFetcher which handles BTQL fetching and caching.
24463
26836
  */
24464
- async getSpans({ spanType } = {}) {
26837
+ async getSpans({
26838
+ spanType,
26839
+ includeScorers = false
26840
+ } = {}) {
24465
26841
  const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
24466
26842
  if (cachedSpans && cachedSpans.length > 0) {
24467
- let spans = cachedSpans.filter(
24468
- (span) => _optionalChain([span, 'access', _563 => _563.span_attributes, 'optionalAccess', _564 => _564.purpose]) !== "scorer"
26843
+ let spans = includeScorers ? cachedSpans : cachedSpans.filter(
26844
+ (span) => _optionalChain([span, 'access', _650 => _650.span_attributes, 'optionalAccess', _651 => _651.purpose]) !== "scorer"
24469
26845
  );
24470
26846
  if (spanType && spanType.length > 0) {
24471
26847
  spans = spans.filter(
24472
- (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _565 => _565.span_attributes, 'optionalAccess', _566 => _566.type]), () => ( "")))
26848
+ (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _652 => _652.span_attributes, 'optionalAccess', _653 => _653.type]), () => ( "")))
24473
26849
  );
24474
26850
  }
24475
26851
  return spans.map((span) => ({
24476
26852
  input: span.input,
24477
26853
  output: span.output,
26854
+ expected: span.expected,
26855
+ error: span.error,
26856
+ scores: span.scores,
26857
+ metrics: span.metrics,
24478
26858
  metadata: span.metadata,
24479
26859
  span_id: span.span_id,
24480
26860
  span_parents: span.span_parents,
24481
- span_attributes: span.span_attributes
26861
+ is_root: span.is_root,
26862
+ span_attributes: span.span_attributes,
26863
+ tags: span.tags
24482
26864
  }));
24483
26865
  }
24484
- return this.cachedFetcher.getSpans({ spanType });
26866
+ return this.cachedFetcher.getSpans({ spanType, includeScorers });
24485
26867
  }
24486
26868
  /**
24487
26869
  * Get the thread (preprocessed messages) for this trace.
24488
26870
  * Calls the API with the project_default preprocessor (which falls back to "thread").
24489
26871
  */
24490
26872
  async getThread(options) {
24491
- const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _567 => _567.preprocessor]), () => ( "project_default"));
26873
+ const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _654 => _654.preprocessor]), () => ( "project_default"));
24492
26874
  if (!this.threadCache.has(cacheKey)) {
24493
26875
  const promise = this.fetchThread(options);
24494
26876
  this.threadCache.set(cacheKey, promise);
@@ -24499,7 +26881,7 @@ var LocalTrace = (_class23 = class {
24499
26881
  await this.ensureSpansReady();
24500
26882
  await this.state.login({});
24501
26883
  const result = await invoke({
24502
- globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _568 => _568.preprocessor]), () => ( "project_default")),
26884
+ globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _655 => _655.preprocessor]), () => ( "project_default")),
24503
26885
  functionType: "preprocessor",
24504
26886
  input: {
24505
26887
  trace_ref: {
@@ -24530,7 +26912,7 @@ var LocalTrace = (_class23 = class {
24530
26912
  }
24531
26913
  await this.spansFlushPromise;
24532
26914
  }
24533
- }, _class23);
26915
+ }, _class28);
24534
26916
 
24535
26917
  // src/reporters/progress.ts
24536
26918
  var SimpleProgressReporter = class {
@@ -24675,10 +27057,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
24675
27057
  const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
24676
27058
  const validate = ajv.compile(schema);
24677
27059
  if (!validate(parameters)) {
24678
- const errorMessages = _optionalChain([validate, 'access', _569 => _569.errors, 'optionalAccess', _570 => _570.map, 'call', _571 => _571((err) => {
27060
+ const errorMessages = _optionalChain([validate, 'access', _656 => _656.errors, 'optionalAccess', _657 => _657.map, 'call', _658 => _658((err) => {
24679
27061
  const path2 = err.instancePath || "root";
24680
27062
  return `${path2}: ${err.message}`;
24681
- }), 'access', _572 => _572.join, 'call', _573 => _573(", ")]);
27063
+ }), 'access', _659 => _659.join, 'call', _660 => _660(", ")]);
24682
27064
  throw Error(`Invalid parameters: ${errorMessages}`);
24683
27065
  }
24684
27066
  return rehydrateRemoteParameters(parameters, schema);
@@ -25018,7 +27400,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
25018
27400
  }
25019
27401
  async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
25020
27402
  if (enableCache) {
25021
- _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _574 => _574.spanCache, 'optionalAccess', _575 => _575.start, 'call', _576 => _576()]);
27403
+ _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _661 => _661.spanCache, 'optionalAccess', _662 => _662.start, 'call', _663 => _663()]);
25022
27404
  }
25023
27405
  try {
25024
27406
  if (typeof evaluator.data === "string") {
@@ -25073,7 +27455,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
25073
27455
  const experimentIdPromise = experiment ? (async () => {
25074
27456
  try {
25075
27457
  return await experiment.id;
25076
- } catch (e50) {
27458
+ } catch (e58) {
25077
27459
  return void 0;
25078
27460
  }
25079
27461
  })() : void 0;
@@ -25129,7 +27511,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
25129
27511
  objectType: parentComponents ? spanObjectTypeV3ToTypedString(
25130
27512
  parentComponents.data.object_type
25131
27513
  ) : "experiment",
25132
- objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _577 => _577.data, 'access', async _578 => _578.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
27514
+ objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _664 => _664.data, 'access', async _665 => _665.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
25133
27515
  rootSpanId: rootSpan.rootSpanId,
25134
27516
  ensureSpansFlushed,
25135
27517
  state
@@ -25159,10 +27541,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
25159
27541
  span,
25160
27542
  parameters: _nullishCoalesce(parameters, () => ( {})),
25161
27543
  reportProgress: (event) => {
25162
- _optionalChain([stream, 'optionalCall', _579 => _579({
27544
+ _optionalChain([stream, 'optionalCall', _666 => _666({
25163
27545
  ...event,
25164
27546
  id: rootSpan.id,
25165
- origin: _optionalChain([baseEvent, 'access', _580 => _580.event, 'optionalAccess', _581 => _581.origin]),
27547
+ origin: _optionalChain([baseEvent, 'access', _667 => _667.event, 'optionalAccess', _668 => _668.origin]),
25166
27548
  name: evaluator.evalName,
25167
27549
  object_type: "task"
25168
27550
  })]);
@@ -25350,7 +27732,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
25350
27732
  tags: tags.length ? tags : void 0,
25351
27733
  metadata,
25352
27734
  error,
25353
- origin: _optionalChain([baseEvent, 'access', _582 => _582.event, 'optionalAccess', _583 => _583.origin])
27735
+ origin: _optionalChain([baseEvent, 'access', _669 => _669.event, 'optionalAccess', _670 => _670.origin])
25354
27736
  };
25355
27737
  collectedResults.push({
25356
27738
  ...baseResult,
@@ -25390,7 +27772,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
25390
27772
  break;
25391
27773
  }
25392
27774
  scheduledTrials++;
25393
- _optionalChain([progressReporter, 'access', _584 => _584.setTotal, 'optionalCall', _585 => _585(evaluator.evalName, scheduledTrials)]);
27775
+ _optionalChain([progressReporter, 'access', _671 => _671.setTotal, 'optionalCall', _672 => _672(evaluator.evalName, scheduledTrials)]);
25394
27776
  q.pushAsync({ datum, trialIndex }).catch((e) => {
25395
27777
  if (queueErrors.length < 5) {
25396
27778
  queueErrors.push(e);
@@ -25475,9 +27857,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
25475
27857
  );
25476
27858
  } finally {
25477
27859
  if (enableCache) {
25478
- const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _586 => _586.spanCache]);
25479
- _optionalChain([spanCache, 'optionalAccess', _587 => _587.dispose, 'call', _588 => _588()]);
25480
- _optionalChain([spanCache, 'optionalAccess', _589 => _589.stop, 'call', _590 => _590()]);
27860
+ const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _673 => _673.spanCache]);
27861
+ _optionalChain([spanCache, 'optionalAccess', _674 => _674.dispose, 'call', _675 => _675()]);
27862
+ _optionalChain([spanCache, 'optionalAccess', _676 => _676.stop, 'call', _677 => _677()]);
25481
27863
  }
25482
27864
  }
25483
27865
  }
@@ -25717,7 +28099,7 @@ async function cachedLogin(options) {
25717
28099
  }
25718
28100
  function makeCheckAuthorized(allowedOrgName) {
25719
28101
  return async (req, _res, next) => {
25720
- if (!_optionalChain([req, 'access', _591 => _591.ctx, 'optionalAccess', _592 => _592.token])) {
28102
+ if (!_optionalChain([req, 'access', _678 => _678.ctx, 'optionalAccess', _679 => _679.token])) {
25721
28103
  return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
25722
28104
  }
25723
28105
  try {
@@ -25730,7 +28112,7 @@ function makeCheckAuthorized(allowedOrgName) {
25730
28112
  return next(_httperrors2.default.call(void 0, 403, errorMessage2));
25731
28113
  }
25732
28114
  const state = await cachedLogin({
25733
- apiKey: _optionalChain([req, 'access', _593 => _593.ctx, 'optionalAccess', _594 => _594.token]),
28115
+ apiKey: _optionalChain([req, 'access', _680 => _680.ctx, 'optionalAccess', _681 => _681.token]),
25734
28116
  orgName
25735
28117
  });
25736
28118
  req.ctx.state = state;
@@ -25922,17 +28304,17 @@ var ProjectBuilder = class {
25922
28304
  }
25923
28305
  };
25924
28306
  var projects = new ProjectBuilder();
25925
- var Project2 = (_class24 = class {
28307
+ var Project2 = (_class29 = class {
25926
28308
 
25927
28309
 
25928
28310
 
25929
28311
 
25930
28312
 
25931
28313
 
25932
- __init91() {this._publishableCodeFunctions = []}
25933
- __init92() {this._publishablePrompts = []}
25934
- __init93() {this._publishableParameters = []}
25935
- constructor(args) {;_class24.prototype.__init91.call(this);_class24.prototype.__init92.call(this);_class24.prototype.__init93.call(this);
28314
+ __init112() {this._publishableCodeFunctions = []}
28315
+ __init113() {this._publishablePrompts = []}
28316
+ __init114() {this._publishableParameters = []}
28317
+ constructor(args) {;_class29.prototype.__init112.call(this);_class29.prototype.__init113.call(this);_class29.prototype.__init114.call(this);
25936
28318
  _initializeSpanContext();
25937
28319
  this.name = "name" in args ? args.name : void 0;
25938
28320
  this.id = "id" in args ? args.id : void 0;
@@ -25984,12 +28366,12 @@ var Project2 = (_class24 = class {
25984
28366
  functions: functionDefinitions
25985
28367
  });
25986
28368
  }
25987
- }, _class24);
25988
- var ToolBuilder = (_class25 = class {
25989
- constructor(project) {;_class25.prototype.__init94.call(this);
28369
+ }, _class29);
28370
+ var ToolBuilder = (_class30 = class {
28371
+ constructor(project) {;_class30.prototype.__init115.call(this);
25990
28372
  this.project = project;
25991
28373
  }
25992
- __init94() {this.taskCounter = 0}
28374
+ __init115() {this.taskCounter = 0}
25993
28375
  // This type definition is just a catch all so that the implementation can be
25994
28376
  // less specific than the two more specific declarations above.
25995
28377
  create(opts) {
@@ -26014,12 +28396,12 @@ var ToolBuilder = (_class25 = class {
26014
28396
  this.project.addCodeFunction(tool);
26015
28397
  return tool;
26016
28398
  }
26017
- }, _class25);
26018
- var ScorerBuilder = (_class26 = class {
26019
- constructor(project) {;_class26.prototype.__init95.call(this);
28399
+ }, _class30);
28400
+ var ScorerBuilder = (_class31 = class {
28401
+ constructor(project) {;_class31.prototype.__init116.call(this);
26020
28402
  this.project = project;
26021
28403
  }
26022
- __init95() {this.taskCounter = 0}
28404
+ __init116() {this.taskCounter = 0}
26023
28405
  create(opts) {
26024
28406
  this.taskCounter++;
26025
28407
  let resolvedName = opts.name;
@@ -26073,7 +28455,7 @@ var ScorerBuilder = (_class26 = class {
26073
28455
  this.project.addPrompt(codePrompt);
26074
28456
  }
26075
28457
  }
26076
- }, _class26);
28458
+ }, _class31);
26077
28459
  var CodeFunction = class {
26078
28460
  constructor(project, opts) {
26079
28461
  this.project = project;
@@ -26375,9 +28757,9 @@ function serializeRemoteEvalParametersContainer(parameters) {
26375
28757
  source: null
26376
28758
  };
26377
28759
  }
26378
- var ProjectNameIdMap = (_class27 = class {constructor() { _class27.prototype.__init96.call(this);_class27.prototype.__init97.call(this); }
26379
- __init96() {this.nameToId = {}}
26380
- __init97() {this.idToName = {}}
28760
+ var ProjectNameIdMap = (_class32 = class {constructor() { _class32.prototype.__init117.call(this);_class32.prototype.__init118.call(this); }
28761
+ __init117() {this.nameToId = {}}
28762
+ __init118() {this.idToName = {}}
26381
28763
  async getId(projectName) {
26382
28764
  if (!(projectName in this.nameToId)) {
26383
28765
  const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
@@ -26410,7 +28792,7 @@ var ProjectNameIdMap = (_class27 = class {constructor() { _class27.prototype.__i
26410
28792
  }
26411
28793
  return this.getId(project.name);
26412
28794
  }
26413
- }, _class27);
28795
+ }, _class32);
26414
28796
 
26415
28797
  // dev/server.ts
26416
28798
  function runDevServer(evaluators, opts) {
@@ -26485,7 +28867,7 @@ function runDevServer(evaluators, opts) {
26485
28867
  scores,
26486
28868
  stream
26487
28869
  } = evalBodySchema.parse(req.body);
26488
- if (!_optionalChain([req, 'access', _595 => _595.ctx, 'optionalAccess', _596 => _596.state])) {
28870
+ if (!_optionalChain([req, 'access', _682 => _682.ctx, 'optionalAccess', _683 => _683.state])) {
26489
28871
  res.status(500).json({ error: "Braintrust state not initialized in request" });
26490
28872
  return;
26491
28873
  }
@@ -26536,12 +28918,12 @@ function runDevServer(evaluators, opts) {
26536
28918
  ...evaluator,
26537
28919
  data: evalData.data,
26538
28920
  scores: (_nullishCoalesce(evaluator.scores, () => ( []))).concat(
26539
- _nullishCoalesce(_optionalChain([scores, 'optionalAccess', _597 => _597.map, 'call', _598 => _598(
28921
+ _nullishCoalesce(_optionalChain([scores, 'optionalAccess', _684 => _684.map, 'call', _685 => _685(
26540
28922
  (score) => makeScorer(
26541
28923
  state,
26542
28924
  score.name,
26543
28925
  score.function_id,
26544
- _optionalChain([req, 'access', _599 => _599.ctx, 'optionalAccess', _600 => _600.projectId])
28926
+ _optionalChain([req, 'access', _686 => _686.ctx, 'optionalAccess', _687 => _687.projectId])
26545
28927
  )
26546
28928
  )]), () => ( []))
26547
28929
  ),