dsh-opencode-status 0.2.3 → 0.4.0
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 +34 -0
- package/README.md +26 -11
- package/package.json +2 -2
- package/src/client.js +597 -183
- package/src/index.js +22 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
- 可访问性与键盘:用量面板从无语义裸容器改为 `role="dialog"`,`aria-label` 随视图变化(「OpenCode GO 用量」/「OpenCode GO 连接配置」);`Esc` 关闭面板,`Tab` 在面板内首尾循环;打开时焦点移入面板(容器 `tabIndex: -1`),关闭(含 Esc)后焦点归还打开前的触发按钮,点击面板外关闭时不抢焦点(焦点留在用户新点的控件上)。面板允许点击外部关闭、属非模态,因此**不声明** `aria-modal`(WAI-ARIA 要求 `aria-modal=true` 时外部内容不可交互,而本面板没有 inert 实现,声明反而误导读屏)。
|
|
6
|
+
- 进度条语义化:每个窗口轨道改为 `role="progressbar"`,带 `aria-valuemin=0` / `aria-valuemax=100` / `aria-valuenow`(取整后的剩余额度)与 `aria-valuetext`(「剩余 X%,已用 Y%,档位」);数据不可用时只给 `aria-valuetext="数据不可用"`,不输出 `aria-valuenow`。
|
|
7
|
+
- 告警不再只靠颜色:剩余 ≤20% 追加 `⚠ 即将耗尽`、≤50% 追加 `⚠ 偏低` 的文字徽标,并并入 `aria-valuetext`;颜色阈值与颜色逻辑一字未改,色觉障碍用户与灰度截图同样能读出档位。
|
|
8
|
+
- 信息去重与层级:
|
|
9
|
+
- 窗口行主百分比只显示剩余额度,进度条条长与主百分比同源(条长 = 剩余占比,条色 = 剩余档位),「已用」降级到 `title` 与 `aria-valuetext`,同一行不再并列「已用 X% / 剩余 Y%」表达同一个数字。
|
|
10
|
+
- 头部去掉与标题行重复的 host;「月剩余 N%」改为判断性摘要「最紧张 <窗口> N%」(取剩余最小的窗口),具体数字只保留在窗口行。
|
|
11
|
+
- 免责说明(上游 `/v1/usage` 只提供百分比、界面不展示金额余额)移出用量视图正文,落到设置视图底部,用量视图只保留结论。
|
|
12
|
+
- 相对时间:底部操作条与悬浮卡的上次同步改为相对时间(刚刚 / 12s 前 / 3 分钟前 / …),绝对时刻(HH:MM:SS)保留在 `title`;同步数据超过 2 分钟追加「· 可能已过期」并用警告色,避免把旧额度当实时值。
|
|
13
|
+
- 倒计时图标:窗口行与悬浮卡的倒计时由 `↻` 改为 `⏱`,与刷新动作的 `↻` 区分,两个语义不再共用一个图标。
|
|
14
|
+
- 宽度自适应:面板宽度改为 `width: min(360px, calc(100vw - 24px))` + `maxWidth: calc(100vw - 24px)` + `boxSizing: border-box`;窄视口(<372px)不再右侧溢出,宽视口仍固定 360px。
|
|
15
|
+
- 数值规整:宿主对三个窗口与月度百分比的 used/remaining 统一取整并双侧钳制到 0–100,且由取整后的 used 反推 remaining,保证两者恒和 100(12.5 → 13/87,旧实现为 13/88);上游拉取失败时不再前移「上次同步」,失败不会伪装成新鲜数据。
|
|
16
|
+
- 轮询策略:仍为 30 秒自动刷新;页面不可见(`document.hidden`)时暂停轮询,元素重新可见时立即补拉一次,避免后台空转和回到前台看到陈旧值;手动刷新仍是唯一绕过服务端 30 秒缓存的入口。
|
|
17
|
+
- 动效收敛:状态灯呼吸动画由无限循环改为进场一次(`ocPulse 1.2s ease-out 1 both`)并绑定 `fetchedAt`,只在数据真正更新时触发;`prefers-reduced-motion: reduce` 下禁用动画。
|
|
18
|
+
- 测试:用例从 36 增至 42,新增 dialog/`aria-label`/无 `aria-modal`、progressbar 数值语义、非颜色档位标记、免责说明位置、最紧张摘要、相对时间与过期提示、百分比规整(含 12.5 边界)等断言;原有用例(含安全、配置、竞态回归)全部保留,未削弱。
|
|
19
|
+
- 版本号升至 `0.4.0`;README 的功能特性与交互说明同步更新。`0.3.0` 已于 2026-11-08 单独发布,其改动不在本版本内重复。
|
|
20
|
+
|
|
21
|
+
## 0.3.0
|
|
22
|
+
|
|
23
|
+
- 重构点击交互:侧栏常驻按钮改为打开用量面板(默认用量视图),不再直接弹出连接配置表单;参考 dsh-cpa-status 的双视图范式。
|
|
24
|
+
- 用量面板新增固定底部操作条:`↻ 刷新` 通过 `POST /api/opencode-status/sync` 绕过 30 秒缓存强制重新拉取当前剩余额度(带 `content-type: application/json`,刷新期间按钮禁用并显示旋转 +「刷新中…」),完成后侧栏与面板数值同步更新;右侧固定展示「上次同步 HH:MM:SS」(取 `status.fetchedAt`)。刷新失败时在面板内显示错误文案,不再静默。
|
|
25
|
+
- 设置改为面板内的独立视图(Base URL + API Key),底部 `⚙ 设置` 进入、`← 返回用量` 退出;保存成功后自动回到用量视图。`mode === 'needs_config'` 时面板直接显示设置视图;「取消」保持 0.2.3 语义:丢弃未保存的编辑并关闭面板。
|
|
26
|
+
- 用量面板的三个窗口行同时给出已用 % 与剩余 %、重置倒计时和进度条;颜色按剩余额度阈值(>50 正常、>20 警告、其余错误)着色,与 cpa 的 QuotaBar 语义一致;窗口 `status` 非 `ok` 时给出可见提示。
|
|
27
|
+
- 重新设计 220ms 悬浮微卡:标题 + GO 标识 + 最近同步时间,三个窗口的紧凑行(名称 / 剩余 % / ↻ 重置倒计时 / 细进度条),底部「点击查看详情」;卡片为纯展示(无假按钮),并设置 `pointer-events: none` 不遮挡 footer 按钮的点击。
|
|
28
|
+
- 侧栏常驻按钮按剩余额度着色(>50 正常、>20 警告、否则错误),文案改为「月剩余 N%」摘要;rail 模式(`wide === false`)渲染为真正的 `button`(键盘可达),点击同样打开用量视图。
|
|
29
|
+
- 保留 30 秒 GET 自动刷新;打开面板 / 切换视图只走 30 秒缓存,不触发额外的上游探测,手动刷新是唯一绕过缓存的入口。
|
|
30
|
+
- 测试目录新增 `test/index.js` 目录入口垫片:Node 22.23 的 `node --test <dir>` 不再展开目录参数,垫片让 `node --test dsh-plugins/dsh-opencode-status/test/` 与 `node --test` 两种调用方式都能执行同一套用例。
|
|
31
|
+
- 明确额度语义:上游 `/v1/usage` 只返回 rolling/weekly/monthly 百分比,没有 USD 余额接口(anomalyco/opencode#10448 仍 open),面板只表述「剩余额度 / 可用百分比」,不编造金额。
|
|
32
|
+
- store 用 `view: 'status' | 'settings'` 表达面板视图,不引入已废弃的 `editing` 标识。
|
|
33
|
+
- 修复评审 F2/F3:手动刷新发起时推进请求序号 `statusEpoch`,更早发出的在途 GET 响应(含失败)不再覆盖 `store.status` / `store.error`,手动刷新自身结果仍正常落库,30 秒轮询行为不变;设置视图的「← 返回用量」在 status 尚未加载(null)时也可见,仅 `mode === 'needs_config'` 时隐藏。
|
|
34
|
+
- 测试更新:新增刷新按钮 `POST /sync` 且带 JSON 内容类型、设置入口与视图切换、悬浮卡上次同步、侧栏着色与 rail button、剩余额度阈值着色、窗口异常提示、版本号与文档一致性断言;原有安全与配置用例保持通过(共 35 个用例,含最小 React stub 下的客户端加载、rail button、设置视图返回入口冒烟,以及 F2 的静态断言与「旧 GET 在 POST 之后返回/失败」运行时竞态回归)。
|
|
35
|
+
- 版本号升至 `0.3.0`;README 的功能特性与配置说明同步更新。
|
|
36
|
+
|
|
3
37
|
## 0.2.3
|
|
4
38
|
|
|
5
39
|
- 修复配置面板「取消」按钮点击无反应:该按钮此前只翻转 `editing` 标记,而 0.1.5 的弹窗重构之后已没有任何渲染逻辑读取它。现在「取消」会丢弃未保存的编辑并关闭面板,重新打开时表单从已保存配置重新渲染。
|
package/README.md
CHANGED
|
@@ -1,23 +1,36 @@
|
|
|
1
1
|
# dsh-opencode-status
|
|
2
2
|
|
|
3
|
-
OpenCode GO 套餐用量与配额状态监控面板插件(DeepSeek Harness
|
|
3
|
+
OpenCode GO 套餐用量与配额状态监控面板插件(DeepSeek Harness):侧栏常驻极简单行胶囊按钮,点击即打开用量面板,查看 5h 滚动窗口、周窗口、月窗口的剩余额度与重置倒计时,并可手动刷新或进入独立设置视图。
|
|
4
4
|
|
|
5
5
|
## 功能特性
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
7
|
+
- **点击打开用量面板**:侧栏常驻按钮(`● OpenCode · 月剩余 70%`)点击后打开的是用量面板(默认用量视图),不再是只弹出连接配置表单;按剩余额度着色(>50 正常、>20 警告、其余错误),rail 模式下同样是可点击的真按钮。
|
|
8
|
+
- **固定底部操作条**:
|
|
9
|
+
- `↻ 刷新`:通过 `POST /api/opencode-status/sync` 绕过 30 秒缓存,强制重新拉取当前剩余额度;刷新期间按钮禁用并显示旋转与「刷新中…」,完成后面板与侧栏数值同步更新,失败时在面板内显示错误文案而不是静默。
|
|
10
|
+
- `⚙ 设置`:进入面板内的独立连接设置视图,可随时 `← 返回用量`。
|
|
11
|
+
- 右侧显示相对时间「上次同步 12s 前」(刚刚 / Ns 前 / N 分钟前 / N 小时前 / N 天前),绝对时刻 HH:MM:SS 保留在 `title`;同步数据超过 2 分钟追加「· 可能已过期」并用警告色,避免把旧额度当成实时值。
|
|
12
|
+
- **3 维度用量行**:主百分比只给剩余额度,进度条条长与它同源(条长 = 剩余占比,条色 = 剩余档位),「已用」放在 `title` 与 `aria-valuetext` 里,同一行不再重复表达同一个数字;窗口状态非 `ok` 时给出可见提示。
|
|
13
|
+
- **滚动窗口 (5h)**:`剩余 70% · ⏱ 4h20m`
|
|
14
|
+
- **周窗口 (Weekly)**:周额度剩余 % + ⏱ 重置倒计时
|
|
15
|
+
- **月窗口 (Monthly)**:月额度剩余 % + ⏱ 重置倒计时
|
|
16
|
+
- 面板头部只给判断性摘要「最紧张 <窗口> N%」(取剩余最小的窗口),具体数字留在窗口行。
|
|
17
|
+
- **可访问性与键盘交互**:
|
|
18
|
+
- 面板是 `role="dialog"`,`aria-label` 随视图切换(「OpenCode GO 用量」/「OpenCode GO 连接配置」)。
|
|
19
|
+
- `Esc` 关闭面板;`Tab` 在面板内首尾循环;打开时焦点移入面板,关闭(含 Esc)后归还到打开前的触发按钮,点击面板外关闭时不抢焦点。
|
|
20
|
+
- 面板允许点击外部关闭,属非模态对话框,因此**不声明** `aria-modal`。
|
|
21
|
+
- 每个窗口的进度条是 `role="progressbar"`,带 `aria-valuemin` / `aria-valuemax` / `aria-valuenow`(取整剩余额度)与 `aria-valuetext`(「剩余 X%,已用 Y%,档位」);数据不可用时只报「数据不可用」,不给数值。
|
|
22
|
+
- 低额度除颜色外还有文字徽标(`⚠ 即将耗尽` ≤20%、`⚠ 偏低` ≤50%),色觉障碍与灰度截图也能读出档位。
|
|
23
|
+
- 面板宽度为 `min(360px, calc(100vw - 24px))`,窄窗口(<372px)不再右侧溢出。
|
|
24
|
+
- **连接设置视图**:未配置(`mode === 'needs_config'`)时面板直接显示设置视图;保存成功后自动回到用量视图;「取消」丢弃未保存的编辑并关闭面板。密钥仅存入宿主凭据库(`OPENCODE_GO_API_KEY`),API 响应不回传明文。数据口径说明(上游只提供百分比、界面不展示金额余额)放在此视图底部,不占用用量视图。
|
|
25
|
+
- **220ms 悬浮微卡**:鼠标悬停常驻按钮 220ms 后在面板关闭且连接正常时出现,展示标题 + GO 标识 + 最近同步时间(相对时间)、三个窗口的紧凑行(名称 / 剩余 % / ⏱ 重置倒计时 / 细进度条)与底部「点击查看详情」;纯展示卡片,不拦截按钮点击。
|
|
26
|
+
- **缓存与刷新**:服务端 30 秒缓存 + 客户端 30 秒自动刷新;页面不可见时暂停轮询,元素重新可见时立即补拉一次;打开面板、切换视图不会额外探测上游,手动刷新是唯一绕过缓存的入口。
|
|
27
|
+
- **额度语义**:上游 `/v1/usage` 只提供 rolling/weekly/monthly 百分比,没有 USD 余额接口([anomalyco/opencode#10448](https://github.com/anomalyco/opencode/issues/10448) 仍 open),因此界面只展示「剩余额度 / 可用百分比」,不编造金额余额;百分比由宿主统一取整并钳制到 0–100,剩余与已用恒和 100。
|
|
15
28
|
|
|
16
29
|
## 安装与更新
|
|
17
30
|
|
|
18
31
|
### 兼容性(DSH 0.2.x)
|
|
19
32
|
|
|
20
|
-
- 本版本(`0.
|
|
33
|
+
- 本版本(`0.4.x`)面向 **DeepSeek Harness 0.2.x**,已在 `0.2.2-rc.1` 上验证;`package.json` 通过 `engines.dsh: ">=0.2.0 <0.3.0-0"` 声明兼容范围(上界带 `-0` 显式排除 0.3.0 的预发布版;宿主用 semver `includePrerelease` 校验,已用其 `evaluatePluginCompatibility` 验证 `0.2.2-rc.1` 为 COMPATIBLE、`0.3.0-rc.1`/`0.3.0-rc.2`/`0.3.0` 为 UNMET)。
|
|
21
34
|
- DSH 0.2.x 移除了旧的插件配置注册接口,改为读取插件导出的 schemastery `Config`:`baseUrl`、`keyRef` 声明为 `volatile` 字段,由宿主生成配置表单并以 live ref 传入 `apply(ctx, config)`。因此 `0.1.x` 及更早版本在 DSH 0.2.x 上无法激活,请升级到 `0.2.0` 或更高版本。
|
|
22
35
|
- 升级方式:先把 DSH 升级到 0.2.x,再执行下面的安装命令(默认拉取 `@latest`),然后重启 `dsh web`。Base URL 会以插件配置写入当前 profile 的 `cordis.patch.yml`,API Key 仍保存在宿主凭据库,无需迁移。
|
|
23
36
|
|
|
@@ -46,10 +59,12 @@ dsh plugin --profile web remove dsh-opencode-status
|
|
|
46
59
|
|
|
47
60
|
## 配置
|
|
48
61
|
|
|
49
|
-
|
|
62
|
+
点侧栏常驻按钮打开用量面板,再点面板底部「⚙ 设置」进入连接设置视图;未配置时面板会直接显示设置视图:
|
|
50
63
|
- **Base URL**:默认 `https://opencode.ai/zen/go`(也可用环境变量 `OPENCODE_BASE_URL` 提供未持久化时的默认值)
|
|
51
64
|
- **API Key**:粘贴你的 OpenCode API Key(亦可直接在环境变量中配置 `OPENCODE_GO_API_KEY`)
|
|
52
65
|
|
|
66
|
+
保存成功后自动回到用量视图;「取消」丢弃未保存的编辑并关闭面板,重新打开时表单从已保存配置重新渲染。
|
|
67
|
+
|
|
53
68
|
配置存储方式(DSH 0.2.x):
|
|
54
69
|
- **Base URL** 属于插件 `Config` 的 `volatile` 字段:修改时通过宿主的 settings 服务写回当前 profile 条目(`opencode-status`),重启后依然生效;宿主未提供 settings 服务时,修改 Base URL 会返回明确的错误响应,插件不会崩溃。
|
|
55
70
|
- **API Key** 只写入宿主凭据库(默认引用 `OPENCODE_GO_API_KEY`),HTTP 响应仅返回 `••••` 掩码提示,不回传明文。
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-opencode-status",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "OpenCode GO quota
|
|
3
|
+
"version": "0.4.0",
|
|
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",
|
|
7
7
|
"keywords": [
|
package/src/client.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-opencode-status — Client half.
|
|
2
|
+
* dsh-opencode-status — Client half (0.3.0).
|
|
3
3
|
*
|
|
4
4
|
* 槽位:
|
|
5
|
-
* sidebar.footer.action
|
|
6
|
-
* shell.overlay
|
|
5
|
+
* sidebar.footer.action 常驻紧凑按钮(状态灯 + 月剩余摘要;点击打开用量面板)
|
|
6
|
+
* shell.overlay 用量面板(状态视图:三个窗口 + 固定操作条;设置视图:Base URL / API Key)
|
|
7
|
+
*
|
|
8
|
+
* 上游 /v1/usage 只提供 rolling/weekly/monthly 百分比,没有 USD 余额接口
|
|
9
|
+
* (anomalyco/opencode#10448 仍 open),界面只表述「剩余额度 / 可用百分比」,不编造金额。
|
|
7
10
|
*/
|
|
8
11
|
window.__ModuleLoader__.load({
|
|
9
12
|
id: 'dsh-opencode-status',
|
|
@@ -22,14 +25,18 @@ window.__ModuleLoader__.load({
|
|
|
22
25
|
@keyframes ocPulse { 0% { box-shadow: 0 0 0 0 rgba(34,197,94,.45) } 70% { box-shadow: 0 0 0 5px rgba(34,197,94,0) } 100% { box-shadow: 0 0 0 0 rgba(34,197,94,0) } }
|
|
23
26
|
.oc-panel-enter { animation: ocFadeSlideUp .2s cubic-bezier(.2,.9,.3,1.2) both }
|
|
24
27
|
.oc-panel-exit { animation: ocFadeSlideDown .15s ease-in both; pointer-events: none !important }
|
|
25
|
-
.oc-btn { min-width: 0; transition: background .15s ease, border-color .15s ease, color .15s ease, transform .1s ease }
|
|
26
|
-
.oc-btn:hover:not(:disabled)
|
|
27
|
-
.oc-btn-primary:hover:not(:disabled) { filter: brightness(1.12) }
|
|
28
|
+
.oc-btn { min-width: 0; transition: background .15s ease, border-color .15s ease, color .15s ease, transform .1s ease, opacity .15s ease }
|
|
29
|
+
.oc-btn:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover, var(--dsw-alias-bg-layer-1)); border-color: var(--dsw-alias-border-l2) }
|
|
28
30
|
.oc-btn:active:not(:disabled) { transform: scale(.98) }
|
|
31
|
+
.oc-btn:disabled { opacity: .55; cursor: default }
|
|
29
32
|
.oc-spin { display: inline-block; animation: ocSpin .8s linear infinite }
|
|
30
|
-
|
|
33
|
+
/* 状态灯呼吸只播一次:数据/状态变化时由 React key 换新节点重播,不再无限循环抢注意力 */
|
|
34
|
+
.oc-dot-live { animation: ocPulse 1.2s ease-out 1 both }
|
|
35
|
+
/* 悬浮微卡只读:不接收任何指针事件,避免遮挡 footer 按钮的点击 */
|
|
36
|
+
.oc-hover-card { pointer-events: none !important }
|
|
31
37
|
@media (prefers-reduced-motion: reduce) {
|
|
32
38
|
.oc-panel-enter, .oc-panel-exit, .oc-spin, .oc-dot-live { animation: none !important }
|
|
39
|
+
.oc-btn { transition: none !important }
|
|
33
40
|
}
|
|
34
41
|
`
|
|
35
42
|
|
|
@@ -85,6 +92,11 @@ window.__ModuleLoader__.load({
|
|
|
85
92
|
outline: 'none',
|
|
86
93
|
}
|
|
87
94
|
|
|
95
|
+
const labelStyle = { display: 'block', fontSize: 12, color: T.secondary, marginBottom: 4 }
|
|
96
|
+
|
|
97
|
+
// 面板内的可聚焦元素:仅用于 Tab 首尾循环,不是完整的焦点陷阱。
|
|
98
|
+
const FOCUSABLE = 'button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])'
|
|
99
|
+
|
|
88
100
|
function timeUntil(ts) {
|
|
89
101
|
if (!ts) return ''
|
|
90
102
|
const diff = ts - Date.now()
|
|
@@ -96,17 +108,91 @@ window.__ModuleLoader__.load({
|
|
|
96
108
|
return `${Math.floor(hours / 24)}d${hours % 24 ? `${hours % 24}h` : ''}`
|
|
97
109
|
}
|
|
98
110
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
111
|
+
/** 同步时间显示为 HH:MM:SS(本地时区,取 status.fetchedAt)。 */
|
|
112
|
+
function fmtClock(ts) {
|
|
113
|
+
if (!ts) return ''
|
|
114
|
+
const d = new Date(ts)
|
|
115
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
116
|
+
return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 剩余额度阈值着色:>50 正常、>20 警告、其余错误(与 cpa 的 QuotaBar 语义一致)。 */
|
|
120
|
+
function remainingColor(remaining) {
|
|
121
|
+
if (remaining === null || remaining === undefined) return T.secondary
|
|
122
|
+
if (remaining > 50) return T.ok
|
|
123
|
+
if (remaining > 20) return T.warn
|
|
124
|
+
return T.err
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 额度档位文字:颜色之外再给一个非颜色信号(色盲用户与读屏都不能只靠颜色),阈值与 remainingColor 对齐。 */
|
|
128
|
+
function remainingBand(remaining) {
|
|
129
|
+
if (remaining === null || remaining === undefined) return null
|
|
130
|
+
if (remaining <= 20) return '即将耗尽'
|
|
131
|
+
if (remaining <= 50) return '偏低'
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 相对时间:面板显示「12s 前」「3 分钟前」,绝对时间放 title,避免每次轮询都盯着一串时分秒。 */
|
|
136
|
+
function fmtRelative(ts, now = Date.now()) {
|
|
137
|
+
if (!ts) return ''
|
|
138
|
+
const diff = Math.max(0, now - ts)
|
|
139
|
+
if (diff < 10_000) return '刚刚'
|
|
140
|
+
const secs = Math.floor(diff / 1000)
|
|
141
|
+
if (secs < 60) return `${secs}s 前`
|
|
142
|
+
const mins = Math.floor(secs / 60)
|
|
143
|
+
if (mins < 60) return `${mins} 分钟前`
|
|
144
|
+
const hours = Math.floor(mins / 60)
|
|
145
|
+
if (hours < 24) return `${hours} 小时前`
|
|
146
|
+
return `${Math.floor(hours / 24)} 天前`
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 同步数据超过 2 分钟未更新:给弱过期提示,避免把旧额度当成实时值。 */
|
|
150
|
+
const STALE_AFTER_MS = 2 * 60_000
|
|
151
|
+
|
|
152
|
+
function clampPct(value) {
|
|
153
|
+
return Math.max(0, Math.min(100, value))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 百分比显示值:取整并钳制到 0–100(宿主已规整,这里再兜一层,旧数据也不会出现小数或越界值)。 */
|
|
157
|
+
function pctText(value) {
|
|
158
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—'
|
|
159
|
+
return `${Math.round(clampPct(value))}%`
|
|
160
|
+
}
|
|
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
|
+
function monthlyRemainingOf(status) {
|
|
175
|
+
if (!status) return null
|
|
176
|
+
const windows = Array.isArray(status.windows) ? status.windows : []
|
|
177
|
+
const monthly = windows.find((w) => w?.id === 'monthly')
|
|
178
|
+
if (monthly && typeof monthly.remainingPct === 'number') return monthly.remainingPct
|
|
179
|
+
if (typeof status.monthlyUsedPct === 'number') return Math.max(0, 100 - status.monthlyUsedPct)
|
|
180
|
+
return null
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function hostOf(url) {
|
|
184
|
+
try {
|
|
185
|
+
return new URL(url).host
|
|
186
|
+
} catch {
|
|
187
|
+
return url
|
|
188
|
+
}
|
|
104
189
|
}
|
|
105
190
|
|
|
106
191
|
// ---------- Store ----------
|
|
107
192
|
const store = {
|
|
108
193
|
state: {
|
|
109
194
|
open: false,
|
|
195
|
+
view: 'status', // 'status' 用量视图 | 'settings' 连接设置视图
|
|
110
196
|
status: null,
|
|
111
197
|
config: null,
|
|
112
198
|
refreshing: false,
|
|
@@ -127,6 +213,7 @@ window.__ModuleLoader__.load({
|
|
|
127
213
|
return React.useSyncExternalStore(store.subscribe, () => store.state)
|
|
128
214
|
}
|
|
129
215
|
|
|
216
|
+
// ---------- 本机 API ----------
|
|
130
217
|
async function api(path, options) {
|
|
131
218
|
const res = await fetch(path, options)
|
|
132
219
|
const data = await res.json().catch(() => ({}))
|
|
@@ -134,18 +221,29 @@ window.__ModuleLoader__.load({
|
|
|
134
221
|
return data
|
|
135
222
|
}
|
|
136
223
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
224
|
+
// 请求序号:手动刷新会推进 epoch,使所有更早发出的 GET 响应(成功或失败)失效,
|
|
225
|
+
// 避免极端时序下旧数据覆盖手动刷新的新结果。
|
|
226
|
+
let statusEpoch = 0
|
|
227
|
+
|
|
228
|
+
/** GET 状态:走宿主 30 秒缓存,打开面板/切换视图都不会额外探测上游。 */
|
|
229
|
+
let statusInFlight = null
|
|
230
|
+
async function refreshStatus() {
|
|
231
|
+
// 并发去重:挂载、打开面板与 30s 轮询同时触发时只发一个 GET,避免首个请求未写入缓存前重复探测。
|
|
232
|
+
if (store.state.refreshing) return
|
|
233
|
+
if (statusInFlight) return statusInFlight
|
|
234
|
+
const epoch = statusEpoch
|
|
235
|
+
statusInFlight = (async () => {
|
|
236
|
+
try {
|
|
237
|
+
const status = await api('/api/opencode-status')
|
|
238
|
+
// 只接受最新 epoch 的响应:手动刷新之后返回的在途 GET 一律丢弃。
|
|
239
|
+
if (epoch === statusEpoch) store.set({ status, error: null })
|
|
240
|
+
} catch (err) {
|
|
241
|
+
if (epoch === statusEpoch) store.set({ error: String(err?.message ?? err) })
|
|
242
|
+
} finally {
|
|
243
|
+
statusInFlight = null
|
|
244
|
+
}
|
|
245
|
+
})()
|
|
246
|
+
return statusInFlight
|
|
149
247
|
}
|
|
150
248
|
|
|
151
249
|
async function refreshConfig() {
|
|
@@ -155,16 +253,71 @@ window.__ModuleLoader__.load({
|
|
|
155
253
|
} catch {}
|
|
156
254
|
}
|
|
157
255
|
|
|
158
|
-
|
|
256
|
+
/** 手动刷新唯一入口:绕过 30 秒缓存,强制重新拉取当前剩余额度。 */
|
|
257
|
+
async function manualRefresh() {
|
|
258
|
+
if (store.state.refreshing) return
|
|
259
|
+
// 先推进序号:手动刷新发起后,任何更早发出的在途 GET 响应都不再落库。
|
|
260
|
+
statusEpoch += 1
|
|
261
|
+
store.set({ refreshing: true, error: null })
|
|
262
|
+
try {
|
|
263
|
+
const status = await api('/api/opencode-status/sync', {
|
|
264
|
+
method: 'POST',
|
|
265
|
+
headers: { 'content-type': 'application/json' },
|
|
266
|
+
})
|
|
267
|
+
// 手动刷新自身的响应始终落库(刷新期间已阻止新的 GET)。
|
|
268
|
+
store.set({ status, error: null })
|
|
269
|
+
} catch (err) {
|
|
270
|
+
store.set({ error: String(err?.message ?? err) })
|
|
271
|
+
} finally {
|
|
272
|
+
store.set({ refreshing: false })
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function openOverlay() {
|
|
277
|
+
store.set({ open: true, view: 'status' })
|
|
278
|
+
// 面板默认落用量视图;GET 命中 30 秒缓存,不触发额外上游探测。
|
|
279
|
+
refreshStatus()
|
|
280
|
+
refreshConfig()
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function closeOverlay() {
|
|
284
|
+
store.set({ open: false })
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function toggleOverlay() {
|
|
288
|
+
if (store.state.open) closeOverlay()
|
|
289
|
+
else openOverlay()
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function openSettings() {
|
|
293
|
+
refreshConfig()
|
|
294
|
+
store.set({ view: 'settings' })
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ---------- 常驻入口(状态灯 + 月剩余摘要 + 220ms 悬浮微卡) ----------
|
|
159
298
|
function FooterEntry(props) {
|
|
160
299
|
const state = useStore()
|
|
161
300
|
const [hovering, setHovering] = useState(false)
|
|
162
301
|
const hoverTimerRef = useRef(null)
|
|
163
302
|
|
|
164
303
|
useEffect(() => {
|
|
165
|
-
refreshStatus(
|
|
166
|
-
|
|
167
|
-
|
|
304
|
+
refreshStatus()
|
|
305
|
+
// 30 秒 GET 自动刷新;页面隐藏时暂停轮询,恢复可见时立即补拉一次,避免后台空转探测上游。
|
|
306
|
+
let timer = setInterval(() => refreshStatus(), 30_000)
|
|
307
|
+
const onVisibilityChange = () => {
|
|
308
|
+
clearInterval(timer)
|
|
309
|
+
timer = null
|
|
310
|
+
if (typeof document !== 'undefined' && document.hidden) return
|
|
311
|
+
refreshStatus()
|
|
312
|
+
timer = setInterval(() => refreshStatus(), 30_000)
|
|
313
|
+
}
|
|
314
|
+
// 测试与 SSR 沙箱里 document 可能没有事件接口,先探测再注册。
|
|
315
|
+
const canListen = typeof document !== 'undefined' && typeof document.addEventListener === 'function'
|
|
316
|
+
if (canListen) document.addEventListener('visibilitychange', onVisibilityChange)
|
|
317
|
+
return () => {
|
|
318
|
+
if (timer !== null) clearInterval(timer)
|
|
319
|
+
if (canListen) document.removeEventListener('visibilitychange', onVisibilityChange)
|
|
320
|
+
}
|
|
168
321
|
}, [])
|
|
169
322
|
|
|
170
323
|
const onMouseEnter = () => {
|
|
@@ -177,95 +330,122 @@ window.__ModuleLoader__.load({
|
|
|
177
330
|
}
|
|
178
331
|
|
|
179
332
|
const st = state.status
|
|
333
|
+
const monthlyRemaining = monthlyRemainingOf(st)
|
|
180
334
|
let color = T.warn
|
|
181
|
-
let
|
|
335
|
+
let label = '连接 OpenCode'
|
|
182
336
|
let value = null
|
|
183
337
|
|
|
184
338
|
if (state.error) {
|
|
185
339
|
color = T.err
|
|
186
|
-
|
|
340
|
+
label = 'OpenCode ?'
|
|
341
|
+
} else if (st?.mode === 'needs_config') {
|
|
342
|
+
color = T.warn
|
|
343
|
+
label = 'OpenCode'
|
|
344
|
+
value = '未配置'
|
|
187
345
|
} else if (st?.mode === 'ready') {
|
|
188
346
|
if (st.ok) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
value = pct !== null ? `月用量 ${pct}%` : '在线'
|
|
347
|
+
color = monthlyRemaining === null ? T.warn : remainingColor(monthlyRemaining)
|
|
348
|
+
label = 'OpenCode'
|
|
349
|
+
value = monthlyRemaining !== null ? `月剩余 ${monthlyRemaining}%` : '在线'
|
|
193
350
|
} else {
|
|
194
351
|
color = T.err
|
|
195
|
-
|
|
352
|
+
label = 'OpenCode'
|
|
196
353
|
value = '异常'
|
|
197
354
|
}
|
|
198
355
|
}
|
|
199
356
|
|
|
357
|
+
const title = state.error
|
|
358
|
+
? `本机状态接口异常:${state.error}`
|
|
359
|
+
: st?.mode === 'needs_config'
|
|
360
|
+
? '尚未配置 OpenCode API Key,点击设置'
|
|
361
|
+
: st?.mode === 'ready' && !st.ok
|
|
362
|
+
? `用量拉取异常:${st.error?.message ?? '未知错误'}`
|
|
363
|
+
: monthlyRemaining !== null
|
|
364
|
+
? `OpenCode GO · 月剩余 ${monthlyRemaining}% · 点击查看详情`
|
|
365
|
+
: 'OpenCode GO,点击查看详情'
|
|
366
|
+
|
|
367
|
+
// rail 模式:真正的 button(键盘可达、可点击),同样打开用量视图
|
|
200
368
|
if (props?.wide === false) {
|
|
201
369
|
return h(
|
|
202
|
-
'
|
|
370
|
+
'button',
|
|
203
371
|
{
|
|
204
|
-
className: 'oc-btn',
|
|
372
|
+
className: 'oc-btn oc-footer-btn',
|
|
373
|
+
type: 'button',
|
|
374
|
+
title,
|
|
375
|
+
'aria-label': '打开 OpenCode GO 用量面板',
|
|
205
376
|
style: {
|
|
206
|
-
boxSizing: 'border-box',
|
|
207
377
|
width: 36,
|
|
208
378
|
height: 36,
|
|
209
379
|
display: 'flex',
|
|
210
380
|
alignItems: 'center',
|
|
211
381
|
justifyContent: 'center',
|
|
212
382
|
borderRadius: 8,
|
|
383
|
+
border: `1px solid ${color === T.err ? T.err : T.border}`,
|
|
384
|
+
background: 'transparent',
|
|
385
|
+
color,
|
|
213
386
|
fontSize: 10,
|
|
214
387
|
fontWeight: 700,
|
|
215
|
-
color: T.label,
|
|
216
388
|
cursor: 'pointer',
|
|
217
389
|
margin: '2px auto',
|
|
390
|
+
padding: 0,
|
|
391
|
+
fontVariantNumeric: 'tabular-nums',
|
|
218
392
|
},
|
|
219
|
-
|
|
220
|
-
onClick: () => store.set({ open: !state.open }),
|
|
393
|
+
onClick: toggleOverlay,
|
|
221
394
|
},
|
|
222
|
-
|
|
395
|
+
monthlyRemaining !== null ? `${monthlyRemaining}%` : 'GO',
|
|
223
396
|
)
|
|
224
397
|
}
|
|
225
398
|
|
|
399
|
+
const segments = [
|
|
400
|
+
{ text: label, color: T.label },
|
|
401
|
+
value ? { text: value, color } : null,
|
|
402
|
+
].filter(Boolean)
|
|
403
|
+
const labelEl = h(
|
|
404
|
+
'span',
|
|
405
|
+
{ style: { display: 'inline-flex', alignItems: 'center', lineHeight: '18px', whiteSpace: 'nowrap', minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis' } },
|
|
406
|
+
segments.flatMap((s, i) =>
|
|
407
|
+
i === 0
|
|
408
|
+
? [h('span', { key: `s${i}`, style: { color: s.color, fontWeight: 500, flexShrink: 0 } }, s.text)]
|
|
409
|
+
: [
|
|
410
|
+
h('span', { key: `sep${i}`, style: { color: T.secondary, margin: '0 5px', opacity: 0.7, flexShrink: 0 } }, '·'),
|
|
411
|
+
h('span', { key: `s${i}`, style: { color: s.color, fontWeight: 500, fontVariantNumeric: 'tabular-nums', overflow: 'hidden', textOverflow: 'ellipsis' } }, s.text),
|
|
412
|
+
],
|
|
413
|
+
),
|
|
414
|
+
)
|
|
415
|
+
|
|
226
416
|
const node = h(
|
|
227
417
|
'button',
|
|
228
418
|
{
|
|
229
419
|
className: 'oc-btn oc-footer-btn',
|
|
420
|
+
type: 'button',
|
|
421
|
+
title,
|
|
230
422
|
onMouseEnter,
|
|
231
423
|
onMouseLeave,
|
|
232
424
|
style: {
|
|
233
425
|
...btnBase,
|
|
234
|
-
display:
|
|
235
|
-
width:
|
|
426
|
+
display: 'flex',
|
|
427
|
+
width: '100%',
|
|
236
428
|
boxSizing: 'border-box',
|
|
237
|
-
justifyContent:
|
|
429
|
+
justifyContent: 'flex-start',
|
|
238
430
|
margin: '2px 0',
|
|
239
431
|
minWidth: 0,
|
|
240
432
|
flexShrink: 0,
|
|
241
433
|
borderColor: state.open ? T.brand : T.border,
|
|
242
434
|
background: state.open ? 'var(--dsw-alias-interactive-bg-hover, rgba(59,130,246,.12))' : 'transparent',
|
|
243
435
|
},
|
|
244
|
-
onClick:
|
|
245
|
-
store.set({ open: !state.open })
|
|
246
|
-
if (!state.open) {
|
|
247
|
-
refreshStatus(false)
|
|
248
|
-
refreshConfig()
|
|
249
|
-
}
|
|
250
|
-
},
|
|
436
|
+
onClick: toggleOverlay,
|
|
251
437
|
},
|
|
252
|
-
h('span', {
|
|
253
|
-
|
|
254
|
-
'
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
? [
|
|
260
|
-
h('span', { key: 'sep', style: { color: T.secondary, margin: '0 5px', opacity: 0.7, flexShrink: 0 } }, '·'),
|
|
261
|
-
h('span', { key: 'v', style: { color, fontWeight: 500, fontVariantNumeric: 'tabular-nums', overflow: 'hidden', textOverflow: 'ellipsis' } }, value),
|
|
262
|
-
]
|
|
263
|
-
: null,
|
|
264
|
-
].flat().filter(Boolean),
|
|
265
|
-
),
|
|
438
|
+
h('span', {
|
|
439
|
+
// 呼吸动画只播一次:key 绑定 fetchedAt,新数据到达时换新节点重播一次,而不是无限循环。
|
|
440
|
+
key: color === T.ok ? `live-${st?.fetchedAt ?? 0}` : 'live',
|
|
441
|
+
className: color === T.ok ? 'oc-dot-live' : undefined,
|
|
442
|
+
style: dotStyle(color),
|
|
443
|
+
}),
|
|
444
|
+
labelEl,
|
|
266
445
|
)
|
|
267
446
|
|
|
268
|
-
|
|
447
|
+
// 悬浮微卡:面板关闭、悬停 220ms、且 ready + ok 时才出现
|
|
448
|
+
if (!hovering || state.open || st?.mode !== 'ready' || !st.ok) return node
|
|
269
449
|
|
|
270
450
|
return h(
|
|
271
451
|
'div',
|
|
@@ -275,59 +455,188 @@ window.__ModuleLoader__.load({
|
|
|
275
455
|
onMouseLeave,
|
|
276
456
|
},
|
|
277
457
|
node,
|
|
278
|
-
h(
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
458
|
+
h(HoverCard, { status: st }),
|
|
459
|
+
)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// ---------- 220ms 悬浮微卡(只读概览,无假按钮) ----------
|
|
463
|
+
function HoverCard(props) {
|
|
464
|
+
const st = props.status
|
|
465
|
+
const windows = Array.isArray(st?.windows) ? st.windows : []
|
|
466
|
+
return h(
|
|
467
|
+
'div',
|
|
468
|
+
{
|
|
469
|
+
className: 'oc-panel-enter oc-hover-card',
|
|
470
|
+
// 只读微卡是面板的视觉重复,对读屏隐藏,避免同一份数据播报两遍。
|
|
471
|
+
'aria-hidden': true,
|
|
472
|
+
style: {
|
|
473
|
+
position: 'absolute',
|
|
474
|
+
left: 0,
|
|
475
|
+
right: 0,
|
|
476
|
+
bottom: 'calc(100% + 6px)',
|
|
477
|
+
pointerEvents: 'none',
|
|
478
|
+
background: T.bg,
|
|
479
|
+
border: `1px solid ${T.border2}`,
|
|
480
|
+
borderRadius: 10,
|
|
481
|
+
boxShadow: '0 8px 24px rgba(0,0,0,.18)',
|
|
482
|
+
padding: '10px 12px 8px',
|
|
483
|
+
fontSize: 11,
|
|
484
|
+
color: T.label,
|
|
485
|
+
zIndex: 100,
|
|
486
|
+
fontFamily: 'system-ui, -apple-system, sans-serif',
|
|
297
487
|
},
|
|
488
|
+
},
|
|
489
|
+
[
|
|
490
|
+
h('div', { key: 'hd', style: { display: 'flex', alignItems: 'center', gap: 6, marginBottom: 8 } }, [
|
|
491
|
+
h('span', { key: 't', style: { fontWeight: 600, fontSize: 12, color: T.label } }, 'OpenCode GO 用量'),
|
|
492
|
+
// Go 标识(纯展示徽章,不是按钮)
|
|
493
|
+
h(
|
|
494
|
+
'span',
|
|
495
|
+
{
|
|
496
|
+
key: 'go',
|
|
497
|
+
style: {
|
|
498
|
+
fontSize: 9,
|
|
499
|
+
fontWeight: 700,
|
|
500
|
+
letterSpacing: 0.4,
|
|
501
|
+
color: '#fff',
|
|
502
|
+
background: T.brand,
|
|
503
|
+
borderRadius: 4,
|
|
504
|
+
padding: '1px 5px',
|
|
505
|
+
lineHeight: '13px',
|
|
506
|
+
flexShrink: 0,
|
|
507
|
+
},
|
|
508
|
+
},
|
|
509
|
+
'GO',
|
|
510
|
+
),
|
|
511
|
+
h(
|
|
512
|
+
'span',
|
|
513
|
+
{
|
|
514
|
+
key: 'at',
|
|
515
|
+
// 相对时间更易读;绝对时刻保留在 title 里,需要精确对时再悬停查看。
|
|
516
|
+
title: st?.fetchedAt ? `上次同步 ${fmtClock(st.fetchedAt)}` : '尚未同步',
|
|
517
|
+
style: { marginLeft: 'auto', fontSize: 10, color: T.secondary, fontVariantNumeric: 'tabular-nums', flexShrink: 0 },
|
|
518
|
+
},
|
|
519
|
+
st?.fetchedAt ? `上次同步 ${fmtRelative(st.fetchedAt)}` : '尚未同步',
|
|
520
|
+
),
|
|
521
|
+
]),
|
|
298
522
|
h(
|
|
299
523
|
'div',
|
|
300
|
-
{
|
|
301
|
-
|
|
302
|
-
display: 'flex',
|
|
303
|
-
alignItems: 'center',
|
|
304
|
-
justifyContent: 'space-between',
|
|
305
|
-
marginBottom: 10,
|
|
306
|
-
},
|
|
307
|
-
},
|
|
308
|
-
h('span', { style: { fontWeight: 600, fontSize: '12px' } }, '🚀 OpenCode GO 用量概览'),
|
|
309
|
-
st.plan ? h('span', { style: { fontSize: '10px', color: T.brand, fontWeight: 600 } }, st.plan) : null,
|
|
524
|
+
{ key: 'rows', style: { display: 'flex', flexDirection: 'column', gap: 6 } },
|
|
525
|
+
windows.map((w, i) => h(MiniQuotaRow, { key: w.id ?? i, window: w })),
|
|
310
526
|
),
|
|
311
|
-
|
|
312
|
-
|
|
527
|
+
h(
|
|
528
|
+
'div',
|
|
529
|
+
{ key: 'hint', style: { marginTop: 8, paddingTop: 6, borderTop: `1px dashed ${T.border}`, fontSize: 10, color: T.secondary, textAlign: 'center' } },
|
|
530
|
+
'点击查看详情',
|
|
531
|
+
),
|
|
532
|
+
],
|
|
313
533
|
)
|
|
314
534
|
}
|
|
315
535
|
|
|
316
|
-
|
|
536
|
+
/** 悬浮卡紧凑行:名称 / 剩余 % / ⏱ 重置倒计时 / 细进度条。 */
|
|
537
|
+
function MiniQuotaRow(props) {
|
|
538
|
+
const w = props.window
|
|
539
|
+
const remaining = w?.remainingPct ?? null
|
|
540
|
+
const color = remainingColor(remaining)
|
|
541
|
+
return h('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } }, [
|
|
542
|
+
h('span', { key: 'n', style: { width: 46, flexShrink: 0, fontSize: 10, color: T.secondary, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, w?.shortLabel ?? w?.label ?? '—'),
|
|
543
|
+
h(
|
|
544
|
+
'div',
|
|
545
|
+
{ key: 'bar', style: { flex: 1, minWidth: 0, height: 3, borderRadius: 2, background: `color-mix(in srgb, ${color} 16%, transparent)`, overflow: 'hidden' } },
|
|
546
|
+
remaining === null
|
|
547
|
+
? null
|
|
548
|
+
: h('div', { style: { height: '100%', width: `${clampPct(remaining)}%`, background: color, borderRadius: 2 } }),
|
|
549
|
+
),
|
|
550
|
+
h('span', { key: 'v', style: { minWidth: 30, textAlign: 'right', fontSize: 10, fontWeight: 600, color, fontVariantNumeric: 'tabular-nums' } }, remaining === null ? '—' : pctText(remaining)),
|
|
551
|
+
// 倒计时用 ⏱ 区分刷新动作的 ↻,避免两个图标同义混淆。
|
|
552
|
+
h('span', { key: 'r', style: { minWidth: 34, textAlign: 'right', fontSize: 10, color: T.secondary, fontVariantNumeric: 'tabular-nums' } }, w?.resetAt ? `⏱ ${timeUntil(w.resetAt)}` : ''),
|
|
553
|
+
])
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// ---------- 用量视图 ----------
|
|
557
|
+
function StatusView() {
|
|
558
|
+
const state = useStore()
|
|
559
|
+
const st = state.status
|
|
560
|
+
|
|
561
|
+
if (!st) {
|
|
562
|
+
return h('div', { style: { fontSize: 12, color: T.secondary } }, state.error ? `读取状态失败:${state.error}` : '加载中…')
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const windows = Array.isArray(st.windows) ? st.windows : []
|
|
566
|
+
// 头部只给判断性摘要(最紧张窗口),具体数字留给下方窗口行,避免同一数字说两遍。
|
|
567
|
+
const tightest = tightestWindow(st)
|
|
568
|
+
|
|
569
|
+
return h('div', null, [
|
|
570
|
+
h('div', { key: 'sum', style: { display: 'flex', alignItems: 'center', gap: 6 } }, [
|
|
571
|
+
h('span', { key: 'd', style: dotStyle(st.ok ? T.ok : T.err) }),
|
|
572
|
+
h('span', { key: 't', style: { fontSize: 12, fontWeight: 600, color: T.label } }, st.ok ? 'OpenCode GO 已连接' : 'OpenCode GO 异常'),
|
|
573
|
+
tightest
|
|
574
|
+
? h(
|
|
575
|
+
'span',
|
|
576
|
+
{
|
|
577
|
+
key: 'm',
|
|
578
|
+
// host 只在面板标题行展示一次;这里不再重复。
|
|
579
|
+
title: `最紧张窗口:${tightest.label ?? tightest.id ?? '—'},剩余 ${pctText(tightest.remainingPct)}`,
|
|
580
|
+
style: { marginLeft: 'auto', fontSize: 11, fontWeight: 600, color: remainingColor(tightest.remainingPct), fontVariantNumeric: 'tabular-nums', flexShrink: 0 },
|
|
581
|
+
},
|
|
582
|
+
`最紧张 ${tightest.shortLabel ?? tightest.label ?? '—'} ${pctText(tightest.remainingPct)}`,
|
|
583
|
+
)
|
|
584
|
+
: null,
|
|
585
|
+
].filter(Boolean)),
|
|
586
|
+
state.error
|
|
587
|
+
? h('div', { key: 'ce', style: { fontSize: 11, color: T.err, marginTop: 8 } }, `刷新失败:${state.error}`)
|
|
588
|
+
: null,
|
|
589
|
+
!st.ok
|
|
590
|
+
? h('div', { key: 'se', style: { fontSize: 12, color: T.err, marginTop: 10 } }, st.error?.message ?? '网关返回异常,稍后重试')
|
|
591
|
+
: null,
|
|
592
|
+
st.ok && windows.length
|
|
593
|
+
? h('div', { key: 'w', style: { marginTop: 14 } }, windows.map((w, i) => h(QuotaRow, { key: w.id ?? i, window: w })))
|
|
594
|
+
: null,
|
|
595
|
+
st.ok && !windows.length
|
|
596
|
+
? h('div', { key: 'empty', style: { fontSize: 11, color: T.secondary, marginTop: 10 } }, '暂无用量窗口数据')
|
|
597
|
+
: null,
|
|
598
|
+
].filter(Boolean))
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* 面板窗口行:主百分比只显示剩余额度(进度条填充同源),「已用」降级到 aria-valuetext 与 title。
|
|
603
|
+
* 语义约定:条长 = 剩余额度占比,条色 = 剩余额度档位;不再同一行并列「已用 X% / 剩余 Y%」重复表达。
|
|
604
|
+
*/
|
|
317
605
|
function QuotaRow(props) {
|
|
318
606
|
const w = props.window
|
|
319
|
-
const used = w
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
607
|
+
const used = w?.usedPct ?? null
|
|
608
|
+
const remaining = w?.remainingPct ?? null
|
|
609
|
+
const color = remainingColor(remaining)
|
|
610
|
+
const band = remainingBand(remaining)
|
|
611
|
+
const statusText = w?.status && w.status !== 'ok' ? w.status : null
|
|
612
|
+
const detail = remaining === null ? '数据不可用' : `已用 ${pctText(used)} · 剩余 ${pctText(remaining)}`
|
|
613
|
+
return h('div', { style: { marginBottom: 12 } }, [
|
|
614
|
+
h('div', { key: 'meta', style: { display: 'flex', alignItems: 'baseline', gap: 6, fontSize: 11, marginBottom: 4, fontVariantNumeric: 'tabular-nums' } }, [
|
|
615
|
+
h('span', { key: 'l', style: { color: T.label, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, w?.label ?? '—'),
|
|
616
|
+
h('span', { key: 'r', title: detail, style: { marginLeft: 'auto', color, fontWeight: 600, flexShrink: 0 } }, remaining === null ? '剩余 —' : `剩余 ${pctText(remaining)}`),
|
|
617
|
+
// 低额度除颜色外再给文字标记:色觉障碍与灰度截图里也能看出档位。
|
|
618
|
+
band
|
|
619
|
+
? h(
|
|
620
|
+
'span',
|
|
621
|
+
{ key: 'band', style: { color, flexShrink: 0, fontSize: 10, lineHeight: '14px', padding: '0 4px', borderRadius: 4, border: `1px solid ${color}` } },
|
|
622
|
+
`⚠ ${band}`,
|
|
623
|
+
)
|
|
624
|
+
: null,
|
|
625
|
+
// 倒计时用 ⏱ 区分刷新动作的 ↻,避免两个图标同义混淆。
|
|
626
|
+
w?.resetAt ? h('span', { key: 't', style: { color: T.secondary, flexShrink: 0 } }, `⏱ ${timeUntil(w.resetAt)}`) : null,
|
|
627
|
+
].filter(Boolean)),
|
|
327
628
|
h(
|
|
328
629
|
'div',
|
|
329
630
|
{
|
|
330
631
|
key: 'track',
|
|
632
|
+
// 进度条语义:aria-valuenow 取剩余额度,与条长、右侧主百分比同源。
|
|
633
|
+
role: 'progressbar',
|
|
634
|
+
'aria-label': `${w?.label ?? '用量窗口'}剩余额度`,
|
|
635
|
+
'aria-valuemin': 0,
|
|
636
|
+
'aria-valuemax': 100,
|
|
637
|
+
...(remaining === null ? {} : { 'aria-valuenow': Math.round(clampPct(remaining)) }),
|
|
638
|
+
'aria-valuetext': remaining === null ? '数据不可用' : `剩余 ${pctText(remaining)},已用 ${pctText(used)}${band ? `,${band}` : ''}`,
|
|
639
|
+
title: detail,
|
|
331
640
|
style: {
|
|
332
641
|
height: 6,
|
|
333
642
|
borderRadius: 3,
|
|
@@ -336,22 +645,25 @@ window.__ModuleLoader__.load({
|
|
|
336
645
|
overflow: 'hidden',
|
|
337
646
|
},
|
|
338
647
|
},
|
|
339
|
-
|
|
648
|
+
remaining === null
|
|
340
649
|
? null
|
|
341
650
|
: h('div', {
|
|
342
651
|
style: {
|
|
343
652
|
height: '100%',
|
|
344
|
-
width: `${
|
|
653
|
+
width: `${clampPct(remaining)}%`,
|
|
345
654
|
background: color,
|
|
346
655
|
borderRadius: 3,
|
|
347
656
|
transition: 'width 0.4s ease',
|
|
348
657
|
},
|
|
349
658
|
}),
|
|
350
659
|
),
|
|
351
|
-
|
|
660
|
+
statusText
|
|
661
|
+
? h('div', { key: 'st', style: { fontSize: 10, color: T.warn, marginTop: 3 } }, `窗口状态异常:${statusText}`)
|
|
662
|
+
: null,
|
|
663
|
+
].filter(Boolean))
|
|
352
664
|
}
|
|
353
665
|
|
|
354
|
-
// ----------
|
|
666
|
+
// ---------- 连接设置视图 ----------
|
|
355
667
|
function ConfigCard() {
|
|
356
668
|
const state = useStore()
|
|
357
669
|
const cfg = state.config
|
|
@@ -374,7 +686,8 @@ window.__ModuleLoader__.load({
|
|
|
374
686
|
headers: { 'content-type': 'application/json' },
|
|
375
687
|
body: JSON.stringify({ baseUrl, ...(apiKey ? { apiKey } : {}) }),
|
|
376
688
|
})
|
|
377
|
-
|
|
689
|
+
// 保存成功后回到用量视图
|
|
690
|
+
store.set({ status: result.status, config: result.config, view: 'status', error: null })
|
|
378
691
|
setApiKey('')
|
|
379
692
|
} catch (e) {
|
|
380
693
|
setError(e?.message ?? '保存失败')
|
|
@@ -393,7 +706,7 @@ window.__ModuleLoader__.load({
|
|
|
393
706
|
headers: { 'content-type': 'application/json' },
|
|
394
707
|
body: JSON.stringify({ baseUrl, clearApiKey: true }),
|
|
395
708
|
})
|
|
396
|
-
store.set({ status: result.status, config: result.config })
|
|
709
|
+
store.set({ status: result.status, config: result.config, error: null })
|
|
397
710
|
} catch (e) {
|
|
398
711
|
setError(e?.message ?? '清除失败')
|
|
399
712
|
} finally {
|
|
@@ -406,12 +719,12 @@ window.__ModuleLoader__.load({
|
|
|
406
719
|
setBaseUrl(cfg?.baseUrl ?? '')
|
|
407
720
|
setApiKey('')
|
|
408
721
|
setError(null)
|
|
409
|
-
store.set({ open: false })
|
|
722
|
+
store.set({ open: false, view: 'status' })
|
|
410
723
|
}
|
|
411
724
|
|
|
412
725
|
return h('div', null, [
|
|
413
726
|
h('div', { key: 'u', style: { marginBottom: 10 } }, [
|
|
414
|
-
h('label', { style:
|
|
727
|
+
h('label', { style: labelStyle }, 'Base URL'),
|
|
415
728
|
h('input', {
|
|
416
729
|
style: inputStyle,
|
|
417
730
|
value: baseUrl,
|
|
@@ -420,7 +733,7 @@ window.__ModuleLoader__.load({
|
|
|
420
733
|
}),
|
|
421
734
|
]),
|
|
422
735
|
h('div', { key: 'k', style: { marginBottom: 10 } }, [
|
|
423
|
-
h('label', { style:
|
|
736
|
+
h('label', { style: labelStyle }, `API Key *${keyState.configured ? `(已保存 ${keyState.hint})` : ''}`),
|
|
424
737
|
h('input', {
|
|
425
738
|
style: { ...inputStyle, opacity: keyState.writable ? 1 : 0.6 },
|
|
426
739
|
type: 'password',
|
|
@@ -434,35 +747,109 @@ window.__ModuleLoader__.load({
|
|
|
434
747
|
h('div', { key: 'btns', style: { display: 'flex', gap: 8, marginTop: 12, alignItems: 'center' } }, [
|
|
435
748
|
h(
|
|
436
749
|
'button',
|
|
437
|
-
{ className: 'oc-btn
|
|
750
|
+
{ className: 'oc-btn', style: { ...btnBase, background: T.brand, borderColor: T.brand, color: '#fff' }, disabled: saving, onClick: save },
|
|
438
751
|
saving ? '保存中…' : '保存并连接',
|
|
439
752
|
),
|
|
440
753
|
h('button', { className: 'oc-btn', style: btnBase, disabled: saving, onClick: cancel }, '取消'),
|
|
441
754
|
keyState.configured && keyState.writable
|
|
442
755
|
? h('button', { className: 'oc-btn', style: { ...btnBase, color: T.err, marginLeft: 'auto' }, disabled: saving, onClick: clearKey }, '清除')
|
|
443
756
|
: null,
|
|
444
|
-
]),
|
|
757
|
+
].filter(Boolean)),
|
|
758
|
+
// 数据口径说明放设置视图:用量视图保持只读结论,正文不再常驻免责声明。
|
|
759
|
+
h(
|
|
760
|
+
'div',
|
|
761
|
+
{ key: 'note', style: { fontSize: 10, color: T.secondary, marginTop: 14, lineHeight: '15px' } },
|
|
762
|
+
'上游 /v1/usage 仅提供百分比额度,界面不展示金额余额。',
|
|
763
|
+
),
|
|
445
764
|
])
|
|
446
765
|
}
|
|
447
766
|
|
|
448
|
-
// ----------
|
|
767
|
+
// ---------- 固定底部操作条 ----------
|
|
768
|
+
function OpsBar(props) {
|
|
769
|
+
const state = useStore()
|
|
770
|
+
const st = state.status
|
|
771
|
+
// 相对时间更好读;绝对时刻保留在 title,同步超过 2 分钟再给弱过期提示。
|
|
772
|
+
const lastSync = st?.fetchedAt ? fmtRelative(st.fetchedAt) : null
|
|
773
|
+
const lastSyncTitle = st?.fetchedAt ? `上次同步 ${fmtClock(st.fetchedAt)}` : '尚未同步'
|
|
774
|
+
const lastSyncStale = Boolean(st?.fetchedAt) && Date.now() - st.fetchedAt > STALE_AFTER_MS
|
|
775
|
+
const syncText = lastSync ? `上次同步 ${lastSync}${lastSyncStale ? ' · 可能已过期' : ''}` : '尚未同步'
|
|
776
|
+
const syncSpan = (key, extraStyle) =>
|
|
777
|
+
h(
|
|
778
|
+
'span',
|
|
779
|
+
{ key, title: lastSyncTitle, style: { ...extraStyle, fontVariantNumeric: 'tabular-nums' } },
|
|
780
|
+
syncText,
|
|
781
|
+
)
|
|
782
|
+
|
|
783
|
+
if (props?.settings) {
|
|
784
|
+
return h('div', { style: barStyle }, [
|
|
785
|
+
// 只有未配置(needs_config)时没有可返回的用量视图;status 尚未加载时也要保留返回入口。
|
|
786
|
+
st?.mode !== 'needs_config'
|
|
787
|
+
? h(
|
|
788
|
+
'button',
|
|
789
|
+
{
|
|
790
|
+
className: 'oc-btn',
|
|
791
|
+
style: btnBase,
|
|
792
|
+
onClick: () => {
|
|
793
|
+
store.set({ view: 'status' })
|
|
794
|
+
refreshStatus()
|
|
795
|
+
},
|
|
796
|
+
},
|
|
797
|
+
'← 返回用量',
|
|
798
|
+
)
|
|
799
|
+
: null,
|
|
800
|
+
syncSpan('at', { marginLeft: 'auto', fontSize: 10, color: T.secondary }),
|
|
801
|
+
].filter(Boolean))
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
return h(
|
|
805
|
+
'div',
|
|
806
|
+
{ style: barStyle },
|
|
807
|
+
[
|
|
808
|
+
h(
|
|
809
|
+
'button',
|
|
810
|
+
{
|
|
811
|
+
className: 'oc-btn',
|
|
812
|
+
style: btnBase,
|
|
813
|
+
disabled: state.refreshing,
|
|
814
|
+
onClick: manualRefresh,
|
|
815
|
+
title: '绕过 30 秒缓存,强制重新拉取当前剩余额度',
|
|
816
|
+
},
|
|
817
|
+
h('span', { key: 'i', className: state.refreshing ? 'oc-spin' : undefined }, '↻'),
|
|
818
|
+
state.refreshing ? '刷新中…' : '刷新',
|
|
819
|
+
),
|
|
820
|
+
h('button', { key: 'set', className: 'oc-btn', style: btnBase, onClick: openSettings, title: '配置 Base URL 与 API Key' }, '⚙ 设置'),
|
|
821
|
+
syncSpan('at', { marginLeft: 'auto', fontSize: 10, color: lastSyncStale ? T.warn : T.secondary }),
|
|
822
|
+
],
|
|
823
|
+
)
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const barStyle = {
|
|
827
|
+
flexShrink: 0,
|
|
828
|
+
borderTop: `1px solid ${T.border}`,
|
|
829
|
+
padding: '10px 16px 12px',
|
|
830
|
+
display: 'flex',
|
|
831
|
+
alignItems: 'center',
|
|
832
|
+
gap: 8,
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// ---------- Overlay 面板(状态视图 / 设置视图) ----------
|
|
449
836
|
function OverlayPanel() {
|
|
450
837
|
const state = useStore()
|
|
451
838
|
const panelRef = useRef(null)
|
|
839
|
+
// 相位机:closed → open → closing(播完退场动画)→ closed。
|
|
840
|
+
// 拆成两个 effect:否则 phase 变化会清掉「170ms 后置 closed」的定时器,面板卡在不可见仍拦截点击的状态。
|
|
452
841
|
const [phase, setPhase] = useState('closed')
|
|
453
|
-
|
|
454
842
|
useEffect(() => {
|
|
455
843
|
if (state.open) setPhase('open')
|
|
456
844
|
else setPhase((p) => (p === 'open' ? 'closing' : p))
|
|
457
845
|
}, [state.open])
|
|
458
|
-
|
|
459
846
|
useEffect(() => {
|
|
460
847
|
if (phase !== 'closing') return
|
|
461
|
-
const timer = setTimeout(() => setPhase('closed'),
|
|
848
|
+
const timer = setTimeout(() => setPhase('closed'), 170)
|
|
462
849
|
return () => clearTimeout(timer)
|
|
463
850
|
}, [phase])
|
|
464
851
|
|
|
465
|
-
//
|
|
852
|
+
// 点击外部关闭;footer 常驻按钮自身不在面板内,放行它的点击。
|
|
466
853
|
useEffect(() => {
|
|
467
854
|
if (!state.open) return
|
|
468
855
|
const handleClickOutside = (e) => {
|
|
@@ -475,88 +862,115 @@ window.__ModuleLoader__.load({
|
|
|
475
862
|
return () => window.removeEventListener('mousedown', handleClickOutside)
|
|
476
863
|
}, [state.open])
|
|
477
864
|
|
|
865
|
+
// Esc 关闭:与面板同开同卸载;Tab 焦点保持在面板内,避免键盘用户掉到背后的不可见区域。
|
|
866
|
+
useEffect(() => {
|
|
867
|
+
if (!state.open) return
|
|
868
|
+
const handleKeyDown = (e) => {
|
|
869
|
+
if (e.key === 'Escape') {
|
|
870
|
+
e.stopPropagation()
|
|
871
|
+
closeOverlay()
|
|
872
|
+
return
|
|
873
|
+
}
|
|
874
|
+
if (e.key !== 'Tab' || !panelRef.current || typeof panelRef.current.querySelectorAll !== 'function') return
|
|
875
|
+
const focusables = [...panelRef.current.querySelectorAll(FOCUSABLE)]
|
|
876
|
+
if (!focusables.length) {
|
|
877
|
+
e.preventDefault()
|
|
878
|
+
panelRef.current.focus()
|
|
879
|
+
return
|
|
880
|
+
}
|
|
881
|
+
const first = focusables[0]
|
|
882
|
+
const last = focusables[focusables.length - 1]
|
|
883
|
+
if (e.shiftKey && (e.target === first || e.target === panelRef.current)) {
|
|
884
|
+
e.preventDefault()
|
|
885
|
+
last.focus()
|
|
886
|
+
} else if (!e.shiftKey && e.target === last) {
|
|
887
|
+
e.preventDefault()
|
|
888
|
+
first.focus()
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
window.addEventListener('keydown', handleKeyDown)
|
|
892
|
+
return () => window.removeEventListener('keydown', handleKeyDown)
|
|
893
|
+
}, [state.open])
|
|
894
|
+
|
|
895
|
+
// 焦点管理:面板挂载(phase 进入 open)后把焦点移入面板(容器 tabIndex=-1),关闭时归还打开前的触发元素(footer 按钮)。
|
|
896
|
+
// 依赖必须带上 phase:面板 DOM 要等 phase effect 把 phase 推到 'open' 才挂载,只依赖 state.open 时
|
|
897
|
+
// 首次执行 panelRef.current 还是 null,panel.focus() 会静默失效(运行时复现由 /tmp/verify-focus.mjs 手工验证,CI 无 jsdom 依赖)。
|
|
898
|
+
useEffect(() => {
|
|
899
|
+
if (!state.open || phase !== 'open') return
|
|
900
|
+
if (typeof document === 'undefined') return
|
|
901
|
+
const previous = document.activeElement
|
|
902
|
+
const panel = panelRef.current
|
|
903
|
+
if (panel && typeof panel.focus === 'function') panel.focus()
|
|
904
|
+
return () => {
|
|
905
|
+
if (!previous || typeof previous.focus !== 'function') return
|
|
906
|
+
// 只把焦点从面板(或已失焦的 body)收回触发按钮:点击外部关闭时不要抢走用户刚点中的控件焦点。
|
|
907
|
+
// cleanup 里不能用打开时捕获的 panel(闭包可能拿到 null),改用当前 ref。
|
|
908
|
+
const current = panelRef.current
|
|
909
|
+
const active = document.activeElement
|
|
910
|
+
if (active && active !== document.body && typeof current?.contains === 'function' && !current.contains(active)) return
|
|
911
|
+
previous.focus()
|
|
912
|
+
}
|
|
913
|
+
}, [state.open, phase])
|
|
914
|
+
|
|
478
915
|
if (phase === 'closed') return null
|
|
479
916
|
|
|
480
917
|
const st = state.status
|
|
918
|
+
// 未配置时必须直接落在设置视图;其余情况由 store.view 决定。
|
|
919
|
+
const settingsView = state.view === 'settings' || st?.mode === 'needs_config'
|
|
481
920
|
|
|
482
921
|
return h(
|
|
483
922
|
'div',
|
|
484
923
|
{
|
|
485
924
|
ref: panelRef,
|
|
486
925
|
className: phase === 'closing' ? 'oc-panel-exit' : 'oc-panel-enter',
|
|
926
|
+
// 非模态对话框:允许点击外部关闭,因此不声明 aria-modal(WAI-ARIA 要求 aria-modal=true 时外部内容不可交互)。
|
|
927
|
+
role: 'dialog',
|
|
928
|
+
'aria-label': settingsView ? 'OpenCode GO 连接配置' : 'OpenCode GO 用量',
|
|
929
|
+
tabIndex: -1,
|
|
487
930
|
style: {
|
|
488
931
|
position: 'fixed',
|
|
489
932
|
left: 12,
|
|
490
933
|
bottom: 52,
|
|
491
|
-
|
|
934
|
+
// 窄视口(<384px)不再右侧溢出:宽度随视口收缩,border-box 让 1px 边框不额外撑宽。
|
|
935
|
+
width: 'min(360px, calc(100vw - 24px))',
|
|
936
|
+
maxWidth: 'calc(100vw - 24px)',
|
|
937
|
+
boxSizing: 'border-box',
|
|
938
|
+
maxHeight: 'calc(100vh - 80px)',
|
|
939
|
+
overflow: 'hidden',
|
|
940
|
+
display: 'flex',
|
|
941
|
+
flexDirection: 'column',
|
|
492
942
|
background: T.bg,
|
|
493
943
|
border: `1px solid ${T.border}`,
|
|
494
944
|
borderRadius: 16,
|
|
495
945
|
boxShadow: 'var(--dsw-shadow-lv3, 0 10px 32px rgba(0,0,0,0.4))',
|
|
496
|
-
padding: '14px 16px 12px',
|
|
497
946
|
zIndex: 60,
|
|
498
947
|
pointerEvents: phase === 'closing' ? 'none' : 'auto',
|
|
499
948
|
},
|
|
500
949
|
},
|
|
501
950
|
[
|
|
502
|
-
//
|
|
503
|
-
h('div', { key: 'hd', style: { display: 'flex', alignItems: 'center',
|
|
504
|
-
h('span', { style: { fontSize: 13, fontWeight: 600, color: T.label } }, '⚙ OpenCode GO 连接配置'),
|
|
505
|
-
|
|
506
|
-
h(
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
store.set({ refreshing: true })
|
|
526
|
-
await refreshStatus(true)
|
|
527
|
-
store.set({ refreshing: false })
|
|
528
|
-
},
|
|
529
|
-
title: '强制刷新同步',
|
|
530
|
-
},
|
|
531
|
-
state.refreshing ? h('span', { className: 'oc-spin' }, '↻') : '↻',
|
|
532
|
-
),
|
|
533
|
-
h(
|
|
534
|
-
'button',
|
|
535
|
-
{
|
|
536
|
-
className: 'oc-btn',
|
|
537
|
-
style: {
|
|
538
|
-
width: 26,
|
|
539
|
-
height: 26,
|
|
540
|
-
padding: 0,
|
|
541
|
-
display: 'inline-flex',
|
|
542
|
-
alignItems: 'center',
|
|
543
|
-
justifyContent: 'center',
|
|
544
|
-
fontSize: 13,
|
|
545
|
-
border: 'none',
|
|
546
|
-
borderRadius: 6,
|
|
547
|
-
cursor: 'pointer',
|
|
548
|
-
color: T.secondary,
|
|
549
|
-
background: 'transparent',
|
|
550
|
-
},
|
|
551
|
-
onClick: () => store.set({ open: false }),
|
|
552
|
-
title: '关闭',
|
|
553
|
-
},
|
|
554
|
-
'✕',
|
|
555
|
-
),
|
|
556
|
-
]),
|
|
557
|
-
]),
|
|
558
|
-
// 内容:直接展示配置与连接管理
|
|
559
|
-
h(ConfigCard),
|
|
951
|
+
// 固定头部:标题 + 关闭
|
|
952
|
+
h('div', { key: 'hd', style: { display: 'flex', alignItems: 'center', padding: '14px 16px 10px', borderBottom: `1px solid ${T.border}`, flexShrink: 0 } }, [
|
|
953
|
+
h('span', { key: 't', style: { fontSize: 13, fontWeight: 600, color: T.label } }, settingsView ? '⚙ OpenCode GO 连接配置' : 'OpenCode GO 用量'),
|
|
954
|
+
!settingsView && st?.config?.baseUrl
|
|
955
|
+
? h('span', { key: 'h', style: { fontSize: 10, color: T.secondary, marginLeft: 8, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, hostOf(st.config.baseUrl))
|
|
956
|
+
: null,
|
|
957
|
+
h(
|
|
958
|
+
'button',
|
|
959
|
+
{
|
|
960
|
+
key: 'x',
|
|
961
|
+
className: 'oc-btn',
|
|
962
|
+
style: { ...btnBase, marginLeft: 'auto', border: 'none', padding: '2px 6px' },
|
|
963
|
+
onClick: closeOverlay,
|
|
964
|
+
title: '关闭',
|
|
965
|
+
'aria-label': '关闭面板',
|
|
966
|
+
},
|
|
967
|
+
'✕',
|
|
968
|
+
),
|
|
969
|
+
].filter(Boolean)),
|
|
970
|
+
// 滚动内容区
|
|
971
|
+
h('div', { key: 'body', style: { flex: 1, minHeight: 0, overflowY: 'auto', padding: '12px 16px' } }, settingsView ? h(ConfigCard) : h(StatusView)),
|
|
972
|
+
// 固定底部操作条:刷新 / 设置 / 上次同步
|
|
973
|
+
h(OpsBar, { key: 'ops', settings: settingsView }),
|
|
560
974
|
],
|
|
561
975
|
)
|
|
562
976
|
}
|
package/src/index.js
CHANGED
|
@@ -53,6 +53,19 @@ function isJsonContentType(value) {
|
|
|
53
53
|
return typeof value === 'string' && /^application\/json\s*(?:;|$)/i.test(value.trim())
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/** 百分比取整并钳制到 0–100:上游给小数或越界值时,界面不会出现 12.34% 或 120%。 */
|
|
57
|
+
function normalizePct(value) {
|
|
58
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return null
|
|
59
|
+
return Math.round(Math.min(100, Math.max(0, value)))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 从上游「已用百分比」派生互斥整数对:先取整 used,再 remaining = 100 - used,保证两者之和恒为 100。 */
|
|
63
|
+
function pctPair(percent) {
|
|
64
|
+
const usedPct = normalizePct(percent)
|
|
65
|
+
if (usedPct === null) return { usedPct: null, remainingPct: null }
|
|
66
|
+
return { usedPct, remainingPct: 100 - usedPct }
|
|
67
|
+
}
|
|
68
|
+
|
|
56
69
|
/** 读取请求头;Node 的 headers 是名字归一化后的普通对象。 */
|
|
57
70
|
function headerValue(headers, name) {
|
|
58
71
|
const value = headers?.[name]
|
|
@@ -154,6 +167,8 @@ async function fetchUsageFromGateway(baseUrl, apiKey) {
|
|
|
154
167
|
export function apply(ctx, config = {}) {
|
|
155
168
|
/** @type {{ at: number, baseUrl: string, keyRef: string, data: any } | null} */
|
|
156
169
|
let usageCache = null
|
|
170
|
+
// 最后一次成功拉取的时间:失败分支沿用它,避免「上次同步」在拉取失败时反而前移。
|
|
171
|
+
let lastSuccessAt = null
|
|
157
172
|
|
|
158
173
|
function invalidateCache() {
|
|
159
174
|
usageCache = null
|
|
@@ -265,8 +280,7 @@ export function apply(ctx, config = {}) {
|
|
|
265
280
|
id: 'rolling',
|
|
266
281
|
label: '滚动窗口 (5h)',
|
|
267
282
|
shortLabel: '5h',
|
|
268
|
-
|
|
269
|
-
remainingPct: typeof u.rolling?.percent === 'number' ? Math.max(0, 100 - u.rolling.percent) : null,
|
|
283
|
+
...pctPair(u.rolling?.percent),
|
|
270
284
|
status: u.rolling?.status ?? 'ok',
|
|
271
285
|
resetAt: u.rolling?.resetsAt ? Date.parse(u.rolling.resetsAt) : null,
|
|
272
286
|
},
|
|
@@ -274,8 +288,7 @@ export function apply(ctx, config = {}) {
|
|
|
274
288
|
id: 'weekly',
|
|
275
289
|
label: '周窗口',
|
|
276
290
|
shortLabel: 'Weekly',
|
|
277
|
-
|
|
278
|
-
remainingPct: typeof u.weekly?.percent === 'number' ? Math.max(0, 100 - u.weekly.percent) : null,
|
|
291
|
+
...pctPair(u.weekly?.percent),
|
|
279
292
|
status: u.weekly?.status ?? 'ok',
|
|
280
293
|
resetAt: u.weekly?.resetsAt ? Date.parse(u.weekly.resetsAt) : null,
|
|
281
294
|
},
|
|
@@ -283,8 +296,7 @@ export function apply(ctx, config = {}) {
|
|
|
283
296
|
id: 'monthly',
|
|
284
297
|
label: '月窗口',
|
|
285
298
|
shortLabel: 'Monthly',
|
|
286
|
-
|
|
287
|
-
remainingPct: typeof u.monthly?.percent === 'number' ? Math.max(0, 100 - u.monthly.percent) : null,
|
|
299
|
+
...pctPair(u.monthly?.percent),
|
|
288
300
|
status: u.monthly?.status ?? 'ok',
|
|
289
301
|
resetAt: u.monthly?.resetsAt ? Date.parse(u.monthly.resetsAt) : null,
|
|
290
302
|
},
|
|
@@ -295,17 +307,19 @@ export function apply(ctx, config = {}) {
|
|
|
295
307
|
mode: 'ready',
|
|
296
308
|
fetchedAt: now,
|
|
297
309
|
config: { baseUrl, hasKey: true },
|
|
298
|
-
monthlyUsedPct: typeof u.monthly?.percent === 'number' ? u.monthly.percent : null,
|
|
310
|
+
monthlyUsedPct: typeof u.monthly?.percent === 'number' ? normalizePct(u.monthly.percent) : null,
|
|
299
311
|
windows,
|
|
300
312
|
error: null,
|
|
301
313
|
}
|
|
302
314
|
usageCache = { at: now, baseUrl, keyRef, data: result }
|
|
315
|
+
lastSuccessAt = now
|
|
303
316
|
return result
|
|
304
317
|
} catch (error) {
|
|
305
318
|
return {
|
|
306
319
|
ok: false,
|
|
307
320
|
mode: 'ready',
|
|
308
|
-
|
|
321
|
+
// 拉取失败不前移「上次同步」:沿用最后一次成功的时间;从未成功过则为 null,界面改说「同步失败」。
|
|
322
|
+
fetchedAt: lastSuccessAt,
|
|
309
323
|
config: { baseUrl, hasKey: true },
|
|
310
324
|
windows: [],
|
|
311
325
|
error: { code: 'GATEWAY_ERROR', message: String(error?.message ?? error) },
|