raccoon 0.1.7: the balance comes from setting_info when points/v1/balance answers 403 (#520)

xiaohuanxiong.com's /api/web/points/v1/balance now answers 403 to a
signed-in account, the vendor's own web page included (RedFlagRF's
screenshots on #520), and usage threw on it: every account showed an
error and routing read its remaining quota as unknown. RedFlagRF found
that /api/web/office/v3/setting_info still carries data.available_points.

- usage asks balance first and reads available_points from setting_info
  when balance fails or has no number. Balance stays first because
  setting_info is the call the clients make on opening and can hand out
  the day's credits; a usage poll shouldn't do that while balance answers.
- an expired sign-in on balance is still said as expired (and renewed),
  not read again from setting_info.
- if neither carries a number, usage says the error instead of the
  "0 credits" the old `|| 0` would have given.
- entitlement_info answers 403 too (#520): an unread plan is now left
  unsaid instead of reported as Free.

Not changed: the 413 on #520. The plugin passes the vendor's 413 through
untouched, and magpie already treats a 413 as the request's (no account
rests, the next group member is asked, the agent is told
request_too_large to compact; gateway.go's overflow branch). The
vendor's words for it were never captured, so nothing more is known.

Run (mote, bun): packages/raccoon `bun test` 48 pass; `bun
scripts/check.mjs` exit 0. Red on the old index.mjs: the two 403 tests
fail. Broken by hand: dropping the expired rethrow, the `throw first`
and the unread-plan check turns the expired, neither-readable and 403
tests red.
Not tried: a real Raccoon account (we have none). Unauthenticated, all
three endpoints answer 401, so the 403 is per signed-in account.

Reported-by: nglsk
Reported-by: RedFlagRF
This commit is contained in:
yetone
2026-11-06 15:43:47 +08:00
parent 4578b11de2
commit 749e9dd12a
4 changed files with 126 additions and 8 deletions
+1 -1
View File
@@ -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 套餐状态。
- **积分余额与套餐查询**:展示可用积分、未过期发放额度、每日积分过期时间与 Pro/Free 套餐状态。积分余额接口 `/api/web/points/v1/balance` 拒绝时(403),改从 `/api/web/office/v3/setting_info` 读可用积分;套餐接口读不到时不显示套餐,而不是当作 Free。
- **每日签到与桌面端登录奖励**:支持 Magpie 每日自动签到(`+300` 积分/天)及首次桌面端 `+3000` 积分登录奖励领取。
- **SSE 模型名重写**:将上游响应中的内部模型名重写为下游请求的 `model`,确保 Claude Code / Codex / OpenCode 等客户端兼容。
+35 -6
View File
@@ -629,18 +629,47 @@ async function allBills(token) {
return bills
}
// availablePoints is the account's balance. /points/v1/balance answers 403,
// to the vendor's own web page too (plugins #520), so a failure there reads
// the same number from /office/v3/setting_info. Balance stays first while it may come back:
// setting_info is the call the clients make on opening, and it can hand out
// the day's credits. An expired sign-in is said as such, not read again.
async function availablePoints(token) {
let first
try {
const points = await apiGetJson("/api/web/points/v1/balance", token)
const n = Number(points?.available_points)
if (Number.isFinite(n)) return Math.floor(n)
first = new Error("balance has no available_points")
} catch (e) {
if (e?.signIn === "expired") throw e
first = e
}
let info
try {
info = await apiGetJson("/api/web/office/v3/setting_info", token)
} catch (e) {
if (e?.signIn === "expired") throw e
throw first
}
const n = Number(info?.available_points)
if (!Number.isFinite(n)) throw first
return Math.floor(n)
}
async function queryUsage(token) {
const [points, entitlement, bills] = await Promise.all([
apiGetJson("/api/web/points/v1/balance", token),
const [avail, entitlement, bills] = await Promise.all([
availablePoints(token),
apiGetJson("/api/web/auth/v1/entitlement_info", token).catch(() => null),
allBills(token).catch(() => null),
])
const avail = Math.floor(Number(points?.available_points) || 0)
// entitlement_info answers 403 too (plugins #520): an unread plan is
// left unsaid, not read as Free.
const office = entitlement?.office ?? entitlement?.code ?? {}
const proEnable = office?.pro_enable === true || entitlement?.code?.pro_enable === true
const planType = office?.active_plan?.type || office?.plan_type || ""
const plan = proEnable ? (planType && planType !== "none" ? planType : "Pro") : "Free"
const plan = !entitlement ? undefined : proEnable ? (planType && planType !== "none" ? planType : "Pro") : "Free"
const expireAt = office?.pro_expired_time || office?.active_plan?.subscription_expired_at || null
// Sum only what the vendor actually reports as granted in unexpired bills.
@@ -671,7 +700,7 @@ async function queryUsage(token) {
if (grantedFromBills > 0 && grantedFromBills >= avail) {
const used = grantedFromBills - avail
return {
plan,
...(plan ? { plan } : {}),
...(proEnable && expireAt ? { until: new Date(expireAt).toISOString() } : {}),
windows: [
{
@@ -688,7 +717,7 @@ async function queryUsage(token) {
}
return {
plan,
...(plan ? { plan } : {}),
...(proEnable && expireAt ? { until: new Date(expireAt).toISOString() } : {}),
balance: `${avail} credits`,
windows: [],
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-raccoon-auth",
"version": "0.1.6",
"version": "0.1.7",
"description": "OpenCode & Magpie provider plugin for SenseTime Raccoon (商汤小浣熊, xiaohuanxiong.com)",
"type": "module",
"main": "./opencode.mjs",
+89
View File
@@ -381,6 +381,95 @@ test("usage reads bills past the newest 200 rows, where the large grants are (pl
expect(asked).toEqual([1000, 5000])
})
// plugins #520: balance and entitlement_info answer 403 to every account,
// the vendor's own web page too; setting_info still carries the balance.
function forbidden() {
return new Response(JSON.stringify({ code: 403, message: "Forbidden" }), { status: 403 })
}
test("usage reads the balance from setting_info when balance answers 403 (plugins #520)", async () => {
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
startServer({
"GET /api/web/points/v1/balance": forbidden,
"GET /api/web/auth/v1/entitlement_info": forbidden,
"GET /api/web/office/v3/setting_info": () => Response.json({ code: 0, message: "success", data: { available_points: 9599 } }),
"GET /api/web/points/v1/bills": () => Response.json({ code: 0, data: { items: [] } }),
})
const hooks = await RaccoonAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000 }))
expect(u.error).toBeUndefined()
expect(u.balance).toBe("9599 credits")
// entitlement_info was not read, so the plan is not known to be Free
expect(u.plan).toBeUndefined()
})
test("setting_info's balance fills the Credits window the bills account for (plugins #520)", async () => {
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
startServer({
"GET /api/web/points/v1/balance": forbidden,
"GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: { office: { pro_enable: false } } }),
"GET /api/web/office/v3/setting_info": () => Response.json({ code: 0, data: { available_points: 9519 } }),
"GET /api/web/points/v1/bills": () =>
Response.json({ code: 0, data: { items: [{ bill_id: "b1", biz_type: "reward_grant", points: 9600 }] } }),
})
const hooks = await RaccoonAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000 }))
expect(u.plan).toBe("Free")
expect(u.windows[0]).toMatchObject({ name: "Credits", amount: 81, limit: 9600 })
})
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
startServer({
"GET /api/web/points/v1/balance": () => Response.json({ code: 0, data: { available_points: 500 } }),
"GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: {} }),
"GET /api/web/office/v3/setting_info": () => {
asked++
return Response.json({ code: 0, data: { available_points: 1 } })
},
"GET /api/web/points/v1/bills": () => Response.json({ code: 0, data: { items: [] } }),
})
const hooks = await RaccoonAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000 }))
expect(u.balance).toBe("500 credits")
expect(asked).toBe(0)
})
test("with neither balance read, usage says the error, not 0 credits (plugins #520)", async () => {
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
startServer({
"GET /api/web/points/v1/balance": forbidden,
"GET /api/web/auth/v1/entitlement_info": forbidden,
"GET /api/web/office/v3/setting_info": () => Response.json({ code: 0, data: { theme: "light" } }),
"GET /api/web/points/v1/bills": () => Response.json({ code: 0, data: { items: [] } }),
})
const hooks = await RaccoonAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000 }))
expect(u.balance).toBeUndefined()
expect(u.error).toBe("Forbidden")
expect(u.signIn).toBe("kept")
})
test("an expired balance read is a sign-in to renew, not a setting_info read (plugins #520)", async () => {
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
let asked = 0
startServer({
"GET /api/web/points/v1/balance": () => new Response("unauthorized", { status: 401 }),
"GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: {} }),
"GET /api/web/office/v3/setting_info": () => {
asked++
return Response.json({ code: 0, data: { available_points: 1 } })
},
"GET /api/web/points/v1/bills": () => Response.json({ code: 0, data: { items: [] } }),
})
const hooks = await RaccoonAuthPlugin({ client: {} })
// a record with no refresh token is not renewed, so the expiry comes back as said
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, expires: Date.now() + 3600_000 }))
expect(u.signIn).toBe("expired")
expect(asked).toBe(0)
})
test("checkin preserves renewed tokens, claims once, and returns done on second call", async () => {
const staleTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) - 5 })
const freshTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })