diff --git a/internal/provider/planquota.go b/internal/provider/planquota.go index d62689370..038780b49 100644 --- a/internal/provider/planquota.go +++ b/internal/provider/planquota.go @@ -104,6 +104,13 @@ func readZhipuPlan(b []byte) (string, []QuotaWindow, error) { if code, msg, ok := zhipuEnvelope(b); ok && ZhipuKeyRefused(code) { // the key itself refused, which no plan can be read past return "", nil, zhipuKeyRefusal{msg} + } else if ok && code != 1113 && zhipuCodeStatus(code) == http.StatusTooManyRequests { + // /api/monitor answers its rate limits 200 in the envelope too + // (1302 "并发数过高", 1303 "频率过高", …), in Chinese words a + // passing failure isn't recognised by: said as the 429 it is, so + // the card keeps what was last read (keepLast) instead of losing + // it to a read that told nothing about the plan + return "", nil, fmt.Errorf("429 rate limited (%d): %s", code, msg) } if r.Success != nil && !*r.Success || r.Data == nil { if r.Msg != "" { @@ -782,9 +789,15 @@ func PlanQuotas(ctx context.Context) []SubscriptionQuota { case err == nil && len(ws) == 0: return // a key with no plan case err != nil && !j.src.sure: - // no plan, unless one was read before + // no plan, unless one was read before: a key whose windows + // were read is a plan's, and a read of it that fails is + // unknown, not "no plan" — its card stays, with what was + // last read when the failure passes, else with why + // (larchsis on X: the second of two GLM Coding Plan keys + // lost its card on each refresh) q.Error = err.Error() - if q = keepReading(ctx, q, tag); q.AsOf != nil { + known := hadReading(q.Provider, tag) + if q = keepReading(ctx, q, tag); q.AsOf != nil || known { got[i] = &q } return diff --git a/internal/provider/quota_last.go b/internal/provider/quota_last.go index 8d5a776ab..af4f0f4b3 100644 --- a/internal/provider/quota_last.go +++ b/internal/provider/quota_last.go @@ -120,6 +120,21 @@ func keepLast(q SubscriptionQuota, user string) SubscriptionQuota { return out } +// hadReading is whether a good reading of provider's account (or key +// tag) user is kept: the account or key was read with windows or a +// balance before. +func hadReading(provider, user string) bool { + if provider == "" || user == "" { + return false + } + c := &lastQuotas + c.Lock() + defer c.Unlock() + c.load() + _, ok := c.m[provider+"/"+strings.ToLower(user)] + return ok +} + // readDeferred is whether err says no read was made this time (allowances // read only when asked, a Claude account's /usage not run) rather than a // read that failed. A read held back by holdUnasked fails inside the diff --git a/internal/provider/zhipu_two_keys_test.go b/internal/provider/zhipu_two_keys_test.go new file mode 100644 index 000000000..2b45be055 --- /dev/null +++ b/internal/provider/zhipu_two_keys_test.go @@ -0,0 +1,120 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "path/filepath" + "sync/atomic" + "testing" + + "github.com/yetone/magpie/internal/agentenv" +) + +// larchsis on X: two GLM Coding Plan keys in one provider, and only one +// card on the Usage page; after a restart both showed at first, and the +// automatic refresh took the second away again. Zhipu's /api/monitor +// answers a rate limit 200 in the platform's envelope, in Chinese +// ("并发数过高"), which wasn't taken for a passing failure, and a key on a +// provider whose OpenAI URL is still pay as you go (one with several keys +// is never moved to the plan's, LearnZhipuCoding) lost its card to any +// failed read. A key read before is a plan's: a read that fails keeps its +// card, as last read when the failure passes, else with why. +func TestZhipuSecondKeyKeepsItsCard(t *testing.T) { + isolate(t) + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache")) + t.Setenv("PATH", h) + for _, v := range agentenv.Vars { + t.Setenv(v, "") + } + restart := func() { + lastQuotas.Lock() + lastQuotas.m, lastQuotas.loaded = nil, false + lastQuotas.Unlock() + forgetPlanQuotas() + } + restart() + t.Cleanup(restart) + + // what the second key's quota read answers: "" its plan + var second atomic.Value + second.Store("") + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.Header.Get("X-Host")+r.URL.Path != "open.bigmodel.cn/api/monitor/usage/quota/limit" { + w.WriteHeader(http.StatusNotFound) // the term and resets: not known + return + } + if r.URL.RawQuery != "" { // a team's: none + w.Write([]byte(`{"code":500,"msg":"no team","success":false}`)) + return + } + if said := second.Load().(string); said != "" && r.Header.Get("Authorization") == "glm-b.secondkey" { + w.Write([]byte(said)) + return + } + w.Write([]byte(`{"code":200,"msg":"操作成功","success":true,"data":{"level":"pro","limits":[ + {"type":"TOKENS_LIMIT","unit":3,"number":5,"percentage":12,"nextResetTime":4102444800000}, + {"type":"TOKENS_LIMIT","unit":6,"number":1,"percentage":40,"nextResetTime":4102444800000}]}}`)) + })) + defer srv.Close() + old := http.DefaultClient.Transport + http.DefaultClient.Transport = rewrite{srv} + t.Cleanup(func() { http.DefaultClient.Transport = old }) + + if err := Save(Provider{ID: "zhipu", Name: "Zhipu GLM", Preset: "zhipu", Key: "glm-a.firstkey", KeyName: "one", + Keys: []KeyAccount{{Name: "two", Key: "glm-b.secondkey"}}, + Chat: "https://open.bigmodel.cn/api/paas/v4", Anthropic: "https://open.bigmodel.cn/api/anthropic"}); err != nil { + t.Fatal(err) + } + read := func() map[string]SubscriptionQuota { + forgetPlanQuotas() + out := map[string]SubscriptionQuota{} + for _, q := range PlanQuotas(context.Background()) { + out[q.User] = q + } + return out + } + cards := read() + for _, u := range []string{"one", "two"} { + if q := cards[u]; len(q.Windows) != 2 || q.Error != "" || q.AsOf != nil { + t.Fatalf("first read, key %s: %+v", u, q) + } + } + + // rate limited, as the envelope says it + second.Store(`{"code":1302,"msg":"您当前使用该API的并发数过高,请降低并发,或联系客服增加限额。","success":false}`) + lastQuotas.Lock() + lastQuotas.m, lastQuotas.loaded = nil, false // from disk, as after a restart + lastQuotas.Unlock() + cards = read() + if q := cards["one"]; len(q.Windows) != 2 || q.AsOf != nil { + t.Fatalf("first key while the second is limited: %+v", q) + } + if q, ok := cards["two"]; !ok || len(q.Windows) != 2 || q.Error != "" || q.AsOf == nil || q.Windows[1].Used != 40 { + t.Fatalf("second key rate limited: kept card wanted, got %v %+v", ok, q) + } + + // a failure that says nothing known: the card stays, saying why + second.Store(`{"code":500,"msg":"系统繁忙","success":false}`) + cards = read() + if q, ok := cards["two"]; !ok || q.Error == "" { + t.Fatalf("second key failing: a card with its error wanted, got %v %+v", ok, q) + } + + // a key never read that fails still has no card: nothing says it's a plan's + if err := Save(Provider{ID: "zhipu-payg", Name: "Pay as you go", Preset: "zhipu", Key: "glm-b.secondkey", + Chat: "https://open.bigmodel.cn/api/paas/v4"}); err != nil { + t.Fatal(err) + } + forgetPlanQuotas() + for _, q := range PlanQuotas(context.Background()) { + if q.Provider == "zhipu-payg" { + t.Fatalf("a key never read has a card: %+v", q) + } + } +}