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