workbuddy 0.1.19: an enterprise account's card shows its credits, from the enterprise meter WorkBuddy's usage page reads (magpie #1904, CnsMaple)

An enterprise (team) account's /billing/meter/get-user-resource-summary
answers with an empty Packages list, so the card had no window and no
error ("暂无额度数据"). WorkBuddy's own usage page reads
/billing/meter/get-enterprise-user-usage for such an account, which
answers credit / limitNum and the cycle's start and reset (UTC+8).

auth.usage now asks the enterprise meter when the summary gives no
credits, and shows a Credits window of credit used of limitNum, resetting
at cycleResetTime, with span from cycleStartTime; plan "Enterprise" when
the sign-in has none. A personal account's refusal of that meter (400
10001 "uid or enterpriseID is empty", seen on a real WorkBuddy AI account)
leaves the card as the summary said. An account with packages never asks
it.

Run: HOME=$(mktemp -d) bun test (1172 pass), bun scripts/check.mjs.
The new enterprise test uses the reporter's two answers byte for byte;
with the fallback turned off it fails (no window, plan Free). A real
personal WorkBuddy AI account's usage read through this index.mjs is
unchanged (same window, no enterprise request). Not tried with a real
enterprise account: none here.
This commit is contained in:
yetone
2026-10-13 11:39:45 +08:00
parent 7b09c423c8
commit 169e4e607c
4 changed files with 107 additions and 2 deletions
+6
View File
@@ -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:
+31 -1
View File
@@ -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 }
+1 -1
View File
@@ -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",
+69
View File
@@ -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" })
})