From 5c0aebce8f8f1cecc0f63deca7e7d4426c11f5ca Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 15:45:44 +0800 Subject: [PATCH] zcode 0.1.18: ZCode app's sign-in reads ZCode 3.15's plan key from config.json, so a valid GLM Coding Plan no longer reads as "no plan" (yetone/magpie#2130) What changed: ZCode 3.15 keeps no coding plan key in credentials.json any more. The key it uses is in config.json, at provider["builtin:bigmodel- coding-plan" / "builtin:zai-coding-plan"].options.apiKey; ZCode 3.15.3's host reads that value as it is. ownSignIn found no key, so a signed-in GLM Coding Plan account fell to the Start Plan and read as having no plan. ownSignIn now falls back to config.json (configKey) when credentials.json has no key: Z.ai's first, then BigModel. It also takes the provider ids 3.15 normalizes to (account:*-individual-coding-plan) and a value written encrypted. A value that isn't a key, such as "{env:...}", is skipped. A key in credentials.json and a team switch still come first. This matches magpie's built-in (magpie 1d8e83a79, zcodeOwn/zcodeConfigKey). What was run: - The new own.test.mjs case uses the reporter's layout (BigModel sign-in, no key in credentials.json, key in config.json). It fails without the fallback ("j1" in place of "bm.secret"). - Every packages/zcode test file passes under a scratch HOME/TMPDIR: dual 25, gift 20, models 1, moved 1, own 6, signin 3, start 10, usage 15. - bun scripts/check.mjs passes. Not tried: a real ZCode 3.15 account. --- packages/zcode/index.mjs | 24 ++++++++++++++++++++++++ packages/zcode/own.test.mjs | 29 +++++++++++++++++++++++++++++ packages/zcode/package.json | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 3b7eb2e..166e881 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1026,6 +1026,27 @@ function ownTeam(store, key) { return null } +// configKey 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. +function configKey(key) { + const providers = readJSON(join(zcodeDir(), "config.json"))?.provider + if (!providers || typeof providers !== "object") return null + for (const [ids, base] of [ + [["builtin:zai-coding-plan", "account:zai-individual-coding-plan"], ZAI_BASE], + [["builtin:bigmodel-coding-plan", "account:bigmodel-individual-coding-plan"], BIGMODEL_BASE], + ]) { + for (const id of ids) { + let k = providers[id]?.options?.apiKey + if (typeof k !== "string") continue + k = k.trim() + if (k.startsWith("enc:")) k = first(decrypt(key, k)) + if (k.includes(".") && !/[\s{$]/.test(k)) return { key: k, base } + } + } + return null +} + // ownSignIn is what ZCode is signed in to now: the account, its coding // plan key, ZCode's session token (the Start Plan's key) and the team plan // it is switched to; null when it has none of them. @@ -1043,6 +1064,9 @@ function ownSignIn() { // Z.ai's first, as ZCode lists it if (!s.key || (base === ZAI_BASE && s.base !== ZAI_BASE)) s = { key: k, base } } + // ZCode 3.15 keeps no plan key in its credentials; the one it uses sits + // in config.json, the provider's options.apiKey (yetone/magpie#2130) + if (!s.key) s = configKey(key) || s const jwt = first(first(decrypt(key, store.zcodejwttoken)).replace(/^Bearer /, "")) const team = ownTeam(store, key) if (team) s = { ...team, jwt } diff --git a/packages/zcode/own.test.mjs b/packages/zcode/own.test.mjs index a0f1e5a..7a53276 100644 --- a/packages/zcode/own.test.mjs +++ b/packages/zcode/own.test.mjs @@ -140,3 +140,32 @@ test("an app sign-in for A whose saved session has lapsed, with ZCode on B, says expect(err?.message).toContain("sign in") expect(sent.join("")).not.toContain("jB-live") }) + +// 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 (yetone/magpie#2130) +test("ZCode 3.15's plan key is read from its config.json", async () => { + const bm = JSON.stringify({ email: "bm@example.com" }) + zcodeHas({ zcodejwttoken: "Bearer j1", "oauth:bigmodel:user_info": bm, "oauth:bigmodel:access_token": "tok" }) + const config = (provider) => writeFileSync(join(dir(), "config.json"), JSON.stringify({ $schema: "https://opencode.ai/config.json", provider })) + config({ + "builtin:bigmodel-coding-plan": { name: "GLM Coding Plan", options: { apiKey: "bm.secret", baseURL: "https://open.bigmodel.cn/api/anthropic" }, models: {} }, + "builtin:zai-coding-plan": { options: { apiKey: "" } }, + }) + const hooks = await ZCodeAuthPlugin({ client: {} }) + const m = hooks.auth.methods.find((x) => x.label === "ZCode app's sign-in") + const got = await (await m.authorize()).callback() + expect(got.type).toBe("success") + expect(got.access).toBe("bm.secret") + const auth = { ...got, type: "oauth" } + expect(_internal.stateOf(auth)).toMatchObject({ key: "bm.secret", jwt: "j1", base: "https://open.bigmodel.cn/api/anthropic", site: "bigmodel" }) + + // one written encrypted reads too; one that isn't a key doesn't + config({ "builtin:zai-coding-plan": { options: { apiKey: "{env:ZAI_KEY}" } }, "builtin:bigmodel-coding-plan": { options: { apiKey: enc("bm.secret2") } } }) + expect(_internal.stateOf(auth)).toMatchObject({ key: "bm.secret2", base: "https://open.bigmodel.cn/api/anthropic" }) + + // a key in credentials.json still comes first + zcodeHas({ [KEY]: "id1.secret1", zcodejwttoken: "j1", "oauth:bigmodel:user_info": bm }) + expect(_internal.stateOf(auth)).toMatchObject({ key: "id1.secret1", base: "https://api.z.ai/api/anthropic" }) + config({}) +}) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 32e862a..e6c228e 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.17", + "version": "0.1.18", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan", "type": "module", "main": "./index.mjs",