From 838f6470dcb97c75df8908573b5b266823ae6c1f Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 16:17:38 +0800 Subject: [PATCH] =?UTF-8?q?cline=200.1.4:=20usage=20shows=20ClinePass's=20?= =?UTF-8?q?5-hour,=20weekly=20and=20monthly=20limits=20(zRain=20on=20Disco?= =?UTF-8?q?rd)=20=E2=80=94=20ClinePass=20counts=20usage=20against=20a=20ro?= =?UTF-8?q?lling=205=20hours,=20the=20calendar=20week=20and=20the=20calend?= =?UTF-8?q?ar=20month,=20but=20the=20plugin's=20usage=20showed=20only=20th?= =?UTF-8?q?e=20credit=20balance.=20It=20now=20also=20reads=20/api/v1/users?= =?UTF-8?q?/me/plan/usage-limits,=20what=20app.cline.bot's=20subscription?= =?UTF-8?q?=20page=20reads=20({limits:=20[{type:=20five=5Fhour|weekly|mont?= =?UTF-8?q?hly,=20percentUsed,=20resetsAt}]}),=20with=20the=20same=20beare?= =?UTF-8?q?r,=20as=205=20hours=20/=20Weekly=20/=20Month=20windows=20with?= =?UTF-8?q?=20their=20spans=20and=20resets.=20An=20account=20the=20limits?= =?UTF-8?q?=20can't=20be=20read=20for=20(no=20ClinePass)=20keeps=20its=20b?= =?UTF-8?q?alance=20and=20its=20sign-in.=20bun=20test=20and=20check.mjs=20?= =?UTF-8?q?pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cline/README.md | 5 +++- packages/cline/index.mjs | 43 +++++++++++++++++++++++++++++----- packages/cline/index.test.mjs | 44 ++++++++++++++++++++++++++++++++++- packages/cline/package.json | 2 +- 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/packages/cline/README.md b/packages/cline/README.md index 256950d..6a335c2 100644 --- a/packages/cline/README.md +++ b/packages/cline/README.md @@ -51,7 +51,10 @@ requests carry the same identity. A finished non-streaming answer rides in a apart too. The model list is read from `/api/v1/ai/cline/recommended-models` and `/api/v1/ai/cline/models` (neither needs a sign-in); usage reads `/api/v1/users/me` and the account's `/balance` -(its balance is counted in millionths of a dollar). +(its balance is counted in millionths of a dollar), and ClinePass's 5-hour, +weekly and monthly limits from `/api/v1/users/me/plan/usage-limits` +(`{limits: [{type, percentUsed, resetsAt}]}`, what app.cline.bot's +subscription page shows); an account without ClinePass has none. ### DeepSeek models only through DeepSeek's own API diff --git a/packages/cline/index.mjs b/packages/cline/index.mjs index 794ec42..2fbde0c 100644 --- a/packages/cline/index.mjs +++ b/packages/cline/index.mjs @@ -4,7 +4,8 @@ // approves, traded at Cline's register endpoint for the account's token pair — // or a plain API key from app.cline.bot. Chat completions go to Cline's // gateway in OpenAI's format; the model list comes from its recommended-models -// and cloud-models feeds; usage reads the account's credit balance. +// and cloud-models feeds; usage reads the account's credit balance and, on +// ClinePass, the plan's 5-hour, weekly and monthly limits. import { STATUS_CODES } from "node:http" import { randomUUID } from "node:crypto" @@ -663,8 +664,34 @@ function balanceOf(v) { return w } -function usageOf(me, balance) { - const out = { windows: [] } +// LIMITS are ClinePass's three limits as /users/me/plan/usage-limits names +// them (app.cline.bot's subscription page reads the same): a rolling 5 +// hours, the calendar week and the calendar month +const LIMITS = { + five_hour: { name: "5 hours", span: 5 * 60 * 60 }, + weekly: { name: "Weekly", span: 7 * 24 * 60 * 60 }, + monthly: { name: "Month", span: 30 * 24 * 60 * 60 }, +} + +// limitWindows are the plan's limits, {limits: [{type, percentUsed, +// resetsAt}]}, in LIMITS' order; a type it doesn't know is left out, as the +// dashboard leaves it +function limitWindows(v) { + const out = [] + for (const l of Array.isArray(v?.limits) ? v.limits : []) { + const k = LIMITS[l?.type] + if (!k) continue + const n = Number(l.percentUsed ?? 0) + const w = { name: k.name, used: Math.min(100, Math.max(0, Number.isFinite(n) ? n : 0)), span: k.span } + const at = timeOf(l, ["resetsAt"]) + if (at) w.resetsAt = at + out.push([Object.keys(LIMITS).indexOf(l.type), w]) + } + return out.sort((a, b) => a[0] - b[0]).map(([, w]) => w) +} + +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) @@ -799,13 +826,16 @@ export const ClinePlugin = async ({ client } = {}, options = {}) => { signIn: e?.expired ? "expired" : undefined, }) - // usage is the account's credit balance, magpie's own hook. A read the - // account's token couldn't make is the sign-in, not the read. + // usage is the account's credit balance and its ClinePass limits, + // magpie's own hook. A read the account's token couldn't make is the + // sign-in, not the read; the limits are asked alongside and an account + // without ClinePass (or one they can't be read for) just has none. const usage = async (getAuth) => { try { const cred = await fresh(getAuth) const authz = { Authorization: `Bearer ${cred.bearer}` } const me = await clineApi(`${API}/users/me`, { headers: authz }) + const limits = clineApi(`${API}/users/me/plan/usage-limits`, { headers: authz, lapsed: () => false }).catch(() => null) const ids = [me?.clineUserId, me?.subject, me?.id, cred.uid].filter((v) => typeof v === "string" && v.trim()) let balance = null let lastErr = null @@ -818,7 +848,7 @@ export const ClinePlugin = async ({ client } = {}, options = {}) => { } } if (!balance) throw lastErr ?? new Error("Cline usage: no user id") - return { ...usageOf(me, balance), user: firstOf(cred.accountId, cred.email, cred.uid, ids[0]), signIn: renewed(cred) ? "renewed" : "kept" } + return { ...usageOf(me, balance, await limits), user: firstOf(cred.accountId, cred.email, cred.uid, ids[0]), signIn: renewed(cred) ? "renewed" : "kept" } } catch (e) { return { windows: [], error: e?.message ?? String(e), signIn: e?.expired ? "expired" : "kept" } } @@ -967,6 +997,7 @@ export const _internal = { balanceWindow, usd, usageOf, + limitWindows, failure, errorResponse, bodyText, diff --git a/packages/cline/index.test.mjs b/packages/cline/index.test.mjs index 98b7816..86ba4bf 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, failure, bearerOf, toMs, refresh, deviceAuthorize, pollDevice, clientHeaders, constants: { CLIENT, DEFAULT_MODELS } } = _internal +const { authOf, parseAuth, parseFeed, prettify, balanceOf, balanceWindow, usd, usageOf, limitWindows, failure, bearerOf, toMs, refresh, deviceAuthorize, pollDevice, clientHeaders, constants: { CLIENT, DEFAULT_MODELS } } = _internal // ---- a fetch that answers from a script ----------------------------------------- @@ -558,6 +558,48 @@ test("usage reads the account's balance", async () => { expect(out.windows[0].display).toBe("250 / 1000") }) +test("usage reads ClinePass's 5-hour, weekly and monthly limits", async () => { + const { client: c } = client() + const hooks = await ClinePlugin({ client: c }) + serve([ + ["/users/me/plan/usage-limits", () => Response.json({ success: true, data: { limits: [ + { type: "monthly", percentUsed: 12.5, resetsAt: "2026-11-01T00:00:00.000Z" }, + { type: "five_hour", percentUsed: 40, resetsAt: "2026-10-05T15:00:00.000Z" }, + { type: "weekly", percentUsed: 130 }, + ] } })], + ["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1" } })], + ["/users/cu1/balance", () => Response.json({ success: true, data: { balance: 25000000 } })], + ]) + const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck" })) + expect(out.windows).toEqual([ + { 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" }, + ]) + const limits = calls.find((x) => x.url.endsWith("/users/me/plan/usage-limits")) + expect(limits.init.headers.Authorization).toBe("Bearer ck") +}) + +test("an account whose limits can't be read keeps its balance and its sign-in", async () => { + const { client: c } = client() + const hooks = await ClinePlugin({ client: c }) + serve([ + ["/users/me/plan/usage-limits", () => Response.json({ success: false, error: "no active plan" }, { status: 403 })], + ["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1" } })], + ["/users/cu1/balance", () => Response.json({ success: true, data: { balance: 1000000 } })], + ]) + 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" }]) +}) + +test("limitWindows leaves out what it doesn't know", () => { + expect(limitWindows(null)).toEqual([]) + expect(limitWindows({ limits: [{ type: "daily", percentUsed: 5 }, { type: "five_hour" }] })).toEqual([{ name: "5 hours", used: 0, span: 18000 }]) +}) + test("usage tries the ids users/me names, in order", async () => { const { client: c } = client() const hooks = await ClinePlugin({ client: c }) diff --git a/packages/cline/package.json b/packages/cline/package.json index b9d65a8..6001f85 100644 --- a/packages/cline/package.json +++ b/packages/cline/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cline-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "Cline (cline.bot) on your own account — usage billing, ClinePass and the free models — in OpenCode and magpie", "type": "module", "main": "./index.mjs",