braintrust 3.18.0 → 3.19.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 (54) hide show
  1. package/README.md +0 -10
  2. package/dev/dist/index.js +3626 -1989
  3. package/dev/dist/index.mjs +4576 -2939
  4. package/dist/apply-auto-instrumentation.js +342 -175
  5. package/dist/apply-auto-instrumentation.mjs +170 -3
  6. package/dist/auto-instrumentations/bundler/esbuild.cjs +209 -3
  7. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  8. package/dist/auto-instrumentations/bundler/next.cjs +209 -3
  9. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  10. package/dist/auto-instrumentations/bundler/rollup.cjs +209 -3
  11. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  12. package/dist/auto-instrumentations/bundler/vite.cjs +209 -3
  13. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  14. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +209 -3
  15. package/dist/auto-instrumentations/bundler/webpack.cjs +209 -3
  16. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  17. package/dist/auto-instrumentations/{chunk-MYCHHXOE.mjs → chunk-K74TZGGM.mjs} +1 -1
  18. package/dist/auto-instrumentations/{chunk-GNUEZ2PE.mjs → chunk-S4374IP6.mjs} +84 -4
  19. package/dist/auto-instrumentations/{chunk-ZYKZEMRT.mjs → chunk-W4E36GIW.mjs} +128 -0
  20. package/dist/auto-instrumentations/hook.mjs +209 -3
  21. package/dist/auto-instrumentations/index.cjs +129 -0
  22. package/dist/auto-instrumentations/index.d.mts +3 -1
  23. package/dist/auto-instrumentations/index.d.ts +3 -1
  24. package/dist/auto-instrumentations/index.mjs +3 -1
  25. package/dist/browser.d.mts +79 -8
  26. package/dist/browser.d.ts +79 -8
  27. package/dist/browser.js +2054 -161
  28. package/dist/browser.mjs +4847 -2954
  29. package/dist/chunk-37RLJF2U.js +29262 -0
  30. package/dist/{chunk-73IYIIOL.js → chunk-LPC4W2WX.js} +54 -4
  31. package/dist/chunk-VEQ2PCMC.mjs +29262 -0
  32. package/dist/{chunk-BYFADLEZ.mjs → chunk-Y7W7WP6H.mjs} +53 -3
  33. package/dist/cli.js +3492 -1849
  34. package/dist/edge-light.d.mts +1 -1
  35. package/dist/edge-light.d.ts +1 -1
  36. package/dist/edge-light.js +2054 -161
  37. package/dist/edge-light.mjs +4847 -2954
  38. package/dist/index.d.mts +79 -8
  39. package/dist/index.d.ts +79 -8
  40. package/dist/index.js +835 -27963
  41. package/dist/index.mjs +578 -27706
  42. package/dist/instrumentation/index.d.mts +66 -6
  43. package/dist/instrumentation/index.d.ts +66 -6
  44. package/dist/instrumentation/index.js +4573 -2937
  45. package/dist/instrumentation/index.mjs +4573 -2937
  46. package/dist/vitest-evals-reporter.d.mts +22 -0
  47. package/dist/vitest-evals-reporter.d.ts +22 -0
  48. package/dist/vitest-evals-reporter.js +401 -0
  49. package/dist/vitest-evals-reporter.mjs +401 -0
  50. package/dist/workerd.d.mts +1 -1
  51. package/dist/workerd.d.ts +1 -1
  52. package/dist/workerd.js +2054 -161
  53. package/dist/workerd.mjs +4847 -2954
  54. package/package.json +7 -1
package/dist/workerd.js CHANGED
@@ -164,6 +164,7 @@ __export(workerd_exports, {
164
164
  wrapAISDKModel: () => wrapAISDKModel,
165
165
  wrapAgentClass: () => wrapAgentClass,
166
166
  wrapAnthropic: () => wrapAnthropic,
167
+ wrapBedrockRuntime: () => wrapBedrockRuntime,
167
168
  wrapClaudeAgentSDK: () => wrapClaudeAgentSDK,
168
169
  wrapCohere: () => wrapCohere,
169
170
  wrapCopilotClient: () => wrapCopilotClient,
@@ -181,6 +182,7 @@ __export(workerd_exports, {
181
182
  wrapOpenRouter: () => wrapOpenRouter,
182
183
  wrapOpenRouterAgent: () => wrapOpenRouterAgent,
183
184
  wrapPiCodingAgentSDK: () => wrapPiCodingAgentSDK,
185
+ wrapStrandsAgentSDK: () => wrapStrandsAgentSDK,
184
186
  wrapTraced: () => wrapTraced,
185
187
  wrapVitest: () => wrapVitest
186
188
  });
@@ -7816,15 +7818,15 @@ function getSpanParentObject(options) {
7816
7818
  return NOOP_SPAN;
7817
7819
  }
7818
7820
  function logError(span, error) {
7819
- let errorMessage2 = "<error>";
7821
+ let errorMessage = "<error>";
7820
7822
  let stackTrace = "";
7821
7823
  if (error instanceof Error) {
7822
- errorMessage2 = error.message;
7824
+ errorMessage = error.message;
7823
7825
  stackTrace = error.stack || "";
7824
7826
  } else {
7825
- errorMessage2 = String(error);
7827
+ errorMessage = String(error);
7826
7828
  }
7827
- span.log({ error: `${errorMessage2}
7829
+ span.log({ error: `${errorMessage}
7828
7830
 
7829
7831
  ${stackTrace}` });
7830
7832
  }
@@ -10260,7 +10262,10 @@ function patchStreamIfNeeded(stream, options) {
10260
10262
  let completed = false;
10261
10263
  stream.next = async (...args) => {
10262
10264
  try {
10263
- const result = await originalNext(...args);
10265
+ const result = await runNextWithWrapper(
10266
+ options,
10267
+ () => originalNext(...args)
10268
+ );
10264
10269
  if (result.done) {
10265
10270
  if (!completed) {
10266
10271
  completed = true;
@@ -10352,7 +10357,10 @@ function patchStreamIfNeeded(stream, options) {
10352
10357
  let completed = false;
10353
10358
  iterator.next = async function(...args) {
10354
10359
  try {
10355
- const result = await originalNext(...args);
10360
+ const result = await runNextWithWrapper(
10361
+ options,
10362
+ () => originalNext(...args)
10363
+ );
10356
10364
  if (result.done) {
10357
10365
  if (!completed) {
10358
10366
  completed = true;
@@ -10438,6 +10446,9 @@ function patchStreamIfNeeded(stream, options) {
10438
10446
  return stream;
10439
10447
  }
10440
10448
  }
10449
+ function runNextWithWrapper(options, callback) {
10450
+ return options.aroundNext ? options.aroundNext(callback) : callback();
10451
+ }
10441
10452
 
10442
10453
  // src/instrumentation/core/channel-tracing-utils.ts
10443
10454
  function hasChannelSpanInfo(value) {
@@ -10462,11 +10473,23 @@ function buildStartSpanArgs(config, event) {
10462
10473
  mergeDicts(spanAttributes, spanInfo.spanAttributes);
10463
10474
  }
10464
10475
  return {
10465
- name: typeof spanInfo?.name === "string" && spanInfo.name ? spanInfo.name : config.name,
10476
+ name: typeof spanInfo?.name === "string" && spanInfo.name ? spanInfo.name : resolveConfigName(config.name, event),
10466
10477
  spanAttributes,
10467
10478
  spanInfoMetadata: isObject(spanInfo?.metadata) ? spanInfo.metadata : void 0
10468
10479
  };
10469
10480
  }
10481
+ function resolveConfigName(name, event) {
10482
+ if (typeof name === "string") {
10483
+ return name;
10484
+ }
10485
+ try {
10486
+ const resolved = name(event.arguments ?? [], event);
10487
+ return resolved || "unknown";
10488
+ } catch (error) {
10489
+ debugLogger.error("Error resolving instrumentation span name:", error);
10490
+ return "unknown";
10491
+ }
10492
+ }
10470
10493
  function mergeInputMetadata(metadata, spanInfoMetadata) {
10471
10494
  if (!spanInfoMetadata) {
10472
10495
  return isObject(metadata) ? (
@@ -10826,6 +10849,26 @@ var BasePlugin = class {
10826
10849
  }
10827
10850
  };
10828
10851
 
10852
+ // src/instrumentation/core/logging.ts
10853
+ function toLoggedError(error) {
10854
+ if (error instanceof Error) {
10855
+ return error.message;
10856
+ }
10857
+ if (typeof error === "string") {
10858
+ return error;
10859
+ }
10860
+ try {
10861
+ const serialized = JSON.stringify(error);
10862
+ return serialized === void 0 ? String(error) : serialized;
10863
+ } catch {
10864
+ try {
10865
+ return String(error);
10866
+ } catch {
10867
+ return "<unserializable error>";
10868
+ }
10869
+ }
10870
+ }
10871
+
10829
10872
  // src/instrumentation/auto-instrumentation-suppression.ts
10830
10873
  var autoInstrumentationSuppressionStore;
10831
10874
  function suppressionStore() {
@@ -12333,7 +12376,7 @@ async function finalizeCodexRun(state, params = {}) {
12333
12376
  try {
12334
12377
  const error = params.error;
12335
12378
  safeLog(state.span, {
12336
- ...error ? { error: error instanceof Error ? error.message : String(error) } : {},
12379
+ ...error ? { error: toLoggedError(error) } : {},
12337
12380
  metadata: state.metadata,
12338
12381
  metrics,
12339
12382
  output
@@ -12439,7 +12482,7 @@ async function finishActiveLlmSpan(state, error) {
12439
12482
  state.activeLlmSpan = void 0;
12440
12483
  const output = buildLlmOutput(active);
12441
12484
  safeLog(active.span, {
12442
- ...error ? { error: error instanceof Error ? error.message : String(error) } : {},
12485
+ ...error ? { error: toLoggedError(error) } : {},
12443
12486
  metadata: active.metadata,
12444
12487
  ...output ? { output } : {}
12445
12488
  });
@@ -12962,9 +13005,6 @@ function extractAnthropicToolNames(tools) {
12962
13005
  }
12963
13006
  return toolNames;
12964
13007
  }
12965
- function toErrorMessage(error) {
12966
- return error instanceof Error ? error.message : String(error);
12967
- }
12968
13008
  function getAnthropicToolRunnerInput(args) {
12969
13009
  return args.length > 0 ? args[0] : void 0;
12970
13010
  }
@@ -12998,7 +13038,7 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
12998
13038
  return result;
12999
13039
  };
13000
13040
  const finalizeError = (error) => {
13001
- span.log({ error: toErrorMessage(error) });
13041
+ span.log({ error: toLoggedError(error) });
13002
13042
  throw error;
13003
13043
  };
13004
13044
  try {
@@ -13154,7 +13194,7 @@ function finalizeAnthropicToolRunnerError(state, error) {
13154
13194
  }
13155
13195
  state.finalized = true;
13156
13196
  state.span.log({
13157
- error: error instanceof Error ? error.message : String(error)
13197
+ error: toLoggedError(error)
13158
13198
  });
13159
13199
  state.span.end();
13160
13200
  }
@@ -13483,7 +13523,7 @@ function processAttachmentsInInput(input) {
13483
13523
  function coalesceInput(messages, system) {
13484
13524
  const input = (messages || []).slice();
13485
13525
  if (system) {
13486
- input.push({ role: "system", content: system });
13526
+ input.unshift({ role: "system", content: system });
13487
13527
  }
13488
13528
  return input;
13489
13529
  }
@@ -15070,9 +15110,7 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
15070
15110
  }
15071
15111
  span.log({ output: lastValue });
15072
15112
  } catch (error) {
15073
- span.log({
15074
- error: error instanceof Error ? error.message : String(error)
15075
- });
15113
+ span.log({ error: toLoggedError(error) });
15076
15114
  throw error;
15077
15115
  } finally {
15078
15116
  span.end();
@@ -16085,9 +16123,6 @@ function bindClaudeLocalToolContextToAsyncIterable(result, localToolContext) {
16085
16123
  var LOCAL_TOOL_HANDLER_WRAPPED = /* @__PURE__ */ Symbol.for(
16086
16124
  "braintrust.claude_agent_sdk.local_tool_handler_wrapped"
16087
16125
  );
16088
- function toErrorMessage2(error) {
16089
- return error instanceof Error ? error.message : String(error);
16090
- }
16091
16126
  function getToolUseIdFromExtra(extra) {
16092
16127
  if (!extra || typeof extra !== "object" || !("_meta" in extra)) {
16093
16128
  return void 0;
@@ -16134,7 +16169,7 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
16134
16169
  return result;
16135
16170
  };
16136
16171
  const finalizeError = (error) => {
16137
- span.log({ error: toErrorMessage2(error) });
16172
+ span.log({ error: toLoggedError(error) });
16138
16173
  span.end();
16139
16174
  throw error;
16140
16175
  };
@@ -17835,7 +17870,7 @@ async function handleToolUpdate(state, update) {
17835
17870
  toolCall
17836
17871
  });
17837
17872
  finishToolSpan(toolState, {
17838
- error: toolCall?.status === "error" ? stringifyUnknown(result) : void 0,
17873
+ error: toolCall?.status === "error" ? toLoggedError(result) : void 0,
17839
17874
  metadata: {
17840
17875
  "cursor_sdk.tool.status": toolCall?.status ?? "completed"
17841
17876
  },
@@ -17918,7 +17953,7 @@ async function handleToolMessage(state, message) {
17918
17953
  truncated: message.truncated
17919
17954
  });
17920
17955
  finishToolSpan(toolState, {
17921
- error: message.status === "error" ? stringifyUnknown(message.result) : void 0,
17956
+ error: message.status === "error" ? toLoggedError(message.result) : void 0,
17922
17957
  metadata: {
17923
17958
  "cursor_sdk.tool.status": message.status
17924
17959
  },
@@ -17969,7 +18004,7 @@ async function handleConversationStep(state, step) {
17969
18004
  toolCall
17970
18005
  });
17971
18006
  finishToolSpan(toolState, {
17972
- error: toolCall?.status === "error" ? stringifyUnknown(toolCall.result) : void 0,
18007
+ error: toolCall?.status === "error" ? toLoggedError(toolCall.result) : void 0,
17973
18008
  metadata: {
17974
18009
  "cursor_sdk.tool.status": toolCall?.status ?? "completed"
17975
18010
  },
@@ -18079,7 +18114,7 @@ async function finalizeCursorRun(state, params = {}) {
18079
18114
  };
18080
18115
  if (error) {
18081
18116
  safeLog2(state.span, {
18082
- error: error instanceof Error ? error.message : String(error),
18117
+ error: toLoggedError(error),
18083
18118
  metadata,
18084
18119
  metrics: {
18085
18120
  ...cleanMetrics2(state.metrics),
@@ -18265,19 +18300,6 @@ function getString(obj, key) {
18265
18300
  const value = obj?.[key];
18266
18301
  return typeof value === "string" ? value : void 0;
18267
18302
  }
18268
- function stringifyUnknown(value) {
18269
- if (value instanceof Error) {
18270
- return value.message;
18271
- }
18272
- if (typeof value === "string") {
18273
- return value;
18274
- }
18275
- try {
18276
- return JSON.stringify(value);
18277
- } catch {
18278
- return String(value);
18279
- }
18280
- }
18281
18303
  function safeLog2(span, event) {
18282
18304
  try {
18283
18305
  span.log(event);
@@ -20793,9 +20815,7 @@ function patchOpenRouterCallModelResult(args) {
20793
20815
  return;
20794
20816
  }
20795
20817
  ended = true;
20796
- span.log({
20797
- error: normalizeError(error).message
20798
- });
20818
+ span.log({ error: toLoggedError(error) });
20799
20819
  span.end();
20800
20820
  };
20801
20821
  const finalizeFromResponse = async (fallbackOutput) => {
@@ -21866,9 +21886,7 @@ function patchOpenRouterCallModelResult2(args) {
21866
21886
  return;
21867
21887
  }
21868
21888
  ended = true;
21869
- span.log({
21870
- error: normalizeError2(error).message
21871
- });
21889
+ span.log({ error: toLoggedError(error) });
21872
21890
  span.end();
21873
21891
  };
21874
21892
  const finalizeFromResponse = async (fallbackOutput) => {
@@ -24242,6 +24260,652 @@ function aggregateGroqReasoning(chunks) {
24242
24260
  return reasoning.length > 0 ? reasoning : void 0;
24243
24261
  }
24244
24262
 
24263
+ // src/instrumentation/plugins/bedrock-runtime-channels.ts
24264
+ var clientSendChannel = channel({
24265
+ channelName: "client.send",
24266
+ kind: "async"
24267
+ });
24268
+ var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
24269
+ clientSend: clientSendChannel
24270
+ });
24271
+ var smithyCoreChannels = defineChannels("@smithy/core", {
24272
+ clientSend: clientSendChannel
24273
+ });
24274
+ var smithyClientChannels = defineChannels("@smithy/smithy-client", {
24275
+ clientSend: clientSendChannel
24276
+ });
24277
+
24278
+ // src/instrumentation/plugins/bedrock-runtime-common.ts
24279
+ var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
24280
+ ConverseCommand: "converse",
24281
+ ConverseStreamCommand: "converseStream",
24282
+ InvokeModelCommand: "invokeModel",
24283
+ InvokeModelWithBidirectionalStreamCommand: "invokeModelWithBidirectionalStream",
24284
+ InvokeModelWithResponseStreamCommand: "invokeModelWithResponseStream"
24285
+ };
24286
+ function getBedrockRuntimeCommandName(command) {
24287
+ if (!isObject(command) || !isObject(command.constructor)) {
24288
+ return void 0;
24289
+ }
24290
+ const input = command.input;
24291
+ if (!isObject(input) || typeof input.modelId !== "string") {
24292
+ return void 0;
24293
+ }
24294
+ const commandName = command.constructor.name;
24295
+ return isBedrockRuntimeCommandName(commandName) ? commandName : void 0;
24296
+ }
24297
+ function getBedrockRuntimeOperation(command) {
24298
+ const commandName = getBedrockRuntimeCommandName(command);
24299
+ return commandName ? BEDROCK_RUNTIME_COMMAND_OPERATIONS[commandName] : void 0;
24300
+ }
24301
+ function getBedrockRuntimeCommandInput(command) {
24302
+ return isObject(command) ? command.input : void 0;
24303
+ }
24304
+ function buildBedrockRuntimeSpanInfo(command) {
24305
+ const commandName = getBedrockRuntimeCommandName(command);
24306
+ const operation = getBedrockRuntimeOperation(command);
24307
+ return {
24308
+ name: operation ? `bedrock.${operation}` : "bedrock.client.send",
24309
+ metadata: {
24310
+ ...commandName ? { command: commandName } : {},
24311
+ ...operation ? { operation } : {}
24312
+ }
24313
+ };
24314
+ }
24315
+ function isBedrockRuntimeCommandName(commandName) {
24316
+ return commandName === "ConverseCommand" || commandName === "ConverseStreamCommand" || commandName === "InvokeModelCommand" || commandName === "InvokeModelWithBidirectionalStreamCommand" || commandName === "InvokeModelWithResponseStreamCommand";
24317
+ }
24318
+
24319
+ // src/instrumentation/plugins/bedrock-runtime-plugin.ts
24320
+ var BedrockRuntimePlugin = class extends BasePlugin {
24321
+ onEnable() {
24322
+ this.unsubscribers.push(
24323
+ ...[
24324
+ bedrockRuntimeChannels.clientSend,
24325
+ smithyCoreChannels.clientSend,
24326
+ smithyClientChannels.clientSend
24327
+ ].map((channel2) => traceBedrockRuntimeClientSendChannel(channel2))
24328
+ );
24329
+ }
24330
+ onDisable() {
24331
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
24332
+ }
24333
+ };
24334
+ function traceBedrockRuntimeClientSendChannel(channel2) {
24335
+ return traceStreamingChannel(channel2, {
24336
+ name: ([command]) => buildBedrockRuntimeSpanInfo(command).name,
24337
+ shouldTrace: ([command, optionsOrCb, cb]) => getBedrockRuntimeOperation(command) !== void 0 && typeof optionsOrCb !== "function" && typeof cb !== "function",
24338
+ type: "llm" /* LLM */,
24339
+ extractInput: ([command]) => extractBedrockRuntimeInput(command),
24340
+ extractOutput: (result, endEvent) => extractBedrockRuntimeOutput(endEvent?.arguments?.[0], result),
24341
+ extractMetadata: (result, endEvent) => extractBedrockRuntimeResponseMetadata(endEvent?.arguments?.[0], result),
24342
+ extractMetrics: (result) => extractBedrockRuntimeResponseMetrics(result),
24343
+ patchResult: ({ endEvent, result, span, startTime }) => patchBedrockRuntimeStreamingResult({
24344
+ command: endEvent.arguments?.[0],
24345
+ result,
24346
+ span,
24347
+ startTime
24348
+ })
24349
+ });
24350
+ }
24351
+ function extractBedrockRuntimeInput(command) {
24352
+ const operation = getBedrockRuntimeOperation(command);
24353
+ const commandName = getBedrockRuntimeCommandName(command);
24354
+ const request = getBedrockRuntimeCommandInput(command);
24355
+ const metadata = {
24356
+ provider: "aws-bedrock",
24357
+ ...commandName ? { command: commandName } : {},
24358
+ ...operation ? { operation } : {},
24359
+ ...extractBedrockRuntimeRequestMetadata(request)
24360
+ };
24361
+ if (operation === "converse" || operation === "converseStream") {
24362
+ const converseRequest = isObject(request) ? request : void 0;
24363
+ return {
24364
+ input: sanitizeBedrockValue({
24365
+ messages: converseRequest?.messages,
24366
+ system: converseRequest?.system
24367
+ }),
24368
+ metadata
24369
+ };
24370
+ }
24371
+ if (operation === "invokeModel" || operation === "invokeModelWithBidirectionalStream" || operation === "invokeModelWithResponseStream") {
24372
+ const invokeRequest = isObject(request) ? request : void 0;
24373
+ return {
24374
+ input: parseJsonBody(invokeRequest?.body) ?? summarizeBody(invokeRequest?.body),
24375
+ metadata
24376
+ };
24377
+ }
24378
+ return {
24379
+ input: sanitizeBedrockValue(request),
24380
+ metadata
24381
+ };
24382
+ }
24383
+ function extractBedrockRuntimeRequestMetadata(request) {
24384
+ if (!isObject(request)) {
24385
+ return {};
24386
+ }
24387
+ const metadata = {};
24388
+ for (const key of [
24389
+ "modelId",
24390
+ "contentType",
24391
+ "accept",
24392
+ "trace",
24393
+ "guardrailIdentifier",
24394
+ "guardrailVersion",
24395
+ "performanceConfig",
24396
+ "performanceConfigLatency",
24397
+ "serviceTier"
24398
+ ]) {
24399
+ const value = request[key];
24400
+ if (value !== void 0) {
24401
+ metadata[key === "modelId" ? "model" : key] = value;
24402
+ }
24403
+ }
24404
+ if (isObject(request.inferenceConfig)) {
24405
+ Object.assign(metadata, sanitizeBedrockValue(request.inferenceConfig));
24406
+ }
24407
+ return metadata;
24408
+ }
24409
+ function extractBedrockRuntimeOutput(command, result) {
24410
+ const operation = getBedrockRuntimeOperation(command);
24411
+ if (operation === "converse") {
24412
+ return sanitizeBedrockValue(
24413
+ result?.output?.message
24414
+ );
24415
+ }
24416
+ if (operation === "invokeModel") {
24417
+ const response = isObject(result) ? result : void 0;
24418
+ return parseJsonBody(response?.body) ?? summarizeBody(response?.body);
24419
+ }
24420
+ return sanitizeBedrockValue(result);
24421
+ }
24422
+ function extractBedrockRuntimeResponseMetadata(command, result) {
24423
+ const operation = getBedrockRuntimeOperation(command);
24424
+ if (!isObject(result)) {
24425
+ return void 0;
24426
+ }
24427
+ const metadata = {};
24428
+ for (const key of [
24429
+ "stopReason",
24430
+ "contentType",
24431
+ "performanceConfig",
24432
+ "performanceConfigLatency",
24433
+ "serviceTier"
24434
+ ]) {
24435
+ const value = result[key];
24436
+ if (value !== void 0) {
24437
+ metadata[key] = value;
24438
+ }
24439
+ }
24440
+ if (operation === "converse" && result.additionalModelResponseFields !== void 0) {
24441
+ metadata.additionalModelResponseFields = sanitizeBedrockValue(
24442
+ result.additionalModelResponseFields
24443
+ );
24444
+ }
24445
+ return Object.keys(metadata).length > 0 ? metadata : void 0;
24446
+ }
24447
+ function extractBedrockRuntimeResponseMetrics(result) {
24448
+ if (!isObject(result)) {
24449
+ return {};
24450
+ }
24451
+ const parsedBody = parseJsonBody(result.body);
24452
+ if (isObject(parsedBody)) {
24453
+ const metadata = isObject(parsedBody.metadata) ? parsedBody.metadata : void 0;
24454
+ const metrics = parseBedrockRuntimeMetrics(
24455
+ parsedBody.usage ?? metadata?.usage,
24456
+ parsedBody.metrics ?? metadata?.metrics
24457
+ );
24458
+ if (Object.keys(metrics).length > 0) {
24459
+ return metrics;
24460
+ }
24461
+ }
24462
+ return parseBedrockRuntimeMetrics(result.usage, result.metrics);
24463
+ }
24464
+ function parseBedrockRuntimeMetrics(usage, responseMetrics) {
24465
+ const metrics = {};
24466
+ const usageRecord = isObject(usage) ? usage : {};
24467
+ const promptTokens = firstNumber3(
24468
+ usageRecord.inputTokens,
24469
+ usageRecord.inputTokenCount,
24470
+ usageRecord.input_tokens,
24471
+ usageRecord.prompt_tokens
24472
+ );
24473
+ if (promptTokens !== void 0) {
24474
+ metrics.prompt_tokens = promptTokens;
24475
+ }
24476
+ const completionTokens = firstNumber3(
24477
+ usageRecord.outputTokens,
24478
+ usageRecord.outputTokenCount,
24479
+ usageRecord.output_tokens,
24480
+ usageRecord.completion_tokens
24481
+ );
24482
+ if (completionTokens !== void 0) {
24483
+ metrics.completion_tokens = completionTokens;
24484
+ }
24485
+ const totalTokens = firstNumber3(
24486
+ usageRecord.totalTokens,
24487
+ usageRecord.totalTokenCount,
24488
+ usageRecord.total_tokens,
24489
+ usageRecord.tokens
24490
+ );
24491
+ if (totalTokens !== void 0) {
24492
+ metrics.tokens = totalTokens;
24493
+ }
24494
+ const cacheReadInputTokens = firstNumber3(
24495
+ usageRecord.cacheReadInputTokens,
24496
+ usageRecord.cacheReadInputTokenCount,
24497
+ usageRecord.cache_read_input_tokens,
24498
+ usageRecord.prompt_cached_tokens
24499
+ );
24500
+ if (cacheReadInputTokens !== void 0) {
24501
+ metrics.prompt_cached_tokens = cacheReadInputTokens;
24502
+ }
24503
+ const cacheWriteInputTokens = firstNumber3(
24504
+ usageRecord.cacheWriteInputTokens,
24505
+ usageRecord.cacheWriteInputTokenCount,
24506
+ usageRecord.cache_write_input_tokens,
24507
+ usageRecord.cache_creation_input_tokens,
24508
+ usageRecord.prompt_cache_creation_tokens
24509
+ );
24510
+ if (cacheWriteInputTokens !== void 0) {
24511
+ metrics.prompt_cache_creation_tokens = cacheWriteInputTokens;
24512
+ }
24513
+ if (metrics.tokens === void 0) {
24514
+ const tokenParts = [
24515
+ promptTokens,
24516
+ completionTokens,
24517
+ cacheReadInputTokens,
24518
+ cacheWriteInputTokens
24519
+ ].filter((value) => value !== void 0);
24520
+ if (tokenParts.length > 0) {
24521
+ metrics.tokens = tokenParts.reduce((total, value) => total + value, 0);
24522
+ }
24523
+ }
24524
+ if (isObject(responseMetrics) && typeof responseMetrics.latencyMs === "number") {
24525
+ metrics.latency_ms = responseMetrics.latencyMs;
24526
+ }
24527
+ return metrics;
24528
+ }
24529
+ function firstNumber3(...values) {
24530
+ return values.find((value) => typeof value === "number");
24531
+ }
24532
+ function patchBedrockRuntimeStreamingResult(args) {
24533
+ const operation = getBedrockRuntimeOperation(args.command);
24534
+ if (!isObject(args.result)) {
24535
+ return false;
24536
+ }
24537
+ if (operation === "converseStream" && isAsyncIterable(args.result.stream)) {
24538
+ patchConverseStream(
24539
+ args.result.stream,
24540
+ args.span,
24541
+ args.startTime
24542
+ );
24543
+ return true;
24544
+ }
24545
+ if ((operation === "invokeModelWithBidirectionalStream" || operation === "invokeModelWithResponseStream") && isAsyncIterable(args.result.body)) {
24546
+ patchInvokeModelResponseStream(
24547
+ args.result.body,
24548
+ args.span,
24549
+ args.startTime
24550
+ );
24551
+ return true;
24552
+ }
24553
+ return false;
24554
+ }
24555
+ function patchConverseStream(stream, span, startTime) {
24556
+ let firstChunkTime;
24557
+ patchStreamIfNeeded(stream, {
24558
+ onChunk: (chunk) => {
24559
+ if (firstChunkTime === void 0 && isObject(chunk.contentBlockDelta)) {
24560
+ firstChunkTime = getCurrentUnixTimestamp();
24561
+ }
24562
+ },
24563
+ onComplete: (chunks) => {
24564
+ const aggregated = aggregateBedrockConverseStreamChunks(chunks);
24565
+ const metrics = { ...aggregated.metrics };
24566
+ if (firstChunkTime !== void 0) {
24567
+ metrics.time_to_first_token = firstChunkTime - startTime;
24568
+ }
24569
+ logBedrockStreamAggregation(span, aggregated, metrics);
24570
+ span.end();
24571
+ },
24572
+ onError: (error) => {
24573
+ span.log({ error: error.message });
24574
+ span.end();
24575
+ }
24576
+ });
24577
+ }
24578
+ function patchInvokeModelResponseStream(stream, span, startTime) {
24579
+ let firstChunkTime;
24580
+ patchStreamIfNeeded(stream, {
24581
+ onChunk: (chunk) => {
24582
+ if (firstChunkTime === void 0 && isObject(chunk.chunk)) {
24583
+ firstChunkTime = getCurrentUnixTimestamp();
24584
+ }
24585
+ },
24586
+ onComplete: (chunks) => {
24587
+ const aggregated = aggregateInvokeModelResponseStreamChunks(chunks);
24588
+ const metrics = { ...aggregated.metrics };
24589
+ if (firstChunkTime !== void 0) {
24590
+ metrics.time_to_first_token = firstChunkTime - startTime;
24591
+ }
24592
+ logBedrockStreamAggregation(span, aggregated, metrics);
24593
+ span.end();
24594
+ },
24595
+ onError: (error) => {
24596
+ span.log({ error: error.message });
24597
+ span.end();
24598
+ }
24599
+ });
24600
+ }
24601
+ var BEDROCK_STREAM_EXCEPTION_KEYS = [
24602
+ "internalServerException",
24603
+ "modelStreamErrorException",
24604
+ "validationException",
24605
+ "throttlingException",
24606
+ "modelTimeoutException",
24607
+ "serviceUnavailableException"
24608
+ ];
24609
+ function aggregateBedrockConverseStreamChunks(chunks) {
24610
+ let role;
24611
+ let stopReason;
24612
+ let usage;
24613
+ let responseMetrics;
24614
+ const contentByIndex = /* @__PURE__ */ new Map();
24615
+ const metadata = {};
24616
+ for (const chunk of chunks) {
24617
+ const exception = extractBedrockStreamException(chunk);
24618
+ if (exception) {
24619
+ return exception;
24620
+ }
24621
+ if (typeof chunk.messageStart?.role === "string") {
24622
+ role = chunk.messageStart.role;
24623
+ }
24624
+ const startIndex = chunk.contentBlockStart?.contentBlockIndex;
24625
+ if (typeof startIndex === "number") {
24626
+ contentByIndex.set(startIndex, {
24627
+ ...contentByIndex.get(startIndex) ?? {},
24628
+ ...sanitizeRecord(chunk.contentBlockStart?.start)
24629
+ });
24630
+ }
24631
+ const deltaIndex = chunk.contentBlockDelta?.contentBlockIndex;
24632
+ const delta = chunk.contentBlockDelta?.delta;
24633
+ if (typeof deltaIndex === "number" && isObject(delta)) {
24634
+ const existing = contentByIndex.get(deltaIndex) ?? {};
24635
+ contentByIndex.set(deltaIndex, mergeContentBlockDelta(existing, delta));
24636
+ }
24637
+ if (typeof chunk.messageStop?.stopReason === "string") {
24638
+ stopReason = chunk.messageStop.stopReason;
24639
+ }
24640
+ if (chunk.messageStop?.additionalModelResponseFields !== void 0) {
24641
+ metadata.additionalModelResponseFields = sanitizeBedrockValue(
24642
+ chunk.messageStop.additionalModelResponseFields
24643
+ );
24644
+ }
24645
+ if (chunk.metadata?.usage !== void 0) {
24646
+ usage = chunk.metadata.usage;
24647
+ }
24648
+ if (chunk.metadata?.metrics !== void 0) {
24649
+ responseMetrics = chunk.metadata.metrics;
24650
+ }
24651
+ if (chunk.metadata?.performanceConfig !== void 0) {
24652
+ metadata.performanceConfig = sanitizeBedrockValue(
24653
+ chunk.metadata.performanceConfig
24654
+ );
24655
+ }
24656
+ if (chunk.metadata?.serviceTier !== void 0) {
24657
+ metadata.serviceTier = chunk.metadata.serviceTier;
24658
+ }
24659
+ }
24660
+ if (stopReason !== void 0) {
24661
+ metadata.stopReason = stopReason;
24662
+ }
24663
+ const content = [...contentByIndex.entries()].sort(([left], [right]) => left - right).map(([, value]) => value).filter((value) => Object.keys(value).length > 0);
24664
+ return {
24665
+ output: {
24666
+ role,
24667
+ content
24668
+ },
24669
+ metrics: parseBedrockRuntimeMetrics(usage, responseMetrics),
24670
+ ...Object.keys(metadata).length > 0 ? { metadata } : {}
24671
+ };
24672
+ }
24673
+ function aggregateInvokeModelResponseStreamChunks(chunks) {
24674
+ for (const chunk of chunks) {
24675
+ const exception = extractBedrockStreamException(chunk);
24676
+ if (exception) {
24677
+ return exception;
24678
+ }
24679
+ }
24680
+ const parsedChunks = chunks.map((chunk) => parseJsonBody(chunk.chunk?.bytes)).filter((chunk) => chunk !== void 0);
24681
+ const jsonLikeChunks = parsedChunks.filter(isObject);
24682
+ const text = parsedChunks.map(extractTextFromJsonLike).join("");
24683
+ const lastMetadataChunk = jsonLikeChunks.slice().reverse().find((chunk) => isObject(chunk.metadata));
24684
+ const metadata = isObject(lastMetadataChunk?.metadata) ? sanitizeRecord(lastMetadataChunk.metadata) : void 0;
24685
+ let usage;
24686
+ for (const chunk of jsonLikeChunks) {
24687
+ const message = chunk.message;
24688
+ for (const candidate of [
24689
+ isObject(chunk.usage) ? chunk.usage : void 0,
24690
+ isObject(message) && isObject(message.usage) ? message.usage : void 0
24691
+ ]) {
24692
+ if (candidate !== void 0) {
24693
+ usage = {
24694
+ ...usage,
24695
+ ...sanitizeRecord(candidate)
24696
+ };
24697
+ }
24698
+ }
24699
+ }
24700
+ return {
24701
+ output: text.length > 0 ? { text } : {
24702
+ chunk_count: chunks.length,
24703
+ chunks: sanitizeBedrockValue(
24704
+ (jsonLikeChunks.length > 0 ? jsonLikeChunks : chunks).slice(
24705
+ 0,
24706
+ 20
24707
+ )
24708
+ )
24709
+ },
24710
+ metrics: parseBedrockRuntimeMetrics(
24711
+ usage ?? (isObject(metadata) ? metadata.usage : void 0),
24712
+ isObject(metadata) ? metadata.metrics : void 0
24713
+ ),
24714
+ ...metadata ? { metadata } : {}
24715
+ };
24716
+ }
24717
+ function logBedrockStreamAggregation(span, aggregated, metrics) {
24718
+ span.log(
24719
+ aggregated.error !== void 0 ? {
24720
+ error: aggregated.error,
24721
+ ...aggregated.metadata ? { metadata: aggregated.metadata } : {},
24722
+ metrics
24723
+ } : {
24724
+ output: aggregated.output,
24725
+ ...aggregated.metadata ? { metadata: aggregated.metadata } : {},
24726
+ metrics
24727
+ }
24728
+ );
24729
+ }
24730
+ function extractBedrockStreamException(chunk) {
24731
+ if (!isObject(chunk)) {
24732
+ return void 0;
24733
+ }
24734
+ for (const [key, value] of Object.entries(chunk)) {
24735
+ if (!isBedrockStreamExceptionKey(key) || !isObject(value)) {
24736
+ continue;
24737
+ }
24738
+ const name = typeof value.name === "string" ? value.name : key;
24739
+ const message = typeof value.message === "string" ? value.message : typeof value.originalMessage === "string" ? value.originalMessage : void 0;
24740
+ const metadata = {
24741
+ exception: key
24742
+ };
24743
+ if (typeof value.name === "string") {
24744
+ metadata.exceptionName = value.name;
24745
+ }
24746
+ if (typeof value.$fault === "string") {
24747
+ metadata.fault = value.$fault;
24748
+ }
24749
+ if (typeof value.originalMessage === "string") {
24750
+ metadata.originalMessage = value.originalMessage;
24751
+ }
24752
+ if (typeof value.originalStatusCode === "number") {
24753
+ metadata.originalStatusCode = value.originalStatusCode;
24754
+ }
24755
+ return {
24756
+ error: message ? `${name}: ${message}` : name,
24757
+ metadata,
24758
+ metrics: {}
24759
+ };
24760
+ }
24761
+ return void 0;
24762
+ }
24763
+ function isBedrockStreamExceptionKey(key) {
24764
+ return BEDROCK_STREAM_EXCEPTION_KEYS.some((candidate) => candidate === key);
24765
+ }
24766
+ function mergeContentBlockDelta(existing, delta) {
24767
+ const next = { ...existing };
24768
+ if (typeof delta.text === "string") {
24769
+ next.text = `${typeof next.text === "string" ? next.text : ""}${delta.text}`;
24770
+ }
24771
+ if (isObject(delta.reasoningContent)) {
24772
+ const existingReasoning = isObject(next.reasoningContent) ? next.reasoningContent : {};
24773
+ next.reasoningContent = {
24774
+ ...existingReasoning,
24775
+ ...sanitizeRecord(delta.reasoningContent),
24776
+ ...typeof delta.reasoningContent.text === "string" ? {
24777
+ text: `${typeof existingReasoning.text === "string" ? existingReasoning.text : ""}${delta.reasoningContent.text}`
24778
+ } : {}
24779
+ };
24780
+ }
24781
+ if (isObject(delta.toolUse)) {
24782
+ const existingToolUse = isObject(next.toolUse) ? next.toolUse : {};
24783
+ next.toolUse = {
24784
+ ...existingToolUse,
24785
+ ...sanitizeRecord(delta.toolUse),
24786
+ ...typeof delta.toolUse.input === "string" ? {
24787
+ input: `${typeof existingToolUse.input === "string" ? existingToolUse.input : ""}${delta.toolUse.input}`
24788
+ } : {}
24789
+ };
24790
+ }
24791
+ for (const [key, value] of Object.entries(delta)) {
24792
+ if (key !== "text" && key !== "reasoningContent" && key !== "toolUse" && isSafeBedrockObjectKey(key)) {
24793
+ next[key] = sanitizeBedrockValue(value);
24794
+ }
24795
+ }
24796
+ return next;
24797
+ }
24798
+ function parseJsonBody(body) {
24799
+ const text = decodeBodyToString(body);
24800
+ if (text === void 0 || text.length === 0) {
24801
+ return void 0;
24802
+ }
24803
+ try {
24804
+ return sanitizeBedrockValue(JSON.parse(text));
24805
+ } catch {
24806
+ return void 0;
24807
+ }
24808
+ }
24809
+ function summarizeBody(body) {
24810
+ if (body === void 0 || body === null) {
24811
+ return void 0;
24812
+ }
24813
+ const text = decodeBodyToString(body);
24814
+ if (text !== void 0) {
24815
+ return text.length > 2e3 ? `${text.slice(0, 2e3)}...` : text;
24816
+ }
24817
+ return sanitizeBedrockValue(body);
24818
+ }
24819
+ function decodeBodyToString(body) {
24820
+ if (typeof body === "string") {
24821
+ return body;
24822
+ }
24823
+ if (body instanceof Uint8Array) {
24824
+ return new TextDecoder().decode(body);
24825
+ }
24826
+ if (body instanceof ArrayBuffer) {
24827
+ return new TextDecoder().decode(new Uint8Array(body));
24828
+ }
24829
+ return void 0;
24830
+ }
24831
+ function sanitizeRecord(value) {
24832
+ return isObject(value) ? sanitizeBedrockValue(value) : {};
24833
+ }
24834
+ function sanitizeBedrockValue(value, depth = 0) {
24835
+ if (value === void 0 || value === null) {
24836
+ return value;
24837
+ }
24838
+ if (typeof value !== "object") {
24839
+ return value;
24840
+ }
24841
+ if (value instanceof Uint8Array) {
24842
+ return { byte_length: value.byteLength };
24843
+ }
24844
+ if (value instanceof ArrayBuffer) {
24845
+ return { byte_length: value.byteLength };
24846
+ }
24847
+ if (Array.isArray(value)) {
24848
+ return depth > 20 ? "[MaxDepth]" : value.map((item) => sanitizeBedrockValue(item, depth + 1));
24849
+ }
24850
+ if (!isObject(value)) {
24851
+ return String(value);
24852
+ }
24853
+ if (depth > 20) {
24854
+ return "[MaxDepth]";
24855
+ }
24856
+ const output = {};
24857
+ for (const [key, nested] of Object.entries(value)) {
24858
+ if (!isSafeBedrockObjectKey(key)) {
24859
+ continue;
24860
+ }
24861
+ output[key] = sanitizeBedrockValue(nested, depth + 1);
24862
+ }
24863
+ return output;
24864
+ }
24865
+ function isSafeBedrockObjectKey(key) {
24866
+ return key !== "__proto__" && key !== "constructor" && key !== "prototype";
24867
+ }
24868
+ function extractTextFromJsonLike(value) {
24869
+ if (typeof value === "string") {
24870
+ return value;
24871
+ }
24872
+ if (Array.isArray(value)) {
24873
+ return value.map(extractTextFromJsonLike).join("");
24874
+ }
24875
+ if (!isObject(value)) {
24876
+ return "";
24877
+ }
24878
+ const contentBlockDelta = value.contentBlockDelta;
24879
+ if (isObject(contentBlockDelta)) {
24880
+ return extractTextFromJsonLike(contentBlockDelta.delta);
24881
+ }
24882
+ if (isObject(value.delta)) {
24883
+ return extractTextFromJsonLike(value.delta);
24884
+ }
24885
+ if (typeof value.text === "string") {
24886
+ return value.text;
24887
+ }
24888
+ if (typeof value.outputText === "string") {
24889
+ return value.outputText;
24890
+ }
24891
+ if (typeof value.completion === "string") {
24892
+ return value.completion;
24893
+ }
24894
+ if (typeof value.generation === "string") {
24895
+ return value.generation;
24896
+ }
24897
+ if (Array.isArray(value.content)) {
24898
+ return value.content.map(extractTextFromJsonLike).join("");
24899
+ }
24900
+ if (Array.isArray(value.output)) {
24901
+ return value.output.map(extractTextFromJsonLike).join("");
24902
+ }
24903
+ if (Array.isArray(value.outputs)) {
24904
+ return value.outputs.map(extractTextFromJsonLike).join("");
24905
+ }
24906
+ return "";
24907
+ }
24908
+
24245
24909
  // src/instrumentation/plugins/genkit-channels.ts
24246
24910
  var genkitChannels = defineChannels("@genkit-ai/ai", {
24247
24911
  generate: channel({
@@ -24666,7 +25330,7 @@ function createDeferredSpanFinalizer(span) {
24666
25330
  try {
24667
25331
  await callback();
24668
25332
  } catch (error) {
24669
- span.log({ error: errorMessage(error) });
25333
+ span.log({ error: toLoggedError(error) });
24670
25334
  } finally {
24671
25335
  span.end();
24672
25336
  }
@@ -24820,9 +25484,6 @@ function pickNumberMetrics(values) {
24820
25484
  })
24821
25485
  );
24822
25486
  }
24823
- function errorMessage(error) {
24824
- return error instanceof Error ? error.message : String(error);
24825
- }
24826
25487
  function stringValue(value) {
24827
25488
  return typeof value === "string" ? value : void 0;
24828
25489
  }
@@ -25367,10 +26028,22 @@ var flueChannels = defineChannels("@flue/runtime", {
25367
26028
 
25368
26029
  // src/instrumentation/plugins/flue-plugin.ts
25369
26030
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
26031
+ var FLUE_INSTRUMENTATION_KEY = /* @__PURE__ */ Symbol.for("braintrust.flue.instrumentation");
25370
26032
  var FLUE_OBSERVE_BRIDGE = /* @__PURE__ */ Symbol.for("braintrust.flue.observe-bridge");
25371
- var braintrustFlueObserver = (event, ctx) => {
26033
+ function observeFlue(event, ctx) {
25372
26034
  getObserveBridge().handle(event, ctx);
25373
- };
26035
+ }
26036
+ var interceptFlueExecution = (operation, ctx, next) => getObserveBridge().intercept(operation, ctx, next);
26037
+ var braintrustFlueObserver = Object.assign(
26038
+ observeFlue,
26039
+ {
26040
+ dispose() {
26041
+ },
26042
+ interceptor: interceptFlueExecution,
26043
+ key: FLUE_INSTRUMENTATION_KEY,
26044
+ observe: observeFlue
26045
+ }
26046
+ );
25374
26047
  var FluePlugin = class extends BasePlugin {
25375
26048
  onEnable() {
25376
26049
  this.unsubscribers.push(enableFlueAutoInstrumentation());
@@ -25385,16 +26058,16 @@ var FluePlugin = class extends BasePlugin {
25385
26058
  function enableFlueAutoInstrumentation() {
25386
26059
  const state = getAutoState();
25387
26060
  state.refCount += 1;
25388
- if (!state.handlers) {
25389
- const channel2 = flueChannels.createContext.tracingChannel();
26061
+ if (!state.createContextHandlers) {
26062
+ const createContextChannel = flueChannels.createContext.tracingChannel();
25390
26063
  const handlers = {
25391
26064
  end: (event) => {
25392
26065
  subscribeToFlueContext(event.result, state);
25393
26066
  }
25394
26067
  };
25395
- channel2.subscribe(handlers);
25396
- state.channel = channel2;
25397
- state.handlers = handlers;
26068
+ createContextChannel.subscribe(handlers);
26069
+ state.createContextChannel = createContextChannel;
26070
+ state.createContextHandlers = handlers;
25398
26071
  }
25399
26072
  let released = false;
25400
26073
  return () => {
@@ -25438,8 +26111,8 @@ function releaseAutoState(state) {
25438
26111
  return;
25439
26112
  }
25440
26113
  try {
25441
- if (state.channel && state.handlers) {
25442
- state.channel.unsubscribe(state.handlers);
26114
+ if (state.createContextChannel && state.createContextHandlers) {
26115
+ state.createContextChannel.unsubscribe(state.createContextHandlers);
25443
26116
  }
25444
26117
  } finally {
25445
26118
  Reflect.deleteProperty(globalThis, FLUE_AUTO_STATE);
@@ -25497,7 +26170,7 @@ function isObservableFlueContext(value) {
25497
26170
  }
25498
26171
  function isFlueEvent(event) {
25499
26172
  const type = Reflect.get(event, "type");
25500
- return type === "run_start" || type === "run_end" || type === "operation_start" || type === "operation" || type === "turn_request" || type === "turn" || type === "tool_start" || type === "tool_call" || type === "task_start" || type === "task" || type === "compaction_start" || type === "compaction";
26173
+ return type === "run_start" || type === "run_resume" || type === "run_end" || type === "operation_start" || type === "operation" || type === "turn_request" || type === "turn" || type === "tool_start" || type === "tool_call" || type === "tool" || type === "task_start" || type === "task" || type === "compaction_start" || type === "compaction";
25501
26174
  }
25502
26175
  function flueContextFromUnknown(ctx) {
25503
26176
  if (!isObjectLike(ctx)) {
@@ -25544,11 +26217,97 @@ var FlueObserveBridge = class {
25544
26217
  this.toolsByKey.clear();
25545
26218
  this.turnsByKey.clear();
25546
26219
  }
26220
+ intercept(operation, executionContext, next) {
26221
+ let span;
26222
+ try {
26223
+ span = this.spanForExecutionOperation(operation, executionContext);
26224
+ } catch (error) {
26225
+ logInstrumentationError3("Flue execution interceptor", error);
26226
+ }
26227
+ return span ? runWithCurrentSpanStore(span, next) : next();
26228
+ }
26229
+ spanForExecutionOperation(operation, executionContext) {
26230
+ switch (operation.type) {
26231
+ case "workflow":
26232
+ return this.ensureWorkflowSpanForExecution(operation, executionContext);
26233
+ case "agent":
26234
+ return this.operationsById.get(operation.operationId)?.span;
26235
+ case "model":
26236
+ return this.turnsByKey.get(operation.turnId)?.span;
26237
+ case "tool":
26238
+ return this.spanForToolExecution(operation, executionContext);
26239
+ case "task":
26240
+ return this.tasksById.get(operation.taskId)?.span;
26241
+ default:
26242
+ return void 0;
26243
+ }
26244
+ }
26245
+ ensureWorkflowSpanForExecution(operation, executionContext) {
26246
+ const existing = this.runsById.get(operation.runId);
26247
+ if (existing) {
26248
+ return existing.span;
26249
+ }
26250
+ const ctx = flueContextFromUnknown(executionContext.eventContext) ?? flueContextFromUnknown({
26251
+ id: operation.workflowName,
26252
+ runId: operation.runId
26253
+ });
26254
+ if (operation.phase === "resume") {
26255
+ this.handleRunResume(
26256
+ {
26257
+ eventIndex: 0,
26258
+ runId: operation.runId,
26259
+ startedAt: operation.startedAt,
26260
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
26261
+ type: "run_resume",
26262
+ v: 3,
26263
+ workflowName: operation.workflowName
26264
+ },
26265
+ ctx
26266
+ );
26267
+ } else {
26268
+ this.handleRunStart(
26269
+ {
26270
+ eventIndex: 0,
26271
+ input: void 0,
26272
+ runId: operation.runId,
26273
+ startedAt: operation.startedAt,
26274
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
26275
+ type: "run_start",
26276
+ v: 3,
26277
+ workflowName: operation.workflowName
26278
+ },
26279
+ ctx
26280
+ );
26281
+ }
26282
+ return this.runsById.get(operation.runId)?.span;
26283
+ }
26284
+ spanForToolExecution(operation, executionContext) {
26285
+ const contextual = this.toolsByKey.get(
26286
+ toolKey({ ...executionContext, toolCallId: operation.toolCallId })
26287
+ );
26288
+ if (contextual) {
26289
+ return contextual.span;
26290
+ }
26291
+ let fallback2;
26292
+ for (const state of this.toolsByKey.values()) {
26293
+ if (state.metadata["flue.tool_call_id"] !== operation.toolCallId) {
26294
+ continue;
26295
+ }
26296
+ if (state.metadata["flue.tool_name"] === operation.toolName) {
26297
+ return state.span;
26298
+ }
26299
+ fallback2 ??= state.span;
26300
+ }
26301
+ return fallback2;
26302
+ }
25547
26303
  handleEvent(event, ctx) {
25548
26304
  switch (event.type) {
25549
26305
  case "run_start":
25550
26306
  this.handleRunStart(event, ctx);
25551
26307
  return;
26308
+ case "run_resume":
26309
+ this.handleRunResume(event, ctx);
26310
+ return;
25552
26311
  case "run_end":
25553
26312
  this.handleRunEnd(event);
25554
26313
  return;
@@ -25568,6 +26327,7 @@ var FlueObserveBridge = class {
25568
26327
  this.handleToolStart(event);
25569
26328
  return;
25570
26329
  case "tool_call":
26330
+ case "tool":
25571
26331
  this.handleToolCall(event);
25572
26332
  return;
25573
26333
  case "task_start":
@@ -25591,30 +26351,61 @@ var FlueObserveBridge = class {
25591
26351
  return;
25592
26352
  }
25593
26353
  const workflowName = event.workflowName ?? event.owner?.workflowName ?? (typeof ctx?.id === "string" ? ctx.id : "unknown");
26354
+ const input = flueRunInput(event);
25594
26355
  const metadata = {
25595
- ...extractPayloadMetadata(event.payload),
26356
+ ...extractPayloadMetadata(input),
25596
26357
  ...extractEventMetadata(event, ctx),
25597
26358
  ...workflowName ? { "flue.workflow_name": workflowName } : {},
25598
26359
  provider: "flue"
25599
26360
  };
26361
+ const existing = this.runsById.get(event.runId);
26362
+ if (existing) {
26363
+ existing.metadata = metadata;
26364
+ safeLog3(existing.span, { input, metadata });
26365
+ return;
26366
+ }
25600
26367
  const span = startSpan({
25601
26368
  name: `workflow:${workflowName}`,
25602
26369
  spanAttributes: { type: "task" /* TASK */ },
25603
26370
  startTime: eventTime(event.startedAt ?? event.timestamp),
25604
26371
  event: {
25605
- input: event.payload,
26372
+ input,
25606
26373
  metadata
25607
26374
  }
25608
26375
  });
25609
- const activeContext = enterCurrentFlueSpan(span);
25610
- this.runsById.set(event.runId, { activeContext, metadata, span });
26376
+ this.runsById.set(event.runId, { metadata, span });
26377
+ }
26378
+ handleRunResume(event, ctx) {
26379
+ if (!event.runId) {
26380
+ return;
26381
+ }
26382
+ const workflowName = event.workflowName ?? (typeof ctx?.id === "string" ? ctx.id : "unknown");
26383
+ const metadata = {
26384
+ ...extractEventMetadata(event, ctx),
26385
+ ...workflowName ? { "flue.workflow_name": workflowName } : {},
26386
+ "flue.workflow_phase": "resume",
26387
+ provider: "flue"
26388
+ };
26389
+ const existing = this.runsById.get(event.runId);
26390
+ if (existing) {
26391
+ existing.metadata = metadata;
26392
+ safeLog3(existing.span, { metadata });
26393
+ return;
26394
+ }
26395
+ const span = startSpan({
26396
+ name: `workflow:${workflowName}`,
26397
+ spanAttributes: { type: "task" /* TASK */ },
26398
+ startTime: eventTime(event.startedAt ?? event.timestamp),
26399
+ event: { metadata }
26400
+ });
26401
+ this.runsById.set(event.runId, { metadata, span });
25611
26402
  }
25612
26403
  handleRunEnd(event) {
25613
26404
  const state = this.runsById.get(event.runId);
25614
26405
  this.finishPendingSpansForRun(event);
25615
26406
  if (state) {
25616
26407
  safeLog3(state.span, {
25617
- ...event.isError ? { error: errorToString(event.error) } : {},
26408
+ ...event.isError ? { error: toLoggedError(event.error) } : {},
25618
26409
  metadata: {
25619
26410
  ...state.metadata,
25620
26411
  ...extractEventMetadata(event),
@@ -25625,7 +26416,6 @@ var FlueObserveBridge = class {
25625
26416
  });
25626
26417
  safeEnd(state.span, eventTime(event.timestamp));
25627
26418
  this.runsById.delete(event.runId);
25628
- restoreCurrentFlueSpan(state.activeContext);
25629
26419
  }
25630
26420
  void flush().catch((error) => {
25631
26421
  logInstrumentationError3("Flue flush", error);
@@ -25663,7 +26453,7 @@ var FlueObserveBridge = class {
25663
26453
  };
25664
26454
  this.finishPendingChildrenForOperation(event, output);
25665
26455
  safeLog3(state.span, {
25666
- ...event.isError ? { error: errorToString(event.error) } : {},
26456
+ ...event.isError ? { error: toLoggedError(event.error) } : {},
25667
26457
  metadata,
25668
26458
  metrics: durationMetrics2(event.durationMs),
25669
26459
  output
@@ -25676,31 +26466,33 @@ var FlueObserveBridge = class {
25676
26466
  if (!key) {
25677
26467
  return;
25678
26468
  }
26469
+ const input = flueTurnRequestInput(event);
26470
+ const model = flueTurnRequestModel(event);
26471
+ const provider = flueTurnRequestProvider(event);
26472
+ const api = flueTurnRequestApi(event);
26473
+ const reasoning = flueTurnRequestReasoning(event);
25679
26474
  const metadata = {
25680
26475
  ...extractEventMetadata(event),
25681
- ...event.api ? { "flue.api": event.api } : {},
25682
- ...event.model ? { model: event.model, "flue.model": event.model } : {},
25683
- ...event.provider ? { provider: event.provider } : { provider: "flue" },
25684
- ...event.provider ? { "flue.provider": event.provider } : {},
26476
+ ...api ? { "flue.api": api } : {},
26477
+ ...model ? { model, "flue.model": model } : {},
26478
+ ...provider ? { provider } : { provider: "flue" },
26479
+ ...provider ? { "flue.provider": provider } : {},
25685
26480
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {},
25686
- ...event.reasoning ? { reasoning: event.reasoning } : {},
25687
- ...event.input?.systemPrompt ? { "flue.system_prompt": event.input.systemPrompt } : {},
25688
- ...event.input?.tools ? { tools: event.input.tools } : {}
26481
+ ...reasoning ? { reasoning } : {},
26482
+ ...input?.systemPrompt ? { "flue.system_prompt": input.systemPrompt } : {},
26483
+ ...input?.tools ? { tools: input.tools } : {}
25689
26484
  };
25690
26485
  const parent = this.parentSpanForTurn(event);
25691
26486
  const span = startFlueSpan(parent, {
25692
- name: `llm:${event.model ?? event.purpose ?? "unknown"}`,
26487
+ name: `llm:${model ?? event.purpose ?? "unknown"}`,
25693
26488
  spanAttributes: { type: "llm" /* LLM */ },
25694
26489
  startTime: eventTime(event.timestamp),
25695
26490
  event: {
25696
- input: event.input?.messages,
26491
+ input: input?.messages,
25697
26492
  metadata
25698
26493
  }
25699
26494
  });
25700
- this.logOperationInput(
25701
- event.operationId,
25702
- event.input?.messages ?? event.input
25703
- );
26495
+ this.logOperationInput(event.operationId, input?.messages ?? input);
25704
26496
  this.turnsByKey.set(key, { metadata, span });
25705
26497
  }
25706
26498
  handleTurn(event) {
@@ -25709,25 +26501,32 @@ var FlueObserveBridge = class {
25709
26501
  return;
25710
26502
  }
25711
26503
  const state = this.turnsByKey.get(key) ?? this.startSyntheticTurn(event);
26504
+ const model = flueTurnModel(event);
26505
+ const provider = flueTurnProvider(event);
26506
+ const api = flueTurnApi(event);
26507
+ const stopReason = flueTurnStopReason(event);
26508
+ const usage = flueTurnUsage(event);
26509
+ const output = flueTurnOutput(event);
26510
+ const error = flueTurnError(event);
25712
26511
  const metadata = {
25713
26512
  ...state.metadata,
25714
26513
  ...extractEventMetadata(event),
25715
- ...event.api ? { "flue.api": event.api } : {},
25716
- ...event.model ? { model: event.model, "flue.model": event.model } : {},
25717
- ...event.provider ? { provider: event.provider } : {},
25718
- ...event.provider ? { "flue.provider": event.provider } : {},
26514
+ ...api ? { "flue.api": api } : {},
26515
+ ...model ? { model, "flue.model": model } : {},
26516
+ ...provider ? { provider } : {},
26517
+ ...provider ? { "flue.provider": provider } : {},
25719
26518
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {},
25720
- ...event.stopReason ? { "flue.stop_reason": event.stopReason } : {},
26519
+ ...stopReason ? { "flue.stop_reason": stopReason } : {},
25721
26520
  ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
25722
26521
  };
25723
26522
  safeLog3(state.span, {
25724
- ...event.isError ? { error: errorToString(event.error) } : {},
26523
+ ...event.isError ? { error: toLoggedError(error) } : {},
25725
26524
  metadata,
25726
26525
  metrics: {
25727
26526
  ...durationMetrics2(event.durationMs),
25728
- ...metricsFromUsage(event.usage)
26527
+ ...metricsFromUsage(usage)
25729
26528
  },
25730
- output: event.output
26529
+ output
25731
26530
  });
25732
26531
  safeEnd(state.span, eventTime(event.timestamp));
25733
26532
  this.turnsByKey.delete(key);
@@ -25736,6 +26535,7 @@ var FlueObserveBridge = class {
25736
26535
  if (!event.toolCallId) {
25737
26536
  return;
25738
26537
  }
26538
+ const input = flueToolInput(event);
25739
26539
  const metadata = {
25740
26540
  ...extractEventMetadata(event),
25741
26541
  ...event.toolName ? { "flue.tool_name": event.toolName } : {},
@@ -25748,12 +26548,11 @@ var FlueObserveBridge = class {
25748
26548
  spanAttributes: { type: "tool" /* TOOL */ },
25749
26549
  startTime: eventTime(event.timestamp),
25750
26550
  event: {
25751
- input: event.args,
26551
+ input,
25752
26552
  metadata
25753
26553
  }
25754
26554
  });
25755
- const activeContext = enterCurrentFlueSpan(span);
25756
- this.toolsByKey.set(toolKey(event), { activeContext, metadata, span });
26555
+ this.toolsByKey.set(toolKey(event), { metadata, span });
25757
26556
  }
25758
26557
  handleToolCall(event) {
25759
26558
  if (!event.toolCallId) {
@@ -25761,6 +26560,8 @@ var FlueObserveBridge = class {
25761
26560
  }
25762
26561
  const key = toolKey(event);
25763
26562
  const state = this.toolsByKey.get(key) ?? this.startSyntheticTool(event);
26563
+ const output = flueToolOutput(event);
26564
+ const error = flueToolError(event);
25764
26565
  const metadata = {
25765
26566
  ...state.metadata,
25766
26567
  ...extractEventMetadata(event),
@@ -25769,14 +26570,13 @@ var FlueObserveBridge = class {
25769
26570
  ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
25770
26571
  };
25771
26572
  safeLog3(state.span, {
25772
- ...event.isError ? { error: errorToString(event.result) } : {},
26573
+ ...event.isError ? { error: toLoggedError(error) } : {},
25773
26574
  metadata,
25774
26575
  metrics: durationMetrics2(event.durationMs),
25775
- output: event.result
26576
+ output
25776
26577
  });
25777
26578
  safeEnd(state.span, eventTime(event.timestamp));
25778
26579
  this.toolsByKey.delete(key);
25779
- restoreCurrentFlueSpan(state.activeContext);
25780
26580
  }
25781
26581
  handleTaskStart(event) {
25782
26582
  if (!event.taskId) {
@@ -25807,7 +26607,7 @@ var FlueObserveBridge = class {
25807
26607
  return;
25808
26608
  }
25809
26609
  safeLog3(state.span, {
25810
- ...event.isError ? { error: errorToString(event.result) } : {},
26610
+ ...event.isError ? { error: toLoggedError(event.result) } : {},
25811
26611
  metadata: {
25812
26612
  ...state.metadata,
25813
26613
  ...extractEventMetadata(event),
@@ -25945,16 +26745,19 @@ var FlueObserveBridge = class {
25945
26745
  return { metadata, span };
25946
26746
  }
25947
26747
  startSyntheticTurn(event) {
26748
+ const model = flueTurnModel(event);
26749
+ const provider = flueTurnProvider(event);
26750
+ const api = flueTurnApi(event);
25948
26751
  const metadata = {
25949
26752
  ...extractEventMetadata(event),
25950
- ...event.api ? { "flue.api": event.api } : {},
25951
- ...event.model ? { model: event.model, "flue.model": event.model } : {},
25952
- ...event.provider ? { provider: event.provider } : { provider: "flue" },
25953
- ...event.provider ? { "flue.provider": event.provider } : {},
26753
+ ...api ? { "flue.api": api } : {},
26754
+ ...model ? { model, "flue.model": model } : {},
26755
+ ...provider ? { provider } : { provider: "flue" },
26756
+ ...provider ? { "flue.provider": provider } : {},
25954
26757
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {}
25955
26758
  };
25956
26759
  const span = startFlueSpan(this.parentSpanForEvent(event), {
25957
- name: `llm:${event.model ?? event.purpose ?? "unknown"}`,
26760
+ name: `llm:${model ?? event.purpose ?? "unknown"}`,
25958
26761
  spanAttributes: { type: "llm" /* LLM */ },
25959
26762
  startTime: eventTime(event.timestamp),
25960
26763
  event: { metadata }
@@ -26108,6 +26911,60 @@ function extractPayloadMetadata(payload) {
26108
26911
  }
26109
26912
  return Object.fromEntries(Object.entries(metadata));
26110
26913
  }
26914
+ function flueRunInput(event) {
26915
+ return event.input !== void 0 ? event.input : event.payload;
26916
+ }
26917
+ function flueTurnRequestInput(event) {
26918
+ return event.request?.input ?? event.input;
26919
+ }
26920
+ function flueTurnRequestModel(event) {
26921
+ return event.request?.model ?? event.model;
26922
+ }
26923
+ function flueTurnRequestProvider(event) {
26924
+ return event.request?.providerName ?? event.provider ?? event.request?.providerId;
26925
+ }
26926
+ function flueTurnRequestApi(event) {
26927
+ return event.request?.api ?? event.api;
26928
+ }
26929
+ function flueTurnRequestReasoning(event) {
26930
+ return event.request?.reasoning ?? event.reasoning;
26931
+ }
26932
+ function flueTurnModel(event) {
26933
+ return event.request?.model ?? event.model;
26934
+ }
26935
+ function flueTurnProvider(event) {
26936
+ return event.request?.providerName ?? event.provider ?? event.request?.providerId;
26937
+ }
26938
+ function flueTurnApi(event) {
26939
+ return event.request?.api ?? event.api;
26940
+ }
26941
+ function flueTurnUsage(event) {
26942
+ return event.response?.usage ?? event.usage;
26943
+ }
26944
+ function flueTurnOutput(event) {
26945
+ return event.response?.output ?? event.output;
26946
+ }
26947
+ function flueTurnStopReason(event) {
26948
+ return event.response?.stopReason ?? event.stopReason;
26949
+ }
26950
+ function flueTurnError(event) {
26951
+ return event.response?.error ?? event.response?.errorInfo?.message ?? event.error;
26952
+ }
26953
+ function flueToolInput(event) {
26954
+ if (event.args !== void 0) {
26955
+ return event.args;
26956
+ }
26957
+ if (event.arguments !== void 0) {
26958
+ return event.arguments;
26959
+ }
26960
+ return event.input;
26961
+ }
26962
+ function flueToolOutput(event) {
26963
+ return event.output !== void 0 ? event.output : event.result;
26964
+ }
26965
+ function flueToolError(event) {
26966
+ return event.error ?? event.errorInfo?.message ?? flueToolOutput(event);
26967
+ }
26111
26968
  function operationOutput(event) {
26112
26969
  if (event.operationKind === "prompt" || event.operationKind === "skill") {
26113
26970
  return llmResultFromOperationResult(event.result);
@@ -26181,33 +27038,14 @@ function stateMatchesRun(state, runId) {
26181
27038
  function startFlueSpan(parent, args) {
26182
27039
  return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
26183
27040
  }
26184
- function enterCurrentFlueSpan(span) {
26185
- const contextManager = _internalGetGlobalState()?.contextManager;
26186
- const store = contextManager ? Reflect.get(contextManager, BRAINTRUST_CURRENT_SPAN_STORE) : void 0;
26187
- if (!contextManager || !isCurrentSpanStore(store)) {
26188
- return void 0;
26189
- }
26190
- const previous = store.getStore();
26191
- try {
26192
- store.enterWith(contextManager.wrapSpanForStore(span));
26193
- return { previous, store };
26194
- } catch (error) {
26195
- logInstrumentationError3("Flue context propagation", error);
26196
- return void 0;
26197
- }
26198
- }
26199
- function isCurrentSpanStore(value) {
26200
- return isObjectLike(value) && typeof Reflect.get(value, "enterWith") === "function" && typeof Reflect.get(value, "getStore") === "function";
26201
- }
26202
- function restoreCurrentFlueSpan(activeContext) {
26203
- if (!activeContext) {
26204
- return;
26205
- }
26206
- try {
26207
- activeContext.store.enterWith(activeContext.previous);
26208
- } catch (error) {
26209
- logInstrumentationError3("Flue context restoration", error);
27041
+ function runWithCurrentSpanStore(span, next) {
27042
+ const state = _internalGetGlobalState();
27043
+ const contextManager = state?.contextManager;
27044
+ const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
27045
+ if (contextManager && typeof currentSpanStore?.run === "function") {
27046
+ return currentSpanStore.run(contextManager.wrapSpanForStore(span), next);
26210
27047
  }
27048
+ return withCurrent(span, () => next());
26211
27049
  }
26212
27050
  function safeLog3(span, event) {
26213
27051
  try {
@@ -26223,19 +27061,6 @@ function safeEnd(span, endTime) {
26223
27061
  logInstrumentationError3("Flue span end", error);
26224
27062
  }
26225
27063
  }
26226
- function errorToString(error) {
26227
- if (error instanceof Error) {
26228
- return error.message;
26229
- }
26230
- if (typeof error === "string") {
26231
- return error;
26232
- }
26233
- try {
26234
- return JSON.stringify(error);
26235
- } catch {
26236
- return String(error);
26237
- }
26238
- }
26239
27064
  function logInstrumentationError3(label, error) {
26240
27065
  console.error(`Error in ${label} instrumentation:`, error);
26241
27066
  }
@@ -27065,11 +27890,11 @@ function patchAssistantMessageStream(stream, promptState, llmState) {
27065
27890
  function recordPiAssistantMessageEvent(promptState, llmState, event) {
27066
27891
  recordFirstTokenMetric(llmState, event);
27067
27892
  const message = "message" in event ? event.message : void 0;
27068
- const errorMessage2 = "error" in event ? event.error : void 0;
27893
+ const errorMessage = "error" in event ? event.error : void 0;
27069
27894
  if (event.type === "done" && isPiAssistantMessage(message)) {
27070
27895
  finishPiLlmSpan(promptState, llmState, message);
27071
- } else if (event.type === "error" && isPiAssistantMessage(errorMessage2)) {
27072
- finishPiLlmSpan(promptState, llmState, errorMessage2);
27896
+ } else if (event.type === "error" && isPiAssistantMessage(errorMessage)) {
27897
+ finishPiLlmSpan(promptState, llmState, errorMessage);
27073
27898
  }
27074
27899
  }
27075
27900
  function recordFirstTokenMetric(state, event) {
@@ -27163,7 +27988,7 @@ function finishPiToolSpan(state, event) {
27163
27988
  };
27164
27989
  try {
27165
27990
  safeLog4(toolState.span, {
27166
- ...event.isError ? { error: stringifyUnknown2(event.result) } : {},
27991
+ ...event.isError ? { error: toLoggedError(event.result) } : {},
27167
27992
  metadata,
27168
27993
  output: event.result
27169
27994
  });
@@ -27195,7 +28020,7 @@ async function finalizePiPromptRun(state, error) {
27195
28020
  };
27196
28021
  try {
27197
28022
  safeLog4(state.span, {
27198
- ...error ? { error: stringifyUnknown2(error) } : {},
28023
+ ...error ? { error: toLoggedError(error) } : {},
27199
28024
  metadata,
27200
28025
  metrics: {
27201
28026
  ...cleanMetrics5(state.metrics),
@@ -27255,7 +28080,7 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
27255
28080
  }
27256
28081
  try {
27257
28082
  safeLog4(llmState.span, {
27258
- ...error || messageError ? { error: stringifyUnknown2(error ?? messageError) } : {},
28083
+ ...error || messageError ? { error: toLoggedError(error ?? messageError) } : {},
27259
28084
  metadata: {
27260
28085
  ...llmState.metadata,
27261
28086
  ...message ? extractAssistantMetadata(message) : {}
@@ -27271,7 +28096,7 @@ function finishOpenToolSpans(state, error) {
27271
28096
  for (const [, toolState] of state.activeToolSpans) {
27272
28097
  try {
27273
28098
  safeLog4(toolState.span, {
27274
- error: error ? stringifyUnknown2(error) : "Pi tool did not complete"
28099
+ error: error ? toLoggedError(error) : "Pi tool did not complete"
27275
28100
  });
27276
28101
  toolState.span.end();
27277
28102
  } finally {
@@ -27543,10 +28368,10 @@ function stringifyArguments(value) {
27543
28368
  try {
27544
28369
  return JSON.stringify(value);
27545
28370
  } catch {
27546
- return stringifyUnknown2(value);
28371
+ return stringifyUnknown(value);
27547
28372
  }
27548
28373
  }
27549
- function stringifyUnknown2(value) {
28374
+ function stringifyUnknown(value) {
27550
28375
  if (value instanceof Error) {
27551
28376
  return value.message;
27552
28377
  }
@@ -27570,10 +28395,790 @@ function logInstrumentationError4(context, error) {
27570
28395
  debugLogger.debug(`${context}:`, error);
27571
28396
  }
27572
28397
 
27573
- // src/instrumentation/braintrust-plugin.ts
27574
- function getIntegrationConfig(integrations, key) {
27575
- return integrations[key];
28398
+ // src/instrumentation/plugins/strands-agent-sdk-channels.ts
28399
+ var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
28400
+ agentStream: channel({
28401
+ channelName: "Agent.stream",
28402
+ kind: "sync-stream"
28403
+ }),
28404
+ graphStream: channel({
28405
+ channelName: "Graph.stream",
28406
+ kind: "sync-stream"
28407
+ }),
28408
+ swarmStream: channel({
28409
+ channelName: "Swarm.stream",
28410
+ kind: "sync-stream"
28411
+ })
28412
+ });
28413
+
28414
+ // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
28415
+ var StrandsAgentSDKPlugin = class extends BasePlugin {
28416
+ activeChildParents = /* @__PURE__ */ new WeakMap();
28417
+ onEnable() {
28418
+ this.subscribeToAgentStream();
28419
+ this.subscribeToMultiAgentStream(
28420
+ strandsAgentSDKChannels.graphStream,
28421
+ "Graph.stream"
28422
+ );
28423
+ this.subscribeToMultiAgentStream(
28424
+ strandsAgentSDKChannels.swarmStream,
28425
+ "Swarm.stream"
28426
+ );
28427
+ }
28428
+ onDisable() {
28429
+ for (const unsubscribe of this.unsubscribers) {
28430
+ unsubscribe();
28431
+ }
28432
+ this.unsubscribers = [];
28433
+ }
28434
+ subscribeToAgentStream() {
28435
+ const channel2 = strandsAgentSDKChannels.agentStream.tracingChannel();
28436
+ const states = /* @__PURE__ */ new WeakMap();
28437
+ const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(channel2);
28438
+ const handlers = {
28439
+ start: (event) => {
28440
+ const state = startAgentStream(event, this.activeChildParents);
28441
+ if (state) {
28442
+ states.set(event, state);
28443
+ }
28444
+ },
28445
+ end: (event) => {
28446
+ const state = states.get(event);
28447
+ if (!state) {
28448
+ return;
28449
+ }
28450
+ const result = event.result;
28451
+ if (isAsyncIterable(result)) {
28452
+ patchStreamIfNeeded(result, {
28453
+ aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
28454
+ onChunk: (chunk) => handleAgentStreamEvent(state, chunk),
28455
+ onComplete: () => {
28456
+ finalizeAgentStream(state);
28457
+ states.delete(event);
28458
+ },
28459
+ onError: (error) => {
28460
+ finalizeAgentStream(state, error);
28461
+ states.delete(event);
28462
+ }
28463
+ });
28464
+ return;
28465
+ }
28466
+ finalizeAgentStream(state, void 0, result);
28467
+ states.delete(event);
28468
+ },
28469
+ error: (event) => {
28470
+ const state = states.get(event);
28471
+ if (!state || !event.error) {
28472
+ return;
28473
+ }
28474
+ finalizeAgentStream(state, event.error);
28475
+ states.delete(event);
28476
+ }
28477
+ };
28478
+ channel2.subscribe(handlers);
28479
+ this.unsubscribers.push(() => {
28480
+ unbindAutoInstrumentationSuppression?.();
28481
+ channel2.unsubscribe(handlers);
28482
+ });
28483
+ }
28484
+ subscribeToMultiAgentStream(channel2, operation) {
28485
+ const tracingChannel2 = channel2.tracingChannel();
28486
+ const states = /* @__PURE__ */ new WeakMap();
28487
+ const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(tracingChannel2);
28488
+ const handlers = {
28489
+ start: (event) => {
28490
+ const state = startMultiAgentStream(
28491
+ event,
28492
+ operation,
28493
+ this.activeChildParents
28494
+ );
28495
+ if (state) {
28496
+ states.set(event, state);
28497
+ }
28498
+ },
28499
+ end: (event) => {
28500
+ const state = states.get(event);
28501
+ if (!state) {
28502
+ return;
28503
+ }
28504
+ const result = event.result;
28505
+ if (isAsyncIterable(result)) {
28506
+ patchStreamIfNeeded(result, {
28507
+ aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
28508
+ onChunk: (chunk) => handleMultiAgentStreamEvent(
28509
+ state,
28510
+ chunk,
28511
+ this.activeChildParents
28512
+ ),
28513
+ onComplete: () => {
28514
+ finalizeMultiAgentStream(state, this.activeChildParents);
28515
+ states.delete(event);
28516
+ },
28517
+ onError: (error) => {
28518
+ finalizeMultiAgentStream(state, this.activeChildParents, error);
28519
+ states.delete(event);
28520
+ }
28521
+ });
28522
+ return;
28523
+ }
28524
+ finalizeMultiAgentStream(
28525
+ state,
28526
+ this.activeChildParents,
28527
+ void 0,
28528
+ result
28529
+ );
28530
+ states.delete(event);
28531
+ },
28532
+ error: (event) => {
28533
+ const state = states.get(event);
28534
+ if (!state || !event.error) {
28535
+ return;
28536
+ }
28537
+ finalizeMultiAgentStream(state, this.activeChildParents, event.error);
28538
+ states.delete(event);
28539
+ }
28540
+ };
28541
+ tracingChannel2.subscribe(handlers);
28542
+ this.unsubscribers.push(() => {
28543
+ unbindAutoInstrumentationSuppression?.();
28544
+ tracingChannel2.unsubscribe(handlers);
28545
+ });
28546
+ }
28547
+ };
28548
+ function startAgentStream(event, activeChildParents) {
28549
+ const agent = extractAgent(event);
28550
+ const model = agent?.model;
28551
+ const metadata = {
28552
+ ...extractAgentMetadata2(agent),
28553
+ ...extractModelMetadata3(model),
28554
+ "strands.operation": "Agent.stream",
28555
+ provider: extractProvider(model),
28556
+ ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
28557
+ };
28558
+ const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
28559
+ const span = parentSpan ? withCurrent(
28560
+ parentSpan,
28561
+ () => startSpan({
28562
+ event: {
28563
+ input: event.arguments[0],
28564
+ metadata
28565
+ },
28566
+ name: formatAgentSpanName(agent),
28567
+ spanAttributes: { type: "task" /* TASK */ }
28568
+ })
28569
+ ) : startSpan({
28570
+ event: {
28571
+ input: event.arguments[0],
28572
+ metadata
28573
+ },
28574
+ name: formatAgentSpanName(agent),
28575
+ spanAttributes: { type: "task" /* TASK */ }
28576
+ });
28577
+ return {
28578
+ activeTools: /* @__PURE__ */ new Map(),
28579
+ finalized: false,
28580
+ metadata,
28581
+ span,
28582
+ startTime: getCurrentUnixTimestamp()
28583
+ };
28584
+ }
28585
+ function startMultiAgentStream(event, operation, activeChildParents) {
28586
+ const orchestrator = extractOrchestrator(event);
28587
+ const metadata = {
28588
+ "strands.operation": operation,
28589
+ provider: "strands",
28590
+ ...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {},
28591
+ ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
28592
+ };
28593
+ const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
28594
+ const span = parentSpan ? withCurrent(
28595
+ parentSpan,
28596
+ () => startSpan({
28597
+ event: {
28598
+ input: event.arguments[0],
28599
+ metadata
28600
+ },
28601
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28602
+ spanAttributes: { type: "task" /* TASK */ }
28603
+ })
28604
+ ) : startSpan({
28605
+ event: {
28606
+ input: event.arguments[0],
28607
+ metadata
28608
+ },
28609
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28610
+ spanAttributes: { type: "task" /* TASK */ }
28611
+ });
28612
+ return {
28613
+ activeNodes: /* @__PURE__ */ new Map(),
28614
+ finalized: false,
28615
+ handoffs: [],
28616
+ metadata,
28617
+ operation,
28618
+ orchestrator,
28619
+ span,
28620
+ startTime: getCurrentUnixTimestamp()
28621
+ };
28622
+ }
28623
+ function handleAgentStreamEvent(state, event) {
28624
+ try {
28625
+ switch (event.type) {
28626
+ case "beforeModelCallEvent":
28627
+ startModelSpan(state, event);
28628
+ break;
28629
+ case "modelStreamUpdateEvent":
28630
+ collectModelStreamMetadata(state, event);
28631
+ break;
28632
+ case "afterModelCallEvent":
28633
+ finalizeModelSpan(state, event);
28634
+ break;
28635
+ case "beforeToolCallEvent":
28636
+ startToolSpan2(state, event);
28637
+ break;
28638
+ case "afterToolCallEvent":
28639
+ finalizeToolSpan(state, event);
28640
+ break;
28641
+ case "toolResultEvent":
28642
+ finalizeToolSpanFromResult(state, event.result);
28643
+ break;
28644
+ case "agentResultEvent":
28645
+ safeLog5(state.span, {
28646
+ metadata: {
28647
+ ...state.metadata,
28648
+ ...extractAgentResultMetadata(event.result)
28649
+ },
28650
+ metrics: {
28651
+ ...buildDurationMetrics4(state.startTime),
28652
+ ...parseUsage(event.result?.metrics?.accumulatedUsage)
28653
+ },
28654
+ output: extractAgentResultOutput(event.result)
28655
+ });
28656
+ break;
28657
+ default:
28658
+ break;
28659
+ }
28660
+ } catch (error) {
28661
+ logInstrumentationError5("Strands Agent SDK event", error);
28662
+ }
28663
+ }
28664
+ function handleMultiAgentStreamEvent(state, event, activeChildParents) {
28665
+ try {
28666
+ switch (event.type) {
28667
+ case "beforeNodeCallEvent":
28668
+ startNodeSpan(state, event, activeChildParents);
28669
+ break;
28670
+ case "nodeResultEvent":
28671
+ logNodeResult(state, event);
28672
+ break;
28673
+ case "afterNodeCallEvent":
28674
+ finalizeNodeSpan(state, event, activeChildParents);
28675
+ break;
28676
+ case "multiAgentHandoffEvent":
28677
+ collectHandoff(state, event);
28678
+ break;
28679
+ case "multiAgentResultEvent":
28680
+ safeLog5(state.span, {
28681
+ metadata: {
28682
+ ...state.metadata,
28683
+ ...state.handoffs.length > 0 ? { "strands.handoffs": state.handoffs } : {},
28684
+ ...extractMultiAgentResultMetadata(event.result)
28685
+ },
28686
+ metrics: {
28687
+ ...buildDurationMetrics4(state.startTime),
28688
+ ...parseUsage(event.result?.usage)
28689
+ },
28690
+ output: extractMultiAgentResultOutput(event.result)
28691
+ });
28692
+ break;
28693
+ default:
28694
+ break;
28695
+ }
28696
+ } catch (error) {
28697
+ logInstrumentationError5("Strands Agent SDK multi-agent event", error);
28698
+ }
28699
+ }
28700
+ function startModelSpan(state, event) {
28701
+ if (state.activeModel) {
28702
+ finalizeModelSpan(state);
28703
+ }
28704
+ const model = event.model ?? event.agent?.model;
28705
+ const metadata = {
28706
+ ...extractModelMetadata3(model),
28707
+ "strands.operation": "model.stream",
28708
+ ...typeof event.projectedInputTokens === "number" ? { "strands.projected_input_tokens": event.projectedInputTokens } : {},
28709
+ provider: extractProvider(model)
28710
+ };
28711
+ const span = withCurrent(
28712
+ state.span,
28713
+ () => startSpan({
28714
+ event: {
28715
+ input: Array.isArray(event.agent?.messages) ? event.agent.messages : void 0,
28716
+ metadata
28717
+ },
28718
+ name: formatModelSpanName(model),
28719
+ spanAttributes: { type: "llm" /* LLM */ }
28720
+ })
28721
+ );
28722
+ state.activeModel = {
28723
+ metadata,
28724
+ metrics: typeof event.projectedInputTokens === "number" ? { prompt_tokens: event.projectedInputTokens } : {},
28725
+ span,
28726
+ startTime: getCurrentUnixTimestamp()
28727
+ };
28728
+ }
28729
+ function collectModelStreamMetadata(state, event) {
28730
+ if (!state.activeModel || event.event?.type !== "modelMetadataEvent") {
28731
+ return;
28732
+ }
28733
+ Object.assign(state.activeModel.metrics, parseUsage(event.event.usage));
28734
+ Object.assign(
28735
+ state.activeModel.metrics,
28736
+ parseModelMetrics(event.event.metrics)
28737
+ );
28738
+ }
28739
+ function finalizeModelSpan(state, event) {
28740
+ const modelState = state.activeModel;
28741
+ if (!modelState) {
28742
+ return;
28743
+ }
28744
+ state.activeModel = void 0;
28745
+ const metadata = {
28746
+ ...modelState.metadata,
28747
+ ...event?.attemptCount ? { "strands.model.attempt": event.attemptCount } : {},
28748
+ ...event?.stopData?.stopReason ? { "strands.stop_reason": event.stopData.stopReason } : {}
28749
+ };
28750
+ const metrics = {
28751
+ ...buildDurationMetrics4(modelState.startTime),
28752
+ ...modelState.metrics
28753
+ };
28754
+ safeLog5(modelState.span, {
28755
+ ...event?.error ? { error: toLoggedError(event.error) } : {},
28756
+ metadata,
28757
+ metrics: cleanMetrics6(metrics),
28758
+ output: event?.stopData?.message
28759
+ });
28760
+ modelState.span.end();
28761
+ }
28762
+ function startToolSpan2(state, event) {
28763
+ const toolUse = event.toolUse;
28764
+ const key = toolKey2(toolUse);
28765
+ if (state.activeTools.has(key)) {
28766
+ finalizeToolSpanState(state.activeTools.get(key));
28767
+ state.activeTools.delete(key);
28768
+ }
28769
+ const name = extractToolName3(toolUse, event.tool);
28770
+ const span = withCurrent(
28771
+ state.span,
28772
+ () => startSpan({
28773
+ event: {
28774
+ input: toolUse?.input,
28775
+ metadata: {
28776
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
28777
+ "gen_ai.tool.name": name,
28778
+ "strands.operation": "tool.call",
28779
+ "strands.tool.name": name,
28780
+ provider: "strands"
28781
+ }
28782
+ },
28783
+ name: `tool: ${name}`,
28784
+ spanAttributes: { type: "tool" /* TOOL */ }
28785
+ })
28786
+ );
28787
+ state.activeTools.set(key, {
28788
+ span,
28789
+ startTime: getCurrentUnixTimestamp(),
28790
+ toolUse
28791
+ });
28792
+ }
28793
+ function finalizeToolSpan(state, event) {
28794
+ const key = toolKey2(event.toolUse);
28795
+ const toolState = state.activeTools.get(key);
28796
+ if (!toolState) {
28797
+ return;
28798
+ }
28799
+ state.activeTools.delete(key);
28800
+ finalizeToolSpanState(toolState, {
28801
+ error: event.error,
28802
+ result: event.result,
28803
+ toolUse: event.toolUse
28804
+ });
28805
+ }
28806
+ function finalizeToolSpanFromResult(state, result) {
28807
+ if (!isObject(result)) {
28808
+ return;
28809
+ }
28810
+ const toolUseId = result.toolUseId;
28811
+ if (typeof toolUseId !== "string") {
28812
+ return;
28813
+ }
28814
+ const toolState = state.activeTools.get(toolUseId);
28815
+ if (!toolState) {
28816
+ return;
28817
+ }
28818
+ state.activeTools.delete(toolUseId);
28819
+ finalizeToolSpanState(toolState, { result });
28820
+ }
28821
+ function finalizeToolSpanState(toolState, data = {}) {
28822
+ if (!toolState) {
28823
+ return;
28824
+ }
28825
+ const result = data.result;
28826
+ safeLog5(toolState.span, {
28827
+ ...data.error ? { error: toLoggedError(data.error) } : {},
28828
+ metadata: {
28829
+ ...data.toolUse?.toolUseId ?? toolState.toolUse?.toolUseId ? {
28830
+ "gen_ai.tool.call.id": data.toolUse?.toolUseId ?? toolState.toolUse?.toolUseId
28831
+ } : {},
28832
+ ...extractToolName3(data.toolUse ?? toolState.toolUse) ? {
28833
+ "gen_ai.tool.name": extractToolName3(
28834
+ data.toolUse ?? toolState.toolUse
28835
+ )
28836
+ } : {},
28837
+ ...isObject(result) && typeof result.status === "string" ? { "strands.tool.status": result.status } : {}
28838
+ },
28839
+ metrics: buildDurationMetrics4(toolState.startTime),
28840
+ output: result
28841
+ });
28842
+ toolState.span.end();
28843
+ }
28844
+ function startNodeSpan(state, event, activeChildParents) {
28845
+ const nodeId = event.nodeId ?? "unknown";
28846
+ const node = findNode(event.orchestrator ?? state.orchestrator, nodeId);
28847
+ const child = extractNodeChild(node);
28848
+ const metadata = {
28849
+ "strands.node.id": nodeId,
28850
+ ...node?.type ? { "strands.node.type": node.type } : {},
28851
+ "strands.operation": "node.call",
28852
+ provider: "strands"
28853
+ };
28854
+ const span = withCurrent(
28855
+ state.span,
28856
+ () => startSpan({
28857
+ event: { metadata },
28858
+ name: `node: ${nodeId}`,
28859
+ spanAttributes: { type: "task" /* TASK */ }
28860
+ })
28861
+ );
28862
+ const nodeState = {
28863
+ ...child ? { child } : {},
28864
+ span,
28865
+ startTime: getCurrentUnixTimestamp()
28866
+ };
28867
+ state.activeNodes.set(nodeId, nodeState);
28868
+ if (child) {
28869
+ pushChildParent(activeChildParents, child, span);
28870
+ }
28871
+ }
28872
+ function logNodeResult(state, event) {
28873
+ const nodeId = event.nodeId ?? event.result?.nodeId ?? "unknown";
28874
+ const nodeState = state.activeNodes.get(nodeId);
28875
+ if (!nodeState) {
28876
+ return;
28877
+ }
28878
+ safeLog5(nodeState.span, {
28879
+ ...event.result?.error ? { error: toLoggedError(event.result.error) } : {},
28880
+ metadata: {
28881
+ ...event.nodeType ? { "strands.node.type": event.nodeType } : {},
28882
+ ...event.result?.status ? { "strands.node.status": event.result.status } : {}
28883
+ },
28884
+ metrics: {
28885
+ ...buildDurationMetrics4(nodeState.startTime),
28886
+ ...typeof event.result?.duration === "number" ? { "strands.node.duration_ms": event.result.duration } : {},
28887
+ ...parseUsage(event.result?.usage)
28888
+ },
28889
+ output: extractNodeResultOutput(event.result)
28890
+ });
28891
+ }
28892
+ function finalizeNodeSpan(state, event, activeChildParents) {
28893
+ const nodeId = event.nodeId ?? "unknown";
28894
+ const nodeState = state.activeNodes.get(nodeId);
28895
+ if (!nodeState) {
28896
+ return;
28897
+ }
28898
+ state.activeNodes.delete(nodeId);
28899
+ if (event.error) {
28900
+ safeLog5(nodeState.span, { error: toLoggedError(event.error) });
28901
+ }
28902
+ nodeState.span.end();
28903
+ if (nodeState.child) {
28904
+ popChildParent(activeChildParents, nodeState.child, nodeState.span);
28905
+ }
28906
+ }
28907
+ function collectHandoff(state, event) {
28908
+ state.handoffs.push({
28909
+ ...event.source ? { source: event.source } : {},
28910
+ ...Array.isArray(event.targets) ? { targets: event.targets } : {}
28911
+ });
28912
+ }
28913
+ function finalizeAgentStream(state, error, output) {
28914
+ if (state.finalized) {
28915
+ return;
28916
+ }
28917
+ state.finalized = true;
28918
+ finalizeModelSpan(state);
28919
+ for (const toolState of state.activeTools.values()) {
28920
+ finalizeToolSpanState(toolState);
28921
+ }
28922
+ state.activeTools.clear();
28923
+ safeLog5(state.span, {
28924
+ ...error ? { error: toLoggedError(error) } : {},
28925
+ metrics: buildDurationMetrics4(state.startTime),
28926
+ ...output !== void 0 ? { output } : {}
28927
+ });
28928
+ state.span.end();
28929
+ }
28930
+ function finalizeMultiAgentStream(state, activeChildParents, error, output) {
28931
+ if (state.finalized) {
28932
+ return;
28933
+ }
28934
+ state.finalized = true;
28935
+ for (const nodeState of state.activeNodes.values()) {
28936
+ if (nodeState.child) {
28937
+ popChildParent(activeChildParents, nodeState.child, nodeState.span);
28938
+ }
28939
+ nodeState.span.end();
28940
+ }
28941
+ state.activeNodes.clear();
28942
+ safeLog5(state.span, {
28943
+ ...error ? { error: toLoggedError(error) } : {},
28944
+ metrics: buildDurationMetrics4(state.startTime),
28945
+ ...output !== void 0 ? { output } : {}
28946
+ });
28947
+ state.span.end();
28948
+ }
28949
+ function extractAgent(event) {
28950
+ const candidate = event.agent ?? event.self;
28951
+ return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
28952
+ }
28953
+ function extractOrchestrator(event) {
28954
+ const candidate = event.orchestrator ?? event.self;
28955
+ return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
28956
+ }
28957
+ function extractAgentMetadata2(agent) {
28958
+ return {
28959
+ ...agent?.id ? { "strands.agent.id": agent.id } : {},
28960
+ ...agent?.name ? { "strands.agent.name": agent.name } : {},
28961
+ ...agent?.description ? { "strands.agent.description": agent.description } : {}
28962
+ };
28963
+ }
28964
+ function extractModelMetadata3(model) {
28965
+ const config = getModelConfig(model);
28966
+ const modelName2 = extractModelName2(model);
28967
+ return {
28968
+ ...modelName2 ? { model: modelName2 } : {},
28969
+ ...config?.api ? { "strands.model.api": config.api } : {},
28970
+ ...model?.stateful !== void 0 ? { "strands.model.stateful": model.stateful } : {}
28971
+ };
28972
+ }
28973
+ function extractAgentResultMetadata(result) {
28974
+ return {
28975
+ ...result?.stopReason ? { "strands.stop_reason": result.stopReason } : {},
28976
+ ...typeof result?.metrics?.latestContextSize === "number" ? { "strands.context_size": result.metrics.latestContextSize } : {},
28977
+ ...typeof result?.metrics?.projectedContextSize === "number" ? {
28978
+ "strands.projected_context_size": result.metrics.projectedContextSize
28979
+ } : {}
28980
+ };
28981
+ }
28982
+ function extractMultiAgentResultMetadata(result) {
28983
+ return {
28984
+ ...result?.status ? { "strands.status": result.status } : {},
28985
+ ...typeof result?.duration === "number" ? { "strands.duration_ms": result.duration } : {}
28986
+ };
28987
+ }
28988
+ function extractProvider(model) {
28989
+ const config = getModelConfig(model);
28990
+ if (typeof config?.provider === "string") {
28991
+ return config.provider;
28992
+ }
28993
+ const constructorName = getConstructorName(model).toLowerCase();
28994
+ if (constructorName.includes("openai")) {
28995
+ return "openai";
28996
+ }
28997
+ if (constructorName.includes("anthropic")) {
28998
+ return "anthropic";
28999
+ }
29000
+ if (constructorName.includes("bedrock")) {
29001
+ return "bedrock";
29002
+ }
29003
+ if (constructorName.includes("google")) {
29004
+ return "google";
29005
+ }
29006
+ if (constructorName.includes("vercel")) {
29007
+ return "vercel";
29008
+ }
29009
+ return "strands";
29010
+ }
29011
+ function extractModelName2(model) {
29012
+ const config = getModelConfig(model);
29013
+ if (typeof model?.modelId === "string") {
29014
+ return model.modelId;
29015
+ }
29016
+ if (typeof config?.modelId === "string") {
29017
+ return config.modelId;
29018
+ }
29019
+ if (typeof config?.model === "string") {
29020
+ return config.model;
29021
+ }
29022
+ return void 0;
29023
+ }
29024
+ function getModelConfig(model) {
29025
+ if (!model || typeof model.getConfig !== "function") {
29026
+ return void 0;
29027
+ }
29028
+ try {
29029
+ return model.getConfig();
29030
+ } catch (error) {
29031
+ logInstrumentationError5("Strands Agent SDK model config", error);
29032
+ return void 0;
29033
+ }
29034
+ }
29035
+ function formatAgentSpanName(agent) {
29036
+ return agent?.name ? `Agent: ${agent.name}` : "Strands Agent";
29037
+ }
29038
+ function formatModelSpanName(model) {
29039
+ const modelName2 = extractModelName2(model);
29040
+ return modelName2 ? `Strands model: ${modelName2}` : "Strands model";
29041
+ }
29042
+ function extractAgentResultOutput(result) {
29043
+ if (!result) {
29044
+ return void 0;
29045
+ }
29046
+ if (result.structuredOutput !== void 0) {
29047
+ return result.structuredOutput;
29048
+ }
29049
+ if (result.lastMessage) {
29050
+ return result.lastMessage;
29051
+ }
29052
+ return result;
29053
+ }
29054
+ function extractMultiAgentResultOutput(result) {
29055
+ if (!result) {
29056
+ return void 0;
29057
+ }
29058
+ if (Array.isArray(result.content)) {
29059
+ return normalizeContentBlocks(result.content);
29060
+ }
29061
+ return result;
29062
+ }
29063
+ function extractNodeResultOutput(result) {
29064
+ if (!result) {
29065
+ return void 0;
29066
+ }
29067
+ if (result.structuredOutput !== void 0) {
29068
+ return result.structuredOutput;
29069
+ }
29070
+ if (Array.isArray(result.content)) {
29071
+ return normalizeContentBlocks(result.content);
29072
+ }
29073
+ return result;
29074
+ }
29075
+ function normalizeContentBlocks(blocks) {
29076
+ const text = blocks.map((block) => typeof block.text === "string" ? block.text : void 0).filter((part) => Boolean(part)).join("");
29077
+ return text.length > 0 ? text : blocks;
29078
+ }
29079
+ function parseUsage(usage) {
29080
+ const metrics = {};
29081
+ assignMetric(metrics, "prompt_tokens", usage?.inputTokens);
29082
+ assignMetric(metrics, "completion_tokens", usage?.outputTokens);
29083
+ assignMetric(metrics, "tokens", usage?.totalTokens);
29084
+ assignMetric(metrics, "prompt_cached_tokens", usage?.cacheReadInputTokens);
29085
+ assignMetric(
29086
+ metrics,
29087
+ "prompt_cache_creation_tokens",
29088
+ usage?.cacheWriteInputTokens
29089
+ );
29090
+ return metrics;
29091
+ }
29092
+ function parseModelMetrics(metrics) {
29093
+ const parsed = {};
29094
+ assignMetric(parsed, "strands.latency_ms", metrics?.latencyMs);
29095
+ assignMetric(
29096
+ parsed,
29097
+ "strands.time_to_first_byte_ms",
29098
+ metrics?.timeToFirstByteMs
29099
+ );
29100
+ return parsed;
29101
+ }
29102
+ function assignMetric(metrics, key, value) {
29103
+ if (typeof value === "number" && Number.isFinite(value)) {
29104
+ metrics[key] = value;
29105
+ }
29106
+ }
29107
+ function buildDurationMetrics4(startTime) {
29108
+ const end = getCurrentUnixTimestamp();
29109
+ return {
29110
+ duration: end - startTime,
29111
+ end,
29112
+ start: startTime
29113
+ };
29114
+ }
29115
+ function cleanMetrics6(metrics) {
29116
+ const cleaned = {};
29117
+ for (const [key, value] of Object.entries(metrics)) {
29118
+ if (Number.isFinite(value)) {
29119
+ cleaned[key] = value;
29120
+ }
29121
+ }
29122
+ return cleaned;
29123
+ }
29124
+ function extractToolName3(toolUse, tool) {
29125
+ return toolUse?.name ?? tool?.name ?? "unknown";
29126
+ }
29127
+ function toolKey2(toolUse) {
29128
+ return toolUse?.toolUseId ?? toolUse?.name ?? "unknown";
29129
+ }
29130
+ function findNode(orchestrator, nodeId) {
29131
+ try {
29132
+ return orchestrator?.nodes?.get(nodeId);
29133
+ } catch {
29134
+ return void 0;
29135
+ }
29136
+ }
29137
+ function extractNodeChild(node) {
29138
+ const child = node?.agent ?? node?.orchestrator;
29139
+ return isObject(child) ? child : void 0;
29140
+ }
29141
+ function pushChildParent(activeChildParents, child, span) {
29142
+ const parents = activeChildParents.get(child) ?? /* @__PURE__ */ new Set();
29143
+ parents.add(span);
29144
+ activeChildParents.set(child, parents);
29145
+ }
29146
+ function popChildParent(activeChildParents, child, span) {
29147
+ const parents = activeChildParents.get(child);
29148
+ if (!parents) {
29149
+ return;
29150
+ }
29151
+ parents.delete(span);
29152
+ if (parents.size === 0) {
29153
+ activeChildParents.delete(child);
29154
+ }
29155
+ }
29156
+ function getOnlyChildParent(activeChildParents, child) {
29157
+ const parents = activeChildParents.get(child);
29158
+ if (!parents || parents.size !== 1) {
29159
+ return void 0;
29160
+ }
29161
+ return parents.values().next().value;
29162
+ }
29163
+ function getConstructorName(value) {
29164
+ if (!isObject(value)) {
29165
+ return "";
29166
+ }
29167
+ const constructor = value.constructor;
29168
+ return typeof constructor === "function" && typeof constructor.name === "string" ? constructor.name : "";
29169
+ }
29170
+ function safeLog5(span, event) {
29171
+ try {
29172
+ span.log(event);
29173
+ } catch (error) {
29174
+ logInstrumentationError5("Strands Agent SDK span log", error);
29175
+ }
27576
29176
  }
29177
+ function logInstrumentationError5(context, error) {
29178
+ debugLogger.debug(`${context}:`, error);
29179
+ }
29180
+
29181
+ // src/instrumentation/braintrust-plugin.ts
27577
29182
  var BraintrustPlugin = class extends BasePlugin {
27578
29183
  config;
27579
29184
  openaiPlugin = null;
@@ -27591,11 +29196,13 @@ var BraintrustPlugin = class extends BasePlugin {
27591
29196
  googleADKPlugin = null;
27592
29197
  coherePlugin = null;
27593
29198
  groqPlugin = null;
29199
+ bedrockRuntimePlugin = null;
27594
29200
  genkitPlugin = null;
27595
29201
  gitHubCopilotPlugin = null;
27596
29202
  fluePlugin = null;
27597
29203
  langChainPlugin = null;
27598
29204
  piCodingAgentPlugin = null;
29205
+ strandsAgentSDKPlugin = null;
27599
29206
  constructor(config = {}) {
27600
29207
  super();
27601
29208
  this.config = config;
@@ -27662,6 +29269,10 @@ var BraintrustPlugin = class extends BasePlugin {
27662
29269
  this.groqPlugin = new GroqPlugin();
27663
29270
  this.groqPlugin.enable();
27664
29271
  }
29272
+ if (integrations.bedrock !== false && integrations.awsBedrock !== false && integrations.awsBedrockRuntime !== false) {
29273
+ this.bedrockRuntimePlugin = new BedrockRuntimePlugin();
29274
+ this.bedrockRuntimePlugin.enable();
29275
+ }
27665
29276
  if (integrations.genkit !== false) {
27666
29277
  this.genkitPlugin = new GenkitPlugin();
27667
29278
  this.genkitPlugin.enable();
@@ -27674,7 +29285,11 @@ var BraintrustPlugin = class extends BasePlugin {
27674
29285
  this.piCodingAgentPlugin = new PiCodingAgentPlugin();
27675
29286
  this.piCodingAgentPlugin.enable();
27676
29287
  }
27677
- if (getIntegrationConfig(integrations, "flue") !== false) {
29288
+ if (integrations.strandsAgentSDK !== false) {
29289
+ this.strandsAgentSDKPlugin = new StrandsAgentSDKPlugin();
29290
+ this.strandsAgentSDKPlugin.enable();
29291
+ }
29292
+ if (integrations.flue !== false) {
27678
29293
  this.fluePlugin = new FluePlugin();
27679
29294
  this.fluePlugin.enable();
27680
29295
  }
@@ -27744,6 +29359,10 @@ var BraintrustPlugin = class extends BasePlugin {
27744
29359
  this.groqPlugin.disable();
27745
29360
  this.groqPlugin = null;
27746
29361
  }
29362
+ if (this.bedrockRuntimePlugin) {
29363
+ this.bedrockRuntimePlugin.disable();
29364
+ this.bedrockRuntimePlugin = null;
29365
+ }
27747
29366
  if (this.genkitPlugin) {
27748
29367
  this.genkitPlugin.disable();
27749
29368
  this.genkitPlugin = null;
@@ -27756,6 +29375,10 @@ var BraintrustPlugin = class extends BasePlugin {
27756
29375
  this.piCodingAgentPlugin.disable();
27757
29376
  this.piCodingAgentPlugin = null;
27758
29377
  }
29378
+ if (this.strandsAgentSDKPlugin) {
29379
+ this.strandsAgentSDKPlugin.disable();
29380
+ this.strandsAgentSDKPlugin = null;
29381
+ }
27759
29382
  if (this.fluePlugin) {
27760
29383
  this.fluePlugin.disable();
27761
29384
  this.fluePlugin = null;
@@ -27780,6 +29403,10 @@ var envIntegrationAliases = {
27780
29403
  picodingagent: "piCodingAgent",
27781
29404
  picodingagentsdk: "piCodingAgent",
27782
29405
  "@earendil-works/pi-coding-agent": "piCodingAgent",
29406
+ strandsAgentSDK: "strandsAgentSDK",
29407
+ strandsagentsdk: "strandsAgentSDK",
29408
+ "strands-agent-sdk": "strandsAgentSDK",
29409
+ "@strands-agents/sdk": "strandsAgentSDK",
27783
29410
  anthropic: "anthropic",
27784
29411
  aisdk: "aisdk",
27785
29412
  "ai-sdk": "aisdk",
@@ -27810,6 +29437,12 @@ var envIntegrationAliases = {
27810
29437
  cohere: "cohere",
27811
29438
  groq: "groq",
27812
29439
  "groq-sdk": "groq",
29440
+ bedrock: "bedrock",
29441
+ "aws-bedrock": "awsBedrock",
29442
+ awsbedrock: "awsBedrock",
29443
+ "aws-bedrock-runtime": "awsBedrockRuntime",
29444
+ awsbedrockruntime: "awsBedrockRuntime",
29445
+ "@aws-sdk/client-bedrock-runtime": "awsBedrockRuntime",
27813
29446
  genkit: "genkit",
27814
29447
  "firebase-genkit": "genkit",
27815
29448
  githubcopilot: "gitHubCopilot",
@@ -27842,20 +29475,25 @@ function getDefaultInstrumentationIntegrations() {
27842
29475
  mistral: true,
27843
29476
  cohere: true,
27844
29477
  groq: true,
29478
+ bedrock: true,
29479
+ awsBedrock: true,
29480
+ awsBedrockRuntime: true,
27845
29481
  genkit: true,
27846
29482
  gitHubCopilot: true,
27847
29483
  langchain: true,
27848
29484
  langgraph: true,
27849
- piCodingAgent: true
29485
+ piCodingAgent: true,
29486
+ strandsAgentSDK: true
27850
29487
  };
27851
29488
  }
27852
29489
  function readDisabledInstrumentationEnvConfig(disabledList) {
27853
29490
  const integrations = {};
27854
29491
  if (disabledList) {
27855
29492
  for (const value of disabledList.split(",")) {
27856
- const sdk = value.trim().toLowerCase();
29493
+ const rawSdk = value.trim();
29494
+ const sdk = rawSdk.toLowerCase();
27857
29495
  if (sdk.length > 0) {
27858
- integrations[envIntegrationAliases[sdk] ?? sdk] = false;
29496
+ integrations[envIntegrationAliases[rawSdk] ?? envIntegrationAliases[sdk] ?? sdk] = false;
27859
29497
  }
27860
29498
  }
27861
29499
  }
@@ -28134,6 +29772,7 @@ __export(exports_exports, {
28134
29772
  wrapAISDKModel: () => wrapAISDKModel,
28135
29773
  wrapAgentClass: () => wrapAgentClass,
28136
29774
  wrapAnthropic: () => wrapAnthropic,
29775
+ wrapBedrockRuntime: () => wrapBedrockRuntime,
28137
29776
  wrapClaudeAgentSDK: () => wrapClaudeAgentSDK,
28138
29777
  wrapCohere: () => wrapCohere,
28139
29778
  wrapCopilotClient: () => wrapCopilotClient,
@@ -28151,6 +29790,7 @@ __export(exports_exports, {
28151
29790
  wrapOpenRouter: () => wrapOpenRouter,
28152
29791
  wrapOpenRouterAgent: () => wrapOpenRouterAgent,
28153
29792
  wrapPiCodingAgentSDK: () => wrapPiCodingAgentSDK,
29793
+ wrapStrandsAgentSDK: () => wrapStrandsAgentSDK,
28154
29794
  wrapTraced: () => wrapTraced,
28155
29795
  wrapVitest: () => wrapVitest
28156
29796
  });
@@ -30259,6 +31899,174 @@ function patchAgentSessionClass(AgentSession) {
30259
31899
  });
30260
31900
  }
30261
31901
 
31902
+ // src/wrappers/strands-agent-sdk.ts
31903
+ var WRAPPED_CLASS = /* @__PURE__ */ Symbol.for("braintrust.strands-agent-sdk.wrapped-class");
31904
+ var WRAPPED_INSTANCE = /* @__PURE__ */ Symbol.for(
31905
+ "braintrust.strands-agent-sdk.wrapped-instance"
31906
+ );
31907
+ function wrapStrandsAgentSDK(sdk) {
31908
+ if (!sdk || typeof sdk !== "object") {
31909
+ return sdk;
31910
+ }
31911
+ const maybeSDK = sdk;
31912
+ if (typeof maybeSDK.Agent !== "function" && typeof maybeSDK.Graph !== "function" && typeof maybeSDK.Swarm !== "function") {
31913
+ console.warn("Unsupported Strands Agent SDK. Not wrapping.");
31914
+ return sdk;
31915
+ }
31916
+ const target = isModuleNamespace4(sdk) ? Object.setPrototypeOf({}, sdk) : sdk;
31917
+ return new Proxy(target, {
31918
+ get(target2, prop, receiver) {
31919
+ const value = Reflect.get(target2, prop, receiver);
31920
+ if (prop === "Agent" && typeof value === "function") {
31921
+ return wrapAgentClass2(value);
31922
+ }
31923
+ if (prop === "Graph" && typeof value === "function") {
31924
+ return wrapMultiAgentClass(
31925
+ value,
31926
+ "graph"
31927
+ );
31928
+ }
31929
+ if (prop === "Swarm" && typeof value === "function") {
31930
+ return wrapMultiAgentClass(
31931
+ value,
31932
+ "swarm"
31933
+ );
31934
+ }
31935
+ return value;
31936
+ }
31937
+ });
31938
+ }
31939
+ function isModuleNamespace4(obj) {
31940
+ if (!obj || typeof obj !== "object") {
31941
+ return false;
31942
+ }
31943
+ if (obj.constructor?.name === "Module") {
31944
+ return true;
31945
+ }
31946
+ const keys = Object.keys(obj);
31947
+ if (keys.length === 0) {
31948
+ return false;
31949
+ }
31950
+ const descriptor = Object.getOwnPropertyDescriptor(obj, keys[0]);
31951
+ return descriptor ? !descriptor.configurable && !descriptor.writable : false;
31952
+ }
31953
+ function wrapAgentClass2(AgentClass) {
31954
+ if (AgentClass[WRAPPED_CLASS]) {
31955
+ return AgentClass;
31956
+ }
31957
+ return new Proxy(AgentClass, {
31958
+ get(target, prop, receiver) {
31959
+ if (prop === WRAPPED_CLASS) {
31960
+ return true;
31961
+ }
31962
+ const value = Reflect.get(target, prop, receiver);
31963
+ return typeof value === "function" ? value.bind(target) : value;
31964
+ },
31965
+ construct(target, args, newTarget) {
31966
+ const instance = Reflect.construct(target, args, newTarget);
31967
+ return wrapAgentInstance(instance);
31968
+ }
31969
+ });
31970
+ }
31971
+ function wrapMultiAgentClass(MultiAgentClass, kind) {
31972
+ if (MultiAgentClass[WRAPPED_CLASS]) {
31973
+ return MultiAgentClass;
31974
+ }
31975
+ return new Proxy(MultiAgentClass, {
31976
+ get(target, prop, receiver) {
31977
+ if (prop === WRAPPED_CLASS) {
31978
+ return true;
31979
+ }
31980
+ const value = Reflect.get(target, prop, receiver);
31981
+ return typeof value === "function" ? value.bind(target) : value;
31982
+ },
31983
+ construct(target, args, newTarget) {
31984
+ const instance = Reflect.construct(target, args, newTarget);
31985
+ return wrapMultiAgentInstance(instance, kind);
31986
+ }
31987
+ });
31988
+ }
31989
+ function wrapAgentInstance(agent) {
31990
+ if (!agent || typeof agent !== "object") {
31991
+ return agent;
31992
+ }
31993
+ if (agent[WRAPPED_INSTANCE]) {
31994
+ return agent;
31995
+ }
31996
+ const proxy = new Proxy(agent, {
31997
+ get(target, prop, receiver) {
31998
+ if (prop === WRAPPED_INSTANCE) {
31999
+ return true;
32000
+ }
32001
+ const value = Reflect.get(target, prop, receiver);
32002
+ if (prop === "stream" && typeof value === "function") {
32003
+ return function(args, options) {
32004
+ const callArgs = [args, options];
32005
+ return strandsAgentSDKChannels.agentStream.traceSync(
32006
+ () => Reflect.apply(value, target, callArgs),
32007
+ {
32008
+ agent: proxy,
32009
+ arguments: callArgs,
32010
+ self: proxy
32011
+ }
32012
+ );
32013
+ };
32014
+ }
32015
+ if (prop === "invoke" && typeof value === "function") {
32016
+ return async function(args, options) {
32017
+ return consumeAsyncGenerator(proxy.stream(args, options));
32018
+ };
32019
+ }
32020
+ return typeof value === "function" ? value.bind(target) : value;
32021
+ }
32022
+ });
32023
+ return proxy;
32024
+ }
32025
+ function wrapMultiAgentInstance(orchestrator, kind) {
32026
+ if (!orchestrator || typeof orchestrator !== "object") {
32027
+ return orchestrator;
32028
+ }
32029
+ if (orchestrator[WRAPPED_INSTANCE]) {
32030
+ return orchestrator;
32031
+ }
32032
+ const proxy = new Proxy(orchestrator, {
32033
+ get(target, prop, receiver) {
32034
+ if (prop === WRAPPED_INSTANCE) {
32035
+ return true;
32036
+ }
32037
+ const value = Reflect.get(target, prop, receiver);
32038
+ if (prop === "stream" && typeof value === "function") {
32039
+ return function(input, options) {
32040
+ const callArgs = [input, options];
32041
+ const channel2 = kind === "graph" ? strandsAgentSDKChannels.graphStream : strandsAgentSDKChannels.swarmStream;
32042
+ return channel2.traceSync(
32043
+ () => Reflect.apply(value, target, callArgs),
32044
+ {
32045
+ arguments: callArgs,
32046
+ orchestrator: proxy,
32047
+ self: proxy
32048
+ }
32049
+ );
32050
+ };
32051
+ }
32052
+ if (prop === "invoke" && typeof value === "function") {
32053
+ return async function(input, options) {
32054
+ return consumeAsyncGenerator(proxy.stream(input, options));
32055
+ };
32056
+ }
32057
+ return typeof value === "function" ? value.bind(target) : value;
32058
+ }
32059
+ });
32060
+ return proxy;
32061
+ }
32062
+ async function consumeAsyncGenerator(generator) {
32063
+ let result = await generator.next();
32064
+ while (!result.done) {
32065
+ result = await generator.next();
32066
+ }
32067
+ return result.value;
32068
+ }
32069
+
30262
32070
  // src/wrappers/google-genai.ts
30263
32071
  function wrapGoogleGenAI(googleGenAI) {
30264
32072
  if (!googleGenAI || typeof googleGenAI !== "object") {
@@ -30411,7 +32219,7 @@ function wrapGoogleADK(adkModule) {
30411
32219
  );
30412
32220
  }
30413
32221
  if ((prop === "LlmAgent" || prop === "Agent" || prop === "SequentialAgent" || prop === "ParallelAgent" || prop === "LoopAgent") && typeof value === "function") {
30414
- return wrapAgentClass2(value);
32222
+ return wrapAgentClass3(value);
30415
32223
  }
30416
32224
  if (prop === "FunctionTool" && typeof value === "function") {
30417
32225
  return wrapToolClass(value);
@@ -30447,15 +32255,15 @@ function wrapRunnerRunAsync(runner) {
30447
32255
  });
30448
32256
  };
30449
32257
  }
30450
- function wrapAgentClass2(AgentClass) {
32258
+ function wrapAgentClass3(AgentClass) {
30451
32259
  return new Proxy(AgentClass, {
30452
32260
  construct(target, args) {
30453
32261
  const instance = Reflect.construct(target, args);
30454
- return wrapAgentInstance(instance);
32262
+ return wrapAgentInstance2(instance);
30455
32263
  }
30456
32264
  });
30457
32265
  }
30458
- function wrapAgentInstance(agent) {
32266
+ function wrapAgentInstance2(agent) {
30459
32267
  return new Proxy(agent, {
30460
32268
  get(target, prop, receiver) {
30461
32269
  if (prop === "runAsync") {
@@ -31427,6 +33235,90 @@ function wrapEmbeddingsCreate2(create) {
31427
33235
  });
31428
33236
  }
31429
33237
 
33238
+ // src/wrappers/bedrock-runtime.ts
33239
+ function wrapBedrockRuntime(client) {
33240
+ if (isSupportedBedrockRuntimeClient(client)) {
33241
+ return bedrockRuntimeProxy(client);
33242
+ }
33243
+ console.warn("Unsupported Bedrock Runtime library. Not wrapping.");
33244
+ return client;
33245
+ }
33246
+ var bedrockRuntimeProxyCache = /* @__PURE__ */ new WeakMap();
33247
+ var BEDROCK_RUNTIME_OPERATION_METHODS = /* @__PURE__ */ new Set([
33248
+ "converse",
33249
+ "converseStream",
33250
+ "invokeModel",
33251
+ "invokeModelWithBidirectionalStream",
33252
+ "invokeModelWithResponseStream"
33253
+ ]);
33254
+ function isRecord6(value) {
33255
+ return typeof value === "object" && value !== null;
33256
+ }
33257
+ function isSupportedBedrockRuntimeClient(value) {
33258
+ return isRecord6(value) && typeof value.send === "function";
33259
+ }
33260
+ function bedrockRuntimeProxy(client) {
33261
+ const cached = bedrockRuntimeProxyCache.get(client);
33262
+ if (cached) {
33263
+ return cached;
33264
+ }
33265
+ const privateMethodWorkaroundCache = /* @__PURE__ */ new WeakMap();
33266
+ const operationMethodCache = /* @__PURE__ */ new WeakMap();
33267
+ const proxy = new Proxy(client, {
33268
+ get(target, prop, receiver) {
33269
+ if (prop === "send") {
33270
+ return wrapSend(target.send.bind(target));
33271
+ }
33272
+ const value = Reflect.get(target, prop, receiver);
33273
+ if (typeof value !== "function") {
33274
+ return value;
33275
+ }
33276
+ if (typeof prop === "string" && BEDROCK_RUNTIME_OPERATION_METHODS.has(prop)) {
33277
+ const cachedValue2 = operationMethodCache.get(value);
33278
+ if (cachedValue2) {
33279
+ return cachedValue2;
33280
+ }
33281
+ const thisBoundValue2 = function(...args) {
33282
+ const thisArg = this === proxy ? proxy : this;
33283
+ const output = Reflect.apply(value, thisArg, args);
33284
+ return output === target ? proxy : output;
33285
+ };
33286
+ operationMethodCache.set(value, thisBoundValue2);
33287
+ return thisBoundValue2;
33288
+ }
33289
+ const cachedValue = privateMethodWorkaroundCache.get(value);
33290
+ if (cachedValue) {
33291
+ return cachedValue;
33292
+ }
33293
+ const thisBoundValue = function(...args) {
33294
+ const thisArg = this === proxy ? target : this;
33295
+ const output = Reflect.apply(value, thisArg, args);
33296
+ return output === target ? proxy : output;
33297
+ };
33298
+ privateMethodWorkaroundCache.set(value, thisBoundValue);
33299
+ return thisBoundValue;
33300
+ }
33301
+ });
33302
+ bedrockRuntimeProxyCache.set(client, proxy);
33303
+ return proxy;
33304
+ }
33305
+ function wrapSend(send) {
33306
+ return (command, optionsOrCb, cb) => {
33307
+ if (getBedrockRuntimeOperation(command) === void 0 || typeof optionsOrCb === "function" || typeof cb === "function") {
33308
+ return send(command, optionsOrCb, cb);
33309
+ }
33310
+ return bedrockRuntimeChannels.clientSend.tracePromise(
33311
+ () => runWithAutoInstrumentationSuppressed(
33312
+ () => send(command, optionsOrCb)
33313
+ ),
33314
+ {
33315
+ arguments: [command, optionsOrCb],
33316
+ span_info: buildBedrockRuntimeSpanInfo(command)
33317
+ }
33318
+ );
33319
+ };
33320
+ }
33321
+
31430
33322
  // src/wrappers/github-copilot.ts
31431
33323
  function isGitHubCopilotClient(value) {
31432
33324
  return value !== null && typeof value === "object" && typeof value.createSession === "function" && typeof value.resumeSession === "function";
@@ -34292,14 +36184,15 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
34292
36184
  return;
34293
36185
  }
34294
36186
  const eventDataset = experiment ? experiment.dataset : Dataset2.isDataset(evaluator.data) ? evaluator.data : void 0;
34295
- const inlineOrigin = datum.origin === void 0 ? void 0 : ObjectReference.parse(datum.origin);
34296
- const origin = inlineOrigin ?? (eventDataset && datum.id && datum._xact_id ? {
36187
+ const inlineDatasetOrigin = eventDataset && datum.id && datum._xact_id ? {
34297
36188
  object_type: "dataset",
34298
36189
  object_id: await eventDataset.id,
34299
36190
  id: datum.id,
34300
36191
  created: datum.created,
34301
36192
  _xact_id: datum._xact_id
34302
- } : void 0);
36193
+ } : void 0;
36194
+ const parsedDatumOrigin = ObjectReference.safeParse(datum.origin);
36195
+ const origin = inlineDatasetOrigin ?? (parsedDatumOrigin?.success ? parsedDatumOrigin.data : void 0);
34303
36196
  const baseEvent = {
34304
36197
  name: "eval",
34305
36198
  spanAttributes: {