@angelyeye/dsh-cost-tracker 1.8.11 → 1.8.12
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 +31 -0
- package/README.en.md +9 -1
- package/README.md +10 -2
- package/client.js +82 -5
- package/index.js +76 -15
- package/package.json +1 -1
- package/view.js +56 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,37 @@
|
|
|
2
2
|
|
|
3
3
|
本文件用中文记录 dsh-cost-tracker 的版本变更。
|
|
4
4
|
|
|
5
|
+
## v1.8.12(2026-09-19)
|
|
6
|
+
|
|
7
|
+
**修复:①「Token 用量统计」热力图不跟随三态视图(本机+云端下屏内自相矛盾)② `cost_stats scope=cloud|both` 崩溃**
|
|
8
|
+
|
|
9
|
+
- **① 热力图此前恒为本机**:它的数据源是宿主的 `usage` 路由 → `buildUsageHeat()`,只遍历本地
|
|
10
|
+
store 的 records/rollups,**没有任何云端输入**;而同一屏的卡片走 `viewDash`(本机+云端合并)。
|
|
11
|
+
于是切到「本机+云端」时,热力图说「累计 98.4M · 536 次调用」(纯本机),上方卡片说 955M ——
|
|
12
|
+
同一屏两套口径,用户第一反应就是"统计显示有问题"。
|
|
13
|
+
- 宿主新增 `route=usage` 取数:走云端 `/api/v1/plugin-view`(**只有它的 byDay 带 token 类型
|
|
14
|
+
拆分**)、固定 `range=all`(热力图是全时段累计口径,与页面区间选择无关)、
|
|
15
|
+
「本机+云端」沿用与卡片相同的 `union` 并集(其他整机 ∪ 本机其它 agent),
|
|
16
|
+
并归一成与本地 `buildUsageHeat` 同形的 `{days:[{date,input,output,cacheRead,cacheWrite,...}], total}`。
|
|
17
|
+
- 归一化时两处口径对齐:**tokens 一律按 `input+output+cacheRead+cacheWrite` 重算**(云端
|
|
18
|
+
byDay 的 tokens 含 `reasoning`,直接相加会比两侧之和大一截);**丢掉云端连续日期轴里的
|
|
19
|
+
补零日**(本地只返回有数据的日期,热力图自己会按 26 周补格)。
|
|
20
|
+
- 客户端新增 `mergeUsageHeat`(`view.js` 与 bundle 内联实现各一份,与 `mergeDash` 同款约定),
|
|
21
|
+
热力图改为消费 `viewUsage`:本机 / 本机+云端 / 仅云端;云端明细取不到时退回本机,
|
|
22
|
+
并在卡片标题右侧标注口径(「本机 + 云端(不重复计数)」/「仅云端」),避免再出现无标注的纯本机数字。
|
|
23
|
+
- **云端前置依赖**:`dsh-cost-cloud ≥ v1.3.2`(`plugin-view` 的 `byDay` 才带
|
|
24
|
+
input/output/cacheRead/cacheWrite)。旧云端下热力图自动退回本机,卡片与其它面板不受影响。
|
|
25
|
+
- **② `cost_stats scope=cloud|both` 必崩**:`Cannot read properties of undefined (reading 'realCost')`。
|
|
26
|
+
根因是云端响应形状漂移 —— `/api/v1/overview` 的金额摊在 `summary.{realCost,subEquivalent,…}`,
|
|
27
|
+
而宿主在云端支持 `devicePluginView` 时会**优先用 `/api/v1/plugin-view`**,后者**没有 `summary`**
|
|
28
|
+
(字段直接摊在顶层)。工具只读了 `c.summary`,于是 README 明确文档化的 `scope=both` 一直不可用。
|
|
29
|
+
现两种形状都兼容。
|
|
30
|
+
- **测试**:`view.test.js` 新增第 11 节(合并的相加不变量、tokens 重算、容错分支);
|
|
31
|
+
`cloud-read.test.js` 新增 4 条路径契约(usage 走 plugin-view、固定 range=all、
|
|
32
|
+
union 对 overview/usage 同时生效、cost_stats 兼容两种形状);`client-render.test.js`
|
|
33
|
+
新增第 7 节(三态下热力图的累计与按天数字,含"云端独有日期必须进入热力图");
|
|
34
|
+
`cloud-view-e2e.test.js` 新增真实云端 payload → `cloudUsageHeat` → `mergeUsageHeat` 的端到端不重不漏断言。
|
|
35
|
+
|
|
5
36
|
## v1.8.11(2026-09-17)
|
|
6
37
|
|
|
7
38
|
**新增:启动时检测「另一个 dsh 实例正在用同一份记录文件」并直接点名**
|
package/README.en.md
CHANGED
|
@@ -159,7 +159,7 @@ rm -rf ~/.dsh/profiles/node_modules/dsh-cost-tracker
|
|
|
159
159
|
- **Cost chart**: segment by peak period or by model; hover for daily breakdowns;
|
|
160
160
|
- **Color schemes**: in "by model" view, three swatches next to the title switch between 橙→黄 / 蓝→紫 / 蓝→浅蓝 palettes. Models are ranked by total spend and colored in a sequential gradient (rank 1 = darkest at the bottom, getting lighter upwards; no cycling, no collisions). The choice is remembered in the browser (localStorage);
|
|
161
161
|
- **Per-model sections**: one request-count chart and one token-composition chart (input / cache write / output / cache hit) per model;
|
|
162
|
-
- **Usage heatmap**: a Codex-style 26-week daily-usage grid; shade by the day's token count relative to the maximum; hover any cell for that day's breakdown (input / cache / output / cost), today outlined;
|
|
162
|
+
- **Usage heatmap**: a Codex-style 26-week daily-usage grid; shade by the day's token count relative to the maximum; hover any cell for that day's breakdown (input / cache / output / cost), today outlined; **the scope follows the three-state switch**: "local" counts this machine only, "local + cloud" merges the cloud's per-day usage (same union scope as the cards above, no double counting), "cloud only" shows the cloud alone, with the active scope labelled next to the title. Cloud per-day detail requires `dsh-cost-cloud ≥ v1.3.2`; on an older cloud it falls back to the local scope;
|
|
163
163
|
- **Peak/off-peak pricing & notice**: pick the tier strip style (**compact single-row — a 24h-proportional bar with a white real-time line** / **ring dial — a 24h hollow dial with a phase-colored dot**), optionally switch the compact strip to a **two-row compact** stacked layout (bar-above-text or text-above-bar), toggle the "Show time" tick labels (00:00–21:00), set the popup-alert lead time (1–30 min), alert type (both / entering peak / entering off-peak), popup position (bottom-right / center), and optional browser system notification; everything auto-saves. A persistent strip in the sidebar footer shows the current tier and countdown to the next switch;
|
|
164
164
|
- **CSV export**: exports detail records (last 180 days) plus daily rollup rows (`purpose=rollup`).
|
|
165
165
|
|
|
@@ -243,6 +243,14 @@ Example: `curl -X POST http://127.0.0.1:3080/api/cost-tracker/summary -d '{}'`
|
|
|
243
243
|
|
|
244
244
|
> Highlights only — the full version-by-version history lives in [`CHANGELOG.md`](./CHANGELOG.md) (Chinese).
|
|
245
245
|
|
|
246
|
+
### v1.8.12 (2026-09-19)
|
|
247
|
+
|
|
248
|
+
**Fixed: the heatmap ignored the three-state switch (self-contradicting screen in "local + cloud"), and `cost_stats scope=cloud|both` crashed**
|
|
249
|
+
|
|
250
|
+
- The "Token Usage" heatmap only ever read the local store, so in "local + cloud" it still showed a local-only total (field report: heatmap 98.4M / 536 calls on the same screen as cards reading 955M). It now fetches per view: the host asks the cloud's `plugin-view` for per-day detail **with the token-type breakdown** (`range=all` all-time scope; "local + cloud" reuses the same `union` as the cards), and the client merges it with `mergeUsageHeat`. When cloud detail is unavailable it falls back to local and labels the scope next to the title. **Requires `dsh-cost-cloud ≥ v1.3.2`.**
|
|
251
|
+
- Fixed the `Cannot read properties of undefined (reading 'realCost')` crash in `cost_stats scope=cloud|both`: cloud responses come in two shapes (`overview.summary` vs. `plugin-view` top level) and only the former was read.
|
|
252
|
+
- Added regression guards: `view.test.js` section 11, four path contracts in `cloud-read.test.js`, `client-render.test.js` section 7, and an end-to-end merge assertion in `cloud-view-e2e.test.js` against the real cloud.
|
|
253
|
+
|
|
246
254
|
### v1.8.11 (2026-09-17)
|
|
247
255
|
|
|
248
256
|
**Added: startup detection of "another dsh instance is using the same records file", reported by name**
|
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
| 🏷️ | **官方模型名对齐** | 计费支持**官方现役名 `deepseek-flash`**(价格卡脚注 (1):「模型名请使用 `deepseek-flash`」),旧写法 `deepseek-v4.1-flash` 等归一化后命中同一档价,避免因官方改名而落入兜底估算 |
|
|
37
37
|
| 📊 | **可视化仪表盘** | 设置页新增「花费统计」:概览卡片、消费柱状图(按峰谷/按模型)、分模型的请求次数与 Tokens 图表,**全部支持鼠标悬停查看明细** |
|
|
38
38
|
| 🧭 | **多机汇总(云端同步)** | 可对接**自建云端服务**(独立仓库 `dsh-cost-cloud`,零运行时依赖):多台电脑的用量汇总到一处,看板顶部出现 **本机 / 本机+云端 / 仅云端** 三态开关,并支持**设备 × Agent 矩阵**(行合计 = 列合计 = 总计);上报内容仅 token 数、费用、时间戳与标识符,可选会话脱敏 |
|
|
39
|
-
| 🔥 | **用量热力图** | 设置页新增「Token 用量统计」:类 Codex 的 **26 周日用量方格热图**,按天着色(输入 / 缓存 / 输出 / 费用)
|
|
39
|
+
| 🔥 | **用量热力图** | 设置页新增「Token 用量统计」:类 Codex 的 **26 周日用量方格热图**,按天着色(输入 / 缓存 / 输出 / 费用),悬停看当日明细、今天高亮描边,顶部显示全时段累计;**跟随三态视图**——「本机+云端 / 仅云端」下自动并入云端按天用量(卡片标题右侧标注当前口径) |
|
|
40
40
|
| 📈 | **订阅配额监控** | Kimi Coding Plan 等订阅套餐:本周配额、5 小时滚动窗口限额、等效按量费用参考 |
|
|
41
41
|
| 💳 | **余额查询** | 一键查询 DeepSeek 官方账户余额(总余额 / 充值 / 赠送 / 状态) |
|
|
42
42
|
| 🤖 | **Agent 工具** | 直接在对话里问:"我现在花了多少钱?"——Agent 会调用 `cost_stats` / `cost_prices` 等工具回答 |
|
|
@@ -159,7 +159,7 @@ rm -rf ~/.dsh/profiles/node_modules/dsh-cost-tracker
|
|
|
159
159
|
- **消费金额图**:支持「按峰谷」「按模型」两种分段方式,鼠标悬停查看当日明细;
|
|
160
160
|
- **配色切换**:「按模型」视图下,消费金额标题旁有三套装色款条(橙→黄 / 蓝→紫 / 蓝→浅蓝)可切换;模型按总消费降序排名取色(第 1 名最深夜底、逐级变浅,不循环不撞色),选择保存在浏览器本地;
|
|
161
161
|
- **分模型区块**:每个模型一张请求次数图 + 一张 Tokens 构成图(输入/缓存写入/输出/缓存命中);
|
|
162
|
-
- **Token 用量统计热力图**:类 Codex 的 26 周每日用量方格,颜色深浅按当日 token 相对最大值分档;悬停任一格看该日明细(输入 / 缓存 / 输出 / 费用)
|
|
162
|
+
- **Token 用量统计热力图**:类 Codex 的 26 周每日用量方格,颜色深浅按当日 token 相对最大值分档;悬停任一格看该日明细(输入 / 缓存 / 输出 / 费用),今天高亮描边;**口径跟随三态开关**:「本机」只统计本机,「本机+云端」把云端按天用量并入(与上方卡片同一并集口径、不重复计数),「仅云端」只看云端,标题右侧标注当前口径。云端按天明细需要 `dsh-cost-cloud ≥ v1.3.2`,旧云端下自动退回本机口径;
|
|
163
163
|
- **峰谷计价与提示**:时段条样式可选(**简洁单行·按 24h 比例**(白色实时进度线) / **环形表盘·相位色点**),简洁样式可选**双行紧凑**(条上文下 / 文上条下),可开关「显示时间」刻度(00:00–21:00),设置峰/谷切换前的弹窗提醒提前量(1–30 分钟)、提醒类型(两者 / 进入高峰 / 进入闲时)、弹窗位置(右下角 / 屏幕中心)与浏览器系统通知;全部自动保存。侧边栏底部常驻时段条显示当前档位与下次切换倒计时;
|
|
164
164
|
- **导出 CSV**:导出明细记录(近 180 天)+ 日汇总行(`purpose=rollup`)。
|
|
165
165
|
|
|
@@ -243,6 +243,14 @@ POST /api/cost-tracker/export 导出 CSV
|
|
|
243
243
|
|
|
244
244
|
> 这里只列重要版本;逐版完整记录见 [`CHANGELOG.md`](./CHANGELOG.md)。
|
|
245
245
|
|
|
246
|
+
### v1.8.12(2026-09-19)
|
|
247
|
+
|
|
248
|
+
**修复:热力图不跟随三态视图(本机+云端下屏内自相矛盾)+ `cost_stats scope=cloud|both` 崩溃**
|
|
249
|
+
|
|
250
|
+
- 「Token 用量统计」热力图此前只读本地 store,切到「本机+云端」时仍显示纯本机累计(现场:热力图 98.4M / 536 次调用,同屏卡片 955M)。现在按视图取数:宿主走云端 `plugin-view` 拿**带 token 类型拆分的按天明细**(`range=all` 全时段口径,「本机+云端」沿用与卡片相同的 `union` 并集),客户端 `mergeUsageHeat` 合并;云端明细不可用时退回本机并在标题右侧标注口径。**需要 `dsh-cost-cloud ≥ v1.3.2`**。
|
|
251
|
+
- 修掉 `cost_stats scope=cloud|both` 必崩的 `Cannot read properties of undefined (reading 'realCost')`:云端响应有 `overview.summary` 与 `plugin-view` 顶层两种形状,此前只读前者。
|
|
252
|
+
- 新增护栏:`view.test.js` 第 11 节、`cloud-read.test.js` 4 条契约、`client-render.test.js` 第 7 节、`cloud-view-e2e.test.js` 真实云端端到端合并断言。
|
|
253
|
+
|
|
246
254
|
### v1.8.11(2026-09-17)
|
|
247
255
|
|
|
248
256
|
**新增:启动时检测「另一个 dsh 实例正在用同一份记录文件」并直接点名**
|
package/client.js
CHANGED
|
@@ -660,6 +660,49 @@ window.__ModuleLoader__.load({
|
|
|
660
660
|
asOf: cloud.asOf || 0,
|
|
661
661
|
});
|
|
662
662
|
}
|
|
663
|
+
// 「Token 用量统计」热力图的跨视图合并(与 view.js 的 mergeUsageHeat 逐行等价,
|
|
664
|
+
// 浏览器里 require("./view") 取不到时必须自带一份,否则云端用量永远并不进来)
|
|
665
|
+
function vR4(x) { return Math.round((Number(x) || 0) * 10000) / 10000; }
|
|
666
|
+
function vMergeUsageHeat(local, cloud) {
|
|
667
|
+
if (!cloud || !Array.isArray(cloud.days)) return local || null;
|
|
668
|
+
if (!local || !Array.isArray(local.days)) return cloud;
|
|
669
|
+
const per = new Map();
|
|
670
|
+
const add = (d) => {
|
|
671
|
+
if (!d || !d.date) return;
|
|
672
|
+
const cur = per.get(d.date) || { date: d.date, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, calls: 0, cost: 0, tokens: 0 };
|
|
673
|
+
cur.input += d.input || 0;
|
|
674
|
+
cur.output += d.output || 0;
|
|
675
|
+
cur.cacheRead += d.cacheRead || 0;
|
|
676
|
+
cur.cacheWrite += d.cacheWrite || 0;
|
|
677
|
+
cur.calls += d.calls || 0;
|
|
678
|
+
cur.cost += d.cost || 0;
|
|
679
|
+
// tokens 一律按本地口径重算:云端 byDay 的 tokens 含 reasoning,直接相加会不同源
|
|
680
|
+
cur.tokens = cur.input + cur.output + cur.cacheRead + cur.cacheWrite;
|
|
681
|
+
per.set(d.date, cur);
|
|
682
|
+
};
|
|
683
|
+
for (const d of local.days) add(d);
|
|
684
|
+
for (const d of cloud.days) add(d);
|
|
685
|
+
const days = Array.from(per.values())
|
|
686
|
+
.sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0))
|
|
687
|
+
.map((d) => Object.assign({}, d, { cost: vR4(d.cost) }));
|
|
688
|
+
const lt = local.total || {};
|
|
689
|
+
const ct = cloud.total || {};
|
|
690
|
+
const n = (v) => Number(v) || 0;
|
|
691
|
+
return {
|
|
692
|
+
ok: true,
|
|
693
|
+
source: "merged",
|
|
694
|
+
days,
|
|
695
|
+
total: {
|
|
696
|
+
tokens: n(lt.tokens) + n(ct.tokens),
|
|
697
|
+
input: n(lt.input) + n(ct.input),
|
|
698
|
+
cache: n(lt.cache) + n(ct.cache),
|
|
699
|
+
output: n(lt.output) + n(ct.output),
|
|
700
|
+
calls: n(lt.calls) + n(ct.calls),
|
|
701
|
+
cost: vR4(n(lt.cost) + n(ct.cost)),
|
|
702
|
+
},
|
|
703
|
+
asOf: cloud.asOf || 0,
|
|
704
|
+
};
|
|
705
|
+
}
|
|
663
706
|
const VIEW = requireLocal("./view", {
|
|
664
707
|
BOARD_VIEWS: [
|
|
665
708
|
{ id: "local", label: "本机" },
|
|
@@ -678,11 +721,13 @@ window.__ModuleLoader__.load({
|
|
|
678
721
|
r2: vR2,
|
|
679
722
|
normalizeCloudDash: vNormalizeCloudDash,
|
|
680
723
|
mergeDash: vMergeDash,
|
|
724
|
+
mergeUsageHeat: vMergeUsageHeat,
|
|
681
725
|
});
|
|
682
726
|
const BOARD_VIEWS = VIEW.BOARD_VIEWS;
|
|
683
727
|
const BOARD_DIMS = VIEW.BOARD_DIMS;
|
|
684
728
|
const normalizeCloudDash = VIEW.normalizeCloudDash;
|
|
685
729
|
const mergeDash = VIEW.mergeDash;
|
|
730
|
+
const mergeUsageHeat = VIEW.mergeUsageHeat || vMergeUsageHeat;
|
|
686
731
|
|
|
687
732
|
function filterRow(days, setDays, onExport, onRefresh, msg, busy, peakWindows, viewCtl) {
|
|
688
733
|
const views = viewCtl && viewCtl.available ? BOARD_VIEWS : [BOARD_VIEWS[0]];
|
|
@@ -1523,6 +1568,8 @@ window.__ModuleLoader__.load({
|
|
|
1523
1568
|
const [cloudDash, setCloudDash] = useState(null);
|
|
1524
1569
|
const [cloudErr, setCloudErr] = useState("");
|
|
1525
1570
|
const [cloudMatrix, setCloudMatrix] = useState(null);
|
|
1571
|
+
const [cloudUsage, setCloudUsage] = useState(null);
|
|
1572
|
+
const [cloudUsageErr, setCloudUsageErr] = useState("");
|
|
1526
1573
|
const [view, setViewState] = useState(() => {
|
|
1527
1574
|
try { return localStorage.getItem("dsh-cost-tracker-view") || "local" } catch (e) { return "local" }
|
|
1528
1575
|
});
|
|
@@ -1577,6 +1624,20 @@ window.__ModuleLoader__.load({
|
|
|
1577
1624
|
if (v && v.ok !== false) setCloudMatrix(v); else setCloudMatrix(null);
|
|
1578
1625
|
}).catch(() => setCloudMatrix(null));
|
|
1579
1626
|
}
|
|
1627
|
+
/**
|
|
1628
|
+
* 云端「按天用量明细」(热力图数据源)。
|
|
1629
|
+
* · view=local+cloud → cloud-rest:其他整机 ∪ 本机其它 agent(与卡片同一并集口径)
|
|
1630
|
+
* · view=cloud → 全网
|
|
1631
|
+
* 宿主把它归一成与本地 buildUsageHeat 同形的 {days:[{date,input,output,cacheRead,cacheWrite,...}], total},
|
|
1632
|
+
* 客户端再按视图合并,热力图与上方卡片口径才能一致(此前热力图恒为本机,屏内自相矛盾)。
|
|
1633
|
+
*/
|
|
1634
|
+
function loadCloudUsage(mode) {
|
|
1635
|
+
if (!mode || mode === "local") { setCloudUsage(null); setCloudUsageErr(""); return; }
|
|
1636
|
+
apiCall("cloud", { route: "usage", view: mode === "local+cloud" ? "cloud-rest" : "cloud" }).then(v => {
|
|
1637
|
+
if (v && v.ok !== false && Array.isArray(v.days)) { setCloudUsage(v); setCloudUsageErr(""); }
|
|
1638
|
+
else { setCloudUsage(null); setCloudUsageErr((v && v.error) || "云端用量明细不可用"); }
|
|
1639
|
+
}).catch(err => { setCloudUsage(null); setCloudUsageErr(String(err && err.message ? err.message : err)); });
|
|
1640
|
+
}
|
|
1580
1641
|
function loadKimi(force) {
|
|
1581
1642
|
apiCall("kimi-usage", { force: !!force }).then(v => setKimi(v)).catch(() => {});
|
|
1582
1643
|
}
|
|
@@ -1604,13 +1665,14 @@ window.__ModuleLoader__.load({
|
|
|
1604
1665
|
else setUsageErr(v && v.error ? String(v.error) : "数据加载失败");
|
|
1605
1666
|
}).catch(err => setUsageErr(String(err && err.message ? err.message : err)));
|
|
1606
1667
|
apiCall("kimi-usage", { force: false }).then(v => setKimi(v)).catch(() => {});
|
|
1668
|
+
loadCloudUsage(view);
|
|
1607
1669
|
apiCall("balance", {}).then(v => { setBalance(v); setBusy(false); }).catch(() => setBusy(false));
|
|
1608
1670
|
}
|
|
1609
1671
|
|
|
1610
1672
|
useEffect(() => { loadDash(days); }, [days]);
|
|
1611
1673
|
useEffect(() => { loadKimi(false); loadBalance(""); loadUsage(); loadSync(); }, []);
|
|
1612
|
-
// 视图切换:local+cloud
|
|
1613
|
-
useEffect(() => { loadCloud(days, view); loadCloudMatrix(days); }, [view, days]);
|
|
1674
|
+
// 视图切换:local+cloud 需要额外拉「排除本机」的云端聚合、矩阵与按天用量明细
|
|
1675
|
+
useEffect(() => { loadCloud(days, view); loadCloudMatrix(days); loadCloudUsage(view); }, [view, days]);
|
|
1614
1676
|
useEffect(() => {
|
|
1615
1677
|
const id = setInterval(() => setNow(Date.now()), 30000);
|
|
1616
1678
|
return () => clearInterval(id);
|
|
@@ -1623,6 +1685,18 @@ window.__ModuleLoader__.load({
|
|
|
1623
1685
|
: view === "cloud"
|
|
1624
1686
|
? cloudNorm
|
|
1625
1687
|
: (cloudNorm ? mergeDash(dash, cloudNorm) : dash);
|
|
1688
|
+
// 热力图同样跟随三态视图:本机 / 本机+云端 / 仅云端。
|
|
1689
|
+
// 云端明细取不到时退回本机,并在标题右侧注明口径,避免与上方卡片数字打架。
|
|
1690
|
+
const viewUsage = view === "local"
|
|
1691
|
+
? usage
|
|
1692
|
+
: view === "cloud"
|
|
1693
|
+
? cloudUsage
|
|
1694
|
+
: (usage && cloudUsage ? mergeUsageHeat(usage, cloudUsage) : (cloudUsage || usage));
|
|
1695
|
+
const usageScope = view === "local"
|
|
1696
|
+
? "本机"
|
|
1697
|
+
: view === "cloud"
|
|
1698
|
+
? ("仅云端" + (cloudUsageErr ? "(" + cloudUsageErr + ")" : ""))
|
|
1699
|
+
: (cloudUsage ? "本机 + 云端(不重复计数)" : "本机(云端明细加载中或不可用)");
|
|
1626
1700
|
const cloudAvailable = !!(sync && sync.enabled && sync.hasToken);
|
|
1627
1701
|
const viewCtl = {
|
|
1628
1702
|
view, setView, dimension, setDimension,
|
|
@@ -1646,7 +1720,7 @@ window.__ModuleLoader__.load({
|
|
|
1646
1720
|
sync && sync.enabled && sync.pending > 0 && view !== "local"
|
|
1647
1721
|
? e("div", { className: "cost-cloudnote" },
|
|
1648
1722
|
"本机还有 " + sync.pending + " 条尚未同步,云端数字会偏小 —— ",
|
|
1649
|
-
e("button", { className: "cost-btn", onClick: () => { apiCall("sync-now", {}).then(() => { loadSync(); loadCloud(days, view); loadCloudMatrix(days); }); } }, "立即同步"))
|
|
1723
|
+
e("button", { className: "cost-btn", onClick: () => { apiCall("sync-now", {}).then(() => { loadSync(); loadCloud(days, view); loadCloudMatrix(days); loadCloudUsage(view); }); } }, "立即同步"))
|
|
1650
1724
|
: null,
|
|
1651
1725
|
cloudErr && view !== "local"
|
|
1652
1726
|
? e("div", { className: "cost-err", style: { marginTop: "6px" } }, "云端数据不可用(已显示本机数据):" + cloudErr)
|
|
@@ -1660,9 +1734,12 @@ window.__ModuleLoader__.load({
|
|
|
1660
1734
|
view !== "local" && dimension !== "total" && viewDash ? dimensionPanel(viewDash, dimension, cloudMatrix, loadingCloud => loadingCloud) : null,
|
|
1661
1735
|
viewDash ? mainPanel(viewDash, tab, setTab, scheme, setScheme) : null,
|
|
1662
1736
|
e("div", { className: "cost-panel" },
|
|
1663
|
-
e("div", { className: "cost-row" },
|
|
1737
|
+
e("div", { className: "cost-row" },
|
|
1738
|
+
e("span", { className: "cost-panel-title" }, "Token 用量统计"),
|
|
1739
|
+
e("span", { className: "cost-spacer" }),
|
|
1740
|
+
e("span", { className: "cost-hint" }, usageScope)),
|
|
1664
1741
|
e("div", { style: { marginTop: "8px" } },
|
|
1665
|
-
|
|
1742
|
+
viewUsage ? e(UsageHeatmap, { data: viewUsage })
|
|
1666
1743
|
: usageErr ? e("div", { className: "cost-err" }, "加载失败:" + usageErr)
|
|
1667
1744
|
: e("div", { className: "cost-hint" }, "加载中…"))),
|
|
1668
1745
|
subPanel(kimi, viewDash, now, () => loadKimi(true)),
|
package/index.js
CHANGED
|
@@ -19,7 +19,7 @@ import { createSyncEngine, setPluginVersion, SOURCE as SYNC_SOURCE, SYNC_VERSION
|
|
|
19
19
|
import { Schema } from './schema.js'
|
|
20
20
|
|
|
21
21
|
/** 插件版本(写入上报信封,便于云端排查版本差异) */
|
|
22
|
-
const PLUGIN_VERSION = '1.8.
|
|
22
|
+
const PLUGIN_VERSION = '1.8.12'
|
|
23
23
|
setPluginVersion(PLUGIN_VERSION)
|
|
24
24
|
|
|
25
25
|
/** 「设置 → 插件 → 插件配置」里的卡片字段(与 settings 命名空间一致) */
|
|
@@ -57,6 +57,48 @@ function startupLog(msg) {
|
|
|
57
57
|
// ============================================================
|
|
58
58
|
const NAV_ICON_BRANCH = 'if (id === "cost-dashboard") return (0, react_jsx_runtime.jsxs)("svg", { className: SettingsRoot_module_css_default.navIcon, width: 16, height: 16, viewBox: "0 0 16 16", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [(0, react_jsx_runtime.jsx)("rect", { x: 1.5, y: 8.6, width: 3.1, height: 5.9, rx: 0.9, fill: "currentColor" }), (0, react_jsx_runtime.jsx)("rect", { x: 5.9, y: 5.2, width: 3.1, height: 9.3, rx: 0.9, fill: "currentColor" }), (0, react_jsx_runtime.jsxs)("g", { fill: "none", stroke: "currentColor", strokeWidth: 1.3, strokeLinecap: "round", strokeLinejoin: "round", children: [(0, react_jsx_runtime.jsx)("path", { d: "M10.7 4.9 L12.3 7.2 L13.9 4.9" }), (0, react_jsx_runtime.jsx)("path", { d: "M12.3 7.2 L12.3 10.9" }), (0, react_jsx_runtime.jsx)("path", { d: "M10.9 7.8 L13.7 7.8" }), (0, react_jsx_runtime.jsx)("path", { d: "M10.9 9.5 L13.7 9.5" })] })] }); // cost-tracker-icon-patch\n\t\t\t'
|
|
59
59
|
|
|
60
|
+
// ============================================================
|
|
61
|
+
// 云端按天用量明细 → 「Token 用量统计」热力图形状(纯函数,跨仓库 e2e 直接单测)
|
|
62
|
+
//
|
|
63
|
+
// 形状与本地 buildUsageHeat() 一致:
|
|
64
|
+
// { ok, source, asOf, days: [{date,input,output,cacheRead,cacheWrite,calls,cost,tokens}],
|
|
65
|
+
// total: {tokens, input, cache, output, calls, cost} }
|
|
66
|
+
//
|
|
67
|
+
// tokens 一律按 input+output+cacheRead+cacheWrite **重算**:云端 byDay 的 tokens 含
|
|
68
|
+
// reasoning,而本地 buildUsageHeat 不含 —— 直接相加会让「本机+云端」比两侧之和大一截。
|
|
69
|
+
// ============================================================
|
|
70
|
+
export function cloudUsageHeat(body, mode) {
|
|
71
|
+
const n = (v) => Number(v) || 0
|
|
72
|
+
const days = ((body && body.byDay) || []).map((d) => {
|
|
73
|
+
const input = n(d.input), output = n(d.output), cacheRead = n(d.cacheRead), cacheWrite = n(d.cacheWrite)
|
|
74
|
+
return {
|
|
75
|
+
date: String(d.date), input, output, cacheRead, cacheWrite,
|
|
76
|
+
calls: n(d.calls), cost: n(d.cost),
|
|
77
|
+
tokens: input + output + cacheRead + cacheWrite,
|
|
78
|
+
}
|
|
79
|
+
// 云端 range=all 返回的是**连续日期轴**(首条记录到今天,含大量补零日),
|
|
80
|
+
// 而本地 buildUsageHeat 只返回有数据的日期。这里对齐本地语义:丢掉全零日,
|
|
81
|
+
// 否则合并后的 days 会多出几百个空格(热力图自己会按 26 周补格,不需要它们)。
|
|
82
|
+
}).filter((d) => d.tokens > 0 || d.calls > 0 || d.cost > 0)
|
|
83
|
+
const sum = (k) => days.reduce((s, d) => s + (d[k] || 0), 0)
|
|
84
|
+
const r4 = (x) => Math.round((Number(x) || 0) * 10000) / 10000
|
|
85
|
+
return {
|
|
86
|
+
ok: true,
|
|
87
|
+
source: 'cloud',
|
|
88
|
+
asOf: Date.now(),
|
|
89
|
+
cloudMode: mode,
|
|
90
|
+
days,
|
|
91
|
+
total: {
|
|
92
|
+
tokens: sum('tokens'),
|
|
93
|
+
input: sum('input'),
|
|
94
|
+
cache: sum('cacheRead') + sum('cacheWrite'),
|
|
95
|
+
output: sum('output'),
|
|
96
|
+
calls: sum('calls'),
|
|
97
|
+
cost: r4(sum('cost')),
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
60
102
|
export function ensureNavIconPatch(opts) {
|
|
61
103
|
const log = (opts && opts.log) || (() => {})
|
|
62
104
|
try {
|
|
@@ -1054,8 +1096,9 @@ export default {
|
|
|
1054
1096
|
}
|
|
1055
1097
|
}
|
|
1056
1098
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1099
|
+
// union 并集:overview(卡片口径)与 plugin-view(按天/按模型图表口径)都支持;
|
|
1100
|
+
// 「本机+云端」的卡片走 overview、热力图走 plugin-view,各自取自己需要的范围。
|
|
1101
|
+
if (unionParts && (query.route === 'overview' || query.route === 'usage')) {
|
|
1059
1102
|
qs.set('union', JSON.stringify(unionParts))
|
|
1060
1103
|
} else if (mode === 'cloud-rest' && !selfKnown && selfId) {
|
|
1061
1104
|
qs.set('excludeDevice', selfId) // 拿不到本机记录时退化为「排除本机整台」
|
|
@@ -1072,7 +1115,13 @@ export default {
|
|
|
1072
1115
|
// · 带 union(「本机+云端」并集)时沿用 overview:其卡片是**全网**口径,与并集相加的
|
|
1073
1116
|
// 语义一致;plugin-view 的卡片落在并集范围内,换成它会让总额口径突变。
|
|
1074
1117
|
let endpoint = query.route
|
|
1075
|
-
if (query.route === '
|
|
1118
|
+
if (query.route === 'usage') {
|
|
1119
|
+
// 热力图要的是**按天明细 + token 类型拆分**,只有 plugin-view 有;
|
|
1120
|
+
// 且热力图是「全时段累计」口径,与页面上的区间选择无关,固定 range=all。
|
|
1121
|
+
endpoint = 'plugin-view'
|
|
1122
|
+
qs.set('range', 'all')
|
|
1123
|
+
qs.delete('days')
|
|
1124
|
+
} else if (query.route === 'overview' && !unionParts) {
|
|
1076
1125
|
const caps = await cloudCaps()
|
|
1077
1126
|
if (caps.pluginView) endpoint = 'plugin-view'
|
|
1078
1127
|
}
|
|
@@ -1089,14 +1138,16 @@ export default {
|
|
|
1089
1138
|
const out = { ok: false, error: hint || (body && body.error) || ('HTTP ' + res.status), code: (body && body.code) || (res.status === 404 ? 'CLOUD_TOO_OLD' : 'CLOUD_ERROR') }
|
|
1090
1139
|
return out
|
|
1091
1140
|
}
|
|
1092
|
-
const value =
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1141
|
+
const value = query.route === 'usage'
|
|
1142
|
+
? Object.assign(cloudUsageHeat(body, mode), { selfResolved: selfKnown, selfDeviceId: selfId, unionParts: unionParts })
|
|
1143
|
+
: Object.assign({}, body, {
|
|
1144
|
+
source: 'cloud',
|
|
1145
|
+
asOf: Date.now(),
|
|
1146
|
+
cloudMode: mode,
|
|
1147
|
+
selfResolved: selfKnown,
|
|
1148
|
+
selfDeviceId: selfId,
|
|
1149
|
+
unionParts: unionParts,
|
|
1150
|
+
})
|
|
1100
1151
|
cloudCache.set(key, { at: Date.now(), value })
|
|
1101
1152
|
return value
|
|
1102
1153
|
} catch (e) {
|
|
@@ -1320,13 +1371,23 @@ export default {
|
|
|
1320
1371
|
}
|
|
1321
1372
|
if (scope === 'cloud' || scope === 'both') {
|
|
1322
1373
|
const c = await fetchCloud({ route: 'overview', range: days === 0 ? 'all' : '7d', days })
|
|
1374
|
+
// 云端响应有两种形状,必须都认:
|
|
1375
|
+
// · /api/v1/overview → 金额摊在 c.summary.{realCost,subEquivalent,...}
|
|
1376
|
+
// · /api/v1/plugin-view → 没有 summary,字段直接摊在顶层(宿主在云端支持
|
|
1377
|
+
// devicePluginView 时会优先用它)。1.8.11 只读了 c.summary,于是
|
|
1378
|
+
// scope=cloud|both 必然抛 "Cannot read properties of undefined (reading 'realCost')"。
|
|
1379
|
+
const s = (c && c.summary) ? c.summary : (c || {})
|
|
1380
|
+
const num = (v) => Number(v) || 0
|
|
1323
1381
|
out.cloud = c.ok === false
|
|
1324
1382
|
? { ok: false, error: c.error || '云端不可用' }
|
|
1325
1383
|
: {
|
|
1326
1384
|
ok: true,
|
|
1327
|
-
realCost:
|
|
1328
|
-
|
|
1329
|
-
|
|
1385
|
+
realCost: num(s.realCost != null ? s.realCost : s.real),
|
|
1386
|
+
realCalls: num(s.realCalls != null ? s.realCalls : s.calls),
|
|
1387
|
+
realTokens: num(s.realTokens != null ? s.realTokens : s.tokens),
|
|
1388
|
+
subEquivalent: num(s.subEquivalent != null ? s.subEquivalent : s.sub),
|
|
1389
|
+
subCalls: num(s.subCalls),
|
|
1390
|
+
peakCost: num(s.peakCost), offCost: num(s.offCost), flatCost: num(s.flatCost),
|
|
1330
1391
|
devices: c.devices, sources: c.sources, asOf: c.asOf,
|
|
1331
1392
|
}
|
|
1332
1393
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angelyeye/dsh-cost-tracker",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.12",
|
|
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",
|
package/view.js
CHANGED
|
@@ -179,6 +179,61 @@ export function mergeDash(local, cloud) {
|
|
|
179
179
|
});
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
// ------------------------------------------------------------
|
|
183
|
+
// 「Token 用量统计」热力图的跨视图合并
|
|
184
|
+
//
|
|
185
|
+
// 形状与宿主 buildUsageHeat() 的返回值一致:
|
|
186
|
+
// { ok, days: [{date, input, output, cacheRead, cacheWrite, calls, cost, tokens}],
|
|
187
|
+
// total: {tokens, input, cache, output, calls, cost} }
|
|
188
|
+
//
|
|
189
|
+
// 两条口径约定(否则合并后屏内自相矛盾):
|
|
190
|
+
// · 按天 tokens 一律重算为 input+output+cacheRead+cacheWrite —— 本地 buildUsageHeat
|
|
191
|
+
// 就是这个口径;云端 byDay 的 tokens 含 reasoning,直接相加会与本地不同源。
|
|
192
|
+
// · total 用两份数据的**全时段** total 相加,而不是对 days 求和:本地 days 只保留最近
|
|
193
|
+
// 27 周,云端 days 覆盖全部历史,只有各自的 total 才是真正的「累计」。
|
|
194
|
+
// ------------------------------------------------------------
|
|
195
|
+
export function mergeUsageHeat(local, cloud) {
|
|
196
|
+
if (!cloud || !Array.isArray(cloud.days)) return local || null;
|
|
197
|
+
if (!local || !Array.isArray(local.days)) return cloud;
|
|
198
|
+
const per = new Map();
|
|
199
|
+
const add = (d) => {
|
|
200
|
+
if (!d || !d.date) return;
|
|
201
|
+
const cur = per.get(d.date) || { date: d.date, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, calls: 0, cost: 0, tokens: 0 };
|
|
202
|
+
cur.input += d.input || 0;
|
|
203
|
+
cur.output += d.output || 0;
|
|
204
|
+
cur.cacheRead += d.cacheRead || 0;
|
|
205
|
+
cur.cacheWrite += d.cacheWrite || 0;
|
|
206
|
+
cur.calls += d.calls || 0;
|
|
207
|
+
cur.cost += d.cost || 0;
|
|
208
|
+
cur.tokens = cur.input + cur.output + cur.cacheRead + cur.cacheWrite;
|
|
209
|
+
per.set(d.date, cur);
|
|
210
|
+
};
|
|
211
|
+
for (const d of local.days) add(d);
|
|
212
|
+
for (const d of cloud.days) add(d);
|
|
213
|
+
const days = Array.from(per.values())
|
|
214
|
+
.sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0))
|
|
215
|
+
.map((d) => Object.assign({}, d, { cost: r4(d.cost) }));
|
|
216
|
+
const lt = local.total || {};
|
|
217
|
+
const ct = cloud.total || {};
|
|
218
|
+
const n = (v) => Number(v) || 0;
|
|
219
|
+
return {
|
|
220
|
+
ok: true,
|
|
221
|
+
source: "merged",
|
|
222
|
+
days,
|
|
223
|
+
total: {
|
|
224
|
+
tokens: n(lt.tokens) + n(ct.tokens),
|
|
225
|
+
input: n(lt.input) + n(ct.input),
|
|
226
|
+
cache: n(lt.cache) + n(ct.cache),
|
|
227
|
+
output: n(lt.output) + n(ct.output),
|
|
228
|
+
calls: n(lt.calls) + n(ct.calls),
|
|
229
|
+
cost: r4(n(lt.cost) + n(ct.cost)),
|
|
230
|
+
},
|
|
231
|
+
asOf: cloud.asOf || 0,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function r4(x) { return Math.round((Number(x) || 0) * 10000) / 10000; }
|
|
236
|
+
|
|
182
237
|
// ------------------------------------------------------------
|
|
183
238
|
// 浏览器 bundle 注册(client.js 通过 require("./view") 取用)
|
|
184
239
|
// ------------------------------------------------------------
|
|
@@ -187,7 +242,7 @@ if (typeof window !== "undefined" && window.__ModuleLoader__ && typeof window.__
|
|
|
187
242
|
window.__ModuleLoader__.load({
|
|
188
243
|
id: "@angelyeye/dsh-cost-tracker/view",
|
|
189
244
|
factory: () => ({
|
|
190
|
-
BOARD_VIEWS, BOARD_DIMS, zeroSlice, addSlice, r2, normalizeCloudDash, mergeDash, cloudSlices,
|
|
245
|
+
BOARD_VIEWS, BOARD_DIMS, zeroSlice, addSlice, r2, normalizeCloudDash, mergeDash, cloudSlices, mergeUsageHeat,
|
|
191
246
|
}),
|
|
192
247
|
});
|
|
193
248
|
}
|