Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 266
fix: improve Claude message conversion#201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
68a06d3
fix: refactor tool result content normalization in FormatConverter
bbbugg 45b6de5
fix: refactor system message extraction and handling in FormatConverter
bbbugg 054ae65
fix: preserve falsy Claude tool results
bbbugg 49b849c
fix: preserve zero initial auth index
bbbugg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2117,15 +2117,46 @@ class FormatConverter { | ||
| } | ||
| } | ||
| // Extract system message (Claude uses a separate 'system' field) | ||
| const appendSystemContent = content => { | ||
| let text = ""; | ||
| if (typeof content === "string") { | ||
| text = content; | ||
| } else if (Array.isArray(content)) { | ||
| text = content | ||
| .map(block => { | ||
| if (typeof block === "string") return block; | ||
| if (block && block.type === "text") return block.text || ""; | ||
| return block?.text || ""; | ||
| }) | ||
| .filter(Boolean) | ||
| .join("\n"); | ||
| } else if (content && typeof content === "object") { | ||
| text = content.text || ""; | ||
| } | ||
| if (!text) return; | ||
| if (systemInstruction) { | ||
| systemInstruction.parts[0].text = `${systemInstruction.parts[0].text}\n${text}`; | ||
| } else { | ||
| systemInstruction = { | ||
| parts: [{ text }], | ||
| role: "system", | ||
| }; | ||
| } | ||
| }; | ||
| // Extract system messages into Gemini systemInstruction. | ||
| if (claudeBody.system) { | ||
| const systemContent = Array.isArray(claudeBody.system) | ||
| ? claudeBody.system.map(block => (typeof block === "string" ? block : block.text || "")).join("\n") | ||
| : claudeBody.system; | ||
| systemInstruction = { | ||
| parts: [{ text: systemContent }], | ||
| role: "system", | ||
| }; | ||
| appendSystemContent(claudeBody.system); | ||
| } | ||
| if (Array.isArray(claudeBody.messages)) { | ||
| for (const message of claudeBody.messages) { | ||
| if (message.role === "system") { | ||
| appendSystemContent(message.content); | ||
| } | ||
| } | ||
| } | ||
| // Buffer for accumulating consecutive tool result parts | ||
| @@ -2141,37 +2172,54 @@ class FormatConverter { | ||
| } | ||
| }; | ||
| const ensureGeminiFunctionResponseObject = value => { | ||
| if (typeof value === "object" && value !== null && !Array.isArray(value)) { | ||
| return value; | ||
| } | ||
| return { result: value }; | ||
| }; | ||
| const normalizeClaudeToolResultContent = content => { | ||
| if (typeof content === "string") { | ||
| try { | ||
| return ensureGeminiFunctionResponseObject(JSON.parse(content)); | ||
| } catch { | ||
| return { result: content }; | ||
| } | ||
| } | ||
| if (Array.isArray(content)) { | ||
| const textParts = content | ||
| .filter(c => c && c.type === "text") | ||
| .map(c => c.text || "") | ||
| .join("\n"); | ||
| if (textParts.length > 0) { | ||
| try { | ||
| return ensureGeminiFunctionResponseObject(JSON.parse(textParts)); | ||
| } catch { | ||
| return { result: textParts }; | ||
| } | ||
| } | ||
| return { result: content }; | ||
| } | ||
| return ensureGeminiFunctionResponseObject(content ?? { result: "" }); | ||
| }; | ||
| // Convert Claude messages to Google format | ||
| for (const message of claudeBody.messages) { | ||
| if (message.role === "system") continue; | ||
| const googleParts = []; | ||
| // Handle tool_result role (Claude's function response) | ||
| if (message.role === "user" && Array.isArray(message.content)) { | ||
| const toolResults = message.content.filter(block => block.type === "tool_result"); | ||
| if (toolResults.length > 0) { | ||
| for (const toolResult of toolResults) { | ||
| let responseContent; | ||
| if (typeof toolResult.content === "string") { | ||
| try { | ||
| responseContent = JSON.parse(toolResult.content); | ||
| } catch (e) { | ||
| /* eslint-disable-line no-unused-vars */ | ||
| responseContent = { result: toolResult.content }; | ||
| } | ||
| } else if (Array.isArray(toolResult.content)) { | ||
| // Handle array content (text blocks, etc.) | ||
| const textParts = toolResult.content | ||
| .filter(c => c.type === "text") | ||
| .map(c => c.text) | ||
| .join("\n"); | ||
| try { | ||
| responseContent = JSON.parse(textParts); | ||
| } catch { | ||
| responseContent = { result: textParts }; | ||
| } | ||
| } else { | ||
| responseContent = toolResult.content || { result: "" }; | ||
| } | ||
| const responseContent = normalizeClaudeToolResultContent(toolResult.content); | ||
| // Resolve function name using the map | ||
| const toolUseId = toolResult.tool_use_id; | ||
| @@ -2195,12 +2243,11 @@ class FormatConverter { | ||
| // Process non-tool_result content in the same message | ||
| const otherContent = message.content.filter(block => block.type !== "tool_result"); | ||
| if (otherContent.length > 0) { | ||
| flushToolParts(); | ||
| for (const block of otherContent) { | ||
| if (block.type === "text") { | ||
| googleParts.push({ text: block.text }); | ||
| pendingToolParts.push({ text: block.text }); | ||
| } else if (block.type === "image") { | ||
| googleParts.push({ | ||
| pendingToolParts.push({ | ||
| inlineData: { | ||
| data: block.source.data, | ||
| mimeType: block.source.media_type, | ||
bbbugg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.