On a Coding Plan with a key Zhipu refuses, Test showed OpenAI 401 and Anthropic 401 but Responses (https://open.bigmodel.cn/api/v1) a green check, and the 5h/weekly windows showed only "Allowance unavailable". Cause: /api/v1 (Zhipu's real Responses endpoint, docs.bigmodel.cn coding-plan/tool/codex) and /api/monitor answer a refused key HTTP 200 with {"code":1000,"msg":"身份验证失败。","success":false}. probe (endpoint Test, model tests, Detect) took any 2xx as ok, and the gateway handed the 200 to Codex as a reply, so no failover and no rest. - provider.ZhipuEnveloped: a 2xx JSON, non-stream, unencoded reply from open.bigmodel.cn or api.z.ai whose body is the envelope with success:false becomes {"error":{...}} under the status Zhipu's code table gives (1000-1005 401, 1113/13xx 429, 1220 403, 121x/1261/1301 400, else 502). Anything else passes through unchanged (64KB cap, body restored). Applied in probe and in the gateway's forwardOnce. - planquota: a refused key (envelope 1000-1005 or HTTP 401 from a Zhipu/Z.ai host) says "Zhipu didn't take this key (...): it has expired, was deleted, or is mistyped; paste a current one from <keys page>", and skips the team retry. The card shows it in en, zh, zh-TW, ja, de. - docs: providers-accounts and gateway-routing rows. Run at this head (before rebase, base95e7c6bcc): - go test -timeout 20m ./internal/gateway ./internal/provider: ok (343s, 93s). New: TestZhipuResponsesRefusalNotGreen, TestZhipuEnveloped, TestZhipuRefusedKeyCard, TestZhipuResponsesRefusalIsRefusal (zhipu and zai presets). - Full go test ./...: every package ok except internal/gateway, which hit the default 10m timeout while the full GUI suite and another session's GUI tests ran beside it; alone with -timeout 20m it passed. - gofmt -l clean; GOOS=linux and GOOS=windows go vet on provider and gateway clean. - GUI: glm-quota-error (new) and opencode-go-quota-error 20/20 in chromium and webkit, five languages. The full GUI suite ran once (under load) and exited 1; each of its 16 red files was rerun alone here and on an origin/main03eb0633acopy of internal/gui: 10 pass on both; quota-count-wrap (1), quota-name-translated (1), refusal-retries-setting (2), usage-agents-hide (22), usage-fold (2) fail the same counts on both; routing-held-member fails 1 here, 2 on main. None touches quotaError's GLM line. Broken by hand: ZhipuEnveloped returning res unchanged, and zhipuEnvelope returning ok=false, turned the provider tests red; reverting the gateway line turned TestZhipuResponsesRefusalIsRefusal red; breaking the GLM regex in app.js turned glm-quota-error 0/5 in chromium. All restored. Not tried: no real Zhipu or Z.ai key, valid or expired. The refusal bytes are from a curl of /api/v1 with a bad key; the reporter's "令牌已过期或验证不正确" code is unknown (assumed 1000-1005 or HTTP 401, both handled). A valid key's quota read with a real plan was not run.
208 lines
8.5 KiB
Go
208 lines
8.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// Zhipu's replies to a key it refuses, byte for byte as open.bigmodel.cn
|
|
// answered them on 2026-10-15: 401 at the OpenAI and Anthropic endpoints,
|
|
// 200 in the platform's envelope at /api/v1 (the Responses API) and at
|
|
// /api/monitor (the plan's quota).
|
|
const (
|
|
zhipuChat401 = `{"error":{"code":"1000","message":"身份验证失败。"}}`
|
|
zhipuAnthropic401 = `{"error":{"message":"身份验证失败。","type":"1000"}}`
|
|
zhipuEnvelope1000 = `{"code":1000,"msg":"身份验证失败。","success":false}`
|
|
)
|
|
|
|
// zhipuRefuses plays the GLM Coding Plan's endpoints: "good" is served on
|
|
// each, any other key refused as Zhipu refuses it. asked is each path.
|
|
type zhipuRefuses struct {
|
|
mu sync.Mutex
|
|
asked []string
|
|
}
|
|
|
|
func (z *zhipuRefuses) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
if r.Body != nil {
|
|
io.Copy(io.Discard, r.Body)
|
|
}
|
|
z.mu.Lock()
|
|
z.asked = append(z.asked, r.URL.Host+r.URL.Path)
|
|
z.mu.Unlock()
|
|
reply := func(status int, body string) (*http.Response, error) {
|
|
return &http.Response{StatusCode: status, Status: http.StatusText(status), Header: http.Header{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
|
|
}
|
|
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
|
if key == "" {
|
|
key = r.Header.Get("x-api-key")
|
|
}
|
|
good := key == "good"
|
|
switch {
|
|
case strings.HasSuffix(r.URL.Path, "/models"):
|
|
return reply(200, `{"data":[{"id":"glm-5.1"}]}`)
|
|
case r.URL.Path == "/api/v1/responses":
|
|
if !good {
|
|
return reply(200, zhipuEnvelope1000)
|
|
}
|
|
return reply(200, `{"id":"resp_1","object":"response","status":"completed","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hi"}]}]}`)
|
|
case r.URL.Path == "/api/coding/paas/v4/chat/completions":
|
|
if !good {
|
|
return reply(401, zhipuChat401)
|
|
}
|
|
return reply(200, `{"id":"c","choices":[{"index":0,"message":{"role":"assistant","content":"hi"}}]}`)
|
|
case r.URL.Path == "/api/anthropic/v1/messages":
|
|
if !good {
|
|
return reply(401, zhipuAnthropic401)
|
|
}
|
|
return reply(200, `{"id":"m","type":"message","role":"assistant","content":[{"type":"text","text":"hi"}]}`)
|
|
case strings.HasPrefix(r.URL.Path, "/api/monitor/usage/quota/limit"):
|
|
if !good {
|
|
return reply(200, zhipuEnvelope1000)
|
|
}
|
|
return reply(200, `{"success":true,"data":{"level":"pro","limits":[{"type":"TOKENS_LIMIT","unit":3,"number":5,"percentage":12}]}}`)
|
|
}
|
|
return reply(200, `{"code":500,"msg":"404 NOT_FOUND","success":false}`)
|
|
}
|
|
|
|
// zhipuCodingPlan is the zhipu preset on its Coding Plan endpoints.
|
|
func zhipuCodingPlan(t *testing.T, key string) Provider {
|
|
t.Helper()
|
|
p, err := FromPreset("zhipu")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, r := range Preset("zhipu").Regions {
|
|
if r.ID == "coding" {
|
|
p.Chat, p.Responses, p.Anthropic = r.Chat, r.Responses, r.Anthropic
|
|
}
|
|
}
|
|
if p.Responses != "https://open.bigmodel.cn/api/v1" {
|
|
t.Fatalf("coding plan's Responses: %q", p.Responses)
|
|
}
|
|
p.ID, p.Name, p.Key, p.Models = "glm", "GLM", key, []string{"glm-5.1"}
|
|
return p
|
|
}
|
|
|
|
// @xanahopper on X: the editor's Test on a GLM Coding Plan showed OpenAI and
|
|
// Anthropic "401 · 令牌已过期或验证不正确" beside a green Responses "25 ms".
|
|
// Zhipu's /api/v1 answers a refused key 200 in its envelope; that is the
|
|
// refusal it is, on every path that tests an endpoint, and a key Zhipu
|
|
// takes is still green there.
|
|
func TestZhipuResponsesRefusalNotGreen(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
fake := &zhipuRefuses{}
|
|
was := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = fake
|
|
t.Cleanup(func() { http.DefaultClient.Transport = was })
|
|
|
|
bad := zhipuCodingPlan(t, "expired")
|
|
res := bad.Test(context.Background())
|
|
seen := map[Protocol]bool{}
|
|
for _, r := range res {
|
|
seen[r.Protocol] = true
|
|
if r.OK || r.Status != http.StatusUnauthorized || !strings.Contains(r.Error, "身份验证失败") {
|
|
t.Errorf("refused key on %s: %+v", r.Protocol, r)
|
|
}
|
|
}
|
|
if !seen[Responses] || !seen[Chat] || !seen[Anthropic] {
|
|
t.Fatalf("endpoints tested: %+v", res)
|
|
}
|
|
// a model's own test, and Detect APIs, on the Responses API
|
|
if r := (Provider{ID: "glm-r", Responses: bad.Responses, Key: "expired", Models: []string{"glm-5.1"}}).TestModels(context.Background(), []string{"glm-5.1"}); r[0].OK || r[0].Status != 401 {
|
|
t.Errorf("model test on the Responses API: %+v", r[0])
|
|
}
|
|
if r := probe(context.Background(), bad, Responses, bad.Responses+"/responses", []byte(`{"model":"glm-5.1","input":"hi"}`), "glm-5.1", testWait); r.OK {
|
|
t.Errorf("detect's probe: %+v", r)
|
|
}
|
|
|
|
good := zhipuCodingPlan(t, "good")
|
|
for _, r := range good.Test(context.Background()) {
|
|
if !r.OK {
|
|
t.Errorf("a key Zhipu takes on %s: %+v", r.Protocol, r)
|
|
}
|
|
}
|
|
}
|
|
|
|
// What ZhipuEnveloped turns into a refusal, and what it leaves alone.
|
|
func TestZhipuEnveloped(t *testing.T) {
|
|
mk := func(host, ct, body string) (*http.Request, *http.Response) {
|
|
req, _ := http.NewRequest("POST", "https://"+host+"/api/v1/responses", nil)
|
|
return req, &http.Response{StatusCode: 200, Status: "200 OK", Header: http.Header{"Content-Type": {ct}}, Body: io.NopCloser(strings.NewReader(body))}
|
|
}
|
|
read := func(res *http.Response) string {
|
|
b, _ := io.ReadAll(res.Body)
|
|
return string(b)
|
|
}
|
|
for _, c := range []struct {
|
|
body string
|
|
status int
|
|
}{
|
|
{zhipuEnvelope1000, 401},
|
|
{`{"code":1001,"msg":"Header中未收到Authorization参数,无法进行身份验证。","success":false}`, 401},
|
|
{`{"code":1003,"msg":"Authentication Token 已过期,请重新生成/获取","success":false}`, 401},
|
|
{`{"code":1113,"msg":"余额不足","success":false}`, 429},
|
|
{`{"code":1310,"msg":"已达到每周使用上限","success":false}`, 429},
|
|
{`{"code":1211,"msg":"模型不存在","success":false}`, 400},
|
|
{`{"code":1220,"msg":"无权访问","success":false}`, 403},
|
|
{`{"code":500,"msg":"404 NOT_FOUND","success":false}`, 502},
|
|
{`{"code":"1000","msg":"身份验证失败。","success":false}`, 401},
|
|
} {
|
|
for _, host := range []string{"open.bigmodel.cn", "api.z.ai"} {
|
|
req, res := mk(host, "application/json", c.body)
|
|
res = ZhipuEnveloped(req, res)
|
|
if b := read(res); res.StatusCode != c.status || !strings.Contains(b, `"error":{`) || !strings.Contains(b, `"message"`) {
|
|
t.Errorf("%s %s: %d %s", host, c.body, res.StatusCode, b)
|
|
}
|
|
}
|
|
}
|
|
// a reply, another host's envelope, a stream and a long body pass as
|
|
// they came
|
|
long := `{"id":"r","output":[{"text":"` + strings.Repeat("x", 70<<10) + `"}],"success":false}`
|
|
for _, c := range []struct{ host, ct, body string }{
|
|
{"open.bigmodel.cn", "application/json", `{"id":"resp_1","object":"response","output":[]}`},
|
|
{"open.bigmodel.cn", "application/json", `{"success":true,"data":{}}`},
|
|
{"api.example.com", "application/json", zhipuEnvelope1000},
|
|
{"open.bigmodel.cn", "text/event-stream", "data: " + zhipuEnvelope1000 + "\n\n"},
|
|
{"open.bigmodel.cn", "application/json", long},
|
|
} {
|
|
req, res := mk(c.host, c.ct, c.body)
|
|
res = ZhipuEnveloped(req, res)
|
|
if b := read(res); res.StatusCode != 200 || b != c.body {
|
|
t.Errorf("%s %s: changed to %d %.80s", c.host, c.ct, res.StatusCode, b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The plan's card for a key Zhipu refuses says the key was refused, and
|
|
// what to do, with Zhipu's words; it isn't asked again as a team's key.
|
|
func TestZhipuRefusedKeyCard(t *testing.T) {
|
|
fake := &zhipuRefuses{}
|
|
was := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = fake
|
|
t.Cleanup(func() { http.DefaultClient.Transport = was })
|
|
src, ok := planQuotaSourceOf(zhipuCodingPlan(t, "expired"))
|
|
if !ok || !src.sure {
|
|
t.Fatalf("coding plan's quota source: %+v %v", src, ok)
|
|
}
|
|
_, _, _, err := planKeyWindows(context.Background(), Provider{ID: "glm"}, src, "expired")
|
|
if err == nil || !strings.HasPrefix(err.Error(), "Zhipu didn't take this key (身份验证失败。)") || !strings.Contains(err.Error(), "open.bigmodel.cn/usercenter/proj-mgmt/apikeys") {
|
|
t.Errorf("refused key's card: %v", err)
|
|
}
|
|
if len(fake.asked) != 1 {
|
|
t.Errorf("asked %v, want the plan's quota alone", fake.asked)
|
|
}
|
|
zai := planQuotaSource{url: "https://api.z.ai/api/monitor/usage/quota/limit", read: readZhipuPlan, sure: true}
|
|
if _, _, err := planWindows(context.Background(), zai, "expired"); err == nil || !strings.HasPrefix(err.Error(), "Z.ai didn't take this key") {
|
|
t.Errorf("Z.ai's refused key: %v", err)
|
|
}
|
|
if _, ws, err := planWindows(context.Background(), src, "good"); err != nil || len(ws) != 1 {
|
|
t.Errorf("a key Zhipu takes: %v %+v", err, ws)
|
|
}
|
|
}
|