@angelyeye/dsh-cost-tracker 1.8.7 → 1.8.8

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,30 @@
2
2
 
3
3
  本文件用中文记录 dsh-cost-tracker 的版本变更。
4
4
 
5
+ ## v1.8.8(2026-09-15)
6
+
7
+ **修复:「仅云端」视图只有请求次数、费用整列为 ¥0.0000(字段名未映射)**
8
+
9
+ - **现场**:看板切到「仅云端」后,三张金额卡与底部汇总全为 `¥0.0000`,而「API 请求次数 / Tokens」正常(如 4,271 次、712.6M)。这个「次数对、金额 0」的组合直接指向字段名不匹配。
10
+ - **根因**:云端 `/api/v1/overview` 的按量金额字段是 **`realCost`**、订阅等效是 **`subEquivalent`**,而本地 `buildDashboard` 与所有卡片读的是 **`real` / `sub`**。客户端的 `normalizeCloudDash`(与内联兜底 `vNormalizeCloudDash`)当时只把 `today/month/all` 原样透传,`summary` 更是整个丢掉 —— 于是金额全 0,`calls/tokens` 因两边同名而幸存。**测试里那份云端假数据误用了 `real/sub`,把缺陷掩盖了**(已改为与线上一致的 `realCost` 形状)。
11
+ - **修法**:`view.js` 新增 `cloudSlices()` 做**逐项字段映射**(同时接受 `realCost/real`、`subCost/subEquivalent/sub` 三种写法,向后兼容旧云端),客户端内联兜底同步实现;`summary` 行按 `realCost → real`、`subEquivalent → sub` 映射,缺字段时回退到 `all` 的对应值。归一化后同时保证 `real + sub` 与 `calls + subCalls` 口径与本地一致。
12
+
13
+ **新增:云端「插件形状」只读接口,仅云端的图表不再为空**
14
+
15
+ - 客户端要画消费柱状图、分模型明细与最近记录,需要 `byDay / byModel / byModelDay / recent`,而 `/api/v1/overview` 只给概览卡片 —— 「仅云端」的图表此前注定为空。
16
+ - 云端新增 **`GET /api/v1/plugin-view`**(设备令牌可读,`caps.devicePluginView=true` 声明),字段名与本地 `buildDashboard` **逐项一致**,从根上消除这层适配;同时支持 `union` 并集(「本机+云端」)。
17
+ - 插件探测 `/api/v1/health` 的 caps 后**优先走 plugin-view**,旧云端自动回退 `overview`(卡片可用、图表为空,不再整体不可用);带 `union` 时仍走 overview(其卡片保持全网口径,与并集相加语义一致)。
18
+ - 顺带修掉云端既有的**同源缺陷**:`pluginView()` 的 today/month/all 原取自 `totalsUnfiltered`(无条件下全表),会**绕过 `excludeDevice`** —— 「本机+云端」相加时本机被计入两次。现改为按同一过滤条件取切片,且 `calls/tokens` 只含按量、订阅另计(`subCalls/subTokens`),与本地口径对齐。
19
+
20
+ **测试**
21
+
22
+ - 插件 `test/client-render.test.js` 的云端假数据改为**线上真实形状**,新增 `[5] 仅云端金额映射`用例:断言今日 ¥0.50、总花费 ¥2.00、不再出现 `¥0.0000`、汇总行按 `realCost` 口径,且内联兜底与 `view.js` 结果逐字一致。
23
+ - 新增 `test/cloud-view-e2e.test.js`:真实启动 `dsh-cost-cloud` 实例 → 设备令牌读 `plugin-view` → 过 `view.js` 归一化 → 断言卡片字段(含向后兼容旧概览口径)。
24
+ - 新增 `test/cloud-read.test.js` 契约断言:概览优先 plugin-view、并集仍走 overview、不得回退到 `/api/admin`。
25
+ - 云端新增 `test/plugin-view.test.js`(6 项):caps 声明、字段形状、三切片受过滤约束、union 不重复计数、鉴权与开关、`range=all` 日期轴覆盖数据起点。
26
+
27
+ **兼容**:无数据格式变更。云端需部署 **1.2.0** 及以上才有 `plugin-view`(未部署时插件自动回退,金额修复本身不依赖云端升级)。
28
+
5
29
  ## v1.8.7(2026-09-15)
6
30
 
7
31
  **修复:`cost_recompute` 的默认范围只覆盖「最近一个价格时代」,更早的陈旧记录被静默跳过**
package/README.md CHANGED
@@ -238,6 +238,19 @@ POST /api/cost-tracker/export 导出 CSV
238
238
 
239
239
  ## 更新记录
240
240
 
241
+ ### v1.8.8(2026-09-15)
242
+
243
+ **修复:「仅云端」视图只有次数、费用整列 ¥0.0000**
244
+
245
+ - 云端按量金额字段是 `realCost`、订阅等效是 `subEquivalent`,本地卡片读的是 `real` / `sub`;客户端归一化只透传原字段(`summary` 甚至整个丢掉),于是金额全 0、`calls/tokens` 因同名而正常。**测试里的云端假数据误用了 `real/sub`,把缺陷掩盖了**,现已改为线上真实形状。
246
+ - `view.js` 新增 `cloudSlices()` 逐项映射(兼容 `realCost/real`、`subCost/subEquivalent/sub` 三种写法),`summary` 行同步映射。
247
+
248
+ **新增:云端 `GET /api/v1/plugin-view`(设备令牌可读),让「仅云端」也能画图**
249
+
250
+ - 该接口返回与本地 `buildDashboard` **同字段名**的完整形状(`today/month/all` + `byDay/byModel/byModelDay/recent`),并支持 `union` 并集。插件探测 `/api/v1/health` 的 caps 后优先使用,旧云端自动回退 `overview`(金额可用、图表为空)。
251
+ - 顺带修掉云端同源缺陷:`pluginView` 的今日/本月/总花费原取自全表无过滤统计,**绕过了 `excludeDevice`**(「本机+云端」会把本机算两次);现按同一过滤条件取切片,`calls/tokens` 只含按量、订阅另计。
252
+ - 需云端 **1.2.0+** 才有该接口;未部署时插件自动回退,金额修复不依赖云端升级。
253
+
241
254
  ### v1.8.7(2026-09-15)
242
255
 
243
256
  **修复:`cost_recompute` 默认只扫描「最近一个价格时代」,更早的陈旧记录被静默跳过**
package/client.js CHANGED
@@ -525,15 +525,65 @@ window.__ModuleLoader__.load({
525
525
  subTokens: (x.subTokens || 0) + (y.subTokens || 0),
526
526
  };
527
527
  }
528
- /** 把云端返回补齐成本地 buildDashboard 形状(缺字段一律 0,避免渲染时报错) */
528
+ /** 把云端返回补齐成本地 buildDashboard 形状(缺字段一律 0,避免渲染时报错)
529
+ *
530
+ * ⚠️ 字段名必须**逐项映射**,不能只靠 Object.assign 透传:
531
+ * · 云端 overview/plugin-view 的按量金额叫 `realCost`、订阅等效叫 `subEquivalent`,
532
+ * 而本地 buildDashboard 与所有卡片读的是 `real` / `sub`;
533
+ * · 归一化函数此前把整个对象重建,只搬运 today/month/all/byDay/byModel/…,
534
+ * 于是 `summary.realCost`、`today.real` 一起丢掉 —— 卡片与底部汇总行双双显示
535
+ * ¥0.0000,而「调用次数 / Tokens」因为两边同名(calls/tokens)显示正常。
536
+ * 这个组合(次数对、金额 0)就是「仅云端只有次数没有费用」的根因。
537
+ */
538
+ function vCloudSlices(cloud) {
539
+ // 概览口径:realCost/subEquivalent;插件形状口径:real/sub。两种都接受。
540
+ const pick = (s) => {
541
+ if (!s) return null;
542
+ return Object.assign(vZeroSlice(), s, {
543
+ real: Number(s.real != null ? s.real : (s.realCost != null ? s.realCost : 0)) || 0,
544
+ sub: Number(s.sub != null ? s.sub : (s.subCost != null ? s.subCost : (s.subEquivalent != null ? s.subEquivalent : 0))) || 0,
545
+ calls: Number(s.calls) || 0,
546
+ tokens: Number(s.tokens) || 0,
547
+ subCalls: Number(s.subCalls) || 0,
548
+ subTokens: Number(s.subTokens) || 0,
549
+ });
550
+ };
551
+ const all = pick(cloud.all) || vZeroSlice();
552
+ const allRaw = cloud.all || {};
553
+ const summary = cloud.summary || {};
554
+ const summaryMapped = {
555
+ real: Number(summary.real != null ? summary.real : (summary.realCost != null ? summary.realCost : all.real)) || 0,
556
+ realCalls: Number(summary.realCalls != null ? summary.realCalls : (allRaw.calls != null ? allRaw.calls : all.calls)) || 0,
557
+ realTokens: Number(summary.realTokens != null ? summary.realTokens : (allRaw.tokens != null ? allRaw.tokens : all.tokens)) || 0,
558
+ sub: Number(summary.sub != null ? summary.sub : (summary.subEquivalent != null ? summary.subEquivalent : summary.subCost != null ? summary.subCost : all.sub)) || 0,
559
+ subCalls: Number(summary.subCalls != null ? summary.subCalls : all.subCalls) || 0,
560
+ subTokens: Number(summary.subTokens != null ? summary.subTokens : all.subTokens) || 0,
561
+ cost: Number(summary.cost != null ? summary.cost : all.real + all.sub) || 0,
562
+ calls: Number(summary.calls != null ? summary.calls : all.calls + all.subCalls) || 0,
563
+ tokens: Number(summary.tokens != null ? summary.tokens : all.tokens + all.subTokens) || 0,
564
+ peakCost: Number(summary.peakCost != null ? summary.peakCost : 0) || 0,
565
+ offCost: Number(summary.offCost != null ? summary.offCost : 0) || 0,
566
+ flatCost: Number(summary.flatCost != null ? summary.flatCost : 0) || 0,
567
+ driftAbs: Number(summary.driftAbs) || 0,
568
+ estimatedRows: Number(summary.estimatedRows) || 0,
569
+ input: Number(summary.input) || 0,
570
+ output: Number(summary.output) || 0,
571
+ cacheRead: Number(summary.cacheRead) || 0,
572
+ cacheWrite: Number(summary.cacheWrite) || 0,
573
+ reasoning: Number(summary.reasoning) || 0,
574
+ };
575
+ return { today: pick(cloud.today) || vZeroSlice(), month: pick(cloud.month) || vZeroSlice(), all, summary: summaryMapped };
576
+ }
529
577
  function vNormalizeCloudDash(cloud, fallbackDays) {
530
578
  if (!cloud || cloud.ok === false) return null;
531
- const safe = (s) => Object.assign(vZeroSlice(), s || {});
579
+ const s = vCloudSlices(cloud);
580
+ // 最近记录:云端 recent 的字段名与本地一致({ts,time,...}),本地 mock 也走同一契约
532
581
  return Object.assign({}, cloud, {
533
582
  days: cloud.days || fallbackDays || 7,
534
- today: safe(cloud.today),
535
- month: safe(cloud.month),
536
- all: safe(cloud.all),
583
+ today: s.today,
584
+ month: s.month,
585
+ all: s.all,
586
+ summary: s.summary,
537
587
  byDay: cloud.byDay || [],
538
588
  byModel: cloud.byModel || [],
539
589
  byModelDay: (cloud.byModelDay || []).map((m) => Object.assign({}, m, {
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.7'
22
+ const PLUGIN_VERSION = '1.8.8'
23
23
  setPluginVersion(PLUGIN_VERSION)
24
24
 
25
25
  /** 「设置 → 插件 → 插件配置」里的卡片字段(与 settings 命名空间一致) */
@@ -953,6 +953,23 @@ export default {
953
953
  }
954
954
 
955
955
  const deviceNameCache = { at: 0, list: [] }
956
+ /** 云端能力探测(/api/v1/health 的 caps,缓存 60s):决定能否用「插件形状」只读接口 */
957
+ const cloudCapsCache = { at: 0, ok: false, pluginView: false }
958
+ async function cloudCaps() {
959
+ if (Date.now() - cloudCapsCache.at < 60000) return cloudCapsCache
960
+ cloudCapsCache.at = Date.now()
961
+ cloudCapsCache.ok = false
962
+ cloudCapsCache.pluginView = false
963
+ try {
964
+ const res = await fetch(cloudConfig.cloudUrl + '/api/v1/health', { headers: { authorization: 'Bearer ' + cloudConfig.cloudToken } })
965
+ const body = await res.json()
966
+ if (body && body.ok === true) {
967
+ cloudCapsCache.ok = true
968
+ cloudCapsCache.pluginView = !!(body.caps && body.caps.devicePluginView)
969
+ }
970
+ } catch (e) { /* 探测失败按「旧云端」处理,走 overview 回退 */ }
971
+ return cloudCapsCache
972
+ }
956
973
  async function listCloudDevices() {
957
974
  if (Date.now() - deviceNameCache.at < 30000 && deviceNameCache.list.length) return deviceNameCache.list
958
975
  try {
@@ -989,7 +1006,8 @@ export default {
989
1006
  if (mode !== 'cloud' && !(query.devices && query.devices.length)) {
990
1007
  // 识别「本机」:设备 ID 命中,或设备名与插件里配置的一致(用户改名后 id 可能不同)
991
1008
  const all = await listCloudDevices()
992
- const myId = (st && st.deviceId) || ''
1009
+ let myId = (st && st.deviceId) || ''
1010
+ if (!myId) { try { myId = loadIdentity().machineId } catch (e) { myId = '' } }
993
1011
  const myName = ((st && st.deviceName) || '').trim()
994
1012
  const isSelf = (d) => (myId && d.id === myId) || (myName && (d.name || '').trim() === myName)
995
1013
  const self = all.filter(isSelf)
@@ -1016,7 +1034,20 @@ export default {
1016
1034
  // 只读聚合走 /api/v1/*(设备令牌可读)。/api/admin/* 只认管理员会话 cookie,
1017
1035
  // 采集端手里只有设备令牌,走那条必然 401 —— 这是 1.8.0~1.8.2 里「仅云端 /
1018
1036
  // 本机+云端」拿不到数据的根因。需要 dsh-cost-cloud ≥ 支持 /api/v1 只读接口的版本。
1019
- const url = cloudConfig.cloudUrl + '/api/v1/' + query.route + '?' + qs.toString()
1037
+ //
1038
+ // 端点选择:
1039
+ // · 概览路由在云端支持 /api/v1/plugin-view 时优先用它 —— 它一次返回**卡片 + 图表**
1040
+ // 的全套字段(today/month/all/byDay/byModel/byModelDay/recent),「仅云端」视图
1041
+ // 才能渲染消费柱状图、分模型明细与最近记录;旧云端没有该接口时回退 overview
1042
+ // (卡片可用,图表为空),保证不因云端版本差异而整体不可用。
1043
+ // · 带 union(「本机+云端」并集)时沿用 overview:其卡片是**全网**口径,与并集相加的
1044
+ // 语义一致;plugin-view 的卡片落在并集范围内,换成它会让总额口径突变。
1045
+ let endpoint = query.route
1046
+ if (query.route === 'overview' && !unionParts) {
1047
+ const caps = await cloudCaps()
1048
+ if (caps.pluginView) endpoint = 'plugin-view'
1049
+ }
1050
+ const url = cloudConfig.cloudUrl + '/api/v1/' + endpoint + '?' + qs.toString()
1020
1051
  const ac = new AbortController()
1021
1052
  const t = setTimeout(() => ac.abort(), 12000)
1022
1053
  try {
@@ -1024,7 +1055,7 @@ export default {
1024
1055
  const body = await res.json().catch(() => null)
1025
1056
  if (!body || body.ok !== true) {
1026
1057
  const hint = res.status === 404
1027
- ? '云端版本过旧:缺少设备只读接口 /api/v1/' + query.route + ',请升级 dsh-cost-cloud 后重试'
1058
+ ? '云端版本过旧:缺少设备只读接口 /api/v1/' + endpoint + ',请升级 dsh-cost-cloud 后重试'
1028
1059
  : null
1029
1060
  const out = { ok: false, error: hint || (body && body.error) || ('HTTP ' + res.status), code: (body && body.code) || (res.status === 404 ? 'CLOUD_TOO_OLD' : 'CLOUD_ERROR') }
1030
1061
  return out
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angelyeye/dsh-cost-tracker",
3
- "version": "1.8.7",
3
+ "version": "1.8.8",
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 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/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",
package/view.js CHANGED
@@ -43,15 +43,68 @@ export function addSlice(a, b) {
43
43
  };
44
44
  }
45
45
 
46
- /** 把云端返回补齐成本地 buildDashboard 形状(缺字段一律 0,避免渲染时报错) */
46
+ /**
47
+ * 云端切片的字段名映射:概览口径用 `realCost` / `subEquivalent`,
48
+ * 插件形状口径(/api/v1/plugin-view)用 `real` / `sub`,这里统一成本地口径。
49
+ * 缺字段一律 0,避免渲染时报错。
50
+ */
51
+ export function cloudSlices(cloud) {
52
+ const pick = (s) => {
53
+ if (!s) return null;
54
+ return Object.assign(zeroSlice(), s, {
55
+ real: Number(s.real != null ? s.real : (s.realCost != null ? s.realCost : 0)) || 0,
56
+ sub: Number(s.sub != null ? s.sub : (s.subCost != null ? s.subCost : (s.subEquivalent != null ? s.subEquivalent : 0))) || 0,
57
+ calls: Number(s.calls) || 0,
58
+ tokens: Number(s.tokens) || 0,
59
+ subCalls: Number(s.subCalls) || 0,
60
+ subTokens: Number(s.subTokens) || 0,
61
+ });
62
+ };
63
+ const all = pick(cloud.all) || zeroSlice();
64
+ const allRaw = cloud.all || {};
65
+ const summary = cloud.summary || {};
66
+ return {
67
+ today: pick(cloud.today) || zeroSlice(),
68
+ month: pick(cloud.month) || zeroSlice(),
69
+ all,
70
+ summary: {
71
+ real: Number(summary.real != null ? summary.real : (summary.realCost != null ? summary.realCost : all.real)) || 0,
72
+ realCalls: Number(summary.realCalls != null ? summary.realCalls : (allRaw.calls != null ? allRaw.calls : all.calls)) || 0,
73
+ realTokens: Number(summary.realTokens != null ? summary.realTokens : (allRaw.tokens != null ? allRaw.tokens : all.tokens)) || 0,
74
+ sub: Number(summary.sub != null ? summary.sub : (summary.subEquivalent != null ? summary.subEquivalent : summary.subCost != null ? summary.subCost : all.sub)) || 0,
75
+ subCalls: Number(summary.subCalls != null ? summary.subCalls : all.subCalls) || 0,
76
+ subTokens: Number(summary.subTokens != null ? summary.subTokens : all.subTokens) || 0,
77
+ cost: Number(summary.cost != null ? summary.cost : all.real + all.sub) || 0,
78
+ calls: Number(summary.calls != null ? summary.calls : all.calls + all.subCalls) || 0,
79
+ tokens: Number(summary.tokens != null ? summary.tokens : all.tokens + all.subTokens) || 0,
80
+ peakCost: Number(summary.peakCost) || 0,
81
+ offCost: Number(summary.offCost) || 0,
82
+ flatCost: Number(summary.flatCost) || 0,
83
+ driftAbs: Number(summary.driftAbs) || 0,
84
+ estimatedRows: Number(summary.estimatedRows) || 0,
85
+ input: Number(summary.input) || 0,
86
+ output: Number(summary.output) || 0,
87
+ cacheRead: Number(summary.cacheRead) || 0,
88
+ cacheWrite: Number(summary.cacheWrite) || 0,
89
+ reasoning: Number(summary.reasoning) || 0,
90
+ },
91
+ };
92
+ }
93
+
94
+ /** 把云端返回补齐成本地 buildDashboard 形状(缺字段一律 0,避免渲染时报错)
95
+ *
96
+ * ⚠️ 必须**逐项映射金额字段**:云端叫 `realCost` / `subEquivalent`,本地卡片读 `real` / `sub`。
97
+ * 此前只透传 today/month/all 的原字段,金额显示 ¥0.0000(次数因同名而正常)——
98
+ * 这就是「仅云端只有次数没有费用」的根因。 */
47
99
  export function normalizeCloudDash(cloud, fallbackDays) {
48
100
  if (!cloud || cloud.ok === false) return null;
49
- const safe = (s) => Object.assign(zeroSlice(), s || {});
101
+ const s = cloudSlices(cloud);
50
102
  return Object.assign({}, cloud, {
51
103
  days: cloud.days || fallbackDays || 7,
52
- today: safe(cloud.today),
53
- month: safe(cloud.month),
54
- all: safe(cloud.all),
104
+ today: s.today,
105
+ month: s.month,
106
+ all: s.all,
107
+ summary: s.summary,
55
108
  byDay: cloud.byDay || [],
56
109
  byModel: cloud.byModel || [],
57
110
  byModelDay: (cloud.byModelDay || []).map((m) => Object.assign({}, m, {
@@ -134,7 +187,7 @@ if (typeof window !== "undefined" && window.__ModuleLoader__ && typeof window.__
134
187
  window.__ModuleLoader__.load({
135
188
  id: "@angelyeye/dsh-cost-tracker/view",
136
189
  factory: () => ({
137
- BOARD_VIEWS, BOARD_DIMS, zeroSlice, addSlice, r2, normalizeCloudDash, mergeDash,
190
+ BOARD_VIEWS, BOARD_DIMS, zeroSlice, addSlice, r2, normalizeCloudDash, mergeDash, cloudSlices,
138
191
  }),
139
192
  });
140
193
  }