@lynn123411/dsh-chat-translate 3.8.1 → 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,17 +34,17 @@ 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
 
@@ -174,4 +174,4 @@ cd <my-dsh 绝对路径>/plugins/dsh-chat-translate && pnpm install && pnpm buil
174
174
  cd plugins/dsh-chat-translate && pnpm check
175
175
  ```
176
176
 
177
- `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
 
@@ -28,7 +28,7 @@ DeepSeek Harness Web 界面的**助手回答正文翻译**插件。它接管助
28
28
  - **缓存按提示词世代整体作废**:磁盘池 `~/.dsh/dsh-chat-translate/cache.json`(1000 条 LRU)在文档里登记 `rev`,世代跟着此刻生效的提示词与采样参数走——改了提示词或换了参数,旧产出整池作废(浏览器这一侧的译文池同时丢掉);同一代里命中即复用。
29
29
  - **跟随宿主显示设置**:Think 行的折叠摘要预览跟随「工作细节」模式(读 `ui-chat` 配置的 `transcriptView`,含旧取值映射),设置缺席时按 standard。
30
30
  - **DSH 原生配置接入**:配置就是本插件在 profile 里的插件配置——配置页经 `ctx.configForms` 读写 `dsh-chat-translate` 条目的 `config`,用户层落在当前 profile 的 patch;无任何自研配置文件,译文从不写回会话上下文。
31
- - **改完点保存才生效**:Base URL、模型、超时、提示词与 API Key 都是草稿——打字期间一个字都不写盘,点一次「保存」才作为一批提交上去;提交成功后自动探一次通道,把结果报在页面上(探不通不改保存结果)。空着保存等于清除该项,超时草稿不合法时保存被拦住。唯一即时生效的是总开关。
31
+ - **离开输入框即自动保存**:Base URL、模型、超时、提示词与 API Key 都是草稿——打字期间一个字都不写盘,离开那个输入框时提交上去,没有保存按钮。空着离开等于清除该项,超时草稿不合法时不写盘、页面报出原因。想验通道就点「测试 AI 通道」。总开关拨一下即时生效。
32
32
 
33
33
  ## 安装
34
34
 
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) {