braintrust 3.18.0 → 3.20.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 +3629 -1989
  3. package/dev/dist/index.mjs +4579 -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 +88 -6
  26. package/dist/browser.d.ts +88 -6
  27. package/dist/browser.js +2059 -161
  28. package/dist/browser.mjs +4851 -2953
  29. package/dist/chunk-F73OMF66.mjs +29266 -0
  30. package/dist/{chunk-73IYIIOL.js → chunk-LPC4W2WX.js} +54 -4
  31. package/dist/chunk-POCCIJAL.js +29266 -0
  32. package/dist/{chunk-BYFADLEZ.mjs → chunk-Y7W7WP6H.mjs} +53 -3
  33. package/dist/cli.js +3495 -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 +2059 -161
  37. package/dist/edge-light.mjs +4851 -2953
  38. package/dist/index.d.mts +88 -6
  39. package/dist/index.d.ts +88 -6
  40. package/dist/index.js +838 -27963
  41. package/dist/index.mjs +581 -27706
  42. package/dist/instrumentation/index.d.mts +75 -5
  43. package/dist/instrumentation/index.d.ts +75 -5
  44. package/dist/instrumentation/index.js +4578 -2937
  45. package/dist/instrumentation/index.mjs +4577 -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 +2059 -161
  53. package/dist/workerd.mjs +4851 -2953
  54. package/package.json +7 -1
package/dist/browser.js CHANGED
@@ -87,6 +87,7 @@ __export(browser_exports, {
87
87
  _internalSetInitialState: () => _internalSetInitialState,
88
88
  addAzureBlobHeaders: () => addAzureBlobHeaders,
89
89
  braintrustAISDKTelemetry: () => braintrustAISDKTelemetry,
90
+ braintrustFlueInstrumentation: () => braintrustFlueInstrumentation,
90
91
  braintrustFlueObserver: () => braintrustFlueObserver,
91
92
  braintrustStreamChunkSchema: () => braintrustStreamChunkSchema,
92
93
  buildLocalSummary: () => buildLocalSummary,
@@ -164,6 +165,7 @@ __export(browser_exports, {
164
165
  wrapAISDKModel: () => wrapAISDKModel,
165
166
  wrapAgentClass: () => wrapAgentClass,
166
167
  wrapAnthropic: () => wrapAnthropic,
168
+ wrapBedrockRuntime: () => wrapBedrockRuntime,
167
169
  wrapClaudeAgentSDK: () => wrapClaudeAgentSDK,
168
170
  wrapCohere: () => wrapCohere,
169
171
  wrapCopilotClient: () => wrapCopilotClient,
@@ -181,6 +183,7 @@ __export(browser_exports, {
181
183
  wrapOpenRouter: () => wrapOpenRouter,
182
184
  wrapOpenRouterAgent: () => wrapOpenRouterAgent,
183
185
  wrapPiCodingAgentSDK: () => wrapPiCodingAgentSDK,
186
+ wrapStrandsAgentSDK: () => wrapStrandsAgentSDK,
184
187
  wrapTraced: () => wrapTraced,
185
188
  wrapVitest: () => wrapVitest
186
189
  });
@@ -7819,15 +7822,15 @@ function getSpanParentObject(options) {
7819
7822
  return NOOP_SPAN;
7820
7823
  }
7821
7824
  function logError(span, error) {
7822
- let errorMessage2 = "<error>";
7825
+ let errorMessage = "<error>";
7823
7826
  let stackTrace = "";
7824
7827
  if (error instanceof Error) {
7825
- errorMessage2 = error.message;
7828
+ errorMessage = error.message;
7826
7829
  stackTrace = error.stack || "";
7827
7830
  } else {
7828
- errorMessage2 = String(error);
7831
+ errorMessage = String(error);
7829
7832
  }
7830
- span.log({ error: `${errorMessage2}
7833
+ span.log({ error: `${errorMessage}
7831
7834
 
7832
7835
  ${stackTrace}` });
7833
7836
  }
@@ -10140,7 +10143,10 @@ function patchStreamIfNeeded(stream, options) {
10140
10143
  let completed = false;
10141
10144
  stream.next = async (...args) => {
10142
10145
  try {
10143
- const result = await originalNext(...args);
10146
+ const result = await runNextWithWrapper(
10147
+ options,
10148
+ () => originalNext(...args)
10149
+ );
10144
10150
  if (result.done) {
10145
10151
  if (!completed) {
10146
10152
  completed = true;
@@ -10232,7 +10238,10 @@ function patchStreamIfNeeded(stream, options) {
10232
10238
  let completed = false;
10233
10239
  iterator.next = async function(...args) {
10234
10240
  try {
10235
- const result = await originalNext(...args);
10241
+ const result = await runNextWithWrapper(
10242
+ options,
10243
+ () => originalNext(...args)
10244
+ );
10236
10245
  if (result.done) {
10237
10246
  if (!completed) {
10238
10247
  completed = true;
@@ -10318,6 +10327,9 @@ function patchStreamIfNeeded(stream, options) {
10318
10327
  return stream;
10319
10328
  }
10320
10329
  }
10330
+ function runNextWithWrapper(options, callback) {
10331
+ return options.aroundNext ? options.aroundNext(callback) : callback();
10332
+ }
10321
10333
 
10322
10334
  // src/instrumentation/core/channel-tracing-utils.ts
10323
10335
  function hasChannelSpanInfo(value) {
@@ -10342,11 +10354,23 @@ function buildStartSpanArgs(config, event) {
10342
10354
  mergeDicts(spanAttributes, spanInfo.spanAttributes);
10343
10355
  }
10344
10356
  return {
10345
- name: typeof spanInfo?.name === "string" && spanInfo.name ? spanInfo.name : config.name,
10357
+ name: typeof spanInfo?.name === "string" && spanInfo.name ? spanInfo.name : resolveConfigName(config.name, event),
10346
10358
  spanAttributes,
10347
10359
  spanInfoMetadata: isObject(spanInfo?.metadata) ? spanInfo.metadata : void 0
10348
10360
  };
10349
10361
  }
10362
+ function resolveConfigName(name, event) {
10363
+ if (typeof name === "string") {
10364
+ return name;
10365
+ }
10366
+ try {
10367
+ const resolved = name(event.arguments ?? [], event);
10368
+ return resolved || "unknown";
10369
+ } catch (error) {
10370
+ debugLogger.error("Error resolving instrumentation span name:", error);
10371
+ return "unknown";
10372
+ }
10373
+ }
10350
10374
  function mergeInputMetadata(metadata, spanInfoMetadata) {
10351
10375
  if (!spanInfoMetadata) {
10352
10376
  return isObject(metadata) ? (
@@ -10706,6 +10730,26 @@ var BasePlugin = class {
10706
10730
  }
10707
10731
  };
10708
10732
 
10733
+ // src/instrumentation/core/logging.ts
10734
+ function toLoggedError(error) {
10735
+ if (error instanceof Error) {
10736
+ return error.message;
10737
+ }
10738
+ if (typeof error === "string") {
10739
+ return error;
10740
+ }
10741
+ try {
10742
+ const serialized = JSON.stringify(error);
10743
+ return serialized === void 0 ? String(error) : serialized;
10744
+ } catch {
10745
+ try {
10746
+ return String(error);
10747
+ } catch {
10748
+ return "<unserializable error>";
10749
+ }
10750
+ }
10751
+ }
10752
+
10709
10753
  // src/instrumentation/auto-instrumentation-suppression.ts
10710
10754
  var autoInstrumentationSuppressionStore;
10711
10755
  function suppressionStore() {
@@ -12213,7 +12257,7 @@ async function finalizeCodexRun(state, params = {}) {
12213
12257
  try {
12214
12258
  const error = params.error;
12215
12259
  safeLog(state.span, {
12216
- ...error ? { error: error instanceof Error ? error.message : String(error) } : {},
12260
+ ...error ? { error: toLoggedError(error) } : {},
12217
12261
  metadata: state.metadata,
12218
12262
  metrics,
12219
12263
  output
@@ -12319,7 +12363,7 @@ async function finishActiveLlmSpan(state, error) {
12319
12363
  state.activeLlmSpan = void 0;
12320
12364
  const output = buildLlmOutput(active);
12321
12365
  safeLog(active.span, {
12322
- ...error ? { error: error instanceof Error ? error.message : String(error) } : {},
12366
+ ...error ? { error: toLoggedError(error) } : {},
12323
12367
  metadata: active.metadata,
12324
12368
  ...output ? { output } : {}
12325
12369
  });
@@ -12842,9 +12886,6 @@ function extractAnthropicToolNames(tools) {
12842
12886
  }
12843
12887
  return toolNames;
12844
12888
  }
12845
- function toErrorMessage(error) {
12846
- return error instanceof Error ? error.message : String(error);
12847
- }
12848
12889
  function getAnthropicToolRunnerInput(args) {
12849
12890
  return args.length > 0 ? args[0] : void 0;
12850
12891
  }
@@ -12878,7 +12919,7 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
12878
12919
  return result;
12879
12920
  };
12880
12921
  const finalizeError = (error) => {
12881
- span.log({ error: toErrorMessage(error) });
12922
+ span.log({ error: toLoggedError(error) });
12882
12923
  throw error;
12883
12924
  };
12884
12925
  try {
@@ -13034,7 +13075,7 @@ function finalizeAnthropicToolRunnerError(state, error) {
13034
13075
  }
13035
13076
  state.finalized = true;
13036
13077
  state.span.log({
13037
- error: error instanceof Error ? error.message : String(error)
13078
+ error: toLoggedError(error)
13038
13079
  });
13039
13080
  state.span.end();
13040
13081
  }
@@ -13363,7 +13404,7 @@ function processAttachmentsInInput(input) {
13363
13404
  function coalesceInput(messages, system) {
13364
13405
  const input = (messages || []).slice();
13365
13406
  if (system) {
13366
- input.push({ role: "system", content: system });
13407
+ input.unshift({ role: "system", content: system });
13367
13408
  }
13368
13409
  return input;
13369
13410
  }
@@ -14950,9 +14991,7 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
14950
14991
  }
14951
14992
  span.log({ output: lastValue });
14952
14993
  } catch (error) {
14953
- span.log({
14954
- error: error instanceof Error ? error.message : String(error)
14955
- });
14994
+ span.log({ error: toLoggedError(error) });
14956
14995
  throw error;
14957
14996
  } finally {
14958
14997
  span.end();
@@ -15965,9 +16004,6 @@ function bindClaudeLocalToolContextToAsyncIterable(result, localToolContext) {
15965
16004
  var LOCAL_TOOL_HANDLER_WRAPPED = /* @__PURE__ */ Symbol.for(
15966
16005
  "braintrust.claude_agent_sdk.local_tool_handler_wrapped"
15967
16006
  );
15968
- function toErrorMessage2(error) {
15969
- return error instanceof Error ? error.message : String(error);
15970
- }
15971
16007
  function getToolUseIdFromExtra(extra) {
15972
16008
  if (!extra || typeof extra !== "object" || !("_meta" in extra)) {
15973
16009
  return void 0;
@@ -16014,7 +16050,7 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
16014
16050
  return result;
16015
16051
  };
16016
16052
  const finalizeError = (error) => {
16017
- span.log({ error: toErrorMessage2(error) });
16053
+ span.log({ error: toLoggedError(error) });
16018
16054
  span.end();
16019
16055
  throw error;
16020
16056
  };
@@ -17715,7 +17751,7 @@ async function handleToolUpdate(state, update) {
17715
17751
  toolCall
17716
17752
  });
17717
17753
  finishToolSpan(toolState, {
17718
- error: toolCall?.status === "error" ? stringifyUnknown(result) : void 0,
17754
+ error: toolCall?.status === "error" ? toLoggedError(result) : void 0,
17719
17755
  metadata: {
17720
17756
  "cursor_sdk.tool.status": toolCall?.status ?? "completed"
17721
17757
  },
@@ -17798,7 +17834,7 @@ async function handleToolMessage(state, message) {
17798
17834
  truncated: message.truncated
17799
17835
  });
17800
17836
  finishToolSpan(toolState, {
17801
- error: message.status === "error" ? stringifyUnknown(message.result) : void 0,
17837
+ error: message.status === "error" ? toLoggedError(message.result) : void 0,
17802
17838
  metadata: {
17803
17839
  "cursor_sdk.tool.status": message.status
17804
17840
  },
@@ -17849,7 +17885,7 @@ async function handleConversationStep(state, step) {
17849
17885
  toolCall
17850
17886
  });
17851
17887
  finishToolSpan(toolState, {
17852
- error: toolCall?.status === "error" ? stringifyUnknown(toolCall.result) : void 0,
17888
+ error: toolCall?.status === "error" ? toLoggedError(toolCall.result) : void 0,
17853
17889
  metadata: {
17854
17890
  "cursor_sdk.tool.status": toolCall?.status ?? "completed"
17855
17891
  },
@@ -17959,7 +17995,7 @@ async function finalizeCursorRun(state, params = {}) {
17959
17995
  };
17960
17996
  if (error) {
17961
17997
  safeLog2(state.span, {
17962
- error: error instanceof Error ? error.message : String(error),
17998
+ error: toLoggedError(error),
17963
17999
  metadata,
17964
18000
  metrics: {
17965
18001
  ...cleanMetrics2(state.metrics),
@@ -18145,19 +18181,6 @@ function getString(obj, key) {
18145
18181
  const value = obj?.[key];
18146
18182
  return typeof value === "string" ? value : void 0;
18147
18183
  }
18148
- function stringifyUnknown(value) {
18149
- if (value instanceof Error) {
18150
- return value.message;
18151
- }
18152
- if (typeof value === "string") {
18153
- return value;
18154
- }
18155
- try {
18156
- return JSON.stringify(value);
18157
- } catch {
18158
- return String(value);
18159
- }
18160
- }
18161
18184
  function safeLog2(span, event) {
18162
18185
  try {
18163
18186
  span.log(event);
@@ -20673,9 +20696,7 @@ function patchOpenRouterCallModelResult(args) {
20673
20696
  return;
20674
20697
  }
20675
20698
  ended = true;
20676
- span.log({
20677
- error: normalizeError(error).message
20678
- });
20699
+ span.log({ error: toLoggedError(error) });
20679
20700
  span.end();
20680
20701
  };
20681
20702
  const finalizeFromResponse = async (fallbackOutput) => {
@@ -21746,9 +21767,7 @@ function patchOpenRouterCallModelResult2(args) {
21746
21767
  return;
21747
21768
  }
21748
21769
  ended = true;
21749
- span.log({
21750
- error: normalizeError2(error).message
21751
- });
21770
+ span.log({ error: toLoggedError(error) });
21752
21771
  span.end();
21753
21772
  };
21754
21773
  const finalizeFromResponse = async (fallbackOutput) => {
@@ -24122,6 +24141,652 @@ function aggregateGroqReasoning(chunks) {
24122
24141
  return reasoning.length > 0 ? reasoning : void 0;
24123
24142
  }
24124
24143
 
24144
+ // src/instrumentation/plugins/bedrock-runtime-channels.ts
24145
+ var clientSendChannel = channel({
24146
+ channelName: "client.send",
24147
+ kind: "async"
24148
+ });
24149
+ var bedrockRuntimeChannels = defineChannels("aws-bedrock-runtime", {
24150
+ clientSend: clientSendChannel
24151
+ });
24152
+ var smithyCoreChannels = defineChannels("@smithy/core", {
24153
+ clientSend: clientSendChannel
24154
+ });
24155
+ var smithyClientChannels = defineChannels("@smithy/smithy-client", {
24156
+ clientSend: clientSendChannel
24157
+ });
24158
+
24159
+ // src/instrumentation/plugins/bedrock-runtime-common.ts
24160
+ var BEDROCK_RUNTIME_COMMAND_OPERATIONS = {
24161
+ ConverseCommand: "converse",
24162
+ ConverseStreamCommand: "converseStream",
24163
+ InvokeModelCommand: "invokeModel",
24164
+ InvokeModelWithBidirectionalStreamCommand: "invokeModelWithBidirectionalStream",
24165
+ InvokeModelWithResponseStreamCommand: "invokeModelWithResponseStream"
24166
+ };
24167
+ function getBedrockRuntimeCommandName(command) {
24168
+ if (!isObject(command) || !isObject(command.constructor)) {
24169
+ return void 0;
24170
+ }
24171
+ const input = command.input;
24172
+ if (!isObject(input) || typeof input.modelId !== "string") {
24173
+ return void 0;
24174
+ }
24175
+ const commandName = command.constructor.name;
24176
+ return isBedrockRuntimeCommandName(commandName) ? commandName : void 0;
24177
+ }
24178
+ function getBedrockRuntimeOperation(command) {
24179
+ const commandName = getBedrockRuntimeCommandName(command);
24180
+ return commandName ? BEDROCK_RUNTIME_COMMAND_OPERATIONS[commandName] : void 0;
24181
+ }
24182
+ function getBedrockRuntimeCommandInput(command) {
24183
+ return isObject(command) ? command.input : void 0;
24184
+ }
24185
+ function buildBedrockRuntimeSpanInfo(command) {
24186
+ const commandName = getBedrockRuntimeCommandName(command);
24187
+ const operation = getBedrockRuntimeOperation(command);
24188
+ return {
24189
+ name: operation ? `bedrock.${operation}` : "bedrock.client.send",
24190
+ metadata: {
24191
+ ...commandName ? { command: commandName } : {},
24192
+ ...operation ? { operation } : {}
24193
+ }
24194
+ };
24195
+ }
24196
+ function isBedrockRuntimeCommandName(commandName) {
24197
+ return commandName === "ConverseCommand" || commandName === "ConverseStreamCommand" || commandName === "InvokeModelCommand" || commandName === "InvokeModelWithBidirectionalStreamCommand" || commandName === "InvokeModelWithResponseStreamCommand";
24198
+ }
24199
+
24200
+ // src/instrumentation/plugins/bedrock-runtime-plugin.ts
24201
+ var BedrockRuntimePlugin = class extends BasePlugin {
24202
+ onEnable() {
24203
+ this.unsubscribers.push(
24204
+ ...[
24205
+ bedrockRuntimeChannels.clientSend,
24206
+ smithyCoreChannels.clientSend,
24207
+ smithyClientChannels.clientSend
24208
+ ].map((channel2) => traceBedrockRuntimeClientSendChannel(channel2))
24209
+ );
24210
+ }
24211
+ onDisable() {
24212
+ this.unsubscribers = unsubscribeAll(this.unsubscribers);
24213
+ }
24214
+ };
24215
+ function traceBedrockRuntimeClientSendChannel(channel2) {
24216
+ return traceStreamingChannel(channel2, {
24217
+ name: ([command]) => buildBedrockRuntimeSpanInfo(command).name,
24218
+ shouldTrace: ([command, optionsOrCb, cb]) => getBedrockRuntimeOperation(command) !== void 0 && typeof optionsOrCb !== "function" && typeof cb !== "function",
24219
+ type: "llm" /* LLM */,
24220
+ extractInput: ([command]) => extractBedrockRuntimeInput(command),
24221
+ extractOutput: (result, endEvent) => extractBedrockRuntimeOutput(endEvent?.arguments?.[0], result),
24222
+ extractMetadata: (result, endEvent) => extractBedrockRuntimeResponseMetadata(endEvent?.arguments?.[0], result),
24223
+ extractMetrics: (result) => extractBedrockRuntimeResponseMetrics(result),
24224
+ patchResult: ({ endEvent, result, span, startTime }) => patchBedrockRuntimeStreamingResult({
24225
+ command: endEvent.arguments?.[0],
24226
+ result,
24227
+ span,
24228
+ startTime
24229
+ })
24230
+ });
24231
+ }
24232
+ function extractBedrockRuntimeInput(command) {
24233
+ const operation = getBedrockRuntimeOperation(command);
24234
+ const commandName = getBedrockRuntimeCommandName(command);
24235
+ const request = getBedrockRuntimeCommandInput(command);
24236
+ const metadata = {
24237
+ provider: "aws-bedrock",
24238
+ ...commandName ? { command: commandName } : {},
24239
+ ...operation ? { operation } : {},
24240
+ ...extractBedrockRuntimeRequestMetadata(request)
24241
+ };
24242
+ if (operation === "converse" || operation === "converseStream") {
24243
+ const converseRequest = isObject(request) ? request : void 0;
24244
+ return {
24245
+ input: sanitizeBedrockValue({
24246
+ messages: converseRequest?.messages,
24247
+ system: converseRequest?.system
24248
+ }),
24249
+ metadata
24250
+ };
24251
+ }
24252
+ if (operation === "invokeModel" || operation === "invokeModelWithBidirectionalStream" || operation === "invokeModelWithResponseStream") {
24253
+ const invokeRequest = isObject(request) ? request : void 0;
24254
+ return {
24255
+ input: parseJsonBody(invokeRequest?.body) ?? summarizeBody(invokeRequest?.body),
24256
+ metadata
24257
+ };
24258
+ }
24259
+ return {
24260
+ input: sanitizeBedrockValue(request),
24261
+ metadata
24262
+ };
24263
+ }
24264
+ function extractBedrockRuntimeRequestMetadata(request) {
24265
+ if (!isObject(request)) {
24266
+ return {};
24267
+ }
24268
+ const metadata = {};
24269
+ for (const key of [
24270
+ "modelId",
24271
+ "contentType",
24272
+ "accept",
24273
+ "trace",
24274
+ "guardrailIdentifier",
24275
+ "guardrailVersion",
24276
+ "performanceConfig",
24277
+ "performanceConfigLatency",
24278
+ "serviceTier"
24279
+ ]) {
24280
+ const value = request[key];
24281
+ if (value !== void 0) {
24282
+ metadata[key === "modelId" ? "model" : key] = value;
24283
+ }
24284
+ }
24285
+ if (isObject(request.inferenceConfig)) {
24286
+ Object.assign(metadata, sanitizeBedrockValue(request.inferenceConfig));
24287
+ }
24288
+ return metadata;
24289
+ }
24290
+ function extractBedrockRuntimeOutput(command, result) {
24291
+ const operation = getBedrockRuntimeOperation(command);
24292
+ if (operation === "converse") {
24293
+ return sanitizeBedrockValue(
24294
+ result?.output?.message
24295
+ );
24296
+ }
24297
+ if (operation === "invokeModel") {
24298
+ const response = isObject(result) ? result : void 0;
24299
+ return parseJsonBody(response?.body) ?? summarizeBody(response?.body);
24300
+ }
24301
+ return sanitizeBedrockValue(result);
24302
+ }
24303
+ function extractBedrockRuntimeResponseMetadata(command, result) {
24304
+ const operation = getBedrockRuntimeOperation(command);
24305
+ if (!isObject(result)) {
24306
+ return void 0;
24307
+ }
24308
+ const metadata = {};
24309
+ for (const key of [
24310
+ "stopReason",
24311
+ "contentType",
24312
+ "performanceConfig",
24313
+ "performanceConfigLatency",
24314
+ "serviceTier"
24315
+ ]) {
24316
+ const value = result[key];
24317
+ if (value !== void 0) {
24318
+ metadata[key] = value;
24319
+ }
24320
+ }
24321
+ if (operation === "converse" && result.additionalModelResponseFields !== void 0) {
24322
+ metadata.additionalModelResponseFields = sanitizeBedrockValue(
24323
+ result.additionalModelResponseFields
24324
+ );
24325
+ }
24326
+ return Object.keys(metadata).length > 0 ? metadata : void 0;
24327
+ }
24328
+ function extractBedrockRuntimeResponseMetrics(result) {
24329
+ if (!isObject(result)) {
24330
+ return {};
24331
+ }
24332
+ const parsedBody = parseJsonBody(result.body);
24333
+ if (isObject(parsedBody)) {
24334
+ const metadata = isObject(parsedBody.metadata) ? parsedBody.metadata : void 0;
24335
+ const metrics = parseBedrockRuntimeMetrics(
24336
+ parsedBody.usage ?? metadata?.usage,
24337
+ parsedBody.metrics ?? metadata?.metrics
24338
+ );
24339
+ if (Object.keys(metrics).length > 0) {
24340
+ return metrics;
24341
+ }
24342
+ }
24343
+ return parseBedrockRuntimeMetrics(result.usage, result.metrics);
24344
+ }
24345
+ function parseBedrockRuntimeMetrics(usage, responseMetrics) {
24346
+ const metrics = {};
24347
+ const usageRecord = isObject(usage) ? usage : {};
24348
+ const promptTokens = firstNumber3(
24349
+ usageRecord.inputTokens,
24350
+ usageRecord.inputTokenCount,
24351
+ usageRecord.input_tokens,
24352
+ usageRecord.prompt_tokens
24353
+ );
24354
+ if (promptTokens !== void 0) {
24355
+ metrics.prompt_tokens = promptTokens;
24356
+ }
24357
+ const completionTokens = firstNumber3(
24358
+ usageRecord.outputTokens,
24359
+ usageRecord.outputTokenCount,
24360
+ usageRecord.output_tokens,
24361
+ usageRecord.completion_tokens
24362
+ );
24363
+ if (completionTokens !== void 0) {
24364
+ metrics.completion_tokens = completionTokens;
24365
+ }
24366
+ const totalTokens = firstNumber3(
24367
+ usageRecord.totalTokens,
24368
+ usageRecord.totalTokenCount,
24369
+ usageRecord.total_tokens,
24370
+ usageRecord.tokens
24371
+ );
24372
+ if (totalTokens !== void 0) {
24373
+ metrics.tokens = totalTokens;
24374
+ }
24375
+ const cacheReadInputTokens = firstNumber3(
24376
+ usageRecord.cacheReadInputTokens,
24377
+ usageRecord.cacheReadInputTokenCount,
24378
+ usageRecord.cache_read_input_tokens,
24379
+ usageRecord.prompt_cached_tokens
24380
+ );
24381
+ if (cacheReadInputTokens !== void 0) {
24382
+ metrics.prompt_cached_tokens = cacheReadInputTokens;
24383
+ }
24384
+ const cacheWriteInputTokens = firstNumber3(
24385
+ usageRecord.cacheWriteInputTokens,
24386
+ usageRecord.cacheWriteInputTokenCount,
24387
+ usageRecord.cache_write_input_tokens,
24388
+ usageRecord.cache_creation_input_tokens,
24389
+ usageRecord.prompt_cache_creation_tokens
24390
+ );
24391
+ if (cacheWriteInputTokens !== void 0) {
24392
+ metrics.prompt_cache_creation_tokens = cacheWriteInputTokens;
24393
+ }
24394
+ if (metrics.tokens === void 0) {
24395
+ const tokenParts = [
24396
+ promptTokens,
24397
+ completionTokens,
24398
+ cacheReadInputTokens,
24399
+ cacheWriteInputTokens
24400
+ ].filter((value) => value !== void 0);
24401
+ if (tokenParts.length > 0) {
24402
+ metrics.tokens = tokenParts.reduce((total, value) => total + value, 0);
24403
+ }
24404
+ }
24405
+ if (isObject(responseMetrics) && typeof responseMetrics.latencyMs === "number") {
24406
+ metrics.latency_ms = responseMetrics.latencyMs;
24407
+ }
24408
+ return metrics;
24409
+ }
24410
+ function firstNumber3(...values) {
24411
+ return values.find((value) => typeof value === "number");
24412
+ }
24413
+ function patchBedrockRuntimeStreamingResult(args) {
24414
+ const operation = getBedrockRuntimeOperation(args.command);
24415
+ if (!isObject(args.result)) {
24416
+ return false;
24417
+ }
24418
+ if (operation === "converseStream" && isAsyncIterable(args.result.stream)) {
24419
+ patchConverseStream(
24420
+ args.result.stream,
24421
+ args.span,
24422
+ args.startTime
24423
+ );
24424
+ return true;
24425
+ }
24426
+ if ((operation === "invokeModelWithBidirectionalStream" || operation === "invokeModelWithResponseStream") && isAsyncIterable(args.result.body)) {
24427
+ patchInvokeModelResponseStream(
24428
+ args.result.body,
24429
+ args.span,
24430
+ args.startTime
24431
+ );
24432
+ return true;
24433
+ }
24434
+ return false;
24435
+ }
24436
+ function patchConverseStream(stream, span, startTime) {
24437
+ let firstChunkTime;
24438
+ patchStreamIfNeeded(stream, {
24439
+ onChunk: (chunk) => {
24440
+ if (firstChunkTime === void 0 && isObject(chunk.contentBlockDelta)) {
24441
+ firstChunkTime = getCurrentUnixTimestamp();
24442
+ }
24443
+ },
24444
+ onComplete: (chunks) => {
24445
+ const aggregated = aggregateBedrockConverseStreamChunks(chunks);
24446
+ const metrics = { ...aggregated.metrics };
24447
+ if (firstChunkTime !== void 0) {
24448
+ metrics.time_to_first_token = firstChunkTime - startTime;
24449
+ }
24450
+ logBedrockStreamAggregation(span, aggregated, metrics);
24451
+ span.end();
24452
+ },
24453
+ onError: (error) => {
24454
+ span.log({ error: error.message });
24455
+ span.end();
24456
+ }
24457
+ });
24458
+ }
24459
+ function patchInvokeModelResponseStream(stream, span, startTime) {
24460
+ let firstChunkTime;
24461
+ patchStreamIfNeeded(stream, {
24462
+ onChunk: (chunk) => {
24463
+ if (firstChunkTime === void 0 && isObject(chunk.chunk)) {
24464
+ firstChunkTime = getCurrentUnixTimestamp();
24465
+ }
24466
+ },
24467
+ onComplete: (chunks) => {
24468
+ const aggregated = aggregateInvokeModelResponseStreamChunks(chunks);
24469
+ const metrics = { ...aggregated.metrics };
24470
+ if (firstChunkTime !== void 0) {
24471
+ metrics.time_to_first_token = firstChunkTime - startTime;
24472
+ }
24473
+ logBedrockStreamAggregation(span, aggregated, metrics);
24474
+ span.end();
24475
+ },
24476
+ onError: (error) => {
24477
+ span.log({ error: error.message });
24478
+ span.end();
24479
+ }
24480
+ });
24481
+ }
24482
+ var BEDROCK_STREAM_EXCEPTION_KEYS = [
24483
+ "internalServerException",
24484
+ "modelStreamErrorException",
24485
+ "validationException",
24486
+ "throttlingException",
24487
+ "modelTimeoutException",
24488
+ "serviceUnavailableException"
24489
+ ];
24490
+ function aggregateBedrockConverseStreamChunks(chunks) {
24491
+ let role;
24492
+ let stopReason;
24493
+ let usage;
24494
+ let responseMetrics;
24495
+ const contentByIndex = /* @__PURE__ */ new Map();
24496
+ const metadata = {};
24497
+ for (const chunk of chunks) {
24498
+ const exception = extractBedrockStreamException(chunk);
24499
+ if (exception) {
24500
+ return exception;
24501
+ }
24502
+ if (typeof chunk.messageStart?.role === "string") {
24503
+ role = chunk.messageStart.role;
24504
+ }
24505
+ const startIndex = chunk.contentBlockStart?.contentBlockIndex;
24506
+ if (typeof startIndex === "number") {
24507
+ contentByIndex.set(startIndex, {
24508
+ ...contentByIndex.get(startIndex) ?? {},
24509
+ ...sanitizeRecord(chunk.contentBlockStart?.start)
24510
+ });
24511
+ }
24512
+ const deltaIndex = chunk.contentBlockDelta?.contentBlockIndex;
24513
+ const delta = chunk.contentBlockDelta?.delta;
24514
+ if (typeof deltaIndex === "number" && isObject(delta)) {
24515
+ const existing = contentByIndex.get(deltaIndex) ?? {};
24516
+ contentByIndex.set(deltaIndex, mergeContentBlockDelta(existing, delta));
24517
+ }
24518
+ if (typeof chunk.messageStop?.stopReason === "string") {
24519
+ stopReason = chunk.messageStop.stopReason;
24520
+ }
24521
+ if (chunk.messageStop?.additionalModelResponseFields !== void 0) {
24522
+ metadata.additionalModelResponseFields = sanitizeBedrockValue(
24523
+ chunk.messageStop.additionalModelResponseFields
24524
+ );
24525
+ }
24526
+ if (chunk.metadata?.usage !== void 0) {
24527
+ usage = chunk.metadata.usage;
24528
+ }
24529
+ if (chunk.metadata?.metrics !== void 0) {
24530
+ responseMetrics = chunk.metadata.metrics;
24531
+ }
24532
+ if (chunk.metadata?.performanceConfig !== void 0) {
24533
+ metadata.performanceConfig = sanitizeBedrockValue(
24534
+ chunk.metadata.performanceConfig
24535
+ );
24536
+ }
24537
+ if (chunk.metadata?.serviceTier !== void 0) {
24538
+ metadata.serviceTier = chunk.metadata.serviceTier;
24539
+ }
24540
+ }
24541
+ if (stopReason !== void 0) {
24542
+ metadata.stopReason = stopReason;
24543
+ }
24544
+ const content = [...contentByIndex.entries()].sort(([left], [right]) => left - right).map(([, value]) => value).filter((value) => Object.keys(value).length > 0);
24545
+ return {
24546
+ output: {
24547
+ role,
24548
+ content
24549
+ },
24550
+ metrics: parseBedrockRuntimeMetrics(usage, responseMetrics),
24551
+ ...Object.keys(metadata).length > 0 ? { metadata } : {}
24552
+ };
24553
+ }
24554
+ function aggregateInvokeModelResponseStreamChunks(chunks) {
24555
+ for (const chunk of chunks) {
24556
+ const exception = extractBedrockStreamException(chunk);
24557
+ if (exception) {
24558
+ return exception;
24559
+ }
24560
+ }
24561
+ const parsedChunks = chunks.map((chunk) => parseJsonBody(chunk.chunk?.bytes)).filter((chunk) => chunk !== void 0);
24562
+ const jsonLikeChunks = parsedChunks.filter(isObject);
24563
+ const text = parsedChunks.map(extractTextFromJsonLike).join("");
24564
+ const lastMetadataChunk = jsonLikeChunks.slice().reverse().find((chunk) => isObject(chunk.metadata));
24565
+ const metadata = isObject(lastMetadataChunk?.metadata) ? sanitizeRecord(lastMetadataChunk.metadata) : void 0;
24566
+ let usage;
24567
+ for (const chunk of jsonLikeChunks) {
24568
+ const message = chunk.message;
24569
+ for (const candidate of [
24570
+ isObject(chunk.usage) ? chunk.usage : void 0,
24571
+ isObject(message) && isObject(message.usage) ? message.usage : void 0
24572
+ ]) {
24573
+ if (candidate !== void 0) {
24574
+ usage = {
24575
+ ...usage,
24576
+ ...sanitizeRecord(candidate)
24577
+ };
24578
+ }
24579
+ }
24580
+ }
24581
+ return {
24582
+ output: text.length > 0 ? { text } : {
24583
+ chunk_count: chunks.length,
24584
+ chunks: sanitizeBedrockValue(
24585
+ (jsonLikeChunks.length > 0 ? jsonLikeChunks : chunks).slice(
24586
+ 0,
24587
+ 20
24588
+ )
24589
+ )
24590
+ },
24591
+ metrics: parseBedrockRuntimeMetrics(
24592
+ usage ?? (isObject(metadata) ? metadata.usage : void 0),
24593
+ isObject(metadata) ? metadata.metrics : void 0
24594
+ ),
24595
+ ...metadata ? { metadata } : {}
24596
+ };
24597
+ }
24598
+ function logBedrockStreamAggregation(span, aggregated, metrics) {
24599
+ span.log(
24600
+ aggregated.error !== void 0 ? {
24601
+ error: aggregated.error,
24602
+ ...aggregated.metadata ? { metadata: aggregated.metadata } : {},
24603
+ metrics
24604
+ } : {
24605
+ output: aggregated.output,
24606
+ ...aggregated.metadata ? { metadata: aggregated.metadata } : {},
24607
+ metrics
24608
+ }
24609
+ );
24610
+ }
24611
+ function extractBedrockStreamException(chunk) {
24612
+ if (!isObject(chunk)) {
24613
+ return void 0;
24614
+ }
24615
+ for (const [key, value] of Object.entries(chunk)) {
24616
+ if (!isBedrockStreamExceptionKey(key) || !isObject(value)) {
24617
+ continue;
24618
+ }
24619
+ const name = typeof value.name === "string" ? value.name : key;
24620
+ const message = typeof value.message === "string" ? value.message : typeof value.originalMessage === "string" ? value.originalMessage : void 0;
24621
+ const metadata = {
24622
+ exception: key
24623
+ };
24624
+ if (typeof value.name === "string") {
24625
+ metadata.exceptionName = value.name;
24626
+ }
24627
+ if (typeof value.$fault === "string") {
24628
+ metadata.fault = value.$fault;
24629
+ }
24630
+ if (typeof value.originalMessage === "string") {
24631
+ metadata.originalMessage = value.originalMessage;
24632
+ }
24633
+ if (typeof value.originalStatusCode === "number") {
24634
+ metadata.originalStatusCode = value.originalStatusCode;
24635
+ }
24636
+ return {
24637
+ error: message ? `${name}: ${message}` : name,
24638
+ metadata,
24639
+ metrics: {}
24640
+ };
24641
+ }
24642
+ return void 0;
24643
+ }
24644
+ function isBedrockStreamExceptionKey(key) {
24645
+ return BEDROCK_STREAM_EXCEPTION_KEYS.some((candidate) => candidate === key);
24646
+ }
24647
+ function mergeContentBlockDelta(existing, delta) {
24648
+ const next = { ...existing };
24649
+ if (typeof delta.text === "string") {
24650
+ next.text = `${typeof next.text === "string" ? next.text : ""}${delta.text}`;
24651
+ }
24652
+ if (isObject(delta.reasoningContent)) {
24653
+ const existingReasoning = isObject(next.reasoningContent) ? next.reasoningContent : {};
24654
+ next.reasoningContent = {
24655
+ ...existingReasoning,
24656
+ ...sanitizeRecord(delta.reasoningContent),
24657
+ ...typeof delta.reasoningContent.text === "string" ? {
24658
+ text: `${typeof existingReasoning.text === "string" ? existingReasoning.text : ""}${delta.reasoningContent.text}`
24659
+ } : {}
24660
+ };
24661
+ }
24662
+ if (isObject(delta.toolUse)) {
24663
+ const existingToolUse = isObject(next.toolUse) ? next.toolUse : {};
24664
+ next.toolUse = {
24665
+ ...existingToolUse,
24666
+ ...sanitizeRecord(delta.toolUse),
24667
+ ...typeof delta.toolUse.input === "string" ? {
24668
+ input: `${typeof existingToolUse.input === "string" ? existingToolUse.input : ""}${delta.toolUse.input}`
24669
+ } : {}
24670
+ };
24671
+ }
24672
+ for (const [key, value] of Object.entries(delta)) {
24673
+ if (key !== "text" && key !== "reasoningContent" && key !== "toolUse" && isSafeBedrockObjectKey(key)) {
24674
+ next[key] = sanitizeBedrockValue(value);
24675
+ }
24676
+ }
24677
+ return next;
24678
+ }
24679
+ function parseJsonBody(body) {
24680
+ const text = decodeBodyToString(body);
24681
+ if (text === void 0 || text.length === 0) {
24682
+ return void 0;
24683
+ }
24684
+ try {
24685
+ return sanitizeBedrockValue(JSON.parse(text));
24686
+ } catch {
24687
+ return void 0;
24688
+ }
24689
+ }
24690
+ function summarizeBody(body) {
24691
+ if (body === void 0 || body === null) {
24692
+ return void 0;
24693
+ }
24694
+ const text = decodeBodyToString(body);
24695
+ if (text !== void 0) {
24696
+ return text.length > 2e3 ? `${text.slice(0, 2e3)}...` : text;
24697
+ }
24698
+ return sanitizeBedrockValue(body);
24699
+ }
24700
+ function decodeBodyToString(body) {
24701
+ if (typeof body === "string") {
24702
+ return body;
24703
+ }
24704
+ if (body instanceof Uint8Array) {
24705
+ return new TextDecoder().decode(body);
24706
+ }
24707
+ if (body instanceof ArrayBuffer) {
24708
+ return new TextDecoder().decode(new Uint8Array(body));
24709
+ }
24710
+ return void 0;
24711
+ }
24712
+ function sanitizeRecord(value) {
24713
+ return isObject(value) ? sanitizeBedrockValue(value) : {};
24714
+ }
24715
+ function sanitizeBedrockValue(value, depth = 0) {
24716
+ if (value === void 0 || value === null) {
24717
+ return value;
24718
+ }
24719
+ if (typeof value !== "object") {
24720
+ return value;
24721
+ }
24722
+ if (value instanceof Uint8Array) {
24723
+ return { byte_length: value.byteLength };
24724
+ }
24725
+ if (value instanceof ArrayBuffer) {
24726
+ return { byte_length: value.byteLength };
24727
+ }
24728
+ if (Array.isArray(value)) {
24729
+ return depth > 20 ? "[MaxDepth]" : value.map((item) => sanitizeBedrockValue(item, depth + 1));
24730
+ }
24731
+ if (!isObject(value)) {
24732
+ return String(value);
24733
+ }
24734
+ if (depth > 20) {
24735
+ return "[MaxDepth]";
24736
+ }
24737
+ const output = {};
24738
+ for (const [key, nested] of Object.entries(value)) {
24739
+ if (!isSafeBedrockObjectKey(key)) {
24740
+ continue;
24741
+ }
24742
+ output[key] = sanitizeBedrockValue(nested, depth + 1);
24743
+ }
24744
+ return output;
24745
+ }
24746
+ function isSafeBedrockObjectKey(key) {
24747
+ return key !== "__proto__" && key !== "constructor" && key !== "prototype";
24748
+ }
24749
+ function extractTextFromJsonLike(value) {
24750
+ if (typeof value === "string") {
24751
+ return value;
24752
+ }
24753
+ if (Array.isArray(value)) {
24754
+ return value.map(extractTextFromJsonLike).join("");
24755
+ }
24756
+ if (!isObject(value)) {
24757
+ return "";
24758
+ }
24759
+ const contentBlockDelta = value.contentBlockDelta;
24760
+ if (isObject(contentBlockDelta)) {
24761
+ return extractTextFromJsonLike(contentBlockDelta.delta);
24762
+ }
24763
+ if (isObject(value.delta)) {
24764
+ return extractTextFromJsonLike(value.delta);
24765
+ }
24766
+ if (typeof value.text === "string") {
24767
+ return value.text;
24768
+ }
24769
+ if (typeof value.outputText === "string") {
24770
+ return value.outputText;
24771
+ }
24772
+ if (typeof value.completion === "string") {
24773
+ return value.completion;
24774
+ }
24775
+ if (typeof value.generation === "string") {
24776
+ return value.generation;
24777
+ }
24778
+ if (Array.isArray(value.content)) {
24779
+ return value.content.map(extractTextFromJsonLike).join("");
24780
+ }
24781
+ if (Array.isArray(value.output)) {
24782
+ return value.output.map(extractTextFromJsonLike).join("");
24783
+ }
24784
+ if (Array.isArray(value.outputs)) {
24785
+ return value.outputs.map(extractTextFromJsonLike).join("");
24786
+ }
24787
+ return "";
24788
+ }
24789
+
24125
24790
  // src/instrumentation/plugins/genkit-channels.ts
24126
24791
  var genkitChannels = defineChannels("@genkit-ai/ai", {
24127
24792
  generate: channel({
@@ -24546,7 +25211,7 @@ function createDeferredSpanFinalizer(span) {
24546
25211
  try {
24547
25212
  await callback();
24548
25213
  } catch (error) {
24549
- span.log({ error: errorMessage(error) });
25214
+ span.log({ error: toLoggedError(error) });
24550
25215
  } finally {
24551
25216
  span.end();
24552
25217
  }
@@ -24700,9 +25365,6 @@ function pickNumberMetrics(values) {
24700
25365
  })
24701
25366
  );
24702
25367
  }
24703
- function errorMessage(error) {
24704
- return error instanceof Error ? error.message : String(error);
24705
- }
24706
25368
  function stringValue(value) {
24707
25369
  return typeof value === "string" ? value : void 0;
24708
25370
  }
@@ -25247,10 +25909,25 @@ var flueChannels = defineChannels("@flue/runtime", {
25247
25909
 
25248
25910
  // src/instrumentation/plugins/flue-plugin.ts
25249
25911
  var FLUE_AUTO_STATE = /* @__PURE__ */ Symbol.for("braintrust.flue.auto-state");
25912
+ var FLUE_INSTRUMENTATION_KEY = /* @__PURE__ */ Symbol.for("braintrust.flue.instrumentation");
25250
25913
  var FLUE_OBSERVE_BRIDGE = /* @__PURE__ */ Symbol.for("braintrust.flue.observe-bridge");
25251
- var braintrustFlueObserver = (event, ctx) => {
25914
+ function observeFlue(event, ctx) {
25252
25915
  getObserveBridge().handle(event, ctx);
25253
- };
25916
+ }
25917
+ var interceptFlueExecution = (operation, ctx, next) => getObserveBridge().intercept(operation, ctx, next);
25918
+ function braintrustFlueInstrumentation() {
25919
+ return {
25920
+ dispose() {
25921
+ },
25922
+ interceptor: interceptFlueExecution,
25923
+ key: FLUE_INSTRUMENTATION_KEY,
25924
+ observe: observeFlue
25925
+ };
25926
+ }
25927
+ var braintrustFlueObserver = Object.assign(
25928
+ observeFlue,
25929
+ braintrustFlueInstrumentation()
25930
+ );
25254
25931
  var FluePlugin = class extends BasePlugin {
25255
25932
  onEnable() {
25256
25933
  this.unsubscribers.push(enableFlueAutoInstrumentation());
@@ -25265,16 +25942,16 @@ var FluePlugin = class extends BasePlugin {
25265
25942
  function enableFlueAutoInstrumentation() {
25266
25943
  const state = getAutoState();
25267
25944
  state.refCount += 1;
25268
- if (!state.handlers) {
25269
- const channel2 = flueChannels.createContext.tracingChannel();
25945
+ if (!state.createContextHandlers) {
25946
+ const createContextChannel = flueChannels.createContext.tracingChannel();
25270
25947
  const handlers = {
25271
25948
  end: (event) => {
25272
25949
  subscribeToFlueContext(event.result, state);
25273
25950
  }
25274
25951
  };
25275
- channel2.subscribe(handlers);
25276
- state.channel = channel2;
25277
- state.handlers = handlers;
25952
+ createContextChannel.subscribe(handlers);
25953
+ state.createContextChannel = createContextChannel;
25954
+ state.createContextHandlers = handlers;
25278
25955
  }
25279
25956
  let released = false;
25280
25957
  return () => {
@@ -25318,8 +25995,8 @@ function releaseAutoState(state) {
25318
25995
  return;
25319
25996
  }
25320
25997
  try {
25321
- if (state.channel && state.handlers) {
25322
- state.channel.unsubscribe(state.handlers);
25998
+ if (state.createContextChannel && state.createContextHandlers) {
25999
+ state.createContextChannel.unsubscribe(state.createContextHandlers);
25323
26000
  }
25324
26001
  } finally {
25325
26002
  Reflect.deleteProperty(globalThis, FLUE_AUTO_STATE);
@@ -25377,7 +26054,7 @@ function isObservableFlueContext(value) {
25377
26054
  }
25378
26055
  function isFlueEvent(event) {
25379
26056
  const type = Reflect.get(event, "type");
25380
- 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";
26057
+ 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";
25381
26058
  }
25382
26059
  function flueContextFromUnknown(ctx) {
25383
26060
  if (!isObjectLike(ctx)) {
@@ -25424,11 +26101,97 @@ var FlueObserveBridge = class {
25424
26101
  this.toolsByKey.clear();
25425
26102
  this.turnsByKey.clear();
25426
26103
  }
26104
+ intercept(operation, executionContext, next) {
26105
+ let span;
26106
+ try {
26107
+ span = this.spanForExecutionOperation(operation, executionContext);
26108
+ } catch (error) {
26109
+ logInstrumentationError3("Flue execution interceptor", error);
26110
+ }
26111
+ return span ? runWithCurrentSpanStore(span, next) : next();
26112
+ }
26113
+ spanForExecutionOperation(operation, executionContext) {
26114
+ switch (operation.type) {
26115
+ case "workflow":
26116
+ return this.ensureWorkflowSpanForExecution(operation, executionContext);
26117
+ case "agent":
26118
+ return this.operationsById.get(operation.operationId)?.span;
26119
+ case "model":
26120
+ return this.turnsByKey.get(operation.turnId)?.span;
26121
+ case "tool":
26122
+ return this.spanForToolExecution(operation, executionContext);
26123
+ case "task":
26124
+ return this.tasksById.get(operation.taskId)?.span;
26125
+ default:
26126
+ return void 0;
26127
+ }
26128
+ }
26129
+ ensureWorkflowSpanForExecution(operation, executionContext) {
26130
+ const existing = this.runsById.get(operation.runId);
26131
+ if (existing) {
26132
+ return existing.span;
26133
+ }
26134
+ const ctx = flueContextFromUnknown(executionContext.eventContext) ?? flueContextFromUnknown({
26135
+ id: operation.workflowName,
26136
+ runId: operation.runId
26137
+ });
26138
+ if (operation.phase === "resume") {
26139
+ this.handleRunResume(
26140
+ {
26141
+ eventIndex: 0,
26142
+ runId: operation.runId,
26143
+ startedAt: operation.startedAt,
26144
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
26145
+ type: "run_resume",
26146
+ v: 3,
26147
+ workflowName: operation.workflowName
26148
+ },
26149
+ ctx
26150
+ );
26151
+ } else {
26152
+ this.handleRunStart(
26153
+ {
26154
+ eventIndex: 0,
26155
+ input: void 0,
26156
+ runId: operation.runId,
26157
+ startedAt: operation.startedAt,
26158
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
26159
+ type: "run_start",
26160
+ v: 3,
26161
+ workflowName: operation.workflowName
26162
+ },
26163
+ ctx
26164
+ );
26165
+ }
26166
+ return this.runsById.get(operation.runId)?.span;
26167
+ }
26168
+ spanForToolExecution(operation, executionContext) {
26169
+ const contextual = this.toolsByKey.get(
26170
+ toolKey({ ...executionContext, toolCallId: operation.toolCallId })
26171
+ );
26172
+ if (contextual) {
26173
+ return contextual.span;
26174
+ }
26175
+ let fallback2;
26176
+ for (const state of this.toolsByKey.values()) {
26177
+ if (state.metadata["flue.tool_call_id"] !== operation.toolCallId) {
26178
+ continue;
26179
+ }
26180
+ if (state.metadata["flue.tool_name"] === operation.toolName) {
26181
+ return state.span;
26182
+ }
26183
+ fallback2 ??= state.span;
26184
+ }
26185
+ return fallback2;
26186
+ }
25427
26187
  handleEvent(event, ctx) {
25428
26188
  switch (event.type) {
25429
26189
  case "run_start":
25430
26190
  this.handleRunStart(event, ctx);
25431
26191
  return;
26192
+ case "run_resume":
26193
+ this.handleRunResume(event, ctx);
26194
+ return;
25432
26195
  case "run_end":
25433
26196
  this.handleRunEnd(event);
25434
26197
  return;
@@ -25448,6 +26211,7 @@ var FlueObserveBridge = class {
25448
26211
  this.handleToolStart(event);
25449
26212
  return;
25450
26213
  case "tool_call":
26214
+ case "tool":
25451
26215
  this.handleToolCall(event);
25452
26216
  return;
25453
26217
  case "task_start":
@@ -25471,30 +26235,61 @@ var FlueObserveBridge = class {
25471
26235
  return;
25472
26236
  }
25473
26237
  const workflowName = event.workflowName ?? event.owner?.workflowName ?? (typeof ctx?.id === "string" ? ctx.id : "unknown");
26238
+ const input = flueRunInput(event);
25474
26239
  const metadata = {
25475
- ...extractPayloadMetadata(event.payload),
26240
+ ...extractPayloadMetadata(input),
25476
26241
  ...extractEventMetadata(event, ctx),
25477
26242
  ...workflowName ? { "flue.workflow_name": workflowName } : {},
25478
26243
  provider: "flue"
25479
26244
  };
26245
+ const existing = this.runsById.get(event.runId);
26246
+ if (existing) {
26247
+ existing.metadata = metadata;
26248
+ safeLog3(existing.span, { input, metadata });
26249
+ return;
26250
+ }
25480
26251
  const span = startSpan({
25481
26252
  name: `workflow:${workflowName}`,
25482
26253
  spanAttributes: { type: "task" /* TASK */ },
25483
26254
  startTime: eventTime(event.startedAt ?? event.timestamp),
25484
26255
  event: {
25485
- input: event.payload,
26256
+ input,
25486
26257
  metadata
25487
26258
  }
25488
26259
  });
25489
- const activeContext = enterCurrentFlueSpan(span);
25490
- this.runsById.set(event.runId, { activeContext, metadata, span });
26260
+ this.runsById.set(event.runId, { metadata, span });
26261
+ }
26262
+ handleRunResume(event, ctx) {
26263
+ if (!event.runId) {
26264
+ return;
26265
+ }
26266
+ const workflowName = event.workflowName ?? (typeof ctx?.id === "string" ? ctx.id : "unknown");
26267
+ const metadata = {
26268
+ ...extractEventMetadata(event, ctx),
26269
+ ...workflowName ? { "flue.workflow_name": workflowName } : {},
26270
+ "flue.workflow_phase": "resume",
26271
+ provider: "flue"
26272
+ };
26273
+ const existing = this.runsById.get(event.runId);
26274
+ if (existing) {
26275
+ existing.metadata = metadata;
26276
+ safeLog3(existing.span, { metadata });
26277
+ return;
26278
+ }
26279
+ const span = startSpan({
26280
+ name: `workflow:${workflowName}`,
26281
+ spanAttributes: { type: "task" /* TASK */ },
26282
+ startTime: eventTime(event.startedAt ?? event.timestamp),
26283
+ event: { metadata }
26284
+ });
26285
+ this.runsById.set(event.runId, { metadata, span });
25491
26286
  }
25492
26287
  handleRunEnd(event) {
25493
26288
  const state = this.runsById.get(event.runId);
25494
26289
  this.finishPendingSpansForRun(event);
25495
26290
  if (state) {
25496
26291
  safeLog3(state.span, {
25497
- ...event.isError ? { error: errorToString(event.error) } : {},
26292
+ ...event.isError ? { error: toLoggedError(event.error) } : {},
25498
26293
  metadata: {
25499
26294
  ...state.metadata,
25500
26295
  ...extractEventMetadata(event),
@@ -25505,7 +26300,6 @@ var FlueObserveBridge = class {
25505
26300
  });
25506
26301
  safeEnd(state.span, eventTime(event.timestamp));
25507
26302
  this.runsById.delete(event.runId);
25508
- restoreCurrentFlueSpan(state.activeContext);
25509
26303
  }
25510
26304
  void flush().catch((error) => {
25511
26305
  logInstrumentationError3("Flue flush", error);
@@ -25543,7 +26337,7 @@ var FlueObserveBridge = class {
25543
26337
  };
25544
26338
  this.finishPendingChildrenForOperation(event, output);
25545
26339
  safeLog3(state.span, {
25546
- ...event.isError ? { error: errorToString(event.error) } : {},
26340
+ ...event.isError ? { error: toLoggedError(event.error) } : {},
25547
26341
  metadata,
25548
26342
  metrics: durationMetrics2(event.durationMs),
25549
26343
  output
@@ -25556,31 +26350,33 @@ var FlueObserveBridge = class {
25556
26350
  if (!key) {
25557
26351
  return;
25558
26352
  }
26353
+ const input = flueTurnRequestInput(event);
26354
+ const model = flueTurnRequestModel(event);
26355
+ const provider = flueTurnRequestProvider(event);
26356
+ const api = flueTurnRequestApi(event);
26357
+ const reasoning = flueTurnRequestReasoning(event);
25559
26358
  const metadata = {
25560
26359
  ...extractEventMetadata(event),
25561
- ...event.api ? { "flue.api": event.api } : {},
25562
- ...event.model ? { model: event.model, "flue.model": event.model } : {},
25563
- ...event.provider ? { provider: event.provider } : { provider: "flue" },
25564
- ...event.provider ? { "flue.provider": event.provider } : {},
26360
+ ...api ? { "flue.api": api } : {},
26361
+ ...model ? { model, "flue.model": model } : {},
26362
+ ...provider ? { provider } : { provider: "flue" },
26363
+ ...provider ? { "flue.provider": provider } : {},
25565
26364
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {},
25566
- ...event.reasoning ? { reasoning: event.reasoning } : {},
25567
- ...event.input?.systemPrompt ? { "flue.system_prompt": event.input.systemPrompt } : {},
25568
- ...event.input?.tools ? { tools: event.input.tools } : {}
26365
+ ...reasoning ? { reasoning } : {},
26366
+ ...input?.systemPrompt ? { "flue.system_prompt": input.systemPrompt } : {},
26367
+ ...input?.tools ? { tools: input.tools } : {}
25569
26368
  };
25570
26369
  const parent = this.parentSpanForTurn(event);
25571
26370
  const span = startFlueSpan(parent, {
25572
- name: `llm:${event.model ?? event.purpose ?? "unknown"}`,
26371
+ name: `llm:${model ?? event.purpose ?? "unknown"}`,
25573
26372
  spanAttributes: { type: "llm" /* LLM */ },
25574
26373
  startTime: eventTime(event.timestamp),
25575
26374
  event: {
25576
- input: event.input?.messages,
26375
+ input: input?.messages,
25577
26376
  metadata
25578
26377
  }
25579
26378
  });
25580
- this.logOperationInput(
25581
- event.operationId,
25582
- event.input?.messages ?? event.input
25583
- );
26379
+ this.logOperationInput(event.operationId, input?.messages ?? input);
25584
26380
  this.turnsByKey.set(key, { metadata, span });
25585
26381
  }
25586
26382
  handleTurn(event) {
@@ -25589,25 +26385,32 @@ var FlueObserveBridge = class {
25589
26385
  return;
25590
26386
  }
25591
26387
  const state = this.turnsByKey.get(key) ?? this.startSyntheticTurn(event);
26388
+ const model = flueTurnModel(event);
26389
+ const provider = flueTurnProvider(event);
26390
+ const api = flueTurnApi(event);
26391
+ const stopReason = flueTurnStopReason(event);
26392
+ const usage = flueTurnUsage(event);
26393
+ const output = flueTurnOutput(event);
26394
+ const error = flueTurnError(event);
25592
26395
  const metadata = {
25593
26396
  ...state.metadata,
25594
26397
  ...extractEventMetadata(event),
25595
- ...event.api ? { "flue.api": event.api } : {},
25596
- ...event.model ? { model: event.model, "flue.model": event.model } : {},
25597
- ...event.provider ? { provider: event.provider } : {},
25598
- ...event.provider ? { "flue.provider": event.provider } : {},
26398
+ ...api ? { "flue.api": api } : {},
26399
+ ...model ? { model, "flue.model": model } : {},
26400
+ ...provider ? { provider } : {},
26401
+ ...provider ? { "flue.provider": provider } : {},
25599
26402
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {},
25600
- ...event.stopReason ? { "flue.stop_reason": event.stopReason } : {},
26403
+ ...stopReason ? { "flue.stop_reason": stopReason } : {},
25601
26404
  ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
25602
26405
  };
25603
26406
  safeLog3(state.span, {
25604
- ...event.isError ? { error: errorToString(event.error) } : {},
26407
+ ...event.isError ? { error: toLoggedError(error) } : {},
25605
26408
  metadata,
25606
26409
  metrics: {
25607
26410
  ...durationMetrics2(event.durationMs),
25608
- ...metricsFromUsage(event.usage)
26411
+ ...metricsFromUsage(usage)
25609
26412
  },
25610
- output: event.output
26413
+ output
25611
26414
  });
25612
26415
  safeEnd(state.span, eventTime(event.timestamp));
25613
26416
  this.turnsByKey.delete(key);
@@ -25616,6 +26419,7 @@ var FlueObserveBridge = class {
25616
26419
  if (!event.toolCallId) {
25617
26420
  return;
25618
26421
  }
26422
+ const input = flueToolInput(event);
25619
26423
  const metadata = {
25620
26424
  ...extractEventMetadata(event),
25621
26425
  ...event.toolName ? { "flue.tool_name": event.toolName } : {},
@@ -25628,12 +26432,11 @@ var FlueObserveBridge = class {
25628
26432
  spanAttributes: { type: "tool" /* TOOL */ },
25629
26433
  startTime: eventTime(event.timestamp),
25630
26434
  event: {
25631
- input: event.args,
26435
+ input,
25632
26436
  metadata
25633
26437
  }
25634
26438
  });
25635
- const activeContext = enterCurrentFlueSpan(span);
25636
- this.toolsByKey.set(toolKey(event), { activeContext, metadata, span });
26439
+ this.toolsByKey.set(toolKey(event), { metadata, span });
25637
26440
  }
25638
26441
  handleToolCall(event) {
25639
26442
  if (!event.toolCallId) {
@@ -25641,6 +26444,8 @@ var FlueObserveBridge = class {
25641
26444
  }
25642
26445
  const key = toolKey(event);
25643
26446
  const state = this.toolsByKey.get(key) ?? this.startSyntheticTool(event);
26447
+ const output = flueToolOutput(event);
26448
+ const error = flueToolError(event);
25644
26449
  const metadata = {
25645
26450
  ...state.metadata,
25646
26451
  ...extractEventMetadata(event),
@@ -25649,14 +26454,13 @@ var FlueObserveBridge = class {
25649
26454
  ...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
25650
26455
  };
25651
26456
  safeLog3(state.span, {
25652
- ...event.isError ? { error: errorToString(event.result) } : {},
26457
+ ...event.isError ? { error: toLoggedError(error) } : {},
25653
26458
  metadata,
25654
26459
  metrics: durationMetrics2(event.durationMs),
25655
- output: event.result
26460
+ output
25656
26461
  });
25657
26462
  safeEnd(state.span, eventTime(event.timestamp));
25658
26463
  this.toolsByKey.delete(key);
25659
- restoreCurrentFlueSpan(state.activeContext);
25660
26464
  }
25661
26465
  handleTaskStart(event) {
25662
26466
  if (!event.taskId) {
@@ -25687,7 +26491,7 @@ var FlueObserveBridge = class {
25687
26491
  return;
25688
26492
  }
25689
26493
  safeLog3(state.span, {
25690
- ...event.isError ? { error: errorToString(event.result) } : {},
26494
+ ...event.isError ? { error: toLoggedError(event.result) } : {},
25691
26495
  metadata: {
25692
26496
  ...state.metadata,
25693
26497
  ...extractEventMetadata(event),
@@ -25825,16 +26629,19 @@ var FlueObserveBridge = class {
25825
26629
  return { metadata, span };
25826
26630
  }
25827
26631
  startSyntheticTurn(event) {
26632
+ const model = flueTurnModel(event);
26633
+ const provider = flueTurnProvider(event);
26634
+ const api = flueTurnApi(event);
25828
26635
  const metadata = {
25829
26636
  ...extractEventMetadata(event),
25830
- ...event.api ? { "flue.api": event.api } : {},
25831
- ...event.model ? { model: event.model, "flue.model": event.model } : {},
25832
- ...event.provider ? { provider: event.provider } : { provider: "flue" },
25833
- ...event.provider ? { "flue.provider": event.provider } : {},
26637
+ ...api ? { "flue.api": api } : {},
26638
+ ...model ? { model, "flue.model": model } : {},
26639
+ ...provider ? { provider } : { provider: "flue" },
26640
+ ...provider ? { "flue.provider": provider } : {},
25834
26641
  ...event.purpose ? { "flue.turn_purpose": event.purpose } : {}
25835
26642
  };
25836
26643
  const span = startFlueSpan(this.parentSpanForEvent(event), {
25837
- name: `llm:${event.model ?? event.purpose ?? "unknown"}`,
26644
+ name: `llm:${model ?? event.purpose ?? "unknown"}`,
25838
26645
  spanAttributes: { type: "llm" /* LLM */ },
25839
26646
  startTime: eventTime(event.timestamp),
25840
26647
  event: { metadata }
@@ -25988,6 +26795,60 @@ function extractPayloadMetadata(payload) {
25988
26795
  }
25989
26796
  return Object.fromEntries(Object.entries(metadata));
25990
26797
  }
26798
+ function flueRunInput(event) {
26799
+ return event.input !== void 0 ? event.input : event.payload;
26800
+ }
26801
+ function flueTurnRequestInput(event) {
26802
+ return event.request?.input ?? event.input;
26803
+ }
26804
+ function flueTurnRequestModel(event) {
26805
+ return event.request?.model ?? event.model;
26806
+ }
26807
+ function flueTurnRequestProvider(event) {
26808
+ return event.request?.providerName ?? event.provider ?? event.request?.providerId;
26809
+ }
26810
+ function flueTurnRequestApi(event) {
26811
+ return event.request?.api ?? event.api;
26812
+ }
26813
+ function flueTurnRequestReasoning(event) {
26814
+ return event.request?.reasoning ?? event.reasoning;
26815
+ }
26816
+ function flueTurnModel(event) {
26817
+ return event.request?.model ?? event.model;
26818
+ }
26819
+ function flueTurnProvider(event) {
26820
+ return event.request?.providerName ?? event.provider ?? event.request?.providerId;
26821
+ }
26822
+ function flueTurnApi(event) {
26823
+ return event.request?.api ?? event.api;
26824
+ }
26825
+ function flueTurnUsage(event) {
26826
+ return event.response?.usage ?? event.usage;
26827
+ }
26828
+ function flueTurnOutput(event) {
26829
+ return event.response?.output ?? event.output;
26830
+ }
26831
+ function flueTurnStopReason(event) {
26832
+ return event.response?.stopReason ?? event.stopReason;
26833
+ }
26834
+ function flueTurnError(event) {
26835
+ return event.response?.error ?? event.response?.errorInfo?.message ?? event.error;
26836
+ }
26837
+ function flueToolInput(event) {
26838
+ if (event.args !== void 0) {
26839
+ return event.args;
26840
+ }
26841
+ if (event.arguments !== void 0) {
26842
+ return event.arguments;
26843
+ }
26844
+ return event.input;
26845
+ }
26846
+ function flueToolOutput(event) {
26847
+ return event.output !== void 0 ? event.output : event.result;
26848
+ }
26849
+ function flueToolError(event) {
26850
+ return event.error ?? event.errorInfo?.message ?? flueToolOutput(event);
26851
+ }
25991
26852
  function operationOutput(event) {
25992
26853
  if (event.operationKind === "prompt" || event.operationKind === "skill") {
25993
26854
  return llmResultFromOperationResult(event.result);
@@ -26061,33 +26922,14 @@ function stateMatchesRun(state, runId) {
26061
26922
  function startFlueSpan(parent, args) {
26062
26923
  return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
26063
26924
  }
26064
- function enterCurrentFlueSpan(span) {
26065
- const contextManager = _internalGetGlobalState()?.contextManager;
26066
- const store = contextManager ? Reflect.get(contextManager, BRAINTRUST_CURRENT_SPAN_STORE) : void 0;
26067
- if (!contextManager || !isCurrentSpanStore(store)) {
26068
- return void 0;
26069
- }
26070
- const previous = store.getStore();
26071
- try {
26072
- store.enterWith(contextManager.wrapSpanForStore(span));
26073
- return { previous, store };
26074
- } catch (error) {
26075
- logInstrumentationError3("Flue context propagation", error);
26076
- return void 0;
26077
- }
26078
- }
26079
- function isCurrentSpanStore(value) {
26080
- return isObjectLike(value) && typeof Reflect.get(value, "enterWith") === "function" && typeof Reflect.get(value, "getStore") === "function";
26081
- }
26082
- function restoreCurrentFlueSpan(activeContext) {
26083
- if (!activeContext) {
26084
- return;
26085
- }
26086
- try {
26087
- activeContext.store.enterWith(activeContext.previous);
26088
- } catch (error) {
26089
- logInstrumentationError3("Flue context restoration", error);
26925
+ function runWithCurrentSpanStore(span, next) {
26926
+ const state = _internalGetGlobalState();
26927
+ const contextManager = state?.contextManager;
26928
+ const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
26929
+ if (contextManager && typeof currentSpanStore?.run === "function") {
26930
+ return currentSpanStore.run(contextManager.wrapSpanForStore(span), next);
26090
26931
  }
26932
+ return withCurrent(span, () => next());
26091
26933
  }
26092
26934
  function safeLog3(span, event) {
26093
26935
  try {
@@ -26103,19 +26945,6 @@ function safeEnd(span, endTime) {
26103
26945
  logInstrumentationError3("Flue span end", error);
26104
26946
  }
26105
26947
  }
26106
- function errorToString(error) {
26107
- if (error instanceof Error) {
26108
- return error.message;
26109
- }
26110
- if (typeof error === "string") {
26111
- return error;
26112
- }
26113
- try {
26114
- return JSON.stringify(error);
26115
- } catch {
26116
- return String(error);
26117
- }
26118
- }
26119
26948
  function logInstrumentationError3(label, error) {
26120
26949
  console.error(`Error in ${label} instrumentation:`, error);
26121
26950
  }
@@ -26945,11 +27774,11 @@ function patchAssistantMessageStream(stream, promptState, llmState) {
26945
27774
  function recordPiAssistantMessageEvent(promptState, llmState, event) {
26946
27775
  recordFirstTokenMetric(llmState, event);
26947
27776
  const message = "message" in event ? event.message : void 0;
26948
- const errorMessage2 = "error" in event ? event.error : void 0;
27777
+ const errorMessage = "error" in event ? event.error : void 0;
26949
27778
  if (event.type === "done" && isPiAssistantMessage(message)) {
26950
27779
  finishPiLlmSpan(promptState, llmState, message);
26951
- } else if (event.type === "error" && isPiAssistantMessage(errorMessage2)) {
26952
- finishPiLlmSpan(promptState, llmState, errorMessage2);
27780
+ } else if (event.type === "error" && isPiAssistantMessage(errorMessage)) {
27781
+ finishPiLlmSpan(promptState, llmState, errorMessage);
26953
27782
  }
26954
27783
  }
26955
27784
  function recordFirstTokenMetric(state, event) {
@@ -27043,7 +27872,7 @@ function finishPiToolSpan(state, event) {
27043
27872
  };
27044
27873
  try {
27045
27874
  safeLog4(toolState.span, {
27046
- ...event.isError ? { error: stringifyUnknown2(event.result) } : {},
27875
+ ...event.isError ? { error: toLoggedError(event.result) } : {},
27047
27876
  metadata,
27048
27877
  output: event.result
27049
27878
  });
@@ -27075,7 +27904,7 @@ async function finalizePiPromptRun(state, error) {
27075
27904
  };
27076
27905
  try {
27077
27906
  safeLog4(state.span, {
27078
- ...error ? { error: stringifyUnknown2(error) } : {},
27907
+ ...error ? { error: toLoggedError(error) } : {},
27079
27908
  metadata,
27080
27909
  metrics: {
27081
27910
  ...cleanMetrics5(state.metrics),
@@ -27135,7 +27964,7 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
27135
27964
  }
27136
27965
  try {
27137
27966
  safeLog4(llmState.span, {
27138
- ...error || messageError ? { error: stringifyUnknown2(error ?? messageError) } : {},
27967
+ ...error || messageError ? { error: toLoggedError(error ?? messageError) } : {},
27139
27968
  metadata: {
27140
27969
  ...llmState.metadata,
27141
27970
  ...message ? extractAssistantMetadata(message) : {}
@@ -27151,7 +27980,7 @@ function finishOpenToolSpans(state, error) {
27151
27980
  for (const [, toolState] of state.activeToolSpans) {
27152
27981
  try {
27153
27982
  safeLog4(toolState.span, {
27154
- error: error ? stringifyUnknown2(error) : "Pi tool did not complete"
27983
+ error: error ? toLoggedError(error) : "Pi tool did not complete"
27155
27984
  });
27156
27985
  toolState.span.end();
27157
27986
  } finally {
@@ -27423,10 +28252,10 @@ function stringifyArguments(value) {
27423
28252
  try {
27424
28253
  return JSON.stringify(value);
27425
28254
  } catch {
27426
- return stringifyUnknown2(value);
28255
+ return stringifyUnknown(value);
27427
28256
  }
27428
28257
  }
27429
- function stringifyUnknown2(value) {
28258
+ function stringifyUnknown(value) {
27430
28259
  if (value instanceof Error) {
27431
28260
  return value.message;
27432
28261
  }
@@ -27450,10 +28279,790 @@ function logInstrumentationError4(context, error) {
27450
28279
  debugLogger.debug(`${context}:`, error);
27451
28280
  }
27452
28281
 
27453
- // src/instrumentation/braintrust-plugin.ts
27454
- function getIntegrationConfig(integrations, key) {
27455
- return integrations[key];
28282
+ // src/instrumentation/plugins/strands-agent-sdk-channels.ts
28283
+ var strandsAgentSDKChannels = defineChannels("@strands-agents/sdk", {
28284
+ agentStream: channel({
28285
+ channelName: "Agent.stream",
28286
+ kind: "sync-stream"
28287
+ }),
28288
+ graphStream: channel({
28289
+ channelName: "Graph.stream",
28290
+ kind: "sync-stream"
28291
+ }),
28292
+ swarmStream: channel({
28293
+ channelName: "Swarm.stream",
28294
+ kind: "sync-stream"
28295
+ })
28296
+ });
28297
+
28298
+ // src/instrumentation/plugins/strands-agent-sdk-plugin.ts
28299
+ var StrandsAgentSDKPlugin = class extends BasePlugin {
28300
+ activeChildParents = /* @__PURE__ */ new WeakMap();
28301
+ onEnable() {
28302
+ this.subscribeToAgentStream();
28303
+ this.subscribeToMultiAgentStream(
28304
+ strandsAgentSDKChannels.graphStream,
28305
+ "Graph.stream"
28306
+ );
28307
+ this.subscribeToMultiAgentStream(
28308
+ strandsAgentSDKChannels.swarmStream,
28309
+ "Swarm.stream"
28310
+ );
28311
+ }
28312
+ onDisable() {
28313
+ for (const unsubscribe of this.unsubscribers) {
28314
+ unsubscribe();
28315
+ }
28316
+ this.unsubscribers = [];
28317
+ }
28318
+ subscribeToAgentStream() {
28319
+ const channel2 = strandsAgentSDKChannels.agentStream.tracingChannel();
28320
+ const states = /* @__PURE__ */ new WeakMap();
28321
+ const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(channel2);
28322
+ const handlers = {
28323
+ start: (event) => {
28324
+ const state = startAgentStream(event, this.activeChildParents);
28325
+ if (state) {
28326
+ states.set(event, state);
28327
+ }
28328
+ },
28329
+ end: (event) => {
28330
+ const state = states.get(event);
28331
+ if (!state) {
28332
+ return;
28333
+ }
28334
+ const result = event.result;
28335
+ if (isAsyncIterable(result)) {
28336
+ patchStreamIfNeeded(result, {
28337
+ aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
28338
+ onChunk: (chunk) => handleAgentStreamEvent(state, chunk),
28339
+ onComplete: () => {
28340
+ finalizeAgentStream(state);
28341
+ states.delete(event);
28342
+ },
28343
+ onError: (error) => {
28344
+ finalizeAgentStream(state, error);
28345
+ states.delete(event);
28346
+ }
28347
+ });
28348
+ return;
28349
+ }
28350
+ finalizeAgentStream(state, void 0, result);
28351
+ states.delete(event);
28352
+ },
28353
+ error: (event) => {
28354
+ const state = states.get(event);
28355
+ if (!state || !event.error) {
28356
+ return;
28357
+ }
28358
+ finalizeAgentStream(state, event.error);
28359
+ states.delete(event);
28360
+ }
28361
+ };
28362
+ channel2.subscribe(handlers);
28363
+ this.unsubscribers.push(() => {
28364
+ unbindAutoInstrumentationSuppression?.();
28365
+ channel2.unsubscribe(handlers);
28366
+ });
28367
+ }
28368
+ subscribeToMultiAgentStream(channel2, operation) {
28369
+ const tracingChannel2 = channel2.tracingChannel();
28370
+ const states = /* @__PURE__ */ new WeakMap();
28371
+ const unbindAutoInstrumentationSuppression = bindAutoInstrumentationSuppressionToStart(tracingChannel2);
28372
+ const handlers = {
28373
+ start: (event) => {
28374
+ const state = startMultiAgentStream(
28375
+ event,
28376
+ operation,
28377
+ this.activeChildParents
28378
+ );
28379
+ if (state) {
28380
+ states.set(event, state);
28381
+ }
28382
+ },
28383
+ end: (event) => {
28384
+ const state = states.get(event);
28385
+ if (!state) {
28386
+ return;
28387
+ }
28388
+ const result = event.result;
28389
+ if (isAsyncIterable(result)) {
28390
+ patchStreamIfNeeded(result, {
28391
+ aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
28392
+ onChunk: (chunk) => handleMultiAgentStreamEvent(
28393
+ state,
28394
+ chunk,
28395
+ this.activeChildParents
28396
+ ),
28397
+ onComplete: () => {
28398
+ finalizeMultiAgentStream(state, this.activeChildParents);
28399
+ states.delete(event);
28400
+ },
28401
+ onError: (error) => {
28402
+ finalizeMultiAgentStream(state, this.activeChildParents, error);
28403
+ states.delete(event);
28404
+ }
28405
+ });
28406
+ return;
28407
+ }
28408
+ finalizeMultiAgentStream(
28409
+ state,
28410
+ this.activeChildParents,
28411
+ void 0,
28412
+ result
28413
+ );
28414
+ states.delete(event);
28415
+ },
28416
+ error: (event) => {
28417
+ const state = states.get(event);
28418
+ if (!state || !event.error) {
28419
+ return;
28420
+ }
28421
+ finalizeMultiAgentStream(state, this.activeChildParents, event.error);
28422
+ states.delete(event);
28423
+ }
28424
+ };
28425
+ tracingChannel2.subscribe(handlers);
28426
+ this.unsubscribers.push(() => {
28427
+ unbindAutoInstrumentationSuppression?.();
28428
+ tracingChannel2.unsubscribe(handlers);
28429
+ });
28430
+ }
28431
+ };
28432
+ function startAgentStream(event, activeChildParents) {
28433
+ const agent = extractAgent(event);
28434
+ const model = agent?.model;
28435
+ const metadata = {
28436
+ ...extractAgentMetadata2(agent),
28437
+ ...extractModelMetadata3(model),
28438
+ "strands.operation": "Agent.stream",
28439
+ provider: extractProvider(model),
28440
+ ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
28441
+ };
28442
+ const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
28443
+ const span = parentSpan ? withCurrent(
28444
+ parentSpan,
28445
+ () => startSpan({
28446
+ event: {
28447
+ input: event.arguments[0],
28448
+ metadata
28449
+ },
28450
+ name: formatAgentSpanName(agent),
28451
+ spanAttributes: { type: "task" /* TASK */ }
28452
+ })
28453
+ ) : startSpan({
28454
+ event: {
28455
+ input: event.arguments[0],
28456
+ metadata
28457
+ },
28458
+ name: formatAgentSpanName(agent),
28459
+ spanAttributes: { type: "task" /* TASK */ }
28460
+ });
28461
+ return {
28462
+ activeTools: /* @__PURE__ */ new Map(),
28463
+ finalized: false,
28464
+ metadata,
28465
+ span,
28466
+ startTime: getCurrentUnixTimestamp()
28467
+ };
27456
28468
  }
28469
+ function startMultiAgentStream(event, operation, activeChildParents) {
28470
+ const orchestrator = extractOrchestrator(event);
28471
+ const metadata = {
28472
+ "strands.operation": operation,
28473
+ provider: "strands",
28474
+ ...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {},
28475
+ ...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
28476
+ };
28477
+ const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
28478
+ const span = parentSpan ? withCurrent(
28479
+ parentSpan,
28480
+ () => startSpan({
28481
+ event: {
28482
+ input: event.arguments[0],
28483
+ metadata
28484
+ },
28485
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28486
+ spanAttributes: { type: "task" /* TASK */ }
28487
+ })
28488
+ ) : startSpan({
28489
+ event: {
28490
+ input: event.arguments[0],
28491
+ metadata
28492
+ },
28493
+ name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
28494
+ spanAttributes: { type: "task" /* TASK */ }
28495
+ });
28496
+ return {
28497
+ activeNodes: /* @__PURE__ */ new Map(),
28498
+ finalized: false,
28499
+ handoffs: [],
28500
+ metadata,
28501
+ operation,
28502
+ orchestrator,
28503
+ span,
28504
+ startTime: getCurrentUnixTimestamp()
28505
+ };
28506
+ }
28507
+ function handleAgentStreamEvent(state, event) {
28508
+ try {
28509
+ switch (event.type) {
28510
+ case "beforeModelCallEvent":
28511
+ startModelSpan(state, event);
28512
+ break;
28513
+ case "modelStreamUpdateEvent":
28514
+ collectModelStreamMetadata(state, event);
28515
+ break;
28516
+ case "afterModelCallEvent":
28517
+ finalizeModelSpan(state, event);
28518
+ break;
28519
+ case "beforeToolCallEvent":
28520
+ startToolSpan2(state, event);
28521
+ break;
28522
+ case "afterToolCallEvent":
28523
+ finalizeToolSpan(state, event);
28524
+ break;
28525
+ case "toolResultEvent":
28526
+ finalizeToolSpanFromResult(state, event.result);
28527
+ break;
28528
+ case "agentResultEvent":
28529
+ safeLog5(state.span, {
28530
+ metadata: {
28531
+ ...state.metadata,
28532
+ ...extractAgentResultMetadata(event.result)
28533
+ },
28534
+ metrics: {
28535
+ ...buildDurationMetrics4(state.startTime),
28536
+ ...parseUsage(event.result?.metrics?.accumulatedUsage)
28537
+ },
28538
+ output: extractAgentResultOutput(event.result)
28539
+ });
28540
+ break;
28541
+ default:
28542
+ break;
28543
+ }
28544
+ } catch (error) {
28545
+ logInstrumentationError5("Strands Agent SDK event", error);
28546
+ }
28547
+ }
28548
+ function handleMultiAgentStreamEvent(state, event, activeChildParents) {
28549
+ try {
28550
+ switch (event.type) {
28551
+ case "beforeNodeCallEvent":
28552
+ startNodeSpan(state, event, activeChildParents);
28553
+ break;
28554
+ case "nodeResultEvent":
28555
+ logNodeResult(state, event);
28556
+ break;
28557
+ case "afterNodeCallEvent":
28558
+ finalizeNodeSpan(state, event, activeChildParents);
28559
+ break;
28560
+ case "multiAgentHandoffEvent":
28561
+ collectHandoff(state, event);
28562
+ break;
28563
+ case "multiAgentResultEvent":
28564
+ safeLog5(state.span, {
28565
+ metadata: {
28566
+ ...state.metadata,
28567
+ ...state.handoffs.length > 0 ? { "strands.handoffs": state.handoffs } : {},
28568
+ ...extractMultiAgentResultMetadata(event.result)
28569
+ },
28570
+ metrics: {
28571
+ ...buildDurationMetrics4(state.startTime),
28572
+ ...parseUsage(event.result?.usage)
28573
+ },
28574
+ output: extractMultiAgentResultOutput(event.result)
28575
+ });
28576
+ break;
28577
+ default:
28578
+ break;
28579
+ }
28580
+ } catch (error) {
28581
+ logInstrumentationError5("Strands Agent SDK multi-agent event", error);
28582
+ }
28583
+ }
28584
+ function startModelSpan(state, event) {
28585
+ if (state.activeModel) {
28586
+ finalizeModelSpan(state);
28587
+ }
28588
+ const model = event.model ?? event.agent?.model;
28589
+ const metadata = {
28590
+ ...extractModelMetadata3(model),
28591
+ "strands.operation": "model.stream",
28592
+ ...typeof event.projectedInputTokens === "number" ? { "strands.projected_input_tokens": event.projectedInputTokens } : {},
28593
+ provider: extractProvider(model)
28594
+ };
28595
+ const span = withCurrent(
28596
+ state.span,
28597
+ () => startSpan({
28598
+ event: {
28599
+ input: Array.isArray(event.agent?.messages) ? event.agent.messages : void 0,
28600
+ metadata
28601
+ },
28602
+ name: formatModelSpanName(model),
28603
+ spanAttributes: { type: "llm" /* LLM */ }
28604
+ })
28605
+ );
28606
+ state.activeModel = {
28607
+ metadata,
28608
+ metrics: typeof event.projectedInputTokens === "number" ? { prompt_tokens: event.projectedInputTokens } : {},
28609
+ span,
28610
+ startTime: getCurrentUnixTimestamp()
28611
+ };
28612
+ }
28613
+ function collectModelStreamMetadata(state, event) {
28614
+ if (!state.activeModel || event.event?.type !== "modelMetadataEvent") {
28615
+ return;
28616
+ }
28617
+ Object.assign(state.activeModel.metrics, parseUsage(event.event.usage));
28618
+ Object.assign(
28619
+ state.activeModel.metrics,
28620
+ parseModelMetrics(event.event.metrics)
28621
+ );
28622
+ }
28623
+ function finalizeModelSpan(state, event) {
28624
+ const modelState = state.activeModel;
28625
+ if (!modelState) {
28626
+ return;
28627
+ }
28628
+ state.activeModel = void 0;
28629
+ const metadata = {
28630
+ ...modelState.metadata,
28631
+ ...event?.attemptCount ? { "strands.model.attempt": event.attemptCount } : {},
28632
+ ...event?.stopData?.stopReason ? { "strands.stop_reason": event.stopData.stopReason } : {}
28633
+ };
28634
+ const metrics = {
28635
+ ...buildDurationMetrics4(modelState.startTime),
28636
+ ...modelState.metrics
28637
+ };
28638
+ safeLog5(modelState.span, {
28639
+ ...event?.error ? { error: toLoggedError(event.error) } : {},
28640
+ metadata,
28641
+ metrics: cleanMetrics6(metrics),
28642
+ output: event?.stopData?.message
28643
+ });
28644
+ modelState.span.end();
28645
+ }
28646
+ function startToolSpan2(state, event) {
28647
+ const toolUse = event.toolUse;
28648
+ const key = toolKey2(toolUse);
28649
+ if (state.activeTools.has(key)) {
28650
+ finalizeToolSpanState(state.activeTools.get(key));
28651
+ state.activeTools.delete(key);
28652
+ }
28653
+ const name = extractToolName3(toolUse, event.tool);
28654
+ const span = withCurrent(
28655
+ state.span,
28656
+ () => startSpan({
28657
+ event: {
28658
+ input: toolUse?.input,
28659
+ metadata: {
28660
+ "gen_ai.tool.call.id": toolUse?.toolUseId,
28661
+ "gen_ai.tool.name": name,
28662
+ "strands.operation": "tool.call",
28663
+ "strands.tool.name": name,
28664
+ provider: "strands"
28665
+ }
28666
+ },
28667
+ name: `tool: ${name}`,
28668
+ spanAttributes: { type: "tool" /* TOOL */ }
28669
+ })
28670
+ );
28671
+ state.activeTools.set(key, {
28672
+ span,
28673
+ startTime: getCurrentUnixTimestamp(),
28674
+ toolUse
28675
+ });
28676
+ }
28677
+ function finalizeToolSpan(state, event) {
28678
+ const key = toolKey2(event.toolUse);
28679
+ const toolState = state.activeTools.get(key);
28680
+ if (!toolState) {
28681
+ return;
28682
+ }
28683
+ state.activeTools.delete(key);
28684
+ finalizeToolSpanState(toolState, {
28685
+ error: event.error,
28686
+ result: event.result,
28687
+ toolUse: event.toolUse
28688
+ });
28689
+ }
28690
+ function finalizeToolSpanFromResult(state, result) {
28691
+ if (!isObject(result)) {
28692
+ return;
28693
+ }
28694
+ const toolUseId = result.toolUseId;
28695
+ if (typeof toolUseId !== "string") {
28696
+ return;
28697
+ }
28698
+ const toolState = state.activeTools.get(toolUseId);
28699
+ if (!toolState) {
28700
+ return;
28701
+ }
28702
+ state.activeTools.delete(toolUseId);
28703
+ finalizeToolSpanState(toolState, { result });
28704
+ }
28705
+ function finalizeToolSpanState(toolState, data = {}) {
28706
+ if (!toolState) {
28707
+ return;
28708
+ }
28709
+ const result = data.result;
28710
+ safeLog5(toolState.span, {
28711
+ ...data.error ? { error: toLoggedError(data.error) } : {},
28712
+ metadata: {
28713
+ ...data.toolUse?.toolUseId ?? toolState.toolUse?.toolUseId ? {
28714
+ "gen_ai.tool.call.id": data.toolUse?.toolUseId ?? toolState.toolUse?.toolUseId
28715
+ } : {},
28716
+ ...extractToolName3(data.toolUse ?? toolState.toolUse) ? {
28717
+ "gen_ai.tool.name": extractToolName3(
28718
+ data.toolUse ?? toolState.toolUse
28719
+ )
28720
+ } : {},
28721
+ ...isObject(result) && typeof result.status === "string" ? { "strands.tool.status": result.status } : {}
28722
+ },
28723
+ metrics: buildDurationMetrics4(toolState.startTime),
28724
+ output: result
28725
+ });
28726
+ toolState.span.end();
28727
+ }
28728
+ function startNodeSpan(state, event, activeChildParents) {
28729
+ const nodeId = event.nodeId ?? "unknown";
28730
+ const node = findNode(event.orchestrator ?? state.orchestrator, nodeId);
28731
+ const child = extractNodeChild(node);
28732
+ const metadata = {
28733
+ "strands.node.id": nodeId,
28734
+ ...node?.type ? { "strands.node.type": node.type } : {},
28735
+ "strands.operation": "node.call",
28736
+ provider: "strands"
28737
+ };
28738
+ const span = withCurrent(
28739
+ state.span,
28740
+ () => startSpan({
28741
+ event: { metadata },
28742
+ name: `node: ${nodeId}`,
28743
+ spanAttributes: { type: "task" /* TASK */ }
28744
+ })
28745
+ );
28746
+ const nodeState = {
28747
+ ...child ? { child } : {},
28748
+ span,
28749
+ startTime: getCurrentUnixTimestamp()
28750
+ };
28751
+ state.activeNodes.set(nodeId, nodeState);
28752
+ if (child) {
28753
+ pushChildParent(activeChildParents, child, span);
28754
+ }
28755
+ }
28756
+ function logNodeResult(state, event) {
28757
+ const nodeId = event.nodeId ?? event.result?.nodeId ?? "unknown";
28758
+ const nodeState = state.activeNodes.get(nodeId);
28759
+ if (!nodeState) {
28760
+ return;
28761
+ }
28762
+ safeLog5(nodeState.span, {
28763
+ ...event.result?.error ? { error: toLoggedError(event.result.error) } : {},
28764
+ metadata: {
28765
+ ...event.nodeType ? { "strands.node.type": event.nodeType } : {},
28766
+ ...event.result?.status ? { "strands.node.status": event.result.status } : {}
28767
+ },
28768
+ metrics: {
28769
+ ...buildDurationMetrics4(nodeState.startTime),
28770
+ ...typeof event.result?.duration === "number" ? { "strands.node.duration_ms": event.result.duration } : {},
28771
+ ...parseUsage(event.result?.usage)
28772
+ },
28773
+ output: extractNodeResultOutput(event.result)
28774
+ });
28775
+ }
28776
+ function finalizeNodeSpan(state, event, activeChildParents) {
28777
+ const nodeId = event.nodeId ?? "unknown";
28778
+ const nodeState = state.activeNodes.get(nodeId);
28779
+ if (!nodeState) {
28780
+ return;
28781
+ }
28782
+ state.activeNodes.delete(nodeId);
28783
+ if (event.error) {
28784
+ safeLog5(nodeState.span, { error: toLoggedError(event.error) });
28785
+ }
28786
+ nodeState.span.end();
28787
+ if (nodeState.child) {
28788
+ popChildParent(activeChildParents, nodeState.child, nodeState.span);
28789
+ }
28790
+ }
28791
+ function collectHandoff(state, event) {
28792
+ state.handoffs.push({
28793
+ ...event.source ? { source: event.source } : {},
28794
+ ...Array.isArray(event.targets) ? { targets: event.targets } : {}
28795
+ });
28796
+ }
28797
+ function finalizeAgentStream(state, error, output) {
28798
+ if (state.finalized) {
28799
+ return;
28800
+ }
28801
+ state.finalized = true;
28802
+ finalizeModelSpan(state);
28803
+ for (const toolState of state.activeTools.values()) {
28804
+ finalizeToolSpanState(toolState);
28805
+ }
28806
+ state.activeTools.clear();
28807
+ safeLog5(state.span, {
28808
+ ...error ? { error: toLoggedError(error) } : {},
28809
+ metrics: buildDurationMetrics4(state.startTime),
28810
+ ...output !== void 0 ? { output } : {}
28811
+ });
28812
+ state.span.end();
28813
+ }
28814
+ function finalizeMultiAgentStream(state, activeChildParents, error, output) {
28815
+ if (state.finalized) {
28816
+ return;
28817
+ }
28818
+ state.finalized = true;
28819
+ for (const nodeState of state.activeNodes.values()) {
28820
+ if (nodeState.child) {
28821
+ popChildParent(activeChildParents, nodeState.child, nodeState.span);
28822
+ }
28823
+ nodeState.span.end();
28824
+ }
28825
+ state.activeNodes.clear();
28826
+ safeLog5(state.span, {
28827
+ ...error ? { error: toLoggedError(error) } : {},
28828
+ metrics: buildDurationMetrics4(state.startTime),
28829
+ ...output !== void 0 ? { output } : {}
28830
+ });
28831
+ state.span.end();
28832
+ }
28833
+ function extractAgent(event) {
28834
+ const candidate = event.agent ?? event.self;
28835
+ return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
28836
+ }
28837
+ function extractOrchestrator(event) {
28838
+ const candidate = event.orchestrator ?? event.self;
28839
+ return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
28840
+ }
28841
+ function extractAgentMetadata2(agent) {
28842
+ return {
28843
+ ...agent?.id ? { "strands.agent.id": agent.id } : {},
28844
+ ...agent?.name ? { "strands.agent.name": agent.name } : {},
28845
+ ...agent?.description ? { "strands.agent.description": agent.description } : {}
28846
+ };
28847
+ }
28848
+ function extractModelMetadata3(model) {
28849
+ const config = getModelConfig(model);
28850
+ const modelName2 = extractModelName2(model);
28851
+ return {
28852
+ ...modelName2 ? { model: modelName2 } : {},
28853
+ ...config?.api ? { "strands.model.api": config.api } : {},
28854
+ ...model?.stateful !== void 0 ? { "strands.model.stateful": model.stateful } : {}
28855
+ };
28856
+ }
28857
+ function extractAgentResultMetadata(result) {
28858
+ return {
28859
+ ...result?.stopReason ? { "strands.stop_reason": result.stopReason } : {},
28860
+ ...typeof result?.metrics?.latestContextSize === "number" ? { "strands.context_size": result.metrics.latestContextSize } : {},
28861
+ ...typeof result?.metrics?.projectedContextSize === "number" ? {
28862
+ "strands.projected_context_size": result.metrics.projectedContextSize
28863
+ } : {}
28864
+ };
28865
+ }
28866
+ function extractMultiAgentResultMetadata(result) {
28867
+ return {
28868
+ ...result?.status ? { "strands.status": result.status } : {},
28869
+ ...typeof result?.duration === "number" ? { "strands.duration_ms": result.duration } : {}
28870
+ };
28871
+ }
28872
+ function extractProvider(model) {
28873
+ const config = getModelConfig(model);
28874
+ if (typeof config?.provider === "string") {
28875
+ return config.provider;
28876
+ }
28877
+ const constructorName = getConstructorName(model).toLowerCase();
28878
+ if (constructorName.includes("openai")) {
28879
+ return "openai";
28880
+ }
28881
+ if (constructorName.includes("anthropic")) {
28882
+ return "anthropic";
28883
+ }
28884
+ if (constructorName.includes("bedrock")) {
28885
+ return "bedrock";
28886
+ }
28887
+ if (constructorName.includes("google")) {
28888
+ return "google";
28889
+ }
28890
+ if (constructorName.includes("vercel")) {
28891
+ return "vercel";
28892
+ }
28893
+ return "strands";
28894
+ }
28895
+ function extractModelName2(model) {
28896
+ const config = getModelConfig(model);
28897
+ if (typeof model?.modelId === "string") {
28898
+ return model.modelId;
28899
+ }
28900
+ if (typeof config?.modelId === "string") {
28901
+ return config.modelId;
28902
+ }
28903
+ if (typeof config?.model === "string") {
28904
+ return config.model;
28905
+ }
28906
+ return void 0;
28907
+ }
28908
+ function getModelConfig(model) {
28909
+ if (!model || typeof model.getConfig !== "function") {
28910
+ return void 0;
28911
+ }
28912
+ try {
28913
+ return model.getConfig();
28914
+ } catch (error) {
28915
+ logInstrumentationError5("Strands Agent SDK model config", error);
28916
+ return void 0;
28917
+ }
28918
+ }
28919
+ function formatAgentSpanName(agent) {
28920
+ return agent?.name ? `Agent: ${agent.name}` : "Strands Agent";
28921
+ }
28922
+ function formatModelSpanName(model) {
28923
+ const modelName2 = extractModelName2(model);
28924
+ return modelName2 ? `Strands model: ${modelName2}` : "Strands model";
28925
+ }
28926
+ function extractAgentResultOutput(result) {
28927
+ if (!result) {
28928
+ return void 0;
28929
+ }
28930
+ if (result.structuredOutput !== void 0) {
28931
+ return result.structuredOutput;
28932
+ }
28933
+ if (result.lastMessage) {
28934
+ return result.lastMessage;
28935
+ }
28936
+ return result;
28937
+ }
28938
+ function extractMultiAgentResultOutput(result) {
28939
+ if (!result) {
28940
+ return void 0;
28941
+ }
28942
+ if (Array.isArray(result.content)) {
28943
+ return normalizeContentBlocks(result.content);
28944
+ }
28945
+ return result;
28946
+ }
28947
+ function extractNodeResultOutput(result) {
28948
+ if (!result) {
28949
+ return void 0;
28950
+ }
28951
+ if (result.structuredOutput !== void 0) {
28952
+ return result.structuredOutput;
28953
+ }
28954
+ if (Array.isArray(result.content)) {
28955
+ return normalizeContentBlocks(result.content);
28956
+ }
28957
+ return result;
28958
+ }
28959
+ function normalizeContentBlocks(blocks) {
28960
+ const text = blocks.map((block) => typeof block.text === "string" ? block.text : void 0).filter((part) => Boolean(part)).join("");
28961
+ return text.length > 0 ? text : blocks;
28962
+ }
28963
+ function parseUsage(usage) {
28964
+ const metrics = {};
28965
+ assignMetric(metrics, "prompt_tokens", usage?.inputTokens);
28966
+ assignMetric(metrics, "completion_tokens", usage?.outputTokens);
28967
+ assignMetric(metrics, "tokens", usage?.totalTokens);
28968
+ assignMetric(metrics, "prompt_cached_tokens", usage?.cacheReadInputTokens);
28969
+ assignMetric(
28970
+ metrics,
28971
+ "prompt_cache_creation_tokens",
28972
+ usage?.cacheWriteInputTokens
28973
+ );
28974
+ return metrics;
28975
+ }
28976
+ function parseModelMetrics(metrics) {
28977
+ const parsed = {};
28978
+ assignMetric(parsed, "strands.latency_ms", metrics?.latencyMs);
28979
+ assignMetric(
28980
+ parsed,
28981
+ "strands.time_to_first_byte_ms",
28982
+ metrics?.timeToFirstByteMs
28983
+ );
28984
+ return parsed;
28985
+ }
28986
+ function assignMetric(metrics, key, value) {
28987
+ if (typeof value === "number" && Number.isFinite(value)) {
28988
+ metrics[key] = value;
28989
+ }
28990
+ }
28991
+ function buildDurationMetrics4(startTime) {
28992
+ const end = getCurrentUnixTimestamp();
28993
+ return {
28994
+ duration: end - startTime,
28995
+ end,
28996
+ start: startTime
28997
+ };
28998
+ }
28999
+ function cleanMetrics6(metrics) {
29000
+ const cleaned = {};
29001
+ for (const [key, value] of Object.entries(metrics)) {
29002
+ if (Number.isFinite(value)) {
29003
+ cleaned[key] = value;
29004
+ }
29005
+ }
29006
+ return cleaned;
29007
+ }
29008
+ function extractToolName3(toolUse, tool) {
29009
+ return toolUse?.name ?? tool?.name ?? "unknown";
29010
+ }
29011
+ function toolKey2(toolUse) {
29012
+ return toolUse?.toolUseId ?? toolUse?.name ?? "unknown";
29013
+ }
29014
+ function findNode(orchestrator, nodeId) {
29015
+ try {
29016
+ return orchestrator?.nodes?.get(nodeId);
29017
+ } catch {
29018
+ return void 0;
29019
+ }
29020
+ }
29021
+ function extractNodeChild(node) {
29022
+ const child = node?.agent ?? node?.orchestrator;
29023
+ return isObject(child) ? child : void 0;
29024
+ }
29025
+ function pushChildParent(activeChildParents, child, span) {
29026
+ const parents = activeChildParents.get(child) ?? /* @__PURE__ */ new Set();
29027
+ parents.add(span);
29028
+ activeChildParents.set(child, parents);
29029
+ }
29030
+ function popChildParent(activeChildParents, child, span) {
29031
+ const parents = activeChildParents.get(child);
29032
+ if (!parents) {
29033
+ return;
29034
+ }
29035
+ parents.delete(span);
29036
+ if (parents.size === 0) {
29037
+ activeChildParents.delete(child);
29038
+ }
29039
+ }
29040
+ function getOnlyChildParent(activeChildParents, child) {
29041
+ const parents = activeChildParents.get(child);
29042
+ if (!parents || parents.size !== 1) {
29043
+ return void 0;
29044
+ }
29045
+ return parents.values().next().value;
29046
+ }
29047
+ function getConstructorName(value) {
29048
+ if (!isObject(value)) {
29049
+ return "";
29050
+ }
29051
+ const constructor = value.constructor;
29052
+ return typeof constructor === "function" && typeof constructor.name === "string" ? constructor.name : "";
29053
+ }
29054
+ function safeLog5(span, event) {
29055
+ try {
29056
+ span.log(event);
29057
+ } catch (error) {
29058
+ logInstrumentationError5("Strands Agent SDK span log", error);
29059
+ }
29060
+ }
29061
+ function logInstrumentationError5(context, error) {
29062
+ debugLogger.debug(`${context}:`, error);
29063
+ }
29064
+
29065
+ // src/instrumentation/braintrust-plugin.ts
27457
29066
  var BraintrustPlugin = class extends BasePlugin {
27458
29067
  config;
27459
29068
  openaiPlugin = null;
@@ -27471,11 +29080,13 @@ var BraintrustPlugin = class extends BasePlugin {
27471
29080
  googleADKPlugin = null;
27472
29081
  coherePlugin = null;
27473
29082
  groqPlugin = null;
29083
+ bedrockRuntimePlugin = null;
27474
29084
  genkitPlugin = null;
27475
29085
  gitHubCopilotPlugin = null;
27476
29086
  fluePlugin = null;
27477
29087
  langChainPlugin = null;
27478
29088
  piCodingAgentPlugin = null;
29089
+ strandsAgentSDKPlugin = null;
27479
29090
  constructor(config = {}) {
27480
29091
  super();
27481
29092
  this.config = config;
@@ -27542,6 +29153,10 @@ var BraintrustPlugin = class extends BasePlugin {
27542
29153
  this.groqPlugin = new GroqPlugin();
27543
29154
  this.groqPlugin.enable();
27544
29155
  }
29156
+ if (integrations.bedrock !== false && integrations.awsBedrock !== false && integrations.awsBedrockRuntime !== false) {
29157
+ this.bedrockRuntimePlugin = new BedrockRuntimePlugin();
29158
+ this.bedrockRuntimePlugin.enable();
29159
+ }
27545
29160
  if (integrations.genkit !== false) {
27546
29161
  this.genkitPlugin = new GenkitPlugin();
27547
29162
  this.genkitPlugin.enable();
@@ -27554,7 +29169,11 @@ var BraintrustPlugin = class extends BasePlugin {
27554
29169
  this.piCodingAgentPlugin = new PiCodingAgentPlugin();
27555
29170
  this.piCodingAgentPlugin.enable();
27556
29171
  }
27557
- if (getIntegrationConfig(integrations, "flue") !== false) {
29172
+ if (integrations.strandsAgentSDK !== false) {
29173
+ this.strandsAgentSDKPlugin = new StrandsAgentSDKPlugin();
29174
+ this.strandsAgentSDKPlugin.enable();
29175
+ }
29176
+ if (integrations.flue !== false) {
27558
29177
  this.fluePlugin = new FluePlugin();
27559
29178
  this.fluePlugin.enable();
27560
29179
  }
@@ -27624,6 +29243,10 @@ var BraintrustPlugin = class extends BasePlugin {
27624
29243
  this.groqPlugin.disable();
27625
29244
  this.groqPlugin = null;
27626
29245
  }
29246
+ if (this.bedrockRuntimePlugin) {
29247
+ this.bedrockRuntimePlugin.disable();
29248
+ this.bedrockRuntimePlugin = null;
29249
+ }
27627
29250
  if (this.genkitPlugin) {
27628
29251
  this.genkitPlugin.disable();
27629
29252
  this.genkitPlugin = null;
@@ -27636,6 +29259,10 @@ var BraintrustPlugin = class extends BasePlugin {
27636
29259
  this.piCodingAgentPlugin.disable();
27637
29260
  this.piCodingAgentPlugin = null;
27638
29261
  }
29262
+ if (this.strandsAgentSDKPlugin) {
29263
+ this.strandsAgentSDKPlugin.disable();
29264
+ this.strandsAgentSDKPlugin = null;
29265
+ }
27639
29266
  if (this.fluePlugin) {
27640
29267
  this.fluePlugin.disable();
27641
29268
  this.fluePlugin = null;
@@ -27660,6 +29287,10 @@ var envIntegrationAliases = {
27660
29287
  picodingagent: "piCodingAgent",
27661
29288
  picodingagentsdk: "piCodingAgent",
27662
29289
  "@earendil-works/pi-coding-agent": "piCodingAgent",
29290
+ strandsAgentSDK: "strandsAgentSDK",
29291
+ strandsagentsdk: "strandsAgentSDK",
29292
+ "strands-agent-sdk": "strandsAgentSDK",
29293
+ "@strands-agents/sdk": "strandsAgentSDK",
27663
29294
  anthropic: "anthropic",
27664
29295
  aisdk: "aisdk",
27665
29296
  "ai-sdk": "aisdk",
@@ -27690,6 +29321,12 @@ var envIntegrationAliases = {
27690
29321
  cohere: "cohere",
27691
29322
  groq: "groq",
27692
29323
  "groq-sdk": "groq",
29324
+ bedrock: "bedrock",
29325
+ "aws-bedrock": "awsBedrock",
29326
+ awsbedrock: "awsBedrock",
29327
+ "aws-bedrock-runtime": "awsBedrockRuntime",
29328
+ awsbedrockruntime: "awsBedrockRuntime",
29329
+ "@aws-sdk/client-bedrock-runtime": "awsBedrockRuntime",
27693
29330
  genkit: "genkit",
27694
29331
  "firebase-genkit": "genkit",
27695
29332
  githubcopilot: "gitHubCopilot",
@@ -27722,20 +29359,25 @@ function getDefaultInstrumentationIntegrations() {
27722
29359
  mistral: true,
27723
29360
  cohere: true,
27724
29361
  groq: true,
29362
+ bedrock: true,
29363
+ awsBedrock: true,
29364
+ awsBedrockRuntime: true,
27725
29365
  genkit: true,
27726
29366
  gitHubCopilot: true,
27727
29367
  langchain: true,
27728
29368
  langgraph: true,
27729
- piCodingAgent: true
29369
+ piCodingAgent: true,
29370
+ strandsAgentSDK: true
27730
29371
  };
27731
29372
  }
27732
29373
  function readDisabledInstrumentationEnvConfig(disabledList) {
27733
29374
  const integrations = {};
27734
29375
  if (disabledList) {
27735
29376
  for (const value of disabledList.split(",")) {
27736
- const sdk = value.trim().toLowerCase();
29377
+ const rawSdk = value.trim();
29378
+ const sdk = rawSdk.toLowerCase();
27737
29379
  if (sdk.length > 0) {
27738
- integrations[envIntegrationAliases[sdk] ?? sdk] = false;
29380
+ integrations[envIntegrationAliases[rawSdk] ?? envIntegrationAliases[sdk] ?? sdk] = false;
27739
29381
  }
27740
29382
  }
27741
29383
  }
@@ -28023,6 +29665,7 @@ __export(exports_exports, {
28023
29665
  _internalSetInitialState: () => _internalSetInitialState,
28024
29666
  addAzureBlobHeaders: () => addAzureBlobHeaders,
28025
29667
  braintrustAISDKTelemetry: () => braintrustAISDKTelemetry,
29668
+ braintrustFlueInstrumentation: () => braintrustFlueInstrumentation,
28026
29669
  braintrustFlueObserver: () => braintrustFlueObserver,
28027
29670
  braintrustStreamChunkSchema: () => braintrustStreamChunkSchema,
28028
29671
  buildLocalSummary: () => buildLocalSummary,
@@ -28099,6 +29742,7 @@ __export(exports_exports, {
28099
29742
  wrapAISDKModel: () => wrapAISDKModel,
28100
29743
  wrapAgentClass: () => wrapAgentClass,
28101
29744
  wrapAnthropic: () => wrapAnthropic,
29745
+ wrapBedrockRuntime: () => wrapBedrockRuntime,
28102
29746
  wrapClaudeAgentSDK: () => wrapClaudeAgentSDK,
28103
29747
  wrapCohere: () => wrapCohere,
28104
29748
  wrapCopilotClient: () => wrapCopilotClient,
@@ -28116,6 +29760,7 @@ __export(exports_exports, {
28116
29760
  wrapOpenRouter: () => wrapOpenRouter,
28117
29761
  wrapOpenRouterAgent: () => wrapOpenRouterAgent,
28118
29762
  wrapPiCodingAgentSDK: () => wrapPiCodingAgentSDK,
29763
+ wrapStrandsAgentSDK: () => wrapStrandsAgentSDK,
28119
29764
  wrapTraced: () => wrapTraced,
28120
29765
  wrapVitest: () => wrapVitest
28121
29766
  });
@@ -30224,6 +31869,174 @@ function patchAgentSessionClass(AgentSession) {
30224
31869
  });
30225
31870
  }
30226
31871
 
31872
+ // src/wrappers/strands-agent-sdk.ts
31873
+ var WRAPPED_CLASS = /* @__PURE__ */ Symbol.for("braintrust.strands-agent-sdk.wrapped-class");
31874
+ var WRAPPED_INSTANCE = /* @__PURE__ */ Symbol.for(
31875
+ "braintrust.strands-agent-sdk.wrapped-instance"
31876
+ );
31877
+ function wrapStrandsAgentSDK(sdk) {
31878
+ if (!sdk || typeof sdk !== "object") {
31879
+ return sdk;
31880
+ }
31881
+ const maybeSDK = sdk;
31882
+ if (typeof maybeSDK.Agent !== "function" && typeof maybeSDK.Graph !== "function" && typeof maybeSDK.Swarm !== "function") {
31883
+ console.warn("Unsupported Strands Agent SDK. Not wrapping.");
31884
+ return sdk;
31885
+ }
31886
+ const target = isModuleNamespace4(sdk) ? Object.setPrototypeOf({}, sdk) : sdk;
31887
+ return new Proxy(target, {
31888
+ get(target2, prop, receiver) {
31889
+ const value = Reflect.get(target2, prop, receiver);
31890
+ if (prop === "Agent" && typeof value === "function") {
31891
+ return wrapAgentClass2(value);
31892
+ }
31893
+ if (prop === "Graph" && typeof value === "function") {
31894
+ return wrapMultiAgentClass(
31895
+ value,
31896
+ "graph"
31897
+ );
31898
+ }
31899
+ if (prop === "Swarm" && typeof value === "function") {
31900
+ return wrapMultiAgentClass(
31901
+ value,
31902
+ "swarm"
31903
+ );
31904
+ }
31905
+ return value;
31906
+ }
31907
+ });
31908
+ }
31909
+ function isModuleNamespace4(obj) {
31910
+ if (!obj || typeof obj !== "object") {
31911
+ return false;
31912
+ }
31913
+ if (obj.constructor?.name === "Module") {
31914
+ return true;
31915
+ }
31916
+ const keys = Object.keys(obj);
31917
+ if (keys.length === 0) {
31918
+ return false;
31919
+ }
31920
+ const descriptor = Object.getOwnPropertyDescriptor(obj, keys[0]);
31921
+ return descriptor ? !descriptor.configurable && !descriptor.writable : false;
31922
+ }
31923
+ function wrapAgentClass2(AgentClass) {
31924
+ if (AgentClass[WRAPPED_CLASS]) {
31925
+ return AgentClass;
31926
+ }
31927
+ return new Proxy(AgentClass, {
31928
+ get(target, prop, receiver) {
31929
+ if (prop === WRAPPED_CLASS) {
31930
+ return true;
31931
+ }
31932
+ const value = Reflect.get(target, prop, receiver);
31933
+ return typeof value === "function" ? value.bind(target) : value;
31934
+ },
31935
+ construct(target, args, newTarget) {
31936
+ const instance = Reflect.construct(target, args, newTarget);
31937
+ return wrapAgentInstance(instance);
31938
+ }
31939
+ });
31940
+ }
31941
+ function wrapMultiAgentClass(MultiAgentClass, kind) {
31942
+ if (MultiAgentClass[WRAPPED_CLASS]) {
31943
+ return MultiAgentClass;
31944
+ }
31945
+ return new Proxy(MultiAgentClass, {
31946
+ get(target, prop, receiver) {
31947
+ if (prop === WRAPPED_CLASS) {
31948
+ return true;
31949
+ }
31950
+ const value = Reflect.get(target, prop, receiver);
31951
+ return typeof value === "function" ? value.bind(target) : value;
31952
+ },
31953
+ construct(target, args, newTarget) {
31954
+ const instance = Reflect.construct(target, args, newTarget);
31955
+ return wrapMultiAgentInstance(instance, kind);
31956
+ }
31957
+ });
31958
+ }
31959
+ function wrapAgentInstance(agent) {
31960
+ if (!agent || typeof agent !== "object") {
31961
+ return agent;
31962
+ }
31963
+ if (agent[WRAPPED_INSTANCE]) {
31964
+ return agent;
31965
+ }
31966
+ const proxy = new Proxy(agent, {
31967
+ get(target, prop, receiver) {
31968
+ if (prop === WRAPPED_INSTANCE) {
31969
+ return true;
31970
+ }
31971
+ const value = Reflect.get(target, prop, receiver);
31972
+ if (prop === "stream" && typeof value === "function") {
31973
+ return function(args, options) {
31974
+ const callArgs = [args, options];
31975
+ return strandsAgentSDKChannels.agentStream.traceSync(
31976
+ () => Reflect.apply(value, target, callArgs),
31977
+ {
31978
+ agent: proxy,
31979
+ arguments: callArgs,
31980
+ self: proxy
31981
+ }
31982
+ );
31983
+ };
31984
+ }
31985
+ if (prop === "invoke" && typeof value === "function") {
31986
+ return async function(args, options) {
31987
+ return consumeAsyncGenerator(proxy.stream(args, options));
31988
+ };
31989
+ }
31990
+ return typeof value === "function" ? value.bind(target) : value;
31991
+ }
31992
+ });
31993
+ return proxy;
31994
+ }
31995
+ function wrapMultiAgentInstance(orchestrator, kind) {
31996
+ if (!orchestrator || typeof orchestrator !== "object") {
31997
+ return orchestrator;
31998
+ }
31999
+ if (orchestrator[WRAPPED_INSTANCE]) {
32000
+ return orchestrator;
32001
+ }
32002
+ const proxy = new Proxy(orchestrator, {
32003
+ get(target, prop, receiver) {
32004
+ if (prop === WRAPPED_INSTANCE) {
32005
+ return true;
32006
+ }
32007
+ const value = Reflect.get(target, prop, receiver);
32008
+ if (prop === "stream" && typeof value === "function") {
32009
+ return function(input, options) {
32010
+ const callArgs = [input, options];
32011
+ const channel2 = kind === "graph" ? strandsAgentSDKChannels.graphStream : strandsAgentSDKChannels.swarmStream;
32012
+ return channel2.traceSync(
32013
+ () => Reflect.apply(value, target, callArgs),
32014
+ {
32015
+ arguments: callArgs,
32016
+ orchestrator: proxy,
32017
+ self: proxy
32018
+ }
32019
+ );
32020
+ };
32021
+ }
32022
+ if (prop === "invoke" && typeof value === "function") {
32023
+ return async function(input, options) {
32024
+ return consumeAsyncGenerator(proxy.stream(input, options));
32025
+ };
32026
+ }
32027
+ return typeof value === "function" ? value.bind(target) : value;
32028
+ }
32029
+ });
32030
+ return proxy;
32031
+ }
32032
+ async function consumeAsyncGenerator(generator) {
32033
+ let result = await generator.next();
32034
+ while (!result.done) {
32035
+ result = await generator.next();
32036
+ }
32037
+ return result.value;
32038
+ }
32039
+
30227
32040
  // src/wrappers/google-genai.ts
30228
32041
  function wrapGoogleGenAI(googleGenAI) {
30229
32042
  if (!googleGenAI || typeof googleGenAI !== "object") {
@@ -30376,7 +32189,7 @@ function wrapGoogleADK(adkModule) {
30376
32189
  );
30377
32190
  }
30378
32191
  if ((prop === "LlmAgent" || prop === "Agent" || prop === "SequentialAgent" || prop === "ParallelAgent" || prop === "LoopAgent") && typeof value === "function") {
30379
- return wrapAgentClass2(value);
32192
+ return wrapAgentClass3(value);
30380
32193
  }
30381
32194
  if (prop === "FunctionTool" && typeof value === "function") {
30382
32195
  return wrapToolClass(value);
@@ -30412,15 +32225,15 @@ function wrapRunnerRunAsync(runner) {
30412
32225
  });
30413
32226
  };
30414
32227
  }
30415
- function wrapAgentClass2(AgentClass) {
32228
+ function wrapAgentClass3(AgentClass) {
30416
32229
  return new Proxy(AgentClass, {
30417
32230
  construct(target, args) {
30418
32231
  const instance = Reflect.construct(target, args);
30419
- return wrapAgentInstance(instance);
32232
+ return wrapAgentInstance2(instance);
30420
32233
  }
30421
32234
  });
30422
32235
  }
30423
- function wrapAgentInstance(agent) {
32236
+ function wrapAgentInstance2(agent) {
30424
32237
  return new Proxy(agent, {
30425
32238
  get(target, prop, receiver) {
30426
32239
  if (prop === "runAsync") {
@@ -31392,6 +33205,90 @@ function wrapEmbeddingsCreate2(create) {
31392
33205
  });
31393
33206
  }
31394
33207
 
33208
+ // src/wrappers/bedrock-runtime.ts
33209
+ function wrapBedrockRuntime(client) {
33210
+ if (isSupportedBedrockRuntimeClient(client)) {
33211
+ return bedrockRuntimeProxy(client);
33212
+ }
33213
+ console.warn("Unsupported Bedrock Runtime library. Not wrapping.");
33214
+ return client;
33215
+ }
33216
+ var bedrockRuntimeProxyCache = /* @__PURE__ */ new WeakMap();
33217
+ var BEDROCK_RUNTIME_OPERATION_METHODS = /* @__PURE__ */ new Set([
33218
+ "converse",
33219
+ "converseStream",
33220
+ "invokeModel",
33221
+ "invokeModelWithBidirectionalStream",
33222
+ "invokeModelWithResponseStream"
33223
+ ]);
33224
+ function isRecord6(value) {
33225
+ return typeof value === "object" && value !== null;
33226
+ }
33227
+ function isSupportedBedrockRuntimeClient(value) {
33228
+ return isRecord6(value) && typeof value.send === "function";
33229
+ }
33230
+ function bedrockRuntimeProxy(client) {
33231
+ const cached = bedrockRuntimeProxyCache.get(client);
33232
+ if (cached) {
33233
+ return cached;
33234
+ }
33235
+ const privateMethodWorkaroundCache = /* @__PURE__ */ new WeakMap();
33236
+ const operationMethodCache = /* @__PURE__ */ new WeakMap();
33237
+ const proxy = new Proxy(client, {
33238
+ get(target, prop, receiver) {
33239
+ if (prop === "send") {
33240
+ return wrapSend(target.send.bind(target));
33241
+ }
33242
+ const value = Reflect.get(target, prop, receiver);
33243
+ if (typeof value !== "function") {
33244
+ return value;
33245
+ }
33246
+ if (typeof prop === "string" && BEDROCK_RUNTIME_OPERATION_METHODS.has(prop)) {
33247
+ const cachedValue2 = operationMethodCache.get(value);
33248
+ if (cachedValue2) {
33249
+ return cachedValue2;
33250
+ }
33251
+ const thisBoundValue2 = function(...args) {
33252
+ const thisArg = this === proxy ? proxy : this;
33253
+ const output = Reflect.apply(value, thisArg, args);
33254
+ return output === target ? proxy : output;
33255
+ };
33256
+ operationMethodCache.set(value, thisBoundValue2);
33257
+ return thisBoundValue2;
33258
+ }
33259
+ const cachedValue = privateMethodWorkaroundCache.get(value);
33260
+ if (cachedValue) {
33261
+ return cachedValue;
33262
+ }
33263
+ const thisBoundValue = function(...args) {
33264
+ const thisArg = this === proxy ? target : this;
33265
+ const output = Reflect.apply(value, thisArg, args);
33266
+ return output === target ? proxy : output;
33267
+ };
33268
+ privateMethodWorkaroundCache.set(value, thisBoundValue);
33269
+ return thisBoundValue;
33270
+ }
33271
+ });
33272
+ bedrockRuntimeProxyCache.set(client, proxy);
33273
+ return proxy;
33274
+ }
33275
+ function wrapSend(send) {
33276
+ return (command, optionsOrCb, cb) => {
33277
+ if (getBedrockRuntimeOperation(command) === void 0 || typeof optionsOrCb === "function" || typeof cb === "function") {
33278
+ return send(command, optionsOrCb, cb);
33279
+ }
33280
+ return bedrockRuntimeChannels.clientSend.tracePromise(
33281
+ () => runWithAutoInstrumentationSuppressed(
33282
+ () => send(command, optionsOrCb)
33283
+ ),
33284
+ {
33285
+ arguments: [command, optionsOrCb],
33286
+ span_info: buildBedrockRuntimeSpanInfo(command)
33287
+ }
33288
+ );
33289
+ };
33290
+ }
33291
+
31395
33292
  // src/wrappers/github-copilot.ts
31396
33293
  function isGitHubCopilotClient(value) {
31397
33294
  return value !== null && typeof value === "object" && typeof value.createSession === "function" && typeof value.resumeSession === "function";
@@ -34257,14 +36154,15 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
34257
36154
  return;
34258
36155
  }
34259
36156
  const eventDataset = experiment ? experiment.dataset : Dataset2.isDataset(evaluator.data) ? evaluator.data : void 0;
34260
- const inlineOrigin = datum.origin === void 0 ? void 0 : ObjectReference.parse(datum.origin);
34261
- const origin = inlineOrigin ?? (eventDataset && datum.id && datum._xact_id ? {
36157
+ const inlineDatasetOrigin = eventDataset && datum.id && datum._xact_id ? {
34262
36158
  object_type: "dataset",
34263
36159
  object_id: await eventDataset.id,
34264
36160
  id: datum.id,
34265
36161
  created: datum.created,
34266
36162
  _xact_id: datum._xact_id
34267
- } : void 0);
36163
+ } : void 0;
36164
+ const parsedDatumOrigin = ObjectReference.safeParse(datum.origin);
36165
+ const origin = inlineDatasetOrigin ?? (parsedDatumOrigin?.success ? parsedDatumOrigin.data : void 0);
34268
36166
  const baseEvent = {
34269
36167
  name: "eval",
34270
36168
  spanAttributes: {