diff --git a/packages/zcode/README.md b/packages/zcode/README.md index d3e0d4f..99b8c3c 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -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 diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs index 725473d..691cb3e 100644 --- a/packages/zcode/dual.test.mjs +++ b/packages/zcode/dual.test.mjs @@ -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: [] }) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 6df8ca5..8a97a55 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -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`, diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 7652248..2a664c9 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -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", diff --git a/packages/zcode/team-gift.test.mjs b/packages/zcode/team-gift.test.mjs index 7aae37e..f2c3626 100644 --- a/packages/zcode/team-gift.test.mjs +++ b/packages/zcode/team-gift.test.mjs @@ -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() +})