@lynn123411/dsh-chat-translate 3.8.0 → 3.9.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/DETAILS.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ### 1. API Key
8
8
 
9
- 打开「插件」→ 本插件详情页(配置就在描述之下的 AI 翻译卡片里),直接填写 API Key 并保存(经 DSH 凭据服务写入 `~/.dsh/.credentials.yaml` 的 `TRANSLATE_API_KEY`,权限 0600,保存后立即生效;留空保存 = 清除该键)。也可以手动编辑该文件:
9
+ 打开「插件」→ 本插件详情页(配置就在描述之下的 AI 翻译卡片里),直接填写 API Key(离开输入框即自动保存,经 DSH 凭据服务写入 `~/.dsh/.credentials.yaml` 的 `TRANSLATE_API_KEY`,权限 0600,保存后立即生效;留空离开 = 清除该键)。也可以手动编辑该文件:
10
10
 
11
11
  ```yaml
12
12
  refs:
@@ -17,7 +17,7 @@ refs:
17
17
 
18
18
  ### 2. Base URL、模型、超时与提示词
19
19
 
20
- 在本插件的配置页里填写 Base URL(如 `https://api.openai.com/v1` 或 `https://api.deepseek.com/v1`)与模型名(如 `gpt-4o-mini`、`deepseek-chat`),点击「保存」提交。**改动先停在页面上**:打字期间一个字都不写盘,点保存才作为一批提交;提交成功后自动探一次通道,结果报在页面上。想随时单独验一次通道,点「测试 AI 通道」。
20
+ 在本插件的配置页里填写 Base URL(如 `https://api.openai.com/v1` 或 `https://api.deepseek.com/v1`)与模型名(如 `gpt-4o-mini`、`deepseek-chat`),离开输入框即自动保存。**改动先停在页面上**:打字期间一个字都不写盘,离开那个输入框才提交;卡片上没有保存按钮。想验一次通道,点「测试 AI 通道」。
21
21
 
22
22
  所有设置都是本插件在 profile 里的插件配置:写入当前 profile 的 patch(`~/.dsh/profiles/<profile>/cordis.patch.yml`)中 `dsh-chat-translate` 条目的 `config`:
23
23
 
@@ -34,20 +34,22 @@ refs:
34
34
 
35
35
  | 键 | 默认值 | 含义 |
36
36
  | --- | --- | --- |
37
- | `enabled` | `true` | 总开关。与输入框下方那一行的「译」胶囊读写同一个字段,两处状态同步;它是**唯一即时生效**的项(胶囊没有保存按钮可点,拨一下就写)。这一行只在会话形态渲染,新建会话的 Hero 形态没有它。 |
37
+ | `enabled` | `true` | 总开关。与输入框下方那一行的「译」胶囊读写同一个字段,两处状态同步;拨一下就写,不用等离开输入框。这一行只在会话形态渲染,新建会话的 Hero 形态没有它。 |
38
38
  | `aiTimeoutMs` | `600000` | 单次请求超时(毫秒),范围 500–900000。每行首跑与每次手点都等到真实结果,嫌等就调小。 |
39
39
  | `baseUrl` | 空 | OpenAI 兼容服务的接入地址。 |
40
40
  | `model` | 空 | 翻译用的模型名。 |
41
41
  | `prompt` | 空 | 拼在待译正文前面的那段指令。空着 = 用内置默认(`src/prompt.ts` 的 `DEFAULT_PROMPT`),配置页上看到的就是这份;写了就整段以配置页那段为准。多段打包时追加的标记约定由插件自己接在后面,不占这一项。请求里只有一条 user 消息:这一段在前,待译正文在后。 |
42
42
 
43
- 目标语言固定为简体中文,没有配置项。空着保存等于清除该项(`unset`,回落到默认值);超时草稿填了非数字时保存被拦住,草稿留在页面上供改正。提示词与内置默认一字不差时同样存成空值:配置页的输入框始终显示「生效的那份」,读者不会看到一个空框却不知道模型收到什么。
43
+ 目标语言固定为简体中文,没有配置项。空着离开等于清除该项(`unset`,回落到默认值);超时草稿填了非数字时不写盘,页面报出原因、草稿留在页面上供改正。提示词与内置默认一字不差时同样存成空值:配置页的输入框始终显示「生效的那份」,读者不会看到一个空框却不知道模型收到什么。
44
44
 
45
45
  ### 2.1 提示词
46
46
 
47
- 提示词是配置页上单独的一张卡片,平时**只读**:一进页面看得到模型实际收到的那整段指令,改不动;点标题右侧「编辑」才进改写态,焦点直接落进输入框,同排多出「恢复默认」与「完成」。点「完成」收起改写态,没保存的草稿原样留在框里;把框清空再点「完成」时,框里填回生效中的指令——只读态永远显示模型实际收到的那一份,不会停在一个空框上。只读态不摆「恢复默认」,免得看着像能直接改。
47
+ 提示词是配置页上单独的一张卡片,平时**只读**:一进页面看得到模型实际收到的那整段指令,改不动;点标题右侧「编辑」才进改写态,焦点直接落进输入框,同排多出「恢复默认」与「完成」。点「完成」(或直接离开输入框)收起改写态并把改动写盘;把框清空再点「完成」时,框里填回生效中的指令——只读态永远显示模型实际收到的那一份,不会停在一个空框上。只读态不摆「恢复默认」,免得看着像能直接改。
48
48
 
49
49
  内置默认那一段的写法有几条承载行为,改文案时别删掉(`src/prompt.ts` 的注释里逐条写了理由):开头先用两个短例立住行为,示例必须排在规则**前面**且写成「输入/输出」的对话样式(放到后面或写成「原文/译文」,模型会把示例连同规则一起复读进译文);首句说「原样抄写」而不是「翻译」或「整理」(这两类写法让模型回到改写模式,已通顺的中文会被换一套说法);中文说的是「原样抄写,只有句子读不通时才改那几句」,那是它唯一被允许改动中文的入口;Markdown 记号只提醒一句,逐行对齐与链接回填由代码兜底。
50
50
 
51
+ 正文分了段时,`promptFor()` 还会在生效指令末尾追加一句成段保真(「原文分几段的,译文也分几段,一段都不要少」),目的是压住「只写开头那段、后面几段整段不写」的退化。它不进默认那一份、也不进配置页的输入框:追加与否由正文段落数决定,读者改指令不用管这件事。实测对照见上面「提示词改动对照」。
52
+
51
53
  ## 提示词改动对照
52
54
 
53
55
  在 `hy-mt2-1.8b` 上量过一次,同一套样本(`scripts/prompt-samples.mjs` 里五条已通顺中文、两条病句),五轮取平均:
@@ -62,6 +64,18 @@ refs:
62
64
 
63
65
  删掉示例是最伤的一步,因为它决定模型把中文当待译内容还是当抄写对象。改完想重新量一次就手动跑 `node scripts/compare-prompt.mjs`(不联网的那份测试不加载它)。
64
66
 
67
+ 另有一条量的是**成段保真**:多段中文里,后几段会不会整段不写回来。`hy-mt2-1.8b` 上四条多段样本各五轮:
68
+
69
+ | 写法 | 多段中文整段丢失 | 单段中文的平均改动率 |
70
+ | --- | --- | --- |
71
+ | 不加这一句 | 10/20 | 约 2% |
72
+ | 那一句常驻(单段正文也带上) | 8/20 | 约 9% |
73
+ | **现行**:正文分了段才追加 | **5/20** | **约 2%** |
74
+
75
+ 单次请求仍会偶发缺段,所以这一句只负责把概率压下来,真正的保证在宿主那层(正文少了不照收,见下面「请求方式」):端到端跑整条管道(条件追加 + 缺段重掷换形状 + 缺段不照收)时,读者看到缺段是 **0/16**。表里前两行是取舍依据——常驻会把本来只有一段的中文多改一遍(约 2% → 约 9%),所以「什么时候追加」和措辞一样是判据的一部分:它不在 `DEFAULT_PROMPT` 里,由 `src/prompt.ts` 的 `promptFor()` 按正文段落数追加,接在生效指令的末尾(读者自己改过的指令同样在后面接上,保真不归读者维护)。
76
+
77
+ 两层各管一段:提示词让模型一开始就写全,宿主保证即使模型还是写少了,缺段也不会被当成成品收下。
78
+
65
79
  内置默认本身改动时改 `src/prompt.ts` 并顺手升 `src/server/prompt-revision.ts` 的 `PROMPT_REVISION`。读者在配置页改的提示词不用升那个号:缓存世代是 `PROMPT_REVISION` 加生效提示词与采样参数的摘要(`cacheGeneration`),改了措辞就自动换一代,旧译文不再命中。浏览器半边那池按原文内容登记的译文跟着一起丢掉(`chatTranslate.dropTranslations()`,接线在 `src/client/index.ts`),否则本页里翻过的段落会一直用旧措辞的产物、一个请求都不发。已经显示在屏幕上的译文不动——下一块要翻的正文才用新指令。
66
80
 
67
81
  ### 3. 翻译范围与时机
@@ -107,7 +121,7 @@ refs:
107
121
  - **通道伤**:超时、断流、空返回。
108
122
  - **设置没配好**:缺 Key / Base URL / 模型。
109
123
 
110
- 块内任一片段失败就让整块按原文渲染,块内不出现半中半英。译文的结构漂移不是失败理由——它由形状修复消化,修不齐的自动重掷一次、仍不齐则照收,永不因格式留下失败标记。控制台各留一条通道失败警告。失败结果不进缓存;通道失败**不自动重试**——滚动、重挂载都不会重发已落定的块,救活由点击发起:点某一块的 ↻ **只重发这一块**,无限次不设额度,已成功的块原地不动(原文命中内容池的块同样直接复用,不花模型调用)。补跑在途时 ↻ **原地转圈**(同一个按钮元素,只换属性,不重挂载——键盘焦点不掉),且不能重复点;补跑失败时上一次的失败结果一直挂着,↻ 因此不消失、标签也不变。一块失败不连坐:同一行后面还没翻的块照常翻。
124
+ 块内任一片段失败就让整块按原文渲染,块内不出现半中半英。译文的结构漂移不是失败理由——它由形状修复消化,修不齐的自动重掷一次、仍不齐则照收,永不因格式留下失败标记。**正文少了是例外**:模型只写回开头那段、后面几段整段不写时,收下就是把那几段永久丢掉,所以它按失败处理(整块保持原文、悬停说「content came back short」、↻ 等你手点),重掷的第二问还会换成带标记的形状去救。控制台各留一条通道失败警告。失败结果不进缓存;通道失败**不自动重试**——滚动、重挂载都不会重发已落定的块,救活由点击发起:点某一块的 ↻ **只重发这一块**,无限次不设额度,已成功的块原地不动(原文命中内容池的块同样直接复用,不花模型调用)。补跑在途时 ↻ **原地转圈**(同一个按钮元素,只换属性,不重挂载——键盘焦点不掉),且不能重复点;补跑失败时上一次的失败结果一直挂着,↻ 因此不消失、标签也不变。一块失败不连坐:同一行后面还没翻的块照常翻。
111
125
 
112
126
  **排版**:插件不给正文加任何 padding、margin 或 border——可翻译与不可翻译时排版完全相同。宿主正文列左侧恒定有 32px(侧栏嵌入 24px)内边距,因此窄带与 ↻ 落在空白里、不压任何宿主元素。已知的小重叠:两位以上序号的有序列表(`10.`、`100.`)的序号会往左伸出正文左缘,与那条 10px 窄带重叠——点那几像素会切换译文而不是选中文字(刻意接受,优先保证排版原生)。
113
127
 
@@ -117,6 +131,8 @@ refs:
117
131
 
118
132
  每片段过**形状修复**(`repairShape`):按段落块对齐,块内结构行(记号类别、表格竖线数、链接个数、缩进代码缩进)逐一对齐并把行首前缀修回原文同款(标题层级、列表符号、序号、引用深度、列表缩进的漂移就此消失),散文行自由重排(软换行渲染无感、只查整块链接总数不丢),空行布局一律取原文。修不齐的片段自动重掷一次;仍不齐则照收模型的译文(链接数不等时不回填,其余原样)。
119
133
 
134
+ **正文少了不照收**:模型把开头那段当整段交差、后面几段一个字都不写,是弱模型的真实退化;收下它就是把原文那几段永久丢掉,读者还会以为是模型没话说。非空行数变少的那一份按失败处理:重掷一次,而且第二问换成带标记的打包形状——标记把这一段框成「一整段待译内容」,同一段原文、同一个模型就不再只写开头;仍然少就整块保持原文并留下 ↻,重翻由手点发起。只有骨架漂移(记号、竖线、链接数)才走照收那句。
135
+
120
136
  同时最多一个在途请求,超时由 `aiTimeoutMs` 控制。客户端只保留一个按行登记、按**原文内容**查结果的翻译池(行以会话锚点为键,LRU 200 行;同一段原文在别处出现直接复用,不再打通道);持久缓存只有磁盘池 `~/.dsh/dsh-chat-translate/cache.json`(1000 条 LRU),文档形态是 `{ rev, entries }`,其中 `rev` 登记写入它的世代(`src/server/prompt-revision.ts` 的 `cacheGeneration`:`PROMPT_REVISION` 加生效提示词与采样参数的摘要)。世代与此刻生效的设置不符时整池作废重来,改提示词或换采样参数都不会让旧译文串代;读盘时按当前设置的世代比对,同一代下命中即复用。
121
137
 
122
138
  ### 6. 通道行为
@@ -158,4 +174,4 @@ cd <my-dsh 绝对路径>/plugins/dsh-chat-translate && pnpm install && pnpm buil
158
174
  cd plugins/dsh-chat-translate && pnpm check
159
175
  ```
160
176
 
161
- `check` 串起类型检查、构建、九份测试与产物门禁(`lib/` 不被索引跟踪)。测试覆盖:行渲染计划与宿主 `AssistantMarkdown` 的分支等价、翻译呈现单点(成功与失败两种标记、失败类别与就地补跑在途)、翻译池(流式增量送译、内容寻址复用、同代幂等、换代作废、换提示词时整池丢弃、按块补跑、逐批落定、LRU)、配置面策略跟随与**暂存式保存**(草稿不写盘、整批提交、被拒后留草稿、保存后自动探通道、提示词按默认/自写两态存取、清空提示词框后回到只读态会填回生效指令、拨「译」胶囊不冲掉提示词草稿),以及真 react + jsdom 的渲染自检——窄带切换、拖选与点正文不误触、悬停气泡贴在光标右边并跟着走、失败段 ↻ 是唯一可点物、补跑在途原地转圈且不重挂载、两类失败各自的读屏文案,配置卡片上提示词那一格默认只读(点「编辑」才改得动、焦点跟着进框、点「完成」收起且草稿留着)、改动停在草稿、点保存才写盘、恢复默认只填回草稿。服务端半边另有结构重装配、缓存、通道契约与「设置没配好逐块如实回报」的回归;`test-host-parity.mjs` 额外盯住宿主渲染器的漂移(宿主多出一种块、或改了分组与守卫规则时当场失败,找不到 DSH 源码则明确跳过)。
177
+ `check` 串起类型检查、构建、九份测试与产物门禁(`lib/` 不被索引跟踪)。测试覆盖:行渲染计划与宿主 `AssistantMarkdown` 的分支等价、翻译呈现单点(成功与失败两种标记、失败类别与就地补跑在途)、翻译池(流式增量送译、内容寻址复用、同代幂等、换代作废、换提示词时整池丢弃、按块补跑、逐批落定、LRU)、配置面策略跟随与**离开输入框即保存**(打字期间不写盘、离开即整批提交、被拒后留草稿、并发提交共享一批写、提示词按默认/自写两态存取、清空提示词框后回到只读态会填回生效指令、拨「译」胶囊不冲掉提示词草稿),以及真 react + jsdom 的渲染自检——窄带切换、拖选与点正文不误触、悬停气泡贴在光标右边并跟着走、失败段 ↻ 是唯一可点物、补跑在途原地转圈且不重挂载、两类失败各自的读屏文案,配置卡片上提示词那一格默认只读(点「编辑」才改得动、焦点跟着进框、点「完成」收起并写盘)、没有保存按钮、打字期间停在草稿、离开输入框即写盘、恢复默认只填回草稿。服务端半边另有结构重装配、缓存、通道契约与「设置没配好逐块如实回报」的回归;`test-host-parity.mjs` 额外盯住宿主渲染器的漂移(宿主多出一种块、或改了分组与守卫规则时当场失败,找不到 DSH 源码则明确跳过)。
package/README.md CHANGED
@@ -11,7 +11,7 @@ DeepSeek Harness Web 界面的**助手回答正文翻译**插件。它接管助
11
11
  - **失败保持原文、只手点重试**:失败分两类——**通道伤**(超时、断流、空返回)与**设置没配好**(缺 Key / Base URL / 模型)。失败块保持原文,左缘留下一个 ↻ 按钮,悬停报出哪一类失败与原因;点它只重发这一块,已成功的块原地不动。失败不自动重试,一块失败不连坐。
12
12
  - **设置没配好不再无声**:缺 Key / Base URL / 模型时如实报出「设置没配好」,气泡与读屏都指向配置页;总开关关着则是纯粹的「不要翻译」,不挂任何标记。
13
13
  - **开关两个入口**:输入框下方那一行的「译」胶囊与本插件配置页的总开关,读写同一个 `enabled`,状态同步、拨一下即时生效。
14
- - **配置页挂在「插件」页**:注册进 `plugins.bundle.config`(key 为包名),路径是「插件」→ 本插件详情页;卡片提供总开关、API Key、Base URL、模型、单次请求超时、**提示词**、**保存**与「测试 AI 通道」。配置项、安装与校验见 [DETAILS.md](./DETAILS.md)。
14
+ - **配置页挂在「插件」页**:注册进 `plugins.bundle.config`(key 为包名),路径是「插件」→ 本插件详情页;卡片提供总开关、API Key、Base URL、模型、单次请求超时、**提示词**与「测试 AI 通道」,改动在离开输入框时自动保存。配置项、安装与校验见 [DETAILS.md](./DETAILS.md)。
15
15
 
16
16
  ## 特性
17
17
 
@@ -19,6 +19,7 @@ DeepSeek Harness Web 界面的**助手回答正文翻译**插件。它接管助
19
19
  - **除正文外与宿主逐分支等价**:Think 折叠行(含工作细节策略下的摘要预览、Turn-process 折叠隐藏与页内搜索揭示)、连续图片组、工具调用行、未知内容块、中断前缀行尾的「已停止」都交回宿主渲染,chrome 文案复用宿主 `chat` 词典,两侧逐字一致。
20
20
  - **正文排版原生,左缘不画任何东西**:正文没有竖线、没有缩进,文字位置与列表、引用的缩进与宿主默认逐字一致。两个可点物都落在正文之外:成功块是左缘 10px 宽的透明窄带(不占正文一个像素),失败块是一个 ↻ 按钮(`dsh-ct-retry`),失败段不放窄带——一段落只对应一个动作。窄带与 ↻ 都落在宿主正文列左侧的空白里,不压任何宿主元素。
21
21
  - **结构重装配,形状问题只修不拒**:宿主先把块按 markdown 结构切段——代码围栏与纯空白段逐字保留、永不送模型——散文段再按输入上限(4096 估算 token)切片、相邻片段打包成一个请求(输出上限 8192 token)。回来的译文过**形状修复**(`repairShape`):按段落块对齐,块内结构行(记号类别、表格竖线数、链接个数、缩进代码缩进)逐一对齐并把行首前缀修回原文同款,散文行自由重排,空行布局一律取原文,`[文字](URL)` 的 URL 按出现序逐字拼回。修不齐的片段自动重掷一次,仍不齐就照收模型的译文——形状问题不否决内容,失败标记只留给「这一段没译出来」。
22
+ - **整段没写回来不算译好**:模型只把开头那段写回来、后面几段整段不写时,那几段正文会被永久丢掉,所以这一份不照收——重掷一次,而且第二问换成带标记的形状(模型因此不再把开头那段当整段),仍然少就整块保持原文并留下 ↻ 等手点重翻。
22
23
  - **内容寻址的翻译池**:客户端按**原文内容**记已送达的译文,同一段原文在别处出现直接复用,不再打通道;行键加文本清单判同代,换代(文本被整份重写)时旧结果作废,迟到的旧代结果丢弃。
23
24
  - **上游重试按块作废**:唯一让已写定的块作废的是上游模型自己重试(整次尝试清空从零重来);那时按块判断,内容一字未变的块其中文留在原地,真变了的块才重翻。
24
25
  - **单通道:OpenAI 兼容协议**:对接任意 OpenAI 兼容的 `chat/completions` 服务(OpenAI、DeepSeek、通义、Ollama、本地 vLLM 等)。Key / Base URL / 模型三者齐备才翻译,缺任何一项保持原文并如实报「设置没配好」,不做降级兜底。API Key 经 DSH 凭据服务读写 `~/.dsh/.credentials.yaml` 的 `TRANSLATE_API_KEY`。
@@ -27,7 +28,7 @@ DeepSeek Harness Web 界面的**助手回答正文翻译**插件。它接管助
27
28
  - **缓存按提示词世代整体作废**:磁盘池 `~/.dsh/dsh-chat-translate/cache.json`(1000 条 LRU)在文档里登记 `rev`,世代跟着此刻生效的提示词与采样参数走——改了提示词或换了参数,旧产出整池作废(浏览器这一侧的译文池同时丢掉);同一代里命中即复用。
28
29
  - **跟随宿主显示设置**:Think 行的折叠摘要预览跟随「工作细节」模式(读 `ui-chat` 配置的 `transcriptView`,含旧取值映射),设置缺席时按 standard。
29
30
  - **DSH 原生配置接入**:配置就是本插件在 profile 里的插件配置——配置页经 `ctx.configForms` 读写 `dsh-chat-translate` 条目的 `config`,用户层落在当前 profile 的 patch;无任何自研配置文件,译文从不写回会话上下文。
30
- - **改完点保存才生效**:Base URL、模型、超时、提示词与 API Key 都是草稿——打字期间一个字都不写盘,点一次「保存」才作为一批提交上去;提交成功后自动探一次通道,把结果报在页面上(探不通不改保存结果)。空着保存等于清除该项,超时草稿不合法时保存被拦住。唯一即时生效的是总开关。
31
+ - **离开输入框即自动保存**:Base URL、模型、超时、提示词与 API Key 都是草稿——打字期间一个字都不写盘,离开那个输入框时提交上去,没有保存按钮。空着离开等于清除该项,超时草稿不合法时不写盘、页面报出原因。想验通道就点「测试 AI 通道」。总开关拨一下即时生效。
31
32
 
32
33
  ## 安装
33
34
 
package/assets/icon.svg CHANGED
@@ -1,6 +1,6 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
2
- <path d="M2.5 6.5A2.5 2.5 0 0 1 5 4h6.5A2.5 2.5 0 0 1 14 6.5v3.5A2.5 2.5 0 0 1 11.5 12.5H7.5L4.5 15v-2.5A2.5 2.5 0 0 1 2.5 10z"/>
3
- <path d="M10 13A2.5 2.5 0 0 1 12.5 10.5h6.5A2.5 2.5 0 0 1 21.5 13v3.5A2.5 2.5 0 0 1 19 19h-4L12 21.5V19A2.5 2.5 0 0 1 10 16.5z"/>
4
- <path d="M5.5 7.8h5.5"/>
5
- <path d="M5.5 10.1h3.5"/>
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="M2.5 6.5A2.5 2.5 0 0 1 5 4h6.5A2.5 2.5 0 0 1 14 6.5v3.5A2.5 2.5 0 0 1 11.5 12.5H7.5L4.5 15v-2.5A2.5 2.5 0 0 1 2.5 10z" fill="#12B5A5" stroke="#0A8478" stroke-width="1.2"/>
3
+ <path d="M10 13A2.5 2.5 0 0 1 12.5 10.5h6.5A2.5 2.5 0 0 1 21.5 13v3.5A2.5 2.5 0 0 1 19 19h-4L12 21.5V19A2.5 2.5 0 0 1 10 16.5z" fill="#8B5CF6" stroke="#6D3AE0" stroke-width="1.2"/>
4
+ <path d="M5.6 7.6h4M5.6 10h2.6" stroke="#FFFFFF" stroke-width="1.3"/>
5
+ <path d="M13.9 14.8h3.9M13.9 17.1h2.3" stroke="#FFFFFF" stroke-width="1.3"/>
6
6
  </svg>
package/lib/client.js CHANGED
@@ -98,45 +98,6 @@ function describeError(err) {
98
98
  return parts.join(" \u2190 ");
99
99
  }
100
100
 
101
- // src/client/translate/api.ts
102
- async function requestTranslateReply(blocks) {
103
- const fallback = () => blocks.map((block) => ({ translated: block, ok: false }));
104
- if (!Array.isArray(blocks) || blocks.length === 0) return [];
105
- try {
106
- const res = await fetch("/api/dsh-chat-translate/translate-reply", {
107
- method: "POST",
108
- headers: { "Content-Type": "application/json" },
109
- body: JSON.stringify({ blocks })
110
- });
111
- if (res.ok) {
112
- const data = await res.json();
113
- if (data.ok && Array.isArray(data.results) && data.results.length === blocks.length) {
114
- return data.results;
115
- }
116
- if (data.error) {
117
- console.warn(`[dsh-chat-translate] \u6B63\u6587\u7FFB\u8BD1\u63A5\u53E3\u9519\u8BEF: ${data.error}`);
118
- }
119
- } else {
120
- console.warn(`[dsh-chat-translate] \u6B63\u6587\u7FFB\u8BD1\u8BF7\u6C42\u5931\u8D25 (HTTP ${res.status})`);
121
- }
122
- } catch (err) {
123
- console.warn(`[dsh-chat-translate] \u6B63\u6587\u7FFB\u8BD1\u8BF7\u6C42\u5F02\u5E38: ${describeError(err)}`);
124
- }
125
- return fallback();
126
- }
127
- async function testServerChannel(channel) {
128
- try {
129
- const res = await fetch("/api/dsh-chat-translate/test-channel", {
130
- method: "POST",
131
- headers: { "Content-Type": "application/json" },
132
- body: JSON.stringify({ channel })
133
- });
134
- return await res.json();
135
- } catch (err) {
136
- return { ok: false, latencyMs: 0, error: describeError(err) };
137
- }
138
- }
139
-
140
101
  // src/client/settings/store.ts
141
102
  var SETTINGS_NAMESPACE = "dsh-chat-translate";
142
103
  var TRANSLATE_API_KEY_REF = "TRANSLATE_API_KEY";
@@ -184,6 +145,8 @@ var SettingsStore = class {
184
145
  * 输入框下方的「译」胶囊)不该把没保存的草稿冲掉。
185
146
  */
186
147
  seeded = null;
148
+ /** One in-flight commit shared by every control that leaves at once. */
149
+ inflight = null;
187
150
  /**
188
151
  * Bind the DSH services. Called once from the settings UI setup; re-binding
189
152
  * (e.g. after a reconnect) detaches the previous subscription first.
@@ -302,8 +265,9 @@ var SettingsStore = class {
302
265
  this.notify();
303
266
  }
304
267
  /**
305
- * Stage one text control. The draft is local: nothing leaves this page until
306
- * a save, so a half-typed Base URL never reaches the translation path.
268
+ * Stage one text control. The draft is local: nothing leaves this page while
269
+ * the reader types, so a half-typed Base URL never reaches the translation
270
+ * path. The control's blur commits it (see {@link save}).
307
271
  */
308
272
  draft(field, text) {
309
273
  this.state = {
@@ -316,17 +280,17 @@ var SettingsStore = class {
316
280
  /**
317
281
  * Put the built-in instruction back into the draft.
318
282
  *
319
- * Restoring is intentionally a draft edit like typing: nothing is written
320
- * until the reader saves, so the button beside the field behaves exactly like
321
- * the field itself.
283
+ * Restoring is intentionally a draft edit like typing: it becomes committed
284
+ * when the reader leaves the field, so the button beside the field behaves
285
+ * exactly like the field itself.
322
286
  */
323
287
  restoreDefaultPrompt() {
324
288
  this.draft("prompt", DEFAULT_PROMPT);
325
289
  }
326
290
  /**
327
291
  * The master switch, written straight through: the composer dock pill and the
328
- * page's own switch are two entries into one state, and the pill has no save
329
- * button to press.
292
+ * page's own switch are two entries into one state, and the pill has no field
293
+ * to leave.
330
294
  */
331
295
  async setEnabled(enabled) {
332
296
  this.state = { ...this.state, committed: { ...this.state.committed, enabled } };
@@ -343,7 +307,7 @@ var SettingsStore = class {
343
307
  * staged no change — the effective prompt is still the default. Leaving the
344
308
  * box blank in that state would contradict what the card promises (the box
345
309
  * always shows what the model receives), so it is filled in again. Called when
346
- * a save lands and when the reader leaves the editing state.
310
+ * a commit lands and when the reader leaves the editing state.
347
311
  */
348
312
  settlePromptDraft() {
349
313
  if (!isDefaultPrompt(this.state.prompt.text)) return;
@@ -354,16 +318,25 @@ var SettingsStore = class {
354
318
  this.recomputeGate();
355
319
  }
356
320
  /**
357
- * Write every staged edit as one revision-fenced batch, then probe the
358
- * channel and report what the probe found.
321
+ * Write every staged edit as one revision-fenced batch.
322
+ *
323
+ * Called when the reader leaves a control. An invalid draft writes nothing at
324
+ * all, so the page keeps it for correction instead of storing a value it never
325
+ * showed. A refused write keeps every draft for the same reason.
359
326
  *
360
- * The probe runs after the save lands, never before it: a probe against
361
- * values the Host has not accepted would report on a configuration nobody
362
- * has. A refused write keeps every draft so the reader can correct it.
327
+ * Concurrent callers (a field blur racing the prompt's **完成** button) share
328
+ * one in-flight commit instead of starting a second one.
363
329
  *
364
330
  * @returns nothing; the outcome is published through {@link getState}.
365
331
  */
366
- async save() {
332
+ save() {
333
+ if (this.inflight) return this.inflight;
334
+ this.inflight = this.runSave().finally(() => {
335
+ this.inflight = null;
336
+ });
337
+ return this.inflight;
338
+ }
339
+ async runSave() {
367
340
  if (this.state.saving) return;
368
341
  if (this.state.invalid) return;
369
342
  const form = this.form;
@@ -395,7 +368,7 @@ var SettingsStore = class {
395
368
  } else if (prompt !== committedPrompt) {
396
369
  ops.push({ op: "set", path: ["prompt"], value: prompt });
397
370
  }
398
- this.state = { ...this.state, saving: true, saveFailed: false, probe: void 0 };
371
+ this.state = { ...this.state, saving: true, saveFailed: false };
399
372
  this.notify();
400
373
  let landed = true;
401
374
  if (ops.length > 0 && form) {
@@ -413,16 +386,10 @@ var SettingsStore = class {
413
386
  ...this.state,
414
387
  saving: false,
415
388
  saveFailed: false,
416
- apiKey: { text: "", dirty: false },
417
- probe: { running: true }
389
+ apiKey: { text: "", dirty: false }
418
390
  };
419
391
  this.settlePromptDraft();
420
392
  this.notify();
421
- const probe = await testServerChannel("openai");
422
- this.state = {
423
- ...this.state,
424
- probe: { running: false, ok: probe.ok, message: probe.error }
425
- };
426
393
  this.deriveDrafts();
427
394
  }
428
395
  /** Pull the key's status-only view and re-derive readiness. */
@@ -626,14 +593,8 @@ var SETTINGS_CSS = String.raw`
626
593
  color: var(--dsw-alias-state-error-primary);
627
594
  }
628
595
 
629
- /* The save row keeps its two items together at the start of the row instead of
630
- pushing them to the card's edges, so the hint reads as part of the button. */
631
- .dsh-tidy-actions {
632
- justify-content: flex-start;
633
- gap: 10px;
634
- }
635
-
636
- .dsh-tidy-hint {
596
+ /* 自动保存状态:一行安静的小字,只说明此刻在写盘还是已经写好。 */
597
+ .dsh-tidy-autosave {
637
598
  color: var(--dsw-alias-label-tertiary);
638
599
  font: var(--dsw-font-xxxs-11);
639
600
  }
@@ -650,12 +611,10 @@ var zh = {
650
611
  badgeConfigured: "\u5DF2\u914D\u7F6E",
651
612
  badgeUnconfigured: "\u672A\u914D\u7F6E",
652
613
  apiKeyTitle: "API Key",
653
- apiKeyDesc: "\u4FDD\u5B58\u81F3 {home} \u7684 {ref}\uFF0C\u4FDD\u5B58\u540E\u7ACB\u5373\u751F\u6548\uFF1B\u7559\u7A7A\u4FDD\u5B58 = \u6E05\u9664",
614
+ apiKeyDesc: "\u4FDD\u5B58\u81F3 {home} \u7684 {ref}\uFF0C\u79BB\u5F00\u8F93\u5165\u6846\u5373\u4FDD\u5B58\u751F\u6548\uFF1B\u7559\u7A7A\u79BB\u5F00 = \u6E05\u9664",
654
615
  apiKeyPlaceholderConfigured: "\u5DF2\u914D\u7F6E\uFF08\u5982\u9700\u66F4\u6362\u8BF7\u76F4\u63A5\u8F93\u5165\uFF09",
655
- save: "\u4FDD\u5B58",
656
616
  saving: "\u4FDD\u5B58\u4E2D\u2026",
657
- keySaved: "\u5DF2\u4FDD\u5B58\u5230 {home}\uFF0C\u7ACB\u5373\u751F\u6548",
658
- keyCleared: "\u5DF2\u6E05\u9664 API Key\uFF08\u672A\u914D\u7F6E\u65F6\u6B63\u6587\u4FDD\u6301\u539F\u6587\uFF0C\u4E0D\u4F1A\u7FFB\u8BD1\uFF09",
617
+ autoSaveHint: "\u6539\u52A8\u5728\u79BB\u5F00\u8F93\u5165\u6846\u65F6\u81EA\u52A8\u4FDD\u5B58",
659
618
  saveFailed: "\u4FDD\u5B58\u5931\u8D25\uFF1A{error}",
660
619
  unknownError: "\u672A\u77E5\u9519\u8BEF",
661
620
  baseUrlTitle: "Base URL",
@@ -668,12 +627,9 @@ var zh = {
668
627
  testing: "\u6D4B\u8BD5\u4E2D\u2026",
669
628
  testOk: "\u8FDE\u63A5\u6B63\u5E38\uFF0C\u5EF6\u8FDF {latency}ms",
670
629
  testFail: "\u5931\u8D25\uFF1A{error}",
671
- unsavedHint: "\u6539\u52A8\u4FDD\u5B58\u540E\u624D\u751F\u6548",
672
630
  invalidTimeout: "\u8D85\u65F6\u586B\u5199\u4E0D\u5408\u6CD5\uFF1A\u8BF7\u586B 500 \u5230 900000 \u4E4B\u95F4\u7684\u6BEB\u79D2\u6570",
673
- saveProbeOk: "\u8BBE\u7F6E\u5DF2\u4FDD\u5B58\uFF0C\u901A\u9053\u8FDE\u63A5\u6B63\u5E38",
674
- saveProbeFail: "\u8BBE\u7F6E\u5DF2\u4FDD\u5B58\uFF0C\u4F46\u901A\u9053\u63A2\u6D4B\u5931\u8D25\uFF1A{error}",
675
631
  promptTitle: "\u7FFB\u8BD1\u63D0\u793A\u8BCD",
676
- promptDesc: "\u62FC\u5728\u5F85\u8BD1\u6B63\u6587\u524D\u9762\u7684\u90A3\u6BB5\u6307\u4EE4\u3002\u70B9\u300C\u7F16\u8F91\u300D\u53EF\u4EE5\u6539\u5199\uFF0C\u6539\u5B8C\u70B9\u4E0B\u9762\u7684\u300C\u4FDD\u5B58\u300D\u751F\u6548\uFF1B\u7559\u7A7A\u7B49\u4E8E\u7528\u5185\u7F6E\u9ED8\u8BA4\u3002",
632
+ promptDesc: "\u62FC\u5728\u5F85\u8BD1\u6B63\u6587\u524D\u9762\u7684\u90A3\u6BB5\u6307\u4EE4\u3002\u70B9\u300C\u7F16\u8F91\u300D\u53EF\u4EE5\u6539\u5199\uFF0C\u79BB\u5F00\u8F93\u5165\u6846\u6216\u70B9\u300C\u5B8C\u6210\u300D\u540E\u7ACB\u5373\u751F\u6548\uFF1B\u7559\u7A7A\u7B49\u4E8E\u7528\u5185\u7F6E\u9ED8\u8BA4\u3002",
677
633
  promptEdit: "\u7F16\u8F91",
678
634
  promptDone: "\u5B8C\u6210",
679
635
  promptRestore: "\u6062\u590D\u9ED8\u8BA4",
@@ -699,12 +655,10 @@ var en = {
699
655
  badgeConfigured: "Configured",
700
656
  badgeUnconfigured: "Not configured",
701
657
  apiKeyTitle: "API Key",
702
- apiKeyDesc: "Saved as {ref} in {home}; takes effect immediately. Saving an empty value clears it.",
658
+ apiKeyDesc: "Saved as {ref} in {home}; leaving the field saves it and it takes effect immediately. Leaving it empty clears it.",
703
659
  apiKeyPlaceholderConfigured: "Configured (type a new value to replace it)",
704
- save: "Save",
705
660
  saving: "Saving\u2026",
706
- keySaved: "Saved to {home}; takes effect immediately",
707
- keyCleared: "API Key cleared (with nothing configured, replies stay in their original language)",
661
+ autoSaveHint: "Changes are saved when you leave a field",
708
662
  saveFailed: "Save failed: {error}",
709
663
  unknownError: "Unknown error",
710
664
  baseUrlTitle: "Base URL",
@@ -717,12 +671,9 @@ var en = {
717
671
  testing: "Testing\u2026",
718
672
  testOk: "Connected, {latency}ms latency",
719
673
  testFail: "Failed: {error}",
720
- unsavedHint: "Changes take effect after you save",
721
674
  invalidTimeout: "Invalid timeout: enter a number of milliseconds between 500 and 900000",
722
- saveProbeOk: "Settings saved; the channel answered",
723
- saveProbeFail: "Settings saved, but the channel probe failed: {error}",
724
675
  promptTitle: "Translation prompt",
725
- promptDesc: "The instruction placed ahead of the text to translate. Press Edit to rewrite it, then Save to apply; an empty field means the built-in default.",
676
+ promptDesc: "The instruction placed ahead of the text to translate. Press Edit to rewrite it; it applies when you leave the field or press Done. An empty field means the built-in default.",
726
677
  promptEdit: "Edit",
727
678
  promptDone: "Done",
728
679
  promptRestore: "Restore default",
@@ -771,6 +722,45 @@ function setupPluginPage(slots, register) {
771
722
  return slots.inject(BUNDLE_CONFIG_SLOT, register);
772
723
  }
773
724
 
725
+ // src/client/translate/api.ts
726
+ async function requestTranslateReply(blocks) {
727
+ const fallback = () => blocks.map((block) => ({ translated: block, ok: false }));
728
+ if (!Array.isArray(blocks) || blocks.length === 0) return [];
729
+ try {
730
+ const res = await fetch("/api/dsh-chat-translate/translate-reply", {
731
+ method: "POST",
732
+ headers: { "Content-Type": "application/json" },
733
+ body: JSON.stringify({ blocks })
734
+ });
735
+ if (res.ok) {
736
+ const data = await res.json();
737
+ if (data.ok && Array.isArray(data.results) && data.results.length === blocks.length) {
738
+ return data.results;
739
+ }
740
+ if (data.error) {
741
+ console.warn(`[dsh-chat-translate] \u6B63\u6587\u7FFB\u8BD1\u63A5\u53E3\u9519\u8BEF: ${data.error}`);
742
+ }
743
+ } else {
744
+ console.warn(`[dsh-chat-translate] \u6B63\u6587\u7FFB\u8BD1\u8BF7\u6C42\u5931\u8D25 (HTTP ${res.status})`);
745
+ }
746
+ } catch (err) {
747
+ console.warn(`[dsh-chat-translate] \u6B63\u6587\u7FFB\u8BD1\u8BF7\u6C42\u5F02\u5E38: ${describeError(err)}`);
748
+ }
749
+ return fallback();
750
+ }
751
+ async function testServerChannel(channel) {
752
+ try {
753
+ const res = await fetch("/api/dsh-chat-translate/test-channel", {
754
+ method: "POST",
755
+ headers: { "Content-Type": "application/json" },
756
+ body: JSON.stringify({ channel })
757
+ });
758
+ return await res.json();
759
+ } catch (err) {
760
+ return { ok: false, latencyMs: 0, error: describeError(err) };
761
+ }
762
+ }
763
+
774
764
  // src/client/settings/ui.tsx
775
765
  var import_jsx_runtime = require("react/jsx-runtime");
776
766
  var stylesInjected = false;
@@ -820,7 +810,6 @@ function TidySettingsPanel(props) {
820
810
  message: res.ok ? t("testOk").replace("{latency}", String(res.latencyMs)) : res.error || t("unknownError")
821
811
  });
822
812
  };
823
- const saveDisabled = state.saving || state.invalid || !state.dirty || !state.writable && state.apiKey.text.trim() === "";
824
813
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-settings", children: [
825
814
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-card", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-title", children: [
826
815
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: t("masterTitle") }),
@@ -857,6 +846,9 @@ function TidySettingsPanel(props) {
857
846
  placeholder: state.committed.aiConfigured ? t("apiKeyPlaceholderConfigured") : "sk-...",
858
847
  value: state.apiKey.text,
859
848
  onChange: (e) => settingsStore.draft("apiKey", e.target.value),
849
+ onBlur: () => {
850
+ void settingsStore.save();
851
+ },
860
852
  "aria-label": t("apiKeyTitle")
861
853
  }
862
854
  )
@@ -877,6 +869,9 @@ function TidySettingsPanel(props) {
877
869
  placeholder: "https://api.openai.com/v1",
878
870
  value: state.baseUrl.text,
879
871
  onChange: (e) => settingsStore.draft("baseUrl", e.target.value),
872
+ onBlur: () => {
873
+ void settingsStore.save();
874
+ },
880
875
  "aria-label": t("baseUrlTitle")
881
876
  }
882
877
  )
@@ -894,6 +889,9 @@ function TidySettingsPanel(props) {
894
889
  placeholder: "gpt-4o-mini",
895
890
  value: state.model.text,
896
891
  onChange: (e) => settingsStore.draft("model", e.target.value),
892
+ onBlur: () => {
893
+ void settingsStore.save();
894
+ },
897
895
  "aria-label": t("modelTitle")
898
896
  }
899
897
  )
@@ -913,10 +911,14 @@ function TidySettingsPanel(props) {
913
911
  step: 1e3,
914
912
  value: state.aiTimeoutMs.text,
915
913
  onChange: (e) => settingsStore.draft("aiTimeoutMs", e.target.value),
914
+ onBlur: () => {
915
+ void settingsStore.save();
916
+ },
916
917
  "aria-label": t("timeoutTitle")
917
918
  }
918
919
  )
919
920
  ] }),
921
+ state.invalid && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-test-result fail", role: "status", children: t("invalidTimeout") }),
920
922
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row", children: [
921
923
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_dsh_client_ui_primitives.Button, { variant: "outline", size: "sm", disabled: testing?.running, onClick: runTest, children: testing?.running ? t("testing") : t("test") }),
922
924
  testing !== null && !testing.running && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: `dsh-tidy-test-result ${testing.ok ? "ok" : "fail"}`, children: testing.message })
@@ -942,8 +944,10 @@ function TidySettingsPanel(props) {
942
944
  variant: "ghost",
943
945
  size: "sm",
944
946
  onClick: () => {
945
- settingsStore.settlePromptDraft();
946
- setEditingPrompt(false);
947
+ void settingsStore.save().then(() => {
948
+ settingsStore.settlePromptDraft();
949
+ setEditingPrompt(false);
950
+ });
947
951
  },
948
952
  children: t("promptDone")
949
953
  }
@@ -963,31 +967,15 @@ function TidySettingsPanel(props) {
963
967
  onChange: (e) => {
964
968
  if (!editingPrompt) return;
965
969
  settingsStore.draft("prompt", e.target.value);
970
+ },
971
+ onBlur: () => {
972
+ if (!editingPrompt) return;
973
+ void settingsStore.save();
966
974
  }
967
975
  }
968
976
  )
969
977
  ] }),
970
- /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-card", children: [
971
- /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row dsh-tidy-actions", children: [
972
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
973
- import_dsh_client_ui_primitives.Button,
974
- {
975
- variant: "primary",
976
- size: "sm",
977
- className: "dsh-tidy-save",
978
- disabled: saveDisabled,
979
- onClick: () => {
980
- void settingsStore.save();
981
- },
982
- children: state.saving ? t("saving") : t("save")
983
- }
984
- ),
985
- state.dirty && !state.saving && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dsh-tidy-hint", children: t("unsavedHint") })
986
- ] }),
987
- state.invalid && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-test-result fail", role: "status", children: t("invalidTimeout") }),
988
- state.saveFailed && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-test-result fail", role: "status", children: t("saveFailed").replace("{error}", t("unknownError")) }),
989
- state.probe !== void 0 && !state.probe.running && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: `dsh-tidy-test-result ${state.probe.ok ? "ok" : "fail"}`, role: "status", children: state.probe.ok ? t("saveProbeOk") : t("saveProbeFail").replace("{error}", state.probe.message || t("unknownError")) })
990
- ] })
978
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-autosave", role: "status", children: state.saving ? t("saving") : state.saveFailed ? t("saveFailed").replace("{error}", t("unknownError")) : t("autoSaveHint") })
991
979
  ] });
992
980
  }
993
981
  function setupSettingsUi(ctx) {