diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 1434c68..ae01fee 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -134,6 +134,12 @@ client reads. The daily Free Plan slice isn't counted: it ends at the same midnight on every account. magpie's smart routing spends the account whose credits run out first. +An enterprise (team) account's resource summary lists no packages. Its +credits are read from the enterprise meter +(`/billing/meter/get-enterprise-user-usage`), the one WorkBuddy's own usage +page reads: the cycle's credits used of its limit, and when the cycle +resets (magpie #1904). + ## Not here magpie's built-in WorkBuddy does more than this package: diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index f8e1a55..739e6c4 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1056,6 +1056,25 @@ function usageOf(sum, plan) { return out } +// enterpriseUsageOf is an enterprise (team) account's credits, from +// /billing/meter/get-enterprise-user-usage, the meter WorkBuddy's own usage +// page (www.workbuddy.cn/profile/usage) reads for one (magpie #1904, +// CnsMaple): the resource summary answers such an account with no +// packages at all. It counts the cycle's credits flat — credit used of +// limitNum — and when the cycle began and resets (UTC+8). null when it +// grants none. +function enterpriseUsageOf(u) { + const limit = capacity(u?.limitNum) + if (!(limit > 0)) return null + const used = capacity(u?.credit) + const w = { name: "Credits", used: (100 * used) / limit, display: `${compact(used)} / ${compact(limit)}`, amount: used, limit, unit: "credits" } + const at = (v) => (typeof v === "string" && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(v.trim()) ? Date.parse(v.trim().replace(" ", "T") + "+08:00") : 0) + const reset = at(u?.cycleResetTime), start = at(u?.cycleStartTime) + if (reset > 0) w.resetsAt = new Date(reset).toISOString() + if (reset > start && start > 0) w.span = (reset - start) / 1000 + return w +} + // 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"). @@ -1325,6 +1344,17 @@ function makePlugin(site) { // the meter has no /v2 prefix, on either site const sum = await meter(site, a, "/billing/meter/get-user-resource-summary") const out = usageOf(sum, auth.plan) + // an enterprise account's summary has no packages: its credits + // are the enterprise meter's (#1904). A personal account's + // refusal of it (10001 "uid or enterpriseID is empty") leaves + // the card as the summary said. + if (!out.windows.length) { + let ent = null + 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" } + } // 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 const credits = out.windows.find((w) => w.name === "Credits") @@ -1438,4 +1468,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } +export const _internal = { enterpriseUsageOf, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 69b2cb5..75a944c 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.18", + "version": "0.1.19", "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 85affb1..ce4461f 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -309,3 +309,72 @@ test("a detail list that can't be read leaves the card as it was", async () => { expect(u.error).toBeUndefined() expect(u.windows[0]).toEqual({ name: "Credits", used: 100 * 17.6 / 2250, display: "17.6 / 2250", amount: 17.6, limit: 2250, unit: "credits" }) }) + +// magpie #1904 (CnsMaple): an enterprise account's resource summary has no +// packages, and its credits are the enterprise meter's, which WorkBuddy's +// own usage page reads. The two answers below are the reporter's, byte for +// byte, from one token at one moment. +const ENT_SUMMARY = `{ + "code": 0, + "msg": "OK", + "data": { + "Packages": [], + "SubscriptionPackageCode": "", + "SubscriptionPackageName": "", + "IsPaidUser": false, + "IsProtectedPriceUser": false + } +}` +const ENT_USAGE = `{ + "code": 0, + "msg": "OK", + "data": { + "credit": 196.13, + "cycleStartTime": "2026-10-08 00:00:00", + "cycleEndTime": "2026-11-08 23:59:59", + "limitNum": 2000, + "cycleResetTime": "2026-11-09 00:00:00" + } +}` +// and a personal account's answer to the enterprise meter, from a real +// WorkBuddy AI Pro account on 2026-10-13 +const PERSONAL_ENT = `{"code":10001,"msg":"uid or enterpriseID is empty","requestId":"36dd0f64-cb93-41e1-a964-9e3c87fea235"}` + +test("an enterprise account's credits are the enterprise meter's, as WorkBuddy's usage page reads them (magpie #1904)", async () => { + const calls = serve({ + "/billing/meter/get-user-resource-summary": () => new Response(ENT_SUMMARY), + "/billing/meter/get-enterprise-user-usage": () => new Response(ENT_USAGE), + }) + 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)).toEqual({ signIn: "kept", plan: "Enterprise", windows: [{ + name: "Credits", used: (100 * 196.13) / 2000, display: "196.13 / 2000", amount: 196.13, limit: 2000, unit: "credits", + resetsAt: "2026-11-08T16:00:00.000Z", span: 32 * 86400 }] }) + const ent = calls.find((c) => c.url.pathname === "/billing/meter/get-enterprise-user-usage") + expect(ent.init.method).toBe("POST") + expect(ent.init.body).toBe("{}") + expect(Object.fromEntries(["authorization", "x-user-id", "x-domain", "x-enterprise-id"].map((h) => [h, ent.headers.get(h)]))) + .toEqual({ authorization: "Bearer ent-access", "x-user-id": "u9", "x-domain": "www.workbuddy.cn", "x-enterprise-id": "ent-1" }) +}) + +test("a personal account with no credits isn't given the enterprise's, and its refusal isn't the card's error", async () => { + serve({ + "/billing/meter/get-user-resource-summary": () => new Response(ENT_SUMMARY), + "/billing/meter/get-enterprise-user-usage": () => new Response(PERSONAL_ENT, { status: 400 }), + }) + const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" } + expect(await usageOf(WorkBuddyAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Free", windows: [] }) +}) + +test("a personal account with credits never asks the enterprise meter", async () => { + const calls = serve({ "/billing/meter/get-user-resource-summary": () => ok(PAID) }) + const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" } + expect((await usageOf(WorkBuddyAuthPlugin, auth)).windows[0].display).toBe("2500 / 10000") + expect(calls.some((c) => c.url.pathname === "/billing/meter/get-enterprise-user-usage")).toBe(false) +}) + +test("enterpriseUsageOf: no limit, no window; times it can't read are left out", () => { + expect(_internal.enterpriseUsageOf({ credit: 3, limitNum: 0 })).toBe(null) + expect(_internal.enterpriseUsageOf(null)).toBe(null) + 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" }) +})