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