From f94ece2ffa184e424f363b65ec7af308fdaf0e41 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 19:35:50 +0800 Subject: [PATCH] catalog: a relay's 404 "no channel for this model" on one API sends the model to the next too (equinox0253 on X) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2c05df52b sent a model on to the relay's next API when New API answers 503 "No available channel ... under group" on the one asked. WandAPI answers 404 for the same case, so a GPT asked on its Anthropic URL stayed a 404. catalog.NoChannel already took a 404 but only by the 503's words; it now also takes a 404 whose JSON error carries New API's model_not_found code (code or type) with a message that names a channel or a group, in English or Chinese, and the Chinese 没有可用渠道. A real 404 stays one: a wrong base URL ("404 page not found", HTML, "Invalid URL"), Anthropic's not_found_error, and OpenAI's "The model X does not exist or you do not have access to it" (same code, no channel or group named) reach the client and nothing is remembered. Fixture basis: New API's source (QuantumNous/new-api, middleware/distributor.go and service/channel_select.go) gives no-channel as 503 with ErrorCodeModelNotFound and "No available channel for model {{.Model}} under group {{.Group}} (distributor)". I did not find WandAPI's 404 body: the 404 fixtures are built from New API's code and message template, not a captured WandAPI reply. If WandAPI's wording names neither a channel nor a group, this will not match; the reporter's body would settle it. Run (mote, -tags nogui): go vet catalog+gateway; go test -run NoChannel catalog and gateway: ok. TestNoChannel and TestNoChannelAs404FromAFork (fallback taken; missing-model and wrong-base-URL 404s not) are red with the 404 branch switched off by hand. Not tried: a real WandAPI account. --- internal/catalog/catalog.go | 29 ++++++++++++- internal/catalog/catalog_test.go | 14 +++++++ internal/gateway/newapi_no_channel_test.go | 49 ++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index ba57308a2..95a7b5812 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -2166,7 +2166,34 @@ func NoChannel(status int, body []byte) bool { return false } msg := strings.ToLower(string(Words(body))) - return strings.Contains(msg, "no available channel") || strings.Contains(msg, "无可用渠道") + for _, phrase := range []string{"no available channel", "无可用渠道", "没有可用渠道"} { + if strings.Contains(msg, phrase) { + return true + } + } + // a relay that answers 404 for the same case (equinox0253 on X: WandAPI, + // 503 on New API): New API gives that refusal the code model_not_found, + // and a fork keeps the code when it changes the status. The code alone + // is not enough: it is also what a model that does not exist is told, + // so the message must name a channel or a group, the relay's own + // routing, as a 404 for a wrong base URL or a missing model never does. + if status == 404 { + var e struct { + Error struct { + Code any `json:"code"` + Type string `json:"type"` + Message string `json:"message"` + } `json:"error"` + } + if json.Unmarshal(body, &e) == nil { + code, _ := e.Error.Code.(string) + if code == "model_not_found" || e.Error.Type == "model_not_found" { + m := strings.ToLower(e.Error.Message) + return strings.Contains(m, "channel") || strings.Contains(m, "渠道") || strings.Contains(m, "分组") || strings.Contains(m, "under group") + } + } + } + return false } // Words is an error body as its words are matched: a JSON body that diff --git a/internal/catalog/catalog_test.go b/internal/catalog/catalog_test.go index d8d753b01..dd98fe4be 100644 --- a/internal/catalog/catalog_test.go +++ b/internal/catalog/catalog_test.go @@ -489,6 +489,20 @@ func TestNoChannel(t *testing.T) { {503, `{"error":{"message":"当前分组 default 下对于模型 gpt-6.1-sol 无可用渠道 (request id: 2026)","type":"new_api_error","code":"model_not_found"}}`, true}, {503, `{"error":{"message":"upstream overloaded"}}`, false}, {429, `No available channel for model x`, false}, + // a fork answering 404 where New API answers 503 (equinox0253 on X, + // WandAPI): New API's model_not_found code, the message naming + // the group or channel; shapes built from New API's message + // templates, not a captured WandAPI body + {404, `{"error":{"message":"No available channel for model gpt-6.1-sol under group default","type":"new_api_error","code":"model_not_found"}}`, true}, + {404, `{"error":{"message":"分组 default 下模型 gpt-6.1-sol 暂无渠道","type":"invalid_request_error","code":"model_not_found"}}`, true}, + {404, `{"type":"error","error":{"type":"model_not_found","message":"model gpt-6.1-sol has no channel in group vip"}}`, true}, + // a real 404 stays one: a wrong base URL, a model that does not + // exist, Anthropic's own not_found_error + {404, `404 page not found`, false}, + {404, `Not Found`, false}, + {404, `{"error":{"message":"The model gpt-9 does not exist or you do not have access to it.","type":"invalid_request_error","code":"model_not_found"}}`, false}, + {404, `{"type":"error","error":{"type":"not_found_error","message":"model: gpt-6.1-sol"}}`, false}, + {404, `{"error":{"message":"Invalid URL (POST /v1/messages)","type":"invalid_request_error","code":null}}`, false}, } { if got := NoChannel(c.status, []byte(c.body)); got != c.want { t.Errorf("NoChannel(%d, %s) = %v", c.status, c.body, got) diff --git a/internal/gateway/newapi_no_channel_test.go b/internal/gateway/newapi_no_channel_test.go index ef9839df1..d4f447c17 100644 --- a/internal/gateway/newapi_no_channel_test.go +++ b/internal/gateway/newapi_no_channel_test.go @@ -24,6 +24,10 @@ const ( type newAPI struct { mu sync.Mutex calls map[string]int + // a fork's refusal of a GPT on Anthropic, where it differs from New + // API's 503 (WandAPI answers 404) + status int + body string } func (f *newAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -42,6 +46,12 @@ func (f *newAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { } switch { case r.URL.Path == "/v1/messages" && gpt: + if f.status != 0 { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(f.status) + io.WriteString(w, f.body) + return + } refuse(newAPINoChannelClaude) case r.URL.Path != "/v1/messages" && !gpt: refuse(newAPINoChannelOpenAI) @@ -138,3 +148,42 @@ func TestNoChannelWithNothingElseIsTheError(t *testing.T) { t.Error("the only API was marked unfit") } } + +// WandAPI answers 404 where New API answers 503 (equinox0253 on X). The +// fallback follows the body's shape (New API's model_not_found code, its +// message naming the group), not the status alone: a 404 of any other kind +// is the relay's word, and nothing is remembered. +func TestNoChannelAs404FromAFork(t *testing.T) { + for _, c := range []struct { + name, body string + falls bool + }{ + {"fork no channel", `{"error":{"message":"分组 default 下模型 gpt-6.1-sol 暂无渠道","type":"new_api_error","code":"model_not_found"}}`, true}, + {"missing model", `{"error":{"message":"The model gpt-6.1-sol does not exist or you do not have access to it.","type":"invalid_request_error","code":"model_not_found"}}`, false}, + {"wrong base URL", `404 page not found`, false}, + } { + t.Run(c.name, func(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + f := &newAPI{status: 404, body: c.body} + up := httptest.NewServer(f) + t.Cleanup(up.Close) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-sol"}, + Chat: up.URL + "/v1", Anthropic: up.URL}); err != nil { + t.Fatal(err) + } + srv := New() + rec := httptest.NewRecorder() + srv.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"relay/gpt-6.1-sol","max_tokens":100,"stream":true,"messages":[{"role":"user","content":"hi"}]}`))) + if c.falls { + if rec.Code != 200 || !strings.Contains(rec.Body.String(), "gpt ok") || srv.fits("relay", "gpt-6.1-sol", provider.Anthropic) { + t.Fatalf("no fallback: %d %s", rec.Code, rec.Body.String()) + } + return + } + if rec.Code != 404 || !srv.fits("relay", "gpt-6.1-sol", provider.Anthropic) || f.count("/v1/chat/completions gpt") != 0 { + t.Fatalf("a real 404 was turned into a protocol switch: %d %s %v", rec.Code, rec.Body.String(), f.calls) + } + }) + } +}