From 9f3763c0616b0d876ff86ce73c0f2315ffd13c88 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 20:59:55 +0800 Subject: [PATCH] raccoon 0.1.11: credits spent under a balance over every grant count as used (#1030) 0.1.10 (#936) made a balance a little over every listed grant (within 10%) the Credits window's total, so the card always read 0 used and the total went down as credits were spent: the reporter saw 0 / 9,965 and then 0 / 9,964. Now the total is the highest balance read for the same account and the same set of grants, and what the balance has gone down by since is used (1 / 9,965). A new grant, or one expiring, changes the set and starts the count again. The highest balance is kept in memory only: a restarted host starts from the balance it reads. Run on a separate test Mac (Bun, temp HOME): - bun test packages/raccoon: 53 pass, 0 fail (1 new, built from the reporter's six income bills and balances). - bun scripts/check.mjs raccoon: ok. Broken by hand: the total taken from the current balance again (the 0.1.10 rule): the new test fails, 52 pass. Restored. Not tried: a real Raccoon account. Why the balance is over the grants (65 credits here) is still unknown, so #1030 stays open for the reporter. Reported-by: ULTIMATE215 --- packages/raccoon/README.md | 2 +- packages/raccoon/index.mjs | 26 ++++++++++++++++++---- packages/raccoon/package.json | 2 +- packages/raccoon/raccoon.test.mjs | 36 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/packages/raccoon/README.md b/packages/raccoon/README.md index 3f23e72..8399430 100644 --- a/packages/raccoon/README.md +++ b/packages/raccoon/README.md @@ -27,7 +27,7 @@ OpenCode & Magpie provider plugin for **SenseTime Raccoon (商汤小浣熊, `xia 3. 直接只读复用本机商汤小浣熊客户端实时登录态(客户端保存的 `auth.json`:新版在客户端配置目录下的 `agent/config/auth.json`(macOS 为 `~/Library/Application Support/office-raccoon`,Windows 为 `%USERPROFILE%\.office-raccoon`),旧版在 `~/.box-agent/config/auth.json`)。桌面端席位会把登录时读到的 token 保存在自己的账号记录里,因此它不会拿到、也不会**上报**另一个账号的凭据或积分;一旦自己的 token 被拒或用尽,就提示重新登录(客户端自身的 refresh token 依旧不会被动用)。 - **Token 自动续期**:浏览器登录账号过期前 5 分钟自动单飞刷新。 - **动态模型目录与积分倍率**:实时同步 `/api/web/llm/v2/model_catalog`,展示模型上下文、最大输出、视觉/思考能力、积分倍率与限免(`Free`)标识。 -- **积分余额与套餐查询**:展示可用积分、未过期发放额度、每日积分过期时间与 Pro/Free 套餐状态。积分余额接口 `/api/web/points/v1/balance` 拒绝时(403),改从 `/api/web/office/v3/setting_info` 读可用积分;套餐接口读不到时不显示套餐,而不是当作 Free。 +- **积分余额与套餐查询**:展示可用积分、未过期发放额度、每日积分过期时间与 Pro/Free 套餐状态。积分余额接口 `/api/web/points/v1/balance` 拒绝时(403),改从 `/api/web/office/v3/setting_info` 读可用积分;套餐接口读不到时不显示套餐,而不是当作 Free。余额比账单里未过期的发放额度略多时(10% 以内),以这批发放下见过的最高余额作总额,之后用掉的积分记为已用;有新的发放或发放过期时重新计。 - **每日签到与桌面端登录奖励**:支持 Magpie 每日自动签到(`+300` 积分/天)及首次桌面端 `+3000` 积分登录奖励领取。 - **SSE 模型名重写**:将上游响应中的内部模型名重写为下游请求的 `model`,确保 Claude Code / Codex / OpenCode 等客户端兼容。 diff --git a/packages/raccoon/index.mjs b/packages/raccoon/index.mjs index 8d41358..8326e21 100644 --- a/packages/raccoon/index.mjs +++ b/packages/raccoon/index.mjs @@ -663,7 +663,16 @@ async function availablePoints(token) { return Math.floor(n) } -async function queryUsage(token) { +// topBalance holds, per account and per set of grants, the highest balance +// a read has seen. A balance over every listed grant (plugins #936) has no +// grant total to measure spending against, so the window's total is that +// highest balance and what was spent since is what it has gone down by +// (plugins #1030: 9,965 then 9,964 read 0 / 9,965 then 0 / 9,964). A new +// or expired grant changes the set and starts it again. Kept in memory: a +// restarted host starts from the balance it reads. +const topBalance = new Map() + +async function queryUsage(token, account = "") { const [avail, entitlement, bills] = await Promise.all([ availablePoints(token), apiGetJson("/api/web/auth/v1/entitlement_info", token).catch(() => null), @@ -684,10 +693,12 @@ async function queryUsage(token) { let soonestExpMs = 0 let expiringPts = 0 const expired = [] + const grantKeys = [] const now = Date.now() for (const item of Array.isArray(bills?.items) ? bills.items : []) { const pts = Math.floor(Number(item?.points) || 0) if (pts <= 0) continue + grantKeys.push(billKey(item)) const expStr = typeof item?.expire_at === "string" ? item.expire_at : "" const expMs = expStr ? Date.parse(expStr) : 0 if (expMs > 0 && expMs <= now) { @@ -720,7 +731,10 @@ async function queryUsage(token) { // the card still shows one. A balance far over the bills is left // balance-only: the bills don't explain it. if (grantedFromBills > 0 && avail > grantedFromBills && avail - grantedFromBills <= avail / 10) { - grantedFromBills = avail + const key = `${account}\n${grantKeys.sort().join("\n")}` + const top = Math.max(avail, topBalance.get(key) ?? 0) + topBalance.set(key, top) + grantedFromBills = top } const runsOutAt = soonestExpMs ? new Date(soonestExpMs).toISOString() : null const runsOut = soonestExpMs ? Math.min(avail, expiringPts) : 0 @@ -752,6 +766,10 @@ async function queryUsage(token) { } } +function usageAccount(a) { + return String(a?.accountId || a?.uid || a?.phone || a?.name || "") +} + function billKey(item) { return String(item?.bill_id || item?.biz_id || `${item?.created_at ?? ""}:${item?.points ?? 0}:${item?.event_name ?? ""}`) } @@ -1193,13 +1211,13 @@ export const RaccoonAuthPlugin = async ({ client }) => { let { a, didRenew } = await currentAuth(client, auth) let result try { - result = await queryUsage(a.access) + result = await queryUsage(a.access, usageAccount(a)) } catch (e) { if (e?.status === 401 && a.refresh && a.source !== "desktop") { const retryAuth = await currentAuth(client, a, true) a = retryAuth.a didRenew = true - result = await queryUsage(a.access) + result = await queryUsage(a.access, usageAccount(a)) } else { throw e } diff --git a/packages/raccoon/package.json b/packages/raccoon/package.json index a7d2a62..760aab2 100644 --- a/packages/raccoon/package.json +++ b/packages/raccoon/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-raccoon-auth", - "version": "0.1.10", + "version": "0.1.11", "description": "OpenCode & Magpie provider plugin for SenseTime Raccoon (商汤小浣熊, xiaohuanxiong.com)", "type": "module", "main": "./opencode.mjs", diff --git a/packages/raccoon/raccoon.test.mjs b/packages/raccoon/raccoon.test.mjs index ed01d9d..d5afa45 100644 --- a/packages/raccoon/raccoon.test.mjs +++ b/packages/raccoon/raccoon.test.mjs @@ -545,6 +545,42 @@ test("a balance a little over every listed grant keeps the Credits window (plugi expect(u.windows[0]).toMatchObject({ name: "Credits", amount: 0, limit: 9623, display: "0 / 9623", runsOut: 300 }) }) +// plugins #1030: the same reporter's income bills, as pasted (six grants, +// 9,900), under a balance of 9,965 and then 9,964 after one call. What was +// spent is counted from the highest balance seen; a new grant starts over. +test("spending under a balance over every grant counts as used (plugins #1030)", async () => { + const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 }) + let avail = 9965 + const grants = [ + { bill_id: "g6", biz_type: "daily_grant", points: 300, expire_at: "2099-11-17T23:59:59+08:00" }, + { bill_id: "g5", biz_type: "reward_grant", points: 3000 }, + { bill_id: "g4", biz_type: "daily_grant", points: 300, expire_at: "2099-11-15T23:59:59+08:00" }, + { bill_id: "g3", biz_type: "reward_grant", points: 3000 }, + { bill_id: "g2", biz_type: "daily_grant", points: 300, expire_at: "2099-11-14T23:59:59+08:00" }, + { bill_id: "g1", biz_type: "reward_grant", points: 3000 }, + ] + startServer({ + "GET /api/web/points/v1/balance": () => Response.json({ code: 0, data: { available_points: avail } }), + "GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: {} }), + "GET /api/web/points/v1/bills": () => Response.json({ code: 0, data: { items: grants } }), + }) + const hooks = await RaccoonAuthPlugin({ client: {} }) + const read = () => + hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000, accountId: "Raccoon-1030" })) + expect((await read()).windows[0]).toMatchObject({ amount: 0, limit: 9965, display: "0 / 9965" }) + avail = 9964 + expect((await read()).windows[0]).toMatchObject({ amount: 1, limit: 9965, display: "1 / 9965" }) + avail = 9940 + expect((await read()).windows[0]).toMatchObject({ amount: 25, limit: 9965, display: "25 / 9965" }) + // another account with the same grants is counted on its own + const other = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000, accountId: "Raccoon-other" })) + expect(other.windows[0]).toMatchObject({ amount: 0, limit: 9940 }) + // the next day's grant starts the count again + grants.unshift({ bill_id: "g7", biz_type: "daily_grant", points: 300, expire_at: "2099-11-18T23:59:59+08:00" }) + avail = 10240 + expect((await read()).windows[0]).toMatchObject({ amount: 0, limit: 10240 }) +}) + test("a readable balance asks setting_info nothing (plugins #520)", async () => { const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 }) let asked = 0