diff --git a/internal/provider/zcode.go b/internal/provider/zcode.go index 290a504e6..da3a9928a 100644 --- a/internal/provider/zcode.go +++ b/internal/provider/zcode.go @@ -180,6 +180,11 @@ func zcodeOwn() (who string, k zcodeKey, ok bool) { k = zcodeKey{Key: key, Base: base} } } + // ZCode 3.15 keeps no plan key in its credentials; the one it uses + // sits in config.json, the provider's options.apiKey (#2130) + if k.Key == "" { + k = zcodeConfigKey(secret) + } if v, ok := store["zcodejwttoken"]; ok { if s, ok := zcodeDecrypt(secret, v); ok { k.JWT = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(s), "Bearer ")) @@ -212,6 +217,41 @@ func zcodeOwn() (who string, k zcodeKey, ok bool) { return zcodeWho(info.Email, info.Name, info.ID), k, true } +// zcodeConfigKey is the coding plan key ZCode's config.json gives its +// built-in coding plan provider, Z.ai's first, as ZCode lists them; ZCode +// reads it as it is, and one written encrypted is read too. +func zcodeConfigKey(secret []byte) zcodeKey { + var cfg struct { + Provider map[string]struct { + Options struct { + APIKey string `json:"apiKey"` + } `json:"options"` + } `json:"provider"` + } + if !readJSON(filepath.Join(filepath.Dir(zcodeCredentialsPath()), "config.json"), &cfg) { + return zcodeKey{} + } + for _, p := range []struct { + ids []string + base string + }{ + {[]string{"builtin:zai-coding-plan", "account:zai-individual-coding-plan"}, ZCodeZaiBase}, + {[]string{"builtin:bigmodel-coding-plan", "account:bigmodel-individual-coding-plan"}, ZCodeBigModelBase}, + } { + for _, id := range p.ids { + key := strings.TrimSpace(cfg.Provider[id].Options.APIKey) + if strings.HasPrefix(key, "enc:") { + key, _ = zcodeDecrypt(secret, key) + key = strings.TrimSpace(key) + } + if strings.Contains(key, ".") && !strings.ContainsAny(key, " {$") { + return zcodeKey{Key: key, Base: p.base} + } + } + } + return zcodeKey{} +} + // zcodeWho names a Z.ai account: its email, or the phone number of one // signed in by phone (its email is then @phone.local). func zcodeWho(email, name, id string) string { diff --git a/internal/provider/zcode_test.go b/internal/provider/zcode_test.go index 2c0e056da..b1f1ef6fc 100644 --- a/internal/provider/zcode_test.go +++ b/internal/provider/zcode_test.go @@ -206,3 +206,87 @@ func TestZCodeAccounts(t *testing.T) { t.Fatal("zcode without an account") } } + +// ZCode 3.15 keeps no coding plan key in its credentials: a BigModel GLM +// Coding Pro's key sits in config.json, the built-in coding plan +// provider's options.apiKey, and the account reads as that plan, not as +// one with no plan and a Start Plan ended (#2130). +func TestZCodePlanKeyFromConfig(t *testing.T) { + home := signIn(t) + t.Setenv("ZCODE_CREDENTIAL_SECRET", "test-secret") + dir := filepath.Join(home, ".zcode", "v2") + writeFile(t, filepath.Join(dir, "credentials.json"), map[string]any{ + "oauth:bigmodel:user_info": zcodeEncrypt(t, `{"user_id":"u1","email":"bm@example.com","name":"bm"}`), + "oauth:bigmodel:access_token": zcodeEncrypt(t, "tok"), + "zcodejwttoken": zcodeEncrypt(t, "Bearer zc"), + }) + // as the reporter's: OpenCode's layout, the key as it is + writeFile(t, filepath.Join(dir, "config.json"), map[string]any{ + "$schema": "https://opencode.ai/config.json", + "provider": map[string]any{ + "builtin:bigmodel-coding-plan": map[string]any{ + "name": "GLM Coding Plan", + "options": map[string]any{"apiKey": "bm.secret", "baseURL": "https://open.bigmodel.cn/api/anthropic"}, + "models": map[string]any{"glm-5.1": map[string]any{}}, + }, + "builtin:zai-coding-plan": map[string]any{"options": map[string]any{"apiKey": ""}}, + }, + }) + var start atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ok := func(data any) { json.NewEncoder(w).Encode(map[string]any{"code": 200, "success": true, "data": data}) } + auth := r.Header.Get("Authorization") + switch { + case strings.Contains(r.URL.Path, "/balance") || strings.Contains(r.URL.Path, "start"): + start.Add(1) + w.WriteHeader(404) + case auth != "bm.secret": + w.WriteHeader(401) + case r.URL.Path == "/api/biz/subscription/list": + ok([]any{map[string]any{"productName": "GLM Coding Pro", "status": "VALID"}}) + case r.URL.Path == "/api/monitor/usage/quota/limit": + reset := time.Now().Add(time.Hour).UnixMilli() + ok(map[string]any{"level": "pro", "limits": []any{ + map[string]any{"type": "TOKENS_LIMIT", "unit": 3, "number": 5, "percentage": 1, "nextResetTime": reset}, + map[string]any{"type": "TOKENS_LIMIT", "unit": 6, "number": 1, "percentage": 5, "nextResetTime": reset}, + }}) + default: + ok([]any{}) + } + })) + defer srv.Close() + oldAPI, oldZai, oldBM, oldZaiBase, oldBMBase := zcodeAPI, zcodeZaiAPI, zcodeBigModelAPI, ZCodeZaiBase, ZCodeBigModelBase + zcodeAPI, zcodeZaiAPI, zcodeBigModelAPI = srv.URL, srv.URL, srv.URL + ZCodeZaiBase, ZCodeBigModelBase = srv.URL+"/zai/api/anthropic", srv.URL+"/bigmodel/api/anthropic" + defer func() { + zcodeAPI, zcodeZaiAPI, zcodeBigModelAPI, ZCodeZaiBase, ZCodeBigModelBase = oldAPI, oldZai, oldBM, oldZaiBase, oldBMBase + }() + + who, k, ok := zcodeOwn() + if !ok || who != "bm@example.com" || k.Key != "bm.secret" || k.Base != ZCodeBigModelBase || k.JWT != "zc" { + t.Fatalf("own: %v %q %+v", ok, who, k) + } + q := zcodeQuota(context.Background(), Login{User: who}, k) + if q.Error != "" || q.Plan != "GLM Coding Pro" || len(q.Windows) != 2 { + t.Fatalf("quota: %+v", q) + } + if n := start.Load(); n != 0 { + t.Fatalf("the Start Plan asked %d times for an account with a Coding Plan", n) + } + + // a key written encrypted reads too; one that isn't a key doesn't + writeFile(t, filepath.Join(dir, "config.json"), map[string]any{"provider": map[string]any{ + "builtin:zai-coding-plan": map[string]any{"options": map[string]any{"apiKey": "{env:ZAI_KEY}"}}, + "builtin:bigmodel-coding-plan": map[string]any{"options": map[string]any{"apiKey": zcodeEncrypt(t, "bm.secret")}}, + }}) + if _, k, _ := zcodeOwn(); k.Key != "bm.secret" || k.Base != ZCodeBigModelBase { + t.Fatalf("encrypted: %+v", k) + } + // a key in credentials.json still comes first + writeFile(t, filepath.Join(dir, "credentials.json"), map[string]any{ + "account-provider:coding-plan:account:zai-individual-coding-plan:account:a:api-key": zcodeEncrypt(t, "own.secret"), + }) + if _, k, _ := zcodeOwn(); k.Key != "own.secret" || k.Base != ZCodeZaiBase { + t.Fatalf("credentials first: %+v", k) + } +}