dsh-opencode-status 0.4.5 → 0.4.6
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 +7 -0
- package/README.md +4 -14
- package/package.json +1 -1
- package/src/client.js +8 -347
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.6
|
|
4
|
+
|
|
5
|
+
- **移除「可用时长估算」**:按当前速率推算「还能撑多久」需要先累计 10 分钟采样、再与重置时间比较,实际信息量低于直接看进度条;功能整体删除,包括采样记录、`localStorage` 持久化、「采样中」状态与时长粗化。面板只保留剩余百分比与 `⏱` 重置倒计时。
|
|
6
|
+
- **移除头部「最紧张 <窗口>」摘要**:三条进度条并列时哪个窗口最紧一眼可见,摘要属于重复表达;头部只保留连接状态(`已连接` / `异常`)。
|
|
7
|
+
- 旧版本写入的 `dsh-opencode-status.eta-samples` 键会在插件激活时清理一次,不影响任何其他数据。
|
|
8
|
+
- 测试 56 → 42:删除估算与采样相关用例;轮询生命周期、可访问性、安全与竞态用例全部保留。
|
|
9
|
+
|
|
3
10
|
## 0.4.5
|
|
4
11
|
|
|
5
12
|
- **修复用量面板长期停在「采样中」、估算始终不出现的问题**:
|
package/README.md
CHANGED
|
@@ -10,20 +10,10 @@ OpenCode GO 套餐用量与配额状态监控面板插件(DeepSeek Harness)
|
|
|
10
10
|
- `⚙ 设置`:进入面板内的独立连接设置视图,可随时 `← 返回用量`。
|
|
11
11
|
- 右侧显示相对时间「上次同步 12s 前」(刚刚 / Ns 前 / N 分钟前 / N 小时前 / N 天前),绝对时刻 HH:MM:SS 保留在 `title`;同步数据超过 2 分钟追加「· 可能已过期」并用警告色,避免把旧额度当成实时值。
|
|
12
12
|
- **3 维度用量行**:主百分比只给剩余额度,进度条条长与它同源(条长 = 剩余占比,条色 = 剩余档位),「已用」放在 `title` 与 `aria-valuetext` 里,同一行不再重复表达同一个数字;窗口状态非 `ok` 时给出可见提示。
|
|
13
|
-
- **滚动窗口 (5h)**:`剩余 70% ·
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
- **可用时长估算(按当前速率,仅供参考)**:窗口行会顺带告诉你「按现在的用法还能撑多久」。三种状态严格区分,不存在第四种:
|
|
18
|
-
- `约 13h 后耗尽`(警告色):按当前速率会在窗口重置前用完;
|
|
19
|
-
- `可支撑到重置`(中性色):按当前速率能撑到重置,此时不再显示具体时长;
|
|
20
|
-
- **采样中**:已有采样点但还攒不够(少于 3 个点,或首尾跨度不足 10 分钟)时显示「采样中」,`title` 给出已有几个点、跨了多少分钟。它只是状态说明,不含任何数字,用来区分「功能没加载」和「还在攒数据」。
|
|
21
|
-
- **不显示**:速率为 0、剩余为 0、重置时间缺失或已过期时,这个位置直接不出现,也不用「--」「0m」之类占位凑数。
|
|
22
|
-
- 鼠标悬停会说明依据:「按最近 N 分钟的消耗速率估算(按当前速率),仅供参考」。它是对已有采样点的机械外推(只用首尾两点,不做回归),**不是预测或承诺**。
|
|
23
|
-
- 时长按两位有效数字粗化:<1h 取 5 分钟、1–10h 取 30 分钟、≥10h 取 1 小时、≥48h 取 1 天。上游只有整数百分比,分钟级精度属于虚假精确,粗化后不再暗示比实际更准。
|
|
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` 兜底。
|
|
26
|
-
- 纯前端计算:不新增网络请求、不新增依赖,数据仍只来自既有的 30 秒轮询与手动刷新。220ms 悬浮微卡不显示估算(一行放不下五项,缩字号或截断都不可接受,因此有意省略)。
|
|
13
|
+
- **滚动窗口 (5h)**:`剩余 70% · ⏱ 4h20m`
|
|
14
|
+
- **周窗口**:剩余 % + ⏱ 重置倒计时
|
|
15
|
+
- **月窗口**:剩余 % + ⏱ 重置倒计时
|
|
16
|
+
- 面板头部只显示连接状态(`已连接` / `异常`),不再给「最紧张」摘要——哪个窗口最紧,看三条进度条一眼就知道。
|
|
27
17
|
- **可访问性与键盘交互**:
|
|
28
18
|
- 面板是 `role="dialog"`,`aria-label` 随视图切换(「OpenCode GO 用量」/「OpenCode GO 连接配置」)。
|
|
29
19
|
- `Esc` 关闭面板;`Tab` 在面板内首尾循环;打开时焦点移入面板,关闭(含 Esc)后归还到打开前的触发按钮,点击面板外关闭时不抢焦点。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-opencode-status",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
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
|
@@ -159,313 +159,6 @@ window.__ModuleLoader__.load({
|
|
|
159
159
|
return `${Math.round(clampPct(value))}%`
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
/** 最紧张的用量窗口:头部只给判断性摘要,避免与窗口行重复表达同一个数字。 */
|
|
163
|
-
function tightestWindow(status) {
|
|
164
|
-
const windows = Array.isArray(status?.windows) ? status.windows : []
|
|
165
|
-
let tightest = null
|
|
166
|
-
for (const w of windows) {
|
|
167
|
-
if (typeof w?.remainingPct !== 'number') continue
|
|
168
|
-
if (!tightest || w.remainingPct < tightest.remainingPct) tightest = w
|
|
169
|
-
}
|
|
170
|
-
return tightest
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// ---------- 消耗速率估算(按当前速率还能撑多久) ----------
|
|
174
|
-
// 采样放模块级而不是 store:每次采样不触发整树重渲染,渲染时按需读取。
|
|
175
|
-
const ETA_SAMPLE_WINDOW_MS = 60 * 60_000
|
|
176
|
-
const ETA_MAX_SAMPLES = 120
|
|
177
|
-
const ETA_MIN_SAMPLES = 3
|
|
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
|
|
182
|
-
|
|
183
|
-
/** windowId -> { resetAt, samples: [{ at, used }] };at 取 status.fetchedAt(不是 Date.now)。 */
|
|
184
|
-
const etaSamples = new Map()
|
|
185
|
-
|
|
186
|
-
/**
|
|
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 会随早先用量过期不断前移,把它当重置信号正是「永远采样中」的根因。
|
|
330
|
-
*/
|
|
331
|
-
function recordSamples(status) {
|
|
332
|
-
// 先用响应里的 baseUrl 确认账号归属:换账号后旧样本必须整体作废,不能混用两个账号的 usedPct。
|
|
333
|
-
noteEtaIdentity(status?.config?.baseUrl, null)
|
|
334
|
-
if (!status?.ok || typeof status.fetchedAt !== 'number') return
|
|
335
|
-
const windows = Array.isArray(status.windows) ? status.windows : []
|
|
336
|
-
let changed = false
|
|
337
|
-
for (const w of windows) {
|
|
338
|
-
const id = w?.id
|
|
339
|
-
if (!id || typeof w?.usedPct !== 'number') continue
|
|
340
|
-
const at = status.fetchedAt
|
|
341
|
-
const used = w.usedPct
|
|
342
|
-
const resetAt = typeof w.resetAt === 'number' && Number.isFinite(w.resetAt) ? w.resetAt : null
|
|
343
|
-
let entry = etaSamples.get(id)
|
|
344
|
-
if (!entry) {
|
|
345
|
-
entry = { resetAt, samples: [] }
|
|
346
|
-
etaSamples.set(id, entry)
|
|
347
|
-
}
|
|
348
|
-
const last = entry.samples[entry.samples.length - 1]
|
|
349
|
-
if (last && last.at === at) continue // 同一份缓存数据不重复采样
|
|
350
|
-
// resetAt 变化只更新基准(下面统一赋值),不清历史;只有用量真实回落才是窗口重置。
|
|
351
|
-
if (last && used < last.used) entry.samples = []
|
|
352
|
-
entry.resetAt = resetAt
|
|
353
|
-
entry.samples.push({ at, used })
|
|
354
|
-
changed = true
|
|
355
|
-
// 只保留最近 60 分钟、每窗口最多 120 个样本。
|
|
356
|
-
const cutoff = at - ETA_SAMPLE_WINDOW_MS
|
|
357
|
-
while (entry.samples.length && entry.samples[0].at < cutoff) entry.samples.shift()
|
|
358
|
-
if (entry.samples.length > ETA_MAX_SAMPLES) entry.samples.splice(0, entry.samples.length - ETA_MAX_SAMPLES)
|
|
359
|
-
}
|
|
360
|
-
if (changed) persistEtaSamples()
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// 模块加载时先读持久化原文;身份要等首个 status/config 响应,确认同账号后才恢复历史。
|
|
364
|
-
readEtaSamplesPayload()
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* 估算「按当前速率还能撑多久」。样本 ≥3、首尾跨度 ≥10 分钟、速率 >0 三者缺一即返回 null;
|
|
368
|
-
* 速率只用首尾两点(不做回归,抗噪且好解释)。resetAt 缺失或已过期时无法判断能否撑到重置,同样返回 null。
|
|
369
|
-
* 返回 { etaMs, untilResetMs, exhaustsBeforeReset, spanMs }:能否撑到重置由调用方决定怎么展示。
|
|
370
|
-
*/
|
|
371
|
-
function estimateEta(samples, remainingPct, resetAt, now = Date.now()) {
|
|
372
|
-
if (!Array.isArray(samples) || samples.length < ETA_MIN_SAMPLES) return null
|
|
373
|
-
if (typeof remainingPct !== 'number' || !Number.isFinite(remainingPct) || remainingPct <= 0) return null
|
|
374
|
-
if (typeof resetAt !== 'number' || !Number.isFinite(resetAt)) return null
|
|
375
|
-
const first = samples[0]
|
|
376
|
-
const last = samples[samples.length - 1]
|
|
377
|
-
if (typeof first?.at !== 'number' || typeof last?.at !== 'number') return null
|
|
378
|
-
if (typeof first?.used !== 'number' || typeof last?.used !== 'number') return null
|
|
379
|
-
const spanMs = last.at - first.at
|
|
380
|
-
if (!(spanMs >= ETA_MIN_SPAN_MS)) return null
|
|
381
|
-
const ratePerMs = (last.used - first.used) / spanMs
|
|
382
|
-
if (!(ratePerMs > 0)) return null
|
|
383
|
-
const etaMs = remainingPct / ratePerMs
|
|
384
|
-
if (!Number.isFinite(etaMs) || etaMs <= 0) return null
|
|
385
|
-
const untilResetMs = resetAt - now
|
|
386
|
-
if (!Number.isFinite(untilResetMs) || untilResetMs <= 0) return null
|
|
387
|
-
return { etaMs, untilResetMs, exhaustsBeforeReset: etaMs < untilResetMs, spanMs }
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/** 时长短文案:<1h 用分钟,<24h 用 h+m,更长用 d+h(「还能撑多久」不需要精确到分秒)。 */
|
|
391
|
-
function fmtEtaDuration(ms) {
|
|
392
|
-
const mins = Math.max(1, Math.round(ms / 60_000))
|
|
393
|
-
if (mins < 60) return `${mins}m`
|
|
394
|
-
const hours = Math.floor(mins / 60)
|
|
395
|
-
if (hours < 24) return `${hours}h${mins % 60 ? `${mins % 60}m` : ''}`
|
|
396
|
-
const days = Math.floor(hours / 24)
|
|
397
|
-
return `${days}d${hours % 24 ? `${hours % 24}h` : ''}`
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* 估算值粗化:上游只给整数 percent,量化误差会让 ETA 的真实区间比点值宽 2-3 倍,
|
|
402
|
-
* 分钟级精度属于虚假精确。按「两位有效数字」粗化,既保留量级又不再暗示精度:
|
|
403
|
-
* <1h 取 5 分钟、1-10h 取 30 分钟、≥10h 取 1 小时、≥48h 取 1 天。
|
|
404
|
-
*/
|
|
405
|
-
function coarsenEta(ms) {
|
|
406
|
-
const minutes = ms / 60_000
|
|
407
|
-
if (minutes < 60) return Math.max(5, Math.round(minutes / 5) * 5) * 60_000
|
|
408
|
-
if (minutes < 600) return Math.round(minutes / 30) * 30 * 60_000
|
|
409
|
-
const hours = minutes / 60
|
|
410
|
-
if (hours < 48) return Math.round(hours) * 3_600_000
|
|
411
|
-
return Math.round(hours / 24) * 86_400_000
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* 估算文案的三态:不可估算返回 null(调用方完全不渲染节点,不显示 "--"/"估算中" 之类占位);
|
|
416
|
-
* 撑不到重置用告警色「约 X 后耗尽」,撑得到用中性色「可支撑到重置」。
|
|
417
|
-
* 文案只用「按当前速率」的描述,不用「预计/将」这种预测口吻。
|
|
418
|
-
* 行内必须保留「约」与「后」:这是常驻可见的唯一表述(悬浮卡等场景 title 不可达),
|
|
419
|
-
* 缺了「约」会被读成确定性断言,与「仅供参考」的定位冲突。
|
|
420
|
-
* compact(悬浮微卡,外宽约 220px)整段不展示:一行放不下「名称/条/剩余/估算/倒计时」五项,
|
|
421
|
-
* 缩字号、砍进度条/倒计时、截断估算都不可接受,所以宁可不显示;估算逻辑仍共用 estimateEta,只是呈现密度不同。
|
|
422
|
-
* 这是有意为之,不是漏渲染。
|
|
423
|
-
*/
|
|
424
|
-
function etaView(w, options = {}) {
|
|
425
|
-
if (options?.compact) return null
|
|
426
|
-
const entry = etaSamples.get(w?.id)
|
|
427
|
-
if (!entry || !entry.samples.length) return null
|
|
428
|
-
const estimate = estimateEta(entry.samples, w?.remainingPct, w?.resetAt)
|
|
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
|
-
}
|
|
443
|
-
const minutes = Math.max(1, Math.round(estimate.spanMs / 60_000))
|
|
444
|
-
// 文案只说「按最近 N 分钟的消耗速率(按当前速率)」的实测依据,不写「预计/将」这类预测口吻。
|
|
445
|
-
const title = `按最近 ${minutes} 分钟的消耗速率估算(按当前速率),仅供参考`
|
|
446
|
-
// 保留完整字面「约 X 后耗尽」:真实 Chrome 实测在 326px/262px 内容宽下都不会截断,只会让标签多收缩。
|
|
447
|
-
return estimate.exhaustsBeforeReset
|
|
448
|
-
? { text: `约 ${fmtEtaDuration(coarsenEta(estimate.etaMs))} 后耗尽`, color: T.warn, title }
|
|
449
|
-
: { text: '可支撑到重置', color: T.secondary, title }
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/** 仅供测试与诊断:读取某个窗口的采样副本。 */
|
|
453
|
-
function getSamples(windowId) {
|
|
454
|
-
const entry = etaSamples.get(windowId)
|
|
455
|
-
return entry ? entry.samples.map((s) => ({ ...s })) : []
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
/** 仅供测试与诊断:清空所有采样(含持久化副本),并阻止本次生命周期内再从存储恢复。 */
|
|
459
|
-
function resetEtaSamples() {
|
|
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 {}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
162
|
/** 月窗口剩余百分比;上游只给百分比,不存在金额余额。 */
|
|
470
163
|
function monthlyRemainingOf(status) {
|
|
471
164
|
if (!status) return null
|
|
@@ -533,7 +226,6 @@ window.__ModuleLoader__.load({
|
|
|
533
226
|
const status = await api('/api/opencode-status')
|
|
534
227
|
// 只接受最新 epoch 的响应:手动刷新之后返回的在途 GET 一律丢弃。
|
|
535
228
|
if (epoch === statusEpoch) {
|
|
536
|
-
recordSamples(status)
|
|
537
229
|
store.set({ status, error: null })
|
|
538
230
|
}
|
|
539
231
|
} catch (err) {
|
|
@@ -548,8 +240,6 @@ window.__ModuleLoader__.load({
|
|
|
548
240
|
async function refreshConfig() {
|
|
549
241
|
try {
|
|
550
242
|
const config = await api('/api/opencode-status/config')
|
|
551
|
-
// config.apiKey.hint 是「••••末四位」掩码(不是密钥),用来判断持久化样本是否同一账号。
|
|
552
|
-
noteEtaIdentity(config?.baseUrl, config?.apiKey?.hint)
|
|
553
243
|
store.set({ config })
|
|
554
244
|
} catch {}
|
|
555
245
|
}
|
|
@@ -562,7 +252,7 @@ window.__ModuleLoader__.load({
|
|
|
562
252
|
/**
|
|
563
253
|
* 页面恢复可见时立即补拉一次;隐藏期间保留 ticker(浏览器会自行节流后台定时器)。
|
|
564
254
|
* 旧实现 hidden 时 clearInterval,恢复只靠这一次 visibilitychange——事件一旦丢失,
|
|
565
|
-
*
|
|
255
|
+
* 轮询就永久停摆,面板会一直显示冻结的旧额度。
|
|
566
256
|
*/
|
|
567
257
|
function onVisibilityChange() {
|
|
568
258
|
if (typeof document !== 'undefined' && document.hidden) return
|
|
@@ -589,9 +279,8 @@ window.__ModuleLoader__.load({
|
|
|
589
279
|
|
|
590
280
|
/**
|
|
591
281
|
* 安装 30 秒 GET 轮询(幂等),返回同一个幂等 disposer。旧实现在 FooterEntry 的挂载 effect 里建 ticker:
|
|
592
|
-
* footer 卸载(侧栏收起、布局切换)或 hidden
|
|
593
|
-
*
|
|
594
|
-
* 但插件 dispose 时必须释放(见 disposeStatusPolling)。
|
|
282
|
+
* footer 卸载(侧栏收起、布局切换)或 hidden 清理后事件丢失,轮询永久停止,面板显示冻结的旧额度。
|
|
283
|
+
* 轮询属于模块级资源,组件卸载不拆除,但插件 dispose 时必须释放(见 disposeStatusPolling)。
|
|
595
284
|
*/
|
|
596
285
|
function ensureStatusPolling() {
|
|
597
286
|
if (!statusPollingStarted) {
|
|
@@ -624,7 +313,6 @@ window.__ModuleLoader__.load({
|
|
|
624
313
|
headers: { 'content-type': 'application/json' },
|
|
625
314
|
})
|
|
626
315
|
// 手动刷新自身的响应始终落库(刷新期间已阻止新的 GET)。
|
|
627
|
-
recordSamples(status)
|
|
628
316
|
store.set({ status, error: null })
|
|
629
317
|
} catch (err) {
|
|
630
318
|
store.set({ error: String(err?.message ?? err) })
|
|
@@ -887,9 +575,6 @@ window.__ModuleLoader__.load({
|
|
|
887
575
|
const w = props.window
|
|
888
576
|
const remaining = w?.remainingPct ?? null
|
|
889
577
|
const color = remainingColor(remaining)
|
|
890
|
-
// compact:悬浮微卡外宽约 220px,五个元素塞不进去。这里整段不渲染估算(etaView 返回 null),
|
|
891
|
-
// 不缩字号、不砍进度条/倒计时、不截断成残句;面板 QuotaRow 仍照常展示。
|
|
892
|
-
const eta = etaView(w, { compact: true })
|
|
893
578
|
return h('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } }, [
|
|
894
579
|
h('span', { key: 'n', style: { width: 46, flexShrink: 0, fontSize: 10, color: T.secondary, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, w?.shortLabel ?? w?.label ?? '—'),
|
|
895
580
|
h(
|
|
@@ -900,10 +585,6 @@ window.__ModuleLoader__.load({
|
|
|
900
585
|
: h('div', { style: { height: '100%', width: `${clampPct(remaining)}%`, background: color, borderRadius: 2 } }),
|
|
901
586
|
),
|
|
902
587
|
h('span', { key: 'v', style: { minWidth: 30, flexShrink: 0, textAlign: 'right', fontSize: 10, fontWeight: 600, color, fontVariantNumeric: 'tabular-nums' } }, remaining === null ? '—' : pctText(remaining)),
|
|
903
|
-
// 紧凑视图不会走到这里(etaView(compact) 恒为 null);保留非截断样式,避免以后误开时变成残句。
|
|
904
|
-
eta
|
|
905
|
-
? h('span', { key: 'e', title: eta.title, style: { flexShrink: 0, fontSize: 10, color: eta.color, whiteSpace: 'nowrap' } }, eta.text)
|
|
906
|
-
: null,
|
|
907
588
|
// 倒计时用 ⏱ 区分刷新动作的 ↻,避免两个图标同义混淆。
|
|
908
589
|
h('span', { key: 'r', style: { minWidth: 34, flexShrink: 0, textAlign: 'right', fontSize: 10, color: T.secondary, fontVariantNumeric: 'tabular-nums' } }, w?.resetAt ? `⏱ ${timeUntil(w.resetAt)}` : ''),
|
|
909
590
|
])
|
|
@@ -919,28 +600,12 @@ window.__ModuleLoader__.load({
|
|
|
919
600
|
}
|
|
920
601
|
|
|
921
602
|
const windows = Array.isArray(st.windows) ? st.windows : []
|
|
922
|
-
// 头部只给判断性摘要(最紧张窗口),具体数字留给下方窗口行,避免同一数字说两遍。
|
|
923
|
-
const tightest = tightestWindow(st)
|
|
924
603
|
|
|
925
604
|
return h('div', null, [
|
|
926
605
|
h('div', { key: 'sum', style: { display: 'flex', alignItems: 'center', gap: 6 } }, [
|
|
927
606
|
h('span', { key: 'd', style: dotStyle(st.ok ? T.ok : T.err) }),
|
|
928
607
|
// 窄视口下让标题让位给右侧摘要:摘要 flexShrink:0,标题可省略。
|
|
929
608
|
h('span', { key: 't', style: { fontSize: 12, fontWeight: 600, color: T.label, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flexShrink: 1 } }, st.ok ? '已连接' : '异常'),
|
|
930
|
-
tightest
|
|
931
|
-
? h(
|
|
932
|
-
'span',
|
|
933
|
-
{
|
|
934
|
-
key: 'm',
|
|
935
|
-
// host 只在面板标题行展示一次;这里不再重复。
|
|
936
|
-
title: `最紧张窗口:${tightest.label ?? tightest.id ?? '—'},剩余 ${pctText(tightest.remainingPct)}`,
|
|
937
|
-
style: { marginLeft: 'auto', fontSize: 11, fontWeight: 600, color: remainingColor(tightest.remainingPct), fontVariantNumeric: 'tabular-nums', flexShrink: 0 },
|
|
938
|
-
},
|
|
939
|
-
// 用 label 而不是 shortLabel:shortLabel 是给 220px 悬浮微卡的窄列设计的紧凑缩写,
|
|
940
|
-
// 复用到这里会把「Weekly / Monthly」这类英文缩写漏进主面板。
|
|
941
|
-
`最紧张 ${tightest.label ?? tightest.id ?? '—'}`,
|
|
942
|
-
)
|
|
943
|
-
: null,
|
|
944
609
|
].filter(Boolean)),
|
|
945
610
|
state.error
|
|
946
611
|
? h('div', { key: 'ce', style: { fontSize: 11, color: T.err, marginTop: 8 } }, `刷新失败:${state.error}`)
|
|
@@ -967,18 +632,12 @@ window.__ModuleLoader__.load({
|
|
|
967
632
|
const remaining = w?.remainingPct ?? null
|
|
968
633
|
const color = remainingColor(remaining)
|
|
969
634
|
const band = remainingBand(remaining)
|
|
970
|
-
const eta = etaView(w)
|
|
971
635
|
const statusText = w?.status && w.status !== 'ok' ? w.status : null
|
|
972
636
|
const detail = remaining === null ? '数据不可用' : `已用 ${pctText(used)} · 剩余 ${pctText(remaining)}`
|
|
973
637
|
return h('div', { style: { marginBottom: 12 } }, [
|
|
974
638
|
h('div', { key: 'meta', style: { display: 'flex', alignItems: 'baseline', gap: 6, fontSize: 11, marginBottom: 4, fontVariantNumeric: 'tabular-nums' } }, [
|
|
975
639
|
h('span', { key: 'l', style: { color: T.label, fontWeight: 500, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, w?.label ?? '—'),
|
|
976
640
|
h('span', { key: 'r', title: detail, style: { marginLeft: 'auto', color, fontWeight: 600, flexShrink: 0 } }, remaining === null ? '剩余 —' : `剩余 ${pctText(remaining)}`),
|
|
977
|
-
// 估算整段展示、不截断:flexShrink:0 且无 ellipsis;空间不足时只让窗口标签 ellipsis,
|
|
978
|
-
// 档位徽标(告警不只靠颜色)、剩余 % 与倒计时都不收缩。
|
|
979
|
-
eta
|
|
980
|
-
? h('span', { key: 'eta', title: eta.title, style: { flexShrink: 0, fontSize: 10, color: eta.color, whiteSpace: 'nowrap' } }, eta.text)
|
|
981
|
-
: null,
|
|
982
641
|
// 低额度除颜色外再给文字标记:色觉障碍与灰度截图里也能看出档位。
|
|
983
642
|
band
|
|
984
643
|
? h(
|
|
@@ -1052,7 +711,6 @@ window.__ModuleLoader__.load({
|
|
|
1052
711
|
body: JSON.stringify({ baseUrl, ...(apiKey ? { apiKey } : {}) }),
|
|
1053
712
|
})
|
|
1054
713
|
// 保存成功后回到用量视图;账号/baseUrl 变了就立刻作废旧账号的采样历史,不等下一次轮询。
|
|
1055
|
-
noteEtaIdentity(result.config?.baseUrl ?? result.status?.config?.baseUrl, result.config?.apiKey?.hint)
|
|
1056
714
|
store.set({ status: result.status, config: result.config, view: 'status', error: null })
|
|
1057
715
|
setApiKey('')
|
|
1058
716
|
} catch (e) {
|
|
@@ -1072,7 +730,6 @@ window.__ModuleLoader__.load({
|
|
|
1072
730
|
headers: { 'content-type': 'application/json' },
|
|
1073
731
|
body: JSON.stringify({ baseUrl, clearApiKey: true }),
|
|
1074
732
|
})
|
|
1075
|
-
noteEtaIdentity(result.config?.baseUrl ?? result.status?.config?.baseUrl, result.config?.apiKey?.hint)
|
|
1076
733
|
store.set({ status: result.status, config: result.config, error: null })
|
|
1077
734
|
} catch (e) {
|
|
1078
735
|
setError(e?.message ?? '清除失败')
|
|
@@ -1345,6 +1002,10 @@ window.__ModuleLoader__.load({
|
|
|
1345
1002
|
|
|
1346
1003
|
// ---------- 注册 ----------
|
|
1347
1004
|
function apply(ctx) {
|
|
1005
|
+
// 0.4.6 起不再使用采样持久化,清掉旧版本留下的键(仅清理,不含任何用户数据)。
|
|
1006
|
+
try {
|
|
1007
|
+
if (typeof localStorage !== 'undefined' && localStorage) localStorage.removeItem('dsh-opencode-status.eta-samples')
|
|
1008
|
+
} catch {}
|
|
1348
1009
|
// 轮询与 visibilitychange 监听随插件生命周期释放:不依赖 footer 是否挂载(侧栏收起/布局切换仍持续采样),
|
|
1349
1010
|
// 但 dispose/HMR 时必须清掉,否则旧 ticker 会和新 ticker 并存、每 30 秒发两次请求。
|
|
1350
1011
|
ctx.effect(() => ensureStatusPolling())
|
|
@@ -1370,7 +1031,7 @@ window.__ModuleLoader__.load({
|
|
|
1370
1031
|
exports.apply = apply
|
|
1371
1032
|
exports.inject = ['slots']
|
|
1372
1033
|
// 仅供测试与诊断:估算/采样是纯函数 + 模块内状态,导出以便做真实单元测试(宿主侧不消费)。
|
|
1373
|
-
exports.__internal = {
|
|
1034
|
+
exports.__internal = { ensureStatusPolling }
|
|
1374
1035
|
return module.exports
|
|
1375
1036
|
},
|
|
1376
1037
|
})
|