diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 0e8c714..948d57a 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -39,7 +39,6 @@ const MODELS = [ { id: "GLM-5-Turbo", context: 200_000, output: 64_000, efforts: ["none", "high"] }, ] const START_MODELS = MODELS.filter((m) => m.id !== "GLM-5.3") -const OUTPUT = 131_072 // when ZCode's config names none const SITES = { zai: { name: "Z.ai", api: ZAI_API, base: ZAI_BASE, subscribe: "z.ai/subscribe" }, @@ -975,7 +974,8 @@ const entry = (m) => ({ tool_call: true, attachment: !!m.image, modalities: { input: m.image ? ["text", "image"] : ["text"], output: ["text"] }, - limit: { context: m.context || 200_000, output: m.output || OUTPUT }, + // what ZCode's config names, else 0 (unknown), as magpie's built-in + limit: { context: m.context || 0, output: m.output || 0 }, cost: { input: 0, output: 0, cache_read: 0, cache_write: 0 }, variants: variants(m.efforts), }) @@ -1102,6 +1102,14 @@ export async function ZCodeAuthPlugin({ client }) { const auth = await getAuth() const s = stateOf(auth) if (!s) return { error: "not signed in" } + // every card starts from the plan the sign-in saved, as magpie's + // built-in did: a reply without a level, a team detail that + // failed or an error keeps it + const saved = (out) => { + const plan = s.plan || auth?.plan + if (plan && !out.plan) out.plan = plan + return out + } try { if (isTeam(s)) { let key = s.key @@ -1112,12 +1120,12 @@ export async function ZCodeAuthPlugin({ client }) { await client?.auth?.set?.({ path: { id: provider?.id ?? PROVIDER }, body: next }).catch?.(() => {}) } } - return await teamUsage(s, key) + return saved(await teamUsage(s, key)) } - if (await onStart(s)) return await startUsage(s) - return await codingUsage(s) + if (await onStart(s)) return saved(await startUsage(s)) + return saved(await codingUsage(s)) } catch (e) { - return { error: e?.message ?? String(e) } + return saved({ error: e?.message ?? String(e) }) } }, methods: [ @@ -1145,4 +1153,4 @@ export async function ZCodeAuthPlugin({ client }) { } // for tests -export const _internal = { limitWindows, termOf, startUsage, routes, teamKeys, ownSignIn, stateOf } +export const _internal = { entry, limitWindows, termOf, startUsage, routes, teamKeys, ownSignIn, stateOf } diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 141e9b6..fa6f452 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.2", + "version": "0.1.3", "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", diff --git a/packages/zcode/usage.test.mjs b/packages/zcode/usage.test.mjs index 4b0878e..23208f1 100644 --- a/packages/zcode/usage.test.mjs +++ b/packages/zcode/usage.test.mjs @@ -190,3 +190,31 @@ test("Z.ai's refusals are the card's error", async () => { expect(await usage(oauth({ site: "zai", key: "k" }))).toEqual({ error: "error 500" }) expect(await usage(undefined)).toEqual({ error: "not signed in" }) }) + +// magpie's built-in starts every card from the saved plan (zcode.go +// zcodeQuota, zcode_team.go zcodeTeamQuota, zcode_start.go zcodeStartQuota) +test("the saved plan stays on the card when the reply has no level, a team detail fails, or on an error", async () => { + serve(({ url }) => { + if (url.pathname === "/api/monitor/usage/quota/limit") return ok({ limits: [] }) + if (url.pathname === "/api/biz/subscription/list") return ok([]) + }) + const own = await usage(oauth({ site: "zai", key: "k", plan: "GLM Coding Max" })) + expect(own.plan).toBe("GLM Coding Max") + expect(own.error).toBeUndefined() + + serve(({ url }) => { + if (url.pathname === "/api/monitor/usage/quota/limit") return ok({ limits: [] }) + return new Response("", { status: 500 }) + }) + const team = { site: "bigmodel", base: "https://open.bigmodel.cn/api/anthropic", key: "tk.ts", token: "Bearer t", org: "t1", project: "tp1", plan: "GLM Coding Team Pro" } + expect((await usage(oauth(team))).plan).toBe("GLM Coding Team Pro") + + serve(() => new Response("", { status: 401 })) + expect(await usage(oauth({ site: "zai", key: "k", plan: "GLM Coding Max" }))).toEqual({ plan: "GLM Coding Max", error: "Unauthorized" }) +}) + +// the built-in left a limit ZCode's config doesn't name at 0 +test("a model ZCode's config gives no limits has none made up", () => { + expect(_internal.entry({ id: "GLM-9", context: 0, output: 0, efforts: [] }).limit).toEqual({ context: 0, output: 0 }) + expect(_internal.entry({ id: "GLM-9", context: 300_000, output: 8_000, efforts: [] }).limit).toEqual({ context: 300_000, output: 8_000 }) +})