@langlink/dsh-l10n 0.0.0-stage → 0.1.0

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/dist/index.js ADDED
@@ -0,0 +1,3974 @@
1
+ // packages/dsh-l10n/src/tools-phase1.ts
2
+ import { createHash as createHash2 } from "node:crypto";
3
+ import { readFile, stat, writeFile } from "node:fs/promises";
4
+ import { basename, resolve } from "node:path";
5
+ import { defineTool } from "@deepseek-ai/dsh-tools";
6
+
7
+ // packages/l10n-core/src/xml.ts
8
+ import { createHash } from "node:crypto";
9
+ import { Parser } from "htmlparser2";
10
+ import { SaxesParser } from "saxes";
11
+ function localName(name2) {
12
+ const index = name2.lastIndexOf(":");
13
+ return index < 0 ? name2 : name2.slice(index + 1);
14
+ }
15
+ function isElement(node) {
16
+ return node.type === "element";
17
+ }
18
+ function parseXml(xml) {
19
+ const root = {
20
+ type: "element",
21
+ name: "#root",
22
+ local: "#root",
23
+ attribs: {},
24
+ start: 0,
25
+ openEnd: 0,
26
+ innerStart: 0,
27
+ innerEnd: xml.length,
28
+ end: xml.length,
29
+ children: []
30
+ };
31
+ const stack = [root];
32
+ const parser = new Parser(
33
+ {
34
+ onopentag(name2, attribs) {
35
+ const parent = stack[stack.length - 1];
36
+ if (!parent) return;
37
+ const start = parser.startIndex;
38
+ const openEnd = parser.endIndex + 1;
39
+ const node = {
40
+ type: "element",
41
+ name: name2,
42
+ local: localName(name2),
43
+ attribs: { ...attribs },
44
+ start,
45
+ openEnd,
46
+ innerStart: openEnd,
47
+ innerEnd: openEnd,
48
+ end: openEnd,
49
+ children: []
50
+ };
51
+ parent.children.push(node);
52
+ stack.push(node);
53
+ },
54
+ onclosetag() {
55
+ const node = stack.pop();
56
+ if (!node || node === root) return;
57
+ const closeStart = parser.startIndex;
58
+ node.innerEnd = closeStart > node.openEnd ? closeStart : node.openEnd;
59
+ node.end = Math.max(node.openEnd, parser.endIndex + 1);
60
+ },
61
+ ontext(text) {
62
+ const parent = stack[stack.length - 1];
63
+ if (!parent) return;
64
+ parent.children.push({
65
+ type: "text",
66
+ text,
67
+ start: parser.startIndex,
68
+ end: parser.endIndex + 1
69
+ });
70
+ }
71
+ },
72
+ {
73
+ xmlMode: true,
74
+ decodeEntities: false,
75
+ lowerCaseTags: false,
76
+ lowerCaseAttributeNames: false,
77
+ recognizeSelfClosing: true
78
+ }
79
+ );
80
+ parser.write(xml);
81
+ parser.end();
82
+ return root;
83
+ }
84
+ function validateXml(xml) {
85
+ const parser = new SaxesParser({ xmlns: false });
86
+ let error;
87
+ parser.on("error", (value) => {
88
+ error = value;
89
+ });
90
+ try {
91
+ parser.write(xml).close();
92
+ } catch (caught) {
93
+ error = caught instanceof Error ? caught : new Error(String(caught));
94
+ }
95
+ if (error) throw new Error(`Invalid XML: ${error.message}`);
96
+ }
97
+ function elementChildren(node) {
98
+ return node.children.filter(isElement);
99
+ }
100
+ function descendants(node, predicate = () => true) {
101
+ const result = [];
102
+ const walk = (parent) => {
103
+ for (const child of parent.children) {
104
+ if (!isElement(child)) continue;
105
+ if (predicate(child)) result.push(child);
106
+ walk(child);
107
+ }
108
+ };
109
+ walk(node);
110
+ return result;
111
+ }
112
+ function decodeXmlEntities(value) {
113
+ return value.replace(
114
+ /&(?:#x([0-9a-fA-F]+)|#(\d+)|(amp|lt|gt|quot|apos));/g,
115
+ (_match, hex, dec, named) => {
116
+ if (hex !== void 0) return String.fromCodePoint(Number.parseInt(hex, 16));
117
+ if (dec !== void 0) return String.fromCodePoint(Number.parseInt(dec, 10));
118
+ switch (named) {
119
+ case "amp":
120
+ return "&";
121
+ case "lt":
122
+ return "<";
123
+ case "gt":
124
+ return ">";
125
+ case "quot":
126
+ return '"';
127
+ case "apos":
128
+ return "'";
129
+ default:
130
+ return _match;
131
+ }
132
+ }
133
+ );
134
+ }
135
+ function textOf(node) {
136
+ let value = "";
137
+ for (const child of node.children) {
138
+ value += child.type === "text" ? decodeXmlEntities(child.text) : textOf(child);
139
+ }
140
+ return value;
141
+ }
142
+ function elementSignature(node) {
143
+ return descendants(node).map((element) => element.local);
144
+ }
145
+ function collectTextNodes(node) {
146
+ const result = [];
147
+ const walk = (parent) => {
148
+ for (const child of parent.children) {
149
+ if (child.type === "text") result.push(child);
150
+ else walk(child);
151
+ }
152
+ };
153
+ walk(node);
154
+ return result;
155
+ }
156
+ function parseFragment(content) {
157
+ validateXml(`<fragment>${content}</fragment>`);
158
+ const root = parseXml(`<fragment>${content}</fragment>`);
159
+ const fragment = elementChildren(root)[0];
160
+ if (!fragment) throw new Error("Fragment root missing");
161
+ return fragment;
162
+ }
163
+ function fragmentSignature(content) {
164
+ return elementSignature(parseFragment(content));
165
+ }
166
+ function structureSummary(xml) {
167
+ const root = parseXml(xml);
168
+ const elements = [];
169
+ const walk = (node, path) => {
170
+ let index = 0;
171
+ for (const child of node.children) {
172
+ if (!isElement(child)) continue;
173
+ const childPath = `${path}/${child.local}[${index}]`;
174
+ const attrs = Object.fromEntries(
175
+ Object.entries(child.attribs).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
176
+ );
177
+ elements.push({ path: childPath, name: child.local, attrs });
178
+ walk(child, childPath);
179
+ index += 1;
180
+ }
181
+ };
182
+ walk(root, "");
183
+ const serialized = JSON.stringify(elements);
184
+ return {
185
+ elements,
186
+ count: elements.length,
187
+ hash: createHash("sha256").update(serialized).digest("hex")
188
+ };
189
+ }
190
+
191
+ // packages/l10n-core/src/formats/xliff12.ts
192
+ var SDL_DATATYPE = "x-sdlfilterframework2";
193
+ function attr(node, name2) {
194
+ return node.attribs[name2];
195
+ }
196
+ function isSegMrk(node) {
197
+ return node.local === "mrk" && node.attribs.mtype === "seg";
198
+ }
199
+ function sdlElement(segDefs) {
200
+ if (!segDefs) return void 0;
201
+ return elementChildren(segDefs).find((child) => child.local === "seg");
202
+ }
203
+ function sdlState(segDefs) {
204
+ const seg = sdlElement(segDefs);
205
+ if (!seg) return void 0;
206
+ const state = {};
207
+ const mapping = [
208
+ ["id", "id"],
209
+ ["conf", "conf"],
210
+ ["origin", "origin"],
211
+ ["percent", "percent"],
212
+ ["textMatch", "text-match"],
213
+ ["locked", "locked"]
214
+ ];
215
+ for (const [key, source] of mapping) {
216
+ const value = seg.attribs[source];
217
+ if (value !== void 0) state[key] = value;
218
+ }
219
+ return Object.keys(state).length > 0 ? state : void 0;
220
+ }
221
+ function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
222
+ const sourceTags = sourceContainer ? elementSignature(sourceContainer) : [];
223
+ const targetTags = targetContainer ? elementSignature(targetContainer) : [];
224
+ const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
225
+ if (!targetContainer) warnings.push(`${id}: target missing`);
226
+ if (targetContainer && sourceTags.length > 0 && sourceTags.join("|") !== targetTags.join("|")) {
227
+ warnings.push(`${id}: source/target inline tag signature differs`);
228
+ }
229
+ return {
230
+ id,
231
+ index: segmentIndex,
232
+ location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
233
+ sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
234
+ targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
235
+ sourceText: sourceContainer ? textOf(sourceContainer) : "",
236
+ targetText: targetContainer ? textOf(targetContainer) : "",
237
+ sourceTags,
238
+ targetTags,
239
+ targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
240
+ ...targetContainer === void 0 ? {} : { targetNode: targetContainer },
241
+ ...sdlNode === void 0 ? {} : { sdlNode },
242
+ ...sdl === void 0 ? {} : { sdl }
243
+ };
244
+ }
245
+ function parseXliff(xml, options = {}) {
246
+ validateXml(xml);
247
+ const root = elementChildren(parseXml(xml))[0];
248
+ if (!root) throw new Error("No root element");
249
+ if (root.local !== "xliff") throw new Error(`Not an XLIFF document: <${root.name}>`);
250
+ const version = attr(root, "version") ?? "";
251
+ const isXliff2 = version === "2.0" || version === "2.1";
252
+ if (version !== "1.2" && !isXliff2) {
253
+ throw new Error(
254
+ `Unsupported XLIFF version: ${version || "(missing)"}; supported: 1.2 (SDLXLIFF/MQXLIFF), 2.0 and 2.1`
255
+ );
256
+ }
257
+ const files = [];
258
+ const segments = [];
259
+ const warnings = [];
260
+ const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
261
+ const allElements = descendants(root);
262
+ const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
263
+ const fileNodes = allElements.filter((element) => element.local === "file");
264
+ const filename = options.filename?.toLowerCase() ?? "";
265
+ let format = version === "2.1" ? "xliff21" : version === "2.0" ? "xliff20" : filename.endsWith(".sdlxliff") ? "sdlxliff" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdlElement ? "sdlxliff" : "xliff12";
266
+ fileNodes.forEach((fileNode, fileIndex) => {
267
+ const datatype = attr(fileNode, "datatype");
268
+ if (datatype === SDL_DATATYPE) format = "sdlxliff";
269
+ const file = {
270
+ index: fileIndex,
271
+ sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(fileNode, "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
272
+ targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(fileNode, "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
273
+ ...datatype === void 0 ? {} : { datatype }
274
+ };
275
+ files.push(file);
276
+ const units = descendants(
277
+ fileNode,
278
+ (candidate) => candidate.local === "trans-unit" || candidate.local === "unit"
279
+ );
280
+ units.forEach((unit, unitIndex) => {
281
+ const unitId = attr(unit, "id") ?? `unit-${unitIndex}`;
282
+ if (isXliff2) {
283
+ const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
284
+ if (segmentNodes.length === 0) {
285
+ warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
286
+ return;
287
+ }
288
+ segmentNodes.forEach((segmentNode, segmentIndex) => {
289
+ const segmentChildren = elementChildren(segmentNode);
290
+ const source = segmentChildren.find((child) => child.local === "source");
291
+ const target = segmentChildren.find((child) => child.local === "target");
292
+ const segmentId = attr(segmentNode, "id") ?? `seg-${segmentIndex}`;
293
+ segments.push(
294
+ makeSegment(
295
+ fileIndex,
296
+ unitId,
297
+ segmentIndex,
298
+ source,
299
+ target,
300
+ segmentId,
301
+ void 0,
302
+ void 0,
303
+ xml,
304
+ warnings
305
+ )
306
+ );
307
+ });
308
+ return;
309
+ }
310
+ const children = elementChildren(unit);
311
+ const sourceElement = children.find((child) => child.local === "source");
312
+ const targetElement = children.find((child) => child.local === "target");
313
+ const segSourceElement = children.find((child) => child.local === "seg-source");
314
+ const segDefsElement = children.find((child) => child.local === "seg-defs");
315
+ const sdl = sdlState(segDefsElement);
316
+ const sdlNode = sdlElement(segDefsElement);
317
+ if (segSourceElement) {
318
+ const sourceMrks = descendants(segSourceElement, isSegMrk);
319
+ const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
320
+ sourceMrks.forEach((sourceMrk, segmentIndex) => {
321
+ const mid = attr(sourceMrk, "mid");
322
+ const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
323
+ if (targetMrk === void 0 && targetMrks.length > 0) {
324
+ warnings.push(`${fileIndex}:${unitId}:${mid ?? segmentIndex}: target mrk missing`);
325
+ }
326
+ segments.push(
327
+ makeSegment(
328
+ fileIndex,
329
+ unitId,
330
+ segmentIndex,
331
+ sourceMrk,
332
+ targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
333
+ mid,
334
+ sdl,
335
+ sdlNode,
336
+ xml,
337
+ warnings
338
+ )
339
+ );
340
+ });
341
+ return;
342
+ }
343
+ if (sourceElement || targetElement) {
344
+ segments.push(
345
+ makeSegment(fileIndex, unitId, 0, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
346
+ );
347
+ } else {
348
+ warnings.push(`${fileIndex}:${unitId}: no source/target`);
349
+ }
350
+ });
351
+ });
352
+ if (hasSdlNamespace || hasSdlElement) format = "sdlxliff";
353
+ return { format, version, files, segments, warnings, xml };
354
+ }
355
+
356
+ // packages/l10n-core/src/formats/patch.ts
357
+ function escapeXmlText(value) {
358
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
359
+ }
360
+ function resolveContent(segment, edit) {
361
+ const hasInlineTags = segment.targetTags.length > 0;
362
+ if (hasInlineTags) {
363
+ if (!edit.xml) {
364
+ throw new Error("target contains inline tags; provide a validated XML fragment with xml=true");
365
+ }
366
+ const signature = fragmentSignature(edit.content);
367
+ if (signature.join("|") !== segment.targetTags.join("|")) {
368
+ throw new Error(
369
+ `inline tag signature mismatch: expected [${segment.targetTags.join(", ")}], got [${signature.join(", ")}]`
370
+ );
371
+ }
372
+ return edit.content;
373
+ }
374
+ if (edit.xml) {
375
+ const signature = fragmentSignature(edit.content);
376
+ if (signature.length > 0) {
377
+ throw new Error("target has no inline tags; XML fragment must not add tags");
378
+ }
379
+ return edit.content;
380
+ }
381
+ return escapeXmlText(edit.content);
382
+ }
383
+ function patchDocument(document, edits) {
384
+ const errors = [];
385
+ const prepared = [];
386
+ const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
387
+ for (const [segmentId, edit] of edits) {
388
+ const segment = byId.get(segmentId);
389
+ if (!segment) {
390
+ errors.push({ segmentId, message: "unknown segment" });
391
+ continue;
392
+ }
393
+ if (!segment.targetRange) {
394
+ errors.push({ segmentId, message: "target missing; cannot patch" });
395
+ continue;
396
+ }
397
+ try {
398
+ prepared.push({
399
+ start: segment.targetRange.start,
400
+ end: segment.targetRange.end,
401
+ content: resolveContent(segment, edit)
402
+ });
403
+ } catch (caught) {
404
+ errors.push({
405
+ segmentId,
406
+ message: caught instanceof Error ? caught.message : String(caught)
407
+ });
408
+ }
409
+ }
410
+ if (errors.length > 0) {
411
+ return { xml: document.xml, applied: 0, errors };
412
+ }
413
+ prepared.sort((left, right) => right.start - left.start);
414
+ let output = document.xml;
415
+ let lastStart = Number.POSITIVE_INFINITY;
416
+ for (const patch of prepared) {
417
+ if (patch.end > lastStart) {
418
+ return {
419
+ xml: document.xml,
420
+ applied: 0,
421
+ errors: [{ segmentId: "overlap", message: "overlapping patch ranges" }]
422
+ };
423
+ }
424
+ if (patch.start < 0 || patch.end > output.length || patch.start > patch.end) {
425
+ return {
426
+ xml: document.xml,
427
+ applied: 0,
428
+ errors: [{ segmentId: "range", message: "patch range out of bounds" }]
429
+ };
430
+ }
431
+ output = output.slice(0, patch.start) + patch.content + output.slice(patch.end);
432
+ lastStart = patch.start;
433
+ }
434
+ return { xml: output, applied: prepared.length, errors: [] };
435
+ }
436
+ function textEdits(document, values) {
437
+ const edits = /* @__PURE__ */ new Map();
438
+ for (const [segmentId, value] of values) {
439
+ const segment = document.segments.find((candidate) => candidate.id === segmentId);
440
+ if (!segment || segment.targetTags.length > 0) continue;
441
+ edits.set(segmentId, { content: value });
442
+ }
443
+ return edits;
444
+ }
445
+ function applyRangePatches(xml, patches) {
446
+ const errors = [];
447
+ const sorted = [...patches].sort((left, right) => right.start - left.start);
448
+ let output = xml;
449
+ let lastStart = Number.POSITIVE_INFINITY;
450
+ for (const patch of sorted) {
451
+ if (!Number.isInteger(patch.start) || !Number.isInteger(patch.end)) {
452
+ errors.push({ id: patch.id ?? "range", message: "range offsets must be integers" });
453
+ continue;
454
+ }
455
+ if (patch.start < 0 || patch.end > output.length || patch.start > patch.end) {
456
+ errors.push({ id: patch.id ?? "range", message: "range out of bounds" });
457
+ continue;
458
+ }
459
+ if (patch.end > lastStart) {
460
+ errors.push({ id: patch.id ?? "overlap", message: "overlapping patch ranges" });
461
+ continue;
462
+ }
463
+ output = output.slice(0, patch.start) + patch.content + output.slice(patch.end);
464
+ lastStart = patch.start;
465
+ }
466
+ if (errors.length > 0) return { xml, applied: 0, errors };
467
+ return { xml: output, applied: sorted.length, errors: [] };
468
+ }
469
+
470
+ // packages/l10n-core/src/formats/sdlxliff.ts
471
+ var ATTRIBUTE_NAMES = [
472
+ ["conf", "conf"],
473
+ ["percent", "percent"],
474
+ ["origin", "origin"],
475
+ ["textMatch", "text-match"],
476
+ ["locked", "locked"]
477
+ ];
478
+ function escapeAttribute(value) {
479
+ return value.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;");
480
+ }
481
+ function setAttribute(tag, name2, rawValue) {
482
+ const value = escapeAttribute(rawValue);
483
+ const expression = new RegExp(`(\\s${name2}\\s*=\\s*)("[^"]*"|'[^']*')`);
484
+ if (expression.test(tag)) {
485
+ return tag.replace(expression, `$1"${value}"`);
486
+ }
487
+ return tag.replace(/\s*(\/?)>$/, (_match, selfClosing) => {
488
+ return ` ${name2}="${value}"${selfClosing === "/" ? "/>" : ">"}`;
489
+ });
490
+ }
491
+ function patchSdlStates(document, updates) {
492
+ const errors = [];
493
+ const patches = [];
494
+ const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
495
+ for (const [segmentId, update] of updates) {
496
+ const segment = byId.get(segmentId);
497
+ if (!segment) {
498
+ errors.push({ segmentId, message: "unknown segment" });
499
+ continue;
500
+ }
501
+ if (!segment.sdlNode) {
502
+ errors.push({ segmentId, message: "sdl:seg node missing" });
503
+ continue;
504
+ }
505
+ const node = segment.sdlNode;
506
+ let tag = document.xml.slice(node.start, node.openEnd);
507
+ for (const [key, attribute] of ATTRIBUTE_NAMES) {
508
+ const value = update[key];
509
+ if (value === void 0) continue;
510
+ tag = setAttribute(tag, attribute, value);
511
+ }
512
+ patches.push({ start: node.start, end: node.openEnd, content: tag, id: segmentId });
513
+ }
514
+ if (errors.length > 0) return { xml: document.xml, applied: 0, errors };
515
+ if (patches.length === 0) return { xml: document.xml, applied: 0, errors: [] };
516
+ const result = applyRangePatches(document.xml, patches);
517
+ if (result.errors.length > 0) {
518
+ return {
519
+ xml: document.xml,
520
+ applied: 0,
521
+ errors: result.errors.map((error) => ({ segmentId: error.id, message: error.message }))
522
+ };
523
+ }
524
+ try {
525
+ validateXml(result.xml);
526
+ } catch (caught) {
527
+ return {
528
+ xml: document.xml,
529
+ applied: 0,
530
+ errors: [
531
+ {
532
+ segmentId: "document",
533
+ message: caught instanceof Error ? caught.message : String(caught)
534
+ }
535
+ ]
536
+ };
537
+ }
538
+ return { xml: result.xml, applied: result.applied, errors: [] };
539
+ }
540
+
541
+ // packages/l10n-core/src/assets/tmx.ts
542
+ var CONTEXT_PRE = "x-context-pre";
543
+ var CONTEXT_NEXT = "x-context-next";
544
+ function normalizeLanguage(language) {
545
+ return language.trim().replace(/_/g, "-").toLowerCase();
546
+ }
547
+ function languageMatches(actual, wanted) {
548
+ const normalizedActual = normalizeLanguage(actual);
549
+ const normalizedWanted = normalizeLanguage(wanted);
550
+ if (normalizedActual === normalizedWanted) return true;
551
+ return normalizedActual.split("-")[0] === normalizedWanted.split("-")[0];
552
+ }
553
+ function findChild(parent, local) {
554
+ return elementChildren(parent).find((child) => child.local === local);
555
+ }
556
+ function findDescendant(parent, local) {
557
+ for (const child of elementChildren(parent)) {
558
+ if (child.local === local) return child;
559
+ const nested = findDescendant(child, local);
560
+ if (nested) return nested;
561
+ }
562
+ return void 0;
563
+ }
564
+ function tuvLanguage(tuv) {
565
+ return tuv.attribs["xml:lang"] ?? tuv.attribs.lang ?? "";
566
+ }
567
+ function findTuv(tuvs, language, excluded) {
568
+ const wanted = normalizeLanguage(language);
569
+ const candidates = tuvs.filter((tuv) => tuv !== excluded);
570
+ const exact = candidates.find((tuv) => normalizeLanguage(tuvLanguage(tuv)) === wanted);
571
+ if (exact) return exact;
572
+ const wantedBase = wanted.split("-")[0];
573
+ return candidates.find((tuv) => normalizeLanguage(tuvLanguage(tuv)).split("-")[0] === wantedBase);
574
+ }
575
+ function tuvText(tuv) {
576
+ const seg = findDescendant(tuv, "seg");
577
+ return seg ? textOf(seg).trim() : void 0;
578
+ }
579
+ function firstLanguage(tus) {
580
+ for (const tu of tus) {
581
+ for (const tuv of elementChildren(tu).filter((child) => child.local === "tuv")) {
582
+ const language = tuvLanguage(tuv);
583
+ if (language !== "") return language;
584
+ }
585
+ }
586
+ return void 0;
587
+ }
588
+ function inferTargetLanguage(tus, sourceLanguage) {
589
+ for (const tu of tus) {
590
+ for (const tuv of elementChildren(tu).filter((child) => child.local === "tuv")) {
591
+ const language = tuvLanguage(tuv);
592
+ if (language !== "" && !languageMatches(language, sourceLanguage)) return language;
593
+ }
594
+ }
595
+ return void 0;
596
+ }
597
+ function readProps(tu) {
598
+ const props = [];
599
+ const walk = (parent) => {
600
+ for (const child of elementChildren(parent)) {
601
+ if (child.local === "prop") props.push(child);
602
+ else walk(child);
603
+ }
604
+ };
605
+ walk(tu);
606
+ return props;
607
+ }
608
+ function parseContext(props) {
609
+ let previousSource;
610
+ let nextSource;
611
+ for (const prop of props) {
612
+ const type = (prop.attribs.type ?? prop.attribs.name ?? "").toLowerCase();
613
+ const value = textOf(prop).trim();
614
+ if (value === "") continue;
615
+ if (type === CONTEXT_PRE) previousSource = value;
616
+ else if (type === CONTEXT_NEXT) nextSource = value;
617
+ }
618
+ if (previousSource === void 0 && nextSource === void 0) return void 0;
619
+ return {
620
+ ...previousSource === void 0 ? {} : { previousSource },
621
+ ...nextSource === void 0 ? {} : { nextSource }
622
+ };
623
+ }
624
+ function readProperties(props) {
625
+ const properties = {};
626
+ let count = 0;
627
+ for (const prop of props) {
628
+ const propType = (prop.attribs.type ?? prop.attribs.name ?? "").toLowerCase();
629
+ if (propType === CONTEXT_PRE || propType === CONTEXT_NEXT) continue;
630
+ const baseKey = (prop.attribs.type ?? prop.attribs.name ?? "").trim() || `prop-${count + 1}`;
631
+ const value = textOf(prop).trim();
632
+ if (value === "") continue;
633
+ let key = baseKey;
634
+ for (let suffix = 2; key in properties; suffix += 1) key = `${baseKey}#${suffix}`;
635
+ properties[key] = value;
636
+ count += 1;
637
+ }
638
+ return count === 0 ? void 0 : properties;
639
+ }
640
+ function parseTmx(xml, options = {}) {
641
+ const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
642
+ validateXml(source);
643
+ const document = parseXml(source);
644
+ const root = elementChildren(document).find((child) => child.local === "tmx");
645
+ if (!root) throw new Error("Not a TMX document: missing <tmx> root element");
646
+ const warnings = [];
647
+ const header = findChild(root, "header");
648
+ const body = findChild(root, "body");
649
+ if (!body) {
650
+ warnings.push("TMX: missing <body> element; no entries parsed");
651
+ return { entries: [], warnings };
652
+ }
653
+ const tus = elementChildren(body).filter((child) => child.local === "tu");
654
+ const sourceLanguage = options.sourceLanguage ?? header?.attribs.srclang ?? firstLanguage(tus);
655
+ if (sourceLanguage === void 0) {
656
+ warnings.push("TMX: source language could not be determined; no entries parsed");
657
+ return { entries: [], warnings };
658
+ }
659
+ const targetLanguage = options.targetLanguage ?? inferTargetLanguage(tus, sourceLanguage);
660
+ if (targetLanguage === void 0) {
661
+ warnings.push("TMX: target language could not be determined; no entries parsed");
662
+ return { entries: [], warnings };
663
+ }
664
+ const entries = [];
665
+ for (const [index, tu] of tus.entries()) {
666
+ const label = tu.attribs.tuid ?? tu.attribs.id ?? `#${index + 1}`;
667
+ const tuvs = elementChildren(tu).filter((child) => child.local === "tuv");
668
+ const sourceTuv = findTuv(tuvs, sourceLanguage);
669
+ const targetTuv = findTuv(tuvs, targetLanguage, sourceTuv);
670
+ if (!sourceTuv) {
671
+ warnings.push(`TMX: tu ${label} has no tuv for source language "${sourceLanguage}"; skipped`);
672
+ continue;
673
+ }
674
+ if (!targetTuv) {
675
+ warnings.push(`TMX: tu ${label} has no tuv for target language "${targetLanguage}"; skipped`);
676
+ continue;
677
+ }
678
+ const sourceText = tuvText(sourceTuv);
679
+ const targetText = tuvText(targetTuv);
680
+ if (!sourceText || !targetText) {
681
+ warnings.push(`TMX: tu ${label} has no <seg> text; skipped`);
682
+ continue;
683
+ }
684
+ const props = readProps(tu);
685
+ const context = parseContext(props);
686
+ const properties = readProperties(props);
687
+ const lastModified = tu.attribs.creationdate ?? tu.attribs.changedate;
688
+ entries.push({
689
+ id: label,
690
+ source: sourceText,
691
+ target: targetText,
692
+ sourceLanguage,
693
+ targetLanguage,
694
+ origin: "tm",
695
+ ...context === void 0 ? {} : { context },
696
+ ...lastModified === void 0 ? {} : { lastModified },
697
+ ...properties === void 0 ? {} : { properties }
698
+ });
699
+ }
700
+ return { entries, warnings };
701
+ }
702
+
703
+ // packages/l10n-core/src/assets/glossary.ts
704
+ var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
705
+ var TARGET_HEADERS = /* @__PURE__ */ new Set(["target", "target term", "translation", "\u8BD1\u6587", "\u76EE\u6807", "\u76EE\u6807\u8BED"]);
706
+ var FORBIDDEN_HEADERS = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
707
+ function stripBom(text) {
708
+ return text.charCodeAt(0) === 65279 ? text.slice(1) : text;
709
+ }
710
+ function detectDelimiter(text) {
711
+ let commas = 0;
712
+ let tabs = 0;
713
+ let inQuotes = false;
714
+ for (let index = 0; index < text.length; index += 1) {
715
+ const char = text[index];
716
+ if (char === '"') {
717
+ if (inQuotes && text[index + 1] === '"') index += 1;
718
+ else inQuotes = !inQuotes;
719
+ } else if (!inQuotes) {
720
+ if (char === "\n") break;
721
+ else if (char === ",") commas += 1;
722
+ else if (char === " ") tabs += 1;
723
+ }
724
+ }
725
+ return tabs > commas ? " " : ",";
726
+ }
727
+ function parseCsvRows(text, delimiter = ",") {
728
+ const rows = [];
729
+ let row = [];
730
+ let field = "";
731
+ let inQuotes = false;
732
+ const pushRow = () => {
733
+ row.push(field);
734
+ field = "";
735
+ if (row.some((cell) => cell.trim() !== "")) rows.push(row);
736
+ row = [];
737
+ };
738
+ for (let index = 0; index < text.length; index += 1) {
739
+ const char = text[index];
740
+ if (inQuotes) {
741
+ if (char === '"') {
742
+ if (text[index + 1] === '"') {
743
+ field += '"';
744
+ index += 1;
745
+ } else {
746
+ inQuotes = false;
747
+ }
748
+ } else if (char === "\r") {
749
+ field += "\n";
750
+ if (text[index + 1] === "\n") index += 1;
751
+ } else {
752
+ field += char;
753
+ }
754
+ continue;
755
+ }
756
+ if (char === '"') {
757
+ inQuotes = true;
758
+ } else if (char === delimiter) {
759
+ row.push(field);
760
+ field = "";
761
+ } else if (char === "\n") {
762
+ pushRow();
763
+ } else if (char === "\r") {
764
+ if (text[index + 1] !== "\n") pushRow();
765
+ } else {
766
+ field += char;
767
+ }
768
+ }
769
+ if (field !== "" || row.length > 0) pushRow();
770
+ return rows;
771
+ }
772
+ function headerIndex(row, aliases) {
773
+ return row.findIndex((cell) => aliases.has(cell.trim().toLowerCase()));
774
+ }
775
+ function isTruthyFlag(value) {
776
+ return /^(?:true|1|yes|y|是|禁用)$/i.test(value.trim());
777
+ }
778
+ function normalizedHeader(row) {
779
+ return row.map((cell) => cell.trim().toLowerCase());
780
+ }
781
+ function parseGlossaryCsv(text, options = {}) {
782
+ const content = stripBom(text);
783
+ const delimiter = detectDelimiter(content);
784
+ const rows = parseCsvRows(content, delimiter);
785
+ const warnings = [];
786
+ if (rows.length === 0) return { entries: [], warnings: ["glossary: no rows found"] };
787
+ let sourceColumn = 0;
788
+ let targetColumn = 1;
789
+ let forbiddenColumn = 2;
790
+ let dataRows = rows;
791
+ const first = rows[0];
792
+ if (first) {
793
+ const header = normalizedHeader(first);
794
+ const sourceIndex = headerIndex(header, SOURCE_HEADERS);
795
+ const targetIndex = headerIndex(header, TARGET_HEADERS);
796
+ if (sourceIndex >= 0 && targetIndex >= 0) {
797
+ sourceColumn = sourceIndex;
798
+ targetColumn = targetIndex;
799
+ const forbiddenIndex = headerIndex(header, FORBIDDEN_HEADERS);
800
+ forbiddenColumn = forbiddenIndex >= 0 ? forbiddenIndex : void 0;
801
+ const known = /* @__PURE__ */ new Set([sourceIndex, targetIndex]);
802
+ if (forbiddenIndex >= 0) known.add(forbiddenIndex);
803
+ header.forEach((cell, index) => {
804
+ if (cell !== "" && !known.has(index)) {
805
+ warnings.push(`glossary: ignored column "${first[index]}"`);
806
+ }
807
+ });
808
+ dataRows = rows.slice(1);
809
+ } else if (sourceIndex >= 0 || targetIndex >= 0) {
810
+ warnings.push("glossary: header is missing a source or target column; using positional columns");
811
+ sourceColumn = sourceIndex >= 0 ? sourceIndex : 0;
812
+ targetColumn = targetIndex >= 0 ? targetIndex : sourceColumn === 0 ? 1 : 0;
813
+ forbiddenColumn = void 0;
814
+ dataRows = rows.slice(1);
815
+ }
816
+ }
817
+ const entries = [];
818
+ dataRows.forEach((row, rowIndex) => {
819
+ const position = rowIndex + 1;
820
+ const source = (row[sourceColumn] ?? "").trim();
821
+ const target = (row[targetColumn] ?? "").trim();
822
+ if (source === "" && target === "") return;
823
+ if (source === "" || target === "") {
824
+ warnings.push(`glossary: row ${position} is missing ${source === "" ? "source" : "target"}; skipped`);
825
+ return;
826
+ }
827
+ const forbiddenCell = forbiddenColumn === void 0 ? "" : row[forbiddenColumn] ?? "";
828
+ const forbidden = isTruthyFlag(forbiddenCell);
829
+ entries.push({
830
+ id: `glossary-${entries.length + 1}`,
831
+ source,
832
+ target,
833
+ ...options.sourceLanguage === void 0 ? {} : { sourceLanguage: options.sourceLanguage },
834
+ ...options.targetLanguage === void 0 ? {} : { targetLanguage: options.targetLanguage },
835
+ ...forbidden ? { forbidden: true } : {}
836
+ });
837
+ });
838
+ return { entries, warnings };
839
+ }
840
+
841
+ // packages/l10n-core/src/assets/tm-matcher.ts
842
+ var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
843
+ var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
844
+ var DOUBLE_QUOTES = /[\u201c\u201d\u201e\u201f\u2033]/gu;
845
+ var DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
846
+ var TERMINAL_PUNCTUATION = /[.!?\u2026;:\u3002\uff01\uff1f\uff1b\uff1a]$/u;
847
+ var CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
848
+ var WORD_CHAR = /[\p{L}\p{N}_]/u;
849
+ var COMBINING_MARK = new RegExp("\\p{M}", "u");
850
+ var TAG_TOKEN = /^<\/?[A-Za-z_][\w.:-]*(?:\s+[^<>]*?)?\s*\/?>/u;
851
+ var PLACEHOLDER_TOKEN = /^(?:\$\{[^{}]*\}|%[0-9]+\$[sdifuxX]|%[-+ 0#]*[0-9]*(?:\.[0-9]+)?[sdifuxXc]|\{[^{}]*\})/u;
852
+ var URL_TOKEN = /^(?:(?:https?|ftp):\/\/|www\.)[^\s<>"'{}]+/u;
853
+ var EMAIL_TOKEN = /^[\w.+-]+@[\w-]+(?:\.[\w-]+)+/u;
854
+ var PATH_TOKEN = /^(?:(?:[A-Za-z]:[\\/]|\\\\|~[\\/])[^\s<>"'{}]+|\/(?:[^\s<>"'{}]*\/)[^\s<>"'{}]*)/u;
855
+ var NUMBER_TOKEN = new RegExp("^\\p{Nd}+(?:[.,]\\p{Nd}+)?", "u");
856
+ var SIMILARITY_TRUNCATE = 200;
857
+ var TRUNCATION_PENALTY = 7;
858
+ var DEFAULT_THRESHOLD = 50;
859
+ var DEFAULT_MAX_MATCHES = 3;
860
+ function normalizeText(text) {
861
+ return text.normalize("NFKC").normalize("NFC").replace(ZERO_WIDTH, "").replace(QUOTES, "'").replace(DOUBLE_QUOTES, '"').replace(DASHES, "-").replace(/\u2026/gu, "...").replace(/\s+/gu, " ").trim().toLowerCase();
862
+ }
863
+ function matchAtomic(rest) {
864
+ const tag = TAG_TOKEN.exec(rest);
865
+ if (tag) return { kind: "tag", raw: tag[0], skeleton: "<TAG>" };
866
+ const placeholder = PLACEHOLDER_TOKEN.exec(rest);
867
+ if (placeholder) return { kind: "placeholder", raw: placeholder[0], skeleton: "<PH>" };
868
+ const url = URL_TOKEN.exec(rest);
869
+ if (url) return { kind: "atomic", raw: url[0], skeleton: url[0] };
870
+ const email = EMAIL_TOKEN.exec(rest);
871
+ if (email) return { kind: "atomic", raw: email[0], skeleton: email[0] };
872
+ const path = PATH_TOKEN.exec(rest);
873
+ if (path) return { kind: "atomic", raw: path[0], skeleton: path[0] };
874
+ return void 0;
875
+ }
876
+ function tokenize(text) {
877
+ const norm = normalizeText(text);
878
+ const tokens = [];
879
+ let index = 0;
880
+ while (index < norm.length) {
881
+ const rest = norm.slice(index);
882
+ const atomic = matchAtomic(rest);
883
+ if (atomic) {
884
+ tokens.push(atomic);
885
+ index += atomic.raw.length;
886
+ continue;
887
+ }
888
+ const char = norm[index];
889
+ if (CJK_CHAR.test(char)) {
890
+ tokens.push({ kind: "cjk", raw: char, skeleton: char });
891
+ index += 1;
892
+ continue;
893
+ }
894
+ const number = NUMBER_TOKEN.exec(rest);
895
+ if (number) {
896
+ tokens.push({ kind: "number", raw: number[0], skeleton: "<NUM>" });
897
+ index += number[0].length;
898
+ continue;
899
+ }
900
+ if (WORD_CHAR.test(char)) {
901
+ let end = index + 1;
902
+ while (end < norm.length) {
903
+ const next = norm[end];
904
+ if (CJK_CHAR.test(next)) break;
905
+ if (WORD_CHAR.test(next) || COMBINING_MARK.test(next)) {
906
+ end += 1;
907
+ continue;
908
+ }
909
+ break;
910
+ }
911
+ const raw = norm.slice(index, end);
912
+ tokens.push({ kind: "word", raw, skeleton: raw });
913
+ index = end;
914
+ continue;
915
+ }
916
+ index += 1;
917
+ }
918
+ return {
919
+ tokens,
920
+ skeleton: tokens.map((token) => token.skeleton),
921
+ numbers: tokens.filter((token) => token.kind === "number").map((token) => token.raw),
922
+ atoms: tokens.filter((token) => token.kind === "tag" || token.kind === "placeholder").map((token) => `${token.kind}:${token.raw}`),
923
+ norm,
924
+ terminalPunct: TERMINAL_PUNCTUATION.test(norm)
925
+ };
926
+ }
927
+ function arraysEqual(a, b) {
928
+ if (a.length !== b.length) return false;
929
+ return a.every((value, index) => value === b[index]);
930
+ }
931
+ function tokensEqual(a, b) {
932
+ if (a.length !== b.length) return false;
933
+ return a.every(
934
+ (token, index) => token.kind === b[index].kind && token.raw === b[index].raw
935
+ );
936
+ }
937
+ function firstDifference(a, b) {
938
+ for (let index = 0; index < Math.min(a.length, b.length); index += 1) {
939
+ if (a[index] !== b[index]) return `${a[index]} vs ${b[index]}`;
940
+ }
941
+ return `${a.length} vs ${b.length}`;
942
+ }
943
+ function compareStructure(source, entry, tmEntry) {
944
+ const skeletonEqual = arraysEqual(source.skeleton, entry.skeleton);
945
+ const numbersEqual = arraysEqual(source.numbers, entry.numbers);
946
+ const atomsEqual = arraysEqual(source.atoms, entry.atoms);
947
+ const numberDiff = !numbersEqual;
948
+ const tagDiff = !atomsEqual;
949
+ const punctDiff = source.terminalPunct !== entry.terminalPunct || tokensEqual(source.tokens, entry.tokens) && source.norm !== entry.norm;
950
+ const penalties = [];
951
+ if (numberDiff) {
952
+ penalties.push({
953
+ kind: "number",
954
+ points: 2,
955
+ detail: `${source.numbers.join(", ") || "none"} vs ${entry.numbers.join(", ") || "none"}`
956
+ });
957
+ }
958
+ if (tagDiff) {
959
+ if (source.atoms.length !== entry.atoms.length) {
960
+ penalties.push({
961
+ kind: "tag_count",
962
+ points: 10,
963
+ detail: `${source.atoms.length} vs ${entry.atoms.length} tags/placeholders`
964
+ });
965
+ } else {
966
+ penalties.push({
967
+ kind: "tag_id",
968
+ points: 3,
969
+ detail: firstDifference(source.atoms, entry.atoms)
970
+ });
971
+ }
972
+ }
973
+ if (punctDiff) {
974
+ penalties.push({
975
+ kind: "punctuation",
976
+ points: 1,
977
+ detail: "difference is limited to punctuation/whitespace"
978
+ });
979
+ }
980
+ if (tmEntry.fuzzy === true) {
981
+ penalties.push({ kind: "fuzzy", points: 5, detail: "TM entry is flagged as fuzzy" });
982
+ }
983
+ if (tmEntry.origin === "alignment") {
984
+ penalties.push({
985
+ kind: "unaligned",
986
+ points: 2,
987
+ detail: "alignment entry (default unaligned penalty)"
988
+ });
989
+ }
990
+ return { skeletonEqual, numbersEqual, atomsEqual, numberDiff, tagDiff, punctDiff, penalties };
991
+ }
992
+ function sumPenalties(penalties) {
993
+ return penalties.reduce((total, penalty) => total + penalty.points, 0);
994
+ }
995
+ function osaDistance(a, b) {
996
+ const rows = a.length + 1;
997
+ const cols = b.length + 1;
998
+ const matrix = Array.from(
999
+ { length: rows },
1000
+ (_, row) => Array.from({ length: cols }, (_2, column) => row === 0 ? column : column === 0 ? row : 0)
1001
+ );
1002
+ for (let i = 1; i < rows; i += 1) {
1003
+ for (let j = 1; j < cols; j += 1) {
1004
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1005
+ let value = Math.min(
1006
+ matrix[i - 1][j] + 1,
1007
+ matrix[i][j - 1] + 1,
1008
+ matrix[i - 1][j - 1] + cost
1009
+ );
1010
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
1011
+ value = Math.min(value, matrix[i - 2][j - 2] + 1);
1012
+ }
1013
+ matrix[i][j] = value;
1014
+ }
1015
+ }
1016
+ return matrix[rows - 1][cols - 1];
1017
+ }
1018
+ function levenshteinDistance(a, b) {
1019
+ if (a === b) return 0;
1020
+ if (a.length === 0) return b.length;
1021
+ if (b.length === 0) return a.length;
1022
+ let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
1023
+ for (let i = 1; i <= a.length; i += 1) {
1024
+ const current = new Array(b.length + 1).fill(0);
1025
+ current[0] = i;
1026
+ for (let j = 1; j <= b.length; j += 1) {
1027
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1028
+ current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
1029
+ }
1030
+ previous = current;
1031
+ }
1032
+ return previous[b.length];
1033
+ }
1034
+ function round1(value) {
1035
+ return Math.round(value * 10) / 10;
1036
+ }
1037
+ function similarityOf(source, entry) {
1038
+ if (arraysEqual(source.skeleton, entry.skeleton)) return { similarity: 100, truncated: false };
1039
+ if (source.skeleton.length >= 2 && entry.skeleton.length >= 2) {
1040
+ const distance2 = osaDistance(source.skeleton, entry.skeleton);
1041
+ const denominator2 = Math.max(source.skeleton.length, entry.skeleton.length, 3);
1042
+ return { similarity: round1(100 * (1 - distance2 / denominator2)), truncated: false };
1043
+ }
1044
+ const truncated = source.norm.length > SIMILARITY_TRUNCATE || entry.norm.length > SIMILARITY_TRUNCATE;
1045
+ const a = source.norm.slice(0, SIMILARITY_TRUNCATE);
1046
+ const b = entry.norm.slice(0, SIMILARITY_TRUNCATE);
1047
+ const denominator = Math.max(a.length, b.length, 3);
1048
+ const distance = levenshteinDistance(a, b);
1049
+ return { similarity: round1(100 * (1 - distance / denominator)), truncated };
1050
+ }
1051
+ function matchTypeFor(rate) {
1052
+ if (rate >= 95) return "near_exact";
1053
+ if (rate >= 85) return "high_fuzzy";
1054
+ if (rate >= 75) return "medium_fuzzy";
1055
+ return "low_fuzzy";
1056
+ }
1057
+ function compareContext(provided, stored) {
1058
+ if (!provided || !stored) return { matched: false, both: false };
1059
+ const previousMatches = provided.previousSource !== void 0 && stored.previousSource !== void 0 && normalizeText(provided.previousSource) === normalizeText(stored.previousSource);
1060
+ const nextMatches = provided.nextSource !== void 0 && stored.nextSource !== void 0 && normalizeText(provided.nextSource) === normalizeText(stored.nextSource);
1061
+ return { matched: previousMatches || nextMatches, both: previousMatches && nextMatches };
1062
+ }
1063
+ function compareLastModified(a, b) {
1064
+ const left = a.lastModified ?? "";
1065
+ const right = b.lastModified ?? "";
1066
+ if (left === right) return 0;
1067
+ if (left === "") return 1;
1068
+ if (right === "") return -1;
1069
+ return left < right ? 1 : -1;
1070
+ }
1071
+ function languageAllowed(entryLanguage, wanted) {
1072
+ if (wanted === void 0) return true;
1073
+ if (entryLanguage === "") return true;
1074
+ return languageMatches(entryLanguage, wanted);
1075
+ }
1076
+ function matchTm(entries, sourceText, options = {}) {
1077
+ const threshold = options.threshold ?? DEFAULT_THRESHOLD;
1078
+ const maxMatches = options.maxMatches ?? DEFAULT_MAX_MATCHES;
1079
+ if (maxMatches <= 0) return [];
1080
+ const source = tokenize(sourceText);
1081
+ const scored = [];
1082
+ entries.forEach((entry, index) => {
1083
+ if (!languageAllowed(entry.sourceLanguage, options.sourceLanguage)) return;
1084
+ if (!languageAllowed(entry.targetLanguage, options.targetLanguage)) return;
1085
+ const target = tokenize(entry.source);
1086
+ const structure = compareStructure(source, target, entry);
1087
+ const context = compareContext(options.context, entry.context);
1088
+ const truncation = structure.skeletonEqual ? { similarity: 100, truncated: false } : similarityOf(source, target);
1089
+ let matchRate;
1090
+ let matchType;
1091
+ if (structure.skeletonEqual && structure.numbersEqual && structure.atomsEqual && !structure.punctDiff) {
1092
+ structure.penalties.length = 0;
1093
+ if (context.both) {
1094
+ matchRate = 102;
1095
+ matchType = "double_context";
1096
+ } else if (context.matched) {
1097
+ matchRate = 101;
1098
+ matchType = "context";
1099
+ } else {
1100
+ matchRate = 100;
1101
+ matchType = "exact";
1102
+ }
1103
+ } else if (structure.skeletonEqual) {
1104
+ const penalties = sumPenalties(structure.penalties);
1105
+ const candidate = Math.min(99, Math.round(100 - penalties));
1106
+ if (candidate < 50) return;
1107
+ matchRate = candidate;
1108
+ matchType = matchTypeFor(candidate);
1109
+ } else {
1110
+ const truncationPenalty = truncation.truncated ? TRUNCATION_PENALTY : 0;
1111
+ const adjusted = truncation.similarity - sumPenalties(structure.penalties) - truncationPenalty;
1112
+ if (adjusted < 50) return;
1113
+ const candidate = Math.min(99, Math.round(adjusted));
1114
+ if (candidate < 50) return;
1115
+ matchRate = candidate;
1116
+ matchType = matchTypeFor(candidate);
1117
+ }
1118
+ if (matchRate < threshold) return;
1119
+ scored.push({
1120
+ index,
1121
+ match: {
1122
+ entry,
1123
+ similarity: truncation.similarity,
1124
+ matchRate,
1125
+ matchType,
1126
+ penalties: structure.penalties,
1127
+ contextMatched: context.matched,
1128
+ flags: {
1129
+ numberDiff: structure.numberDiff,
1130
+ tagDiff: structure.tagDiff,
1131
+ punctDiff: structure.punctDiff,
1132
+ truncated: truncation.truncated
1133
+ }
1134
+ }
1135
+ });
1136
+ });
1137
+ scored.sort((left, right) => {
1138
+ const a = left.match;
1139
+ const b = right.match;
1140
+ if (b.matchRate !== a.matchRate) return b.matchRate - a.matchRate;
1141
+ if (b.similarity !== a.similarity) return b.similarity - a.similarity;
1142
+ const modified = compareLastModified(a.entry, b.entry);
1143
+ if (modified !== 0) return modified;
1144
+ if (a.entry.id !== b.entry.id) return a.entry.id < b.entry.id ? -1 : 1;
1145
+ return left.index - right.index;
1146
+ });
1147
+ return scored.slice(0, maxMatches).map((item) => item.match);
1148
+ }
1149
+
1150
+ // packages/l10n-core/src/assets/term-matcher.ts
1151
+ var CJK_CHAR2 = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
1152
+ var WORD_CHAR2 = /[\p{L}\p{N}_]/u;
1153
+ var COMBINING_MARK2 = new RegExp("\\p{M}", "u");
1154
+ var MAX_TERM_TOKENS = 8;
1155
+ var FUZZY_THRESHOLD = 0.8;
1156
+ function readToken(text, index) {
1157
+ const codePoint = text.codePointAt(index);
1158
+ if (codePoint === void 0) return void 0;
1159
+ const char = String.fromCodePoint(codePoint);
1160
+ const end = index + char.length;
1161
+ if (CJK_CHAR2.test(char)) return { value: char, start: index, end, kind: "cjk" };
1162
+ return { value: char, start: index, end, kind: "word" };
1163
+ }
1164
+ function tokenizeText(text) {
1165
+ const tokens = [];
1166
+ let index = 0;
1167
+ while (index < text.length) {
1168
+ const first = readToken(text, index);
1169
+ if (!first) break;
1170
+ if (first.kind === "cjk") {
1171
+ tokens.push(first);
1172
+ index = first.end;
1173
+ continue;
1174
+ }
1175
+ if (!WORD_CHAR2.test(first.value)) {
1176
+ index = first.end;
1177
+ continue;
1178
+ }
1179
+ let end = first.end;
1180
+ while (end < text.length) {
1181
+ const next = readToken(text, end);
1182
+ if (!next || next.kind === "cjk") break;
1183
+ if (!WORD_CHAR2.test(next.value) && !COMBINING_MARK2.test(next.value)) break;
1184
+ end = next.end;
1185
+ }
1186
+ tokens.push({ value: text.slice(index, end), start: index, end, kind: "word" });
1187
+ index = end;
1188
+ }
1189
+ return tokens;
1190
+ }
1191
+ function codePointLength(value) {
1192
+ return [...value].length;
1193
+ }
1194
+ function caseModeOf(entry) {
1195
+ if (entry.caseSensitive === true) return "yes";
1196
+ if (entry.caseSensitive === false) return "no";
1197
+ return "permissive";
1198
+ }
1199
+ function caseMatches(term, text, mode) {
1200
+ if (term.length !== text.length) return false;
1201
+ if (mode === "yes") return term === text;
1202
+ if (mode === "no") return term.toLowerCase() === text.toLowerCase();
1203
+ for (let index = 0; index < term.length; index += 1) {
1204
+ const termChar = term[index];
1205
+ const textChar = text[index];
1206
+ if (termChar === textChar) continue;
1207
+ if (termChar.toLowerCase() !== textChar.toLowerCase()) return false;
1208
+ if (termChar !== termChar.toLowerCase()) return false;
1209
+ }
1210
+ return true;
1211
+ }
1212
+ function levenshtein(a, b) {
1213
+ if (a === b) return 0;
1214
+ if (a.length === 0) return b.length;
1215
+ if (b.length === 0) return a.length;
1216
+ let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
1217
+ for (let i = 1; i <= a.length; i += 1) {
1218
+ const current = new Array(b.length + 1).fill(0);
1219
+ current[0] = i;
1220
+ for (let j = 1; j <= b.length; j += 1) {
1221
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1222
+ current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
1223
+ }
1224
+ previous = current;
1225
+ }
1226
+ return previous[b.length];
1227
+ }
1228
+ function similarity(a, b) {
1229
+ const longest = Math.max(a.length, b.length);
1230
+ if (longest === 0) return 1;
1231
+ return 1 - levenshtein(a, b) / longest;
1232
+ }
1233
+ function languageAllowed2(entryLanguage, wanted) {
1234
+ if (wanted === void 0) return true;
1235
+ if (entryLanguage === void 0 || entryLanguage === "") return true;
1236
+ return languageMatches(entryLanguage, wanted);
1237
+ }
1238
+ function scanExactOrPrefix(tokens, termTokens, caseMode, options) {
1239
+ const matches = [];
1240
+ const lastIndex = termTokens.length - 1;
1241
+ for (let start = 0; start + termTokens.length <= tokens.length; start += 1) {
1242
+ let matched = true;
1243
+ let exactCase = true;
1244
+ let extended = false;
1245
+ for (let offset = 0; offset <= lastIndex; offset += 1) {
1246
+ const termValue = termTokens[offset];
1247
+ const textToken = tokens[start + offset];
1248
+ if (offset === lastIndex && options.suffixLimit >= 0) {
1249
+ if (textToken.value.length < termValue.length) {
1250
+ matched = false;
1251
+ break;
1252
+ }
1253
+ const head = textToken.value.slice(0, termValue.length);
1254
+ if (!caseMatches(termValue, head, caseMode)) {
1255
+ matched = false;
1256
+ break;
1257
+ }
1258
+ const suffix = textToken.value.slice(termValue.length);
1259
+ if (suffix !== "" && codePointLength(suffix) > options.suffixLimit) {
1260
+ matched = false;
1261
+ break;
1262
+ }
1263
+ if (head !== termValue) exactCase = false;
1264
+ if (suffix !== "") extended = true;
1265
+ } else {
1266
+ if (!caseMatches(termValue, textToken.value, caseMode)) {
1267
+ matched = false;
1268
+ break;
1269
+ }
1270
+ if (textToken.value !== termValue) exactCase = false;
1271
+ }
1272
+ }
1273
+ if (!matched) continue;
1274
+ const first = tokens[start];
1275
+ const last = tokens[start + lastIndex];
1276
+ matches.push({
1277
+ start: first.start,
1278
+ end: last.end,
1279
+ exactCase,
1280
+ kind: extended ? options.kind : "exact"
1281
+ });
1282
+ }
1283
+ return matches;
1284
+ }
1285
+ function scanFuzzy(tokens, term, caseMode) {
1286
+ const normalizedTerm = caseMode === "yes" ? term : term.toLowerCase();
1287
+ const matches = [];
1288
+ for (const token of tokens) {
1289
+ if (token.kind !== "word") continue;
1290
+ const normalizedText = caseMode === "yes" ? token.value : token.value.toLowerCase();
1291
+ if (similarity(normalizedTerm, normalizedText) >= FUZZY_THRESHOLD) {
1292
+ matches.push({
1293
+ start: token.start,
1294
+ end: token.end,
1295
+ exactCase: token.value === term,
1296
+ kind: "fuzzy"
1297
+ });
1298
+ }
1299
+ }
1300
+ return matches;
1301
+ }
1302
+ function matchTerms(text, entries, options = {}) {
1303
+ const source = text.normalize("NFC");
1304
+ const tokens = tokenizeText(source);
1305
+ const hits = [];
1306
+ const warnings = [];
1307
+ const seenWarnings = /* @__PURE__ */ new Set();
1308
+ const emittedHits = /* @__PURE__ */ new Set();
1309
+ const warn = (message) => {
1310
+ if (seenWarnings.has(message)) return;
1311
+ seenWarnings.add(message);
1312
+ warnings.push(message);
1313
+ };
1314
+ for (const entry of entries) {
1315
+ if (!languageAllowed2(entry.sourceLanguage, options.sourceLanguage)) continue;
1316
+ if (!languageAllowed2(entry.targetLanguage, options.targetLanguage)) continue;
1317
+ const caseMode = caseModeOf(entry);
1318
+ const mode = entry.matching ?? "prefix50";
1319
+ const forms = /* @__PURE__ */ new Set();
1320
+ for (const form of [entry.source, entry.target]) {
1321
+ const normalized = form.normalize("NFC").trim();
1322
+ if (normalized !== "") forms.add(normalized);
1323
+ }
1324
+ for (const form of forms) {
1325
+ let termTokens = tokenizeText(form).map((token) => token.value);
1326
+ if (termTokens.length === 0) continue;
1327
+ if (termTokens.length > MAX_TERM_TOKENS) {
1328
+ warn(`glossary: term "${form}" exceeds ${MAX_TERM_TOKENS} tokens; skipped`);
1329
+ continue;
1330
+ }
1331
+ let candidates = [];
1332
+ if (mode === "fuzzy" && options.fuzzy === true) {
1333
+ if (termTokens.length === 1) candidates = scanFuzzy(tokens, termTokens[0], caseMode);
1334
+ else warn(`glossary: fuzzy matching only supports single-token terms; "${form}" skipped`);
1335
+ } else if (mode === "custom") {
1336
+ if (form.endsWith("*") && !form.slice(0, -1).includes("*")) {
1337
+ termTokens = tokenizeText(form.slice(0, -1)).map((token) => token.value);
1338
+ if (termTokens.length === 0) continue;
1339
+ if (termTokens.length > MAX_TERM_TOKENS) {
1340
+ warn(`glossary: custom term "${form}" exceeds ${MAX_TERM_TOKENS} tokens; skipped`);
1341
+ continue;
1342
+ }
1343
+ candidates = scanExactOrPrefix(tokens, termTokens, caseMode, {
1344
+ suffixLimit: Number.POSITIVE_INFINITY,
1345
+ kind: "custom"
1346
+ });
1347
+ } else {
1348
+ warn(`glossary: unsupported custom pattern "${form}"; matched as exact`);
1349
+ candidates = scanExactOrPrefix(tokens, termTokens, caseMode, { suffixLimit: -1, kind: "exact" });
1350
+ }
1351
+ } else {
1352
+ const isPrefix = mode === "prefix50";
1353
+ const lastToken = termTokens[termTokens.length - 1];
1354
+ candidates = scanExactOrPrefix(tokens, termTokens, caseMode, {
1355
+ suffixLimit: isPrefix ? codePointLength(lastToken) : -1,
1356
+ kind: isPrefix ? "prefix50" : "exact"
1357
+ });
1358
+ }
1359
+ for (const candidate of candidates) {
1360
+ const key = `${entry.id}:${candidate.start}:${candidate.end}:${candidate.kind}`;
1361
+ if (emittedHits.has(key)) continue;
1362
+ emittedHits.add(key);
1363
+ hits.push({
1364
+ entry,
1365
+ start: candidate.start,
1366
+ end: candidate.end,
1367
+ matchKind: candidate.kind,
1368
+ caseMatch: candidate.exactCase ? "exact" : "case_only_diff"
1369
+ });
1370
+ }
1371
+ }
1372
+ }
1373
+ const deduped = [];
1374
+ for (const hit of hits) {
1375
+ const covering = deduped.findIndex(
1376
+ (kept) => kept.entry.id === hit.entry.id && kept.start < hit.end && hit.start < kept.end
1377
+ );
1378
+ if (covering < 0) {
1379
+ deduped.push(hit);
1380
+ continue;
1381
+ }
1382
+ const existing = deduped[covering];
1383
+ if (hit.end - hit.start > existing.end - existing.start) deduped[covering] = hit;
1384
+ }
1385
+ hits.length = 0;
1386
+ hits.push(...deduped);
1387
+ hits.sort((left, right) => {
1388
+ if (left.start !== right.start) return left.start - right.start;
1389
+ if (left.end !== right.end) return right.end - left.end;
1390
+ if (left.entry.id !== right.entry.id) return left.entry.id < right.entry.id ? -1 : 1;
1391
+ return 0;
1392
+ });
1393
+ return { hits, issues: [], warnings };
1394
+ }
1395
+
1396
+ // packages/l10n-core/src/qa/rules.ts
1397
+ var RULES = [
1398
+ // structural class
1399
+ { id: "EMPTY_TARGET", severity: "critical" },
1400
+ { id: "UNTRANSLATED_COPY", severity: "critical" },
1401
+ { id: "PLACEHOLDER_MISSING", severity: "critical" },
1402
+ { id: "PLACEHOLDER_EXTRA", severity: "critical" },
1403
+ { id: "TAG_MISMATCH", severity: "critical" },
1404
+ { id: "URL_CHANGED", severity: "critical" },
1405
+ { id: "TB_SOURCE_FORBIDDEN", severity: "major", requiresTerms: true },
1406
+ { id: "TB_FORBIDDEN_USED", severity: "critical", requiresTerms: true },
1407
+ { id: "UNPAIRED_BRACKET", severity: "major" },
1408
+ // content class
1409
+ { id: "NUMBER_MISMATCH", severity: "major" },
1410
+ { id: "DATE_MISMATCH", severity: "major" },
1411
+ { id: "DUPLICATE_WORD", severity: "major" },
1412
+ { id: "TERM_MISSING", severity: "major", requiresTerms: true },
1413
+ { id: "SOURCE_LEFTOVER", severity: "major" },
1414
+ { id: "PUNCT_DUP", severity: "major" },
1415
+ { id: "TRAILING_COLON", severity: "minor" },
1416
+ // language variant / style class
1417
+ { id: "SENTENCE_END_PUNCT", severity: "minor" },
1418
+ { id: "WHITESPACE", severity: "minor" },
1419
+ { id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
1420
+ { id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
1421
+ ];
1422
+ var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
1423
+ var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
1424
+ var TERM_RULE_IDS = RULES.filter((rule) => rule.requiresTerms).map((rule) => rule.id);
1425
+ var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
1426
+ function normalizeForCompare(value) {
1427
+ return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
1428
+ }
1429
+ var CJK_RE = /[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/u;
1430
+ function cjkFraction(value) {
1431
+ let cjk = 0;
1432
+ let visible = 0;
1433
+ for (const char of value) {
1434
+ if (/\s/u.test(char)) continue;
1435
+ visible += 1;
1436
+ if (CJK_RE.test(char)) cjk += 1;
1437
+ }
1438
+ return visible === 0 ? 0 : cjk / visible;
1439
+ }
1440
+ function isCjkHeavy(value) {
1441
+ return cjkFraction(value) >= 0.3;
1442
+ }
1443
+ function charCount(value) {
1444
+ return Array.from(value).length;
1445
+ }
1446
+ function wordTokens(value) {
1447
+ const matches = value.normalize("NFKC").match(/[\p{L}\p{N}][\p{L}\p{N}'’_-]*/gu);
1448
+ return matches ?? [];
1449
+ }
1450
+ function escapeRegExp(value) {
1451
+ return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
1452
+ }
1453
+ function truncate(value, max = 120) {
1454
+ const collapsed = value.replace(/\s+/gu, " ").trim();
1455
+ return collapsed.length <= max ? collapsed : `${collapsed.slice(0, max - 1)}\u2026`;
1456
+ }
1457
+ function multiset(values) {
1458
+ const counts = /* @__PURE__ */ new Map();
1459
+ for (const value of values) counts.set(value, (counts.get(value) ?? 0) + 1);
1460
+ return counts;
1461
+ }
1462
+ function multisetDelta(expected, actual) {
1463
+ const expectedCounts = multiset(expected);
1464
+ const actualCounts = multiset(actual);
1465
+ const missing = [];
1466
+ const extra = [];
1467
+ for (const [value, count] of expectedCounts) {
1468
+ for (let index = actualCounts.get(value) ?? 0; index < count; index += 1) missing.push(value);
1469
+ }
1470
+ for (const [value, count] of actualCounts) {
1471
+ for (let index = expectedCounts.get(value) ?? 0; index < count; index += 1) extra.push(value);
1472
+ }
1473
+ missing.sort();
1474
+ extra.sort();
1475
+ return { missing, extra };
1476
+ }
1477
+ function draft(context, issue) {
1478
+ context.drafts.push(issue);
1479
+ }
1480
+ var PLACEHOLDER_PATTERNS = [
1481
+ /%(?:\d+\$)?[-+#0 ]*\d*(?:\.\d+)?[sdfuxXoc]/gu,
1482
+ /\{\{[^{}\n]{1,64}\}\}/gu,
1483
+ /\$\{[^{}\n]{1,64}\}/gu,
1484
+ /\{[A-Za-z_][A-Za-z0-9_.:-]{0,63}\}/gu,
1485
+ /\{\d{1,6}\}/gu
1486
+ ];
1487
+ function collectPlaceholders(value) {
1488
+ const matches = [];
1489
+ for (const pattern of PLACEHOLDER_PATTERNS) {
1490
+ const regex = new RegExp(pattern.source, pattern.flags);
1491
+ for (const match of value.matchAll(regex)) {
1492
+ if (match.index === void 0) continue;
1493
+ matches.push({ text: match[0], start: match.index, end: match.index + match[0].length });
1494
+ }
1495
+ }
1496
+ matches.sort((left, right) => left.start - right.start || right.end - left.end);
1497
+ const result = [];
1498
+ let lastEnd = -1;
1499
+ for (const match of matches) {
1500
+ if (match.start < lastEnd) continue;
1501
+ result.push(match);
1502
+ lastEnd = match.end;
1503
+ }
1504
+ return result;
1505
+ }
1506
+ function checkPlaceholders(context) {
1507
+ const sourcePlaceholders = collectPlaceholders(context.sourceInner);
1508
+ const targetPlaceholders = collectPlaceholders(context.targetInner);
1509
+ if (sourcePlaceholders.length === 0 && targetPlaceholders.length === 0) return;
1510
+ const delta = multisetDelta(
1511
+ sourcePlaceholders.map((token) => token.text),
1512
+ targetPlaceholders.map((token) => token.text)
1513
+ );
1514
+ if (delta.missing.length > 0) {
1515
+ const first = sourcePlaceholders.find((token) => token.text === delta.missing[0]);
1516
+ draft(context, {
1517
+ ruleId: "PLACEHOLDER_MISSING",
1518
+ message: `\u8BD1\u6587\u7F3A\u5C11\u5360\u4F4D\u7B26: ${delta.missing.join(", ")}`,
1519
+ evidence: `source placeholders: [${sourcePlaceholders.map((token) => token.text).join(", ")}]`,
1520
+ suggestion: `\u4FDD\u7559\u5360\u4F4D\u7B26: ${delta.missing.join(", ")}`,
1521
+ ...first === void 0 ? {} : { sourceRange: { start: first.start, end: first.end } }
1522
+ });
1523
+ }
1524
+ if (delta.extra.length > 0) {
1525
+ const first = targetPlaceholders.find((token) => token.text === delta.extra[0]);
1526
+ draft(context, {
1527
+ ruleId: "PLACEHOLDER_EXTRA",
1528
+ message: `\u8BD1\u6587\u591A\u51FA\u5360\u4F4D\u7B26: ${delta.extra.join(", ")}`,
1529
+ evidence: `target placeholders: [${targetPlaceholders.map((token) => token.text).join(", ")}]`,
1530
+ ...first === void 0 ? {} : { targetRange: { start: first.start, end: first.end } }
1531
+ });
1532
+ }
1533
+ }
1534
+ function checkTags(context) {
1535
+ const sourceTags = context.segment.sourceTags;
1536
+ const targetTags = context.segment.targetTags;
1537
+ if (sourceTags.length === 0 && targetTags.length === 0) return;
1538
+ const sourceSignature = sourceTags.join("|");
1539
+ const targetSignature = targetTags.join("|");
1540
+ if (sourceSignature === targetSignature) return;
1541
+ const sameMultiset = sourceTags.length === targetTags.length && JSON.stringify([...sourceTags].sort()) === JSON.stringify([...targetTags].sort());
1542
+ draft(context, {
1543
+ ruleId: "TAG_MISMATCH",
1544
+ message: sameMultiset ? "inline tag \u987A\u5E8F\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4" : `inline tag \u4E0D\u4E00\u81F4: \u6E90\u6587 [${sourceTags.join(", ")}] / \u8BD1\u6587 [${targetTags.join(", ")}]`,
1545
+ evidence: `source tags: [${sourceTags.join(", ")}], target tags: [${targetTags.join(", ")}]`,
1546
+ suggestion: "\u4FDD\u6301 inline tag \u6570\u91CF\u4E0E\u987A\u5E8F\u548C\u6E90\u6587\u4E00\u81F4"
1547
+ });
1548
+ }
1549
+ var URL_PATTERNS = [
1550
+ /\bhttps?:\/\/[^\s<>"']+/giu,
1551
+ /\bwww\.[^\s<>"']+/giu,
1552
+ /\bmailto:[^\s<>"']+/giu,
1553
+ /\b[\w.+-]+@[\w-]+\.[\w.-]+\b/gu,
1554
+ /\b[A-Za-z]:\\[^\s<>"']+/gu,
1555
+ /\\\\[^\s<>"']+/gu,
1556
+ /(?:^|[\s("'[,。;:])(\/(?:[\w.-]+\/)+[\w.-]+\.(?:json|yaml|yml|xml|txt|csv|tsv|png|jpe?g|gif|svg|pdf|docx?|xlsx?|pptx?|zip|log|md|html?|js|ts|css|ini|conf|config|exe|dll|sh|bat|py))\b/giu
1557
+ ];
1558
+ function extractUrls(value) {
1559
+ const found = [];
1560
+ for (const pattern of URL_PATTERNS) {
1561
+ const regex = new RegExp(pattern.source, pattern.flags);
1562
+ for (const match of value.matchAll(regex)) {
1563
+ let text = pattern.source.includes("(?:^|[\\s(") ? match[1] ?? "" : match[0];
1564
+ text = text.replace(/[),.;:!?'"\]】。,;:]+$/u, "");
1565
+ if (text.length > 0) found.push(text);
1566
+ }
1567
+ }
1568
+ const unique = /* @__PURE__ */ new Map();
1569
+ for (const item of found) {
1570
+ const key = item.toLocaleLowerCase();
1571
+ if (!unique.has(key)) unique.set(key, item);
1572
+ }
1573
+ return Array.from(unique.values()).sort();
1574
+ }
1575
+ function checkUrls(context) {
1576
+ const sourceUrls = extractUrls(context.sourceInner.length > 0 ? context.sourceInner : context.source);
1577
+ const targetUrls = extractUrls(context.targetInner.length > 0 ? context.targetInner : context.target);
1578
+ if (sourceUrls.length === 0) return;
1579
+ const targetSet = new Set(targetUrls.map((url) => url.toLocaleLowerCase()));
1580
+ const sourceSet = new Set(sourceUrls.map((url) => url.toLocaleLowerCase()));
1581
+ const missing = sourceUrls.filter((url) => !targetSet.has(url.toLocaleLowerCase()));
1582
+ const extra = targetUrls.filter((url) => !sourceSet.has(url.toLocaleLowerCase()));
1583
+ if (missing.length === 0 && extra.length === 0) return;
1584
+ const firstMissing = missing[0];
1585
+ const index = firstMissing === void 0 ? -1 : context.source.indexOf(firstMissing);
1586
+ draft(context, {
1587
+ ruleId: "URL_CHANGED",
1588
+ message: missing.length > 0 ? `\u8BD1\u6587\u7F3A\u5C11/\u6539\u52A8\u4E86 URL\u3001\u90AE\u7BB1\u6216\u8DEF\u5F84: ${missing.join(", ")}` : `\u8BD1\u6587\u591A\u51FA\u6E90\u6587\u6CA1\u6709\u7684 URL\u3001\u90AE\u7BB1\u6216\u8DEF\u5F84: ${extra.join(", ")}`,
1589
+ evidence: `source: [${sourceUrls.join(", ")}] target: [${targetUrls.join(", ")}]`,
1590
+ ...firstMissing === void 0 ? {} : { suggestion: `\u4FDD\u7559\u539F\u6587: ${firstMissing}` },
1591
+ ...index < 0 || firstMissing === void 0 ? {} : { sourceRange: { start: index, end: index + firstMissing.length } }
1592
+ });
1593
+ }
1594
+ var MONTHS = new Map(
1595
+ [
1596
+ "january",
1597
+ "february",
1598
+ "march",
1599
+ "april",
1600
+ "may",
1601
+ "june",
1602
+ "july",
1603
+ "august",
1604
+ "september",
1605
+ "october",
1606
+ "november",
1607
+ "december"
1608
+ ].map((month, index) => [month, index + 1])
1609
+ );
1610
+ var MONTH_ALIASES = new Map([
1611
+ ...MONTHS,
1612
+ ...Array.from(MONTHS, ([month, number]) => [month.slice(0, 3), number]),
1613
+ ["sept", 9]
1614
+ ]);
1615
+ var CJK_DIGITS = /* @__PURE__ */ new Map([
1616
+ ["\u3007", "0"],
1617
+ ["\u96F6", "0"],
1618
+ ["\u4E00", "1"],
1619
+ ["\u4E8C", "2"],
1620
+ ["\u4E09", "3"],
1621
+ ["\u56DB", "4"],
1622
+ ["\u4E94", "5"],
1623
+ ["\u516D", "6"],
1624
+ ["\u4E03", "7"],
1625
+ ["\u516B", "8"],
1626
+ ["\u4E5D", "9"]
1627
+ ]);
1628
+ function pad2(value) {
1629
+ return String(value).padStart(2, "0");
1630
+ }
1631
+ function validMonthDay(month, day) {
1632
+ return month >= 1 && month <= 12 && day >= 1 && day <= 31;
1633
+ }
1634
+ var DATE_PATTERNS = [
1635
+ /\b(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})\b/gu,
1636
+ /\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b/gu,
1637
+ /(\d{4}|[〇零一二三四五六七八九]{4})年(\d{1,2})月(?:(\d{1,2})日)?/gu
1638
+ ];
1639
+ function extractDates(value) {
1640
+ const tokens = [];
1641
+ const push = (key, text, start) => {
1642
+ tokens.push({ key, text, span: { start, end: start + text.length } });
1643
+ };
1644
+ for (const match of value.matchAll(DATE_PATTERNS[0] ?? /$^/gu)) {
1645
+ const [text = "", year = "", month = "", day = ""] = match;
1646
+ if (match.index === void 0) continue;
1647
+ const monthNumber = Number(month);
1648
+ const dayNumber = Number(day);
1649
+ if (!validMonthDay(monthNumber, dayNumber)) continue;
1650
+ push(`${year}-${pad2(monthNumber)}-${pad2(dayNumber)}`, text, match.index);
1651
+ }
1652
+ for (const match of value.matchAll(DATE_PATTERNS[1] ?? /$^/gu)) {
1653
+ const [text = "", first = "", second = "", yearRaw = ""] = match;
1654
+ if (match.index === void 0) continue;
1655
+ let day = Number(first);
1656
+ let month = Number(second);
1657
+ if (month > 12 && day <= 12) {
1658
+ const swap = day;
1659
+ day = month;
1660
+ month = swap;
1661
+ }
1662
+ if (!validMonthDay(month, day)) continue;
1663
+ if (yearRaw.length === 2) {
1664
+ push(`${yearRaw}-${pad2(month)}-${pad2(day)}`, text, match.index);
1665
+ continue;
1666
+ }
1667
+ const year = Number(yearRaw);
1668
+ if (year < 1e3 || year > 2999) continue;
1669
+ push(`${year}-${pad2(month)}-${pad2(day)}`, text, match.index);
1670
+ }
1671
+ for (const match of value.matchAll(DATE_PATTERNS[2] ?? /$^/gu)) {
1672
+ const [text = "", yearRaw = "", month = "", day] = match;
1673
+ if (match.index === void 0) continue;
1674
+ let year;
1675
+ if (/^\d{4}$/u.test(yearRaw)) {
1676
+ year = yearRaw;
1677
+ } else {
1678
+ let converted = "";
1679
+ for (const char of yearRaw) converted += CJK_DIGITS.get(char) ?? "";
1680
+ if (converted.length !== 4) continue;
1681
+ year = converted;
1682
+ }
1683
+ const monthNumber = Number(month);
1684
+ if (!validMonthDay(monthNumber, 1)) continue;
1685
+ if (day === void 0) {
1686
+ push(`${year}-${pad2(monthNumber)}`, text, match.index);
1687
+ } else {
1688
+ const dayNumber = Number(day);
1689
+ if (!validMonthDay(monthNumber, dayNumber)) continue;
1690
+ push(`${year}-${pad2(monthNumber)}-${pad2(dayNumber)}`, text, match.index);
1691
+ }
1692
+ }
1693
+ const monthNames = Array.from(MONTH_ALIASES.keys()).sort((left, right) => right.length - left.length).map(escapeRegExp).join("|");
1694
+ const mdy = new RegExp(
1695
+ `\\b(${monthNames})[ .]+(\\d{1,2})(?:st|nd|rd|th)?(?:[, ]+(\\d{4}))?\\b`,
1696
+ "giu"
1697
+ );
1698
+ for (const match of value.matchAll(mdy)) {
1699
+ const [text = "", monthRaw = "", day = "", year] = match;
1700
+ if (match.index === void 0) continue;
1701
+ const month = MONTH_ALIASES.get(monthRaw.toLowerCase());
1702
+ if (month === void 0) continue;
1703
+ const dayNumber = Number(day);
1704
+ if (!validMonthDay(month, dayNumber)) continue;
1705
+ push(
1706
+ year === void 0 ? `?-${pad2(month)}-${pad2(dayNumber)}` : `${year}-${pad2(month)}-${pad2(dayNumber)}`,
1707
+ text,
1708
+ match.index
1709
+ );
1710
+ }
1711
+ const dmy = new RegExp(`\\b(\\d{1,2})(?:st|nd|rd|th)?[ .]+(${monthNames})[ .]*(\\d{4})?\\b`, "giu");
1712
+ for (const match of value.matchAll(dmy)) {
1713
+ const [text = "", day = "", monthRaw = "", year] = match;
1714
+ if (match.index === void 0) continue;
1715
+ const month = MONTH_ALIASES.get(monthRaw.toLowerCase());
1716
+ if (month === void 0) continue;
1717
+ const dayNumber = Number(day);
1718
+ if (!validMonthDay(month, dayNumber)) continue;
1719
+ push(
1720
+ year === void 0 ? `?-${pad2(month)}-${pad2(dayNumber)}` : `${year}-${pad2(month)}-${pad2(dayNumber)}`,
1721
+ text,
1722
+ match.index
1723
+ );
1724
+ }
1725
+ tokens.sort((left, right) => left.span.start - right.span.start);
1726
+ const result = [];
1727
+ let lastEnd = -1;
1728
+ for (const token of tokens) {
1729
+ if (token.span.start < lastEnd) continue;
1730
+ result.push(token);
1731
+ lastEnd = token.span.end;
1732
+ }
1733
+ return result;
1734
+ }
1735
+ function maskSpans(value, spans) {
1736
+ if (spans.length === 0) return value;
1737
+ const chars = Array.from(value);
1738
+ for (const span of spans) {
1739
+ for (let index = span.start; index < span.end && index < chars.length; index += 1) chars[index] = " ";
1740
+ }
1741
+ return chars.join("");
1742
+ }
1743
+ var NUMBER_RE = /(?<![\p{L}\p{N}_])[-+]?\d+(?:[.,]\d+)*(?:[eE][-+]?\d+)?(?![\p{L}\p{N}_])/gu;
1744
+ function normalizeNumber(raw) {
1745
+ let value = raw;
1746
+ const sign = value.startsWith("-") ? "-" : "";
1747
+ value = value.replace(/^[+-]/u, "");
1748
+ if (/^\d{1,3}(?:,\d{3})+(?:\.\d+)?$/u.test(value)) {
1749
+ value = value.replaceAll(",", "");
1750
+ } else if (/^\d{1,3}(?:\.\d{3}){2,}$/u.test(value)) {
1751
+ value = value.replaceAll(".", "");
1752
+ }
1753
+ if (!/^\d/u.test(value)) return raw.normalize("NFKC");
1754
+ if (/^\d+\.0+$/u.test(value)) value = value.replace(/\.0+$/u, "");
1755
+ return `${sign}${value}`;
1756
+ }
1757
+ function extractNumbers(value) {
1758
+ const masked = maskSpans(
1759
+ value,
1760
+ extractDates(value).map((token) => token.span)
1761
+ );
1762
+ const results = [];
1763
+ for (const match of masked.matchAll(NUMBER_RE)) results.push(normalizeNumber(match[0]));
1764
+ return results;
1765
+ }
1766
+ function checkNumbers(context) {
1767
+ const sourceNumbers = extractNumbers(context.source);
1768
+ const targetNumbers = extractNumbers(context.target);
1769
+ if (sourceNumbers.length === 0 && targetNumbers.length === 0) return;
1770
+ const delta = multisetDelta(sourceNumbers, targetNumbers);
1771
+ if (delta.missing.length === 0 && delta.extra.length === 0) return;
1772
+ const firstMissing = delta.missing[0];
1773
+ const index = firstMissing === void 0 ? -1 : context.target.indexOf(firstMissing);
1774
+ draft(context, {
1775
+ ruleId: "NUMBER_MISMATCH",
1776
+ message: delta.missing.length > 0 ? `\u8BD1\u6587\u7F3A\u5C11/\u6539\u52A8\u4E86\u6570\u5B57: ${delta.missing.join(", ")}` : `\u8BD1\u6587\u591A\u51FA\u6570\u5B57: ${delta.extra.join(", ")}`,
1777
+ evidence: `source numbers: [${sourceNumbers.join(", ")}] target numbers: [${targetNumbers.join(", ")}]`,
1778
+ ...firstMissing === void 0 ? {} : { suggestion: `\u6838\u5BF9\u6570\u5B57: ${firstMissing}` },
1779
+ ...index < 0 || firstMissing === void 0 ? {} : { targetRange: { start: index, end: index + firstMissing.length } }
1780
+ });
1781
+ }
1782
+ function checkDates(context) {
1783
+ const sourceDates = extractDates(context.source);
1784
+ const targetDates = extractDates(context.target);
1785
+ if (sourceDates.length === 0 && targetDates.length === 0) return;
1786
+ const delta = multisetDelta(
1787
+ sourceDates.map((token) => token.key),
1788
+ targetDates.map((token) => token.key)
1789
+ );
1790
+ if (delta.missing.length === 0 && delta.extra.length === 0) return;
1791
+ const firstMissing = sourceDates.find((token) => delta.missing.includes(token.key));
1792
+ draft(context, {
1793
+ ruleId: "DATE_MISMATCH",
1794
+ message: delta.missing.length > 0 ? `\u8BD1\u6587\u7F3A\u5C11/\u6539\u52A8\u4E86\u65E5\u671F: ${delta.missing.join(", ")}` : `\u8BD1\u6587\u591A\u51FA\u65E5\u671F: ${delta.extra.join(", ")}`,
1795
+ evidence: `source dates: [${sourceDates.map((token) => token.text).join(", ")}] target dates: [${targetDates.map((token) => token.text).join(", ")}]`,
1796
+ ...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
1797
+ });
1798
+ }
1799
+ var BRACKET_PAIRS = /* @__PURE__ */ new Map([
1800
+ ["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
1801
+ ["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
1802
+ ["[", /* @__PURE__ */ new Set(["]", "\u3011"])],
1803
+ ["\u3010", /* @__PURE__ */ new Set(["]", "\u3011"])],
1804
+ ["{", /* @__PURE__ */ new Set(["}", "\uFF5D"])],
1805
+ ["\uFF5B", /* @__PURE__ */ new Set(["}", "\uFF5D"])],
1806
+ ["\u3008", /* @__PURE__ */ new Set(["\u3009", ">"])],
1807
+ ["\u300A", /* @__PURE__ */ new Set(["\u300B"])],
1808
+ ["\u300C", /* @__PURE__ */ new Set(["\u300D"])],
1809
+ ["\u300E", /* @__PURE__ */ new Set(["\u300F"])],
1810
+ ["<", /* @__PURE__ */ new Set([">"])]
1811
+ ]);
1812
+ var CLOSE_TO_OPEN = /* @__PURE__ */ new Map([
1813
+ [")", "("],
1814
+ ["\uFF09", "\uFF08"],
1815
+ ["]", "["],
1816
+ ["\u3011", "\u3010"],
1817
+ ["}", "{"],
1818
+ ["\uFF5D", "\uFF5B"],
1819
+ ["\u3009", "\u3008"],
1820
+ ["\u300B", "\u300A"],
1821
+ ["\u300D", "\u300C"],
1822
+ ["\u300F", "\u300E"],
1823
+ [">", "<"]
1824
+ ]);
1825
+ function unpairedTokens(value) {
1826
+ const result = [];
1827
+ const stack = [];
1828
+ let offset = 0;
1829
+ for (const char of value) {
1830
+ const closes = BRACKET_PAIRS.get(char);
1831
+ if (closes) {
1832
+ stack.push({ open: char, index: offset });
1833
+ offset += 1;
1834
+ continue;
1835
+ }
1836
+ const expectedOpen = CLOSE_TO_OPEN.get(char);
1837
+ if (expectedOpen) {
1838
+ const top = stack[stack.length - 1];
1839
+ const topCloses = top === void 0 ? void 0 : BRACKET_PAIRS.get(top.open);
1840
+ if (top !== void 0 && topCloses?.has(char) === true) {
1841
+ stack.pop();
1842
+ } else {
1843
+ result.push({ char, index: offset, kind: "bracket" });
1844
+ }
1845
+ }
1846
+ offset += 1;
1847
+ }
1848
+ for (const entry of stack) result.push({ char: entry.open, index: entry.index, kind: "bracket" });
1849
+ const quotePairs = [["\u201C", "\u201D"], ["\u2018", "\u2019"]];
1850
+ for (const [open, close] of quotePairs) {
1851
+ const openCount = Array.from(value).filter((char) => char === open).length;
1852
+ const closeCount = Array.from(value).filter((char) => char === close).length;
1853
+ if (openCount !== closeCount) {
1854
+ const index = value.indexOf(openCount > closeCount ? open : close);
1855
+ result.push({ char: openCount > closeCount ? open : close, index: index < 0 ? 0 : index, kind: "quote" });
1856
+ }
1857
+ }
1858
+ const straightQuotes = Array.from(value).filter((char) => char === '"').length;
1859
+ if (straightQuotes % 2 === 1) {
1860
+ const index = value.indexOf('"');
1861
+ result.push({ char: '"', index: index < 0 ? 0 : index, kind: "quote" });
1862
+ }
1863
+ result.sort((left, right) => left.index - right.index);
1864
+ return result;
1865
+ }
1866
+ function checkUnpairedBrackets(context) {
1867
+ if (context.target.trim().length === 0) return;
1868
+ const unpaired = unpairedTokens(context.target);
1869
+ if (unpaired.length === 0) return;
1870
+ const first = unpaired[0];
1871
+ draft(context, {
1872
+ ruleId: "UNPAIRED_BRACKET",
1873
+ message: `\u8BD1\u6587\u62EC\u53F7/\u5F15\u53F7\u4E0D\u914D\u5BF9: ${unpaired.slice(0, 5).map((entry) => `'${entry.char}'`).join(", ")}`,
1874
+ evidence: `text: ${truncate(context.target)}`,
1875
+ suggestion: "\u68C0\u67E5\u62EC\u53F7\u3001\u5F15\u53F7\u662F\u5426\u6210\u5BF9\uFF08\u5168\u89D2/\u534A\u89D2\u9700\u5BF9\u5E94\uFF09",
1876
+ ...first === void 0 ? {} : { targetRange: { start: first.index, end: first.index + 1 } }
1877
+ });
1878
+ }
1879
+ var CJK_REDUPLICATION_OK = /* @__PURE__ */ new Set([
1880
+ "\u8C22\u8C22",
1881
+ "\u770B\u770B",
1882
+ "\u6162\u6162",
1883
+ "\u521A\u521A",
1884
+ "\u5988\u5988",
1885
+ "\u7238\u7238",
1886
+ "\u54E5\u54E5",
1887
+ "\u59D0\u59D0",
1888
+ "\u5F1F\u5F1F",
1889
+ "\u59B9\u59B9",
1890
+ "\u661F\u661F",
1891
+ "\u5929\u5929",
1892
+ "\u5E74\u5E74",
1893
+ "\u4EBA\u4EBA",
1894
+ "\u5904\u5904",
1895
+ "\u65F6\u65F6",
1896
+ "\u597D\u597D",
1897
+ "\u591A\u591A",
1898
+ "\u5E38\u5E38",
1899
+ "\u5F80\u5F80",
1900
+ "\u6E10\u6E10",
1901
+ "\u9ED8\u9ED8",
1902
+ "\u6084\u6084",
1903
+ "\u8F7B\u8F7B",
1904
+ "\u6DF1\u6DF1",
1905
+ "\u8FDC\u8FDC",
1906
+ "\u9AD8\u9AD8",
1907
+ "\u5927\u5927",
1908
+ "\u5C0F\u5C0F",
1909
+ "\u7EA2\u7EA2",
1910
+ "\u767D\u767D",
1911
+ "\u4E00\u4E00",
1912
+ "\u4E2A\u4E2A",
1913
+ "\u4EF6\u4EF6",
1914
+ "\u6761\u6761",
1915
+ "\u79CD\u79CD",
1916
+ "\u5C42\u5C42",
1917
+ "\u6B65\u6B65",
1918
+ "\u6B21\u6B21",
1919
+ "\u591C\u591C",
1920
+ "\u58F0\u58F0",
1921
+ "\u5B57\u5B57",
1922
+ "\u53E5\u53E5",
1923
+ "\u6735\u6735",
1924
+ "\u7247\u7247",
1925
+ "\u70B9\u70B9",
1926
+ "\u6EF4\u6EF4",
1927
+ "\u4E1D\u4E1D",
1928
+ "\u9635\u9635",
1929
+ "\u7FA4\u7FA4",
1930
+ "\u961F\u961F",
1931
+ "\u672C\u672C",
1932
+ "\u53F0\u53F0",
1933
+ "\u5BB6\u5BB6",
1934
+ "\u6237\u6237",
1935
+ "\u6751\u6751",
1936
+ "\u9547\u9547",
1937
+ "\u4EC5\u4EC5",
1938
+ "\u504F\u504F",
1939
+ "\u6070\u6070",
1940
+ "\u7EDF\u7EDF",
1941
+ "\u901A\u901A",
1942
+ "\u5386\u5386",
1943
+ "\u803F\u803F",
1944
+ "\u6829\u6829",
1945
+ "\u6D25\u6D25",
1946
+ "\u8DC3\u8DC3",
1947
+ "\u6124\u6124",
1948
+ "\u60BB\u60BB",
1949
+ "\u7A83\u7A83",
1950
+ "\u59D7\u59D7",
1951
+ "\u5F90\u5F90",
1952
+ "\u7F13\u7F13",
1953
+ "\u5189\u5189",
1954
+ "\u6ED4\u6ED4",
1955
+ "\u6EDA\u6EDA",
1956
+ "\u832B\u832B",
1957
+ "\u82CD\u82CD",
1958
+ "\u90C1\u90C1",
1959
+ "\u8471\u8471",
1960
+ "\u54D7\u54D7",
1961
+ "\u6C99\u6C99",
1962
+ "\u9686\u9686",
1963
+ "\u549A\u549A",
1964
+ "\u545C\u545C",
1965
+ "\u6C6A\u6C6A",
1966
+ "\u55B5\u55B5",
1967
+ "\u54A9\u54A9"
1968
+ ]);
1969
+ function checkDuplicateWords(context) {
1970
+ const target = context.target;
1971
+ if (target.trim().length === 0) return;
1972
+ let emitted = 0;
1973
+ const latin = /(?<![\p{L}\p{N}])([\p{L}][\p{L}'’_-]{1,})(\s+)\1(?![\p{L}\p{N}])/giu;
1974
+ for (const match of target.matchAll(latin)) {
1975
+ if (match.index === void 0) continue;
1976
+ draft(context, {
1977
+ ruleId: "DUPLICATE_WORD",
1978
+ message: `\u76F8\u90BB\u91CD\u590D\u8BCD: '${match[1]} ${match[1]}'`,
1979
+ evidence: truncate(match[0]),
1980
+ targetRange: { start: match.index, end: match.index + match[0].length }
1981
+ });
1982
+ emitted += 1;
1983
+ if (emitted >= 3) return;
1984
+ }
1985
+ const chars = Array.from(target);
1986
+ let cjkOffset = 0;
1987
+ for (let index = 0; index + 1 < chars.length; index += 1) {
1988
+ const first = chars[index];
1989
+ const second = chars[index + 1];
1990
+ if (first === void 0 || second === void 0 || first !== second || !CJK_RE.test(first)) {
1991
+ cjkOffset += first === void 0 ? 0 : charCount(first);
1992
+ continue;
1993
+ }
1994
+ const pair = first + second;
1995
+ if (!CJK_REDUPLICATION_OK.has(pair)) {
1996
+ draft(context, {
1997
+ ruleId: "DUPLICATE_WORD",
1998
+ message: `CJK \u91CD\u590D\u5B57: '${pair}'`,
1999
+ evidence: `text: ${truncate(target)}`,
2000
+ targetRange: { start: cjkOffset, end: cjkOffset + 2 }
2001
+ });
2002
+ emitted += 1;
2003
+ if (emitted >= 3) return;
2004
+ }
2005
+ cjkOffset += 2;
2006
+ index += 1;
2007
+ }
2008
+ }
2009
+ var PUNCT_DUP_EXEMPT = /* @__PURE__ */ new Set(["\u2026", "\u2014", "\uFF0D", "*", "=", "-", "~", "\uFF5E", "/"]);
2010
+ function checkPunctDup(context) {
2011
+ if (context.target.trim().length === 0) return;
2012
+ const regex = /([!?.,;:。?!,、;:])\1+/gu;
2013
+ let emitted = 0;
2014
+ for (const match of context.target.matchAll(regex)) {
2015
+ if (match.index === void 0) continue;
2016
+ if (PUNCT_DUP_EXEMPT.has(match[1] ?? "")) continue;
2017
+ draft(context, {
2018
+ ruleId: "PUNCT_DUP",
2019
+ message: `\u8FDE\u7EED\u91CD\u590D\u6807\u70B9: '${match[0]}'`,
2020
+ evidence: match[0],
2021
+ targetRange: { start: match.index, end: match.index + match[0].length }
2022
+ });
2023
+ emitted += 1;
2024
+ if (emitted >= 3) return;
2025
+ }
2026
+ }
2027
+ function checkSourceLeftover(context) {
2028
+ const { source, target } = context;
2029
+ if (source.trim().length === 0 || target.trim().length === 0) return;
2030
+ if (normalizeForCompare(source) === normalizeForCompare(target)) return;
2031
+ const normalizedTarget = normalizeForCompare(target);
2032
+ for (const match of source.normalize("NFKC").matchAll(/[\u4e00-\u9fff]{4,}/gu)) {
2033
+ const run = match[0];
2034
+ const index = normalizedTarget.indexOf(run);
2035
+ if (index < 0) continue;
2036
+ draft(context, {
2037
+ ruleId: "SOURCE_LEFTOVER",
2038
+ message: `\u8BD1\u6587\u6B8B\u7559\u6E90\u6587\u7247\u6BB5: '${truncate(run)}'`,
2039
+ evidence: `source run: ${truncate(run)}`,
2040
+ targetRange: { start: index, end: index + run.length }
2041
+ });
2042
+ return;
2043
+ }
2044
+ const tokens = wordTokens(source);
2045
+ if (tokens.length < 3) return;
2046
+ for (let start = 0; start + 3 <= tokens.length; start += 1) {
2047
+ const window = tokens.slice(start, start + 3);
2048
+ if (window.some((token) => token.length < 2)) continue;
2049
+ const pattern = new RegExp(`\\b${window.map(escapeRegExp).join("\\s+")}\\b`, "iu");
2050
+ const match = pattern.exec(target);
2051
+ if (!match || match.index === void 0) continue;
2052
+ draft(context, {
2053
+ ruleId: "SOURCE_LEFTOVER",
2054
+ message: `\u8BD1\u6587\u6B8B\u7559\u6E90\u6587\u7247\u6BB5: '${window.join(" ")}'`,
2055
+ evidence: `source window: ${window.join(" ")}`,
2056
+ targetRange: { start: match.index, end: match.index + match[0].length }
2057
+ });
2058
+ return;
2059
+ }
2060
+ }
2061
+ function checkTrailingColon(context) {
2062
+ const source = context.source.trimEnd();
2063
+ const target = context.target.trimEnd();
2064
+ if (target.length === 0) return;
2065
+ if (!source.endsWith(":") && !source.endsWith("\uFF1A")) return;
2066
+ if (target.endsWith(":") || target.endsWith("\uFF1A")) return;
2067
+ draft(context, {
2068
+ ruleId: "TRAILING_COLON",
2069
+ message: "\u6E90\u6587\u4EE5\u5192\u53F7\u7ED3\u5C3E\uFF0C\u8BD1\u6587\u672A\u4EE5\u5192\u53F7\u7ED3\u5C3E",
2070
+ evidence: `source tail: '${truncate(source.slice(-30))}' target tail: '${truncate(target.slice(-30))}'`,
2071
+ suggestion: "\u4FDD\u6301\u5217\u8868\u5F15\u5BFC\u5192\u53F7\u4E00\u81F4"
2072
+ });
2073
+ }
2074
+ var TERMINAL_CLASS = /* @__PURE__ */ new Map([
2075
+ [".", "period"],
2076
+ ["\u3002", "period"],
2077
+ ["\uFF0E", "period"],
2078
+ ["?", "question"],
2079
+ ["\uFF1F", "question"],
2080
+ ["!", "exclamation"],
2081
+ ["\uFF01", "exclamation"]
2082
+ ]);
2083
+ var TRAILING_CLOSERS = /* @__PURE__ */ new Set([")", "\uFF09", "]", "\u3011", "\u300D", "\u300F", "\u201D", "\u2019", '"', "'", "\u300B", "\u3009"]);
2084
+ function terminalPunctuation(value) {
2085
+ for (let index = value.length - 1; index >= 0; index -= 1) {
2086
+ const char = value[index];
2087
+ if (char === void 0) return void 0;
2088
+ if (/\s/u.test(char)) continue;
2089
+ if (TRAILING_CLOSERS.has(char)) continue;
2090
+ const className = TERMINAL_CLASS.get(char);
2091
+ if (className === void 0) return void 0;
2092
+ return { char, index, className };
2093
+ }
2094
+ return void 0;
2095
+ }
2096
+ function checkSentenceEndPunct(context) {
2097
+ const source = context.source.trimEnd();
2098
+ const target = context.target.trimEnd();
2099
+ if (source.length === 0 || target.length === 0) return;
2100
+ if (source.endsWith("...") || source.endsWith("\u2026")) return;
2101
+ const sourceTerminal = terminalPunctuation(source);
2102
+ if (sourceTerminal === void 0) return;
2103
+ if (sourceTerminal.className === "period" && wordTokens(source).length <= 2) return;
2104
+ const targetTerminal = terminalPunctuation(target);
2105
+ if (targetTerminal === void 0) {
2106
+ draft(context, {
2107
+ ruleId: "SENTENCE_END_PUNCT",
2108
+ message: "\u8BD1\u6587\u7F3A\u5C11\u53E5\u672B\u6807\u70B9",
2109
+ evidence: `source ends with '${sourceTerminal.char}', target has no sentence-final punctuation`,
2110
+ suggestion: context.targetIsCjk ? "\u8865\u5168\u5168\u89D2\u53E5\u672B\u6807\u70B9" : "\u8865\u5168\u53E5\u672B\u6807\u70B9"
2111
+ });
2112
+ return;
2113
+ }
2114
+ if (targetTerminal.className === sourceTerminal.className) return;
2115
+ draft(context, {
2116
+ ruleId: "SENTENCE_END_PUNCT",
2117
+ message: `\u53E5\u672B\u6807\u70B9\u7C7B\u578B\u4E0D\u5339\u914D: \u6E90\u6587 '${sourceTerminal.char}' / \u8BD1\u6587 '${targetTerminal.char}'`,
2118
+ evidence: `source tail: '${truncate(source.slice(-20))}' target tail: '${truncate(target.slice(-20))}'`,
2119
+ targetRange: { start: targetTerminal.index, end: targetTerminal.index + 1 }
2120
+ });
2121
+ }
2122
+ function checkWhitespace(context) {
2123
+ const raw = context.targetInner;
2124
+ if (raw.length === 0) return;
2125
+ if (raw !== raw.trim()) {
2126
+ draft(context, {
2127
+ ruleId: "WHITESPACE",
2128
+ message: "\u8BD1\u6587\u5B58\u5728\u9996\u5C3E\u7A7A\u767D",
2129
+ evidence: "target inner starts/ends with whitespace",
2130
+ suggestion: "\u5220\u9664\u9996\u5C3E\u7A7A\u767D"
2131
+ });
2132
+ }
2133
+ const doubleSpace = /[ \t]{2,}/u.exec(raw);
2134
+ if (doubleSpace && doubleSpace.index !== void 0) {
2135
+ draft(context, {
2136
+ ruleId: "WHITESPACE",
2137
+ message: "\u8BD1\u6587\u5B58\u5728\u8FDE\u7EED\u7A7A\u683C",
2138
+ evidence: "target contains 2+ consecutive spaces/tabs",
2139
+ targetRange: { start: doubleSpace.index, end: doubleSpace.index + doubleSpace[0].length }
2140
+ });
2141
+ }
2142
+ const ideographic = raw.indexOf("\u3000");
2143
+ if (ideographic >= 0) {
2144
+ draft(context, {
2145
+ ruleId: "WHITESPACE",
2146
+ message: "\u8BD1\u6587\u542B\u5168\u89D2\u7A7A\u683C\uFF08U+3000\uFF09",
2147
+ evidence: "target contains ideographic space",
2148
+ targetRange: { start: ideographic, end: ideographic + 1 }
2149
+ });
2150
+ }
2151
+ const nbsp = raw.indexOf("\xA0");
2152
+ if (nbsp >= 0) {
2153
+ draft(context, {
2154
+ ruleId: "WHITESPACE",
2155
+ message: "\u8BD1\u6587\u542B\u4E0D\u6362\u884C\u7A7A\u683C\uFF08U+00A0\uFF09",
2156
+ evidence: "target contains non-breaking space",
2157
+ targetRange: { start: nbsp, end: nbsp + 1 }
2158
+ });
2159
+ }
2160
+ }
2161
+ function checkFullwidthHalfwidth(context) {
2162
+ const target = context.target;
2163
+ if (target.trim().length === 0) return;
2164
+ const fullwidth = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/u.exec(target);
2165
+ if (fullwidth && fullwidth.index !== void 0) {
2166
+ draft(context, {
2167
+ ruleId: "FULLWIDTH_HALFWIDTH",
2168
+ message: "\u8BD1\u6587\u4E2D\u5B57\u6BCD/\u6570\u5B57\u4F7F\u7528\u5168\u89D2\u5F62\u5F0F",
2169
+ evidence: `found '${fullwidth[0]}'`,
2170
+ suggestion: "\u5B57\u6BCD\u4E0E\u6570\u5B57\u901A\u5E38\u4F7F\u7528\u534A\u89D2\u5F62\u5F0F",
2171
+ targetRange: { start: fullwidth.index, end: fullwidth.index + 1 }
2172
+ });
2173
+ return;
2174
+ }
2175
+ if (!context.targetIsCjk) return;
2176
+ const mixed = /[\u4e00-\u9fff][,;:!?]|[,;:!?][\u4e00-\u9fff]/u.exec(target);
2177
+ if (mixed === null || mixed.index === void 0) return;
2178
+ draft(context, {
2179
+ ruleId: "FULLWIDTH_HALFWIDTH",
2180
+ message: "\u4E2D\u6587\u4E0E\u534A\u89D2\u6807\u70B9\u6DF7\u6392",
2181
+ evidence: `found '${mixed[0]}'`,
2182
+ suggestion: "\u4E2D\u6587\u8BED\u5883\u4F7F\u7528\u5168\u89D2\u6807\u70B9",
2183
+ targetRange: { start: mixed.index, end: mixed.index + mixed[0].length }
2184
+ });
2185
+ }
2186
+ function checkLengthRatio(context) {
2187
+ const sourceLength = charCount(context.source.trim());
2188
+ const targetLength = charCount(context.target.trim());
2189
+ if (sourceLength === 0 || targetLength === 0) return;
2190
+ const sourceIsCjk = isCjkHeavy(context.source);
2191
+ const targetIsCjk = context.targetIsCjk;
2192
+ let min = 0.4;
2193
+ let max = 2.5;
2194
+ if (targetIsCjk && !sourceIsCjk) {
2195
+ min = 0.15;
2196
+ max = 1.2;
2197
+ } else if (sourceIsCjk && !targetIsCjk) {
2198
+ min = 0.5;
2199
+ max = 3;
2200
+ } else if (sourceIsCjk && targetIsCjk) {
2201
+ min = 0.5;
2202
+ max = 2;
2203
+ }
2204
+ const ratio = targetLength / sourceLength;
2205
+ if (ratio >= min && ratio <= max) return;
2206
+ draft(context, {
2207
+ ruleId: "LENGTH_RATIO",
2208
+ message: `\u8BD1\u6587/\u6E90\u6587\u957F\u5EA6\u6BD4 ${ratio.toFixed(2)} \u8D85\u51FA\u5EFA\u8BAE\u533A\u95F4 [${min}, ${max}]`,
2209
+ evidence: `source length: ${sourceLength}, target length: ${targetLength}`,
2210
+ suggestion: "\u786E\u8BA4\u662F\u5426\u5B58\u5728\u6F0F\u8BD1\u6216\u5197\u4F59\uFF08\u4EC5\u63D0\u793A\uFF09"
2211
+ });
2212
+ }
2213
+ function isApproved(entry) {
2214
+ return entry.status === void 0 || entry.status === "approved";
2215
+ }
2216
+ function longestHits(hits) {
2217
+ return hits.filter(
2218
+ (hit) => !hits.some(
2219
+ (other) => other !== hit && other.start <= hit.start && other.end >= hit.end && other.end - other.start > hit.end - hit.start
2220
+ )
2221
+ );
2222
+ }
2223
+ function targetMentionsTerm(entry, targetText, targetHits) {
2224
+ if (targetHits.some((hit) => hit.entry.id === entry.id)) return true;
2225
+ const expected = (entry.target ?? "").trim();
2226
+ if (expected.length === 0) return true;
2227
+ if (entry.caseSensitive === true) return targetText.includes(expected);
2228
+ return normalizeForCompare(targetText).includes(normalizeForCompare(expected));
2229
+ }
2230
+ function checkTermRules(context) {
2231
+ const termHits = context.termHits;
2232
+ if (termHits === void 0) return;
2233
+ const sourceHits = longestHits(termHits.source);
2234
+ const targetHits = termHits.target;
2235
+ const emittedTermIds = /* @__PURE__ */ new Set();
2236
+ for (const hit of sourceHits) {
2237
+ const entry = hit.entry;
2238
+ if (entry.forbidden === true) {
2239
+ draft(context, {
2240
+ ruleId: "TB_SOURCE_FORBIDDEN",
2241
+ message: `\u6E90\u6587\u51FA\u73B0\u7981\u7528\u672F\u8BED: '${entry.source}'`,
2242
+ evidence: `source term: ${truncate(entry.source)}`,
2243
+ suggestion: "\u66FF\u6362\u6E90\u6587\u6216\u786E\u8BA4\u672F\u8BED\u5E93\u72B6\u6001",
2244
+ sourceRange: { start: hit.start, end: hit.end },
2245
+ termId: entry.id
2246
+ });
2247
+ emittedTermIds.add(`src:${entry.id}`);
2248
+ continue;
2249
+ }
2250
+ if (!isApproved(entry)) continue;
2251
+ if ((entry.target ?? "").trim().length === 0) continue;
2252
+ if (targetMentionsTerm(entry, context.target, targetHits)) continue;
2253
+ draft(context, {
2254
+ ruleId: "TERM_MISSING",
2255
+ message: `\u8BD1\u6587\u672A\u4F7F\u7528 approved \u672F\u8BED '${entry.source}' \u2192 '${entry.target}'`,
2256
+ evidence: `source term hit: ${truncate(entry.source)}`,
2257
+ suggestion: entry.target,
2258
+ sourceRange: { start: hit.start, end: hit.end },
2259
+ termId: entry.id
2260
+ });
2261
+ }
2262
+ for (const entry of context.glossary.values()) {
2263
+ if (entry.forbidden !== true) continue;
2264
+ const expected = (entry.target ?? "").trim();
2265
+ if (expected.length === 0) continue;
2266
+ const targetHit = targetHits.find((hit) => hit.entry.id === entry.id);
2267
+ const mentioned = targetHit !== void 0 || (entry.caseSensitive === true ? context.target.includes(expected) : normalizeForCompare(context.target).includes(normalizeForCompare(expected)));
2268
+ if (!mentioned) continue;
2269
+ if (emittedTermIds.has(`tgt:${entry.id}`)) continue;
2270
+ draft(context, {
2271
+ ruleId: "TB_FORBIDDEN_USED",
2272
+ message: `\u8BD1\u6587\u4F7F\u7528\u4E86\u7981\u7528\u672F\u8BED: '${entry.source}' \u2192 '${entry.target}'`,
2273
+ evidence: `target term: ${truncate(entry.target)}`,
2274
+ ...targetHit === void 0 ? {} : { targetRange: { start: targetHit.start, end: targetHit.end } },
2275
+ termId: entry.id
2276
+ });
2277
+ }
2278
+ }
2279
+ var CHECK_GROUPS = [
2280
+ { ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
2281
+ { ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
2282
+ { ids: ["PLACEHOLDER_MISSING", "PLACEHOLDER_EXTRA"], run: checkPlaceholders },
2283
+ { ids: ["TAG_MISMATCH"], run: checkTags },
2284
+ { ids: ["URL_CHANGED"], run: checkUrls },
2285
+ { ids: ["TB_SOURCE_FORBIDDEN", "TB_FORBIDDEN_USED", "TERM_MISSING"], run: checkTermRules },
2286
+ { ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
2287
+ { ids: ["NUMBER_MISMATCH"], run: checkNumbers },
2288
+ { ids: ["DATE_MISMATCH"], run: checkDates },
2289
+ { ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
2290
+ { ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
2291
+ { ids: ["PUNCT_DUP"], run: checkPunctDup },
2292
+ { ids: ["TRAILING_COLON"], run: checkTrailingColon },
2293
+ { ids: ["SENTENCE_END_PUNCT"], run: checkSentenceEndPunct },
2294
+ { ids: ["WHITESPACE"], run: checkWhitespace },
2295
+ { ids: ["FULLWIDTH_HALFWIDTH"], run: checkFullwidthHalfwidth },
2296
+ { ids: ["LENGTH_RATIO"], run: checkLengthRatio }
2297
+ ];
2298
+ function checkEmptyTargetGroup(context) {
2299
+ const source = context.source;
2300
+ if (source.trim().length === 0) return;
2301
+ const targetMissing = context.segment.targetRange === null && context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2302
+ const empty = context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2303
+ if (!targetMissing && !empty) return;
2304
+ draft(context, {
2305
+ ruleId: "EMPTY_TARGET",
2306
+ message: targetMissing ? "\u76EE\u6807\u7F3A\u5931\uFF08target \u5143\u7D20\u4E0D\u5B58\u5728\uFF09" : "\u8BD1\u6587\u4E3A\u7A7A\uFF08\u6E90\u6587\u975E\u7A7A\uFF09",
2307
+ evidence: `source: ${truncate(source)}`
2308
+ });
2309
+ }
2310
+ function checkUntranslatedCopyGroup(context) {
2311
+ const { source, target } = context;
2312
+ if (source.trim().length === 0 || target.trim().length === 0) return;
2313
+ if (normalizeForCompare(source) !== normalizeForCompare(target)) return;
2314
+ const tokens = wordTokens(source);
2315
+ const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
2316
+ if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= 4) return;
2317
+ draft(context, {
2318
+ ruleId: "UNTRANSLATED_COPY",
2319
+ message: "\u8BD1\u6587\u4E0E\u6E90\u6587\u5B8C\u5168\u76F8\u540C\uFF08\u672A\u7FFB\u8BD1\uFF09",
2320
+ evidence: `text: ${truncate(source)}`,
2321
+ targetRange: { start: 0, end: charCount(target) }
2322
+ });
2323
+ }
2324
+ function isApplicable(ruleId, segment) {
2325
+ const hasSource = segment.sourceText.trim().length > 0;
2326
+ const hasTargetText = segment.targetText.trim().length > 0;
2327
+ const hasTargetContainer = segment.targetRange !== null;
2328
+ switch (ruleId) {
2329
+ case "EMPTY_TARGET":
2330
+ return hasSource;
2331
+ case "TAG_MISMATCH":
2332
+ return hasSource && (hasTargetText || hasTargetContainer);
2333
+ case "WHITESPACE":
2334
+ return hasTargetContainer || segment.targetInner.length > 0;
2335
+ case "UNTRANSLATED_COPY":
2336
+ case "PLACEHOLDER_MISSING":
2337
+ case "PLACEHOLDER_EXTRA":
2338
+ case "URL_CHANGED":
2339
+ case "TB_SOURCE_FORBIDDEN":
2340
+ case "TB_FORBIDDEN_USED":
2341
+ case "UNPAIRED_BRACKET":
2342
+ case "NUMBER_MISMATCH":
2343
+ case "DATE_MISMATCH":
2344
+ case "DUPLICATE_WORD":
2345
+ case "TERM_MISSING":
2346
+ case "SOURCE_LEFTOVER":
2347
+ case "PUNCT_DUP":
2348
+ case "TRAILING_COLON":
2349
+ case "SENTENCE_END_PUNCT":
2350
+ case "FULLWIDTH_HALFWIDTH":
2351
+ case "LENGTH_RATIO":
2352
+ return hasSource && hasTargetText;
2353
+ default:
2354
+ return hasSource && hasTargetText;
2355
+ }
2356
+ }
2357
+ function termAssetAvailable(options) {
2358
+ if (options.segmentTermHits !== void 0 && options.segmentTermHits.size > 0) return true;
2359
+ return options.glossary !== void 0 && options.glossary.length > 0;
2360
+ }
2361
+ function resolveEnabledRules(options) {
2362
+ if (options.enabledRules !== void 0) {
2363
+ const enabled2 = /* @__PURE__ */ new Set();
2364
+ for (const ruleId of options.enabledRules) {
2365
+ if (RULE_INDEX.has(ruleId)) enabled2.add(ruleId);
2366
+ }
2367
+ return enabled2;
2368
+ }
2369
+ const enabled = new Set(DEFAULT_ENABLED);
2370
+ if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
2371
+ return enabled;
2372
+ }
2373
+ function termGlossary(options) {
2374
+ const entries = /* @__PURE__ */ new Map();
2375
+ for (const entry of options.glossary ?? []) entries.set(entry.id, entry);
2376
+ for (const hits of options.segmentTermHits?.values() ?? []) {
2377
+ for (const hit of [...hits.source, ...hits.target]) {
2378
+ if (!entries.has(hit.entry.id)) entries.set(hit.entry.id, hit.entry);
2379
+ }
2380
+ }
2381
+ return entries;
2382
+ }
2383
+ function buildContext(segment, options, glossary) {
2384
+ const termHits = options.segmentTermHits?.get(segment.id);
2385
+ return {
2386
+ segment,
2387
+ source: segment.sourceText,
2388
+ target: segment.targetText,
2389
+ sourceInner: segment.sourceInner,
2390
+ targetInner: segment.targetInner,
2391
+ targetIsCjk: isCjkHeavy(segment.targetText),
2392
+ glossary,
2393
+ drafts: [],
2394
+ ...termHits === void 0 ? {} : { termHits }
2395
+ };
2396
+ }
2397
+ function toIssue(draft2, segment, ruleId) {
2398
+ return {
2399
+ ruleId,
2400
+ severity: RULE_SEVERITY.get(ruleId) ?? "major",
2401
+ segmentId: segment.id,
2402
+ segmentIndex: segment.index,
2403
+ message: draft2.message,
2404
+ evidence: draft2.evidence,
2405
+ ...draft2.suggestion === void 0 ? {} : { suggestion: draft2.suggestion },
2406
+ ...draft2.sourceRange === void 0 ? {} : { sourceRange: draft2.sourceRange },
2407
+ ...draft2.targetRange === void 0 ? {} : { targetRange: draft2.targetRange },
2408
+ origin: "deterministic",
2409
+ ...draft2.termId === void 0 ? {} : { termId: draft2.termId }
2410
+ };
2411
+ }
2412
+ function compareIssues(left, right) {
2413
+ const leftRule = RULE_INDEX.get(left.ruleId) ?? Number.MAX_SAFE_INTEGER;
2414
+ const rightRule = RULE_INDEX.get(right.ruleId) ?? Number.MAX_SAFE_INTEGER;
2415
+ if (leftRule !== rightRule) return leftRule - rightRule;
2416
+ if (left.segmentIndex !== right.segmentIndex) return left.segmentIndex - right.segmentIndex;
2417
+ if (left.segmentId !== right.segmentId) return left.segmentId < right.segmentId ? -1 : 1;
2418
+ const leftStart = left.targetRange?.start ?? left.sourceRange?.start ?? 0;
2419
+ const rightStart = right.targetRange?.start ?? right.sourceRange?.start ?? 0;
2420
+ return leftStart - rightStart;
2421
+ }
2422
+ function runDeterministicChecks(segments, options = {}) {
2423
+ const enabled = resolveEnabledRules(options);
2424
+ const termAvailable = termAssetAvailable(options);
2425
+ const glossary = termGlossary(options);
2426
+ const state = /* @__PURE__ */ new Map();
2427
+ for (const rule of RULES) {
2428
+ if (!enabled.has(rule.id)) continue;
2429
+ if (rule.requiresTerms === true && !termAvailable) {
2430
+ state.set(rule.id, {
2431
+ status: "skipped",
2432
+ reason: "no glossary/segmentTermHits provided; term rules were not executed",
2433
+ checkedSegments: 0
2434
+ });
2435
+ continue;
2436
+ }
2437
+ state.set(rule.id, { status: "executed", reason: "", checkedSegments: 0 });
2438
+ }
2439
+ const issues = [];
2440
+ for (const segment of segments) {
2441
+ const context = buildContext(segment, options, glossary);
2442
+ const applicable = /* @__PURE__ */ new Set();
2443
+ for (const ruleId of enabled) {
2444
+ if (state.get(ruleId)?.status !== "executed") continue;
2445
+ if (isApplicable(ruleId, segment)) applicable.add(ruleId);
2446
+ }
2447
+ if (applicable.size === 0) continue;
2448
+ const groupsRun = /* @__PURE__ */ new Set();
2449
+ const failedRules = /* @__PURE__ */ new Set();
2450
+ for (const group of CHECK_GROUPS) {
2451
+ const groupRules = group.ids.filter((ruleId) => applicable.has(ruleId));
2452
+ if (groupRules.length === 0) continue;
2453
+ const key = group.ids.join("|");
2454
+ if (groupsRun.has(key)) continue;
2455
+ groupsRun.add(key);
2456
+ try {
2457
+ group.run(context);
2458
+ } catch (caught) {
2459
+ const message = caught instanceof Error ? caught.message : String(caught);
2460
+ for (const ruleId of groupRules) failedRules.add(`${ruleId}\0${message}`);
2461
+ }
2462
+ }
2463
+ for (const ruleId of applicable) {
2464
+ const execution = state.get(ruleId);
2465
+ if (execution === void 0) continue;
2466
+ const failed = Array.from(failedRules).some((entry) => entry.startsWith(`${ruleId}\0`));
2467
+ if (failed) {
2468
+ const message = Array.from(failedRules).find((entry) => entry.startsWith(`${ruleId}\0`))?.split("\0")[1] ?? "rule threw";
2469
+ state.set(ruleId, {
2470
+ status: "failed",
2471
+ reason: `rule threw: ${message}`,
2472
+ checkedSegments: execution.checkedSegments
2473
+ });
2474
+ continue;
2475
+ }
2476
+ execution.checkedSegments += 1;
2477
+ }
2478
+ for (const draft2 of context.drafts) {
2479
+ if (!applicable.has(draft2.ruleId)) continue;
2480
+ issues.push(toIssue(draft2, segment, draft2.ruleId));
2481
+ }
2482
+ }
2483
+ const rules = [];
2484
+ for (const rule of RULES) {
2485
+ const execution = state.get(rule.id);
2486
+ if (execution === void 0) continue;
2487
+ const status = execution.status === "executed" && execution.checkedSegments === 0 ? "not_applicable" : execution.status;
2488
+ rules.push({
2489
+ ruleId: rule.id,
2490
+ status,
2491
+ reason: status === "not_applicable" ? "no applicable segments" : execution.reason,
2492
+ checkedSegments: execution.checkedSegments
2493
+ });
2494
+ }
2495
+ issues.sort(compareIssues);
2496
+ const coverageIncomplete = rules.some((rule) => rule.status === "skipped" || rule.status === "failed");
2497
+ const anyExecuted = rules.some(
2498
+ (rule) => rule.status === "executed" || rule.status === "not_applicable"
2499
+ );
2500
+ const checkedSegments = anyExecuted ? segments.length : 0;
2501
+ return {
2502
+ issues,
2503
+ totalSegments: segments.length,
2504
+ checkedSegments,
2505
+ coverage: coverageIncomplete ? "incomplete" : "complete",
2506
+ rules
2507
+ };
2508
+ }
2509
+
2510
+ // packages/l10n-core/src/workflows/check.ts
2511
+ function termMatchOptions(document, segment) {
2512
+ const file = document.files[segment.location.fileIndex];
2513
+ const options = {};
2514
+ if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
2515
+ if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
2516
+ return options;
2517
+ }
2518
+ function checkDocument(document, options = {}) {
2519
+ const glossary = options.glossary ?? [];
2520
+ const segmentTermHits = /* @__PURE__ */ new Map();
2521
+ if (glossary.length > 0) {
2522
+ for (const segment of document.segments) {
2523
+ const termOptions = termMatchOptions(document, segment);
2524
+ const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
2525
+ const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
2526
+ segmentTermHits.set(segment.id, {
2527
+ source: sourceMatches.hits,
2528
+ target: targetMatches.hits
2529
+ });
2530
+ }
2531
+ }
2532
+ const qaOptions = {
2533
+ ...options.glossary === void 0 ? {} : { glossary: options.glossary },
2534
+ ...segmentTermHits.size === 0 ? {} : { segmentTermHits },
2535
+ ...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules }
2536
+ };
2537
+ const qa = runDeterministicChecks(document.segments, qaOptions);
2538
+ return { qa, segmentTermHits };
2539
+ }
2540
+
2541
+ // packages/l10n-core/src/workflows/translate.ts
2542
+ var DEFAULT_THRESHOLD2 = 75;
2543
+ function bestMatch(matches) {
2544
+ let best;
2545
+ for (const match of matches) {
2546
+ if (best === void 0 || match.matchRate > best.matchRate) best = match;
2547
+ else if (match.matchRate === best.matchRate && match.similarity > best.similarity) best = match;
2548
+ }
2549
+ return best;
2550
+ }
2551
+ function translateDocument(document, tmEntries, options = {}) {
2552
+ const threshold = options.threshold ?? DEFAULT_THRESHOLD2;
2553
+ const warnings = [];
2554
+ const values = /* @__PURE__ */ new Map();
2555
+ let skipped = 0;
2556
+ for (const segment of document.segments) {
2557
+ if (segment.targetText.trim().length > 0) continue;
2558
+ if (segment.targetRange === null) {
2559
+ warnings.push(`${segment.id}: target missing; cannot fill`);
2560
+ skipped += 1;
2561
+ continue;
2562
+ }
2563
+ if (segment.targetTags.length > 0) {
2564
+ warnings.push(
2565
+ `${segment.id}: target contains inline tags; skipped (fill tagged segments manually)`
2566
+ );
2567
+ skipped += 1;
2568
+ continue;
2569
+ }
2570
+ if (segment.targetNode !== void 0 && segment.targetNode.end === segment.targetNode.openEnd) {
2571
+ warnings.push(`${segment.id}: self-closing target; skipped`);
2572
+ skipped += 1;
2573
+ continue;
2574
+ }
2575
+ if (segment.sourceText.trim().length === 0) {
2576
+ skipped += 1;
2577
+ continue;
2578
+ }
2579
+ const file = document.files[segment.location.fileIndex];
2580
+ const matchOptions = {
2581
+ threshold,
2582
+ maxMatches: 1,
2583
+ ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
2584
+ ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
2585
+ };
2586
+ let matches;
2587
+ try {
2588
+ matches = options.tmIndex ? options.tmIndex.query(segment.sourceText, matchOptions) : matchTm(tmEntries, segment.sourceText, matchOptions);
2589
+ } catch (caught) {
2590
+ const message = caught instanceof Error ? caught.message : String(caught);
2591
+ warnings.push(`${segment.id}: TM matching failed: ${message}`);
2592
+ skipped += 1;
2593
+ continue;
2594
+ }
2595
+ const match = bestMatch(matches);
2596
+ const target = match?.entry.target.trim() ?? "";
2597
+ if (match === void 0 || match.matchType === "no_match" || match.matchRate < threshold || target.length === 0) {
2598
+ skipped += 1;
2599
+ continue;
2600
+ }
2601
+ values.set(segment.id, target);
2602
+ }
2603
+ if (values.size === 0) {
2604
+ return { xml: document.xml, applied: 0, skipped, warnings };
2605
+ }
2606
+ const patch = patchDocument(document, textEdits(document, values));
2607
+ if (patch.errors.length > 0) {
2608
+ const details = patch.errors.map((error) => `${error.segmentId}: ${error.message}`);
2609
+ warnings.push(...details.map((detail) => `patch failed: ${detail}`));
2610
+ return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
2611
+ }
2612
+ let originalHash;
2613
+ let patchedHash;
2614
+ try {
2615
+ originalHash = structureSummary(document.xml).hash;
2616
+ patchedHash = structureSummary(patch.xml).hash;
2617
+ } catch (caught) {
2618
+ const message = caught instanceof Error ? caught.message : String(caught);
2619
+ warnings.push(`structure check failed: ${message}`);
2620
+ return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
2621
+ }
2622
+ if (originalHash !== patchedHash) {
2623
+ warnings.push("structure summary changed after fill; refusing to write output");
2624
+ return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
2625
+ }
2626
+ let xml = patch.xml;
2627
+ if (options.setSdlState === true && document.format === "sdlxliff") {
2628
+ const conf = options.sdlConf ?? "Translated";
2629
+ try {
2630
+ const patchedDocument = parseXliff(xml);
2631
+ const updates = new Map(
2632
+ [...values.keys()].map((segmentId) => [segmentId, { conf, percent: "100" }])
2633
+ );
2634
+ const state = patchSdlStates(patchedDocument, updates);
2635
+ if (state.errors.length > 0) {
2636
+ warnings.push(...state.errors.map((error) => `state patch failed: ${error.segmentId}: ${error.message}`));
2637
+ return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
2638
+ }
2639
+ xml = state.xml;
2640
+ } catch (caught) {
2641
+ const message = caught instanceof Error ? caught.message : String(caught);
2642
+ warnings.push(`state patch failed: ${message}`);
2643
+ return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
2644
+ }
2645
+ }
2646
+ return { xml, applied: patch.applied, skipped, warnings };
2647
+ }
2648
+
2649
+ // packages/l10n-core/src/formats/bilingual.ts
2650
+ import ExcelJS from "exceljs";
2651
+ function clean(value) {
2652
+ return value.replace(/^\uFEFF/u, "");
2653
+ }
2654
+ function normalizeHeader(value) {
2655
+ return value.trim().toLowerCase().replace(/[\s_-]+/g, "");
2656
+ }
2657
+ var SOURCE_HEADERS2 = /* @__PURE__ */ new Set(["source", "src", "source text", "\u6E90\u6587", "\u539F\u6587", "\u6E90\u8BED\u8A00"]);
2658
+ var TARGET_HEADERS2 = /* @__PURE__ */ new Set(["target", "tgt", "target text", "\u8BD1\u6587", "\u76EE\u6A19", "\u76EE\u6807\u8BED\u8A00"]);
2659
+ var ID_HEADERS = /* @__PURE__ */ new Set(["id", "segment id", "segmentid", "\u7F16\u53F7"]);
2660
+ var LOCATION_HEADERS = /* @__PURE__ */ new Set(["location", "\u4F4D\u7F6E", "\u6587\u4EF6", "file"]);
2661
+ function findColumn(headers, candidates) {
2662
+ for (let index = 0; index < headers.length; index += 1) {
2663
+ const normalized = normalizeHeader(headers[index] ?? "");
2664
+ if (candidates.has(normalized)) return index;
2665
+ }
2666
+ return -1;
2667
+ }
2668
+ function parseBilingualCsv(text, _options = {}) {
2669
+ const cleaned = clean(text);
2670
+ const rows = parseCsvRows(cleaned);
2671
+ if (rows.length === 0) return { rows: [], warnings: ["bilingual table is empty"] };
2672
+ const first = rows[0] ?? [];
2673
+ const sourceIndex = findColumn(first, SOURCE_HEADERS2);
2674
+ const targetIndex = findColumn(first, TARGET_HEADERS2);
2675
+ const hasHeader = sourceIndex >= 0 && targetIndex >= 0;
2676
+ const idIndex = hasHeader ? findColumn(first, ID_HEADERS) : -1;
2677
+ const locationIndex = hasHeader ? findColumn(first, LOCATION_HEADERS) : -1;
2678
+ const dataRows = hasHeader ? rows.slice(1) : rows;
2679
+ const warnings = [];
2680
+ const result = [];
2681
+ const source = hasHeader ? sourceIndex : 0;
2682
+ const target = hasHeader ? targetIndex : 1;
2683
+ dataRows.forEach((row, index) => {
2684
+ const sourceText = row[source] ?? "";
2685
+ const targetText = row[target] ?? "";
2686
+ if (sourceText.trim() === "" && targetText.trim() === "") return;
2687
+ if (sourceText.trim() === "") warnings.push(`row ${index + (hasHeader ? 2 : 1)}: empty source`);
2688
+ const id = (idIndex >= 0 ? row[idIndex] : void 0)?.trim() || `row-${index + 1}`;
2689
+ result.push({
2690
+ id,
2691
+ source: sourceText,
2692
+ target: targetText,
2693
+ ...locationIndex >= 0 && row[locationIndex] ? { location: row[locationIndex] } : {}
2694
+ });
2695
+ });
2696
+ if (result.length === 0) warnings.push("no usable bilingual rows");
2697
+ return { rows: result, warnings };
2698
+ }
2699
+ function parseBilingualTxt(text, options = {}) {
2700
+ const cleaned = clean(text);
2701
+ const lines = cleaned.split(/\r?\n/u);
2702
+ const rows = [];
2703
+ const warnings = [];
2704
+ lines.forEach((line, index) => {
2705
+ if (line.trim() === "") return;
2706
+ const parts = line.split(" ");
2707
+ if (parts.length < 2) {
2708
+ warnings.push(`line ${index + 1}: expected TAB-separated source and target`);
2709
+ return;
2710
+ }
2711
+ rows.push({ id: `line-${index + 1}`, source: parts[0] ?? "", target: parts.slice(1).join(" ") });
2712
+ });
2713
+ if (rows.length === 0) warnings.push("no usable bilingual rows");
2714
+ return { rows, warnings };
2715
+ }
2716
+ function parseBilingualFile(filename, text, options = {}) {
2717
+ const lower = filename.toLowerCase();
2718
+ if (lower.endsWith(".csv")) return parseBilingualCsv(text, options);
2719
+ if (lower.endsWith(".txt") || lower.endsWith(".tsv")) return parseBilingualTxt(text, options);
2720
+ throw new Error(`unsupported bilingual table format: ${filename}`);
2721
+ }
2722
+ function escapeField(value, delimiter) {
2723
+ if (value.includes('"') || value.includes("\n") || value.includes("\r") || value.includes(delimiter)) {
2724
+ return `"${value.replaceAll('"', '""')}"`;
2725
+ }
2726
+ return value;
2727
+ }
2728
+ function writeBilingualCsv(rows, options = {}) {
2729
+ const delimiter = options.delimiter ?? ",";
2730
+ const lines = [];
2731
+ if (options.header !== false) lines.push(["id", "source", "target", "location"].join(delimiter));
2732
+ for (const row of rows) {
2733
+ lines.push(
2734
+ [row.id, row.source, row.target, row.location ?? ""].map((value) => escapeField(value, delimiter)).join(delimiter)
2735
+ );
2736
+ }
2737
+ return `${lines.join("\n")}
2738
+ `;
2739
+ }
2740
+ function matrixToCsv(matrix) {
2741
+ return matrix.map((row) => row.map((value) => escapeField(value, ",")).join(",")).join("\n");
2742
+ }
2743
+ async function parseBilingualXlsx(buffer, options = {}) {
2744
+ const workbook = new ExcelJS.Workbook();
2745
+ await workbook.xlsx.load(buffer);
2746
+ const worksheet = workbook.worksheets[0];
2747
+ if (!worksheet) return { rows: [], warnings: ["XLSX contains no worksheets"] };
2748
+ const matrix = [];
2749
+ worksheet.eachRow({ includeEmpty: false }, (row) => {
2750
+ const values = [];
2751
+ for (let index = 1; index <= row.cellCount; index += 1) {
2752
+ values.push(row.getCell(index).text ?? "");
2753
+ }
2754
+ matrix.push(values);
2755
+ });
2756
+ const parsed = parseBilingualCsv(matrixToCsv(matrix), options);
2757
+ const warnings = [...parsed.warnings];
2758
+ if (workbook.worksheets.length > 1) {
2759
+ warnings.push(`XLSX has ${workbook.worksheets.length} worksheets; only the first is used`);
2760
+ }
2761
+ return { rows: parsed.rows, warnings };
2762
+ }
2763
+ async function writeBilingualXlsx(rows) {
2764
+ const workbook = new ExcelJS.Workbook();
2765
+ const worksheet = workbook.addWorksheet("bilingual");
2766
+ worksheet.addRow(["id", "source", "target", "location"]);
2767
+ for (const row of rows) {
2768
+ worksheet.addRow([row.id, row.source, row.target, row.location ?? ""]);
2769
+ }
2770
+ worksheet.getRow(1).font = { bold: true };
2771
+ worksheet.columns = [
2772
+ { width: 18 },
2773
+ { width: 60 },
2774
+ { width: 60 },
2775
+ { width: 24 }
2776
+ ];
2777
+ const output = await workbook.xlsx.writeBuffer();
2778
+ return Buffer.from(output);
2779
+ }
2780
+ async function parseBilingualFileAsync(filename, data, options = {}) {
2781
+ if (filename.toLowerCase().endsWith(".xlsx")) {
2782
+ if (!Buffer.isBuffer(data)) throw new Error("XLSX input must be a Buffer");
2783
+ return parseBilingualXlsx(data, options);
2784
+ }
2785
+ return parseBilingualFile(filename, typeof data === "string" ? data : data.toString("utf8"), options);
2786
+ }
2787
+
2788
+ // packages/l10n-core/src/qa/semantic.ts
2789
+ var CATEGORIES = /* @__PURE__ */ new Set([
2790
+ "accuracy",
2791
+ "fluency",
2792
+ "terminology",
2793
+ "style",
2794
+ "locale",
2795
+ "markup",
2796
+ "other"
2797
+ ]);
2798
+ var SEVERITIES = /* @__PURE__ */ new Set(["critical", "major", "minor", "neutral"]);
2799
+ function buildSemanticPrompt(segments) {
2800
+ const payload = segments.map((segment) => ({
2801
+ segmentId: segment.id,
2802
+ source: segment.sourceText,
2803
+ target: segment.targetText
2804
+ }));
2805
+ return [
2806
+ "You are a localization QA reviewer. Review source/target pairs for accuracy, fluency, terminology, style and locale issues.",
2807
+ 'Return STRICT JSON only: {"issues":[{"segmentId":"...","category":"accuracy|fluency|terminology|style|locale|markup|other","severity":"critical|major|minor|neutral","evidenceQuote":"exact substring copied from source or target","suggestion":"...","confidence":0.0}]}',
2808
+ "Rules: evidenceQuote must be an exact, unique substring of the source or target; do not quote the whole segment; omit issues without evidence; max 20 issues.",
2809
+ JSON.stringify(payload)
2810
+ ].join("\n");
2811
+ }
2812
+ function countOccurrences(haystack, needle) {
2813
+ if (needle.length === 0) return 0;
2814
+ let count = 0;
2815
+ let index = haystack.indexOf(needle);
2816
+ while (index >= 0) {
2817
+ count += 1;
2818
+ index = haystack.indexOf(needle, index + needle.length);
2819
+ }
2820
+ return count;
2821
+ }
2822
+ function severityOf(value) {
2823
+ return typeof value === "string" && SEVERITIES.has(value) ? value : void 0;
2824
+ }
2825
+ function categoryOf(value) {
2826
+ return typeof value === "string" && CATEGORIES.has(value) ? value : void 0;
2827
+ }
2828
+ function parseSemanticResponse(raw, segments, options = {}) {
2829
+ const dropBelow = options.dropBelow ?? 0.55;
2830
+ const highSeverityThreshold = options.highSeverityThreshold ?? 0.8;
2831
+ const minorThreshold = options.minorThreshold ?? 0.7;
2832
+ const maxIssues = options.maxIssuesPerBatch ?? 20;
2833
+ let parsed;
2834
+ try {
2835
+ parsed = JSON.parse(raw);
2836
+ } catch {
2837
+ return { issues: [], rejected: [{ reason: "invalid JSON", raw }] };
2838
+ }
2839
+ const rawIssues = parsed?.issues;
2840
+ if (!Array.isArray(rawIssues)) {
2841
+ return { issues: [], rejected: [{ reason: "missing issues array", raw: parsed }] };
2842
+ }
2843
+ if (rawIssues.length > maxIssues) {
2844
+ return { issues: [], rejected: [{ reason: `too many issues: ${rawIssues.length}`, raw: parsed }] };
2845
+ }
2846
+ const byId = new Map(segments.map((segment) => [segment.id, segment]));
2847
+ const issues = [];
2848
+ const rejected = [];
2849
+ for (const item of rawIssues) {
2850
+ const segmentId = typeof item.segmentId === "string" ? item.segmentId : "";
2851
+ const segment = byId.get(segmentId);
2852
+ if (!segment) {
2853
+ rejected.push({ reason: `unknown segment: ${segmentId}`, raw: item });
2854
+ continue;
2855
+ }
2856
+ const category = categoryOf(item.category);
2857
+ if (!category) {
2858
+ rejected.push({ reason: "invalid category", raw: item });
2859
+ continue;
2860
+ }
2861
+ const severity = severityOf(item.severity);
2862
+ if (!severity) {
2863
+ rejected.push({ reason: "invalid severity", raw: item });
2864
+ continue;
2865
+ }
2866
+ const quote = typeof item.evidenceQuote === "string" ? item.evidenceQuote.trim() : "";
2867
+ if (quote.length < 2) {
2868
+ rejected.push({ reason: "evidenceQuote too short", raw: item });
2869
+ continue;
2870
+ }
2871
+ const sourceCount = countOccurrences(segment.sourceText, quote);
2872
+ const targetCount = countOccurrences(segment.targetText, quote);
2873
+ if (sourceCount + targetCount === 0) {
2874
+ rejected.push({ reason: "evidenceQuote not found in source/target", raw: item });
2875
+ continue;
2876
+ }
2877
+ if (sourceCount + targetCount > 1) {
2878
+ rejected.push({ reason: "evidenceQuote is not unique", raw: item });
2879
+ continue;
2880
+ }
2881
+ const confidence = typeof item.confidence === "number" ? item.confidence : Number(item.confidence);
2882
+ if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) {
2883
+ rejected.push({ reason: "invalid confidence", raw: item });
2884
+ continue;
2885
+ }
2886
+ if (confidence < dropBelow) continue;
2887
+ let effective = severity;
2888
+ if ((severity === "critical" || severity === "major") && confidence < highSeverityThreshold) {
2889
+ effective = "neutral";
2890
+ } else if (severity === "minor" && confidence < minorThreshold) {
2891
+ effective = "neutral";
2892
+ }
2893
+ issues.push({
2894
+ ruleId: `SEMANTIC_${category.toUpperCase()}`,
2895
+ severity: effective,
2896
+ segmentId,
2897
+ segmentIndex: segment.index,
2898
+ message: typeof item.suggestion === "string" && item.suggestion.trim() ? item.suggestion.trim() : `Semantic ${category} issue`,
2899
+ evidence: quote,
2900
+ ...typeof item.suggestion === "string" && item.suggestion.trim() ? { suggestion: item.suggestion.trim() } : {},
2901
+ origin: "semantic"
2902
+ });
2903
+ }
2904
+ return { issues, rejected };
2905
+ }
2906
+ async function runSemanticCheck(document, provider, options = {}) {
2907
+ const batchSize = Math.max(1, options.batchSize ?? 20);
2908
+ const batches = [];
2909
+ for (let index = 0; index < document.segments.length; index += batchSize) {
2910
+ batches.push(document.segments.slice(index, index + batchSize));
2911
+ }
2912
+ const issues = [];
2913
+ const failures = [];
2914
+ let completed = 0;
2915
+ for (const [index, batch] of batches.entries()) {
2916
+ if (batch.length === 0) continue;
2917
+ try {
2918
+ const raw = await provider.complete({
2919
+ prompt: buildSemanticPrompt(batch),
2920
+ segmentIds: batch.map((segment) => segment.id)
2921
+ });
2922
+ const parsed = parseSemanticResponse(raw, batch, options);
2923
+ issues.push(...parsed.issues);
2924
+ if (parsed.issues.length === 0 && parsed.rejected.length > 0) {
2925
+ failures.push(`batch ${index + 1}: ${parsed.rejected.length} rejected item(s)`);
2926
+ continue;
2927
+ }
2928
+ completed += 1;
2929
+ } catch (caught) {
2930
+ failures.push(`batch ${index + 1}: ${caught instanceof Error ? caught.message : String(caught)}`);
2931
+ }
2932
+ }
2933
+ return {
2934
+ issues,
2935
+ failures,
2936
+ batches: batches.length,
2937
+ completedBatches: completed,
2938
+ coverage: completed === batches.length ? "complete" : "incomplete"
2939
+ };
2940
+ }
2941
+
2942
+ // packages/l10n-core/src/formats/ooxml.ts
2943
+ import { unzipSync, zipSync } from "fflate";
2944
+ var DOCX_PARTS = [
2945
+ /^word\/document\.xml$/u,
2946
+ /^word\/header\d+\.xml$/u,
2947
+ /^word\/footer\d+\.xml$/u,
2948
+ /^word\/footnotes\.xml$/u,
2949
+ /^word\/endnotes\.xml$/u
2950
+ ];
2951
+ function decode(bytes) {
2952
+ return Buffer.from(bytes).toString("utf8");
2953
+ }
2954
+ function encode(value) {
2955
+ return new Uint8Array(Buffer.from(value, "utf8"));
2956
+ }
2957
+ function partText(element) {
2958
+ return textOf(element);
2959
+ }
2960
+ function paragraphElements(root) {
2961
+ return descendants(root, (element) => element.local === "p");
2962
+ }
2963
+ function textElements(paragraph) {
2964
+ return descendants(paragraph, (element) => element.local === "t");
2965
+ }
2966
+ function docxRows(entries) {
2967
+ const rows = [];
2968
+ const segments = [];
2969
+ const warnings = [];
2970
+ const parts = Object.keys(entries).filter((name2) => DOCX_PARTS.some((pattern) => pattern.test(name2)));
2971
+ parts.sort();
2972
+ for (const part of parts) {
2973
+ const xml = decode(entries[part]);
2974
+ let root;
2975
+ try {
2976
+ validateXml(xml);
2977
+ root = parseXml(xml);
2978
+ } catch (caught) {
2979
+ warnings.push(`${part}: ${caught instanceof Error ? caught.message : String(caught)}`);
2980
+ continue;
2981
+ }
2982
+ const paragraphs = paragraphElements(root);
2983
+ paragraphs.forEach((paragraph, paragraphIndex) => {
2984
+ const source = textElements(paragraph).map((element) => partText(element)).join("");
2985
+ if (source.trim() === "") return;
2986
+ const id = `docx:${part}:${paragraphIndex}`;
2987
+ rows.push({ id, source, target: "" });
2988
+ segments.push({ id, part, paragraphIndex });
2989
+ });
2990
+ }
2991
+ return { format: "docx", rows, map: { format: "docx", segments }, warnings };
2992
+ }
2993
+ function sharedStringsMap(entries) {
2994
+ const warnings = [];
2995
+ const bytes = entries["xl/sharedStrings.xml"];
2996
+ if (!bytes) return { values: [], warnings: ["xlsx: sharedStrings.xml missing"] };
2997
+ try {
2998
+ const xml = decode(bytes);
2999
+ validateXml(xml);
3000
+ const root = parseXml(xml);
3001
+ const values = descendants(root, (element) => element.local === "si").map((element) => textOf(element));
3002
+ return { values, warnings };
3003
+ } catch (caught) {
3004
+ return { values: [], warnings: [`xlsx: sharedStrings invalid: ${caught instanceof Error ? caught.message : String(caught)}`] };
3005
+ }
3006
+ }
3007
+ function sheetParts(entries) {
3008
+ return Object.keys(entries).filter((name2) => /^xl\/worksheets\/sheet\d+\.xml$/u.test(name2)).sort();
3009
+ }
3010
+ function xlsxRows(entries) {
3011
+ const rows = [];
3012
+ const segments = [];
3013
+ const warnings = [];
3014
+ const shared = sharedStringsMap(entries);
3015
+ warnings.push(...shared.warnings);
3016
+ for (const part of sheetParts(entries)) {
3017
+ const xml = decode(entries[part]);
3018
+ let root;
3019
+ try {
3020
+ validateXml(xml);
3021
+ root = parseXml(xml);
3022
+ } catch (caught) {
3023
+ warnings.push(`${part}: ${caught instanceof Error ? caught.message : String(caught)}`);
3024
+ continue;
3025
+ }
3026
+ const cells = descendants(root, (element) => element.local === "c");
3027
+ for (const cell of cells) {
3028
+ const ref = cell.attribs.r;
3029
+ if (!ref) continue;
3030
+ const type = cell.attribs.t;
3031
+ const children = elementChildren(cell);
3032
+ const inline = children.find((child) => child.local === "is");
3033
+ const value = children.find((child) => child.local === "v");
3034
+ let source;
3035
+ if (type === "s" && value) {
3036
+ const index = Number.parseInt(textOf(value), 10);
3037
+ source = Number.isInteger(index) ? shared.values[index] : void 0;
3038
+ } else if (type === "inlineStr" && inline) {
3039
+ source = textOf(inline);
3040
+ } else if (type === "str" && value) {
3041
+ source = textOf(value);
3042
+ }
3043
+ if (source === void 0 || source.trim() === "") continue;
3044
+ const id = `xlsx:${part}:${ref}`;
3045
+ rows.push({ id, source, target: "", location: `${part}!${ref}` });
3046
+ segments.push({ id, part, cellRef: ref });
3047
+ }
3048
+ }
3049
+ return { format: "xlsx", rows, map: { format: "xlsx", segments }, warnings };
3050
+ }
3051
+ function pptxRows(entries) {
3052
+ const rows = [];
3053
+ const segments = [];
3054
+ const warnings = [];
3055
+ const parts = Object.keys(entries).filter((name2) => /^ppt\/slides\/slide\d+\.xml$/u.test(name2)).sort();
3056
+ if (parts.length === 0) warnings.push("pptx: no slide parts found");
3057
+ for (const part of parts) {
3058
+ const xml = decode(entries[part]);
3059
+ let root;
3060
+ try {
3061
+ validateXml(xml);
3062
+ root = parseXml(xml);
3063
+ } catch (caught) {
3064
+ warnings.push(`${part}: ${caught instanceof Error ? caught.message : String(caught)}`);
3065
+ continue;
3066
+ }
3067
+ const paragraphs = paragraphElements(root);
3068
+ paragraphs.forEach((paragraph, paragraphIndex) => {
3069
+ const source = textElements(paragraph).map((element) => partText(element)).join("");
3070
+ if (source.trim() === "") return;
3071
+ const id = `pptx:${part}:${paragraphIndex}`;
3072
+ rows.push({ id, source, target: "", location: `${part}!${paragraphIndex}` });
3073
+ segments.push({ id, part, paragraphIndex });
3074
+ });
3075
+ }
3076
+ return { format: "pptx", rows, map: { format: "pptx", segments }, warnings };
3077
+ }
3078
+ function extractOoxml(buffer, filename) {
3079
+ const entries = unzipSync(new Uint8Array(buffer));
3080
+ const lower = filename?.toLowerCase() ?? "";
3081
+ if (lower.endsWith(".docx") || "word/document.xml" in entries) return docxRows(entries);
3082
+ if (lower.endsWith(".xlsx") || "xl/workbook.xml" in entries) return xlsxRows(entries);
3083
+ if (lower.endsWith(".pptx") || "ppt/presentation.xml" in entries) return pptxRows(entries);
3084
+ throw new Error("unsupported OOXML format; expected DOCX, XLSX or PPTX");
3085
+ }
3086
+ function docxBackfill(entries, map, translations) {
3087
+ const warnings = [];
3088
+ const byPart = /* @__PURE__ */ new Map();
3089
+ for (const entry of map.segments) {
3090
+ const list = byPart.get(entry.part) ?? [];
3091
+ list.push(entry);
3092
+ byPart.set(entry.part, list);
3093
+ }
3094
+ let applied = 0;
3095
+ let skipped = 0;
3096
+ for (const [part, entriesForPart] of byPart) {
3097
+ const bytes = entries[part];
3098
+ if (!bytes) {
3099
+ warnings.push(`${part}: part missing`);
3100
+ skipped += entriesForPart.length;
3101
+ continue;
3102
+ }
3103
+ const xml = decode(bytes);
3104
+ let root;
3105
+ try {
3106
+ validateXml(xml);
3107
+ root = parseXml(xml);
3108
+ } catch (caught) {
3109
+ warnings.push(`${part}: ${caught instanceof Error ? caught.message : String(caught)}`);
3110
+ skipped += entriesForPart.length;
3111
+ continue;
3112
+ }
3113
+ const paragraphs = paragraphElements(root);
3114
+ const patches = [];
3115
+ for (const entry of entriesForPart) {
3116
+ const translation = translations.get(entry.id);
3117
+ if (translation === void 0) {
3118
+ skipped += 1;
3119
+ continue;
3120
+ }
3121
+ const paragraph = entry.paragraphIndex === void 0 ? void 0 : paragraphs[entry.paragraphIndex];
3122
+ if (!paragraph) {
3123
+ warnings.push(`${entry.id}: paragraph not found`);
3124
+ skipped += 1;
3125
+ continue;
3126
+ }
3127
+ const targets = textElements(paragraph);
3128
+ if (targets.length === 0) {
3129
+ skipped += 1;
3130
+ continue;
3131
+ }
3132
+ const escaped = escapeXmlText(translation);
3133
+ targets.forEach((element, index) => {
3134
+ const nodes = collectTextNodes(element);
3135
+ if (nodes.length === 0) return;
3136
+ nodes.forEach((node, nodeIndex) => {
3137
+ patches.push({
3138
+ start: node.start,
3139
+ end: node.end,
3140
+ content: index === 0 && nodeIndex === 0 ? escaped : "",
3141
+ id: entry.id
3142
+ });
3143
+ });
3144
+ });
3145
+ applied += 1;
3146
+ }
3147
+ if (patches.length === 0) continue;
3148
+ const patched = applyRangePatches(xml, patches);
3149
+ if (patched.errors.length > 0) {
3150
+ warnings.push(...patched.errors.map((error) => `${part}: ${error.message}`));
3151
+ continue;
3152
+ }
3153
+ try {
3154
+ validateXml(patched.xml);
3155
+ } catch (caught) {
3156
+ warnings.push(`${part}: patched XML invalid: ${caught instanceof Error ? caught.message : String(caught)}`);
3157
+ continue;
3158
+ }
3159
+ entries[part] = encode(patched.xml);
3160
+ }
3161
+ return { entries, applied, skipped, warnings };
3162
+ }
3163
+ function xlsxBackfill(entries, map, translations) {
3164
+ const warnings = [];
3165
+ const byPart = /* @__PURE__ */ new Map();
3166
+ for (const entry of map.segments) {
3167
+ const list = byPart.get(entry.part) ?? [];
3168
+ list.push(entry);
3169
+ byPart.set(entry.part, list);
3170
+ }
3171
+ let applied = 0;
3172
+ let skipped = 0;
3173
+ for (const [part, entriesForPart] of byPart) {
3174
+ const bytes = entries[part];
3175
+ if (!bytes) {
3176
+ warnings.push(`${part}: part missing`);
3177
+ skipped += entriesForPart.length;
3178
+ continue;
3179
+ }
3180
+ const xml = decode(bytes);
3181
+ let root;
3182
+ try {
3183
+ validateXml(xml);
3184
+ root = parseXml(xml);
3185
+ } catch (caught) {
3186
+ warnings.push(`${part}: ${caught instanceof Error ? caught.message : String(caught)}`);
3187
+ skipped += entriesForPart.length;
3188
+ continue;
3189
+ }
3190
+ const cells = /* @__PURE__ */ new Map();
3191
+ for (const cell of descendants(root, (element) => element.local === "c")) {
3192
+ const ref = cell.attribs.r;
3193
+ if (ref) cells.set(ref, cell);
3194
+ }
3195
+ const patches = [];
3196
+ for (const entry of entriesForPart) {
3197
+ const translation = translations.get(entry.id);
3198
+ if (translation === void 0) {
3199
+ skipped += 1;
3200
+ continue;
3201
+ }
3202
+ const cell = entry.cellRef ? cells.get(entry.cellRef) : void 0;
3203
+ if (!cell) {
3204
+ warnings.push(`${entry.id}: cell not found`);
3205
+ skipped += 1;
3206
+ continue;
3207
+ }
3208
+ if (elementChildren(cell).some((child) => child.local === "f")) {
3209
+ warnings.push(`${entry.id}: formula cell skipped`);
3210
+ skipped += 1;
3211
+ continue;
3212
+ }
3213
+ const attributes = Object.entries(cell.attribs).filter(([key]) => key !== "t").map(([key, value]) => `${key}="${value.replaceAll("&", "&amp;").replaceAll('"', "&quot;")}"`);
3214
+ const replacement = `<c ${attributes.join(" ")} t="inlineStr"><is><t xml:space="preserve">${escapeXmlText(translation)}</t></is></c>`;
3215
+ patches.push({ start: cell.start, end: cell.end, content: replacement, id: entry.id });
3216
+ applied += 1;
3217
+ }
3218
+ if (patches.length === 0) continue;
3219
+ const patched = applyRangePatches(xml, patches);
3220
+ if (patched.errors.length > 0) {
3221
+ warnings.push(...patched.errors.map((error) => `${part}: ${error.message}`));
3222
+ continue;
3223
+ }
3224
+ try {
3225
+ validateXml(patched.xml);
3226
+ } catch (caught) {
3227
+ warnings.push(`${part}: patched XML invalid: ${caught instanceof Error ? caught.message : String(caught)}`);
3228
+ continue;
3229
+ }
3230
+ entries[part] = encode(patched.xml);
3231
+ }
3232
+ return { entries, applied, skipped, warnings };
3233
+ }
3234
+ function backfillOoxml(buffer, map, translations) {
3235
+ const entries = unzipSync(new Uint8Array(buffer));
3236
+ const result = map.format === "xlsx" ? xlsxBackfill(entries, map, translations) : docxBackfill(entries, map, translations);
3237
+ if (result.applied === 0) {
3238
+ return { buffer, applied: 0, skipped: result.skipped, warnings: result.warnings };
3239
+ }
3240
+ const zipped = zipSync(result.entries, { level: 6 });
3241
+ return {
3242
+ buffer: Buffer.from(zipped),
3243
+ applied: result.applied,
3244
+ skipped: result.skipped,
3245
+ warnings: result.warnings
3246
+ };
3247
+ }
3248
+
3249
+ // packages/l10n-core/src/workflows/polish.ts
3250
+ function polishDocument(document, edits) {
3251
+ const normalized = /* @__PURE__ */ new Map();
3252
+ const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
3253
+ const warnings = [];
3254
+ for (const [segmentId, value] of edits) {
3255
+ const segment = byId.get(segmentId);
3256
+ if (!segment) {
3257
+ warnings.push(`${segmentId}: unknown segment`);
3258
+ continue;
3259
+ }
3260
+ normalized.set(segmentId, typeof value === "string" ? { content: value } : value);
3261
+ }
3262
+ if (warnings.length > 0) {
3263
+ return { xml: document.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
3264
+ }
3265
+ const before = structureSummary(document.xml).hash;
3266
+ const patch = patchDocument(document, normalized);
3267
+ if (patch.errors.length > 0) {
3268
+ warnings.push(...patch.errors.map((error) => `${error.segmentId}: ${error.message}`));
3269
+ return { xml: document.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
3270
+ }
3271
+ const after = structureSummary(patch.xml).hash;
3272
+ if (before !== after) {
3273
+ return {
3274
+ xml: document.xml,
3275
+ applied: 0,
3276
+ skipped: normalized.size,
3277
+ warnings: ["structure summary changed after polish; refusing to write"],
3278
+ changes: []
3279
+ };
3280
+ }
3281
+ const changes = [...normalized.entries()].map(([segmentId, edit]) => {
3282
+ const segment = byId.get(segmentId);
3283
+ let afterText = edit.content;
3284
+ if (edit.xml) {
3285
+ try {
3286
+ afterText = textOf(parseFragment(edit.content));
3287
+ } catch {
3288
+ afterText = edit.content;
3289
+ }
3290
+ }
3291
+ return { segmentId, before: segment.targetText, after: afterText };
3292
+ });
3293
+ return {
3294
+ xml: patch.xml,
3295
+ applied: patch.applied,
3296
+ skipped: 0,
3297
+ warnings,
3298
+ changes
3299
+ };
3300
+ }
3301
+
3302
+ // packages/dsh-l10n/src/tools-phase1.ts
3303
+ var MAX_BYTES = 200 * 1024 * 1024;
3304
+ async function readBytes(path) {
3305
+ const absolute = resolve(path);
3306
+ const info = await stat(absolute);
3307
+ if (!info.isFile()) throw new Error("path is not a regular file");
3308
+ if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
3309
+ return readFile(absolute);
3310
+ }
3311
+ async function readUtf8(path) {
3312
+ const absolute = resolve(path);
3313
+ const info = await stat(absolute);
3314
+ if (!info.isFile()) throw new Error("path is not a regular file");
3315
+ if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
3316
+ const value = await readFile(absolute, { encoding: "utf8" });
3317
+ if (value.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
3318
+ return value;
3319
+ }
3320
+ function documentFromXml(xml, path) {
3321
+ return parseXliff(xml, { filename: basename(path) });
3322
+ }
3323
+ function createMatchTmTool() {
3324
+ return defineTool({
3325
+ name: "l10n_match_tm",
3326
+ description: "Match a local XLIFF/SDLXLIFF file against a TMX 1.4b translation memory. Returns match-rate metadata per segment (no file modification).",
3327
+ parameters: {
3328
+ path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3329
+ tm: { type: "string", required: true, description: "TMX 1.4b file path." },
3330
+ threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
3331
+ maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
3332
+ },
3333
+ output: {
3334
+ schema: { type: "string" },
3335
+ render: (_args, value) => [{ type: "text", text: value }]
3336
+ },
3337
+ async execute(args) {
3338
+ const xml = await readUtf8(args.path);
3339
+ const document = documentFromXml(xml, args.path);
3340
+ const tm = parseTmx(await readUtf8(args.tm));
3341
+ if (tm.entries.length === 0) throw new Error("TMX contains no usable entries");
3342
+ const threshold = args.threshold ?? 75;
3343
+ const limit = Math.max(1, args.maxSegments ?? 50);
3344
+ const matches = document.segments.slice(0, limit).map((segment) => {
3345
+ const file = document.files[segment.location.fileIndex];
3346
+ const candidates = matchTm(tm.entries, segment.sourceText, {
3347
+ threshold,
3348
+ maxMatches: 1,
3349
+ ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
3350
+ ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
3351
+ });
3352
+ const best = candidates[0];
3353
+ return {
3354
+ segmentId: segment.id,
3355
+ matchRate: best?.matchRate ?? 0,
3356
+ similarity: best?.similarity ?? 0,
3357
+ matchType: best?.matchType ?? "no_match"
3358
+ };
3359
+ });
3360
+ return JSON.stringify({
3361
+ format: document.format,
3362
+ segments: document.segments.length,
3363
+ checked: matches.length,
3364
+ threshold,
3365
+ matches
3366
+ });
3367
+ }
3368
+ });
3369
+ }
3370
+ function createMatchTermsTool() {
3371
+ return defineTool({
3372
+ name: "l10n_match_terms",
3373
+ description: "Match a local XLIFF/SDLXLIFF file against a CSV glossary. Returns term hits and forbidden-term counts (no file modification).",
3374
+ parameters: {
3375
+ path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3376
+ glossary: { type: "string", required: true, description: "CSV glossary path." },
3377
+ maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
3378
+ },
3379
+ output: {
3380
+ schema: { type: "string" },
3381
+ render: (_args, value) => [{ type: "text", text: value }]
3382
+ },
3383
+ async execute(args) {
3384
+ const xml = await readUtf8(args.path);
3385
+ const document = documentFromXml(xml, args.path);
3386
+ const glossary = parseGlossaryCsv(await readUtf8(args.glossary));
3387
+ if (glossary.entries.length === 0) throw new Error("glossary contains no usable entries");
3388
+ const limit = Math.max(1, args.maxSegments ?? 50);
3389
+ const hits = [];
3390
+ let forbidden = 0;
3391
+ for (const segment of document.segments.slice(0, limit)) {
3392
+ const file = document.files[segment.location.fileIndex];
3393
+ for (const side of ["source", "target"]) {
3394
+ const result = matchTerms(segment[`${side}Text`], glossary.entries, {
3395
+ ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
3396
+ ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
3397
+ });
3398
+ for (const hit of result.hits) {
3399
+ if (hit.entry.forbidden) forbidden += 1;
3400
+ hits.push({
3401
+ segmentId: segment.id,
3402
+ side,
3403
+ termId: hit.entry.id,
3404
+ sourceTerm: hit.entry.source,
3405
+ targetTerm: hit.entry.target,
3406
+ start: hit.start,
3407
+ end: hit.end,
3408
+ matchKind: hit.matchKind,
3409
+ forbidden: hit.entry.forbidden === true
3410
+ });
3411
+ }
3412
+ }
3413
+ }
3414
+ return JSON.stringify({
3415
+ format: document.format,
3416
+ segments: document.segments.length,
3417
+ checked: Math.min(limit, document.segments.length),
3418
+ entries: glossary.entries.length,
3419
+ hits: hits.slice(0, 200),
3420
+ hitCount: hits.length,
3421
+ forbiddenCount: forbidden
3422
+ });
3423
+ }
3424
+ });
3425
+ }
3426
+ function createCheckTool() {
3427
+ return defineTool({
3428
+ name: "l10n_check",
3429
+ description: "Run deterministic QA on a local XLIFF/SDLXLIFF file, optionally with a TMX and CSV glossary. Returns rule/severity counts and a bounded issue list; never modifies files.",
3430
+ parameters: {
3431
+ path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3432
+ tm: { type: "string", description: "Optional TMX 1.4b file path." },
3433
+ glossary: { type: "string", description: "Optional CSV glossary path." },
3434
+ maxIssues: { type: "integer", description: "Max issues returned; default 100." }
3435
+ },
3436
+ output: {
3437
+ schema: { type: "string" },
3438
+ render: (_args, value) => [{ type: "text", text: value }]
3439
+ },
3440
+ async execute(args) {
3441
+ const xml = await readUtf8(args.path);
3442
+ const document = documentFromXml(xml, args.path);
3443
+ const tmEntries = args.tm ? parseTmx(await readUtf8(args.tm)).entries : [];
3444
+ const glossary = args.glossary ? parseGlossaryCsv(await readUtf8(args.glossary)).entries : [];
3445
+ const checked = checkDocument(document, { tmEntries, glossary });
3446
+ const bySeverity = {};
3447
+ const byRule = {};
3448
+ for (const issue of checked.qa.issues) {
3449
+ bySeverity[issue.severity] = (bySeverity[issue.severity] ?? 0) + 1;
3450
+ byRule[issue.ruleId] = (byRule[issue.ruleId] ?? 0) + 1;
3451
+ }
3452
+ const limit = Math.max(1, args.maxIssues ?? 100);
3453
+ return JSON.stringify({
3454
+ format: document.format,
3455
+ segments: document.segments.length,
3456
+ checkedSegments: checked.qa.checkedSegments,
3457
+ coverage: checked.qa.coverage,
3458
+ totalIssues: checked.qa.issues.length,
3459
+ bySeverity,
3460
+ byRule,
3461
+ issues: checked.qa.issues.slice(0, limit).map((issue) => ({
3462
+ ruleId: issue.ruleId,
3463
+ severity: issue.severity,
3464
+ segmentId: issue.segmentId,
3465
+ message: issue.message,
3466
+ evidence: issue.evidence,
3467
+ ...issue.suggestion === void 0 ? {} : { suggestion: issue.suggestion },
3468
+ origin: issue.origin
3469
+ })),
3470
+ issuesReturned: Math.min(limit, checked.qa.issues.length)
3471
+ });
3472
+ }
3473
+ });
3474
+ }
3475
+ function createTranslateTool() {
3476
+ return defineTool({
3477
+ name: "l10n_translate",
3478
+ description: "Fill empty targets in a local XLIFF/SDLXLIFF file from a TMX, writing a NEW file. Existing targets and tagged segments are never overwritten.",
3479
+ parameters: {
3480
+ path: { type: "string", required: true, description: "Input XLIFF/SDLXLIFF path." },
3481
+ tm: { type: "string", required: true, description: "TMX 1.4b file path." },
3482
+ output: { type: "string", required: true, description: "New output file path; must differ from input." },
3483
+ threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
3484
+ setSdlConf: { type: "string", description: "SDLXLIFF only: set sdl:seg@conf on filled segments." }
3485
+ },
3486
+ output: {
3487
+ schema: {
3488
+ type: "object",
3489
+ additionalProperties: false,
3490
+ properties: {
3491
+ output: { type: "string" },
3492
+ applied: { type: "integer" },
3493
+ skipped: { type: "integer" },
3494
+ warnings: { type: "array", items: { type: "string" } },
3495
+ sha256: { type: "string" }
3496
+ }
3497
+ },
3498
+ render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3499
+ },
3500
+ async execute(args) {
3501
+ const input = resolve(args.path);
3502
+ const output = resolve(args.output);
3503
+ if (input === output) throw new Error("refusing to overwrite the input file");
3504
+ const xml = await readUtf8(input);
3505
+ const document = documentFromXml(xml, input);
3506
+ const tm = parseTmx(await readUtf8(args.tm));
3507
+ const result = translateDocument(document, tm.entries, {
3508
+ threshold: args.threshold ?? 75,
3509
+ ...args.setSdlConf === void 0 ? {} : { setSdlState: true, sdlConf: args.setSdlConf }
3510
+ });
3511
+ await writeFile(output, result.xml, "utf8");
3512
+ return {
3513
+ output,
3514
+ applied: result.applied,
3515
+ skipped: result.skipped,
3516
+ warnings: result.warnings,
3517
+ sha256: createHash2("sha256").update(result.xml, "utf8").digest("hex")
3518
+ };
3519
+ }
3520
+ });
3521
+ }
3522
+ function createConvertTool() {
3523
+ return defineTool({
3524
+ name: "l10n_convert",
3525
+ description: "Convert a bilingual CSV/TXT/XLSX table between formats. Writes a new file; never modifies the input.",
3526
+ parameters: {
3527
+ input: { type: "string", required: true, description: "Input CSV/TXT/XLSX file." },
3528
+ to: { type: "string", required: true, enum: ["csv", "txt", "xlsx"], description: "Target format." },
3529
+ output: { type: "string", required: true, description: "New output file path." }
3530
+ },
3531
+ output: {
3532
+ schema: {
3533
+ type: "object",
3534
+ additionalProperties: false,
3535
+ properties: {
3536
+ output: { type: "string" },
3537
+ rows: { type: "integer" },
3538
+ warnings: { type: "array", items: { type: "string" } }
3539
+ }
3540
+ },
3541
+ render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3542
+ },
3543
+ async execute(args) {
3544
+ const input = resolve(args.input);
3545
+ const output = resolve(args.output);
3546
+ if (input === output) throw new Error("refusing to overwrite the input file");
3547
+ const table = await parseBilingualFileAsync(input, await readBytes(input));
3548
+ if (args.to === "xlsx") {
3549
+ await writeFile(output, await writeBilingualXlsx(table.rows));
3550
+ } else if (args.to === "csv") {
3551
+ await writeFile(output, writeBilingualCsv(table.rows, { delimiter: "," }), "utf8");
3552
+ } else {
3553
+ await writeFile(output, writeBilingualCsv(table.rows, { delimiter: " ", header: false }), "utf8");
3554
+ }
3555
+ return { output, rows: table.rows.length, warnings: table.warnings };
3556
+ }
3557
+ });
3558
+ }
3559
+ function createValidateTool() {
3560
+ return defineTool({
3561
+ name: "l10n_validate",
3562
+ description: "Validate one local XLIFF 1.2/SDLXLIFF file: XML well-formedness, segment counts, missing targets, inline-tag mismatches. Read-only.",
3563
+ parameters: {
3564
+ path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." }
3565
+ },
3566
+ output: {
3567
+ schema: { type: "string" },
3568
+ render: (_args, value) => [{ type: "text", text: value }]
3569
+ },
3570
+ async execute(args) {
3571
+ const xml = await readUtf8(args.path);
3572
+ const document = documentFromXml(xml, args.path);
3573
+ let missingTarget = 0;
3574
+ let tagMismatch = 0;
3575
+ for (const segment of document.segments) {
3576
+ if (!segment.targetRange) missingTarget += 1;
3577
+ if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
3578
+ }
3579
+ return JSON.stringify({
3580
+ valid: true,
3581
+ format: document.format,
3582
+ segments: document.segments.length,
3583
+ missingTarget,
3584
+ tagMismatch,
3585
+ warnings: document.warnings
3586
+ });
3587
+ }
3588
+ });
3589
+ }
3590
+
3591
+ // packages/dsh-l10n/src/tools-phase2.ts
3592
+ import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
3593
+ import { basename as basename2, resolve as resolve2 } from "node:path";
3594
+ import { defineTool as defineTool2 } from "@deepseek-ai/dsh-tools";
3595
+ var MAX_BYTES2 = 200 * 1024 * 1024;
3596
+ async function readBytes2(path) {
3597
+ const absolute = resolve2(path);
3598
+ const info = await stat2(absolute);
3599
+ if (!info.isFile()) throw new Error("path is not a regular file");
3600
+ if (info.size > MAX_BYTES2) throw new Error(`file exceeds ${MAX_BYTES2} bytes`);
3601
+ return readFile2(absolute);
3602
+ }
3603
+ async function readText(path) {
3604
+ return (await readBytes2(path)).toString("utf8");
3605
+ }
3606
+ function createExtractDocTool() {
3607
+ return defineTool2({
3608
+ name: "l10n_extract_doc",
3609
+ description: "Extract local DOCX paragraph text or XLSX text cells into a bilingual table plus a map.json. Writes new files; the original is never modified.",
3610
+ parameters: {
3611
+ path: { type: "string", required: true, description: "DOCX or XLSX file path." },
3612
+ rowsOutput: { type: "string", required: true, description: "Bilingual CSV/XLSX output path." },
3613
+ mapOutput: { type: "string", required: true, description: "map.json output path." },
3614
+ format: { type: "string", enum: ["csv", "xlsx"], description: "Rows output format; default by extension." }
3615
+ },
3616
+ output: {
3617
+ schema: {
3618
+ type: "object",
3619
+ additionalProperties: false,
3620
+ properties: {
3621
+ format: { type: "string" },
3622
+ rows: { type: "integer" },
3623
+ rowsOutput: { type: "string" },
3624
+ mapOutput: { type: "string" },
3625
+ warnings: { type: "array", items: { type: "string" } }
3626
+ }
3627
+ },
3628
+ render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3629
+ },
3630
+ async execute(args) {
3631
+ const input = resolve2(args.path);
3632
+ const rowsOutput = resolve2(args.rowsOutput);
3633
+ const mapOutput = resolve2(args.mapOutput);
3634
+ if (input === rowsOutput || input === mapOutput) throw new Error("refusing to overwrite the input file");
3635
+ const result = extractOoxml(await readBytes2(input), basename2(input));
3636
+ const format = args.format ?? (rowsOutput.toLowerCase().endsWith(".xlsx") ? "xlsx" : "csv");
3637
+ if (format === "xlsx") {
3638
+ await writeFile2(rowsOutput, await writeBilingualXlsx(result.rows));
3639
+ } else {
3640
+ await writeFile2(rowsOutput, writeBilingualCsv(result.rows, { delimiter: "," }), "utf8");
3641
+ }
3642
+ await writeFile2(mapOutput, JSON.stringify(result.map, null, 2), "utf8");
3643
+ return {
3644
+ format: result.format,
3645
+ rows: result.rows.length,
3646
+ rowsOutput,
3647
+ mapOutput,
3648
+ warnings: result.warnings
3649
+ };
3650
+ }
3651
+ });
3652
+ }
3653
+ function createBackfillTool() {
3654
+ return defineTool2({
3655
+ name: "l10n_backfill",
3656
+ description: "Backfill translated bilingual rows into a NEW DOCX/XLSX file using a previously generated map.json. The original is never modified.",
3657
+ parameters: {
3658
+ original: { type: "string", required: true, description: "Original DOCX/XLSX path." },
3659
+ map: { type: "string", required: true, description: "map.json path generated by l10n_extract_doc." },
3660
+ bilingual: { type: "string", required: true, description: "Translated bilingual CSV/XLSX path." },
3661
+ output: { type: "string", required: true, description: "New output DOCX/XLSX path." }
3662
+ },
3663
+ output: {
3664
+ schema: {
3665
+ type: "object",
3666
+ additionalProperties: false,
3667
+ properties: {
3668
+ output: { type: "string" },
3669
+ applied: { type: "integer" },
3670
+ skipped: { type: "integer" },
3671
+ warnings: { type: "array", items: { type: "string" } }
3672
+ }
3673
+ },
3674
+ render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3675
+ },
3676
+ async execute(args) {
3677
+ const original = resolve2(args.original);
3678
+ const output = resolve2(args.output);
3679
+ if (original === output) throw new Error("refusing to overwrite the input file");
3680
+ const map = JSON.parse(await readText(args.map));
3681
+ if (map.format !== "docx" && map.format !== "xlsx") throw new Error("invalid map.json");
3682
+ const table = await parseBilingualFileAsync(args.bilingual, await readBytes2(args.bilingual));
3683
+ const translations = new Map(
3684
+ table.rows.filter((row) => row.target.trim() !== "").map((row) => [row.id, row.target])
3685
+ );
3686
+ const result = backfillOoxml(await readBytes2(original), map, translations);
3687
+ await writeFile2(output, result.buffer);
3688
+ return {
3689
+ output,
3690
+ applied: result.applied,
3691
+ skipped: result.skipped,
3692
+ warnings: result.warnings
3693
+ };
3694
+ }
3695
+ });
3696
+ }
3697
+ function createPolishTool() {
3698
+ return defineTool2({
3699
+ name: "l10n_polish",
3700
+ description: "Apply reviewed target edits (plain text for untagged segments; inline XML fragments for tagged segments) and write a NEW XLIFF/SDLXLIFF file. Fails closed on tag/structure mismatch.",
3701
+ parameters: {
3702
+ path: { type: "string", required: true, description: "Input XLIFF/SDLXLIFF path." },
3703
+ edits: {
3704
+ type: "string",
3705
+ required: true,
3706
+ description: 'JSON object: {"segmentId":{"content":"...","xml":true|false}}'
3707
+ },
3708
+ output: { type: "string", required: true, description: "New output file path; must differ from input." }
3709
+ },
3710
+ output: {
3711
+ schema: {
3712
+ type: "object",
3713
+ additionalProperties: false,
3714
+ properties: {
3715
+ output: { type: "string" },
3716
+ applied: { type: "integer" },
3717
+ skipped: { type: "integer" },
3718
+ warnings: { type: "array", items: { type: "string" } },
3719
+ changes: {
3720
+ type: "array",
3721
+ items: {
3722
+ type: "object",
3723
+ additionalProperties: false,
3724
+ properties: {
3725
+ segmentId: { type: "string" },
3726
+ before: { type: "string" },
3727
+ after: { type: "string" }
3728
+ }
3729
+ }
3730
+ }
3731
+ }
3732
+ },
3733
+ render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3734
+ },
3735
+ async execute(args) {
3736
+ const input = resolve2(args.path);
3737
+ const output = resolve2(args.output);
3738
+ if (input === output) throw new Error("refusing to overwrite the input file");
3739
+ const parsed = JSON.parse(args.edits);
3740
+ const edits = /* @__PURE__ */ new Map();
3741
+ for (const [segmentId, value] of Object.entries(parsed)) {
3742
+ if (typeof value?.content !== "string") throw new Error(`invalid edit for ${segmentId}`);
3743
+ edits.set(segmentId, {
3744
+ content: value.content,
3745
+ ...typeof value.xml === "boolean" ? { xml: value.xml } : {}
3746
+ });
3747
+ }
3748
+ const xml = await readText(input);
3749
+ const document = parseXliff(xml, { filename: input });
3750
+ const result = polishDocument(document, edits);
3751
+ if (result.applied === 0 && result.warnings.length > 0) {
3752
+ throw new Error(result.warnings.join("; "));
3753
+ }
3754
+ await writeFile2(output, result.xml, "utf8");
3755
+ return {
3756
+ output,
3757
+ applied: result.applied,
3758
+ skipped: result.skipped,
3759
+ warnings: result.warnings,
3760
+ changes: result.changes
3761
+ };
3762
+ }
3763
+ });
3764
+ }
3765
+
3766
+ // packages/dsh-l10n/src/tools-assets.ts
3767
+ import { defineTool as defineTool3 } from "@deepseek-ai/dsh-tools";
3768
+ function createAssetInfoTool(baseUrl) {
3769
+ return defineTool3({
3770
+ name: "l10n_assets",
3771
+ description: "Read-only company localization asset service: health, glossary entries, or TM entries. Requires L10N_ASSET_BASE_URL.",
3772
+ parameters: {
3773
+ view: { type: "string", required: true, enum: ["health", "glossary", "tm"], description: "Which asset view to read." },
3774
+ maxEntries: { type: "integer", description: "Cap entries returned; default 100." }
3775
+ },
3776
+ output: {
3777
+ schema: { type: "string" },
3778
+ render: (_args, value) => [{ type: "text", text: value }]
3779
+ },
3780
+ async execute(args) {
3781
+ if (!baseUrl) throw new Error("L10N_ASSET_BASE_URL is not configured");
3782
+ const root = baseUrl.replace(/\/$/u, "");
3783
+ const path = args.view === "health" ? "/health" : args.view === "glossary" ? "/glossary" : "/tm";
3784
+ const response = await fetch(`${root}${path}`, { signal: AbortSignal.timeout(15e3) });
3785
+ if (!response.ok) throw new Error(`asset service returned ${response.status}`);
3786
+ const value = await response.json();
3787
+ if (Array.isArray(value.entries) && args.view !== "health") {
3788
+ const limit = Math.max(1, args.maxEntries ?? 100);
3789
+ return JSON.stringify({ ...value, entries: value.entries.slice(0, limit), total: value.entries.length });
3790
+ }
3791
+ return JSON.stringify(value);
3792
+ }
3793
+ });
3794
+ }
3795
+
3796
+ // packages/dsh-l10n/src/tools-semantic.ts
3797
+ import { readFile as readFile3 } from "node:fs/promises";
3798
+ import { basename as basename3, resolve as resolve3 } from "node:path";
3799
+ import { defineTool as defineTool4 } from "@deepseek-ai/dsh-tools";
3800
+ function createDshSemanticProvider(llm, provider, model) {
3801
+ return {
3802
+ async complete({ prompt }) {
3803
+ let text = "";
3804
+ for await (const chunk of llm.stream({
3805
+ provider,
3806
+ model,
3807
+ messages: [{ role: "user", content: [{ type: "text", text: prompt }] }]
3808
+ })) {
3809
+ if (chunk.type === "text-delta" && typeof chunk.text === "string") text += chunk.text;
3810
+ if (chunk.type === "finish" && chunk.kind === "error") {
3811
+ throw new Error("model stream ended with an error");
3812
+ }
3813
+ }
3814
+ return text;
3815
+ }
3816
+ };
3817
+ }
3818
+ function createSemanticCheckTool(llm) {
3819
+ return defineTool4({
3820
+ name: "l10n_semantic_check",
3821
+ description: "Run semantic localization QA through the dsh LLM service. Requires a configured provider/model; returns validated issues only (evidence must be uniquely locatable).",
3822
+ parameters: {
3823
+ path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3824
+ provider: { type: "string", required: true, description: "dsh LLM provider route, e.g. aperture." },
3825
+ model: { type: "string", required: true, description: "Model id, e.g. deepseek-flash." },
3826
+ batchSize: { type: "integer", description: "Segments per model call; default 20." }
3827
+ },
3828
+ output: {
3829
+ schema: { type: "string" },
3830
+ render: (_args, value) => [{ type: "text", text: value }]
3831
+ },
3832
+ async execute(args) {
3833
+ if (!llm) throw new Error("dsh LLM service is unavailable; configure a model provider first");
3834
+ const xml = await readFile3(resolve3(args.path), "utf8");
3835
+ const document = parseXliff(xml, { filename: basename3(args.path) });
3836
+ const result = await runSemanticCheck(
3837
+ document,
3838
+ createDshSemanticProvider(llm, args.provider, args.model),
3839
+ { batchSize: args.batchSize ?? 20 }
3840
+ );
3841
+ return JSON.stringify({
3842
+ issues: result.issues.length,
3843
+ coverage: result.coverage,
3844
+ completedBatches: result.completedBatches,
3845
+ batches: result.batches,
3846
+ failures: result.failures,
3847
+ items: result.issues
3848
+ });
3849
+ }
3850
+ });
3851
+ }
3852
+
3853
+ // packages/dsh-l10n/src/tool.ts
3854
+ import { readFile as readFile4, stat as stat3 } from "node:fs/promises";
3855
+ import { basename as basename4, resolve as resolve4 } from "node:path";
3856
+ import { defineTool as defineTool5 } from "@deepseek-ai/dsh-tools";
3857
+ var MAX_BYTES3 = 200 * 1024 * 1024;
3858
+ async function inspectFile(path, signal) {
3859
+ signal?.throwIfAborted();
3860
+ const absolute = resolve4(path);
3861
+ const info = await stat3(absolute);
3862
+ if (!info.isFile()) throw new Error("path is not a regular file");
3863
+ if (info.size > MAX_BYTES3) throw new Error(`file exceeds ${MAX_BYTES3} bytes`);
3864
+ const xml = await readFile4(absolute, { encoding: "utf8", signal });
3865
+ if (xml.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
3866
+ signal?.throwIfAborted();
3867
+ const document = parseXliff(xml, { filename: basename4(absolute) });
3868
+ const targetTagDistribution = {};
3869
+ let missingTarget = 0;
3870
+ let tagMismatch = 0;
3871
+ for (const segment of document.segments) {
3872
+ const key = String(segment.targetTags.length);
3873
+ targetTagDistribution[key] = (targetTagDistribution[key] ?? 0) + 1;
3874
+ if (!segment.targetRange) missingTarget += 1;
3875
+ if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
3876
+ }
3877
+ const structure = structureSummary(xml);
3878
+ return {
3879
+ format: document.format,
3880
+ version: document.version,
3881
+ bytes: Buffer.byteLength(xml, "utf8"),
3882
+ files: document.files.length,
3883
+ languages: document.files.map(
3884
+ (file) => `${file.sourceLanguage}->${file.targetLanguage}`
3885
+ ),
3886
+ segments: document.segments.length,
3887
+ missingTarget,
3888
+ tagMismatch,
3889
+ warnings: document.warnings.length,
3890
+ targetTagDistribution,
3891
+ structureElements: structure.count,
3892
+ structureHash: structure.hash.slice(0, 16)
3893
+ };
3894
+ }
3895
+ function createInspectTool() {
3896
+ return defineTool5({
3897
+ name: "l10n_inspect",
3898
+ description: "Inspect a local XLIFF 1.2 or SDLXLIFF file. Returns format, languages, segment count, inline-tag statistics, warnings and a structure hash. Never returns source or target text. Read-only; does not modify the file.",
3899
+ parameters: {
3900
+ path: {
3901
+ type: "string",
3902
+ required: true,
3903
+ description: "Path to a .sdlxliff, .xlf, .xliff or .mqxliff file (UTF-8)."
3904
+ }
3905
+ },
3906
+ output: {
3907
+ schema: {
3908
+ type: "object",
3909
+ additionalProperties: false,
3910
+ properties: {
3911
+ format: { type: "string" },
3912
+ version: { type: "string" },
3913
+ bytes: { type: "integer" },
3914
+ files: { type: "integer" },
3915
+ languages: { type: "array", items: { type: "string" } },
3916
+ segments: { type: "integer" },
3917
+ missingTarget: { type: "integer" },
3918
+ tagMismatch: { type: "integer" },
3919
+ warnings: { type: "integer" },
3920
+ targetTagDistribution: { type: "object", additionalProperties: true },
3921
+ structureElements: { type: "integer" },
3922
+ structureHash: { type: "string" }
3923
+ }
3924
+ },
3925
+ render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3926
+ },
3927
+ async execute(args, exec) {
3928
+ return inspectFile(args.path, exec.signal);
3929
+ }
3930
+ });
3931
+ }
3932
+
3933
+ // packages/dsh-l10n/src/index.ts
3934
+ var name = "langlink-l10n";
3935
+ var inject = ["tools"];
3936
+ function apply(ctx) {
3937
+ const llm = ctx.get?.("llm");
3938
+ for (const tool of [
3939
+ createInspectTool(),
3940
+ createMatchTmTool(),
3941
+ createMatchTermsTool(),
3942
+ createCheckTool(),
3943
+ createTranslateTool(),
3944
+ createConvertTool(),
3945
+ createValidateTool(),
3946
+ createExtractDocTool(),
3947
+ createBackfillTool(),
3948
+ createPolishTool(),
3949
+ createSemanticCheckTool(llm),
3950
+ createAssetInfoTool(process.env.L10N_ASSET_BASE_URL)
3951
+ ]) {
3952
+ ctx.tools.register(tool);
3953
+ }
3954
+ }
3955
+ export {
3956
+ apply,
3957
+ createAssetInfoTool,
3958
+ createBackfillTool,
3959
+ createCheckTool,
3960
+ createConvertTool,
3961
+ createDshSemanticProvider,
3962
+ createExtractDocTool,
3963
+ createInspectTool,
3964
+ createMatchTermsTool,
3965
+ createMatchTmTool,
3966
+ createPolishTool,
3967
+ createSemanticCheckTool,
3968
+ createTranslateTool,
3969
+ createValidateTool,
3970
+ inject,
3971
+ inspectFile,
3972
+ name
3973
+ };
3974
+ //# sourceMappingURL=index.js.map