Skip to content

Commit b4bfb7f

Browse files
author
Peter Steinberger
committed
fix(bedrock): reject truncated streams and preserve audio results
1 parent 12297b7 commit b4bfb7f

2 files changed

Lines changed: 129 additions & 3 deletions

File tree

‎extensions/amazon-bedrock/stream.runtime.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,74 @@ describe("Bedrock inbound image base64", () => {
123123
});
124124

125125
describe("Bedrock tool-result replay", () => {
126+
it("replays unsupported audio attachments as their canonical text placeholder", () => {
127+
const messages = testing.convertMessages(
128+
{
129+
messages: [
130+
{
131+
role: "toolResult",
132+
toolCallId: "call_audio",
133+
toolName: "listen",
134+
content: [{ type: "audio", mimeType: "audio/wav", data: "YXVkaW8=" }],
135+
isError: false,
136+
},
137+
],
138+
} as never,
139+
bedrockModel({ input: ["text", "image"] }),
140+
"none",
141+
);
142+
143+
expect(messages).toHaveLength(1);
144+
expect(messages[0]).toMatchObject({
145+
role: ConversationRole.USER,
146+
content: [
147+
{
148+
toolResult: {
149+
toolUseId: "call_audio",
150+
content: [{ text: "(see attached audio)" }],
151+
},
152+
},
153+
],
154+
});
155+
});
156+
157+
it("preserves valid text and image attachments alongside unsupported audio", () => {
158+
const messages = testing.convertMessages(
159+
{
160+
messages: [
161+
{
162+
role: "toolResult",
163+
toolCallId: "call_media",
164+
toolName: "inspect",
165+
content: [
166+
{ type: "audio", mimeType: "audio/wav", data: "YXVkaW8=" },
167+
{ type: "text", text: "actual tool output" },
168+
{ type: "image", mimeType: "image/png", data: "aW1hZ2U=" },
169+
],
170+
isError: false,
171+
},
172+
],
173+
} as never,
174+
bedrockModel({ input: ["text", "image"] }),
175+
"none",
176+
);
177+
178+
expect(messages[0]).toMatchObject({
179+
role: ConversationRole.USER,
180+
content: [
181+
{
182+
toolResult: {
183+
toolUseId: "call_media",
184+
content: [
185+
{ text: "actual tool output" },
186+
{ image: { format: "png", source: { bytes: expect.any(Uint8Array) } } },
187+
],
188+
},
189+
},
190+
],
191+
});
192+
});
193+
126194
it("drops payload-less image husks from consecutive tool results", () => {
127195
const messages = testing.convertMessages(
128196
{
@@ -335,6 +403,61 @@ describe("Bedrock profile endpoint resolution", () => {
335403
});
336404

337405
describe("Bedrock stop reasons", () => {
406+
it.each([
407+
{
408+
name: "text",
409+
events: [
410+
{ contentBlockDelta: { contentBlockIndex: 0, delta: { text: "truncated response" } } },
411+
{ contentBlockStop: { contentBlockIndex: 0 } },
412+
],
413+
contentType: "text",
414+
},
415+
{
416+
name: "tool call",
417+
events: [
418+
{
419+
contentBlockStart: {
420+
contentBlockIndex: 0,
421+
start: { toolUse: { toolUseId: "call_lookup", name: "lookup" } },
422+
},
423+
},
424+
{
425+
contentBlockDelta: {
426+
contentBlockIndex: 0,
427+
delta: { toolUse: { input: '{"query":"partial"}' } },
428+
},
429+
},
430+
{ contentBlockStop: { contentBlockIndex: 0 } },
431+
],
432+
contentType: "toolCall",
433+
},
434+
])(
435+
"reports truncated $name streams without a terminal messageStop",
436+
async ({ events, contentType }) => {
437+
vi.spyOn(BedrockRuntimeClient.prototype, "send").mockResolvedValue({
438+
$metadata: { httpStatusCode: 200 },
439+
stream: streamEvents([{ messageStart: { role: ConversationRole.ASSISTANT } }, ...events]),
440+
} as never);
441+
442+
const stream = streamBedrockForTest(bedrockModel({}), {
443+
messages: [{ role: "user", content: "Hello", timestamp: 0 }],
444+
} as never);
445+
const eventTypes: string[] = [];
446+
for await (const event of stream) {
447+
eventTypes.push(event.type);
448+
}
449+
const result = await stream.result();
450+
451+
expect(eventTypes.at(-1)).toBe("error");
452+
expect(eventTypes).not.toContain("done");
453+
expect(result.stopReason).toBe("error");
454+
expect(result.errorMessage).toBe("Bedrock stream ended before messageStop");
455+
expect(result.content).toEqual([expect.objectContaining({ type: contentType })]);
456+
expect(result.content[0]).not.toHaveProperty("index");
457+
expect(result.content[0]).not.toHaveProperty("partialJson");
458+
},
459+
);
460+
338461
it.each([
339462
BedrockStopReason.CONTENT_FILTERED,
340463
BedrockStopReason.GUARDRAIL_INTERVENED,

‎extensions/amazon-bedrock/stream.runtime.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOptions> =
332332
}
333333
}
334334

335-
if (refusalBuffer && !sawMessageStop) {
335+
if (!sawMessageStop) {
336336
throw new Error("Bedrock stream ended before messageStop");
337337
}
338338
if (options.signal?.aborted) {
@@ -812,15 +812,18 @@ function createBedrockToolResult(message: ToolResultMessage): ContentBlock.ToolR
812812
content.push({ text: sanitizeSurrogates(block.text) });
813813
continue;
814814
}
815-
if (describeToolResultMediaPlaceholder([block])) {
815+
if (block.type === "image" && describeToolResultMediaPlaceholder([block])) {
816816
content.push({ image: createImageBlock(block.mimeType, block.data) });
817817
}
818818
}
819819

820820
return {
821821
toolResult: {
822822
toolUseId: message.toolCallId,
823-
content: content.length > 0 ? content : [{ text: "(no output)" }],
823+
content:
824+
content.length > 0
825+
? content
826+
: [{ text: describeToolResultMediaPlaceholder(message.content) ?? "(no output)" }],
824827
status: message.isError ? ToolResultStatus.ERROR : ToolResultStatus.SUCCESS,
825828
},
826829
};

0 commit comments

Comments
 (0)