From 873449a2f40379014a9bed31b4abd556d70010d0 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 16:40:09 +0800 Subject: [PATCH] =?UTF-8?q?workbuddy=200.1.8:=20the=20credits=20window=20t?= =?UTF-8?q?ells=20its=20count,=20amount=20of=20limit=20in=20credits,=20bes?= =?UTF-8?q?ide=20its=20share=20and=20display=20=E2=80=94=20for=20magpie=20?= =?UTF-8?q?to=20say=20how=20many=20credits=20are=20used=20or=20left=20on?= =?UTF-8?q?=20the=20Routing=20page,=20the=20Usage=20page=20and=20the=20men?= =?UTF-8?q?u=20bar=20as=20it=20says=20the=20share=20(yetone/magpie#659).?= =?UTF-8?q?=20usage.test.mjs=20checks=20the=20count=20for=20a=20paid,=20a?= =?UTF-8?q?=20free=20and=20a=20fractional=20account;=20it=20fails=20on=20t?= =?UTF-8?q?he=20old=20code=20and=20passes.=20Tried=20with=20the=20real=20W?= =?UTF-8?q?orkBuddy=20and=20WorkBuddy=20AI=20accounts=20in=20a=20sandbox?= =?UTF-8?q?=20magpie:=2033.29=20/=203900=20and=2013.47=20/=202250=20credit?= =?UTF-8?q?s=20come=20through=20the=20host.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/workbuddy/index.mjs | 6 ++++-- packages/workbuddy/package.json | 2 +- packages/workbuddy/usage.test.mjs | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index f04f113..bc4a6cd 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -474,7 +474,9 @@ function capacity(v) { const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/0+$/, "").replace(/\.$/, "")) // usageOf is the account's credits, from its resource summary: the plan's -// credits used against what the cycle grants, as magpie's built-in said. +// credits used against what the cycle grants, as magpie's built-in said — +// the count itself too (amount of limit, in credits), for magpie to say it +// as used or left beside the share (magpie#659). function usageOf(sum, plan) { const out = { plan: plan || (sum?.IsPaidUser ? "Pro" : "Free"), windows: [] } let total = 0, used = 0 @@ -482,7 +484,7 @@ function usageOf(sum, plan) { total += capacity(p?.CycleTotalCapacity) used += capacity(p?.CycleUsedCapacity) } - if (total > 0) out.windows.push({ name: "Credits", used: (100 * used) / total, display: `${compact(used)} / ${compact(total)}` }) + if (total > 0) out.windows.push({ name: "Credits", used: (100 * used) / total, display: `${compact(used)} / ${compact(total)}`, amount: used, limit: total, unit: "credits" }) return out } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 6d0c4ac..de1eb60 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.7", + "version": "0.1.8", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs", diff --git a/packages/workbuddy/usage.test.mjs b/packages/workbuddy/usage.test.mjs index efb519e..5ec405a 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -50,7 +50,7 @@ const usageOf = async (plugin, auth, client = {}) => (await plugin({ client })). test("a paid WorkBuddy account's credits", async () => { const calls = serve({ "/billing/meter/get-user-resource-summary": () => ok(PAID) }) const auth = { type: "oauth", access: "two-access", refresh: "r", expires: later(), uid: "u2", domain: "" } - expect(await usageOf(WorkBuddyAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Pro", windows: [{ name: "Credits", used: 25, display: "2500 / 10000" }] }) + expect(await usageOf(WorkBuddyAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Pro", windows: [{ name: "Credits", used: 25, display: "2500 / 10000", amount: 2500, limit: 10000, unit: "credits" }] }) const c = calls[0] expect(c.url.origin).toBe("https://copilot.tencent.com") expect(c.init.method).toBe("POST") @@ -64,14 +64,14 @@ test("a free WorkBuddy AI account, at its own site and domain", async () => { const calls = serve({ "/billing/meter/get-user-resource-summary": () => ok({ IsPaidUser: false, Packages: [{ CycleTotalCapacity: "1000", CycleUsedCapacity: "100" }] }) }) const auth = { type: "oauth", access: "ai-access", expires: later(), uid: "ai2", domain: "www.codebuddy.ai" } - expect(await usageOf(WorkBuddyAIAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Free", windows: [{ name: "Credits", used: 10, display: "100 / 1000" }] }) + expect(await usageOf(WorkBuddyAIAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Free", windows: [{ name: "Credits", used: 10, display: "100 / 1000", amount: 100, limit: 1000, unit: "credits" }] }) expect(calls[0].url.origin).toBe("https://www.workbuddy.ai") expect(calls[0].headers.get("x-domain")).toBe("www.codebuddy.ai") }) test("counts and plans as Go says them", () => { expect(_internal.usageOf({ IsPaidUser: true, Packages: [{ CycleTotalCapacity: 3300, CycleUsedCapacity: "438.88000002" }, {}] }, "Team")) - .toEqual({ plan: "Team", windows: [{ name: "Credits", used: (100 * 438.88000002) / 3300, display: "438.88 / 3300" }] }) + .toEqual({ plan: "Team", windows: [{ name: "Credits", used: (100 * 438.88000002) / 3300, display: "438.88 / 3300", amount: 438.88000002, limit: 3300, unit: "credits" }] }) // no capacity, no window expect(_internal.usageOf({ IsPaidUser: false, Packages: [{ CycleTotalCapacity: "", CycleUsedCapacity: null }] }, "")) .toEqual({ plan: "Free", windows: [] }) @@ -132,7 +132,7 @@ test("the desktop's sign-in is renewed in memory only", async () => { }) const auth = { type: "oauth", access: "", refresh: "", expires: 0, source: "desktop", uid: "u1" } const client = { auth: { set: async (x) => sets.push(x) } } - expect(await usageOf(WorkBuddyAuthPlugin, auth, client)).toEqual({ signIn: "kept", plan: "Pro", windows: [{ name: "Credits", used: 25, display: "2500 / 10000" }] }) + expect(await usageOf(WorkBuddyAuthPlugin, auth, client)).toEqual({ signIn: "kept", plan: "Pro", windows: [{ name: "Credits", used: 25, display: "2500 / 10000", amount: 2500, limit: 10000, unit: "credits" }] }) expect(calls[1].headers.get("authorization")).toBe("Bearer desk-new") expect(calls[1].headers.get("x-domain")).toBe("www.codebuddy.cn") expect(sets).toEqual([]) // never saved,