mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-06-02 04:42:46 +08:00
fix(translator): skip empty text parts in Claude request conversion
- Updated `ConvertClaudeRequestToGemini` to ignore empty `text` entries during processing. - Added unit tests to ensure empty `text` parts are skipped correctly. Closes: #3485
This commit is contained in:
@@ -81,8 +81,12 @@ func ConvertClaudeRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
||||
contentsResult.ForEach(func(_, contentResult gjson.Result) bool {
|
||||
switch contentResult.Get("type").String() {
|
||||
case "text":
|
||||
text := contentResult.Get("text").String()
|
||||
if text == "" {
|
||||
return true
|
||||
}
|
||||
part := []byte(`{"text":""}`)
|
||||
part, _ = sjson.SetBytes(part, "text", contentResult.Get("text").String())
|
||||
part, _ = sjson.SetBytes(part, "text", text)
|
||||
contentJSON, _ = sjson.SetRawBytes(contentJSON, "parts.-1", part)
|
||||
|
||||
case "tool_use":
|
||||
|
||||
@@ -106,3 +106,29 @@ func TestConvertClaudeRequestToGemini_StripsClaudeCodeAttribution(t *testing.T)
|
||||
t.Fatalf("Claude Code attribution block was forwarded: %s", gjson.GetBytes(output, "system_instruction.parts").Raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertClaudeRequestToGemini_SkipsEmptyTextParts(t *testing.T) {
|
||||
inputJSON := []byte(`{
|
||||
"model": "claude-3-5-sonnet",
|
||||
"messages": [
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [
|
||||
{"type": "text", "text": ""},
|
||||
{"type": "text", "text": "hello"},
|
||||
{"type": "text", "text": ""}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
output := ConvertClaudeRequestToGemini("gemini-3-flash-preview", inputJSON, false)
|
||||
|
||||
parts := gjson.GetBytes(output, "contents.0.parts").Array()
|
||||
if len(parts) != 1 {
|
||||
t.Fatalf("Expected 1 part after skipping empty text, got %d: %s", len(parts), output)
|
||||
}
|
||||
if got := parts[0].Get("text").String(); got != "hello" {
|
||||
t.Fatalf("Expected part text 'hello', got '%s'", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user