mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 20:31:37 -05:00
Fix order of tool results and user images for openrouter
This commit is contained in:
@@ -158,10 +158,11 @@ export class OpenRouterHandler implements ApiHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process tool result messages
|
// Process tool result messages
|
||||||
|
let toolResultImages: Anthropic.Messages.ImageBlockParam[] = []
|
||||||
toolMessages.forEach((toolMessage) => {
|
toolMessages.forEach((toolMessage) => {
|
||||||
// The Anthropic SDK allows tool results to be a string or an array of text and image blocks, enabling rich and structured content. In contrast, the OpenAI SDK only supports tool results as a single string, so we map the Anthropic tool result parts into one concatenated string to maintain compatibility.
|
// The Anthropic SDK allows tool results to be a string or an array of text and image blocks, enabling rich and structured content. In contrast, the OpenAI SDK only supports tool results as a single string, so we map the Anthropic tool result parts into one concatenated string to maintain compatibility.
|
||||||
let content: string
|
let content: string
|
||||||
let images: Anthropic.Messages.ImageBlockParam[] = []
|
|
||||||
if (typeof toolMessage.content === "string") {
|
if (typeof toolMessage.content === "string") {
|
||||||
content = toolMessage.content
|
content = toolMessage.content
|
||||||
} else {
|
} else {
|
||||||
@@ -169,7 +170,7 @@ export class OpenRouterHandler implements ApiHandler {
|
|||||||
toolMessage.content
|
toolMessage.content
|
||||||
?.map((part) => {
|
?.map((part) => {
|
||||||
if (part.type === "image") {
|
if (part.type === "image") {
|
||||||
images.push(part)
|
toolResultImages.push(part)
|
||||||
return "(see following user message for image)"
|
return "(see following user message for image)"
|
||||||
}
|
}
|
||||||
return part.text
|
return part.text
|
||||||
@@ -181,17 +182,21 @@ export class OpenRouterHandler implements ApiHandler {
|
|||||||
tool_call_id: toolMessage.tool_use_id,
|
tool_call_id: toolMessage.tool_use_id,
|
||||||
content: content,
|
content: content,
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// If tool results contain images, send as a separate user message
|
// If tool results contain images, send as a separate user message
|
||||||
if (images.length > 0) {
|
// I ran into an issue where if I gave feedback for one of many tool uses, the request would fail.
|
||||||
|
// "Messages following `tool_use` blocks must begin with a matching number of `tool_result` blocks."
|
||||||
|
// Therefore we need to send these images after the tool result messages
|
||||||
|
if (toolResultImages.length > 0) {
|
||||||
openAiMessages.push({
|
openAiMessages.push({
|
||||||
role: "user",
|
role: "user",
|
||||||
content: images.map((part) => ({
|
content: toolResultImages.map((part) => ({
|
||||||
type: "image_url",
|
type: "image_url",
|
||||||
image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` },
|
image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` },
|
||||||
})),
|
})),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
|
||||||
} else if (anthropicMessage.role === "assistant") {
|
} else if (anthropicMessage.role === "assistant") {
|
||||||
const { nonToolMessages, toolMessages } = anthropicMessage.content.reduce<{
|
const { nonToolMessages, toolMessages } = anthropicMessage.content.reduce<{
|
||||||
nonToolMessages: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[]
|
nonToolMessages: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[]
|
||||||
|
|||||||
Reference in New Issue
Block a user