diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index de5d47193..560f4792d 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -365,7 +365,8 @@ var alwaysThinks = regexp.MustCompile(`(?i)always engages in thinking|thinking ( // it takes: Sub2API's gpt-6.1-sol, "gpt-6.1-sol does not support reasoning // effort \"none\"; use low, medium, high, xhigh or max" (#1465). func refusesThinkingOff(b []byte) bool { - return alwaysThinks.Match(b) || mandatoryReasoning.Match(b) || thinkingOffRefused.Match(b) || + // alwaysThinks' words are matched decoded, too (#1678): 思考 escaped as \uXXXX + return alwaysThinks.Match(wordsOf(b)) || mandatoryReasoning.Match(b) || thinkingOffRefused.Match(b) || effortLevelsNamed.Match(b) && noneQuoted.Match(b) } diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index c565160f3..bb89b6b52 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -921,6 +921,8 @@ func modelRefused(status int, body []byte) bool { default: return false } + // the words are matched decoded, too (#1669) + body = wordsOf(body) return unservedWords.Match(body) && !quotaWords.Match(body) && !refusedWords.Match(body) } @@ -945,6 +947,8 @@ func modelRetired(status int, body []byte) bool { default: return false } + // the words are matched decoded, too (#1669) + body = wordsOf(body) return retiredWords.Match(body) && !quotaWords.Match(body) } @@ -1037,6 +1041,8 @@ func elsewhere(left []candidate, c candidate) []candidate { // that failed this way: the vendor was busy, out of quota or failing, or // this key or provider can't serve it — not the request itself at fault. func retryable(status int, body []byte) bool { + // the words are matched decoded, too (#1669) + body = wordsOf(body) switch { case overflowed(status, body): // a conversation too long for the model is as long on every diff --git a/internal/gateway/fallback_test.go b/internal/gateway/fallback_test.go index f18cddcee..d5c92cace 100644 --- a/internal/gateway/fallback_test.go +++ b/internal/gateway/fallback_test.go @@ -243,6 +243,26 @@ func TestModelNotInPlanRestsTheModelNotTheKey(t *testing.T) { } } +// The same words said escaped as \uXXXX (a relay whose encoder escapes +// what isn't ASCII, #1669) are read by the other deciders too: what +// rests the model alone, and what another provider may answer. +func TestEscapedWordsReadByTheOtherDeciders(t *testing.T) { + unserved := []byte(`{"error":{"code":7,"message":"\u8be5\u6a21\u578b\u4e0d\u652f\u6301\u5f53\u524d\u4ee4\u724c\u8ba1\u5212"}}`) + if !modelRefused(403, unserved) { + t.Errorf("escaped 模型不支持: modelRefused false; the key would rest for its model") + } + if !modelRefused(403, []byte(`{"error":{"message":"该模型不支持当前令牌计划"}}`)) { + t.Errorf("the same refusal unescaped: modelRefused false") + } + quota := []byte(`{"error":{"message":"\u989d\u5ea6\u5df2\u7528\u5b8c\uff0c\u8bf7\u660e\u5929\u518d\u8bd5","type":"invalid_request_error"}}`) + if !retryable(400, quota) { + t.Errorf("escaped 额度已用完: retryable false; the next decision model isn't asked") + } + if !retryable(400, []byte(`{"error":{"message":"额度已用完,请明天再试","type":"invalid_request_error"}}`)) { + t.Errorf("the same refusal unescaped: retryable false") + } +} + // Two ChatGPT accounts ticked: the one Codex is signed in to is out of // quota, so the request goes to the saved one, signed with its own tokens. func TestSubscriptionAccountsTakeOver(t *testing.T) { diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index 9a9a91e86..5a1459dc5 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -711,6 +711,23 @@ func TestAnthropicPassthroughMiniMaxArkThinkingOffRefused(t *testing.T) { } } +// The same refusal, escaped as \uXXXX the way a relay whose encoder +// escapes what isn't ASCII sends it (#1669): asked again all the same. +func TestAnthropicPassthroughAlwaysThinksInEscapedChinese(t *testing.T) { + f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`} + f.refuse = func(b []byte) (int, string) { + if bytes.Contains(b, []byte(`"disabled"`)) { + return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"\u8be5\u6a21\u578b\u59cb\u7ec8\u652f\u6301\u601d\u8003\uff0c\u4e0d\u53ef\u5173\u95ed"}}` + } + return 0, "" + } + setup(t, provider.Anthropic, f) + code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"title?"}],"thinking":{"type":"disabled"}}`) + if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"title?"`)) { + t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got) + } +} + func TestOpenRouterMandatoryReasoningRetries(t *testing.T) { const refusal = `{"error":{"message":"OpenRouter: Reasoning is mandatory for this endpoint and cannot be disabled.","type":"invalid_request_error"},"type":"error"}` for _, tc := range []struct {