diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index dd99b94..48a8ed4 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -205,3 +205,5 @@ magpie's built-in WorkBuddy does more than this package: - several accounts under one provider. OpenCode's plugin API has no place for these. + +Credit multipliers accept the international catalog's `x0.34 credits` text as well as `x0.34`. An empty credits value (for example Auto) stays unknown, never free. diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 2c7846b..58700c3 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1403,7 +1403,7 @@ function modelOf(site, provider, m) { // 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 s = String(c).trim().toLowerCase().replace(/\s+credits?$/, "").replace(/^x/, "") const n = Number(s) return s !== "" && Number.isFinite(n) && n >= 0 ? n : undefined } diff --git a/packages/workbuddy/price.test.mjs b/packages/workbuddy/price.test.mjs index fc454ea..5eab0c5 100644 --- a/packages/workbuddy/price.test.mjs +++ b/packages/workbuddy/price.test.mjs @@ -1,5 +1,5 @@ import { test, expect } from "bun:test" -import { _internal } from "./index.mjs" +import { WorkBuddyAIAuthPlugin, _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 @@ -37,3 +37,35 @@ test("without promotions a model is priced by its credits, as before", () => { expect(_internal.priceOf({ id: "kimi-k3-1", credits: "x1.62" }, undefined)).toEqual({ free: false, rate: 1.62 }) expect(_internal.priceOf({ id: "auto" }, promotions)).toEqual({}) }) + +// International /v3/config returned these aliases with a credits suffix on +// 2026-10-17. Number("0.34 credits") loses an otherwise known multiplier. +test("international credit units preserve alias rates and unknown prices", () => { + for (const [credits, rate] of [["x0.34 credits", 0.34], ["x0.59 credits", 0.59], ["x0.99 credits", 0.99], ["x0.00 credits", 0]]) { + expect(_internal.priceOf({ id: "fast-model", credits })).toEqual({ free: rate === 0, rate }) + } + for (const credits of ["", "credits", "x0.34 bananas", "xNaN credits", "xInfinity credits", "x-1 credits"]) { + expect(_internal.priceOf({ id: "default-model", credits })).toEqual({}) + } +}) + +test("browser model discovery carries unit-suffixed rates to the host", async () => { + const real = globalThis.fetch + globalThis.fetch = async (input) => { + expect(String(input)).toBe("https://www.workbuddy.ai/v3/config") + return Response.json({ code: 0, data: { + agents: [{ name: "cli", models: ["default-model", "fast-model", "balanced-model", "primary-model"] }], + models: [{ id: "default-model", credits: "" }, { id: "fast-model", credits: "x0.34 credits" }, { id: "balanced-model", credits: "x0.59 credits" }, { id: "primary-model", credits: "x0.99 credits" }], + } }) + } + try { + const h = await WorkBuddyAIAuthPlugin({ client: { auth: { set: async () => { throw Error("fixture does not write auth") } } } }) + const config = {} + await h.config(config) + const models = await h.provider.models(config.provider["workbuddy-ai"], { auth: { type: "oauth", access: "offline-access", expires: Date.now() + 86400000 } }) + expect(models["fast-model"].rate).toBe(0.34) + expect(models["balanced-model"].rate).toBe(0.59) + expect(models["primary-model"].rate).toBe(0.99) + expect(models["default-model"].free).toBe(false) + } finally { globalThis.fetch = real } +})