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/cli.js CHANGED
@@ -7,7 +7,11 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
9
  var __commonJS = (cb, mod) => function __require() {
10
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ try {
11
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
+ } catch (e) {
13
+ throw mod = 0, e;
14
+ }
11
15
  };
12
16
  var __export = (target, all) => {
13
17
  for (var name in all)
@@ -1232,7 +1236,7 @@ var require_package = __commonJS({
1232
1236
  "package.json"(exports2, module2) {
1233
1237
  module2.exports = {
1234
1238
  name: "braintrust",
1235
- version: "3.23.1",
1239
+ version: "3.25.0",
1236
1240
  description: "SDK for integrating Braintrust",
1237
1241
  repository: {
1238
1242
  type: "git",
@@ -1430,7 +1434,7 @@ var require_package = __commonJS({
1430
1434
  "eslint-plugin-node-import": "^1.0.5",
1431
1435
  openai: "6.25.0",
1432
1436
  rollup: "^4.60.3",
1433
- tar: "^7.5.16",
1437
+ tar: "^7.5.19",
1434
1438
  tinybench: "^4.0.1",
1435
1439
  "ts-declaration-location": "^1.0.7",
1436
1440
  tsup: "^8.5.1",
@@ -1438,7 +1442,7 @@ var require_package = __commonJS({
1438
1442
  typedoc: "^0.25.13",
1439
1443
  "typedoc-plugin-markdown": "^3.17.1",
1440
1444
  typescript: "5.4.4",
1441
- vite: "^6.4.3",
1445
+ vite: "^8.1.5",
1442
1446
  "vite-tsconfig-paths": "^4.3.2",
1443
1447
  vitest: "4.1.5",
1444
1448
  webpack: "^5.106.2",
@@ -1458,7 +1462,7 @@ var require_package = __commonJS({
1458
1462
  cors: "^2.8.5",
1459
1463
  "dc-browser": "^1.0.4",
1460
1464
  dotenv: "^16.4.5",
1461
- esbuild: "0.28.0",
1465
+ esbuild: "0.28.1",
1462
1466
  esquery: "^1.7.0",
1463
1467
  "eventsource-parser": "^1.1.2",
1464
1468
  express: "^5.2.1",
@@ -1537,6 +1541,7 @@ var DefaultChannel = class {
1537
1541
  constructor(name) {
1538
1542
  this.name = name;
1539
1543
  }
1544
+ name;
1540
1545
  hasSubscribers = false;
1541
1546
  subscribe(_subscription) {
1542
1547
  }
@@ -2586,6 +2591,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
2586
2591
  constructor(data) {
2587
2592
  this.data = data;
2588
2593
  }
2594
+ data;
2589
2595
  toStr() {
2590
2596
  const jsonObj = {
2591
2597
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -3104,6 +3110,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
3104
3110
  constructor(data) {
3105
3111
  this.data = data;
3106
3112
  }
3113
+ data;
3107
3114
  toStr() {
3108
3115
  const jsonObj = {
3109
3116
  compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
@@ -6353,6 +6360,144 @@ var SpanCache = class {
6353
6360
  }
6354
6361
  };
6355
6362
 
6363
+ // src/span-origin.ts
6364
+ var INSTRUMENTATION_NAMES = {
6365
+ AI_SDK: "ai-sdk",
6366
+ ANTHROPIC: "anthropic",
6367
+ BEDROCK_RUNTIME: "bedrock-runtime",
6368
+ BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
6369
+ CLAUDE_AGENT_SDK: "claude-agent-sdk",
6370
+ COHERE: "cohere",
6371
+ CURSOR_SDK: "cursor-sdk",
6372
+ EVE: "eve",
6373
+ FLUE: "flue",
6374
+ GENKIT: "genkit",
6375
+ GITHUB_COPILOT: "github-copilot",
6376
+ GOOGLE_ADK: "google-adk",
6377
+ GOOGLE_GENAI: "google-genai",
6378
+ GROQ: "groq",
6379
+ HUGGINGFACE: "huggingface",
6380
+ LANGCHAIN: "langchain",
6381
+ LANGSMITH: "langsmith",
6382
+ MASTRA: "mastra",
6383
+ MISTRAL: "mistral",
6384
+ OPENAI: "openai",
6385
+ OPENAI_AGENTS: "openai-agents",
6386
+ OPENAI_CODEX: "openai-codex",
6387
+ OPENROUTER: "openrouter",
6388
+ OPENROUTER_AGENT: "openrouter-agent",
6389
+ PI_CODING_AGENT: "pi-coding-agent",
6390
+ STRANDS_AGENT_SDK: "strands-agent-sdk"
6391
+ };
6392
+ var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
6393
+ "braintrust.spanInstrumentationName"
6394
+ );
6395
+ var SDK_VERSION = true ? "3.25.0" : "0.0.0";
6396
+ function withSpanInstrumentationName(args, instrumentationName) {
6397
+ return {
6398
+ ...args,
6399
+ [INTERNAL_SPAN_INSTRUMENTATION_NAME]: instrumentationName
6400
+ };
6401
+ }
6402
+ function getSpanInstrumentationName(args) {
6403
+ if (typeof args !== "object" || args === null) {
6404
+ return void 0;
6405
+ }
6406
+ const value = args[INTERNAL_SPAN_INSTRUMENTATION_NAME];
6407
+ return isSpanInstrumentationName(value) ? value : void 0;
6408
+ }
6409
+ function detectSpanOriginEnvironment(explicit) {
6410
+ if (explicit) return explicit;
6411
+ const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
6412
+ const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
6413
+ if (envType || envName) {
6414
+ return {
6415
+ ...envType ? { type: envType } : {},
6416
+ ...envName ? { name: envName } : {}
6417
+ };
6418
+ }
6419
+ const ci = firstPresent([
6420
+ ["GITHUB_ACTIONS", "github_actions"],
6421
+ ["GITLAB_CI", "gitlab_ci"],
6422
+ ["CIRCLECI", "circleci"],
6423
+ ["BUILDKITE", "buildkite"],
6424
+ ["JENKINS_URL", "jenkins"],
6425
+ ["JENKINS_HOME", "jenkins"],
6426
+ ["TF_BUILD", "azure_pipelines"],
6427
+ ["TEAMCITY_VERSION", "teamcity"],
6428
+ ["TRAVIS", "travis"],
6429
+ ["BITBUCKET_BUILD_NUMBER", "bitbucket"]
6430
+ ]);
6431
+ if (ci) return { type: "ci", name: ci };
6432
+ if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
6433
+ const earlyServer = firstPresent([
6434
+ ["VERCEL", "vercel"],
6435
+ ["NETLIFY", "netlify"]
6436
+ ]);
6437
+ if (earlyServer) return { type: "server", name: earlyServer };
6438
+ if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
6439
+ return { type: "server", name: "ecs" };
6440
+ }
6441
+ const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
6442
+ if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
6443
+ return { type: "server", name: "ecs" };
6444
+ }
6445
+ if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
6446
+ return { type: "server", name: "aws_lambda" };
6447
+ }
6448
+ if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
6449
+ return { type: "server", name: "aws_lambda" };
6450
+ }
6451
+ const server = firstPresent([
6452
+ ["K_SERVICE", "cloud_run"],
6453
+ ["FUNCTION_TARGET", "gcp_functions"],
6454
+ ["KUBERNETES_SERVICE_HOST", "kubernetes"],
6455
+ ["DYNO", "heroku"],
6456
+ ["FLY_APP_NAME", "fly"],
6457
+ ["RAILWAY_ENVIRONMENT", "railway"],
6458
+ ["RENDER_SERVICE_NAME", "render"]
6459
+ ]);
6460
+ if (server) return { type: "server", name: server };
6461
+ return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
6462
+ }
6463
+ function makeSpanOrigin(instrumentationName, environment) {
6464
+ return {
6465
+ name: "braintrust.sdk.javascript",
6466
+ version: SDK_VERSION,
6467
+ instrumentation: { name: instrumentationName },
6468
+ ...environment ? { environment } : {}
6469
+ };
6470
+ }
6471
+ function mergeSpanOriginContext(context2, instrumentationName, environment) {
6472
+ const next = { ...context2 ?? {} };
6473
+ const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
6474
+ next.span_origin = {
6475
+ ...makeSpanOrigin(instrumentationName, environment),
6476
+ ...current
6477
+ };
6478
+ return next;
6479
+ }
6480
+ function isSpanInstrumentationName(value) {
6481
+ return Object.values(INSTRUMENTATION_NAMES).some((name) => name === value);
6482
+ }
6483
+ function firstPresent(entries) {
6484
+ return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
6485
+ }
6486
+ function deploymentModeEnvironment(_key, value) {
6487
+ if (!value) return void 0;
6488
+ const normalized = value.toLowerCase();
6489
+ if (normalized === "production" || normalized === "staging") {
6490
+ return { type: "server", name: normalized };
6491
+ }
6492
+ if (normalized === "development" || normalized === "local") {
6493
+ return { type: "local", name: normalized };
6494
+ }
6495
+ return { name: value };
6496
+ }
6497
+ function isObject2(value) {
6498
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6499
+ }
6500
+
6356
6501
  // src/logger.ts
6357
6502
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
6358
6503
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
@@ -6401,6 +6546,7 @@ var LoginInvalidOrgError = class extends Error {
6401
6546
  super(message);
6402
6547
  this.message = message;
6403
6548
  }
6549
+ message;
6404
6550
  };
6405
6551
  var REDACTION_FIELDS = [
6406
6552
  "input",
@@ -6416,6 +6562,8 @@ var MaskingError = class {
6416
6562
  this.fieldName = fieldName;
6417
6563
  this.errorType = errorType;
6418
6564
  }
6565
+ fieldName;
6566
+ errorType;
6419
6567
  get errorMsg() {
6420
6568
  return `ERROR: Failed to mask field '${this.fieldName}' - ${this.errorType}`;
6421
6569
  }
@@ -6625,7 +6773,10 @@ var BraintrustState = class _BraintrustState {
6625
6773
  diskCache: parametersDiskCache
6626
6774
  });
6627
6775
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
6776
+ this.spanOriginEnvironment = detectSpanOriginEnvironment();
6777
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
6628
6778
  }
6779
+ loginParams;
6629
6780
  id;
6630
6781
  currentExperiment;
6631
6782
  // Note: the value of IsAsyncFlush doesn't really matter here, since we
@@ -6660,6 +6811,19 @@ var BraintrustState = class _BraintrustState {
6660
6811
  _idGenerator = null;
6661
6812
  _contextManager = null;
6662
6813
  _otelFlushCallback = null;
6814
+ spanOriginEnvironment;
6815
+ traceContextSigningSecret;
6816
+ /** @internal */
6817
+ _internalSetTraceContextSigningSecret(secret) {
6818
+ const normalizedSecret = secret?.trim();
6819
+ if (normalizedSecret) {
6820
+ this.traceContextSigningSecret = normalizedSecret;
6821
+ }
6822
+ }
6823
+ /** @internal */
6824
+ _internalGetTraceContextSigningSecret() {
6825
+ return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
6826
+ }
6663
6827
  resetLoginInfo() {
6664
6828
  this.appUrl = null;
6665
6829
  this.appPublicUrl = null;
@@ -6720,6 +6884,7 @@ var BraintrustState = class _BraintrustState {
6720
6884
  this.gitMetadataSettings = other.gitMetadataSettings;
6721
6885
  this.debugLogLevel = other.debugLogLevel;
6722
6886
  this.debugLogLevelConfigured = other.debugLogLevelConfigured;
6887
+ this.traceContextSigningSecret = other.traceContextSigningSecret;
6723
6888
  setGlobalDebugLogLevel(
6724
6889
  this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
6725
6890
  );
@@ -6807,6 +6972,7 @@ var BraintrustState = class _BraintrustState {
6807
6972
  return this.debugLogLevelConfigured;
6808
6973
  }
6809
6974
  async login(loginParams) {
6975
+ this._internalSetTraceContextSigningSecret(loginParams.apiKey);
6810
6976
  this.setDebugLogLevel(loginParams.debugLogLevel);
6811
6977
  if (this.apiUrl && !loginParams.forceLogin) {
6812
6978
  return;
@@ -7471,6 +7637,28 @@ function updateSpanImpl({
7471
7637
  }));
7472
7638
  state.bgLogger().log([record]);
7473
7639
  }
7640
+ function updateSpan({
7641
+ exported,
7642
+ state,
7643
+ ...event
7644
+ }) {
7645
+ const resolvedState = state ?? _globalState;
7646
+ const components = SpanComponentsV4.fromStr(exported);
7647
+ if (!components.data.row_id) {
7648
+ throw new Error("Exported span must have a row id");
7649
+ }
7650
+ updateSpanImpl({
7651
+ state: resolvedState,
7652
+ parentObjectType: components.data.object_type,
7653
+ parentObjectId: new LazyValue(
7654
+ spanComponentsToObjectIdLambda(resolvedState, components)
7655
+ ),
7656
+ id: components.data.row_id,
7657
+ root_span_id: components.data.root_span_id,
7658
+ span_id: components.data.span_id,
7659
+ event
7660
+ });
7661
+ }
7474
7662
  function spanComponentsToObjectIdLambda(state, components) {
7475
7663
  if (components.data.object_id) {
7476
7664
  const ret = components.data.object_id;
@@ -8557,6 +8745,7 @@ function init(projectOrOptions, optionalOptions) {
8557
8745
  throw new Error("Cannot open and update an experiment at the same time");
8558
8746
  }
8559
8747
  const state = stateArg ?? _globalState;
8748
+ state._internalSetTraceContextSigningSecret(apiKey);
8560
8749
  state.enforceQueueSizeLimit(false);
8561
8750
  if (open2) {
8562
8751
  if (isEmpty2(experiment)) {
@@ -9013,6 +9202,7 @@ function initLogger(options = {}) {
9013
9202
  orgName,
9014
9203
  forceLogin,
9015
9204
  debugLogLevel,
9205
+ environment,
9016
9206
  fetch: fetch2,
9017
9207
  state: stateArg
9018
9208
  } = options || {};
@@ -9028,7 +9218,9 @@ function initLogger(options = {}) {
9028
9218
  project_id: projectId
9029
9219
  };
9030
9220
  const state = stateArg ?? _globalState;
9221
+ state._internalSetTraceContextSigningSecret(apiKey);
9031
9222
  state.setDebugLogLevel(debugLogLevel);
9223
+ state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
9032
9224
  state.enforceQueueSizeLimit(true);
9033
9225
  const lazyMetadata = new LazyValue(
9034
9226
  async () => {
@@ -9456,6 +9648,12 @@ function traced(callback, args) {
9456
9648
  function startSpan(args) {
9457
9649
  return startSpanAndIsLogger(args).span;
9458
9650
  }
9651
+ function _internalStartSpanWithInitialMerge(args) {
9652
+ return startSpanAndIsLogger({
9653
+ ...args,
9654
+ [INITIAL_SPAN_WRITE_AS_MERGE]: true
9655
+ }).span;
9656
+ }
9459
9657
  async function flush(options) {
9460
9658
  const state = options?.state ?? _globalState;
9461
9659
  return await state.bgLogger().flush();
@@ -9607,7 +9805,9 @@ function deepCopyEvent(event) {
9607
9805
  const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
9608
9806
  const attachmentMarker = ++deepCopyEventMarkerCounter;
9609
9807
  const serialized = JSON.stringify(event, (_k, v) => {
9610
- if (v instanceof SpanImpl || v instanceof NoopSpan) {
9808
+ if (v instanceof Error) {
9809
+ return v.message;
9810
+ } else if (v instanceof SpanImpl || v instanceof NoopSpan) {
9611
9811
  return `<span>`;
9612
9812
  } else if (v instanceof Experiment2) {
9613
9813
  return `<experiment>`;
@@ -9737,6 +9937,11 @@ var ObjectFetcher = class {
9737
9937
  this._internal_btql = _internal_btql;
9738
9938
  this._internalBrainstoreRealtime = _internalBrainstoreRealtime;
9739
9939
  }
9940
+ objectType;
9941
+ pinnedVersion;
9942
+ mutateRecord;
9943
+ _internal_btql;
9944
+ _internalBrainstoreRealtime;
9740
9945
  _fetchedData = void 0;
9741
9946
  get id() {
9742
9947
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
@@ -10175,6 +10380,8 @@ var ReadonlyExperiment = class extends ObjectFetcher {
10175
10380
  this.state = state;
10176
10381
  this.lazyMetadata = lazyMetadata;
10177
10382
  }
10383
+ state;
10384
+ lazyMetadata;
10178
10385
  get id() {
10179
10386
  return (async () => {
10180
10387
  return (await this.lazyMetadata.get()).experiment.id;
@@ -10268,6 +10475,7 @@ var SpanImpl = class _SpanImpl {
10268
10475
  constructor(args) {
10269
10476
  this._state = args.state;
10270
10477
  this._propagatedState = args.propagatedState;
10478
+ const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
10271
10479
  const spanAttributes = args.spanAttributes ?? {};
10272
10480
  const rawEvent = args.event ?? {};
10273
10481
  const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
@@ -10297,7 +10505,11 @@ var SpanImpl = class _SpanImpl {
10297
10505
  metrics: {
10298
10506
  start: args.startTime ?? getCurrentUnixTimestamp()
10299
10507
  },
10300
- context: { ...callerLocation },
10508
+ context: mergeSpanOriginContext(
10509
+ { ...callerLocation },
10510
+ instrumentationName,
10511
+ this._state.spanOriginEnvironment
10512
+ ),
10301
10513
  span_attributes: {
10302
10514
  name,
10303
10515
  type,
@@ -10671,6 +10883,7 @@ var Dataset2 = class extends ObjectFetcher {
10671
10883
  this.pinnedEnvironment = pinState?.pinnedEnvironment;
10672
10884
  this.pinnedSnapshotName = pinState?.pinnedSnapshotName;
10673
10885
  }
10886
+ state;
10674
10887
  lazyMetadata;
10675
10888
  __braintrust_dataset_marker = true;
10676
10889
  newRecords = 0;
@@ -11182,6 +11395,9 @@ var Prompt2 = class _Prompt {
11182
11395
  this.noTrace = noTrace;
11183
11396
  void this.__braintrust_prompt_marker;
11184
11397
  }
11398
+ metadata;
11399
+ defaults;
11400
+ noTrace;
11185
11401
  parsedPromptData;
11186
11402
  hasParsedPromptData = false;
11187
11403
  __braintrust_prompt_marker = true;
@@ -11419,6 +11635,7 @@ var RemoteEvalParameters = class {
11419
11635
  this.metadata = metadata;
11420
11636
  void this.__braintrust_parameters_marker;
11421
11637
  }
11638
+ metadata;
11422
11639
  __braintrust_parameters_marker = true;
11423
11640
  get id() {
11424
11641
  return this.metadata.id;
@@ -12663,6 +12880,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
12663
12880
  this._state = _state;
12664
12881
  this.spanTypeFilter = spanTypeFilter;
12665
12882
  }
12883
+ _objectId;
12884
+ rootSpanId;
12885
+ _state;
12886
+ spanTypeFilter;
12666
12887
  static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
12667
12888
  const children = [
12668
12889
  // Base filter: root_span_id = 'value'
@@ -13097,6 +13318,8 @@ var EvalResultWithSummary = class {
13097
13318
  this.summary = summary;
13098
13319
  this.results = results;
13099
13320
  }
13321
+ summary;
13322
+ results;
13100
13323
  /**
13101
13324
  * @deprecated Use `summary` instead.
13102
13325
  */
@@ -13821,26 +14044,30 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13821
14044
  }
13822
14045
  }
13823
14046
  })();
14047
+ let timeoutId;
14048
+ let abortHandler;
14049
+ const cleanupCancellation = () => {
14050
+ if (timeoutId !== void 0) {
14051
+ clearTimeout(timeoutId);
14052
+ timeoutId = void 0;
14053
+ }
14054
+ if (abortHandler && evaluator.signal) {
14055
+ evaluator.signal.removeEventListener("abort", abortHandler);
14056
+ abortHandler = void 0;
14057
+ }
14058
+ };
13824
14059
  const cancel = async () => {
13825
14060
  await new Promise((_, reject2) => {
13826
14061
  if (cancelled) {
13827
14062
  reject2(new InternalAbortError("Evaluator already cancelled"));
13828
14063
  return;
13829
14064
  }
13830
- let timeoutId;
13831
- let abortHandler;
13832
14065
  const rejectOnce = (error2) => {
13833
14066
  if (cancelled) {
13834
14067
  return;
13835
14068
  }
13836
14069
  cancelled = true;
13837
- if (timeoutId) {
13838
- clearTimeout(timeoutId);
13839
- timeoutId = void 0;
13840
- }
13841
- if (abortHandler && evaluator.signal) {
13842
- evaluator.signal.removeEventListener("abort", abortHandler);
13843
- }
14070
+ cleanupCancellation();
13844
14071
  reject2(error2);
13845
14072
  };
13846
14073
  if (evaluator.timeout) {
@@ -13880,6 +14107,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13880
14107
  }
13881
14108
  throw e;
13882
14109
  } finally {
14110
+ cleanupCancellation();
13883
14111
  if (!collectResults) {
13884
14112
  collectedResults.length = 0;
13885
14113
  }
@@ -15264,15 +15492,25 @@ function hasChoices(value) {
15264
15492
  function normalizeMetadata(metadata) {
15265
15493
  return isObject(metadata) ? metadata : void 0;
15266
15494
  }
15267
- function startSpanForEvent(config3, event, channelName) {
15495
+ function startSpanForEvent(config3, event, channelName, instrumentationName) {
15268
15496
  const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
15269
15497
  config3,
15270
15498
  event
15271
15499
  );
15272
- const span = startSpan({
15273
- name,
15274
- spanAttributes
15275
- });
15500
+ const spanArgs = withSpanInstrumentationName(
15501
+ {
15502
+ name,
15503
+ spanAttributes
15504
+ },
15505
+ instrumentationName
15506
+ );
15507
+ let span;
15508
+ try {
15509
+ span = config3.startSpan?.(spanArgs) ?? startSpan(spanArgs);
15510
+ } catch (error2) {
15511
+ debugLogger.error(`Error starting span for ${channelName}:`, error2);
15512
+ span = startSpan(spanArgs);
15513
+ }
15276
15514
  const startTime = getCurrentUnixTimestamp();
15277
15515
  try {
15278
15516
  const { input, metadata } = config3.extractInput(
@@ -15303,7 +15541,7 @@ function shouldTraceEvent(config3, event, channelName) {
15303
15541
  return true;
15304
15542
  }
15305
15543
  }
15306
- function ensureSpanStateForEvent(states, config3, event, channelName) {
15544
+ function ensureSpanStateForEvent(states, config3, event, channelName, instrumentationName) {
15307
15545
  const key = event;
15308
15546
  const existing = states.get(key);
15309
15547
  if (existing) {
@@ -15312,11 +15550,16 @@ function ensureSpanStateForEvent(states, config3, event, channelName) {
15312
15550
  if (!shouldTraceEvent(config3, event, channelName)) {
15313
15551
  return void 0;
15314
15552
  }
15315
- const created = startSpanForEvent(config3, event, channelName);
15553
+ const created = startSpanForEvent(
15554
+ config3,
15555
+ event,
15556
+ channelName,
15557
+ instrumentationName
15558
+ );
15316
15559
  states.set(key, created);
15317
15560
  return created;
15318
15561
  }
15319
- function bindCurrentSpanStoreToStart(tracingChannel2, states, config3, channelName) {
15562
+ function bindCurrentSpanStoreToStart(tracingChannel2, states, config3, channelName, instrumentationName) {
15320
15563
  const state = _internalGetGlobalState();
15321
15564
  const startChannel = tracingChannel2.start;
15322
15565
  const contextManager = state?.contextManager;
@@ -15334,7 +15577,8 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config3, channelNa
15334
15577
  states,
15335
15578
  config3,
15336
15579
  event,
15337
- channelName
15580
+ channelName,
15581
+ instrumentationName
15338
15582
  );
15339
15583
  return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
15340
15584
  }
@@ -15343,15 +15587,21 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config3, channelNa
15343
15587
  startChannel.unbindStore(currentSpanStore);
15344
15588
  };
15345
15589
  }
15346
- function logErrorAndEnd(states, event) {
15590
+ function logErrorAndEnd(states, event, channelName) {
15347
15591
  const spanData = states.get(event);
15348
15592
  if (!spanData) {
15349
15593
  return;
15350
15594
  }
15351
- spanData.span.log({
15352
- error: event.error.message
15353
- });
15354
- spanData.span.end();
15595
+ try {
15596
+ spanData.span.log({ error: event.error });
15597
+ } catch (error2) {
15598
+ debugLogger.error(`Error logging failure for ${channelName}:`, error2);
15599
+ }
15600
+ try {
15601
+ spanData.span.end();
15602
+ } catch (error2) {
15603
+ debugLogger.error(`Error ending span for ${channelName}:`, error2);
15604
+ }
15355
15605
  states.delete(event);
15356
15606
  }
15357
15607
  function runStreamingCompletionHook(args) {
@@ -15401,7 +15651,8 @@ function traceAsyncChannel(channel2, config3) {
15401
15651
  tracingChannel2,
15402
15652
  states,
15403
15653
  config3,
15404
- channelName
15654
+ channelName,
15655
+ channel2.instrumentationName
15405
15656
  );
15406
15657
  const handlers = {
15407
15658
  start: (event) => {
@@ -15412,7 +15663,8 @@ function traceAsyncChannel(channel2, config3) {
15412
15663
  states,
15413
15664
  config3,
15414
15665
  event,
15415
- channelName
15666
+ channelName,
15667
+ channel2.instrumentationName
15416
15668
  );
15417
15669
  },
15418
15670
  asyncEnd: (event) => {
@@ -15449,7 +15701,7 @@ function traceAsyncChannel(channel2, config3) {
15449
15701
  }
15450
15702
  },
15451
15703
  error: (event) => {
15452
- logErrorAndEnd(states, event);
15704
+ logErrorAndEnd(states, event, channelName);
15453
15705
  }
15454
15706
  };
15455
15707
  tracingChannel2.subscribe(handlers);
@@ -15466,7 +15718,8 @@ function traceStreamingChannel(channel2, config3) {
15466
15718
  tracingChannel2,
15467
15719
  states,
15468
15720
  config3,
15469
- channelName
15721
+ channelName,
15722
+ channel2.instrumentationName
15470
15723
  );
15471
15724
  const handlers = {
15472
15725
  start: (event) => {
@@ -15477,7 +15730,8 @@ function traceStreamingChannel(channel2, config3) {
15477
15730
  states,
15478
15731
  config3,
15479
15732
  event,
15480
- channelName
15733
+ channelName,
15734
+ channel2.instrumentationName
15481
15735
  );
15482
15736
  },
15483
15737
  asyncEnd: (event) => {
@@ -15496,6 +15750,7 @@ function traceStreamingChannel(channel2, config3) {
15496
15750
  }
15497
15751
  },
15498
15752
  onComplete: (chunks) => {
15753
+ let completion2;
15499
15754
  try {
15500
15755
  let output;
15501
15756
  let metrics;
@@ -15526,18 +15781,11 @@ function traceStreamingChannel(channel2, config3) {
15526
15781
  } else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
15527
15782
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
15528
15783
  }
15529
- runStreamingCompletionHook({
15530
- channelName,
15531
- chunks,
15532
- config: config3,
15533
- endEvent: asyncEndEvent,
15784
+ completion2 = {
15534
15785
  ...metadata !== void 0 ? { metadata } : {},
15535
15786
  metrics,
15536
- output,
15537
- result: asyncEndEvent.result,
15538
- span,
15539
- startTime
15540
- });
15787
+ output
15788
+ };
15541
15789
  span.log({
15542
15790
  output,
15543
15791
  ...metadata !== void 0 ? { metadata } : {},
@@ -15549,11 +15797,49 @@ function traceStreamingChannel(channel2, config3) {
15549
15797
  error2
15550
15798
  );
15551
15799
  } finally {
15552
- span.end();
15800
+ try {
15801
+ span.end();
15802
+ } catch (error2) {
15803
+ debugLogger.error(
15804
+ `Error ending span for ${channelName}:`,
15805
+ error2
15806
+ );
15807
+ }
15553
15808
  states.delete(event);
15554
15809
  }
15810
+ if (completion2) {
15811
+ runStreamingCompletionHook({
15812
+ channelName,
15813
+ chunks,
15814
+ config: config3,
15815
+ endEvent: asyncEndEvent,
15816
+ ...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
15817
+ metrics: completion2.metrics,
15818
+ output: completion2.output,
15819
+ result: asyncEndEvent.result,
15820
+ span,
15821
+ startTime
15822
+ });
15823
+ }
15555
15824
  },
15556
15825
  onError: (error2) => {
15826
+ try {
15827
+ span.log({ error: error2 });
15828
+ } catch (loggingError) {
15829
+ debugLogger.error(
15830
+ `Error logging failure for ${channelName}:`,
15831
+ loggingError
15832
+ );
15833
+ }
15834
+ try {
15835
+ span.end();
15836
+ } catch (endingError) {
15837
+ debugLogger.error(
15838
+ `Error ending span for ${channelName}:`,
15839
+ endingError
15840
+ );
15841
+ }
15842
+ states.delete(event);
15557
15843
  runStreamingErrorHook({
15558
15844
  channelName,
15559
15845
  config: config3,
@@ -15562,11 +15848,6 @@ function traceStreamingChannel(channel2, config3) {
15562
15848
  span,
15563
15849
  startTime
15564
15850
  });
15565
- span.log({
15566
- error: error2.message
15567
- });
15568
- span.end();
15569
- states.delete(event);
15570
15851
  }
15571
15852
  });
15572
15853
  return;
@@ -15581,6 +15862,7 @@ function traceStreamingChannel(channel2, config3) {
15581
15862
  states.delete(event);
15582
15863
  return;
15583
15864
  }
15865
+ let completion;
15584
15866
  try {
15585
15867
  const output = config3.extractOutput(
15586
15868
  asyncEndEvent.result,
@@ -15595,17 +15877,11 @@ function traceStreamingChannel(channel2, config3) {
15595
15877
  asyncEndEvent.result,
15596
15878
  asyncEndEvent
15597
15879
  );
15598
- runStreamingCompletionHook({
15599
- channelName,
15600
- config: config3,
15601
- endEvent: asyncEndEvent,
15880
+ completion = {
15602
15881
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
15603
15882
  metrics,
15604
- output,
15605
- result: asyncEndEvent.result,
15606
- span,
15607
- startTime
15608
- });
15883
+ output
15884
+ };
15609
15885
  span.log({
15610
15886
  output,
15611
15887
  ...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
@@ -15614,12 +15890,30 @@ function traceStreamingChannel(channel2, config3) {
15614
15890
  } catch (error2) {
15615
15891
  debugLogger.error(`Error extracting output for ${channelName}:`, error2);
15616
15892
  } finally {
15617
- span.end();
15893
+ try {
15894
+ span.end();
15895
+ } catch (error2) {
15896
+ debugLogger.error(`Error ending span for ${channelName}:`, error2);
15897
+ }
15618
15898
  states.delete(event);
15619
15899
  }
15900
+ if (completion) {
15901
+ runStreamingCompletionHook({
15902
+ channelName,
15903
+ config: config3,
15904
+ endEvent: asyncEndEvent,
15905
+ ...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
15906
+ metrics: completion.metrics,
15907
+ output: completion.output,
15908
+ result: asyncEndEvent.result,
15909
+ span,
15910
+ startTime
15911
+ });
15912
+ }
15620
15913
  },
15621
15914
  error: (event) => {
15622
15915
  const spanData = states.get(event);
15916
+ logErrorAndEnd(states, event, channelName);
15623
15917
  if (spanData) {
15624
15918
  runStreamingErrorHook({
15625
15919
  channelName,
@@ -15630,7 +15924,6 @@ function traceStreamingChannel(channel2, config3) {
15630
15924
  startTime: spanData.startTime
15631
15925
  });
15632
15926
  }
15633
- logErrorAndEnd(states, event);
15634
15927
  }
15635
15928
  };
15636
15929
  tracingChannel2.subscribe(handlers);
@@ -15647,7 +15940,8 @@ function traceSyncStreamChannel(channel2, config3) {
15647
15940
  tracingChannel2,
15648
15941
  states,
15649
15942
  config3,
15650
- channelName
15943
+ channelName,
15944
+ channel2.instrumentationName
15651
15945
  );
15652
15946
  const handlers = {
15653
15947
  start: (event) => {
@@ -15658,7 +15952,8 @@ function traceSyncStreamChannel(channel2, config3) {
15658
15952
  states,
15659
15953
  config3,
15660
15954
  event,
15661
- channelName
15955
+ channelName,
15956
+ channel2.instrumentationName
15662
15957
  );
15663
15958
  },
15664
15959
  end: (event) => {
@@ -15752,7 +16047,7 @@ function traceSyncStreamChannel(channel2, config3) {
15752
16047
  handleResolvedResult(endEvent.result);
15753
16048
  },
15754
16049
  error: (event) => {
15755
- logErrorAndEnd(states, event);
16050
+ logErrorAndEnd(states, event, channelName);
15756
16051
  }
15757
16052
  };
15758
16053
  tracingChannel2.subscribe(handlers);
@@ -15931,7 +16226,8 @@ function processInputAttachments(input) {
15931
16226
  function channel(spec) {
15932
16227
  return spec;
15933
16228
  }
15934
- function defineChannels(pkg, channels) {
16229
+ function defineChannels(pkg, channels, options) {
16230
+ const { instrumentationName } = options;
15935
16231
  return Object.fromEntries(
15936
16232
  Object.entries(channels).map(([key, spec]) => {
15937
16233
  const fullChannelName = `orchestrion:${pkg}:${spec.channelName}`;
@@ -15944,6 +16240,7 @@ function defineChannels(pkg, channels) {
15944
16240
  key,
15945
16241
  {
15946
16242
  ...asyncSpec,
16243
+ instrumentationName,
15947
16244
  tracingChannel: tracingChannel3,
15948
16245
  tracePromise: (fn, context2) => tracingChannel3().tracePromise(
15949
16246
  fn,
@@ -15961,6 +16258,7 @@ function defineChannels(pkg, channels) {
15961
16258
  key,
15962
16259
  {
15963
16260
  ...syncSpec,
16261
+ instrumentationName,
15964
16262
  tracingChannel: tracingChannel2,
15965
16263
  traceSync: (fn, context2) => tracingChannel2().traceSync(
15966
16264
  fn,
@@ -15974,44 +16272,48 @@ function defineChannels(pkg, channels) {
15974
16272
  }
15975
16273
 
15976
16274
  // src/instrumentation/plugins/openai-channels.ts
15977
- var openAIChannels = defineChannels("openai", {
15978
- chatCompletionsCreate: channel({
15979
- channelName: "chat.completions.create",
15980
- kind: "async"
15981
- }),
15982
- embeddingsCreate: channel({
15983
- channelName: "embeddings.create",
15984
- kind: "async"
15985
- }),
15986
- betaChatCompletionsParse: channel({
15987
- channelName: "beta.chat.completions.parse",
15988
- kind: "async"
15989
- }),
15990
- betaChatCompletionsStream: channel({
15991
- channelName: "beta.chat.completions.stream",
15992
- kind: "sync-stream"
15993
- }),
15994
- moderationsCreate: channel({
15995
- channelName: "moderations.create",
15996
- kind: "async"
15997
- }),
15998
- responsesCreate: channel({
15999
- channelName: "responses.create",
16000
- kind: "async"
16001
- }),
16002
- responsesStream: channel({
16003
- channelName: "responses.stream",
16004
- kind: "sync-stream"
16005
- }),
16006
- responsesParse: channel({
16007
- channelName: "responses.parse",
16008
- kind: "async"
16009
- }),
16010
- responsesCompact: channel({
16011
- channelName: "responses.compact",
16012
- kind: "async"
16013
- })
16014
- });
16275
+ var openAIChannels = defineChannels(
16276
+ "openai",
16277
+ {
16278
+ chatCompletionsCreate: channel({
16279
+ channelName: "chat.completions.create",
16280
+ kind: "async"
16281
+ }),
16282
+ embeddingsCreate: channel({
16283
+ channelName: "embeddings.create",
16284
+ kind: "async"
16285
+ }),
16286
+ betaChatCompletionsParse: channel({
16287
+ channelName: "beta.chat.completions.parse",
16288
+ kind: "async"
16289
+ }),
16290
+ betaChatCompletionsStream: channel({
16291
+ channelName: "beta.chat.completions.stream",
16292
+ kind: "sync-stream"
16293
+ }),
16294
+ moderationsCreate: channel({
16295
+ channelName: "moderations.create",
16296
+ kind: "async"
16297
+ }),
16298
+ responsesCreate: channel({
16299
+ channelName: "responses.create",
16300
+ kind: "async"
16301
+ }),
16302
+ responsesStream: channel({
16303
+ channelName: "responses.stream",
16304
+ kind: "sync-stream"
16305
+ }),
16306
+ responsesParse: channel({
16307
+ channelName: "responses.parse",
16308
+ kind: "async"
16309
+ }),
16310
+ responsesCompact: channel({
16311
+ channelName: "responses.compact",
16312
+ kind: "async"
16313
+ })
16314
+ },
16315
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI }
16316
+ );
16015
16317
 
16016
16318
  // src/openai-utils.ts
16017
16319
  var BRAINTRUST_CACHED_STREAM_METRIC = "__braintrust_cached_metric";
@@ -16526,16 +16828,20 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
16526
16828
  }
16527
16829
 
16528
16830
  // src/instrumentation/plugins/openai-codex-channels.ts
16529
- var openAICodexChannels = defineChannels("@openai/codex-sdk", {
16530
- run: channel({
16531
- channelName: "Thread.run",
16532
- kind: "async"
16533
- }),
16534
- runStreamed: channel({
16535
- channelName: "Thread.runStreamed",
16536
- kind: "async"
16537
- })
16538
- });
16831
+ var openAICodexChannels = defineChannels(
16832
+ "@openai/codex-sdk",
16833
+ {
16834
+ run: channel({
16835
+ channelName: "Thread.run",
16836
+ kind: "async"
16837
+ }),
16838
+ runStreamed: channel({
16839
+ channelName: "Thread.runStreamed",
16840
+ kind: "async"
16841
+ })
16842
+ },
16843
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_CODEX }
16844
+ );
16539
16845
 
16540
16846
  // src/instrumentation/plugins/openai-codex-plugin.ts
16541
16847
  var PATCHED_STREAMED_TURN = /* @__PURE__ */ Symbol.for(
@@ -16623,10 +16929,15 @@ function startCodexRun(event, operation) {
16623
16929
  provider: "openai",
16624
16930
  ...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
16625
16931
  };
16626
- const span = startSpan({
16627
- name: "OpenAI Codex",
16628
- spanAttributes: { type: "task" /* TASK */ }
16629
- });
16932
+ const span = startSpan(
16933
+ withSpanInstrumentationName(
16934
+ {
16935
+ name: "OpenAI Codex",
16936
+ spanAttributes: { type: "task" /* TASK */ }
16937
+ },
16938
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
16939
+ )
16940
+ );
16630
16941
  const startTime = getCurrentUnixTimestamp();
16631
16942
  safeLog(span, {
16632
16943
  input: sanitizedInput,
@@ -16775,7 +17086,12 @@ async function createCompletedItemSpan(state, item) {
16775
17086
  if (!spanArgs) {
16776
17087
  return;
16777
17088
  }
16778
- const span = startSpan(spanArgs.start);
17089
+ const span = startSpan(
17090
+ withSpanInstrumentationName(
17091
+ spanArgs.start,
17092
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
17093
+ )
17094
+ );
16779
17095
  safeLog(span, spanArgs.end);
16780
17096
  span.end();
16781
17097
  }
@@ -16817,15 +17133,20 @@ async function ensureActiveLlmSpan(state) {
16817
17133
  ...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
16818
17134
  "openai_codex.llm_sequence": sequence
16819
17135
  };
16820
- const span = startSpan({
16821
- event: {
16822
- ...sequence === 1 ? { input: state.input } : {},
16823
- metadata
16824
- },
16825
- name: "OpenAI Codex LLM",
16826
- parent: await state.span.export(),
16827
- spanAttributes: { type: "llm" /* LLM */ }
16828
- });
17136
+ const span = startSpan(
17137
+ withSpanInstrumentationName(
17138
+ {
17139
+ event: {
17140
+ ...sequence === 1 ? { input: state.input } : {},
17141
+ metadata
17142
+ },
17143
+ name: "OpenAI Codex LLM",
17144
+ parent: await state.span.export(),
17145
+ spanAttributes: { type: "llm" /* LLM */ }
17146
+ },
17147
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
17148
+ )
17149
+ );
16829
17150
  state.activeLlmSpan = {
16830
17151
  anonymousMessages: [],
16831
17152
  anonymousReasoning: [],
@@ -16871,7 +17192,15 @@ async function startCodexItemSpan(state, item) {
16871
17192
  if (!spanArgs) {
16872
17193
  return;
16873
17194
  }
16874
- state.activeItemSpans.set(itemId, startSpan(spanArgs.start));
17195
+ state.activeItemSpans.set(
17196
+ itemId,
17197
+ startSpan(
17198
+ withSpanInstrumentationName(
17199
+ spanArgs.start,
17200
+ INSTRUMENTATION_NAMES.OPENAI_CODEX
17201
+ )
17202
+ )
17203
+ );
16875
17204
  }
16876
17205
  function updateCodexItem(state, item) {
16877
17206
  if (item.type === "agent_message" && typeof item.text === "string") {
@@ -17181,20 +17510,24 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
17181
17510
  }
17182
17511
 
17183
17512
  // src/instrumentation/plugins/anthropic-channels.ts
17184
- var anthropicChannels = defineChannels("@anthropic-ai/sdk", {
17185
- messagesCreate: channel({
17186
- channelName: "messages.create",
17187
- kind: "async"
17188
- }),
17189
- betaMessagesCreate: channel({
17190
- channelName: "beta.messages.create",
17191
- kind: "async"
17192
- }),
17193
- betaMessagesToolRunner: channel({
17194
- channelName: "beta.messages.toolRunner",
17195
- kind: "sync-stream"
17196
- })
17197
- });
17513
+ var anthropicChannels = defineChannels(
17514
+ "@anthropic-ai/sdk",
17515
+ {
17516
+ messagesCreate: channel({
17517
+ channelName: "messages.create",
17518
+ kind: "async"
17519
+ }),
17520
+ betaMessagesCreate: channel({
17521
+ channelName: "beta.messages.create",
17522
+ kind: "async"
17523
+ }),
17524
+ betaMessagesToolRunner: channel({
17525
+ channelName: "beta.messages.toolRunner",
17526
+ kind: "sync-stream"
17527
+ })
17528
+ },
17529
+ { instrumentationName: INSTRUMENTATION_NAMES.ANTHROPIC }
17530
+ );
17198
17531
 
17199
17532
  // src/instrumentation/plugins/anthropic-plugin.ts
17200
17533
  var ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED = /* @__PURE__ */ Symbol.for(
@@ -17267,12 +17600,17 @@ var AnthropicPlugin = class extends BasePlugin {
17267
17600
  return;
17268
17601
  }
17269
17602
  const params = event.arguments[0] ?? {};
17270
- const span = startSpan({
17271
- name: "anthropic.beta.messages.toolRunner",
17272
- spanAttributes: {
17273
- type: "task" /* TASK */
17274
- }
17275
- });
17603
+ const span = startSpan(
17604
+ withSpanInstrumentationName(
17605
+ {
17606
+ name: "anthropic.beta.messages.toolRunner",
17607
+ spanAttributes: {
17608
+ type: "task" /* TASK */
17609
+ }
17610
+ },
17611
+ INSTRUMENTATION_NAMES.ANTHROPIC
17612
+ )
17613
+ );
17276
17614
  span.log({
17277
17615
  input: processAttachmentsInInput(
17278
17616
  coalesceInput(params.messages ?? [], params.system)
@@ -17413,19 +17751,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
17413
17751
  return finalizeError(error2);
17414
17752
  }
17415
17753
  },
17416
- {
17417
- event: {
17418
- input: getAnthropicToolRunnerInput(args),
17419
- metadata: {
17420
- "gen_ai.tool.name": toolName,
17421
- provider: "anthropic"
17754
+ withSpanInstrumentationName(
17755
+ {
17756
+ event: {
17757
+ input: getAnthropicToolRunnerInput(args),
17758
+ metadata: {
17759
+ "gen_ai.tool.name": toolName,
17760
+ provider: "anthropic"
17761
+ }
17762
+ },
17763
+ name: `tool: ${toolName}`,
17764
+ spanAttributes: {
17765
+ type: "tool" /* TOOL */
17422
17766
  }
17423
17767
  },
17424
- name: `tool: ${toolName}`,
17425
- spanAttributes: {
17426
- type: "tool" /* TOOL */
17427
- }
17428
- }
17768
+ INSTRUMENTATION_NAMES.ANTHROPIC
17769
+ )
17429
17770
  );
17430
17771
  },
17431
17772
  writable: runDescriptor?.writable ?? true
@@ -18020,6 +18361,267 @@ function currentWorkflowAgentWrapperSpan() {
18020
18361
  return void 0;
18021
18362
  }
18022
18363
 
18364
+ // src/wrappers/ai-sdk/harness-agent-context.ts
18365
+ var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
18366
+ var sessionTurnParents = /* @__PURE__ */ new WeakMap();
18367
+ var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
18368
+ var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
18369
+ var harnessTurnParentStore;
18370
+ function serializedTurnContext(value) {
18371
+ if (!isObject(value)) {
18372
+ return void 0;
18373
+ }
18374
+ const context2 = value[BRAINTRUST_TURN_CONTEXT_KEY];
18375
+ if (!isObject(context2) || typeof context2.parent !== "string" || typeof context2.sessionId !== "string" || typeof context2.signature !== "string") {
18376
+ return void 0;
18377
+ }
18378
+ const expectedSignature = signTurnContext({
18379
+ parent: context2.parent,
18380
+ sessionId: context2.sessionId
18381
+ });
18382
+ if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context2.signature, expectedSignature)) {
18383
+ return void 0;
18384
+ }
18385
+ const parent = SpanComponentsV4.fromStr(context2.parent);
18386
+ if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
18387
+ return void 0;
18388
+ }
18389
+ return {
18390
+ parent: context2.parent,
18391
+ sessionId: context2.sessionId
18392
+ };
18393
+ }
18394
+ function signTurnContext(context2) {
18395
+ const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
18396
+ return secret ? isomorph_default.hmacSha256?.(
18397
+ secret,
18398
+ JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context2])
18399
+ ) : void 0;
18400
+ }
18401
+ function continuationParentFromCreateSessionParams(params) {
18402
+ if (!isObject(params)) {
18403
+ return void 0;
18404
+ }
18405
+ const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
18406
+ const context2 = serializedTurnContext(continuation);
18407
+ if (!context2) {
18408
+ return void 0;
18409
+ }
18410
+ if (params.sessionId !== context2.sessionId) {
18411
+ return void 0;
18412
+ }
18413
+ return context2.parent;
18414
+ }
18415
+ function exportSpanSynchronously(span) {
18416
+ const parentInfo = span.getParentInfo();
18417
+ if (!parentInfo) {
18418
+ return void 0;
18419
+ }
18420
+ const objectId = parentInfo.objectId.getSync().value;
18421
+ if (!objectId && !parentInfo.computeObjectMetadataArgs) {
18422
+ return void 0;
18423
+ }
18424
+ return new SpanComponentsV4({
18425
+ object_type: parentInfo.objectType,
18426
+ ...objectId ? { object_id: objectId } : {
18427
+ compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
18428
+ },
18429
+ row_id: span.id,
18430
+ root_span_id: span.rootSpanId,
18431
+ span_id: span.spanId
18432
+ }).toStr();
18433
+ }
18434
+ function exportedParent(parent) {
18435
+ return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
18436
+ }
18437
+ function addSerializedContext(args) {
18438
+ if (!isObject(args.continuation) || args.sessionId === void 0) {
18439
+ return;
18440
+ }
18441
+ const parent = exportedParent(args.parent);
18442
+ if (!parent) {
18443
+ return;
18444
+ }
18445
+ const context2 = {
18446
+ parent,
18447
+ sessionId: args.sessionId
18448
+ };
18449
+ const signature = signTurnContext(context2);
18450
+ if (!signature) {
18451
+ return;
18452
+ }
18453
+ Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
18454
+ configurable: true,
18455
+ enumerable: true,
18456
+ value: {
18457
+ ...context2,
18458
+ signature
18459
+ },
18460
+ writable: true
18461
+ });
18462
+ }
18463
+ function lifecycleMethodDescriptor(session, method) {
18464
+ let owner = session;
18465
+ while (owner) {
18466
+ const descriptor = Object.getOwnPropertyDescriptor(owner, method);
18467
+ if (descriptor) {
18468
+ return { descriptor, owner };
18469
+ }
18470
+ owner = Object.getPrototypeOf(owner);
18471
+ }
18472
+ return void 0;
18473
+ }
18474
+ function patchLifecycleMethod(session, method) {
18475
+ let resolvedDescriptor;
18476
+ try {
18477
+ resolvedDescriptor = lifecycleMethodDescriptor(session, method);
18478
+ } catch {
18479
+ return;
18480
+ }
18481
+ if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
18482
+ return;
18483
+ }
18484
+ const { descriptor, owner } = resolvedDescriptor;
18485
+ const patched = patchedLifecycleMethods.get(owner);
18486
+ if (patched?.has(method)) {
18487
+ return;
18488
+ }
18489
+ const original = descriptor.value;
18490
+ const replacement = function(...args) {
18491
+ const result = Reflect.apply(original, this, args);
18492
+ const addContext = (state) => {
18493
+ try {
18494
+ const parent = sessionTurnParents.get(this);
18495
+ if (!parent) {
18496
+ return;
18497
+ }
18498
+ const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
18499
+ addSerializedContext({
18500
+ continuation,
18501
+ parent,
18502
+ sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
18503
+ });
18504
+ } catch {
18505
+ }
18506
+ };
18507
+ try {
18508
+ if (isObject(result) && typeof result.then === "function") {
18509
+ void Promise.resolve(result).then(addContext, () => {
18510
+ });
18511
+ } else {
18512
+ addContext(result);
18513
+ }
18514
+ } catch {
18515
+ }
18516
+ return result;
18517
+ };
18518
+ try {
18519
+ Object.defineProperty(owner, method, {
18520
+ ...descriptor,
18521
+ value: replacement
18522
+ });
18523
+ const ownerMethods = patched ?? /* @__PURE__ */ new Set();
18524
+ ownerMethods.add(method);
18525
+ if (!patched) {
18526
+ patchedLifecycleMethods.set(owner, ownerMethods);
18527
+ }
18528
+ } catch {
18529
+ }
18530
+ }
18531
+ function patchLifecycleMethods(session) {
18532
+ patchLifecycleMethod(session, "suspendTurn");
18533
+ patchLifecycleMethod(session, "detach");
18534
+ patchLifecycleMethod(session, "stop");
18535
+ }
18536
+ function registerHarnessTurnSpan(args) {
18537
+ if (!isObject(args.session)) {
18538
+ return;
18539
+ }
18540
+ const session = args.session;
18541
+ sessionTurnParents.set(session, args.span);
18542
+ wrapperTurnParents.set(args.span, args.span);
18543
+ patchLifecycleMethods(session);
18544
+ }
18545
+ function harnessContinuationParent(session) {
18546
+ return isObject(session) ? sessionTurnParents.get(session) : void 0;
18547
+ }
18548
+ function captureHarnessCreateSessionParent(params) {
18549
+ try {
18550
+ return continuationParentFromCreateSessionParams(params);
18551
+ } catch {
18552
+ return void 0;
18553
+ }
18554
+ }
18555
+ function registerHarnessSessionParent(session, parent) {
18556
+ if (!parent || !isObject(session)) {
18557
+ return;
18558
+ }
18559
+ const harnessSession = session;
18560
+ sessionTurnParents.set(harnessSession, parent);
18561
+ patchLifecycleMethods(harnessSession);
18562
+ }
18563
+ function currentHarnessTurnParent() {
18564
+ return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
18565
+ }
18566
+ function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
18567
+ const startChannel = tracingChannel2.start;
18568
+ if (!startChannel) {
18569
+ return () => {
18570
+ };
18571
+ }
18572
+ harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
18573
+ const store = harnessTurnParentStore;
18574
+ startChannel.bindStore(
18575
+ store,
18576
+ (event) => parentFromEvent(event) ?? store.getStore()
18577
+ );
18578
+ return () => {
18579
+ startChannel.unbindStore(store);
18580
+ };
18581
+ }
18582
+ function startHarnessTurnChildSpan(parent, args) {
18583
+ const spanArgs = withSpanInstrumentationName(
18584
+ args,
18585
+ INSTRUMENTATION_NAMES.AI_SDK
18586
+ );
18587
+ return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
18588
+ }
18589
+ function updateHarnessTurn(parent, update = {}) {
18590
+ if (!parent) {
18591
+ return;
18592
+ }
18593
+ const log2 = (event) => {
18594
+ if (typeof parent === "string") {
18595
+ updateSpan({ exported: parent, ...event });
18596
+ } else {
18597
+ parent.log(event);
18598
+ }
18599
+ };
18600
+ try {
18601
+ if (Object.prototype.hasOwnProperty.call(update, "output")) {
18602
+ log2({ output: null });
18603
+ }
18604
+ log2(update);
18605
+ } catch {
18606
+ }
18607
+ }
18608
+ function endHarnessTurn(parent) {
18609
+ const endTime = getCurrentUnixTimestamp();
18610
+ if (!parent) {
18611
+ return endTime;
18612
+ }
18613
+ try {
18614
+ const event = { metrics: { end: endTime } };
18615
+ if (typeof parent === "string") {
18616
+ updateSpan({ exported: parent, ...event });
18617
+ } else {
18618
+ parent.log(event);
18619
+ }
18620
+ } catch {
18621
+ }
18622
+ return endTime;
18623
+ }
18624
+
18023
18625
  // src/wrappers/ai-sdk/telemetry.ts
18024
18626
  function braintrustAISDKTelemetry() {
18025
18627
  const operations = /* @__PURE__ */ new Map();
@@ -18038,14 +18640,17 @@ function braintrustAISDKTelemetry() {
18038
18640
  console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error2);
18039
18641
  }
18040
18642
  };
18041
- const startChildSpan = (operationKey, name, type, event) => {
18643
+ const startChildSpan = (operationKey, name, type, event, parentOverride) => {
18042
18644
  const parent = operations.get(operationKey)?.span;
18043
- const spanArgs = {
18044
- name,
18045
- spanAttributes: { type },
18046
- ...event ? { event } : {}
18047
- };
18048
- const span = parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
18645
+ const spanArgs = withSpanInstrumentationName(
18646
+ {
18647
+ name,
18648
+ spanAttributes: { type },
18649
+ ...event ? { event } : {}
18650
+ },
18651
+ INSTRUMENTATION_NAMES.AI_SDK
18652
+ );
18653
+ const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
18049
18654
  const state = operations.get(operationKey);
18050
18655
  if (state && type === "llm" /* LLM */) {
18051
18656
  state.hadModelChild = true;
@@ -18325,14 +18930,30 @@ function braintrustAISDKTelemetry() {
18325
18930
  const workflowAgent = operationName === "WorkflowAgent.stream";
18326
18931
  const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
18327
18932
  const ownsSpan = !wrapperSpan;
18328
- const span = ownsSpan ? startSpan({
18329
- name: operationName,
18330
- spanAttributes: { type: "function" /* FUNCTION */ }
18331
- }) : wrapperSpan;
18933
+ const harnessTurnParent = currentHarnessTurnParent();
18934
+ const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
18935
+ harnessTurnParent,
18936
+ withSpanInstrumentationName(
18937
+ {
18938
+ name: operationName,
18939
+ spanAttributes: { type: "function" /* FUNCTION */ }
18940
+ },
18941
+ INSTRUMENTATION_NAMES.AI_SDK
18942
+ )
18943
+ ) : startSpan(
18944
+ withSpanInstrumentationName(
18945
+ {
18946
+ name: operationName,
18947
+ spanAttributes: { type: "function" /* FUNCTION */ }
18948
+ },
18949
+ INSTRUMENTATION_NAMES.AI_SDK
18950
+ )
18951
+ ) : wrapperSpan;
18332
18952
  const operationKey = createOperationKey(event, operationName);
18333
18953
  registerOperation({
18334
18954
  callId: event.callId,
18335
18955
  hadModelChild: false,
18956
+ harnessTurnParent,
18336
18957
  loggedInput: false,
18337
18958
  operationName,
18338
18959
  operationKey,
@@ -18573,7 +19194,8 @@ function braintrustAISDKTelemetry() {
18573
19194
  toolCallId,
18574
19195
  ...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
18575
19196
  }
18576
- }
19197
+ },
19198
+ state?.harnessTurnParent
18577
19199
  );
18578
19200
  toolSpans.set(toolCallId, { operationKey, span });
18579
19201
  });
@@ -18809,74 +19431,102 @@ function finishOutput(result, operationName) {
18809
19431
  }
18810
19432
 
18811
19433
  // src/instrumentation/plugins/ai-sdk-channels.ts
18812
- var aiSDKChannels = defineChannels("ai", {
18813
- generateText: channel({
18814
- channelName: "generateText",
18815
- kind: "async"
18816
- }),
18817
- streamText: channel({
18818
- channelName: "streamText",
18819
- kind: "async"
18820
- }),
18821
- streamTextSync: channel({
18822
- channelName: "streamText.sync",
18823
- kind: "sync-stream"
18824
- }),
18825
- generateObject: channel({
18826
- channelName: "generateObject",
18827
- kind: "async"
18828
- }),
18829
- streamObject: channel({
18830
- channelName: "streamObject",
18831
- kind: "async"
18832
- }),
18833
- streamObjectSync: channel({
18834
- channelName: "streamObject.sync",
18835
- kind: "sync-stream"
18836
- }),
18837
- embed: channel(
18838
- {
19434
+ var aiSDKChannels = defineChannels(
19435
+ "ai",
19436
+ {
19437
+ generateText: channel({
19438
+ channelName: "generateText",
19439
+ kind: "async"
19440
+ }),
19441
+ streamText: channel({
19442
+ channelName: "streamText",
19443
+ kind: "async"
19444
+ }),
19445
+ streamTextSync: channel({
19446
+ channelName: "streamText.sync",
19447
+ kind: "sync-stream"
19448
+ }),
19449
+ generateObject: channel({
19450
+ channelName: "generateObject",
19451
+ kind: "async"
19452
+ }),
19453
+ streamObject: channel({
19454
+ channelName: "streamObject",
19455
+ kind: "async"
19456
+ }),
19457
+ streamObjectSync: channel({
19458
+ channelName: "streamObject.sync",
19459
+ kind: "sync-stream"
19460
+ }),
19461
+ embed: channel({
18839
19462
  channelName: "embed",
18840
19463
  kind: "async"
18841
- }
18842
- ),
18843
- embedMany: channel({
18844
- channelName: "embedMany",
18845
- kind: "async"
18846
- }),
18847
- rerank: channel({
18848
- channelName: "rerank",
18849
- kind: "async"
18850
- }),
18851
- agentGenerate: channel({
18852
- channelName: "Agent.generate",
18853
- kind: "async"
18854
- }),
18855
- agentStream: channel({
18856
- channelName: "Agent.stream",
18857
- kind: "async"
18858
- }),
18859
- agentStreamSync: channel({
18860
- channelName: "Agent.stream.sync",
18861
- kind: "sync-stream"
18862
- }),
18863
- toolLoopAgentGenerate: channel({
18864
- channelName: "ToolLoopAgent.generate",
18865
- kind: "async"
18866
- }),
18867
- toolLoopAgentStream: channel({
18868
- channelName: "ToolLoopAgent.stream",
18869
- kind: "async"
18870
- }),
18871
- workflowAgentStream: channel({
18872
- channelName: "WorkflowAgent.stream",
18873
- kind: "async"
18874
- }),
18875
- v7CreateTelemetryDispatcher: channel({
18876
- channelName: "createTelemetryDispatcher",
18877
- kind: "sync-stream"
18878
- })
18879
- });
19464
+ }),
19465
+ embedMany: channel({
19466
+ channelName: "embedMany",
19467
+ kind: "async"
19468
+ }),
19469
+ rerank: channel({
19470
+ channelName: "rerank",
19471
+ kind: "async"
19472
+ }),
19473
+ agentGenerate: channel({
19474
+ channelName: "Agent.generate",
19475
+ kind: "async"
19476
+ }),
19477
+ agentStream: channel({
19478
+ channelName: "Agent.stream",
19479
+ kind: "async"
19480
+ }),
19481
+ agentStreamSync: channel({
19482
+ channelName: "Agent.stream.sync",
19483
+ kind: "sync-stream"
19484
+ }),
19485
+ toolLoopAgentGenerate: channel({
19486
+ channelName: "ToolLoopAgent.generate",
19487
+ kind: "async"
19488
+ }),
19489
+ toolLoopAgentStream: channel({
19490
+ channelName: "ToolLoopAgent.stream",
19491
+ kind: "async"
19492
+ }),
19493
+ workflowAgentStream: channel({
19494
+ channelName: "WorkflowAgent.stream",
19495
+ kind: "async"
19496
+ }),
19497
+ v7CreateTelemetryDispatcher: channel({
19498
+ channelName: "createTelemetryDispatcher",
19499
+ kind: "sync-stream"
19500
+ })
19501
+ },
19502
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
19503
+ );
19504
+ var harnessAgentChannels = defineChannels(
19505
+ "@ai-sdk/harness",
19506
+ {
19507
+ createSession: channel({
19508
+ channelName: "HarnessAgent.createSession",
19509
+ kind: "async"
19510
+ }),
19511
+ generate: channel({
19512
+ channelName: "HarnessAgent.generate",
19513
+ kind: "async"
19514
+ }),
19515
+ stream: channel({
19516
+ channelName: "HarnessAgent.stream",
19517
+ kind: "async"
19518
+ }),
19519
+ continueGenerate: channel({
19520
+ channelName: "HarnessAgent.continueGenerate",
19521
+ kind: "async"
19522
+ }),
19523
+ continueStream: channel({
19524
+ channelName: "HarnessAgent.continueStream",
19525
+ kind: "async"
19526
+ })
19527
+ },
19528
+ { instrumentationName: INSTRUMENTATION_NAMES.AI_SDK }
19529
+ );
18880
19530
 
18881
19531
  // src/instrumentation/plugins/ai-sdk-plugin.ts
18882
19532
  var DEFAULT_DENY_OUTPUT_PATHS = [
@@ -18953,6 +19603,17 @@ var AISDKPlugin = class extends BasePlugin {
18953
19603
  subscribeToAISDK() {
18954
19604
  const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
18955
19605
  this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
19606
+ this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
19607
+ this.unsubscribers.push(
19608
+ subscribeToHarnessContinuation(
19609
+ harnessAgentChannels.continueGenerate,
19610
+ denyOutputPaths
19611
+ ),
19612
+ subscribeToHarnessContinuation(
19613
+ harnessAgentChannels.continueStream,
19614
+ denyOutputPaths
19615
+ )
19616
+ );
18956
19617
  this.unsubscribers.push(
18957
19618
  traceStreamingChannel(aiSDKChannels.generateText, {
18958
19619
  name: "generateText",
@@ -19145,6 +19806,90 @@ var AISDKPlugin = class extends BasePlugin {
19145
19806
  })
19146
19807
  })
19147
19808
  );
19809
+ this.unsubscribers.push(
19810
+ traceStreamingChannel(harnessAgentChannels.generate, {
19811
+ name: "HarnessAgent.generate",
19812
+ startSpan: _internalStartSpanWithInitialMerge,
19813
+ type: "task" /* TASK */,
19814
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
19815
+ extractOutput: (result, endEvent) => processAISDKOutput(
19816
+ result,
19817
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
19818
+ ),
19819
+ extractMetrics: (result) => extractTokenMetrics(result),
19820
+ aggregateChunks: aggregateAISDKChunks
19821
+ })
19822
+ );
19823
+ this.unsubscribers.push(
19824
+ traceStreamingChannel(harnessAgentChannels.stream, {
19825
+ name: "HarnessAgent.stream",
19826
+ startSpan: _internalStartSpanWithInitialMerge,
19827
+ type: "task" /* TASK */,
19828
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
19829
+ extractOutput: (result, endEvent) => processAISDKOutput(
19830
+ result,
19831
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
19832
+ ),
19833
+ extractMetrics: (result, startTime) => ({
19834
+ ...extractTokenMetrics(result),
19835
+ ...startTime === void 0 ? {} : {
19836
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
19837
+ }
19838
+ }),
19839
+ aggregateChunks: aggregateAISDKChunks,
19840
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
19841
+ defaultDenyOutputPaths: denyOutputPaths,
19842
+ endEvent,
19843
+ result,
19844
+ resolvePromiseUsage: true,
19845
+ span,
19846
+ startTime
19847
+ })
19848
+ })
19849
+ );
19850
+ this.unsubscribers.push(
19851
+ traceStreamingChannel(harnessAgentChannels.continueGenerate, {
19852
+ name: "HarnessAgent.continueGenerate",
19853
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
19854
+ startSpan: _internalStartSpanWithInitialMerge,
19855
+ type: "task" /* TASK */,
19856
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
19857
+ extractOutput: (result, endEvent) => processAISDKOutput(
19858
+ result,
19859
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
19860
+ ),
19861
+ extractMetrics: (result) => extractTokenMetrics(result),
19862
+ aggregateChunks: aggregateAISDKChunks
19863
+ })
19864
+ );
19865
+ this.unsubscribers.push(
19866
+ traceStreamingChannel(harnessAgentChannels.continueStream, {
19867
+ name: "HarnessAgent.continueStream",
19868
+ shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
19869
+ startSpan: _internalStartSpanWithInitialMerge,
19870
+ type: "task" /* TASK */,
19871
+ extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
19872
+ extractOutput: (result, endEvent) => processAISDKOutput(
19873
+ result,
19874
+ resolveDenyOutputPaths(endEvent, denyOutputPaths)
19875
+ ),
19876
+ extractMetrics: (result, startTime) => ({
19877
+ ...extractTokenMetrics(result),
19878
+ ...startTime === void 0 ? {} : {
19879
+ time_to_first_token: getCurrentUnixTimestamp() - startTime
19880
+ }
19881
+ }),
19882
+ aggregateChunks: aggregateAISDKChunks,
19883
+ patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
19884
+ defaultDenyOutputPaths: denyOutputPaths,
19885
+ endEvent,
19886
+ result,
19887
+ resolvePromiseUsage: true,
19888
+ span,
19889
+ startTime
19890
+ })
19891
+ })
19892
+ );
19148
19893
  this.unsubscribers.push(
19149
19894
  traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
19150
19895
  name: "ToolLoopAgent.generate",
@@ -19215,6 +19960,8 @@ var AISDKPlugin = class extends BasePlugin {
19215
19960
  defaultDenyOutputPaths: denyOutputPaths,
19216
19961
  endEvent,
19217
19962
  onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
19963
+ onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
19964
+ onError: () => unregisterWorkflowAgentWrapperSpan(span),
19218
19965
  result,
19219
19966
  span,
19220
19967
  startTime
@@ -19223,6 +19970,153 @@ var AISDKPlugin = class extends BasePlugin {
19223
19970
  );
19224
19971
  }
19225
19972
  };
19973
+ function subscribeToHarnessAgentCreateSession() {
19974
+ const channel2 = harnessAgentChannels.createSession.tracingChannel();
19975
+ const parents = /* @__PURE__ */ new WeakMap();
19976
+ const handlers = {
19977
+ start: (event) => {
19978
+ const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
19979
+ if (parent) {
19980
+ parents.set(event, parent);
19981
+ }
19982
+ },
19983
+ asyncEnd: (event) => {
19984
+ registerHarnessSessionParent(event.result, parents.get(event));
19985
+ parents.delete(event);
19986
+ },
19987
+ error: (event) => {
19988
+ parents.delete(event);
19989
+ }
19990
+ };
19991
+ channel2.subscribe(handlers);
19992
+ return () => channel2.unsubscribe(handlers);
19993
+ }
19994
+ function harnessContinuationParentFromEvent(event) {
19995
+ try {
19996
+ return harnessContinuationParent(event.arguments?.[0]?.session);
19997
+ } catch {
19998
+ return void 0;
19999
+ }
20000
+ }
20001
+ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
20002
+ const channel2 = continuationChannel.tracingChannel();
20003
+ const parents = /* @__PURE__ */ new WeakMap();
20004
+ const startTimes = /* @__PURE__ */ new WeakMap();
20005
+ const unbindParentStore = bindHarnessTurnParentToStart(
20006
+ channel2,
20007
+ harnessContinuationParentFromEvent
20008
+ );
20009
+ const handlers = {
20010
+ start: (event) => {
20011
+ const parent = harnessContinuationParentFromEvent(event);
20012
+ if (!parent) {
20013
+ return;
20014
+ }
20015
+ parents.set(event, parent);
20016
+ startTimes.set(event, getCurrentUnixTimestamp());
20017
+ try {
20018
+ const params = event.arguments?.[0];
20019
+ if (params) {
20020
+ prepareAISDKHarnessAgentInput(params, event.self);
20021
+ }
20022
+ } catch (error2) {
20023
+ debugLogger.error(
20024
+ "Error preparing Harness continuation telemetry:",
20025
+ error2
20026
+ );
20027
+ }
20028
+ },
20029
+ asyncEnd: (event) => {
20030
+ const parent = parents.get(event);
20031
+ const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
20032
+ parents.delete(event);
20033
+ startTimes.delete(event);
20034
+ if (!parent) {
20035
+ return;
20036
+ }
20037
+ const endEvent = event;
20038
+ const span = {
20039
+ end: () => endHarnessTurn(parent),
20040
+ log: (update) => updateHarnessTurn(
20041
+ parent,
20042
+ Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
20043
+ )
20044
+ };
20045
+ try {
20046
+ if (isAsyncIterable3(endEvent.result)) {
20047
+ patchStreamIfNeeded(endEvent.result, {
20048
+ onComplete: (chunks) => {
20049
+ try {
20050
+ const { metadata, metrics, output } = aggregateAISDKChunks(
20051
+ chunks,
20052
+ endEvent.result,
20053
+ endEvent
20054
+ );
20055
+ span.log({
20056
+ ...metadata ? { metadata } : {},
20057
+ metrics,
20058
+ output
20059
+ });
20060
+ } catch (error2) {
20061
+ debugLogger.error(
20062
+ "Error aggregating Harness continuation stream:",
20063
+ error2
20064
+ );
20065
+ } finally {
20066
+ span.end();
20067
+ }
20068
+ },
20069
+ onError: (error2) => {
20070
+ span.log({ error: toLoggedError(error2), output: null });
20071
+ span.end();
20072
+ }
20073
+ });
20074
+ return;
20075
+ }
20076
+ if (patchAISDKStreamingResult({
20077
+ defaultDenyOutputPaths,
20078
+ endEvent,
20079
+ result: endEvent.result,
20080
+ resolvePromiseUsage: true,
20081
+ span,
20082
+ startTime
20083
+ })) {
20084
+ return;
20085
+ }
20086
+ finalizeAISDKChildTracing(endEvent);
20087
+ span.log({
20088
+ metrics: extractTokenMetrics(endEvent.result),
20089
+ output: processAISDKOutput(
20090
+ endEvent.result,
20091
+ resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
20092
+ )
20093
+ });
20094
+ span.end();
20095
+ } catch (error2) {
20096
+ debugLogger.error("Error tracing Harness continuation:", error2);
20097
+ span.end();
20098
+ }
20099
+ },
20100
+ error: (event) => {
20101
+ const parent = parents.get(event);
20102
+ parents.delete(event);
20103
+ startTimes.delete(event);
20104
+ if (!parent) {
20105
+ return;
20106
+ }
20107
+ updateHarnessTurn(parent, {
20108
+ error: toLoggedError(event.error),
20109
+ output: null
20110
+ });
20111
+ endHarnessTurn(parent);
20112
+ }
20113
+ };
20114
+ channel2.subscribe(handlers);
20115
+ return () => {
20116
+ unbindParentStore();
20117
+ channel2.unsubscribe(handlers);
20118
+ };
20119
+ }
19226
20120
  function subscribeToAISDKV7TelemetryDispatcher() {
19227
20121
  const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
19228
20122
  const telemetry = braintrustAISDKTelemetry();
@@ -19675,6 +20569,55 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
19675
20569
  metadata
19676
20570
  };
19677
20571
  }
20572
+ function prepareAISDKHarnessAgentInput(params, self, span) {
20573
+ if (span) {
20574
+ registerHarnessTurnSpan({
20575
+ session: params.session,
20576
+ span
20577
+ });
20578
+ }
20579
+ if (isObject(self)) {
20580
+ try {
20581
+ if (isObject(self.settings)) {
20582
+ const settings = self.settings;
20583
+ if (settings.telemetry === void 0) {
20584
+ Reflect.set(self, "settings", { ...settings, telemetry: {} });
20585
+ }
20586
+ }
20587
+ } catch {
20588
+ }
20589
+ }
20590
+ const selectedInput = {};
20591
+ if (params.prompt !== void 0) {
20592
+ selectedInput.prompt = params.prompt;
20593
+ }
20594
+ if (params.messages !== void 0) {
20595
+ selectedInput.messages = params.messages;
20596
+ }
20597
+ if (params.toolApprovalContinuations !== void 0) {
20598
+ selectedInput.toolApprovalContinuations = params.toolApprovalContinuations;
20599
+ }
20600
+ const metadata = extractMetadataFromCallParams({}, self);
20601
+ if (isObject(params.session) && typeof params.session.sessionId === "string") {
20602
+ metadata.sessionId = params.session.sessionId;
20603
+ }
20604
+ if (isObject(self)) {
20605
+ if (typeof self.harnessId === "string") {
20606
+ metadata.harnessId = self.harnessId;
20607
+ }
20608
+ if (typeof self.permissionMode === "string") {
20609
+ metadata.permissionMode = self.permissionMode;
20610
+ }
20611
+ }
20612
+ return {
20613
+ input: processAISDKCallInput(selectedInput).input,
20614
+ metadata
20615
+ };
20616
+ }
20617
+ function harnessSessionFromArguments(args) {
20618
+ const params = args[0];
20619
+ return isObject(params) ? params.session : void 0;
20620
+ }
19678
20621
  function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
19679
20622
  registerWorkflowAgentWrapperSpan(span);
19680
20623
  const { input } = processAISDKWorkflowAgentCallInput(params);
@@ -20122,19 +21065,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
20122
21065
  activeEntry.openSpans.delete(span);
20123
21066
  }
20124
21067
  },
20125
- {
20126
- name: "doGenerate",
20127
- spanAttributes: {
20128
- type: "llm" /* LLM */
21068
+ withSpanInstrumentationName(
21069
+ {
21070
+ name: "doGenerate",
21071
+ spanAttributes: {
21072
+ type: "llm" /* LLM */
21073
+ },
21074
+ event: buildAISDKModelStartEvent(
21075
+ callOptions,
21076
+ activeEntry.baseMetadata,
21077
+ {
21078
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
21079
+ }
21080
+ )
20129
21081
  },
20130
- event: buildAISDKModelStartEvent(
20131
- callOptions,
20132
- activeEntry.baseMetadata,
20133
- {
20134
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
20135
- }
20136
- )
20137
- }
21082
+ INSTRUMENTATION_NAMES.AI_SDK
21083
+ )
20138
21084
  );
20139
21085
  };
20140
21086
  if (originalDoStream) {
@@ -20144,19 +21090,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
20144
21090
  return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
20145
21091
  }
20146
21092
  closeOpenAISDKModelPatchSpans(activeEntry);
20147
- const span = activeEntry.parentSpan.startSpan({
20148
- name: "doStream",
20149
- spanAttributes: {
20150
- type: "llm" /* LLM */
20151
- },
20152
- event: buildAISDKModelStartEvent(
20153
- callOptions,
20154
- activeEntry.baseMetadata,
21093
+ const span = activeEntry.parentSpan.startSpan(
21094
+ withSpanInstrumentationName(
20155
21095
  {
20156
- workflowAgent: activeEntry.childTracingOptions.workflowAgent
20157
- }
21096
+ name: "doStream",
21097
+ spanAttributes: {
21098
+ type: "llm" /* LLM */
21099
+ },
21100
+ event: buildAISDKModelStartEvent(
21101
+ callOptions,
21102
+ activeEntry.baseMetadata,
21103
+ {
21104
+ workflowAgent: activeEntry.childTracingOptions.workflowAgent
21105
+ }
21106
+ )
21107
+ },
21108
+ INSTRUMENTATION_NAMES.AI_SDK
20158
21109
  )
20159
- });
21110
+ );
20160
21111
  activeEntry.openSpans.add(span);
20161
21112
  const streamStartTime = getCurrentUnixTimestamp();
20162
21113
  const result = await withCurrent(
@@ -20328,12 +21279,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
20328
21279
  }
20329
21280
  if (isAsyncGenerator2(result)) {
20330
21281
  return (async function* () {
20331
- const span = activeEntry.parentSpan.startSpan({
20332
- name: activeEntry.name,
20333
- spanAttributes: {
20334
- type: "tool" /* TOOL */
20335
- }
20336
- });
21282
+ const span = activeEntry.parentSpan.startSpan(
21283
+ withSpanInstrumentationName(
21284
+ {
21285
+ name: activeEntry.name,
21286
+ spanAttributes: {
21287
+ type: "tool" /* TOOL */
21288
+ }
21289
+ },
21290
+ INSTRUMENTATION_NAMES.AI_SDK
21291
+ )
21292
+ );
20337
21293
  span.log({ input: serializeToolExecutionInput(args) });
20338
21294
  try {
20339
21295
  let lastValue;
@@ -20357,12 +21313,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
20357
21313
  span.log({ output: awaitedResult });
20358
21314
  return awaitedResult;
20359
21315
  },
20360
- {
20361
- name: activeEntry.name,
20362
- spanAttributes: {
20363
- type: "tool" /* TOOL */
20364
- }
20365
- }
21316
+ withSpanInstrumentationName(
21317
+ {
21318
+ name: activeEntry.name,
21319
+ spanAttributes: {
21320
+ type: "tool" /* TOOL */
21321
+ }
21322
+ },
21323
+ INSTRUMENTATION_NAMES.AI_SDK
21324
+ )
20366
21325
  );
20367
21326
  };
20368
21327
  cleanup.push(() => {
@@ -20521,7 +21480,10 @@ function patchAISDKStreamingResult(args) {
20521
21480
  defaultDenyOutputPaths,
20522
21481
  endEvent,
20523
21482
  onComplete,
21483
+ onCancel,
21484
+ onError,
20524
21485
  result,
21486
+ resolvePromiseUsage,
20525
21487
  span,
20526
21488
  startTime
20527
21489
  } = args;
@@ -20531,14 +21493,43 @@ function patchAISDKStreamingResult(args) {
20531
21493
  const resultRecord = result;
20532
21494
  attachKnownResultPromiseHandlers(resultRecord);
20533
21495
  let finalized = false;
20534
- const finalize = () => {
21496
+ const finalize = (outcome) => {
20535
21497
  if (finalized) {
20536
21498
  return;
20537
21499
  }
20538
21500
  finalized = true;
20539
- finalizeAISDKChildTracing(endEvent);
20540
- span.end();
20541
- onComplete?.();
21501
+ try {
21502
+ finalizeAISDKChildTracing(endEvent);
21503
+ } catch (error2) {
21504
+ debugLogger.error("Error finalizing AI SDK child tracing:", error2);
21505
+ }
21506
+ try {
21507
+ span.end();
21508
+ } catch (error2) {
21509
+ debugLogger.error("Error ending AI SDK streaming span:", error2);
21510
+ }
21511
+ if (outcome && "error" in outcome) {
21512
+ try {
21513
+ onError?.(outcome.error);
21514
+ } catch (error2) {
21515
+ debugLogger.error("Error in AI SDK streaming error hook:", error2);
21516
+ }
21517
+ } else if (outcome) {
21518
+ try {
21519
+ onComplete?.(outcome);
21520
+ } catch (error2) {
21521
+ debugLogger.error("Error in AI SDK streaming completion hook:", error2);
21522
+ }
21523
+ } else {
21524
+ try {
21525
+ onCancel?.();
21526
+ } catch (error2) {
21527
+ debugLogger.error(
21528
+ "Error in AI SDK streaming cancellation hook:",
21529
+ error2
21530
+ );
21531
+ }
21532
+ }
20542
21533
  };
20543
21534
  let outputLogged = false;
20544
21535
  const logStreamingOutput = async (firstChunkTime) => {
@@ -20548,6 +21539,22 @@ function patchAISDKStreamingResult(args) {
20548
21539
  outputLogged = true;
20549
21540
  try {
20550
21541
  const metrics = extractTopLevelAISDKMetrics(result, endEvent);
21542
+ if (resolvePromiseUsage) {
21543
+ try {
21544
+ const resultRecord2 = result;
21545
+ const pendingUsage = resultRecord2.totalUsage ?? resultRecord2.usage;
21546
+ if (isPromiseLike(pendingUsage)) {
21547
+ const usage = await Promise.resolve(pendingUsage);
21548
+ if (isObject(usage)) {
21549
+ Object.assign(
21550
+ metrics,
21551
+ extractTokenMetrics({ usage })
21552
+ );
21553
+ }
21554
+ }
21555
+ } catch {
21556
+ }
21557
+ }
20551
21558
  if (metrics.time_to_first_token === void 0 && firstChunkTime !== void 0) {
20552
21559
  metrics.time_to_first_token = firstChunkTime - startTime;
20553
21560
  }
@@ -20556,15 +21563,29 @@ function patchAISDKStreamingResult(args) {
20556
21563
  resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
20557
21564
  );
20558
21565
  const metadata = buildResolvedMetadataPayload(result).metadata;
20559
- span.log({
20560
- output,
20561
- ...metadata ? { metadata } : {},
20562
- metrics
20563
- });
21566
+ try {
21567
+ span.log({
21568
+ output,
21569
+ ...metadata ? { metadata } : {},
21570
+ metrics
21571
+ });
21572
+ } catch (error2) {
21573
+ debugLogger.error("Error logging AI SDK streaming output:", error2);
21574
+ }
21575
+ finalize({ metrics, output });
20564
21576
  } catch (error2) {
20565
- span.log({ error: toLoggedError(error2) });
20566
- } finally {
20567
- finalize();
21577
+ const loggedError = toLoggedError(error2);
21578
+ try {
21579
+ span.log({ error: loggedError });
21580
+ } catch (loggingError) {
21581
+ debugLogger.error(
21582
+ "Error logging AI SDK streaming failure:",
21583
+ loggingError
21584
+ );
21585
+ }
21586
+ finalize({
21587
+ error: error2 instanceof Error ? error2 : new Error(String(loggedError))
21588
+ });
20568
21589
  }
20569
21590
  };
20570
21591
  const createAsyncIterableHooks = () => {
@@ -20581,11 +21602,16 @@ function patchAISDKStreamingResult(args) {
20581
21602
  onError: (error2) => {
20582
21603
  if (!outputLogged) {
20583
21604
  outputLogged = true;
20584
- span.log({
20585
- error: error2.message
20586
- });
21605
+ try {
21606
+ span.log({ error: error2 });
21607
+ } catch (loggingError) {
21608
+ debugLogger.error(
21609
+ "Error logging AI SDK stream failure:",
21610
+ loggingError
21611
+ );
21612
+ }
20587
21613
  }
20588
- finalize();
21614
+ finalize({ error: error2 });
20589
21615
  },
20590
21616
  onCancel: finalize
20591
21617
  };
@@ -20671,8 +21697,18 @@ function patchAISDKStreamingResult(args) {
20671
21697
  }
20672
21698
  controller.enqueue(value);
20673
21699
  } catch (error2) {
20674
- span.log({ error: toLoggedError(error2) });
20675
- finalize();
21700
+ const loggedError = toLoggedError(error2);
21701
+ try {
21702
+ span.log({ error: loggedError });
21703
+ } catch (loggingError) {
21704
+ debugLogger.error(
21705
+ "Error logging AI SDK base stream failure:",
21706
+ loggingError
21707
+ );
21708
+ }
21709
+ finalize({
21710
+ error: error2 instanceof Error ? error2 : new Error(String(loggedError))
21711
+ });
20676
21712
  controller.error(error2);
20677
21713
  }
20678
21714
  },
@@ -21461,7 +22497,8 @@ var claudeAgentSDKChannels = defineChannels(
21461
22497
  channelName: "query",
21462
22498
  kind: "sync-stream"
21463
22499
  })
21464
- }
22500
+ },
22501
+ { instrumentationName: INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK }
21465
22502
  );
21466
22503
 
21467
22504
  // src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
@@ -21609,22 +22646,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
21609
22646
  const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
21610
22647
  const runWithResolvedParent = async () => {
21611
22648
  const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
21612
- const span = startSpan({
21613
- event: {
21614
- input: handlerArgs[0],
21615
- metadata: {
21616
- "claude_agent_sdk.raw_tool_name": rawToolName,
21617
- "gen_ai.tool.name": metadata.toolName,
21618
- ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
21619
- ...metadata.serverName && {
21620
- "mcp.server": metadata.serverName
21621
- }
21622
- }
21623
- },
21624
- name: spanName,
21625
- ...parent && { parent },
21626
- spanAttributes: { type: "tool" /* TOOL */ }
21627
- });
22649
+ const span = startSpan(
22650
+ withSpanInstrumentationName(
22651
+ {
22652
+ event: {
22653
+ input: handlerArgs[0],
22654
+ metadata: {
22655
+ "claude_agent_sdk.raw_tool_name": rawToolName,
22656
+ "gen_ai.tool.name": metadata.toolName,
22657
+ ...toolUseId && { "gen_ai.tool.call.id": toolUseId },
22658
+ ...metadata.serverName && {
22659
+ "mcp.server": metadata.serverName
22660
+ }
22661
+ }
22662
+ },
22663
+ name: spanName,
22664
+ ...parent && { parent },
22665
+ spanAttributes: { type: "tool" /* TOOL */ }
22666
+ },
22667
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
22668
+ )
22669
+ );
21628
22670
  const runHandler = () => Reflect.apply(handler, this, handlerArgs);
21629
22671
  const finalizeSuccess = (result) => {
21630
22672
  span.log({ output: result });
@@ -21949,14 +22991,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
21949
22991
  ).filter(
21950
22992
  (c) => c !== void 0
21951
22993
  );
21952
- const span = existingSpan ?? startSpan({
21953
- name: "anthropic.messages.create",
21954
- parent: parentSpan,
21955
- spanAttributes: {
21956
- type: "llm" /* LLM */
21957
- },
21958
- startTime
21959
- });
22994
+ const span = existingSpan ?? startSpan(
22995
+ withSpanInstrumentationName(
22996
+ {
22997
+ name: "anthropic.messages.create",
22998
+ parent: parentSpan,
22999
+ spanAttributes: {
23000
+ type: "llm" /* LLM */
23001
+ },
23002
+ startTime
23003
+ },
23004
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
23005
+ )
23006
+ );
21960
23007
  span.log({
21961
23008
  input,
21962
23009
  metadata: model ? { model } : void 0,
@@ -22059,25 +23106,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
22059
23106
  return {};
22060
23107
  }
22061
23108
  const parsed = parseToolName(input.tool_name);
22062
- const toolSpan = startSpan({
22063
- event: {
22064
- input: input.tool_input,
22065
- metadata: {
22066
- "claude_agent_sdk.cwd": input.cwd,
22067
- "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
22068
- "claude_agent_sdk.session_id": input.session_id,
22069
- "gen_ai.tool.call.id": toolUseID,
22070
- "gen_ai.tool.name": parsed.toolName,
22071
- ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
22072
- ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
22073
- }
22074
- },
22075
- name: parsed.displayName,
22076
- parent: await resolveParentSpan(toolUseID, {
22077
- agentId: input.agent_id
22078
- }),
22079
- spanAttributes: { type: "tool" /* TOOL */ }
22080
- });
23109
+ const toolSpan = startSpan(
23110
+ withSpanInstrumentationName(
23111
+ {
23112
+ event: {
23113
+ input: input.tool_input,
23114
+ metadata: {
23115
+ "claude_agent_sdk.cwd": input.cwd,
23116
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
23117
+ "claude_agent_sdk.session_id": input.session_id,
23118
+ "gen_ai.tool.call.id": toolUseID,
23119
+ "gen_ai.tool.name": parsed.toolName,
23120
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
23121
+ ...getMcpServerMetadata(parsed.mcpServer, mcpServers)
23122
+ }
23123
+ },
23124
+ name: parsed.displayName,
23125
+ parent: await resolveParentSpan(toolUseID, {
23126
+ agentId: input.agent_id
23127
+ }),
23128
+ spanAttributes: { type: "tool" /* TOOL */ }
23129
+ },
23130
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
23131
+ )
23132
+ );
22081
23133
  activeToolSpans.set(toolUseID, toolSpan);
22082
23134
  return {};
22083
23135
  };
@@ -22443,14 +23495,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
22443
23495
  const spanName = formatSubAgentSpanName(details);
22444
23496
  const parentToolSpan = activeToolSpans.get(parentToolUseId);
22445
23497
  const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
22446
- const subAgentSpan = startSpan({
22447
- event: {
22448
- metadata: subAgentDetailsToMetadata(details)
22449
- },
22450
- name: spanName,
22451
- parent: parentSpan,
22452
- spanAttributes: { type: "task" /* TASK */ }
22453
- });
23498
+ const subAgentSpan = startSpan(
23499
+ withSpanInstrumentationName(
23500
+ {
23501
+ event: {
23502
+ metadata: subAgentDetailsToMetadata(details)
23503
+ },
23504
+ name: spanName,
23505
+ parent: parentSpan,
23506
+ spanAttributes: { type: "task" /* TASK */ }
23507
+ },
23508
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
23509
+ )
23510
+ );
22454
23511
  subAgentSpans.set(parentToolUseId, subAgentSpan);
22455
23512
  return subAgentSpan;
22456
23513
  }
@@ -22471,14 +23528,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
22471
23528
  );
22472
23529
  llmParentSpan = await subAgentSpan.export();
22473
23530
  }
22474
- const llmSpan = startSpan({
22475
- name: "anthropic.messages.create",
22476
- parent: llmParentSpan,
22477
- spanAttributes: {
22478
- type: "llm" /* LLM */
22479
- },
22480
- startTime
22481
- });
23531
+ const llmSpan = startSpan(
23532
+ withSpanInstrumentationName(
23533
+ {
23534
+ name: "anthropic.messages.create",
23535
+ parent: llmParentSpan,
23536
+ spanAttributes: {
23537
+ type: "llm" /* LLM */
23538
+ },
23539
+ startTime
23540
+ },
23541
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
23542
+ )
23543
+ );
22482
23544
  activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
22483
23545
  return llmSpan;
22484
23546
  }
@@ -22747,12 +23809,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
22747
23809
  }
22748
23810
  })();
22749
23811
  }
22750
- const span = startSpan({
22751
- name: "Claude Agent",
22752
- spanAttributes: {
22753
- type: "task" /* TASK */
22754
- }
22755
- });
23812
+ const span = startSpan(
23813
+ withSpanInstrumentationName(
23814
+ {
23815
+ name: "Claude Agent",
23816
+ spanAttributes: {
23817
+ type: "task" /* TASK */
23818
+ }
23819
+ },
23820
+ INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
23821
+ )
23822
+ );
22756
23823
  const startTime = getCurrentUnixTimestamp();
22757
23824
  try {
22758
23825
  span.log({
@@ -22917,24 +23984,28 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
22917
23984
  };
22918
23985
 
22919
23986
  // src/instrumentation/plugins/cursor-sdk-channels.ts
22920
- var cursorSDKChannels = defineChannels("@cursor/sdk", {
22921
- create: channel({
22922
- channelName: "Agent.create",
22923
- kind: "async"
22924
- }),
22925
- resume: channel({
22926
- channelName: "Agent.resume",
22927
- kind: "async"
22928
- }),
22929
- prompt: channel({
22930
- channelName: "Agent.prompt",
22931
- kind: "async"
22932
- }),
22933
- send: channel({
22934
- channelName: "agent.send",
22935
- kind: "async"
22936
- })
22937
- });
23987
+ var cursorSDKChannels = defineChannels(
23988
+ "@cursor/sdk",
23989
+ {
23990
+ create: channel({
23991
+ channelName: "Agent.create",
23992
+ kind: "async"
23993
+ }),
23994
+ resume: channel({
23995
+ channelName: "Agent.resume",
23996
+ kind: "async"
23997
+ }),
23998
+ prompt: channel({
23999
+ channelName: "Agent.prompt",
24000
+ kind: "async"
24001
+ }),
24002
+ send: channel({
24003
+ channelName: "agent.send",
24004
+ kind: "async"
24005
+ })
24006
+ },
24007
+ { instrumentationName: INSTRUMENTATION_NAMES.CURSOR_SDK }
24008
+ );
22938
24009
 
22939
24010
  // src/instrumentation/plugins/cursor-sdk-plugin.ts
22940
24011
  var PATCHED_AGENT = /* @__PURE__ */ Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
@@ -22986,10 +24057,15 @@ var CursorSDKPlugin = class extends BasePlugin {
22986
24057
  provider: "cursor",
22987
24058
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
22988
24059
  };
22989
- const span = startSpan({
22990
- name: "Cursor Agent",
22991
- spanAttributes: { type: "task" /* TASK */ }
22992
- });
24060
+ const span = startSpan(
24061
+ withSpanInstrumentationName(
24062
+ {
24063
+ name: "Cursor Agent",
24064
+ spanAttributes: { type: "task" /* TASK */ }
24065
+ },
24066
+ INSTRUMENTATION_NAMES.CURSOR_SDK
24067
+ )
24068
+ );
22993
24069
  const startTime = getCurrentUnixTimestamp();
22994
24070
  safeLog2(span, {
22995
24071
  input: sanitizeUserMessage(message),
@@ -23051,10 +24127,15 @@ var CursorSDKPlugin = class extends BasePlugin {
23051
24127
  provider: "cursor",
23052
24128
  ...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
23053
24129
  };
23054
- const span = startSpan({
23055
- name: "Cursor Agent",
23056
- spanAttributes: { type: "task" /* TASK */ }
23057
- });
24130
+ const span = startSpan(
24131
+ withSpanInstrumentationName(
24132
+ {
24133
+ name: "Cursor Agent",
24134
+ spanAttributes: { type: "task" /* TASK */ }
24135
+ },
24136
+ INSTRUMENTATION_NAMES.CURSOR_SDK
24137
+ )
24138
+ );
23058
24139
  const startTime = getCurrentUnixTimestamp();
23059
24140
  safeLog2(span, {
23060
24141
  input: sanitizeUserMessage(message),
@@ -23501,29 +24582,39 @@ async function startToolSpan(state, args) {
23501
24582
  if (args.truncated?.result !== void 0) {
23502
24583
  metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
23503
24584
  }
23504
- const span = startSpan({
23505
- event: {
23506
- input: args.args,
23507
- metadata
23508
- },
23509
- name: `tool: ${name}`,
23510
- parent: await state.span.export(),
23511
- spanAttributes: { type: "tool" /* TOOL */ }
23512
- });
24585
+ const span = startSpan(
24586
+ withSpanInstrumentationName(
24587
+ {
24588
+ event: {
24589
+ input: args.args,
24590
+ metadata
24591
+ },
24592
+ name: `tool: ${name}`,
24593
+ parent: await state.span.export(),
24594
+ spanAttributes: { type: "tool" /* TOOL */ }
24595
+ },
24596
+ INSTRUMENTATION_NAMES.CURSOR_SDK
24597
+ )
24598
+ );
23513
24599
  let subAgentSpan;
23514
24600
  if (isSubAgentToolName(name)) {
23515
- subAgentSpan = startSpan({
23516
- event: {
23517
- input: args.args,
23518
- metadata: {
23519
- "cursor_sdk.subagent.tool_call_id": args.callId,
23520
- "gen_ai.tool.name": name
23521
- }
23522
- },
23523
- name: formatSubAgentSpanName2(args.toolCall, args.args),
23524
- parent: await span.export(),
23525
- spanAttributes: { type: "task" /* TASK */ }
23526
- });
24601
+ subAgentSpan = startSpan(
24602
+ withSpanInstrumentationName(
24603
+ {
24604
+ event: {
24605
+ input: args.args,
24606
+ metadata: {
24607
+ "cursor_sdk.subagent.tool_call_id": args.callId,
24608
+ "gen_ai.tool.name": name
24609
+ }
24610
+ },
24611
+ name: formatSubAgentSpanName2(args.toolCall, args.args),
24612
+ parent: await span.export(),
24613
+ spanAttributes: { type: "task" /* TASK */ }
24614
+ },
24615
+ INSTRUMENTATION_NAMES.CURSOR_SDK
24616
+ )
24617
+ );
23527
24618
  }
23528
24619
  return { span, subAgentSpan };
23529
24620
  }
@@ -23782,24 +24873,28 @@ function cleanMetrics2(metrics) {
23782
24873
  }
23783
24874
 
23784
24875
  // src/instrumentation/plugins/openai-agents-channels.ts
23785
- var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
23786
- onTraceStart: channel({
23787
- channelName: "tracing.processor.onTraceStart",
23788
- kind: "async"
23789
- }),
23790
- onTraceEnd: channel({
23791
- channelName: "tracing.processor.onTraceEnd",
23792
- kind: "async"
23793
- }),
23794
- onSpanStart: channel({
23795
- channelName: "tracing.processor.onSpanStart",
23796
- kind: "async"
23797
- }),
23798
- onSpanEnd: channel({
23799
- channelName: "tracing.processor.onSpanEnd",
23800
- kind: "async"
23801
- })
23802
- });
24876
+ var openAIAgentsCoreChannels = defineChannels(
24877
+ "@openai/agents-core",
24878
+ {
24879
+ onTraceStart: channel({
24880
+ channelName: "tracing.processor.onTraceStart",
24881
+ kind: "async"
24882
+ }),
24883
+ onTraceEnd: channel({
24884
+ channelName: "tracing.processor.onTraceEnd",
24885
+ kind: "async"
24886
+ }),
24887
+ onSpanStart: channel({
24888
+ channelName: "tracing.processor.onSpanStart",
24889
+ kind: "async"
24890
+ }),
24891
+ onSpanEnd: channel({
24892
+ channelName: "tracing.processor.onSpanEnd",
24893
+ kind: "async"
24894
+ })
24895
+ },
24896
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENAI_AGENTS }
24897
+ );
23803
24898
 
23804
24899
  // src/instrumentation/plugins/openai-agents-trace-processor.ts
23805
24900
  function isSpanData(spanData, type) {
@@ -23915,16 +25010,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
23915
25010
  this.evictOldestTrace();
23916
25011
  }
23917
25012
  const current = currentSpan();
23918
- const span = current && current !== NOOP_SPAN ? current.startSpan({
23919
- name: trace.name,
23920
- type: "task" /* TASK */
23921
- }) : this.logger ? this.logger.startSpan({
23922
- name: trace.name,
23923
- type: "task" /* TASK */
23924
- }) : startSpan({
23925
- name: trace.name,
23926
- type: "task" /* TASK */
23927
- });
25013
+ const spanArgs = withSpanInstrumentationName(
25014
+ {
25015
+ name: trace.name,
25016
+ type: "task" /* TASK */
25017
+ },
25018
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
25019
+ );
25020
+ const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
25021
+ withSpanInstrumentationName(
25022
+ spanArgs,
25023
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
25024
+ )
25025
+ );
23928
25026
  span.log({
23929
25027
  input: "Agent workflow started",
23930
25028
  metadata: {
@@ -23975,10 +25073,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
23975
25073
  if (!parentSpan) {
23976
25074
  return Promise.resolve();
23977
25075
  }
23978
- const childSpan = parentSpan.startSpan({
23979
- name: spanNameFromAgents(span),
23980
- type: spanTypeFromAgents(span)
23981
- });
25076
+ const childSpan = parentSpan.startSpan(
25077
+ withSpanInstrumentationName(
25078
+ {
25079
+ name: spanNameFromAgents(span),
25080
+ type: spanTypeFromAgents(span)
25081
+ },
25082
+ INSTRUMENTATION_NAMES.OPENAI_AGENTS
25083
+ )
25084
+ );
23982
25085
  traceData.childSpans.set(span.spanId, childSpan);
23983
25086
  return Promise.resolve();
23984
25087
  }
@@ -24243,24 +25346,28 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
24243
25346
  };
24244
25347
 
24245
25348
  // src/instrumentation/plugins/google-genai-channels.ts
24246
- var googleGenAIChannels = defineChannels("@google/genai", {
24247
- generateContent: channel({
24248
- channelName: "models.generateContent",
24249
- kind: "async"
24250
- }),
24251
- generateContentStream: channel({
24252
- channelName: "models.generateContentStream",
24253
- kind: "async"
24254
- }),
24255
- embedContent: channel({
24256
- channelName: "models.embedContent",
24257
- kind: "async"
24258
- }),
24259
- interactionsCreate: channel({
24260
- channelName: "interactions.create",
24261
- kind: "async"
24262
- })
24263
- });
25349
+ var googleGenAIChannels = defineChannels(
25350
+ "@google/genai",
25351
+ {
25352
+ generateContent: channel({
25353
+ channelName: "models.generateContent",
25354
+ kind: "async"
25355
+ }),
25356
+ generateContentStream: channel({
25357
+ channelName: "models.generateContentStream",
25358
+ kind: "async"
25359
+ }),
25360
+ embedContent: channel({
25361
+ channelName: "models.embedContent",
25362
+ kind: "async"
25363
+ }),
25364
+ interactionsCreate: channel({
25365
+ channelName: "interactions.create",
25366
+ kind: "async"
25367
+ })
25368
+ },
25369
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENAI }
25370
+ );
24264
25371
 
24265
25372
  // src/instrumentation/plugins/google-genai-plugin.ts
24266
25373
  var GOOGLE_GENAI_INTERNAL_CONTEXT = {
@@ -24298,13 +25405,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
24298
25405
  const params = event.arguments[0];
24299
25406
  const input = serializeGenerateContentInput(params);
24300
25407
  const metadata = extractGenerateContentMetadata(params);
24301
- const span = startSpan({
24302
- name: "generate_content",
24303
- spanAttributes: {
24304
- type: "llm" /* LLM */
24305
- },
24306
- event: createWrapperParityEvent({ input, metadata })
24307
- });
25408
+ const span = startSpan(
25409
+ withSpanInstrumentationName(
25410
+ {
25411
+ name: "generate_content",
25412
+ spanAttributes: {
25413
+ type: "llm" /* LLM */
25414
+ },
25415
+ event: createWrapperParityEvent({ input, metadata })
25416
+ },
25417
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
25418
+ )
25419
+ );
24308
25420
  return {
24309
25421
  span,
24310
25422
  startTime: getCurrentUnixTimestamp()
@@ -24317,13 +25429,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
24317
25429
  const params = event.arguments[0];
24318
25430
  const input = serializeGenerateContentInput(params);
24319
25431
  const metadata = extractGenerateContentMetadata(params);
24320
- const span = startSpan({
24321
- name: "generate_content",
24322
- spanAttributes: {
24323
- type: "llm" /* LLM */
24324
- },
24325
- event: createWrapperParityEvent({ input, metadata })
24326
- });
25432
+ const span = startSpan(
25433
+ withSpanInstrumentationName(
25434
+ {
25435
+ name: "generate_content",
25436
+ spanAttributes: {
25437
+ type: "llm" /* LLM */
25438
+ },
25439
+ event: createWrapperParityEvent({ input, metadata })
25440
+ },
25441
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
25442
+ )
25443
+ );
24327
25444
  return {
24328
25445
  span,
24329
25446
  startTime: getCurrentUnixTimestamp()
@@ -24399,13 +25516,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
24399
25516
  const params = event.arguments[0];
24400
25517
  const input = serializeEmbedContentInput(params);
24401
25518
  const metadata = extractEmbedContentMetadata(params);
24402
- const span = startSpan({
24403
- name: "embed_content",
24404
- spanAttributes: {
24405
- type: "llm" /* LLM */
24406
- },
24407
- event: createWrapperParityEvent({ input, metadata })
24408
- });
25519
+ const span = startSpan(
25520
+ withSpanInstrumentationName(
25521
+ {
25522
+ name: "embed_content",
25523
+ spanAttributes: {
25524
+ type: "llm" /* LLM */
25525
+ },
25526
+ event: createWrapperParityEvent({ input, metadata })
25527
+ },
25528
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
25529
+ )
25530
+ );
24409
25531
  return {
24410
25532
  span,
24411
25533
  startTime: getCurrentUnixTimestamp()
@@ -24418,13 +25540,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
24418
25540
  const params = event.arguments[0];
24419
25541
  const input = serializeEmbedContentInput(params);
24420
25542
  const metadata = extractEmbedContentMetadata(params);
24421
- const span = startSpan({
24422
- name: "embed_content",
24423
- spanAttributes: {
24424
- type: "llm" /* LLM */
24425
- },
24426
- event: createWrapperParityEvent({ input, metadata })
24427
- });
25543
+ const span = startSpan(
25544
+ withSpanInstrumentationName(
25545
+ {
25546
+ name: "embed_content",
25547
+ spanAttributes: {
25548
+ type: "llm" /* LLM */
25549
+ },
25550
+ event: createWrapperParityEvent({ input, metadata })
25551
+ },
25552
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
25553
+ )
25554
+ );
24428
25555
  return {
24429
25556
  span,
24430
25557
  startTime: getCurrentUnixTimestamp()
@@ -24535,16 +25662,21 @@ function patchGoogleGenAIStreamingResult(args) {
24535
25662
  const requestStartTime = startTime ?? getCurrentUnixTimestamp();
24536
25663
  const ensureSpan = () => {
24537
25664
  if (!span) {
24538
- span = startSpan({
24539
- name: "generate_content_stream",
24540
- spanAttributes: {
24541
- type: "llm" /* LLM */
24542
- },
24543
- event: {
24544
- input,
24545
- metadata
24546
- }
24547
- });
25665
+ span = startSpan(
25666
+ withSpanInstrumentationName(
25667
+ {
25668
+ name: "generate_content_stream",
25669
+ spanAttributes: {
25670
+ type: "llm" /* LLM */
25671
+ },
25672
+ event: {
25673
+ input,
25674
+ metadata
25675
+ }
25676
+ },
25677
+ INSTRUMENTATION_NAMES.GOOGLE_GENAI
25678
+ )
25679
+ );
24548
25680
  }
24549
25681
  return span;
24550
25682
  };
@@ -25312,28 +26444,32 @@ function tryToDict(obj) {
25312
26444
  }
25313
26445
 
25314
26446
  // src/instrumentation/plugins/huggingface-channels.ts
25315
- var huggingFaceChannels = defineChannels("@huggingface/inference", {
25316
- chatCompletion: channel({
25317
- channelName: "chatCompletion",
25318
- kind: "async"
25319
- }),
25320
- chatCompletionStream: channel({
25321
- channelName: "chatCompletionStream",
25322
- kind: "sync-stream"
25323
- }),
25324
- textGeneration: channel({
25325
- channelName: "textGeneration",
25326
- kind: "async"
25327
- }),
25328
- textGenerationStream: channel({
25329
- channelName: "textGenerationStream",
25330
- kind: "sync-stream"
25331
- }),
25332
- featureExtraction: channel({
25333
- channelName: "featureExtraction",
25334
- kind: "async"
25335
- })
25336
- });
26447
+ var huggingFaceChannels = defineChannels(
26448
+ "@huggingface/inference",
26449
+ {
26450
+ chatCompletion: channel({
26451
+ channelName: "chatCompletion",
26452
+ kind: "async"
26453
+ }),
26454
+ chatCompletionStream: channel({
26455
+ channelName: "chatCompletionStream",
26456
+ kind: "sync-stream"
26457
+ }),
26458
+ textGeneration: channel({
26459
+ channelName: "textGeneration",
26460
+ kind: "async"
26461
+ }),
26462
+ textGenerationStream: channel({
26463
+ channelName: "textGenerationStream",
26464
+ kind: "sync-stream"
26465
+ }),
26466
+ featureExtraction: channel({
26467
+ channelName: "featureExtraction",
26468
+ kind: "async"
26469
+ })
26470
+ },
26471
+ { instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
26472
+ );
25337
26473
 
25338
26474
  // src/instrumentation/plugins/huggingface-plugin.ts
25339
26475
  var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
@@ -25760,20 +26896,24 @@ function extractTextGenerationStreamMetadata(chunks) {
25760
26896
  }
25761
26897
 
25762
26898
  // src/instrumentation/plugins/openrouter-agent-channels.ts
25763
- var openRouterAgentChannels = defineChannels("@openrouter/agent", {
25764
- callModel: channel({
25765
- channelName: "callModel",
25766
- kind: "sync-stream"
25767
- }),
25768
- callModelTurn: channel({
25769
- channelName: "callModel.turn",
25770
- kind: "async"
25771
- }),
25772
- toolExecute: channel({
25773
- channelName: "tool.execute",
25774
- kind: "async"
25775
- })
25776
- });
26899
+ var openRouterAgentChannels = defineChannels(
26900
+ "@openrouter/agent",
26901
+ {
26902
+ callModel: channel({
26903
+ channelName: "callModel",
26904
+ kind: "sync-stream"
26905
+ }),
26906
+ callModelTurn: channel({
26907
+ channelName: "callModel.turn",
26908
+ kind: "async"
26909
+ }),
26910
+ toolExecute: channel({
26911
+ channelName: "tool.execute",
26912
+ kind: "async"
26913
+ })
26914
+ },
26915
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER_AGENT }
26916
+ );
25777
26917
 
25778
26918
  // src/instrumentation/plugins/openrouter-agent-plugin.ts
25779
26919
  var OpenRouterAgentPlugin = class extends BasePlugin {
@@ -26543,38 +27683,40 @@ function normalizeError(error2) {
26543
27683
  }
26544
27684
 
26545
27685
  // src/instrumentation/plugins/openrouter-channels.ts
26546
- var openRouterChannels = defineChannels("@openrouter/sdk", {
26547
- chatSend: channel({
26548
- channelName: "chat.send",
26549
- kind: "async"
26550
- }),
26551
- embeddingsGenerate: channel({
26552
- channelName: "embeddings.generate",
26553
- kind: "async"
26554
- }),
26555
- rerankRerank: channel(
26556
- {
27686
+ var openRouterChannels = defineChannels(
27687
+ "@openrouter/sdk",
27688
+ {
27689
+ chatSend: channel({
27690
+ channelName: "chat.send",
27691
+ kind: "async"
27692
+ }),
27693
+ embeddingsGenerate: channel({
27694
+ channelName: "embeddings.generate",
27695
+ kind: "async"
27696
+ }),
27697
+ rerankRerank: channel({
26557
27698
  channelName: "rerank.rerank",
26558
27699
  kind: "async"
26559
- }
26560
- ),
26561
- betaResponsesSend: channel({
26562
- channelName: "beta.responses.send",
26563
- kind: "async"
26564
- }),
26565
- callModel: channel({
26566
- channelName: "callModel",
26567
- kind: "sync-stream"
26568
- }),
26569
- callModelTurn: channel({
26570
- channelName: "callModel.turn",
26571
- kind: "async"
26572
- }),
26573
- toolExecute: channel({
26574
- channelName: "tool.execute",
26575
- kind: "async"
26576
- })
26577
- });
27700
+ }),
27701
+ betaResponsesSend: channel({
27702
+ channelName: "beta.responses.send",
27703
+ kind: "async"
27704
+ }),
27705
+ callModel: channel({
27706
+ channelName: "callModel",
27707
+ kind: "sync-stream"
27708
+ }),
27709
+ callModelTurn: channel({
27710
+ channelName: "callModel.turn",
27711
+ kind: "async"
27712
+ }),
27713
+ toolExecute: channel({
27714
+ channelName: "tool.execute",
27715
+ kind: "async"
27716
+ })
27717
+ },
27718
+ { instrumentationName: INSTRUMENTATION_NAMES.OPENROUTER }
27719
+ );
26578
27720
 
26579
27721
  // src/instrumentation/plugins/openrouter-plugin.ts
26580
27722
  var OpenRouterPlugin = class extends BasePlugin {
@@ -27614,52 +28756,56 @@ function normalizeError2(error2) {
27614
28756
  }
27615
28757
 
27616
28758
  // src/instrumentation/plugins/mistral-channels.ts
27617
- var mistralChannels = defineChannels("@mistralai/mistralai", {
27618
- chatComplete: channel({
27619
- channelName: "chat.complete",
27620
- kind: "async"
27621
- }),
27622
- chatStream: channel({
27623
- channelName: "chat.stream",
27624
- kind: "async"
27625
- }),
27626
- embeddingsCreate: channel({
27627
- channelName: "embeddings.create",
27628
- kind: "async"
27629
- }),
27630
- classifiersModerate: channel({
27631
- channelName: "classifiers.moderate",
27632
- kind: "async"
27633
- }),
27634
- classifiersModerateChat: channel({
27635
- channelName: "classifiers.moderateChat",
27636
- kind: "async"
27637
- }),
27638
- classifiersClassify: channel({
27639
- channelName: "classifiers.classify",
27640
- kind: "async"
27641
- }),
27642
- classifiersClassifyChat: channel({
27643
- channelName: "classifiers.classifyChat",
27644
- kind: "async"
27645
- }),
27646
- fimComplete: channel({
27647
- channelName: "fim.complete",
27648
- kind: "async"
27649
- }),
27650
- fimStream: channel({
27651
- channelName: "fim.stream",
27652
- kind: "async"
27653
- }),
27654
- agentsComplete: channel({
27655
- channelName: "agents.complete",
27656
- kind: "async"
27657
- }),
27658
- agentsStream: channel({
27659
- channelName: "agents.stream",
27660
- kind: "async"
27661
- })
27662
- });
28759
+ var mistralChannels = defineChannels(
28760
+ "@mistralai/mistralai",
28761
+ {
28762
+ chatComplete: channel({
28763
+ channelName: "chat.complete",
28764
+ kind: "async"
28765
+ }),
28766
+ chatStream: channel({
28767
+ channelName: "chat.stream",
28768
+ kind: "async"
28769
+ }),
28770
+ embeddingsCreate: channel({
28771
+ channelName: "embeddings.create",
28772
+ kind: "async"
28773
+ }),
28774
+ classifiersModerate: channel({
28775
+ channelName: "classifiers.moderate",
28776
+ kind: "async"
28777
+ }),
28778
+ classifiersModerateChat: channel({
28779
+ channelName: "classifiers.moderateChat",
28780
+ kind: "async"
28781
+ }),
28782
+ classifiersClassify: channel({
28783
+ channelName: "classifiers.classify",
28784
+ kind: "async"
28785
+ }),
28786
+ classifiersClassifyChat: channel({
28787
+ channelName: "classifiers.classifyChat",
28788
+ kind: "async"
28789
+ }),
28790
+ fimComplete: channel({
28791
+ channelName: "fim.complete",
28792
+ kind: "async"
28793
+ }),
28794
+ fimStream: channel({
28795
+ channelName: "fim.stream",
28796
+ kind: "async"
28797
+ }),
28798
+ agentsComplete: channel({
28799
+ channelName: "agents.complete",
28800
+ kind: "async"
28801
+ }),
28802
+ agentsStream: channel({
28803
+ channelName: "agents.stream",
28804
+ kind: "async"
28805
+ })
28806
+ },
28807
+ { instrumentationName: INSTRUMENTATION_NAMES.MISTRAL }
28808
+ );
27663
28809
 
27664
28810
  // src/instrumentation/plugins/mistral-plugin.ts
27665
28811
  var MistralPlugin = class extends BasePlugin {
@@ -28279,20 +29425,24 @@ function aggregateMistralStreamChunks(chunks) {
28279
29425
  }
28280
29426
 
28281
29427
  // src/instrumentation/plugins/google-adk-channels.ts
28282
- var googleADKChannels = defineChannels("@google/adk", {
28283
- runnerRunAsync: channel({
28284
- channelName: "runner.runAsync",
28285
- kind: "sync-stream"
28286
- }),
28287
- agentRunAsync: channel({
28288
- channelName: "agent.runAsync",
28289
- kind: "sync-stream"
28290
- }),
28291
- toolRunAsync: channel({
28292
- channelName: "tool.runAsync",
28293
- kind: "async"
28294
- })
28295
- });
29428
+ var googleADKChannels = defineChannels(
29429
+ "@google/adk",
29430
+ {
29431
+ runnerRunAsync: channel({
29432
+ channelName: "runner.runAsync",
29433
+ kind: "sync-stream"
29434
+ }),
29435
+ agentRunAsync: channel({
29436
+ channelName: "agent.runAsync",
29437
+ kind: "sync-stream"
29438
+ }),
29439
+ toolRunAsync: channel({
29440
+ channelName: "tool.runAsync",
29441
+ kind: "async"
29442
+ })
29443
+ },
29444
+ { instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_ADK }
29445
+ );
28296
29446
 
28297
29447
  // src/instrumentation/plugins/google-adk-plugin.ts
28298
29448
  var GoogleADKPlugin = class extends BasePlugin {
@@ -28317,12 +29467,17 @@ var GoogleADKPlugin = class extends BasePlugin {
28317
29467
  const createState = (event) => {
28318
29468
  const params = event.arguments[0] ?? {};
28319
29469
  const contextKey = extractRunnerContextKey(params);
28320
- const span = startSpan({
28321
- name: "Google ADK Runner",
28322
- spanAttributes: {
28323
- type: "task" /* TASK */
28324
- }
28325
- });
29470
+ const span = startSpan(
29471
+ withSpanInstrumentationName(
29472
+ {
29473
+ name: "Google ADK Runner",
29474
+ spanAttributes: {
29475
+ type: "task" /* TASK */
29476
+ }
29477
+ },
29478
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
29479
+ )
29480
+ );
28326
29481
  const startTime = getCurrentUnixTimestamp();
28327
29482
  try {
28328
29483
  const metadata = extractRunnerMetadata(params);
@@ -28408,18 +29563,23 @@ var GoogleADKPlugin = class extends BasePlugin {
28408
29563
  this.activeRunnerSpans
28409
29564
  );
28410
29565
  const contextKey = extractInvocationContextKey(parentContext);
28411
- const span = startSpan({
28412
- name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
28413
- spanAttributes: {
28414
- type: "task" /* TASK */
28415
- },
28416
- ...runnerParentSpan ? {
28417
- parentSpanIds: {
28418
- spanId: runnerParentSpan.spanId,
28419
- rootSpanId: runnerParentSpan.rootSpanId
28420
- }
28421
- } : {}
28422
- });
29566
+ const span = startSpan(
29567
+ withSpanInstrumentationName(
29568
+ {
29569
+ name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
29570
+ spanAttributes: {
29571
+ type: "task" /* TASK */
29572
+ },
29573
+ ...runnerParentSpan ? {
29574
+ parentSpanIds: {
29575
+ spanId: runnerParentSpan.spanId,
29576
+ rootSpanId: runnerParentSpan.rootSpanId
29577
+ }
29578
+ } : {}
29579
+ },
29580
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
29581
+ )
29582
+ );
28423
29583
  const startTime = getCurrentUnixTimestamp();
28424
29584
  try {
28425
29585
  const metadata = {
@@ -28512,22 +29672,27 @@ var GoogleADKPlugin = class extends BasePlugin {
28512
29672
  this.activeAgentSpans,
28513
29673
  this.activeRunnerSpans
28514
29674
  );
28515
- const createSpan = () => startSpan({
28516
- name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
28517
- spanAttributes: {
28518
- type: "tool" /* TOOL */
28519
- },
28520
- event: {
28521
- input: req.args,
28522
- metadata: {
28523
- provider: "google-adk",
28524
- ...toolName && { "google_adk.tool_name": toolName },
28525
- ...extractToolCallId(req) && {
28526
- "google_adk.tool_call_id": extractToolCallId(req)
29675
+ const createSpan = () => startSpan(
29676
+ withSpanInstrumentationName(
29677
+ {
29678
+ name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
29679
+ spanAttributes: {
29680
+ type: "tool" /* TOOL */
29681
+ },
29682
+ event: {
29683
+ input: req.args,
29684
+ metadata: {
29685
+ provider: "google-adk",
29686
+ ...toolName && { "google_adk.tool_name": toolName },
29687
+ ...extractToolCallId(req) && {
29688
+ "google_adk.tool_call_id": extractToolCallId(req)
29689
+ }
29690
+ }
28527
29691
  }
28528
- }
28529
- }
28530
- });
29692
+ },
29693
+ INSTRUMENTATION_NAMES.GOOGLE_ADK
29694
+ )
29695
+ );
28531
29696
  const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
28532
29697
  const startTime = getCurrentUnixTimestamp();
28533
29698
  states.set(event, { span, startTime });
@@ -28926,24 +30091,28 @@ function cleanMetrics4(metrics) {
28926
30091
  }
28927
30092
 
28928
30093
  // src/instrumentation/plugins/cohere-channels.ts
28929
- var cohereChannels = defineChannels("cohere-ai", {
28930
- chat: channel({
28931
- channelName: "chat",
28932
- kind: "async"
28933
- }),
28934
- chatStream: channel({
28935
- channelName: "chatStream",
28936
- kind: "async"
28937
- }),
28938
- embed: channel({
28939
- channelName: "embed",
28940
- kind: "async"
28941
- }),
28942
- rerank: channel({
28943
- channelName: "rerank",
28944
- kind: "async"
28945
- })
28946
- });
30094
+ var cohereChannels = defineChannels(
30095
+ "cohere-ai",
30096
+ {
30097
+ chat: channel({
30098
+ channelName: "chat",
30099
+ kind: "async"
30100
+ }),
30101
+ chatStream: channel({
30102
+ channelName: "chatStream",
30103
+ kind: "async"
30104
+ }),
30105
+ embed: channel({
30106
+ channelName: "embed",
30107
+ kind: "async"
30108
+ }),
30109
+ rerank: channel({
30110
+ channelName: "rerank",
30111
+ kind: "async"
30112
+ })
30113
+ },
30114
+ { instrumentationName: INSTRUMENTATION_NAMES.COHERE }
30115
+ );
28947
30116
 
28948
30117
  // src/instrumentation/plugins/cohere-plugin.ts
28949
30118
  var CoherePlugin = class extends BasePlugin {
@@ -29612,18 +30781,20 @@ function aggregateCohereChatStreamChunks(chunks) {
29612
30781
  }
29613
30782
 
29614
30783
  // src/instrumentation/plugins/groq-channels.ts
29615
- var groqChannels = defineChannels("groq-sdk", {
29616
- chatCompletionsCreate: channel({
29617
- channelName: "chat.completions.create",
29618
- kind: "async"
29619
- }),
29620
- embeddingsCreate: channel(
29621
- {
30784
+ var groqChannels = defineChannels(
30785
+ "groq-sdk",
30786
+ {
30787
+ chatCompletionsCreate: channel({
30788
+ channelName: "chat.completions.create",
30789
+ kind: "async"
30790
+ }),
30791
+ embeddingsCreate: channel({
29622
30792
  channelName: "embeddings.create",
29623
30793
  kind: "async"
29624
- }
29625
- )
29626
- });
30794
+ })
30795
+ },
30796
+ { instrumentationName: INSTRUMENTATION_NAMES.GROQ }
30797
+ );
29627
30798
 
29628
30799
  // src/instrumentation/plugins/groq-plugin.ts
29629
30800
  var GroqPlugin = class extends BasePlugin {
@@ -29726,15 +30897,27 @@ var clientSendChannel = channel({
29726
30897
  channelName: "client.send",
29727
30898
  kind: "async"
29728
30899
  });
29729
- var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
29730
- clientSend: clientSendChannel
29731
- });
29732
- var smithyCoreChannels = defineChannels("@smithy/core", {
29733
- clientSend: clientSendChannel
29734
- });
29735
- var smithyClientChannels = defineChannels("@smithy/smithy-client", {
29736
- clientSend: clientSendChannel
29737
- });
30900
+ var bedrockRuntimeChannels = defineChannels(
30901
+ "aws-bedrock-runtime",
30902
+ {
30903
+ clientSend: clientSendChannel
30904
+ },
30905
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
30906
+ );
30907
+ var smithyCoreChannels = defineChannels(
30908
+ "@smithy/core",
30909
+ {
30910
+ clientSend: clientSendChannel
30911
+ },
30912
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
30913
+ );
30914
+ var smithyClientChannels = defineChannels(
30915
+ "@smithy/smithy-client",
30916
+ {
30917
+ clientSend: clientSendChannel
30918
+ },
30919
+ { instrumentationName: INSTRUMENTATION_NAMES.BEDROCK_RUNTIME }
30920
+ );
29738
30921
 
29739
30922
  // src/instrumentation/plugins/bedrock-runtime-common.ts
29740
30923
  var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
@@ -30368,38 +31551,46 @@ function extractTextFromJsonLike(value) {
30368
31551
  }
30369
31552
 
30370
31553
  // src/instrumentation/plugins/genkit-channels.ts
30371
- var genkitChannels = defineChannels("@genkit-ai/ai", {
30372
- generate: channel({
30373
- channelName: "generate",
30374
- kind: "async"
30375
- }),
30376
- generateStream: channel({
30377
- channelName: "generateStream",
30378
- kind: "sync-stream"
30379
- }),
30380
- embed: channel({
30381
- channelName: "embed",
30382
- kind: "async"
30383
- }),
30384
- embedMany: channel({
30385
- channelName: "embedMany",
30386
- kind: "async"
30387
- }),
30388
- actionRun: channel({
30389
- channelName: "action.run",
30390
- kind: "async"
30391
- }),
30392
- actionStream: channel({
30393
- channelName: "action.stream",
30394
- kind: "sync-stream"
30395
- })
30396
- });
30397
- var genkitCoreChannels = defineChannels("@genkit-ai/core", {
30398
- actionSpan: channel({
30399
- channelName: "action.span",
30400
- kind: "async"
30401
- })
30402
- });
31554
+ var genkitChannels = defineChannels(
31555
+ "@genkit-ai/ai",
31556
+ {
31557
+ generate: channel({
31558
+ channelName: "generate",
31559
+ kind: "async"
31560
+ }),
31561
+ generateStream: channel({
31562
+ channelName: "generateStream",
31563
+ kind: "sync-stream"
31564
+ }),
31565
+ embed: channel({
31566
+ channelName: "embed",
31567
+ kind: "async"
31568
+ }),
31569
+ embedMany: channel({
31570
+ channelName: "embedMany",
31571
+ kind: "async"
31572
+ }),
31573
+ actionRun: channel({
31574
+ channelName: "action.run",
31575
+ kind: "async"
31576
+ }),
31577
+ actionStream: channel({
31578
+ channelName: "action.stream",
31579
+ kind: "sync-stream"
31580
+ })
31581
+ },
31582
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
31583
+ );
31584
+ var genkitCoreChannels = defineChannels(
31585
+ "@genkit-ai/core",
31586
+ {
31587
+ actionSpan: channel({
31588
+ channelName: "action.span",
31589
+ kind: "async"
31590
+ })
31591
+ },
31592
+ { instrumentationName: INSTRUMENTATION_NAMES.GENKIT }
31593
+ );
30403
31594
 
30404
31595
  // src/instrumentation/plugins/genkit-plugin.ts
30405
31596
  var GenkitPlugin = class extends BasePlugin {
@@ -30584,12 +31775,17 @@ function startActionSpanState(args) {
30584
31775
  if (!shouldTraceAction(args.metadata, args.runStepName)) {
30585
31776
  return void 0;
30586
31777
  }
30587
- const span = startSpan({
30588
- name: actionSpanName(args.metadata, args.runStepName),
30589
- spanAttributes: {
30590
- type: actionSpanType(args.metadata)
30591
- }
30592
- });
31778
+ const span = startSpan(
31779
+ withSpanInstrumentationName(
31780
+ {
31781
+ name: actionSpanName(args.metadata, args.runStepName),
31782
+ spanAttributes: {
31783
+ type: actionSpanType(args.metadata)
31784
+ }
31785
+ },
31786
+ INSTRUMENTATION_NAMES.GENKIT
31787
+ )
31788
+ );
30593
31789
  const startTime = getCurrentUnixTimestamp();
30594
31790
  span.log({
30595
31791
  input: args.input,
@@ -30950,20 +32146,24 @@ function stringValue(value) {
30950
32146
  }
30951
32147
 
30952
32148
  // src/instrumentation/plugins/github-copilot-channels.ts
30953
- var gitHubCopilotChannels = defineChannels("@github/copilot-sdk", {
30954
- createSession: channel({
30955
- channelName: "client.createSession",
30956
- kind: "async"
30957
- }),
30958
- resumeSession: channel({
30959
- channelName: "client.resumeSession",
30960
- kind: "async"
30961
- }),
30962
- sendAndWait: channel({
30963
- channelName: "session.sendAndWait",
30964
- kind: "async"
30965
- })
30966
- });
32149
+ var gitHubCopilotChannels = defineChannels(
32150
+ "@github/copilot-sdk",
32151
+ {
32152
+ createSession: channel({
32153
+ channelName: "client.createSession",
32154
+ kind: "async"
32155
+ }),
32156
+ resumeSession: channel({
32157
+ channelName: "client.resumeSession",
32158
+ kind: "async"
32159
+ }),
32160
+ sendAndWait: channel({
32161
+ channelName: "session.sendAndWait",
32162
+ kind: "async"
32163
+ })
32164
+ },
32165
+ { instrumentationName: INSTRUMENTATION_NAMES.GITHUB_COPILOT }
32166
+ );
30967
32167
 
30968
32168
  // src/instrumentation/plugins/github-copilot-plugin.ts
30969
32169
  var ROOT_AGENT_KEY = "__root__";
@@ -31054,11 +32254,16 @@ async function handleTurnStart(state, agentId) {
31054
32254
  return;
31055
32255
  }
31056
32256
  const parentId = await getParentIdForAgent(state, agentId);
31057
- const span = startSpan({
31058
- name: "Copilot Turn",
31059
- parent: parentId,
31060
- spanAttributes: { type: "task" /* TASK */ }
31061
- });
32257
+ const span = startSpan(
32258
+ withSpanInstrumentationName(
32259
+ {
32260
+ name: "Copilot Turn",
32261
+ parent: parentId,
32262
+ spanAttributes: { type: "task" /* TASK */ }
32263
+ },
32264
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
32265
+ )
32266
+ );
31062
32267
  const pendingUserMessage = state.pendingUserMessages.get(key);
31063
32268
  if (pendingUserMessage) {
31064
32269
  span.log({ input: pendingUserMessage });
@@ -31089,11 +32294,16 @@ async function handleUsage(state, agentId, usage) {
31089
32294
  const parentId = turn ? await turn.id : await state.session.id;
31090
32295
  const content = state.currentMessageContent.get(key);
31091
32296
  const { metrics, metadata } = extractMetricsFromUsage(usage);
31092
- const llmSpan = startSpan({
31093
- name: "github.copilot.llm",
31094
- parent: parentId,
31095
- spanAttributes: { type: "llm" /* LLM */ }
31096
- });
32297
+ const llmSpan = startSpan(
32298
+ withSpanInstrumentationName(
32299
+ {
32300
+ name: "github.copilot.llm",
32301
+ parent: parentId,
32302
+ spanAttributes: { type: "llm" /* LLM */ }
32303
+ },
32304
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
32305
+ )
32306
+ );
31097
32307
  llmSpan.log({
31098
32308
  output: content ?? void 0,
31099
32309
  metadata,
@@ -31119,11 +32329,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
31119
32329
  }
31120
32330
  const parentId = await getToolParentId(state, agentId);
31121
32331
  const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
31122
- const span = startSpan({
31123
- name: displayName,
31124
- parent: parentId,
31125
- spanAttributes: { type: "tool" /* TOOL */ }
31126
- });
32332
+ const span = startSpan(
32333
+ withSpanInstrumentationName(
32334
+ {
32335
+ name: displayName,
32336
+ parent: parentId,
32337
+ spanAttributes: { type: "tool" /* TOOL */ }
32338
+ },
32339
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
32340
+ )
32341
+ );
31127
32342
  const metadata = {
31128
32343
  "gen_ai.tool.name": toolName,
31129
32344
  "gen_ai.tool.call.id": toolCallId
@@ -31159,11 +32374,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
31159
32374
  }
31160
32375
  const tool = state.activeTools.get(toolCallId);
31161
32376
  const parentId = tool ? await tool.id : await state.session.id;
31162
- const span = startSpan({
31163
- name: `Agent: ${agentDisplayName}`,
31164
- parent: parentId,
31165
- spanAttributes: { type: "task" /* TASK */ }
31166
- });
32377
+ const span = startSpan(
32378
+ withSpanInstrumentationName(
32379
+ {
32380
+ name: `Agent: ${agentDisplayName}`,
32381
+ parent: parentId,
32382
+ spanAttributes: { type: "task" /* TASK */ }
32383
+ },
32384
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
32385
+ )
32386
+ );
31167
32387
  span.log({
31168
32388
  metadata: {
31169
32389
  "github_copilot.agent_name": agentDisplayName,
@@ -31389,10 +32609,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
31389
32609
  if (!config3 || typeof config3 !== "object") {
31390
32610
  return;
31391
32611
  }
31392
- const sessionSpan = startSpan({
31393
- name: "Copilot Session",
31394
- spanAttributes: { type: "task" /* TASK */ }
31395
- });
32612
+ const sessionSpan = startSpan(
32613
+ withSpanInstrumentationName(
32614
+ {
32615
+ name: "Copilot Session",
32616
+ spanAttributes: { type: "task" /* TASK */ }
32617
+ },
32618
+ INSTRUMENTATION_NAMES.GITHUB_COPILOT
32619
+ )
32620
+ );
31396
32621
  const metadata = {};
31397
32622
  if (config3.model) {
31398
32623
  metadata["github_copilot.model"] = config3.model;
@@ -31480,12 +32705,16 @@ var GitHubCopilotPlugin = class extends BasePlugin {
31480
32705
  };
31481
32706
 
31482
32707
  // src/instrumentation/plugins/flue-channels.ts
31483
- var flueChannels = defineChannels("@flue/runtime", {
31484
- createContext: channel({
31485
- channelName: "createFlueContext",
31486
- kind: "sync-stream"
31487
- })
31488
- });
32708
+ var flueChannels = defineChannels(
32709
+ "@flue/runtime",
32710
+ {
32711
+ createContext: channel({
32712
+ channelName: "createFlueContext",
32713
+ kind: "sync-stream"
32714
+ })
32715
+ },
32716
+ { instrumentationName: INSTRUMENTATION_NAMES.FLUE }
32717
+ );
31489
32718
 
31490
32719
  // src/instrumentation/plugins/flue-plugin.ts
31491
32720
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
@@ -31828,15 +33057,20 @@ var FlueObserveBridge = class {
31828
33057
  safeLog3(existing.span, { input, metadata });
31829
33058
  return;
31830
33059
  }
31831
- const span = startSpan({
31832
- name: `workflow:${workflowName}`,
31833
- spanAttributes: { type: "task" /* TASK */ },
31834
- startTime: eventTime(event.startedAt ?? event.timestamp),
31835
- event: {
31836
- input,
31837
- metadata
31838
- }
31839
- });
33060
+ const span = startSpan(
33061
+ withSpanInstrumentationName(
33062
+ {
33063
+ name: `workflow:${workflowName}`,
33064
+ spanAttributes: { type: "task" /* TASK */ },
33065
+ startTime: eventTime(event.startedAt ?? event.timestamp),
33066
+ event: {
33067
+ input,
33068
+ metadata
33069
+ }
33070
+ },
33071
+ INSTRUMENTATION_NAMES.FLUE
33072
+ )
33073
+ );
31840
33074
  this.runsById.set(event.runId, { metadata, span });
31841
33075
  }
31842
33076
  handleRunResume(event, ctx) {
@@ -31856,12 +33090,17 @@ var FlueObserveBridge = class {
31856
33090
  safeLog3(existing.span, { metadata });
31857
33091
  return;
31858
33092
  }
31859
- const span = startSpan({
31860
- name: `workflow:${workflowName}`,
31861
- spanAttributes: { type: "task" /* TASK */ },
31862
- startTime: eventTime(event.startedAt ?? event.timestamp),
31863
- event: { metadata }
31864
- });
33093
+ const span = startSpan(
33094
+ withSpanInstrumentationName(
33095
+ {
33096
+ name: `workflow:${workflowName}`,
33097
+ spanAttributes: { type: "task" /* TASK */ },
33098
+ startTime: eventTime(event.startedAt ?? event.timestamp),
33099
+ event: { metadata }
33100
+ },
33101
+ INSTRUMENTATION_NAMES.FLUE
33102
+ )
33103
+ );
31865
33104
  this.runsById.set(event.runId, { metadata, span });
31866
33105
  }
31867
33106
  handleRunEnd(event) {
@@ -32500,7 +33739,14 @@ function stateMatchesRun(state, runId) {
32500
33739
  return state.metadata["flue.run_id"] === runId;
32501
33740
  }
32502
33741
  function startFlueSpan(parent, args) {
32503
- return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
33742
+ return parent ? withCurrent(
33743
+ parent,
33744
+ () => startSpan(
33745
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
33746
+ )
33747
+ ) : startSpan(
33748
+ withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
33749
+ );
32504
33750
  }
32505
33751
  function runWithCurrentSpanStore(span, next) {
32506
33752
  const state = _internalGetGlobalState();
@@ -32590,9 +33836,13 @@ var BraintrustLangChainCallbackHandler = class {
32590
33836
  ...this.options.debug ? { runId, parentRunId } : {}
32591
33837
  }
32592
33838
  };
32593
- let span = parentSpan.startSpan(args);
33839
+ const spanArgs = withSpanInstrumentationName(
33840
+ args,
33841
+ INSTRUMENTATION_NAMES.LANGCHAIN
33842
+ );
33843
+ let span = parentSpan.startSpan(spanArgs);
32594
33844
  if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
32595
- span = initLogger().startSpan(args);
33845
+ span = initLogger().startSpan(spanArgs);
32596
33846
  }
32597
33847
  this.spans.set(runId, span);
32598
33848
  }
@@ -32805,6 +34055,16 @@ function cleanObject(obj) {
32805
34055
  })
32806
34056
  );
32807
34057
  }
34058
+ function normalizeTokenMetrics(obj) {
34059
+ const metrics = cleanObject(obj);
34060
+ if (metrics.total_tokens !== void 0) {
34061
+ metrics.tokens = metrics.total_tokens;
34062
+ } else if (metrics.prompt_tokens !== void 0 && metrics.completion_tokens !== void 0) {
34063
+ metrics.tokens = metrics.prompt_tokens + metrics.completion_tokens;
34064
+ }
34065
+ delete metrics.total_tokens;
34066
+ return metrics;
34067
+ }
32808
34068
  function walkGenerations(response) {
32809
34069
  const result = [];
32810
34070
  const generations = response.generations || [];
@@ -32851,7 +34111,7 @@ function getMetricsFromResponse(response) {
32851
34111
  continue;
32852
34112
  }
32853
34113
  const inputTokenDetails = usageMetadata.input_token_details;
32854
- return cleanObject({
34114
+ return normalizeTokenMetrics({
32855
34115
  total_tokens: usageMetadata.total_tokens,
32856
34116
  prompt_tokens: usageMetadata.input_tokens,
32857
34117
  completion_tokens: usageMetadata.output_tokens,
@@ -32861,7 +34121,7 @@ function getMetricsFromResponse(response) {
32861
34121
  }
32862
34122
  const llmOutput = response.llmOutput || {};
32863
34123
  const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
32864
- return cleanObject({
34124
+ return normalizeTokenMetrics({
32865
34125
  total_tokens: tokenUsage.totalTokens,
32866
34126
  prompt_tokens: tokenUsage.promptTokens,
32867
34127
  completion_tokens: tokenUsage.completionTokens
@@ -32879,16 +34139,20 @@ function isRecord(value) {
32879
34139
  }
32880
34140
 
32881
34141
  // src/instrumentation/plugins/langchain-channels.ts
32882
- var langChainChannels = defineChannels("@langchain/core", {
32883
- configure: channel({
32884
- channelName: "CallbackManager.configure",
32885
- kind: "sync-stream"
32886
- }),
32887
- configureSync: channel({
32888
- channelName: "CallbackManager._configureSync",
32889
- kind: "sync-stream"
32890
- })
32891
- });
34142
+ var langChainChannels = defineChannels(
34143
+ "@langchain/core",
34144
+ {
34145
+ configure: channel({
34146
+ channelName: "CallbackManager.configure",
34147
+ kind: "sync-stream"
34148
+ }),
34149
+ configureSync: channel({
34150
+ channelName: "CallbackManager._configureSync",
34151
+ kind: "sync-stream"
34152
+ })
34153
+ },
34154
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGCHAIN }
34155
+ );
32892
34156
 
32893
34157
  // src/instrumentation/plugins/langchain-plugin.ts
32894
34158
  var LangChainPlugin = class extends BasePlugin {
@@ -32976,20 +34240,24 @@ function isBraintrustHandler(handler) {
32976
34240
  }
32977
34241
 
32978
34242
  // src/instrumentation/plugins/langsmith-channels.ts
32979
- var langSmithChannels = defineChannels("langsmith", {
32980
- createRun: channel({
32981
- channelName: "Client.createRun",
32982
- kind: "async"
32983
- }),
32984
- updateRun: channel({
32985
- channelName: "Client.updateRun",
32986
- kind: "async"
32987
- }),
32988
- batchIngestRuns: channel({
32989
- channelName: "Client.batchIngestRuns",
32990
- kind: "async"
32991
- })
32992
- });
34243
+ var langSmithChannels = defineChannels(
34244
+ "langsmith",
34245
+ {
34246
+ createRun: channel({
34247
+ channelName: "Client.createRun",
34248
+ kind: "async"
34249
+ }),
34250
+ updateRun: channel({
34251
+ channelName: "Client.updateRun",
34252
+ kind: "async"
34253
+ }),
34254
+ batchIngestRuns: channel({
34255
+ channelName: "Client.batchIngestRuns",
34256
+ kind: "async"
34257
+ })
34258
+ },
34259
+ { instrumentationName: INSTRUMENTATION_NAMES.LANGSMITH }
34260
+ );
32993
34261
 
32994
34262
  // src/instrumentation/plugins/langsmith-plugin.ts
32995
34263
  var MAX_COMPLETED_RUNS = 1e4;
@@ -33153,19 +34421,24 @@ var LangSmithPlugin = class extends BasePlugin {
33153
34421
  const traceId = stringValue2(ownValue(run2, "trace_id")) ?? id;
33154
34422
  const parentId = stringValue2(ownValue(run2, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run2, "parent_run"), "id"));
33155
34423
  const startTime = timestampSeconds(ownValue(run2, "start_time"));
33156
- return startSpan({
33157
- name: stringValue2(ownValue(run2, "name")) ?? "LangSmith run",
33158
- spanId: id,
33159
- parentSpanIds: {
33160
- parentSpanIds: parentId ? [parentId] : [],
33161
- rootSpanId: traceId
33162
- },
33163
- spanAttributes: {
33164
- type: mapRunType(ownValue(run2, "run_type"))
33165
- },
33166
- ...startTime === void 0 ? {} : { startTime },
33167
- event: { id }
33168
- });
34424
+ return startSpan(
34425
+ withSpanInstrumentationName(
34426
+ {
34427
+ name: stringValue2(ownValue(run2, "name")) ?? "LangSmith run",
34428
+ spanId: id,
34429
+ parentSpanIds: {
34430
+ parentSpanIds: parentId ? [parentId] : [],
34431
+ rootSpanId: traceId
34432
+ },
34433
+ spanAttributes: {
34434
+ type: mapRunType(ownValue(run2, "run_type"))
34435
+ },
34436
+ ...startTime === void 0 ? {} : { startTime },
34437
+ event: { id }
34438
+ },
34439
+ INSTRUMENTATION_NAMES.LANGSMITH
34440
+ )
34441
+ );
33169
34442
  }
33170
34443
  logRun(span, run2, previous, includeOutput) {
33171
34444
  const inputs = preferOwnValue(run2, previous, "inputs");
@@ -33423,7 +34696,8 @@ var piCodingAgentChannels = defineChannels(
33423
34696
  channelName: "AgentSession.prompt",
33424
34697
  kind: "async"
33425
34698
  })
33426
- }
34699
+ },
34700
+ { instrumentationName: INSTRUMENTATION_NAMES.PI_CODING_AGENT }
33427
34701
  );
33428
34702
 
33429
34703
  // src/instrumentation/plugins/pi-coding-agent-plugin.ts
@@ -33507,14 +34781,19 @@ function startPiPromptRun(event, onFinalize) {
33507
34781
  ...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
33508
34782
  ...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
33509
34783
  };
33510
- const span = startSpan({
33511
- event: {
33512
- input: extractPromptInput(event.arguments[0], event.arguments[1]),
33513
- metadata
33514
- },
33515
- name: "AgentSession.prompt",
33516
- spanAttributes: { type: "task" /* TASK */ }
33517
- });
34784
+ const span = startSpan(
34785
+ withSpanInstrumentationName(
34786
+ {
34787
+ event: {
34788
+ input: extractPromptInput(event.arguments[0], event.arguments[1]),
34789
+ metadata
34790
+ },
34791
+ name: "AgentSession.prompt",
34792
+ spanAttributes: { type: "task" /* TASK */ }
34793
+ },
34794
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
34795
+ )
34796
+ );
33518
34797
  const streamPatchState = installPiStreamPatch(agent);
33519
34798
  const options = event.arguments[1];
33520
34799
  const promptText = event.arguments[0];
@@ -33684,15 +34963,20 @@ async function startPiLlmSpan(state, model, context2, options) {
33684
34963
  ...extractToolMetadata(context2.tools),
33685
34964
  "pi_coding_agent.operation": "agent.streamFn"
33686
34965
  };
33687
- const span = startSpan({
33688
- event: {
33689
- input: processInputAttachments(normalizePiContextInput(context2)),
33690
- metadata
33691
- },
33692
- name: getLlmSpanName(model),
33693
- parent: await state.span.export(),
33694
- spanAttributes: { type: "llm" /* LLM */ }
33695
- });
34966
+ const span = startSpan(
34967
+ withSpanInstrumentationName(
34968
+ {
34969
+ event: {
34970
+ input: processInputAttachments(normalizePiContextInput(context2)),
34971
+ metadata
34972
+ },
34973
+ name: getLlmSpanName(model),
34974
+ parent: await state.span.export(),
34975
+ spanAttributes: { type: "llm" /* LLM */ }
34976
+ },
34977
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
34978
+ )
34979
+ );
33696
34980
  const llmState = {
33697
34981
  finalized: false,
33698
34982
  metadata,
@@ -33860,15 +35144,20 @@ async function startPiToolSpan(state, event) {
33860
35144
  "pi_coding_agent.tool.name": event.toolName
33861
35145
  };
33862
35146
  try {
33863
- const span = startSpan({
33864
- event: {
33865
- input: event.args,
33866
- metadata
33867
- },
33868
- name: event.toolName || "tool",
33869
- parent: await state.span.export(),
33870
- spanAttributes: { type: "tool" /* TOOL */ }
33871
- });
35147
+ const span = startSpan(
35148
+ withSpanInstrumentationName(
35149
+ {
35150
+ event: {
35151
+ input: event.args,
35152
+ metadata
35153
+ },
35154
+ name: event.toolName || "tool",
35155
+ parent: await state.span.export(),
35156
+ spanAttributes: { type: "tool" /* TOOL */ }
35157
+ },
35158
+ INSTRUMENTATION_NAMES.PI_CODING_AGENT
35159
+ )
35160
+ );
33872
35161
  state.activeToolSpans.set(event.toolCallId, {
33873
35162
  restoreAutoInstrumentation,
33874
35163
  span
@@ -34300,20 +35589,24 @@ function logInstrumentationError4(context2, error2) {
34300
35589
  }
34301
35590
 
34302
35591
  // src/instrumentation/plugins/strands-agent-sdk-channels.ts
34303
- var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
34304
- agentStream: channel({
34305
- channelName: "Agent.stream",
34306
- kind: "sync-stream"
34307
- }),
34308
- graphStream: channel({
34309
- channelName: "Graph.stream",
34310
- kind: "sync-stream"
34311
- }),
34312
- swarmStream: channel({
34313
- channelName: "Swarm.stream",
34314
- kind: "sync-stream"
34315
- })
34316
- });
35592
+ var strandsAgentSDKChannels = defineChannels(
35593
+ "@strands-agents/sdk",
35594
+ {
35595
+ agentStream: channel({
35596
+ channelName: "Agent.stream",
35597
+ kind: "sync-stream"
35598
+ }),
35599
+ graphStream: channel({
35600
+ channelName: "Graph.stream",
35601
+ kind: "sync-stream"
35602
+ }),
35603
+ swarmStream: channel({
35604
+ channelName: "Swarm.stream",
35605
+ kind: "sync-stream"
35606
+ })
35607
+ },
35608
+ { instrumentationName: INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK }
35609
+ );
34317
35610
 
34318
35611
  // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
34319
35612
  var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
@@ -34468,22 +35761,32 @@ function startAgentStream(event, activeChildParents) {
34468
35761
  );
34469
35762
  const span = parentSpan ? withCurrent(
34470
35763
  parentSpan,
34471
- () => startSpan({
34472
- event: {
34473
- input,
34474
- metadata
35764
+ () => startSpan(
35765
+ withSpanInstrumentationName(
35766
+ {
35767
+ event: {
35768
+ input,
35769
+ metadata
35770
+ },
35771
+ name: formatAgentSpanName(agent),
35772
+ spanAttributes: { type: "task" /* TASK */ }
35773
+ },
35774
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
35775
+ )
35776
+ )
35777
+ ) : startSpan(
35778
+ withSpanInstrumentationName(
35779
+ {
35780
+ event: {
35781
+ input,
35782
+ metadata
35783
+ },
35784
+ name: formatAgentSpanName(agent),
35785
+ spanAttributes: { type: "task" /* TASK */ }
34475
35786
  },
34476
- name: formatAgentSpanName(agent),
34477
- spanAttributes: { type: "task" /* TASK */ }
34478
- })
34479
- ) : startSpan({
34480
- event: {
34481
- input,
34482
- metadata
34483
- },
34484
- name: formatAgentSpanName(agent),
34485
- spanAttributes: { type: "task" /* TASK */ }
34486
- });
35787
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
35788
+ )
35789
+ );
34487
35790
  return {
34488
35791
  activeTools: /* @__PURE__ */ new Map(),
34489
35792
  attachmentCache,
@@ -34505,22 +35808,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
34505
35808
  const input = processStrandsInputAttachments(event.arguments[0]);
34506
35809
  const span = parentSpan ? withCurrent(
34507
35810
  parentSpan,
34508
- () => startSpan({
34509
- event: {
34510
- input,
34511
- metadata
35811
+ () => startSpan(
35812
+ withSpanInstrumentationName(
35813
+ {
35814
+ event: {
35815
+ input,
35816
+ metadata
35817
+ },
35818
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
35819
+ spanAttributes: { type: "task" /* TASK */ }
35820
+ },
35821
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
35822
+ )
35823
+ )
35824
+ ) : startSpan(
35825
+ withSpanInstrumentationName(
35826
+ {
35827
+ event: {
35828
+ input,
35829
+ metadata
35830
+ },
35831
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
35832
+ spanAttributes: { type: "task" /* TASK */ }
34512
35833
  },
34513
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
34514
- spanAttributes: { type: "task" /* TASK */ }
34515
- })
34516
- ) : startSpan({
34517
- event: {
34518
- input,
34519
- metadata
34520
- },
34521
- name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
34522
- spanAttributes: { type: "task" /* TASK */ }
34523
- });
35834
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
35835
+ )
35836
+ );
34524
35837
  return {
34525
35838
  activeNodes: /* @__PURE__ */ new Map(),
34526
35839
  finalized: false,
@@ -34622,17 +35935,22 @@ function startModelSpan(state, event) {
34622
35935
  };
34623
35936
  const span = withCurrent(
34624
35937
  state.span,
34625
- () => startSpan({
34626
- event: {
34627
- input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
34628
- event.agent.messages,
34629
- state.attachmentCache
34630
- ) : void 0,
34631
- metadata
34632
- },
34633
- name: formatModelSpanName(model),
34634
- spanAttributes: { type: "llm" /* LLM */ }
34635
- })
35938
+ () => startSpan(
35939
+ withSpanInstrumentationName(
35940
+ {
35941
+ event: {
35942
+ input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
35943
+ event.agent.messages,
35944
+ state.attachmentCache
35945
+ ) : void 0,
35946
+ metadata
35947
+ },
35948
+ name: formatModelSpanName(model),
35949
+ spanAttributes: { type: "llm" /* LLM */ }
35950
+ },
35951
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
35952
+ )
35953
+ )
34636
35954
  );
34637
35955
  state.activeModel = {
34638
35956
  metadata,
@@ -34684,20 +36002,25 @@ function startToolSpan2(state, event) {
34684
36002
  const name = extractToolName3(toolUse, event.tool);
34685
36003
  const span = withCurrent(
34686
36004
  state.span,
34687
- () => startSpan({
34688
- event: {
34689
- input: toolUse?.input,
34690
- metadata: {
34691
- "gen_ai.tool.call.id": toolUse?.toolUseId,
34692
- "gen_ai.tool.name": name,
34693
- "strands.operation": "tool.call",
34694
- "strands.tool.name": name,
34695
- provider: "strands"
34696
- }
34697
- },
34698
- name: `tool: ${name}`,
34699
- spanAttributes: { type: "tool" /* TOOL */ }
34700
- })
36005
+ () => startSpan(
36006
+ withSpanInstrumentationName(
36007
+ {
36008
+ event: {
36009
+ input: toolUse?.input,
36010
+ metadata: {
36011
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
36012
+ "gen_ai.tool.name": name,
36013
+ "strands.operation": "tool.call",
36014
+ "strands.tool.name": name,
36015
+ provider: "strands"
36016
+ }
36017
+ },
36018
+ name: `tool: ${name}`,
36019
+ spanAttributes: { type: "tool" /* TOOL */ }
36020
+ },
36021
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
36022
+ )
36023
+ )
34701
36024
  );
34702
36025
  state.activeTools.set(key, {
34703
36026
  span,
@@ -34768,11 +36091,16 @@ function startNodeSpan(state, event, activeChildParents) {
34768
36091
  };
34769
36092
  const span = withCurrent(
34770
36093
  state.span,
34771
- () => startSpan({
34772
- event: { metadata },
34773
- name: `node: ${nodeId}`,
34774
- spanAttributes: { type: "task" /* TASK */ }
34775
- })
36094
+ () => startSpan(
36095
+ withSpanInstrumentationName(
36096
+ {
36097
+ event: { metadata },
36098
+ name: `node: ${nodeId}`,
36099
+ spanAttributes: { type: "task" /* TASK */ }
36100
+ },
36101
+ INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
36102
+ )
36103
+ )
34776
36104
  );
34777
36105
  const nodeState = {
34778
36106
  ...child ? { child } : {},
@@ -35886,17 +37214,20 @@ var BraintrustObservabilityExporter = class {
35886
37214
  }
35887
37215
  onStart(exported) {
35888
37216
  if (this.spans.has(exported.id)) return;
35889
- const args = {
35890
- name: exported.name,
35891
- spanAttributes: { type: spanTypeFor(exported.type) },
35892
- startTime: epochSeconds(exported.startTime),
35893
- // Use the Mastra span id as the Braintrust row id so that
35894
- // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
35895
- // attach to the right row. Without this, `SpanImpl` auto-generates a
35896
- // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
35897
- // external caller could know.
35898
- event: { id: exported.id }
35899
- };
37217
+ const args = withSpanInstrumentationName(
37218
+ {
37219
+ name: exported.name,
37220
+ spanAttributes: { type: spanTypeFor(exported.type) },
37221
+ startTime: epochSeconds(exported.startTime),
37222
+ // Use the Mastra span id as the Braintrust row id so that
37223
+ // `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
37224
+ // attach to the right row. Without this, `SpanImpl` auto-generates a
37225
+ // row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
37226
+ // external caller could know.
37227
+ event: { id: exported.id }
37228
+ },
37229
+ INSTRUMENTATION_NAMES.MASTRA
37230
+ );
35900
37231
  const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
35901
37232
  if (!this.capturedParent) {
35902
37233
  const probe = currentSpan();
@@ -35971,7 +37302,13 @@ function configureNode() {
35971
37302
  isomorph_default.getPastNAncestors = getPastNAncestors;
35972
37303
  isomorph_default.getEnv = (name) => {
35973
37304
  const value = process.env[name];
35974
- return name === "BRAINTRUST_API_KEY" && !value?.trim() ? void 0 : value;
37305
+ if (name === "BRAINTRUST_API_KEY") {
37306
+ return value?.trim() ? value : void 0;
37307
+ }
37308
+ if ((name === "BRAINTRUST_ENVIRONMENT_TYPE" || name === "BRAINTRUST_ENVIRONMENT_NAME") && !value?.trim()) {
37309
+ return getNearestBraintrustEnvValue(name);
37310
+ }
37311
+ return value;
35975
37312
  };
35976
37313
  isomorph_default.getBraintrustApiKey = async () => {
35977
37314
  const value = process.env.BRAINTRUST_API_KEY;
@@ -36047,6 +37384,12 @@ function configureNode() {
36047
37384
  isomorph_default.gzip = (0, import_node_util3.promisify)(zlib.gzip);
36048
37385
  isomorph_default.gunzip = (0, import_node_util3.promisify)(zlib.gunzip);
36049
37386
  isomorph_default.hash = (data) => crypto.createHash("sha256").update(data).digest("hex");
37387
+ isomorph_default.hmacSha256 = (key, data) => crypto.createHmac("sha256", key).update(data).digest("hex");
37388
+ isomorph_default.timingSafeEqual = (left, right) => {
37389
+ const leftBuffer = Buffer.from(left);
37390
+ const rightBuffer = Buffer.from(right);
37391
+ return leftBuffer.length === rightBuffer.length && crypto.timingSafeEqual(leftBuffer, rightBuffer);
37392
+ };
36050
37393
  _internalSetInitialState();
36051
37394
  const disabled = readDisabledInstrumentationEnvConfig(
36052
37395
  isomorph_default.getEnv("BRAINTRUST_DISABLE_INSTRUMENTATION")
@@ -36056,6 +37399,24 @@ function configureNode() {
36056
37399
  }
36057
37400
  registry.enable();
36058
37401
  }
37402
+ function getNearestBraintrustEnvValue(name) {
37403
+ for (let dir2 = process.cwd(), depth = 0; depth <= BRAINTRUST_ENV_SEARCH_PARENT_LIMIT; dir2 = path.dirname(dir2), depth++) {
37404
+ const envPath = path.join(dir2, ".env.braintrust");
37405
+ try {
37406
+ const parsed = dotenv.parse(fsSync.readFileSync(envPath, "utf8"));
37407
+ const value = parsed[name];
37408
+ return value?.trim() ? value : void 0;
37409
+ } catch (e) {
37410
+ if (typeof e !== "object" || e === null || !("code" in e) || e.code !== "ENOENT") {
37411
+ return void 0;
37412
+ }
37413
+ }
37414
+ if (path.dirname(dir2) === dir2) {
37415
+ break;
37416
+ }
37417
+ }
37418
+ return void 0;
37419
+ }
36059
37420
 
36060
37421
  // src/cli/index.ts
36061
37422
  var import_env2 = require("@next/env");
@@ -36388,6 +37749,7 @@ var ToolBuilder = class {
36388
37749
  constructor(project) {
36389
37750
  this.project = project;
36390
37751
  }
37752
+ project;
36391
37753
  taskCounter = 0;
36392
37754
  // This type definition is just a catch all so that the implementation can be
36393
37755
  // less specific than the two more specific declarations above.
@@ -36418,6 +37780,7 @@ var ScorerBuilder = class {
36418
37780
  constructor(project) {
36419
37781
  this.project = project;
36420
37782
  }
37783
+ project;
36421
37784
  taskCounter = 0;
36422
37785
  create(opts) {
36423
37786
  this.taskCounter++;
@@ -36477,6 +37840,7 @@ var ClassifierBuilder = class {
36477
37840
  constructor(project) {
36478
37841
  this.project = project;
36479
37842
  }
37843
+ project;
36480
37844
  taskCounter = 0;
36481
37845
  create(opts) {
36482
37846
  this.taskCounter++;
@@ -36512,6 +37876,7 @@ var CodeFunction = class {
36512
37876
  throw new Error("parameters are required if return type is defined");
36513
37877
  }
36514
37878
  }
37879
+ project;
36515
37880
  handler;
36516
37881
  name;
36517
37882
  slug;
@@ -36599,6 +37964,7 @@ var PromptBuilder = class {
36599
37964
  constructor(project) {
36600
37965
  this.project = project;
36601
37966
  }
37967
+ project;
36602
37968
  create(opts) {
36603
37969
  const toolFunctions = [];
36604
37970
  const rawTools = [];
@@ -36675,6 +38041,7 @@ var ParametersBuilder = class {
36675
38041
  constructor(project) {
36676
38042
  this.project = project;
36677
38043
  }
38044
+ project;
36678
38045
  create(opts) {
36679
38046
  const slug = opts.slug ?? slugify(opts.name, { lower: true, strict: true });
36680
38047
  const codeParameters = new CodeParameters(this.project, {