Skip to content

Commit e39ca15

Browse files
committed
fix: remove metrics/usage from message history selection set and add SerializedStreamingResponseChunk type
1 parent 912c287 commit e39ca15

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

packages/ai-constructs/src/conversation/runtime/bedrock_converse_adapter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ export class BedrockConverseAdapter {
175175
let blockIndex = 0;
176176
let lastBlockIndex = 0;
177177
let stopReason = '';
178+
// The following metadata´ are overwritten on each iteration of the tool-use loop.
179+
// Only the final iteration's values are reported, as intermediate iterations
180+
// are tool-use round-trips and the final iteration contains the actual model response.
178181
let latencyMs = 0;
179182
let inputTokens = 0;
180183
let outputTokens = 0;

packages/ai-constructs/src/conversation/runtime/conversation_message_history_retriever.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,6 @@ const messageItemSelectionSet = `
9292
toolUseId
9393
}
9494
}
95-
metrics {
96-
latencyMs
97-
}
98-
usage {
99-
inputTokens
100-
outputTokens
101-
totalTokens
102-
}
10395
`;
10496

10597
/**

packages/ai-constructs/src/conversation/runtime/conversation_turn_response_sender.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ export type MutationResponseInput = {
1515
};
1616
};
1717

18+
export type SerializedStreamingResponseChunk = Omit<
19+
StreamingResponseChunk,
20+
'metrics' | 'usage'
21+
> & {
22+
metrics?: string;
23+
usage?: string;
24+
};
25+
1826
export type MutationStreamingResponseInput = {
19-
input: StreamingResponseChunk;
27+
input: SerializedStreamingResponseChunk;
2028
};
2129

2230
export type MutationErrorsResponseInput = {
@@ -133,17 +141,19 @@ export class ConversationTurnResponseSender {
133141
}
134142
}
135143
`;
136-
const serializedChunk = {
137-
...chunk,
144+
145+
const { metrics, usage, ...rest } = chunk;
146+
const serializedChunk: SerializedStreamingResponseChunk = {
147+
...rest,
138148
accumulatedTurnContent: this.serializeContent(
139149
chunk.accumulatedTurnContent,
140150
),
141-
...(chunk.metrics && { metrics: JSON.stringify(chunk.metrics) }),
142-
...(chunk.usage && { usage: JSON.stringify(chunk.usage) }),
151+
...(metrics && { metrics: JSON.stringify(metrics) }),
152+
...(usage && { usage: JSON.stringify(usage) }),
143153
};
144154
const variables = {
145155
input: serializedChunk,
146-
} as MutationStreamingResponseInput;
156+
};
147157
return { query, variables };
148158
};
149159

0 commit comments

Comments
 (0)