braintrust 3.21.0 → 3.23.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 (54) hide show
  1. package/README.md +47 -0
  2. package/dev/dist/index.d.mts +1030 -1
  3. package/dev/dist/index.d.ts +1030 -1
  4. package/dev/dist/index.js +1429 -663
  5. package/dev/dist/index.mjs +811 -45
  6. package/dist/apply-auto-instrumentation.js +222 -186
  7. package/dist/apply-auto-instrumentation.mjs +37 -1
  8. package/dist/auto-instrumentations/bundler/esbuild.cjs +53 -1
  9. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  10. package/dist/auto-instrumentations/bundler/next.cjs +53 -1
  11. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  12. package/dist/auto-instrumentations/bundler/rollup.cjs +53 -1
  13. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  14. package/dist/auto-instrumentations/bundler/vite.cjs +53 -1
  15. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  16. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +53 -1
  17. package/dist/auto-instrumentations/bundler/webpack.cjs +53 -1
  18. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  19. package/dist/auto-instrumentations/{chunk-TKRPRPGD.mjs → chunk-EXY7QCJD.mjs} +5 -2
  20. package/dist/auto-instrumentations/{chunk-T6J4C7LX.mjs → chunk-KIMLYPRW.mjs} +1 -1
  21. package/dist/auto-instrumentations/{chunk-BRQX23KL.mjs → chunk-YXLNSAMJ.mjs} +51 -0
  22. package/dist/auto-instrumentations/hook.mjs +224 -31
  23. package/dist/auto-instrumentations/index.cjs +52 -0
  24. package/dist/auto-instrumentations/index.d.mts +3 -1
  25. package/dist/auto-instrumentations/index.d.ts +3 -1
  26. package/dist/auto-instrumentations/index.mjs +3 -1
  27. package/dist/browser.d.mts +3273 -93
  28. package/dist/browser.d.ts +3273 -93
  29. package/dist/browser.js +2495 -63
  30. package/dist/browser.mjs +2495 -63
  31. package/dist/{chunk-KMGUTPB7.mjs → chunk-36IPYKMG.mjs} +20 -1
  32. package/dist/{chunk-BFGIH2ZJ.js → chunk-CDIKAHDZ.js} +21 -2
  33. package/dist/{chunk-FY7DAKA5.js → chunk-FZWPFCVE.js} +1590 -832
  34. package/dist/{chunk-O2P765XK.mjs → chunk-IXL4PMY4.mjs} +802 -44
  35. package/dist/cli.js +816 -47
  36. package/dist/edge-light.d.mts +1 -1
  37. package/dist/edge-light.d.ts +1 -1
  38. package/dist/edge-light.js +2495 -63
  39. package/dist/edge-light.mjs +2495 -63
  40. package/dist/index.d.mts +3273 -93
  41. package/dist/index.d.ts +3273 -93
  42. package/dist/index.js +2078 -416
  43. package/dist/index.mjs +1664 -2
  44. package/dist/instrumentation/index.d.mts +912 -2
  45. package/dist/instrumentation/index.d.ts +912 -2
  46. package/dist/instrumentation/index.js +2202 -42
  47. package/dist/instrumentation/index.mjs +2200 -42
  48. package/dist/vitest-evals-reporter.js +16 -16
  49. package/dist/vitest-evals-reporter.mjs +2 -2
  50. package/dist/workerd.d.mts +1 -1
  51. package/dist/workerd.d.ts +1 -1
  52. package/dist/workerd.js +2495 -63
  53. package/dist/workerd.mjs +2495 -63
  54. package/package.json +5 -2
package/dist/browser.mjs CHANGED
@@ -2194,19 +2194,30 @@ var CallEvent = z6.union([
2194
2194
  var ChatCompletionContentPartTextWithTitle = z6.object({
2195
2195
  text: z6.string().default(""),
2196
2196
  type: z6.literal("text"),
2197
- cache_control: z6.object({ type: z6.literal("ephemeral") }).optional()
2197
+ cache_control: z6.object({
2198
+ type: z6.literal("ephemeral"),
2199
+ ttl: z6.enum(["5m", "1h"]).optional()
2200
+ }).optional()
2198
2201
  });
2199
2202
  var ChatCompletionContentPartImageWithTitle = z6.object({
2200
2203
  image_url: z6.object({
2201
2204
  url: z6.string(),
2202
2205
  detail: z6.union([z6.literal("auto"), z6.literal("low"), z6.literal("high")]).optional()
2203
2206
  }),
2204
- type: z6.literal("image_url")
2207
+ type: z6.literal("image_url"),
2208
+ cache_control: z6.object({
2209
+ type: z6.literal("ephemeral"),
2210
+ ttl: z6.enum(["5m", "1h"]).optional()
2211
+ }).optional()
2205
2212
  });
2206
2213
  var ChatCompletionContentPartFileFile = z6.object({ file_data: z6.string(), filename: z6.string(), file_id: z6.string() }).partial();
2207
2214
  var ChatCompletionContentPartFileWithTitle = z6.object({
2208
2215
  file: ChatCompletionContentPartFileFile,
2209
- type: z6.literal("file")
2216
+ type: z6.literal("file"),
2217
+ cache_control: z6.object({
2218
+ type: z6.literal("ephemeral"),
2219
+ ttl: z6.enum(["5m", "1h"]).optional()
2220
+ }).optional()
2210
2221
  });
2211
2222
  var ChatCompletionContentPart = z6.union([
2212
2223
  ChatCompletionContentPartTextWithTitle,
@@ -2216,7 +2227,10 @@ var ChatCompletionContentPart = z6.union([
2216
2227
  var ChatCompletionContentPartText = z6.object({
2217
2228
  text: z6.string().default(""),
2218
2229
  type: z6.literal("text"),
2219
- cache_control: z6.object({ type: z6.literal("ephemeral") }).optional()
2230
+ cache_control: z6.object({
2231
+ type: z6.literal("ephemeral"),
2232
+ ttl: z6.enum(["5m", "1h"]).optional()
2233
+ }).optional()
2220
2234
  });
2221
2235
  var ChatCompletionMessageToolCall = z6.object({
2222
2236
  id: z6.string(),
@@ -4253,7 +4267,7 @@ var DiskCache = class {
4253
4267
  }
4254
4268
  };
4255
4269
 
4256
- // src/prompt-cache/lru-cache.ts
4270
+ // src/lru-cache.ts
4257
4271
  var LRUCache = class {
4258
4272
  cache;
4259
4273
  maxSize;
@@ -4269,13 +4283,23 @@ var LRUCache = class {
4269
4283
  * @returns The cached value if found, undefined otherwise.
4270
4284
  */
4271
4285
  get(key) {
4272
- const value = this.cache.get(key);
4273
- if (value === void 0) {
4286
+ const entry = this.cache.get(key);
4287
+ if (entry === void 0) {
4274
4288
  return void 0;
4275
4289
  }
4276
4290
  this.cache.delete(key);
4277
- this.cache.set(key, value);
4278
- return value;
4291
+ this.cache.set(key, entry);
4292
+ return entry.value;
4293
+ }
4294
+ /**
4295
+ * Checks whether a key exists and marks it as most recently used.
4296
+ */
4297
+ has(key) {
4298
+ if (!this.cache.has(key)) {
4299
+ return false;
4300
+ }
4301
+ this.get(key);
4302
+ return true;
4279
4303
  }
4280
4304
  /**
4281
4305
  * Stores a value in the cache.
@@ -4292,7 +4316,38 @@ var LRUCache = class {
4292
4316
  const first = this.cache.keys().next().value;
4293
4317
  this.cache.delete(first);
4294
4318
  }
4295
- this.cache.set(key, value);
4319
+ this.cache.set(key, { value });
4320
+ }
4321
+ /**
4322
+ * Removes an item from the cache.
4323
+ */
4324
+ delete(key) {
4325
+ return this.cache.delete(key);
4326
+ }
4327
+ /**
4328
+ * Iterates over cache entries from least to most recently used.
4329
+ */
4330
+ *entries() {
4331
+ for (const [key, entry] of this.cache) {
4332
+ yield [key, entry.value];
4333
+ }
4334
+ }
4335
+ /**
4336
+ * Iterates over cache keys from least to most recently used.
4337
+ */
4338
+ keys() {
4339
+ return this.cache.keys();
4340
+ }
4341
+ /**
4342
+ * Iterates over cache values from least to most recently used.
4343
+ */
4344
+ *values() {
4345
+ for (const entry of this.cache.values()) {
4346
+ yield entry.value;
4347
+ }
4348
+ }
4349
+ [Symbol.iterator]() {
4350
+ return this.entries();
4296
4351
  }
4297
4352
  /**
4298
4353
  * Removes all items from the cache.
@@ -4850,6 +4905,9 @@ function applyMaskingToField(maskingFunction, data, fieldName) {
4850
4905
  return `ERROR: Failed to mask field '${fieldName}' - ${errorType}`;
4851
4906
  }
4852
4907
  }
4908
+ var INITIAL_SPAN_WRITE_AS_MERGE = /* @__PURE__ */ Symbol(
4909
+ "braintrust.initial-span-write-as-merge"
4910
+ );
4853
4911
  var BRAINTRUST_CURRENT_SPAN_STORE = /* @__PURE__ */ Symbol.for(
4854
4912
  "braintrust.currentSpanStore"
4855
4913
  );
@@ -5451,25 +5509,46 @@ var HTTPConnection = class _HTTPConnection {
5451
5509
  })
5452
5510
  );
5453
5511
  }
5454
- async post(path, params, config) {
5512
+ async post(path, params, config, retries = 0) {
5455
5513
  const { headers, ...rest } = config || {};
5456
5514
  const this_fetch = this.fetch;
5457
5515
  const this_base_url = this.base_url;
5458
5516
  const this_headers = this.headers;
5459
- return await checkResponse(
5460
- await this_fetch(_urljoin(this_base_url, path), {
5461
- method: "POST",
5462
- headers: {
5463
- Accept: "application/json",
5464
- "Content-Type": "application/json",
5465
- ...this_headers,
5466
- ...headers
5467
- },
5468
- body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
5469
- keepalive: true,
5470
- ...rest
5471
- })
5472
- );
5517
+ const tries = retries + 1;
5518
+ for (let i = 0; i < tries; i++) {
5519
+ try {
5520
+ return await checkResponse(
5521
+ await this_fetch(_urljoin(this_base_url, path), {
5522
+ method: "POST",
5523
+ headers: {
5524
+ Accept: "application/json",
5525
+ "Content-Type": "application/json",
5526
+ ...this_headers,
5527
+ ...headers
5528
+ },
5529
+ body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
5530
+ keepalive: true,
5531
+ ...rest
5532
+ })
5533
+ );
5534
+ } catch (error) {
5535
+ if (config?.signal?.aborted) {
5536
+ throw getAbortReason(config.signal);
5537
+ }
5538
+ if (i === tries - 1 || !isRetryableHTTPError(error)) {
5539
+ throw error;
5540
+ }
5541
+ debugLogger.debug(
5542
+ `Retrying API request ${path} after ${formatHTTPError(error)}`
5543
+ );
5544
+ const sleepTimeMs = HTTP_RETRY_BASE_SLEEP_TIME_S * 1e3 * 2 ** i + Math.random() * HTTP_RETRY_JITTER_MS;
5545
+ debugLogger.info(
5546
+ `Sleeping for ${sleepTimeMs}ms before retrying API request`
5547
+ );
5548
+ await waitForRetry(sleepTimeMs, config?.signal);
5549
+ }
5550
+ }
5551
+ throw new Error("Unexpected retry state");
5473
5552
  }
5474
5553
  async get_json(object_type, args = void 0, retries = 0) {
5475
5554
  const tries = retries + 1;
@@ -6566,6 +6645,45 @@ var TestBackgroundLogger = class {
6566
6645
  };
6567
6646
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
6568
6647
  var HTTP_RETRY_BASE_SLEEP_TIME_S = 1;
6648
+ var HTTP_RETRY_JITTER_MS = 200;
6649
+ var BTQL_HTTP_RETRIES = 3;
6650
+ var RETRYABLE_HTTP_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
6651
+ function isRetryableHTTPError(error) {
6652
+ return !(error instanceof FailedHTTPResponse) || RETRYABLE_HTTP_STATUS_CODES.has(error.status);
6653
+ }
6654
+ function formatHTTPError(error) {
6655
+ if (error instanceof FailedHTTPResponse) {
6656
+ return `${error.status} ${error.text}`;
6657
+ }
6658
+ return error instanceof Error ? error.message : String(error);
6659
+ }
6660
+ function getAbortReason(signal) {
6661
+ return signal.reason ?? new Error("Request aborted");
6662
+ }
6663
+ async function waitForRetry(delayMs, signal) {
6664
+ if (!signal) {
6665
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
6666
+ return;
6667
+ }
6668
+ if (signal.aborted) {
6669
+ throw getAbortReason(signal);
6670
+ }
6671
+ await new Promise((resolve, reject2) => {
6672
+ const onAbort = () => {
6673
+ clearTimeout(timeout);
6674
+ signal.removeEventListener("abort", onAbort);
6675
+ reject2(getAbortReason(signal));
6676
+ };
6677
+ const timeout = setTimeout(() => {
6678
+ signal.removeEventListener("abort", onAbort);
6679
+ resolve();
6680
+ }, delayMs);
6681
+ signal.addEventListener("abort", onAbort, { once: true });
6682
+ if (signal.aborted) {
6683
+ onAbort();
6684
+ }
6685
+ });
6686
+ }
6569
6687
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6570
6688
  apiConn;
6571
6689
  queue;
@@ -8304,15 +8422,15 @@ function parentSpanIdsUsable(spanId, rootSpanId) {
8304
8422
  return Boolean(spanId) && Boolean(rootSpanId);
8305
8423
  }
8306
8424
  function logError(span, error) {
8307
- let errorMessage = "<error>";
8425
+ let errorMessage2 = "<error>";
8308
8426
  let stackTrace = "";
8309
8427
  if (error instanceof Error) {
8310
- errorMessage = error.message;
8428
+ errorMessage2 = error.message;
8311
8429
  stackTrace = error.stack || "";
8312
8430
  } else {
8313
- errorMessage = String(error);
8431
+ errorMessage2 = String(error);
8314
8432
  }
8315
- span.log({ error: `${errorMessage}
8433
+ span.log({ error: `${errorMessage2}
8316
8434
 
8317
8435
  ${stackTrace}` });
8318
8436
  }
@@ -8500,6 +8618,12 @@ var traceable = wrapTraced;
8500
8618
  function startSpan(args) {
8501
8619
  return startSpanAndIsLogger(args).span;
8502
8620
  }
8621
+ function _internalStartSpanWithInitialMerge(args) {
8622
+ return startSpanAndIsLogger({
8623
+ ...args,
8624
+ [INITIAL_SPAN_WRITE_AS_MERGE]: true
8625
+ }).span;
8626
+ }
8503
8627
  async function flush(options) {
8504
8628
  const state = options?.state ?? _globalState;
8505
8629
  return await state.bgLogger().flush();
@@ -8893,7 +9017,8 @@ var ObjectFetcher = class {
8893
9017
  version: this.pinnedVersion
8894
9018
  } : {}
8895
9019
  },
8896
- { headers: { "Accept-Encoding": "gzip" } }
9020
+ { headers: { "Accept-Encoding": "gzip" } },
9021
+ BTQL_HTTP_RETRIES
8897
9022
  );
8898
9023
  const respJson = await resp.json();
8899
9024
  const mutate = this.mutateRecord;
@@ -9410,7 +9535,7 @@ var SpanImpl = class _SpanImpl {
9410
9535
  this._spanId = resolvedIds.spanId;
9411
9536
  this._rootSpanId = resolvedIds.rootSpanId;
9412
9537
  this._spanParents = resolvedIds.spanParents;
9413
- this.isMerge = false;
9538
+ this.isMerge = args[INITIAL_SPAN_WRITE_AS_MERGE] === true;
9414
9539
  this.logInternal({ event, internalData });
9415
9540
  this.isMerge = true;
9416
9541
  }
@@ -10617,7 +10742,8 @@ async function getPromptVersions(projectId, promptId) {
10617
10742
  use_columnstore: false,
10618
10743
  brainstore_realtime: true
10619
10744
  },
10620
- { headers: { "Accept-Encoding": "gzip" } }
10745
+ { headers: { "Accept-Encoding": "gzip" } },
10746
+ BTQL_HTTP_RETRIES
10621
10747
  );
10622
10748
  if (!response.ok) {
10623
10749
  throw new Error(
@@ -29078,6 +29204,446 @@ function isBraintrustHandler(handler) {
29078
29204
  return Reflect.get(handler, "name") === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
29079
29205
  }
29080
29206
 
29207
+ // src/instrumentation/plugins/langsmith-channels.ts
29208
+ var langSmithChannels = defineChannels("langsmith", {
29209
+ createRun: channel({
29210
+ channelName: "Client.createRun",
29211
+ kind: "async"
29212
+ }),
29213
+ updateRun: channel({
29214
+ channelName: "Client.updateRun",
29215
+ kind: "async"
29216
+ }),
29217
+ batchIngestRuns: channel({
29218
+ channelName: "Client.batchIngestRuns",
29219
+ kind: "async"
29220
+ })
29221
+ });
29222
+
29223
+ // src/instrumentation/plugins/langsmith-plugin.ts
29224
+ var MAX_COMPLETED_RUNS = 1e4;
29225
+ var BLOCKED_METADATA_KEYS = /* @__PURE__ */ new Set([
29226
+ "__proto__",
29227
+ "constructor",
29228
+ "prototype",
29229
+ "usage_metadata"
29230
+ ]);
29231
+ var BLOCKED_METADATA_PREFIXES = ["__pregel_", "langgraph_", "lc_"];
29232
+ var LLM_SETTING_KEYS = [
29233
+ "temperature",
29234
+ "top_p",
29235
+ "max_tokens",
29236
+ "frequency_penalty",
29237
+ "presence_penalty",
29238
+ "stop",
29239
+ "response_format"
29240
+ ];
29241
+ var LangSmithPlugin = class extends BasePlugin {
29242
+ activeRuns = /* @__PURE__ */ new Map();
29243
+ completedRuns = new LRUCache({
29244
+ max: MAX_COMPLETED_RUNS
29245
+ });
29246
+ skipLangChainRuns;
29247
+ constructor(options = {}) {
29248
+ super();
29249
+ this.skipLangChainRuns = options.skipLangChainRuns ?? true;
29250
+ }
29251
+ onEnable() {
29252
+ const createChannel = langSmithChannels.createRun.tracingChannel();
29253
+ const createHandlers = {
29254
+ start: (event) => {
29255
+ this.containLifecycleFailure("createRun", () => {
29256
+ this.processCreate(event.arguments[0]);
29257
+ });
29258
+ }
29259
+ };
29260
+ createChannel.subscribe(createHandlers);
29261
+ this.unsubscribers.push(() => createChannel.unsubscribe(createHandlers));
29262
+ const updateChannel = langSmithChannels.updateRun.tracingChannel();
29263
+ const updateHandlers = {
29264
+ start: (event) => {
29265
+ this.containLifecycleFailure("updateRun", () => {
29266
+ this.processUpdate(event.arguments[0], event.arguments[1]);
29267
+ });
29268
+ }
29269
+ };
29270
+ updateChannel.subscribe(updateHandlers);
29271
+ this.unsubscribers.push(() => updateChannel.unsubscribe(updateHandlers));
29272
+ const batchChannel = langSmithChannels.batchIngestRuns.tracingChannel();
29273
+ const batchHandlers = {
29274
+ start: (event) => {
29275
+ this.containLifecycleFailure("batchIngestRuns", () => {
29276
+ this.processBatch(event.arguments[0]);
29277
+ });
29278
+ }
29279
+ };
29280
+ batchChannel.subscribe(batchHandlers);
29281
+ this.unsubscribers.push(() => batchChannel.unsubscribe(batchHandlers));
29282
+ }
29283
+ onDisable() {
29284
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
29285
+ for (const { span } of this.activeRuns.values()) {
29286
+ span.end();
29287
+ }
29288
+ this.activeRuns.clear();
29289
+ this.completedRuns.clear();
29290
+ }
29291
+ processBatch(batch) {
29292
+ if (!isRecord2(batch)) {
29293
+ return;
29294
+ }
29295
+ const creates = ownValue(batch, "runCreates");
29296
+ if (Array.isArray(creates)) {
29297
+ const parentFirst = [...creates].sort((left, right) => {
29298
+ const leftId = stringValue2(ownValue(left, "id"));
29299
+ const rightId = stringValue2(ownValue(right, "id"));
29300
+ const leftParent = stringValue2(ownValue(left, "parent_run_id"));
29301
+ const rightParent = stringValue2(ownValue(right, "parent_run_id"));
29302
+ if (leftParent && leftParent === rightId) {
29303
+ return 1;
29304
+ }
29305
+ if (rightParent && rightParent === leftId) {
29306
+ return -1;
29307
+ }
29308
+ return dottedOrderDepth(left) - dottedOrderDepth(right);
29309
+ });
29310
+ for (const run of parentFirst) {
29311
+ this.containLifecycleFailure("batchIngestRuns create", () => {
29312
+ this.processCreate(run);
29313
+ });
29314
+ }
29315
+ }
29316
+ const updates = ownValue(batch, "runUpdates");
29317
+ if (Array.isArray(updates)) {
29318
+ for (const run of updates) {
29319
+ this.containLifecycleFailure("batchIngestRuns update", () => {
29320
+ this.processUpdate(stringValue2(ownValue(run, "id")) ?? "", run);
29321
+ });
29322
+ }
29323
+ }
29324
+ }
29325
+ processCreate(run) {
29326
+ const id = stringValue2(ownValue(run, "id"));
29327
+ if (!id || this.completedRuns.get(id)) {
29328
+ return;
29329
+ }
29330
+ if (this.shouldSkipLangChainRun(run)) {
29331
+ this.completedRuns.set(id, true);
29332
+ return;
29333
+ }
29334
+ const active = this.activeRuns.get(id);
29335
+ if (active) {
29336
+ const previous = active.run;
29337
+ active.run = mergeRuns(previous, run);
29338
+ this.logRun(
29339
+ active.span,
29340
+ active.run,
29341
+ previous,
29342
+ timestampSeconds(ownValue(active.run, "end_time")) !== void 0 || errorMessage(ownValue(active.run, "error")) !== void 0
29343
+ );
29344
+ this.endIfComplete(id, active, active.run);
29345
+ return;
29346
+ }
29347
+ const span = this.startRunSpan(id, run);
29348
+ const activeRun = { run, span };
29349
+ this.activeRuns.set(id, activeRun);
29350
+ this.logRun(
29351
+ span,
29352
+ run,
29353
+ void 0,
29354
+ timestampSeconds(ownValue(run, "end_time")) !== void 0 || errorMessage(ownValue(run, "error")) !== void 0
29355
+ );
29356
+ this.endIfComplete(id, activeRun, run);
29357
+ }
29358
+ processUpdate(explicitId, run) {
29359
+ const id = stringValue2(explicitId) ?? stringValue2(ownValue(run, "id"));
29360
+ if (!id || this.completedRuns.get(id)) {
29361
+ return;
29362
+ }
29363
+ if (this.shouldSkipLangChainRun(run)) {
29364
+ const active2 = this.activeRuns.get(id);
29365
+ active2?.span.end();
29366
+ this.activeRuns.delete(id);
29367
+ this.completedRuns.set(id, true);
29368
+ return;
29369
+ }
29370
+ let active = this.activeRuns.get(id);
29371
+ if (!active) {
29372
+ const span = this.startRunSpan(id, run);
29373
+ active = { run, span };
29374
+ this.activeRuns.set(id, active);
29375
+ }
29376
+ const previous = active.run;
29377
+ active.run = mergeRuns(previous, run);
29378
+ this.logRun(active.span, active.run, previous, true);
29379
+ this.endIfComplete(id, active, active.run);
29380
+ }
29381
+ startRunSpan(id, run) {
29382
+ const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
29383
+ const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
29384
+ const startTime = timestampSeconds(ownValue(run, "start_time"));
29385
+ return startSpan({
29386
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
29387
+ spanId: id,
29388
+ parentSpanIds: {
29389
+ parentSpanIds: parentId ? [parentId] : [],
29390
+ rootSpanId: traceId
29391
+ },
29392
+ spanAttributes: {
29393
+ type: mapRunType(ownValue(run, "run_type"))
29394
+ },
29395
+ ...startTime === void 0 ? {} : { startTime },
29396
+ event: { id }
29397
+ });
29398
+ }
29399
+ logRun(span, run, previous, includeOutput) {
29400
+ const inputs = preferOwnValue(run, previous, "inputs");
29401
+ const outputs = preferOwnValue(run, previous, "outputs");
29402
+ const error = errorMessage(preferOwnValue(run, previous, "error"));
29403
+ const metadata = extractMetadata(run, previous);
29404
+ const tags = extractTags(preferOwnValue(run, previous, "tags"));
29405
+ const metrics = extractMetrics(run, previous);
29406
+ span.log({
29407
+ ...inputs === void 0 ? {} : { input: sanitizeLoggedValue(inputs) },
29408
+ ...includeOutput && outputs !== void 0 ? { output: sanitizeLoggedValue(outputs) } : {},
29409
+ ...error === void 0 ? {} : { error },
29410
+ ...metadata === void 0 ? {} : { metadata },
29411
+ ...tags === void 0 ? {} : { tags },
29412
+ ...Object.keys(metrics).length === 0 ? {} : { metrics }
29413
+ });
29414
+ }
29415
+ endIfComplete(id, active, run) {
29416
+ const endTime = timestampSeconds(ownValue(run, "end_time"));
29417
+ if (endTime === void 0 && errorMessage(ownValue(run, "error")) === void 0) {
29418
+ return;
29419
+ }
29420
+ active.span.end(endTime === void 0 ? void 0 : { endTime });
29421
+ this.activeRuns.delete(id);
29422
+ this.completedRuns.set(id, true);
29423
+ }
29424
+ shouldSkipLangChainRun(run) {
29425
+ if (!this.skipLangChainRuns) {
29426
+ return false;
29427
+ }
29428
+ const serialized = ownValue(run, "serialized");
29429
+ return isRecord2(serialized) && ownValue(serialized, "lc") === 1;
29430
+ }
29431
+ containLifecycleFailure(operation, fn) {
29432
+ try {
29433
+ fn();
29434
+ } catch (error) {
29435
+ debugLogger.error(
29436
+ `Failed to process LangSmith ${operation} instrumentation:`,
29437
+ error
29438
+ );
29439
+ }
29440
+ }
29441
+ };
29442
+ function ownValue(value, key) {
29443
+ if (!isRecord2(value)) {
29444
+ return void 0;
29445
+ }
29446
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
29447
+ return descriptor && "value" in descriptor ? descriptor.value : void 0;
29448
+ }
29449
+ function preferOwnValue(current, previous, key) {
29450
+ const currentDescriptor = isRecord2(current) ? Object.getOwnPropertyDescriptor(current, key) : void 0;
29451
+ if (currentDescriptor && "value" in currentDescriptor) {
29452
+ return currentDescriptor.value;
29453
+ }
29454
+ return ownValue(previous, key);
29455
+ }
29456
+ function mergeRuns(previous, current) {
29457
+ const entries = /* @__PURE__ */ new Map();
29458
+ for (const value of [previous, current]) {
29459
+ if (!isRecord2(value)) {
29460
+ continue;
29461
+ }
29462
+ for (const [key, descriptor] of Object.entries(
29463
+ Object.getOwnPropertyDescriptors(value)
29464
+ )) {
29465
+ if (descriptor.enumerable && "value" in descriptor) {
29466
+ entries.set(key, descriptor.value);
29467
+ }
29468
+ }
29469
+ }
29470
+ return Object.fromEntries(entries);
29471
+ }
29472
+ function isRecord2(value) {
29473
+ return typeof value === "object" && value !== null && !Array.isArray(value);
29474
+ }
29475
+ function stringValue2(value) {
29476
+ return typeof value === "string" && value.length > 0 ? value : void 0;
29477
+ }
29478
+ function timestampSeconds(value) {
29479
+ if (value instanceof Date) {
29480
+ const timestamp = value.getTime();
29481
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
29482
+ }
29483
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
29484
+ return value > 1e10 ? value / 1e3 : value;
29485
+ }
29486
+ if (typeof value === "string") {
29487
+ const timestamp = Date.parse(value);
29488
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
29489
+ }
29490
+ return void 0;
29491
+ }
29492
+ function dottedOrderDepth(run) {
29493
+ const dottedOrder = stringValue2(ownValue(run, "dotted_order"));
29494
+ return dottedOrder ? dottedOrder.split(".").length : Number.MAX_SAFE_INTEGER;
29495
+ }
29496
+ function mapRunType(runType) {
29497
+ switch (runType) {
29498
+ case "llm":
29499
+ case "embedding":
29500
+ return "llm" /* LLM */;
29501
+ case "tool":
29502
+ case "retriever":
29503
+ return "tool" /* TOOL */;
29504
+ default:
29505
+ return "task" /* TASK */;
29506
+ }
29507
+ }
29508
+ function extractMetadata(run, previous) {
29509
+ const extra = preferOwnValue(run, previous, "extra");
29510
+ const rawMetadata = ownValue(extra, "metadata");
29511
+ if (!isRecord2(rawMetadata)) {
29512
+ return void 0;
29513
+ }
29514
+ const metadata = {};
29515
+ for (const [key, descriptor] of Object.entries(
29516
+ Object.getOwnPropertyDescriptors(rawMetadata)
29517
+ )) {
29518
+ if (!("value" in descriptor) || !descriptor.enumerable || BLOCKED_METADATA_KEYS.has(key) || BLOCKED_METADATA_PREFIXES.some((prefix) => key.startsWith(prefix)) || key.startsWith("ls_") && key !== "ls_provider" && key !== "ls_model_name" && !LLM_SETTING_KEYS.some((setting) => key === `ls_${setting}`)) {
29519
+ continue;
29520
+ }
29521
+ const normalizedKey = key === "ls_provider" ? "provider" : key === "ls_model_name" ? "model" : key.startsWith("ls_") ? key.slice(3) : key;
29522
+ const sanitized = sanitizeLoggedValue(descriptor.value);
29523
+ if (sanitized !== void 0) {
29524
+ metadata[normalizedKey] = sanitized;
29525
+ }
29526
+ }
29527
+ return Object.keys(metadata).length === 0 ? void 0 : metadata;
29528
+ }
29529
+ function extractTags(value) {
29530
+ if (!Array.isArray(value)) {
29531
+ return void 0;
29532
+ }
29533
+ const tags = value.filter(
29534
+ (tag) => typeof tag === "string" && tag.length > 0
29535
+ );
29536
+ return tags.length > 0 ? tags : void 0;
29537
+ }
29538
+ function extractMetrics(run, previous) {
29539
+ const outputs = preferOwnValue(run, previous, "outputs");
29540
+ const extra = preferOwnValue(run, previous, "extra");
29541
+ const metadata = ownValue(extra, "metadata");
29542
+ const usage = ownValue(outputs, "usage_metadata") ?? ownValue(metadata, "usage_metadata");
29543
+ const metrics = {};
29544
+ assignFirstMetric(metrics, "prompt_tokens", usage, [
29545
+ "input_tokens",
29546
+ "prompt_tokens"
29547
+ ]);
29548
+ assignFirstMetric(metrics, "completion_tokens", usage, [
29549
+ "output_tokens",
29550
+ "completion_tokens"
29551
+ ]);
29552
+ assignFirstMetric(metrics, "tokens", usage, ["total_tokens", "tokens"]);
29553
+ const inputTokenDetails = ownValue(usage, "input_token_details");
29554
+ assignFirstMetric(metrics, "prompt_cached_tokens", inputTokenDetails, [
29555
+ "cache_read",
29556
+ "cached_tokens"
29557
+ ]);
29558
+ if (metrics.prompt_cached_tokens === void 0) {
29559
+ assignFirstMetric(metrics, "prompt_cached_tokens", usage, [
29560
+ "cache_read_input_tokens",
29561
+ "prompt_cached_tokens"
29562
+ ]);
29563
+ }
29564
+ assignFirstMetric(
29565
+ metrics,
29566
+ "prompt_cache_creation_tokens",
29567
+ inputTokenDetails,
29568
+ ["cache_creation"]
29569
+ );
29570
+ if (metrics.prompt_cache_creation_tokens === void 0) {
29571
+ assignFirstMetric(metrics, "prompt_cache_creation_tokens", usage, [
29572
+ "cache_creation_input_tokens",
29573
+ "prompt_cache_creation_tokens"
29574
+ ]);
29575
+ }
29576
+ if (metrics.tokens === void 0 && (metrics.prompt_tokens !== void 0 || metrics.completion_tokens !== void 0)) {
29577
+ metrics.tokens = (metrics.prompt_tokens ?? 0) + (metrics.completion_tokens ?? 0);
29578
+ }
29579
+ const startTime = timestampSeconds(
29580
+ preferOwnValue(run, previous, "start_time")
29581
+ );
29582
+ const events = preferOwnValue(run, previous, "events");
29583
+ if (startTime !== void 0 && Array.isArray(events)) {
29584
+ for (const event of events) {
29585
+ if (ownValue(event, "name") !== "new_token") {
29586
+ continue;
29587
+ }
29588
+ const eventTime3 = timestampSeconds(ownValue(event, "time"));
29589
+ if (eventTime3 !== void 0 && eventTime3 >= startTime) {
29590
+ metrics.time_to_first_token = eventTime3 - startTime;
29591
+ }
29592
+ break;
29593
+ }
29594
+ }
29595
+ return metrics;
29596
+ }
29597
+ function assignFirstMetric(metrics, target, source, keys) {
29598
+ for (const key of keys) {
29599
+ const value = ownValue(source, key);
29600
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
29601
+ metrics[target] = value;
29602
+ return;
29603
+ }
29604
+ }
29605
+ }
29606
+ function errorMessage(value) {
29607
+ if (typeof value === "string") {
29608
+ return value;
29609
+ }
29610
+ if (value instanceof Error) {
29611
+ return value.message;
29612
+ }
29613
+ return void 0;
29614
+ }
29615
+ function sanitizeLoggedValue(value, seen = /* @__PURE__ */ new WeakSet(), depth = 0) {
29616
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
29617
+ return value;
29618
+ }
29619
+ if (typeof value === "number") {
29620
+ return Number.isFinite(value) ? value : void 0;
29621
+ }
29622
+ if (value instanceof Date) {
29623
+ return Number.isFinite(value.getTime()) ? value.toISOString() : void 0;
29624
+ }
29625
+ if (typeof value !== "object" || depth >= 20) {
29626
+ return void 0;
29627
+ }
29628
+ if (seen.has(value)) {
29629
+ return "[Circular]";
29630
+ }
29631
+ seen.add(value);
29632
+ if (Array.isArray(value)) {
29633
+ return value.map((item) => sanitizeLoggedValue(item, seen, depth + 1));
29634
+ }
29635
+ const entries = [];
29636
+ for (const [key, descriptor] of Object.entries(
29637
+ Object.getOwnPropertyDescriptors(value)
29638
+ )) {
29639
+ if (!descriptor.enumerable || !("value" in descriptor) || BLOCKED_METADATA_KEYS.has(key)) {
29640
+ continue;
29641
+ }
29642
+ entries.push([key, sanitizeLoggedValue(descriptor.value, seen, depth + 1)]);
29643
+ }
29644
+ return Object.fromEntries(entries);
29645
+ }
29646
+
29081
29647
  // src/instrumentation/plugins/pi-coding-agent-channels.ts
29082
29648
  var piCodingAgentChannels = defineChannels(
29083
29649
  "@earendil-works/pi-coding-agent",
@@ -29457,11 +30023,11 @@ function patchAssistantMessageStream(stream, promptState, llmState) {
29457
30023
  function recordPiAssistantMessageEvent(promptState, llmState, event) {
29458
30024
  recordFirstTokenMetric(llmState, event);
29459
30025
  const message = "message" in event ? event.message : void 0;
29460
- const errorMessage = "error" in event ? event.error : void 0;
30026
+ const errorMessage2 = "error" in event ? event.error : void 0;
29461
30027
  if (event.type === "done" && isPiAssistantMessage(message)) {
29462
30028
  finishPiLlmSpan(promptState, llmState, message);
29463
- } else if (event.type === "error" && isPiAssistantMessage(errorMessage)) {
29464
- finishPiLlmSpan(promptState, llmState, errorMessage);
30029
+ } else if (event.type === "error" && isPiAssistantMessage(errorMessage2)) {
30030
+ finishPiLlmSpan(promptState, llmState, errorMessage2);
29465
30031
  }
29466
30032
  }
29467
30033
  function recordFirstTokenMetric(state, event) {
@@ -29979,6 +30545,7 @@ var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
29979
30545
  });
29980
30546
 
29981
30547
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
30548
+ var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
29982
30549
  var StrandsAgentSDKPlugin = class extends BasePlugin {
29983
30550
  activeChildParents = /* @__PURE__ */ new WeakMap();
29984
30551
  onEnable() {
@@ -30123,11 +30690,16 @@ function startAgentStream(event, activeChildParents) {
30123
30690
  ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
30124
30691
  };
30125
30692
  const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
30693
+ const attachmentCache = createStrandsAttachmentCache();
30694
+ const input = processStrandsInputAttachments(
30695
+ event.arguments[0],
30696
+ attachmentCache
30697
+ );
30126
30698
  const span = parentSpan ? withCurrent(
30127
30699
  parentSpan,
30128
30700
  () => startSpan({
30129
30701
  event: {
30130
- input: event.arguments[0],
30702
+ input,
30131
30703
  metadata
30132
30704
  },
30133
30705
  name: formatAgentSpanName(agent),
@@ -30135,7 +30707,7 @@ function startAgentStream(event, activeChildParents) {
30135
30707
  })
30136
30708
  ) : startSpan({
30137
30709
  event: {
30138
- input: event.arguments[0],
30710
+ input,
30139
30711
  metadata
30140
30712
  },
30141
30713
  name: formatAgentSpanName(agent),
@@ -30143,6 +30715,7 @@ function startAgentStream(event, activeChildParents) {
30143
30715
  });
30144
30716
  return {
30145
30717
  activeTools: /* @__PURE__ */ new Map(),
30718
+ attachmentCache,
30146
30719
  finalized: false,
30147
30720
  metadata,
30148
30721
  span,
@@ -30158,11 +30731,12 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30158
30731
  ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
30159
30732
  };
30160
30733
  const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
30734
+ const input = processStrandsInputAttachments(event.arguments[0]);
30161
30735
  const span = parentSpan ? withCurrent(
30162
30736
  parentSpan,
30163
30737
  () => startSpan({
30164
30738
  event: {
30165
- input: event.arguments[0],
30739
+ input,
30166
30740
  metadata
30167
30741
  },
30168
30742
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -30170,7 +30744,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30170
30744
  })
30171
30745
  ) : startSpan({
30172
30746
  event: {
30173
- input: event.arguments[0],
30747
+ input,
30174
30748
  metadata
30175
30749
  },
30176
30750
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -30279,7 +30853,10 @@ function startModelSpan(state, event) {
30279
30853
  state.span,
30280
30854
  () => startSpan({
30281
30855
  event: {
30282
- input: Array.isArray(event.agent?.messages) ? event.agent.messages : void 0,
30856
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
30857
+ event.agent.messages,
30858
+ state.attachmentCache
30859
+ ) : void 0,
30283
30860
  metadata
30284
30861
  },
30285
30862
  name: formatModelSpanName(model),
@@ -30493,6 +31070,7 @@ function finalizeAgentStream(state, error, output) {
30493
31070
  ...output !== void 0 ? { output } : {}
30494
31071
  });
30495
31072
  state.span.end();
31073
+ state.attachmentCache.strings.clear();
30496
31074
  }
30497
31075
  function finalizeMultiAgentStream(state, activeChildParents, error, output) {
30498
31076
  if (state.finalized) {
@@ -30639,6 +31217,166 @@ function extractNodeResultOutput(result) {
30639
31217
  }
30640
31218
  return result;
30641
31219
  }
31220
+ var STRANDS_MEDIA_TYPES = {
31221
+ png: "image/png",
31222
+ jpg: "image/jpeg",
31223
+ jpeg: "image/jpeg",
31224
+ gif: "image/gif",
31225
+ webp: "image/webp",
31226
+ mkv: "video/x-matroska",
31227
+ mov: "video/quicktime",
31228
+ mp4: "video/mp4",
31229
+ webm: "video/webm",
31230
+ flv: "video/x-flv",
31231
+ mpeg: "video/mpeg",
31232
+ mpg: "video/mpeg",
31233
+ wmv: "video/x-ms-wmv",
31234
+ "3gp": "video/3gpp",
31235
+ pdf: "application/pdf",
31236
+ csv: "text/csv",
31237
+ doc: "application/msword",
31238
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
31239
+ xls: "application/vnd.ms-excel",
31240
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
31241
+ html: "text/html",
31242
+ txt: "text/plain",
31243
+ md: "text/markdown",
31244
+ json: "application/json",
31245
+ xml: "application/xml"
31246
+ };
31247
+ function createStrandsAttachmentCache() {
31248
+ return {
31249
+ objects: /* @__PURE__ */ new WeakMap(),
31250
+ strings: new LRUCache({
31251
+ max: MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES
31252
+ })
31253
+ };
31254
+ }
31255
+ function processStrandsInputAttachments(input, cache = createStrandsAttachmentCache()) {
31256
+ try {
31257
+ return processStrandsInputNode(input, cache);
31258
+ } catch (error) {
31259
+ logInstrumentationError5("Strands Agent SDK input attachments", error);
31260
+ return input;
31261
+ }
31262
+ }
31263
+ function processStrandsInputNode(value, cache) {
31264
+ if (value instanceof BaseAttachment) {
31265
+ return value;
31266
+ }
31267
+ if (Array.isArray(value)) {
31268
+ return value.map((child) => processStrandsInputNode(child, cache));
31269
+ }
31270
+ if (!isObject(value)) {
31271
+ return value;
31272
+ }
31273
+ const directMedia = processDirectStrandsMediaBlock(value, cache);
31274
+ if (directMedia !== void 0) {
31275
+ return directMedia;
31276
+ }
31277
+ const wrappedMedia = processWrappedStrandsMediaBlock(value, cache);
31278
+ if (wrappedMedia !== void 0) {
31279
+ return wrappedMedia;
31280
+ }
31281
+ if (value.type === "message" && Array.isArray(value.content)) {
31282
+ return {
31283
+ role: value.role,
31284
+ content: value.content.map(
31285
+ (child) => processStrandsInputNode(child, cache)
31286
+ ),
31287
+ ...value.metadata !== void 0 ? { metadata: value.metadata } : {}
31288
+ };
31289
+ }
31290
+ if (typeof value.toJSON === "function") {
31291
+ return processStrandsInputNode(value.toJSON(), cache);
31292
+ }
31293
+ return Object.fromEntries(
31294
+ Object.entries(value).map(([key, child]) => [
31295
+ key,
31296
+ processStrandsInputNode(child, cache)
31297
+ ])
31298
+ );
31299
+ }
31300
+ function processDirectStrandsMediaBlock(block, cache) {
31301
+ if (!isStrandsMediaBlock(block)) {
31302
+ return void 0;
31303
+ }
31304
+ const mediaKey = block.type === "imageBlock" ? "image" : block.type === "videoBlock" ? "video" : "document";
31305
+ return createStrandsMediaAttachment(mediaKey, block, cache);
31306
+ }
31307
+ function isStrandsMediaBlock(block) {
31308
+ return (block.type === "imageBlock" || block.type === "videoBlock" || block.type === "documentBlock") && typeof block.format === "string" && isObject(block.source);
31309
+ }
31310
+ function processWrappedStrandsMediaBlock(block, cache) {
31311
+ for (const mediaKey of ["image", "video", "document"]) {
31312
+ const media = block[mediaKey];
31313
+ if (!Object.hasOwn(block, mediaKey) || !isObject(media)) {
31314
+ continue;
31315
+ }
31316
+ const processed = createStrandsMediaAttachment(mediaKey, media, cache);
31317
+ if (processed !== void 0) {
31318
+ return processed;
31319
+ }
31320
+ }
31321
+ return void 0;
31322
+ }
31323
+ function createStrandsMediaAttachment(mediaKey, media, cache) {
31324
+ const format = media.format;
31325
+ const source = media.source;
31326
+ if (typeof format !== "string" || !isObject(source) || !Object.hasOwn(source, "bytes")) {
31327
+ return void 0;
31328
+ }
31329
+ const contentType = STRANDS_MEDIA_TYPES[format.toLowerCase()];
31330
+ if (!contentType) {
31331
+ return void 0;
31332
+ }
31333
+ const filename = mediaKey === "document" && typeof media.name === "string" && media.name.length > 0 ? media.name : `${mediaKey}.${format.toLowerCase()}`;
31334
+ const attachment = getOrCreateStrandsAttachment(
31335
+ source.bytes,
31336
+ filename,
31337
+ contentType,
31338
+ cache
31339
+ );
31340
+ if (!attachment) {
31341
+ return void 0;
31342
+ }
31343
+ const { type: _type, ...serializedMedia } = media;
31344
+ const { type: _sourceType, ...serializedSource } = source;
31345
+ return {
31346
+ [mediaKey]: {
31347
+ ...serializedMedia,
31348
+ source: {
31349
+ ...serializedSource,
31350
+ bytes: attachment
31351
+ }
31352
+ }
31353
+ };
31354
+ }
31355
+ function getOrCreateStrandsAttachment(data, filename, contentType, cache) {
31356
+ const key = `${contentType}\0${filename}`;
31357
+ const attachments = typeof data === "string" ? cache.strings.get(data) : isObject(data) ? cache.objects.get(data) : void 0;
31358
+ const cached = attachments?.get(key);
31359
+ if (cached) {
31360
+ return cached;
31361
+ }
31362
+ const blob = convertDataToBlob(data, contentType);
31363
+ if (!blob) {
31364
+ return void 0;
31365
+ }
31366
+ const attachment = new Attachment({
31367
+ data: blob,
31368
+ filename,
31369
+ contentType
31370
+ });
31371
+ const updatedAttachments = attachments ?? /* @__PURE__ */ new Map();
31372
+ updatedAttachments.set(key, attachment);
31373
+ if (typeof data === "string") {
31374
+ cache.strings.set(data, updatedAttachments);
31375
+ } else if (isObject(data)) {
31376
+ cache.objects.set(data, updatedAttachments);
31377
+ }
31378
+ return attachment;
31379
+ }
30642
31380
  function normalizeContentBlocks(blocks) {
30643
31381
  const text = blocks.map((block) => typeof block.text === "string" ? block.text : void 0).filter((part) => Boolean(part)).join("");
30644
31382
  return text.length > 0 ? text : blocks;
@@ -30768,6 +31506,7 @@ var BraintrustPlugin = class extends BasePlugin {
30768
31506
  gitHubCopilotPlugin = null;
30769
31507
  fluePlugin = null;
30770
31508
  langChainPlugin = null;
31509
+ langSmithPlugin = null;
30771
31510
  piCodingAgentPlugin = null;
30772
31511
  strandsAgentSDKPlugin = null;
30773
31512
  constructor(config = {}) {
@@ -30864,6 +31603,12 @@ var BraintrustPlugin = class extends BasePlugin {
30864
31603
  this.langChainPlugin = new LangChainPlugin();
30865
31604
  this.langChainPlugin.enable();
30866
31605
  }
31606
+ if (integrations.langsmith !== false) {
31607
+ this.langSmithPlugin = new LangSmithPlugin({
31608
+ skipLangChainRuns: integrations.langchain !== false
31609
+ });
31610
+ this.langSmithPlugin.enable();
31611
+ }
30867
31612
  }
30868
31613
  onDisable() {
30869
31614
  if (this.openaiPlugin) {
@@ -30954,6 +31699,10 @@ var BraintrustPlugin = class extends BasePlugin {
30954
31699
  this.langChainPlugin.disable();
30955
31700
  this.langChainPlugin = null;
30956
31701
  }
31702
+ if (this.langSmithPlugin) {
31703
+ this.langSmithPlugin.disable();
31704
+ this.langSmithPlugin = null;
31705
+ }
30957
31706
  }
30958
31707
  };
30959
31708
 
@@ -31018,7 +31767,8 @@ var envIntegrationAliases = {
31018
31767
  langchain: "langchain",
31019
31768
  "langchain-js": "langchain",
31020
31769
  "@langchain": "langchain",
31021
- langgraph: "langgraph"
31770
+ langgraph: "langgraph",
31771
+ langsmith: "langsmith"
31022
31772
  };
31023
31773
  function getDefaultInstrumentationIntegrations() {
31024
31774
  return {
@@ -31049,6 +31799,7 @@ function getDefaultInstrumentationIntegrations() {
31049
31799
  gitHubCopilot: true,
31050
31800
  langchain: true,
31051
31801
  langgraph: true,
31802
+ langsmith: true,
31052
31803
  piCodingAgent: true,
31053
31804
  strandsAgentSDK: true
31054
31805
  };
@@ -31354,6 +32105,8 @@ __export(exports_exports, {
31354
32105
  addAzureBlobHeaders: () => addAzureBlobHeaders,
31355
32106
  agentAssertionScorer: () => agentAssertionScorer,
31356
32107
  braintrustAISDKTelemetry: () => braintrustAISDKTelemetry,
32108
+ braintrustEveHook: () => braintrustEveHook,
32109
+ braintrustEveInstrumentation: () => braintrustEveInstrumentation,
31357
32110
  braintrustFlueInstrumentation: () => braintrustFlueInstrumentation,
31358
32111
  braintrustFlueObserver: () => braintrustFlueObserver,
31359
32112
  braintrustStreamChunkSchema: () => braintrustStreamChunkSchema,
@@ -31443,6 +32196,9 @@ __export(exports_exports, {
31443
32196
  wrapGoogleGenAI: () => wrapGoogleGenAI,
31444
32197
  wrapGroq: () => wrapGroq,
31445
32198
  wrapHuggingFace: () => wrapHuggingFace,
32199
+ wrapLangSmithClient: () => wrapLangSmithClient,
32200
+ wrapLangSmithRunTrees: () => wrapLangSmithRunTrees,
32201
+ wrapLangSmithTraceable: () => wrapLangSmithTraceable,
31446
32202
  wrapMastraAgent: () => wrapMastraAgent,
31447
32203
  wrapMistral: () => wrapMistral,
31448
32204
  wrapOpenAI: () => wrapOpenAI,
@@ -32854,6 +33610,1390 @@ function BraintrustMiddleware(config = {}) {
32854
33610
  };
32855
33611
  }
32856
33612
 
33613
+ // src/instrumentation/plugins/eve-plugin.ts
33614
+ var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
33615
+ var MAX_EVE_CACHE_ENTRIES = 1e4;
33616
+ var MAX_STORED_LLM_INPUTS = 100;
33617
+ var MAX_STORED_SPAN_REFERENCES = 1e4;
33618
+ var MAX_STORED_STEP_STARTS = 1e4;
33619
+ function braintrustEveHook(options) {
33620
+ const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
33621
+ const bridge = new EveBridge(state);
33622
+ return {
33623
+ events: {
33624
+ "*": async (event, ctx) => {
33625
+ await bridge.handle(event, ctx, options.metadata);
33626
+ }
33627
+ }
33628
+ };
33629
+ }
33630
+ function braintrustEveInstrumentation(options) {
33631
+ const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
33632
+ return {
33633
+ events: {
33634
+ "step.started": (input) => {
33635
+ try {
33636
+ captureEveModelInput(state, input);
33637
+ } catch (error) {
33638
+ debugLogger.warn("Error in Eve LLM input capture:", error);
33639
+ }
33640
+ }
33641
+ },
33642
+ recordInputs: false,
33643
+ recordOutputs: false,
33644
+ setup: options.setup
33645
+ };
33646
+ }
33647
+ function isEveHandleMessageStreamEvent(event) {
33648
+ return isObject(event) && typeof event["type"] === "string";
33649
+ }
33650
+ var ResumedEveSpan = class {
33651
+ constructor(reference) {
33652
+ this.reference = reference;
33653
+ this.endTime = reference.endTime;
33654
+ }
33655
+ endTime;
33656
+ get rootSpanId() {
33657
+ return this.reference.rootSpanId;
33658
+ }
33659
+ get spanId() {
33660
+ return this.reference.spanId;
33661
+ }
33662
+ log(event) {
33663
+ const metrics = {
33664
+ ...this.reference.startEvent?.metrics,
33665
+ ...this.endTime === void 0 ? {} : { end: this.endTime },
33666
+ ...event.metrics
33667
+ };
33668
+ updateSpan({
33669
+ exported: this.reference.exported,
33670
+ ...this.reference.startEvent,
33671
+ ...event,
33672
+ ...Object.keys(metrics).length > 0 ? { metrics } : {}
33673
+ });
33674
+ }
33675
+ end(args) {
33676
+ if (this.endTime === void 0) {
33677
+ this.endTime = args?.endTime ?? getCurrentUnixTimestamp();
33678
+ this.log({ metrics: { end: this.endTime } });
33679
+ }
33680
+ return this.endTime;
33681
+ }
33682
+ };
33683
+ var EveBridge = class {
33684
+ constructor(state) {
33685
+ this.state = state;
33686
+ }
33687
+ eventQueuesBySession = /* @__PURE__ */ new Map();
33688
+ completedToolKeys = new LRUCache({
33689
+ max: MAX_EVE_CACHE_ENTRIES
33690
+ });
33691
+ toolsByCallKey = new LRUCache({
33692
+ max: MAX_EVE_CACHE_ENTRIES
33693
+ });
33694
+ turnsByKey = new LRUCache({
33695
+ max: MAX_EVE_CACHE_ENTRIES
33696
+ });
33697
+ async startEveSpan(args) {
33698
+ const rowId = args?.event?.id;
33699
+ const reference = typeof rowId === "string" && readEveTraceState(this.state).spanReferences.find(
33700
+ (candidate) => candidate.rowId === rowId
33701
+ );
33702
+ if (reference) {
33703
+ return new ResumedEveSpan(reference);
33704
+ }
33705
+ const startTime = args?.startTime ?? getCurrentUnixTimestamp();
33706
+ const parentSpanIds = args?.parentSpanIds;
33707
+ const startEvent = {
33708
+ created: (/* @__PURE__ */ new Date()).toISOString(),
33709
+ metrics: { start: startTime },
33710
+ span_attributes: {
33711
+ ...args?.name ? { name: args.name } : {},
33712
+ ...args?.type ? { type: args.type } : {},
33713
+ ...args?.spanAttributes
33714
+ },
33715
+ span_parents: parentSpanIds ? "spanId" in parentSpanIds ? [parentSpanIds.spanId] : parentSpanIds.parentSpanIds : []
33716
+ };
33717
+ const span = withCurrent(
33718
+ NOOP_SPAN,
33719
+ () => _internalStartSpanWithInitialMerge({ ...args, startTime })
33720
+ );
33721
+ if (typeof rowId !== "string") {
33722
+ return span;
33723
+ }
33724
+ try {
33725
+ const exported = await span.export();
33726
+ const reference2 = {
33727
+ exported,
33728
+ rootSpanId: span.rootSpanId,
33729
+ rowId,
33730
+ spanId: span.spanId,
33731
+ startEvent
33732
+ };
33733
+ this.state.update((current) => {
33734
+ const normalized = normalizeEveTraceState(current);
33735
+ return normalized.spanReferences.some(
33736
+ (candidate) => candidate.rowId === rowId
33737
+ ) ? normalized : {
33738
+ ...normalized,
33739
+ spanReferences: [...normalized.spanReferences, reference2].slice(
33740
+ -MAX_STORED_SPAN_REFERENCES
33741
+ )
33742
+ };
33743
+ });
33744
+ } catch (error) {
33745
+ debugLogger.warn("Error exporting Eve span for resumption:", error);
33746
+ }
33747
+ return span;
33748
+ }
33749
+ async startEveChildSpan(parent, args) {
33750
+ return await this.startEveSpan({
33751
+ ...args,
33752
+ parentSpanIds: {
33753
+ rootSpanId: parent.rootSpanId,
33754
+ spanId: parent.spanId
33755
+ }
33756
+ });
33757
+ }
33758
+ stepOrdinal(event) {
33759
+ let ordinal = 0;
33760
+ this.state.update((current) => {
33761
+ const state = normalizeEveTraceState(current);
33762
+ const previous = state.stepStarts.filter(
33763
+ (entry) => entry.turnId === event.data.turnId && entry.stepIndex === event.data.stepIndex
33764
+ ).at(-1);
33765
+ if (previous?.open) {
33766
+ ordinal = previous.ordinal;
33767
+ return state;
33768
+ }
33769
+ ordinal = state.stepStarts.filter(
33770
+ (entry) => entry.turnId === event.data.turnId
33771
+ ).length;
33772
+ return {
33773
+ ...state,
33774
+ stepStarts: [
33775
+ ...state.stepStarts,
33776
+ {
33777
+ open: true,
33778
+ ordinal,
33779
+ stepIndex: event.data.stepIndex,
33780
+ turnId: event.data.turnId
33781
+ }
33782
+ ].slice(-MAX_STORED_STEP_STARTS)
33783
+ };
33784
+ });
33785
+ return ordinal;
33786
+ }
33787
+ markStepEnded(turnId, stepIndex) {
33788
+ this.state.update((current) => {
33789
+ const state = normalizeEveTraceState(current);
33790
+ let index = -1;
33791
+ for (let i = state.stepStarts.length - 1; i >= 0; i--) {
33792
+ const entry = state.stepStarts[i];
33793
+ if (entry?.turnId === turnId && entry.stepIndex === stepIndex) {
33794
+ index = i;
33795
+ break;
33796
+ }
33797
+ }
33798
+ if (index < 0 || !state.stepStarts[index]?.open) {
33799
+ return state;
33800
+ }
33801
+ return {
33802
+ ...state,
33803
+ stepStarts: state.stepStarts.map(
33804
+ (entry, entryIndex) => entryIndex === index ? { ...entry, open: false } : entry
33805
+ )
33806
+ };
33807
+ });
33808
+ }
33809
+ async handle(event, ctx, hookMetadata) {
33810
+ if (!isEveHandleMessageStreamEvent(event)) {
33811
+ return;
33812
+ }
33813
+ const run = async () => {
33814
+ try {
33815
+ if (!await this.handleEvent(event, ctx, hookMetadata)) {
33816
+ return;
33817
+ }
33818
+ if (event.type === "session.failed") {
33819
+ const sessionId2 = event.data.sessionId || sessionIdFromContext(ctx);
33820
+ await this.flushInstrumentation();
33821
+ if (sessionId2) {
33822
+ this.cleanupSession(sessionId2);
33823
+ }
33824
+ } else if (event.type === "session.completed") {
33825
+ const sessionId2 = sessionIdFromContext(ctx);
33826
+ await this.flushInstrumentation();
33827
+ if (sessionId2) {
33828
+ this.cleanupSession(sessionId2);
33829
+ }
33830
+ }
33831
+ } catch (error) {
33832
+ debugLogger.warn("Error in Eve hook instrumentation:", error);
33833
+ }
33834
+ };
33835
+ const sessionId = event.type === "session.failed" ? event.data.sessionId || sessionIdFromContext(ctx) : sessionIdFromContext(ctx);
33836
+ if (!sessionId) {
33837
+ await run();
33838
+ return;
33839
+ }
33840
+ const previous = this.eventQueuesBySession.get(sessionId);
33841
+ const queued = previous ? previous.then(run) : run();
33842
+ this.eventQueuesBySession.set(sessionId, queued);
33843
+ try {
33844
+ await queued;
33845
+ } finally {
33846
+ if (this.eventQueuesBySession.get(sessionId) === queued) {
33847
+ this.eventQueuesBySession.delete(sessionId);
33848
+ }
33849
+ }
33850
+ }
33851
+ async handleEvent(event, ctx, hookMetadata) {
33852
+ switch (event.type) {
33853
+ case "session.started":
33854
+ this.handleSessionStarted(event, ctx, hookMetadata);
33855
+ return true;
33856
+ case "turn.started":
33857
+ await this.handleTurnStarted(event, ctx, hookMetadata);
33858
+ return true;
33859
+ case "message.received":
33860
+ await this.handleMessageReceived(event, ctx, hookMetadata);
33861
+ return true;
33862
+ case "step.started":
33863
+ await this.handleStepStarted(event, ctx, hookMetadata);
33864
+ return true;
33865
+ case "message.completed":
33866
+ this.handleMessageCompleted(event, ctx);
33867
+ return true;
33868
+ case "result.completed":
33869
+ this.handleResultCompleted(event, ctx);
33870
+ return true;
33871
+ case "actions.requested":
33872
+ await this.handleActionsRequested(event, ctx, hookMetadata);
33873
+ return true;
33874
+ case "action.result":
33875
+ await this.handleActionResult(event, ctx, hookMetadata);
33876
+ return true;
33877
+ case "subagent.called":
33878
+ await this.handleSubagentCalled(event, ctx, hookMetadata);
33879
+ return true;
33880
+ case "subagent.completed":
33881
+ await this.handleSubagentCompleted(event, ctx, hookMetadata);
33882
+ return true;
33883
+ case "step.completed":
33884
+ this.handleStepCompleted(event, ctx);
33885
+ return true;
33886
+ case "step.failed":
33887
+ this.handleStepFailed(event, ctx);
33888
+ return true;
33889
+ case "turn.completed":
33890
+ this.handleTurnCompleted(event, ctx);
33891
+ return true;
33892
+ case "turn.failed":
33893
+ this.handleTurnFailed(event, ctx);
33894
+ return true;
33895
+ case "session.failed":
33896
+ this.handleSessionFailed(event, ctx);
33897
+ return true;
33898
+ case "session.completed":
33899
+ this.handleSessionCompleted(event, ctx);
33900
+ return true;
33901
+ default:
33902
+ return false;
33903
+ }
33904
+ }
33905
+ handleSessionStarted(event, ctx, hookMetadata) {
33906
+ const sessionId = sessionIdFromContext(ctx);
33907
+ if (!sessionId) {
33908
+ return;
33909
+ }
33910
+ const metadata = {
33911
+ ...hookMetadata ?? {},
33912
+ ...modelMetadataFromRuntime(event.data.runtime)
33913
+ };
33914
+ this.state.update((current) => {
33915
+ const normalized = normalizeEveTraceState(current);
33916
+ return {
33917
+ ...normalized,
33918
+ metadata: { ...normalized.metadata, ...metadata }
33919
+ };
33920
+ });
33921
+ for (const [key, turn] of this.turnsByKey) {
33922
+ if (!key.startsWith(`${sessionId}:`)) {
33923
+ continue;
33924
+ }
33925
+ turn.metadata = { ...turn.metadata, ...metadata };
33926
+ turn.span.log({ metadata: turn.metadata });
33927
+ for (const step of turn.stepsByIndex.values()) {
33928
+ step.metadata = { ...step.metadata, ...metadata };
33929
+ step.span.log({ metadata: step.metadata });
33930
+ }
33931
+ }
33932
+ }
33933
+ async handleTurnStarted(event, ctx, hookMetadata) {
33934
+ const sessionId = sessionIdFromContext(ctx);
33935
+ if (!sessionId) {
33936
+ return;
33937
+ }
33938
+ const key = turnKey2(sessionId, event.data.turnId);
33939
+ const metadata = {
33940
+ ...readEveTraceState(this.state).metadata,
33941
+ ...hookMetadata ?? {},
33942
+ "eve.session_id": sessionId
33943
+ };
33944
+ const existing = this.turnsByKey.get(key);
33945
+ if (existing) {
33946
+ existing.metadata = { ...existing.metadata, ...metadata };
33947
+ existing.span.log({ metadata: existing.metadata });
33948
+ return;
33949
+ }
33950
+ const span = await this.startTurnSpan(sessionId, event, ctx, metadata);
33951
+ span.log({ metadata });
33952
+ this.turnsByKey.set(key, {
33953
+ key,
33954
+ metadata,
33955
+ metrics: {},
33956
+ span,
33957
+ stepsByIndex: /* @__PURE__ */ new Map(),
33958
+ turnId: event.data.turnId
33959
+ });
33960
+ }
33961
+ async handleMessageReceived(event, ctx, hookMetadata) {
33962
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
33963
+ if (!turn) {
33964
+ return;
33965
+ }
33966
+ const input = [{ content: event.data.message, role: "user" }];
33967
+ turn.span.log({ input });
33968
+ }
33969
+ async handleStepStarted(event, ctx, hookMetadata) {
33970
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
33971
+ const sessionId = sessionIdFromContext(ctx);
33972
+ if (!turn || !sessionId) {
33973
+ return;
33974
+ }
33975
+ const existing = turn.stepsByIndex.get(event.data.stepIndex);
33976
+ if (existing) {
33977
+ existing.span.log({
33978
+ ...existing.input !== void 0 ? { input: existing.input } : {},
33979
+ metadata: existing.metadata,
33980
+ metrics: existing.metrics,
33981
+ output: existing.output
33982
+ });
33983
+ const endTime = eventTime2(event);
33984
+ existing.span.end(endTime === void 0 ? void 0 : { endTime });
33985
+ this.markStepEnded(event.data.turnId, event.data.stepIndex);
33986
+ }
33987
+ const stepOrdinal = this.stepOrdinal(event);
33988
+ const metadata = { ...turn.metadata };
33989
+ const input = consumeCapturedEveModelInput(
33990
+ this.state,
33991
+ sessionId,
33992
+ event.data.turnId,
33993
+ event.data.stepIndex
33994
+ );
33995
+ const { rowId: eventId, spanId } = await generateEveIds(
33996
+ "step",
33997
+ sessionId,
33998
+ event.data.turnId,
33999
+ String(stepOrdinal)
34000
+ );
34001
+ const span = await this.startEveChildSpan(turn.span, {
34002
+ event: {
34003
+ id: eventId,
34004
+ ...input !== void 0 ? { input } : {},
34005
+ metadata
34006
+ },
34007
+ name: "eve.step",
34008
+ spanAttributes: { type: "llm" /* LLM */ },
34009
+ spanId,
34010
+ startTime: eventTime2(event)
34011
+ });
34012
+ span.log({ ...input !== void 0 ? { input } : {}, metadata });
34013
+ turn.stepsByIndex.set(event.data.stepIndex, {
34014
+ ...input !== void 0 ? { input } : {},
34015
+ metadata,
34016
+ metrics: {},
34017
+ span
34018
+ });
34019
+ }
34020
+ handleMessageCompleted(event, ctx) {
34021
+ const step = this.stepForEvent(event, ctx);
34022
+ if (!step) {
34023
+ return;
34024
+ }
34025
+ const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
34026
+ const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
34027
+ step.output = [
34028
+ {
34029
+ finish_reason: normalizedFinishReason(event.data.finishReason),
34030
+ index: 0,
34031
+ message: {
34032
+ content: event.data.message,
34033
+ role: "assistant",
34034
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
34035
+ }
34036
+ }
34037
+ ];
34038
+ const turn = this.turnForEvent(event, ctx);
34039
+ if (turn && event.data.finishReason !== "tool-calls") {
34040
+ turn.output = event.data.message;
34041
+ }
34042
+ }
34043
+ handleResultCompleted(event, ctx) {
34044
+ const step = this.stepForEvent(event, ctx);
34045
+ if (step) {
34046
+ step.output = [
34047
+ {
34048
+ finish_reason: "stop",
34049
+ index: 0,
34050
+ message: {
34051
+ content: event.data.result,
34052
+ role: "assistant"
34053
+ }
34054
+ }
34055
+ ];
34056
+ }
34057
+ const turn = this.turnForEvent(event, ctx);
34058
+ if (turn) {
34059
+ turn.output = event.data.result;
34060
+ }
34061
+ }
34062
+ async handleActionsRequested(event, ctx, hookMetadata) {
34063
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34064
+ const sessionId = sessionIdFromContext(ctx);
34065
+ if (!turn || !sessionId) {
34066
+ return;
34067
+ }
34068
+ const traceActions = event.data.actions.filter(isTraceableActionRequest);
34069
+ if (traceActions.length === 0) {
34070
+ return;
34071
+ }
34072
+ for (const action of traceActions) {
34073
+ if (isToolCallAction(action)) {
34074
+ await this.startRequestedTool(event, turn, sessionId, action);
34075
+ } else if (isLocalSubagentCallAction(action)) {
34076
+ await this.startRequestedSubagent(event, turn, sessionId, action);
34077
+ }
34078
+ }
34079
+ const step = turn.stepsByIndex.get(event.data.stepIndex);
34080
+ if (!step) {
34081
+ return;
34082
+ }
34083
+ const toolCallsById = /* @__PURE__ */ new Map();
34084
+ if (Array.isArray(step.output) && isObject(step.output[0])) {
34085
+ const message = step.output[0]["message"];
34086
+ if (isObject(message) && Array.isArray(message["tool_calls"])) {
34087
+ for (const toolCall of message["tool_calls"]) {
34088
+ if (isObject(toolCall) && typeof toolCall["id"] === "string") {
34089
+ toolCallsById.set(toolCall["id"], toolCall);
34090
+ }
34091
+ }
34092
+ }
34093
+ }
34094
+ for (const action of traceActions) {
34095
+ const name = action.kind === "tool-call" ? action.toolName : action.subagentName ?? action.name ?? "agent";
34096
+ toolCallsById.set(action.callId, {
34097
+ function: {
34098
+ arguments: JSON.stringify(action.input),
34099
+ name
34100
+ },
34101
+ id: action.callId,
34102
+ type: "function"
34103
+ });
34104
+ }
34105
+ step.output = [
34106
+ {
34107
+ finish_reason: "tool_calls",
34108
+ index: 0,
34109
+ message: {
34110
+ content: null,
34111
+ role: "assistant",
34112
+ tool_calls: [...toolCallsById.values()]
34113
+ }
34114
+ }
34115
+ ];
34116
+ step.span.log({ metadata: step.metadata, output: step.output });
34117
+ }
34118
+ async handleActionResult(event, ctx, hookMetadata) {
34119
+ if (isToolResult(event.data.result)) {
34120
+ await this.handleToolResult(event, ctx, event.data.result, hookMetadata);
34121
+ return;
34122
+ }
34123
+ if (isSubagentResult(event.data.result)) {
34124
+ await this.handleSubagentResult(
34125
+ event,
34126
+ ctx,
34127
+ event.data.result,
34128
+ hookMetadata
34129
+ );
34130
+ }
34131
+ }
34132
+ async handleToolResult(event, ctx, result, hookMetadata) {
34133
+ const sessionId = sessionIdFromContext(ctx);
34134
+ if (!sessionId) {
34135
+ return;
34136
+ }
34137
+ const key = toolKey3(sessionId, result.callId);
34138
+ if (this.completedToolKeys.has(key)) {
34139
+ return;
34140
+ }
34141
+ const tool = this.toolsByCallKey.get(key) ?? await this.startSyntheticTool(event, ctx, result, hookMetadata);
34142
+ if (!tool) {
34143
+ return;
34144
+ }
34145
+ const failed = event.data.status === "failed" || result.isError === true || event.data.error !== void 0;
34146
+ tool.span.log({
34147
+ ...failed ? {
34148
+ error: actionResultError(event.data.error, result.output)
34149
+ } : {},
34150
+ metadata: tool.metadata,
34151
+ output: result.output
34152
+ });
34153
+ const endTime = eventTime2(event);
34154
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34155
+ this.toolsByCallKey.delete(key);
34156
+ this.completedToolKeys.set(key, true);
34157
+ }
34158
+ async handleSubagentCalled(event, ctx, hookMetadata) {
34159
+ if (event.data.remote?.url) {
34160
+ return;
34161
+ }
34162
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34163
+ const sessionId = sessionIdFromContext(ctx);
34164
+ if (!turn || !sessionId) {
34165
+ return;
34166
+ }
34167
+ const key = toolKey3(sessionId, event.data.callId);
34168
+ const metadata = toolMetadataFromTurn(turn);
34169
+ const existing = this.toolsByCallKey.get(key);
34170
+ if (existing) {
34171
+ existing.metadata = { ...existing.metadata, ...metadata };
34172
+ existing.span.log({ metadata: existing.metadata });
34173
+ return;
34174
+ }
34175
+ if (this.completedToolKeys.has(key)) {
34176
+ return;
34177
+ }
34178
+ const { rowId: eventId, spanId } = await generateEveIds(
34179
+ "subagent",
34180
+ sessionId,
34181
+ event.data.callId
34182
+ );
34183
+ const pending = this.toolsByCallKey.get(key);
34184
+ if (pending || this.completedToolKeys.has(key)) {
34185
+ if (pending) {
34186
+ pending.metadata = { ...pending.metadata, ...metadata };
34187
+ pending.span.log({ metadata: pending.metadata });
34188
+ }
34189
+ return;
34190
+ }
34191
+ const span = await this.startEveChildSpan(turn.span, {
34192
+ event: {
34193
+ id: eventId,
34194
+ metadata
34195
+ },
34196
+ name: event.data.toolName ?? event.data.name,
34197
+ spanAttributes: { type: "tool" /* TOOL */ },
34198
+ spanId,
34199
+ startTime: eventTime2(event)
34200
+ });
34201
+ span.log({ metadata });
34202
+ this.toolsByCallKey.set(key, {
34203
+ metadata,
34204
+ span,
34205
+ turnKey: turnKey2(sessionId, event.data.turnId)
34206
+ });
34207
+ }
34208
+ async handleSubagentCompleted(event, ctx, hookMetadata) {
34209
+ const sessionId = sessionIdFromContext(ctx);
34210
+ if (!sessionId) {
34211
+ return;
34212
+ }
34213
+ const key = toolKey3(sessionId, event.data.callId);
34214
+ if (this.completedToolKeys.has(key)) {
34215
+ return;
34216
+ }
34217
+ const subagent = this.toolsByCallKey.get(key) ?? await this.startSyntheticSubagent(event, ctx, hookMetadata);
34218
+ if (!subagent) {
34219
+ return;
34220
+ }
34221
+ subagent.span.log({
34222
+ ...event.data.status === "failed" ? {
34223
+ error: actionResultError(event.data.error, event.data.output)
34224
+ } : {},
34225
+ metadata: subagent.metadata,
34226
+ ...event.data.output !== void 0 ? { output: event.data.output } : {}
34227
+ });
34228
+ const endTime = eventTime2(event);
34229
+ const recordedEndTime = subagent.span.end(
34230
+ endTime === void 0 ? void 0 : { endTime }
34231
+ );
34232
+ this.state.update((current) => {
34233
+ const normalized = normalizeEveTraceState(current);
34234
+ return {
34235
+ ...normalized,
34236
+ spanReferences: normalized.spanReferences.map(
34237
+ (reference) => reference.spanId === subagent.span.spanId ? { ...reference, endTime: recordedEndTime } : reference
34238
+ )
34239
+ };
34240
+ });
34241
+ }
34242
+ async handleSubagentResult(event, ctx, result, hookMetadata) {
34243
+ const sessionId = sessionIdFromContext(ctx);
34244
+ if (!sessionId) {
34245
+ return;
34246
+ }
34247
+ const key = toolKey3(sessionId, result.callId);
34248
+ if (this.completedToolKeys.has(key)) {
34249
+ return;
34250
+ }
34251
+ const subagent = this.toolsByCallKey.get(key) ?? await this.startSyntheticSubagentResult(
34252
+ event,
34253
+ ctx,
34254
+ result,
34255
+ hookMetadata
34256
+ );
34257
+ if (!subagent) {
34258
+ return;
34259
+ }
34260
+ const isError = event.data.status === "failed" || result.isError === true || event.data.error !== void 0;
34261
+ subagent.span.log({
34262
+ ...isError ? {
34263
+ error: actionResultError(event.data.error, result.output)
34264
+ } : {},
34265
+ metadata: subagent.metadata,
34266
+ output: result.output
34267
+ });
34268
+ const endTime = eventTime2(event);
34269
+ subagent.span.end(endTime === void 0 ? void 0 : { endTime });
34270
+ this.toolsByCallKey.delete(key);
34271
+ this.completedToolKeys.set(key, true);
34272
+ }
34273
+ handleStepCompleted(event, ctx) {
34274
+ const step = this.stepForEvent(event, ctx);
34275
+ if (!step) {
34276
+ return;
34277
+ }
34278
+ const usage = event.data.usage;
34279
+ const inputTokens = typeof usage?.inputTokens === "number" && Number.isFinite(usage.inputTokens) && usage.inputTokens >= 0 ? usage.inputTokens : void 0;
34280
+ const outputTokens = typeof usage?.outputTokens === "number" && Number.isFinite(usage.outputTokens) && usage.outputTokens >= 0 ? usage.outputTokens : void 0;
34281
+ const cacheReadTokens = typeof usage?.cacheReadTokens === "number" && Number.isFinite(usage.cacheReadTokens) && usage.cacheReadTokens >= 0 ? usage.cacheReadTokens : void 0;
34282
+ const cacheWriteTokens = typeof usage?.cacheWriteTokens === "number" && Number.isFinite(usage.cacheWriteTokens) && usage.cacheWriteTokens >= 0 ? usage.cacheWriteTokens : void 0;
34283
+ const costUsd = typeof usage?.costUsd === "number" && Number.isFinite(usage.costUsd) && usage.costUsd >= 0 ? usage.costUsd : void 0;
34284
+ const total = inputTokens !== void 0 && outputTokens !== void 0 ? inputTokens + outputTokens : void 0;
34285
+ const metrics = {
34286
+ ...inputTokens !== void 0 ? { prompt_tokens: inputTokens } : {},
34287
+ ...outputTokens !== void 0 ? { completion_tokens: outputTokens } : {},
34288
+ ...total !== void 0 ? { tokens: total } : {},
34289
+ ...cacheReadTokens !== void 0 ? { prompt_cached_tokens: cacheReadTokens } : {},
34290
+ ...cacheWriteTokens !== void 0 ? { prompt_cache_creation_tokens: cacheWriteTokens } : {},
34291
+ ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
34292
+ };
34293
+ step.metrics = { ...step.metrics, ...metrics };
34294
+ if (Array.isArray(step.output) && isObject(step.output[0])) {
34295
+ const finishReason = step.output[0].finish_reason;
34296
+ if (typeof finishReason !== "string") {
34297
+ step.output[0].finish_reason = normalizedFinishReason(
34298
+ event.data.finishReason
34299
+ );
34300
+ }
34301
+ }
34302
+ step.span.log({
34303
+ ...step.input !== void 0 ? { input: step.input } : {},
34304
+ metadata: step.metadata,
34305
+ metrics,
34306
+ output: step.output
34307
+ });
34308
+ const endTime = eventTime2(event);
34309
+ step.span.end(endTime === void 0 ? void 0 : { endTime });
34310
+ const turn = this.turnForEvent(event, ctx);
34311
+ if (turn) {
34312
+ for (const [key, value] of Object.entries(metrics)) {
34313
+ turn.metrics[key] = (turn.metrics[key] ?? 0) + value;
34314
+ }
34315
+ turn.stepsByIndex.delete(event.data.stepIndex);
34316
+ }
34317
+ this.markStepEnded(event.data.turnId, event.data.stepIndex);
34318
+ }
34319
+ handleStepFailed(event, ctx) {
34320
+ const step = this.stepForEvent(event, ctx);
34321
+ if (step) {
34322
+ step.span.log({
34323
+ error: errorFromMessage(
34324
+ event.data.message,
34325
+ event.data.code,
34326
+ event.data.details
34327
+ )
34328
+ });
34329
+ const endTime = eventTime2(event);
34330
+ step.span.end(endTime === void 0 ? void 0 : { endTime });
34331
+ }
34332
+ const turn = this.turnForEvent(event, ctx);
34333
+ turn?.stepsByIndex.delete(event.data.stepIndex);
34334
+ this.markStepEnded(event.data.turnId, event.data.stepIndex);
34335
+ }
34336
+ handleTurnCompleted(event, ctx) {
34337
+ const turn = this.turnForEvent(event, ctx);
34338
+ if (!turn) {
34339
+ return;
34340
+ }
34341
+ this.finalizeTurn(turn, {
34342
+ endTime: eventTime2(event)
34343
+ });
34344
+ }
34345
+ handleTurnFailed(event, ctx) {
34346
+ const turn = this.turnForEvent(event, ctx);
34347
+ if (!turn) {
34348
+ return;
34349
+ }
34350
+ this.finalizeTurn(turn, {
34351
+ endTime: eventTime2(event),
34352
+ error: errorFromMessage(
34353
+ event.data.message,
34354
+ event.data.code,
34355
+ event.data.details
34356
+ )
34357
+ });
34358
+ }
34359
+ handleSessionFailed(event, ctx) {
34360
+ const sessionId = event.data.sessionId || sessionIdFromContext(ctx);
34361
+ if (!sessionId) {
34362
+ return;
34363
+ }
34364
+ const error = errorFromMessage(
34365
+ event.data.message,
34366
+ event.data.code,
34367
+ event.data.details
34368
+ );
34369
+ for (const [key, turn] of this.turnsByKey) {
34370
+ if (!key.startsWith(`${sessionId}:`)) {
34371
+ continue;
34372
+ }
34373
+ this.finalizeTurn(turn, {
34374
+ endTime: eventTime2(event),
34375
+ error
34376
+ });
34377
+ }
34378
+ for (const [key, tool] of this.toolsByCallKey) {
34379
+ if (key.startsWith(`${sessionId}:`)) {
34380
+ const endTime = eventTime2(event);
34381
+ if (!tool.endedByTurn) {
34382
+ tool.span.log({ metadata: tool.metadata });
34383
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34384
+ tool.endedByTurn = true;
34385
+ }
34386
+ }
34387
+ }
34388
+ }
34389
+ handleSessionCompleted(event, ctx) {
34390
+ const sessionId = sessionIdFromContext(ctx);
34391
+ if (!sessionId) {
34392
+ return;
34393
+ }
34394
+ for (const [key, turn] of this.turnsByKey) {
34395
+ if (!key.startsWith(`${sessionId}:`)) {
34396
+ continue;
34397
+ }
34398
+ this.finalizeTurn(turn, {
34399
+ endTime: eventTime2(event)
34400
+ });
34401
+ }
34402
+ for (const [key, tool] of this.toolsByCallKey) {
34403
+ if (key.startsWith(`${sessionId}:`) && !tool.endedByTurn) {
34404
+ const endTime = eventTime2(event);
34405
+ tool.span.log({ metadata: tool.metadata });
34406
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34407
+ tool.endedByTurn = true;
34408
+ }
34409
+ }
34410
+ }
34411
+ async ensureTurn(event, ctx, hookMetadata) {
34412
+ const sessionId = sessionIdFromContext(ctx);
34413
+ if (!sessionId) {
34414
+ return void 0;
34415
+ }
34416
+ const key = turnKey2(sessionId, event.data.turnId);
34417
+ const existing = this.turnsByKey.get(key);
34418
+ if (existing) {
34419
+ return existing;
34420
+ }
34421
+ const metadata = {
34422
+ ...readEveTraceState(this.state).metadata,
34423
+ ...hookMetadata ?? {},
34424
+ "eve.session_id": sessionId
34425
+ };
34426
+ const span = await this.startTurnSpan(sessionId, event, ctx, metadata);
34427
+ span.log({ metadata });
34428
+ const state = {
34429
+ key,
34430
+ metadata,
34431
+ metrics: {},
34432
+ span,
34433
+ stepsByIndex: /* @__PURE__ */ new Map(),
34434
+ turnId: event.data.turnId
34435
+ };
34436
+ this.turnsByKey.set(key, state);
34437
+ return state;
34438
+ }
34439
+ async startRequestedTool(event, turn, sessionId, action) {
34440
+ const key = toolKey3(sessionId, action.callId);
34441
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34442
+ return;
34443
+ }
34444
+ const metadata = toolMetadataFromTurn(turn);
34445
+ const { rowId: eventId, spanId } = await generateEveIds(
34446
+ "tool",
34447
+ sessionId,
34448
+ event.data.turnId,
34449
+ action.callId
34450
+ );
34451
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34452
+ return;
34453
+ }
34454
+ const span = await this.startEveChildSpan(turn.span, {
34455
+ event: {
34456
+ id: eventId,
34457
+ input: action.input,
34458
+ metadata
34459
+ },
34460
+ name: action.toolName,
34461
+ spanAttributes: { type: "tool" /* TOOL */ },
34462
+ spanId,
34463
+ startTime: eventTime2(event)
34464
+ });
34465
+ span.log({ input: action.input, metadata });
34466
+ this.toolsByCallKey.set(key, {
34467
+ metadata,
34468
+ span,
34469
+ turnKey: turnKey2(sessionId, event.data.turnId)
34470
+ });
34471
+ }
34472
+ async startRequestedSubagent(event, turn, sessionId, action) {
34473
+ const key = toolKey3(sessionId, action.callId);
34474
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34475
+ return;
34476
+ }
34477
+ const name = action.subagentName ?? action.name ?? "agent";
34478
+ const metadata = toolMetadataFromTurn(turn);
34479
+ const { rowId: eventId, spanId } = await generateEveIds(
34480
+ "subagent",
34481
+ sessionId,
34482
+ action.callId
34483
+ );
34484
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34485
+ return;
34486
+ }
34487
+ const span = await this.startEveChildSpan(turn.span, {
34488
+ event: {
34489
+ id: eventId,
34490
+ input: action.input,
34491
+ metadata
34492
+ },
34493
+ name,
34494
+ spanAttributes: { type: "tool" /* TOOL */ },
34495
+ spanId,
34496
+ startTime: eventTime2(event)
34497
+ });
34498
+ span.log({ input: action.input, metadata });
34499
+ this.toolsByCallKey.set(key, {
34500
+ metadata,
34501
+ span,
34502
+ turnKey: turnKey2(sessionId, event.data.turnId)
34503
+ });
34504
+ }
34505
+ async startSyntheticTool(event, ctx, result, hookMetadata) {
34506
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34507
+ const sessionId = sessionIdFromContext(ctx);
34508
+ if (!turn || !sessionId) {
34509
+ return void 0;
34510
+ }
34511
+ const metadata = toolMetadataFromTurn(turn);
34512
+ const { rowId: eventId, spanId } = await generateEveIds(
34513
+ "tool",
34514
+ sessionId,
34515
+ event.data.turnId,
34516
+ result.callId
34517
+ );
34518
+ const existing = this.toolsByCallKey.get(toolKey3(sessionId, result.callId));
34519
+ if (existing) {
34520
+ return existing;
34521
+ }
34522
+ const span = await this.startEveChildSpan(turn.span, {
34523
+ event: {
34524
+ id: eventId,
34525
+ metadata
34526
+ },
34527
+ name: result.toolName,
34528
+ spanAttributes: { type: "tool" /* TOOL */ },
34529
+ spanId,
34530
+ startTime: eventTime2(event)
34531
+ });
34532
+ span.log({ metadata });
34533
+ const state = {
34534
+ metadata,
34535
+ span,
34536
+ turnKey: turnKey2(sessionId, event.data.turnId)
34537
+ };
34538
+ this.toolsByCallKey.set(toolKey3(sessionId, result.callId), state);
34539
+ return state;
34540
+ }
34541
+ async startSyntheticSubagent(event, ctx, hookMetadata) {
34542
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34543
+ const sessionId = sessionIdFromContext(ctx);
34544
+ if (!turn || !sessionId) {
34545
+ return void 0;
34546
+ }
34547
+ const metadata = toolMetadataFromTurn(turn);
34548
+ const { rowId: eventId, spanId } = await generateEveIds(
34549
+ "subagent",
34550
+ sessionId,
34551
+ event.data.callId
34552
+ );
34553
+ const existing = this.toolsByCallKey.get(
34554
+ toolKey3(sessionId, event.data.callId)
34555
+ );
34556
+ if (existing) {
34557
+ return existing;
34558
+ }
34559
+ const span = await this.startEveChildSpan(turn.span, {
34560
+ event: {
34561
+ id: eventId,
34562
+ metadata
34563
+ },
34564
+ name: event.data.subagentName,
34565
+ spanAttributes: { type: "tool" /* TOOL */ },
34566
+ spanId,
34567
+ startTime: eventTime2(event)
34568
+ });
34569
+ span.log({ metadata });
34570
+ const state = {
34571
+ metadata,
34572
+ span,
34573
+ turnKey: turnKey2(sessionId, event.data.turnId)
34574
+ };
34575
+ this.toolsByCallKey.set(toolKey3(sessionId, event.data.callId), state);
34576
+ return state;
34577
+ }
34578
+ async startSyntheticSubagentResult(event, ctx, result, hookMetadata) {
34579
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34580
+ const sessionId = sessionIdFromContext(ctx);
34581
+ if (!turn || !sessionId) {
34582
+ return void 0;
34583
+ }
34584
+ const metadata = toolMetadataFromTurn(turn);
34585
+ const { rowId: eventId, spanId } = await generateEveIds(
34586
+ "subagent",
34587
+ sessionId,
34588
+ result.callId
34589
+ );
34590
+ const existing = this.toolsByCallKey.get(toolKey3(sessionId, result.callId));
34591
+ if (existing) {
34592
+ return existing;
34593
+ }
34594
+ const span = await this.startEveChildSpan(turn.span, {
34595
+ event: {
34596
+ id: eventId,
34597
+ metadata
34598
+ },
34599
+ name: result.subagentName,
34600
+ spanAttributes: { type: "tool" /* TOOL */ },
34601
+ spanId,
34602
+ startTime: eventTime2(event)
34603
+ });
34604
+ span.log({ metadata });
34605
+ const state = {
34606
+ metadata,
34607
+ span,
34608
+ turnKey: turnKey2(sessionId, event.data.turnId)
34609
+ };
34610
+ this.toolsByCallKey.set(toolKey3(sessionId, result.callId), state);
34611
+ return state;
34612
+ }
34613
+ async startTurnSpan(sessionId, event, ctx, metadata) {
34614
+ const session = isObject(ctx) ? ctx["session"] : void 0;
34615
+ const parent = isObject(session) ? session["parent"] : void 0;
34616
+ const parentTurn = isObject(parent) ? parent["turn"] : void 0;
34617
+ const parentLineage = isObject(parent) && typeof parent["callId"] === "string" && typeof parent["sessionId"] === "string" && isObject(parentTurn) && typeof parentTurn["id"] === "string" ? {
34618
+ callId: parent["callId"],
34619
+ sessionId: parent["sessionId"],
34620
+ turnId: parentTurn["id"]
34621
+ } : void 0;
34622
+ const [{ rowId: eventId, spanId }, rootSpanId, parentSpanId] = await Promise.all([
34623
+ generateEveIds("turn", sessionId, event.data.turnId),
34624
+ deterministicEveId(
34625
+ "eve:root",
34626
+ parentLineage?.sessionId ?? sessionId,
34627
+ parentLineage?.turnId ?? event.data.turnId
34628
+ ),
34629
+ parentLineage ? deterministicEveId(
34630
+ "eve:subagent",
34631
+ parentLineage.sessionId,
34632
+ parentLineage.callId
34633
+ ) : Promise.resolve(void 0)
34634
+ ]);
34635
+ return await this.startEveSpan({
34636
+ event: {
34637
+ id: eventId,
34638
+ metadata
34639
+ },
34640
+ name: "eve.turn",
34641
+ parentSpanIds: parentSpanId ? { rootSpanId, spanId: parentSpanId } : { parentSpanIds: [], rootSpanId },
34642
+ spanAttributes: { type: "task" /* TASK */ },
34643
+ spanId,
34644
+ startTime: eventTime2(event)
34645
+ });
34646
+ }
34647
+ turnForEvent(event, ctx) {
34648
+ const sessionId = sessionIdFromContext(ctx);
34649
+ return sessionId ? this.turnsByKey.get(turnKey2(sessionId, event.data.turnId)) : void 0;
34650
+ }
34651
+ stepForEvent(event, ctx) {
34652
+ return this.turnForEvent(event, ctx)?.stepsByIndex.get(
34653
+ event.data.stepIndex
34654
+ );
34655
+ }
34656
+ finalizeTurn(turn, args) {
34657
+ const { endTime } = args;
34658
+ for (const step of turn.stepsByIndex.values()) {
34659
+ step.span.log({
34660
+ ...step.input !== void 0 ? { input: step.input } : {},
34661
+ metadata: step.metadata,
34662
+ metrics: step.metrics,
34663
+ output: step.output
34664
+ });
34665
+ step.span.end(endTime === void 0 ? void 0 : { endTime });
34666
+ }
34667
+ turn.stepsByIndex.clear();
34668
+ for (const tool of this.toolsByCallKey.values()) {
34669
+ if (tool.turnKey !== turn.key) {
34670
+ continue;
34671
+ }
34672
+ if (tool.endedByTurn) {
34673
+ continue;
34674
+ }
34675
+ tool.span.log({ metadata: tool.metadata });
34676
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34677
+ tool.endedByTurn = true;
34678
+ }
34679
+ if (args.error) {
34680
+ turn.span.log({ error: args.error });
34681
+ } else {
34682
+ turn.span.log({
34683
+ metadata: turn.metadata,
34684
+ metrics: turn.metrics,
34685
+ output: turn.output
34686
+ });
34687
+ }
34688
+ turn.span.end(endTime === void 0 ? void 0 : { endTime });
34689
+ this.turnsByKey.delete(turn.key);
34690
+ this.state.update((current) => {
34691
+ const normalized = normalizeEveTraceState(current);
34692
+ return {
34693
+ ...normalized,
34694
+ stepStarts: normalized.stepStarts.filter(
34695
+ (entry) => entry.turnId !== turn.turnId
34696
+ )
34697
+ };
34698
+ });
34699
+ }
34700
+ cleanupSession(sessionId) {
34701
+ const keyPrefix = `${sessionId}:`;
34702
+ for (const key of this.turnsByKey.keys()) {
34703
+ if (key.startsWith(keyPrefix)) {
34704
+ this.turnsByKey.delete(key);
34705
+ }
34706
+ }
34707
+ for (const key of this.toolsByCallKey.keys()) {
34708
+ if (key.startsWith(keyPrefix)) {
34709
+ this.toolsByCallKey.delete(key);
34710
+ }
34711
+ }
34712
+ for (const key of this.completedToolKeys.keys()) {
34713
+ if (key.startsWith(keyPrefix)) {
34714
+ this.completedToolKeys.delete(key);
34715
+ }
34716
+ }
34717
+ this.state.update(() => emptyEveTraceState());
34718
+ }
34719
+ async flushInstrumentation() {
34720
+ try {
34721
+ await flush();
34722
+ return true;
34723
+ } catch (error) {
34724
+ debugLogger.warn("Error in Eve flush instrumentation:", error);
34725
+ return false;
34726
+ }
34727
+ }
34728
+ };
34729
+ function emptyEveTraceState() {
34730
+ return {
34731
+ llmInputs: [],
34732
+ metadata: {},
34733
+ spanReferences: [],
34734
+ stepStarts: []
34735
+ };
34736
+ }
34737
+ function normalizeEveTraceState(state) {
34738
+ if (!isObject(state)) {
34739
+ return emptyEveTraceState();
34740
+ }
34741
+ const metadata = isObject(state["metadata"]) ? state["metadata"] : {};
34742
+ const spanReferences = Array.isArray(state["spanReferences"]) ? state["spanReferences"].flatMap((entry) => {
34743
+ if (!isObject(entry)) {
34744
+ return [];
34745
+ }
34746
+ const exported = entry["exported"];
34747
+ const endTime = entry["endTime"];
34748
+ const rootSpanId = entry["rootSpanId"];
34749
+ const rowId = entry["rowId"];
34750
+ const spanId = entry["spanId"];
34751
+ const startEvent = entry["startEvent"];
34752
+ const startEventCreated = isObject(startEvent) ? startEvent["created"] : void 0;
34753
+ const startEventMetrics = isObject(startEvent) ? startEvent["metrics"] : void 0;
34754
+ const startEventSpanAttributes = isObject(startEvent) ? startEvent["span_attributes"] : void 0;
34755
+ const startEventSpanParents = isObject(startEvent) ? startEvent["span_parents"] : void 0;
34756
+ const normalizedStartEvent = typeof startEventCreated === "string" && isObject(startEventMetrics) && typeof startEventMetrics["start"] === "number" && Number.isFinite(startEventMetrics["start"]) && isObject(startEventSpanAttributes) && Array.isArray(startEventSpanParents) && startEventSpanParents.every(
34757
+ (parent) => typeof parent === "string"
34758
+ ) ? {
34759
+ created: startEventCreated,
34760
+ metrics: { start: startEventMetrics["start"] },
34761
+ span_attributes: { ...startEventSpanAttributes },
34762
+ span_parents: [...startEventSpanParents]
34763
+ } : void 0;
34764
+ return typeof exported === "string" && typeof rootSpanId === "string" && typeof rowId === "string" && typeof spanId === "string" ? [
34765
+ {
34766
+ ...typeof endTime === "number" && Number.isFinite(endTime) ? { endTime } : {},
34767
+ exported,
34768
+ rootSpanId,
34769
+ rowId,
34770
+ spanId,
34771
+ ...normalizedStartEvent ? { startEvent: normalizedStartEvent } : {}
34772
+ }
34773
+ ] : [];
34774
+ }).slice(-MAX_STORED_SPAN_REFERENCES) : [];
34775
+ const llmInputs = Array.isArray(state["llmInputs"]) ? state["llmInputs"].flatMap((entry) => {
34776
+ if (!isObject(entry)) {
34777
+ return [];
34778
+ }
34779
+ const key = entry["key"];
34780
+ const input = entry["input"];
34781
+ return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
34782
+ }).slice(-MAX_STORED_LLM_INPUTS) : [];
34783
+ const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
34784
+ if (!isObject(entry)) {
34785
+ return [];
34786
+ }
34787
+ const ordinal = entry["ordinal"];
34788
+ const open = entry["open"];
34789
+ const stepIndex = entry["stepIndex"];
34790
+ const turnId = entry["turnId"];
34791
+ return typeof ordinal === "number" && Number.isInteger(ordinal) && ordinal >= 0 && typeof open === "boolean" && typeof stepIndex === "number" && Number.isInteger(stepIndex) && typeof turnId === "string" ? [{ open, ordinal, stepIndex, turnId }] : [];
34792
+ }).slice(-MAX_STORED_STEP_STARTS) : [];
34793
+ return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
34794
+ }
34795
+ function readEveTraceState(state) {
34796
+ try {
34797
+ return normalizeEveTraceState(state.get());
34798
+ } catch {
34799
+ return emptyEveTraceState();
34800
+ }
34801
+ }
34802
+ function captureEveModelInput(state, input) {
34803
+ if (!isObject(input)) {
34804
+ return;
34805
+ }
34806
+ const session = input["session"];
34807
+ const turn = input["turn"];
34808
+ const step = input["step"];
34809
+ if (!isObject(session) || !isObject(turn) || !isObject(step)) {
34810
+ return;
34811
+ }
34812
+ const sessionId = session["id"];
34813
+ const turnId = turn["id"];
34814
+ const stepIndex = step["index"];
34815
+ if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
34816
+ return;
34817
+ }
34818
+ const captured = capturedModelInput(input["modelInput"]);
34819
+ if (!captured) {
34820
+ return;
34821
+ }
34822
+ const key = llmInputKey(sessionId, turnId, stepIndex);
34823
+ state.update((current) => {
34824
+ const normalized = normalizeEveTraceState(current);
34825
+ const llmInputs = [...normalized.llmInputs, { input: captured, key }];
34826
+ return {
34827
+ ...normalized,
34828
+ llmInputs: llmInputs.slice(-MAX_STORED_LLM_INPUTS)
34829
+ };
34830
+ });
34831
+ }
34832
+ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
34833
+ try {
34834
+ const key = llmInputKey(sessionId, turnId, stepIndex);
34835
+ let input;
34836
+ state.update((current) => {
34837
+ const normalized = normalizeEveTraceState(current);
34838
+ const index = normalized.llmInputs.findIndex(
34839
+ (candidate) => candidate.key === key
34840
+ );
34841
+ if (index < 0) {
34842
+ return normalized;
34843
+ }
34844
+ input = normalized.llmInputs[index]?.input;
34845
+ return {
34846
+ ...normalized,
34847
+ llmInputs: normalized.llmInputs.filter(
34848
+ (_, candidateIndex) => candidateIndex !== index
34849
+ )
34850
+ };
34851
+ });
34852
+ return input;
34853
+ } catch (error) {
34854
+ debugLogger.warn("Error in Eve LLM input consumption:", error);
34855
+ return void 0;
34856
+ }
34857
+ }
34858
+ function capturedModelInput(modelInput) {
34859
+ if (!isObject(modelInput)) {
34860
+ return void 0;
34861
+ }
34862
+ const messages = modelInput["messages"];
34863
+ if (!Array.isArray(messages)) {
34864
+ return void 0;
34865
+ }
34866
+ const instructions = modelInput["instructions"];
34867
+ const value = [
34868
+ ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
34869
+ ...messages
34870
+ ];
34871
+ try {
34872
+ const cloned = JSON.parse(JSON.stringify(value));
34873
+ if (!Array.isArray(cloned)) {
34874
+ return void 0;
34875
+ }
34876
+ return cloned;
34877
+ } catch {
34878
+ return void 0;
34879
+ }
34880
+ }
34881
+ function isCapturedModelInput(input) {
34882
+ return Array.isArray(input);
34883
+ }
34884
+ function llmInputKey(sessionId, turnId, stepIndex) {
34885
+ return `${sessionId}\0${turnId}\0${stepIndex}`;
34886
+ }
34887
+ function modelMetadataFromRuntime(runtime) {
34888
+ if (!isObject(runtime)) {
34889
+ return {};
34890
+ }
34891
+ const modelId = runtime["modelId"];
34892
+ return typeof modelId === "string" && !modelId.trim().startsWith("dynamic:") ? modelMetadataFromModelId(modelId) : {};
34893
+ }
34894
+ function modelMetadataFromModelId(modelId) {
34895
+ const normalized = modelId.trim();
34896
+ if (!normalized) {
34897
+ return {};
34898
+ }
34899
+ const slashIndex = normalized.indexOf("/");
34900
+ if (slashIndex > 0 && slashIndex < normalized.length - 1) {
34901
+ return {
34902
+ model: normalized.slice(slashIndex + 1),
34903
+ provider: normalized.slice(0, slashIndex)
34904
+ };
34905
+ }
34906
+ return {
34907
+ model: normalized
34908
+ };
34909
+ }
34910
+ function sessionIdFromContext(ctx) {
34911
+ if (!isObject(ctx)) {
34912
+ return void 0;
34913
+ }
34914
+ const session = ctx["session"];
34915
+ if (!isObject(session)) {
34916
+ return void 0;
34917
+ }
34918
+ const id = session["id"];
34919
+ return typeof id === "string" ? id : void 0;
34920
+ }
34921
+ function toolMetadataFromTurn(turn) {
34922
+ const { model: _model, provider: _provider, ...metadata } = turn.metadata;
34923
+ return metadata;
34924
+ }
34925
+ function isToolCallAction(action) {
34926
+ return isObject(action) && action["kind"] === "tool-call" && typeof action["callId"] === "string" && typeof action["toolName"] === "string" && isObject(action["input"]);
34927
+ }
34928
+ function isLocalSubagentCallAction(action) {
34929
+ return isObject(action) && action["kind"] === "subagent-call" && typeof action["callId"] === "string" && isObject(action["input"]);
34930
+ }
34931
+ function isTraceableActionRequest(action) {
34932
+ return isToolCallAction(action) || isLocalSubagentCallAction(action);
34933
+ }
34934
+ function isToolResult(result) {
34935
+ return isObject(result) && result["kind"] === "tool-result" && typeof result["callId"] === "string" && typeof result["toolName"] === "string";
34936
+ }
34937
+ function isSubagentResult(result) {
34938
+ return isObject(result) && result["kind"] === "subagent-result" && typeof result["callId"] === "string" && typeof result["subagentName"] === "string";
34939
+ }
34940
+ function normalizedFinishReason(finishReason) {
34941
+ switch (finishReason) {
34942
+ case "content-filter":
34943
+ return "content_filter";
34944
+ case "tool-calls":
34945
+ return "tool_calls";
34946
+ default:
34947
+ return finishReason;
34948
+ }
34949
+ }
34950
+ function errorFromMessage(message, code, details) {
34951
+ const error = new Error(`${code}: ${message}`);
34952
+ if (details !== void 0) {
34953
+ error.cause = details;
34954
+ }
34955
+ return error;
34956
+ }
34957
+ function actionResultError(error, output) {
34958
+ if (error) {
34959
+ return errorFromMessage(error.message, error.code);
34960
+ }
34961
+ const result = new Error("Eve action failed");
34962
+ if (output !== void 0) {
34963
+ result.cause = output;
34964
+ }
34965
+ return result;
34966
+ }
34967
+ function eventTime2(event) {
34968
+ if (!event.meta?.at) {
34969
+ return void 0;
34970
+ }
34971
+ const timestamp = Date.parse(event.meta.at);
34972
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
34973
+ }
34974
+ function turnKey2(sessionId, turnId) {
34975
+ return `${sessionId}:${turnId}`;
34976
+ }
34977
+ function toolKey3(sessionId, callId) {
34978
+ return `${sessionId}:${callId}`;
34979
+ }
34980
+ async function generateEveIds(kind, ...parts) {
34981
+ const [rowId, spanId] = await Promise.all([
34982
+ deterministicEveId(`eve:row:${kind}`, ...parts),
34983
+ deterministicEveId(`eve:${kind}`, ...parts)
34984
+ ]);
34985
+ return { rowId, spanId };
34986
+ }
34987
+ async function deterministicEveId(...parts) {
34988
+ const data = new TextEncoder().encode(
34989
+ parts.map((part) => `${part.length}:${part}`).join("\0")
34990
+ );
34991
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", data);
34992
+ const bytes = Array.from(new Uint8Array(digest, 0, 16));
34993
+ const hex = bytes.map((byte) => byte.toString(16).padStart(2, "0")).join("");
34994
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
34995
+ }
34996
+
32857
34997
  // src/typed-instrumentation-helpers.ts
32858
34998
  var TypedApplyProxy = Proxy;
32859
34999
 
@@ -33016,6 +35156,15 @@ function modelMetrics(attributes) {
33016
35156
  }
33017
35157
  return Object.keys(out).length > 0 ? out : void 0;
33018
35158
  }
35159
+ function timeToFirstTokenSeconds(attributes, spanStartSeconds) {
35160
+ if (!isObject(attributes)) return void 0;
35161
+ if (spanStartSeconds === void 0) return void 0;
35162
+ const raw = attributes.completionStartTime;
35163
+ const completionStart = raw instanceof Date || typeof raw === "string" || typeof raw === "number" ? epochSeconds(raw) : void 0;
35164
+ if (completionStart === void 0) return void 0;
35165
+ const ttft = completionStart - spanStartSeconds;
35166
+ return Number.isFinite(ttft) && ttft >= 0 ? ttft : void 0;
35167
+ }
33019
35168
  function buildMetadata(exported) {
33020
35169
  const out = {};
33021
35170
  if (exported.entityId !== void 0) out.entity_id = exported.entityId;
@@ -33085,7 +35234,13 @@ var BraintrustObservabilityExporter = class {
33085
35234
  const args = {
33086
35235
  name: exported.name,
33087
35236
  spanAttributes: { type: spanTypeFor(exported.type) },
33088
- startTime: epochSeconds(exported.startTime)
35237
+ startTime: epochSeconds(exported.startTime),
35238
+ // Use the Mastra span id as the Braintrust row id so that
35239
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35240
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
35241
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35242
+ // external caller could know.
35243
+ event: { id: exported.id }
33089
35244
  };
33090
35245
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
33091
35246
  if (!this.capturedParent) {
@@ -33134,8 +35289,15 @@ var BraintrustObservabilityExporter = class {
33134
35289
  event.metadata = metadata;
33135
35290
  }
33136
35291
  const metrics = modelMetrics(exported.attributes);
33137
- if (metrics) {
33138
- event.metrics = metrics;
35292
+ const ttft = timeToFirstTokenSeconds(
35293
+ exported.attributes,
35294
+ epochSeconds(exported.startTime)
35295
+ );
35296
+ if (metrics || ttft !== void 0) {
35297
+ event.metrics = {
35298
+ ...metrics ?? {},
35299
+ ...ttft !== void 0 ? { time_to_first_token: ttft } : {}
35300
+ };
33139
35301
  }
33140
35302
  if (Object.keys(event).length > 0) {
33141
35303
  record.span.log(event);
@@ -34014,17 +36176,17 @@ function wrapGenkit(genkit) {
34014
36176
  console.warn("Unsupported Genkit object. Not wrapping.");
34015
36177
  return genkit;
34016
36178
  }
34017
- function isRecord2(value) {
36179
+ function isRecord3(value) {
34018
36180
  return typeof value === "object" && value !== null;
34019
36181
  }
34020
36182
  function isPropertyBag(value) {
34021
- return isRecord2(value) || typeof value === "function";
36183
+ return isRecord3(value) || typeof value === "function";
34022
36184
  }
34023
36185
  function hasFunction(value, methodName) {
34024
36186
  return isPropertyBag(value) && methodName in value && typeof value[methodName] === "function";
34025
36187
  }
34026
36188
  function isGenkitInstance(value) {
34027
- return isRecord2(value) && (hasFunction(value, "generate") || hasFunction(value, "generateStream") || hasFunction(value, "defineFlow") || hasFunction(value, "defineTool"));
36189
+ return isRecord3(value) && (hasFunction(value, "generate") || hasFunction(value, "generateStream") || hasFunction(value, "defineFlow") || hasFunction(value, "defineTool"));
34028
36190
  }
34029
36191
  function isGenkitModule(value) {
34030
36192
  return hasFunction(value, "genkit");
@@ -34078,7 +36240,7 @@ function patchGenkitRegistry(instance) {
34078
36240
  patchGenkitRegistryConstructor(registry2);
34079
36241
  }
34080
36242
  function patchGenkitRegistryLookup(registry2) {
34081
- if (!isRecord2(registry2) || hasRegistryPatchedFlag(registry2) || !hasFunction(registry2, "lookupAction")) {
36243
+ if (!isRecord3(registry2) || hasRegistryPatchedFlag(registry2) || !hasFunction(registry2, "lookupAction")) {
34082
36244
  return;
34083
36245
  }
34084
36246
  const originalLookupAction = registry2.lookupAction;
@@ -34104,7 +36266,7 @@ function patchGenkitRegistryLookup(registry2) {
34104
36266
  }
34105
36267
  }
34106
36268
  function patchGenkitRegistryConstructor(registry2) {
34107
- if (!isRecord2(registry2)) {
36269
+ if (!isRecord3(registry2)) {
34108
36270
  return;
34109
36271
  }
34110
36272
  const constructor = registry2.constructor;
@@ -34120,7 +36282,7 @@ function patchGenkitRegistryConstructor(registry2) {
34120
36282
  configurable: true,
34121
36283
  value: (...args) => {
34122
36284
  const childRegistry = originalWithParent.apply(constructor, args);
34123
- if (args.some((arg) => isRecord2(arg) && hasRegistryPatchedFlag(arg))) {
36285
+ if (args.some((arg) => isRecord3(arg) && hasRegistryPatchedFlag(arg))) {
34124
36286
  patchGenkitRegistryLookup(childRegistry);
34125
36287
  patchGenkitRegistryConstructor(childRegistry);
34126
36288
  }
@@ -34219,7 +36381,7 @@ function hasRegistryConstructorPatchedFlag(value) {
34219
36381
  );
34220
36382
  }
34221
36383
  function isPromiseLike2(value) {
34222
- return isRecord2(value) && "then" in value && typeof value.then === "function";
36384
+ return isRecord3(value) && "then" in value && typeof value.then === "function";
34223
36385
  }
34224
36386
 
34225
36387
  // src/wrappers/huggingface.ts
@@ -34582,14 +36744,14 @@ function wrapMistral(mistral) {
34582
36744
  console.warn("Unsupported Mistral library. Not wrapping.");
34583
36745
  return mistral;
34584
36746
  }
34585
- function isRecord3(value) {
36747
+ function isRecord4(value) {
34586
36748
  return typeof value === "object" && value !== null;
34587
36749
  }
34588
36750
  function hasFunction3(value, methodName) {
34589
- return isRecord3(value) && methodName in value && typeof value[methodName] === "function";
36751
+ return isRecord4(value) && methodName in value && typeof value[methodName] === "function";
34590
36752
  }
34591
36753
  function isSupportedMistralClient(value) {
34592
- if (!isRecord3(value)) {
36754
+ if (!isRecord4(value)) {
34593
36755
  return false;
34594
36756
  }
34595
36757
  return value.chat !== void 0 && hasChat(value.chat) || value.embeddings !== void 0 && hasEmbeddings(value.embeddings) || value.fim !== void 0 && hasFim(value.fim) || value.agents !== void 0 && hasAgents(value.agents) || value.classifiers !== void 0 && hasClassifiers(value.classifiers);
@@ -34773,14 +36935,14 @@ function wrapCohere(cohere) {
34773
36935
  return cohere;
34774
36936
  }
34775
36937
  var cohereProxyCache = /* @__PURE__ */ new WeakMap();
34776
- function isRecord4(value) {
36938
+ function isRecord5(value) {
34777
36939
  return typeof value === "object" && value !== null;
34778
36940
  }
34779
36941
  function hasFunction4(value, methodName) {
34780
- return isRecord4(value) && methodName in value && typeof value[methodName] === "function";
36942
+ return isRecord5(value) && methodName in value && typeof value[methodName] === "function";
34781
36943
  }
34782
36944
  function isSupportedCohereClient(value) {
34783
- if (!isRecord4(value)) {
36945
+ if (!isRecord5(value)) {
34784
36946
  return false;
34785
36947
  }
34786
36948
  return hasFunction4(value, "chat") || hasFunction4(value, "chatStream") || hasFunction4(value, "embed") || hasFunction4(value, "rerank");
@@ -34840,20 +37002,20 @@ function wrapGroq(groq) {
34840
37002
  console.warn("Unsupported Groq library. Not wrapping.");
34841
37003
  return groq;
34842
37004
  }
34843
- function isRecord5(value) {
37005
+ function isRecord6(value) {
34844
37006
  return typeof value === "object" && value !== null;
34845
37007
  }
34846
37008
  function hasFunction5(value, methodName) {
34847
- return isRecord5(value) && methodName in value && typeof value[methodName] === "function";
37009
+ return isRecord6(value) && methodName in value && typeof value[methodName] === "function";
34848
37010
  }
34849
37011
  function hasChat2(value) {
34850
- return isRecord5(value) && isRecord5(value.completions) && hasFunction5(value.completions, "create");
37012
+ return isRecord6(value) && isRecord6(value.completions) && hasFunction5(value.completions, "create");
34851
37013
  }
34852
37014
  function hasEmbeddings2(value) {
34853
37015
  return hasFunction5(value, "create");
34854
37016
  }
34855
37017
  function isSupportedGroqClient(value) {
34856
- return isRecord5(value) && (value.chat !== void 0 && hasChat2(value.chat) || value.embeddings !== void 0 && hasEmbeddings2(value.embeddings));
37018
+ return isRecord6(value) && (value.chat !== void 0 && hasChat2(value.chat) || value.embeddings !== void 0 && hasEmbeddings2(value.embeddings));
34857
37019
  }
34858
37020
  function groqProxy(groq) {
34859
37021
  const privateMethodWorkaroundCache = /* @__PURE__ */ new WeakMap();
@@ -34936,11 +37098,11 @@ var BEDROCK_RUNTIME_OPERATION_METHODS = /* @__PURE__ */ new Set([
34936
37098
  "invokeModelWithBidirectionalStream",
34937
37099
  "invokeModelWithResponseStream"
34938
37100
  ]);
34939
- function isRecord6(value) {
37101
+ function isRecord7(value) {
34940
37102
  return typeof value === "object" && value !== null;
34941
37103
  }
34942
37104
  function isSupportedBedrockRuntimeClient(value) {
34943
- return isRecord6(value) && typeof value.send === "function";
37105
+ return isRecord7(value) && typeof value.send === "function";
34944
37106
  }
34945
37107
  function bedrockRuntimeProxy(client) {
34946
37108
  const cached = bedrockRuntimeProxyCache.get(client);
@@ -35059,6 +37221,271 @@ function wrappedResumeSession(client) {
35059
37221
  );
35060
37222
  }
35061
37223
 
37224
+ // src/wrappers/langsmith.ts
37225
+ var WRAPPED_CLIENT_CLASS = /* @__PURE__ */ Symbol.for(
37226
+ "braintrust.langsmith.wrapped-client-class"
37227
+ );
37228
+ var WRAPPED_CLIENT_INSTANCE = /* @__PURE__ */ Symbol.for(
37229
+ "braintrust.langsmith.wrapped-client-instance"
37230
+ );
37231
+ var WRAPPED_CLIENT_NAMESPACE = /* @__PURE__ */ Symbol.for(
37232
+ "braintrust.langsmith.wrapped-client-namespace"
37233
+ );
37234
+ var WRAPPED_RUN_TREE_CLASS = /* @__PURE__ */ Symbol.for(
37235
+ "braintrust.langsmith.wrapped-run-tree-class"
37236
+ );
37237
+ var WRAPPED_RUN_TREE_INSTANCE = /* @__PURE__ */ Symbol.for(
37238
+ "braintrust.langsmith.wrapped-run-tree-instance"
37239
+ );
37240
+ var WRAPPED_RUN_TREES_NAMESPACE = /* @__PURE__ */ Symbol.for(
37241
+ "braintrust.langsmith.wrapped-run-trees-namespace"
37242
+ );
37243
+ var WRAPPED_TRACEABLE = /* @__PURE__ */ Symbol.for("braintrust.langsmith.wrapped-traceable");
37244
+ var WRAPPED_TRACEABLE_NAMESPACE = /* @__PURE__ */ Symbol.for(
37245
+ "braintrust.langsmith.wrapped-traceable-namespace"
37246
+ );
37247
+ function wrapLangSmithTraceable(namespace) {
37248
+ return wrapNamespaceExport(
37249
+ namespace,
37250
+ "traceable",
37251
+ WRAPPED_TRACEABLE_NAMESPACE,
37252
+ (value) => wrapTraceable(value)
37253
+ );
37254
+ }
37255
+ function wrapLangSmithRunTrees(namespace) {
37256
+ return wrapNamespaceExport(
37257
+ namespace,
37258
+ "RunTree",
37259
+ WRAPPED_RUN_TREES_NAMESPACE,
37260
+ (value) => wrapRunTreeClass(value)
37261
+ );
37262
+ }
37263
+ function wrapLangSmithClient(namespace) {
37264
+ return wrapNamespaceExport(
37265
+ namespace,
37266
+ "Client",
37267
+ WRAPPED_CLIENT_NAMESPACE,
37268
+ (value) => wrapClientClass(value)
37269
+ );
37270
+ }
37271
+ function wrapNamespaceExport(namespace, exportName, marker, wrap2) {
37272
+ if (!namespace || typeof namespace !== "object") {
37273
+ return namespace;
37274
+ }
37275
+ const candidate = namespace;
37276
+ if (candidate[marker] === true) {
37277
+ return namespace;
37278
+ }
37279
+ if (typeof candidate[exportName] !== "function") {
37280
+ console.warn(
37281
+ `Unsupported LangSmith ${exportName} namespace. Not wrapping.`
37282
+ );
37283
+ return namespace;
37284
+ }
37285
+ const target = isModuleNamespace5(namespace) ? Object.setPrototypeOf({}, namespace) : candidate;
37286
+ const moduleNamespace = target !== candidate;
37287
+ let wrappedExport;
37288
+ return new Proxy(target, {
37289
+ get(target2, prop, receiver) {
37290
+ if (prop === marker) {
37291
+ return true;
37292
+ }
37293
+ const value = Reflect.get(target2, prop, receiver);
37294
+ if (prop !== exportName || typeof value !== "function") {
37295
+ return value;
37296
+ }
37297
+ wrappedExport ??= wrap2(value);
37298
+ return wrappedExport;
37299
+ },
37300
+ getOwnPropertyDescriptor(target2, prop) {
37301
+ const descriptor = Reflect.getOwnPropertyDescriptor(target2, prop);
37302
+ if (descriptor || !moduleNamespace) {
37303
+ return descriptor;
37304
+ }
37305
+ const namespaceDescriptor = Reflect.getOwnPropertyDescriptor(
37306
+ candidate,
37307
+ prop
37308
+ );
37309
+ return namespaceDescriptor ? { ...namespaceDescriptor, configurable: true } : void 0;
37310
+ },
37311
+ has(target2, prop) {
37312
+ return Reflect.has(target2, prop) || moduleNamespace && prop in candidate;
37313
+ },
37314
+ ownKeys(target2) {
37315
+ return moduleNamespace ? Reflect.ownKeys(candidate) : Reflect.ownKeys(target2);
37316
+ }
37317
+ });
37318
+ }
37319
+ function isModuleNamespace5(value) {
37320
+ if (!value || typeof value !== "object") {
37321
+ return false;
37322
+ }
37323
+ if (value.constructor?.name === "Module") {
37324
+ return true;
37325
+ }
37326
+ const firstKey = Object.keys(value)[0];
37327
+ if (!firstKey) {
37328
+ return false;
37329
+ }
37330
+ const descriptor = Object.getOwnPropertyDescriptor(value, firstKey);
37331
+ return descriptor ? !descriptor.configurable && !descriptor.writable : false;
37332
+ }
37333
+ function wrapTraceable(traceable2) {
37334
+ if (traceable2[WRAPPED_TRACEABLE]) {
37335
+ return traceable2;
37336
+ }
37337
+ return new Proxy(traceable2, {
37338
+ get(target, prop, receiver) {
37339
+ if (prop === WRAPPED_TRACEABLE) {
37340
+ return true;
37341
+ }
37342
+ return Reflect.get(target, prop, receiver);
37343
+ },
37344
+ apply(target, thisArg, argArray) {
37345
+ const [fn, rawConfig] = argArray;
37346
+ const config = rawConfig && typeof rawConfig === "object" ? rawConfig : void 0;
37347
+ const originalOnEnd = config?.on_end;
37348
+ const wrappedConfig = {
37349
+ ...config,
37350
+ on_end(runTree) {
37351
+ publishRunUpdate(runTree);
37352
+ if (originalOnEnd) {
37353
+ Reflect.apply(originalOnEnd, config, [runTree]);
37354
+ }
37355
+ }
37356
+ };
37357
+ return Reflect.apply(target, thisArg, [fn, wrappedConfig]);
37358
+ }
37359
+ });
37360
+ }
37361
+ function wrapRunTreeClass(RunTree) {
37362
+ if (RunTree[WRAPPED_RUN_TREE_CLASS]) {
37363
+ return RunTree;
37364
+ }
37365
+ return new Proxy(RunTree, {
37366
+ get(target, prop, receiver) {
37367
+ if (prop === WRAPPED_RUN_TREE_CLASS) {
37368
+ return true;
37369
+ }
37370
+ const value = Reflect.get(target, prop, receiver);
37371
+ return typeof value === "function" ? value.bind(target) : value;
37372
+ },
37373
+ construct(target, args, newTarget) {
37374
+ return wrapRunTreeInstance(Reflect.construct(target, args, newTarget));
37375
+ }
37376
+ });
37377
+ }
37378
+ function wrapRunTreeInstance(runTree) {
37379
+ if (runTree[WRAPPED_RUN_TREE_INSTANCE]) {
37380
+ return runTree;
37381
+ }
37382
+ return new Proxy(runTree, {
37383
+ get(target, prop, receiver) {
37384
+ if (prop === WRAPPED_RUN_TREE_INSTANCE) {
37385
+ return true;
37386
+ }
37387
+ const value = Reflect.get(target, prop, receiver);
37388
+ if (typeof value !== "function") {
37389
+ return value;
37390
+ }
37391
+ let wrapped;
37392
+ if (prop === "createChild") {
37393
+ wrapped = (...args) => wrapRunTreeInstance(Reflect.apply(value, target, args));
37394
+ } else if (prop === "postRun") {
37395
+ const method = value;
37396
+ wrapped = (...args) => langSmithChannels.createRun.tracePromise(
37397
+ () => Reflect.apply(method, target, args),
37398
+ { arguments: [target] }
37399
+ );
37400
+ } else if (prop === "patchRun") {
37401
+ const method = value;
37402
+ wrapped = (...args) => langSmithChannels.updateRun.tracePromise(
37403
+ () => Reflect.apply(method, target, args),
37404
+ {
37405
+ arguments: [
37406
+ typeof target.id === "string" ? target.id : "",
37407
+ target
37408
+ ]
37409
+ }
37410
+ );
37411
+ } else {
37412
+ wrapped = value.bind(target);
37413
+ }
37414
+ return wrapped;
37415
+ }
37416
+ });
37417
+ }
37418
+ function wrapClientClass(Client) {
37419
+ if (Client[WRAPPED_CLIENT_CLASS]) {
37420
+ return Client;
37421
+ }
37422
+ return new Proxy(Client, {
37423
+ get(target, prop, receiver) {
37424
+ if (prop === WRAPPED_CLIENT_CLASS) {
37425
+ return true;
37426
+ }
37427
+ const value = Reflect.get(target, prop, receiver);
37428
+ return typeof value === "function" ? value.bind(target) : value;
37429
+ },
37430
+ construct(target, args, newTarget) {
37431
+ return wrapClientInstance(Reflect.construct(target, args, newTarget));
37432
+ }
37433
+ });
37434
+ }
37435
+ function wrapClientInstance(client) {
37436
+ if (client[WRAPPED_CLIENT_INSTANCE]) {
37437
+ return client;
37438
+ }
37439
+ return new Proxy(client, {
37440
+ get(target, prop, receiver) {
37441
+ if (prop === WRAPPED_CLIENT_INSTANCE) {
37442
+ return true;
37443
+ }
37444
+ const value = Reflect.get(target, prop, receiver);
37445
+ if (typeof value !== "function") {
37446
+ return value;
37447
+ }
37448
+ let wrapped;
37449
+ if (prop === "createRun") {
37450
+ const method = value;
37451
+ wrapped = (...args) => langSmithChannels.createRun.tracePromise(
37452
+ () => Reflect.apply(method, target, args),
37453
+ { arguments: args }
37454
+ );
37455
+ } else if (prop === "updateRun") {
37456
+ const method = value;
37457
+ wrapped = (...args) => langSmithChannels.updateRun.tracePromise(
37458
+ () => Reflect.apply(method, target, args),
37459
+ { arguments: args }
37460
+ );
37461
+ } else if (prop === "batchIngestRuns") {
37462
+ const method = value;
37463
+ wrapped = (...args) => langSmithChannels.batchIngestRuns.tracePromise(
37464
+ () => Reflect.apply(method, target, args),
37465
+ { arguments: args }
37466
+ );
37467
+ } else {
37468
+ wrapped = value.bind(target);
37469
+ }
37470
+ return wrapped;
37471
+ }
37472
+ });
37473
+ }
37474
+ function publishRunUpdate(runTree) {
37475
+ if (!runTree || typeof runTree.id !== "string" || !runTree.id) {
37476
+ return;
37477
+ }
37478
+ try {
37479
+ void langSmithChannels.updateRun.tracePromise(() => Promise.resolve(void 0), {
37480
+ arguments: [runTree.id, runTree]
37481
+ }).catch((error) => {
37482
+ debugLogger.error("LangSmith traceable instrumentation failed:", error);
37483
+ });
37484
+ } catch (error) {
37485
+ debugLogger.error("LangSmith traceable instrumentation failed:", error);
37486
+ }
37487
+ }
37488
+
35062
37489
  // src/wrappers/vitest/context-manager.ts
35063
37490
  var VitestContextManager = class {
35064
37491
  /**
@@ -39420,6 +41847,8 @@ export {
39420
41847
  addAzureBlobHeaders,
39421
41848
  agentAssertionScorer,
39422
41849
  braintrustAISDKTelemetry,
41850
+ braintrustEveHook,
41851
+ braintrustEveInstrumentation,
39423
41852
  braintrustFlueInstrumentation,
39424
41853
  braintrustFlueObserver,
39425
41854
  braintrustStreamChunkSchema,
@@ -39510,6 +41939,9 @@ export {
39510
41939
  wrapGoogleGenAI,
39511
41940
  wrapGroq,
39512
41941
  wrapHuggingFace,
41942
+ wrapLangSmithClient,
41943
+ wrapLangSmithRunTrees,
41944
+ wrapLangSmithTraceable,
39513
41945
  wrapMastraAgent,
39514
41946
  wrapMistral,
39515
41947
  wrapOpenAI,