braintrust 3.23.1 → 3.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dev/dist/index.d.mts +11 -0
  2. package/dev/dist/index.d.ts +11 -0
  3. package/dev/dist/index.js +2881 -1518
  4. package/dev/dist/index.mjs +2298 -935
  5. package/dist/apply-auto-instrumentation.js +218 -195
  6. package/dist/apply-auto-instrumentation.mjs +30 -7
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +618 -444
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  9. package/dist/auto-instrumentations/bundler/next.cjs +618 -444
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +618 -444
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  13. package/dist/auto-instrumentations/bundler/vite.cjs +618 -444
  14. package/dist/auto-instrumentations/bundler/vite.d.mts +1 -1
  15. package/dist/auto-instrumentations/bundler/vite.d.ts +1 -1
  16. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  17. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +618 -444
  18. package/dist/auto-instrumentations/bundler/webpack.cjs +618 -444
  19. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  20. package/dist/auto-instrumentations/{chunk-KIMLYPRW.mjs → chunk-7P6563SW.mjs} +1 -1
  21. package/dist/auto-instrumentations/{chunk-YXLNSAMJ.mjs → chunk-CZ24KNHT.mjs} +591 -424
  22. package/dist/auto-instrumentations/{chunk-EXY7QCJD.mjs → chunk-JPVCUKTY.mjs} +30 -21
  23. package/dist/auto-instrumentations/hook.mjs +778 -457
  24. package/dist/auto-instrumentations/index.cjs +590 -424
  25. package/dist/auto-instrumentations/index.mjs +1 -1
  26. package/dist/browser.d.mts +138 -9
  27. package/dist/browser.d.ts +138 -9
  28. package/dist/browser.js +2700 -1011
  29. package/dist/browser.mjs +2700 -1011
  30. package/dist/{chunk-RBXD2KYN.mjs → chunk-2SANLSWX.mjs} +1577 -513
  31. package/dist/{chunk-36IPYKMG.mjs → chunk-ABR2QWDP.mjs} +704 -438
  32. package/dist/{chunk-B6ZQIAK3.js → chunk-GSIDVFE6.js} +2361 -1297
  33. package/dist/{chunk-CDIKAHDZ.js → chunk-P25IOOU4.js} +706 -440
  34. package/dist/cli.js +2307 -940
  35. package/dist/edge-light.js +2700 -1011
  36. package/dist/edge-light.mjs +2700 -1011
  37. package/dist/index.d.mts +138 -9
  38. package/dist/index.d.ts +138 -9
  39. package/dist/index.js +955 -551
  40. package/dist/index.mjs +493 -89
  41. package/dist/instrumentation/index.d.mts +135 -9
  42. package/dist/instrumentation/index.d.ts +135 -9
  43. package/dist/instrumentation/index.js +2554 -1009
  44. package/dist/instrumentation/index.mjs +2554 -1009
  45. package/dist/vitest-evals-reporter.js +17 -16
  46. package/dist/vitest-evals-reporter.mjs +3 -2
  47. package/dist/workerd.js +2700 -1011
  48. package/dist/workerd.mjs +2700 -1011
  49. package/package.json +4 -4
  50. package/util/dist/index.js +4 -0
  51. package/util/dist/index.mjs +4 -0
package/dist/workerd.mjs CHANGED
@@ -21,6 +21,7 @@ var DefaultChannel = class {
21
21
  constructor(name) {
22
22
  this.name = name;
23
23
  }
24
+ name;
24
25
  hasSubscribers = false;
25
26
  subscribe(_subscription) {
26
27
  }
@@ -1077,6 +1078,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
1077
1078
  constructor(data) {
1078
1079
  this.data = data;
1079
1080
  }
1081
+ data;
1080
1082
  toStr() {
1081
1083
  const jsonObj = {
1082
1084
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -1584,6 +1586,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
1584
1586
  constructor(data) {
1585
1587
  this.data = data;
1586
1588
  }
1589
+ data;
1587
1590
  toStr() {
1588
1591
  const jsonObj = {
1589
1592
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -4819,6 +4822,144 @@ var SpanCache = class {
4819
4822
  }
4820
4823
  };
4821
4824
 
4825
+ // src/span-origin.ts
4826
+ var INSTRUMENTATION_NAMES = {
4827
+ AI_SDK: "ai-sdk",
4828
+ ANTHROPIC: "anthropic",
4829
+ BEDROCK_RUNTIME: "bedrock-runtime",
4830
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
4831
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
4832
+ COHERE: "cohere",
4833
+ CURSOR_SDK: "cursor-sdk",
4834
+ EVE: "eve",
4835
+ FLUE: "flue",
4836
+ GENKIT: "genkit",
4837
+ GITHUB_COPILOT: "github-copilot",
4838
+ GOOGLE_ADK: "google-adk",
4839
+ GOOGLE_GENAI: "google-genai",
4840
+ GROQ: "groq",
4841
+ HUGGINGFACE: "huggingface",
4842
+ LANGCHAIN: "langchain",
4843
+ LANGSMITH: "langsmith",
4844
+ MASTRA: "mastra",
4845
+ MISTRAL: "mistral",
4846
+ OPENAI: "openai",
4847
+ OPENAI_AGENTS: "openai-agents",
4848
+ OPENAI_CODEX: "openai-codex",
4849
+ OPENROUTER: "openrouter",
4850
+ OPENROUTER_AGENT: "openrouter-agent",
4851
+ PI_CODING_AGENT: "pi-coding-agent",
4852
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
4853
+ };
4854
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
4855
+ "braintrust.spanInstrumentationName"
4856
+ );
4857
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
4858
+ function withSpanInstrumentationName(args, instrumentationName) {
4859
+ return {
4860
+ ...args,
4861
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
4862
+ };
4863
+ }
4864
+ function getSpanInstrumentationName(args) {
4865
+ if (typeof args !== "object" || args === null) {
4866
+ return void 0;
4867
+ }
4868
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
4869
+ return isSpanInstrumentationName(value) ? value : void 0;
4870
+ }
4871
+ function detectSpanOriginEnvironment(explicit) {
4872
+ if (explicit) return explicit;
4873
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
4874
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
4875
+ if (envType || envName) {
4876
+ return {
4877
+ ...envType ? { type: envType } : {},
4878
+ ...envName ? { name: envName } : {}
4879
+ };
4880
+ }
4881
+ const ci = firstPresent([
4882
+ ["GITHUB_ACTIONS", "github_actions"],
4883
+ ["GITLAB_CI", "gitlab_ci"],
4884
+ ["CIRCLECI", "circleci"],
4885
+ ["BUILDKITE", "buildkite"],
4886
+ ["JENKINS_URL", "jenkins"],
4887
+ ["JENKINS_HOME", "jenkins"],
4888
+ ["TF_BUILD", "azure_pipelines"],
4889
+ ["TEAMCITY_VERSION", "teamcity"],
4890
+ ["TRAVIS", "travis"],
4891
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
4892
+ ]);
4893
+ if (ci) return { type: "ci", name: ci };
4894
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
4895
+ const earlyServer = firstPresent([
4896
+ ["VERCEL", "vercel"],
4897
+ ["NETLIFY", "netlify"]
4898
+ ]);
4899
+ if (earlyServer) return { type: "server", name: earlyServer };
4900
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
4901
+ return { type: "server", name: "ecs" };
4902
+ }
4903
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
4904
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
4905
+ return { type: "server", name: "ecs" };
4906
+ }
4907
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
4908
+ return { type: "server", name: "aws_lambda" };
4909
+ }
4910
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
4911
+ return { type: "server", name: "aws_lambda" };
4912
+ }
4913
+ const server = firstPresent([
4914
+ ["K_SERVICE", "cloud_run"],
4915
+ ["FUNCTION_TARGET", "gcp_functions"],
4916
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
4917
+ ["DYNO", "heroku"],
4918
+ ["FLY_APP_NAME", "fly"],
4919
+ ["RAILWAY_ENVIRONMENT", "railway"],
4920
+ ["RENDER_SERVICE_NAME", "render"]
4921
+ ]);
4922
+ if (server) return { type: "server", name: server };
4923
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
4924
+ }
4925
+ function makeSpanOrigin(instrumentationName, environment) {
4926
+ return {
4927
+ name: "braintrust.sdk.javascript",
4928
+ version: SDK_VERSION,
4929
+ instrumentation: { name: instrumentationName },
4930
+ ...environment ? { environment } : {}
4931
+ };
4932
+ }
4933
+ function mergeSpanOriginContext(context, instrumentationName, environment) {
4934
+ const next = { ...context ?? {} };
4935
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
4936
+ next.span_origin = {
4937
+ ...makeSpanOrigin(instrumentationName, environment),
4938
+ ...current
4939
+ };
4940
+ return next;
4941
+ }
4942
+ function isSpanInstrumentationName(value) {
4943
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
4944
+ }
4945
+ function firstPresent(entries) {
4946
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
4947
+ }
4948
+ function deploymentModeEnvironment(_key, value) {
4949
+ if (!value) return void 0;
4950
+ const normalized = value.toLowerCase();
4951
+ if (normalized === "production" || normalized === "staging") {
4952
+ return { type: "server", name: normalized };
4953
+ }
4954
+ if (normalized === "development" || normalized === "local") {
4955
+ return { type: "local", name: normalized };
4956
+ }
4957
+ return { name: value };
4958
+ }
4959
+ function isObject2(value) {
4960
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4961
+ }
4962
+
4822
4963
  // src/logger.ts
4823
4964
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
4824
4965
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -4867,6 +5008,7 @@ var LoginInvalidOrgError = class extends Error {
4867
5008
  super(message);
4868
5009
  this.message = message;
4869
5010
  }
5011
+ message;
4870
5012
  };
4871
5013
  var REDACTION_FIELDS = [
4872
5014
  "input",
@@ -4882,6 +5024,8 @@ var MaskingError = class {
4882
5024
  this.fieldName = fieldName;
4883
5025
  this.errorType = errorType;
4884
5026
  }
5027
+ fieldName;
5028
+ errorType;
4885
5029
  get errorMsg() {
4886
5030
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
4887
5031
  }
@@ -5091,7 +5235,10 @@ var BraintrustState = class _BraintrustState {
5091
5235
  diskCache: parametersDiskCache
5092
5236
  });
5093
5237
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
5238
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
5239
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5094
5240
  }
5241
+ loginParams;
5095
5242
  id;
5096
5243
  currentExperiment;
5097
5244
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -5126,6 +5273,19 @@ var BraintrustState = class _BraintrustState {
5126
5273
  _idGenerator = null;
5127
5274
  _contextManager = null;
5128
5275
  _otelFlushCallback = null;
5276
+ spanOriginEnvironment;
5277
+ traceContextSigningSecret;
5278
+ /** @internal */
5279
+ _internalSetTraceContextSigningSecret(secret) {
5280
+ const normalizedSecret = secret?.trim();
5281
+ if (normalizedSecret) {
5282
+ this.traceContextSigningSecret = normalizedSecret;
5283
+ }
5284
+ }
5285
+ /** @internal */
5286
+ _internalGetTraceContextSigningSecret() {
5287
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
5288
+ }
5129
5289
  resetLoginInfo() {
5130
5290
  this.appUrl = null;
5131
5291
  this.appPublicUrl = null;
@@ -5186,6 +5346,7 @@ var BraintrustState = class _BraintrustState {
5186
5346
  this.gitMetadataSettings = other.gitMetadataSettings;
5187
5347
  this.debugLogLevel = other.debugLogLevel;
5188
5348
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
5349
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
5189
5350
  setGlobalDebugLogLevel(
5190
5351
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
5191
5352
  );
@@ -5273,6 +5434,7 @@ var BraintrustState = class _BraintrustState {
5273
5434
  return this.debugLogLevelConfigured;
5274
5435
  }
5275
5436
  async login(loginParams) {
5437
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5276
5438
  this.setDebugLogLevel(loginParams.debugLogLevel);
5277
5439
  if (this.apiUrl && !loginParams.forceLogin) {
5278
5440
  return;
@@ -7252,6 +7414,7 @@ function init(projectOrOptions, optionalOptions) {
7252
7414
  throw new Error("Cannot open and update an experiment at the same time");
7253
7415
  }
7254
7416
  const state = stateArg ?? _globalState;
7417
+ state._internalSetTraceContextSigningSecret(apiKey);
7255
7418
  state.enforceQueueSizeLimit(false);
7256
7419
  if (open) {
7257
7420
  if (isEmpty2(experiment)) {
@@ -7744,6 +7907,7 @@ function initLogger(options = {}) {
7744
7907
  orgName,
7745
7908
  forceLogin,
7746
7909
  debugLogLevel,
7910
+ environment,
7747
7911
  fetch: fetch2,
7748
7912
  state: stateArg
7749
7913
  } = options || {};
@@ -7759,7 +7923,9 @@ function initLogger(options = {}) {
7759
7923
  project_id: projectId
7760
7924
  };
7761
7925
  const state = stateArg ?? _globalState;
7926
+ state._internalSetTraceContextSigningSecret(apiKey);
7762
7927
  state.setDebugLogLevel(debugLogLevel);
7928
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
7763
7929
  state.enforceQueueSizeLimit(true);
7764
7930
  const lazyMetadata = new LazyValue(
7765
7931
  async () => {
@@ -8821,7 +8987,9 @@ function deepCopyEvent(event) {
8821
8987
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
8822
8988
  const attachmentMarker = ++deepCopyEventMarkerCounter;
8823
8989
  const serialized = JSON.stringify(event, (_k, v) => {
8824
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
8990
+ if (v instanceof Error) {
8991
+ return v.message;
8992
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
8825
8993
  return `<span>`;
8826
8994
  } else if (v instanceof Experiment2) {
8827
8995
  return `<experiment>`;
@@ -8951,6 +9119,11 @@ var ObjectFetcher = class {
8951
9119
  this._internal_btql = _internal_btql;
8952
9120
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
8953
9121
  }
9122
+ objectType;
9123
+ pinnedVersion;
9124
+ mutateRecord;
9125
+ _internal_btql;
9126
+ _internalBrainstoreRealtime;
8954
9127
  _fetchedData = void 0;
8955
9128
  get id() {
8956
9129
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -9389,6 +9562,8 @@ var ReadonlyExperiment = class extends ObjectFetcher {
9389
9562
  this.state = state;
9390
9563
  this.lazyMetadata = lazyMetadata;
9391
9564
  }
9565
+ state;
9566
+ lazyMetadata;
9392
9567
  get id() {
9393
9568
  return (async () => {
9394
9569
  return (await this.lazyMetadata.get()).experiment.id;
@@ -9482,6 +9657,7 @@ var SpanImpl = class _SpanImpl {
9482
9657
  constructor(args) {
9483
9658
  this._state = args.state;
9484
9659
  this._propagatedState = args.propagatedState;
9660
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
9485
9661
  const spanAttributes = args.spanAttributes ?? {};
9486
9662
  const rawEvent = args.event ?? {};
9487
9663
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -9511,7 +9687,11 @@ var SpanImpl = class _SpanImpl {
9511
9687
  metrics: {
9512
9688
  start: args.startTime ?? getCurrentUnixTimestamp()
9513
9689
  },
9514
- context: { ...callerLocation },
9690
+ context: mergeSpanOriginContext(
9691
+ { ...callerLocation },
9692
+ instrumentationName,
9693
+ this._state.spanOriginEnvironment
9694
+ ),
9515
9695
  span_attributes: {
9516
9696
  name,
9517
9697
  type,
@@ -9885,6 +10065,7 @@ var Dataset2 = class extends ObjectFetcher {
9885
10065
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
9886
10066
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
9887
10067
  }
10068
+ state;
9888
10069
  lazyMetadata;
9889
10070
  __braintrust_dataset_marker = true;
9890
10071
  newRecords = 0;
@@ -10399,6 +10580,9 @@ var Prompt2 = class _Prompt {
10399
10580
  this.noTrace = noTrace;
10400
10581
  void this.__braintrust_prompt_marker;
10401
10582
  }
10583
+ metadata;
10584
+ defaults;
10585
+ noTrace;
10402
10586
  parsedPromptData;
10403
10587
  hasParsedPromptData = false;
10404
10588
  __braintrust_prompt_marker = true;
@@ -10636,6 +10820,7 @@ var RemoteEvalParameters = class {
10636
10820
  this.metadata = metadata;
10637
10821
  void this.__braintrust_parameters_marker;
10638
10822
  }
10823
+ metadata;
10639
10824
  __braintrust_parameters_marker = true;
10640
10825
  get id() {
10641
10826
  return this.metadata.id;
@@ -11611,15 +11796,25 @@ function hasChoices(value) {
11611
11796
  function normalizeMetadata(metadata) {
11612
11797
  return isObject(metadata) ? metadata : void 0;
11613
11798
  }
11614
- function startSpanForEvent(config, event, channelName) {
11799
+ function startSpanForEvent(config, event, channelName, instrumentationName) {
11615
11800
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
11616
11801
  config,
11617
11802
  event
11618
11803
  );
11619
- const span = startSpan({
11620
- name,
11621
- spanAttributes
11622
- });
11804
+ const spanArgs = withSpanInstrumentationName(
11805
+ {
11806
+ name,
11807
+ spanAttributes
11808
+ },
11809
+ instrumentationName
11810
+ );
11811
+ let span;
11812
+ try {
11813
+ span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
11814
+ } catch (error) {
11815
+ debugLogger.error(`Error starting span for ${channelName}:`, error);
11816
+ span = startSpan(spanArgs);
11817
+ }
11623
11818
  const startTime = getCurrentUnixTimestamp();
11624
11819
  try {
11625
11820
  const { input, metadata } = config.extractInput(
@@ -11650,7 +11845,7 @@ function shouldTraceEvent(config, event, channelName) {
11650
11845
  return true;
11651
11846
  }
11652
11847
  }
11653
- function ensureSpanStateForEvent(states, config, event, channelName) {
11848
+ function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
11654
11849
  const key = event;
11655
11850
  const existing = states.get(key);
11656
11851
  if (existing) {
@@ -11659,11 +11854,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
11659
11854
  if (!shouldTraceEvent(config, event, channelName)) {
11660
11855
  return void 0;
11661
11856
  }
11662
- const created = startSpanForEvent(config, event, channelName);
11857
+ const created = startSpanForEvent(
11858
+ config,
11859
+ event,
11860
+ channelName,
11861
+ instrumentationName
11862
+ );
11663
11863
  states.set(key, created);
11664
11864
  return created;
11665
11865
  }
11666
- function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
11866
+ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName, instrumentationName) {
11667
11867
  const state = _internalGetGlobalState();
11668
11868
  const startChannel = tracingChannel2.start;
11669
11869
  const contextManager = state?.contextManager;
@@ -11681,7 +11881,8 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11681
11881
  states,
11682
11882
  config,
11683
11883
  event,
11684
- channelName
11884
+ channelName,
11885
+ instrumentationName
11685
11886
  );
11686
11887
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
11687
11888
  }
@@ -11690,15 +11891,21 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11690
11891
  startChannel.unbindStore(currentSpanStore);
11691
11892
  };
11692
11893
  }
11693
- function logErrorAndEnd(states, event) {
11894
+ function logErrorAndEnd(states, event, channelName) {
11694
11895
  const spanData = states.get(event);
11695
11896
  if (!spanData) {
11696
11897
  return;
11697
11898
  }
11698
- spanData.span.log({
11699
- error: event.error.message
11700
- });
11701
- spanData.span.end();
11899
+ try {
11900
+ spanData.span.log({ error: event.error });
11901
+ } catch (error) {
11902
+ debugLogger.error(`Error logging failure for ${channelName}:`, error);
11903
+ }
11904
+ try {
11905
+ spanData.span.end();
11906
+ } catch (error) {
11907
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
11908
+ }
11702
11909
  states.delete(event);
11703
11910
  }
11704
11911
  function runStreamingCompletionHook(args) {
@@ -11748,7 +11955,8 @@ function traceAsyncChannel(channel2, config) {
11748
11955
  tracingChannel2,
11749
11956
  states,
11750
11957
  config,
11751
- channelName
11958
+ channelName,
11959
+ channel2.instrumentationName
11752
11960
  );
11753
11961
  const handlers = {
11754
11962
  start: (event) => {
@@ -11759,7 +11967,8 @@ function traceAsyncChannel(channel2, config) {
11759
11967
  states,
11760
11968
  config,
11761
11969
  event,
11762
- channelName
11970
+ channelName,
11971
+ channel2.instrumentationName
11763
11972
  );
11764
11973
  },
11765
11974
  asyncEnd: (event) => {
@@ -11796,7 +12005,7 @@ function traceAsyncChannel(channel2, config) {
11796
12005
  }
11797
12006
  },
11798
12007
  error: (event) => {
11799
- logErrorAndEnd(states, event);
12008
+ logErrorAndEnd(states, event, channelName);
11800
12009
  }
11801
12010
  };
11802
12011
  tracingChannel2.subscribe(handlers);
@@ -11813,7 +12022,8 @@ function traceStreamingChannel(channel2, config) {
11813
12022
  tracingChannel2,
11814
12023
  states,
11815
12024
  config,
11816
- channelName
12025
+ channelName,
12026
+ channel2.instrumentationName
11817
12027
  );
11818
12028
  const handlers = {
11819
12029
  start: (event) => {
@@ -11824,7 +12034,8 @@ function traceStreamingChannel(channel2, config) {
11824
12034
  states,
11825
12035
  config,
11826
12036
  event,
11827
- channelName
12037
+ channelName,
12038
+ channel2.instrumentationName
11828
12039
  );
11829
12040
  },
11830
12041
  asyncEnd: (event) => {
@@ -11843,6 +12054,7 @@ function traceStreamingChannel(channel2, config) {
11843
12054
  }
11844
12055
  },
11845
12056
  onComplete: (chunks) => {
12057
+ let completion2;
11846
12058
  try {
11847
12059
  let output;
11848
12060
  let metrics;
@@ -11873,18 +12085,11 @@ function traceStreamingChannel(channel2, config) {
11873
12085
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
11874
12086
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
11875
12087
  }
11876
- runStreamingCompletionHook({
11877
- channelName,
11878
- chunks,
11879
- config,
11880
- endEvent: asyncEndEvent,
12088
+ completion2 = {
11881
12089
  ...metadata !== void 0 ? { metadata } : {},
11882
12090
  metrics,
11883
- output,
11884
- result: asyncEndEvent.result,
11885
- span,
11886
- startTime
11887
- });
12091
+ output
12092
+ };
11888
12093
  span.log({
11889
12094
  output,
11890
12095
  ...metadata !== void 0 ? { metadata } : {},
@@ -11896,11 +12101,49 @@ function traceStreamingChannel(channel2, config) {
11896
12101
  error
11897
12102
  );
11898
12103
  } finally {
11899
- span.end();
12104
+ try {
12105
+ span.end();
12106
+ } catch (error) {
12107
+ debugLogger.error(
12108
+ `Error ending span for ${channelName}:`,
12109
+ error
12110
+ );
12111
+ }
11900
12112
  states.delete(event);
11901
12113
  }
12114
+ if (completion2) {
12115
+ runStreamingCompletionHook({
12116
+ channelName,
12117
+ chunks,
12118
+ config,
12119
+ endEvent: asyncEndEvent,
12120
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
12121
+ metrics: completion2.metrics,
12122
+ output: completion2.output,
12123
+ result: asyncEndEvent.result,
12124
+ span,
12125
+ startTime
12126
+ });
12127
+ }
11902
12128
  },
11903
12129
  onError: (error) => {
12130
+ try {
12131
+ span.log({ error });
12132
+ } catch (loggingError) {
12133
+ debugLogger.error(
12134
+ `Error logging failure for ${channelName}:`,
12135
+ loggingError
12136
+ );
12137
+ }
12138
+ try {
12139
+ span.end();
12140
+ } catch (endingError) {
12141
+ debugLogger.error(
12142
+ `Error ending span for ${channelName}:`,
12143
+ endingError
12144
+ );
12145
+ }
12146
+ states.delete(event);
11904
12147
  runStreamingErrorHook({
11905
12148
  channelName,
11906
12149
  config,
@@ -11909,11 +12152,6 @@ function traceStreamingChannel(channel2, config) {
11909
12152
  span,
11910
12153
  startTime
11911
12154
  });
11912
- span.log({
11913
- error: error.message
11914
- });
11915
- span.end();
11916
- states.delete(event);
11917
12155
  }
11918
12156
  });
11919
12157
  return;
@@ -11928,6 +12166,7 @@ function traceStreamingChannel(channel2, config) {
11928
12166
  states.delete(event);
11929
12167
  return;
11930
12168
  }
12169
+ let completion;
11931
12170
  try {
11932
12171
  const output = config.extractOutput(
11933
12172
  asyncEndEvent.result,
@@ -11942,17 +12181,11 @@ function traceStreamingChannel(channel2, config) {
11942
12181
  asyncEndEvent.result,
11943
12182
  asyncEndEvent
11944
12183
  );
11945
- runStreamingCompletionHook({
11946
- channelName,
11947
- config,
11948
- endEvent: asyncEndEvent,
12184
+ completion = {
11949
12185
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
11950
12186
  metrics,
11951
- output,
11952
- result: asyncEndEvent.result,
11953
- span,
11954
- startTime
11955
- });
12187
+ output
12188
+ };
11956
12189
  span.log({
11957
12190
  output,
11958
12191
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -11961,12 +12194,30 @@ function traceStreamingChannel(channel2, config) {
11961
12194
  } catch (error) {
11962
12195
  debugLogger.error(`Error extracting output for ${channelName}:`, error);
11963
12196
  } finally {
11964
- span.end();
12197
+ try {
12198
+ span.end();
12199
+ } catch (error) {
12200
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
12201
+ }
11965
12202
  states.delete(event);
11966
12203
  }
12204
+ if (completion) {
12205
+ runStreamingCompletionHook({
12206
+ channelName,
12207
+ config,
12208
+ endEvent: asyncEndEvent,
12209
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
12210
+ metrics: completion.metrics,
12211
+ output: completion.output,
12212
+ result: asyncEndEvent.result,
12213
+ span,
12214
+ startTime
12215
+ });
12216
+ }
11967
12217
  },
11968
12218
  error: (event) => {
11969
12219
  const spanData = states.get(event);
12220
+ logErrorAndEnd(states, event, channelName);
11970
12221
  if (spanData) {
11971
12222
  runStreamingErrorHook({
11972
12223
  channelName,
@@ -11977,7 +12228,6 @@ function traceStreamingChannel(channel2, config) {
11977
12228
  startTime: spanData.startTime
11978
12229
  });
11979
12230
  }
11980
- logErrorAndEnd(states, event);
11981
12231
  }
11982
12232
  };
11983
12233
  tracingChannel2.subscribe(handlers);
@@ -11994,7 +12244,8 @@ function traceSyncStreamChannel(channel2, config) {
11994
12244
  tracingChannel2,
11995
12245
  states,
11996
12246
  config,
11997
- channelName
12247
+ channelName,
12248
+ channel2.instrumentationName
11998
12249
  );
11999
12250
  const handlers = {
12000
12251
  start: (event) => {
@@ -12005,7 +12256,8 @@ function traceSyncStreamChannel(channel2, config) {
12005
12256
  states,
12006
12257
  config,
12007
12258
  event,
12008
- channelName
12259
+ channelName,
12260
+ channel2.instrumentationName
12009
12261
  );
12010
12262
  },
12011
12263
  end: (event) => {
@@ -12099,7 +12351,7 @@ function traceSyncStreamChannel(channel2, config) {
12099
12351
  handleResolvedResult(endEvent.result);
12100
12352
  },
12101
12353
  error: (event) => {
12102
- logErrorAndEnd(states, event);
12354
+ logErrorAndEnd(states, event, channelName);
12103
12355
  }
12104
12356
  };
12105
12357
  tracingChannel2.subscribe(handlers);
@@ -12278,7 +12530,8 @@ function processInputAttachments(input) {
12278
12530
  function channel(spec) {
12279
12531
  return spec;
12280
12532
  }
12281
- function defineChannels(pkg, channels) {
12533
+ function defineChannels(pkg, channels, options) {
12534
+ const { instrumentationName } = options;
12282
12535
  return Object.fromEntries(
12283
12536
  Object.entries(channels).map(([key, spec]) => {
12284
12537
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -12291,6 +12544,7 @@ function defineChannels(pkg, channels) {
12291
12544
  key,
12292
12545
  {
12293
12546
  ...asyncSpec,
12547
+ instrumentationName,
12294
12548
  tracingChannel: tracingChannel3,
12295
12549
  tracePromise: (fn, context) => tracingChannel3().tracePromise(
12296
12550
  fn,
@@ -12308,6 +12562,7 @@ function defineChannels(pkg, channels) {
12308
12562
  key,
12309
12563
  {
12310
12564
  ...syncSpec,
12565
+ instrumentationName,
12311
12566
  tracingChannel: tracingChannel2,
12312
12567
  traceSync: (fn, context) => tracingChannel2().traceSync(
12313
12568
  fn,
@@ -12321,44 +12576,48 @@ function defineChannels(pkg, channels) {
12321
12576
  }
12322
12577
 
12323
12578
  // src/instrumentation/plugins/openai-channels.ts
12324
- var openAIChannels = defineChannels("openai", {
12325
- chatCompletionsCreate: channel({
12326
- channelName: "chat.completions.create",
12327
- kind: "async"
12328
- }),
12329
- embeddingsCreate: channel({
12330
- channelName: "embeddings.create",
12331
- kind: "async"
12332
- }),
12333
- betaChatCompletionsParse: channel({
12334
- channelName: "beta.chat.completions.parse",
12335
- kind: "async"
12336
- }),
12337
- betaChatCompletionsStream: channel({
12338
- channelName: "beta.chat.completions.stream",
12339
- kind: "sync-stream"
12340
- }),
12341
- moderationsCreate: channel({
12342
- channelName: "moderations.create",
12343
- kind: "async"
12344
- }),
12345
- responsesCreate: channel({
12346
- channelName: "responses.create",
12347
- kind: "async"
12348
- }),
12349
- responsesStream: channel({
12350
- channelName: "responses.stream",
12351
- kind: "sync-stream"
12352
- }),
12353
- responsesParse: channel({
12354
- channelName: "responses.parse",
12355
- kind: "async"
12356
- }),
12357
- responsesCompact: channel({
12358
- channelName: "responses.compact",
12359
- kind: "async"
12360
- })
12361
- });
12579
+ var openAIChannels = defineChannels(
12580
+ "openai",
12581
+ {
12582
+ chatCompletionsCreate: channel({
12583
+ channelName: "chat.completions.create",
12584
+ kind: "async"
12585
+ }),
12586
+ embeddingsCreate: channel({
12587
+ channelName: "embeddings.create",
12588
+ kind: "async"
12589
+ }),
12590
+ betaChatCompletionsParse: channel({
12591
+ channelName: "beta.chat.completions.parse",
12592
+ kind: "async"
12593
+ }),
12594
+ betaChatCompletionsStream: channel({
12595
+ channelName: "beta.chat.completions.stream",
12596
+ kind: "sync-stream"
12597
+ }),
12598
+ moderationsCreate: channel({
12599
+ channelName: "moderations.create",
12600
+ kind: "async"
12601
+ }),
12602
+ responsesCreate: channel({
12603
+ channelName: "responses.create",
12604
+ kind: "async"
12605
+ }),
12606
+ responsesStream: channel({
12607
+ channelName: "responses.stream",
12608
+ kind: "sync-stream"
12609
+ }),
12610
+ responsesParse: channel({
12611
+ channelName: "responses.parse",
12612
+ kind: "async"
12613
+ }),
12614
+ responsesCompact: channel({
12615
+ channelName: "responses.compact",
12616
+ kind: "async"
12617
+ })
12618
+ },
12619
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
12620
+ );
12362
12621
 
12363
12622
  // src/openai-utils.ts
12364
12623
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -12873,16 +13132,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
12873
13132
  }
12874
13133
 
12875
13134
  // src/instrumentation/plugins/openai-codex-channels.ts
12876
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
12877
- run: channel({
12878
- channelName: "Thread.run",
12879
- kind: "async"
12880
- }),
12881
- runStreamed: channel({
12882
- channelName: "Thread.runStreamed",
12883
- kind: "async"
12884
- })
12885
- });
13135
+ var openAICodexChannels = defineChannels(
13136
+ "@openai/codex-sdk",
13137
+ {
13138
+ run: channel({
13139
+ channelName: "Thread.run",
13140
+ kind: "async"
13141
+ }),
13142
+ runStreamed: channel({
13143
+ channelName: "Thread.runStreamed",
13144
+ kind: "async"
13145
+ })
13146
+ },
13147
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
13148
+ );
12886
13149
 
12887
13150
  // src/instrumentation/plugins/openai-codex-plugin.ts
12888
13151
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -12970,10 +13233,15 @@ function startCodexRun(event, operation) {
12970
13233
  provider: "openai",
12971
13234
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
12972
13235
  };
12973
- const span = startSpan({
12974
- name: "OpenAI Codex",
12975
- spanAttributes: { type: "task" /* TASK */ }
12976
- });
13236
+ const span = startSpan(
13237
+ withSpanInstrumentationName(
13238
+ {
13239
+ name: "OpenAI Codex",
13240
+ spanAttributes: { type: "task" /* TASK */ }
13241
+ },
13242
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13243
+ )
13244
+ );
12977
13245
  const startTime = getCurrentUnixTimestamp();
12978
13246
  safeLog(span, {
12979
13247
  input: sanitizedInput,
@@ -13122,7 +13390,12 @@ async function createCompletedItemSpan(state, item) {
13122
13390
  if (!spanArgs) {
13123
13391
  return;
13124
13392
  }
13125
- const span = startSpan(spanArgs.start);
13393
+ const span = startSpan(
13394
+ withSpanInstrumentationName(
13395
+ spanArgs.start,
13396
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13397
+ )
13398
+ );
13126
13399
  safeLog(span, spanArgs.end);
13127
13400
  span.end();
13128
13401
  }
@@ -13164,15 +13437,20 @@ async function ensureActiveLlmSpan(state) {
13164
13437
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
13165
13438
  "openai_codex.llm_sequence": sequence
13166
13439
  };
13167
- const span = startSpan({
13168
- event: {
13169
- ...sequence === 1 ? { input: state.input } : {},
13170
- metadata
13171
- },
13172
- name: "OpenAI Codex LLM",
13173
- parent: await state.span.export(),
13174
- spanAttributes: { type: "llm" /* LLM */ }
13175
- });
13440
+ const span = startSpan(
13441
+ withSpanInstrumentationName(
13442
+ {
13443
+ event: {
13444
+ ...sequence === 1 ? { input: state.input } : {},
13445
+ metadata
13446
+ },
13447
+ name: "OpenAI Codex LLM",
13448
+ parent: await state.span.export(),
13449
+ spanAttributes: { type: "llm" /* LLM */ }
13450
+ },
13451
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13452
+ )
13453
+ );
13176
13454
  state.activeLlmSpan = {
13177
13455
  anonymousMessages: [],
13178
13456
  anonymousReasoning: [],
@@ -13218,7 +13496,15 @@ async function startCodexItemSpan(state, item) {
13218
13496
  if (!spanArgs) {
13219
13497
  return;
13220
13498
  }
13221
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
13499
+ state.activeItemSpans.set(
13500
+ itemId,
13501
+ startSpan(
13502
+ withSpanInstrumentationName(
13503
+ spanArgs.start,
13504
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13505
+ )
13506
+ )
13507
+ );
13222
13508
  }
13223
13509
  function updateCodexItem(state, item) {
13224
13510
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -13528,20 +13814,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
13528
13814
  }
13529
13815
 
13530
13816
  // src/instrumentation/plugins/anthropic-channels.ts
13531
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
13532
- messagesCreate: channel({
13533
- channelName: "messages.create",
13534
- kind: "async"
13535
- }),
13536
- betaMessagesCreate: channel({
13537
- channelName: "beta.messages.create",
13538
- kind: "async"
13539
- }),
13540
- betaMessagesToolRunner: channel({
13541
- channelName: "beta.messages.toolRunner",
13542
- kind: "sync-stream"
13543
- })
13544
- });
13817
+ var anthropicChannels = defineChannels(
13818
+ "@anthropic-ai/sdk",
13819
+ {
13820
+ messagesCreate: channel({
13821
+ channelName: "messages.create",
13822
+ kind: "async"
13823
+ }),
13824
+ betaMessagesCreate: channel({
13825
+ channelName: "beta.messages.create",
13826
+ kind: "async"
13827
+ }),
13828
+ betaMessagesToolRunner: channel({
13829
+ channelName: "beta.messages.toolRunner",
13830
+ kind: "sync-stream"
13831
+ })
13832
+ },
13833
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
13834
+ );
13545
13835
 
13546
13836
  // src/instrumentation/plugins/anthropic-plugin.ts
13547
13837
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -13614,12 +13904,17 @@ var AnthropicPlugin = class extends BasePlugin {
13614
13904
  return;
13615
13905
  }
13616
13906
  const params = event.arguments[0] ?? {};
13617
- const span = startSpan({
13618
- name: "anthropic.beta.messages.toolRunner",
13619
- spanAttributes: {
13620
- type: "task" /* TASK */
13621
- }
13622
- });
13907
+ const span = startSpan(
13908
+ withSpanInstrumentationName(
13909
+ {
13910
+ name: "anthropic.beta.messages.toolRunner",
13911
+ spanAttributes: {
13912
+ type: "task" /* TASK */
13913
+ }
13914
+ },
13915
+ INSTRUMENTATION_NAMES.ANTHROPIC
13916
+ )
13917
+ );
13623
13918
  span.log({
13624
13919
  input: processAttachmentsInInput(
13625
13920
  coalesceInput(params.messages ?? [], params.system)
@@ -13760,19 +14055,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
13760
14055
  return finalizeError(error);
13761
14056
  }
13762
14057
  },
13763
- {
13764
- event: {
13765
- input: getAnthropicToolRunnerInput(args),
13766
- metadata: {
13767
- "gen_ai.tool.name": toolName,
13768
- provider: "anthropic"
14058
+ withSpanInstrumentationName(
14059
+ {
14060
+ event: {
14061
+ input: getAnthropicToolRunnerInput(args),
14062
+ metadata: {
14063
+ "gen_ai.tool.name": toolName,
14064
+ provider: "anthropic"
14065
+ }
14066
+ },
14067
+ name: `tool: ${toolName}`,
14068
+ spanAttributes: {
14069
+ type: "tool" /* TOOL */
13769
14070
  }
13770
14071
  },
13771
- name: `tool: ${toolName}`,
13772
- spanAttributes: {
13773
- type: "tool" /* TOOL */
13774
- }
13775
- }
14072
+ INSTRUMENTATION_NAMES.ANTHROPIC
14073
+ )
13776
14074
  );
13777
14075
  },
13778
14076
  writable: runDescriptor?.writable ?? true
@@ -14367,6 +14665,267 @@ function currentWorkflowAgentWrapperSpan() {
14367
14665
  return void 0;
14368
14666
  }
14369
14667
 
14668
+ // src/wrappers/ai-sdk/harness-agent-context.ts
14669
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
14670
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
14671
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
14672
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
14673
+ var harnessTurnParentStore;
14674
+ function serializedTurnContext(value) {
14675
+ if (!isObject(value)) {
14676
+ return void 0;
14677
+ }
14678
+ const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
14679
+ if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
14680
+ return void 0;
14681
+ }
14682
+ const expectedSignature = signTurnContext({
14683
+ parent: context.parent,
14684
+ sessionId: context.sessionId
14685
+ });
14686
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
14687
+ return void 0;
14688
+ }
14689
+ const parent = SpanComponentsV4.fromStr(context.parent);
14690
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
14691
+ return void 0;
14692
+ }
14693
+ return {
14694
+ parent: context.parent,
14695
+ sessionId: context.sessionId
14696
+ };
14697
+ }
14698
+ function signTurnContext(context) {
14699
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
14700
+ return secret ? isomorph_default.hmacSha256?.(
14701
+ secret,
14702
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
14703
+ ) : void 0;
14704
+ }
14705
+ function continuationParentFromCreateSessionParams(params) {
14706
+ if (!isObject(params)) {
14707
+ return void 0;
14708
+ }
14709
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
14710
+ const context = serializedTurnContext(continuation);
14711
+ if (!context) {
14712
+ return void 0;
14713
+ }
14714
+ if (params.sessionId !== context.sessionId) {
14715
+ return void 0;
14716
+ }
14717
+ return context.parent;
14718
+ }
14719
+ function exportSpanSynchronously(span) {
14720
+ const parentInfo = span.getParentInfo();
14721
+ if (!parentInfo) {
14722
+ return void 0;
14723
+ }
14724
+ const objectId = parentInfo.objectId.getSync().value;
14725
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
14726
+ return void 0;
14727
+ }
14728
+ return new SpanComponentsV4({
14729
+ object_type: parentInfo.objectType,
14730
+ ...objectId ? { object_id: objectId } : {
14731
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
14732
+ },
14733
+ row_id: span.id,
14734
+ root_span_id: span.rootSpanId,
14735
+ span_id: span.spanId
14736
+ }).toStr();
14737
+ }
14738
+ function exportedParent(parent) {
14739
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
14740
+ }
14741
+ function addSerializedContext(args) {
14742
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
14743
+ return;
14744
+ }
14745
+ const parent = exportedParent(args.parent);
14746
+ if (!parent) {
14747
+ return;
14748
+ }
14749
+ const context = {
14750
+ parent,
14751
+ sessionId: args.sessionId
14752
+ };
14753
+ const signature = signTurnContext(context);
14754
+ if (!signature) {
14755
+ return;
14756
+ }
14757
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
14758
+ configurable: true,
14759
+ enumerable: true,
14760
+ value: {
14761
+ ...context,
14762
+ signature
14763
+ },
14764
+ writable: true
14765
+ });
14766
+ }
14767
+ function lifecycleMethodDescriptor(session, method) {
14768
+ let owner = session;
14769
+ while (owner) {
14770
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
14771
+ if (descriptor) {
14772
+ return { descriptor, owner };
14773
+ }
14774
+ owner = Object.getPrototypeOf(owner);
14775
+ }
14776
+ return void 0;
14777
+ }
14778
+ function patchLifecycleMethod(session, method) {
14779
+ let resolvedDescriptor;
14780
+ try {
14781
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
14782
+ } catch {
14783
+ return;
14784
+ }
14785
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
14786
+ return;
14787
+ }
14788
+ const { descriptor, owner } = resolvedDescriptor;
14789
+ const patched = patchedLifecycleMethods.get(owner);
14790
+ if (patched?.has(method)) {
14791
+ return;
14792
+ }
14793
+ const original = descriptor.value;
14794
+ const replacement = function(...args) {
14795
+ const result = Reflect.apply(original, this, args);
14796
+ const addContext = (state) => {
14797
+ try {
14798
+ const parent = sessionTurnParents.get(this);
14799
+ if (!parent) {
14800
+ return;
14801
+ }
14802
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
14803
+ addSerializedContext({
14804
+ continuation,
14805
+ parent,
14806
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
14807
+ });
14808
+ } catch {
14809
+ }
14810
+ };
14811
+ try {
14812
+ if (isObject(result) && typeof result.then === "function") {
14813
+ void Promise.resolve(result).then(addContext, () => {
14814
+ });
14815
+ } else {
14816
+ addContext(result);
14817
+ }
14818
+ } catch {
14819
+ }
14820
+ return result;
14821
+ };
14822
+ try {
14823
+ Object.defineProperty(owner, method, {
14824
+ ...descriptor,
14825
+ value: replacement
14826
+ });
14827
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
14828
+ ownerMethods.add(method);
14829
+ if (!patched) {
14830
+ patchedLifecycleMethods.set(owner, ownerMethods);
14831
+ }
14832
+ } catch {
14833
+ }
14834
+ }
14835
+ function patchLifecycleMethods(session) {
14836
+ patchLifecycleMethod(session, "suspendTurn");
14837
+ patchLifecycleMethod(session, "detach");
14838
+ patchLifecycleMethod(session, "stop");
14839
+ }
14840
+ function registerHarnessTurnSpan(args) {
14841
+ if (!isObject(args.session)) {
14842
+ return;
14843
+ }
14844
+ const session = args.session;
14845
+ sessionTurnParents.set(session, args.span);
14846
+ wrapperTurnParents.set(args.span, args.span);
14847
+ patchLifecycleMethods(session);
14848
+ }
14849
+ function harnessContinuationParent(session) {
14850
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
14851
+ }
14852
+ function captureHarnessCreateSessionParent(params) {
14853
+ try {
14854
+ return continuationParentFromCreateSessionParams(params);
14855
+ } catch {
14856
+ return void 0;
14857
+ }
14858
+ }
14859
+ function registerHarnessSessionParent(session, parent) {
14860
+ if (!parent || !isObject(session)) {
14861
+ return;
14862
+ }
14863
+ const harnessSession = session;
14864
+ sessionTurnParents.set(harnessSession, parent);
14865
+ patchLifecycleMethods(harnessSession);
14866
+ }
14867
+ function currentHarnessTurnParent() {
14868
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
14869
+ }
14870
+ function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
14871
+ const startChannel = tracingChannel2.start;
14872
+ if (!startChannel) {
14873
+ return () => {
14874
+ };
14875
+ }
14876
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
14877
+ const store = harnessTurnParentStore;
14878
+ startChannel.bindStore(
14879
+ store,
14880
+ (event) => parentFromEvent(event) ?? store.getStore()
14881
+ );
14882
+ return () => {
14883
+ startChannel.unbindStore(store);
14884
+ };
14885
+ }
14886
+ function startHarnessTurnChildSpan(parent, args) {
14887
+ const spanArgs = withSpanInstrumentationName(
14888
+ args,
14889
+ INSTRUMENTATION_NAMES.AI_SDK
14890
+ );
14891
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
14892
+ }
14893
+ function updateHarnessTurn(parent, update = {}) {
14894
+ if (!parent) {
14895
+ return;
14896
+ }
14897
+ const log3 = (event) => {
14898
+ if (typeof parent === "string") {
14899
+ updateSpan({ exported: parent, ...event });
14900
+ } else {
14901
+ parent.log(event);
14902
+ }
14903
+ };
14904
+ try {
14905
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
14906
+ log3({ output: null });
14907
+ }
14908
+ log3(update);
14909
+ } catch {
14910
+ }
14911
+ }
14912
+ function endHarnessTurn(parent) {
14913
+ const endTime = getCurrentUnixTimestamp();
14914
+ if (!parent) {
14915
+ return endTime;
14916
+ }
14917
+ try {
14918
+ const event = { metrics: { end: endTime } };
14919
+ if (typeof parent === "string") {
14920
+ updateSpan({ exported: parent, ...event });
14921
+ } else {
14922
+ parent.log(event);
14923
+ }
14924
+ } catch {
14925
+ }
14926
+ return endTime;
14927
+ }
14928
+
14370
14929
  // src/wrappers/ai-sdk/telemetry.ts
14371
14930
  function braintrustAISDKTelemetry() {
14372
14931
  const operations = /* @__PURE__ */ new Map();
@@ -14385,14 +14944,17 @@ function braintrustAISDKTelemetry() {
14385
14944
  console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
14386
14945
  }
14387
14946
  };
14388
- const startChildSpan = (operationKey, name, type, event) => {
14947
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
14389
14948
  const parent = operations.get(operationKey)?.span;
14390
- const spanArgs = {
14391
- name,
14392
- spanAttributes: { type },
14393
- ...event ? { event } : {}
14394
- };
14395
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
14949
+ const spanArgs = withSpanInstrumentationName(
14950
+ {
14951
+ name,
14952
+ spanAttributes: { type },
14953
+ ...event ? { event } : {}
14954
+ },
14955
+ INSTRUMENTATION_NAMES.AI_SDK
14956
+ );
14957
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
14396
14958
  const state = operations.get(operationKey);
14397
14959
  if (state && type === "llm" /* LLM */) {
14398
14960
  state.hadModelChild = true;
@@ -14672,14 +15234,30 @@ function braintrustAISDKTelemetry() {
14672
15234
  const workflowAgent = operationName === "WorkflowAgent.stream";
14673
15235
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
14674
15236
  const ownsSpan = !wrapperSpan;
14675
- const span = ownsSpan ? startSpan({
14676
- name: operationName,
14677
- spanAttributes: { type: "function" /* FUNCTION */ }
14678
- }) : wrapperSpan;
15237
+ const harnessTurnParent = currentHarnessTurnParent();
15238
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
15239
+ harnessTurnParent,
15240
+ withSpanInstrumentationName(
15241
+ {
15242
+ name: operationName,
15243
+ spanAttributes: { type: "function" /* FUNCTION */ }
15244
+ },
15245
+ INSTRUMENTATION_NAMES.AI_SDK
15246
+ )
15247
+ ) : startSpan(
15248
+ withSpanInstrumentationName(
15249
+ {
15250
+ name: operationName,
15251
+ spanAttributes: { type: "function" /* FUNCTION */ }
15252
+ },
15253
+ INSTRUMENTATION_NAMES.AI_SDK
15254
+ )
15255
+ ) : wrapperSpan;
14679
15256
  const operationKey = createOperationKey(event, operationName);
14680
15257
  registerOperation({
14681
15258
  callId: event.callId,
14682
15259
  hadModelChild: false,
15260
+ harnessTurnParent,
14683
15261
  loggedInput: false,
14684
15262
  operationName,
14685
15263
  operationKey,
@@ -14920,7 +15498,8 @@ function braintrustAISDKTelemetry() {
14920
15498
  toolCallId,
14921
15499
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
14922
15500
  }
14923
- }
15501
+ },
15502
+ state?.harnessTurnParent
14924
15503
  );
14925
15504
  toolSpans.set(toolCallId, { operationKey, span });
14926
15505
  });
@@ -15156,74 +15735,102 @@ function finishOutput(result, operationName) {
15156
15735
  }
15157
15736
 
15158
15737
  // src/instrumentation/plugins/ai-sdk-channels.ts
15159
- var aiSDKChannels = defineChannels("ai", {
15160
- generateText: channel({
15161
- channelName: "generateText",
15162
- kind: "async"
15163
- }),
15164
- streamText: channel({
15165
- channelName: "streamText",
15166
- kind: "async"
15167
- }),
15168
- streamTextSync: channel({
15169
- channelName: "streamText.sync",
15170
- kind: "sync-stream"
15171
- }),
15172
- generateObject: channel({
15173
- channelName: "generateObject",
15174
- kind: "async"
15175
- }),
15176
- streamObject: channel({
15177
- channelName: "streamObject",
15178
- kind: "async"
15179
- }),
15180
- streamObjectSync: channel({
15181
- channelName: "streamObject.sync",
15182
- kind: "sync-stream"
15183
- }),
15184
- embed: channel(
15185
- {
15738
+ var aiSDKChannels = defineChannels(
15739
+ "ai",
15740
+ {
15741
+ generateText: channel({
15742
+ channelName: "generateText",
15743
+ kind: "async"
15744
+ }),
15745
+ streamText: channel({
15746
+ channelName: "streamText",
15747
+ kind: "async"
15748
+ }),
15749
+ streamTextSync: channel({
15750
+ channelName: "streamText.sync",
15751
+ kind: "sync-stream"
15752
+ }),
15753
+ generateObject: channel({
15754
+ channelName: "generateObject",
15755
+ kind: "async"
15756
+ }),
15757
+ streamObject: channel({
15758
+ channelName: "streamObject",
15759
+ kind: "async"
15760
+ }),
15761
+ streamObjectSync: channel({
15762
+ channelName: "streamObject.sync",
15763
+ kind: "sync-stream"
15764
+ }),
15765
+ embed: channel({
15186
15766
  channelName: "embed",
15187
15767
  kind: "async"
15188
- }
15189
- ),
15190
- embedMany: channel({
15191
- channelName: "embedMany",
15192
- kind: "async"
15193
- }),
15194
- rerank: channel({
15195
- channelName: "rerank",
15196
- kind: "async"
15197
- }),
15198
- agentGenerate: channel({
15199
- channelName: "Agent.generate",
15200
- kind: "async"
15201
- }),
15202
- agentStream: channel({
15203
- channelName: "Agent.stream",
15204
- kind: "async"
15205
- }),
15206
- agentStreamSync: channel({
15207
- channelName: "Agent.stream.sync",
15208
- kind: "sync-stream"
15209
- }),
15210
- toolLoopAgentGenerate: channel({
15211
- channelName: "ToolLoopAgent.generate",
15212
- kind: "async"
15213
- }),
15214
- toolLoopAgentStream: channel({
15215
- channelName: "ToolLoopAgent.stream",
15216
- kind: "async"
15217
- }),
15218
- workflowAgentStream: channel({
15219
- channelName: "WorkflowAgent.stream",
15220
- kind: "async"
15221
- }),
15222
- v7CreateTelemetryDispatcher: channel({
15223
- channelName: "createTelemetryDispatcher",
15224
- kind: "sync-stream"
15225
- })
15226
- });
15768
+ }),
15769
+ embedMany: channel({
15770
+ channelName: "embedMany",
15771
+ kind: "async"
15772
+ }),
15773
+ rerank: channel({
15774
+ channelName: "rerank",
15775
+ kind: "async"
15776
+ }),
15777
+ agentGenerate: channel({
15778
+ channelName: "Agent.generate",
15779
+ kind: "async"
15780
+ }),
15781
+ agentStream: channel({
15782
+ channelName: "Agent.stream",
15783
+ kind: "async"
15784
+ }),
15785
+ agentStreamSync: channel({
15786
+ channelName: "Agent.stream.sync",
15787
+ kind: "sync-stream"
15788
+ }),
15789
+ toolLoopAgentGenerate: channel({
15790
+ channelName: "ToolLoopAgent.generate",
15791
+ kind: "async"
15792
+ }),
15793
+ toolLoopAgentStream: channel({
15794
+ channelName: "ToolLoopAgent.stream",
15795
+ kind: "async"
15796
+ }),
15797
+ workflowAgentStream: channel({
15798
+ channelName: "WorkflowAgent.stream",
15799
+ kind: "async"
15800
+ }),
15801
+ v7CreateTelemetryDispatcher: channel({
15802
+ channelName: "createTelemetryDispatcher",
15803
+ kind: "sync-stream"
15804
+ })
15805
+ },
15806
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
15807
+ );
15808
+ var harnessAgentChannels = defineChannels(
15809
+ "@ai-sdk/harness",
15810
+ {
15811
+ createSession: channel({
15812
+ channelName: "HarnessAgent.createSession",
15813
+ kind: "async"
15814
+ }),
15815
+ generate: channel({
15816
+ channelName: "HarnessAgent.generate",
15817
+ kind: "async"
15818
+ }),
15819
+ stream: channel({
15820
+ channelName: "HarnessAgent.stream",
15821
+ kind: "async"
15822
+ }),
15823
+ continueGenerate: channel({
15824
+ channelName: "HarnessAgent.continueGenerate",
15825
+ kind: "async"
15826
+ }),
15827
+ continueStream: channel({
15828
+ channelName: "HarnessAgent.continueStream",
15829
+ kind: "async"
15830
+ })
15831
+ },
15832
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
15833
+ );
15227
15834
 
15228
15835
  // src/instrumentation/plugins/ai-sdk-plugin.ts
15229
15836
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -15300,6 +15907,17 @@ var AISDKPlugin = class extends BasePlugin {
15300
15907
  subscribeToAISDK() {
15301
15908
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
15302
15909
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
15910
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
15911
+ this.unsubscribers.push(
15912
+ subscribeToHarnessContinuation(
15913
+ harnessAgentChannels.continueGenerate,
15914
+ denyOutputPaths
15915
+ ),
15916
+ subscribeToHarnessContinuation(
15917
+ harnessAgentChannels.continueStream,
15918
+ denyOutputPaths
15919
+ )
15920
+ );
15303
15921
  this.unsubscribers.push(
15304
15922
  traceStreamingChannel(aiSDKChannels.generateText, {
15305
15923
  name: "generateText",
@@ -15492,6 +16110,90 @@ var AISDKPlugin = class extends BasePlugin {
15492
16110
  })
15493
16111
  })
15494
16112
  );
16113
+ this.unsubscribers.push(
16114
+ traceStreamingChannel(harnessAgentChannels.generate, {
16115
+ name: "HarnessAgent.generate",
16116
+ startSpan: _internalStartSpanWithInitialMerge,
16117
+ type: "task" /* TASK */,
16118
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16119
+ extractOutput: (result, endEvent) => processAISDKOutput(
16120
+ result,
16121
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16122
+ ),
16123
+ extractMetrics: (result) => extractTokenMetrics(result),
16124
+ aggregateChunks: aggregateAISDKChunks
16125
+ })
16126
+ );
16127
+ this.unsubscribers.push(
16128
+ traceStreamingChannel(harnessAgentChannels.stream, {
16129
+ name: "HarnessAgent.stream",
16130
+ startSpan: _internalStartSpanWithInitialMerge,
16131
+ type: "task" /* TASK */,
16132
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16133
+ extractOutput: (result, endEvent) => processAISDKOutput(
16134
+ result,
16135
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16136
+ ),
16137
+ extractMetrics: (result, startTime) => ({
16138
+ ...extractTokenMetrics(result),
16139
+ ...startTime === void 0 ? {} : {
16140
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16141
+ }
16142
+ }),
16143
+ aggregateChunks: aggregateAISDKChunks,
16144
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16145
+ defaultDenyOutputPaths: denyOutputPaths,
16146
+ endEvent,
16147
+ result,
16148
+ resolvePromiseUsage: true,
16149
+ span,
16150
+ startTime
16151
+ })
16152
+ })
16153
+ );
16154
+ this.unsubscribers.push(
16155
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
16156
+ name: "HarnessAgent.continueGenerate",
16157
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16158
+ startSpan: _internalStartSpanWithInitialMerge,
16159
+ type: "task" /* TASK */,
16160
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16161
+ extractOutput: (result, endEvent) => processAISDKOutput(
16162
+ result,
16163
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16164
+ ),
16165
+ extractMetrics: (result) => extractTokenMetrics(result),
16166
+ aggregateChunks: aggregateAISDKChunks
16167
+ })
16168
+ );
16169
+ this.unsubscribers.push(
16170
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
16171
+ name: "HarnessAgent.continueStream",
16172
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16173
+ startSpan: _internalStartSpanWithInitialMerge,
16174
+ type: "task" /* TASK */,
16175
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16176
+ extractOutput: (result, endEvent) => processAISDKOutput(
16177
+ result,
16178
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16179
+ ),
16180
+ extractMetrics: (result, startTime) => ({
16181
+ ...extractTokenMetrics(result),
16182
+ ...startTime === void 0 ? {} : {
16183
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16184
+ }
16185
+ }),
16186
+ aggregateChunks: aggregateAISDKChunks,
16187
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16188
+ defaultDenyOutputPaths: denyOutputPaths,
16189
+ endEvent,
16190
+ result,
16191
+ resolvePromiseUsage: true,
16192
+ span,
16193
+ startTime
16194
+ })
16195
+ })
16196
+ );
15495
16197
  this.unsubscribers.push(
15496
16198
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
15497
16199
  name: "ToolLoopAgent.generate",
@@ -15562,6 +16264,8 @@ var AISDKPlugin = class extends BasePlugin {
15562
16264
  defaultDenyOutputPaths: denyOutputPaths,
15563
16265
  endEvent,
15564
16266
  onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
16267
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
16268
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
15565
16269
  result,
15566
16270
  span,
15567
16271
  startTime
@@ -15570,6 +16274,153 @@ var AISDKPlugin = class extends BasePlugin {
15570
16274
  );
15571
16275
  }
15572
16276
  };
16277
+ function subscribeToHarnessAgentCreateSession() {
16278
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
16279
+ const parents = /* @__PURE__ */ new WeakMap();
16280
+ const handlers = {
16281
+ start: (event) => {
16282
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
16283
+ if (parent) {
16284
+ parents.set(event, parent);
16285
+ }
16286
+ },
16287
+ asyncEnd: (event) => {
16288
+ registerHarnessSessionParent(event.result, parents.get(event));
16289
+ parents.delete(event);
16290
+ },
16291
+ error: (event) => {
16292
+ parents.delete(event);
16293
+ }
16294
+ };
16295
+ channel2.subscribe(handlers);
16296
+ return () => channel2.unsubscribe(handlers);
16297
+ }
16298
+ function harnessContinuationParentFromEvent(event) {
16299
+ try {
16300
+ return harnessContinuationParent(event.arguments?.[0]?.session);
16301
+ } catch {
16302
+ return void 0;
16303
+ }
16304
+ }
16305
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
16306
+ const channel2 = continuationChannel.tracingChannel();
16307
+ const parents = /* @__PURE__ */ new WeakMap();
16308
+ const startTimes = /* @__PURE__ */ new WeakMap();
16309
+ const unbindParentStore = bindHarnessTurnParentToStart(
16310
+ channel2,
16311
+ harnessContinuationParentFromEvent
16312
+ );
16313
+ const handlers = {
16314
+ start: (event) => {
16315
+ const parent = harnessContinuationParentFromEvent(event);
16316
+ if (!parent) {
16317
+ return;
16318
+ }
16319
+ parents.set(event, parent);
16320
+ startTimes.set(event, getCurrentUnixTimestamp());
16321
+ try {
16322
+ const params = event.arguments?.[0];
16323
+ if (params) {
16324
+ prepareAISDKHarnessAgentInput(params, event.self);
16325
+ }
16326
+ } catch (error) {
16327
+ debugLogger.error(
16328
+ "Error preparing Harness continuation telemetry:",
16329
+ error
16330
+ );
16331
+ }
16332
+ },
16333
+ asyncEnd: (event) => {
16334
+ const parent = parents.get(event);
16335
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
16336
+ parents.delete(event);
16337
+ startTimes.delete(event);
16338
+ if (!parent) {
16339
+ return;
16340
+ }
16341
+ const endEvent = event;
16342
+ const span = {
16343
+ end: () => endHarnessTurn(parent),
16344
+ log: (update) => updateHarnessTurn(
16345
+ parent,
16346
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
16347
+ )
16348
+ };
16349
+ try {
16350
+ if (isAsyncIterable(endEvent.result)) {
16351
+ patchStreamIfNeeded(endEvent.result, {
16352
+ onComplete: (chunks) => {
16353
+ try {
16354
+ const { metadata, metrics, output } = aggregateAISDKChunks(
16355
+ chunks,
16356
+ endEvent.result,
16357
+ endEvent
16358
+ );
16359
+ span.log({
16360
+ ...metadata ? { metadata } : {},
16361
+ metrics,
16362
+ output
16363
+ });
16364
+ } catch (error) {
16365
+ debugLogger.error(
16366
+ "Error aggregating Harness continuation stream:",
16367
+ error
16368
+ );
16369
+ } finally {
16370
+ span.end();
16371
+ }
16372
+ },
16373
+ onError: (error) => {
16374
+ span.log({ error: toLoggedError(error), output: null });
16375
+ span.end();
16376
+ }
16377
+ });
16378
+ return;
16379
+ }
16380
+ if (patchAISDKStreamingResult({
16381
+ defaultDenyOutputPaths,
16382
+ endEvent,
16383
+ result: endEvent.result,
16384
+ resolvePromiseUsage: true,
16385
+ span,
16386
+ startTime
16387
+ })) {
16388
+ return;
16389
+ }
16390
+ finalizeAISDKChildTracing(endEvent);
16391
+ span.log({
16392
+ metrics: extractTokenMetrics(endEvent.result),
16393
+ output: processAISDKOutput(
16394
+ endEvent.result,
16395
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16396
+ )
16397
+ });
16398
+ span.end();
16399
+ } catch (error) {
16400
+ debugLogger.error("Error tracing Harness continuation:", error);
16401
+ span.end();
16402
+ }
16403
+ },
16404
+ error: (event) => {
16405
+ const parent = parents.get(event);
16406
+ parents.delete(event);
16407
+ startTimes.delete(event);
16408
+ if (!parent) {
16409
+ return;
16410
+ }
16411
+ updateHarnessTurn(parent, {
16412
+ error: toLoggedError(event.error),
16413
+ output: null
16414
+ });
16415
+ endHarnessTurn(parent);
16416
+ }
16417
+ };
16418
+ channel2.subscribe(handlers);
16419
+ return () => {
16420
+ unbindParentStore();
16421
+ channel2.unsubscribe(handlers);
16422
+ };
16423
+ }
15573
16424
  function subscribeToAISDKV7TelemetryDispatcher() {
15574
16425
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
15575
16426
  const telemetry = braintrustAISDKTelemetry();
@@ -16022,6 +16873,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
16022
16873
  metadata
16023
16874
  };
16024
16875
  }
16876
+ function prepareAISDKHarnessAgentInput(params, self, span) {
16877
+ if (span) {
16878
+ registerHarnessTurnSpan({
16879
+ session: params.session,
16880
+ span
16881
+ });
16882
+ }
16883
+ if (isObject(self)) {
16884
+ try {
16885
+ if (isObject(self.settings)) {
16886
+ const settings = self.settings;
16887
+ if (settings.telemetry === void 0) {
16888
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
16889
+ }
16890
+ }
16891
+ } catch {
16892
+ }
16893
+ }
16894
+ const selectedInput = {};
16895
+ if (params.prompt !== void 0) {
16896
+ selectedInput.prompt = params.prompt;
16897
+ }
16898
+ if (params.messages !== void 0) {
16899
+ selectedInput.messages = params.messages;
16900
+ }
16901
+ if (params.toolApprovalContinuations !== void 0) {
16902
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
16903
+ }
16904
+ const metadata = extractMetadataFromCallParams({}, self);
16905
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
16906
+ metadata.sessionId = params.session.sessionId;
16907
+ }
16908
+ if (isObject(self)) {
16909
+ if (typeof self.harnessId === "string") {
16910
+ metadata.harnessId = self.harnessId;
16911
+ }
16912
+ if (typeof self.permissionMode === "string") {
16913
+ metadata.permissionMode = self.permissionMode;
16914
+ }
16915
+ }
16916
+ return {
16917
+ input: processAISDKCallInput(selectedInput).input,
16918
+ metadata
16919
+ };
16920
+ }
16921
+ function harnessSessionFromArguments(args) {
16922
+ const params = args[0];
16923
+ return isObject(params) ? params.session : void 0;
16924
+ }
16025
16925
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
16026
16926
  registerWorkflowAgentWrapperSpan(span);
16027
16927
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -16469,19 +17369,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16469
17369
  activeEntry.openSpans.delete(span);
16470
17370
  }
16471
17371
  },
16472
- {
16473
- name: "doGenerate",
16474
- spanAttributes: {
16475
- type: "llm" /* LLM */
17372
+ withSpanInstrumentationName(
17373
+ {
17374
+ name: "doGenerate",
17375
+ spanAttributes: {
17376
+ type: "llm" /* LLM */
17377
+ },
17378
+ event: buildAISDKModelStartEvent(
17379
+ callOptions,
17380
+ activeEntry.baseMetadata,
17381
+ {
17382
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17383
+ }
17384
+ )
16476
17385
  },
16477
- event: buildAISDKModelStartEvent(
16478
- callOptions,
16479
- activeEntry.baseMetadata,
16480
- {
16481
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16482
- }
16483
- )
16484
- }
17386
+ INSTRUMENTATION_NAMES.AI_SDK
17387
+ )
16485
17388
  );
16486
17389
  };
16487
17390
  if (originalDoStream) {
@@ -16491,19 +17394,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16491
17394
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
16492
17395
  }
16493
17396
  closeOpenAISDKModelPatchSpans(activeEntry);
16494
- const span = activeEntry.parentSpan.startSpan({
16495
- name: "doStream",
16496
- spanAttributes: {
16497
- type: "llm" /* LLM */
16498
- },
16499
- event: buildAISDKModelStartEvent(
16500
- callOptions,
16501
- activeEntry.baseMetadata,
17397
+ const span = activeEntry.parentSpan.startSpan(
17398
+ withSpanInstrumentationName(
16502
17399
  {
16503
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16504
- }
17400
+ name: "doStream",
17401
+ spanAttributes: {
17402
+ type: "llm" /* LLM */
17403
+ },
17404
+ event: buildAISDKModelStartEvent(
17405
+ callOptions,
17406
+ activeEntry.baseMetadata,
17407
+ {
17408
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17409
+ }
17410
+ )
17411
+ },
17412
+ INSTRUMENTATION_NAMES.AI_SDK
16505
17413
  )
16506
- });
17414
+ );
16507
17415
  activeEntry.openSpans.add(span);
16508
17416
  const streamStartTime = getCurrentUnixTimestamp();
16509
17417
  const result = await withCurrent(
@@ -16675,12 +17583,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16675
17583
  }
16676
17584
  if (isAsyncGenerator(result)) {
16677
17585
  return (async function* () {
16678
- const span = activeEntry.parentSpan.startSpan({
16679
- name: activeEntry.name,
16680
- spanAttributes: {
16681
- type: "tool" /* TOOL */
16682
- }
16683
- });
17586
+ const span = activeEntry.parentSpan.startSpan(
17587
+ withSpanInstrumentationName(
17588
+ {
17589
+ name: activeEntry.name,
17590
+ spanAttributes: {
17591
+ type: "tool" /* TOOL */
17592
+ }
17593
+ },
17594
+ INSTRUMENTATION_NAMES.AI_SDK
17595
+ )
17596
+ );
16684
17597
  span.log({ input: serializeToolExecutionInput(args) });
16685
17598
  try {
16686
17599
  let lastValue;
@@ -16704,12 +17617,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16704
17617
  span.log({ output: awaitedResult });
16705
17618
  return awaitedResult;
16706
17619
  },
16707
- {
16708
- name: activeEntry.name,
16709
- spanAttributes: {
16710
- type: "tool" /* TOOL */
16711
- }
16712
- }
17620
+ withSpanInstrumentationName(
17621
+ {
17622
+ name: activeEntry.name,
17623
+ spanAttributes: {
17624
+ type: "tool" /* TOOL */
17625
+ }
17626
+ },
17627
+ INSTRUMENTATION_NAMES.AI_SDK
17628
+ )
16713
17629
  );
16714
17630
  };
16715
17631
  cleanup.push(() => {
@@ -16868,7 +17784,10 @@ function patchAISDKStreamingResult(args) {
16868
17784
  defaultDenyOutputPaths,
16869
17785
  endEvent,
16870
17786
  onComplete,
17787
+ onCancel,
17788
+ onError,
16871
17789
  result,
17790
+ resolvePromiseUsage,
16872
17791
  span,
16873
17792
  startTime
16874
17793
  } = args;
@@ -16878,14 +17797,43 @@ function patchAISDKStreamingResult(args) {
16878
17797
  const resultRecord = result;
16879
17798
  attachKnownResultPromiseHandlers(resultRecord);
16880
17799
  let finalized = false;
16881
- const finalize = () => {
17800
+ const finalize = (outcome) => {
16882
17801
  if (finalized) {
16883
17802
  return;
16884
17803
  }
16885
17804
  finalized = true;
16886
- finalizeAISDKChildTracing(endEvent);
16887
- span.end();
16888
- onComplete?.();
17805
+ try {
17806
+ finalizeAISDKChildTracing(endEvent);
17807
+ } catch (error) {
17808
+ debugLogger.error("Error finalizing AI SDK child tracing:", error);
17809
+ }
17810
+ try {
17811
+ span.end();
17812
+ } catch (error) {
17813
+ debugLogger.error("Error ending AI SDK streaming span:", error);
17814
+ }
17815
+ if (outcome && "error" in outcome) {
17816
+ try {
17817
+ onError?.(outcome.error);
17818
+ } catch (error) {
17819
+ debugLogger.error("Error in AI SDK streaming error hook:", error);
17820
+ }
17821
+ } else if (outcome) {
17822
+ try {
17823
+ onComplete?.(outcome);
17824
+ } catch (error) {
17825
+ debugLogger.error("Error in AI SDK streaming completion hook:", error);
17826
+ }
17827
+ } else {
17828
+ try {
17829
+ onCancel?.();
17830
+ } catch (error) {
17831
+ debugLogger.error(
17832
+ "Error in AI SDK streaming cancellation hook:",
17833
+ error
17834
+ );
17835
+ }
17836
+ }
16889
17837
  };
16890
17838
  let outputLogged = false;
16891
17839
  const logStreamingOutput = async (firstChunkTime) => {
@@ -16895,6 +17843,22 @@ function patchAISDKStreamingResult(args) {
16895
17843
  outputLogged = true;
16896
17844
  try {
16897
17845
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
17846
+ if (resolvePromiseUsage) {
17847
+ try {
17848
+ const resultRecord2 = result;
17849
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
17850
+ if (isPromiseLike(pendingUsage)) {
17851
+ const usage = await Promise.resolve(pendingUsage);
17852
+ if (isObject(usage)) {
17853
+ Object.assign(
17854
+ metrics,
17855
+ extractTokenMetrics({ usage })
17856
+ );
17857
+ }
17858
+ }
17859
+ } catch {
17860
+ }
17861
+ }
16898
17862
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
16899
17863
  metrics.time_to_first_token = firstChunkTime - startTime;
16900
17864
  }
@@ -16903,15 +17867,29 @@ function patchAISDKStreamingResult(args) {
16903
17867
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16904
17868
  );
16905
17869
  const metadata = buildResolvedMetadataPayload(result).metadata;
16906
- span.log({
16907
- output,
16908
- ...metadata ? { metadata } : {},
16909
- metrics
16910
- });
17870
+ try {
17871
+ span.log({
17872
+ output,
17873
+ ...metadata ? { metadata } : {},
17874
+ metrics
17875
+ });
17876
+ } catch (error) {
17877
+ debugLogger.error("Error logging AI SDK streaming output:", error);
17878
+ }
17879
+ finalize({ metrics, output });
16911
17880
  } catch (error) {
16912
- span.log({ error: toLoggedError(error) });
16913
- } finally {
16914
- finalize();
17881
+ const loggedError = toLoggedError(error);
17882
+ try {
17883
+ span.log({ error: loggedError });
17884
+ } catch (loggingError) {
17885
+ debugLogger.error(
17886
+ "Error logging AI SDK streaming failure:",
17887
+ loggingError
17888
+ );
17889
+ }
17890
+ finalize({
17891
+ error: error instanceof Error ? error : new Error(String(loggedError))
17892
+ });
16915
17893
  }
16916
17894
  };
16917
17895
  const createAsyncIterableHooks = () => {
@@ -16928,11 +17906,16 @@ function patchAISDKStreamingResult(args) {
16928
17906
  onError: (error) => {
16929
17907
  if (!outputLogged) {
16930
17908
  outputLogged = true;
16931
- span.log({
16932
- error: error.message
16933
- });
17909
+ try {
17910
+ span.log({ error });
17911
+ } catch (loggingError) {
17912
+ debugLogger.error(
17913
+ "Error logging AI SDK stream failure:",
17914
+ loggingError
17915
+ );
17916
+ }
16934
17917
  }
16935
- finalize();
17918
+ finalize({ error });
16936
17919
  },
16937
17920
  onCancel: finalize
16938
17921
  };
@@ -17018,8 +18001,18 @@ function patchAISDKStreamingResult(args) {
17018
18001
  }
17019
18002
  controller.enqueue(value);
17020
18003
  } catch (error) {
17021
- span.log({ error: toLoggedError(error) });
17022
- finalize();
18004
+ const loggedError = toLoggedError(error);
18005
+ try {
18006
+ span.log({ error: loggedError });
18007
+ } catch (loggingError) {
18008
+ debugLogger.error(
18009
+ "Error logging AI SDK base stream failure:",
18010
+ loggingError
18011
+ );
18012
+ }
18013
+ finalize({
18014
+ error: error instanceof Error ? error : new Error(String(loggedError))
18015
+ });
17023
18016
  controller.error(error);
17024
18017
  }
17025
18018
  },
@@ -17808,7 +18801,8 @@ var claudeAgentSDKChannels = defineChannels(
17808
18801
  channelName: "query",
17809
18802
  kind: "sync-stream"
17810
18803
  })
17811
- }
18804
+ },
18805
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
17812
18806
  );
17813
18807
 
17814
18808
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -17956,22 +18950,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
17956
18950
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
17957
18951
  const runWithResolvedParent = async () => {
17958
18952
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
17959
- const span = startSpan({
17960
- event: {
17961
- input: handlerArgs[0],
17962
- metadata: {
17963
- "claude_agent_sdk.raw_tool_name": rawToolName,
17964
- "gen_ai.tool.name": metadata.toolName,
17965
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
17966
- ...metadata.serverName && {
17967
- "mcp.server": metadata.serverName
17968
- }
17969
- }
17970
- },
17971
- name: spanName,
17972
- ...parent && { parent },
17973
- spanAttributes: { type: "tool" /* TOOL */ }
17974
- });
18953
+ const span = startSpan(
18954
+ withSpanInstrumentationName(
18955
+ {
18956
+ event: {
18957
+ input: handlerArgs[0],
18958
+ metadata: {
18959
+ "claude_agent_sdk.raw_tool_name": rawToolName,
18960
+ "gen_ai.tool.name": metadata.toolName,
18961
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
18962
+ ...metadata.serverName && {
18963
+ "mcp.server": metadata.serverName
18964
+ }
18965
+ }
18966
+ },
18967
+ name: spanName,
18968
+ ...parent && { parent },
18969
+ spanAttributes: { type: "tool" /* TOOL */ }
18970
+ },
18971
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
18972
+ )
18973
+ );
17975
18974
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
17976
18975
  const finalizeSuccess = (result) => {
17977
18976
  span.log({ output: result });
@@ -18296,14 +19295,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
18296
19295
  ).filter(
18297
19296
  (c) => c !== void 0
18298
19297
  );
18299
- const span = existingSpan ?? startSpan({
18300
- name: "anthropic.messages.create",
18301
- parent: parentSpan,
18302
- spanAttributes: {
18303
- type: "llm" /* LLM */
18304
- },
18305
- startTime
18306
- });
19298
+ const span = existingSpan ?? startSpan(
19299
+ withSpanInstrumentationName(
19300
+ {
19301
+ name: "anthropic.messages.create",
19302
+ parent: parentSpan,
19303
+ spanAttributes: {
19304
+ type: "llm" /* LLM */
19305
+ },
19306
+ startTime
19307
+ },
19308
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19309
+ )
19310
+ );
18307
19311
  span.log({
18308
19312
  input,
18309
19313
  metadata: model ? { model } : void 0,
@@ -18406,25 +19410,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
18406
19410
  return {};
18407
19411
  }
18408
19412
  const parsed = parseToolName(input.tool_name);
18409
- const toolSpan = startSpan({
18410
- event: {
18411
- input: input.tool_input,
18412
- metadata: {
18413
- "claude_agent_sdk.cwd": input.cwd,
18414
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
18415
- "claude_agent_sdk.session_id": input.session_id,
18416
- "gen_ai.tool.call.id": toolUseID,
18417
- "gen_ai.tool.name": parsed.toolName,
18418
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
18419
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
18420
- }
18421
- },
18422
- name: parsed.displayName,
18423
- parent: await resolveParentSpan(toolUseID, {
18424
- agentId: input.agent_id
18425
- }),
18426
- spanAttributes: { type: "tool" /* TOOL */ }
18427
- });
19413
+ const toolSpan = startSpan(
19414
+ withSpanInstrumentationName(
19415
+ {
19416
+ event: {
19417
+ input: input.tool_input,
19418
+ metadata: {
19419
+ "claude_agent_sdk.cwd": input.cwd,
19420
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
19421
+ "claude_agent_sdk.session_id": input.session_id,
19422
+ "gen_ai.tool.call.id": toolUseID,
19423
+ "gen_ai.tool.name": parsed.toolName,
19424
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
19425
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
19426
+ }
19427
+ },
19428
+ name: parsed.displayName,
19429
+ parent: await resolveParentSpan(toolUseID, {
19430
+ agentId: input.agent_id
19431
+ }),
19432
+ spanAttributes: { type: "tool" /* TOOL */ }
19433
+ },
19434
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19435
+ )
19436
+ );
18428
19437
  activeToolSpans.set(toolUseID, toolSpan);
18429
19438
  return {};
18430
19439
  };
@@ -18790,14 +19799,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
18790
19799
  const spanName = formatSubAgentSpanName(details);
18791
19800
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
18792
19801
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
18793
- const subAgentSpan = startSpan({
18794
- event: {
18795
- metadata: subAgentDetailsToMetadata(details)
18796
- },
18797
- name: spanName,
18798
- parent: parentSpan,
18799
- spanAttributes: { type: "task" /* TASK */ }
18800
- });
19802
+ const subAgentSpan = startSpan(
19803
+ withSpanInstrumentationName(
19804
+ {
19805
+ event: {
19806
+ metadata: subAgentDetailsToMetadata(details)
19807
+ },
19808
+ name: spanName,
19809
+ parent: parentSpan,
19810
+ spanAttributes: { type: "task" /* TASK */ }
19811
+ },
19812
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19813
+ )
19814
+ );
18801
19815
  subAgentSpans.set(parentToolUseId, subAgentSpan);
18802
19816
  return subAgentSpan;
18803
19817
  }
@@ -18818,14 +19832,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
18818
19832
  );
18819
19833
  llmParentSpan = await subAgentSpan.export();
18820
19834
  }
18821
- const llmSpan = startSpan({
18822
- name: "anthropic.messages.create",
18823
- parent: llmParentSpan,
18824
- spanAttributes: {
18825
- type: "llm" /* LLM */
18826
- },
18827
- startTime
18828
- });
19835
+ const llmSpan = startSpan(
19836
+ withSpanInstrumentationName(
19837
+ {
19838
+ name: "anthropic.messages.create",
19839
+ parent: llmParentSpan,
19840
+ spanAttributes: {
19841
+ type: "llm" /* LLM */
19842
+ },
19843
+ startTime
19844
+ },
19845
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19846
+ )
19847
+ );
18829
19848
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
18830
19849
  return llmSpan;
18831
19850
  }
@@ -19094,12 +20113,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19094
20113
  }
19095
20114
  })();
19096
20115
  }
19097
- const span = startSpan({
19098
- name: "Claude Agent",
19099
- spanAttributes: {
19100
- type: "task" /* TASK */
19101
- }
19102
- });
20116
+ const span = startSpan(
20117
+ withSpanInstrumentationName(
20118
+ {
20119
+ name: "Claude Agent",
20120
+ spanAttributes: {
20121
+ type: "task" /* TASK */
20122
+ }
20123
+ },
20124
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
20125
+ )
20126
+ );
19103
20127
  const startTime = getCurrentUnixTimestamp();
19104
20128
  try {
19105
20129
  span.log({
@@ -19264,24 +20288,28 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19264
20288
  };
19265
20289
 
19266
20290
  // src/instrumentation/plugins/cursor-sdk-channels.ts
19267
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
19268
- create: channel({
19269
- channelName: "Agent.create",
19270
- kind: "async"
19271
- }),
19272
- resume: channel({
19273
- channelName: "Agent.resume",
19274
- kind: "async"
19275
- }),
19276
- prompt: channel({
19277
- channelName: "Agent.prompt",
19278
- kind: "async"
19279
- }),
19280
- send: channel({
19281
- channelName: "agent.send",
19282
- kind: "async"
19283
- })
19284
- });
20291
+ var cursorSDKChannels = defineChannels(
20292
+ "@cursor/sdk",
20293
+ {
20294
+ create: channel({
20295
+ channelName: "Agent.create",
20296
+ kind: "async"
20297
+ }),
20298
+ resume: channel({
20299
+ channelName: "Agent.resume",
20300
+ kind: "async"
20301
+ }),
20302
+ prompt: channel({
20303
+ channelName: "Agent.prompt",
20304
+ kind: "async"
20305
+ }),
20306
+ send: channel({
20307
+ channelName: "agent.send",
20308
+ kind: "async"
20309
+ })
20310
+ },
20311
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
20312
+ );
19285
20313
 
19286
20314
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
19287
20315
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -19333,10 +20361,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19333
20361
  provider: "cursor",
19334
20362
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19335
20363
  };
19336
- const span = startSpan({
19337
- name: "Cursor Agent",
19338
- spanAttributes: { type: "task" /* TASK */ }
19339
- });
20364
+ const span = startSpan(
20365
+ withSpanInstrumentationName(
20366
+ {
20367
+ name: "Cursor Agent",
20368
+ spanAttributes: { type: "task" /* TASK */ }
20369
+ },
20370
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20371
+ )
20372
+ );
19340
20373
  const startTime = getCurrentUnixTimestamp();
19341
20374
  safeLog2(span, {
19342
20375
  input: sanitizeUserMessage(message),
@@ -19398,10 +20431,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19398
20431
  provider: "cursor",
19399
20432
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19400
20433
  };
19401
- const span = startSpan({
19402
- name: "Cursor Agent",
19403
- spanAttributes: { type: "task" /* TASK */ }
19404
- });
20434
+ const span = startSpan(
20435
+ withSpanInstrumentationName(
20436
+ {
20437
+ name: "Cursor Agent",
20438
+ spanAttributes: { type: "task" /* TASK */ }
20439
+ },
20440
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20441
+ )
20442
+ );
19405
20443
  const startTime = getCurrentUnixTimestamp();
19406
20444
  safeLog2(span, {
19407
20445
  input: sanitizeUserMessage(message),
@@ -19848,29 +20886,39 @@ async function startToolSpan(state, args) {
19848
20886
  if (args.truncated?.result !== void 0) {
19849
20887
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
19850
20888
  }
19851
- const span = startSpan({
19852
- event: {
19853
- input: args.args,
19854
- metadata
19855
- },
19856
- name: `tool: ${name}`,
19857
- parent: await state.span.export(),
19858
- spanAttributes: { type: "tool" /* TOOL */ }
19859
- });
20889
+ const span = startSpan(
20890
+ withSpanInstrumentationName(
20891
+ {
20892
+ event: {
20893
+ input: args.args,
20894
+ metadata
20895
+ },
20896
+ name: `tool: ${name}`,
20897
+ parent: await state.span.export(),
20898
+ spanAttributes: { type: "tool" /* TOOL */ }
20899
+ },
20900
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20901
+ )
20902
+ );
19860
20903
  let subAgentSpan;
19861
20904
  if (isSubAgentToolName(name)) {
19862
- subAgentSpan = startSpan({
19863
- event: {
19864
- input: args.args,
19865
- metadata: {
19866
- "cursor_sdk.subagent.tool_call_id": args.callId,
19867
- "gen_ai.tool.name": name
19868
- }
19869
- },
19870
- name: formatSubAgentSpanName2(args.toolCall, args.args),
19871
- parent: await span.export(),
19872
- spanAttributes: { type: "task" /* TASK */ }
19873
- });
20905
+ subAgentSpan = startSpan(
20906
+ withSpanInstrumentationName(
20907
+ {
20908
+ event: {
20909
+ input: args.args,
20910
+ metadata: {
20911
+ "cursor_sdk.subagent.tool_call_id": args.callId,
20912
+ "gen_ai.tool.name": name
20913
+ }
20914
+ },
20915
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
20916
+ parent: await span.export(),
20917
+ spanAttributes: { type: "task" /* TASK */ }
20918
+ },
20919
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20920
+ )
20921
+ );
19874
20922
  }
19875
20923
  return { span, subAgentSpan };
19876
20924
  }
@@ -20129,24 +21177,28 @@ function cleanMetrics2(metrics) {
20129
21177
  }
20130
21178
 
20131
21179
  // src/instrumentation/plugins/openai-agents-channels.ts
20132
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
20133
- onTraceStart: channel({
20134
- channelName: "tracing.processor.onTraceStart",
20135
- kind: "async"
20136
- }),
20137
- onTraceEnd: channel({
20138
- channelName: "tracing.processor.onTraceEnd",
20139
- kind: "async"
20140
- }),
20141
- onSpanStart: channel({
20142
- channelName: "tracing.processor.onSpanStart",
20143
- kind: "async"
20144
- }),
20145
- onSpanEnd: channel({
20146
- channelName: "tracing.processor.onSpanEnd",
20147
- kind: "async"
20148
- })
20149
- });
21180
+ var openAIAgentsCoreChannels = defineChannels(
21181
+ "@openai/agents-core",
21182
+ {
21183
+ onTraceStart: channel({
21184
+ channelName: "tracing.processor.onTraceStart",
21185
+ kind: "async"
21186
+ }),
21187
+ onTraceEnd: channel({
21188
+ channelName: "tracing.processor.onTraceEnd",
21189
+ kind: "async"
21190
+ }),
21191
+ onSpanStart: channel({
21192
+ channelName: "tracing.processor.onSpanStart",
21193
+ kind: "async"
21194
+ }),
21195
+ onSpanEnd: channel({
21196
+ channelName: "tracing.processor.onSpanEnd",
21197
+ kind: "async"
21198
+ })
21199
+ },
21200
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
21201
+ );
20150
21202
 
20151
21203
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
20152
21204
  function isSpanData(spanData, type) {
@@ -20262,16 +21314,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20262
21314
  this.evictOldestTrace();
20263
21315
  }
20264
21316
  const current = currentSpan();
20265
- const span = current && current !== NOOP_SPAN ? current.startSpan({
20266
- name: trace.name,
20267
- type: "task" /* TASK */
20268
- }) : this.logger ? this.logger.startSpan({
20269
- name: trace.name,
20270
- type: "task" /* TASK */
20271
- }) : startSpan({
20272
- name: trace.name,
20273
- type: "task" /* TASK */
20274
- });
21317
+ const spanArgs = withSpanInstrumentationName(
21318
+ {
21319
+ name: trace.name,
21320
+ type: "task" /* TASK */
21321
+ },
21322
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21323
+ );
21324
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
21325
+ withSpanInstrumentationName(
21326
+ spanArgs,
21327
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21328
+ )
21329
+ );
20275
21330
  span.log({
20276
21331
  input: "Agent workflow started",
20277
21332
  metadata: {
@@ -20322,10 +21377,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20322
21377
  if (!parentSpan) {
20323
21378
  return Promise.resolve();
20324
21379
  }
20325
- const childSpan = parentSpan.startSpan({
20326
- name: spanNameFromAgents(span),
20327
- type: spanTypeFromAgents(span)
20328
- });
21380
+ const childSpan = parentSpan.startSpan(
21381
+ withSpanInstrumentationName(
21382
+ {
21383
+ name: spanNameFromAgents(span),
21384
+ type: spanTypeFromAgents(span)
21385
+ },
21386
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21387
+ )
21388
+ );
20329
21389
  traceData.childSpans.set(span.spanId, childSpan);
20330
21390
  return Promise.resolve();
20331
21391
  }
@@ -20590,24 +21650,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
20590
21650
  };
20591
21651
 
20592
21652
  // src/instrumentation/plugins/google-genai-channels.ts
20593
- var googleGenAIChannels = defineChannels("@google/genai", {
20594
- generateContent: channel({
20595
- channelName: "models.generateContent",
20596
- kind: "async"
20597
- }),
20598
- generateContentStream: channel({
20599
- channelName: "models.generateContentStream",
20600
- kind: "async"
20601
- }),
20602
- embedContent: channel({
20603
- channelName: "models.embedContent",
20604
- kind: "async"
20605
- }),
20606
- interactionsCreate: channel({
20607
- channelName: "interactions.create",
20608
- kind: "async"
20609
- })
20610
- });
21653
+ var googleGenAIChannels = defineChannels(
21654
+ "@google/genai",
21655
+ {
21656
+ generateContent: channel({
21657
+ channelName: "models.generateContent",
21658
+ kind: "async"
21659
+ }),
21660
+ generateContentStream: channel({
21661
+ channelName: "models.generateContentStream",
21662
+ kind: "async"
21663
+ }),
21664
+ embedContent: channel({
21665
+ channelName: "models.embedContent",
21666
+ kind: "async"
21667
+ }),
21668
+ interactionsCreate: channel({
21669
+ channelName: "interactions.create",
21670
+ kind: "async"
21671
+ })
21672
+ },
21673
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
21674
+ );
20611
21675
 
20612
21676
  // src/instrumentation/plugins/google-genai-plugin.ts
20613
21677
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -20645,13 +21709,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20645
21709
  const params = event.arguments[0];
20646
21710
  const input = serializeGenerateContentInput(params);
20647
21711
  const metadata = extractGenerateContentMetadata(params);
20648
- const span = startSpan({
20649
- name: "generate_content",
20650
- spanAttributes: {
20651
- type: "llm" /* LLM */
20652
- },
20653
- event: createWrapperParityEvent({ input, metadata })
20654
- });
21712
+ const span = startSpan(
21713
+ withSpanInstrumentationName(
21714
+ {
21715
+ name: "generate_content",
21716
+ spanAttributes: {
21717
+ type: "llm" /* LLM */
21718
+ },
21719
+ event: createWrapperParityEvent({ input, metadata })
21720
+ },
21721
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21722
+ )
21723
+ );
20655
21724
  return {
20656
21725
  span,
20657
21726
  startTime: getCurrentUnixTimestamp()
@@ -20664,13 +21733,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20664
21733
  const params = event.arguments[0];
20665
21734
  const input = serializeGenerateContentInput(params);
20666
21735
  const metadata = extractGenerateContentMetadata(params);
20667
- const span = startSpan({
20668
- name: "generate_content",
20669
- spanAttributes: {
20670
- type: "llm" /* LLM */
20671
- },
20672
- event: createWrapperParityEvent({ input, metadata })
20673
- });
21736
+ const span = startSpan(
21737
+ withSpanInstrumentationName(
21738
+ {
21739
+ name: "generate_content",
21740
+ spanAttributes: {
21741
+ type: "llm" /* LLM */
21742
+ },
21743
+ event: createWrapperParityEvent({ input, metadata })
21744
+ },
21745
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21746
+ )
21747
+ );
20674
21748
  return {
20675
21749
  span,
20676
21750
  startTime: getCurrentUnixTimestamp()
@@ -20746,13 +21820,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20746
21820
  const params = event.arguments[0];
20747
21821
  const input = serializeEmbedContentInput(params);
20748
21822
  const metadata = extractEmbedContentMetadata(params);
20749
- const span = startSpan({
20750
- name: "embed_content",
20751
- spanAttributes: {
20752
- type: "llm" /* LLM */
20753
- },
20754
- event: createWrapperParityEvent({ input, metadata })
20755
- });
21823
+ const span = startSpan(
21824
+ withSpanInstrumentationName(
21825
+ {
21826
+ name: "embed_content",
21827
+ spanAttributes: {
21828
+ type: "llm" /* LLM */
21829
+ },
21830
+ event: createWrapperParityEvent({ input, metadata })
21831
+ },
21832
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21833
+ )
21834
+ );
20756
21835
  return {
20757
21836
  span,
20758
21837
  startTime: getCurrentUnixTimestamp()
@@ -20765,13 +21844,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20765
21844
  const params = event.arguments[0];
20766
21845
  const input = serializeEmbedContentInput(params);
20767
21846
  const metadata = extractEmbedContentMetadata(params);
20768
- const span = startSpan({
20769
- name: "embed_content",
20770
- spanAttributes: {
20771
- type: "llm" /* LLM */
20772
- },
20773
- event: createWrapperParityEvent({ input, metadata })
20774
- });
21847
+ const span = startSpan(
21848
+ withSpanInstrumentationName(
21849
+ {
21850
+ name: "embed_content",
21851
+ spanAttributes: {
21852
+ type: "llm" /* LLM */
21853
+ },
21854
+ event: createWrapperParityEvent({ input, metadata })
21855
+ },
21856
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21857
+ )
21858
+ );
20775
21859
  return {
20776
21860
  span,
20777
21861
  startTime: getCurrentUnixTimestamp()
@@ -20882,16 +21966,21 @@ function patchGoogleGenAIStreamingResult(args) {
20882
21966
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
20883
21967
  const ensureSpan = () => {
20884
21968
  if (!span) {
20885
- span = startSpan({
20886
- name: "generate_content_stream",
20887
- spanAttributes: {
20888
- type: "llm" /* LLM */
20889
- },
20890
- event: {
20891
- input,
20892
- metadata
20893
- }
20894
- });
21969
+ span = startSpan(
21970
+ withSpanInstrumentationName(
21971
+ {
21972
+ name: "generate_content_stream",
21973
+ spanAttributes: {
21974
+ type: "llm" /* LLM */
21975
+ },
21976
+ event: {
21977
+ input,
21978
+ metadata
21979
+ }
21980
+ },
21981
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21982
+ )
21983
+ );
20895
21984
  }
20896
21985
  return span;
20897
21986
  };
@@ -21659,28 +22748,32 @@ function tryToDict(obj) {
21659
22748
  }
21660
22749
 
21661
22750
  // src/instrumentation/plugins/huggingface-channels.ts
21662
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
21663
- chatCompletion: channel({
21664
- channelName: "chatCompletion",
21665
- kind: "async"
21666
- }),
21667
- chatCompletionStream: channel({
21668
- channelName: "chatCompletionStream",
21669
- kind: "sync-stream"
21670
- }),
21671
- textGeneration: channel({
21672
- channelName: "textGeneration",
21673
- kind: "async"
21674
- }),
21675
- textGenerationStream: channel({
21676
- channelName: "textGenerationStream",
21677
- kind: "sync-stream"
21678
- }),
21679
- featureExtraction: channel({
21680
- channelName: "featureExtraction",
21681
- kind: "async"
21682
- })
21683
- });
22751
+ var huggingFaceChannels = defineChannels(
22752
+ "@huggingface/inference",
22753
+ {
22754
+ chatCompletion: channel({
22755
+ channelName: "chatCompletion",
22756
+ kind: "async"
22757
+ }),
22758
+ chatCompletionStream: channel({
22759
+ channelName: "chatCompletionStream",
22760
+ kind: "sync-stream"
22761
+ }),
22762
+ textGeneration: channel({
22763
+ channelName: "textGeneration",
22764
+ kind: "async"
22765
+ }),
22766
+ textGenerationStream: channel({
22767
+ channelName: "textGenerationStream",
22768
+ kind: "sync-stream"
22769
+ }),
22770
+ featureExtraction: channel({
22771
+ channelName: "featureExtraction",
22772
+ kind: "async"
22773
+ })
22774
+ },
22775
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
22776
+ );
21684
22777
 
21685
22778
  // src/instrumentation/plugins/huggingface-plugin.ts
21686
22779
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -22107,20 +23200,24 @@ function extractTextGenerationStreamMetadata(chunks) {
22107
23200
  }
22108
23201
 
22109
23202
  // src/instrumentation/plugins/openrouter-agent-channels.ts
22110
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
22111
- callModel: channel({
22112
- channelName: "callModel",
22113
- kind: "sync-stream"
22114
- }),
22115
- callModelTurn: channel({
22116
- channelName: "callModel.turn",
22117
- kind: "async"
22118
- }),
22119
- toolExecute: channel({
22120
- channelName: "tool.execute",
22121
- kind: "async"
22122
- })
22123
- });
23203
+ var openRouterAgentChannels = defineChannels(
23204
+ "@openrouter/agent",
23205
+ {
23206
+ callModel: channel({
23207
+ channelName: "callModel",
23208
+ kind: "sync-stream"
23209
+ }),
23210
+ callModelTurn: channel({
23211
+ channelName: "callModel.turn",
23212
+ kind: "async"
23213
+ }),
23214
+ toolExecute: channel({
23215
+ channelName: "tool.execute",
23216
+ kind: "async"
23217
+ })
23218
+ },
23219
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
23220
+ );
22124
23221
 
22125
23222
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
22126
23223
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -22890,38 +23987,40 @@ function normalizeError(error) {
22890
23987
  }
22891
23988
 
22892
23989
  // src/instrumentation/plugins/openrouter-channels.ts
22893
- var openRouterChannels = defineChannels("@openrouter/sdk", {
22894
- chatSend: channel({
22895
- channelName: "chat.send",
22896
- kind: "async"
22897
- }),
22898
- embeddingsGenerate: channel({
22899
- channelName: "embeddings.generate",
22900
- kind: "async"
22901
- }),
22902
- rerankRerank: channel(
22903
- {
23990
+ var openRouterChannels = defineChannels(
23991
+ "@openrouter/sdk",
23992
+ {
23993
+ chatSend: channel({
23994
+ channelName: "chat.send",
23995
+ kind: "async"
23996
+ }),
23997
+ embeddingsGenerate: channel({
23998
+ channelName: "embeddings.generate",
23999
+ kind: "async"
24000
+ }),
24001
+ rerankRerank: channel({
22904
24002
  channelName: "rerank.rerank",
22905
24003
  kind: "async"
22906
- }
22907
- ),
22908
- betaResponsesSend: channel({
22909
- channelName: "beta.responses.send",
22910
- kind: "async"
22911
- }),
22912
- callModel: channel({
22913
- channelName: "callModel",
22914
- kind: "sync-stream"
22915
- }),
22916
- callModelTurn: channel({
22917
- channelName: "callModel.turn",
22918
- kind: "async"
22919
- }),
22920
- toolExecute: channel({
22921
- channelName: "tool.execute",
22922
- kind: "async"
22923
- })
22924
- });
24004
+ }),
24005
+ betaResponsesSend: channel({
24006
+ channelName: "beta.responses.send",
24007
+ kind: "async"
24008
+ }),
24009
+ callModel: channel({
24010
+ channelName: "callModel",
24011
+ kind: "sync-stream"
24012
+ }),
24013
+ callModelTurn: channel({
24014
+ channelName: "callModel.turn",
24015
+ kind: "async"
24016
+ }),
24017
+ toolExecute: channel({
24018
+ channelName: "tool.execute",
24019
+ kind: "async"
24020
+ })
24021
+ },
24022
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
24023
+ );
22925
24024
 
22926
24025
  // src/instrumentation/plugins/openrouter-plugin.ts
22927
24026
  var OpenRouterPlugin = class extends BasePlugin {
@@ -23961,52 +25060,56 @@ function normalizeError2(error) {
23961
25060
  }
23962
25061
 
23963
25062
  // src/instrumentation/plugins/mistral-channels.ts
23964
- var mistralChannels = defineChannels("@mistralai/mistralai", {
23965
- chatComplete: channel({
23966
- channelName: "chat.complete",
23967
- kind: "async"
23968
- }),
23969
- chatStream: channel({
23970
- channelName: "chat.stream",
23971
- kind: "async"
23972
- }),
23973
- embeddingsCreate: channel({
23974
- channelName: "embeddings.create",
23975
- kind: "async"
23976
- }),
23977
- classifiersModerate: channel({
23978
- channelName: "classifiers.moderate",
23979
- kind: "async"
23980
- }),
23981
- classifiersModerateChat: channel({
23982
- channelName: "classifiers.moderateChat",
23983
- kind: "async"
23984
- }),
23985
- classifiersClassify: channel({
23986
- channelName: "classifiers.classify",
23987
- kind: "async"
23988
- }),
23989
- classifiersClassifyChat: channel({
23990
- channelName: "classifiers.classifyChat",
23991
- kind: "async"
23992
- }),
23993
- fimComplete: channel({
23994
- channelName: "fim.complete",
23995
- kind: "async"
23996
- }),
23997
- fimStream: channel({
23998
- channelName: "fim.stream",
23999
- kind: "async"
24000
- }),
24001
- agentsComplete: channel({
24002
- channelName: "agents.complete",
24003
- kind: "async"
24004
- }),
24005
- agentsStream: channel({
24006
- channelName: "agents.stream",
24007
- kind: "async"
24008
- })
24009
- });
25063
+ var mistralChannels = defineChannels(
25064
+ "@mistralai/mistralai",
25065
+ {
25066
+ chatComplete: channel({
25067
+ channelName: "chat.complete",
25068
+ kind: "async"
25069
+ }),
25070
+ chatStream: channel({
25071
+ channelName: "chat.stream",
25072
+ kind: "async"
25073
+ }),
25074
+ embeddingsCreate: channel({
25075
+ channelName: "embeddings.create",
25076
+ kind: "async"
25077
+ }),
25078
+ classifiersModerate: channel({
25079
+ channelName: "classifiers.moderate",
25080
+ kind: "async"
25081
+ }),
25082
+ classifiersModerateChat: channel({
25083
+ channelName: "classifiers.moderateChat",
25084
+ kind: "async"
25085
+ }),
25086
+ classifiersClassify: channel({
25087
+ channelName: "classifiers.classify",
25088
+ kind: "async"
25089
+ }),
25090
+ classifiersClassifyChat: channel({
25091
+ channelName: "classifiers.classifyChat",
25092
+ kind: "async"
25093
+ }),
25094
+ fimComplete: channel({
25095
+ channelName: "fim.complete",
25096
+ kind: "async"
25097
+ }),
25098
+ fimStream: channel({
25099
+ channelName: "fim.stream",
25100
+ kind: "async"
25101
+ }),
25102
+ agentsComplete: channel({
25103
+ channelName: "agents.complete",
25104
+ kind: "async"
25105
+ }),
25106
+ agentsStream: channel({
25107
+ channelName: "agents.stream",
25108
+ kind: "async"
25109
+ })
25110
+ },
25111
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
25112
+ );
24010
25113
 
24011
25114
  // src/instrumentation/plugins/mistral-plugin.ts
24012
25115
  var MistralPlugin = class extends BasePlugin {
@@ -24626,20 +25729,24 @@ function aggregateMistralStreamChunks(chunks) {
24626
25729
  }
24627
25730
 
24628
25731
  // src/instrumentation/plugins/google-adk-channels.ts
24629
- var googleADKChannels = defineChannels("@google/adk", {
24630
- runnerRunAsync: channel({
24631
- channelName: "runner.runAsync",
24632
- kind: "sync-stream"
24633
- }),
24634
- agentRunAsync: channel({
24635
- channelName: "agent.runAsync",
24636
- kind: "sync-stream"
24637
- }),
24638
- toolRunAsync: channel({
24639
- channelName: "tool.runAsync",
24640
- kind: "async"
24641
- })
24642
- });
25732
+ var googleADKChannels = defineChannels(
25733
+ "@google/adk",
25734
+ {
25735
+ runnerRunAsync: channel({
25736
+ channelName: "runner.runAsync",
25737
+ kind: "sync-stream"
25738
+ }),
25739
+ agentRunAsync: channel({
25740
+ channelName: "agent.runAsync",
25741
+ kind: "sync-stream"
25742
+ }),
25743
+ toolRunAsync: channel({
25744
+ channelName: "tool.runAsync",
25745
+ kind: "async"
25746
+ })
25747
+ },
25748
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
25749
+ );
24643
25750
 
24644
25751
  // src/instrumentation/plugins/google-adk-plugin.ts
24645
25752
  var GoogleADKPlugin = class extends BasePlugin {
@@ -24664,12 +25771,17 @@ var GoogleADKPlugin = class extends BasePlugin {
24664
25771
  const createState = (event) => {
24665
25772
  const params = event.arguments[0] ?? {};
24666
25773
  const contextKey = extractRunnerContextKey(params);
24667
- const span = startSpan({
24668
- name: "Google ADK Runner",
24669
- spanAttributes: {
24670
- type: "task" /* TASK */
24671
- }
24672
- });
25774
+ const span = startSpan(
25775
+ withSpanInstrumentationName(
25776
+ {
25777
+ name: "Google ADK Runner",
25778
+ spanAttributes: {
25779
+ type: "task" /* TASK */
25780
+ }
25781
+ },
25782
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25783
+ )
25784
+ );
24673
25785
  const startTime = getCurrentUnixTimestamp();
24674
25786
  try {
24675
25787
  const metadata = extractRunnerMetadata(params);
@@ -24755,18 +25867,23 @@ var GoogleADKPlugin = class extends BasePlugin {
24755
25867
  this.activeRunnerSpans
24756
25868
  );
24757
25869
  const contextKey = extractInvocationContextKey(parentContext);
24758
- const span = startSpan({
24759
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
24760
- spanAttributes: {
24761
- type: "task" /* TASK */
24762
- },
24763
- ...runnerParentSpan ? {
24764
- parentSpanIds: {
24765
- spanId: runnerParentSpan.spanId,
24766
- rootSpanId: runnerParentSpan.rootSpanId
24767
- }
24768
- } : {}
24769
- });
25870
+ const span = startSpan(
25871
+ withSpanInstrumentationName(
25872
+ {
25873
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
25874
+ spanAttributes: {
25875
+ type: "task" /* TASK */
25876
+ },
25877
+ ...runnerParentSpan ? {
25878
+ parentSpanIds: {
25879
+ spanId: runnerParentSpan.spanId,
25880
+ rootSpanId: runnerParentSpan.rootSpanId
25881
+ }
25882
+ } : {}
25883
+ },
25884
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25885
+ )
25886
+ );
24770
25887
  const startTime = getCurrentUnixTimestamp();
24771
25888
  try {
24772
25889
  const metadata = {
@@ -24859,22 +25976,27 @@ var GoogleADKPlugin = class extends BasePlugin {
24859
25976
  this.activeAgentSpans,
24860
25977
  this.activeRunnerSpans
24861
25978
  );
24862
- const createSpan = () => startSpan({
24863
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
24864
- spanAttributes: {
24865
- type: "tool" /* TOOL */
24866
- },
24867
- event: {
24868
- input: req.args,
24869
- metadata: {
24870
- provider: "google-adk",
24871
- ...toolName && { "google_adk.tool_name": toolName },
24872
- ...extractToolCallId(req) && {
24873
- "google_adk.tool_call_id": extractToolCallId(req)
25979
+ const createSpan = () => startSpan(
25980
+ withSpanInstrumentationName(
25981
+ {
25982
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
25983
+ spanAttributes: {
25984
+ type: "tool" /* TOOL */
25985
+ },
25986
+ event: {
25987
+ input: req.args,
25988
+ metadata: {
25989
+ provider: "google-adk",
25990
+ ...toolName && { "google_adk.tool_name": toolName },
25991
+ ...extractToolCallId(req) && {
25992
+ "google_adk.tool_call_id": extractToolCallId(req)
25993
+ }
25994
+ }
24874
25995
  }
24875
- }
24876
- }
24877
- });
25996
+ },
25997
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25998
+ )
25999
+ );
24878
26000
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
24879
26001
  const startTime = getCurrentUnixTimestamp();
24880
26002
  states.set(event, { span, startTime });
@@ -25273,24 +26395,28 @@ function cleanMetrics4(metrics) {
25273
26395
  }
25274
26396
 
25275
26397
  // src/instrumentation/plugins/cohere-channels.ts
25276
- var cohereChannels = defineChannels("cohere-ai", {
25277
- chat: channel({
25278
- channelName: "chat",
25279
- kind: "async"
25280
- }),
25281
- chatStream: channel({
25282
- channelName: "chatStream",
25283
- kind: "async"
25284
- }),
25285
- embed: channel({
25286
- channelName: "embed",
25287
- kind: "async"
25288
- }),
25289
- rerank: channel({
25290
- channelName: "rerank",
25291
- kind: "async"
25292
- })
25293
- });
26398
+ var cohereChannels = defineChannels(
26399
+ "cohere-ai",
26400
+ {
26401
+ chat: channel({
26402
+ channelName: "chat",
26403
+ kind: "async"
26404
+ }),
26405
+ chatStream: channel({
26406
+ channelName: "chatStream",
26407
+ kind: "async"
26408
+ }),
26409
+ embed: channel({
26410
+ channelName: "embed",
26411
+ kind: "async"
26412
+ }),
26413
+ rerank: channel({
26414
+ channelName: "rerank",
26415
+ kind: "async"
26416
+ })
26417
+ },
26418
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
26419
+ );
25294
26420
 
25295
26421
  // src/instrumentation/plugins/cohere-plugin.ts
25296
26422
  var CoherePlugin = class extends BasePlugin {
@@ -25959,18 +27085,20 @@ function aggregateCohereChatStreamChunks(chunks) {
25959
27085
  }
25960
27086
 
25961
27087
  // src/instrumentation/plugins/groq-channels.ts
25962
- var groqChannels = defineChannels("groq-sdk", {
25963
- chatCompletionsCreate: channel({
25964
- channelName: "chat.completions.create",
25965
- kind: "async"
25966
- }),
25967
- embeddingsCreate: channel(
25968
- {
27088
+ var groqChannels = defineChannels(
27089
+ "groq-sdk",
27090
+ {
27091
+ chatCompletionsCreate: channel({
27092
+ channelName: "chat.completions.create",
27093
+ kind: "async"
27094
+ }),
27095
+ embeddingsCreate: channel({
25969
27096
  channelName: "embeddings.create",
25970
27097
  kind: "async"
25971
- }
25972
- )
25973
- });
27098
+ })
27099
+ },
27100
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
27101
+ );
25974
27102
 
25975
27103
  // src/instrumentation/plugins/groq-plugin.ts
25976
27104
  var GroqPlugin = class extends BasePlugin {
@@ -26073,15 +27201,27 @@ var clientSendChannel = channel({
26073
27201
  channelName: "client.send",
26074
27202
  kind: "async"
26075
27203
  });
26076
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
26077
- clientSend: clientSendChannel
26078
- });
26079
- var smithyCoreChannels = defineChannels("@smithy/core", {
26080
- clientSend: clientSendChannel
26081
- });
26082
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
26083
- clientSend: clientSendChannel
26084
- });
27204
+ var bedrockRuntimeChannels = defineChannels(
27205
+ "aws-bedrock-runtime",
27206
+ {
27207
+ clientSend: clientSendChannel
27208
+ },
27209
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27210
+ );
27211
+ var smithyCoreChannels = defineChannels(
27212
+ "@smithy/core",
27213
+ {
27214
+ clientSend: clientSendChannel
27215
+ },
27216
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27217
+ );
27218
+ var smithyClientChannels = defineChannels(
27219
+ "@smithy/smithy-client",
27220
+ {
27221
+ clientSend: clientSendChannel
27222
+ },
27223
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27224
+ );
26085
27225
 
26086
27226
  // src/instrumentation/plugins/bedrock-runtime-common.ts
26087
27227
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -26715,38 +27855,46 @@ function extractTextFromJsonLike(value) {
26715
27855
  }
26716
27856
 
26717
27857
  // src/instrumentation/plugins/genkit-channels.ts
26718
- var genkitChannels = defineChannels("@genkit-ai/ai", {
26719
- generate: channel({
26720
- channelName: "generate",
26721
- kind: "async"
26722
- }),
26723
- generateStream: channel({
26724
- channelName: "generateStream",
26725
- kind: "sync-stream"
26726
- }),
26727
- embed: channel({
26728
- channelName: "embed",
26729
- kind: "async"
26730
- }),
26731
- embedMany: channel({
26732
- channelName: "embedMany",
26733
- kind: "async"
26734
- }),
26735
- actionRun: channel({
26736
- channelName: "action.run",
26737
- kind: "async"
26738
- }),
26739
- actionStream: channel({
26740
- channelName: "action.stream",
26741
- kind: "sync-stream"
26742
- })
26743
- });
26744
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
26745
- actionSpan: channel({
26746
- channelName: "action.span",
26747
- kind: "async"
26748
- })
26749
- });
27858
+ var genkitChannels = defineChannels(
27859
+ "@genkit-ai/ai",
27860
+ {
27861
+ generate: channel({
27862
+ channelName: "generate",
27863
+ kind: "async"
27864
+ }),
27865
+ generateStream: channel({
27866
+ channelName: "generateStream",
27867
+ kind: "sync-stream"
27868
+ }),
27869
+ embed: channel({
27870
+ channelName: "embed",
27871
+ kind: "async"
27872
+ }),
27873
+ embedMany: channel({
27874
+ channelName: "embedMany",
27875
+ kind: "async"
27876
+ }),
27877
+ actionRun: channel({
27878
+ channelName: "action.run",
27879
+ kind: "async"
27880
+ }),
27881
+ actionStream: channel({
27882
+ channelName: "action.stream",
27883
+ kind: "sync-stream"
27884
+ })
27885
+ },
27886
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
27887
+ );
27888
+ var genkitCoreChannels = defineChannels(
27889
+ "@genkit-ai/core",
27890
+ {
27891
+ actionSpan: channel({
27892
+ channelName: "action.span",
27893
+ kind: "async"
27894
+ })
27895
+ },
27896
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
27897
+ );
26750
27898
 
26751
27899
  // src/instrumentation/plugins/genkit-plugin.ts
26752
27900
  var GenkitPlugin = class extends BasePlugin {
@@ -26931,12 +28079,17 @@ function startActionSpanState(args) {
26931
28079
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
26932
28080
  return void 0;
26933
28081
  }
26934
- const span = startSpan({
26935
- name: actionSpanName(args.metadata, args.runStepName),
26936
- spanAttributes: {
26937
- type: actionSpanType(args.metadata)
26938
- }
26939
- });
28082
+ const span = startSpan(
28083
+ withSpanInstrumentationName(
28084
+ {
28085
+ name: actionSpanName(args.metadata, args.runStepName),
28086
+ spanAttributes: {
28087
+ type: actionSpanType(args.metadata)
28088
+ }
28089
+ },
28090
+ INSTRUMENTATION_NAMES.GENKIT
28091
+ )
28092
+ );
26940
28093
  const startTime = getCurrentUnixTimestamp();
26941
28094
  span.log({
26942
28095
  input: args.input,
@@ -27297,20 +28450,24 @@ function stringValue(value) {
27297
28450
  }
27298
28451
 
27299
28452
  // src/instrumentation/plugins/github-copilot-channels.ts
27300
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
27301
- createSession: channel({
27302
- channelName: "client.createSession",
27303
- kind: "async"
27304
- }),
27305
- resumeSession: channel({
27306
- channelName: "client.resumeSession",
27307
- kind: "async"
27308
- }),
27309
- sendAndWait: channel({
27310
- channelName: "session.sendAndWait",
27311
- kind: "async"
27312
- })
27313
- });
28453
+ var gitHubCopilotChannels = defineChannels(
28454
+ "@github/copilot-sdk",
28455
+ {
28456
+ createSession: channel({
28457
+ channelName: "client.createSession",
28458
+ kind: "async"
28459
+ }),
28460
+ resumeSession: channel({
28461
+ channelName: "client.resumeSession",
28462
+ kind: "async"
28463
+ }),
28464
+ sendAndWait: channel({
28465
+ channelName: "session.sendAndWait",
28466
+ kind: "async"
28467
+ })
28468
+ },
28469
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
28470
+ );
27314
28471
 
27315
28472
  // src/instrumentation/plugins/github-copilot-plugin.ts
27316
28473
  var ROOT_AGENT_KEY = "__root__";
@@ -27401,11 +28558,16 @@ async function handleTurnStart(state, agentId) {
27401
28558
  return;
27402
28559
  }
27403
28560
  const parentId = await getParentIdForAgent(state, agentId);
27404
- const span = startSpan({
27405
- name: "Copilot Turn",
27406
- parent: parentId,
27407
- spanAttributes: { type: "task" /* TASK */ }
27408
- });
28561
+ const span = startSpan(
28562
+ withSpanInstrumentationName(
28563
+ {
28564
+ name: "Copilot Turn",
28565
+ parent: parentId,
28566
+ spanAttributes: { type: "task" /* TASK */ }
28567
+ },
28568
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28569
+ )
28570
+ );
27409
28571
  const pendingUserMessage = state.pendingUserMessages.get(key);
27410
28572
  if (pendingUserMessage) {
27411
28573
  span.log({ input: pendingUserMessage });
@@ -27436,11 +28598,16 @@ async function handleUsage(state, agentId, usage) {
27436
28598
  const parentId = turn ? await turn.id : await state.session.id;
27437
28599
  const content = state.currentMessageContent.get(key);
27438
28600
  const { metrics, metadata } = extractMetricsFromUsage(usage);
27439
- const llmSpan = startSpan({
27440
- name: "github.copilot.llm",
27441
- parent: parentId,
27442
- spanAttributes: { type: "llm" /* LLM */ }
27443
- });
28601
+ const llmSpan = startSpan(
28602
+ withSpanInstrumentationName(
28603
+ {
28604
+ name: "github.copilot.llm",
28605
+ parent: parentId,
28606
+ spanAttributes: { type: "llm" /* LLM */ }
28607
+ },
28608
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28609
+ )
28610
+ );
27444
28611
  llmSpan.log({
27445
28612
  output: content ?? void 0,
27446
28613
  metadata,
@@ -27466,11 +28633,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
27466
28633
  }
27467
28634
  const parentId = await getToolParentId(state, agentId);
27468
28635
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
27469
- const span = startSpan({
27470
- name: displayName,
27471
- parent: parentId,
27472
- spanAttributes: { type: "tool" /* TOOL */ }
27473
- });
28636
+ const span = startSpan(
28637
+ withSpanInstrumentationName(
28638
+ {
28639
+ name: displayName,
28640
+ parent: parentId,
28641
+ spanAttributes: { type: "tool" /* TOOL */ }
28642
+ },
28643
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28644
+ )
28645
+ );
27474
28646
  const metadata = {
27475
28647
  "gen_ai.tool.name": toolName,
27476
28648
  "gen_ai.tool.call.id": toolCallId
@@ -27506,11 +28678,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
27506
28678
  }
27507
28679
  const tool = state.activeTools.get(toolCallId);
27508
28680
  const parentId = tool ? await tool.id : await state.session.id;
27509
- const span = startSpan({
27510
- name: `Agent: ${agentDisplayName}`,
27511
- parent: parentId,
27512
- spanAttributes: { type: "task" /* TASK */ }
27513
- });
28681
+ const span = startSpan(
28682
+ withSpanInstrumentationName(
28683
+ {
28684
+ name: `Agent: ${agentDisplayName}`,
28685
+ parent: parentId,
28686
+ spanAttributes: { type: "task" /* TASK */ }
28687
+ },
28688
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28689
+ )
28690
+ );
27514
28691
  span.log({
27515
28692
  metadata: {
27516
28693
  "github_copilot.agent_name": agentDisplayName,
@@ -27736,10 +28913,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
27736
28913
  if (!config || typeof config !== "object") {
27737
28914
  return;
27738
28915
  }
27739
- const sessionSpan = startSpan({
27740
- name: "Copilot Session",
27741
- spanAttributes: { type: "task" /* TASK */ }
27742
- });
28916
+ const sessionSpan = startSpan(
28917
+ withSpanInstrumentationName(
28918
+ {
28919
+ name: "Copilot Session",
28920
+ spanAttributes: { type: "task" /* TASK */ }
28921
+ },
28922
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28923
+ )
28924
+ );
27743
28925
  const metadata = {};
27744
28926
  if (config.model) {
27745
28927
  metadata["github_copilot.model"] = config.model;
@@ -27827,12 +29009,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
27827
29009
  };
27828
29010
 
27829
29011
  // src/instrumentation/plugins/flue-channels.ts
27830
- var flueChannels = defineChannels("@flue/runtime", {
27831
- createContext: channel({
27832
- channelName: "createFlueContext",
27833
- kind: "sync-stream"
27834
- })
27835
- });
29012
+ var flueChannels = defineChannels(
29013
+ "@flue/runtime",
29014
+ {
29015
+ createContext: channel({
29016
+ channelName: "createFlueContext",
29017
+ kind: "sync-stream"
29018
+ })
29019
+ },
29020
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
29021
+ );
27836
29022
 
27837
29023
  // src/instrumentation/plugins/flue-plugin.ts
27838
29024
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -28175,15 +29361,20 @@ var FlueObserveBridge = class {
28175
29361
  safeLog3(existing.span, { input, metadata });
28176
29362
  return;
28177
29363
  }
28178
- const span = startSpan({
28179
- name: `workflow:${workflowName}`,
28180
- spanAttributes: { type: "task" /* TASK */ },
28181
- startTime: eventTime(event.startedAt ?? event.timestamp),
28182
- event: {
28183
- input,
28184
- metadata
28185
- }
28186
- });
29364
+ const span = startSpan(
29365
+ withSpanInstrumentationName(
29366
+ {
29367
+ name: `workflow:${workflowName}`,
29368
+ spanAttributes: { type: "task" /* TASK */ },
29369
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29370
+ event: {
29371
+ input,
29372
+ metadata
29373
+ }
29374
+ },
29375
+ INSTRUMENTATION_NAMES.FLUE
29376
+ )
29377
+ );
28187
29378
  this.runsById.set(event.runId, { metadata, span });
28188
29379
  }
28189
29380
  handleRunResume(event, ctx) {
@@ -28203,12 +29394,17 @@ var FlueObserveBridge = class {
28203
29394
  safeLog3(existing.span, { metadata });
28204
29395
  return;
28205
29396
  }
28206
- const span = startSpan({
28207
- name: `workflow:${workflowName}`,
28208
- spanAttributes: { type: "task" /* TASK */ },
28209
- startTime: eventTime(event.startedAt ?? event.timestamp),
28210
- event: { metadata }
28211
- });
29397
+ const span = startSpan(
29398
+ withSpanInstrumentationName(
29399
+ {
29400
+ name: `workflow:${workflowName}`,
29401
+ spanAttributes: { type: "task" /* TASK */ },
29402
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29403
+ event: { metadata }
29404
+ },
29405
+ INSTRUMENTATION_NAMES.FLUE
29406
+ )
29407
+ );
28212
29408
  this.runsById.set(event.runId, { metadata, span });
28213
29409
  }
28214
29410
  handleRunEnd(event) {
@@ -28847,7 +30043,14 @@ function stateMatchesRun(state, runId) {
28847
30043
  return state.metadata["flue.run_id"] === runId;
28848
30044
  }
28849
30045
  function startFlueSpan(parent, args) {
28850
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
30046
+ return parent ? withCurrent(
30047
+ parent,
30048
+ () => startSpan(
30049
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30050
+ )
30051
+ ) : startSpan(
30052
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
30053
+ );
28851
30054
  }
28852
30055
  function runWithCurrentSpanStore(span, next) {
28853
30056
  const state = _internalGetGlobalState();
@@ -28937,9 +30140,13 @@ var BraintrustLangChainCallbackHandler = class {
28937
30140
  ...this.options.debug ? { runId, parentRunId } : {}
28938
30141
  }
28939
30142
  };
28940
- let span = parentSpan.startSpan(args);
30143
+ const spanArgs = withSpanInstrumentationName(
30144
+ args,
30145
+ INSTRUMENTATION_NAMES.LANGCHAIN
30146
+ );
30147
+ let span = parentSpan.startSpan(spanArgs);
28941
30148
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
28942
- span = initLogger().startSpan(args);
30149
+ span = initLogger().startSpan(spanArgs);
28943
30150
  }
28944
30151
  this.spans.set(runId, span);
28945
30152
  }
@@ -29152,6 +30359,16 @@ function cleanObject(obj) {
29152
30359
  })
29153
30360
  );
29154
30361
  }
30362
+ function normalizeTokenMetrics(obj) {
30363
+ const metrics = cleanObject(obj);
30364
+ if (metrics.total_tokens !== void 0) {
30365
+ metrics.tokens = metrics.total_tokens;
30366
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
30367
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
30368
+ }
30369
+ delete metrics.total_tokens;
30370
+ return metrics;
30371
+ }
29155
30372
  function walkGenerations(response) {
29156
30373
  const result = [];
29157
30374
  const generations = response.generations || [];
@@ -29198,7 +30415,7 @@ function getMetricsFromResponse(response) {
29198
30415
  continue;
29199
30416
  }
29200
30417
  const inputTokenDetails = usageMetadata.input_token_details;
29201
- return cleanObject({
30418
+ return normalizeTokenMetrics({
29202
30419
  total_tokens: usageMetadata.total_tokens,
29203
30420
  prompt_tokens: usageMetadata.input_tokens,
29204
30421
  completion_tokens: usageMetadata.output_tokens,
@@ -29208,7 +30425,7 @@ function getMetricsFromResponse(response) {
29208
30425
  }
29209
30426
  const llmOutput = response.llmOutput || {};
29210
30427
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
29211
- return cleanObject({
30428
+ return normalizeTokenMetrics({
29212
30429
  total_tokens: tokenUsage.totalTokens,
29213
30430
  prompt_tokens: tokenUsage.promptTokens,
29214
30431
  completion_tokens: tokenUsage.completionTokens
@@ -29226,16 +30443,20 @@ function isRecord(value) {
29226
30443
  }
29227
30444
 
29228
30445
  // src/instrumentation/plugins/langchain-channels.ts
29229
- var langChainChannels = defineChannels("@langchain/core", {
29230
- configure: channel({
29231
- channelName: "CallbackManager.configure",
29232
- kind: "sync-stream"
29233
- }),
29234
- configureSync: channel({
29235
- channelName: "CallbackManager._configureSync",
29236
- kind: "sync-stream"
29237
- })
29238
- });
30446
+ var langChainChannels = defineChannels(
30447
+ "@langchain/core",
30448
+ {
30449
+ configure: channel({
30450
+ channelName: "CallbackManager.configure",
30451
+ kind: "sync-stream"
30452
+ }),
30453
+ configureSync: channel({
30454
+ channelName: "CallbackManager._configureSync",
30455
+ kind: "sync-stream"
30456
+ })
30457
+ },
30458
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
30459
+ );
29239
30460
 
29240
30461
  // src/instrumentation/plugins/langchain-plugin.ts
29241
30462
  var LangChainPlugin = class extends BasePlugin {
@@ -29323,20 +30544,24 @@ function isBraintrustHandler(handler) {
29323
30544
  }
29324
30545
 
29325
30546
  // src/instrumentation/plugins/langsmith-channels.ts
29326
- var langSmithChannels = defineChannels("langsmith", {
29327
- createRun: channel({
29328
- channelName: "Client.createRun",
29329
- kind: "async"
29330
- }),
29331
- updateRun: channel({
29332
- channelName: "Client.updateRun",
29333
- kind: "async"
29334
- }),
29335
- batchIngestRuns: channel({
29336
- channelName: "Client.batchIngestRuns",
29337
- kind: "async"
29338
- })
29339
- });
30547
+ var langSmithChannels = defineChannels(
30548
+ "langsmith",
30549
+ {
30550
+ createRun: channel({
30551
+ channelName: "Client.createRun",
30552
+ kind: "async"
30553
+ }),
30554
+ updateRun: channel({
30555
+ channelName: "Client.updateRun",
30556
+ kind: "async"
30557
+ }),
30558
+ batchIngestRuns: channel({
30559
+ channelName: "Client.batchIngestRuns",
30560
+ kind: "async"
30561
+ })
30562
+ },
30563
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
30564
+ );
29340
30565
 
29341
30566
  // src/instrumentation/plugins/langsmith-plugin.ts
29342
30567
  var MAX_COMPLETED_RUNS = 1e4;
@@ -29500,19 +30725,24 @@ var LangSmithPlugin = class extends BasePlugin {
29500
30725
  const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
29501
30726
  const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
29502
30727
  const startTime = timestampSeconds(ownValue(run, "start_time"));
29503
- return startSpan({
29504
- name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
29505
- spanId: id,
29506
- parentSpanIds: {
29507
- parentSpanIds: parentId ? [parentId] : [],
29508
- rootSpanId: traceId
29509
- },
29510
- spanAttributes: {
29511
- type: mapRunType(ownValue(run, "run_type"))
29512
- },
29513
- ...startTime === void 0 ? {} : { startTime },
29514
- event: { id }
29515
- });
30728
+ return startSpan(
30729
+ withSpanInstrumentationName(
30730
+ {
30731
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
30732
+ spanId: id,
30733
+ parentSpanIds: {
30734
+ parentSpanIds: parentId ? [parentId] : [],
30735
+ rootSpanId: traceId
30736
+ },
30737
+ spanAttributes: {
30738
+ type: mapRunType(ownValue(run, "run_type"))
30739
+ },
30740
+ ...startTime === void 0 ? {} : { startTime },
30741
+ event: { id }
30742
+ },
30743
+ INSTRUMENTATION_NAMES.LANGSMITH
30744
+ )
30745
+ );
29516
30746
  }
29517
30747
  logRun(span, run, previous, includeOutput) {
29518
30748
  const inputs = preferOwnValue(run, previous, "inputs");
@@ -29770,7 +31000,8 @@ var piCodingAgentChannels = defineChannels(
29770
31000
  channelName: "AgentSession.prompt",
29771
31001
  kind: "async"
29772
31002
  })
29773
- }
31003
+ },
31004
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
29774
31005
  );
29775
31006
 
29776
31007
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -29854,14 +31085,19 @@ function startPiPromptRun(event, onFinalize) {
29854
31085
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
29855
31086
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
29856
31087
  };
29857
- const span = startSpan({
29858
- event: {
29859
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
29860
- metadata
29861
- },
29862
- name: "AgentSession.prompt",
29863
- spanAttributes: { type: "task" /* TASK */ }
29864
- });
31088
+ const span = startSpan(
31089
+ withSpanInstrumentationName(
31090
+ {
31091
+ event: {
31092
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
31093
+ metadata
31094
+ },
31095
+ name: "AgentSession.prompt",
31096
+ spanAttributes: { type: "task" /* TASK */ }
31097
+ },
31098
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31099
+ )
31100
+ );
29865
31101
  const streamPatchState = installPiStreamPatch(agent);
29866
31102
  const options = event.arguments[1];
29867
31103
  const promptText = event.arguments[0];
@@ -30031,15 +31267,20 @@ async function startPiLlmSpan(state, model, context, options) {
30031
31267
  ...extractToolMetadata(context.tools),
30032
31268
  "pi_coding_agent.operation": "agent.streamFn"
30033
31269
  };
30034
- const span = startSpan({
30035
- event: {
30036
- input: processInputAttachments(normalizePiContextInput(context)),
30037
- metadata
30038
- },
30039
- name: getLlmSpanName(model),
30040
- parent: await state.span.export(),
30041
- spanAttributes: { type: "llm" /* LLM */ }
30042
- });
31270
+ const span = startSpan(
31271
+ withSpanInstrumentationName(
31272
+ {
31273
+ event: {
31274
+ input: processInputAttachments(normalizePiContextInput(context)),
31275
+ metadata
31276
+ },
31277
+ name: getLlmSpanName(model),
31278
+ parent: await state.span.export(),
31279
+ spanAttributes: { type: "llm" /* LLM */ }
31280
+ },
31281
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31282
+ )
31283
+ );
30043
31284
  const llmState = {
30044
31285
  finalized: false,
30045
31286
  metadata,
@@ -30207,15 +31448,20 @@ async function startPiToolSpan(state, event) {
30207
31448
  "pi_coding_agent.tool.name": event.toolName
30208
31449
  };
30209
31450
  try {
30210
- const span = startSpan({
30211
- event: {
30212
- input: event.args,
30213
- metadata
30214
- },
30215
- name: event.toolName || "tool",
30216
- parent: await state.span.export(),
30217
- spanAttributes: { type: "tool" /* TOOL */ }
30218
- });
31451
+ const span = startSpan(
31452
+ withSpanInstrumentationName(
31453
+ {
31454
+ event: {
31455
+ input: event.args,
31456
+ metadata
31457
+ },
31458
+ name: event.toolName || "tool",
31459
+ parent: await state.span.export(),
31460
+ spanAttributes: { type: "tool" /* TOOL */ }
31461
+ },
31462
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31463
+ )
31464
+ );
30219
31465
  state.activeToolSpans.set(event.toolCallId, {
30220
31466
  restoreAutoInstrumentation,
30221
31467
  span
@@ -30647,20 +31893,24 @@ function logInstrumentationError4(context, error) {
30647
31893
  }
30648
31894
 
30649
31895
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
30650
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
30651
- agentStream: channel({
30652
- channelName: "Agent.stream",
30653
- kind: "sync-stream"
30654
- }),
30655
- graphStream: channel({
30656
- channelName: "Graph.stream",
30657
- kind: "sync-stream"
30658
- }),
30659
- swarmStream: channel({
30660
- channelName: "Swarm.stream",
30661
- kind: "sync-stream"
30662
- })
30663
- });
31896
+ var strandsAgentSDKChannels = defineChannels(
31897
+ "@strands-agents/sdk",
31898
+ {
31899
+ agentStream: channel({
31900
+ channelName: "Agent.stream",
31901
+ kind: "sync-stream"
31902
+ }),
31903
+ graphStream: channel({
31904
+ channelName: "Graph.stream",
31905
+ kind: "sync-stream"
31906
+ }),
31907
+ swarmStream: channel({
31908
+ channelName: "Swarm.stream",
31909
+ kind: "sync-stream"
31910
+ })
31911
+ },
31912
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
31913
+ );
30664
31914
 
30665
31915
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
30666
31916
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -30815,22 +32065,32 @@ function startAgentStream(event, activeChildParents) {
30815
32065
  );
30816
32066
  const span = parentSpan ? withCurrent(
30817
32067
  parentSpan,
30818
- () => startSpan({
30819
- event: {
30820
- input,
30821
- metadata
32068
+ () => startSpan(
32069
+ withSpanInstrumentationName(
32070
+ {
32071
+ event: {
32072
+ input,
32073
+ metadata
32074
+ },
32075
+ name: formatAgentSpanName(agent),
32076
+ spanAttributes: { type: "task" /* TASK */ }
32077
+ },
32078
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32079
+ )
32080
+ )
32081
+ ) : startSpan(
32082
+ withSpanInstrumentationName(
32083
+ {
32084
+ event: {
32085
+ input,
32086
+ metadata
32087
+ },
32088
+ name: formatAgentSpanName(agent),
32089
+ spanAttributes: { type: "task" /* TASK */ }
30822
32090
  },
30823
- name: formatAgentSpanName(agent),
30824
- spanAttributes: { type: "task" /* TASK */ }
30825
- })
30826
- ) : startSpan({
30827
- event: {
30828
- input,
30829
- metadata
30830
- },
30831
- name: formatAgentSpanName(agent),
30832
- spanAttributes: { type: "task" /* TASK */ }
30833
- });
32091
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32092
+ )
32093
+ );
30834
32094
  return {
30835
32095
  activeTools: /* @__PURE__ */ new Map(),
30836
32096
  attachmentCache,
@@ -30852,22 +32112,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30852
32112
  const input = processStrandsInputAttachments(event.arguments[0]);
30853
32113
  const span = parentSpan ? withCurrent(
30854
32114
  parentSpan,
30855
- () => startSpan({
30856
- event: {
30857
- input,
30858
- metadata
32115
+ () => startSpan(
32116
+ withSpanInstrumentationName(
32117
+ {
32118
+ event: {
32119
+ input,
32120
+ metadata
32121
+ },
32122
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32123
+ spanAttributes: { type: "task" /* TASK */ }
32124
+ },
32125
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32126
+ )
32127
+ )
32128
+ ) : startSpan(
32129
+ withSpanInstrumentationName(
32130
+ {
32131
+ event: {
32132
+ input,
32133
+ metadata
32134
+ },
32135
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32136
+ spanAttributes: { type: "task" /* TASK */ }
30859
32137
  },
30860
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30861
- spanAttributes: { type: "task" /* TASK */ }
30862
- })
30863
- ) : startSpan({
30864
- event: {
30865
- input,
30866
- metadata
30867
- },
30868
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30869
- spanAttributes: { type: "task" /* TASK */ }
30870
- });
32138
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32139
+ )
32140
+ );
30871
32141
  return {
30872
32142
  activeNodes: /* @__PURE__ */ new Map(),
30873
32143
  finalized: false,
@@ -30969,17 +32239,22 @@ function startModelSpan(state, event) {
30969
32239
  };
30970
32240
  const span = withCurrent(
30971
32241
  state.span,
30972
- () => startSpan({
30973
- event: {
30974
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
30975
- event.agent.messages,
30976
- state.attachmentCache
30977
- ) : void 0,
30978
- metadata
30979
- },
30980
- name: formatModelSpanName(model),
30981
- spanAttributes: { type: "llm" /* LLM */ }
30982
- })
32242
+ () => startSpan(
32243
+ withSpanInstrumentationName(
32244
+ {
32245
+ event: {
32246
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
32247
+ event.agent.messages,
32248
+ state.attachmentCache
32249
+ ) : void 0,
32250
+ metadata
32251
+ },
32252
+ name: formatModelSpanName(model),
32253
+ spanAttributes: { type: "llm" /* LLM */ }
32254
+ },
32255
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32256
+ )
32257
+ )
30983
32258
  );
30984
32259
  state.activeModel = {
30985
32260
  metadata,
@@ -31031,20 +32306,25 @@ function startToolSpan2(state, event) {
31031
32306
  const name = extractToolName3(toolUse, event.tool);
31032
32307
  const span = withCurrent(
31033
32308
  state.span,
31034
- () => startSpan({
31035
- event: {
31036
- input: toolUse?.input,
31037
- metadata: {
31038
- "gen_ai.tool.call.id": toolUse?.toolUseId,
31039
- "gen_ai.tool.name": name,
31040
- "strands.operation": "tool.call",
31041
- "strands.tool.name": name,
31042
- provider: "strands"
31043
- }
31044
- },
31045
- name: `tool: ${name}`,
31046
- spanAttributes: { type: "tool" /* TOOL */ }
31047
- })
32309
+ () => startSpan(
32310
+ withSpanInstrumentationName(
32311
+ {
32312
+ event: {
32313
+ input: toolUse?.input,
32314
+ metadata: {
32315
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
32316
+ "gen_ai.tool.name": name,
32317
+ "strands.operation": "tool.call",
32318
+ "strands.tool.name": name,
32319
+ provider: "strands"
32320
+ }
32321
+ },
32322
+ name: `tool: ${name}`,
32323
+ spanAttributes: { type: "tool" /* TOOL */ }
32324
+ },
32325
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32326
+ )
32327
+ )
31048
32328
  );
31049
32329
  state.activeTools.set(key, {
31050
32330
  span,
@@ -31115,11 +32395,16 @@ function startNodeSpan(state, event, activeChildParents) {
31115
32395
  };
31116
32396
  const span = withCurrent(
31117
32397
  state.span,
31118
- () => startSpan({
31119
- event: { metadata },
31120
- name: `node: ${nodeId}`,
31121
- spanAttributes: { type: "task" /* TASK */ }
31122
- })
32398
+ () => startSpan(
32399
+ withSpanInstrumentationName(
32400
+ {
32401
+ event: { metadata },
32402
+ name: `node: ${nodeId}`,
32403
+ spanAttributes: { type: "task" /* TASK */ }
32404
+ },
32405
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32406
+ )
32407
+ )
31123
32408
  );
31124
32409
  const nodeState = {
31125
32410
  ...child ? { child } : {},
@@ -32812,6 +34097,26 @@ function wrapAISDK(aiSDK, options = {}) {
32812
34097
  }
32813
34098
  });
32814
34099
  }
34100
+ function isHarnessAgentInstance(instance) {
34101
+ try {
34102
+ const visited = /* @__PURE__ */ new Set();
34103
+ let prototype = Object.getPrototypeOf(instance);
34104
+ while (prototype !== null && !visited.has(prototype)) {
34105
+ visited.add(prototype);
34106
+ const constructor = Object.getOwnPropertyDescriptor(
34107
+ prototype,
34108
+ "constructor"
34109
+ )?.value;
34110
+ const constructorName = typeof constructor === "function" ? Object.getOwnPropertyDescriptor(constructor, "name")?.value : void 0;
34111
+ if (constructorName === "HarnessAgent") {
34112
+ return true;
34113
+ }
34114
+ prototype = Object.getPrototypeOf(prototype);
34115
+ }
34116
+ } catch {
34117
+ }
34118
+ return false;
34119
+ }
32815
34120
  var wrapAgentClass = (AgentClass, options = {}) => {
32816
34121
  const typedAgentClass = AgentClass;
32817
34122
  return new Proxy(typedAgentClass, {
@@ -32821,9 +34126,52 @@ var wrapAgentClass = (AgentClass, options = {}) => {
32821
34126
  args,
32822
34127
  newTarget
32823
34128
  );
34129
+ const harnessAgent = isHarnessAgentInstance(instance) ? instance : null;
32824
34130
  return new Proxy(instance, {
32825
34131
  get(instanceTarget, prop, instanceReceiver) {
32826
34132
  const original = Reflect.get(instanceTarget, prop, instanceTarget);
34133
+ if (harnessAgent && typeof original === "function") {
34134
+ switch (prop) {
34135
+ case "createSession":
34136
+ return wrapHarnessAgentCreateSession(
34137
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
34138
+ original,
34139
+ harnessAgent
34140
+ );
34141
+ case "generate":
34142
+ return wrapHarnessAgentGenerate(
34143
+ original,
34144
+ harnessAgent,
34145
+ harnessAgentChannels.generate,
34146
+ "HarnessAgent.generate",
34147
+ options
34148
+ );
34149
+ case "stream":
34150
+ return wrapHarnessAgentStream(
34151
+ original,
34152
+ harnessAgent,
34153
+ harnessAgentChannels.stream,
34154
+ "HarnessAgent.stream",
34155
+ options
34156
+ );
34157
+ case "continueGenerate":
34158
+ return wrapHarnessAgentGenerate(
34159
+ original,
34160
+ harnessAgent,
34161
+ harnessAgentChannels.continueGenerate,
34162
+ "HarnessAgent.continueGenerate",
34163
+ options
34164
+ );
34165
+ case "continueStream":
34166
+ return wrapHarnessAgentStream(
34167
+ original,
34168
+ harnessAgent,
34169
+ harnessAgentChannels.continueStream,
34170
+ "HarnessAgent.continueStream",
34171
+ options
34172
+ );
34173
+ }
34174
+ }
32827
34175
  if (prop === "generate" && typeof original === "function" && instanceTarget.constructor.name !== "WorkflowAgent") {
32828
34176
  return wrapAgentGenerate(original, instanceTarget, options);
32829
34177
  }
@@ -32839,6 +34187,39 @@ var wrapAgentClass = (AgentClass, options = {}) => {
32839
34187
  }
32840
34188
  });
32841
34189
  };
34190
+ var wrapHarnessAgentCreateSession = (createSession, instance) => {
34191
+ const wrapper = function(params) {
34192
+ return harnessAgentChannels.createSession.tracePromise(
34193
+ () => params === void 0 ? createSession.call(instance) : createSession.call(instance, params),
34194
+ createAISDKChannelContext(params ?? {}, { self: instance })
34195
+ );
34196
+ };
34197
+ Object.defineProperty(wrapper, "name", {
34198
+ value: "HarnessAgent.createSession",
34199
+ writable: false
34200
+ });
34201
+ return wrapper;
34202
+ };
34203
+ var wrapHarnessAgentGenerate = (generate, instance, channel2, name, options) => makeGenerateTextWrapper(
34204
+ channel2,
34205
+ name,
34206
+ generate.bind(instance),
34207
+ {
34208
+ self: instance,
34209
+ spanType: "task" /* TASK */
34210
+ },
34211
+ options
34212
+ );
34213
+ var wrapHarnessAgentStream = (stream, instance, channel2, name, options) => makeStreamWrapper(
34214
+ channel2,
34215
+ name,
34216
+ stream.bind(instance),
34217
+ {
34218
+ self: instance,
34219
+ spanType: "task" /* TASK */
34220
+ },
34221
+ options
34222
+ );
32842
34223
  var wrapAgentGenerate = (generate, instance, options = {}) => {
32843
34224
  const defaultName = `${instance.constructor.name}.generate`;
32844
34225
  return async (params) => makeGenerateTextWrapper(
@@ -33065,6 +34446,7 @@ var BraintrustLanguageModelWrapper = class {
33065
34446
  this.supportsUrl = (url) => this.model.supportsUrl(url);
33066
34447
  }
33067
34448
  }
34449
+ model;
33068
34450
  supportsUrl;
33069
34451
  get specificationVersion() {
33070
34452
  return this.model.specificationVersion;
@@ -33087,12 +34469,17 @@ var BraintrustLanguageModelWrapper = class {
33087
34469
  // For the first cut, do not support custom span_info arguments. We can
33088
34470
  // propagate those via async local storage
33089
34471
  async doGenerate(options) {
33090
- const span = startSpan({
33091
- name: "Chat Completion",
33092
- spanAttributes: {
33093
- type: "llm"
33094
- }
33095
- });
34472
+ const span = startSpan(
34473
+ withSpanInstrumentationName(
34474
+ {
34475
+ name: "Chat Completion",
34476
+ spanAttributes: {
34477
+ type: "llm"
34478
+ }
34479
+ },
34480
+ INSTRUMENTATION_NAMES.AI_SDK
34481
+ )
34482
+ );
33096
34483
  const { prompt, mode, ...rest } = options;
33097
34484
  const startTime = getCurrentUnixTimestamp();
33098
34485
  try {
@@ -33123,12 +34510,17 @@ var BraintrustLanguageModelWrapper = class {
33123
34510
  async doStream(options) {
33124
34511
  const { prompt, mode, ...rest } = options;
33125
34512
  const startTime = getCurrentUnixTimestamp();
33126
- const span = startSpan({
33127
- name: "Chat Completion",
33128
- spanAttributes: {
33129
- type: "llm"
33130
- }
33131
- });
34513
+ const span = startSpan(
34514
+ withSpanInstrumentationName(
34515
+ {
34516
+ name: "Chat Completion",
34517
+ spanAttributes: {
34518
+ type: "llm"
34519
+ }
34520
+ },
34521
+ INSTRUMENTATION_NAMES.AI_SDK
34522
+ )
34523
+ );
33132
34524
  span.log({
33133
34525
  input: postProcessPrompt(prompt),
33134
34526
  metadata: {
@@ -33483,7 +34875,9 @@ function BraintrustMiddleware(config = {}) {
33483
34875
  }
33484
34876
  }
33485
34877
  };
33486
- const span = startSpan(spanArgs);
34878
+ const span = startSpan(
34879
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
34880
+ );
33487
34881
  try {
33488
34882
  const result = await doGenerate();
33489
34883
  const metadata = {};
@@ -33545,7 +34939,9 @@ function BraintrustMiddleware(config = {}) {
33545
34939
  }
33546
34940
  }
33547
34941
  };
33548
- const span = startSpan(spanArgs);
34942
+ const span = startSpan(
34943
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
34944
+ );
33549
34945
  try {
33550
34946
  const { stream, ...rest } = await doStream();
33551
34947
  const textChunks = [];
@@ -33647,6 +35043,7 @@ function BraintrustMiddleware(config = {}) {
33647
35043
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
33648
35044
  var MAX_EVE_CACHE_ENTRIES = 1e4;
33649
35045
  var MAX_STORED_LLM_INPUTS = 100;
35046
+ var MAX_STORED_REASONING_BLOCKS = 100;
33650
35047
  var MAX_STORED_SPAN_REFERENCES = 1e4;
33651
35048
  var MAX_STORED_STEP_STARTS = 1e4;
33652
35049
  function braintrustEveHook(options) {
@@ -33685,6 +35082,7 @@ var ResumedEveSpan = class {
33685
35082
  this.reference = reference;
33686
35083
  this.endTime = reference.endTime;
33687
35084
  }
35085
+ reference;
33688
35086
  endTime;
33689
35087
  get rootSpanId() {
33690
35088
  return this.reference.rootSpanId;
@@ -33717,6 +35115,7 @@ var EveBridge = class {
33717
35115
  constructor(state) {
33718
35116
  this.state = state;
33719
35117
  }
35118
+ state;
33720
35119
  eventQueuesBySession = /* @__PURE__ */ new Map();
33721
35120
  completedToolKeys = new LRUCache({
33722
35121
  max: MAX_EVE_CACHE_ENTRIES
@@ -33749,7 +35148,12 @@ var EveBridge = class {
33749
35148
  };
33750
35149
  const span = withCurrent(
33751
35150
  NOOP_SPAN,
33752
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
35151
+ () => _internalStartSpanWithInitialMerge(
35152
+ withSpanInstrumentationName(
35153
+ { ...args, startTime },
35154
+ INSTRUMENTATION_NAMES.EVE
35155
+ )
35156
+ )
33753
35157
  );
33754
35158
  if (typeof rowId !== "string") {
33755
35159
  return span;
@@ -33895,6 +35299,9 @@ var EveBridge = class {
33895
35299
  case "step.started":
33896
35300
  await this.handleStepStarted(event, ctx, hookMetadata);
33897
35301
  return true;
35302
+ case "reasoning.completed":
35303
+ this.handleReasoningCompleted(event, ctx);
35304
+ return true;
33898
35305
  case "message.completed":
33899
35306
  this.handleMessageCompleted(event, ctx);
33900
35307
  return true;
@@ -33986,6 +35393,7 @@ var EveBridge = class {
33986
35393
  key,
33987
35394
  metadata,
33988
35395
  metrics: {},
35396
+ sessionId,
33989
35397
  span,
33990
35398
  stepsByIndex: /* @__PURE__ */ new Map(),
33991
35399
  turnId: event.data.turnId
@@ -34016,6 +35424,12 @@ var EveBridge = class {
34016
35424
  const endTime = eventTime2(event);
34017
35425
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
34018
35426
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35427
+ clearStoredEveReasoning(
35428
+ this.state,
35429
+ sessionId,
35430
+ event.data.turnId,
35431
+ event.data.stepIndex
35432
+ );
34019
35433
  }
34020
35434
  const stepOrdinal = this.stepOrdinal(event);
34021
35435
  const metadata = { ...turn.metadata };
@@ -34025,6 +35439,13 @@ var EveBridge = class {
34025
35439
  event.data.turnId,
34026
35440
  event.data.stepIndex
34027
35441
  );
35442
+ const reasoning = readStoredEveReasoning(
35443
+ this.state,
35444
+ sessionId,
35445
+ event.data.turnId,
35446
+ event.data.stepIndex
35447
+ );
35448
+ const output = mergeEveReasoning(void 0, reasoning);
34028
35449
  const { rowId: eventId, spanId } = await generateEveIds(
34029
35450
  "step",
34030
35451
  sessionId,
@@ -34042,32 +35463,52 @@ var EveBridge = class {
34042
35463
  spanId,
34043
35464
  startTime: eventTime2(event)
34044
35465
  });
34045
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
35466
+ span.log({
35467
+ ...input !== void 0 ? { input } : {},
35468
+ metadata
35469
+ });
34046
35470
  turn.stepsByIndex.set(event.data.stepIndex, {
34047
35471
  ...input !== void 0 ? { input } : {},
34048
35472
  metadata,
34049
35473
  metrics: {},
35474
+ ...output !== void 0 ? { output } : {},
35475
+ reasoning,
34050
35476
  span
34051
35477
  });
34052
35478
  }
35479
+ handleReasoningCompleted(event, ctx) {
35480
+ const sessionId = sessionIdFromContext(ctx);
35481
+ if (!sessionId) {
35482
+ return;
35483
+ }
35484
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
35485
+ const step = this.stepForEvent(event, ctx);
35486
+ if (step) {
35487
+ step.reasoning = reasoning;
35488
+ step.output = mergeEveReasoning(step.output, reasoning);
35489
+ }
35490
+ }
34053
35491
  handleMessageCompleted(event, ctx) {
34054
35492
  const step = this.stepForEvent(event, ctx);
34055
35493
  if (!step) {
34056
35494
  return;
34057
35495
  }
34058
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
35496
+ const existingMessage = eveOutputMessage(step.output);
34059
35497
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
34060
- step.output = [
34061
- {
34062
- finish_reason: normalizedFinishReason(event.data.finishReason),
34063
- index: 0,
34064
- message: {
34065
- content: event.data.message,
34066
- role: "assistant",
34067
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35498
+ step.output = mergeEveReasoning(
35499
+ [
35500
+ {
35501
+ finish_reason: normalizedFinishReason(event.data.finishReason),
35502
+ index: 0,
35503
+ message: {
35504
+ content: event.data.message,
35505
+ role: "assistant",
35506
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35507
+ }
34068
35508
  }
34069
- }
34070
- ];
35509
+ ],
35510
+ step.reasoning
35511
+ );
34071
35512
  const turn = this.turnForEvent(event, ctx);
34072
35513
  if (turn && event.data.finishReason !== "tool-calls") {
34073
35514
  turn.output = event.data.message;
@@ -34076,16 +35517,19 @@ var EveBridge = class {
34076
35517
  handleResultCompleted(event, ctx) {
34077
35518
  const step = this.stepForEvent(event, ctx);
34078
35519
  if (step) {
34079
- step.output = [
34080
- {
34081
- finish_reason: "stop",
34082
- index: 0,
34083
- message: {
34084
- content: event.data.result,
34085
- role: "assistant"
35520
+ step.output = mergeEveReasoning(
35521
+ [
35522
+ {
35523
+ finish_reason: "stop",
35524
+ index: 0,
35525
+ message: {
35526
+ content: event.data.result,
35527
+ role: "assistant"
35528
+ }
34086
35529
  }
34087
- }
34088
- ];
35530
+ ],
35531
+ step.reasoning
35532
+ );
34089
35533
  }
34090
35534
  const turn = this.turnForEvent(event, ctx);
34091
35535
  if (turn) {
@@ -34135,18 +35579,20 @@ var EveBridge = class {
34135
35579
  type: "function"
34136
35580
  });
34137
35581
  }
34138
- step.output = [
34139
- {
34140
- finish_reason: "tool_calls",
34141
- index: 0,
34142
- message: {
34143
- content: null,
34144
- role: "assistant",
34145
- tool_calls: [...toolCallsById.values()]
35582
+ step.output = mergeEveReasoning(
35583
+ [
35584
+ {
35585
+ finish_reason: "tool_calls",
35586
+ index: 0,
35587
+ message: {
35588
+ content: null,
35589
+ role: "assistant",
35590
+ tool_calls: [...toolCallsById.values()]
35591
+ }
34146
35592
  }
34147
- }
34148
- ];
34149
- step.span.log({ metadata: step.metadata, output: step.output });
35593
+ ],
35594
+ step.reasoning
35595
+ );
34150
35596
  }
34151
35597
  async handleActionResult(event, ctx, hookMetadata) {
34152
35598
  if (isToolResult(event.data.result)) {
@@ -34324,6 +35770,7 @@ var EveBridge = class {
34324
35770
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
34325
35771
  };
34326
35772
  step.metrics = { ...step.metrics, ...metrics };
35773
+ const sessionId = sessionIdFromContext(ctx);
34327
35774
  if (Array.isArray(step.output) && isObject(step.output[0])) {
34328
35775
  const finishReason = step.output[0].finish_reason;
34329
35776
  if (typeof finishReason !== "string") {
@@ -34348,6 +35795,14 @@ var EveBridge = class {
34348
35795
  turn.stepsByIndex.delete(event.data.stepIndex);
34349
35796
  }
34350
35797
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35798
+ if (sessionId) {
35799
+ clearStoredEveReasoning(
35800
+ this.state,
35801
+ sessionId,
35802
+ event.data.turnId,
35803
+ event.data.stepIndex
35804
+ );
35805
+ }
34351
35806
  }
34352
35807
  handleStepFailed(event, ctx) {
34353
35808
  const step = this.stepForEvent(event, ctx);
@@ -34365,6 +35820,15 @@ var EveBridge = class {
34365
35820
  const turn = this.turnForEvent(event, ctx);
34366
35821
  turn?.stepsByIndex.delete(event.data.stepIndex);
34367
35822
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35823
+ const sessionId = sessionIdFromContext(ctx);
35824
+ if (sessionId) {
35825
+ clearStoredEveReasoning(
35826
+ this.state,
35827
+ sessionId,
35828
+ event.data.turnId,
35829
+ event.data.stepIndex
35830
+ );
35831
+ }
34368
35832
  }
34369
35833
  handleTurnCompleted(event, ctx) {
34370
35834
  const turn = this.turnForEvent(event, ctx);
@@ -34462,6 +35926,7 @@ var EveBridge = class {
34462
35926
  key,
34463
35927
  metadata,
34464
35928
  metrics: {},
35929
+ sessionId,
34465
35930
  span,
34466
35931
  stepsByIndex: /* @__PURE__ */ new Map(),
34467
35932
  turnId: event.data.turnId
@@ -34724,6 +36189,9 @@ var EveBridge = class {
34724
36189
  const normalized = normalizeEveTraceState(current);
34725
36190
  return {
34726
36191
  ...normalized,
36192
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36193
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
36194
+ ),
34727
36195
  stepStarts: normalized.stepStarts.filter(
34728
36196
  (entry) => entry.turnId !== turn.turnId
34729
36197
  )
@@ -34763,6 +36231,7 @@ function emptyEveTraceState() {
34763
36231
  return {
34764
36232
  llmInputs: [],
34765
36233
  metadata: {},
36234
+ reasoningBlocks: [],
34766
36235
  spanReferences: [],
34767
36236
  stepStarts: []
34768
36237
  };
@@ -34813,6 +36282,21 @@ function normalizeEveTraceState(state) {
34813
36282
  const input = entry["input"];
34814
36283
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
34815
36284
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
36285
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
36286
+ if (!isObject(entry)) {
36287
+ return [];
36288
+ }
36289
+ const content = entry["content"];
36290
+ const eventAt = entry["eventAt"];
36291
+ const key = entry["key"];
36292
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
36293
+ {
36294
+ content,
36295
+ ...typeof eventAt === "string" ? { eventAt } : {},
36296
+ key
36297
+ }
36298
+ ] : [];
36299
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
34816
36300
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
34817
36301
  if (!isObject(entry)) {
34818
36302
  return [];
@@ -34823,7 +36307,13 @@ function normalizeEveTraceState(state) {
34823
36307
  const turnId = entry["turnId"];
34824
36308
  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 }] : [];
34825
36309
  }).slice(-MAX_STORED_STEP_STARTS) : [];
34826
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
36310
+ return {
36311
+ llmInputs,
36312
+ metadata: { ...metadata },
36313
+ reasoningBlocks,
36314
+ spanReferences,
36315
+ stepStarts
36316
+ };
34827
36317
  }
34828
36318
  function readEveTraceState(state) {
34829
36319
  try {
@@ -34832,23 +36322,85 @@ function readEveTraceState(state) {
34832
36322
  return emptyEveTraceState();
34833
36323
  }
34834
36324
  }
34835
- function captureEveModelInput(state, input) {
34836
- if (!isObject(input)) {
34837
- return;
34838
- }
34839
- const session = input["session"];
34840
- const turn = input["turn"];
34841
- const step = input["step"];
34842
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
34843
- return;
34844
- }
34845
- const sessionId = session["id"];
34846
- const turnId = turn["id"];
34847
- const stepIndex = step["index"];
34848
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
34849
- return;
36325
+ function storeEveReasoning(state, sessionId, event) {
36326
+ const eventAt = event.meta?.at;
36327
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
36328
+ let stored = [];
36329
+ state.update((current) => {
36330
+ const normalized = normalizeEveTraceState(current);
36331
+ const alreadyStored = normalized.reasoningBlocks.some(
36332
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
36333
+ );
36334
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
36335
+ ...normalized.reasoningBlocks,
36336
+ {
36337
+ content: event.data.reasoning,
36338
+ ...eventAt ? { eventAt } : {},
36339
+ key
36340
+ }
36341
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
36342
+ stored = reasoningBlocks.flatMap(
36343
+ (entry) => entry.key === key ? [
36344
+ {
36345
+ content: entry.content,
36346
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36347
+ }
36348
+ ] : []
36349
+ );
36350
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
36351
+ });
36352
+ return stored;
36353
+ }
36354
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36355
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36356
+ return readEveTraceState(state).reasoningBlocks.flatMap(
36357
+ (entry) => entry.key === key ? [
36358
+ {
36359
+ content: entry.content,
36360
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36361
+ }
36362
+ ] : []
36363
+ );
36364
+ }
36365
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36366
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36367
+ state.update((current) => {
36368
+ const normalized = normalizeEveTraceState(current);
36369
+ return {
36370
+ ...normalized,
36371
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36372
+ (entry) => entry.key !== key
36373
+ )
36374
+ };
36375
+ });
36376
+ }
36377
+ function eveOutputMessage(output) {
36378
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
36379
+ }
36380
+ function mergeEveReasoning(output, reasoning) {
36381
+ if (reasoning.length === 0) {
36382
+ return output;
34850
36383
  }
34851
- const captured = capturedModelInput(input["modelInput"]);
36384
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
36385
+ const message = isObject(choice["message"]) ? choice["message"] : {};
36386
+ return [
36387
+ {
36388
+ ...choice,
36389
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
36390
+ message: {
36391
+ ...message,
36392
+ content: "content" in message ? message["content"] : null,
36393
+ reasoning: reasoning.map((block) => ({ content: block.content })),
36394
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
36395
+ }
36396
+ }
36397
+ ];
36398
+ }
36399
+ function captureEveModelInput(state, input) {
36400
+ const sessionId = input.session.id;
36401
+ const turnId = input.turn.id;
36402
+ const stepIndex = input.step.index;
36403
+ const captured = capturedModelInput(input.modelInput);
34852
36404
  if (!captured) {
34853
36405
  return;
34854
36406
  }
@@ -34889,18 +36441,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
34889
36441
  }
34890
36442
  }
34891
36443
  function capturedModelInput(modelInput) {
34892
- if (!isObject(modelInput)) {
34893
- return void 0;
34894
- }
34895
- const messages = modelInput["messages"];
34896
- if (!Array.isArray(messages)) {
34897
- return void 0;
34898
- }
34899
- const instructions = modelInput["instructions"];
34900
- const value = [
34901
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
34902
- ...messages
34903
- ];
36444
+ const { instructions, messages } = modelInput;
36445
+ const value = [];
36446
+ if (typeof instructions === "string") {
36447
+ value.push({ content: instructions, role: "system" });
36448
+ } else if (instructions) {
36449
+ value.push(...instructions.map(capturedEveModelMessage));
36450
+ }
36451
+ value.push(...messages.map(capturedEveModelMessage));
34904
36452
  try {
34905
36453
  const cloned = JSON.parse(JSON.stringify(value));
34906
36454
  if (!Array.isArray(cloned)) {
@@ -34911,8 +36459,123 @@ function capturedModelInput(modelInput) {
34911
36459
  return void 0;
34912
36460
  }
34913
36461
  }
36462
+ function capturedEveModelMessage(message) {
36463
+ const { content, role } = message;
36464
+ if (typeof content === "string") {
36465
+ return { content, role };
36466
+ }
36467
+ return { content: content.map(capturedEveModelContentPart), role };
36468
+ }
36469
+ function capturedEveModelContentPart(part) {
36470
+ switch (part.type) {
36471
+ case "text":
36472
+ case "reasoning":
36473
+ return { text: part.text, type: part.type };
36474
+ case "image":
36475
+ return {
36476
+ image: part.image,
36477
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36478
+ type: "image"
36479
+ };
36480
+ case "file":
36481
+ case "reasoning-file":
36482
+ return {
36483
+ data: part.data,
36484
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
36485
+ mediaType: part.mediaType,
36486
+ type: part.type
36487
+ };
36488
+ case "custom":
36489
+ return {
36490
+ ..."kind" in part ? { kind: part.kind } : {},
36491
+ type: "custom"
36492
+ };
36493
+ case "tool-call":
36494
+ return {
36495
+ input: part.input,
36496
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36497
+ toolCallId: part.toolCallId,
36498
+ toolName: part.toolName,
36499
+ type: "tool-call"
36500
+ };
36501
+ case "tool-result": {
36502
+ const output = part.output;
36503
+ let capturedOutput;
36504
+ switch (output.type) {
36505
+ case "text":
36506
+ case "error-text":
36507
+ capturedOutput = { type: output.type, value: output.value };
36508
+ break;
36509
+ case "json":
36510
+ case "error-json":
36511
+ capturedOutput = { type: output.type, value: output.value };
36512
+ break;
36513
+ case "execution-denied":
36514
+ capturedOutput = {
36515
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
36516
+ type: "execution-denied"
36517
+ };
36518
+ break;
36519
+ case "content":
36520
+ capturedOutput = {
36521
+ type: "content",
36522
+ value: output.value.map(capturedEveModelContentPart)
36523
+ };
36524
+ break;
36525
+ }
36526
+ return {
36527
+ output: capturedOutput,
36528
+ toolCallId: part.toolCallId,
36529
+ toolName: part.toolName,
36530
+ type: "tool-result"
36531
+ };
36532
+ }
36533
+ case "tool-approval-request":
36534
+ return {
36535
+ approvalId: part.approvalId,
36536
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
36537
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
36538
+ toolCallId: part.toolCallId,
36539
+ type: "tool-approval-request"
36540
+ };
36541
+ case "tool-approval-response":
36542
+ return {
36543
+ approvalId: part.approvalId,
36544
+ approved: part.approved,
36545
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36546
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
36547
+ type: "tool-approval-response"
36548
+ };
36549
+ case "file-data":
36550
+ case "image-data":
36551
+ return {
36552
+ data: part.data,
36553
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
36554
+ mediaType: part.mediaType,
36555
+ type: part.type
36556
+ };
36557
+ case "file-url":
36558
+ case "image-url":
36559
+ return {
36560
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36561
+ type: part.type,
36562
+ url: part.url
36563
+ };
36564
+ case "file-id":
36565
+ case "image-file-id":
36566
+ return { fileId: part.fileId, type: part.type };
36567
+ case "file-reference":
36568
+ case "image-file-reference":
36569
+ return {
36570
+ providerReference: part.providerReference,
36571
+ type: part.type
36572
+ };
36573
+ }
36574
+ }
34914
36575
  function isCapturedModelInput(input) {
34915
- return Array.isArray(input);
36576
+ return Array.isArray(input) && input.every(
36577
+ (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))
36578
+ );
34916
36579
  }
34917
36580
  function llmInputKey(sessionId, turnId, stepIndex) {
34918
36581
  return `${sessionId}\0${turnId}\0${stepIndex}`;
@@ -35264,17 +36927,20 @@ var BraintrustObservabilityExporter = class {
35264
36927
  }
35265
36928
  onStart(exported) {
35266
36929
  if (this.spans.has(exported.id)) return;
35267
- const args = {
35268
- name: exported.name,
35269
- spanAttributes: { type: spanTypeFor(exported.type) },
35270
- startTime: epochSeconds(exported.startTime),
35271
- // Use the Mastra span id as the Braintrust row id so that
35272
- // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35273
- // attach to the right row. Without this, `SpanImpl` auto-generates a
35274
- // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35275
- // external caller could know.
35276
- event: { id: exported.id }
35277
- };
36930
+ const args = withSpanInstrumentationName(
36931
+ {
36932
+ name: exported.name,
36933
+ spanAttributes: { type: spanTypeFor(exported.type) },
36934
+ startTime: epochSeconds(exported.startTime),
36935
+ // Use the Mastra span id as the Braintrust row id so that
36936
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
36937
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
36938
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
36939
+ // external caller could know.
36940
+ event: { id: exported.id }
36941
+ },
36942
+ INSTRUMENTATION_NAMES.MASTRA
36943
+ );
35278
36944
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
35279
36945
  if (!this.capturedParent) {
35280
36946
  const probe = currentSpan();
@@ -38307,6 +39973,8 @@ var BaseNode = class {
38307
39973
  this.graph = graph;
38308
39974
  this.id = id;
38309
39975
  }
39976
+ graph;
39977
+ id;
38310
39978
  __type = "node";
38311
39979
  dependencies = [];
38312
39980
  addDependency(dependency) {
@@ -38340,6 +40008,7 @@ var PromptNode = class extends BaseNode {
38340
40008
  super(graph, id);
38341
40009
  this.prompt = prompt;
38342
40010
  }
40011
+ prompt;
38343
40012
  async build(context) {
38344
40013
  return {
38345
40014
  type: "function",
@@ -38352,6 +40021,7 @@ var GateNode = class extends BaseNode {
38352
40021
  super(graph, id);
38353
40022
  this.condition = condition;
38354
40023
  }
40024
+ condition;
38355
40025
  async build(context) {
38356
40026
  return {
38357
40027
  type: "gate",
@@ -38376,6 +40046,7 @@ var PromptTemplateNode = class extends BaseNode {
38376
40046
  super(graph, id);
38377
40047
  this.prompt = prompt;
38378
40048
  }
40049
+ prompt;
38379
40050
  async build(context) {
38380
40051
  return {
38381
40052
  type: "prompt_template",
@@ -38388,6 +40059,7 @@ var LiteralNode = class extends BaseNode {
38388
40059
  super(graph, id);
38389
40060
  this.value = value;
38390
40061
  }
40062
+ value;
38391
40063
  async build(context) {
38392
40064
  return {
38393
40065
  type: "literal",
@@ -39481,6 +41153,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
39481
41153
  this._state = _state;
39482
41154
  this.spanTypeFilter = spanTypeFilter;
39483
41155
  }
41156
+ _objectId;
41157
+ rootSpanId;
41158
+ _state;
41159
+ spanTypeFilter;
39484
41160
  static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
39485
41161
  const children = [
39486
41162
  // Base filter: root_span_id = 'value'
@@ -39931,6 +41607,8 @@ var EvalResultWithSummary = class {
39931
41607
  this.summary = summary;
39932
41608
  this.results = results;
39933
41609
  }
41610
+ summary;
41611
+ results;
39934
41612
  /**
39935
41613
  * @deprecated Use `summary` instead.
39936
41614
  */
@@ -40648,26 +42326,30 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40648
42326
  }
40649
42327
  }
40650
42328
  })();
42329
+ let timeoutId;
42330
+ let abortHandler;
42331
+ const cleanupCancellation = () => {
42332
+ if (timeoutId !== void 0) {
42333
+ clearTimeout(timeoutId);
42334
+ timeoutId = void 0;
42335
+ }
42336
+ if (abortHandler && evaluator.signal) {
42337
+ evaluator.signal.removeEventListener("abort", abortHandler);
42338
+ abortHandler = void 0;
42339
+ }
42340
+ };
40651
42341
  const cancel = async () => {
40652
42342
  await new Promise((_, reject2) => {
40653
42343
  if (cancelled) {
40654
42344
  reject2(new InternalAbortError("Evaluator already cancelled"));
40655
42345
  return;
40656
42346
  }
40657
- let timeoutId;
40658
- let abortHandler;
40659
42347
  const rejectOnce = (error) => {
40660
42348
  if (cancelled) {
40661
42349
  return;
40662
42350
  }
40663
42351
  cancelled = true;
40664
- if (timeoutId) {
40665
- clearTimeout(timeoutId);
40666
- timeoutId = void 0;
40667
- }
40668
- if (abortHandler && evaluator.signal) {
40669
- evaluator.signal.removeEventListener("abort", abortHandler);
40670
- }
42352
+ cleanupCancellation();
40671
42353
  reject2(error);
40672
42354
  };
40673
42355
  if (evaluator.timeout) {
@@ -40707,6 +42389,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40707
42389
  }
40708
42390
  throw e;
40709
42391
  } finally {
42392
+ cleanupCancellation();
40710
42393
  if (!collectResults) {
40711
42394
  collectedResults.length = 0;
40712
42395
  }
@@ -41349,6 +43032,7 @@ var ToolBuilder = class {
41349
43032
  constructor(project) {
41350
43033
  this.project = project;
41351
43034
  }
43035
+ project;
41352
43036
  taskCounter = 0;
41353
43037
  // This type definition is just a catch all so that the implementation can be
41354
43038
  // less specific than the two more specific declarations above.
@@ -41379,6 +43063,7 @@ var ScorerBuilder = class {
41379
43063
  constructor(project) {
41380
43064
  this.project = project;
41381
43065
  }
43066
+ project;
41382
43067
  taskCounter = 0;
41383
43068
  create(opts) {
41384
43069
  this.taskCounter++;
@@ -41438,6 +43123,7 @@ var ClassifierBuilder = class {
41438
43123
  constructor(project) {
41439
43124
  this.project = project;
41440
43125
  }
43126
+ project;
41441
43127
  taskCounter = 0;
41442
43128
  create(opts) {
41443
43129
  this.taskCounter++;
@@ -41473,6 +43159,7 @@ var CodeFunction = class {
41473
43159
  throw new Error("parameters are required if return type is defined");
41474
43160
  }
41475
43161
  }
43162
+ project;
41476
43163
  handler;
41477
43164
  name;
41478
43165
  slug;
@@ -41560,6 +43247,7 @@ var PromptBuilder = class {
41560
43247
  constructor(project) {
41561
43248
  this.project = project;
41562
43249
  }
43250
+ project;
41563
43251
  create(opts) {
41564
43252
  const toolFunctions = [];
41565
43253
  const rawTools = [];
@@ -41636,6 +43324,7 @@ var ParametersBuilder = class {
41636
43324
  constructor(project) {
41637
43325
  this.project = project;
41638
43326
  }
43327
+ project;
41639
43328
  create(opts) {
41640
43329
  const slug = opts.slug ?? slugify(opts.name, { lower: true, strict: true });
41641
43330
  const codeParameters = new CodeParameters(this.project, {