workbuddy 0.1.41: a package with no cycle total reads as a wallet balance (#450)

A WorkBuddy package with no CycleTotalCapacity is a prepaid wallet: it is sent as a balance window ("Prepaid credits", magpie#3950), beside the share of packages that have a total. A literal "0" total stays on the old path, so a lapsed package adds nothing; an account with no packages keeps its unknown allowance (no TotalCredits fallback).

Reviewed at 5bdfa9048d66095d35af5e7955d0945db02dc3b5 over four rounds. Run on mote at that head: HOME=$(mktemp -d) bun test packages/workbuddy, 185 pass / 0 fail; bun scripts/check.mjs workbuddy ok. npm had 0.1.40, so 0.1.41 is free.

Co-authored-by: MYCF711 <MYCF711@users.noreply.github.com>
This commit is contained in:
MYCF711
2026-10-28 10:26:47 +08:00
committed by GitHub
co-authored by MYCF711
parent fd40b20c3b
commit e460dae2f0
3 changed files with 51 additions and 4 deletions
+20 -3
View File
@@ -1738,14 +1738,31 @@ const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/
// as used or left beside the share (magpie#659). The free plan is said as a
// plan: "Free" alone beside the account read as the model being free, and
// routing as preferring it for that (#337).
//
// A package with no CycleTotalCapacity has no denominator: it is sent as
// magpie's balance-shaped window (#3950), {balance, unit} with no used,
// limit or aside, and routing ranks it by what is left. A literal "0" total
// is still a total, not a wallet: it stays on the old path, so a lapsed or
// placeholder package adds nothing. A literal "0" remainder is a reading
// of nothing left, not an absent field; only a package with no
// CycleRemainCapacity at all leaves the window out. TotalCredits is not a
// fallback: an account with no packages at all keeps its unknown allowance,
// as before, so its free models are not skipped.
function usageOf(sum, plan) {
const out = { plan: plan || (sum?.IsPaidUser ? "Pro" : "Free plan"), windows: [] }
let total = 0, used = 0
let total = 0, used = 0, left = 0, bags = 0
for (const p of sum?.Packages ?? []) {
total += capacity(p?.CycleTotalCapacity)
used += capacity(p?.CycleUsedCapacity)
const hasTotal = p?.CycleTotalCapacity != null && p.CycleTotalCapacity !== ""
if (hasTotal) {
total += capacity(p.CycleTotalCapacity)
used += capacity(p?.CycleUsedCapacity)
} else if (p?.CycleRemainCapacity != null && p.CycleRemainCapacity !== "") {
left += capacity(p.CycleRemainCapacity)
bags++
}
}
if (total > 0) out.windows.push({ name: "Credits", used: (100 * used) / total, display: `${compact(used)} / ${compact(total)}`, amount: used, limit: total, unit: "credits" })
if (bags > 0 && (total === 0 || left > 0)) out.windows.push({ name: "Prepaid credits", balance: left, display: `${compact(left)} credits`, unit: "credits" })
return out
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-workbuddy-auth",
"version": "0.1.40",
"version": "0.1.41",
"description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)",
"type": "module",
"main": "./index.mjs",
+30
View File
@@ -78,6 +78,36 @@ test("counts and plans as Go says them", () => {
expect(() => _internal.usageOf({ Packages: [{ CycleTotalCapacity: "lots" }] })).toThrow('strconv.ParseFloat: parsing "lots": invalid syntax')
})
test("a prepaid credit package with no total is a balance reading (#3950)", () => {
expect(_internal.usageOf({ IsPaidUser: false, Packages: [{ CycleTotalCapacity: "", CycleUsedCapacity: "", CycleRemainCapacity: "750" }] }, ""))
.toEqual({ plan: "Free plan", windows: [{ name: "Prepaid credits", balance: 750, display: "750 credits", unit: "credits" }] })
// a literal "0" is a reading of nothing left, not an absent field
expect(_internal.usageOf({ Packages: [{ CycleTotalCapacity: "", CycleRemainCapacity: "0" }] }, ""))
.toEqual({ plan: "Free plan", windows: [{ name: "Prepaid credits", balance: 0, display: "0 credits", unit: "credits" }] })
// no remainder field at all: unknown, not a balance
expect(_internal.usageOf({ Packages: [{ CycleTotalCapacity: "", CycleRemainCapacity: "" }] }, ""))
.toEqual({ plan: "Free plan", windows: [] })
// a "0" total is still a total (a lapsed or placeholder package): old path,
// no balance reading, so the account stays unknown as on main
expect(_internal.usageOf({ Packages: [{ CycleTotalCapacity: "0", CycleRemainCapacity: "0" }] }, ""))
.toEqual({ plan: "Free plan", windows: [] })
// one package with a total and one without: the latter keeps its own reading
expect(_internal.usageOf({ Packages: [
{ CycleTotalCapacity: "1000", CycleUsedCapacity: "100" },
{ CycleTotalCapacity: "", CycleRemainCapacity: "750" },
] }, ""))
.toEqual({ plan: "Free plan", windows: [
{ name: "Credits", used: 10, display: "100 / 1000", amount: 100, limit: 1000, unit: "credits" },
{ name: "Prepaid credits", balance: 750, display: "750 credits", unit: "credits" },
] })
// TotalCredits alone is not a reading: an account with no packages stays
// unknown, so its free models are not skipped
expect(_internal.usageOf({ Packages: [], TotalCredits: "120" }, ""))
.toEqual({ plan: "Free plan", windows: [] })
expect(_internal.usageOf({ Packages: [], TotalCredits: "0" }, ""))
.toEqual({ plan: "Free plan", windows: [] })
})
test("WorkBuddy's refusals are the card's error", async () => {
const auth = { type: "oauth", access: "a", expires: later(), uid: "u" }
// a bare 401 (no invalid_token) with nothing to renew with: said, not marked