dsh-message-actions 0.2.1 → 0.2.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/store.cjs CHANGED
@@ -16,6 +16,8 @@ const os = require("node:os");
16
16
  const path = require("node:path");
17
17
 
18
18
  const STATE_VERSION = 1;
19
+ /** 默认预算 ¥5:首次运行、旧状态文件缺字段时都落到这里。 */
20
+ const DEFAULT_BUDGET_CENTS = 500;
19
21
  const FILE_NAME = "cost-state.json";
20
22
  const DIR_NAME = "dsh-message-actions";
21
23
  /** 状态文件读取上限:正常只有几 KB,超限视为异常文件,不再解析。 */
@@ -55,10 +57,11 @@ function defaultState() {
55
57
  lastRouting: null,
56
58
  observedSpendTotalCents: 0,
57
59
  dailySpendCents: {},
58
- pendingAdjustmentCents: 0,
59
- pendingAdjustmentAt: 0,
60
- correctionCreditCents: 0,
61
- correctionDebitCents: 0,
60
+ settledSpendTotalCents: 0,
61
+ settledSinceObserveCents: 0,
62
+ lastSettledKey: "",
63
+ /** 会话统计的预算(分,人民币);0 表示未启用预算比例。 */
64
+ budgetCents: DEFAULT_BUDGET_CENTS,
62
65
  updatedAt: 0
63
66
  };
64
67
  }
@@ -99,10 +102,11 @@ function normalizeCostState(raw) {
99
102
  lastRouting: typeof raw.lastRouting === "string" ? raw.lastRouting : null,
100
103
  observedSpendTotalCents: intOr(raw.observedSpendTotalCents, 0),
101
104
  dailySpendCents: daily,
102
- pendingAdjustmentCents: intOr(raw.pendingAdjustmentCents, 0),
103
- pendingAdjustmentAt: intOr(raw.pendingAdjustmentAt, 0),
104
- correctionCreditCents: intOr(raw.correctionCreditCents, 0),
105
- correctionDebitCents: intOr(raw.correctionDebitCents, 0),
105
+ settledSpendTotalCents: intOr(raw.settledSpendTotalCents, 0),
106
+ settledSinceObserveCents: Math.max(0, intOr(raw.settledSinceObserveCents, 0)),
107
+ lastSettledKey: typeof raw.lastSettledKey === "string" ? raw.lastSettledKey : "",
108
+ // 旧状态文件没有该字段:按默认预算补齐,不视为损坏。
109
+ budgetCents: Math.max(0, intOr(raw.budgetCents, DEFAULT_BUDGET_CENTS)),
106
110
  updatedAt: intOr(raw.updatedAt, 0)
107
111
  };
108
112
  return { state, readOnly };
@@ -249,6 +253,7 @@ function persistCostState(filePath, state, readOnly, logger) {
249
253
 
250
254
  module.exports = {
251
255
  STATE_VERSION,
256
+ DEFAULT_BUDGET_CENTS,
252
257
  MAX_STATE_BYTES,
253
258
  expandHome,
254
259
  resolveDshHome,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-message-actions",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "消息费用:撤回、删除、删至末尾、分享、导出、回答统计与 DeepSeek 费用统计(DSH 插件)。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 aijacko
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/README.cn.md DELETED
@@ -1,250 +0,0 @@
1
- # DSH 插件 dsh-message-actions
2
-
3
- [English](README.md) · 中文
4
-
5
- ## 简介
6
-
7
- **让 DSH 的每一轮对话都可控、可分享、可计量。**
8
-
9
- DSH 本身没有撤回和删除,发错一句只能重开会话;想把一段对话发给别人,也得自己复制排版。部分插件功能堆得重、数据去向不透明,于是干脆自己写一个:需要的功能都有,能不碰的数据就不碰。
10
-
11
- - 发错消息:鼠标移到提问行点「撤回」,整轮问答消失,原文回到输入框,改完再发。
12
- - 想清掉跑偏的探索:问行「删除」删整轮;只去掉某一步的工具调用,就点该工具行右侧的垃圾桶。
13
- - 想分享给同事:多选轮次,导出 Markdown 或生成长图 PNG,都在本地完成、不上传。
14
- - 想知道刚才那轮花了多少:侧边栏显示余额 / 日用 / 单次消耗(卡片上简写为「单次」);本地模型自动收起为「本地模型 · 无需计费」。
15
-
16
- 用不上的功能可以在设置里关闭。比如关掉费用统计后,插件会停止余额取数与记账,也不再渲染侧边栏卡片;删除、分享、导出等开关同理,关掉后对应入口与动作都不再出现。它不常驻面板、不弹更新提示、不发系统通知,也不会改写你的提问;分享 / 导出会打开临时全屏选择浮层,撤回 / 删除会按设置弹出确认框,都是正常交互。
17
-
18
- ---
19
-
20
- ## 界面截图
21
-
22
- **提问行操作按钮、回答下方的统计信息、侧边栏底部的费用卡片**
23
-
24
- ![提问行操作、回答统计与费用卡片](pic/1.png)
25
-
26
- **设置 → 消息费用 → 消息操作**
27
-
28
- ![消息操作标签](pic/2.png)
29
-
30
- **设置 → 消息费用 → 费用统计**
31
-
32
- ![费用统计标签](pic/3.png)
33
-
34
- ---
35
-
36
- ## 功能
37
-
38
- | 功能 | 说明 |
39
- |---|---|
40
- | **撤回** | 点一下,整轮问答从会话里消失,原文自动回到输入框,改完再发 |
41
- | **删除** | 问行删整轮问答;工具行删该步内容(同一步的答行 / 其它工具结果会一并遮蔽,其它步骤不受影响) |
42
- | **删至末尾** | 从这一条起全部清掉,适合把一个话题彻底收尾 |
43
- | **分享长图** | 全屏勾选想要的轮次,一键生成长图 PNG,本地生成、不上传 |
44
- | **导出 Markdown** | 同样多选,按轮次导出带「问 / 答」结构的 `.md` |
45
- | **回答统计** | 每条回答下方显示 `tok · 字`,一眼看清这次回复的体量 |
46
- | **导航同步** | 删除后侧边栏对应刻度自动隐藏并收拢空隙,不留「幽灵条目」 |
47
- | **费用卡片** | 余额 / 日用 / 单次消耗 一眼看完,有单行 / 多行两套计费样式 |
48
- | **峰谷提示** | 高峰橙、谷价蓝,告诉你现在贵不贵、几点切换 |
49
- | **余额校正** | 充值、赠金、异常扣减导致数字对不上时,一次性校正 |
50
- | **中英双语** | 跟随 DSH 语言,界面与悬浮提示自动切换 |
51
- | **浮层键盘** | 分享 / 导出浮层内 `Enter` 执行主操作(生成图片 / 导出);确认框内 `Enter` 确认、`Esc` 取消;焦点在输入框内时不拦截,主操作禁用或生成中时不做任何事 |
52
-
53
- 删除采用的是**墓碑式表面替换**:只追加一条标记并遮蔽原区间,不改写历史日志,也不影响其他会话。标记持久化在会话事件日志里,因此刷新页面、切换到其它对话再切回、以及重启 DSH 后,被删内容都不会重新出现。`/messages` 与 `full=1` 都会按墓碑重算可见节点,而不是只相信宿主表面。
54
-
55
- ---
56
-
57
- ## 费用卡片
58
-
59
- **单行样式**(默认,无背景、单行居左):
60
-
61
- ```
62
- ▲ 余额 ¥6.00 日用 ¥0.08 单次 ¥0.02
63
- ```
64
-
65
- **多行样式**(带背景卡片与峰谷文字行):
66
-
67
- ```
68
- 余额 ¥6.00 日用 ¥0.08 单次 ¥0.02
69
- ▲ 谷价 · 明日 9:00 起高峰
70
- ```
71
-
72
- 新安装默认是**单行样式**;已经保存过偏好的用户沿用原来的选择,不会因为改默认值被重置。
73
-
74
- - 高峰 = 暖橙,谷价 = 冷蓝;14px SVG 状态图标,左侧一眼可辨。
75
- - 侧边栏收起时自动降级为一个钱币图标,悬停即可看到余额。
76
- - 本地模型 / 非 DeepSeek 路由自动收起为「本地模型 · 无需计费」,不打扰。
77
-
78
- | 项 | 含义 |
79
- |---|---|
80
- | 余额 | DeepSeek 账户当前余额(人民币),最短间隔 60 秒刷新一次;点击卡片可强制刷新,不受该间隔限制 |
81
- | 日用 | 今天(北京时间)观测到的余额下降累计值 |
82
- | 单次 | 最近一轮消耗的换算金额;默认常驻,可设 0–600 秒后自动隐藏 |
83
-
84
- - 「单次消耗」按官方价目(`deepseek-flash` / `deepseek-v4-pro`)与实际用量估算;内部记账精度高于界面显示精度,避免很小的费用被舍成 0。
85
- - 峰谷按北京时间:工作日 `09:00–12:00`、`14:00–18:00` 为高峰,其余为谷价;谷价为高峰的 `0.5` 倍。
86
- - 「日用」来自账户余额的实际下降观测,与「单次消耗」口径不同,不要求逐轮对齐。
87
- - 余额上升时不会冲减已记消费,而是标记「待核对」,引导到「余额校正」。
88
-
89
- ---
90
-
91
- ## 安装
92
-
93
- ### 插件市场
94
-
95
- 在 DSH 插件市场搜索 `dsh-message-actions` 安装;尚未上架时可改用下方的本地安装。安装后重启 DSH。
96
-
97
- ### 本地安装
98
-
99
- ```bash
100
- git clone <repository-url> dsh-message-actions
101
- cd dsh-message-actions
102
- node build.mjs # 生成 lib/
103
- node test/check.mjs # 应输出 ALL CHECKS PASSED
104
- ```
105
-
106
- 把构建后的目录装进 DSH profile:
107
-
108
- ```bash
109
- pnpm add "file:/path/to/dsh-message-actions"
110
- ```
111
-
112
- - `pnpm add file:` 是**复制**语义:改代码后需卸载重装(或加 `--force`)。
113
- - 宿主侧改动(`src/index.mjs`、Config)需重启 DSH;纯客户端改动可「禁用 → 启用插件」或刷新页面。
114
- - 登记:把包名加入 profile `package.json` 的 `dsh.profile.bundles`。
115
- **不要**在 profile 的 `cordis.patch.yml` 里再追加 `insert`——插件包自带的 patch 已在 bundle 层插入自身,重复 insert 会导致设置页「无法保存设置」。
116
-
117
- ---
118
-
119
- ## 使用
120
-
121
- 1. 鼠标悬停在任意**提问**上,右侧出现:撤回、分享、导出、删除、删至末尾。
122
- 2. 鼠标悬停在**工具调用行**上,右侧出现「删除该工具行」;删除的是该 `(turn, step)` 的节点集合——同一步的答行 / 其它工具结果会一并消失,其它步骤不受影响。
123
- 3. 每条**回答**下方显示 `tok · 字`。
124
- 4. 撤回与删除类操作默认弹出确认框;关闭「操作确认」后直接执行;确认后对应内容消失,侧边栏刻度同步隐藏。
125
- 5. 分享 / 导出打开全屏选择面板,每张卡片显示该轮的「问」与「答」预览,可单选、多选或全选;生成时再按需拉取完整原文。拉全文或生成图片 / 文件失败时会提示,并**保留面板与已选轮次**方便重试,只有成功下载才关闭。
126
- 6. 生成回复期间所有按钮置灰,避免与运行中的会话冲突;极端竞态下的写入仍会返回 `423 AGENT_BUSY`。
127
-
128
- ### 余额校正
129
-
130
- 设置 → 消息费用 → 费用统计 → **余额校正**:
131
-
132
- ```
133
- 校正后余额 = 基线 − 已观测消费 + 实际累计到账 − 非调用扣减
134
- ```
135
-
136
- 两项均为**累计口径**(填「到目前为止的总数」,覆盖保存),允许校正后余额为负。
137
-
138
- ---
139
-
140
- ## 设置
141
-
142
- 设置 → 插件 → **消息费用**(English: `Messages & Cost`),页内两个标签,改动**即时保存并立即生效**。
143
-
144
- **消息操作**
145
-
146
- | 开关 | 作用 |
147
- |---|---|
148
- | 撤回 | 提问行显示「撤回」 |
149
- | 分享 | 提问行显示「分享」 |
150
- | 导出 | 提问行显示「导出」 |
151
- | 删除 | 提问行删除整轮问答;工具行删除该步工具结果 |
152
- | 删至末尾 | 提问行显示「删至末尾」 |
153
- | 操作确认 | 撤回与删除类操作是否弹确认框 |
154
- | 回答统计 | 回答下方是否显示 `tok · 字` |
155
-
156
- **费用统计**
157
-
158
- | 设置 | 作用 |
159
- |---|---|
160
- | 费用统计 | 侧边栏显示余额 / 日用 / 单次消耗;关闭后停止取数与记账 |
161
- | 计费样式 | `单行样式`(默认,无背景)/ `多行样式`(带背景与峰谷行),即时切换并持久保存 |
162
- | 单次消耗 | 每轮结束后显示本轮金额;关闭后不再渲染该组 |
163
- | 自动关闭 | 本轮金额显示秒数(0–600,0 = 常驻) |
164
- | 峰谷定价行 | 显示当前时段与下一次切换时间 |
165
- | 余额校正 | 见上节 |
166
-
167
- ---
168
-
169
- ## 隐私与安全
170
-
171
- 这一节只写代码里实际做了的事情。它是降低风险的设计,不是「零风险」的承诺;下面每条都能在 `src/` 里找到对应实现。
172
-
173
- **接口只对本机开放**
174
-
175
- - 插件不自建端口,路由注册在 DSH 现有的 `webServer` 上。
176
- - 每个请求都校验 `Host` 必须是 `localhost`、`127.0.0.0/8` 或 `::1`,否则返回 `403`;这挡住了 DNS rebinding 与局域网来源。
177
- - 浏览器请求还要过 `Sec-Fetch-Site`:只接受 `same-origin` / `none`,或非浏览器客户端缺失该头;显式拒绝 `cross-site` 与 `same-site`。
178
- - 携带 `Origin` 的请求必须与宿主当前同源;宿主给不出期望 `Origin` 时,带 `Origin` 的请求一律拒绝。
179
-
180
- **响应头与写请求**
181
-
182
- - 所有 JSON 响应统一带 `cache-control: no-store`、`x-content-type-options: nosniff`、`referrer-policy: no-referrer`、`cross-origin-resource-policy: same-origin`、`content-security-policy: default-src 'none'; frame-ancestors 'none'`。
183
- - 写请求只接受 `content-type: application/json`;请求体上限 1MB,超限返回 `413` 并排空连接;方法不匹配返回 `405`(带 `allow` 头),错误体同样是统一 JSON。
184
- - 不返回任何 CORS 放行头。
185
-
186
- **会话数据**
187
-
188
- - `GET /messages` 是**摘要接口**:只回消息 id、轮次、step、最多 200 字符的短预览、用量与工具 `callId`,不含正文。客户端不设固定轮询定时器:会话 DOM 变化时触发,经 120ms 合并调度,并用 3 秒 TTL 避免重复请求。
189
- - 摘要支持增量:带 `since=<上次 version>` 只回 `seq > since` 的行(并附 `removedSeqs` 告知这段时间被删的行);不带 `since` 仍是全量快照。删除状态(`goneTurns` / `goneTools`)始终全量返回,因此增量刷新后已删内容不会复活。
190
- - `preview` 只对真实提问与助手回答生成;system / developer 消息、运行时注入的上下文与工具结果一律回空,正文不会被该接口回传(代码位置:`src/shared.cjs` 的 `messagesSummaryOf`)。
191
- - 分享图与导出需要全文时,才按选中的轮次请求 `full=1`,且只返回问 / 答文本。
192
-
193
- **费用数据**
194
-
195
- - 只存一个本地文件:`$DSH_HOME`(默认 `~/.dsh`)下的 `plugins/dsh-message-actions/cost-state.json`。
196
- - 写入方式是「同目录临时文件(`0600`)→ `fsync` → `rename`」整份替换,避免写一半留下坏文件;目录按 `0700` 创建。
197
- - 内容只有余额观测、校正值与按日消费,没有对话正文。
198
- - 余额校正只在状态文件写成功后才算成功:只读状态文件返回 `500 READ_ONLY`,写盘失败返回 `500 WRITE_FAILED` 并回滚内存中的校正值,界面提示「保存失败」而不是假装成功。
199
- - 文件超过 4MB 会自动重建为默认状态,并记一条含原因与大小的诊断日志;未超限但 JSON 损坏时,原文件改名为 `cost-state.bad-<时间戳>.json` 留档。
200
-
201
- **网络**
202
-
203
- - 插件自身不直接向外部地址发起请求;客户端只请求同源的 `/dsh-message-actions/*`。
204
- - 余额通过宿主已登录的 DeepSeek 账户服务(`deepseekAccount`)读取,这一步的网络请求由宿主发起;未登录时显示「未登录」。
205
- - 插件不保存任何密钥;判断是否走 API key 时只向宿主凭据服务查询「是否存在」,不落盘、不回传。
206
-
207
- ---
208
-
209
- ## 已知限制
210
-
211
- - 删除粒度:问行删整轮;工具行删该 step,会连带遮蔽同一步的答行与其它工具结果,其它步骤不受影响。
212
- - 不支持撤销删除,也不会清理跨会话记忆。
213
- - 删除标记保存在会话事件日志里:刷新、切换对话、重启 DSH 都不会恢复;如果手动删除整个会话目录,标记会随会话一起消失。
214
- - 若宿主重建会话表面时没有重放墓碑,插件会根据 `goneTools` 重新隐藏工具行;这些行仍留在 DOM 中(CSS `display:none`),超大会话可能保留少量不可见节点。
215
- - 侧边栏刻度由官方虚拟列表绘制,删过内容后滚过该区域可能出现空白。
216
- - 入口依赖宿主的会话 DOM 结构:DSH 大版本升级后按钮可能不再出现(结果是静默消失,不会误删)。
217
- - 「单次消耗」只含大模型调用费用,不含搜索 / 插件等外部计费。
218
- - Web 模式未实测:同机 `localhost` 可能可用,局域网 / 远程访问会被安全围栏拒绝。
219
-
220
- ---
221
-
222
- ## 开发
223
-
224
- ```bash
225
- node build.mjs # 构建到 lib/
226
- node test/check.mjs # 离线断言,当前 2609 项
227
- # 另有真实会话日志回归:18/18
228
- ```
229
-
230
- ```
231
- src/
232
- ├── index.mjs # 宿主:Config + 4 条 HTTP 路由 + 费用服务
233
- ├── client.js # 客户端:槽位、DOM 注入、分享 / 导出浮层、费用卡片
234
- ├── shared.cjs # 宿主与客户端共用的纯函数(可离线测试)
235
- ├── store.cjs # 费用状态文件原子读写、损坏恢复
236
- ├── http.cjs # HTTP 工具:JSON 响应、413 排空、请求体读取
237
- └── i18n.cjs # 中英文案单一来源(构建时生成 locale/*.json)
238
- test/check.mjs # 无依赖的断言脚本
239
- ```
240
-
241
-
242
- - 版本:`0.2.1`
243
- - 兼容:在 DSH `0.2.1-rc.5` 桌面版(profile `desktop`)实测通过
244
- - 依赖:无运行时第三方依赖
245
-
246
- ---
247
-
248
- ## License
249
-
250
- [MIT](LICENSE)
package/README.md DELETED
@@ -1,250 +0,0 @@
1
- # DSH plugin dsh-message-actions
2
-
3
- English · [中文](README.cn.md)
4
-
5
- ## Overview
6
-
7
- **Make every turn in DSH controllable, shareable, and measurable.**
8
-
9
- DSH has no recall or delete built in: send a message by mistake and your only option is to start a new session; share a conversation and you end up copying, pasting, and re-formatting by hand. Some plugins are overloaded with features or vague about where conversation data ends up, so I wrote my own: the features I needed, and data I don't need to touch stays untouched.
10
-
11
- - Sent the wrong message: hover over a question row, click "Recall", the whole turn disappears and the text returns to the composer; edit and resend.
12
- - Cleaning up a tangent: "Delete" on the question row removes the whole turn; to drop just one step's tool call, click the trash icon on that tool row.
13
- - Sharing with a colleague: select the turns you want, export Markdown or generate a long PNG, all locally, nothing uploaded.
14
- - Wondering what the last turn cost: the sidebar shows balance / today / per-turn cost (shown as "Bal / Today / Turn" on the card); local models collapse to "Local · no billing".
15
-
16
- Features you don't use can be turned off in settings. Turn off cost statistics, for example, and the plugin stops fetching balances and recording spend, and stops rendering the sidebar card. The delete, share, and export switches work the same way: turn them off and their entries and actions disappear. It adds no persistent panel, no update prompts, and no system notifications, and it never rewrites your question. Share and export open a temporary full-screen picker, and recall / delete show a confirmation dialog according to your settings — all normal interactions.
17
-
18
- ---
19
-
20
- ## Screenshots
21
-
22
- **Actions on the question row, reply statistics under answers, and the cost card at the bottom of the sidebar**
23
-
24
- ![Message actions, reply statistics and cost card](pic/en1.png)
25
-
26
- **Settings → Messages & Cost → Message Actions**
27
-
28
- ![Message Actions tab](pic/en2.png)
29
-
30
- **Settings → Messages & Cost → Cost Statistics**
31
-
32
- ![Cost Statistics tab](pic/en3.png)
33
-
34
- ---
35
-
36
- ## Features
37
-
38
- | Feature | What it does |
39
- |---|---|
40
- | **Recall** | One click removes the whole exchange from the session and restores the original text to the composer, ready to resend |
41
- | **Delete** | Question row deletes the whole exchange; tool row deletes that step (the answer and other tool results in the same step are hidden too, other steps are unaffected) |
42
- | **Delete to end** | Removes everything from this message to the end, for closing off a topic |
43
- | **Share as image** | Pick the turns you want in a full-screen overlay and generate a long PNG, generated locally, never uploaded |
44
- | **Export Markdown** | Same selection, exports a `.md` with question / answer structure per turn |
45
- | **Reply statistics** | `tok · chars` under every answer, the size of a reply at a glance |
46
- | **Rail sync** | After deletion, the matching sidebar ticks hide and the gaps close, leaving no ghost entries |
47
- | **Cost card** | Balance / today / per-turn cost at a glance, with single-line and multi-line billing styles |
48
- | **Peak / off-peak hint** | Peak in orange, off-peak in blue, showing whether now is expensive and when it switches |
49
- | **Balance correction** | One-off correction when top-ups, credits, or unexpected deductions make the numbers disagree |
50
- | **Bilingual UI** | Follows the DSH language; interface and tooltips switch automatically |
51
- | **Overlay keyboard** | In the share / export overlay, `Enter` runs the primary action (Share image / Export); in a confirmation dialog it confirms, and `Esc` cancels. Enter is ignored while focus is in a text field, and when the primary action is disabled or busy |
52
-
53
- Deletion uses **tombstone-style surface replacement**: it only appends a marker and hides the original span; it never rewrites the history log and doesn't affect other sessions. The marker is persisted in the session event log, so deleted content stays hidden after a page refresh, after switching to another conversation and back, and after restarting DSH. `/messages` and `full=1` both recompute the visible set from those markers instead of trusting the host surface alone.
54
-
55
- ---
56
-
57
- ## Cost card
58
-
59
- **Single-line** (default, no background, one row aligned left):
60
-
61
- ```
62
- ▲ Bal ¥6.00 Today ¥0.08 Turn ¥0.02
63
- ```
64
-
65
- **Multi-line** (with a background card and a peak / off-peak text row):
66
-
67
- ```
68
- Bal ¥6.00 Today ¥0.08 Turn ¥0.02
69
- ▲ Off-peak · peak at 9:00 tomorrow
70
- ```
71
-
72
- New installations default to **single-line**; users who have already saved a preference keep their choice and are not reset by the new default.
73
-
74
- - Peak = warm orange, off-peak = cool blue; 14px SVG status icons, readable at a glance on the left.
75
- - When the sidebar collapses, the card degrades to a coin icon; hover to see the balance.
76
- - Local models / non-DeepSeek routing collapse to "Local · no billing", out of the way.
77
-
78
- | Item | Meaning |
79
- |---|---|
80
- | Bal | Current DeepSeek account balance (CNY), refreshed at a minimum interval of 60 seconds; clicking the card forces a refresh regardless of that interval |
81
- | Today | Cumulative balance drop observed today (Beijing time) |
82
- | Turn | Converted cost of the most recent turn; shown by default, can hide after 0–600 seconds |
83
-
84
- - "Turn" (per-turn cost) is estimated from official list prices (`deepseek-flash` / `deepseek-v4-pro`) and actual usage; internal accounting is more precise than the UI, so very small costs are not rounded down to zero.
85
- - Peak / off-peak uses Beijing time: weekdays `09:00–12:00` and `14:00–18:00` are peak, everything else is off-peak; off-peak is `0.5×` the peak price.
86
- - "Today" comes from observing actual balance drops; it uses a different basis from "per-turn cost" and is not expected to match turn by turn.
87
- - When the balance goes up, recorded spend is not reduced; the card shows "Check" and points you to balance correction.
88
-
89
- ---
90
-
91
- ## Installation
92
-
93
- ### Plugin marketplace
94
-
95
- Search for `dsh-message-actions` in the DSH plugin marketplace and install it (if it isn't listed yet, use the local install below), then restart DSH.
96
-
97
- ### Local install
98
-
99
- ```bash
100
- git clone <repository-url> dsh-message-actions
101
- cd dsh-message-actions
102
- node build.mjs # writes lib/
103
- node test/check.mjs # should print ALL CHECKS PASSED
104
- ```
105
-
106
- Install the built directory into a DSH profile:
107
-
108
- ```bash
109
- pnpm add "file:/path/to/dsh-message-actions"
110
- ```
111
-
112
- - `pnpm add file:` uses **copy** semantics: after editing code, reinstall (or pass `--force`).
113
- - Host-side changes (`src/index.mjs`, Config) need a DSH restart; client-only changes can be picked up by disabling → enabling the plugin, or refreshing the page.
114
- - Registration: add the package name to `dsh.profile.bundles` in the profile `package.json`.
115
- **Do not** append another `insert` in the profile's `cordis.patch.yml` — the package's own patch already inserts itself at the bundle layer, and a duplicate insert makes the settings page fail to save.
116
-
117
- ---
118
-
119
- ## Usage
120
-
121
- 1. Hover over any **question** row: Recall, Share, Export, Delete, Delete to end appear on the right.
122
- 2. Hover over a **tool-call row**: "Delete tool" appears on the right; it deletes the node set for that `(turn, step)` — the answer and other tool results in the same step disappear too; other steps are unaffected.
123
- 3. Under every **answer**, `tok · chars` is shown.
124
- 4. Recall and delete actions ask for confirmation by default; turn off "Confirm actions" to run them immediately. After confirming, the content disappears and the sidebar ticks hide in sync.
125
- 5. Share / export opens a full-screen picker; each card shows the question and answer preview for that turn, and you can select one, several, or all; full text is fetched only when you generate. If fetching full text or generating the image / file fails, a toast appears and the picker **stays open with your selection**, so you can retry; it closes only after a successful download.
126
- 6. While a reply is being generated, all buttons are disabled to avoid conflicting with the running session; a write that still races the agent returns `423 AGENT_BUSY`.
127
-
128
- ### Balance correction
129
-
130
- Settings → Messages & Cost → Cost Statistics → **Balance correction**:
131
-
132
- ```
133
- Corrected balance = baseline − observed spend + total credited − non-usage deductions
134
- ```
135
-
136
- Both inputs are **cumulative** ("total so far", saved as an overwrite); the corrected balance may be negative.
137
-
138
- ---
139
-
140
- ## Settings
141
-
142
- Settings → Plugins → **Messages & Cost**; the page has two tabs, and changes **save immediately and apply right away**.
143
-
144
- **Message Actions**
145
-
146
- | Toggle | Effect |
147
- |---|---|
148
- | Recall | Show "Recall" on question rows |
149
- | Share | Show "Share" on question rows |
150
- | Export | Show "Export" on question rows |
151
- | Delete | Question row deletes the whole exchange; tool row deletes that step's tool results |
152
- | Delete to end | Show "Delete to end" on question rows |
153
- | Confirm actions | Whether recall and delete actions ask for confirmation |
154
- | Reply statistics | Whether `tok · chars` is shown under answers |
155
-
156
- **Cost Statistics**
157
-
158
- | Setting | Effect |
159
- |---|---|
160
- | Cost statistics | Show balance / today / per-turn cost in the sidebar; when off, stop fetching and recording |
161
- | Style | `Single-line` (default, no background) / `Multi-line` (card with the peak / off-peak row), switch instantly and persist |
162
- | Per-turn | Show the cost of each finished turn; when off, don't render that group |
163
- | Auto-hide | Seconds before the turn cost hides (0–600, 0 = always) |
164
- | Peak/off-peak | Show the current pricing period and the next switch |
165
- | Balance correction | See the previous section |
166
-
167
- ---
168
-
169
- ## Privacy and security
170
-
171
- This section only describes what the code actually does. It is risk reduction, not a "zero-risk" promise; every claim below maps to an implementation under `src/`.
172
-
173
- **The interface is reachable from this machine only**
174
-
175
- - The plugin doesn't open its own port; routes register on DSH's existing `webServer`.
176
- - Every request checks that `Host` is `localhost`, `127.0.0.0/8`, or `::1`, otherwise it returns `403`; this blocks DNS rebinding and LAN origins.
177
- - Browser requests also go through `Sec-Fetch-Site`: only `same-origin` / `none`, or a non-browser client that omits the header, is accepted; `cross-site` and `same-site` are rejected explicitly.
178
- - Requests carrying `Origin` must be same-origin with the host; when the host can't provide an expected `Origin`, any request with `Origin` is rejected.
179
-
180
- **Response headers and write requests**
181
-
182
- - Every JSON response carries `cache-control: no-store`, `x-content-type-options: nosniff`, `referrer-policy: no-referrer`, `cross-origin-resource-policy: same-origin`, and `content-security-policy: default-src 'none'; frame-ancestors 'none'`.
183
- - Write requests only accept `content-type: application/json`; the body limit is 1MB, and oversized bodies get `413` with the connection drained; a wrong method gets `405` (with an `allow` header), and the error body is the same JSON shape.
184
- - No CORS allow headers are returned.
185
-
186
- **Conversation data**
187
-
188
- - `GET /messages` is a **summary** interface: it returns message ids, turn, step, a preview of at most 200 characters, usage, and the tool `callId`; no body text. The client has no fixed polling timer: it refetches when the conversation DOM changes, coalesced by a 120 ms scheduler, with a 3-second TTL to avoid duplicate requests.
189
- - The summary supports incremental reads: a request with `since=<last version>` returns only rows with `seq > since` (plus `removedSeqs` for rows deleted since then); without `since` it still returns the full snapshot. Deletion state (`goneTurns` / `goneTools`) is always returned in full, so deleted content stays hidden after an incremental refresh.
190
- - `preview` is only generated for real questions and assistant answers; system / developer messages, runtime-injected context, and tool results are always empty, so their text is never returned by this endpoint (implementation: `messagesSummaryOf` in `src/shared.cjs`).
191
- - Share images and export fetch full text only for the selected turns via `full=1`, and only questions / answers are returned.
192
-
193
- **Cost data**
194
-
195
- - Only one local file: `cost-state.json` under `$DSH_HOME` (default `~/.dsh`), in `plugins/dsh-message-actions/`.
196
- - Writes are a whole-file replacement: temp file in the same directory (`0600`) → `fsync` → `rename`, so a half-written file can't be left behind; the directory is created with `0700`.
197
- - Contents are only balance observations, correction values, and per-day spend; no conversation text.
198
- - Balance correction reports success only after the state file is written: a read-only state file returns `500 READ_ONLY`, a failed write returns `500 WRITE_FAILED` and rolls the in-memory correction back, so the UI shows "save failed" instead of a false success.
199
- - A file over 4MB is rebuilt as the default state and logged with the reason and size; a corrupt file under the limit is renamed to `cost-state.bad-<timestamp>.json` and kept.
200
-
201
- **Network**
202
-
203
- - The plugin itself does not make requests to external addresses; the client only calls same-origin `/dsh-message-actions/*`.
204
- - The balance is read through the host's signed-in DeepSeek account service (`deepseekAccount`); that network request is made by the host. When signed out, it shows "Signed out".
205
- - The plugin stores no keys; to decide whether an API-key path applies, it only asks the host credential service whether one exists, without persisting it or returning it.
206
-
207
- ---
208
-
209
- ## Known limitations
210
-
211
- - Deletion granularity: the question row deletes the whole turn; the tool row deletes that step, hiding the answer and other tool results in the same step. Other steps are unaffected.
212
- - No undo for deletion, and no cross-session memory cleanup.
213
- - Deletion markers live in the session event log. They survive refresh, conversation switching, and a DSH restart; deleting the session directory removes the markers along with the session.
214
- - If the host ever rebuilt the conversation surface without replaying the tombstone, this plugin hides the removed tool rows again from `goneTools`; the hidden rows still exist in the DOM (CSS `display:none`), so very large sessions may keep some invisible nodes.
215
- - The sidebar ticks are drawn by the official virtual list; after deleting content, scrolling past that area may show blank space.
216
- - Entries depend on the host's conversation DOM structure: after a major DSH upgrade the buttons may stop appearing (they disappear silently rather than deleting the wrong thing).
217
- - "Per-turn cost" covers model calls only, not external billing such as search or plugins.
218
- - Web mode is untested: `localhost` on the same machine may work; LAN / remote access is rejected by the security fence.
219
-
220
- ---
221
-
222
- ## Development
223
-
224
- ```bash
225
- node build.mjs # build to lib/
226
- node test/check.mjs # offline assertions, 2609 currently
227
- # plus a real session-log regression: 18/18
228
- ```
229
-
230
- ```
231
- src/
232
- ├── index.mjs # host: Config + 4 HTTP routes + cost service
233
- ├── client.js # client: slots, DOM injection, share / export overlay, cost card
234
- ├── shared.cjs # pure functions shared by host and client (offline testable)
235
- ├── store.cjs # cost state file: atomic read/write, recovery on corrupt files
236
- ├── http.cjs # HTTP helpers: JSON responses, 413 drain, body reading
237
- └── i18n.cjs # single source for Chinese/English copy (builds locale/*.json)
238
- test/check.mjs # dependency-free assertion script
239
- ```
240
-
241
-
242
- - Version: `0.2.1`
243
- - Compatible with: tested on DSH `0.2.1-rc.5` desktop (profile `desktop`)
244
- - Dependencies: none at runtime
245
-
246
- ---
247
-
248
- ## License
249
-
250
- [MIT](LICENSE)