diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index bb89b6b52..b24738680 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -988,6 +988,8 @@ var shapeWords = regexp.MustCompile(`(?i)failed to deserialize|unknown (item |co // shapeRefused says a request failed over its shape alone: the next // member is asked, and this one doesn't rest, as nothing is wrong with it. func shapeRefused(status int, body []byte) bool { + // the words are matched decoded, too (#1669) + body = wordsOf(body) return (status == 400 || status == 422) && shapeWords.Match(body) && !quotaWords.Match(body) && !unservedWords.Match(body) && !refusedWords.Match(body) } @@ -1095,6 +1097,8 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, status int, body [ default: return "", false } + // the words are matched decoded, too (#1669) + body = wordsOf(body) if overflowed(status, body) || failure(status, body) != failOther || quotaWords.Match(body) || unservedWords.Match(body) || refusedWords.Match(body) || provider.EdgeBlocked(body) || regionRefused(status, body) { return "", false diff --git a/internal/gateway/fallback_test.go b/internal/gateway/fallback_test.go index d5c92cace..1555c1418 100644 --- a/internal/gateway/fallback_test.go +++ b/internal/gateway/fallback_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "fmt" "io" "net/http" "net/http/httptest" @@ -263,6 +264,44 @@ func TestEscapedWordsReadByTheOtherDeciders(t *testing.T) { } } +// The rest of the deciders whose words are Chinese read them escaped as +// well: a conversation too long, a retired model or a shape refusal that +// also says the quota is spent, and a size inferred only when nothing +// else is said. Each case beside the same words unescaped. +func TestEscapedWordsReadByEveryDecider(t *testing.T) { + for _, esc := range []bool{true, false} { + say := func(s string) []byte { + if !esc { + return []byte(`{"error":{"message":"` + s + `"}}`) + } + q, _ := json.Marshal(s) + // as Python's json.dumps writes it + var b strings.Builder + for _, r := range string(q) { + if r > 127 { + fmt.Fprintf(&b, `\u%04x`, r) + } else { + b.WriteRune(r) + } + } + return []byte(`{"error":{"message":` + b.String() + `}}`) + } + if !overflowed(400, say("上下文长度超过限制")) { + t.Errorf("escaped %v: 上下文长度超过限制 not overflowed", esc) + } + if modelRetired(410, say("Model m1 has been deprecated, 额度已用完")) { + t.Errorf("escaped %v: a retirement that says the quota is spent rests the model alone", esc) + } + if shapeRefused(400, say("Unknown parameter: 'x'. 额度已用完")) { + t.Errorf("escaped %v: a shape refusal that says the quota is spent rests nobody", esc) + } + p := provider.Provider{ID: "fake", Name: "Fake", Contexts: map[string]int{"m": 100}} + if said, ok := tooLongUnsaid(p, "m", 1000, 400, say("该模型不支持当前令牌计划")); ok { + t.Errorf("escaped %v: a model the plan doesn't serve told as too long: %s", esc, said) + } + } +} + // 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.go b/internal/gateway/gateway.go index bde12522f..cd88c9288 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -5802,7 +5802,8 @@ var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too lo // overflowed says a refused request was too long for the model — a // request error (400, 413, 422) whose words say so. func overflowed(status int, body []byte) bool { - return (status == 400 || status == 413 || status == 422) && tooLong(status, string(body)) + // the words are matched decoded, too (#1669): 上下文 escaped as \uXXXX + return (status == 400 || status == 413 || status == 422) && tooLong(status, string(wordsOf(body))) } // tooLong is whether a vendor's error says the conversation no longer diff --git a/internal/gateway/wire_effort_test.go b/internal/gateway/wire_effort_test.go index 57e9edebd..c6d3b3e2f 100644 --- a/internal/gateway/wire_effort_test.go +++ b/internal/gateway/wire_effort_test.go @@ -100,6 +100,9 @@ func TestTranslatedThinkingOffRefused(t *testing.T) { "thinking.type `disabled` is not supported by this model Request id: 0217", // Sub2API's gpt-6.1-sol reads disabled as reasoning effort none (#1465) `gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max`, + // #699's Chinese, escaped as \uXXXX the way a relay's + // json.dumps sends it (#1669) + `\u8be5\u6a21\u578b\u59cb\u7ec8\u652f\u6301\u601d\u8003\uff0c\u4e0d\u53ef\u5173\u95ed`, } { t.Run(fmt.Sprint(i), func(t *testing.T) { translatedThinkingOffRefused(t, refusal) }) }