dsh-cait 1.1.3 → 1.1.7
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 +186 -0
- package/README.ja.md +51 -14
- package/README.md +137 -30
- package/README.zh-CN.md +51 -14
- package/client/components.js +2033 -48
- package/client/entry.js +2 -0
- package/client/helpers.js +12 -0
- package/client/locales.js +658 -8
- package/client/styles.js +111 -4
- package/client.js +2814 -58
- package/lib/aligner.js +6 -6
- package/lib/analysis.js +88 -0
- package/lib/api.js +46 -0
- package/lib/brief.js +275 -0
- package/lib/docx.js +155 -26
- package/lib/formats.js +37 -0
- package/lib/glossary.js +20 -4
- package/lib/import-export.js +212 -13
- package/lib/libraries.js +156 -0
- package/lib/library-export.js +7 -1
- package/lib/localization.js +1098 -0
- package/lib/lock.js +30 -0
- package/lib/notes.js +144 -0
- package/lib/persistence.js +54 -7
- package/lib/pptx.js +33 -1
- package/lib/pre-translate.js +62 -45
- package/lib/propagate.js +3 -0
- package/lib/qa.js +94 -4
- package/lib/replace.js +243 -0
- package/lib/review.js +131 -0
- package/lib/segment-edit.js +199 -0
- package/lib/store-utils.js +33 -6
- package/lib/store.js +507 -47
- package/lib/subtitles.js +213 -0
- package/lib/term-extract.js +105 -3
- package/lib/terms.js +4 -2
- package/lib/tm-maintenance.js +157 -0
- package/lib/tm.js +4 -1
- package/lib/tools.js +294 -0
- package/lib/xlsx.js +176 -66
- package/package.json +1 -1
package/lib/replace.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { CatError } from './errors.js'
|
|
2
|
+
import { cleanText, escapeRegExp, normalizeSegmentState, now } from './store-utils.js'
|
|
3
|
+
|
|
4
|
+
const FIELDS = ['source', 'target']
|
|
5
|
+
const MAX_PREVIEW_MATCHES = 200
|
|
6
|
+
|
|
7
|
+
function requireScope(scope) {
|
|
8
|
+
const value = String(scope || 'target')
|
|
9
|
+
if (!['source', 'target', 'both'].includes(value)) {
|
|
10
|
+
throw new CatError('INVALID_REPLACE_SCOPE', '替换范围必须是 source、target 或 both。')
|
|
11
|
+
}
|
|
12
|
+
return value
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function buildPattern(query, regex, matchCase) {
|
|
16
|
+
const text = String(query ?? '')
|
|
17
|
+
if (!text) throw new CatError('EMPTY_REPLACE_QUERY', '查找内容不能为空。')
|
|
18
|
+
try {
|
|
19
|
+
return new RegExp(regex ? text : escapeRegExp(text), matchCase ? 'g' : 'gi')
|
|
20
|
+
} catch (error) {
|
|
21
|
+
throw new CatError('INVALID_REGEX', `正则表达式无效:${error.message}`)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function countMatches(text, pattern) {
|
|
26
|
+
// matchAll is the only way to count zero-width matches safely; String.match
|
|
27
|
+
// with /g returns an empty array for a zero-width regex.
|
|
28
|
+
let count = 0
|
|
29
|
+
for (const _ of text.matchAll(pattern)) count += 1
|
|
30
|
+
return count
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function snippet(text, limit = 140) {
|
|
34
|
+
const value = String(text ?? '')
|
|
35
|
+
return value.length <= limit ? value : `${value.slice(0, limit)}…`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function replaceField(segment, field, pattern, replacement) {
|
|
39
|
+
const before = String(segment[field] ?? '')
|
|
40
|
+
const count = countMatches(before, pattern)
|
|
41
|
+
if (!count) return { field, count: 0, before, after: before }
|
|
42
|
+
const after = before.replace(pattern, replacement)
|
|
43
|
+
return { field, count, before, after }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Find/replace is deliberately a document operation, not a global TM edit:
|
|
48
|
+
* replacing text never writes or deletes memory entries. Source edits change
|
|
49
|
+
* only the segment source; target edits invalidate a confirmation the same way
|
|
50
|
+
* a manual draft edit does.
|
|
51
|
+
*/
|
|
52
|
+
export const replaceMethods = {
|
|
53
|
+
replaceInDocument({
|
|
54
|
+
documentId,
|
|
55
|
+
query,
|
|
56
|
+
replacement = '',
|
|
57
|
+
scope = 'target',
|
|
58
|
+
matchCase = false,
|
|
59
|
+
regex = false,
|
|
60
|
+
apply = false,
|
|
61
|
+
} = {}) {
|
|
62
|
+
const doc = this.requireDocument(documentId)
|
|
63
|
+
const fields = requireScope(scope) === 'both' ? FIELDS : [requireScope(scope)]
|
|
64
|
+
const pattern = buildPattern(query, regex, matchCase)
|
|
65
|
+
const value = String(replacement ?? '')
|
|
66
|
+
const matches = []
|
|
67
|
+
const changes = []
|
|
68
|
+
let total = 0
|
|
69
|
+
const matchedIndexes = new Set()
|
|
70
|
+
|
|
71
|
+
for (let index = 0; index < doc.segments.length; index += 1) {
|
|
72
|
+
const segment = doc.segments[index]
|
|
73
|
+
// A locked segment is a review boundary: preview and apply both skip it.
|
|
74
|
+
if (segment.locked) continue
|
|
75
|
+
const fieldsForSegment = []
|
|
76
|
+
for (const field of fields) {
|
|
77
|
+
const result = replaceField(segment, field, pattern, value)
|
|
78
|
+
if (!result.count) continue
|
|
79
|
+
total += result.count
|
|
80
|
+
matchedIndexes.add(index)
|
|
81
|
+
fieldsForSegment.push(result)
|
|
82
|
+
if (matches.length < MAX_PREVIEW_MATCHES) {
|
|
83
|
+
matches.push({
|
|
84
|
+
index,
|
|
85
|
+
uid: segment.uid,
|
|
86
|
+
field,
|
|
87
|
+
count: result.count,
|
|
88
|
+
before: snippet(result.before),
|
|
89
|
+
after: snippet(result.after),
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (fieldsForSegment.some((item) => item.after !== item.before)) {
|
|
94
|
+
changes.push({ index, segment, fields: fieldsForSegment })
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!apply) {
|
|
99
|
+
return {
|
|
100
|
+
documentId: doc.id,
|
|
101
|
+
documentName: doc.name,
|
|
102
|
+
query: String(query ?? ''),
|
|
103
|
+
replacement: value,
|
|
104
|
+
scope,
|
|
105
|
+
matchCase: Boolean(matchCase),
|
|
106
|
+
regex: Boolean(regex),
|
|
107
|
+
total,
|
|
108
|
+
changed: changes.length,
|
|
109
|
+
matchedSegments: matchedIndexes.size,
|
|
110
|
+
truncated: matches.length >= MAX_PREVIEW_MATCHES && total > matches.length,
|
|
111
|
+
matches,
|
|
112
|
+
requiresConfirmation: true,
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!changes.length) {
|
|
117
|
+
return { documentId: doc.id, documentName: doc.name, total, applied: 0, changed: 0, undo: [] }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const stamp = now()
|
|
121
|
+
const undo = changes.map(({ index, segment }) => ({
|
|
122
|
+
index,
|
|
123
|
+
uid: segment.uid,
|
|
124
|
+
source: segment.source,
|
|
125
|
+
target: segment.target,
|
|
126
|
+
origin: segment.origin ?? null,
|
|
127
|
+
confirmedAt: segment.confirmedAt ?? null,
|
|
128
|
+
updatedAt: segment.updatedAt,
|
|
129
|
+
}))
|
|
130
|
+
const snapshot = {
|
|
131
|
+
cursor: doc.cursor,
|
|
132
|
+
updatedAt: doc.updatedAt,
|
|
133
|
+
revision: this.revision,
|
|
134
|
+
}
|
|
135
|
+
let applied = 0
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
for (const { index, segment, fields: segmentFields } of changes) {
|
|
139
|
+
for (const item of segmentFields) {
|
|
140
|
+
if (item.after === item.before) continue
|
|
141
|
+
segment[item.field] = item.field === 'target'
|
|
142
|
+
? cleanText(item.after, '译文')
|
|
143
|
+
: cleanText(item.after, '原文')
|
|
144
|
+
applied += item.count
|
|
145
|
+
}
|
|
146
|
+
if (segmentFields.some((item) => item.field === 'target' && item.after !== item.before)) {
|
|
147
|
+
segment.confirmedAt = null
|
|
148
|
+
if (segment.target) segment.origin = 'manual'
|
|
149
|
+
}
|
|
150
|
+
segment.updatedAt = stamp
|
|
151
|
+
normalizeSegmentState(segment)
|
|
152
|
+
}
|
|
153
|
+
doc.updatedAt = stamp
|
|
154
|
+
this.touch()
|
|
155
|
+
this.persist()
|
|
156
|
+
} catch (error) {
|
|
157
|
+
for (const item of undo) {
|
|
158
|
+
const segment = doc.segments[item.index]
|
|
159
|
+
if (!segment) continue
|
|
160
|
+
segment.source = item.source
|
|
161
|
+
segment.target = item.target
|
|
162
|
+
segment.origin = item.origin
|
|
163
|
+
segment.confirmedAt = item.confirmedAt
|
|
164
|
+
segment.updatedAt = item.updatedAt
|
|
165
|
+
normalizeSegmentState(segment)
|
|
166
|
+
}
|
|
167
|
+
doc.cursor = snapshot.cursor
|
|
168
|
+
doc.updatedAt = snapshot.updatedAt
|
|
169
|
+
this.revision = snapshot.revision
|
|
170
|
+
throw error
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
documentId: doc.id,
|
|
175
|
+
documentName: doc.name,
|
|
176
|
+
total,
|
|
177
|
+
applied,
|
|
178
|
+
changed: changes.length,
|
|
179
|
+
undo,
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
undoReplace({ documentId, undo } = {}) {
|
|
184
|
+
const doc = this.requireDocument(documentId)
|
|
185
|
+
const list = Array.isArray(undo) ? undo : []
|
|
186
|
+
if (!list.length) throw new CatError('EMPTY_REPLACE_UNDO', '没有可撤销的替换。')
|
|
187
|
+
const snapshot = {
|
|
188
|
+
cursor: doc.cursor,
|
|
189
|
+
updatedAt: doc.updatedAt,
|
|
190
|
+
revision: this.revision,
|
|
191
|
+
segments: list.map((item) => {
|
|
192
|
+
const segment = this.findSegment(doc, item.index)
|
|
193
|
+
if (item.uid && segment.uid !== item.uid) {
|
|
194
|
+
throw new CatError('REPLACE_UNDO_MISMATCH', '文档已变化,无法按原记录撤销替换。')
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
segment,
|
|
198
|
+
current: {
|
|
199
|
+
source: segment.source,
|
|
200
|
+
target: segment.target,
|
|
201
|
+
origin: segment.origin ?? null,
|
|
202
|
+
confirmedAt: segment.confirmedAt ?? null,
|
|
203
|
+
updatedAt: segment.updatedAt,
|
|
204
|
+
},
|
|
205
|
+
restore: {
|
|
206
|
+
source: item.source,
|
|
207
|
+
target: item.target,
|
|
208
|
+
origin: item.origin ?? null,
|
|
209
|
+
confirmedAt: item.confirmedAt ?? null,
|
|
210
|
+
},
|
|
211
|
+
}
|
|
212
|
+
}),
|
|
213
|
+
}
|
|
214
|
+
const stamp = now()
|
|
215
|
+
try {
|
|
216
|
+
for (const item of snapshot.segments) {
|
|
217
|
+
item.segment.source = String(item.restore.source ?? '')
|
|
218
|
+
item.segment.target = String(item.restore.target ?? '')
|
|
219
|
+
item.segment.origin = item.restore.origin
|
|
220
|
+
item.segment.confirmedAt = item.restore.confirmedAt
|
|
221
|
+
item.segment.updatedAt = stamp
|
|
222
|
+
normalizeSegmentState(item.segment)
|
|
223
|
+
}
|
|
224
|
+
doc.updatedAt = stamp
|
|
225
|
+
this.touch()
|
|
226
|
+
this.persist()
|
|
227
|
+
} catch (error) {
|
|
228
|
+
for (const item of snapshot.segments) {
|
|
229
|
+
item.segment.source = item.current.source
|
|
230
|
+
item.segment.target = item.current.target
|
|
231
|
+
item.segment.origin = item.current.origin
|
|
232
|
+
item.segment.confirmedAt = item.current.confirmedAt
|
|
233
|
+
item.segment.updatedAt = item.current.updatedAt
|
|
234
|
+
normalizeSegmentState(item.segment)
|
|
235
|
+
}
|
|
236
|
+
doc.cursor = snapshot.cursor
|
|
237
|
+
doc.updatedAt = snapshot.updatedAt
|
|
238
|
+
this.revision = snapshot.revision
|
|
239
|
+
throw error
|
|
240
|
+
}
|
|
241
|
+
return { documentId: doc.id, documentName: doc.name, restored: list.length }
|
|
242
|
+
},
|
|
243
|
+
}
|
package/lib/review.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { CatError } from './errors.js'
|
|
2
|
+
import { briefPromptBlock, neighborContext } from './brief.js'
|
|
3
|
+
import { canonicalLanguage, cleanText } from './store-utils.js'
|
|
4
|
+
import { collectStreamText } from './mt.js'
|
|
5
|
+
|
|
6
|
+
const MAX_REVIEW_SEGMENTS = 20
|
|
7
|
+
const MAX_REVIEW_ISSUES = 120
|
|
8
|
+
|
|
9
|
+
function parseIssues(output, from, to) {
|
|
10
|
+
const text = String(output ?? '').trim()
|
|
11
|
+
if (!text) throw new CatError('REVIEW_EMPTY', '模型没有返回审校结果。', 502)
|
|
12
|
+
let parsed
|
|
13
|
+
try {
|
|
14
|
+
parsed = JSON.parse(text.replace(/^```(?:json)?\s*/i, '').replace(/\s*```$/, ''))
|
|
15
|
+
} catch {
|
|
16
|
+
const start = text.indexOf('[')
|
|
17
|
+
const end = text.lastIndexOf(']')
|
|
18
|
+
if (start < 0 || end <= start) {
|
|
19
|
+
throw new CatError('REVIEW_INVALID', '模型返回的审校结果不是有效 JSON。', 502)
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(text.slice(start, end + 1))
|
|
23
|
+
} catch {
|
|
24
|
+
throw new CatError('REVIEW_INVALID', '模型返回的审校结果不是有效 JSON。', 502)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (!Array.isArray(parsed)) throw new CatError('REVIEW_INVALID', '模型返回的审校结果必须是数组。', 502)
|
|
28
|
+
const issues = []
|
|
29
|
+
for (const raw of parsed) {
|
|
30
|
+
const index = Number(raw?.index)
|
|
31
|
+
if (!Number.isInteger(index) || index < from || index >= to) continue
|
|
32
|
+
const severity = ['high', 'medium', 'low'].includes(raw?.severity) ? raw.severity : 'medium'
|
|
33
|
+
const message = String(raw?.message ?? '').trim()
|
|
34
|
+
if (!message) continue
|
|
35
|
+
issues.push({
|
|
36
|
+
index,
|
|
37
|
+
severity,
|
|
38
|
+
code: String(raw?.code ?? 'REVIEW').trim().slice(0, 40) || 'REVIEW',
|
|
39
|
+
message: message.slice(0, 1000),
|
|
40
|
+
suggestion: String(raw?.suggestion ?? '').trim().slice(0, 2000),
|
|
41
|
+
})
|
|
42
|
+
if (issues.length >= MAX_REVIEW_ISSUES) break
|
|
43
|
+
}
|
|
44
|
+
return issues
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const reviewMethods = {
|
|
48
|
+
async reviewDocument({ documentId, from = 0, count = MAX_REVIEW_SEGMENTS } = {}) {
|
|
49
|
+
const doc = this.requireDocument(documentId)
|
|
50
|
+
const start = Math.max(0, Number.isInteger(from) ? from : 0)
|
|
51
|
+
const size = Math.max(1, Math.min(MAX_REVIEW_SEGMENTS, Number(count) || MAX_REVIEW_SEGMENTS))
|
|
52
|
+
const end = Math.min(doc.segments.length, start + size)
|
|
53
|
+
if (start >= doc.segments.length) {
|
|
54
|
+
throw new CatError('REVIEW_RANGE_INVALID', '审校范围超出文档。', 400)
|
|
55
|
+
}
|
|
56
|
+
const route = await this.resolveReviewRoute()
|
|
57
|
+
if (route.mock) {
|
|
58
|
+
throw new CatError(
|
|
59
|
+
'REVIEW_UNAVAILABLE',
|
|
60
|
+
'AI 审校需要可用的 DSH 模型路由;当前只有离线术语替换。',
|
|
61
|
+
409,
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
const items = doc.segments.slice(start, end).map((segment, offset) => ({
|
|
65
|
+
index: start + offset,
|
|
66
|
+
segment,
|
|
67
|
+
}))
|
|
68
|
+
const src = canonicalLanguage(doc.sourceLang)
|
|
69
|
+
const tgt = canonicalLanguage(doc.targetLang)
|
|
70
|
+
const sections = items
|
|
71
|
+
.map(({ index, segment }) => {
|
|
72
|
+
const before = neighborContext(doc, index, { before: 1, after: 0 }).before
|
|
73
|
+
const after = neighborContext(doc, index, { before: 0, after: 1 }).after
|
|
74
|
+
const context = [
|
|
75
|
+
...before.map((item) => `previous: ${String(item.source).slice(0, 400)}`),
|
|
76
|
+
...after.map((item) => `next: ${String(item.source).slice(0, 400)}`),
|
|
77
|
+
]
|
|
78
|
+
return [
|
|
79
|
+
`[S${index}]`,
|
|
80
|
+
...context,
|
|
81
|
+
`source: ${cleanText(segment.source, '原文').slice(0, 4000)}`,
|
|
82
|
+
`target: ${cleanText(segment.target, '译文').slice(0, 4000) || '(empty)'}`,
|
|
83
|
+
].join('\n')
|
|
84
|
+
})
|
|
85
|
+
.join('\n\n')
|
|
86
|
+
const prompt = [
|
|
87
|
+
`Review ${items.length} translated segment(s) from ${src} to ${tgt}.`,
|
|
88
|
+
'Return a JSON array only, one object per real issue: [{"index":12,"severity":"high|medium|low","code":"MISTRANSLATION|TERM|OMISSION|ADDITION|FLUENCY|STYLE","message":"...","suggestion":"..."}].',
|
|
89
|
+
'Only report actual problems. Empty target is an issue. Do not rewrite correct segments; return [] when nothing is wrong.',
|
|
90
|
+
briefPromptBlock(doc.brief),
|
|
91
|
+
`Segments:\n${sections}`,
|
|
92
|
+
]
|
|
93
|
+
.filter((part) => String(part ?? '').trim())
|
|
94
|
+
.join('\n\n')
|
|
95
|
+
const systemText =
|
|
96
|
+
'You are a bilingual translation reviewer. Judge meaning, terminology, omissions, additions and fluency against the source and the document brief. Return only the requested JSON array.'
|
|
97
|
+
const started = Date.now()
|
|
98
|
+
let output
|
|
99
|
+
try {
|
|
100
|
+
const stream = await this.mtRuntime.stream({
|
|
101
|
+
provider: route.provider,
|
|
102
|
+
model: route.model,
|
|
103
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }],
|
|
104
|
+
system: systemText,
|
|
105
|
+
temperature: 0,
|
|
106
|
+
purpose: 'cait-review',
|
|
107
|
+
signal: AbortSignal.timeout(60000),
|
|
108
|
+
})
|
|
109
|
+
output = await collectStreamText(stream)
|
|
110
|
+
} catch (error) {
|
|
111
|
+
this.logger?.warn?.(
|
|
112
|
+
`CAIT: AI review failed provider=${route.provider} model=${route.model} code=${
|
|
113
|
+
error?.code || 'UNKNOWN'
|
|
114
|
+
}: ${error?.message || error}`,
|
|
115
|
+
)
|
|
116
|
+
throw new CatError('REVIEW_PROVIDER_FAILED', 'AI 审校调用失败,请检查 DSH 模型路由配置。', 502)
|
|
117
|
+
}
|
|
118
|
+
const issues = parseIssues(output, start, end)
|
|
119
|
+
return {
|
|
120
|
+
documentId: doc.id,
|
|
121
|
+
documentName: doc.name,
|
|
122
|
+
from: start,
|
|
123
|
+
to: end,
|
|
124
|
+
issues,
|
|
125
|
+
provider: route.provider,
|
|
126
|
+
model: route.model,
|
|
127
|
+
inherited: Boolean(route.inherited),
|
|
128
|
+
latencyMs: Date.now() - started,
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { CatError } from './errors.js'
|
|
2
|
+
import { makeId, normalizeSegmentState, now } from './store-utils.js'
|
|
3
|
+
|
|
4
|
+
// Merging and splitting rewrite the segment list. Office formats carry a source
|
|
5
|
+
// package whose paragraph/cell mapping cannot be rebuilt from segment text, so
|
|
6
|
+
// the first version only offers this for plain text documents. Other formats
|
|
7
|
+
// stay import/export only until their mapping layer grows a segment identity.
|
|
8
|
+
const TEXT_EDIT_FORMATS = new Set(['txt', 'md', 'markdown'])
|
|
9
|
+
|
|
10
|
+
function editError(code, message) {
|
|
11
|
+
return new CatError(code, message, 400)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function editableDocument(store, documentId) {
|
|
15
|
+
const doc = store.requireDocument(documentId)
|
|
16
|
+
if (!TEXT_EDIT_FORMATS.has(String(doc.format || 'txt').toLowerCase())) {
|
|
17
|
+
throw editError(
|
|
18
|
+
'SEGMENT_EDIT_FORMAT_UNSUPPORTED',
|
|
19
|
+
'当前格式不支持合并或拆分句段;请先在 TXT / Markdown 文档中操作。',
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
return doc
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function renumber(segments) {
|
|
26
|
+
segments.forEach((segment, index) => {
|
|
27
|
+
segment.id = `s${index + 1}`
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function snapshotDoc(doc) {
|
|
32
|
+
return {
|
|
33
|
+
segments: doc.segments.map((segment) => ({
|
|
34
|
+
...segment,
|
|
35
|
+
notes: Array.isArray(segment.notes) ? segment.notes.map((note) => ({ ...note })) : [],
|
|
36
|
+
meta: segment.meta ? { ...segment.meta } : segment.meta,
|
|
37
|
+
})),
|
|
38
|
+
cursor: doc.cursor,
|
|
39
|
+
updatedAt: doc.updatedAt,
|
|
40
|
+
revision: doc.revision,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function restoreDoc(doc, snapshot) {
|
|
45
|
+
doc.segments = snapshot.segments
|
|
46
|
+
doc.cursor = snapshot.cursor
|
|
47
|
+
doc.updatedAt = snapshot.updatedAt
|
|
48
|
+
doc.revision = snapshot.revision
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function targetStatus(target) {
|
|
52
|
+
return String(target || '').trim() ? 'translated' : 'pending'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const segmentEditMethods = {
|
|
56
|
+
mergeSegments({ documentId, index, count = 2 } = {}) {
|
|
57
|
+
const doc = editableDocument(this, documentId)
|
|
58
|
+
const start = Number(index)
|
|
59
|
+
const size = Number(count)
|
|
60
|
+
if (!Number.isInteger(start) || start < 0 || start >= doc.segments.length - 1) {
|
|
61
|
+
throw editError('MERGE_RANGE_INVALID', '合并范围无效:至少需要当前句段和下一句段。')
|
|
62
|
+
}
|
|
63
|
+
if (!Number.isInteger(size) || size < 2 || start + size > doc.segments.length) {
|
|
64
|
+
throw editError('MERGE_RANGE_INVALID', '合并范围无效:句段数量不足。')
|
|
65
|
+
}
|
|
66
|
+
const group = doc.segments.slice(start, start + size)
|
|
67
|
+
if (group.some((segment) => segment.locked)) {
|
|
68
|
+
throw editError('SEGMENT_LOCKED', '锁定的句段不能合并。')
|
|
69
|
+
}
|
|
70
|
+
const paragraph = group[0].paragraph
|
|
71
|
+
if (group.some((segment) => segment.paragraph !== paragraph)) {
|
|
72
|
+
throw editError('MERGE_PARAGRAPH_MISMATCH', '不能跨段落合并句段。')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const before = snapshotDoc(doc)
|
|
76
|
+
try {
|
|
77
|
+
const target = group
|
|
78
|
+
.map((segment) => String(segment.target || '').trim())
|
|
79
|
+
.filter(Boolean)
|
|
80
|
+
.join(' ')
|
|
81
|
+
const firstOrigin = group.find((segment) => segment.origin)?.origin || (target ? 'mt' : null)
|
|
82
|
+
const origin = target
|
|
83
|
+
? group.some((segment) => segment.origin === 'manual')
|
|
84
|
+
? 'manual'
|
|
85
|
+
: firstOrigin
|
|
86
|
+
: null
|
|
87
|
+
const source = group.map((segment) => String(segment.source || '').trim()).join(' ')
|
|
88
|
+
const notes = group.flatMap((segment) =>
|
|
89
|
+
Array.isArray(segment.notes) ? segment.notes.map((note) => ({ ...note })) : [],
|
|
90
|
+
)
|
|
91
|
+
const merged = {
|
|
92
|
+
...group[0],
|
|
93
|
+
id: group[0].id,
|
|
94
|
+
uid: makeId('u'),
|
|
95
|
+
source,
|
|
96
|
+
target,
|
|
97
|
+
status: targetStatus(target),
|
|
98
|
+
origin,
|
|
99
|
+
suggestion: '',
|
|
100
|
+
suggestionReason: '',
|
|
101
|
+
notes,
|
|
102
|
+
locked: false,
|
|
103
|
+
confirmedAt: null,
|
|
104
|
+
updatedAt: now(),
|
|
105
|
+
meta: {
|
|
106
|
+
...(group[0].meta || {}),
|
|
107
|
+
mergedFrom: group.map((segment) => segment.uid).filter(Boolean),
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
normalizeSegmentState(merged)
|
|
111
|
+
doc.segments.splice(start, size, merged)
|
|
112
|
+
renumber(doc.segments)
|
|
113
|
+
doc.cursor = start
|
|
114
|
+
this.touch()
|
|
115
|
+
this.persist()
|
|
116
|
+
return {
|
|
117
|
+
index: start,
|
|
118
|
+
segment: this.segmentView(merged, start),
|
|
119
|
+
mergedFrom: merged.meta.mergedFrom,
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
restoreDoc(doc, before)
|
|
123
|
+
throw error
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
splitSegment({ documentId, index, at } = {}) {
|
|
128
|
+
const doc = editableDocument(this, documentId)
|
|
129
|
+
const position = Number(index)
|
|
130
|
+
if (!Number.isInteger(position) || position < 0 || position >= doc.segments.length) {
|
|
131
|
+
throw editError('SPLIT_RANGE_INVALID', '拆分位置无效。')
|
|
132
|
+
}
|
|
133
|
+
const segment = doc.segments[position]
|
|
134
|
+
if (segment.locked) throw editError('SEGMENT_LOCKED', '锁定的句段不能拆分。')
|
|
135
|
+
const source = String(segment.source || '')
|
|
136
|
+
const offset = Number(at)
|
|
137
|
+
if (!Number.isInteger(offset) || offset <= 0 || offset >= source.length) {
|
|
138
|
+
throw editError('SPLIT_RANGE_INVALID', '拆分位置必须落在源文中间。')
|
|
139
|
+
}
|
|
140
|
+
const leftSource = source.slice(0, offset).trim()
|
|
141
|
+
const rightSource = source.slice(offset).trim()
|
|
142
|
+
if (!leftSource || !rightSource) {
|
|
143
|
+
throw editError('SPLIT_RANGE_INVALID', '拆分后两侧都必须有内容。')
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const target = String(segment.target || '')
|
|
147
|
+
let targetOffset = 0
|
|
148
|
+
if (target) {
|
|
149
|
+
targetOffset = Math.round(target.length * (offset / source.length))
|
|
150
|
+
targetOffset = Math.max(0, Math.min(target.length, targetOffset))
|
|
151
|
+
}
|
|
152
|
+
const leftTarget = target.slice(0, targetOffset).trim()
|
|
153
|
+
const rightTarget = target.slice(targetOffset).trim()
|
|
154
|
+
const notes = Array.isArray(segment.notes) ? segment.notes.map((note) => ({ ...note })) : []
|
|
155
|
+
const parentUid = segment.uid
|
|
156
|
+
const makePart = (partSource, partTarget, withNotes) => {
|
|
157
|
+
const part = {
|
|
158
|
+
...segment,
|
|
159
|
+
id: '',
|
|
160
|
+
uid: makeId('u'),
|
|
161
|
+
source: partSource,
|
|
162
|
+
target: partTarget,
|
|
163
|
+
status: targetStatus(partTarget),
|
|
164
|
+
origin: partTarget ? 'manual' : null,
|
|
165
|
+
suggestion: '',
|
|
166
|
+
suggestionReason: '',
|
|
167
|
+
notes: withNotes ? notes : [],
|
|
168
|
+
locked: false,
|
|
169
|
+
confirmedAt: null,
|
|
170
|
+
updatedAt: now(),
|
|
171
|
+
meta: {
|
|
172
|
+
...(segment.meta || {}),
|
|
173
|
+
splitFrom: parentUid,
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
normalizeSegmentState(part)
|
|
177
|
+
return part
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const before = snapshotDoc(doc)
|
|
181
|
+
try {
|
|
182
|
+
const left = makePart(leftSource, leftTarget, true)
|
|
183
|
+
const right = makePart(rightSource, rightTarget, false)
|
|
184
|
+
doc.segments.splice(position, 1, left, right)
|
|
185
|
+
renumber(doc.segments)
|
|
186
|
+
doc.cursor = position
|
|
187
|
+
this.touch()
|
|
188
|
+
this.persist()
|
|
189
|
+
return {
|
|
190
|
+
index: position,
|
|
191
|
+
segments: [this.segmentView(left, position), this.segmentView(right, position + 1)],
|
|
192
|
+
splitFrom: parentUid,
|
|
193
|
+
}
|
|
194
|
+
} catch (error) {
|
|
195
|
+
restoreDoc(doc, before)
|
|
196
|
+
throw error
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
}
|
package/lib/store-utils.js
CHANGED
|
@@ -6,12 +6,13 @@ import { DEFAULT_PREPROCESS, normalizePreprocessOptions } from './preprocess.js'
|
|
|
6
6
|
export { clamp, clampInt }
|
|
7
7
|
|
|
8
8
|
export const MAX_SEGMENTS = 20000
|
|
9
|
-
export const CAIT_VERSION = '1.1.
|
|
9
|
+
export const CAIT_VERSION = '1.1.7'
|
|
10
10
|
export const MAX_SEGMENT_CHARS = 20000
|
|
11
11
|
export const MAX_IMPORT_CHARS = 8_000_000
|
|
12
12
|
export const MAX_DOCX_BASE64 = 28_000_000
|
|
13
13
|
export const MAX_STORE_BYTES = 64 * 1024 * 1024
|
|
14
|
-
export const STORE_VERSION =
|
|
14
|
+
export const STORE_VERSION = 7
|
|
15
|
+
export const MT_BATCH_MAX = 8
|
|
15
16
|
export const STORE_FILENAME = 'store.json'
|
|
16
17
|
export const DEFAULT_TM_ID = 'tm_default'
|
|
17
18
|
export const DEFAULT_GLOSSARY_ID = 'glossary_default'
|
|
@@ -54,10 +55,15 @@ export function normalizeSegmentState(segment) {
|
|
|
54
55
|
// UI languages the workbench can switch between; 'auto' follows the DSH locale.
|
|
55
56
|
export const UI_LANG_CODES = ['auto', 'zh', 'en', 'ja']
|
|
56
57
|
|
|
58
|
+
// Below ~70% a fuzzy match usually shares little more than function words, so
|
|
59
|
+
// showing it costs review time without saving any. Mainstream CAT tools sit in
|
|
60
|
+
// the 70–75% range for the same reason.
|
|
61
|
+
export const DEFAULT_FUZZY_THRESHOLD = 0.7
|
|
62
|
+
|
|
57
63
|
export const DEFAULT_SETTINGS = {
|
|
58
64
|
sourceLang: 'en',
|
|
59
65
|
targetLang: 'zh-CN',
|
|
60
|
-
fuzzyThreshold:
|
|
66
|
+
fuzzyThreshold: DEFAULT_FUZZY_THRESHOLD,
|
|
61
67
|
tmLimit: 5,
|
|
62
68
|
uiLang: 'auto',
|
|
63
69
|
preprocess: { ...DEFAULT_PREPROCESS },
|
|
@@ -66,6 +72,13 @@ export const DEFAULT_SETTINGS = {
|
|
|
66
72
|
model: 'cait-mock',
|
|
67
73
|
temperature: 0.2,
|
|
68
74
|
},
|
|
75
|
+
// Empty provider/model means "inherit the MT route". A separate route lets
|
|
76
|
+
// review use a stronger model without slowing down bulk pre-translation.
|
|
77
|
+
review: {
|
|
78
|
+
provider: '',
|
|
79
|
+
model: '',
|
|
80
|
+
temperature: 0,
|
|
81
|
+
},
|
|
69
82
|
}
|
|
70
83
|
|
|
71
84
|
export function now() {
|
|
@@ -196,11 +209,11 @@ function cleanInlineTokens(value) {
|
|
|
196
209
|
export function cleanSegmentMeta(value) {
|
|
197
210
|
if (!value || typeof value !== 'object' || Array.isArray(value)) return null
|
|
198
211
|
const meta = {}
|
|
199
|
-
for (const key of ['start', 'end', 'xlsxRow', 'pptxParagraph', 'epubBlock']) {
|
|
212
|
+
for (const key of ['start', 'end', 'xlsxRow', 'pptxParagraph', 'epubBlock', 'docxParagraph', 'table', 'row', 'cell', 'sheet']) {
|
|
200
213
|
const number = Number(value[key])
|
|
201
214
|
if (Number.isFinite(number) && number >= 0) meta[key] = Math.round(number)
|
|
202
215
|
}
|
|
203
|
-
for (const key of ['settings', 'identifier', 'context', 'msgidPlural', 'xliffId', 'format', 'jsonPath', 'pptxSlide', 'epubHref']) {
|
|
216
|
+
for (const key of ['settings', 'identifier', 'context', 'msgidPlural', 'xliffId', 'format', 'jsonPath', 'pptxSlide', 'epubHref', 'docxPart', 'keyPath', 'icuType', 'sheetName', 'cell', 'targetCell']) {
|
|
204
217
|
if (typeof value[key] === 'string' && value[key].trim()) {
|
|
205
218
|
meta[key] = value[key].slice(0, 500)
|
|
206
219
|
}
|
|
@@ -225,9 +238,14 @@ export function cleanFormatMeta(value) {
|
|
|
225
238
|
const meta = {}
|
|
226
239
|
const obsolete = cleanStringList(value.poObsolete, 200, 5000)
|
|
227
240
|
if (obsolete) meta.poObsolete = obsolete
|
|
241
|
+
const docxParts = cleanStringList(value.docxParts, 50, 200)
|
|
242
|
+
if (docxParts) meta.docxParts = docxParts
|
|
228
243
|
if (typeof value.jsonTemplate === 'string' && value.jsonTemplate.length <= 8 * 1024 * 1024) {
|
|
229
244
|
meta.jsonTemplate = value.jsonTemplate
|
|
230
245
|
}
|
|
246
|
+
if (typeof value.localizationTemplate === 'string' && value.localizationTemplate.length <= 8 * 1024 * 1024) {
|
|
247
|
+
meta.localizationTemplate = value.localizationTemplate
|
|
248
|
+
}
|
|
231
249
|
return Object.keys(meta).length ? meta : null
|
|
232
250
|
}
|
|
233
251
|
|
|
@@ -256,6 +274,12 @@ export function restoreSettings(raw) {
|
|
|
256
274
|
const temperature = Number(raw.mt.temperature)
|
|
257
275
|
if (Number.isFinite(temperature)) settings.mt.temperature = clamp(temperature, 0, 2)
|
|
258
276
|
}
|
|
277
|
+
if (raw.review && typeof raw.review === 'object' && !Array.isArray(raw.review)) {
|
|
278
|
+
settings.review.provider = String(raw.review.provider ?? '').trim().slice(0, 120)
|
|
279
|
+
settings.review.model = String(raw.review.model ?? '').trim().slice(0, 200)
|
|
280
|
+
const temperature = Number(raw.review.temperature)
|
|
281
|
+
if (Number.isFinite(temperature)) settings.review.temperature = clamp(temperature, 0, 2)
|
|
282
|
+
}
|
|
259
283
|
return settings
|
|
260
284
|
} catch {
|
|
261
285
|
return structuredClone(DEFAULT_SETTINGS)
|
|
@@ -366,15 +390,18 @@ export function parseGlossaryCsv(text) {
|
|
|
366
390
|
|
|
367
391
|
const entries = []
|
|
368
392
|
for (const cells of rows) {
|
|
369
|
-
const [source, target, note, lang, caseFlag] = cells.map((cell) => cell.trim())
|
|
393
|
+
const [source, target, note, lang, caseFlag, forbiddenFlag, preferredFlag] = cells.map((cell) => cell.trim())
|
|
370
394
|
if (!source || !target) continue
|
|
371
395
|
if (/^(source|term|原文|术语)$/i.test(source) && /^(target|translation|译文)$/i.test(target)) continue
|
|
396
|
+
const forbidden = /^(true|1|yes|y)$/i.test(forbiddenFlag || '')
|
|
372
397
|
entries.push({
|
|
373
398
|
source,
|
|
374
399
|
target,
|
|
375
400
|
note: note || '',
|
|
376
401
|
sourceLang: lang || '',
|
|
377
402
|
caseSensitive: /^(true|1|yes|y)$/i.test(caseFlag || ''),
|
|
403
|
+
forbidden,
|
|
404
|
+
preferred: !forbidden && /^(true|1|yes|y)$/i.test(preferredFlag || ''),
|
|
378
405
|
})
|
|
379
406
|
}
|
|
380
407
|
return entries
|