braintrust 3.23.1 → 3.25.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 (51) hide show
  1. package/dev/dist/index.d.mts +11 -0
  2. package/dev/dist/index.d.ts +11 -0
  3. package/dev/dist/index.js +2881 -1518
  4. package/dev/dist/index.mjs +2298 -935
  5. package/dist/apply-auto-instrumentation.js +218 -195
  6. package/dist/apply-auto-instrumentation.mjs +30 -7
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +618 -444
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  9. package/dist/auto-instrumentations/bundler/next.cjs +618 -444
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +618 -444
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  13. package/dist/auto-instrumentations/bundler/vite.cjs +618 -444
  14. package/dist/auto-instrumentations/bundler/vite.d.mts +1 -1
  15. package/dist/auto-instrumentations/bundler/vite.d.ts +1 -1
  16. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  17. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +618 -444
  18. package/dist/auto-instrumentations/bundler/webpack.cjs +618 -444
  19. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  20. package/dist/auto-instrumentations/{chunk-KIMLYPRW.mjs → chunk-7P6563SW.mjs} +1 -1
  21. package/dist/auto-instrumentations/{chunk-YXLNSAMJ.mjs → chunk-CZ24KNHT.mjs} +591 -424
  22. package/dist/auto-instrumentations/{chunk-EXY7QCJD.mjs → chunk-JPVCUKTY.mjs} +30 -21
  23. package/dist/auto-instrumentations/hook.mjs +778 -457
  24. package/dist/auto-instrumentations/index.cjs +590 -424
  25. package/dist/auto-instrumentations/index.mjs +1 -1
  26. package/dist/browser.d.mts +138 -9
  27. package/dist/browser.d.ts +138 -9
  28. package/dist/browser.js +2700 -1011
  29. package/dist/browser.mjs +2700 -1011
  30. package/dist/{chunk-RBXD2KYN.mjs → chunk-2SANLSWX.mjs} +1577 -513
  31. package/dist/{chunk-36IPYKMG.mjs → chunk-ABR2QWDP.mjs} +704 -438
  32. package/dist/{chunk-B6ZQIAK3.js → chunk-GSIDVFE6.js} +2361 -1297
  33. package/dist/{chunk-CDIKAHDZ.js → chunk-P25IOOU4.js} +706 -440
  34. package/dist/cli.js +2307 -940
  35. package/dist/edge-light.js +2700 -1011
  36. package/dist/edge-light.mjs +2700 -1011
  37. package/dist/index.d.mts +138 -9
  38. package/dist/index.d.ts +138 -9
  39. package/dist/index.js +955 -551
  40. package/dist/index.mjs +493 -89
  41. package/dist/instrumentation/index.d.mts +135 -9
  42. package/dist/instrumentation/index.d.ts +135 -9
  43. package/dist/instrumentation/index.js +2554 -1009
  44. package/dist/instrumentation/index.mjs +2554 -1009
  45. package/dist/vitest-evals-reporter.js +17 -16
  46. package/dist/vitest-evals-reporter.mjs +3 -2
  47. package/dist/workerd.js +2700 -1011
  48. package/dist/workerd.mjs +2700 -1011
  49. package/package.json +4 -4
  50. package/util/dist/index.js +4 -0
  51. package/util/dist/index.mjs +4 -0
@@ -15,6 +15,7 @@ var DefaultChannel = class {
15
15
  constructor(name) {
16
16
  this.name = name;
17
17
  }
18
+ name;
18
19
  hasSubscribers = false;
19
20
  subscribe(_subscription) {
20
21
  }
@@ -1291,6 +1292,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
1291
1292
  constructor(data) {
1292
1293
  this.data = data;
1293
1294
  }
1295
+ data;
1294
1296
  toStr() {
1295
1297
  const jsonObj = {
1296
1298
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -1711,6 +1713,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
1711
1713
  constructor(data) {
1712
1714
  this.data = data;
1713
1715
  }
1716
+ data;
1714
1717
  toStr() {
1715
1718
  const jsonObj = {
1716
1719
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -4872,6 +4875,144 @@ var SpanCache = class {
4872
4875
  }
4873
4876
  };
4874
4877
 
4878
+ // src/span-origin.ts
4879
+ var INSTRUMENTATION_NAMES = {
4880
+ AI_SDK: "ai-sdk",
4881
+ ANTHROPIC: "anthropic",
4882
+ BEDROCK_RUNTIME: "bedrock-runtime",
4883
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
4884
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
4885
+ COHERE: "cohere",
4886
+ CURSOR_SDK: "cursor-sdk",
4887
+ EVE: "eve",
4888
+ FLUE: "flue",
4889
+ GENKIT: "genkit",
4890
+ GITHUB_COPILOT: "github-copilot",
4891
+ GOOGLE_ADK: "google-adk",
4892
+ GOOGLE_GENAI: "google-genai",
4893
+ GROQ: "groq",
4894
+ HUGGINGFACE: "huggingface",
4895
+ LANGCHAIN: "langchain",
4896
+ LANGSMITH: "langsmith",
4897
+ MASTRA: "mastra",
4898
+ MISTRAL: "mistral",
4899
+ OPENAI: "openai",
4900
+ OPENAI_AGENTS: "openai-agents",
4901
+ OPENAI_CODEX: "openai-codex",
4902
+ OPENROUTER: "openrouter",
4903
+ OPENROUTER_AGENT: "openrouter-agent",
4904
+ PI_CODING_AGENT: "pi-coding-agent",
4905
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
4906
+ };
4907
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
4908
+ "braintrust.spanInstrumentationName"
4909
+ );
4910
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
4911
+ function withSpanInstrumentationName(args, instrumentationName) {
4912
+ return {
4913
+ ...args,
4914
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
4915
+ };
4916
+ }
4917
+ function getSpanInstrumentationName(args) {
4918
+ if (typeof args !== "object" || args === null) {
4919
+ return void 0;
4920
+ }
4921
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
4922
+ return isSpanInstrumentationName(value) ? value : void 0;
4923
+ }
4924
+ function detectSpanOriginEnvironment(explicit) {
4925
+ if (explicit) return explicit;
4926
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
4927
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
4928
+ if (envType || envName) {
4929
+ return {
4930
+ ...envType ? { type: envType } : {},
4931
+ ...envName ? { name: envName } : {}
4932
+ };
4933
+ }
4934
+ const ci = firstPresent([
4935
+ ["GITHUB_ACTIONS", "github_actions"],
4936
+ ["GITLAB_CI", "gitlab_ci"],
4937
+ ["CIRCLECI", "circleci"],
4938
+ ["BUILDKITE", "buildkite"],
4939
+ ["JENKINS_URL", "jenkins"],
4940
+ ["JENKINS_HOME", "jenkins"],
4941
+ ["TF_BUILD", "azure_pipelines"],
4942
+ ["TEAMCITY_VERSION", "teamcity"],
4943
+ ["TRAVIS", "travis"],
4944
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
4945
+ ]);
4946
+ if (ci) return { type: "ci", name: ci };
4947
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
4948
+ const earlyServer = firstPresent([
4949
+ ["VERCEL", "vercel"],
4950
+ ["NETLIFY", "netlify"]
4951
+ ]);
4952
+ if (earlyServer) return { type: "server", name: earlyServer };
4953
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
4954
+ return { type: "server", name: "ecs" };
4955
+ }
4956
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
4957
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
4958
+ return { type: "server", name: "ecs" };
4959
+ }
4960
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
4961
+ return { type: "server", name: "aws_lambda" };
4962
+ }
4963
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
4964
+ return { type: "server", name: "aws_lambda" };
4965
+ }
4966
+ const server = firstPresent([
4967
+ ["K_SERVICE", "cloud_run"],
4968
+ ["FUNCTION_TARGET", "gcp_functions"],
4969
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
4970
+ ["DYNO", "heroku"],
4971
+ ["FLY_APP_NAME", "fly"],
4972
+ ["RAILWAY_ENVIRONMENT", "railway"],
4973
+ ["RENDER_SERVICE_NAME", "render"]
4974
+ ]);
4975
+ if (server) return { type: "server", name: server };
4976
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
4977
+ }
4978
+ function makeSpanOrigin(instrumentationName, environment) {
4979
+ return {
4980
+ name: "braintrust.sdk.javascript",
4981
+ version: SDK_VERSION,
4982
+ instrumentation: { name: instrumentationName },
4983
+ ...environment ? { environment } : {}
4984
+ };
4985
+ }
4986
+ function mergeSpanOriginContext(context, instrumentationName, environment) {
4987
+ const next = { ...context ?? {} };
4988
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
4989
+ next.span_origin = {
4990
+ ...makeSpanOrigin(instrumentationName, environment),
4991
+ ...current
4992
+ };
4993
+ return next;
4994
+ }
4995
+ function isSpanInstrumentationName(value) {
4996
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
4997
+ }
4998
+ function firstPresent(entries) {
4999
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
5000
+ }
5001
+ function deploymentModeEnvironment(_key, value) {
5002
+ if (!value) return void 0;
5003
+ const normalized = value.toLowerCase();
5004
+ if (normalized === "production" || normalized === "staging") {
5005
+ return { type: "server", name: normalized };
5006
+ }
5007
+ if (normalized === "development" || normalized === "local") {
5008
+ return { type: "local", name: normalized };
5009
+ }
5010
+ return { name: value };
5011
+ }
5012
+ function isObject2(value) {
5013
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5014
+ }
5015
+
4875
5016
  // src/logger.ts
4876
5017
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
4877
5018
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -4920,6 +5061,7 @@ var LoginInvalidOrgError = class extends Error {
4920
5061
  super(message);
4921
5062
  this.message = message;
4922
5063
  }
5064
+ message;
4923
5065
  };
4924
5066
  var REDACTION_FIELDS = [
4925
5067
  "input",
@@ -4935,6 +5077,8 @@ var MaskingError = class {
4935
5077
  this.fieldName = fieldName;
4936
5078
  this.errorType = errorType;
4937
5079
  }
5080
+ fieldName;
5081
+ errorType;
4938
5082
  get errorMsg() {
4939
5083
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
4940
5084
  }
@@ -5144,7 +5288,10 @@ var BraintrustState = class _BraintrustState {
5144
5288
  diskCache: parametersDiskCache
5145
5289
  });
5146
5290
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
5291
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
5292
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5147
5293
  }
5294
+ loginParams;
5148
5295
  id;
5149
5296
  currentExperiment;
5150
5297
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -5179,6 +5326,19 @@ var BraintrustState = class _BraintrustState {
5179
5326
  _idGenerator = null;
5180
5327
  _contextManager = null;
5181
5328
  _otelFlushCallback = null;
5329
+ spanOriginEnvironment;
5330
+ traceContextSigningSecret;
5331
+ /** @internal */
5332
+ _internalSetTraceContextSigningSecret(secret) {
5333
+ const normalizedSecret = secret?.trim();
5334
+ if (normalizedSecret) {
5335
+ this.traceContextSigningSecret = normalizedSecret;
5336
+ }
5337
+ }
5338
+ /** @internal */
5339
+ _internalGetTraceContextSigningSecret() {
5340
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
5341
+ }
5182
5342
  resetLoginInfo() {
5183
5343
  this.appUrl = null;
5184
5344
  this.appPublicUrl = null;
@@ -5239,6 +5399,7 @@ var BraintrustState = class _BraintrustState {
5239
5399
  this.gitMetadataSettings = other.gitMetadataSettings;
5240
5400
  this.debugLogLevel = other.debugLogLevel;
5241
5401
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
5402
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
5242
5403
  setGlobalDebugLogLevel(
5243
5404
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
5244
5405
  );
@@ -5326,6 +5487,7 @@ var BraintrustState = class _BraintrustState {
5326
5487
  return this.debugLogLevelConfigured;
5327
5488
  }
5328
5489
  async login(loginParams) {
5490
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5329
5491
  this.setDebugLogLevel(loginParams.debugLogLevel);
5330
5492
  if (this.apiUrl && !loginParams.forceLogin) {
5331
5493
  return;
@@ -7096,6 +7258,7 @@ function initLogger(options = {}) {
7096
7258
  orgName,
7097
7259
  forceLogin,
7098
7260
  debugLogLevel,
7261
+ environment,
7099
7262
  fetch: fetch2,
7100
7263
  state: stateArg
7101
7264
  } = options || {};
@@ -7111,7 +7274,9 @@ function initLogger(options = {}) {
7111
7274
  project_id: projectId
7112
7275
  };
7113
7276
  const state = stateArg ?? _globalState;
7277
+ state._internalSetTraceContextSigningSecret(apiKey);
7114
7278
  state.setDebugLogLevel(debugLogLevel);
7279
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
7115
7280
  state.enforceQueueSizeLimit(true);
7116
7281
  const lazyMetadata = new LazyValue(
7117
7282
  async () => {
@@ -7634,7 +7799,9 @@ function deepCopyEvent(event) {
7634
7799
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
7635
7800
  const attachmentMarker = ++deepCopyEventMarkerCounter;
7636
7801
  const serialized = JSON.stringify(event, (_k, v) => {
7637
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
7802
+ if (v instanceof Error) {
7803
+ return v.message;
7804
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
7638
7805
  return `<span>`;
7639
7806
  } else if (v instanceof Experiment2) {
7640
7807
  return `<experiment>`;
@@ -7751,6 +7918,11 @@ var ObjectFetcher = class {
7751
7918
  this._internal_btql = _internal_btql;
7752
7919
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
7753
7920
  }
7921
+ objectType;
7922
+ pinnedVersion;
7923
+ mutateRecord;
7924
+ _internal_btql;
7925
+ _internalBrainstoreRealtime;
7754
7926
  _fetchedData = void 0;
7755
7927
  get id() {
7756
7928
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -8241,6 +8413,7 @@ var SpanImpl = class _SpanImpl {
8241
8413
  constructor(args) {
8242
8414
  this._state = args.state;
8243
8415
  this._propagatedState = args.propagatedState;
8416
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
8244
8417
  const spanAttributes = args.spanAttributes ?? {};
8245
8418
  const rawEvent = args.event ?? {};
8246
8419
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -8270,7 +8443,11 @@ var SpanImpl = class _SpanImpl {
8270
8443
  metrics: {
8271
8444
  start: args.startTime ?? getCurrentUnixTimestamp()
8272
8445
  },
8273
- context: { ...callerLocation },
8446
+ context: mergeSpanOriginContext(
8447
+ { ...callerLocation },
8448
+ instrumentationName,
8449
+ this._state.spanOriginEnvironment
8450
+ ),
8274
8451
  span_attributes: {
8275
8452
  name,
8276
8453
  type,
@@ -8644,6 +8821,7 @@ var Dataset2 = class extends ObjectFetcher {
8644
8821
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
8645
8822
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
8646
8823
  }
8824
+ state;
8647
8825
  lazyMetadata;
8648
8826
  __braintrust_dataset_marker = true;
8649
8827
  newRecords = 0;
@@ -9492,15 +9670,25 @@ function hasChoices(value) {
9492
9670
  function normalizeMetadata(metadata) {
9493
9671
  return isObject(metadata) ? metadata : void 0;
9494
9672
  }
9495
- function startSpanForEvent(config, event, channelName) {
9673
+ function startSpanForEvent(config, event, channelName, instrumentationName) {
9496
9674
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
9497
9675
  config,
9498
9676
  event
9499
9677
  );
9500
- const span = startSpan({
9501
- name,
9502
- spanAttributes
9503
- });
9678
+ const spanArgs = withSpanInstrumentationName(
9679
+ {
9680
+ name,
9681
+ spanAttributes
9682
+ },
9683
+ instrumentationName
9684
+ );
9685
+ let span;
9686
+ try {
9687
+ span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
9688
+ } catch (error) {
9689
+ debugLogger.error(`Error starting span for ${channelName}:`, error);
9690
+ span = startSpan(spanArgs);
9691
+ }
9504
9692
  const startTime = getCurrentUnixTimestamp();
9505
9693
  try {
9506
9694
  const { input, metadata } = config.extractInput(
@@ -9531,7 +9719,7 @@ function shouldTraceEvent(config, event, channelName) {
9531
9719
  return true;
9532
9720
  }
9533
9721
  }
9534
- function ensureSpanStateForEvent(states, config, event, channelName) {
9722
+ function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
9535
9723
  const key = event;
9536
9724
  const existing = states.get(key);
9537
9725
  if (existing) {
@@ -9540,11 +9728,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
9540
9728
  if (!shouldTraceEvent(config, event, channelName)) {
9541
9729
  return void 0;
9542
9730
  }
9543
- const created = startSpanForEvent(config, event, channelName);
9731
+ const created = startSpanForEvent(
9732
+ config,
9733
+ event,
9734
+ channelName,
9735
+ instrumentationName
9736
+ );
9544
9737
  states.set(key, created);
9545
9738
  return created;
9546
9739
  }
9547
- function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName) {
9740
+ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName, instrumentationName) {
9548
9741
  const state = _internalGetGlobalState();
9549
9742
  const startChannel = tracingChannel.start;
9550
9743
  const contextManager = state?.contextManager;
@@ -9562,7 +9755,8 @@ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName
9562
9755
  states,
9563
9756
  config,
9564
9757
  event,
9565
- channelName
9758
+ channelName,
9759
+ instrumentationName
9566
9760
  );
9567
9761
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
9568
9762
  }
@@ -9571,15 +9765,21 @@ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName
9571
9765
  startChannel.unbindStore(currentSpanStore);
9572
9766
  };
9573
9767
  }
9574
- function logErrorAndEnd(states, event) {
9768
+ function logErrorAndEnd(states, event, channelName) {
9575
9769
  const spanData = states.get(event);
9576
9770
  if (!spanData) {
9577
9771
  return;
9578
9772
  }
9579
- spanData.span.log({
9580
- error: event.error.message
9581
- });
9582
- spanData.span.end();
9773
+ try {
9774
+ spanData.span.log({ error: event.error });
9775
+ } catch (error) {
9776
+ debugLogger.error(`Error logging failure for ${channelName}:`, error);
9777
+ }
9778
+ try {
9779
+ spanData.span.end();
9780
+ } catch (error) {
9781
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
9782
+ }
9583
9783
  states.delete(event);
9584
9784
  }
9585
9785
  function runStreamingCompletionHook(args) {
@@ -9629,7 +9829,8 @@ function traceAsyncChannel(channel2, config) {
9629
9829
  tracingChannel,
9630
9830
  states,
9631
9831
  config,
9632
- channelName
9832
+ channelName,
9833
+ channel2.instrumentationName
9633
9834
  );
9634
9835
  const handlers = {
9635
9836
  start: (event) => {
@@ -9640,7 +9841,8 @@ function traceAsyncChannel(channel2, config) {
9640
9841
  states,
9641
9842
  config,
9642
9843
  event,
9643
- channelName
9844
+ channelName,
9845
+ channel2.instrumentationName
9644
9846
  );
9645
9847
  },
9646
9848
  asyncEnd: (event) => {
@@ -9677,7 +9879,7 @@ function traceAsyncChannel(channel2, config) {
9677
9879
  }
9678
9880
  },
9679
9881
  error: (event) => {
9680
- logErrorAndEnd(states, event);
9882
+ logErrorAndEnd(states, event, channelName);
9681
9883
  }
9682
9884
  };
9683
9885
  tracingChannel.subscribe(handlers);
@@ -9694,7 +9896,8 @@ function traceStreamingChannel(channel2, config) {
9694
9896
  tracingChannel,
9695
9897
  states,
9696
9898
  config,
9697
- channelName
9899
+ channelName,
9900
+ channel2.instrumentationName
9698
9901
  );
9699
9902
  const handlers = {
9700
9903
  start: (event) => {
@@ -9705,7 +9908,8 @@ function traceStreamingChannel(channel2, config) {
9705
9908
  states,
9706
9909
  config,
9707
9910
  event,
9708
- channelName
9911
+ channelName,
9912
+ channel2.instrumentationName
9709
9913
  );
9710
9914
  },
9711
9915
  asyncEnd: (event) => {
@@ -9724,6 +9928,7 @@ function traceStreamingChannel(channel2, config) {
9724
9928
  }
9725
9929
  },
9726
9930
  onComplete: (chunks) => {
9931
+ let completion2;
9727
9932
  try {
9728
9933
  let output;
9729
9934
  let metrics;
@@ -9754,18 +9959,11 @@ function traceStreamingChannel(channel2, config) {
9754
9959
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
9755
9960
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9756
9961
  }
9757
- runStreamingCompletionHook({
9758
- channelName,
9759
- chunks,
9760
- config,
9761
- endEvent: asyncEndEvent,
9962
+ completion2 = {
9762
9963
  ...metadata !== void 0 ? { metadata } : {},
9763
9964
  metrics,
9764
- output,
9765
- result: asyncEndEvent.result,
9766
- span,
9767
- startTime
9768
- });
9965
+ output
9966
+ };
9769
9967
  span.log({
9770
9968
  output,
9771
9969
  ...metadata !== void 0 ? { metadata } : {},
@@ -9777,11 +9975,49 @@ function traceStreamingChannel(channel2, config) {
9777
9975
  error
9778
9976
  );
9779
9977
  } finally {
9780
- span.end();
9978
+ try {
9979
+ span.end();
9980
+ } catch (error) {
9981
+ debugLogger.error(
9982
+ `Error ending span for ${channelName}:`,
9983
+ error
9984
+ );
9985
+ }
9781
9986
  states.delete(event);
9782
9987
  }
9988
+ if (completion2) {
9989
+ runStreamingCompletionHook({
9990
+ channelName,
9991
+ chunks,
9992
+ config,
9993
+ endEvent: asyncEndEvent,
9994
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
9995
+ metrics: completion2.metrics,
9996
+ output: completion2.output,
9997
+ result: asyncEndEvent.result,
9998
+ span,
9999
+ startTime
10000
+ });
10001
+ }
9783
10002
  },
9784
10003
  onError: (error) => {
10004
+ try {
10005
+ span.log({ error });
10006
+ } catch (loggingError) {
10007
+ debugLogger.error(
10008
+ `Error logging failure for ${channelName}:`,
10009
+ loggingError
10010
+ );
10011
+ }
10012
+ try {
10013
+ span.end();
10014
+ } catch (endingError) {
10015
+ debugLogger.error(
10016
+ `Error ending span for ${channelName}:`,
10017
+ endingError
10018
+ );
10019
+ }
10020
+ states.delete(event);
9785
10021
  runStreamingErrorHook({
9786
10022
  channelName,
9787
10023
  config,
@@ -9790,11 +10026,6 @@ function traceStreamingChannel(channel2, config) {
9790
10026
  span,
9791
10027
  startTime
9792
10028
  });
9793
- span.log({
9794
- error: error.message
9795
- });
9796
- span.end();
9797
- states.delete(event);
9798
10029
  }
9799
10030
  });
9800
10031
  return;
@@ -9809,6 +10040,7 @@ function traceStreamingChannel(channel2, config) {
9809
10040
  states.delete(event);
9810
10041
  return;
9811
10042
  }
10043
+ let completion;
9812
10044
  try {
9813
10045
  const output = config.extractOutput(
9814
10046
  asyncEndEvent.result,
@@ -9823,17 +10055,11 @@ function traceStreamingChannel(channel2, config) {
9823
10055
  asyncEndEvent.result,
9824
10056
  asyncEndEvent
9825
10057
  );
9826
- runStreamingCompletionHook({
9827
- channelName,
9828
- config,
9829
- endEvent: asyncEndEvent,
10058
+ completion = {
9830
10059
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
9831
10060
  metrics,
9832
- output,
9833
- result: asyncEndEvent.result,
9834
- span,
9835
- startTime
9836
- });
10061
+ output
10062
+ };
9837
10063
  span.log({
9838
10064
  output,
9839
10065
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -9842,12 +10068,30 @@ function traceStreamingChannel(channel2, config) {
9842
10068
  } catch (error) {
9843
10069
  debugLogger.error(`Error extracting output for ${channelName}:`, error);
9844
10070
  } finally {
9845
- span.end();
10071
+ try {
10072
+ span.end();
10073
+ } catch (error) {
10074
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
10075
+ }
9846
10076
  states.delete(event);
9847
10077
  }
10078
+ if (completion) {
10079
+ runStreamingCompletionHook({
10080
+ channelName,
10081
+ config,
10082
+ endEvent: asyncEndEvent,
10083
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
10084
+ metrics: completion.metrics,
10085
+ output: completion.output,
10086
+ result: asyncEndEvent.result,
10087
+ span,
10088
+ startTime
10089
+ });
10090
+ }
9848
10091
  },
9849
10092
  error: (event) => {
9850
10093
  const spanData = states.get(event);
10094
+ logErrorAndEnd(states, event, channelName);
9851
10095
  if (spanData) {
9852
10096
  runStreamingErrorHook({
9853
10097
  channelName,
@@ -9858,7 +10102,6 @@ function traceStreamingChannel(channel2, config) {
9858
10102
  startTime: spanData.startTime
9859
10103
  });
9860
10104
  }
9861
- logErrorAndEnd(states, event);
9862
10105
  }
9863
10106
  };
9864
10107
  tracingChannel.subscribe(handlers);
@@ -9875,7 +10118,8 @@ function traceSyncStreamChannel(channel2, config) {
9875
10118
  tracingChannel,
9876
10119
  states,
9877
10120
  config,
9878
- channelName
10121
+ channelName,
10122
+ channel2.instrumentationName
9879
10123
  );
9880
10124
  const handlers = {
9881
10125
  start: (event) => {
@@ -9886,7 +10130,8 @@ function traceSyncStreamChannel(channel2, config) {
9886
10130
  states,
9887
10131
  config,
9888
10132
  event,
9889
- channelName
10133
+ channelName,
10134
+ channel2.instrumentationName
9890
10135
  );
9891
10136
  },
9892
10137
  end: (event) => {
@@ -9980,7 +10225,7 @@ function traceSyncStreamChannel(channel2, config) {
9980
10225
  handleResolvedResult(endEvent.result);
9981
10226
  },
9982
10227
  error: (event) => {
9983
- logErrorAndEnd(states, event);
10228
+ logErrorAndEnd(states, event, channelName);
9984
10229
  }
9985
10230
  };
9986
10231
  tracingChannel.subscribe(handlers);
@@ -10159,7 +10404,8 @@ function processInputAttachments(input) {
10159
10404
  function channel(spec) {
10160
10405
  return spec;
10161
10406
  }
10162
- function defineChannels(pkg, channels) {
10407
+ function defineChannels(pkg, channels, options) {
10408
+ const { instrumentationName } = options;
10163
10409
  return Object.fromEntries(
10164
10410
  Object.entries(channels).map(([key, spec]) => {
10165
10411
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -10172,6 +10418,7 @@ function defineChannels(pkg, channels) {
10172
10418
  key,
10173
10419
  {
10174
10420
  ...asyncSpec,
10421
+ instrumentationName,
10175
10422
  tracingChannel: tracingChannel2,
10176
10423
  tracePromise: (fn, context) => tracingChannel2().tracePromise(
10177
10424
  fn,
@@ -10189,6 +10436,7 @@ function defineChannels(pkg, channels) {
10189
10436
  key,
10190
10437
  {
10191
10438
  ...syncSpec,
10439
+ instrumentationName,
10192
10440
  tracingChannel,
10193
10441
  traceSync: (fn, context) => tracingChannel().traceSync(
10194
10442
  fn,
@@ -10202,44 +10450,48 @@ function defineChannels(pkg, channels) {
10202
10450
  }
10203
10451
 
10204
10452
  // src/instrumentation/plugins/openai-channels.ts
10205
- var openAIChannels = defineChannels("openai", {
10206
- chatCompletionsCreate: channel({
10207
- channelName: "chat.completions.create",
10208
- kind: "async"
10209
- }),
10210
- embeddingsCreate: channel({
10211
- channelName: "embeddings.create",
10212
- kind: "async"
10213
- }),
10214
- betaChatCompletionsParse: channel({
10215
- channelName: "beta.chat.completions.parse",
10216
- kind: "async"
10217
- }),
10218
- betaChatCompletionsStream: channel({
10219
- channelName: "beta.chat.completions.stream",
10220
- kind: "sync-stream"
10221
- }),
10222
- moderationsCreate: channel({
10223
- channelName: "moderations.create",
10224
- kind: "async"
10225
- }),
10226
- responsesCreate: channel({
10227
- channelName: "responses.create",
10228
- kind: "async"
10229
- }),
10230
- responsesStream: channel({
10231
- channelName: "responses.stream",
10232
- kind: "sync-stream"
10233
- }),
10234
- responsesParse: channel({
10235
- channelName: "responses.parse",
10236
- kind: "async"
10237
- }),
10238
- responsesCompact: channel({
10239
- channelName: "responses.compact",
10240
- kind: "async"
10241
- })
10242
- });
10453
+ var openAIChannels = defineChannels(
10454
+ "openai",
10455
+ {
10456
+ chatCompletionsCreate: channel({
10457
+ channelName: "chat.completions.create",
10458
+ kind: "async"
10459
+ }),
10460
+ embeddingsCreate: channel({
10461
+ channelName: "embeddings.create",
10462
+ kind: "async"
10463
+ }),
10464
+ betaChatCompletionsParse: channel({
10465
+ channelName: "beta.chat.completions.parse",
10466
+ kind: "async"
10467
+ }),
10468
+ betaChatCompletionsStream: channel({
10469
+ channelName: "beta.chat.completions.stream",
10470
+ kind: "sync-stream"
10471
+ }),
10472
+ moderationsCreate: channel({
10473
+ channelName: "moderations.create",
10474
+ kind: "async"
10475
+ }),
10476
+ responsesCreate: channel({
10477
+ channelName: "responses.create",
10478
+ kind: "async"
10479
+ }),
10480
+ responsesStream: channel({
10481
+ channelName: "responses.stream",
10482
+ kind: "sync-stream"
10483
+ }),
10484
+ responsesParse: channel({
10485
+ channelName: "responses.parse",
10486
+ kind: "async"
10487
+ }),
10488
+ responsesCompact: channel({
10489
+ channelName: "responses.compact",
10490
+ kind: "async"
10491
+ })
10492
+ },
10493
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
10494
+ );
10243
10495
 
10244
10496
  // src/openai-utils.ts
10245
10497
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -10754,16 +11006,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
10754
11006
  }
10755
11007
 
10756
11008
  // src/instrumentation/plugins/openai-codex-channels.ts
10757
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
10758
- run: channel({
10759
- channelName: "Thread.run",
10760
- kind: "async"
10761
- }),
10762
- runStreamed: channel({
10763
- channelName: "Thread.runStreamed",
10764
- kind: "async"
10765
- })
10766
- });
11009
+ var openAICodexChannels = defineChannels(
11010
+ "@openai/codex-sdk",
11011
+ {
11012
+ run: channel({
11013
+ channelName: "Thread.run",
11014
+ kind: "async"
11015
+ }),
11016
+ runStreamed: channel({
11017
+ channelName: "Thread.runStreamed",
11018
+ kind: "async"
11019
+ })
11020
+ },
11021
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
11022
+ );
10767
11023
 
10768
11024
  // src/instrumentation/plugins/openai-codex-plugin.ts
10769
11025
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -10851,10 +11107,15 @@ function startCodexRun(event, operation) {
10851
11107
  provider: "openai",
10852
11108
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
10853
11109
  };
10854
- const span = startSpan({
10855
- name: "OpenAI Codex",
10856
- spanAttributes: { type: "task" /* TASK */ }
10857
- });
11110
+ const span = startSpan(
11111
+ withSpanInstrumentationName(
11112
+ {
11113
+ name: "OpenAI Codex",
11114
+ spanAttributes: { type: "task" /* TASK */ }
11115
+ },
11116
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11117
+ )
11118
+ );
10858
11119
  const startTime = getCurrentUnixTimestamp();
10859
11120
  safeLog(span, {
10860
11121
  input: sanitizedInput,
@@ -11003,7 +11264,12 @@ async function createCompletedItemSpan(state, item) {
11003
11264
  if (!spanArgs) {
11004
11265
  return;
11005
11266
  }
11006
- const span = startSpan(spanArgs.start);
11267
+ const span = startSpan(
11268
+ withSpanInstrumentationName(
11269
+ spanArgs.start,
11270
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11271
+ )
11272
+ );
11007
11273
  safeLog(span, spanArgs.end);
11008
11274
  span.end();
11009
11275
  }
@@ -11045,15 +11311,20 @@ async function ensureActiveLlmSpan(state) {
11045
11311
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
11046
11312
  "openai_codex.llm_sequence": sequence
11047
11313
  };
11048
- const span = startSpan({
11049
- event: {
11050
- ...sequence === 1 ? { input: state.input } : {},
11051
- metadata
11052
- },
11053
- name: "OpenAI Codex LLM",
11054
- parent: await state.span.export(),
11055
- spanAttributes: { type: "llm" /* LLM */ }
11056
- });
11314
+ const span = startSpan(
11315
+ withSpanInstrumentationName(
11316
+ {
11317
+ event: {
11318
+ ...sequence === 1 ? { input: state.input } : {},
11319
+ metadata
11320
+ },
11321
+ name: "OpenAI Codex LLM",
11322
+ parent: await state.span.export(),
11323
+ spanAttributes: { type: "llm" /* LLM */ }
11324
+ },
11325
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11326
+ )
11327
+ );
11057
11328
  state.activeLlmSpan = {
11058
11329
  anonymousMessages: [],
11059
11330
  anonymousReasoning: [],
@@ -11099,7 +11370,15 @@ async function startCodexItemSpan(state, item) {
11099
11370
  if (!spanArgs) {
11100
11371
  return;
11101
11372
  }
11102
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
11373
+ state.activeItemSpans.set(
11374
+ itemId,
11375
+ startSpan(
11376
+ withSpanInstrumentationName(
11377
+ spanArgs.start,
11378
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11379
+ )
11380
+ )
11381
+ );
11103
11382
  }
11104
11383
  function updateCodexItem(state, item) {
11105
11384
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -11409,20 +11688,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
11409
11688
  }
11410
11689
 
11411
11690
  // src/instrumentation/plugins/anthropic-channels.ts
11412
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
11413
- messagesCreate: channel({
11414
- channelName: "messages.create",
11415
- kind: "async"
11416
- }),
11417
- betaMessagesCreate: channel({
11418
- channelName: "beta.messages.create",
11419
- kind: "async"
11420
- }),
11421
- betaMessagesToolRunner: channel({
11422
- channelName: "beta.messages.toolRunner",
11423
- kind: "sync-stream"
11424
- })
11425
- });
11691
+ var anthropicChannels = defineChannels(
11692
+ "@anthropic-ai/sdk",
11693
+ {
11694
+ messagesCreate: channel({
11695
+ channelName: "messages.create",
11696
+ kind: "async"
11697
+ }),
11698
+ betaMessagesCreate: channel({
11699
+ channelName: "beta.messages.create",
11700
+ kind: "async"
11701
+ }),
11702
+ betaMessagesToolRunner: channel({
11703
+ channelName: "beta.messages.toolRunner",
11704
+ kind: "sync-stream"
11705
+ })
11706
+ },
11707
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
11708
+ );
11426
11709
 
11427
11710
  // src/instrumentation/plugins/anthropic-plugin.ts
11428
11711
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -11495,12 +11778,17 @@ var AnthropicPlugin = class extends BasePlugin {
11495
11778
  return;
11496
11779
  }
11497
11780
  const params = event.arguments[0] ?? {};
11498
- const span = startSpan({
11499
- name: "anthropic.beta.messages.toolRunner",
11500
- spanAttributes: {
11501
- type: "task" /* TASK */
11502
- }
11503
- });
11781
+ const span = startSpan(
11782
+ withSpanInstrumentationName(
11783
+ {
11784
+ name: "anthropic.beta.messages.toolRunner",
11785
+ spanAttributes: {
11786
+ type: "task" /* TASK */
11787
+ }
11788
+ },
11789
+ INSTRUMENTATION_NAMES.ANTHROPIC
11790
+ )
11791
+ );
11504
11792
  span.log({
11505
11793
  input: processAttachmentsInInput(
11506
11794
  coalesceInput(params.messages ?? [], params.system)
@@ -11641,19 +11929,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
11641
11929
  return finalizeError(error);
11642
11930
  }
11643
11931
  },
11644
- {
11645
- event: {
11646
- input: getAnthropicToolRunnerInput(args),
11647
- metadata: {
11648
- "gen_ai.tool.name": toolName,
11649
- provider: "anthropic"
11932
+ withSpanInstrumentationName(
11933
+ {
11934
+ event: {
11935
+ input: getAnthropicToolRunnerInput(args),
11936
+ metadata: {
11937
+ "gen_ai.tool.name": toolName,
11938
+ provider: "anthropic"
11939
+ }
11940
+ },
11941
+ name: `tool: ${toolName}`,
11942
+ spanAttributes: {
11943
+ type: "tool" /* TOOL */
11650
11944
  }
11651
11945
  },
11652
- name: `tool: ${toolName}`,
11653
- spanAttributes: {
11654
- type: "tool" /* TOOL */
11655
- }
11656
- }
11946
+ INSTRUMENTATION_NAMES.ANTHROPIC
11947
+ )
11657
11948
  );
11658
11949
  },
11659
11950
  writable: runDescriptor?.writable ?? true
@@ -12248,32 +12539,296 @@ function currentWorkflowAgentWrapperSpan() {
12248
12539
  return void 0;
12249
12540
  }
12250
12541
 
12251
- // src/wrappers/ai-sdk/telemetry.ts
12252
- function braintrustAISDKTelemetry() {
12253
- const operations = /* @__PURE__ */ new Map();
12254
- const operationKeysByCallId = /* @__PURE__ */ new Map();
12255
- const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
12256
- const modelSpans = /* @__PURE__ */ new Map();
12257
- const objectSpans = /* @__PURE__ */ new Map();
12258
- const embedSpans = /* @__PURE__ */ new Map();
12259
- const rerankSpans = /* @__PURE__ */ new Map();
12260
- const toolSpans = /* @__PURE__ */ new Map();
12261
- let workflowAgentOperationCounter = 0;
12262
- const runSafely = (name, callback) => {
12263
- try {
12264
- callback();
12265
- } catch (error) {
12266
- console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
12267
- }
12542
+ // src/wrappers/ai-sdk/harness-agent-context.ts
12543
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
12544
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
12545
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
12546
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
12547
+ var harnessTurnParentStore;
12548
+ function serializedTurnContext(value) {
12549
+ if (!isObject(value)) {
12550
+ return void 0;
12551
+ }
12552
+ const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
12553
+ if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
12554
+ return void 0;
12555
+ }
12556
+ const expectedSignature = signTurnContext({
12557
+ parent: context.parent,
12558
+ sessionId: context.sessionId
12559
+ });
12560
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
12561
+ return void 0;
12562
+ }
12563
+ const parent = SpanComponentsV4.fromStr(context.parent);
12564
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
12565
+ return void 0;
12566
+ }
12567
+ return {
12568
+ parent: context.parent,
12569
+ sessionId: context.sessionId
12268
12570
  };
12269
- const startChildSpan = (operationKey, name, type, event) => {
12270
- const parent = operations.get(operationKey)?.span;
12271
- const spanArgs = {
12272
- name,
12273
- spanAttributes: { type },
12274
- ...event ? { event } : {}
12275
- };
12276
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
12571
+ }
12572
+ function signTurnContext(context) {
12573
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
12574
+ return secret ? isomorph_default.hmacSha256?.(
12575
+ secret,
12576
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
12577
+ ) : void 0;
12578
+ }
12579
+ function continuationParentFromCreateSessionParams(params) {
12580
+ if (!isObject(params)) {
12581
+ return void 0;
12582
+ }
12583
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
12584
+ const context = serializedTurnContext(continuation);
12585
+ if (!context) {
12586
+ return void 0;
12587
+ }
12588
+ if (params.sessionId !== context.sessionId) {
12589
+ return void 0;
12590
+ }
12591
+ return context.parent;
12592
+ }
12593
+ function exportSpanSynchronously(span) {
12594
+ const parentInfo = span.getParentInfo();
12595
+ if (!parentInfo) {
12596
+ return void 0;
12597
+ }
12598
+ const objectId = parentInfo.objectId.getSync().value;
12599
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
12600
+ return void 0;
12601
+ }
12602
+ return new SpanComponentsV4({
12603
+ object_type: parentInfo.objectType,
12604
+ ...objectId ? { object_id: objectId } : {
12605
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
12606
+ },
12607
+ row_id: span.id,
12608
+ root_span_id: span.rootSpanId,
12609
+ span_id: span.spanId
12610
+ }).toStr();
12611
+ }
12612
+ function exportedParent(parent) {
12613
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
12614
+ }
12615
+ function addSerializedContext(args) {
12616
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
12617
+ return;
12618
+ }
12619
+ const parent = exportedParent(args.parent);
12620
+ if (!parent) {
12621
+ return;
12622
+ }
12623
+ const context = {
12624
+ parent,
12625
+ sessionId: args.sessionId
12626
+ };
12627
+ const signature = signTurnContext(context);
12628
+ if (!signature) {
12629
+ return;
12630
+ }
12631
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
12632
+ configurable: true,
12633
+ enumerable: true,
12634
+ value: {
12635
+ ...context,
12636
+ signature
12637
+ },
12638
+ writable: true
12639
+ });
12640
+ }
12641
+ function lifecycleMethodDescriptor(session, method) {
12642
+ let owner = session;
12643
+ while (owner) {
12644
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
12645
+ if (descriptor) {
12646
+ return { descriptor, owner };
12647
+ }
12648
+ owner = Object.getPrototypeOf(owner);
12649
+ }
12650
+ return void 0;
12651
+ }
12652
+ function patchLifecycleMethod(session, method) {
12653
+ let resolvedDescriptor;
12654
+ try {
12655
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
12656
+ } catch {
12657
+ return;
12658
+ }
12659
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
12660
+ return;
12661
+ }
12662
+ const { descriptor, owner } = resolvedDescriptor;
12663
+ const patched = patchedLifecycleMethods.get(owner);
12664
+ if (patched?.has(method)) {
12665
+ return;
12666
+ }
12667
+ const original = descriptor.value;
12668
+ const replacement = function(...args) {
12669
+ const result = Reflect.apply(original, this, args);
12670
+ const addContext = (state) => {
12671
+ try {
12672
+ const parent = sessionTurnParents.get(this);
12673
+ if (!parent) {
12674
+ return;
12675
+ }
12676
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
12677
+ addSerializedContext({
12678
+ continuation,
12679
+ parent,
12680
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
12681
+ });
12682
+ } catch {
12683
+ }
12684
+ };
12685
+ try {
12686
+ if (isObject(result) && typeof result.then === "function") {
12687
+ void Promise.resolve(result).then(addContext, () => {
12688
+ });
12689
+ } else {
12690
+ addContext(result);
12691
+ }
12692
+ } catch {
12693
+ }
12694
+ return result;
12695
+ };
12696
+ try {
12697
+ Object.defineProperty(owner, method, {
12698
+ ...descriptor,
12699
+ value: replacement
12700
+ });
12701
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
12702
+ ownerMethods.add(method);
12703
+ if (!patched) {
12704
+ patchedLifecycleMethods.set(owner, ownerMethods);
12705
+ }
12706
+ } catch {
12707
+ }
12708
+ }
12709
+ function patchLifecycleMethods(session) {
12710
+ patchLifecycleMethod(session, "suspendTurn");
12711
+ patchLifecycleMethod(session, "detach");
12712
+ patchLifecycleMethod(session, "stop");
12713
+ }
12714
+ function registerHarnessTurnSpan(args) {
12715
+ if (!isObject(args.session)) {
12716
+ return;
12717
+ }
12718
+ const session = args.session;
12719
+ sessionTurnParents.set(session, args.span);
12720
+ wrapperTurnParents.set(args.span, args.span);
12721
+ patchLifecycleMethods(session);
12722
+ }
12723
+ function harnessContinuationParent(session) {
12724
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
12725
+ }
12726
+ function captureHarnessCreateSessionParent(params) {
12727
+ try {
12728
+ return continuationParentFromCreateSessionParams(params);
12729
+ } catch {
12730
+ return void 0;
12731
+ }
12732
+ }
12733
+ function registerHarnessSessionParent(session, parent) {
12734
+ if (!parent || !isObject(session)) {
12735
+ return;
12736
+ }
12737
+ const harnessSession = session;
12738
+ sessionTurnParents.set(harnessSession, parent);
12739
+ patchLifecycleMethods(harnessSession);
12740
+ }
12741
+ function currentHarnessTurnParent() {
12742
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
12743
+ }
12744
+ function bindHarnessTurnParentToStart(tracingChannel, parentFromEvent) {
12745
+ const startChannel = tracingChannel.start;
12746
+ if (!startChannel) {
12747
+ return () => {
12748
+ };
12749
+ }
12750
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
12751
+ const store = harnessTurnParentStore;
12752
+ startChannel.bindStore(
12753
+ store,
12754
+ (event) => parentFromEvent(event) ?? store.getStore()
12755
+ );
12756
+ return () => {
12757
+ startChannel.unbindStore(store);
12758
+ };
12759
+ }
12760
+ function startHarnessTurnChildSpan(parent, args) {
12761
+ const spanArgs = withSpanInstrumentationName(
12762
+ args,
12763
+ INSTRUMENTATION_NAMES.AI_SDK
12764
+ );
12765
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
12766
+ }
12767
+ function updateHarnessTurn(parent, update = {}) {
12768
+ if (!parent) {
12769
+ return;
12770
+ }
12771
+ const log = (event) => {
12772
+ if (typeof parent === "string") {
12773
+ updateSpan({ exported: parent, ...event });
12774
+ } else {
12775
+ parent.log(event);
12776
+ }
12777
+ };
12778
+ try {
12779
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
12780
+ log({ output: null });
12781
+ }
12782
+ log(update);
12783
+ } catch {
12784
+ }
12785
+ }
12786
+ function endHarnessTurn(parent) {
12787
+ const endTime = getCurrentUnixTimestamp();
12788
+ if (!parent) {
12789
+ return endTime;
12790
+ }
12791
+ try {
12792
+ const event = { metrics: { end: endTime } };
12793
+ if (typeof parent === "string") {
12794
+ updateSpan({ exported: parent, ...event });
12795
+ } else {
12796
+ parent.log(event);
12797
+ }
12798
+ } catch {
12799
+ }
12800
+ return endTime;
12801
+ }
12802
+
12803
+ // src/wrappers/ai-sdk/telemetry.ts
12804
+ function braintrustAISDKTelemetry() {
12805
+ const operations = /* @__PURE__ */ new Map();
12806
+ const operationKeysByCallId = /* @__PURE__ */ new Map();
12807
+ const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
12808
+ const modelSpans = /* @__PURE__ */ new Map();
12809
+ const objectSpans = /* @__PURE__ */ new Map();
12810
+ const embedSpans = /* @__PURE__ */ new Map();
12811
+ const rerankSpans = /* @__PURE__ */ new Map();
12812
+ const toolSpans = /* @__PURE__ */ new Map();
12813
+ let workflowAgentOperationCounter = 0;
12814
+ const runSafely = (name, callback) => {
12815
+ try {
12816
+ callback();
12817
+ } catch (error) {
12818
+ console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
12819
+ }
12820
+ };
12821
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
12822
+ const parent = operations.get(operationKey)?.span;
12823
+ const spanArgs = withSpanInstrumentationName(
12824
+ {
12825
+ name,
12826
+ spanAttributes: { type },
12827
+ ...event ? { event } : {}
12828
+ },
12829
+ INSTRUMENTATION_NAMES.AI_SDK
12830
+ );
12831
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
12277
12832
  const state = operations.get(operationKey);
12278
12833
  if (state && type === "llm" /* LLM */) {
12279
12834
  state.hadModelChild = true;
@@ -12553,14 +13108,30 @@ function braintrustAISDKTelemetry() {
12553
13108
  const workflowAgent = operationName === "WorkflowAgent.stream";
12554
13109
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
12555
13110
  const ownsSpan = !wrapperSpan;
12556
- const span = ownsSpan ? startSpan({
12557
- name: operationName,
12558
- spanAttributes: { type: "function" /* FUNCTION */ }
12559
- }) : wrapperSpan;
13111
+ const harnessTurnParent = currentHarnessTurnParent();
13112
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
13113
+ harnessTurnParent,
13114
+ withSpanInstrumentationName(
13115
+ {
13116
+ name: operationName,
13117
+ spanAttributes: { type: "function" /* FUNCTION */ }
13118
+ },
13119
+ INSTRUMENTATION_NAMES.AI_SDK
13120
+ )
13121
+ ) : startSpan(
13122
+ withSpanInstrumentationName(
13123
+ {
13124
+ name: operationName,
13125
+ spanAttributes: { type: "function" /* FUNCTION */ }
13126
+ },
13127
+ INSTRUMENTATION_NAMES.AI_SDK
13128
+ )
13129
+ ) : wrapperSpan;
12560
13130
  const operationKey = createOperationKey(event, operationName);
12561
13131
  registerOperation({
12562
13132
  callId: event.callId,
12563
13133
  hadModelChild: false,
13134
+ harnessTurnParent,
12564
13135
  loggedInput: false,
12565
13136
  operationName,
12566
13137
  operationKey,
@@ -12801,7 +13372,8 @@ function braintrustAISDKTelemetry() {
12801
13372
  toolCallId,
12802
13373
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
12803
13374
  }
12804
- }
13375
+ },
13376
+ state?.harnessTurnParent
12805
13377
  );
12806
13378
  toolSpans.set(toolCallId, { operationKey, span });
12807
13379
  });
@@ -13037,74 +13609,102 @@ function finishOutput(result, operationName) {
13037
13609
  }
13038
13610
 
13039
13611
  // src/instrumentation/plugins/ai-sdk-channels.ts
13040
- var aiSDKChannels = defineChannels("ai", {
13041
- generateText: channel({
13042
- channelName: "generateText",
13043
- kind: "async"
13044
- }),
13045
- streamText: channel({
13046
- channelName: "streamText",
13047
- kind: "async"
13048
- }),
13049
- streamTextSync: channel({
13050
- channelName: "streamText.sync",
13051
- kind: "sync-stream"
13052
- }),
13053
- generateObject: channel({
13054
- channelName: "generateObject",
13055
- kind: "async"
13056
- }),
13057
- streamObject: channel({
13058
- channelName: "streamObject",
13059
- kind: "async"
13060
- }),
13061
- streamObjectSync: channel({
13062
- channelName: "streamObject.sync",
13063
- kind: "sync-stream"
13064
- }),
13065
- embed: channel(
13066
- {
13612
+ var aiSDKChannels = defineChannels(
13613
+ "ai",
13614
+ {
13615
+ generateText: channel({
13616
+ channelName: "generateText",
13617
+ kind: "async"
13618
+ }),
13619
+ streamText: channel({
13620
+ channelName: "streamText",
13621
+ kind: "async"
13622
+ }),
13623
+ streamTextSync: channel({
13624
+ channelName: "streamText.sync",
13625
+ kind: "sync-stream"
13626
+ }),
13627
+ generateObject: channel({
13628
+ channelName: "generateObject",
13629
+ kind: "async"
13630
+ }),
13631
+ streamObject: channel({
13632
+ channelName: "streamObject",
13633
+ kind: "async"
13634
+ }),
13635
+ streamObjectSync: channel({
13636
+ channelName: "streamObject.sync",
13637
+ kind: "sync-stream"
13638
+ }),
13639
+ embed: channel({
13067
13640
  channelName: "embed",
13068
13641
  kind: "async"
13069
- }
13070
- ),
13071
- embedMany: channel({
13072
- channelName: "embedMany",
13073
- kind: "async"
13074
- }),
13075
- rerank: channel({
13076
- channelName: "rerank",
13077
- kind: "async"
13078
- }),
13079
- agentGenerate: channel({
13080
- channelName: "Agent.generate",
13081
- kind: "async"
13082
- }),
13083
- agentStream: channel({
13084
- channelName: "Agent.stream",
13085
- kind: "async"
13086
- }),
13087
- agentStreamSync: channel({
13088
- channelName: "Agent.stream.sync",
13089
- kind: "sync-stream"
13090
- }),
13091
- toolLoopAgentGenerate: channel({
13092
- channelName: "ToolLoopAgent.generate",
13093
- kind: "async"
13094
- }),
13095
- toolLoopAgentStream: channel({
13096
- channelName: "ToolLoopAgent.stream",
13097
- kind: "async"
13098
- }),
13099
- workflowAgentStream: channel({
13100
- channelName: "WorkflowAgent.stream",
13101
- kind: "async"
13102
- }),
13103
- v7CreateTelemetryDispatcher: channel({
13104
- channelName: "createTelemetryDispatcher",
13105
- kind: "sync-stream"
13106
- })
13107
- });
13642
+ }),
13643
+ embedMany: channel({
13644
+ channelName: "embedMany",
13645
+ kind: "async"
13646
+ }),
13647
+ rerank: channel({
13648
+ channelName: "rerank",
13649
+ kind: "async"
13650
+ }),
13651
+ agentGenerate: channel({
13652
+ channelName: "Agent.generate",
13653
+ kind: "async"
13654
+ }),
13655
+ agentStream: channel({
13656
+ channelName: "Agent.stream",
13657
+ kind: "async"
13658
+ }),
13659
+ agentStreamSync: channel({
13660
+ channelName: "Agent.stream.sync",
13661
+ kind: "sync-stream"
13662
+ }),
13663
+ toolLoopAgentGenerate: channel({
13664
+ channelName: "ToolLoopAgent.generate",
13665
+ kind: "async"
13666
+ }),
13667
+ toolLoopAgentStream: channel({
13668
+ channelName: "ToolLoopAgent.stream",
13669
+ kind: "async"
13670
+ }),
13671
+ workflowAgentStream: channel({
13672
+ channelName: "WorkflowAgent.stream",
13673
+ kind: "async"
13674
+ }),
13675
+ v7CreateTelemetryDispatcher: channel({
13676
+ channelName: "createTelemetryDispatcher",
13677
+ kind: "sync-stream"
13678
+ })
13679
+ },
13680
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
13681
+ );
13682
+ var harnessAgentChannels = defineChannels(
13683
+ "@ai-sdk/harness",
13684
+ {
13685
+ createSession: channel({
13686
+ channelName: "HarnessAgent.createSession",
13687
+ kind: "async"
13688
+ }),
13689
+ generate: channel({
13690
+ channelName: "HarnessAgent.generate",
13691
+ kind: "async"
13692
+ }),
13693
+ stream: channel({
13694
+ channelName: "HarnessAgent.stream",
13695
+ kind: "async"
13696
+ }),
13697
+ continueGenerate: channel({
13698
+ channelName: "HarnessAgent.continueGenerate",
13699
+ kind: "async"
13700
+ }),
13701
+ continueStream: channel({
13702
+ channelName: "HarnessAgent.continueStream",
13703
+ kind: "async"
13704
+ })
13705
+ },
13706
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
13707
+ );
13108
13708
 
13109
13709
  // src/instrumentation/plugins/ai-sdk-plugin.ts
13110
13710
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -13181,6 +13781,17 @@ var AISDKPlugin = class extends BasePlugin {
13181
13781
  subscribeToAISDK() {
13182
13782
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
13183
13783
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
13784
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
13785
+ this.unsubscribers.push(
13786
+ subscribeToHarnessContinuation(
13787
+ harnessAgentChannels.continueGenerate,
13788
+ denyOutputPaths
13789
+ ),
13790
+ subscribeToHarnessContinuation(
13791
+ harnessAgentChannels.continueStream,
13792
+ denyOutputPaths
13793
+ )
13794
+ );
13184
13795
  this.unsubscribers.push(
13185
13796
  traceStreamingChannel(aiSDKChannels.generateText, {
13186
13797
  name: "generateText",
@@ -13373,6 +13984,90 @@ var AISDKPlugin = class extends BasePlugin {
13373
13984
  })
13374
13985
  })
13375
13986
  );
13987
+ this.unsubscribers.push(
13988
+ traceStreamingChannel(harnessAgentChannels.generate, {
13989
+ name: "HarnessAgent.generate",
13990
+ startSpan: _internalStartSpanWithInitialMerge,
13991
+ type: "task" /* TASK */,
13992
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
13993
+ extractOutput: (result, endEvent) => processAISDKOutput(
13994
+ result,
13995
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
13996
+ ),
13997
+ extractMetrics: (result) => extractTokenMetrics(result),
13998
+ aggregateChunks: aggregateAISDKChunks
13999
+ })
14000
+ );
14001
+ this.unsubscribers.push(
14002
+ traceStreamingChannel(harnessAgentChannels.stream, {
14003
+ name: "HarnessAgent.stream",
14004
+ startSpan: _internalStartSpanWithInitialMerge,
14005
+ type: "task" /* TASK */,
14006
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14007
+ extractOutput: (result, endEvent) => processAISDKOutput(
14008
+ result,
14009
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14010
+ ),
14011
+ extractMetrics: (result, startTime) => ({
14012
+ ...extractTokenMetrics(result),
14013
+ ...startTime === void 0 ? {} : {
14014
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
14015
+ }
14016
+ }),
14017
+ aggregateChunks: aggregateAISDKChunks,
14018
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
14019
+ defaultDenyOutputPaths: denyOutputPaths,
14020
+ endEvent,
14021
+ result,
14022
+ resolvePromiseUsage: true,
14023
+ span,
14024
+ startTime
14025
+ })
14026
+ })
14027
+ );
14028
+ this.unsubscribers.push(
14029
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
14030
+ name: "HarnessAgent.continueGenerate",
14031
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
14032
+ startSpan: _internalStartSpanWithInitialMerge,
14033
+ type: "task" /* TASK */,
14034
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14035
+ extractOutput: (result, endEvent) => processAISDKOutput(
14036
+ result,
14037
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14038
+ ),
14039
+ extractMetrics: (result) => extractTokenMetrics(result),
14040
+ aggregateChunks: aggregateAISDKChunks
14041
+ })
14042
+ );
14043
+ this.unsubscribers.push(
14044
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
14045
+ name: "HarnessAgent.continueStream",
14046
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
14047
+ startSpan: _internalStartSpanWithInitialMerge,
14048
+ type: "task" /* TASK */,
14049
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14050
+ extractOutput: (result, endEvent) => processAISDKOutput(
14051
+ result,
14052
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14053
+ ),
14054
+ extractMetrics: (result, startTime) => ({
14055
+ ...extractTokenMetrics(result),
14056
+ ...startTime === void 0 ? {} : {
14057
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
14058
+ }
14059
+ }),
14060
+ aggregateChunks: aggregateAISDKChunks,
14061
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
14062
+ defaultDenyOutputPaths: denyOutputPaths,
14063
+ endEvent,
14064
+ result,
14065
+ resolvePromiseUsage: true,
14066
+ span,
14067
+ startTime
14068
+ })
14069
+ })
14070
+ );
13376
14071
  this.unsubscribers.push(
13377
14072
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
13378
14073
  name: "ToolLoopAgent.generate",
@@ -13442,15 +14137,164 @@ var AISDKPlugin = class extends BasePlugin {
13442
14137
  patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
13443
14138
  defaultDenyOutputPaths: denyOutputPaths,
13444
14139
  endEvent,
13445
- onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
13446
- result,
14140
+ onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
14141
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
14142
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
14143
+ result,
14144
+ span,
14145
+ startTime
14146
+ })
14147
+ })
14148
+ );
14149
+ }
14150
+ };
14151
+ function subscribeToHarnessAgentCreateSession() {
14152
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
14153
+ const parents = /* @__PURE__ */ new WeakMap();
14154
+ const handlers = {
14155
+ start: (event) => {
14156
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
14157
+ if (parent) {
14158
+ parents.set(event, parent);
14159
+ }
14160
+ },
14161
+ asyncEnd: (event) => {
14162
+ registerHarnessSessionParent(event.result, parents.get(event));
14163
+ parents.delete(event);
14164
+ },
14165
+ error: (event) => {
14166
+ parents.delete(event);
14167
+ }
14168
+ };
14169
+ channel2.subscribe(handlers);
14170
+ return () => channel2.unsubscribe(handlers);
14171
+ }
14172
+ function harnessContinuationParentFromEvent(event) {
14173
+ try {
14174
+ return harnessContinuationParent(event.arguments?.[0]?.session);
14175
+ } catch {
14176
+ return void 0;
14177
+ }
14178
+ }
14179
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
14180
+ const channel2 = continuationChannel.tracingChannel();
14181
+ const parents = /* @__PURE__ */ new WeakMap();
14182
+ const startTimes = /* @__PURE__ */ new WeakMap();
14183
+ const unbindParentStore = bindHarnessTurnParentToStart(
14184
+ channel2,
14185
+ harnessContinuationParentFromEvent
14186
+ );
14187
+ const handlers = {
14188
+ start: (event) => {
14189
+ const parent = harnessContinuationParentFromEvent(event);
14190
+ if (!parent) {
14191
+ return;
14192
+ }
14193
+ parents.set(event, parent);
14194
+ startTimes.set(event, getCurrentUnixTimestamp());
14195
+ try {
14196
+ const params = event.arguments?.[0];
14197
+ if (params) {
14198
+ prepareAISDKHarnessAgentInput(params, event.self);
14199
+ }
14200
+ } catch (error) {
14201
+ debugLogger.error(
14202
+ "Error preparing Harness continuation telemetry:",
14203
+ error
14204
+ );
14205
+ }
14206
+ },
14207
+ asyncEnd: (event) => {
14208
+ const parent = parents.get(event);
14209
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
14210
+ parents.delete(event);
14211
+ startTimes.delete(event);
14212
+ if (!parent) {
14213
+ return;
14214
+ }
14215
+ const endEvent = event;
14216
+ const span = {
14217
+ end: () => endHarnessTurn(parent),
14218
+ log: (update) => updateHarnessTurn(
14219
+ parent,
14220
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
14221
+ )
14222
+ };
14223
+ try {
14224
+ if (isAsyncIterable(endEvent.result)) {
14225
+ patchStreamIfNeeded(endEvent.result, {
14226
+ onComplete: (chunks) => {
14227
+ try {
14228
+ const { metadata, metrics, output } = aggregateAISDKChunks(
14229
+ chunks,
14230
+ endEvent.result,
14231
+ endEvent
14232
+ );
14233
+ span.log({
14234
+ ...metadata ? { metadata } : {},
14235
+ metrics,
14236
+ output
14237
+ });
14238
+ } catch (error) {
14239
+ debugLogger.error(
14240
+ "Error aggregating Harness continuation stream:",
14241
+ error
14242
+ );
14243
+ } finally {
14244
+ span.end();
14245
+ }
14246
+ },
14247
+ onError: (error) => {
14248
+ span.log({ error: toLoggedError(error), output: null });
14249
+ span.end();
14250
+ }
14251
+ });
14252
+ return;
14253
+ }
14254
+ if (patchAISDKStreamingResult({
14255
+ defaultDenyOutputPaths,
14256
+ endEvent,
14257
+ result: endEvent.result,
14258
+ resolvePromiseUsage: true,
13447
14259
  span,
13448
14260
  startTime
13449
- })
13450
- })
13451
- );
13452
- }
13453
- };
14261
+ })) {
14262
+ return;
14263
+ }
14264
+ finalizeAISDKChildTracing(endEvent);
14265
+ span.log({
14266
+ metrics: extractTokenMetrics(endEvent.result),
14267
+ output: processAISDKOutput(
14268
+ endEvent.result,
14269
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
14270
+ )
14271
+ });
14272
+ span.end();
14273
+ } catch (error) {
14274
+ debugLogger.error("Error tracing Harness continuation:", error);
14275
+ span.end();
14276
+ }
14277
+ },
14278
+ error: (event) => {
14279
+ const parent = parents.get(event);
14280
+ parents.delete(event);
14281
+ startTimes.delete(event);
14282
+ if (!parent) {
14283
+ return;
14284
+ }
14285
+ updateHarnessTurn(parent, {
14286
+ error: toLoggedError(event.error),
14287
+ output: null
14288
+ });
14289
+ endHarnessTurn(parent);
14290
+ }
14291
+ };
14292
+ channel2.subscribe(handlers);
14293
+ return () => {
14294
+ unbindParentStore();
14295
+ channel2.unsubscribe(handlers);
14296
+ };
14297
+ }
13454
14298
  function subscribeToAISDKV7TelemetryDispatcher() {
13455
14299
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
13456
14300
  const telemetry = braintrustAISDKTelemetry();
@@ -13903,6 +14747,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
13903
14747
  metadata
13904
14748
  };
13905
14749
  }
14750
+ function prepareAISDKHarnessAgentInput(params, self, span) {
14751
+ if (span) {
14752
+ registerHarnessTurnSpan({
14753
+ session: params.session,
14754
+ span
14755
+ });
14756
+ }
14757
+ if (isObject(self)) {
14758
+ try {
14759
+ if (isObject(self.settings)) {
14760
+ const settings = self.settings;
14761
+ if (settings.telemetry === void 0) {
14762
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
14763
+ }
14764
+ }
14765
+ } catch {
14766
+ }
14767
+ }
14768
+ const selectedInput = {};
14769
+ if (params.prompt !== void 0) {
14770
+ selectedInput.prompt = params.prompt;
14771
+ }
14772
+ if (params.messages !== void 0) {
14773
+ selectedInput.messages = params.messages;
14774
+ }
14775
+ if (params.toolApprovalContinuations !== void 0) {
14776
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
14777
+ }
14778
+ const metadata = extractMetadataFromCallParams({}, self);
14779
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
14780
+ metadata.sessionId = params.session.sessionId;
14781
+ }
14782
+ if (isObject(self)) {
14783
+ if (typeof self.harnessId === "string") {
14784
+ metadata.harnessId = self.harnessId;
14785
+ }
14786
+ if (typeof self.permissionMode === "string") {
14787
+ metadata.permissionMode = self.permissionMode;
14788
+ }
14789
+ }
14790
+ return {
14791
+ input: processAISDKCallInput(selectedInput).input,
14792
+ metadata
14793
+ };
14794
+ }
14795
+ function harnessSessionFromArguments(args) {
14796
+ const params = args[0];
14797
+ return isObject(params) ? params.session : void 0;
14798
+ }
13906
14799
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
13907
14800
  registerWorkflowAgentWrapperSpan(span);
13908
14801
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -14350,19 +15243,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14350
15243
  activeEntry.openSpans.delete(span);
14351
15244
  }
14352
15245
  },
14353
- {
14354
- name: "doGenerate",
14355
- spanAttributes: {
14356
- type: "llm" /* LLM */
15246
+ withSpanInstrumentationName(
15247
+ {
15248
+ name: "doGenerate",
15249
+ spanAttributes: {
15250
+ type: "llm" /* LLM */
15251
+ },
15252
+ event: buildAISDKModelStartEvent(
15253
+ callOptions,
15254
+ activeEntry.baseMetadata,
15255
+ {
15256
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
15257
+ }
15258
+ )
14357
15259
  },
14358
- event: buildAISDKModelStartEvent(
14359
- callOptions,
14360
- activeEntry.baseMetadata,
14361
- {
14362
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
14363
- }
14364
- )
14365
- }
15260
+ INSTRUMENTATION_NAMES.AI_SDK
15261
+ )
14366
15262
  );
14367
15263
  };
14368
15264
  if (originalDoStream) {
@@ -14372,19 +15268,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14372
15268
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
14373
15269
  }
14374
15270
  closeOpenAISDKModelPatchSpans(activeEntry);
14375
- const span = activeEntry.parentSpan.startSpan({
14376
- name: "doStream",
14377
- spanAttributes: {
14378
- type: "llm" /* LLM */
14379
- },
14380
- event: buildAISDKModelStartEvent(
14381
- callOptions,
14382
- activeEntry.baseMetadata,
15271
+ const span = activeEntry.parentSpan.startSpan(
15272
+ withSpanInstrumentationName(
14383
15273
  {
14384
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
14385
- }
15274
+ name: "doStream",
15275
+ spanAttributes: {
15276
+ type: "llm" /* LLM */
15277
+ },
15278
+ event: buildAISDKModelStartEvent(
15279
+ callOptions,
15280
+ activeEntry.baseMetadata,
15281
+ {
15282
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
15283
+ }
15284
+ )
15285
+ },
15286
+ INSTRUMENTATION_NAMES.AI_SDK
14386
15287
  )
14387
- });
15288
+ );
14388
15289
  activeEntry.openSpans.add(span);
14389
15290
  const streamStartTime = getCurrentUnixTimestamp();
14390
15291
  const result = await withCurrent(
@@ -14556,12 +15457,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14556
15457
  }
14557
15458
  if (isAsyncGenerator(result)) {
14558
15459
  return (async function* () {
14559
- const span = activeEntry.parentSpan.startSpan({
14560
- name: activeEntry.name,
14561
- spanAttributes: {
14562
- type: "tool" /* TOOL */
14563
- }
14564
- });
15460
+ const span = activeEntry.parentSpan.startSpan(
15461
+ withSpanInstrumentationName(
15462
+ {
15463
+ name: activeEntry.name,
15464
+ spanAttributes: {
15465
+ type: "tool" /* TOOL */
15466
+ }
15467
+ },
15468
+ INSTRUMENTATION_NAMES.AI_SDK
15469
+ )
15470
+ );
14565
15471
  span.log({ input: serializeToolExecutionInput(args) });
14566
15472
  try {
14567
15473
  let lastValue;
@@ -14585,12 +15491,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14585
15491
  span.log({ output: awaitedResult });
14586
15492
  return awaitedResult;
14587
15493
  },
14588
- {
14589
- name: activeEntry.name,
14590
- spanAttributes: {
14591
- type: "tool" /* TOOL */
14592
- }
14593
- }
15494
+ withSpanInstrumentationName(
15495
+ {
15496
+ name: activeEntry.name,
15497
+ spanAttributes: {
15498
+ type: "tool" /* TOOL */
15499
+ }
15500
+ },
15501
+ INSTRUMENTATION_NAMES.AI_SDK
15502
+ )
14594
15503
  );
14595
15504
  };
14596
15505
  cleanup.push(() => {
@@ -14749,7 +15658,10 @@ function patchAISDKStreamingResult(args) {
14749
15658
  defaultDenyOutputPaths,
14750
15659
  endEvent,
14751
15660
  onComplete,
15661
+ onCancel,
15662
+ onError,
14752
15663
  result,
15664
+ resolvePromiseUsage,
14753
15665
  span,
14754
15666
  startTime
14755
15667
  } = args;
@@ -14759,14 +15671,43 @@ function patchAISDKStreamingResult(args) {
14759
15671
  const resultRecord = result;
14760
15672
  attachKnownResultPromiseHandlers(resultRecord);
14761
15673
  let finalized = false;
14762
- const finalize = () => {
15674
+ const finalize = (outcome) => {
14763
15675
  if (finalized) {
14764
15676
  return;
14765
15677
  }
14766
15678
  finalized = true;
14767
- finalizeAISDKChildTracing(endEvent);
14768
- span.end();
14769
- onComplete?.();
15679
+ try {
15680
+ finalizeAISDKChildTracing(endEvent);
15681
+ } catch (error) {
15682
+ debugLogger.error("Error finalizing AI SDK child tracing:", error);
15683
+ }
15684
+ try {
15685
+ span.end();
15686
+ } catch (error) {
15687
+ debugLogger.error("Error ending AI SDK streaming span:", error);
15688
+ }
15689
+ if (outcome && "error" in outcome) {
15690
+ try {
15691
+ onError?.(outcome.error);
15692
+ } catch (error) {
15693
+ debugLogger.error("Error in AI SDK streaming error hook:", error);
15694
+ }
15695
+ } else if (outcome) {
15696
+ try {
15697
+ onComplete?.(outcome);
15698
+ } catch (error) {
15699
+ debugLogger.error("Error in AI SDK streaming completion hook:", error);
15700
+ }
15701
+ } else {
15702
+ try {
15703
+ onCancel?.();
15704
+ } catch (error) {
15705
+ debugLogger.error(
15706
+ "Error in AI SDK streaming cancellation hook:",
15707
+ error
15708
+ );
15709
+ }
15710
+ }
14770
15711
  };
14771
15712
  let outputLogged = false;
14772
15713
  const logStreamingOutput = async (firstChunkTime) => {
@@ -14776,6 +15717,22 @@ function patchAISDKStreamingResult(args) {
14776
15717
  outputLogged = true;
14777
15718
  try {
14778
15719
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
15720
+ if (resolvePromiseUsage) {
15721
+ try {
15722
+ const resultRecord2 = result;
15723
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
15724
+ if (isPromiseLike(pendingUsage)) {
15725
+ const usage = await Promise.resolve(pendingUsage);
15726
+ if (isObject(usage)) {
15727
+ Object.assign(
15728
+ metrics,
15729
+ extractTokenMetrics({ usage })
15730
+ );
15731
+ }
15732
+ }
15733
+ } catch {
15734
+ }
15735
+ }
14779
15736
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
14780
15737
  metrics.time_to_first_token = firstChunkTime - startTime;
14781
15738
  }
@@ -14784,15 +15741,29 @@ function patchAISDKStreamingResult(args) {
14784
15741
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
14785
15742
  );
14786
15743
  const metadata = buildResolvedMetadataPayload(result).metadata;
14787
- span.log({
14788
- output,
14789
- ...metadata ? { metadata } : {},
14790
- metrics
14791
- });
15744
+ try {
15745
+ span.log({
15746
+ output,
15747
+ ...metadata ? { metadata } : {},
15748
+ metrics
15749
+ });
15750
+ } catch (error) {
15751
+ debugLogger.error("Error logging AI SDK streaming output:", error);
15752
+ }
15753
+ finalize({ metrics, output });
14792
15754
  } catch (error) {
14793
- span.log({ error: toLoggedError(error) });
14794
- } finally {
14795
- finalize();
15755
+ const loggedError = toLoggedError(error);
15756
+ try {
15757
+ span.log({ error: loggedError });
15758
+ } catch (loggingError) {
15759
+ debugLogger.error(
15760
+ "Error logging AI SDK streaming failure:",
15761
+ loggingError
15762
+ );
15763
+ }
15764
+ finalize({
15765
+ error: error instanceof Error ? error : new Error(String(loggedError))
15766
+ });
14796
15767
  }
14797
15768
  };
14798
15769
  const createAsyncIterableHooks = () => {
@@ -14809,11 +15780,16 @@ function patchAISDKStreamingResult(args) {
14809
15780
  onError: (error) => {
14810
15781
  if (!outputLogged) {
14811
15782
  outputLogged = true;
14812
- span.log({
14813
- error: error.message
14814
- });
15783
+ try {
15784
+ span.log({ error });
15785
+ } catch (loggingError) {
15786
+ debugLogger.error(
15787
+ "Error logging AI SDK stream failure:",
15788
+ loggingError
15789
+ );
15790
+ }
14815
15791
  }
14816
- finalize();
15792
+ finalize({ error });
14817
15793
  },
14818
15794
  onCancel: finalize
14819
15795
  };
@@ -14899,8 +15875,18 @@ function patchAISDKStreamingResult(args) {
14899
15875
  }
14900
15876
  controller.enqueue(value);
14901
15877
  } catch (error) {
14902
- span.log({ error: toLoggedError(error) });
14903
- finalize();
15878
+ const loggedError = toLoggedError(error);
15879
+ try {
15880
+ span.log({ error: loggedError });
15881
+ } catch (loggingError) {
15882
+ debugLogger.error(
15883
+ "Error logging AI SDK base stream failure:",
15884
+ loggingError
15885
+ );
15886
+ }
15887
+ finalize({
15888
+ error: error instanceof Error ? error : new Error(String(loggedError))
15889
+ });
14904
15890
  controller.error(error);
14905
15891
  }
14906
15892
  },
@@ -15689,7 +16675,8 @@ var claudeAgentSDKChannels = defineChannels(
15689
16675
  channelName: "query",
15690
16676
  kind: "sync-stream"
15691
16677
  })
15692
- }
16678
+ },
16679
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
15693
16680
  );
15694
16681
 
15695
16682
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -15837,22 +16824,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
15837
16824
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
15838
16825
  const runWithResolvedParent = async () => {
15839
16826
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
15840
- const span = startSpan({
15841
- event: {
15842
- input: handlerArgs[0],
15843
- metadata: {
15844
- "claude_agent_sdk.raw_tool_name": rawToolName,
15845
- "gen_ai.tool.name": metadata.toolName,
15846
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
15847
- ...metadata.serverName && {
15848
- "mcp.server": metadata.serverName
15849
- }
15850
- }
15851
- },
15852
- name: spanName,
15853
- ...parent && { parent },
15854
- spanAttributes: { type: "tool" /* TOOL */ }
15855
- });
16827
+ const span = startSpan(
16828
+ withSpanInstrumentationName(
16829
+ {
16830
+ event: {
16831
+ input: handlerArgs[0],
16832
+ metadata: {
16833
+ "claude_agent_sdk.raw_tool_name": rawToolName,
16834
+ "gen_ai.tool.name": metadata.toolName,
16835
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
16836
+ ...metadata.serverName && {
16837
+ "mcp.server": metadata.serverName
16838
+ }
16839
+ }
16840
+ },
16841
+ name: spanName,
16842
+ ...parent && { parent },
16843
+ spanAttributes: { type: "tool" /* TOOL */ }
16844
+ },
16845
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
16846
+ )
16847
+ );
15856
16848
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
15857
16849
  const finalizeSuccess = (result) => {
15858
16850
  span.log({ output: result });
@@ -16177,14 +17169,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
16177
17169
  ).filter(
16178
17170
  (c) => c !== void 0
16179
17171
  );
16180
- const span = existingSpan ?? startSpan({
16181
- name: "anthropic.messages.create",
16182
- parent: parentSpan,
16183
- spanAttributes: {
16184
- type: "llm" /* LLM */
16185
- },
16186
- startTime
16187
- });
17172
+ const span = existingSpan ?? startSpan(
17173
+ withSpanInstrumentationName(
17174
+ {
17175
+ name: "anthropic.messages.create",
17176
+ parent: parentSpan,
17177
+ spanAttributes: {
17178
+ type: "llm" /* LLM */
17179
+ },
17180
+ startTime
17181
+ },
17182
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17183
+ )
17184
+ );
16188
17185
  span.log({
16189
17186
  input,
16190
17187
  metadata: model ? { model } : void 0,
@@ -16287,25 +17284,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
16287
17284
  return {};
16288
17285
  }
16289
17286
  const parsed = parseToolName(input.tool_name);
16290
- const toolSpan = startSpan({
16291
- event: {
16292
- input: input.tool_input,
16293
- metadata: {
16294
- "claude_agent_sdk.cwd": input.cwd,
16295
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
16296
- "claude_agent_sdk.session_id": input.session_id,
16297
- "gen_ai.tool.call.id": toolUseID,
16298
- "gen_ai.tool.name": parsed.toolName,
16299
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
16300
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
16301
- }
16302
- },
16303
- name: parsed.displayName,
16304
- parent: await resolveParentSpan(toolUseID, {
16305
- agentId: input.agent_id
16306
- }),
16307
- spanAttributes: { type: "tool" /* TOOL */ }
16308
- });
17287
+ const toolSpan = startSpan(
17288
+ withSpanInstrumentationName(
17289
+ {
17290
+ event: {
17291
+ input: input.tool_input,
17292
+ metadata: {
17293
+ "claude_agent_sdk.cwd": input.cwd,
17294
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
17295
+ "claude_agent_sdk.session_id": input.session_id,
17296
+ "gen_ai.tool.call.id": toolUseID,
17297
+ "gen_ai.tool.name": parsed.toolName,
17298
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
17299
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
17300
+ }
17301
+ },
17302
+ name: parsed.displayName,
17303
+ parent: await resolveParentSpan(toolUseID, {
17304
+ agentId: input.agent_id
17305
+ }),
17306
+ spanAttributes: { type: "tool" /* TOOL */ }
17307
+ },
17308
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17309
+ )
17310
+ );
16309
17311
  activeToolSpans.set(toolUseID, toolSpan);
16310
17312
  return {};
16311
17313
  };
@@ -16671,14 +17673,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
16671
17673
  const spanName = formatSubAgentSpanName(details);
16672
17674
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
16673
17675
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
16674
- const subAgentSpan = startSpan({
16675
- event: {
16676
- metadata: subAgentDetailsToMetadata(details)
16677
- },
16678
- name: spanName,
16679
- parent: parentSpan,
16680
- spanAttributes: { type: "task" /* TASK */ }
16681
- });
17676
+ const subAgentSpan = startSpan(
17677
+ withSpanInstrumentationName(
17678
+ {
17679
+ event: {
17680
+ metadata: subAgentDetailsToMetadata(details)
17681
+ },
17682
+ name: spanName,
17683
+ parent: parentSpan,
17684
+ spanAttributes: { type: "task" /* TASK */ }
17685
+ },
17686
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17687
+ )
17688
+ );
16682
17689
  subAgentSpans.set(parentToolUseId, subAgentSpan);
16683
17690
  return subAgentSpan;
16684
17691
  }
@@ -16699,14 +17706,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
16699
17706
  );
16700
17707
  llmParentSpan = await subAgentSpan.export();
16701
17708
  }
16702
- const llmSpan = startSpan({
16703
- name: "anthropic.messages.create",
16704
- parent: llmParentSpan,
16705
- spanAttributes: {
16706
- type: "llm" /* LLM */
16707
- },
16708
- startTime
16709
- });
17709
+ const llmSpan = startSpan(
17710
+ withSpanInstrumentationName(
17711
+ {
17712
+ name: "anthropic.messages.create",
17713
+ parent: llmParentSpan,
17714
+ spanAttributes: {
17715
+ type: "llm" /* LLM */
17716
+ },
17717
+ startTime
17718
+ },
17719
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17720
+ )
17721
+ );
16710
17722
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
16711
17723
  return llmSpan;
16712
17724
  }
@@ -16975,12 +17987,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
16975
17987
  }
16976
17988
  })();
16977
17989
  }
16978
- const span = startSpan({
16979
- name: "Claude Agent",
16980
- spanAttributes: {
16981
- type: "task" /* TASK */
16982
- }
16983
- });
17990
+ const span = startSpan(
17991
+ withSpanInstrumentationName(
17992
+ {
17993
+ name: "Claude Agent",
17994
+ spanAttributes: {
17995
+ type: "task" /* TASK */
17996
+ }
17997
+ },
17998
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17999
+ )
18000
+ );
16984
18001
  const startTime = getCurrentUnixTimestamp();
16985
18002
  try {
16986
18003
  span.log({
@@ -17142,27 +18159,31 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
17142
18159
  channel2.unsubscribe(handlers);
17143
18160
  });
17144
18161
  }
17145
- };
17146
-
17147
- // src/instrumentation/plugins/cursor-sdk-channels.ts
17148
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
17149
- create: channel({
17150
- channelName: "Agent.create",
17151
- kind: "async"
17152
- }),
17153
- resume: channel({
17154
- channelName: "Agent.resume",
17155
- kind: "async"
17156
- }),
17157
- prompt: channel({
17158
- channelName: "Agent.prompt",
17159
- kind: "async"
17160
- }),
17161
- send: channel({
17162
- channelName: "agent.send",
17163
- kind: "async"
17164
- })
17165
- });
18162
+ };
18163
+
18164
+ // src/instrumentation/plugins/cursor-sdk-channels.ts
18165
+ var cursorSDKChannels = defineChannels(
18166
+ "@cursor/sdk",
18167
+ {
18168
+ create: channel({
18169
+ channelName: "Agent.create",
18170
+ kind: "async"
18171
+ }),
18172
+ resume: channel({
18173
+ channelName: "Agent.resume",
18174
+ kind: "async"
18175
+ }),
18176
+ prompt: channel({
18177
+ channelName: "Agent.prompt",
18178
+ kind: "async"
18179
+ }),
18180
+ send: channel({
18181
+ channelName: "agent.send",
18182
+ kind: "async"
18183
+ })
18184
+ },
18185
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
18186
+ );
17166
18187
 
17167
18188
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
17168
18189
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -17214,10 +18235,15 @@ var CursorSDKPlugin = class extends BasePlugin {
17214
18235
  provider: "cursor",
17215
18236
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
17216
18237
  };
17217
- const span = startSpan({
17218
- name: "Cursor Agent",
17219
- spanAttributes: { type: "task" /* TASK */ }
17220
- });
18238
+ const span = startSpan(
18239
+ withSpanInstrumentationName(
18240
+ {
18241
+ name: "Cursor Agent",
18242
+ spanAttributes: { type: "task" /* TASK */ }
18243
+ },
18244
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18245
+ )
18246
+ );
17221
18247
  const startTime = getCurrentUnixTimestamp();
17222
18248
  safeLog2(span, {
17223
18249
  input: sanitizeUserMessage(message),
@@ -17279,10 +18305,15 @@ var CursorSDKPlugin = class extends BasePlugin {
17279
18305
  provider: "cursor",
17280
18306
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
17281
18307
  };
17282
- const span = startSpan({
17283
- name: "Cursor Agent",
17284
- spanAttributes: { type: "task" /* TASK */ }
17285
- });
18308
+ const span = startSpan(
18309
+ withSpanInstrumentationName(
18310
+ {
18311
+ name: "Cursor Agent",
18312
+ spanAttributes: { type: "task" /* TASK */ }
18313
+ },
18314
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18315
+ )
18316
+ );
17286
18317
  const startTime = getCurrentUnixTimestamp();
17287
18318
  safeLog2(span, {
17288
18319
  input: sanitizeUserMessage(message),
@@ -17729,29 +18760,39 @@ async function startToolSpan(state, args) {
17729
18760
  if (args.truncated?.result !== void 0) {
17730
18761
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
17731
18762
  }
17732
- const span = startSpan({
17733
- event: {
17734
- input: args.args,
17735
- metadata
17736
- },
17737
- name: `tool: ${name}`,
17738
- parent: await state.span.export(),
17739
- spanAttributes: { type: "tool" /* TOOL */ }
17740
- });
18763
+ const span = startSpan(
18764
+ withSpanInstrumentationName(
18765
+ {
18766
+ event: {
18767
+ input: args.args,
18768
+ metadata
18769
+ },
18770
+ name: `tool: ${name}`,
18771
+ parent: await state.span.export(),
18772
+ spanAttributes: { type: "tool" /* TOOL */ }
18773
+ },
18774
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18775
+ )
18776
+ );
17741
18777
  let subAgentSpan;
17742
18778
  if (isSubAgentToolName(name)) {
17743
- subAgentSpan = startSpan({
17744
- event: {
17745
- input: args.args,
17746
- metadata: {
17747
- "cursor_sdk.subagent.tool_call_id": args.callId,
17748
- "gen_ai.tool.name": name
17749
- }
17750
- },
17751
- name: formatSubAgentSpanName2(args.toolCall, args.args),
17752
- parent: await span.export(),
17753
- spanAttributes: { type: "task" /* TASK */ }
17754
- });
18779
+ subAgentSpan = startSpan(
18780
+ withSpanInstrumentationName(
18781
+ {
18782
+ event: {
18783
+ input: args.args,
18784
+ metadata: {
18785
+ "cursor_sdk.subagent.tool_call_id": args.callId,
18786
+ "gen_ai.tool.name": name
18787
+ }
18788
+ },
18789
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
18790
+ parent: await span.export(),
18791
+ spanAttributes: { type: "task" /* TASK */ }
18792
+ },
18793
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18794
+ )
18795
+ );
17755
18796
  }
17756
18797
  return { span, subAgentSpan };
17757
18798
  }
@@ -18010,24 +19051,28 @@ function cleanMetrics2(metrics) {
18010
19051
  }
18011
19052
 
18012
19053
  // src/instrumentation/plugins/openai-agents-channels.ts
18013
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
18014
- onTraceStart: channel({
18015
- channelName: "tracing.processor.onTraceStart",
18016
- kind: "async"
18017
- }),
18018
- onTraceEnd: channel({
18019
- channelName: "tracing.processor.onTraceEnd",
18020
- kind: "async"
18021
- }),
18022
- onSpanStart: channel({
18023
- channelName: "tracing.processor.onSpanStart",
18024
- kind: "async"
18025
- }),
18026
- onSpanEnd: channel({
18027
- channelName: "tracing.processor.onSpanEnd",
18028
- kind: "async"
18029
- })
18030
- });
19054
+ var openAIAgentsCoreChannels = defineChannels(
19055
+ "@openai/agents-core",
19056
+ {
19057
+ onTraceStart: channel({
19058
+ channelName: "tracing.processor.onTraceStart",
19059
+ kind: "async"
19060
+ }),
19061
+ onTraceEnd: channel({
19062
+ channelName: "tracing.processor.onTraceEnd",
19063
+ kind: "async"
19064
+ }),
19065
+ onSpanStart: channel({
19066
+ channelName: "tracing.processor.onSpanStart",
19067
+ kind: "async"
19068
+ }),
19069
+ onSpanEnd: channel({
19070
+ channelName: "tracing.processor.onSpanEnd",
19071
+ kind: "async"
19072
+ })
19073
+ },
19074
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
19075
+ );
18031
19076
 
18032
19077
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
18033
19078
  function isSpanData(spanData, type) {
@@ -18143,16 +19188,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
18143
19188
  this.evictOldestTrace();
18144
19189
  }
18145
19190
  const current = currentSpan();
18146
- const span = current && current !== NOOP_SPAN ? current.startSpan({
18147
- name: trace.name,
18148
- type: "task" /* TASK */
18149
- }) : this.logger ? this.logger.startSpan({
18150
- name: trace.name,
18151
- type: "task" /* TASK */
18152
- }) : startSpan({
18153
- name: trace.name,
18154
- type: "task" /* TASK */
18155
- });
19191
+ const spanArgs = withSpanInstrumentationName(
19192
+ {
19193
+ name: trace.name,
19194
+ type: "task" /* TASK */
19195
+ },
19196
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
19197
+ );
19198
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
19199
+ withSpanInstrumentationName(
19200
+ spanArgs,
19201
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
19202
+ )
19203
+ );
18156
19204
  span.log({
18157
19205
  input: "Agent workflow started",
18158
19206
  metadata: {
@@ -18203,10 +19251,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
18203
19251
  if (!parentSpan) {
18204
19252
  return Promise.resolve();
18205
19253
  }
18206
- const childSpan = parentSpan.startSpan({
18207
- name: spanNameFromAgents(span),
18208
- type: spanTypeFromAgents(span)
18209
- });
19254
+ const childSpan = parentSpan.startSpan(
19255
+ withSpanInstrumentationName(
19256
+ {
19257
+ name: spanNameFromAgents(span),
19258
+ type: spanTypeFromAgents(span)
19259
+ },
19260
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
19261
+ )
19262
+ );
18210
19263
  traceData.childSpans.set(span.spanId, childSpan);
18211
19264
  return Promise.resolve();
18212
19265
  }
@@ -18471,24 +19524,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
18471
19524
  };
18472
19525
 
18473
19526
  // src/instrumentation/plugins/google-genai-channels.ts
18474
- var googleGenAIChannels = defineChannels("@google/genai", {
18475
- generateContent: channel({
18476
- channelName: "models.generateContent",
18477
- kind: "async"
18478
- }),
18479
- generateContentStream: channel({
18480
- channelName: "models.generateContentStream",
18481
- kind: "async"
18482
- }),
18483
- embedContent: channel({
18484
- channelName: "models.embedContent",
18485
- kind: "async"
18486
- }),
18487
- interactionsCreate: channel({
18488
- channelName: "interactions.create",
18489
- kind: "async"
18490
- })
18491
- });
19527
+ var googleGenAIChannels = defineChannels(
19528
+ "@google/genai",
19529
+ {
19530
+ generateContent: channel({
19531
+ channelName: "models.generateContent",
19532
+ kind: "async"
19533
+ }),
19534
+ generateContentStream: channel({
19535
+ channelName: "models.generateContentStream",
19536
+ kind: "async"
19537
+ }),
19538
+ embedContent: channel({
19539
+ channelName: "models.embedContent",
19540
+ kind: "async"
19541
+ }),
19542
+ interactionsCreate: channel({
19543
+ channelName: "interactions.create",
19544
+ kind: "async"
19545
+ })
19546
+ },
19547
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
19548
+ );
18492
19549
 
18493
19550
  // src/instrumentation/plugins/google-genai-plugin.ts
18494
19551
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -18526,13 +19583,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18526
19583
  const params = event.arguments[0];
18527
19584
  const input = serializeGenerateContentInput(params);
18528
19585
  const metadata = extractGenerateContentMetadata(params);
18529
- const span = startSpan({
18530
- name: "generate_content",
18531
- spanAttributes: {
18532
- type: "llm" /* LLM */
18533
- },
18534
- event: createWrapperParityEvent({ input, metadata })
18535
- });
19586
+ const span = startSpan(
19587
+ withSpanInstrumentationName(
19588
+ {
19589
+ name: "generate_content",
19590
+ spanAttributes: {
19591
+ type: "llm" /* LLM */
19592
+ },
19593
+ event: createWrapperParityEvent({ input, metadata })
19594
+ },
19595
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19596
+ )
19597
+ );
18536
19598
  return {
18537
19599
  span,
18538
19600
  startTime: getCurrentUnixTimestamp()
@@ -18545,13 +19607,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18545
19607
  const params = event.arguments[0];
18546
19608
  const input = serializeGenerateContentInput(params);
18547
19609
  const metadata = extractGenerateContentMetadata(params);
18548
- const span = startSpan({
18549
- name: "generate_content",
18550
- spanAttributes: {
18551
- type: "llm" /* LLM */
18552
- },
18553
- event: createWrapperParityEvent({ input, metadata })
18554
- });
19610
+ const span = startSpan(
19611
+ withSpanInstrumentationName(
19612
+ {
19613
+ name: "generate_content",
19614
+ spanAttributes: {
19615
+ type: "llm" /* LLM */
19616
+ },
19617
+ event: createWrapperParityEvent({ input, metadata })
19618
+ },
19619
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19620
+ )
19621
+ );
18555
19622
  return {
18556
19623
  span,
18557
19624
  startTime: getCurrentUnixTimestamp()
@@ -18627,13 +19694,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18627
19694
  const params = event.arguments[0];
18628
19695
  const input = serializeEmbedContentInput(params);
18629
19696
  const metadata = extractEmbedContentMetadata(params);
18630
- const span = startSpan({
18631
- name: "embed_content",
18632
- spanAttributes: {
18633
- type: "llm" /* LLM */
18634
- },
18635
- event: createWrapperParityEvent({ input, metadata })
18636
- });
19697
+ const span = startSpan(
19698
+ withSpanInstrumentationName(
19699
+ {
19700
+ name: "embed_content",
19701
+ spanAttributes: {
19702
+ type: "llm" /* LLM */
19703
+ },
19704
+ event: createWrapperParityEvent({ input, metadata })
19705
+ },
19706
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19707
+ )
19708
+ );
18637
19709
  return {
18638
19710
  span,
18639
19711
  startTime: getCurrentUnixTimestamp()
@@ -18646,13 +19718,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18646
19718
  const params = event.arguments[0];
18647
19719
  const input = serializeEmbedContentInput(params);
18648
19720
  const metadata = extractEmbedContentMetadata(params);
18649
- const span = startSpan({
18650
- name: "embed_content",
18651
- spanAttributes: {
18652
- type: "llm" /* LLM */
18653
- },
18654
- event: createWrapperParityEvent({ input, metadata })
18655
- });
19721
+ const span = startSpan(
19722
+ withSpanInstrumentationName(
19723
+ {
19724
+ name: "embed_content",
19725
+ spanAttributes: {
19726
+ type: "llm" /* LLM */
19727
+ },
19728
+ event: createWrapperParityEvent({ input, metadata })
19729
+ },
19730
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19731
+ )
19732
+ );
18656
19733
  return {
18657
19734
  span,
18658
19735
  startTime: getCurrentUnixTimestamp()
@@ -18763,16 +19840,21 @@ function patchGoogleGenAIStreamingResult(args) {
18763
19840
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
18764
19841
  const ensureSpan = () => {
18765
19842
  if (!span) {
18766
- span = startSpan({
18767
- name: "generate_content_stream",
18768
- spanAttributes: {
18769
- type: "llm" /* LLM */
18770
- },
18771
- event: {
18772
- input,
18773
- metadata
18774
- }
18775
- });
19843
+ span = startSpan(
19844
+ withSpanInstrumentationName(
19845
+ {
19846
+ name: "generate_content_stream",
19847
+ spanAttributes: {
19848
+ type: "llm" /* LLM */
19849
+ },
19850
+ event: {
19851
+ input,
19852
+ metadata
19853
+ }
19854
+ },
19855
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19856
+ )
19857
+ );
18776
19858
  }
18777
19859
  return span;
18778
19860
  };
@@ -19540,28 +20622,32 @@ function tryToDict(obj) {
19540
20622
  }
19541
20623
 
19542
20624
  // src/instrumentation/plugins/huggingface-channels.ts
19543
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
19544
- chatCompletion: channel({
19545
- channelName: "chatCompletion",
19546
- kind: "async"
19547
- }),
19548
- chatCompletionStream: channel({
19549
- channelName: "chatCompletionStream",
19550
- kind: "sync-stream"
19551
- }),
19552
- textGeneration: channel({
19553
- channelName: "textGeneration",
19554
- kind: "async"
19555
- }),
19556
- textGenerationStream: channel({
19557
- channelName: "textGenerationStream",
19558
- kind: "sync-stream"
19559
- }),
19560
- featureExtraction: channel({
19561
- channelName: "featureExtraction",
19562
- kind: "async"
19563
- })
19564
- });
20625
+ var huggingFaceChannels = defineChannels(
20626
+ "@huggingface/inference",
20627
+ {
20628
+ chatCompletion: channel({
20629
+ channelName: "chatCompletion",
20630
+ kind: "async"
20631
+ }),
20632
+ chatCompletionStream: channel({
20633
+ channelName: "chatCompletionStream",
20634
+ kind: "sync-stream"
20635
+ }),
20636
+ textGeneration: channel({
20637
+ channelName: "textGeneration",
20638
+ kind: "async"
20639
+ }),
20640
+ textGenerationStream: channel({
20641
+ channelName: "textGenerationStream",
20642
+ kind: "sync-stream"
20643
+ }),
20644
+ featureExtraction: channel({
20645
+ channelName: "featureExtraction",
20646
+ kind: "async"
20647
+ })
20648
+ },
20649
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
20650
+ );
19565
20651
 
19566
20652
  // src/instrumentation/plugins/huggingface-plugin.ts
19567
20653
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -19988,20 +21074,24 @@ function extractTextGenerationStreamMetadata(chunks) {
19988
21074
  }
19989
21075
 
19990
21076
  // src/instrumentation/plugins/openrouter-agent-channels.ts
19991
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
19992
- callModel: channel({
19993
- channelName: "callModel",
19994
- kind: "sync-stream"
19995
- }),
19996
- callModelTurn: channel({
19997
- channelName: "callModel.turn",
19998
- kind: "async"
19999
- }),
20000
- toolExecute: channel({
20001
- channelName: "tool.execute",
20002
- kind: "async"
20003
- })
20004
- });
21077
+ var openRouterAgentChannels = defineChannels(
21078
+ "@openrouter/agent",
21079
+ {
21080
+ callModel: channel({
21081
+ channelName: "callModel",
21082
+ kind: "sync-stream"
21083
+ }),
21084
+ callModelTurn: channel({
21085
+ channelName: "callModel.turn",
21086
+ kind: "async"
21087
+ }),
21088
+ toolExecute: channel({
21089
+ channelName: "tool.execute",
21090
+ kind: "async"
21091
+ })
21092
+ },
21093
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
21094
+ );
20005
21095
 
20006
21096
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
20007
21097
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -20771,38 +21861,40 @@ function normalizeError(error) {
20771
21861
  }
20772
21862
 
20773
21863
  // src/instrumentation/plugins/openrouter-channels.ts
20774
- var openRouterChannels = defineChannels("@openrouter/sdk", {
20775
- chatSend: channel({
20776
- channelName: "chat.send",
20777
- kind: "async"
20778
- }),
20779
- embeddingsGenerate: channel({
20780
- channelName: "embeddings.generate",
20781
- kind: "async"
20782
- }),
20783
- rerankRerank: channel(
20784
- {
21864
+ var openRouterChannels = defineChannels(
21865
+ "@openrouter/sdk",
21866
+ {
21867
+ chatSend: channel({
21868
+ channelName: "chat.send",
21869
+ kind: "async"
21870
+ }),
21871
+ embeddingsGenerate: channel({
21872
+ channelName: "embeddings.generate",
21873
+ kind: "async"
21874
+ }),
21875
+ rerankRerank: channel({
20785
21876
  channelName: "rerank.rerank",
20786
21877
  kind: "async"
20787
- }
20788
- ),
20789
- betaResponsesSend: channel({
20790
- channelName: "beta.responses.send",
20791
- kind: "async"
20792
- }),
20793
- callModel: channel({
20794
- channelName: "callModel",
20795
- kind: "sync-stream"
20796
- }),
20797
- callModelTurn: channel({
20798
- channelName: "callModel.turn",
20799
- kind: "async"
20800
- }),
20801
- toolExecute: channel({
20802
- channelName: "tool.execute",
20803
- kind: "async"
20804
- })
20805
- });
21878
+ }),
21879
+ betaResponsesSend: channel({
21880
+ channelName: "beta.responses.send",
21881
+ kind: "async"
21882
+ }),
21883
+ callModel: channel({
21884
+ channelName: "callModel",
21885
+ kind: "sync-stream"
21886
+ }),
21887
+ callModelTurn: channel({
21888
+ channelName: "callModel.turn",
21889
+ kind: "async"
21890
+ }),
21891
+ toolExecute: channel({
21892
+ channelName: "tool.execute",
21893
+ kind: "async"
21894
+ })
21895
+ },
21896
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
21897
+ );
20806
21898
 
20807
21899
  // src/instrumentation/plugins/openrouter-plugin.ts
20808
21900
  var OpenRouterPlugin = class extends BasePlugin {
@@ -21842,52 +22934,56 @@ function normalizeError2(error) {
21842
22934
  }
21843
22935
 
21844
22936
  // src/instrumentation/plugins/mistral-channels.ts
21845
- var mistralChannels = defineChannels("@mistralai/mistralai", {
21846
- chatComplete: channel({
21847
- channelName: "chat.complete",
21848
- kind: "async"
21849
- }),
21850
- chatStream: channel({
21851
- channelName: "chat.stream",
21852
- kind: "async"
21853
- }),
21854
- embeddingsCreate: channel({
21855
- channelName: "embeddings.create",
21856
- kind: "async"
21857
- }),
21858
- classifiersModerate: channel({
21859
- channelName: "classifiers.moderate",
21860
- kind: "async"
21861
- }),
21862
- classifiersModerateChat: channel({
21863
- channelName: "classifiers.moderateChat",
21864
- kind: "async"
21865
- }),
21866
- classifiersClassify: channel({
21867
- channelName: "classifiers.classify",
21868
- kind: "async"
21869
- }),
21870
- classifiersClassifyChat: channel({
21871
- channelName: "classifiers.classifyChat",
21872
- kind: "async"
21873
- }),
21874
- fimComplete: channel({
21875
- channelName: "fim.complete",
21876
- kind: "async"
21877
- }),
21878
- fimStream: channel({
21879
- channelName: "fim.stream",
21880
- kind: "async"
21881
- }),
21882
- agentsComplete: channel({
21883
- channelName: "agents.complete",
21884
- kind: "async"
21885
- }),
21886
- agentsStream: channel({
21887
- channelName: "agents.stream",
21888
- kind: "async"
21889
- })
21890
- });
22937
+ var mistralChannels = defineChannels(
22938
+ "@mistralai/mistralai",
22939
+ {
22940
+ chatComplete: channel({
22941
+ channelName: "chat.complete",
22942
+ kind: "async"
22943
+ }),
22944
+ chatStream: channel({
22945
+ channelName: "chat.stream",
22946
+ kind: "async"
22947
+ }),
22948
+ embeddingsCreate: channel({
22949
+ channelName: "embeddings.create",
22950
+ kind: "async"
22951
+ }),
22952
+ classifiersModerate: channel({
22953
+ channelName: "classifiers.moderate",
22954
+ kind: "async"
22955
+ }),
22956
+ classifiersModerateChat: channel({
22957
+ channelName: "classifiers.moderateChat",
22958
+ kind: "async"
22959
+ }),
22960
+ classifiersClassify: channel({
22961
+ channelName: "classifiers.classify",
22962
+ kind: "async"
22963
+ }),
22964
+ classifiersClassifyChat: channel({
22965
+ channelName: "classifiers.classifyChat",
22966
+ kind: "async"
22967
+ }),
22968
+ fimComplete: channel({
22969
+ channelName: "fim.complete",
22970
+ kind: "async"
22971
+ }),
22972
+ fimStream: channel({
22973
+ channelName: "fim.stream",
22974
+ kind: "async"
22975
+ }),
22976
+ agentsComplete: channel({
22977
+ channelName: "agents.complete",
22978
+ kind: "async"
22979
+ }),
22980
+ agentsStream: channel({
22981
+ channelName: "agents.stream",
22982
+ kind: "async"
22983
+ })
22984
+ },
22985
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
22986
+ );
21891
22987
 
21892
22988
  // src/instrumentation/plugins/mistral-plugin.ts
21893
22989
  var MistralPlugin = class extends BasePlugin {
@@ -22507,20 +23603,24 @@ function aggregateMistralStreamChunks(chunks) {
22507
23603
  }
22508
23604
 
22509
23605
  // src/instrumentation/plugins/google-adk-channels.ts
22510
- var googleADKChannels = defineChannels("@google/adk", {
22511
- runnerRunAsync: channel({
22512
- channelName: "runner.runAsync",
22513
- kind: "sync-stream"
22514
- }),
22515
- agentRunAsync: channel({
22516
- channelName: "agent.runAsync",
22517
- kind: "sync-stream"
22518
- }),
22519
- toolRunAsync: channel({
22520
- channelName: "tool.runAsync",
22521
- kind: "async"
22522
- })
22523
- });
23606
+ var googleADKChannels = defineChannels(
23607
+ "@google/adk",
23608
+ {
23609
+ runnerRunAsync: channel({
23610
+ channelName: "runner.runAsync",
23611
+ kind: "sync-stream"
23612
+ }),
23613
+ agentRunAsync: channel({
23614
+ channelName: "agent.runAsync",
23615
+ kind: "sync-stream"
23616
+ }),
23617
+ toolRunAsync: channel({
23618
+ channelName: "tool.runAsync",
23619
+ kind: "async"
23620
+ })
23621
+ },
23622
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
23623
+ );
22524
23624
 
22525
23625
  // src/instrumentation/plugins/google-adk-plugin.ts
22526
23626
  var GoogleADKPlugin = class extends BasePlugin {
@@ -22545,12 +23645,17 @@ var GoogleADKPlugin = class extends BasePlugin {
22545
23645
  const createState = (event) => {
22546
23646
  const params = event.arguments[0] ?? {};
22547
23647
  const contextKey = extractRunnerContextKey(params);
22548
- const span = startSpan({
22549
- name: "Google ADK Runner",
22550
- spanAttributes: {
22551
- type: "task" /* TASK */
22552
- }
22553
- });
23648
+ const span = startSpan(
23649
+ withSpanInstrumentationName(
23650
+ {
23651
+ name: "Google ADK Runner",
23652
+ spanAttributes: {
23653
+ type: "task" /* TASK */
23654
+ }
23655
+ },
23656
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
23657
+ )
23658
+ );
22554
23659
  const startTime = getCurrentUnixTimestamp();
22555
23660
  try {
22556
23661
  const metadata = extractRunnerMetadata(params);
@@ -22636,18 +23741,23 @@ var GoogleADKPlugin = class extends BasePlugin {
22636
23741
  this.activeRunnerSpans
22637
23742
  );
22638
23743
  const contextKey = extractInvocationContextKey(parentContext);
22639
- const span = startSpan({
22640
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
22641
- spanAttributes: {
22642
- type: "task" /* TASK */
22643
- },
22644
- ...runnerParentSpan ? {
22645
- parentSpanIds: {
22646
- spanId: runnerParentSpan.spanId,
22647
- rootSpanId: runnerParentSpan.rootSpanId
22648
- }
22649
- } : {}
22650
- });
23744
+ const span = startSpan(
23745
+ withSpanInstrumentationName(
23746
+ {
23747
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
23748
+ spanAttributes: {
23749
+ type: "task" /* TASK */
23750
+ },
23751
+ ...runnerParentSpan ? {
23752
+ parentSpanIds: {
23753
+ spanId: runnerParentSpan.spanId,
23754
+ rootSpanId: runnerParentSpan.rootSpanId
23755
+ }
23756
+ } : {}
23757
+ },
23758
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
23759
+ )
23760
+ );
22651
23761
  const startTime = getCurrentUnixTimestamp();
22652
23762
  try {
22653
23763
  const metadata = {
@@ -22740,22 +23850,27 @@ var GoogleADKPlugin = class extends BasePlugin {
22740
23850
  this.activeAgentSpans,
22741
23851
  this.activeRunnerSpans
22742
23852
  );
22743
- const createSpan = () => startSpan({
22744
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
22745
- spanAttributes: {
22746
- type: "tool" /* TOOL */
22747
- },
22748
- event: {
22749
- input: req.args,
22750
- metadata: {
22751
- provider: "google-adk",
22752
- ...toolName && { "google_adk.tool_name": toolName },
22753
- ...extractToolCallId(req) && {
22754
- "google_adk.tool_call_id": extractToolCallId(req)
23853
+ const createSpan = () => startSpan(
23854
+ withSpanInstrumentationName(
23855
+ {
23856
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
23857
+ spanAttributes: {
23858
+ type: "tool" /* TOOL */
23859
+ },
23860
+ event: {
23861
+ input: req.args,
23862
+ metadata: {
23863
+ provider: "google-adk",
23864
+ ...toolName && { "google_adk.tool_name": toolName },
23865
+ ...extractToolCallId(req) && {
23866
+ "google_adk.tool_call_id": extractToolCallId(req)
23867
+ }
23868
+ }
22755
23869
  }
22756
- }
22757
- }
22758
- });
23870
+ },
23871
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
23872
+ )
23873
+ );
22759
23874
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
22760
23875
  const startTime = getCurrentUnixTimestamp();
22761
23876
  states.set(event, { span, startTime });
@@ -23154,24 +24269,28 @@ function cleanMetrics4(metrics) {
23154
24269
  }
23155
24270
 
23156
24271
  // src/instrumentation/plugins/cohere-channels.ts
23157
- var cohereChannels = defineChannels("cohere-ai", {
23158
- chat: channel({
23159
- channelName: "chat",
23160
- kind: "async"
23161
- }),
23162
- chatStream: channel({
23163
- channelName: "chatStream",
23164
- kind: "async"
23165
- }),
23166
- embed: channel({
23167
- channelName: "embed",
23168
- kind: "async"
23169
- }),
23170
- rerank: channel({
23171
- channelName: "rerank",
23172
- kind: "async"
23173
- })
23174
- });
24272
+ var cohereChannels = defineChannels(
24273
+ "cohere-ai",
24274
+ {
24275
+ chat: channel({
24276
+ channelName: "chat",
24277
+ kind: "async"
24278
+ }),
24279
+ chatStream: channel({
24280
+ channelName: "chatStream",
24281
+ kind: "async"
24282
+ }),
24283
+ embed: channel({
24284
+ channelName: "embed",
24285
+ kind: "async"
24286
+ }),
24287
+ rerank: channel({
24288
+ channelName: "rerank",
24289
+ kind: "async"
24290
+ })
24291
+ },
24292
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
24293
+ );
23175
24294
 
23176
24295
  // src/instrumentation/plugins/cohere-plugin.ts
23177
24296
  var CoherePlugin = class extends BasePlugin {
@@ -23840,18 +24959,20 @@ function aggregateCohereChatStreamChunks(chunks) {
23840
24959
  }
23841
24960
 
23842
24961
  // src/instrumentation/plugins/groq-channels.ts
23843
- var groqChannels = defineChannels("groq-sdk", {
23844
- chatCompletionsCreate: channel({
23845
- channelName: "chat.completions.create",
23846
- kind: "async"
23847
- }),
23848
- embeddingsCreate: channel(
23849
- {
24962
+ var groqChannels = defineChannels(
24963
+ "groq-sdk",
24964
+ {
24965
+ chatCompletionsCreate: channel({
24966
+ channelName: "chat.completions.create",
24967
+ kind: "async"
24968
+ }),
24969
+ embeddingsCreate: channel({
23850
24970
  channelName: "embeddings.create",
23851
24971
  kind: "async"
23852
- }
23853
- )
23854
- });
24972
+ })
24973
+ },
24974
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
24975
+ );
23855
24976
 
23856
24977
  // src/instrumentation/plugins/groq-plugin.ts
23857
24978
  var GroqPlugin = class extends BasePlugin {
@@ -23954,15 +25075,27 @@ var clientSendChannel = channel({
23954
25075
  channelName: "client.send",
23955
25076
  kind: "async"
23956
25077
  });
23957
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
23958
- clientSend: clientSendChannel
23959
- });
23960
- var smithyCoreChannels = defineChannels("@smithy/core", {
23961
- clientSend: clientSendChannel
23962
- });
23963
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
23964
- clientSend: clientSendChannel
23965
- });
25078
+ var bedrockRuntimeChannels = defineChannels(
25079
+ "aws-bedrock-runtime",
25080
+ {
25081
+ clientSend: clientSendChannel
25082
+ },
25083
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
25084
+ );
25085
+ var smithyCoreChannels = defineChannels(
25086
+ "@smithy/core",
25087
+ {
25088
+ clientSend: clientSendChannel
25089
+ },
25090
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
25091
+ );
25092
+ var smithyClientChannels = defineChannels(
25093
+ "@smithy/smithy-client",
25094
+ {
25095
+ clientSend: clientSendChannel
25096
+ },
25097
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
25098
+ );
23966
25099
 
23967
25100
  // src/instrumentation/plugins/bedrock-runtime-common.ts
23968
25101
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -24596,38 +25729,46 @@ function extractTextFromJsonLike(value) {
24596
25729
  }
24597
25730
 
24598
25731
  // src/instrumentation/plugins/genkit-channels.ts
24599
- var genkitChannels = defineChannels("@genkit-ai/ai", {
24600
- generate: channel({
24601
- channelName: "generate",
24602
- kind: "async"
24603
- }),
24604
- generateStream: channel({
24605
- channelName: "generateStream",
24606
- kind: "sync-stream"
24607
- }),
24608
- embed: channel({
24609
- channelName: "embed",
24610
- kind: "async"
24611
- }),
24612
- embedMany: channel({
24613
- channelName: "embedMany",
24614
- kind: "async"
24615
- }),
24616
- actionRun: channel({
24617
- channelName: "action.run",
24618
- kind: "async"
24619
- }),
24620
- actionStream: channel({
24621
- channelName: "action.stream",
24622
- kind: "sync-stream"
24623
- })
24624
- });
24625
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
24626
- actionSpan: channel({
24627
- channelName: "action.span",
24628
- kind: "async"
24629
- })
24630
- });
25732
+ var genkitChannels = defineChannels(
25733
+ "@genkit-ai/ai",
25734
+ {
25735
+ generate: channel({
25736
+ channelName: "generate",
25737
+ kind: "async"
25738
+ }),
25739
+ generateStream: channel({
25740
+ channelName: "generateStream",
25741
+ kind: "sync-stream"
25742
+ }),
25743
+ embed: channel({
25744
+ channelName: "embed",
25745
+ kind: "async"
25746
+ }),
25747
+ embedMany: channel({
25748
+ channelName: "embedMany",
25749
+ kind: "async"
25750
+ }),
25751
+ actionRun: channel({
25752
+ channelName: "action.run",
25753
+ kind: "async"
25754
+ }),
25755
+ actionStream: channel({
25756
+ channelName: "action.stream",
25757
+ kind: "sync-stream"
25758
+ })
25759
+ },
25760
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
25761
+ );
25762
+ var genkitCoreChannels = defineChannels(
25763
+ "@genkit-ai/core",
25764
+ {
25765
+ actionSpan: channel({
25766
+ channelName: "action.span",
25767
+ kind: "async"
25768
+ })
25769
+ },
25770
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
25771
+ );
24631
25772
 
24632
25773
  // src/instrumentation/plugins/genkit-plugin.ts
24633
25774
  var GenkitPlugin = class extends BasePlugin {
@@ -24812,12 +25953,17 @@ function startActionSpanState(args) {
24812
25953
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
24813
25954
  return void 0;
24814
25955
  }
24815
- const span = startSpan({
24816
- name: actionSpanName(args.metadata, args.runStepName),
24817
- spanAttributes: {
24818
- type: actionSpanType(args.metadata)
24819
- }
24820
- });
25956
+ const span = startSpan(
25957
+ withSpanInstrumentationName(
25958
+ {
25959
+ name: actionSpanName(args.metadata, args.runStepName),
25960
+ spanAttributes: {
25961
+ type: actionSpanType(args.metadata)
25962
+ }
25963
+ },
25964
+ INSTRUMENTATION_NAMES.GENKIT
25965
+ )
25966
+ );
24821
25967
  const startTime = getCurrentUnixTimestamp();
24822
25968
  span.log({
24823
25969
  input: args.input,
@@ -25178,20 +26324,24 @@ function stringValue(value) {
25178
26324
  }
25179
26325
 
25180
26326
  // src/instrumentation/plugins/github-copilot-channels.ts
25181
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
25182
- createSession: channel({
25183
- channelName: "client.createSession",
25184
- kind: "async"
25185
- }),
25186
- resumeSession: channel({
25187
- channelName: "client.resumeSession",
25188
- kind: "async"
25189
- }),
25190
- sendAndWait: channel({
25191
- channelName: "session.sendAndWait",
25192
- kind: "async"
25193
- })
25194
- });
26327
+ var gitHubCopilotChannels = defineChannels(
26328
+ "@github/copilot-sdk",
26329
+ {
26330
+ createSession: channel({
26331
+ channelName: "client.createSession",
26332
+ kind: "async"
26333
+ }),
26334
+ resumeSession: channel({
26335
+ channelName: "client.resumeSession",
26336
+ kind: "async"
26337
+ }),
26338
+ sendAndWait: channel({
26339
+ channelName: "session.sendAndWait",
26340
+ kind: "async"
26341
+ })
26342
+ },
26343
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
26344
+ );
25195
26345
 
25196
26346
  // src/instrumentation/plugins/github-copilot-plugin.ts
25197
26347
  var ROOT_AGENT_KEY = "__root__";
@@ -25282,11 +26432,16 @@ async function handleTurnStart(state, agentId) {
25282
26432
  return;
25283
26433
  }
25284
26434
  const parentId = await getParentIdForAgent(state, agentId);
25285
- const span = startSpan({
25286
- name: "Copilot Turn",
25287
- parent: parentId,
25288
- spanAttributes: { type: "task" /* TASK */ }
25289
- });
26435
+ const span = startSpan(
26436
+ withSpanInstrumentationName(
26437
+ {
26438
+ name: "Copilot Turn",
26439
+ parent: parentId,
26440
+ spanAttributes: { type: "task" /* TASK */ }
26441
+ },
26442
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26443
+ )
26444
+ );
25290
26445
  const pendingUserMessage = state.pendingUserMessages.get(key);
25291
26446
  if (pendingUserMessage) {
25292
26447
  span.log({ input: pendingUserMessage });
@@ -25317,11 +26472,16 @@ async function handleUsage(state, agentId, usage) {
25317
26472
  const parentId = turn ? await turn.id : await state.session.id;
25318
26473
  const content = state.currentMessageContent.get(key);
25319
26474
  const { metrics, metadata } = extractMetricsFromUsage(usage);
25320
- const llmSpan = startSpan({
25321
- name: "github.copilot.llm",
25322
- parent: parentId,
25323
- spanAttributes: { type: "llm" /* LLM */ }
25324
- });
26475
+ const llmSpan = startSpan(
26476
+ withSpanInstrumentationName(
26477
+ {
26478
+ name: "github.copilot.llm",
26479
+ parent: parentId,
26480
+ spanAttributes: { type: "llm" /* LLM */ }
26481
+ },
26482
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26483
+ )
26484
+ );
25325
26485
  llmSpan.log({
25326
26486
  output: content ?? void 0,
25327
26487
  metadata,
@@ -25347,11 +26507,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
25347
26507
  }
25348
26508
  const parentId = await getToolParentId(state, agentId);
25349
26509
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
25350
- const span = startSpan({
25351
- name: displayName,
25352
- parent: parentId,
25353
- spanAttributes: { type: "tool" /* TOOL */ }
25354
- });
26510
+ const span = startSpan(
26511
+ withSpanInstrumentationName(
26512
+ {
26513
+ name: displayName,
26514
+ parent: parentId,
26515
+ spanAttributes: { type: "tool" /* TOOL */ }
26516
+ },
26517
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26518
+ )
26519
+ );
25355
26520
  const metadata = {
25356
26521
  "gen_ai.tool.name": toolName,
25357
26522
  "gen_ai.tool.call.id": toolCallId
@@ -25387,11 +26552,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
25387
26552
  }
25388
26553
  const tool = state.activeTools.get(toolCallId);
25389
26554
  const parentId = tool ? await tool.id : await state.session.id;
25390
- const span = startSpan({
25391
- name: `Agent: ${agentDisplayName}`,
25392
- parent: parentId,
25393
- spanAttributes: { type: "task" /* TASK */ }
25394
- });
26555
+ const span = startSpan(
26556
+ withSpanInstrumentationName(
26557
+ {
26558
+ name: `Agent: ${agentDisplayName}`,
26559
+ parent: parentId,
26560
+ spanAttributes: { type: "task" /* TASK */ }
26561
+ },
26562
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26563
+ )
26564
+ );
25395
26565
  span.log({
25396
26566
  metadata: {
25397
26567
  "github_copilot.agent_name": agentDisplayName,
@@ -25617,10 +26787,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
25617
26787
  if (!config || typeof config !== "object") {
25618
26788
  return;
25619
26789
  }
25620
- const sessionSpan = startSpan({
25621
- name: "Copilot Session",
25622
- spanAttributes: { type: "task" /* TASK */ }
25623
- });
26790
+ const sessionSpan = startSpan(
26791
+ withSpanInstrumentationName(
26792
+ {
26793
+ name: "Copilot Session",
26794
+ spanAttributes: { type: "task" /* TASK */ }
26795
+ },
26796
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26797
+ )
26798
+ );
25624
26799
  const metadata = {};
25625
26800
  if (config.model) {
25626
26801
  metadata["github_copilot.model"] = config.model;
@@ -25708,12 +26883,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
25708
26883
  };
25709
26884
 
25710
26885
  // src/instrumentation/plugins/flue-channels.ts
25711
- var flueChannels = defineChannels("@flue/runtime", {
25712
- createContext: channel({
25713
- channelName: "createFlueContext",
25714
- kind: "sync-stream"
25715
- })
25716
- });
26886
+ var flueChannels = defineChannels(
26887
+ "@flue/runtime",
26888
+ {
26889
+ createContext: channel({
26890
+ channelName: "createFlueContext",
26891
+ kind: "sync-stream"
26892
+ })
26893
+ },
26894
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
26895
+ );
25717
26896
 
25718
26897
  // src/instrumentation/plugins/flue-plugin.ts
25719
26898
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -26056,15 +27235,20 @@ var FlueObserveBridge = class {
26056
27235
  safeLog3(existing.span, { input, metadata });
26057
27236
  return;
26058
27237
  }
26059
- const span = startSpan({
26060
- name: `workflow:${workflowName}`,
26061
- spanAttributes: { type: "task" /* TASK */ },
26062
- startTime: eventTime(event.startedAt ?? event.timestamp),
26063
- event: {
26064
- input,
26065
- metadata
26066
- }
26067
- });
27238
+ const span = startSpan(
27239
+ withSpanInstrumentationName(
27240
+ {
27241
+ name: `workflow:${workflowName}`,
27242
+ spanAttributes: { type: "task" /* TASK */ },
27243
+ startTime: eventTime(event.startedAt ?? event.timestamp),
27244
+ event: {
27245
+ input,
27246
+ metadata
27247
+ }
27248
+ },
27249
+ INSTRUMENTATION_NAMES.FLUE
27250
+ )
27251
+ );
26068
27252
  this.runsById.set(event.runId, { metadata, span });
26069
27253
  }
26070
27254
  handleRunResume(event, ctx) {
@@ -26084,12 +27268,17 @@ var FlueObserveBridge = class {
26084
27268
  safeLog3(existing.span, { metadata });
26085
27269
  return;
26086
27270
  }
26087
- const span = startSpan({
26088
- name: `workflow:${workflowName}`,
26089
- spanAttributes: { type: "task" /* TASK */ },
26090
- startTime: eventTime(event.startedAt ?? event.timestamp),
26091
- event: { metadata }
26092
- });
27271
+ const span = startSpan(
27272
+ withSpanInstrumentationName(
27273
+ {
27274
+ name: `workflow:${workflowName}`,
27275
+ spanAttributes: { type: "task" /* TASK */ },
27276
+ startTime: eventTime(event.startedAt ?? event.timestamp),
27277
+ event: { metadata }
27278
+ },
27279
+ INSTRUMENTATION_NAMES.FLUE
27280
+ )
27281
+ );
26093
27282
  this.runsById.set(event.runId, { metadata, span });
26094
27283
  }
26095
27284
  handleRunEnd(event) {
@@ -26728,7 +27917,14 @@ function stateMatchesRun(state, runId) {
26728
27917
  return state.metadata["flue.run_id"] === runId;
26729
27918
  }
26730
27919
  function startFlueSpan(parent, args) {
26731
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
27920
+ return parent ? withCurrent(
27921
+ parent,
27922
+ () => startSpan(
27923
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
27924
+ )
27925
+ ) : startSpan(
27926
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
27927
+ );
26732
27928
  }
26733
27929
  function runWithCurrentSpanStore(span, next) {
26734
27930
  const state = _internalGetGlobalState();
@@ -26818,9 +28014,13 @@ var BraintrustLangChainCallbackHandler = class {
26818
28014
  ...this.options.debug ? { runId, parentRunId } : {}
26819
28015
  }
26820
28016
  };
26821
- let span = parentSpan.startSpan(args);
28017
+ const spanArgs = withSpanInstrumentationName(
28018
+ args,
28019
+ INSTRUMENTATION_NAMES.LANGCHAIN
28020
+ );
28021
+ let span = parentSpan.startSpan(spanArgs);
26822
28022
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
26823
- span = initLogger().startSpan(args);
28023
+ span = initLogger().startSpan(spanArgs);
26824
28024
  }
26825
28025
  this.spans.set(runId, span);
26826
28026
  }
@@ -27033,6 +28233,16 @@ function cleanObject(obj) {
27033
28233
  })
27034
28234
  );
27035
28235
  }
28236
+ function normalizeTokenMetrics(obj) {
28237
+ const metrics = cleanObject(obj);
28238
+ if (metrics.total_tokens !== void 0) {
28239
+ metrics.tokens = metrics.total_tokens;
28240
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
28241
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
28242
+ }
28243
+ delete metrics.total_tokens;
28244
+ return metrics;
28245
+ }
27036
28246
  function walkGenerations(response) {
27037
28247
  const result = [];
27038
28248
  const generations = response.generations || [];
@@ -27079,7 +28289,7 @@ function getMetricsFromResponse(response) {
27079
28289
  continue;
27080
28290
  }
27081
28291
  const inputTokenDetails = usageMetadata.input_token_details;
27082
- return cleanObject({
28292
+ return normalizeTokenMetrics({
27083
28293
  total_tokens: usageMetadata.total_tokens,
27084
28294
  prompt_tokens: usageMetadata.input_tokens,
27085
28295
  completion_tokens: usageMetadata.output_tokens,
@@ -27089,7 +28299,7 @@ function getMetricsFromResponse(response) {
27089
28299
  }
27090
28300
  const llmOutput = response.llmOutput || {};
27091
28301
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
27092
- return cleanObject({
28302
+ return normalizeTokenMetrics({
27093
28303
  total_tokens: tokenUsage.totalTokens,
27094
28304
  prompt_tokens: tokenUsage.promptTokens,
27095
28305
  completion_tokens: tokenUsage.completionTokens
@@ -27107,16 +28317,20 @@ function isRecord(value) {
27107
28317
  }
27108
28318
 
27109
28319
  // src/instrumentation/plugins/langchain-channels.ts
27110
- var langChainChannels = defineChannels("@langchain/core", {
27111
- configure: channel({
27112
- channelName: "CallbackManager.configure",
27113
- kind: "sync-stream"
27114
- }),
27115
- configureSync: channel({
27116
- channelName: "CallbackManager._configureSync",
27117
- kind: "sync-stream"
27118
- })
27119
- });
28320
+ var langChainChannels = defineChannels(
28321
+ "@langchain/core",
28322
+ {
28323
+ configure: channel({
28324
+ channelName: "CallbackManager.configure",
28325
+ kind: "sync-stream"
28326
+ }),
28327
+ configureSync: channel({
28328
+ channelName: "CallbackManager._configureSync",
28329
+ kind: "sync-stream"
28330
+ })
28331
+ },
28332
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
28333
+ );
27120
28334
 
27121
28335
  // src/instrumentation/plugins/langchain-plugin.ts
27122
28336
  var LangChainPlugin = class extends BasePlugin {
@@ -27204,20 +28418,24 @@ function isBraintrustHandler(handler) {
27204
28418
  }
27205
28419
 
27206
28420
  // src/instrumentation/plugins/langsmith-channels.ts
27207
- var langSmithChannels = defineChannels("langsmith", {
27208
- createRun: channel({
27209
- channelName: "Client.createRun",
27210
- kind: "async"
27211
- }),
27212
- updateRun: channel({
27213
- channelName: "Client.updateRun",
27214
- kind: "async"
27215
- }),
27216
- batchIngestRuns: channel({
27217
- channelName: "Client.batchIngestRuns",
27218
- kind: "async"
27219
- })
27220
- });
28421
+ var langSmithChannels = defineChannels(
28422
+ "langsmith",
28423
+ {
28424
+ createRun: channel({
28425
+ channelName: "Client.createRun",
28426
+ kind: "async"
28427
+ }),
28428
+ updateRun: channel({
28429
+ channelName: "Client.updateRun",
28430
+ kind: "async"
28431
+ }),
28432
+ batchIngestRuns: channel({
28433
+ channelName: "Client.batchIngestRuns",
28434
+ kind: "async"
28435
+ })
28436
+ },
28437
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
28438
+ );
27221
28439
 
27222
28440
  // src/instrumentation/plugins/langsmith-plugin.ts
27223
28441
  var MAX_COMPLETED_RUNS = 1e4;
@@ -27381,19 +28599,24 @@ var LangSmithPlugin = class extends BasePlugin {
27381
28599
  const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
27382
28600
  const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
27383
28601
  const startTime = timestampSeconds(ownValue(run, "start_time"));
27384
- return startSpan({
27385
- name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
27386
- spanId: id,
27387
- parentSpanIds: {
27388
- parentSpanIds: parentId ? [parentId] : [],
27389
- rootSpanId: traceId
27390
- },
27391
- spanAttributes: {
27392
- type: mapRunType(ownValue(run, "run_type"))
27393
- },
27394
- ...startTime === void 0 ? {} : { startTime },
27395
- event: { id }
27396
- });
28602
+ return startSpan(
28603
+ withSpanInstrumentationName(
28604
+ {
28605
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
28606
+ spanId: id,
28607
+ parentSpanIds: {
28608
+ parentSpanIds: parentId ? [parentId] : [],
28609
+ rootSpanId: traceId
28610
+ },
28611
+ spanAttributes: {
28612
+ type: mapRunType(ownValue(run, "run_type"))
28613
+ },
28614
+ ...startTime === void 0 ? {} : { startTime },
28615
+ event: { id }
28616
+ },
28617
+ INSTRUMENTATION_NAMES.LANGSMITH
28618
+ )
28619
+ );
27397
28620
  }
27398
28621
  logRun(span, run, previous, includeOutput) {
27399
28622
  const inputs = preferOwnValue(run, previous, "inputs");
@@ -27651,7 +28874,8 @@ var piCodingAgentChannels = defineChannels(
27651
28874
  channelName: "AgentSession.prompt",
27652
28875
  kind: "async"
27653
28876
  })
27654
- }
28877
+ },
28878
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
27655
28879
  );
27656
28880
 
27657
28881
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -27735,14 +28959,19 @@ function startPiPromptRun(event, onFinalize) {
27735
28959
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
27736
28960
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
27737
28961
  };
27738
- const span = startSpan({
27739
- event: {
27740
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
27741
- metadata
27742
- },
27743
- name: "AgentSession.prompt",
27744
- spanAttributes: { type: "task" /* TASK */ }
27745
- });
28962
+ const span = startSpan(
28963
+ withSpanInstrumentationName(
28964
+ {
28965
+ event: {
28966
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
28967
+ metadata
28968
+ },
28969
+ name: "AgentSession.prompt",
28970
+ spanAttributes: { type: "task" /* TASK */ }
28971
+ },
28972
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
28973
+ )
28974
+ );
27746
28975
  const streamPatchState = installPiStreamPatch(agent);
27747
28976
  const options = event.arguments[1];
27748
28977
  const promptText = event.arguments[0];
@@ -27912,15 +29141,20 @@ async function startPiLlmSpan(state, model, context, options) {
27912
29141
  ...extractToolMetadata(context.tools),
27913
29142
  "pi_coding_agent.operation": "agent.streamFn"
27914
29143
  };
27915
- const span = startSpan({
27916
- event: {
27917
- input: processInputAttachments(normalizePiContextInput(context)),
27918
- metadata
27919
- },
27920
- name: getLlmSpanName(model),
27921
- parent: await state.span.export(),
27922
- spanAttributes: { type: "llm" /* LLM */ }
27923
- });
29144
+ const span = startSpan(
29145
+ withSpanInstrumentationName(
29146
+ {
29147
+ event: {
29148
+ input: processInputAttachments(normalizePiContextInput(context)),
29149
+ metadata
29150
+ },
29151
+ name: getLlmSpanName(model),
29152
+ parent: await state.span.export(),
29153
+ spanAttributes: { type: "llm" /* LLM */ }
29154
+ },
29155
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
29156
+ )
29157
+ );
27924
29158
  const llmState = {
27925
29159
  finalized: false,
27926
29160
  metadata,
@@ -28084,19 +29318,24 @@ async function startPiToolSpan(state, event) {
28084
29318
  const restoreAutoInstrumentation = enterAutoInstrumentationAllowed();
28085
29319
  const metadata = {
28086
29320
  "gen_ai.tool.call.id": event.toolCallId,
28087
- "gen_ai.tool.name": event.toolName,
28088
- "pi_coding_agent.tool.name": event.toolName
28089
- };
28090
- try {
28091
- const span = startSpan({
28092
- event: {
28093
- input: event.args,
28094
- metadata
28095
- },
28096
- name: event.toolName || "tool",
28097
- parent: await state.span.export(),
28098
- spanAttributes: { type: "tool" /* TOOL */ }
28099
- });
29321
+ "gen_ai.tool.name": event.toolName,
29322
+ "pi_coding_agent.tool.name": event.toolName
29323
+ };
29324
+ try {
29325
+ const span = startSpan(
29326
+ withSpanInstrumentationName(
29327
+ {
29328
+ event: {
29329
+ input: event.args,
29330
+ metadata
29331
+ },
29332
+ name: event.toolName || "tool",
29333
+ parent: await state.span.export(),
29334
+ spanAttributes: { type: "tool" /* TOOL */ }
29335
+ },
29336
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
29337
+ )
29338
+ );
28100
29339
  state.activeToolSpans.set(event.toolCallId, {
28101
29340
  restoreAutoInstrumentation,
28102
29341
  span
@@ -28528,20 +29767,24 @@ function logInstrumentationError4(context, error) {
28528
29767
  }
28529
29768
 
28530
29769
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
28531
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
28532
- agentStream: channel({
28533
- channelName: "Agent.stream",
28534
- kind: "sync-stream"
28535
- }),
28536
- graphStream: channel({
28537
- channelName: "Graph.stream",
28538
- kind: "sync-stream"
28539
- }),
28540
- swarmStream: channel({
28541
- channelName: "Swarm.stream",
28542
- kind: "sync-stream"
28543
- })
28544
- });
29770
+ var strandsAgentSDKChannels = defineChannels(
29771
+ "@strands-agents/sdk",
29772
+ {
29773
+ agentStream: channel({
29774
+ channelName: "Agent.stream",
29775
+ kind: "sync-stream"
29776
+ }),
29777
+ graphStream: channel({
29778
+ channelName: "Graph.stream",
29779
+ kind: "sync-stream"
29780
+ }),
29781
+ swarmStream: channel({
29782
+ channelName: "Swarm.stream",
29783
+ kind: "sync-stream"
29784
+ })
29785
+ },
29786
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
29787
+ );
28545
29788
 
28546
29789
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
28547
29790
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -28696,22 +29939,32 @@ function startAgentStream(event, activeChildParents) {
28696
29939
  );
28697
29940
  const span = parentSpan ? withCurrent(
28698
29941
  parentSpan,
28699
- () => startSpan({
28700
- event: {
28701
- input,
28702
- metadata
29942
+ () => startSpan(
29943
+ withSpanInstrumentationName(
29944
+ {
29945
+ event: {
29946
+ input,
29947
+ metadata
29948
+ },
29949
+ name: formatAgentSpanName(agent),
29950
+ spanAttributes: { type: "task" /* TASK */ }
29951
+ },
29952
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
29953
+ )
29954
+ )
29955
+ ) : startSpan(
29956
+ withSpanInstrumentationName(
29957
+ {
29958
+ event: {
29959
+ input,
29960
+ metadata
29961
+ },
29962
+ name: formatAgentSpanName(agent),
29963
+ spanAttributes: { type: "task" /* TASK */ }
28703
29964
  },
28704
- name: formatAgentSpanName(agent),
28705
- spanAttributes: { type: "task" /* TASK */ }
28706
- })
28707
- ) : startSpan({
28708
- event: {
28709
- input,
28710
- metadata
28711
- },
28712
- name: formatAgentSpanName(agent),
28713
- spanAttributes: { type: "task" /* TASK */ }
28714
- });
29965
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
29966
+ )
29967
+ );
28715
29968
  return {
28716
29969
  activeTools: /* @__PURE__ */ new Map(),
28717
29970
  attachmentCache,
@@ -28733,22 +29986,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
28733
29986
  const input = processStrandsInputAttachments(event.arguments[0]);
28734
29987
  const span = parentSpan ? withCurrent(
28735
29988
  parentSpan,
28736
- () => startSpan({
28737
- event: {
28738
- input,
28739
- metadata
29989
+ () => startSpan(
29990
+ withSpanInstrumentationName(
29991
+ {
29992
+ event: {
29993
+ input,
29994
+ metadata
29995
+ },
29996
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
29997
+ spanAttributes: { type: "task" /* TASK */ }
29998
+ },
29999
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30000
+ )
30001
+ )
30002
+ ) : startSpan(
30003
+ withSpanInstrumentationName(
30004
+ {
30005
+ event: {
30006
+ input,
30007
+ metadata
30008
+ },
30009
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30010
+ spanAttributes: { type: "task" /* TASK */ }
28740
30011
  },
28741
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28742
- spanAttributes: { type: "task" /* TASK */ }
28743
- })
28744
- ) : startSpan({
28745
- event: {
28746
- input,
28747
- metadata
28748
- },
28749
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28750
- spanAttributes: { type: "task" /* TASK */ }
28751
- });
30012
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30013
+ )
30014
+ );
28752
30015
  return {
28753
30016
  activeNodes: /* @__PURE__ */ new Map(),
28754
30017
  finalized: false,
@@ -28850,17 +30113,22 @@ function startModelSpan(state, event) {
28850
30113
  };
28851
30114
  const span = withCurrent(
28852
30115
  state.span,
28853
- () => startSpan({
28854
- event: {
28855
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
28856
- event.agent.messages,
28857
- state.attachmentCache
28858
- ) : void 0,
28859
- metadata
28860
- },
28861
- name: formatModelSpanName(model),
28862
- spanAttributes: { type: "llm" /* LLM */ }
28863
- })
30116
+ () => startSpan(
30117
+ withSpanInstrumentationName(
30118
+ {
30119
+ event: {
30120
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
30121
+ event.agent.messages,
30122
+ state.attachmentCache
30123
+ ) : void 0,
30124
+ metadata
30125
+ },
30126
+ name: formatModelSpanName(model),
30127
+ spanAttributes: { type: "llm" /* LLM */ }
30128
+ },
30129
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30130
+ )
30131
+ )
28864
30132
  );
28865
30133
  state.activeModel = {
28866
30134
  metadata,
@@ -28912,20 +30180,25 @@ function startToolSpan2(state, event) {
28912
30180
  const name = extractToolName3(toolUse, event.tool);
28913
30181
  const span = withCurrent(
28914
30182
  state.span,
28915
- () => startSpan({
28916
- event: {
28917
- input: toolUse?.input,
28918
- metadata: {
28919
- "gen_ai.tool.call.id": toolUse?.toolUseId,
28920
- "gen_ai.tool.name": name,
28921
- "strands.operation": "tool.call",
28922
- "strands.tool.name": name,
28923
- provider: "strands"
28924
- }
28925
- },
28926
- name: `tool: ${name}`,
28927
- spanAttributes: { type: "tool" /* TOOL */ }
28928
- })
30183
+ () => startSpan(
30184
+ withSpanInstrumentationName(
30185
+ {
30186
+ event: {
30187
+ input: toolUse?.input,
30188
+ metadata: {
30189
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
30190
+ "gen_ai.tool.name": name,
30191
+ "strands.operation": "tool.call",
30192
+ "strands.tool.name": name,
30193
+ provider: "strands"
30194
+ }
30195
+ },
30196
+ name: `tool: ${name}`,
30197
+ spanAttributes: { type: "tool" /* TOOL */ }
30198
+ },
30199
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30200
+ )
30201
+ )
28929
30202
  );
28930
30203
  state.activeTools.set(key, {
28931
30204
  span,
@@ -28996,11 +30269,16 @@ function startNodeSpan(state, event, activeChildParents) {
28996
30269
  };
28997
30270
  const span = withCurrent(
28998
30271
  state.span,
28999
- () => startSpan({
29000
- event: { metadata },
29001
- name: `node: ${nodeId}`,
29002
- spanAttributes: { type: "task" /* TASK */ }
29003
- })
30272
+ () => startSpan(
30273
+ withSpanInstrumentationName(
30274
+ {
30275
+ event: { metadata },
30276
+ name: `node: ${nodeId}`,
30277
+ spanAttributes: { type: "task" /* TASK */ }
30278
+ },
30279
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30280
+ )
30281
+ )
29004
30282
  );
29005
30283
  const nodeState = {
29006
30284
  ...child ? { child } : {},
@@ -29709,6 +30987,7 @@ var BraintrustPlugin = class extends BasePlugin {
29709
30987
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
29710
30988
  var MAX_EVE_CACHE_ENTRIES = 1e4;
29711
30989
  var MAX_STORED_LLM_INPUTS = 100;
30990
+ var MAX_STORED_REASONING_BLOCKS = 100;
29712
30991
  var MAX_STORED_SPAN_REFERENCES = 1e4;
29713
30992
  var MAX_STORED_STEP_STARTS = 1e4;
29714
30993
  function braintrustEveHook(options) {
@@ -29747,6 +31026,7 @@ var ResumedEveSpan = class {
29747
31026
  this.reference = reference;
29748
31027
  this.endTime = reference.endTime;
29749
31028
  }
31029
+ reference;
29750
31030
  endTime;
29751
31031
  get rootSpanId() {
29752
31032
  return this.reference.rootSpanId;
@@ -29779,6 +31059,7 @@ var EveBridge = class {
29779
31059
  constructor(state) {
29780
31060
  this.state = state;
29781
31061
  }
31062
+ state;
29782
31063
  eventQueuesBySession = /* @__PURE__ */ new Map();
29783
31064
  completedToolKeys = new LRUCache({
29784
31065
  max: MAX_EVE_CACHE_ENTRIES
@@ -29811,7 +31092,12 @@ var EveBridge = class {
29811
31092
  };
29812
31093
  const span = withCurrent(
29813
31094
  NOOP_SPAN,
29814
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
31095
+ () => _internalStartSpanWithInitialMerge(
31096
+ withSpanInstrumentationName(
31097
+ { ...args, startTime },
31098
+ INSTRUMENTATION_NAMES.EVE
31099
+ )
31100
+ )
29815
31101
  );
29816
31102
  if (typeof rowId !== "string") {
29817
31103
  return span;
@@ -29957,6 +31243,9 @@ var EveBridge = class {
29957
31243
  case "step.started":
29958
31244
  await this.handleStepStarted(event, ctx, hookMetadata);
29959
31245
  return true;
31246
+ case "reasoning.completed":
31247
+ this.handleReasoningCompleted(event, ctx);
31248
+ return true;
29960
31249
  case "message.completed":
29961
31250
  this.handleMessageCompleted(event, ctx);
29962
31251
  return true;
@@ -30048,6 +31337,7 @@ var EveBridge = class {
30048
31337
  key,
30049
31338
  metadata,
30050
31339
  metrics: {},
31340
+ sessionId,
30051
31341
  span,
30052
31342
  stepsByIndex: /* @__PURE__ */ new Map(),
30053
31343
  turnId: event.data.turnId
@@ -30078,6 +31368,12 @@ var EveBridge = class {
30078
31368
  const endTime = eventTime2(event);
30079
31369
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
30080
31370
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
31371
+ clearStoredEveReasoning(
31372
+ this.state,
31373
+ sessionId,
31374
+ event.data.turnId,
31375
+ event.data.stepIndex
31376
+ );
30081
31377
  }
30082
31378
  const stepOrdinal = this.stepOrdinal(event);
30083
31379
  const metadata = { ...turn.metadata };
@@ -30087,6 +31383,13 @@ var EveBridge = class {
30087
31383
  event.data.turnId,
30088
31384
  event.data.stepIndex
30089
31385
  );
31386
+ const reasoning = readStoredEveReasoning(
31387
+ this.state,
31388
+ sessionId,
31389
+ event.data.turnId,
31390
+ event.data.stepIndex
31391
+ );
31392
+ const output = mergeEveReasoning(void 0, reasoning);
30090
31393
  const { rowId: eventId, spanId } = await generateEveIds(
30091
31394
  "step",
30092
31395
  sessionId,
@@ -30104,32 +31407,52 @@ var EveBridge = class {
30104
31407
  spanId,
30105
31408
  startTime: eventTime2(event)
30106
31409
  });
30107
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
31410
+ span.log({
31411
+ ...input !== void 0 ? { input } : {},
31412
+ metadata
31413
+ });
30108
31414
  turn.stepsByIndex.set(event.data.stepIndex, {
30109
31415
  ...input !== void 0 ? { input } : {},
30110
31416
  metadata,
30111
31417
  metrics: {},
31418
+ ...output !== void 0 ? { output } : {},
31419
+ reasoning,
30112
31420
  span
30113
31421
  });
30114
31422
  }
31423
+ handleReasoningCompleted(event, ctx) {
31424
+ const sessionId = sessionIdFromContext(ctx);
31425
+ if (!sessionId) {
31426
+ return;
31427
+ }
31428
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
31429
+ const step = this.stepForEvent(event, ctx);
31430
+ if (step) {
31431
+ step.reasoning = reasoning;
31432
+ step.output = mergeEveReasoning(step.output, reasoning);
31433
+ }
31434
+ }
30115
31435
  handleMessageCompleted(event, ctx) {
30116
31436
  const step = this.stepForEvent(event, ctx);
30117
31437
  if (!step) {
30118
31438
  return;
30119
31439
  }
30120
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
31440
+ const existingMessage = eveOutputMessage(step.output);
30121
31441
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
30122
- step.output = [
30123
- {
30124
- finish_reason: normalizedFinishReason(event.data.finishReason),
30125
- index: 0,
30126
- message: {
30127
- content: event.data.message,
30128
- role: "assistant",
30129
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
31442
+ step.output = mergeEveReasoning(
31443
+ [
31444
+ {
31445
+ finish_reason: normalizedFinishReason(event.data.finishReason),
31446
+ index: 0,
31447
+ message: {
31448
+ content: event.data.message,
31449
+ role: "assistant",
31450
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
31451
+ }
30130
31452
  }
30131
- }
30132
- ];
31453
+ ],
31454
+ step.reasoning
31455
+ );
30133
31456
  const turn = this.turnForEvent(event, ctx);
30134
31457
  if (turn && event.data.finishReason !== "tool-calls") {
30135
31458
  turn.output = event.data.message;
@@ -30138,16 +31461,19 @@ var EveBridge = class {
30138
31461
  handleResultCompleted(event, ctx) {
30139
31462
  const step = this.stepForEvent(event, ctx);
30140
31463
  if (step) {
30141
- step.output = [
30142
- {
30143
- finish_reason: "stop",
30144
- index: 0,
30145
- message: {
30146
- content: event.data.result,
30147
- role: "assistant"
31464
+ step.output = mergeEveReasoning(
31465
+ [
31466
+ {
31467
+ finish_reason: "stop",
31468
+ index: 0,
31469
+ message: {
31470
+ content: event.data.result,
31471
+ role: "assistant"
31472
+ }
30148
31473
  }
30149
- }
30150
- ];
31474
+ ],
31475
+ step.reasoning
31476
+ );
30151
31477
  }
30152
31478
  const turn = this.turnForEvent(event, ctx);
30153
31479
  if (turn) {
@@ -30197,18 +31523,20 @@ var EveBridge = class {
30197
31523
  type: "function"
30198
31524
  });
30199
31525
  }
30200
- step.output = [
30201
- {
30202
- finish_reason: "tool_calls",
30203
- index: 0,
30204
- message: {
30205
- content: null,
30206
- role: "assistant",
30207
- tool_calls: [...toolCallsById.values()]
31526
+ step.output = mergeEveReasoning(
31527
+ [
31528
+ {
31529
+ finish_reason: "tool_calls",
31530
+ index: 0,
31531
+ message: {
31532
+ content: null,
31533
+ role: "assistant",
31534
+ tool_calls: [...toolCallsById.values()]
31535
+ }
30208
31536
  }
30209
- }
30210
- ];
30211
- step.span.log({ metadata: step.metadata, output: step.output });
31537
+ ],
31538
+ step.reasoning
31539
+ );
30212
31540
  }
30213
31541
  async handleActionResult(event, ctx, hookMetadata) {
30214
31542
  if (isToolResult(event.data.result)) {
@@ -30386,6 +31714,7 @@ var EveBridge = class {
30386
31714
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
30387
31715
  };
30388
31716
  step.metrics = { ...step.metrics, ...metrics };
31717
+ const sessionId = sessionIdFromContext(ctx);
30389
31718
  if (Array.isArray(step.output) && isObject(step.output[0])) {
30390
31719
  const finishReason = step.output[0].finish_reason;
30391
31720
  if (typeof finishReason !== "string") {
@@ -30410,6 +31739,14 @@ var EveBridge = class {
30410
31739
  turn.stepsByIndex.delete(event.data.stepIndex);
30411
31740
  }
30412
31741
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
31742
+ if (sessionId) {
31743
+ clearStoredEveReasoning(
31744
+ this.state,
31745
+ sessionId,
31746
+ event.data.turnId,
31747
+ event.data.stepIndex
31748
+ );
31749
+ }
30413
31750
  }
30414
31751
  handleStepFailed(event, ctx) {
30415
31752
  const step = this.stepForEvent(event, ctx);
@@ -30427,6 +31764,15 @@ var EveBridge = class {
30427
31764
  const turn = this.turnForEvent(event, ctx);
30428
31765
  turn?.stepsByIndex.delete(event.data.stepIndex);
30429
31766
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
31767
+ const sessionId = sessionIdFromContext(ctx);
31768
+ if (sessionId) {
31769
+ clearStoredEveReasoning(
31770
+ this.state,
31771
+ sessionId,
31772
+ event.data.turnId,
31773
+ event.data.stepIndex
31774
+ );
31775
+ }
30430
31776
  }
30431
31777
  handleTurnCompleted(event, ctx) {
30432
31778
  const turn = this.turnForEvent(event, ctx);
@@ -30524,6 +31870,7 @@ var EveBridge = class {
30524
31870
  key,
30525
31871
  metadata,
30526
31872
  metrics: {},
31873
+ sessionId,
30527
31874
  span,
30528
31875
  stepsByIndex: /* @__PURE__ */ new Map(),
30529
31876
  turnId: event.data.turnId
@@ -30786,6 +32133,9 @@ var EveBridge = class {
30786
32133
  const normalized = normalizeEveTraceState(current);
30787
32134
  return {
30788
32135
  ...normalized,
32136
+ reasoningBlocks: normalized.reasoningBlocks.filter(
32137
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
32138
+ ),
30789
32139
  stepStarts: normalized.stepStarts.filter(
30790
32140
  (entry) => entry.turnId !== turn.turnId
30791
32141
  )
@@ -30825,6 +32175,7 @@ function emptyEveTraceState() {
30825
32175
  return {
30826
32176
  llmInputs: [],
30827
32177
  metadata: {},
32178
+ reasoningBlocks: [],
30828
32179
  spanReferences: [],
30829
32180
  stepStarts: []
30830
32181
  };
@@ -30875,6 +32226,21 @@ function normalizeEveTraceState(state) {
30875
32226
  const input = entry["input"];
30876
32227
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
30877
32228
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
32229
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
32230
+ if (!isObject(entry)) {
32231
+ return [];
32232
+ }
32233
+ const content = entry["content"];
32234
+ const eventAt = entry["eventAt"];
32235
+ const key = entry["key"];
32236
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
32237
+ {
32238
+ content,
32239
+ ...typeof eventAt === "string" ? { eventAt } : {},
32240
+ key
32241
+ }
32242
+ ] : [];
32243
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
30878
32244
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
30879
32245
  if (!isObject(entry)) {
30880
32246
  return [];
@@ -30885,7 +32251,13 @@ function normalizeEveTraceState(state) {
30885
32251
  const turnId = entry["turnId"];
30886
32252
  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 }] : [];
30887
32253
  }).slice(-MAX_STORED_STEP_STARTS) : [];
30888
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
32254
+ return {
32255
+ llmInputs,
32256
+ metadata: { ...metadata },
32257
+ reasoningBlocks,
32258
+ spanReferences,
32259
+ stepStarts
32260
+ };
30889
32261
  }
30890
32262
  function readEveTraceState(state) {
30891
32263
  try {
@@ -30894,23 +32266,85 @@ function readEveTraceState(state) {
30894
32266
  return emptyEveTraceState();
30895
32267
  }
30896
32268
  }
30897
- function captureEveModelInput(state, input) {
30898
- if (!isObject(input)) {
30899
- return;
30900
- }
30901
- const session = input["session"];
30902
- const turn = input["turn"];
30903
- const step = input["step"];
30904
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
30905
- return;
30906
- }
30907
- const sessionId = session["id"];
30908
- const turnId = turn["id"];
30909
- const stepIndex = step["index"];
30910
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
30911
- return;
32269
+ function storeEveReasoning(state, sessionId, event) {
32270
+ const eventAt = event.meta?.at;
32271
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
32272
+ let stored = [];
32273
+ state.update((current) => {
32274
+ const normalized = normalizeEveTraceState(current);
32275
+ const alreadyStored = normalized.reasoningBlocks.some(
32276
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
32277
+ );
32278
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
32279
+ ...normalized.reasoningBlocks,
32280
+ {
32281
+ content: event.data.reasoning,
32282
+ ...eventAt ? { eventAt } : {},
32283
+ key
32284
+ }
32285
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
32286
+ stored = reasoningBlocks.flatMap(
32287
+ (entry) => entry.key === key ? [
32288
+ {
32289
+ content: entry.content,
32290
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
32291
+ }
32292
+ ] : []
32293
+ );
32294
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
32295
+ });
32296
+ return stored;
32297
+ }
32298
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
32299
+ const key = llmInputKey(sessionId, turnId, stepIndex);
32300
+ return readEveTraceState(state).reasoningBlocks.flatMap(
32301
+ (entry) => entry.key === key ? [
32302
+ {
32303
+ content: entry.content,
32304
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
32305
+ }
32306
+ ] : []
32307
+ );
32308
+ }
32309
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
32310
+ const key = llmInputKey(sessionId, turnId, stepIndex);
32311
+ state.update((current) => {
32312
+ const normalized = normalizeEveTraceState(current);
32313
+ return {
32314
+ ...normalized,
32315
+ reasoningBlocks: normalized.reasoningBlocks.filter(
32316
+ (entry) => entry.key !== key
32317
+ )
32318
+ };
32319
+ });
32320
+ }
32321
+ function eveOutputMessage(output) {
32322
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
32323
+ }
32324
+ function mergeEveReasoning(output, reasoning) {
32325
+ if (reasoning.length === 0) {
32326
+ return output;
30912
32327
  }
30913
- const captured = capturedModelInput(input["modelInput"]);
32328
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
32329
+ const message = isObject(choice["message"]) ? choice["message"] : {};
32330
+ return [
32331
+ {
32332
+ ...choice,
32333
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
32334
+ message: {
32335
+ ...message,
32336
+ content: "content" in message ? message["content"] : null,
32337
+ reasoning: reasoning.map((block) => ({ content: block.content })),
32338
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
32339
+ }
32340
+ }
32341
+ ];
32342
+ }
32343
+ function captureEveModelInput(state, input) {
32344
+ const sessionId = input.session.id;
32345
+ const turnId = input.turn.id;
32346
+ const stepIndex = input.step.index;
32347
+ const captured = capturedModelInput(input.modelInput);
30914
32348
  if (!captured) {
30915
32349
  return;
30916
32350
  }
@@ -30951,18 +32385,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
30951
32385
  }
30952
32386
  }
30953
32387
  function capturedModelInput(modelInput) {
30954
- if (!isObject(modelInput)) {
30955
- return void 0;
30956
- }
30957
- const messages = modelInput["messages"];
30958
- if (!Array.isArray(messages)) {
30959
- return void 0;
30960
- }
30961
- const instructions = modelInput["instructions"];
30962
- const value = [
30963
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
30964
- ...messages
30965
- ];
32388
+ const { instructions, messages } = modelInput;
32389
+ const value = [];
32390
+ if (typeof instructions === "string") {
32391
+ value.push({ content: instructions, role: "system" });
32392
+ } else if (instructions) {
32393
+ value.push(...instructions.map(capturedEveModelMessage));
32394
+ }
32395
+ value.push(...messages.map(capturedEveModelMessage));
30966
32396
  try {
30967
32397
  const cloned = JSON.parse(JSON.stringify(value));
30968
32398
  if (!Array.isArray(cloned)) {
@@ -30973,8 +32403,123 @@ function capturedModelInput(modelInput) {
30973
32403
  return void 0;
30974
32404
  }
30975
32405
  }
32406
+ function capturedEveModelMessage(message) {
32407
+ const { content, role } = message;
32408
+ if (typeof content === "string") {
32409
+ return { content, role };
32410
+ }
32411
+ return { content: content.map(capturedEveModelContentPart), role };
32412
+ }
32413
+ function capturedEveModelContentPart(part) {
32414
+ switch (part.type) {
32415
+ case "text":
32416
+ case "reasoning":
32417
+ return { text: part.text, type: part.type };
32418
+ case "image":
32419
+ return {
32420
+ image: part.image,
32421
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
32422
+ type: "image"
32423
+ };
32424
+ case "file":
32425
+ case "reasoning-file":
32426
+ return {
32427
+ data: part.data,
32428
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
32429
+ mediaType: part.mediaType,
32430
+ type: part.type
32431
+ };
32432
+ case "custom":
32433
+ return {
32434
+ ..."kind" in part ? { kind: part.kind } : {},
32435
+ type: "custom"
32436
+ };
32437
+ case "tool-call":
32438
+ return {
32439
+ input: part.input,
32440
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
32441
+ toolCallId: part.toolCallId,
32442
+ toolName: part.toolName,
32443
+ type: "tool-call"
32444
+ };
32445
+ case "tool-result": {
32446
+ const output = part.output;
32447
+ let capturedOutput;
32448
+ switch (output.type) {
32449
+ case "text":
32450
+ case "error-text":
32451
+ capturedOutput = { type: output.type, value: output.value };
32452
+ break;
32453
+ case "json":
32454
+ case "error-json":
32455
+ capturedOutput = { type: output.type, value: output.value };
32456
+ break;
32457
+ case "execution-denied":
32458
+ capturedOutput = {
32459
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
32460
+ type: "execution-denied"
32461
+ };
32462
+ break;
32463
+ case "content":
32464
+ capturedOutput = {
32465
+ type: "content",
32466
+ value: output.value.map(capturedEveModelContentPart)
32467
+ };
32468
+ break;
32469
+ }
32470
+ return {
32471
+ output: capturedOutput,
32472
+ toolCallId: part.toolCallId,
32473
+ toolName: part.toolName,
32474
+ type: "tool-result"
32475
+ };
32476
+ }
32477
+ case "tool-approval-request":
32478
+ return {
32479
+ approvalId: part.approvalId,
32480
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
32481
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
32482
+ toolCallId: part.toolCallId,
32483
+ type: "tool-approval-request"
32484
+ };
32485
+ case "tool-approval-response":
32486
+ return {
32487
+ approvalId: part.approvalId,
32488
+ approved: part.approved,
32489
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
32490
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
32491
+ type: "tool-approval-response"
32492
+ };
32493
+ case "file-data":
32494
+ case "image-data":
32495
+ return {
32496
+ data: part.data,
32497
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
32498
+ mediaType: part.mediaType,
32499
+ type: part.type
32500
+ };
32501
+ case "file-url":
32502
+ case "image-url":
32503
+ return {
32504
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
32505
+ type: part.type,
32506
+ url: part.url
32507
+ };
32508
+ case "file-id":
32509
+ case "image-file-id":
32510
+ return { fileId: part.fileId, type: part.type };
32511
+ case "file-reference":
32512
+ case "image-file-reference":
32513
+ return {
32514
+ providerReference: part.providerReference,
32515
+ type: part.type
32516
+ };
32517
+ }
32518
+ }
30976
32519
  function isCapturedModelInput(input) {
30977
- return Array.isArray(input);
32520
+ return Array.isArray(input) && input.every(
32521
+ (message) => isObject(message) && (message["role"] === "system" || message["role"] === "user" || message["role"] === "assistant" || message["role"] === "tool") && (typeof message["content"] === "string" || Array.isArray(message["content"]) && message["content"].every(isObject))
32522
+ );
30978
32523
  }
30979
32524
  function llmInputKey(sessionId, turnId, stepIndex) {
30980
32525
  return `${sessionId}\0${turnId}\0${stepIndex}`;