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
@@ -219,6 +219,7 @@ var DefaultChannel = class {
219
219
  constructor(name) {
220
220
  this.name = name;
221
221
  }
222
+ name;
222
223
  hasSubscribers = false;
223
224
  subscribe(_subscription) {
224
225
  }
@@ -1275,6 +1276,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
1275
1276
  constructor(data) {
1276
1277
  this.data = data;
1277
1278
  }
1279
+ data;
1278
1280
  toStr() {
1279
1281
  const jsonObj = {
1280
1282
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -1782,6 +1784,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
1782
1784
  constructor(data) {
1783
1785
  this.data = data;
1784
1786
  }
1787
+ data;
1785
1788
  toStr() {
1786
1789
  const jsonObj = {
1787
1790
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -5015,6 +5018,144 @@ var SpanCache = class {
5015
5018
  }
5016
5019
  };
5017
5020
 
5021
+ // src/span-origin.ts
5022
+ var INSTRUMENTATION_NAMES = {
5023
+ AI_SDK: "ai-sdk",
5024
+ ANTHROPIC: "anthropic",
5025
+ BEDROCK_RUNTIME: "bedrock-runtime",
5026
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
5027
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
5028
+ COHERE: "cohere",
5029
+ CURSOR_SDK: "cursor-sdk",
5030
+ EVE: "eve",
5031
+ FLUE: "flue",
5032
+ GENKIT: "genkit",
5033
+ GITHUB_COPILOT: "github-copilot",
5034
+ GOOGLE_ADK: "google-adk",
5035
+ GOOGLE_GENAI: "google-genai",
5036
+ GROQ: "groq",
5037
+ HUGGINGFACE: "huggingface",
5038
+ LANGCHAIN: "langchain",
5039
+ LANGSMITH: "langsmith",
5040
+ MASTRA: "mastra",
5041
+ MISTRAL: "mistral",
5042
+ OPENAI: "openai",
5043
+ OPENAI_AGENTS: "openai-agents",
5044
+ OPENAI_CODEX: "openai-codex",
5045
+ OPENROUTER: "openrouter",
5046
+ OPENROUTER_AGENT: "openrouter-agent",
5047
+ PI_CODING_AGENT: "pi-coding-agent",
5048
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
5049
+ };
5050
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
5051
+ "braintrust.spanInstrumentationName"
5052
+ );
5053
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
5054
+ function withSpanInstrumentationName(args, instrumentationName) {
5055
+ return {
5056
+ ...args,
5057
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
5058
+ };
5059
+ }
5060
+ function getSpanInstrumentationName(args) {
5061
+ if (typeof args !== "object" || args === null) {
5062
+ return void 0;
5063
+ }
5064
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
5065
+ return isSpanInstrumentationName(value) ? value : void 0;
5066
+ }
5067
+ function detectSpanOriginEnvironment(explicit) {
5068
+ if (explicit) return explicit;
5069
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
5070
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
5071
+ if (envType || envName) {
5072
+ return {
5073
+ ...envType ? { type: envType } : {},
5074
+ ...envName ? { name: envName } : {}
5075
+ };
5076
+ }
5077
+ const ci = firstPresent([
5078
+ ["GITHUB_ACTIONS", "github_actions"],
5079
+ ["GITLAB_CI", "gitlab_ci"],
5080
+ ["CIRCLECI", "circleci"],
5081
+ ["BUILDKITE", "buildkite"],
5082
+ ["JENKINS_URL", "jenkins"],
5083
+ ["JENKINS_HOME", "jenkins"],
5084
+ ["TF_BUILD", "azure_pipelines"],
5085
+ ["TEAMCITY_VERSION", "teamcity"],
5086
+ ["TRAVIS", "travis"],
5087
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
5088
+ ]);
5089
+ if (ci) return { type: "ci", name: ci };
5090
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
5091
+ const earlyServer = firstPresent([
5092
+ ["VERCEL", "vercel"],
5093
+ ["NETLIFY", "netlify"]
5094
+ ]);
5095
+ if (earlyServer) return { type: "server", name: earlyServer };
5096
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
5097
+ return { type: "server", name: "ecs" };
5098
+ }
5099
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
5100
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
5101
+ return { type: "server", name: "ecs" };
5102
+ }
5103
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
5104
+ return { type: "server", name: "aws_lambda" };
5105
+ }
5106
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
5107
+ return { type: "server", name: "aws_lambda" };
5108
+ }
5109
+ const server = firstPresent([
5110
+ ["K_SERVICE", "cloud_run"],
5111
+ ["FUNCTION_TARGET", "gcp_functions"],
5112
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
5113
+ ["DYNO", "heroku"],
5114
+ ["FLY_APP_NAME", "fly"],
5115
+ ["RAILWAY_ENVIRONMENT", "railway"],
5116
+ ["RENDER_SERVICE_NAME", "render"]
5117
+ ]);
5118
+ if (server) return { type: "server", name: server };
5119
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
5120
+ }
5121
+ function makeSpanOrigin(instrumentationName, environment) {
5122
+ return {
5123
+ name: "braintrust.sdk.javascript",
5124
+ version: SDK_VERSION,
5125
+ instrumentation: { name: instrumentationName },
5126
+ ...environment ? { environment } : {}
5127
+ };
5128
+ }
5129
+ function mergeSpanOriginContext(context, instrumentationName, environment) {
5130
+ const next = { ...context ?? {} };
5131
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
5132
+ next.span_origin = {
5133
+ ...makeSpanOrigin(instrumentationName, environment),
5134
+ ...current
5135
+ };
5136
+ return next;
5137
+ }
5138
+ function isSpanInstrumentationName(value) {
5139
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
5140
+ }
5141
+ function firstPresent(entries) {
5142
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
5143
+ }
5144
+ function deploymentModeEnvironment(_key, value) {
5145
+ if (!value) return void 0;
5146
+ const normalized = value.toLowerCase();
5147
+ if (normalized === "production" || normalized === "staging") {
5148
+ return { type: "server", name: normalized };
5149
+ }
5150
+ if (normalized === "development" || normalized === "local") {
5151
+ return { type: "local", name: normalized };
5152
+ }
5153
+ return { name: value };
5154
+ }
5155
+ function isObject2(value) {
5156
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5157
+ }
5158
+
5018
5159
  // src/logger.ts
5019
5160
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
5020
5161
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -5063,6 +5204,7 @@ var LoginInvalidOrgError = class extends Error {
5063
5204
  super(message);
5064
5205
  this.message = message;
5065
5206
  }
5207
+ message;
5066
5208
  };
5067
5209
  var REDACTION_FIELDS = [
5068
5210
  "input",
@@ -5078,6 +5220,8 @@ var MaskingError = class {
5078
5220
  this.fieldName = fieldName;
5079
5221
  this.errorType = errorType;
5080
5222
  }
5223
+ fieldName;
5224
+ errorType;
5081
5225
  get errorMsg() {
5082
5226
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
5083
5227
  }
@@ -5287,7 +5431,10 @@ var BraintrustState = class _BraintrustState {
5287
5431
  diskCache: parametersDiskCache
5288
5432
  });
5289
5433
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
5434
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
5435
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5290
5436
  }
5437
+ loginParams;
5291
5438
  id;
5292
5439
  currentExperiment;
5293
5440
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -5322,6 +5469,19 @@ var BraintrustState = class _BraintrustState {
5322
5469
  _idGenerator = null;
5323
5470
  _contextManager = null;
5324
5471
  _otelFlushCallback = null;
5472
+ spanOriginEnvironment;
5473
+ traceContextSigningSecret;
5474
+ /** @internal */
5475
+ _internalSetTraceContextSigningSecret(secret) {
5476
+ const normalizedSecret = secret?.trim();
5477
+ if (normalizedSecret) {
5478
+ this.traceContextSigningSecret = normalizedSecret;
5479
+ }
5480
+ }
5481
+ /** @internal */
5482
+ _internalGetTraceContextSigningSecret() {
5483
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
5484
+ }
5325
5485
  resetLoginInfo() {
5326
5486
  this.appUrl = null;
5327
5487
  this.appPublicUrl = null;
@@ -5382,6 +5542,7 @@ var BraintrustState = class _BraintrustState {
5382
5542
  this.gitMetadataSettings = other.gitMetadataSettings;
5383
5543
  this.debugLogLevel = other.debugLogLevel;
5384
5544
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
5545
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
5385
5546
  setGlobalDebugLogLevel(
5386
5547
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
5387
5548
  );
@@ -5469,6 +5630,7 @@ var BraintrustState = class _BraintrustState {
5469
5630
  return this.debugLogLevelConfigured;
5470
5631
  }
5471
5632
  async login(loginParams) {
5633
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5472
5634
  this.setDebugLogLevel(loginParams.debugLogLevel);
5473
5635
  if (this.apiUrl && !loginParams.forceLogin) {
5474
5636
  return;
@@ -7448,6 +7610,7 @@ function init(projectOrOptions, optionalOptions) {
7448
7610
  throw new Error("Cannot open and update an experiment at the same time");
7449
7611
  }
7450
7612
  const state = stateArg ?? _globalState;
7613
+ state._internalSetTraceContextSigningSecret(apiKey);
7451
7614
  state.enforceQueueSizeLimit(false);
7452
7615
  if (open) {
7453
7616
  if (isEmpty2(experiment)) {
@@ -7940,6 +8103,7 @@ function initLogger(options = {}) {
7940
8103
  orgName,
7941
8104
  forceLogin,
7942
8105
  debugLogLevel,
8106
+ environment,
7943
8107
  fetch: fetch2,
7944
8108
  state: stateArg
7945
8109
  } = options || {};
@@ -7955,7 +8119,9 @@ function initLogger(options = {}) {
7955
8119
  project_id: projectId
7956
8120
  };
7957
8121
  const state = stateArg ?? _globalState;
8122
+ state._internalSetTraceContextSigningSecret(apiKey);
7958
8123
  state.setDebugLogLevel(debugLogLevel);
8124
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
7959
8125
  state.enforceQueueSizeLimit(true);
7960
8126
  const lazyMetadata = new LazyValue(
7961
8127
  async () => {
@@ -9017,7 +9183,9 @@ function deepCopyEvent(event) {
9017
9183
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
9018
9184
  const attachmentMarker = ++deepCopyEventMarkerCounter;
9019
9185
  const serialized = JSON.stringify(event, (_k, v) => {
9020
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
9186
+ if (v instanceof Error) {
9187
+ return v.message;
9188
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
9021
9189
  return `<span>`;
9022
9190
  } else if (v instanceof Experiment2) {
9023
9191
  return `<experiment>`;
@@ -9147,6 +9315,11 @@ var ObjectFetcher = class {
9147
9315
  this._internal_btql = _internal_btql;
9148
9316
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
9149
9317
  }
9318
+ objectType;
9319
+ pinnedVersion;
9320
+ mutateRecord;
9321
+ _internal_btql;
9322
+ _internalBrainstoreRealtime;
9150
9323
  _fetchedData = void 0;
9151
9324
  get id() {
9152
9325
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -9585,6 +9758,8 @@ var ReadonlyExperiment = class extends ObjectFetcher {
9585
9758
  this.state = state;
9586
9759
  this.lazyMetadata = lazyMetadata;
9587
9760
  }
9761
+ state;
9762
+ lazyMetadata;
9588
9763
  get id() {
9589
9764
  return (async () => {
9590
9765
  return (await this.lazyMetadata.get()).experiment.id;
@@ -9678,6 +9853,7 @@ var SpanImpl = class _SpanImpl {
9678
9853
  constructor(args) {
9679
9854
  this._state = args.state;
9680
9855
  this._propagatedState = args.propagatedState;
9856
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
9681
9857
  const spanAttributes = args.spanAttributes ?? {};
9682
9858
  const rawEvent = args.event ?? {};
9683
9859
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -9707,7 +9883,11 @@ var SpanImpl = class _SpanImpl {
9707
9883
  metrics: {
9708
9884
  start: args.startTime ?? getCurrentUnixTimestamp()
9709
9885
  },
9710
- context: { ...callerLocation },
9886
+ context: mergeSpanOriginContext(
9887
+ { ...callerLocation },
9888
+ instrumentationName,
9889
+ this._state.spanOriginEnvironment
9890
+ ),
9711
9891
  span_attributes: {
9712
9892
  name,
9713
9893
  type,
@@ -10081,6 +10261,7 @@ var Dataset2 = class extends ObjectFetcher {
10081
10261
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
10082
10262
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
10083
10263
  }
10264
+ state;
10084
10265
  lazyMetadata;
10085
10266
  __braintrust_dataset_marker = true;
10086
10267
  newRecords = 0;
@@ -10595,6 +10776,9 @@ var Prompt2 = class _Prompt {
10595
10776
  this.noTrace = noTrace;
10596
10777
  void this.__braintrust_prompt_marker;
10597
10778
  }
10779
+ metadata;
10780
+ defaults;
10781
+ noTrace;
10598
10782
  parsedPromptData;
10599
10783
  hasParsedPromptData = false;
10600
10784
  __braintrust_prompt_marker = true;
@@ -10832,6 +11016,7 @@ var RemoteEvalParameters = class {
10832
11016
  this.metadata = metadata;
10833
11017
  void this.__braintrust_parameters_marker;
10834
11018
  }
11019
+ metadata;
10835
11020
  __braintrust_parameters_marker = true;
10836
11021
  get id() {
10837
11022
  return this.metadata.id;
@@ -11807,15 +11992,25 @@ function hasChoices(value) {
11807
11992
  function normalizeMetadata(metadata) {
11808
11993
  return isObject(metadata) ? metadata : void 0;
11809
11994
  }
11810
- function startSpanForEvent(config, event, channelName) {
11995
+ function startSpanForEvent(config, event, channelName, instrumentationName) {
11811
11996
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
11812
11997
  config,
11813
11998
  event
11814
11999
  );
11815
- const span = startSpan({
11816
- name,
11817
- spanAttributes
11818
- });
12000
+ const spanArgs = withSpanInstrumentationName(
12001
+ {
12002
+ name,
12003
+ spanAttributes
12004
+ },
12005
+ instrumentationName
12006
+ );
12007
+ let span;
12008
+ try {
12009
+ span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
12010
+ } catch (error) {
12011
+ debugLogger.error(`Error starting span for ${channelName}:`, error);
12012
+ span = startSpan(spanArgs);
12013
+ }
11819
12014
  const startTime = getCurrentUnixTimestamp();
11820
12015
  try {
11821
12016
  const { input, metadata } = config.extractInput(
@@ -11846,7 +12041,7 @@ function shouldTraceEvent(config, event, channelName) {
11846
12041
  return true;
11847
12042
  }
11848
12043
  }
11849
- function ensureSpanStateForEvent(states, config, event, channelName) {
12044
+ function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
11850
12045
  const key = event;
11851
12046
  const existing = states.get(key);
11852
12047
  if (existing) {
@@ -11855,11 +12050,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
11855
12050
  if (!shouldTraceEvent(config, event, channelName)) {
11856
12051
  return void 0;
11857
12052
  }
11858
- const created = startSpanForEvent(config, event, channelName);
12053
+ const created = startSpanForEvent(
12054
+ config,
12055
+ event,
12056
+ channelName,
12057
+ instrumentationName
12058
+ );
11859
12059
  states.set(key, created);
11860
12060
  return created;
11861
12061
  }
11862
- function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
12062
+ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName, instrumentationName) {
11863
12063
  const state = _internalGetGlobalState();
11864
12064
  const startChannel = tracingChannel2.start;
11865
12065
  const contextManager = state?.contextManager;
@@ -11877,7 +12077,8 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11877
12077
  states,
11878
12078
  config,
11879
12079
  event,
11880
- channelName
12080
+ channelName,
12081
+ instrumentationName
11881
12082
  );
11882
12083
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
11883
12084
  }
@@ -11886,15 +12087,21 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11886
12087
  startChannel.unbindStore(currentSpanStore);
11887
12088
  };
11888
12089
  }
11889
- function logErrorAndEnd(states, event) {
12090
+ function logErrorAndEnd(states, event, channelName) {
11890
12091
  const spanData = states.get(event);
11891
12092
  if (!spanData) {
11892
12093
  return;
11893
12094
  }
11894
- spanData.span.log({
11895
- error: event.error.message
11896
- });
11897
- spanData.span.end();
12095
+ try {
12096
+ spanData.span.log({ error: event.error });
12097
+ } catch (error) {
12098
+ debugLogger.error(`Error logging failure for ${channelName}:`, error);
12099
+ }
12100
+ try {
12101
+ spanData.span.end();
12102
+ } catch (error) {
12103
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
12104
+ }
11898
12105
  states.delete(event);
11899
12106
  }
11900
12107
  function runStreamingCompletionHook(args) {
@@ -11944,7 +12151,8 @@ function traceAsyncChannel(channel2, config) {
11944
12151
  tracingChannel2,
11945
12152
  states,
11946
12153
  config,
11947
- channelName
12154
+ channelName,
12155
+ channel2.instrumentationName
11948
12156
  );
11949
12157
  const handlers = {
11950
12158
  start: (event) => {
@@ -11955,7 +12163,8 @@ function traceAsyncChannel(channel2, config) {
11955
12163
  states,
11956
12164
  config,
11957
12165
  event,
11958
- channelName
12166
+ channelName,
12167
+ channel2.instrumentationName
11959
12168
  );
11960
12169
  },
11961
12170
  asyncEnd: (event) => {
@@ -11992,7 +12201,7 @@ function traceAsyncChannel(channel2, config) {
11992
12201
  }
11993
12202
  },
11994
12203
  error: (event) => {
11995
- logErrorAndEnd(states, event);
12204
+ logErrorAndEnd(states, event, channelName);
11996
12205
  }
11997
12206
  };
11998
12207
  tracingChannel2.subscribe(handlers);
@@ -12009,7 +12218,8 @@ function traceStreamingChannel(channel2, config) {
12009
12218
  tracingChannel2,
12010
12219
  states,
12011
12220
  config,
12012
- channelName
12221
+ channelName,
12222
+ channel2.instrumentationName
12013
12223
  );
12014
12224
  const handlers = {
12015
12225
  start: (event) => {
@@ -12020,7 +12230,8 @@ function traceStreamingChannel(channel2, config) {
12020
12230
  states,
12021
12231
  config,
12022
12232
  event,
12023
- channelName
12233
+ channelName,
12234
+ channel2.instrumentationName
12024
12235
  );
12025
12236
  },
12026
12237
  asyncEnd: (event) => {
@@ -12039,6 +12250,7 @@ function traceStreamingChannel(channel2, config) {
12039
12250
  }
12040
12251
  },
12041
12252
  onComplete: (chunks) => {
12253
+ let completion2;
12042
12254
  try {
12043
12255
  let output;
12044
12256
  let metrics;
@@ -12069,18 +12281,11 @@ function traceStreamingChannel(channel2, config) {
12069
12281
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
12070
12282
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
12071
12283
  }
12072
- runStreamingCompletionHook({
12073
- channelName,
12074
- chunks,
12075
- config,
12076
- endEvent: asyncEndEvent,
12284
+ completion2 = {
12077
12285
  ...metadata !== void 0 ? { metadata } : {},
12078
12286
  metrics,
12079
- output,
12080
- result: asyncEndEvent.result,
12081
- span,
12082
- startTime
12083
- });
12287
+ output
12288
+ };
12084
12289
  span.log({
12085
12290
  output,
12086
12291
  ...metadata !== void 0 ? { metadata } : {},
@@ -12092,11 +12297,49 @@ function traceStreamingChannel(channel2, config) {
12092
12297
  error
12093
12298
  );
12094
12299
  } finally {
12095
- span.end();
12300
+ try {
12301
+ span.end();
12302
+ } catch (error) {
12303
+ debugLogger.error(
12304
+ `Error ending span for ${channelName}:`,
12305
+ error
12306
+ );
12307
+ }
12096
12308
  states.delete(event);
12097
12309
  }
12310
+ if (completion2) {
12311
+ runStreamingCompletionHook({
12312
+ channelName,
12313
+ chunks,
12314
+ config,
12315
+ endEvent: asyncEndEvent,
12316
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
12317
+ metrics: completion2.metrics,
12318
+ output: completion2.output,
12319
+ result: asyncEndEvent.result,
12320
+ span,
12321
+ startTime
12322
+ });
12323
+ }
12098
12324
  },
12099
12325
  onError: (error) => {
12326
+ try {
12327
+ span.log({ error });
12328
+ } catch (loggingError) {
12329
+ debugLogger.error(
12330
+ `Error logging failure for ${channelName}:`,
12331
+ loggingError
12332
+ );
12333
+ }
12334
+ try {
12335
+ span.end();
12336
+ } catch (endingError) {
12337
+ debugLogger.error(
12338
+ `Error ending span for ${channelName}:`,
12339
+ endingError
12340
+ );
12341
+ }
12342
+ states.delete(event);
12100
12343
  runStreamingErrorHook({
12101
12344
  channelName,
12102
12345
  config,
@@ -12105,11 +12348,6 @@ function traceStreamingChannel(channel2, config) {
12105
12348
  span,
12106
12349
  startTime
12107
12350
  });
12108
- span.log({
12109
- error: error.message
12110
- });
12111
- span.end();
12112
- states.delete(event);
12113
12351
  }
12114
12352
  });
12115
12353
  return;
@@ -12124,6 +12362,7 @@ function traceStreamingChannel(channel2, config) {
12124
12362
  states.delete(event);
12125
12363
  return;
12126
12364
  }
12365
+ let completion;
12127
12366
  try {
12128
12367
  const output = config.extractOutput(
12129
12368
  asyncEndEvent.result,
@@ -12138,17 +12377,11 @@ function traceStreamingChannel(channel2, config) {
12138
12377
  asyncEndEvent.result,
12139
12378
  asyncEndEvent
12140
12379
  );
12141
- runStreamingCompletionHook({
12142
- channelName,
12143
- config,
12144
- endEvent: asyncEndEvent,
12380
+ completion = {
12145
12381
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
12146
12382
  metrics,
12147
- output,
12148
- result: asyncEndEvent.result,
12149
- span,
12150
- startTime
12151
- });
12383
+ output
12384
+ };
12152
12385
  span.log({
12153
12386
  output,
12154
12387
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -12157,12 +12390,30 @@ function traceStreamingChannel(channel2, config) {
12157
12390
  } catch (error) {
12158
12391
  debugLogger.error(`Error extracting output for ${channelName}:`, error);
12159
12392
  } finally {
12160
- span.end();
12393
+ try {
12394
+ span.end();
12395
+ } catch (error) {
12396
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
12397
+ }
12161
12398
  states.delete(event);
12162
12399
  }
12400
+ if (completion) {
12401
+ runStreamingCompletionHook({
12402
+ channelName,
12403
+ config,
12404
+ endEvent: asyncEndEvent,
12405
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
12406
+ metrics: completion.metrics,
12407
+ output: completion.output,
12408
+ result: asyncEndEvent.result,
12409
+ span,
12410
+ startTime
12411
+ });
12412
+ }
12163
12413
  },
12164
12414
  error: (event) => {
12165
12415
  const spanData = states.get(event);
12416
+ logErrorAndEnd(states, event, channelName);
12166
12417
  if (spanData) {
12167
12418
  runStreamingErrorHook({
12168
12419
  channelName,
@@ -12173,7 +12424,6 @@ function traceStreamingChannel(channel2, config) {
12173
12424
  startTime: spanData.startTime
12174
12425
  });
12175
12426
  }
12176
- logErrorAndEnd(states, event);
12177
12427
  }
12178
12428
  };
12179
12429
  tracingChannel2.subscribe(handlers);
@@ -12190,7 +12440,8 @@ function traceSyncStreamChannel(channel2, config) {
12190
12440
  tracingChannel2,
12191
12441
  states,
12192
12442
  config,
12193
- channelName
12443
+ channelName,
12444
+ channel2.instrumentationName
12194
12445
  );
12195
12446
  const handlers = {
12196
12447
  start: (event) => {
@@ -12201,7 +12452,8 @@ function traceSyncStreamChannel(channel2, config) {
12201
12452
  states,
12202
12453
  config,
12203
12454
  event,
12204
- channelName
12455
+ channelName,
12456
+ channel2.instrumentationName
12205
12457
  );
12206
12458
  },
12207
12459
  end: (event) => {
@@ -12295,7 +12547,7 @@ function traceSyncStreamChannel(channel2, config) {
12295
12547
  handleResolvedResult(endEvent.result);
12296
12548
  },
12297
12549
  error: (event) => {
12298
- logErrorAndEnd(states, event);
12550
+ logErrorAndEnd(states, event, channelName);
12299
12551
  }
12300
12552
  };
12301
12553
  tracingChannel2.subscribe(handlers);
@@ -12474,7 +12726,8 @@ function processInputAttachments(input) {
12474
12726
  function channel(spec) {
12475
12727
  return spec;
12476
12728
  }
12477
- function defineChannels(pkg, channels) {
12729
+ function defineChannels(pkg, channels, options) {
12730
+ const { instrumentationName } = options;
12478
12731
  return Object.fromEntries(
12479
12732
  Object.entries(channels).map(([key, spec]) => {
12480
12733
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -12487,6 +12740,7 @@ function defineChannels(pkg, channels) {
12487
12740
  key,
12488
12741
  {
12489
12742
  ...asyncSpec,
12743
+ instrumentationName,
12490
12744
  tracingChannel: tracingChannel3,
12491
12745
  tracePromise: (fn, context) => tracingChannel3().tracePromise(
12492
12746
  fn,
@@ -12504,6 +12758,7 @@ function defineChannels(pkg, channels) {
12504
12758
  key,
12505
12759
  {
12506
12760
  ...syncSpec,
12761
+ instrumentationName,
12507
12762
  tracingChannel: tracingChannel2,
12508
12763
  traceSync: (fn, context) => tracingChannel2().traceSync(
12509
12764
  fn,
@@ -12517,44 +12772,48 @@ function defineChannels(pkg, channels) {
12517
12772
  }
12518
12773
 
12519
12774
  // src/instrumentation/plugins/openai-channels.ts
12520
- var openAIChannels = defineChannels("openai", {
12521
- chatCompletionsCreate: channel({
12522
- channelName: "chat.completions.create",
12523
- kind: "async"
12524
- }),
12525
- embeddingsCreate: channel({
12526
- channelName: "embeddings.create",
12527
- kind: "async"
12528
- }),
12529
- betaChatCompletionsParse: channel({
12530
- channelName: "beta.chat.completions.parse",
12531
- kind: "async"
12532
- }),
12533
- betaChatCompletionsStream: channel({
12534
- channelName: "beta.chat.completions.stream",
12535
- kind: "sync-stream"
12536
- }),
12537
- moderationsCreate: channel({
12538
- channelName: "moderations.create",
12539
- kind: "async"
12540
- }),
12541
- responsesCreate: channel({
12542
- channelName: "responses.create",
12543
- kind: "async"
12544
- }),
12545
- responsesStream: channel({
12546
- channelName: "responses.stream",
12547
- kind: "sync-stream"
12548
- }),
12549
- responsesParse: channel({
12550
- channelName: "responses.parse",
12551
- kind: "async"
12552
- }),
12553
- responsesCompact: channel({
12554
- channelName: "responses.compact",
12555
- kind: "async"
12556
- })
12557
- });
12775
+ var openAIChannels = defineChannels(
12776
+ "openai",
12777
+ {
12778
+ chatCompletionsCreate: channel({
12779
+ channelName: "chat.completions.create",
12780
+ kind: "async"
12781
+ }),
12782
+ embeddingsCreate: channel({
12783
+ channelName: "embeddings.create",
12784
+ kind: "async"
12785
+ }),
12786
+ betaChatCompletionsParse: channel({
12787
+ channelName: "beta.chat.completions.parse",
12788
+ kind: "async"
12789
+ }),
12790
+ betaChatCompletionsStream: channel({
12791
+ channelName: "beta.chat.completions.stream",
12792
+ kind: "sync-stream"
12793
+ }),
12794
+ moderationsCreate: channel({
12795
+ channelName: "moderations.create",
12796
+ kind: "async"
12797
+ }),
12798
+ responsesCreate: channel({
12799
+ channelName: "responses.create",
12800
+ kind: "async"
12801
+ }),
12802
+ responsesStream: channel({
12803
+ channelName: "responses.stream",
12804
+ kind: "sync-stream"
12805
+ }),
12806
+ responsesParse: channel({
12807
+ channelName: "responses.parse",
12808
+ kind: "async"
12809
+ }),
12810
+ responsesCompact: channel({
12811
+ channelName: "responses.compact",
12812
+ kind: "async"
12813
+ })
12814
+ },
12815
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
12816
+ );
12558
12817
 
12559
12818
  // src/openai-utils.ts
12560
12819
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -13069,16 +13328,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
13069
13328
  }
13070
13329
 
13071
13330
  // src/instrumentation/plugins/openai-codex-channels.ts
13072
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
13073
- run: channel({
13074
- channelName: "Thread.run",
13075
- kind: "async"
13076
- }),
13077
- runStreamed: channel({
13078
- channelName: "Thread.runStreamed",
13079
- kind: "async"
13080
- })
13081
- });
13331
+ var openAICodexChannels = defineChannels(
13332
+ "@openai/codex-sdk",
13333
+ {
13334
+ run: channel({
13335
+ channelName: "Thread.run",
13336
+ kind: "async"
13337
+ }),
13338
+ runStreamed: channel({
13339
+ channelName: "Thread.runStreamed",
13340
+ kind: "async"
13341
+ })
13342
+ },
13343
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
13344
+ );
13082
13345
 
13083
13346
  // src/instrumentation/plugins/openai-codex-plugin.ts
13084
13347
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -13166,10 +13429,15 @@ function startCodexRun(event, operation) {
13166
13429
  provider: "openai",
13167
13430
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
13168
13431
  };
13169
- const span = startSpan({
13170
- name: "OpenAI Codex",
13171
- spanAttributes: { type: "task" /* TASK */ }
13172
- });
13432
+ const span = startSpan(
13433
+ withSpanInstrumentationName(
13434
+ {
13435
+ name: "OpenAI Codex",
13436
+ spanAttributes: { type: "task" /* TASK */ }
13437
+ },
13438
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13439
+ )
13440
+ );
13173
13441
  const startTime = getCurrentUnixTimestamp();
13174
13442
  safeLog(span, {
13175
13443
  input: sanitizedInput,
@@ -13318,7 +13586,12 @@ async function createCompletedItemSpan(state, item) {
13318
13586
  if (!spanArgs) {
13319
13587
  return;
13320
13588
  }
13321
- const span = startSpan(spanArgs.start);
13589
+ const span = startSpan(
13590
+ withSpanInstrumentationName(
13591
+ spanArgs.start,
13592
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13593
+ )
13594
+ );
13322
13595
  safeLog(span, spanArgs.end);
13323
13596
  span.end();
13324
13597
  }
@@ -13360,15 +13633,20 @@ async function ensureActiveLlmSpan(state) {
13360
13633
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
13361
13634
  "openai_codex.llm_sequence": sequence
13362
13635
  };
13363
- const span = startSpan({
13364
- event: {
13365
- ...sequence === 1 ? { input: state.input } : {},
13366
- metadata
13367
- },
13368
- name: "OpenAI Codex LLM",
13369
- parent: await state.span.export(),
13370
- spanAttributes: { type: "llm" /* LLM */ }
13371
- });
13636
+ const span = startSpan(
13637
+ withSpanInstrumentationName(
13638
+ {
13639
+ event: {
13640
+ ...sequence === 1 ? { input: state.input } : {},
13641
+ metadata
13642
+ },
13643
+ name: "OpenAI Codex LLM",
13644
+ parent: await state.span.export(),
13645
+ spanAttributes: { type: "llm" /* LLM */ }
13646
+ },
13647
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13648
+ )
13649
+ );
13372
13650
  state.activeLlmSpan = {
13373
13651
  anonymousMessages: [],
13374
13652
  anonymousReasoning: [],
@@ -13414,7 +13692,15 @@ async function startCodexItemSpan(state, item) {
13414
13692
  if (!spanArgs) {
13415
13693
  return;
13416
13694
  }
13417
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
13695
+ state.activeItemSpans.set(
13696
+ itemId,
13697
+ startSpan(
13698
+ withSpanInstrumentationName(
13699
+ spanArgs.start,
13700
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13701
+ )
13702
+ )
13703
+ );
13418
13704
  }
13419
13705
  function updateCodexItem(state, item) {
13420
13706
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -13724,20 +14010,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
13724
14010
  }
13725
14011
 
13726
14012
  // src/instrumentation/plugins/anthropic-channels.ts
13727
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
13728
- messagesCreate: channel({
13729
- channelName: "messages.create",
13730
- kind: "async"
13731
- }),
13732
- betaMessagesCreate: channel({
13733
- channelName: "beta.messages.create",
13734
- kind: "async"
13735
- }),
13736
- betaMessagesToolRunner: channel({
13737
- channelName: "beta.messages.toolRunner",
13738
- kind: "sync-stream"
13739
- })
13740
- });
14013
+ var anthropicChannels = defineChannels(
14014
+ "@anthropic-ai/sdk",
14015
+ {
14016
+ messagesCreate: channel({
14017
+ channelName: "messages.create",
14018
+ kind: "async"
14019
+ }),
14020
+ betaMessagesCreate: channel({
14021
+ channelName: "beta.messages.create",
14022
+ kind: "async"
14023
+ }),
14024
+ betaMessagesToolRunner: channel({
14025
+ channelName: "beta.messages.toolRunner",
14026
+ kind: "sync-stream"
14027
+ })
14028
+ },
14029
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
14030
+ );
13741
14031
 
13742
14032
  // src/instrumentation/plugins/anthropic-plugin.ts
13743
14033
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -13810,12 +14100,17 @@ var AnthropicPlugin = class extends BasePlugin {
13810
14100
  return;
13811
14101
  }
13812
14102
  const params = event.arguments[0] ?? {};
13813
- const span = startSpan({
13814
- name: "anthropic.beta.messages.toolRunner",
13815
- spanAttributes: {
13816
- type: "task" /* TASK */
13817
- }
13818
- });
14103
+ const span = startSpan(
14104
+ withSpanInstrumentationName(
14105
+ {
14106
+ name: "anthropic.beta.messages.toolRunner",
14107
+ spanAttributes: {
14108
+ type: "task" /* TASK */
14109
+ }
14110
+ },
14111
+ INSTRUMENTATION_NAMES.ANTHROPIC
14112
+ )
14113
+ );
13819
14114
  span.log({
13820
14115
  input: processAttachmentsInInput(
13821
14116
  coalesceInput(params.messages ?? [], params.system)
@@ -13956,19 +14251,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
13956
14251
  return finalizeError(error);
13957
14252
  }
13958
14253
  },
13959
- {
13960
- event: {
13961
- input: getAnthropicToolRunnerInput(args),
13962
- metadata: {
13963
- "gen_ai.tool.name": toolName,
13964
- provider: "anthropic"
14254
+ withSpanInstrumentationName(
14255
+ {
14256
+ event: {
14257
+ input: getAnthropicToolRunnerInput(args),
14258
+ metadata: {
14259
+ "gen_ai.tool.name": toolName,
14260
+ provider: "anthropic"
14261
+ }
14262
+ },
14263
+ name: `tool: ${toolName}`,
14264
+ spanAttributes: {
14265
+ type: "tool" /* TOOL */
13965
14266
  }
13966
14267
  },
13967
- name: `tool: ${toolName}`,
13968
- spanAttributes: {
13969
- type: "tool" /* TOOL */
13970
- }
13971
- }
14268
+ INSTRUMENTATION_NAMES.ANTHROPIC
14269
+ )
13972
14270
  );
13973
14271
  },
13974
14272
  writable: runDescriptor?.writable ?? true
@@ -14563,6 +14861,267 @@ function currentWorkflowAgentWrapperSpan() {
14563
14861
  return void 0;
14564
14862
  }
14565
14863
 
14864
+ // src/wrappers/ai-sdk/harness-agent-context.ts
14865
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
14866
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
14867
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
14868
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
14869
+ var harnessTurnParentStore;
14870
+ function serializedTurnContext(value) {
14871
+ if (!isObject(value)) {
14872
+ return void 0;
14873
+ }
14874
+ const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
14875
+ if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
14876
+ return void 0;
14877
+ }
14878
+ const expectedSignature = signTurnContext({
14879
+ parent: context.parent,
14880
+ sessionId: context.sessionId
14881
+ });
14882
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
14883
+ return void 0;
14884
+ }
14885
+ const parent = SpanComponentsV4.fromStr(context.parent);
14886
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
14887
+ return void 0;
14888
+ }
14889
+ return {
14890
+ parent: context.parent,
14891
+ sessionId: context.sessionId
14892
+ };
14893
+ }
14894
+ function signTurnContext(context) {
14895
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
14896
+ return secret ? isomorph_default.hmacSha256?.(
14897
+ secret,
14898
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
14899
+ ) : void 0;
14900
+ }
14901
+ function continuationParentFromCreateSessionParams(params) {
14902
+ if (!isObject(params)) {
14903
+ return void 0;
14904
+ }
14905
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
14906
+ const context = serializedTurnContext(continuation);
14907
+ if (!context) {
14908
+ return void 0;
14909
+ }
14910
+ if (params.sessionId !== context.sessionId) {
14911
+ return void 0;
14912
+ }
14913
+ return context.parent;
14914
+ }
14915
+ function exportSpanSynchronously(span) {
14916
+ const parentInfo = span.getParentInfo();
14917
+ if (!parentInfo) {
14918
+ return void 0;
14919
+ }
14920
+ const objectId = parentInfo.objectId.getSync().value;
14921
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
14922
+ return void 0;
14923
+ }
14924
+ return new SpanComponentsV4({
14925
+ object_type: parentInfo.objectType,
14926
+ ...objectId ? { object_id: objectId } : {
14927
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
14928
+ },
14929
+ row_id: span.id,
14930
+ root_span_id: span.rootSpanId,
14931
+ span_id: span.spanId
14932
+ }).toStr();
14933
+ }
14934
+ function exportedParent(parent) {
14935
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
14936
+ }
14937
+ function addSerializedContext(args) {
14938
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
14939
+ return;
14940
+ }
14941
+ const parent = exportedParent(args.parent);
14942
+ if (!parent) {
14943
+ return;
14944
+ }
14945
+ const context = {
14946
+ parent,
14947
+ sessionId: args.sessionId
14948
+ };
14949
+ const signature = signTurnContext(context);
14950
+ if (!signature) {
14951
+ return;
14952
+ }
14953
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
14954
+ configurable: true,
14955
+ enumerable: true,
14956
+ value: {
14957
+ ...context,
14958
+ signature
14959
+ },
14960
+ writable: true
14961
+ });
14962
+ }
14963
+ function lifecycleMethodDescriptor(session, method) {
14964
+ let owner = session;
14965
+ while (owner) {
14966
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
14967
+ if (descriptor) {
14968
+ return { descriptor, owner };
14969
+ }
14970
+ owner = Object.getPrototypeOf(owner);
14971
+ }
14972
+ return void 0;
14973
+ }
14974
+ function patchLifecycleMethod(session, method) {
14975
+ let resolvedDescriptor;
14976
+ try {
14977
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
14978
+ } catch {
14979
+ return;
14980
+ }
14981
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
14982
+ return;
14983
+ }
14984
+ const { descriptor, owner } = resolvedDescriptor;
14985
+ const patched = patchedLifecycleMethods.get(owner);
14986
+ if (patched?.has(method)) {
14987
+ return;
14988
+ }
14989
+ const original = descriptor.value;
14990
+ const replacement = function(...args) {
14991
+ const result = Reflect.apply(original, this, args);
14992
+ const addContext = (state) => {
14993
+ try {
14994
+ const parent = sessionTurnParents.get(this);
14995
+ if (!parent) {
14996
+ return;
14997
+ }
14998
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
14999
+ addSerializedContext({
15000
+ continuation,
15001
+ parent,
15002
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
15003
+ });
15004
+ } catch {
15005
+ }
15006
+ };
15007
+ try {
15008
+ if (isObject(result) && typeof result.then === "function") {
15009
+ void Promise.resolve(result).then(addContext, () => {
15010
+ });
15011
+ } else {
15012
+ addContext(result);
15013
+ }
15014
+ } catch {
15015
+ }
15016
+ return result;
15017
+ };
15018
+ try {
15019
+ Object.defineProperty(owner, method, {
15020
+ ...descriptor,
15021
+ value: replacement
15022
+ });
15023
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
15024
+ ownerMethods.add(method);
15025
+ if (!patched) {
15026
+ patchedLifecycleMethods.set(owner, ownerMethods);
15027
+ }
15028
+ } catch {
15029
+ }
15030
+ }
15031
+ function patchLifecycleMethods(session) {
15032
+ patchLifecycleMethod(session, "suspendTurn");
15033
+ patchLifecycleMethod(session, "detach");
15034
+ patchLifecycleMethod(session, "stop");
15035
+ }
15036
+ function registerHarnessTurnSpan(args) {
15037
+ if (!isObject(args.session)) {
15038
+ return;
15039
+ }
15040
+ const session = args.session;
15041
+ sessionTurnParents.set(session, args.span);
15042
+ wrapperTurnParents.set(args.span, args.span);
15043
+ patchLifecycleMethods(session);
15044
+ }
15045
+ function harnessContinuationParent(session) {
15046
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
15047
+ }
15048
+ function captureHarnessCreateSessionParent(params) {
15049
+ try {
15050
+ return continuationParentFromCreateSessionParams(params);
15051
+ } catch {
15052
+ return void 0;
15053
+ }
15054
+ }
15055
+ function registerHarnessSessionParent(session, parent) {
15056
+ if (!parent || !isObject(session)) {
15057
+ return;
15058
+ }
15059
+ const harnessSession = session;
15060
+ sessionTurnParents.set(harnessSession, parent);
15061
+ patchLifecycleMethods(harnessSession);
15062
+ }
15063
+ function currentHarnessTurnParent() {
15064
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
15065
+ }
15066
+ function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
15067
+ const startChannel = tracingChannel2.start;
15068
+ if (!startChannel) {
15069
+ return () => {
15070
+ };
15071
+ }
15072
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
15073
+ const store = harnessTurnParentStore;
15074
+ startChannel.bindStore(
15075
+ store,
15076
+ (event) => parentFromEvent(event) ?? store.getStore()
15077
+ );
15078
+ return () => {
15079
+ startChannel.unbindStore(store);
15080
+ };
15081
+ }
15082
+ function startHarnessTurnChildSpan(parent, args) {
15083
+ const spanArgs = withSpanInstrumentationName(
15084
+ args,
15085
+ INSTRUMENTATION_NAMES.AI_SDK
15086
+ );
15087
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
15088
+ }
15089
+ function updateHarnessTurn(parent, update = {}) {
15090
+ if (!parent) {
15091
+ return;
15092
+ }
15093
+ const log3 = (event) => {
15094
+ if (typeof parent === "string") {
15095
+ updateSpan({ exported: parent, ...event });
15096
+ } else {
15097
+ parent.log(event);
15098
+ }
15099
+ };
15100
+ try {
15101
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
15102
+ log3({ output: null });
15103
+ }
15104
+ log3(update);
15105
+ } catch {
15106
+ }
15107
+ }
15108
+ function endHarnessTurn(parent) {
15109
+ const endTime = getCurrentUnixTimestamp();
15110
+ if (!parent) {
15111
+ return endTime;
15112
+ }
15113
+ try {
15114
+ const event = { metrics: { end: endTime } };
15115
+ if (typeof parent === "string") {
15116
+ updateSpan({ exported: parent, ...event });
15117
+ } else {
15118
+ parent.log(event);
15119
+ }
15120
+ } catch {
15121
+ }
15122
+ return endTime;
15123
+ }
15124
+
14566
15125
  // src/wrappers/ai-sdk/telemetry.ts
14567
15126
  function braintrustAISDKTelemetry() {
14568
15127
  const operations = /* @__PURE__ */ new Map();
@@ -14581,14 +15140,17 @@ function braintrustAISDKTelemetry() {
14581
15140
  console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
14582
15141
  }
14583
15142
  };
14584
- const startChildSpan = (operationKey, name, type, event) => {
15143
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
14585
15144
  const parent = operations.get(operationKey)?.span;
14586
- const spanArgs = {
14587
- name,
14588
- spanAttributes: { type },
14589
- ...event ? { event } : {}
14590
- };
14591
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
15145
+ const spanArgs = withSpanInstrumentationName(
15146
+ {
15147
+ name,
15148
+ spanAttributes: { type },
15149
+ ...event ? { event } : {}
15150
+ },
15151
+ INSTRUMENTATION_NAMES.AI_SDK
15152
+ );
15153
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
14592
15154
  const state = operations.get(operationKey);
14593
15155
  if (state && type === "llm" /* LLM */) {
14594
15156
  state.hadModelChild = true;
@@ -14868,14 +15430,30 @@ function braintrustAISDKTelemetry() {
14868
15430
  const workflowAgent = operationName === "WorkflowAgent.stream";
14869
15431
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
14870
15432
  const ownsSpan = !wrapperSpan;
14871
- const span = ownsSpan ? startSpan({
14872
- name: operationName,
14873
- spanAttributes: { type: "function" /* FUNCTION */ }
14874
- }) : wrapperSpan;
15433
+ const harnessTurnParent = currentHarnessTurnParent();
15434
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
15435
+ harnessTurnParent,
15436
+ withSpanInstrumentationName(
15437
+ {
15438
+ name: operationName,
15439
+ spanAttributes: { type: "function" /* FUNCTION */ }
15440
+ },
15441
+ INSTRUMENTATION_NAMES.AI_SDK
15442
+ )
15443
+ ) : startSpan(
15444
+ withSpanInstrumentationName(
15445
+ {
15446
+ name: operationName,
15447
+ spanAttributes: { type: "function" /* FUNCTION */ }
15448
+ },
15449
+ INSTRUMENTATION_NAMES.AI_SDK
15450
+ )
15451
+ ) : wrapperSpan;
14875
15452
  const operationKey = createOperationKey(event, operationName);
14876
15453
  registerOperation({
14877
15454
  callId: event.callId,
14878
15455
  hadModelChild: false,
15456
+ harnessTurnParent,
14879
15457
  loggedInput: false,
14880
15458
  operationName,
14881
15459
  operationKey,
@@ -15116,7 +15694,8 @@ function braintrustAISDKTelemetry() {
15116
15694
  toolCallId,
15117
15695
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
15118
15696
  }
15119
- }
15697
+ },
15698
+ state?.harnessTurnParent
15120
15699
  );
15121
15700
  toolSpans.set(toolCallId, { operationKey, span });
15122
15701
  });
@@ -15352,74 +15931,102 @@ function finishOutput(result, operationName) {
15352
15931
  }
15353
15932
 
15354
15933
  // src/instrumentation/plugins/ai-sdk-channels.ts
15355
- var aiSDKChannels = defineChannels("ai", {
15356
- generateText: channel({
15357
- channelName: "generateText",
15358
- kind: "async"
15359
- }),
15360
- streamText: channel({
15361
- channelName: "streamText",
15362
- kind: "async"
15363
- }),
15364
- streamTextSync: channel({
15365
- channelName: "streamText.sync",
15366
- kind: "sync-stream"
15367
- }),
15368
- generateObject: channel({
15369
- channelName: "generateObject",
15370
- kind: "async"
15371
- }),
15372
- streamObject: channel({
15373
- channelName: "streamObject",
15374
- kind: "async"
15375
- }),
15376
- streamObjectSync: channel({
15377
- channelName: "streamObject.sync",
15378
- kind: "sync-stream"
15379
- }),
15380
- embed: channel(
15381
- {
15934
+ var aiSDKChannels = defineChannels(
15935
+ "ai",
15936
+ {
15937
+ generateText: channel({
15938
+ channelName: "generateText",
15939
+ kind: "async"
15940
+ }),
15941
+ streamText: channel({
15942
+ channelName: "streamText",
15943
+ kind: "async"
15944
+ }),
15945
+ streamTextSync: channel({
15946
+ channelName: "streamText.sync",
15947
+ kind: "sync-stream"
15948
+ }),
15949
+ generateObject: channel({
15950
+ channelName: "generateObject",
15951
+ kind: "async"
15952
+ }),
15953
+ streamObject: channel({
15954
+ channelName: "streamObject",
15955
+ kind: "async"
15956
+ }),
15957
+ streamObjectSync: channel({
15958
+ channelName: "streamObject.sync",
15959
+ kind: "sync-stream"
15960
+ }),
15961
+ embed: channel({
15382
15962
  channelName: "embed",
15383
15963
  kind: "async"
15384
- }
15385
- ),
15386
- embedMany: channel({
15387
- channelName: "embedMany",
15388
- kind: "async"
15389
- }),
15390
- rerank: channel({
15391
- channelName: "rerank",
15392
- kind: "async"
15393
- }),
15394
- agentGenerate: channel({
15395
- channelName: "Agent.generate",
15396
- kind: "async"
15397
- }),
15398
- agentStream: channel({
15399
- channelName: "Agent.stream",
15400
- kind: "async"
15401
- }),
15402
- agentStreamSync: channel({
15403
- channelName: "Agent.stream.sync",
15404
- kind: "sync-stream"
15405
- }),
15406
- toolLoopAgentGenerate: channel({
15407
- channelName: "ToolLoopAgent.generate",
15408
- kind: "async"
15409
- }),
15410
- toolLoopAgentStream: channel({
15411
- channelName: "ToolLoopAgent.stream",
15412
- kind: "async"
15413
- }),
15414
- workflowAgentStream: channel({
15415
- channelName: "WorkflowAgent.stream",
15416
- kind: "async"
15417
- }),
15418
- v7CreateTelemetryDispatcher: channel({
15419
- channelName: "createTelemetryDispatcher",
15420
- kind: "sync-stream"
15421
- })
15422
- });
15964
+ }),
15965
+ embedMany: channel({
15966
+ channelName: "embedMany",
15967
+ kind: "async"
15968
+ }),
15969
+ rerank: channel({
15970
+ channelName: "rerank",
15971
+ kind: "async"
15972
+ }),
15973
+ agentGenerate: channel({
15974
+ channelName: "Agent.generate",
15975
+ kind: "async"
15976
+ }),
15977
+ agentStream: channel({
15978
+ channelName: "Agent.stream",
15979
+ kind: "async"
15980
+ }),
15981
+ agentStreamSync: channel({
15982
+ channelName: "Agent.stream.sync",
15983
+ kind: "sync-stream"
15984
+ }),
15985
+ toolLoopAgentGenerate: channel({
15986
+ channelName: "ToolLoopAgent.generate",
15987
+ kind: "async"
15988
+ }),
15989
+ toolLoopAgentStream: channel({
15990
+ channelName: "ToolLoopAgent.stream",
15991
+ kind: "async"
15992
+ }),
15993
+ workflowAgentStream: channel({
15994
+ channelName: "WorkflowAgent.stream",
15995
+ kind: "async"
15996
+ }),
15997
+ v7CreateTelemetryDispatcher: channel({
15998
+ channelName: "createTelemetryDispatcher",
15999
+ kind: "sync-stream"
16000
+ })
16001
+ },
16002
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
16003
+ );
16004
+ var harnessAgentChannels = defineChannels(
16005
+ "@ai-sdk/harness",
16006
+ {
16007
+ createSession: channel({
16008
+ channelName: "HarnessAgent.createSession",
16009
+ kind: "async"
16010
+ }),
16011
+ generate: channel({
16012
+ channelName: "HarnessAgent.generate",
16013
+ kind: "async"
16014
+ }),
16015
+ stream: channel({
16016
+ channelName: "HarnessAgent.stream",
16017
+ kind: "async"
16018
+ }),
16019
+ continueGenerate: channel({
16020
+ channelName: "HarnessAgent.continueGenerate",
16021
+ kind: "async"
16022
+ }),
16023
+ continueStream: channel({
16024
+ channelName: "HarnessAgent.continueStream",
16025
+ kind: "async"
16026
+ })
16027
+ },
16028
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
16029
+ );
15423
16030
 
15424
16031
  // src/instrumentation/plugins/ai-sdk-plugin.ts
15425
16032
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -15496,6 +16103,17 @@ var AISDKPlugin = class extends BasePlugin {
15496
16103
  subscribeToAISDK() {
15497
16104
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
15498
16105
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
16106
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
16107
+ this.unsubscribers.push(
16108
+ subscribeToHarnessContinuation(
16109
+ harnessAgentChannels.continueGenerate,
16110
+ denyOutputPaths
16111
+ ),
16112
+ subscribeToHarnessContinuation(
16113
+ harnessAgentChannels.continueStream,
16114
+ denyOutputPaths
16115
+ )
16116
+ );
15499
16117
  this.unsubscribers.push(
15500
16118
  traceStreamingChannel(aiSDKChannels.generateText, {
15501
16119
  name: "generateText",
@@ -15688,6 +16306,90 @@ var AISDKPlugin = class extends BasePlugin {
15688
16306
  })
15689
16307
  })
15690
16308
  );
16309
+ this.unsubscribers.push(
16310
+ traceStreamingChannel(harnessAgentChannels.generate, {
16311
+ name: "HarnessAgent.generate",
16312
+ startSpan: _internalStartSpanWithInitialMerge,
16313
+ type: "task" /* TASK */,
16314
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16315
+ extractOutput: (result, endEvent) => processAISDKOutput(
16316
+ result,
16317
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16318
+ ),
16319
+ extractMetrics: (result) => extractTokenMetrics(result),
16320
+ aggregateChunks: aggregateAISDKChunks
16321
+ })
16322
+ );
16323
+ this.unsubscribers.push(
16324
+ traceStreamingChannel(harnessAgentChannels.stream, {
16325
+ name: "HarnessAgent.stream",
16326
+ startSpan: _internalStartSpanWithInitialMerge,
16327
+ type: "task" /* TASK */,
16328
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16329
+ extractOutput: (result, endEvent) => processAISDKOutput(
16330
+ result,
16331
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16332
+ ),
16333
+ extractMetrics: (result, startTime) => ({
16334
+ ...extractTokenMetrics(result),
16335
+ ...startTime === void 0 ? {} : {
16336
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16337
+ }
16338
+ }),
16339
+ aggregateChunks: aggregateAISDKChunks,
16340
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16341
+ defaultDenyOutputPaths: denyOutputPaths,
16342
+ endEvent,
16343
+ result,
16344
+ resolvePromiseUsage: true,
16345
+ span,
16346
+ startTime
16347
+ })
16348
+ })
16349
+ );
16350
+ this.unsubscribers.push(
16351
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
16352
+ name: "HarnessAgent.continueGenerate",
16353
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16354
+ startSpan: _internalStartSpanWithInitialMerge,
16355
+ type: "task" /* TASK */,
16356
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16357
+ extractOutput: (result, endEvent) => processAISDKOutput(
16358
+ result,
16359
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16360
+ ),
16361
+ extractMetrics: (result) => extractTokenMetrics(result),
16362
+ aggregateChunks: aggregateAISDKChunks
16363
+ })
16364
+ );
16365
+ this.unsubscribers.push(
16366
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
16367
+ name: "HarnessAgent.continueStream",
16368
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16369
+ startSpan: _internalStartSpanWithInitialMerge,
16370
+ type: "task" /* TASK */,
16371
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16372
+ extractOutput: (result, endEvent) => processAISDKOutput(
16373
+ result,
16374
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16375
+ ),
16376
+ extractMetrics: (result, startTime) => ({
16377
+ ...extractTokenMetrics(result),
16378
+ ...startTime === void 0 ? {} : {
16379
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16380
+ }
16381
+ }),
16382
+ aggregateChunks: aggregateAISDKChunks,
16383
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16384
+ defaultDenyOutputPaths: denyOutputPaths,
16385
+ endEvent,
16386
+ result,
16387
+ resolvePromiseUsage: true,
16388
+ span,
16389
+ startTime
16390
+ })
16391
+ })
16392
+ );
15691
16393
  this.unsubscribers.push(
15692
16394
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
15693
16395
  name: "ToolLoopAgent.generate",
@@ -15758,6 +16460,8 @@ var AISDKPlugin = class extends BasePlugin {
15758
16460
  defaultDenyOutputPaths: denyOutputPaths,
15759
16461
  endEvent,
15760
16462
  onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
16463
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
16464
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
15761
16465
  result,
15762
16466
  span,
15763
16467
  startTime
@@ -15766,6 +16470,153 @@ var AISDKPlugin = class extends BasePlugin {
15766
16470
  );
15767
16471
  }
15768
16472
  };
16473
+ function subscribeToHarnessAgentCreateSession() {
16474
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
16475
+ const parents = /* @__PURE__ */ new WeakMap();
16476
+ const handlers = {
16477
+ start: (event) => {
16478
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
16479
+ if (parent) {
16480
+ parents.set(event, parent);
16481
+ }
16482
+ },
16483
+ asyncEnd: (event) => {
16484
+ registerHarnessSessionParent(event.result, parents.get(event));
16485
+ parents.delete(event);
16486
+ },
16487
+ error: (event) => {
16488
+ parents.delete(event);
16489
+ }
16490
+ };
16491
+ channel2.subscribe(handlers);
16492
+ return () => channel2.unsubscribe(handlers);
16493
+ }
16494
+ function harnessContinuationParentFromEvent(event) {
16495
+ try {
16496
+ return harnessContinuationParent(event.arguments?.[0]?.session);
16497
+ } catch {
16498
+ return void 0;
16499
+ }
16500
+ }
16501
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
16502
+ const channel2 = continuationChannel.tracingChannel();
16503
+ const parents = /* @__PURE__ */ new WeakMap();
16504
+ const startTimes = /* @__PURE__ */ new WeakMap();
16505
+ const unbindParentStore = bindHarnessTurnParentToStart(
16506
+ channel2,
16507
+ harnessContinuationParentFromEvent
16508
+ );
16509
+ const handlers = {
16510
+ start: (event) => {
16511
+ const parent = harnessContinuationParentFromEvent(event);
16512
+ if (!parent) {
16513
+ return;
16514
+ }
16515
+ parents.set(event, parent);
16516
+ startTimes.set(event, getCurrentUnixTimestamp());
16517
+ try {
16518
+ const params = event.arguments?.[0];
16519
+ if (params) {
16520
+ prepareAISDKHarnessAgentInput(params, event.self);
16521
+ }
16522
+ } catch (error) {
16523
+ debugLogger.error(
16524
+ "Error preparing Harness continuation telemetry:",
16525
+ error
16526
+ );
16527
+ }
16528
+ },
16529
+ asyncEnd: (event) => {
16530
+ const parent = parents.get(event);
16531
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
16532
+ parents.delete(event);
16533
+ startTimes.delete(event);
16534
+ if (!parent) {
16535
+ return;
16536
+ }
16537
+ const endEvent = event;
16538
+ const span = {
16539
+ end: () => endHarnessTurn(parent),
16540
+ log: (update) => updateHarnessTurn(
16541
+ parent,
16542
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
16543
+ )
16544
+ };
16545
+ try {
16546
+ if (isAsyncIterable(endEvent.result)) {
16547
+ patchStreamIfNeeded(endEvent.result, {
16548
+ onComplete: (chunks) => {
16549
+ try {
16550
+ const { metadata, metrics, output } = aggregateAISDKChunks(
16551
+ chunks,
16552
+ endEvent.result,
16553
+ endEvent
16554
+ );
16555
+ span.log({
16556
+ ...metadata ? { metadata } : {},
16557
+ metrics,
16558
+ output
16559
+ });
16560
+ } catch (error) {
16561
+ debugLogger.error(
16562
+ "Error aggregating Harness continuation stream:",
16563
+ error
16564
+ );
16565
+ } finally {
16566
+ span.end();
16567
+ }
16568
+ },
16569
+ onError: (error) => {
16570
+ span.log({ error: toLoggedError(error), output: null });
16571
+ span.end();
16572
+ }
16573
+ });
16574
+ return;
16575
+ }
16576
+ if (patchAISDKStreamingResult({
16577
+ defaultDenyOutputPaths,
16578
+ endEvent,
16579
+ result: endEvent.result,
16580
+ resolvePromiseUsage: true,
16581
+ span,
16582
+ startTime
16583
+ })) {
16584
+ return;
16585
+ }
16586
+ finalizeAISDKChildTracing(endEvent);
16587
+ span.log({
16588
+ metrics: extractTokenMetrics(endEvent.result),
16589
+ output: processAISDKOutput(
16590
+ endEvent.result,
16591
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16592
+ )
16593
+ });
16594
+ span.end();
16595
+ } catch (error) {
16596
+ debugLogger.error("Error tracing Harness continuation:", error);
16597
+ span.end();
16598
+ }
16599
+ },
16600
+ error: (event) => {
16601
+ const parent = parents.get(event);
16602
+ parents.delete(event);
16603
+ startTimes.delete(event);
16604
+ if (!parent) {
16605
+ return;
16606
+ }
16607
+ updateHarnessTurn(parent, {
16608
+ error: toLoggedError(event.error),
16609
+ output: null
16610
+ });
16611
+ endHarnessTurn(parent);
16612
+ }
16613
+ };
16614
+ channel2.subscribe(handlers);
16615
+ return () => {
16616
+ unbindParentStore();
16617
+ channel2.unsubscribe(handlers);
16618
+ };
16619
+ }
15769
16620
  function subscribeToAISDKV7TelemetryDispatcher() {
15770
16621
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
15771
16622
  const telemetry = braintrustAISDKTelemetry();
@@ -16218,6 +17069,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
16218
17069
  metadata
16219
17070
  };
16220
17071
  }
17072
+ function prepareAISDKHarnessAgentInput(params, self, span) {
17073
+ if (span) {
17074
+ registerHarnessTurnSpan({
17075
+ session: params.session,
17076
+ span
17077
+ });
17078
+ }
17079
+ if (isObject(self)) {
17080
+ try {
17081
+ if (isObject(self.settings)) {
17082
+ const settings = self.settings;
17083
+ if (settings.telemetry === void 0) {
17084
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
17085
+ }
17086
+ }
17087
+ } catch {
17088
+ }
17089
+ }
17090
+ const selectedInput = {};
17091
+ if (params.prompt !== void 0) {
17092
+ selectedInput.prompt = params.prompt;
17093
+ }
17094
+ if (params.messages !== void 0) {
17095
+ selectedInput.messages = params.messages;
17096
+ }
17097
+ if (params.toolApprovalContinuations !== void 0) {
17098
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
17099
+ }
17100
+ const metadata = extractMetadataFromCallParams({}, self);
17101
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
17102
+ metadata.sessionId = params.session.sessionId;
17103
+ }
17104
+ if (isObject(self)) {
17105
+ if (typeof self.harnessId === "string") {
17106
+ metadata.harnessId = self.harnessId;
17107
+ }
17108
+ if (typeof self.permissionMode === "string") {
17109
+ metadata.permissionMode = self.permissionMode;
17110
+ }
17111
+ }
17112
+ return {
17113
+ input: processAISDKCallInput(selectedInput).input,
17114
+ metadata
17115
+ };
17116
+ }
17117
+ function harnessSessionFromArguments(args) {
17118
+ const params = args[0];
17119
+ return isObject(params) ? params.session : void 0;
17120
+ }
16221
17121
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
16222
17122
  registerWorkflowAgentWrapperSpan(span);
16223
17123
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -16665,19 +17565,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16665
17565
  activeEntry.openSpans.delete(span);
16666
17566
  }
16667
17567
  },
16668
- {
16669
- name: "doGenerate",
16670
- spanAttributes: {
16671
- type: "llm" /* LLM */
17568
+ withSpanInstrumentationName(
17569
+ {
17570
+ name: "doGenerate",
17571
+ spanAttributes: {
17572
+ type: "llm" /* LLM */
17573
+ },
17574
+ event: buildAISDKModelStartEvent(
17575
+ callOptions,
17576
+ activeEntry.baseMetadata,
17577
+ {
17578
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17579
+ }
17580
+ )
16672
17581
  },
16673
- event: buildAISDKModelStartEvent(
16674
- callOptions,
16675
- activeEntry.baseMetadata,
16676
- {
16677
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16678
- }
16679
- )
16680
- }
17582
+ INSTRUMENTATION_NAMES.AI_SDK
17583
+ )
16681
17584
  );
16682
17585
  };
16683
17586
  if (originalDoStream) {
@@ -16687,19 +17590,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16687
17590
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
16688
17591
  }
16689
17592
  closeOpenAISDKModelPatchSpans(activeEntry);
16690
- const span = activeEntry.parentSpan.startSpan({
16691
- name: "doStream",
16692
- spanAttributes: {
16693
- type: "llm" /* LLM */
16694
- },
16695
- event: buildAISDKModelStartEvent(
16696
- callOptions,
16697
- activeEntry.baseMetadata,
17593
+ const span = activeEntry.parentSpan.startSpan(
17594
+ withSpanInstrumentationName(
16698
17595
  {
16699
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16700
- }
17596
+ name: "doStream",
17597
+ spanAttributes: {
17598
+ type: "llm" /* LLM */
17599
+ },
17600
+ event: buildAISDKModelStartEvent(
17601
+ callOptions,
17602
+ activeEntry.baseMetadata,
17603
+ {
17604
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17605
+ }
17606
+ )
17607
+ },
17608
+ INSTRUMENTATION_NAMES.AI_SDK
16701
17609
  )
16702
- });
17610
+ );
16703
17611
  activeEntry.openSpans.add(span);
16704
17612
  const streamStartTime = getCurrentUnixTimestamp();
16705
17613
  const result = await withCurrent(
@@ -16871,12 +17779,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16871
17779
  }
16872
17780
  if (isAsyncGenerator(result)) {
16873
17781
  return (async function* () {
16874
- const span = activeEntry.parentSpan.startSpan({
16875
- name: activeEntry.name,
16876
- spanAttributes: {
16877
- type: "tool" /* TOOL */
16878
- }
16879
- });
17782
+ const span = activeEntry.parentSpan.startSpan(
17783
+ withSpanInstrumentationName(
17784
+ {
17785
+ name: activeEntry.name,
17786
+ spanAttributes: {
17787
+ type: "tool" /* TOOL */
17788
+ }
17789
+ },
17790
+ INSTRUMENTATION_NAMES.AI_SDK
17791
+ )
17792
+ );
16880
17793
  span.log({ input: serializeToolExecutionInput(args) });
16881
17794
  try {
16882
17795
  let lastValue;
@@ -16900,12 +17813,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16900
17813
  span.log({ output: awaitedResult });
16901
17814
  return awaitedResult;
16902
17815
  },
16903
- {
16904
- name: activeEntry.name,
16905
- spanAttributes: {
16906
- type: "tool" /* TOOL */
16907
- }
16908
- }
17816
+ withSpanInstrumentationName(
17817
+ {
17818
+ name: activeEntry.name,
17819
+ spanAttributes: {
17820
+ type: "tool" /* TOOL */
17821
+ }
17822
+ },
17823
+ INSTRUMENTATION_NAMES.AI_SDK
17824
+ )
16909
17825
  );
16910
17826
  };
16911
17827
  cleanup.push(() => {
@@ -17064,7 +17980,10 @@ function patchAISDKStreamingResult(args) {
17064
17980
  defaultDenyOutputPaths,
17065
17981
  endEvent,
17066
17982
  onComplete,
17983
+ onCancel,
17984
+ onError,
17067
17985
  result,
17986
+ resolvePromiseUsage,
17068
17987
  span,
17069
17988
  startTime
17070
17989
  } = args;
@@ -17074,14 +17993,43 @@ function patchAISDKStreamingResult(args) {
17074
17993
  const resultRecord = result;
17075
17994
  attachKnownResultPromiseHandlers(resultRecord);
17076
17995
  let finalized = false;
17077
- const finalize = () => {
17996
+ const finalize = (outcome) => {
17078
17997
  if (finalized) {
17079
17998
  return;
17080
17999
  }
17081
18000
  finalized = true;
17082
- finalizeAISDKChildTracing(endEvent);
17083
- span.end();
17084
- onComplete?.();
18001
+ try {
18002
+ finalizeAISDKChildTracing(endEvent);
18003
+ } catch (error) {
18004
+ debugLogger.error("Error finalizing AI SDK child tracing:", error);
18005
+ }
18006
+ try {
18007
+ span.end();
18008
+ } catch (error) {
18009
+ debugLogger.error("Error ending AI SDK streaming span:", error);
18010
+ }
18011
+ if (outcome && "error" in outcome) {
18012
+ try {
18013
+ onError?.(outcome.error);
18014
+ } catch (error) {
18015
+ debugLogger.error("Error in AI SDK streaming error hook:", error);
18016
+ }
18017
+ } else if (outcome) {
18018
+ try {
18019
+ onComplete?.(outcome);
18020
+ } catch (error) {
18021
+ debugLogger.error("Error in AI SDK streaming completion hook:", error);
18022
+ }
18023
+ } else {
18024
+ try {
18025
+ onCancel?.();
18026
+ } catch (error) {
18027
+ debugLogger.error(
18028
+ "Error in AI SDK streaming cancellation hook:",
18029
+ error
18030
+ );
18031
+ }
18032
+ }
17085
18033
  };
17086
18034
  let outputLogged = false;
17087
18035
  const logStreamingOutput = async (firstChunkTime) => {
@@ -17091,6 +18039,22 @@ function patchAISDKStreamingResult(args) {
17091
18039
  outputLogged = true;
17092
18040
  try {
17093
18041
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
18042
+ if (resolvePromiseUsage) {
18043
+ try {
18044
+ const resultRecord2 = result;
18045
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
18046
+ if (isPromiseLike(pendingUsage)) {
18047
+ const usage = await Promise.resolve(pendingUsage);
18048
+ if (isObject(usage)) {
18049
+ Object.assign(
18050
+ metrics,
18051
+ extractTokenMetrics({ usage })
18052
+ );
18053
+ }
18054
+ }
18055
+ } catch {
18056
+ }
18057
+ }
17094
18058
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
17095
18059
  metrics.time_to_first_token = firstChunkTime - startTime;
17096
18060
  }
@@ -17099,15 +18063,29 @@ function patchAISDKStreamingResult(args) {
17099
18063
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
17100
18064
  );
17101
18065
  const metadata = buildResolvedMetadataPayload(result).metadata;
17102
- span.log({
17103
- output,
17104
- ...metadata ? { metadata } : {},
17105
- metrics
17106
- });
18066
+ try {
18067
+ span.log({
18068
+ output,
18069
+ ...metadata ? { metadata } : {},
18070
+ metrics
18071
+ });
18072
+ } catch (error) {
18073
+ debugLogger.error("Error logging AI SDK streaming output:", error);
18074
+ }
18075
+ finalize({ metrics, output });
17107
18076
  } catch (error) {
17108
- span.log({ error: toLoggedError(error) });
17109
- } finally {
17110
- finalize();
18077
+ const loggedError = toLoggedError(error);
18078
+ try {
18079
+ span.log({ error: loggedError });
18080
+ } catch (loggingError) {
18081
+ debugLogger.error(
18082
+ "Error logging AI SDK streaming failure:",
18083
+ loggingError
18084
+ );
18085
+ }
18086
+ finalize({
18087
+ error: error instanceof Error ? error : new Error(String(loggedError))
18088
+ });
17111
18089
  }
17112
18090
  };
17113
18091
  const createAsyncIterableHooks = () => {
@@ -17124,11 +18102,16 @@ function patchAISDKStreamingResult(args) {
17124
18102
  onError: (error) => {
17125
18103
  if (!outputLogged) {
17126
18104
  outputLogged = true;
17127
- span.log({
17128
- error: error.message
17129
- });
18105
+ try {
18106
+ span.log({ error });
18107
+ } catch (loggingError) {
18108
+ debugLogger.error(
18109
+ "Error logging AI SDK stream failure:",
18110
+ loggingError
18111
+ );
18112
+ }
17130
18113
  }
17131
- finalize();
18114
+ finalize({ error });
17132
18115
  },
17133
18116
  onCancel: finalize
17134
18117
  };
@@ -17214,8 +18197,18 @@ function patchAISDKStreamingResult(args) {
17214
18197
  }
17215
18198
  controller.enqueue(value);
17216
18199
  } catch (error) {
17217
- span.log({ error: toLoggedError(error) });
17218
- finalize();
18200
+ const loggedError = toLoggedError(error);
18201
+ try {
18202
+ span.log({ error: loggedError });
18203
+ } catch (loggingError) {
18204
+ debugLogger.error(
18205
+ "Error logging AI SDK base stream failure:",
18206
+ loggingError
18207
+ );
18208
+ }
18209
+ finalize({
18210
+ error: error instanceof Error ? error : new Error(String(loggedError))
18211
+ });
17219
18212
  controller.error(error);
17220
18213
  }
17221
18214
  },
@@ -18004,7 +18997,8 @@ var claudeAgentSDKChannels = defineChannels(
18004
18997
  channelName: "query",
18005
18998
  kind: "sync-stream"
18006
18999
  })
18007
- }
19000
+ },
19001
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
18008
19002
  );
18009
19003
 
18010
19004
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -18152,22 +19146,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
18152
19146
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
18153
19147
  const runWithResolvedParent = async () => {
18154
19148
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
18155
- const span = startSpan({
18156
- event: {
18157
- input: handlerArgs[0],
18158
- metadata: {
18159
- "claude_agent_sdk.raw_tool_name": rawToolName,
18160
- "gen_ai.tool.name": metadata.toolName,
18161
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
18162
- ...metadata.serverName && {
18163
- "mcp.server": metadata.serverName
18164
- }
18165
- }
18166
- },
18167
- name: spanName,
18168
- ...parent && { parent },
18169
- spanAttributes: { type: "tool" /* TOOL */ }
18170
- });
19149
+ const span = startSpan(
19150
+ withSpanInstrumentationName(
19151
+ {
19152
+ event: {
19153
+ input: handlerArgs[0],
19154
+ metadata: {
19155
+ "claude_agent_sdk.raw_tool_name": rawToolName,
19156
+ "gen_ai.tool.name": metadata.toolName,
19157
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
19158
+ ...metadata.serverName && {
19159
+ "mcp.server": metadata.serverName
19160
+ }
19161
+ }
19162
+ },
19163
+ name: spanName,
19164
+ ...parent && { parent },
19165
+ spanAttributes: { type: "tool" /* TOOL */ }
19166
+ },
19167
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19168
+ )
19169
+ );
18171
19170
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
18172
19171
  const finalizeSuccess = (result) => {
18173
19172
  span.log({ output: result });
@@ -18492,14 +19491,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
18492
19491
  ).filter(
18493
19492
  (c) => c !== void 0
18494
19493
  );
18495
- const span = existingSpan ?? startSpan({
18496
- name: "anthropic.messages.create",
18497
- parent: parentSpan,
18498
- spanAttributes: {
18499
- type: "llm" /* LLM */
18500
- },
18501
- startTime
18502
- });
19494
+ const span = existingSpan ?? startSpan(
19495
+ withSpanInstrumentationName(
19496
+ {
19497
+ name: "anthropic.messages.create",
19498
+ parent: parentSpan,
19499
+ spanAttributes: {
19500
+ type: "llm" /* LLM */
19501
+ },
19502
+ startTime
19503
+ },
19504
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19505
+ )
19506
+ );
18503
19507
  span.log({
18504
19508
  input,
18505
19509
  metadata: model ? { model } : void 0,
@@ -18602,25 +19606,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
18602
19606
  return {};
18603
19607
  }
18604
19608
  const parsed = parseToolName(input.tool_name);
18605
- const toolSpan = startSpan({
18606
- event: {
18607
- input: input.tool_input,
18608
- metadata: {
18609
- "claude_agent_sdk.cwd": input.cwd,
18610
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
18611
- "claude_agent_sdk.session_id": input.session_id,
18612
- "gen_ai.tool.call.id": toolUseID,
18613
- "gen_ai.tool.name": parsed.toolName,
18614
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
18615
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
18616
- }
18617
- },
18618
- name: parsed.displayName,
18619
- parent: await resolveParentSpan(toolUseID, {
18620
- agentId: input.agent_id
18621
- }),
18622
- spanAttributes: { type: "tool" /* TOOL */ }
18623
- });
19609
+ const toolSpan = startSpan(
19610
+ withSpanInstrumentationName(
19611
+ {
19612
+ event: {
19613
+ input: input.tool_input,
19614
+ metadata: {
19615
+ "claude_agent_sdk.cwd": input.cwd,
19616
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
19617
+ "claude_agent_sdk.session_id": input.session_id,
19618
+ "gen_ai.tool.call.id": toolUseID,
19619
+ "gen_ai.tool.name": parsed.toolName,
19620
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
19621
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
19622
+ }
19623
+ },
19624
+ name: parsed.displayName,
19625
+ parent: await resolveParentSpan(toolUseID, {
19626
+ agentId: input.agent_id
19627
+ }),
19628
+ spanAttributes: { type: "tool" /* TOOL */ }
19629
+ },
19630
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19631
+ )
19632
+ );
18624
19633
  activeToolSpans.set(toolUseID, toolSpan);
18625
19634
  return {};
18626
19635
  };
@@ -18986,14 +19995,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
18986
19995
  const spanName = formatSubAgentSpanName(details);
18987
19996
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
18988
19997
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
18989
- const subAgentSpan = startSpan({
18990
- event: {
18991
- metadata: subAgentDetailsToMetadata(details)
18992
- },
18993
- name: spanName,
18994
- parent: parentSpan,
18995
- spanAttributes: { type: "task" /* TASK */ }
18996
- });
19998
+ const subAgentSpan = startSpan(
19999
+ withSpanInstrumentationName(
20000
+ {
20001
+ event: {
20002
+ metadata: subAgentDetailsToMetadata(details)
20003
+ },
20004
+ name: spanName,
20005
+ parent: parentSpan,
20006
+ spanAttributes: { type: "task" /* TASK */ }
20007
+ },
20008
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
20009
+ )
20010
+ );
18997
20011
  subAgentSpans.set(parentToolUseId, subAgentSpan);
18998
20012
  return subAgentSpan;
18999
20013
  }
@@ -19014,14 +20028,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
19014
20028
  );
19015
20029
  llmParentSpan = await subAgentSpan.export();
19016
20030
  }
19017
- const llmSpan = startSpan({
19018
- name: "anthropic.messages.create",
19019
- parent: llmParentSpan,
19020
- spanAttributes: {
19021
- type: "llm" /* LLM */
19022
- },
19023
- startTime
19024
- });
20031
+ const llmSpan = startSpan(
20032
+ withSpanInstrumentationName(
20033
+ {
20034
+ name: "anthropic.messages.create",
20035
+ parent: llmParentSpan,
20036
+ spanAttributes: {
20037
+ type: "llm" /* LLM */
20038
+ },
20039
+ startTime
20040
+ },
20041
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
20042
+ )
20043
+ );
19025
20044
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
19026
20045
  return llmSpan;
19027
20046
  }
@@ -19290,12 +20309,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19290
20309
  }
19291
20310
  })();
19292
20311
  }
19293
- const span = startSpan({
19294
- name: "Claude Agent",
19295
- spanAttributes: {
19296
- type: "task" /* TASK */
19297
- }
19298
- });
20312
+ const span = startSpan(
20313
+ withSpanInstrumentationName(
20314
+ {
20315
+ name: "Claude Agent",
20316
+ spanAttributes: {
20317
+ type: "task" /* TASK */
20318
+ }
20319
+ },
20320
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
20321
+ )
20322
+ );
19299
20323
  const startTime = getCurrentUnixTimestamp();
19300
20324
  try {
19301
20325
  span.log({
@@ -19460,24 +20484,28 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19460
20484
  };
19461
20485
 
19462
20486
  // src/instrumentation/plugins/cursor-sdk-channels.ts
19463
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
19464
- create: channel({
19465
- channelName: "Agent.create",
19466
- kind: "async"
19467
- }),
19468
- resume: channel({
19469
- channelName: "Agent.resume",
19470
- kind: "async"
19471
- }),
19472
- prompt: channel({
19473
- channelName: "Agent.prompt",
19474
- kind: "async"
19475
- }),
19476
- send: channel({
19477
- channelName: "agent.send",
19478
- kind: "async"
19479
- })
19480
- });
20487
+ var cursorSDKChannels = defineChannels(
20488
+ "@cursor/sdk",
20489
+ {
20490
+ create: channel({
20491
+ channelName: "Agent.create",
20492
+ kind: "async"
20493
+ }),
20494
+ resume: channel({
20495
+ channelName: "Agent.resume",
20496
+ kind: "async"
20497
+ }),
20498
+ prompt: channel({
20499
+ channelName: "Agent.prompt",
20500
+ kind: "async"
20501
+ }),
20502
+ send: channel({
20503
+ channelName: "agent.send",
20504
+ kind: "async"
20505
+ })
20506
+ },
20507
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
20508
+ );
19481
20509
 
19482
20510
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
19483
20511
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -19529,10 +20557,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19529
20557
  provider: "cursor",
19530
20558
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19531
20559
  };
19532
- const span = startSpan({
19533
- name: "Cursor Agent",
19534
- spanAttributes: { type: "task" /* TASK */ }
19535
- });
20560
+ const span = startSpan(
20561
+ withSpanInstrumentationName(
20562
+ {
20563
+ name: "Cursor Agent",
20564
+ spanAttributes: { type: "task" /* TASK */ }
20565
+ },
20566
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20567
+ )
20568
+ );
19536
20569
  const startTime = getCurrentUnixTimestamp();
19537
20570
  safeLog2(span, {
19538
20571
  input: sanitizeUserMessage(message),
@@ -19594,10 +20627,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19594
20627
  provider: "cursor",
19595
20628
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19596
20629
  };
19597
- const span = startSpan({
19598
- name: "Cursor Agent",
19599
- spanAttributes: { type: "task" /* TASK */ }
19600
- });
20630
+ const span = startSpan(
20631
+ withSpanInstrumentationName(
20632
+ {
20633
+ name: "Cursor Agent",
20634
+ spanAttributes: { type: "task" /* TASK */ }
20635
+ },
20636
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20637
+ )
20638
+ );
19601
20639
  const startTime = getCurrentUnixTimestamp();
19602
20640
  safeLog2(span, {
19603
20641
  input: sanitizeUserMessage(message),
@@ -20044,29 +21082,39 @@ async function startToolSpan(state, args) {
20044
21082
  if (args.truncated?.result !== void 0) {
20045
21083
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
20046
21084
  }
20047
- const span = startSpan({
20048
- event: {
20049
- input: args.args,
20050
- metadata
20051
- },
20052
- name: `tool: ${name}`,
20053
- parent: await state.span.export(),
20054
- spanAttributes: { type: "tool" /* TOOL */ }
20055
- });
21085
+ const span = startSpan(
21086
+ withSpanInstrumentationName(
21087
+ {
21088
+ event: {
21089
+ input: args.args,
21090
+ metadata
21091
+ },
21092
+ name: `tool: ${name}`,
21093
+ parent: await state.span.export(),
21094
+ spanAttributes: { type: "tool" /* TOOL */ }
21095
+ },
21096
+ INSTRUMENTATION_NAMES.CURSOR_SDK
21097
+ )
21098
+ );
20056
21099
  let subAgentSpan;
20057
21100
  if (isSubAgentToolName(name)) {
20058
- subAgentSpan = startSpan({
20059
- event: {
20060
- input: args.args,
20061
- metadata: {
20062
- "cursor_sdk.subagent.tool_call_id": args.callId,
20063
- "gen_ai.tool.name": name
20064
- }
20065
- },
20066
- name: formatSubAgentSpanName2(args.toolCall, args.args),
20067
- parent: await span.export(),
20068
- spanAttributes: { type: "task" /* TASK */ }
20069
- });
21101
+ subAgentSpan = startSpan(
21102
+ withSpanInstrumentationName(
21103
+ {
21104
+ event: {
21105
+ input: args.args,
21106
+ metadata: {
21107
+ "cursor_sdk.subagent.tool_call_id": args.callId,
21108
+ "gen_ai.tool.name": name
21109
+ }
21110
+ },
21111
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
21112
+ parent: await span.export(),
21113
+ spanAttributes: { type: "task" /* TASK */ }
21114
+ },
21115
+ INSTRUMENTATION_NAMES.CURSOR_SDK
21116
+ )
21117
+ );
20070
21118
  }
20071
21119
  return { span, subAgentSpan };
20072
21120
  }
@@ -20325,24 +21373,28 @@ function cleanMetrics2(metrics) {
20325
21373
  }
20326
21374
 
20327
21375
  // src/instrumentation/plugins/openai-agents-channels.ts
20328
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
20329
- onTraceStart: channel({
20330
- channelName: "tracing.processor.onTraceStart",
20331
- kind: "async"
20332
- }),
20333
- onTraceEnd: channel({
20334
- channelName: "tracing.processor.onTraceEnd",
20335
- kind: "async"
20336
- }),
20337
- onSpanStart: channel({
20338
- channelName: "tracing.processor.onSpanStart",
20339
- kind: "async"
20340
- }),
20341
- onSpanEnd: channel({
20342
- channelName: "tracing.processor.onSpanEnd",
20343
- kind: "async"
20344
- })
20345
- });
21376
+ var openAIAgentsCoreChannels = defineChannels(
21377
+ "@openai/agents-core",
21378
+ {
21379
+ onTraceStart: channel({
21380
+ channelName: "tracing.processor.onTraceStart",
21381
+ kind: "async"
21382
+ }),
21383
+ onTraceEnd: channel({
21384
+ channelName: "tracing.processor.onTraceEnd",
21385
+ kind: "async"
21386
+ }),
21387
+ onSpanStart: channel({
21388
+ channelName: "tracing.processor.onSpanStart",
21389
+ kind: "async"
21390
+ }),
21391
+ onSpanEnd: channel({
21392
+ channelName: "tracing.processor.onSpanEnd",
21393
+ kind: "async"
21394
+ })
21395
+ },
21396
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
21397
+ );
20346
21398
 
20347
21399
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
20348
21400
  function isSpanData(spanData, type) {
@@ -20458,16 +21510,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20458
21510
  this.evictOldestTrace();
20459
21511
  }
20460
21512
  const current = currentSpan();
20461
- const span = current && current !== NOOP_SPAN ? current.startSpan({
20462
- name: trace.name,
20463
- type: "task" /* TASK */
20464
- }) : this.logger ? this.logger.startSpan({
20465
- name: trace.name,
20466
- type: "task" /* TASK */
20467
- }) : startSpan({
20468
- name: trace.name,
20469
- type: "task" /* TASK */
20470
- });
21513
+ const spanArgs = withSpanInstrumentationName(
21514
+ {
21515
+ name: trace.name,
21516
+ type: "task" /* TASK */
21517
+ },
21518
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21519
+ );
21520
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
21521
+ withSpanInstrumentationName(
21522
+ spanArgs,
21523
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21524
+ )
21525
+ );
20471
21526
  span.log({
20472
21527
  input: "Agent workflow started",
20473
21528
  metadata: {
@@ -20518,10 +21573,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20518
21573
  if (!parentSpan) {
20519
21574
  return Promise.resolve();
20520
21575
  }
20521
- const childSpan = parentSpan.startSpan({
20522
- name: spanNameFromAgents(span),
20523
- type: spanTypeFromAgents(span)
20524
- });
21576
+ const childSpan = parentSpan.startSpan(
21577
+ withSpanInstrumentationName(
21578
+ {
21579
+ name: spanNameFromAgents(span),
21580
+ type: spanTypeFromAgents(span)
21581
+ },
21582
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21583
+ )
21584
+ );
20525
21585
  traceData.childSpans.set(span.spanId, childSpan);
20526
21586
  return Promise.resolve();
20527
21587
  }
@@ -20786,24 +21846,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
20786
21846
  };
20787
21847
 
20788
21848
  // src/instrumentation/plugins/google-genai-channels.ts
20789
- var googleGenAIChannels = defineChannels("@google/genai", {
20790
- generateContent: channel({
20791
- channelName: "models.generateContent",
20792
- kind: "async"
20793
- }),
20794
- generateContentStream: channel({
20795
- channelName: "models.generateContentStream",
20796
- kind: "async"
20797
- }),
20798
- embedContent: channel({
20799
- channelName: "models.embedContent",
20800
- kind: "async"
20801
- }),
20802
- interactionsCreate: channel({
20803
- channelName: "interactions.create",
20804
- kind: "async"
20805
- })
20806
- });
21849
+ var googleGenAIChannels = defineChannels(
21850
+ "@google/genai",
21851
+ {
21852
+ generateContent: channel({
21853
+ channelName: "models.generateContent",
21854
+ kind: "async"
21855
+ }),
21856
+ generateContentStream: channel({
21857
+ channelName: "models.generateContentStream",
21858
+ kind: "async"
21859
+ }),
21860
+ embedContent: channel({
21861
+ channelName: "models.embedContent",
21862
+ kind: "async"
21863
+ }),
21864
+ interactionsCreate: channel({
21865
+ channelName: "interactions.create",
21866
+ kind: "async"
21867
+ })
21868
+ },
21869
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
21870
+ );
20807
21871
 
20808
21872
  // src/instrumentation/plugins/google-genai-plugin.ts
20809
21873
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -20841,13 +21905,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20841
21905
  const params = event.arguments[0];
20842
21906
  const input = serializeGenerateContentInput(params);
20843
21907
  const metadata = extractGenerateContentMetadata(params);
20844
- const span = startSpan({
20845
- name: "generate_content",
20846
- spanAttributes: {
20847
- type: "llm" /* LLM */
20848
- },
20849
- event: createWrapperParityEvent({ input, metadata })
20850
- });
21908
+ const span = startSpan(
21909
+ withSpanInstrumentationName(
21910
+ {
21911
+ name: "generate_content",
21912
+ spanAttributes: {
21913
+ type: "llm" /* LLM */
21914
+ },
21915
+ event: createWrapperParityEvent({ input, metadata })
21916
+ },
21917
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21918
+ )
21919
+ );
20851
21920
  return {
20852
21921
  span,
20853
21922
  startTime: getCurrentUnixTimestamp()
@@ -20860,13 +21929,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20860
21929
  const params = event.arguments[0];
20861
21930
  const input = serializeGenerateContentInput(params);
20862
21931
  const metadata = extractGenerateContentMetadata(params);
20863
- const span = startSpan({
20864
- name: "generate_content",
20865
- spanAttributes: {
20866
- type: "llm" /* LLM */
20867
- },
20868
- event: createWrapperParityEvent({ input, metadata })
20869
- });
21932
+ const span = startSpan(
21933
+ withSpanInstrumentationName(
21934
+ {
21935
+ name: "generate_content",
21936
+ spanAttributes: {
21937
+ type: "llm" /* LLM */
21938
+ },
21939
+ event: createWrapperParityEvent({ input, metadata })
21940
+ },
21941
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21942
+ )
21943
+ );
20870
21944
  return {
20871
21945
  span,
20872
21946
  startTime: getCurrentUnixTimestamp()
@@ -20942,13 +22016,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20942
22016
  const params = event.arguments[0];
20943
22017
  const input = serializeEmbedContentInput(params);
20944
22018
  const metadata = extractEmbedContentMetadata(params);
20945
- const span = startSpan({
20946
- name: "embed_content",
20947
- spanAttributes: {
20948
- type: "llm" /* LLM */
20949
- },
20950
- event: createWrapperParityEvent({ input, metadata })
20951
- });
22019
+ const span = startSpan(
22020
+ withSpanInstrumentationName(
22021
+ {
22022
+ name: "embed_content",
22023
+ spanAttributes: {
22024
+ type: "llm" /* LLM */
22025
+ },
22026
+ event: createWrapperParityEvent({ input, metadata })
22027
+ },
22028
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
22029
+ )
22030
+ );
20952
22031
  return {
20953
22032
  span,
20954
22033
  startTime: getCurrentUnixTimestamp()
@@ -20961,13 +22040,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20961
22040
  const params = event.arguments[0];
20962
22041
  const input = serializeEmbedContentInput(params);
20963
22042
  const metadata = extractEmbedContentMetadata(params);
20964
- const span = startSpan({
20965
- name: "embed_content",
20966
- spanAttributes: {
20967
- type: "llm" /* LLM */
20968
- },
20969
- event: createWrapperParityEvent({ input, metadata })
20970
- });
22043
+ const span = startSpan(
22044
+ withSpanInstrumentationName(
22045
+ {
22046
+ name: "embed_content",
22047
+ spanAttributes: {
22048
+ type: "llm" /* LLM */
22049
+ },
22050
+ event: createWrapperParityEvent({ input, metadata })
22051
+ },
22052
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
22053
+ )
22054
+ );
20971
22055
  return {
20972
22056
  span,
20973
22057
  startTime: getCurrentUnixTimestamp()
@@ -21078,16 +22162,21 @@ function patchGoogleGenAIStreamingResult(args) {
21078
22162
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
21079
22163
  const ensureSpan = () => {
21080
22164
  if (!span) {
21081
- span = startSpan({
21082
- name: "generate_content_stream",
21083
- spanAttributes: {
21084
- type: "llm" /* LLM */
21085
- },
21086
- event: {
21087
- input,
21088
- metadata
21089
- }
21090
- });
22165
+ span = startSpan(
22166
+ withSpanInstrumentationName(
22167
+ {
22168
+ name: "generate_content_stream",
22169
+ spanAttributes: {
22170
+ type: "llm" /* LLM */
22171
+ },
22172
+ event: {
22173
+ input,
22174
+ metadata
22175
+ }
22176
+ },
22177
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
22178
+ )
22179
+ );
21091
22180
  }
21092
22181
  return span;
21093
22182
  };
@@ -21855,28 +22944,32 @@ function tryToDict(obj) {
21855
22944
  }
21856
22945
 
21857
22946
  // src/instrumentation/plugins/huggingface-channels.ts
21858
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
21859
- chatCompletion: channel({
21860
- channelName: "chatCompletion",
21861
- kind: "async"
21862
- }),
21863
- chatCompletionStream: channel({
21864
- channelName: "chatCompletionStream",
21865
- kind: "sync-stream"
21866
- }),
21867
- textGeneration: channel({
21868
- channelName: "textGeneration",
21869
- kind: "async"
21870
- }),
21871
- textGenerationStream: channel({
21872
- channelName: "textGenerationStream",
21873
- kind: "sync-stream"
21874
- }),
21875
- featureExtraction: channel({
21876
- channelName: "featureExtraction",
21877
- kind: "async"
21878
- })
21879
- });
22947
+ var huggingFaceChannels = defineChannels(
22948
+ "@huggingface/inference",
22949
+ {
22950
+ chatCompletion: channel({
22951
+ channelName: "chatCompletion",
22952
+ kind: "async"
22953
+ }),
22954
+ chatCompletionStream: channel({
22955
+ channelName: "chatCompletionStream",
22956
+ kind: "sync-stream"
22957
+ }),
22958
+ textGeneration: channel({
22959
+ channelName: "textGeneration",
22960
+ kind: "async"
22961
+ }),
22962
+ textGenerationStream: channel({
22963
+ channelName: "textGenerationStream",
22964
+ kind: "sync-stream"
22965
+ }),
22966
+ featureExtraction: channel({
22967
+ channelName: "featureExtraction",
22968
+ kind: "async"
22969
+ })
22970
+ },
22971
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
22972
+ );
21880
22973
 
21881
22974
  // src/instrumentation/plugins/huggingface-plugin.ts
21882
22975
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -22303,20 +23396,24 @@ function extractTextGenerationStreamMetadata(chunks) {
22303
23396
  }
22304
23397
 
22305
23398
  // src/instrumentation/plugins/openrouter-agent-channels.ts
22306
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
22307
- callModel: channel({
22308
- channelName: "callModel",
22309
- kind: "sync-stream"
22310
- }),
22311
- callModelTurn: channel({
22312
- channelName: "callModel.turn",
22313
- kind: "async"
22314
- }),
22315
- toolExecute: channel({
22316
- channelName: "tool.execute",
22317
- kind: "async"
22318
- })
22319
- });
23399
+ var openRouterAgentChannels = defineChannels(
23400
+ "@openrouter/agent",
23401
+ {
23402
+ callModel: channel({
23403
+ channelName: "callModel",
23404
+ kind: "sync-stream"
23405
+ }),
23406
+ callModelTurn: channel({
23407
+ channelName: "callModel.turn",
23408
+ kind: "async"
23409
+ }),
23410
+ toolExecute: channel({
23411
+ channelName: "tool.execute",
23412
+ kind: "async"
23413
+ })
23414
+ },
23415
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
23416
+ );
22320
23417
 
22321
23418
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
22322
23419
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -23086,38 +24183,40 @@ function normalizeError(error) {
23086
24183
  }
23087
24184
 
23088
24185
  // src/instrumentation/plugins/openrouter-channels.ts
23089
- var openRouterChannels = defineChannels("@openrouter/sdk", {
23090
- chatSend: channel({
23091
- channelName: "chat.send",
23092
- kind: "async"
23093
- }),
23094
- embeddingsGenerate: channel({
23095
- channelName: "embeddings.generate",
23096
- kind: "async"
23097
- }),
23098
- rerankRerank: channel(
23099
- {
24186
+ var openRouterChannels = defineChannels(
24187
+ "@openrouter/sdk",
24188
+ {
24189
+ chatSend: channel({
24190
+ channelName: "chat.send",
24191
+ kind: "async"
24192
+ }),
24193
+ embeddingsGenerate: channel({
24194
+ channelName: "embeddings.generate",
24195
+ kind: "async"
24196
+ }),
24197
+ rerankRerank: channel({
23100
24198
  channelName: "rerank.rerank",
23101
24199
  kind: "async"
23102
- }
23103
- ),
23104
- betaResponsesSend: channel({
23105
- channelName: "beta.responses.send",
23106
- kind: "async"
23107
- }),
23108
- callModel: channel({
23109
- channelName: "callModel",
23110
- kind: "sync-stream"
23111
- }),
23112
- callModelTurn: channel({
23113
- channelName: "callModel.turn",
23114
- kind: "async"
23115
- }),
23116
- toolExecute: channel({
23117
- channelName: "tool.execute",
23118
- kind: "async"
23119
- })
23120
- });
24200
+ }),
24201
+ betaResponsesSend: channel({
24202
+ channelName: "beta.responses.send",
24203
+ kind: "async"
24204
+ }),
24205
+ callModel: channel({
24206
+ channelName: "callModel",
24207
+ kind: "sync-stream"
24208
+ }),
24209
+ callModelTurn: channel({
24210
+ channelName: "callModel.turn",
24211
+ kind: "async"
24212
+ }),
24213
+ toolExecute: channel({
24214
+ channelName: "tool.execute",
24215
+ kind: "async"
24216
+ })
24217
+ },
24218
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
24219
+ );
23121
24220
 
23122
24221
  // src/instrumentation/plugins/openrouter-plugin.ts
23123
24222
  var OpenRouterPlugin = class extends BasePlugin {
@@ -24157,52 +25256,56 @@ function normalizeError2(error) {
24157
25256
  }
24158
25257
 
24159
25258
  // src/instrumentation/plugins/mistral-channels.ts
24160
- var mistralChannels = defineChannels("@mistralai/mistralai", {
24161
- chatComplete: channel({
24162
- channelName: "chat.complete",
24163
- kind: "async"
24164
- }),
24165
- chatStream: channel({
24166
- channelName: "chat.stream",
24167
- kind: "async"
24168
- }),
24169
- embeddingsCreate: channel({
24170
- channelName: "embeddings.create",
24171
- kind: "async"
24172
- }),
24173
- classifiersModerate: channel({
24174
- channelName: "classifiers.moderate",
24175
- kind: "async"
24176
- }),
24177
- classifiersModerateChat: channel({
24178
- channelName: "classifiers.moderateChat",
24179
- kind: "async"
24180
- }),
24181
- classifiersClassify: channel({
24182
- channelName: "classifiers.classify",
24183
- kind: "async"
24184
- }),
24185
- classifiersClassifyChat: channel({
24186
- channelName: "classifiers.classifyChat",
24187
- kind: "async"
24188
- }),
24189
- fimComplete: channel({
24190
- channelName: "fim.complete",
24191
- kind: "async"
24192
- }),
24193
- fimStream: channel({
24194
- channelName: "fim.stream",
24195
- kind: "async"
24196
- }),
24197
- agentsComplete: channel({
24198
- channelName: "agents.complete",
24199
- kind: "async"
24200
- }),
24201
- agentsStream: channel({
24202
- channelName: "agents.stream",
24203
- kind: "async"
24204
- })
24205
- });
25259
+ var mistralChannels = defineChannels(
25260
+ "@mistralai/mistralai",
25261
+ {
25262
+ chatComplete: channel({
25263
+ channelName: "chat.complete",
25264
+ kind: "async"
25265
+ }),
25266
+ chatStream: channel({
25267
+ channelName: "chat.stream",
25268
+ kind: "async"
25269
+ }),
25270
+ embeddingsCreate: channel({
25271
+ channelName: "embeddings.create",
25272
+ kind: "async"
25273
+ }),
25274
+ classifiersModerate: channel({
25275
+ channelName: "classifiers.moderate",
25276
+ kind: "async"
25277
+ }),
25278
+ classifiersModerateChat: channel({
25279
+ channelName: "classifiers.moderateChat",
25280
+ kind: "async"
25281
+ }),
25282
+ classifiersClassify: channel({
25283
+ channelName: "classifiers.classify",
25284
+ kind: "async"
25285
+ }),
25286
+ classifiersClassifyChat: channel({
25287
+ channelName: "classifiers.classifyChat",
25288
+ kind: "async"
25289
+ }),
25290
+ fimComplete: channel({
25291
+ channelName: "fim.complete",
25292
+ kind: "async"
25293
+ }),
25294
+ fimStream: channel({
25295
+ channelName: "fim.stream",
25296
+ kind: "async"
25297
+ }),
25298
+ agentsComplete: channel({
25299
+ channelName: "agents.complete",
25300
+ kind: "async"
25301
+ }),
25302
+ agentsStream: channel({
25303
+ channelName: "agents.stream",
25304
+ kind: "async"
25305
+ })
25306
+ },
25307
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
25308
+ );
24206
25309
 
24207
25310
  // src/instrumentation/plugins/mistral-plugin.ts
24208
25311
  var MistralPlugin = class extends BasePlugin {
@@ -24822,20 +25925,24 @@ function aggregateMistralStreamChunks(chunks) {
24822
25925
  }
24823
25926
 
24824
25927
  // src/instrumentation/plugins/google-adk-channels.ts
24825
- var googleADKChannels = defineChannels("@google/adk", {
24826
- runnerRunAsync: channel({
24827
- channelName: "runner.runAsync",
24828
- kind: "sync-stream"
24829
- }),
24830
- agentRunAsync: channel({
24831
- channelName: "agent.runAsync",
24832
- kind: "sync-stream"
24833
- }),
24834
- toolRunAsync: channel({
24835
- channelName: "tool.runAsync",
24836
- kind: "async"
24837
- })
24838
- });
25928
+ var googleADKChannels = defineChannels(
25929
+ "@google/adk",
25930
+ {
25931
+ runnerRunAsync: channel({
25932
+ channelName: "runner.runAsync",
25933
+ kind: "sync-stream"
25934
+ }),
25935
+ agentRunAsync: channel({
25936
+ channelName: "agent.runAsync",
25937
+ kind: "sync-stream"
25938
+ }),
25939
+ toolRunAsync: channel({
25940
+ channelName: "tool.runAsync",
25941
+ kind: "async"
25942
+ })
25943
+ },
25944
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
25945
+ );
24839
25946
 
24840
25947
  // src/instrumentation/plugins/google-adk-plugin.ts
24841
25948
  var GoogleADKPlugin = class extends BasePlugin {
@@ -24860,12 +25967,17 @@ var GoogleADKPlugin = class extends BasePlugin {
24860
25967
  const createState = (event) => {
24861
25968
  const params = event.arguments[0] ?? {};
24862
25969
  const contextKey = extractRunnerContextKey(params);
24863
- const span = startSpan({
24864
- name: "Google ADK Runner",
24865
- spanAttributes: {
24866
- type: "task" /* TASK */
24867
- }
24868
- });
25970
+ const span = startSpan(
25971
+ withSpanInstrumentationName(
25972
+ {
25973
+ name: "Google ADK Runner",
25974
+ spanAttributes: {
25975
+ type: "task" /* TASK */
25976
+ }
25977
+ },
25978
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25979
+ )
25980
+ );
24869
25981
  const startTime = getCurrentUnixTimestamp();
24870
25982
  try {
24871
25983
  const metadata = extractRunnerMetadata(params);
@@ -24951,18 +26063,23 @@ var GoogleADKPlugin = class extends BasePlugin {
24951
26063
  this.activeRunnerSpans
24952
26064
  );
24953
26065
  const contextKey = extractInvocationContextKey(parentContext);
24954
- const span = startSpan({
24955
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
24956
- spanAttributes: {
24957
- type: "task" /* TASK */
24958
- },
24959
- ...runnerParentSpan ? {
24960
- parentSpanIds: {
24961
- spanId: runnerParentSpan.spanId,
24962
- rootSpanId: runnerParentSpan.rootSpanId
24963
- }
24964
- } : {}
24965
- });
26066
+ const span = startSpan(
26067
+ withSpanInstrumentationName(
26068
+ {
26069
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
26070
+ spanAttributes: {
26071
+ type: "task" /* TASK */
26072
+ },
26073
+ ...runnerParentSpan ? {
26074
+ parentSpanIds: {
26075
+ spanId: runnerParentSpan.spanId,
26076
+ rootSpanId: runnerParentSpan.rootSpanId
26077
+ }
26078
+ } : {}
26079
+ },
26080
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
26081
+ )
26082
+ );
24966
26083
  const startTime = getCurrentUnixTimestamp();
24967
26084
  try {
24968
26085
  const metadata = {
@@ -25055,22 +26172,27 @@ var GoogleADKPlugin = class extends BasePlugin {
25055
26172
  this.activeAgentSpans,
25056
26173
  this.activeRunnerSpans
25057
26174
  );
25058
- const createSpan = () => startSpan({
25059
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
25060
- spanAttributes: {
25061
- type: "tool" /* TOOL */
25062
- },
25063
- event: {
25064
- input: req.args,
25065
- metadata: {
25066
- provider: "google-adk",
25067
- ...toolName && { "google_adk.tool_name": toolName },
25068
- ...extractToolCallId(req) && {
25069
- "google_adk.tool_call_id": extractToolCallId(req)
26175
+ const createSpan = () => startSpan(
26176
+ withSpanInstrumentationName(
26177
+ {
26178
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
26179
+ spanAttributes: {
26180
+ type: "tool" /* TOOL */
26181
+ },
26182
+ event: {
26183
+ input: req.args,
26184
+ metadata: {
26185
+ provider: "google-adk",
26186
+ ...toolName && { "google_adk.tool_name": toolName },
26187
+ ...extractToolCallId(req) && {
26188
+ "google_adk.tool_call_id": extractToolCallId(req)
26189
+ }
26190
+ }
25070
26191
  }
25071
- }
25072
- }
25073
- });
26192
+ },
26193
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
26194
+ )
26195
+ );
25074
26196
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
25075
26197
  const startTime = getCurrentUnixTimestamp();
25076
26198
  states.set(event, { span, startTime });
@@ -25469,24 +26591,28 @@ function cleanMetrics4(metrics) {
25469
26591
  }
25470
26592
 
25471
26593
  // src/instrumentation/plugins/cohere-channels.ts
25472
- var cohereChannels = defineChannels("cohere-ai", {
25473
- chat: channel({
25474
- channelName: "chat",
25475
- kind: "async"
25476
- }),
25477
- chatStream: channel({
25478
- channelName: "chatStream",
25479
- kind: "async"
25480
- }),
25481
- embed: channel({
25482
- channelName: "embed",
25483
- kind: "async"
25484
- }),
25485
- rerank: channel({
25486
- channelName: "rerank",
25487
- kind: "async"
25488
- })
25489
- });
26594
+ var cohereChannels = defineChannels(
26595
+ "cohere-ai",
26596
+ {
26597
+ chat: channel({
26598
+ channelName: "chat",
26599
+ kind: "async"
26600
+ }),
26601
+ chatStream: channel({
26602
+ channelName: "chatStream",
26603
+ kind: "async"
26604
+ }),
26605
+ embed: channel({
26606
+ channelName: "embed",
26607
+ kind: "async"
26608
+ }),
26609
+ rerank: channel({
26610
+ channelName: "rerank",
26611
+ kind: "async"
26612
+ })
26613
+ },
26614
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
26615
+ );
25490
26616
 
25491
26617
  // src/instrumentation/plugins/cohere-plugin.ts
25492
26618
  var CoherePlugin = class extends BasePlugin {
@@ -26155,18 +27281,20 @@ function aggregateCohereChatStreamChunks(chunks) {
26155
27281
  }
26156
27282
 
26157
27283
  // src/instrumentation/plugins/groq-channels.ts
26158
- var groqChannels = defineChannels("groq-sdk", {
26159
- chatCompletionsCreate: channel({
26160
- channelName: "chat.completions.create",
26161
- kind: "async"
26162
- }),
26163
- embeddingsCreate: channel(
26164
- {
27284
+ var groqChannels = defineChannels(
27285
+ "groq-sdk",
27286
+ {
27287
+ chatCompletionsCreate: channel({
27288
+ channelName: "chat.completions.create",
27289
+ kind: "async"
27290
+ }),
27291
+ embeddingsCreate: channel({
26165
27292
  channelName: "embeddings.create",
26166
27293
  kind: "async"
26167
- }
26168
- )
26169
- });
27294
+ })
27295
+ },
27296
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
27297
+ );
26170
27298
 
26171
27299
  // src/instrumentation/plugins/groq-plugin.ts
26172
27300
  var GroqPlugin = class extends BasePlugin {
@@ -26269,15 +27397,27 @@ var clientSendChannel = channel({
26269
27397
  channelName: "client.send",
26270
27398
  kind: "async"
26271
27399
  });
26272
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
26273
- clientSend: clientSendChannel
26274
- });
26275
- var smithyCoreChannels = defineChannels("@smithy/core", {
26276
- clientSend: clientSendChannel
26277
- });
26278
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
26279
- clientSend: clientSendChannel
26280
- });
27400
+ var bedrockRuntimeChannels = defineChannels(
27401
+ "aws-bedrock-runtime",
27402
+ {
27403
+ clientSend: clientSendChannel
27404
+ },
27405
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27406
+ );
27407
+ var smithyCoreChannels = defineChannels(
27408
+ "@smithy/core",
27409
+ {
27410
+ clientSend: clientSendChannel
27411
+ },
27412
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27413
+ );
27414
+ var smithyClientChannels = defineChannels(
27415
+ "@smithy/smithy-client",
27416
+ {
27417
+ clientSend: clientSendChannel
27418
+ },
27419
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27420
+ );
26281
27421
 
26282
27422
  // src/instrumentation/plugins/bedrock-runtime-common.ts
26283
27423
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -26911,38 +28051,46 @@ function extractTextFromJsonLike(value) {
26911
28051
  }
26912
28052
 
26913
28053
  // src/instrumentation/plugins/genkit-channels.ts
26914
- var genkitChannels = defineChannels("@genkit-ai/ai", {
26915
- generate: channel({
26916
- channelName: "generate",
26917
- kind: "async"
26918
- }),
26919
- generateStream: channel({
26920
- channelName: "generateStream",
26921
- kind: "sync-stream"
26922
- }),
26923
- embed: channel({
26924
- channelName: "embed",
26925
- kind: "async"
26926
- }),
26927
- embedMany: channel({
26928
- channelName: "embedMany",
26929
- kind: "async"
26930
- }),
26931
- actionRun: channel({
26932
- channelName: "action.run",
26933
- kind: "async"
26934
- }),
26935
- actionStream: channel({
26936
- channelName: "action.stream",
26937
- kind: "sync-stream"
26938
- })
26939
- });
26940
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
26941
- actionSpan: channel({
26942
- channelName: "action.span",
26943
- kind: "async"
26944
- })
26945
- });
28054
+ var genkitChannels = defineChannels(
28055
+ "@genkit-ai/ai",
28056
+ {
28057
+ generate: channel({
28058
+ channelName: "generate",
28059
+ kind: "async"
28060
+ }),
28061
+ generateStream: channel({
28062
+ channelName: "generateStream",
28063
+ kind: "sync-stream"
28064
+ }),
28065
+ embed: channel({
28066
+ channelName: "embed",
28067
+ kind: "async"
28068
+ }),
28069
+ embedMany: channel({
28070
+ channelName: "embedMany",
28071
+ kind: "async"
28072
+ }),
28073
+ actionRun: channel({
28074
+ channelName: "action.run",
28075
+ kind: "async"
28076
+ }),
28077
+ actionStream: channel({
28078
+ channelName: "action.stream",
28079
+ kind: "sync-stream"
28080
+ })
28081
+ },
28082
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
28083
+ );
28084
+ var genkitCoreChannels = defineChannels(
28085
+ "@genkit-ai/core",
28086
+ {
28087
+ actionSpan: channel({
28088
+ channelName: "action.span",
28089
+ kind: "async"
28090
+ })
28091
+ },
28092
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
28093
+ );
26946
28094
 
26947
28095
  // src/instrumentation/plugins/genkit-plugin.ts
26948
28096
  var GenkitPlugin = class extends BasePlugin {
@@ -27127,12 +28275,17 @@ function startActionSpanState(args) {
27127
28275
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
27128
28276
  return void 0;
27129
28277
  }
27130
- const span = startSpan({
27131
- name: actionSpanName(args.metadata, args.runStepName),
27132
- spanAttributes: {
27133
- type: actionSpanType(args.metadata)
27134
- }
27135
- });
28278
+ const span = startSpan(
28279
+ withSpanInstrumentationName(
28280
+ {
28281
+ name: actionSpanName(args.metadata, args.runStepName),
28282
+ spanAttributes: {
28283
+ type: actionSpanType(args.metadata)
28284
+ }
28285
+ },
28286
+ INSTRUMENTATION_NAMES.GENKIT
28287
+ )
28288
+ );
27136
28289
  const startTime = getCurrentUnixTimestamp();
27137
28290
  span.log({
27138
28291
  input: args.input,
@@ -27493,20 +28646,24 @@ function stringValue(value) {
27493
28646
  }
27494
28647
 
27495
28648
  // src/instrumentation/plugins/github-copilot-channels.ts
27496
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
27497
- createSession: channel({
27498
- channelName: "client.createSession",
27499
- kind: "async"
27500
- }),
27501
- resumeSession: channel({
27502
- channelName: "client.resumeSession",
27503
- kind: "async"
27504
- }),
27505
- sendAndWait: channel({
27506
- channelName: "session.sendAndWait",
27507
- kind: "async"
27508
- })
27509
- });
28649
+ var gitHubCopilotChannels = defineChannels(
28650
+ "@github/copilot-sdk",
28651
+ {
28652
+ createSession: channel({
28653
+ channelName: "client.createSession",
28654
+ kind: "async"
28655
+ }),
28656
+ resumeSession: channel({
28657
+ channelName: "client.resumeSession",
28658
+ kind: "async"
28659
+ }),
28660
+ sendAndWait: channel({
28661
+ channelName: "session.sendAndWait",
28662
+ kind: "async"
28663
+ })
28664
+ },
28665
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
28666
+ );
27510
28667
 
27511
28668
  // src/instrumentation/plugins/github-copilot-plugin.ts
27512
28669
  var ROOT_AGENT_KEY = "__root__";
@@ -27597,11 +28754,16 @@ async function handleTurnStart(state, agentId) {
27597
28754
  return;
27598
28755
  }
27599
28756
  const parentId = await getParentIdForAgent(state, agentId);
27600
- const span = startSpan({
27601
- name: "Copilot Turn",
27602
- parent: parentId,
27603
- spanAttributes: { type: "task" /* TASK */ }
27604
- });
28757
+ const span = startSpan(
28758
+ withSpanInstrumentationName(
28759
+ {
28760
+ name: "Copilot Turn",
28761
+ parent: parentId,
28762
+ spanAttributes: { type: "task" /* TASK */ }
28763
+ },
28764
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28765
+ )
28766
+ );
27605
28767
  const pendingUserMessage = state.pendingUserMessages.get(key);
27606
28768
  if (pendingUserMessage) {
27607
28769
  span.log({ input: pendingUserMessage });
@@ -27632,11 +28794,16 @@ async function handleUsage(state, agentId, usage) {
27632
28794
  const parentId = turn ? await turn.id : await state.session.id;
27633
28795
  const content = state.currentMessageContent.get(key);
27634
28796
  const { metrics, metadata } = extractMetricsFromUsage(usage);
27635
- const llmSpan = startSpan({
27636
- name: "github.copilot.llm",
27637
- parent: parentId,
27638
- spanAttributes: { type: "llm" /* LLM */ }
27639
- });
28797
+ const llmSpan = startSpan(
28798
+ withSpanInstrumentationName(
28799
+ {
28800
+ name: "github.copilot.llm",
28801
+ parent: parentId,
28802
+ spanAttributes: { type: "llm" /* LLM */ }
28803
+ },
28804
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28805
+ )
28806
+ );
27640
28807
  llmSpan.log({
27641
28808
  output: content ?? void 0,
27642
28809
  metadata,
@@ -27662,11 +28829,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
27662
28829
  }
27663
28830
  const parentId = await getToolParentId(state, agentId);
27664
28831
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
27665
- const span = startSpan({
27666
- name: displayName,
27667
- parent: parentId,
27668
- spanAttributes: { type: "tool" /* TOOL */ }
27669
- });
28832
+ const span = startSpan(
28833
+ withSpanInstrumentationName(
28834
+ {
28835
+ name: displayName,
28836
+ parent: parentId,
28837
+ spanAttributes: { type: "tool" /* TOOL */ }
28838
+ },
28839
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28840
+ )
28841
+ );
27670
28842
  const metadata = {
27671
28843
  "gen_ai.tool.name": toolName,
27672
28844
  "gen_ai.tool.call.id": toolCallId
@@ -27702,11 +28874,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
27702
28874
  }
27703
28875
  const tool = state.activeTools.get(toolCallId);
27704
28876
  const parentId = tool ? await tool.id : await state.session.id;
27705
- const span = startSpan({
27706
- name: `Agent: ${agentDisplayName}`,
27707
- parent: parentId,
27708
- spanAttributes: { type: "task" /* TASK */ }
27709
- });
28877
+ const span = startSpan(
28878
+ withSpanInstrumentationName(
28879
+ {
28880
+ name: `Agent: ${agentDisplayName}`,
28881
+ parent: parentId,
28882
+ spanAttributes: { type: "task" /* TASK */ }
28883
+ },
28884
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28885
+ )
28886
+ );
27710
28887
  span.log({
27711
28888
  metadata: {
27712
28889
  "github_copilot.agent_name": agentDisplayName,
@@ -27932,10 +29109,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
27932
29109
  if (!config || typeof config !== "object") {
27933
29110
  return;
27934
29111
  }
27935
- const sessionSpan = startSpan({
27936
- name: "Copilot Session",
27937
- spanAttributes: { type: "task" /* TASK */ }
27938
- });
29112
+ const sessionSpan = startSpan(
29113
+ withSpanInstrumentationName(
29114
+ {
29115
+ name: "Copilot Session",
29116
+ spanAttributes: { type: "task" /* TASK */ }
29117
+ },
29118
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
29119
+ )
29120
+ );
27939
29121
  const metadata = {};
27940
29122
  if (config.model) {
27941
29123
  metadata["github_copilot.model"] = config.model;
@@ -28023,12 +29205,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
28023
29205
  };
28024
29206
 
28025
29207
  // src/instrumentation/plugins/flue-channels.ts
28026
- var flueChannels = defineChannels("@flue/runtime", {
28027
- createContext: channel({
28028
- channelName: "createFlueContext",
28029
- kind: "sync-stream"
28030
- })
28031
- });
29208
+ var flueChannels = defineChannels(
29209
+ "@flue/runtime",
29210
+ {
29211
+ createContext: channel({
29212
+ channelName: "createFlueContext",
29213
+ kind: "sync-stream"
29214
+ })
29215
+ },
29216
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
29217
+ );
28032
29218
 
28033
29219
  // src/instrumentation/plugins/flue-plugin.ts
28034
29220
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -28371,15 +29557,20 @@ var FlueObserveBridge = class {
28371
29557
  safeLog3(existing.span, { input, metadata });
28372
29558
  return;
28373
29559
  }
28374
- const span = startSpan({
28375
- name: `workflow:${workflowName}`,
28376
- spanAttributes: { type: "task" /* TASK */ },
28377
- startTime: eventTime(event.startedAt ?? event.timestamp),
28378
- event: {
28379
- input,
28380
- metadata
28381
- }
28382
- });
29560
+ const span = startSpan(
29561
+ withSpanInstrumentationName(
29562
+ {
29563
+ name: `workflow:${workflowName}`,
29564
+ spanAttributes: { type: "task" /* TASK */ },
29565
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29566
+ event: {
29567
+ input,
29568
+ metadata
29569
+ }
29570
+ },
29571
+ INSTRUMENTATION_NAMES.FLUE
29572
+ )
29573
+ );
28383
29574
  this.runsById.set(event.runId, { metadata, span });
28384
29575
  }
28385
29576
  handleRunResume(event, ctx) {
@@ -28399,12 +29590,17 @@ var FlueObserveBridge = class {
28399
29590
  safeLog3(existing.span, { metadata });
28400
29591
  return;
28401
29592
  }
28402
- const span = startSpan({
28403
- name: `workflow:${workflowName}`,
28404
- spanAttributes: { type: "task" /* TASK */ },
28405
- startTime: eventTime(event.startedAt ?? event.timestamp),
28406
- event: { metadata }
28407
- });
29593
+ const span = startSpan(
29594
+ withSpanInstrumentationName(
29595
+ {
29596
+ name: `workflow:${workflowName}`,
29597
+ spanAttributes: { type: "task" /* TASK */ },
29598
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29599
+ event: { metadata }
29600
+ },
29601
+ INSTRUMENTATION_NAMES.FLUE
29602
+ )
29603
+ );
28408
29604
  this.runsById.set(event.runId, { metadata, span });
28409
29605
  }
28410
29606
  handleRunEnd(event) {
@@ -29043,7 +30239,14 @@ function stateMatchesRun(state, runId) {
29043
30239
  return state.metadata["flue.run_id"] === runId;
29044
30240
  }
29045
30241
  function startFlueSpan(parent, args) {
29046
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
30242
+ return parent ? withCurrent(
30243
+ parent,
30244
+ () => startSpan(
30245
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30246
+ )
30247
+ ) : startSpan(
30248
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30249
+ );
29047
30250
  }
29048
30251
  function runWithCurrentSpanStore(span, next) {
29049
30252
  const state = _internalGetGlobalState();
@@ -29133,9 +30336,13 @@ var BraintrustLangChainCallbackHandler = class {
29133
30336
  ...this.options.debug ? { runId, parentRunId } : {}
29134
30337
  }
29135
30338
  };
29136
- let span = parentSpan.startSpan(args);
30339
+ const spanArgs = withSpanInstrumentationName(
30340
+ args,
30341
+ INSTRUMENTATION_NAMES.LANGCHAIN
30342
+ );
30343
+ let span = parentSpan.startSpan(spanArgs);
29137
30344
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
29138
- span = initLogger().startSpan(args);
30345
+ span = initLogger().startSpan(spanArgs);
29139
30346
  }
29140
30347
  this.spans.set(runId, span);
29141
30348
  }
@@ -29348,6 +30555,16 @@ function cleanObject(obj) {
29348
30555
  })
29349
30556
  );
29350
30557
  }
30558
+ function normalizeTokenMetrics(obj) {
30559
+ const metrics = cleanObject(obj);
30560
+ if (metrics.total_tokens !== void 0) {
30561
+ metrics.tokens = metrics.total_tokens;
30562
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
30563
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
30564
+ }
30565
+ delete metrics.total_tokens;
30566
+ return metrics;
30567
+ }
29351
30568
  function walkGenerations(response) {
29352
30569
  const result = [];
29353
30570
  const generations = response.generations || [];
@@ -29394,7 +30611,7 @@ function getMetricsFromResponse(response) {
29394
30611
  continue;
29395
30612
  }
29396
30613
  const inputTokenDetails = usageMetadata.input_token_details;
29397
- return cleanObject({
30614
+ return normalizeTokenMetrics({
29398
30615
  total_tokens: usageMetadata.total_tokens,
29399
30616
  prompt_tokens: usageMetadata.input_tokens,
29400
30617
  completion_tokens: usageMetadata.output_tokens,
@@ -29404,7 +30621,7 @@ function getMetricsFromResponse(response) {
29404
30621
  }
29405
30622
  const llmOutput = response.llmOutput || {};
29406
30623
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
29407
- return cleanObject({
30624
+ return normalizeTokenMetrics({
29408
30625
  total_tokens: tokenUsage.totalTokens,
29409
30626
  prompt_tokens: tokenUsage.promptTokens,
29410
30627
  completion_tokens: tokenUsage.completionTokens
@@ -29422,16 +30639,20 @@ function isRecord(value) {
29422
30639
  }
29423
30640
 
29424
30641
  // src/instrumentation/plugins/langchain-channels.ts
29425
- var langChainChannels = defineChannels("@langchain/core", {
29426
- configure: channel({
29427
- channelName: "CallbackManager.configure",
29428
- kind: "sync-stream"
29429
- }),
29430
- configureSync: channel({
29431
- channelName: "CallbackManager._configureSync",
29432
- kind: "sync-stream"
29433
- })
29434
- });
30642
+ var langChainChannels = defineChannels(
30643
+ "@langchain/core",
30644
+ {
30645
+ configure: channel({
30646
+ channelName: "CallbackManager.configure",
30647
+ kind: "sync-stream"
30648
+ }),
30649
+ configureSync: channel({
30650
+ channelName: "CallbackManager._configureSync",
30651
+ kind: "sync-stream"
30652
+ })
30653
+ },
30654
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
30655
+ );
29435
30656
 
29436
30657
  // src/instrumentation/plugins/langchain-plugin.ts
29437
30658
  var LangChainPlugin = class extends BasePlugin {
@@ -29519,20 +30740,24 @@ function isBraintrustHandler(handler) {
29519
30740
  }
29520
30741
 
29521
30742
  // src/instrumentation/plugins/langsmith-channels.ts
29522
- var langSmithChannels = defineChannels("langsmith", {
29523
- createRun: channel({
29524
- channelName: "Client.createRun",
29525
- kind: "async"
29526
- }),
29527
- updateRun: channel({
29528
- channelName: "Client.updateRun",
29529
- kind: "async"
29530
- }),
29531
- batchIngestRuns: channel({
29532
- channelName: "Client.batchIngestRuns",
29533
- kind: "async"
29534
- })
29535
- });
30743
+ var langSmithChannels = defineChannels(
30744
+ "langsmith",
30745
+ {
30746
+ createRun: channel({
30747
+ channelName: "Client.createRun",
30748
+ kind: "async"
30749
+ }),
30750
+ updateRun: channel({
30751
+ channelName: "Client.updateRun",
30752
+ kind: "async"
30753
+ }),
30754
+ batchIngestRuns: channel({
30755
+ channelName: "Client.batchIngestRuns",
30756
+ kind: "async"
30757
+ })
30758
+ },
30759
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
30760
+ );
29536
30761
 
29537
30762
  // src/instrumentation/plugins/langsmith-plugin.ts
29538
30763
  var MAX_COMPLETED_RUNS = 1e4;
@@ -29696,19 +30921,24 @@ var LangSmithPlugin = class extends BasePlugin {
29696
30921
  const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
29697
30922
  const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
29698
30923
  const startTime = timestampSeconds(ownValue(run, "start_time"));
29699
- return startSpan({
29700
- name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
29701
- spanId: id,
29702
- parentSpanIds: {
29703
- parentSpanIds: parentId ? [parentId] : [],
29704
- rootSpanId: traceId
29705
- },
29706
- spanAttributes: {
29707
- type: mapRunType(ownValue(run, "run_type"))
29708
- },
29709
- ...startTime === void 0 ? {} : { startTime },
29710
- event: { id }
29711
- });
30924
+ return startSpan(
30925
+ withSpanInstrumentationName(
30926
+ {
30927
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
30928
+ spanId: id,
30929
+ parentSpanIds: {
30930
+ parentSpanIds: parentId ? [parentId] : [],
30931
+ rootSpanId: traceId
30932
+ },
30933
+ spanAttributes: {
30934
+ type: mapRunType(ownValue(run, "run_type"))
30935
+ },
30936
+ ...startTime === void 0 ? {} : { startTime },
30937
+ event: { id }
30938
+ },
30939
+ INSTRUMENTATION_NAMES.LANGSMITH
30940
+ )
30941
+ );
29712
30942
  }
29713
30943
  logRun(span, run, previous, includeOutput) {
29714
30944
  const inputs = preferOwnValue(run, previous, "inputs");
@@ -29966,7 +31196,8 @@ var piCodingAgentChannels = defineChannels(
29966
31196
  channelName: "AgentSession.prompt",
29967
31197
  kind: "async"
29968
31198
  })
29969
- }
31199
+ },
31200
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
29970
31201
  );
29971
31202
 
29972
31203
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -30050,14 +31281,19 @@ function startPiPromptRun(event, onFinalize) {
30050
31281
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
30051
31282
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
30052
31283
  };
30053
- const span = startSpan({
30054
- event: {
30055
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
30056
- metadata
30057
- },
30058
- name: "AgentSession.prompt",
30059
- spanAttributes: { type: "task" /* TASK */ }
30060
- });
31284
+ const span = startSpan(
31285
+ withSpanInstrumentationName(
31286
+ {
31287
+ event: {
31288
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
31289
+ metadata
31290
+ },
31291
+ name: "AgentSession.prompt",
31292
+ spanAttributes: { type: "task" /* TASK */ }
31293
+ },
31294
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31295
+ )
31296
+ );
30061
31297
  const streamPatchState = installPiStreamPatch(agent);
30062
31298
  const options = event.arguments[1];
30063
31299
  const promptText = event.arguments[0];
@@ -30227,15 +31463,20 @@ async function startPiLlmSpan(state, model, context, options) {
30227
31463
  ...extractToolMetadata(context.tools),
30228
31464
  "pi_coding_agent.operation": "agent.streamFn"
30229
31465
  };
30230
- const span = startSpan({
30231
- event: {
30232
- input: processInputAttachments(normalizePiContextInput(context)),
30233
- metadata
30234
- },
30235
- name: getLlmSpanName(model),
30236
- parent: await state.span.export(),
30237
- spanAttributes: { type: "llm" /* LLM */ }
30238
- });
31466
+ const span = startSpan(
31467
+ withSpanInstrumentationName(
31468
+ {
31469
+ event: {
31470
+ input: processInputAttachments(normalizePiContextInput(context)),
31471
+ metadata
31472
+ },
31473
+ name: getLlmSpanName(model),
31474
+ parent: await state.span.export(),
31475
+ spanAttributes: { type: "llm" /* LLM */ }
31476
+ },
31477
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31478
+ )
31479
+ );
30239
31480
  const llmState = {
30240
31481
  finalized: false,
30241
31482
  metadata,
@@ -30403,15 +31644,20 @@ async function startPiToolSpan(state, event) {
30403
31644
  "pi_coding_agent.tool.name": event.toolName
30404
31645
  };
30405
31646
  try {
30406
- const span = startSpan({
30407
- event: {
30408
- input: event.args,
30409
- metadata
30410
- },
30411
- name: event.toolName || "tool",
30412
- parent: await state.span.export(),
30413
- spanAttributes: { type: "tool" /* TOOL */ }
30414
- });
31647
+ const span = startSpan(
31648
+ withSpanInstrumentationName(
31649
+ {
31650
+ event: {
31651
+ input: event.args,
31652
+ metadata
31653
+ },
31654
+ name: event.toolName || "tool",
31655
+ parent: await state.span.export(),
31656
+ spanAttributes: { type: "tool" /* TOOL */ }
31657
+ },
31658
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31659
+ )
31660
+ );
30415
31661
  state.activeToolSpans.set(event.toolCallId, {
30416
31662
  restoreAutoInstrumentation,
30417
31663
  span
@@ -30843,20 +32089,24 @@ function logInstrumentationError4(context, error) {
30843
32089
  }
30844
32090
 
30845
32091
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
30846
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
30847
- agentStream: channel({
30848
- channelName: "Agent.stream",
30849
- kind: "sync-stream"
30850
- }),
30851
- graphStream: channel({
30852
- channelName: "Graph.stream",
30853
- kind: "sync-stream"
30854
- }),
30855
- swarmStream: channel({
30856
- channelName: "Swarm.stream",
30857
- kind: "sync-stream"
30858
- })
30859
- });
32092
+ var strandsAgentSDKChannels = defineChannels(
32093
+ "@strands-agents/sdk",
32094
+ {
32095
+ agentStream: channel({
32096
+ channelName: "Agent.stream",
32097
+ kind: "sync-stream"
32098
+ }),
32099
+ graphStream: channel({
32100
+ channelName: "Graph.stream",
32101
+ kind: "sync-stream"
32102
+ }),
32103
+ swarmStream: channel({
32104
+ channelName: "Swarm.stream",
32105
+ kind: "sync-stream"
32106
+ })
32107
+ },
32108
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
32109
+ );
30860
32110
 
30861
32111
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
30862
32112
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -31011,22 +32261,32 @@ function startAgentStream(event, activeChildParents) {
31011
32261
  );
31012
32262
  const span = parentSpan ? withCurrent(
31013
32263
  parentSpan,
31014
- () => startSpan({
31015
- event: {
31016
- input,
31017
- metadata
32264
+ () => startSpan(
32265
+ withSpanInstrumentationName(
32266
+ {
32267
+ event: {
32268
+ input,
32269
+ metadata
32270
+ },
32271
+ name: formatAgentSpanName(agent),
32272
+ spanAttributes: { type: "task" /* TASK */ }
32273
+ },
32274
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32275
+ )
32276
+ )
32277
+ ) : startSpan(
32278
+ withSpanInstrumentationName(
32279
+ {
32280
+ event: {
32281
+ input,
32282
+ metadata
32283
+ },
32284
+ name: formatAgentSpanName(agent),
32285
+ spanAttributes: { type: "task" /* TASK */ }
31018
32286
  },
31019
- name: formatAgentSpanName(agent),
31020
- spanAttributes: { type: "task" /* TASK */ }
31021
- })
31022
- ) : startSpan({
31023
- event: {
31024
- input,
31025
- metadata
31026
- },
31027
- name: formatAgentSpanName(agent),
31028
- spanAttributes: { type: "task" /* TASK */ }
31029
- });
32287
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32288
+ )
32289
+ );
31030
32290
  return {
31031
32291
  activeTools: /* @__PURE__ */ new Map(),
31032
32292
  attachmentCache,
@@ -31048,22 +32308,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
31048
32308
  const input = processStrandsInputAttachments(event.arguments[0]);
31049
32309
  const span = parentSpan ? withCurrent(
31050
32310
  parentSpan,
31051
- () => startSpan({
31052
- event: {
31053
- input,
31054
- metadata
32311
+ () => startSpan(
32312
+ withSpanInstrumentationName(
32313
+ {
32314
+ event: {
32315
+ input,
32316
+ metadata
32317
+ },
32318
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32319
+ spanAttributes: { type: "task" /* TASK */ }
32320
+ },
32321
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32322
+ )
32323
+ )
32324
+ ) : startSpan(
32325
+ withSpanInstrumentationName(
32326
+ {
32327
+ event: {
32328
+ input,
32329
+ metadata
32330
+ },
32331
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32332
+ spanAttributes: { type: "task" /* TASK */ }
31055
32333
  },
31056
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
31057
- spanAttributes: { type: "task" /* TASK */ }
31058
- })
31059
- ) : startSpan({
31060
- event: {
31061
- input,
31062
- metadata
31063
- },
31064
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
31065
- spanAttributes: { type: "task" /* TASK */ }
31066
- });
32334
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32335
+ )
32336
+ );
31067
32337
  return {
31068
32338
  activeNodes: /* @__PURE__ */ new Map(),
31069
32339
  finalized: false,
@@ -31165,17 +32435,22 @@ function startModelSpan(state, event) {
31165
32435
  };
31166
32436
  const span = withCurrent(
31167
32437
  state.span,
31168
- () => startSpan({
31169
- event: {
31170
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
31171
- event.agent.messages,
31172
- state.attachmentCache
31173
- ) : void 0,
31174
- metadata
31175
- },
31176
- name: formatModelSpanName(model),
31177
- spanAttributes: { type: "llm" /* LLM */ }
31178
- })
32438
+ () => startSpan(
32439
+ withSpanInstrumentationName(
32440
+ {
32441
+ event: {
32442
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
32443
+ event.agent.messages,
32444
+ state.attachmentCache
32445
+ ) : void 0,
32446
+ metadata
32447
+ },
32448
+ name: formatModelSpanName(model),
32449
+ spanAttributes: { type: "llm" /* LLM */ }
32450
+ },
32451
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32452
+ )
32453
+ )
31179
32454
  );
31180
32455
  state.activeModel = {
31181
32456
  metadata,
@@ -31227,20 +32502,25 @@ function startToolSpan2(state, event) {
31227
32502
  const name = extractToolName3(toolUse, event.tool);
31228
32503
  const span = withCurrent(
31229
32504
  state.span,
31230
- () => startSpan({
31231
- event: {
31232
- input: toolUse?.input,
31233
- metadata: {
31234
- "gen_ai.tool.call.id": toolUse?.toolUseId,
31235
- "gen_ai.tool.name": name,
31236
- "strands.operation": "tool.call",
31237
- "strands.tool.name": name,
31238
- provider: "strands"
31239
- }
31240
- },
31241
- name: `tool: ${name}`,
31242
- spanAttributes: { type: "tool" /* TOOL */ }
31243
- })
32505
+ () => startSpan(
32506
+ withSpanInstrumentationName(
32507
+ {
32508
+ event: {
32509
+ input: toolUse?.input,
32510
+ metadata: {
32511
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
32512
+ "gen_ai.tool.name": name,
32513
+ "strands.operation": "tool.call",
32514
+ "strands.tool.name": name,
32515
+ provider: "strands"
32516
+ }
32517
+ },
32518
+ name: `tool: ${name}`,
32519
+ spanAttributes: { type: "tool" /* TOOL */ }
32520
+ },
32521
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32522
+ )
32523
+ )
31244
32524
  );
31245
32525
  state.activeTools.set(key, {
31246
32526
  span,
@@ -31311,11 +32591,16 @@ function startNodeSpan(state, event, activeChildParents) {
31311
32591
  };
31312
32592
  const span = withCurrent(
31313
32593
  state.span,
31314
- () => startSpan({
31315
- event: { metadata },
31316
- name: `node: ${nodeId}`,
31317
- spanAttributes: { type: "task" /* TASK */ }
31318
- })
32594
+ () => startSpan(
32595
+ withSpanInstrumentationName(
32596
+ {
32597
+ event: { metadata },
32598
+ name: `node: ${nodeId}`,
32599
+ spanAttributes: { type: "task" /* TASK */ }
32600
+ },
32601
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32602
+ )
32603
+ )
31319
32604
  );
31320
32605
  const nodeState = {
31321
32606
  ...child ? { child } : {},
@@ -33008,6 +34293,26 @@ function wrapAISDK(aiSDK, options = {}) {
33008
34293
  }
33009
34294
  });
33010
34295
  }
34296
+ function isHarnessAgentInstance(instance) {
34297
+ try {
34298
+ const visited = /* @__PURE__ */ new Set();
34299
+ let prototype = Object.getPrototypeOf(instance);
34300
+ while (prototype !== null && !visited.has(prototype)) {
34301
+ visited.add(prototype);
34302
+ const constructor = Object.getOwnPropertyDescriptor(
34303
+ prototype,
34304
+ "constructor"
34305
+ )?.value;
34306
+ const constructorName = typeof constructor === "function" ? Object.getOwnPropertyDescriptor(constructor, "name")?.value : void 0;
34307
+ if (constructorName === "HarnessAgent") {
34308
+ return true;
34309
+ }
34310
+ prototype = Object.getPrototypeOf(prototype);
34311
+ }
34312
+ } catch {
34313
+ }
34314
+ return false;
34315
+ }
33011
34316
  var wrapAgentClass = (AgentClass, options = {}) => {
33012
34317
  const typedAgentClass = AgentClass;
33013
34318
  return new Proxy(typedAgentClass, {
@@ -33017,9 +34322,52 @@ var wrapAgentClass = (AgentClass, options = {}) => {
33017
34322
  args,
33018
34323
  newTarget
33019
34324
  );
34325
+ const harnessAgent = isHarnessAgentInstance(instance) ? instance : null;
33020
34326
  return new Proxy(instance, {
33021
34327
  get(instanceTarget, prop, instanceReceiver) {
33022
34328
  const original = Reflect.get(instanceTarget, prop, instanceTarget);
34329
+ if (harnessAgent && typeof original === "function") {
34330
+ switch (prop) {
34331
+ case "createSession":
34332
+ return wrapHarnessAgentCreateSession(
34333
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
34334
+ original,
34335
+ harnessAgent
34336
+ );
34337
+ case "generate":
34338
+ return wrapHarnessAgentGenerate(
34339
+ original,
34340
+ harnessAgent,
34341
+ harnessAgentChannels.generate,
34342
+ "HarnessAgent.generate",
34343
+ options
34344
+ );
34345
+ case "stream":
34346
+ return wrapHarnessAgentStream(
34347
+ original,
34348
+ harnessAgent,
34349
+ harnessAgentChannels.stream,
34350
+ "HarnessAgent.stream",
34351
+ options
34352
+ );
34353
+ case "continueGenerate":
34354
+ return wrapHarnessAgentGenerate(
34355
+ original,
34356
+ harnessAgent,
34357
+ harnessAgentChannels.continueGenerate,
34358
+ "HarnessAgent.continueGenerate",
34359
+ options
34360
+ );
34361
+ case "continueStream":
34362
+ return wrapHarnessAgentStream(
34363
+ original,
34364
+ harnessAgent,
34365
+ harnessAgentChannels.continueStream,
34366
+ "HarnessAgent.continueStream",
34367
+ options
34368
+ );
34369
+ }
34370
+ }
33023
34371
  if (prop === "generate" && typeof original === "function" && instanceTarget.constructor.name !== "WorkflowAgent") {
33024
34372
  return wrapAgentGenerate(original, instanceTarget, options);
33025
34373
  }
@@ -33035,6 +34383,39 @@ var wrapAgentClass = (AgentClass, options = {}) => {
33035
34383
  }
33036
34384
  });
33037
34385
  };
34386
+ var wrapHarnessAgentCreateSession = (createSession, instance) => {
34387
+ const wrapper = function(params) {
34388
+ return harnessAgentChannels.createSession.tracePromise(
34389
+ () => params === void 0 ? createSession.call(instance) : createSession.call(instance, params),
34390
+ createAISDKChannelContext(params ?? {}, { self: instance })
34391
+ );
34392
+ };
34393
+ Object.defineProperty(wrapper, "name", {
34394
+ value: "HarnessAgent.createSession",
34395
+ writable: false
34396
+ });
34397
+ return wrapper;
34398
+ };
34399
+ var wrapHarnessAgentGenerate = (generate, instance, channel2, name, options) => makeGenerateTextWrapper(
34400
+ channel2,
34401
+ name,
34402
+ generate.bind(instance),
34403
+ {
34404
+ self: instance,
34405
+ spanType: "task" /* TASK */
34406
+ },
34407
+ options
34408
+ );
34409
+ var wrapHarnessAgentStream = (stream, instance, channel2, name, options) => makeStreamWrapper(
34410
+ channel2,
34411
+ name,
34412
+ stream.bind(instance),
34413
+ {
34414
+ self: instance,
34415
+ spanType: "task" /* TASK */
34416
+ },
34417
+ options
34418
+ );
33038
34419
  var wrapAgentGenerate = (generate, instance, options = {}) => {
33039
34420
  const defaultName = `${instance.constructor.name}.generate`;
33040
34421
  return async (params) => makeGenerateTextWrapper(
@@ -33261,6 +34642,7 @@ var BraintrustLanguageModelWrapper = class {
33261
34642
  this.supportsUrl = (url) => this.model.supportsUrl(url);
33262
34643
  }
33263
34644
  }
34645
+ model;
33264
34646
  supportsUrl;
33265
34647
  get specificationVersion() {
33266
34648
  return this.model.specificationVersion;
@@ -33283,12 +34665,17 @@ var BraintrustLanguageModelWrapper = class {
33283
34665
  // For the first cut, do not support custom span_info arguments. We can
33284
34666
  // propagate those via async local storage
33285
34667
  async doGenerate(options) {
33286
- const span = startSpan({
33287
- name: "Chat Completion",
33288
- spanAttributes: {
33289
- type: "llm"
33290
- }
33291
- });
34668
+ const span = startSpan(
34669
+ withSpanInstrumentationName(
34670
+ {
34671
+ name: "Chat Completion",
34672
+ spanAttributes: {
34673
+ type: "llm"
34674
+ }
34675
+ },
34676
+ INSTRUMENTATION_NAMES.AI_SDK
34677
+ )
34678
+ );
33292
34679
  const { prompt, mode, ...rest } = options;
33293
34680
  const startTime = getCurrentUnixTimestamp();
33294
34681
  try {
@@ -33319,12 +34706,17 @@ var BraintrustLanguageModelWrapper = class {
33319
34706
  async doStream(options) {
33320
34707
  const { prompt, mode, ...rest } = options;
33321
34708
  const startTime = getCurrentUnixTimestamp();
33322
- const span = startSpan({
33323
- name: "Chat Completion",
33324
- spanAttributes: {
33325
- type: "llm"
33326
- }
33327
- });
34709
+ const span = startSpan(
34710
+ withSpanInstrumentationName(
34711
+ {
34712
+ name: "Chat Completion",
34713
+ spanAttributes: {
34714
+ type: "llm"
34715
+ }
34716
+ },
34717
+ INSTRUMENTATION_NAMES.AI_SDK
34718
+ )
34719
+ );
33328
34720
  span.log({
33329
34721
  input: postProcessPrompt(prompt),
33330
34722
  metadata: {
@@ -33679,7 +35071,9 @@ function BraintrustMiddleware(config = {}) {
33679
35071
  }
33680
35072
  }
33681
35073
  };
33682
- const span = startSpan(spanArgs);
35074
+ const span = startSpan(
35075
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
35076
+ );
33683
35077
  try {
33684
35078
  const result = await doGenerate();
33685
35079
  const metadata = {};
@@ -33741,7 +35135,9 @@ function BraintrustMiddleware(config = {}) {
33741
35135
  }
33742
35136
  }
33743
35137
  };
33744
- const span = startSpan(spanArgs);
35138
+ const span = startSpan(
35139
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
35140
+ );
33745
35141
  try {
33746
35142
  const { stream, ...rest } = await doStream();
33747
35143
  const textChunks = [];
@@ -33843,6 +35239,7 @@ function BraintrustMiddleware(config = {}) {
33843
35239
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
33844
35240
  var MAX_EVE_CACHE_ENTRIES = 1e4;
33845
35241
  var MAX_STORED_LLM_INPUTS = 100;
35242
+ var MAX_STORED_REASONING_BLOCKS = 100;
33846
35243
  var MAX_STORED_SPAN_REFERENCES = 1e4;
33847
35244
  var MAX_STORED_STEP_STARTS = 1e4;
33848
35245
  function braintrustEveHook(options) {
@@ -33881,6 +35278,7 @@ var ResumedEveSpan = class {
33881
35278
  this.reference = reference;
33882
35279
  this.endTime = reference.endTime;
33883
35280
  }
35281
+ reference;
33884
35282
  endTime;
33885
35283
  get rootSpanId() {
33886
35284
  return this.reference.rootSpanId;
@@ -33913,6 +35311,7 @@ var EveBridge = class {
33913
35311
  constructor(state) {
33914
35312
  this.state = state;
33915
35313
  }
35314
+ state;
33916
35315
  eventQueuesBySession = /* @__PURE__ */ new Map();
33917
35316
  completedToolKeys = new LRUCache({
33918
35317
  max: MAX_EVE_CACHE_ENTRIES
@@ -33945,7 +35344,12 @@ var EveBridge = class {
33945
35344
  };
33946
35345
  const span = withCurrent(
33947
35346
  NOOP_SPAN,
33948
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
35347
+ () => _internalStartSpanWithInitialMerge(
35348
+ withSpanInstrumentationName(
35349
+ { ...args, startTime },
35350
+ INSTRUMENTATION_NAMES.EVE
35351
+ )
35352
+ )
33949
35353
  );
33950
35354
  if (typeof rowId !== "string") {
33951
35355
  return span;
@@ -34091,6 +35495,9 @@ var EveBridge = class {
34091
35495
  case "step.started":
34092
35496
  await this.handleStepStarted(event, ctx, hookMetadata);
34093
35497
  return true;
35498
+ case "reasoning.completed":
35499
+ this.handleReasoningCompleted(event, ctx);
35500
+ return true;
34094
35501
  case "message.completed":
34095
35502
  this.handleMessageCompleted(event, ctx);
34096
35503
  return true;
@@ -34182,6 +35589,7 @@ var EveBridge = class {
34182
35589
  key,
34183
35590
  metadata,
34184
35591
  metrics: {},
35592
+ sessionId,
34185
35593
  span,
34186
35594
  stepsByIndex: /* @__PURE__ */ new Map(),
34187
35595
  turnId: event.data.turnId
@@ -34212,6 +35620,12 @@ var EveBridge = class {
34212
35620
  const endTime = eventTime2(event);
34213
35621
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
34214
35622
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35623
+ clearStoredEveReasoning(
35624
+ this.state,
35625
+ sessionId,
35626
+ event.data.turnId,
35627
+ event.data.stepIndex
35628
+ );
34215
35629
  }
34216
35630
  const stepOrdinal = this.stepOrdinal(event);
34217
35631
  const metadata = { ...turn.metadata };
@@ -34221,6 +35635,13 @@ var EveBridge = class {
34221
35635
  event.data.turnId,
34222
35636
  event.data.stepIndex
34223
35637
  );
35638
+ const reasoning = readStoredEveReasoning(
35639
+ this.state,
35640
+ sessionId,
35641
+ event.data.turnId,
35642
+ event.data.stepIndex
35643
+ );
35644
+ const output = mergeEveReasoning(void 0, reasoning);
34224
35645
  const { rowId: eventId, spanId } = await generateEveIds(
34225
35646
  "step",
34226
35647
  sessionId,
@@ -34238,32 +35659,52 @@ var EveBridge = class {
34238
35659
  spanId,
34239
35660
  startTime: eventTime2(event)
34240
35661
  });
34241
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
35662
+ span.log({
35663
+ ...input !== void 0 ? { input } : {},
35664
+ metadata
35665
+ });
34242
35666
  turn.stepsByIndex.set(event.data.stepIndex, {
34243
35667
  ...input !== void 0 ? { input } : {},
34244
35668
  metadata,
34245
35669
  metrics: {},
35670
+ ...output !== void 0 ? { output } : {},
35671
+ reasoning,
34246
35672
  span
34247
35673
  });
34248
35674
  }
35675
+ handleReasoningCompleted(event, ctx) {
35676
+ const sessionId = sessionIdFromContext(ctx);
35677
+ if (!sessionId) {
35678
+ return;
35679
+ }
35680
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
35681
+ const step = this.stepForEvent(event, ctx);
35682
+ if (step) {
35683
+ step.reasoning = reasoning;
35684
+ step.output = mergeEveReasoning(step.output, reasoning);
35685
+ }
35686
+ }
34249
35687
  handleMessageCompleted(event, ctx) {
34250
35688
  const step = this.stepForEvent(event, ctx);
34251
35689
  if (!step) {
34252
35690
  return;
34253
35691
  }
34254
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
35692
+ const existingMessage = eveOutputMessage(step.output);
34255
35693
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
34256
- step.output = [
34257
- {
34258
- finish_reason: normalizedFinishReason(event.data.finishReason),
34259
- index: 0,
34260
- message: {
34261
- content: event.data.message,
34262
- role: "assistant",
34263
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35694
+ step.output = mergeEveReasoning(
35695
+ [
35696
+ {
35697
+ finish_reason: normalizedFinishReason(event.data.finishReason),
35698
+ index: 0,
35699
+ message: {
35700
+ content: event.data.message,
35701
+ role: "assistant",
35702
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35703
+ }
34264
35704
  }
34265
- }
34266
- ];
35705
+ ],
35706
+ step.reasoning
35707
+ );
34267
35708
  const turn = this.turnForEvent(event, ctx);
34268
35709
  if (turn && event.data.finishReason !== "tool-calls") {
34269
35710
  turn.output = event.data.message;
@@ -34272,16 +35713,19 @@ var EveBridge = class {
34272
35713
  handleResultCompleted(event, ctx) {
34273
35714
  const step = this.stepForEvent(event, ctx);
34274
35715
  if (step) {
34275
- step.output = [
34276
- {
34277
- finish_reason: "stop",
34278
- index: 0,
34279
- message: {
34280
- content: event.data.result,
34281
- role: "assistant"
35716
+ step.output = mergeEveReasoning(
35717
+ [
35718
+ {
35719
+ finish_reason: "stop",
35720
+ index: 0,
35721
+ message: {
35722
+ content: event.data.result,
35723
+ role: "assistant"
35724
+ }
34282
35725
  }
34283
- }
34284
- ];
35726
+ ],
35727
+ step.reasoning
35728
+ );
34285
35729
  }
34286
35730
  const turn = this.turnForEvent(event, ctx);
34287
35731
  if (turn) {
@@ -34331,18 +35775,20 @@ var EveBridge = class {
34331
35775
  type: "function"
34332
35776
  });
34333
35777
  }
34334
- step.output = [
34335
- {
34336
- finish_reason: "tool_calls",
34337
- index: 0,
34338
- message: {
34339
- content: null,
34340
- role: "assistant",
34341
- tool_calls: [...toolCallsById.values()]
35778
+ step.output = mergeEveReasoning(
35779
+ [
35780
+ {
35781
+ finish_reason: "tool_calls",
35782
+ index: 0,
35783
+ message: {
35784
+ content: null,
35785
+ role: "assistant",
35786
+ tool_calls: [...toolCallsById.values()]
35787
+ }
34342
35788
  }
34343
- }
34344
- ];
34345
- step.span.log({ metadata: step.metadata, output: step.output });
35789
+ ],
35790
+ step.reasoning
35791
+ );
34346
35792
  }
34347
35793
  async handleActionResult(event, ctx, hookMetadata) {
34348
35794
  if (isToolResult(event.data.result)) {
@@ -34520,6 +35966,7 @@ var EveBridge = class {
34520
35966
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
34521
35967
  };
34522
35968
  step.metrics = { ...step.metrics, ...metrics };
35969
+ const sessionId = sessionIdFromContext(ctx);
34523
35970
  if (Array.isArray(step.output) && isObject(step.output[0])) {
34524
35971
  const finishReason = step.output[0].finish_reason;
34525
35972
  if (typeof finishReason !== "string") {
@@ -34544,6 +35991,14 @@ var EveBridge = class {
34544
35991
  turn.stepsByIndex.delete(event.data.stepIndex);
34545
35992
  }
34546
35993
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35994
+ if (sessionId) {
35995
+ clearStoredEveReasoning(
35996
+ this.state,
35997
+ sessionId,
35998
+ event.data.turnId,
35999
+ event.data.stepIndex
36000
+ );
36001
+ }
34547
36002
  }
34548
36003
  handleStepFailed(event, ctx) {
34549
36004
  const step = this.stepForEvent(event, ctx);
@@ -34561,6 +36016,15 @@ var EveBridge = class {
34561
36016
  const turn = this.turnForEvent(event, ctx);
34562
36017
  turn?.stepsByIndex.delete(event.data.stepIndex);
34563
36018
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
36019
+ const sessionId = sessionIdFromContext(ctx);
36020
+ if (sessionId) {
36021
+ clearStoredEveReasoning(
36022
+ this.state,
36023
+ sessionId,
36024
+ event.data.turnId,
36025
+ event.data.stepIndex
36026
+ );
36027
+ }
34564
36028
  }
34565
36029
  handleTurnCompleted(event, ctx) {
34566
36030
  const turn = this.turnForEvent(event, ctx);
@@ -34658,6 +36122,7 @@ var EveBridge = class {
34658
36122
  key,
34659
36123
  metadata,
34660
36124
  metrics: {},
36125
+ sessionId,
34661
36126
  span,
34662
36127
  stepsByIndex: /* @__PURE__ */ new Map(),
34663
36128
  turnId: event.data.turnId
@@ -34920,6 +36385,9 @@ var EveBridge = class {
34920
36385
  const normalized = normalizeEveTraceState(current);
34921
36386
  return {
34922
36387
  ...normalized,
36388
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36389
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
36390
+ ),
34923
36391
  stepStarts: normalized.stepStarts.filter(
34924
36392
  (entry) => entry.turnId !== turn.turnId
34925
36393
  )
@@ -34959,6 +36427,7 @@ function emptyEveTraceState() {
34959
36427
  return {
34960
36428
  llmInputs: [],
34961
36429
  metadata: {},
36430
+ reasoningBlocks: [],
34962
36431
  spanReferences: [],
34963
36432
  stepStarts: []
34964
36433
  };
@@ -35009,6 +36478,21 @@ function normalizeEveTraceState(state) {
35009
36478
  const input = entry["input"];
35010
36479
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
35011
36480
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
36481
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
36482
+ if (!isObject(entry)) {
36483
+ return [];
36484
+ }
36485
+ const content = entry["content"];
36486
+ const eventAt = entry["eventAt"];
36487
+ const key = entry["key"];
36488
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
36489
+ {
36490
+ content,
36491
+ ...typeof eventAt === "string" ? { eventAt } : {},
36492
+ key
36493
+ }
36494
+ ] : [];
36495
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
35012
36496
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
35013
36497
  if (!isObject(entry)) {
35014
36498
  return [];
@@ -35019,7 +36503,13 @@ function normalizeEveTraceState(state) {
35019
36503
  const turnId = entry["turnId"];
35020
36504
  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 }] : [];
35021
36505
  }).slice(-MAX_STORED_STEP_STARTS) : [];
35022
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
36506
+ return {
36507
+ llmInputs,
36508
+ metadata: { ...metadata },
36509
+ reasoningBlocks,
36510
+ spanReferences,
36511
+ stepStarts
36512
+ };
35023
36513
  }
35024
36514
  function readEveTraceState(state) {
35025
36515
  try {
@@ -35028,23 +36518,85 @@ function readEveTraceState(state) {
35028
36518
  return emptyEveTraceState();
35029
36519
  }
35030
36520
  }
35031
- function captureEveModelInput(state, input) {
35032
- if (!isObject(input)) {
35033
- return;
35034
- }
35035
- const session = input["session"];
35036
- const turn = input["turn"];
35037
- const step = input["step"];
35038
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
35039
- return;
35040
- }
35041
- const sessionId = session["id"];
35042
- const turnId = turn["id"];
35043
- const stepIndex = step["index"];
35044
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
35045
- return;
36521
+ function storeEveReasoning(state, sessionId, event) {
36522
+ const eventAt = event.meta?.at;
36523
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
36524
+ let stored = [];
36525
+ state.update((current) => {
36526
+ const normalized = normalizeEveTraceState(current);
36527
+ const alreadyStored = normalized.reasoningBlocks.some(
36528
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
36529
+ );
36530
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
36531
+ ...normalized.reasoningBlocks,
36532
+ {
36533
+ content: event.data.reasoning,
36534
+ ...eventAt ? { eventAt } : {},
36535
+ key
36536
+ }
36537
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
36538
+ stored = reasoningBlocks.flatMap(
36539
+ (entry) => entry.key === key ? [
36540
+ {
36541
+ content: entry.content,
36542
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36543
+ }
36544
+ ] : []
36545
+ );
36546
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
36547
+ });
36548
+ return stored;
36549
+ }
36550
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36551
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36552
+ return readEveTraceState(state).reasoningBlocks.flatMap(
36553
+ (entry) => entry.key === key ? [
36554
+ {
36555
+ content: entry.content,
36556
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36557
+ }
36558
+ ] : []
36559
+ );
36560
+ }
36561
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36562
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36563
+ state.update((current) => {
36564
+ const normalized = normalizeEveTraceState(current);
36565
+ return {
36566
+ ...normalized,
36567
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36568
+ (entry) => entry.key !== key
36569
+ )
36570
+ };
36571
+ });
36572
+ }
36573
+ function eveOutputMessage(output) {
36574
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
36575
+ }
36576
+ function mergeEveReasoning(output, reasoning) {
36577
+ if (reasoning.length === 0) {
36578
+ return output;
35046
36579
  }
35047
- const captured = capturedModelInput(input["modelInput"]);
36580
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
36581
+ const message = isObject(choice["message"]) ? choice["message"] : {};
36582
+ return [
36583
+ {
36584
+ ...choice,
36585
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
36586
+ message: {
36587
+ ...message,
36588
+ content: "content" in message ? message["content"] : null,
36589
+ reasoning: reasoning.map((block) => ({ content: block.content })),
36590
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
36591
+ }
36592
+ }
36593
+ ];
36594
+ }
36595
+ function captureEveModelInput(state, input) {
36596
+ const sessionId = input.session.id;
36597
+ const turnId = input.turn.id;
36598
+ const stepIndex = input.step.index;
36599
+ const captured = capturedModelInput(input.modelInput);
35048
36600
  if (!captured) {
35049
36601
  return;
35050
36602
  }
@@ -35085,18 +36637,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
35085
36637
  }
35086
36638
  }
35087
36639
  function capturedModelInput(modelInput) {
35088
- if (!isObject(modelInput)) {
35089
- return void 0;
35090
- }
35091
- const messages = modelInput["messages"];
35092
- if (!Array.isArray(messages)) {
35093
- return void 0;
35094
- }
35095
- const instructions = modelInput["instructions"];
35096
- const value = [
35097
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
35098
- ...messages
35099
- ];
36640
+ const { instructions, messages } = modelInput;
36641
+ const value = [];
36642
+ if (typeof instructions === "string") {
36643
+ value.push({ content: instructions, role: "system" });
36644
+ } else if (instructions) {
36645
+ value.push(...instructions.map(capturedEveModelMessage));
36646
+ }
36647
+ value.push(...messages.map(capturedEveModelMessage));
35100
36648
  try {
35101
36649
  const cloned = JSON.parse(JSON.stringify(value));
35102
36650
  if (!Array.isArray(cloned)) {
@@ -35107,8 +36655,123 @@ function capturedModelInput(modelInput) {
35107
36655
  return void 0;
35108
36656
  }
35109
36657
  }
36658
+ function capturedEveModelMessage(message) {
36659
+ const { content, role } = message;
36660
+ if (typeof content === "string") {
36661
+ return { content, role };
36662
+ }
36663
+ return { content: content.map(capturedEveModelContentPart), role };
36664
+ }
36665
+ function capturedEveModelContentPart(part) {
36666
+ switch (part.type) {
36667
+ case "text":
36668
+ case "reasoning":
36669
+ return { text: part.text, type: part.type };
36670
+ case "image":
36671
+ return {
36672
+ image: part.image,
36673
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36674
+ type: "image"
36675
+ };
36676
+ case "file":
36677
+ case "reasoning-file":
36678
+ return {
36679
+ data: part.data,
36680
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
36681
+ mediaType: part.mediaType,
36682
+ type: part.type
36683
+ };
36684
+ case "custom":
36685
+ return {
36686
+ ..."kind" in part ? { kind: part.kind } : {},
36687
+ type: "custom"
36688
+ };
36689
+ case "tool-call":
36690
+ return {
36691
+ input: part.input,
36692
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36693
+ toolCallId: part.toolCallId,
36694
+ toolName: part.toolName,
36695
+ type: "tool-call"
36696
+ };
36697
+ case "tool-result": {
36698
+ const output = part.output;
36699
+ let capturedOutput;
36700
+ switch (output.type) {
36701
+ case "text":
36702
+ case "error-text":
36703
+ capturedOutput = { type: output.type, value: output.value };
36704
+ break;
36705
+ case "json":
36706
+ case "error-json":
36707
+ capturedOutput = { type: output.type, value: output.value };
36708
+ break;
36709
+ case "execution-denied":
36710
+ capturedOutput = {
36711
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
36712
+ type: "execution-denied"
36713
+ };
36714
+ break;
36715
+ case "content":
36716
+ capturedOutput = {
36717
+ type: "content",
36718
+ value: output.value.map(capturedEveModelContentPart)
36719
+ };
36720
+ break;
36721
+ }
36722
+ return {
36723
+ output: capturedOutput,
36724
+ toolCallId: part.toolCallId,
36725
+ toolName: part.toolName,
36726
+ type: "tool-result"
36727
+ };
36728
+ }
36729
+ case "tool-approval-request":
36730
+ return {
36731
+ approvalId: part.approvalId,
36732
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
36733
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
36734
+ toolCallId: part.toolCallId,
36735
+ type: "tool-approval-request"
36736
+ };
36737
+ case "tool-approval-response":
36738
+ return {
36739
+ approvalId: part.approvalId,
36740
+ approved: part.approved,
36741
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36742
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
36743
+ type: "tool-approval-response"
36744
+ };
36745
+ case "file-data":
36746
+ case "image-data":
36747
+ return {
36748
+ data: part.data,
36749
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
36750
+ mediaType: part.mediaType,
36751
+ type: part.type
36752
+ };
36753
+ case "file-url":
36754
+ case "image-url":
36755
+ return {
36756
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36757
+ type: part.type,
36758
+ url: part.url
36759
+ };
36760
+ case "file-id":
36761
+ case "image-file-id":
36762
+ return { fileId: part.fileId, type: part.type };
36763
+ case "file-reference":
36764
+ case "image-file-reference":
36765
+ return {
36766
+ providerReference: part.providerReference,
36767
+ type: part.type
36768
+ };
36769
+ }
36770
+ }
35110
36771
  function isCapturedModelInput(input) {
35111
- return Array.isArray(input);
36772
+ return Array.isArray(input) && input.every(
36773
+ (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))
36774
+ );
35112
36775
  }
35113
36776
  function llmInputKey(sessionId, turnId, stepIndex) {
35114
36777
  return `${sessionId}\0${turnId}\0${stepIndex}`;
@@ -35460,17 +37123,20 @@ var BraintrustObservabilityExporter = class {
35460
37123
  }
35461
37124
  onStart(exported) {
35462
37125
  if (this.spans.has(exported.id)) return;
35463
- const args = {
35464
- name: exported.name,
35465
- spanAttributes: { type: spanTypeFor(exported.type) },
35466
- startTime: epochSeconds(exported.startTime),
35467
- // Use the Mastra span id as the Braintrust row id so that
35468
- // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35469
- // attach to the right row. Without this, `SpanImpl` auto-generates a
35470
- // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35471
- // external caller could know.
35472
- event: { id: exported.id }
35473
- };
37126
+ const args = withSpanInstrumentationName(
37127
+ {
37128
+ name: exported.name,
37129
+ spanAttributes: { type: spanTypeFor(exported.type) },
37130
+ startTime: epochSeconds(exported.startTime),
37131
+ // Use the Mastra span id as the Braintrust row id so that
37132
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
37133
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
37134
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
37135
+ // external caller could know.
37136
+ event: { id: exported.id }
37137
+ },
37138
+ INSTRUMENTATION_NAMES.MASTRA
37139
+ );
35474
37140
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
35475
37141
  if (!this.capturedParent) {
35476
37142
  const probe = currentSpan();
@@ -38503,6 +40169,8 @@ var BaseNode = class {
38503
40169
  this.graph = graph;
38504
40170
  this.id = id;
38505
40171
  }
40172
+ graph;
40173
+ id;
38506
40174
  __type = "node";
38507
40175
  dependencies = [];
38508
40176
  addDependency(dependency) {
@@ -38536,6 +40204,7 @@ var PromptNode = class extends BaseNode {
38536
40204
  super(graph, id);
38537
40205
  this.prompt = prompt;
38538
40206
  }
40207
+ prompt;
38539
40208
  async build(context) {
38540
40209
  return {
38541
40210
  type: "function",
@@ -38548,6 +40217,7 @@ var GateNode = class extends BaseNode {
38548
40217
  super(graph, id);
38549
40218
  this.condition = condition;
38550
40219
  }
40220
+ condition;
38551
40221
  async build(context) {
38552
40222
  return {
38553
40223
  type: "gate",
@@ -38572,6 +40242,7 @@ var PromptTemplateNode = class extends BaseNode {
38572
40242
  super(graph, id);
38573
40243
  this.prompt = prompt;
38574
40244
  }
40245
+ prompt;
38575
40246
  async build(context) {
38576
40247
  return {
38577
40248
  type: "prompt_template",
@@ -38584,6 +40255,7 @@ var LiteralNode = class extends BaseNode {
38584
40255
  super(graph, id);
38585
40256
  this.value = value;
38586
40257
  }
40258
+ value;
38587
40259
  async build(context) {
38588
40260
  return {
38589
40261
  type: "literal",
@@ -39677,6 +41349,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
39677
41349
  this._state = _state;
39678
41350
  this.spanTypeFilter = spanTypeFilter;
39679
41351
  }
41352
+ _objectId;
41353
+ rootSpanId;
41354
+ _state;
41355
+ spanTypeFilter;
39680
41356
  static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
39681
41357
  const children = [
39682
41358
  // Base filter: root_span_id = 'value'
@@ -40127,6 +41803,8 @@ var EvalResultWithSummary = class {
40127
41803
  this.summary = summary;
40128
41804
  this.results = results;
40129
41805
  }
41806
+ summary;
41807
+ results;
40130
41808
  /**
40131
41809
  * @deprecated Use `summary` instead.
40132
41810
  */
@@ -40844,26 +42522,30 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40844
42522
  }
40845
42523
  }
40846
42524
  })();
42525
+ let timeoutId;
42526
+ let abortHandler;
42527
+ const cleanupCancellation = () => {
42528
+ if (timeoutId !== void 0) {
42529
+ clearTimeout(timeoutId);
42530
+ timeoutId = void 0;
42531
+ }
42532
+ if (abortHandler && evaluator.signal) {
42533
+ evaluator.signal.removeEventListener("abort", abortHandler);
42534
+ abortHandler = void 0;
42535
+ }
42536
+ };
40847
42537
  const cancel = async () => {
40848
42538
  await new Promise((_, reject2) => {
40849
42539
  if (cancelled) {
40850
42540
  reject2(new InternalAbortError("Evaluator already cancelled"));
40851
42541
  return;
40852
42542
  }
40853
- let timeoutId;
40854
- let abortHandler;
40855
42543
  const rejectOnce = (error) => {
40856
42544
  if (cancelled) {
40857
42545
  return;
40858
42546
  }
40859
42547
  cancelled = true;
40860
- if (timeoutId) {
40861
- clearTimeout(timeoutId);
40862
- timeoutId = void 0;
40863
- }
40864
- if (abortHandler && evaluator.signal) {
40865
- evaluator.signal.removeEventListener("abort", abortHandler);
40866
- }
42548
+ cleanupCancellation();
40867
42549
  reject2(error);
40868
42550
  };
40869
42551
  if (evaluator.timeout) {
@@ -40903,6 +42585,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40903
42585
  }
40904
42586
  throw e;
40905
42587
  } finally {
42588
+ cleanupCancellation();
40906
42589
  if (!collectResults) {
40907
42590
  collectedResults.length = 0;
40908
42591
  }
@@ -41545,6 +43228,7 @@ var ToolBuilder = class {
41545
43228
  constructor(project) {
41546
43229
  this.project = project;
41547
43230
  }
43231
+ project;
41548
43232
  taskCounter = 0;
41549
43233
  // This type definition is just a catch all so that the implementation can be
41550
43234
  // less specific than the two more specific declarations above.
@@ -41575,6 +43259,7 @@ var ScorerBuilder = class {
41575
43259
  constructor(project) {
41576
43260
  this.project = project;
41577
43261
  }
43262
+ project;
41578
43263
  taskCounter = 0;
41579
43264
  create(opts) {
41580
43265
  this.taskCounter++;
@@ -41634,6 +43319,7 @@ var ClassifierBuilder = class {
41634
43319
  constructor(project) {
41635
43320
  this.project = project;
41636
43321
  }
43322
+ project;
41637
43323
  taskCounter = 0;
41638
43324
  create(opts) {
41639
43325
  this.taskCounter++;
@@ -41669,6 +43355,7 @@ var CodeFunction = class {
41669
43355
  throw new Error("parameters are required if return type is defined");
41670
43356
  }
41671
43357
  }
43358
+ project;
41672
43359
  handler;
41673
43360
  name;
41674
43361
  slug;
@@ -41756,6 +43443,7 @@ var PromptBuilder = class {
41756
43443
  constructor(project) {
41757
43444
  this.project = project;
41758
43445
  }
43446
+ project;
41759
43447
  create(opts) {
41760
43448
  const toolFunctions = [];
41761
43449
  const rawTools = [];
@@ -41832,6 +43520,7 @@ var ParametersBuilder = class {
41832
43520
  constructor(project) {
41833
43521
  this.project = project;
41834
43522
  }
43523
+ project;
41835
43524
  create(opts) {
41836
43525
  const slug = opts.slug ?? slugify(opts.name, { lower: true, strict: true });
41837
43526
  const codeParameters = new CodeParameters(this.project, {