cline 0.1.4: usage shows ClinePass's 5-hour, weekly and monthly limits (zRain on Discord) — ClinePass counts usage against a rolling 5 hours, the calendar week and the calendar month, but the plugin's usage showed only the credit balance. It now also reads /api/v1/users/me/plan/usage-limits, what app.cline.bot's subscription page reads ({limits: [{type: five_hour|weekly|monthly, percentUsed, resetsAt}]}), with the same bearer, as 5 hours / Weekly / Month windows with their spans and resets. An account the limits can't be read for (no ClinePass) keeps its balance and its sign-in. bun test and check.mjs pass.

This commit is contained in:
yetone
2026-10-05 16:17:38 +08:00
parent 99cac44cf8
commit 838f6470dc
4 changed files with 85 additions and 9 deletions
+4 -1
View File
@@ -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
+37 -6
View File
@@ -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,
+43 -1
View File
@@ -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 })
+1 -1
View File
@@ -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",