@alexpeng/dsh-custom-plugin 0.1.4 → 0.1.6

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
@@ -4,14 +4,28 @@ All notable changes to this project are documented here. The format loosely
4
4
  follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions
5
5
  follow [Semantic Versioning](https://semver.org/).
6
6
 
7
- ## Unreleased
7
+ ## 0.1.6 — 2026-09-02
8
8
 
9
9
  ### Fixed
10
10
 
11
+ - Usage scans now preserve the live ledger when a session read fails or new
12
+ usage arrives during replay, and return the replayed Beijing day so a
13
+ midnight-crossing scan cannot relabel its result.
14
+ - Legacy API-key migration no longer overwrites an existing system credential;
15
+ failed system-key deletion is reported instead of being treated as success.
16
+ - Legacy usage rows without a recoverable peak/off-peak split are marked as
17
+ inexact and are no longer shown with a misleading cost estimate.
11
18
  - DeepSeek peak/off-peak cost estimates now treat only Beijing Monday-Friday
12
19
  09:00–12:00 / 14:00–18:00 as peak; weekends are off-peak.
13
20
  - The browser usage panel now uses the same Beijing-time day bucket as the
14
21
  Host, including when the desktop host runs outside UTC+8.
22
+ - Client state snapshots are serialized in invocation order so rapid settings
23
+ changes cannot be overwritten by an older save completing later.
24
+ - Timeline refreshes ignore stale session/request responses and follow live
25
+ conversation running-state changes.
26
+ - Mermaid modal changes reload correctly, while concurrent CDN loads share one
27
+ in-flight request; the balance hover panel also remains reachable across its
28
+ trigger gap.
15
29
 
16
30
  ### Changed
17
31
 
@@ -23,6 +37,14 @@ follow [Semantic Versioning](https://semver.org/).
23
37
  source link, and the built-in rule check date.
24
38
  - Saved panel keys prefer the optional OS credential store, migrate legacy
25
39
  state-file keys when possible, and no longer cross the browser state boundary.
40
+ - Light/dark root theme styles now take precedence over the host OS preference,
41
+ and the build uses the current tsdown dependency configuration without
42
+ deprecation warnings.
43
+
44
+ ### Added
45
+
46
+ - Regression coverage for ordered state saves and shared in-flight Mermaid
47
+ loading.
26
48
 
27
49
  ## 0.1.3 — 2026-08-23
28
50
 
package/lib/client.js CHANGED
@@ -33,6 +33,19 @@ window.__ModuleLoader__.load({
33
33
  * exact. The published peak/off-peak schedule and the ledger day buckets are
34
34
  * both defined on this clock, independent of the host's local timezone. */
35
35
  const CST_OFFSET_MS = 8 * 36e5;
36
+ const PEAK_COUNTER_KEYS = [
37
+ "peakIn",
38
+ "peakCacheIn",
39
+ "peakCacheW",
40
+ "peakOut"
41
+ ];
42
+ function hasAllPeakCounters(row) {
43
+ return PEAK_COUNTER_KEYS.every((key) => typeof row[key] === "number");
44
+ }
45
+ /** Whether a row has enough persisted information for exact tariff math. */
46
+ function hasKnownPeakSplit(row) {
47
+ return row.peakSplitKnown === true || row.peakSplitKnown === void 0 && hasAllPeakCounters(row);
48
+ }
36
49
  function cstParts(time) {
37
50
  const d = new Date(time + CST_OFFSET_MS);
38
51
  return {
@@ -52,6 +65,12 @@ window.__ModuleLoader__.load({
52
65
  }
53
66
  //#endregion
54
67
  //#region src/pricing.ts
68
+ /**
69
+ * DeepSeek cost-estimation helpers shared by the host-facing usage model and
70
+ * the browser panel. Prices are CNY per 1M tokens at peak rate; off-peak is
71
+ * applied to the portion without a peak counter.
72
+ * @module @alexpeng/dsh-custom-plugin/pricing
73
+ */
55
74
  /** Official source used for the built-in estimate and shown in the panel. */
56
75
  const DEEPSEEK_PRICING_SOURCE_URL = "https://api-docs.deepseek.com/zh-cn/quick_start/pricing/";
57
76
  /** Date on which the built-in rule was checked against the official page. */
@@ -79,9 +98,8 @@ window.__ModuleLoader__.load({
79
98
  }
80
99
  /**
81
100
  * Split one row into peak/off-peak token volume and CNY cost. Rows without
82
- * peak counters are treated as
83
- * entirely off-peak for compatibility with state written before peak
84
- * tracking existed; a rescan can rebuild the row with exact split counters.
101
+ * peak counters retain the old all-off-peak numeric fallback for compatibility,
102
+ * but `exact` is false until a rescan rebuilds the row with split counters.
85
103
  */
86
104
  function usageCostBreakdown(row, model) {
87
105
  const price = priceOf(model);
@@ -92,6 +110,7 @@ window.__ModuleLoader__.load({
92
110
  const peakCost = peakIn * price.input + peakCacheIn * price.cacheHit + peakCacheW * price.input + peakOut * price.output;
93
111
  const offPeakCost = (row.in - peakIn) * price.input + (row.cacheIn - peakCacheIn) * price.cacheHit + (row.cacheW - peakCacheW) * price.input + (row.out - peakOut) * price.output;
94
112
  return {
113
+ exact: hasKnownPeakSplit(row),
95
114
  peakTokens: peakIn + peakCacheIn + peakCacheW + peakOut,
96
115
  offPeakTokens: row.in - peakIn + (row.cacheIn - peakCacheIn) + (row.cacheW - peakCacheW) + (row.out - peakOut),
97
116
  peakCostCny: peakCost / 1e6,
@@ -158,13 +177,21 @@ window.__ModuleLoader__.load({
158
177
  body: JSON.stringify(body)
159
178
  });
160
179
  }
180
+ /**
181
+ * State POSTs carry a complete snapshot rather than an operation log. Keep
182
+ * them on one client-side queue so a quick sequence of toggles cannot be
183
+ * applied out of order when the network completes requests differently.
184
+ */
185
+ let stateSaveQueue = Promise.resolve();
161
186
  /** Read the whole state document. */
162
187
  async function apiStateGet() {
163
188
  return await request("/state", { cache: "no-store" });
164
189
  }
165
190
  /** Persist a state edit. */
166
191
  async function apiStateSave(edit) {
167
- return await postJson("/state", edit);
192
+ const run = stateSaveQueue.catch(() => {}).then(() => postJson("/state", edit));
193
+ stateSaveQueue = run.then(() => {}, () => {});
194
+ return await run;
168
195
  }
169
196
  /** Timeline nodes for one session (tail-capped by the host). */
170
197
  async function apiTimelineGet(sessionId) {
@@ -1231,7 +1258,71 @@ window.__ModuleLoader__.load({
1231
1258
  .vx-mmd-stage svg {
1232
1259
  max-width: 100%;
1233
1260
  height: auto;
1234
- }`;
1261
+ }
1262
+
1263
+ /* DSH can select a theme independently of the operating system. The root
1264
+ classes are applied from the resolved GUI theme and intentionally win
1265
+ over the media-query fallback above. */
1266
+ .vx-root.vx-dark .vx-glass {
1267
+ background: rgba(13, 17, 25, .62);
1268
+ color: #eef1f7;
1269
+ }
1270
+ .vx-root.vx-dark .vx-liquid .vx-glass {
1271
+ background: rgba(0, 0, 0, .06);
1272
+ }
1273
+ .vx-root.vx-dark .vx-liquid .vx-pattern {
1274
+ background-image: radial-gradient(rgba(110, 110, 128, .5) 3px, transparent 3px);
1275
+ }
1276
+ .vx-root.vx-dark .vx-pattern {
1277
+ background-image: radial-gradient(rgba(100, 100, 116, .5) 1px, transparent 1px);
1278
+ }
1279
+ .vx-root.vx-dark .vx-dot {
1280
+ background: rgba(205, 215, 230, .95);
1281
+ border-color: rgba(28, 34, 46, .9);
1282
+ }
1283
+ .vx-root.vx-dark .vx-thumb {
1284
+ background: rgba(165, 180, 205, .45);
1285
+ }
1286
+ .vx-root.vx-dark .vx-balance-hover,
1287
+ .vx-root.vx-dark .vx-panel,
1288
+ .vx-root.vx-dark .vx-modal,
1289
+ .vx-root.vx-dark .vx-folders {
1290
+ color: #e8ebf2;
1291
+ }
1292
+ .vx-root.vx-dark .vx-toast {
1293
+ background: rgba(18, 22, 30, .94);
1294
+ }
1295
+
1296
+ .vx-root.vx-light .vx-glass {
1297
+ background: rgba(255, 255, 255, .64);
1298
+ color: #20242e;
1299
+ }
1300
+ .vx-root.vx-light .vx-liquid .vx-glass {
1301
+ background: rgba(255, 255, 255, .06);
1302
+ }
1303
+ .vx-root.vx-light .vx-liquid .vx-pattern {
1304
+ background-image: radial-gradient(rgba(150, 150, 160, .5) 3px, transparent 3px);
1305
+ }
1306
+ .vx-root.vx-light .vx-pattern {
1307
+ background-image: radial-gradient(rgba(150, 150, 160, .42) 1px, transparent 1px);
1308
+ }
1309
+ .vx-root.vx-light .vx-dot {
1310
+ background: rgba(70, 85, 110, .92);
1311
+ border-color: rgba(255, 255, 255, .92);
1312
+ }
1313
+ .vx-root.vx-light .vx-thumb {
1314
+ background: rgba(90, 105, 135, .5);
1315
+ }
1316
+ .vx-root.vx-light .vx-balance-hover,
1317
+ .vx-root.vx-light .vx-panel,
1318
+ .vx-root.vx-light .vx-modal,
1319
+ .vx-root.vx-light .vx-folders {
1320
+ color: #20242e;
1321
+ }
1322
+ .vx-root.vx-light .vx-toast {
1323
+ background: rgba(26, 32, 44, .92);
1324
+ }
1325
+ }`;
1235
1326
  //#endregion
1236
1327
  //#region src/client/custom.tsx
1237
1328
  /**
@@ -2494,6 +2585,7 @@ window.__ModuleLoader__.load({
2494
2585
  });
2495
2586
  }
2496
2587
  let lastRefetchAt = 0;
2588
+ let turnsRequestSerial = 0;
2497
2589
  let railRefetchRows = -1;
2498
2590
  let lastDiagAt = 0;
2499
2591
  function diagThrottled(message) {
@@ -2509,9 +2601,10 @@ window.__ModuleLoader__.load({
2509
2601
  fetchTurns(S.sessionId);
2510
2602
  }
2511
2603
  async function fetchTurns(sessionId) {
2604
+ const requestSerial = ++turnsRequestSerial;
2512
2605
  try {
2513
2606
  const result = await apiTimelineGet(sessionId);
2514
- if (sessionId !== S.sessionId) return;
2607
+ if (sessionId !== S.sessionId || requestSerial !== turnsRequestSerial) return;
2515
2608
  if (result.ok === true) {
2516
2609
  S.turns = {
2517
2610
  sessionId,
@@ -2617,8 +2710,10 @@ window.__ModuleLoader__.load({
2617
2710
  let viewH = height;
2618
2711
  let railRight = 4;
2619
2712
  let railLeft = 56;
2713
+ let scrollerRect = null;
2620
2714
  if (scroller !== null) try {
2621
2715
  const r = scroller.getBoundingClientRect();
2716
+ scrollerRect = r;
2622
2717
  top = Math.max(0, r.top);
2623
2718
  height = Math.max(60, r.height);
2624
2719
  viewH = Math.max(1, scroller.clientHeight || r.height);
@@ -2630,7 +2725,7 @@ window.__ModuleLoader__.load({
2630
2725
  const trackH = height;
2631
2726
  const pos = [];
2632
2727
  const seqAnchor = /* @__PURE__ */ new Map();
2633
- if (scroller !== null) {
2728
+ if (scroller !== null && scrollerRect !== null) {
2634
2729
  const rows = queryUserRows(scroller);
2635
2730
  const offset = items.length - rows.length;
2636
2731
  if (rows.length > items.length && railRefetchRows < rows.length) {
@@ -2642,8 +2737,7 @@ window.__ModuleLoader__.load({
2642
2737
  if (item === void 0) continue;
2643
2738
  try {
2644
2739
  const r = rows[i].getBoundingClientRect();
2645
- const sr = scroller.getBoundingClientRect();
2646
- const pct = Math.max(0, Math.min(1, (r.top + r.height / 2 - sr.top + scrollTop) / Math.max(1, scroller.scrollHeight || 1)));
2740
+ const pct = Math.max(0, Math.min(1, (r.top + r.height / 2 - scrollerRect.top + scrollTop) / Math.max(1, scroller.scrollHeight || 1)));
2647
2741
  const starred = S.stars[S.sessionId]?.[item.seq] === true;
2648
2742
  seqAnchor.set(item.seq, rows[i]);
2649
2743
  pos.push({
@@ -3067,9 +3161,10 @@ window.__ModuleLoader__.load({
3067
3161
  const result = await apiUsageScan();
3068
3162
  if (result.ok === true) {
3069
3163
  S.usage = { ...S.usage };
3070
- S.usage[dayKey()] = result.usageToday;
3164
+ const scanDay = result.day || dayKey();
3165
+ S.usage[scanDay] = result.usageToday;
3071
3166
  setS({ usage: S.usage });
3072
- toast(`已扫描今日会话日志(${String(result.scannedSessions)} 个今日活跃会话)`, "info");
3167
+ toast(`已扫描 ${scanDay} 会话日志(${String(result.scannedSessions)} 个活跃会话)`, "info");
3073
3168
  } else toast(result.error ?? "扫描失败", "error");
3074
3169
  } catch (error) {
3075
3170
  toast("扫描失败: " + String(error?.message ?? error), "error");
@@ -3261,20 +3356,18 @@ window.__ModuleLoader__.load({
3261
3356
  const bal = b?.ok === true && b.balance !== null && b.balance !== void 0 ? b.balance : null;
3262
3357
  const balance = bal !== null ? (bal.currency ?? "CNY") === "CNY" ? "¥" + (bal.total ?? "") : `${bal.total ?? ""} ${bal.currency ?? ""}` : b?.ok === false ? b.keyConfigured === true ? "额度?" : "未配置密钥" : "额度…";
3263
3358
  const open = hover || pinned;
3264
- return react.createElement("span", { className: "vx-balance-wrap" }, react.createElement("span", {
3265
- className: "vx-balance-text" + (pinned ? " vx-balance-pinned" : ""),
3266
- title: pinned ? "再次点击取消固定" : "点击固定密钥面板",
3267
- onClick: () => setPinned(!pinned),
3359
+ return react.createElement("span", {
3360
+ className: "vx-balance-wrap",
3268
3361
  onMouseEnter: () => setHover(true),
3269
3362
  onMouseLeave: () => setHover(false)
3363
+ }, react.createElement("span", {
3364
+ className: "vx-balance-text" + (pinned ? " vx-balance-pinned" : ""),
3365
+ title: pinned ? "再次点击取消固定" : "点击固定密钥面板",
3366
+ onClick: () => setPinned(!pinned)
3270
3367
  }, react.createElement(Icon, {
3271
3368
  n: "wallet",
3272
3369
  size: 13
3273
- }), react.createElement("span", null, balance), calls > 0 ? react.createElement("span", { className: "vx-balance-today" }, `今日 ${calls} 次`) : null), open ? react.createElement("div", {
3274
- className: "vx-glass vx-balance-hover",
3275
- onMouseEnter: () => setHover(true),
3276
- onMouseLeave: () => setHover(false)
3277
- }, react.createElement("div", { className: "vx-pattern" }), pinned ? react.createElement("div", { className: "vx-balance-head" }, react.createElement("span", { className: "vx-muted" }, "已固定 · 再次点击「额度」或下方按钮取消"), react.createElement("button", {
3370
+ }), react.createElement("span", null, balance), calls > 0 ? react.createElement("span", { className: "vx-balance-today" }, `今日 ${calls} 次`) : null), open ? react.createElement("div", { className: "vx-glass vx-balance-hover" }, react.createElement("div", { className: "vx-pattern" }), pinned ? react.createElement("div", { className: "vx-balance-head" }, react.createElement("span", { className: "vx-muted" }, "已固定 · 再次点击「额度」或下方按钮取消"), react.createElement("button", {
3278
3371
  className: "vx-btn vx-btn-sm",
3279
3372
  onClick: () => setPinned(false)
3280
3373
  }, react.createElement(Icon, {
@@ -3291,13 +3384,14 @@ window.__ModuleLoader__.load({
3291
3384
  row: day[model],
3292
3385
  breakdown: usageCostBreakdown(day[model], model)
3293
3386
  }));
3387
+ const allSplitsKnown = rows.every((item) => item.breakdown.exact);
3294
3388
  const totalPeakTokens = rows.reduce((sum, item) => sum + item.breakdown.peakTokens, 0);
3295
3389
  const totalOffPeakTokens = rows.reduce((sum, item) => sum + item.breakdown.offPeakTokens, 0);
3296
3390
  const totalPeakCost = rows.reduce((sum, item) => sum + item.breakdown.peakCostCny, 0);
3297
3391
  const totalOffPeakCost = rows.reduce((sum, item) => sum + item.breakdown.offPeakCostCny, 0);
3298
3392
  return react.createElement("div", null, react.createElement("table", { className: "vx-table" }, react.createElement("thead", null, react.createElement("tr", null, react.createElement("th", null, "模型"), react.createElement("th", null, "输入"), react.createElement("th", null, "缓存"), react.createElement("th", null, "输出"), react.createElement("th", null, "调用"), react.createElement("th", null, "费用 ¥"))), react.createElement("tbody", null, rows.map(({ model, row, breakdown }) => {
3299
- return react.createElement("tr", { key: model }, react.createElement("td", null, model), react.createElement("td", null, row.in ?? 0), react.createElement("td", null, row.cacheIn ?? 0), react.createElement("td", null, row.out ?? 0), react.createElement("td", null, row.calls ?? 0), react.createElement("td", { title: `峰 ¥${breakdown.peakCostCny.toFixed(4)} · 闲 ¥${breakdown.offPeakCostCny.toFixed(4)}` }, breakdown.totalCostCny.toFixed(4)));
3300
- }))), react.createElement("div", { className: "vx-muted" }, `峰 token ${formatTokenCount(totalPeakTokens)} · 闲 token ${formatTokenCount(totalOffPeakTokens)} · 峰费用 ¥${totalPeakCost.toFixed(4)} · 闲费用 ¥${totalOffPeakCost.toFixed(4)}`), react.createElement("div", { className: "vx-muted" }, `规则核对于 ${DEEPSEEK_PRICING_CHECKED_ON} · `, react.createElement("a", {
3393
+ return react.createElement("tr", { key: model }, react.createElement("td", null, model), react.createElement("td", null, row.in ?? 0), react.createElement("td", null, row.cacheIn ?? 0), react.createElement("td", null, row.out ?? 0), react.createElement("td", null, row.calls ?? 0), react.createElement("td", { title: breakdown.exact ? `峰 ¥${breakdown.peakCostCny.toFixed(4)} · 闲 ¥${breakdown.offPeakCostCny.toFixed(4)}` : "旧用量缺少峰闲时拆分,请先扫描今日会话日志" }, breakdown.exact ? breakdown.totalCostCny.toFixed(4) : "—"));
3394
+ }))), allSplitsKnown ? react.createElement("div", { className: "vx-muted" }, `峰 token ${formatTokenCount(totalPeakTokens)} · 闲 token ${formatTokenCount(totalOffPeakTokens)} · 峰费用 ¥${totalPeakCost.toFixed(4)} · 闲费用 ¥${totalOffPeakCost.toFixed(4)}`) : react.createElement("div", { className: "vx-muted" }, "部分旧用量缺少峰闲时拆分,费用暂不估算;请先扫描今日会话日志。"), react.createElement("div", { className: "vx-muted" }, `规则核对于 ${DEEPSEEK_PRICING_CHECKED_ON} · `, react.createElement("a", {
3301
3395
  href: DEEPSEEK_PRICING_SOURCE_URL,
3302
3396
  target: "_blank",
3303
3397
  rel: "noreferrer"
@@ -3587,11 +3681,16 @@ window.__ModuleLoader__.load({
3587
3681
  }, [current]);
3588
3682
  react.useEffect(() => {
3589
3683
  if (s.cfg.timeline !== true || S.sessionId === null) return;
3684
+ const running = summary?.running === true;
3590
3685
  const handle = setInterval(() => {
3591
- if (summary !== null && summary.running === true) fetchTurns(S.sessionId);
3686
+ if (running) fetchTurns(S.sessionId);
3592
3687
  }, 3e3);
3593
3688
  return () => clearInterval(handle);
3594
- }, [s.cfg.timeline, current]);
3689
+ }, [
3690
+ s.cfg.timeline,
3691
+ current,
3692
+ summary?.running
3693
+ ]);
3595
3694
  react.useEffect(() => {
3596
3695
  if (s.cfg.timeline !== true || S.sessionId === null) return;
3597
3696
  const el = trackRef.current;
@@ -4384,7 +4483,7 @@ window.__ModuleLoader__.load({
4384
4483
  return () => {
4385
4484
  alive = false;
4386
4485
  };
4387
- }, [mm?.index]);
4486
+ }, [mm]);
4388
4487
  if (mm === null) return null;
4389
4488
  return react.createElement("div", {
4390
4489
  className: "vx-modal-mask",
@@ -4877,7 +4976,7 @@ window.__ModuleLoader__.load({
4877
4976
  if (s.ask !== null) children.push(react.createElement(AskModal, { key: "ask" }));
4878
4977
  if (s.confirmAsk !== null) children.push(react.createElement(ConfirmModal, { key: "confirm" }));
4879
4978
  children.push(react.createElement(Toasts, { key: "toasts" }));
4880
- return react.createElement("div", { className: "vx-root" }, children);
4979
+ return react.createElement("div", { className: `vx-root ${s.dark ? "vx-dark" : "vx-light"}` }, children);
4881
4980
  }
4882
4981
  setDynCss("");
4883
4982
  try {