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
@@ -93,6 +93,8 @@ __export(edge_light_exports, {
93
93
  addAzureBlobHeaders: () => addAzureBlobHeaders,
94
94
  agentAssertionScorer: () => agentAssertionScorer,
95
95
  braintrustAISDKTelemetry: () => braintrustAISDKTelemetry,
96
+ braintrustEveHook: () => braintrustEveHook,
97
+ braintrustEveInstrumentation: () => braintrustEveInstrumentation,
96
98
  braintrustFlueInstrumentation: () => braintrustFlueInstrumentation,
97
99
  braintrustFlueObserver: () => braintrustFlueObserver,
98
100
  braintrustStreamChunkSchema: () => braintrustStreamChunkSchema,
@@ -183,6 +185,9 @@ __export(edge_light_exports, {
183
185
  wrapGoogleGenAI: () => wrapGoogleGenAI,
184
186
  wrapGroq: () => wrapGroq,
185
187
  wrapHuggingFace: () => wrapHuggingFace,
188
+ wrapLangSmithClient: () => wrapLangSmithClient,
189
+ wrapLangSmithRunTrees: () => wrapLangSmithRunTrees,
190
+ wrapLangSmithTraceable: () => wrapLangSmithTraceable,
186
191
  wrapMastraAgent: () => wrapMastraAgent,
187
192
  wrapMistral: () => wrapMistral,
188
193
  wrapOpenAI: () => wrapOpenAI,
@@ -2384,19 +2389,30 @@ var CallEvent = import_v36.z.union([
2384
2389
  var ChatCompletionContentPartTextWithTitle = import_v36.z.object({
2385
2390
  text: import_v36.z.string().default(""),
2386
2391
  type: import_v36.z.literal("text"),
2387
- cache_control: import_v36.z.object({ type: import_v36.z.literal("ephemeral") }).optional()
2392
+ cache_control: import_v36.z.object({
2393
+ type: import_v36.z.literal("ephemeral"),
2394
+ ttl: import_v36.z.enum(["5m", "1h"]).optional()
2395
+ }).optional()
2388
2396
  });
2389
2397
  var ChatCompletionContentPartImageWithTitle = import_v36.z.object({
2390
2398
  image_url: import_v36.z.object({
2391
2399
  url: import_v36.z.string(),
2392
2400
  detail: import_v36.z.union([import_v36.z.literal("auto"), import_v36.z.literal("low"), import_v36.z.literal("high")]).optional()
2393
2401
  }),
2394
- type: import_v36.z.literal("image_url")
2402
+ type: import_v36.z.literal("image_url"),
2403
+ cache_control: import_v36.z.object({
2404
+ type: import_v36.z.literal("ephemeral"),
2405
+ ttl: import_v36.z.enum(["5m", "1h"]).optional()
2406
+ }).optional()
2395
2407
  });
2396
2408
  var ChatCompletionContentPartFileFile = import_v36.z.object({ file_data: import_v36.z.string(), filename: import_v36.z.string(), file_id: import_v36.z.string() }).partial();
2397
2409
  var ChatCompletionContentPartFileWithTitle = import_v36.z.object({
2398
2410
  file: ChatCompletionContentPartFileFile,
2399
- type: import_v36.z.literal("file")
2411
+ type: import_v36.z.literal("file"),
2412
+ cache_control: import_v36.z.object({
2413
+ type: import_v36.z.literal("ephemeral"),
2414
+ ttl: import_v36.z.enum(["5m", "1h"]).optional()
2415
+ }).optional()
2400
2416
  });
2401
2417
  var ChatCompletionContentPart = import_v36.z.union([
2402
2418
  ChatCompletionContentPartTextWithTitle,
@@ -2406,7 +2422,10 @@ var ChatCompletionContentPart = import_v36.z.union([
2406
2422
  var ChatCompletionContentPartText = import_v36.z.object({
2407
2423
  text: import_v36.z.string().default(""),
2408
2424
  type: import_v36.z.literal("text"),
2409
- cache_control: import_v36.z.object({ type: import_v36.z.literal("ephemeral") }).optional()
2425
+ cache_control: import_v36.z.object({
2426
+ type: import_v36.z.literal("ephemeral"),
2427
+ ttl: import_v36.z.enum(["5m", "1h"]).optional()
2428
+ }).optional()
2410
2429
  });
2411
2430
  var ChatCompletionMessageToolCall = import_v36.z.object({
2412
2431
  id: import_v36.z.string(),
@@ -4441,7 +4460,7 @@ var DiskCache = class {
4441
4460
  }
4442
4461
  };
4443
4462
 
4444
- // src/prompt-cache/lru-cache.ts
4463
+ // src/lru-cache.ts
4445
4464
  var LRUCache = class {
4446
4465
  cache;
4447
4466
  maxSize;
@@ -4457,13 +4476,23 @@ var LRUCache = class {
4457
4476
  * @returns The cached value if found, undefined otherwise.
4458
4477
  */
4459
4478
  get(key) {
4460
- const value = this.cache.get(key);
4461
- if (value === void 0) {
4479
+ const entry = this.cache.get(key);
4480
+ if (entry === void 0) {
4462
4481
  return void 0;
4463
4482
  }
4464
4483
  this.cache.delete(key);
4465
- this.cache.set(key, value);
4466
- return value;
4484
+ this.cache.set(key, entry);
4485
+ return entry.value;
4486
+ }
4487
+ /**
4488
+ * Checks whether a key exists and marks it as most recently used.
4489
+ */
4490
+ has(key) {
4491
+ if (!this.cache.has(key)) {
4492
+ return false;
4493
+ }
4494
+ this.get(key);
4495
+ return true;
4467
4496
  }
4468
4497
  /**
4469
4498
  * Stores a value in the cache.
@@ -4480,7 +4509,38 @@ var LRUCache = class {
4480
4509
  const first = this.cache.keys().next().value;
4481
4510
  this.cache.delete(first);
4482
4511
  }
4483
- this.cache.set(key, value);
4512
+ this.cache.set(key, { value });
4513
+ }
4514
+ /**
4515
+ * Removes an item from the cache.
4516
+ */
4517
+ delete(key) {
4518
+ return this.cache.delete(key);
4519
+ }
4520
+ /**
4521
+ * Iterates over cache entries from least to most recently used.
4522
+ */
4523
+ *entries() {
4524
+ for (const [key, entry] of this.cache) {
4525
+ yield [key, entry.value];
4526
+ }
4527
+ }
4528
+ /**
4529
+ * Iterates over cache keys from least to most recently used.
4530
+ */
4531
+ keys() {
4532
+ return this.cache.keys();
4533
+ }
4534
+ /**
4535
+ * Iterates over cache values from least to most recently used.
4536
+ */
4537
+ *values() {
4538
+ for (const entry of this.cache.values()) {
4539
+ yield entry.value;
4540
+ }
4541
+ }
4542
+ [Symbol.iterator]() {
4543
+ return this.entries();
4484
4544
  }
4485
4545
  /**
4486
4546
  * Removes all items from the cache.
@@ -5038,6 +5098,9 @@ function applyMaskingToField(maskingFunction, data, fieldName) {
5038
5098
  return `ERROR: Failed to mask field '${fieldName}' - ${errorType}`;
5039
5099
  }
5040
5100
  }
5101
+ var INITIAL_SPAN_WRITE_AS_MERGE = /* @__PURE__ */ Symbol(
5102
+ "braintrust.initial-span-write-as-merge"
5103
+ );
5041
5104
  var BRAINTRUST_CURRENT_SPAN_STORE = /* @__PURE__ */ Symbol.for(
5042
5105
  "braintrust.currentSpanStore"
5043
5106
  );
@@ -5639,25 +5702,46 @@ var HTTPConnection = class _HTTPConnection {
5639
5702
  })
5640
5703
  );
5641
5704
  }
5642
- async post(path, params, config) {
5705
+ async post(path, params, config, retries = 0) {
5643
5706
  const { headers, ...rest } = config || {};
5644
5707
  const this_fetch = this.fetch;
5645
5708
  const this_base_url = this.base_url;
5646
5709
  const this_headers = this.headers;
5647
- return await checkResponse(
5648
- await this_fetch(_urljoin(this_base_url, path), {
5649
- method: "POST",
5650
- headers: {
5651
- Accept: "application/json",
5652
- "Content-Type": "application/json",
5653
- ...this_headers,
5654
- ...headers
5655
- },
5656
- body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
5657
- keepalive: true,
5658
- ...rest
5659
- })
5660
- );
5710
+ const tries = retries + 1;
5711
+ for (let i = 0; i < tries; i++) {
5712
+ try {
5713
+ return await checkResponse(
5714
+ await this_fetch(_urljoin(this_base_url, path), {
5715
+ method: "POST",
5716
+ headers: {
5717
+ Accept: "application/json",
5718
+ "Content-Type": "application/json",
5719
+ ...this_headers,
5720
+ ...headers
5721
+ },
5722
+ body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
5723
+ keepalive: true,
5724
+ ...rest
5725
+ })
5726
+ );
5727
+ } catch (error) {
5728
+ if (config?.signal?.aborted) {
5729
+ throw getAbortReason(config.signal);
5730
+ }
5731
+ if (i === tries - 1 || !isRetryableHTTPError(error)) {
5732
+ throw error;
5733
+ }
5734
+ debugLogger.debug(
5735
+ `Retrying API request ${path} after ${formatHTTPError(error)}`
5736
+ );
5737
+ const sleepTimeMs = HTTP_RETRY_BASE_SLEEP_TIME_S * 1e3 * 2 ** i + Math.random() * HTTP_RETRY_JITTER_MS;
5738
+ debugLogger.info(
5739
+ `Sleeping for ${sleepTimeMs}ms before retrying API request`
5740
+ );
5741
+ await waitForRetry(sleepTimeMs, config?.signal);
5742
+ }
5743
+ }
5744
+ throw new Error("Unexpected retry state");
5661
5745
  }
5662
5746
  async get_json(object_type, args = void 0, retries = 0) {
5663
5747
  const tries = retries + 1;
@@ -6754,6 +6838,45 @@ var TestBackgroundLogger = class {
6754
6838
  };
6755
6839
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
6756
6840
  var HTTP_RETRY_BASE_SLEEP_TIME_S = 1;
6841
+ var HTTP_RETRY_JITTER_MS = 200;
6842
+ var BTQL_HTTP_RETRIES = 3;
6843
+ var RETRYABLE_HTTP_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
6844
+ function isRetryableHTTPError(error) {
6845
+ return !(error instanceof FailedHTTPResponse) || RETRYABLE_HTTP_STATUS_CODES.has(error.status);
6846
+ }
6847
+ function formatHTTPError(error) {
6848
+ if (error instanceof FailedHTTPResponse) {
6849
+ return `${error.status} ${error.text}`;
6850
+ }
6851
+ return error instanceof Error ? error.message : String(error);
6852
+ }
6853
+ function getAbortReason(signal) {
6854
+ return signal.reason ?? new Error("Request aborted");
6855
+ }
6856
+ async function waitForRetry(delayMs, signal) {
6857
+ if (!signal) {
6858
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
6859
+ return;
6860
+ }
6861
+ if (signal.aborted) {
6862
+ throw getAbortReason(signal);
6863
+ }
6864
+ await new Promise((resolve, reject2) => {
6865
+ const onAbort = () => {
6866
+ clearTimeout(timeout);
6867
+ signal.removeEventListener("abort", onAbort);
6868
+ reject2(getAbortReason(signal));
6869
+ };
6870
+ const timeout = setTimeout(() => {
6871
+ signal.removeEventListener("abort", onAbort);
6872
+ resolve();
6873
+ }, delayMs);
6874
+ signal.addEventListener("abort", onAbort, { once: true });
6875
+ if (signal.aborted) {
6876
+ onAbort();
6877
+ }
6878
+ });
6879
+ }
6757
6880
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6758
6881
  apiConn;
6759
6882
  queue;
@@ -8492,15 +8615,15 @@ function parentSpanIdsUsable(spanId, rootSpanId) {
8492
8615
  return Boolean(spanId) && Boolean(rootSpanId);
8493
8616
  }
8494
8617
  function logError(span, error) {
8495
- let errorMessage = "<error>";
8618
+ let errorMessage2 = "<error>";
8496
8619
  let stackTrace = "";
8497
8620
  if (error instanceof Error) {
8498
- errorMessage = error.message;
8621
+ errorMessage2 = error.message;
8499
8622
  stackTrace = error.stack || "";
8500
8623
  } else {
8501
- errorMessage = String(error);
8624
+ errorMessage2 = String(error);
8502
8625
  }
8503
- span.log({ error: `${errorMessage}
8626
+ span.log({ error: `${errorMessage2}
8504
8627
 
8505
8628
  ${stackTrace}` });
8506
8629
  }
@@ -8688,6 +8811,12 @@ var traceable = wrapTraced;
8688
8811
  function startSpan(args) {
8689
8812
  return startSpanAndIsLogger(args).span;
8690
8813
  }
8814
+ function _internalStartSpanWithInitialMerge(args) {
8815
+ return startSpanAndIsLogger({
8816
+ ...args,
8817
+ [INITIAL_SPAN_WRITE_AS_MERGE]: true
8818
+ }).span;
8819
+ }
8691
8820
  async function flush(options) {
8692
8821
  const state = options?.state ?? _globalState;
8693
8822
  return await state.bgLogger().flush();
@@ -9081,7 +9210,8 @@ var ObjectFetcher = class {
9081
9210
  version: this.pinnedVersion
9082
9211
  } : {}
9083
9212
  },
9084
- { headers: { "Accept-Encoding": "gzip" } }
9213
+ { headers: { "Accept-Encoding": "gzip" } },
9214
+ BTQL_HTTP_RETRIES
9085
9215
  );
9086
9216
  const respJson = await resp.json();
9087
9217
  const mutate = this.mutateRecord;
@@ -9598,7 +9728,7 @@ var SpanImpl = class _SpanImpl {
9598
9728
  this._spanId = resolvedIds.spanId;
9599
9729
  this._rootSpanId = resolvedIds.rootSpanId;
9600
9730
  this._spanParents = resolvedIds.spanParents;
9601
- this.isMerge = false;
9731
+ this.isMerge = args[INITIAL_SPAN_WRITE_AS_MERGE] === true;
9602
9732
  this.logInternal({ event, internalData });
9603
9733
  this.isMerge = true;
9604
9734
  }
@@ -10805,7 +10935,8 @@ async function getPromptVersions(projectId, promptId) {
10805
10935
  use_columnstore: false,
10806
10936
  brainstore_realtime: true
10807
10937
  },
10808
- { headers: { "Accept-Encoding": "gzip" } }
10938
+ { headers: { "Accept-Encoding": "gzip" } },
10939
+ BTQL_HTTP_RETRIES
10809
10940
  );
10810
10941
  if (!response.ok) {
10811
10942
  throw new Error(
@@ -29389,6 +29520,446 @@ function isBraintrustHandler(handler) {
29389
29520
  return Reflect.get(handler, "name") === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
29390
29521
  }
29391
29522
 
29523
+ // src/instrumentation/plugins/langsmith-channels.ts
29524
+ var langSmithChannels = defineChannels("langsmith", {
29525
+ createRun: channel({
29526
+ channelName: "Client.createRun",
29527
+ kind: "async"
29528
+ }),
29529
+ updateRun: channel({
29530
+ channelName: "Client.updateRun",
29531
+ kind: "async"
29532
+ }),
29533
+ batchIngestRuns: channel({
29534
+ channelName: "Client.batchIngestRuns",
29535
+ kind: "async"
29536
+ })
29537
+ });
29538
+
29539
+ // src/instrumentation/plugins/langsmith-plugin.ts
29540
+ var MAX_COMPLETED_RUNS = 1e4;
29541
+ var BLOCKED_METADATA_KEYS = /* @__PURE__ */ new Set([
29542
+ "__proto__",
29543
+ "constructor",
29544
+ "prototype",
29545
+ "usage_metadata"
29546
+ ]);
29547
+ var BLOCKED_METADATA_PREFIXES = ["__pregel_", "langgraph_", "lc_"];
29548
+ var LLM_SETTING_KEYS = [
29549
+ "temperature",
29550
+ "top_p",
29551
+ "max_tokens",
29552
+ "frequency_penalty",
29553
+ "presence_penalty",
29554
+ "stop",
29555
+ "response_format"
29556
+ ];
29557
+ var LangSmithPlugin = class extends BasePlugin {
29558
+ activeRuns = /* @__PURE__ */ new Map();
29559
+ completedRuns = new LRUCache({
29560
+ max: MAX_COMPLETED_RUNS
29561
+ });
29562
+ skipLangChainRuns;
29563
+ constructor(options = {}) {
29564
+ super();
29565
+ this.skipLangChainRuns = options.skipLangChainRuns ?? true;
29566
+ }
29567
+ onEnable() {
29568
+ const createChannel = langSmithChannels.createRun.tracingChannel();
29569
+ const createHandlers = {
29570
+ start: (event) => {
29571
+ this.containLifecycleFailure("createRun", () => {
29572
+ this.processCreate(event.arguments[0]);
29573
+ });
29574
+ }
29575
+ };
29576
+ createChannel.subscribe(createHandlers);
29577
+ this.unsubscribers.push(() => createChannel.unsubscribe(createHandlers));
29578
+ const updateChannel = langSmithChannels.updateRun.tracingChannel();
29579
+ const updateHandlers = {
29580
+ start: (event) => {
29581
+ this.containLifecycleFailure("updateRun", () => {
29582
+ this.processUpdate(event.arguments[0], event.arguments[1]);
29583
+ });
29584
+ }
29585
+ };
29586
+ updateChannel.subscribe(updateHandlers);
29587
+ this.unsubscribers.push(() => updateChannel.unsubscribe(updateHandlers));
29588
+ const batchChannel = langSmithChannels.batchIngestRuns.tracingChannel();
29589
+ const batchHandlers = {
29590
+ start: (event) => {
29591
+ this.containLifecycleFailure("batchIngestRuns", () => {
29592
+ this.processBatch(event.arguments[0]);
29593
+ });
29594
+ }
29595
+ };
29596
+ batchChannel.subscribe(batchHandlers);
29597
+ this.unsubscribers.push(() => batchChannel.unsubscribe(batchHandlers));
29598
+ }
29599
+ onDisable() {
29600
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
29601
+ for (const { span } of this.activeRuns.values()) {
29602
+ span.end();
29603
+ }
29604
+ this.activeRuns.clear();
29605
+ this.completedRuns.clear();
29606
+ }
29607
+ processBatch(batch) {
29608
+ if (!isRecord2(batch)) {
29609
+ return;
29610
+ }
29611
+ const creates = ownValue(batch, "runCreates");
29612
+ if (Array.isArray(creates)) {
29613
+ const parentFirst = [...creates].sort((left, right) => {
29614
+ const leftId = stringValue2(ownValue(left, "id"));
29615
+ const rightId = stringValue2(ownValue(right, "id"));
29616
+ const leftParent = stringValue2(ownValue(left, "parent_run_id"));
29617
+ const rightParent = stringValue2(ownValue(right, "parent_run_id"));
29618
+ if (leftParent && leftParent === rightId) {
29619
+ return 1;
29620
+ }
29621
+ if (rightParent && rightParent === leftId) {
29622
+ return -1;
29623
+ }
29624
+ return dottedOrderDepth(left) - dottedOrderDepth(right);
29625
+ });
29626
+ for (const run of parentFirst) {
29627
+ this.containLifecycleFailure("batchIngestRuns create", () => {
29628
+ this.processCreate(run);
29629
+ });
29630
+ }
29631
+ }
29632
+ const updates = ownValue(batch, "runUpdates");
29633
+ if (Array.isArray(updates)) {
29634
+ for (const run of updates) {
29635
+ this.containLifecycleFailure("batchIngestRuns update", () => {
29636
+ this.processUpdate(stringValue2(ownValue(run, "id")) ?? "", run);
29637
+ });
29638
+ }
29639
+ }
29640
+ }
29641
+ processCreate(run) {
29642
+ const id = stringValue2(ownValue(run, "id"));
29643
+ if (!id || this.completedRuns.get(id)) {
29644
+ return;
29645
+ }
29646
+ if (this.shouldSkipLangChainRun(run)) {
29647
+ this.completedRuns.set(id, true);
29648
+ return;
29649
+ }
29650
+ const active = this.activeRuns.get(id);
29651
+ if (active) {
29652
+ const previous = active.run;
29653
+ active.run = mergeRuns(previous, run);
29654
+ this.logRun(
29655
+ active.span,
29656
+ active.run,
29657
+ previous,
29658
+ timestampSeconds(ownValue(active.run, "end_time")) !== void 0 || errorMessage(ownValue(active.run, "error")) !== void 0
29659
+ );
29660
+ this.endIfComplete(id, active, active.run);
29661
+ return;
29662
+ }
29663
+ const span = this.startRunSpan(id, run);
29664
+ const activeRun = { run, span };
29665
+ this.activeRuns.set(id, activeRun);
29666
+ this.logRun(
29667
+ span,
29668
+ run,
29669
+ void 0,
29670
+ timestampSeconds(ownValue(run, "end_time")) !== void 0 || errorMessage(ownValue(run, "error")) !== void 0
29671
+ );
29672
+ this.endIfComplete(id, activeRun, run);
29673
+ }
29674
+ processUpdate(explicitId, run) {
29675
+ const id = stringValue2(explicitId) ?? stringValue2(ownValue(run, "id"));
29676
+ if (!id || this.completedRuns.get(id)) {
29677
+ return;
29678
+ }
29679
+ if (this.shouldSkipLangChainRun(run)) {
29680
+ const active2 = this.activeRuns.get(id);
29681
+ active2?.span.end();
29682
+ this.activeRuns.delete(id);
29683
+ this.completedRuns.set(id, true);
29684
+ return;
29685
+ }
29686
+ let active = this.activeRuns.get(id);
29687
+ if (!active) {
29688
+ const span = this.startRunSpan(id, run);
29689
+ active = { run, span };
29690
+ this.activeRuns.set(id, active);
29691
+ }
29692
+ const previous = active.run;
29693
+ active.run = mergeRuns(previous, run);
29694
+ this.logRun(active.span, active.run, previous, true);
29695
+ this.endIfComplete(id, active, active.run);
29696
+ }
29697
+ startRunSpan(id, run) {
29698
+ const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
29699
+ const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
29700
+ const startTime = timestampSeconds(ownValue(run, "start_time"));
29701
+ return startSpan({
29702
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
29703
+ spanId: id,
29704
+ parentSpanIds: {
29705
+ parentSpanIds: parentId ? [parentId] : [],
29706
+ rootSpanId: traceId
29707
+ },
29708
+ spanAttributes: {
29709
+ type: mapRunType(ownValue(run, "run_type"))
29710
+ },
29711
+ ...startTime === void 0 ? {} : { startTime },
29712
+ event: { id }
29713
+ });
29714
+ }
29715
+ logRun(span, run, previous, includeOutput) {
29716
+ const inputs = preferOwnValue(run, previous, "inputs");
29717
+ const outputs = preferOwnValue(run, previous, "outputs");
29718
+ const error = errorMessage(preferOwnValue(run, previous, "error"));
29719
+ const metadata = extractMetadata(run, previous);
29720
+ const tags = extractTags(preferOwnValue(run, previous, "tags"));
29721
+ const metrics = extractMetrics(run, previous);
29722
+ span.log({
29723
+ ...inputs === void 0 ? {} : { input: sanitizeLoggedValue(inputs) },
29724
+ ...includeOutput && outputs !== void 0 ? { output: sanitizeLoggedValue(outputs) } : {},
29725
+ ...error === void 0 ? {} : { error },
29726
+ ...metadata === void 0 ? {} : { metadata },
29727
+ ...tags === void 0 ? {} : { tags },
29728
+ ...Object.keys(metrics).length === 0 ? {} : { metrics }
29729
+ });
29730
+ }
29731
+ endIfComplete(id, active, run) {
29732
+ const endTime = timestampSeconds(ownValue(run, "end_time"));
29733
+ if (endTime === void 0 && errorMessage(ownValue(run, "error")) === void 0) {
29734
+ return;
29735
+ }
29736
+ active.span.end(endTime === void 0 ? void 0 : { endTime });
29737
+ this.activeRuns.delete(id);
29738
+ this.completedRuns.set(id, true);
29739
+ }
29740
+ shouldSkipLangChainRun(run) {
29741
+ if (!this.skipLangChainRuns) {
29742
+ return false;
29743
+ }
29744
+ const serialized = ownValue(run, "serialized");
29745
+ return isRecord2(serialized) && ownValue(serialized, "lc") === 1;
29746
+ }
29747
+ containLifecycleFailure(operation, fn) {
29748
+ try {
29749
+ fn();
29750
+ } catch (error) {
29751
+ debugLogger.error(
29752
+ `Failed to process LangSmith ${operation} instrumentation:`,
29753
+ error
29754
+ );
29755
+ }
29756
+ }
29757
+ };
29758
+ function ownValue(value, key) {
29759
+ if (!isRecord2(value)) {
29760
+ return void 0;
29761
+ }
29762
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
29763
+ return descriptor && "value" in descriptor ? descriptor.value : void 0;
29764
+ }
29765
+ function preferOwnValue(current, previous, key) {
29766
+ const currentDescriptor = isRecord2(current) ? Object.getOwnPropertyDescriptor(current, key) : void 0;
29767
+ if (currentDescriptor && "value" in currentDescriptor) {
29768
+ return currentDescriptor.value;
29769
+ }
29770
+ return ownValue(previous, key);
29771
+ }
29772
+ function mergeRuns(previous, current) {
29773
+ const entries = /* @__PURE__ */ new Map();
29774
+ for (const value of [previous, current]) {
29775
+ if (!isRecord2(value)) {
29776
+ continue;
29777
+ }
29778
+ for (const [key, descriptor] of Object.entries(
29779
+ Object.getOwnPropertyDescriptors(value)
29780
+ )) {
29781
+ if (descriptor.enumerable && "value" in descriptor) {
29782
+ entries.set(key, descriptor.value);
29783
+ }
29784
+ }
29785
+ }
29786
+ return Object.fromEntries(entries);
29787
+ }
29788
+ function isRecord2(value) {
29789
+ return typeof value === "object" && value !== null && !Array.isArray(value);
29790
+ }
29791
+ function stringValue2(value) {
29792
+ return typeof value === "string" && value.length > 0 ? value : void 0;
29793
+ }
29794
+ function timestampSeconds(value) {
29795
+ if (value instanceof Date) {
29796
+ const timestamp = value.getTime();
29797
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
29798
+ }
29799
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
29800
+ return value > 1e10 ? value / 1e3 : value;
29801
+ }
29802
+ if (typeof value === "string") {
29803
+ const timestamp = Date.parse(value);
29804
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
29805
+ }
29806
+ return void 0;
29807
+ }
29808
+ function dottedOrderDepth(run) {
29809
+ const dottedOrder = stringValue2(ownValue(run, "dotted_order"));
29810
+ return dottedOrder ? dottedOrder.split(".").length : Number.MAX_SAFE_INTEGER;
29811
+ }
29812
+ function mapRunType(runType) {
29813
+ switch (runType) {
29814
+ case "llm":
29815
+ case "embedding":
29816
+ return "llm" /* LLM */;
29817
+ case "tool":
29818
+ case "retriever":
29819
+ return "tool" /* TOOL */;
29820
+ default:
29821
+ return "task" /* TASK */;
29822
+ }
29823
+ }
29824
+ function extractMetadata(run, previous) {
29825
+ const extra = preferOwnValue(run, previous, "extra");
29826
+ const rawMetadata = ownValue(extra, "metadata");
29827
+ if (!isRecord2(rawMetadata)) {
29828
+ return void 0;
29829
+ }
29830
+ const metadata = {};
29831
+ for (const [key, descriptor] of Object.entries(
29832
+ Object.getOwnPropertyDescriptors(rawMetadata)
29833
+ )) {
29834
+ 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}`)) {
29835
+ continue;
29836
+ }
29837
+ const normalizedKey = key === "ls_provider" ? "provider" : key === "ls_model_name" ? "model" : key.startsWith("ls_") ? key.slice(3) : key;
29838
+ const sanitized = sanitizeLoggedValue(descriptor.value);
29839
+ if (sanitized !== void 0) {
29840
+ metadata[normalizedKey] = sanitized;
29841
+ }
29842
+ }
29843
+ return Object.keys(metadata).length === 0 ? void 0 : metadata;
29844
+ }
29845
+ function extractTags(value) {
29846
+ if (!Array.isArray(value)) {
29847
+ return void 0;
29848
+ }
29849
+ const tags = value.filter(
29850
+ (tag) => typeof tag === "string" && tag.length > 0
29851
+ );
29852
+ return tags.length > 0 ? tags : void 0;
29853
+ }
29854
+ function extractMetrics(run, previous) {
29855
+ const outputs = preferOwnValue(run, previous, "outputs");
29856
+ const extra = preferOwnValue(run, previous, "extra");
29857
+ const metadata = ownValue(extra, "metadata");
29858
+ const usage = ownValue(outputs, "usage_metadata") ?? ownValue(metadata, "usage_metadata");
29859
+ const metrics = {};
29860
+ assignFirstMetric(metrics, "prompt_tokens", usage, [
29861
+ "input_tokens",
29862
+ "prompt_tokens"
29863
+ ]);
29864
+ assignFirstMetric(metrics, "completion_tokens", usage, [
29865
+ "output_tokens",
29866
+ "completion_tokens"
29867
+ ]);
29868
+ assignFirstMetric(metrics, "tokens", usage, ["total_tokens", "tokens"]);
29869
+ const inputTokenDetails = ownValue(usage, "input_token_details");
29870
+ assignFirstMetric(metrics, "prompt_cached_tokens", inputTokenDetails, [
29871
+ "cache_read",
29872
+ "cached_tokens"
29873
+ ]);
29874
+ if (metrics.prompt_cached_tokens === void 0) {
29875
+ assignFirstMetric(metrics, "prompt_cached_tokens", usage, [
29876
+ "cache_read_input_tokens",
29877
+ "prompt_cached_tokens"
29878
+ ]);
29879
+ }
29880
+ assignFirstMetric(
29881
+ metrics,
29882
+ "prompt_cache_creation_tokens",
29883
+ inputTokenDetails,
29884
+ ["cache_creation"]
29885
+ );
29886
+ if (metrics.prompt_cache_creation_tokens === void 0) {
29887
+ assignFirstMetric(metrics, "prompt_cache_creation_tokens", usage, [
29888
+ "cache_creation_input_tokens",
29889
+ "prompt_cache_creation_tokens"
29890
+ ]);
29891
+ }
29892
+ if (metrics.tokens === void 0 && (metrics.prompt_tokens !== void 0 || metrics.completion_tokens !== void 0)) {
29893
+ metrics.tokens = (metrics.prompt_tokens ?? 0) + (metrics.completion_tokens ?? 0);
29894
+ }
29895
+ const startTime = timestampSeconds(
29896
+ preferOwnValue(run, previous, "start_time")
29897
+ );
29898
+ const events = preferOwnValue(run, previous, "events");
29899
+ if (startTime !== void 0 && Array.isArray(events)) {
29900
+ for (const event of events) {
29901
+ if (ownValue(event, "name") !== "new_token") {
29902
+ continue;
29903
+ }
29904
+ const eventTime3 = timestampSeconds(ownValue(event, "time"));
29905
+ if (eventTime3 !== void 0 && eventTime3 >= startTime) {
29906
+ metrics.time_to_first_token = eventTime3 - startTime;
29907
+ }
29908
+ break;
29909
+ }
29910
+ }
29911
+ return metrics;
29912
+ }
29913
+ function assignFirstMetric(metrics, target, source, keys) {
29914
+ for (const key of keys) {
29915
+ const value = ownValue(source, key);
29916
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
29917
+ metrics[target] = value;
29918
+ return;
29919
+ }
29920
+ }
29921
+ }
29922
+ function errorMessage(value) {
29923
+ if (typeof value === "string") {
29924
+ return value;
29925
+ }
29926
+ if (value instanceof Error) {
29927
+ return value.message;
29928
+ }
29929
+ return void 0;
29930
+ }
29931
+ function sanitizeLoggedValue(value, seen = /* @__PURE__ */ new WeakSet(), depth = 0) {
29932
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
29933
+ return value;
29934
+ }
29935
+ if (typeof value === "number") {
29936
+ return Number.isFinite(value) ? value : void 0;
29937
+ }
29938
+ if (value instanceof Date) {
29939
+ return Number.isFinite(value.getTime()) ? value.toISOString() : void 0;
29940
+ }
29941
+ if (typeof value !== "object" || depth >= 20) {
29942
+ return void 0;
29943
+ }
29944
+ if (seen.has(value)) {
29945
+ return "[Circular]";
29946
+ }
29947
+ seen.add(value);
29948
+ if (Array.isArray(value)) {
29949
+ return value.map((item) => sanitizeLoggedValue(item, seen, depth + 1));
29950
+ }
29951
+ const entries = [];
29952
+ for (const [key, descriptor] of Object.entries(
29953
+ Object.getOwnPropertyDescriptors(value)
29954
+ )) {
29955
+ if (!descriptor.enumerable || !("value" in descriptor) || BLOCKED_METADATA_KEYS.has(key)) {
29956
+ continue;
29957
+ }
29958
+ entries.push([key, sanitizeLoggedValue(descriptor.value, seen, depth + 1)]);
29959
+ }
29960
+ return Object.fromEntries(entries);
29961
+ }
29962
+
29392
29963
  // src/instrumentation/plugins/pi-coding-agent-channels.ts
29393
29964
  var piCodingAgentChannels = defineChannels(
29394
29965
  "@earendil-works/pi-coding-agent",
@@ -29768,11 +30339,11 @@ function patchAssistantMessageStream(stream, promptState, llmState) {
29768
30339
  function recordPiAssistantMessageEvent(promptState, llmState, event) {
29769
30340
  recordFirstTokenMetric(llmState, event);
29770
30341
  const message = "message" in event ? event.message : void 0;
29771
- const errorMessage = "error" in event ? event.error : void 0;
30342
+ const errorMessage2 = "error" in event ? event.error : void 0;
29772
30343
  if (event.type === "done" && isPiAssistantMessage(message)) {
29773
30344
  finishPiLlmSpan(promptState, llmState, message);
29774
- } else if (event.type === "error" && isPiAssistantMessage(errorMessage)) {
29775
- finishPiLlmSpan(promptState, llmState, errorMessage);
30345
+ } else if (event.type === "error" && isPiAssistantMessage(errorMessage2)) {
30346
+ finishPiLlmSpan(promptState, llmState, errorMessage2);
29776
30347
  }
29777
30348
  }
29778
30349
  function recordFirstTokenMetric(state, event) {
@@ -30290,6 +30861,7 @@ var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
30290
30861
  });
30291
30862
 
30292
30863
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
30864
+ var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
30293
30865
  var StrandsAgentSDKPlugin = class extends BasePlugin {
30294
30866
  activeChildParents = /* @__PURE__ */ new WeakMap();
30295
30867
  onEnable() {
@@ -30434,11 +31006,16 @@ function startAgentStream(event, activeChildParents) {
30434
31006
  ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
30435
31007
  };
30436
31008
  const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
31009
+ const attachmentCache = createStrandsAttachmentCache();
31010
+ const input = processStrandsInputAttachments(
31011
+ event.arguments[0],
31012
+ attachmentCache
31013
+ );
30437
31014
  const span = parentSpan ? withCurrent(
30438
31015
  parentSpan,
30439
31016
  () => startSpan({
30440
31017
  event: {
30441
- input: event.arguments[0],
31018
+ input,
30442
31019
  metadata
30443
31020
  },
30444
31021
  name: formatAgentSpanName(agent),
@@ -30446,7 +31023,7 @@ function startAgentStream(event, activeChildParents) {
30446
31023
  })
30447
31024
  ) : startSpan({
30448
31025
  event: {
30449
- input: event.arguments[0],
31026
+ input,
30450
31027
  metadata
30451
31028
  },
30452
31029
  name: formatAgentSpanName(agent),
@@ -30454,6 +31031,7 @@ function startAgentStream(event, activeChildParents) {
30454
31031
  });
30455
31032
  return {
30456
31033
  activeTools: /* @__PURE__ */ new Map(),
31034
+ attachmentCache,
30457
31035
  finalized: false,
30458
31036
  metadata,
30459
31037
  span,
@@ -30469,11 +31047,12 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30469
31047
  ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
30470
31048
  };
30471
31049
  const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
31050
+ const input = processStrandsInputAttachments(event.arguments[0]);
30472
31051
  const span = parentSpan ? withCurrent(
30473
31052
  parentSpan,
30474
31053
  () => startSpan({
30475
31054
  event: {
30476
- input: event.arguments[0],
31055
+ input,
30477
31056
  metadata
30478
31057
  },
30479
31058
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -30481,7 +31060,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30481
31060
  })
30482
31061
  ) : startSpan({
30483
31062
  event: {
30484
- input: event.arguments[0],
31063
+ input,
30485
31064
  metadata
30486
31065
  },
30487
31066
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -30590,7 +31169,10 @@ function startModelSpan(state, event) {
30590
31169
  state.span,
30591
31170
  () => startSpan({
30592
31171
  event: {
30593
- input: Array.isArray(event.agent?.messages) ? event.agent.messages : void 0,
31172
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
31173
+ event.agent.messages,
31174
+ state.attachmentCache
31175
+ ) : void 0,
30594
31176
  metadata
30595
31177
  },
30596
31178
  name: formatModelSpanName(model),
@@ -30804,6 +31386,7 @@ function finalizeAgentStream(state, error, output) {
30804
31386
  ...output !== void 0 ? { output } : {}
30805
31387
  });
30806
31388
  state.span.end();
31389
+ state.attachmentCache.strings.clear();
30807
31390
  }
30808
31391
  function finalizeMultiAgentStream(state, activeChildParents, error, output) {
30809
31392
  if (state.finalized) {
@@ -30950,6 +31533,166 @@ function extractNodeResultOutput(result) {
30950
31533
  }
30951
31534
  return result;
30952
31535
  }
31536
+ var STRANDS_MEDIA_TYPES = {
31537
+ png: "image/png",
31538
+ jpg: "image/jpeg",
31539
+ jpeg: "image/jpeg",
31540
+ gif: "image/gif",
31541
+ webp: "image/webp",
31542
+ mkv: "video/x-matroska",
31543
+ mov: "video/quicktime",
31544
+ mp4: "video/mp4",
31545
+ webm: "video/webm",
31546
+ flv: "video/x-flv",
31547
+ mpeg: "video/mpeg",
31548
+ mpg: "video/mpeg",
31549
+ wmv: "video/x-ms-wmv",
31550
+ "3gp": "video/3gpp",
31551
+ pdf: "application/pdf",
31552
+ csv: "text/csv",
31553
+ doc: "application/msword",
31554
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
31555
+ xls: "application/vnd.ms-excel",
31556
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
31557
+ html: "text/html",
31558
+ txt: "text/plain",
31559
+ md: "text/markdown",
31560
+ json: "application/json",
31561
+ xml: "application/xml"
31562
+ };
31563
+ function createStrandsAttachmentCache() {
31564
+ return {
31565
+ objects: /* @__PURE__ */ new WeakMap(),
31566
+ strings: new LRUCache({
31567
+ max: MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES
31568
+ })
31569
+ };
31570
+ }
31571
+ function processStrandsInputAttachments(input, cache = createStrandsAttachmentCache()) {
31572
+ try {
31573
+ return processStrandsInputNode(input, cache);
31574
+ } catch (error) {
31575
+ logInstrumentationError5("Strands Agent SDK input attachments", error);
31576
+ return input;
31577
+ }
31578
+ }
31579
+ function processStrandsInputNode(value, cache) {
31580
+ if (value instanceof BaseAttachment) {
31581
+ return value;
31582
+ }
31583
+ if (Array.isArray(value)) {
31584
+ return value.map((child) => processStrandsInputNode(child, cache));
31585
+ }
31586
+ if (!isObject(value)) {
31587
+ return value;
31588
+ }
31589
+ const directMedia = processDirectStrandsMediaBlock(value, cache);
31590
+ if (directMedia !== void 0) {
31591
+ return directMedia;
31592
+ }
31593
+ const wrappedMedia = processWrappedStrandsMediaBlock(value, cache);
31594
+ if (wrappedMedia !== void 0) {
31595
+ return wrappedMedia;
31596
+ }
31597
+ if (value.type === "message" && Array.isArray(value.content)) {
31598
+ return {
31599
+ role: value.role,
31600
+ content: value.content.map(
31601
+ (child) => processStrandsInputNode(child, cache)
31602
+ ),
31603
+ ...value.metadata !== void 0 ? { metadata: value.metadata } : {}
31604
+ };
31605
+ }
31606
+ if (typeof value.toJSON === "function") {
31607
+ return processStrandsInputNode(value.toJSON(), cache);
31608
+ }
31609
+ return Object.fromEntries(
31610
+ Object.entries(value).map(([key, child]) => [
31611
+ key,
31612
+ processStrandsInputNode(child, cache)
31613
+ ])
31614
+ );
31615
+ }
31616
+ function processDirectStrandsMediaBlock(block, cache) {
31617
+ if (!isStrandsMediaBlock(block)) {
31618
+ return void 0;
31619
+ }
31620
+ const mediaKey = block.type === "imageBlock" ? "image" : block.type === "videoBlock" ? "video" : "document";
31621
+ return createStrandsMediaAttachment(mediaKey, block, cache);
31622
+ }
31623
+ function isStrandsMediaBlock(block) {
31624
+ return (block.type === "imageBlock" || block.type === "videoBlock" || block.type === "documentBlock") && typeof block.format === "string" && isObject(block.source);
31625
+ }
31626
+ function processWrappedStrandsMediaBlock(block, cache) {
31627
+ for (const mediaKey of ["image", "video", "document"]) {
31628
+ const media = block[mediaKey];
31629
+ if (!Object.hasOwn(block, mediaKey) || !isObject(media)) {
31630
+ continue;
31631
+ }
31632
+ const processed = createStrandsMediaAttachment(mediaKey, media, cache);
31633
+ if (processed !== void 0) {
31634
+ return processed;
31635
+ }
31636
+ }
31637
+ return void 0;
31638
+ }
31639
+ function createStrandsMediaAttachment(mediaKey, media, cache) {
31640
+ const format = media.format;
31641
+ const source = media.source;
31642
+ if (typeof format !== "string" || !isObject(source) || !Object.hasOwn(source, "bytes")) {
31643
+ return void 0;
31644
+ }
31645
+ const contentType = STRANDS_MEDIA_TYPES[format.toLowerCase()];
31646
+ if (!contentType) {
31647
+ return void 0;
31648
+ }
31649
+ const filename = mediaKey === "document" && typeof media.name === "string" && media.name.length > 0 ? media.name : `${mediaKey}.${format.toLowerCase()}`;
31650
+ const attachment = getOrCreateStrandsAttachment(
31651
+ source.bytes,
31652
+ filename,
31653
+ contentType,
31654
+ cache
31655
+ );
31656
+ if (!attachment) {
31657
+ return void 0;
31658
+ }
31659
+ const { type: _type, ...serializedMedia } = media;
31660
+ const { type: _sourceType, ...serializedSource } = source;
31661
+ return {
31662
+ [mediaKey]: {
31663
+ ...serializedMedia,
31664
+ source: {
31665
+ ...serializedSource,
31666
+ bytes: attachment
31667
+ }
31668
+ }
31669
+ };
31670
+ }
31671
+ function getOrCreateStrandsAttachment(data, filename, contentType, cache) {
31672
+ const key = `${contentType}\0${filename}`;
31673
+ const attachments = typeof data === "string" ? cache.strings.get(data) : isObject(data) ? cache.objects.get(data) : void 0;
31674
+ const cached = attachments?.get(key);
31675
+ if (cached) {
31676
+ return cached;
31677
+ }
31678
+ const blob = convertDataToBlob(data, contentType);
31679
+ if (!blob) {
31680
+ return void 0;
31681
+ }
31682
+ const attachment = new Attachment({
31683
+ data: blob,
31684
+ filename,
31685
+ contentType
31686
+ });
31687
+ const updatedAttachments = attachments ?? /* @__PURE__ */ new Map();
31688
+ updatedAttachments.set(key, attachment);
31689
+ if (typeof data === "string") {
31690
+ cache.strings.set(data, updatedAttachments);
31691
+ } else if (isObject(data)) {
31692
+ cache.objects.set(data, updatedAttachments);
31693
+ }
31694
+ return attachment;
31695
+ }
30953
31696
  function normalizeContentBlocks(blocks) {
30954
31697
  const text = blocks.map((block) => typeof block.text === "string" ? block.text : void 0).filter((part) => Boolean(part)).join("");
30955
31698
  return text.length > 0 ? text : blocks;
@@ -31079,6 +31822,7 @@ var BraintrustPlugin = class extends BasePlugin {
31079
31822
  gitHubCopilotPlugin = null;
31080
31823
  fluePlugin = null;
31081
31824
  langChainPlugin = null;
31825
+ langSmithPlugin = null;
31082
31826
  piCodingAgentPlugin = null;
31083
31827
  strandsAgentSDKPlugin = null;
31084
31828
  constructor(config = {}) {
@@ -31175,6 +31919,12 @@ var BraintrustPlugin = class extends BasePlugin {
31175
31919
  this.langChainPlugin = new LangChainPlugin();
31176
31920
  this.langChainPlugin.enable();
31177
31921
  }
31922
+ if (integrations.langsmith !== false) {
31923
+ this.langSmithPlugin = new LangSmithPlugin({
31924
+ skipLangChainRuns: integrations.langchain !== false
31925
+ });
31926
+ this.langSmithPlugin.enable();
31927
+ }
31178
31928
  }
31179
31929
  onDisable() {
31180
31930
  if (this.openaiPlugin) {
@@ -31265,6 +32015,10 @@ var BraintrustPlugin = class extends BasePlugin {
31265
32015
  this.langChainPlugin.disable();
31266
32016
  this.langChainPlugin = null;
31267
32017
  }
32018
+ if (this.langSmithPlugin) {
32019
+ this.langSmithPlugin.disable();
32020
+ this.langSmithPlugin = null;
32021
+ }
31268
32022
  }
31269
32023
  };
31270
32024
 
@@ -31329,7 +32083,8 @@ var envIntegrationAliases = {
31329
32083
  langchain: "langchain",
31330
32084
  "langchain-js": "langchain",
31331
32085
  "@langchain": "langchain",
31332
- langgraph: "langgraph"
32086
+ langgraph: "langgraph",
32087
+ langsmith: "langsmith"
31333
32088
  };
31334
32089
  function getDefaultInstrumentationIntegrations() {
31335
32090
  return {
@@ -31360,6 +32115,7 @@ function getDefaultInstrumentationIntegrations() {
31360
32115
  gitHubCopilot: true,
31361
32116
  langchain: true,
31362
32117
  langgraph: true,
32118
+ langsmith: true,
31363
32119
  piCodingAgent: true,
31364
32120
  strandsAgentSDK: true
31365
32121
  };
@@ -31580,6 +32336,8 @@ __export(exports_exports, {
31580
32336
  addAzureBlobHeaders: () => addAzureBlobHeaders,
31581
32337
  agentAssertionScorer: () => agentAssertionScorer,
31582
32338
  braintrustAISDKTelemetry: () => braintrustAISDKTelemetry,
32339
+ braintrustEveHook: () => braintrustEveHook,
32340
+ braintrustEveInstrumentation: () => braintrustEveInstrumentation,
31583
32341
  braintrustFlueInstrumentation: () => braintrustFlueInstrumentation,
31584
32342
  braintrustFlueObserver: () => braintrustFlueObserver,
31585
32343
  braintrustStreamChunkSchema: () => braintrustStreamChunkSchema,
@@ -31669,6 +32427,9 @@ __export(exports_exports, {
31669
32427
  wrapGoogleGenAI: () => wrapGoogleGenAI,
31670
32428
  wrapGroq: () => wrapGroq,
31671
32429
  wrapHuggingFace: () => wrapHuggingFace,
32430
+ wrapLangSmithClient: () => wrapLangSmithClient,
32431
+ wrapLangSmithRunTrees: () => wrapLangSmithRunTrees,
32432
+ wrapLangSmithTraceable: () => wrapLangSmithTraceable,
31672
32433
  wrapMastraAgent: () => wrapMastraAgent,
31673
32434
  wrapMistral: () => wrapMistral,
31674
32435
  wrapOpenAI: () => wrapOpenAI,
@@ -33080,6 +33841,1390 @@ function BraintrustMiddleware(config = {}) {
33080
33841
  };
33081
33842
  }
33082
33843
 
33844
+ // src/instrumentation/plugins/eve-plugin.ts
33845
+ var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
33846
+ var MAX_EVE_CACHE_ENTRIES = 1e4;
33847
+ var MAX_STORED_LLM_INPUTS = 100;
33848
+ var MAX_STORED_SPAN_REFERENCES = 1e4;
33849
+ var MAX_STORED_STEP_STARTS = 1e4;
33850
+ function braintrustEveHook(options) {
33851
+ const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
33852
+ const bridge = new EveBridge(state);
33853
+ return {
33854
+ events: {
33855
+ "*": async (event, ctx) => {
33856
+ await bridge.handle(event, ctx, options.metadata);
33857
+ }
33858
+ }
33859
+ };
33860
+ }
33861
+ function braintrustEveInstrumentation(options) {
33862
+ const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
33863
+ return {
33864
+ events: {
33865
+ "step.started": (input) => {
33866
+ try {
33867
+ captureEveModelInput(state, input);
33868
+ } catch (error) {
33869
+ debugLogger.warn("Error in Eve LLM input capture:", error);
33870
+ }
33871
+ }
33872
+ },
33873
+ recordInputs: false,
33874
+ recordOutputs: false,
33875
+ setup: options.setup
33876
+ };
33877
+ }
33878
+ function isEveHandleMessageStreamEvent(event) {
33879
+ return isObject(event) && typeof event["type"] === "string";
33880
+ }
33881
+ var ResumedEveSpan = class {
33882
+ constructor(reference) {
33883
+ this.reference = reference;
33884
+ this.endTime = reference.endTime;
33885
+ }
33886
+ endTime;
33887
+ get rootSpanId() {
33888
+ return this.reference.rootSpanId;
33889
+ }
33890
+ get spanId() {
33891
+ return this.reference.spanId;
33892
+ }
33893
+ log(event) {
33894
+ const metrics = {
33895
+ ...this.reference.startEvent?.metrics,
33896
+ ...this.endTime === void 0 ? {} : { end: this.endTime },
33897
+ ...event.metrics
33898
+ };
33899
+ updateSpan({
33900
+ exported: this.reference.exported,
33901
+ ...this.reference.startEvent,
33902
+ ...event,
33903
+ ...Object.keys(metrics).length > 0 ? { metrics } : {}
33904
+ });
33905
+ }
33906
+ end(args) {
33907
+ if (this.endTime === void 0) {
33908
+ this.endTime = args?.endTime ?? getCurrentUnixTimestamp();
33909
+ this.log({ metrics: { end: this.endTime } });
33910
+ }
33911
+ return this.endTime;
33912
+ }
33913
+ };
33914
+ var EveBridge = class {
33915
+ constructor(state) {
33916
+ this.state = state;
33917
+ }
33918
+ eventQueuesBySession = /* @__PURE__ */ new Map();
33919
+ completedToolKeys = new LRUCache({
33920
+ max: MAX_EVE_CACHE_ENTRIES
33921
+ });
33922
+ toolsByCallKey = new LRUCache({
33923
+ max: MAX_EVE_CACHE_ENTRIES
33924
+ });
33925
+ turnsByKey = new LRUCache({
33926
+ max: MAX_EVE_CACHE_ENTRIES
33927
+ });
33928
+ async startEveSpan(args) {
33929
+ const rowId = args?.event?.id;
33930
+ const reference = typeof rowId === "string" && readEveTraceState(this.state).spanReferences.find(
33931
+ (candidate) => candidate.rowId === rowId
33932
+ );
33933
+ if (reference) {
33934
+ return new ResumedEveSpan(reference);
33935
+ }
33936
+ const startTime = args?.startTime ?? getCurrentUnixTimestamp();
33937
+ const parentSpanIds = args?.parentSpanIds;
33938
+ const startEvent = {
33939
+ created: (/* @__PURE__ */ new Date()).toISOString(),
33940
+ metrics: { start: startTime },
33941
+ span_attributes: {
33942
+ ...args?.name ? { name: args.name } : {},
33943
+ ...args?.type ? { type: args.type } : {},
33944
+ ...args?.spanAttributes
33945
+ },
33946
+ span_parents: parentSpanIds ? "spanId" in parentSpanIds ? [parentSpanIds.spanId] : parentSpanIds.parentSpanIds : []
33947
+ };
33948
+ const span = withCurrent(
33949
+ NOOP_SPAN,
33950
+ () => _internalStartSpanWithInitialMerge({ ...args, startTime })
33951
+ );
33952
+ if (typeof rowId !== "string") {
33953
+ return span;
33954
+ }
33955
+ try {
33956
+ const exported = await span.export();
33957
+ const reference2 = {
33958
+ exported,
33959
+ rootSpanId: span.rootSpanId,
33960
+ rowId,
33961
+ spanId: span.spanId,
33962
+ startEvent
33963
+ };
33964
+ this.state.update((current) => {
33965
+ const normalized = normalizeEveTraceState(current);
33966
+ return normalized.spanReferences.some(
33967
+ (candidate) => candidate.rowId === rowId
33968
+ ) ? normalized : {
33969
+ ...normalized,
33970
+ spanReferences: [...normalized.spanReferences, reference2].slice(
33971
+ -MAX_STORED_SPAN_REFERENCES
33972
+ )
33973
+ };
33974
+ });
33975
+ } catch (error) {
33976
+ debugLogger.warn("Error exporting Eve span for resumption:", error);
33977
+ }
33978
+ return span;
33979
+ }
33980
+ async startEveChildSpan(parent, args) {
33981
+ return await this.startEveSpan({
33982
+ ...args,
33983
+ parentSpanIds: {
33984
+ rootSpanId: parent.rootSpanId,
33985
+ spanId: parent.spanId
33986
+ }
33987
+ });
33988
+ }
33989
+ stepOrdinal(event) {
33990
+ let ordinal = 0;
33991
+ this.state.update((current) => {
33992
+ const state = normalizeEveTraceState(current);
33993
+ const previous = state.stepStarts.filter(
33994
+ (entry) => entry.turnId === event.data.turnId && entry.stepIndex === event.data.stepIndex
33995
+ ).at(-1);
33996
+ if (previous?.open) {
33997
+ ordinal = previous.ordinal;
33998
+ return state;
33999
+ }
34000
+ ordinal = state.stepStarts.filter(
34001
+ (entry) => entry.turnId === event.data.turnId
34002
+ ).length;
34003
+ return {
34004
+ ...state,
34005
+ stepStarts: [
34006
+ ...state.stepStarts,
34007
+ {
34008
+ open: true,
34009
+ ordinal,
34010
+ stepIndex: event.data.stepIndex,
34011
+ turnId: event.data.turnId
34012
+ }
34013
+ ].slice(-MAX_STORED_STEP_STARTS)
34014
+ };
34015
+ });
34016
+ return ordinal;
34017
+ }
34018
+ markStepEnded(turnId, stepIndex) {
34019
+ this.state.update((current) => {
34020
+ const state = normalizeEveTraceState(current);
34021
+ let index = -1;
34022
+ for (let i = state.stepStarts.length - 1; i >= 0; i--) {
34023
+ const entry = state.stepStarts[i];
34024
+ if (entry?.turnId === turnId && entry.stepIndex === stepIndex) {
34025
+ index = i;
34026
+ break;
34027
+ }
34028
+ }
34029
+ if (index < 0 || !state.stepStarts[index]?.open) {
34030
+ return state;
34031
+ }
34032
+ return {
34033
+ ...state,
34034
+ stepStarts: state.stepStarts.map(
34035
+ (entry, entryIndex) => entryIndex === index ? { ...entry, open: false } : entry
34036
+ )
34037
+ };
34038
+ });
34039
+ }
34040
+ async handle(event, ctx, hookMetadata) {
34041
+ if (!isEveHandleMessageStreamEvent(event)) {
34042
+ return;
34043
+ }
34044
+ const run = async () => {
34045
+ try {
34046
+ if (!await this.handleEvent(event, ctx, hookMetadata)) {
34047
+ return;
34048
+ }
34049
+ if (event.type === "session.failed") {
34050
+ const sessionId2 = event.data.sessionId || sessionIdFromContext(ctx);
34051
+ await this.flushInstrumentation();
34052
+ if (sessionId2) {
34053
+ this.cleanupSession(sessionId2);
34054
+ }
34055
+ } else if (event.type === "session.completed") {
34056
+ const sessionId2 = sessionIdFromContext(ctx);
34057
+ await this.flushInstrumentation();
34058
+ if (sessionId2) {
34059
+ this.cleanupSession(sessionId2);
34060
+ }
34061
+ }
34062
+ } catch (error) {
34063
+ debugLogger.warn("Error in Eve hook instrumentation:", error);
34064
+ }
34065
+ };
34066
+ const sessionId = event.type === "session.failed" ? event.data.sessionId || sessionIdFromContext(ctx) : sessionIdFromContext(ctx);
34067
+ if (!sessionId) {
34068
+ await run();
34069
+ return;
34070
+ }
34071
+ const previous = this.eventQueuesBySession.get(sessionId);
34072
+ const queued = previous ? previous.then(run) : run();
34073
+ this.eventQueuesBySession.set(sessionId, queued);
34074
+ try {
34075
+ await queued;
34076
+ } finally {
34077
+ if (this.eventQueuesBySession.get(sessionId) === queued) {
34078
+ this.eventQueuesBySession.delete(sessionId);
34079
+ }
34080
+ }
34081
+ }
34082
+ async handleEvent(event, ctx, hookMetadata) {
34083
+ switch (event.type) {
34084
+ case "session.started":
34085
+ this.handleSessionStarted(event, ctx, hookMetadata);
34086
+ return true;
34087
+ case "turn.started":
34088
+ await this.handleTurnStarted(event, ctx, hookMetadata);
34089
+ return true;
34090
+ case "message.received":
34091
+ await this.handleMessageReceived(event, ctx, hookMetadata);
34092
+ return true;
34093
+ case "step.started":
34094
+ await this.handleStepStarted(event, ctx, hookMetadata);
34095
+ return true;
34096
+ case "message.completed":
34097
+ this.handleMessageCompleted(event, ctx);
34098
+ return true;
34099
+ case "result.completed":
34100
+ this.handleResultCompleted(event, ctx);
34101
+ return true;
34102
+ case "actions.requested":
34103
+ await this.handleActionsRequested(event, ctx, hookMetadata);
34104
+ return true;
34105
+ case "action.result":
34106
+ await this.handleActionResult(event, ctx, hookMetadata);
34107
+ return true;
34108
+ case "subagent.called":
34109
+ await this.handleSubagentCalled(event, ctx, hookMetadata);
34110
+ return true;
34111
+ case "subagent.completed":
34112
+ await this.handleSubagentCompleted(event, ctx, hookMetadata);
34113
+ return true;
34114
+ case "step.completed":
34115
+ this.handleStepCompleted(event, ctx);
34116
+ return true;
34117
+ case "step.failed":
34118
+ this.handleStepFailed(event, ctx);
34119
+ return true;
34120
+ case "turn.completed":
34121
+ this.handleTurnCompleted(event, ctx);
34122
+ return true;
34123
+ case "turn.failed":
34124
+ this.handleTurnFailed(event, ctx);
34125
+ return true;
34126
+ case "session.failed":
34127
+ this.handleSessionFailed(event, ctx);
34128
+ return true;
34129
+ case "session.completed":
34130
+ this.handleSessionCompleted(event, ctx);
34131
+ return true;
34132
+ default:
34133
+ return false;
34134
+ }
34135
+ }
34136
+ handleSessionStarted(event, ctx, hookMetadata) {
34137
+ const sessionId = sessionIdFromContext(ctx);
34138
+ if (!sessionId) {
34139
+ return;
34140
+ }
34141
+ const metadata = {
34142
+ ...hookMetadata ?? {},
34143
+ ...modelMetadataFromRuntime(event.data.runtime)
34144
+ };
34145
+ this.state.update((current) => {
34146
+ const normalized = normalizeEveTraceState(current);
34147
+ return {
34148
+ ...normalized,
34149
+ metadata: { ...normalized.metadata, ...metadata }
34150
+ };
34151
+ });
34152
+ for (const [key, turn] of this.turnsByKey) {
34153
+ if (!key.startsWith(`${sessionId}:`)) {
34154
+ continue;
34155
+ }
34156
+ turn.metadata = { ...turn.metadata, ...metadata };
34157
+ turn.span.log({ metadata: turn.metadata });
34158
+ for (const step of turn.stepsByIndex.values()) {
34159
+ step.metadata = { ...step.metadata, ...metadata };
34160
+ step.span.log({ metadata: step.metadata });
34161
+ }
34162
+ }
34163
+ }
34164
+ async handleTurnStarted(event, ctx, hookMetadata) {
34165
+ const sessionId = sessionIdFromContext(ctx);
34166
+ if (!sessionId) {
34167
+ return;
34168
+ }
34169
+ const key = turnKey2(sessionId, event.data.turnId);
34170
+ const metadata = {
34171
+ ...readEveTraceState(this.state).metadata,
34172
+ ...hookMetadata ?? {},
34173
+ "eve.session_id": sessionId
34174
+ };
34175
+ const existing = this.turnsByKey.get(key);
34176
+ if (existing) {
34177
+ existing.metadata = { ...existing.metadata, ...metadata };
34178
+ existing.span.log({ metadata: existing.metadata });
34179
+ return;
34180
+ }
34181
+ const span = await this.startTurnSpan(sessionId, event, ctx, metadata);
34182
+ span.log({ metadata });
34183
+ this.turnsByKey.set(key, {
34184
+ key,
34185
+ metadata,
34186
+ metrics: {},
34187
+ span,
34188
+ stepsByIndex: /* @__PURE__ */ new Map(),
34189
+ turnId: event.data.turnId
34190
+ });
34191
+ }
34192
+ async handleMessageReceived(event, ctx, hookMetadata) {
34193
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34194
+ if (!turn) {
34195
+ return;
34196
+ }
34197
+ const input = [{ content: event.data.message, role: "user" }];
34198
+ turn.span.log({ input });
34199
+ }
34200
+ async handleStepStarted(event, ctx, hookMetadata) {
34201
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34202
+ const sessionId = sessionIdFromContext(ctx);
34203
+ if (!turn || !sessionId) {
34204
+ return;
34205
+ }
34206
+ const existing = turn.stepsByIndex.get(event.data.stepIndex);
34207
+ if (existing) {
34208
+ existing.span.log({
34209
+ ...existing.input !== void 0 ? { input: existing.input } : {},
34210
+ metadata: existing.metadata,
34211
+ metrics: existing.metrics,
34212
+ output: existing.output
34213
+ });
34214
+ const endTime = eventTime2(event);
34215
+ existing.span.end(endTime === void 0 ? void 0 : { endTime });
34216
+ this.markStepEnded(event.data.turnId, event.data.stepIndex);
34217
+ }
34218
+ const stepOrdinal = this.stepOrdinal(event);
34219
+ const metadata = { ...turn.metadata };
34220
+ const input = consumeCapturedEveModelInput(
34221
+ this.state,
34222
+ sessionId,
34223
+ event.data.turnId,
34224
+ event.data.stepIndex
34225
+ );
34226
+ const { rowId: eventId, spanId } = await generateEveIds(
34227
+ "step",
34228
+ sessionId,
34229
+ event.data.turnId,
34230
+ String(stepOrdinal)
34231
+ );
34232
+ const span = await this.startEveChildSpan(turn.span, {
34233
+ event: {
34234
+ id: eventId,
34235
+ ...input !== void 0 ? { input } : {},
34236
+ metadata
34237
+ },
34238
+ name: "eve.step",
34239
+ spanAttributes: { type: "llm" /* LLM */ },
34240
+ spanId,
34241
+ startTime: eventTime2(event)
34242
+ });
34243
+ span.log({ ...input !== void 0 ? { input } : {}, metadata });
34244
+ turn.stepsByIndex.set(event.data.stepIndex, {
34245
+ ...input !== void 0 ? { input } : {},
34246
+ metadata,
34247
+ metrics: {},
34248
+ span
34249
+ });
34250
+ }
34251
+ handleMessageCompleted(event, ctx) {
34252
+ const step = this.stepForEvent(event, ctx);
34253
+ if (!step) {
34254
+ return;
34255
+ }
34256
+ const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
34257
+ const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
34258
+ step.output = [
34259
+ {
34260
+ finish_reason: normalizedFinishReason(event.data.finishReason),
34261
+ index: 0,
34262
+ message: {
34263
+ content: event.data.message,
34264
+ role: "assistant",
34265
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
34266
+ }
34267
+ }
34268
+ ];
34269
+ const turn = this.turnForEvent(event, ctx);
34270
+ if (turn && event.data.finishReason !== "tool-calls") {
34271
+ turn.output = event.data.message;
34272
+ }
34273
+ }
34274
+ handleResultCompleted(event, ctx) {
34275
+ const step = this.stepForEvent(event, ctx);
34276
+ if (step) {
34277
+ step.output = [
34278
+ {
34279
+ finish_reason: "stop",
34280
+ index: 0,
34281
+ message: {
34282
+ content: event.data.result,
34283
+ role: "assistant"
34284
+ }
34285
+ }
34286
+ ];
34287
+ }
34288
+ const turn = this.turnForEvent(event, ctx);
34289
+ if (turn) {
34290
+ turn.output = event.data.result;
34291
+ }
34292
+ }
34293
+ async handleActionsRequested(event, ctx, hookMetadata) {
34294
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34295
+ const sessionId = sessionIdFromContext(ctx);
34296
+ if (!turn || !sessionId) {
34297
+ return;
34298
+ }
34299
+ const traceActions = event.data.actions.filter(isTraceableActionRequest);
34300
+ if (traceActions.length === 0) {
34301
+ return;
34302
+ }
34303
+ for (const action of traceActions) {
34304
+ if (isToolCallAction(action)) {
34305
+ await this.startRequestedTool(event, turn, sessionId, action);
34306
+ } else if (isLocalSubagentCallAction(action)) {
34307
+ await this.startRequestedSubagent(event, turn, sessionId, action);
34308
+ }
34309
+ }
34310
+ const step = turn.stepsByIndex.get(event.data.stepIndex);
34311
+ if (!step) {
34312
+ return;
34313
+ }
34314
+ const toolCallsById = /* @__PURE__ */ new Map();
34315
+ if (Array.isArray(step.output) && isObject(step.output[0])) {
34316
+ const message = step.output[0]["message"];
34317
+ if (isObject(message) && Array.isArray(message["tool_calls"])) {
34318
+ for (const toolCall of message["tool_calls"]) {
34319
+ if (isObject(toolCall) && typeof toolCall["id"] === "string") {
34320
+ toolCallsById.set(toolCall["id"], toolCall);
34321
+ }
34322
+ }
34323
+ }
34324
+ }
34325
+ for (const action of traceActions) {
34326
+ const name = action.kind === "tool-call" ? action.toolName : action.subagentName ?? action.name ?? "agent";
34327
+ toolCallsById.set(action.callId, {
34328
+ function: {
34329
+ arguments: JSON.stringify(action.input),
34330
+ name
34331
+ },
34332
+ id: action.callId,
34333
+ type: "function"
34334
+ });
34335
+ }
34336
+ step.output = [
34337
+ {
34338
+ finish_reason: "tool_calls",
34339
+ index: 0,
34340
+ message: {
34341
+ content: null,
34342
+ role: "assistant",
34343
+ tool_calls: [...toolCallsById.values()]
34344
+ }
34345
+ }
34346
+ ];
34347
+ step.span.log({ metadata: step.metadata, output: step.output });
34348
+ }
34349
+ async handleActionResult(event, ctx, hookMetadata) {
34350
+ if (isToolResult(event.data.result)) {
34351
+ await this.handleToolResult(event, ctx, event.data.result, hookMetadata);
34352
+ return;
34353
+ }
34354
+ if (isSubagentResult(event.data.result)) {
34355
+ await this.handleSubagentResult(
34356
+ event,
34357
+ ctx,
34358
+ event.data.result,
34359
+ hookMetadata
34360
+ );
34361
+ }
34362
+ }
34363
+ async handleToolResult(event, ctx, result, hookMetadata) {
34364
+ const sessionId = sessionIdFromContext(ctx);
34365
+ if (!sessionId) {
34366
+ return;
34367
+ }
34368
+ const key = toolKey3(sessionId, result.callId);
34369
+ if (this.completedToolKeys.has(key)) {
34370
+ return;
34371
+ }
34372
+ const tool = this.toolsByCallKey.get(key) ?? await this.startSyntheticTool(event, ctx, result, hookMetadata);
34373
+ if (!tool) {
34374
+ return;
34375
+ }
34376
+ const failed = event.data.status === "failed" || result.isError === true || event.data.error !== void 0;
34377
+ tool.span.log({
34378
+ ...failed ? {
34379
+ error: actionResultError(event.data.error, result.output)
34380
+ } : {},
34381
+ metadata: tool.metadata,
34382
+ output: result.output
34383
+ });
34384
+ const endTime = eventTime2(event);
34385
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34386
+ this.toolsByCallKey.delete(key);
34387
+ this.completedToolKeys.set(key, true);
34388
+ }
34389
+ async handleSubagentCalled(event, ctx, hookMetadata) {
34390
+ if (event.data.remote?.url) {
34391
+ return;
34392
+ }
34393
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34394
+ const sessionId = sessionIdFromContext(ctx);
34395
+ if (!turn || !sessionId) {
34396
+ return;
34397
+ }
34398
+ const key = toolKey3(sessionId, event.data.callId);
34399
+ const metadata = toolMetadataFromTurn(turn);
34400
+ const existing = this.toolsByCallKey.get(key);
34401
+ if (existing) {
34402
+ existing.metadata = { ...existing.metadata, ...metadata };
34403
+ existing.span.log({ metadata: existing.metadata });
34404
+ return;
34405
+ }
34406
+ if (this.completedToolKeys.has(key)) {
34407
+ return;
34408
+ }
34409
+ const { rowId: eventId, spanId } = await generateEveIds(
34410
+ "subagent",
34411
+ sessionId,
34412
+ event.data.callId
34413
+ );
34414
+ const pending = this.toolsByCallKey.get(key);
34415
+ if (pending || this.completedToolKeys.has(key)) {
34416
+ if (pending) {
34417
+ pending.metadata = { ...pending.metadata, ...metadata };
34418
+ pending.span.log({ metadata: pending.metadata });
34419
+ }
34420
+ return;
34421
+ }
34422
+ const span = await this.startEveChildSpan(turn.span, {
34423
+ event: {
34424
+ id: eventId,
34425
+ metadata
34426
+ },
34427
+ name: event.data.toolName ?? event.data.name,
34428
+ spanAttributes: { type: "tool" /* TOOL */ },
34429
+ spanId,
34430
+ startTime: eventTime2(event)
34431
+ });
34432
+ span.log({ metadata });
34433
+ this.toolsByCallKey.set(key, {
34434
+ metadata,
34435
+ span,
34436
+ turnKey: turnKey2(sessionId, event.data.turnId)
34437
+ });
34438
+ }
34439
+ async handleSubagentCompleted(event, ctx, hookMetadata) {
34440
+ const sessionId = sessionIdFromContext(ctx);
34441
+ if (!sessionId) {
34442
+ return;
34443
+ }
34444
+ const key = toolKey3(sessionId, event.data.callId);
34445
+ if (this.completedToolKeys.has(key)) {
34446
+ return;
34447
+ }
34448
+ const subagent = this.toolsByCallKey.get(key) ?? await this.startSyntheticSubagent(event, ctx, hookMetadata);
34449
+ if (!subagent) {
34450
+ return;
34451
+ }
34452
+ subagent.span.log({
34453
+ ...event.data.status === "failed" ? {
34454
+ error: actionResultError(event.data.error, event.data.output)
34455
+ } : {},
34456
+ metadata: subagent.metadata,
34457
+ ...event.data.output !== void 0 ? { output: event.data.output } : {}
34458
+ });
34459
+ const endTime = eventTime2(event);
34460
+ const recordedEndTime = subagent.span.end(
34461
+ endTime === void 0 ? void 0 : { endTime }
34462
+ );
34463
+ this.state.update((current) => {
34464
+ const normalized = normalizeEveTraceState(current);
34465
+ return {
34466
+ ...normalized,
34467
+ spanReferences: normalized.spanReferences.map(
34468
+ (reference) => reference.spanId === subagent.span.spanId ? { ...reference, endTime: recordedEndTime } : reference
34469
+ )
34470
+ };
34471
+ });
34472
+ }
34473
+ async handleSubagentResult(event, ctx, result, hookMetadata) {
34474
+ const sessionId = sessionIdFromContext(ctx);
34475
+ if (!sessionId) {
34476
+ return;
34477
+ }
34478
+ const key = toolKey3(sessionId, result.callId);
34479
+ if (this.completedToolKeys.has(key)) {
34480
+ return;
34481
+ }
34482
+ const subagent = this.toolsByCallKey.get(key) ?? await this.startSyntheticSubagentResult(
34483
+ event,
34484
+ ctx,
34485
+ result,
34486
+ hookMetadata
34487
+ );
34488
+ if (!subagent) {
34489
+ return;
34490
+ }
34491
+ const isError = event.data.status === "failed" || result.isError === true || event.data.error !== void 0;
34492
+ subagent.span.log({
34493
+ ...isError ? {
34494
+ error: actionResultError(event.data.error, result.output)
34495
+ } : {},
34496
+ metadata: subagent.metadata,
34497
+ output: result.output
34498
+ });
34499
+ const endTime = eventTime2(event);
34500
+ subagent.span.end(endTime === void 0 ? void 0 : { endTime });
34501
+ this.toolsByCallKey.delete(key);
34502
+ this.completedToolKeys.set(key, true);
34503
+ }
34504
+ handleStepCompleted(event, ctx) {
34505
+ const step = this.stepForEvent(event, ctx);
34506
+ if (!step) {
34507
+ return;
34508
+ }
34509
+ const usage = event.data.usage;
34510
+ const inputTokens = typeof usage?.inputTokens === "number" && Number.isFinite(usage.inputTokens) && usage.inputTokens >= 0 ? usage.inputTokens : void 0;
34511
+ const outputTokens = typeof usage?.outputTokens === "number" && Number.isFinite(usage.outputTokens) && usage.outputTokens >= 0 ? usage.outputTokens : void 0;
34512
+ const cacheReadTokens = typeof usage?.cacheReadTokens === "number" && Number.isFinite(usage.cacheReadTokens) && usage.cacheReadTokens >= 0 ? usage.cacheReadTokens : void 0;
34513
+ const cacheWriteTokens = typeof usage?.cacheWriteTokens === "number" && Number.isFinite(usage.cacheWriteTokens) && usage.cacheWriteTokens >= 0 ? usage.cacheWriteTokens : void 0;
34514
+ const costUsd = typeof usage?.costUsd === "number" && Number.isFinite(usage.costUsd) && usage.costUsd >= 0 ? usage.costUsd : void 0;
34515
+ const total = inputTokens !== void 0 && outputTokens !== void 0 ? inputTokens + outputTokens : void 0;
34516
+ const metrics = {
34517
+ ...inputTokens !== void 0 ? { prompt_tokens: inputTokens } : {},
34518
+ ...outputTokens !== void 0 ? { completion_tokens: outputTokens } : {},
34519
+ ...total !== void 0 ? { tokens: total } : {},
34520
+ ...cacheReadTokens !== void 0 ? { prompt_cached_tokens: cacheReadTokens } : {},
34521
+ ...cacheWriteTokens !== void 0 ? { prompt_cache_creation_tokens: cacheWriteTokens } : {},
34522
+ ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
34523
+ };
34524
+ step.metrics = { ...step.metrics, ...metrics };
34525
+ if (Array.isArray(step.output) && isObject(step.output[0])) {
34526
+ const finishReason = step.output[0].finish_reason;
34527
+ if (typeof finishReason !== "string") {
34528
+ step.output[0].finish_reason = normalizedFinishReason(
34529
+ event.data.finishReason
34530
+ );
34531
+ }
34532
+ }
34533
+ step.span.log({
34534
+ ...step.input !== void 0 ? { input: step.input } : {},
34535
+ metadata: step.metadata,
34536
+ metrics,
34537
+ output: step.output
34538
+ });
34539
+ const endTime = eventTime2(event);
34540
+ step.span.end(endTime === void 0 ? void 0 : { endTime });
34541
+ const turn = this.turnForEvent(event, ctx);
34542
+ if (turn) {
34543
+ for (const [key, value] of Object.entries(metrics)) {
34544
+ turn.metrics[key] = (turn.metrics[key] ?? 0) + value;
34545
+ }
34546
+ turn.stepsByIndex.delete(event.data.stepIndex);
34547
+ }
34548
+ this.markStepEnded(event.data.turnId, event.data.stepIndex);
34549
+ }
34550
+ handleStepFailed(event, ctx) {
34551
+ const step = this.stepForEvent(event, ctx);
34552
+ if (step) {
34553
+ step.span.log({
34554
+ error: errorFromMessage(
34555
+ event.data.message,
34556
+ event.data.code,
34557
+ event.data.details
34558
+ )
34559
+ });
34560
+ const endTime = eventTime2(event);
34561
+ step.span.end(endTime === void 0 ? void 0 : { endTime });
34562
+ }
34563
+ const turn = this.turnForEvent(event, ctx);
34564
+ turn?.stepsByIndex.delete(event.data.stepIndex);
34565
+ this.markStepEnded(event.data.turnId, event.data.stepIndex);
34566
+ }
34567
+ handleTurnCompleted(event, ctx) {
34568
+ const turn = this.turnForEvent(event, ctx);
34569
+ if (!turn) {
34570
+ return;
34571
+ }
34572
+ this.finalizeTurn(turn, {
34573
+ endTime: eventTime2(event)
34574
+ });
34575
+ }
34576
+ handleTurnFailed(event, ctx) {
34577
+ const turn = this.turnForEvent(event, ctx);
34578
+ if (!turn) {
34579
+ return;
34580
+ }
34581
+ this.finalizeTurn(turn, {
34582
+ endTime: eventTime2(event),
34583
+ error: errorFromMessage(
34584
+ event.data.message,
34585
+ event.data.code,
34586
+ event.data.details
34587
+ )
34588
+ });
34589
+ }
34590
+ handleSessionFailed(event, ctx) {
34591
+ const sessionId = event.data.sessionId || sessionIdFromContext(ctx);
34592
+ if (!sessionId) {
34593
+ return;
34594
+ }
34595
+ const error = errorFromMessage(
34596
+ event.data.message,
34597
+ event.data.code,
34598
+ event.data.details
34599
+ );
34600
+ for (const [key, turn] of this.turnsByKey) {
34601
+ if (!key.startsWith(`${sessionId}:`)) {
34602
+ continue;
34603
+ }
34604
+ this.finalizeTurn(turn, {
34605
+ endTime: eventTime2(event),
34606
+ error
34607
+ });
34608
+ }
34609
+ for (const [key, tool] of this.toolsByCallKey) {
34610
+ if (key.startsWith(`${sessionId}:`)) {
34611
+ const endTime = eventTime2(event);
34612
+ if (!tool.endedByTurn) {
34613
+ tool.span.log({ metadata: tool.metadata });
34614
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34615
+ tool.endedByTurn = true;
34616
+ }
34617
+ }
34618
+ }
34619
+ }
34620
+ handleSessionCompleted(event, ctx) {
34621
+ const sessionId = sessionIdFromContext(ctx);
34622
+ if (!sessionId) {
34623
+ return;
34624
+ }
34625
+ for (const [key, turn] of this.turnsByKey) {
34626
+ if (!key.startsWith(`${sessionId}:`)) {
34627
+ continue;
34628
+ }
34629
+ this.finalizeTurn(turn, {
34630
+ endTime: eventTime2(event)
34631
+ });
34632
+ }
34633
+ for (const [key, tool] of this.toolsByCallKey) {
34634
+ if (key.startsWith(`${sessionId}:`) && !tool.endedByTurn) {
34635
+ const endTime = eventTime2(event);
34636
+ tool.span.log({ metadata: tool.metadata });
34637
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34638
+ tool.endedByTurn = true;
34639
+ }
34640
+ }
34641
+ }
34642
+ async ensureTurn(event, ctx, hookMetadata) {
34643
+ const sessionId = sessionIdFromContext(ctx);
34644
+ if (!sessionId) {
34645
+ return void 0;
34646
+ }
34647
+ const key = turnKey2(sessionId, event.data.turnId);
34648
+ const existing = this.turnsByKey.get(key);
34649
+ if (existing) {
34650
+ return existing;
34651
+ }
34652
+ const metadata = {
34653
+ ...readEveTraceState(this.state).metadata,
34654
+ ...hookMetadata ?? {},
34655
+ "eve.session_id": sessionId
34656
+ };
34657
+ const span = await this.startTurnSpan(sessionId, event, ctx, metadata);
34658
+ span.log({ metadata });
34659
+ const state = {
34660
+ key,
34661
+ metadata,
34662
+ metrics: {},
34663
+ span,
34664
+ stepsByIndex: /* @__PURE__ */ new Map(),
34665
+ turnId: event.data.turnId
34666
+ };
34667
+ this.turnsByKey.set(key, state);
34668
+ return state;
34669
+ }
34670
+ async startRequestedTool(event, turn, sessionId, action) {
34671
+ const key = toolKey3(sessionId, action.callId);
34672
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34673
+ return;
34674
+ }
34675
+ const metadata = toolMetadataFromTurn(turn);
34676
+ const { rowId: eventId, spanId } = await generateEveIds(
34677
+ "tool",
34678
+ sessionId,
34679
+ event.data.turnId,
34680
+ action.callId
34681
+ );
34682
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34683
+ return;
34684
+ }
34685
+ const span = await this.startEveChildSpan(turn.span, {
34686
+ event: {
34687
+ id: eventId,
34688
+ input: action.input,
34689
+ metadata
34690
+ },
34691
+ name: action.toolName,
34692
+ spanAttributes: { type: "tool" /* TOOL */ },
34693
+ spanId,
34694
+ startTime: eventTime2(event)
34695
+ });
34696
+ span.log({ input: action.input, metadata });
34697
+ this.toolsByCallKey.set(key, {
34698
+ metadata,
34699
+ span,
34700
+ turnKey: turnKey2(sessionId, event.data.turnId)
34701
+ });
34702
+ }
34703
+ async startRequestedSubagent(event, turn, sessionId, action) {
34704
+ const key = toolKey3(sessionId, action.callId);
34705
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34706
+ return;
34707
+ }
34708
+ const name = action.subagentName ?? action.name ?? "agent";
34709
+ const metadata = toolMetadataFromTurn(turn);
34710
+ const { rowId: eventId, spanId } = await generateEveIds(
34711
+ "subagent",
34712
+ sessionId,
34713
+ action.callId
34714
+ );
34715
+ if (this.toolsByCallKey.has(key) || this.completedToolKeys.has(key)) {
34716
+ return;
34717
+ }
34718
+ const span = await this.startEveChildSpan(turn.span, {
34719
+ event: {
34720
+ id: eventId,
34721
+ input: action.input,
34722
+ metadata
34723
+ },
34724
+ name,
34725
+ spanAttributes: { type: "tool" /* TOOL */ },
34726
+ spanId,
34727
+ startTime: eventTime2(event)
34728
+ });
34729
+ span.log({ input: action.input, metadata });
34730
+ this.toolsByCallKey.set(key, {
34731
+ metadata,
34732
+ span,
34733
+ turnKey: turnKey2(sessionId, event.data.turnId)
34734
+ });
34735
+ }
34736
+ async startSyntheticTool(event, ctx, result, hookMetadata) {
34737
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34738
+ const sessionId = sessionIdFromContext(ctx);
34739
+ if (!turn || !sessionId) {
34740
+ return void 0;
34741
+ }
34742
+ const metadata = toolMetadataFromTurn(turn);
34743
+ const { rowId: eventId, spanId } = await generateEveIds(
34744
+ "tool",
34745
+ sessionId,
34746
+ event.data.turnId,
34747
+ result.callId
34748
+ );
34749
+ const existing = this.toolsByCallKey.get(toolKey3(sessionId, result.callId));
34750
+ if (existing) {
34751
+ return existing;
34752
+ }
34753
+ const span = await this.startEveChildSpan(turn.span, {
34754
+ event: {
34755
+ id: eventId,
34756
+ metadata
34757
+ },
34758
+ name: result.toolName,
34759
+ spanAttributes: { type: "tool" /* TOOL */ },
34760
+ spanId,
34761
+ startTime: eventTime2(event)
34762
+ });
34763
+ span.log({ metadata });
34764
+ const state = {
34765
+ metadata,
34766
+ span,
34767
+ turnKey: turnKey2(sessionId, event.data.turnId)
34768
+ };
34769
+ this.toolsByCallKey.set(toolKey3(sessionId, result.callId), state);
34770
+ return state;
34771
+ }
34772
+ async startSyntheticSubagent(event, ctx, hookMetadata) {
34773
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34774
+ const sessionId = sessionIdFromContext(ctx);
34775
+ if (!turn || !sessionId) {
34776
+ return void 0;
34777
+ }
34778
+ const metadata = toolMetadataFromTurn(turn);
34779
+ const { rowId: eventId, spanId } = await generateEveIds(
34780
+ "subagent",
34781
+ sessionId,
34782
+ event.data.callId
34783
+ );
34784
+ const existing = this.toolsByCallKey.get(
34785
+ toolKey3(sessionId, event.data.callId)
34786
+ );
34787
+ if (existing) {
34788
+ return existing;
34789
+ }
34790
+ const span = await this.startEveChildSpan(turn.span, {
34791
+ event: {
34792
+ id: eventId,
34793
+ metadata
34794
+ },
34795
+ name: event.data.subagentName,
34796
+ spanAttributes: { type: "tool" /* TOOL */ },
34797
+ spanId,
34798
+ startTime: eventTime2(event)
34799
+ });
34800
+ span.log({ metadata });
34801
+ const state = {
34802
+ metadata,
34803
+ span,
34804
+ turnKey: turnKey2(sessionId, event.data.turnId)
34805
+ };
34806
+ this.toolsByCallKey.set(toolKey3(sessionId, event.data.callId), state);
34807
+ return state;
34808
+ }
34809
+ async startSyntheticSubagentResult(event, ctx, result, hookMetadata) {
34810
+ const turn = await this.ensureTurn(event, ctx, hookMetadata);
34811
+ const sessionId = sessionIdFromContext(ctx);
34812
+ if (!turn || !sessionId) {
34813
+ return void 0;
34814
+ }
34815
+ const metadata = toolMetadataFromTurn(turn);
34816
+ const { rowId: eventId, spanId } = await generateEveIds(
34817
+ "subagent",
34818
+ sessionId,
34819
+ result.callId
34820
+ );
34821
+ const existing = this.toolsByCallKey.get(toolKey3(sessionId, result.callId));
34822
+ if (existing) {
34823
+ return existing;
34824
+ }
34825
+ const span = await this.startEveChildSpan(turn.span, {
34826
+ event: {
34827
+ id: eventId,
34828
+ metadata
34829
+ },
34830
+ name: result.subagentName,
34831
+ spanAttributes: { type: "tool" /* TOOL */ },
34832
+ spanId,
34833
+ startTime: eventTime2(event)
34834
+ });
34835
+ span.log({ metadata });
34836
+ const state = {
34837
+ metadata,
34838
+ span,
34839
+ turnKey: turnKey2(sessionId, event.data.turnId)
34840
+ };
34841
+ this.toolsByCallKey.set(toolKey3(sessionId, result.callId), state);
34842
+ return state;
34843
+ }
34844
+ async startTurnSpan(sessionId, event, ctx, metadata) {
34845
+ const session = isObject(ctx) ? ctx["session"] : void 0;
34846
+ const parent = isObject(session) ? session["parent"] : void 0;
34847
+ const parentTurn = isObject(parent) ? parent["turn"] : void 0;
34848
+ const parentLineage = isObject(parent) && typeof parent["callId"] === "string" && typeof parent["sessionId"] === "string" && isObject(parentTurn) && typeof parentTurn["id"] === "string" ? {
34849
+ callId: parent["callId"],
34850
+ sessionId: parent["sessionId"],
34851
+ turnId: parentTurn["id"]
34852
+ } : void 0;
34853
+ const [{ rowId: eventId, spanId }, rootSpanId, parentSpanId] = await Promise.all([
34854
+ generateEveIds("turn", sessionId, event.data.turnId),
34855
+ deterministicEveId(
34856
+ "eve:root",
34857
+ parentLineage?.sessionId ?? sessionId,
34858
+ parentLineage?.turnId ?? event.data.turnId
34859
+ ),
34860
+ parentLineage ? deterministicEveId(
34861
+ "eve:subagent",
34862
+ parentLineage.sessionId,
34863
+ parentLineage.callId
34864
+ ) : Promise.resolve(void 0)
34865
+ ]);
34866
+ return await this.startEveSpan({
34867
+ event: {
34868
+ id: eventId,
34869
+ metadata
34870
+ },
34871
+ name: "eve.turn",
34872
+ parentSpanIds: parentSpanId ? { rootSpanId, spanId: parentSpanId } : { parentSpanIds: [], rootSpanId },
34873
+ spanAttributes: { type: "task" /* TASK */ },
34874
+ spanId,
34875
+ startTime: eventTime2(event)
34876
+ });
34877
+ }
34878
+ turnForEvent(event, ctx) {
34879
+ const sessionId = sessionIdFromContext(ctx);
34880
+ return sessionId ? this.turnsByKey.get(turnKey2(sessionId, event.data.turnId)) : void 0;
34881
+ }
34882
+ stepForEvent(event, ctx) {
34883
+ return this.turnForEvent(event, ctx)?.stepsByIndex.get(
34884
+ event.data.stepIndex
34885
+ );
34886
+ }
34887
+ finalizeTurn(turn, args) {
34888
+ const { endTime } = args;
34889
+ for (const step of turn.stepsByIndex.values()) {
34890
+ step.span.log({
34891
+ ...step.input !== void 0 ? { input: step.input } : {},
34892
+ metadata: step.metadata,
34893
+ metrics: step.metrics,
34894
+ output: step.output
34895
+ });
34896
+ step.span.end(endTime === void 0 ? void 0 : { endTime });
34897
+ }
34898
+ turn.stepsByIndex.clear();
34899
+ for (const tool of this.toolsByCallKey.values()) {
34900
+ if (tool.turnKey !== turn.key) {
34901
+ continue;
34902
+ }
34903
+ if (tool.endedByTurn) {
34904
+ continue;
34905
+ }
34906
+ tool.span.log({ metadata: tool.metadata });
34907
+ tool.span.end(endTime === void 0 ? void 0 : { endTime });
34908
+ tool.endedByTurn = true;
34909
+ }
34910
+ if (args.error) {
34911
+ turn.span.log({ error: args.error });
34912
+ } else {
34913
+ turn.span.log({
34914
+ metadata: turn.metadata,
34915
+ metrics: turn.metrics,
34916
+ output: turn.output
34917
+ });
34918
+ }
34919
+ turn.span.end(endTime === void 0 ? void 0 : { endTime });
34920
+ this.turnsByKey.delete(turn.key);
34921
+ this.state.update((current) => {
34922
+ const normalized = normalizeEveTraceState(current);
34923
+ return {
34924
+ ...normalized,
34925
+ stepStarts: normalized.stepStarts.filter(
34926
+ (entry) => entry.turnId !== turn.turnId
34927
+ )
34928
+ };
34929
+ });
34930
+ }
34931
+ cleanupSession(sessionId) {
34932
+ const keyPrefix = `${sessionId}:`;
34933
+ for (const key of this.turnsByKey.keys()) {
34934
+ if (key.startsWith(keyPrefix)) {
34935
+ this.turnsByKey.delete(key);
34936
+ }
34937
+ }
34938
+ for (const key of this.toolsByCallKey.keys()) {
34939
+ if (key.startsWith(keyPrefix)) {
34940
+ this.toolsByCallKey.delete(key);
34941
+ }
34942
+ }
34943
+ for (const key of this.completedToolKeys.keys()) {
34944
+ if (key.startsWith(keyPrefix)) {
34945
+ this.completedToolKeys.delete(key);
34946
+ }
34947
+ }
34948
+ this.state.update(() => emptyEveTraceState());
34949
+ }
34950
+ async flushInstrumentation() {
34951
+ try {
34952
+ await flush();
34953
+ return true;
34954
+ } catch (error) {
34955
+ debugLogger.warn("Error in Eve flush instrumentation:", error);
34956
+ return false;
34957
+ }
34958
+ }
34959
+ };
34960
+ function emptyEveTraceState() {
34961
+ return {
34962
+ llmInputs: [],
34963
+ metadata: {},
34964
+ spanReferences: [],
34965
+ stepStarts: []
34966
+ };
34967
+ }
34968
+ function normalizeEveTraceState(state) {
34969
+ if (!isObject(state)) {
34970
+ return emptyEveTraceState();
34971
+ }
34972
+ const metadata = isObject(state["metadata"]) ? state["metadata"] : {};
34973
+ const spanReferences = Array.isArray(state["spanReferences"]) ? state["spanReferences"].flatMap((entry) => {
34974
+ if (!isObject(entry)) {
34975
+ return [];
34976
+ }
34977
+ const exported = entry["exported"];
34978
+ const endTime = entry["endTime"];
34979
+ const rootSpanId = entry["rootSpanId"];
34980
+ const rowId = entry["rowId"];
34981
+ const spanId = entry["spanId"];
34982
+ const startEvent = entry["startEvent"];
34983
+ const startEventCreated = isObject(startEvent) ? startEvent["created"] : void 0;
34984
+ const startEventMetrics = isObject(startEvent) ? startEvent["metrics"] : void 0;
34985
+ const startEventSpanAttributes = isObject(startEvent) ? startEvent["span_attributes"] : void 0;
34986
+ const startEventSpanParents = isObject(startEvent) ? startEvent["span_parents"] : void 0;
34987
+ const normalizedStartEvent = typeof startEventCreated === "string" && isObject(startEventMetrics) && typeof startEventMetrics["start"] === "number" && Number.isFinite(startEventMetrics["start"]) && isObject(startEventSpanAttributes) && Array.isArray(startEventSpanParents) && startEventSpanParents.every(
34988
+ (parent) => typeof parent === "string"
34989
+ ) ? {
34990
+ created: startEventCreated,
34991
+ metrics: { start: startEventMetrics["start"] },
34992
+ span_attributes: { ...startEventSpanAttributes },
34993
+ span_parents: [...startEventSpanParents]
34994
+ } : void 0;
34995
+ return typeof exported === "string" && typeof rootSpanId === "string" && typeof rowId === "string" && typeof spanId === "string" ? [
34996
+ {
34997
+ ...typeof endTime === "number" && Number.isFinite(endTime) ? { endTime } : {},
34998
+ exported,
34999
+ rootSpanId,
35000
+ rowId,
35001
+ spanId,
35002
+ ...normalizedStartEvent ? { startEvent: normalizedStartEvent } : {}
35003
+ }
35004
+ ] : [];
35005
+ }).slice(-MAX_STORED_SPAN_REFERENCES) : [];
35006
+ const llmInputs = Array.isArray(state["llmInputs"]) ? state["llmInputs"].flatMap((entry) => {
35007
+ if (!isObject(entry)) {
35008
+ return [];
35009
+ }
35010
+ const key = entry["key"];
35011
+ const input = entry["input"];
35012
+ return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
35013
+ }).slice(-MAX_STORED_LLM_INPUTS) : [];
35014
+ const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
35015
+ if (!isObject(entry)) {
35016
+ return [];
35017
+ }
35018
+ const ordinal = entry["ordinal"];
35019
+ const open = entry["open"];
35020
+ const stepIndex = entry["stepIndex"];
35021
+ const turnId = entry["turnId"];
35022
+ 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 }] : [];
35023
+ }).slice(-MAX_STORED_STEP_STARTS) : [];
35024
+ return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
35025
+ }
35026
+ function readEveTraceState(state) {
35027
+ try {
35028
+ return normalizeEveTraceState(state.get());
35029
+ } catch {
35030
+ return emptyEveTraceState();
35031
+ }
35032
+ }
35033
+ function captureEveModelInput(state, input) {
35034
+ if (!isObject(input)) {
35035
+ return;
35036
+ }
35037
+ const session = input["session"];
35038
+ const turn = input["turn"];
35039
+ const step = input["step"];
35040
+ if (!isObject(session) || !isObject(turn) || !isObject(step)) {
35041
+ return;
35042
+ }
35043
+ const sessionId = session["id"];
35044
+ const turnId = turn["id"];
35045
+ const stepIndex = step["index"];
35046
+ if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
35047
+ return;
35048
+ }
35049
+ const captured = capturedModelInput(input["modelInput"]);
35050
+ if (!captured) {
35051
+ return;
35052
+ }
35053
+ const key = llmInputKey(sessionId, turnId, stepIndex);
35054
+ state.update((current) => {
35055
+ const normalized = normalizeEveTraceState(current);
35056
+ const llmInputs = [...normalized.llmInputs, { input: captured, key }];
35057
+ return {
35058
+ ...normalized,
35059
+ llmInputs: llmInputs.slice(-MAX_STORED_LLM_INPUTS)
35060
+ };
35061
+ });
35062
+ }
35063
+ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
35064
+ try {
35065
+ const key = llmInputKey(sessionId, turnId, stepIndex);
35066
+ let input;
35067
+ state.update((current) => {
35068
+ const normalized = normalizeEveTraceState(current);
35069
+ const index = normalized.llmInputs.findIndex(
35070
+ (candidate) => candidate.key === key
35071
+ );
35072
+ if (index < 0) {
35073
+ return normalized;
35074
+ }
35075
+ input = normalized.llmInputs[index]?.input;
35076
+ return {
35077
+ ...normalized,
35078
+ llmInputs: normalized.llmInputs.filter(
35079
+ (_, candidateIndex) => candidateIndex !== index
35080
+ )
35081
+ };
35082
+ });
35083
+ return input;
35084
+ } catch (error) {
35085
+ debugLogger.warn("Error in Eve LLM input consumption:", error);
35086
+ return void 0;
35087
+ }
35088
+ }
35089
+ function capturedModelInput(modelInput) {
35090
+ if (!isObject(modelInput)) {
35091
+ return void 0;
35092
+ }
35093
+ const messages = modelInput["messages"];
35094
+ if (!Array.isArray(messages)) {
35095
+ return void 0;
35096
+ }
35097
+ const instructions = modelInput["instructions"];
35098
+ const value = [
35099
+ ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
35100
+ ...messages
35101
+ ];
35102
+ try {
35103
+ const cloned = JSON.parse(JSON.stringify(value));
35104
+ if (!Array.isArray(cloned)) {
35105
+ return void 0;
35106
+ }
35107
+ return cloned;
35108
+ } catch {
35109
+ return void 0;
35110
+ }
35111
+ }
35112
+ function isCapturedModelInput(input) {
35113
+ return Array.isArray(input);
35114
+ }
35115
+ function llmInputKey(sessionId, turnId, stepIndex) {
35116
+ return `${sessionId}\0${turnId}\0${stepIndex}`;
35117
+ }
35118
+ function modelMetadataFromRuntime(runtime) {
35119
+ if (!isObject(runtime)) {
35120
+ return {};
35121
+ }
35122
+ const modelId = runtime["modelId"];
35123
+ return typeof modelId === "string" && !modelId.trim().startsWith("dynamic:") ? modelMetadataFromModelId(modelId) : {};
35124
+ }
35125
+ function modelMetadataFromModelId(modelId) {
35126
+ const normalized = modelId.trim();
35127
+ if (!normalized) {
35128
+ return {};
35129
+ }
35130
+ const slashIndex = normalized.indexOf("/");
35131
+ if (slashIndex > 0 && slashIndex < normalized.length - 1) {
35132
+ return {
35133
+ model: normalized.slice(slashIndex + 1),
35134
+ provider: normalized.slice(0, slashIndex)
35135
+ };
35136
+ }
35137
+ return {
35138
+ model: normalized
35139
+ };
35140
+ }
35141
+ function sessionIdFromContext(ctx) {
35142
+ if (!isObject(ctx)) {
35143
+ return void 0;
35144
+ }
35145
+ const session = ctx["session"];
35146
+ if (!isObject(session)) {
35147
+ return void 0;
35148
+ }
35149
+ const id = session["id"];
35150
+ return typeof id === "string" ? id : void 0;
35151
+ }
35152
+ function toolMetadataFromTurn(turn) {
35153
+ const { model: _model, provider: _provider, ...metadata } = turn.metadata;
35154
+ return metadata;
35155
+ }
35156
+ function isToolCallAction(action) {
35157
+ return isObject(action) && action["kind"] === "tool-call" && typeof action["callId"] === "string" && typeof action["toolName"] === "string" && isObject(action["input"]);
35158
+ }
35159
+ function isLocalSubagentCallAction(action) {
35160
+ return isObject(action) && action["kind"] === "subagent-call" && typeof action["callId"] === "string" && isObject(action["input"]);
35161
+ }
35162
+ function isTraceableActionRequest(action) {
35163
+ return isToolCallAction(action) || isLocalSubagentCallAction(action);
35164
+ }
35165
+ function isToolResult(result) {
35166
+ return isObject(result) && result["kind"] === "tool-result" && typeof result["callId"] === "string" && typeof result["toolName"] === "string";
35167
+ }
35168
+ function isSubagentResult(result) {
35169
+ return isObject(result) && result["kind"] === "subagent-result" && typeof result["callId"] === "string" && typeof result["subagentName"] === "string";
35170
+ }
35171
+ function normalizedFinishReason(finishReason) {
35172
+ switch (finishReason) {
35173
+ case "content-filter":
35174
+ return "content_filter";
35175
+ case "tool-calls":
35176
+ return "tool_calls";
35177
+ default:
35178
+ return finishReason;
35179
+ }
35180
+ }
35181
+ function errorFromMessage(message, code, details) {
35182
+ const error = new Error(`${code}: ${message}`);
35183
+ if (details !== void 0) {
35184
+ error.cause = details;
35185
+ }
35186
+ return error;
35187
+ }
35188
+ function actionResultError(error, output) {
35189
+ if (error) {
35190
+ return errorFromMessage(error.message, error.code);
35191
+ }
35192
+ const result = new Error("Eve action failed");
35193
+ if (output !== void 0) {
35194
+ result.cause = output;
35195
+ }
35196
+ return result;
35197
+ }
35198
+ function eventTime2(event) {
35199
+ if (!event.meta?.at) {
35200
+ return void 0;
35201
+ }
35202
+ const timestamp = Date.parse(event.meta.at);
35203
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
35204
+ }
35205
+ function turnKey2(sessionId, turnId) {
35206
+ return `${sessionId}:${turnId}`;
35207
+ }
35208
+ function toolKey3(sessionId, callId) {
35209
+ return `${sessionId}:${callId}`;
35210
+ }
35211
+ async function generateEveIds(kind, ...parts) {
35212
+ const [rowId, spanId] = await Promise.all([
35213
+ deterministicEveId(`eve:row:${kind}`, ...parts),
35214
+ deterministicEveId(`eve:${kind}`, ...parts)
35215
+ ]);
35216
+ return { rowId, spanId };
35217
+ }
35218
+ async function deterministicEveId(...parts) {
35219
+ const data = new TextEncoder().encode(
35220
+ parts.map((part) => `${part.length}:${part}`).join("\0")
35221
+ );
35222
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", data);
35223
+ const bytes = Array.from(new Uint8Array(digest, 0, 16));
35224
+ const hex = bytes.map((byte) => byte.toString(16).padStart(2, "0")).join("");
35225
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
35226
+ }
35227
+
33083
35228
  // src/typed-instrumentation-helpers.ts
33084
35229
  var TypedApplyProxy = Proxy;
33085
35230
 
@@ -33242,6 +35387,15 @@ function modelMetrics(attributes) {
33242
35387
  }
33243
35388
  return Object.keys(out).length > 0 ? out : void 0;
33244
35389
  }
35390
+ function timeToFirstTokenSeconds(attributes, spanStartSeconds) {
35391
+ if (!isObject(attributes)) return void 0;
35392
+ if (spanStartSeconds === void 0) return void 0;
35393
+ const raw = attributes.completionStartTime;
35394
+ const completionStart = raw instanceof Date || typeof raw === "string" || typeof raw === "number" ? epochSeconds(raw) : void 0;
35395
+ if (completionStart === void 0) return void 0;
35396
+ const ttft = completionStart - spanStartSeconds;
35397
+ return Number.isFinite(ttft) && ttft >= 0 ? ttft : void 0;
35398
+ }
33245
35399
  function buildMetadata(exported) {
33246
35400
  const out = {};
33247
35401
  if (exported.entityId !== void 0) out.entity_id = exported.entityId;
@@ -33311,7 +35465,13 @@ var BraintrustObservabilityExporter = class {
33311
35465
  const args = {
33312
35466
  name: exported.name,
33313
35467
  spanAttributes: { type: spanTypeFor(exported.type) },
33314
- startTime: epochSeconds(exported.startTime)
35468
+ startTime: epochSeconds(exported.startTime),
35469
+ // Use the Mastra span id as the Braintrust row id so that
35470
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35471
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
35472
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35473
+ // external caller could know.
35474
+ event: { id: exported.id }
33315
35475
  };
33316
35476
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
33317
35477
  if (!this.capturedParent) {
@@ -33360,8 +35520,15 @@ var BraintrustObservabilityExporter = class {
33360
35520
  event.metadata = metadata;
33361
35521
  }
33362
35522
  const metrics = modelMetrics(exported.attributes);
33363
- if (metrics) {
33364
- event.metrics = metrics;
35523
+ const ttft = timeToFirstTokenSeconds(
35524
+ exported.attributes,
35525
+ epochSeconds(exported.startTime)
35526
+ );
35527
+ if (metrics || ttft !== void 0) {
35528
+ event.metrics = {
35529
+ ...metrics ?? {},
35530
+ ...ttft !== void 0 ? { time_to_first_token: ttft } : {}
35531
+ };
33365
35532
  }
33366
35533
  if (Object.keys(event).length > 0) {
33367
35534
  record.span.log(event);
@@ -34240,17 +36407,17 @@ function wrapGenkit(genkit) {
34240
36407
  console.warn("Unsupported Genkit object. Not wrapping.");
34241
36408
  return genkit;
34242
36409
  }
34243
- function isRecord2(value) {
36410
+ function isRecord3(value) {
34244
36411
  return typeof value === "object" && value !== null;
34245
36412
  }
34246
36413
  function isPropertyBag(value) {
34247
- return isRecord2(value) || typeof value === "function";
36414
+ return isRecord3(value) || typeof value === "function";
34248
36415
  }
34249
36416
  function hasFunction(value, methodName) {
34250
36417
  return isPropertyBag(value) && methodName in value && typeof value[methodName] === "function";
34251
36418
  }
34252
36419
  function isGenkitInstance(value) {
34253
- return isRecord2(value) && (hasFunction(value, "generate") || hasFunction(value, "generateStream") || hasFunction(value, "defineFlow") || hasFunction(value, "defineTool"));
36420
+ return isRecord3(value) && (hasFunction(value, "generate") || hasFunction(value, "generateStream") || hasFunction(value, "defineFlow") || hasFunction(value, "defineTool"));
34254
36421
  }
34255
36422
  function isGenkitModule(value) {
34256
36423
  return hasFunction(value, "genkit");
@@ -34304,7 +36471,7 @@ function patchGenkitRegistry(instance) {
34304
36471
  patchGenkitRegistryConstructor(registry2);
34305
36472
  }
34306
36473
  function patchGenkitRegistryLookup(registry2) {
34307
- if (!isRecord2(registry2) || hasRegistryPatchedFlag(registry2) || !hasFunction(registry2, "lookupAction")) {
36474
+ if (!isRecord3(registry2) || hasRegistryPatchedFlag(registry2) || !hasFunction(registry2, "lookupAction")) {
34308
36475
  return;
34309
36476
  }
34310
36477
  const originalLookupAction = registry2.lookupAction;
@@ -34330,7 +36497,7 @@ function patchGenkitRegistryLookup(registry2) {
34330
36497
  }
34331
36498
  }
34332
36499
  function patchGenkitRegistryConstructor(registry2) {
34333
- if (!isRecord2(registry2)) {
36500
+ if (!isRecord3(registry2)) {
34334
36501
  return;
34335
36502
  }
34336
36503
  const constructor = registry2.constructor;
@@ -34346,7 +36513,7 @@ function patchGenkitRegistryConstructor(registry2) {
34346
36513
  configurable: true,
34347
36514
  value: (...args) => {
34348
36515
  const childRegistry = originalWithParent.apply(constructor, args);
34349
- if (args.some((arg) => isRecord2(arg) && hasRegistryPatchedFlag(arg))) {
36516
+ if (args.some((arg) => isRecord3(arg) && hasRegistryPatchedFlag(arg))) {
34350
36517
  patchGenkitRegistryLookup(childRegistry);
34351
36518
  patchGenkitRegistryConstructor(childRegistry);
34352
36519
  }
@@ -34445,7 +36612,7 @@ function hasRegistryConstructorPatchedFlag(value) {
34445
36612
  );
34446
36613
  }
34447
36614
  function isPromiseLike2(value) {
34448
- return isRecord2(value) && "then" in value && typeof value.then === "function";
36615
+ return isRecord3(value) && "then" in value && typeof value.then === "function";
34449
36616
  }
34450
36617
 
34451
36618
  // src/wrappers/huggingface.ts
@@ -34808,14 +36975,14 @@ function wrapMistral(mistral) {
34808
36975
  console.warn("Unsupported Mistral library. Not wrapping.");
34809
36976
  return mistral;
34810
36977
  }
34811
- function isRecord3(value) {
36978
+ function isRecord4(value) {
34812
36979
  return typeof value === "object" && value !== null;
34813
36980
  }
34814
36981
  function hasFunction3(value, methodName) {
34815
- return isRecord3(value) && methodName in value && typeof value[methodName] === "function";
36982
+ return isRecord4(value) && methodName in value && typeof value[methodName] === "function";
34816
36983
  }
34817
36984
  function isSupportedMistralClient(value) {
34818
- if (!isRecord3(value)) {
36985
+ if (!isRecord4(value)) {
34819
36986
  return false;
34820
36987
  }
34821
36988
  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);
@@ -34999,14 +37166,14 @@ function wrapCohere(cohere) {
34999
37166
  return cohere;
35000
37167
  }
35001
37168
  var cohereProxyCache = /* @__PURE__ */ new WeakMap();
35002
- function isRecord4(value) {
37169
+ function isRecord5(value) {
35003
37170
  return typeof value === "object" && value !== null;
35004
37171
  }
35005
37172
  function hasFunction4(value, methodName) {
35006
- return isRecord4(value) && methodName in value && typeof value[methodName] === "function";
37173
+ return isRecord5(value) && methodName in value && typeof value[methodName] === "function";
35007
37174
  }
35008
37175
  function isSupportedCohereClient(value) {
35009
- if (!isRecord4(value)) {
37176
+ if (!isRecord5(value)) {
35010
37177
  return false;
35011
37178
  }
35012
37179
  return hasFunction4(value, "chat") || hasFunction4(value, "chatStream") || hasFunction4(value, "embed") || hasFunction4(value, "rerank");
@@ -35066,20 +37233,20 @@ function wrapGroq(groq) {
35066
37233
  console.warn("Unsupported Groq library. Not wrapping.");
35067
37234
  return groq;
35068
37235
  }
35069
- function isRecord5(value) {
37236
+ function isRecord6(value) {
35070
37237
  return typeof value === "object" && value !== null;
35071
37238
  }
35072
37239
  function hasFunction5(value, methodName) {
35073
- return isRecord5(value) && methodName in value && typeof value[methodName] === "function";
37240
+ return isRecord6(value) && methodName in value && typeof value[methodName] === "function";
35074
37241
  }
35075
37242
  function hasChat2(value) {
35076
- return isRecord5(value) && isRecord5(value.completions) && hasFunction5(value.completions, "create");
37243
+ return isRecord6(value) && isRecord6(value.completions) && hasFunction5(value.completions, "create");
35077
37244
  }
35078
37245
  function hasEmbeddings2(value) {
35079
37246
  return hasFunction5(value, "create");
35080
37247
  }
35081
37248
  function isSupportedGroqClient(value) {
35082
- return isRecord5(value) && (value.chat !== void 0 && hasChat2(value.chat) || value.embeddings !== void 0 && hasEmbeddings2(value.embeddings));
37249
+ return isRecord6(value) && (value.chat !== void 0 && hasChat2(value.chat) || value.embeddings !== void 0 && hasEmbeddings2(value.embeddings));
35083
37250
  }
35084
37251
  function groqProxy(groq) {
35085
37252
  const privateMethodWorkaroundCache = /* @__PURE__ */ new WeakMap();
@@ -35162,11 +37329,11 @@ var BEDROCK_RUNTIME_OPERATION_METHODS = /* @__PURE__ */ new Set([
35162
37329
  "invokeModelWithBidirectionalStream",
35163
37330
  "invokeModelWithResponseStream"
35164
37331
  ]);
35165
- function isRecord6(value) {
37332
+ function isRecord7(value) {
35166
37333
  return typeof value === "object" && value !== null;
35167
37334
  }
35168
37335
  function isSupportedBedrockRuntimeClient(value) {
35169
- return isRecord6(value) && typeof value.send === "function";
37336
+ return isRecord7(value) && typeof value.send === "function";
35170
37337
  }
35171
37338
  function bedrockRuntimeProxy(client) {
35172
37339
  const cached = bedrockRuntimeProxyCache.get(client);
@@ -35285,6 +37452,271 @@ function wrappedResumeSession(client) {
35285
37452
  );
35286
37453
  }
35287
37454
 
37455
+ // src/wrappers/langsmith.ts
37456
+ var WRAPPED_CLIENT_CLASS = /* @__PURE__ */ Symbol.for(
37457
+ "braintrust.langsmith.wrapped-client-class"
37458
+ );
37459
+ var WRAPPED_CLIENT_INSTANCE = /* @__PURE__ */ Symbol.for(
37460
+ "braintrust.langsmith.wrapped-client-instance"
37461
+ );
37462
+ var WRAPPED_CLIENT_NAMESPACE = /* @__PURE__ */ Symbol.for(
37463
+ "braintrust.langsmith.wrapped-client-namespace"
37464
+ );
37465
+ var WRAPPED_RUN_TREE_CLASS = /* @__PURE__ */ Symbol.for(
37466
+ "braintrust.langsmith.wrapped-run-tree-class"
37467
+ );
37468
+ var WRAPPED_RUN_TREE_INSTANCE = /* @__PURE__ */ Symbol.for(
37469
+ "braintrust.langsmith.wrapped-run-tree-instance"
37470
+ );
37471
+ var WRAPPED_RUN_TREES_NAMESPACE = /* @__PURE__ */ Symbol.for(
37472
+ "braintrust.langsmith.wrapped-run-trees-namespace"
37473
+ );
37474
+ var WRAPPED_TRACEABLE = /* @__PURE__ */ Symbol.for("braintrust.langsmith.wrapped-traceable");
37475
+ var WRAPPED_TRACEABLE_NAMESPACE = /* @__PURE__ */ Symbol.for(
37476
+ "braintrust.langsmith.wrapped-traceable-namespace"
37477
+ );
37478
+ function wrapLangSmithTraceable(namespace) {
37479
+ return wrapNamespaceExport(
37480
+ namespace,
37481
+ "traceable",
37482
+ WRAPPED_TRACEABLE_NAMESPACE,
37483
+ (value) => wrapTraceable(value)
37484
+ );
37485
+ }
37486
+ function wrapLangSmithRunTrees(namespace) {
37487
+ return wrapNamespaceExport(
37488
+ namespace,
37489
+ "RunTree",
37490
+ WRAPPED_RUN_TREES_NAMESPACE,
37491
+ (value) => wrapRunTreeClass(value)
37492
+ );
37493
+ }
37494
+ function wrapLangSmithClient(namespace) {
37495
+ return wrapNamespaceExport(
37496
+ namespace,
37497
+ "Client",
37498
+ WRAPPED_CLIENT_NAMESPACE,
37499
+ (value) => wrapClientClass(value)
37500
+ );
37501
+ }
37502
+ function wrapNamespaceExport(namespace, exportName, marker, wrap2) {
37503
+ if (!namespace || typeof namespace !== "object") {
37504
+ return namespace;
37505
+ }
37506
+ const candidate = namespace;
37507
+ if (candidate[marker] === true) {
37508
+ return namespace;
37509
+ }
37510
+ if (typeof candidate[exportName] !== "function") {
37511
+ console.warn(
37512
+ `Unsupported LangSmith ${exportName} namespace. Not wrapping.`
37513
+ );
37514
+ return namespace;
37515
+ }
37516
+ const target = isModuleNamespace5(namespace) ? Object.setPrototypeOf({}, namespace) : candidate;
37517
+ const moduleNamespace = target !== candidate;
37518
+ let wrappedExport;
37519
+ return new Proxy(target, {
37520
+ get(target2, prop, receiver) {
37521
+ if (prop === marker) {
37522
+ return true;
37523
+ }
37524
+ const value = Reflect.get(target2, prop, receiver);
37525
+ if (prop !== exportName || typeof value !== "function") {
37526
+ return value;
37527
+ }
37528
+ wrappedExport ??= wrap2(value);
37529
+ return wrappedExport;
37530
+ },
37531
+ getOwnPropertyDescriptor(target2, prop) {
37532
+ const descriptor = Reflect.getOwnPropertyDescriptor(target2, prop);
37533
+ if (descriptor || !moduleNamespace) {
37534
+ return descriptor;
37535
+ }
37536
+ const namespaceDescriptor = Reflect.getOwnPropertyDescriptor(
37537
+ candidate,
37538
+ prop
37539
+ );
37540
+ return namespaceDescriptor ? { ...namespaceDescriptor, configurable: true } : void 0;
37541
+ },
37542
+ has(target2, prop) {
37543
+ return Reflect.has(target2, prop) || moduleNamespace && prop in candidate;
37544
+ },
37545
+ ownKeys(target2) {
37546
+ return moduleNamespace ? Reflect.ownKeys(candidate) : Reflect.ownKeys(target2);
37547
+ }
37548
+ });
37549
+ }
37550
+ function isModuleNamespace5(value) {
37551
+ if (!value || typeof value !== "object") {
37552
+ return false;
37553
+ }
37554
+ if (value.constructor?.name === "Module") {
37555
+ return true;
37556
+ }
37557
+ const firstKey = Object.keys(value)[0];
37558
+ if (!firstKey) {
37559
+ return false;
37560
+ }
37561
+ const descriptor = Object.getOwnPropertyDescriptor(value, firstKey);
37562
+ return descriptor ? !descriptor.configurable && !descriptor.writable : false;
37563
+ }
37564
+ function wrapTraceable(traceable2) {
37565
+ if (traceable2[WRAPPED_TRACEABLE]) {
37566
+ return traceable2;
37567
+ }
37568
+ return new Proxy(traceable2, {
37569
+ get(target, prop, receiver) {
37570
+ if (prop === WRAPPED_TRACEABLE) {
37571
+ return true;
37572
+ }
37573
+ return Reflect.get(target, prop, receiver);
37574
+ },
37575
+ apply(target, thisArg, argArray) {
37576
+ const [fn, rawConfig] = argArray;
37577
+ const config = rawConfig && typeof rawConfig === "object" ? rawConfig : void 0;
37578
+ const originalOnEnd = config?.on_end;
37579
+ const wrappedConfig = {
37580
+ ...config,
37581
+ on_end(runTree) {
37582
+ publishRunUpdate(runTree);
37583
+ if (originalOnEnd) {
37584
+ Reflect.apply(originalOnEnd, config, [runTree]);
37585
+ }
37586
+ }
37587
+ };
37588
+ return Reflect.apply(target, thisArg, [fn, wrappedConfig]);
37589
+ }
37590
+ });
37591
+ }
37592
+ function wrapRunTreeClass(RunTree) {
37593
+ if (RunTree[WRAPPED_RUN_TREE_CLASS]) {
37594
+ return RunTree;
37595
+ }
37596
+ return new Proxy(RunTree, {
37597
+ get(target, prop, receiver) {
37598
+ if (prop === WRAPPED_RUN_TREE_CLASS) {
37599
+ return true;
37600
+ }
37601
+ const value = Reflect.get(target, prop, receiver);
37602
+ return typeof value === "function" ? value.bind(target) : value;
37603
+ },
37604
+ construct(target, args, newTarget) {
37605
+ return wrapRunTreeInstance(Reflect.construct(target, args, newTarget));
37606
+ }
37607
+ });
37608
+ }
37609
+ function wrapRunTreeInstance(runTree) {
37610
+ if (runTree[WRAPPED_RUN_TREE_INSTANCE]) {
37611
+ return runTree;
37612
+ }
37613
+ return new Proxy(runTree, {
37614
+ get(target, prop, receiver) {
37615
+ if (prop === WRAPPED_RUN_TREE_INSTANCE) {
37616
+ return true;
37617
+ }
37618
+ const value = Reflect.get(target, prop, receiver);
37619
+ if (typeof value !== "function") {
37620
+ return value;
37621
+ }
37622
+ let wrapped;
37623
+ if (prop === "createChild") {
37624
+ wrapped = (...args) => wrapRunTreeInstance(Reflect.apply(value, target, args));
37625
+ } else if (prop === "postRun") {
37626
+ const method = value;
37627
+ wrapped = (...args) => langSmithChannels.createRun.tracePromise(
37628
+ () => Reflect.apply(method, target, args),
37629
+ { arguments: [target] }
37630
+ );
37631
+ } else if (prop === "patchRun") {
37632
+ const method = value;
37633
+ wrapped = (...args) => langSmithChannels.updateRun.tracePromise(
37634
+ () => Reflect.apply(method, target, args),
37635
+ {
37636
+ arguments: [
37637
+ typeof target.id === "string" ? target.id : "",
37638
+ target
37639
+ ]
37640
+ }
37641
+ );
37642
+ } else {
37643
+ wrapped = value.bind(target);
37644
+ }
37645
+ return wrapped;
37646
+ }
37647
+ });
37648
+ }
37649
+ function wrapClientClass(Client) {
37650
+ if (Client[WRAPPED_CLIENT_CLASS]) {
37651
+ return Client;
37652
+ }
37653
+ return new Proxy(Client, {
37654
+ get(target, prop, receiver) {
37655
+ if (prop === WRAPPED_CLIENT_CLASS) {
37656
+ return true;
37657
+ }
37658
+ const value = Reflect.get(target, prop, receiver);
37659
+ return typeof value === "function" ? value.bind(target) : value;
37660
+ },
37661
+ construct(target, args, newTarget) {
37662
+ return wrapClientInstance(Reflect.construct(target, args, newTarget));
37663
+ }
37664
+ });
37665
+ }
37666
+ function wrapClientInstance(client) {
37667
+ if (client[WRAPPED_CLIENT_INSTANCE]) {
37668
+ return client;
37669
+ }
37670
+ return new Proxy(client, {
37671
+ get(target, prop, receiver) {
37672
+ if (prop === WRAPPED_CLIENT_INSTANCE) {
37673
+ return true;
37674
+ }
37675
+ const value = Reflect.get(target, prop, receiver);
37676
+ if (typeof value !== "function") {
37677
+ return value;
37678
+ }
37679
+ let wrapped;
37680
+ if (prop === "createRun") {
37681
+ const method = value;
37682
+ wrapped = (...args) => langSmithChannels.createRun.tracePromise(
37683
+ () => Reflect.apply(method, target, args),
37684
+ { arguments: args }
37685
+ );
37686
+ } else if (prop === "updateRun") {
37687
+ const method = value;
37688
+ wrapped = (...args) => langSmithChannels.updateRun.tracePromise(
37689
+ () => Reflect.apply(method, target, args),
37690
+ { arguments: args }
37691
+ );
37692
+ } else if (prop === "batchIngestRuns") {
37693
+ const method = value;
37694
+ wrapped = (...args) => langSmithChannels.batchIngestRuns.tracePromise(
37695
+ () => Reflect.apply(method, target, args),
37696
+ { arguments: args }
37697
+ );
37698
+ } else {
37699
+ wrapped = value.bind(target);
37700
+ }
37701
+ return wrapped;
37702
+ }
37703
+ });
37704
+ }
37705
+ function publishRunUpdate(runTree) {
37706
+ if (!runTree || typeof runTree.id !== "string" || !runTree.id) {
37707
+ return;
37708
+ }
37709
+ try {
37710
+ void langSmithChannels.updateRun.tracePromise(() => Promise.resolve(void 0), {
37711
+ arguments: [runTree.id, runTree]
37712
+ }).catch((error) => {
37713
+ debugLogger.error("LangSmith traceable instrumentation failed:", error);
37714
+ });
37715
+ } catch (error) {
37716
+ debugLogger.error("LangSmith traceable instrumentation failed:", error);
37717
+ }
37718
+ }
37719
+
35288
37720
  // src/wrappers/vitest/context-manager.ts
35289
37721
  var VitestContextManager = class {
35290
37722
  /**