zcode 0.1.19: a GLM Coding Plan's own resets show on the card, each with its end, a team's say so, and a Start Plan refusal says what to do, as the built-in did

What changed, for parity with magpie's built-in ZCode before it retires:

- Personal resets. The built-in's zcodeQuota read
  /api/biz/customer-package-reset/list?targetType=PERSONAL with the plan's
  key (zhipuPersonalResets, yetone/magpie#1191) and showed the count, by
  window, and each reset's end. The plugin read only the team's list.
  codingUsage now reads the personal list alongside the subscription list,
  so a Coding Plan card (and a dual gift + Coding Plan card, built on it)
  carries resets: {count, byWindow, fiveHour, weekly, until, each}.
- Team resets now carry team: true and each, as zhipuTeamResets set Team.
  Both lists are read by one resetsOf: only available ones count, each
  {until?, window} sorted soonest first with an endless one last
  (sortResetCards), nothing on a failed read or none left.
- Start Plan refusal. A Start Plan answer that is a 405, the block page
  ("request has been blocked" / errors.aliyun.com) or code 3012 / "unusual
  activity" now comes back, same status, as {type:"error", error:{message:
  "<vendor's words, or the status for a block page> — <hint>"}}, the hint
  word for word magpie's provider.ZCodeStartBlockedHint, as the built-in's
  zcodeStartExplain / Provider.Explain put it. The block page's own words
  are left out, so magpie's APIError no longer reads it as a network
  firewall's (BlockedHint). Coding Plan and team answers are untouched.
- README: the resets and the Start Plan's hint.

What was run:
- usage.test.mjs: a new test with #1191's literal reset list (the
  built-in's zhipuResetList) fails on 0.1.18 (no resets); the team test,
  now expecting team and each, fails on 0.1.18 too.
- start.test.mjs: the new refusal test fails with the startBlocked line
  disabled by hand (block page passed through), passes with it.
- Every packages/zcode test file under a scratch HOME: dual 25, gift 20,
  models 1, moved 1, own 6, signin 3, start 11, usage 16 (83 pass).
- bun scripts/check.mjs zcode passes.

Not tried: a real account's reset list or a real Start Plan refusal.
`team` and `each` reach magpie's card from its next release; older magpie
ignores them.
This commit is contained in:
yetone
2026-10-16 17:36:24 +08:00
parent 4a35799021
commit 09f946b5e6
5 changed files with 184 additions and 9 deletions
+39
View File
@@ -14,6 +14,7 @@ import { realpathSync } from "node:fs"
let ZCodeAuthPlugin, PROMPT
let server
const got = []
let answer = null // what the fake answers instead, when set
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"))
@@ -22,6 +23,7 @@ beforeAll(async () => {
port: 0,
async fetch(req) {
got.push({ method: req.method, path: new URL(req.url).pathname, headers: Object.fromEntries(req.headers), body: await req.text() })
if (answer) return answer()
return Response.json({ type: "message", content: [] })
},
})
@@ -42,6 +44,7 @@ function routeToFake() {
afterEach(() => {
globalThis.fetch = real
got.length = 0
answer = null
})
const sha = (s) => createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16)
@@ -310,3 +313,39 @@ test("a team seat's request goes as the agent sent it, ZCode's token or not", as
const auth = { type: "oauth", access: s.key, refresh: JSON.stringify(s), expires: 0 }
sentAsIs(await send(auth, agentText), "team.secret", "/api.z.ai/api/anthropic/v1/messages")
})
// magpie's provider.ZCodeStartBlockedHint, word for word
const START_BLOCKED = "ZCode's Start Plan still turned this request away, though magpie sends it as the ZCode app does; it can be a network block of this IP, or ZCode checking for something new. Use an account with a GLM Coding Plan, or add another provider to this group"
test("the Start Plan turning a request away says what to do about it, as the built-in's zcodeStartExplain did (#425)", async () => {
const ask = async (auth, url = "https://api.z.ai/api/anthropic/v1/messages") => {
routeToFake()
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
const opts = await hooks.auth.loader(async () => auth, { id: "zcode" })
const res = await opts.fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "zcode" }, body: agentText })
return { status: res.status, text: await res.text(), signIn: res.headers.get("x-magpie-sign-in") }
}
// the block page zcode.z.ai serves: its status, then the hint, and none
// of the page's words, which magpie would read as a firewall's
answer = () => new Response("<html>Sorry, your request has been blocked due to unusual activity.</html>", { status: 405, headers: { "content-type": "text/html" } })
let r = await ask(start("zai"))
expect(r.status).toBe(405)
expect(JSON.parse(r.text)).toEqual({ type: "error", error: { type: "permission_error", message: "405 Method Not Allowed — " + START_BLOCKED } })
expect(r.text).not.toMatch(/request has been blocked/)
expect(r.signIn).toBe("kept")
// code 3012 in JSON: the vendor's own words, then the hint
answer = () => Response.json({ code: 3012, msg: "method not allowed" }, { status: 403 })
r = await ask(start("zai"))
expect(r.status).toBe(403)
expect(JSON.parse(r.text).error.message).toBe("method not allowed — " + START_BLOCKED)
// any other refusal of the Start Plan goes on as it came
answer = () => Response.json({ type: "error", error: { type: "overloaded_error", message: "busy" } }, { status: 529 })
r = await ask(start("zai"))
expect(r.status).toBe(529)
expect(JSON.parse(r.text).error.message).toBe("busy")
// a GLM Coding Plan's 405 is not the Start Plan's, and says nothing of it
answer = () => new Response("request has been blocked", { status: 405 })
r = await ask({ type: "api", key: "one.secret", metadata: { site: "zai" } })
expect(r.status).toBe(405)
expect(r.text).toBe("request has been blocked")
})