@gamegeek-saikel/dsh-cost-meter 0.1.3 → 0.2.1

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/README.md CHANGED
@@ -29,11 +29,11 @@ Then start the harness:
29
29
  npx @deepseek-ai/dsh web
30
30
  ```
31
31
 
32
- If you have the DSH CLI installed globally, you can also use `dsh` instead of `npx @deepseek-ai/dsh`. To install into another profile, replace `web` with your profile name. The host half requires Node `^22.19.0 || >=24.0.0` and pnpm `11.7.0` for development.
32
+ If you have the DSH CLI installed globally, you can also use `dsh` instead of `npx @deepseek-ai/dsh`. To install into another profile, replace `web` with your profile name. The plugin declares and is verified against DSH `>=0.1.1-rc.1 <0.2.0` (current local version `0.1.1-rc.1`). The host half requires Node `^22.19.0 || >=24.0.0` and pnpm `11.7.0` for development.
33
33
 
34
34
  ## Overview
35
35
 
36
- Chat costs in DeepSeek pricing change over time (list prices, USD→CNY exchange, and the upcoming 2026-08-17 peak/off-peak rollout), and a conversation spans many turns with cache-hit, cache-miss, cache-write, and output token buckets. Naively recomputing costs at *current* prices makes history drift every time the price table changes.
36
+ Chat costs in DeepSeek pricing change over time (list prices, USD→CNY exchange, and the 2026-08-17 peak/off-peak rollout; on 2026-08-21 the official zh/en pages were redesigned into a combined table with three model columns and OFF-PEAK/PEAK cells), and a conversation spans many turns with cache-hit, cache-miss, cache-write, and output token buckets. Naively recomputing costs at *current* prices makes history drift every time the price table changes.
37
37
 
38
38
  **Cost Meter** solves this with an **append-only pricebook**: every price/fx/band-table change starts a new immutable `PricebookSnapshot` (monotonic `version`, `effectiveAt`), and each usage event anchors to the snapshot effective at its own time. The result is an immutable per-step cost ledger that only grows — it never mutates. Live streaming estimates are explicitly labeled 估算/estimate because they use *current* prices; they are replaced by the exact anchored value once the step settles.
39
39
 
@@ -43,7 +43,8 @@ Chat costs in DeepSeek pricing change over time (list prices, USD→CNY exchange
43
43
  |---|---|
44
44
  | Cost anchoring | Append-only pricebook snapshots; step cost computed once at the event's own time |
45
45
  | Price sources | Manual override > official pricing page > built-in fallback > OpenRouter (fallback only, USD→CNY) > none |
46
- | Peak pricing | 2026-08-17 00:00 Beijing rollout; peak/off-peak windows parsed from each page's own schedule (the zh and en pages may differ; fallback 09:00–12:00 / 14:00–18:00 Beijing), off-peak half price |
46
+ | Official page | Parses both the 2026-08-21 combined zh/en tables (including the `deepseek-v4-flash-vision-exp` column and the English UTC schedule) and the legacy split tables; the built-in historical list still anchors `single` when the page no longer carries one |
47
+ | Peak pricing | 2026-08-17 00:00 Beijing rollout; peak windows apply Monday–Friday only (the zh and en pages may state different timezones; fallback 09:00–12:00 / 14:00–18:00 Beijing), weekends and all other hours off-peak at half price |
47
48
  | Cost formula | Uncached input + cache reads (hit rate) + cache writes (billed at uncached input rate) + output, per 1M tokens, CNY |
48
49
  | Account balance | Official `GET /user/balance`, cached 60 s, single in-flight request, trust-fenced route |
49
50
  | Subagent support | BFS over the live agent tree; conversation totals = main session + descendants |
@@ -71,7 +72,7 @@ The pricebook (`src/pricebook.ts`) is the durable price source, persisted on the
71
72
 
72
73
  - **Priority chain** — per canonical model key (`provider/model`, bare model, or the `flash`/`pro` pricing key for DeepSeek-family models): manual override > official page > built-in fallback > OpenRouter (fallback only, USD→CNY, cache reads at the configured discount) > none.
73
74
  - **Snapshot selection** — `snapshotForTime` picks the newest snapshot with `effectiveAt <= event time` (pre-install sessions anchor to the first snapshot once).
74
- - **Peak/off-peak** — before the 2026-08-17 rollout all steps price at the single list price; after it, the band is chosen by the EVENT's own time against the schedule of the pricebook's own page (the zh and en pages each parse their own peak windows, falling back to Beijing 09:00–12:00 / 14:00–18:00, everything else off-peak). Each step's band is anchored once at fold time, so the per-reply cards and chips always show the band that round was billed at — never the band of the moment you are looking.
75
+ - **Peak/off-peak** — before the 2026-08-17 rollout all steps price at the single list price; after it, the band is chosen by the EVENT's own time against the schedule of the pricebook's own page (the zh and en pages each parse their own peak windows — the redesigned English page states UTC — falling back to Beijing 09:00–12:00 / 14:00–18:00, everything else off-peak). The official pages restrict peak windows to Monday–Friday, so Saturdays and Sundays are off-peak all day in the schedule's own timezone. When the combined page has no legacy single-price column, `single` keeps anchoring to the built-in historical list; a page that still carries a separate legacy table wins. Each step's band is anchored once at fold time, so the per-reply cards and chips always show the band that round was billed at — never the band of the moment you are looking.
75
76
  - **Immutable ledger** — the `sessionCost` projection (`src/session-cost-projection.ts`) folds `request/header` (model) and usage-carrying events into per-step rows; a second usage sample for the same (turn, step) replaces the first (same-step finalization, not a re-price), with O(1) incremental totals.
76
77
 
77
78
  ## Project Structure
package/README.zh-CN.md CHANGED
@@ -29,11 +29,11 @@ npx @deepseek-ai/dsh plugin --profile web add @gamegeek-saikel/dsh-cost-meter
29
29
  npx @deepseek-ai/dsh web
30
30
  ```
31
31
 
32
- 如果已全局安装 DSH CLI,也可以使用 `dsh` 代替 `npx @deepseek-ai/dsh`。安装到其他 profile 时,把 `web` 替换成你的 profile 名称即可。开发环境要求 Node `^22.19.0 || >=24.0.0` 与 pnpm `11.7.0`。
32
+ 如果已全局安装 DSH CLI,也可以使用 `dsh` 代替 `npx @deepseek-ai/dsh`。安装到其他 profile 时,把 `web` 替换成你的 profile 名称即可。插件声明并已验证兼容 DSH `>=0.1.1-rc.1 <0.2.0`(当前本地版本 `0.1.1-rc.1`)。开发环境要求 Node `^22.19.0 || >=24.0.0` 与 pnpm `11.7.0`。
33
33
 
34
34
  ## 概述
35
35
 
36
- DeepSeek 的价格随时间变化(官方价目表、USD→CNY 汇率、以及 2026-08-17 上线的峰值/闲时分时计价),而一次对话跨越很多轮,且每轮都含缓存命中、缓存未命中、缓存写入与输出等 token 桶。若按"当前价格"重算成本,每次价目变动都会让历史记录漂移。
36
+ DeepSeek 的价格随时间变化(官方价目表、USD→CNY 汇率、以及 2026-08-17 上线的峰值/闲时分时计价;2026-08-21 官方又把中英文页面改为一张三模型列、空闲/高峰列的价格表),而一次对话跨越很多轮,且每轮都含缓存命中、缓存未命中、缓存写入与输出等 token 桶。若按"当前价格"重算成本,每次价目变动都会让历史记录漂移。
37
37
 
38
38
  **Cost Meter** 用**只追加的价格簿**解决这一问题:每次价格/汇率/分时表变化都会开启一个新的不可变 `PricebookSnapshot`(单调 `version`、`effectiveAt`),每个用量事件锚定到其自身时刻生效的快照。结果是一个**只增长、永不改写**的不可变逐步成本账本。流式实时估算明确标注为"估算"——因为它使用*当前*价格;一旦该步结算,即被精确的锚定值取代。
39
39
 
@@ -43,7 +43,8 @@ DeepSeek 的价格随时间变化(官方价目表、USD→CNY 汇率、以及
43
43
  |---|---|
44
44
  | 成本锚定 | 只追加价格簿快照;步成本在事件自身时刻只计算一次 |
45
45
  | 价格来源 | 手动覆盖 > 官方价格页 > 内置回退 > OpenRouter(仅回退,USD→CNY)> 无 |
46
- | 峰值定价 | 2026-08-17 00:00 北京生效;高峰/闲时窗口按各页面自己的时段表解析(中英文页面可能不同,缺省 09:00–12:00 / 14:00–18:00 北京),闲时为半价 |
46
+ | 官方页适配 | 同时解析 2026-08-21 新版中英文页面(含 `deepseek-v4-flash-vision-exp` 第三列与英文 UTC 时段)和旧版分表;无旧单价的页面继续用内置历史单价锚定 |
47
+ | 峰值定价 | 2026-08-17 00:00 北京生效;高峰窗口仅周一至周五(中英文页面时区可能不同,缺省 09:00–12:00 / 14:00–18:00 北京),周六、周日全天及其余时段为半价闲时 |
47
48
  | 成本公式 | 未命中输入 + 缓存命中(命中价)+ 缓存写入(按未命中输入价计)+ 输出,每百万 tokens,CNY |
48
49
  | 账户余额 | 官方 `GET /user/balance`,缓存 60 秒,单飞请求,路由带信任围栏 |
49
50
  | 子代理支持 | 沿活跃代理树 BFS;对话总花费 = 主会话 + 全部后代 |
@@ -71,7 +72,7 @@ DeepSeek 的价格随时间变化(官方价目表、USD→CNY 汇率、以及
71
72
 
72
73
  - **优先级链**——按规范模型键(`provider/model`、裸模型名,或 DeepSeek 系模型的 `flash`/`pro` 定价键):手动覆盖 > 官方页面 > 内置回退 > OpenRouter(仅回退,USD→CNY,缓存读按配置折扣)> 无。
73
74
  - **快照选取**——`snapshotForTime` 取 `effectiveAt <= 事件时间` 的最新快照(安装前的会话一次性锚到首个快照)。
74
- - **峰值/闲时**——2026-08-17 上线前所有步按单一价目计费;上线后按事件自身时刻与该价格簿页面的时段表选档(中英文页面各自解析自己的高峰窗口,抓取失败时回退到北京 09:00–12:00 / 14:00–18:00,其余闲时)。每步的档位在折叠时锚定一次,逐回复卡片与回复小标签永远显示该轮对话当时被计价的档位,而不是查看时刻的档位。
75
+ - **峰值/闲时**——2026-08-17 上线前所有步按单一价目计费;上线后按事件自身时刻与该价格簿页面的时段表选档(中英文页面各自解析自己的高峰窗口,英文新版为 UTC,抓取失败时回退到北京 09:00–12:00 / 14:00–18:00,其余闲时)。官方页面限定高峰仅周一至周五,因此北京时间周六、周日全天为闲时。新版合并表没有旧单价列时,`single` 继续锚定内置历史单价;页面若有独立旧表,则优先用它。每步的档位在折叠时锚定一次,逐回复卡片与回复小标签永远显示该轮对话当时被计价的档位,而不是查看时刻的档位。
75
76
  - **不可变账本**——`sessionCost` 投影(`src/session-cost-projection.ts`)把 `request/header`(模型)与携带用量的事件折叠为逐步记录;同一 (turn, step) 的第二次用量样本**替换**第一条(同一步终结,而非重新计价),总计以 O(1) 增量维护。
76
77
 
77
78
  ## 项目结构
package/lib/client.js CHANGED
@@ -157,11 +157,13 @@ window.__ModuleLoader__.load({
157
157
  const PEAK_PRICING_START_MS = Date.UTC(2026, 7, 16, 16, 0, 0);
158
158
  /**
159
159
  * Built-in fallback zh peak schedule: the official announcement's windows in
160
- * Beijing time (peak 09:00-12:00 / 14:00-18:00, everything else off-peak).
160
+ * Beijing time (peak 09:00-12:00 / 14:00-18:00, Monday–Friday; everything
161
+ * else, including all of Saturday and Sunday, off-peak).
161
162
  */
162
163
  const PEAK_SCHEDULE_ZH = {
163
164
  timezone: "Asia/Shanghai",
164
- ranges: [[9, 12], [14, 18]]
165
+ ranges: [[9, 12], [14, 18]],
166
+ weekdaysOnly: true
165
167
  };
166
168
  /** Built-in fallback USD prices from the English pricing page. */
167
169
  const FALLBACK_CURRENT_USD = {
@@ -180,7 +182,9 @@ window.__ModuleLoader__.load({
180
182
  /**
181
183
  * Whether the given instant is a peak-pricing hour under one schedule: the
182
184
  * zh fallback is Beijing time 09:00-12:00 and 14:00-18:00; each pricebook
183
- * passes the schedule parsed from its own (zh or en) pricing page.
185
+ * passes the schedule parsed from its own (zh or en) pricing page. When the
186
+ * schedule is Monday–Friday only, Saturdays and Sundays in the schedule's
187
+ * timezone are off-peak all day.
184
188
  * @param now - the instant to classify.
185
189
  * @param schedule - the peak-hour schedule to classify against.
186
190
  * @returns true during peak hours.
@@ -189,8 +193,13 @@ window.__ModuleLoader__.load({
189
193
  const parts = new Intl.DateTimeFormat("en-US", {
190
194
  timeZone: schedule.timezone,
191
195
  hour: "numeric",
192
- hour12: false
196
+ hour12: false,
197
+ weekday: "short"
193
198
  }).formatToParts(now);
199
+ if (schedule.weekdaysOnly === true) {
200
+ const weekday = parts.find((part) => part.type === "weekday")?.value;
201
+ if (weekday === "Sat" || weekday === "Sun") return false;
202
+ }
194
203
  const hour = Number(parts.find((part) => part.type === "hour")?.value);
195
204
  if (Number.isNaN(hour)) return false;
196
205
  return schedule.ranges.some(([start, end]) => hour >= start && hour < end);