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
@@ -61,6 +61,7 @@ var DefaultChannel = class {
61
61
  constructor(name) {
62
62
  this.name = name;
63
63
  }
64
+ name;
64
65
  hasSubscribers = false;
65
66
  subscribe(_subscription) {
66
67
  }
@@ -1337,6 +1338,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
1337
1338
  constructor(data) {
1338
1339
  this.data = data;
1339
1340
  }
1341
+ data;
1340
1342
  toStr() {
1341
1343
  const jsonObj = {
1342
1344
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -1757,6 +1759,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
1757
1759
  constructor(data) {
1758
1760
  this.data = data;
1759
1761
  }
1762
+ data;
1760
1763
  toStr() {
1761
1764
  const jsonObj = {
1762
1765
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -4916,6 +4919,144 @@ var SpanCache = class {
4916
4919
  }
4917
4920
  };
4918
4921
 
4922
+ // src/span-origin.ts
4923
+ var INSTRUMENTATION_NAMES = {
4924
+ AI_SDK: "ai-sdk",
4925
+ ANTHROPIC: "anthropic",
4926
+ BEDROCK_RUNTIME: "bedrock-runtime",
4927
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
4928
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
4929
+ COHERE: "cohere",
4930
+ CURSOR_SDK: "cursor-sdk",
4931
+ EVE: "eve",
4932
+ FLUE: "flue",
4933
+ GENKIT: "genkit",
4934
+ GITHUB_COPILOT: "github-copilot",
4935
+ GOOGLE_ADK: "google-adk",
4936
+ GOOGLE_GENAI: "google-genai",
4937
+ GROQ: "groq",
4938
+ HUGGINGFACE: "huggingface",
4939
+ LANGCHAIN: "langchain",
4940
+ LANGSMITH: "langsmith",
4941
+ MASTRA: "mastra",
4942
+ MISTRAL: "mistral",
4943
+ OPENAI: "openai",
4944
+ OPENAI_AGENTS: "openai-agents",
4945
+ OPENAI_CODEX: "openai-codex",
4946
+ OPENROUTER: "openrouter",
4947
+ OPENROUTER_AGENT: "openrouter-agent",
4948
+ PI_CODING_AGENT: "pi-coding-agent",
4949
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
4950
+ };
4951
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
4952
+ "braintrust.spanInstrumentationName"
4953
+ );
4954
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
4955
+ function withSpanInstrumentationName(args, instrumentationName) {
4956
+ return {
4957
+ ...args,
4958
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
4959
+ };
4960
+ }
4961
+ function getSpanInstrumentationName(args) {
4962
+ if (typeof args !== "object" || args === null) {
4963
+ return void 0;
4964
+ }
4965
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
4966
+ return isSpanInstrumentationName(value) ? value : void 0;
4967
+ }
4968
+ function detectSpanOriginEnvironment(explicit) {
4969
+ if (explicit) return explicit;
4970
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
4971
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
4972
+ if (envType || envName) {
4973
+ return {
4974
+ ...envType ? { type: envType } : {},
4975
+ ...envName ? { name: envName } : {}
4976
+ };
4977
+ }
4978
+ const ci = firstPresent([
4979
+ ["GITHUB_ACTIONS", "github_actions"],
4980
+ ["GITLAB_CI", "gitlab_ci"],
4981
+ ["CIRCLECI", "circleci"],
4982
+ ["BUILDKITE", "buildkite"],
4983
+ ["JENKINS_URL", "jenkins"],
4984
+ ["JENKINS_HOME", "jenkins"],
4985
+ ["TF_BUILD", "azure_pipelines"],
4986
+ ["TEAMCITY_VERSION", "teamcity"],
4987
+ ["TRAVIS", "travis"],
4988
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
4989
+ ]);
4990
+ if (ci) return { type: "ci", name: ci };
4991
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
4992
+ const earlyServer = firstPresent([
4993
+ ["VERCEL", "vercel"],
4994
+ ["NETLIFY", "netlify"]
4995
+ ]);
4996
+ if (earlyServer) return { type: "server", name: earlyServer };
4997
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
4998
+ return { type: "server", name: "ecs" };
4999
+ }
5000
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
5001
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
5002
+ return { type: "server", name: "ecs" };
5003
+ }
5004
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
5005
+ return { type: "server", name: "aws_lambda" };
5006
+ }
5007
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
5008
+ return { type: "server", name: "aws_lambda" };
5009
+ }
5010
+ const server = firstPresent([
5011
+ ["K_SERVICE", "cloud_run"],
5012
+ ["FUNCTION_TARGET", "gcp_functions"],
5013
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
5014
+ ["DYNO", "heroku"],
5015
+ ["FLY_APP_NAME", "fly"],
5016
+ ["RAILWAY_ENVIRONMENT", "railway"],
5017
+ ["RENDER_SERVICE_NAME", "render"]
5018
+ ]);
5019
+ if (server) return { type: "server", name: server };
5020
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
5021
+ }
5022
+ function makeSpanOrigin(instrumentationName, environment) {
5023
+ return {
5024
+ name: "braintrust.sdk.javascript",
5025
+ version: SDK_VERSION,
5026
+ instrumentation: { name: instrumentationName },
5027
+ ...environment ? { environment } : {}
5028
+ };
5029
+ }
5030
+ function mergeSpanOriginContext(context, instrumentationName, environment) {
5031
+ const next = { ...context ?? {} };
5032
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
5033
+ next.span_origin = {
5034
+ ...makeSpanOrigin(instrumentationName, environment),
5035
+ ...current
5036
+ };
5037
+ return next;
5038
+ }
5039
+ function isSpanInstrumentationName(value) {
5040
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
5041
+ }
5042
+ function firstPresent(entries) {
5043
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
5044
+ }
5045
+ function deploymentModeEnvironment(_key, value) {
5046
+ if (!value) return void 0;
5047
+ const normalized = value.toLowerCase();
5048
+ if (normalized === "production" || normalized === "staging") {
5049
+ return { type: "server", name: normalized };
5050
+ }
5051
+ if (normalized === "development" || normalized === "local") {
5052
+ return { type: "local", name: normalized };
5053
+ }
5054
+ return { name: value };
5055
+ }
5056
+ function isObject2(value) {
5057
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5058
+ }
5059
+
4919
5060
  // src/logger.ts
4920
5061
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
4921
5062
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -4964,6 +5105,7 @@ var LoginInvalidOrgError = class extends Error {
4964
5105
  super(message);
4965
5106
  this.message = message;
4966
5107
  }
5108
+ message;
4967
5109
  };
4968
5110
  var REDACTION_FIELDS = [
4969
5111
  "input",
@@ -4979,6 +5121,8 @@ var MaskingError = class {
4979
5121
  this.fieldName = fieldName;
4980
5122
  this.errorType = errorType;
4981
5123
  }
5124
+ fieldName;
5125
+ errorType;
4982
5126
  get errorMsg() {
4983
5127
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
4984
5128
  }
@@ -5188,7 +5332,10 @@ var BraintrustState = class _BraintrustState {
5188
5332
  diskCache: parametersDiskCache
5189
5333
  });
5190
5334
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
5335
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
5336
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5191
5337
  }
5338
+ loginParams;
5192
5339
  id;
5193
5340
  currentExperiment;
5194
5341
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -5223,6 +5370,19 @@ var BraintrustState = class _BraintrustState {
5223
5370
  _idGenerator = null;
5224
5371
  _contextManager = null;
5225
5372
  _otelFlushCallback = null;
5373
+ spanOriginEnvironment;
5374
+ traceContextSigningSecret;
5375
+ /** @internal */
5376
+ _internalSetTraceContextSigningSecret(secret) {
5377
+ const normalizedSecret = secret?.trim();
5378
+ if (normalizedSecret) {
5379
+ this.traceContextSigningSecret = normalizedSecret;
5380
+ }
5381
+ }
5382
+ /** @internal */
5383
+ _internalGetTraceContextSigningSecret() {
5384
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
5385
+ }
5226
5386
  resetLoginInfo() {
5227
5387
  this.appUrl = null;
5228
5388
  this.appPublicUrl = null;
@@ -5283,6 +5443,7 @@ var BraintrustState = class _BraintrustState {
5283
5443
  this.gitMetadataSettings = other.gitMetadataSettings;
5284
5444
  this.debugLogLevel = other.debugLogLevel;
5285
5445
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
5446
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
5286
5447
  setGlobalDebugLogLevel(
5287
5448
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
5288
5449
  );
@@ -5370,6 +5531,7 @@ var BraintrustState = class _BraintrustState {
5370
5531
  return this.debugLogLevelConfigured;
5371
5532
  }
5372
5533
  async login(loginParams) {
5534
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5373
5535
  this.setDebugLogLevel(loginParams.debugLogLevel);
5374
5536
  if (this.apiUrl && !loginParams.forceLogin) {
5375
5537
  return;
@@ -7140,6 +7302,7 @@ function initLogger(options = {}) {
7140
7302
  orgName,
7141
7303
  forceLogin,
7142
7304
  debugLogLevel,
7305
+ environment,
7143
7306
  fetch: fetch2,
7144
7307
  state: stateArg
7145
7308
  } = options || {};
@@ -7155,7 +7318,9 @@ function initLogger(options = {}) {
7155
7318
  project_id: projectId
7156
7319
  };
7157
7320
  const state = stateArg ?? _globalState;
7321
+ state._internalSetTraceContextSigningSecret(apiKey);
7158
7322
  state.setDebugLogLevel(debugLogLevel);
7323
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
7159
7324
  state.enforceQueueSizeLimit(true);
7160
7325
  const lazyMetadata = new LazyValue(
7161
7326
  async () => {
@@ -7678,7 +7843,9 @@ function deepCopyEvent(event) {
7678
7843
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
7679
7844
  const attachmentMarker = ++deepCopyEventMarkerCounter;
7680
7845
  const serialized = JSON.stringify(event, (_k, v) => {
7681
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
7846
+ if (v instanceof Error) {
7847
+ return v.message;
7848
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
7682
7849
  return `<span>`;
7683
7850
  } else if (v instanceof Experiment2) {
7684
7851
  return `<experiment>`;
@@ -7795,6 +7962,11 @@ var ObjectFetcher = class {
7795
7962
  this._internal_btql = _internal_btql;
7796
7963
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
7797
7964
  }
7965
+ objectType;
7966
+ pinnedVersion;
7967
+ mutateRecord;
7968
+ _internal_btql;
7969
+ _internalBrainstoreRealtime;
7798
7970
  _fetchedData = void 0;
7799
7971
  get id() {
7800
7972
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -8285,6 +8457,7 @@ var SpanImpl = class _SpanImpl {
8285
8457
  constructor(args) {
8286
8458
  this._state = args.state;
8287
8459
  this._propagatedState = args.propagatedState;
8460
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
8288
8461
  const spanAttributes = args.spanAttributes ?? {};
8289
8462
  const rawEvent = args.event ?? {};
8290
8463
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -8314,7 +8487,11 @@ var SpanImpl = class _SpanImpl {
8314
8487
  metrics: {
8315
8488
  start: args.startTime ?? getCurrentUnixTimestamp()
8316
8489
  },
8317
- context: { ...callerLocation },
8490
+ context: mergeSpanOriginContext(
8491
+ { ...callerLocation },
8492
+ instrumentationName,
8493
+ this._state.spanOriginEnvironment
8494
+ ),
8318
8495
  span_attributes: {
8319
8496
  name,
8320
8497
  type,
@@ -8688,6 +8865,7 @@ var Dataset2 = class extends ObjectFetcher {
8688
8865
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
8689
8866
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
8690
8867
  }
8868
+ state;
8691
8869
  lazyMetadata;
8692
8870
  __braintrust_dataset_marker = true;
8693
8871
  newRecords = 0;
@@ -9536,15 +9714,25 @@ function hasChoices(value) {
9536
9714
  function normalizeMetadata(metadata) {
9537
9715
  return isObject(metadata) ? metadata : void 0;
9538
9716
  }
9539
- function startSpanForEvent(config, event, channelName) {
9717
+ function startSpanForEvent(config, event, channelName, instrumentationName) {
9540
9718
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
9541
9719
  config,
9542
9720
  event
9543
9721
  );
9544
- const span = startSpan({
9545
- name,
9546
- spanAttributes
9547
- });
9722
+ const spanArgs = withSpanInstrumentationName(
9723
+ {
9724
+ name,
9725
+ spanAttributes
9726
+ },
9727
+ instrumentationName
9728
+ );
9729
+ let span;
9730
+ try {
9731
+ span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
9732
+ } catch (error) {
9733
+ debugLogger.error(`Error starting span for ${channelName}:`, error);
9734
+ span = startSpan(spanArgs);
9735
+ }
9548
9736
  const startTime = getCurrentUnixTimestamp();
9549
9737
  try {
9550
9738
  const { input, metadata } = config.extractInput(
@@ -9575,7 +9763,7 @@ function shouldTraceEvent(config, event, channelName) {
9575
9763
  return true;
9576
9764
  }
9577
9765
  }
9578
- function ensureSpanStateForEvent(states, config, event, channelName) {
9766
+ function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
9579
9767
  const key = event;
9580
9768
  const existing = states.get(key);
9581
9769
  if (existing) {
@@ -9584,11 +9772,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
9584
9772
  if (!shouldTraceEvent(config, event, channelName)) {
9585
9773
  return void 0;
9586
9774
  }
9587
- const created = startSpanForEvent(config, event, channelName);
9775
+ const created = startSpanForEvent(
9776
+ config,
9777
+ event,
9778
+ channelName,
9779
+ instrumentationName
9780
+ );
9588
9781
  states.set(key, created);
9589
9782
  return created;
9590
9783
  }
9591
- function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName) {
9784
+ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName, instrumentationName) {
9592
9785
  const state = _internalGetGlobalState();
9593
9786
  const startChannel = tracingChannel.start;
9594
9787
  const contextManager = state?.contextManager;
@@ -9606,7 +9799,8 @@ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName
9606
9799
  states,
9607
9800
  config,
9608
9801
  event,
9609
- channelName
9802
+ channelName,
9803
+ instrumentationName
9610
9804
  );
9611
9805
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
9612
9806
  }
@@ -9615,15 +9809,21 @@ function bindCurrentSpanStoreToStart(tracingChannel, states, config, channelName
9615
9809
  startChannel.unbindStore(currentSpanStore);
9616
9810
  };
9617
9811
  }
9618
- function logErrorAndEnd(states, event) {
9812
+ function logErrorAndEnd(states, event, channelName) {
9619
9813
  const spanData = states.get(event);
9620
9814
  if (!spanData) {
9621
9815
  return;
9622
9816
  }
9623
- spanData.span.log({
9624
- error: event.error.message
9625
- });
9626
- spanData.span.end();
9817
+ try {
9818
+ spanData.span.log({ error: event.error });
9819
+ } catch (error) {
9820
+ debugLogger.error(`Error logging failure for ${channelName}:`, error);
9821
+ }
9822
+ try {
9823
+ spanData.span.end();
9824
+ } catch (error) {
9825
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
9826
+ }
9627
9827
  states.delete(event);
9628
9828
  }
9629
9829
  function runStreamingCompletionHook(args) {
@@ -9673,7 +9873,8 @@ function traceAsyncChannel(channel2, config) {
9673
9873
  tracingChannel,
9674
9874
  states,
9675
9875
  config,
9676
- channelName
9876
+ channelName,
9877
+ channel2.instrumentationName
9677
9878
  );
9678
9879
  const handlers = {
9679
9880
  start: (event) => {
@@ -9684,7 +9885,8 @@ function traceAsyncChannel(channel2, config) {
9684
9885
  states,
9685
9886
  config,
9686
9887
  event,
9687
- channelName
9888
+ channelName,
9889
+ channel2.instrumentationName
9688
9890
  );
9689
9891
  },
9690
9892
  asyncEnd: (event) => {
@@ -9721,7 +9923,7 @@ function traceAsyncChannel(channel2, config) {
9721
9923
  }
9722
9924
  },
9723
9925
  error: (event) => {
9724
- logErrorAndEnd(states, event);
9926
+ logErrorAndEnd(states, event, channelName);
9725
9927
  }
9726
9928
  };
9727
9929
  tracingChannel.subscribe(handlers);
@@ -9738,7 +9940,8 @@ function traceStreamingChannel(channel2, config) {
9738
9940
  tracingChannel,
9739
9941
  states,
9740
9942
  config,
9741
- channelName
9943
+ channelName,
9944
+ channel2.instrumentationName
9742
9945
  );
9743
9946
  const handlers = {
9744
9947
  start: (event) => {
@@ -9749,7 +9952,8 @@ function traceStreamingChannel(channel2, config) {
9749
9952
  states,
9750
9953
  config,
9751
9954
  event,
9752
- channelName
9955
+ channelName,
9956
+ channel2.instrumentationName
9753
9957
  );
9754
9958
  },
9755
9959
  asyncEnd: (event) => {
@@ -9768,6 +9972,7 @@ function traceStreamingChannel(channel2, config) {
9768
9972
  }
9769
9973
  },
9770
9974
  onComplete: (chunks) => {
9975
+ let completion2;
9771
9976
  try {
9772
9977
  let output;
9773
9978
  let metrics;
@@ -9798,18 +10003,11 @@ function traceStreamingChannel(channel2, config) {
9798
10003
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
9799
10004
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9800
10005
  }
9801
- runStreamingCompletionHook({
9802
- channelName,
9803
- chunks,
9804
- config,
9805
- endEvent: asyncEndEvent,
10006
+ completion2 = {
9806
10007
  ...metadata !== void 0 ? { metadata } : {},
9807
10008
  metrics,
9808
- output,
9809
- result: asyncEndEvent.result,
9810
- span,
9811
- startTime
9812
- });
10009
+ output
10010
+ };
9813
10011
  span.log({
9814
10012
  output,
9815
10013
  ...metadata !== void 0 ? { metadata } : {},
@@ -9821,11 +10019,49 @@ function traceStreamingChannel(channel2, config) {
9821
10019
  error
9822
10020
  );
9823
10021
  } finally {
9824
- span.end();
10022
+ try {
10023
+ span.end();
10024
+ } catch (error) {
10025
+ debugLogger.error(
10026
+ `Error ending span for ${channelName}:`,
10027
+ error
10028
+ );
10029
+ }
9825
10030
  states.delete(event);
9826
10031
  }
10032
+ if (completion2) {
10033
+ runStreamingCompletionHook({
10034
+ channelName,
10035
+ chunks,
10036
+ config,
10037
+ endEvent: asyncEndEvent,
10038
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
10039
+ metrics: completion2.metrics,
10040
+ output: completion2.output,
10041
+ result: asyncEndEvent.result,
10042
+ span,
10043
+ startTime
10044
+ });
10045
+ }
9827
10046
  },
9828
10047
  onError: (error) => {
10048
+ try {
10049
+ span.log({ error });
10050
+ } catch (loggingError) {
10051
+ debugLogger.error(
10052
+ `Error logging failure for ${channelName}:`,
10053
+ loggingError
10054
+ );
10055
+ }
10056
+ try {
10057
+ span.end();
10058
+ } catch (endingError) {
10059
+ debugLogger.error(
10060
+ `Error ending span for ${channelName}:`,
10061
+ endingError
10062
+ );
10063
+ }
10064
+ states.delete(event);
9829
10065
  runStreamingErrorHook({
9830
10066
  channelName,
9831
10067
  config,
@@ -9834,11 +10070,6 @@ function traceStreamingChannel(channel2, config) {
9834
10070
  span,
9835
10071
  startTime
9836
10072
  });
9837
- span.log({
9838
- error: error.message
9839
- });
9840
- span.end();
9841
- states.delete(event);
9842
10073
  }
9843
10074
  });
9844
10075
  return;
@@ -9853,6 +10084,7 @@ function traceStreamingChannel(channel2, config) {
9853
10084
  states.delete(event);
9854
10085
  return;
9855
10086
  }
10087
+ let completion;
9856
10088
  try {
9857
10089
  const output = config.extractOutput(
9858
10090
  asyncEndEvent.result,
@@ -9867,17 +10099,11 @@ function traceStreamingChannel(channel2, config) {
9867
10099
  asyncEndEvent.result,
9868
10100
  asyncEndEvent
9869
10101
  );
9870
- runStreamingCompletionHook({
9871
- channelName,
9872
- config,
9873
- endEvent: asyncEndEvent,
10102
+ completion = {
9874
10103
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
9875
10104
  metrics,
9876
- output,
9877
- result: asyncEndEvent.result,
9878
- span,
9879
- startTime
9880
- });
10105
+ output
10106
+ };
9881
10107
  span.log({
9882
10108
  output,
9883
10109
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -9886,12 +10112,30 @@ function traceStreamingChannel(channel2, config) {
9886
10112
  } catch (error) {
9887
10113
  debugLogger.error(`Error extracting output for ${channelName}:`, error);
9888
10114
  } finally {
9889
- span.end();
10115
+ try {
10116
+ span.end();
10117
+ } catch (error) {
10118
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
10119
+ }
9890
10120
  states.delete(event);
9891
10121
  }
10122
+ if (completion) {
10123
+ runStreamingCompletionHook({
10124
+ channelName,
10125
+ config,
10126
+ endEvent: asyncEndEvent,
10127
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
10128
+ metrics: completion.metrics,
10129
+ output: completion.output,
10130
+ result: asyncEndEvent.result,
10131
+ span,
10132
+ startTime
10133
+ });
10134
+ }
9892
10135
  },
9893
10136
  error: (event) => {
9894
10137
  const spanData = states.get(event);
10138
+ logErrorAndEnd(states, event, channelName);
9895
10139
  if (spanData) {
9896
10140
  runStreamingErrorHook({
9897
10141
  channelName,
@@ -9902,7 +10146,6 @@ function traceStreamingChannel(channel2, config) {
9902
10146
  startTime: spanData.startTime
9903
10147
  });
9904
10148
  }
9905
- logErrorAndEnd(states, event);
9906
10149
  }
9907
10150
  };
9908
10151
  tracingChannel.subscribe(handlers);
@@ -9919,7 +10162,8 @@ function traceSyncStreamChannel(channel2, config) {
9919
10162
  tracingChannel,
9920
10163
  states,
9921
10164
  config,
9922
- channelName
10165
+ channelName,
10166
+ channel2.instrumentationName
9923
10167
  );
9924
10168
  const handlers = {
9925
10169
  start: (event) => {
@@ -9930,7 +10174,8 @@ function traceSyncStreamChannel(channel2, config) {
9930
10174
  states,
9931
10175
  config,
9932
10176
  event,
9933
- channelName
10177
+ channelName,
10178
+ channel2.instrumentationName
9934
10179
  );
9935
10180
  },
9936
10181
  end: (event) => {
@@ -10024,7 +10269,7 @@ function traceSyncStreamChannel(channel2, config) {
10024
10269
  handleResolvedResult(endEvent.result);
10025
10270
  },
10026
10271
  error: (event) => {
10027
- logErrorAndEnd(states, event);
10272
+ logErrorAndEnd(states, event, channelName);
10028
10273
  }
10029
10274
  };
10030
10275
  tracingChannel.subscribe(handlers);
@@ -10203,7 +10448,8 @@ function processInputAttachments(input) {
10203
10448
  function channel(spec) {
10204
10449
  return spec;
10205
10450
  }
10206
- function defineChannels(pkg, channels) {
10451
+ function defineChannels(pkg, channels, options) {
10452
+ const { instrumentationName } = options;
10207
10453
  return Object.fromEntries(
10208
10454
  Object.entries(channels).map(([key, spec]) => {
10209
10455
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -10216,6 +10462,7 @@ function defineChannels(pkg, channels) {
10216
10462
  key,
10217
10463
  {
10218
10464
  ...asyncSpec,
10465
+ instrumentationName,
10219
10466
  tracingChannel: tracingChannel2,
10220
10467
  tracePromise: (fn, context) => tracingChannel2().tracePromise(
10221
10468
  fn,
@@ -10233,6 +10480,7 @@ function defineChannels(pkg, channels) {
10233
10480
  key,
10234
10481
  {
10235
10482
  ...syncSpec,
10483
+ instrumentationName,
10236
10484
  tracingChannel,
10237
10485
  traceSync: (fn, context) => tracingChannel().traceSync(
10238
10486
  fn,
@@ -10246,44 +10494,48 @@ function defineChannels(pkg, channels) {
10246
10494
  }
10247
10495
 
10248
10496
  // src/instrumentation/plugins/openai-channels.ts
10249
- var openAIChannels = defineChannels("openai", {
10250
- chatCompletionsCreate: channel({
10251
- channelName: "chat.completions.create",
10252
- kind: "async"
10253
- }),
10254
- embeddingsCreate: channel({
10255
- channelName: "embeddings.create",
10256
- kind: "async"
10257
- }),
10258
- betaChatCompletionsParse: channel({
10259
- channelName: "beta.chat.completions.parse",
10260
- kind: "async"
10261
- }),
10262
- betaChatCompletionsStream: channel({
10263
- channelName: "beta.chat.completions.stream",
10264
- kind: "sync-stream"
10265
- }),
10266
- moderationsCreate: channel({
10267
- channelName: "moderations.create",
10268
- kind: "async"
10269
- }),
10270
- responsesCreate: channel({
10271
- channelName: "responses.create",
10272
- kind: "async"
10273
- }),
10274
- responsesStream: channel({
10275
- channelName: "responses.stream",
10276
- kind: "sync-stream"
10277
- }),
10278
- responsesParse: channel({
10279
- channelName: "responses.parse",
10280
- kind: "async"
10281
- }),
10282
- responsesCompact: channel({
10283
- channelName: "responses.compact",
10284
- kind: "async"
10285
- })
10286
- });
10497
+ var openAIChannels = defineChannels(
10498
+ "openai",
10499
+ {
10500
+ chatCompletionsCreate: channel({
10501
+ channelName: "chat.completions.create",
10502
+ kind: "async"
10503
+ }),
10504
+ embeddingsCreate: channel({
10505
+ channelName: "embeddings.create",
10506
+ kind: "async"
10507
+ }),
10508
+ betaChatCompletionsParse: channel({
10509
+ channelName: "beta.chat.completions.parse",
10510
+ kind: "async"
10511
+ }),
10512
+ betaChatCompletionsStream: channel({
10513
+ channelName: "beta.chat.completions.stream",
10514
+ kind: "sync-stream"
10515
+ }),
10516
+ moderationsCreate: channel({
10517
+ channelName: "moderations.create",
10518
+ kind: "async"
10519
+ }),
10520
+ responsesCreate: channel({
10521
+ channelName: "responses.create",
10522
+ kind: "async"
10523
+ }),
10524
+ responsesStream: channel({
10525
+ channelName: "responses.stream",
10526
+ kind: "sync-stream"
10527
+ }),
10528
+ responsesParse: channel({
10529
+ channelName: "responses.parse",
10530
+ kind: "async"
10531
+ }),
10532
+ responsesCompact: channel({
10533
+ channelName: "responses.compact",
10534
+ kind: "async"
10535
+ })
10536
+ },
10537
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
10538
+ );
10287
10539
 
10288
10540
  // src/openai-utils.ts
10289
10541
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -10798,16 +11050,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
10798
11050
  }
10799
11051
 
10800
11052
  // src/instrumentation/plugins/openai-codex-channels.ts
10801
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
10802
- run: channel({
10803
- channelName: "Thread.run",
10804
- kind: "async"
10805
- }),
10806
- runStreamed: channel({
10807
- channelName: "Thread.runStreamed",
10808
- kind: "async"
10809
- })
10810
- });
11053
+ var openAICodexChannels = defineChannels(
11054
+ "@openai/codex-sdk",
11055
+ {
11056
+ run: channel({
11057
+ channelName: "Thread.run",
11058
+ kind: "async"
11059
+ }),
11060
+ runStreamed: channel({
11061
+ channelName: "Thread.runStreamed",
11062
+ kind: "async"
11063
+ })
11064
+ },
11065
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
11066
+ );
10811
11067
 
10812
11068
  // src/instrumentation/plugins/openai-codex-plugin.ts
10813
11069
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -10895,10 +11151,15 @@ function startCodexRun(event, operation) {
10895
11151
  provider: "openai",
10896
11152
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
10897
11153
  };
10898
- const span = startSpan({
10899
- name: "OpenAI Codex",
10900
- spanAttributes: { type: "task" /* TASK */ }
10901
- });
11154
+ const span = startSpan(
11155
+ withSpanInstrumentationName(
11156
+ {
11157
+ name: "OpenAI Codex",
11158
+ spanAttributes: { type: "task" /* TASK */ }
11159
+ },
11160
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11161
+ )
11162
+ );
10902
11163
  const startTime = getCurrentUnixTimestamp();
10903
11164
  safeLog(span, {
10904
11165
  input: sanitizedInput,
@@ -11047,7 +11308,12 @@ async function createCompletedItemSpan(state, item) {
11047
11308
  if (!spanArgs) {
11048
11309
  return;
11049
11310
  }
11050
- const span = startSpan(spanArgs.start);
11311
+ const span = startSpan(
11312
+ withSpanInstrumentationName(
11313
+ spanArgs.start,
11314
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11315
+ )
11316
+ );
11051
11317
  safeLog(span, spanArgs.end);
11052
11318
  span.end();
11053
11319
  }
@@ -11089,15 +11355,20 @@ async function ensureActiveLlmSpan(state) {
11089
11355
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
11090
11356
  "openai_codex.llm_sequence": sequence
11091
11357
  };
11092
- const span = startSpan({
11093
- event: {
11094
- ...sequence === 1 ? { input: state.input } : {},
11095
- metadata
11096
- },
11097
- name: "OpenAI Codex LLM",
11098
- parent: await state.span.export(),
11099
- spanAttributes: { type: "llm" /* LLM */ }
11100
- });
11358
+ const span = startSpan(
11359
+ withSpanInstrumentationName(
11360
+ {
11361
+ event: {
11362
+ ...sequence === 1 ? { input: state.input } : {},
11363
+ metadata
11364
+ },
11365
+ name: "OpenAI Codex LLM",
11366
+ parent: await state.span.export(),
11367
+ spanAttributes: { type: "llm" /* LLM */ }
11368
+ },
11369
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11370
+ )
11371
+ );
11101
11372
  state.activeLlmSpan = {
11102
11373
  anonymousMessages: [],
11103
11374
  anonymousReasoning: [],
@@ -11143,7 +11414,15 @@ async function startCodexItemSpan(state, item) {
11143
11414
  if (!spanArgs) {
11144
11415
  return;
11145
11416
  }
11146
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
11417
+ state.activeItemSpans.set(
11418
+ itemId,
11419
+ startSpan(
11420
+ withSpanInstrumentationName(
11421
+ spanArgs.start,
11422
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
11423
+ )
11424
+ )
11425
+ );
11147
11426
  }
11148
11427
  function updateCodexItem(state, item) {
11149
11428
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -11453,20 +11732,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
11453
11732
  }
11454
11733
 
11455
11734
  // src/instrumentation/plugins/anthropic-channels.ts
11456
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
11457
- messagesCreate: channel({
11458
- channelName: "messages.create",
11459
- kind: "async"
11460
- }),
11461
- betaMessagesCreate: channel({
11462
- channelName: "beta.messages.create",
11463
- kind: "async"
11464
- }),
11465
- betaMessagesToolRunner: channel({
11466
- channelName: "beta.messages.toolRunner",
11467
- kind: "sync-stream"
11468
- })
11469
- });
11735
+ var anthropicChannels = defineChannels(
11736
+ "@anthropic-ai/sdk",
11737
+ {
11738
+ messagesCreate: channel({
11739
+ channelName: "messages.create",
11740
+ kind: "async"
11741
+ }),
11742
+ betaMessagesCreate: channel({
11743
+ channelName: "beta.messages.create",
11744
+ kind: "async"
11745
+ }),
11746
+ betaMessagesToolRunner: channel({
11747
+ channelName: "beta.messages.toolRunner",
11748
+ kind: "sync-stream"
11749
+ })
11750
+ },
11751
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
11752
+ );
11470
11753
 
11471
11754
  // src/instrumentation/plugins/anthropic-plugin.ts
11472
11755
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -11539,12 +11822,17 @@ var AnthropicPlugin = class extends BasePlugin {
11539
11822
  return;
11540
11823
  }
11541
11824
  const params = event.arguments[0] ?? {};
11542
- const span = startSpan({
11543
- name: "anthropic.beta.messages.toolRunner",
11544
- spanAttributes: {
11545
- type: "task" /* TASK */
11546
- }
11547
- });
11825
+ const span = startSpan(
11826
+ withSpanInstrumentationName(
11827
+ {
11828
+ name: "anthropic.beta.messages.toolRunner",
11829
+ spanAttributes: {
11830
+ type: "task" /* TASK */
11831
+ }
11832
+ },
11833
+ INSTRUMENTATION_NAMES.ANTHROPIC
11834
+ )
11835
+ );
11548
11836
  span.log({
11549
11837
  input: processAttachmentsInInput(
11550
11838
  coalesceInput(params.messages ?? [], params.system)
@@ -11685,19 +11973,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
11685
11973
  return finalizeError(error);
11686
11974
  }
11687
11975
  },
11688
- {
11689
- event: {
11690
- input: getAnthropicToolRunnerInput(args),
11691
- metadata: {
11692
- "gen_ai.tool.name": toolName,
11693
- provider: "anthropic"
11976
+ withSpanInstrumentationName(
11977
+ {
11978
+ event: {
11979
+ input: getAnthropicToolRunnerInput(args),
11980
+ metadata: {
11981
+ "gen_ai.tool.name": toolName,
11982
+ provider: "anthropic"
11983
+ }
11984
+ },
11985
+ name: `tool: ${toolName}`,
11986
+ spanAttributes: {
11987
+ type: "tool" /* TOOL */
11694
11988
  }
11695
11989
  },
11696
- name: `tool: ${toolName}`,
11697
- spanAttributes: {
11698
- type: "tool" /* TOOL */
11699
- }
11700
- }
11990
+ INSTRUMENTATION_NAMES.ANTHROPIC
11991
+ )
11701
11992
  );
11702
11993
  },
11703
11994
  writable: runDescriptor?.writable ?? true
@@ -12292,32 +12583,296 @@ function currentWorkflowAgentWrapperSpan() {
12292
12583
  return void 0;
12293
12584
  }
12294
12585
 
12295
- // src/wrappers/ai-sdk/telemetry.ts
12296
- function braintrustAISDKTelemetry() {
12297
- const operations = /* @__PURE__ */ new Map();
12298
- const operationKeysByCallId = /* @__PURE__ */ new Map();
12299
- const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
12300
- const modelSpans = /* @__PURE__ */ new Map();
12301
- const objectSpans = /* @__PURE__ */ new Map();
12302
- const embedSpans = /* @__PURE__ */ new Map();
12303
- const rerankSpans = /* @__PURE__ */ new Map();
12304
- const toolSpans = /* @__PURE__ */ new Map();
12305
- let workflowAgentOperationCounter = 0;
12306
- const runSafely = (name, callback) => {
12307
- try {
12308
- callback();
12309
- } catch (error) {
12310
- console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
12311
- }
12586
+ // src/wrappers/ai-sdk/harness-agent-context.ts
12587
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
12588
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
12589
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
12590
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
12591
+ var harnessTurnParentStore;
12592
+ function serializedTurnContext(value) {
12593
+ if (!isObject(value)) {
12594
+ return void 0;
12595
+ }
12596
+ const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
12597
+ if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
12598
+ return void 0;
12599
+ }
12600
+ const expectedSignature = signTurnContext({
12601
+ parent: context.parent,
12602
+ sessionId: context.sessionId
12603
+ });
12604
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
12605
+ return void 0;
12606
+ }
12607
+ const parent = SpanComponentsV4.fromStr(context.parent);
12608
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
12609
+ return void 0;
12610
+ }
12611
+ return {
12612
+ parent: context.parent,
12613
+ sessionId: context.sessionId
12312
12614
  };
12313
- const startChildSpan = (operationKey, name, type, event) => {
12314
- const parent = operations.get(operationKey)?.span;
12315
- const spanArgs = {
12316
- name,
12317
- spanAttributes: { type },
12318
- ...event ? { event } : {}
12319
- };
12320
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
12615
+ }
12616
+ function signTurnContext(context) {
12617
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
12618
+ return secret ? isomorph_default.hmacSha256?.(
12619
+ secret,
12620
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
12621
+ ) : void 0;
12622
+ }
12623
+ function continuationParentFromCreateSessionParams(params) {
12624
+ if (!isObject(params)) {
12625
+ return void 0;
12626
+ }
12627
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
12628
+ const context = serializedTurnContext(continuation);
12629
+ if (!context) {
12630
+ return void 0;
12631
+ }
12632
+ if (params.sessionId !== context.sessionId) {
12633
+ return void 0;
12634
+ }
12635
+ return context.parent;
12636
+ }
12637
+ function exportSpanSynchronously(span) {
12638
+ const parentInfo = span.getParentInfo();
12639
+ if (!parentInfo) {
12640
+ return void 0;
12641
+ }
12642
+ const objectId = parentInfo.objectId.getSync().value;
12643
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
12644
+ return void 0;
12645
+ }
12646
+ return new SpanComponentsV4({
12647
+ object_type: parentInfo.objectType,
12648
+ ...objectId ? { object_id: objectId } : {
12649
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
12650
+ },
12651
+ row_id: span.id,
12652
+ root_span_id: span.rootSpanId,
12653
+ span_id: span.spanId
12654
+ }).toStr();
12655
+ }
12656
+ function exportedParent(parent) {
12657
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
12658
+ }
12659
+ function addSerializedContext(args) {
12660
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
12661
+ return;
12662
+ }
12663
+ const parent = exportedParent(args.parent);
12664
+ if (!parent) {
12665
+ return;
12666
+ }
12667
+ const context = {
12668
+ parent,
12669
+ sessionId: args.sessionId
12670
+ };
12671
+ const signature = signTurnContext(context);
12672
+ if (!signature) {
12673
+ return;
12674
+ }
12675
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
12676
+ configurable: true,
12677
+ enumerable: true,
12678
+ value: {
12679
+ ...context,
12680
+ signature
12681
+ },
12682
+ writable: true
12683
+ });
12684
+ }
12685
+ function lifecycleMethodDescriptor(session, method) {
12686
+ let owner = session;
12687
+ while (owner) {
12688
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
12689
+ if (descriptor) {
12690
+ return { descriptor, owner };
12691
+ }
12692
+ owner = Object.getPrototypeOf(owner);
12693
+ }
12694
+ return void 0;
12695
+ }
12696
+ function patchLifecycleMethod(session, method) {
12697
+ let resolvedDescriptor;
12698
+ try {
12699
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
12700
+ } catch {
12701
+ return;
12702
+ }
12703
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
12704
+ return;
12705
+ }
12706
+ const { descriptor, owner } = resolvedDescriptor;
12707
+ const patched = patchedLifecycleMethods.get(owner);
12708
+ if (patched?.has(method)) {
12709
+ return;
12710
+ }
12711
+ const original = descriptor.value;
12712
+ const replacement = function(...args) {
12713
+ const result = Reflect.apply(original, this, args);
12714
+ const addContext = (state) => {
12715
+ try {
12716
+ const parent = sessionTurnParents.get(this);
12717
+ if (!parent) {
12718
+ return;
12719
+ }
12720
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
12721
+ addSerializedContext({
12722
+ continuation,
12723
+ parent,
12724
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
12725
+ });
12726
+ } catch {
12727
+ }
12728
+ };
12729
+ try {
12730
+ if (isObject(result) && typeof result.then === "function") {
12731
+ void Promise.resolve(result).then(addContext, () => {
12732
+ });
12733
+ } else {
12734
+ addContext(result);
12735
+ }
12736
+ } catch {
12737
+ }
12738
+ return result;
12739
+ };
12740
+ try {
12741
+ Object.defineProperty(owner, method, {
12742
+ ...descriptor,
12743
+ value: replacement
12744
+ });
12745
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
12746
+ ownerMethods.add(method);
12747
+ if (!patched) {
12748
+ patchedLifecycleMethods.set(owner, ownerMethods);
12749
+ }
12750
+ } catch {
12751
+ }
12752
+ }
12753
+ function patchLifecycleMethods(session) {
12754
+ patchLifecycleMethod(session, "suspendTurn");
12755
+ patchLifecycleMethod(session, "detach");
12756
+ patchLifecycleMethod(session, "stop");
12757
+ }
12758
+ function registerHarnessTurnSpan(args) {
12759
+ if (!isObject(args.session)) {
12760
+ return;
12761
+ }
12762
+ const session = args.session;
12763
+ sessionTurnParents.set(session, args.span);
12764
+ wrapperTurnParents.set(args.span, args.span);
12765
+ patchLifecycleMethods(session);
12766
+ }
12767
+ function harnessContinuationParent(session) {
12768
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
12769
+ }
12770
+ function captureHarnessCreateSessionParent(params) {
12771
+ try {
12772
+ return continuationParentFromCreateSessionParams(params);
12773
+ } catch {
12774
+ return void 0;
12775
+ }
12776
+ }
12777
+ function registerHarnessSessionParent(session, parent) {
12778
+ if (!parent || !isObject(session)) {
12779
+ return;
12780
+ }
12781
+ const harnessSession = session;
12782
+ sessionTurnParents.set(harnessSession, parent);
12783
+ patchLifecycleMethods(harnessSession);
12784
+ }
12785
+ function currentHarnessTurnParent() {
12786
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
12787
+ }
12788
+ function bindHarnessTurnParentToStart(tracingChannel, parentFromEvent) {
12789
+ const startChannel = tracingChannel.start;
12790
+ if (!startChannel) {
12791
+ return () => {
12792
+ };
12793
+ }
12794
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
12795
+ const store = harnessTurnParentStore;
12796
+ startChannel.bindStore(
12797
+ store,
12798
+ (event) => parentFromEvent(event) ?? store.getStore()
12799
+ );
12800
+ return () => {
12801
+ startChannel.unbindStore(store);
12802
+ };
12803
+ }
12804
+ function startHarnessTurnChildSpan(parent, args) {
12805
+ const spanArgs = withSpanInstrumentationName(
12806
+ args,
12807
+ INSTRUMENTATION_NAMES.AI_SDK
12808
+ );
12809
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
12810
+ }
12811
+ function updateHarnessTurn(parent, update = {}) {
12812
+ if (!parent) {
12813
+ return;
12814
+ }
12815
+ const log = (event) => {
12816
+ if (typeof parent === "string") {
12817
+ updateSpan({ exported: parent, ...event });
12818
+ } else {
12819
+ parent.log(event);
12820
+ }
12821
+ };
12822
+ try {
12823
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
12824
+ log({ output: null });
12825
+ }
12826
+ log(update);
12827
+ } catch {
12828
+ }
12829
+ }
12830
+ function endHarnessTurn(parent) {
12831
+ const endTime = getCurrentUnixTimestamp();
12832
+ if (!parent) {
12833
+ return endTime;
12834
+ }
12835
+ try {
12836
+ const event = { metrics: { end: endTime } };
12837
+ if (typeof parent === "string") {
12838
+ updateSpan({ exported: parent, ...event });
12839
+ } else {
12840
+ parent.log(event);
12841
+ }
12842
+ } catch {
12843
+ }
12844
+ return endTime;
12845
+ }
12846
+
12847
+ // src/wrappers/ai-sdk/telemetry.ts
12848
+ function braintrustAISDKTelemetry() {
12849
+ const operations = /* @__PURE__ */ new Map();
12850
+ const operationKeysByCallId = /* @__PURE__ */ new Map();
12851
+ const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
12852
+ const modelSpans = /* @__PURE__ */ new Map();
12853
+ const objectSpans = /* @__PURE__ */ new Map();
12854
+ const embedSpans = /* @__PURE__ */ new Map();
12855
+ const rerankSpans = /* @__PURE__ */ new Map();
12856
+ const toolSpans = /* @__PURE__ */ new Map();
12857
+ let workflowAgentOperationCounter = 0;
12858
+ const runSafely = (name, callback) => {
12859
+ try {
12860
+ callback();
12861
+ } catch (error) {
12862
+ console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
12863
+ }
12864
+ };
12865
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
12866
+ const parent = operations.get(operationKey)?.span;
12867
+ const spanArgs = withSpanInstrumentationName(
12868
+ {
12869
+ name,
12870
+ spanAttributes: { type },
12871
+ ...event ? { event } : {}
12872
+ },
12873
+ INSTRUMENTATION_NAMES.AI_SDK
12874
+ );
12875
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
12321
12876
  const state = operations.get(operationKey);
12322
12877
  if (state && type === "llm" /* LLM */) {
12323
12878
  state.hadModelChild = true;
@@ -12597,14 +13152,30 @@ function braintrustAISDKTelemetry() {
12597
13152
  const workflowAgent = operationName === "WorkflowAgent.stream";
12598
13153
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
12599
13154
  const ownsSpan = !wrapperSpan;
12600
- const span = ownsSpan ? startSpan({
12601
- name: operationName,
12602
- spanAttributes: { type: "function" /* FUNCTION */ }
12603
- }) : wrapperSpan;
13155
+ const harnessTurnParent = currentHarnessTurnParent();
13156
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
13157
+ harnessTurnParent,
13158
+ withSpanInstrumentationName(
13159
+ {
13160
+ name: operationName,
13161
+ spanAttributes: { type: "function" /* FUNCTION */ }
13162
+ },
13163
+ INSTRUMENTATION_NAMES.AI_SDK
13164
+ )
13165
+ ) : startSpan(
13166
+ withSpanInstrumentationName(
13167
+ {
13168
+ name: operationName,
13169
+ spanAttributes: { type: "function" /* FUNCTION */ }
13170
+ },
13171
+ INSTRUMENTATION_NAMES.AI_SDK
13172
+ )
13173
+ ) : wrapperSpan;
12604
13174
  const operationKey = createOperationKey(event, operationName);
12605
13175
  registerOperation({
12606
13176
  callId: event.callId,
12607
13177
  hadModelChild: false,
13178
+ harnessTurnParent,
12608
13179
  loggedInput: false,
12609
13180
  operationName,
12610
13181
  operationKey,
@@ -12845,7 +13416,8 @@ function braintrustAISDKTelemetry() {
12845
13416
  toolCallId,
12846
13417
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
12847
13418
  }
12848
- }
13419
+ },
13420
+ state?.harnessTurnParent
12849
13421
  );
12850
13422
  toolSpans.set(toolCallId, { operationKey, span });
12851
13423
  });
@@ -13081,74 +13653,102 @@ function finishOutput(result, operationName) {
13081
13653
  }
13082
13654
 
13083
13655
  // src/instrumentation/plugins/ai-sdk-channels.ts
13084
- var aiSDKChannels = defineChannels("ai", {
13085
- generateText: channel({
13086
- channelName: "generateText",
13087
- kind: "async"
13088
- }),
13089
- streamText: channel({
13090
- channelName: "streamText",
13091
- kind: "async"
13092
- }),
13093
- streamTextSync: channel({
13094
- channelName: "streamText.sync",
13095
- kind: "sync-stream"
13096
- }),
13097
- generateObject: channel({
13098
- channelName: "generateObject",
13099
- kind: "async"
13100
- }),
13101
- streamObject: channel({
13102
- channelName: "streamObject",
13103
- kind: "async"
13104
- }),
13105
- streamObjectSync: channel({
13106
- channelName: "streamObject.sync",
13107
- kind: "sync-stream"
13108
- }),
13109
- embed: channel(
13110
- {
13656
+ var aiSDKChannels = defineChannels(
13657
+ "ai",
13658
+ {
13659
+ generateText: channel({
13660
+ channelName: "generateText",
13661
+ kind: "async"
13662
+ }),
13663
+ streamText: channel({
13664
+ channelName: "streamText",
13665
+ kind: "async"
13666
+ }),
13667
+ streamTextSync: channel({
13668
+ channelName: "streamText.sync",
13669
+ kind: "sync-stream"
13670
+ }),
13671
+ generateObject: channel({
13672
+ channelName: "generateObject",
13673
+ kind: "async"
13674
+ }),
13675
+ streamObject: channel({
13676
+ channelName: "streamObject",
13677
+ kind: "async"
13678
+ }),
13679
+ streamObjectSync: channel({
13680
+ channelName: "streamObject.sync",
13681
+ kind: "sync-stream"
13682
+ }),
13683
+ embed: channel({
13111
13684
  channelName: "embed",
13112
13685
  kind: "async"
13113
- }
13114
- ),
13115
- embedMany: channel({
13116
- channelName: "embedMany",
13117
- kind: "async"
13118
- }),
13119
- rerank: channel({
13120
- channelName: "rerank",
13121
- kind: "async"
13122
- }),
13123
- agentGenerate: channel({
13124
- channelName: "Agent.generate",
13125
- kind: "async"
13126
- }),
13127
- agentStream: channel({
13128
- channelName: "Agent.stream",
13129
- kind: "async"
13130
- }),
13131
- agentStreamSync: channel({
13132
- channelName: "Agent.stream.sync",
13133
- kind: "sync-stream"
13134
- }),
13135
- toolLoopAgentGenerate: channel({
13136
- channelName: "ToolLoopAgent.generate",
13137
- kind: "async"
13138
- }),
13139
- toolLoopAgentStream: channel({
13140
- channelName: "ToolLoopAgent.stream",
13141
- kind: "async"
13142
- }),
13143
- workflowAgentStream: channel({
13144
- channelName: "WorkflowAgent.stream",
13145
- kind: "async"
13146
- }),
13147
- v7CreateTelemetryDispatcher: channel({
13148
- channelName: "createTelemetryDispatcher",
13149
- kind: "sync-stream"
13150
- })
13151
- });
13686
+ }),
13687
+ embedMany: channel({
13688
+ channelName: "embedMany",
13689
+ kind: "async"
13690
+ }),
13691
+ rerank: channel({
13692
+ channelName: "rerank",
13693
+ kind: "async"
13694
+ }),
13695
+ agentGenerate: channel({
13696
+ channelName: "Agent.generate",
13697
+ kind: "async"
13698
+ }),
13699
+ agentStream: channel({
13700
+ channelName: "Agent.stream",
13701
+ kind: "async"
13702
+ }),
13703
+ agentStreamSync: channel({
13704
+ channelName: "Agent.stream.sync",
13705
+ kind: "sync-stream"
13706
+ }),
13707
+ toolLoopAgentGenerate: channel({
13708
+ channelName: "ToolLoopAgent.generate",
13709
+ kind: "async"
13710
+ }),
13711
+ toolLoopAgentStream: channel({
13712
+ channelName: "ToolLoopAgent.stream",
13713
+ kind: "async"
13714
+ }),
13715
+ workflowAgentStream: channel({
13716
+ channelName: "WorkflowAgent.stream",
13717
+ kind: "async"
13718
+ }),
13719
+ v7CreateTelemetryDispatcher: channel({
13720
+ channelName: "createTelemetryDispatcher",
13721
+ kind: "sync-stream"
13722
+ })
13723
+ },
13724
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
13725
+ );
13726
+ var harnessAgentChannels = defineChannels(
13727
+ "@ai-sdk/harness",
13728
+ {
13729
+ createSession: channel({
13730
+ channelName: "HarnessAgent.createSession",
13731
+ kind: "async"
13732
+ }),
13733
+ generate: channel({
13734
+ channelName: "HarnessAgent.generate",
13735
+ kind: "async"
13736
+ }),
13737
+ stream: channel({
13738
+ channelName: "HarnessAgent.stream",
13739
+ kind: "async"
13740
+ }),
13741
+ continueGenerate: channel({
13742
+ channelName: "HarnessAgent.continueGenerate",
13743
+ kind: "async"
13744
+ }),
13745
+ continueStream: channel({
13746
+ channelName: "HarnessAgent.continueStream",
13747
+ kind: "async"
13748
+ })
13749
+ },
13750
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
13751
+ );
13152
13752
 
13153
13753
  // src/instrumentation/plugins/ai-sdk-plugin.ts
13154
13754
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -13225,6 +13825,17 @@ var AISDKPlugin = class extends BasePlugin {
13225
13825
  subscribeToAISDK() {
13226
13826
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
13227
13827
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
13828
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
13829
+ this.unsubscribers.push(
13830
+ subscribeToHarnessContinuation(
13831
+ harnessAgentChannels.continueGenerate,
13832
+ denyOutputPaths
13833
+ ),
13834
+ subscribeToHarnessContinuation(
13835
+ harnessAgentChannels.continueStream,
13836
+ denyOutputPaths
13837
+ )
13838
+ );
13228
13839
  this.unsubscribers.push(
13229
13840
  traceStreamingChannel(aiSDKChannels.generateText, {
13230
13841
  name: "generateText",
@@ -13417,6 +14028,90 @@ var AISDKPlugin = class extends BasePlugin {
13417
14028
  })
13418
14029
  })
13419
14030
  );
14031
+ this.unsubscribers.push(
14032
+ traceStreamingChannel(harnessAgentChannels.generate, {
14033
+ name: "HarnessAgent.generate",
14034
+ startSpan: _internalStartSpanWithInitialMerge,
14035
+ type: "task" /* TASK */,
14036
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14037
+ extractOutput: (result, endEvent) => processAISDKOutput(
14038
+ result,
14039
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14040
+ ),
14041
+ extractMetrics: (result) => extractTokenMetrics(result),
14042
+ aggregateChunks: aggregateAISDKChunks
14043
+ })
14044
+ );
14045
+ this.unsubscribers.push(
14046
+ traceStreamingChannel(harnessAgentChannels.stream, {
14047
+ name: "HarnessAgent.stream",
14048
+ startSpan: _internalStartSpanWithInitialMerge,
14049
+ type: "task" /* TASK */,
14050
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14051
+ extractOutput: (result, endEvent) => processAISDKOutput(
14052
+ result,
14053
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14054
+ ),
14055
+ extractMetrics: (result, startTime) => ({
14056
+ ...extractTokenMetrics(result),
14057
+ ...startTime === void 0 ? {} : {
14058
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
14059
+ }
14060
+ }),
14061
+ aggregateChunks: aggregateAISDKChunks,
14062
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
14063
+ defaultDenyOutputPaths: denyOutputPaths,
14064
+ endEvent,
14065
+ result,
14066
+ resolvePromiseUsage: true,
14067
+ span,
14068
+ startTime
14069
+ })
14070
+ })
14071
+ );
14072
+ this.unsubscribers.push(
14073
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
14074
+ name: "HarnessAgent.continueGenerate",
14075
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
14076
+ startSpan: _internalStartSpanWithInitialMerge,
14077
+ type: "task" /* TASK */,
14078
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14079
+ extractOutput: (result, endEvent) => processAISDKOutput(
14080
+ result,
14081
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14082
+ ),
14083
+ extractMetrics: (result) => extractTokenMetrics(result),
14084
+ aggregateChunks: aggregateAISDKChunks
14085
+ })
14086
+ );
14087
+ this.unsubscribers.push(
14088
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
14089
+ name: "HarnessAgent.continueStream",
14090
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
14091
+ startSpan: _internalStartSpanWithInitialMerge,
14092
+ type: "task" /* TASK */,
14093
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
14094
+ extractOutput: (result, endEvent) => processAISDKOutput(
14095
+ result,
14096
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
14097
+ ),
14098
+ extractMetrics: (result, startTime) => ({
14099
+ ...extractTokenMetrics(result),
14100
+ ...startTime === void 0 ? {} : {
14101
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
14102
+ }
14103
+ }),
14104
+ aggregateChunks: aggregateAISDKChunks,
14105
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
14106
+ defaultDenyOutputPaths: denyOutputPaths,
14107
+ endEvent,
14108
+ result,
14109
+ resolvePromiseUsage: true,
14110
+ span,
14111
+ startTime
14112
+ })
14113
+ })
14114
+ );
13420
14115
  this.unsubscribers.push(
13421
14116
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
13422
14117
  name: "ToolLoopAgent.generate",
@@ -13486,15 +14181,164 @@ var AISDKPlugin = class extends BasePlugin {
13486
14181
  patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
13487
14182
  defaultDenyOutputPaths: denyOutputPaths,
13488
14183
  endEvent,
13489
- onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
13490
- result,
14184
+ onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
14185
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
14186
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
14187
+ result,
14188
+ span,
14189
+ startTime
14190
+ })
14191
+ })
14192
+ );
14193
+ }
14194
+ };
14195
+ function subscribeToHarnessAgentCreateSession() {
14196
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
14197
+ const parents = /* @__PURE__ */ new WeakMap();
14198
+ const handlers = {
14199
+ start: (event) => {
14200
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
14201
+ if (parent) {
14202
+ parents.set(event, parent);
14203
+ }
14204
+ },
14205
+ asyncEnd: (event) => {
14206
+ registerHarnessSessionParent(event.result, parents.get(event));
14207
+ parents.delete(event);
14208
+ },
14209
+ error: (event) => {
14210
+ parents.delete(event);
14211
+ }
14212
+ };
14213
+ channel2.subscribe(handlers);
14214
+ return () => channel2.unsubscribe(handlers);
14215
+ }
14216
+ function harnessContinuationParentFromEvent(event) {
14217
+ try {
14218
+ return harnessContinuationParent(event.arguments?.[0]?.session);
14219
+ } catch {
14220
+ return void 0;
14221
+ }
14222
+ }
14223
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
14224
+ const channel2 = continuationChannel.tracingChannel();
14225
+ const parents = /* @__PURE__ */ new WeakMap();
14226
+ const startTimes = /* @__PURE__ */ new WeakMap();
14227
+ const unbindParentStore = bindHarnessTurnParentToStart(
14228
+ channel2,
14229
+ harnessContinuationParentFromEvent
14230
+ );
14231
+ const handlers = {
14232
+ start: (event) => {
14233
+ const parent = harnessContinuationParentFromEvent(event);
14234
+ if (!parent) {
14235
+ return;
14236
+ }
14237
+ parents.set(event, parent);
14238
+ startTimes.set(event, getCurrentUnixTimestamp());
14239
+ try {
14240
+ const params = event.arguments?.[0];
14241
+ if (params) {
14242
+ prepareAISDKHarnessAgentInput(params, event.self);
14243
+ }
14244
+ } catch (error) {
14245
+ debugLogger.error(
14246
+ "Error preparing Harness continuation telemetry:",
14247
+ error
14248
+ );
14249
+ }
14250
+ },
14251
+ asyncEnd: (event) => {
14252
+ const parent = parents.get(event);
14253
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
14254
+ parents.delete(event);
14255
+ startTimes.delete(event);
14256
+ if (!parent) {
14257
+ return;
14258
+ }
14259
+ const endEvent = event;
14260
+ const span = {
14261
+ end: () => endHarnessTurn(parent),
14262
+ log: (update) => updateHarnessTurn(
14263
+ parent,
14264
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
14265
+ )
14266
+ };
14267
+ try {
14268
+ if (isAsyncIterable(endEvent.result)) {
14269
+ patchStreamIfNeeded(endEvent.result, {
14270
+ onComplete: (chunks) => {
14271
+ try {
14272
+ const { metadata, metrics, output } = aggregateAISDKChunks(
14273
+ chunks,
14274
+ endEvent.result,
14275
+ endEvent
14276
+ );
14277
+ span.log({
14278
+ ...metadata ? { metadata } : {},
14279
+ metrics,
14280
+ output
14281
+ });
14282
+ } catch (error) {
14283
+ debugLogger.error(
14284
+ "Error aggregating Harness continuation stream:",
14285
+ error
14286
+ );
14287
+ } finally {
14288
+ span.end();
14289
+ }
14290
+ },
14291
+ onError: (error) => {
14292
+ span.log({ error: toLoggedError(error), output: null });
14293
+ span.end();
14294
+ }
14295
+ });
14296
+ return;
14297
+ }
14298
+ if (patchAISDKStreamingResult({
14299
+ defaultDenyOutputPaths,
14300
+ endEvent,
14301
+ result: endEvent.result,
14302
+ resolvePromiseUsage: true,
13491
14303
  span,
13492
14304
  startTime
13493
- })
13494
- })
13495
- );
13496
- }
13497
- };
14305
+ })) {
14306
+ return;
14307
+ }
14308
+ finalizeAISDKChildTracing(endEvent);
14309
+ span.log({
14310
+ metrics: extractTokenMetrics(endEvent.result),
14311
+ output: processAISDKOutput(
14312
+ endEvent.result,
14313
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
14314
+ )
14315
+ });
14316
+ span.end();
14317
+ } catch (error) {
14318
+ debugLogger.error("Error tracing Harness continuation:", error);
14319
+ span.end();
14320
+ }
14321
+ },
14322
+ error: (event) => {
14323
+ const parent = parents.get(event);
14324
+ parents.delete(event);
14325
+ startTimes.delete(event);
14326
+ if (!parent) {
14327
+ return;
14328
+ }
14329
+ updateHarnessTurn(parent, {
14330
+ error: toLoggedError(event.error),
14331
+ output: null
14332
+ });
14333
+ endHarnessTurn(parent);
14334
+ }
14335
+ };
14336
+ channel2.subscribe(handlers);
14337
+ return () => {
14338
+ unbindParentStore();
14339
+ channel2.unsubscribe(handlers);
14340
+ };
14341
+ }
13498
14342
  function subscribeToAISDKV7TelemetryDispatcher() {
13499
14343
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
13500
14344
  const telemetry = braintrustAISDKTelemetry();
@@ -13947,6 +14791,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
13947
14791
  metadata
13948
14792
  };
13949
14793
  }
14794
+ function prepareAISDKHarnessAgentInput(params, self, span) {
14795
+ if (span) {
14796
+ registerHarnessTurnSpan({
14797
+ session: params.session,
14798
+ span
14799
+ });
14800
+ }
14801
+ if (isObject(self)) {
14802
+ try {
14803
+ if (isObject(self.settings)) {
14804
+ const settings = self.settings;
14805
+ if (settings.telemetry === void 0) {
14806
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
14807
+ }
14808
+ }
14809
+ } catch {
14810
+ }
14811
+ }
14812
+ const selectedInput = {};
14813
+ if (params.prompt !== void 0) {
14814
+ selectedInput.prompt = params.prompt;
14815
+ }
14816
+ if (params.messages !== void 0) {
14817
+ selectedInput.messages = params.messages;
14818
+ }
14819
+ if (params.toolApprovalContinuations !== void 0) {
14820
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
14821
+ }
14822
+ const metadata = extractMetadataFromCallParams({}, self);
14823
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
14824
+ metadata.sessionId = params.session.sessionId;
14825
+ }
14826
+ if (isObject(self)) {
14827
+ if (typeof self.harnessId === "string") {
14828
+ metadata.harnessId = self.harnessId;
14829
+ }
14830
+ if (typeof self.permissionMode === "string") {
14831
+ metadata.permissionMode = self.permissionMode;
14832
+ }
14833
+ }
14834
+ return {
14835
+ input: processAISDKCallInput(selectedInput).input,
14836
+ metadata
14837
+ };
14838
+ }
14839
+ function harnessSessionFromArguments(args) {
14840
+ const params = args[0];
14841
+ return isObject(params) ? params.session : void 0;
14842
+ }
13950
14843
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
13951
14844
  registerWorkflowAgentWrapperSpan(span);
13952
14845
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -14394,19 +15287,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14394
15287
  activeEntry.openSpans.delete(span);
14395
15288
  }
14396
15289
  },
14397
- {
14398
- name: "doGenerate",
14399
- spanAttributes: {
14400
- type: "llm" /* LLM */
15290
+ withSpanInstrumentationName(
15291
+ {
15292
+ name: "doGenerate",
15293
+ spanAttributes: {
15294
+ type: "llm" /* LLM */
15295
+ },
15296
+ event: buildAISDKModelStartEvent(
15297
+ callOptions,
15298
+ activeEntry.baseMetadata,
15299
+ {
15300
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
15301
+ }
15302
+ )
14401
15303
  },
14402
- event: buildAISDKModelStartEvent(
14403
- callOptions,
14404
- activeEntry.baseMetadata,
14405
- {
14406
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
14407
- }
14408
- )
14409
- }
15304
+ INSTRUMENTATION_NAMES.AI_SDK
15305
+ )
14410
15306
  );
14411
15307
  };
14412
15308
  if (originalDoStream) {
@@ -14416,19 +15312,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14416
15312
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
14417
15313
  }
14418
15314
  closeOpenAISDKModelPatchSpans(activeEntry);
14419
- const span = activeEntry.parentSpan.startSpan({
14420
- name: "doStream",
14421
- spanAttributes: {
14422
- type: "llm" /* LLM */
14423
- },
14424
- event: buildAISDKModelStartEvent(
14425
- callOptions,
14426
- activeEntry.baseMetadata,
15315
+ const span = activeEntry.parentSpan.startSpan(
15316
+ withSpanInstrumentationName(
14427
15317
  {
14428
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
14429
- }
15318
+ name: "doStream",
15319
+ spanAttributes: {
15320
+ type: "llm" /* LLM */
15321
+ },
15322
+ event: buildAISDKModelStartEvent(
15323
+ callOptions,
15324
+ activeEntry.baseMetadata,
15325
+ {
15326
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
15327
+ }
15328
+ )
15329
+ },
15330
+ INSTRUMENTATION_NAMES.AI_SDK
14430
15331
  )
14431
- });
15332
+ );
14432
15333
  activeEntry.openSpans.add(span);
14433
15334
  const streamStartTime = getCurrentUnixTimestamp();
14434
15335
  const result = await withCurrent(
@@ -14600,12 +15501,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14600
15501
  }
14601
15502
  if (isAsyncGenerator(result)) {
14602
15503
  return (async function* () {
14603
- const span = activeEntry.parentSpan.startSpan({
14604
- name: activeEntry.name,
14605
- spanAttributes: {
14606
- type: "tool" /* TOOL */
14607
- }
14608
- });
15504
+ const span = activeEntry.parentSpan.startSpan(
15505
+ withSpanInstrumentationName(
15506
+ {
15507
+ name: activeEntry.name,
15508
+ spanAttributes: {
15509
+ type: "tool" /* TOOL */
15510
+ }
15511
+ },
15512
+ INSTRUMENTATION_NAMES.AI_SDK
15513
+ )
15514
+ );
14609
15515
  span.log({ input: serializeToolExecutionInput(args) });
14610
15516
  try {
14611
15517
  let lastValue;
@@ -14629,12 +15535,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14629
15535
  span.log({ output: awaitedResult });
14630
15536
  return awaitedResult;
14631
15537
  },
14632
- {
14633
- name: activeEntry.name,
14634
- spanAttributes: {
14635
- type: "tool" /* TOOL */
14636
- }
14637
- }
15538
+ withSpanInstrumentationName(
15539
+ {
15540
+ name: activeEntry.name,
15541
+ spanAttributes: {
15542
+ type: "tool" /* TOOL */
15543
+ }
15544
+ },
15545
+ INSTRUMENTATION_NAMES.AI_SDK
15546
+ )
14638
15547
  );
14639
15548
  };
14640
15549
  cleanup.push(() => {
@@ -14793,7 +15702,10 @@ function patchAISDKStreamingResult(args) {
14793
15702
  defaultDenyOutputPaths,
14794
15703
  endEvent,
14795
15704
  onComplete,
15705
+ onCancel,
15706
+ onError,
14796
15707
  result,
15708
+ resolvePromiseUsage,
14797
15709
  span,
14798
15710
  startTime
14799
15711
  } = args;
@@ -14803,14 +15715,43 @@ function patchAISDKStreamingResult(args) {
14803
15715
  const resultRecord = result;
14804
15716
  attachKnownResultPromiseHandlers(resultRecord);
14805
15717
  let finalized = false;
14806
- const finalize = () => {
15718
+ const finalize = (outcome) => {
14807
15719
  if (finalized) {
14808
15720
  return;
14809
15721
  }
14810
15722
  finalized = true;
14811
- finalizeAISDKChildTracing(endEvent);
14812
- span.end();
14813
- onComplete?.();
15723
+ try {
15724
+ finalizeAISDKChildTracing(endEvent);
15725
+ } catch (error) {
15726
+ debugLogger.error("Error finalizing AI SDK child tracing:", error);
15727
+ }
15728
+ try {
15729
+ span.end();
15730
+ } catch (error) {
15731
+ debugLogger.error("Error ending AI SDK streaming span:", error);
15732
+ }
15733
+ if (outcome && "error" in outcome) {
15734
+ try {
15735
+ onError?.(outcome.error);
15736
+ } catch (error) {
15737
+ debugLogger.error("Error in AI SDK streaming error hook:", error);
15738
+ }
15739
+ } else if (outcome) {
15740
+ try {
15741
+ onComplete?.(outcome);
15742
+ } catch (error) {
15743
+ debugLogger.error("Error in AI SDK streaming completion hook:", error);
15744
+ }
15745
+ } else {
15746
+ try {
15747
+ onCancel?.();
15748
+ } catch (error) {
15749
+ debugLogger.error(
15750
+ "Error in AI SDK streaming cancellation hook:",
15751
+ error
15752
+ );
15753
+ }
15754
+ }
14814
15755
  };
14815
15756
  let outputLogged = false;
14816
15757
  const logStreamingOutput = async (firstChunkTime) => {
@@ -14820,6 +15761,22 @@ function patchAISDKStreamingResult(args) {
14820
15761
  outputLogged = true;
14821
15762
  try {
14822
15763
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
15764
+ if (resolvePromiseUsage) {
15765
+ try {
15766
+ const resultRecord2 = result;
15767
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
15768
+ if (isPromiseLike(pendingUsage)) {
15769
+ const usage = await Promise.resolve(pendingUsage);
15770
+ if (isObject(usage)) {
15771
+ Object.assign(
15772
+ metrics,
15773
+ extractTokenMetrics({ usage })
15774
+ );
15775
+ }
15776
+ }
15777
+ } catch {
15778
+ }
15779
+ }
14823
15780
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
14824
15781
  metrics.time_to_first_token = firstChunkTime - startTime;
14825
15782
  }
@@ -14828,15 +15785,29 @@ function patchAISDKStreamingResult(args) {
14828
15785
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
14829
15786
  );
14830
15787
  const metadata = buildResolvedMetadataPayload(result).metadata;
14831
- span.log({
14832
- output,
14833
- ...metadata ? { metadata } : {},
14834
- metrics
14835
- });
15788
+ try {
15789
+ span.log({
15790
+ output,
15791
+ ...metadata ? { metadata } : {},
15792
+ metrics
15793
+ });
15794
+ } catch (error) {
15795
+ debugLogger.error("Error logging AI SDK streaming output:", error);
15796
+ }
15797
+ finalize({ metrics, output });
14836
15798
  } catch (error) {
14837
- span.log({ error: toLoggedError(error) });
14838
- } finally {
14839
- finalize();
15799
+ const loggedError = toLoggedError(error);
15800
+ try {
15801
+ span.log({ error: loggedError });
15802
+ } catch (loggingError) {
15803
+ debugLogger.error(
15804
+ "Error logging AI SDK streaming failure:",
15805
+ loggingError
15806
+ );
15807
+ }
15808
+ finalize({
15809
+ error: error instanceof Error ? error : new Error(String(loggedError))
15810
+ });
14840
15811
  }
14841
15812
  };
14842
15813
  const createAsyncIterableHooks = () => {
@@ -14853,11 +15824,16 @@ function patchAISDKStreamingResult(args) {
14853
15824
  onError: (error) => {
14854
15825
  if (!outputLogged) {
14855
15826
  outputLogged = true;
14856
- span.log({
14857
- error: error.message
14858
- });
15827
+ try {
15828
+ span.log({ error });
15829
+ } catch (loggingError) {
15830
+ debugLogger.error(
15831
+ "Error logging AI SDK stream failure:",
15832
+ loggingError
15833
+ );
15834
+ }
14859
15835
  }
14860
- finalize();
15836
+ finalize({ error });
14861
15837
  },
14862
15838
  onCancel: finalize
14863
15839
  };
@@ -14943,8 +15919,18 @@ function patchAISDKStreamingResult(args) {
14943
15919
  }
14944
15920
  controller.enqueue(value);
14945
15921
  } catch (error) {
14946
- span.log({ error: toLoggedError(error) });
14947
- finalize();
15922
+ const loggedError = toLoggedError(error);
15923
+ try {
15924
+ span.log({ error: loggedError });
15925
+ } catch (loggingError) {
15926
+ debugLogger.error(
15927
+ "Error logging AI SDK base stream failure:",
15928
+ loggingError
15929
+ );
15930
+ }
15931
+ finalize({
15932
+ error: error instanceof Error ? error : new Error(String(loggedError))
15933
+ });
14948
15934
  controller.error(error);
14949
15935
  }
14950
15936
  },
@@ -15733,7 +16719,8 @@ var claudeAgentSDKChannels = defineChannels(
15733
16719
  channelName: "query",
15734
16720
  kind: "sync-stream"
15735
16721
  })
15736
- }
16722
+ },
16723
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
15737
16724
  );
15738
16725
 
15739
16726
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -15881,22 +16868,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
15881
16868
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
15882
16869
  const runWithResolvedParent = async () => {
15883
16870
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
15884
- const span = startSpan({
15885
- event: {
15886
- input: handlerArgs[0],
15887
- metadata: {
15888
- "claude_agent_sdk.raw_tool_name": rawToolName,
15889
- "gen_ai.tool.name": metadata.toolName,
15890
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
15891
- ...metadata.serverName && {
15892
- "mcp.server": metadata.serverName
15893
- }
15894
- }
15895
- },
15896
- name: spanName,
15897
- ...parent && { parent },
15898
- spanAttributes: { type: "tool" /* TOOL */ }
15899
- });
16871
+ const span = startSpan(
16872
+ withSpanInstrumentationName(
16873
+ {
16874
+ event: {
16875
+ input: handlerArgs[0],
16876
+ metadata: {
16877
+ "claude_agent_sdk.raw_tool_name": rawToolName,
16878
+ "gen_ai.tool.name": metadata.toolName,
16879
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
16880
+ ...metadata.serverName && {
16881
+ "mcp.server": metadata.serverName
16882
+ }
16883
+ }
16884
+ },
16885
+ name: spanName,
16886
+ ...parent && { parent },
16887
+ spanAttributes: { type: "tool" /* TOOL */ }
16888
+ },
16889
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
16890
+ )
16891
+ );
15900
16892
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
15901
16893
  const finalizeSuccess = (result) => {
15902
16894
  span.log({ output: result });
@@ -16221,14 +17213,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
16221
17213
  ).filter(
16222
17214
  (c) => c !== void 0
16223
17215
  );
16224
- const span = existingSpan ?? startSpan({
16225
- name: "anthropic.messages.create",
16226
- parent: parentSpan,
16227
- spanAttributes: {
16228
- type: "llm" /* LLM */
16229
- },
16230
- startTime
16231
- });
17216
+ const span = existingSpan ?? startSpan(
17217
+ withSpanInstrumentationName(
17218
+ {
17219
+ name: "anthropic.messages.create",
17220
+ parent: parentSpan,
17221
+ spanAttributes: {
17222
+ type: "llm" /* LLM */
17223
+ },
17224
+ startTime
17225
+ },
17226
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17227
+ )
17228
+ );
16232
17229
  span.log({
16233
17230
  input,
16234
17231
  metadata: model ? { model } : void 0,
@@ -16331,25 +17328,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
16331
17328
  return {};
16332
17329
  }
16333
17330
  const parsed = parseToolName(input.tool_name);
16334
- const toolSpan = startSpan({
16335
- event: {
16336
- input: input.tool_input,
16337
- metadata: {
16338
- "claude_agent_sdk.cwd": input.cwd,
16339
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
16340
- "claude_agent_sdk.session_id": input.session_id,
16341
- "gen_ai.tool.call.id": toolUseID,
16342
- "gen_ai.tool.name": parsed.toolName,
16343
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
16344
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
16345
- }
16346
- },
16347
- name: parsed.displayName,
16348
- parent: await resolveParentSpan(toolUseID, {
16349
- agentId: input.agent_id
16350
- }),
16351
- spanAttributes: { type: "tool" /* TOOL */ }
16352
- });
17331
+ const toolSpan = startSpan(
17332
+ withSpanInstrumentationName(
17333
+ {
17334
+ event: {
17335
+ input: input.tool_input,
17336
+ metadata: {
17337
+ "claude_agent_sdk.cwd": input.cwd,
17338
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
17339
+ "claude_agent_sdk.session_id": input.session_id,
17340
+ "gen_ai.tool.call.id": toolUseID,
17341
+ "gen_ai.tool.name": parsed.toolName,
17342
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
17343
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
17344
+ }
17345
+ },
17346
+ name: parsed.displayName,
17347
+ parent: await resolveParentSpan(toolUseID, {
17348
+ agentId: input.agent_id
17349
+ }),
17350
+ spanAttributes: { type: "tool" /* TOOL */ }
17351
+ },
17352
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17353
+ )
17354
+ );
16353
17355
  activeToolSpans.set(toolUseID, toolSpan);
16354
17356
  return {};
16355
17357
  };
@@ -16715,14 +17717,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
16715
17717
  const spanName = formatSubAgentSpanName(details);
16716
17718
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
16717
17719
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
16718
- const subAgentSpan = startSpan({
16719
- event: {
16720
- metadata: subAgentDetailsToMetadata(details)
16721
- },
16722
- name: spanName,
16723
- parent: parentSpan,
16724
- spanAttributes: { type: "task" /* TASK */ }
16725
- });
17720
+ const subAgentSpan = startSpan(
17721
+ withSpanInstrumentationName(
17722
+ {
17723
+ event: {
17724
+ metadata: subAgentDetailsToMetadata(details)
17725
+ },
17726
+ name: spanName,
17727
+ parent: parentSpan,
17728
+ spanAttributes: { type: "task" /* TASK */ }
17729
+ },
17730
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17731
+ )
17732
+ );
16726
17733
  subAgentSpans.set(parentToolUseId, subAgentSpan);
16727
17734
  return subAgentSpan;
16728
17735
  }
@@ -16743,14 +17750,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
16743
17750
  );
16744
17751
  llmParentSpan = await subAgentSpan.export();
16745
17752
  }
16746
- const llmSpan = startSpan({
16747
- name: "anthropic.messages.create",
16748
- parent: llmParentSpan,
16749
- spanAttributes: {
16750
- type: "llm" /* LLM */
16751
- },
16752
- startTime
16753
- });
17753
+ const llmSpan = startSpan(
17754
+ withSpanInstrumentationName(
17755
+ {
17756
+ name: "anthropic.messages.create",
17757
+ parent: llmParentSpan,
17758
+ spanAttributes: {
17759
+ type: "llm" /* LLM */
17760
+ },
17761
+ startTime
17762
+ },
17763
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
17764
+ )
17765
+ );
16754
17766
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
16755
17767
  return llmSpan;
16756
17768
  }
@@ -17019,12 +18031,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
17019
18031
  }
17020
18032
  })();
17021
18033
  }
17022
- const span = startSpan({
17023
- name: "Claude Agent",
17024
- spanAttributes: {
17025
- type: "task" /* TASK */
17026
- }
17027
- });
18034
+ const span = startSpan(
18035
+ withSpanInstrumentationName(
18036
+ {
18037
+ name: "Claude Agent",
18038
+ spanAttributes: {
18039
+ type: "task" /* TASK */
18040
+ }
18041
+ },
18042
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
18043
+ )
18044
+ );
17028
18045
  const startTime = getCurrentUnixTimestamp();
17029
18046
  try {
17030
18047
  span.log({
@@ -17186,27 +18203,31 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
17186
18203
  channel2.unsubscribe(handlers);
17187
18204
  });
17188
18205
  }
17189
- };
17190
-
17191
- // src/instrumentation/plugins/cursor-sdk-channels.ts
17192
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
17193
- create: channel({
17194
- channelName: "Agent.create",
17195
- kind: "async"
17196
- }),
17197
- resume: channel({
17198
- channelName: "Agent.resume",
17199
- kind: "async"
17200
- }),
17201
- prompt: channel({
17202
- channelName: "Agent.prompt",
17203
- kind: "async"
17204
- }),
17205
- send: channel({
17206
- channelName: "agent.send",
17207
- kind: "async"
17208
- })
17209
- });
18206
+ };
18207
+
18208
+ // src/instrumentation/plugins/cursor-sdk-channels.ts
18209
+ var cursorSDKChannels = defineChannels(
18210
+ "@cursor/sdk",
18211
+ {
18212
+ create: channel({
18213
+ channelName: "Agent.create",
18214
+ kind: "async"
18215
+ }),
18216
+ resume: channel({
18217
+ channelName: "Agent.resume",
18218
+ kind: "async"
18219
+ }),
18220
+ prompt: channel({
18221
+ channelName: "Agent.prompt",
18222
+ kind: "async"
18223
+ }),
18224
+ send: channel({
18225
+ channelName: "agent.send",
18226
+ kind: "async"
18227
+ })
18228
+ },
18229
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
18230
+ );
17210
18231
 
17211
18232
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
17212
18233
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -17258,10 +18279,15 @@ var CursorSDKPlugin = class extends BasePlugin {
17258
18279
  provider: "cursor",
17259
18280
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
17260
18281
  };
17261
- const span = startSpan({
17262
- name: "Cursor Agent",
17263
- spanAttributes: { type: "task" /* TASK */ }
17264
- });
18282
+ const span = startSpan(
18283
+ withSpanInstrumentationName(
18284
+ {
18285
+ name: "Cursor Agent",
18286
+ spanAttributes: { type: "task" /* TASK */ }
18287
+ },
18288
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18289
+ )
18290
+ );
17265
18291
  const startTime = getCurrentUnixTimestamp();
17266
18292
  safeLog2(span, {
17267
18293
  input: sanitizeUserMessage(message),
@@ -17323,10 +18349,15 @@ var CursorSDKPlugin = class extends BasePlugin {
17323
18349
  provider: "cursor",
17324
18350
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
17325
18351
  };
17326
- const span = startSpan({
17327
- name: "Cursor Agent",
17328
- spanAttributes: { type: "task" /* TASK */ }
17329
- });
18352
+ const span = startSpan(
18353
+ withSpanInstrumentationName(
18354
+ {
18355
+ name: "Cursor Agent",
18356
+ spanAttributes: { type: "task" /* TASK */ }
18357
+ },
18358
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18359
+ )
18360
+ );
17330
18361
  const startTime = getCurrentUnixTimestamp();
17331
18362
  safeLog2(span, {
17332
18363
  input: sanitizeUserMessage(message),
@@ -17773,29 +18804,39 @@ async function startToolSpan(state, args) {
17773
18804
  if (args.truncated?.result !== void 0) {
17774
18805
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
17775
18806
  }
17776
- const span = startSpan({
17777
- event: {
17778
- input: args.args,
17779
- metadata
17780
- },
17781
- name: `tool: ${name}`,
17782
- parent: await state.span.export(),
17783
- spanAttributes: { type: "tool" /* TOOL */ }
17784
- });
18807
+ const span = startSpan(
18808
+ withSpanInstrumentationName(
18809
+ {
18810
+ event: {
18811
+ input: args.args,
18812
+ metadata
18813
+ },
18814
+ name: `tool: ${name}`,
18815
+ parent: await state.span.export(),
18816
+ spanAttributes: { type: "tool" /* TOOL */ }
18817
+ },
18818
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18819
+ )
18820
+ );
17785
18821
  let subAgentSpan;
17786
18822
  if (isSubAgentToolName(name)) {
17787
- subAgentSpan = startSpan({
17788
- event: {
17789
- input: args.args,
17790
- metadata: {
17791
- "cursor_sdk.subagent.tool_call_id": args.callId,
17792
- "gen_ai.tool.name": name
17793
- }
17794
- },
17795
- name: formatSubAgentSpanName2(args.toolCall, args.args),
17796
- parent: await span.export(),
17797
- spanAttributes: { type: "task" /* TASK */ }
17798
- });
18823
+ subAgentSpan = startSpan(
18824
+ withSpanInstrumentationName(
18825
+ {
18826
+ event: {
18827
+ input: args.args,
18828
+ metadata: {
18829
+ "cursor_sdk.subagent.tool_call_id": args.callId,
18830
+ "gen_ai.tool.name": name
18831
+ }
18832
+ },
18833
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
18834
+ parent: await span.export(),
18835
+ spanAttributes: { type: "task" /* TASK */ }
18836
+ },
18837
+ INSTRUMENTATION_NAMES.CURSOR_SDK
18838
+ )
18839
+ );
17799
18840
  }
17800
18841
  return { span, subAgentSpan };
17801
18842
  }
@@ -18054,24 +19095,28 @@ function cleanMetrics2(metrics) {
18054
19095
  }
18055
19096
 
18056
19097
  // src/instrumentation/plugins/openai-agents-channels.ts
18057
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
18058
- onTraceStart: channel({
18059
- channelName: "tracing.processor.onTraceStart",
18060
- kind: "async"
18061
- }),
18062
- onTraceEnd: channel({
18063
- channelName: "tracing.processor.onTraceEnd",
18064
- kind: "async"
18065
- }),
18066
- onSpanStart: channel({
18067
- channelName: "tracing.processor.onSpanStart",
18068
- kind: "async"
18069
- }),
18070
- onSpanEnd: channel({
18071
- channelName: "tracing.processor.onSpanEnd",
18072
- kind: "async"
18073
- })
18074
- });
19098
+ var openAIAgentsCoreChannels = defineChannels(
19099
+ "@openai/agents-core",
19100
+ {
19101
+ onTraceStart: channel({
19102
+ channelName: "tracing.processor.onTraceStart",
19103
+ kind: "async"
19104
+ }),
19105
+ onTraceEnd: channel({
19106
+ channelName: "tracing.processor.onTraceEnd",
19107
+ kind: "async"
19108
+ }),
19109
+ onSpanStart: channel({
19110
+ channelName: "tracing.processor.onSpanStart",
19111
+ kind: "async"
19112
+ }),
19113
+ onSpanEnd: channel({
19114
+ channelName: "tracing.processor.onSpanEnd",
19115
+ kind: "async"
19116
+ })
19117
+ },
19118
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
19119
+ );
18075
19120
 
18076
19121
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
18077
19122
  function isSpanData(spanData, type) {
@@ -18187,16 +19232,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
18187
19232
  this.evictOldestTrace();
18188
19233
  }
18189
19234
  const current = currentSpan();
18190
- const span = current && current !== NOOP_SPAN ? current.startSpan({
18191
- name: trace.name,
18192
- type: "task" /* TASK */
18193
- }) : this.logger ? this.logger.startSpan({
18194
- name: trace.name,
18195
- type: "task" /* TASK */
18196
- }) : startSpan({
18197
- name: trace.name,
18198
- type: "task" /* TASK */
18199
- });
19235
+ const spanArgs = withSpanInstrumentationName(
19236
+ {
19237
+ name: trace.name,
19238
+ type: "task" /* TASK */
19239
+ },
19240
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
19241
+ );
19242
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
19243
+ withSpanInstrumentationName(
19244
+ spanArgs,
19245
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
19246
+ )
19247
+ );
18200
19248
  span.log({
18201
19249
  input: "Agent workflow started",
18202
19250
  metadata: {
@@ -18247,10 +19295,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
18247
19295
  if (!parentSpan) {
18248
19296
  return Promise.resolve();
18249
19297
  }
18250
- const childSpan = parentSpan.startSpan({
18251
- name: spanNameFromAgents(span),
18252
- type: spanTypeFromAgents(span)
18253
- });
19298
+ const childSpan = parentSpan.startSpan(
19299
+ withSpanInstrumentationName(
19300
+ {
19301
+ name: spanNameFromAgents(span),
19302
+ type: spanTypeFromAgents(span)
19303
+ },
19304
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
19305
+ )
19306
+ );
18254
19307
  traceData.childSpans.set(span.spanId, childSpan);
18255
19308
  return Promise.resolve();
18256
19309
  }
@@ -18515,24 +19568,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
18515
19568
  };
18516
19569
 
18517
19570
  // src/instrumentation/plugins/google-genai-channels.ts
18518
- var googleGenAIChannels = defineChannels("@google/genai", {
18519
- generateContent: channel({
18520
- channelName: "models.generateContent",
18521
- kind: "async"
18522
- }),
18523
- generateContentStream: channel({
18524
- channelName: "models.generateContentStream",
18525
- kind: "async"
18526
- }),
18527
- embedContent: channel({
18528
- channelName: "models.embedContent",
18529
- kind: "async"
18530
- }),
18531
- interactionsCreate: channel({
18532
- channelName: "interactions.create",
18533
- kind: "async"
18534
- })
18535
- });
19571
+ var googleGenAIChannels = defineChannels(
19572
+ "@google/genai",
19573
+ {
19574
+ generateContent: channel({
19575
+ channelName: "models.generateContent",
19576
+ kind: "async"
19577
+ }),
19578
+ generateContentStream: channel({
19579
+ channelName: "models.generateContentStream",
19580
+ kind: "async"
19581
+ }),
19582
+ embedContent: channel({
19583
+ channelName: "models.embedContent",
19584
+ kind: "async"
19585
+ }),
19586
+ interactionsCreate: channel({
19587
+ channelName: "interactions.create",
19588
+ kind: "async"
19589
+ })
19590
+ },
19591
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
19592
+ );
18536
19593
 
18537
19594
  // src/instrumentation/plugins/google-genai-plugin.ts
18538
19595
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -18570,13 +19627,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18570
19627
  const params = event.arguments[0];
18571
19628
  const input = serializeGenerateContentInput(params);
18572
19629
  const metadata = extractGenerateContentMetadata(params);
18573
- const span = startSpan({
18574
- name: "generate_content",
18575
- spanAttributes: {
18576
- type: "llm" /* LLM */
18577
- },
18578
- event: createWrapperParityEvent({ input, metadata })
18579
- });
19630
+ const span = startSpan(
19631
+ withSpanInstrumentationName(
19632
+ {
19633
+ name: "generate_content",
19634
+ spanAttributes: {
19635
+ type: "llm" /* LLM */
19636
+ },
19637
+ event: createWrapperParityEvent({ input, metadata })
19638
+ },
19639
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19640
+ )
19641
+ );
18580
19642
  return {
18581
19643
  span,
18582
19644
  startTime: getCurrentUnixTimestamp()
@@ -18589,13 +19651,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18589
19651
  const params = event.arguments[0];
18590
19652
  const input = serializeGenerateContentInput(params);
18591
19653
  const metadata = extractGenerateContentMetadata(params);
18592
- const span = startSpan({
18593
- name: "generate_content",
18594
- spanAttributes: {
18595
- type: "llm" /* LLM */
18596
- },
18597
- event: createWrapperParityEvent({ input, metadata })
18598
- });
19654
+ const span = startSpan(
19655
+ withSpanInstrumentationName(
19656
+ {
19657
+ name: "generate_content",
19658
+ spanAttributes: {
19659
+ type: "llm" /* LLM */
19660
+ },
19661
+ event: createWrapperParityEvent({ input, metadata })
19662
+ },
19663
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19664
+ )
19665
+ );
18599
19666
  return {
18600
19667
  span,
18601
19668
  startTime: getCurrentUnixTimestamp()
@@ -18671,13 +19738,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18671
19738
  const params = event.arguments[0];
18672
19739
  const input = serializeEmbedContentInput(params);
18673
19740
  const metadata = extractEmbedContentMetadata(params);
18674
- const span = startSpan({
18675
- name: "embed_content",
18676
- spanAttributes: {
18677
- type: "llm" /* LLM */
18678
- },
18679
- event: createWrapperParityEvent({ input, metadata })
18680
- });
19741
+ const span = startSpan(
19742
+ withSpanInstrumentationName(
19743
+ {
19744
+ name: "embed_content",
19745
+ spanAttributes: {
19746
+ type: "llm" /* LLM */
19747
+ },
19748
+ event: createWrapperParityEvent({ input, metadata })
19749
+ },
19750
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19751
+ )
19752
+ );
18681
19753
  return {
18682
19754
  span,
18683
19755
  startTime: getCurrentUnixTimestamp()
@@ -18690,13 +19762,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
18690
19762
  const params = event.arguments[0];
18691
19763
  const input = serializeEmbedContentInput(params);
18692
19764
  const metadata = extractEmbedContentMetadata(params);
18693
- const span = startSpan({
18694
- name: "embed_content",
18695
- spanAttributes: {
18696
- type: "llm" /* LLM */
18697
- },
18698
- event: createWrapperParityEvent({ input, metadata })
18699
- });
19765
+ const span = startSpan(
19766
+ withSpanInstrumentationName(
19767
+ {
19768
+ name: "embed_content",
19769
+ spanAttributes: {
19770
+ type: "llm" /* LLM */
19771
+ },
19772
+ event: createWrapperParityEvent({ input, metadata })
19773
+ },
19774
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19775
+ )
19776
+ );
18700
19777
  return {
18701
19778
  span,
18702
19779
  startTime: getCurrentUnixTimestamp()
@@ -18807,16 +19884,21 @@ function patchGoogleGenAIStreamingResult(args) {
18807
19884
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
18808
19885
  const ensureSpan = () => {
18809
19886
  if (!span) {
18810
- span = startSpan({
18811
- name: "generate_content_stream",
18812
- spanAttributes: {
18813
- type: "llm" /* LLM */
18814
- },
18815
- event: {
18816
- input,
18817
- metadata
18818
- }
18819
- });
19887
+ span = startSpan(
19888
+ withSpanInstrumentationName(
19889
+ {
19890
+ name: "generate_content_stream",
19891
+ spanAttributes: {
19892
+ type: "llm" /* LLM */
19893
+ },
19894
+ event: {
19895
+ input,
19896
+ metadata
19897
+ }
19898
+ },
19899
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
19900
+ )
19901
+ );
18820
19902
  }
18821
19903
  return span;
18822
19904
  };
@@ -19584,28 +20666,32 @@ function tryToDict(obj) {
19584
20666
  }
19585
20667
 
19586
20668
  // src/instrumentation/plugins/huggingface-channels.ts
19587
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
19588
- chatCompletion: channel({
19589
- channelName: "chatCompletion",
19590
- kind: "async"
19591
- }),
19592
- chatCompletionStream: channel({
19593
- channelName: "chatCompletionStream",
19594
- kind: "sync-stream"
19595
- }),
19596
- textGeneration: channel({
19597
- channelName: "textGeneration",
19598
- kind: "async"
19599
- }),
19600
- textGenerationStream: channel({
19601
- channelName: "textGenerationStream",
19602
- kind: "sync-stream"
19603
- }),
19604
- featureExtraction: channel({
19605
- channelName: "featureExtraction",
19606
- kind: "async"
19607
- })
19608
- });
20669
+ var huggingFaceChannels = defineChannels(
20670
+ "@huggingface/inference",
20671
+ {
20672
+ chatCompletion: channel({
20673
+ channelName: "chatCompletion",
20674
+ kind: "async"
20675
+ }),
20676
+ chatCompletionStream: channel({
20677
+ channelName: "chatCompletionStream",
20678
+ kind: "sync-stream"
20679
+ }),
20680
+ textGeneration: channel({
20681
+ channelName: "textGeneration",
20682
+ kind: "async"
20683
+ }),
20684
+ textGenerationStream: channel({
20685
+ channelName: "textGenerationStream",
20686
+ kind: "sync-stream"
20687
+ }),
20688
+ featureExtraction: channel({
20689
+ channelName: "featureExtraction",
20690
+ kind: "async"
20691
+ })
20692
+ },
20693
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
20694
+ );
19609
20695
 
19610
20696
  // src/instrumentation/plugins/huggingface-plugin.ts
19611
20697
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -20032,20 +21118,24 @@ function extractTextGenerationStreamMetadata(chunks) {
20032
21118
  }
20033
21119
 
20034
21120
  // src/instrumentation/plugins/openrouter-agent-channels.ts
20035
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
20036
- callModel: channel({
20037
- channelName: "callModel",
20038
- kind: "sync-stream"
20039
- }),
20040
- callModelTurn: channel({
20041
- channelName: "callModel.turn",
20042
- kind: "async"
20043
- }),
20044
- toolExecute: channel({
20045
- channelName: "tool.execute",
20046
- kind: "async"
20047
- })
20048
- });
21121
+ var openRouterAgentChannels = defineChannels(
21122
+ "@openrouter/agent",
21123
+ {
21124
+ callModel: channel({
21125
+ channelName: "callModel",
21126
+ kind: "sync-stream"
21127
+ }),
21128
+ callModelTurn: channel({
21129
+ channelName: "callModel.turn",
21130
+ kind: "async"
21131
+ }),
21132
+ toolExecute: channel({
21133
+ channelName: "tool.execute",
21134
+ kind: "async"
21135
+ })
21136
+ },
21137
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
21138
+ );
20049
21139
 
20050
21140
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
20051
21141
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -20815,38 +21905,40 @@ function normalizeError(error) {
20815
21905
  }
20816
21906
 
20817
21907
  // src/instrumentation/plugins/openrouter-channels.ts
20818
- var openRouterChannels = defineChannels("@openrouter/sdk", {
20819
- chatSend: channel({
20820
- channelName: "chat.send",
20821
- kind: "async"
20822
- }),
20823
- embeddingsGenerate: channel({
20824
- channelName: "embeddings.generate",
20825
- kind: "async"
20826
- }),
20827
- rerankRerank: channel(
20828
- {
21908
+ var openRouterChannels = defineChannels(
21909
+ "@openrouter/sdk",
21910
+ {
21911
+ chatSend: channel({
21912
+ channelName: "chat.send",
21913
+ kind: "async"
21914
+ }),
21915
+ embeddingsGenerate: channel({
21916
+ channelName: "embeddings.generate",
21917
+ kind: "async"
21918
+ }),
21919
+ rerankRerank: channel({
20829
21920
  channelName: "rerank.rerank",
20830
21921
  kind: "async"
20831
- }
20832
- ),
20833
- betaResponsesSend: channel({
20834
- channelName: "beta.responses.send",
20835
- kind: "async"
20836
- }),
20837
- callModel: channel({
20838
- channelName: "callModel",
20839
- kind: "sync-stream"
20840
- }),
20841
- callModelTurn: channel({
20842
- channelName: "callModel.turn",
20843
- kind: "async"
20844
- }),
20845
- toolExecute: channel({
20846
- channelName: "tool.execute",
20847
- kind: "async"
20848
- })
20849
- });
21922
+ }),
21923
+ betaResponsesSend: channel({
21924
+ channelName: "beta.responses.send",
21925
+ kind: "async"
21926
+ }),
21927
+ callModel: channel({
21928
+ channelName: "callModel",
21929
+ kind: "sync-stream"
21930
+ }),
21931
+ callModelTurn: channel({
21932
+ channelName: "callModel.turn",
21933
+ kind: "async"
21934
+ }),
21935
+ toolExecute: channel({
21936
+ channelName: "tool.execute",
21937
+ kind: "async"
21938
+ })
21939
+ },
21940
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
21941
+ );
20850
21942
 
20851
21943
  // src/instrumentation/plugins/openrouter-plugin.ts
20852
21944
  var OpenRouterPlugin = class extends BasePlugin {
@@ -21886,52 +22978,56 @@ function normalizeError2(error) {
21886
22978
  }
21887
22979
 
21888
22980
  // src/instrumentation/plugins/mistral-channels.ts
21889
- var mistralChannels = defineChannels("@mistralai/mistralai", {
21890
- chatComplete: channel({
21891
- channelName: "chat.complete",
21892
- kind: "async"
21893
- }),
21894
- chatStream: channel({
21895
- channelName: "chat.stream",
21896
- kind: "async"
21897
- }),
21898
- embeddingsCreate: channel({
21899
- channelName: "embeddings.create",
21900
- kind: "async"
21901
- }),
21902
- classifiersModerate: channel({
21903
- channelName: "classifiers.moderate",
21904
- kind: "async"
21905
- }),
21906
- classifiersModerateChat: channel({
21907
- channelName: "classifiers.moderateChat",
21908
- kind: "async"
21909
- }),
21910
- classifiersClassify: channel({
21911
- channelName: "classifiers.classify",
21912
- kind: "async"
21913
- }),
21914
- classifiersClassifyChat: channel({
21915
- channelName: "classifiers.classifyChat",
21916
- kind: "async"
21917
- }),
21918
- fimComplete: channel({
21919
- channelName: "fim.complete",
21920
- kind: "async"
21921
- }),
21922
- fimStream: channel({
21923
- channelName: "fim.stream",
21924
- kind: "async"
21925
- }),
21926
- agentsComplete: channel({
21927
- channelName: "agents.complete",
21928
- kind: "async"
21929
- }),
21930
- agentsStream: channel({
21931
- channelName: "agents.stream",
21932
- kind: "async"
21933
- })
21934
- });
22981
+ var mistralChannels = defineChannels(
22982
+ "@mistralai/mistralai",
22983
+ {
22984
+ chatComplete: channel({
22985
+ channelName: "chat.complete",
22986
+ kind: "async"
22987
+ }),
22988
+ chatStream: channel({
22989
+ channelName: "chat.stream",
22990
+ kind: "async"
22991
+ }),
22992
+ embeddingsCreate: channel({
22993
+ channelName: "embeddings.create",
22994
+ kind: "async"
22995
+ }),
22996
+ classifiersModerate: channel({
22997
+ channelName: "classifiers.moderate",
22998
+ kind: "async"
22999
+ }),
23000
+ classifiersModerateChat: channel({
23001
+ channelName: "classifiers.moderateChat",
23002
+ kind: "async"
23003
+ }),
23004
+ classifiersClassify: channel({
23005
+ channelName: "classifiers.classify",
23006
+ kind: "async"
23007
+ }),
23008
+ classifiersClassifyChat: channel({
23009
+ channelName: "classifiers.classifyChat",
23010
+ kind: "async"
23011
+ }),
23012
+ fimComplete: channel({
23013
+ channelName: "fim.complete",
23014
+ kind: "async"
23015
+ }),
23016
+ fimStream: channel({
23017
+ channelName: "fim.stream",
23018
+ kind: "async"
23019
+ }),
23020
+ agentsComplete: channel({
23021
+ channelName: "agents.complete",
23022
+ kind: "async"
23023
+ }),
23024
+ agentsStream: channel({
23025
+ channelName: "agents.stream",
23026
+ kind: "async"
23027
+ })
23028
+ },
23029
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
23030
+ );
21935
23031
 
21936
23032
  // src/instrumentation/plugins/mistral-plugin.ts
21937
23033
  var MistralPlugin = class extends BasePlugin {
@@ -22551,20 +23647,24 @@ function aggregateMistralStreamChunks(chunks) {
22551
23647
  }
22552
23648
 
22553
23649
  // src/instrumentation/plugins/google-adk-channels.ts
22554
- var googleADKChannels = defineChannels("@google/adk", {
22555
- runnerRunAsync: channel({
22556
- channelName: "runner.runAsync",
22557
- kind: "sync-stream"
22558
- }),
22559
- agentRunAsync: channel({
22560
- channelName: "agent.runAsync",
22561
- kind: "sync-stream"
22562
- }),
22563
- toolRunAsync: channel({
22564
- channelName: "tool.runAsync",
22565
- kind: "async"
22566
- })
22567
- });
23650
+ var googleADKChannels = defineChannels(
23651
+ "@google/adk",
23652
+ {
23653
+ runnerRunAsync: channel({
23654
+ channelName: "runner.runAsync",
23655
+ kind: "sync-stream"
23656
+ }),
23657
+ agentRunAsync: channel({
23658
+ channelName: "agent.runAsync",
23659
+ kind: "sync-stream"
23660
+ }),
23661
+ toolRunAsync: channel({
23662
+ channelName: "tool.runAsync",
23663
+ kind: "async"
23664
+ })
23665
+ },
23666
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
23667
+ );
22568
23668
 
22569
23669
  // src/instrumentation/plugins/google-adk-plugin.ts
22570
23670
  var GoogleADKPlugin = class extends BasePlugin {
@@ -22589,12 +23689,17 @@ var GoogleADKPlugin = class extends BasePlugin {
22589
23689
  const createState = (event) => {
22590
23690
  const params = event.arguments[0] ?? {};
22591
23691
  const contextKey = extractRunnerContextKey(params);
22592
- const span = startSpan({
22593
- name: "Google ADK Runner",
22594
- spanAttributes: {
22595
- type: "task" /* TASK */
22596
- }
22597
- });
23692
+ const span = startSpan(
23693
+ withSpanInstrumentationName(
23694
+ {
23695
+ name: "Google ADK Runner",
23696
+ spanAttributes: {
23697
+ type: "task" /* TASK */
23698
+ }
23699
+ },
23700
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
23701
+ )
23702
+ );
22598
23703
  const startTime = getCurrentUnixTimestamp();
22599
23704
  try {
22600
23705
  const metadata = extractRunnerMetadata(params);
@@ -22680,18 +23785,23 @@ var GoogleADKPlugin = class extends BasePlugin {
22680
23785
  this.activeRunnerSpans
22681
23786
  );
22682
23787
  const contextKey = extractInvocationContextKey(parentContext);
22683
- const span = startSpan({
22684
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
22685
- spanAttributes: {
22686
- type: "task" /* TASK */
22687
- },
22688
- ...runnerParentSpan ? {
22689
- parentSpanIds: {
22690
- spanId: runnerParentSpan.spanId,
22691
- rootSpanId: runnerParentSpan.rootSpanId
22692
- }
22693
- } : {}
22694
- });
23788
+ const span = startSpan(
23789
+ withSpanInstrumentationName(
23790
+ {
23791
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
23792
+ spanAttributes: {
23793
+ type: "task" /* TASK */
23794
+ },
23795
+ ...runnerParentSpan ? {
23796
+ parentSpanIds: {
23797
+ spanId: runnerParentSpan.spanId,
23798
+ rootSpanId: runnerParentSpan.rootSpanId
23799
+ }
23800
+ } : {}
23801
+ },
23802
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
23803
+ )
23804
+ );
22695
23805
  const startTime = getCurrentUnixTimestamp();
22696
23806
  try {
22697
23807
  const metadata = {
@@ -22784,22 +23894,27 @@ var GoogleADKPlugin = class extends BasePlugin {
22784
23894
  this.activeAgentSpans,
22785
23895
  this.activeRunnerSpans
22786
23896
  );
22787
- const createSpan = () => startSpan({
22788
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
22789
- spanAttributes: {
22790
- type: "tool" /* TOOL */
22791
- },
22792
- event: {
22793
- input: req.args,
22794
- metadata: {
22795
- provider: "google-adk",
22796
- ...toolName && { "google_adk.tool_name": toolName },
22797
- ...extractToolCallId(req) && {
22798
- "google_adk.tool_call_id": extractToolCallId(req)
23897
+ const createSpan = () => startSpan(
23898
+ withSpanInstrumentationName(
23899
+ {
23900
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
23901
+ spanAttributes: {
23902
+ type: "tool" /* TOOL */
23903
+ },
23904
+ event: {
23905
+ input: req.args,
23906
+ metadata: {
23907
+ provider: "google-adk",
23908
+ ...toolName && { "google_adk.tool_name": toolName },
23909
+ ...extractToolCallId(req) && {
23910
+ "google_adk.tool_call_id": extractToolCallId(req)
23911
+ }
23912
+ }
22799
23913
  }
22800
- }
22801
- }
22802
- });
23914
+ },
23915
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
23916
+ )
23917
+ );
22803
23918
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
22804
23919
  const startTime = getCurrentUnixTimestamp();
22805
23920
  states.set(event, { span, startTime });
@@ -23198,24 +24313,28 @@ function cleanMetrics4(metrics) {
23198
24313
  }
23199
24314
 
23200
24315
  // src/instrumentation/plugins/cohere-channels.ts
23201
- var cohereChannels = defineChannels("cohere-ai", {
23202
- chat: channel({
23203
- channelName: "chat",
23204
- kind: "async"
23205
- }),
23206
- chatStream: channel({
23207
- channelName: "chatStream",
23208
- kind: "async"
23209
- }),
23210
- embed: channel({
23211
- channelName: "embed",
23212
- kind: "async"
23213
- }),
23214
- rerank: channel({
23215
- channelName: "rerank",
23216
- kind: "async"
23217
- })
23218
- });
24316
+ var cohereChannels = defineChannels(
24317
+ "cohere-ai",
24318
+ {
24319
+ chat: channel({
24320
+ channelName: "chat",
24321
+ kind: "async"
24322
+ }),
24323
+ chatStream: channel({
24324
+ channelName: "chatStream",
24325
+ kind: "async"
24326
+ }),
24327
+ embed: channel({
24328
+ channelName: "embed",
24329
+ kind: "async"
24330
+ }),
24331
+ rerank: channel({
24332
+ channelName: "rerank",
24333
+ kind: "async"
24334
+ })
24335
+ },
24336
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
24337
+ );
23219
24338
 
23220
24339
  // src/instrumentation/plugins/cohere-plugin.ts
23221
24340
  var CoherePlugin = class extends BasePlugin {
@@ -23884,18 +25003,20 @@ function aggregateCohereChatStreamChunks(chunks) {
23884
25003
  }
23885
25004
 
23886
25005
  // src/instrumentation/plugins/groq-channels.ts
23887
- var groqChannels = defineChannels("groq-sdk", {
23888
- chatCompletionsCreate: channel({
23889
- channelName: "chat.completions.create",
23890
- kind: "async"
23891
- }),
23892
- embeddingsCreate: channel(
23893
- {
25006
+ var groqChannels = defineChannels(
25007
+ "groq-sdk",
25008
+ {
25009
+ chatCompletionsCreate: channel({
25010
+ channelName: "chat.completions.create",
25011
+ kind: "async"
25012
+ }),
25013
+ embeddingsCreate: channel({
23894
25014
  channelName: "embeddings.create",
23895
25015
  kind: "async"
23896
- }
23897
- )
23898
- });
25016
+ })
25017
+ },
25018
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
25019
+ );
23899
25020
 
23900
25021
  // src/instrumentation/plugins/groq-plugin.ts
23901
25022
  var GroqPlugin = class extends BasePlugin {
@@ -23998,15 +25119,27 @@ var clientSendChannel = channel({
23998
25119
  channelName: "client.send",
23999
25120
  kind: "async"
24000
25121
  });
24001
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
24002
- clientSend: clientSendChannel
24003
- });
24004
- var smithyCoreChannels = defineChannels("@smithy/core", {
24005
- clientSend: clientSendChannel
24006
- });
24007
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
24008
- clientSend: clientSendChannel
24009
- });
25122
+ var bedrockRuntimeChannels = defineChannels(
25123
+ "aws-bedrock-runtime",
25124
+ {
25125
+ clientSend: clientSendChannel
25126
+ },
25127
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
25128
+ );
25129
+ var smithyCoreChannels = defineChannels(
25130
+ "@smithy/core",
25131
+ {
25132
+ clientSend: clientSendChannel
25133
+ },
25134
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
25135
+ );
25136
+ var smithyClientChannels = defineChannels(
25137
+ "@smithy/smithy-client",
25138
+ {
25139
+ clientSend: clientSendChannel
25140
+ },
25141
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
25142
+ );
24010
25143
 
24011
25144
  // src/instrumentation/plugins/bedrock-runtime-common.ts
24012
25145
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -24640,38 +25773,46 @@ function extractTextFromJsonLike(value) {
24640
25773
  }
24641
25774
 
24642
25775
  // src/instrumentation/plugins/genkit-channels.ts
24643
- var genkitChannels = defineChannels("@genkit-ai/ai", {
24644
- generate: channel({
24645
- channelName: "generate",
24646
- kind: "async"
24647
- }),
24648
- generateStream: channel({
24649
- channelName: "generateStream",
24650
- kind: "sync-stream"
24651
- }),
24652
- embed: channel({
24653
- channelName: "embed",
24654
- kind: "async"
24655
- }),
24656
- embedMany: channel({
24657
- channelName: "embedMany",
24658
- kind: "async"
24659
- }),
24660
- actionRun: channel({
24661
- channelName: "action.run",
24662
- kind: "async"
24663
- }),
24664
- actionStream: channel({
24665
- channelName: "action.stream",
24666
- kind: "sync-stream"
24667
- })
24668
- });
24669
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
24670
- actionSpan: channel({
24671
- channelName: "action.span",
24672
- kind: "async"
24673
- })
24674
- });
25776
+ var genkitChannels = defineChannels(
25777
+ "@genkit-ai/ai",
25778
+ {
25779
+ generate: channel({
25780
+ channelName: "generate",
25781
+ kind: "async"
25782
+ }),
25783
+ generateStream: channel({
25784
+ channelName: "generateStream",
25785
+ kind: "sync-stream"
25786
+ }),
25787
+ embed: channel({
25788
+ channelName: "embed",
25789
+ kind: "async"
25790
+ }),
25791
+ embedMany: channel({
25792
+ channelName: "embedMany",
25793
+ kind: "async"
25794
+ }),
25795
+ actionRun: channel({
25796
+ channelName: "action.run",
25797
+ kind: "async"
25798
+ }),
25799
+ actionStream: channel({
25800
+ channelName: "action.stream",
25801
+ kind: "sync-stream"
25802
+ })
25803
+ },
25804
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
25805
+ );
25806
+ var genkitCoreChannels = defineChannels(
25807
+ "@genkit-ai/core",
25808
+ {
25809
+ actionSpan: channel({
25810
+ channelName: "action.span",
25811
+ kind: "async"
25812
+ })
25813
+ },
25814
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
25815
+ );
24675
25816
 
24676
25817
  // src/instrumentation/plugins/genkit-plugin.ts
24677
25818
  var GenkitPlugin = class extends BasePlugin {
@@ -24856,12 +25997,17 @@ function startActionSpanState(args) {
24856
25997
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
24857
25998
  return void 0;
24858
25999
  }
24859
- const span = startSpan({
24860
- name: actionSpanName(args.metadata, args.runStepName),
24861
- spanAttributes: {
24862
- type: actionSpanType(args.metadata)
24863
- }
24864
- });
26000
+ const span = startSpan(
26001
+ withSpanInstrumentationName(
26002
+ {
26003
+ name: actionSpanName(args.metadata, args.runStepName),
26004
+ spanAttributes: {
26005
+ type: actionSpanType(args.metadata)
26006
+ }
26007
+ },
26008
+ INSTRUMENTATION_NAMES.GENKIT
26009
+ )
26010
+ );
24865
26011
  const startTime = getCurrentUnixTimestamp();
24866
26012
  span.log({
24867
26013
  input: args.input,
@@ -25222,20 +26368,24 @@ function stringValue(value) {
25222
26368
  }
25223
26369
 
25224
26370
  // src/instrumentation/plugins/github-copilot-channels.ts
25225
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
25226
- createSession: channel({
25227
- channelName: "client.createSession",
25228
- kind: "async"
25229
- }),
25230
- resumeSession: channel({
25231
- channelName: "client.resumeSession",
25232
- kind: "async"
25233
- }),
25234
- sendAndWait: channel({
25235
- channelName: "session.sendAndWait",
25236
- kind: "async"
25237
- })
25238
- });
26371
+ var gitHubCopilotChannels = defineChannels(
26372
+ "@github/copilot-sdk",
26373
+ {
26374
+ createSession: channel({
26375
+ channelName: "client.createSession",
26376
+ kind: "async"
26377
+ }),
26378
+ resumeSession: channel({
26379
+ channelName: "client.resumeSession",
26380
+ kind: "async"
26381
+ }),
26382
+ sendAndWait: channel({
26383
+ channelName: "session.sendAndWait",
26384
+ kind: "async"
26385
+ })
26386
+ },
26387
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
26388
+ );
25239
26389
 
25240
26390
  // src/instrumentation/plugins/github-copilot-plugin.ts
25241
26391
  var ROOT_AGENT_KEY = "__root__";
@@ -25326,11 +26476,16 @@ async function handleTurnStart(state, agentId) {
25326
26476
  return;
25327
26477
  }
25328
26478
  const parentId = await getParentIdForAgent(state, agentId);
25329
- const span = startSpan({
25330
- name: "Copilot Turn",
25331
- parent: parentId,
25332
- spanAttributes: { type: "task" /* TASK */ }
25333
- });
26479
+ const span = startSpan(
26480
+ withSpanInstrumentationName(
26481
+ {
26482
+ name: "Copilot Turn",
26483
+ parent: parentId,
26484
+ spanAttributes: { type: "task" /* TASK */ }
26485
+ },
26486
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26487
+ )
26488
+ );
25334
26489
  const pendingUserMessage = state.pendingUserMessages.get(key);
25335
26490
  if (pendingUserMessage) {
25336
26491
  span.log({ input: pendingUserMessage });
@@ -25361,11 +26516,16 @@ async function handleUsage(state, agentId, usage) {
25361
26516
  const parentId = turn ? await turn.id : await state.session.id;
25362
26517
  const content = state.currentMessageContent.get(key);
25363
26518
  const { metrics, metadata } = extractMetricsFromUsage(usage);
25364
- const llmSpan = startSpan({
25365
- name: "github.copilot.llm",
25366
- parent: parentId,
25367
- spanAttributes: { type: "llm" /* LLM */ }
25368
- });
26519
+ const llmSpan = startSpan(
26520
+ withSpanInstrumentationName(
26521
+ {
26522
+ name: "github.copilot.llm",
26523
+ parent: parentId,
26524
+ spanAttributes: { type: "llm" /* LLM */ }
26525
+ },
26526
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26527
+ )
26528
+ );
25369
26529
  llmSpan.log({
25370
26530
  output: content ?? void 0,
25371
26531
  metadata,
@@ -25391,11 +26551,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
25391
26551
  }
25392
26552
  const parentId = await getToolParentId(state, agentId);
25393
26553
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
25394
- const span = startSpan({
25395
- name: displayName,
25396
- parent: parentId,
25397
- spanAttributes: { type: "tool" /* TOOL */ }
25398
- });
26554
+ const span = startSpan(
26555
+ withSpanInstrumentationName(
26556
+ {
26557
+ name: displayName,
26558
+ parent: parentId,
26559
+ spanAttributes: { type: "tool" /* TOOL */ }
26560
+ },
26561
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26562
+ )
26563
+ );
25399
26564
  const metadata = {
25400
26565
  "gen_ai.tool.name": toolName,
25401
26566
  "gen_ai.tool.call.id": toolCallId
@@ -25431,11 +26596,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
25431
26596
  }
25432
26597
  const tool = state.activeTools.get(toolCallId);
25433
26598
  const parentId = tool ? await tool.id : await state.session.id;
25434
- const span = startSpan({
25435
- name: `Agent: ${agentDisplayName}`,
25436
- parent: parentId,
25437
- spanAttributes: { type: "task" /* TASK */ }
25438
- });
26599
+ const span = startSpan(
26600
+ withSpanInstrumentationName(
26601
+ {
26602
+ name: `Agent: ${agentDisplayName}`,
26603
+ parent: parentId,
26604
+ spanAttributes: { type: "task" /* TASK */ }
26605
+ },
26606
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26607
+ )
26608
+ );
25439
26609
  span.log({
25440
26610
  metadata: {
25441
26611
  "github_copilot.agent_name": agentDisplayName,
@@ -25661,10 +26831,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
25661
26831
  if (!config || typeof config !== "object") {
25662
26832
  return;
25663
26833
  }
25664
- const sessionSpan = startSpan({
25665
- name: "Copilot Session",
25666
- spanAttributes: { type: "task" /* TASK */ }
25667
- });
26834
+ const sessionSpan = startSpan(
26835
+ withSpanInstrumentationName(
26836
+ {
26837
+ name: "Copilot Session",
26838
+ spanAttributes: { type: "task" /* TASK */ }
26839
+ },
26840
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
26841
+ )
26842
+ );
25668
26843
  const metadata = {};
25669
26844
  if (config.model) {
25670
26845
  metadata["github_copilot.model"] = config.model;
@@ -25752,12 +26927,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
25752
26927
  };
25753
26928
 
25754
26929
  // src/instrumentation/plugins/flue-channels.ts
25755
- var flueChannels = defineChannels("@flue/runtime", {
25756
- createContext: channel({
25757
- channelName: "createFlueContext",
25758
- kind: "sync-stream"
25759
- })
25760
- });
26930
+ var flueChannels = defineChannels(
26931
+ "@flue/runtime",
26932
+ {
26933
+ createContext: channel({
26934
+ channelName: "createFlueContext",
26935
+ kind: "sync-stream"
26936
+ })
26937
+ },
26938
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
26939
+ );
25761
26940
 
25762
26941
  // src/instrumentation/plugins/flue-plugin.ts
25763
26942
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -26100,15 +27279,20 @@ var FlueObserveBridge = class {
26100
27279
  safeLog3(existing.span, { input, metadata });
26101
27280
  return;
26102
27281
  }
26103
- const span = startSpan({
26104
- name: `workflow:${workflowName}`,
26105
- spanAttributes: { type: "task" /* TASK */ },
26106
- startTime: eventTime(event.startedAt ?? event.timestamp),
26107
- event: {
26108
- input,
26109
- metadata
26110
- }
26111
- });
27282
+ const span = startSpan(
27283
+ withSpanInstrumentationName(
27284
+ {
27285
+ name: `workflow:${workflowName}`,
27286
+ spanAttributes: { type: "task" /* TASK */ },
27287
+ startTime: eventTime(event.startedAt ?? event.timestamp),
27288
+ event: {
27289
+ input,
27290
+ metadata
27291
+ }
27292
+ },
27293
+ INSTRUMENTATION_NAMES.FLUE
27294
+ )
27295
+ );
26112
27296
  this.runsById.set(event.runId, { metadata, span });
26113
27297
  }
26114
27298
  handleRunResume(event, ctx) {
@@ -26128,12 +27312,17 @@ var FlueObserveBridge = class {
26128
27312
  safeLog3(existing.span, { metadata });
26129
27313
  return;
26130
27314
  }
26131
- const span = startSpan({
26132
- name: `workflow:${workflowName}`,
26133
- spanAttributes: { type: "task" /* TASK */ },
26134
- startTime: eventTime(event.startedAt ?? event.timestamp),
26135
- event: { metadata }
26136
- });
27315
+ const span = startSpan(
27316
+ withSpanInstrumentationName(
27317
+ {
27318
+ name: `workflow:${workflowName}`,
27319
+ spanAttributes: { type: "task" /* TASK */ },
27320
+ startTime: eventTime(event.startedAt ?? event.timestamp),
27321
+ event: { metadata }
27322
+ },
27323
+ INSTRUMENTATION_NAMES.FLUE
27324
+ )
27325
+ );
26137
27326
  this.runsById.set(event.runId, { metadata, span });
26138
27327
  }
26139
27328
  handleRunEnd(event) {
@@ -26772,7 +27961,14 @@ function stateMatchesRun(state, runId) {
26772
27961
  return state.metadata["flue.run_id"] === runId;
26773
27962
  }
26774
27963
  function startFlueSpan(parent, args) {
26775
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
27964
+ return parent ? withCurrent(
27965
+ parent,
27966
+ () => startSpan(
27967
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
27968
+ )
27969
+ ) : startSpan(
27970
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
27971
+ );
26776
27972
  }
26777
27973
  function runWithCurrentSpanStore(span, next) {
26778
27974
  const state = _internalGetGlobalState();
@@ -26862,9 +28058,13 @@ var BraintrustLangChainCallbackHandler = class {
26862
28058
  ...this.options.debug ? { runId, parentRunId } : {}
26863
28059
  }
26864
28060
  };
26865
- let span = parentSpan.startSpan(args);
28061
+ const spanArgs = withSpanInstrumentationName(
28062
+ args,
28063
+ INSTRUMENTATION_NAMES.LANGCHAIN
28064
+ );
28065
+ let span = parentSpan.startSpan(spanArgs);
26866
28066
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
26867
- span = initLogger().startSpan(args);
28067
+ span = initLogger().startSpan(spanArgs);
26868
28068
  }
26869
28069
  this.spans.set(runId, span);
26870
28070
  }
@@ -27077,6 +28277,16 @@ function cleanObject(obj) {
27077
28277
  })
27078
28278
  );
27079
28279
  }
28280
+ function normalizeTokenMetrics(obj) {
28281
+ const metrics = cleanObject(obj);
28282
+ if (metrics.total_tokens !== void 0) {
28283
+ metrics.tokens = metrics.total_tokens;
28284
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
28285
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
28286
+ }
28287
+ delete metrics.total_tokens;
28288
+ return metrics;
28289
+ }
27080
28290
  function walkGenerations(response) {
27081
28291
  const result = [];
27082
28292
  const generations = response.generations || [];
@@ -27123,7 +28333,7 @@ function getMetricsFromResponse(response) {
27123
28333
  continue;
27124
28334
  }
27125
28335
  const inputTokenDetails = usageMetadata.input_token_details;
27126
- return cleanObject({
28336
+ return normalizeTokenMetrics({
27127
28337
  total_tokens: usageMetadata.total_tokens,
27128
28338
  prompt_tokens: usageMetadata.input_tokens,
27129
28339
  completion_tokens: usageMetadata.output_tokens,
@@ -27133,7 +28343,7 @@ function getMetricsFromResponse(response) {
27133
28343
  }
27134
28344
  const llmOutput = response.llmOutput || {};
27135
28345
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
27136
- return cleanObject({
28346
+ return normalizeTokenMetrics({
27137
28347
  total_tokens: tokenUsage.totalTokens,
27138
28348
  prompt_tokens: tokenUsage.promptTokens,
27139
28349
  completion_tokens: tokenUsage.completionTokens
@@ -27151,16 +28361,20 @@ function isRecord(value) {
27151
28361
  }
27152
28362
 
27153
28363
  // src/instrumentation/plugins/langchain-channels.ts
27154
- var langChainChannels = defineChannels("@langchain/core", {
27155
- configure: channel({
27156
- channelName: "CallbackManager.configure",
27157
- kind: "sync-stream"
27158
- }),
27159
- configureSync: channel({
27160
- channelName: "CallbackManager._configureSync",
27161
- kind: "sync-stream"
27162
- })
27163
- });
28364
+ var langChainChannels = defineChannels(
28365
+ "@langchain/core",
28366
+ {
28367
+ configure: channel({
28368
+ channelName: "CallbackManager.configure",
28369
+ kind: "sync-stream"
28370
+ }),
28371
+ configureSync: channel({
28372
+ channelName: "CallbackManager._configureSync",
28373
+ kind: "sync-stream"
28374
+ })
28375
+ },
28376
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
28377
+ );
27164
28378
 
27165
28379
  // src/instrumentation/plugins/langchain-plugin.ts
27166
28380
  var LangChainPlugin = class extends BasePlugin {
@@ -27248,20 +28462,24 @@ function isBraintrustHandler(handler) {
27248
28462
  }
27249
28463
 
27250
28464
  // src/instrumentation/plugins/langsmith-channels.ts
27251
- var langSmithChannels = defineChannels("langsmith", {
27252
- createRun: channel({
27253
- channelName: "Client.createRun",
27254
- kind: "async"
27255
- }),
27256
- updateRun: channel({
27257
- channelName: "Client.updateRun",
27258
- kind: "async"
27259
- }),
27260
- batchIngestRuns: channel({
27261
- channelName: "Client.batchIngestRuns",
27262
- kind: "async"
27263
- })
27264
- });
28465
+ var langSmithChannels = defineChannels(
28466
+ "langsmith",
28467
+ {
28468
+ createRun: channel({
28469
+ channelName: "Client.createRun",
28470
+ kind: "async"
28471
+ }),
28472
+ updateRun: channel({
28473
+ channelName: "Client.updateRun",
28474
+ kind: "async"
28475
+ }),
28476
+ batchIngestRuns: channel({
28477
+ channelName: "Client.batchIngestRuns",
28478
+ kind: "async"
28479
+ })
28480
+ },
28481
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
28482
+ );
27265
28483
 
27266
28484
  // src/instrumentation/plugins/langsmith-plugin.ts
27267
28485
  var MAX_COMPLETED_RUNS = 1e4;
@@ -27425,19 +28643,24 @@ var LangSmithPlugin = class extends BasePlugin {
27425
28643
  const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
27426
28644
  const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
27427
28645
  const startTime = timestampSeconds(ownValue(run, "start_time"));
27428
- return startSpan({
27429
- name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
27430
- spanId: id,
27431
- parentSpanIds: {
27432
- parentSpanIds: parentId ? [parentId] : [],
27433
- rootSpanId: traceId
27434
- },
27435
- spanAttributes: {
27436
- type: mapRunType(ownValue(run, "run_type"))
27437
- },
27438
- ...startTime === void 0 ? {} : { startTime },
27439
- event: { id }
27440
- });
28646
+ return startSpan(
28647
+ withSpanInstrumentationName(
28648
+ {
28649
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
28650
+ spanId: id,
28651
+ parentSpanIds: {
28652
+ parentSpanIds: parentId ? [parentId] : [],
28653
+ rootSpanId: traceId
28654
+ },
28655
+ spanAttributes: {
28656
+ type: mapRunType(ownValue(run, "run_type"))
28657
+ },
28658
+ ...startTime === void 0 ? {} : { startTime },
28659
+ event: { id }
28660
+ },
28661
+ INSTRUMENTATION_NAMES.LANGSMITH
28662
+ )
28663
+ );
27441
28664
  }
27442
28665
  logRun(span, run, previous, includeOutput) {
27443
28666
  const inputs = preferOwnValue(run, previous, "inputs");
@@ -27695,7 +28918,8 @@ var piCodingAgentChannels = defineChannels(
27695
28918
  channelName: "AgentSession.prompt",
27696
28919
  kind: "async"
27697
28920
  })
27698
- }
28921
+ },
28922
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
27699
28923
  );
27700
28924
 
27701
28925
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -27779,14 +29003,19 @@ function startPiPromptRun(event, onFinalize) {
27779
29003
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
27780
29004
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
27781
29005
  };
27782
- const span = startSpan({
27783
- event: {
27784
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
27785
- metadata
27786
- },
27787
- name: "AgentSession.prompt",
27788
- spanAttributes: { type: "task" /* TASK */ }
27789
- });
29006
+ const span = startSpan(
29007
+ withSpanInstrumentationName(
29008
+ {
29009
+ event: {
29010
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
29011
+ metadata
29012
+ },
29013
+ name: "AgentSession.prompt",
29014
+ spanAttributes: { type: "task" /* TASK */ }
29015
+ },
29016
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
29017
+ )
29018
+ );
27790
29019
  const streamPatchState = installPiStreamPatch(agent);
27791
29020
  const options = event.arguments[1];
27792
29021
  const promptText = event.arguments[0];
@@ -27956,15 +29185,20 @@ async function startPiLlmSpan(state, model, context, options) {
27956
29185
  ...extractToolMetadata(context.tools),
27957
29186
  "pi_coding_agent.operation": "agent.streamFn"
27958
29187
  };
27959
- const span = startSpan({
27960
- event: {
27961
- input: processInputAttachments(normalizePiContextInput(context)),
27962
- metadata
27963
- },
27964
- name: getLlmSpanName(model),
27965
- parent: await state.span.export(),
27966
- spanAttributes: { type: "llm" /* LLM */ }
27967
- });
29188
+ const span = startSpan(
29189
+ withSpanInstrumentationName(
29190
+ {
29191
+ event: {
29192
+ input: processInputAttachments(normalizePiContextInput(context)),
29193
+ metadata
29194
+ },
29195
+ name: getLlmSpanName(model),
29196
+ parent: await state.span.export(),
29197
+ spanAttributes: { type: "llm" /* LLM */ }
29198
+ },
29199
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
29200
+ )
29201
+ );
27968
29202
  const llmState = {
27969
29203
  finalized: false,
27970
29204
  metadata,
@@ -28128,19 +29362,24 @@ async function startPiToolSpan(state, event) {
28128
29362
  const restoreAutoInstrumentation = enterAutoInstrumentationAllowed();
28129
29363
  const metadata = {
28130
29364
  "gen_ai.tool.call.id": event.toolCallId,
28131
- "gen_ai.tool.name": event.toolName,
28132
- "pi_coding_agent.tool.name": event.toolName
28133
- };
28134
- try {
28135
- const span = startSpan({
28136
- event: {
28137
- input: event.args,
28138
- metadata
28139
- },
28140
- name: event.toolName || "tool",
28141
- parent: await state.span.export(),
28142
- spanAttributes: { type: "tool" /* TOOL */ }
28143
- });
29365
+ "gen_ai.tool.name": event.toolName,
29366
+ "pi_coding_agent.tool.name": event.toolName
29367
+ };
29368
+ try {
29369
+ const span = startSpan(
29370
+ withSpanInstrumentationName(
29371
+ {
29372
+ event: {
29373
+ input: event.args,
29374
+ metadata
29375
+ },
29376
+ name: event.toolName || "tool",
29377
+ parent: await state.span.export(),
29378
+ spanAttributes: { type: "tool" /* TOOL */ }
29379
+ },
29380
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
29381
+ )
29382
+ );
28144
29383
  state.activeToolSpans.set(event.toolCallId, {
28145
29384
  restoreAutoInstrumentation,
28146
29385
  span
@@ -28572,20 +29811,24 @@ function logInstrumentationError4(context, error) {
28572
29811
  }
28573
29812
 
28574
29813
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
28575
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
28576
- agentStream: channel({
28577
- channelName: "Agent.stream",
28578
- kind: "sync-stream"
28579
- }),
28580
- graphStream: channel({
28581
- channelName: "Graph.stream",
28582
- kind: "sync-stream"
28583
- }),
28584
- swarmStream: channel({
28585
- channelName: "Swarm.stream",
28586
- kind: "sync-stream"
28587
- })
28588
- });
29814
+ var strandsAgentSDKChannels = defineChannels(
29815
+ "@strands-agents/sdk",
29816
+ {
29817
+ agentStream: channel({
29818
+ channelName: "Agent.stream",
29819
+ kind: "sync-stream"
29820
+ }),
29821
+ graphStream: channel({
29822
+ channelName: "Graph.stream",
29823
+ kind: "sync-stream"
29824
+ }),
29825
+ swarmStream: channel({
29826
+ channelName: "Swarm.stream",
29827
+ kind: "sync-stream"
29828
+ })
29829
+ },
29830
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
29831
+ );
28589
29832
 
28590
29833
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
28591
29834
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -28740,22 +29983,32 @@ function startAgentStream(event, activeChildParents) {
28740
29983
  );
28741
29984
  const span = parentSpan ? withCurrent(
28742
29985
  parentSpan,
28743
- () => startSpan({
28744
- event: {
28745
- input,
28746
- metadata
29986
+ () => startSpan(
29987
+ withSpanInstrumentationName(
29988
+ {
29989
+ event: {
29990
+ input,
29991
+ metadata
29992
+ },
29993
+ name: formatAgentSpanName(agent),
29994
+ spanAttributes: { type: "task" /* TASK */ }
29995
+ },
29996
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
29997
+ )
29998
+ )
29999
+ ) : startSpan(
30000
+ withSpanInstrumentationName(
30001
+ {
30002
+ event: {
30003
+ input,
30004
+ metadata
30005
+ },
30006
+ name: formatAgentSpanName(agent),
30007
+ spanAttributes: { type: "task" /* TASK */ }
28747
30008
  },
28748
- name: formatAgentSpanName(agent),
28749
- spanAttributes: { type: "task" /* TASK */ }
28750
- })
28751
- ) : startSpan({
28752
- event: {
28753
- input,
28754
- metadata
28755
- },
28756
- name: formatAgentSpanName(agent),
28757
- spanAttributes: { type: "task" /* TASK */ }
28758
- });
30009
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30010
+ )
30011
+ );
28759
30012
  return {
28760
30013
  activeTools: /* @__PURE__ */ new Map(),
28761
30014
  attachmentCache,
@@ -28777,22 +30030,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
28777
30030
  const input = processStrandsInputAttachments(event.arguments[0]);
28778
30031
  const span = parentSpan ? withCurrent(
28779
30032
  parentSpan,
28780
- () => startSpan({
28781
- event: {
28782
- input,
28783
- metadata
30033
+ () => startSpan(
30034
+ withSpanInstrumentationName(
30035
+ {
30036
+ event: {
30037
+ input,
30038
+ metadata
30039
+ },
30040
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30041
+ spanAttributes: { type: "task" /* TASK */ }
30042
+ },
30043
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30044
+ )
30045
+ )
30046
+ ) : startSpan(
30047
+ withSpanInstrumentationName(
30048
+ {
30049
+ event: {
30050
+ input,
30051
+ metadata
30052
+ },
30053
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30054
+ spanAttributes: { type: "task" /* TASK */ }
28784
30055
  },
28785
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28786
- spanAttributes: { type: "task" /* TASK */ }
28787
- })
28788
- ) : startSpan({
28789
- event: {
28790
- input,
28791
- metadata
28792
- },
28793
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28794
- spanAttributes: { type: "task" /* TASK */ }
28795
- });
30056
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30057
+ )
30058
+ );
28796
30059
  return {
28797
30060
  activeNodes: /* @__PURE__ */ new Map(),
28798
30061
  finalized: false,
@@ -28894,17 +30157,22 @@ function startModelSpan(state, event) {
28894
30157
  };
28895
30158
  const span = withCurrent(
28896
30159
  state.span,
28897
- () => startSpan({
28898
- event: {
28899
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
28900
- event.agent.messages,
28901
- state.attachmentCache
28902
- ) : void 0,
28903
- metadata
28904
- },
28905
- name: formatModelSpanName(model),
28906
- spanAttributes: { type: "llm" /* LLM */ }
28907
- })
30160
+ () => startSpan(
30161
+ withSpanInstrumentationName(
30162
+ {
30163
+ event: {
30164
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
30165
+ event.agent.messages,
30166
+ state.attachmentCache
30167
+ ) : void 0,
30168
+ metadata
30169
+ },
30170
+ name: formatModelSpanName(model),
30171
+ spanAttributes: { type: "llm" /* LLM */ }
30172
+ },
30173
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30174
+ )
30175
+ )
28908
30176
  );
28909
30177
  state.activeModel = {
28910
30178
  metadata,
@@ -28956,20 +30224,25 @@ function startToolSpan2(state, event) {
28956
30224
  const name = extractToolName3(toolUse, event.tool);
28957
30225
  const span = withCurrent(
28958
30226
  state.span,
28959
- () => startSpan({
28960
- event: {
28961
- input: toolUse?.input,
28962
- metadata: {
28963
- "gen_ai.tool.call.id": toolUse?.toolUseId,
28964
- "gen_ai.tool.name": name,
28965
- "strands.operation": "tool.call",
28966
- "strands.tool.name": name,
28967
- provider: "strands"
28968
- }
28969
- },
28970
- name: `tool: ${name}`,
28971
- spanAttributes: { type: "tool" /* TOOL */ }
28972
- })
30227
+ () => startSpan(
30228
+ withSpanInstrumentationName(
30229
+ {
30230
+ event: {
30231
+ input: toolUse?.input,
30232
+ metadata: {
30233
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
30234
+ "gen_ai.tool.name": name,
30235
+ "strands.operation": "tool.call",
30236
+ "strands.tool.name": name,
30237
+ provider: "strands"
30238
+ }
30239
+ },
30240
+ name: `tool: ${name}`,
30241
+ spanAttributes: { type: "tool" /* TOOL */ }
30242
+ },
30243
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30244
+ )
30245
+ )
28973
30246
  );
28974
30247
  state.activeTools.set(key, {
28975
30248
  span,
@@ -29040,11 +30313,16 @@ function startNodeSpan(state, event, activeChildParents) {
29040
30313
  };
29041
30314
  const span = withCurrent(
29042
30315
  state.span,
29043
- () => startSpan({
29044
- event: { metadata },
29045
- name: `node: ${nodeId}`,
29046
- spanAttributes: { type: "task" /* TASK */ }
29047
- })
30316
+ () => startSpan(
30317
+ withSpanInstrumentationName(
30318
+ {
30319
+ event: { metadata },
30320
+ name: `node: ${nodeId}`,
30321
+ spanAttributes: { type: "task" /* TASK */ }
30322
+ },
30323
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
30324
+ )
30325
+ )
29048
30326
  );
29049
30327
  const nodeState = {
29050
30328
  ...child ? { child } : {},
@@ -29753,6 +31031,7 @@ var BraintrustPlugin = class extends BasePlugin {
29753
31031
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
29754
31032
  var MAX_EVE_CACHE_ENTRIES = 1e4;
29755
31033
  var MAX_STORED_LLM_INPUTS = 100;
31034
+ var MAX_STORED_REASONING_BLOCKS = 100;
29756
31035
  var MAX_STORED_SPAN_REFERENCES = 1e4;
29757
31036
  var MAX_STORED_STEP_STARTS = 1e4;
29758
31037
  function braintrustEveHook(options) {
@@ -29791,6 +31070,7 @@ var ResumedEveSpan = class {
29791
31070
  this.reference = reference;
29792
31071
  this.endTime = reference.endTime;
29793
31072
  }
31073
+ reference;
29794
31074
  endTime;
29795
31075
  get rootSpanId() {
29796
31076
  return this.reference.rootSpanId;
@@ -29823,6 +31103,7 @@ var EveBridge = class {
29823
31103
  constructor(state) {
29824
31104
  this.state = state;
29825
31105
  }
31106
+ state;
29826
31107
  eventQueuesBySession = /* @__PURE__ */ new Map();
29827
31108
  completedToolKeys = new LRUCache({
29828
31109
  max: MAX_EVE_CACHE_ENTRIES
@@ -29855,7 +31136,12 @@ var EveBridge = class {
29855
31136
  };
29856
31137
  const span = withCurrent(
29857
31138
  NOOP_SPAN,
29858
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
31139
+ () => _internalStartSpanWithInitialMerge(
31140
+ withSpanInstrumentationName(
31141
+ { ...args, startTime },
31142
+ INSTRUMENTATION_NAMES.EVE
31143
+ )
31144
+ )
29859
31145
  );
29860
31146
  if (typeof rowId !== "string") {
29861
31147
  return span;
@@ -30001,6 +31287,9 @@ var EveBridge = class {
30001
31287
  case "step.started":
30002
31288
  await this.handleStepStarted(event, ctx, hookMetadata);
30003
31289
  return true;
31290
+ case "reasoning.completed":
31291
+ this.handleReasoningCompleted(event, ctx);
31292
+ return true;
30004
31293
  case "message.completed":
30005
31294
  this.handleMessageCompleted(event, ctx);
30006
31295
  return true;
@@ -30092,6 +31381,7 @@ var EveBridge = class {
30092
31381
  key,
30093
31382
  metadata,
30094
31383
  metrics: {},
31384
+ sessionId,
30095
31385
  span,
30096
31386
  stepsByIndex: /* @__PURE__ */ new Map(),
30097
31387
  turnId: event.data.turnId
@@ -30122,6 +31412,12 @@ var EveBridge = class {
30122
31412
  const endTime = eventTime2(event);
30123
31413
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
30124
31414
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
31415
+ clearStoredEveReasoning(
31416
+ this.state,
31417
+ sessionId,
31418
+ event.data.turnId,
31419
+ event.data.stepIndex
31420
+ );
30125
31421
  }
30126
31422
  const stepOrdinal = this.stepOrdinal(event);
30127
31423
  const metadata = { ...turn.metadata };
@@ -30131,6 +31427,13 @@ var EveBridge = class {
30131
31427
  event.data.turnId,
30132
31428
  event.data.stepIndex
30133
31429
  );
31430
+ const reasoning = readStoredEveReasoning(
31431
+ this.state,
31432
+ sessionId,
31433
+ event.data.turnId,
31434
+ event.data.stepIndex
31435
+ );
31436
+ const output = mergeEveReasoning(void 0, reasoning);
30134
31437
  const { rowId: eventId, spanId } = await generateEveIds(
30135
31438
  "step",
30136
31439
  sessionId,
@@ -30148,32 +31451,52 @@ var EveBridge = class {
30148
31451
  spanId,
30149
31452
  startTime: eventTime2(event)
30150
31453
  });
30151
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
31454
+ span.log({
31455
+ ...input !== void 0 ? { input } : {},
31456
+ metadata
31457
+ });
30152
31458
  turn.stepsByIndex.set(event.data.stepIndex, {
30153
31459
  ...input !== void 0 ? { input } : {},
30154
31460
  metadata,
30155
31461
  metrics: {},
31462
+ ...output !== void 0 ? { output } : {},
31463
+ reasoning,
30156
31464
  span
30157
31465
  });
30158
31466
  }
31467
+ handleReasoningCompleted(event, ctx) {
31468
+ const sessionId = sessionIdFromContext(ctx);
31469
+ if (!sessionId) {
31470
+ return;
31471
+ }
31472
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
31473
+ const step = this.stepForEvent(event, ctx);
31474
+ if (step) {
31475
+ step.reasoning = reasoning;
31476
+ step.output = mergeEveReasoning(step.output, reasoning);
31477
+ }
31478
+ }
30159
31479
  handleMessageCompleted(event, ctx) {
30160
31480
  const step = this.stepForEvent(event, ctx);
30161
31481
  if (!step) {
30162
31482
  return;
30163
31483
  }
30164
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
31484
+ const existingMessage = eveOutputMessage(step.output);
30165
31485
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
30166
- step.output = [
30167
- {
30168
- finish_reason: normalizedFinishReason(event.data.finishReason),
30169
- index: 0,
30170
- message: {
30171
- content: event.data.message,
30172
- role: "assistant",
30173
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
31486
+ step.output = mergeEveReasoning(
31487
+ [
31488
+ {
31489
+ finish_reason: normalizedFinishReason(event.data.finishReason),
31490
+ index: 0,
31491
+ message: {
31492
+ content: event.data.message,
31493
+ role: "assistant",
31494
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
31495
+ }
30174
31496
  }
30175
- }
30176
- ];
31497
+ ],
31498
+ step.reasoning
31499
+ );
30177
31500
  const turn = this.turnForEvent(event, ctx);
30178
31501
  if (turn && event.data.finishReason !== "tool-calls") {
30179
31502
  turn.output = event.data.message;
@@ -30182,16 +31505,19 @@ var EveBridge = class {
30182
31505
  handleResultCompleted(event, ctx) {
30183
31506
  const step = this.stepForEvent(event, ctx);
30184
31507
  if (step) {
30185
- step.output = [
30186
- {
30187
- finish_reason: "stop",
30188
- index: 0,
30189
- message: {
30190
- content: event.data.result,
30191
- role: "assistant"
31508
+ step.output = mergeEveReasoning(
31509
+ [
31510
+ {
31511
+ finish_reason: "stop",
31512
+ index: 0,
31513
+ message: {
31514
+ content: event.data.result,
31515
+ role: "assistant"
31516
+ }
30192
31517
  }
30193
- }
30194
- ];
31518
+ ],
31519
+ step.reasoning
31520
+ );
30195
31521
  }
30196
31522
  const turn = this.turnForEvent(event, ctx);
30197
31523
  if (turn) {
@@ -30241,18 +31567,20 @@ var EveBridge = class {
30241
31567
  type: "function"
30242
31568
  });
30243
31569
  }
30244
- step.output = [
30245
- {
30246
- finish_reason: "tool_calls",
30247
- index: 0,
30248
- message: {
30249
- content: null,
30250
- role: "assistant",
30251
- tool_calls: [...toolCallsById.values()]
31570
+ step.output = mergeEveReasoning(
31571
+ [
31572
+ {
31573
+ finish_reason: "tool_calls",
31574
+ index: 0,
31575
+ message: {
31576
+ content: null,
31577
+ role: "assistant",
31578
+ tool_calls: [...toolCallsById.values()]
31579
+ }
30252
31580
  }
30253
- }
30254
- ];
30255
- step.span.log({ metadata: step.metadata, output: step.output });
31581
+ ],
31582
+ step.reasoning
31583
+ );
30256
31584
  }
30257
31585
  async handleActionResult(event, ctx, hookMetadata) {
30258
31586
  if (isToolResult(event.data.result)) {
@@ -30430,6 +31758,7 @@ var EveBridge = class {
30430
31758
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
30431
31759
  };
30432
31760
  step.metrics = { ...step.metrics, ...metrics };
31761
+ const sessionId = sessionIdFromContext(ctx);
30433
31762
  if (Array.isArray(step.output) && isObject(step.output[0])) {
30434
31763
  const finishReason = step.output[0].finish_reason;
30435
31764
  if (typeof finishReason !== "string") {
@@ -30454,6 +31783,14 @@ var EveBridge = class {
30454
31783
  turn.stepsByIndex.delete(event.data.stepIndex);
30455
31784
  }
30456
31785
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
31786
+ if (sessionId) {
31787
+ clearStoredEveReasoning(
31788
+ this.state,
31789
+ sessionId,
31790
+ event.data.turnId,
31791
+ event.data.stepIndex
31792
+ );
31793
+ }
30457
31794
  }
30458
31795
  handleStepFailed(event, ctx) {
30459
31796
  const step = this.stepForEvent(event, ctx);
@@ -30471,6 +31808,15 @@ var EveBridge = class {
30471
31808
  const turn = this.turnForEvent(event, ctx);
30472
31809
  turn?.stepsByIndex.delete(event.data.stepIndex);
30473
31810
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
31811
+ const sessionId = sessionIdFromContext(ctx);
31812
+ if (sessionId) {
31813
+ clearStoredEveReasoning(
31814
+ this.state,
31815
+ sessionId,
31816
+ event.data.turnId,
31817
+ event.data.stepIndex
31818
+ );
31819
+ }
30474
31820
  }
30475
31821
  handleTurnCompleted(event, ctx) {
30476
31822
  const turn = this.turnForEvent(event, ctx);
@@ -30568,6 +31914,7 @@ var EveBridge = class {
30568
31914
  key,
30569
31915
  metadata,
30570
31916
  metrics: {},
31917
+ sessionId,
30571
31918
  span,
30572
31919
  stepsByIndex: /* @__PURE__ */ new Map(),
30573
31920
  turnId: event.data.turnId
@@ -30830,6 +32177,9 @@ var EveBridge = class {
30830
32177
  const normalized = normalizeEveTraceState(current);
30831
32178
  return {
30832
32179
  ...normalized,
32180
+ reasoningBlocks: normalized.reasoningBlocks.filter(
32181
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
32182
+ ),
30833
32183
  stepStarts: normalized.stepStarts.filter(
30834
32184
  (entry) => entry.turnId !== turn.turnId
30835
32185
  )
@@ -30869,6 +32219,7 @@ function emptyEveTraceState() {
30869
32219
  return {
30870
32220
  llmInputs: [],
30871
32221
  metadata: {},
32222
+ reasoningBlocks: [],
30872
32223
  spanReferences: [],
30873
32224
  stepStarts: []
30874
32225
  };
@@ -30919,6 +32270,21 @@ function normalizeEveTraceState(state) {
30919
32270
  const input = entry["input"];
30920
32271
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
30921
32272
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
32273
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
32274
+ if (!isObject(entry)) {
32275
+ return [];
32276
+ }
32277
+ const content = entry["content"];
32278
+ const eventAt = entry["eventAt"];
32279
+ const key = entry["key"];
32280
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
32281
+ {
32282
+ content,
32283
+ ...typeof eventAt === "string" ? { eventAt } : {},
32284
+ key
32285
+ }
32286
+ ] : [];
32287
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
30922
32288
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
30923
32289
  if (!isObject(entry)) {
30924
32290
  return [];
@@ -30929,7 +32295,13 @@ function normalizeEveTraceState(state) {
30929
32295
  const turnId = entry["turnId"];
30930
32296
  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 }] : [];
30931
32297
  }).slice(-MAX_STORED_STEP_STARTS) : [];
30932
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
32298
+ return {
32299
+ llmInputs,
32300
+ metadata: { ...metadata },
32301
+ reasoningBlocks,
32302
+ spanReferences,
32303
+ stepStarts
32304
+ };
30933
32305
  }
30934
32306
  function readEveTraceState(state) {
30935
32307
  try {
@@ -30938,23 +32310,85 @@ function readEveTraceState(state) {
30938
32310
  return emptyEveTraceState();
30939
32311
  }
30940
32312
  }
30941
- function captureEveModelInput(state, input) {
30942
- if (!isObject(input)) {
30943
- return;
30944
- }
30945
- const session = input["session"];
30946
- const turn = input["turn"];
30947
- const step = input["step"];
30948
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
30949
- return;
30950
- }
30951
- const sessionId = session["id"];
30952
- const turnId = turn["id"];
30953
- const stepIndex = step["index"];
30954
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
30955
- return;
32313
+ function storeEveReasoning(state, sessionId, event) {
32314
+ const eventAt = event.meta?.at;
32315
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
32316
+ let stored = [];
32317
+ state.update((current) => {
32318
+ const normalized = normalizeEveTraceState(current);
32319
+ const alreadyStored = normalized.reasoningBlocks.some(
32320
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
32321
+ );
32322
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
32323
+ ...normalized.reasoningBlocks,
32324
+ {
32325
+ content: event.data.reasoning,
32326
+ ...eventAt ? { eventAt } : {},
32327
+ key
32328
+ }
32329
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
32330
+ stored = reasoningBlocks.flatMap(
32331
+ (entry) => entry.key === key ? [
32332
+ {
32333
+ content: entry.content,
32334
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
32335
+ }
32336
+ ] : []
32337
+ );
32338
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
32339
+ });
32340
+ return stored;
32341
+ }
32342
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
32343
+ const key = llmInputKey(sessionId, turnId, stepIndex);
32344
+ return readEveTraceState(state).reasoningBlocks.flatMap(
32345
+ (entry) => entry.key === key ? [
32346
+ {
32347
+ content: entry.content,
32348
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
32349
+ }
32350
+ ] : []
32351
+ );
32352
+ }
32353
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
32354
+ const key = llmInputKey(sessionId, turnId, stepIndex);
32355
+ state.update((current) => {
32356
+ const normalized = normalizeEveTraceState(current);
32357
+ return {
32358
+ ...normalized,
32359
+ reasoningBlocks: normalized.reasoningBlocks.filter(
32360
+ (entry) => entry.key !== key
32361
+ )
32362
+ };
32363
+ });
32364
+ }
32365
+ function eveOutputMessage(output) {
32366
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
32367
+ }
32368
+ function mergeEveReasoning(output, reasoning) {
32369
+ if (reasoning.length === 0) {
32370
+ return output;
30956
32371
  }
30957
- const captured = capturedModelInput(input["modelInput"]);
32372
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
32373
+ const message = isObject(choice["message"]) ? choice["message"] : {};
32374
+ return [
32375
+ {
32376
+ ...choice,
32377
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
32378
+ message: {
32379
+ ...message,
32380
+ content: "content" in message ? message["content"] : null,
32381
+ reasoning: reasoning.map((block) => ({ content: block.content })),
32382
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
32383
+ }
32384
+ }
32385
+ ];
32386
+ }
32387
+ function captureEveModelInput(state, input) {
32388
+ const sessionId = input.session.id;
32389
+ const turnId = input.turn.id;
32390
+ const stepIndex = input.step.index;
32391
+ const captured = capturedModelInput(input.modelInput);
30958
32392
  if (!captured) {
30959
32393
  return;
30960
32394
  }
@@ -30995,18 +32429,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
30995
32429
  }
30996
32430
  }
30997
32431
  function capturedModelInput(modelInput) {
30998
- if (!isObject(modelInput)) {
30999
- return void 0;
31000
- }
31001
- const messages = modelInput["messages"];
31002
- if (!Array.isArray(messages)) {
31003
- return void 0;
31004
- }
31005
- const instructions = modelInput["instructions"];
31006
- const value = [
31007
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
31008
- ...messages
31009
- ];
32432
+ const { instructions, messages } = modelInput;
32433
+ const value = [];
32434
+ if (typeof instructions === "string") {
32435
+ value.push({ content: instructions, role: "system" });
32436
+ } else if (instructions) {
32437
+ value.push(...instructions.map(capturedEveModelMessage));
32438
+ }
32439
+ value.push(...messages.map(capturedEveModelMessage));
31010
32440
  try {
31011
32441
  const cloned = JSON.parse(JSON.stringify(value));
31012
32442
  if (!Array.isArray(cloned)) {
@@ -31017,8 +32447,123 @@ function capturedModelInput(modelInput) {
31017
32447
  return void 0;
31018
32448
  }
31019
32449
  }
32450
+ function capturedEveModelMessage(message) {
32451
+ const { content, role } = message;
32452
+ if (typeof content === "string") {
32453
+ return { content, role };
32454
+ }
32455
+ return { content: content.map(capturedEveModelContentPart), role };
32456
+ }
32457
+ function capturedEveModelContentPart(part) {
32458
+ switch (part.type) {
32459
+ case "text":
32460
+ case "reasoning":
32461
+ return { text: part.text, type: part.type };
32462
+ case "image":
32463
+ return {
32464
+ image: part.image,
32465
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
32466
+ type: "image"
32467
+ };
32468
+ case "file":
32469
+ case "reasoning-file":
32470
+ return {
32471
+ data: part.data,
32472
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
32473
+ mediaType: part.mediaType,
32474
+ type: part.type
32475
+ };
32476
+ case "custom":
32477
+ return {
32478
+ ..."kind" in part ? { kind: part.kind } : {},
32479
+ type: "custom"
32480
+ };
32481
+ case "tool-call":
32482
+ return {
32483
+ input: part.input,
32484
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
32485
+ toolCallId: part.toolCallId,
32486
+ toolName: part.toolName,
32487
+ type: "tool-call"
32488
+ };
32489
+ case "tool-result": {
32490
+ const output = part.output;
32491
+ let capturedOutput;
32492
+ switch (output.type) {
32493
+ case "text":
32494
+ case "error-text":
32495
+ capturedOutput = { type: output.type, value: output.value };
32496
+ break;
32497
+ case "json":
32498
+ case "error-json":
32499
+ capturedOutput = { type: output.type, value: output.value };
32500
+ break;
32501
+ case "execution-denied":
32502
+ capturedOutput = {
32503
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
32504
+ type: "execution-denied"
32505
+ };
32506
+ break;
32507
+ case "content":
32508
+ capturedOutput = {
32509
+ type: "content",
32510
+ value: output.value.map(capturedEveModelContentPart)
32511
+ };
32512
+ break;
32513
+ }
32514
+ return {
32515
+ output: capturedOutput,
32516
+ toolCallId: part.toolCallId,
32517
+ toolName: part.toolName,
32518
+ type: "tool-result"
32519
+ };
32520
+ }
32521
+ case "tool-approval-request":
32522
+ return {
32523
+ approvalId: part.approvalId,
32524
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
32525
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
32526
+ toolCallId: part.toolCallId,
32527
+ type: "tool-approval-request"
32528
+ };
32529
+ case "tool-approval-response":
32530
+ return {
32531
+ approvalId: part.approvalId,
32532
+ approved: part.approved,
32533
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
32534
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
32535
+ type: "tool-approval-response"
32536
+ };
32537
+ case "file-data":
32538
+ case "image-data":
32539
+ return {
32540
+ data: part.data,
32541
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
32542
+ mediaType: part.mediaType,
32543
+ type: part.type
32544
+ };
32545
+ case "file-url":
32546
+ case "image-url":
32547
+ return {
32548
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
32549
+ type: part.type,
32550
+ url: part.url
32551
+ };
32552
+ case "file-id":
32553
+ case "image-file-id":
32554
+ return { fileId: part.fileId, type: part.type };
32555
+ case "file-reference":
32556
+ case "image-file-reference":
32557
+ return {
32558
+ providerReference: part.providerReference,
32559
+ type: part.type
32560
+ };
32561
+ }
32562
+ }
31020
32563
  function isCapturedModelInput(input) {
31021
- return Array.isArray(input);
32564
+ return Array.isArray(input) && input.every(
32565
+ (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))
32566
+ );
31022
32567
  }
31023
32568
  function llmInputKey(sessionId, turnId, stepIndex) {
31024
32569
  return `${sessionId}\0${turnId}\0${stepIndex}`;