@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.
- package/README.md +11 -9
- package/docs//351/205/215/347/275/256.md +9 -9
- package/lib/client.js +644 -277
- package/lib/client.js.map +3 -3
- package/lib/index.js +68 -16
- package/lib/index.js.map +3 -3
- package/lib/types/client/chat/assistant-step.d.ts +13 -3
- package/lib/types/client/chat/hint-bubble.d.ts +52 -0
- package/lib/types/client/chat/row-plan.d.ts +53 -6
- package/lib/types/client/chat/styles.d.ts +20 -6
- package/lib/types/client/chat/translate-store.d.ts +87 -19
- package/lib/types/client/locales.d.ts +18 -5
- package/lib/types/client/settings/store.d.ts +136 -42
- package/lib/types/client/settings/ui.d.ts +5 -5
- package/lib/types/server/config.d.ts +10 -0
- package/lib/types/server/dispatcher.d.ts +3 -3
- package/lib/types/server/types.d.ts +23 -4
- package/package.json +5 -4
package/lib/client.js
CHANGED
|
@@ -124,26 +124,45 @@ async function testServerChannel(channel) {
|
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
// src/client/settings/store.ts
|
|
127
|
-
var AI_TIMEOUT_MIN = 500;
|
|
128
|
-
var AI_TIMEOUT_MAX = 9e5;
|
|
129
127
|
var SETTINGS_NAMESPACE = "dsh-chat-translate";
|
|
130
128
|
var TRANSLATE_API_KEY_REF = "TRANSLATE_API_KEY";
|
|
131
|
-
var
|
|
129
|
+
var AI_TIMEOUT_MIN = 500;
|
|
130
|
+
var AI_TIMEOUT_MAX = 9e5;
|
|
131
|
+
var DEFAULT_CONFIG = {
|
|
132
132
|
enabled: true,
|
|
133
133
|
aiTimeoutMs: 6e5,
|
|
134
134
|
baseUrl: "",
|
|
135
135
|
model: "",
|
|
136
136
|
aiConfigured: false
|
|
137
137
|
};
|
|
138
|
+
function parseTimeout(text) {
|
|
139
|
+
const trimmed = text.trim();
|
|
140
|
+
if (trimmed === "") return null;
|
|
141
|
+
const parsed = Number(trimmed);
|
|
142
|
+
if (!Number.isFinite(parsed)) return void 0;
|
|
143
|
+
const rounded = Math.round(parsed);
|
|
144
|
+
return rounded < AI_TIMEOUT_MIN || rounded > AI_TIMEOUT_MAX ? void 0 : rounded;
|
|
145
|
+
}
|
|
138
146
|
var SettingsStore = class {
|
|
139
|
-
state = {
|
|
147
|
+
state = {
|
|
148
|
+
available: false,
|
|
149
|
+
writable: false,
|
|
150
|
+
committed: { ...DEFAULT_CONFIG },
|
|
151
|
+
baseUrl: { text: "", dirty: false, invalid: false },
|
|
152
|
+
model: { text: "", dirty: false, invalid: false },
|
|
153
|
+
aiTimeoutMs: { text: "", dirty: false, invalid: false },
|
|
154
|
+
apiKey: { text: "", dirty: false },
|
|
155
|
+
dirty: false,
|
|
156
|
+
invalid: false,
|
|
157
|
+
saving: false,
|
|
158
|
+
saveFailed: false
|
|
159
|
+
};
|
|
140
160
|
listeners = /* @__PURE__ */ new Set();
|
|
141
161
|
form = null;
|
|
142
162
|
credentials = null;
|
|
143
163
|
unsubscribeForm = null;
|
|
144
164
|
keyConfigured = false;
|
|
145
|
-
|
|
146
|
-
pendingFields = /* @__PURE__ */ new Set();
|
|
165
|
+
probeFn = null;
|
|
147
166
|
/**
|
|
148
167
|
* Bind the DSH services. Called once from the settings UI setup; re-binding
|
|
149
168
|
* (e.g. after a reconnect) detaches the previous subscription first.
|
|
@@ -156,13 +175,17 @@ var SettingsStore = class {
|
|
|
156
175
|
this.form = form;
|
|
157
176
|
this.credentials = credentials;
|
|
158
177
|
if (form) {
|
|
159
|
-
this.unsubscribeForm = form.subscribe(() => this.
|
|
160
|
-
this.
|
|
178
|
+
this.unsubscribeForm = form.subscribe(() => this.deriveCommitted());
|
|
179
|
+
this.deriveCommitted();
|
|
161
180
|
}
|
|
162
181
|
void this.refreshKeyStatus();
|
|
163
182
|
}
|
|
183
|
+
/** Inject the probe the settings page's save runs (the translate route). */
|
|
184
|
+
useProbe(fn) {
|
|
185
|
+
this.probeFn = fn;
|
|
186
|
+
}
|
|
164
187
|
getState() {
|
|
165
|
-
return
|
|
188
|
+
return this.state;
|
|
166
189
|
}
|
|
167
190
|
subscribe(listener) {
|
|
168
191
|
this.listeners.add(listener);
|
|
@@ -170,110 +193,189 @@ var SettingsStore = class {
|
|
|
170
193
|
this.listeners.delete(listener);
|
|
171
194
|
};
|
|
172
195
|
}
|
|
173
|
-
/** Map the resolved section into
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
196
|
+
/** Map the Host's resolved section into committed state and notify. */
|
|
197
|
+
deriveCommitted() {
|
|
198
|
+
const form = this.form;
|
|
199
|
+
if (!form) return;
|
|
200
|
+
const snap = form.getSnapshot();
|
|
177
201
|
const value = snap.value;
|
|
178
202
|
if (!value || typeof value !== "object") return;
|
|
179
|
-
const
|
|
180
|
-
if (typeof value.enabled === "boolean")
|
|
203
|
+
const committed = { ...DEFAULT_CONFIG };
|
|
204
|
+
if (typeof value.enabled === "boolean") committed.enabled = value.enabled;
|
|
181
205
|
const timeout = value.aiTimeoutMs;
|
|
182
206
|
if (typeof timeout === "number" && Number.isFinite(timeout)) {
|
|
183
|
-
|
|
184
|
-
Math.max(Math.round(timeout), AI_TIMEOUT_MIN),
|
|
185
|
-
AI_TIMEOUT_MAX
|
|
186
|
-
);
|
|
207
|
+
committed.aiTimeoutMs = Math.min(Math.max(Math.round(timeout), AI_TIMEOUT_MIN), AI_TIMEOUT_MAX);
|
|
187
208
|
}
|
|
188
|
-
if (typeof value.baseUrl === "string")
|
|
189
|
-
if (typeof value.model === "string")
|
|
190
|
-
this.
|
|
209
|
+
if (typeof value.baseUrl === "string") committed.baseUrl = value.baseUrl;
|
|
210
|
+
if (typeof value.model === "string") committed.model = value.model;
|
|
211
|
+
committed.aiConfigured = Boolean(committed.baseUrl && committed.model && this.keyConfigured);
|
|
212
|
+
this.state = {
|
|
213
|
+
...this.state,
|
|
214
|
+
available: snap.status === "ready",
|
|
215
|
+
writable: snap.writable,
|
|
216
|
+
committed
|
|
217
|
+
};
|
|
218
|
+
this.seedDrafts();
|
|
191
219
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
this.state
|
|
220
|
+
/** Seed every control from what the Host serves, once, and mark nothing dirty. */
|
|
221
|
+
seedDrafts() {
|
|
222
|
+
const committed = this.state.committed;
|
|
223
|
+
this.state = {
|
|
224
|
+
...this.state,
|
|
225
|
+
baseUrl: { text: committed.baseUrl, dirty: false, invalid: false },
|
|
226
|
+
model: { text: committed.model, dirty: false, invalid: false },
|
|
227
|
+
aiTimeoutMs: { text: String(committed.aiTimeoutMs), dirty: false, invalid: false }
|
|
228
|
+
};
|
|
229
|
+
this.recomputeGate();
|
|
230
|
+
}
|
|
231
|
+
/** Rebuild every draft's dirty/invalid flags against the committed values. */
|
|
232
|
+
deriveDrafts() {
|
|
233
|
+
const committed = this.state.committed;
|
|
234
|
+
const stages = this.state;
|
|
235
|
+
const timeout = parseTimeout(stages.aiTimeoutMs.text);
|
|
236
|
+
this.state = {
|
|
237
|
+
...stages,
|
|
238
|
+
baseUrl: {
|
|
239
|
+
text: stages.baseUrl.text,
|
|
240
|
+
dirty: stages.baseUrl.text.trim() !== committed.baseUrl.trim(),
|
|
241
|
+
invalid: false
|
|
242
|
+
},
|
|
243
|
+
model: {
|
|
244
|
+
text: stages.model.text,
|
|
245
|
+
dirty: stages.model.text.trim() !== committed.model.trim(),
|
|
246
|
+
invalid: false
|
|
247
|
+
},
|
|
248
|
+
aiTimeoutMs: {
|
|
249
|
+
text: stages.aiTimeoutMs.text,
|
|
250
|
+
dirty: (timeout === null ? "" : timeout === void 0 ? stages.aiTimeoutMs.text : String(timeout)) !== String(committed.aiTimeoutMs),
|
|
251
|
+
invalid: timeout === void 0
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
this.recomputeGate();
|
|
255
|
+
}
|
|
256
|
+
/** Fold dirty/invalid from every draft into the page-level flags. */
|
|
257
|
+
recomputeGate() {
|
|
258
|
+
const s = this.state;
|
|
259
|
+
const fields = [s.baseUrl, s.model, s.aiTimeoutMs];
|
|
260
|
+
this.state = {
|
|
261
|
+
...s,
|
|
262
|
+
dirty: fields.some((field) => field.dirty) || s.apiKey.dirty,
|
|
263
|
+
invalid: fields.some((field) => field.invalid)
|
|
264
|
+
};
|
|
195
265
|
this.notify();
|
|
196
266
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Stage one text control. The draft is local: nothing leaves this page until
|
|
269
|
+
* a save, so a half-typed Base URL never reaches the translation path.
|
|
270
|
+
*/
|
|
271
|
+
draft(field, text) {
|
|
272
|
+
this.state = {
|
|
273
|
+
...this.state,
|
|
274
|
+
[field]: field === "apiKey" ? { text, dirty: true } : { ...this.state[field], text },
|
|
275
|
+
saveFailed: false
|
|
276
|
+
};
|
|
277
|
+
this.deriveDrafts();
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The master switch, written straight through: the composer dock pill and the
|
|
281
|
+
* page's own switch are two entries into one state, and the pill has no save
|
|
282
|
+
* button to press.
|
|
283
|
+
*/
|
|
284
|
+
async setEnabled(enabled) {
|
|
285
|
+
this.state = { ...this.state, committed: { ...this.state.committed, enabled } };
|
|
286
|
+
this.notify();
|
|
287
|
+
const form = this.form;
|
|
288
|
+
if (!form || !this.state.writable) return;
|
|
289
|
+
await form.mutate([{ op: "set", path: ["enabled"], value: enabled }]).catch(() => {
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Write every staged edit as one revision-fenced batch, then probe the
|
|
294
|
+
* channel and report what the probe found.
|
|
295
|
+
*
|
|
296
|
+
* The probe runs after the save lands, never before it: a probe against
|
|
297
|
+
* values the Host has not accepted would report on a configuration nobody
|
|
298
|
+
* has. A refused write keeps every draft so the reader can correct it.
|
|
299
|
+
*
|
|
300
|
+
* @returns nothing; the outcome is published through {@link getState}.
|
|
301
|
+
*/
|
|
302
|
+
async save() {
|
|
303
|
+
if (this.state.saving) return;
|
|
304
|
+
if (this.state.invalid) return;
|
|
305
|
+
const form = this.form;
|
|
306
|
+
const keyTouched = this.state.apiKey.dirty;
|
|
307
|
+
if (!this.state.dirty) return;
|
|
308
|
+
if (!form && !keyTouched) return;
|
|
309
|
+
if (!this.state.writable && !keyTouched) return;
|
|
310
|
+
const ops = [];
|
|
311
|
+
const baseUrl = this.state.baseUrl.text.trim();
|
|
312
|
+
const model = this.state.model.text.trim();
|
|
313
|
+
const timeout = parseTimeout(this.state.aiTimeoutMs.text);
|
|
314
|
+
if (baseUrl !== this.state.committed.baseUrl.trim()) {
|
|
315
|
+
ops.push(baseUrl === "" ? { op: "unset", path: ["baseUrl"] } : { op: "set", path: ["baseUrl"], value: baseUrl });
|
|
316
|
+
}
|
|
317
|
+
if (model !== this.state.committed.model.trim()) {
|
|
318
|
+
ops.push(model === "" ? { op: "unset", path: ["model"] } : { op: "set", path: ["model"], value: model });
|
|
319
|
+
}
|
|
320
|
+
if (timeout === null) {
|
|
321
|
+
if (this.state.committed.aiTimeoutMs !== DEFAULT_CONFIG.aiTimeoutMs) {
|
|
322
|
+
ops.push({ op: "unset", path: ["aiTimeoutMs"] });
|
|
202
323
|
}
|
|
324
|
+
} else if (typeof timeout === "number" && timeout !== this.state.committed.aiTimeoutMs) {
|
|
325
|
+
ops.push({ op: "set", path: ["aiTimeoutMs"], value: timeout });
|
|
326
|
+
}
|
|
327
|
+
this.state = { ...this.state, saving: true, saveFailed: false, message: void 0, probe: void 0 };
|
|
328
|
+
this.notify();
|
|
329
|
+
let landed = true;
|
|
330
|
+
if (ops.length > 0 && form) {
|
|
331
|
+
landed = await form.mutate(ops, form.getSnapshot().revision).then(() => true).catch(() => false);
|
|
203
332
|
}
|
|
333
|
+
if (landed && keyTouched) {
|
|
334
|
+
landed = (await this.saveApiKey(this.state.apiKey.text)).ok;
|
|
335
|
+
}
|
|
336
|
+
if (!landed) {
|
|
337
|
+
this.state = { ...this.state, saving: false, saveFailed: true };
|
|
338
|
+
this.notify();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this.state = {
|
|
342
|
+
...this.state,
|
|
343
|
+
saving: false,
|
|
344
|
+
saveFailed: false,
|
|
345
|
+
apiKey: { text: "", dirty: false },
|
|
346
|
+
probe: { running: true }
|
|
347
|
+
};
|
|
348
|
+
this.notify();
|
|
349
|
+
const probe = this.probeFn ? await this.probeFn() : null;
|
|
350
|
+
this.state = {
|
|
351
|
+
...this.state,
|
|
352
|
+
probe: probe === null ? void 0 : { running: false, ok: probe.ok, message: probe.error }
|
|
353
|
+
};
|
|
354
|
+
this.deriveDrafts();
|
|
204
355
|
}
|
|
205
|
-
/** Pull the key's status-only view and re-derive
|
|
356
|
+
/** Pull the key's status-only view and re-derive readiness. */
|
|
206
357
|
async refreshKeyStatus() {
|
|
207
358
|
if (!this.credentials) return;
|
|
208
359
|
try {
|
|
209
360
|
const res = await this.credentials.describe([TRANSLATE_API_KEY_REF]);
|
|
210
361
|
if (!res.ok) return;
|
|
211
362
|
const info = res.value[TRANSLATE_API_KEY_REF];
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
this.keyConfigured = configured;
|
|
215
|
-
this.applyState({ ...this.state });
|
|
216
|
-
}
|
|
363
|
+
this.keyConfigured = Boolean(info?.configured);
|
|
364
|
+
this.deriveCommitted();
|
|
217
365
|
} catch {
|
|
218
366
|
}
|
|
219
367
|
}
|
|
220
|
-
/**
|
|
221
|
-
* Optimistically apply locally, then persist each touched field through the
|
|
222
|
-
* configuration form. Writes are trailing-debounced (300ms) so typing in the
|
|
223
|
-
* baseUrl/model inputs collapses into a single queued mutation instead of
|
|
224
|
-
* one write per keystroke — which would otherwise flash stale mirror values
|
|
225
|
-
* back into the inputs between commits. A failed write makes the form reload
|
|
226
|
-
* its mirror, which re-derives this store from the Host document
|
|
227
|
-
* (conflict-safe recovery).
|
|
228
|
-
*/
|
|
229
|
-
async update(partial) {
|
|
230
|
-
let sanitizedTimeout = this.state.aiTimeoutMs;
|
|
231
|
-
if (typeof partial.aiTimeoutMs === "number" && Number.isFinite(partial.aiTimeoutMs)) {
|
|
232
|
-
sanitizedTimeout = Math.min(
|
|
233
|
-
Math.max(Math.round(partial.aiTimeoutMs), AI_TIMEOUT_MIN),
|
|
234
|
-
AI_TIMEOUT_MAX
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
const next = {
|
|
238
|
-
...this.state,
|
|
239
|
-
...partial,
|
|
240
|
-
aiTimeoutMs: sanitizedTimeout
|
|
241
|
-
};
|
|
242
|
-
this.applyState(next);
|
|
243
|
-
if (this.form) {
|
|
244
|
-
const fields = ["enabled", "aiTimeoutMs", "baseUrl", "model"];
|
|
245
|
-
for (const field of fields) {
|
|
246
|
-
if (partial[field] !== void 0) {
|
|
247
|
-
this.pendingFields.add(field);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
this.scheduleWrite();
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
scheduleWrite() {
|
|
254
|
-
if (this.writeTimer !== null || typeof window === "undefined") return;
|
|
255
|
-
this.writeTimer = window.setTimeout(() => {
|
|
256
|
-
this.writeTimer = null;
|
|
257
|
-
void this.flushWrite();
|
|
258
|
-
}, 300);
|
|
259
|
-
}
|
|
260
|
-
async flushWrite() {
|
|
261
|
-
if (!this.form) return;
|
|
262
|
-
const fields = [...this.pendingFields];
|
|
263
|
-
this.pendingFields.clear();
|
|
264
|
-
if (fields.length === 0) return;
|
|
265
|
-
const ops = fields.map((field) => ({
|
|
266
|
-
op: "set",
|
|
267
|
-
path: [field],
|
|
268
|
-
value: this.state[field]
|
|
269
|
-
}));
|
|
270
|
-
await this.form.mutate(ops).catch(() => {
|
|
271
|
-
});
|
|
272
|
-
}
|
|
368
|
+
/** Probe the translation channel; the settings page's test button. */
|
|
273
369
|
async testChannel(channel) {
|
|
274
370
|
return testServerChannel(channel);
|
|
275
371
|
}
|
|
276
|
-
/**
|
|
372
|
+
/**
|
|
373
|
+
* Write (or clear) the API key through the credentials Remote API.
|
|
374
|
+
*
|
|
375
|
+
* @param apiKey - the staged key; a blank value clears the ref, because the
|
|
376
|
+
* save only reaches here for a draft the reader actually produced.
|
|
377
|
+
* @returns whether the credentials surface accepted the write.
|
|
378
|
+
*/
|
|
277
379
|
async saveApiKey(apiKey) {
|
|
278
380
|
if (!this.credentials) {
|
|
279
381
|
return { ok: false, error: "\u51ED\u636E\u670D\u52A1\u4E0D\u53EF\u7528\uFF08\u975E\u56DE\u73AF\u9875\u9762\uFF09" };
|
|
@@ -291,19 +393,23 @@ var SettingsStore = class {
|
|
|
291
393
|
}
|
|
292
394
|
}
|
|
293
395
|
dispose() {
|
|
294
|
-
if (this.writeTimer !== null && typeof window !== "undefined") {
|
|
295
|
-
clearTimeout(this.writeTimer);
|
|
296
|
-
this.writeTimer = null;
|
|
297
|
-
}
|
|
298
|
-
this.pendingFields.clear();
|
|
299
396
|
if (this.unsubscribeForm) {
|
|
300
397
|
this.unsubscribeForm();
|
|
301
398
|
this.unsubscribeForm = null;
|
|
302
399
|
}
|
|
303
400
|
this.form = null;
|
|
304
401
|
this.credentials = null;
|
|
402
|
+
this.probeFn = null;
|
|
305
403
|
this.listeners.clear();
|
|
306
404
|
}
|
|
405
|
+
notify() {
|
|
406
|
+
for (const listener of [...this.listeners]) {
|
|
407
|
+
try {
|
|
408
|
+
listener();
|
|
409
|
+
} catch {
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
307
413
|
};
|
|
308
414
|
var settingsStore = new SettingsStore();
|
|
309
415
|
|
|
@@ -409,6 +515,18 @@ var SETTINGS_CSS = String.raw`
|
|
|
409
515
|
color: var(--dsw-alias-state-error-primary);
|
|
410
516
|
}
|
|
411
517
|
|
|
518
|
+
/* The save row keeps its two items together at the start of the row instead of
|
|
519
|
+
pushing them to the card's edges, so the hint reads as part of the button. */
|
|
520
|
+
.dsh-tidy-actions {
|
|
521
|
+
justify-content: flex-start;
|
|
522
|
+
gap: 10px;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
.dsh-tidy-hint {
|
|
526
|
+
color: var(--dsw-alias-label-tertiary);
|
|
527
|
+
font: var(--dsw-font-xxxs-11);
|
|
528
|
+
}
|
|
529
|
+
|
|
412
530
|
`;
|
|
413
531
|
|
|
414
532
|
// src/client/locales.ts
|
|
@@ -440,16 +558,25 @@ var zh = {
|
|
|
440
558
|
testing: "\u6D4B\u8BD5\u4E2D\u2026",
|
|
441
559
|
testOk: "\u8FDE\u63A5\u6B63\u5E38\uFF0C\u5EF6\u8FDF {latency}ms",
|
|
442
560
|
testFail: "\u5931\u8D25\uFF1A{error}",
|
|
561
|
+
unsavedHint: "\u6539\u52A8\u4FDD\u5B58\u540E\u624D\u751F\u6548",
|
|
562
|
+
invalidTimeout: "\u8D85\u65F6\u586B\u5199\u4E0D\u5408\u6CD5\uFF1A\u8BF7\u586B 500 \u5230 900000 \u4E4B\u95F4\u7684\u6BEB\u79D2\u6570",
|
|
563
|
+
saveProbeOk: "\u8BBE\u7F6E\u5DF2\u4FDD\u5B58\uFF0C\u901A\u9053\u8FDE\u63A5\u6B63\u5E38",
|
|
564
|
+
saveProbeFail: "\u8BBE\u7F6E\u5DF2\u4FDD\u5B58\uFF0C\u4F46\u901A\u9053\u63A2\u6D4B\u5931\u8D25\uFF1A{error}",
|
|
443
565
|
behaviorTitle: "\u7FFB\u8BD1\u8303\u56F4",
|
|
444
566
|
behaviorRule: "\u52A9\u624B\u56DE\u7B54\u6B63\u6587\u4E00\u5F8B\u7FFB\u8BD1\uFF0C\u5305\u62EC\u6700\u7EC8\u6C47\u603B\u4E0E\u6574\u6BB5\u4E2D\u6587\u7684\u5757\uFF1A\u4E2D\u6587\u5757\u540C\u6837\u4EA4\u7ED9\u6A21\u578B\u6539\u5199\u6210\u81EA\u7136\u4E2D\u6587\uFF0C\u610F\u4E49\u3001\u672F\u8BED\u4E0E\u6570\u5B57\u4E0D\u53D8\uFF1B\u7A7A\u767D\u5757\u4E0D\u9001\u8BD1\u3001\u5DE6\u7F18\u4E0D\u51FA\u73B0\u4EFB\u4F55\u6807\u8BB0\u3002\u601D\u8003\u94FE\u6B63\u6587\u3001\u5DE5\u5177\u8C03\u7528\u6807\u9898\u4E0E\u6298\u53E0\u6458\u8981\u4E0D\u7FFB\u8BD1\uFF0C\u4EE3\u7801\u5757\u53CA\u5176\u5185\u90E8\u539F\u6837\u4FDD\u7559\u3002",
|
|
445
567
|
dockLabel: "\u8BD1",
|
|
446
568
|
dockOn: "\u6B63\u6587\u7FFB\u8BD1\u5DF2\u5F00\u542F\uFF0C\u70B9\u51FB\u5173\u95ED",
|
|
447
569
|
dockOff: "\u6B63\u6587\u7FFB\u8BD1\u5DF2\u5173\u95ED\uFF0C\u70B9\u51FB\u5F00\u542F",
|
|
448
570
|
failTitle: "\u7FFB\u8BD1\u5931\u8D25",
|
|
449
|
-
|
|
571
|
+
failTitleUnconfigured: "\u7FFB\u8BD1\u901A\u9053\u8FD8\u6CA1\u914D\u597D",
|
|
572
|
+
retryAria: "\u7FFB\u8BD1\u5931\u8D25\uFF0C\u70B9\u8FD9\u4E2A\u6309\u94AE\u91CD\u65B0\u7FFB\u8BD1\u8FD9\u4E00\u5757",
|
|
450
573
|
retryBusyAria: "\u6B63\u5728\u91CD\u65B0\u7FFB\u8BD1\uFF0C\u8BF7\u7B49\u5F85",
|
|
574
|
+
retryAriaUnconfigured: "\u7FFB\u8BD1\u901A\u9053\u8FD8\u6CA1\u914D\u597D\uFF1A\u5148\u53BB\u300C\u63D2\u4EF6\u300D\u9875\u628A API Key\u3001Base URL \u4E0E\u6A21\u578B\u586B\u9F50\uFF0C\u518D\u56DE\u6765\u70B9\u8FD9\u4E2A\u6309\u94AE\u91CD\u8BD5",
|
|
575
|
+
retryBusyAriaUnconfigured: "\u6B63\u5728\u91CD\u8BD5\uFF0C\u8BF7\u7B49\u5F85",
|
|
451
576
|
toggleToOriginalAria: "\u67E5\u770B\u539F\u6587",
|
|
452
|
-
toggleToTranslatedAria: "\u67E5\u770B\u8BD1\u6587"
|
|
577
|
+
toggleToTranslatedAria: "\u67E5\u770B\u8BD1\u6587",
|
|
578
|
+
hotspotShowOriginal: "\u67E5\u770B\u539F\u6587",
|
|
579
|
+
hotspotShowTranslation: "\u67E5\u770B\u8BD1\u6587"
|
|
453
580
|
};
|
|
454
581
|
var en = {
|
|
455
582
|
masterTitle: "Translation master switch",
|
|
@@ -478,16 +605,25 @@ var en = {
|
|
|
478
605
|
testing: "Testing\u2026",
|
|
479
606
|
testOk: "Connected, {latency}ms latency",
|
|
480
607
|
testFail: "Failed: {error}",
|
|
608
|
+
unsavedHint: "Changes take effect after you save",
|
|
609
|
+
invalidTimeout: "Invalid timeout: enter a number of milliseconds between 500 and 900000",
|
|
610
|
+
saveProbeOk: "Settings saved; the channel answered",
|
|
611
|
+
saveProbeFail: "Settings saved, but the channel probe failed: {error}",
|
|
481
612
|
behaviorTitle: "What gets translated",
|
|
482
613
|
behaviorRule: "All assistant reply prose, the final summary and Chinese-only blocks included: Chinese blocks are also handed to the model, which rewrites them into natural Chinese while keeping meaning, terms and numbers unchanged; blank blocks are never sent and never marked. Think-chain text, tool-call titles and folded summaries are never translated; code blocks and their contents stay untouched.",
|
|
483
614
|
dockLabel: "Translate",
|
|
484
615
|
dockOn: "Translation is on; click to turn it off",
|
|
485
616
|
dockOff: "Translation is off; click to turn it on",
|
|
486
617
|
failTitle: "Translation failed",
|
|
487
|
-
|
|
618
|
+
failTitleUnconfigured: "The translation channel is not configured yet",
|
|
619
|
+
retryAria: "Translation failed \u2014 press this button to translate this block again",
|
|
488
620
|
retryBusyAria: "Translating again, please wait",
|
|
621
|
+
retryAriaUnconfigured: "The translation channel is not configured yet: fill in the API Key, Base URL and model on the Plugins page, then press this button to retry",
|
|
622
|
+
retryBusyAriaUnconfigured: "Retrying, please wait",
|
|
489
623
|
toggleToOriginalAria: "Show the original",
|
|
490
|
-
toggleToTranslatedAria: "Show the translation"
|
|
624
|
+
toggleToTranslatedAria: "Show the translation",
|
|
625
|
+
hotspotShowOriginal: "Show the original",
|
|
626
|
+
hotspotShowTranslation: "Show the translation"
|
|
491
627
|
};
|
|
492
628
|
var rowT = (key) => zh[key] ?? key;
|
|
493
629
|
function bindRowCopy(locale) {
|
|
@@ -498,12 +634,15 @@ function bindRowCopy(locale) {
|
|
|
498
634
|
}
|
|
499
635
|
function rowCopy() {
|
|
500
636
|
const withDetail = (label, detail) => detail === void 0 || detail === "" || detail === null ? label : `${label} \u2014 ${detail}`;
|
|
637
|
+
const unconfigured = (kind) => kind === "not-configured";
|
|
501
638
|
return {
|
|
502
|
-
retryAria: (detail) => withDetail(rowT("retryAria"), detail),
|
|
503
|
-
retryBusyAria: (detail) => withDetail(rowT("retryBusyAria"), detail),
|
|
639
|
+
retryAria: (detail, kind) => withDetail(rowT(unconfigured(kind) ? "retryAriaUnconfigured" : "retryAria"), detail),
|
|
640
|
+
retryBusyAria: (detail, kind) => withDetail(rowT(unconfigured(kind) ? "retryBusyAriaUnconfigured" : "retryBusyAria"), detail),
|
|
504
641
|
toggleToOriginalAria: rowT("toggleToOriginalAria"),
|
|
505
642
|
toggleToTranslatedAria: rowT("toggleToTranslatedAria"),
|
|
506
|
-
|
|
643
|
+
hotspotShowOriginal: rowT("hotspotShowOriginal"),
|
|
644
|
+
hotspotShowTranslation: rowT("hotspotShowTranslation"),
|
|
645
|
+
failTitle: (kind, detail) => withDetail(rowT(unconfigured(kind) ? "failTitleUnconfigured" : "failTitle"), detail)
|
|
507
646
|
};
|
|
508
647
|
}
|
|
509
648
|
|
|
@@ -541,39 +680,20 @@ function TidySettingsPanel(props) {
|
|
|
541
680
|
ensureSettingsStyles();
|
|
542
681
|
const [state, setState] = (0, import_react.useState)(() => settingsStore.getState());
|
|
543
682
|
const [testing, setTesting] = (0, import_react.useState)(null);
|
|
544
|
-
const [apiKeyInput, setApiKeyInput] = (0, import_react.useState)("");
|
|
545
|
-
const [savingKey, setSavingKey] = (0, import_react.useState)(false);
|
|
546
|
-
const [keyMsg, setKeyMsg] = (0, import_react.useState)(null);
|
|
547
683
|
(0, import_react.useEffect)(() => {
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
});
|
|
684
|
+
setState(settingsStore.getState());
|
|
685
|
+
return settingsStore.subscribe(() => setState(settingsStore.getState()));
|
|
551
686
|
}, []);
|
|
552
|
-
const runTest = async (
|
|
553
|
-
setTesting({
|
|
554
|
-
const res = await
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
setKeyMsg(null);
|
|
561
|
-
const res = await settingsStore.saveApiKey(apiKeyInput);
|
|
562
|
-
setSavingKey(false);
|
|
563
|
-
if (res.ok) {
|
|
564
|
-
const cleared = !apiKeyInput.trim();
|
|
565
|
-
setKeyMsg({
|
|
566
|
-
ok: true,
|
|
567
|
-
text: cleared ? t("keyCleared") : t("keySaved").replace("{home}", CREDENTIALS_PATH)
|
|
568
|
-
});
|
|
569
|
-
setApiKeyInput("");
|
|
570
|
-
} else {
|
|
571
|
-
setKeyMsg({
|
|
572
|
-
ok: false,
|
|
573
|
-
text: t("saveFailed").replace("{error}", res.error || t("unknownError"))
|
|
574
|
-
});
|
|
575
|
-
}
|
|
687
|
+
const runTest = async () => {
|
|
688
|
+
setTesting({ running: true });
|
|
689
|
+
const res = await testServerChannel("openai");
|
|
690
|
+
setTesting({
|
|
691
|
+
running: false,
|
|
692
|
+
ok: res.ok,
|
|
693
|
+
message: res.ok ? t("testOk").replace("{latency}", String(res.latencyMs)) : res.error || t("unknownError")
|
|
694
|
+
});
|
|
576
695
|
};
|
|
696
|
+
const saveDisabled = state.saving || state.invalid || !state.dirty || !state.writable && state.apiKey.text.trim() === "";
|
|
577
697
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-settings", children: [
|
|
578
698
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-card", children: [
|
|
579
699
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-title", children: [
|
|
@@ -581,19 +701,21 @@ function TidySettingsPanel(props) {
|
|
|
581
701
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
582
702
|
import_dsh_client_ui_primitives.Switch,
|
|
583
703
|
{
|
|
584
|
-
checked: state.enabled,
|
|
585
|
-
onChange: (next) =>
|
|
704
|
+
checked: state.committed.enabled,
|
|
705
|
+
onChange: (next) => {
|
|
706
|
+
void settingsStore.setEnabled(next);
|
|
707
|
+
},
|
|
586
708
|
label: t("enableTranslation")
|
|
587
709
|
}
|
|
588
710
|
)
|
|
589
711
|
] }),
|
|
590
712
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-desc", children: withSamples(t("masterDesc"), { example: "I traced the failing path to a stale lock file." }) })
|
|
591
713
|
] }),
|
|
592
|
-
state.enabled && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
714
|
+
state.committed.enabled && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
593
715
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-card", children: [
|
|
594
716
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-title", children: [
|
|
595
717
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: t("aiTitle") }),
|
|
596
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_dsh_client_ui_primitives.Tag, { tone: state.aiConfigured ? "success" : "warning", children: t(state.aiConfigured ? "badgeConfigured" : "badgeUnconfigured") })
|
|
718
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_dsh_client_ui_primitives.Tag, { tone: state.committed.aiConfigured ? "success" : "warning", children: t(state.committed.aiConfigured ? "badgeConfigured" : "badgeUnconfigured") })
|
|
597
719
|
] }),
|
|
598
720
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-desc", children: t("aiRule") }),
|
|
599
721
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row", children: [
|
|
@@ -604,31 +726,18 @@ function TidySettingsPanel(props) {
|
|
|
604
726
|
ref: TRANSLATE_API_KEY_REF
|
|
605
727
|
}) })
|
|
606
728
|
] }),
|
|
607
|
-
/* @__PURE__ */ (0, import_jsx_runtime.
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
),
|
|
619
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
620
|
-
import_dsh_client_ui_primitives.Button,
|
|
621
|
-
{
|
|
622
|
-
variant: "primary",
|
|
623
|
-
size: "sm",
|
|
624
|
-
disabled: savingKey,
|
|
625
|
-
onClick: handleSaveKey,
|
|
626
|
-
children: savingKey ? t("saving") : t("save")
|
|
627
|
-
}
|
|
628
|
-
)
|
|
629
|
-
] })
|
|
729
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
730
|
+
import_dsh_client_ui_primitives.Input,
|
|
731
|
+
{
|
|
732
|
+
type: "password",
|
|
733
|
+
className: "dsh-tidy-field-wide",
|
|
734
|
+
placeholder: state.committed.aiConfigured ? t("apiKeyPlaceholderConfigured") : "sk-...",
|
|
735
|
+
value: state.apiKey.text,
|
|
736
|
+
onChange: (e) => settingsStore.draft("apiKey", e.target.value),
|
|
737
|
+
"aria-label": t("apiKeyTitle")
|
|
738
|
+
}
|
|
739
|
+
)
|
|
630
740
|
] }),
|
|
631
|
-
keyMsg && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: `dsh-tidy-test-result ${keyMsg.ok ? "ok" : "fail"}`, children: keyMsg.text }),
|
|
632
741
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row", children: [
|
|
633
742
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row-info", children: [
|
|
634
743
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-row-title", children: t("baseUrlTitle") }),
|
|
@@ -643,8 +752,8 @@ function TidySettingsPanel(props) {
|
|
|
643
752
|
type: "text",
|
|
644
753
|
className: "dsh-tidy-field-wide",
|
|
645
754
|
placeholder: "https://api.openai.com/v1",
|
|
646
|
-
value: state.baseUrl,
|
|
647
|
-
onChange: (e) => settingsStore.
|
|
755
|
+
value: state.baseUrl.text,
|
|
756
|
+
onChange: (e) => settingsStore.draft("baseUrl", e.target.value),
|
|
648
757
|
"aria-label": t("baseUrlTitle")
|
|
649
758
|
}
|
|
650
759
|
)
|
|
@@ -652,10 +761,7 @@ function TidySettingsPanel(props) {
|
|
|
652
761
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row", children: [
|
|
653
762
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row-info", children: [
|
|
654
763
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-row-title", children: t("modelTitle") }),
|
|
655
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-row-desc", children: withSamples(t("modelDesc"), {
|
|
656
|
-
model1: "gpt-4o-mini",
|
|
657
|
-
model2: "deepseek-chat"
|
|
658
|
-
}) })
|
|
764
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-row-desc", children: withSamples(t("modelDesc"), { model1: "gpt-4o-mini", model2: "deepseek-chat" }) })
|
|
659
765
|
] }),
|
|
660
766
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
661
767
|
import_dsh_client_ui_primitives.Input,
|
|
@@ -663,8 +769,8 @@ function TidySettingsPanel(props) {
|
|
|
663
769
|
type: "text",
|
|
664
770
|
className: "dsh-tidy-field-wide",
|
|
665
771
|
placeholder: "gpt-4o-mini",
|
|
666
|
-
value: state.model,
|
|
667
|
-
onChange: (e) => settingsStore.
|
|
772
|
+
value: state.model.text,
|
|
773
|
+
onChange: (e) => settingsStore.draft("model", e.target.value),
|
|
668
774
|
"aria-label": t("modelTitle")
|
|
669
775
|
}
|
|
670
776
|
)
|
|
@@ -682,30 +788,41 @@ function TidySettingsPanel(props) {
|
|
|
682
788
|
min: 500,
|
|
683
789
|
max: 9e5,
|
|
684
790
|
step: 1e3,
|
|
685
|
-
value: state.aiTimeoutMs,
|
|
686
|
-
onChange: (e) =>
|
|
687
|
-
const val = parseInt(e.target.value, 10);
|
|
688
|
-
if (!Number.isFinite(val)) return;
|
|
689
|
-
settingsStore.update({
|
|
690
|
-
aiTimeoutMs: Math.min(Math.max(val, 500), 9e5)
|
|
691
|
-
});
|
|
692
|
-
},
|
|
791
|
+
value: state.aiTimeoutMs.text,
|
|
792
|
+
onChange: (e) => settingsStore.draft("aiTimeoutMs", e.target.value),
|
|
693
793
|
"aria-label": t("timeoutTitle")
|
|
694
794
|
}
|
|
695
795
|
)
|
|
696
796
|
] }),
|
|
697
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row", children: [
|
|
797
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row dsh-tidy-actions", children: [
|
|
698
798
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
699
799
|
import_dsh_client_ui_primitives.Button,
|
|
700
800
|
{
|
|
701
|
-
variant: "
|
|
801
|
+
variant: "primary",
|
|
702
802
|
size: "sm",
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
803
|
+
className: "dsh-tidy-save",
|
|
804
|
+
disabled: saveDisabled,
|
|
805
|
+
onClick: () => {
|
|
806
|
+
void settingsStore.save();
|
|
807
|
+
},
|
|
808
|
+
children: state.saving ? t("saving") : t("save")
|
|
706
809
|
}
|
|
707
810
|
),
|
|
708
|
-
|
|
811
|
+
state.dirty && !state.saving && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dsh-tidy-hint", children: t("unsavedHint") })
|
|
812
|
+
] }),
|
|
813
|
+
state.invalid && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-test-result fail", role: "status", children: t("invalidTimeout") }),
|
|
814
|
+
state.saveFailed && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dsh-tidy-test-result fail", role: "status", children: t("saveFailed").replace("{error}", t("unknownError")) }),
|
|
815
|
+
state.probe !== void 0 && !state.probe.running && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
816
|
+
"div",
|
|
817
|
+
{
|
|
818
|
+
className: `dsh-tidy-test-result ${state.probe.ok ? "ok" : "fail"}`,
|
|
819
|
+
role: "status",
|
|
820
|
+
children: state.probe.ok ? t("saveProbeOk") : t("saveProbeFail").replace("{error}", state.probe.message || t("unknownError"))
|
|
821
|
+
}
|
|
822
|
+
),
|
|
823
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-row", children: [
|
|
824
|
+
/* @__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") }),
|
|
825
|
+
testing !== null && !testing.running && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: `dsh-tidy-test-result ${testing.ok ? "ok" : "fail"}`, children: testing.message })
|
|
709
826
|
] })
|
|
710
827
|
] }),
|
|
711
828
|
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dsh-tidy-card", children: [
|
|
@@ -721,6 +838,7 @@ function setupSettingsUi(ctx) {
|
|
|
721
838
|
const remoteCredentials = ctx.remote?.credentials ?? null;
|
|
722
839
|
try {
|
|
723
840
|
settingsStore.attach(configForms.get(SETTINGS_NAMESPACE), remoteCredentials);
|
|
841
|
+
settingsStore.useProbe(() => testServerChannel("openai"));
|
|
724
842
|
} catch (err) {
|
|
725
843
|
console.warn("[dsh-chat-translate] Failed to bind the configuration form:", err);
|
|
726
844
|
}
|
|
@@ -759,14 +877,23 @@ function ComposerTranslateToggle(props) {
|
|
|
759
877
|
const { t } = props;
|
|
760
878
|
const [state, setState] = (0, import_react2.useState)(() => settingsStore.getState());
|
|
761
879
|
(0, import_react2.useEffect)(() => settingsStore.subscribe(() => setState(settingsStore.getState())), []);
|
|
762
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
763
|
-
import_dsh_client_ui_primitives2.
|
|
880
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
881
|
+
import_dsh_client_ui_primitives2.Tooltip,
|
|
764
882
|
{
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
883
|
+
label: () => state.committed.enabled ? t("dockOn") : t("dockOff"),
|
|
884
|
+
side: "top",
|
|
885
|
+
align: "end",
|
|
886
|
+
portal: true,
|
|
887
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
888
|
+
import_dsh_client_ui_primitives2.Pill,
|
|
889
|
+
{
|
|
890
|
+
active: state.committed.enabled,
|
|
891
|
+
onClick: () => void settingsStore.setEnabled(!settingsStore.getState().committed.enabled),
|
|
892
|
+
children: t("dockLabel")
|
|
893
|
+
}
|
|
894
|
+
)
|
|
768
895
|
}
|
|
769
|
-
)
|
|
896
|
+
);
|
|
770
897
|
}
|
|
771
898
|
function setupComposerToggle(ctx) {
|
|
772
899
|
if (typeof window === "undefined") return () => {
|
|
@@ -792,31 +919,40 @@ function setupComposerToggle(ctx) {
|
|
|
792
919
|
// src/client/chat/assistant-step.tsx
|
|
793
920
|
var React = __toESM(require("react"), 1);
|
|
794
921
|
var import_react3 = require("react");
|
|
922
|
+
var import_react_dom = require("react-dom");
|
|
795
923
|
var import_dsh_client_ui_primitives3 = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
796
924
|
|
|
797
925
|
// src/client/chat/row-plan.ts
|
|
798
926
|
function isBlankText(value) {
|
|
799
927
|
return (value ?? "").trim() === "";
|
|
800
928
|
}
|
|
929
|
+
function proseTexts(blocks, groupPart) {
|
|
930
|
+
const texts = [];
|
|
931
|
+
if (groupPart === "reasoning") return texts;
|
|
932
|
+
for (const block of blocks) {
|
|
933
|
+
if (block.kind === "text" && !isBlankText(block.text)) texts.push(block.text ?? "");
|
|
934
|
+
}
|
|
935
|
+
return texts;
|
|
936
|
+
}
|
|
937
|
+
function growingTailIndex(blocks, groupPart) {
|
|
938
|
+
return Math.max(0, proseTexts(blocks, groupPart).length - 1);
|
|
939
|
+
}
|
|
940
|
+
var NOT_CONFIGURED_DETAIL = "no translation request was sent: the channel is not configured";
|
|
801
941
|
function planAssistantRow(input) {
|
|
802
942
|
const {
|
|
803
943
|
blocks,
|
|
804
944
|
streaming,
|
|
805
945
|
interrupted,
|
|
806
946
|
groupPart,
|
|
807
|
-
|
|
947
|
+
channel,
|
|
808
948
|
outcomes,
|
|
809
|
-
|
|
949
|
+
frozen,
|
|
950
|
+
retrying,
|
|
810
951
|
originalKeys
|
|
811
952
|
} = input;
|
|
812
953
|
const hasVisible = streaming || interrupted || blocks.some((block) => block.kind !== "tool-call");
|
|
813
954
|
if (!hasVisible) return { entries: null, texts: [] };
|
|
814
|
-
const texts =
|
|
815
|
-
if (groupPart !== "reasoning") {
|
|
816
|
-
for (const block of blocks) {
|
|
817
|
-
if (block.kind === "text" && !isBlankText(block.text)) texts.push(block.text ?? "");
|
|
818
|
-
}
|
|
819
|
-
}
|
|
955
|
+
const texts = proseTexts(blocks, groupPart);
|
|
820
956
|
const entries = [];
|
|
821
957
|
let textIndex = 0;
|
|
822
958
|
let imageRun = null;
|
|
@@ -838,17 +974,26 @@ function planAssistantRow(input) {
|
|
|
838
974
|
const index = textIndex;
|
|
839
975
|
if (!blank) textIndex += 1;
|
|
840
976
|
const key = entries.length;
|
|
841
|
-
const
|
|
977
|
+
const settled = index < (frozen ?? Number.POSITIVE_INFINITY);
|
|
978
|
+
const attemptable = !blank && channel === "ready" && settled;
|
|
979
|
+
const unconfigured = !blank && channel === "unconfigured" && settled;
|
|
842
980
|
const outcome = attemptable && outcomes !== void 0 ? outcomes[index] ?? null : null;
|
|
843
981
|
let mark = null;
|
|
844
982
|
let fail = null;
|
|
983
|
+
let failKind = null;
|
|
845
984
|
let retryBusy = false;
|
|
985
|
+
if (unconfigured) {
|
|
986
|
+
mark = "failed";
|
|
987
|
+
fail = NOT_CONFIGURED_DETAIL;
|
|
988
|
+
failKind = "not-configured";
|
|
989
|
+
}
|
|
846
990
|
if (outcome !== null && outcome.ok) {
|
|
847
991
|
mark = "translated";
|
|
848
992
|
} else if (outcome !== null) {
|
|
849
993
|
mark = "failed";
|
|
850
994
|
fail = outcome.detail ?? null;
|
|
851
|
-
|
|
995
|
+
failKind = outcome.reason ?? "channel";
|
|
996
|
+
retryBusy = retrying?.includes(index) ?? false;
|
|
852
997
|
}
|
|
853
998
|
entries.push({
|
|
854
999
|
type: "prose",
|
|
@@ -858,7 +1003,9 @@ function planAssistantRow(input) {
|
|
|
858
1003
|
mark,
|
|
859
1004
|
showingOriginal: outcome !== null && outcome.ok && (originalKeys?.has(key) ?? false),
|
|
860
1005
|
fail,
|
|
861
|
-
|
|
1006
|
+
failKind,
|
|
1007
|
+
retryBusy,
|
|
1008
|
+
frozen: settled
|
|
862
1009
|
});
|
|
863
1010
|
break;
|
|
864
1011
|
}
|
|
@@ -894,6 +1041,29 @@ function isCleanHotspotClick(event, view) {
|
|
|
894
1041
|
return true;
|
|
895
1042
|
}
|
|
896
1043
|
|
|
1044
|
+
// src/client/chat/hint-bubble.ts
|
|
1045
|
+
var HINT_GAP = 12;
|
|
1046
|
+
var EDGE_MARGIN = 12;
|
|
1047
|
+
function hintPlacement(pointer, size, viewport) {
|
|
1048
|
+
const right = pointer.clientX + HINT_GAP;
|
|
1049
|
+
const side = right + size.width <= viewport.width - EDGE_MARGIN ? "right" : "left";
|
|
1050
|
+
const half = size.height / 2;
|
|
1051
|
+
const bottomMost = Math.max(EDGE_MARGIN + half, viewport.height - EDGE_MARGIN - half);
|
|
1052
|
+
const top = Math.min(Math.max(pointer.clientY, EDGE_MARGIN + half), bottomMost);
|
|
1053
|
+
return { left: side === "right" ? right : pointer.clientX - HINT_GAP, top, side };
|
|
1054
|
+
}
|
|
1055
|
+
function placeHintBubble(bubble, pointer, viewport) {
|
|
1056
|
+
const placement = hintPlacement(pointer, { width: bubble.offsetWidth, height: bubble.offsetHeight }, viewport);
|
|
1057
|
+
bubble.style.left = `${placement.left}px`;
|
|
1058
|
+
bubble.style.top = `${placement.top}px`;
|
|
1059
|
+
bubble.dataset.side = placement.side;
|
|
1060
|
+
bubble.dataset.visible = "true";
|
|
1061
|
+
}
|
|
1062
|
+
function hideHintBubble(bubble) {
|
|
1063
|
+
if (bubble === null) return;
|
|
1064
|
+
delete bubble.dataset.visible;
|
|
1065
|
+
}
|
|
1066
|
+
|
|
897
1067
|
// src/client/chat/styles.ts
|
|
898
1068
|
var STYLE_TAG_ID = "@lynn123411/dsh-chat-translate/chat-assistant.css";
|
|
899
1069
|
var ASSISTANT_CSS = [
|
|
@@ -909,19 +1079,33 @@ var ASSISTANT_CSS = [
|
|
|
909
1079
|
// ---- 正文块:排版与宿主默认逐字一致(无 padding、无 border),只做两件事——
|
|
910
1080
|
// 当窄带与 ↻ 的定位父级,以及承载一个纯语义的状态类 ----
|
|
911
1081
|
".dsh-ct-prose{position:relative}",
|
|
912
|
-
// ---- 左缘窄带:从正文左缘往外
|
|
913
|
-
//
|
|
914
|
-
// + `width:
|
|
1082
|
+
// ---- 左缘窄带:从正文左缘往外 10px 的一条透明带子,整条在正文之外、随块高铺满。
|
|
1083
|
+
// 绝对定位的偏移以 padding box 为基准,而正文从 padding box 左缘起排;
|
|
1084
|
+
// `left:-10px` + `width:10px` 于是恰好落在 [-10px,0),一个字都不压,也不占
|
|
1085
|
+
// 排版。带子**不动**:它必须始终停在光标底下,鼠标移进来才有一条带子可停,
|
|
1086
|
+
// 跟鼠标走的是它里面那个定位锚点(见渲染层的 placeHintAtCursor)。
|
|
915
1087
|
// z-index 压过宽表格的透明出血层(那条规则给 table 挂了 z-index:1)。
|
|
916
1088
|
// 宿主正文列左侧恒定有 32px(侧栏嵌入 24px)内边距,这条带子落在空白里。 ----
|
|
917
|
-
".dsh-ct-hotspot{position:absolute;left:-
|
|
1089
|
+
".dsh-ct-hotspot{position:absolute;left:-10px;top:0;bottom:0;width:10px;margin:0;padding:0;border:0;background:0 0;cursor:pointer;z-index:2;-webkit-appearance:none;appearance:none}",
|
|
918
1090
|
// 键盘可聚焦,但不加任何可见标记:没有 outline,也没有别的变化。
|
|
919
1091
|
".dsh-ct-hotspot:focus-visible{outline:none}",
|
|
1092
|
+
// ---- 悬停气泡:贴在光标右边的一条提示(位置由渲染层算,见 hint-bubble.ts)。
|
|
1093
|
+
// 外观取宿主 Tooltip 的同款令牌与尺寸(背景、字色、字号、行高、圆角、
|
|
1094
|
+
// 内边距、最大宽度),所以它看起来就是宿主自己的气泡,只是位置跟着光标。
|
|
1095
|
+
// 常挂不卸:没悬停时 `visibility:hidden` 留在布局里,量得到尺寸,悬停时
|
|
1096
|
+
// 只挂上 `data-visible`(动画随之重跑一次)。投到 body 下(渲染层 portal),
|
|
1097
|
+
// 因此不受正文块的裁剪与叠层影响;它不接指针事件——鼠标始终归那条窄带。 ----
|
|
1098
|
+
".dsh-ct-hint{position:fixed;z-index:1100;width:max-content;max-width:50vw;margin:0;padding:3px 7px;border:0;border-radius:var(--dsw-radius-sm);background:var(--dsw-alias-tooltip-bg);color:var(--dsw-static-neutral-bluish-00);font-size:13px;line-height:20px;white-space:pre-line;overflow-wrap:break-word;pointer-events:none;visibility:hidden;transform:translateY(-50%)}",
|
|
1099
|
+
".dsh-ct-hint[data-side=left]{transform:translate(-100%,-50%)}",
|
|
1100
|
+
".dsh-ct-hint[data-visible]{visibility:visible;animation:150ms var(--ds-ease-in-out) dsh-ct-hint-in}",
|
|
1101
|
+
"@keyframes dsh-ct-hint-in{from{opacity:0}}",
|
|
1102
|
+
"@media (prefers-reduced-motion:reduce){.dsh-ct-hint[data-visible]{animation:none}}",
|
|
920
1103
|
// ---- 失败块的 ↻:真按钮,常驻在正文左侧的空隙里(`left:-18px` + 16px 宽,
|
|
921
1104
|
// 右缘距正文 2px,覆盖不到任何文字)。它是失败段唯一的点击目标——失败
|
|
922
1105
|
// 块不放窄带,一段落只对应一个动作。 ----
|
|
923
1106
|
".dsh-ct-retry{position:absolute;left:-18px;top:2px;width:16px;height:20px;display:flex;align-items:center;justify-content:center;margin:0;padding:0;border:0;background:0 0;color:var(--dsw-alias-state-error-primary);cursor:pointer;z-index:3;-webkit-appearance:none;appearance:none}",
|
|
924
1107
|
// 补跑在途:原位转圈(同一个节点,不重挂载——键盘焦点不掉),并停止承诺重复点。
|
|
1108
|
+
".dsh-ct-retry[data-unconfigured]{color:var(--dsw-alias-label-tertiary)}",
|
|
925
1109
|
".dsh-ct-retry[data-busy]{cursor:default}",
|
|
926
1110
|
".dsh-ct-retry[data-busy] .dsh-ct-retry-spin{animation:0.9s linear infinite dsh-ct-retry-spin}",
|
|
927
1111
|
"@keyframes dsh-ct-retry-spin{to{transform:rotate(360deg)}}",
|
|
@@ -967,8 +1151,10 @@ function proseClassNames(mark) {
|
|
|
967
1151
|
return presentation === null ? void 0 : `${PROSE_BASE_CLASS} ${presentation.className}`;
|
|
968
1152
|
}
|
|
969
1153
|
var HOTSPOT_CLASS = "dsh-ct-hotspot";
|
|
1154
|
+
var HINT_BUBBLE_CLASS = "dsh-ct-hint";
|
|
970
1155
|
var RETRY_CLASS = "dsh-ct-retry";
|
|
971
1156
|
var RETRY_BUSY_ATTR = "data-busy";
|
|
1157
|
+
var RETRY_UNCONFIGURED_ATTR = "data-unconfigured";
|
|
972
1158
|
var RETRY_SPIN_CLASS = "dsh-ct-retry-spin";
|
|
973
1159
|
function ensureAssistantStyles() {
|
|
974
1160
|
if (typeof document === "undefined") return;
|
|
@@ -1012,11 +1198,40 @@ function chunkTexts(texts) {
|
|
|
1012
1198
|
if (current.length > 0) batches.push(current);
|
|
1013
1199
|
return batches;
|
|
1014
1200
|
}
|
|
1201
|
+
function sameTexts(a, b) {
|
|
1202
|
+
if (a === b) return true;
|
|
1203
|
+
if (a.length !== b.length) return false;
|
|
1204
|
+
for (let i = 0; i < a.length; i++) {
|
|
1205
|
+
if (a[i] !== b[i]) return false;
|
|
1206
|
+
}
|
|
1207
|
+
return true;
|
|
1208
|
+
}
|
|
1209
|
+
function isAppendOnly(prev, next) {
|
|
1210
|
+
if (next.length < prev.length) return false;
|
|
1211
|
+
for (let i = 0; i < prev.length; i++) {
|
|
1212
|
+
if (prev[i] !== next[i]) return false;
|
|
1213
|
+
}
|
|
1214
|
+
return true;
|
|
1215
|
+
}
|
|
1015
1216
|
var ChatTranslateStore = class {
|
|
1016
1217
|
rows = /* @__PURE__ */ new Map();
|
|
1017
1218
|
listeners = /* @__PURE__ */ new Set();
|
|
1018
1219
|
version = 0;
|
|
1019
1220
|
fetch;
|
|
1221
|
+
/** 已送达的译文,按原文内容寻址:跨行、跨代复用同一段文本的结果。 */
|
|
1222
|
+
translated = /* @__PURE__ */ new Map();
|
|
1223
|
+
/** 每行写定到哪个下标为止;null(缺席)= 还没有登记过。 */
|
|
1224
|
+
settled = /* @__PURE__ */ new Map();
|
|
1225
|
+
/**
|
|
1226
|
+
* 每行在途的那一轮请求,登记的是**这一轮认领的那份文本数组**。登记换代时旧
|
|
1227
|
+
* 一轮的写入按它丢弃:`live.texts` 与它不再同一个数组就说明行已经换代。
|
|
1228
|
+
*/
|
|
1229
|
+
active = /* @__PURE__ */ new Map();
|
|
1230
|
+
/**
|
|
1231
|
+
* 每行**在飞的块**:下标 → 那一轮请求认领的原文。流式期间新代接手它们的结果位
|
|
1232
|
+
* 而不重发,等结果回来按当时登记的原文落账(文本没变才算同一块)。
|
|
1233
|
+
*/
|
|
1234
|
+
pending = /* @__PURE__ */ new Map();
|
|
1020
1235
|
constructor(fetcher) {
|
|
1021
1236
|
this.fetch = fetcher;
|
|
1022
1237
|
}
|
|
@@ -1038,38 +1253,111 @@ var ChatTranslateStore = class {
|
|
|
1038
1253
|
this.rows.set(rowKey, state);
|
|
1039
1254
|
return state;
|
|
1040
1255
|
}
|
|
1256
|
+
/** 按原文内容查已送达的译文;null = 还没有结果。 */
|
|
1257
|
+
lookup(text) {
|
|
1258
|
+
return this.translated.get(cacheKey(text)) ?? null;
|
|
1259
|
+
}
|
|
1041
1260
|
/**
|
|
1042
1261
|
* 确保该行按当前文本被翻译。
|
|
1043
|
-
* @param
|
|
1262
|
+
* @param rowKey - 行键(会话内锚点)。
|
|
1263
|
+
* @param texts - 该行正文的原文清单,与渲染用的下标对齐。
|
|
1264
|
+
* @param options - `frozen`(写定到第几块)与 `retries`(手点补跑哪些块)。
|
|
1044
1265
|
* @returns 是否发出了新的请求。
|
|
1045
1266
|
*/
|
|
1046
|
-
ensure(rowKey, texts,
|
|
1267
|
+
ensure(rowKey, texts, options = {}) {
|
|
1268
|
+
const next = [...texts];
|
|
1269
|
+
const frozen = clampFrozen(options.frozen ?? next.length, next.length);
|
|
1270
|
+
const retries = normalizeRetries(options.retries);
|
|
1047
1271
|
const current = this.rows.get(rowKey);
|
|
1272
|
+
const settledThrough = this.settled.get(rowKey) ?? 0;
|
|
1048
1273
|
let seed = null;
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1274
|
+
let previousFrozen = 0;
|
|
1275
|
+
const sameGeneration = current !== void 0 && sameTexts(current.texts, next);
|
|
1276
|
+
if (current !== void 0 && (sameGeneration || isAppendOnly(current.texts, next))) {
|
|
1277
|
+
if (sameGeneration && current.status === "pending") return false;
|
|
1278
|
+
if (retries.length === 0 && frozen <= settledThrough && sameGeneration) return false;
|
|
1052
1279
|
seed = current.outcomes.map((outcome) => outcome === null ? null : { ...outcome });
|
|
1280
|
+
previousFrozen = Math.min(current.settledThrough, next.length);
|
|
1053
1281
|
}
|
|
1054
|
-
const
|
|
1055
|
-
const
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1282
|
+
const inflight = this.pending.get(rowKey);
|
|
1283
|
+
const plan = this.planFor(next, frozen, retries, previousFrozen, seed, current?.texts, inflight);
|
|
1284
|
+
if (plan.indexes.length > 0) {
|
|
1285
|
+
const entry = inflight ?? { texts: /* @__PURE__ */ new Map() };
|
|
1286
|
+
for (let i = 0; i < plan.indexes.length; i++) {
|
|
1287
|
+
entry.texts.set(plan.indexes[i], next[plan.indexes[i]] ?? "");
|
|
1288
|
+
}
|
|
1289
|
+
this.pending.set(rowKey, entry);
|
|
1290
|
+
}
|
|
1291
|
+
const work = plan.indexes.length > 0;
|
|
1062
1292
|
this.rows.delete(rowKey);
|
|
1063
|
-
this.rows.set(rowKey,
|
|
1293
|
+
this.rows.set(rowKey, {
|
|
1294
|
+
status: work || inflight !== void 0 && inflight.texts.size > 0 ? "pending" : "settled",
|
|
1295
|
+
texts: next,
|
|
1296
|
+
outcomes: plan.outcomes,
|
|
1297
|
+
settledThrough: frozen,
|
|
1298
|
+
retrying: work ? plan.retrying : []
|
|
1299
|
+
});
|
|
1300
|
+
this.settled.set(rowKey, Math.max(settledThrough, frozen));
|
|
1064
1301
|
this.prune();
|
|
1065
1302
|
this.bump();
|
|
1066
|
-
if (
|
|
1303
|
+
if (!work) return false;
|
|
1304
|
+
this.active.set(rowKey, { texts: next });
|
|
1305
|
+
void this.run(rowKey, next, frozen, plan.indexes, plan.sending);
|
|
1067
1306
|
return true;
|
|
1068
1307
|
}
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1308
|
+
/**
|
|
1309
|
+
* 建本轮的取数计划:哪些下标要送、送什么文本、结果位怎么排。
|
|
1310
|
+
*
|
|
1311
|
+
* 只对**已经写定、而且内容还没命中**的块发请求。自动首跑里先前写定的下标一律
|
|
1312
|
+
* 跳过(它们要么已经成功,要么在等手点补跑);`retries` 只对点名的块解禁一次。
|
|
1313
|
+
*/
|
|
1314
|
+
planFor(texts, frozen, retries, previousFrozen, seed, seedTexts, inflight) {
|
|
1315
|
+
const outcomes = texts.map((_, index) => seed?.[index] ?? null);
|
|
1316
|
+
const retrySet = new Set(retries);
|
|
1317
|
+
const indexes = [];
|
|
1318
|
+
const sending = [];
|
|
1319
|
+
for (let index = 0; index < texts.length; index++) {
|
|
1320
|
+
const text = texts[index] ?? "";
|
|
1321
|
+
if (index >= frozen) {
|
|
1322
|
+
outcomes[index] = null;
|
|
1323
|
+
continue;
|
|
1324
|
+
}
|
|
1325
|
+
const cached = this.translated.get(cacheKey(text));
|
|
1326
|
+
if (cached !== void 0) {
|
|
1327
|
+
outcomes[index] = { translated: cached, ok: true };
|
|
1328
|
+
continue;
|
|
1329
|
+
}
|
|
1330
|
+
if (inflight?.texts.get(index) === text) {
|
|
1331
|
+
continue;
|
|
1332
|
+
}
|
|
1333
|
+
if (retrySet.has(index)) {
|
|
1334
|
+
if (outcomes[index] === null || outcomes[index] === void 0) {
|
|
1335
|
+
outcomes[index] = { translated: text, ok: false };
|
|
1336
|
+
}
|
|
1337
|
+
indexes.push(index);
|
|
1338
|
+
sending.push(text);
|
|
1339
|
+
continue;
|
|
1340
|
+
}
|
|
1341
|
+
const kept = seed?.[index];
|
|
1342
|
+
if (kept !== null && kept !== void 0 && seedTexts?.[index] === text) {
|
|
1343
|
+
outcomes[index] = { ...kept };
|
|
1344
|
+
continue;
|
|
1345
|
+
}
|
|
1346
|
+
if (index < previousFrozen) continue;
|
|
1347
|
+
if (outcomes[index] === null) {
|
|
1348
|
+
indexes.push(index);
|
|
1349
|
+
sending.push(text);
|
|
1350
|
+
outcomes[index] = null;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
return { indexes, sending, outcomes, retrying: indexes };
|
|
1354
|
+
}
|
|
1355
|
+
async run(rowKey, body, frozen, indexes, sending) {
|
|
1356
|
+
const collected = /* @__PURE__ */ new Map();
|
|
1357
|
+
const sent = /* @__PURE__ */ new Map();
|
|
1358
|
+
for (let i = 0; i < indexes.length; i++) sent.set(indexes[i], sending[i] ?? "");
|
|
1359
|
+
let cursor = 0;
|
|
1360
|
+
for (const batch of chunkTexts(sending)) {
|
|
1073
1361
|
let results = [];
|
|
1074
1362
|
try {
|
|
1075
1363
|
results = await this.fetch(batch);
|
|
@@ -1077,45 +1365,69 @@ var ChatTranslateStore = class {
|
|
|
1077
1365
|
results = [];
|
|
1078
1366
|
}
|
|
1079
1367
|
for (let i = 0; i < batch.length; i++) {
|
|
1368
|
+
const index = indexes[cursor + i];
|
|
1369
|
+
if (index === void 0) break;
|
|
1370
|
+
const text = batch[i] ?? "";
|
|
1080
1371
|
const result = results[i];
|
|
1081
1372
|
if (result !== void 0 && result.ok && result.translated && result.translated.trim()) {
|
|
1082
|
-
|
|
1373
|
+
collected.set(index, { translated: result.translated, ok: true });
|
|
1374
|
+
this.translated.set(cacheKey(text), result.translated);
|
|
1083
1375
|
} else {
|
|
1084
1376
|
const detail = typeof result?.detail === "string" && result.detail !== "" ? result.detail : void 0;
|
|
1085
|
-
|
|
1086
|
-
translated:
|
|
1377
|
+
collected.set(index, {
|
|
1378
|
+
translated: body[index] ?? text,
|
|
1087
1379
|
ok: false,
|
|
1088
|
-
...detail === void 0 ? {} : { detail }
|
|
1089
|
-
|
|
1380
|
+
...detail === void 0 ? {} : { detail },
|
|
1381
|
+
...result?.reason === "not-configured" ? { reason: "not-configured" } : {}
|
|
1382
|
+
});
|
|
1090
1383
|
}
|
|
1091
1384
|
}
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
if (live === void 0 || live.status !== "pending" || !sameTexts(live.texts, texts)) return;
|
|
1095
|
-
this.rows.set(rowKey, {
|
|
1096
|
-
status: "pending",
|
|
1097
|
-
texts: live.texts,
|
|
1098
|
-
outcomes: [...outcomes],
|
|
1099
|
-
retrying
|
|
1100
|
-
});
|
|
1101
|
-
this.bump();
|
|
1385
|
+
cursor += batch.length;
|
|
1386
|
+
this.commit(rowKey, frozen, indexes, sent, collected);
|
|
1102
1387
|
}
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1388
|
+
this.commit(rowKey, frozen, indexes, sent, collected, true);
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* 把这一次请求已经拿到的结果写回行。
|
|
1392
|
+
*
|
|
1393
|
+
* 落账的判据是**在飞账本**而不是文本数组的身份:这一块还在飞、而且行的原文与
|
|
1394
|
+
* 那时登记的一字不差,结果才算数。行被换代(旧结果整份作废)、被 LRU 淘汰、
|
|
1395
|
+
* 或者那一块已经被手动补跑重新认领时,结果一律丢弃。
|
|
1396
|
+
*/
|
|
1397
|
+
commit(rowKey, frozen, indexes, sent, collected, finish = false) {
|
|
1398
|
+
const live = this.rows.get(rowKey);
|
|
1399
|
+
const inflight = this.pending.get(rowKey);
|
|
1400
|
+
if (live === void 0 || inflight === void 0) {
|
|
1401
|
+
if (finish) this.pending.delete(rowKey);
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
const outcomes = live.outcomes.map((outcome) => outcome === null ? null : { ...outcome });
|
|
1405
|
+
for (const [index, outcome] of collected) {
|
|
1406
|
+
if (inflight.texts.get(index) !== sent.get(index) || live.texts[index] !== sent.get(index)) continue;
|
|
1407
|
+
outcomes[index] = outcome;
|
|
1112
1408
|
}
|
|
1409
|
+
for (const [index, text] of sent) {
|
|
1410
|
+
if (collected.has(index) && inflight.texts.get(index) === text) inflight.texts.delete(index);
|
|
1411
|
+
}
|
|
1412
|
+
const retrying = finish ? [] : indexes.filter((index) => inflight.texts.has(index));
|
|
1413
|
+
const done = finish && inflight.texts.size === 0;
|
|
1414
|
+
if (done) this.pending.delete(rowKey);
|
|
1415
|
+
if (done) this.active.delete(rowKey);
|
|
1416
|
+
this.rows.set(rowKey, {
|
|
1417
|
+
status: done ? "settled" : "pending",
|
|
1418
|
+
texts: live.texts,
|
|
1419
|
+
outcomes,
|
|
1420
|
+
settledThrough: Math.max(live.settledThrough, frozen),
|
|
1421
|
+
retrying
|
|
1422
|
+
});
|
|
1423
|
+
this.bump();
|
|
1113
1424
|
}
|
|
1114
1425
|
prune() {
|
|
1115
1426
|
while (this.rows.size > MAX_TRANSLATED_ROWS) {
|
|
1116
1427
|
const oldest = this.rows.keys().next().value;
|
|
1117
1428
|
if (oldest === void 0) break;
|
|
1118
1429
|
this.rows.delete(oldest);
|
|
1430
|
+
this.settled.delete(oldest);
|
|
1119
1431
|
}
|
|
1120
1432
|
}
|
|
1121
1433
|
bump() {
|
|
@@ -1128,13 +1440,20 @@ var ChatTranslateStore = class {
|
|
|
1128
1440
|
}
|
|
1129
1441
|
}
|
|
1130
1442
|
};
|
|
1131
|
-
function
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1443
|
+
function cacheKey(text) {
|
|
1444
|
+
return text.trim().toLowerCase();
|
|
1445
|
+
}
|
|
1446
|
+
function clampFrozen(frozen, length) {
|
|
1447
|
+
if (!Number.isFinite(frozen) || frozen < 0) return 0;
|
|
1448
|
+
return Math.min(Math.floor(frozen), length);
|
|
1449
|
+
}
|
|
1450
|
+
function normalizeRetries(retries) {
|
|
1451
|
+
if (retries === void 0 || retries.length === 0) return [];
|
|
1452
|
+
const unique = /* @__PURE__ */ new Set();
|
|
1453
|
+
for (const index of retries) {
|
|
1454
|
+
if (Number.isInteger(index) && index >= 0) unique.add(index);
|
|
1136
1455
|
}
|
|
1137
|
-
return
|
|
1456
|
+
return [...unique].sort((a, b) => a - b);
|
|
1138
1457
|
}
|
|
1139
1458
|
function createTranslateStore(fetcher) {
|
|
1140
1459
|
return new ChatTranslateStore(fetcher);
|
|
@@ -1220,7 +1539,6 @@ function useSearchableHidden(hidden, reveal) {
|
|
|
1220
1539
|
return ref;
|
|
1221
1540
|
}
|
|
1222
1541
|
var subscribeSettings = (listener) => settingsStore.subscribe(listener);
|
|
1223
|
-
var subscribeTranslate = (listener) => chatTranslate.subscribe(listener);
|
|
1224
1542
|
function useSettledInView(ref, armed) {
|
|
1225
1543
|
const noObserver = typeof IntersectionObserver === "undefined";
|
|
1226
1544
|
const [inView, setInView] = (0, import_react3.useState)(noObserver);
|
|
@@ -1311,6 +1629,7 @@ function ProseBlock({
|
|
|
1311
1629
|
mark,
|
|
1312
1630
|
showingOriginal,
|
|
1313
1631
|
fail,
|
|
1632
|
+
failKind,
|
|
1314
1633
|
retryBusy,
|
|
1315
1634
|
onToggle,
|
|
1316
1635
|
onRetry,
|
|
@@ -1323,12 +1642,26 @@ function ProseBlock({
|
|
|
1323
1642
|
const source = mark === "translated" && !showingOriginal ? translated : text;
|
|
1324
1643
|
const copy = rowCopy();
|
|
1325
1644
|
const className = proseClassNames(mark) ?? PROSE_BASE_CLASS;
|
|
1326
|
-
const
|
|
1645
|
+
const hintRef = (0, import_react3.useRef)(null);
|
|
1646
|
+
const showHint = (event) => {
|
|
1647
|
+
const bubble = hintRef.current;
|
|
1648
|
+
if (bubble === null) return;
|
|
1649
|
+
placeHintBubble(bubble, event, { width: window.innerWidth, height: window.innerHeight });
|
|
1650
|
+
};
|
|
1651
|
+
const hideHint = () => {
|
|
1652
|
+
hideHintBubble(hintRef.current);
|
|
1653
|
+
};
|
|
1654
|
+
const hotspotButton = action !== "toggle" ? null : React.createElement("button", {
|
|
1327
1655
|
type: "button",
|
|
1328
1656
|
className: HOTSPOT_CLASS,
|
|
1329
1657
|
// 窄带是覆盖层,读屏只应听到它的动作,正文仍由 MarkdownText 读。
|
|
1330
1658
|
"aria-label": showingOriginal ? copy.toggleToTranslatedAria : copy.toggleToOriginalAria,
|
|
1331
1659
|
tabIndex: 0,
|
|
1660
|
+
// 光标跟踪只挂在带上:鼠标移到正文上不动气泡,气泡也就一直收着——悬停
|
|
1661
|
+
// 提示只在鼠标真的停在这条透明带上时才出现。
|
|
1662
|
+
onMouseEnter: (event) => showHint(event),
|
|
1663
|
+
onMouseMove: (event) => showHint(event),
|
|
1664
|
+
onMouseLeave: hideHint,
|
|
1332
1665
|
// button 自己会按 Enter/Space 合成 click,若不接管就会与这段键盘处理
|
|
1333
1666
|
// 叠加成两次动作:这里两个键都走 keydown 并 preventDefault,合成被掐掉。
|
|
1334
1667
|
onKeyDown: (event) => {
|
|
@@ -1345,15 +1678,25 @@ function ProseBlock({
|
|
|
1345
1678
|
}
|
|
1346
1679
|
}
|
|
1347
1680
|
});
|
|
1681
|
+
const hint = action !== "toggle" ? null : (0, import_react_dom.createPortal)(
|
|
1682
|
+
React.createElement(
|
|
1683
|
+
"span",
|
|
1684
|
+
{ ref: hintRef, className: HINT_BUBBLE_CLASS, "aria-hidden": true },
|
|
1685
|
+
showingOriginal ? copy.hotspotShowTranslation : copy.hotspotShowOriginal
|
|
1686
|
+
),
|
|
1687
|
+
document.body
|
|
1688
|
+
);
|
|
1348
1689
|
const retryButton = React.createElement(
|
|
1349
1690
|
"button",
|
|
1350
1691
|
{
|
|
1351
1692
|
type: "button",
|
|
1352
1693
|
className: RETRY_CLASS,
|
|
1353
1694
|
// 败因进按钮自己的读屏标签:气泡只在 openOnClick 时才挂 aria-describedby,
|
|
1354
|
-
// 读屏用户不悬停,标签是他们唯一能听到「超时 / 断流 /
|
|
1355
|
-
|
|
1695
|
+
// 读屏用户不悬停,标签是他们唯一能听到「超时 / 断流 / 空返回」,或者听到
|
|
1696
|
+
// 「设置还没配好、要去哪儿改」的地方。
|
|
1697
|
+
"aria-label": retryBusy ? copy.retryBusyAria(fail, failKind) : copy.retryAria(fail, failKind),
|
|
1356
1698
|
...retryBusy ? { [RETRY_BUSY_ATTR]: "true" } : {},
|
|
1699
|
+
...failKind === "not-configured" ? { [RETRY_UNCONFIGURED_ATTR]: "true" } : {},
|
|
1357
1700
|
tabIndex: 0,
|
|
1358
1701
|
onKeyDown: (event) => {
|
|
1359
1702
|
if (event.key !== "Enter" && event.key !== " ") return;
|
|
@@ -1371,7 +1714,7 @@ function ProseBlock({
|
|
|
1371
1714
|
)
|
|
1372
1715
|
);
|
|
1373
1716
|
const retry = action !== "retry" ? null : React.createElement(import_dsh_client_ui_primitives3.Tooltip, {
|
|
1374
|
-
label: () => copy.failTitle(fail),
|
|
1717
|
+
label: () => copy.failTitle(failKind, fail),
|
|
1375
1718
|
side: "top",
|
|
1376
1719
|
portal: true,
|
|
1377
1720
|
children: retryButton
|
|
@@ -1380,7 +1723,8 @@ function ProseBlock({
|
|
|
1380
1723
|
"div",
|
|
1381
1724
|
{ className },
|
|
1382
1725
|
retry,
|
|
1383
|
-
|
|
1726
|
+
hotspotButton,
|
|
1727
|
+
hint,
|
|
1384
1728
|
React.createElement(import_dsh_client_ui_primitives3.MarkdownText, {
|
|
1385
1729
|
text: source,
|
|
1386
1730
|
streaming,
|
|
@@ -1392,14 +1736,22 @@ function ProseBlock({
|
|
|
1392
1736
|
}
|
|
1393
1737
|
var AssistantStepView = (0, import_react3.memo)(function AssistantStepView2(props) {
|
|
1394
1738
|
ensureAssistantStyles();
|
|
1395
|
-
const { node, groupPart, useDisclosure, useTurnData, turnProcess, openFile, renderMessageImages, fileMentions, usePresentation, t } = props;
|
|
1739
|
+
const { store, node, groupPart, useDisclosure, useTurnData, turnProcess, openFile, renderMessageImages, fileMentions, usePresentation, t } = props;
|
|
1396
1740
|
const data = node.data;
|
|
1397
1741
|
const streaming = data.status === "running";
|
|
1398
1742
|
const interrupted = data.status === "interrupted";
|
|
1399
|
-
const
|
|
1400
|
-
const
|
|
1401
|
-
|
|
1402
|
-
|
|
1743
|
+
const translate = store ?? chatTranslate;
|
|
1744
|
+
const subscribeStore = (0, import_react3.useCallback)(
|
|
1745
|
+
(listener) => translate.subscribe(listener),
|
|
1746
|
+
[translate]
|
|
1747
|
+
);
|
|
1748
|
+
const enabled = (0, import_react3.useSyncExternalStore)(subscribeSettings, () => settingsStore.getState().committed.enabled);
|
|
1749
|
+
const aiConfigured = (0, import_react3.useSyncExternalStore)(
|
|
1750
|
+
subscribeSettings,
|
|
1751
|
+
() => settingsStore.getState().committed.aiConfigured
|
|
1752
|
+
);
|
|
1753
|
+
(0, import_react3.useSyncExternalStore)(subscribeStore, () => translate.getVersion());
|
|
1754
|
+
const channel = !enabled ? "off" : aiConfigured ? "ready" : "unconfigured";
|
|
1403
1755
|
const rowKey = `a${node.anchorSeq ?? `t${data.turn}:s${data.step}`}`;
|
|
1404
1756
|
const labels = (0, import_react3.useMemo)(() => markdownLabels(t), [t]);
|
|
1405
1757
|
const pathImages = (0, import_react3.useMemo)(
|
|
@@ -1421,19 +1773,24 @@ var AssistantStepView = (0, import_react3.memo)(function AssistantStepView2(prop
|
|
|
1421
1773
|
streaming,
|
|
1422
1774
|
interrupted,
|
|
1423
1775
|
groupPart,
|
|
1424
|
-
|
|
1776
|
+
channel,
|
|
1425
1777
|
outcomes: void 0
|
|
1426
1778
|
}),
|
|
1427
|
-
[blocks, streaming, interrupted, groupPart,
|
|
1779
|
+
[blocks, streaming, interrupted, groupPart, channel]
|
|
1780
|
+
);
|
|
1781
|
+
const frozenCount = (0, import_react3.useMemo)(
|
|
1782
|
+
() => streaming ? growingTailIndex(blocks, groupPart) : plan.texts.length,
|
|
1783
|
+
[blocks, groupPart, plan.texts.length, streaming]
|
|
1428
1784
|
);
|
|
1429
1785
|
const rootRef = (0, import_react3.useRef)(null);
|
|
1430
|
-
const
|
|
1786
|
+
const canTranslate = channel === "ready";
|
|
1787
|
+
const inView = useSettledInView(rootRef, canTranslate && plan.entries !== null && frozenCount > 0);
|
|
1431
1788
|
(0, import_react3.useEffect)(() => {
|
|
1432
|
-
if (
|
|
1433
|
-
|
|
1434
|
-
}, [
|
|
1789
|
+
if (!canTranslate || !inView || plan.entries === null || frozenCount === 0) return;
|
|
1790
|
+
translate.ensure(rowKey, plan.texts, { frozen: frozenCount });
|
|
1791
|
+
}, [canTranslate, inView, rowKey, plan, frozenCount, translate]);
|
|
1435
1792
|
const [originalKeys, setOriginalKeys] = (0, import_react3.useState)(() => /* @__PURE__ */ new Set());
|
|
1436
|
-
const registered =
|
|
1793
|
+
const registered = translate.getState(rowKey);
|
|
1437
1794
|
const rowState = registered !== void 0 && sameTexts(registered.texts, plan.texts) ? registered : void 0;
|
|
1438
1795
|
const displayed = (0, import_react3.useMemo)(
|
|
1439
1796
|
() => planAssistantRow({
|
|
@@ -1441,12 +1798,13 @@ var AssistantStepView = (0, import_react3.memo)(function AssistantStepView2(prop
|
|
|
1441
1798
|
streaming,
|
|
1442
1799
|
interrupted,
|
|
1443
1800
|
groupPart,
|
|
1444
|
-
|
|
1801
|
+
channel,
|
|
1445
1802
|
outcomes: rowState?.outcomes,
|
|
1446
|
-
|
|
1803
|
+
frozen: rowState?.settledThrough ?? frozenCount,
|
|
1804
|
+
retrying: rowState?.retrying,
|
|
1447
1805
|
originalKeys
|
|
1448
1806
|
}),
|
|
1449
|
-
[blocks, streaming, interrupted, groupPart,
|
|
1807
|
+
[blocks, streaming, interrupted, groupPart, channel, rowState, frozenCount, originalKeys]
|
|
1450
1808
|
);
|
|
1451
1809
|
const reasoningHidden = turnProcess !== void 0 && turnProcess.foldable && turnProcess.spec.answerStep === data.step && turnProcess.spec.inlineReasoning && !turnProcess.open;
|
|
1452
1810
|
const revealProcess = (0, import_react3.useCallback)(() => {
|
|
@@ -1460,9 +1818,17 @@ var AssistantStepView = (0, import_react3.memo)(function AssistantStepView2(prop
|
|
|
1460
1818
|
return next;
|
|
1461
1819
|
});
|
|
1462
1820
|
}, []);
|
|
1463
|
-
const
|
|
1464
|
-
|
|
1465
|
-
|
|
1821
|
+
const retryBlock = (0, import_react3.useCallback)(() => {
|
|
1822
|
+
const failed = [];
|
|
1823
|
+
const outcomes = rowState?.outcomes;
|
|
1824
|
+
if (outcomes === void 0) return;
|
|
1825
|
+
for (let index = 0; index < outcomes.length; index++) {
|
|
1826
|
+
const outcome = outcomes[index];
|
|
1827
|
+
if (outcome !== null && outcome !== void 0 && !outcome.ok) failed.push(index);
|
|
1828
|
+
}
|
|
1829
|
+
if (failed.length === 0) return;
|
|
1830
|
+
translate.ensure(rowKey, plan.texts, { frozen: frozenCount, retries: failed });
|
|
1831
|
+
}, [frozenCount, plan, rowKey, rowState, translate]);
|
|
1466
1832
|
if (displayed.entries === null) return null;
|
|
1467
1833
|
const last = blocks.length - 1;
|
|
1468
1834
|
const rendered = [];
|
|
@@ -1477,9 +1843,10 @@ var AssistantStepView = (0, import_react3.memo)(function AssistantStepView2(prop
|
|
|
1477
1843
|
mark: entry.mark,
|
|
1478
1844
|
showingOriginal: entry.showingOriginal,
|
|
1479
1845
|
fail: entry.fail,
|
|
1846
|
+
failKind: entry.failKind,
|
|
1480
1847
|
retryBusy: entry.retryBusy,
|
|
1481
1848
|
onToggle: () => toggleBlock(entry.key),
|
|
1482
|
-
onRetry:
|
|
1849
|
+
onRetry: retryBlock,
|
|
1483
1850
|
streaming,
|
|
1484
1851
|
labels,
|
|
1485
1852
|
mentions,
|