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
package/dist/browser.js CHANGED
@@ -222,6 +222,7 @@ var DefaultChannel = class {
222
222
  constructor(name) {
223
223
  this.name = name;
224
224
  }
225
+ name;
225
226
  hasSubscribers = false;
226
227
  subscribe(_subscription) {
227
228
  }
@@ -1278,6 +1279,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
1278
1279
  constructor(data) {
1279
1280
  this.data = data;
1280
1281
  }
1282
+ data;
1281
1283
  toStr() {
1282
1284
  const jsonObj = {
1283
1285
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -1785,6 +1787,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
1785
1787
  constructor(data) {
1786
1788
  this.data = data;
1787
1789
  }
1790
+ data;
1788
1791
  toStr() {
1789
1792
  const jsonObj = {
1790
1793
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -5018,6 +5021,144 @@ var SpanCache = class {
5018
5021
  }
5019
5022
  };
5020
5023
 
5024
+ // src/span-origin.ts
5025
+ var INSTRUMENTATION_NAMES = {
5026
+ AI_SDK: "ai-sdk",
5027
+ ANTHROPIC: "anthropic",
5028
+ BEDROCK_RUNTIME: "bedrock-runtime",
5029
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
5030
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
5031
+ COHERE: "cohere",
5032
+ CURSOR_SDK: "cursor-sdk",
5033
+ EVE: "eve",
5034
+ FLUE: "flue",
5035
+ GENKIT: "genkit",
5036
+ GITHUB_COPILOT: "github-copilot",
5037
+ GOOGLE_ADK: "google-adk",
5038
+ GOOGLE_GENAI: "google-genai",
5039
+ GROQ: "groq",
5040
+ HUGGINGFACE: "huggingface",
5041
+ LANGCHAIN: "langchain",
5042
+ LANGSMITH: "langsmith",
5043
+ MASTRA: "mastra",
5044
+ MISTRAL: "mistral",
5045
+ OPENAI: "openai",
5046
+ OPENAI_AGENTS: "openai-agents",
5047
+ OPENAI_CODEX: "openai-codex",
5048
+ OPENROUTER: "openrouter",
5049
+ OPENROUTER_AGENT: "openrouter-agent",
5050
+ PI_CODING_AGENT: "pi-coding-agent",
5051
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
5052
+ };
5053
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
5054
+ "braintrust.spanInstrumentationName"
5055
+ );
5056
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
5057
+ function withSpanInstrumentationName(args, instrumentationName) {
5058
+ return {
5059
+ ...args,
5060
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
5061
+ };
5062
+ }
5063
+ function getSpanInstrumentationName(args) {
5064
+ if (typeof args !== "object" || args === null) {
5065
+ return void 0;
5066
+ }
5067
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
5068
+ return isSpanInstrumentationName(value) ? value : void 0;
5069
+ }
5070
+ function detectSpanOriginEnvironment(explicit) {
5071
+ if (explicit) return explicit;
5072
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
5073
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
5074
+ if (envType || envName) {
5075
+ return {
5076
+ ...envType ? { type: envType } : {},
5077
+ ...envName ? { name: envName } : {}
5078
+ };
5079
+ }
5080
+ const ci = firstPresent([
5081
+ ["GITHUB_ACTIONS", "github_actions"],
5082
+ ["GITLAB_CI", "gitlab_ci"],
5083
+ ["CIRCLECI", "circleci"],
5084
+ ["BUILDKITE", "buildkite"],
5085
+ ["JENKINS_URL", "jenkins"],
5086
+ ["JENKINS_HOME", "jenkins"],
5087
+ ["TF_BUILD", "azure_pipelines"],
5088
+ ["TEAMCITY_VERSION", "teamcity"],
5089
+ ["TRAVIS", "travis"],
5090
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
5091
+ ]);
5092
+ if (ci) return { type: "ci", name: ci };
5093
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
5094
+ const earlyServer = firstPresent([
5095
+ ["VERCEL", "vercel"],
5096
+ ["NETLIFY", "netlify"]
5097
+ ]);
5098
+ if (earlyServer) return { type: "server", name: earlyServer };
5099
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
5100
+ return { type: "server", name: "ecs" };
5101
+ }
5102
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
5103
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
5104
+ return { type: "server", name: "ecs" };
5105
+ }
5106
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
5107
+ return { type: "server", name: "aws_lambda" };
5108
+ }
5109
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
5110
+ return { type: "server", name: "aws_lambda" };
5111
+ }
5112
+ const server = firstPresent([
5113
+ ["K_SERVICE", "cloud_run"],
5114
+ ["FUNCTION_TARGET", "gcp_functions"],
5115
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
5116
+ ["DYNO", "heroku"],
5117
+ ["FLY_APP_NAME", "fly"],
5118
+ ["RAILWAY_ENVIRONMENT", "railway"],
5119
+ ["RENDER_SERVICE_NAME", "render"]
5120
+ ]);
5121
+ if (server) return { type: "server", name: server };
5122
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
5123
+ }
5124
+ function makeSpanOrigin(instrumentationName, environment) {
5125
+ return {
5126
+ name: "braintrust.sdk.javascript",
5127
+ version: SDK_VERSION,
5128
+ instrumentation: { name: instrumentationName },
5129
+ ...environment ? { environment } : {}
5130
+ };
5131
+ }
5132
+ function mergeSpanOriginContext(context, instrumentationName, environment) {
5133
+ const next = { ...context ?? {} };
5134
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
5135
+ next.span_origin = {
5136
+ ...makeSpanOrigin(instrumentationName, environment),
5137
+ ...current
5138
+ };
5139
+ return next;
5140
+ }
5141
+ function isSpanInstrumentationName(value) {
5142
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
5143
+ }
5144
+ function firstPresent(entries) {
5145
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
5146
+ }
5147
+ function deploymentModeEnvironment(_key, value) {
5148
+ if (!value) return void 0;
5149
+ const normalized = value.toLowerCase();
5150
+ if (normalized === "production" || normalized === "staging") {
5151
+ return { type: "server", name: normalized };
5152
+ }
5153
+ if (normalized === "development" || normalized === "local") {
5154
+ return { type: "local", name: normalized };
5155
+ }
5156
+ return { name: value };
5157
+ }
5158
+ function isObject2(value) {
5159
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5160
+ }
5161
+
5021
5162
  // src/logger.ts
5022
5163
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
5023
5164
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -5066,6 +5207,7 @@ var LoginInvalidOrgError = class extends Error {
5066
5207
  super(message);
5067
5208
  this.message = message;
5068
5209
  }
5210
+ message;
5069
5211
  };
5070
5212
  var REDACTION_FIELDS = [
5071
5213
  "input",
@@ -5081,6 +5223,8 @@ var MaskingError = class {
5081
5223
  this.fieldName = fieldName;
5082
5224
  this.errorType = errorType;
5083
5225
  }
5226
+ fieldName;
5227
+ errorType;
5084
5228
  get errorMsg() {
5085
5229
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
5086
5230
  }
@@ -5290,7 +5434,10 @@ var BraintrustState = class _BraintrustState {
5290
5434
  diskCache: parametersDiskCache
5291
5435
  });
5292
5436
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
5437
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
5438
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5293
5439
  }
5440
+ loginParams;
5294
5441
  id;
5295
5442
  currentExperiment;
5296
5443
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -5325,6 +5472,19 @@ var BraintrustState = class _BraintrustState {
5325
5472
  _idGenerator = null;
5326
5473
  _contextManager = null;
5327
5474
  _otelFlushCallback = null;
5475
+ spanOriginEnvironment;
5476
+ traceContextSigningSecret;
5477
+ /** @internal */
5478
+ _internalSetTraceContextSigningSecret(secret) {
5479
+ const normalizedSecret = secret?.trim();
5480
+ if (normalizedSecret) {
5481
+ this.traceContextSigningSecret = normalizedSecret;
5482
+ }
5483
+ }
5484
+ /** @internal */
5485
+ _internalGetTraceContextSigningSecret() {
5486
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
5487
+ }
5328
5488
  resetLoginInfo() {
5329
5489
  this.appUrl = null;
5330
5490
  this.appPublicUrl = null;
@@ -5385,6 +5545,7 @@ var BraintrustState = class _BraintrustState {
5385
5545
  this.gitMetadataSettings = other.gitMetadataSettings;
5386
5546
  this.debugLogLevel = other.debugLogLevel;
5387
5547
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
5548
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
5388
5549
  setGlobalDebugLogLevel(
5389
5550
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
5390
5551
  );
@@ -5472,6 +5633,7 @@ var BraintrustState = class _BraintrustState {
5472
5633
  return this.debugLogLevelConfigured;
5473
5634
  }
5474
5635
  async login(loginParams) {
5636
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5475
5637
  this.setDebugLogLevel(loginParams.debugLogLevel);
5476
5638
  if (this.apiUrl && !loginParams.forceLogin) {
5477
5639
  return;
@@ -7451,6 +7613,7 @@ function init(projectOrOptions, optionalOptions) {
7451
7613
  throw new Error("Cannot open and update an experiment at the same time");
7452
7614
  }
7453
7615
  const state = stateArg ?? _globalState;
7616
+ state._internalSetTraceContextSigningSecret(apiKey);
7454
7617
  state.enforceQueueSizeLimit(false);
7455
7618
  if (open) {
7456
7619
  if (isEmpty2(experiment)) {
@@ -7943,6 +8106,7 @@ function initLogger(options = {}) {
7943
8106
  orgName,
7944
8107
  forceLogin,
7945
8108
  debugLogLevel,
8109
+ environment,
7946
8110
  fetch: fetch2,
7947
8111
  state: stateArg
7948
8112
  } = options || {};
@@ -7958,7 +8122,9 @@ function initLogger(options = {}) {
7958
8122
  project_id: projectId
7959
8123
  };
7960
8124
  const state = stateArg ?? _globalState;
8125
+ state._internalSetTraceContextSigningSecret(apiKey);
7961
8126
  state.setDebugLogLevel(debugLogLevel);
8127
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
7962
8128
  state.enforceQueueSizeLimit(true);
7963
8129
  const lazyMetadata = new LazyValue(
7964
8130
  async () => {
@@ -9020,7 +9186,9 @@ function deepCopyEvent(event) {
9020
9186
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
9021
9187
  const attachmentMarker = ++deepCopyEventMarkerCounter;
9022
9188
  const serialized = JSON.stringify(event, (_k, v) => {
9023
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
9189
+ if (v instanceof Error) {
9190
+ return v.message;
9191
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
9024
9192
  return `<span>`;
9025
9193
  } else if (v instanceof Experiment2) {
9026
9194
  return `<experiment>`;
@@ -9150,6 +9318,11 @@ var ObjectFetcher = class {
9150
9318
  this._internal_btql = _internal_btql;
9151
9319
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
9152
9320
  }
9321
+ objectType;
9322
+ pinnedVersion;
9323
+ mutateRecord;
9324
+ _internal_btql;
9325
+ _internalBrainstoreRealtime;
9153
9326
  _fetchedData = void 0;
9154
9327
  get id() {
9155
9328
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -9588,6 +9761,8 @@ var ReadonlyExperiment = class extends ObjectFetcher {
9588
9761
  this.state = state;
9589
9762
  this.lazyMetadata = lazyMetadata;
9590
9763
  }
9764
+ state;
9765
+ lazyMetadata;
9591
9766
  get id() {
9592
9767
  return (async () => {
9593
9768
  return (await this.lazyMetadata.get()).experiment.id;
@@ -9681,6 +9856,7 @@ var SpanImpl = class _SpanImpl {
9681
9856
  constructor(args) {
9682
9857
  this._state = args.state;
9683
9858
  this._propagatedState = args.propagatedState;
9859
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
9684
9860
  const spanAttributes = args.spanAttributes ?? {};
9685
9861
  const rawEvent = args.event ?? {};
9686
9862
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -9710,7 +9886,11 @@ var SpanImpl = class _SpanImpl {
9710
9886
  metrics: {
9711
9887
  start: args.startTime ?? getCurrentUnixTimestamp()
9712
9888
  },
9713
- context: { ...callerLocation },
9889
+ context: mergeSpanOriginContext(
9890
+ { ...callerLocation },
9891
+ instrumentationName,
9892
+ this._state.spanOriginEnvironment
9893
+ ),
9714
9894
  span_attributes: {
9715
9895
  name,
9716
9896
  type,
@@ -10084,6 +10264,7 @@ var Dataset2 = class extends ObjectFetcher {
10084
10264
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
10085
10265
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
10086
10266
  }
10267
+ state;
10087
10268
  lazyMetadata;
10088
10269
  __braintrust_dataset_marker = true;
10089
10270
  newRecords = 0;
@@ -10598,6 +10779,9 @@ var Prompt2 = class _Prompt {
10598
10779
  this.noTrace = noTrace;
10599
10780
  void this.__braintrust_prompt_marker;
10600
10781
  }
10782
+ metadata;
10783
+ defaults;
10784
+ noTrace;
10601
10785
  parsedPromptData;
10602
10786
  hasParsedPromptData = false;
10603
10787
  __braintrust_prompt_marker = true;
@@ -10835,6 +11019,7 @@ var RemoteEvalParameters = class {
10835
11019
  this.metadata = metadata;
10836
11020
  void this.__braintrust_parameters_marker;
10837
11021
  }
11022
+ metadata;
10838
11023
  __braintrust_parameters_marker = true;
10839
11024
  get id() {
10840
11025
  return this.metadata.id;
@@ -11687,15 +11872,25 @@ function hasChoices(value) {
11687
11872
  function normalizeMetadata(metadata) {
11688
11873
  return isObject(metadata) ? metadata : void 0;
11689
11874
  }
11690
- function startSpanForEvent(config, event, channelName) {
11875
+ function startSpanForEvent(config, event, channelName, instrumentationName) {
11691
11876
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
11692
11877
  config,
11693
11878
  event
11694
11879
  );
11695
- const span = startSpan({
11696
- name,
11697
- spanAttributes
11698
- });
11880
+ const spanArgs = withSpanInstrumentationName(
11881
+ {
11882
+ name,
11883
+ spanAttributes
11884
+ },
11885
+ instrumentationName
11886
+ );
11887
+ let span;
11888
+ try {
11889
+ span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
11890
+ } catch (error) {
11891
+ debugLogger.error(`Error starting span for ${channelName}:`, error);
11892
+ span = startSpan(spanArgs);
11893
+ }
11699
11894
  const startTime = getCurrentUnixTimestamp();
11700
11895
  try {
11701
11896
  const { input, metadata } = config.extractInput(
@@ -11726,7 +11921,7 @@ function shouldTraceEvent(config, event, channelName) {
11726
11921
  return true;
11727
11922
  }
11728
11923
  }
11729
- function ensureSpanStateForEvent(states, config, event, channelName) {
11924
+ function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
11730
11925
  const key = event;
11731
11926
  const existing = states.get(key);
11732
11927
  if (existing) {
@@ -11735,11 +11930,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
11735
11930
  if (!shouldTraceEvent(config, event, channelName)) {
11736
11931
  return void 0;
11737
11932
  }
11738
- const created = startSpanForEvent(config, event, channelName);
11933
+ const created = startSpanForEvent(
11934
+ config,
11935
+ event,
11936
+ channelName,
11937
+ instrumentationName
11938
+ );
11739
11939
  states.set(key, created);
11740
11940
  return created;
11741
11941
  }
11742
- function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
11942
+ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName, instrumentationName) {
11743
11943
  const state = _internalGetGlobalState();
11744
11944
  const startChannel = tracingChannel2.start;
11745
11945
  const contextManager = state?.contextManager;
@@ -11757,7 +11957,8 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11757
11957
  states,
11758
11958
  config,
11759
11959
  event,
11760
- channelName
11960
+ channelName,
11961
+ instrumentationName
11761
11962
  );
11762
11963
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
11763
11964
  }
@@ -11766,15 +11967,21 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11766
11967
  startChannel.unbindStore(currentSpanStore);
11767
11968
  };
11768
11969
  }
11769
- function logErrorAndEnd(states, event) {
11970
+ function logErrorAndEnd(states, event, channelName) {
11770
11971
  const spanData = states.get(event);
11771
11972
  if (!spanData) {
11772
11973
  return;
11773
11974
  }
11774
- spanData.span.log({
11775
- error: event.error.message
11776
- });
11777
- spanData.span.end();
11975
+ try {
11976
+ spanData.span.log({ error: event.error });
11977
+ } catch (error) {
11978
+ debugLogger.error(`Error logging failure for ${channelName}:`, error);
11979
+ }
11980
+ try {
11981
+ spanData.span.end();
11982
+ } catch (error) {
11983
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
11984
+ }
11778
11985
  states.delete(event);
11779
11986
  }
11780
11987
  function runStreamingCompletionHook(args) {
@@ -11824,7 +12031,8 @@ function traceAsyncChannel(channel2, config) {
11824
12031
  tracingChannel2,
11825
12032
  states,
11826
12033
  config,
11827
- channelName
12034
+ channelName,
12035
+ channel2.instrumentationName
11828
12036
  );
11829
12037
  const handlers = {
11830
12038
  start: (event) => {
@@ -11835,7 +12043,8 @@ function traceAsyncChannel(channel2, config) {
11835
12043
  states,
11836
12044
  config,
11837
12045
  event,
11838
- channelName
12046
+ channelName,
12047
+ channel2.instrumentationName
11839
12048
  );
11840
12049
  },
11841
12050
  asyncEnd: (event) => {
@@ -11872,7 +12081,7 @@ function traceAsyncChannel(channel2, config) {
11872
12081
  }
11873
12082
  },
11874
12083
  error: (event) => {
11875
- logErrorAndEnd(states, event);
12084
+ logErrorAndEnd(states, event, channelName);
11876
12085
  }
11877
12086
  };
11878
12087
  tracingChannel2.subscribe(handlers);
@@ -11889,7 +12098,8 @@ function traceStreamingChannel(channel2, config) {
11889
12098
  tracingChannel2,
11890
12099
  states,
11891
12100
  config,
11892
- channelName
12101
+ channelName,
12102
+ channel2.instrumentationName
11893
12103
  );
11894
12104
  const handlers = {
11895
12105
  start: (event) => {
@@ -11900,7 +12110,8 @@ function traceStreamingChannel(channel2, config) {
11900
12110
  states,
11901
12111
  config,
11902
12112
  event,
11903
- channelName
12113
+ channelName,
12114
+ channel2.instrumentationName
11904
12115
  );
11905
12116
  },
11906
12117
  asyncEnd: (event) => {
@@ -11919,6 +12130,7 @@ function traceStreamingChannel(channel2, config) {
11919
12130
  }
11920
12131
  },
11921
12132
  onComplete: (chunks) => {
12133
+ let completion2;
11922
12134
  try {
11923
12135
  let output;
11924
12136
  let metrics;
@@ -11949,18 +12161,11 @@ function traceStreamingChannel(channel2, config) {
11949
12161
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
11950
12162
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
11951
12163
  }
11952
- runStreamingCompletionHook({
11953
- channelName,
11954
- chunks,
11955
- config,
11956
- endEvent: asyncEndEvent,
12164
+ completion2 = {
11957
12165
  ...metadata !== void 0 ? { metadata } : {},
11958
12166
  metrics,
11959
- output,
11960
- result: asyncEndEvent.result,
11961
- span,
11962
- startTime
11963
- });
12167
+ output
12168
+ };
11964
12169
  span.log({
11965
12170
  output,
11966
12171
  ...metadata !== void 0 ? { metadata } : {},
@@ -11972,11 +12177,49 @@ function traceStreamingChannel(channel2, config) {
11972
12177
  error
11973
12178
  );
11974
12179
  } finally {
11975
- span.end();
12180
+ try {
12181
+ span.end();
12182
+ } catch (error) {
12183
+ debugLogger.error(
12184
+ `Error ending span for ${channelName}:`,
12185
+ error
12186
+ );
12187
+ }
11976
12188
  states.delete(event);
11977
12189
  }
12190
+ if (completion2) {
12191
+ runStreamingCompletionHook({
12192
+ channelName,
12193
+ chunks,
12194
+ config,
12195
+ endEvent: asyncEndEvent,
12196
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
12197
+ metrics: completion2.metrics,
12198
+ output: completion2.output,
12199
+ result: asyncEndEvent.result,
12200
+ span,
12201
+ startTime
12202
+ });
12203
+ }
11978
12204
  },
11979
12205
  onError: (error) => {
12206
+ try {
12207
+ span.log({ error });
12208
+ } catch (loggingError) {
12209
+ debugLogger.error(
12210
+ `Error logging failure for ${channelName}:`,
12211
+ loggingError
12212
+ );
12213
+ }
12214
+ try {
12215
+ span.end();
12216
+ } catch (endingError) {
12217
+ debugLogger.error(
12218
+ `Error ending span for ${channelName}:`,
12219
+ endingError
12220
+ );
12221
+ }
12222
+ states.delete(event);
11980
12223
  runStreamingErrorHook({
11981
12224
  channelName,
11982
12225
  config,
@@ -11985,11 +12228,6 @@ function traceStreamingChannel(channel2, config) {
11985
12228
  span,
11986
12229
  startTime
11987
12230
  });
11988
- span.log({
11989
- error: error.message
11990
- });
11991
- span.end();
11992
- states.delete(event);
11993
12231
  }
11994
12232
  });
11995
12233
  return;
@@ -12004,6 +12242,7 @@ function traceStreamingChannel(channel2, config) {
12004
12242
  states.delete(event);
12005
12243
  return;
12006
12244
  }
12245
+ let completion;
12007
12246
  try {
12008
12247
  const output = config.extractOutput(
12009
12248
  asyncEndEvent.result,
@@ -12018,17 +12257,11 @@ function traceStreamingChannel(channel2, config) {
12018
12257
  asyncEndEvent.result,
12019
12258
  asyncEndEvent
12020
12259
  );
12021
- runStreamingCompletionHook({
12022
- channelName,
12023
- config,
12024
- endEvent: asyncEndEvent,
12260
+ completion = {
12025
12261
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
12026
12262
  metrics,
12027
- output,
12028
- result: asyncEndEvent.result,
12029
- span,
12030
- startTime
12031
- });
12263
+ output
12264
+ };
12032
12265
  span.log({
12033
12266
  output,
12034
12267
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -12037,12 +12270,30 @@ function traceStreamingChannel(channel2, config) {
12037
12270
  } catch (error) {
12038
12271
  debugLogger.error(`Error extracting output for ${channelName}:`, error);
12039
12272
  } finally {
12040
- span.end();
12273
+ try {
12274
+ span.end();
12275
+ } catch (error) {
12276
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
12277
+ }
12041
12278
  states.delete(event);
12042
12279
  }
12280
+ if (completion) {
12281
+ runStreamingCompletionHook({
12282
+ channelName,
12283
+ config,
12284
+ endEvent: asyncEndEvent,
12285
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
12286
+ metrics: completion.metrics,
12287
+ output: completion.output,
12288
+ result: asyncEndEvent.result,
12289
+ span,
12290
+ startTime
12291
+ });
12292
+ }
12043
12293
  },
12044
12294
  error: (event) => {
12045
12295
  const spanData = states.get(event);
12296
+ logErrorAndEnd(states, event, channelName);
12046
12297
  if (spanData) {
12047
12298
  runStreamingErrorHook({
12048
12299
  channelName,
@@ -12053,7 +12304,6 @@ function traceStreamingChannel(channel2, config) {
12053
12304
  startTime: spanData.startTime
12054
12305
  });
12055
12306
  }
12056
- logErrorAndEnd(states, event);
12057
12307
  }
12058
12308
  };
12059
12309
  tracingChannel2.subscribe(handlers);
@@ -12070,7 +12320,8 @@ function traceSyncStreamChannel(channel2, config) {
12070
12320
  tracingChannel2,
12071
12321
  states,
12072
12322
  config,
12073
- channelName
12323
+ channelName,
12324
+ channel2.instrumentationName
12074
12325
  );
12075
12326
  const handlers = {
12076
12327
  start: (event) => {
@@ -12081,7 +12332,8 @@ function traceSyncStreamChannel(channel2, config) {
12081
12332
  states,
12082
12333
  config,
12083
12334
  event,
12084
- channelName
12335
+ channelName,
12336
+ channel2.instrumentationName
12085
12337
  );
12086
12338
  },
12087
12339
  end: (event) => {
@@ -12175,7 +12427,7 @@ function traceSyncStreamChannel(channel2, config) {
12175
12427
  handleResolvedResult(endEvent.result);
12176
12428
  },
12177
12429
  error: (event) => {
12178
- logErrorAndEnd(states, event);
12430
+ logErrorAndEnd(states, event, channelName);
12179
12431
  }
12180
12432
  };
12181
12433
  tracingChannel2.subscribe(handlers);
@@ -12354,7 +12606,8 @@ function processInputAttachments(input) {
12354
12606
  function channel(spec) {
12355
12607
  return spec;
12356
12608
  }
12357
- function defineChannels(pkg, channels) {
12609
+ function defineChannels(pkg, channels, options) {
12610
+ const { instrumentationName } = options;
12358
12611
  return Object.fromEntries(
12359
12612
  Object.entries(channels).map(([key, spec]) => {
12360
12613
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -12367,6 +12620,7 @@ function defineChannels(pkg, channels) {
12367
12620
  key,
12368
12621
  {
12369
12622
  ...asyncSpec,
12623
+ instrumentationName,
12370
12624
  tracingChannel: tracingChannel3,
12371
12625
  tracePromise: (fn, context) => tracingChannel3().tracePromise(
12372
12626
  fn,
@@ -12384,6 +12638,7 @@ function defineChannels(pkg, channels) {
12384
12638
  key,
12385
12639
  {
12386
12640
  ...syncSpec,
12641
+ instrumentationName,
12387
12642
  tracingChannel: tracingChannel2,
12388
12643
  traceSync: (fn, context) => tracingChannel2().traceSync(
12389
12644
  fn,
@@ -12397,44 +12652,48 @@ function defineChannels(pkg, channels) {
12397
12652
  }
12398
12653
 
12399
12654
  // src/instrumentation/plugins/openai-channels.ts
12400
- var openAIChannels = defineChannels("openai", {
12401
- chatCompletionsCreate: channel({
12402
- channelName: "chat.completions.create",
12403
- kind: "async"
12404
- }),
12405
- embeddingsCreate: channel({
12406
- channelName: "embeddings.create",
12407
- kind: "async"
12408
- }),
12409
- betaChatCompletionsParse: channel({
12410
- channelName: "beta.chat.completions.parse",
12411
- kind: "async"
12412
- }),
12413
- betaChatCompletionsStream: channel({
12414
- channelName: "beta.chat.completions.stream",
12415
- kind: "sync-stream"
12416
- }),
12417
- moderationsCreate: channel({
12418
- channelName: "moderations.create",
12419
- kind: "async"
12420
- }),
12421
- responsesCreate: channel({
12422
- channelName: "responses.create",
12423
- kind: "async"
12424
- }),
12425
- responsesStream: channel({
12426
- channelName: "responses.stream",
12427
- kind: "sync-stream"
12428
- }),
12429
- responsesParse: channel({
12430
- channelName: "responses.parse",
12431
- kind: "async"
12432
- }),
12433
- responsesCompact: channel({
12434
- channelName: "responses.compact",
12435
- kind: "async"
12436
- })
12437
- });
12655
+ var openAIChannels = defineChannels(
12656
+ "openai",
12657
+ {
12658
+ chatCompletionsCreate: channel({
12659
+ channelName: "chat.completions.create",
12660
+ kind: "async"
12661
+ }),
12662
+ embeddingsCreate: channel({
12663
+ channelName: "embeddings.create",
12664
+ kind: "async"
12665
+ }),
12666
+ betaChatCompletionsParse: channel({
12667
+ channelName: "beta.chat.completions.parse",
12668
+ kind: "async"
12669
+ }),
12670
+ betaChatCompletionsStream: channel({
12671
+ channelName: "beta.chat.completions.stream",
12672
+ kind: "sync-stream"
12673
+ }),
12674
+ moderationsCreate: channel({
12675
+ channelName: "moderations.create",
12676
+ kind: "async"
12677
+ }),
12678
+ responsesCreate: channel({
12679
+ channelName: "responses.create",
12680
+ kind: "async"
12681
+ }),
12682
+ responsesStream: channel({
12683
+ channelName: "responses.stream",
12684
+ kind: "sync-stream"
12685
+ }),
12686
+ responsesParse: channel({
12687
+ channelName: "responses.parse",
12688
+ kind: "async"
12689
+ }),
12690
+ responsesCompact: channel({
12691
+ channelName: "responses.compact",
12692
+ kind: "async"
12693
+ })
12694
+ },
12695
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
12696
+ );
12438
12697
 
12439
12698
  // src/openai-utils.ts
12440
12699
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -12949,16 +13208,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
12949
13208
  }
12950
13209
 
12951
13210
  // src/instrumentation/plugins/openai-codex-channels.ts
12952
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
12953
- run: channel({
12954
- channelName: "Thread.run",
12955
- kind: "async"
12956
- }),
12957
- runStreamed: channel({
12958
- channelName: "Thread.runStreamed",
12959
- kind: "async"
12960
- })
12961
- });
13211
+ var openAICodexChannels = defineChannels(
13212
+ "@openai/codex-sdk",
13213
+ {
13214
+ run: channel({
13215
+ channelName: "Thread.run",
13216
+ kind: "async"
13217
+ }),
13218
+ runStreamed: channel({
13219
+ channelName: "Thread.runStreamed",
13220
+ kind: "async"
13221
+ })
13222
+ },
13223
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
13224
+ );
12962
13225
 
12963
13226
  // src/instrumentation/plugins/openai-codex-plugin.ts
12964
13227
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -13046,10 +13309,15 @@ function startCodexRun(event, operation) {
13046
13309
  provider: "openai",
13047
13310
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
13048
13311
  };
13049
- const span = startSpan({
13050
- name: "OpenAI Codex",
13051
- spanAttributes: { type: "task" /* TASK */ }
13052
- });
13312
+ const span = startSpan(
13313
+ withSpanInstrumentationName(
13314
+ {
13315
+ name: "OpenAI Codex",
13316
+ spanAttributes: { type: "task" /* TASK */ }
13317
+ },
13318
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13319
+ )
13320
+ );
13053
13321
  const startTime = getCurrentUnixTimestamp();
13054
13322
  safeLog(span, {
13055
13323
  input: sanitizedInput,
@@ -13198,7 +13466,12 @@ async function createCompletedItemSpan(state, item) {
13198
13466
  if (!spanArgs) {
13199
13467
  return;
13200
13468
  }
13201
- const span = startSpan(spanArgs.start);
13469
+ const span = startSpan(
13470
+ withSpanInstrumentationName(
13471
+ spanArgs.start,
13472
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13473
+ )
13474
+ );
13202
13475
  safeLog(span, spanArgs.end);
13203
13476
  span.end();
13204
13477
  }
@@ -13240,15 +13513,20 @@ async function ensureActiveLlmSpan(state) {
13240
13513
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
13241
13514
  "openai_codex.llm_sequence": sequence
13242
13515
  };
13243
- const span = startSpan({
13244
- event: {
13245
- ...sequence === 1 ? { input: state.input } : {},
13246
- metadata
13247
- },
13248
- name: "OpenAI Codex LLM",
13249
- parent: await state.span.export(),
13250
- spanAttributes: { type: "llm" /* LLM */ }
13251
- });
13516
+ const span = startSpan(
13517
+ withSpanInstrumentationName(
13518
+ {
13519
+ event: {
13520
+ ...sequence === 1 ? { input: state.input } : {},
13521
+ metadata
13522
+ },
13523
+ name: "OpenAI Codex LLM",
13524
+ parent: await state.span.export(),
13525
+ spanAttributes: { type: "llm" /* LLM */ }
13526
+ },
13527
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13528
+ )
13529
+ );
13252
13530
  state.activeLlmSpan = {
13253
13531
  anonymousMessages: [],
13254
13532
  anonymousReasoning: [],
@@ -13294,7 +13572,15 @@ async function startCodexItemSpan(state, item) {
13294
13572
  if (!spanArgs) {
13295
13573
  return;
13296
13574
  }
13297
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
13575
+ state.activeItemSpans.set(
13576
+ itemId,
13577
+ startSpan(
13578
+ withSpanInstrumentationName(
13579
+ spanArgs.start,
13580
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13581
+ )
13582
+ )
13583
+ );
13298
13584
  }
13299
13585
  function updateCodexItem(state, item) {
13300
13586
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -13604,20 +13890,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
13604
13890
  }
13605
13891
 
13606
13892
  // src/instrumentation/plugins/anthropic-channels.ts
13607
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
13608
- messagesCreate: channel({
13609
- channelName: "messages.create",
13610
- kind: "async"
13611
- }),
13612
- betaMessagesCreate: channel({
13613
- channelName: "beta.messages.create",
13614
- kind: "async"
13615
- }),
13616
- betaMessagesToolRunner: channel({
13617
- channelName: "beta.messages.toolRunner",
13618
- kind: "sync-stream"
13619
- })
13620
- });
13893
+ var anthropicChannels = defineChannels(
13894
+ "@anthropic-ai/sdk",
13895
+ {
13896
+ messagesCreate: channel({
13897
+ channelName: "messages.create",
13898
+ kind: "async"
13899
+ }),
13900
+ betaMessagesCreate: channel({
13901
+ channelName: "beta.messages.create",
13902
+ kind: "async"
13903
+ }),
13904
+ betaMessagesToolRunner: channel({
13905
+ channelName: "beta.messages.toolRunner",
13906
+ kind: "sync-stream"
13907
+ })
13908
+ },
13909
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
13910
+ );
13621
13911
 
13622
13912
  // src/instrumentation/plugins/anthropic-plugin.ts
13623
13913
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -13690,12 +13980,17 @@ var AnthropicPlugin = class extends BasePlugin {
13690
13980
  return;
13691
13981
  }
13692
13982
  const params = event.arguments[0] ?? {};
13693
- const span = startSpan({
13694
- name: "anthropic.beta.messages.toolRunner",
13695
- spanAttributes: {
13696
- type: "task" /* TASK */
13697
- }
13698
- });
13983
+ const span = startSpan(
13984
+ withSpanInstrumentationName(
13985
+ {
13986
+ name: "anthropic.beta.messages.toolRunner",
13987
+ spanAttributes: {
13988
+ type: "task" /* TASK */
13989
+ }
13990
+ },
13991
+ INSTRUMENTATION_NAMES.ANTHROPIC
13992
+ )
13993
+ );
13699
13994
  span.log({
13700
13995
  input: processAttachmentsInInput(
13701
13996
  coalesceInput(params.messages ?? [], params.system)
@@ -13836,19 +14131,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
13836
14131
  return finalizeError(error);
13837
14132
  }
13838
14133
  },
13839
- {
13840
- event: {
13841
- input: getAnthropicToolRunnerInput(args),
13842
- metadata: {
13843
- "gen_ai.tool.name": toolName,
13844
- provider: "anthropic"
14134
+ withSpanInstrumentationName(
14135
+ {
14136
+ event: {
14137
+ input: getAnthropicToolRunnerInput(args),
14138
+ metadata: {
14139
+ "gen_ai.tool.name": toolName,
14140
+ provider: "anthropic"
14141
+ }
14142
+ },
14143
+ name: `tool: ${toolName}`,
14144
+ spanAttributes: {
14145
+ type: "tool" /* TOOL */
13845
14146
  }
13846
14147
  },
13847
- name: `tool: ${toolName}`,
13848
- spanAttributes: {
13849
- type: "tool" /* TOOL */
13850
- }
13851
- }
14148
+ INSTRUMENTATION_NAMES.ANTHROPIC
14149
+ )
13852
14150
  );
13853
14151
  },
13854
14152
  writable: runDescriptor?.writable ?? true
@@ -14443,6 +14741,267 @@ function currentWorkflowAgentWrapperSpan() {
14443
14741
  return void 0;
14444
14742
  }
14445
14743
 
14744
+ // src/wrappers/ai-sdk/harness-agent-context.ts
14745
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
14746
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
14747
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
14748
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
14749
+ var harnessTurnParentStore;
14750
+ function serializedTurnContext(value) {
14751
+ if (!isObject(value)) {
14752
+ return void 0;
14753
+ }
14754
+ const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
14755
+ if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
14756
+ return void 0;
14757
+ }
14758
+ const expectedSignature = signTurnContext({
14759
+ parent: context.parent,
14760
+ sessionId: context.sessionId
14761
+ });
14762
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
14763
+ return void 0;
14764
+ }
14765
+ const parent = SpanComponentsV4.fromStr(context.parent);
14766
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
14767
+ return void 0;
14768
+ }
14769
+ return {
14770
+ parent: context.parent,
14771
+ sessionId: context.sessionId
14772
+ };
14773
+ }
14774
+ function signTurnContext(context) {
14775
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
14776
+ return secret ? isomorph_default.hmacSha256?.(
14777
+ secret,
14778
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
14779
+ ) : void 0;
14780
+ }
14781
+ function continuationParentFromCreateSessionParams(params) {
14782
+ if (!isObject(params)) {
14783
+ return void 0;
14784
+ }
14785
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
14786
+ const context = serializedTurnContext(continuation);
14787
+ if (!context) {
14788
+ return void 0;
14789
+ }
14790
+ if (params.sessionId !== context.sessionId) {
14791
+ return void 0;
14792
+ }
14793
+ return context.parent;
14794
+ }
14795
+ function exportSpanSynchronously(span) {
14796
+ const parentInfo = span.getParentInfo();
14797
+ if (!parentInfo) {
14798
+ return void 0;
14799
+ }
14800
+ const objectId = parentInfo.objectId.getSync().value;
14801
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
14802
+ return void 0;
14803
+ }
14804
+ return new SpanComponentsV4({
14805
+ object_type: parentInfo.objectType,
14806
+ ...objectId ? { object_id: objectId } : {
14807
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
14808
+ },
14809
+ row_id: span.id,
14810
+ root_span_id: span.rootSpanId,
14811
+ span_id: span.spanId
14812
+ }).toStr();
14813
+ }
14814
+ function exportedParent(parent) {
14815
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
14816
+ }
14817
+ function addSerializedContext(args) {
14818
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
14819
+ return;
14820
+ }
14821
+ const parent = exportedParent(args.parent);
14822
+ if (!parent) {
14823
+ return;
14824
+ }
14825
+ const context = {
14826
+ parent,
14827
+ sessionId: args.sessionId
14828
+ };
14829
+ const signature = signTurnContext(context);
14830
+ if (!signature) {
14831
+ return;
14832
+ }
14833
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
14834
+ configurable: true,
14835
+ enumerable: true,
14836
+ value: {
14837
+ ...context,
14838
+ signature
14839
+ },
14840
+ writable: true
14841
+ });
14842
+ }
14843
+ function lifecycleMethodDescriptor(session, method) {
14844
+ let owner = session;
14845
+ while (owner) {
14846
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
14847
+ if (descriptor) {
14848
+ return { descriptor, owner };
14849
+ }
14850
+ owner = Object.getPrototypeOf(owner);
14851
+ }
14852
+ return void 0;
14853
+ }
14854
+ function patchLifecycleMethod(session, method) {
14855
+ let resolvedDescriptor;
14856
+ try {
14857
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
14858
+ } catch {
14859
+ return;
14860
+ }
14861
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
14862
+ return;
14863
+ }
14864
+ const { descriptor, owner } = resolvedDescriptor;
14865
+ const patched = patchedLifecycleMethods.get(owner);
14866
+ if (patched?.has(method)) {
14867
+ return;
14868
+ }
14869
+ const original = descriptor.value;
14870
+ const replacement = function(...args) {
14871
+ const result = Reflect.apply(original, this, args);
14872
+ const addContext = (state) => {
14873
+ try {
14874
+ const parent = sessionTurnParents.get(this);
14875
+ if (!parent) {
14876
+ return;
14877
+ }
14878
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
14879
+ addSerializedContext({
14880
+ continuation,
14881
+ parent,
14882
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
14883
+ });
14884
+ } catch {
14885
+ }
14886
+ };
14887
+ try {
14888
+ if (isObject(result) && typeof result.then === "function") {
14889
+ void Promise.resolve(result).then(addContext, () => {
14890
+ });
14891
+ } else {
14892
+ addContext(result);
14893
+ }
14894
+ } catch {
14895
+ }
14896
+ return result;
14897
+ };
14898
+ try {
14899
+ Object.defineProperty(owner, method, {
14900
+ ...descriptor,
14901
+ value: replacement
14902
+ });
14903
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
14904
+ ownerMethods.add(method);
14905
+ if (!patched) {
14906
+ patchedLifecycleMethods.set(owner, ownerMethods);
14907
+ }
14908
+ } catch {
14909
+ }
14910
+ }
14911
+ function patchLifecycleMethods(session) {
14912
+ patchLifecycleMethod(session, "suspendTurn");
14913
+ patchLifecycleMethod(session, "detach");
14914
+ patchLifecycleMethod(session, "stop");
14915
+ }
14916
+ function registerHarnessTurnSpan(args) {
14917
+ if (!isObject(args.session)) {
14918
+ return;
14919
+ }
14920
+ const session = args.session;
14921
+ sessionTurnParents.set(session, args.span);
14922
+ wrapperTurnParents.set(args.span, args.span);
14923
+ patchLifecycleMethods(session);
14924
+ }
14925
+ function harnessContinuationParent(session) {
14926
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
14927
+ }
14928
+ function captureHarnessCreateSessionParent(params) {
14929
+ try {
14930
+ return continuationParentFromCreateSessionParams(params);
14931
+ } catch {
14932
+ return void 0;
14933
+ }
14934
+ }
14935
+ function registerHarnessSessionParent(session, parent) {
14936
+ if (!parent || !isObject(session)) {
14937
+ return;
14938
+ }
14939
+ const harnessSession = session;
14940
+ sessionTurnParents.set(harnessSession, parent);
14941
+ patchLifecycleMethods(harnessSession);
14942
+ }
14943
+ function currentHarnessTurnParent() {
14944
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
14945
+ }
14946
+ function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
14947
+ const startChannel = tracingChannel2.start;
14948
+ if (!startChannel) {
14949
+ return () => {
14950
+ };
14951
+ }
14952
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
14953
+ const store = harnessTurnParentStore;
14954
+ startChannel.bindStore(
14955
+ store,
14956
+ (event) => parentFromEvent(event) ?? store.getStore()
14957
+ );
14958
+ return () => {
14959
+ startChannel.unbindStore(store);
14960
+ };
14961
+ }
14962
+ function startHarnessTurnChildSpan(parent, args) {
14963
+ const spanArgs = withSpanInstrumentationName(
14964
+ args,
14965
+ INSTRUMENTATION_NAMES.AI_SDK
14966
+ );
14967
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
14968
+ }
14969
+ function updateHarnessTurn(parent, update = {}) {
14970
+ if (!parent) {
14971
+ return;
14972
+ }
14973
+ const log3 = (event) => {
14974
+ if (typeof parent === "string") {
14975
+ updateSpan({ exported: parent, ...event });
14976
+ } else {
14977
+ parent.log(event);
14978
+ }
14979
+ };
14980
+ try {
14981
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
14982
+ log3({ output: null });
14983
+ }
14984
+ log3(update);
14985
+ } catch {
14986
+ }
14987
+ }
14988
+ function endHarnessTurn(parent) {
14989
+ const endTime = getCurrentUnixTimestamp();
14990
+ if (!parent) {
14991
+ return endTime;
14992
+ }
14993
+ try {
14994
+ const event = { metrics: { end: endTime } };
14995
+ if (typeof parent === "string") {
14996
+ updateSpan({ exported: parent, ...event });
14997
+ } else {
14998
+ parent.log(event);
14999
+ }
15000
+ } catch {
15001
+ }
15002
+ return endTime;
15003
+ }
15004
+
14446
15005
  // src/wrappers/ai-sdk/telemetry.ts
14447
15006
  function braintrustAISDKTelemetry() {
14448
15007
  const operations = /* @__PURE__ */ new Map();
@@ -14461,14 +15020,17 @@ function braintrustAISDKTelemetry() {
14461
15020
  console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
14462
15021
  }
14463
15022
  };
14464
- const startChildSpan = (operationKey, name, type, event) => {
15023
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
14465
15024
  const parent = operations.get(operationKey)?.span;
14466
- const spanArgs = {
14467
- name,
14468
- spanAttributes: { type },
14469
- ...event ? { event } : {}
14470
- };
14471
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
15025
+ const spanArgs = withSpanInstrumentationName(
15026
+ {
15027
+ name,
15028
+ spanAttributes: { type },
15029
+ ...event ? { event } : {}
15030
+ },
15031
+ INSTRUMENTATION_NAMES.AI_SDK
15032
+ );
15033
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
14472
15034
  const state = operations.get(operationKey);
14473
15035
  if (state && type === "llm" /* LLM */) {
14474
15036
  state.hadModelChild = true;
@@ -14748,14 +15310,30 @@ function braintrustAISDKTelemetry() {
14748
15310
  const workflowAgent = operationName === "WorkflowAgent.stream";
14749
15311
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
14750
15312
  const ownsSpan = !wrapperSpan;
14751
- const span = ownsSpan ? startSpan({
14752
- name: operationName,
14753
- spanAttributes: { type: "function" /* FUNCTION */ }
14754
- }) : wrapperSpan;
15313
+ const harnessTurnParent = currentHarnessTurnParent();
15314
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
15315
+ harnessTurnParent,
15316
+ withSpanInstrumentationName(
15317
+ {
15318
+ name: operationName,
15319
+ spanAttributes: { type: "function" /* FUNCTION */ }
15320
+ },
15321
+ INSTRUMENTATION_NAMES.AI_SDK
15322
+ )
15323
+ ) : startSpan(
15324
+ withSpanInstrumentationName(
15325
+ {
15326
+ name: operationName,
15327
+ spanAttributes: { type: "function" /* FUNCTION */ }
15328
+ },
15329
+ INSTRUMENTATION_NAMES.AI_SDK
15330
+ )
15331
+ ) : wrapperSpan;
14755
15332
  const operationKey = createOperationKey(event, operationName);
14756
15333
  registerOperation({
14757
15334
  callId: event.callId,
14758
15335
  hadModelChild: false,
15336
+ harnessTurnParent,
14759
15337
  loggedInput: false,
14760
15338
  operationName,
14761
15339
  operationKey,
@@ -14996,7 +15574,8 @@ function braintrustAISDKTelemetry() {
14996
15574
  toolCallId,
14997
15575
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
14998
15576
  }
14999
- }
15577
+ },
15578
+ state?.harnessTurnParent
15000
15579
  );
15001
15580
  toolSpans.set(toolCallId, { operationKey, span });
15002
15581
  });
@@ -15232,74 +15811,102 @@ function finishOutput(result, operationName) {
15232
15811
  }
15233
15812
 
15234
15813
  // src/instrumentation/plugins/ai-sdk-channels.ts
15235
- var aiSDKChannels = defineChannels("ai", {
15236
- generateText: channel({
15237
- channelName: "generateText",
15238
- kind: "async"
15239
- }),
15240
- streamText: channel({
15241
- channelName: "streamText",
15242
- kind: "async"
15243
- }),
15244
- streamTextSync: channel({
15245
- channelName: "streamText.sync",
15246
- kind: "sync-stream"
15247
- }),
15248
- generateObject: channel({
15249
- channelName: "generateObject",
15250
- kind: "async"
15251
- }),
15252
- streamObject: channel({
15253
- channelName: "streamObject",
15254
- kind: "async"
15255
- }),
15256
- streamObjectSync: channel({
15257
- channelName: "streamObject.sync",
15258
- kind: "sync-stream"
15259
- }),
15260
- embed: channel(
15261
- {
15814
+ var aiSDKChannels = defineChannels(
15815
+ "ai",
15816
+ {
15817
+ generateText: channel({
15818
+ channelName: "generateText",
15819
+ kind: "async"
15820
+ }),
15821
+ streamText: channel({
15822
+ channelName: "streamText",
15823
+ kind: "async"
15824
+ }),
15825
+ streamTextSync: channel({
15826
+ channelName: "streamText.sync",
15827
+ kind: "sync-stream"
15828
+ }),
15829
+ generateObject: channel({
15830
+ channelName: "generateObject",
15831
+ kind: "async"
15832
+ }),
15833
+ streamObject: channel({
15834
+ channelName: "streamObject",
15835
+ kind: "async"
15836
+ }),
15837
+ streamObjectSync: channel({
15838
+ channelName: "streamObject.sync",
15839
+ kind: "sync-stream"
15840
+ }),
15841
+ embed: channel({
15262
15842
  channelName: "embed",
15263
15843
  kind: "async"
15264
- }
15265
- ),
15266
- embedMany: channel({
15267
- channelName: "embedMany",
15268
- kind: "async"
15269
- }),
15270
- rerank: channel({
15271
- channelName: "rerank",
15272
- kind: "async"
15273
- }),
15274
- agentGenerate: channel({
15275
- channelName: "Agent.generate",
15276
- kind: "async"
15277
- }),
15278
- agentStream: channel({
15279
- channelName: "Agent.stream",
15280
- kind: "async"
15281
- }),
15282
- agentStreamSync: channel({
15283
- channelName: "Agent.stream.sync",
15284
- kind: "sync-stream"
15285
- }),
15286
- toolLoopAgentGenerate: channel({
15287
- channelName: "ToolLoopAgent.generate",
15288
- kind: "async"
15289
- }),
15290
- toolLoopAgentStream: channel({
15291
- channelName: "ToolLoopAgent.stream",
15292
- kind: "async"
15293
- }),
15294
- workflowAgentStream: channel({
15295
- channelName: "WorkflowAgent.stream",
15296
- kind: "async"
15297
- }),
15298
- v7CreateTelemetryDispatcher: channel({
15299
- channelName: "createTelemetryDispatcher",
15300
- kind: "sync-stream"
15301
- })
15302
- });
15844
+ }),
15845
+ embedMany: channel({
15846
+ channelName: "embedMany",
15847
+ kind: "async"
15848
+ }),
15849
+ rerank: channel({
15850
+ channelName: "rerank",
15851
+ kind: "async"
15852
+ }),
15853
+ agentGenerate: channel({
15854
+ channelName: "Agent.generate",
15855
+ kind: "async"
15856
+ }),
15857
+ agentStream: channel({
15858
+ channelName: "Agent.stream",
15859
+ kind: "async"
15860
+ }),
15861
+ agentStreamSync: channel({
15862
+ channelName: "Agent.stream.sync",
15863
+ kind: "sync-stream"
15864
+ }),
15865
+ toolLoopAgentGenerate: channel({
15866
+ channelName: "ToolLoopAgent.generate",
15867
+ kind: "async"
15868
+ }),
15869
+ toolLoopAgentStream: channel({
15870
+ channelName: "ToolLoopAgent.stream",
15871
+ kind: "async"
15872
+ }),
15873
+ workflowAgentStream: channel({
15874
+ channelName: "WorkflowAgent.stream",
15875
+ kind: "async"
15876
+ }),
15877
+ v7CreateTelemetryDispatcher: channel({
15878
+ channelName: "createTelemetryDispatcher",
15879
+ kind: "sync-stream"
15880
+ })
15881
+ },
15882
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
15883
+ );
15884
+ var harnessAgentChannels = defineChannels(
15885
+ "@ai-sdk/harness",
15886
+ {
15887
+ createSession: channel({
15888
+ channelName: "HarnessAgent.createSession",
15889
+ kind: "async"
15890
+ }),
15891
+ generate: channel({
15892
+ channelName: "HarnessAgent.generate",
15893
+ kind: "async"
15894
+ }),
15895
+ stream: channel({
15896
+ channelName: "HarnessAgent.stream",
15897
+ kind: "async"
15898
+ }),
15899
+ continueGenerate: channel({
15900
+ channelName: "HarnessAgent.continueGenerate",
15901
+ kind: "async"
15902
+ }),
15903
+ continueStream: channel({
15904
+ channelName: "HarnessAgent.continueStream",
15905
+ kind: "async"
15906
+ })
15907
+ },
15908
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
15909
+ );
15303
15910
 
15304
15911
  // src/instrumentation/plugins/ai-sdk-plugin.ts
15305
15912
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -15376,6 +15983,17 @@ var AISDKPlugin = class extends BasePlugin {
15376
15983
  subscribeToAISDK() {
15377
15984
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
15378
15985
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
15986
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
15987
+ this.unsubscribers.push(
15988
+ subscribeToHarnessContinuation(
15989
+ harnessAgentChannels.continueGenerate,
15990
+ denyOutputPaths
15991
+ ),
15992
+ subscribeToHarnessContinuation(
15993
+ harnessAgentChannels.continueStream,
15994
+ denyOutputPaths
15995
+ )
15996
+ );
15379
15997
  this.unsubscribers.push(
15380
15998
  traceStreamingChannel(aiSDKChannels.generateText, {
15381
15999
  name: "generateText",
@@ -15568,6 +16186,90 @@ var AISDKPlugin = class extends BasePlugin {
15568
16186
  })
15569
16187
  })
15570
16188
  );
16189
+ this.unsubscribers.push(
16190
+ traceStreamingChannel(harnessAgentChannels.generate, {
16191
+ name: "HarnessAgent.generate",
16192
+ startSpan: _internalStartSpanWithInitialMerge,
16193
+ type: "task" /* TASK */,
16194
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16195
+ extractOutput: (result, endEvent) => processAISDKOutput(
16196
+ result,
16197
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16198
+ ),
16199
+ extractMetrics: (result) => extractTokenMetrics(result),
16200
+ aggregateChunks: aggregateAISDKChunks
16201
+ })
16202
+ );
16203
+ this.unsubscribers.push(
16204
+ traceStreamingChannel(harnessAgentChannels.stream, {
16205
+ name: "HarnessAgent.stream",
16206
+ startSpan: _internalStartSpanWithInitialMerge,
16207
+ type: "task" /* TASK */,
16208
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16209
+ extractOutput: (result, endEvent) => processAISDKOutput(
16210
+ result,
16211
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16212
+ ),
16213
+ extractMetrics: (result, startTime) => ({
16214
+ ...extractTokenMetrics(result),
16215
+ ...startTime === void 0 ? {} : {
16216
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16217
+ }
16218
+ }),
16219
+ aggregateChunks: aggregateAISDKChunks,
16220
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16221
+ defaultDenyOutputPaths: denyOutputPaths,
16222
+ endEvent,
16223
+ result,
16224
+ resolvePromiseUsage: true,
16225
+ span,
16226
+ startTime
16227
+ })
16228
+ })
16229
+ );
16230
+ this.unsubscribers.push(
16231
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
16232
+ name: "HarnessAgent.continueGenerate",
16233
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16234
+ startSpan: _internalStartSpanWithInitialMerge,
16235
+ type: "task" /* TASK */,
16236
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16237
+ extractOutput: (result, endEvent) => processAISDKOutput(
16238
+ result,
16239
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16240
+ ),
16241
+ extractMetrics: (result) => extractTokenMetrics(result),
16242
+ aggregateChunks: aggregateAISDKChunks
16243
+ })
16244
+ );
16245
+ this.unsubscribers.push(
16246
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
16247
+ name: "HarnessAgent.continueStream",
16248
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16249
+ startSpan: _internalStartSpanWithInitialMerge,
16250
+ type: "task" /* TASK */,
16251
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16252
+ extractOutput: (result, endEvent) => processAISDKOutput(
16253
+ result,
16254
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16255
+ ),
16256
+ extractMetrics: (result, startTime) => ({
16257
+ ...extractTokenMetrics(result),
16258
+ ...startTime === void 0 ? {} : {
16259
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16260
+ }
16261
+ }),
16262
+ aggregateChunks: aggregateAISDKChunks,
16263
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16264
+ defaultDenyOutputPaths: denyOutputPaths,
16265
+ endEvent,
16266
+ result,
16267
+ resolvePromiseUsage: true,
16268
+ span,
16269
+ startTime
16270
+ })
16271
+ })
16272
+ );
15571
16273
  this.unsubscribers.push(
15572
16274
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
15573
16275
  name: "ToolLoopAgent.generate",
@@ -15638,6 +16340,8 @@ var AISDKPlugin = class extends BasePlugin {
15638
16340
  defaultDenyOutputPaths: denyOutputPaths,
15639
16341
  endEvent,
15640
16342
  onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
16343
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
16344
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
15641
16345
  result,
15642
16346
  span,
15643
16347
  startTime
@@ -15646,6 +16350,153 @@ var AISDKPlugin = class extends BasePlugin {
15646
16350
  );
15647
16351
  }
15648
16352
  };
16353
+ function subscribeToHarnessAgentCreateSession() {
16354
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
16355
+ const parents = /* @__PURE__ */ new WeakMap();
16356
+ const handlers = {
16357
+ start: (event) => {
16358
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
16359
+ if (parent) {
16360
+ parents.set(event, parent);
16361
+ }
16362
+ },
16363
+ asyncEnd: (event) => {
16364
+ registerHarnessSessionParent(event.result, parents.get(event));
16365
+ parents.delete(event);
16366
+ },
16367
+ error: (event) => {
16368
+ parents.delete(event);
16369
+ }
16370
+ };
16371
+ channel2.subscribe(handlers);
16372
+ return () => channel2.unsubscribe(handlers);
16373
+ }
16374
+ function harnessContinuationParentFromEvent(event) {
16375
+ try {
16376
+ return harnessContinuationParent(event.arguments?.[0]?.session);
16377
+ } catch {
16378
+ return void 0;
16379
+ }
16380
+ }
16381
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
16382
+ const channel2 = continuationChannel.tracingChannel();
16383
+ const parents = /* @__PURE__ */ new WeakMap();
16384
+ const startTimes = /* @__PURE__ */ new WeakMap();
16385
+ const unbindParentStore = bindHarnessTurnParentToStart(
16386
+ channel2,
16387
+ harnessContinuationParentFromEvent
16388
+ );
16389
+ const handlers = {
16390
+ start: (event) => {
16391
+ const parent = harnessContinuationParentFromEvent(event);
16392
+ if (!parent) {
16393
+ return;
16394
+ }
16395
+ parents.set(event, parent);
16396
+ startTimes.set(event, getCurrentUnixTimestamp());
16397
+ try {
16398
+ const params = event.arguments?.[0];
16399
+ if (params) {
16400
+ prepareAISDKHarnessAgentInput(params, event.self);
16401
+ }
16402
+ } catch (error) {
16403
+ debugLogger.error(
16404
+ "Error preparing Harness continuation telemetry:",
16405
+ error
16406
+ );
16407
+ }
16408
+ },
16409
+ asyncEnd: (event) => {
16410
+ const parent = parents.get(event);
16411
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
16412
+ parents.delete(event);
16413
+ startTimes.delete(event);
16414
+ if (!parent) {
16415
+ return;
16416
+ }
16417
+ const endEvent = event;
16418
+ const span = {
16419
+ end: () => endHarnessTurn(parent),
16420
+ log: (update) => updateHarnessTurn(
16421
+ parent,
16422
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
16423
+ )
16424
+ };
16425
+ try {
16426
+ if (isAsyncIterable(endEvent.result)) {
16427
+ patchStreamIfNeeded(endEvent.result, {
16428
+ onComplete: (chunks) => {
16429
+ try {
16430
+ const { metadata, metrics, output } = aggregateAISDKChunks(
16431
+ chunks,
16432
+ endEvent.result,
16433
+ endEvent
16434
+ );
16435
+ span.log({
16436
+ ...metadata ? { metadata } : {},
16437
+ metrics,
16438
+ output
16439
+ });
16440
+ } catch (error) {
16441
+ debugLogger.error(
16442
+ "Error aggregating Harness continuation stream:",
16443
+ error
16444
+ );
16445
+ } finally {
16446
+ span.end();
16447
+ }
16448
+ },
16449
+ onError: (error) => {
16450
+ span.log({ error: toLoggedError(error), output: null });
16451
+ span.end();
16452
+ }
16453
+ });
16454
+ return;
16455
+ }
16456
+ if (patchAISDKStreamingResult({
16457
+ defaultDenyOutputPaths,
16458
+ endEvent,
16459
+ result: endEvent.result,
16460
+ resolvePromiseUsage: true,
16461
+ span,
16462
+ startTime
16463
+ })) {
16464
+ return;
16465
+ }
16466
+ finalizeAISDKChildTracing(endEvent);
16467
+ span.log({
16468
+ metrics: extractTokenMetrics(endEvent.result),
16469
+ output: processAISDKOutput(
16470
+ endEvent.result,
16471
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16472
+ )
16473
+ });
16474
+ span.end();
16475
+ } catch (error) {
16476
+ debugLogger.error("Error tracing Harness continuation:", error);
16477
+ span.end();
16478
+ }
16479
+ },
16480
+ error: (event) => {
16481
+ const parent = parents.get(event);
16482
+ parents.delete(event);
16483
+ startTimes.delete(event);
16484
+ if (!parent) {
16485
+ return;
16486
+ }
16487
+ updateHarnessTurn(parent, {
16488
+ error: toLoggedError(event.error),
16489
+ output: null
16490
+ });
16491
+ endHarnessTurn(parent);
16492
+ }
16493
+ };
16494
+ channel2.subscribe(handlers);
16495
+ return () => {
16496
+ unbindParentStore();
16497
+ channel2.unsubscribe(handlers);
16498
+ };
16499
+ }
15649
16500
  function subscribeToAISDKV7TelemetryDispatcher() {
15650
16501
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
15651
16502
  const telemetry = braintrustAISDKTelemetry();
@@ -16098,6 +16949,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
16098
16949
  metadata
16099
16950
  };
16100
16951
  }
16952
+ function prepareAISDKHarnessAgentInput(params, self, span) {
16953
+ if (span) {
16954
+ registerHarnessTurnSpan({
16955
+ session: params.session,
16956
+ span
16957
+ });
16958
+ }
16959
+ if (isObject(self)) {
16960
+ try {
16961
+ if (isObject(self.settings)) {
16962
+ const settings = self.settings;
16963
+ if (settings.telemetry === void 0) {
16964
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
16965
+ }
16966
+ }
16967
+ } catch {
16968
+ }
16969
+ }
16970
+ const selectedInput = {};
16971
+ if (params.prompt !== void 0) {
16972
+ selectedInput.prompt = params.prompt;
16973
+ }
16974
+ if (params.messages !== void 0) {
16975
+ selectedInput.messages = params.messages;
16976
+ }
16977
+ if (params.toolApprovalContinuations !== void 0) {
16978
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
16979
+ }
16980
+ const metadata = extractMetadataFromCallParams({}, self);
16981
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
16982
+ metadata.sessionId = params.session.sessionId;
16983
+ }
16984
+ if (isObject(self)) {
16985
+ if (typeof self.harnessId === "string") {
16986
+ metadata.harnessId = self.harnessId;
16987
+ }
16988
+ if (typeof self.permissionMode === "string") {
16989
+ metadata.permissionMode = self.permissionMode;
16990
+ }
16991
+ }
16992
+ return {
16993
+ input: processAISDKCallInput(selectedInput).input,
16994
+ metadata
16995
+ };
16996
+ }
16997
+ function harnessSessionFromArguments(args) {
16998
+ const params = args[0];
16999
+ return isObject(params) ? params.session : void 0;
17000
+ }
16101
17001
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
16102
17002
  registerWorkflowAgentWrapperSpan(span);
16103
17003
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -16545,19 +17445,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16545
17445
  activeEntry.openSpans.delete(span);
16546
17446
  }
16547
17447
  },
16548
- {
16549
- name: "doGenerate",
16550
- spanAttributes: {
16551
- type: "llm" /* LLM */
17448
+ withSpanInstrumentationName(
17449
+ {
17450
+ name: "doGenerate",
17451
+ spanAttributes: {
17452
+ type: "llm" /* LLM */
17453
+ },
17454
+ event: buildAISDKModelStartEvent(
17455
+ callOptions,
17456
+ activeEntry.baseMetadata,
17457
+ {
17458
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17459
+ }
17460
+ )
16552
17461
  },
16553
- event: buildAISDKModelStartEvent(
16554
- callOptions,
16555
- activeEntry.baseMetadata,
16556
- {
16557
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16558
- }
16559
- )
16560
- }
17462
+ INSTRUMENTATION_NAMES.AI_SDK
17463
+ )
16561
17464
  );
16562
17465
  };
16563
17466
  if (originalDoStream) {
@@ -16567,19 +17470,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16567
17470
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
16568
17471
  }
16569
17472
  closeOpenAISDKModelPatchSpans(activeEntry);
16570
- const span = activeEntry.parentSpan.startSpan({
16571
- name: "doStream",
16572
- spanAttributes: {
16573
- type: "llm" /* LLM */
16574
- },
16575
- event: buildAISDKModelStartEvent(
16576
- callOptions,
16577
- activeEntry.baseMetadata,
17473
+ const span = activeEntry.parentSpan.startSpan(
17474
+ withSpanInstrumentationName(
16578
17475
  {
16579
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16580
- }
17476
+ name: "doStream",
17477
+ spanAttributes: {
17478
+ type: "llm" /* LLM */
17479
+ },
17480
+ event: buildAISDKModelStartEvent(
17481
+ callOptions,
17482
+ activeEntry.baseMetadata,
17483
+ {
17484
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17485
+ }
17486
+ )
17487
+ },
17488
+ INSTRUMENTATION_NAMES.AI_SDK
16581
17489
  )
16582
- });
17490
+ );
16583
17491
  activeEntry.openSpans.add(span);
16584
17492
  const streamStartTime = getCurrentUnixTimestamp();
16585
17493
  const result = await withCurrent(
@@ -16751,12 +17659,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16751
17659
  }
16752
17660
  if (isAsyncGenerator(result)) {
16753
17661
  return (async function* () {
16754
- const span = activeEntry.parentSpan.startSpan({
16755
- name: activeEntry.name,
16756
- spanAttributes: {
16757
- type: "tool" /* TOOL */
16758
- }
16759
- });
17662
+ const span = activeEntry.parentSpan.startSpan(
17663
+ withSpanInstrumentationName(
17664
+ {
17665
+ name: activeEntry.name,
17666
+ spanAttributes: {
17667
+ type: "tool" /* TOOL */
17668
+ }
17669
+ },
17670
+ INSTRUMENTATION_NAMES.AI_SDK
17671
+ )
17672
+ );
16760
17673
  span.log({ input: serializeToolExecutionInput(args) });
16761
17674
  try {
16762
17675
  let lastValue;
@@ -16780,12 +17693,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16780
17693
  span.log({ output: awaitedResult });
16781
17694
  return awaitedResult;
16782
17695
  },
16783
- {
16784
- name: activeEntry.name,
16785
- spanAttributes: {
16786
- type: "tool" /* TOOL */
16787
- }
16788
- }
17696
+ withSpanInstrumentationName(
17697
+ {
17698
+ name: activeEntry.name,
17699
+ spanAttributes: {
17700
+ type: "tool" /* TOOL */
17701
+ }
17702
+ },
17703
+ INSTRUMENTATION_NAMES.AI_SDK
17704
+ )
16789
17705
  );
16790
17706
  };
16791
17707
  cleanup.push(() => {
@@ -16944,7 +17860,10 @@ function patchAISDKStreamingResult(args) {
16944
17860
  defaultDenyOutputPaths,
16945
17861
  endEvent,
16946
17862
  onComplete,
17863
+ onCancel,
17864
+ onError,
16947
17865
  result,
17866
+ resolvePromiseUsage,
16948
17867
  span,
16949
17868
  startTime
16950
17869
  } = args;
@@ -16954,14 +17873,43 @@ function patchAISDKStreamingResult(args) {
16954
17873
  const resultRecord = result;
16955
17874
  attachKnownResultPromiseHandlers(resultRecord);
16956
17875
  let finalized = false;
16957
- const finalize = () => {
17876
+ const finalize = (outcome) => {
16958
17877
  if (finalized) {
16959
17878
  return;
16960
17879
  }
16961
17880
  finalized = true;
16962
- finalizeAISDKChildTracing(endEvent);
16963
- span.end();
16964
- onComplete?.();
17881
+ try {
17882
+ finalizeAISDKChildTracing(endEvent);
17883
+ } catch (error) {
17884
+ debugLogger.error("Error finalizing AI SDK child tracing:", error);
17885
+ }
17886
+ try {
17887
+ span.end();
17888
+ } catch (error) {
17889
+ debugLogger.error("Error ending AI SDK streaming span:", error);
17890
+ }
17891
+ if (outcome && "error" in outcome) {
17892
+ try {
17893
+ onError?.(outcome.error);
17894
+ } catch (error) {
17895
+ debugLogger.error("Error in AI SDK streaming error hook:", error);
17896
+ }
17897
+ } else if (outcome) {
17898
+ try {
17899
+ onComplete?.(outcome);
17900
+ } catch (error) {
17901
+ debugLogger.error("Error in AI SDK streaming completion hook:", error);
17902
+ }
17903
+ } else {
17904
+ try {
17905
+ onCancel?.();
17906
+ } catch (error) {
17907
+ debugLogger.error(
17908
+ "Error in AI SDK streaming cancellation hook:",
17909
+ error
17910
+ );
17911
+ }
17912
+ }
16965
17913
  };
16966
17914
  let outputLogged = false;
16967
17915
  const logStreamingOutput = async (firstChunkTime) => {
@@ -16971,6 +17919,22 @@ function patchAISDKStreamingResult(args) {
16971
17919
  outputLogged = true;
16972
17920
  try {
16973
17921
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
17922
+ if (resolvePromiseUsage) {
17923
+ try {
17924
+ const resultRecord2 = result;
17925
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
17926
+ if (isPromiseLike(pendingUsage)) {
17927
+ const usage = await Promise.resolve(pendingUsage);
17928
+ if (isObject(usage)) {
17929
+ Object.assign(
17930
+ metrics,
17931
+ extractTokenMetrics({ usage })
17932
+ );
17933
+ }
17934
+ }
17935
+ } catch {
17936
+ }
17937
+ }
16974
17938
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
16975
17939
  metrics.time_to_first_token = firstChunkTime - startTime;
16976
17940
  }
@@ -16979,15 +17943,29 @@ function patchAISDKStreamingResult(args) {
16979
17943
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16980
17944
  );
16981
17945
  const metadata = buildResolvedMetadataPayload(result).metadata;
16982
- span.log({
16983
- output,
16984
- ...metadata ? { metadata } : {},
16985
- metrics
16986
- });
17946
+ try {
17947
+ span.log({
17948
+ output,
17949
+ ...metadata ? { metadata } : {},
17950
+ metrics
17951
+ });
17952
+ } catch (error) {
17953
+ debugLogger.error("Error logging AI SDK streaming output:", error);
17954
+ }
17955
+ finalize({ metrics, output });
16987
17956
  } catch (error) {
16988
- span.log({ error: toLoggedError(error) });
16989
- } finally {
16990
- finalize();
17957
+ const loggedError = toLoggedError(error);
17958
+ try {
17959
+ span.log({ error: loggedError });
17960
+ } catch (loggingError) {
17961
+ debugLogger.error(
17962
+ "Error logging AI SDK streaming failure:",
17963
+ loggingError
17964
+ );
17965
+ }
17966
+ finalize({
17967
+ error: error instanceof Error ? error : new Error(String(loggedError))
17968
+ });
16991
17969
  }
16992
17970
  };
16993
17971
  const createAsyncIterableHooks = () => {
@@ -17004,11 +17982,16 @@ function patchAISDKStreamingResult(args) {
17004
17982
  onError: (error) => {
17005
17983
  if (!outputLogged) {
17006
17984
  outputLogged = true;
17007
- span.log({
17008
- error: error.message
17009
- });
17985
+ try {
17986
+ span.log({ error });
17987
+ } catch (loggingError) {
17988
+ debugLogger.error(
17989
+ "Error logging AI SDK stream failure:",
17990
+ loggingError
17991
+ );
17992
+ }
17010
17993
  }
17011
- finalize();
17994
+ finalize({ error });
17012
17995
  },
17013
17996
  onCancel: finalize
17014
17997
  };
@@ -17094,8 +18077,18 @@ function patchAISDKStreamingResult(args) {
17094
18077
  }
17095
18078
  controller.enqueue(value);
17096
18079
  } catch (error) {
17097
- span.log({ error: toLoggedError(error) });
17098
- finalize();
18080
+ const loggedError = toLoggedError(error);
18081
+ try {
18082
+ span.log({ error: loggedError });
18083
+ } catch (loggingError) {
18084
+ debugLogger.error(
18085
+ "Error logging AI SDK base stream failure:",
18086
+ loggingError
18087
+ );
18088
+ }
18089
+ finalize({
18090
+ error: error instanceof Error ? error : new Error(String(loggedError))
18091
+ });
17099
18092
  controller.error(error);
17100
18093
  }
17101
18094
  },
@@ -17884,7 +18877,8 @@ var claudeAgentSDKChannels = defineChannels(
17884
18877
  channelName: "query",
17885
18878
  kind: "sync-stream"
17886
18879
  })
17887
- }
18880
+ },
18881
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
17888
18882
  );
17889
18883
 
17890
18884
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -18032,22 +19026,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
18032
19026
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
18033
19027
  const runWithResolvedParent = async () => {
18034
19028
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
18035
- const span = startSpan({
18036
- event: {
18037
- input: handlerArgs[0],
18038
- metadata: {
18039
- "claude_agent_sdk.raw_tool_name": rawToolName,
18040
- "gen_ai.tool.name": metadata.toolName,
18041
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
18042
- ...metadata.serverName && {
18043
- "mcp.server": metadata.serverName
18044
- }
18045
- }
18046
- },
18047
- name: spanName,
18048
- ...parent && { parent },
18049
- spanAttributes: { type: "tool" /* TOOL */ }
18050
- });
19029
+ const span = startSpan(
19030
+ withSpanInstrumentationName(
19031
+ {
19032
+ event: {
19033
+ input: handlerArgs[0],
19034
+ metadata: {
19035
+ "claude_agent_sdk.raw_tool_name": rawToolName,
19036
+ "gen_ai.tool.name": metadata.toolName,
19037
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
19038
+ ...metadata.serverName && {
19039
+ "mcp.server": metadata.serverName
19040
+ }
19041
+ }
19042
+ },
19043
+ name: spanName,
19044
+ ...parent && { parent },
19045
+ spanAttributes: { type: "tool" /* TOOL */ }
19046
+ },
19047
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19048
+ )
19049
+ );
18051
19050
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
18052
19051
  const finalizeSuccess = (result) => {
18053
19052
  span.log({ output: result });
@@ -18372,14 +19371,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
18372
19371
  ).filter(
18373
19372
  (c) => c !== void 0
18374
19373
  );
18375
- const span = existingSpan ?? startSpan({
18376
- name: "anthropic.messages.create",
18377
- parent: parentSpan,
18378
- spanAttributes: {
18379
- type: "llm" /* LLM */
18380
- },
18381
- startTime
18382
- });
19374
+ const span = existingSpan ?? startSpan(
19375
+ withSpanInstrumentationName(
19376
+ {
19377
+ name: "anthropic.messages.create",
19378
+ parent: parentSpan,
19379
+ spanAttributes: {
19380
+ type: "llm" /* LLM */
19381
+ },
19382
+ startTime
19383
+ },
19384
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19385
+ )
19386
+ );
18383
19387
  span.log({
18384
19388
  input,
18385
19389
  metadata: model ? { model } : void 0,
@@ -18482,25 +19486,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
18482
19486
  return {};
18483
19487
  }
18484
19488
  const parsed = parseToolName(input.tool_name);
18485
- const toolSpan = startSpan({
18486
- event: {
18487
- input: input.tool_input,
18488
- metadata: {
18489
- "claude_agent_sdk.cwd": input.cwd,
18490
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
18491
- "claude_agent_sdk.session_id": input.session_id,
18492
- "gen_ai.tool.call.id": toolUseID,
18493
- "gen_ai.tool.name": parsed.toolName,
18494
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
18495
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
18496
- }
18497
- },
18498
- name: parsed.displayName,
18499
- parent: await resolveParentSpan(toolUseID, {
18500
- agentId: input.agent_id
18501
- }),
18502
- spanAttributes: { type: "tool" /* TOOL */ }
18503
- });
19489
+ const toolSpan = startSpan(
19490
+ withSpanInstrumentationName(
19491
+ {
19492
+ event: {
19493
+ input: input.tool_input,
19494
+ metadata: {
19495
+ "claude_agent_sdk.cwd": input.cwd,
19496
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
19497
+ "claude_agent_sdk.session_id": input.session_id,
19498
+ "gen_ai.tool.call.id": toolUseID,
19499
+ "gen_ai.tool.name": parsed.toolName,
19500
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
19501
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
19502
+ }
19503
+ },
19504
+ name: parsed.displayName,
19505
+ parent: await resolveParentSpan(toolUseID, {
19506
+ agentId: input.agent_id
19507
+ }),
19508
+ spanAttributes: { type: "tool" /* TOOL */ }
19509
+ },
19510
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19511
+ )
19512
+ );
18504
19513
  activeToolSpans.set(toolUseID, toolSpan);
18505
19514
  return {};
18506
19515
  };
@@ -18866,14 +19875,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
18866
19875
  const spanName = formatSubAgentSpanName(details);
18867
19876
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
18868
19877
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
18869
- const subAgentSpan = startSpan({
18870
- event: {
18871
- metadata: subAgentDetailsToMetadata(details)
18872
- },
18873
- name: spanName,
18874
- parent: parentSpan,
18875
- spanAttributes: { type: "task" /* TASK */ }
18876
- });
19878
+ const subAgentSpan = startSpan(
19879
+ withSpanInstrumentationName(
19880
+ {
19881
+ event: {
19882
+ metadata: subAgentDetailsToMetadata(details)
19883
+ },
19884
+ name: spanName,
19885
+ parent: parentSpan,
19886
+ spanAttributes: { type: "task" /* TASK */ }
19887
+ },
19888
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19889
+ )
19890
+ );
18877
19891
  subAgentSpans.set(parentToolUseId, subAgentSpan);
18878
19892
  return subAgentSpan;
18879
19893
  }
@@ -18894,14 +19908,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
18894
19908
  );
18895
19909
  llmParentSpan = await subAgentSpan.export();
18896
19910
  }
18897
- const llmSpan = startSpan({
18898
- name: "anthropic.messages.create",
18899
- parent: llmParentSpan,
18900
- spanAttributes: {
18901
- type: "llm" /* LLM */
18902
- },
18903
- startTime
18904
- });
19911
+ const llmSpan = startSpan(
19912
+ withSpanInstrumentationName(
19913
+ {
19914
+ name: "anthropic.messages.create",
19915
+ parent: llmParentSpan,
19916
+ spanAttributes: {
19917
+ type: "llm" /* LLM */
19918
+ },
19919
+ startTime
19920
+ },
19921
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19922
+ )
19923
+ );
18905
19924
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
18906
19925
  return llmSpan;
18907
19926
  }
@@ -19170,12 +20189,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19170
20189
  }
19171
20190
  })();
19172
20191
  }
19173
- const span = startSpan({
19174
- name: "Claude Agent",
19175
- spanAttributes: {
19176
- type: "task" /* TASK */
19177
- }
19178
- });
20192
+ const span = startSpan(
20193
+ withSpanInstrumentationName(
20194
+ {
20195
+ name: "Claude Agent",
20196
+ spanAttributes: {
20197
+ type: "task" /* TASK */
20198
+ }
20199
+ },
20200
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
20201
+ )
20202
+ );
19179
20203
  const startTime = getCurrentUnixTimestamp();
19180
20204
  try {
19181
20205
  span.log({
@@ -19340,24 +20364,28 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19340
20364
  };
19341
20365
 
19342
20366
  // src/instrumentation/plugins/cursor-sdk-channels.ts
19343
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
19344
- create: channel({
19345
- channelName: "Agent.create",
19346
- kind: "async"
19347
- }),
19348
- resume: channel({
19349
- channelName: "Agent.resume",
19350
- kind: "async"
19351
- }),
19352
- prompt: channel({
19353
- channelName: "Agent.prompt",
19354
- kind: "async"
19355
- }),
19356
- send: channel({
19357
- channelName: "agent.send",
19358
- kind: "async"
19359
- })
19360
- });
20367
+ var cursorSDKChannels = defineChannels(
20368
+ "@cursor/sdk",
20369
+ {
20370
+ create: channel({
20371
+ channelName: "Agent.create",
20372
+ kind: "async"
20373
+ }),
20374
+ resume: channel({
20375
+ channelName: "Agent.resume",
20376
+ kind: "async"
20377
+ }),
20378
+ prompt: channel({
20379
+ channelName: "Agent.prompt",
20380
+ kind: "async"
20381
+ }),
20382
+ send: channel({
20383
+ channelName: "agent.send",
20384
+ kind: "async"
20385
+ })
20386
+ },
20387
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
20388
+ );
19361
20389
 
19362
20390
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
19363
20391
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -19409,10 +20437,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19409
20437
  provider: "cursor",
19410
20438
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19411
20439
  };
19412
- const span = startSpan({
19413
- name: "Cursor Agent",
19414
- spanAttributes: { type: "task" /* TASK */ }
19415
- });
20440
+ const span = startSpan(
20441
+ withSpanInstrumentationName(
20442
+ {
20443
+ name: "Cursor Agent",
20444
+ spanAttributes: { type: "task" /* TASK */ }
20445
+ },
20446
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20447
+ )
20448
+ );
19416
20449
  const startTime = getCurrentUnixTimestamp();
19417
20450
  safeLog2(span, {
19418
20451
  input: sanitizeUserMessage(message),
@@ -19474,10 +20507,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19474
20507
  provider: "cursor",
19475
20508
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19476
20509
  };
19477
- const span = startSpan({
19478
- name: "Cursor Agent",
19479
- spanAttributes: { type: "task" /* TASK */ }
19480
- });
20510
+ const span = startSpan(
20511
+ withSpanInstrumentationName(
20512
+ {
20513
+ name: "Cursor Agent",
20514
+ spanAttributes: { type: "task" /* TASK */ }
20515
+ },
20516
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20517
+ )
20518
+ );
19481
20519
  const startTime = getCurrentUnixTimestamp();
19482
20520
  safeLog2(span, {
19483
20521
  input: sanitizeUserMessage(message),
@@ -19924,29 +20962,39 @@ async function startToolSpan(state, args) {
19924
20962
  if (args.truncated?.result !== void 0) {
19925
20963
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
19926
20964
  }
19927
- const span = startSpan({
19928
- event: {
19929
- input: args.args,
19930
- metadata
19931
- },
19932
- name: `tool: ${name}`,
19933
- parent: await state.span.export(),
19934
- spanAttributes: { type: "tool" /* TOOL */ }
19935
- });
20965
+ const span = startSpan(
20966
+ withSpanInstrumentationName(
20967
+ {
20968
+ event: {
20969
+ input: args.args,
20970
+ metadata
20971
+ },
20972
+ name: `tool: ${name}`,
20973
+ parent: await state.span.export(),
20974
+ spanAttributes: { type: "tool" /* TOOL */ }
20975
+ },
20976
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20977
+ )
20978
+ );
19936
20979
  let subAgentSpan;
19937
20980
  if (isSubAgentToolName(name)) {
19938
- subAgentSpan = startSpan({
19939
- event: {
19940
- input: args.args,
19941
- metadata: {
19942
- "cursor_sdk.subagent.tool_call_id": args.callId,
19943
- "gen_ai.tool.name": name
19944
- }
19945
- },
19946
- name: formatSubAgentSpanName2(args.toolCall, args.args),
19947
- parent: await span.export(),
19948
- spanAttributes: { type: "task" /* TASK */ }
19949
- });
20981
+ subAgentSpan = startSpan(
20982
+ withSpanInstrumentationName(
20983
+ {
20984
+ event: {
20985
+ input: args.args,
20986
+ metadata: {
20987
+ "cursor_sdk.subagent.tool_call_id": args.callId,
20988
+ "gen_ai.tool.name": name
20989
+ }
20990
+ },
20991
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
20992
+ parent: await span.export(),
20993
+ spanAttributes: { type: "task" /* TASK */ }
20994
+ },
20995
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20996
+ )
20997
+ );
19950
20998
  }
19951
20999
  return { span, subAgentSpan };
19952
21000
  }
@@ -20205,24 +21253,28 @@ function cleanMetrics2(metrics) {
20205
21253
  }
20206
21254
 
20207
21255
  // src/instrumentation/plugins/openai-agents-channels.ts
20208
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
20209
- onTraceStart: channel({
20210
- channelName: "tracing.processor.onTraceStart",
20211
- kind: "async"
20212
- }),
20213
- onTraceEnd: channel({
20214
- channelName: "tracing.processor.onTraceEnd",
20215
- kind: "async"
20216
- }),
20217
- onSpanStart: channel({
20218
- channelName: "tracing.processor.onSpanStart",
20219
- kind: "async"
20220
- }),
20221
- onSpanEnd: channel({
20222
- channelName: "tracing.processor.onSpanEnd",
20223
- kind: "async"
20224
- })
20225
- });
21256
+ var openAIAgentsCoreChannels = defineChannels(
21257
+ "@openai/agents-core",
21258
+ {
21259
+ onTraceStart: channel({
21260
+ channelName: "tracing.processor.onTraceStart",
21261
+ kind: "async"
21262
+ }),
21263
+ onTraceEnd: channel({
21264
+ channelName: "tracing.processor.onTraceEnd",
21265
+ kind: "async"
21266
+ }),
21267
+ onSpanStart: channel({
21268
+ channelName: "tracing.processor.onSpanStart",
21269
+ kind: "async"
21270
+ }),
21271
+ onSpanEnd: channel({
21272
+ channelName: "tracing.processor.onSpanEnd",
21273
+ kind: "async"
21274
+ })
21275
+ },
21276
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
21277
+ );
20226
21278
 
20227
21279
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
20228
21280
  function isSpanData(spanData, type) {
@@ -20338,16 +21390,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20338
21390
  this.evictOldestTrace();
20339
21391
  }
20340
21392
  const current = currentSpan();
20341
- const span = current && current !== NOOP_SPAN ? current.startSpan({
20342
- name: trace.name,
20343
- type: "task" /* TASK */
20344
- }) : this.logger ? this.logger.startSpan({
20345
- name: trace.name,
20346
- type: "task" /* TASK */
20347
- }) : startSpan({
20348
- name: trace.name,
20349
- type: "task" /* TASK */
20350
- });
21393
+ const spanArgs = withSpanInstrumentationName(
21394
+ {
21395
+ name: trace.name,
21396
+ type: "task" /* TASK */
21397
+ },
21398
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21399
+ );
21400
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
21401
+ withSpanInstrumentationName(
21402
+ spanArgs,
21403
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21404
+ )
21405
+ );
20351
21406
  span.log({
20352
21407
  input: "Agent workflow started",
20353
21408
  metadata: {
@@ -20398,10 +21453,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20398
21453
  if (!parentSpan) {
20399
21454
  return Promise.resolve();
20400
21455
  }
20401
- const childSpan = parentSpan.startSpan({
20402
- name: spanNameFromAgents(span),
20403
- type: spanTypeFromAgents(span)
20404
- });
21456
+ const childSpan = parentSpan.startSpan(
21457
+ withSpanInstrumentationName(
21458
+ {
21459
+ name: spanNameFromAgents(span),
21460
+ type: spanTypeFromAgents(span)
21461
+ },
21462
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21463
+ )
21464
+ );
20405
21465
  traceData.childSpans.set(span.spanId, childSpan);
20406
21466
  return Promise.resolve();
20407
21467
  }
@@ -20666,24 +21726,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
20666
21726
  };
20667
21727
 
20668
21728
  // src/instrumentation/plugins/google-genai-channels.ts
20669
- var googleGenAIChannels = defineChannels("@google/genai", {
20670
- generateContent: channel({
20671
- channelName: "models.generateContent",
20672
- kind: "async"
20673
- }),
20674
- generateContentStream: channel({
20675
- channelName: "models.generateContentStream",
20676
- kind: "async"
20677
- }),
20678
- embedContent: channel({
20679
- channelName: "models.embedContent",
20680
- kind: "async"
20681
- }),
20682
- interactionsCreate: channel({
20683
- channelName: "interactions.create",
20684
- kind: "async"
20685
- })
20686
- });
21729
+ var googleGenAIChannels = defineChannels(
21730
+ "@google/genai",
21731
+ {
21732
+ generateContent: channel({
21733
+ channelName: "models.generateContent",
21734
+ kind: "async"
21735
+ }),
21736
+ generateContentStream: channel({
21737
+ channelName: "models.generateContentStream",
21738
+ kind: "async"
21739
+ }),
21740
+ embedContent: channel({
21741
+ channelName: "models.embedContent",
21742
+ kind: "async"
21743
+ }),
21744
+ interactionsCreate: channel({
21745
+ channelName: "interactions.create",
21746
+ kind: "async"
21747
+ })
21748
+ },
21749
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
21750
+ );
20687
21751
 
20688
21752
  // src/instrumentation/plugins/google-genai-plugin.ts
20689
21753
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -20721,13 +21785,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20721
21785
  const params = event.arguments[0];
20722
21786
  const input = serializeGenerateContentInput(params);
20723
21787
  const metadata = extractGenerateContentMetadata(params);
20724
- const span = startSpan({
20725
- name: "generate_content",
20726
- spanAttributes: {
20727
- type: "llm" /* LLM */
20728
- },
20729
- event: createWrapperParityEvent({ input, metadata })
20730
- });
21788
+ const span = startSpan(
21789
+ withSpanInstrumentationName(
21790
+ {
21791
+ name: "generate_content",
21792
+ spanAttributes: {
21793
+ type: "llm" /* LLM */
21794
+ },
21795
+ event: createWrapperParityEvent({ input, metadata })
21796
+ },
21797
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21798
+ )
21799
+ );
20731
21800
  return {
20732
21801
  span,
20733
21802
  startTime: getCurrentUnixTimestamp()
@@ -20740,13 +21809,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20740
21809
  const params = event.arguments[0];
20741
21810
  const input = serializeGenerateContentInput(params);
20742
21811
  const metadata = extractGenerateContentMetadata(params);
20743
- const span = startSpan({
20744
- name: "generate_content",
20745
- spanAttributes: {
20746
- type: "llm" /* LLM */
20747
- },
20748
- event: createWrapperParityEvent({ input, metadata })
20749
- });
21812
+ const span = startSpan(
21813
+ withSpanInstrumentationName(
21814
+ {
21815
+ name: "generate_content",
21816
+ spanAttributes: {
21817
+ type: "llm" /* LLM */
21818
+ },
21819
+ event: createWrapperParityEvent({ input, metadata })
21820
+ },
21821
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21822
+ )
21823
+ );
20750
21824
  return {
20751
21825
  span,
20752
21826
  startTime: getCurrentUnixTimestamp()
@@ -20822,13 +21896,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20822
21896
  const params = event.arguments[0];
20823
21897
  const input = serializeEmbedContentInput(params);
20824
21898
  const metadata = extractEmbedContentMetadata(params);
20825
- const span = startSpan({
20826
- name: "embed_content",
20827
- spanAttributes: {
20828
- type: "llm" /* LLM */
20829
- },
20830
- event: createWrapperParityEvent({ input, metadata })
20831
- });
21899
+ const span = startSpan(
21900
+ withSpanInstrumentationName(
21901
+ {
21902
+ name: "embed_content",
21903
+ spanAttributes: {
21904
+ type: "llm" /* LLM */
21905
+ },
21906
+ event: createWrapperParityEvent({ input, metadata })
21907
+ },
21908
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21909
+ )
21910
+ );
20832
21911
  return {
20833
21912
  span,
20834
21913
  startTime: getCurrentUnixTimestamp()
@@ -20841,13 +21920,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20841
21920
  const params = event.arguments[0];
20842
21921
  const input = serializeEmbedContentInput(params);
20843
21922
  const metadata = extractEmbedContentMetadata(params);
20844
- const span = startSpan({
20845
- name: "embed_content",
20846
- spanAttributes: {
20847
- type: "llm" /* LLM */
20848
- },
20849
- event: createWrapperParityEvent({ input, metadata })
20850
- });
21923
+ const span = startSpan(
21924
+ withSpanInstrumentationName(
21925
+ {
21926
+ name: "embed_content",
21927
+ spanAttributes: {
21928
+ type: "llm" /* LLM */
21929
+ },
21930
+ event: createWrapperParityEvent({ input, metadata })
21931
+ },
21932
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21933
+ )
21934
+ );
20851
21935
  return {
20852
21936
  span,
20853
21937
  startTime: getCurrentUnixTimestamp()
@@ -20958,16 +22042,21 @@ function patchGoogleGenAIStreamingResult(args) {
20958
22042
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
20959
22043
  const ensureSpan = () => {
20960
22044
  if (!span) {
20961
- span = startSpan({
20962
- name: "generate_content_stream",
20963
- spanAttributes: {
20964
- type: "llm" /* LLM */
20965
- },
20966
- event: {
20967
- input,
20968
- metadata
20969
- }
20970
- });
22045
+ span = startSpan(
22046
+ withSpanInstrumentationName(
22047
+ {
22048
+ name: "generate_content_stream",
22049
+ spanAttributes: {
22050
+ type: "llm" /* LLM */
22051
+ },
22052
+ event: {
22053
+ input,
22054
+ metadata
22055
+ }
22056
+ },
22057
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
22058
+ )
22059
+ );
20971
22060
  }
20972
22061
  return span;
20973
22062
  };
@@ -21735,28 +22824,32 @@ function tryToDict(obj) {
21735
22824
  }
21736
22825
 
21737
22826
  // src/instrumentation/plugins/huggingface-channels.ts
21738
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
21739
- chatCompletion: channel({
21740
- channelName: "chatCompletion",
21741
- kind: "async"
21742
- }),
21743
- chatCompletionStream: channel({
21744
- channelName: "chatCompletionStream",
21745
- kind: "sync-stream"
21746
- }),
21747
- textGeneration: channel({
21748
- channelName: "textGeneration",
21749
- kind: "async"
21750
- }),
21751
- textGenerationStream: channel({
21752
- channelName: "textGenerationStream",
21753
- kind: "sync-stream"
21754
- }),
21755
- featureExtraction: channel({
21756
- channelName: "featureExtraction",
21757
- kind: "async"
21758
- })
21759
- });
22827
+ var huggingFaceChannels = defineChannels(
22828
+ "@huggingface/inference",
22829
+ {
22830
+ chatCompletion: channel({
22831
+ channelName: "chatCompletion",
22832
+ kind: "async"
22833
+ }),
22834
+ chatCompletionStream: channel({
22835
+ channelName: "chatCompletionStream",
22836
+ kind: "sync-stream"
22837
+ }),
22838
+ textGeneration: channel({
22839
+ channelName: "textGeneration",
22840
+ kind: "async"
22841
+ }),
22842
+ textGenerationStream: channel({
22843
+ channelName: "textGenerationStream",
22844
+ kind: "sync-stream"
22845
+ }),
22846
+ featureExtraction: channel({
22847
+ channelName: "featureExtraction",
22848
+ kind: "async"
22849
+ })
22850
+ },
22851
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
22852
+ );
21760
22853
 
21761
22854
  // src/instrumentation/plugins/huggingface-plugin.ts
21762
22855
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -22183,20 +23276,24 @@ function extractTextGenerationStreamMetadata(chunks) {
22183
23276
  }
22184
23277
 
22185
23278
  // src/instrumentation/plugins/openrouter-agent-channels.ts
22186
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
22187
- callModel: channel({
22188
- channelName: "callModel",
22189
- kind: "sync-stream"
22190
- }),
22191
- callModelTurn: channel({
22192
- channelName: "callModel.turn",
22193
- kind: "async"
22194
- }),
22195
- toolExecute: channel({
22196
- channelName: "tool.execute",
22197
- kind: "async"
22198
- })
22199
- });
23279
+ var openRouterAgentChannels = defineChannels(
23280
+ "@openrouter/agent",
23281
+ {
23282
+ callModel: channel({
23283
+ channelName: "callModel",
23284
+ kind: "sync-stream"
23285
+ }),
23286
+ callModelTurn: channel({
23287
+ channelName: "callModel.turn",
23288
+ kind: "async"
23289
+ }),
23290
+ toolExecute: channel({
23291
+ channelName: "tool.execute",
23292
+ kind: "async"
23293
+ })
23294
+ },
23295
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
23296
+ );
22200
23297
 
22201
23298
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
22202
23299
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -22966,38 +24063,40 @@ function normalizeError(error) {
22966
24063
  }
22967
24064
 
22968
24065
  // src/instrumentation/plugins/openrouter-channels.ts
22969
- var openRouterChannels = defineChannels("@openrouter/sdk", {
22970
- chatSend: channel({
22971
- channelName: "chat.send",
22972
- kind: "async"
22973
- }),
22974
- embeddingsGenerate: channel({
22975
- channelName: "embeddings.generate",
22976
- kind: "async"
22977
- }),
22978
- rerankRerank: channel(
22979
- {
24066
+ var openRouterChannels = defineChannels(
24067
+ "@openrouter/sdk",
24068
+ {
24069
+ chatSend: channel({
24070
+ channelName: "chat.send",
24071
+ kind: "async"
24072
+ }),
24073
+ embeddingsGenerate: channel({
24074
+ channelName: "embeddings.generate",
24075
+ kind: "async"
24076
+ }),
24077
+ rerankRerank: channel({
22980
24078
  channelName: "rerank.rerank",
22981
24079
  kind: "async"
22982
- }
22983
- ),
22984
- betaResponsesSend: channel({
22985
- channelName: "beta.responses.send",
22986
- kind: "async"
22987
- }),
22988
- callModel: channel({
22989
- channelName: "callModel",
22990
- kind: "sync-stream"
22991
- }),
22992
- callModelTurn: channel({
22993
- channelName: "callModel.turn",
22994
- kind: "async"
22995
- }),
22996
- toolExecute: channel({
22997
- channelName: "tool.execute",
22998
- kind: "async"
22999
- })
23000
- });
24080
+ }),
24081
+ betaResponsesSend: channel({
24082
+ channelName: "beta.responses.send",
24083
+ kind: "async"
24084
+ }),
24085
+ callModel: channel({
24086
+ channelName: "callModel",
24087
+ kind: "sync-stream"
24088
+ }),
24089
+ callModelTurn: channel({
24090
+ channelName: "callModel.turn",
24091
+ kind: "async"
24092
+ }),
24093
+ toolExecute: channel({
24094
+ channelName: "tool.execute",
24095
+ kind: "async"
24096
+ })
24097
+ },
24098
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
24099
+ );
23001
24100
 
23002
24101
  // src/instrumentation/plugins/openrouter-plugin.ts
23003
24102
  var OpenRouterPlugin = class extends BasePlugin {
@@ -24037,52 +25136,56 @@ function normalizeError2(error) {
24037
25136
  }
24038
25137
 
24039
25138
  // src/instrumentation/plugins/mistral-channels.ts
24040
- var mistralChannels = defineChannels("@mistralai/mistralai", {
24041
- chatComplete: channel({
24042
- channelName: "chat.complete",
24043
- kind: "async"
24044
- }),
24045
- chatStream: channel({
24046
- channelName: "chat.stream",
24047
- kind: "async"
24048
- }),
24049
- embeddingsCreate: channel({
24050
- channelName: "embeddings.create",
24051
- kind: "async"
24052
- }),
24053
- classifiersModerate: channel({
24054
- channelName: "classifiers.moderate",
24055
- kind: "async"
24056
- }),
24057
- classifiersModerateChat: channel({
24058
- channelName: "classifiers.moderateChat",
24059
- kind: "async"
24060
- }),
24061
- classifiersClassify: channel({
24062
- channelName: "classifiers.classify",
24063
- kind: "async"
24064
- }),
24065
- classifiersClassifyChat: channel({
24066
- channelName: "classifiers.classifyChat",
24067
- kind: "async"
24068
- }),
24069
- fimComplete: channel({
24070
- channelName: "fim.complete",
24071
- kind: "async"
24072
- }),
24073
- fimStream: channel({
24074
- channelName: "fim.stream",
24075
- kind: "async"
24076
- }),
24077
- agentsComplete: channel({
24078
- channelName: "agents.complete",
24079
- kind: "async"
24080
- }),
24081
- agentsStream: channel({
24082
- channelName: "agents.stream",
24083
- kind: "async"
24084
- })
24085
- });
25139
+ var mistralChannels = defineChannels(
25140
+ "@mistralai/mistralai",
25141
+ {
25142
+ chatComplete: channel({
25143
+ channelName: "chat.complete",
25144
+ kind: "async"
25145
+ }),
25146
+ chatStream: channel({
25147
+ channelName: "chat.stream",
25148
+ kind: "async"
25149
+ }),
25150
+ embeddingsCreate: channel({
25151
+ channelName: "embeddings.create",
25152
+ kind: "async"
25153
+ }),
25154
+ classifiersModerate: channel({
25155
+ channelName: "classifiers.moderate",
25156
+ kind: "async"
25157
+ }),
25158
+ classifiersModerateChat: channel({
25159
+ channelName: "classifiers.moderateChat",
25160
+ kind: "async"
25161
+ }),
25162
+ classifiersClassify: channel({
25163
+ channelName: "classifiers.classify",
25164
+ kind: "async"
25165
+ }),
25166
+ classifiersClassifyChat: channel({
25167
+ channelName: "classifiers.classifyChat",
25168
+ kind: "async"
25169
+ }),
25170
+ fimComplete: channel({
25171
+ channelName: "fim.complete",
25172
+ kind: "async"
25173
+ }),
25174
+ fimStream: channel({
25175
+ channelName: "fim.stream",
25176
+ kind: "async"
25177
+ }),
25178
+ agentsComplete: channel({
25179
+ channelName: "agents.complete",
25180
+ kind: "async"
25181
+ }),
25182
+ agentsStream: channel({
25183
+ channelName: "agents.stream",
25184
+ kind: "async"
25185
+ })
25186
+ },
25187
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
25188
+ );
24086
25189
 
24087
25190
  // src/instrumentation/plugins/mistral-plugin.ts
24088
25191
  var MistralPlugin = class extends BasePlugin {
@@ -24702,20 +25805,24 @@ function aggregateMistralStreamChunks(chunks) {
24702
25805
  }
24703
25806
 
24704
25807
  // src/instrumentation/plugins/google-adk-channels.ts
24705
- var googleADKChannels = defineChannels("@google/adk", {
24706
- runnerRunAsync: channel({
24707
- channelName: "runner.runAsync",
24708
- kind: "sync-stream"
24709
- }),
24710
- agentRunAsync: channel({
24711
- channelName: "agent.runAsync",
24712
- kind: "sync-stream"
24713
- }),
24714
- toolRunAsync: channel({
24715
- channelName: "tool.runAsync",
24716
- kind: "async"
24717
- })
24718
- });
25808
+ var googleADKChannels = defineChannels(
25809
+ "@google/adk",
25810
+ {
25811
+ runnerRunAsync: channel({
25812
+ channelName: "runner.runAsync",
25813
+ kind: "sync-stream"
25814
+ }),
25815
+ agentRunAsync: channel({
25816
+ channelName: "agent.runAsync",
25817
+ kind: "sync-stream"
25818
+ }),
25819
+ toolRunAsync: channel({
25820
+ channelName: "tool.runAsync",
25821
+ kind: "async"
25822
+ })
25823
+ },
25824
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
25825
+ );
24719
25826
 
24720
25827
  // src/instrumentation/plugins/google-adk-plugin.ts
24721
25828
  var GoogleADKPlugin = class extends BasePlugin {
@@ -24740,12 +25847,17 @@ var GoogleADKPlugin = class extends BasePlugin {
24740
25847
  const createState = (event) => {
24741
25848
  const params = event.arguments[0] ?? {};
24742
25849
  const contextKey = extractRunnerContextKey(params);
24743
- const span = startSpan({
24744
- name: "Google ADK Runner",
24745
- spanAttributes: {
24746
- type: "task" /* TASK */
24747
- }
24748
- });
25850
+ const span = startSpan(
25851
+ withSpanInstrumentationName(
25852
+ {
25853
+ name: "Google ADK Runner",
25854
+ spanAttributes: {
25855
+ type: "task" /* TASK */
25856
+ }
25857
+ },
25858
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25859
+ )
25860
+ );
24749
25861
  const startTime = getCurrentUnixTimestamp();
24750
25862
  try {
24751
25863
  const metadata = extractRunnerMetadata(params);
@@ -24831,18 +25943,23 @@ var GoogleADKPlugin = class extends BasePlugin {
24831
25943
  this.activeRunnerSpans
24832
25944
  );
24833
25945
  const contextKey = extractInvocationContextKey(parentContext);
24834
- const span = startSpan({
24835
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
24836
- spanAttributes: {
24837
- type: "task" /* TASK */
24838
- },
24839
- ...runnerParentSpan ? {
24840
- parentSpanIds: {
24841
- spanId: runnerParentSpan.spanId,
24842
- rootSpanId: runnerParentSpan.rootSpanId
24843
- }
24844
- } : {}
24845
- });
25946
+ const span = startSpan(
25947
+ withSpanInstrumentationName(
25948
+ {
25949
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
25950
+ spanAttributes: {
25951
+ type: "task" /* TASK */
25952
+ },
25953
+ ...runnerParentSpan ? {
25954
+ parentSpanIds: {
25955
+ spanId: runnerParentSpan.spanId,
25956
+ rootSpanId: runnerParentSpan.rootSpanId
25957
+ }
25958
+ } : {}
25959
+ },
25960
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25961
+ )
25962
+ );
24846
25963
  const startTime = getCurrentUnixTimestamp();
24847
25964
  try {
24848
25965
  const metadata = {
@@ -24935,22 +26052,27 @@ var GoogleADKPlugin = class extends BasePlugin {
24935
26052
  this.activeAgentSpans,
24936
26053
  this.activeRunnerSpans
24937
26054
  );
24938
- const createSpan = () => startSpan({
24939
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
24940
- spanAttributes: {
24941
- type: "tool" /* TOOL */
24942
- },
24943
- event: {
24944
- input: req.args,
24945
- metadata: {
24946
- provider: "google-adk",
24947
- ...toolName && { "google_adk.tool_name": toolName },
24948
- ...extractToolCallId(req) && {
24949
- "google_adk.tool_call_id": extractToolCallId(req)
26055
+ const createSpan = () => startSpan(
26056
+ withSpanInstrumentationName(
26057
+ {
26058
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
26059
+ spanAttributes: {
26060
+ type: "tool" /* TOOL */
26061
+ },
26062
+ event: {
26063
+ input: req.args,
26064
+ metadata: {
26065
+ provider: "google-adk",
26066
+ ...toolName && { "google_adk.tool_name": toolName },
26067
+ ...extractToolCallId(req) && {
26068
+ "google_adk.tool_call_id": extractToolCallId(req)
26069
+ }
26070
+ }
24950
26071
  }
24951
- }
24952
- }
24953
- });
26072
+ },
26073
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
26074
+ )
26075
+ );
24954
26076
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
24955
26077
  const startTime = getCurrentUnixTimestamp();
24956
26078
  states.set(event, { span, startTime });
@@ -25349,24 +26471,28 @@ function cleanMetrics4(metrics) {
25349
26471
  }
25350
26472
 
25351
26473
  // src/instrumentation/plugins/cohere-channels.ts
25352
- var cohereChannels = defineChannels("cohere-ai", {
25353
- chat: channel({
25354
- channelName: "chat",
25355
- kind: "async"
25356
- }),
25357
- chatStream: channel({
25358
- channelName: "chatStream",
25359
- kind: "async"
25360
- }),
25361
- embed: channel({
25362
- channelName: "embed",
25363
- kind: "async"
25364
- }),
25365
- rerank: channel({
25366
- channelName: "rerank",
25367
- kind: "async"
25368
- })
25369
- });
26474
+ var cohereChannels = defineChannels(
26475
+ "cohere-ai",
26476
+ {
26477
+ chat: channel({
26478
+ channelName: "chat",
26479
+ kind: "async"
26480
+ }),
26481
+ chatStream: channel({
26482
+ channelName: "chatStream",
26483
+ kind: "async"
26484
+ }),
26485
+ embed: channel({
26486
+ channelName: "embed",
26487
+ kind: "async"
26488
+ }),
26489
+ rerank: channel({
26490
+ channelName: "rerank",
26491
+ kind: "async"
26492
+ })
26493
+ },
26494
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
26495
+ );
25370
26496
 
25371
26497
  // src/instrumentation/plugins/cohere-plugin.ts
25372
26498
  var CoherePlugin = class extends BasePlugin {
@@ -26035,18 +27161,20 @@ function aggregateCohereChatStreamChunks(chunks) {
26035
27161
  }
26036
27162
 
26037
27163
  // src/instrumentation/plugins/groq-channels.ts
26038
- var groqChannels = defineChannels("groq-sdk", {
26039
- chatCompletionsCreate: channel({
26040
- channelName: "chat.completions.create",
26041
- kind: "async"
26042
- }),
26043
- embeddingsCreate: channel(
26044
- {
27164
+ var groqChannels = defineChannels(
27165
+ "groq-sdk",
27166
+ {
27167
+ chatCompletionsCreate: channel({
27168
+ channelName: "chat.completions.create",
27169
+ kind: "async"
27170
+ }),
27171
+ embeddingsCreate: channel({
26045
27172
  channelName: "embeddings.create",
26046
27173
  kind: "async"
26047
- }
26048
- )
26049
- });
27174
+ })
27175
+ },
27176
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
27177
+ );
26050
27178
 
26051
27179
  // src/instrumentation/plugins/groq-plugin.ts
26052
27180
  var GroqPlugin = class extends BasePlugin {
@@ -26149,15 +27277,27 @@ var clientSendChannel = channel({
26149
27277
  channelName: "client.send",
26150
27278
  kind: "async"
26151
27279
  });
26152
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
26153
- clientSend: clientSendChannel
26154
- });
26155
- var smithyCoreChannels = defineChannels("@smithy/core", {
26156
- clientSend: clientSendChannel
26157
- });
26158
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
26159
- clientSend: clientSendChannel
26160
- });
27280
+ var bedrockRuntimeChannels = defineChannels(
27281
+ "aws-bedrock-runtime",
27282
+ {
27283
+ clientSend: clientSendChannel
27284
+ },
27285
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27286
+ );
27287
+ var smithyCoreChannels = defineChannels(
27288
+ "@smithy/core",
27289
+ {
27290
+ clientSend: clientSendChannel
27291
+ },
27292
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27293
+ );
27294
+ var smithyClientChannels = defineChannels(
27295
+ "@smithy/smithy-client",
27296
+ {
27297
+ clientSend: clientSendChannel
27298
+ },
27299
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27300
+ );
26161
27301
 
26162
27302
  // src/instrumentation/plugins/bedrock-runtime-common.ts
26163
27303
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -26791,38 +27931,46 @@ function extractTextFromJsonLike(value) {
26791
27931
  }
26792
27932
 
26793
27933
  // src/instrumentation/plugins/genkit-channels.ts
26794
- var genkitChannels = defineChannels("@genkit-ai/ai", {
26795
- generate: channel({
26796
- channelName: "generate",
26797
- kind: "async"
26798
- }),
26799
- generateStream: channel({
26800
- channelName: "generateStream",
26801
- kind: "sync-stream"
26802
- }),
26803
- embed: channel({
26804
- channelName: "embed",
26805
- kind: "async"
26806
- }),
26807
- embedMany: channel({
26808
- channelName: "embedMany",
26809
- kind: "async"
26810
- }),
26811
- actionRun: channel({
26812
- channelName: "action.run",
26813
- kind: "async"
26814
- }),
26815
- actionStream: channel({
26816
- channelName: "action.stream",
26817
- kind: "sync-stream"
26818
- })
26819
- });
26820
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
26821
- actionSpan: channel({
26822
- channelName: "action.span",
26823
- kind: "async"
26824
- })
26825
- });
27934
+ var genkitChannels = defineChannels(
27935
+ "@genkit-ai/ai",
27936
+ {
27937
+ generate: channel({
27938
+ channelName: "generate",
27939
+ kind: "async"
27940
+ }),
27941
+ generateStream: channel({
27942
+ channelName: "generateStream",
27943
+ kind: "sync-stream"
27944
+ }),
27945
+ embed: channel({
27946
+ channelName: "embed",
27947
+ kind: "async"
27948
+ }),
27949
+ embedMany: channel({
27950
+ channelName: "embedMany",
27951
+ kind: "async"
27952
+ }),
27953
+ actionRun: channel({
27954
+ channelName: "action.run",
27955
+ kind: "async"
27956
+ }),
27957
+ actionStream: channel({
27958
+ channelName: "action.stream",
27959
+ kind: "sync-stream"
27960
+ })
27961
+ },
27962
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
27963
+ );
27964
+ var genkitCoreChannels = defineChannels(
27965
+ "@genkit-ai/core",
27966
+ {
27967
+ actionSpan: channel({
27968
+ channelName: "action.span",
27969
+ kind: "async"
27970
+ })
27971
+ },
27972
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
27973
+ );
26826
27974
 
26827
27975
  // src/instrumentation/plugins/genkit-plugin.ts
26828
27976
  var GenkitPlugin = class extends BasePlugin {
@@ -27007,12 +28155,17 @@ function startActionSpanState(args) {
27007
28155
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
27008
28156
  return void 0;
27009
28157
  }
27010
- const span = startSpan({
27011
- name: actionSpanName(args.metadata, args.runStepName),
27012
- spanAttributes: {
27013
- type: actionSpanType(args.metadata)
27014
- }
27015
- });
28158
+ const span = startSpan(
28159
+ withSpanInstrumentationName(
28160
+ {
28161
+ name: actionSpanName(args.metadata, args.runStepName),
28162
+ spanAttributes: {
28163
+ type: actionSpanType(args.metadata)
28164
+ }
28165
+ },
28166
+ INSTRUMENTATION_NAMES.GENKIT
28167
+ )
28168
+ );
27016
28169
  const startTime = getCurrentUnixTimestamp();
27017
28170
  span.log({
27018
28171
  input: args.input,
@@ -27373,20 +28526,24 @@ function stringValue(value) {
27373
28526
  }
27374
28527
 
27375
28528
  // src/instrumentation/plugins/github-copilot-channels.ts
27376
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
27377
- createSession: channel({
27378
- channelName: "client.createSession",
27379
- kind: "async"
27380
- }),
27381
- resumeSession: channel({
27382
- channelName: "client.resumeSession",
27383
- kind: "async"
27384
- }),
27385
- sendAndWait: channel({
27386
- channelName: "session.sendAndWait",
27387
- kind: "async"
27388
- })
27389
- });
28529
+ var gitHubCopilotChannels = defineChannels(
28530
+ "@github/copilot-sdk",
28531
+ {
28532
+ createSession: channel({
28533
+ channelName: "client.createSession",
28534
+ kind: "async"
28535
+ }),
28536
+ resumeSession: channel({
28537
+ channelName: "client.resumeSession",
28538
+ kind: "async"
28539
+ }),
28540
+ sendAndWait: channel({
28541
+ channelName: "session.sendAndWait",
28542
+ kind: "async"
28543
+ })
28544
+ },
28545
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
28546
+ );
27390
28547
 
27391
28548
  // src/instrumentation/plugins/github-copilot-plugin.ts
27392
28549
  var ROOT_AGENT_KEY = "__root__";
@@ -27477,11 +28634,16 @@ async function handleTurnStart(state, agentId) {
27477
28634
  return;
27478
28635
  }
27479
28636
  const parentId = await getParentIdForAgent(state, agentId);
27480
- const span = startSpan({
27481
- name: "Copilot Turn",
27482
- parent: parentId,
27483
- spanAttributes: { type: "task" /* TASK */ }
27484
- });
28637
+ const span = startSpan(
28638
+ withSpanInstrumentationName(
28639
+ {
28640
+ name: "Copilot Turn",
28641
+ parent: parentId,
28642
+ spanAttributes: { type: "task" /* TASK */ }
28643
+ },
28644
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28645
+ )
28646
+ );
27485
28647
  const pendingUserMessage = state.pendingUserMessages.get(key);
27486
28648
  if (pendingUserMessage) {
27487
28649
  span.log({ input: pendingUserMessage });
@@ -27512,11 +28674,16 @@ async function handleUsage(state, agentId, usage) {
27512
28674
  const parentId = turn ? await turn.id : await state.session.id;
27513
28675
  const content = state.currentMessageContent.get(key);
27514
28676
  const { metrics, metadata } = extractMetricsFromUsage(usage);
27515
- const llmSpan = startSpan({
27516
- name: "github.copilot.llm",
27517
- parent: parentId,
27518
- spanAttributes: { type: "llm" /* LLM */ }
27519
- });
28677
+ const llmSpan = startSpan(
28678
+ withSpanInstrumentationName(
28679
+ {
28680
+ name: "github.copilot.llm",
28681
+ parent: parentId,
28682
+ spanAttributes: { type: "llm" /* LLM */ }
28683
+ },
28684
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28685
+ )
28686
+ );
27520
28687
  llmSpan.log({
27521
28688
  output: content ?? void 0,
27522
28689
  metadata,
@@ -27542,11 +28709,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
27542
28709
  }
27543
28710
  const parentId = await getToolParentId(state, agentId);
27544
28711
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
27545
- const span = startSpan({
27546
- name: displayName,
27547
- parent: parentId,
27548
- spanAttributes: { type: "tool" /* TOOL */ }
27549
- });
28712
+ const span = startSpan(
28713
+ withSpanInstrumentationName(
28714
+ {
28715
+ name: displayName,
28716
+ parent: parentId,
28717
+ spanAttributes: { type: "tool" /* TOOL */ }
28718
+ },
28719
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28720
+ )
28721
+ );
27550
28722
  const metadata = {
27551
28723
  "gen_ai.tool.name": toolName,
27552
28724
  "gen_ai.tool.call.id": toolCallId
@@ -27582,11 +28754,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
27582
28754
  }
27583
28755
  const tool = state.activeTools.get(toolCallId);
27584
28756
  const parentId = tool ? await tool.id : await state.session.id;
27585
- const span = startSpan({
27586
- name: `Agent: ${agentDisplayName}`,
27587
- parent: parentId,
27588
- spanAttributes: { type: "task" /* TASK */ }
27589
- });
28757
+ const span = startSpan(
28758
+ withSpanInstrumentationName(
28759
+ {
28760
+ name: `Agent: ${agentDisplayName}`,
28761
+ parent: parentId,
28762
+ spanAttributes: { type: "task" /* TASK */ }
28763
+ },
28764
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28765
+ )
28766
+ );
27590
28767
  span.log({
27591
28768
  metadata: {
27592
28769
  "github_copilot.agent_name": agentDisplayName,
@@ -27812,10 +28989,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
27812
28989
  if (!config || typeof config !== "object") {
27813
28990
  return;
27814
28991
  }
27815
- const sessionSpan = startSpan({
27816
- name: "Copilot Session",
27817
- spanAttributes: { type: "task" /* TASK */ }
27818
- });
28992
+ const sessionSpan = startSpan(
28993
+ withSpanInstrumentationName(
28994
+ {
28995
+ name: "Copilot Session",
28996
+ spanAttributes: { type: "task" /* TASK */ }
28997
+ },
28998
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28999
+ )
29000
+ );
27819
29001
  const metadata = {};
27820
29002
  if (config.model) {
27821
29003
  metadata["github_copilot.model"] = config.model;
@@ -27903,12 +29085,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
27903
29085
  };
27904
29086
 
27905
29087
  // src/instrumentation/plugins/flue-channels.ts
27906
- var flueChannels = defineChannels("@flue/runtime", {
27907
- createContext: channel({
27908
- channelName: "createFlueContext",
27909
- kind: "sync-stream"
27910
- })
27911
- });
29088
+ var flueChannels = defineChannels(
29089
+ "@flue/runtime",
29090
+ {
29091
+ createContext: channel({
29092
+ channelName: "createFlueContext",
29093
+ kind: "sync-stream"
29094
+ })
29095
+ },
29096
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
29097
+ );
27912
29098
 
27913
29099
  // src/instrumentation/plugins/flue-plugin.ts
27914
29100
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -28251,15 +29437,20 @@ var FlueObserveBridge = class {
28251
29437
  safeLog3(existing.span, { input, metadata });
28252
29438
  return;
28253
29439
  }
28254
- const span = startSpan({
28255
- name: `workflow:${workflowName}`,
28256
- spanAttributes: { type: "task" /* TASK */ },
28257
- startTime: eventTime(event.startedAt ?? event.timestamp),
28258
- event: {
28259
- input,
28260
- metadata
28261
- }
28262
- });
29440
+ const span = startSpan(
29441
+ withSpanInstrumentationName(
29442
+ {
29443
+ name: `workflow:${workflowName}`,
29444
+ spanAttributes: { type: "task" /* TASK */ },
29445
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29446
+ event: {
29447
+ input,
29448
+ metadata
29449
+ }
29450
+ },
29451
+ INSTRUMENTATION_NAMES.FLUE
29452
+ )
29453
+ );
28263
29454
  this.runsById.set(event.runId, { metadata, span });
28264
29455
  }
28265
29456
  handleRunResume(event, ctx) {
@@ -28279,12 +29470,17 @@ var FlueObserveBridge = class {
28279
29470
  safeLog3(existing.span, { metadata });
28280
29471
  return;
28281
29472
  }
28282
- const span = startSpan({
28283
- name: `workflow:${workflowName}`,
28284
- spanAttributes: { type: "task" /* TASK */ },
28285
- startTime: eventTime(event.startedAt ?? event.timestamp),
28286
- event: { metadata }
28287
- });
29473
+ const span = startSpan(
29474
+ withSpanInstrumentationName(
29475
+ {
29476
+ name: `workflow:${workflowName}`,
29477
+ spanAttributes: { type: "task" /* TASK */ },
29478
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29479
+ event: { metadata }
29480
+ },
29481
+ INSTRUMENTATION_NAMES.FLUE
29482
+ )
29483
+ );
28288
29484
  this.runsById.set(event.runId, { metadata, span });
28289
29485
  }
28290
29486
  handleRunEnd(event) {
@@ -28923,7 +30119,14 @@ function stateMatchesRun(state, runId) {
28923
30119
  return state.metadata["flue.run_id"] === runId;
28924
30120
  }
28925
30121
  function startFlueSpan(parent, args) {
28926
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
30122
+ return parent ? withCurrent(
30123
+ parent,
30124
+ () => startSpan(
30125
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30126
+ )
30127
+ ) : startSpan(
30128
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30129
+ );
28927
30130
  }
28928
30131
  function runWithCurrentSpanStore(span, next) {
28929
30132
  const state = _internalGetGlobalState();
@@ -29013,9 +30216,13 @@ var BraintrustLangChainCallbackHandler = class {
29013
30216
  ...this.options.debug ? { runId, parentRunId } : {}
29014
30217
  }
29015
30218
  };
29016
- let span = parentSpan.startSpan(args);
30219
+ const spanArgs = withSpanInstrumentationName(
30220
+ args,
30221
+ INSTRUMENTATION_NAMES.LANGCHAIN
30222
+ );
30223
+ let span = parentSpan.startSpan(spanArgs);
29017
30224
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
29018
- span = initLogger().startSpan(args);
30225
+ span = initLogger().startSpan(spanArgs);
29019
30226
  }
29020
30227
  this.spans.set(runId, span);
29021
30228
  }
@@ -29228,6 +30435,16 @@ function cleanObject(obj) {
29228
30435
  })
29229
30436
  );
29230
30437
  }
30438
+ function normalizeTokenMetrics(obj) {
30439
+ const metrics = cleanObject(obj);
30440
+ if (metrics.total_tokens !== void 0) {
30441
+ metrics.tokens = metrics.total_tokens;
30442
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
30443
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
30444
+ }
30445
+ delete metrics.total_tokens;
30446
+ return metrics;
30447
+ }
29231
30448
  function walkGenerations(response) {
29232
30449
  const result = [];
29233
30450
  const generations = response.generations || [];
@@ -29274,7 +30491,7 @@ function getMetricsFromResponse(response) {
29274
30491
  continue;
29275
30492
  }
29276
30493
  const inputTokenDetails = usageMetadata.input_token_details;
29277
- return cleanObject({
30494
+ return normalizeTokenMetrics({
29278
30495
  total_tokens: usageMetadata.total_tokens,
29279
30496
  prompt_tokens: usageMetadata.input_tokens,
29280
30497
  completion_tokens: usageMetadata.output_tokens,
@@ -29284,7 +30501,7 @@ function getMetricsFromResponse(response) {
29284
30501
  }
29285
30502
  const llmOutput = response.llmOutput || {};
29286
30503
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
29287
- return cleanObject({
30504
+ return normalizeTokenMetrics({
29288
30505
  total_tokens: tokenUsage.totalTokens,
29289
30506
  prompt_tokens: tokenUsage.promptTokens,
29290
30507
  completion_tokens: tokenUsage.completionTokens
@@ -29302,16 +30519,20 @@ function isRecord(value) {
29302
30519
  }
29303
30520
 
29304
30521
  // src/instrumentation/plugins/langchain-channels.ts
29305
- var langChainChannels = defineChannels("@langchain/core", {
29306
- configure: channel({
29307
- channelName: "CallbackManager.configure",
29308
- kind: "sync-stream"
29309
- }),
29310
- configureSync: channel({
29311
- channelName: "CallbackManager._configureSync",
29312
- kind: "sync-stream"
29313
- })
29314
- });
30522
+ var langChainChannels = defineChannels(
30523
+ "@langchain/core",
30524
+ {
30525
+ configure: channel({
30526
+ channelName: "CallbackManager.configure",
30527
+ kind: "sync-stream"
30528
+ }),
30529
+ configureSync: channel({
30530
+ channelName: "CallbackManager._configureSync",
30531
+ kind: "sync-stream"
30532
+ })
30533
+ },
30534
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
30535
+ );
29315
30536
 
29316
30537
  // src/instrumentation/plugins/langchain-plugin.ts
29317
30538
  var LangChainPlugin = class extends BasePlugin {
@@ -29399,20 +30620,24 @@ function isBraintrustHandler(handler) {
29399
30620
  }
29400
30621
 
29401
30622
  // src/instrumentation/plugins/langsmith-channels.ts
29402
- var langSmithChannels = defineChannels("langsmith", {
29403
- createRun: channel({
29404
- channelName: "Client.createRun",
29405
- kind: "async"
29406
- }),
29407
- updateRun: channel({
29408
- channelName: "Client.updateRun",
29409
- kind: "async"
29410
- }),
29411
- batchIngestRuns: channel({
29412
- channelName: "Client.batchIngestRuns",
29413
- kind: "async"
29414
- })
29415
- });
30623
+ var langSmithChannels = defineChannels(
30624
+ "langsmith",
30625
+ {
30626
+ createRun: channel({
30627
+ channelName: "Client.createRun",
30628
+ kind: "async"
30629
+ }),
30630
+ updateRun: channel({
30631
+ channelName: "Client.updateRun",
30632
+ kind: "async"
30633
+ }),
30634
+ batchIngestRuns: channel({
30635
+ channelName: "Client.batchIngestRuns",
30636
+ kind: "async"
30637
+ })
30638
+ },
30639
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
30640
+ );
29416
30641
 
29417
30642
  // src/instrumentation/plugins/langsmith-plugin.ts
29418
30643
  var MAX_COMPLETED_RUNS = 1e4;
@@ -29576,19 +30801,24 @@ var LangSmithPlugin = class extends BasePlugin {
29576
30801
  const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
29577
30802
  const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
29578
30803
  const startTime = timestampSeconds(ownValue(run, "start_time"));
29579
- return startSpan({
29580
- name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
29581
- spanId: id,
29582
- parentSpanIds: {
29583
- parentSpanIds: parentId ? [parentId] : [],
29584
- rootSpanId: traceId
29585
- },
29586
- spanAttributes: {
29587
- type: mapRunType(ownValue(run, "run_type"))
29588
- },
29589
- ...startTime === void 0 ? {} : { startTime },
29590
- event: { id }
29591
- });
30804
+ return startSpan(
30805
+ withSpanInstrumentationName(
30806
+ {
30807
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
30808
+ spanId: id,
30809
+ parentSpanIds: {
30810
+ parentSpanIds: parentId ? [parentId] : [],
30811
+ rootSpanId: traceId
30812
+ },
30813
+ spanAttributes: {
30814
+ type: mapRunType(ownValue(run, "run_type"))
30815
+ },
30816
+ ...startTime === void 0 ? {} : { startTime },
30817
+ event: { id }
30818
+ },
30819
+ INSTRUMENTATION_NAMES.LANGSMITH
30820
+ )
30821
+ );
29592
30822
  }
29593
30823
  logRun(span, run, previous, includeOutput) {
29594
30824
  const inputs = preferOwnValue(run, previous, "inputs");
@@ -29846,7 +31076,8 @@ var piCodingAgentChannels = defineChannels(
29846
31076
  channelName: "AgentSession.prompt",
29847
31077
  kind: "async"
29848
31078
  })
29849
- }
31079
+ },
31080
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
29850
31081
  );
29851
31082
 
29852
31083
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -29930,14 +31161,19 @@ function startPiPromptRun(event, onFinalize) {
29930
31161
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
29931
31162
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
29932
31163
  };
29933
- const span = startSpan({
29934
- event: {
29935
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
29936
- metadata
29937
- },
29938
- name: "AgentSession.prompt",
29939
- spanAttributes: { type: "task" /* TASK */ }
29940
- });
31164
+ const span = startSpan(
31165
+ withSpanInstrumentationName(
31166
+ {
31167
+ event: {
31168
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
31169
+ metadata
31170
+ },
31171
+ name: "AgentSession.prompt",
31172
+ spanAttributes: { type: "task" /* TASK */ }
31173
+ },
31174
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31175
+ )
31176
+ );
29941
31177
  const streamPatchState = installPiStreamPatch(agent);
29942
31178
  const options = event.arguments[1];
29943
31179
  const promptText = event.arguments[0];
@@ -30107,15 +31343,20 @@ async function startPiLlmSpan(state, model, context, options) {
30107
31343
  ...extractToolMetadata(context.tools),
30108
31344
  "pi_coding_agent.operation": "agent.streamFn"
30109
31345
  };
30110
- const span = startSpan({
30111
- event: {
30112
- input: processInputAttachments(normalizePiContextInput(context)),
30113
- metadata
30114
- },
30115
- name: getLlmSpanName(model),
30116
- parent: await state.span.export(),
30117
- spanAttributes: { type: "llm" /* LLM */ }
30118
- });
31346
+ const span = startSpan(
31347
+ withSpanInstrumentationName(
31348
+ {
31349
+ event: {
31350
+ input: processInputAttachments(normalizePiContextInput(context)),
31351
+ metadata
31352
+ },
31353
+ name: getLlmSpanName(model),
31354
+ parent: await state.span.export(),
31355
+ spanAttributes: { type: "llm" /* LLM */ }
31356
+ },
31357
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31358
+ )
31359
+ );
30119
31360
  const llmState = {
30120
31361
  finalized: false,
30121
31362
  metadata,
@@ -30283,15 +31524,20 @@ async function startPiToolSpan(state, event) {
30283
31524
  "pi_coding_agent.tool.name": event.toolName
30284
31525
  };
30285
31526
  try {
30286
- const span = startSpan({
30287
- event: {
30288
- input: event.args,
30289
- metadata
30290
- },
30291
- name: event.toolName || "tool",
30292
- parent: await state.span.export(),
30293
- spanAttributes: { type: "tool" /* TOOL */ }
30294
- });
31527
+ const span = startSpan(
31528
+ withSpanInstrumentationName(
31529
+ {
31530
+ event: {
31531
+ input: event.args,
31532
+ metadata
31533
+ },
31534
+ name: event.toolName || "tool",
31535
+ parent: await state.span.export(),
31536
+ spanAttributes: { type: "tool" /* TOOL */ }
31537
+ },
31538
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31539
+ )
31540
+ );
30295
31541
  state.activeToolSpans.set(event.toolCallId, {
30296
31542
  restoreAutoInstrumentation,
30297
31543
  span
@@ -30723,20 +31969,24 @@ function logInstrumentationError4(context, error) {
30723
31969
  }
30724
31970
 
30725
31971
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
30726
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
30727
- agentStream: channel({
30728
- channelName: "Agent.stream",
30729
- kind: "sync-stream"
30730
- }),
30731
- graphStream: channel({
30732
- channelName: "Graph.stream",
30733
- kind: "sync-stream"
30734
- }),
30735
- swarmStream: channel({
30736
- channelName: "Swarm.stream",
30737
- kind: "sync-stream"
30738
- })
30739
- });
31972
+ var strandsAgentSDKChannels = defineChannels(
31973
+ "@strands-agents/sdk",
31974
+ {
31975
+ agentStream: channel({
31976
+ channelName: "Agent.stream",
31977
+ kind: "sync-stream"
31978
+ }),
31979
+ graphStream: channel({
31980
+ channelName: "Graph.stream",
31981
+ kind: "sync-stream"
31982
+ }),
31983
+ swarmStream: channel({
31984
+ channelName: "Swarm.stream",
31985
+ kind: "sync-stream"
31986
+ })
31987
+ },
31988
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
31989
+ );
30740
31990
 
30741
31991
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
30742
31992
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -30891,22 +32141,32 @@ function startAgentStream(event, activeChildParents) {
30891
32141
  );
30892
32142
  const span = parentSpan ? withCurrent(
30893
32143
  parentSpan,
30894
- () => startSpan({
30895
- event: {
30896
- input,
30897
- metadata
32144
+ () => startSpan(
32145
+ withSpanInstrumentationName(
32146
+ {
32147
+ event: {
32148
+ input,
32149
+ metadata
32150
+ },
32151
+ name: formatAgentSpanName(agent),
32152
+ spanAttributes: { type: "task" /* TASK */ }
32153
+ },
32154
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32155
+ )
32156
+ )
32157
+ ) : startSpan(
32158
+ withSpanInstrumentationName(
32159
+ {
32160
+ event: {
32161
+ input,
32162
+ metadata
32163
+ },
32164
+ name: formatAgentSpanName(agent),
32165
+ spanAttributes: { type: "task" /* TASK */ }
30898
32166
  },
30899
- name: formatAgentSpanName(agent),
30900
- spanAttributes: { type: "task" /* TASK */ }
30901
- })
30902
- ) : startSpan({
30903
- event: {
30904
- input,
30905
- metadata
30906
- },
30907
- name: formatAgentSpanName(agent),
30908
- spanAttributes: { type: "task" /* TASK */ }
30909
- });
32167
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32168
+ )
32169
+ );
30910
32170
  return {
30911
32171
  activeTools: /* @__PURE__ */ new Map(),
30912
32172
  attachmentCache,
@@ -30928,22 +32188,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30928
32188
  const input = processStrandsInputAttachments(event.arguments[0]);
30929
32189
  const span = parentSpan ? withCurrent(
30930
32190
  parentSpan,
30931
- () => startSpan({
30932
- event: {
30933
- input,
30934
- metadata
32191
+ () => startSpan(
32192
+ withSpanInstrumentationName(
32193
+ {
32194
+ event: {
32195
+ input,
32196
+ metadata
32197
+ },
32198
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32199
+ spanAttributes: { type: "task" /* TASK */ }
32200
+ },
32201
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32202
+ )
32203
+ )
32204
+ ) : startSpan(
32205
+ withSpanInstrumentationName(
32206
+ {
32207
+ event: {
32208
+ input,
32209
+ metadata
32210
+ },
32211
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32212
+ spanAttributes: { type: "task" /* TASK */ }
30935
32213
  },
30936
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30937
- spanAttributes: { type: "task" /* TASK */ }
30938
- })
30939
- ) : startSpan({
30940
- event: {
30941
- input,
30942
- metadata
30943
- },
30944
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30945
- spanAttributes: { type: "task" /* TASK */ }
30946
- });
32214
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32215
+ )
32216
+ );
30947
32217
  return {
30948
32218
  activeNodes: /* @__PURE__ */ new Map(),
30949
32219
  finalized: false,
@@ -31045,17 +32315,22 @@ function startModelSpan(state, event) {
31045
32315
  };
31046
32316
  const span = withCurrent(
31047
32317
  state.span,
31048
- () => startSpan({
31049
- event: {
31050
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
31051
- event.agent.messages,
31052
- state.attachmentCache
31053
- ) : void 0,
31054
- metadata
31055
- },
31056
- name: formatModelSpanName(model),
31057
- spanAttributes: { type: "llm" /* LLM */ }
31058
- })
32318
+ () => startSpan(
32319
+ withSpanInstrumentationName(
32320
+ {
32321
+ event: {
32322
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
32323
+ event.agent.messages,
32324
+ state.attachmentCache
32325
+ ) : void 0,
32326
+ metadata
32327
+ },
32328
+ name: formatModelSpanName(model),
32329
+ spanAttributes: { type: "llm" /* LLM */ }
32330
+ },
32331
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32332
+ )
32333
+ )
31059
32334
  );
31060
32335
  state.activeModel = {
31061
32336
  metadata,
@@ -31107,20 +32382,25 @@ function startToolSpan2(state, event) {
31107
32382
  const name = extractToolName3(toolUse, event.tool);
31108
32383
  const span = withCurrent(
31109
32384
  state.span,
31110
- () => startSpan({
31111
- event: {
31112
- input: toolUse?.input,
31113
- metadata: {
31114
- "gen_ai.tool.call.id": toolUse?.toolUseId,
31115
- "gen_ai.tool.name": name,
31116
- "strands.operation": "tool.call",
31117
- "strands.tool.name": name,
31118
- provider: "strands"
31119
- }
31120
- },
31121
- name: `tool: ${name}`,
31122
- spanAttributes: { type: "tool" /* TOOL */ }
31123
- })
32385
+ () => startSpan(
32386
+ withSpanInstrumentationName(
32387
+ {
32388
+ event: {
32389
+ input: toolUse?.input,
32390
+ metadata: {
32391
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
32392
+ "gen_ai.tool.name": name,
32393
+ "strands.operation": "tool.call",
32394
+ "strands.tool.name": name,
32395
+ provider: "strands"
32396
+ }
32397
+ },
32398
+ name: `tool: ${name}`,
32399
+ spanAttributes: { type: "tool" /* TOOL */ }
32400
+ },
32401
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32402
+ )
32403
+ )
31124
32404
  );
31125
32405
  state.activeTools.set(key, {
31126
32406
  span,
@@ -31191,11 +32471,16 @@ function startNodeSpan(state, event, activeChildParents) {
31191
32471
  };
31192
32472
  const span = withCurrent(
31193
32473
  state.span,
31194
- () => startSpan({
31195
- event: { metadata },
31196
- name: `node: ${nodeId}`,
31197
- spanAttributes: { type: "task" /* TASK */ }
31198
- })
32474
+ () => startSpan(
32475
+ withSpanInstrumentationName(
32476
+ {
32477
+ event: { metadata },
32478
+ name: `node: ${nodeId}`,
32479
+ spanAttributes: { type: "task" /* TASK */ }
32480
+ },
32481
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32482
+ )
32483
+ )
31199
32484
  );
31200
32485
  const nodeState = {
31201
32486
  ...child ? { child } : {},
@@ -32973,6 +34258,26 @@ function wrapAISDK(aiSDK, options = {}) {
32973
34258
  }
32974
34259
  });
32975
34260
  }
34261
+ function isHarnessAgentInstance(instance) {
34262
+ try {
34263
+ const visited = /* @__PURE__ */ new Set();
34264
+ let prototype = Object.getPrototypeOf(instance);
34265
+ while (prototype !== null && !visited.has(prototype)) {
34266
+ visited.add(prototype);
34267
+ const constructor = Object.getOwnPropertyDescriptor(
34268
+ prototype,
34269
+ "constructor"
34270
+ )?.value;
34271
+ const constructorName = typeof constructor === "function" ? Object.getOwnPropertyDescriptor(constructor, "name")?.value : void 0;
34272
+ if (constructorName === "HarnessAgent") {
34273
+ return true;
34274
+ }
34275
+ prototype = Object.getPrototypeOf(prototype);
34276
+ }
34277
+ } catch {
34278
+ }
34279
+ return false;
34280
+ }
32976
34281
  var wrapAgentClass = (AgentClass, options = {}) => {
32977
34282
  const typedAgentClass = AgentClass;
32978
34283
  return new Proxy(typedAgentClass, {
@@ -32982,9 +34287,52 @@ var wrapAgentClass = (AgentClass, options = {}) => {
32982
34287
  args,
32983
34288
  newTarget
32984
34289
  );
34290
+ const harnessAgent = isHarnessAgentInstance(instance) ? instance : null;
32985
34291
  return new Proxy(instance, {
32986
34292
  get(instanceTarget, prop, instanceReceiver) {
32987
34293
  const original = Reflect.get(instanceTarget, prop, instanceTarget);
34294
+ if (harnessAgent && typeof original === "function") {
34295
+ switch (prop) {
34296
+ case "createSession":
34297
+ return wrapHarnessAgentCreateSession(
34298
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
34299
+ original,
34300
+ harnessAgent
34301
+ );
34302
+ case "generate":
34303
+ return wrapHarnessAgentGenerate(
34304
+ original,
34305
+ harnessAgent,
34306
+ harnessAgentChannels.generate,
34307
+ "HarnessAgent.generate",
34308
+ options
34309
+ );
34310
+ case "stream":
34311
+ return wrapHarnessAgentStream(
34312
+ original,
34313
+ harnessAgent,
34314
+ harnessAgentChannels.stream,
34315
+ "HarnessAgent.stream",
34316
+ options
34317
+ );
34318
+ case "continueGenerate":
34319
+ return wrapHarnessAgentGenerate(
34320
+ original,
34321
+ harnessAgent,
34322
+ harnessAgentChannels.continueGenerate,
34323
+ "HarnessAgent.continueGenerate",
34324
+ options
34325
+ );
34326
+ case "continueStream":
34327
+ return wrapHarnessAgentStream(
34328
+ original,
34329
+ harnessAgent,
34330
+ harnessAgentChannels.continueStream,
34331
+ "HarnessAgent.continueStream",
34332
+ options
34333
+ );
34334
+ }
34335
+ }
32988
34336
  if (prop === "generate" && typeof original === "function" && instanceTarget.constructor.name !== "WorkflowAgent") {
32989
34337
  return wrapAgentGenerate(original, instanceTarget, options);
32990
34338
  }
@@ -33000,6 +34348,39 @@ var wrapAgentClass = (AgentClass, options = {}) => {
33000
34348
  }
33001
34349
  });
33002
34350
  };
34351
+ var wrapHarnessAgentCreateSession = (createSession, instance) => {
34352
+ const wrapper = function(params) {
34353
+ return harnessAgentChannels.createSession.tracePromise(
34354
+ () => params === void 0 ? createSession.call(instance) : createSession.call(instance, params),
34355
+ createAISDKChannelContext(params ?? {}, { self: instance })
34356
+ );
34357
+ };
34358
+ Object.defineProperty(wrapper, "name", {
34359
+ value: "HarnessAgent.createSession",
34360
+ writable: false
34361
+ });
34362
+ return wrapper;
34363
+ };
34364
+ var wrapHarnessAgentGenerate = (generate, instance, channel2, name, options) => makeGenerateTextWrapper(
34365
+ channel2,
34366
+ name,
34367
+ generate.bind(instance),
34368
+ {
34369
+ self: instance,
34370
+ spanType: "task" /* TASK */
34371
+ },
34372
+ options
34373
+ );
34374
+ var wrapHarnessAgentStream = (stream, instance, channel2, name, options) => makeStreamWrapper(
34375
+ channel2,
34376
+ name,
34377
+ stream.bind(instance),
34378
+ {
34379
+ self: instance,
34380
+ spanType: "task" /* TASK */
34381
+ },
34382
+ options
34383
+ );
33003
34384
  var wrapAgentGenerate = (generate, instance, options = {}) => {
33004
34385
  const defaultName = `${instance.constructor.name}.generate`;
33005
34386
  return async (params) => makeGenerateTextWrapper(
@@ -33226,6 +34607,7 @@ var BraintrustLanguageModelWrapper = class {
33226
34607
  this.supportsUrl = (url) => this.model.supportsUrl(url);
33227
34608
  }
33228
34609
  }
34610
+ model;
33229
34611
  supportsUrl;
33230
34612
  get specificationVersion() {
33231
34613
  return this.model.specificationVersion;
@@ -33248,12 +34630,17 @@ var BraintrustLanguageModelWrapper = class {
33248
34630
  // For the first cut, do not support custom span_info arguments. We can
33249
34631
  // propagate those via async local storage
33250
34632
  async doGenerate(options) {
33251
- const span = startSpan({
33252
- name: "Chat Completion",
33253
- spanAttributes: {
33254
- type: "llm"
33255
- }
33256
- });
34633
+ const span = startSpan(
34634
+ withSpanInstrumentationName(
34635
+ {
34636
+ name: "Chat Completion",
34637
+ spanAttributes: {
34638
+ type: "llm"
34639
+ }
34640
+ },
34641
+ INSTRUMENTATION_NAMES.AI_SDK
34642
+ )
34643
+ );
33257
34644
  const { prompt, mode, ...rest } = options;
33258
34645
  const startTime = getCurrentUnixTimestamp();
33259
34646
  try {
@@ -33284,12 +34671,17 @@ var BraintrustLanguageModelWrapper = class {
33284
34671
  async doStream(options) {
33285
34672
  const { prompt, mode, ...rest } = options;
33286
34673
  const startTime = getCurrentUnixTimestamp();
33287
- const span = startSpan({
33288
- name: "Chat Completion",
33289
- spanAttributes: {
33290
- type: "llm"
33291
- }
33292
- });
34674
+ const span = startSpan(
34675
+ withSpanInstrumentationName(
34676
+ {
34677
+ name: "Chat Completion",
34678
+ spanAttributes: {
34679
+ type: "llm"
34680
+ }
34681
+ },
34682
+ INSTRUMENTATION_NAMES.AI_SDK
34683
+ )
34684
+ );
33293
34685
  span.log({
33294
34686
  input: postProcessPrompt(prompt),
33295
34687
  metadata: {
@@ -33644,7 +35036,9 @@ function BraintrustMiddleware(config = {}) {
33644
35036
  }
33645
35037
  }
33646
35038
  };
33647
- const span = startSpan(spanArgs);
35039
+ const span = startSpan(
35040
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
35041
+ );
33648
35042
  try {
33649
35043
  const result = await doGenerate();
33650
35044
  const metadata = {};
@@ -33706,7 +35100,9 @@ function BraintrustMiddleware(config = {}) {
33706
35100
  }
33707
35101
  }
33708
35102
  };
33709
- const span = startSpan(spanArgs);
35103
+ const span = startSpan(
35104
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
35105
+ );
33710
35106
  try {
33711
35107
  const { stream, ...rest } = await doStream();
33712
35108
  const textChunks = [];
@@ -33808,6 +35204,7 @@ function BraintrustMiddleware(config = {}) {
33808
35204
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
33809
35205
  var MAX_EVE_CACHE_ENTRIES = 1e4;
33810
35206
  var MAX_STORED_LLM_INPUTS = 100;
35207
+ var MAX_STORED_REASONING_BLOCKS = 100;
33811
35208
  var MAX_STORED_SPAN_REFERENCES = 1e4;
33812
35209
  var MAX_STORED_STEP_STARTS = 1e4;
33813
35210
  function braintrustEveHook(options) {
@@ -33846,6 +35243,7 @@ var ResumedEveSpan = class {
33846
35243
  this.reference = reference;
33847
35244
  this.endTime = reference.endTime;
33848
35245
  }
35246
+ reference;
33849
35247
  endTime;
33850
35248
  get rootSpanId() {
33851
35249
  return this.reference.rootSpanId;
@@ -33878,6 +35276,7 @@ var EveBridge = class {
33878
35276
  constructor(state) {
33879
35277
  this.state = state;
33880
35278
  }
35279
+ state;
33881
35280
  eventQueuesBySession = /* @__PURE__ */ new Map();
33882
35281
  completedToolKeys = new LRUCache({
33883
35282
  max: MAX_EVE_CACHE_ENTRIES
@@ -33910,7 +35309,12 @@ var EveBridge = class {
33910
35309
  };
33911
35310
  const span = withCurrent(
33912
35311
  NOOP_SPAN,
33913
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
35312
+ () => _internalStartSpanWithInitialMerge(
35313
+ withSpanInstrumentationName(
35314
+ { ...args, startTime },
35315
+ INSTRUMENTATION_NAMES.EVE
35316
+ )
35317
+ )
33914
35318
  );
33915
35319
  if (typeof rowId !== "string") {
33916
35320
  return span;
@@ -34056,6 +35460,9 @@ var EveBridge = class {
34056
35460
  case "step.started":
34057
35461
  await this.handleStepStarted(event, ctx, hookMetadata);
34058
35462
  return true;
35463
+ case "reasoning.completed":
35464
+ this.handleReasoningCompleted(event, ctx);
35465
+ return true;
34059
35466
  case "message.completed":
34060
35467
  this.handleMessageCompleted(event, ctx);
34061
35468
  return true;
@@ -34147,6 +35554,7 @@ var EveBridge = class {
34147
35554
  key,
34148
35555
  metadata,
34149
35556
  metrics: {},
35557
+ sessionId,
34150
35558
  span,
34151
35559
  stepsByIndex: /* @__PURE__ */ new Map(),
34152
35560
  turnId: event.data.turnId
@@ -34177,6 +35585,12 @@ var EveBridge = class {
34177
35585
  const endTime = eventTime2(event);
34178
35586
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
34179
35587
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35588
+ clearStoredEveReasoning(
35589
+ this.state,
35590
+ sessionId,
35591
+ event.data.turnId,
35592
+ event.data.stepIndex
35593
+ );
34180
35594
  }
34181
35595
  const stepOrdinal = this.stepOrdinal(event);
34182
35596
  const metadata = { ...turn.metadata };
@@ -34186,6 +35600,13 @@ var EveBridge = class {
34186
35600
  event.data.turnId,
34187
35601
  event.data.stepIndex
34188
35602
  );
35603
+ const reasoning = readStoredEveReasoning(
35604
+ this.state,
35605
+ sessionId,
35606
+ event.data.turnId,
35607
+ event.data.stepIndex
35608
+ );
35609
+ const output = mergeEveReasoning(void 0, reasoning);
34189
35610
  const { rowId: eventId, spanId } = await generateEveIds(
34190
35611
  "step",
34191
35612
  sessionId,
@@ -34203,32 +35624,52 @@ var EveBridge = class {
34203
35624
  spanId,
34204
35625
  startTime: eventTime2(event)
34205
35626
  });
34206
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
35627
+ span.log({
35628
+ ...input !== void 0 ? { input } : {},
35629
+ metadata
35630
+ });
34207
35631
  turn.stepsByIndex.set(event.data.stepIndex, {
34208
35632
  ...input !== void 0 ? { input } : {},
34209
35633
  metadata,
34210
35634
  metrics: {},
35635
+ ...output !== void 0 ? { output } : {},
35636
+ reasoning,
34211
35637
  span
34212
35638
  });
34213
35639
  }
35640
+ handleReasoningCompleted(event, ctx) {
35641
+ const sessionId = sessionIdFromContext(ctx);
35642
+ if (!sessionId) {
35643
+ return;
35644
+ }
35645
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
35646
+ const step = this.stepForEvent(event, ctx);
35647
+ if (step) {
35648
+ step.reasoning = reasoning;
35649
+ step.output = mergeEveReasoning(step.output, reasoning);
35650
+ }
35651
+ }
34214
35652
  handleMessageCompleted(event, ctx) {
34215
35653
  const step = this.stepForEvent(event, ctx);
34216
35654
  if (!step) {
34217
35655
  return;
34218
35656
  }
34219
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
35657
+ const existingMessage = eveOutputMessage(step.output);
34220
35658
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
34221
- step.output = [
34222
- {
34223
- finish_reason: normalizedFinishReason(event.data.finishReason),
34224
- index: 0,
34225
- message: {
34226
- content: event.data.message,
34227
- role: "assistant",
34228
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35659
+ step.output = mergeEveReasoning(
35660
+ [
35661
+ {
35662
+ finish_reason: normalizedFinishReason(event.data.finishReason),
35663
+ index: 0,
35664
+ message: {
35665
+ content: event.data.message,
35666
+ role: "assistant",
35667
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35668
+ }
34229
35669
  }
34230
- }
34231
- ];
35670
+ ],
35671
+ step.reasoning
35672
+ );
34232
35673
  const turn = this.turnForEvent(event, ctx);
34233
35674
  if (turn && event.data.finishReason !== "tool-calls") {
34234
35675
  turn.output = event.data.message;
@@ -34237,16 +35678,19 @@ var EveBridge = class {
34237
35678
  handleResultCompleted(event, ctx) {
34238
35679
  const step = this.stepForEvent(event, ctx);
34239
35680
  if (step) {
34240
- step.output = [
34241
- {
34242
- finish_reason: "stop",
34243
- index: 0,
34244
- message: {
34245
- content: event.data.result,
34246
- role: "assistant"
35681
+ step.output = mergeEveReasoning(
35682
+ [
35683
+ {
35684
+ finish_reason: "stop",
35685
+ index: 0,
35686
+ message: {
35687
+ content: event.data.result,
35688
+ role: "assistant"
35689
+ }
34247
35690
  }
34248
- }
34249
- ];
35691
+ ],
35692
+ step.reasoning
35693
+ );
34250
35694
  }
34251
35695
  const turn = this.turnForEvent(event, ctx);
34252
35696
  if (turn) {
@@ -34296,18 +35740,20 @@ var EveBridge = class {
34296
35740
  type: "function"
34297
35741
  });
34298
35742
  }
34299
- step.output = [
34300
- {
34301
- finish_reason: "tool_calls",
34302
- index: 0,
34303
- message: {
34304
- content: null,
34305
- role: "assistant",
34306
- tool_calls: [...toolCallsById.values()]
35743
+ step.output = mergeEveReasoning(
35744
+ [
35745
+ {
35746
+ finish_reason: "tool_calls",
35747
+ index: 0,
35748
+ message: {
35749
+ content: null,
35750
+ role: "assistant",
35751
+ tool_calls: [...toolCallsById.values()]
35752
+ }
34307
35753
  }
34308
- }
34309
- ];
34310
- step.span.log({ metadata: step.metadata, output: step.output });
35754
+ ],
35755
+ step.reasoning
35756
+ );
34311
35757
  }
34312
35758
  async handleActionResult(event, ctx, hookMetadata) {
34313
35759
  if (isToolResult(event.data.result)) {
@@ -34485,6 +35931,7 @@ var EveBridge = class {
34485
35931
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
34486
35932
  };
34487
35933
  step.metrics = { ...step.metrics, ...metrics };
35934
+ const sessionId = sessionIdFromContext(ctx);
34488
35935
  if (Array.isArray(step.output) && isObject(step.output[0])) {
34489
35936
  const finishReason = step.output[0].finish_reason;
34490
35937
  if (typeof finishReason !== "string") {
@@ -34509,6 +35956,14 @@ var EveBridge = class {
34509
35956
  turn.stepsByIndex.delete(event.data.stepIndex);
34510
35957
  }
34511
35958
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35959
+ if (sessionId) {
35960
+ clearStoredEveReasoning(
35961
+ this.state,
35962
+ sessionId,
35963
+ event.data.turnId,
35964
+ event.data.stepIndex
35965
+ );
35966
+ }
34512
35967
  }
34513
35968
  handleStepFailed(event, ctx) {
34514
35969
  const step = this.stepForEvent(event, ctx);
@@ -34526,6 +35981,15 @@ var EveBridge = class {
34526
35981
  const turn = this.turnForEvent(event, ctx);
34527
35982
  turn?.stepsByIndex.delete(event.data.stepIndex);
34528
35983
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35984
+ const sessionId = sessionIdFromContext(ctx);
35985
+ if (sessionId) {
35986
+ clearStoredEveReasoning(
35987
+ this.state,
35988
+ sessionId,
35989
+ event.data.turnId,
35990
+ event.data.stepIndex
35991
+ );
35992
+ }
34529
35993
  }
34530
35994
  handleTurnCompleted(event, ctx) {
34531
35995
  const turn = this.turnForEvent(event, ctx);
@@ -34623,6 +36087,7 @@ var EveBridge = class {
34623
36087
  key,
34624
36088
  metadata,
34625
36089
  metrics: {},
36090
+ sessionId,
34626
36091
  span,
34627
36092
  stepsByIndex: /* @__PURE__ */ new Map(),
34628
36093
  turnId: event.data.turnId
@@ -34885,6 +36350,9 @@ var EveBridge = class {
34885
36350
  const normalized = normalizeEveTraceState(current);
34886
36351
  return {
34887
36352
  ...normalized,
36353
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36354
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
36355
+ ),
34888
36356
  stepStarts: normalized.stepStarts.filter(
34889
36357
  (entry) => entry.turnId !== turn.turnId
34890
36358
  )
@@ -34924,6 +36392,7 @@ function emptyEveTraceState() {
34924
36392
  return {
34925
36393
  llmInputs: [],
34926
36394
  metadata: {},
36395
+ reasoningBlocks: [],
34927
36396
  spanReferences: [],
34928
36397
  stepStarts: []
34929
36398
  };
@@ -34974,6 +36443,21 @@ function normalizeEveTraceState(state) {
34974
36443
  const input = entry["input"];
34975
36444
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
34976
36445
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
36446
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
36447
+ if (!isObject(entry)) {
36448
+ return [];
36449
+ }
36450
+ const content = entry["content"];
36451
+ const eventAt = entry["eventAt"];
36452
+ const key = entry["key"];
36453
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
36454
+ {
36455
+ content,
36456
+ ...typeof eventAt === "string" ? { eventAt } : {},
36457
+ key
36458
+ }
36459
+ ] : [];
36460
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
34977
36461
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
34978
36462
  if (!isObject(entry)) {
34979
36463
  return [];
@@ -34984,7 +36468,13 @@ function normalizeEveTraceState(state) {
34984
36468
  const turnId = entry["turnId"];
34985
36469
  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 }] : [];
34986
36470
  }).slice(-MAX_STORED_STEP_STARTS) : [];
34987
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
36471
+ return {
36472
+ llmInputs,
36473
+ metadata: { ...metadata },
36474
+ reasoningBlocks,
36475
+ spanReferences,
36476
+ stepStarts
36477
+ };
34988
36478
  }
34989
36479
  function readEveTraceState(state) {
34990
36480
  try {
@@ -34993,23 +36483,85 @@ function readEveTraceState(state) {
34993
36483
  return emptyEveTraceState();
34994
36484
  }
34995
36485
  }
34996
- function captureEveModelInput(state, input) {
34997
- if (!isObject(input)) {
34998
- return;
34999
- }
35000
- const session = input["session"];
35001
- const turn = input["turn"];
35002
- const step = input["step"];
35003
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
35004
- return;
35005
- }
35006
- const sessionId = session["id"];
35007
- const turnId = turn["id"];
35008
- const stepIndex = step["index"];
35009
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
35010
- return;
36486
+ function storeEveReasoning(state, sessionId, event) {
36487
+ const eventAt = event.meta?.at;
36488
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
36489
+ let stored = [];
36490
+ state.update((current) => {
36491
+ const normalized = normalizeEveTraceState(current);
36492
+ const alreadyStored = normalized.reasoningBlocks.some(
36493
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
36494
+ );
36495
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
36496
+ ...normalized.reasoningBlocks,
36497
+ {
36498
+ content: event.data.reasoning,
36499
+ ...eventAt ? { eventAt } : {},
36500
+ key
36501
+ }
36502
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
36503
+ stored = reasoningBlocks.flatMap(
36504
+ (entry) => entry.key === key ? [
36505
+ {
36506
+ content: entry.content,
36507
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36508
+ }
36509
+ ] : []
36510
+ );
36511
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
36512
+ });
36513
+ return stored;
36514
+ }
36515
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36516
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36517
+ return readEveTraceState(state).reasoningBlocks.flatMap(
36518
+ (entry) => entry.key === key ? [
36519
+ {
36520
+ content: entry.content,
36521
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36522
+ }
36523
+ ] : []
36524
+ );
36525
+ }
36526
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36527
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36528
+ state.update((current) => {
36529
+ const normalized = normalizeEveTraceState(current);
36530
+ return {
36531
+ ...normalized,
36532
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36533
+ (entry) => entry.key !== key
36534
+ )
36535
+ };
36536
+ });
36537
+ }
36538
+ function eveOutputMessage(output) {
36539
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
36540
+ }
36541
+ function mergeEveReasoning(output, reasoning) {
36542
+ if (reasoning.length === 0) {
36543
+ return output;
35011
36544
  }
35012
- const captured = capturedModelInput(input["modelInput"]);
36545
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
36546
+ const message = isObject(choice["message"]) ? choice["message"] : {};
36547
+ return [
36548
+ {
36549
+ ...choice,
36550
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
36551
+ message: {
36552
+ ...message,
36553
+ content: "content" in message ? message["content"] : null,
36554
+ reasoning: reasoning.map((block) => ({ content: block.content })),
36555
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
36556
+ }
36557
+ }
36558
+ ];
36559
+ }
36560
+ function captureEveModelInput(state, input) {
36561
+ const sessionId = input.session.id;
36562
+ const turnId = input.turn.id;
36563
+ const stepIndex = input.step.index;
36564
+ const captured = capturedModelInput(input.modelInput);
35013
36565
  if (!captured) {
35014
36566
  return;
35015
36567
  }
@@ -35050,18 +36602,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
35050
36602
  }
35051
36603
  }
35052
36604
  function capturedModelInput(modelInput) {
35053
- if (!isObject(modelInput)) {
35054
- return void 0;
35055
- }
35056
- const messages = modelInput["messages"];
35057
- if (!Array.isArray(messages)) {
35058
- return void 0;
35059
- }
35060
- const instructions = modelInput["instructions"];
35061
- const value = [
35062
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
35063
- ...messages
35064
- ];
36605
+ const { instructions, messages } = modelInput;
36606
+ const value = [];
36607
+ if (typeof instructions === "string") {
36608
+ value.push({ content: instructions, role: "system" });
36609
+ } else if (instructions) {
36610
+ value.push(...instructions.map(capturedEveModelMessage));
36611
+ }
36612
+ value.push(...messages.map(capturedEveModelMessage));
35065
36613
  try {
35066
36614
  const cloned = JSON.parse(JSON.stringify(value));
35067
36615
  if (!Array.isArray(cloned)) {
@@ -35072,8 +36620,123 @@ function capturedModelInput(modelInput) {
35072
36620
  return void 0;
35073
36621
  }
35074
36622
  }
36623
+ function capturedEveModelMessage(message) {
36624
+ const { content, role } = message;
36625
+ if (typeof content === "string") {
36626
+ return { content, role };
36627
+ }
36628
+ return { content: content.map(capturedEveModelContentPart), role };
36629
+ }
36630
+ function capturedEveModelContentPart(part) {
36631
+ switch (part.type) {
36632
+ case "text":
36633
+ case "reasoning":
36634
+ return { text: part.text, type: part.type };
36635
+ case "image":
36636
+ return {
36637
+ image: part.image,
36638
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36639
+ type: "image"
36640
+ };
36641
+ case "file":
36642
+ case "reasoning-file":
36643
+ return {
36644
+ data: part.data,
36645
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
36646
+ mediaType: part.mediaType,
36647
+ type: part.type
36648
+ };
36649
+ case "custom":
36650
+ return {
36651
+ ..."kind" in part ? { kind: part.kind } : {},
36652
+ type: "custom"
36653
+ };
36654
+ case "tool-call":
36655
+ return {
36656
+ input: part.input,
36657
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36658
+ toolCallId: part.toolCallId,
36659
+ toolName: part.toolName,
36660
+ type: "tool-call"
36661
+ };
36662
+ case "tool-result": {
36663
+ const output = part.output;
36664
+ let capturedOutput;
36665
+ switch (output.type) {
36666
+ case "text":
36667
+ case "error-text":
36668
+ capturedOutput = { type: output.type, value: output.value };
36669
+ break;
36670
+ case "json":
36671
+ case "error-json":
36672
+ capturedOutput = { type: output.type, value: output.value };
36673
+ break;
36674
+ case "execution-denied":
36675
+ capturedOutput = {
36676
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
36677
+ type: "execution-denied"
36678
+ };
36679
+ break;
36680
+ case "content":
36681
+ capturedOutput = {
36682
+ type: "content",
36683
+ value: output.value.map(capturedEveModelContentPart)
36684
+ };
36685
+ break;
36686
+ }
36687
+ return {
36688
+ output: capturedOutput,
36689
+ toolCallId: part.toolCallId,
36690
+ toolName: part.toolName,
36691
+ type: "tool-result"
36692
+ };
36693
+ }
36694
+ case "tool-approval-request":
36695
+ return {
36696
+ approvalId: part.approvalId,
36697
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
36698
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
36699
+ toolCallId: part.toolCallId,
36700
+ type: "tool-approval-request"
36701
+ };
36702
+ case "tool-approval-response":
36703
+ return {
36704
+ approvalId: part.approvalId,
36705
+ approved: part.approved,
36706
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36707
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
36708
+ type: "tool-approval-response"
36709
+ };
36710
+ case "file-data":
36711
+ case "image-data":
36712
+ return {
36713
+ data: part.data,
36714
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
36715
+ mediaType: part.mediaType,
36716
+ type: part.type
36717
+ };
36718
+ case "file-url":
36719
+ case "image-url":
36720
+ return {
36721
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36722
+ type: part.type,
36723
+ url: part.url
36724
+ };
36725
+ case "file-id":
36726
+ case "image-file-id":
36727
+ return { fileId: part.fileId, type: part.type };
36728
+ case "file-reference":
36729
+ case "image-file-reference":
36730
+ return {
36731
+ providerReference: part.providerReference,
36732
+ type: part.type
36733
+ };
36734
+ }
36735
+ }
35075
36736
  function isCapturedModelInput(input) {
35076
- return Array.isArray(input);
36737
+ return Array.isArray(input) && input.every(
36738
+ (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))
36739
+ );
35077
36740
  }
35078
36741
  function llmInputKey(sessionId, turnId, stepIndex) {
35079
36742
  return `${sessionId}\0${turnId}\0${stepIndex}`;
@@ -35425,17 +37088,20 @@ var BraintrustObservabilityExporter = class {
35425
37088
  }
35426
37089
  onStart(exported) {
35427
37090
  if (this.spans.has(exported.id)) return;
35428
- const args = {
35429
- name: exported.name,
35430
- spanAttributes: { type: spanTypeFor(exported.type) },
35431
- startTime: epochSeconds(exported.startTime),
35432
- // Use the Mastra span id as the Braintrust row id so that
35433
- // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35434
- // attach to the right row. Without this, `SpanImpl` auto-generates a
35435
- // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35436
- // external caller could know.
35437
- event: { id: exported.id }
35438
- };
37091
+ const args = withSpanInstrumentationName(
37092
+ {
37093
+ name: exported.name,
37094
+ spanAttributes: { type: spanTypeFor(exported.type) },
37095
+ startTime: epochSeconds(exported.startTime),
37096
+ // Use the Mastra span id as the Braintrust row id so that
37097
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
37098
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
37099
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
37100
+ // external caller could know.
37101
+ event: { id: exported.id }
37102
+ },
37103
+ INSTRUMENTATION_NAMES.MASTRA
37104
+ );
35439
37105
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
35440
37106
  if (!this.capturedParent) {
35441
37107
  const probe = currentSpan();
@@ -38468,6 +40134,8 @@ var BaseNode = class {
38468
40134
  this.graph = graph;
38469
40135
  this.id = id;
38470
40136
  }
40137
+ graph;
40138
+ id;
38471
40139
  __type = "node";
38472
40140
  dependencies = [];
38473
40141
  addDependency(dependency) {
@@ -38501,6 +40169,7 @@ var PromptNode = class extends BaseNode {
38501
40169
  super(graph, id);
38502
40170
  this.prompt = prompt;
38503
40171
  }
40172
+ prompt;
38504
40173
  async build(context) {
38505
40174
  return {
38506
40175
  type: "function",
@@ -38513,6 +40182,7 @@ var GateNode = class extends BaseNode {
38513
40182
  super(graph, id);
38514
40183
  this.condition = condition;
38515
40184
  }
40185
+ condition;
38516
40186
  async build(context) {
38517
40187
  return {
38518
40188
  type: "gate",
@@ -38537,6 +40207,7 @@ var PromptTemplateNode = class extends BaseNode {
38537
40207
  super(graph, id);
38538
40208
  this.prompt = prompt;
38539
40209
  }
40210
+ prompt;
38540
40211
  async build(context) {
38541
40212
  return {
38542
40213
  type: "prompt_template",
@@ -38549,6 +40220,7 @@ var LiteralNode = class extends BaseNode {
38549
40220
  super(graph, id);
38550
40221
  this.value = value;
38551
40222
  }
40223
+ value;
38552
40224
  async build(context) {
38553
40225
  return {
38554
40226
  type: "literal",
@@ -39642,6 +41314,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
39642
41314
  this._state = _state;
39643
41315
  this.spanTypeFilter = spanTypeFilter;
39644
41316
  }
41317
+ _objectId;
41318
+ rootSpanId;
41319
+ _state;
41320
+ spanTypeFilter;
39645
41321
  static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
39646
41322
  const children = [
39647
41323
  // Base filter: root_span_id = 'value'
@@ -40092,6 +41768,8 @@ var EvalResultWithSummary = class {
40092
41768
  this.summary = summary;
40093
41769
  this.results = results;
40094
41770
  }
41771
+ summary;
41772
+ results;
40095
41773
  /**
40096
41774
  * @deprecated Use `summary` instead.
40097
41775
  */
@@ -40809,26 +42487,30 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40809
42487
  }
40810
42488
  }
40811
42489
  })();
42490
+ let timeoutId;
42491
+ let abortHandler;
42492
+ const cleanupCancellation = () => {
42493
+ if (timeoutId !== void 0) {
42494
+ clearTimeout(timeoutId);
42495
+ timeoutId = void 0;
42496
+ }
42497
+ if (abortHandler && evaluator.signal) {
42498
+ evaluator.signal.removeEventListener("abort", abortHandler);
42499
+ abortHandler = void 0;
42500
+ }
42501
+ };
40812
42502
  const cancel = async () => {
40813
42503
  await new Promise((_, reject2) => {
40814
42504
  if (cancelled) {
40815
42505
  reject2(new InternalAbortError("Evaluator already cancelled"));
40816
42506
  return;
40817
42507
  }
40818
- let timeoutId;
40819
- let abortHandler;
40820
42508
  const rejectOnce = (error) => {
40821
42509
  if (cancelled) {
40822
42510
  return;
40823
42511
  }
40824
42512
  cancelled = true;
40825
- if (timeoutId) {
40826
- clearTimeout(timeoutId);
40827
- timeoutId = void 0;
40828
- }
40829
- if (abortHandler && evaluator.signal) {
40830
- evaluator.signal.removeEventListener("abort", abortHandler);
40831
- }
42513
+ cleanupCancellation();
40832
42514
  reject2(error);
40833
42515
  };
40834
42516
  if (evaluator.timeout) {
@@ -40868,6 +42550,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40868
42550
  }
40869
42551
  throw e;
40870
42552
  } finally {
42553
+ cleanupCancellation();
40871
42554
  if (!collectResults) {
40872
42555
  collectedResults.length = 0;
40873
42556
  }
@@ -41510,6 +43193,7 @@ var ToolBuilder = class {
41510
43193
  constructor(project) {
41511
43194
  this.project = project;
41512
43195
  }
43196
+ project;
41513
43197
  taskCounter = 0;
41514
43198
  // This type definition is just a catch all so that the implementation can be
41515
43199
  // less specific than the two more specific declarations above.
@@ -41540,6 +43224,7 @@ var ScorerBuilder = class {
41540
43224
  constructor(project) {
41541
43225
  this.project = project;
41542
43226
  }
43227
+ project;
41543
43228
  taskCounter = 0;
41544
43229
  create(opts) {
41545
43230
  this.taskCounter++;
@@ -41599,6 +43284,7 @@ var ClassifierBuilder = class {
41599
43284
  constructor(project) {
41600
43285
  this.project = project;
41601
43286
  }
43287
+ project;
41602
43288
  taskCounter = 0;
41603
43289
  create(opts) {
41604
43290
  this.taskCounter++;
@@ -41634,6 +43320,7 @@ var CodeFunction = class {
41634
43320
  throw new Error("parameters are required if return type is defined");
41635
43321
  }
41636
43322
  }
43323
+ project;
41637
43324
  handler;
41638
43325
  name;
41639
43326
  slug;
@@ -41721,6 +43408,7 @@ var PromptBuilder = class {
41721
43408
  constructor(project) {
41722
43409
  this.project = project;
41723
43410
  }
43411
+ project;
41724
43412
  create(opts) {
41725
43413
  const toolFunctions = [];
41726
43414
  const rawTools = [];
@@ -41797,6 +43485,7 @@ var ParametersBuilder = class {
41797
43485
  constructor(project) {
41798
43486
  this.project = project;
41799
43487
  }
43488
+ project;
41800
43489
  create(opts) {
41801
43490
  const slug = opts.slug ?? slugify(opts.name, { lower: true, strict: true });
41802
43491
  const codeParameters = new CodeParameters(this.project, {