diff --git a/internal/gateway/context_full_test.go b/internal/gateway/context_full_test.go new file mode 100644 index 000000000..61a199f4d --- /dev/null +++ b/internal/gateway/context_full_test.go @@ -0,0 +1,105 @@ +package gateway + +import ( + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// contextFull is how Kiro-backed relays say a conversation is past the +// model's window: kiro.rs (src/anthropic/handlers.rs, map_provider_error) +// and kiro2claude (error-mapper.ts) turn Kiro's +// CONTENT_LENGTH_EXCEEDS_THRESHOLD into it, as a 400 +// invalid_request_error, and a new-api relay in front of one adds its +// request id. ergou0954_54426 on Discord had it from a "kiro opus" model +// of an API-key relay in an intent-routed group: not read as an overflow, +// it went to the agent as a plain 400, which never compacted, and no other +// member was asked. +const contextFull = "Context window is full. Reduce conversation history, system prompt, or tools." + +// The words are an overflow on every request-error status a relay sends +// them with, and in a stream's error event; neither a used-up plan nor +// a fault another account of the model would answer differently. An +// ordinary 400 is still not one. +func TestContextWindowFullIsAnOverflow(t *testing.T) { + said := contextFull + " (request id: 2026110112345678901234567)" + bodies := []string{ + `{"type":"error","error":{"type":"invalid_request_error","message":"` + contextFull + `"}}`, + `{"error":{"message":"` + said + `","type":"invalid_request_error","param":"","code":null}}`, + // its sibling in kiro.rs, for one input past Kiro's own limit + `{"type":"error","error":{"type":"invalid_request_error","message":"Input is too long. Reduce the size of your messages."}}`, + } + for _, status := range []int{400, 413, 422} { + for _, b := range bodies { + if !overflowed(status, []byte(b)) { + t.Errorf("%d %s isn't an overflow", status, b) + } + if retryable(status, []byte(b)) { + t.Errorf("%d %s is asked of another account", status, b) + } + if f := failure(status, []byte(b)); f != failOther { + t.Errorf("%d %s is a %s failure", status, b, f) + } + } + } + // in a stream, as an error event: an overflow's 400, not a vendor's + // passing 502 that rests the account and is asked again + if got := streamStatus(said); got != 400 { + t.Errorf("a stream's %q stands for %d", said, got) + } + if lateRests(said) { + t.Errorf("a reply cut by %q rests the account", said) + } + for _, msg := range []string{ + `{"error":{"message":"invalid model: kiro-opus-9 (request id: 20261101)","type":"invalid_request_error"}}`, + `{"error":{"message":"model not found","type":"invalid_request_error"}}`, + `{"error":{"message":"Invalid request: messages is required","type":"invalid_request_error"}}`, + } { + if overflowed(400, []byte(msg)) { + t.Errorf("an ordinary 400 %s is taken for an overflow", msg) + } + } +} + +// In a group the member saying its context window is full is passed for +// the next, asked once and not rested, both as an error status and as an +// error event of a stream begun with 200; alone, the status tells the +// agent context_length_exceeded, so it compacts. +func TestContextWindowFullGoesToAMemberWithRoom(t *testing.T) { + said := contextFull + " (request id: 2026110112345678901234567)" + for _, full := range []reply{ + {400, "application/json", `{"error":{"message":"` + said + `","type":"invalid_request_error","param":"","code":null}}`}, + {200, "text/event-stream", "data: {\"error\":{\"message\":\"" + said + "\",\"type\":\"invalid_request_error\"}}\n\n"}, + } { + fresh(t) + relay := &scripted{replies: []reply{full}} + other := &scripted{replies: []reply{{200, "", chatOK}}} + scriptedOn(t, "r", provider.Chat, relay) + scriptedOn(t, "d", provider.Chat, other) + if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"r/m", "d/m"}, Routing: provider.Ordered}); err != nil { + t.Fatal(err) + } + s := New() + code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`) + if code != 200 || !strings.Contains(body, "hello") || relay.n != 1 || other.n != 1 { + t.Fatalf("%d %s\ngroup: %d %s (relay %d, other %d)", full.code, full.ctype, code, body, relay.n, other.n) + } + r := s.trace.routes[len(s.trace.routes)-1] + if len(r.Tries) != 2 || r.Tries[0].Fail != failOverflow || r.Tries[0].Rest != nil { + t.Fatalf("%d %s\ntries %+v", full.code, full.ctype, r.Tries) + } + + // alone, the agent is told it overflowed, asked once. (A stream's + // error event from the one candidate left reaches the agent as the + // relay sent it; that is #4675.) + if full.code != 400 { + continue + } + relay.n = 0 + code, body = postAs(t, s, "", `{"model":"r/m","messages":[{"role":"user","content":"hi"}]}`) + if code != 400 || !strings.Contains(body, "context_length_exceeded") || relay.n != 1 { + t.Fatalf("%d %s\nalone: %d %s (relay %d)", full.code, full.ctype, code, body, relay.n) + } + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 0d680e83c..51b528b67 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -8580,6 +8580,9 @@ func writeErrorSaid(w http.ResponseWriter, proto provider.Protocol, status int, // DashScope's "Range of input length should be", "token limit exceeded", // a relay's own cap on one request's input, "Input token exceed the limit" // (#2445: B.ai, under the model's window, which another member holds). +// "Context window is full" is how Kiro-backed relays (kiro.rs, +// kiro2claude) say Kiro's CONTENT_LENGTH_EXCEEDS_THRESHOLD, a 400 +// (ergou0954_54426 on Discord, through a new-api relay). // The third line is Chinese that names the input rather than the context // (#4420, MYCF711): a group member's 400 "内容太长了,请精简输入或开启新对话", // z.ai's code 1261 "Prompt 超长", iFlytek Spark's 10907 "token数量超过上限。 @@ -8588,7 +8591,7 @@ func writeErrorSaid(w http.ResponseWriter, proto provider.Protocol, status int, // 提示词, prompt…) beside its length, so "名称超出最大长度" (a field) or // "输出过长" (the reply) isn't taken; bare "token数量超过上限" isn't either, // as a day's allowance is said so too. -var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too long|input is too long|(exceeds?|exceeded|over|beyond)( the)?( model'?s?)?( maximum)? context|context (length|limit|window) (exceeded|is exceeded)|maximum context length|too many (input |prompt )?tokens|上下文(长度)?(超|过长)|超(过|出)(了)?(模型)?(的)?(最大)?上下文` + +var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too long|input is too long|(exceeds?|exceeded|over|beyond)( the)?( model'?s?)?( maximum)? context|context (length|limit|window) (exceeded|is exceeded)|context (window )?(is )?full\b|maximum context length|too many (input |prompt )?tokens|上下文(长度)?(超|过长)|超(过|出)(了)?(模型)?(的)?(最大)?上下文` + `|input token count.*exceeds the maximum|maximum prompt length is \d|reduce the length of the messages|maximum allowed input length|longer than the model'?s context length|available context size|greater than the context length|prompt token count of [\d,]+ exceeds the limit|context window exceeds limit|exceeded model token limit|configured context size|prompt too long|prompt exceeds max length|context_window_exceeded|exceeded (max |maximum )?context length|range of input length should be|token limit exceeded|input tokens? (count )?(exceeds?|exceeded) (the )?(max(imum)? )?limit` + `|(内容|输入|文本|提示词?|prompt|问题|对话|消息)(内容|文本)?(长度)?\s*(太长|过长|超长)|字数(太多|过多)` + `|(内容|输入|文本|提示词?|prompt|问题|对话|消息)[^,。,.;;!!]{0,12}超(过|出)[^,。,.;;!!]{0,12}(长度|token|令牌|字数)` +