Skip to content

Commit 7776a8f

Browse files
ericc-chopencode
andcommitted
fix: ensure tool results precede user content to comply with Anthropic protocol
Reordered message processing in handleUserMessage to place tool_result blocks before user content blocks. This prevents 400 errors when clients send messages containing both tool results and additional user content. 🤖 Generated with [opencode](https://opencode.ai) Co-Authored-By: opencode <noreply@opencode.ai>
1 parent 72c19ed commit 7776a8f

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/routes/messages/non-stream-translation.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,21 @@ function handleUserMessage(message: AnthropicUserMessage): Array<Message> {
8888
(block) => block.type !== "tool_result",
8989
)
9090

91-
if (otherBlocks.length > 0) {
92-
newMessages.push({
93-
role: "user",
94-
content: mapContent(otherBlocks),
95-
})
96-
}
97-
91+
// Tool results must come first to maintain protocol: tool_use -> tool_result -> user
9892
for (const block of toolResultBlocks) {
9993
newMessages.push({
10094
role: "tool",
10195
tool_call_id: block.tool_use_id,
10296
content: block.content,
10397
})
10498
}
99+
100+
if (otherBlocks.length > 0) {
101+
newMessages.push({
102+
role: "user",
103+
content: mapContent(otherBlocks),
104+
})
105+
}
105106
} else {
106107
newMessages.push({
107108
role: "user",

0 commit comments

Comments
 (0)