balance-dsh 2.3.1 → 2.3.2

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/lib/client.js CHANGED
@@ -40,10 +40,85 @@ window.__ModuleLoader__.load({
40
40
  // `Rendered more hooks than during the previous render`,整块 UI 白掉。
41
41
  // 用 no-op 占位后 hook 数量恒定,缺失时的返回值与原来的 `null` / `undefined` 完全一致,
42
42
  // 所以**行为与视觉零变化**,只是把合规风险消掉了。
43
+ //
44
+ // ⚠️ 2026-11-19(DSH 0.2.2-rc.5):`primitives.useAnchoredPosition` **已被移除**
45
+ // —— 官方升级指南 `v0.2.2-alpha.2/css-anchored-menus`:
46
+ // 「`@deepseek-ai/dsh-client-ui-primitives` 移除 `useAnchoredPosition` 值导出和
47
+ // `AnchoredPositionOptions` 类型导出。`Menu` 将 `getAnchorRect` 替换为 `anchorRef`…」
48
+ // 它原来负责给「本会话消耗明细」浮层算 top/left。缺了之后原来退回 `() => null`,
49
+ // 而 `.balance-usage-panel` 的 CSS 只有 `position:fixed`、**没有 top/left** ——
50
+ // portal 到 body 后它落在文档流原位(页面底部附近),用户点开明细看到浮层乱跑。
51
+ // 所以这里补一个等价的自实现(不依赖 CSS Anchor Positioning 的浏览器支持度):
52
+ // 量锚点矩形 → 优先放上面、放不下翻到下面 → 水平居中并夹进视口 → 跟随 resize / 滚动 /
53
+ // 两侧尺寸变化重算。⚠️ 不用 requestAnimationFrame(DSH 渲染进程里窗口不可见时它会被挂起)。
54
+ function useAnchoredPositionFallback(options) {
55
+ const open = options?.open === true;
56
+ const anchorRef = options?.anchorRef;
57
+ const panelRef = options?.panelRef;
58
+ const side = options?.side === "bottom" ? "bottom" : "top";
59
+ const gap = Number(options?.gap ?? 6) || 0;
60
+ const margin = Number(options?.margin ?? 8) || 0;
61
+ const [style, setStyle] = react.useState(null);
62
+ react.useEffect(() => {
63
+ if (!open) {
64
+ setStyle(null);
65
+ return;
66
+ }
67
+ let timer = null;
68
+ let disposed = false;
69
+ const compute = () => {
70
+ if (disposed) return;
71
+ const anchor = anchorRef?.current;
72
+ const panel = panelRef?.current;
73
+ if (anchor == null || typeof anchor.getBoundingClientRect !== "function") return;
74
+ const ar = anchor.getBoundingClientRect();
75
+ if (ar.width === 0 && ar.height === 0) return;
76
+ const pw = panel != null ? panel.offsetWidth : 0;
77
+ const ph = panel != null ? panel.offsetHeight : 0;
78
+ const vw = window.innerWidth || 0;
79
+ const vh = window.innerHeight || 0;
80
+ let left = Math.round(ar.left + ar.width / 2 - pw / 2);
81
+ if (vw > 0) left = Math.max(margin, Math.min(left, vw - pw - margin));
82
+ let top = side === "top" ? Math.round(ar.top - ph - gap) : Math.round(ar.bottom + gap);
83
+ if (top < margin) top = Math.round(ar.bottom + gap);
84
+ if (vh > 0 && top + ph > vh - margin) top = Math.max(margin, vh - ph - margin);
85
+ // 同值不换对象:避免每次 resize 都触发一次无意义的重渲染
86
+ setStyle((prev) =>
87
+ prev !== null && prev.left === left && prev.top === top
88
+ ? prev
89
+ : { position: "fixed", left, top, right: "auto", bottom: "auto" },
90
+ );
91
+ };
92
+ const schedule = () => {
93
+ if (disposed || timer !== null) return;
94
+ timer = setTimeout(() => {
95
+ timer = null;
96
+ compute();
97
+ }, 16);
98
+ };
99
+ // 首帧同步算一次:panel 已经在 DOM 里(detailsOpen 变 true 之后才渲染)
100
+ compute();
101
+ window.addEventListener("resize", schedule);
102
+ window.addEventListener("scroll", schedule, true);
103
+ const ro = typeof ResizeObserver === "function" ? new ResizeObserver(schedule) : null;
104
+ if (ro !== null) {
105
+ if (anchorRef?.current != null) ro.observe(anchorRef.current);
106
+ if (panelRef?.current != null) ro.observe(panelRef.current);
107
+ }
108
+ return () => {
109
+ disposed = true;
110
+ if (timer !== null) clearTimeout(timer);
111
+ window.removeEventListener("resize", schedule);
112
+ window.removeEventListener("scroll", schedule, true);
113
+ if (ro !== null) ro.disconnect();
114
+ };
115
+ }, [open, side, gap, margin, anchorRef, panelRef]);
116
+ return style;
117
+ }
43
118
  const useAnchoredPositionHook =
44
119
  typeof primitives.useAnchoredPosition === "function"
45
120
  ? primitives.useAnchoredPosition.bind(primitives)
46
- : () => null;
121
+ : useAnchoredPositionFallback;
47
122
  const useDismissOnOutsidePointerHook =
48
123
  typeof primitives.useDismissOnOutsidePointer === "function"
49
124
  ? primitives.useDismissOnOutsidePointer.bind(primitives)
package/lib/index.js CHANGED
@@ -1077,7 +1077,7 @@ function formatBalance(balance) {
1077
1077
  // | { status:"failed" } (网络失败;官方明确“失败绝不变成 0 余额”)
1078
1078
  // client 是 AccountClientMetadata(version / locale / timezoneOffsetSeconds),
1079
1079
  // 缺字段会在平台侧组装请求头时抛错,所以三个字段都要给。
1080
- const PLUGIN_VERSION = "2.3.1";
1080
+ const PLUGIN_VERSION = "2.3.2";
1081
1081
 
1082
1082
  /** 斜杠命令名(2026-10-31 定案:用带作者后缀的全名,避免和别的余额插件撞车,也不设备用名)。
1083
1083
  * ⚠️ DSH 的 COMMAND_NAME = /^[a-z][a-z0-9_-]*$/u —— **只允许小写**。
@@ -1758,7 +1758,23 @@ const WEB_TOOL_BILLING_START = Date.parse("2026-11-01T00:00:00+08:00");
1758
1758
  const WEB_TOOL_PRICE_PER_CALL = 5 / 1000;
1759
1759
 
1760
1760
  /** 统计网页工具调用:次数、可计费次数与金额。
1761
- * 只认 tool/call 与 tool/result 配对成功、且 isError !== true 的调用(官方口径:失败不计费)。 */
1761
+ * 只认 tool/call 与 tool/result 配对成功、且 isError !== true 的调用(官方口径:失败不计费)。
1762
+ *
1763
+ * ⚠️ 2026-11-19(DSH 0.2.2-rc.4 起,用户「更新了新的 dsh 看看有哪里需要适配」):
1764
+ * 搜索的计费口径变了。官方升级指南 `v0.2.2-rc.4/web-search-deepseek-model-default`:
1765
+ * 「为 `deepseek-official` / `deepseek-account` 路由上的模型请求发起的搜索使用
1766
+ * `deepseek-official` 搜索提供方:它向 `web-search-deepseek` 的端点发送一次辅助
1767
+ * Messages 请求,**不按次收费,按模型 token 计费**,并向会话日志追加一个
1768
+ * `web/deepseek-search-llm-request` 事件。」
1769
+ * `dsh-web-search-deepseek/README.md` 也写明:「DeepSeek makes no per-search charge for it,
1770
+ * so its cost is that model turn's tokens.」
1771
+ *
1772
+ * 于是走这条路线的 `web_search` **不该再按 ¥5/千次 计**。而那次辅助 Messages 请求是
1773
+ * **log-only** 事件(只带 endpoint / apiVersion / body,不含 usage),插件**拿不到**它的
1774
+ * token —— 所以只能做到「不按次收费」,那部分 token 成本无法估算。
1775
+ *
1776
+ * 判据:会话里出现 `web/deepseek-search-llm-request` = 该次搜索走 Messages 搜索 = 不计次费。
1777
+ * 下面按「每个北京日期有几条该事件」做抵扣,数量上限为该日可计费的搜索次数。 */
1762
1778
  function webToolUsage(events) {
1763
1779
  const calls = new Map(); // callId -> { name, time }
1764
1780
  for (const event of events ?? []) {
@@ -1791,13 +1807,25 @@ function webToolUsage(events) {
1791
1807
  if (call.time >= WEB_TOOL_BILLING_START) {
1792
1808
  if (call.name === "web_search") {
1793
1809
  webSearchBillable += 1;
1794
- webSearchCost += WEB_TOOL_PRICE_PER_CALL;
1795
1810
  } else {
1796
1811
  webFetchBillable += 1;
1797
1812
  webFetchCost += WEB_TOOL_PRICE_PER_CALL;
1798
1813
  }
1799
1814
  }
1800
1815
  }
1816
+ // 走 Messages 搜索的那几次不按次收费(见函数头注释)
1817
+ let llmSearchCount = 0;
1818
+ for (const event of events ?? []) {
1819
+ if (event.type !== "web/deepseek-search-llm-request") continue;
1820
+ const time = Number(event.time ?? 0);
1821
+ if (!Number.isFinite(time) || time < WEB_TOOL_BILLING_START) continue;
1822
+ llmSearchCount += 1;
1823
+ }
1824
+ if (llmSearchCount > 0) {
1825
+ webSearchBillable = Math.max(0, webSearchBillable - Math.min(llmSearchCount, webSearchBillable));
1826
+ }
1827
+ // 统一按次数重算,避免逐次累加的浮点漂移
1828
+ webSearchCost = webSearchBillable * WEB_TOOL_PRICE_PER_CALL;
1801
1829
  return {
1802
1830
  webSearchCalls,
1803
1831
  webFetchCalls,
@@ -1806,6 +1834,7 @@ function webToolUsage(events) {
1806
1834
  webSearchCost,
1807
1835
  webFetchCost,
1808
1836
  webToolCost: webSearchCost + webFetchCost,
1837
+ llmSearchCount,
1809
1838
  };
1810
1839
  }
1811
1840
 
@@ -2776,6 +2805,9 @@ function ensureDay(days, key) {
2776
2805
  // 网页搜索 / 抓取的工具调用次数与按次费用(官方 ¥5/千次,2026-11-01 起计费)
2777
2806
  webSearchCalls: 0,
2778
2807
  webFetchCalls: 0,
2808
+ // 走「旧版 Messages 搜索」的搜索次数(2026-11-19 起):这部分**不按次收费**,
2809
+ // 只用于展示与核对,不计入 webSearchBillable / webSearchCost。
2810
+ webSearchLlm: 0,
2779
2811
  webSearchBillable: 0,
2780
2812
  webFetchBillable: 0,
2781
2813
  webSearchCost: 0,
@@ -2961,6 +2993,20 @@ function collectDailyUsage(events, config, fallbackModel, cutoffMs, days, sessio
2961
2993
  }
2962
2994
  // 网页搜索 / 抓取:按调用发生的北京日期归日,金额按官方「每千次 ¥5」、且只在
2963
2995
  // 2026-11-01 00:00(北京)之后计费。tool/call 与 tool/result 配对 + isError 过滤同 webToolUsage。
2996
+ //
2997
+ // ⚠️ 2026-11-19(DSH 0.2.2-rc.4 起):走「旧版 Messages 搜索」的 `web_search` **不按次收费**
2998
+ // (官方 README:「no per-search charge,成本是那一次模型调用的 token」)。
2999
+ // 判据是 `web/deepseek-search-llm-request` 事件 —— 每次走 Messages 搜索的搜索都追加一条。
3000
+ // 这里先按天统计该事件的条数作为「抵扣额度」,累加时优先用掉额度、额度用尽的才计次费。
3001
+ const llmBudget = new Map(); // 北京日期 -> 还能抵扣几次
3002
+ for (const event of events ?? []) {
3003
+ if (event.type !== "web/deepseek-search-llm-request") continue;
3004
+ const time = Number(event.time ?? 0);
3005
+ if (!Number.isFinite(time) || time < cutoffMs) continue;
3006
+ if (time < WEB_TOOL_BILLING_START) continue;
3007
+ const k = beijingDayKey(time);
3008
+ llmBudget.set(k, (llmBudget.get(k) ?? 0) + 1);
3009
+ }
2964
3010
  const webCalls = new Map(); // callId -> { name, time }
2965
3011
  for (const event of events ?? []) {
2966
3012
  if (event.type !== "tool/call") continue;
@@ -2982,11 +3028,17 @@ function collectDailyUsage(events, config, fallbackModel, cutoffMs, days, sessio
2982
3028
  if (call === void 0 || seen.has(key)) continue;
2983
3029
  seen.add(key);
2984
3030
  if (event.data?.message?.isError === true) continue;
2985
- const day = ensureDay(days, beijingDayKey(call.time));
3031
+ const dayKey = beijingDayKey(call.time);
3032
+ const day = ensureDay(days, dayKey);
2986
3033
  const billable = call.time >= WEB_TOOL_BILLING_START;
2987
3034
  if (call.name === "web_search") {
2988
3035
  day.webSearchCalls += 1;
2989
- if (billable) {
3036
+ // 这次是否走了 Messages 搜索(额度按天发放,先到先扣)
3037
+ const left = llmBudget.get(dayKey) ?? 0;
3038
+ const viaMessages = left > 0;
3039
+ if (viaMessages) llmBudget.set(dayKey, left - 1);
3040
+ day.webSearchLlm += viaMessages ? 1 : 0;
3041
+ if (billable && !viaMessages) {
2990
3042
  day.webSearchBillable += 1;
2991
3043
  day.webSearchCost += WEB_TOOL_PRICE_PER_CALL;
2992
3044
  day.cost += WEB_TOOL_PRICE_PER_CALL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "balance-dsh",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "在 DeepSeek Harness 中显示 DeepSeek 账户余额和当前会话 token/费用消耗(含高峰/空闲峰谷计费)。仅支持 DeepSeek 官方模型与账号,暂不支持其他大模型。",
5
5
  "author": "Rannist",
6
6
  "type": "module",
@@ -26,9 +26,6 @@
26
26
  "patch": "./cordis.patch.yml"
27
27
  },
28
28
  "client": {
29
- "inject": [
30
- "@deepseek-ai/dsh-client-runtime"
31
- ],
32
29
  "platform": "web",
33
30
  "immediately": true
34
31
  }