From fdcec818149ea02a0b7f5a2a4256cdfae440b11f Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:54:05 -0400 Subject: [PATCH] Fix openrouter message transform for resuming tasks --- package.json | 2 +- src/utils/openai-format.ts | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 7215353..34b4305 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "claude-dev", "displayName": "Claude Dev", "description": "Autonomous coding agent right in your IDE, capable of creating/editing files, executing commands, and more with your permission every step of the way.", - "version": "1.5.11", + "version": "1.5.12", "icon": "icon.png", "engines": { "vscode": "^1.84.0" diff --git a/src/utils/openai-format.ts b/src/utils/openai-format.ts index 41ea418..91cd837 100644 --- a/src/utils/openai-format.ts +++ b/src/utils/openai-format.ts @@ -34,23 +34,7 @@ export function convertToOpenAiMessages( { nonToolMessages: [], toolMessages: [] } ) - // Process non-tool messages - if (nonToolMessages.length > 0) { - openAiMessages.push({ - role: "user", - content: nonToolMessages.map((part) => { - if (part.type === "image") { - return { - type: "image_url", - image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` }, - } - } - return { type: "text", text: part.text } - }), - }) - } - - // Process tool result messages + // Process tool result messages FIRST since they must follow the tool use messages let toolResultImages: Anthropic.Messages.ImageBlockParam[] = [] 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. @@ -90,6 +74,22 @@ export function convertToOpenAiMessages( })), }) } + + // Process non-tool messages + if (nonToolMessages.length > 0) { + openAiMessages.push({ + role: "user", + content: nonToolMessages.map((part) => { + if (part.type === "image") { + return { + type: "image_url", + image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` }, + } + } + return { type: "text", text: part.text } + }), + }) + } } else if (anthropicMessage.role === "assistant") { const { nonToolMessages, toolMessages } = anthropicMessage.content.reduce<{ nonToolMessages: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] @@ -106,7 +106,7 @@ export function convertToOpenAiMessages( { nonToolMessages: [], toolMessages: [] } ) - // Process non-tool messages + // Process non-tool messages FIRST let content: string | undefined if (nonToolMessages.length > 0) { content = nonToolMessages