From f791646a91deb5132e34d253ff739360bbc951d7 Mon Sep 17 00:00:00 2001 From: wizzy-yang <244719629+wizzy-yang@users.noreply.github.com> Date: Wed, 7 Oct 2026 18:45:30 +0800 Subject: [PATCH] =?UTF-8?q?cline=200.1.5:=20a=20balance=20with=20no=20whol?= =?UTF-8?q?e=20is=20the=20card's=20balance,=20not=20a=20meter=20=E2=80=94?= =?UTF-8?q?=20an=20account=20Cline=20serves=20only=20{balance}=20got=20a?= =?UTF-8?q?=20Credits=20window=20with=20used=200,=20so=20the=20card=20read?= =?UTF-8?q?=20a=20full=20bar=20and=20"100%=20left"=20whatever=20the=20bala?= =?UTF-8?q?nce=20($0.48=20in=20the=20report).=20The=20balance=20now=20goes?= =?UTF-8?q?=20to=20the=20card's=20balance=20field,=20as=20magpie's=20built?= =?UTF-8?q?-in=20Cline=20says=20it;=20a=20whole=20still=20makes=20a=20wind?= =?UTF-8?q?ow,=20carrying=20amount=20of=20limit=20(#659).=20"a=20balance?= =?UTF-8?q?=20with=20no=20whole=20is=20the=20card's=20balance,=20not=20a?= =?UTF-8?q?=20meter"=20fails=20on=200.1.4;=20bun=20test=20packages/cline?= =?UTF-8?q?=20(67=20pass)=20and=20bun=20scripts/check.mjs=20cline=20pass.?= =?UTF-8?q?=20(#36)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: wizzy-yang --- packages/cline/index.mjs | 56 +++++++++++++++++------------ packages/cline/index.test.mjs | 66 ++++++++++++++++++++++++----------- packages/cline/package.json | 2 +- 3 files changed, 80 insertions(+), 44 deletions(-) diff --git a/packages/cline/index.mjs b/packages/cline/index.mjs index 2fbde0c..9ddfaa0 100644 --- a/packages/cline/index.mjs +++ b/packages/cline/index.mjs @@ -614,19 +614,25 @@ async function unwrapped(res) { // decimals under one (an account with half a cent isn't "$0.00" to its owner) const usd = (n) => (n === 0 || n >= 0.01 ? `$${n.toFixed(2)}` : `$${Number(n.toFixed(4))}`) -// balanceWindow is the one balance shape Cline serves today: {balance} in +// balanceUSD is the one balance shape Cline serves today: {balance} in // millionths of a dollar — the chat gateway's current_balance is the same // number with the decimal point moved six places -function balanceWindow(v) { +function balanceUSD(v) { const n = typeof v?.balance === "number" ? v.balance : typeof v?.balance === "string" && Number.isFinite(Number(v.balance)) ? Number(v.balance) : NaN if (!Number.isFinite(n) || n < 0) return null - return { name: "Credits", used: 0, display: `${usd(n / 1e6)} left` } + return usd(n / 1e6) } -// balanceOf is the account's credit balance, a shape Cline hasn't fixed: a -// total with what is used or left of it, or just what is left -function balanceOf(v) { - if (!v || typeof v !== "object") return null +// creditsOf is the account's credit balance, a shape Cline hasn't fixed: a +// total with what is used or left of it, or a count alone. A whole is what +// magpie says as used or as left, so it is { window } — carrying the count +// itself (amount of limit) for a card that says either. A count alone is no +// share of anything: what is left is { balance }, the card's balance, as +// magpie's built-in Cline says it; what is used alone is { window } set +// aside, a line that holds nothing up. (A window made of a count alone read +// as 0% used — its bar full, "100% left", whatever the balance.) +function creditsOf(v) { + if (!v || typeof v !== "object") return {} const flat = {} const collect = (n, depth) => { if (!n || typeof n !== "object" || depth > 2) return @@ -644,24 +650,24 @@ function balanceOf(v) { const total = pick(["total", "totalcredits", "totalbalance", "creditstotal", "limit", "amount", "quota"]) const used = pick(["used", "usedcredits", "usedamount", "spent", "consumed", "usagetotal"]) const remaining = pick(["remaining", "remainingcredits", "remainingbalance", "left", "available", "balance", "credits", "current"]) - const w = { name: "Credits", used: 0 } if (Number.isFinite(total) && total > 0) { + const w = { name: "Credits", used: 0, limit: total } if (Number.isFinite(used)) { w.used = (100 * used) / total + w.amount = used w.display = `${compactNumber(used)} / ${compactNumber(total)}` } else if (Number.isFinite(remaining) && remaining <= total) { w.used = (100 * (total - remaining)) / total + w.amount = total - remaining w.display = `${compactNumber(total - remaining)} / ${compactNumber(total)}` - } else return null - } else if (Number.isFinite(remaining) && remaining > 0) { - w.display = `${compactNumber(remaining)} credits left` - } else if (Number.isFinite(used) && used > 0) { - w.used = used - w.display = `${compactNumber(used)} credits used` - } else return null - const at = timeOf(v, ["resetAt", "resetTime", "nextResetAt", "renewsAt", "expiresAt"]) - if (at) w.resetsAt = at - return w + } else return {} + const at = timeOf(v, ["resetAt", "resetTime", "nextResetAt", "renewsAt", "expiresAt"]) + if (at) w.resetsAt = at + return { window: w } + } + if (Number.isFinite(remaining) && remaining > 0) return { balance: `${compactNumber(remaining)} credits` } + if (Number.isFinite(used) && used > 0) return { window: { name: "Credits", used: 0, aside: true, display: `${compactNumber(used)} credits used` } } + return {} } // LIMITS are ClinePass's three limits as /users/me/plan/usage-limits names @@ -694,8 +700,14 @@ function usageOf(me, balance, limits) { const out = { windows: limitWindows(limits) } const plan = firstOf(me?.plan, me?.planType, me?.planName, me?.membership, me?.tier) if (plan) out.plan = plan - const w = balanceWindow(balance) ?? balanceOf(balance) - if (w) out.windows.push(w) + // the credits as magpie says them: a window when Cline tells a whole to + // be a share of, else the card's balance (magpie's built-in Cline, which + // reads the same reply, says the same) + const dollars = balanceUSD(balance) + const credits = dollars ? {} : creditsOf(balance) + if (credits.window) out.windows.push(credits.window) + const left = dollars ?? credits.balance + if (left) out.balance = left return out } @@ -993,8 +1005,8 @@ export const _internal = { fetchFeed, fetchRecommended, fetchCloudModels, - balanceOf, - balanceWindow, + creditsOf, + balanceUSD, usd, usageOf, limitWindows, diff --git a/packages/cline/index.test.mjs b/packages/cline/index.test.mjs index 86ba4bf..3619404 100644 --- a/packages/cline/index.test.mjs +++ b/packages/cline/index.test.mjs @@ -3,7 +3,7 @@ import { test, expect, beforeEach, afterEach } from "bun:test" import { ClinePlugin, _internal } from "./index.mjs" -const { authOf, parseAuth, parseFeed, prettify, balanceOf, balanceWindow, usd, usageOf, limitWindows, failure, bearerOf, toMs, refresh, deviceAuthorize, pollDevice, clientHeaders, constants: { CLIENT, DEFAULT_MODELS } } = _internal +const { authOf, parseAuth, parseFeed, prettify, creditsOf, balanceUSD, usd, usageOf, limitWindows, failure, bearerOf, toMs, refresh, deviceAuthorize, pollDevice, clientHeaders, constants: { CLIENT, DEFAULT_MODELS } } = _internal // ---- a fetch that answers from a script ----------------------------------------- @@ -556,6 +556,25 @@ test("usage reads the account's balance", async () => { expect(out.windows[0].name).toBe("Credits") expect(out.windows[0].used).toBeCloseTo(25) expect(out.windows[0].display).toBe("250 / 1000") + // what is used of the whole, so a card that says left says 750 / 1000 + expect(out.windows[0].amount).toBe(250) + expect(out.windows[0].limit).toBe(1000) +}) + +// magpie says a window's share from what is used of it, so a window made of a +// count alone reads as 0% used: its bar full and "100% left", whatever the +// balance. Cline's accounts are served {balance} in millionths of a dollar, +// and the card said "$0.48 left" beside 100% left. +test("a balance with no whole is the card's balance, not a meter", async () => { + const { client: c } = client() + const hooks = await ClinePlugin({ client: c }) + serve([ + ["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1", email: "a@b.c" } })], + ["/users/cu1/balance", () => Response.json({ success: true, data: { balance: 480000 } })], + ]) + const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck", accountId: "a@b.c" })) + expect(out.balance).toBe("$0.48") + expect(out.windows).toEqual([]) }) test("usage reads ClinePass's 5-hour, weekly and monthly limits", async () => { @@ -575,8 +594,9 @@ test("usage reads ClinePass's 5-hour, weekly and monthly limits", async () => { { name: "5 hours", used: 40, span: 18000, resetsAt: "2026-10-05T15:00:00.000Z" }, { name: "Weekly", used: 100, span: 604800 }, { name: "Month", used: 12.5, span: 2592000, resetsAt: "2026-11-01T00:00:00.000Z" }, - { name: "Credits", used: 0, display: "$25.00 left" }, ]) + // the credits are a balance, not a fourth meter + expect(out.balance).toBe("$25.00") const limits = calls.find((x) => x.url.endsWith("/users/me/plan/usage-limits")) expect(limits.init.headers.Authorization).toBe("Bearer ck") }) @@ -592,7 +612,8 @@ test("an account whose limits can't be read keeps its balance and its sign-in", const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck" })) expect(out.signIn).toBe("kept") expect(out.error).toBeUndefined() - expect(out.windows).toEqual([{ name: "Credits", used: 0, display: "$1.00 left" }]) + expect(out.windows).toEqual([]) + expect(out.balance).toBe("$1.00") }) test("limitWindows leaves out what it doesn't know", () => { @@ -608,7 +629,8 @@ test("usage tries the ids users/me names, in order", async () => { ["/users/sub1/balance", () => Response.json({ success: true, data: { credits: "42" } })], ]) const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck" })) - expect(out.windows[0].display).toBe("42 credits left") + expect(out.windows).toEqual([]) + expect(out.balance).toBe("42 credits") }) test("usage reports a lapsed account as expired", async () => { @@ -769,13 +791,13 @@ test("prettify reads a model id as a name", () => { expect(prettify("moonshotai/kimi-k3")).toBe("Kimi K3") }) -test("balanceWindow reads Cline's balance shape, in dollars", () => { - expect(balanceWindow({ balance: 4507 })).toEqual({ name: "Credits", used: 0, display: "$0.0045 left" }) - expect(balanceWindow({ balance: 25000000 }).display).toBe("$25.00 left") - expect(balanceWindow({ balance: 0 }).display).toBe("$0.00 left") - expect(balanceWindow({ balance: "900000" }).display).toBe("$0.90 left") - expect(balanceWindow({ totalCredits: 100 })).toBe(null) - expect(balanceWindow(null)).toBe(null) +test("balanceUSD reads Cline's balance shape, in dollars", () => { + expect(balanceUSD({ balance: 4507 })).toBe("$0.0045") + expect(balanceUSD({ balance: 25000000 })).toBe("$25.00") + expect(balanceUSD({ balance: 0 })).toBe("$0.00") + expect(balanceUSD({ balance: "900000" })).toBe("$0.90") + expect(balanceUSD({ totalCredits: 100 })).toBe(null) + expect(balanceUSD(null)).toBe(null) }) test("usd writes cents above a cent and four decimals under one", () => { @@ -785,16 +807,18 @@ test("usd writes cents above a cent and four decimals under one", () => { expect(usd(0.9)).toBe("$0.90") }) -test("balanceOf reads a total with its used or remaining, or just what's left", () => { - expect(balanceOf({ totalCredits: 1000, usedCredits: 250 })).toEqual({ name: "Credits", used: 25, display: "250 / 1000" }) - expect(balanceOf({ total: 100, remaining: 75 }).used).toBeCloseTo(25) - expect(balanceOf({ credits: 42 }).display).toBe("42 credits left") - expect(balanceOf({ used: 30 }).display).toBe("30 credits used") - const withReset = balanceOf({ totalCredits: 10, usedCredits: 1, resetAt: 1893456000000 }) - expect(withReset.resetsAt).toBeTruthy() - expect(balanceOf({})).toBe(null) - expect(balanceOf(null)).toBe(null) - expect(balanceOf({ hello: "world" })).toBe(null) +test("creditsOf reads a whole as a window, a count alone as the balance", () => { + expect(creditsOf({ totalCredits: 1000, usedCredits: 250 })).toEqual({ window: { name: "Credits", used: 25, amount: 250, limit: 1000, display: "250 / 1000" } }) + expect(creditsOf({ total: 100, remaining: 75 }).window.used).toBeCloseTo(25) + expect(creditsOf({ total: 100, remaining: 75 }).window.amount).toBe(25) + expect(creditsOf({ credits: 42 })).toEqual({ balance: "42 credits" }) + // what is used alone is no share of anything: a line, set aside + expect(creditsOf({ used: 30 })).toEqual({ window: { name: "Credits", used: 0, aside: true, display: "30 credits used" } }) + const withReset = creditsOf({ totalCredits: 10, usedCredits: 1, resetAt: 1893456000000 }) + expect(withReset.window.resetsAt).toBeTruthy() + expect(creditsOf({})).toEqual({}) + expect(creditsOf(null)).toEqual({}) + expect(creditsOf({ hello: "world" })).toEqual({}) }) test("usageOf reads a plan when the profile names one", () => { diff --git a/packages/cline/package.json b/packages/cline/package.json index 6001f85..4173839 100644 --- a/packages/cline/package.json +++ b/packages/cline/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cline-auth", - "version": "0.1.4", + "version": "0.1.5", "description": "Cline (cline.bot) on your own account — usage billing, ClinePass and the free models — in OpenCode and magpie", "type": "module", "main": "./index.mjs",