fix: tweaks to transform logic for anthropic and bedrock (#26276)

This commit is contained in:
Aiden Cline
2026-05-07 23:17:43 -05:00
committed by GitHub
parent e0396b809a
commit 4e14f79511
3 changed files with 190 additions and 24 deletions

View File

@@ -155,6 +155,54 @@ describe("session.message-v2.toModelMessage", () => {
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([])
})
test("filters out user messages with only empty text parts", async () => {
const messageID = "m-user"
const input: MessageV2.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "",
},
] as MessageV2.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([])
})
test("filters empty user text parts while keeping non-empty parts", async () => {
const messageID = "m-user"
const input: MessageV2.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "",
},
{
...basePart(messageID, "p2"),
type: "text",
text: "hello",
},
] as MessageV2.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "hello" }],
},
])
})
test("includes synthetic text parts", async () => {
const messageID = "m-user"
@@ -443,6 +491,108 @@ describe("session.message-v2.toModelMessage", () => {
})
})
test("moves bedrock pdf tool-result media into a separate user message", async () => {
const bedrockModel: Provider.Model = {
...model,
id: ModelID.make("amazon-bedrock/anthropic.claude-sonnet-4-6"),
providerID: ProviderID.make("amazon-bedrock"),
api: {
id: "anthropic.claude-sonnet-4-6",
url: "https://bedrock-runtime.us-east-1.amazonaws.com",
npm: "@ai-sdk/amazon-bedrock",
},
capabilities: {
...model.capabilities,
attachment: true,
input: {
...model.capabilities.input,
image: true,
pdf: true,
},
},
}
const pdf = Buffer.from("%PDF-1.4\n").toString("base64")
const userID = "m-user-bedrock-pdf"
const assistantID = "m-assistant-bedrock-pdf"
const input: MessageV2.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1-bedrock-pdf"),
type: "text",
text: "run tool",
},
] as MessageV2.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1-bedrock-pdf"),
type: "tool",
callID: "call-bedrock-pdf-1",
tool: "read",
state: {
status: "completed",
input: { filePath: "/tmp/example.pdf" },
output: "PDF read successfully",
title: "Read",
metadata: {},
time: { start: 0, end: 1 },
attachments: [
{
...basePart(assistantID, "file-bedrock-pdf-1"),
type: "file",
mime: "application/pdf",
filename: "example.pdf",
url: `data:application/pdf;base64,${pdf}`,
},
],
},
},
] as MessageV2.Part[],
},
]
expect(await MessageV2.toModelMessages(input, bedrockModel)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-bedrock-pdf-1",
toolName: "read",
input: { filePath: "/tmp/example.pdf" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-bedrock-pdf-1",
toolName: "read",
output: { type: "text", value: "PDF read successfully" },
},
],
},
{
role: "user",
content: [
{ type: "text", text: "Attached media from tool result:" },
{ type: "file", mediaType: "application/pdf", filename: "example.pdf", data: `data:application/pdf;base64,${pdf}` },
],
},
])
})
test("omits provider metadata when assistant model differs", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
@@ -1134,8 +1284,9 @@ describe("session.message-v2.toModelMessage", () => {
expect((result[1].content as any[]).find((p) => p.type === "text").text).toBe("the answer")
})
test("substitutes space for empty text when reasoning signature is under 'bedrock' namespace", async () => {
// AWS Bedrock hosts Anthropic Claude but stores signatures under metadata.bedrock
test("leaves empty text alone when reasoning signature is under 'bedrock' namespace", async () => {
// Bedrock signed reasoning is preserved as reasoning metadata, but unlike the
// direct Anthropic path we do not preserve empty text separators for Bedrock.
const assistantID = "m-assistant-bedrock"
const input: MessageV2.WithParts[] = [
{
@@ -1157,7 +1308,7 @@ describe("session.message-v2.toModelMessage", () => {
expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual([" ", "answer"])
expect(texts.map((t) => t.text)).toStrictEqual(["", "answer"])
})
test("leaves empty text alone when reasoning has no Anthropic signature", async () => {