sensenova 0.1.3: the grants' expiry back beside the figure

sensenova 0.1.3: the grants' expiry goes back beside the figure ("175,089.31 积分 · 11-04 到期", full date once the year differs, UTC). `until`/`renew` are no longer sent, so the card head's plan-term slot is the plan's alone again (#695 had put the credits' expiry there). The PR's zen-free 0.1.24 -> 0.1.25 bump is already on main, so it merges to nothing.

Run at reviewed head 952dd27: `bun test packages/sensenova` (46 pass) and `bun scripts/check.mjs sensenova` / `zen-free` on mote via mote-run; CI test + on-npm green. Broke it by hand (dropped the date from the balance string): the 3 grant tests went red. Not tried: against a real SenseNova account.
This commit is contained in:
Joey Shao
2026-11-04 10:49:50 +08:00
committed by GitHub
parent f7e87877e0
commit 0ca5ef59aa
4 changed files with 44 additions and 37 deletions
+6 -6
View File
@@ -22,12 +22,12 @@ API Key 是可选的:不填,账号仍显示额度,只是不服务模型。
读 `GET /lite/console/v1/tokenplan/pool-usage`,把每个积分池的
`window_5h` / `window_7d` 显示成额度窗口,把 `grant_balance` 显示成卡片的
**余额**——只放数字(magpie 自己的千分位格式)加「积分」,因为这一栏是按短
数字排版的;到期时间(`nearest_grant_expiry`)不混进度数串,走 `until`
(`renew: "off"`),由 magpie 在卡片头部渲染成本地化的「x月x日到期」。多个
积分池的赠款相加,到期取最早的一个。每个池的窗口标注它覆盖的模型(`models`
字段),让 magpie 按模型分组渲染——通用积分池与 Flash-Lite 积分池各自独立
显示,不挤进同一个 tooltip。
**余额**——数字(magpie 自己的千分位格式)加「积分」,到期时间紧凑地跟在
旁边(`· 11-04 到期`,跨年带年份)。到期是**积分的到期**,不是套餐的,所以
不放进卡片头部的套餐到期位(`until`)。这一栏按短数字排版,太长的句子会被
放大到占满整张卡。多个积分池的赠款相加,到期取最早的一个。每个池的窗口标注
它覆盖的模型(`models` 字段),让 magpie 按模型分组渲染——通用积分池与
Flash-Lite 积分池各自独立显示,不挤进同一个 tooltip。
## 模型
+20 -18
View File
@@ -454,18 +454,17 @@ function modelOf(m, provider) {
//
// 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.
// is styled for a short figure ("¥12.34", "1,234 积分"), so the balance stays
// a short line: the figure in magpie's own countFormat (thousands separators,
// at most two decimals), the console's own word for it (积分, as joycode's
// balance does), and the expiry right beside it — the expiry belongs to the
// credits, not to the plan, so it must not sit in the head's plan-term slot.
// The date is UTC, as the console's epoch seconds read, month and day within
// the current year and with the year once it differs.
//
// 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.
// 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
@@ -511,11 +510,16 @@ function usageOf(pools) {
}
}
if (!(grants > 0)) return { windows }
return {
windows,
balance: `${count.format(grants)} 积分`,
...(expiryAt > 0 ? { until: new Date(expiryAt * 1000).toISOString(), renew: "off" } : {}),
}
// the expiry day, UTC as the console's epoch reads: "11-04" this year,
// "2027-03-01" once the year differs
const day = expiryAt > 0
? (() => {
const d = new Date(expiryAt * 1000), now = new Date()
const md = `${String(d.getUTCMonth() + 1).padStart(2, "0")}-${String(d.getUTCDate()).padStart(2, "0")}`
return d.getUTCFullYear() === now.getUTCFullYear() ? md : `${d.getUTCFullYear()}-${md}`
})()
: ""
return { windows, balance: `${count.format(grants)} 积分${day ? " · " + day + " 到期" : ""}` }
}
async function poolUsage(access) {
@@ -675,8 +679,6 @@ 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,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-sensenova-auth",
"version": "0.1.2",
"version": "0.1.3",
"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",
+17 -12
View File
@@ -19,7 +19,7 @@ const pool = (over = {}) => ({
window_5h: { limit: "120000", used: "39657.43456", remaining: "80342.56544", reset_at: "1730000000" },
window_7d: { limit: "1200000", used: "237942.98928", remaining: "962057.01072", reset_at: "1731000000" },
grant_balance: "12733.76104",
nearest_grant_expiry: "1732000000",
nearest_grant_expiry: "2000000000",
...over,
})
@@ -71,28 +71,35 @@ test("a grant is a balance that expires, told as the account's balance", async (
const u = await usage()
// 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")
// console's own word, the expiry beside it — the expiry is the credits',
// not the plan's, so it never goes near the head's plan-term slot. 2000000000
// is 2033-05-18 UTC, a year apart from any run date, so the full date shows
expect(u.balance).toBe("12,733.76 积分 · 2033-05-18 到期")
expect(u.windows.some((w) => w.name.includes("grants"))).toBe(false)
})
test("a grant expiring this year tells the month and day alone", async () => {
f = fakeSenseNova()
// two days out: this year or the next by a hair, both shapes legal
const soon = String(Math.floor(Date.now() / 1000) + 2 * 86400)
f.route("GET /lite/console/v1/tokenplan/pool-usage", () => json({ plan: { name: "Free Plan" }, pools: [pool({ nearest_grant_expiry: soon })] }))
const u = await usage()
expect(u.balance).toMatch(/^12,733\.76 积分 · (?:\d{4}-)?\d{2}-\d{2} 到期$/)
})
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" }),
pool({ grant_balance: "100", nearest_grant_expiry: "2100000000" }),
pool({ name: "Flash-Lite积分池", pool_type: "dedicated", grant_balance: "50.5", nearest_grant_expiry: "2000000000" }),
],
}),
)
const u = await usage()
expect(u.balance).toBe("150.5 积分")
expect(u.until).toBe(new Date(1731000000 * 1000).toISOString())
expect(u.balance).toBe("150.5 积分 · 2033-05-18 到期")
})
test("a grant with no expiry is a balance alone", async () => {
@@ -102,8 +109,6 @@ test("a grant with no expiry is a balance alone", async () => {
)
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 () => {