@angelyeye/dsh-cost-tracker 1.9.3 → 1.9.5
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 +135 -0
- package/README.en.md +56 -0
- package/README.md +48 -0
- package/client.js +36 -3
- package/credstore.js +105 -2
- package/index.js +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,141 @@
|
|
|
2
2
|
|
|
3
3
|
本文件用中文记录 dsh-cost-tracker 的版本变更。
|
|
4
4
|
|
|
5
|
+
## v1.9.5(2026-10-07)
|
|
6
|
+
|
|
7
|
+
**修复「桌面端(Electron 外壳)花费统计整块看板变成无样式原生控件」—— 自建 `<style>` 缺 `data-plugin` 归属标记,被 DSH 客户端模块系统认领给别的模块后在 HMR 清理时连带删除**
|
|
8
|
+
|
|
9
|
+
### 一、根因(已复现并逐帧比对)
|
|
10
|
+
|
|
11
|
+
DSH 0.2 的客户端模块系统 `@deepseek-ai/dsh-client-modules/lib/client.js` 用两个函数管理插件样式表:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// 物化(materialize)一个模块的 factory 之后立刻盘点样式
|
|
15
|
+
const claimStyles = (id) => {
|
|
16
|
+
for (const el of document.querySelectorAll("style:not([data-plugin])")) el.setAttribute("data-plugin", id); // ← 无主的全认领
|
|
17
|
+
...
|
|
18
|
+
};
|
|
19
|
+
// 某个模块被 HMR 失效 / 重载 / 剪枝时清理它的样式
|
|
20
|
+
function removeOwnedStyles(id) {
|
|
21
|
+
for (const el of document.querySelectorAll("style[data-plugin]")) if (el.getAttribute("data-plugin") === id) el.remove();
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**没打 `data-plugin` 的 `<style>` 会被「当时正在物化的那个模块」认领走。** 插件旧实现只打了
|
|
26
|
+
`data-plugin-css`,于是自己的样式表被登记在别的模块名下(本机实测被
|
|
27
|
+
`@deepseek-ai/dsh-api-remotes` 认领)。该模块一旦走到 `removeOwnedStyles(id)`
|
|
28
|
+
(HMR 清单里 `rev` 变化 → `updateManifest` 的 invalidate 分支、`reload()` 的非托管分支、
|
|
29
|
+
entry 重载、`prune()` 剪枝、factory 抛错的 catch),**连带把整块花费统计看板的 170 条
|
|
30
|
+
CSS 规则一起删掉**,界面退化成浏览器默认的 `<select>`/`<button>`/复选框排版:卡片边框、
|
|
31
|
+
38 处自定义控件皮肤、图表网格与图例全部消失。
|
|
32
|
+
|
|
33
|
+
为什么只在桌面端出现:Electron 外壳把 `apps/web` 的**原始 vite dist** 从
|
|
34
|
+
`dsh-app://app/` 提供(`resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-web-frontend/dist`),
|
|
35
|
+
启动图(`__ModuleLoader__` 队列引导、插件 `<link rel=preload>`、`__DSH_CONTACT_CONFIG__`)
|
|
36
|
+
改由 IPC 取回 `ctx.webServer.collectIndexInjections()` 后**在运行期注入**;`dsh-client-hmr`
|
|
37
|
+
随后推送的清单会让若干非托管行的 `rev` 与初始值不一致,触发上面那条清理路径。
|
|
38
|
+
浏览器里的 `dsh web` 是宿主直接把启动图内联进 index.html,物化顺序不同、清单 rev 与本次启动一致,
|
|
39
|
+
所以看不到 —— 这也解释了「同一个插件、同一份 client.js,网页版正常、桌面端无样式」。
|
|
40
|
+
|
|
41
|
+
**归属标记在插入前就要写好**:`claimStyles` 只认「当前是否已带 `data-plugin`」,
|
|
42
|
+
插入后再补标会留下一个未被认领的窗口。
|
|
43
|
+
|
|
44
|
+
### 二、修法(`client.js`)
|
|
45
|
+
|
|
46
|
+
1. 新增模块 id 单一事实源 `var DSH_BUNDLE_ID = "@angelyeye/dsh-cost-tracker"`,
|
|
47
|
+
`__ModuleLoader__.load({ id })` 与样式归属标记共用它;
|
|
48
|
+
2. `applyStyles()` 改为**先打标再插入**:
|
|
49
|
+
```js
|
|
50
|
+
tag.setAttribute("data-plugin", DSH_BUNDLE_ID);
|
|
51
|
+
tag.setAttribute("data-plugin-css", DSH_STYLE_TAG_ID);
|
|
52
|
+
```
|
|
53
|
+
与宿主自带包的写法(`tag.dataset.plugin = "<包名>"`)逐字对齐;
|
|
54
|
+
3. 顺带加上宿主同款去重护栏(`style[data-plugin-css="cost-tracker-plugin"]` 已存在就不再插第二张);
|
|
55
|
+
4. 文件末尾注册名护栏保留它**刻意独立**的字面量副本(若与 `DSH_BUNDLE_ID` 同源,
|
|
56
|
+
故障注入测试就永远测不出「注册名 ≠ 包名」了)。
|
|
57
|
+
|
|
58
|
+
### 三、验证
|
|
59
|
+
|
|
60
|
+
- 真机复现(桌面端 profile 的等价宿主 + 无头 Edge/CDP,1280×820,与用户截图同视口):
|
|
61
|
+
- 修复前:插件样式表 `data-plugin = "@deepseek-ai/dsh-api-remotes"`(被误认领),
|
|
62
|
+
模拟该模块的 `removeOwnedStyles` → 样式标签 1 张被删、看板立刻变成无样式原生控件,
|
|
63
|
+
与用户截图逐项一致;
|
|
64
|
+
- 修复后:`data-plugin = "@angelyeye/dsh-cost-tracker"`,同样触发
|
|
65
|
+
`@deepseek-ai/dsh-api-remotes` 的清理 → 标签存活、`sheet.cssRules.length = 170`、
|
|
66
|
+
6 张卡片仍在(`border-radius: 10px`、数值 `font-size: 24px`),截图正常。
|
|
67
|
+
- 极端对照:把 47 个无关模块的 168 张样式全删,本插件看板依然保持有样式。
|
|
68
|
+
- `test/client-render.test.js` 新增 [11](7 条断言):恰好注入一张样式表、
|
|
69
|
+
`data-plugin` 等于 bundle 注册名、`data-plugin-css` 保留、正文含关键规则、
|
|
70
|
+
打标早于 `head.appendChild`、注册 id 与归属标记同源;
|
|
71
|
+
- `test/client-registration.test.js` 新增 [3b](5 条断言)钉住单一事实源与独立护栏副本,
|
|
72
|
+
故障注入改为改写 `DSH_BUNDLE_ID`(同时覆盖注册与归属标记两个消费点);
|
|
73
|
+
- 全量 19 个测试文件通过。
|
|
74
|
+
|
|
75
|
+
### 四、说明
|
|
76
|
+
|
|
77
|
+
- 只改客户端半端 `client.js`:不动任何计费/同步口径,HTTP 面、配置键、落盘路径、云端契约零改动;
|
|
78
|
+
- 客户端半端换版后**浏览器硬刷新**即可生效;另需确认宿主半端也已是新版(`index.js`/`PLUGIN_VERSION`);
|
|
79
|
+
- 同生态插件若自建 `<style>` 且只打 `data-plugin-css`,会踩同一个坑(本仓库的 `dshmarket` 已按约定打标)。
|
|
80
|
+
|
|
81
|
+
## v1.9.4(2026-09-24)
|
|
82
|
+
|
|
83
|
+
**修复「配额查询不可用:火山方舟配额响应不是合法 JSON(GetCodingPlanUsage)」—— 宿主全局 fetch 被 undici 包污染后不解压 gzip,插件改为自带解压兜底**
|
|
84
|
+
|
|
85
|
+
### 一、根因(已在本机复现并定位到机制)
|
|
86
|
+
|
|
87
|
+
DSH 宿主进程里只要有插件 `import` 了 **npm 的 `undici` 包**,Node **内置 `fetch`** 就不再解压
|
|
88
|
+
gzip:响应**既没有 `content-encoding`,正文也是原始压缩字节**。本机引入方:`dshmarket/lib/net.js`
|
|
89
|
+
(静态 import)、`dsh-http-proxy` 与 `dsh-web-fetch-http`(动态 import)。
|
|
90
|
+
|
|
91
|
+
矩阵实测(同一进程、同一凭据、真实接口):
|
|
92
|
+
|
|
93
|
+
| 阶段 | npmjs `/latest` | 火山 `GetCodingPlanUsage` |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| 纯 Node(未 import undici) | 200 · `ce=gzip` · 3055B · JSON OK | 200 · `ce=gzip` · 560B · 解析 OK(三档窗口) |
|
|
96
|
+
| `await import('undici')` 之后(不装 dispatcher 也一样) | 200 · `ce=null` · 1506B · **原始 gzip(`1f8b`)· FAIL** | 200 · `ce=null` · 326B · **原始 gzip · FAIL** |
|
|
97
|
+
|
|
98
|
+
两条现场症状同源:
|
|
99
|
+
- 本插件配额面板:`safeFetch` → 全局 fetch → 拿到压缩字节 → `JSON.parse` 失败 → 面板报
|
|
100
|
+
「配额查询不可用:火山方舟配额响应不是合法 JSON(GetCodingPlanUsage)」;
|
|
101
|
+
- 插件市场把所有包的新版本读成 `null`(它的 `marketFetch` 在无代理时回退的也是全局 fetch,
|
|
102
|
+
日志里的 `… is not valid JSON` 就是同一形态的乱码)。
|
|
103
|
+
|
|
104
|
+
补充对照:`web_fetch` 工具一直正常,因为它用的是 undici 包自己的 `fetch`(解压正常);
|
|
105
|
+
`content-encoding: gzip` 存在时同样会中招 —— 关键在「谁解压」,不在响应长什么样。
|
|
106
|
+
|
|
107
|
+
### 二、修法(两层,都在 `credstore.js` 的出站封套里,对调用方透明)
|
|
108
|
+
|
|
109
|
+
1. **请求侧**:`safeFetch` 默认补 `accept-encoding: identity`,从源头要未压缩正文
|
|
110
|
+
(调用方显式指定过就尊重调用方);
|
|
111
|
+
2. **响应侧**:新增 `decodeCompressedBody()` + `withDecodedBody()`,按**字节**判断(gzip `1f8b`、
|
|
112
|
+
zlib `78xx`、响应头明说的 `br`/`deflate`)自行解压,绝不只看响应头 —— 服务端忽略 `identity`
|
|
113
|
+
时同样能正确解析;解压异常一律**原样返回**,不因为猜测丢内容。
|
|
114
|
+
`withDecodedBody()` 只实现调用方真正用到的成员(`status`/`ok`/`url`/`redirected`/`headers`/
|
|
115
|
+
`text`/`json`/`arrayBuffer`),正文只读取一次并缓存,且对「只有 `text()` 的 fetch 替身」保持兼容。
|
|
116
|
+
|
|
117
|
+
影响面:插件全部出站请求都走 `safeFetch`(火山配额、Kimi 配额、云端同步与读取、官方定价页抓取),
|
|
118
|
+
因此这一处修复同时给这些路径兜住了同一个坑。
|
|
119
|
+
|
|
120
|
+
### 三、验证
|
|
121
|
+
|
|
122
|
+
- 真机端到端(先 `import('undici')` 污染进程,再走 `index.js` 的真实接线):
|
|
123
|
+
- 旧行为(裸全局 fetch)→ 报出与用户完全一致的错误;
|
|
124
|
+
- 修复后(`safeFetch` + 白名单)→ `action=GetCodingPlanUsage`,三档窗口
|
|
125
|
+
`fiveHour / weekly / monthly` 全部解析(实测 100% / 27.08% / 14.77%);
|
|
126
|
+
- `test/credstore.test.js` 新增 [5]:坏宿主(无 `content-encoding` 的 gzip 正文)可解析、
|
|
127
|
+
健康宿主(头写 gzip 但正文已解压)不二次解压、deflate 兜底、纯文本替身兼容、
|
|
128
|
+
非压缩内容原样返回、`accept-encoding` 默认值与调用方覆盖;
|
|
129
|
+
- `test/volcengine-host.test.js` 新增 [6]:端到端 —— 宿主返回未解压 gzip 时配额路由仍 `ok:true`、
|
|
130
|
+
三档窗口齐全、百分比不被放大、请求确带 `accept-encoding: identity`;
|
|
131
|
+
- 全量 18 个测试文件通过。
|
|
132
|
+
|
|
133
|
+
### 四、说明
|
|
134
|
+
|
|
135
|
+
- 这是**环境韧性**修复,不改变任何计费/同步口径:HTTP 面、配置键、落盘路径、云端契约零改动;
|
|
136
|
+
- 需要重启 `dsh web` 才生效(改动在宿主半端 `index.js`/`credstore.js`);
|
|
137
|
+
- 插件市场那条不属于本插件可控范围(其自身回退路径同样依赖全局 fetch),
|
|
138
|
+
可在 `dshmarket` 侧改为始终使用 undici 包的 fetch 或自行解压;本插件侧的同类风险已消除。
|
|
139
|
+
|
|
5
140
|
## v1.9.3(2026-09-24)
|
|
6
141
|
|
|
7
142
|
**适配 DSH 0.1.7-alpha.2:配置入口内迁到「花费统计」页头齿轮(带返回键),旧插件配置卡片入口保留兼容**
|
package/README.en.md
CHANGED
|
@@ -340,6 +340,53 @@ Example: `curl -X POST http://127.0.0.1:3080/api/cost-tracker/summary -d '{}'`
|
|
|
340
340
|
|
|
341
341
|
> Highlights only — the full version-by-version history lives in [`CHANGELOG.md`](./CHANGELOG.md) (Chinese).
|
|
342
342
|
|
|
343
|
+
### v1.9.5 (2026-10-07)
|
|
344
|
+
|
|
345
|
+
**Fix "the whole Cost dashboard renders as unstyled native controls in the Desktop (Electron) app": the plugin's own `<style>` lacked a `data-plugin` ownership tag, so another module claimed it and HMR cleanup deleted it along with its own styles**
|
|
346
|
+
|
|
347
|
+
- **Root cause (reproduced)**: DSH 0.2's `@deepseek-ai/dsh-client-modules` calls `claimStyles(ownerId)`,
|
|
348
|
+
which claims **every `<style>` that has no `data-plugin`** for whichever module is materializing at that
|
|
349
|
+
moment, and `removeOwnedStyles(id)` deletes all tags tagged `id` when that module is invalidated,
|
|
350
|
+
reloaded or pruned. The plugin only set `data-plugin-css`, so its stylesheet was claimed by
|
|
351
|
+
`@deepseek-ai/dsh-api-remotes` — and when that module's styles were torn down, all 170 dashboard CSS
|
|
352
|
+
rules went with it: card borders, control skins and chart grid vanished, leaving raw
|
|
353
|
+
`<select>`/`<button>` chrome;
|
|
354
|
+
- **Why Desktop only**: the Electron shell serves the raw `apps/web` vite dist from `dsh-app://app/` and
|
|
355
|
+
applies the boot graph (ModuleLoader bootstrap, plugin preloads, contact config) **at runtime** over IPC;
|
|
356
|
+
the manifest `dsh-client-hmr` then pushes gives non-managed rows a different `rev`, which is exactly what
|
|
357
|
+
triggers that teardown path. `dsh web` inlines the boot graph into index.html, so materialization order
|
|
358
|
+
and revisions differ and the bug never fires there;
|
|
359
|
+
- **Fix**: `client.js` now has a single source of truth `DSH_BUNDLE_ID`, and `applyStyles()` sets
|
|
360
|
+
`data-plugin` **before** appending the tag (byte-for-byte the convention the host's own packages use),
|
|
361
|
+
plus the host's duplicate-tag guard;
|
|
362
|
+
- **Verification**: desktop-equivalent host + headless Edge/CDP at the same viewport as the report — before
|
|
363
|
+
the fix the emulated teardown removed the stylesheet and the dashboard became native controls; after the
|
|
364
|
+
fix the tag survives, `cssRules.length = 170` and all 6 cards keep their styling. 12 new assertions in
|
|
365
|
+
`client-render` [11] and `client-registration` [3b]; all 19 test files pass;
|
|
366
|
+
- **How it takes effect**: client half only — a **hard refresh** is enough; the host half's version is
|
|
367
|
+
bumped to 1.9.5 as well.
|
|
368
|
+
|
|
369
|
+
### v1.9.4 (2026-09-24)
|
|
370
|
+
|
|
371
|
+
**Fix "Volcengine Ark quota response is not valid JSON": the host's global fetch stops decompressing gzip once the `undici` package is loaded, so the plugin now decodes bodies itself**
|
|
372
|
+
|
|
373
|
+
- **Root cause (reproduced locally)**: as soon as any plugin in the host process imports the npm `undici`
|
|
374
|
+
package (`dshmarket` does it statically here; `dsh-http-proxy` / `dsh-web-fetch-http` dynamically), Node's
|
|
375
|
+
**built-in** `fetch` no longer decompresses gzip — the response has no `content-encoding` and the body is
|
|
376
|
+
raw compressed bytes (measured: 560 B of plain JSON without the import, 326 B starting with `1f 8b` with it).
|
|
377
|
+
`JSON.parse(await res.text())` therefore always fails, which is exactly the quota-panel error; **the plugin
|
|
378
|
+
market reading every package's latest version as `null` shares the same root cause** (its no-proxy fallback
|
|
379
|
+
also uses the global fetch);
|
|
380
|
+
- **Fix (two layers, transparent to callers)**: `safeFetch` now sends `accept-encoding: identity` to ask for
|
|
381
|
+
uncompressed bodies, and after receiving the body it decompresses by **bytes** (gzip `1f8b` / zlib `78xx` /
|
|
382
|
+
`br`, `deflate` when the header says so); any decompression failure returns the original bytes untouched.
|
|
383
|
+
Every plugin egress (Volcengine/Kimi quota, cloud sync and reads, official pricing-page fetch) goes through
|
|
384
|
+
this wrapper, so all of them are covered;
|
|
385
|
+
- **Verification**: live end-to-end — with the process polluted by `import('undici')` the old path reproduces
|
|
386
|
+
the exact error, while the fixed path returns the three real windows (100% / 27.08% / 14.77%); new
|
|
387
|
+
`credstore` [5] and `volcengine-host` [6] regressions; all 18 test files pass;
|
|
388
|
+
- **Takes effect** after a `dsh web` restart (host-half change).
|
|
389
|
+
|
|
343
390
|
### v1.9.3 (2026-09-24)
|
|
344
391
|
|
|
345
392
|
**DSH 0.1.7-alpha.2 support: the configuration entry moved into the Cost Tracker page (gear icon + back control); the old card entry is kept for compatibility**
|
|
@@ -601,6 +648,15 @@ Example: `curl -X POST http://127.0.0.1:3080/api/cost-tracker/summary -d '{}'`
|
|
|
601
648
|
|
|
602
649
|
## FAQ
|
|
603
650
|
|
|
651
|
+
**Q: The quota panel says "Volcengine Ark quota response is not valid JSON (GetCodingPlanUsage)" — what now?**
|
|
652
|
+
Upgrade to **v1.9.4 or later** and restart `dsh web`. The cause is neither Volcengine nor your credentials:
|
|
653
|
+
once any plugin in the host process does `import('undici')` (on this machine `dshmarket` does), Node's
|
|
654
|
+
**built-in `fetch`** stops decompressing gzip — the response carries no `content-encoding` and the body is
|
|
655
|
+
raw compressed bytes, so `JSON.parse` always fails. From v1.9.4 the plugin decompresses bodies itself
|
|
656
|
+
inside its outbound wrapper and asks for `accept-encoding: identity` by default, so it is immune.
|
|
657
|
+
Related: **the plugin market showing no updates (every package "up to date") has the same root cause**, but
|
|
658
|
+
that path is owned by `dshmarket` itself and needs a fix there.
|
|
659
|
+
|
|
604
660
|
**Q: Where is my data? Is it safe?**
|
|
605
661
|
Everything stays on your machine in `~/.dsh/storages/cost-tracker-records.json`; nothing is uploaded. The API binds to loopback but has no authentication — **do not expose the DSH port to the public internet**.
|
|
606
662
|
|
package/README.md
CHANGED
|
@@ -341,6 +341,47 @@ POST /api/cost-tracker/export 导出 CSV
|
|
|
341
341
|
|
|
342
342
|
> 这里只列重要版本;逐版完整记录见 [`CHANGELOG.md`](./CHANGELOG.md)。
|
|
343
343
|
|
|
344
|
+
### v1.9.5(2026-10-07)
|
|
345
|
+
|
|
346
|
+
**修复「桌面端(Electron 外壳)花费统计整块看板变成无样式原生控件」:自建 `<style>` 缺 `data-plugin` 归属标记,被别的模块认领后在 HMR 清理时连带删除**
|
|
347
|
+
|
|
348
|
+
- **根因(已复现)**:DSH 0.2 的 `@deepseek-ai/dsh-client-modules` 用
|
|
349
|
+
`claimStyles(ownerId)` 把**所有没带 `data-plugin` 的 `<style>` 认领给当时正在物化的模块**,
|
|
350
|
+
再用 `removeOwnedStyles(id)` 在该模块 HMR 失效 / 重载 / 剪枝时整片删除。
|
|
351
|
+
插件旧实现只打了 `data-plugin-css`,实测样式表被 `@deepseek-ai/dsh-api-remotes` 认领,
|
|
352
|
+
该模块一被清理就带走看板的 170 条 CSS 规则 —— 卡片边框、控件皮肤、图表网格全部消失,
|
|
353
|
+
界面退化成浏览器默认的 `<select>`/`<button>` 排版;
|
|
354
|
+
- **为什么只在桌面端**:Electron 外壳把 `apps/web` 的原始 vite dist 从 `dsh-app://app/` 提供,
|
|
355
|
+
启动图(ModuleLoader 引导、插件 preload、contact config)改由 IPC 取回后在**运行期注入**,
|
|
356
|
+
随后 `dsh-client-hmr` 推送的清单让非托管行的 `rev` 与初始值不一致,正好命中那条清理路径;
|
|
357
|
+
浏览器里的 `dsh web` 把启动图内联在 index.html 里,物化顺序与 rev 都不同,所以看不到;
|
|
358
|
+
- **修法**:`client.js` 新增模块 id 单一事实源 `DSH_BUNDLE_ID`,`applyStyles()` 改为
|
|
359
|
+
**先 `setAttribute("data-plugin", DSH_BUNDLE_ID)` 再插入**(与宿主自带包 `tag.dataset.plugin = "<包名>"`
|
|
360
|
+
的约定逐字对齐),并补上宿主同款去重护栏;
|
|
361
|
+
- **验证**:桌面端等价宿主 + 无头 Edge/CDP(与用户截图同视口)—— 修复前模拟该模块清理后样式表被删、
|
|
362
|
+
看板变原生控件;修复后同样清理,标签存活、`cssRules.length = 170`、6 张卡片样式完好。
|
|
363
|
+
新增 `client-render` [11] 与 `client-registration` [3b] 共 12 条断言,全量 19 个测试文件通过;
|
|
364
|
+
- **生效条件**:只改客户端半端,**浏览器硬刷新**即可;宿主半端版本号同步升到 1.9.5。
|
|
365
|
+
|
|
366
|
+
### v1.9.4(2026-09-24)
|
|
367
|
+
|
|
368
|
+
**修复「火山方舟配额响应不是合法 JSON」:宿主全局 fetch 被 undici 包污染后不解压 gzip,插件改为自带解压兜底**
|
|
369
|
+
|
|
370
|
+
- **根因(本机复现)**:宿主进程里只要有插件 `import` 了 npm 的 `undici` 包(本机是
|
|
371
|
+
`dshmarket` 静态引入,另有 `dsh-http-proxy` / `dsh-web-fetch-http`),Node **内置** `fetch`
|
|
372
|
+
就不再解压 gzip —— 响应既没有 `content-encoding`,正文也是原始压缩字节
|
|
373
|
+
(实测:未 import 时 560B 明文 JSON;import 后 326B 且以 `1f 8b` 开头)。于是
|
|
374
|
+
`JSON.parse(await res.text())` 必然失败,面板报「配额查询不可用:火山方舟配额响应不是合法 JSON」;
|
|
375
|
+
**插件市场把所有包的新版本读成 `null` 也是同一个根因**(它无代理时回退用的同样是全局 fetch);
|
|
376
|
+
- **修法(两层,对调用方透明)**:`safeFetch` 默认补 `accept-encoding: identity` 从源头要未压缩正文;
|
|
377
|
+
收到正文后按**字节**判断(gzip `1f8b` / zlib `78xx` / 响应头明说的 `br`、`deflate`)自行解压,
|
|
378
|
+
解压异常一律原样返回。插件所有出站请求(火山/Kimi 配额、云端同步与读取、官方定价页抓取)
|
|
379
|
+
都走这里,因此一并兜住;
|
|
380
|
+
- **验证**:真机端到端 —— 先 `import('undici')` 污染进程,旧路径复现同一条错误,
|
|
381
|
+
修复后 `GetCodingPlanUsage` 正常返回三档窗口(实测 100% / 27.08% / 14.77%);
|
|
382
|
+
新增 `credstore` [5] 与 `volcengine-host` [6] 两组回归,全量 18 个测试文件通过;
|
|
383
|
+
- **生效条件**:改动在宿主半端,需重启 `dsh web`。
|
|
384
|
+
|
|
344
385
|
### v1.9.3(2026-09-24)
|
|
345
386
|
|
|
346
387
|
**适配 DSH 0.1.7-alpha.2:配置入口内迁到「花费统计」页头齿轮(旧卡片入口保留兼容)**
|
|
@@ -600,6 +641,13 @@ POST /api/cost-tracker/export 导出 CSV
|
|
|
600
641
|
|
|
601
642
|
## 常见问题
|
|
602
643
|
|
|
644
|
+
**Q:配额面板提示「火山方舟配额响应不是合法 JSON(GetCodingPlanUsage)」怎么办?**
|
|
645
|
+
先升级到 **v1.9.4 或更高**并重启 `dsh web`。根因不在火山侧,也不在凭据:宿主进程里只要有插件
|
|
646
|
+
`import` 了 npm 的 `undici` 包(本机是 `dshmarket`),Node **内置 `fetch`** 就不再解压 gzip ——
|
|
647
|
+
响应没有 `content-encoding`、正文是原始压缩字节,`JSON.parse` 必然失败。v1.9.4 起插件在出站封套里
|
|
648
|
+
自带解压(并默认请求 `accept-encoding: identity`),因此不再受影响。附带说明:**插件市场看不到新版本
|
|
649
|
+
(所有包都显示已是最新)是同一个根因**,但那条路径由 `dshmarket` 自己发起,需在其侧修复。
|
|
650
|
+
|
|
603
651
|
**Q:数据存在哪里?安全吗?**
|
|
604
652
|
全部数据只存在你本机的 `~/.dsh/storages/cost-tracker-records.json`,不会上传到任何服务器。API 只监听本机回环地址,但无鉴权——**不要把 DSH 端口暴露到公网**。
|
|
605
653
|
|
package/client.js
CHANGED
|
@@ -6,8 +6,28 @@
|
|
|
6
6
|
// React → require('react')
|
|
7
7
|
// 输出为浏览器 ModuleLoader bundle:exports.apply / exports.inject
|
|
8
8
|
// ============================================================
|
|
9
|
+
/**
|
|
10
|
+
* 单一事实源:本 bundle 的模块 id(= package.json 的 name)。
|
|
11
|
+
* 两处消费它:
|
|
12
|
+
* 1. `__ModuleLoader__.load({ id })` —— 加载器按模块图里的 row.id 取 bundle,
|
|
13
|
+
* 注册名必须逐字一致(见文件末尾的注册名护栏)。
|
|
14
|
+
* 2. 运行时注入的 <style> 上的 `data-plugin` 归属标记(见 applyStyles)。
|
|
15
|
+
* DSH 0.2 的客户端模块系统(@deepseek-ai/dsh-client-modules)用
|
|
16
|
+
* `claimStyles(ownerId)` 认领样式:「已带 data-plugin 的按标注归属,
|
|
17
|
+
* 没带的一律算到当前正在物化的那个插件头上(HMR 记账)」,随后
|
|
18
|
+
* `removeOwnedStyles(id)` 会把这些标记为 id 的 <style> 整片删除。
|
|
19
|
+
* 所以自建 <style> 必须**先**打上自己的 data-plugin 再插入,否则会被
|
|
20
|
+
* 别的模块认领,并在该模块 HMR/失效时连同我们自己的样式一起被删掉
|
|
21
|
+
* (桌面端 Electron 外壳就是这条路径:启动期内核注入 + HMR 清单同步,
|
|
22
|
+
* 导致样式被连带清除 —— 整块看板退化为无样式的原生控件)。
|
|
23
|
+
* 注:文件末尾的注册名护栏另有一份**刻意独立**的字面量副本,见那里的说明。
|
|
24
|
+
*/
|
|
25
|
+
var DSH_BUNDLE_ID = "@angelyeye/dsh-cost-tracker";
|
|
26
|
+
/** 本插件样式表的稳定标识(data-plugin-css 值,用于去重与盘点)。 */
|
|
27
|
+
var DSH_STYLE_TAG_ID = "cost-tracker-plugin";
|
|
28
|
+
|
|
9
29
|
window.__ModuleLoader__.load({
|
|
10
|
-
id:
|
|
30
|
+
id: DSH_BUNDLE_ID,
|
|
11
31
|
factory: (require) => {
|
|
12
32
|
var module = { exports: {} };
|
|
13
33
|
var exports = module.exports;
|
|
@@ -283,8 +303,13 @@ window.__ModuleLoader__.load({
|
|
|
283
303
|
.cost-subblock { border-top: .5px dashed var(--dsw-alias-border-l2, #e5e7eb); margin-top: 10px; padding-top: 10px; }
|
|
284
304
|
.cost-list { font-size: 12px; color: var(--dsw-alias-label-secondary, #5b6472); line-height: 1.7; }
|
|
285
305
|
`;
|
|
306
|
+
// 先打归属标记再插入:宿主在 style[data-plugin] 上做归属记账,
|
|
307
|
+
// 未标注的标签会被下一个物化的模块用 claimStyles() 认领走
|
|
308
|
+
// (随后 removeOwnedStyles() 会在该模块失效时连带删除)。
|
|
309
|
+
if (document.querySelector('style[data-plugin-css="' + DSH_STYLE_TAG_ID + '"]') !== null) return;
|
|
286
310
|
const tag = document.createElement("style");
|
|
287
|
-
tag.setAttribute("data-plugin
|
|
311
|
+
tag.setAttribute("data-plugin", DSH_BUNDLE_ID);
|
|
312
|
+
tag.setAttribute("data-plugin-css", DSH_STYLE_TAG_ID);
|
|
288
313
|
tag.textContent = css;
|
|
289
314
|
document.head.appendChild(tag);
|
|
290
315
|
ctx.effect(() => () => { tag.remove(); }, "cost-tracker: styles");
|
|
@@ -2955,7 +2980,15 @@ window.__ModuleLoader__.load({
|
|
|
2955
2980
|
// 同时若宿主将来改变约定,也能在控制台给出明确指引,而不是只留一句加载器报错。
|
|
2956
2981
|
// ------------------------------------------------------------
|
|
2957
2982
|
(function () {
|
|
2958
|
-
/**
|
|
2983
|
+
/**
|
|
2984
|
+
* 护栏自己的**独立**期望值:刻意不从 DSH_BUNDLE_ID 派生。
|
|
2985
|
+
*
|
|
2986
|
+
* 这里必须是第二份字面量:护栏的职责正是「注册名 ≠ 包名」时报警,
|
|
2987
|
+
* 若两个值来自同一个变量,注入故障后两者一起变错,护栏永远不会响
|
|
2988
|
+
* (test/client-registration.test.js 的 [4]/[5] 就是靠改写 DSH_BUNDLE_ID
|
|
2989
|
+
* 来注入故障,没有这份独立副本就测不出来)。
|
|
2990
|
+
* 三处必须逐字一致:本行、DSH_BUNDLE_ID、package.json 的 name。
|
|
2991
|
+
*/
|
|
2959
2992
|
var _DSH_BUNDLE_ID = "@angelyeye/dsh-cost-tracker";
|
|
2960
2993
|
/** v1.7.0 发布时用过的旧裸名,仅用于给出针对性提示。 */
|
|
2961
2994
|
var _DSH_LEGACY_BARE_ID = "dsh-cost-tracker";
|
package/credstore.js
CHANGED
|
@@ -16,7 +16,18 @@
|
|
|
16
16
|
// redirect:'manual' + 自管跳转(最多 3 跳):**携带凭据的请求不跨主机跟随**
|
|
17
17
|
// (防重定向把凭据带去别处),不带凭据的请求可在白名单内跟随
|
|
18
18
|
// (官方定价页 `/pricing` → `/pricing/` 这种同站加斜杠的 302 必须放行)。
|
|
19
|
+
// 4. 响应体自解压(v1.9.4):**不依赖宿主 fetch 的解压**。宿主进程里只要有任何
|
|
20
|
+
// 插件 `import` 了 npm 的 `undici` 包(本机实测:`dshmarket/lib/net.js` 静态引入,
|
|
21
|
+
// `dsh-http-proxy` / `dsh-web-fetch-http` 动态引入),Node 内置 `fetch` 就不再
|
|
22
|
+
// 解压 gzip —— 响应既不带 `content-encoding`,body 也是原始压缩字节,于是
|
|
23
|
+
// `JSON.parse(await res.text())` 必然失败。真实故障:火山方舟配额面板报
|
|
24
|
+
// 「配额响应不是合法 JSON(GetCodingPlanUsage)」,插件市场把所有包的新版本
|
|
25
|
+
// 读成 null(它回退用的也是全局 fetch)。修法有两层:
|
|
26
|
+
// · 请求默认带 `accept-encoding: identity`,从源头要未压缩正文;
|
|
27
|
+
// · 收到正文后按**字节**(gzip 1f8b / zlib 78xx / brotli 头)自行解压,
|
|
28
|
+
// 绝不只看响应头 —— 服务端忽略 identity 时同样能正确解析。
|
|
19
29
|
// ============================================================
|
|
30
|
+
import * as zlib from 'node:zlib'
|
|
20
31
|
|
|
21
32
|
/** 本插件占用的凭据引用名(POSIX shell 标识符风格,宿主凭据库通用) */
|
|
22
33
|
export const CRED_REFS = {
|
|
@@ -28,6 +39,95 @@ function isLoopbackHost(host) {
|
|
|
28
39
|
return /^(localhost|127\.0\.0\.1|\[::1?\]|::1)$/i.test(String(host || ''))
|
|
29
40
|
}
|
|
30
41
|
|
|
42
|
+
/** gzip 魔数(1f 8b)—— 压缩流的第一、二字节 */
|
|
43
|
+
function looksGzip(buf) {
|
|
44
|
+
return buf.length > 2 && buf[0] === 0x1f && buf[1] === 0x8b
|
|
45
|
+
}
|
|
46
|
+
/** zlib 魔数(78 01 / 78 9c / 78 da)—— 即 `deflate` 头 */
|
|
47
|
+
function looksZlib(buf) {
|
|
48
|
+
return buf.length > 2 && buf[0] === 0x78 && (buf[1] === 0x01 || buf[1] === 0x9c || buf[1] === 0xda)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 响应体字节 → 明文 Buffer(v1.9.4)。
|
|
53
|
+
*
|
|
54
|
+
* 判定顺序刻意「先看字节、再看响应头」:宿主全局 fetch 被 undici 包污染后,
|
|
55
|
+
* 响应头里的 `content-encoding` 会消失而正文仍是压缩流,只看头会漏掉;
|
|
56
|
+
* 反过来服务端若已按 `identity` 返回明文,字节判定也不会误伤。
|
|
57
|
+
* 任何解压异常都**原样返回**原始字节 —— 宁可让上层报「不是合法 JSON」,
|
|
58
|
+
* 也不能因为这里的猜测把可读内容弄丢。
|
|
59
|
+
*
|
|
60
|
+
* @param {Buffer} buf - 原始响应体
|
|
61
|
+
* @param {string} contentEncoding - 响应头里声明的编码(可能为空)
|
|
62
|
+
* @returns {Buffer}
|
|
63
|
+
*/
|
|
64
|
+
export function decodeCompressedBody(buf, contentEncoding) {
|
|
65
|
+
if (!Buffer.isBuffer(buf) || buf.length === 0) return buf
|
|
66
|
+
const ce = String(contentEncoding || '').trim().toLowerCase()
|
|
67
|
+
try {
|
|
68
|
+
if (looksGzip(buf)) return zlib.gunzipSync(buf)
|
|
69
|
+
if (looksZlib(buf)) return zlib.inflateSync(buf)
|
|
70
|
+
// brotli / 其它编码没有稳定魔数:只在响应头明说、且确实是压缩流时才尝试
|
|
71
|
+
if (ce === 'br' || ce === 'brotli') return zlib.brotliDecompressSync(buf)
|
|
72
|
+
if (ce === 'deflate') return zlib.inflateSync(buf)
|
|
73
|
+
if (ce === 'gzip' || ce === 'x-gzip') return zlib.gunzipSync(buf)
|
|
74
|
+
} catch (e) {
|
|
75
|
+
return buf // 已经解压过 / 截断 / 不是压缩流:交回原样
|
|
76
|
+
}
|
|
77
|
+
return buf
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 把 Response 包成「自带解压」的等价物(v1.9.4)。
|
|
82
|
+
*
|
|
83
|
+
* 只实现调用方真正用到的成员(status / ok / url / redirected / headers /
|
|
84
|
+
* text / json / arrayBuffer),因此对既有接线是透明的;正文只读取一次并缓存,
|
|
85
|
+
* `text()` 与 `json()` 复用同一份解码结果。
|
|
86
|
+
* @param {Response|object} res
|
|
87
|
+
* @returns {object}
|
|
88
|
+
*/
|
|
89
|
+
export function withDecodedBody(res) {
|
|
90
|
+
let cached = null
|
|
91
|
+
const bytes = () => {
|
|
92
|
+
if (cached === null) {
|
|
93
|
+
cached = (async () => {
|
|
94
|
+
let raw = Buffer.alloc(0)
|
|
95
|
+
try {
|
|
96
|
+
if (typeof res.arrayBuffer === 'function') {
|
|
97
|
+
raw = Buffer.from(await res.arrayBuffer())
|
|
98
|
+
} else if (typeof res.text === 'function') {
|
|
99
|
+
raw = Buffer.from(String(await res.text()), 'utf8')
|
|
100
|
+
}
|
|
101
|
+
} catch (e) {
|
|
102
|
+
raw = Buffer.alloc(0)
|
|
103
|
+
}
|
|
104
|
+
const ce = res && res.headers && typeof res.headers.get === 'function' ? res.headers.get('content-encoding') : ''
|
|
105
|
+
return decodeCompressedBody(raw, ce)
|
|
106
|
+
})()
|
|
107
|
+
}
|
|
108
|
+
return cached
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
status: res.status,
|
|
112
|
+
ok: res.ok,
|
|
113
|
+
statusText: res.statusText,
|
|
114
|
+
url: res.url,
|
|
115
|
+
redirected: res.redirected,
|
|
116
|
+
headers: res.headers,
|
|
117
|
+
text: async () => (await bytes()).toString('utf8'),
|
|
118
|
+
json: async () => JSON.parse((await bytes()).toString('utf8')),
|
|
119
|
+
arrayBuffer: async () => {
|
|
120
|
+
const b = await bytes()
|
|
121
|
+
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength)
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 判断请求头里是否已经指定了 accept-encoding(大小写不敏感) */
|
|
127
|
+
function hasAcceptEncoding(headers) {
|
|
128
|
+
return Object.keys(headers || {}).some((k) => k.toLowerCase() === 'accept-encoding')
|
|
129
|
+
}
|
|
130
|
+
|
|
31
131
|
/** 从 URL 提取 hostname(不含端口);非法 URL 返回 '' */
|
|
32
132
|
export function hostnameOf(url) {
|
|
33
133
|
try {
|
|
@@ -95,6 +195,9 @@ export async function safeFetch(url, opts) {
|
|
|
95
195
|
}
|
|
96
196
|
// init.headers 在前、opts.headers 在后:调用方经 init 传的签名头不被覆盖
|
|
97
197
|
const headers = Object.assign({}, init.headers || {}, o.headers || {})
|
|
198
|
+
// v1.9.4:从源头要未压缩正文(宿主全局 fetch 可能已不解压,见文件头「响应体自解压」)。
|
|
199
|
+
// 调用方显式指定过 accept-encoding 时尊重调用方。
|
|
200
|
+
if (!hasAcceptEncoding(headers)) headers['accept-encoding'] = 'identity'
|
|
98
201
|
const credentialHeader = Object.keys(headers).find((k) => /^(authorization|proxy-authorization|x-api-key|api-key)$/i.test(k))
|
|
99
202
|
let current = String(url)
|
|
100
203
|
let method = init.method
|
|
@@ -111,9 +214,9 @@ export async function safeFetch(url, opts) {
|
|
|
111
214
|
} finally {
|
|
112
215
|
clearTimeout(timer)
|
|
113
216
|
}
|
|
114
|
-
if (!(res.status >= 300 && res.status < 400)) return res
|
|
217
|
+
if (!(res.status >= 300 && res.status < 400)) return withDecodedBody(res)
|
|
115
218
|
const loc = typeof res.headers.get === 'function' ? (res.headers.get('location') || '') : ''
|
|
116
|
-
if (!loc) return res // 3xx 但没给 Location:交给调用方按状态自行判断
|
|
219
|
+
if (!loc) return withDecodedBody(res) // 3xx 但没给 Location:交给调用方按状态自行判断
|
|
117
220
|
let target
|
|
118
221
|
try {
|
|
119
222
|
target = new URL(loc, current).toString()
|
package/index.js
CHANGED
|
@@ -33,7 +33,7 @@ import { createCredSeam, migrateLegacySecrets, safeFetch, CRED_REFS, hostnameOf
|
|
|
33
33
|
import { listSessionLogs, planAndBuildImports, loadManifest, IMPORT_SOURCE } from './import.js'
|
|
34
34
|
|
|
35
35
|
/** 插件版本(写入上报信封,便于云端排查版本差异) */
|
|
36
|
-
const PLUGIN_VERSION = '1.9.
|
|
36
|
+
const PLUGIN_VERSION = '1.9.5'
|
|
37
37
|
setPluginVersion(PLUGIN_VERSION)
|
|
38
38
|
|
|
39
39
|
/** 配置面板可写的字段(与 settings 命名空间一致)。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angelyeye/dsh-cost-tracker",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.5",
|
|
4
4
|
"description": "DeepSeek Harness (DSH) plugin: LLM token usage & cost tracking with a settings dashboard, agent tools, HTTP API, persistent storage, multi-device cloud sync, session-log history import, official price sync and a multi-vendor price catalog.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"./package.json": "./package.json"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
|
-
"test": "node test/storage.test.js && node test/pricing.test.js && node test/peak-holidays.test.js && node test/volcengine-plan.test.js && node test/volcengine-host.test.js && node test/config.test.js && node test/recompute.test.js && node test/credstore.test.js && node test/vendor-catalog.test.js && node test/price-sync.test.js && node test/import.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"
|
|
36
|
+
"test": "node test/storage.test.js && node test/pricing.test.js && node test/peak-holidays.test.js && node test/volcengine-plan.test.js && node test/volcengine-host.test.js && node test/config.test.js && node test/recompute.test.js && node test/credstore.test.js && node test/vendor-catalog.test.js && node test/price-sync.test.js && node test/import.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 && node test/release-notes.test.js"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"dsh-plugin",
|