@lynn123411/dsh-chat-translate 3.4.1 → 3.5.3

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.
@@ -1,21 +1,82 @@
1
- export interface ClientSettingsState {
1
+ /**
2
+ * This plugin's configuration page state: what the Host serves, what the reader
3
+ * has staged on the page, and what a save would do.
4
+ *
5
+ * There is no optimistic write and no debounce. Every control stages its edit
6
+ * in a draft; the live configuration changes only when the reader presses
7
+ * **保存**, and the whole batch crosses the wire as one revision-fenced write.
8
+ * That is what keeps a half-typed Base URL from reaching the translation path
9
+ * mid-edit, and it is why the reply renderer reads only the committed values:
10
+ * a draft never changes what is already on screen.
11
+ *
12
+ * The API Key is staged the same way but written through the credentials
13
+ * Remote on the same save, so the key and the rest of the settings commit as
14
+ * one gesture.
15
+ */
16
+ /** Resolved config as the Host serves it, plus the derived readiness flag. */
17
+ export interface CommittedConfig {
2
18
  enabled: boolean;
3
19
  aiTimeoutMs: number;
4
20
  baseUrl: string;
5
21
  model: string;
6
- /** 派生值:Key / Base URL / 模型齐全,正文才会被翻译。 */
22
+ /** Derived: Key / Base URL / model all present, so prose would be translated. */
7
23
  aiConfigured: boolean;
8
24
  }
25
+ /** One staged text control. */
26
+ export interface DraftField {
27
+ /** What the control shows. */
28
+ text: string;
29
+ /** Whether the draft differs from the committed value. */
30
+ dirty: boolean;
31
+ /** Whether the draft is not a value this field accepts (blocks saving). */
32
+ invalid: boolean;
33
+ }
34
+ /** The channel probe run by one save: what the settings page reports. */
35
+ export interface ProbeState {
36
+ running: boolean;
37
+ ok?: boolean;
38
+ message?: string;
39
+ }
40
+ /** Everything the page renders. */
41
+ export interface SettingsPageState {
42
+ /** False while the Host does not serve this plugin's config entry. */
43
+ available: boolean;
44
+ /** Whether the Host document accepts writes. */
45
+ writable: boolean;
46
+ /** The committed configuration the translation path actually uses. */
47
+ committed: CommittedConfig;
48
+ baseUrl: DraftField;
49
+ model: DraftField;
50
+ aiTimeoutMs: DraftField;
51
+ /**
52
+ * Write-only control. Blank until typed; `dirty` records that the reader has
53
+ * typed into it, because a secret is never echoed back — an untouched blank
54
+ * draft means "leave the key alone", while a blank draft the reader produced
55
+ * means "clear it".
56
+ */
57
+ apiKey: {
58
+ text: string;
59
+ dirty: boolean;
60
+ };
61
+ /** Whether the drafts differ from what has been committed. */
62
+ dirty: boolean;
63
+ /** Whether any draft is invalid, which blocks the save. */
64
+ invalid: boolean;
65
+ saving: boolean;
66
+ /** The last save did not land: drafts stay for correction. */
67
+ saveFailed: boolean;
68
+ /** Probe from the last save; undefined until a save ran. */
69
+ probe?: ProbeState;
70
+ /** Localized one-liner for the last save ('' when nothing to say). */
71
+ message?: string;
72
+ }
73
+ /** Profile entry id (== settings namespace) + credentials ref. */
74
+ export declare const SETTINGS_NAMESPACE = "dsh-chat-translate";
75
+ export declare const TRANSLATE_API_KEY_REF = "TRANSLATE_API_KEY";
9
76
  /** 正文单次请求超时的取值范围,与宿主半边保持一致。 */
10
77
  export declare const AI_TIMEOUT_MIN = 500;
11
78
  export declare const AI_TIMEOUT_MAX = 900000;
12
- /** Profile entry id (== settings namespace) + credentials ref, mirroring the host constants. */
13
- export declare const SETTINGS_NAMESPACE = "dsh-chat-translate";
14
- export declare const TRANSLATE_API_KEY_REF = "TRANSLATE_API_KEY";
15
- /**
16
- * One path-addressed edit, matching the wire shape the shared configuration
17
- * form's `mutate` accepts.
18
- */
79
+ /** One path-addressed edit, matching the wire shape the form's `mutate` accepts. */
19
80
  export type SettingsPathOpLike = {
20
81
  op: 'set';
21
82
  path: string[];
@@ -26,11 +87,9 @@ export type SettingsPathOpLike = {
26
87
  };
27
88
  /**
28
89
  * Minimal structural shape of the DSH shared configuration form this store
29
- * rides on — `ConfigForm<T>` from @deepseek-ai/dsh-client-ui-settings, reached
30
- * as `ctx.configForms.get(<profile entry id>)`. Keeping it structural keeps
31
- * this bundle free of the settings package and lets tests inject fakes; the
32
- * real form is a structural superset (it also carries `base`, `user` and
33
- * `mode`).
90
+ * rides on — reached as `ctx.configForms.get(<profile entry id>)`. Keeping it
91
+ * structural keeps this bundle testable with fakes; the real form is a
92
+ * structural superset.
34
93
  */
35
94
  export interface ConfigFormLike {
36
95
  getSnapshot(): {
@@ -69,15 +128,23 @@ export interface CredentialsRemoteLike {
69
128
  unset(ref: string): Promise<RemoteResult<void>>;
70
129
  }
71
130
  /**
72
- * Client settings store backed by this plugin's own configuration form.
131
+ * Parse one timeout draft.
132
+ *
133
+ * @param text - the draft as typed.
134
+ * @returns the committed value, `null` when the draft is empty (which clears the
135
+ * field, so the resolved default takes over), or `undefined` when the draft is
136
+ * not a number this field accepts. An out-of-range value is refused rather than
137
+ * silently pulled into range: a save that quietly stores something other than
138
+ * what the page shows is worse than one that asks the reader to fix it.
139
+ */
140
+ export declare function parseTimeout(text: string): number | null | undefined;
141
+ /**
142
+ * The configuration page's own store: committed reads for the whole client,
143
+ * plus the drafts and the save that this page alone owns.
73
144
  *
74
- * There is no localStorage overlay and no custom config HTTP endpoint: the
75
- * store derives from the bound `ConfigForm`, which mirrors the profile entry's
76
- * config section (schema defaults → composition base → user layer) and folds
77
- * every accepted write back, and each debounced flush commits its touched
78
- * fields as one `mutate` batch — one revision fence, serialized and persisted
79
- * by DSH. Without a bound form (unit tests) it degrades to an in-memory store
80
- * with the same semantics.
145
+ * `update` is the surface for controls outside the page — the composer dock
146
+ * toggle writes the master switch straight through, which is the one place a
147
+ * click takes effect without a save.
81
148
  */
82
149
  declare class SettingsStore {
83
150
  private state;
@@ -86,44 +153,71 @@ declare class SettingsStore {
86
153
  private credentials;
87
154
  private unsubscribeForm;
88
155
  private keyConfigured;
89
- private writeTimer;
90
- private pendingFields;
156
+ private probeFn;
91
157
  /**
92
158
  * Bind the DSH services. Called once from the settings UI setup; re-binding
93
159
  * (e.g. after a reconnect) detaches the previous subscription first.
94
160
  */
95
161
  attach(form: ConfigFormLike | null, credentials: CredentialsRemoteLike | null): void;
96
- getState(): ClientSettingsState;
162
+ /** Inject the probe the settings page's save runs (the translate route). */
163
+ useProbe(fn: (() => Promise<{
164
+ ok: boolean;
165
+ latencyMs: number;
166
+ error?: string;
167
+ }>) | null): void;
168
+ getState(): SettingsPageState;
97
169
  subscribe(listener: () => void): () => void;
98
- /** Map the resolved section into client state and notify. */
99
- private derive;
100
- private applyState;
101
- private notify;
102
- /** Pull the key's status-only view and re-derive aiConfigured. */
103
- private refreshKeyStatus;
170
+ /** Map the Host's resolved section into committed state and notify. */
171
+ private deriveCommitted;
172
+ /** Seed every control from what the Host serves, once, and mark nothing dirty. */
173
+ private seedDrafts;
174
+ /** Rebuild every draft's dirty/invalid flags against the committed values. */
175
+ private deriveDrafts;
176
+ /** Fold dirty/invalid from every draft into the page-level flags. */
177
+ private recomputeGate;
104
178
  /**
105
- * Optimistically apply locally, then persist each touched field through the
106
- * configuration form. Writes are trailing-debounced (300ms) so typing in the
107
- * baseUrl/model inputs collapses into a single queued mutation instead of
108
- * one write per keystroke — which would otherwise flash stale mirror values
109
- * back into the inputs between commits. A failed write makes the form reload
110
- * its mirror, which re-derives this store from the Host document
111
- * (conflict-safe recovery).
179
+ * Stage one text control. The draft is local: nothing leaves this page until
180
+ * a save, so a half-typed Base URL never reaches the translation path.
112
181
  */
113
- update(partial: Partial<ClientSettingsState>): Promise<void>;
114
- private scheduleWrite;
115
- private flushWrite;
182
+ draft(field: 'baseUrl' | 'model' | 'aiTimeoutMs' | 'apiKey', text: string): void;
183
+ /**
184
+ * The master switch, written straight through: the composer dock pill and the
185
+ * page's own switch are two entries into one state, and the pill has no save
186
+ * button to press.
187
+ */
188
+ setEnabled(enabled: boolean): Promise<void>;
189
+ /**
190
+ * Write every staged edit as one revision-fenced batch, then probe the
191
+ * channel and report what the probe found.
192
+ *
193
+ * The probe runs after the save lands, never before it: a probe against
194
+ * values the Host has not accepted would report on a configuration nobody
195
+ * has. A refused write keeps every draft so the reader can correct it.
196
+ *
197
+ * @returns nothing; the outcome is published through {@link getState}.
198
+ */
199
+ save(): Promise<void>;
200
+ /** Pull the key's status-only view and re-derive readiness. */
201
+ refreshKeyStatus(): Promise<void>;
202
+ /** Probe the translation channel; the settings page's test button. */
116
203
  testChannel(channel: string): Promise<{
117
204
  ok: boolean;
118
205
  latencyMs: number;
119
206
  error?: string;
120
207
  }>;
121
- /** Write (or clear) the API key through the credentials Remote API. */
208
+ /**
209
+ * Write (or clear) the API key through the credentials Remote API.
210
+ *
211
+ * @param apiKey - the staged key; a blank value clears the ref, because the
212
+ * save only reaches here for a draft the reader actually produced.
213
+ * @returns whether the credentials surface accepted the write.
214
+ */
122
215
  saveApiKey(apiKey: string): Promise<{
123
216
  ok: boolean;
124
217
  error?: string;
125
218
  }>;
126
219
  dispose(): void;
220
+ private notify;
127
221
  }
128
222
  export declare const settingsStore: SettingsStore;
129
223
  export {};
@@ -4,11 +4,11 @@ import { NS } from '../locales.ts';
4
4
  /**
5
5
  * Chat-translate's own configuration page inside the DSH **Plugins** page.
6
6
  *
7
- * The host's Plugins page owns per-plugin configuration: it draws the icon, the
8
- * title and the crumb, and renders a `plugins.bundle.config` entry ON the
9
- * bundle's own page, under its description and above its rows. The card keeps
10
- * its own chrome — the switches, the credential form, the section order — so the
11
- * move from the Settings page changed where it is reached from, not what it does.
7
+ * Every control stages its edit; **保存** writes the whole batch as one
8
+ * revision-fenced document mutation and then probes the channel, reporting
9
+ * what it found. Nothing here is written while the reader types, so a
10
+ * half-finished Base URL can never reach the translation path.
11
+ *
12
12
  * @param props - renderer-bound seat of the `plugins.bundle.config` slot: the
13
13
  * `view` asked for plus the `t` seat its `locale: NS` registration injects.
14
14
  * @returns The card element.
@@ -72,5 +72,15 @@ export declare class ConfigManager {
72
72
  getConfig(): PluginConfig;
73
73
  /** Whether the AI channel has every required piece: baseUrl, model and key. */
74
74
  isAiConfigured(): boolean;
75
+ /**
76
+ * Name every missing piece of the AI channel, as the reply path reports a
77
+ * setup failure. This travels to the hover bubble and the screen reader, so
78
+ * it names fields rather than values and never echoes the key.
79
+ *
80
+ * @param config - the resolved config to inspect; defaults to the current one.
81
+ * @returns the unset pieces by name, or a generic phrase when all are set
82
+ * (the adapter may still refuse the configuration).
83
+ */
84
+ describeMissing(config?: PluginConfig): string;
75
85
  onConfigChange(listener: (config: PluginConfig) => void): () => void;
76
86
  }
@@ -20,12 +20,12 @@ export declare class TranslationDispatcher {
20
20
  *
21
21
  * **形状问题不否决内容**:修不好的片段自动重掷一次;再修不好就照收模型的
22
22
  * 译文(链接数不等时不回填,其余原样)——原文一键可回,可读的译文优先于
23
- * 形状完整。失败只剩通道伤一种:超时、断流、空返回,块保持原文并带一句
24
- * detail 供悬停报因,救活由用户的点击发起。
23
+ * 形状完整。失败只有两类:通道伤(超时、断流、空返回)与设置没配好;两者
24
+ * 都让块保持原文并带上类别与一句 detail,救活由用户的点击发起。
25
25
  *
26
26
  * 调用方传进来的每个块要么整块译出、要么整块保持原文:任一片段缺失都让该块
27
27
  * 作废,避免半中半英的段落;失败块带 `detail` 一句技术细节。每行只跑首跑
28
- * 一次,通道恢复后由点击救活。
28
+ * 一次(新的写定块由调用方逐次递进来,本方法对它们各自独立决定成败)。
29
29
  *
30
30
  * 客户端按与宿主同源的 4096 估算 token 切批,所以一个 markdown 块可能跨多次
31
31
  * 调用;每次调用都独立决定成败,不会出现「前一段已挂译文、后一段失败」的
@@ -17,13 +17,21 @@ export interface ITranslationAdapter {
17
17
  isAvailable(config: PluginConfig): boolean;
18
18
  translate(text: string, signal: AbortSignal, config: PluginConfig, options?: TranslateAdapterOptions): Promise<string>;
19
19
  }
20
+ /**
21
+ * Why one block failed, as the client's label needs it. `channel` is a hurt
22
+ * channel (timeout, HTTP error, broken stream, empty return) — retrying it
23
+ * may work. `not-configured` is a setup problem (missing key, base URL or
24
+ * model) — retrying the same request can never fix it, so the wording points
25
+ * at the configuration page instead.
26
+ */
27
+ export type TranslateFailureKind = 'channel' | 'not-configured';
20
28
  /**
21
29
  * One reply block's outcome, aligned by index with the request's block list.
22
30
  * Failure means the channel was hurt (timeout, HTTP error, broken stream,
23
- * empty return) — the answer's markdown shape is never a failure reason:
24
- * drift is repaired by construction, re-rolled once, or accepted as written
25
- * (the original stays one click away). `detail` carries the technical
26
- * one-liner for the hover text.
31
+ * empty return) or the channel is not configured — the answer's markdown shape
32
+ * is never a failure reason: drift is repaired by construction, re-rolled
33
+ * once, or accepted as written (the original stays one click away). `detail`
34
+ * carries the technical one-liner for the hover text.
27
35
  */
28
36
  export interface ReplyBlockResult {
29
37
  original: string;
@@ -37,4 +45,15 @@ export interface ReplyBlockResult {
37
45
  * no detail exists.
38
46
  */
39
47
  detail?: string;
48
+ /** Present only when `ok` is false: the failure class the label branches on. */
49
+ reason?: TranslateFailureKind;
50
+ }
51
+ /** A translation failure the reply path can classify for the client. */
52
+ export declare class TranslationFailure extends Error {
53
+ readonly reason: TranslateFailureKind;
54
+ /**
55
+ * @param reason - failure class the client's label branches on.
56
+ * @param message - technical one-liner safe to show in a hover bubble.
57
+ */
58
+ constructor(reason: TranslateFailureKind, message: string);
40
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynn123411/dsh-chat-translate",
3
- "version": "3.4.1",
3
+ "version": "3.5.3",
4
4
  "description": "Assistant reply-body translation for the DeepSeek Harness Web UI (OpenAI-compatible channel)",
5
5
  "license": "MIT",
6
6
  "author": "lynn123411",
@@ -66,7 +66,7 @@
66
66
  "scripts": {
67
67
  "build": "node build.mjs",
68
68
  "typecheck": "tsc -p tsconfig.json --noEmit",
69
- "test": "node scripts/test-client-wiring.mjs && node scripts/test-render.mjs && node scripts/test-chat-translate.mjs && node scripts/test-regression-suite.mjs && node scripts/test-channel-logic.mjs && node scripts/test-store.mjs && node scripts/test-reply-pipeline.mjs",
69
+ "test": "node scripts/test-client-wiring.mjs && node scripts/test-render.mjs && node scripts/test-chat-translate.mjs && node scripts/test-regression-suite.mjs && node scripts/test-channel-logic.mjs && node scripts/test-store.mjs && node scripts/test-reply-pipeline.mjs && node scripts/test-host-parity.mjs",
70
70
  "assert:lib-untracked": "node scripts/assert-lib-untracked.mjs",
71
71
  "check": "pnpm typecheck && pnpm build && pnpm test && pnpm assert:lib-untracked"
72
72
  },
@@ -77,6 +77,7 @@
77
77
  "devDependencies": {
78
78
  "@deepseek-ai/cordis": "^4.0.5-alpha.2",
79
79
  "@deepseek-ai/dsh-client-connection": ">=0.2.1-alpha.3",
80
+ "@deepseek-ai/dsh-client-ui-plugin-manager": ">=0.2.1-alpha.3",
80
81
  "@deepseek-ai/dsh-client-ui-primitives": ">=0.2.1-alpha.3",
81
82
  "@deepseek-ai/dsh-client-ui-renderer": ">=0.2.1-alpha.3",
82
83
  "@deepseek-ai/dsh-client-ui-settings": ">=0.2.1-alpha.3",
@@ -86,12 +87,12 @@
86
87
  "@eslint/js": "^9.30.0",
87
88
  "@types/node": "^22.19.0",
88
89
  "@types/react": "^19.2.18",
90
+ "@types/react-dom": "^19.3.0",
89
91
  "esbuild": "^0.25.0",
90
92
  "jsdom": "^26.1.0",
91
93
  "react": "^18.3.1",
92
94
  "react-dom": "^18.3.1",
93
- "typescript": "^6.0.0",
94
- "@deepseek-ai/dsh-client-ui-plugin-manager": ">=0.2.1-alpha.3"
95
+ "typescript": "^6.0.0"
95
96
  },
96
97
  "peerDependencies": {
97
98
  "@deepseek-ai/dsh-client-ui-primitives": ">=0.2.1-alpha.3",