qoder 0.2.2, workbuddy 0.1.6: each model's credit rate goes to magpie (rate, rateWas), as magpie's built-ins read it (yetone/magpie 33acb6bc, v0.1.667)

magpie now shows each model's credit rate in the provider's model list and an agent's picker (01huadalang on Discord: pick the cheap models without opening Qoder or WorkBuddy); a plugin gives it beside free on each model its provider.models hook lists. Qoder and Qoder CN: rate is the price_factor (priceFactor); rateWas an active promotion's before_promotion_price_factor, else an original_price_factor above the price (Qwen3.8-Flash: 0x, 0.1x 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 — the rule of internal/qoder/models.go's ModelInfo.free. WorkBuddy and WorkBuddy AI: rate is the model's credits ("x0.03" is 0.03; x0.00 stays free; an unreadable, negative or infinite rate is neither free nor rated), as wbCredits reads it.

usage.test.mjs in each package gets a test fed a fake listing / fake /v3/config (the cases of core's TestParseModelsRate and TestWBCreditsRate); both fail against the previous index.mjs and pass now. No request was made to Qoder or WorkBuddy. bun test (HOME a temp dir) and bun scripts/check.mjs pass.
This commit is contained in:
yetone
2026-10-02 22:54:55 +08:00
parent 7c859cc532
commit e2c72b2ae7
6 changed files with 127 additions and 8 deletions
+33 -1
View File
@@ -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
+1 -1
View File
@@ -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",
+35
View File
@@ -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 }