braintrust 3.23.1 → 3.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dev/dist/index.d.mts +11 -0
  2. package/dev/dist/index.d.ts +11 -0
  3. package/dev/dist/index.js +2881 -1518
  4. package/dev/dist/index.mjs +2298 -935
  5. package/dist/apply-auto-instrumentation.js +218 -195
  6. package/dist/apply-auto-instrumentation.mjs +30 -7
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +618 -444
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  9. package/dist/auto-instrumentations/bundler/next.cjs +618 -444
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +618 -444
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  13. package/dist/auto-instrumentations/bundler/vite.cjs +618 -444
  14. package/dist/auto-instrumentations/bundler/vite.d.mts +1 -1
  15. package/dist/auto-instrumentations/bundler/vite.d.ts +1 -1
  16. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  17. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +618 -444
  18. package/dist/auto-instrumentations/bundler/webpack.cjs +618 -444
  19. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  20. package/dist/auto-instrumentations/{chunk-KIMLYPRW.mjs → chunk-7P6563SW.mjs} +1 -1
  21. package/dist/auto-instrumentations/{chunk-YXLNSAMJ.mjs → chunk-CZ24KNHT.mjs} +591 -424
  22. package/dist/auto-instrumentations/{chunk-EXY7QCJD.mjs → chunk-JPVCUKTY.mjs} +30 -21
  23. package/dist/auto-instrumentations/hook.mjs +778 -457
  24. package/dist/auto-instrumentations/index.cjs +590 -424
  25. package/dist/auto-instrumentations/index.mjs +1 -1
  26. package/dist/browser.d.mts +138 -9
  27. package/dist/browser.d.ts +138 -9
  28. package/dist/browser.js +2700 -1011
  29. package/dist/browser.mjs +2700 -1011
  30. package/dist/{chunk-RBXD2KYN.mjs → chunk-2SANLSWX.mjs} +1577 -513
  31. package/dist/{chunk-36IPYKMG.mjs → chunk-ABR2QWDP.mjs} +704 -438
  32. package/dist/{chunk-B6ZQIAK3.js → chunk-GSIDVFE6.js} +2361 -1297
  33. package/dist/{chunk-CDIKAHDZ.js → chunk-P25IOOU4.js} +706 -440
  34. package/dist/cli.js +2307 -940
  35. package/dist/edge-light.js +2700 -1011
  36. package/dist/edge-light.mjs +2700 -1011
  37. package/dist/index.d.mts +138 -9
  38. package/dist/index.d.ts +138 -9
  39. package/dist/index.js +955 -551
  40. package/dist/index.mjs +493 -89
  41. package/dist/instrumentation/index.d.mts +135 -9
  42. package/dist/instrumentation/index.d.ts +135 -9
  43. package/dist/instrumentation/index.js +2554 -1009
  44. package/dist/instrumentation/index.mjs +2554 -1009
  45. package/dist/vitest-evals-reporter.js +17 -16
  46. package/dist/vitest-evals-reporter.mjs +3 -2
  47. package/dist/workerd.js +2700 -1011
  48. package/dist/workerd.mjs +2700 -1011
  49. package/package.json +4 -4
  50. package/util/dist/index.js +4 -0
  51. package/util/dist/index.mjs +4 -0
package/dist/browser.mjs CHANGED
@@ -24,6 +24,7 @@ var DefaultChannel = class {
24
24
  constructor(name) {
25
25
  this.name = name;
26
26
  }
27
+ name;
27
28
  hasSubscribers = false;
28
29
  subscribe(_subscription) {
29
30
  }
@@ -1080,6 +1081,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
1080
1081
  constructor(data) {
1081
1082
  this.data = data;
1082
1083
  }
1084
+ data;
1083
1085
  toStr() {
1084
1086
  const jsonObj = {
1085
1087
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -1587,6 +1589,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
1587
1589
  constructor(data) {
1588
1590
  this.data = data;
1589
1591
  }
1592
+ data;
1590
1593
  toStr() {
1591
1594
  const jsonObj = {
1592
1595
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -4822,6 +4825,144 @@ var SpanCache = class {
4822
4825
  }
4823
4826
  };
4824
4827
 
4828
+ // src/span-origin.ts
4829
+ var INSTRUMENTATION_NAMES = {
4830
+ AI_SDK: "ai-sdk",
4831
+ ANTHROPIC: "anthropic",
4832
+ BEDROCK_RUNTIME: "bedrock-runtime",
4833
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
4834
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
4835
+ COHERE: "cohere",
4836
+ CURSOR_SDK: "cursor-sdk",
4837
+ EVE: "eve",
4838
+ FLUE: "flue",
4839
+ GENKIT: "genkit",
4840
+ GITHUB_COPILOT: "github-copilot",
4841
+ GOOGLE_ADK: "google-adk",
4842
+ GOOGLE_GENAI: "google-genai",
4843
+ GROQ: "groq",
4844
+ HUGGINGFACE: "huggingface",
4845
+ LANGCHAIN: "langchain",
4846
+ LANGSMITH: "langsmith",
4847
+ MASTRA: "mastra",
4848
+ MISTRAL: "mistral",
4849
+ OPENAI: "openai",
4850
+ OPENAI_AGENTS: "openai-agents",
4851
+ OPENAI_CODEX: "openai-codex",
4852
+ OPENROUTER: "openrouter",
4853
+ OPENROUTER_AGENT: "openrouter-agent",
4854
+ PI_CODING_AGENT: "pi-coding-agent",
4855
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
4856
+ };
4857
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
4858
+ "braintrust.spanInstrumentationName"
4859
+ );
4860
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
4861
+ function withSpanInstrumentationName(args, instrumentationName) {
4862
+ return {
4863
+ ...args,
4864
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
4865
+ };
4866
+ }
4867
+ function getSpanInstrumentationName(args) {
4868
+ if (typeof args !== "object" || args === null) {
4869
+ return void 0;
4870
+ }
4871
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
4872
+ return isSpanInstrumentationName(value) ? value : void 0;
4873
+ }
4874
+ function detectSpanOriginEnvironment(explicit) {
4875
+ if (explicit) return explicit;
4876
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
4877
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
4878
+ if (envType || envName) {
4879
+ return {
4880
+ ...envType ? { type: envType } : {},
4881
+ ...envName ? { name: envName } : {}
4882
+ };
4883
+ }
4884
+ const ci = firstPresent([
4885
+ ["GITHUB_ACTIONS", "github_actions"],
4886
+ ["GITLAB_CI", "gitlab_ci"],
4887
+ ["CIRCLECI", "circleci"],
4888
+ ["BUILDKITE", "buildkite"],
4889
+ ["JENKINS_URL", "jenkins"],
4890
+ ["JENKINS_HOME", "jenkins"],
4891
+ ["TF_BUILD", "azure_pipelines"],
4892
+ ["TEAMCITY_VERSION", "teamcity"],
4893
+ ["TRAVIS", "travis"],
4894
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
4895
+ ]);
4896
+ if (ci) return { type: "ci", name: ci };
4897
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
4898
+ const earlyServer = firstPresent([
4899
+ ["VERCEL", "vercel"],
4900
+ ["NETLIFY", "netlify"]
4901
+ ]);
4902
+ if (earlyServer) return { type: "server", name: earlyServer };
4903
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
4904
+ return { type: "server", name: "ecs" };
4905
+ }
4906
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
4907
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
4908
+ return { type: "server", name: "ecs" };
4909
+ }
4910
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
4911
+ return { type: "server", name: "aws_lambda" };
4912
+ }
4913
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
4914
+ return { type: "server", name: "aws_lambda" };
4915
+ }
4916
+ const server = firstPresent([
4917
+ ["K_SERVICE", "cloud_run"],
4918
+ ["FUNCTION_TARGET", "gcp_functions"],
4919
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
4920
+ ["DYNO", "heroku"],
4921
+ ["FLY_APP_NAME", "fly"],
4922
+ ["RAILWAY_ENVIRONMENT", "railway"],
4923
+ ["RENDER_SERVICE_NAME", "render"]
4924
+ ]);
4925
+ if (server) return { type: "server", name: server };
4926
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
4927
+ }
4928
+ function makeSpanOrigin(instrumentationName, environment) {
4929
+ return {
4930
+ name: "braintrust.sdk.javascript",
4931
+ version: SDK_VERSION,
4932
+ instrumentation: { name: instrumentationName },
4933
+ ...environment ? { environment } : {}
4934
+ };
4935
+ }
4936
+ function mergeSpanOriginContext(context, instrumentationName, environment) {
4937
+ const next = { ...context ?? {} };
4938
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
4939
+ next.span_origin = {
4940
+ ...makeSpanOrigin(instrumentationName, environment),
4941
+ ...current
4942
+ };
4943
+ return next;
4944
+ }
4945
+ function isSpanInstrumentationName(value) {
4946
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
4947
+ }
4948
+ function firstPresent(entries) {
4949
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
4950
+ }
4951
+ function deploymentModeEnvironment(_key, value) {
4952
+ if (!value) return void 0;
4953
+ const normalized = value.toLowerCase();
4954
+ if (normalized === "production" || normalized === "staging") {
4955
+ return { type: "server", name: normalized };
4956
+ }
4957
+ if (normalized === "development" || normalized === "local") {
4958
+ return { type: "local", name: normalized };
4959
+ }
4960
+ return { name: value };
4961
+ }
4962
+ function isObject2(value) {
4963
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4964
+ }
4965
+
4825
4966
  // src/logger.ts
4826
4967
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
4827
4968
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -4870,6 +5011,7 @@ var LoginInvalidOrgError = class extends Error {
4870
5011
  super(message);
4871
5012
  this.message = message;
4872
5013
  }
5014
+ message;
4873
5015
  };
4874
5016
  var REDACTION_FIELDS = [
4875
5017
  "input",
@@ -4885,6 +5027,8 @@ var MaskingError = class {
4885
5027
  this.fieldName = fieldName;
4886
5028
  this.errorType = errorType;
4887
5029
  }
5030
+ fieldName;
5031
+ errorType;
4888
5032
  get errorMsg() {
4889
5033
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
4890
5034
  }
@@ -5094,7 +5238,10 @@ var BraintrustState = class _BraintrustState {
5094
5238
  diskCache: parametersDiskCache
5095
5239
  });
5096
5240
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
5241
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
5242
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5097
5243
  }
5244
+ loginParams;
5098
5245
  id;
5099
5246
  currentExperiment;
5100
5247
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -5129,6 +5276,19 @@ var BraintrustState = class _BraintrustState {
5129
5276
  _idGenerator = null;
5130
5277
  _contextManager = null;
5131
5278
  _otelFlushCallback = null;
5279
+ spanOriginEnvironment;
5280
+ traceContextSigningSecret;
5281
+ /** @internal */
5282
+ _internalSetTraceContextSigningSecret(secret) {
5283
+ const normalizedSecret = secret?.trim();
5284
+ if (normalizedSecret) {
5285
+ this.traceContextSigningSecret = normalizedSecret;
5286
+ }
5287
+ }
5288
+ /** @internal */
5289
+ _internalGetTraceContextSigningSecret() {
5290
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
5291
+ }
5132
5292
  resetLoginInfo() {
5133
5293
  this.appUrl = null;
5134
5294
  this.appPublicUrl = null;
@@ -5189,6 +5349,7 @@ var BraintrustState = class _BraintrustState {
5189
5349
  this.gitMetadataSettings = other.gitMetadataSettings;
5190
5350
  this.debugLogLevel = other.debugLogLevel;
5191
5351
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
5352
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
5192
5353
  setGlobalDebugLogLevel(
5193
5354
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
5194
5355
  );
@@ -5276,6 +5437,7 @@ var BraintrustState = class _BraintrustState {
5276
5437
  return this.debugLogLevelConfigured;
5277
5438
  }
5278
5439
  async login(loginParams) {
5440
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
5279
5441
  this.setDebugLogLevel(loginParams.debugLogLevel);
5280
5442
  if (this.apiUrl && !loginParams.forceLogin) {
5281
5443
  return;
@@ -7255,6 +7417,7 @@ function init(projectOrOptions, optionalOptions) {
7255
7417
  throw new Error("Cannot open and update an experiment at the same time");
7256
7418
  }
7257
7419
  const state = stateArg ?? _globalState;
7420
+ state._internalSetTraceContextSigningSecret(apiKey);
7258
7421
  state.enforceQueueSizeLimit(false);
7259
7422
  if (open) {
7260
7423
  if (isEmpty2(experiment)) {
@@ -7747,6 +7910,7 @@ function initLogger(options = {}) {
7747
7910
  orgName,
7748
7911
  forceLogin,
7749
7912
  debugLogLevel,
7913
+ environment,
7750
7914
  fetch: fetch2,
7751
7915
  state: stateArg
7752
7916
  } = options || {};
@@ -7762,7 +7926,9 @@ function initLogger(options = {}) {
7762
7926
  project_id: projectId
7763
7927
  };
7764
7928
  const state = stateArg ?? _globalState;
7929
+ state._internalSetTraceContextSigningSecret(apiKey);
7765
7930
  state.setDebugLogLevel(debugLogLevel);
7931
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
7766
7932
  state.enforceQueueSizeLimit(true);
7767
7933
  const lazyMetadata = new LazyValue(
7768
7934
  async () => {
@@ -8824,7 +8990,9 @@ function deepCopyEvent(event) {
8824
8990
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
8825
8991
  const attachmentMarker = ++deepCopyEventMarkerCounter;
8826
8992
  const serialized = JSON.stringify(event, (_k, v) => {
8827
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
8993
+ if (v instanceof Error) {
8994
+ return v.message;
8995
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
8828
8996
  return `<span>`;
8829
8997
  } else if (v instanceof Experiment2) {
8830
8998
  return `<experiment>`;
@@ -8954,6 +9122,11 @@ var ObjectFetcher = class {
8954
9122
  this._internal_btql = _internal_btql;
8955
9123
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
8956
9124
  }
9125
+ objectType;
9126
+ pinnedVersion;
9127
+ mutateRecord;
9128
+ _internal_btql;
9129
+ _internalBrainstoreRealtime;
8957
9130
  _fetchedData = void 0;
8958
9131
  get id() {
8959
9132
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -9392,6 +9565,8 @@ var ReadonlyExperiment = class extends ObjectFetcher {
9392
9565
  this.state = state;
9393
9566
  this.lazyMetadata = lazyMetadata;
9394
9567
  }
9568
+ state;
9569
+ lazyMetadata;
9395
9570
  get id() {
9396
9571
  return (async () => {
9397
9572
  return (await this.lazyMetadata.get()).experiment.id;
@@ -9485,6 +9660,7 @@ var SpanImpl = class _SpanImpl {
9485
9660
  constructor(args) {
9486
9661
  this._state = args.state;
9487
9662
  this._propagatedState = args.propagatedState;
9663
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
9488
9664
  const spanAttributes = args.spanAttributes ?? {};
9489
9665
  const rawEvent = args.event ?? {};
9490
9666
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -9514,7 +9690,11 @@ var SpanImpl = class _SpanImpl {
9514
9690
  metrics: {
9515
9691
  start: args.startTime ?? getCurrentUnixTimestamp()
9516
9692
  },
9517
- context: { ...callerLocation },
9693
+ context: mergeSpanOriginContext(
9694
+ { ...callerLocation },
9695
+ instrumentationName,
9696
+ this._state.spanOriginEnvironment
9697
+ ),
9518
9698
  span_attributes: {
9519
9699
  name,
9520
9700
  type,
@@ -9888,6 +10068,7 @@ var Dataset2 = class extends ObjectFetcher {
9888
10068
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
9889
10069
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
9890
10070
  }
10071
+ state;
9891
10072
  lazyMetadata;
9892
10073
  __braintrust_dataset_marker = true;
9893
10074
  newRecords = 0;
@@ -10402,6 +10583,9 @@ var Prompt2 = class _Prompt {
10402
10583
  this.noTrace = noTrace;
10403
10584
  void this.__braintrust_prompt_marker;
10404
10585
  }
10586
+ metadata;
10587
+ defaults;
10588
+ noTrace;
10405
10589
  parsedPromptData;
10406
10590
  hasParsedPromptData = false;
10407
10591
  __braintrust_prompt_marker = true;
@@ -10639,6 +10823,7 @@ var RemoteEvalParameters = class {
10639
10823
  this.metadata = metadata;
10640
10824
  void this.__braintrust_parameters_marker;
10641
10825
  }
10826
+ metadata;
10642
10827
  __braintrust_parameters_marker = true;
10643
10828
  get id() {
10644
10829
  return this.metadata.id;
@@ -11491,15 +11676,25 @@ function hasChoices(value) {
11491
11676
  function normalizeMetadata(metadata) {
11492
11677
  return isObject(metadata) ? metadata : void 0;
11493
11678
  }
11494
- function startSpanForEvent(config, event, channelName) {
11679
+ function startSpanForEvent(config, event, channelName, instrumentationName) {
11495
11680
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
11496
11681
  config,
11497
11682
  event
11498
11683
  );
11499
- const span = startSpan({
11500
- name,
11501
- spanAttributes
11502
- });
11684
+ const spanArgs = withSpanInstrumentationName(
11685
+ {
11686
+ name,
11687
+ spanAttributes
11688
+ },
11689
+ instrumentationName
11690
+ );
11691
+ let span;
11692
+ try {
11693
+ span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
11694
+ } catch (error) {
11695
+ debugLogger.error(`Error starting span for ${channelName}:`, error);
11696
+ span = startSpan(spanArgs);
11697
+ }
11503
11698
  const startTime = getCurrentUnixTimestamp();
11504
11699
  try {
11505
11700
  const { input, metadata } = config.extractInput(
@@ -11530,7 +11725,7 @@ function shouldTraceEvent(config, event, channelName) {
11530
11725
  return true;
11531
11726
  }
11532
11727
  }
11533
- function ensureSpanStateForEvent(states, config, event, channelName) {
11728
+ function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
11534
11729
  const key = event;
11535
11730
  const existing = states.get(key);
11536
11731
  if (existing) {
@@ -11539,11 +11734,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
11539
11734
  if (!shouldTraceEvent(config, event, channelName)) {
11540
11735
  return void 0;
11541
11736
  }
11542
- const created = startSpanForEvent(config, event, channelName);
11737
+ const created = startSpanForEvent(
11738
+ config,
11739
+ event,
11740
+ channelName,
11741
+ instrumentationName
11742
+ );
11543
11743
  states.set(key, created);
11544
11744
  return created;
11545
11745
  }
11546
- function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
11746
+ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName, instrumentationName) {
11547
11747
  const state = _internalGetGlobalState();
11548
11748
  const startChannel = tracingChannel2.start;
11549
11749
  const contextManager = state?.contextManager;
@@ -11561,7 +11761,8 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11561
11761
  states,
11562
11762
  config,
11563
11763
  event,
11564
- channelName
11764
+ channelName,
11765
+ instrumentationName
11565
11766
  );
11566
11767
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
11567
11768
  }
@@ -11570,15 +11771,21 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
11570
11771
  startChannel.unbindStore(currentSpanStore);
11571
11772
  };
11572
11773
  }
11573
- function logErrorAndEnd(states, event) {
11774
+ function logErrorAndEnd(states, event, channelName) {
11574
11775
  const spanData = states.get(event);
11575
11776
  if (!spanData) {
11576
11777
  return;
11577
11778
  }
11578
- spanData.span.log({
11579
- error: event.error.message
11580
- });
11581
- spanData.span.end();
11779
+ try {
11780
+ spanData.span.log({ error: event.error });
11781
+ } catch (error) {
11782
+ debugLogger.error(`Error logging failure for ${channelName}:`, error);
11783
+ }
11784
+ try {
11785
+ spanData.span.end();
11786
+ } catch (error) {
11787
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
11788
+ }
11582
11789
  states.delete(event);
11583
11790
  }
11584
11791
  function runStreamingCompletionHook(args) {
@@ -11628,7 +11835,8 @@ function traceAsyncChannel(channel2, config) {
11628
11835
  tracingChannel2,
11629
11836
  states,
11630
11837
  config,
11631
- channelName
11838
+ channelName,
11839
+ channel2.instrumentationName
11632
11840
  );
11633
11841
  const handlers = {
11634
11842
  start: (event) => {
@@ -11639,7 +11847,8 @@ function traceAsyncChannel(channel2, config) {
11639
11847
  states,
11640
11848
  config,
11641
11849
  event,
11642
- channelName
11850
+ channelName,
11851
+ channel2.instrumentationName
11643
11852
  );
11644
11853
  },
11645
11854
  asyncEnd: (event) => {
@@ -11676,7 +11885,7 @@ function traceAsyncChannel(channel2, config) {
11676
11885
  }
11677
11886
  },
11678
11887
  error: (event) => {
11679
- logErrorAndEnd(states, event);
11888
+ logErrorAndEnd(states, event, channelName);
11680
11889
  }
11681
11890
  };
11682
11891
  tracingChannel2.subscribe(handlers);
@@ -11693,7 +11902,8 @@ function traceStreamingChannel(channel2, config) {
11693
11902
  tracingChannel2,
11694
11903
  states,
11695
11904
  config,
11696
- channelName
11905
+ channelName,
11906
+ channel2.instrumentationName
11697
11907
  );
11698
11908
  const handlers = {
11699
11909
  start: (event) => {
@@ -11704,7 +11914,8 @@ function traceStreamingChannel(channel2, config) {
11704
11914
  states,
11705
11915
  config,
11706
11916
  event,
11707
- channelName
11917
+ channelName,
11918
+ channel2.instrumentationName
11708
11919
  );
11709
11920
  },
11710
11921
  asyncEnd: (event) => {
@@ -11723,6 +11934,7 @@ function traceStreamingChannel(channel2, config) {
11723
11934
  }
11724
11935
  },
11725
11936
  onComplete: (chunks) => {
11937
+ let completion2;
11726
11938
  try {
11727
11939
  let output;
11728
11940
  let metrics;
@@ -11753,18 +11965,11 @@ function traceStreamingChannel(channel2, config) {
11753
11965
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
11754
11966
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
11755
11967
  }
11756
- runStreamingCompletionHook({
11757
- channelName,
11758
- chunks,
11759
- config,
11760
- endEvent: asyncEndEvent,
11968
+ completion2 = {
11761
11969
  ...metadata !== void 0 ? { metadata } : {},
11762
11970
  metrics,
11763
- output,
11764
- result: asyncEndEvent.result,
11765
- span,
11766
- startTime
11767
- });
11971
+ output
11972
+ };
11768
11973
  span.log({
11769
11974
  output,
11770
11975
  ...metadata !== void 0 ? { metadata } : {},
@@ -11776,11 +11981,49 @@ function traceStreamingChannel(channel2, config) {
11776
11981
  error
11777
11982
  );
11778
11983
  } finally {
11779
- span.end();
11984
+ try {
11985
+ span.end();
11986
+ } catch (error) {
11987
+ debugLogger.error(
11988
+ `Error ending span for ${channelName}:`,
11989
+ error
11990
+ );
11991
+ }
11780
11992
  states.delete(event);
11781
11993
  }
11994
+ if (completion2) {
11995
+ runStreamingCompletionHook({
11996
+ channelName,
11997
+ chunks,
11998
+ config,
11999
+ endEvent: asyncEndEvent,
12000
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
12001
+ metrics: completion2.metrics,
12002
+ output: completion2.output,
12003
+ result: asyncEndEvent.result,
12004
+ span,
12005
+ startTime
12006
+ });
12007
+ }
11782
12008
  },
11783
12009
  onError: (error) => {
12010
+ try {
12011
+ span.log({ error });
12012
+ } catch (loggingError) {
12013
+ debugLogger.error(
12014
+ `Error logging failure for ${channelName}:`,
12015
+ loggingError
12016
+ );
12017
+ }
12018
+ try {
12019
+ span.end();
12020
+ } catch (endingError) {
12021
+ debugLogger.error(
12022
+ `Error ending span for ${channelName}:`,
12023
+ endingError
12024
+ );
12025
+ }
12026
+ states.delete(event);
11784
12027
  runStreamingErrorHook({
11785
12028
  channelName,
11786
12029
  config,
@@ -11789,11 +12032,6 @@ function traceStreamingChannel(channel2, config) {
11789
12032
  span,
11790
12033
  startTime
11791
12034
  });
11792
- span.log({
11793
- error: error.message
11794
- });
11795
- span.end();
11796
- states.delete(event);
11797
12035
  }
11798
12036
  });
11799
12037
  return;
@@ -11808,6 +12046,7 @@ function traceStreamingChannel(channel2, config) {
11808
12046
  states.delete(event);
11809
12047
  return;
11810
12048
  }
12049
+ let completion;
11811
12050
  try {
11812
12051
  const output = config.extractOutput(
11813
12052
  asyncEndEvent.result,
@@ -11822,17 +12061,11 @@ function traceStreamingChannel(channel2, config) {
11822
12061
  asyncEndEvent.result,
11823
12062
  asyncEndEvent
11824
12063
  );
11825
- runStreamingCompletionHook({
11826
- channelName,
11827
- config,
11828
- endEvent: asyncEndEvent,
12064
+ completion = {
11829
12065
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
11830
12066
  metrics,
11831
- output,
11832
- result: asyncEndEvent.result,
11833
- span,
11834
- startTime
11835
- });
12067
+ output
12068
+ };
11836
12069
  span.log({
11837
12070
  output,
11838
12071
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -11841,12 +12074,30 @@ function traceStreamingChannel(channel2, config) {
11841
12074
  } catch (error) {
11842
12075
  debugLogger.error(`Error extracting output for ${channelName}:`, error);
11843
12076
  } finally {
11844
- span.end();
12077
+ try {
12078
+ span.end();
12079
+ } catch (error) {
12080
+ debugLogger.error(`Error ending span for ${channelName}:`, error);
12081
+ }
11845
12082
  states.delete(event);
11846
12083
  }
12084
+ if (completion) {
12085
+ runStreamingCompletionHook({
12086
+ channelName,
12087
+ config,
12088
+ endEvent: asyncEndEvent,
12089
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
12090
+ metrics: completion.metrics,
12091
+ output: completion.output,
12092
+ result: asyncEndEvent.result,
12093
+ span,
12094
+ startTime
12095
+ });
12096
+ }
11847
12097
  },
11848
12098
  error: (event) => {
11849
12099
  const spanData = states.get(event);
12100
+ logErrorAndEnd(states, event, channelName);
11850
12101
  if (spanData) {
11851
12102
  runStreamingErrorHook({
11852
12103
  channelName,
@@ -11857,7 +12108,6 @@ function traceStreamingChannel(channel2, config) {
11857
12108
  startTime: spanData.startTime
11858
12109
  });
11859
12110
  }
11860
- logErrorAndEnd(states, event);
11861
12111
  }
11862
12112
  };
11863
12113
  tracingChannel2.subscribe(handlers);
@@ -11874,7 +12124,8 @@ function traceSyncStreamChannel(channel2, config) {
11874
12124
  tracingChannel2,
11875
12125
  states,
11876
12126
  config,
11877
- channelName
12127
+ channelName,
12128
+ channel2.instrumentationName
11878
12129
  );
11879
12130
  const handlers = {
11880
12131
  start: (event) => {
@@ -11885,7 +12136,8 @@ function traceSyncStreamChannel(channel2, config) {
11885
12136
  states,
11886
12137
  config,
11887
12138
  event,
11888
- channelName
12139
+ channelName,
12140
+ channel2.instrumentationName
11889
12141
  );
11890
12142
  },
11891
12143
  end: (event) => {
@@ -11979,7 +12231,7 @@ function traceSyncStreamChannel(channel2, config) {
11979
12231
  handleResolvedResult(endEvent.result);
11980
12232
  },
11981
12233
  error: (event) => {
11982
- logErrorAndEnd(states, event);
12234
+ logErrorAndEnd(states, event, channelName);
11983
12235
  }
11984
12236
  };
11985
12237
  tracingChannel2.subscribe(handlers);
@@ -12158,7 +12410,8 @@ function processInputAttachments(input) {
12158
12410
  function channel(spec) {
12159
12411
  return spec;
12160
12412
  }
12161
- function defineChannels(pkg, channels) {
12413
+ function defineChannels(pkg, channels, options) {
12414
+ const { instrumentationName } = options;
12162
12415
  return Object.fromEntries(
12163
12416
  Object.entries(channels).map(([key, spec]) => {
12164
12417
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -12171,6 +12424,7 @@ function defineChannels(pkg, channels) {
12171
12424
  key,
12172
12425
  {
12173
12426
  ...asyncSpec,
12427
+ instrumentationName,
12174
12428
  tracingChannel: tracingChannel3,
12175
12429
  tracePromise: (fn, context) => tracingChannel3().tracePromise(
12176
12430
  fn,
@@ -12188,6 +12442,7 @@ function defineChannels(pkg, channels) {
12188
12442
  key,
12189
12443
  {
12190
12444
  ...syncSpec,
12445
+ instrumentationName,
12191
12446
  tracingChannel: tracingChannel2,
12192
12447
  traceSync: (fn, context) => tracingChannel2().traceSync(
12193
12448
  fn,
@@ -12201,44 +12456,48 @@ function defineChannels(pkg, channels) {
12201
12456
  }
12202
12457
 
12203
12458
  // src/instrumentation/plugins/openai-channels.ts
12204
- var openAIChannels = defineChannels("openai", {
12205
- chatCompletionsCreate: channel({
12206
- channelName: "chat.completions.create",
12207
- kind: "async"
12208
- }),
12209
- embeddingsCreate: channel({
12210
- channelName: "embeddings.create",
12211
- kind: "async"
12212
- }),
12213
- betaChatCompletionsParse: channel({
12214
- channelName: "beta.chat.completions.parse",
12215
- kind: "async"
12216
- }),
12217
- betaChatCompletionsStream: channel({
12218
- channelName: "beta.chat.completions.stream",
12219
- kind: "sync-stream"
12220
- }),
12221
- moderationsCreate: channel({
12222
- channelName: "moderations.create",
12223
- kind: "async"
12224
- }),
12225
- responsesCreate: channel({
12226
- channelName: "responses.create",
12227
- kind: "async"
12228
- }),
12229
- responsesStream: channel({
12230
- channelName: "responses.stream",
12231
- kind: "sync-stream"
12232
- }),
12233
- responsesParse: channel({
12234
- channelName: "responses.parse",
12235
- kind: "async"
12236
- }),
12237
- responsesCompact: channel({
12238
- channelName: "responses.compact",
12239
- kind: "async"
12240
- })
12241
- });
12459
+ var openAIChannels = defineChannels(
12460
+ "openai",
12461
+ {
12462
+ chatCompletionsCreate: channel({
12463
+ channelName: "chat.completions.create",
12464
+ kind: "async"
12465
+ }),
12466
+ embeddingsCreate: channel({
12467
+ channelName: "embeddings.create",
12468
+ kind: "async"
12469
+ }),
12470
+ betaChatCompletionsParse: channel({
12471
+ channelName: "beta.chat.completions.parse",
12472
+ kind: "async"
12473
+ }),
12474
+ betaChatCompletionsStream: channel({
12475
+ channelName: "beta.chat.completions.stream",
12476
+ kind: "sync-stream"
12477
+ }),
12478
+ moderationsCreate: channel({
12479
+ channelName: "moderations.create",
12480
+ kind: "async"
12481
+ }),
12482
+ responsesCreate: channel({
12483
+ channelName: "responses.create",
12484
+ kind: "async"
12485
+ }),
12486
+ responsesStream: channel({
12487
+ channelName: "responses.stream",
12488
+ kind: "sync-stream"
12489
+ }),
12490
+ responsesParse: channel({
12491
+ channelName: "responses.parse",
12492
+ kind: "async"
12493
+ }),
12494
+ responsesCompact: channel({
12495
+ channelName: "responses.compact",
12496
+ kind: "async"
12497
+ })
12498
+ },
12499
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
12500
+ );
12242
12501
 
12243
12502
  // src/openai-utils.ts
12244
12503
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -12753,16 +13012,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
12753
13012
  }
12754
13013
 
12755
13014
  // src/instrumentation/plugins/openai-codex-channels.ts
12756
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
12757
- run: channel({
12758
- channelName: "Thread.run",
12759
- kind: "async"
12760
- }),
12761
- runStreamed: channel({
12762
- channelName: "Thread.runStreamed",
12763
- kind: "async"
12764
- })
12765
- });
13015
+ var openAICodexChannels = defineChannels(
13016
+ "@openai/codex-sdk",
13017
+ {
13018
+ run: channel({
13019
+ channelName: "Thread.run",
13020
+ kind: "async"
13021
+ }),
13022
+ runStreamed: channel({
13023
+ channelName: "Thread.runStreamed",
13024
+ kind: "async"
13025
+ })
13026
+ },
13027
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
13028
+ );
12766
13029
 
12767
13030
  // src/instrumentation/plugins/openai-codex-plugin.ts
12768
13031
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -12850,10 +13113,15 @@ function startCodexRun(event, operation) {
12850
13113
  provider: "openai",
12851
13114
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
12852
13115
  };
12853
- const span = startSpan({
12854
- name: "OpenAI Codex",
12855
- spanAttributes: { type: "task" /* TASK */ }
12856
- });
13116
+ const span = startSpan(
13117
+ withSpanInstrumentationName(
13118
+ {
13119
+ name: "OpenAI Codex",
13120
+ spanAttributes: { type: "task" /* TASK */ }
13121
+ },
13122
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13123
+ )
13124
+ );
12857
13125
  const startTime = getCurrentUnixTimestamp();
12858
13126
  safeLog(span, {
12859
13127
  input: sanitizedInput,
@@ -13002,7 +13270,12 @@ async function createCompletedItemSpan(state, item) {
13002
13270
  if (!spanArgs) {
13003
13271
  return;
13004
13272
  }
13005
- const span = startSpan(spanArgs.start);
13273
+ const span = startSpan(
13274
+ withSpanInstrumentationName(
13275
+ spanArgs.start,
13276
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13277
+ )
13278
+ );
13006
13279
  safeLog(span, spanArgs.end);
13007
13280
  span.end();
13008
13281
  }
@@ -13044,15 +13317,20 @@ async function ensureActiveLlmSpan(state) {
13044
13317
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
13045
13318
  "openai_codex.llm_sequence": sequence
13046
13319
  };
13047
- const span = startSpan({
13048
- event: {
13049
- ...sequence === 1 ? { input: state.input } : {},
13050
- metadata
13051
- },
13052
- name: "OpenAI Codex LLM",
13053
- parent: await state.span.export(),
13054
- spanAttributes: { type: "llm" /* LLM */ }
13055
- });
13320
+ const span = startSpan(
13321
+ withSpanInstrumentationName(
13322
+ {
13323
+ event: {
13324
+ ...sequence === 1 ? { input: state.input } : {},
13325
+ metadata
13326
+ },
13327
+ name: "OpenAI Codex LLM",
13328
+ parent: await state.span.export(),
13329
+ spanAttributes: { type: "llm" /* LLM */ }
13330
+ },
13331
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13332
+ )
13333
+ );
13056
13334
  state.activeLlmSpan = {
13057
13335
  anonymousMessages: [],
13058
13336
  anonymousReasoning: [],
@@ -13098,7 +13376,15 @@ async function startCodexItemSpan(state, item) {
13098
13376
  if (!spanArgs) {
13099
13377
  return;
13100
13378
  }
13101
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
13379
+ state.activeItemSpans.set(
13380
+ itemId,
13381
+ startSpan(
13382
+ withSpanInstrumentationName(
13383
+ spanArgs.start,
13384
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
13385
+ )
13386
+ )
13387
+ );
13102
13388
  }
13103
13389
  function updateCodexItem(state, item) {
13104
13390
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -13408,20 +13694,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
13408
13694
  }
13409
13695
 
13410
13696
  // src/instrumentation/plugins/anthropic-channels.ts
13411
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
13412
- messagesCreate: channel({
13413
- channelName: "messages.create",
13414
- kind: "async"
13415
- }),
13416
- betaMessagesCreate: channel({
13417
- channelName: "beta.messages.create",
13418
- kind: "async"
13419
- }),
13420
- betaMessagesToolRunner: channel({
13421
- channelName: "beta.messages.toolRunner",
13422
- kind: "sync-stream"
13423
- })
13424
- });
13697
+ var anthropicChannels = defineChannels(
13698
+ "@anthropic-ai/sdk",
13699
+ {
13700
+ messagesCreate: channel({
13701
+ channelName: "messages.create",
13702
+ kind: "async"
13703
+ }),
13704
+ betaMessagesCreate: channel({
13705
+ channelName: "beta.messages.create",
13706
+ kind: "async"
13707
+ }),
13708
+ betaMessagesToolRunner: channel({
13709
+ channelName: "beta.messages.toolRunner",
13710
+ kind: "sync-stream"
13711
+ })
13712
+ },
13713
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
13714
+ );
13425
13715
 
13426
13716
  // src/instrumentation/plugins/anthropic-plugin.ts
13427
13717
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -13494,12 +13784,17 @@ var AnthropicPlugin = class extends BasePlugin {
13494
13784
  return;
13495
13785
  }
13496
13786
  const params = event.arguments[0] ?? {};
13497
- const span = startSpan({
13498
- name: "anthropic.beta.messages.toolRunner",
13499
- spanAttributes: {
13500
- type: "task" /* TASK */
13501
- }
13502
- });
13787
+ const span = startSpan(
13788
+ withSpanInstrumentationName(
13789
+ {
13790
+ name: "anthropic.beta.messages.toolRunner",
13791
+ spanAttributes: {
13792
+ type: "task" /* TASK */
13793
+ }
13794
+ },
13795
+ INSTRUMENTATION_NAMES.ANTHROPIC
13796
+ )
13797
+ );
13503
13798
  span.log({
13504
13799
  input: processAttachmentsInInput(
13505
13800
  coalesceInput(params.messages ?? [], params.system)
@@ -13640,19 +13935,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
13640
13935
  return finalizeError(error);
13641
13936
  }
13642
13937
  },
13643
- {
13644
- event: {
13645
- input: getAnthropicToolRunnerInput(args),
13646
- metadata: {
13647
- "gen_ai.tool.name": toolName,
13648
- provider: "anthropic"
13938
+ withSpanInstrumentationName(
13939
+ {
13940
+ event: {
13941
+ input: getAnthropicToolRunnerInput(args),
13942
+ metadata: {
13943
+ "gen_ai.tool.name": toolName,
13944
+ provider: "anthropic"
13945
+ }
13946
+ },
13947
+ name: `tool: ${toolName}`,
13948
+ spanAttributes: {
13949
+ type: "tool" /* TOOL */
13649
13950
  }
13650
13951
  },
13651
- name: `tool: ${toolName}`,
13652
- spanAttributes: {
13653
- type: "tool" /* TOOL */
13654
- }
13655
- }
13952
+ INSTRUMENTATION_NAMES.ANTHROPIC
13953
+ )
13656
13954
  );
13657
13955
  },
13658
13956
  writable: runDescriptor?.writable ?? true
@@ -14247,6 +14545,267 @@ function currentWorkflowAgentWrapperSpan() {
14247
14545
  return void 0;
14248
14546
  }
14249
14547
 
14548
+ // src/wrappers/ai-sdk/harness-agent-context.ts
14549
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
14550
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
14551
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
14552
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
14553
+ var harnessTurnParentStore;
14554
+ function serializedTurnContext(value) {
14555
+ if (!isObject(value)) {
14556
+ return void 0;
14557
+ }
14558
+ const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
14559
+ if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
14560
+ return void 0;
14561
+ }
14562
+ const expectedSignature = signTurnContext({
14563
+ parent: context.parent,
14564
+ sessionId: context.sessionId
14565
+ });
14566
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
14567
+ return void 0;
14568
+ }
14569
+ const parent = SpanComponentsV4.fromStr(context.parent);
14570
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
14571
+ return void 0;
14572
+ }
14573
+ return {
14574
+ parent: context.parent,
14575
+ sessionId: context.sessionId
14576
+ };
14577
+ }
14578
+ function signTurnContext(context) {
14579
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
14580
+ return secret ? isomorph_default.hmacSha256?.(
14581
+ secret,
14582
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
14583
+ ) : void 0;
14584
+ }
14585
+ function continuationParentFromCreateSessionParams(params) {
14586
+ if (!isObject(params)) {
14587
+ return void 0;
14588
+ }
14589
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
14590
+ const context = serializedTurnContext(continuation);
14591
+ if (!context) {
14592
+ return void 0;
14593
+ }
14594
+ if (params.sessionId !== context.sessionId) {
14595
+ return void 0;
14596
+ }
14597
+ return context.parent;
14598
+ }
14599
+ function exportSpanSynchronously(span) {
14600
+ const parentInfo = span.getParentInfo();
14601
+ if (!parentInfo) {
14602
+ return void 0;
14603
+ }
14604
+ const objectId = parentInfo.objectId.getSync().value;
14605
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
14606
+ return void 0;
14607
+ }
14608
+ return new SpanComponentsV4({
14609
+ object_type: parentInfo.objectType,
14610
+ ...objectId ? { object_id: objectId } : {
14611
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
14612
+ },
14613
+ row_id: span.id,
14614
+ root_span_id: span.rootSpanId,
14615
+ span_id: span.spanId
14616
+ }).toStr();
14617
+ }
14618
+ function exportedParent(parent) {
14619
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
14620
+ }
14621
+ function addSerializedContext(args) {
14622
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
14623
+ return;
14624
+ }
14625
+ const parent = exportedParent(args.parent);
14626
+ if (!parent) {
14627
+ return;
14628
+ }
14629
+ const context = {
14630
+ parent,
14631
+ sessionId: args.sessionId
14632
+ };
14633
+ const signature = signTurnContext(context);
14634
+ if (!signature) {
14635
+ return;
14636
+ }
14637
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
14638
+ configurable: true,
14639
+ enumerable: true,
14640
+ value: {
14641
+ ...context,
14642
+ signature
14643
+ },
14644
+ writable: true
14645
+ });
14646
+ }
14647
+ function lifecycleMethodDescriptor(session, method) {
14648
+ let owner = session;
14649
+ while (owner) {
14650
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
14651
+ if (descriptor) {
14652
+ return { descriptor, owner };
14653
+ }
14654
+ owner = Object.getPrototypeOf(owner);
14655
+ }
14656
+ return void 0;
14657
+ }
14658
+ function patchLifecycleMethod(session, method) {
14659
+ let resolvedDescriptor;
14660
+ try {
14661
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
14662
+ } catch {
14663
+ return;
14664
+ }
14665
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
14666
+ return;
14667
+ }
14668
+ const { descriptor, owner } = resolvedDescriptor;
14669
+ const patched = patchedLifecycleMethods.get(owner);
14670
+ if (patched?.has(method)) {
14671
+ return;
14672
+ }
14673
+ const original = descriptor.value;
14674
+ const replacement = function(...args) {
14675
+ const result = Reflect.apply(original, this, args);
14676
+ const addContext = (state) => {
14677
+ try {
14678
+ const parent = sessionTurnParents.get(this);
14679
+ if (!parent) {
14680
+ return;
14681
+ }
14682
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
14683
+ addSerializedContext({
14684
+ continuation,
14685
+ parent,
14686
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
14687
+ });
14688
+ } catch {
14689
+ }
14690
+ };
14691
+ try {
14692
+ if (isObject(result) && typeof result.then === "function") {
14693
+ void Promise.resolve(result).then(addContext, () => {
14694
+ });
14695
+ } else {
14696
+ addContext(result);
14697
+ }
14698
+ } catch {
14699
+ }
14700
+ return result;
14701
+ };
14702
+ try {
14703
+ Object.defineProperty(owner, method, {
14704
+ ...descriptor,
14705
+ value: replacement
14706
+ });
14707
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
14708
+ ownerMethods.add(method);
14709
+ if (!patched) {
14710
+ patchedLifecycleMethods.set(owner, ownerMethods);
14711
+ }
14712
+ } catch {
14713
+ }
14714
+ }
14715
+ function patchLifecycleMethods(session) {
14716
+ patchLifecycleMethod(session, "suspendTurn");
14717
+ patchLifecycleMethod(session, "detach");
14718
+ patchLifecycleMethod(session, "stop");
14719
+ }
14720
+ function registerHarnessTurnSpan(args) {
14721
+ if (!isObject(args.session)) {
14722
+ return;
14723
+ }
14724
+ const session = args.session;
14725
+ sessionTurnParents.set(session, args.span);
14726
+ wrapperTurnParents.set(args.span, args.span);
14727
+ patchLifecycleMethods(session);
14728
+ }
14729
+ function harnessContinuationParent(session) {
14730
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
14731
+ }
14732
+ function captureHarnessCreateSessionParent(params) {
14733
+ try {
14734
+ return continuationParentFromCreateSessionParams(params);
14735
+ } catch {
14736
+ return void 0;
14737
+ }
14738
+ }
14739
+ function registerHarnessSessionParent(session, parent) {
14740
+ if (!parent || !isObject(session)) {
14741
+ return;
14742
+ }
14743
+ const harnessSession = session;
14744
+ sessionTurnParents.set(harnessSession, parent);
14745
+ patchLifecycleMethods(harnessSession);
14746
+ }
14747
+ function currentHarnessTurnParent() {
14748
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
14749
+ }
14750
+ function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
14751
+ const startChannel = tracingChannel2.start;
14752
+ if (!startChannel) {
14753
+ return () => {
14754
+ };
14755
+ }
14756
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
14757
+ const store = harnessTurnParentStore;
14758
+ startChannel.bindStore(
14759
+ store,
14760
+ (event) => parentFromEvent(event) ?? store.getStore()
14761
+ );
14762
+ return () => {
14763
+ startChannel.unbindStore(store);
14764
+ };
14765
+ }
14766
+ function startHarnessTurnChildSpan(parent, args) {
14767
+ const spanArgs = withSpanInstrumentationName(
14768
+ args,
14769
+ INSTRUMENTATION_NAMES.AI_SDK
14770
+ );
14771
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
14772
+ }
14773
+ function updateHarnessTurn(parent, update = {}) {
14774
+ if (!parent) {
14775
+ return;
14776
+ }
14777
+ const log3 = (event) => {
14778
+ if (typeof parent === "string") {
14779
+ updateSpan({ exported: parent, ...event });
14780
+ } else {
14781
+ parent.log(event);
14782
+ }
14783
+ };
14784
+ try {
14785
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
14786
+ log3({ output: null });
14787
+ }
14788
+ log3(update);
14789
+ } catch {
14790
+ }
14791
+ }
14792
+ function endHarnessTurn(parent) {
14793
+ const endTime = getCurrentUnixTimestamp();
14794
+ if (!parent) {
14795
+ return endTime;
14796
+ }
14797
+ try {
14798
+ const event = { metrics: { end: endTime } };
14799
+ if (typeof parent === "string") {
14800
+ updateSpan({ exported: parent, ...event });
14801
+ } else {
14802
+ parent.log(event);
14803
+ }
14804
+ } catch {
14805
+ }
14806
+ return endTime;
14807
+ }
14808
+
14250
14809
  // src/wrappers/ai-sdk/telemetry.ts
14251
14810
  function braintrustAISDKTelemetry() {
14252
14811
  const operations = /* @__PURE__ */ new Map();
@@ -14265,14 +14824,17 @@ function braintrustAISDKTelemetry() {
14265
14824
  console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
14266
14825
  }
14267
14826
  };
14268
- const startChildSpan = (operationKey, name, type, event) => {
14827
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
14269
14828
  const parent = operations.get(operationKey)?.span;
14270
- const spanArgs = {
14271
- name,
14272
- spanAttributes: { type },
14273
- ...event ? { event } : {}
14274
- };
14275
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
14829
+ const spanArgs = withSpanInstrumentationName(
14830
+ {
14831
+ name,
14832
+ spanAttributes: { type },
14833
+ ...event ? { event } : {}
14834
+ },
14835
+ INSTRUMENTATION_NAMES.AI_SDK
14836
+ );
14837
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
14276
14838
  const state = operations.get(operationKey);
14277
14839
  if (state && type === "llm" /* LLM */) {
14278
14840
  state.hadModelChild = true;
@@ -14552,14 +15114,30 @@ function braintrustAISDKTelemetry() {
14552
15114
  const workflowAgent = operationName === "WorkflowAgent.stream";
14553
15115
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
14554
15116
  const ownsSpan = !wrapperSpan;
14555
- const span = ownsSpan ? startSpan({
14556
- name: operationName,
14557
- spanAttributes: { type: "function" /* FUNCTION */ }
14558
- }) : wrapperSpan;
15117
+ const harnessTurnParent = currentHarnessTurnParent();
15118
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
15119
+ harnessTurnParent,
15120
+ withSpanInstrumentationName(
15121
+ {
15122
+ name: operationName,
15123
+ spanAttributes: { type: "function" /* FUNCTION */ }
15124
+ },
15125
+ INSTRUMENTATION_NAMES.AI_SDK
15126
+ )
15127
+ ) : startSpan(
15128
+ withSpanInstrumentationName(
15129
+ {
15130
+ name: operationName,
15131
+ spanAttributes: { type: "function" /* FUNCTION */ }
15132
+ },
15133
+ INSTRUMENTATION_NAMES.AI_SDK
15134
+ )
15135
+ ) : wrapperSpan;
14559
15136
  const operationKey = createOperationKey(event, operationName);
14560
15137
  registerOperation({
14561
15138
  callId: event.callId,
14562
15139
  hadModelChild: false,
15140
+ harnessTurnParent,
14563
15141
  loggedInput: false,
14564
15142
  operationName,
14565
15143
  operationKey,
@@ -14800,7 +15378,8 @@ function braintrustAISDKTelemetry() {
14800
15378
  toolCallId,
14801
15379
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
14802
15380
  }
14803
- }
15381
+ },
15382
+ state?.harnessTurnParent
14804
15383
  );
14805
15384
  toolSpans.set(toolCallId, { operationKey, span });
14806
15385
  });
@@ -15036,74 +15615,102 @@ function finishOutput(result, operationName) {
15036
15615
  }
15037
15616
 
15038
15617
  // src/instrumentation/plugins/ai-sdk-channels.ts
15039
- var aiSDKChannels = defineChannels("ai", {
15040
- generateText: channel({
15041
- channelName: "generateText",
15042
- kind: "async"
15043
- }),
15044
- streamText: channel({
15045
- channelName: "streamText",
15046
- kind: "async"
15047
- }),
15048
- streamTextSync: channel({
15049
- channelName: "streamText.sync",
15050
- kind: "sync-stream"
15051
- }),
15052
- generateObject: channel({
15053
- channelName: "generateObject",
15054
- kind: "async"
15055
- }),
15056
- streamObject: channel({
15057
- channelName: "streamObject",
15058
- kind: "async"
15059
- }),
15060
- streamObjectSync: channel({
15061
- channelName: "streamObject.sync",
15062
- kind: "sync-stream"
15063
- }),
15064
- embed: channel(
15065
- {
15618
+ var aiSDKChannels = defineChannels(
15619
+ "ai",
15620
+ {
15621
+ generateText: channel({
15622
+ channelName: "generateText",
15623
+ kind: "async"
15624
+ }),
15625
+ streamText: channel({
15626
+ channelName: "streamText",
15627
+ kind: "async"
15628
+ }),
15629
+ streamTextSync: channel({
15630
+ channelName: "streamText.sync",
15631
+ kind: "sync-stream"
15632
+ }),
15633
+ generateObject: channel({
15634
+ channelName: "generateObject",
15635
+ kind: "async"
15636
+ }),
15637
+ streamObject: channel({
15638
+ channelName: "streamObject",
15639
+ kind: "async"
15640
+ }),
15641
+ streamObjectSync: channel({
15642
+ channelName: "streamObject.sync",
15643
+ kind: "sync-stream"
15644
+ }),
15645
+ embed: channel({
15066
15646
  channelName: "embed",
15067
15647
  kind: "async"
15068
- }
15069
- ),
15070
- embedMany: channel({
15071
- channelName: "embedMany",
15072
- kind: "async"
15073
- }),
15074
- rerank: channel({
15075
- channelName: "rerank",
15076
- kind: "async"
15077
- }),
15078
- agentGenerate: channel({
15079
- channelName: "Agent.generate",
15080
- kind: "async"
15081
- }),
15082
- agentStream: channel({
15083
- channelName: "Agent.stream",
15084
- kind: "async"
15085
- }),
15086
- agentStreamSync: channel({
15087
- channelName: "Agent.stream.sync",
15088
- kind: "sync-stream"
15089
- }),
15090
- toolLoopAgentGenerate: channel({
15091
- channelName: "ToolLoopAgent.generate",
15092
- kind: "async"
15093
- }),
15094
- toolLoopAgentStream: channel({
15095
- channelName: "ToolLoopAgent.stream",
15096
- kind: "async"
15097
- }),
15098
- workflowAgentStream: channel({
15099
- channelName: "WorkflowAgent.stream",
15100
- kind: "async"
15101
- }),
15102
- v7CreateTelemetryDispatcher: channel({
15103
- channelName: "createTelemetryDispatcher",
15104
- kind: "sync-stream"
15105
- })
15106
- });
15648
+ }),
15649
+ embedMany: channel({
15650
+ channelName: "embedMany",
15651
+ kind: "async"
15652
+ }),
15653
+ rerank: channel({
15654
+ channelName: "rerank",
15655
+ kind: "async"
15656
+ }),
15657
+ agentGenerate: channel({
15658
+ channelName: "Agent.generate",
15659
+ kind: "async"
15660
+ }),
15661
+ agentStream: channel({
15662
+ channelName: "Agent.stream",
15663
+ kind: "async"
15664
+ }),
15665
+ agentStreamSync: channel({
15666
+ channelName: "Agent.stream.sync",
15667
+ kind: "sync-stream"
15668
+ }),
15669
+ toolLoopAgentGenerate: channel({
15670
+ channelName: "ToolLoopAgent.generate",
15671
+ kind: "async"
15672
+ }),
15673
+ toolLoopAgentStream: channel({
15674
+ channelName: "ToolLoopAgent.stream",
15675
+ kind: "async"
15676
+ }),
15677
+ workflowAgentStream: channel({
15678
+ channelName: "WorkflowAgent.stream",
15679
+ kind: "async"
15680
+ }),
15681
+ v7CreateTelemetryDispatcher: channel({
15682
+ channelName: "createTelemetryDispatcher",
15683
+ kind: "sync-stream"
15684
+ })
15685
+ },
15686
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
15687
+ );
15688
+ var harnessAgentChannels = defineChannels(
15689
+ "@ai-sdk/harness",
15690
+ {
15691
+ createSession: channel({
15692
+ channelName: "HarnessAgent.createSession",
15693
+ kind: "async"
15694
+ }),
15695
+ generate: channel({
15696
+ channelName: "HarnessAgent.generate",
15697
+ kind: "async"
15698
+ }),
15699
+ stream: channel({
15700
+ channelName: "HarnessAgent.stream",
15701
+ kind: "async"
15702
+ }),
15703
+ continueGenerate: channel({
15704
+ channelName: "HarnessAgent.continueGenerate",
15705
+ kind: "async"
15706
+ }),
15707
+ continueStream: channel({
15708
+ channelName: "HarnessAgent.continueStream",
15709
+ kind: "async"
15710
+ })
15711
+ },
15712
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
15713
+ );
15107
15714
 
15108
15715
  // src/instrumentation/plugins/ai-sdk-plugin.ts
15109
15716
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -15180,6 +15787,17 @@ var AISDKPlugin = class extends BasePlugin {
15180
15787
  subscribeToAISDK() {
15181
15788
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
15182
15789
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
15790
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
15791
+ this.unsubscribers.push(
15792
+ subscribeToHarnessContinuation(
15793
+ harnessAgentChannels.continueGenerate,
15794
+ denyOutputPaths
15795
+ ),
15796
+ subscribeToHarnessContinuation(
15797
+ harnessAgentChannels.continueStream,
15798
+ denyOutputPaths
15799
+ )
15800
+ );
15183
15801
  this.unsubscribers.push(
15184
15802
  traceStreamingChannel(aiSDKChannels.generateText, {
15185
15803
  name: "generateText",
@@ -15372,6 +15990,90 @@ var AISDKPlugin = class extends BasePlugin {
15372
15990
  })
15373
15991
  })
15374
15992
  );
15993
+ this.unsubscribers.push(
15994
+ traceStreamingChannel(harnessAgentChannels.generate, {
15995
+ name: "HarnessAgent.generate",
15996
+ startSpan: _internalStartSpanWithInitialMerge,
15997
+ type: "task" /* TASK */,
15998
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
15999
+ extractOutput: (result, endEvent) => processAISDKOutput(
16000
+ result,
16001
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16002
+ ),
16003
+ extractMetrics: (result) => extractTokenMetrics(result),
16004
+ aggregateChunks: aggregateAISDKChunks
16005
+ })
16006
+ );
16007
+ this.unsubscribers.push(
16008
+ traceStreamingChannel(harnessAgentChannels.stream, {
16009
+ name: "HarnessAgent.stream",
16010
+ startSpan: _internalStartSpanWithInitialMerge,
16011
+ type: "task" /* TASK */,
16012
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16013
+ extractOutput: (result, endEvent) => processAISDKOutput(
16014
+ result,
16015
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16016
+ ),
16017
+ extractMetrics: (result, startTime) => ({
16018
+ ...extractTokenMetrics(result),
16019
+ ...startTime === void 0 ? {} : {
16020
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16021
+ }
16022
+ }),
16023
+ aggregateChunks: aggregateAISDKChunks,
16024
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16025
+ defaultDenyOutputPaths: denyOutputPaths,
16026
+ endEvent,
16027
+ result,
16028
+ resolvePromiseUsage: true,
16029
+ span,
16030
+ startTime
16031
+ })
16032
+ })
16033
+ );
16034
+ this.unsubscribers.push(
16035
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
16036
+ name: "HarnessAgent.continueGenerate",
16037
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16038
+ startSpan: _internalStartSpanWithInitialMerge,
16039
+ type: "task" /* TASK */,
16040
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16041
+ extractOutput: (result, endEvent) => processAISDKOutput(
16042
+ result,
16043
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16044
+ ),
16045
+ extractMetrics: (result) => extractTokenMetrics(result),
16046
+ aggregateChunks: aggregateAISDKChunks
16047
+ })
16048
+ );
16049
+ this.unsubscribers.push(
16050
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
16051
+ name: "HarnessAgent.continueStream",
16052
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
16053
+ startSpan: _internalStartSpanWithInitialMerge,
16054
+ type: "task" /* TASK */,
16055
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
16056
+ extractOutput: (result, endEvent) => processAISDKOutput(
16057
+ result,
16058
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
16059
+ ),
16060
+ extractMetrics: (result, startTime) => ({
16061
+ ...extractTokenMetrics(result),
16062
+ ...startTime === void 0 ? {} : {
16063
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
16064
+ }
16065
+ }),
16066
+ aggregateChunks: aggregateAISDKChunks,
16067
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
16068
+ defaultDenyOutputPaths: denyOutputPaths,
16069
+ endEvent,
16070
+ result,
16071
+ resolvePromiseUsage: true,
16072
+ span,
16073
+ startTime
16074
+ })
16075
+ })
16076
+ );
15375
16077
  this.unsubscribers.push(
15376
16078
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
15377
16079
  name: "ToolLoopAgent.generate",
@@ -15442,6 +16144,8 @@ var AISDKPlugin = class extends BasePlugin {
15442
16144
  defaultDenyOutputPaths: denyOutputPaths,
15443
16145
  endEvent,
15444
16146
  onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
16147
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
16148
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
15445
16149
  result,
15446
16150
  span,
15447
16151
  startTime
@@ -15450,6 +16154,153 @@ var AISDKPlugin = class extends BasePlugin {
15450
16154
  );
15451
16155
  }
15452
16156
  };
16157
+ function subscribeToHarnessAgentCreateSession() {
16158
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
16159
+ const parents = /* @__PURE__ */ new WeakMap();
16160
+ const handlers = {
16161
+ start: (event) => {
16162
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
16163
+ if (parent) {
16164
+ parents.set(event, parent);
16165
+ }
16166
+ },
16167
+ asyncEnd: (event) => {
16168
+ registerHarnessSessionParent(event.result, parents.get(event));
16169
+ parents.delete(event);
16170
+ },
16171
+ error: (event) => {
16172
+ parents.delete(event);
16173
+ }
16174
+ };
16175
+ channel2.subscribe(handlers);
16176
+ return () => channel2.unsubscribe(handlers);
16177
+ }
16178
+ function harnessContinuationParentFromEvent(event) {
16179
+ try {
16180
+ return harnessContinuationParent(event.arguments?.[0]?.session);
16181
+ } catch {
16182
+ return void 0;
16183
+ }
16184
+ }
16185
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
16186
+ const channel2 = continuationChannel.tracingChannel();
16187
+ const parents = /* @__PURE__ */ new WeakMap();
16188
+ const startTimes = /* @__PURE__ */ new WeakMap();
16189
+ const unbindParentStore = bindHarnessTurnParentToStart(
16190
+ channel2,
16191
+ harnessContinuationParentFromEvent
16192
+ );
16193
+ const handlers = {
16194
+ start: (event) => {
16195
+ const parent = harnessContinuationParentFromEvent(event);
16196
+ if (!parent) {
16197
+ return;
16198
+ }
16199
+ parents.set(event, parent);
16200
+ startTimes.set(event, getCurrentUnixTimestamp());
16201
+ try {
16202
+ const params = event.arguments?.[0];
16203
+ if (params) {
16204
+ prepareAISDKHarnessAgentInput(params, event.self);
16205
+ }
16206
+ } catch (error) {
16207
+ debugLogger.error(
16208
+ "Error preparing Harness continuation telemetry:",
16209
+ error
16210
+ );
16211
+ }
16212
+ },
16213
+ asyncEnd: (event) => {
16214
+ const parent = parents.get(event);
16215
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
16216
+ parents.delete(event);
16217
+ startTimes.delete(event);
16218
+ if (!parent) {
16219
+ return;
16220
+ }
16221
+ const endEvent = event;
16222
+ const span = {
16223
+ end: () => endHarnessTurn(parent),
16224
+ log: (update) => updateHarnessTurn(
16225
+ parent,
16226
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
16227
+ )
16228
+ };
16229
+ try {
16230
+ if (isAsyncIterable(endEvent.result)) {
16231
+ patchStreamIfNeeded(endEvent.result, {
16232
+ onComplete: (chunks) => {
16233
+ try {
16234
+ const { metadata, metrics, output } = aggregateAISDKChunks(
16235
+ chunks,
16236
+ endEvent.result,
16237
+ endEvent
16238
+ );
16239
+ span.log({
16240
+ ...metadata ? { metadata } : {},
16241
+ metrics,
16242
+ output
16243
+ });
16244
+ } catch (error) {
16245
+ debugLogger.error(
16246
+ "Error aggregating Harness continuation stream:",
16247
+ error
16248
+ );
16249
+ } finally {
16250
+ span.end();
16251
+ }
16252
+ },
16253
+ onError: (error) => {
16254
+ span.log({ error: toLoggedError(error), output: null });
16255
+ span.end();
16256
+ }
16257
+ });
16258
+ return;
16259
+ }
16260
+ if (patchAISDKStreamingResult({
16261
+ defaultDenyOutputPaths,
16262
+ endEvent,
16263
+ result: endEvent.result,
16264
+ resolvePromiseUsage: true,
16265
+ span,
16266
+ startTime
16267
+ })) {
16268
+ return;
16269
+ }
16270
+ finalizeAISDKChildTracing(endEvent);
16271
+ span.log({
16272
+ metrics: extractTokenMetrics(endEvent.result),
16273
+ output: processAISDKOutput(
16274
+ endEvent.result,
16275
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16276
+ )
16277
+ });
16278
+ span.end();
16279
+ } catch (error) {
16280
+ debugLogger.error("Error tracing Harness continuation:", error);
16281
+ span.end();
16282
+ }
16283
+ },
16284
+ error: (event) => {
16285
+ const parent = parents.get(event);
16286
+ parents.delete(event);
16287
+ startTimes.delete(event);
16288
+ if (!parent) {
16289
+ return;
16290
+ }
16291
+ updateHarnessTurn(parent, {
16292
+ error: toLoggedError(event.error),
16293
+ output: null
16294
+ });
16295
+ endHarnessTurn(parent);
16296
+ }
16297
+ };
16298
+ channel2.subscribe(handlers);
16299
+ return () => {
16300
+ unbindParentStore();
16301
+ channel2.unsubscribe(handlers);
16302
+ };
16303
+ }
15453
16304
  function subscribeToAISDKV7TelemetryDispatcher() {
15454
16305
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
15455
16306
  const telemetry = braintrustAISDKTelemetry();
@@ -15902,6 +16753,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
15902
16753
  metadata
15903
16754
  };
15904
16755
  }
16756
+ function prepareAISDKHarnessAgentInput(params, self, span) {
16757
+ if (span) {
16758
+ registerHarnessTurnSpan({
16759
+ session: params.session,
16760
+ span
16761
+ });
16762
+ }
16763
+ if (isObject(self)) {
16764
+ try {
16765
+ if (isObject(self.settings)) {
16766
+ const settings = self.settings;
16767
+ if (settings.telemetry === void 0) {
16768
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
16769
+ }
16770
+ }
16771
+ } catch {
16772
+ }
16773
+ }
16774
+ const selectedInput = {};
16775
+ if (params.prompt !== void 0) {
16776
+ selectedInput.prompt = params.prompt;
16777
+ }
16778
+ if (params.messages !== void 0) {
16779
+ selectedInput.messages = params.messages;
16780
+ }
16781
+ if (params.toolApprovalContinuations !== void 0) {
16782
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
16783
+ }
16784
+ const metadata = extractMetadataFromCallParams({}, self);
16785
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
16786
+ metadata.sessionId = params.session.sessionId;
16787
+ }
16788
+ if (isObject(self)) {
16789
+ if (typeof self.harnessId === "string") {
16790
+ metadata.harnessId = self.harnessId;
16791
+ }
16792
+ if (typeof self.permissionMode === "string") {
16793
+ metadata.permissionMode = self.permissionMode;
16794
+ }
16795
+ }
16796
+ return {
16797
+ input: processAISDKCallInput(selectedInput).input,
16798
+ metadata
16799
+ };
16800
+ }
16801
+ function harnessSessionFromArguments(args) {
16802
+ const params = args[0];
16803
+ return isObject(params) ? params.session : void 0;
16804
+ }
15905
16805
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
15906
16806
  registerWorkflowAgentWrapperSpan(span);
15907
16807
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -16349,19 +17249,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16349
17249
  activeEntry.openSpans.delete(span);
16350
17250
  }
16351
17251
  },
16352
- {
16353
- name: "doGenerate",
16354
- spanAttributes: {
16355
- type: "llm" /* LLM */
17252
+ withSpanInstrumentationName(
17253
+ {
17254
+ name: "doGenerate",
17255
+ spanAttributes: {
17256
+ type: "llm" /* LLM */
17257
+ },
17258
+ event: buildAISDKModelStartEvent(
17259
+ callOptions,
17260
+ activeEntry.baseMetadata,
17261
+ {
17262
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17263
+ }
17264
+ )
16356
17265
  },
16357
- event: buildAISDKModelStartEvent(
16358
- callOptions,
16359
- activeEntry.baseMetadata,
16360
- {
16361
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16362
- }
16363
- )
16364
- }
17266
+ INSTRUMENTATION_NAMES.AI_SDK
17267
+ )
16365
17268
  );
16366
17269
  };
16367
17270
  if (originalDoStream) {
@@ -16371,19 +17274,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16371
17274
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
16372
17275
  }
16373
17276
  closeOpenAISDKModelPatchSpans(activeEntry);
16374
- const span = activeEntry.parentSpan.startSpan({
16375
- name: "doStream",
16376
- spanAttributes: {
16377
- type: "llm" /* LLM */
16378
- },
16379
- event: buildAISDKModelStartEvent(
16380
- callOptions,
16381
- activeEntry.baseMetadata,
17277
+ const span = activeEntry.parentSpan.startSpan(
17278
+ withSpanInstrumentationName(
16382
17279
  {
16383
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
16384
- }
17280
+ name: "doStream",
17281
+ spanAttributes: {
17282
+ type: "llm" /* LLM */
17283
+ },
17284
+ event: buildAISDKModelStartEvent(
17285
+ callOptions,
17286
+ activeEntry.baseMetadata,
17287
+ {
17288
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
17289
+ }
17290
+ )
17291
+ },
17292
+ INSTRUMENTATION_NAMES.AI_SDK
16385
17293
  )
16386
- });
17294
+ );
16387
17295
  activeEntry.openSpans.add(span);
16388
17296
  const streamStartTime = getCurrentUnixTimestamp();
16389
17297
  const result = await withCurrent(
@@ -16555,12 +17463,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16555
17463
  }
16556
17464
  if (isAsyncGenerator(result)) {
16557
17465
  return (async function* () {
16558
- const span = activeEntry.parentSpan.startSpan({
16559
- name: activeEntry.name,
16560
- spanAttributes: {
16561
- type: "tool" /* TOOL */
16562
- }
16563
- });
17466
+ const span = activeEntry.parentSpan.startSpan(
17467
+ withSpanInstrumentationName(
17468
+ {
17469
+ name: activeEntry.name,
17470
+ spanAttributes: {
17471
+ type: "tool" /* TOOL */
17472
+ }
17473
+ },
17474
+ INSTRUMENTATION_NAMES.AI_SDK
17475
+ )
17476
+ );
16564
17477
  span.log({ input: serializeToolExecutionInput(args) });
16565
17478
  try {
16566
17479
  let lastValue;
@@ -16584,12 +17497,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
16584
17497
  span.log({ output: awaitedResult });
16585
17498
  return awaitedResult;
16586
17499
  },
16587
- {
16588
- name: activeEntry.name,
16589
- spanAttributes: {
16590
- type: "tool" /* TOOL */
16591
- }
16592
- }
17500
+ withSpanInstrumentationName(
17501
+ {
17502
+ name: activeEntry.name,
17503
+ spanAttributes: {
17504
+ type: "tool" /* TOOL */
17505
+ }
17506
+ },
17507
+ INSTRUMENTATION_NAMES.AI_SDK
17508
+ )
16593
17509
  );
16594
17510
  };
16595
17511
  cleanup.push(() => {
@@ -16748,7 +17664,10 @@ function patchAISDKStreamingResult(args) {
16748
17664
  defaultDenyOutputPaths,
16749
17665
  endEvent,
16750
17666
  onComplete,
17667
+ onCancel,
17668
+ onError,
16751
17669
  result,
17670
+ resolvePromiseUsage,
16752
17671
  span,
16753
17672
  startTime
16754
17673
  } = args;
@@ -16758,14 +17677,43 @@ function patchAISDKStreamingResult(args) {
16758
17677
  const resultRecord = result;
16759
17678
  attachKnownResultPromiseHandlers(resultRecord);
16760
17679
  let finalized = false;
16761
- const finalize = () => {
17680
+ const finalize = (outcome) => {
16762
17681
  if (finalized) {
16763
17682
  return;
16764
17683
  }
16765
17684
  finalized = true;
16766
- finalizeAISDKChildTracing(endEvent);
16767
- span.end();
16768
- onComplete?.();
17685
+ try {
17686
+ finalizeAISDKChildTracing(endEvent);
17687
+ } catch (error) {
17688
+ debugLogger.error("Error finalizing AI SDK child tracing:", error);
17689
+ }
17690
+ try {
17691
+ span.end();
17692
+ } catch (error) {
17693
+ debugLogger.error("Error ending AI SDK streaming span:", error);
17694
+ }
17695
+ if (outcome && "error" in outcome) {
17696
+ try {
17697
+ onError?.(outcome.error);
17698
+ } catch (error) {
17699
+ debugLogger.error("Error in AI SDK streaming error hook:", error);
17700
+ }
17701
+ } else if (outcome) {
17702
+ try {
17703
+ onComplete?.(outcome);
17704
+ } catch (error) {
17705
+ debugLogger.error("Error in AI SDK streaming completion hook:", error);
17706
+ }
17707
+ } else {
17708
+ try {
17709
+ onCancel?.();
17710
+ } catch (error) {
17711
+ debugLogger.error(
17712
+ "Error in AI SDK streaming cancellation hook:",
17713
+ error
17714
+ );
17715
+ }
17716
+ }
16769
17717
  };
16770
17718
  let outputLogged = false;
16771
17719
  const logStreamingOutput = async (firstChunkTime) => {
@@ -16775,6 +17723,22 @@ function patchAISDKStreamingResult(args) {
16775
17723
  outputLogged = true;
16776
17724
  try {
16777
17725
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
17726
+ if (resolvePromiseUsage) {
17727
+ try {
17728
+ const resultRecord2 = result;
17729
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
17730
+ if (isPromiseLike(pendingUsage)) {
17731
+ const usage = await Promise.resolve(pendingUsage);
17732
+ if (isObject(usage)) {
17733
+ Object.assign(
17734
+ metrics,
17735
+ extractTokenMetrics({ usage })
17736
+ );
17737
+ }
17738
+ }
17739
+ } catch {
17740
+ }
17741
+ }
16778
17742
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
16779
17743
  metrics.time_to_first_token = firstChunkTime - startTime;
16780
17744
  }
@@ -16783,15 +17747,29 @@ function patchAISDKStreamingResult(args) {
16783
17747
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
16784
17748
  );
16785
17749
  const metadata = buildResolvedMetadataPayload(result).metadata;
16786
- span.log({
16787
- output,
16788
- ...metadata ? { metadata } : {},
16789
- metrics
16790
- });
17750
+ try {
17751
+ span.log({
17752
+ output,
17753
+ ...metadata ? { metadata } : {},
17754
+ metrics
17755
+ });
17756
+ } catch (error) {
17757
+ debugLogger.error("Error logging AI SDK streaming output:", error);
17758
+ }
17759
+ finalize({ metrics, output });
16791
17760
  } catch (error) {
16792
- span.log({ error: toLoggedError(error) });
16793
- } finally {
16794
- finalize();
17761
+ const loggedError = toLoggedError(error);
17762
+ try {
17763
+ span.log({ error: loggedError });
17764
+ } catch (loggingError) {
17765
+ debugLogger.error(
17766
+ "Error logging AI SDK streaming failure:",
17767
+ loggingError
17768
+ );
17769
+ }
17770
+ finalize({
17771
+ error: error instanceof Error ? error : new Error(String(loggedError))
17772
+ });
16795
17773
  }
16796
17774
  };
16797
17775
  const createAsyncIterableHooks = () => {
@@ -16808,11 +17786,16 @@ function patchAISDKStreamingResult(args) {
16808
17786
  onError: (error) => {
16809
17787
  if (!outputLogged) {
16810
17788
  outputLogged = true;
16811
- span.log({
16812
- error: error.message
16813
- });
17789
+ try {
17790
+ span.log({ error });
17791
+ } catch (loggingError) {
17792
+ debugLogger.error(
17793
+ "Error logging AI SDK stream failure:",
17794
+ loggingError
17795
+ );
17796
+ }
16814
17797
  }
16815
- finalize();
17798
+ finalize({ error });
16816
17799
  },
16817
17800
  onCancel: finalize
16818
17801
  };
@@ -16898,8 +17881,18 @@ function patchAISDKStreamingResult(args) {
16898
17881
  }
16899
17882
  controller.enqueue(value);
16900
17883
  } catch (error) {
16901
- span.log({ error: toLoggedError(error) });
16902
- finalize();
17884
+ const loggedError = toLoggedError(error);
17885
+ try {
17886
+ span.log({ error: loggedError });
17887
+ } catch (loggingError) {
17888
+ debugLogger.error(
17889
+ "Error logging AI SDK base stream failure:",
17890
+ loggingError
17891
+ );
17892
+ }
17893
+ finalize({
17894
+ error: error instanceof Error ? error : new Error(String(loggedError))
17895
+ });
16903
17896
  controller.error(error);
16904
17897
  }
16905
17898
  },
@@ -17688,7 +18681,8 @@ var claudeAgentSDKChannels = defineChannels(
17688
18681
  channelName: "query",
17689
18682
  kind: "sync-stream"
17690
18683
  })
17691
- }
18684
+ },
18685
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
17692
18686
  );
17693
18687
 
17694
18688
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -17836,22 +18830,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
17836
18830
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
17837
18831
  const runWithResolvedParent = async () => {
17838
18832
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
17839
- const span = startSpan({
17840
- event: {
17841
- input: handlerArgs[0],
17842
- metadata: {
17843
- "claude_agent_sdk.raw_tool_name": rawToolName,
17844
- "gen_ai.tool.name": metadata.toolName,
17845
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
17846
- ...metadata.serverName && {
17847
- "mcp.server": metadata.serverName
17848
- }
17849
- }
17850
- },
17851
- name: spanName,
17852
- ...parent && { parent },
17853
- spanAttributes: { type: "tool" /* TOOL */ }
17854
- });
18833
+ const span = startSpan(
18834
+ withSpanInstrumentationName(
18835
+ {
18836
+ event: {
18837
+ input: handlerArgs[0],
18838
+ metadata: {
18839
+ "claude_agent_sdk.raw_tool_name": rawToolName,
18840
+ "gen_ai.tool.name": metadata.toolName,
18841
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
18842
+ ...metadata.serverName && {
18843
+ "mcp.server": metadata.serverName
18844
+ }
18845
+ }
18846
+ },
18847
+ name: spanName,
18848
+ ...parent && { parent },
18849
+ spanAttributes: { type: "tool" /* TOOL */ }
18850
+ },
18851
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
18852
+ )
18853
+ );
17855
18854
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
17856
18855
  const finalizeSuccess = (result) => {
17857
18856
  span.log({ output: result });
@@ -18176,14 +19175,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
18176
19175
  ).filter(
18177
19176
  (c) => c !== void 0
18178
19177
  );
18179
- const span = existingSpan ?? startSpan({
18180
- name: "anthropic.messages.create",
18181
- parent: parentSpan,
18182
- spanAttributes: {
18183
- type: "llm" /* LLM */
18184
- },
18185
- startTime
18186
- });
19178
+ const span = existingSpan ?? startSpan(
19179
+ withSpanInstrumentationName(
19180
+ {
19181
+ name: "anthropic.messages.create",
19182
+ parent: parentSpan,
19183
+ spanAttributes: {
19184
+ type: "llm" /* LLM */
19185
+ },
19186
+ startTime
19187
+ },
19188
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19189
+ )
19190
+ );
18187
19191
  span.log({
18188
19192
  input,
18189
19193
  metadata: model ? { model } : void 0,
@@ -18286,25 +19290,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
18286
19290
  return {};
18287
19291
  }
18288
19292
  const parsed = parseToolName(input.tool_name);
18289
- const toolSpan = startSpan({
18290
- event: {
18291
- input: input.tool_input,
18292
- metadata: {
18293
- "claude_agent_sdk.cwd": input.cwd,
18294
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
18295
- "claude_agent_sdk.session_id": input.session_id,
18296
- "gen_ai.tool.call.id": toolUseID,
18297
- "gen_ai.tool.name": parsed.toolName,
18298
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
18299
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
18300
- }
18301
- },
18302
- name: parsed.displayName,
18303
- parent: await resolveParentSpan(toolUseID, {
18304
- agentId: input.agent_id
18305
- }),
18306
- spanAttributes: { type: "tool" /* TOOL */ }
18307
- });
19293
+ const toolSpan = startSpan(
19294
+ withSpanInstrumentationName(
19295
+ {
19296
+ event: {
19297
+ input: input.tool_input,
19298
+ metadata: {
19299
+ "claude_agent_sdk.cwd": input.cwd,
19300
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
19301
+ "claude_agent_sdk.session_id": input.session_id,
19302
+ "gen_ai.tool.call.id": toolUseID,
19303
+ "gen_ai.tool.name": parsed.toolName,
19304
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
19305
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
19306
+ }
19307
+ },
19308
+ name: parsed.displayName,
19309
+ parent: await resolveParentSpan(toolUseID, {
19310
+ agentId: input.agent_id
19311
+ }),
19312
+ spanAttributes: { type: "tool" /* TOOL */ }
19313
+ },
19314
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19315
+ )
19316
+ );
18308
19317
  activeToolSpans.set(toolUseID, toolSpan);
18309
19318
  return {};
18310
19319
  };
@@ -18670,14 +19679,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
18670
19679
  const spanName = formatSubAgentSpanName(details);
18671
19680
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
18672
19681
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
18673
- const subAgentSpan = startSpan({
18674
- event: {
18675
- metadata: subAgentDetailsToMetadata(details)
18676
- },
18677
- name: spanName,
18678
- parent: parentSpan,
18679
- spanAttributes: { type: "task" /* TASK */ }
18680
- });
19682
+ const subAgentSpan = startSpan(
19683
+ withSpanInstrumentationName(
19684
+ {
19685
+ event: {
19686
+ metadata: subAgentDetailsToMetadata(details)
19687
+ },
19688
+ name: spanName,
19689
+ parent: parentSpan,
19690
+ spanAttributes: { type: "task" /* TASK */ }
19691
+ },
19692
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19693
+ )
19694
+ );
18681
19695
  subAgentSpans.set(parentToolUseId, subAgentSpan);
18682
19696
  return subAgentSpan;
18683
19697
  }
@@ -18698,14 +19712,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
18698
19712
  );
18699
19713
  llmParentSpan = await subAgentSpan.export();
18700
19714
  }
18701
- const llmSpan = startSpan({
18702
- name: "anthropic.messages.create",
18703
- parent: llmParentSpan,
18704
- spanAttributes: {
18705
- type: "llm" /* LLM */
18706
- },
18707
- startTime
18708
- });
19715
+ const llmSpan = startSpan(
19716
+ withSpanInstrumentationName(
19717
+ {
19718
+ name: "anthropic.messages.create",
19719
+ parent: llmParentSpan,
19720
+ spanAttributes: {
19721
+ type: "llm" /* LLM */
19722
+ },
19723
+ startTime
19724
+ },
19725
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
19726
+ )
19727
+ );
18709
19728
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
18710
19729
  return llmSpan;
18711
19730
  }
@@ -18974,12 +19993,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
18974
19993
  }
18975
19994
  })();
18976
19995
  }
18977
- const span = startSpan({
18978
- name: "Claude Agent",
18979
- spanAttributes: {
18980
- type: "task" /* TASK */
18981
- }
18982
- });
19996
+ const span = startSpan(
19997
+ withSpanInstrumentationName(
19998
+ {
19999
+ name: "Claude Agent",
20000
+ spanAttributes: {
20001
+ type: "task" /* TASK */
20002
+ }
20003
+ },
20004
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
20005
+ )
20006
+ );
18983
20007
  const startTime = getCurrentUnixTimestamp();
18984
20008
  try {
18985
20009
  span.log({
@@ -19144,24 +20168,28 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
19144
20168
  };
19145
20169
 
19146
20170
  // src/instrumentation/plugins/cursor-sdk-channels.ts
19147
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
19148
- create: channel({
19149
- channelName: "Agent.create",
19150
- kind: "async"
19151
- }),
19152
- resume: channel({
19153
- channelName: "Agent.resume",
19154
- kind: "async"
19155
- }),
19156
- prompt: channel({
19157
- channelName: "Agent.prompt",
19158
- kind: "async"
19159
- }),
19160
- send: channel({
19161
- channelName: "agent.send",
19162
- kind: "async"
19163
- })
19164
- });
20171
+ var cursorSDKChannels = defineChannels(
20172
+ "@cursor/sdk",
20173
+ {
20174
+ create: channel({
20175
+ channelName: "Agent.create",
20176
+ kind: "async"
20177
+ }),
20178
+ resume: channel({
20179
+ channelName: "Agent.resume",
20180
+ kind: "async"
20181
+ }),
20182
+ prompt: channel({
20183
+ channelName: "Agent.prompt",
20184
+ kind: "async"
20185
+ }),
20186
+ send: channel({
20187
+ channelName: "agent.send",
20188
+ kind: "async"
20189
+ })
20190
+ },
20191
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
20192
+ );
19165
20193
 
19166
20194
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
19167
20195
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -19213,10 +20241,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19213
20241
  provider: "cursor",
19214
20242
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19215
20243
  };
19216
- const span = startSpan({
19217
- name: "Cursor Agent",
19218
- spanAttributes: { type: "task" /* TASK */ }
19219
- });
20244
+ const span = startSpan(
20245
+ withSpanInstrumentationName(
20246
+ {
20247
+ name: "Cursor Agent",
20248
+ spanAttributes: { type: "task" /* TASK */ }
20249
+ },
20250
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20251
+ )
20252
+ );
19220
20253
  const startTime = getCurrentUnixTimestamp();
19221
20254
  safeLog2(span, {
19222
20255
  input: sanitizeUserMessage(message),
@@ -19278,10 +20311,15 @@ var CursorSDKPlugin = class extends BasePlugin {
19278
20311
  provider: "cursor",
19279
20312
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
19280
20313
  };
19281
- const span = startSpan({
19282
- name: "Cursor Agent",
19283
- spanAttributes: { type: "task" /* TASK */ }
19284
- });
20314
+ const span = startSpan(
20315
+ withSpanInstrumentationName(
20316
+ {
20317
+ name: "Cursor Agent",
20318
+ spanAttributes: { type: "task" /* TASK */ }
20319
+ },
20320
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20321
+ )
20322
+ );
19285
20323
  const startTime = getCurrentUnixTimestamp();
19286
20324
  safeLog2(span, {
19287
20325
  input: sanitizeUserMessage(message),
@@ -19728,29 +20766,39 @@ async function startToolSpan(state, args) {
19728
20766
  if (args.truncated?.result !== void 0) {
19729
20767
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
19730
20768
  }
19731
- const span = startSpan({
19732
- event: {
19733
- input: args.args,
19734
- metadata
19735
- },
19736
- name: `tool: ${name}`,
19737
- parent: await state.span.export(),
19738
- spanAttributes: { type: "tool" /* TOOL */ }
19739
- });
20769
+ const span = startSpan(
20770
+ withSpanInstrumentationName(
20771
+ {
20772
+ event: {
20773
+ input: args.args,
20774
+ metadata
20775
+ },
20776
+ name: `tool: ${name}`,
20777
+ parent: await state.span.export(),
20778
+ spanAttributes: { type: "tool" /* TOOL */ }
20779
+ },
20780
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20781
+ )
20782
+ );
19740
20783
  let subAgentSpan;
19741
20784
  if (isSubAgentToolName(name)) {
19742
- subAgentSpan = startSpan({
19743
- event: {
19744
- input: args.args,
19745
- metadata: {
19746
- "cursor_sdk.subagent.tool_call_id": args.callId,
19747
- "gen_ai.tool.name": name
19748
- }
19749
- },
19750
- name: formatSubAgentSpanName2(args.toolCall, args.args),
19751
- parent: await span.export(),
19752
- spanAttributes: { type: "task" /* TASK */ }
19753
- });
20785
+ subAgentSpan = startSpan(
20786
+ withSpanInstrumentationName(
20787
+ {
20788
+ event: {
20789
+ input: args.args,
20790
+ metadata: {
20791
+ "cursor_sdk.subagent.tool_call_id": args.callId,
20792
+ "gen_ai.tool.name": name
20793
+ }
20794
+ },
20795
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
20796
+ parent: await span.export(),
20797
+ spanAttributes: { type: "task" /* TASK */ }
20798
+ },
20799
+ INSTRUMENTATION_NAMES.CURSOR_SDK
20800
+ )
20801
+ );
19754
20802
  }
19755
20803
  return { span, subAgentSpan };
19756
20804
  }
@@ -20009,24 +21057,28 @@ function cleanMetrics2(metrics) {
20009
21057
  }
20010
21058
 
20011
21059
  // src/instrumentation/plugins/openai-agents-channels.ts
20012
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
20013
- onTraceStart: channel({
20014
- channelName: "tracing.processor.onTraceStart",
20015
- kind: "async"
20016
- }),
20017
- onTraceEnd: channel({
20018
- channelName: "tracing.processor.onTraceEnd",
20019
- kind: "async"
20020
- }),
20021
- onSpanStart: channel({
20022
- channelName: "tracing.processor.onSpanStart",
20023
- kind: "async"
20024
- }),
20025
- onSpanEnd: channel({
20026
- channelName: "tracing.processor.onSpanEnd",
20027
- kind: "async"
20028
- })
20029
- });
21060
+ var openAIAgentsCoreChannels = defineChannels(
21061
+ "@openai/agents-core",
21062
+ {
21063
+ onTraceStart: channel({
21064
+ channelName: "tracing.processor.onTraceStart",
21065
+ kind: "async"
21066
+ }),
21067
+ onTraceEnd: channel({
21068
+ channelName: "tracing.processor.onTraceEnd",
21069
+ kind: "async"
21070
+ }),
21071
+ onSpanStart: channel({
21072
+ channelName: "tracing.processor.onSpanStart",
21073
+ kind: "async"
21074
+ }),
21075
+ onSpanEnd: channel({
21076
+ channelName: "tracing.processor.onSpanEnd",
21077
+ kind: "async"
21078
+ })
21079
+ },
21080
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
21081
+ );
20030
21082
 
20031
21083
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
20032
21084
  function isSpanData(spanData, type) {
@@ -20142,16 +21194,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20142
21194
  this.evictOldestTrace();
20143
21195
  }
20144
21196
  const current = currentSpan();
20145
- const span = current && current !== NOOP_SPAN ? current.startSpan({
20146
- name: trace.name,
20147
- type: "task" /* TASK */
20148
- }) : this.logger ? this.logger.startSpan({
20149
- name: trace.name,
20150
- type: "task" /* TASK */
20151
- }) : startSpan({
20152
- name: trace.name,
20153
- type: "task" /* TASK */
20154
- });
21197
+ const spanArgs = withSpanInstrumentationName(
21198
+ {
21199
+ name: trace.name,
21200
+ type: "task" /* TASK */
21201
+ },
21202
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21203
+ );
21204
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
21205
+ withSpanInstrumentationName(
21206
+ spanArgs,
21207
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21208
+ )
21209
+ );
20155
21210
  span.log({
20156
21211
  input: "Agent workflow started",
20157
21212
  metadata: {
@@ -20202,10 +21257,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
20202
21257
  if (!parentSpan) {
20203
21258
  return Promise.resolve();
20204
21259
  }
20205
- const childSpan = parentSpan.startSpan({
20206
- name: spanNameFromAgents(span),
20207
- type: spanTypeFromAgents(span)
20208
- });
21260
+ const childSpan = parentSpan.startSpan(
21261
+ withSpanInstrumentationName(
21262
+ {
21263
+ name: spanNameFromAgents(span),
21264
+ type: spanTypeFromAgents(span)
21265
+ },
21266
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
21267
+ )
21268
+ );
20209
21269
  traceData.childSpans.set(span.spanId, childSpan);
20210
21270
  return Promise.resolve();
20211
21271
  }
@@ -20470,24 +21530,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
20470
21530
  };
20471
21531
 
20472
21532
  // src/instrumentation/plugins/google-genai-channels.ts
20473
- var googleGenAIChannels = defineChannels("@google/genai", {
20474
- generateContent: channel({
20475
- channelName: "models.generateContent",
20476
- kind: "async"
20477
- }),
20478
- generateContentStream: channel({
20479
- channelName: "models.generateContentStream",
20480
- kind: "async"
20481
- }),
20482
- embedContent: channel({
20483
- channelName: "models.embedContent",
20484
- kind: "async"
20485
- }),
20486
- interactionsCreate: channel({
20487
- channelName: "interactions.create",
20488
- kind: "async"
20489
- })
20490
- });
21533
+ var googleGenAIChannels = defineChannels(
21534
+ "@google/genai",
21535
+ {
21536
+ generateContent: channel({
21537
+ channelName: "models.generateContent",
21538
+ kind: "async"
21539
+ }),
21540
+ generateContentStream: channel({
21541
+ channelName: "models.generateContentStream",
21542
+ kind: "async"
21543
+ }),
21544
+ embedContent: channel({
21545
+ channelName: "models.embedContent",
21546
+ kind: "async"
21547
+ }),
21548
+ interactionsCreate: channel({
21549
+ channelName: "interactions.create",
21550
+ kind: "async"
21551
+ })
21552
+ },
21553
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
21554
+ );
20491
21555
 
20492
21556
  // src/instrumentation/plugins/google-genai-plugin.ts
20493
21557
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -20525,13 +21589,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20525
21589
  const params = event.arguments[0];
20526
21590
  const input = serializeGenerateContentInput(params);
20527
21591
  const metadata = extractGenerateContentMetadata(params);
20528
- const span = startSpan({
20529
- name: "generate_content",
20530
- spanAttributes: {
20531
- type: "llm" /* LLM */
20532
- },
20533
- event: createWrapperParityEvent({ input, metadata })
20534
- });
21592
+ const span = startSpan(
21593
+ withSpanInstrumentationName(
21594
+ {
21595
+ name: "generate_content",
21596
+ spanAttributes: {
21597
+ type: "llm" /* LLM */
21598
+ },
21599
+ event: createWrapperParityEvent({ input, metadata })
21600
+ },
21601
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21602
+ )
21603
+ );
20535
21604
  return {
20536
21605
  span,
20537
21606
  startTime: getCurrentUnixTimestamp()
@@ -20544,13 +21613,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20544
21613
  const params = event.arguments[0];
20545
21614
  const input = serializeGenerateContentInput(params);
20546
21615
  const metadata = extractGenerateContentMetadata(params);
20547
- const span = startSpan({
20548
- name: "generate_content",
20549
- spanAttributes: {
20550
- type: "llm" /* LLM */
20551
- },
20552
- event: createWrapperParityEvent({ input, metadata })
20553
- });
21616
+ const span = startSpan(
21617
+ withSpanInstrumentationName(
21618
+ {
21619
+ name: "generate_content",
21620
+ spanAttributes: {
21621
+ type: "llm" /* LLM */
21622
+ },
21623
+ event: createWrapperParityEvent({ input, metadata })
21624
+ },
21625
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21626
+ )
21627
+ );
20554
21628
  return {
20555
21629
  span,
20556
21630
  startTime: getCurrentUnixTimestamp()
@@ -20626,13 +21700,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20626
21700
  const params = event.arguments[0];
20627
21701
  const input = serializeEmbedContentInput(params);
20628
21702
  const metadata = extractEmbedContentMetadata(params);
20629
- const span = startSpan({
20630
- name: "embed_content",
20631
- spanAttributes: {
20632
- type: "llm" /* LLM */
20633
- },
20634
- event: createWrapperParityEvent({ input, metadata })
20635
- });
21703
+ const span = startSpan(
21704
+ withSpanInstrumentationName(
21705
+ {
21706
+ name: "embed_content",
21707
+ spanAttributes: {
21708
+ type: "llm" /* LLM */
21709
+ },
21710
+ event: createWrapperParityEvent({ input, metadata })
21711
+ },
21712
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21713
+ )
21714
+ );
20636
21715
  return {
20637
21716
  span,
20638
21717
  startTime: getCurrentUnixTimestamp()
@@ -20645,13 +21724,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
20645
21724
  const params = event.arguments[0];
20646
21725
  const input = serializeEmbedContentInput(params);
20647
21726
  const metadata = extractEmbedContentMetadata(params);
20648
- const span = startSpan({
20649
- name: "embed_content",
20650
- spanAttributes: {
20651
- type: "llm" /* LLM */
20652
- },
20653
- event: createWrapperParityEvent({ input, metadata })
20654
- });
21727
+ const span = startSpan(
21728
+ withSpanInstrumentationName(
21729
+ {
21730
+ name: "embed_content",
21731
+ spanAttributes: {
21732
+ type: "llm" /* LLM */
21733
+ },
21734
+ event: createWrapperParityEvent({ input, metadata })
21735
+ },
21736
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21737
+ )
21738
+ );
20655
21739
  return {
20656
21740
  span,
20657
21741
  startTime: getCurrentUnixTimestamp()
@@ -20762,16 +21846,21 @@ function patchGoogleGenAIStreamingResult(args) {
20762
21846
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
20763
21847
  const ensureSpan = () => {
20764
21848
  if (!span) {
20765
- span = startSpan({
20766
- name: "generate_content_stream",
20767
- spanAttributes: {
20768
- type: "llm" /* LLM */
20769
- },
20770
- event: {
20771
- input,
20772
- metadata
20773
- }
20774
- });
21849
+ span = startSpan(
21850
+ withSpanInstrumentationName(
21851
+ {
21852
+ name: "generate_content_stream",
21853
+ spanAttributes: {
21854
+ type: "llm" /* LLM */
21855
+ },
21856
+ event: {
21857
+ input,
21858
+ metadata
21859
+ }
21860
+ },
21861
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
21862
+ )
21863
+ );
20775
21864
  }
20776
21865
  return span;
20777
21866
  };
@@ -21539,28 +22628,32 @@ function tryToDict(obj) {
21539
22628
  }
21540
22629
 
21541
22630
  // src/instrumentation/plugins/huggingface-channels.ts
21542
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
21543
- chatCompletion: channel({
21544
- channelName: "chatCompletion",
21545
- kind: "async"
21546
- }),
21547
- chatCompletionStream: channel({
21548
- channelName: "chatCompletionStream",
21549
- kind: "sync-stream"
21550
- }),
21551
- textGeneration: channel({
21552
- channelName: "textGeneration",
21553
- kind: "async"
21554
- }),
21555
- textGenerationStream: channel({
21556
- channelName: "textGenerationStream",
21557
- kind: "sync-stream"
21558
- }),
21559
- featureExtraction: channel({
21560
- channelName: "featureExtraction",
21561
- kind: "async"
21562
- })
21563
- });
22631
+ var huggingFaceChannels = defineChannels(
22632
+ "@huggingface/inference",
22633
+ {
22634
+ chatCompletion: channel({
22635
+ channelName: "chatCompletion",
22636
+ kind: "async"
22637
+ }),
22638
+ chatCompletionStream: channel({
22639
+ channelName: "chatCompletionStream",
22640
+ kind: "sync-stream"
22641
+ }),
22642
+ textGeneration: channel({
22643
+ channelName: "textGeneration",
22644
+ kind: "async"
22645
+ }),
22646
+ textGenerationStream: channel({
22647
+ channelName: "textGenerationStream",
22648
+ kind: "sync-stream"
22649
+ }),
22650
+ featureExtraction: channel({
22651
+ channelName: "featureExtraction",
22652
+ kind: "async"
22653
+ })
22654
+ },
22655
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
22656
+ );
21564
22657
 
21565
22658
  // src/instrumentation/plugins/huggingface-plugin.ts
21566
22659
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -21987,20 +23080,24 @@ function extractTextGenerationStreamMetadata(chunks) {
21987
23080
  }
21988
23081
 
21989
23082
  // src/instrumentation/plugins/openrouter-agent-channels.ts
21990
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
21991
- callModel: channel({
21992
- channelName: "callModel",
21993
- kind: "sync-stream"
21994
- }),
21995
- callModelTurn: channel({
21996
- channelName: "callModel.turn",
21997
- kind: "async"
21998
- }),
21999
- toolExecute: channel({
22000
- channelName: "tool.execute",
22001
- kind: "async"
22002
- })
22003
- });
23083
+ var openRouterAgentChannels = defineChannels(
23084
+ "@openrouter/agent",
23085
+ {
23086
+ callModel: channel({
23087
+ channelName: "callModel",
23088
+ kind: "sync-stream"
23089
+ }),
23090
+ callModelTurn: channel({
23091
+ channelName: "callModel.turn",
23092
+ kind: "async"
23093
+ }),
23094
+ toolExecute: channel({
23095
+ channelName: "tool.execute",
23096
+ kind: "async"
23097
+ })
23098
+ },
23099
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
23100
+ );
22004
23101
 
22005
23102
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
22006
23103
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -22770,38 +23867,40 @@ function normalizeError(error) {
22770
23867
  }
22771
23868
 
22772
23869
  // src/instrumentation/plugins/openrouter-channels.ts
22773
- var openRouterChannels = defineChannels("@openrouter/sdk", {
22774
- chatSend: channel({
22775
- channelName: "chat.send",
22776
- kind: "async"
22777
- }),
22778
- embeddingsGenerate: channel({
22779
- channelName: "embeddings.generate",
22780
- kind: "async"
22781
- }),
22782
- rerankRerank: channel(
22783
- {
23870
+ var openRouterChannels = defineChannels(
23871
+ "@openrouter/sdk",
23872
+ {
23873
+ chatSend: channel({
23874
+ channelName: "chat.send",
23875
+ kind: "async"
23876
+ }),
23877
+ embeddingsGenerate: channel({
23878
+ channelName: "embeddings.generate",
23879
+ kind: "async"
23880
+ }),
23881
+ rerankRerank: channel({
22784
23882
  channelName: "rerank.rerank",
22785
23883
  kind: "async"
22786
- }
22787
- ),
22788
- betaResponsesSend: channel({
22789
- channelName: "beta.responses.send",
22790
- kind: "async"
22791
- }),
22792
- callModel: channel({
22793
- channelName: "callModel",
22794
- kind: "sync-stream"
22795
- }),
22796
- callModelTurn: channel({
22797
- channelName: "callModel.turn",
22798
- kind: "async"
22799
- }),
22800
- toolExecute: channel({
22801
- channelName: "tool.execute",
22802
- kind: "async"
22803
- })
22804
- });
23884
+ }),
23885
+ betaResponsesSend: channel({
23886
+ channelName: "beta.responses.send",
23887
+ kind: "async"
23888
+ }),
23889
+ callModel: channel({
23890
+ channelName: "callModel",
23891
+ kind: "sync-stream"
23892
+ }),
23893
+ callModelTurn: channel({
23894
+ channelName: "callModel.turn",
23895
+ kind: "async"
23896
+ }),
23897
+ toolExecute: channel({
23898
+ channelName: "tool.execute",
23899
+ kind: "async"
23900
+ })
23901
+ },
23902
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
23903
+ );
22805
23904
 
22806
23905
  // src/instrumentation/plugins/openrouter-plugin.ts
22807
23906
  var OpenRouterPlugin = class extends BasePlugin {
@@ -23841,52 +24940,56 @@ function normalizeError2(error) {
23841
24940
  }
23842
24941
 
23843
24942
  // src/instrumentation/plugins/mistral-channels.ts
23844
- var mistralChannels = defineChannels("@mistralai/mistralai", {
23845
- chatComplete: channel({
23846
- channelName: "chat.complete",
23847
- kind: "async"
23848
- }),
23849
- chatStream: channel({
23850
- channelName: "chat.stream",
23851
- kind: "async"
23852
- }),
23853
- embeddingsCreate: channel({
23854
- channelName: "embeddings.create",
23855
- kind: "async"
23856
- }),
23857
- classifiersModerate: channel({
23858
- channelName: "classifiers.moderate",
23859
- kind: "async"
23860
- }),
23861
- classifiersModerateChat: channel({
23862
- channelName: "classifiers.moderateChat",
23863
- kind: "async"
23864
- }),
23865
- classifiersClassify: channel({
23866
- channelName: "classifiers.classify",
23867
- kind: "async"
23868
- }),
23869
- classifiersClassifyChat: channel({
23870
- channelName: "classifiers.classifyChat",
23871
- kind: "async"
23872
- }),
23873
- fimComplete: channel({
23874
- channelName: "fim.complete",
23875
- kind: "async"
23876
- }),
23877
- fimStream: channel({
23878
- channelName: "fim.stream",
23879
- kind: "async"
23880
- }),
23881
- agentsComplete: channel({
23882
- channelName: "agents.complete",
23883
- kind: "async"
23884
- }),
23885
- agentsStream: channel({
23886
- channelName: "agents.stream",
23887
- kind: "async"
23888
- })
23889
- });
24943
+ var mistralChannels = defineChannels(
24944
+ "@mistralai/mistralai",
24945
+ {
24946
+ chatComplete: channel({
24947
+ channelName: "chat.complete",
24948
+ kind: "async"
24949
+ }),
24950
+ chatStream: channel({
24951
+ channelName: "chat.stream",
24952
+ kind: "async"
24953
+ }),
24954
+ embeddingsCreate: channel({
24955
+ channelName: "embeddings.create",
24956
+ kind: "async"
24957
+ }),
24958
+ classifiersModerate: channel({
24959
+ channelName: "classifiers.moderate",
24960
+ kind: "async"
24961
+ }),
24962
+ classifiersModerateChat: channel({
24963
+ channelName: "classifiers.moderateChat",
24964
+ kind: "async"
24965
+ }),
24966
+ classifiersClassify: channel({
24967
+ channelName: "classifiers.classify",
24968
+ kind: "async"
24969
+ }),
24970
+ classifiersClassifyChat: channel({
24971
+ channelName: "classifiers.classifyChat",
24972
+ kind: "async"
24973
+ }),
24974
+ fimComplete: channel({
24975
+ channelName: "fim.complete",
24976
+ kind: "async"
24977
+ }),
24978
+ fimStream: channel({
24979
+ channelName: "fim.stream",
24980
+ kind: "async"
24981
+ }),
24982
+ agentsComplete: channel({
24983
+ channelName: "agents.complete",
24984
+ kind: "async"
24985
+ }),
24986
+ agentsStream: channel({
24987
+ channelName: "agents.stream",
24988
+ kind: "async"
24989
+ })
24990
+ },
24991
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
24992
+ );
23890
24993
 
23891
24994
  // src/instrumentation/plugins/mistral-plugin.ts
23892
24995
  var MistralPlugin = class extends BasePlugin {
@@ -24506,20 +25609,24 @@ function aggregateMistralStreamChunks(chunks) {
24506
25609
  }
24507
25610
 
24508
25611
  // src/instrumentation/plugins/google-adk-channels.ts
24509
- var googleADKChannels = defineChannels("@google/adk", {
24510
- runnerRunAsync: channel({
24511
- channelName: "runner.runAsync",
24512
- kind: "sync-stream"
24513
- }),
24514
- agentRunAsync: channel({
24515
- channelName: "agent.runAsync",
24516
- kind: "sync-stream"
24517
- }),
24518
- toolRunAsync: channel({
24519
- channelName: "tool.runAsync",
24520
- kind: "async"
24521
- })
24522
- });
25612
+ var googleADKChannels = defineChannels(
25613
+ "@google/adk",
25614
+ {
25615
+ runnerRunAsync: channel({
25616
+ channelName: "runner.runAsync",
25617
+ kind: "sync-stream"
25618
+ }),
25619
+ agentRunAsync: channel({
25620
+ channelName: "agent.runAsync",
25621
+ kind: "sync-stream"
25622
+ }),
25623
+ toolRunAsync: channel({
25624
+ channelName: "tool.runAsync",
25625
+ kind: "async"
25626
+ })
25627
+ },
25628
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
25629
+ );
24523
25630
 
24524
25631
  // src/instrumentation/plugins/google-adk-plugin.ts
24525
25632
  var GoogleADKPlugin = class extends BasePlugin {
@@ -24544,12 +25651,17 @@ var GoogleADKPlugin = class extends BasePlugin {
24544
25651
  const createState = (event) => {
24545
25652
  const params = event.arguments[0] ?? {};
24546
25653
  const contextKey = extractRunnerContextKey(params);
24547
- const span = startSpan({
24548
- name: "Google ADK Runner",
24549
- spanAttributes: {
24550
- type: "task" /* TASK */
24551
- }
24552
- });
25654
+ const span = startSpan(
25655
+ withSpanInstrumentationName(
25656
+ {
25657
+ name: "Google ADK Runner",
25658
+ spanAttributes: {
25659
+ type: "task" /* TASK */
25660
+ }
25661
+ },
25662
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25663
+ )
25664
+ );
24553
25665
  const startTime = getCurrentUnixTimestamp();
24554
25666
  try {
24555
25667
  const metadata = extractRunnerMetadata(params);
@@ -24635,18 +25747,23 @@ var GoogleADKPlugin = class extends BasePlugin {
24635
25747
  this.activeRunnerSpans
24636
25748
  );
24637
25749
  const contextKey = extractInvocationContextKey(parentContext);
24638
- const span = startSpan({
24639
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
24640
- spanAttributes: {
24641
- type: "task" /* TASK */
24642
- },
24643
- ...runnerParentSpan ? {
24644
- parentSpanIds: {
24645
- spanId: runnerParentSpan.spanId,
24646
- rootSpanId: runnerParentSpan.rootSpanId
24647
- }
24648
- } : {}
24649
- });
25750
+ const span = startSpan(
25751
+ withSpanInstrumentationName(
25752
+ {
25753
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
25754
+ spanAttributes: {
25755
+ type: "task" /* TASK */
25756
+ },
25757
+ ...runnerParentSpan ? {
25758
+ parentSpanIds: {
25759
+ spanId: runnerParentSpan.spanId,
25760
+ rootSpanId: runnerParentSpan.rootSpanId
25761
+ }
25762
+ } : {}
25763
+ },
25764
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25765
+ )
25766
+ );
24650
25767
  const startTime = getCurrentUnixTimestamp();
24651
25768
  try {
24652
25769
  const metadata = {
@@ -24739,22 +25856,27 @@ var GoogleADKPlugin = class extends BasePlugin {
24739
25856
  this.activeAgentSpans,
24740
25857
  this.activeRunnerSpans
24741
25858
  );
24742
- const createSpan = () => startSpan({
24743
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
24744
- spanAttributes: {
24745
- type: "tool" /* TOOL */
24746
- },
24747
- event: {
24748
- input: req.args,
24749
- metadata: {
24750
- provider: "google-adk",
24751
- ...toolName && { "google_adk.tool_name": toolName },
24752
- ...extractToolCallId(req) && {
24753
- "google_adk.tool_call_id": extractToolCallId(req)
25859
+ const createSpan = () => startSpan(
25860
+ withSpanInstrumentationName(
25861
+ {
25862
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
25863
+ spanAttributes: {
25864
+ type: "tool" /* TOOL */
25865
+ },
25866
+ event: {
25867
+ input: req.args,
25868
+ metadata: {
25869
+ provider: "google-adk",
25870
+ ...toolName && { "google_adk.tool_name": toolName },
25871
+ ...extractToolCallId(req) && {
25872
+ "google_adk.tool_call_id": extractToolCallId(req)
25873
+ }
25874
+ }
24754
25875
  }
24755
- }
24756
- }
24757
- });
25876
+ },
25877
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
25878
+ )
25879
+ );
24758
25880
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
24759
25881
  const startTime = getCurrentUnixTimestamp();
24760
25882
  states.set(event, { span, startTime });
@@ -25153,24 +26275,28 @@ function cleanMetrics4(metrics) {
25153
26275
  }
25154
26276
 
25155
26277
  // src/instrumentation/plugins/cohere-channels.ts
25156
- var cohereChannels = defineChannels("cohere-ai", {
25157
- chat: channel({
25158
- channelName: "chat",
25159
- kind: "async"
25160
- }),
25161
- chatStream: channel({
25162
- channelName: "chatStream",
25163
- kind: "async"
25164
- }),
25165
- embed: channel({
25166
- channelName: "embed",
25167
- kind: "async"
25168
- }),
25169
- rerank: channel({
25170
- channelName: "rerank",
25171
- kind: "async"
25172
- })
25173
- });
26278
+ var cohereChannels = defineChannels(
26279
+ "cohere-ai",
26280
+ {
26281
+ chat: channel({
26282
+ channelName: "chat",
26283
+ kind: "async"
26284
+ }),
26285
+ chatStream: channel({
26286
+ channelName: "chatStream",
26287
+ kind: "async"
26288
+ }),
26289
+ embed: channel({
26290
+ channelName: "embed",
26291
+ kind: "async"
26292
+ }),
26293
+ rerank: channel({
26294
+ channelName: "rerank",
26295
+ kind: "async"
26296
+ })
26297
+ },
26298
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
26299
+ );
25174
26300
 
25175
26301
  // src/instrumentation/plugins/cohere-plugin.ts
25176
26302
  var CoherePlugin = class extends BasePlugin {
@@ -25839,18 +26965,20 @@ function aggregateCohereChatStreamChunks(chunks) {
25839
26965
  }
25840
26966
 
25841
26967
  // src/instrumentation/plugins/groq-channels.ts
25842
- var groqChannels = defineChannels("groq-sdk", {
25843
- chatCompletionsCreate: channel({
25844
- channelName: "chat.completions.create",
25845
- kind: "async"
25846
- }),
25847
- embeddingsCreate: channel(
25848
- {
26968
+ var groqChannels = defineChannels(
26969
+ "groq-sdk",
26970
+ {
26971
+ chatCompletionsCreate: channel({
26972
+ channelName: "chat.completions.create",
26973
+ kind: "async"
26974
+ }),
26975
+ embeddingsCreate: channel({
25849
26976
  channelName: "embeddings.create",
25850
26977
  kind: "async"
25851
- }
25852
- )
25853
- });
26978
+ })
26979
+ },
26980
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
26981
+ );
25854
26982
 
25855
26983
  // src/instrumentation/plugins/groq-plugin.ts
25856
26984
  var GroqPlugin = class extends BasePlugin {
@@ -25953,15 +27081,27 @@ var clientSendChannel = channel({
25953
27081
  channelName: "client.send",
25954
27082
  kind: "async"
25955
27083
  });
25956
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
25957
- clientSend: clientSendChannel
25958
- });
25959
- var smithyCoreChannels = defineChannels("@smithy/core", {
25960
- clientSend: clientSendChannel
25961
- });
25962
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
25963
- clientSend: clientSendChannel
25964
- });
27084
+ var bedrockRuntimeChannels = defineChannels(
27085
+ "aws-bedrock-runtime",
27086
+ {
27087
+ clientSend: clientSendChannel
27088
+ },
27089
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27090
+ );
27091
+ var smithyCoreChannels = defineChannels(
27092
+ "@smithy/core",
27093
+ {
27094
+ clientSend: clientSendChannel
27095
+ },
27096
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27097
+ );
27098
+ var smithyClientChannels = defineChannels(
27099
+ "@smithy/smithy-client",
27100
+ {
27101
+ clientSend: clientSendChannel
27102
+ },
27103
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
27104
+ );
25965
27105
 
25966
27106
  // src/instrumentation/plugins/bedrock-runtime-common.ts
25967
27107
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -26595,38 +27735,46 @@ function extractTextFromJsonLike(value) {
26595
27735
  }
26596
27736
 
26597
27737
  // src/instrumentation/plugins/genkit-channels.ts
26598
- var genkitChannels = defineChannels("@genkit-ai/ai", {
26599
- generate: channel({
26600
- channelName: "generate",
26601
- kind: "async"
26602
- }),
26603
- generateStream: channel({
26604
- channelName: "generateStream",
26605
- kind: "sync-stream"
26606
- }),
26607
- embed: channel({
26608
- channelName: "embed",
26609
- kind: "async"
26610
- }),
26611
- embedMany: channel({
26612
- channelName: "embedMany",
26613
- kind: "async"
26614
- }),
26615
- actionRun: channel({
26616
- channelName: "action.run",
26617
- kind: "async"
26618
- }),
26619
- actionStream: channel({
26620
- channelName: "action.stream",
26621
- kind: "sync-stream"
26622
- })
26623
- });
26624
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
26625
- actionSpan: channel({
26626
- channelName: "action.span",
26627
- kind: "async"
26628
- })
26629
- });
27738
+ var genkitChannels = defineChannels(
27739
+ "@genkit-ai/ai",
27740
+ {
27741
+ generate: channel({
27742
+ channelName: "generate",
27743
+ kind: "async"
27744
+ }),
27745
+ generateStream: channel({
27746
+ channelName: "generateStream",
27747
+ kind: "sync-stream"
27748
+ }),
27749
+ embed: channel({
27750
+ channelName: "embed",
27751
+ kind: "async"
27752
+ }),
27753
+ embedMany: channel({
27754
+ channelName: "embedMany",
27755
+ kind: "async"
27756
+ }),
27757
+ actionRun: channel({
27758
+ channelName: "action.run",
27759
+ kind: "async"
27760
+ }),
27761
+ actionStream: channel({
27762
+ channelName: "action.stream",
27763
+ kind: "sync-stream"
27764
+ })
27765
+ },
27766
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
27767
+ );
27768
+ var genkitCoreChannels = defineChannels(
27769
+ "@genkit-ai/core",
27770
+ {
27771
+ actionSpan: channel({
27772
+ channelName: "action.span",
27773
+ kind: "async"
27774
+ })
27775
+ },
27776
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
27777
+ );
26630
27778
 
26631
27779
  // src/instrumentation/plugins/genkit-plugin.ts
26632
27780
  var GenkitPlugin = class extends BasePlugin {
@@ -26811,12 +27959,17 @@ function startActionSpanState(args) {
26811
27959
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
26812
27960
  return void 0;
26813
27961
  }
26814
- const span = startSpan({
26815
- name: actionSpanName(args.metadata, args.runStepName),
26816
- spanAttributes: {
26817
- type: actionSpanType(args.metadata)
26818
- }
26819
- });
27962
+ const span = startSpan(
27963
+ withSpanInstrumentationName(
27964
+ {
27965
+ name: actionSpanName(args.metadata, args.runStepName),
27966
+ spanAttributes: {
27967
+ type: actionSpanType(args.metadata)
27968
+ }
27969
+ },
27970
+ INSTRUMENTATION_NAMES.GENKIT
27971
+ )
27972
+ );
26820
27973
  const startTime = getCurrentUnixTimestamp();
26821
27974
  span.log({
26822
27975
  input: args.input,
@@ -27177,20 +28330,24 @@ function stringValue(value) {
27177
28330
  }
27178
28331
 
27179
28332
  // src/instrumentation/plugins/github-copilot-channels.ts
27180
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
27181
- createSession: channel({
27182
- channelName: "client.createSession",
27183
- kind: "async"
27184
- }),
27185
- resumeSession: channel({
27186
- channelName: "client.resumeSession",
27187
- kind: "async"
27188
- }),
27189
- sendAndWait: channel({
27190
- channelName: "session.sendAndWait",
27191
- kind: "async"
27192
- })
27193
- });
28333
+ var gitHubCopilotChannels = defineChannels(
28334
+ "@github/copilot-sdk",
28335
+ {
28336
+ createSession: channel({
28337
+ channelName: "client.createSession",
28338
+ kind: "async"
28339
+ }),
28340
+ resumeSession: channel({
28341
+ channelName: "client.resumeSession",
28342
+ kind: "async"
28343
+ }),
28344
+ sendAndWait: channel({
28345
+ channelName: "session.sendAndWait",
28346
+ kind: "async"
28347
+ })
28348
+ },
28349
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
28350
+ );
27194
28351
 
27195
28352
  // src/instrumentation/plugins/github-copilot-plugin.ts
27196
28353
  var ROOT_AGENT_KEY = "__root__";
@@ -27281,11 +28438,16 @@ async function handleTurnStart(state, agentId) {
27281
28438
  return;
27282
28439
  }
27283
28440
  const parentId = await getParentIdForAgent(state, agentId);
27284
- const span = startSpan({
27285
- name: "Copilot Turn",
27286
- parent: parentId,
27287
- spanAttributes: { type: "task" /* TASK */ }
27288
- });
28441
+ const span = startSpan(
28442
+ withSpanInstrumentationName(
28443
+ {
28444
+ name: "Copilot Turn",
28445
+ parent: parentId,
28446
+ spanAttributes: { type: "task" /* TASK */ }
28447
+ },
28448
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28449
+ )
28450
+ );
27289
28451
  const pendingUserMessage = state.pendingUserMessages.get(key);
27290
28452
  if (pendingUserMessage) {
27291
28453
  span.log({ input: pendingUserMessage });
@@ -27316,11 +28478,16 @@ async function handleUsage(state, agentId, usage) {
27316
28478
  const parentId = turn ? await turn.id : await state.session.id;
27317
28479
  const content = state.currentMessageContent.get(key);
27318
28480
  const { metrics, metadata } = extractMetricsFromUsage(usage);
27319
- const llmSpan = startSpan({
27320
- name: "github.copilot.llm",
27321
- parent: parentId,
27322
- spanAttributes: { type: "llm" /* LLM */ }
27323
- });
28481
+ const llmSpan = startSpan(
28482
+ withSpanInstrumentationName(
28483
+ {
28484
+ name: "github.copilot.llm",
28485
+ parent: parentId,
28486
+ spanAttributes: { type: "llm" /* LLM */ }
28487
+ },
28488
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28489
+ )
28490
+ );
27324
28491
  llmSpan.log({
27325
28492
  output: content ?? void 0,
27326
28493
  metadata,
@@ -27346,11 +28513,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
27346
28513
  }
27347
28514
  const parentId = await getToolParentId(state, agentId);
27348
28515
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
27349
- const span = startSpan({
27350
- name: displayName,
27351
- parent: parentId,
27352
- spanAttributes: { type: "tool" /* TOOL */ }
27353
- });
28516
+ const span = startSpan(
28517
+ withSpanInstrumentationName(
28518
+ {
28519
+ name: displayName,
28520
+ parent: parentId,
28521
+ spanAttributes: { type: "tool" /* TOOL */ }
28522
+ },
28523
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28524
+ )
28525
+ );
27354
28526
  const metadata = {
27355
28527
  "gen_ai.tool.name": toolName,
27356
28528
  "gen_ai.tool.call.id": toolCallId
@@ -27386,11 +28558,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
27386
28558
  }
27387
28559
  const tool = state.activeTools.get(toolCallId);
27388
28560
  const parentId = tool ? await tool.id : await state.session.id;
27389
- const span = startSpan({
27390
- name: `Agent: ${agentDisplayName}`,
27391
- parent: parentId,
27392
- spanAttributes: { type: "task" /* TASK */ }
27393
- });
28561
+ const span = startSpan(
28562
+ withSpanInstrumentationName(
28563
+ {
28564
+ name: `Agent: ${agentDisplayName}`,
28565
+ parent: parentId,
28566
+ spanAttributes: { type: "task" /* TASK */ }
28567
+ },
28568
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28569
+ )
28570
+ );
27394
28571
  span.log({
27395
28572
  metadata: {
27396
28573
  "github_copilot.agent_name": agentDisplayName,
@@ -27616,10 +28793,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
27616
28793
  if (!config || typeof config !== "object") {
27617
28794
  return;
27618
28795
  }
27619
- const sessionSpan = startSpan({
27620
- name: "Copilot Session",
27621
- spanAttributes: { type: "task" /* TASK */ }
27622
- });
28796
+ const sessionSpan = startSpan(
28797
+ withSpanInstrumentationName(
28798
+ {
28799
+ name: "Copilot Session",
28800
+ spanAttributes: { type: "task" /* TASK */ }
28801
+ },
28802
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
28803
+ )
28804
+ );
27623
28805
  const metadata = {};
27624
28806
  if (config.model) {
27625
28807
  metadata["github_copilot.model"] = config.model;
@@ -27707,12 +28889,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
27707
28889
  };
27708
28890
 
27709
28891
  // src/instrumentation/plugins/flue-channels.ts
27710
- var flueChannels = defineChannels("@flue/runtime", {
27711
- createContext: channel({
27712
- channelName: "createFlueContext",
27713
- kind: "sync-stream"
27714
- })
27715
- });
28892
+ var flueChannels = defineChannels(
28893
+ "@flue/runtime",
28894
+ {
28895
+ createContext: channel({
28896
+ channelName: "createFlueContext",
28897
+ kind: "sync-stream"
28898
+ })
28899
+ },
28900
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
28901
+ );
27716
28902
 
27717
28903
  // src/instrumentation/plugins/flue-plugin.ts
27718
28904
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -28055,15 +29241,20 @@ var FlueObserveBridge = class {
28055
29241
  safeLog3(existing.span, { input, metadata });
28056
29242
  return;
28057
29243
  }
28058
- const span = startSpan({
28059
- name: `workflow:${workflowName}`,
28060
- spanAttributes: { type: "task" /* TASK */ },
28061
- startTime: eventTime(event.startedAt ?? event.timestamp),
28062
- event: {
28063
- input,
28064
- metadata
28065
- }
28066
- });
29244
+ const span = startSpan(
29245
+ withSpanInstrumentationName(
29246
+ {
29247
+ name: `workflow:${workflowName}`,
29248
+ spanAttributes: { type: "task" /* TASK */ },
29249
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29250
+ event: {
29251
+ input,
29252
+ metadata
29253
+ }
29254
+ },
29255
+ INSTRUMENTATION_NAMES.FLUE
29256
+ )
29257
+ );
28067
29258
  this.runsById.set(event.runId, { metadata, span });
28068
29259
  }
28069
29260
  handleRunResume(event, ctx) {
@@ -28083,12 +29274,17 @@ var FlueObserveBridge = class {
28083
29274
  safeLog3(existing.span, { metadata });
28084
29275
  return;
28085
29276
  }
28086
- const span = startSpan({
28087
- name: `workflow:${workflowName}`,
28088
- spanAttributes: { type: "task" /* TASK */ },
28089
- startTime: eventTime(event.startedAt ?? event.timestamp),
28090
- event: { metadata }
28091
- });
29277
+ const span = startSpan(
29278
+ withSpanInstrumentationName(
29279
+ {
29280
+ name: `workflow:${workflowName}`,
29281
+ spanAttributes: { type: "task" /* TASK */ },
29282
+ startTime: eventTime(event.startedAt ?? event.timestamp),
29283
+ event: { metadata }
29284
+ },
29285
+ INSTRUMENTATION_NAMES.FLUE
29286
+ )
29287
+ );
28092
29288
  this.runsById.set(event.runId, { metadata, span });
28093
29289
  }
28094
29290
  handleRunEnd(event) {
@@ -28727,7 +29923,14 @@ function stateMatchesRun(state, runId) {
28727
29923
  return state.metadata["flue.run_id"] === runId;
28728
29924
  }
28729
29925
  function startFlueSpan(parent, args) {
28730
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
29926
+ return parent ? withCurrent(
29927
+ parent,
29928
+ () => startSpan(
29929
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
29930
+ )
29931
+ ) : startSpan(
29932
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
29933
+ );
28731
29934
  }
28732
29935
  function runWithCurrentSpanStore(span, next) {
28733
29936
  const state = _internalGetGlobalState();
@@ -28817,9 +30020,13 @@ var BraintrustLangChainCallbackHandler = class {
28817
30020
  ...this.options.debug ? { runId, parentRunId } : {}
28818
30021
  }
28819
30022
  };
28820
- let span = parentSpan.startSpan(args);
30023
+ const spanArgs = withSpanInstrumentationName(
30024
+ args,
30025
+ INSTRUMENTATION_NAMES.LANGCHAIN
30026
+ );
30027
+ let span = parentSpan.startSpan(spanArgs);
28821
30028
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
28822
- span = initLogger().startSpan(args);
30029
+ span = initLogger().startSpan(spanArgs);
28823
30030
  }
28824
30031
  this.spans.set(runId, span);
28825
30032
  }
@@ -29032,6 +30239,16 @@ function cleanObject(obj) {
29032
30239
  })
29033
30240
  );
29034
30241
  }
30242
+ function normalizeTokenMetrics(obj) {
30243
+ const metrics = cleanObject(obj);
30244
+ if (metrics.total_tokens !== void 0) {
30245
+ metrics.tokens = metrics.total_tokens;
30246
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
30247
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
30248
+ }
30249
+ delete metrics.total_tokens;
30250
+ return metrics;
30251
+ }
29035
30252
  function walkGenerations(response) {
29036
30253
  const result = [];
29037
30254
  const generations = response.generations || [];
@@ -29078,7 +30295,7 @@ function getMetricsFromResponse(response) {
29078
30295
  continue;
29079
30296
  }
29080
30297
  const inputTokenDetails = usageMetadata.input_token_details;
29081
- return cleanObject({
30298
+ return normalizeTokenMetrics({
29082
30299
  total_tokens: usageMetadata.total_tokens,
29083
30300
  prompt_tokens: usageMetadata.input_tokens,
29084
30301
  completion_tokens: usageMetadata.output_tokens,
@@ -29088,7 +30305,7 @@ function getMetricsFromResponse(response) {
29088
30305
  }
29089
30306
  const llmOutput = response.llmOutput || {};
29090
30307
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
29091
- return cleanObject({
30308
+ return normalizeTokenMetrics({
29092
30309
  total_tokens: tokenUsage.totalTokens,
29093
30310
  prompt_tokens: tokenUsage.promptTokens,
29094
30311
  completion_tokens: tokenUsage.completionTokens
@@ -29106,16 +30323,20 @@ function isRecord(value) {
29106
30323
  }
29107
30324
 
29108
30325
  // src/instrumentation/plugins/langchain-channels.ts
29109
- var langChainChannels = defineChannels("@langchain/core", {
29110
- configure: channel({
29111
- channelName: "CallbackManager.configure",
29112
- kind: "sync-stream"
29113
- }),
29114
- configureSync: channel({
29115
- channelName: "CallbackManager._configureSync",
29116
- kind: "sync-stream"
29117
- })
29118
- });
30326
+ var langChainChannels = defineChannels(
30327
+ "@langchain/core",
30328
+ {
30329
+ configure: channel({
30330
+ channelName: "CallbackManager.configure",
30331
+ kind: "sync-stream"
30332
+ }),
30333
+ configureSync: channel({
30334
+ channelName: "CallbackManager._configureSync",
30335
+ kind: "sync-stream"
30336
+ })
30337
+ },
30338
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
30339
+ );
29119
30340
 
29120
30341
  // src/instrumentation/plugins/langchain-plugin.ts
29121
30342
  var LangChainPlugin = class extends BasePlugin {
@@ -29203,20 +30424,24 @@ function isBraintrustHandler(handler) {
29203
30424
  }
29204
30425
 
29205
30426
  // src/instrumentation/plugins/langsmith-channels.ts
29206
- var langSmithChannels = defineChannels("langsmith", {
29207
- createRun: channel({
29208
- channelName: "Client.createRun",
29209
- kind: "async"
29210
- }),
29211
- updateRun: channel({
29212
- channelName: "Client.updateRun",
29213
- kind: "async"
29214
- }),
29215
- batchIngestRuns: channel({
29216
- channelName: "Client.batchIngestRuns",
29217
- kind: "async"
29218
- })
29219
- });
30427
+ var langSmithChannels = defineChannels(
30428
+ "langsmith",
30429
+ {
30430
+ createRun: channel({
30431
+ channelName: "Client.createRun",
30432
+ kind: "async"
30433
+ }),
30434
+ updateRun: channel({
30435
+ channelName: "Client.updateRun",
30436
+ kind: "async"
30437
+ }),
30438
+ batchIngestRuns: channel({
30439
+ channelName: "Client.batchIngestRuns",
30440
+ kind: "async"
30441
+ })
30442
+ },
30443
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
30444
+ );
29220
30445
 
29221
30446
  // src/instrumentation/plugins/langsmith-plugin.ts
29222
30447
  var MAX_COMPLETED_RUNS = 1e4;
@@ -29380,19 +30605,24 @@ var LangSmithPlugin = class extends BasePlugin {
29380
30605
  const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
29381
30606
  const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
29382
30607
  const startTime = timestampSeconds(ownValue(run, "start_time"));
29383
- return startSpan({
29384
- name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
29385
- spanId: id,
29386
- parentSpanIds: {
29387
- parentSpanIds: parentId ? [parentId] : [],
29388
- rootSpanId: traceId
29389
- },
29390
- spanAttributes: {
29391
- type: mapRunType(ownValue(run, "run_type"))
29392
- },
29393
- ...startTime === void 0 ? {} : { startTime },
29394
- event: { id }
29395
- });
30608
+ return startSpan(
30609
+ withSpanInstrumentationName(
30610
+ {
30611
+ name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
30612
+ spanId: id,
30613
+ parentSpanIds: {
30614
+ parentSpanIds: parentId ? [parentId] : [],
30615
+ rootSpanId: traceId
30616
+ },
30617
+ spanAttributes: {
30618
+ type: mapRunType(ownValue(run, "run_type"))
30619
+ },
30620
+ ...startTime === void 0 ? {} : { startTime },
30621
+ event: { id }
30622
+ },
30623
+ INSTRUMENTATION_NAMES.LANGSMITH
30624
+ )
30625
+ );
29396
30626
  }
29397
30627
  logRun(span, run, previous, includeOutput) {
29398
30628
  const inputs = preferOwnValue(run, previous, "inputs");
@@ -29650,7 +30880,8 @@ var piCodingAgentChannels = defineChannels(
29650
30880
  channelName: "AgentSession.prompt",
29651
30881
  kind: "async"
29652
30882
  })
29653
- }
30883
+ },
30884
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
29654
30885
  );
29655
30886
 
29656
30887
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -29734,14 +30965,19 @@ function startPiPromptRun(event, onFinalize) {
29734
30965
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
29735
30966
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
29736
30967
  };
29737
- const span = startSpan({
29738
- event: {
29739
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
29740
- metadata
29741
- },
29742
- name: "AgentSession.prompt",
29743
- spanAttributes: { type: "task" /* TASK */ }
29744
- });
30968
+ const span = startSpan(
30969
+ withSpanInstrumentationName(
30970
+ {
30971
+ event: {
30972
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
30973
+ metadata
30974
+ },
30975
+ name: "AgentSession.prompt",
30976
+ spanAttributes: { type: "task" /* TASK */ }
30977
+ },
30978
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
30979
+ )
30980
+ );
29745
30981
  const streamPatchState = installPiStreamPatch(agent);
29746
30982
  const options = event.arguments[1];
29747
30983
  const promptText = event.arguments[0];
@@ -29911,15 +31147,20 @@ async function startPiLlmSpan(state, model, context, options) {
29911
31147
  ...extractToolMetadata(context.tools),
29912
31148
  "pi_coding_agent.operation": "agent.streamFn"
29913
31149
  };
29914
- const span = startSpan({
29915
- event: {
29916
- input: processInputAttachments(normalizePiContextInput(context)),
29917
- metadata
29918
- },
29919
- name: getLlmSpanName(model),
29920
- parent: await state.span.export(),
29921
- spanAttributes: { type: "llm" /* LLM */ }
29922
- });
31150
+ const span = startSpan(
31151
+ withSpanInstrumentationName(
31152
+ {
31153
+ event: {
31154
+ input: processInputAttachments(normalizePiContextInput(context)),
31155
+ metadata
31156
+ },
31157
+ name: getLlmSpanName(model),
31158
+ parent: await state.span.export(),
31159
+ spanAttributes: { type: "llm" /* LLM */ }
31160
+ },
31161
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31162
+ )
31163
+ );
29923
31164
  const llmState = {
29924
31165
  finalized: false,
29925
31166
  metadata,
@@ -30087,15 +31328,20 @@ async function startPiToolSpan(state, event) {
30087
31328
  "pi_coding_agent.tool.name": event.toolName
30088
31329
  };
30089
31330
  try {
30090
- const span = startSpan({
30091
- event: {
30092
- input: event.args,
30093
- metadata
30094
- },
30095
- name: event.toolName || "tool",
30096
- parent: await state.span.export(),
30097
- spanAttributes: { type: "tool" /* TOOL */ }
30098
- });
31331
+ const span = startSpan(
31332
+ withSpanInstrumentationName(
31333
+ {
31334
+ event: {
31335
+ input: event.args,
31336
+ metadata
31337
+ },
31338
+ name: event.toolName || "tool",
31339
+ parent: await state.span.export(),
31340
+ spanAttributes: { type: "tool" /* TOOL */ }
31341
+ },
31342
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
31343
+ )
31344
+ );
30099
31345
  state.activeToolSpans.set(event.toolCallId, {
30100
31346
  restoreAutoInstrumentation,
30101
31347
  span
@@ -30527,20 +31773,24 @@ function logInstrumentationError4(context, error) {
30527
31773
  }
30528
31774
 
30529
31775
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
30530
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
30531
- agentStream: channel({
30532
- channelName: "Agent.stream",
30533
- kind: "sync-stream"
30534
- }),
30535
- graphStream: channel({
30536
- channelName: "Graph.stream",
30537
- kind: "sync-stream"
30538
- }),
30539
- swarmStream: channel({
30540
- channelName: "Swarm.stream",
30541
- kind: "sync-stream"
30542
- })
30543
- });
31776
+ var strandsAgentSDKChannels = defineChannels(
31777
+ "@strands-agents/sdk",
31778
+ {
31779
+ agentStream: channel({
31780
+ channelName: "Agent.stream",
31781
+ kind: "sync-stream"
31782
+ }),
31783
+ graphStream: channel({
31784
+ channelName: "Graph.stream",
31785
+ kind: "sync-stream"
31786
+ }),
31787
+ swarmStream: channel({
31788
+ channelName: "Swarm.stream",
31789
+ kind: "sync-stream"
31790
+ })
31791
+ },
31792
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
31793
+ );
30544
31794
 
30545
31795
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
30546
31796
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -30695,22 +31945,32 @@ function startAgentStream(event, activeChildParents) {
30695
31945
  );
30696
31946
  const span = parentSpan ? withCurrent(
30697
31947
  parentSpan,
30698
- () => startSpan({
30699
- event: {
30700
- input,
30701
- metadata
31948
+ () => startSpan(
31949
+ withSpanInstrumentationName(
31950
+ {
31951
+ event: {
31952
+ input,
31953
+ metadata
31954
+ },
31955
+ name: formatAgentSpanName(agent),
31956
+ spanAttributes: { type: "task" /* TASK */ }
31957
+ },
31958
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
31959
+ )
31960
+ )
31961
+ ) : startSpan(
31962
+ withSpanInstrumentationName(
31963
+ {
31964
+ event: {
31965
+ input,
31966
+ metadata
31967
+ },
31968
+ name: formatAgentSpanName(agent),
31969
+ spanAttributes: { type: "task" /* TASK */ }
30702
31970
  },
30703
- name: formatAgentSpanName(agent),
30704
- spanAttributes: { type: "task" /* TASK */ }
30705
- })
30706
- ) : startSpan({
30707
- event: {
30708
- input,
30709
- metadata
30710
- },
30711
- name: formatAgentSpanName(agent),
30712
- spanAttributes: { type: "task" /* TASK */ }
30713
- });
31971
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
31972
+ )
31973
+ );
30714
31974
  return {
30715
31975
  activeTools: /* @__PURE__ */ new Map(),
30716
31976
  attachmentCache,
@@ -30732,22 +31992,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
30732
31992
  const input = processStrandsInputAttachments(event.arguments[0]);
30733
31993
  const span = parentSpan ? withCurrent(
30734
31994
  parentSpan,
30735
- () => startSpan({
30736
- event: {
30737
- input,
30738
- metadata
31995
+ () => startSpan(
31996
+ withSpanInstrumentationName(
31997
+ {
31998
+ event: {
31999
+ input,
32000
+ metadata
32001
+ },
32002
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32003
+ spanAttributes: { type: "task" /* TASK */ }
32004
+ },
32005
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32006
+ )
32007
+ )
32008
+ ) : startSpan(
32009
+ withSpanInstrumentationName(
32010
+ {
32011
+ event: {
32012
+ input,
32013
+ metadata
32014
+ },
32015
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
32016
+ spanAttributes: { type: "task" /* TASK */ }
30739
32017
  },
30740
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30741
- spanAttributes: { type: "task" /* TASK */ }
30742
- })
30743
- ) : startSpan({
30744
- event: {
30745
- input,
30746
- metadata
30747
- },
30748
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
30749
- spanAttributes: { type: "task" /* TASK */ }
30750
- });
32018
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32019
+ )
32020
+ );
30751
32021
  return {
30752
32022
  activeNodes: /* @__PURE__ */ new Map(),
30753
32023
  finalized: false,
@@ -30849,17 +32119,22 @@ function startModelSpan(state, event) {
30849
32119
  };
30850
32120
  const span = withCurrent(
30851
32121
  state.span,
30852
- () => startSpan({
30853
- event: {
30854
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
30855
- event.agent.messages,
30856
- state.attachmentCache
30857
- ) : void 0,
30858
- metadata
30859
- },
30860
- name: formatModelSpanName(model),
30861
- spanAttributes: { type: "llm" /* LLM */ }
30862
- })
32122
+ () => startSpan(
32123
+ withSpanInstrumentationName(
32124
+ {
32125
+ event: {
32126
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
32127
+ event.agent.messages,
32128
+ state.attachmentCache
32129
+ ) : void 0,
32130
+ metadata
32131
+ },
32132
+ name: formatModelSpanName(model),
32133
+ spanAttributes: { type: "llm" /* LLM */ }
32134
+ },
32135
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32136
+ )
32137
+ )
30863
32138
  );
30864
32139
  state.activeModel = {
30865
32140
  metadata,
@@ -30911,20 +32186,25 @@ function startToolSpan2(state, event) {
30911
32186
  const name = extractToolName3(toolUse, event.tool);
30912
32187
  const span = withCurrent(
30913
32188
  state.span,
30914
- () => startSpan({
30915
- event: {
30916
- input: toolUse?.input,
30917
- metadata: {
30918
- "gen_ai.tool.call.id": toolUse?.toolUseId,
30919
- "gen_ai.tool.name": name,
30920
- "strands.operation": "tool.call",
30921
- "strands.tool.name": name,
30922
- provider: "strands"
30923
- }
30924
- },
30925
- name: `tool: ${name}`,
30926
- spanAttributes: { type: "tool" /* TOOL */ }
30927
- })
32189
+ () => startSpan(
32190
+ withSpanInstrumentationName(
32191
+ {
32192
+ event: {
32193
+ input: toolUse?.input,
32194
+ metadata: {
32195
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
32196
+ "gen_ai.tool.name": name,
32197
+ "strands.operation": "tool.call",
32198
+ "strands.tool.name": name,
32199
+ provider: "strands"
32200
+ }
32201
+ },
32202
+ name: `tool: ${name}`,
32203
+ spanAttributes: { type: "tool" /* TOOL */ }
32204
+ },
32205
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32206
+ )
32207
+ )
30928
32208
  );
30929
32209
  state.activeTools.set(key, {
30930
32210
  span,
@@ -30995,11 +32275,16 @@ function startNodeSpan(state, event, activeChildParents) {
30995
32275
  };
30996
32276
  const span = withCurrent(
30997
32277
  state.span,
30998
- () => startSpan({
30999
- event: { metadata },
31000
- name: `node: ${nodeId}`,
31001
- spanAttributes: { type: "task" /* TASK */ }
31002
- })
32278
+ () => startSpan(
32279
+ withSpanInstrumentationName(
32280
+ {
32281
+ event: { metadata },
32282
+ name: `node: ${nodeId}`,
32283
+ spanAttributes: { type: "task" /* TASK */ }
32284
+ },
32285
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
32286
+ )
32287
+ )
31003
32288
  );
31004
32289
  const nodeState = {
31005
32290
  ...child ? { child } : {},
@@ -32777,6 +34062,26 @@ function wrapAISDK(aiSDK, options = {}) {
32777
34062
  }
32778
34063
  });
32779
34064
  }
34065
+ function isHarnessAgentInstance(instance) {
34066
+ try {
34067
+ const visited = /* @__PURE__ */ new Set();
34068
+ let prototype = Object.getPrototypeOf(instance);
34069
+ while (prototype !== null && !visited.has(prototype)) {
34070
+ visited.add(prototype);
34071
+ const constructor = Object.getOwnPropertyDescriptor(
34072
+ prototype,
34073
+ "constructor"
34074
+ )?.value;
34075
+ const constructorName = typeof constructor === "function" ? Object.getOwnPropertyDescriptor(constructor, "name")?.value : void 0;
34076
+ if (constructorName === "HarnessAgent") {
34077
+ return true;
34078
+ }
34079
+ prototype = Object.getPrototypeOf(prototype);
34080
+ }
34081
+ } catch {
34082
+ }
34083
+ return false;
34084
+ }
32780
34085
  var wrapAgentClass = (AgentClass, options = {}) => {
32781
34086
  const typedAgentClass = AgentClass;
32782
34087
  return new Proxy(typedAgentClass, {
@@ -32786,9 +34091,52 @@ var wrapAgentClass = (AgentClass, options = {}) => {
32786
34091
  args,
32787
34092
  newTarget
32788
34093
  );
34094
+ const harnessAgent = isHarnessAgentInstance(instance) ? instance : null;
32789
34095
  return new Proxy(instance, {
32790
34096
  get(instanceTarget, prop, instanceReceiver) {
32791
34097
  const original = Reflect.get(instanceTarget, prop, instanceTarget);
34098
+ if (harnessAgent && typeof original === "function") {
34099
+ switch (prop) {
34100
+ case "createSession":
34101
+ return wrapHarnessAgentCreateSession(
34102
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
34103
+ original,
34104
+ harnessAgent
34105
+ );
34106
+ case "generate":
34107
+ return wrapHarnessAgentGenerate(
34108
+ original,
34109
+ harnessAgent,
34110
+ harnessAgentChannels.generate,
34111
+ "HarnessAgent.generate",
34112
+ options
34113
+ );
34114
+ case "stream":
34115
+ return wrapHarnessAgentStream(
34116
+ original,
34117
+ harnessAgent,
34118
+ harnessAgentChannels.stream,
34119
+ "HarnessAgent.stream",
34120
+ options
34121
+ );
34122
+ case "continueGenerate":
34123
+ return wrapHarnessAgentGenerate(
34124
+ original,
34125
+ harnessAgent,
34126
+ harnessAgentChannels.continueGenerate,
34127
+ "HarnessAgent.continueGenerate",
34128
+ options
34129
+ );
34130
+ case "continueStream":
34131
+ return wrapHarnessAgentStream(
34132
+ original,
34133
+ harnessAgent,
34134
+ harnessAgentChannels.continueStream,
34135
+ "HarnessAgent.continueStream",
34136
+ options
34137
+ );
34138
+ }
34139
+ }
32792
34140
  if (prop === "generate" && typeof original === "function" && instanceTarget.constructor.name !== "WorkflowAgent") {
32793
34141
  return wrapAgentGenerate(original, instanceTarget, options);
32794
34142
  }
@@ -32804,6 +34152,39 @@ var wrapAgentClass = (AgentClass, options = {}) => {
32804
34152
  }
32805
34153
  });
32806
34154
  };
34155
+ var wrapHarnessAgentCreateSession = (createSession, instance) => {
34156
+ const wrapper = function(params) {
34157
+ return harnessAgentChannels.createSession.tracePromise(
34158
+ () => params === void 0 ? createSession.call(instance) : createSession.call(instance, params),
34159
+ createAISDKChannelContext(params ?? {}, { self: instance })
34160
+ );
34161
+ };
34162
+ Object.defineProperty(wrapper, "name", {
34163
+ value: "HarnessAgent.createSession",
34164
+ writable: false
34165
+ });
34166
+ return wrapper;
34167
+ };
34168
+ var wrapHarnessAgentGenerate = (generate, instance, channel2, name, options) => makeGenerateTextWrapper(
34169
+ channel2,
34170
+ name,
34171
+ generate.bind(instance),
34172
+ {
34173
+ self: instance,
34174
+ spanType: "task" /* TASK */
34175
+ },
34176
+ options
34177
+ );
34178
+ var wrapHarnessAgentStream = (stream, instance, channel2, name, options) => makeStreamWrapper(
34179
+ channel2,
34180
+ name,
34181
+ stream.bind(instance),
34182
+ {
34183
+ self: instance,
34184
+ spanType: "task" /* TASK */
34185
+ },
34186
+ options
34187
+ );
32807
34188
  var wrapAgentGenerate = (generate, instance, options = {}) => {
32808
34189
  const defaultName = `${instance.constructor.name}.generate`;
32809
34190
  return async (params) => makeGenerateTextWrapper(
@@ -33030,6 +34411,7 @@ var BraintrustLanguageModelWrapper = class {
33030
34411
  this.supportsUrl = (url) => this.model.supportsUrl(url);
33031
34412
  }
33032
34413
  }
34414
+ model;
33033
34415
  supportsUrl;
33034
34416
  get specificationVersion() {
33035
34417
  return this.model.specificationVersion;
@@ -33052,12 +34434,17 @@ var BraintrustLanguageModelWrapper = class {
33052
34434
  // For the first cut, do not support custom span_info arguments. We can
33053
34435
  // propagate those via async local storage
33054
34436
  async doGenerate(options) {
33055
- const span = startSpan({
33056
- name: "Chat Completion",
33057
- spanAttributes: {
33058
- type: "llm"
33059
- }
33060
- });
34437
+ const span = startSpan(
34438
+ withSpanInstrumentationName(
34439
+ {
34440
+ name: "Chat Completion",
34441
+ spanAttributes: {
34442
+ type: "llm"
34443
+ }
34444
+ },
34445
+ INSTRUMENTATION_NAMES.AI_SDK
34446
+ )
34447
+ );
33061
34448
  const { prompt, mode, ...rest } = options;
33062
34449
  const startTime = getCurrentUnixTimestamp();
33063
34450
  try {
@@ -33088,12 +34475,17 @@ var BraintrustLanguageModelWrapper = class {
33088
34475
  async doStream(options) {
33089
34476
  const { prompt, mode, ...rest } = options;
33090
34477
  const startTime = getCurrentUnixTimestamp();
33091
- const span = startSpan({
33092
- name: "Chat Completion",
33093
- spanAttributes: {
33094
- type: "llm"
33095
- }
33096
- });
34478
+ const span = startSpan(
34479
+ withSpanInstrumentationName(
34480
+ {
34481
+ name: "Chat Completion",
34482
+ spanAttributes: {
34483
+ type: "llm"
34484
+ }
34485
+ },
34486
+ INSTRUMENTATION_NAMES.AI_SDK
34487
+ )
34488
+ );
33097
34489
  span.log({
33098
34490
  input: postProcessPrompt(prompt),
33099
34491
  metadata: {
@@ -33448,7 +34840,9 @@ function BraintrustMiddleware(config = {}) {
33448
34840
  }
33449
34841
  }
33450
34842
  };
33451
- const span = startSpan(spanArgs);
34843
+ const span = startSpan(
34844
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
34845
+ );
33452
34846
  try {
33453
34847
  const result = await doGenerate();
33454
34848
  const metadata = {};
@@ -33510,7 +34904,9 @@ function BraintrustMiddleware(config = {}) {
33510
34904
  }
33511
34905
  }
33512
34906
  };
33513
- const span = startSpan(spanArgs);
34907
+ const span = startSpan(
34908
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
34909
+ );
33514
34910
  try {
33515
34911
  const { stream, ...rest } = await doStream();
33516
34912
  const textChunks = [];
@@ -33612,6 +35008,7 @@ function BraintrustMiddleware(config = {}) {
33612
35008
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
33613
35009
  var MAX_EVE_CACHE_ENTRIES = 1e4;
33614
35010
  var MAX_STORED_LLM_INPUTS = 100;
35011
+ var MAX_STORED_REASONING_BLOCKS = 100;
33615
35012
  var MAX_STORED_SPAN_REFERENCES = 1e4;
33616
35013
  var MAX_STORED_STEP_STARTS = 1e4;
33617
35014
  function braintrustEveHook(options) {
@@ -33650,6 +35047,7 @@ var ResumedEveSpan = class {
33650
35047
  this.reference = reference;
33651
35048
  this.endTime = reference.endTime;
33652
35049
  }
35050
+ reference;
33653
35051
  endTime;
33654
35052
  get rootSpanId() {
33655
35053
  return this.reference.rootSpanId;
@@ -33682,6 +35080,7 @@ var EveBridge = class {
33682
35080
  constructor(state) {
33683
35081
  this.state = state;
33684
35082
  }
35083
+ state;
33685
35084
  eventQueuesBySession = /* @__PURE__ */ new Map();
33686
35085
  completedToolKeys = new LRUCache({
33687
35086
  max: MAX_EVE_CACHE_ENTRIES
@@ -33714,7 +35113,12 @@ var EveBridge = class {
33714
35113
  };
33715
35114
  const span = withCurrent(
33716
35115
  NOOP_SPAN,
33717
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
35116
+ () => _internalStartSpanWithInitialMerge(
35117
+ withSpanInstrumentationName(
35118
+ { ...args, startTime },
35119
+ INSTRUMENTATION_NAMES.EVE
35120
+ )
35121
+ )
33718
35122
  );
33719
35123
  if (typeof rowId !== "string") {
33720
35124
  return span;
@@ -33860,6 +35264,9 @@ var EveBridge = class {
33860
35264
  case "step.started":
33861
35265
  await this.handleStepStarted(event, ctx, hookMetadata);
33862
35266
  return true;
35267
+ case "reasoning.completed":
35268
+ this.handleReasoningCompleted(event, ctx);
35269
+ return true;
33863
35270
  case "message.completed":
33864
35271
  this.handleMessageCompleted(event, ctx);
33865
35272
  return true;
@@ -33951,6 +35358,7 @@ var EveBridge = class {
33951
35358
  key,
33952
35359
  metadata,
33953
35360
  metrics: {},
35361
+ sessionId,
33954
35362
  span,
33955
35363
  stepsByIndex: /* @__PURE__ */ new Map(),
33956
35364
  turnId: event.data.turnId
@@ -33981,6 +35389,12 @@ var EveBridge = class {
33981
35389
  const endTime = eventTime2(event);
33982
35390
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
33983
35391
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35392
+ clearStoredEveReasoning(
35393
+ this.state,
35394
+ sessionId,
35395
+ event.data.turnId,
35396
+ event.data.stepIndex
35397
+ );
33984
35398
  }
33985
35399
  const stepOrdinal = this.stepOrdinal(event);
33986
35400
  const metadata = { ...turn.metadata };
@@ -33990,6 +35404,13 @@ var EveBridge = class {
33990
35404
  event.data.turnId,
33991
35405
  event.data.stepIndex
33992
35406
  );
35407
+ const reasoning = readStoredEveReasoning(
35408
+ this.state,
35409
+ sessionId,
35410
+ event.data.turnId,
35411
+ event.data.stepIndex
35412
+ );
35413
+ const output = mergeEveReasoning(void 0, reasoning);
33993
35414
  const { rowId: eventId, spanId } = await generateEveIds(
33994
35415
  "step",
33995
35416
  sessionId,
@@ -34007,32 +35428,52 @@ var EveBridge = class {
34007
35428
  spanId,
34008
35429
  startTime: eventTime2(event)
34009
35430
  });
34010
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
35431
+ span.log({
35432
+ ...input !== void 0 ? { input } : {},
35433
+ metadata
35434
+ });
34011
35435
  turn.stepsByIndex.set(event.data.stepIndex, {
34012
35436
  ...input !== void 0 ? { input } : {},
34013
35437
  metadata,
34014
35438
  metrics: {},
35439
+ ...output !== void 0 ? { output } : {},
35440
+ reasoning,
34015
35441
  span
34016
35442
  });
34017
35443
  }
35444
+ handleReasoningCompleted(event, ctx) {
35445
+ const sessionId = sessionIdFromContext(ctx);
35446
+ if (!sessionId) {
35447
+ return;
35448
+ }
35449
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
35450
+ const step = this.stepForEvent(event, ctx);
35451
+ if (step) {
35452
+ step.reasoning = reasoning;
35453
+ step.output = mergeEveReasoning(step.output, reasoning);
35454
+ }
35455
+ }
34018
35456
  handleMessageCompleted(event, ctx) {
34019
35457
  const step = this.stepForEvent(event, ctx);
34020
35458
  if (!step) {
34021
35459
  return;
34022
35460
  }
34023
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
35461
+ const existingMessage = eveOutputMessage(step.output);
34024
35462
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
34025
- step.output = [
34026
- {
34027
- finish_reason: normalizedFinishReason(event.data.finishReason),
34028
- index: 0,
34029
- message: {
34030
- content: event.data.message,
34031
- role: "assistant",
34032
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35463
+ step.output = mergeEveReasoning(
35464
+ [
35465
+ {
35466
+ finish_reason: normalizedFinishReason(event.data.finishReason),
35467
+ index: 0,
35468
+ message: {
35469
+ content: event.data.message,
35470
+ role: "assistant",
35471
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
35472
+ }
34033
35473
  }
34034
- }
34035
- ];
35474
+ ],
35475
+ step.reasoning
35476
+ );
34036
35477
  const turn = this.turnForEvent(event, ctx);
34037
35478
  if (turn && event.data.finishReason !== "tool-calls") {
34038
35479
  turn.output = event.data.message;
@@ -34041,16 +35482,19 @@ var EveBridge = class {
34041
35482
  handleResultCompleted(event, ctx) {
34042
35483
  const step = this.stepForEvent(event, ctx);
34043
35484
  if (step) {
34044
- step.output = [
34045
- {
34046
- finish_reason: "stop",
34047
- index: 0,
34048
- message: {
34049
- content: event.data.result,
34050
- role: "assistant"
35485
+ step.output = mergeEveReasoning(
35486
+ [
35487
+ {
35488
+ finish_reason: "stop",
35489
+ index: 0,
35490
+ message: {
35491
+ content: event.data.result,
35492
+ role: "assistant"
35493
+ }
34051
35494
  }
34052
- }
34053
- ];
35495
+ ],
35496
+ step.reasoning
35497
+ );
34054
35498
  }
34055
35499
  const turn = this.turnForEvent(event, ctx);
34056
35500
  if (turn) {
@@ -34100,18 +35544,20 @@ var EveBridge = class {
34100
35544
  type: "function"
34101
35545
  });
34102
35546
  }
34103
- step.output = [
34104
- {
34105
- finish_reason: "tool_calls",
34106
- index: 0,
34107
- message: {
34108
- content: null,
34109
- role: "assistant",
34110
- tool_calls: [...toolCallsById.values()]
35547
+ step.output = mergeEveReasoning(
35548
+ [
35549
+ {
35550
+ finish_reason: "tool_calls",
35551
+ index: 0,
35552
+ message: {
35553
+ content: null,
35554
+ role: "assistant",
35555
+ tool_calls: [...toolCallsById.values()]
35556
+ }
34111
35557
  }
34112
- }
34113
- ];
34114
- step.span.log({ metadata: step.metadata, output: step.output });
35558
+ ],
35559
+ step.reasoning
35560
+ );
34115
35561
  }
34116
35562
  async handleActionResult(event, ctx, hookMetadata) {
34117
35563
  if (isToolResult(event.data.result)) {
@@ -34289,6 +35735,7 @@ var EveBridge = class {
34289
35735
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
34290
35736
  };
34291
35737
  step.metrics = { ...step.metrics, ...metrics };
35738
+ const sessionId = sessionIdFromContext(ctx);
34292
35739
  if (Array.isArray(step.output) && isObject(step.output[0])) {
34293
35740
  const finishReason = step.output[0].finish_reason;
34294
35741
  if (typeof finishReason !== "string") {
@@ -34313,6 +35760,14 @@ var EveBridge = class {
34313
35760
  turn.stepsByIndex.delete(event.data.stepIndex);
34314
35761
  }
34315
35762
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35763
+ if (sessionId) {
35764
+ clearStoredEveReasoning(
35765
+ this.state,
35766
+ sessionId,
35767
+ event.data.turnId,
35768
+ event.data.stepIndex
35769
+ );
35770
+ }
34316
35771
  }
34317
35772
  handleStepFailed(event, ctx) {
34318
35773
  const step = this.stepForEvent(event, ctx);
@@ -34330,6 +35785,15 @@ var EveBridge = class {
34330
35785
  const turn = this.turnForEvent(event, ctx);
34331
35786
  turn?.stepsByIndex.delete(event.data.stepIndex);
34332
35787
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
35788
+ const sessionId = sessionIdFromContext(ctx);
35789
+ if (sessionId) {
35790
+ clearStoredEveReasoning(
35791
+ this.state,
35792
+ sessionId,
35793
+ event.data.turnId,
35794
+ event.data.stepIndex
35795
+ );
35796
+ }
34333
35797
  }
34334
35798
  handleTurnCompleted(event, ctx) {
34335
35799
  const turn = this.turnForEvent(event, ctx);
@@ -34427,6 +35891,7 @@ var EveBridge = class {
34427
35891
  key,
34428
35892
  metadata,
34429
35893
  metrics: {},
35894
+ sessionId,
34430
35895
  span,
34431
35896
  stepsByIndex: /* @__PURE__ */ new Map(),
34432
35897
  turnId: event.data.turnId
@@ -34689,6 +36154,9 @@ var EveBridge = class {
34689
36154
  const normalized = normalizeEveTraceState(current);
34690
36155
  return {
34691
36156
  ...normalized,
36157
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36158
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
36159
+ ),
34692
36160
  stepStarts: normalized.stepStarts.filter(
34693
36161
  (entry) => entry.turnId !== turn.turnId
34694
36162
  )
@@ -34728,6 +36196,7 @@ function emptyEveTraceState() {
34728
36196
  return {
34729
36197
  llmInputs: [],
34730
36198
  metadata: {},
36199
+ reasoningBlocks: [],
34731
36200
  spanReferences: [],
34732
36201
  stepStarts: []
34733
36202
  };
@@ -34778,6 +36247,21 @@ function normalizeEveTraceState(state) {
34778
36247
  const input = entry["input"];
34779
36248
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
34780
36249
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
36250
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
36251
+ if (!isObject(entry)) {
36252
+ return [];
36253
+ }
36254
+ const content = entry["content"];
36255
+ const eventAt = entry["eventAt"];
36256
+ const key = entry["key"];
36257
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
36258
+ {
36259
+ content,
36260
+ ...typeof eventAt === "string" ? { eventAt } : {},
36261
+ key
36262
+ }
36263
+ ] : [];
36264
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
34781
36265
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
34782
36266
  if (!isObject(entry)) {
34783
36267
  return [];
@@ -34788,7 +36272,13 @@ function normalizeEveTraceState(state) {
34788
36272
  const turnId = entry["turnId"];
34789
36273
  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 }] : [];
34790
36274
  }).slice(-MAX_STORED_STEP_STARTS) : [];
34791
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
36275
+ return {
36276
+ llmInputs,
36277
+ metadata: { ...metadata },
36278
+ reasoningBlocks,
36279
+ spanReferences,
36280
+ stepStarts
36281
+ };
34792
36282
  }
34793
36283
  function readEveTraceState(state) {
34794
36284
  try {
@@ -34797,23 +36287,85 @@ function readEveTraceState(state) {
34797
36287
  return emptyEveTraceState();
34798
36288
  }
34799
36289
  }
34800
- function captureEveModelInput(state, input) {
34801
- if (!isObject(input)) {
34802
- return;
34803
- }
34804
- const session = input["session"];
34805
- const turn = input["turn"];
34806
- const step = input["step"];
34807
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
34808
- return;
34809
- }
34810
- const sessionId = session["id"];
34811
- const turnId = turn["id"];
34812
- const stepIndex = step["index"];
34813
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
34814
- return;
36290
+ function storeEveReasoning(state, sessionId, event) {
36291
+ const eventAt = event.meta?.at;
36292
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
36293
+ let stored = [];
36294
+ state.update((current) => {
36295
+ const normalized = normalizeEveTraceState(current);
36296
+ const alreadyStored = normalized.reasoningBlocks.some(
36297
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
36298
+ );
36299
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
36300
+ ...normalized.reasoningBlocks,
36301
+ {
36302
+ content: event.data.reasoning,
36303
+ ...eventAt ? { eventAt } : {},
36304
+ key
36305
+ }
36306
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
36307
+ stored = reasoningBlocks.flatMap(
36308
+ (entry) => entry.key === key ? [
36309
+ {
36310
+ content: entry.content,
36311
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36312
+ }
36313
+ ] : []
36314
+ );
36315
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
36316
+ });
36317
+ return stored;
36318
+ }
36319
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36320
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36321
+ return readEveTraceState(state).reasoningBlocks.flatMap(
36322
+ (entry) => entry.key === key ? [
36323
+ {
36324
+ content: entry.content,
36325
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
36326
+ }
36327
+ ] : []
36328
+ );
36329
+ }
36330
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
36331
+ const key = llmInputKey(sessionId, turnId, stepIndex);
36332
+ state.update((current) => {
36333
+ const normalized = normalizeEveTraceState(current);
36334
+ return {
36335
+ ...normalized,
36336
+ reasoningBlocks: normalized.reasoningBlocks.filter(
36337
+ (entry) => entry.key !== key
36338
+ )
36339
+ };
36340
+ });
36341
+ }
36342
+ function eveOutputMessage(output) {
36343
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
36344
+ }
36345
+ function mergeEveReasoning(output, reasoning) {
36346
+ if (reasoning.length === 0) {
36347
+ return output;
34815
36348
  }
34816
- const captured = capturedModelInput(input["modelInput"]);
36349
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
36350
+ const message = isObject(choice["message"]) ? choice["message"] : {};
36351
+ return [
36352
+ {
36353
+ ...choice,
36354
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
36355
+ message: {
36356
+ ...message,
36357
+ content: "content" in message ? message["content"] : null,
36358
+ reasoning: reasoning.map((block) => ({ content: block.content })),
36359
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
36360
+ }
36361
+ }
36362
+ ];
36363
+ }
36364
+ function captureEveModelInput(state, input) {
36365
+ const sessionId = input.session.id;
36366
+ const turnId = input.turn.id;
36367
+ const stepIndex = input.step.index;
36368
+ const captured = capturedModelInput(input.modelInput);
34817
36369
  if (!captured) {
34818
36370
  return;
34819
36371
  }
@@ -34854,18 +36406,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
34854
36406
  }
34855
36407
  }
34856
36408
  function capturedModelInput(modelInput) {
34857
- if (!isObject(modelInput)) {
34858
- return void 0;
34859
- }
34860
- const messages = modelInput["messages"];
34861
- if (!Array.isArray(messages)) {
34862
- return void 0;
34863
- }
34864
- const instructions = modelInput["instructions"];
34865
- const value = [
34866
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
34867
- ...messages
34868
- ];
36409
+ const { instructions, messages } = modelInput;
36410
+ const value = [];
36411
+ if (typeof instructions === "string") {
36412
+ value.push({ content: instructions, role: "system" });
36413
+ } else if (instructions) {
36414
+ value.push(...instructions.map(capturedEveModelMessage));
36415
+ }
36416
+ value.push(...messages.map(capturedEveModelMessage));
34869
36417
  try {
34870
36418
  const cloned = JSON.parse(JSON.stringify(value));
34871
36419
  if (!Array.isArray(cloned)) {
@@ -34876,8 +36424,123 @@ function capturedModelInput(modelInput) {
34876
36424
  return void 0;
34877
36425
  }
34878
36426
  }
36427
+ function capturedEveModelMessage(message) {
36428
+ const { content, role } = message;
36429
+ if (typeof content === "string") {
36430
+ return { content, role };
36431
+ }
36432
+ return { content: content.map(capturedEveModelContentPart), role };
36433
+ }
36434
+ function capturedEveModelContentPart(part) {
36435
+ switch (part.type) {
36436
+ case "text":
36437
+ case "reasoning":
36438
+ return { text: part.text, type: part.type };
36439
+ case "image":
36440
+ return {
36441
+ image: part.image,
36442
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36443
+ type: "image"
36444
+ };
36445
+ case "file":
36446
+ case "reasoning-file":
36447
+ return {
36448
+ data: part.data,
36449
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
36450
+ mediaType: part.mediaType,
36451
+ type: part.type
36452
+ };
36453
+ case "custom":
36454
+ return {
36455
+ ..."kind" in part ? { kind: part.kind } : {},
36456
+ type: "custom"
36457
+ };
36458
+ case "tool-call":
36459
+ return {
36460
+ input: part.input,
36461
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36462
+ toolCallId: part.toolCallId,
36463
+ toolName: part.toolName,
36464
+ type: "tool-call"
36465
+ };
36466
+ case "tool-result": {
36467
+ const output = part.output;
36468
+ let capturedOutput;
36469
+ switch (output.type) {
36470
+ case "text":
36471
+ case "error-text":
36472
+ capturedOutput = { type: output.type, value: output.value };
36473
+ break;
36474
+ case "json":
36475
+ case "error-json":
36476
+ capturedOutput = { type: output.type, value: output.value };
36477
+ break;
36478
+ case "execution-denied":
36479
+ capturedOutput = {
36480
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
36481
+ type: "execution-denied"
36482
+ };
36483
+ break;
36484
+ case "content":
36485
+ capturedOutput = {
36486
+ type: "content",
36487
+ value: output.value.map(capturedEveModelContentPart)
36488
+ };
36489
+ break;
36490
+ }
36491
+ return {
36492
+ output: capturedOutput,
36493
+ toolCallId: part.toolCallId,
36494
+ toolName: part.toolName,
36495
+ type: "tool-result"
36496
+ };
36497
+ }
36498
+ case "tool-approval-request":
36499
+ return {
36500
+ approvalId: part.approvalId,
36501
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
36502
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
36503
+ toolCallId: part.toolCallId,
36504
+ type: "tool-approval-request"
36505
+ };
36506
+ case "tool-approval-response":
36507
+ return {
36508
+ approvalId: part.approvalId,
36509
+ approved: part.approved,
36510
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
36511
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
36512
+ type: "tool-approval-response"
36513
+ };
36514
+ case "file-data":
36515
+ case "image-data":
36516
+ return {
36517
+ data: part.data,
36518
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
36519
+ mediaType: part.mediaType,
36520
+ type: part.type
36521
+ };
36522
+ case "file-url":
36523
+ case "image-url":
36524
+ return {
36525
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
36526
+ type: part.type,
36527
+ url: part.url
36528
+ };
36529
+ case "file-id":
36530
+ case "image-file-id":
36531
+ return { fileId: part.fileId, type: part.type };
36532
+ case "file-reference":
36533
+ case "image-file-reference":
36534
+ return {
36535
+ providerReference: part.providerReference,
36536
+ type: part.type
36537
+ };
36538
+ }
36539
+ }
34879
36540
  function isCapturedModelInput(input) {
34880
- return Array.isArray(input);
36541
+ return Array.isArray(input) && input.every(
36542
+ (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))
36543
+ );
34881
36544
  }
34882
36545
  function llmInputKey(sessionId, turnId, stepIndex) {
34883
36546
  return `${sessionId}\0${turnId}\0${stepIndex}`;
@@ -35229,17 +36892,20 @@ var BraintrustObservabilityExporter = class {
35229
36892
  }
35230
36893
  onStart(exported) {
35231
36894
  if (this.spans.has(exported.id)) return;
35232
- const args = {
35233
- name: exported.name,
35234
- spanAttributes: { type: spanTypeFor(exported.type) },
35235
- startTime: epochSeconds(exported.startTime),
35236
- // Use the Mastra span id as the Braintrust row id so that
35237
- // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35238
- // attach to the right row. Without this, `SpanImpl` auto-generates a
35239
- // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35240
- // external caller could know.
35241
- event: { id: exported.id }
35242
- };
36895
+ const args = withSpanInstrumentationName(
36896
+ {
36897
+ name: exported.name,
36898
+ spanAttributes: { type: spanTypeFor(exported.type) },
36899
+ startTime: epochSeconds(exported.startTime),
36900
+ // Use the Mastra span id as the Braintrust row id so that
36901
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
36902
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
36903
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
36904
+ // external caller could know.
36905
+ event: { id: exported.id }
36906
+ },
36907
+ INSTRUMENTATION_NAMES.MASTRA
36908
+ );
35243
36909
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
35244
36910
  if (!this.capturedParent) {
35245
36911
  const probe = currentSpan();
@@ -38272,6 +39938,8 @@ var BaseNode = class {
38272
39938
  this.graph = graph;
38273
39939
  this.id = id;
38274
39940
  }
39941
+ graph;
39942
+ id;
38275
39943
  __type = "node";
38276
39944
  dependencies = [];
38277
39945
  addDependency(dependency) {
@@ -38305,6 +39973,7 @@ var PromptNode = class extends BaseNode {
38305
39973
  super(graph, id);
38306
39974
  this.prompt = prompt;
38307
39975
  }
39976
+ prompt;
38308
39977
  async build(context) {
38309
39978
  return {
38310
39979
  type: "function",
@@ -38317,6 +39986,7 @@ var GateNode = class extends BaseNode {
38317
39986
  super(graph, id);
38318
39987
  this.condition = condition;
38319
39988
  }
39989
+ condition;
38320
39990
  async build(context) {
38321
39991
  return {
38322
39992
  type: "gate",
@@ -38341,6 +40011,7 @@ var PromptTemplateNode = class extends BaseNode {
38341
40011
  super(graph, id);
38342
40012
  this.prompt = prompt;
38343
40013
  }
40014
+ prompt;
38344
40015
  async build(context) {
38345
40016
  return {
38346
40017
  type: "prompt_template",
@@ -38353,6 +40024,7 @@ var LiteralNode = class extends BaseNode {
38353
40024
  super(graph, id);
38354
40025
  this.value = value;
38355
40026
  }
40027
+ value;
38356
40028
  async build(context) {
38357
40029
  return {
38358
40030
  type: "literal",
@@ -39446,6 +41118,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
39446
41118
  this._state = _state;
39447
41119
  this.spanTypeFilter = spanTypeFilter;
39448
41120
  }
41121
+ _objectId;
41122
+ rootSpanId;
41123
+ _state;
41124
+ spanTypeFilter;
39449
41125
  static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
39450
41126
  const children = [
39451
41127
  // Base filter: root_span_id = 'value'
@@ -39896,6 +41572,8 @@ var EvalResultWithSummary = class {
39896
41572
  this.summary = summary;
39897
41573
  this.results = results;
39898
41574
  }
41575
+ summary;
41576
+ results;
39899
41577
  /**
39900
41578
  * @deprecated Use `summary` instead.
39901
41579
  */
@@ -40613,26 +42291,30 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40613
42291
  }
40614
42292
  }
40615
42293
  })();
42294
+ let timeoutId;
42295
+ let abortHandler;
42296
+ const cleanupCancellation = () => {
42297
+ if (timeoutId !== void 0) {
42298
+ clearTimeout(timeoutId);
42299
+ timeoutId = void 0;
42300
+ }
42301
+ if (abortHandler && evaluator.signal) {
42302
+ evaluator.signal.removeEventListener("abort", abortHandler);
42303
+ abortHandler = void 0;
42304
+ }
42305
+ };
40616
42306
  const cancel = async () => {
40617
42307
  await new Promise((_, reject2) => {
40618
42308
  if (cancelled) {
40619
42309
  reject2(new InternalAbortError("Evaluator already cancelled"));
40620
42310
  return;
40621
42311
  }
40622
- let timeoutId;
40623
- let abortHandler;
40624
42312
  const rejectOnce = (error) => {
40625
42313
  if (cancelled) {
40626
42314
  return;
40627
42315
  }
40628
42316
  cancelled = true;
40629
- if (timeoutId) {
40630
- clearTimeout(timeoutId);
40631
- timeoutId = void 0;
40632
- }
40633
- if (abortHandler && evaluator.signal) {
40634
- evaluator.signal.removeEventListener("abort", abortHandler);
40635
- }
42317
+ cleanupCancellation();
40636
42318
  reject2(error);
40637
42319
  };
40638
42320
  if (evaluator.timeout) {
@@ -40672,6 +42354,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
40672
42354
  }
40673
42355
  throw e;
40674
42356
  } finally {
42357
+ cleanupCancellation();
40675
42358
  if (!collectResults) {
40676
42359
  collectedResults.length = 0;
40677
42360
  }
@@ -41314,6 +42997,7 @@ var ToolBuilder = class {
41314
42997
  constructor(project) {
41315
42998
  this.project = project;
41316
42999
  }
43000
+ project;
41317
43001
  taskCounter = 0;
41318
43002
  // This type definition is just a catch all so that the implementation can be
41319
43003
  // less specific than the two more specific declarations above.
@@ -41344,6 +43028,7 @@ var ScorerBuilder = class {
41344
43028
  constructor(project) {
41345
43029
  this.project = project;
41346
43030
  }
43031
+ project;
41347
43032
  taskCounter = 0;
41348
43033
  create(opts) {
41349
43034
  this.taskCounter++;
@@ -41403,6 +43088,7 @@ var ClassifierBuilder = class {
41403
43088
  constructor(project) {
41404
43089
  this.project = project;
41405
43090
  }
43091
+ project;
41406
43092
  taskCounter = 0;
41407
43093
  create(opts) {
41408
43094
  this.taskCounter++;
@@ -41438,6 +43124,7 @@ var CodeFunction = class {
41438
43124
  throw new Error("parameters are required if return type is defined");
41439
43125
  }
41440
43126
  }
43127
+ project;
41441
43128
  handler;
41442
43129
  name;
41443
43130
  slug;
@@ -41525,6 +43212,7 @@ var PromptBuilder = class {
41525
43212
  constructor(project) {
41526
43213
  this.project = project;
41527
43214
  }
43215
+ project;
41528
43216
  create(opts) {
41529
43217
  const toolFunctions = [];
41530
43218
  const rawTools = [];
@@ -41601,6 +43289,7 @@ var ParametersBuilder = class {
41601
43289
  constructor(project) {
41602
43290
  this.project = project;
41603
43291
  }
43292
+ project;
41604
43293
  create(opts) {
41605
43294
  const slug = opts.slug ?? slugify(opts.name, { lower: true, strict: true });
41606
43295
  const codeParameters = new CodeParameters(this.project, {