dsh-cait 1.1.3 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,14 @@
1
1
  import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
3
  import { CatError } from './errors.js'
4
- import { MAX_DOCX_BASE64, MAX_IMPORT_CHARS, MAX_SEGMENTS, MAX_SEGMENT_CHARS, canonicalLanguage, cleanLang, cleanName, cleanText, csvCell, makeId, mdCell, now, safeBaseName } from './store-utils.js'
4
+ import { MAX_DOCX_BASE64, MAX_IMPORT_CHARS, MAX_SEGMENTS, MAX_SEGMENT_CHARS, canonicalLanguage, cleanLang, cleanName, cleanText, csvCell, makeId, mdCell, normalizeSegmentState, now, safeBaseName } from './store-utils.js'
5
5
  import { stripMarkdown } from './markdown.js'
6
- import { DOCX_MIME, buildDocx, buildDocxFromText, parseDocxDetailed } from './docx.js'
6
+ import { DOCX_MIME, buildBilingualDocx, buildDocx, buildDocxFromText, parseDocxDetailed } from './docx.js'
7
7
  import { EPUB_MIME, buildEpub, buildEpubFromText, parseEpub } from './epub.js'
8
8
  import { PPTX_MIME, buildPptx, parsePptxDetailed } from './pptx.js'
9
9
  import { XLSX_MIME, buildXlsx, parseXlsx } from './xlsx.js'
10
10
  import { buildTmx, parseTmx } from './tmx.js'
11
- import { buildHtml, buildJson, buildPo, buildRtf, buildSrt, buildVtt, buildXliff, buildXliff2, parseCsv, parseHtml, parseJson, parsePo, parseRtf, parseSrt, parseVtt, parseXliff } from './formats.js'
11
+ import { buildHtml, buildJson, buildPo, buildRtf, buildSrt, buildVtt, buildXliff, buildXliff2, buildXliffReview, parseCsv, parseHtml, parseJson, parsePo, parseRtf, parseSrt, parseVtt, parseXliff } from './formats.js'
12
12
  import { decodeXml } from './xml.js'
13
13
 
14
14
  const BINARY_SOURCES = {
@@ -243,6 +243,78 @@ export const importExportMethods = {
243
243
  }
244
244
  },
245
245
 
246
+ applyXliffReview({ documentId, text, overwrite = false } = {}) {
247
+ const doc = this.requireDocument(documentId)
248
+ if (!text || !String(text).trim()) throw new CatError('EMPTY_FILE', '审校包内容为空。')
249
+ const parsed = parseXliff(String(text))
250
+ const normalize = (value) => String(value ?? '').replace(/\s+/g, ' ').trim()
251
+ const byUid = new Map(doc.segments.filter((segment) => segment.uid).map((segment) => [segment.uid, segment]))
252
+ const byId = new Map(doc.segments.map((segment) => [segment.id, segment]))
253
+ const byXliffId = new Map(
254
+ doc.segments
255
+ .filter((segment) => segment.meta?.xliffId)
256
+ .map((segment) => [String(segment.meta.xliffId), segment]),
257
+ )
258
+ const bySource = new Map()
259
+ for (const segment of doc.segments) {
260
+ const key = normalize(segment.source)
261
+ if (!bySource.has(key)) bySource.set(key, [])
262
+ bySource.get(key).push(segment)
263
+ }
264
+
265
+ let applied = 0
266
+ let changed = 0
267
+ let unmatched = 0
268
+ let sourceMismatch = 0
269
+ const now = new Date().toISOString()
270
+ for (const incoming of parsed.segments) {
271
+ const uid = incoming.meta?.caitUid
272
+ const xliffId = incoming.meta?.xliffId
273
+ let segment = uid ? byUid.get(uid) : null
274
+ if (!segment && xliffId) segment = byXliffId.get(String(xliffId))
275
+ if (!segment && xliffId) segment = byId.get(String(xliffId))
276
+ if (!segment) {
277
+ const matches = bySource.get(normalize(incoming.source)) || []
278
+ if (matches.length === 1) segment = matches[0]
279
+ }
280
+ if (!segment) {
281
+ unmatched += 1
282
+ continue
283
+ }
284
+ if (normalize(segment.source) !== normalize(incoming.source)) {
285
+ sourceMismatch += 1
286
+ continue
287
+ }
288
+ const target = String(incoming.target ?? '').trim()
289
+ if (!target) continue
290
+ if (!overwrite && segment.target === target) continue
291
+ segment.target = target
292
+ segment.suggestion = null
293
+ segment.suggestionReason = null
294
+ segment.origin = 'manual'
295
+ segment.confirmedAt = ['final', 'signed-off'].includes(incoming.meta?.targetState)
296
+ ? now
297
+ : null
298
+ segment.updatedAt = now
299
+ normalizeSegmentState(segment)
300
+ applied += 1
301
+ changed += 1
302
+ }
303
+ if (changed) {
304
+ this.touch()
305
+ this.persist()
306
+ }
307
+ return {
308
+ documentId: doc.id,
309
+ documentName: doc.name,
310
+ total: parsed.segments.length,
311
+ applied,
312
+ changed,
313
+ unmatched,
314
+ sourceMismatch,
315
+ }
316
+ },
317
+
246
318
  importFile({ name, text, base64, sourceLang, targetLang, importAs, csvSourceColumn, csvTargetColumn } = {}) {
247
319
  const fileName = cleanName(name, '导入文档')
248
320
  const extension = (fileName.match(/\.([a-z0-9]+)$/i)?.[1] ?? '').toLowerCase()
@@ -479,6 +551,19 @@ export const importExportMethods = {
479
551
  }
480
552
  }
481
553
 
554
+ if (format === 'bilingual-docx') {
555
+ const content = buildBilingualDocx(doc.segments, {
556
+ includeStatus: true,
557
+ title: doc.name,
558
+ })
559
+ return {
560
+ filename: `${base}.bilingual.docx`,
561
+ mime: DOCX_MIME,
562
+ content,
563
+ count: doc.segments.length,
564
+ }
565
+ }
566
+
482
567
  if (format === 'epub') {
483
568
  let content = null
484
569
  const template = this.readEpubSource(doc)
@@ -525,6 +610,15 @@ export const importExportMethods = {
525
610
  }
526
611
  }
527
612
 
613
+ if (format === 'xliff-review') {
614
+ return {
615
+ filename: `${base}.review.xliff`,
616
+ mime: 'application/x-xliff+xml;charset=utf-8',
617
+ content: buildXliffReview(doc),
618
+ count: doc.segments.length,
619
+ }
620
+ }
621
+
528
622
  if (format === 'xliff' || format === 'xlf' || format === 'sdlxliff') {
529
623
  return {
530
624
  filename: `${base}.${doc.targetLang}.${format === 'sdlxliff' ? 'sdlxliff' : 'xliff'}`,
package/lib/libraries.js CHANGED
@@ -76,6 +76,152 @@ export const libraryMethods = {
76
76
  defaultGlossaryId: this.mounts.defaultGlossaryId,
77
77
  }
78
78
  }
79
+ ,
80
+ /**
81
+ * A document-level binding is either null (inherit the global mounts) or an
82
+ * explicit list. An explicit empty list is meaningful: this document must not
83
+ * read or write that kind of resource at all.
84
+ */
85
+ normalizeResourceIds(kind, ids) {
86
+ if (ids === null || ids === undefined) return null
87
+ if (!Array.isArray(ids)) {
88
+ throw new CatError('INVALID_RESOURCE_IDS', '文档绑定的库列表必须是数组或 null。')
89
+ }
90
+ const unique = []
91
+ for (const raw of ids) {
92
+ const id = String(raw ?? '').trim()
93
+ if (!id || unique.includes(id)) continue
94
+ if (kind === 'tm') this.requireTmLibrary(id)
95
+ else this.requireGlossaryLibrary(id)
96
+ unique.push(id)
97
+ }
98
+ return unique
99
+ }
100
+ ,
101
+ effectiveTmIds(doc) {
102
+ if (!doc || doc.tmIds === null || doc.tmIds === undefined) return [...this.mounts.tmIds]
103
+ const known = new Set(this.tmLibraries.map((library) => library.tmId))
104
+ return doc.tmIds.filter((id) => known.has(id))
105
+ }
106
+ ,
107
+ effectiveGlossaryIds(doc) {
108
+ if (!doc || doc.glossaryIds === null || doc.glossaryIds === undefined) {
109
+ return [...this.mounts.glossaryIds]
110
+ }
111
+ const known = new Set(this.glossaryLibraries.map((library) => library.glossaryId))
112
+ return doc.glossaryIds.filter((id) => known.has(id))
113
+ }
114
+ ,
115
+ effectiveDefaultTmId(doc) {
116
+ const ids = this.effectiveTmIds(doc)
117
+ const preferred = doc?.defaultWriteTmId
118
+ if (preferred && ids.includes(preferred)) return preferred
119
+ // Inheriting documents keep the global write preference. Explicit document
120
+ // bindings fall back to their own first library.
121
+ if ((doc?.tmIds === null || doc?.tmIds === undefined) && ids.includes(this.mounts.defaultTmId)) {
122
+ return this.mounts.defaultTmId
123
+ }
124
+ return ids[0] ?? null
125
+ }
126
+ ,
127
+ documentWriteTmId(doc, requestedTmId) {
128
+ const ids = this.effectiveTmIds(doc)
129
+ if (!ids.length) {
130
+ throw new CatError(
131
+ 'NO_TM_BOUND',
132
+ '当前文档没有绑定任何翻译记忆库,无法写入。请在文档资源里选择一个写入库。',
133
+ 409,
134
+ )
135
+ }
136
+ let id = requestedTmId ? this.requireTmLibrary(requestedTmId) : this.effectiveDefaultTmId(doc)
137
+ if (requestedTmId && id && !this.mounts.tmIds.includes(id)) {
138
+ throw new CatError(
139
+ 'TM_NOT_MOUNTED',
140
+ `翻译记忆库未挂载: ${id},请先在 CAIT 的库面板挂载它。`,
141
+ 409,
142
+ )
143
+ }
144
+ if (!id || !ids.includes(id)) {
145
+ throw new CatError(
146
+ 'TM_NOT_BOUND',
147
+ `翻译记忆库未绑定到当前文档: ${id ?? ''},请先在文档资源里选择它。`,
148
+ 409,
149
+ )
150
+ }
151
+ return id
152
+ }
153
+ ,
154
+ documentResourcesView(doc) {
155
+ return {
156
+ tmIds: doc.tmIds === null || doc.tmIds === undefined ? null : [...doc.tmIds],
157
+ glossaryIds:
158
+ doc.glossaryIds === null || doc.glossaryIds === undefined ? null : [...doc.glossaryIds],
159
+ defaultWriteTmId: doc.defaultWriteTmId ?? null,
160
+ effectiveTmIds: this.effectiveTmIds(doc),
161
+ effectiveGlossaryIds: this.effectiveGlossaryIds(doc),
162
+ effectiveDefaultTmId: this.effectiveDefaultTmId(doc),
163
+ inheritsTm: doc.tmIds === null || doc.tmIds === undefined,
164
+ inheritsGlossary: doc.glossaryIds === null || doc.glossaryIds === undefined,
165
+ }
166
+ }
167
+ ,
168
+ setDocumentResources(input = {}) {
169
+ const { documentId, tmIds, glossaryIds, defaultWriteTmId } = input
170
+ const doc = this.requireDocument(documentId)
171
+ const hasTm = Object.prototype.hasOwnProperty.call(input, 'tmIds')
172
+ const hasGlossary = Object.prototype.hasOwnProperty.call(input, 'glossaryIds')
173
+ const hasDefault = Object.prototype.hasOwnProperty.call(input, 'defaultWriteTmId')
174
+
175
+ const nextTmIds = hasTm ? this.normalizeResourceIds('tm', tmIds) : doc.tmIds
176
+ const nextGlossaryIds = hasGlossary
177
+ ? this.normalizeResourceIds('glossary', glossaryIds)
178
+ : doc.glossaryIds
179
+ let nextDefaultTmId = hasDefault ? defaultWriteTmId : doc.defaultWriteTmId
180
+ if (nextDefaultTmId === undefined || nextDefaultTmId === '') nextDefaultTmId = null
181
+ const effectiveTm = nextTmIds === null || nextTmIds === undefined
182
+ ? this.mounts.tmIds
183
+ : nextTmIds
184
+ if (nextDefaultTmId) {
185
+ nextDefaultTmId = this.requireTmLibrary(nextDefaultTmId)
186
+ if (!effectiveTm.includes(nextDefaultTmId)) {
187
+ if (hasDefault) {
188
+ throw new CatError(
189
+ 'TM_NOT_BOUND',
190
+ `翻译记忆库未绑定到当前文档: ${nextDefaultTmId},不能设为默认写入库。`,
191
+ 409,
192
+ )
193
+ }
194
+ nextDefaultTmId = null
195
+ }
196
+ }
197
+
198
+ const before = {
199
+ revision: this.revision,
200
+ updatedAt: doc.updatedAt,
201
+ tmIds: doc.tmIds,
202
+ glossaryIds: doc.glossaryIds,
203
+ defaultWriteTmId: doc.defaultWriteTmId,
204
+ }
205
+ doc.tmIds = nextTmIds
206
+ doc.glossaryIds = nextGlossaryIds
207
+ doc.defaultWriteTmId = nextDefaultTmId
208
+ doc.updatedAt = now()
209
+ this.touch()
210
+ try {
211
+ this.persist({ strict: true })
212
+ } catch (error) {
213
+ this.revision = before.revision
214
+ doc.updatedAt = before.updatedAt
215
+ doc.tmIds = before.tmIds
216
+ doc.glossaryIds = before.glossaryIds
217
+ doc.defaultWriteTmId = before.defaultWriteTmId
218
+ throw error
219
+ }
220
+ return {
221
+ document: this.documentSummary(doc),
222
+ resources: this.documentResourcesView(doc),
223
+ }
224
+ }
79
225
  ,
80
226
  librariesView() {
81
227
  const mountedTm = new Set(this.mounts.tmIds)
@@ -175,6 +321,16 @@ export const libraryMethods = {
175
321
  this.mounts[idsKey] = remaining.length ? remaining : [list[0][idKey]]
176
322
  if (this.mounts[defaultKey] === libraryId) this.mounts[defaultKey] = this.mounts[idsKey][0]
177
323
 
324
+ for (const doc of this.docs.values()) {
325
+ if (isTm && Array.isArray(doc.tmIds)) {
326
+ doc.tmIds = doc.tmIds.filter((id) => id !== libraryId)
327
+ }
328
+ if (!isTm && Array.isArray(doc.glossaryIds)) {
329
+ doc.glossaryIds = doc.glossaryIds.filter((id) => id !== libraryId)
330
+ }
331
+ if (isTm && doc.defaultWriteTmId === libraryId) doc.defaultWriteTmId = null
332
+ }
333
+
178
334
  this.touch()
179
335
  this.persist()
180
336
  return { deleted: libraryId, mounts: this.mountsView(), libraries: this.librariesView() }
@@ -6,7 +6,7 @@
6
6
 
7
7
  import { escapeXml } from './xml.js'
8
8
 
9
- const CSV_COLUMNS = ['source', 'target', 'note', 'sourceLang', 'caseSensitive']
9
+ const CSV_COLUMNS = ['source', 'target', 'note', 'sourceLang', 'caseSensitive', 'forbidden', 'preferred']
10
10
 
11
11
  function csvCell(value) {
12
12
  const text = String(value ?? '')
@@ -23,6 +23,8 @@ export function glossaryToCsv(entries) {
23
23
  csvCell(entry.note || ''),
24
24
  csvCell(entry.sourceLang || ''),
25
25
  entry.caseSensitive ? 'true' : 'false',
26
+ entry.forbidden ? 'true' : 'false',
27
+ entry.preferred ? 'true' : 'false',
26
28
  ].join(','),
27
29
  )
28
30
  }
@@ -37,6 +39,8 @@ export function glossaryToJson(entries) {
37
39
  note: entry.note || '',
38
40
  sourceLang: entry.sourceLang || '',
39
41
  caseSensitive: Boolean(entry.caseSensitive),
42
+ forbidden: Boolean(entry.forbidden),
43
+ preferred: Boolean(entry.preferred),
40
44
  })),
41
45
  null,
42
46
  2,
@@ -68,6 +72,8 @@ export function glossaryToTbx(entries, { sourceLang = 'en', targetLang = 'zh-CN'
68
72
  lines.push(` <tig><term>${escapeXml(entry.target)}</term></tig>`)
69
73
  lines.push(' </langSet>')
70
74
  if (entry.note) lines.push(` <descrip type="note">${escapeXml(entry.note)}</descrip>`)
75
+ if (entry.forbidden) lines.push(' <descrip type="status">forbidden</descrip>')
76
+ else if (entry.preferred) lines.push(' <descrip type="status">preferred</descrip>')
71
77
  lines.push(' </termEntry>')
72
78
  }
73
79
  lines.push(' </body>', '</tbx>', '')
package/lib/lock.js ADDED
@@ -0,0 +1,30 @@
1
+ import { now } from './store-utils.js'
2
+
3
+ /**
4
+ * A locked segment is a review boundary: MT, replacement and editing may not
5
+ * change its target until the user unlocks it. The flag lives on the segment
6
+ * so it survives reload and travels with exports that carry segment metadata.
7
+ */
8
+ export const lockMethods = {
9
+ setSegmentLocked({ documentId, index, locked = true } = {}) {
10
+ const doc = this.requireDocument(documentId)
11
+ const segment = this.findSegment(doc, index)
12
+ const next = Boolean(locked)
13
+ if (Boolean(segment.locked) === next) {
14
+ return { documentId: doc.id, index: segment.index, locked: next, unchanged: true }
15
+ }
16
+ const previous = segment.locked
17
+ const previousUpdatedAt = segment.updatedAt
18
+ segment.locked = next
19
+ segment.updatedAt = now()
20
+ this.touch()
21
+ try {
22
+ this.persist()
23
+ } catch (error) {
24
+ segment.locked = previous
25
+ segment.updatedAt = previousUpdatedAt
26
+ throw error
27
+ }
28
+ return { documentId: doc.id, index: segment.index, locked: next }
29
+ },
30
+ }
package/lib/notes.js ADDED
@@ -0,0 +1,144 @@
1
+ import { CatError } from './errors.js'
2
+ import { makeId, now } from './store-utils.js'
3
+
4
+ const MAX_NOTE_CHARS = 2000
5
+ const MAX_NOTES = 50
6
+
7
+ function cleanNoteAuthor(author) {
8
+ const value = String(author ?? 'user').trim().slice(0, 40)
9
+ return value || 'user'
10
+ }
11
+
12
+ /**
13
+ * Notes are review context, not translation state: they never affect TM,
14
+ * confirmation, QA or export. Keeping them as a small array on the segment
15
+ * lets a reviewer leave more than one comment and lets the agent add one
16
+ * without overwriting the human's note.
17
+ */
18
+ export function normalizeNotes(raw) {
19
+ if (!Array.isArray(raw)) return []
20
+ const seen = new Set()
21
+ const notes = []
22
+ for (const item of raw) {
23
+ if (!item || typeof item !== 'object') continue
24
+ const text = String(item.text ?? '').trim()
25
+ if (!text) continue
26
+ let id = typeof item.id === 'string' && /^n_[A-Za-z0-9-]{4,40}$/.test(item.id) ? item.id : makeId('n')
27
+ while (seen.has(id)) id = makeId('n')
28
+ seen.add(id)
29
+ notes.push({
30
+ id,
31
+ text: text.slice(0, MAX_NOTE_CHARS),
32
+ author: cleanNoteAuthor(item.author),
33
+ createdAt: typeof item.createdAt === 'string' ? item.createdAt : now(),
34
+ updatedAt: typeof item.updatedAt === 'string' ? item.updatedAt : now(),
35
+ })
36
+ if (notes.length >= MAX_NOTES) break
37
+ }
38
+ return notes
39
+ }
40
+
41
+ function noteView(note) {
42
+ return { ...note }
43
+ }
44
+
45
+ function notesView(notes) {
46
+ return (Array.isArray(notes) ? notes : []).map(noteView)
47
+ }
48
+
49
+ export const noteMethods = {
50
+ notesForSegment({ documentId, index } = {}) {
51
+ const doc = this.requireDocument(documentId)
52
+ const segment = this.findSegment(doc, index)
53
+ return { documentId: doc.id, index, notes: notesView(segment.notes) }
54
+ },
55
+
56
+ addSegmentNote({ documentId, index, text, author = 'user' } = {}) {
57
+ const doc = this.requireDocument(documentId)
58
+ const segment = this.findSegment(doc, index)
59
+ const value = String(text ?? '').trim()
60
+ if (!value) throw new CatError('EMPTY_NOTE', '备注内容不能为空。')
61
+ if (Array.from(value).length > MAX_NOTE_CHARS) {
62
+ throw new CatError('NOTE_TOO_LONG', `备注不能超过 ${MAX_NOTE_CHARS} 个字符。`)
63
+ }
64
+ const notes = Array.isArray(segment.notes) ? segment.notes : []
65
+ if (notes.length >= MAX_NOTES) {
66
+ throw new CatError('TOO_MANY_NOTES', `每个句段最多 ${MAX_NOTES} 条备注。`)
67
+ }
68
+ const stamp = now()
69
+ const note = {
70
+ id: makeId('n'),
71
+ text: value.slice(0, MAX_NOTE_CHARS),
72
+ author: cleanNoteAuthor(author),
73
+ createdAt: stamp,
74
+ updatedAt: stamp,
75
+ }
76
+ const previous = notes.map((item) => ({ ...item }))
77
+ const previousUpdatedAt = segment.updatedAt
78
+ notes.push(note)
79
+ segment.notes = notes
80
+ segment.updatedAt = stamp
81
+ try {
82
+ this.touch()
83
+ this.persist()
84
+ } catch (error) {
85
+ segment.notes = previous
86
+ segment.updatedAt = previousUpdatedAt
87
+ throw error
88
+ }
89
+ return { note: noteView(note), notes: notesView(notes) }
90
+ },
91
+
92
+ updateSegmentNote({ documentId, index, noteId, text } = {}) {
93
+ const doc = this.requireDocument(documentId)
94
+ const segment = this.findSegment(doc, index)
95
+ const notes = Array.isArray(segment.notes) ? segment.notes : []
96
+ const note = notes.find((item) => item.id === noteId)
97
+ if (!note) throw new CatError('NOTE_NOT_FOUND', '找不到这条备注。')
98
+ const value = String(text ?? '').trim()
99
+ if (!value) throw new CatError('EMPTY_NOTE', '备注内容不能为空。')
100
+ if (Array.from(value).length > MAX_NOTE_CHARS) {
101
+ throw new CatError('NOTE_TOO_LONG', `备注不能超过 ${MAX_NOTE_CHARS} 个字符。`)
102
+ }
103
+ const previous = { text: note.text, updatedAt: note.updatedAt }
104
+ const previousSegmentUpdatedAt = segment.updatedAt
105
+ const stamp = now()
106
+ note.text = value.slice(0, MAX_NOTE_CHARS)
107
+ note.updatedAt = stamp
108
+ segment.updatedAt = stamp
109
+ try {
110
+ this.touch()
111
+ this.persist()
112
+ } catch (error) {
113
+ note.text = previous.text
114
+ note.updatedAt = previous.updatedAt
115
+ segment.updatedAt = previousSegmentUpdatedAt
116
+ throw error
117
+ }
118
+ return { note: noteView(note), notes: notesView(notes) }
119
+ },
120
+
121
+ deleteSegmentNote({ documentId, index, noteId } = {}) {
122
+ const doc = this.requireDocument(documentId)
123
+ const segment = this.findSegment(doc, index)
124
+ const notes = Array.isArray(segment.notes) ? segment.notes : []
125
+ const at = notes.findIndex((item) => item.id === noteId)
126
+ if (at < 0) throw new CatError('NOTE_NOT_FOUND', '找不到这条备注。')
127
+ const previous = notes.slice()
128
+ const previousSegmentUpdatedAt = segment.updatedAt
129
+ notes.splice(at, 1)
130
+ segment.notes = notes
131
+ segment.updatedAt = now()
132
+ try {
133
+ this.touch()
134
+ this.persist()
135
+ } catch (error) {
136
+ segment.notes = previous
137
+ segment.updatedAt = previousSegmentUpdatedAt
138
+ throw error
139
+ }
140
+ return { notes: notesView(notes) }
141
+ },
142
+ }
143
+
144
+ export { MAX_NOTE_CHARS, MAX_NOTES }
@@ -4,7 +4,9 @@ import { join, resolve } from 'node:path'
4
4
  import { CatError } from './errors.js'
5
5
  import { normalizeText } from './similarity.js'
6
6
  import { contextHashOf, normalizeContext } from './context.js'
7
- import { documentFingerprint, DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, DEFAULT_SETTINGS, DEFAULT_TM_ID, DEFAULT_TM_NAME, MAX_SEGMENTS, MAX_STORE_BYTES, SEGMENT_ORIGINS, SEGMENT_STATUSES, STORE_FILENAME, STORE_VERSION, clamp, clampInt, cleanFormatMeta, cleanName, cleanSegmentMeta, normalizeSegmentState, now, restoreLibraries, restoreMountIds, restoreSettings, restoredDate, restoredLang, restoredText } from './store-utils.js'
7
+ import { normalizeBrief } from './brief.js'
8
+ import { normalizeNotes } from './notes.js'
9
+ import { documentFingerprint, DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, DEFAULT_SETTINGS, DEFAULT_TM_ID, DEFAULT_TM_NAME, MAX_SEGMENTS, MAX_STORE_BYTES, SEGMENT_ORIGINS, SEGMENT_STATUSES, STORE_FILENAME, STORE_VERSION, clamp, clampInt, cleanFormatMeta, cleanName, cleanSegmentMeta, makeId, normalizeSegmentState, now, restoreLibraries, restoreMountIds, restoreSettings, restoredDate, restoredLang, restoredText } from './store-utils.js'
8
10
 
9
11
  export const persistenceMethods = {
10
12
  load() {
@@ -61,12 +63,7 @@ export const persistenceMethods = {
61
63
  }
62
64
  // v1 and v2 predate the three-state model: their segments only carried
63
65
  // pending/confirmed, so their target text maps onto `translated`.
64
- if (
65
- payload.version !== 1 &&
66
- payload.version !== 2 &&
67
- payload.version !== 3 &&
68
- payload.version !== STORE_VERSION
69
- ) {
66
+ if (![1, 2, 3, 4, 5, 6, STORE_VERSION].includes(payload.version)) {
70
67
  throw new Error(`不兼容的数据版本: ${String(payload.version ?? 'missing')}`)
71
68
  }
72
69
  if (!Array.isArray(payload.documents) || !Array.isArray(payload.tm) || !Array.isArray(payload.glossary)) {
@@ -84,6 +81,7 @@ export const persistenceMethods = {
84
81
 
85
82
  const sourceLang = restoredLang(rawDoc.sourceLang, 'en')
86
83
  const targetLang = restoredLang(rawDoc.targetLang, 'zh-CN')
84
+ const segmentUids = new Set()
87
85
  const segments = rawDoc.segments.map((rawSegment, index) => {
88
86
  if (!rawSegment || typeof rawSegment !== 'object') {
89
87
  throw new Error(`文档 ${rawDoc.id} 的第 ${index + 1} 个句段不是对象`)
@@ -98,11 +96,18 @@ export const persistenceMethods = {
98
96
  ? 'manual'
99
97
  : null
100
98
  const meta = cleanSegmentMeta(rawSegment.meta)
99
+ let uid =
100
+ typeof rawSegment.uid === 'string' && /^u_[A-Za-z0-9-]{4,40}$/.test(rawSegment.uid)
101
+ ? rawSegment.uid
102
+ : makeId('u')
103
+ while (segmentUids.has(uid)) uid = makeId('u')
104
+ segmentUids.add(uid)
101
105
  const segment = {
102
106
  id:
103
107
  typeof rawSegment.id === 'string' && rawSegment.id
104
108
  ? rawSegment.id
105
109
  : `s${index + 1}`,
110
+ uid,
106
111
  source,
107
112
  target,
108
113
  // normalizeSegmentState re-derives this from target/suggestion; the
@@ -116,6 +121,8 @@ export const persistenceMethods = {
116
121
  ? rawSegment.suggestionReason.slice(0, 500)
117
122
  : '',
118
123
  paragraph: Number.isInteger(rawSegment.paragraph) ? rawSegment.paragraph : index,
124
+ notes: normalizeNotes(rawSegment.notes),
125
+ locked: Boolean(rawSegment.locked),
119
126
  ...(meta ? { meta } : {}),
120
127
  confirmedAt: confirmed ? restoredDate(rawSegment.confirmedAt) : null,
121
128
  updatedAt: restoredDate(rawSegment.updatedAt),
@@ -132,6 +139,16 @@ export const persistenceMethods = {
132
139
  cursor: clampInt(rawDoc.cursor, 0, segments.length - 1, 0),
133
140
  createdAt: restoredDate(rawDoc.createdAt),
134
141
  updatedAt: restoredDate(rawDoc.updatedAt),
142
+ tmIds: Array.isArray(rawDoc.tmIds)
143
+ ? [...new Set(rawDoc.tmIds.filter((id) => typeof id === 'string' && id.trim()).map((id) => id.trim()))]
144
+ : null,
145
+ glossaryIds: Array.isArray(rawDoc.glossaryIds)
146
+ ? [...new Set(rawDoc.glossaryIds.filter((id) => typeof id === 'string' && id.trim()).map((id) => id.trim()))]
147
+ : null,
148
+ defaultWriteTmId:
149
+ typeof rawDoc.defaultWriteTmId === 'string' && rawDoc.defaultWriteTmId.trim()
150
+ ? rawDoc.defaultWriteTmId.trim()
151
+ : null,
135
152
  format: typeof rawDoc.format === 'string' ? rawDoc.format.slice(0, 24) : '',
136
153
  fingerprint:
137
154
  typeof rawDoc.fingerprint === 'string' && rawDoc.fingerprint
@@ -140,6 +157,8 @@ export const persistenceMethods = {
140
157
  }
141
158
  const formatMeta = cleanFormatMeta(rawDoc.formatMeta)
142
159
  if (formatMeta) doc.formatMeta = formatMeta
160
+ const brief = normalizeBrief(rawDoc.brief)
161
+ if (brief) doc.brief = brief
143
162
  const sourceDocx = rawDoc.sourceDocx
144
163
  if (
145
164
  sourceDocx &&
@@ -235,6 +254,8 @@ export const persistenceMethods = {
235
254
  ? restoredLang(rawEntry.sourceLang, '')
236
255
  : '',
237
256
  caseSensitive: Boolean(rawEntry.caseSensitive),
257
+ forbidden: Boolean(rawEntry.forbidden),
258
+ preferred: Boolean(rawEntry.preferred),
238
259
  createdAt: restoredDate(rawEntry.createdAt),
239
260
  updatedAt: restoredDate(rawEntry.updatedAt),
240
261
  })
@@ -278,6 +299,18 @@ export const persistenceMethods = {
278
299
  this.tmLibraries = tmLibraries
279
300
  this.glossaryLibraries = glossaryLibraries
280
301
  this.mounts = { tmIds, glossaryIds, defaultTmId, defaultGlossaryId }
302
+ // Library ids may have been removed while the document was closed. Drop
303
+ // dangling bindings rather than failing the whole restore.
304
+ const knownTm = new Set(tmLibraries.map((library) => library.tmId))
305
+ const knownGlossary = new Set(glossaryLibraries.map((library) => library.glossaryId))
306
+ for (const doc of docs.values()) {
307
+ if (doc.tmIds) doc.tmIds = doc.tmIds.filter((id) => knownTm.has(id))
308
+ if (doc.glossaryIds) doc.glossaryIds = doc.glossaryIds.filter((id) => knownGlossary.has(id))
309
+ const effective = doc.tmIds ?? tmIds
310
+ if (doc.defaultWriteTmId && !effective.includes(doc.defaultWriteTmId)) {
311
+ doc.defaultWriteTmId = null
312
+ }
313
+ }
281
314
  const hasOpenDocIds = Array.isArray(payload.openDocIds)
282
315
  const openDocIds = []
283
316
  for (const id of hasOpenDocIds ? payload.openDocIds : []) {