zcode 0.1.42: a plain GLM model spends a live ZCode gift first, by default (sgfvdsg86265 on X)

The request (X, 2117210897471169009): when the account has gift quota
for a GLM model, which ZCode hands out every weekend, spend that first.
freeFirst (#505) already does it, but only when it was turned on, so a
gift lapsed unused behind an agent that asks for the plain model. It is
now on unless the plugin's options or the provider's say
`"freeFirst": false`, which are still honoured as they are. Every way
the gift can fail (spent, a model it doesn't serve, no ZCode session, a
refusal's minute of rest) still falls back to the coding plan, as
before. A team seat's plain model spends the personal gift first too.

The tests that are about the plan and the -Trial picks now build the
plugin with freeFirst off, as they assumed it was.

Run on mote: HOME=$(mktemp -d) bun test in packages/zcode, 187 pass;
bun scripts/check.mjs zcode ok.
Broken by hand: with the default back to off, "free first is on when
nothing sets it" and "free first is on by default for a team seat"
fail; the rest pass.
Not tried: a real account with a claimed gift. The owner's account has
only an unclaimed one, which wasn't claimed for this.
This commit is contained in:
yetone
2026-11-02 19:13:09 +08:00
parent 731277004a
commit b820704803
5 changed files with 51 additions and 11 deletions
+7 -6
View File
@@ -136,14 +136,15 @@ never read or buffered on the way.
**Free first.** Picking `-Trial` by hand keeps the choice the user's,
but an agent that always asks the plain model never sees the gift.
`freeFirst` turns the preference around: the plain model spends the
account's own live gift first, and falls back to the coding plan when
the gift cannot serve it — a bucket that is spent, a model the gift
does not serve, no ZCode session, or the minute's rest a refusal just
earned. Turn it on for the whole plugin in magpie's Plugins › Options
`freeFirst`, on by default since 0.1.42, turns the preference around:
the plain model spends the account's own live gift first, and falls
back to the coding plan when the gift cannot serve it — a bucket that
is spent, a model the gift does not serve, no ZCode session, or the
minute's rest a refusal just earned. Turn it off for the whole plugin
in magpie's Plugins › Options
```json
{ "freeFirst": true }
{ "freeFirst": false }
```
or on the provider alone, under `provider.zcode.options` in the config
+25 -1
View File
@@ -54,7 +54,9 @@ function planAndBalance() {
answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }])
answers["/api/v1/zcode-plan/billing/balance"] = ok(balance)
}
const hooks = async () => (await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })).auth
// these tests are about the plan and the -Trial picks, so freeFirst (on by
// default) is off here; the freeFirst tests below build their own
const hooks = async () => (await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }, { freeFirst: false })).auth
const loader = async () => (await hooks()).loader(async () => coding, { id: "zcode" })
async function sendTo(url, body) {
const opts = await loader()
@@ -137,6 +139,28 @@ test("without freeFirst the plain model goes to the coding plan as before", asyn
expect(JSON.parse(sent.at(-1).body).model).toBe("GLM-5.3-Flash")
})
// freeFirst is on by default: a plugin and provider that say nothing spend
// the gift first; false on either keeps the plain model on the plan.
test("free first is on when nothing sets it", async () => {
planAndBalance()
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] })
const opts = await freePlugin().then((h) => h.loader(async () => coding, { id: "zcode" }))
const res = await opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body("GLM-5.3-Flash") })
expect(res.status).toBe(200)
expect(sent.at(-1).origin).toBe("https://zcode.z.ai")
expect(JSON.parse(sent.at(-1).body).model).toBe("glm-5.3-flash")
})
test("freeFirst false on the provider keeps the plan, over a plugin that says nothing", async () => {
planAndBalance()
answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] })
const opts = await freePlugin().then((h) => h.loader(async () => coding, { id: "zcode", options: { freeFirst: false } }))
const res = await opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body("GLM-5.3-Flash") })
expect(res.status).toBe(200)
expect(sent.at(-1).origin).toBe("https://api.z.ai")
expect(JSON.parse(sent.at(-1).body).model).toBe("GLM-5.3-Flash")
})
test("free first with no live gift reads the balance once and stays on the plan", async () => {
answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }])
answers["/api/v1/zcode-plan/billing/balance"] = ok({ server_time: now, plans: [], balances: [] })
+5 -2
View File
@@ -2241,9 +2241,12 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
}
// freeFirst reads the option from the provider config's options first,
// and from the plugin's own (magpie's Plugins › Options) when the
// provider carries no freeFirst of its own.
// provider carries no freeFirst of its own. It is on unless one of them
// says false: a gift ZCode hands out (each weekend, sgfvdsg86265 on X)
// lapses unused behind an agent that asks the plain model, and every
// failure of the gift is answered from the plan.
const freeFirst = (providerOptions) =>
providerOptions?.freeFirst !== undefined ? providerOptions.freeFirst === true : options?.freeFirst === true
providerOptions?.freeFirst !== undefined ? providerOptions.freeFirst !== false : options?.freeFirst !== false
const oauth = (site) => ({
type: "oauth",
label: `ZCode: ${SITES[site].name}${site === "bigmodel" ? " (智谱)" : ""} GLM Coding Plan`,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.41",
"version": "0.1.42",
"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": "./index.mjs",
+13 -1
View File
@@ -39,7 +39,9 @@ beforeEach(() => {
}
})
afterEach(() => { globalThis.fetch = real })
const hooks = () => ZCodeAuthPlugin({ client: {} })
// the team plan and the -Trial picks, with freeFirst (on by default) off;
// "free first is on by default for a team seat" builds its own
const hooks = (options = { freeFirst: false }) => ZCodeAuthPlugin({ client: {} }, options)
test("a team seat lists personal gift trials even without a resolved team key", async () => {
const h = await hooks()
@@ -96,3 +98,13 @@ test("ordinary models spend the team; trials spend the personal gift without tea
expect(messages[1].headers.get("Bigmodel-Organization")).toBeNull()
expect(JSON.parse(messages[1].body).model).toBe("glm-5.3")
})
test("free first is on by default for a team seat: the plain model spends the personal gift", async () => {
const h = await hooks({})
const loader = await h.auth.loader(async () => auth(), { id: "zcode" })
expect((await loader.fetch(state.base + "/v1/messages", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "GLM-5.3", max_tokens: 1, messages: [{ role: "user", content: "hi" }] }) })).status).toBe(200)
const messages = sent.filter((x) => x.url.pathname.endsWith("/messages"))
expect(messages).toHaveLength(1)
expect(messages[0].url.origin).toBe("https://zcode.z.ai")
expect(messages[0].headers.get("x-api-key")).toBeNull()
})