Unified · Split
| @@ -11,6 +11,7 @@ import ( · 10 unmodified lines | ||
| 11 | 11 | "strings" |
| 12 | 12 | "time" |
| 13 | 13 | |
| 14 | + sigcompat "github.com/router-for-me/CLIProxyAPI/v7/internal/signature" | |
| 14 | 15 | translatorcommon "github.com/router-for-me/CLIProxyAPI/v7/internal/translator/common" |
| 15 | 16 | "github.com/tidwall/gjson" |
| 16 | 17 | "github.com/tidwall/sjson" |
| @@ -116,6 +117,13 @@ func ConvertClaudeResponseToGemini(_ context.Context, modelName string, original · 99 unmodified lines | ||
| 116 | 117 | } |
| 117 | 118 | (*param).(*ConvertAnthropicResponseToGeminiParams).ToolUseIDs[idx] = toolID |
| 118 | 119 | } |
| 120 | + } else if cb.Get("type").String() == "thinking" { | |
| 121 | + if sig := cb.Get("signature"); sig.Exists() && sig.String() != "" { | |
| 122 | + thinkingPart := []byte(`{"thought":true,"thoughtSignature":""}`) | |
| 123 | + thinkingPart, _ = sjson.SetBytes(thinkingPart, "thoughtSignature", sigcompat.GeminiReplaySignatureOrBypass(sig.String(), sigcompat.SignatureBlockKindGeminiModelPart)) | |
| 124 | + template, _ = sjson.SetRawBytes(template, "candidates.0.content.parts.-1", thinkingPart) | |
| 125 | + return [][]byte{template} | |
| 126 | + } | |
| 119 | 127 | } |
| 120 | 128 | } |
| 121 | 129 | return [][]byte{} |
| @@ -140,6 +148,12 @@ func ConvertClaudeResponseToGemini(_ context.Context, modelName string, original · 18 unmodified lines | ||
| 140 | 148 | thinkingPart, _ = sjson.SetBytes(thinkingPart, "text", text.String()) |
| 141 | 149 | template, _ = sjson.SetRawBytes(template, "candidates.0.content.parts.-1", thinkingPart) |
| 142 | 150 | } |
| 151 | + case "signature_delta": | |
| 152 | + if sig := delta.Get("signature"); sig.Exists() && sig.String() != "" { | |
| 153 | + thinkingPart := []byte(`{"thought":true,"thoughtSignature":""}`) | |
| 154 | + thinkingPart, _ = sjson.SetBytes(thinkingPart, "thoughtSignature", sigcompat.GeminiReplaySignatureOrBypass(sig.String(), sigcompat.SignatureBlockKindGeminiModelPart)) | |
| 155 | + template, _ = sjson.SetRawBytes(template, "candidates.0.content.parts.-1", thinkingPart) | |
| 156 | + } | |
| 143 | 157 | case "input_json_delta": |
| 144 | 158 | // Tool use input delta - accumulate partial_json by index for later assembly at content_block_stop |
| 145 | 159 | idx := int(root.Get("index").Int()) |
| @@ -376,6 +390,12 @@ func ConvertClaudeResponseToGeminiNonStream(_ context.Context, modelName string, · 230 unmodified lines | ||
| 376 | 390 | } |
| 377 | 391 | newParam.ToolUseIDs[idx] = toolID |
| 378 | 392 | } |
| 393 | + } else if cb.Get("type").String() == "thinking" { | |
| 394 | + if sig := cb.Get("signature"); sig.Exists() && sig.String() != "" { | |
| 395 | + partJSON := []byte(`{"thought":true,"thoughtSignature":""}`) | |
| 396 | + partJSON, _ = sjson.SetBytes(partJSON, "thoughtSignature", sigcompat.GeminiReplaySignatureOrBypass(sig.String(), sigcompat.SignatureBlockKindGeminiModelPart)) | |
| 397 | + allParts = append(allParts, partJSON) | |
| 398 | + } | |
| 379 | 399 | } |
| 380 | 400 | } |
| 381 | 401 | continue |
| @@ -399,6 +419,12 @@ func ConvertClaudeResponseToGeminiNonStream(_ context.Context, modelName string, · 17 unmodified lines | ||
| 399 | 419 | partJSON, _ = sjson.SetBytes(partJSON, "text", text.String()) |
| 400 | 420 | allParts = append(allParts, partJSON) |
| 401 | 421 | } |
| 422 | + case "signature_delta": | |
| 423 | + if sig := delta.Get("signature"); sig.Exists() && sig.String() != "" { | |
| 424 | + partJSON := []byte(`{"thought":true,"thoughtSignature":""}`) | |
| 425 | + partJSON, _ = sjson.SetBytes(partJSON, "thoughtSignature", sigcompat.GeminiReplaySignatureOrBypass(sig.String(), sigcompat.SignatureBlockKindGeminiModelPart)) | |
| 426 | + allParts = append(allParts, partJSON) | |
| 427 | + } | |
| 402 | 428 | case "input_json_delta": |
| 403 | 429 | // accumulate args partial_json for this index |
| 404 | 430 | idx := int(root.Get("index").Int()) |
| @@ -535,6 +561,7 @@ func consolidateParts(parts [][]byte) [][]byte { · 130 unmodified lines | ||
| 535 | 561 | var consolidated [][]byte |
| 536 | 562 | var currentTextPart strings.Builder |
| 537 | 563 | var currentThoughtPart strings.Builder |
| 564 | + var currentThoughtSignature string | |
| 538 | 565 | var hasText, hasThought bool |
| 539 | 566 | |
| 540 | 567 | flushText := func() { |
| @@ -550,11 +577,15 @@ func consolidateParts(parts [][]byte) [][]byte { · 9 unmodified lines | ||
| 550 | 577 | |
| 551 | 578 | flushThought := func() { |
| 552 | 579 | // Flush accumulated thinking content to the consolidated parts array |
| 553 | − if hasThought && currentThoughtPart.Len() > 0 { | |
| 580 | + if hasThought && (currentThoughtPart.Len() > 0 || currentThoughtSignature != "") { | |
| 554 | 581 | thoughtPartJSON := []byte(`{"thought":true,"text":""}`) |
| 555 | 582 | thoughtPartJSON, _ = sjson.SetBytes(thoughtPartJSON, "text", currentThoughtPart.String()) |
| 583 | + if currentThoughtSignature != "" { | |
| 584 | + thoughtPartJSON, _ = sjson.SetBytes(thoughtPartJSON, "thoughtSignature", currentThoughtSignature) | |
| 585 | + } | |
| 556 | 586 | consolidated = append(consolidated, thoughtPartJSON) |
| 557 | 587 | currentThoughtPart.Reset() |
| 588 | + currentThoughtSignature = "" | |
| 558 | 589 | hasThought = false |
| 559 | 590 | } |
| 560 | 591 | } |
| @@ -578,6 +609,10 @@ func consolidateParts(parts [][]byte) [][]byte { · 17 unmodified lines | ||
| 578 | 609 | currentThoughtPart.WriteString(text.String()) |
| 579 | 610 | hasThought = true |
| 580 | 611 | } |
| 612 | + if sig := part.Get("thoughtSignature"); sig.Exists() && sig.Type == gjson.String && sig.String() != "" { | |
| 613 | + currentThoughtSignature = sig.String() | |
| 614 | + hasThought = true | |
| 615 | + } | |
| 581 | 616 | } else if text := part.Get("text"); text.Exists() && text.Type == gjson.String { |
| 582 | 617 | // This is a regular text part - flush any pending thought first |
| 583 | 618 | flushThought() // Flush any pending thought first |
| @@ -51,3 +51,116 @@ func TestConvertClaudeResponseToGeminiNonStreamPreservesToolUseID(t *testing.T) · 50 unmodified lines | ||
| 51 | 51 | t.Fatalf("expected functionCall.id %q, got %q; chunk=%s", "toolu_gateway", got, string(out)) |
| 52 | 52 | } |
| 53 | 53 | } |
| 54 | + | |
| 55 | +func TestConvertClaudeResponseToGemini_StreamThinkingSignature(t *testing.T) { | |
| 56 | + const validGeminiSignature = "EjQKMgEMOdbHO0Gd+c9Mxk4ELwPGbpCEcp2mFfYYLix2UVtBH3fL8GECc4+JITVnHF4qZDsA" | |
| 57 | + | |
| 58 | + tests := []struct { | |
| 59 | + name string | |
| 60 | + signature string | |
| 61 | + wantSignature string | |
| 62 | + }{ | |
| 63 | + { | |
| 64 | + name: "foreign claude signature maps to bypass sentinel", | |
| 65 | + signature: "foreign_claude_sig_123", | |
| 66 | + wantSignature: "skip_thought_signature_validator", | |
| 67 | + }, | |
| 68 | + { | |
| 69 | + name: "preserves valid gemini signature", | |
| 70 | + signature: "gemini#" + validGeminiSignature, | |
| 71 | + wantSignature: validGeminiSignature, | |
| 72 | + }, | |
| 73 | + } | |
| 74 | + | |
| 75 | + for _, tt := range tests { | |
| 76 | + t.Run(tt.name, func(t *testing.T) { | |
| 77 | + ctx := context.Background() | |
| 78 | + var param any | |
| 79 | + | |
| 80 | + chunks := [][]byte{ | |
| 81 | + []byte(`data: {"type":"message_start","message":{"id":"msg_123","model":"claude-3-7-sonnet-20250219"}}`), | |
| 82 | + []byte(`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`), | |
| 83 | + []byte(`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"thinking text"}}`), | |
| 84 | + []byte(`data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"` + tt.signature + `"}}`), | |
| 85 | + []byte(`data: {"type":"content_block_stop","index":0}`), | |
| 86 | + []byte(`data: {"type":"content_block_start","index":1,"content_block":{"type":"text","text":""}}`), | |
| 87 | + []byte(`data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"final answer"}}`), | |
| 88 | + []byte(`data: {"type":"content_block_stop","index":1}`), | |
| 89 | + []byte(`data: {"type":"message_stop"}`), | |
| 90 | + } | |
| 91 | + | |
| 92 | + var emittedParts []gjson.Result | |
| 93 | + for _, chunk := range chunks { | |
| 94 | + out := ConvertClaudeResponseToGemini(ctx, "gemini-2.5-pro", nil, nil, chunk, ¶m) | |
| 95 | + for _, c := range out { | |
| 96 | + parts := gjson.GetBytes(c, "candidates.0.content.parts").Array() | |
| 97 | + emittedParts = append(emittedParts, parts...) | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + var foundSignature string | |
| 102 | + for _, p := range emittedParts { | |
| 103 | + if p.Get("thought").Bool() && p.Get("thoughtSignature").Exists() { | |
| 104 | + foundSignature = p.Get("thoughtSignature").String() | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + if foundSignature != tt.wantSignature { | |
| 109 | + t.Fatalf("expected thoughtSignature %q, got %q", tt.wantSignature, foundSignature) | |
| 110 | + } | |
| 111 | + }) | |
| 112 | + } | |
| 113 | +} | |
| 114 | + | |
| 115 | +func TestConvertClaudeResponseToGeminiNonStream_ThinkingSignature(t *testing.T) { | |
| 116 | + const validGeminiSignature = "EjQKMgEMOdbHO0Gd+c9Mxk4ELwPGbpCEcp2mFfYYLix2UVtBH3fL8GECc4+JITVnHF4qZDsA" | |
| 117 | + | |
| 118 | + tests := []struct { | |
| 119 | + name string | |
| 120 | + signature string | |
| 121 | + wantSignature string | |
| 122 | + }{ | |
| 123 | + { | |
| 124 | + name: "foreign claude signature maps to bypass sentinel", | |
| 125 | + signature: "foreign_claude_sig_123", | |
| 126 | + wantSignature: "skip_thought_signature_validator", | |
| 127 | + }, | |
| 128 | + { | |
| 129 | + name: "preserves valid gemini signature", | |
| 130 | + signature: "gemini#" + validGeminiSignature, | |
| 131 | + wantSignature: validGeminiSignature, | |
| 132 | + }, | |
| 133 | + } | |
| 134 | + | |
| 135 | + for _, tt := range tests { | |
| 136 | + t.Run(tt.name, func(t *testing.T) { | |
| 137 | + ctx := context.Background() | |
| 138 | + raw := []byte(strings.Join([]string{ | |
| 139 | + `data: {"type":"message_start","message":{"id":"msg_123","model":"claude-3-7-sonnet-20250219"}}`, | |
| 140 | + `data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`, | |
| 141 | + `data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"thinking text"}}`, | |
| 142 | + `data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"` + tt.signature + `"}}`, | |
| 143 | + `data: {"type":"content_block_stop","index":0}`, | |
| 144 | + `data: {"type":"content_block_start","index":1,"content_block":{"type":"text","text":""}}`, | |
| 145 | + `data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"final answer"}}`, | |
| 146 | + `data: {"type":"content_block_stop","index":1}`, | |
| 147 | + `data: {"type":"message_stop"}`, | |
| 148 | + }, "\n")) | |
| 149 | + | |
| 150 | + out := ConvertClaudeResponseToGeminiNonStream(ctx, "gemini-2.5-pro", nil, nil, raw, nil) | |
| 151 | + | |
| 152 | + thoughtPart := gjson.GetBytes(out, "candidates.0.content.parts.0") | |
| 153 | + if !thoughtPart.Get("thought").Bool() || thoughtPart.Get("text").String() != "thinking text" { | |
| 154 | + t.Fatalf("expected thought part with text 'thinking text', got %s", thoughtPart.Raw) | |
| 155 | + } | |
| 156 | + if got := thoughtPart.Get("thoughtSignature").String(); got != tt.wantSignature { | |
| 157 | + t.Fatalf("expected thoughtSignature %q, got %q", tt.wantSignature, got) | |
| 158 | + } | |
| 159 | + | |
| 160 | + textPart := gjson.GetBytes(out, "candidates.0.content.parts.1") | |
| 161 | + if textPart.Get("text").String() != "final answer" { | |
| 162 | + t.Fatalf("expected text part 'final answer', got %s", textPart.Raw) | |
| 163 | + } | |
| 164 | + }) | |
| 165 | + } | |
| 166 | +} | |
| @@ -3,9 +3,50 @@ package claude · 2 unmodified lines | ||
| 3 | 3 | import ( |
| 4 | 4 | "testing" |
| 5 | 5 | |
| 6 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/signature" | |
| 6 | 7 | "github.com/tidwall/gjson" |
| 7 | 8 | ) |
| 8 | 9 | |
| 10 | +const capturedGeminiThinkingSignature = "EjQKMgEMOdbHO0Gd+c9Mxk4ELwPGbpCEcp2mFfYYLix2UVtBH3fL8GECc4+JITVnHF4qZDsA" | |
| 11 | + | |
| 12 | +func TestConvertClaudeRequestToGeminiWithCompat_SignatureCompatibility(t *testing.T) { | |
| 13 | + tests := []struct { | |
| 14 | + name string | |
| 15 | + signature string | |
| 16 | + wantSignature string | |
| 17 | + }{ | |
| 18 | + { | |
| 19 | + name: "preserves valid gemini signature", | |
| 20 | + signature: "gemini#" + capturedGeminiThinkingSignature, | |
| 21 | + wantSignature: capturedGeminiThinkingSignature, | |
| 22 | + }, | |
| 23 | + { | |
| 24 | + name: "foreign claude signature maps to bypass sentinel", | |
| 25 | + signature: "claude#opaque-signature-12345", | |
| 26 | + wantSignature: signature.GeminiSkipThoughtSignatureValidator, | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + name: "empty signature maps to bypass sentinel", | |
| 30 | + signature: "", | |
| 31 | + wantSignature: signature.GeminiSkipThoughtSignatureValidator, | |
| 32 | + }, | |
| 33 | + } | |
| 34 | + | |
| 35 | + for _, tt := range tests { | |
| 36 | + t.Run(tt.name, func(t *testing.T) { | |
| 37 | + payload := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"reason","signature":"` + tt.signature + `"}]}]}`) | |
| 38 | + withCompat := ConvertClaudeRequestToGeminiWithCompat("deepseek-v4", payload, false) | |
| 39 | + part := gjson.GetBytes(withCompat, "contents.0.parts.0") | |
| 40 | + if !part.Get("thought").Bool() || part.Get("text").String() != "reason" { | |
| 41 | + t.Fatalf("compat translation missing thought part: %s", withCompat) | |
| 42 | + } | |
| 43 | + if got := part.Get("thoughtSignature").String(); got != tt.wantSignature { | |
| 44 | + t.Fatalf("thoughtSignature = %q, want %q; output: %s", got, tt.wantSignature, withCompat) | |
| 45 | + } | |
| 46 | + }) | |
| 47 | + } | |
| 48 | +} | |
| 49 | + | |
| 9 | 50 | func TestConvertClaudeRequestToGeminiWithCompatPreservesEmptyThinking(t *testing.T) { |
| 10 | 51 | payload := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"reason","signature":""}]}]}`) |
| 11 | 52 | |
| @@ -19,7 +60,7 @@ func TestConvertClaudeRequestToGeminiWithCompatPreservesEmptyThinking(t *testing · 7 unmodified lines | ||
| 19 | 60 | if !part.Get("thought").Bool() || part.Get("text").String() != "reason" { |
| 20 | 61 | t.Fatalf("compat translation missing thought part: %s", withCompat) |
| 21 | 62 | } |
| 22 | − if !part.Get("thoughtSignature").Exists() || part.Get("thoughtSignature").String() != "" { | |
| 23 | − t.Fatalf("compat translation did not preserve empty signature: %s", withCompat) | |
| 63 | + if !part.Get("thoughtSignature").Exists() || part.Get("thoughtSignature").String() != signature.GeminiSkipThoughtSignatureValidator { | |
| 64 | + t.Fatalf("compat translation did not preserve bypass signature: %s", withCompat) | |
| 24 | 65 | } |
| 25 | 66 | } |
| @@ -9,6 +9,7 @@ import ( · 8 unmodified lines | ||
| 9 | 9 | "strings" |
| 10 | 10 | |
| 11 | 11 | "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" |
| 12 | + sigcompat "github.com/router-for-me/CLIProxyAPI/v7/internal/signature" | |
| 12 | 13 | translatorcommon "github.com/router-for-me/CLIProxyAPI/v7/internal/translator/common" |
| 13 | 14 | "github.com/router-for-me/CLIProxyAPI/v7/internal/translator/gemini/common" |
| 14 | 15 | "github.com/router-for-me/CLIProxyAPI/v7/internal/util" |
| @@ -119,7 +120,8 @@ func convertClaudeRequestToGemini(modelName string, inputRawJSON []byte, _ bool, · 104 unmodified lines | ||
| 119 | 120 | } |
| 120 | 121 | part := []byte(`{"text":"","thought":true,"thoughtSignature":""}`) |
| 121 | 122 | part, _ = sjson.SetBytes(part, "text", contentResult.Get("thinking").String()) |
| 122 | − part, _ = sjson.SetBytes(part, "thoughtSignature", contentResult.Get("signature").String()) | |
| 123 | + signature := sigcompat.GeminiReplaySignatureOrBypass(contentResult.Get("signature").String(), sigcompat.SignatureBlockKindGeminiModelPart) | |
| 124 | + part, _ = sjson.SetBytes(part, "thoughtSignature", signature) | |
| 123 | 125 | partItems = append(partItems, part) |
| 124 | 126 | |
| 125 | 127 | case "tool_use": |