braintrust 3.22.0 → 3.23.1

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 +43 -0
  2. package/dev/dist/index.d.mts +1 -1
  3. package/dev/dist/index.d.ts +1 -1
  4. package/dev/dist/index.js +1354 -654
  5. package/dev/dist/index.mjs +736 -36
  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 +149 -20
  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 +11 -13
  28. package/dist/browser.d.ts +11 -13
  29. package/dist/browser.js +1098 -205
  30. package/dist/browser.mjs +1098 -205
  31. package/dist/{chunk-KMGUTPB7.mjs → chunk-36IPYKMG.mjs} +20 -1
  32. package/dist/{chunk-MWVVR5LR.js → chunk-B6ZQIAK3.js} +1506 -822
  33. package/dist/{chunk-BFGIH2ZJ.js → chunk-CDIKAHDZ.js} +21 -2
  34. package/dist/{chunk-ZG2O3XVF.mjs → chunk-RBXD2KYN.mjs} +719 -35
  35. package/dist/cli.js +737 -37
  36. package/dist/edge-light.d.mts +1 -1
  37. package/dist/edge-light.d.ts +1 -1
  38. package/dist/edge-light.js +1098 -205
  39. package/dist/edge-light.mjs +1098 -205
  40. package/dist/index.d.mts +11 -13
  41. package/dist/index.d.ts +11 -13
  42. package/dist/index.js +786 -593
  43. package/dist/index.mjs +346 -153
  44. package/dist/instrumentation/index.d.mts +3 -11
  45. package/dist/instrumentation/index.d.ts +3 -11
  46. package/dist/instrumentation/index.js +788 -183
  47. package/dist/instrumentation/index.mjs +788 -183
  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 +1098 -205
  53. package/dist/workerd.mjs +1098 -205
  54. package/package.json +1 -1
@@ -18,6 +18,7 @@ import {
18
18
  isInstrumentationIntegrationDisabled,
19
19
  isomorph_default,
20
20
  langChainChannels,
21
+ langSmithChannels,
21
22
  mistralChannels,
22
23
  openAIAgentsCoreChannels,
23
24
  openAIChannels,
@@ -30,7 +31,7 @@ import {
30
31
  smithyClientChannels,
31
32
  smithyCoreChannels,
32
33
  strandsAgentSDKChannels
33
- } from "./chunk-KMGUTPB7.mjs";
34
+ } from "./chunk-36IPYKMG.mjs";
34
35
 
35
36
  // src/id-gen.ts
36
37
  import { v4 as uuidv4 } from "uuid";
@@ -4194,7 +4195,7 @@ var DiskCache = class {
4194
4195
  }
4195
4196
  };
4196
4197
 
4197
- // src/prompt-cache/lru-cache.ts
4198
+ // src/lru-cache.ts
4198
4199
  var LRUCache = class {
4199
4200
  cache;
4200
4201
  maxSize;
@@ -5436,25 +5437,46 @@ var HTTPConnection = class _HTTPConnection {
5436
5437
  })
5437
5438
  );
5438
5439
  }
5439
- async post(path2, params, config) {
5440
+ async post(path2, params, config, retries = 0) {
5440
5441
  const { headers, ...rest } = config || {};
5441
5442
  const this_fetch = this.fetch;
5442
5443
  const this_base_url = this.base_url;
5443
5444
  const this_headers = this.headers;
5444
- return await checkResponse(
5445
- await this_fetch(_urljoin(this_base_url, path2), {
5446
- method: "POST",
5447
- headers: {
5448
- Accept: "application/json",
5449
- "Content-Type": "application/json",
5450
- ...this_headers,
5451
- ...headers
5452
- },
5453
- body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
5454
- keepalive: true,
5455
- ...rest
5456
- })
5457
- );
5445
+ const tries = retries + 1;
5446
+ for (let i = 0; i < tries; i++) {
5447
+ try {
5448
+ return await checkResponse(
5449
+ await this_fetch(_urljoin(this_base_url, path2), {
5450
+ method: "POST",
5451
+ headers: {
5452
+ Accept: "application/json",
5453
+ "Content-Type": "application/json",
5454
+ ...this_headers,
5455
+ ...headers
5456
+ },
5457
+ body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
5458
+ keepalive: true,
5459
+ ...rest
5460
+ })
5461
+ );
5462
+ } catch (error) {
5463
+ if (config?.signal?.aborted) {
5464
+ throw getAbortReason(config.signal);
5465
+ }
5466
+ if (i === tries - 1 || !isRetryableHTTPError(error)) {
5467
+ throw error;
5468
+ }
5469
+ debugLogger.debug(
5470
+ `Retrying API request ${path2} after ${formatHTTPError(error)}`
5471
+ );
5472
+ const sleepTimeMs = HTTP_RETRY_BASE_SLEEP_TIME_S * 1e3 * 2 ** i + Math.random() * HTTP_RETRY_JITTER_MS;
5473
+ debugLogger.info(
5474
+ `Sleeping for ${sleepTimeMs}ms before retrying API request`
5475
+ );
5476
+ await waitForRetry(sleepTimeMs, config?.signal);
5477
+ }
5478
+ }
5479
+ throw new Error("Unexpected retry state");
5458
5480
  }
5459
5481
  async get_json(object_type, args = void 0, retries = 0) {
5460
5482
  const tries = retries + 1;
@@ -6551,6 +6573,45 @@ var TestBackgroundLogger = class {
6551
6573
  };
6552
6574
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
6553
6575
  var HTTP_RETRY_BASE_SLEEP_TIME_S = 1;
6576
+ var HTTP_RETRY_JITTER_MS = 200;
6577
+ var BTQL_HTTP_RETRIES = 3;
6578
+ var RETRYABLE_HTTP_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
6579
+ function isRetryableHTTPError(error) {
6580
+ return !(error instanceof FailedHTTPResponse) || RETRYABLE_HTTP_STATUS_CODES.has(error.status);
6581
+ }
6582
+ function formatHTTPError(error) {
6583
+ if (error instanceof FailedHTTPResponse) {
6584
+ return `${error.status} ${error.text}`;
6585
+ }
6586
+ return error instanceof Error ? error.message : String(error);
6587
+ }
6588
+ function getAbortReason(signal) {
6589
+ return signal.reason ?? new Error("Request aborted");
6590
+ }
6591
+ async function waitForRetry(delayMs, signal) {
6592
+ if (!signal) {
6593
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
6594
+ return;
6595
+ }
6596
+ if (signal.aborted) {
6597
+ throw getAbortReason(signal);
6598
+ }
6599
+ await new Promise((resolve, reject) => {
6600
+ const onAbort = () => {
6601
+ clearTimeout(timeout);
6602
+ signal.removeEventListener("abort", onAbort);
6603
+ reject(getAbortReason(signal));
6604
+ };
6605
+ const timeout = setTimeout(() => {
6606
+ signal.removeEventListener("abort", onAbort);
6607
+ resolve();
6608
+ }, delayMs);
6609
+ signal.addEventListener("abort", onAbort, { once: true });
6610
+ if (signal.aborted) {
6611
+ onAbort();
6612
+ }
6613
+ });
6614
+ }
6554
6615
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6555
6616
  apiConn;
6556
6617
  queue;
@@ -8289,15 +8350,15 @@ function parentSpanIdsUsable(spanId, rootSpanId) {
8289
8350
  return Boolean(spanId) && Boolean(rootSpanId);
8290
8351
  }
8291
8352
  function logError(span, error) {
8292
- let errorMessage = "<error>";
8353
+ let errorMessage2 = "<error>";
8293
8354
  let stackTrace = "";
8294
8355
  if (error instanceof Error) {
8295
- errorMessage = error.message;
8356
+ errorMessage2 = error.message;
8296
8357
  stackTrace = error.stack || "";
8297
8358
  } else {
8298
- errorMessage = String(error);
8359
+ errorMessage2 = String(error);
8299
8360
  }
8300
- span.log({ error: `${errorMessage}
8361
+ span.log({ error: `${errorMessage2}
8301
8362
 
8302
8363
  ${stackTrace}` });
8303
8364
  }
@@ -8884,7 +8945,8 @@ var ObjectFetcher = class {
8884
8945
  version: this.pinnedVersion
8885
8946
  } : {}
8886
8947
  },
8887
- { headers: { "Accept-Encoding": "gzip" } }
8948
+ { headers: { "Accept-Encoding": "gzip" } },
8949
+ BTQL_HTTP_RETRIES
8888
8950
  );
8889
8951
  const respJson = await resp.json();
8890
8952
  const mutate = this.mutateRecord;
@@ -10608,7 +10670,8 @@ async function getPromptVersions(projectId, promptId) {
10608
10670
  use_columnstore: false,
10609
10671
  brainstore_realtime: true
10610
10672
  },
10611
- { headers: { "Accept-Encoding": "gzip" } }
10673
+ { headers: { "Accept-Encoding": "gzip" } },
10674
+ BTQL_HTTP_RETRIES
10612
10675
  );
10613
10676
  if (!response.ok) {
10614
10677
  throw new Error(
@@ -19066,8 +19129,6 @@ function filterSerializableOptions(options) {
19066
19129
  "additionalDirectories",
19067
19130
  "permissionMode",
19068
19131
  "debug",
19069
- "apiKey",
19070
- "apiKeySource",
19071
19132
  "agentName",
19072
19133
  "instructions"
19073
19134
  ];
@@ -28557,6 +28618,430 @@ function isBraintrustHandler(handler) {
28557
28618
  return Reflect.get(handler, "name") === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
28558
28619
  }
28559
28620
 
28621
+ // src/instrumentation/plugins/langsmith-plugin.ts
28622
+ var MAX_COMPLETED_RUNS = 1e4;
28623
+ var BLOCKED_METADATA_KEYS = /* @__PURE__ */ new Set([
28624
+ "__proto__",
28625
+ "constructor",
28626
+ "prototype",
28627
+ "usage_metadata"
28628
+ ]);
28629
+ var BLOCKED_METADATA_PREFIXES = ["__pregel_", "langgraph_", "lc_"];
28630
+ var LLM_SETTING_KEYS = [
28631
+ "temperature",
28632
+ "top_p",
28633
+ "max_tokens",
28634
+ "frequency_penalty",
28635
+ "presence_penalty",
28636
+ "stop",
28637
+ "response_format"
28638
+ ];
28639
+ var LangSmithPlugin = class extends BasePlugin {
28640
+ activeRuns = /* @__PURE__ */ new Map();
28641
+ completedRuns = new LRUCache({
28642
+ max: MAX_COMPLETED_RUNS
28643
+ });
28644
+ skipLangChainRuns;
28645
+ constructor(options = {}) {
28646
+ super();
28647
+ this.skipLangChainRuns = options.skipLangChainRuns ?? true;
28648
+ }
28649
+ onEnable() {
28650
+ const createChannel = langSmithChannels.createRun.tracingChannel();
28651
+ const createHandlers = {
28652
+ start: (event) => {
28653
+ this.containLifecycleFailure("createRun", () => {
28654
+ this.processCreate(event.arguments[0]);
28655
+ });
28656
+ }
28657
+ };
28658
+ createChannel.subscribe(createHandlers);
28659
+ this.unsubscribers.push(() => createChannel.unsubscribe(createHandlers));
28660
+ const updateChannel = langSmithChannels.updateRun.tracingChannel();
28661
+ const updateHandlers = {
28662
+ start: (event) => {
28663
+ this.containLifecycleFailure("updateRun", () => {
28664
+ this.processUpdate(event.arguments[0], event.arguments[1]);
28665
+ });
28666
+ }
28667
+ };
28668
+ updateChannel.subscribe(updateHandlers);
28669
+ this.unsubscribers.push(() => updateChannel.unsubscribe(updateHandlers));
28670
+ const batchChannel = langSmithChannels.batchIngestRuns.tracingChannel();
28671
+ const batchHandlers = {
28672
+ start: (event) => {
28673
+ this.containLifecycleFailure("batchIngestRuns", () => {
28674
+ this.processBatch(event.arguments[0]);
28675
+ });
28676
+ }
28677
+ };
28678
+ batchChannel.subscribe(batchHandlers);
28679
+ this.unsubscribers.push(() => batchChannel.unsubscribe(batchHandlers));
28680
+ }
28681
+ onDisable() {
28682
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
28683
+ for (const { span } of this.activeRuns.values()) {
28684
+ span.end();
28685
+ }
28686
+ this.activeRuns.clear();
28687
+ this.completedRuns.clear();
28688
+ }
28689
+ processBatch(batch) {
28690
+ if (!isRecord2(batch)) {
28691
+ return;
28692
+ }
28693
+ const creates = ownValue(batch, "runCreates");
28694
+ if (Array.isArray(creates)) {
28695
+ const parentFirst = [...creates].sort((left, right) => {
28696
+ const leftId = stringValue2(ownValue(left, "id"));
28697
+ const rightId = stringValue2(ownValue(right, "id"));
28698
+ const leftParent = stringValue2(ownValue(left, "parent_run_id"));
28699
+ const rightParent = stringValue2(ownValue(right, "parent_run_id"));
28700
+ if (leftParent && leftParent === rightId) {
28701
+ return 1;
28702
+ }
28703
+ if (rightParent && rightParent === leftId) {
28704
+ return -1;
28705
+ }
28706
+ return dottedOrderDepth(left) - dottedOrderDepth(right);
28707
+ });
28708
+ for (const run of parentFirst) {
28709
+ this.containLifecycleFailure("batchIngestRuns create", () => {
28710
+ this.processCreate(run);
28711
+ });
28712
+ }
28713
+ }
28714
+ const updates = ownValue(batch, "runUpdates");
28715
+ if (Array.isArray(updates)) {
28716
+ for (const run of updates) {
28717
+ this.containLifecycleFailure("batchIngestRuns update", () => {
28718
+ this.processUpdate(stringValue2(ownValue(run, "id")) ?? "", run);
28719
+ });
28720
+ }
28721
+ }
28722
+ }
28723
+ processCreate(run) {
28724
+ const id = stringValue2(ownValue(run, "id"));
28725
+ if (!id || this.completedRuns.get(id)) {
28726
+ return;
28727
+ }
28728
+ if (this.shouldSkipLangChainRun(run)) {
28729
+ this.completedRuns.set(id, true);
28730
+ return;
28731
+ }
28732
+ const active = this.activeRuns.get(id);
28733
+ if (active) {
28734
+ const previous = active.run;
28735
+ active.run = mergeRuns(previous, run);
28736
+ this.logRun(
28737
+ active.span,
28738
+ active.run,
28739
+ previous,
28740
+ timestampSeconds(ownValue(active.run, "end_time")) !== void 0 || errorMessage(ownValue(active.run, "error")) !== void 0
28741
+ );
28742
+ this.endIfComplete(id, active, active.run);
28743
+ return;
28744
+ }
28745
+ const span = this.startRunSpan(id, run);
28746
+ const activeRun = { run, span };
28747
+ this.activeRuns.set(id, activeRun);
28748
+ this.logRun(
28749
+ span,
28750
+ run,
28751
+ void 0,
28752
+ timestampSeconds(ownValue(run, "end_time")) !== void 0 || errorMessage(ownValue(run, "error")) !== void 0
28753
+ );
28754
+ this.endIfComplete(id, activeRun, run);
28755
+ }
28756
+ processUpdate(explicitId, run) {
28757
+ const id = stringValue2(explicitId) ?? stringValue2(ownValue(run, "id"));
28758
+ if (!id || this.completedRuns.get(id)) {
28759
+ return;
28760
+ }
28761
+ if (this.shouldSkipLangChainRun(run)) {
28762
+ const active2 = this.activeRuns.get(id);
28763
+ active2?.span.end();
28764
+ this.activeRuns.delete(id);
28765
+ this.completedRuns.set(id, true);
28766
+ return;
28767
+ }
28768
+ let active = this.activeRuns.get(id);
28769
+ if (!active) {
28770
+ const span = this.startRunSpan(id, run);
28771
+ active = { run, span };
28772
+ this.activeRuns.set(id, active);
28773
+ }
28774
+ const previous = active.run;
28775
+ active.run = mergeRuns(previous, run);
28776
+ this.logRun(active.span, active.run, previous, true);
28777
+ this.endIfComplete(id, active, active.run);
28778
+ }
28779
+ startRunSpan(id, run) {
28780
+ const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
28781
+ const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
28782
+ const startTime = timestampSeconds(ownValue(run, "start_time"));
28783
+ return startSpan({
28784
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
28785
+ spanId: id,
28786
+ parentSpanIds: {
28787
+ parentSpanIds: parentId ? [parentId] : [],
28788
+ rootSpanId: traceId
28789
+ },
28790
+ spanAttributes: {
28791
+ type: mapRunType(ownValue(run, "run_type"))
28792
+ },
28793
+ ...startTime === void 0 ? {} : { startTime },
28794
+ event: { id }
28795
+ });
28796
+ }
28797
+ logRun(span, run, previous, includeOutput) {
28798
+ const inputs = preferOwnValue(run, previous, "inputs");
28799
+ const outputs = preferOwnValue(run, previous, "outputs");
28800
+ const error = errorMessage(preferOwnValue(run, previous, "error"));
28801
+ const metadata = extractMetadata(run, previous);
28802
+ const tags = extractTags(preferOwnValue(run, previous, "tags"));
28803
+ const metrics = extractMetrics(run, previous);
28804
+ span.log({
28805
+ ...inputs === void 0 ? {} : { input: sanitizeLoggedValue(inputs) },
28806
+ ...includeOutput && outputs !== void 0 ? { output: sanitizeLoggedValue(outputs) } : {},
28807
+ ...error === void 0 ? {} : { error },
28808
+ ...metadata === void 0 ? {} : { metadata },
28809
+ ...tags === void 0 ? {} : { tags },
28810
+ ...Object.keys(metrics).length === 0 ? {} : { metrics }
28811
+ });
28812
+ }
28813
+ endIfComplete(id, active, run) {
28814
+ const endTime = timestampSeconds(ownValue(run, "end_time"));
28815
+ if (endTime === void 0 && errorMessage(ownValue(run, "error")) === void 0) {
28816
+ return;
28817
+ }
28818
+ active.span.end(endTime === void 0 ? void 0 : { endTime });
28819
+ this.activeRuns.delete(id);
28820
+ this.completedRuns.set(id, true);
28821
+ }
28822
+ shouldSkipLangChainRun(run) {
28823
+ if (!this.skipLangChainRuns) {
28824
+ return false;
28825
+ }
28826
+ const serialized = ownValue(run, "serialized");
28827
+ return isRecord2(serialized) && ownValue(serialized, "lc") === 1;
28828
+ }
28829
+ containLifecycleFailure(operation, fn) {
28830
+ try {
28831
+ fn();
28832
+ } catch (error) {
28833
+ debugLogger.error(
28834
+ `Failed to process LangSmith ${operation} instrumentation:`,
28835
+ error
28836
+ );
28837
+ }
28838
+ }
28839
+ };
28840
+ function ownValue(value, key) {
28841
+ if (!isRecord2(value)) {
28842
+ return void 0;
28843
+ }
28844
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
28845
+ return descriptor && "value" in descriptor ? descriptor.value : void 0;
28846
+ }
28847
+ function preferOwnValue(current, previous, key) {
28848
+ const currentDescriptor = isRecord2(current) ? Object.getOwnPropertyDescriptor(current, key) : void 0;
28849
+ if (currentDescriptor && "value" in currentDescriptor) {
28850
+ return currentDescriptor.value;
28851
+ }
28852
+ return ownValue(previous, key);
28853
+ }
28854
+ function mergeRuns(previous, current) {
28855
+ const entries = /* @__PURE__ */ new Map();
28856
+ for (const value of [previous, current]) {
28857
+ if (!isRecord2(value)) {
28858
+ continue;
28859
+ }
28860
+ for (const [key, descriptor] of Object.entries(
28861
+ Object.getOwnPropertyDescriptors(value)
28862
+ )) {
28863
+ if (descriptor.enumerable && "value" in descriptor) {
28864
+ entries.set(key, descriptor.value);
28865
+ }
28866
+ }
28867
+ }
28868
+ return Object.fromEntries(entries);
28869
+ }
28870
+ function isRecord2(value) {
28871
+ return typeof value === "object" && value !== null && !Array.isArray(value);
28872
+ }
28873
+ function stringValue2(value) {
28874
+ return typeof value === "string" && value.length > 0 ? value : void 0;
28875
+ }
28876
+ function timestampSeconds(value) {
28877
+ if (value instanceof Date) {
28878
+ const timestamp = value.getTime();
28879
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
28880
+ }
28881
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
28882
+ return value > 1e10 ? value / 1e3 : value;
28883
+ }
28884
+ if (typeof value === "string") {
28885
+ const timestamp = Date.parse(value);
28886
+ return Number.isFinite(timestamp) ? timestamp / 1e3 : void 0;
28887
+ }
28888
+ return void 0;
28889
+ }
28890
+ function dottedOrderDepth(run) {
28891
+ const dottedOrder = stringValue2(ownValue(run, "dotted_order"));
28892
+ return dottedOrder ? dottedOrder.split(".").length : Number.MAX_SAFE_INTEGER;
28893
+ }
28894
+ function mapRunType(runType) {
28895
+ switch (runType) {
28896
+ case "llm":
28897
+ case "embedding":
28898
+ return "llm" /* LLM */;
28899
+ case "tool":
28900
+ case "retriever":
28901
+ return "tool" /* TOOL */;
28902
+ default:
28903
+ return "task" /* TASK */;
28904
+ }
28905
+ }
28906
+ function extractMetadata(run, previous) {
28907
+ const extra = preferOwnValue(run, previous, "extra");
28908
+ const rawMetadata = ownValue(extra, "metadata");
28909
+ if (!isRecord2(rawMetadata)) {
28910
+ return void 0;
28911
+ }
28912
+ const metadata = {};
28913
+ for (const [key, descriptor] of Object.entries(
28914
+ Object.getOwnPropertyDescriptors(rawMetadata)
28915
+ )) {
28916
+ 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}`)) {
28917
+ continue;
28918
+ }
28919
+ const normalizedKey = key === "ls_provider" ? "provider" : key === "ls_model_name" ? "model" : key.startsWith("ls_") ? key.slice(3) : key;
28920
+ const sanitized = sanitizeLoggedValue(descriptor.value);
28921
+ if (sanitized !== void 0) {
28922
+ metadata[normalizedKey] = sanitized;
28923
+ }
28924
+ }
28925
+ return Object.keys(metadata).length === 0 ? void 0 : metadata;
28926
+ }
28927
+ function extractTags(value) {
28928
+ if (!Array.isArray(value)) {
28929
+ return void 0;
28930
+ }
28931
+ const tags = value.filter(
28932
+ (tag) => typeof tag === "string" && tag.length > 0
28933
+ );
28934
+ return tags.length > 0 ? tags : void 0;
28935
+ }
28936
+ function extractMetrics(run, previous) {
28937
+ const outputs = preferOwnValue(run, previous, "outputs");
28938
+ const extra = preferOwnValue(run, previous, "extra");
28939
+ const metadata = ownValue(extra, "metadata");
28940
+ const usage = ownValue(outputs, "usage_metadata") ?? ownValue(metadata, "usage_metadata");
28941
+ const metrics = {};
28942
+ assignFirstMetric(metrics, "prompt_tokens", usage, [
28943
+ "input_tokens",
28944
+ "prompt_tokens"
28945
+ ]);
28946
+ assignFirstMetric(metrics, "completion_tokens", usage, [
28947
+ "output_tokens",
28948
+ "completion_tokens"
28949
+ ]);
28950
+ assignFirstMetric(metrics, "tokens", usage, ["total_tokens", "tokens"]);
28951
+ const inputTokenDetails = ownValue(usage, "input_token_details");
28952
+ assignFirstMetric(metrics, "prompt_cached_tokens", inputTokenDetails, [
28953
+ "cache_read",
28954
+ "cached_tokens"
28955
+ ]);
28956
+ if (metrics.prompt_cached_tokens === void 0) {
28957
+ assignFirstMetric(metrics, "prompt_cached_tokens", usage, [
28958
+ "cache_read_input_tokens",
28959
+ "prompt_cached_tokens"
28960
+ ]);
28961
+ }
28962
+ assignFirstMetric(
28963
+ metrics,
28964
+ "prompt_cache_creation_tokens",
28965
+ inputTokenDetails,
28966
+ ["cache_creation"]
28967
+ );
28968
+ if (metrics.prompt_cache_creation_tokens === void 0) {
28969
+ assignFirstMetric(metrics, "prompt_cache_creation_tokens", usage, [
28970
+ "cache_creation_input_tokens",
28971
+ "prompt_cache_creation_tokens"
28972
+ ]);
28973
+ }
28974
+ if (metrics.tokens === void 0 && (metrics.prompt_tokens !== void 0 || metrics.completion_tokens !== void 0)) {
28975
+ metrics.tokens = (metrics.prompt_tokens ?? 0) + (metrics.completion_tokens ?? 0);
28976
+ }
28977
+ const startTime = timestampSeconds(
28978
+ preferOwnValue(run, previous, "start_time")
28979
+ );
28980
+ const events = preferOwnValue(run, previous, "events");
28981
+ if (startTime !== void 0 && Array.isArray(events)) {
28982
+ for (const event of events) {
28983
+ if (ownValue(event, "name") !== "new_token") {
28984
+ continue;
28985
+ }
28986
+ const eventTime2 = timestampSeconds(ownValue(event, "time"));
28987
+ if (eventTime2 !== void 0 && eventTime2 >= startTime) {
28988
+ metrics.time_to_first_token = eventTime2 - startTime;
28989
+ }
28990
+ break;
28991
+ }
28992
+ }
28993
+ return metrics;
28994
+ }
28995
+ function assignFirstMetric(metrics, target, source, keys) {
28996
+ for (const key of keys) {
28997
+ const value = ownValue(source, key);
28998
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
28999
+ metrics[target] = value;
29000
+ return;
29001
+ }
29002
+ }
29003
+ }
29004
+ function errorMessage(value) {
29005
+ if (typeof value === "string") {
29006
+ return value;
29007
+ }
29008
+ if (value instanceof Error) {
29009
+ return value.message;
29010
+ }
29011
+ return void 0;
29012
+ }
29013
+ function sanitizeLoggedValue(value, seen = /* @__PURE__ */ new WeakSet(), depth = 0) {
29014
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
29015
+ return value;
29016
+ }
29017
+ if (typeof value === "number") {
29018
+ return Number.isFinite(value) ? value : void 0;
29019
+ }
29020
+ if (value instanceof Date) {
29021
+ return Number.isFinite(value.getTime()) ? value.toISOString() : void 0;
29022
+ }
29023
+ if (typeof value !== "object" || depth >= 20) {
29024
+ return void 0;
29025
+ }
29026
+ if (seen.has(value)) {
29027
+ return "[Circular]";
29028
+ }
29029
+ seen.add(value);
29030
+ if (Array.isArray(value)) {
29031
+ return value.map((item) => sanitizeLoggedValue(item, seen, depth + 1));
29032
+ }
29033
+ const entries = [];
29034
+ for (const [key, descriptor] of Object.entries(
29035
+ Object.getOwnPropertyDescriptors(value)
29036
+ )) {
29037
+ if (!descriptor.enumerable || !("value" in descriptor) || BLOCKED_METADATA_KEYS.has(key)) {
29038
+ continue;
29039
+ }
29040
+ entries.push([key, sanitizeLoggedValue(descriptor.value, seen, depth + 1)]);
29041
+ }
29042
+ return Object.fromEntries(entries);
29043
+ }
29044
+
28560
29045
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
28561
29046
  var piStreamPatchStates = /* @__PURE__ */ new WeakMap();
28562
29047
  var piPromptContextStore;
@@ -28925,11 +29410,11 @@ function patchAssistantMessageStream(stream, promptState, llmState) {
28925
29410
  function recordPiAssistantMessageEvent(promptState, llmState, event) {
28926
29411
  recordFirstTokenMetric(llmState, event);
28927
29412
  const message = "message" in event ? event.message : void 0;
28928
- const errorMessage = "error" in event ? event.error : void 0;
29413
+ const errorMessage2 = "error" in event ? event.error : void 0;
28929
29414
  if (event.type === "done" && isPiAssistantMessage(message)) {
28930
29415
  finishPiLlmSpan(promptState, llmState, message);
28931
- } else if (event.type === "error" && isPiAssistantMessage(errorMessage)) {
28932
- finishPiLlmSpan(promptState, llmState, errorMessage);
29416
+ } else if (event.type === "error" && isPiAssistantMessage(errorMessage2)) {
29417
+ finishPiLlmSpan(promptState, llmState, errorMessage2);
28933
29418
  }
28934
29419
  }
28935
29420
  function recordFirstTokenMetric(state, event) {
@@ -29431,6 +29916,7 @@ function logInstrumentationError4(context, error) {
29431
29916
  }
29432
29917
 
29433
29918
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
29919
+ var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
29434
29920
  var StrandsAgentSDKPlugin = class extends BasePlugin {
29435
29921
  activeChildParents = /* @__PURE__ */ new WeakMap();
29436
29922
  onEnable() {
@@ -29575,11 +30061,16 @@ function startAgentStream(event, activeChildParents) {
29575
30061
  ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
29576
30062
  };
29577
30063
  const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
30064
+ const attachmentCache = createStrandsAttachmentCache();
30065
+ const input = processStrandsInputAttachments(
30066
+ event.arguments[0],
30067
+ attachmentCache
30068
+ );
29578
30069
  const span = parentSpan ? withCurrent(
29579
30070
  parentSpan,
29580
30071
  () => startSpan({
29581
30072
  event: {
29582
- input: event.arguments[0],
30073
+ input,
29583
30074
  metadata
29584
30075
  },
29585
30076
  name: formatAgentSpanName(agent),
@@ -29587,7 +30078,7 @@ function startAgentStream(event, activeChildParents) {
29587
30078
  })
29588
30079
  ) : startSpan({
29589
30080
  event: {
29590
- input: event.arguments[0],
30081
+ input,
29591
30082
  metadata
29592
30083
  },
29593
30084
  name: formatAgentSpanName(agent),
@@ -29595,6 +30086,7 @@ function startAgentStream(event, activeChildParents) {
29595
30086
  });
29596
30087
  return {
29597
30088
  activeTools: /* @__PURE__ */ new Map(),
30089
+ attachmentCache,
29598
30090
  finalized: false,
29599
30091
  metadata,
29600
30092
  span,
@@ -29610,11 +30102,12 @@ function startMultiAgentStream(event, operation, activeChildParents) {
29610
30102
  ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
29611
30103
  };
29612
30104
  const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
30105
+ const input = processStrandsInputAttachments(event.arguments[0]);
29613
30106
  const span = parentSpan ? withCurrent(
29614
30107
  parentSpan,
29615
30108
  () => startSpan({
29616
30109
  event: {
29617
- input: event.arguments[0],
30110
+ input,
29618
30111
  metadata
29619
30112
  },
29620
30113
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -29622,7 +30115,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
29622
30115
  })
29623
30116
  ) : startSpan({
29624
30117
  event: {
29625
- input: event.arguments[0],
30118
+ input,
29626
30119
  metadata
29627
30120
  },
29628
30121
  name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
@@ -29731,7 +30224,10 @@ function startModelSpan(state, event) {
29731
30224
  state.span,
29732
30225
  () => startSpan({
29733
30226
  event: {
29734
- input: Array.isArray(event.agent?.messages) ? event.agent.messages : void 0,
30227
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
30228
+ event.agent.messages,
30229
+ state.attachmentCache
30230
+ ) : void 0,
29735
30231
  metadata
29736
30232
  },
29737
30233
  name: formatModelSpanName(model),
@@ -29945,6 +30441,7 @@ function finalizeAgentStream(state, error, output) {
29945
30441
  ...output !== void 0 ? { output } : {}
29946
30442
  });
29947
30443
  state.span.end();
30444
+ state.attachmentCache.strings.clear();
29948
30445
  }
29949
30446
  function finalizeMultiAgentStream(state, activeChildParents, error, output) {
29950
30447
  if (state.finalized) {
@@ -30091,6 +30588,166 @@ function extractNodeResultOutput(result) {
30091
30588
  }
30092
30589
  return result;
30093
30590
  }
30591
+ var STRANDS_MEDIA_TYPES = {
30592
+ png: "image/png",
30593
+ jpg: "image/jpeg",
30594
+ jpeg: "image/jpeg",
30595
+ gif: "image/gif",
30596
+ webp: "image/webp",
30597
+ mkv: "video/x-matroska",
30598
+ mov: "video/quicktime",
30599
+ mp4: "video/mp4",
30600
+ webm: "video/webm",
30601
+ flv: "video/x-flv",
30602
+ mpeg: "video/mpeg",
30603
+ mpg: "video/mpeg",
30604
+ wmv: "video/x-ms-wmv",
30605
+ "3gp": "video/3gpp",
30606
+ pdf: "application/pdf",
30607
+ csv: "text/csv",
30608
+ doc: "application/msword",
30609
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
30610
+ xls: "application/vnd.ms-excel",
30611
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
30612
+ html: "text/html",
30613
+ txt: "text/plain",
30614
+ md: "text/markdown",
30615
+ json: "application/json",
30616
+ xml: "application/xml"
30617
+ };
30618
+ function createStrandsAttachmentCache() {
30619
+ return {
30620
+ objects: /* @__PURE__ */ new WeakMap(),
30621
+ strings: new LRUCache({
30622
+ max: MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES
30623
+ })
30624
+ };
30625
+ }
30626
+ function processStrandsInputAttachments(input, cache = createStrandsAttachmentCache()) {
30627
+ try {
30628
+ return processStrandsInputNode(input, cache);
30629
+ } catch (error) {
30630
+ logInstrumentationError5("Strands Agent SDK input attachments", error);
30631
+ return input;
30632
+ }
30633
+ }
30634
+ function processStrandsInputNode(value, cache) {
30635
+ if (value instanceof BaseAttachment) {
30636
+ return value;
30637
+ }
30638
+ if (Array.isArray(value)) {
30639
+ return value.map((child) => processStrandsInputNode(child, cache));
30640
+ }
30641
+ if (!isObject(value)) {
30642
+ return value;
30643
+ }
30644
+ const directMedia = processDirectStrandsMediaBlock(value, cache);
30645
+ if (directMedia !== void 0) {
30646
+ return directMedia;
30647
+ }
30648
+ const wrappedMedia = processWrappedStrandsMediaBlock(value, cache);
30649
+ if (wrappedMedia !== void 0) {
30650
+ return wrappedMedia;
30651
+ }
30652
+ if (value.type === "message" && Array.isArray(value.content)) {
30653
+ return {
30654
+ role: value.role,
30655
+ content: value.content.map(
30656
+ (child) => processStrandsInputNode(child, cache)
30657
+ ),
30658
+ ...value.metadata !== void 0 ? { metadata: value.metadata } : {}
30659
+ };
30660
+ }
30661
+ if (typeof value.toJSON === "function") {
30662
+ return processStrandsInputNode(value.toJSON(), cache);
30663
+ }
30664
+ return Object.fromEntries(
30665
+ Object.entries(value).map(([key, child]) => [
30666
+ key,
30667
+ processStrandsInputNode(child, cache)
30668
+ ])
30669
+ );
30670
+ }
30671
+ function processDirectStrandsMediaBlock(block, cache) {
30672
+ if (!isStrandsMediaBlock(block)) {
30673
+ return void 0;
30674
+ }
30675
+ const mediaKey = block.type === "imageBlock" ? "image" : block.type === "videoBlock" ? "video" : "document";
30676
+ return createStrandsMediaAttachment(mediaKey, block, cache);
30677
+ }
30678
+ function isStrandsMediaBlock(block) {
30679
+ return (block.type === "imageBlock" || block.type === "videoBlock" || block.type === "documentBlock") && typeof block.format === "string" && isObject(block.source);
30680
+ }
30681
+ function processWrappedStrandsMediaBlock(block, cache) {
30682
+ for (const mediaKey of ["image", "video", "document"]) {
30683
+ const media = block[mediaKey];
30684
+ if (!Object.hasOwn(block, mediaKey) || !isObject(media)) {
30685
+ continue;
30686
+ }
30687
+ const processed = createStrandsMediaAttachment(mediaKey, media, cache);
30688
+ if (processed !== void 0) {
30689
+ return processed;
30690
+ }
30691
+ }
30692
+ return void 0;
30693
+ }
30694
+ function createStrandsMediaAttachment(mediaKey, media, cache) {
30695
+ const format = media.format;
30696
+ const source = media.source;
30697
+ if (typeof format !== "string" || !isObject(source) || !Object.hasOwn(source, "bytes")) {
30698
+ return void 0;
30699
+ }
30700
+ const contentType = STRANDS_MEDIA_TYPES[format.toLowerCase()];
30701
+ if (!contentType) {
30702
+ return void 0;
30703
+ }
30704
+ const filename = mediaKey === "document" && typeof media.name === "string" && media.name.length > 0 ? media.name : `${mediaKey}.${format.toLowerCase()}`;
30705
+ const attachment = getOrCreateStrandsAttachment(
30706
+ source.bytes,
30707
+ filename,
30708
+ contentType,
30709
+ cache
30710
+ );
30711
+ if (!attachment) {
30712
+ return void 0;
30713
+ }
30714
+ const { type: _type, ...serializedMedia } = media;
30715
+ const { type: _sourceType, ...serializedSource } = source;
30716
+ return {
30717
+ [mediaKey]: {
30718
+ ...serializedMedia,
30719
+ source: {
30720
+ ...serializedSource,
30721
+ bytes: attachment
30722
+ }
30723
+ }
30724
+ };
30725
+ }
30726
+ function getOrCreateStrandsAttachment(data, filename, contentType, cache) {
30727
+ const key = `${contentType}\0${filename}`;
30728
+ const attachments = typeof data === "string" ? cache.strings.get(data) : isObject(data) ? cache.objects.get(data) : void 0;
30729
+ const cached = attachments?.get(key);
30730
+ if (cached) {
30731
+ return cached;
30732
+ }
30733
+ const blob = convertDataToBlob(data, contentType);
30734
+ if (!blob) {
30735
+ return void 0;
30736
+ }
30737
+ const attachment = new Attachment({
30738
+ data: blob,
30739
+ filename,
30740
+ contentType
30741
+ });
30742
+ const updatedAttachments = attachments ?? /* @__PURE__ */ new Map();
30743
+ updatedAttachments.set(key, attachment);
30744
+ if (typeof data === "string") {
30745
+ cache.strings.set(data, updatedAttachments);
30746
+ } else if (isObject(data)) {
30747
+ cache.objects.set(data, updatedAttachments);
30748
+ }
30749
+ return attachment;
30750
+ }
30094
30751
  function normalizeContentBlocks(blocks) {
30095
30752
  const text = blocks.map((block) => typeof block.text === "string" ? block.text : void 0).filter((part) => Boolean(part)).join("");
30096
30753
  return text.length > 0 ? text : blocks;
@@ -30220,6 +30877,7 @@ var BraintrustPlugin = class extends BasePlugin {
30220
30877
  gitHubCopilotPlugin = null;
30221
30878
  fluePlugin = null;
30222
30879
  langChainPlugin = null;
30880
+ langSmithPlugin = null;
30223
30881
  piCodingAgentPlugin = null;
30224
30882
  strandsAgentSDKPlugin = null;
30225
30883
  constructor(config = {}) {
@@ -30316,6 +30974,12 @@ var BraintrustPlugin = class extends BasePlugin {
30316
30974
  this.langChainPlugin = new LangChainPlugin();
30317
30975
  this.langChainPlugin.enable();
30318
30976
  }
30977
+ if (integrations.langsmith !== false) {
30978
+ this.langSmithPlugin = new LangSmithPlugin({
30979
+ skipLangChainRuns: integrations.langchain !== false
30980
+ });
30981
+ this.langSmithPlugin.enable();
30982
+ }
30319
30983
  }
30320
30984
  onDisable() {
30321
30985
  if (this.openaiPlugin) {
@@ -30406,6 +31070,10 @@ var BraintrustPlugin = class extends BasePlugin {
30406
31070
  this.langChainPlugin.disable();
30407
31071
  this.langChainPlugin = null;
30408
31072
  }
31073
+ if (this.langSmithPlugin) {
31074
+ this.langSmithPlugin.disable();
31075
+ this.langSmithPlugin = null;
31076
+ }
30409
31077
  }
30410
31078
  };
30411
31079
 
@@ -30566,6 +31234,15 @@ function modelMetrics(attributes) {
30566
31234
  }
30567
31235
  return Object.keys(out).length > 0 ? out : void 0;
30568
31236
  }
31237
+ function timeToFirstTokenSeconds(attributes, spanStartSeconds) {
31238
+ if (!isObject(attributes)) return void 0;
31239
+ if (spanStartSeconds === void 0) return void 0;
31240
+ const raw = attributes.completionStartTime;
31241
+ const completionStart = raw instanceof Date || typeof raw === "string" || typeof raw === "number" ? epochSeconds(raw) : void 0;
31242
+ if (completionStart === void 0) return void 0;
31243
+ const ttft = completionStart - spanStartSeconds;
31244
+ return Number.isFinite(ttft) && ttft >= 0 ? ttft : void 0;
31245
+ }
30569
31246
  function buildMetadata(exported) {
30570
31247
  const out = {};
30571
31248
  if (exported.entityId !== void 0) out.entity_id = exported.entityId;
@@ -30690,8 +31367,15 @@ var BraintrustObservabilityExporter = class {
30690
31367
  event.metadata = metadata;
30691
31368
  }
30692
31369
  const metrics = modelMetrics(exported.attributes);
30693
- if (metrics) {
30694
- event.metrics = metrics;
31370
+ const ttft = timeToFirstTokenSeconds(
31371
+ exported.attributes,
31372
+ epochSeconds(exported.startTime)
31373
+ );
31374
+ if (metrics || ttft !== void 0) {
31375
+ event.metrics = {
31376
+ ...metrics ?? {},
31377
+ ...ttft !== void 0 ? { time_to_first_token: ttft } : {}
31378
+ };
30695
31379
  }
30696
31380
  if (Object.keys(event).length > 0) {
30697
31381
  record.span.log(event);