dsh-opencode-status 0.4.2 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +8 -6
- package/package.json +1 -1
- package/src/client.js +253 -27
- package/src/index.js +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.5
|
|
4
|
+
|
|
5
|
+
- **修复用量面板长期停在「采样中」、估算始终不出现的问题**:
|
|
6
|
+
- 现象:面板三个窗口的估算位置长期只显示「采样中」,攒不出「约 X 后耗尽 / 可支撑到重置」;刷新页面或重启 DSH 后依旧如此,用户无法判断该等还是该重启。
|
|
7
|
+
- 主因(采样停摆):30 秒状态轮询原本建在侧栏按钮组件 `FooterEntry` 的挂载 `useEffect` 里——侧栏收起、布局切换导致组件卸载,或页面隐藏时 `clearInterval` 之后 `visibilitychange` 事件丢失,ticker 都不会再被重建,采样永久停止;面板读到的其实是冻结在 1–2 个内存样本的快照,而「采样中」这个状态本身(有样本但不足 3 个点、或首尾跨度不足 10 分钟)恰好掩盖了「轮询已经死了」。诊断时实测页面根本没有在按 30 秒轮询状态接口(7/7 探测直接穿透到上游),周 / 月窗口的 `resetAt` 与 `usedPct` 在真实数据里全程恒定。
|
|
8
|
+
- 次因(`resetAt` 被误当作窗口重置):`recordSamples` 原先用 `resetAt !== entry.resetAt` 判定「窗口已重置」并清空历史。但 5h 滚动窗口是滑动窗口,早先用量的过期会让重置时间不断前移——窗口根本没有重置——于是每次采样都清空历史,样本永远攒不到 3 个点。构造 `resetAt` 每 30 秒前移、`usedPct` 单调上升的 22 样本序列可 100% 复现同样的「采样中」。
|
|
9
|
+
- 不是接口数据、也不是估算算法的问题:真实 24 条响应回放显示估算链路本身健康(每 5 个样本 +1% 的健康输入在第 21 个样本、跨过 10 分钟门槛后即给出「约 3h30m 后耗尽」)。
|
|
10
|
+
- **修复方式**:
|
|
11
|
+
- 轮询提升为模块级幂等 ticker:`ensureStatusPolling` 只安装一次,footer 只幂等调用、组件卸载不再拆除,并随插件生命周期释放——`apply` 内改用 `ctx.effect(() => ensureStatusPolling())` 注册 disposer,插件 dispose / HMR 重载时 `clearInterval`、移除 `visibilitychange` 监听并复位状态,不再出现新旧 ticker 并存、每 30 秒发两次请求。页面隐藏期间保留 ticker(交给浏览器节流),恢复可见时立即补拉一次。
|
|
12
|
+
- `recordSamples` 删除 `resetAt !== entry.resetAt` 清空条件,只按 `usedPct` 真实回落(`used < last.used`)清空历史,`resetAt` 仅更新基准。源码注释写明前提(同一窗口内 `usedPct` 单调不减)与已知漏检(重置恰好发生在两次轮询之间、且新窗口用量已超过旧值时不会清空),并明确不能改用 `resetAt` 兜底。同一 `fetchedAt` 去重与 60 分钟 / 每窗口 120 条上限保持不变。
|
|
13
|
+
- 采样持久化到 `localStorage`(键 `dsh-opencode-status.eta-samples`,v2;每窗口最近 60 分钟、最多 120 条、按 `fetchedAt` 去重,只存窗口 id、`fetchedAt`、用量百分比与 `resetAt`,不含任何密钥或凭证),并按账号隔离:payload 带 `baseUrl` 与可获取时的密钥末四位掩码(不是密钥本身),恢复延迟到首个 status/config 身份响应,`baseUrl` 或双方已知末四位不匹配则整体丢弃并清除存储;运行时换 `baseUrl` / 换账号、设置保存与清除 Key 后立即作废旧历史。判据遵循「缺证据不下结论」:末四位晚到且相同只补写身份、不清历史;旧版 v1 无归属信息,整体丢弃不恢复。页面刷新与 DSH 重启后接着采样,不必再从 1 个点重新等满 10 分钟。
|
|
14
|
+
- **诚实性边界不变**:估算门槛与呈现规则一字未改——仍然要求样本 ≥3 个、首尾跨度 ≥10 分钟、速率 >0;速率为 0 / 剩余为 0 / `resetAt` 缺失或过期时该位置依旧完全不渲染,绝不补「--」「0m」占位。`estimateEta` / `etaView` 函数体与 0.4.4 逐字相同,「采样中」的文案与语义(只说明状态、不含任何数字)也保持原样;本次没有删掉「采样中」、没有放宽门槛、没有为了让数字出现而编造估算。
|
|
15
|
+
- 测试 49 → 56(48 → 55 个 `test()`,另 1 条文件级条目):按修复契约反转了「`resetsAt` 变化即清空」的旧断言,把旧的轮询源码字符串断言改为可控定时器 + 可见性的运行时用例,并新增模块级 ticker 生命周期、`resetAt` 前移累积出估算、`usedPct` 回落清空、同一 `fetchedAt` 去重、`localStorage` 恢复与重载续采、v2 账号隔离(含「末四位晚到、同账号不清空」与「换账号整体丢弃」)等真实运行时回归;其余既有断言未改,`npm test` 56/56 全绿。
|
|
16
|
+
- 已知限制(均为 low,不影响本次修复):持久化归属里的 `baseUrl` 按字符串精确比较,同一地址的不同写法(如结尾斜杠差异)会被判为换了账号并丢弃旧历史;轮询 disposer 是模块级共享函数、没有代际 token,正常 DSH「先 dispose 旧实例再 apply 新实例」的顺序不会触发问题,但旧实例的 disposer 迟到调用可能清掉新一代 ticker。
|
|
17
|
+
|
|
18
|
+
## 0.4.4
|
|
19
|
+
|
|
20
|
+
- **去掉面板内的两处重复**:
|
|
21
|
+
- 状态行原本是「OpenCode GO 已连接」,而标题已经是「OpenCode GO 用量」,同一产品名在 40px 内出现两次;现在状态行只表达连接状态(「已连接」/「异常」)。
|
|
22
|
+
- 头部「最紧张」摘要不再带百分比:该数字在对应窗口行已经出现一次(`最紧张 月窗口` + `月窗口 剩余 78%` 是同一个数),现在摘要只给判断,具体数值保留在悬停 `title` 里。
|
|
23
|
+
- 测试 49 保持全绿:更新 2 处头部摘要断言,并新增「该百分比只在窗口行出现一次」的反向断言。
|
|
24
|
+
|
|
25
|
+
## 0.4.3
|
|
26
|
+
|
|
27
|
+
- **修掉主面板的中英夹杂**:面板头部「最紧张」摘要此前复用了 `shortLabel`(`Weekly` / `Monthly`),而那是给 220px 悬浮微卡窄列准备的紧凑缩写。现在头部改用中文窗口名(`最紧张 月窗口 78%`),`shortLabel` 本身也改为 `5h` / `周` / `月`,两个位置都不再出现英文整词。
|
|
28
|
+
- **新增「采样中」提示**:已有采样点但还攒不够(少于 3 个点,或跨度不足 10 分钟)时,原本的位置显示「采样中」,`title` 说明已有几个点、跨了多少分钟。此前这种情况与「插件没加载」在界面上完全一样,都是空白,用户无法判断该等还是该重启。它只说明状态、不含任何数字,速率为 0 / 剩余为 0 / 重置过期时仍然完全不渲染。
|
|
29
|
+
- 面板头部标题在窄视口下可省略(`flexShrink:1` + ellipsis),把空间让给右侧摘要,避免中文全名撑破一行。
|
|
30
|
+
- 测试 48 → 49:新增「采样中 → 真实估算」的状态迁移用例与悬浮卡不显示采样提示的断言。
|
|
31
|
+
|
|
3
32
|
## 0.4.2
|
|
4
33
|
|
|
5
34
|
- **估算时长按两位有效数字粗化**:`<1h` 取 5 分钟、`1–10h` 取 30 分钟、`≥10h` 取 1 小时、`≥48h` 取 1 天(下限 5 分钟,不会出现「约 0m」)。上游只给整数 percent,量化误差会让 ETA 的真实区间比点值宽 2–3 倍,分钟级精度属于虚假精确;现在 `约 13h10m 后耗尽` 显示为 `约 13h 后耗尽`,`约 1h54m` 显示为 `约 2h`。
|
package/README.md
CHANGED
|
@@ -11,16 +11,18 @@ OpenCode GO 套餐用量与配额状态监控面板插件(DeepSeek Harness)
|
|
|
11
11
|
- 右侧显示相对时间「上次同步 12s 前」(刚刚 / Ns 前 / N 分钟前 / N 小时前 / N 天前),绝对时刻 HH:MM:SS 保留在 `title`;同步数据超过 2 分钟追加「· 可能已过期」并用警告色,避免把旧额度当成实时值。
|
|
12
12
|
- **3 维度用量行**:主百分比只给剩余额度,进度条条长与它同源(条长 = 剩余占比,条色 = 剩余档位),「已用」放在 `title` 与 `aria-valuetext` 里,同一行不再重复表达同一个数字;窗口状态非 `ok` 时给出可见提示。
|
|
13
13
|
- **滚动窗口 (5h)**:`剩余 70% · 约 13h 后耗尽 · ⏱ 4h20m`
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
- 面板头部只给判断性摘要「最紧张
|
|
14
|
+
- **周窗口**:周额度剩余 % + 可用时长估算 + ⏱ 重置倒计时
|
|
15
|
+
- **月窗口**:月额度剩余 % + 可用时长估算 + ⏱ 重置倒计时
|
|
16
|
+
- 面板头部只给判断性摘要「最紧张 月窗口」(取剩余最小的窗口,用中文窗口名而不是悬浮卡的紧凑缩写),**不带百分比**——数字只在该窗口行出现一次,悬停摘要可看具体数值。
|
|
17
17
|
- **可用时长估算(按当前速率,仅供参考)**:窗口行会顺带告诉你「按现在的用法还能撑多久」。三种状态严格区分,不存在第四种:
|
|
18
18
|
- `约 13h 后耗尽`(警告色):按当前速率会在窗口重置前用完;
|
|
19
19
|
- `可支撑到重置`(中性色):按当前速率能撑到重置,此时不再显示具体时长;
|
|
20
|
-
-
|
|
20
|
+
- **采样中**:已有采样点但还攒不够(少于 3 个点,或首尾跨度不足 10 分钟)时显示「采样中」,`title` 给出已有几个点、跨了多少分钟。它只是状态说明,不含任何数字,用来区分「功能没加载」和「还在攒数据」。
|
|
21
|
+
- **不显示**:速率为 0、剩余为 0、重置时间缺失或已过期时,这个位置直接不出现,也不用「--」「0m」之类占位凑数。
|
|
21
22
|
- 鼠标悬停会说明依据:「按最近 N 分钟的消耗速率估算(按当前速率),仅供参考」。它是对已有采样点的机械外推(只用首尾两点,不做回归),**不是预测或承诺**。
|
|
22
23
|
- 时长按两位有效数字粗化:<1h 取 5 分钟、1–10h 取 30 分钟、≥10h 取 1 小时、≥48h 取 1 天。上游只有整数百分比,分钟级精度属于虚假精确,粗化后不再暗示比实际更准。
|
|
23
|
-
-
|
|
24
|
+
- 采样同时留在内存与本浏览器的 `localStorage`(键 `dsh-opencode-status.eta-samples`,v2:每窗口最近 60 分钟、最多 120 个点、按 `fetchedAt` 去重,只存窗口 id、`fetchedAt`、用量百分比与重置时间,**不含任何配置、密钥或凭证**)。持久化按账号隔离:样本带 `baseUrl` 与可获取时的密钥末四位(掩码,不是密钥)一起落盘,恢复延迟到首个状态/配置响应、归属一致才接续——`baseUrl` 不同则整体丢弃,两端都有末四位且不同才判为换账号,拿不到末四位时退回只按 `baseUrl` 隔离;换 `baseUrl` / 换账号或清除 Key 会立即作废旧历史。因此页面刷新、DSH 重启后会接着之前的样本继续采样,不必再从 1 个点重新等满 10 分钟;存储不可用(沙箱、隐私模式、配额满、数据损坏)时静默降级为纯内存采样。全新环境或历史已过期时仍需重新积累约 10 分钟跨度;上游只给整数百分比,样本不足时宁可不出数,也不给看起来精确的假时长。
|
|
25
|
+
- 窗口重置只认「`usedPct` 真实回落」:同一窗口内上游百分比只增不减,回落才说明窗口重置、需要清空历史。重置时间 `resetAt` 前移不算重置——5h 滚动窗口是滑动窗口,早先用量的过期本来就会让重置时间前移,把它当重置信号会导致每次采样都清空、样本永远攒不够(0.4.5 修复的次因)。已知漏检:重置恰好发生在两次轮询之间、且新窗口用量已超过旧值(例如 5 → 0 → 6)时不会清空,历史会短暂跨重置累积;这是有意取舍,宁可漏检也不用 `resetAt` 兜底。
|
|
24
26
|
- 纯前端计算:不新增网络请求、不新增依赖,数据仍只来自既有的 30 秒轮询与手动刷新。220ms 悬浮微卡不显示估算(一行放不下五项,缩字号或截断都不可接受,因此有意省略)。
|
|
25
27
|
- **可访问性与键盘交互**:
|
|
26
28
|
- 面板是 `role="dialog"`,`aria-label` 随视图切换(「OpenCode GO 用量」/「OpenCode GO 连接配置」)。
|
|
@@ -31,7 +33,7 @@ OpenCode GO 套餐用量与配额状态监控面板插件(DeepSeek Harness)
|
|
|
31
33
|
- 面板宽度为 `min(360px, calc(100vw - 24px))`,窄窗口(<372px)不再右侧溢出。
|
|
32
34
|
- **连接设置视图**:未配置(`mode === 'needs_config'`)时面板直接显示设置视图;保存成功后自动回到用量视图;「取消」丢弃未保存的编辑并关闭面板。密钥仅存入宿主凭据库(`OPENCODE_GO_API_KEY`),API 响应不回传明文。数据口径说明(上游只提供百分比、界面不展示金额余额)放在此视图底部,不占用用量视图。
|
|
33
35
|
- **220ms 悬浮微卡**:鼠标悬停常驻按钮 220ms 后在面板关闭且连接正常时出现,展示标题 + GO 标识 + 最近同步时间(相对时间)、三个窗口的紧凑行(名称 / 剩余 % / ⏱ 重置倒计时 / 细进度条)与底部「点击查看详情」;纯展示卡片,不拦截按钮点击。
|
|
34
|
-
- **缓存与刷新**:服务端 30 秒缓存 + 客户端 30
|
|
36
|
+
- **缓存与刷新**:服务端 30 秒缓存 + 客户端 30 秒自动刷新。自动轮询是插件级单例:插件激活时安装一次,侧栏按钮组件卸载(侧栏收起、布局切换)不会停表,只有插件被卸载 / 热替换时才随生命周期释放(避免旧 ticker 残留);页面隐藏期间保留 ticker、由浏览器自行节流,恢复可见时立即补拉一次。打开面板、切换视图不会额外探测上游,手动刷新是唯一绕过缓存的入口。
|
|
35
37
|
- **额度语义**:上游 `/v1/usage` 只提供 rolling/weekly/monthly 百分比,没有 USD 余额接口([anomalyco/opencode#10448](https://github.com/anomalyco/opencode/issues/10448) 仍 open),因此界面只展示「剩余额度 / 可用百分比」,不编造金额余额;百分比由宿主统一取整并钳制到 0–100,剩余与已用恒和 100。
|
|
36
38
|
|
|
37
39
|
## 安装与更新
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-opencode-status",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"description": "OpenCode GO quota status panel for DeepSeek Harness: click to open a usage panel with manual refresh, per-window remaining quota, a hover overview card, and in-app connection settings",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
package/src/client.js
CHANGED
|
@@ -176,17 +176,164 @@ window.__ModuleLoader__.load({
|
|
|
176
176
|
const ETA_MAX_SAMPLES = 120
|
|
177
177
|
const ETA_MIN_SAMPLES = 3
|
|
178
178
|
const ETA_MIN_SPAN_MS = 10 * 60_000
|
|
179
|
+
// 持久化键与版本:v2 起带账号归属(baseUrl + 可选 keyHint)。
|
|
180
|
+
const ETA_SAMPLES_STORAGE_KEY = 'dsh-opencode-status.eta-samples'
|
|
181
|
+
const ETA_SAMPLES_STORAGE_VERSION = 2
|
|
179
182
|
|
|
180
183
|
/** windowId -> { resetAt, samples: [{ at, used }] };at 取 status.fetchedAt(不是 Date.now)。 */
|
|
181
184
|
const etaSamples = new Map()
|
|
182
185
|
|
|
183
186
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
187
|
+
* 采样归属身份:baseUrl 必有(status.config.baseUrl / config.baseUrl),apiKeyHint 取自
|
|
188
|
+
* GET /api/opencode-status/config 的 apiKey.hint(只含末四位的掩码,不是密钥本身),可能拿不到。
|
|
189
|
+
* 持久化按身份隔离,判据遵循「缺证据不下结论」:baseUrl 不同才确凿丢弃;hint 只有双方都存在
|
|
190
|
+
* 且不同才确凿丢弃;任一方 hint 缺失(例如 status 先到、config 后到)退回只按 baseUrl 隔离,保留历史。
|
|
191
|
+
* 已知限制:同一 baseUrl 下换账号、且拿不到 hint(hint 为空)时无法识别,旧样本会被复用;
|
|
192
|
+
* 此时只按 baseUrl 隔离——宁可暂时不分账号,也不能误删同账号数据。
|
|
193
|
+
*/
|
|
194
|
+
const etaIdentity = { baseUrl: null, keyHint: null }
|
|
195
|
+
let etaRestorePayload = null
|
|
196
|
+
let etaRestoreSettled = false
|
|
197
|
+
// 恢复时持久化里记录的 hint:首轮拿不到 hint(只按 baseUrl 恢复)时,用它在 hint 晚到后补一次账号校验。
|
|
198
|
+
let etaRestoredKeyHint = null
|
|
199
|
+
|
|
200
|
+
/** 读取持久化原文但先不恢复:身份要等首个 status/config 响应才知道,不能盲信旧账号样本。 */
|
|
201
|
+
function readEtaSamplesPayload() {
|
|
202
|
+
try {
|
|
203
|
+
if (typeof localStorage === 'undefined' || !localStorage) {
|
|
204
|
+
etaRestoreSettled = true
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
const raw = localStorage.getItem(ETA_SAMPLES_STORAGE_KEY)
|
|
208
|
+
if (!raw) {
|
|
209
|
+
etaRestoreSettled = true
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
const parsed = JSON.parse(raw)
|
|
213
|
+
// 旧版 v1 没有账号归属,无法校验是否同一账号:整体丢弃,不做部分恢复。
|
|
214
|
+
if (!parsed || parsed.v !== ETA_SAMPLES_STORAGE_VERSION || typeof parsed.baseUrl !== 'string' || !parsed.baseUrl) {
|
|
215
|
+
etaRestoreSettled = true
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
etaRestorePayload = parsed
|
|
219
|
+
} catch {
|
|
220
|
+
etaRestoreSettled = true
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 判断持久化样本是否属于当前账号:baseUrl 必须一致;双方都有 hint 且不同才判为不同账号。 */
|
|
225
|
+
function etaIdentityMatches(payload) {
|
|
226
|
+
if (!etaIdentity.baseUrl || payload?.baseUrl !== etaIdentity.baseUrl) return false
|
|
227
|
+
const storedHint = typeof payload.keyHint === 'string' && payload.keyHint ? payload.keyHint : null
|
|
228
|
+
if (storedHint && etaIdentity.keyHint && storedHint !== etaIdentity.keyHint) return false
|
|
229
|
+
return true
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 首次拿到 baseUrl 后恢复持久化样本:只保留最近 60 分钟、每窗口最多 120 个样本、按 fetchedAt 去重。
|
|
234
|
+
* 身份不匹配(或旧版无身份)时整体丢弃,绝不部分恢复;存储不可用/损坏时静默降级为空历史。
|
|
235
|
+
*/
|
|
236
|
+
function restoreEtaSamples() {
|
|
237
|
+
if (etaRestoreSettled || !etaIdentity.baseUrl) return
|
|
238
|
+
etaRestoreSettled = true
|
|
239
|
+
const payload = etaRestorePayload
|
|
240
|
+
etaRestorePayload = null
|
|
241
|
+
etaRestoredKeyHint = null
|
|
242
|
+
if (!payload || !etaIdentityMatches(payload)) {
|
|
243
|
+
// 不是当前账号的历史:清掉,避免换账号/换 baseUrl 后混用出看似正常的错误估算。
|
|
244
|
+
if (payload) {
|
|
245
|
+
try {
|
|
246
|
+
if (typeof localStorage !== 'undefined' && localStorage) localStorage.removeItem(ETA_SAMPLES_STORAGE_KEY)
|
|
247
|
+
} catch {}
|
|
248
|
+
}
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
etaRestoredKeyHint = typeof payload.keyHint === 'string' && payload.keyHint ? payload.keyHint : null
|
|
252
|
+
try {
|
|
253
|
+
const storedWindows = payload.windows
|
|
254
|
+
if (!storedWindows || typeof storedWindows !== 'object' || Array.isArray(storedWindows)) return
|
|
255
|
+
const cutoff = Date.now() - ETA_SAMPLE_WINDOW_MS
|
|
256
|
+
for (const [id, stored] of Object.entries(storedWindows)) {
|
|
257
|
+
if (typeof id !== 'string' || !id) continue
|
|
258
|
+
const rows = Array.isArray(stored?.samples) ? stored.samples : []
|
|
259
|
+
// 同一 fetchedAt 只留一条(与运行时去重口径一致,后写入值覆盖)。
|
|
260
|
+
const byAt = new Map()
|
|
261
|
+
for (const row of rows) {
|
|
262
|
+
if (typeof row?.at !== 'number' || !Number.isFinite(row.at)) continue
|
|
263
|
+
if (typeof row?.used !== 'number' || !Number.isFinite(row.used)) continue
|
|
264
|
+
byAt.set(row.at, { at: row.at, used: row.used })
|
|
265
|
+
}
|
|
266
|
+
const samples = [...byAt.values()].filter((s) => s.at >= cutoff).sort((a, b) => a.at - b.at)
|
|
267
|
+
if (!samples.length) continue
|
|
268
|
+
const resetAt = typeof stored?.resetAt === 'number' && Number.isFinite(stored.resetAt) ? stored.resetAt : null
|
|
269
|
+
etaSamples.set(id, { resetAt, samples: samples.slice(-ETA_MAX_SAMPLES) })
|
|
270
|
+
}
|
|
271
|
+
} catch {}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 记录当前账号身份(baseUrl 必有、keyHint 可选):身份变化说明换了 baseUrl/账号,
|
|
276
|
+
* 旧历史必须整体作废,否则两个账号的 usedPct 会拼出错误速率(例如旧账号 10→20 + 新账号 50)。
|
|
277
|
+
*/
|
|
278
|
+
function noteEtaIdentity(baseUrl, keyHint) {
|
|
279
|
+
let changed = false
|
|
280
|
+
if (typeof baseUrl === 'string' && baseUrl && baseUrl !== etaIdentity.baseUrl) {
|
|
281
|
+
if (etaIdentity.baseUrl) changed = true
|
|
282
|
+
etaIdentity.baseUrl = baseUrl
|
|
283
|
+
}
|
|
284
|
+
if (typeof keyHint === 'string' && keyHint && keyHint !== etaIdentity.keyHint) {
|
|
285
|
+
// status 与 config 是两个请求,config.hint 天然可能晚到(此时 etaIdentity.keyHint 为 null)。
|
|
286
|
+
// 只有「已知 hint 与本次 hint 都确凿存在且不同」才能判定换了账号;缺证据一律不下结论,
|
|
287
|
+
// 否则同一账号在 status 先到、config 后到时会被误判并清空刚恢复的持久化历史。
|
|
288
|
+
const knownHint = etaIdentity.keyHint ?? etaRestoredKeyHint
|
|
289
|
+
if (knownHint && keyHint !== knownHint) changed = true
|
|
290
|
+
// 同 hint 晚到只补写身份,不清空历史。
|
|
291
|
+
etaIdentity.keyHint = keyHint
|
|
292
|
+
}
|
|
293
|
+
if (changed) {
|
|
294
|
+
etaSamples.clear()
|
|
295
|
+
etaRestoredKeyHint = null
|
|
296
|
+
persistEtaSamples()
|
|
297
|
+
}
|
|
298
|
+
restoreEtaSamples()
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** 把采样写回 localStorage:带 baseUrl/keyHint 归属;只含窗口 id、fetchedAt 与用量百分比,不写密钥或凭证。 */
|
|
302
|
+
function persistEtaSamples() {
|
|
303
|
+
try {
|
|
304
|
+
if (typeof localStorage === 'undefined' || !localStorage) return
|
|
305
|
+
if (!etaIdentity.baseUrl) return // 身份未知不落盘,避免无法校验归属的历史
|
|
306
|
+
const windows = {}
|
|
307
|
+
for (const [id, entry] of etaSamples) {
|
|
308
|
+
if (!entry.samples.length) continue
|
|
309
|
+
windows[id] = {
|
|
310
|
+
resetAt: entry.resetAt,
|
|
311
|
+
samples: entry.samples.map(({ at, used }) => ({ at, used })),
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
localStorage.setItem(ETA_SAMPLES_STORAGE_KEY, JSON.stringify({
|
|
315
|
+
v: ETA_SAMPLES_STORAGE_VERSION,
|
|
316
|
+
baseUrl: etaIdentity.baseUrl,
|
|
317
|
+
keyHint: etaIdentity.keyHint,
|
|
318
|
+
windows,
|
|
319
|
+
}))
|
|
320
|
+
} catch {}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* 记录一次成功刷新的样本:按窗口分组,用 fetchedAt 去重(宿主 30s 缓存会让多次轮询拿到同一个时间)。
|
|
325
|
+
* 清空判据的前提:同一窗口内上游 usedPct 单调不减(percent 由窗口内累计用量换算,只增不减);
|
|
326
|
+
* 只有 usedPct 真实回落(used < last.used)才说明窗口已重置、需要清空历史。
|
|
327
|
+
* 已知漏检:窗口重置恰好发生在两次轮询之间、且新窗口用量已超过旧值(例如 5 → 0 → 6)时不会清空,
|
|
328
|
+
* 历史会跨重置累积;0 → 0 的次 1% 重置也无法检测。这是有意的取舍:不能改用 resetAt 兜底——
|
|
329
|
+
* 滚动 5h 是滑动窗口,resetAt 会随早先用量过期不断前移,把它当重置信号正是「永远采样中」的根因。
|
|
186
330
|
*/
|
|
187
331
|
function recordSamples(status) {
|
|
332
|
+
// 先用响应里的 baseUrl 确认账号归属:换账号后旧样本必须整体作废,不能混用两个账号的 usedPct。
|
|
333
|
+
noteEtaIdentity(status?.config?.baseUrl, null)
|
|
188
334
|
if (!status?.ok || typeof status.fetchedAt !== 'number') return
|
|
189
335
|
const windows = Array.isArray(status.windows) ? status.windows : []
|
|
336
|
+
let changed = false
|
|
190
337
|
for (const w of windows) {
|
|
191
338
|
const id = w?.id
|
|
192
339
|
if (!id || typeof w?.usedPct !== 'number') continue
|
|
@@ -200,16 +347,22 @@ window.__ModuleLoader__.load({
|
|
|
200
347
|
}
|
|
201
348
|
const last = entry.samples[entry.samples.length - 1]
|
|
202
349
|
if (last && last.at === at) continue // 同一份缓存数据不重复采样
|
|
203
|
-
|
|
350
|
+
// resetAt 变化只更新基准(下面统一赋值),不清历史;只有用量真实回落才是窗口重置。
|
|
351
|
+
if (last && used < last.used) entry.samples = []
|
|
204
352
|
entry.resetAt = resetAt
|
|
205
353
|
entry.samples.push({ at, used })
|
|
354
|
+
changed = true
|
|
206
355
|
// 只保留最近 60 分钟、每窗口最多 120 个样本。
|
|
207
356
|
const cutoff = at - ETA_SAMPLE_WINDOW_MS
|
|
208
357
|
while (entry.samples.length && entry.samples[0].at < cutoff) entry.samples.shift()
|
|
209
358
|
if (entry.samples.length > ETA_MAX_SAMPLES) entry.samples.splice(0, entry.samples.length - ETA_MAX_SAMPLES)
|
|
210
359
|
}
|
|
360
|
+
if (changed) persistEtaSamples()
|
|
211
361
|
}
|
|
212
362
|
|
|
363
|
+
// 模块加载时先读持久化原文;身份要等首个 status/config 响应,确认同账号后才恢复历史。
|
|
364
|
+
readEtaSamplesPayload()
|
|
365
|
+
|
|
213
366
|
/**
|
|
214
367
|
* 估算「按当前速率还能撑多久」。样本 ≥3、首尾跨度 ≥10 分钟、速率 >0 三者缺一即返回 null;
|
|
215
368
|
* 速率只用首尾两点(不做回归,抗噪且好解释)。resetAt 缺失或已过期时无法判断能否撑到重置,同样返回 null。
|
|
@@ -271,9 +424,22 @@ window.__ModuleLoader__.load({
|
|
|
271
424
|
function etaView(w, options = {}) {
|
|
272
425
|
if (options?.compact) return null
|
|
273
426
|
const entry = etaSamples.get(w?.id)
|
|
274
|
-
if (!entry) return null
|
|
427
|
+
if (!entry || !entry.samples.length) return null
|
|
275
428
|
const estimate = estimateEta(entry.samples, w?.remainingPct, w?.resetAt)
|
|
276
|
-
if (!estimate)
|
|
429
|
+
if (!estimate) {
|
|
430
|
+
// 「采样中」只在样本数或跨度还不够时出现:让用户能区分「功能没加载」与「还在攒数据」。
|
|
431
|
+
// 速率 0、剩余 0、重置缺失或已过期都不是采样问题,仍然什么都不显示。
|
|
432
|
+
const samples = entry.samples
|
|
433
|
+
const span = samples[samples.length - 1].at - samples[0].at
|
|
434
|
+
if (samples.length >= ETA_MIN_SAMPLES && span >= ETA_MIN_SPAN_MS) return null
|
|
435
|
+
if (typeof w?.remainingPct !== 'number' || w.remainingPct <= 0) return null
|
|
436
|
+
if (!w?.resetAt || w.resetAt <= Date.now()) return null
|
|
437
|
+
return {
|
|
438
|
+
text: '采样中',
|
|
439
|
+
color: T.secondary,
|
|
440
|
+
title: `正在积累采样:已有 ${samples.length} 个点、跨度约 ${Math.max(1, Math.round(span / 60_000))} 分钟;需要 ${ETA_MIN_SAMPLES} 个点且跨度满 ${ETA_MIN_SPAN_MS / 60_000} 分钟`,
|
|
441
|
+
}
|
|
442
|
+
}
|
|
277
443
|
const minutes = Math.max(1, Math.round(estimate.spanMs / 60_000))
|
|
278
444
|
// 文案只说「按最近 N 分钟的消耗速率(按当前速率)」的实测依据,不写「预计/将」这类预测口吻。
|
|
279
445
|
const title = `按最近 ${minutes} 分钟的消耗速率估算(按当前速率),仅供参考`
|
|
@@ -289,9 +455,15 @@ window.__ModuleLoader__.load({
|
|
|
289
455
|
return entry ? entry.samples.map((s) => ({ ...s })) : []
|
|
290
456
|
}
|
|
291
457
|
|
|
292
|
-
/**
|
|
458
|
+
/** 仅供测试与诊断:清空所有采样(含持久化副本),并阻止本次生命周期内再从存储恢复。 */
|
|
293
459
|
function resetEtaSamples() {
|
|
294
460
|
etaSamples.clear()
|
|
461
|
+
etaRestorePayload = null
|
|
462
|
+
etaRestoreSettled = true
|
|
463
|
+
etaRestoredKeyHint = null
|
|
464
|
+
try {
|
|
465
|
+
if (typeof localStorage !== 'undefined' && localStorage) localStorage.removeItem(ETA_SAMPLES_STORAGE_KEY)
|
|
466
|
+
} catch {}
|
|
295
467
|
}
|
|
296
468
|
|
|
297
469
|
/** 月窗口剩余百分比;上游只给百分比,不存在金额余额。 */
|
|
@@ -376,10 +548,70 @@ window.__ModuleLoader__.load({
|
|
|
376
548
|
async function refreshConfig() {
|
|
377
549
|
try {
|
|
378
550
|
const config = await api('/api/opencode-status/config')
|
|
551
|
+
// config.apiKey.hint 是「••••末四位」掩码(不是密钥),用来判断持久化样本是否同一账号。
|
|
552
|
+
noteEtaIdentity(config?.baseUrl, config?.apiKey?.hint)
|
|
379
553
|
store.set({ config })
|
|
380
554
|
} catch {}
|
|
381
555
|
}
|
|
382
556
|
|
|
557
|
+
// 30 秒状态轮询是模块级单例:采样服务于面板,不能依赖 footer 组件是否挂载。
|
|
558
|
+
let statusPollingStarted = false
|
|
559
|
+
let statusPollTimer = null
|
|
560
|
+
let visibilityListenerBound = false
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* 页面恢复可见时立即补拉一次;隐藏期间保留 ticker(浏览器会自行节流后台定时器)。
|
|
564
|
+
* 旧实现 hidden 时 clearInterval,恢复只靠这一次 visibilitychange——事件一旦丢失,
|
|
565
|
+
* 采样就永久停摆,面板只能停在 1-2 个样本的「采样中」。
|
|
566
|
+
*/
|
|
567
|
+
function onVisibilityChange() {
|
|
568
|
+
if (typeof document !== 'undefined' && document.hidden) return
|
|
569
|
+
refreshStatus()
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* 幂等释放轮询与可见性监听:插件 dispose / HMR 重载时由 apply 的 ctx.effect 调用。
|
|
574
|
+
* 不释放的话旧 ticker 会随模块退役继续存活,新旧 ticker 并存时每 30 秒发两次请求。
|
|
575
|
+
*/
|
|
576
|
+
function disposeStatusPolling() {
|
|
577
|
+
if (statusPollTimer !== null) {
|
|
578
|
+
clearInterval(statusPollTimer)
|
|
579
|
+
statusPollTimer = null
|
|
580
|
+
}
|
|
581
|
+
statusPollingStarted = false
|
|
582
|
+
if (visibilityListenerBound) {
|
|
583
|
+
visibilityListenerBound = false
|
|
584
|
+
if (typeof document !== 'undefined' && typeof document.removeEventListener === 'function') {
|
|
585
|
+
document.removeEventListener('visibilitychange', onVisibilityChange)
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* 安装 30 秒 GET 轮询(幂等),返回同一个幂等 disposer。旧实现在 FooterEntry 的挂载 effect 里建 ticker:
|
|
592
|
+
* footer 卸载(侧栏收起、布局切换)或 hidden 清理后事件丢失,采样永久停止,
|
|
593
|
+
* 面板只剩 1-2 个内存样本,于是永远「采样中」。轮询属于模块级资源,组件卸载不拆除,
|
|
594
|
+
* 但插件 dispose 时必须释放(见 disposeStatusPolling)。
|
|
595
|
+
*/
|
|
596
|
+
function ensureStatusPolling() {
|
|
597
|
+
if (!statusPollingStarted) {
|
|
598
|
+
statusPollingStarted = true
|
|
599
|
+
statusPollTimer = setInterval(() => refreshStatus(), 30_000)
|
|
600
|
+
// 首次安装立即拉一次,面板不用等第一个 30 秒。
|
|
601
|
+
refreshStatus()
|
|
602
|
+
}
|
|
603
|
+
if (!visibilityListenerBound) {
|
|
604
|
+
// 测试与 SSR 沙箱里 document 可能没有事件接口,先探测再注册。
|
|
605
|
+
const canListen = typeof document !== 'undefined' && typeof document.addEventListener === 'function'
|
|
606
|
+
if (canListen) {
|
|
607
|
+
document.addEventListener('visibilitychange', onVisibilityChange)
|
|
608
|
+
visibilityListenerBound = true
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
// 多次 ensure 返回同一个函数引用:FooterEntry 的幂等调用不会造出第二个 disposer。
|
|
612
|
+
return disposeStatusPolling
|
|
613
|
+
}
|
|
614
|
+
|
|
383
615
|
/** 手动刷新唯一入口:绕过 30 秒缓存,强制重新拉取当前剩余额度。 */
|
|
384
616
|
async function manualRefresh() {
|
|
385
617
|
if (store.state.refreshing) return
|
|
@@ -429,23 +661,9 @@ window.__ModuleLoader__.load({
|
|
|
429
661
|
const hoverTimerRef = useRef(null)
|
|
430
662
|
|
|
431
663
|
useEffect(() => {
|
|
432
|
-
|
|
433
|
-
//
|
|
434
|
-
|
|
435
|
-
const onVisibilityChange = () => {
|
|
436
|
-
clearInterval(timer)
|
|
437
|
-
timer = null
|
|
438
|
-
if (typeof document !== 'undefined' && document.hidden) return
|
|
439
|
-
refreshStatus()
|
|
440
|
-
timer = setInterval(() => refreshStatus(), 30_000)
|
|
441
|
-
}
|
|
442
|
-
// 测试与 SSR 沙箱里 document 可能没有事件接口,先探测再注册。
|
|
443
|
-
const canListen = typeof document !== 'undefined' && typeof document.addEventListener === 'function'
|
|
444
|
-
if (canListen) document.addEventListener('visibilitychange', onVisibilityChange)
|
|
445
|
-
return () => {
|
|
446
|
-
if (timer !== null) clearInterval(timer)
|
|
447
|
-
if (canListen) document.removeEventListener('visibilitychange', onVisibilityChange)
|
|
448
|
-
}
|
|
664
|
+
// 只幂等确保 ticker 存在,不接管清理:ticker 的生命周期归 apply 的 ctx.effect 管理。
|
|
665
|
+
// footer 卸载(侧栏收起、布局切换)不得停采,这里也不能创建第二个 disposer 去重复清理。
|
|
666
|
+
ensureStatusPolling()
|
|
449
667
|
}, [])
|
|
450
668
|
|
|
451
669
|
// 卸载时清掉待触发的悬停定时器,避免 220ms 后对已卸载组件 setState。
|
|
@@ -707,7 +925,8 @@ window.__ModuleLoader__.load({
|
|
|
707
925
|
return h('div', null, [
|
|
708
926
|
h('div', { key: 'sum', style: { display: 'flex', alignItems: 'center', gap: 6 } }, [
|
|
709
927
|
h('span', { key: 'd', style: dotStyle(st.ok ? T.ok : T.err) }),
|
|
710
|
-
|
|
928
|
+
// 窄视口下让标题让位给右侧摘要:摘要 flexShrink:0,标题可省略。
|
|
929
|
+
h('span', { key: 't', style: { fontSize: 12, fontWeight: 600, color: T.label, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flexShrink: 1 } }, st.ok ? '已连接' : '异常'),
|
|
711
930
|
tightest
|
|
712
931
|
? h(
|
|
713
932
|
'span',
|
|
@@ -717,7 +936,9 @@ window.__ModuleLoader__.load({
|
|
|
717
936
|
title: `最紧张窗口:${tightest.label ?? tightest.id ?? '—'},剩余 ${pctText(tightest.remainingPct)}`,
|
|
718
937
|
style: { marginLeft: 'auto', fontSize: 11, fontWeight: 600, color: remainingColor(tightest.remainingPct), fontVariantNumeric: 'tabular-nums', flexShrink: 0 },
|
|
719
938
|
},
|
|
720
|
-
|
|
939
|
+
// 用 label 而不是 shortLabel:shortLabel 是给 220px 悬浮微卡的窄列设计的紧凑缩写,
|
|
940
|
+
// 复用到这里会把「Weekly / Monthly」这类英文缩写漏进主面板。
|
|
941
|
+
`最紧张 ${tightest.label ?? tightest.id ?? '—'}`,
|
|
721
942
|
)
|
|
722
943
|
: null,
|
|
723
944
|
].filter(Boolean)),
|
|
@@ -830,7 +1051,8 @@ window.__ModuleLoader__.load({
|
|
|
830
1051
|
headers: { 'content-type': 'application/json' },
|
|
831
1052
|
body: JSON.stringify({ baseUrl, ...(apiKey ? { apiKey } : {}) }),
|
|
832
1053
|
})
|
|
833
|
-
//
|
|
1054
|
+
// 保存成功后回到用量视图;账号/baseUrl 变了就立刻作废旧账号的采样历史,不等下一次轮询。
|
|
1055
|
+
noteEtaIdentity(result.config?.baseUrl ?? result.status?.config?.baseUrl, result.config?.apiKey?.hint)
|
|
834
1056
|
store.set({ status: result.status, config: result.config, view: 'status', error: null })
|
|
835
1057
|
setApiKey('')
|
|
836
1058
|
} catch (e) {
|
|
@@ -850,6 +1072,7 @@ window.__ModuleLoader__.load({
|
|
|
850
1072
|
headers: { 'content-type': 'application/json' },
|
|
851
1073
|
body: JSON.stringify({ baseUrl, clearApiKey: true }),
|
|
852
1074
|
})
|
|
1075
|
+
noteEtaIdentity(result.config?.baseUrl ?? result.status?.config?.baseUrl, result.config?.apiKey?.hint)
|
|
853
1076
|
store.set({ status: result.status, config: result.config, error: null })
|
|
854
1077
|
} catch (e) {
|
|
855
1078
|
setError(e?.message ?? '清除失败')
|
|
@@ -1122,6 +1345,9 @@ window.__ModuleLoader__.load({
|
|
|
1122
1345
|
|
|
1123
1346
|
// ---------- 注册 ----------
|
|
1124
1347
|
function apply(ctx) {
|
|
1348
|
+
// 轮询与 visibilitychange 监听随插件生命周期释放:不依赖 footer 是否挂载(侧栏收起/布局切换仍持续采样),
|
|
1349
|
+
// 但 dispose/HMR 时必须清掉,否则旧 ticker 会和新 ticker 并存、每 30 秒发两次请求。
|
|
1350
|
+
ctx.effect(() => ensureStatusPolling())
|
|
1125
1351
|
ctx.effect(() => {
|
|
1126
1352
|
const el = document.createElement('style')
|
|
1127
1353
|
el.id = 'dsh-opencode-status-styles'
|
|
@@ -1144,7 +1370,7 @@ window.__ModuleLoader__.load({
|
|
|
1144
1370
|
exports.apply = apply
|
|
1145
1371
|
exports.inject = ['slots']
|
|
1146
1372
|
// 仅供测试与诊断:估算/采样是纯函数 + 模块内状态,导出以便做真实单元测试(宿主侧不消费)。
|
|
1147
|
-
exports.__internal = { estimateEta, recordSamples, getSamples, resetEtaSamples, fmtEtaDuration, coarsenEta, etaView }
|
|
1373
|
+
exports.__internal = { estimateEta, recordSamples, getSamples, resetEtaSamples, fmtEtaDuration, coarsenEta, etaView, ensureStatusPolling, noteEtaIdentity }
|
|
1148
1374
|
return module.exports
|
|
1149
1375
|
},
|
|
1150
1376
|
})
|
package/src/index.js
CHANGED
|
@@ -287,7 +287,7 @@ export function apply(ctx, config = {}) {
|
|
|
287
287
|
{
|
|
288
288
|
id: 'weekly',
|
|
289
289
|
label: '周窗口',
|
|
290
|
-
shortLabel: '
|
|
290
|
+
shortLabel: '周',
|
|
291
291
|
...pctPair(u.weekly?.percent),
|
|
292
292
|
status: u.weekly?.status ?? 'ok',
|
|
293
293
|
resetAt: u.weekly?.resetsAt ? Date.parse(u.weekly.resetsAt) : null,
|
|
@@ -295,7 +295,7 @@ export function apply(ctx, config = {}) {
|
|
|
295
295
|
{
|
|
296
296
|
id: 'monthly',
|
|
297
297
|
label: '月窗口',
|
|
298
|
-
shortLabel: '
|
|
298
|
+
shortLabel: '月',
|
|
299
299
|
...pctPair(u.monthly?.percent),
|
|
300
300
|
status: u.monthly?.status ?? 'ok',
|
|
301
301
|
resetAt: u.monthly?.resetsAt ? Date.parse(u.monthly.resetsAt) : null,
|