zcode 0.1.63: the app's China sign-in keeps its identity, and the active provider's profile names the account (#1135)

ZCode's app sign-in reads oauth:active_provider and includes zai_cn, so
a China-channel account keeps its name and user id, follows renewal for
the same user, and stays on its own session when the app switches to
another user with the same display name. Unreadable active_provider
falls back to the old order with zai_cn added.

Reviewed at 5d6e923e3. Run (mica, sandbox HOME): bun test packages/zcode
260 pass; scripts/check.mjs zcode ok. Broken by hand: main's index.mjs
with this PR's tests fails 3 of own.test.mjs (China identity, active
zai_cn, active bigmodel).

Co-authored-by: Lucentio-Huang <Lucentio-Huang@users.noreply.github.com>
This commit is contained in:
Lucentio-Huang
2026-11-14 17:02:35 +08:00
committed by GitHub
co-authored by Lucentio-Huang
parent 191d94bf6e
commit 3f37b858db
3 changed files with 46 additions and 2 deletions
+5 -1
View File
@@ -1804,7 +1804,11 @@ function ownSignIn() {
else s = { ...s, jwt, base: s.base || ZAI_BASE, site: s.base === BIGMODEL_BASE ? "bigmodel" : "zai" }
if (!s.key && !s.jwt && !isTeam(s)) return null
let info = {}
for (const name of ["oauth:zai:user_info", "oauth:bigmodel:user_info"]) {
// The JWT belongs to the app's active sign-in. Cached profiles from
// another provider must not name it, and 3.18's China sign-in is zai_cn.
const active = decrypt(key, store["oauth:active_provider"])
const families = [...new Set([active, "zai", "zai_cn", "bigmodel"].filter((x) => ["zai", "zai_cn", "bigmodel"].includes(x)))]
for (const name of families.map((x) => `oauth:${x}:user_info`)) {
const v = decrypt(key, store[name])
if (v && !first(info.email, info.name, info.user_id)) {
try {
+40
View File
@@ -315,3 +315,43 @@ test("ZCode 3.16's key location lists the coding plan's models", async () => {
_internal.routes.clear()
await expect(hooks.provider.models({ models: {} }, { auth: { ...z, type: "oauth" } })).rejects.toThrow("coding plan key")
})
// ZCode 3.18's China sign-in keeps its identity under zai_cn. Losing it
// makes every gift-only account "ZCode" and lets it follow a different
// account's session when the app switches users.
test("the app's China sign-in keeps its identity and follows only that user", async () => {
const as = (id) => JSON.stringify({ email: "ada@phone.local", user_id: id })
zcodeHas({
zcodejwttoken: "Bearer china-a",
"oauth:active_provider": "zai_cn",
"oauth:zai_cn:user_info": as("china-user-a"),
})
const auth = await appSignIn()
expect(auth.accountId).toBe("ada")
expect(auth.uid).toBe("china-user-a")
zcodeHas({
zcodejwttoken: "Bearer china-renewed",
"oauth:active_provider": "zai_cn",
"oauth:zai_cn:user_info": as("china-user-a"),
})
expect((await _internal.stateOf(auth)).jwt).toBe("china-renewed")
// Two accounts may share the same display name; the uid guards them.
zcodeHas({
zcodejwttoken: "Bearer china-b",
"oauth:active_provider": "zai_cn",
"oauth:zai_cn:user_info": as("china-user-b"),
})
expect((await _internal.stateOf(auth)).jwt).toBe("china-a")
})
test.each(["zai_cn", "zai", "bigmodel"])("the app's active %s identity wins over another provider's cached profile", async (active) => {
const store = { zcodejwttoken: "Bearer active-session", "oauth:active_provider": active }
for (const family of ["zai", "zai_cn", "bigmodel"])
store[`oauth:${family}:user_info`] = JSON.stringify({ email: `${family}@example.com`, user_id: `user-${family}` })
zcodeHas(store)
const auth = await appSignIn()
expect(auth.accountId).toBe(`${active}@example.com`)
expect(auth.uid).toBe(`user-${active}`)
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.61",
"version": "0.1.63",
"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, and claims its gift plans daily",
"type": "module",
"main": "./opencode.mjs",