@angelyeye/dsh-cost-tracker 1.8.12 → 1.8.13

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 CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  本文件用中文记录 dsh-cost-tracker 的版本变更。
4
4
 
5
+ ## v1.8.13(2026-09-25)
6
+
7
+ **新增:可以关掉插件在前端的显示(三个落点各自独立)**
8
+
9
+ - **背景**:插件的几个前端落点此前都是常驻的、没有开关 —— 输入框上方的「本会话花费」胶囊、侧边栏底部的峰谷时段条、设置页的「花费统计」看板。不想看的人只能忍,或者干脆卸载插件(连带失去记账与云端同步)。
10
+ - **现在**:设置 → 插件 → 插件配置 → 花费统计 → **界面显示**,三个开关**各自独立**、**勾选即刻生效**(不需要点上面的「保存」,也不需要重启 dsh):
11
+ | 开关 | 配置键 | 落点 |
12
+ | --- | --- | --- |
13
+ | 输入框上方的花费胶囊 | `uiDockEnabled` | `conversation.composer.dock` |
14
+ | 侧边栏峰谷时段条 | `uiPeakEnabled` | `sidebar.footer.action` |
15
+ | 设置页「花费统计」看板 | `uiDashboardEnabled` | `settings.section` |
16
+ 另有一键「全部显示」恢复默认。
17
+ - **只影响渲染**:关掉之后记账、落盘、云端同步与 Agent 工具(`cost_stats` 等)全部照常工作;关掉看板时,设置项仍在,页面里给出一句「如何重新打开」的提示,而不是留一个点不开的空白导航项。
18
+ - **插件配置卡片本身不受这三个开关控制** —— 否则关掉看板之后就再没有入口能打开了。
19
+ - **安全默认**:显隐的真值与 `peakEnabled` 相反,**只有显式 `false` 才隐藏**,缺省/非布尔一律按「可见」处理。老配置文件里没有这几个键,升级后界面与升级前逐像素一致。
20
+ - **实现要点**:
21
+ - 显隐在**渲染期**判定(`uiOn(ui, key)` = `ui[key] !== false`),不是注册期 —— 宿主不会重启插件、插槽注册也无法撤销,只有在渲染期判定才能做到「改完立刻生效」。
22
+ - 配置卡片保存后广播 `dsh-cost-tracker-ui` 事件,已挂载的胶囊 / 时段条 / 看板立刻跟手;另有 30 秒轮询兜底。
23
+ - 新增宿主路由 `ui-config`(写)与 `peak`/`sync` 快照里的 `ui` 字段(读),三个开关与峰谷、云端字段**同存一份配置文件**(`~/.dsh/storages/cost-tracker-config.json`),互不覆盖。
24
+ - 侧边栏时段条关闭时,峰谷切换弹窗与系统通知一并停用(同属一个落点);只想留提醒不要时段条,用「峰谷计价与提示 → 峰时高价时段显著提示」。
25
+ - **测试**:新增 `test/ui-display.test.js`(23 条断言,四层:宿主 HTTP 面读写、落盘不覆盖其它字段、重启后读回、客户端三处判定);`test/client-render.test.js` 新增 [8][9] 两节(关掉即渲染为空、重新打开即恢复、卡片三个开关各自提交自己的键、client.js 与 config.js 的开关清单逐项对齐);`test/config.test.js` 新增界面开关一节(默认可见 / 老配置可见 / 非布尔回退)。全量测试通过。
26
+ - **顺带修掉一处假红**:`cloud-view-e2e.test.js` 的「热力图不重不漏」用例在**本地兄弟目录 dsh-cost-cloud 还是旧版**(< v1.3.2,`byDay` 无 token 类型拆分)时会硬失败 —— 那与「跨仓库依赖检测不到就跳过」的既定原则不一致。现改为探测到旧云端就明确 skip 并提示升级兄弟目录,不再把一个环境问题伪装成发布闸门红灯(线上真连旧云端时热力图本就会退回本机并在卡片标注口径,属已声明的降级)。
27
+
5
28
  ## v1.8.12(2026-09-19)
6
29
 
7
30
  **修复:①「Token 用量统计」热力图不跟随三态视图(本机+云端下屏内自相矛盾)② `cost_stats scope=cloud|both` 崩溃**
package/README.en.md CHANGED
@@ -213,6 +213,21 @@ The dimension selector adds per-device / per-agent / per-model breakdowns and a
213
213
 
214
214
  **Privacy**: only token counts, cost, timestamps and identifiers are uploaded — never prompts, responses, file paths or code. The config card offers "mask session id" (irreversible hash) and "omit purpose".
215
215
 
216
+ ### Interface visibility (v1.8.12, optional)
217
+
218
+ Don't want the plugin in your face? **Settings → Plugins → Plugin configuration → Cost Tracker → Interface visibility** has three independent switches that apply **the moment you tick them** (no Save, no `dsh` restart):
219
+
220
+ | Switch | What turning it off does |
221
+ | --- | --- |
222
+ | Composer cost pill | the per-session cost / model breakdown above the chat input disappears |
223
+ | Sidebar peak bar | the phase / countdown strip at the bottom of the sidebar disappears (the peak-switch popup and web notifications go with it; if you only want the reminder without the strip, use the notice switch under "Peak/off-peak pricing & notices" instead) |
224
+ | Settings dashboard | the "Cost Tracker" entry disappears from the Settings nav (accounting and cloud sync keep running; the page then shows a one-line hint on how to bring it back) |
225
+
226
+ - **Display only**: accounting, persistence, cloud sync and the agent tools (`cost_stats` etc.) are unaffected;
227
+ - **The plugin configuration card itself is never hidden by these switches** — otherwise you would have no way to turn them back on;
228
+ - Truth value is "hidden only on an explicit `false`", so a missing key means visible: **an existing config file looks exactly the same after upgrading**;
229
+ - All three keys live in the same `~/.dsh/storages/cost-tracker-config.json` (`uiDockEnabled` / `uiPeakEnabled` / `uiDashboardEnabled`) and never overwrite the peak/cloud fields.
230
+
216
231
  ### HTTP API (for other tools)
217
232
 
218
233
  All endpoints are `POST` + JSON and listen on the loopback address:
@@ -221,9 +236,10 @@ All endpoints are `POST` + JSON and listen on the loopback address:
221
236
  POST /api/cost-tracker/summary Overview
222
237
  POST /api/cost-tracker/dashboard Dashboard data
223
238
  POST /api/cost-tracker/usage Usage heatmap (all-time totals + daily token aggregation)
224
- POST /api/cost-tracker/peak Peak-phase snapshot (current tier / next switch / config)
239
+ POST /api/cost-tracker/peak Peak-phase snapshot (current tier / next switch / config / interface visibility)
225
240
  POST /api/cost-tracker/peak-config Save peak-price notice config
226
- POST /api/cost-tracker/sync Cloud sync status (device id / watermark / pending / last error)
241
+ POST /api/cost-tracker/ui-config Save interface-visibility switches (pill / sidebar bar / dashboard; partial patches OK)
242
+ POST /api/cost-tracker/sync Cloud sync status (device id / watermark / pending / last error / interface visibility)
227
243
  POST /api/cost-tracker/sync-now Sync now ({"full":true} to re-send everything)
228
244
  POST /api/cost-tracker/sync-test Test the cloud connection
229
245
  POST /api/cost-tracker/sync-config Save cloud sync config
@@ -243,6 +259,14 @@ Example: `curl -X POST http://127.0.0.1:3080/api/cost-tracker/summary -d '{}'`
243
259
 
244
260
  > Highlights only — the full version-by-version history lives in [`CHANGELOG.md`](./CHANGELOG.md) (Chinese).
245
261
 
262
+ ### v1.8.13 (2026-09-25)
263
+
264
+ **Added: the plugin's front-end surfaces can now be turned off (three independent switches)**
265
+
266
+ - **Settings → Plugins → Plugin configuration → Cost Tracker → Interface visibility**: the composer cost pill, the sidebar peak bar and the Settings "Cost Tracker" dashboard can each be switched off on their own, applied **immediately** (no Save, no `dsh` restart), plus a one-click "Show all".
267
+ - **Display only**: accounting, cloud sync and the agent tools keep working; the plugin configuration card itself is never hidden (otherwise there would be no way back).
268
+ - **Safe default**: hidden only on an explicit `false`, so missing keys mean visible — an existing config file looks identical after upgrading. See the "Interface visibility" section above.
269
+
246
270
  ### v1.8.12 (2026-09-19)
247
271
 
248
272
  **Fixed: the heatmap ignored the three-state switch (self-contradicting screen in "local + cloud"), and `cost_stats scope=cloud|both` crashed**
package/README.md CHANGED
@@ -41,6 +41,7 @@
41
41
  | 💳 | **余额查询** | 一键查询 DeepSeek 官方账户余额(总余额 / 充值 / 赠送 / 状态) |
42
42
  | 🤖 | **Agent 工具** | 直接在对话里问:"我现在花了多少钱?"——Agent 会调用 `cost_stats` / `cost_prices` 等工具回答 |
43
43
  | 🔻 | **状态栏** | 聊天输入框下方实时显示:**本会话花费**(胶囊分段:本会话 / 订阅套餐 / 分模型),按会话实际用到的模型与订阅拆分,**多模型默认折叠只显示 top2,点击展开全部明细**;订阅显示具体套餐名,不再展示配额与调用次数 |
44
+ | 👁️ | **界面显示开关** | 输入框上方的花费胶囊、侧边栏峰谷时段条、设置页「花费统计」看板**三个落点各自可关**(设置 → 插件 → 插件配置 → 花费统计 → 界面显示),勾选即刻生效;**只影响显示**,记账、云端同步与 Agent 工具照常,配置卡片本身不受开关控制 |
44
45
  | 💾 | **本地持久化** | 数据存本机 `~/.dsh/storages/cost-tracker-records.json`,重启不丢、不上传;**明细保留最近 180 天,更早自动压缩为永久日汇总,全时段统计永远精确且内存/磁盘有界** |
45
46
  | 📤 | **CSV 导出** | 一键导出明细 + 日汇总(purpose=rollup),方便用 Excel / Numbers 做进一步分析 |
46
47
 
@@ -213,6 +214,21 @@ docker compose up -d # 打开 http://<服务器>:8787 ,在「设置」页
213
214
 
214
215
  **隐私**:只上报 token 数量、费用、时间戳与标识符(不涉及 prompt / 回复 / 文件内容);可在配置卡开启「会话脱敏」(`sessionId` 上报前替换为不可逆哈希)与「不含 purpose」。
215
216
 
217
+ ### 界面显示(v1.8.12,可选)
218
+
219
+ 不想在界面上看到插件?**设置 → 插件 → 插件配置 → 花费统计 → 界面显示**里三个开关**各自独立**、**勾选即刻生效**(无需点「保存」,也无需重启 dsh):
220
+
221
+ | 开关 | 关掉之后 |
222
+ | --- | --- |
223
+ | 输入框上方的花费胶囊 | 会话输入区不再显示本会话花费与模型明细 |
224
+ | 侧边栏峰谷时段条 | 侧边栏底部的档位 / 倒计时消失(峰谷切换弹窗与系统通知一并停用;只想留提醒不想要时段条,请改用「峰谷计价与提示」里的提示开关) |
225
+ | 设置页「花费统计」看板 | 设置页左侧不再出现该入口(记账与云端同步照常;关掉后该页只留一句重新打开的指引) |
226
+
227
+ - **只影响显示**:记账、落盘、云端同步与 Agent 工具(`cost_stats` 等)全部照常工作;
228
+ - **插件配置卡片本身不受这三个开关控制** —— 否则关掉之后就没有入口能再打开了;
229
+ - 取值为「只有显式 `false` 才隐藏」,缺省即显示。老配置文件里没有这几个键,**升级后界面与升级前一致**;
230
+ - 落盘在同一份 `~/.dsh/storages/cost-tracker-config.json`(键名 `uiDockEnabled` / `uiPeakEnabled` / `uiDashboardEnabled`),与峰谷、云端字段互不覆盖。
231
+
216
232
  ### HTTP API(供其他工具调用)
217
233
 
218
234
  全部为 `POST` + JSON,监听本机地址:
@@ -221,9 +237,10 @@ docker compose up -d # 打开 http://<服务器>:8787 ,在「设置」页
221
237
  POST /api/cost-tracker/summary 概览(含本会话按模型拆分 / 订阅)
222
238
  POST /api/cost-tracker/dashboard 仪表盘数据
223
239
  POST /api/cost-tracker/usage 用量热力图(全时段累计 + 按天 token 聚合)
224
- POST /api/cost-tracker/peak 峰谷相位快照(当前档位/下次切换/配置)
240
+ POST /api/cost-tracker/peak 峰谷相位快照(当前档位/下次切换/配置/界面显示开关)
225
241
  POST /api/cost-tracker/peak-config 保存峰谷计价提示配置
226
- POST /api/cost-tracker/sync 云端同步状态(设备 ID/水位/待上报/最近错误)
242
+ POST /api/cost-tracker/ui-config 保存界面显示开关(胶囊/时段条/看板,可只传要改的键)
243
+ POST /api/cost-tracker/sync 云端同步状态(设备 ID/水位/待上报/最近错误/界面显示开关)
227
244
  POST /api/cost-tracker/sync-now 立即同步(传 {"full":true} 全量补传)
228
245
  POST /api/cost-tracker/sync-test 测试云端连接
229
246
  POST /api/cost-tracker/sync-config 保存云端同步配置
@@ -243,6 +260,14 @@ POST /api/cost-tracker/export 导出 CSV
243
260
 
244
261
  > 这里只列重要版本;逐版完整记录见 [`CHANGELOG.md`](./CHANGELOG.md)。
245
262
 
263
+ ### v1.8.13(2026-09-25)
264
+
265
+ **新增:可以关掉插件在前端的显示(三个落点各自独立)**
266
+
267
+ - 设置 → 插件 → 插件配置 → 花费统计 → **界面显示**:输入框上方的花费胶囊、侧边栏峰谷时段条、设置页「花费统计」看板三个落点**各自可关**,勾选即刻生效(无需点「保存」、无需重启 dsh),另有「全部显示」一键恢复。
268
+ - **只影响显示**:记账、云端同步与 Agent 工具照常;配置卡片本身不受这三个开关控制(否则关掉就没入口再打开)。
269
+ - **安全默认**:只有显式 `false` 才隐藏,缺省即显示 —— 老配置升级后界面与升级前一致。详见上方「界面显示」一节。
270
+
246
271
  ### v1.8.12(2026-09-19)
247
272
 
248
273
  **修复:热力图不跟随三态视图(本机+云端下屏内自相矛盾)+ `cost_stats scope=cloud|both` 崩溃**
package/client.js CHANGED
@@ -58,6 +58,40 @@ window.__ModuleLoader__.load({
58
58
  }
59
59
  const MEMBERSHIP = { LEVEL_FREE: "免费版", LEVEL_BASIC: "基础版", LEVEL_INTERMEDIATE: "进阶版", LEVEL_ADVANCED: "高级版" };
60
60
 
61
+ // ---------- 前端显隐开关(服务端 ui-config,见 config.js 的 UI_SURFACES) ----------
62
+ // 配置改动后由配置卡片广播,已挂载的部件立刻响应,不必等下一次轮询。
63
+ const UI_EVENT = "dsh-cost-tracker-ui";
64
+ /**
65
+ * 取显隐标志:只有显式 false 才算关闭。
66
+ * snap 还没加载完 / 老服务端不返回该字段时按「可见」处理 —— 否则界面会先闪一下再出现。
67
+ */
68
+ function uiOn(ui, key) {
69
+ return !(ui && ui[key] === false);
70
+ }
71
+ /**
72
+ * 「界面显示」分组的开关清单。
73
+ * 说明书(label/desc)在浏览器里必须有本地副本:配置卡片渲染时不该为了拿一段
74
+ * 文案再往返一次服务端。key 与 config.js 的 UI_SURFACES 必须逐字一致 ——
75
+ * test/client-render.test.js 的 [8] 会把两份清单对起来,漂移即报红。
76
+ */
77
+ const UI_SURFACES = [
78
+ {
79
+ key: "uiDockEnabled",
80
+ label: "输入框上方的花费胶囊",
81
+ desc: "本会话花费与模型明细(会话输入区上方)。关闭后输入区不再显示任何花费信息。",
82
+ },
83
+ {
84
+ key: "uiPeakEnabled",
85
+ label: "侧边栏峰谷时段条",
86
+ desc: "侧边栏底部的当前档位 / 倒计时。关闭后峰谷切换弹窗与系统通知一并停用;只想留提醒不想要时段条时,请改用「峰谷计价与提示」里的提示开关。",
87
+ },
88
+ {
89
+ key: "uiDashboardEnabled",
90
+ label: "设置页「花费统计」看板",
91
+ desc: "设置页左侧导航的花费统计入口与看板。关闭后该入口隐藏,插件仍照常记账并同步云端。",
92
+ },
93
+ ];
94
+
61
95
  // ---------- styles ----------
62
96
  function applyStyles(ctx) {
63
97
  const css = `
@@ -1296,6 +1330,10 @@ window.__ModuleLoader__.load({
1296
1330
  // 预览跟随用户配置的弹窗位置(右下角 / 屏幕中心),不强制覆盖
1297
1331
  popup = e(PeakAlertPopup, { snap, preview, now: now + 120000, countdownText: "2 分", onDismiss: () => setPreview(null) });
1298
1332
  }
1333
+ // 显隐开关(在所有 hook 之后判定,避免条件式 hook 顺序漂移):
1334
+ // 时段条、切换弹窗与系统通知同属这一个落点,关闭即整体停用;
1335
+ // 想「只留提醒、不要时段条」请关掉「峰谷计价与提示 → 峰时高价时段显著提示」。
1336
+ if (!uiOn(snap && snap.ui, "uiPeakEnabled")) return null;
1299
1337
  return e("div", null,
1300
1338
  e(PeakStrip, { snap, style: snap ? snap.style : "compact", wide, now, ringSize: 112 }),
1301
1339
  popup);
@@ -1443,6 +1481,9 @@ window.__ModuleLoader__.load({
1443
1481
  const [msg, setMsg] = useState("");
1444
1482
  const [busy, setBusy] = useState(false);
1445
1483
  const [testing, setTesting] = useState("");
1484
+ // 界面显示三开关(独立于云端同步草稿:勾选即刻生效并落盘)
1485
+ const [uiDraft, setUiDraft] = useState({ uiDockEnabled: true, uiPeakEnabled: true, uiDashboardEnabled: true });
1486
+ const [uiMsg, setUiMsg] = useState("");
1446
1487
  // 折叠态:与宿主「插件配置」里其它卡片一致——默认收起,点标题展开
1447
1488
  const [open, setOpen] = useState(false);
1448
1489
  function load() {
@@ -1459,6 +1500,12 @@ window.__ModuleLoader__.load({
1459
1500
  includePurpose: v.includePurpose !== false,
1460
1501
  cloudView: v.view || "local",
1461
1502
  });
1503
+ // 界面显示:缺省视为可见(与 normalizeUiConfig 同一真值语义)
1504
+ setUiDraft({
1505
+ uiDockEnabled: v.uiDockEnabled !== false,
1506
+ uiPeakEnabled: v.uiPeakEnabled !== false,
1507
+ uiDashboardEnabled: v.uiDashboardEnabled !== false,
1508
+ });
1462
1509
  }).catch(() => {});
1463
1510
  }
1464
1511
  useEffect(() => { load(); }, []);
@@ -1494,6 +1541,32 @@ window.__ModuleLoader__.load({
1494
1541
  load();
1495
1542
  }).catch(err => { setBusy(false); setMsg("同步失败:" + String(err && err.message ? err.message : err)); });
1496
1543
  }
1544
+ /**
1545
+ * 界面显示开关:勾选即刻提交并落盘(无需点「保存」),随后广播 UI_EVENT,
1546
+ * 让已经挂载的胶囊 / 时段条 / 看板立刻跟着显隐。
1547
+ * 关掉看板后本卡片仍在原位(它不受这三个开关控制),随时能改回来。
1548
+ */
1549
+ function saveUi(patch) {
1550
+ const next = Object.assign({}, uiDraft, patch);
1551
+ setUiDraft(next);
1552
+ setUiMsg("保存中…");
1553
+ apiCall("ui-config", patch).then(v => {
1554
+ if (v && v.ok) {
1555
+ setUiDraft({
1556
+ uiDockEnabled: v.uiDockEnabled !== false,
1557
+ uiPeakEnabled: v.uiPeakEnabled !== false,
1558
+ uiDashboardEnabled: v.uiDashboardEnabled !== false,
1559
+ });
1560
+ setUiMsg("已保存,界面立即生效");
1561
+ try { window.dispatchEvent(new CustomEvent(UI_EVENT)); } catch (_) {}
1562
+ } else {
1563
+ setUiMsg("保存失败:" + ((v && v.error) || "未知错误"));
1564
+ }
1565
+ setTimeout(() => setUiMsg(""), 2500);
1566
+ }).catch(err => {
1567
+ setUiMsg("保存失败:" + String(err && err.message ? err.message : err));
1568
+ });
1569
+ }
1497
1570
  // 卡片外壳对齐宿主 PluginCard:li.cost-pcard > button.cost-pcard-head(标题+副标题+箭头)> body
1498
1571
  const head = e("button", { type: "button", className: "cost-pcard-head", "aria-expanded": open, onClick: () => setOpen(v => !v) },
1499
1572
  e("span", { className: "cost-pcard-headtext" },
@@ -1537,12 +1610,60 @@ window.__ModuleLoader__.load({
1537
1610
  "设备 ID " + (st.deviceId || "(未生成)") + " · 水位 seq=" + st.watermark + " · 待上报 " + st.pending + " 条 · 上次同步 " + (st.lastSyncAt ? timeLabel(st.lastSyncAt) : "从未")),
1538
1611
  st.lastError ? e("div", { className: "cost-err" }, "最近错误:" + st.lastError) : null,
1539
1612
  st.needAuth ? e("div", { className: "cost-err" }, "令牌无效:请在云端看板重新生成共享引导令牌后填入上方「共享令牌」。") : null,
1540
- st.url ? e("div", { className: "cost-hint", style: { marginTop: "6px" } }, "云端看板:" + st.url) : null);
1613
+ st.url ? e("div", { className: "cost-hint", style: { marginTop: "6px" } }, "云端看板:" + st.url) : null,
1614
+ // ---------- 界面显示:三个前端落点各自显隐,勾选即刻生效 ----------
1615
+ // 只影响渲染,不影响记账 / 云端同步 / Agent 工具;本卡片不受这三个开关控制
1616
+ // (否则关掉之后就再没有入口能打开了)。
1617
+ e("div", { className: "cost-sync-card" },
1618
+ e("div", { className: "cost-panel-title" }, "界面显示"),
1619
+ e("div", { className: "cost-hint", style: { marginTop: "6px" } },
1620
+ "控制插件在 DSH 界面上的落点,勾选后立即生效(无需点上面的「保存」)。关闭只影响显示:记账、云端同步与 Agent 工具照常工作。"),
1621
+ e("div", { style: { marginTop: "8px", display: "grid", gap: "6px" } },
1622
+ UI_SURFACES.map(s => e("label", { key: s.key, className: "cost-row", style: { gap: "8px", alignItems: "flex-start" } },
1623
+ e("input", {
1624
+ type: "checkbox",
1625
+ checked: uiDraft[s.key] !== false,
1626
+ onChange: ev => saveUi({ [s.key]: ev.target.checked }),
1627
+ }),
1628
+ e("span", null,
1629
+ e("span", null, s.label),
1630
+ e("div", { className: "cost-hint" }, s.desc))))),
1631
+ e("div", { className: "cost-row", style: { gap: "8px", marginTop: "6px" } },
1632
+ e("button", { className: "cost-btn", onClick: () => saveUi({ uiDockEnabled: true, uiPeakEnabled: true, uiDashboardEnabled: true }) }, "全部显示"),
1633
+ uiMsg ? e("span", { className: "cost-hint" }, uiMsg) : null)));
1541
1634
  return e("li", { className: "cost-pcard" + (open ? " is-open" : "") },
1542
1635
  head,
1543
1636
  open ? e("div", { className: "cost-pcard-body" }, body) : null);
1544
1637
  }
1545
1638
 
1639
+ /**
1640
+ * 「花费统计」看板的插槽门卫。
1641
+ * 关掉界面显示时**不能注册成空渲染**(会留下一个点不开的空白导航项),
1642
+ * 所以这里保留设置项但只渲染一句说明 —— 用户随时能在插件配置卡片里改回来。
1643
+ * 宿主不重启插件、插槽注册也无法撤销,因此显隐必须在渲染期判定。
1644
+ */
1645
+ function DashGate() {
1646
+ const [ui, setUi] = useState(null);
1647
+ useEffect(() => {
1648
+ let alive = true;
1649
+ function load() {
1650
+ apiCall("peak", {}).then(v => { if (alive && v && v.ok) setUi(v.ui || {}); }).catch(() => {});
1651
+ }
1652
+ load();
1653
+ const id = setInterval(load, 30000);
1654
+ window.addEventListener(UI_EVENT, load);
1655
+ return () => { alive = false; clearInterval(id); window.removeEventListener(UI_EVENT, load); };
1656
+ }, []);
1657
+ if (!uiOn(ui, "uiDashboardEnabled")) {
1658
+ return e("div", { className: "cost-wrap" },
1659
+ e("div", { className: "cost-h1" }, pluginIcon(18), "花费统计"),
1660
+ e("div", { className: "cost-hint", style: { marginTop: "8px" } },
1661
+ "花费统计看板已在设置里关闭显示。记账与云端同步不受影响;要重新打开:" +
1662
+ "设置 → 插件 → 插件配置 → 花费统计 → 界面显示 → 打开「设置页花费统计看板」。"));
1663
+ }
1664
+ return e(Dashboard, {});
1665
+ }
1666
+
1546
1667
  function Dashboard() {
1547
1668
  const [days, setDays] = useState(7);
1548
1669
  const [dash, setDash] = useState(null);
@@ -1802,7 +1923,12 @@ window.__ModuleLoader__.load({
1802
1923
  e("span", { className: "cost-matrix-num" }, "¥" + fmtMoney((matrix.totals || {}).cost || 0))))));
1803
1924
  }
1804
1925
 
1805
- function StatusLine(props) {
1926
+ /**
1927
+ * 输入框上方的花费胶囊(conversation.composer.dock)。
1928
+ * 显隐只在本部件内部判定:StatusLine 始终注册,快照说关就渲染 null ——
1929
+ * 用户改配置后无需重载页面(宿主不重启插件,插槽注册也无法撤销)。
1930
+ */
1931
+ function CostDock(props) {
1806
1932
  const sessionId = props && props.sessionId ? String(props.sessionId) : "";
1807
1933
  const [s, setS] = useState(null);
1808
1934
  const [expanded, setExpanded] = useState(false);
@@ -1813,9 +1939,12 @@ window.__ModuleLoader__.load({
1813
1939
  }
1814
1940
  load();
1815
1941
  const id = setInterval(load, 30000);
1816
- return () => { alive = false; clearInterval(id); };
1942
+ const onUi = () => load();
1943
+ window.addEventListener(UI_EVENT, onUi);
1944
+ return () => { alive = false; clearInterval(id); window.removeEventListener(UI_EVENT, onUi); };
1817
1945
  }, [sessionId]);
1818
1946
  if (!s) return null;
1947
+ if (!uiOn(s.ui, "uiDockEnabled")) return null;
1819
1948
  // 按会话实际内容决定显示(而不是按当前选中的模型):
1820
1949
  // - realModels 按量模型 / subModels 订阅模型(分开)
1821
1950
  // - 订阅只用一个着色徽标展示套餐名 + 总等效费用,订阅模型不再单独进模型区(避免重复)
@@ -1859,13 +1988,19 @@ window.__ModuleLoader__.load({
1859
1988
  }
1860
1989
  }
1861
1990
  // 分段组装:主胶囊 | 订阅胶囊 | 模型胶囊(细竖线分隔,换行时自动分段)
1991
+ // hasReal 为假时(本会话还没有任何按量花费)不渲染主胶囊,避免出现 ¥0.0000 的空壳
1862
1992
  const segs = [];
1863
- segs.push(costPill);
1993
+ if (hasReal || !hasSub) segs.push(costPill);
1864
1994
  if (subPill) segs.push(subPill);
1865
1995
  if (modelPill) segs.push(modelPill);
1866
1996
  return e("div", { className: "cost-dock" }, segs.map((seg, i) => e("span", { key: "seg" + i, className: "cost-seg" }, seg)));
1867
1997
  }
1868
1998
 
1999
+ /** 插槽入口:插槽本身始终注册,显隐在 CostDock 内判定 */
2000
+ function StatusLine(props) {
2001
+ return e(CostDock, props || {});
2002
+ }
2003
+
1869
2004
  const inject = ["slots"];
1870
2005
  function apply(ctx) {
1871
2006
  applyStyles(ctx);
@@ -1873,7 +2008,7 @@ window.__ModuleLoader__.load({
1873
2008
  if (slots === undefined) return;
1874
2009
  slots.inject("settings.section", () => slots.register(
1875
2010
  { name: "settings.section", id: "cost-dashboard", order: 30, label: "花费统计" },
1876
- () => e(Dashboard, {}),
2011
+ () => e(DashGate, {}),
1877
2012
  ));
1878
2013
  // 插件配置卡片(设置 → 插件 → 插件配置):以 settings 命名空间为键。
1879
2014
  // 这里必须**无条件** inject,不能拿 slots.entries(key).length 当"插槽是否存在"的探测:
package/config.js CHANGED
@@ -1,9 +1,10 @@
1
1
  // ============================================================
2
2
  // DSH 花费统计插件 —— 配置层(纯逻辑,可独立测试)
3
3
  //
4
- // 两部分配置同存于 $DSH_HOME/storages/cost-tracker-config.json:
4
+ // 三部分配置同存于 $DSH_HOME/storages/cost-tracker-config.json:
5
5
  // 1. 峰谷计价提示(peak*):默认值与参考项目 dsh-cost-meter 保持一致;
6
- // 2. 云端同步(cloud* / sync* / boardView):多机汇总与设备身份。
6
+ // 2. 云端同步(cloud* / sync* / boardView):多机汇总与设备身份;
7
+ // 3. 界面显示(ui*):前端各落点的显隐(本文件末尾有说明)。
7
8
  //
8
9
  // 字段(峰谷):
9
10
  // peakEnabled 启用 DeepSeek 峰谷时段价格(影响计费 + 提示显隐)
@@ -36,6 +37,19 @@
36
37
  // 于是「本机可见数据 + 云端结果」= 本机全部 + 其他整机 + 本机其它 agent(不重不漏)
37
38
  // cloudPanelDevices 云端视图的机器过滤(空数组 = 全部)
38
39
  // lastSyncAt/lastSyncOk/lastSyncError 最近一次同步结果(只读回显用)
40
+ //
41
+ // 字段(界面显示,v1.8.12):
42
+ // uiDockEnabled 输入框上方的「本会话花费」胶囊(conversation.composer.dock)
43
+ // uiPeakEnabled 侧边栏底部的峰谷时段条(sidebar.footer.action)
44
+ // uiDashboardEnabled 设置页左侧的「花费统计」看板入口(settings.section)
45
+ //
46
+ // 三者**互相独立**,且都只影响前端渲染,不影响记账、云端同步与 Agent 工具。
47
+ // 插件配置卡片(settings.plugin.item)不受这三个开关控制 —— 它必须始终可点,
48
+ // 否则关掉之后用户就没有入口再打开了。
49
+ //
50
+ // 真值语义与 peakEnabled 相反:null/undefined → true(显隐是从 v1.8.12 才有的新
51
+ // 能力,老配置文件里没有这几个键,必须保持默认全部可见),只有显式 false 才隐藏。
52
+ // 这样两个方向上都安全:老配置升级后界面不变,新配置关掉就是关掉。
39
53
  // ============================================================
40
54
 
41
55
  /** 默认峰谷计价生效时间(UTC;两档方案已即时生效,门控恒通过) */
@@ -84,6 +98,39 @@ export function defaultCloudConfig() {
84
98
  /** 看板三态视图取值 */
85
99
  export const BOARD_VIEWS = ['local', 'local+cloud', 'cloud']
86
100
 
101
+ /**
102
+ * 前端显隐开关(设置 → 插件 → 插件配置 → 花费统计 → 界面显示)。
103
+ * 每项声明:配置键、文案、说明,以及它在 DSH 里对应的插槽(便于对照宿主与测试)。
104
+ * 新增可控落点时只改这张表:服务端 normalize 与前端卡片都从它派生。
105
+ */
106
+ export const UI_SURFACES = [
107
+ {
108
+ key: 'uiDockEnabled',
109
+ label: '输入框上方的花费胶囊',
110
+ desc: '本会话花费与模型明细(会话输入区上方)。关闭后输入区不再显示任何花费信息。',
111
+ slot: 'conversation.composer.dock',
112
+ },
113
+ {
114
+ key: 'uiPeakEnabled',
115
+ label: '侧边栏峰谷时段条',
116
+ desc: '侧边栏底部的当前档位 / 倒计时。关闭后峰谷切换弹窗与系统通知一并停用;只想留提醒不想要时段条时,请改用「峰谷计价与提示」里的提示开关。',
117
+ slot: 'sidebar.footer.action',
118
+ },
119
+ {
120
+ key: 'uiDashboardEnabled',
121
+ label: '设置页「花费统计」看板',
122
+ desc: '设置页左侧导航的花费统计入口与看板。关闭后该入口隐藏,插件仍照常记账并同步云端。',
123
+ slot: 'settings.section',
124
+ },
125
+ ]
126
+
127
+ /** 界面显示默认配置(全部可见;老配置文件里没有这几个键,读出来就是 true) */
128
+ export function defaultUiConfig() {
129
+ const out = {}
130
+ for (const s of UI_SURFACES) out[s.key] = true
131
+ return out
132
+ }
133
+
87
134
  function bool(v, fallback) { return typeof v === 'boolean' ? v : fallback }
88
135
  function intIn(v, lo, hi, fallback) { return typeof v === 'number' && Number.isInteger(v) && v >= lo && v <= hi ? v : fallback }
89
136
 
@@ -143,9 +190,21 @@ export function normalizeCloudConfig(raw) {
143
190
  }
144
191
  }
145
192
 
146
- /** 合并规范化:一份配置文件同时承载峰谷与云端同步字段 */
193
+ /**
194
+ * 规范化界面显示开关(显隐真值见文件头的说明:缺省/非布尔 → 可见)。
195
+ * @param {object} raw - 任意输入
196
+ */
197
+ export function normalizeUiConfig(raw) {
198
+ const def = defaultUiConfig()
199
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return def
200
+ const out = {}
201
+ for (const s of UI_SURFACES) out[s.key] = bool(raw[s.key], def[s.key])
202
+ return out
203
+ }
204
+
205
+ /** 合并规范化:一份配置文件同时承载峰谷、云端同步与界面显示三组字段 */
147
206
  export function normalizePluginConfig(raw) {
148
- return Object.assign(normalizePeakConfig(raw), normalizeCloudConfig(raw))
207
+ return Object.assign(normalizePeakConfig(raw), normalizeCloudConfig(raw), normalizeUiConfig(raw))
149
208
  }
150
209
 
151
210
  /**
package/index.js CHANGED
@@ -14,12 +14,13 @@ import { PRICE_ERAS, V41_EFFECTIVE_AT, V41_PRO_ROUTE_AT, exactModelsAt, eraAt, S
14
14
  import {
15
15
  normalizePeakConfig, defaultPeakConfig, peakEffective,
16
16
  normalizeCloudConfig, defaultCloudConfig, normalizePluginConfig,
17
+ normalizeUiConfig, defaultUiConfig, UI_SURFACES,
17
18
  } from './config.js'
18
19
  import { createSyncEngine, setPluginVersion, SOURCE as SYNC_SOURCE, SYNC_VERSION, loadIdentity } from './sync.js'
19
20
  import { Schema } from './schema.js'
20
21
 
21
22
  /** 插件版本(写入上报信封,便于云端排查版本差异) */
22
- const PLUGIN_VERSION = '1.8.12'
23
+ const PLUGIN_VERSION = '1.8.13'
23
24
  setPluginVersion(PLUGIN_VERSION)
24
25
 
25
26
  /** 「设置 → 插件 → 插件配置」里的卡片字段(与 settings 命名空间一致) */
@@ -35,6 +36,10 @@ const SyncSchema = Schema.object({
35
36
  syncRollups: Schema.boolean().default(undefined).description('上报历史日汇总快照'),
36
37
  syncSinceDays: Schema.natural().default(undefined).description('补传起始窗口天数(0 = 不限)'),
37
38
  cloudView: Schema.string().default(undefined).description('看板视图:local / local+cloud / cloud'),
39
+ // 界面显示(v1.8.12):三个前端落点各自显隐,缺省/非布尔 = 可见
40
+ uiDockEnabled: Schema.boolean().default(undefined).description('显示输入框上方的花费胶囊'),
41
+ uiPeakEnabled: Schema.boolean().default(undefined).description('显示侧边栏峰谷时段条'),
42
+ uiDashboardEnabled: Schema.boolean().default(undefined).description('显示设置页「花费统计」看板'),
38
43
  })
39
44
 
40
45
  // ============================================================
@@ -217,6 +222,7 @@ export default {
217
222
  const CONFIG_FILE = join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'storages', 'cost-tracker-config.json')
218
223
  let peakConfig = defaultPeakConfig()
219
224
  let cloudConfig = defaultCloudConfig()
225
+ let uiConfig = defaultUiConfig()
220
226
  let configLoadWarned = false
221
227
 
222
228
  function loadConfig() {
@@ -225,6 +231,7 @@ export default {
225
231
  const parsed = JSON.parse(readFileSync(CONFIG_FILE, 'utf8'))
226
232
  peakConfig = normalizePeakConfig(parsed)
227
233
  cloudConfig = normalizeCloudConfig(parsed)
234
+ uiConfig = normalizeUiConfig(parsed)
228
235
  } catch (e) {
229
236
  if (!configLoadWarned) { console.error('cost tracker config load failed, using defaults', e); configLoadWarned = true }
230
237
  }
@@ -234,7 +241,7 @@ export default {
234
241
  try {
235
242
  mkdirSync(dirname(CONFIG_FILE), { recursive: true })
236
243
  const tmp = CONFIG_FILE + '.tmp'
237
- writeFileSync(tmp, JSON.stringify(Object.assign({}, peakConfig, cloudConfig)), 'utf8')
244
+ writeFileSync(tmp, JSON.stringify(Object.assign({}, peakConfig, cloudConfig, uiConfig)), 'utf8')
238
245
  renameSync(tmp, CONFIG_FILE)
239
246
  return true
240
247
  } catch (e) {
@@ -255,6 +262,13 @@ export default {
255
262
  return cloudConfig
256
263
  }
257
264
 
265
+ /** 界面显示开关:显式写 false 才隐藏,缺省保持可见(老配置升级后界面不变) */
266
+ function setUiConfig(raw) {
267
+ uiConfig = normalizeUiConfig(Object.assign({}, uiConfig, raw))
268
+ saveConfig()
269
+ return uiConfig
270
+ }
271
+
258
272
  // ---------- settings 命名空间(可选服务) ----------
259
273
  // 字段全部 .default(undefined):只有用户在卡片里显式保存才写入用户层,
260
274
  // 从而不覆盖我们自己配置文件里的既有值。
@@ -268,9 +282,25 @@ export default {
268
282
  const patch = {}
269
283
  for (const k of Object.keys(next)) if (next[k] !== undefined) patch[k] = next[k]
270
284
  if (!Object.keys(patch).length) return
271
- const before = JSON.stringify(cloudConfig)
272
- cloudConfig = normalizeCloudConfig(Object.assign({}, cloudConfig, patch))
273
- if (JSON.stringify(cloudConfig) !== before) saveConfig()
285
+ // 卡片里既有云端同步字段也有界面显示字段,分给各自的规范化函数(互不覆盖)
286
+ const cloudPatch = {}
287
+ const uiPatch = {}
288
+ for (const [k, v] of Object.entries(patch)) {
289
+ if (UI_SURFACES.some((s) => s.key === k)) uiPatch[k] = v
290
+ else cloudPatch[k] = v
291
+ }
292
+ let changed = false
293
+ if (Object.keys(cloudPatch).length) {
294
+ const before = JSON.stringify(cloudConfig)
295
+ cloudConfig = normalizeCloudConfig(Object.assign({}, cloudConfig, cloudPatch))
296
+ if (JSON.stringify(cloudConfig) !== before) changed = true
297
+ }
298
+ if (Object.keys(uiPatch).length) {
299
+ const before = JSON.stringify(uiConfig)
300
+ uiConfig = normalizeUiConfig(Object.assign({}, uiConfig, uiPatch))
301
+ if (JSON.stringify(uiConfig) !== before) changed = true
302
+ }
303
+ if (changed) saveConfig()
274
304
  }
275
305
 
276
306
  function installSettingsSection(provider, owner) {
@@ -319,6 +349,8 @@ export default {
319
349
  peakWindows: PEAK_WINDOWS,
320
350
  peakHours: PEAK_HOUR_WINDOWS,
321
351
  effectiveAt: peakConfig.peakEffectiveAt,
352
+ // 前端显隐开关(只影响渲染;与峰谷计价本身无关,因此不参与 enabled/effective 判定)
353
+ ui: Object.assign({}, uiConfig),
322
354
  now,
323
355
  }
324
356
  }
@@ -964,7 +996,7 @@ export default {
964
996
  // ---------- cloud sync ----------
965
997
  // 同步引擎:只上报、不回写;失败只影响云端视图,绝不影响本地记账。
966
998
  const syncEngine = createSyncEngine({
967
- getConfig: () => Object.assign({}, peakConfig, cloudConfig),
999
+ getConfig: () => Object.assign({}, peakConfig, cloudConfig, uiConfig),
968
1000
  getSnapshot: () => ({
969
1001
  details: records,
970
1002
  rollups,
@@ -1169,7 +1201,10 @@ export default {
1169
1201
  usage: () => buildUsageHeat(),
1170
1202
  peak: () => peakSnapshot(),
1171
1203
  'peak-config': (args) => setPeakConfig(args),
1172
- sync: () => syncEngine.status(),
1204
+ // 界面显示三开关(设置卡片的「界面显示」分组):patch 里只带这几个键,写回后原样回显
1205
+ 'ui-config': (args) => Object.assign({ ok: true }, setUiConfig(args || {})),
1206
+ // 同步状态里一并带上界面显示开关:配置卡片只需一次往返就能初始化表单
1207
+ sync: () => Object.assign({}, syncEngine.status(), uiConfig),
1173
1208
  'sync-now': async (args) => {
1174
1209
  const result = await syncEngine.runOnce({ manual: true, full: !!(args && args.full) })
1175
1210
  return Object.assign({ ok: result.ok !== false || !result.error, result }, syncEngine.status())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angelyeye/dsh-cost-tracker",
3
- "version": "1.8.12",
3
+ "version": "1.8.13",
4
4
  "description": "DeepSeek Harness (DSH) plugin: LLM token usage & cost tracking with a settings dashboard, agent tools, HTTP API, persistent storage and multi-device cloud sync.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -23,7 +23,7 @@
23
23
  "./package.json": "./package.json"
24
24
  },
25
25
  "scripts": {
26
- "test": "node test/storage.test.js && node test/pricing.test.js && node test/config.test.js && node test/recompute.test.js && node test/client-registration.test.js && node test/client-render.test.js && node test/cloud-read.test.js && node --no-warnings test/cloud-view-e2e.test.js && node test/sync.test.js && node test/view.test.js"
26
+ "test": "node test/storage.test.js && node test/pricing.test.js && node test/config.test.js && node test/recompute.test.js && node test/client-registration.test.js && node test/ui-display.test.js && node test/client-render.test.js && node test/cloud-read.test.js && node --no-warnings test/cloud-view-e2e.test.js && node test/sync.test.js && node test/view.test.js"
27
27
  },
28
28
  "keywords": [
29
29
  "dsh-plugin",