diff --git a/packages/raccoon/README.md b/packages/raccoon/README.md index 319bf6c..3f23e72 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 套餐状态。 +- **积分余额与套餐查询**:展示可用积分、未过期发放额度、每日积分过期时间与 Pro/Free 套餐状态。积分余额接口 `/api/web/points/v1/balance` 拒绝时(403),改从 `/api/web/office/v3/setting_info` 读可用积分;套餐接口读不到时不显示套餐,而不是当作 Free。 - **每日签到与桌面端登录奖励**:支持 Magpie 每日自动签到(`+300` 积分/天)及首次桌面端 `+3000` 积分登录奖励领取。 - **SSE 模型名重写**:将上游响应中的内部模型名重写为下游请求的 `model`,确保 Claude Code / Codex / OpenCode 等客户端兼容。 diff --git a/packages/raccoon/index.mjs b/packages/raccoon/index.mjs index 75e1fe4..9631010 100644 --- a/packages/raccoon/index.mjs +++ b/packages/raccoon/index.mjs @@ -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: [], diff --git a/packages/raccoon/package.json b/packages/raccoon/package.json index 1dc236a..40d4d23 100644 --- a/packages/raccoon/package.json +++ b/packages/raccoon/package.json @@ -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", diff --git a/packages/raccoon/raccoon.test.mjs b/packages/raccoon/raccoon.test.mjs index c88d824..2f11958 100644 --- a/packages/raccoon/raccoon.test.mjs +++ b/packages/raccoon/raccoon.test.mjs @@ -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 })