all: each plugin fails with its built-in's status and marks the sign-in as it did, through X-Magpie-Sign-In (要保证插件跟内置的所有逻辑都要一模一样) — Factory's and MiMo's refused renewals are the built-ins' 502 marked expired, not 401s; Qoder's vendor 403 is the built-in's 401 kept, its model list's failure a 400, its job refresh refused marked expired; Zed's retried 401 is a 401 kept, not a 403; Kiro's unreachable refresh a 401 kept; Cursor's expired trial a 401 kept; a vendor 401 WorkBuddy, Grok, Devin, Command Code and ZCode passed on no longer marks the account, as their built-ins never did, and Grok and MiMo no longer retry what their built-ins sent once. Messages take the built-ins' words. New tests fail against the previous index.mjs.

This commit is contained in:
yetone
2026-10-01 04:47:03 +08:00
parent 53e0db6648
commit 85d7dd0d0a
38 changed files with 769 additions and 182 deletions
+13 -3
View File
@@ -120,7 +120,9 @@ const jwtExpired = (jwt) => {
const t = jwtExpiry(jwt)
return t > 0 && Date.now() > t
}
const EXPIRED = "ZCode's sign-in has expired; sign in again"
// EXPIRED is the built-in's words (errZCodeExpired) for a Start Plan
// sign-in past its end.
const EXPIRED = "ZCode's sign-in has expired; sign in to ZCode again (or add the account again in magpie)"
const num = (v) => {
const n = typeof v === "string" ? parseFloat(v) : v
@@ -236,7 +238,7 @@ async function teamKey(s) {
teamKeys.set(id, { key, at: Date.now() })
return key
} catch (e) {
const err = new Error(`ZCode's team plan: ${e.message} — sign in again`)
const err = new Error(`ZCode's team plan: ${e.message} — sign in to ZCode again`)
teamKeys.set(id, { err, at: Date.now() })
throw err
}
@@ -1093,7 +1095,15 @@ export async function ZCodeAuthPlugin({ client }) {
h.delete("authorization")
h.set("x-api-key", key)
h.set("Authorization", "Bearer " + key)
return fetch(url, { ...opts, headers: h })
const res = await fetch(url, { ...opts, headers: h })
// the plan's 401 goes on as it came: the built-in never
// marked a ZCode account lapsed
if (res.status !== 401) return res
const kept = new Headers(res.headers)
kept.delete("content-length")
kept.delete("content-encoding")
kept.set("X-Magpie-Sign-In", "kept")
return new Response(res.body, { status: res.status, statusText: res.statusText, headers: kept })
},
}
},
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.3",
"version": "0.1.4",
"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",
+48
View File
@@ -0,0 +1,48 @@
// A request's failures, as magpie's built-in ZCode account answered them
// (internal/provider/zcode.go zcodeProvider): the plan's 401 goes on as it
// came, saying X-Magpie-Sign-In: kept, as the built-in never marked a
// ZCode account lapsed; a Start Plan sign-in past its end fails in
// errZCodeExpired's words.
import { afterEach, beforeAll, expect, test } from "bun:test"
import { homedir, tmpdir } from "node:os"
import { realpathSync } from "node:fs"
let ZCodeAuthPlugin
beforeAll(async () => {
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
;({ ZCodeAuthPlugin } = await import("./index.mjs"))
})
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const jwt = (exp) => ["{}", JSON.stringify({ exp })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig"
async function send(auth, reply) {
globalThis.fetch = async () => reply()
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
const opts = await hooks.auth.loader(async () => auth, { id: "zcode" })
return opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", body: "{}" })
}
const key = { type: "api", key: "one.secret", metadata: { site: "zai" } }
test("the plan's 401 goes on as it came, the account kept", async () => {
const res = await send(key, () => Response.json({ error: { message: "token expired or incorrect" } }, { status: 401 }))
expect(res.status).toBe(401)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
expect(await res.json()).toEqual({ error: { message: "token expired or incorrect" } })
})
test("other answers go as they came", async () => {
for (const status of [200, 429]) {
const res = await send(key, () => new Response("x", { status }))
expect(res.status).toBe(status)
expect(res.headers.get("X-Magpie-Sign-In")).toBeNull()
}
})
test("a Start Plan sign-in past its end fails as errZCodeExpired", async () => {
const s = { site: "zai", jwt: jwt(Math.floor(Date.now() / 1000) - 60) }
const auth = { type: "oauth", access: s.jwt, refresh: JSON.stringify(s), expires: 0 }
await expect(send(auth, () => new Response(""))).rejects.toThrow("ZCode's sign-in has expired; sign in to ZCode again (or add the account again in magpie)")
})
+1 -1
View File
@@ -130,7 +130,7 @@ test("ZCode's Start Plan: its buckets, each for its models", async () => {
expect((await usage(auth)).windows).toEqual([{ name: "GLM-5-Turbo", used: 25, display: "50 / 200", span: 7 * 86400, models: ["GLM-5-Turbo"] }])
// its token run out
expect(await usage(oauth({ site: "zai", jwt: jwt(now - 60) }))).toEqual({ error: "ZCode's sign-in has expired; sign in again" })
expect(await usage(oauth({ site: "zai", jwt: jwt(now - 60) }))).toEqual({ error: "ZCode's sign-in has expired; sign in to ZCode again (or add the account again in magpie)" })
})
test("a team seat: the team plan's windows, name, end and resets, its key found and saved", async () => {