diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 6add82c..ddcde22 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -311,6 +311,33 @@ function freeOf(raw) { return (raw.is_free ?? raw.isFree) === true } +// rateOf reads a model's price as Qoder's client shows it beside the model +// (magpie shows it in its lists, so the cheap ones can be picked without +// opening Qoder): rate, the price_factor (0.5×), and rateWas, the price +// struck through beside it while a discount runs: an active promotion's +// before_promotion_price_factor, else an original_price_factor above the +// price (Qwen3.8-Flash: 0×, 0.1× struck through). A promotion's 0 that +// isn't free is its price before times its discount_factor. A listing with +// no price gives none (0). The same rule as magpie's built-in +// (internal/qoder/models.go, ModelInfo.free). +function rateOf(raw) { + const price = raw.price_factor ?? raw.priceFactor + if (typeof price !== "number" || !Number.isFinite(price)) return { rate: 0, rateWas: 0 } + const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : 0) + const p = raw.promotion ?? raw.prommotion + const before = p?.before_promotion_price_factor ?? p?.beforePromotionPriceFactor + const discount = num(p?.discount_factor) || num(p?.discountFactor) + const discounted = p?.active === true && typeof before === "number" && before > 0 + let rate = Math.max(price, 0) + let rateWas = Math.max(num(raw.original_price_factor), num(raw.originalPriceFactor)) + if (discounted) { + rateWas = before + if (rate === 0) rate = before * discount + } + if (rateWas <= rate) rateWas = 0 + return { rate, rateWas } +} + // modelInfo reads one entry of the listing's "chat" array: its efforts and // default from thinking_config (is_reasoning alone when it has none). function modelInfo(raw) { @@ -325,6 +352,7 @@ function modelInfo(raw) { efforts: Array.isArray(raw.reasoning_efforts) ? [...raw.reasoning_efforts] : [], defaultEffort: "", free: freeOf(raw), + ...rateOf(raw), config: raw, } const tc = raw.thinking_config @@ -394,6 +422,10 @@ function runtimeModel(site, m) { variants: Object.fromEntries((m.efforts ?? []).map((e) => [e, { reasoningEffort: e }])), // magpie's own: served at no cost to the plan's credits free: !!m.free, + // magpie's own: the credits a request costs, as a multiple, and the + // price before a discount running now (0: none) + rate: m.rate ?? 0, + rateWas: m.rateWas ?? 0, } } @@ -1143,7 +1175,7 @@ const makePlugin = (site) => async ({ client }) => { const ms = await models(cred, true) if (!ms.length) return provider.models return Object.fromEntries( - ms.map((m) => [m.key, runtimeModel(site, { id: m.key, name: m.name, context: m.context, images: m.images, efforts: m.thinks ? m.efforts : [], free: m.free })]), + ms.map((m) => [m.key, runtimeModel(site, { id: m.key, name: m.name, context: m.context, images: m.images, efforts: m.thinks ? m.efforts : [], free: m.free, rate: m.rate, rateWas: m.rateWas })]), ) } catch { return provider.models diff --git a/packages/qoder/package.json b/packages/qoder/package.json index e7cc7f97..7fb3b59 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.1", + "version": "0.2.2", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/usage.test.mjs b/packages/qoder/usage.test.mjs index 204ab88..16870de 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -282,6 +282,41 @@ test("a discount is not free: Qwen3.8-Max (0.5×, is_free true) is priced, Qwen3 expect(_internal.modelInfo({ key: "x", priceFactor: 0, promotion: { active: true, beforePromotionPriceFactor: 1 } }).free).toBe(false) }) +// Each model carries its price as Qoder's client shows it beside the model, +// as magpie's built-in reads it (internal/qoder/models_test.go, +// TestParseModelsRate): Qwen3.8-Max 0.5×, Kimi-K3 1.4×, Qwen3.8-Flash 0× +// with 0.1× struck through; a running promotion's price before it struck +// through, and its 0 that isn't free read as that price times its +// discount. A listing with no price gives none. +test("each model's rate, and its price before a running discount", async () => { + const RATED = { + chat: [ + { key: "qmodel_38max", enable: true, price_factor: 0.5, is_free: true, promotion: OFF_PEAK }, + { key: "qmodel_38max-offpeak", enable: true, price_factor: 0.2, promotion: { ...OFF_PEAK, active: true } }, + { key: "qfmodel", enable: true, price_factor: 0.0, original_price_factor: 0.1, is_free: true }, + { key: "qmodel_latest", enable: true, price_factor: 0.5, original_price_factor: 0.5 }, + { key: "kmodel_latest", enable: true, priceFactor: 1.4, originalPriceFactor: 2 }, + { key: "window", enable: true, price_factor: 0, promotion: { active: true, discount_factor: 0.5, before_promotion_price_factor: 1 } }, + { key: "window-camel", enable: true, priceFactor: 0, promotion: { active: true, discountFactor: 0.25, beforePromotionPriceFactor: 2 } }, + { key: "unpriced", enable: true, is_free: true }, + ], + } + const { hooks, auth } = await chat(RATED, () => new Response("")) + const ms = await hooks.provider.models({ models: {} }, { auth }) + expect(Object.fromEntries(Object.entries(ms).map(([k, m]) => [k, [m.rate, m.rateWas]]))).toEqual({ + qmodel_38max: [0.5, 0], + "qmodel_38max-offpeak": [0.2, 0.5], + qfmodel: [0, 0.1], + qmodel_latest: [0.5, 0], + kmodel_latest: [1.4, 2], + window: [0.5, 1], + "window-camel": [0.5, 2], + unpriced: [0, 0], + }) + expect(ms.qfmodel.free).toBe(true) + expect(ms.window.free).toBe(false) +}) + // a usage read on an account whose job token is due, the refresh answered by refresh() async function dueUsage(refresh, read) { let auth = { ...account(), expires: 0 } diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 16e6154..69532f8 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -465,18 +465,29 @@ function modelOf(site, provider, m) { variants: variants(m.efforts), // magpie's own: served at no cost to the plan's credits free: !!m.free, + // magpie's own: the credits a request costs, as a multiple (0: none) + rate: m.rate ?? 0, } } // liveModels asks WorkBuddy's product config for the plan's models: the // "cli" agent's list, each with its details. The CLI User-Agent picks // WorkBuddy's config (a bare WorkBuddy/ gets CodeBuddy IDE's, no cli). +// creditsOf reads a model's credits, the multiple WorkBuddy's picker shows +// by it: "x0.03", "0" or 0; "x0.00" is free. A rate it can't read (or +// none, or an infinite one) is undefined: the model is neither free nor +// rated. The same rule as magpie's built-in (wbCredits). +function creditsOf(c) { + if (c == null || c === "") return undefined + const s = String(c).trim().toLowerCase().replace(/^x/, "") + const n = Number(s) + return s !== "" && Number.isFinite(n) && n >= 0 ? n : undefined +} + // freeCredits says whether a model's credits are none: "x0.00", "0" or 0. // A rate it can't read is not free. function freeCredits(c) { - if (c == null || c === "") return false - const s = String(c).trim().toLowerCase().replace(/^x/, "") - return s !== "" && Number.isFinite(Number(s)) && Number(s) === 0 + return creditsOf(c) === 0 } async function liveModels(site, a) { @@ -500,7 +511,11 @@ async function liveModels(site, a) { m.context = d.maxInputTokens m.output = d.maxOutputTokens m.images = d.supportsImages === true - m.free = freeCredits(d.credits) + const r = creditsOf(d.credits) + if (r !== undefined) { + m.free = r === 0 + m.rate = r + } const es = d.reasoning?.supportedEfforts ?? [] if (es.length) { const canOff = d.reasoning?.canDisableThinking @@ -661,4 +676,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, fresh, SITES } +export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, creditsOf, fresh, SITES } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 321fb2f..42d9b37 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.5", + "version": "0.1.6", "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 f97473b..0ce3ec0 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -143,3 +143,40 @@ test("a desktop sign-in with the app signed out says why it failed", async () => const r = await (await m.authorize()).callback() expect(r).toEqual({ type: "failed", error: "WorkBuddy AI desktop isn't signed in" }) }) + +// Each model carries the credits WorkBuddy's picker shows by it, as +// magpie's built-in reads them (internal/provider/workbuddy_free_test.go, +// TestWBCreditsRate): x0.03 is 0.03, X1.00 is 1; x0.00 is free and no +// rate, and one it can't read (or an infinite one) is neither. +test("each model's rate is its credits", async () => { + for (const plugin of [WorkBuddyAuthPlugin, WorkBuddyAIAuthPlugin]) { + const calls = serve({ "/v3/config": () => ok({ + agents: [{ name: "cli", models: ["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg", "gpt-5.5", "kimi-k3", "hy3", "odd", "inf"] }], + models: [ + { id: "deepseek-v4.1-flash", name: "Deepseek-V4.1-Flash", credits: "x0.00" }, + { id: "deepseek-v4.1-flash-sg", name: "Deepseek-V4.1-Flash SG", credits: "x0.03" }, + { id: "gpt-5.5", credits: "X1.00" }, + { id: "kimi-k3", credits: 1.5 }, + { id: "hy3" }, + { id: "odd", credits: "lots" }, + { id: "inf", credits: "xInfinity" }, + ], + }) }) + const hooks = await plugin({ client: {} }) + const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u", domain: "" } + const ms = await hooks.provider.models({ id: hooks.auth.provider, models: {} }, { auth }) + expect(calls.map((c) => c.url.pathname)).toEqual(["/v3/config"]) + expect(Object.fromEntries(Object.entries(ms).map(([k, m]) => [k, [m.rate, m.free]]))).toEqual({ + "deepseek-v4.1-flash": [0, true], + "deepseek-v4.1-flash-sg": [0.03, false], + "gpt-5.5": [1, false], + "kimi-k3": [1.5, false], + hy3: [0, false], + odd: [0, false], + inf: [0, false], + }) + } + const { creditsOf } = _internal + expect(["x0.03", " X1.00 ", 1.5, "0"].map(creditsOf)).toEqual([0.03, 1, 1.5, 0]) + for (const c of ["", null, undefined, "free", "x-1", "Infinity"]) expect(creditsOf(c)).toBeUndefined() +})