diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index bd6f4bd..e1de36f 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1209,6 +1209,24 @@ function enterpriseUsageOf(u) { return w } +// spareFree has the credits windows not count the models that cost none +// (notModels): a model at x0.00 draws no credits, and with the credits +// used up WorkBuddy still serves it (deepseek-v4.1-flash at 560 / 560, +// magpie-community/plugins#138, hheei). Counted, the window at 100% put +// the account last, and behind every other, for its free models too. A +// model free only some hours (a night promotion) is not free here, as in +// the list (priceOf). A list that can't be read leaves the windows as they +// are: every model counted, as before. +async function spareFree(site, a, windows) { + if (!windows.length) return windows + let free = [] + try { + free = (await liveModels(site, a)).filter((m) => m.free).map((m) => m.id) + } catch {} + if (free.length) for (const w of windows) w.notModels = free + return windows +} + // The packages' codes, as WorkBuddy's own client lists them when it asks // for each package's credits (its COMMODITY_CODES): the API refuses a // detail list without them (10001 "PackageCodes required"). @@ -1577,7 +1595,7 @@ function makePlugin(site) { try { ent = enterpriseUsageOf(await meter(site, a, "/billing/meter/get-enterprise-user-usage")) } catch {} - if (ent) return { plan: auth.plan || "Enterprise", windows: [ent], signIn: "kept" } + if (ent) return { plan: auth.plan || "Enterprise", windows: await spareFree(site, a, [ent]), signIn: "kept" } } // when the credits left run out, for routing to spend them first; // a list that can't be read leaves it unknown, the card stands @@ -1587,6 +1605,7 @@ function makePlugin(site) { Object.assign(credits, runsOutOf(sum, await packagesOf(site, a)) ?? {}) } catch {} } + await spareFree(site, a, out.windows) return { ...out, signIn: "kept" } } catch (e) { return { error: e?.message ?? String(e), signIn: "kept" } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index ba422cf..290798f 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.22", + "version": "0.1.23", "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 ce4461f..8ada4ee 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -378,3 +378,47 @@ test("enterpriseUsageOf: no limit, no window; times it can't read are left out", expect(_internal.enterpriseUsageOf({ credit: "12.5", limitNum: "100", cycleResetTime: "soon" })) .toEqual({ name: "Credits", used: 12.5, display: "12.5 / 100", amount: 12.5, limit: 100, unit: "credits" }) }) + +// A model at x0.00 draws no credits, and with the credits used up +// WorkBuddy still serves it: hheei's account at 560 / 560 was served +// deepseek-v4.1-flash (magpie-community/plugins#138). The credits window +// leaves those models out (notModels), so magpie doesn't put the account +// last for them. One free only at night (a daily promotion) still counts. +const FREE_CFG = () => ok({ + agents: [{ name: "cli", models: ["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg", "gpt-5.5", "hy4"] }], + models: [ + { id: "deepseek-v4.1-flash", name: "Deepseek-V4.1-Flash", credits: "x0.00" }, + { id: "deepseek-v4.1-flash-sg", name: "Deepseek-V4.1-Flash", credits: "x0.03" }, + { id: "gpt-5.5", credits: "x1.00" }, + { id: "hy4", credits: "x0.50" }, + ], + modelPromotions: [{ kind: "discount", modelIds: ["hy4"], discount: { discountedCredits: "0.00x" }, schedule: { daily: [{ start: "23:00", end: "08:00" }] } }], +}) +const SPENT = { IsPaidUser: false, Packages: [{ CycleTotalCapacity: "560", CycleUsedCapacity: "560" }] } + +test("credits used up don't count the free models (plugins#138, hheei)", async () => { + for (const plugin of [WorkBuddyAuthPlugin, WorkBuddyAIAuthPlugin]) { + serve({ "/billing/meter/get-user-resource-summary": () => ok(SPENT), "/v3/config": FREE_CFG }) + const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u", domain: "" } + expect(await usageOf(plugin, auth)).toEqual({ signIn: "kept", plan: "Free", windows: [{ + name: "Credits", used: 100, display: "560 / 560", amount: 560, limit: 560, unit: "credits", notModels: ["deepseek-v4.1-flash"] }] }) + } +}) + +test("an enterprise account's credits don't count the free models either", async () => { + serve({ + "/billing/meter/get-user-resource-summary": () => new Response(ENT_SUMMARY), + "/billing/meter/get-enterprise-user-usage": () => new Response(ENT_USAGE), + "/v3/config": FREE_CFG, + }) + const auth = { type: "oauth", access: "ent-access", refresh: "r", expires: later(), uid: "u9", domain: "www.workbuddy.cn", enterpriseId: "ent-1" } + expect((await usageOf(WorkBuddyAuthPlugin, auth)).windows[0].notModels).toEqual(["deepseek-v4.1-flash"]) +}) + +test("a model list that can't be read leaves the credits counting every model", async () => { + serve({ "/billing/meter/get-user-resource-summary": () => ok(SPENT), "/v3/config": () => new Response("busy", { status: 503 }) }) + const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u", domain: "" } + const w = (await usageOf(WorkBuddyAuthPlugin, auth)).windows[0] + expect(w.used).toBe(100) + expect(w.notModels).toBeUndefined() +})