dsh-model-billing 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.0] - 2026-10-28
4
+
5
+ ### Added
6
+
7
+ - Historical price book under Settings → Models & Cost: add, edit, or remove three-price entries for models that are no longer in the configured list. Removing a model keeps its price, so past turns keep their cost and fall back to the raw model id when no display name exists.
8
+ - Price-source labels and one-click restore: a complete custom price overrides the built-in DeepSeek table and is marked “Custom”; the official peak/off-peak table can be restored without another request.
9
+ - Unpriced-route list in the cost panel, plus a switch to hide unpriced notices.
10
+ - Separate settings sections for the turn tail and the composer cost estimate, each with a larger master switch and indented sub-switches. The composer summary now has its own `showDock` switch and the turn tail has `showTurnTail`.
11
+ - LAN and reverse-proxy access: besides loopback, a request is trusted when its Host header matches the address the socket actually listens on. A forged Host is still rejected.
12
+ - Keyboard access: the cost panel is a modal dialog that takes focus on open; the picker supports ↑/↓/Home/End with a roving tabindex.
13
+ - Regression tests for pricing consistency, arrival-time peak/off-peak, catalog and usage caches, concurrent saves, 413 preservation, archive pricing, historical billing after model removal, and trusted authorities.
14
+
15
+ ### Changed
16
+
17
+ - Peak/off-peak is now chosen from the `request/header` arrival time instead of the completion time.
18
+ - A `totalTokens` that disagrees with the three usage buckets keeps the buckets and marks the attempt partial instead of dropping it.
19
+ - Catalog and usage reads are cached; catalog caching can be bypassed with `?refresh=1`.
20
+ - The composer summary and detail panel are labelled “Composer cost estimate” (输入框下方费用估算), avoiding the “费用明细” name used by another plugin.
21
+ - Cost figures no longer carry a trailing asterisk; partial state is shown by the notice instead.
22
+ - Historical price rows use a two-row layout so provider, model, and all three prices stay readable; the remove button reads “删除”.
23
+ - Requires DSH `^0.2.1-rc.3`.
24
+
25
+ ### Fixed
26
+
27
+ - Saves that arrive together are serialized so one cannot overwrite the other; oversized bodies keep their 413 status on both `/save` and `/select`.
28
+ - Stale temporary state files are removed on load; state written by a newer plugin version is read-only instead of being overwritten.
29
+ - The picker no longer takes over when the catalog is unavailable, so the built-in picker remains usable.
30
+ - Display settings have a single source of truth in Host state; legacy localStorage values are cleared once.
31
+ - Turn-tail and detail-panel amounts share one formatter, and the settings save button is present even with no models configured.
32
+
3
33
  ## [0.1.2] - 2026-10-22
4
34
 
5
35
  ### Added
package/README.md CHANGED
@@ -29,11 +29,12 @@
29
29
  ### 功能
30
30
 
31
31
  - **自定义模型顺序**:在“设置 → 模型与费用”中用 ↑ / ↓ 调整已配置模型的顺序。模型选择器按这个顺序展示,跨提供方也保持你排的顺序,并用提供方名称分组。
32
- - **三档 token 价格**:每个非 DeepSeek 模型可填写每百万 tokens 的缓存命中输入、缓存未命中输入、输出价格。`0` 是有效价格,留空表示未设置。
33
- - **DeepSeek 官方价内置**:`deepseek-flash` 和 `deepseek-v4-pro` 直接使用 DeepSeek 官网的峰谷价,只读展示,不需要填写。
32
+ - **三档 token 价格**:每个模型可填写每百万 tokens 的缓存命中输入、缓存未命中输入、输出价格。`0` 是有效价格,留空表示未设置。
33
+ - **DeepSeek 官方价内置**:`deepseek-flash` 和 `deepseek-v4-pro` 默认使用 DeepSeek 官网的峰谷价,并在设置页标明“官方价”;填入完整的三档自定义价即可覆盖官方价,标为“自定义”。
34
34
  - **逐轮费用与使用模型**:每轮结束后在轮尾显示这一轮使用的模型,以及该轮费用和缓存命中率。
35
- - **可配置轮尾**:在设置中开关使用模型、模型提供商、费用、token、缓存命中率、文字标签,并调整字号、间距和字重;也可以关闭选择器接管,恢复 DSH 内置模型选择器。
35
+ - **可配置轮尾**:在设置中总开关轮尾显示,并可分别开关使用模型、模型提供商、费用、token、缓存命中率、文字标签,调整字号、间距和字重;也可以关闭选择器接管,恢复 DSH 内置模型选择器。输入框下方的费用估算另有独立开关。
36
36
  - **本会话费用**:输入框下方显示本会话累计估算、最近一轮费用和缓存命中率;展开后可查看按轮次和按模型的明细。
37
+ - **历史价格与未定价路由**:设置页的“历史价格”区可以为已移出配置的模型补价、改价或删除,不必把模型加回列表;从模型列表删除模型不会删除它的价格,历史调用仍按保存的价格计算,查不到名称时显示原始模型 id。缺价时显示统一提示,可在设置里关闭这条提示。
37
38
  - **DeepSeek 也计入金额**:每条 usage 按事件发生时的北京时间自动选择高峰或空闲价,与第三方模型一起进入统计。
38
39
 
39
40
  ### 估算口径
@@ -59,7 +60,7 @@
59
60
 
60
61
  高峰时段为北京时间周一至周五 `9:00–12:00`、`14:00–18:00`(中国法定节假日除外);其余时间、周末和法定节假日全天为空闲时段。空闲价是高峰价的一半。
61
62
 
62
- DeepSeek 价格只读,不写入本地价格配置。官网调价或节假日安排变化后,需要更新 `host.js` 中的 `DEEPSEEK_PRICES` 和 `DEEPSEEK_HOLIDAYS`;目前内置的是 2026 年节假日。
63
+ DeepSeek 价格默认只读展示;如需按其他渠道或折扣价计费,可在设置页点“自定义价格”填入完整三档价格,保存后以自定义价为准并标为“自定义”;三项清空后保存即可恢复官方价。官网调价或节假日安排变化后,需要更新 `host.js` 中的 `DEEPSEEK_PRICES` 和 `DEEPSEEK_HOLIDAYS`;目前内置的是 2026 年节假日。
63
64
 
64
65
  ### 其他模型价格
65
66
 
@@ -69,7 +70,7 @@ DeepSeek 价格只读,不写入本地价格配置。官网调价或节假日
69
70
 
70
71
  ### 安装
71
72
 
72
- 需要 DSH `^0.2.0-rc.2`。
73
+ 需要 DSH `^0.2.1-rc.3`。
73
74
 
74
75
  **npm**
75
76
 
@@ -122,21 +123,25 @@ npm 包和 GitHub Release 使用相同版本号,两个渠道分别发布:
122
123
 
123
124
  ### 已知限制
124
125
 
125
- - 模型选择器由本插件接管,依赖 `ctx.modelDirectories`。该服务不可用时,插件不会强行覆盖内置选择器,价格和费用统计不受影响。
126
+ - DeepSeek 官方峰谷价只对 `deepseek` 开头的官方 provider(如 `deepseek-account`)且模型名在官方价表内时生效;`openrouter`、`my-deepseek-relay` 这类第三方即使模型名含 `deepseek` 也按普通规则计费,可自行填价。
127
+ - 显示设置(轮尾开关、字号、间距、是否接管选择器等)只保存在 Host 的 `model-billing.json` 里,不再镜像到浏览器 localStorage,因此换设备或换浏览器不会不同步;旧版本写入的 localStorage 键会被自动清除。
128
+ - 除本机回环地址(`127.0.0.1` / `localhost` / `[::1]`)外,Host 头与该连接实际监听的地址(`socket.localAddress` + `localPort`)一致时也会放行,因此 `--host 0.0.0.0` 后用局域网 IP 访问可用;反向代理若改写了 Host,需要让它保留原 Host 或与实际监听地址一致,否则四个路由返回 403,此时模型选择器回退为 DSH 内置版本(费用与排序功能不可用),不会影响正常聊天。
129
+ - 模型选择器由本插件接管,依赖 `ctx.modelDirectories`。该服务不可用或 catalog 尚未加载完成时,插件不会强行覆盖内置选择器,价格和费用统计不受影响。
126
130
  - `/model` 命令仍由 DSH 自带实现,顺序不保证与右下角选择器一致。
127
131
  - 一轮里的多次模型尝试(重试、失败等)会分别计入;失败调用如果 provider 没有返回 usage,则无法计入。
128
132
  - 只统计当前载入 Host 的会话;未打开的历史会话在进入后按需计算。
129
- - 第三方价格是静态配置,不会自动跟随官网调价。
133
+ - provider 返回的 `totalTokens` 与三档明细不一致时,保留三档明细并把该轮标为“部分未定价/不完整”,不再整条丢弃,避免大幅少报。
134
+ - 第三方价格是静态配置,不会自动跟随官网调价;内置节假日表目前只覆盖 2026 年,2027 年起需更新 `DEEPSEEK_HOLIDAYS`。
130
135
 
131
136
  ### 开发
132
137
 
133
138
  ```sh
134
139
  node --check host.js
135
140
  node --check client.js
136
- node --test test/host.test.mjs test/routes.test.mjs
141
+ node --test test/*.test.mjs
137
142
  ```
138
143
 
139
- 当前 10 个测试通过。插件没有运行时 npm 依赖。
144
+ 当前 25 个测试通过。插件没有运行时 npm 依赖。
140
145
 
141
146
  ### License
142
147
 
@@ -152,10 +157,11 @@ node --test test/host.test.mjs test/routes.test.mjs
152
157
 
153
158
  - **Custom model order** in Settings → Models & Cost; the composer picker follows that order across providers and groups models by provider.
154
159
  - **Three prices per model**: cache-hit input, cache-miss input, and output, in CNY per million tokens. `0` is a valid price; blank means unset.
155
- - **Built-in official DeepSeek pricing** for `deepseek-flash` and `deepseek-v4-pro`, selected per usage event between peak and off-peak Beijing time.
160
+ - **Built-in official DeepSeek pricing** for `deepseek-flash` and `deepseek-v4-pro`, selected per usage event between peak and off-peak Beijing time. A complete set of custom prices overrides the table and is labelled "Custom".
156
161
  - **Per-turn model and amount** at the end of each turn, together with the turn's cache-hit rate.
157
- - **Configurable turn tail**: toggle models, provider names, cost, tokens, cache-hit rate, and text labels; adjust font size, spacing, and weight; or disable the picker takeover to restore the built-in DSH picker.
162
+ - **Configurable turn tail**: a master switch for the turn tail, plus individual toggles for models, provider names, cost, tokens, cache-hit rate, and text labels; adjust font size, spacing, and weight; or disable the picker takeover to restore the built-in DSH picker. The composer cost estimate has its own switch.
158
163
  - **Session summary** below the composer plus a detail panel grouped by turn and by model.
164
+ - **Historical prices and unpriced routes**: the "Historical prices" section in Settings lets you add, edit, or remove prices for models no longer in your order without re-adding them. Removing a model from the list keeps its price, so historical calls still bill correctly; when the name is unknown, the raw model id is shown. Unpriced calls share one notice that can be switched off.
159
165
  - **DeepSeek is billed too**, using the same three-price formula and the event's timestamp.
160
166
 
161
167
  ### Cost formula
@@ -168,11 +174,11 @@ cache-hit input × hit price
168
174
 
169
175
  Prices are CNY per million tokens. Cache writes are approximated at the cache-miss price. Figures come from provider usage in the session log; the provider's invoice is authoritative.
170
176
 
171
- The plugin ships **no third-party price defaults**. Enter your provider's actual rates in Settings → Models & Cost. Models without a complete three-price set are marked as unpriced and contribute no amount. DeepSeek prices are read-only and built into `host.js`.
177
+ The plugin ships **no third-party price defaults**. Enter your provider's actual rates in Settings → Models & Cost. Models without a complete three-price set are marked as unpriced and contribute no amount. DeepSeek prices come from the built-in official table; enter a complete custom set to override it, or clear all three fields to restore the official rates.
172
178
 
173
179
  ### Install
174
180
 
175
- Requires DSH `^0.2.0-rc.2`.
181
+ Requires DSH `^0.2.1-rc.3`.
176
182
 
177
183
  **npm**
178
184
 
@@ -216,11 +222,25 @@ Order and prices live in `~/.dsh/model-billing.json` (or `$DSH_HOME/model-billin
216
222
 
217
223
  ### Limitations
218
224
 
219
- - The custom picker depends on `ctx.modelDirectories`; when that service is unavailable the plugin leaves the built-in picker alone.
225
+ - Official DeepSeek peak/off-peak rates apply only to providers whose id starts with `deepseek` (such as `deepseek-account`) and to models present in the official table. A third-party route such as `openrouter` or `my-deepseek-relay` is billed by the normal rules even when the model name contains `deepseek`, and you can price it yourself.
226
+ - Display settings (turn-tail switches, font size, spacing, picker takeover) are stored only in the Host's `model-billing.json`; they are no longer mirrored into browser localStorage, so they do not sync across devices or browsers. Any key written by older builds is cleared automatically.
227
+ - Besides loopback (`127.0.0.1`, `localhost`, `[::1]`), a request is accepted when its Host header matches the address this socket actually listens on (`socket.localAddress` + `localPort`), so a LAN IP works with `--host 0.0.0.0`. A reverse proxy that rewrites Host must preserve it or match the listening address, otherwise the four routes return 403 and the picker falls back to the DSH built-in one; chat itself keeps working.
228
+ - The custom picker depends on `ctx.modelDirectories`; when that service is unavailable or the catalog has not loaded yet, the plugin leaves the built-in picker alone.
220
229
  - The `/model` command is still implemented by DSH and may not follow the custom order.
221
230
  - Multiple attempts within a turn are counted separately; failed calls without provider usage cannot be counted.
222
231
  - Only sessions loaded by the current Host are counted.
223
- - Third-party prices are static and are not refreshed automatically.
232
+ - When a provider's `totalTokens` disagrees with its three buckets, the buckets are kept and the turn is marked partial instead of being dropped, which avoids large under-reporting.
233
+ - Third-party prices are static and are not refreshed automatically. The bundled holiday table covers 2026 only; update `DEEPSEEK_HOLIDAYS` for 2027 onward.
234
+
235
+ ### Development
236
+
237
+ ```sh
238
+ node --check host.js
239
+ node --check client.js
240
+ node --test test/*.test.mjs
241
+ ```
242
+
243
+ 25 tests currently pass. The plugin has no runtime npm dependencies.
224
244
 
225
245
  ### License
226
246