fix: strip Claude Code attribution from non-Anthropic translations

This commit is contained in:
Mad Wiki
2026-05-17 04:21:53 +08:00
parent 53d1fd6c5c
commit d606faa99c
11 changed files with 166 additions and 7 deletions
@@ -9,6 +9,7 @@ import (
"strings"
"github.com/router-for-me/CLIProxyAPI/v7/internal/thinking"
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
@@ -103,7 +104,7 @@ func ConvertClaudeRequestToOpenAI(modelName string, inputRawJSON []byte, stream
hasSystemContent := false
if system := root.Get("system"); system.Exists() {
if system.Type == gjson.String {
if system.String() != "" {
if system.String() != "" && !util.IsClaudeCodeAttributionSystemText(system.String()) {
oldSystem := []byte(`{"type":"text","text":""}`)
oldSystem, _ = sjson.SetBytes(oldSystem, "text", system.String())
systemMsgJSON, _ = sjson.SetRawBytes(systemMsgJSON, "content.-1", oldSystem)
@@ -334,7 +335,7 @@ func convertClaudeContentPart(part gjson.Result) (string, bool) {
switch partType {
case "text":
text := part.Get("text").String()
if strings.TrimSpace(text) == "" {
if strings.TrimSpace(text) == "" || util.IsClaudeCodeAttributionSystemText(text) {
return "", false
}
textContent := []byte(`{"type":"text","text":""}`)
@@ -696,3 +696,28 @@ func TestConvertClaudeRequestToOpenAI_AssistantThinkingToolUseThinkingSplit(t *t
t.Fatalf("Expected reasoning_content %q, got %q", "t1\n\nt2", got)
}
}
func TestConvertClaudeRequestToOpenAI_StripsClaudeCodeAttribution(t *testing.T) {
inputJSON := []byte(`{
"model": "claude-sonnet-4-5",
"system": [
{"type": "text", "text": "x-anthropic-billing-header: cc_version=2.1.63.abc; cc_entrypoint=cli; cch=12345;"},
{"type": "text", "text": "User system prompt"}
],
"messages": [{"role": "user", "content": [{"type": "text", "text": "hi"}]}]
}`)
output := ConvertClaudeRequestToOpenAI("gpt-5", inputJSON, false)
messages := gjson.GetBytes(output, "messages").Array()
if len(messages) == 0 || messages[0].Get("role").String() != "system" {
t.Fatalf("Expected first message to be system, got: %s", gjson.GetBytes(output, "messages").Raw)
}
content := messages[0].Get("content").Array()
if len(content) != 1 {
t.Fatalf("Expected 1 system content item after attribution strip, got %d: %s", len(content), messages[0].Get("content").Raw)
}
if got := content[0].Get("text").String(); got != "User system prompt" {
t.Fatalf("Unexpected system content: %q", got)
}
}