diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 904f10d..b3bc1c4 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -349,15 +349,19 @@ midnight on every account. magpie's smart routing spends the account whose credits run out first. The plan's own credits and the rest are two windows. "Credits" is the -packages whose credits come back each cycle, as WorkBuddy's client lists -them (Pro monthly or yearly, youth, advanced, flagship, the plan's bonus, -the free plan's monthly base, the Pro trials, or the subscription the -summary names), with the plan's cycle, so its pace line is the month's. -"Extra credits" is the packages that only lapse: gifted and activity -credits (赠送积分) and bought credit packs. Only one of the two stops the +plan's base credits: what the subscription grants each cycle (Pro monthly +or yearly, youth, advanced, flagship, the Pro trials, the free plan's +monthly base, or the subscription the summary names), with the plan's +cycle, so its pace line is the month's. "Extra credits" is every other +kind WorkBuddy's own usage panel lists: the plan's bonus, top-ups (bought +credit packs) and platform rewards (gifted and activity credits, +赠送积分). The summary's CreditGroups say which is which (plan_base +against plan_bonus, addon and benefit); a summary without them is split +by package code as WorkBuddy's client does. Only one of the two stops the account, since WorkBuddy serves it while any credits are left: the extra window is set aside until the plan's credits are spent. A summary with -only one kind keeps the one "Credits" window (akic404 on Discord). +only one kind keeps the one "Credits" window (akic404 on Discord; since +0.2.16 the plan's bonus is extra too, Jiabubu on Discord). An enterprise (team) account's resource summary lists no packages. Its credits are read from the enterprise meter diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 4e7082e..86ecdc3 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -2868,12 +2868,15 @@ function pinning() { // The plan's own credits and the rest are two windows (akic404 on // Discord): summed, the gifted and activity packages' credits used long // ago put the card at most of its allowance on the first day of the -// month, and the plan's pace line said nothing. "Credits" is the packages -// whose credits come back each cycle — WorkBuddy's client's own -// onlyRefreshCodes (PLAN_CODES), or the subscription the summary names — -// and "Extra credits" the ones that only lapse (gift, activity, bought -// credit packs, any code the client doesn't list). A summary with only -// one kind keeps the one "Credits" window, as before. +// month, and the plan's pace line said nothing. "Credits" is the plan's +// base credits and "Extra credits" every other kind, as WorkBuddy's own +// usage panel breaks them down (Jiabubu on Discord: a plan's bonus and +// bought credits were nowhere on the card). The summary's CreditGroups +// say it in WorkBuddy's words: plan_base ("Plan Base Credits") is the +// plan's, plan_bonus, addon ("Top-up Credits") and benefit ("Platform +// Reward Credits") are extra. A summary without them is split by code as +// the client's resolveCreditGroup does (PLAN_CODES, see planCodes). A +// summary with only one kind keeps the one "Credits" window, as before. // // Only one of the two stops the account at a time: WorkBuddy serves it // while any credits are left, so the one set aside is the one whose @@ -2884,12 +2887,13 @@ function usageOf(sum, plan, site) { const ofPlan = planCodes(sum, site) const kinds = { plan: { total: 0, used: 0 }, extra: { total: 0, used: 0 } } let left = 0, bags = 0 + const hasTotal = (p) => p?.CycleTotalCapacity != null && p.CycleTotalCapacity !== "" + const add = (k, p) => ((k.total += capacity(p.CycleTotalCapacity)), (k.used += capacity(p?.CycleUsedCapacity))) + const groups = (Array.isArray(sum?.CreditGroups) ? sum.CreditGroups : []).filter(hasTotal) + for (const g of groups) add(kinds[g?.Kind === "plan_base" ? "plan" : "extra"], g) for (const p of sum?.Packages ?? []) { - const hasTotal = p?.CycleTotalCapacity != null && p.CycleTotalCapacity !== "" - if (hasTotal) { - const k = kinds[ofPlan(p?.PackageCode) ? "plan" : "extra"] - k.total += capacity(p.CycleTotalCapacity) - k.used += capacity(p?.CycleUsedCapacity) + if (hasTotal(p)) { + if (!groups.length) add(kinds[ofPlan(p?.PackageCode) ? "plan" : "extra"], p) } else if (p?.CycleRemainCapacity != null && p.CycleRemainCapacity !== "") { left += capacity(p.CycleRemainCapacity) bags++ @@ -2953,7 +2957,7 @@ async function spareFree(site, a, windows) { // detail list without them (10001 "PackageCodes required"). const CODE = (n, s) => `TCACA_code_${n}_${s}` const PAID_CODES = [CODE("002", "AkiJS3ZHF5"), CODE("005", "maRGyrHhw1"), CODE("003", "FAnt7lcmRT"), CODE("023", "4xbGhMrE6q"), - CODE("026", "BaESVICNoi"), CODE("027", "0FCGVA6vSa"), CODE("009", "0XmEQc2xOf"), CODE("038", "OhvqZtiPKr"), CODE("036", "lupO5WgNdG")] + CODE("026", "BaESVICNoi"), CODE("027", "0FCGVA6vSa"), CODE("043", "6oIkxT7Z0S"), CODE("009", "0XmEQc2xOf"), CODE("038", "OhvqZtiPKr"), CODE("036", "lupO5WgNdG")] const DAILY_CODE = CODE("001", "PqouKr6QWV") const FREE_CODES = [DAILY_CODE, CODE("008", "cfWoLwvjU4"), CODE("035", "ArVxJcGDsm"), CODE("006", "DbXS0lrypC"), CODE("039", "KRcQj7wUat"), CODE("040", "mi9rCYg46x"), CODE("007", "nzdH5h4Nl0"), CODE("028", "NtpWi0jzXs"), CODE("037", "WxOD3MpI2o"), CODE("029", "6wCGEWquYy"), @@ -2962,17 +2966,18 @@ const FREE_CODES = [DAILY_CODE, CODE("008", "cfWoLwvjU4"), CODE("035", "ArVxJcGD // lost at the same midnight on every account, so they tell none apart const SLICE = "4" -// PLAN_CODES are the packages whose credits come back each cycle, as -// WorkBuddy's client (5.9.x) lists them (onlyRefreshCodes): Pro monthly, -// Pro monthly plus, Pro yearly, youth, advanced, flagship, the plan's -// bonus (bonus28; bonusIntl, "Plan Bonus Quota"), the international free -// plan (freeMonIntl), the Pro trials, and on the China build alone the -// free plan's monthly base (freeMon, 套餐基础积分). Every other code only -// lapses: gift (006, "Bonus Pack" / Pro trial), activity (007, 赠送积分), -// bonus29/30, and the credit packs (009, 036, 038). +// PLAN_CODES are the plan's base credits, as WorkBuddy's client (WorkBuddy +// AI 5.9.4) groups them in its usage panel (resolveCreditGroup's +// planBase): Pro monthly, Pro monthly plus, Pro yearly, youth, advanced, +// flagship, buddyOneVip, the international free plan (freeMonIntl), the +// Pro trials, and on the China build alone the free plan's monthly base +// (freeMon, 套餐基础积分). The plan's bonus (bonus28; bonusIntl, "Plan +// Bonus Quota") comes back each cycle too, but the client says it apart +// (planBonus), as it does the credit packs (009, 036, 038: addon) and +// activity, bonus29/30 and gift (benefit). const PLAN_CODES = [CODE("002", "AkiJS3ZHF5"), CODE("005", "maRGyrHhw1"), CODE("003", "FAnt7lcmRT"), CODE("023", "4xbGhMrE6q"), - CODE("026", "BaESVICNoi"), CODE("027", "0FCGVA6vSa"), CODE("028", "NtpWi0jzXs"), CODE("035", "ArVxJcGDsm"), - CODE("039", "KRcQj7wUat"), CODE("040", "mi9rCYg46x"), CODE("037", "WxOD3MpI2o")] + CODE("026", "BaESVICNoi"), CODE("027", "0FCGVA6vSa"), CODE("043", "6oIkxT7Z0S"), CODE("035", "ArVxJcGDsm"), + CODE("039", "KRcQj7wUat"), CODE("040", "mi9rCYg46x")] const FREE_MON = CODE("008", "cfWoLwvjU4") // planCodes says whether a package of the summary is the plan's own: one diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 2f34123..fc229bb 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.15", + "version": "0.2.16", "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 32df388..56ae57e 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -428,10 +428,10 @@ test("a detail list that can't be read leaves the card as it was", async () => { }) const u = await usageOf(WorkBuddyAIAuthPlugin, { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" }) expect(u.error).toBeUndefined() - // the plan's (Pro, Plan Bonus) and the Bonus Pack apart, as usageOf says them + // the plan's base (Pro) apart from its Plan Bonus and the Bonus Pack, as usageOf says them expect(u.windows.slice(0, 2)).toEqual([ - { name: "Credits", used: 0, display: "0 / 2000", amount: 0, limit: 2000, unit: "credits" }, - { name: "Extra credits", used: 100 * 17.6 / 250, display: "17.6 / 250", amount: 17.6, limit: 250, unit: "credits", aside: true }, + { name: "Credits", used: 0, display: "0 / 1000", amount: 0, limit: 1000, unit: "credits" }, + { name: "Extra credits", used: 100 * 17.6 / 1250, display: "17.6 / 1250", amount: 17.6, limit: 1250, unit: "credits", aside: true }, ]) }) @@ -448,11 +448,12 @@ test("the plan's credits and the gifted ones are two windows, the plan's with it { name: "Credits", used: 0, display: "0 / 500", amount: 0, limit: 500, unit: "credits" }, { name: "Extra credits", used: (100 * 281.9999965) / 2900, display: "282 / 2900", amount: 281.9999965, limit: 2900, unit: "credits", aside: true }, ]) - // WorkBuddy AI: Pro and its Plan Bonus are the plan's, the Bonus Pack (006) is not + // WorkBuddy AI: Pro is the plan's base; its Plan Bonus (037) and the + // Bonus Pack (006) are extra, as WorkBuddy's usage panel groups them expect(_internal.usageOf(AI_SUMMARY, "", SITES["workbuddy-ai"]).windows.map((w) => [w.name, w.display, !!w.aside])) - .toEqual([["Credits", "0 / 2000", false], ["Extra credits", "17.6 / 250", true]]) - // a summary of the plan's packages alone keeps the one window, as before - expect(_internal.usageOf(LIVE_SUMMARY, "", SITES["workbuddy-ai"]).windows.map((w) => w.name)).toEqual(["Credits"]) + .toEqual([["Credits", "0 / 1000", false], ["Extra credits", "17.6 / 1250", true]]) + // a summary of the plan's base alone keeps the one window, as before + expect(_internal.usageOf({ ...LIVE_SUMMARY, Packages: LIVE_SUMMARY.Packages.slice(0, 1) }, "", SITES["workbuddy-ai"]).windows.map((w) => w.name)).toEqual(["Credits"]) // and one of packages the client names nothing for (or none at all) too expect(_internal.usageOf(PAID, "", SITES.workbuddy).windows.map((w) => [w.name, w.display])).toEqual([["Credits", "2500 / 10000"]]) // the plan's spent with extra ones left: those keep the account going, so @@ -471,6 +472,51 @@ test("the plan's credits and the gifted ones are two windows, the plan's with it expect(_internal.planCodes({ SubscriptionPackageCode: "" }, SITES.workbuddy)("")).toBe(false) }) +// Jiabubu on Discord (magpie 0.1.2738): a WorkBuddy account's card showed +// the plan's credits only; its extra ones (a plan's bonus, bought, promo) +// were nowhere. WorkBuddy's own usage panel (WorkBuddy AI 5.9.4, +// resolveCreditGroup) has four kinds: the plan's base, the plan's bonus +// (bonus28, bonusIntl), top-ups (009, 036, 038) and platform rewards +// (activity, bonus29/30; gift and freeMon on WorkBuddy AI). The summary +// says the same four as CreditGroups. Its bytes below are a real WorkBuddy +// AI free account's, 2026-11-03 (ids left out). +const GROUPS_SUMMARY = { Packages: [ + { PackageCode: "TCACA_code_035_ArVxJcGDsm", CycleTotalCapacity: "100", CycleRemainCapacity: "99.81", CycleUsedCapacity: "0.19", CycleFrozenCapacity: "0", CapacityUnit: "credits", TotalCount: 1 }, +], TotalCredits: "99.81", SubscriptionPackageCode: "", SubscriptionPackageName: "Free", IsPaidUser: false, IsProtectedPriceUser: false, ProTrialStatus: 1, CreditGroups: [ + { Kind: "plan_base", Name: "Plan Base Credits", CycleTotalCapacity: "100", CycleRemainCapacity: "99.81", CycleUsedCapacity: "0.19", CycleFrozenCapacity: "0", CapacityUnit: "credits", TotalCount: 1 }, + { Kind: "plan_bonus", Name: "Plan Bonus Credits", CycleTotalCapacity: "0", CycleRemainCapacity: "0", CycleUsedCapacity: "0", CycleFrozenCapacity: "0", CapacityUnit: "credits", TotalCount: 0 }, + { Kind: "addon", Name: "Top-up Credits", CycleTotalCapacity: "0", CycleRemainCapacity: "0", CycleUsedCapacity: "0", CycleFrozenCapacity: "0", CapacityUnit: "credits", TotalCount: 0 }, + { Kind: "benefit", Name: "Platform Reward Credits", CycleTotalCapacity: "0", CycleRemainCapacity: "0", CycleUsedCapacity: "0", CycleFrozenCapacity: "0", CapacityUnit: "credits", TotalCount: 0 }, +] } + +test("extra credits are every kind but the plan's base, as WorkBuddy's own panel and CreditGroups say (Jiabubu on Discord)", () => { + const { SITES } = _internal + // the real free account: only the plan's base, one window as before + expect(_internal.usageOf(GROUPS_SUMMARY, "", SITES["workbuddy-ai"]).windows.map((w) => [w.name, w.display])).toEqual([["Credits", "0.19 / 100"]]) + // a Pro account (LIVE_SUMMARY) with the four kinds filled in (not real + // bytes; the shape is the free account's): the plan's bonus, a top-up the + // summary's Packages doesn't list, and a reward are all extra, by the + // groups, not by the codes the Packages happen to list + const group = (Kind, total, used) => ({ ...GROUPS_SUMMARY.CreditGroups.find((g) => g.Kind === Kind), CycleTotalCapacity: String(total), CycleUsedCapacity: String(used), CycleRemainCapacity: String(total - used) }) + const pro = { ...LIVE_SUMMARY, CreditGroups: [group("plan_base", 1000, 89.2), group("plan_bonus", 1000, 0), group("addon", 500, 120), group("benefit", 300, 30)] } + expect(_internal.usageOf(pro, "", SITES["workbuddy-ai"]).windows.map((w) => [w.name, w.display, w.limit, !!w.aside])) + .toEqual([["Credits", "89.2 / 1000", 1000, false], ["Extra credits", "150 / 1800", 1800, true]]) + // a kind the client doesn't know yet is extra too, never the plan's + const odd = { ...pro, CreditGroups: [...pro.CreditGroups, { ...group("addon", 50, 5), Kind: "something_new" }] } + expect(_internal.usageOf(odd, "", SITES["workbuddy-ai"]).windows[1].display).toBe("155 / 1850") + // without CreditGroups the codes split as the client's panel does: the + // plan's bonus (037) is extra, not the plan's + expect(_internal.usageOf(LIVE_SUMMARY, "", SITES["workbuddy-ai"]).windows.map((w) => [w.name, w.display, !!w.aside])) + .toEqual([["Credits", "89.2 / 1000", false], ["Extra credits", "0 / 1000", true]]) + for (const code of ["TCACA_code_028_NtpWi0jzXs", "TCACA_code_037_WxOD3MpI2o", "TCACA_code_009_0XmEQc2xOf", "TCACA_code_036_lupO5WgNdG", "TCACA_code_038_OhvqZtiPKr", "TCACA_code_007_nzdH5h4Nl0"]) + expect(_internal.planCodes({}, SITES.workbuddy)(code)).toBe(false) + // buddyOneVip is a plan's base on both builds + expect(_internal.planCodes({}, SITES.workbuddy)("TCACA_code_043_6oIkxT7Z0S")).toBe(true) + // and the prepaid balance still comes from the Packages, beside the groups + const bag = { ...GROUPS_SUMMARY, Packages: [...GROUPS_SUMMARY.Packages, { PackageCode: "WALLET", CycleRemainCapacity: "42" }] } + expect(_internal.usageOf(bag, "", SITES["workbuddy-ai"]).windows.map((w) => w.name)).toEqual(["Credits", "Prepaid credits"]) +}) + test("split, the plan's window keeps its cycle and each says what lapses", async () => { // CN_FREE's lists a year 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),