From 15d16f5ff0b620adaf1a2ad130eb9f18a2255798 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 17:16:39 +0800 Subject: [PATCH] workbuddy 0.1.21: a model free only at night is never said free; a whole-day discount shows the price before it (#121) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WorkBuddy's /v3/config prices a model by its "credits" and by a modelPromotions list the picker reads for its badges: Hy3 限时免费 (whole days to 10-31), Step-5-Preview 独家限免 (10-12..10-25), Hy4 preview 夜间免费 (free 23:00–8:00 only), GLM-5.2 and DeepSeek 夜间折扣. The plugin read only "credits", and magpie keeps a plugin's model list until it next asks, so a list read at night said Hy4 preview was free all day (#121's screenshot: 免费 in magpie, 0.29x and 夜间免费 in WorkBuddy). priceOf now reads both. A model under a promotion with daily hours is given at its full price and never as free (unpriced when the credits read 0 under a night discount). A running whole-day discount is the price now, with the credits before it as rateWas, which magpie already shows struck through. Run: - bun test across the repo (TMPDIR+HOME in the scratchpad): 1219 pass. The new price.test.mjs, built from the promotions copilot.tencent.com gave on 2026-10-14, fails 3 on the old code. - The owner's real WorkBuddy and WorkBuddy AI accounts through liveModels (17:10 CST): Hy4 preview 0.29, Hy3 and Step-5-Preview free, the rest at their credits, matching WorkBuddy's picker. Not tried: the config as served at night (whether "credits" itself reads x0.00 then); the unpriced branch covers that case. --- packages/workbuddy/README.md | 7 ++++ packages/workbuddy/index.mjs | 57 +++++++++++++++++++++++++++---- packages/workbuddy/package.json | 2 +- packages/workbuddy/price.test.mjs | 39 +++++++++++++++++++++ 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 packages/workbuddy/price.test.mjs diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index e415b0b..41d894d 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -134,6 +134,13 @@ is used. A model's reasoning levels are its variants (`reasoningEffort`). +A model's price is its `credits` ("x0.29") and the config's +`modelPromotions`. A discount for whole days (Step-5-Preview's 独家限免) is +the price now, with the price before it (`rateWas`). A price that holds only +some hours of the day (Hy4 preview, free 23:00–8:00) is given as its full +price, never as free: magpie keeps the list until it asks again, so a list +asked for at night would say "free" all day (#121). + ## Credits in magpie magpie's usage card shows the account's credits from the resource summary. diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 100de46..88ea5ad 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1187,6 +1187,8 @@ function modelOf(site, provider, m) { free: !!m.free, // magpie's own: the credits a request costs, as a multiple (0: none) rate: m.rate ?? 0, + // magpie's own: the price before a discount running now (0: none) + rateWas: m.rateWas ?? 0, } } @@ -1210,6 +1212,53 @@ function freeCredits(c) { return creditsOf(c) === 0 } +// discountOf reads a promotion's price: its discountedCredits ("0.50x", +// "0.00x"), else the factor times the model's own credits. +function discountOf(p, base) { + const said = creditsOf(String(p.discount?.discountedCredits ?? "").trim().replace(/x$/i, "")) + if (said !== undefined) return said + const f = Number(p.discount?.factor) + return Number.isFinite(f) && f >= 0 && base !== undefined ? base * f : undefined +} + +// running says whether a promotion is on at now: enabled, and inside its +// validFrom..validUntil. Its daily hours are not looked at here. +function running(p, now) { + if (!p || p.enabled === false) return false + const from = Date.parse(p.schedule?.validFrom ?? "") + const until = Date.parse(p.schedule?.validUntil ?? "") + return !(from > now) && !(until <= now) +} + +// priceOf is a model's price as magpie keeps it, from its credits and the +// config's modelPromotions (what WorkBuddy's picker shows as 限时免费, +// 夜间免费, 夜间折扣). magpie keeps the list it is given until it asks +// again, so a price that holds only some hours of the day (Hy4 preview, +// free 23:00–8:00 only) is given as its full price, never as free: a list +// asked for at night would otherwise say "free" all day (#121). A discount +// for whole days (Step-5-Preview's 独家限免) is the price now, with the +// price before it as rateWas. +function priceOf(d, promotions, now = Date.now()) { + const base = creditsOf(d.credits) + const mine = (Array.isArray(promotions) ? promotions : []).filter( + (p) => p?.kind === "discount" && Array.isArray(p.modelIds) && p.modelIds.includes(d.id) && running(p, now), + ) + if (mine.some((p) => Array.isArray(p.schedule?.daily) && p.schedule.daily.length)) { + // by the hour: the full price, which the credits are unless a night + // discount already took them to its own price + const night = mine.filter((p) => p.discount && Array.isArray(p.schedule?.daily) && p.schedule.daily.length) + if (base === undefined || (base === 0 && night.some((p) => discountOf(p, base) === 0))) return { free: false } + return { free: false, rate: base } + } + const off = mine.filter((p) => p.discount).map((p) => discountOf(p, base)).filter((r) => r !== undefined) + if (off.length) { + const r = Math.min(...off) + return { free: r === 0, rate: r, ...(base !== undefined && base > r ? { rateWas: base } : {}) } + } + if (base === undefined) return {} + return { free: base === 0, rate: base } +} + async function liveModels(site, a) { const headers = new Headers() sign(site, a, headers) @@ -1231,11 +1280,7 @@ async function liveModels(site, a) { m.context = d.maxInputTokens m.output = d.maxOutputTokens m.images = d.supportsImages === true && !textOnly(site, id) - const r = creditsOf(d.credits) - if (r !== undefined) { - m.free = r === 0 - m.rate = r - } + Object.assign(m, priceOf(d, cfg.modelPromotions)) const es = d.reasoning?.supportedEfforts ?? [] if (es.length) { const canOff = d.reasoning?.canDisableThinking @@ -1513,4 +1558,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { 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, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } +export const _internal = { 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 } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 6d4b1de..74451a0 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.20", + "version": "0.1.21", "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/price.test.mjs b/packages/workbuddy/price.test.mjs new file mode 100644 index 0000000..fc454ea --- /dev/null +++ b/packages/workbuddy/price.test.mjs @@ -0,0 +1,39 @@ +import { test, expect } from "bun:test" +import { _internal } from "./index.mjs" + +// modelPromotions as copilot.tencent.com's /v3/config gave them on +// 2026-10-14 (icons left out): Hy4 preview free 23:00–8:00 only, Hy3 free +// for whole days to 10-31, Step-5-Preview free 10-12..10-25 +const promotions = [ + { badge: { label: "限时免费", display: "activeOnly" }, discount: { discountedCredits: "0x", displayMode: "replace", factor: 0 }, enabled: true, id: "hy3-free-trial-202608", kind: "discount", modelIds: ["hy3", "hy3-b", "hy3-c"], priority: 200, schedule: { timezone: "Asia/Shanghai", validFrom: "2026-07-06T00:00:00+08:00", validUntil: "2026-11-01T00:00:00+08:00" } }, + { badge: { label: "夜间免费" }, enabled: true, id: "hy4-night-discount-badge-202609", kind: "discount", modelIds: ["hy4-preview", "hy4-preview-ctrl", "hy4-preview-exp"], priority: 50, schedule: { daily: [{ end: "23:00", start: "8:00" }], timezone: "Asia/Shanghai", validFrom: "2026-09-11T00:00:00+08:00", validUntil: "2026-11-01T00:00:00+08:00" } }, + { badge: { label: "夜间免费" }, discount: { discountedCredits: "0.00x", displayMode: "strikethrough", factor: 0 }, enabled: true, id: "hy4-night-discount-daytime-badge-202609", kind: "discount", modelIds: ["hy4-preview", "hy4-preview-ctrl", "hy4-preview-exp"], priority: 50, schedule: { daily: [{ end: "23:59", start: "23:00" }, { end: "8:00", start: "0:00" }], timezone: "Asia/Shanghai", validFrom: "2026-09-11T00:00:00+08:00", validUntil: "2026-11-01T00:00:00+08:00" } }, + { badge: { label: "夜间折扣" }, discount: { discountedCredits: "0.50x", displayMode: "strikethrough", factor: 0.5 }, enabled: true, id: "glm-52-night-discount-202607", kind: "discount", modelIds: ["glm-5.2"], priority: 100, schedule: { daily: [{ end: "7:50", start: "23:00" }], timezone: "Asia/Shanghai" } }, + { badge: { label: "独家限免" }, discount: { discountedCredits: "0.00x", displayMode: "strikethrough", factor: 0 }, enabled: true, id: "step-5-discount-daytime-badge-202610", kind: "discount", modelIds: ["step-5-preview"], priority: 50, schedule: { timezone: "Asia/Shanghai", validFrom: "2026-10-12T00:00:00+08:00", validUntil: "2026-10-26T00:00:00+08:00" } }, + { badge: { label: "夜间折扣" }, enabled: true, id: "ds-discount-daytime-badge-202608", kind: "discount", modelIds: ["deepseek-v4.1-flash"], priority: 50, schedule: { daily: [{ end: "23:59", start: "0:00" }], timezone: "Asia/Shanghai" } }, +] +const day = Date.parse("2026-10-14T17:00:00+08:00") +const night = Date.parse("2026-10-14T23:30:00+08:00") + +test("a model free only at night is never said free, day or night (#121)", () => { + for (const at of [day, night]) { + expect(_internal.priceOf({ id: "hy4-preview", credits: "x0.29" }, promotions, at)).toEqual({ free: false, rate: 0.29 }) + // a list asked for at night whose credits already read 0 + expect(_internal.priceOf({ id: "hy4-preview", credits: "x0.00" }, promotions, at).free).toBe(false) + } + expect(_internal.priceOf({ id: "glm-5.2", credits: "x0.79" }, promotions, night)).toEqual({ free: false, rate: 0.79 }) + expect(_internal.priceOf({ id: "deepseek-v4.1-flash", credits: "x0.11" }, promotions, day)).toEqual({ free: false, rate: 0.11 }) +}) + +test("a discount for whole days is the price now, with the price before it", () => { + expect(_internal.priceOf({ id: "hy3", credits: "x0.00" }, promotions, day)).toEqual({ free: true, rate: 0 }) + expect(_internal.priceOf({ id: "step-5-preview", credits: "x0.40" }, promotions, day)).toEqual({ free: true, rate: 0, rateWas: 0.4 }) + // after it ends, the credits again + expect(_internal.priceOf({ id: "step-5-preview", credits: "x0.40" }, promotions, Date.parse("2026-10-27T00:00:00+08:00"))).toEqual({ free: false, rate: 0.4 }) +}) + +test("without promotions a model is priced by its credits, as before", () => { + expect(_internal.priceOf({ id: "hy3", credits: "x0.00" })).toEqual({ free: true, rate: 0 }) + expect(_internal.priceOf({ id: "kimi-k3-1", credits: "x1.62" }, undefined)).toEqual({ free: false, rate: 1.62 }) + expect(_internal.priceOf({ id: "auto" }, promotions)).toEqual({}) +})