diff --git a/packages/sensenova/README.md b/packages/sensenova/README.md index 3ca0966..d784d11 100644 --- a/packages/sensenova/README.md +++ b/packages/sensenova/README.md @@ -21,9 +21,13 @@ API Key 是可选的:不填,账号仍显示额度,只是不服务模型。 ## 额度 读 `GET /lite/console/v1/tokenplan/pool-usage`,把每个积分池的 -`window_5h` / `window_7d` 显示成额度窗口,把 `grant_balance` 显示成会过期 -的返赠余额。每个池的窗口标注它覆盖的模型(`models` 字段),让 magpie 按模型 -分组渲染——通用积分池与 Flash-Lite 积分池各自独立显示,不挤进同一个 tooltip。 +`window_5h` / `window_7d` 显示成额度窗口,把 `grant_balance` 显示成卡片的 +**余额**——只放数字(magpie 自己的千分位格式)加「积分」,因为这一栏是按短 +数字排版的;到期时间(`nearest_grant_expiry`)不混进度数串,走 `until` +(`renew: "off"`),由 magpie 在卡片头部渲染成本地化的「x月x日到期」。多个 +积分池的赠款相加,到期取最早的一个。每个池的窗口标注它覆盖的模型(`models` +字段),让 magpie 按模型分组渲染——通用积分池与 Flash-Lite 积分池各自独立 +显示,不挤进同一个 tooltip。 ## 模型 diff --git a/packages/sensenova/index.mjs b/packages/sensenova/index.mjs index 69bfa29..e996df0 100644 --- a/packages/sensenova/index.mjs +++ b/packages/sensenova/index.mjs @@ -450,12 +450,28 @@ function modelOf(m, provider) { // rest in the row's tooltip, so each window says which models its pool covers // — the host keeps that field (`ids(w?.models)`) and the GUI groups windows by // it, which gives every pool its own meters instead of one pool's meters with -// the rest hidden. The grants — a balance that expires, not a window that gets -// spent — go to `balance`, which the GUI draws as its own chip beside them. +// the rest hidden. +// +// The grants — a balance that expires, not a window that gets spent — go to +// `balance`, which the GUI draws as its own chip under the windows. That chip +// is styled for a short figure ("¥12.34", "1,234 积分"), so the balance is the +// number alone, in the console's own word for it (积分, as joycode's balance +// does), and formatted like magpie's own countFormat: thousands separators, +// at most two decimals. The full raw grant amount stays in the card's +// tooltip, which carries the same string. +// +// When the grants expire is not said in the chip: it goes to `until` (with +// `renew: "off"`), which magpie renders in the card's head as its own +// localized "expires" date — planTerm's "Expires {date}" — instead of a date +// bolted onto a figure. Several granted pools add up (a pool's grant is its +// own; the chip is the account's), and the earliest expiry is the one told, +// that being when the first of them is lost. function usageOf(pools) { + const count = new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 }) const round = (n) => Math.round(n * 100) / 100 const windows = [] - let grants = "" + let grants = 0 + let expiryAt = 0 const ordered = [...(Array.isArray(pools) ? pools : [])].sort( (a, b) => (a?.pool_type === "default" ? -1 : 1) - (b?.pool_type === "default" ? -1 : 1), ) @@ -489,11 +505,17 @@ function usageOf(pools) { } const grant = num(pool?.grant_balance) if (grant !== undefined && grant > 0) { + grants += grant const expiry = num(pool?.nearest_grant_expiry) - grants = `${round(grant)} credits granted${expiry && expiry > 0 ? " · expires " + new Date(expiry * 1000).toISOString().slice(0, 10) : ""}` + if (expiry && expiry > 0 && (!expiryAt || expiry < expiryAt)) expiryAt = expiry } } - return { windows, ...(grants ? { balance: grants } : {}) } + if (!(grants > 0)) return { windows } + return { + windows, + balance: `${count.format(grants)} 积分`, + ...(expiryAt > 0 ? { until: new Date(expiryAt * 1000).toISOString(), renew: "off" } : {}), + } } async function poolUsage(access) { @@ -653,6 +675,8 @@ function makePlugin() { const card = (u, signIn) => ({ plan: u.plan, ...(u.balance ? { balance: u.balance } : {}), + // the grants' expiry, as the head's own localized "expires" date + ...(u.until ? { until: u.until, renew: u.renew } : {}), windows: u.windows, user: auth.accountId ?? auth.email ?? "sensenova", signIn, diff --git a/packages/sensenova/package.json b/packages/sensenova/package.json index d84e924..c32ddcd 100644 --- a/packages/sensenova/package.json +++ b/packages/sensenova/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-sensenova-auth", - "version": "0.1.1", + "version": "0.1.2", "description": "SenseNova (SenseTime 日日新) Token Plan in OpenCode and magpie: one sign-in gives the console account's credit pools, windows and grants, and (with an optional API key) the plan's models for chat", "type": "module", "main": "./opencode.mjs", diff --git a/packages/sensenova/usage.test.mjs b/packages/sensenova/usage.test.mjs index 1d0eafb..4ee671b 100644 --- a/packages/sensenova/usage.test.mjs +++ b/packages/sensenova/usage.test.mjs @@ -69,12 +69,43 @@ test("a grant is a balance that expires, told as the account's balance", async ( f = fakeSenseNova() f.route("GET /lite/console/v1/tokenplan/pool-usage", () => json({ plan: { name: "Free Plan" }, pools: [pool()] })) const u = await usage() - // the GUI draws a usage balance as its own chip; a grant is never a window - expect(u.balance).toContain("12733.76") - expect(u.balance).toContain("granted") + // the GUI draws a usage balance as its own chip, styled for a short figure: + // the number in magpie's own countFormat (separators, ≤ 2 decimals), in the + // console's own word — the expiry is not in the chip but on `until` (with + // renew "off"), which the GUI renders in the head as "Expires {date}" + expect(u.balance).toBe("12,733.76 积分") + expect(u.until).toBe(new Date(1732000000 * 1000).toISOString()) + expect(u.renew).toBe("off") expect(u.windows.some((w) => w.name.includes("grants"))).toBe(false) }) +test("several granted pools add up, and the earliest expiry is the one told", async () => { + f = fakeSenseNova() + f.route("GET /lite/console/v1/tokenplan/pool-usage", () => + json({ + plan: { name: "Free Plan" }, + pools: [ + pool({ grant_balance: "100", nearest_grant_expiry: "1732000000" }), + pool({ name: "Flash-Lite积分池", pool_type: "dedicated", grant_balance: "50.5", nearest_grant_expiry: "1731000000" }), + ], + }), + ) + const u = await usage() + expect(u.balance).toBe("150.5 积分") + expect(u.until).toBe(new Date(1731000000 * 1000).toISOString()) +}) + +test("a grant with no expiry is a balance alone", async () => { + f = fakeSenseNova() + f.route("GET /lite/console/v1/tokenplan/pool-usage", () => + json({ plan: { name: "Free Plan" }, pools: [pool({ nearest_grant_expiry: "0" })] }), + ) + const u = await usage() + expect(u.balance).toBe("12,733.76 积分") + expect(u.until).toBeUndefined() + expect(u.renew).toBeUndefined() +}) + test("a pool with no window of its own is left out", async () => { f = fakeSenseNova() f.route("GET /lite/console/v1/tokenplan/pool-usage", () =>