dsh-cait 1.0.3 → 1.0.5
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/CHANGELOG.md +410 -352
- package/LICENSE +201 -201
- package/README.ja.md +55 -14
- package/README.md +466 -423
- package/README.zh-CN.md +429 -390
- package/client/components.js +2408 -2377
- package/client/helpers.js +103 -103
- package/client/locales.js +751 -730
- package/client/rpc.js +93 -93
- package/client/styles.js +258 -258
- package/client.js +3608 -3556
- package/icon.svg +8 -8
- package/index.js +45 -45
- package/lib/api.js +223 -223
- package/lib/docx.js +392 -389
- package/lib/epub.js +355 -0
- package/lib/errors.js +8 -8
- package/lib/formats.js +972 -917
- package/lib/glossary.js +178 -178
- package/lib/html.js +271 -0
- package/lib/import-export.js +650 -605
- package/lib/libraries.js +183 -183
- package/lib/markdown.js +28 -28
- package/lib/mt.js +152 -152
- package/lib/persistence.js +341 -321
- package/lib/pptx.js +89 -89
- package/lib/preprocess.js +65 -0
- package/lib/segmenter.js +84 -84
- package/lib/similarity.js +113 -113
- package/lib/store-utils.js +342 -339
- package/lib/store.js +746 -734
- package/lib/tm.js +160 -160
- package/lib/tmx.js +85 -85
- package/lib/tools.js +472 -472
- package/lib/xlsx.js +218 -218
- package/lib/xml.js +31 -31
- package/locale/en.json +11 -11
- package/locale/zh.json +11 -11
- package/package.json +83 -83
- package/scripts/build-client.mjs +13 -13
package/lib/store.js
CHANGED
|
@@ -1,734 +1,746 @@
|
|
|
1
|
-
import { CatError } from './errors.js'
|
|
2
|
-
import { createRequire } from 'node:module'
|
|
3
|
-
import { join, resolve } from 'node:path'
|
|
4
|
-
import { splitIntoSegments } from './segmenter.js'
|
|
5
|
-
import { canonicalLanguage, cleanFormatMeta, cleanLang, cleanName, cleanSegmentMeta, cleanText, clamp, clampInt, countWords, documentFingerprint, DEFAULT_SETTINGS, DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, DEFAULT_TM_ID, DEFAULT_TM_NAME, escapeRegExp, MAX_IMPORT_CHARS, MAX_SEGMENTS, MAX_SEGMENT_CHARS, now, makeId, parseGlossaryCsv, restoredDate, restoredLang, restoredText, restoreSettings, safeBaseName, STORE_FILENAME, UI_LANG_CODES } from './store-utils.js'
|
|
6
|
-
import { libraryMethods } from './libraries.js'
|
|
7
|
-
import { tmMethods } from './tm.js'
|
|
8
|
-
import { glossaryMethods } from './glossary.js'
|
|
9
|
-
import { persistenceMethods } from './persistence.js'
|
|
10
|
-
import { importExportMethods } from './import-export.js'
|
|
11
|
-
import { classifyMatchType, normalizeSource, normalizeText, similarity } from './similarity.js'
|
|
12
|
-
import { MOCK_MODEL, MOCK_PROVIDER, NO_MODEL_MESSAGE, NO_ROUTE_MESSAGE, collectStreamText } from './mt.js'
|
|
13
|
-
import { buildTmx } from './tmx.js'
|
|
14
|
-
import { missingInlineTokens } from './formats.js'
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
.
|
|
187
|
-
.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (text.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (rawSegments.length
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (input.
|
|
255
|
-
if (
|
|
256
|
-
this.
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
this.
|
|
260
|
-
this.
|
|
261
|
-
this.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
this.
|
|
283
|
-
this.
|
|
284
|
-
if (
|
|
285
|
-
|
|
286
|
-
this.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
this.
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
this.
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
this.
|
|
320
|
-
this.
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
this.
|
|
333
|
-
this.
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
this.
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
doc
|
|
354
|
-
this.
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
this.
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
if (
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (!
|
|
527
|
-
throw new CatError('
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
const
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
)
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
'
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
return {
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
:
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
['
|
|
721
|
-
['
|
|
722
|
-
['
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
1
|
+
import { CatError } from './errors.js'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { join, resolve } from 'node:path'
|
|
4
|
+
import { splitIntoSegments } from './segmenter.js'
|
|
5
|
+
import { canonicalLanguage, cleanFormatMeta, cleanLang, cleanName, cleanSegmentMeta, cleanText, clamp, clampInt, countWords, documentFingerprint, DEFAULT_SETTINGS, DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, DEFAULT_TM_ID, DEFAULT_TM_NAME, escapeRegExp, MAX_IMPORT_CHARS, MAX_SEGMENTS, MAX_SEGMENT_CHARS, now, makeId, parseGlossaryCsv, restoredDate, restoredLang, restoredText, restoreSettings, safeBaseName, STORE_FILENAME, UI_LANG_CODES } from './store-utils.js'
|
|
6
|
+
import { libraryMethods } from './libraries.js'
|
|
7
|
+
import { tmMethods } from './tm.js'
|
|
8
|
+
import { glossaryMethods } from './glossary.js'
|
|
9
|
+
import { persistenceMethods } from './persistence.js'
|
|
10
|
+
import { importExportMethods } from './import-export.js'
|
|
11
|
+
import { classifyMatchType, normalizeSource, normalizeText, similarity } from './similarity.js'
|
|
12
|
+
import { MOCK_MODEL, MOCK_PROVIDER, NO_MODEL_MESSAGE, NO_ROUTE_MESSAGE, collectStreamText } from './mt.js'
|
|
13
|
+
import { buildTmx } from './tmx.js'
|
|
14
|
+
import { missingInlineTokens } from './formats.js'
|
|
15
|
+
import { normalizePreprocessOptions, preprocessSegments } from './preprocess.js'
|
|
16
|
+
|
|
17
|
+
export { CatError } from './errors.js'
|
|
18
|
+
export { canonicalLanguage } from './store-utils.js'
|
|
19
|
+
|
|
20
|
+
export class CatStore {
|
|
21
|
+
|
|
22
|
+
constructor({ dataDir = '', logger = console, mtRuntime = null } = {}) {
|
|
23
|
+
this.docs = new Map()
|
|
24
|
+
this.activeDocId = null
|
|
25
|
+
this.openDocIds = []
|
|
26
|
+
this.tm = []
|
|
27
|
+
this.glossary = []
|
|
28
|
+
this.settings = structuredClone(DEFAULT_SETTINGS)
|
|
29
|
+
this.revision = 0
|
|
30
|
+
this.logger = logger
|
|
31
|
+
this.mtRuntime = mtRuntime
|
|
32
|
+
this.resetLibraryState()
|
|
33
|
+
this.dataDir = String(dataDir ?? '').trim() ? resolve(String(dataDir)) : ''
|
|
34
|
+
this.storageFile = this.dataDir ? join(this.dataDir, STORE_FILENAME) : ''
|
|
35
|
+
this.persistence = {
|
|
36
|
+
enabled: Boolean(this.storageFile),
|
|
37
|
+
file: this.storageFile || null,
|
|
38
|
+
loaded: false,
|
|
39
|
+
lastSavedAt: null,
|
|
40
|
+
error: null,
|
|
41
|
+
corruptBackup: null,
|
|
42
|
+
}
|
|
43
|
+
if (this.storageFile) this.load()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
touch() {
|
|
47
|
+
this.revision += 1
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
activeDocument() {
|
|
51
|
+
return this.activeDocId ? this.docs.get(this.activeDocId) ?? null : null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
requireDocument(documentId) {
|
|
55
|
+
const doc = documentId ? this.docs.get(documentId) : this.activeDocument()
|
|
56
|
+
if (!doc) throw new CatError('NO_DOCUMENT', '没有活动文档,请先在 CAIT 工作台创建或导入文档。', 409)
|
|
57
|
+
return doc
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
findSegment(doc, index) {
|
|
61
|
+
if (!Number.isInteger(index) || index < 0 || index >= doc.segments.length) {
|
|
62
|
+
throw new CatError('SEGMENT_OUT_OF_RANGE', `句段索引超出范围: ${index}`)
|
|
63
|
+
}
|
|
64
|
+
return doc.segments[index]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
publicSettings() {
|
|
68
|
+
return {
|
|
69
|
+
...this.settings,
|
|
70
|
+
mt: {
|
|
71
|
+
provider: this.settings.mt.provider,
|
|
72
|
+
model: this.settings.mt.model,
|
|
73
|
+
temperature: this.settings.mt.temperature,
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async listMtRoutes(options = {}) {
|
|
79
|
+
if (this.mtRuntime && typeof this.mtRuntime.listRoutes === 'function') {
|
|
80
|
+
return this.mtRuntime.listRoutes(options)
|
|
81
|
+
}
|
|
82
|
+
return { providers: [], fallback: true, message: NO_ROUTE_MESSAGE }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
progress(doc) {
|
|
86
|
+
const total = doc.segments.length
|
|
87
|
+
const confirmed = doc.segments.filter((segment) => segment.status === 'confirmed').length
|
|
88
|
+
return {
|
|
89
|
+
total,
|
|
90
|
+
confirmed,
|
|
91
|
+
pending: total - confirmed,
|
|
92
|
+
percent: total ? Math.round((confirmed / total) * 100) : 0,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
documentSummary(doc) {
|
|
97
|
+
return {
|
|
98
|
+
id: doc.id,
|
|
99
|
+
name: doc.name,
|
|
100
|
+
sourceLang: doc.sourceLang,
|
|
101
|
+
targetLang: doc.targetLang,
|
|
102
|
+
cursor: doc.cursor,
|
|
103
|
+
createdAt: doc.createdAt,
|
|
104
|
+
updatedAt: doc.updatedAt,
|
|
105
|
+
progress: this.progress(doc),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
segmentView(segment, index) {
|
|
110
|
+
return {
|
|
111
|
+
id: segment.id,
|
|
112
|
+
index,
|
|
113
|
+
source: segment.source,
|
|
114
|
+
target: segment.target,
|
|
115
|
+
status: segment.status,
|
|
116
|
+
paragraph: segment.paragraph,
|
|
117
|
+
confirmedAt: segment.confirmedAt,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
segmentDetail(doc, index) {
|
|
122
|
+
const segment = this.findSegment(doc, index)
|
|
123
|
+
return {
|
|
124
|
+
...this.segmentView(segment, index),
|
|
125
|
+
chars: Array.from(segment.source).length,
|
|
126
|
+
words: countWords(segment.source),
|
|
127
|
+
tmMatches: this.lookupTM({
|
|
128
|
+
query: segment.source,
|
|
129
|
+
sourceLang: doc.sourceLang,
|
|
130
|
+
targetLang: doc.targetLang,
|
|
131
|
+
}).matches,
|
|
132
|
+
glossaryHits: this.findGlossaryHits(segment.source, doc.sourceLang),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
documentView(doc) {
|
|
137
|
+
return {
|
|
138
|
+
...this.documentSummary(doc),
|
|
139
|
+
segments: doc.segments.map((segment, index) => this.segmentView(segment, index)),
|
|
140
|
+
current: this.segmentDetail(doc, doc.cursor),
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
bootstrap() {
|
|
145
|
+
const doc = this.activeDocument()
|
|
146
|
+
const mountedTm = new Set(this.mounts.tmIds)
|
|
147
|
+
const mountedGlossary = new Set(this.mounts.glossaryIds)
|
|
148
|
+
const mountedTmEntries = this.tm
|
|
149
|
+
.filter((entry) => mountedTm.has(entry.tmId || DEFAULT_TM_ID))
|
|
150
|
+
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))
|
|
151
|
+
const tmEntries = mountedTmEntries.slice(0, 40).map((entry) => ({
|
|
152
|
+
id: entry.id,
|
|
153
|
+
tmId: entry.tmId || DEFAULT_TM_ID,
|
|
154
|
+
source: entry.source,
|
|
155
|
+
target: entry.target,
|
|
156
|
+
sourceLang: entry.sourceLang,
|
|
157
|
+
targetLang: entry.targetLang,
|
|
158
|
+
updatedAt: entry.updatedAt,
|
|
159
|
+
}))
|
|
160
|
+
const glossary = this.glossary.filter((entry) =>
|
|
161
|
+
mountedGlossary.has(entry.glossaryId || DEFAULT_GLOSSARY_ID),
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
revision: this.revision,
|
|
166
|
+
settings: this.publicSettings(),
|
|
167
|
+
storage: {
|
|
168
|
+
enabled: this.persistence.enabled,
|
|
169
|
+
file: this.persistence.file,
|
|
170
|
+
loaded: this.persistence.loaded,
|
|
171
|
+
lastSavedAt: this.persistence.lastSavedAt,
|
|
172
|
+
error: this.persistence.error,
|
|
173
|
+
},
|
|
174
|
+
stats: {
|
|
175
|
+
documents: this.docs.size,
|
|
176
|
+
segments: [...this.docs.values()].reduce((sum, item) => sum + item.segments.length, 0),
|
|
177
|
+
confirmed: [...this.docs.values()].reduce(
|
|
178
|
+
(sum, item) => sum + item.segments.filter((segment) => segment.status === 'confirmed').length,
|
|
179
|
+
0,
|
|
180
|
+
),
|
|
181
|
+
tm: this.tm.length,
|
|
182
|
+
glossary: this.glossary.length,
|
|
183
|
+
},
|
|
184
|
+
documents: [...this.docs.values()].map((item) => this.documentSummary(item)),
|
|
185
|
+
openDocuments: this.openDocIds
|
|
186
|
+
.map((id) => this.docs.get(id))
|
|
187
|
+
.filter(Boolean)
|
|
188
|
+
.map((item) => this.documentSummary(item)),
|
|
189
|
+
activeDocument: doc ? this.documentView(doc) : null,
|
|
190
|
+
libraries: this.librariesView(),
|
|
191
|
+
mounts: this.mountsView(),
|
|
192
|
+
tm: {
|
|
193
|
+
count: mountedTmEntries.length,
|
|
194
|
+
total: this.tm.length,
|
|
195
|
+
entries: tmEntries,
|
|
196
|
+
},
|
|
197
|
+
glossary: glossary.map((entry) => ({ ...entry })),
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
createDocument(input = {}) {
|
|
202
|
+
const sourceLang = cleanLang(input.sourceLang || this.settings.sourceLang, this.settings.sourceLang)
|
|
203
|
+
const targetLang = cleanLang(input.targetLang || this.settings.targetLang, this.settings.targetLang)
|
|
204
|
+
let rawSegments = []
|
|
205
|
+
|
|
206
|
+
if (Array.isArray(input.segments) && input.segments.length) {
|
|
207
|
+
rawSegments = input.segments.map((segment) =>
|
|
208
|
+
typeof segment === 'string' ? { source: segment } : segment,
|
|
209
|
+
)
|
|
210
|
+
} else {
|
|
211
|
+
const text = String(input.text ?? '')
|
|
212
|
+
if (!text.trim()) throw new CatError('EMPTY_SOURCE', '源文本为空,无法创建文档。')
|
|
213
|
+
if (text.length > MAX_IMPORT_CHARS) {
|
|
214
|
+
throw new CatError('SOURCE_TOO_LARGE', `源文本超过 ${MAX_IMPORT_CHARS} 字符上限`)
|
|
215
|
+
}
|
|
216
|
+
rawSegments = splitIntoSegments(text)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (!rawSegments.length) throw new CatError('EMPTY_SOURCE', '没有可翻译的句段。')
|
|
220
|
+
rawSegments = preprocessSegments(rawSegments, this.settings.preprocess)
|
|
221
|
+
if (rawSegments.length > MAX_SEGMENTS) {
|
|
222
|
+
throw new CatError('TOO_MANY_SEGMENTS', `句段数量超过 ${MAX_SEGMENTS} 上限`)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const createdAt = now()
|
|
226
|
+
const paragraphMap = Array.isArray(input.paragraphMap) ? input.paragraphMap : null
|
|
227
|
+
const segments = rawSegments.map((raw, index) => {
|
|
228
|
+
const source = cleanText(raw.source, '原文')
|
|
229
|
+
const target = cleanText(raw.target, '译文')
|
|
230
|
+
if (!source) throw new CatError('EMPTY_SEGMENT', `第 ${index + 1} 句原文为空`)
|
|
231
|
+
const paragraph = paragraphMap && Number.isInteger(paragraphMap[raw.paragraph])
|
|
232
|
+
? paragraphMap[raw.paragraph]
|
|
233
|
+
: Number.isInteger(raw.paragraph)
|
|
234
|
+
? raw.paragraph
|
|
235
|
+
: index
|
|
236
|
+
const meta = cleanSegmentMeta(raw.meta)
|
|
237
|
+
return {
|
|
238
|
+
id: `s${index + 1}`,
|
|
239
|
+
source,
|
|
240
|
+
target,
|
|
241
|
+
status: target ? 'confirmed' : 'pending',
|
|
242
|
+
paragraph,
|
|
243
|
+
...(meta ? { meta } : {}),
|
|
244
|
+
confirmedAt: target ? createdAt : null,
|
|
245
|
+
updatedAt: createdAt,
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
const format = typeof input.format === 'string' ? input.format.slice(0, 24) : ''
|
|
250
|
+
const formatMeta = cleanFormatMeta(input.formatMeta)
|
|
251
|
+
const fingerprint = documentFingerprint(sourceLang, targetLang, segments, format)
|
|
252
|
+
const existing = [...this.docs.values()].find((item) => item.fingerprint === fingerprint)
|
|
253
|
+
if (existing) {
|
|
254
|
+
if (input.docxSource) this.attachDocxSource(existing, input.docxSource, input.docxName)
|
|
255
|
+
if (input.pptxSource) this.attachPptxSource(existing, input.pptxSource, input.pptxName)
|
|
256
|
+
if (input.epubSource) this.attachEpubSource(existing, input.epubSource, input.epubName)
|
|
257
|
+
if (input.format) existing.format = String(input.format).slice(0, 24)
|
|
258
|
+
if (formatMeta) existing.formatMeta = cleanFormatMeta({ ...(existing.formatMeta || {}), ...formatMeta })
|
|
259
|
+
this.activeDocId = existing.id
|
|
260
|
+
if (!this.openDocIds.includes(existing.id)) this.openDocIds.push(existing.id)
|
|
261
|
+
this.settings.sourceLang = sourceLang
|
|
262
|
+
this.settings.targetLang = targetLang
|
|
263
|
+
this.touch()
|
|
264
|
+
this.persist()
|
|
265
|
+
return { document: this.documentSummary(existing), reused: true }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const doc = {
|
|
269
|
+
id: makeId('doc'),
|
|
270
|
+
name: cleanName(input.name, String(input.name || '').trim() || '未命名文档'),
|
|
271
|
+
sourceLang,
|
|
272
|
+
targetLang,
|
|
273
|
+
segments,
|
|
274
|
+
cursor: segments.findIndex((segment) => segment.status === 'pending'),
|
|
275
|
+
createdAt,
|
|
276
|
+
updatedAt: createdAt,
|
|
277
|
+
fingerprint,
|
|
278
|
+
format,
|
|
279
|
+
...(formatMeta ? { formatMeta } : {}),
|
|
280
|
+
}
|
|
281
|
+
if (doc.cursor < 0) doc.cursor = 0
|
|
282
|
+
if (input.docxSource) this.attachDocxSource(doc, input.docxSource, input.docxName)
|
|
283
|
+
if (input.pptxSource) this.attachPptxSource(doc, input.pptxSource, input.pptxName)
|
|
284
|
+
if (input.epubSource) this.attachEpubSource(doc, input.epubSource, input.epubName)
|
|
285
|
+
|
|
286
|
+
this.docs.set(doc.id, doc)
|
|
287
|
+
this.activeDocId = doc.id
|
|
288
|
+
if (!this.openDocIds.includes(doc.id)) this.openDocIds.push(doc.id)
|
|
289
|
+
this.settings.sourceLang = sourceLang
|
|
290
|
+
this.settings.targetLang = targetLang
|
|
291
|
+
|
|
292
|
+
for (const segment of segments) {
|
|
293
|
+
if (segment.target) this.upsertTM(segment.source, segment.target, sourceLang, targetLang)
|
|
294
|
+
}
|
|
295
|
+
this.touch()
|
|
296
|
+
this.persist()
|
|
297
|
+
|
|
298
|
+
return { document: this.documentSummary(doc) }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
deleteDocument({ documentId } = {}) {
|
|
302
|
+
const doc = this.requireDocument(documentId)
|
|
303
|
+
const tabIndex = this.openDocIds.indexOf(doc.id)
|
|
304
|
+
this.removeDocxSource(doc)
|
|
305
|
+
this.removePptxSource(doc)
|
|
306
|
+
this.removeEpubSource(doc)
|
|
307
|
+
this.docs.delete(doc.id)
|
|
308
|
+
this.openDocIds = this.openDocIds.filter((id) => id !== doc.id)
|
|
309
|
+
if (this.activeDocId === doc.id) {
|
|
310
|
+
const nextId = this.openDocIds[Math.min(tabIndex, this.openDocIds.length - 1)]
|
|
311
|
+
this.activeDocId = nextId && this.docs.has(nextId) ? nextId : null
|
|
312
|
+
}
|
|
313
|
+
this.touch()
|
|
314
|
+
this.persist()
|
|
315
|
+
return { deleted: doc.id }
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
openDocument({ documentId } = {}) {
|
|
319
|
+
const doc = this.requireDocument(documentId)
|
|
320
|
+
this.activeDocId = doc.id
|
|
321
|
+
if (!this.openDocIds.includes(doc.id)) this.openDocIds.push(doc.id)
|
|
322
|
+
this.settings.sourceLang = doc.sourceLang
|
|
323
|
+
this.settings.targetLang = doc.targetLang
|
|
324
|
+
this.touch()
|
|
325
|
+
this.persist()
|
|
326
|
+
return { document: this.documentSummary(doc) }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
closeDocument({ documentId } = {}) {
|
|
330
|
+
const doc = this.requireDocument(documentId)
|
|
331
|
+
const tabIndex = this.openDocIds.indexOf(doc.id)
|
|
332
|
+
this.openDocIds = this.openDocIds.filter((id) => id !== doc.id)
|
|
333
|
+
if (this.activeDocId === doc.id) {
|
|
334
|
+
const nextId = this.openDocIds[Math.min(tabIndex, this.openDocIds.length - 1)]
|
|
335
|
+
this.activeDocId = nextId && this.docs.has(nextId) ? nextId : null
|
|
336
|
+
}
|
|
337
|
+
this.touch()
|
|
338
|
+
this.persist()
|
|
339
|
+
return { closed: doc.id }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
selectSegment({ documentId, index } = {}) {
|
|
343
|
+
const doc = this.requireDocument(documentId)
|
|
344
|
+
const target = Number(index ?? doc.cursor)
|
|
345
|
+
this.findSegment(doc, target)
|
|
346
|
+
doc.cursor = target
|
|
347
|
+
doc.updatedAt = now()
|
|
348
|
+
this.touch()
|
|
349
|
+
return { cursor: doc.cursor, current: this.segmentDetail(doc, doc.cursor) }
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
saveDraft({ documentId, index, target } = {}) {
|
|
353
|
+
const doc = this.requireDocument(documentId)
|
|
354
|
+
const segment = this.findSegment(doc, Number(index ?? doc.cursor))
|
|
355
|
+
const value = cleanText(target, '译文')
|
|
356
|
+
segment.target = value
|
|
357
|
+
segment.updatedAt = now()
|
|
358
|
+
doc.updatedAt = segment.updatedAt
|
|
359
|
+
this.touch()
|
|
360
|
+
return {
|
|
361
|
+
segment: this.segmentView(segment, doc.segments.indexOf(segment)),
|
|
362
|
+
cursor: doc.cursor,
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
confirmSegment({ documentId, index, target, addToTm = true, targetTmId } = {}) {
|
|
367
|
+
const doc = this.requireDocument(documentId)
|
|
368
|
+
const segmentIndex = Number(index ?? doc.cursor)
|
|
369
|
+
const segment = this.findSegment(doc, segmentIndex)
|
|
370
|
+
const writeTmId = addToTm ? this.requireMountedTmLibrary(targetTmId) : null
|
|
371
|
+
const before = {
|
|
372
|
+
revision: this.revision,
|
|
373
|
+
cursor: doc.cursor,
|
|
374
|
+
updatedAt: doc.updatedAt,
|
|
375
|
+
segment: {
|
|
376
|
+
target: segment.target,
|
|
377
|
+
status: segment.status,
|
|
378
|
+
confirmedAt: segment.confirmedAt,
|
|
379
|
+
updatedAt: segment.updatedAt,
|
|
380
|
+
},
|
|
381
|
+
tm: addToTm ? this.tm.map((entry) => ({ ...entry })) : null,
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (target !== undefined) {
|
|
385
|
+
segment.target = cleanText(target, '译文')
|
|
386
|
+
}
|
|
387
|
+
if (!segment.target) {
|
|
388
|
+
throw new CatError('EMPTY_TARGET', '译文为空,无法确认句段。')
|
|
389
|
+
}
|
|
390
|
+
const missingInline = missingInlineTokens(segment.meta?.inline, segment.target)
|
|
391
|
+
if (missingInline.length) {
|
|
392
|
+
throw new CatError(
|
|
393
|
+
'PLACEHOLDER_MISMATCH',
|
|
394
|
+
`译文缺少占位符 ${missingInline.join('、')},请保留源文中的占位符后再确认。`,
|
|
395
|
+
400,
|
|
396
|
+
)
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
segment.status = 'confirmed'
|
|
400
|
+
segment.confirmedAt = now()
|
|
401
|
+
segment.updatedAt = segment.confirmedAt
|
|
402
|
+
doc.updatedAt = segment.confirmedAt
|
|
403
|
+
|
|
404
|
+
const tmResult = addToTm
|
|
405
|
+
? this.upsertTM(segment.source, segment.target, doc.sourceLang, doc.targetLang, writeTmId)
|
|
406
|
+
: null
|
|
407
|
+
|
|
408
|
+
const nextIndex = this.findNextUnconfirmed(doc, segmentIndex)
|
|
409
|
+
doc.cursor = nextIndex
|
|
410
|
+
this.touch()
|
|
411
|
+
try {
|
|
412
|
+
this.persist({ strict: true })
|
|
413
|
+
} catch (error) {
|
|
414
|
+
if (before.tm) this.tm = before.tm
|
|
415
|
+
this.revision = before.revision
|
|
416
|
+
doc.cursor = before.cursor
|
|
417
|
+
doc.updatedAt = before.updatedAt
|
|
418
|
+
Object.assign(segment, before.segment)
|
|
419
|
+
throw error
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const progress = this.progress(doc)
|
|
423
|
+
return {
|
|
424
|
+
confirmed: this.segmentView(segment, segmentIndex),
|
|
425
|
+
next: progress.pending ? this.segmentDetail(doc, doc.cursor) : null,
|
|
426
|
+
cursor: doc.cursor,
|
|
427
|
+
progress,
|
|
428
|
+
done: progress.pending === 0,
|
|
429
|
+
tm: tmResult
|
|
430
|
+
? {
|
|
431
|
+
added: tmResult.created ? 1 : 0,
|
|
432
|
+
updated: tmResult.created ? 0 : 1,
|
|
433
|
+
count: this.mountedTmCount(),
|
|
434
|
+
total: this.tm.length,
|
|
435
|
+
tmId: tmResult.entry.tmId,
|
|
436
|
+
}
|
|
437
|
+
: null,
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
findNextUnconfirmed(doc, fromIndex) {
|
|
442
|
+
const total = doc.segments.length
|
|
443
|
+
for (let step = 1; step <= total; step += 1) {
|
|
444
|
+
const index = (fromIndex + step) % total
|
|
445
|
+
if (doc.segments[index].status !== 'confirmed') return index
|
|
446
|
+
}
|
|
447
|
+
return fromIndex
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
moveSegment({ documentId, delta = 1 } = {}) {
|
|
451
|
+
const doc = this.requireDocument(documentId)
|
|
452
|
+
const total = doc.segments.length
|
|
453
|
+
const amount = Number.isFinite(Number(delta)) ? Math.trunc(Number(delta)) : 1
|
|
454
|
+
doc.cursor = ((doc.cursor + amount) % total + total) % total
|
|
455
|
+
doc.updatedAt = now()
|
|
456
|
+
this.touch()
|
|
457
|
+
return { cursor: doc.cursor, current: this.segmentDetail(doc, doc.cursor) }
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
saveSettings(patch = {}) {
|
|
461
|
+
if (patch.sourceLang !== undefined) {
|
|
462
|
+
this.settings.sourceLang = cleanLang(patch.sourceLang, this.settings.sourceLang)
|
|
463
|
+
}
|
|
464
|
+
if (patch.targetLang !== undefined) {
|
|
465
|
+
this.settings.targetLang = cleanLang(patch.targetLang, this.settings.targetLang)
|
|
466
|
+
}
|
|
467
|
+
if (patch.fuzzyThreshold !== undefined) {
|
|
468
|
+
this.settings.fuzzyThreshold = clamp(Number(patch.fuzzyThreshold), 0, 1)
|
|
469
|
+
}
|
|
470
|
+
if (patch.tmLimit !== undefined) {
|
|
471
|
+
this.settings.tmLimit = clampInt(patch.tmLimit, 1, 20, this.settings.tmLimit)
|
|
472
|
+
}
|
|
473
|
+
if (patch.uiLang !== undefined) {
|
|
474
|
+
const uiLang = String(patch.uiLang ?? '').trim().toLowerCase()
|
|
475
|
+
if (!UI_LANG_CODES.includes(uiLang)) {
|
|
476
|
+
throw new CatError('INVALID_UI_LANG', '界面语言无效,可选 auto / zh / en / ja。')
|
|
477
|
+
}
|
|
478
|
+
this.settings.uiLang = uiLang
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (patch.preprocess && typeof patch.preprocess === 'object') {
|
|
482
|
+
this.settings.preprocess = normalizePreprocessOptions({
|
|
483
|
+
...this.settings.preprocess,
|
|
484
|
+
...patch.preprocess,
|
|
485
|
+
})
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (patch.mt && typeof patch.mt === 'object') {
|
|
489
|
+
const mt = patch.mt
|
|
490
|
+
if (mt.provider !== undefined) {
|
|
491
|
+
const provider = String(mt.provider ?? '').trim()
|
|
492
|
+
if (!provider) throw new CatError('INVALID_PROVIDER', '请选择 MT provider 路由。')
|
|
493
|
+
this.settings.mt.provider = provider.slice(0, 120)
|
|
494
|
+
}
|
|
495
|
+
if (mt.model !== undefined) {
|
|
496
|
+
this.settings.mt.model = String(mt.model ?? '').trim().slice(0, 200)
|
|
497
|
+
}
|
|
498
|
+
if (mt.temperature !== undefined) {
|
|
499
|
+
this.settings.mt.temperature = clamp(Number(mt.temperature), 0, 2)
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
this.touch()
|
|
504
|
+
const storage = this.persist()
|
|
505
|
+
return { settings: this.publicSettings(), storage }
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
mockSuggestion(sourceText, hits, started, note) {
|
|
509
|
+
let output = sourceText
|
|
510
|
+
for (const hit of hits) {
|
|
511
|
+
output = output.replace(new RegExp(escapeRegExp(hit.term), 'gi'), hit.target)
|
|
512
|
+
}
|
|
513
|
+
if (output === sourceText) output = `【机器翻译示例 · 术语替换】${sourceText}`
|
|
514
|
+
return {
|
|
515
|
+
text: output,
|
|
516
|
+
provider: MOCK_PROVIDER,
|
|
517
|
+
model: MOCK_MODEL,
|
|
518
|
+
latencyMs: Date.now() - started,
|
|
519
|
+
note,
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
async mtSuggest({ text, documentId, index, sourceLang, targetLang } = {}) {
|
|
524
|
+
let doc = null
|
|
525
|
+
if (documentId !== undefined && documentId !== null) {
|
|
526
|
+
if (typeof documentId !== 'string' || !documentId.trim()) {
|
|
527
|
+
throw new CatError('DOCUMENT_NOT_FOUND', '文档不存在。', 404)
|
|
528
|
+
}
|
|
529
|
+
doc = this.docs.get(documentId)
|
|
530
|
+
if (!doc) throw new CatError('DOCUMENT_NOT_FOUND', `文档不存在: ${documentId}`, 404)
|
|
531
|
+
} else {
|
|
532
|
+
doc = this.activeDocument()
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
let segment = null
|
|
536
|
+
if (index !== undefined && index !== null) {
|
|
537
|
+
if (!doc) throw new CatError('NO_DOCUMENT', '没有活动文档,无法定位句段。', 409)
|
|
538
|
+
if (!Number.isInteger(index)) {
|
|
539
|
+
throw new CatError('SEGMENT_OUT_OF_RANGE', `句段索引不是整数: ${index}`)
|
|
540
|
+
}
|
|
541
|
+
segment = this.findSegment(doc, index)
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const fallbackSource = segment?.source ?? (doc ? doc.segments[doc.cursor]?.source : undefined)
|
|
545
|
+
if (text === undefined && fallbackSource === undefined) {
|
|
546
|
+
throw new CatError('NO_DOCUMENT', '没有活动文档,无法生成译文建议。', 409)
|
|
547
|
+
}
|
|
548
|
+
const sourceText = cleanText(text === undefined ? fallbackSource : text, '原文')
|
|
549
|
+
if (!sourceText) throw new CatError('EMPTY_SOURCE', '没有可翻译的原文。')
|
|
550
|
+
|
|
551
|
+
const src = canonicalLanguage(sourceLang || doc?.sourceLang || this.settings.sourceLang)
|
|
552
|
+
const tgt = canonicalLanguage(targetLang || doc?.targetLang || this.settings.targetLang)
|
|
553
|
+
const started = Date.now()
|
|
554
|
+
const provider = this.settings.mt.provider
|
|
555
|
+
const hits = this.findGlossaryHits(sourceText, src)
|
|
556
|
+
|
|
557
|
+
let routes
|
|
558
|
+
try {
|
|
559
|
+
routes = await this.listMtRoutes()
|
|
560
|
+
} catch {
|
|
561
|
+
routes = { providers: [], fallback: true, message: NO_ROUTE_MESSAGE }
|
|
562
|
+
}
|
|
563
|
+
const providers = Array.isArray(routes?.providers) ? routes.providers : []
|
|
564
|
+
const selectedRoute = providers.find((route) => route.id === provider)
|
|
565
|
+
|
|
566
|
+
if (provider === MOCK_PROVIDER) {
|
|
567
|
+
return this.mockSuggestion(
|
|
568
|
+
sourceText,
|
|
569
|
+
hits,
|
|
570
|
+
started,
|
|
571
|
+
providers.length ? '术语替换(离线):当前未调用 DSH 模型。' : NO_ROUTE_MESSAGE,
|
|
572
|
+
)
|
|
573
|
+
}
|
|
574
|
+
if (!selectedRoute) {
|
|
575
|
+
return this.mockSuggestion(sourceText, hits, started, NO_ROUTE_MESSAGE)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const models = Array.isArray(selectedRoute.models) ? selectedRoute.models : []
|
|
579
|
+
const model = models.some((item) => item.id === this.settings.mt.model)
|
|
580
|
+
? this.settings.mt.model
|
|
581
|
+
: models[0]?.id
|
|
582
|
+
if (!model) {
|
|
583
|
+
return this.mockSuggestion(sourceText, hits, started, NO_MODEL_MESSAGE)
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const glossaryRule = hits.length
|
|
587
|
+
? `\nUse this terminology consistently: ${hits.map((hit) => `${hit.term} => ${hit.target}`).join('; ')}.`
|
|
588
|
+
: ''
|
|
589
|
+
const systemText =
|
|
590
|
+
'You are a professional translator. Preserve meaning, numbers, markup and tone. Return only the translation, without quotes or explanations.'
|
|
591
|
+
const messages = [
|
|
592
|
+
{
|
|
593
|
+
role: 'user',
|
|
594
|
+
content: [
|
|
595
|
+
{
|
|
596
|
+
type: 'text',
|
|
597
|
+
text: `Translate from ${src} to ${tgt}.${glossaryRule}\n\n${sourceText}`,
|
|
598
|
+
},
|
|
599
|
+
],
|
|
600
|
+
},
|
|
601
|
+
]
|
|
602
|
+
|
|
603
|
+
let output
|
|
604
|
+
try {
|
|
605
|
+
const stream = await this.mtRuntime.stream({
|
|
606
|
+
provider,
|
|
607
|
+
model,
|
|
608
|
+
messages,
|
|
609
|
+
system: systemText,
|
|
610
|
+
temperature: this.settings.mt.temperature,
|
|
611
|
+
purpose: 'cait-mt',
|
|
612
|
+
signal: AbortSignal.timeout(45000),
|
|
613
|
+
})
|
|
614
|
+
output = await collectStreamText(stream)
|
|
615
|
+
if (!output) {
|
|
616
|
+
const empty = new Error('DSH 模型路由没有返回译文')
|
|
617
|
+
empty.code = 'MT_EMPTY'
|
|
618
|
+
throw empty
|
|
619
|
+
}
|
|
620
|
+
} catch (error) {
|
|
621
|
+
this.logger?.warn?.(
|
|
622
|
+
`CAIT: DSH MT 路由调用失败 provider=${provider} model=${model} code=${
|
|
623
|
+
error?.code || 'UNKNOWN'
|
|
624
|
+
} status=${error?.status ?? '-'}: ${error?.message || error}`,
|
|
625
|
+
)
|
|
626
|
+
throw new CatError(
|
|
627
|
+
'MT_PROVIDER_FAILED',
|
|
628
|
+
'机器翻译调用失败,请检查 DSH 模型路由配置。',
|
|
629
|
+
502,
|
|
630
|
+
)
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
return {
|
|
634
|
+
text: output,
|
|
635
|
+
provider,
|
|
636
|
+
model,
|
|
637
|
+
latencyMs: Date.now() - started,
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
getSegmentForTool({ documentId } = {}) {
|
|
642
|
+
const doc = this.requireDocument(documentId)
|
|
643
|
+
const detail = this.segmentDetail(doc, doc.cursor)
|
|
644
|
+
const progress = this.progress(doc)
|
|
645
|
+
return {
|
|
646
|
+
document: {
|
|
647
|
+
id: doc.id,
|
|
648
|
+
name: doc.name,
|
|
649
|
+
sourceLang: doc.sourceLang,
|
|
650
|
+
targetLang: doc.targetLang,
|
|
651
|
+
total: progress.total,
|
|
652
|
+
cursor: doc.cursor,
|
|
653
|
+
},
|
|
654
|
+
segment: {
|
|
655
|
+
id: detail.id,
|
|
656
|
+
index: detail.index,
|
|
657
|
+
source: detail.source,
|
|
658
|
+
target: detail.target,
|
|
659
|
+
status: detail.status,
|
|
660
|
+
},
|
|
661
|
+
progress,
|
|
662
|
+
tmMatches: detail.tmMatches,
|
|
663
|
+
glossaryHits: detail.glossaryHits,
|
|
664
|
+
done: progress.pending === 0,
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
submitTranslationForTool({ documentId, index, target, addToTm = true, targetTmId } = {}) {
|
|
669
|
+
const result = this.confirmSegment({ documentId, index, target, addToTm, targetTmId })
|
|
670
|
+
const doc = this.requireDocument(documentId)
|
|
671
|
+
return {
|
|
672
|
+
document: {
|
|
673
|
+
id: doc.id,
|
|
674
|
+
name: doc.name,
|
|
675
|
+
sourceLang: doc.sourceLang,
|
|
676
|
+
targetLang: doc.targetLang,
|
|
677
|
+
total: doc.segments.length,
|
|
678
|
+
cursor: doc.cursor,
|
|
679
|
+
},
|
|
680
|
+
confirmed: {
|
|
681
|
+
index: result.confirmed.index,
|
|
682
|
+
source: result.confirmed.source,
|
|
683
|
+
target: result.confirmed.target,
|
|
684
|
+
status: result.confirmed.status,
|
|
685
|
+
},
|
|
686
|
+
next: result.next
|
|
687
|
+
? {
|
|
688
|
+
index: result.next.index,
|
|
689
|
+
source: result.next.source,
|
|
690
|
+
target: result.next.target,
|
|
691
|
+
status: result.next.status,
|
|
692
|
+
}
|
|
693
|
+
: null,
|
|
694
|
+
progress: result.progress,
|
|
695
|
+
done: result.done,
|
|
696
|
+
tm: {
|
|
697
|
+
...result.tm,
|
|
698
|
+
count: result.tm?.count ?? this.mountedTmCount(),
|
|
699
|
+
total: this.tm.length,
|
|
700
|
+
},
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
loadDemo() {
|
|
705
|
+
const text = [
|
|
706
|
+
'Computer-aided translation tools help translators work faster and more consistently. A translation memory stores previously translated sentences, so repetitive passages can be reused with a single click.',
|
|
707
|
+
'Terminology databases keep product names and technical terms consistent across documents. Every confirmed segment can be written back to the translation memory, building a reusable knowledge base for the next project.',
|
|
708
|
+
'A good CAT workbench brings the source text, the target editor, translation memory matches, glossary hits, and machine translation suggestions into one focused workspace.',
|
|
709
|
+
].join('\n\n')
|
|
710
|
+
|
|
711
|
+
const created = this.createDocument({
|
|
712
|
+
name: 'CAIT 演示 · 产品简介',
|
|
713
|
+
text,
|
|
714
|
+
sourceLang: 'en',
|
|
715
|
+
targetLang: 'zh-CN',
|
|
716
|
+
})
|
|
717
|
+
|
|
718
|
+
const terms = [
|
|
719
|
+
['computer-aided translation', '计算机辅助翻译', 'CAT 的全称'],
|
|
720
|
+
['translation memory', '翻译记忆库', '存储已翻译句段'],
|
|
721
|
+
['terminology database', '术语库', '维护术语一致性'],
|
|
722
|
+
['segment', '句段', 'CAT 的基本翻译单位'],
|
|
723
|
+
['workspace', '工作台', '译者的操作界面'],
|
|
724
|
+
]
|
|
725
|
+
for (const [source, target, note] of terms) {
|
|
726
|
+
if (!this.glossary.some((entry) => normalizeText(entry.source) === normalizeText(source))) {
|
|
727
|
+
this.addGlossary({ source, target, note, sourceLang: 'en' })
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const memories = [
|
|
732
|
+
['A translation memory stores previously translated sentences.', '翻译记忆库存储以前翻译过的句子。'],
|
|
733
|
+
['Computer-aided translation tools help translators work faster and more consistently.', '计算机辅助翻译工具帮助译者更快、更一致地工作。'],
|
|
734
|
+
['Terminology databases keep product names and technical terms consistent.', '术语库让产品名称和技术术语保持一致。'],
|
|
735
|
+
]
|
|
736
|
+
for (const [source, target] of memories) {
|
|
737
|
+
this.upsertTM(source, target, 'en', 'zh-CN')
|
|
738
|
+
}
|
|
739
|
+
this.touch()
|
|
740
|
+
this.persist()
|
|
741
|
+
|
|
742
|
+
return created
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
Object.assign(CatStore.prototype, libraryMethods, tmMethods, glossaryMethods, persistenceMethods, importExportMethods)
|