cline 0.1.7: ClinePass limits that fail to read say why on the card, and limits read are kept when the balance fails (#79, AkaChou)

usage read /users/me/plan/usage-limits with .catch(() => null), so every
failure there (401, 403, 429, 5xx, a timeout, a {success:false} envelope)
looked like an account without ClinePass: the card showed the balance
alone and said nothing. Only a 404 means no ClinePass (AxonHub's Cline
checker reads it the same way); any other failure now sets the usage
error "ClinePass limits couldn't be read (HTTP n: ...)" beside the balance,
the sign-in kept. clineApi's errors carry the HTTP status for this.

When /users/me or the balance failed, usage returned an error alone and
dropped the limits already read. Now the windows read are returned with
no error when the balance can't be; a failed /users/me still tries the
signed-in account's uid for the balance, and only a lapsed /users/me is
the sign-in gone. With nothing read at all, the balance's failure is the
error as before.

Tests (real reply shape {success, data: {limits: [{type, percentUsed,
resetsAt}]}}, 404 for no ClinePass): limits failing with 403, 401, 500,
429, a refused envelope and a network error; balance failing with
limits read; /users/me failing with limits read; neither read. The 8 new
cases fail on 0.1.6 (bun test: 70 pass, 8 fail) and pass now (78 pass);
bun scripts/check.mjs passes. Not tried against a real Cline account
(none at hand).
This commit is contained in:
yetone
2026-10-10 20:14:47 +08:00
parent d7e7eaf5f8
commit d335db0a6e
4 changed files with 142 additions and 12 deletions
+3 -1
View File
@@ -58,7 +58,9 @@ needs a sign-in); usage reads `/api/v1/users/me` and the account's `/balance`
(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.
subscription page shows). An account without ClinePass (a 404 there) has
none; limits that fail to read for any other reason say so on the card beside
the balance, and limits read are kept when the balance can't be.
### DeepSeek models only through DeepSeek's own API
+29 -8
View File
@@ -195,7 +195,7 @@ async function clineApi(url, { method = "GET", body, headers = {}, signal, lapse
const text = (await res.text().catch(() => "")).trim()
if (gone(res.status, text))
throw new Lapsed(`the sign-in lapsed — Cline refused it (HTTP ${res.status}${text ? `: ${text.slice(0, 200)}` : ""}); sign in to Cline again`)
if (!res.ok) throw new Error(`Cline request failed (HTTP ${res.status}${text ? `: ${text.slice(0, 200)}` : ""})`)
if (!res.ok) throw Object.assign(new Error(`Cline request failed (HTTP ${res.status}${text ? `: ${text.slice(0, 200)}` : ""})`), { status: res.status })
const env = safeJson(text)
if (env && typeof env === "object" && "success" in env) {
if (!env.success) throw new Error(`Cline request failed${env.error ? `: ${env.error}` : ""}`)
@@ -860,17 +860,31 @@ export const ClinePlugin = async ({ client } = {}, options = {}) => {
// 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.
// sign-in, not the read. The limits are asked alongside: a 404 there is
// an account without ClinePass, which has its credits alone (AxonHub's
// Cline checker reads it the same way); any other failure says so on the
// card rather than leaving the limits off unsaid (#79). Whichever of the
// two reads succeeds is shown when the other fails: windows read are
// kept when the balance can't be, as magpie's built-in key card keeps
// them.
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 limits = clineApi(`${API}/users/me/plan/usage-limits`, { headers: authz, lapsed: () => false }).then(
(v) => ({ v }),
(e) => (e?.status === 404 ? { v: null } : { e }),
)
let me = null
let lastErr = null
try {
me = await clineApi(`${API}/users/me`, { headers: authz })
} catch (e) {
if (e?.expired) throw e
lastErr = e
}
const ids = [me?.clineUserId, me?.subject, me?.id, cred.uid].filter((v) => typeof v === "string" && v.trim())
let balance = null
let lastErr = null
for (const id of ids) {
try {
balance = await clineApi(`${API}/users/${encodeURIComponent(id)}/balance`, { headers: authz })
@@ -879,8 +893,15 @@ export const ClinePlugin = async ({ client } = {}, options = {}) => {
lastErr = e
}
}
if (!balance) throw lastErr ?? new Error("Cline usage: no user id")
return { ...usageOf(me, balance, await limits), user: firstOf(cred.accountId, cred.email, cred.uid, ids[0]), signIn: renewed(cred) ? "renewed" : "kept" }
const lim = await limits
const out = usageOf(me, balance, lim.v)
if (!balance && !out.windows.length) throw lastErr ?? lim.e ?? new Error("Cline usage: no user id")
if (lim.e) {
// the words magpie's card knows (quotaError), the reason after
const why = String(lim.e?.message ?? lim.e).replace(/^Cline(?: request failed)?:?\s*/, "")
out.error = "ClinePass limits couldn't be read" + (why.startsWith("(") ? " " : ": ") + why
}
return { ...out, 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" }
}
+109 -2
View File
@@ -65,6 +65,11 @@ const client = () => {
const chatUrl = "https://api.cline.bot/api/v1/chat/completions"
const chatInit = (body) => ({ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body ?? { model: "anthropic/claude-sonnet-5" }) })
// noPass is usage-limits as Cline answers an account without ClinePass: a 404
// (AxonHub's Cline checker reads it the same way). It is listed first, since
// "/users/me" is a part of its URL too.
const noPass = ["/users/me/plan/usage-limits", () => Response.json({ success: false, error: "no active plan" }, { status: 404 })]
// ---- the hook shapes the plugin market checks ------------------------------------
test("the plugin exports the auth/config/provider hooks", async () => {
@@ -573,6 +578,7 @@ test("usage reads the account's balance", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
noPass,
["/users/me", () => Response.json({ success: true, data: { subject: "sub1", clineUserId: "cu1", email: "a@b.c", plan: "Usage-Billing" } })],
["/users/cu1/balance", () => Response.json({ success: true, data: { totalCredits: 1000, usedCredits: 250 } })],
])
@@ -597,6 +603,7 @@ 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([
noPass,
["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1", email: "a@b.c" } })],
["/users/cu1/balance", () => Response.json({ success: true, data: { balance: 480000 } })],
])
@@ -629,11 +636,11 @@ test("usage reads ClinePass's 5-hour, weekly and monthly limits", async () => {
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 () => {
test("an account without ClinePass (usage-limits 404) has its balance alone, and no error", 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 })],
noPass,
["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1" } })],
["/users/cu1/balance", () => Response.json({ success: true, data: { balance: 1000000 } })],
])
@@ -644,6 +651,104 @@ test("an account whose limits can't be read keeps its balance and its sign-in",
expect(out.balance).toBe("$1.00")
})
// #79: a ClinePass account whose limits failed to read for any other reason
// showed its balance alone, as if it had no ClinePass. The card now says the
// limits couldn't be read, and why, beside the balance; the sign-in stays.
for (const [why, reply, says] of [
["a 403", () => Response.json({ success: false, error: "forbidden" }, { status: 403 }), "ClinePass limits couldn't be read (HTTP 403: "],
["a 401", () => Response.json({ success: false, error: "unauthorized" }, { status: 401 }), "ClinePass limits couldn't be read (HTTP 401: "],
["a 500", () => new Response("upstream error", { status: 500 }), "ClinePass limits couldn't be read (HTTP 500: upstream error)"],
["a 429", () => Response.json({ success: false, error: "rate limited" }, { status: 429 }), "ClinePass limits couldn't be read (HTTP 429: "],
["a refused envelope", () => Response.json({ success: false, error: "plan lookup failed" }), "ClinePass limits couldn't be read: plan lookup failed"],
["a network error", () => { throw new TypeError("fetch failed") }, "ClinePass limits couldn't be read: fetch failed"],
]) {
test(`limits that fail with ${why} say so beside the balance`, async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
["/users/me/plan/usage-limits", reply],
["/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", accountId: "a@b.c" }))
expect(out.signIn).toBe("kept")
expect(out.error).toStartWith(says)
expect(out.windows).toEqual([])
expect(out.balance).toBe("$1.00")
expect(out.user).toBe("a@b.c")
})
}
// #79: when the balance couldn't be read, the limits already read were
// dropped with it, and the card showed the balance's error alone.
const passLimits = ["/users/me/plan/usage-limits", () => Response.json({ success: true, data: { limits: [
{ type: "five_hour", percentUsed: 40, resetsAt: "2026-10-05T15:00:00.000Z" },
{ type: "weekly", percentUsed: 10, resetsAt: "2026-10-12T00:00:00.000Z" },
{ type: "monthly", percentUsed: 12.5, resetsAt: "2026-11-01T00:00:00.000Z" },
] } })]
const passWindows = [
{ name: "5 hours", used: 40, span: 18000, resetsAt: "2026-10-05T15:00:00.000Z" },
{ name: "Weekly", used: 10, span: 604800, resetsAt: "2026-10-12T00:00:00.000Z" },
{ name: "Month", used: 12.5, span: 2592000, resetsAt: "2026-11-01T00:00:00.000Z" },
]
test("a balance that can't be read keeps the ClinePass limits read", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
passLimits,
["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1", plan: "ClinePass" } })],
["/users/cu1/balance", () => new Response("bad gateway", { status: 502 })],
])
const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck", accountId: "a@b.c" }))
expect(out.windows).toEqual(passWindows)
expect(out.error).toBeUndefined()
expect(out.balance).toBeUndefined()
expect(out.plan).toBe("ClinePass")
expect(out.user).toBe("a@b.c")
expect(out.signIn).toBe("kept")
})
test("a users/me that fails keeps the ClinePass limits read, and still tries the signed-in account's id for the balance", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
passLimits,
["/users/me", () => new Response("unavailable", { status: 503 })],
["/users/uid1/balance", () => Response.json({ success: true, data: { balance: 3000000 } })],
])
const out = await hooks.auth.usage(async () => ({ type: "oauth", access: "jwt", refresh: "r", expires: Date.now() + 3600_000, uid: "uid1" }))
expect(out.windows).toEqual(passWindows)
expect(out.balance).toBe("$3.00")
expect(out.error).toBeUndefined()
})
test("a users/me that fails with nothing else read is the card's error", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
noPass,
["/users/me", () => new Response("unavailable", { status: 503 })],
])
const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck" }))
expect(out.windows).toEqual([])
expect(out.error).toContain("HTTP 503")
expect(out.signIn).toBe("kept")
})
test("neither read: the balance's failure is the card's error", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
["/users/me/plan/usage-limits", () => new Response("down", { status: 500 })],
["/users/me", () => Response.json({ success: true, data: { clineUserId: "cu1" } })],
["/users/cu1/balance", () => new Response("bad gateway", { status: 502 })],
])
const out = await hooks.auth.usage(async () => ({ type: "api", key: "ck" }))
expect(out.windows).toEqual([])
expect(out.error).toContain("HTTP 502")
})
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 }])
@@ -653,6 +758,7 @@ test("usage tries the ids users/me names, in order", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
noPass,
["/users/me", () => Response.json({ success: true, data: { subject: "sub1" } })],
["/users/sub1/balance", () => Response.json({ success: true, data: { credits: "42" } })],
])
@@ -665,6 +771,7 @@ test("usage reports a lapsed account as expired", async () => {
const { client: c } = client()
const hooks = await ClinePlugin({ client: c })
serve([
noPass,
["/users/me", () => Response.json({ message: "expired" }, { status: 401 })],
])
const out = await hooks.auth.usage(async () => ({ type: "oauth", access: "jwt", refresh: "r", expires: Date.now() + 3600_000 }))
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-cline-auth",
"version": "0.1.6",
"version": "0.1.7",
"description": "Cline (cline.bot) on your own account — usage billing, ClinePass and the free models — in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",