From 9da5cd3426b41ed48098a869d96c928ed148e457 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 26 Oct 2026 15:00:30 +0800 Subject: [PATCH] workbuddy 0.1.35: used credits say when the plan's cycle starts again (windy on Discord) windy's WorkBuddy card in magpie showed 46% of the credits used and, beside it, "Not used yet". The Credits window had no resetsAt, and magpie's card says "Not used yet" when no used window has a reset. WorkBuddy's own client (5.5.6, getCurrentPlan) says the plan refreshes at the cycle end of the package the summary names as the subscription (SubscriptionPackageCode). cycleOf now gives the Credits window that reset (CycleEndTime, UTC+8) and the cycle's span, from the paid and free lists usage already reads for runsOutOf; none named, not listed or a cycle already over leaves it unknown, as before. Run: bun test in packages/workbuddy, sandbox HOME, fetch stubbed: 154 pass 0 fail. On the old index.mjs the two new tests fail (resetsAt undefined; cycleOf's cases). bun scripts/check.mjs: workbuddy ok. Real: the summary and paid list of a real WorkBuddy AI Pro account (2026-10-26, read-only, no renewal) through the new code give Credits 89.2 / 2000, resetsAt 2026-10-28T16:40:52Z, span 30d-1s. Not tried: a China-site (copilot.tencent.com) account: the desktop sign-in on this machine is refused (401), so a CN free plan's SubscriptionPackageCode wasn't seen; without one the card stays as it was. magpie's card saying "Not used yet" beside a used window without a reset is fixed in magpie itself. --- packages/workbuddy/index.mjs | 28 +++++++++++++++++--- packages/workbuddy/package.json | 2 +- packages/workbuddy/usage.test.mjs | 44 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 62a0c9a..740bdf4 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1719,6 +1719,26 @@ function runsOutOf(sum, packages, now = Date.now()) { return at ? { runsOutAt: new Date(at).toISOString(), runsOut: amount } : null } +// cycleOf is when the plan's credits start again, as WorkBuddy's own client +// says it (getCurrentPlan's refreshAt): the cycle end of the package the +// summary names as the subscription (SubscriptionPackageCode), in UTC+8, +// with how long the cycle is. Without it the card had used credits and no +// reset, and magpie said "Not used yet" beside 46% (windy on Discord, +// 2026-10-26). null when the subscription isn't in the lists, or its cycle +// is over. +function cycleOf(sum, packages, now = Date.now()) { + const code = sum?.SubscriptionPackageCode + if (!code) return null + 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) + for (const p of packages ?? []) { + if (p?.PackageCode !== code) continue + const end = at(p?.CycleEndTime), start = at(p?.CycleStartTime) + if (!(end > now)) continue + return { resetsAt: new Date(end).toISOString(), ...(start > 0 && start < end ? { span: (end - start) / 1000 } : {}) } + } + return null +} + // ---- models ----------------------------------------------------------------- // variants are the reasoning levels as OpenCode gives them to the AI SDK. @@ -2159,12 +2179,14 @@ function makePlugin(site) { } catch {} if (ent) return { plan: auth.plan || "Enterprise", windows: await spareFree(site, a, [ent]), signIn: said } } - // when the credits left run out, for routing to spend them first; + // when the plan's credits start again (cycleOf), and when the credits + // left run out, for routing to spend them first (runsOutOf); // a list that can't be read leaves it unknown, the card stands const credits = out.windows.find((w) => w.name === "Credits") if (credits) { try { - Object.assign(credits, runsOutOf(sum, await packagesOf(site, a)) ?? {}) + const packages = await packagesOf(site, a) + Object.assign(credits, cycleOf(sum, packages) ?? {}, runsOutOf(sum, packages) ?? {}) } catch {} } await spareFree(site, a, out.windows) @@ -2291,4 +2313,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, 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, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, answeredBefore, nativeOf, modelOf, configModels, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } +export const _internal = { cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, 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, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, answeredBefore, nativeOf, modelOf, configModels, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 87742ce..585ba58 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.34", + "version": "0.1.35", "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 83a0091..99e8ecb 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -302,6 +302,50 @@ test("runsOutOf: soonest end of credits left, by the summary's packages, never t expect(_internal.runsOutOf(AI_SUMMARY, AI_FREE.Accounts, Date.parse("2026-11-01T00:00:00Z"))).toBeNull() }) +// 2026-10-26, a real WorkBuddy AI Pro account (ids left out): the summary +// names its subscription, and the paid list has that package's cycle. With +// no reset on the window, magpie's card said "Not used yet" beside 46% +// (windy on Discord); WorkBuddy's own client says the plan refreshes at +// the cycle's end. +const LIVE_SUMMARY = { Packages: [ + { PackageCode: "TCACA_code_002_AkiJS3ZHF5", CycleTotalCapacity: "1000", CycleRemainCapacity: "910.80000003", CycleUsedCapacity: "89.19999997", CycleFrozenCapacity: "0", CapacityUnit: "credit", TotalCount: 1 }, + { PackageCode: "TCACA_code_037_WxOD3MpI2o", CycleTotalCapacity: "1000", CycleRemainCapacity: "1000", CycleUsedCapacity: "0", CycleFrozenCapacity: "0", CapacityUnit: "credits", TotalCount: 1 }, +], TotalCredits: "1910.80000003", SubscriptionPackageCode: "TCACA_code_002_AkiJS3ZHF5", SubscriptionPackageName: "Pro Monthly", IsPaidUser: true } +const LIVE_PAID = { TotalCount: 1, Accounts: [ + { PackageCode: "TCACA_code_002_AkiJS3ZHF5", PackageName: "Pro Plan Monthly Subscription", CapacityType: 2, CycleStartTime: "2026-09-29 00:40:53", CycleEndTime: "2026-10-29 00:40:52", + DeductionStartTime: 1790613653000, DeductionEndTime: 1793205652000, ExpiredTime: "", CycleCapacitySizePrecise: "1000", CycleCapacityRemainPrecise: "910.80000003", CycleCapacityUsedPrecise: "89.19999997", Status: 0 }, +] } +const LIVE_FREE = { TotalCount: 1, Accounts: [ + { PackageCode: "TCACA_code_037_WxOD3MpI2o", PackageName: "Plan Bonus Quota", CapacityType: 2, CycleStartTime: "2026-09-29 00:40:53", CycleEndTime: "2026-10-29 00:40:52", + DeductionStartTime: 1790613653000, DeductionEndTime: 1793205652000, ExpiredTime: "", CycleCapacitySizePrecise: "1000", CycleCapacityRemainPrecise: "1000", CycleCapacityUsedPrecise: "0", Status: 0 }, +] } + +test("used credits say when the plan's cycle starts again, as WorkBuddy's client does (windy on Discord)", async () => { + // the same lists ten years on, so the cycle is still to come + const on = (d) => ({ ...d, Accounts: d.Accounts.map((p) => ({ ...p, DeductionEndTime: p.DeductionEndTime + Date.UTC(2036, 9, 1) - Date.UTC(2026, 9, 1), + CycleStartTime: "2036" + p.CycleStartTime.slice(4), CycleEndTime: "2036" + p.CycleEndTime.slice(4) })) }) + serve({ + "/billing/meter/get-user-resource-summary": () => ok(LIVE_SUMMARY), + "/billing/meter/get-user-resource-paid-packages": () => ok(on(LIVE_PAID)), + "/billing/meter/get-user-resource-free-packages": () => ok(on(LIVE_FREE)), + }) + const u = await usageOf(WorkBuddyAIAuthPlugin, { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" }) + const w = u.windows.find((x) => x.name === "Credits") + expect(w.amount).toBeCloseTo(89.2, 5) + expect(w.resetsAt).toBe("2036-10-28T16:40:52.000Z") + expect(w.span).toBe(30 * 86400 - 1) +}) + +test("cycleOf: the subscription's cycle end in UTC+8; none named, not listed or over is unknown", () => { + const now = Date.parse("2026-10-26T06:00:00Z") + expect(_internal.cycleOf(LIVE_SUMMARY, LIVE_PAID.Accounts, now)).toEqual({ resetsAt: "2026-10-28T16:40:52.000Z", span: 30 * 86400 - 1 }) + expect(_internal.cycleOf({ ...LIVE_SUMMARY, SubscriptionPackageCode: "" }, LIVE_PAID.Accounts, now)).toBeNull() + expect(_internal.cycleOf(LIVE_SUMMARY, LIVE_FREE.Accounts, now)).toBeNull() + expect(_internal.cycleOf(LIVE_SUMMARY, LIVE_PAID.Accounts, Date.parse("2026-11-01T00:00:00Z"))).toBeNull() + // a start it can't read leaves the span out, the reset stays + expect(_internal.cycleOf(LIVE_SUMMARY, [{ ...LIVE_PAID.Accounts[0], CycleStartTime: "" }], now)).toEqual({ resetsAt: "2026-10-28T16:40:52.000Z" }) +}) + test("a detail list that can't be read leaves the card as it was", async () => { serve({ "/billing/meter/get-user-resource-summary": () => ok(AI_SUMMARY),