@cleverpigo on X: "glm coding plan放在codex/chatgpt里显示余额不足没法用". The editor
showed OpenAI https://open.bigmodel.cn/... red with 429 · 余额不足或无可用资源包,
请充值。 next to a green Anthropic https://open.bigmodel.cn/api/anthropic.
Root cause: the zhipu and zai presets start on their first region, Pay as
you go, whose OpenAI URL is /api/paas/v4. The Anthropic URL is the same in
both regions, and it bills the Coding Plan when the account has one. So a
Coding Plan key left at the default works in Claude Code, while Codex goes
to /api/paas/v4 and Zhipu answers 429 code 1113 (no balance). A provider
made by pasting the paas/v4 URL hits the same wall. Nothing in the key
says which kind it is. LESSONS.md's e63fbb0e entry records that last
time only the label was changed; this commit fixes the request itself.
Fix (internal/provider/zhipu_coding.go, following RetryOneAuth):
- A 429 1113 from {open.bigmodel.cn,api.z.ai}/api/paas/v4/... is sent
once more to /api/coding/paas/v4/.... This happens both in the gateway
(forwardOnce, Chat) and in the editor's endpoint test (probe).
- If the coding endpoint serves it, LearnZhipuCoding moves the saved
provider to the plan's OpenAI URL, plus the preset's /api/v1 Responses
URL if none is set. It only does this when the provider has that one
key and is still at the paas URL. A provider with more keys, or one
the user has changed since, is left alone.
- If the coding endpoint refuses too, the 1113 body keeps its code, so
the error is still classed as out of credit. Its message also names
both URLs and says what to change: set Plan to Coding Plan (with its
URL), or top up the pay-as-you-go balance.
- A pay-as-you-go key that has a balance never gets a 1113. It stays on
/api/paas/v4 and is never sent to the plan's endpoint.
Tests:
- TestGLMCodingPlanKeyAtPayAsYouGo (gateway; zhipu and zai) covers:
- the Codex /v1/responses path
- the move
- a second request going straight to coding
- a payg key staying put
- the broke key's message
- TestZhipuCodingPlanKeyTested and TestZhipuCodingBase (provider) cover:
- a draft
- a saved preset
- a pasted custom provider (only Chat moves)
- a provider with several keys (not moved)
- payg
- broke
- a key the user has changed
- With the two hooks reverted, both new behaviour tests fail.
Run (sandbox HOME, pinned Go caches):
- gofmt -l: clean
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...:
clean
- GOOS=linux go build -tags nogui and GOOS=windows go build: OK
- go test -tags nogui ./internal/provider/: ok (363s)
- go test -tags nogui -timeout 40m ./internal/gateway/: ok (1922s, on a
machine at load ~200)
Not tried: a real Zhipu or Z.ai key. None was available, so the 1113
body and the coding endpoint's answer come from the report and the
presets, not a live call.
185 lines
6.4 KiB
Go
185 lines
6.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// A GLM Coding Plan (Zhipu's, on open.bigmodel.cn, and Z.ai's, on
|
|
// api.z.ai) is served at its own OpenAI endpoint, /api/coding/paas/v4,
|
|
// while the pay-as-you-go API is /api/paas/v4. Both share the Anthropic
|
|
// endpoint (/api/anthropic), which bills the plan when the account has one,
|
|
// so a plan's key given the zhipu or zai preset at its first plan, Pay as
|
|
// you go, works in Claude Code and is refused in Codex: the pay-as-you-go
|
|
// endpoint answers it 429, code 1113 "余额不足或无可用资源包,请充值。" (the
|
|
// account has no balance), as @cleverpigo on X and kkgg on Discord found.
|
|
// Nothing in the key tells the two apart, so a 1113 at /api/paas/v4 is
|
|
// asked again at /api/coding/paas/v4: served there, the key is a plan's,
|
|
// and the provider is moved to the plan's endpoints (LearnZhipuCoding).
|
|
// Refused there too, the refusal says what each endpoint said and what to
|
|
// change. A pay-as-you-go key with a balance is never answered 1113 and
|
|
// stays where it is.
|
|
|
|
// zhipuPaygPath and zhipuCodingPath are the pay-as-you-go and the Coding
|
|
// Plan's OpenAI paths on both hosts.
|
|
const (
|
|
zhipuPaygPath = "/api/paas/v4"
|
|
zhipuCodingPath = "/api/coding/paas/v4"
|
|
)
|
|
|
|
// zhipuHost is a host that serves a GLM Coding Plan beside pay as you go.
|
|
func zhipuHost(host string) bool {
|
|
switch strings.ToLower(host) {
|
|
case "open.bigmodel.cn", "api.z.ai":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ZhipuCodingBase is the GLM Coding Plan's OpenAI base for base, Zhipu's or
|
|
// Z.ai's pay-as-you-go one (https://open.bigmodel.cn/api/paas/v4 →
|
|
// https://open.bigmodel.cn/api/coding/paas/v4), and whether base is one.
|
|
func ZhipuCodingBase(base string) (string, bool) {
|
|
b := strings.TrimRight(strings.TrimSpace(base), "/")
|
|
if !zhipuHost(HostOf(b)) || basePath(b) != zhipuPaygPath {
|
|
return "", false
|
|
}
|
|
return strings.TrimSuffix(b, zhipuPaygPath) + zhipuCodingPath, true
|
|
}
|
|
|
|
// zhipuNoBalance is Zhipu's refusal of a key whose account has no balance
|
|
// for the API asked: 429 with code 1113.
|
|
func zhipuNoBalance(status int, b []byte) bool {
|
|
return status == http.StatusTooManyRequests && ErrorType(b) == "1113"
|
|
}
|
|
|
|
// RetryZhipuCoding hands on res, the answer to req; or, when req went to
|
|
// Zhipu's or Z.ai's pay-as-you-go OpenAI API and was refused for want of a
|
|
// balance (1113), the answer to req asked again at the GLM Coding Plan's
|
|
// endpoint when that one serves it — remembering it for p
|
|
// (LearnZhipuCoding) — else res with what to change added to its message.
|
|
// do sends a request as req was sent.
|
|
func RetryZhipuCoding(p Provider, req *http.Request, res *http.Response, do func(*http.Request) (*http.Response, error)) (*http.Response, error) {
|
|
if res == nil || res.StatusCode != http.StatusTooManyRequests || req.GetBody == nil || !zhipuHost(req.URL.Host) {
|
|
return res, nil
|
|
}
|
|
rest, ok := strings.CutPrefix(req.URL.Path, zhipuPaygPath+"/")
|
|
if !ok {
|
|
return res, nil
|
|
}
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 64<<10))
|
|
res.Body.Close()
|
|
res.Body = io.NopCloser(bytes.NewReader(b))
|
|
if !zhipuNoBalance(res.StatusCode, b) {
|
|
return res, nil
|
|
}
|
|
body, err := req.GetBody()
|
|
if err != nil {
|
|
return res, nil
|
|
}
|
|
again := req.Clone(req.Context())
|
|
again.Body = body
|
|
again.URL.Path = zhipuCodingPath + "/" + rest
|
|
again.URL.RawPath = ""
|
|
from := req.URL.Scheme + "://" + req.URL.Host + zhipuPaygPath
|
|
coding := req.URL.Scheme + "://" + req.URL.Host + zhipuCodingPath
|
|
got, err := do(again)
|
|
said := ""
|
|
if err != nil {
|
|
said = err.Error()
|
|
} else if got.StatusCode < 300 {
|
|
LearnZhipuCoding(p, from)
|
|
return got, nil
|
|
} else {
|
|
gb, _ := io.ReadAll(io.LimitReader(got.Body, 64<<10))
|
|
got.Body.Close()
|
|
said = fmt.Sprintf("%d %s", got.StatusCode, APIError(gb, got.Status))
|
|
}
|
|
b = withMessage(b, apiError(b, res.Status)+" — "+ZhipuNoBalanceHint(from, coding, said))
|
|
res.Body = io.NopCloser(bytes.NewReader(b))
|
|
res.ContentLength = int64(len(b))
|
|
if res.Header == nil {
|
|
res.Header = http.Header{}
|
|
}
|
|
res.Header.Set("Content-Length", strconv.Itoa(len(b)))
|
|
return res, nil
|
|
}
|
|
|
|
// ZhipuNoBalanceHint says what a 1113 at the pay-as-you-go endpoint from
|
|
// means when the Coding Plan's endpoint coding didn't serve the key either
|
|
// (said is what it answered), and what to change.
|
|
func ZhipuNoBalanceHint(from, coding, said string) string {
|
|
site := "open.bigmodel.cn"
|
|
if HostOf(from) == "api.z.ai" {
|
|
site = "z.ai"
|
|
}
|
|
return fmt.Sprintf("this key's account has no balance for the pay-as-you-go API (%s), and the GLM Coding Plan's endpoint (%s) didn't serve it either (%s). "+
|
|
"A GLM Coding Plan's key: set the provider's Plan to Coding Plan (its OpenAI URL %s), and check the plan is active at %s. "+
|
|
"A pay-as-you-go key: top up the account's balance at %s",
|
|
from, coding, said, coding, site, site)
|
|
}
|
|
|
|
// withMessage is the JSON error body b with its message set to msg, the
|
|
// rest (Zhipu's code 1113) as it was.
|
|
func withMessage(b []byte, msg string) []byte {
|
|
var v map[string]any
|
|
if json.Unmarshal(b, &v) != nil || v == nil {
|
|
v = map[string]any{}
|
|
}
|
|
e, _ := v["error"].(map[string]any)
|
|
if e == nil {
|
|
e = map[string]any{}
|
|
v["error"] = e
|
|
}
|
|
e["message"] = msg
|
|
out, err := json.Marshal(v)
|
|
if err != nil {
|
|
return b
|
|
}
|
|
return out
|
|
}
|
|
|
|
// LearnZhipuCoding moves provider p, whose key the pay-as-you-go endpoint
|
|
// from refused and the GLM Coding Plan's served, to the plan's endpoints:
|
|
// its OpenAI URL, and the plan's Responses URL when it has none (the zhipu
|
|
// and zai presets' Coding Plan region). Only a provider with that one key
|
|
// still at from is moved: one with other keys may hold pay-as-you-go keys
|
|
// too, and one the user has moved since is theirs. Each of its requests
|
|
// is then asked again at the plan's endpoint as this one was.
|
|
func LearnZhipuCoding(p Provider, from string) {
|
|
coding, ok := ZhipuCodingBase(from)
|
|
if !ok || p.ID == "" || p.Account != nil {
|
|
return
|
|
}
|
|
learnMu.Lock()
|
|
defer learnMu.Unlock()
|
|
f, err := read()
|
|
if err != nil {
|
|
return
|
|
}
|
|
for i := range f.Providers {
|
|
q := &f.Providers[i]
|
|
if q.ID != p.ID {
|
|
continue
|
|
}
|
|
if q.Account != nil || len(q.Keys) > 0 || q.Key != p.Key || strings.TrimRight(q.Chat, "/") != strings.TrimRight(from, "/") {
|
|
return
|
|
}
|
|
q.Chat = coding
|
|
if pr := Preset(q.Preset); pr != nil && q.Responses == "" {
|
|
for _, r := range pr.Regions {
|
|
if r.ID == "coding" && r.Chat == coding {
|
|
q.Responses = r.Responses
|
|
}
|
|
}
|
|
}
|
|
_ = store(f)
|
|
return
|
|
}
|
|
}
|