@langlink/dsh-l10n 0.2.1 → 0.3.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 CHANGED
@@ -61,39 +61,39 @@ function createPlatformClient(options) {
61
61
  }
62
62
  return result;
63
63
  }
64
- async function json(method, endpoint, body, request) {
64
+ async function json2(method, endpoint, body, request) {
65
65
  return await (await response(method, endpoint, body, request)).json();
66
66
  }
67
67
  return {
68
- qaRules: () => json("GET", path("qa-rules")),
69
- configurations: (resource, offset = 0) => json("GET", `${path(resource)}?offset=${offset}`),
70
- configurationVersion: (resource, id, version) => json("GET", `${path(resource, id)}?version=${version}`),
71
- documentConfiguration: (id) => json("GET", path("documents", id, "configuration")),
72
- setDocumentConfiguration: (id, input) => json("PATCH", path("documents", id, "configuration"), input),
73
- me: () => json("GET", path("me")),
74
- plugins: () => json("GET", path("plugins")),
75
- models: () => json("GET", path("models")),
76
- createDocument: (input, request) => json("POST", path("documents"), input, request),
77
- documents: (offset = 0, limit = 50) => json("GET", `${path("documents")}?offset=${offset}&limit=${limit}`),
78
- documentChanges: (id, after) => json("GET", `${path("documents", id, "changes")}?after=${encodeURIComponent(after)}`),
79
- document: (id, offset = 0, limit = 50, request) => json("GET", `${path("documents", id)}?segments.offset=${offset}&segments.limit=${limit}`, void 0, request),
80
- segment: (id, segmentId) => json("GET", path("documents", id, "segments", segmentId)),
81
- edit: (id, segmentId, input, request) => json("PATCH", path("documents", id, "segments", segmentId), input, request),
82
- attachAssets: (id, input, request) => json("POST", path("documents", id, "assets"), input, request),
83
- segmentAssets: (id, segmentId) => json("GET", path("documents", id, "segments", segmentId, "assets")),
84
- comments: (id, segmentId) => json("GET", path("documents", id, "comments") + (segmentId ? `?segmentId=${encodeURIComponent(segmentId)}` : "")),
85
- addComment: (id, segmentId, body) => json("POST", path("documents", id, "comments"), { segmentId, body }),
86
- setStyle: (id, style, customStyleInstruction) => json("PATCH", path("documents", id, "style"), { style, ...customStyleInstruction === void 0 ? {} : { customStyleInstruction } }),
87
- setModels: (id, input) => json("PATCH", path("documents", id, "models"), input),
88
- createJob: (id, input, request) => json("POST", path("documents", id, "jobs"), input, request),
89
- job: (id, request) => json("GET", path("jobs", id), void 0, request),
90
- cancelJob: (id) => json("POST", path("jobs", id, "cancel"), {}),
91
- retryJob: (id, request) => json("POST", path("jobs", id, "retry"), {}, request),
92
- undoJob: (id) => json("POST", path("jobs", id, "undo"), {}),
93
- jobResults: (id) => json("GET", path("jobs", id, "results")),
94
- updateSuggestion: (id, segmentId, input) => json("PATCH", path("documents", id, "segments", segmentId, "suggestion"), input),
95
- discardSuggestion: (id, segmentId, input) => json("DELETE", path("documents", id, "segments", segmentId, "suggestion"), input),
96
- applySuggestion: (id, segmentId, input) => json("POST", path("documents", id, "segments", segmentId, "apply-suggestion"), input),
68
+ qaRules: () => json2("GET", path("qa-rules")),
69
+ configurations: (resource, offset = 0) => json2("GET", `${path(resource)}?offset=${offset}`),
70
+ configurationVersion: (resource, id, version) => json2("GET", `${path(resource, id)}?version=${version}`),
71
+ documentConfiguration: (id) => json2("GET", path("documents", id, "configuration")),
72
+ setDocumentConfiguration: (id, input) => json2("PATCH", path("documents", id, "configuration"), input),
73
+ me: () => json2("GET", path("me")),
74
+ plugins: () => json2("GET", path("plugins")),
75
+ models: () => json2("GET", path("models")),
76
+ createDocument: (input, request) => json2("POST", path("documents"), input, request),
77
+ documents: (offset = 0, limit = 50) => json2("GET", `${path("documents")}?offset=${offset}&limit=${limit}`),
78
+ documentChanges: (id, after) => json2("GET", `${path("documents", id, "changes")}?after=${encodeURIComponent(after)}`),
79
+ document: (id, offset = 0, limit = 50, request) => json2("GET", `${path("documents", id)}?segments.offset=${offset}&segments.limit=${limit}`, void 0, request),
80
+ segment: (id, segmentId) => json2("GET", path("documents", id, "segments", segmentId)),
81
+ edit: (id, segmentId, input, request) => json2("PATCH", path("documents", id, "segments", segmentId), input, request),
82
+ attachAssets: (id, input, request) => json2("POST", path("documents", id, "assets"), input, request),
83
+ segmentAssets: (id, segmentId) => json2("GET", path("documents", id, "segments", segmentId, "assets")),
84
+ comments: (id, segmentId) => json2("GET", path("documents", id, "comments") + (segmentId ? `?segmentId=${encodeURIComponent(segmentId)}` : "")),
85
+ addComment: (id, segmentId, body) => json2("POST", path("documents", id, "comments"), { segmentId, body }),
86
+ setStyle: (id, style, customStyleInstruction) => json2("PATCH", path("documents", id, "style"), { style, ...customStyleInstruction === void 0 ? {} : { customStyleInstruction } }),
87
+ setModels: (id, input) => json2("PATCH", path("documents", id, "models"), input),
88
+ createJob: (id, input, request) => json2("POST", path("documents", id, "jobs"), input, request),
89
+ job: (id, request) => json2("GET", path("jobs", id), void 0, request),
90
+ cancelJob: (id) => json2("POST", path("jobs", id, "cancel"), {}),
91
+ retryJob: (id, request) => json2("POST", path("jobs", id, "retry"), {}, request),
92
+ undoJob: (id) => json2("POST", path("jobs", id, "undo"), {}),
93
+ jobResults: (id) => json2("GET", path("jobs", id, "results")),
94
+ updateSuggestion: (id, segmentId, input) => json2("PATCH", path("documents", id, "segments", segmentId, "suggestion"), input),
95
+ discardSuggestion: (id, segmentId, input) => json2("DELETE", path("documents", id, "segments", segmentId, "suggestion"), input),
96
+ applySuggestion: (id, segmentId, input) => json2("POST", path("documents", id, "segments", segmentId, "apply-suggestion"), input),
97
97
  exportDocument: async (id, request) => {
98
98
  const result = await response("POST", path("documents", id, "export"), {}, request);
99
99
  const body = await result.json();
@@ -111,7 +111,7 @@ function createPlatformClient(options) {
111
111
  if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
112
112
  let result;
113
113
  try {
114
- result = await json("GET", path("jobs", id), void 0, { signal });
114
+ result = await json2("GET", path("jobs", id), void 0, { signal });
115
115
  } catch (error) {
116
116
  if (deadlineSignal.aborted && !settings.signal?.aborted) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
117
117
  throw error;
@@ -479,8 +479,31 @@ function validateInlineEdit(segment, content) {
479
479
  if (order.every((value, i) => value === i)) return;
480
480
  const source = segment.sourceNode ? fragment(segment.sourceInner, segment.sourceNode) : void 0;
481
481
  const sourceCodes = source ? codes(source.root, source.xml).filter((code) => !code.close) : [];
482
+ validateCodeOrder(original, order, sourceCodes, old.root.namespaceUri ?? "");
483
+ }
484
+ function inlineCodeOrderError(source, target) {
485
+ const sourceCodes = codes(source, ""), targetCodes = codes(target, "");
486
+ const namespace = source.namespaceUri ?? "";
487
+ const positions = /* @__PURE__ */ new Map();
488
+ const key = (code) => (code.close ? "/" : "") + codeIdentity(code.node, true, namespace);
489
+ sourceCodes.forEach((code, index) => {
490
+ const identity = key(code), group = positions.get(identity) ?? [];
491
+ group.push(index);
492
+ positions.set(identity, group);
493
+ });
494
+ const order = targetCodes.map((code) => positions.get(key(code))?.shift() ?? -1);
495
+ if (sourceCodes.length !== order.length || order.includes(-1)) return void 0;
496
+ try {
497
+ validateCodeOrder(sourceCodes, order, sourceCodes.filter((code) => !code.close), namespace);
498
+ return void 0;
499
+ } catch (error) {
500
+ return error instanceof Error ? error.message : "\u6807\u7B7E\u987A\u5E8F\u65E0\u6548";
501
+ }
502
+ }
503
+ function validateCodeOrder(original, order, sourceCodes, namespace) {
504
+ if (order.every((value, i) => value === i)) return;
482
505
  const hintFor = (code) => {
483
- if ((code.node.namespaceUri ?? "") !== (old.root.namespaceUri ?? "")) return void 0;
506
+ if ((code.node.namespaceUri ?? "") !== namespace) return void 0;
484
507
  const matches = sourceCodes.filter((item) => codeIdentity(item.node, false) === codeIdentity(code.node, false));
485
508
  const sourceHint = matches.find((item) => ["no", "firstNo"].includes(item.node.attribs.canReorder ?? ""))?.node.attribs.canReorder;
486
509
  if (sourceHint && matches.length !== 1) throw new Error("ambiguous source editing hints require original code order");
@@ -491,7 +514,7 @@ function validateInlineEdit(segment, content) {
491
514
  let fixedOrder = false;
492
515
  for (let i = 0; i < original.length; i++) {
493
516
  const code = original[i], node = code.node;
494
- if (code.close || (node.namespaceUri ?? "") !== (old.root.namespaceUri ?? "")) continue;
517
+ if (code.close || (node.namespaceUri ?? "") !== namespace) continue;
495
518
  const name2 = node.local;
496
519
  if (node.attribs.isolated === "yes" || node.attribs.isolated === "true") fixedOrder = true;
497
520
  if (["bpt", "bx", "sc", "sm"].includes(name2)) stack.push(i);
@@ -737,10 +760,10 @@ function resolveContent(segment, edit) {
737
760
  }
738
761
  return escapeXmlText(edit.content);
739
762
  }
740
- function patchDocument(document, edits) {
763
+ function patchDocument(document2, edits) {
741
764
  const errors = [];
742
765
  const prepared = [];
743
- const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
766
+ const byId = new Map(document2.segments.map((segment) => [segment.id, segment]));
744
767
  for (const [segmentId, edit] of edits) {
745
768
  const segment = byId.get(segmentId);
746
769
  if (!segment) {
@@ -754,7 +777,7 @@ function patchDocument(document, edits) {
754
777
  try {
755
778
  const content = resolveContent(segment, edit);
756
779
  const target = segment.targetNode;
757
- const opening = target ? document.xml.slice(target.start, target.openEnd) : "";
780
+ const opening = target ? document2.xml.slice(target.start, target.openEnd) : "";
758
781
  const expandEmptyTarget = content !== "" && target !== void 0 && target.openEnd === target.end && opening.endsWith("/>");
759
782
  prepared.push({
760
783
  start: expandEmptyTarget ? target.start : segment.targetRange.start,
@@ -769,22 +792,22 @@ function patchDocument(document, edits) {
769
792
  }
770
793
  }
771
794
  if (errors.length > 0) {
772
- return { xml: document.xml, applied: 0, errors };
795
+ return { xml: document2.xml, applied: 0, errors };
773
796
  }
774
797
  prepared.sort((left, right) => right.start - left.start);
775
- let output = document.xml;
798
+ let output = document2.xml;
776
799
  let lastStart = Number.POSITIVE_INFINITY;
777
800
  for (const patch of prepared) {
778
801
  if (patch.end > lastStart) {
779
802
  return {
780
- xml: document.xml,
803
+ xml: document2.xml,
781
804
  applied: 0,
782
805
  errors: [{ segmentId: "overlap", message: "overlapping patch ranges" }]
783
806
  };
784
807
  }
785
808
  if (patch.start < 0 || patch.end > output.length || patch.start > patch.end) {
786
809
  return {
787
- xml: document.xml,
810
+ xml: document2.xml,
788
811
  applied: 0,
789
812
  errors: [{ segmentId: "range", message: "patch range out of bounds" }]
790
813
  };
@@ -794,10 +817,10 @@ function patchDocument(document, edits) {
794
817
  }
795
818
  return { xml: output, applied: prepared.length, errors: [] };
796
819
  }
797
- function textEdits(document, values) {
820
+ function textEdits(document2, values) {
798
821
  const edits = /* @__PURE__ */ new Map();
799
822
  for (const [segmentId, value] of values) {
800
- const segment = document.segments.find((candidate) => candidate.id === segmentId);
823
+ const segment = document2.segments.find((candidate) => candidate.id === segmentId);
801
824
  if (!segment || segment.targetTags.length > 0) continue;
802
825
  edits.set(segmentId, { content: value });
803
826
  }
@@ -849,10 +872,10 @@ function setAttribute(tag, name2, rawValue) {
849
872
  return ` ${name2}="${value}"${selfClosing === "/" ? "/>" : ">"}`;
850
873
  });
851
874
  }
852
- function patchSdlStates(document, updates) {
875
+ function patchSdlStates(document2, updates) {
853
876
  const errors = [];
854
877
  const patches = [];
855
- const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
878
+ const byId = new Map(document2.segments.map((segment) => [segment.id, segment]));
856
879
  for (const [segmentId, update] of updates) {
857
880
  const segment = byId.get(segmentId);
858
881
  if (!segment) {
@@ -868,7 +891,7 @@ function patchSdlStates(document, updates) {
868
891
  continue;
869
892
  }
870
893
  const node = segment.sdlNode;
871
- let tag = document.xml.slice(node.start, node.openEnd);
894
+ let tag = document2.xml.slice(node.start, node.openEnd);
872
895
  for (const [key, attribute] of ATTRIBUTE_NAMES) {
873
896
  const value = update[key];
874
897
  if (value === void 0) continue;
@@ -876,12 +899,12 @@ function patchSdlStates(document, updates) {
876
899
  }
877
900
  patches.push({ start: node.start, end: node.openEnd, content: tag, id: segmentId });
878
901
  }
879
- if (errors.length > 0) return { xml: document.xml, applied: 0, errors };
880
- if (patches.length === 0) return { xml: document.xml, applied: 0, errors: [] };
881
- const result = applyRangePatches(document.xml, patches);
902
+ if (errors.length > 0) return { xml: document2.xml, applied: 0, errors };
903
+ if (patches.length === 0) return { xml: document2.xml, applied: 0, errors: [] };
904
+ const result = applyRangePatches(document2.xml, patches);
882
905
  if (result.errors.length > 0) {
883
906
  return {
884
- xml: document.xml,
907
+ xml: document2.xml,
885
908
  applied: 0,
886
909
  errors: result.errors.map((error) => ({ segmentId: error.id, message: error.message }))
887
910
  };
@@ -890,7 +913,7 @@ function patchSdlStates(document, updates) {
890
913
  validateXml(result.xml);
891
914
  } catch (caught) {
892
915
  return {
893
- xml: document.xml,
916
+ xml: document2.xml,
894
917
  applied: 0,
895
918
  errors: [
896
919
  {
@@ -1005,8 +1028,8 @@ function readProperties(props) {
1005
1028
  function parseTmx(xml, options = {}) {
1006
1029
  const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
1007
1030
  validateXml(source);
1008
- const document = parseXml(source);
1009
- const root = elementChildren(document).find((child) => child.local === "tmx");
1031
+ const document2 = parseXml(source);
1032
+ const root = elementChildren(document2).find((child) => child.local === "tmx");
1010
1033
  if (!root) throw new Error("Not a TMX document: missing <tmx> root element");
1011
1034
  const warnings = [];
1012
1035
  const header = findChild(root, "header");
@@ -1288,8 +1311,8 @@ function preferredLanguageOrder(langSets) {
1288
1311
  function parseTbx(xml, options = {}) {
1289
1312
  const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
1290
1313
  validateXml(source);
1291
- const document = parseXml(source);
1292
- const root = elementChildren(document).find((child) => child.local === "tbx");
1314
+ const document2 = parseXml(source);
1315
+ const root = elementChildren(document2).find((child) => child.local === "tbx");
1293
1316
  if (!root) throw new Error("Not a TBX document: missing <tbx> root element");
1294
1317
  const warnings = [];
1295
1318
  const body = findChild2(root, "body");
@@ -2066,11 +2089,13 @@ function checkTags(context) {
2066
2089
  if (sourceTags.length === 0 && targetTags.length === 0) return;
2067
2090
  const source = context.segment.sourceNode ? inlineCodeSignatures(context.segment.sourceNode) : sourceTags;
2068
2091
  const target = context.segment.targetNode ? inlineCodeSignatures(context.segment.targetNode) : targetTags;
2069
- if (JSON.stringify([...source].sort()) === JSON.stringify([...target].sort())) return;
2092
+ const sameInventory = JSON.stringify([...source].sort()) === JSON.stringify([...target].sort());
2093
+ const orderError = sameInventory && context.segment.sourceNode && context.segment.targetNode ? inlineCodeOrderError(context.segment.sourceNode, context.segment.targetNode) : void 0;
2094
+ if (sameInventory && !orderError) return;
2070
2095
  draft(context, {
2071
2096
  ruleId: "TAG_MISMATCH",
2072
- message: `\u6807\u7B7E\u7684\u6570\u91CF\u3001\u6807\u8BC6\u6216\u539F\u751F\u4EE3\u7801\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4`,
2073
- evidence: JSON.stringify({ source, target }),
2097
+ message: orderError ? "\u6807\u7B7E\u7684\u914D\u5BF9\u6216\u987A\u5E8F\u4E0E\u6E90\u6587\u7EA6\u675F\u4E0D\u4E00\u81F4" : "\u6807\u7B7E\u7684\u6570\u91CF\u3001\u6807\u8BC6\u6216\u539F\u751F\u4EE3\u7801\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4",
2098
+ evidence: JSON.stringify({ source, target, ...orderError ? { orderError } : {} }),
2074
2099
  suggestion: "\u6838\u5BF9\u6807\u7B7E\u7684 ID\u3001\u914D\u5BF9\u5F15\u7528\u548C\u539F\u751F\u4EE3\u7801\uFF0C\u4FDD\u7559\u6E90\u6587\u4E2D\u7684\u6807\u7B7E"
2075
2100
  });
2076
2101
  }
@@ -2162,9 +2187,10 @@ function validMonthDay(month, day) {
2162
2187
  var DATE_PATTERNS = [
2163
2188
  /\b(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})\b/gu,
2164
2189
  /\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b/gu,
2165
- /(\d{4}|[〇零一二三四五六七八九]{4})年(\d{1,2})月(?:(\d{1,2})日)?/gu
2190
+ /(\d{4}|[〇零一二三四五六七八九]{4})\s*年\s*(\d{1,2})\s*月(?:\s*(\d{1,2})\s*日)?/gu
2166
2191
  ];
2167
2192
  function extractDates(value) {
2193
+ value = normalizeNumericText(value);
2168
2194
  const tokens = [];
2169
2195
  const push = (key, text, start) => {
2170
2196
  tokens.push({ key, text, span: { start, end: start + text.length } });
@@ -2262,7 +2288,7 @@ function extractDates(value) {
2262
2288
  }
2263
2289
  function maskSpans(value, spans) {
2264
2290
  if (spans.length === 0) return value;
2265
- const chars = Array.from(value);
2291
+ const chars = value.split("");
2266
2292
  for (const span of spans) {
2267
2293
  for (let index = span.start; index < span.end && index < chars.length; index += 1) chars[index] = " ";
2268
2294
  }
@@ -2282,10 +2308,14 @@ function normalizeNumber(raw) {
2282
2308
  if (/^\d+\.0+$/u.test(value)) value = value.replace(/\.0+$/u, "");
2283
2309
  return `${sign}${value}`;
2284
2310
  }
2311
+ function normalizeNumericText(value) {
2312
+ return value.replace(/[0-9.,+-]/gu, (character) => character.normalize("NFKC"));
2313
+ }
2285
2314
  function extractNumbers(value) {
2315
+ const normalized = normalizeNumericText(value);
2286
2316
  const masked = maskSpans(
2287
- value,
2288
- extractDates(value).map((token) => token.span)
2317
+ normalized,
2318
+ [...extractDates(normalized).map((token) => token.span), ...collectPlaceholders(normalized)]
2289
2319
  );
2290
2320
  const results = [];
2291
2321
  for (const match of masked.matchAll(NUMBER_RE)) results.push(normalizeNumber(match[0]));
@@ -2960,10 +2990,13 @@ function checkEmptyTargetGroup(context) {
2960
2990
  if (source.trim().length === 0) return;
2961
2991
  const targetMissing = context.segment.targetRange === null && context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2962
2992
  const empty = context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2963
- if (!targetMissing && !empty) return;
2993
+ const targetPlaceholders = collectPlaceholders(context.target);
2994
+ const hasVisibleBody = /[\p{L}\p{N}]/u.test(maskSpans(source, collectPlaceholders(source)));
2995
+ const missingBody = hasVisibleBody && (context.target.trim() === "" || targetPlaceholders.length > 0 && !/[\p{L}\p{N}]/u.test(maskSpans(context.target, targetPlaceholders)));
2996
+ if (!targetMissing && !empty && !missingBody) return;
2964
2997
  draft(context, {
2965
2998
  ruleId: "EMPTY_TARGET",
2966
- message: targetMissing ? "\u76EE\u6807\u7F3A\u5931\uFF08target \u5143\u7D20\u4E0D\u5B58\u5728\uFF09" : "\u8BD1\u6587\u4E3A\u7A7A\uFF08\u6E90\u6587\u975E\u7A7A\uFF09",
2999
+ message: targetMissing ? "\u76EE\u6807\u7F3A\u5931\uFF08target \u5143\u7D20\u4E0D\u5B58\u5728\uFF09" : empty ? "\u8BD1\u6587\u4E3A\u7A7A\uFF08\u6E90\u6587\u975E\u7A7A\uFF09" : "\u8BD1\u6587\u7F3A\u5C11\u6B63\u6587",
2967
3000
  evidence: `source: ${truncate(source)}`
2968
3001
  });
2969
3002
  }
@@ -2974,6 +3007,7 @@ function checkUntranslatedCopyGroup(context) {
2974
3007
  if (normalizedSource !== normalizeForCompare(target)) return;
2975
3008
  const policy = context.untranslatedCopy;
2976
3009
  if (policy?.whitelist?.some((entry) => normalizeForCompare(entry) === normalizedSource)) return;
3010
+ if (policy?.ignoreIdentifiers && /^(?=.*[0-9])[a-z0-9][a-z0-9._/+:%-]*$/u.test(normalizedSource)) return;
2977
3011
  const tokens = wordTokens(source);
2978
3012
  const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
2979
3013
  const maxShort = policy?.shortSegmentMaxLength ?? 4;
@@ -3199,20 +3233,26 @@ function runDeterministicChecks(segments, options = {}) {
3199
3233
  };
3200
3234
  }
3201
3235
 
3236
+ // packages/l10n-core/src/qa/report.ts
3237
+ function escapeCsvField(value) {
3238
+ if (!/[",\r\n]/u.test(value)) return value;
3239
+ return `"${value.replaceAll('"', '""')}"`;
3240
+ }
3241
+
3202
3242
  // packages/l10n-core/src/workflows/check.ts
3203
- function termMatchOptions(document, segment) {
3204
- const file = document.files[segment.location.fileIndex];
3243
+ function termMatchOptions(document2, segment) {
3244
+ const file = document2.files[segment.location.fileIndex];
3205
3245
  const options = {};
3206
3246
  if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
3207
3247
  if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
3208
3248
  return options;
3209
3249
  }
3210
- function checkDocument(document, options = {}) {
3250
+ function checkDocument(document2, options = {}) {
3211
3251
  const glossary = options.glossary ?? [];
3212
3252
  const segmentTermHits = /* @__PURE__ */ new Map();
3213
3253
  if (glossary.length > 0) {
3214
- for (const segment of document.segments) {
3215
- const termOptions = termMatchOptions(document, segment);
3254
+ for (const segment of document2.segments) {
3255
+ const termOptions = termMatchOptions(document2, segment);
3216
3256
  const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
3217
3257
  const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
3218
3258
  segmentTermHits.set(segment.id, {
@@ -3228,7 +3268,7 @@ function checkDocument(document, options = {}) {
3228
3268
  ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
3229
3269
  ...options.locale === void 0 ? {} : { locale: options.locale }
3230
3270
  };
3231
- const qa = runDeterministicChecks(document.segments, qaOptions);
3271
+ const qa = runDeterministicChecks(document2.segments, qaOptions);
3232
3272
  return { qa, segmentTermHits };
3233
3273
  }
3234
3274
 
@@ -3242,12 +3282,12 @@ function bestMatch(matches) {
3242
3282
  }
3243
3283
  return best;
3244
3284
  }
3245
- function translateDocument(document, tmEntries, options = {}) {
3285
+ function translateDocument(document2, tmEntries, options = {}) {
3246
3286
  const threshold = options.threshold ?? DEFAULT_THRESHOLD2;
3247
3287
  const warnings = [];
3248
3288
  const values = /* @__PURE__ */ new Map();
3249
3289
  let skipped = 0;
3250
- for (const segment of document.segments) {
3290
+ for (const segment of document2.segments) {
3251
3291
  if (segment.targetText.trim().length > 0) continue;
3252
3292
  if (segment.readOnlyReason) {
3253
3293
  warnings.push(`${segment.id}: read-only: ${segment.readOnlyReason}; skipped`);
@@ -3275,7 +3315,7 @@ function translateDocument(document, tmEntries, options = {}) {
3275
3315
  skipped += 1;
3276
3316
  continue;
3277
3317
  }
3278
- const file = document.files[segment.location.fileIndex];
3318
+ const file = document2.files[segment.location.fileIndex];
3279
3319
  const matchOptions = {
3280
3320
  threshold,
3281
3321
  maxMatches: 1,
@@ -3300,30 +3340,30 @@ function translateDocument(document, tmEntries, options = {}) {
3300
3340
  values.set(segment.id, target);
3301
3341
  }
3302
3342
  if (values.size === 0) {
3303
- return { xml: document.xml, applied: 0, skipped, warnings };
3343
+ return { xml: document2.xml, applied: 0, skipped, warnings };
3304
3344
  }
3305
- const patch = patchDocument(document, textEdits(document, values));
3345
+ const patch = patchDocument(document2, textEdits(document2, values));
3306
3346
  if (patch.errors.length > 0) {
3307
3347
  const details = patch.errors.map((error) => `${error.segmentId}: ${error.message}`);
3308
3348
  warnings.push(...details.map((detail) => `patch failed: ${detail}`));
3309
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3349
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
3310
3350
  }
3311
3351
  let originalHash;
3312
3352
  let patchedHash;
3313
3353
  try {
3314
- originalHash = structureSummary(document.xml).hash;
3354
+ originalHash = structureSummary(document2.xml).hash;
3315
3355
  patchedHash = structureSummary(patch.xml).hash;
3316
3356
  } catch (caught) {
3317
3357
  const message = caught instanceof Error ? caught.message : String(caught);
3318
3358
  warnings.push(`structure check failed: ${message}`);
3319
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3359
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
3320
3360
  }
3321
3361
  if (originalHash !== patchedHash) {
3322
3362
  warnings.push("structure summary changed after fill; refusing to write output");
3323
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3363
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
3324
3364
  }
3325
3365
  let xml = patch.xml;
3326
- if (options.setSdlState === true && document.format === "sdlxliff") {
3366
+ if (options.setSdlState === true && document2.format === "sdlxliff") {
3327
3367
  const conf = options.sdlConf ?? "Translated";
3328
3368
  try {
3329
3369
  const patchedDocument = parseXliff(xml);
@@ -3333,13 +3373,13 @@ function translateDocument(document, tmEntries, options = {}) {
3333
3373
  const state = patchSdlStates(patchedDocument, updates);
3334
3374
  if (state.errors.length > 0) {
3335
3375
  warnings.push(...state.errors.map((error) => `state patch failed: ${error.segmentId}: ${error.message}`));
3336
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3376
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
3337
3377
  }
3338
3378
  xml = state.xml;
3339
3379
  } catch (caught) {
3340
3380
  const message = caught instanceof Error ? caught.message : String(caught);
3341
3381
  warnings.push(`state patch failed: ${message}`);
3342
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3382
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
3343
3383
  }
3344
3384
  }
3345
3385
  return { xml, applied: patch.applied, skipped, warnings };
@@ -3602,11 +3642,11 @@ function parseSemanticResponse(raw, segments, options = {}) {
3602
3642
  }
3603
3643
  return { issues, rejected };
3604
3644
  }
3605
- async function runSemanticCheck(document, provider, options = {}) {
3645
+ async function runSemanticCheck(document2, provider, options = {}) {
3606
3646
  const batchSize = Math.max(1, options.batchSize ?? 20);
3607
3647
  const batches = [];
3608
- for (let index = 0; index < document.segments.length; index += batchSize) {
3609
- batches.push(document.segments.slice(index, index + batchSize));
3648
+ for (let index = 0; index < document2.segments.length; index += batchSize) {
3649
+ batches.push(document2.segments.slice(index, index + batchSize));
3610
3650
  }
3611
3651
  const issues = [];
3612
3652
  const failures = [];
@@ -3946,9 +3986,9 @@ function backfillOoxml(buffer, map, translations) {
3946
3986
  }
3947
3987
 
3948
3988
  // packages/l10n-core/src/workflows/polish.ts
3949
- function polishDocument(document, edits) {
3989
+ function polishDocument(document2, edits) {
3950
3990
  const normalized = /* @__PURE__ */ new Map();
3951
- const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
3991
+ const byId = new Map(document2.segments.map((segment) => [segment.id, segment]));
3952
3992
  const warnings = [];
3953
3993
  for (const [segmentId, value] of edits) {
3954
3994
  const segment = byId.get(segmentId);
@@ -3959,18 +3999,18 @@ function polishDocument(document, edits) {
3959
3999
  normalized.set(segmentId, typeof value === "string" ? { content: value } : value);
3960
4000
  }
3961
4001
  if (warnings.length > 0) {
3962
- return { xml: document.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
4002
+ return { xml: document2.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
3963
4003
  }
3964
- const before = structureSummary(document.xml).hash;
3965
- const patch = patchDocument(document, normalized);
4004
+ const before = structureSummary(document2.xml).hash;
4005
+ const patch = patchDocument(document2, normalized);
3966
4006
  if (patch.errors.length > 0) {
3967
4007
  warnings.push(...patch.errors.map((error) => `${error.segmentId}: ${error.message}`));
3968
- return { xml: document.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
4008
+ return { xml: document2.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
3969
4009
  }
3970
4010
  const after = structureSummary(patch.xml).hash;
3971
4011
  if (before !== after) {
3972
4012
  return {
3973
- xml: document.xml,
4013
+ xml: document2.xml,
3974
4014
  applied: 0,
3975
4015
  skipped: normalized.size,
3976
4016
  warnings: ["structure summary changed after polish; refusing to write"],
@@ -4868,6 +4908,609 @@ import { createHash as createHash3 } from "node:crypto";
4868
4908
  import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
4869
4909
  import { basename, resolve as resolve2 } from "node:path";
4870
4910
  import { defineTool as defineTool3 } from "@deepseek-ai/dsh-tools";
4911
+
4912
+ // packages/l10n-core/src/qa/review-server.ts
4913
+ import { randomBytes } from "node:crypto";
4914
+ import { createServer } from "node:http";
4915
+ import ExcelJS3 from "exceljs";
4916
+
4917
+ // packages/l10n-core/src/qa/review-data.ts
4918
+ import { unzipSync as unzipSync2 } from "fflate";
4919
+ var REVIEW_GROUPS = [
4920
+ { label: "\u6807\u70B9\u4E0E\u7A7A\u683C", rules: ["UNPAIRED_BRACKET", "PUNCT_DUP", "WHITESPACE"] },
4921
+ { label: "Tag \u4E0E\u53D8\u91CF", rules: ["TAG_MISMATCH", "PLACEHOLDER_MISSING", "PLACEHOLDER_EXTRA"] },
4922
+ { label: "\u6570\u5B57\u4E0E\u65E5\u671F", rules: ["NUMBER_MISMATCH", "DATE_MISMATCH"] },
4923
+ { label: "\u7A7A\u8BD1\u4E0E\u6B63\u6587\u7F3A\u5931", rules: ["EMPTY_TARGET"] },
4924
+ { label: "\u539F\u8BD1\u76F8\u540C", rules: ["UNTRANSLATED_COPY"] }
4925
+ ];
4926
+ var REVIEW_FILE_BYTES = 12 * 1024 * 1024;
4927
+ var REVIEW_FILE_LIMIT = 20;
4928
+ var FILE_EXTENSION = /\.(?:xlf|xliff|sdlxliff|mqxliff|mqxlz)$/iu;
4929
+ var categories = new Map(
4930
+ REVIEW_GROUPS.flatMap((group) => group.rules.map((rule) => [rule, group.label]))
4931
+ );
4932
+ var REVIEW_COLUMNS = {
4933
+ file: "\u6587\u4EF6",
4934
+ segment: "\u6BB5\u53F7",
4935
+ source: "\u539F\u6587",
4936
+ target: "\u8BD1\u6587",
4937
+ category: "\u5206\u7C7B",
4938
+ severity: "\u4E25\u91CD\u7A0B\u5EA6",
4939
+ rule: "\u68C0\u67E5\u9879",
4940
+ message: "\u95EE\u9898",
4941
+ evidence: "\u4F9D\u636E"
4942
+ };
4943
+ function reviewOptions(value) {
4944
+ if (typeof value !== "object" || value === null) throw new Error("\u68C0\u67E5\u8BBE\u7F6E\u65E0\u6548\u3002");
4945
+ const { rules, identicalWhitelist, customRules } = value;
4946
+ if (!Array.isArray(rules) || rules.some((rule) => typeof rule !== "string" || !categories.has(rule))) {
4947
+ throw new Error("\u68C0\u67E5\u8BBE\u7F6E\u5305\u542B\u672A\u77E5\u89C4\u5219\u3002");
4948
+ }
4949
+ if (!Array.isArray(identicalWhitelist) || identicalWhitelist.length > 100 || identicalWhitelist.some((term) => typeof term !== "string" || term.length > 300)) {
4950
+ throw new Error("\u539F\u8BD1\u76F8\u540C\u653E\u884C\u540D\u5355\u6700\u591A 100 \u6761\uFF0C\u6BCF\u6761\u4E0D\u8D85\u8FC7 300 \u5B57\u3002");
4951
+ }
4952
+ if (!Array.isArray(customRules) || customRules.length > 20) throw new Error("\u6700\u591A\u6DFB\u52A0 20 \u6761\u81EA\u5B9A\u4E49\u68C0\u67E5\u3002");
4953
+ for (const rule of customRules) {
4954
+ if (typeof rule !== "object" || rule === null || typeof rule.name !== "string" || !rule.name.trim() || rule.name.length > 80 || typeof rule.sourceContains !== "string" || rule.sourceContains.length > 300 || typeof rule.value !== "string" || !rule.value.trim() || rule.value.length > 300 || !["required", "forbidden", "maxLength"].includes(rule.kind) || rule.kind === "maxLength" && !/^[1-9]\d{0,4}$/u.test(rule.value)) {
4955
+ throw new Error("\u8BF7\u68C0\u67E5\u81EA\u5B9A\u4E49\u9879\u7684\u540D\u79F0\u3001\u68C0\u6D4B\u7C7B\u578B\u548C\u5339\u914D\u503C\u3002\u957F\u5EA6\u4E0A\u9650\u987B\u4E3A\u6B63\u6574\u6570\u3002");
4956
+ }
4957
+ }
4958
+ if (rules.length === 0 && customRules.length === 0) throw new Error("\u8BF7\u9009\u62E9\u81F3\u5C11\u4E00\u9879\u68C0\u67E5\u3002");
4959
+ return { rules, identicalWhitelist, customRules };
4960
+ }
4961
+ var decoder = new TextDecoder("utf-8", { fatal: true });
4962
+ function parseReviewFile(name2, bytes) {
4963
+ if (!FILE_EXTENSION.test(name2)) throw new Error("\u8BF7\u9009\u62E9 XLIFF\u3001SDLXLIFF\u3001MQXLIFF \u6216 MQXLZ \u6587\u4EF6\u3002");
4964
+ if (bytes.byteLength > REVIEW_FILE_BYTES) throw new Error("\u6587\u4EF6\u8D85\u8FC7 12 MB\u3002");
4965
+ if (!name2.toLowerCase().endsWith(".mqxlz")) {
4966
+ return [{ label: name2, document: parseXliff(decoder.decode(bytes), { filename: name2 }) }];
4967
+ }
4968
+ let size = 0;
4969
+ const entries = unzipSync2(bytes, {
4970
+ filter(entry) {
4971
+ if (!entry.name.toLowerCase().endsWith(".mqxliff")) return false;
4972
+ size += entry.originalSize;
4973
+ if (size > REVIEW_FILE_BYTES) throw new Error("MQXLZ \u4E2D\u7684\u53CC\u8BED\u5185\u5BB9\u89E3\u538B\u540E\u8D85\u8FC7 12 MB\u3002");
4974
+ return true;
4975
+ }
4976
+ });
4977
+ if (Object.keys(entries).length === 0) throw new Error("MQXLZ \u4E2D\u672A\u627E\u5230 MQXLIFF \u6587\u4EF6\u3002");
4978
+ return Object.entries(entries).map(([filename, content]) => ({
4979
+ label: `${name2} / ${filename}`,
4980
+ document: parseXliff(decoder.decode(content), { filename })
4981
+ }));
4982
+ }
4983
+ function rowLocation(file, document2, segment) {
4984
+ const { fileIndex, unitId, mid } = segment.location;
4985
+ return {
4986
+ file: document2.files.length === 1 ? file : `${file} / \u6587\u6863 ${fileIndex + 1}`,
4987
+ segment: mid === void 0 || mid === unitId ? unitId : `${unitId}/${mid}`,
4988
+ source: segment.sourceTags.length ? segment.sourceInner : segment.sourceText,
4989
+ target: segment.targetTags.length ? segment.targetInner : segment.targetText
4990
+ };
4991
+ }
4992
+ function customFinding(rule, segment) {
4993
+ if (!segment.sourceText.trim() || !segment.targetText.trim() || !segment.sourceText.includes(rule.sourceContains)) return void 0;
4994
+ const length = Array.from(segment.targetText).length;
4995
+ if (rule.kind === "required" && !segment.targetText.includes(rule.value)) return `\u8BD1\u6587\u7F3A\u5C11\u300C${rule.value}\u300D\u3002`;
4996
+ if (rule.kind === "forbidden" && segment.targetText.includes(rule.value)) return `\u8BD1\u6587\u5305\u542B\u7981\u7528\u5185\u5BB9\u300C${rule.value}\u300D\u3002`;
4997
+ if (rule.kind === "maxLength" && length > Number(rule.value)) return `\u8BD1\u6587\u957F ${length} \u5B57\uFF0C\u8D85\u8FC7\u4E0A\u9650 ${rule.value}\u3002`;
4998
+ return void 0;
4999
+ }
5000
+ function reviewDocument(file, document2, options) {
5001
+ const segments = document2.segments.filter((segment) => segment.readOnlyReason !== "translate=no");
5002
+ const { qa } = checkDocument({ ...document2, segments }, {
5003
+ enabledRules: options.rules,
5004
+ untranslatedCopy: { shortSegmentMaxLength: 0, ignoreIdentifiers: true, whitelist: options.identicalWhitelist }
5005
+ });
5006
+ const indexed = new Map(segments.map((segment) => [segment.id, segment]));
5007
+ const rows = qa.issues.map((issue) => {
5008
+ const candidate = issue.ruleId === "UNTRANSLATED_COPY";
5009
+ const segment = indexed.get(issue.segmentId);
5010
+ const same = segment.sourceText === segment.targetText ? "\u539F\u8BD1\u5B8C\u5168\u76F8\u540C" : "\u539F\u8BD1\u7ECF\u5F52\u4E00\u5316\u540E\u76F8\u540C";
5011
+ return {
5012
+ ...rowLocation(file, document2, segment),
5013
+ category: categories.get(issue.ruleId),
5014
+ severity: candidate ? "neutral" : issue.severity,
5015
+ rule: issue.ruleId,
5016
+ message: candidate ? `${same}\uFF0C\u8BF7\u786E\u8BA4\u662F\u5426\u9700\u8981\u7FFB\u8BD1\u3002` : issue.message,
5017
+ evidence: issue.evidence
5018
+ };
5019
+ });
5020
+ for (const segment of segments) {
5021
+ for (const rule of options.customRules) {
5022
+ const finding = customFinding(rule, segment);
5023
+ if (finding !== void 0) {
5024
+ rows.push({
5025
+ ...rowLocation(file, document2, segment),
5026
+ category: "\u81EA\u5B9A\u4E49",
5027
+ severity: "major",
5028
+ rule: rule.name,
5029
+ message: finding,
5030
+ evidence: rule.sourceContains ? `\u539F\u6587\u5305\u542B\u300C${rule.sourceContains}\u300D` : ""
5031
+ });
5032
+ }
5033
+ }
5034
+ }
5035
+ return { rows, segments: segments.length, incomplete: qa.rules.some((rule) => rule.status === "failed" || rule.status === "skipped") };
5036
+ }
5037
+ async function runQaReview(files, options) {
5038
+ if (files.length === 0 || files.length > REVIEW_FILE_LIMIT) throw new Error("\u8BF7\u9009\u62E9 1\u201320 \u4E2A\u53CC\u8BED\u6587\u4EF6\u3002");
5039
+ const report = { rows: [], files: [] };
5040
+ for (const file of files) {
5041
+ try {
5042
+ const parsed = parseReviewFile(file.name, new Uint8Array(await file.arrayBuffer()));
5043
+ let segments = 0;
5044
+ let incomplete = false;
5045
+ for (const { label, document: document2 } of parsed) {
5046
+ const result = reviewDocument(label, document2, options);
5047
+ for (const row of result.rows) report.rows.push(row);
5048
+ segments += result.segments;
5049
+ incomplete ||= result.incomplete;
5050
+ }
5051
+ if (segments === 0) throw new Error("\u672A\u627E\u5230\u53EF\u68C0\u67E5\u7684\u6BB5\u843D\u3002");
5052
+ report.files.push({ name: file.name, segments, incomplete });
5053
+ } catch (error) {
5054
+ report.files.push({
5055
+ name: file.name,
5056
+ segments: 0,
5057
+ incomplete: true,
5058
+ error: error instanceof Error ? error.message : "\u6587\u4EF6\u5904\u7406\u5931\u8D25\u3002"
5059
+ });
5060
+ }
5061
+ }
5062
+ return report;
5063
+ }
5064
+
5065
+ // packages/l10n-core/src/qa/review-page.ts
5066
+ var styles = `
5067
+ :root { color-scheme: light; --paper: #f4f6f9; --ink: #192438; --muted: #4b5a6f;
5068
+ --border: #d9e0ea; --blue: #164aa4; --surface: #fff; font: 15px/1.5 system-ui, sans-serif; }
5069
+ * { box-sizing: border-box; }
5070
+ body { margin: 0; color: var(--ink); background: var(--paper); }
5071
+ main { width: min(1480px, 100%); margin: auto; padding: 28px clamp(14px, 3vw, 36px) 44px; }
5072
+ .brand { color: var(--blue); font-weight: 700; letter-spacing: .04em; }
5073
+ h1 { font-size: clamp(26px, 4vw, 35px); margin: 4px 0 22px; letter-spacing: -.03em; }
5074
+ h2 { font-size: 18px; margin: 0 0 14px; }
5075
+ p { margin: 0; } small, .muted { color: var(--muted); }
5076
+ .layout { display: grid; grid-template-columns: minmax(250px, 285px) minmax(0, 1fr); gap: 18px; align-items: start; }
5077
+ .layout.offline { grid-template-columns: minmax(0, 1fr); }
5078
+ .panel { min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; }
5079
+ label, legend { font-weight: 600; }
5080
+ .field { display: grid; gap: 5px; }
5081
+ .field + .field { margin-top: 12px; }
5082
+ input, button, select, textarea { font: inherit; color: inherit; }
5083
+ input:not([type=checkbox]), select, textarea { background: #fff; width: 100%; min-width: 0;
5084
+ border: 1px solid #aab6c7; padding: 6px 8px; border-radius: 6px; }
5085
+ input[type=file] { padding: 7px 4px; font-size: 12px; }
5086
+ input::file-selector-button { font: inherit; margin-right: 6px; }
5087
+ fieldset { margin: 14px 0; padding: 8px 10px 10px; border: 1px solid var(--border); border-radius: 8px; }
5088
+ .checkbox { display: flex; align-items: center; gap: 6px; font-weight: 450; min-height: 30px; }
5089
+ input[type=checkbox] { accent-color: var(--blue); }
5090
+ summary { cursor: pointer; font-weight: 600; }
5091
+ .custom { display: grid; gap: 9px; margin: 10px 0; }
5092
+ .custom .field { margin: 0; }
5093
+ .custom input, .custom select { font-size: 13px; }
5094
+ .run, .filters, .exports, .pager { display: flex; flex-wrap: wrap; align-items: end; gap: 8px; }
5095
+ .run { margin-top: 18px; }
5096
+ button { background: #fff; border: 1px solid #aab6c7; border-radius: 6px; min-height: 34px; padding: 5px 11px; cursor: pointer; }
5097
+ button.primary { color: white; background: var(--blue); border-color: var(--blue); font-weight: 650; }
5098
+ button:disabled { opacity: .55; cursor: default; }
5099
+ :focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
5100
+ #status { min-height: 22px; margin-bottom: 10px; }
5101
+ #error, .fail { color: #a22628; }
5102
+ #files { margin: 4px 0 12px; padding-left: 20px; overflow-wrap: anywhere; }
5103
+ .filters { margin: 8px 0 12px; }
5104
+ .filters .field, .exports .field { flex: 1 1 125px; margin: 0; font-size: 13px; }
5105
+ .filters .search { flex-basis: 180px; }
5106
+ .exports { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 14px; align-items: end; }
5107
+ .exports .field { flex: 0 1 150px; }
5108
+ .table { overflow: auto; max-height: 64vh; border: 1px solid var(--border); border-radius: 8px; }
5109
+ table { width: 100%; min-width: 680px; border-collapse: collapse; table-layout: fixed; text-align: left; font-size: 13px; }
5110
+ th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; overflow-wrap: anywhere; white-space: pre-wrap; }
5111
+ th { position: sticky; top: 0; background: #edf2f8; }
5112
+ th:nth-child(1) { width: 17%; } th:nth-child(2), th:nth-child(3) { width: 23%; } th:nth-child(4) { width: 14%; }
5113
+ tr:last-child td { border-bottom: 0; }
5114
+ td small { display: block; }
5115
+ td details { margin-top: 5px; }
5116
+ .pager { justify-content: space-between; align-items: center; margin-top: 10px; }
5117
+ [hidden] { display: none !important; }
5118
+ @media (max-width: 950px) { .layout { grid-template-columns: minmax(0, 1fr); } }
5119
+ `;
5120
+ function reviewClient() {
5121
+ const get = (id) => document.getElementById(id);
5122
+ const form = document.getElementById("settings");
5123
+ const results = get("results");
5124
+ const reportData = document.getElementById("report-data");
5125
+ const search = get("search");
5126
+ const file = get("file-filter");
5127
+ const category = get("category");
5128
+ const severity = get("severity");
5129
+ const order = get("order");
5130
+ const pagerText = get("pager-text");
5131
+ const previous = get("previous");
5132
+ const next = get("next");
5133
+ const table = get("rows");
5134
+ const status = get("status");
5135
+ const error = get("error");
5136
+ const severityLabel = { critical: "\u4E25\u91CD", major: "\u4E3B\u8981", minor: "\u8F7B\u5FAE", neutral: "\u5F85\u6838\u67E5" };
5137
+ const severityRank = { critical: 0, major: 1, minor: 2, neutral: 3 };
5138
+ const collator = new Intl.Collator(void 0, { numeric: true });
5139
+ let report = { rows: [], files: [] };
5140
+ let filtered = [];
5141
+ let groups = [];
5142
+ let page = 0;
5143
+ const pageSize = 50;
5144
+ function fillOptions(select, values) {
5145
+ select.options.length = 1;
5146
+ for (const value of [...new Set(values)].sort(collator.compare)) select.add(new Option(value, value));
5147
+ }
5148
+ function cell(row, text) {
5149
+ const td = row.insertCell();
5150
+ td.textContent = text;
5151
+ return td;
5152
+ }
5153
+ function render() {
5154
+ const maxPage = Math.max(0, Math.ceil(groups.length / pageSize) - 1);
5155
+ page = Math.min(page, maxPage);
5156
+ table.replaceChildren();
5157
+ for (const group of groups.slice(page * pageSize, (page + 1) * pageSize)) {
5158
+ const issue = group[0];
5159
+ const row = document.createElement("tr");
5160
+ const location = cell(row, issue.file);
5161
+ const segment = document.createElement("small");
5162
+ segment.textContent = "\u6BB5 " + issue.segment;
5163
+ location.append(segment);
5164
+ cell(row, issue.source || "\u2014");
5165
+ cell(row, issue.target || "\u2014");
5166
+ const kind = cell(row, [...new Set(group.map((entry) => entry.category))].join("\uFF0F"));
5167
+ const level = document.createElement("small");
5168
+ const strongest = group.reduce((left, right) => (severityRank[left.severity] ?? 4) <= (severityRank[right.severity] ?? 4) ? left : right);
5169
+ level.textContent = severityLabel[strongest.severity] || strongest.severity;
5170
+ kind.append(level);
5171
+ const problem = cell(row, "");
5172
+ for (const finding of group) {
5173
+ const details = document.createElement("details");
5174
+ const summary = document.createElement("summary");
5175
+ summary.textContent = finding.message;
5176
+ const evidence = document.createElement("p");
5177
+ evidence.textContent = [finding.rule, finding.evidence].filter(Boolean).join(" \xB7 ");
5178
+ details.append(summary, evidence);
5179
+ problem.append(details);
5180
+ }
5181
+ table.append(row);
5182
+ }
5183
+ get("no-results").hidden = filtered.length > 0;
5184
+ get("table").hidden = filtered.length === 0;
5185
+ pagerText.textContent = groups.length + " \u4E2A\u6BB5 \xB7 " + filtered.length + " / " + report.rows.length + " \u6761\u95EE\u9898 \xB7 \u7B2C " + (page + 1) + " / " + (maxPage + 1) + " \u9875";
5186
+ previous.disabled = page === 0;
5187
+ next.disabled = page === maxPage;
5188
+ }
5189
+ function filter() {
5190
+ const query = search.value.trim().toLocaleLowerCase();
5191
+ filtered = report.rows.filter(
5192
+ (row) => (!file.value || row.file === file.value) && (!category.value || row.category === category.value) && (!severity.value || row.severity === severity.value) && (!query || Object.values(row).some((value) => value.toLocaleLowerCase().includes(query)))
5193
+ );
5194
+ filtered.sort((left, right) => {
5195
+ const byLocation = collator.compare(left.file, right.file) || collator.compare(left.segment, right.segment);
5196
+ if (order.value === "severity") return (severityRank[left.severity] ?? 4) - (severityRank[right.severity] ?? 4) || byLocation;
5197
+ if (order.value === "category") return collator.compare(left.category, right.category) || byLocation;
5198
+ return order.value === "desc" ? -byLocation : byLocation;
5199
+ });
5200
+ const grouped = /* @__PURE__ */ new Map();
5201
+ for (const issue of filtered) {
5202
+ const key = JSON.stringify([issue.file, issue.segment]);
5203
+ const existing = grouped.get(key);
5204
+ if (existing) existing.push(issue);
5205
+ else grouped.set(key, [issue]);
5206
+ }
5207
+ groups = [...grouped.values()];
5208
+ page = 0;
5209
+ render();
5210
+ }
5211
+ function show(data) {
5212
+ report = data;
5213
+ const failures = data.files.filter((entry) => entry.incomplete).length;
5214
+ const segments = data.files.reduce((total, entry) => total + entry.segments, 0);
5215
+ status.textContent = (failures ? "\u68C0\u67E5\u672A\u5B8C\u6574\u5B8C\u6210" : "\u68C0\u67E5\u5B8C\u6210") + " \xB7 " + data.files.length + " \u4E2A\u6587\u4EF6 \xB7 " + segments + " \u6BB5 \xB7 " + data.rows.length + " \u6761\u95EE\u9898" + (failures ? " \xB7 " + failures + " \u4E2A\u6587\u4EF6\u672A\u5B8C\u6574\u68C0\u67E5" : "");
5216
+ const fileList = get("files");
5217
+ fileList.replaceChildren();
5218
+ for (const entry of data.files.filter((item) => item.incomplete)) {
5219
+ const li = document.createElement("li");
5220
+ li.className = "fail";
5221
+ li.textContent = entry.name + " \xB7 " + (entry.error || "\u90E8\u5206\u89C4\u5219\u672A\u5B8C\u6210");
5222
+ fileList.append(li);
5223
+ }
5224
+ fileList.hidden = failures === 0;
5225
+ search.value = "";
5226
+ severity.value = "";
5227
+ order.value = "file";
5228
+ fillOptions(file, data.rows.map((row) => row.file));
5229
+ fillOptions(category, data.rows.map((row) => row.category));
5230
+ results.hidden = false;
5231
+ filter();
5232
+ }
5233
+ search.addEventListener("input", filter);
5234
+ for (const select of [file, category, severity, order]) select.addEventListener("change", filter);
5235
+ previous.addEventListener("click", () => {
5236
+ page -= 1;
5237
+ render();
5238
+ });
5239
+ next.addEventListener("click", () => {
5240
+ page += 1;
5241
+ render();
5242
+ });
5243
+ if (reportData) show(JSON.parse(reportData.textContent ?? "{}"));
5244
+ if (!form) return;
5245
+ const add = get("add-rule");
5246
+ const ruleList = get("custom-rules");
5247
+ add.addEventListener("click", () => {
5248
+ const rule = get("rule-template").content.cloneNode(true);
5249
+ const row = rule.querySelector("fieldset");
5250
+ row.querySelector("button").addEventListener("click", () => {
5251
+ row.remove();
5252
+ add.disabled = false;
5253
+ add.focus();
5254
+ });
5255
+ ruleList.append(rule);
5256
+ add.disabled = ruleList.children.length >= 20;
5257
+ row.querySelector("input").focus();
5258
+ });
5259
+ form.addEventListener("submit", async (event) => {
5260
+ event.preventDefault();
5261
+ const run = get("run");
5262
+ if (run.disabled) return;
5263
+ const body = new FormData();
5264
+ for (const upload of get("uploads").files ?? []) body.append("file", upload);
5265
+ const rules = Array.from(form.querySelectorAll("input[data-rules]:checked")).flatMap((item) => (item.dataset.rules ?? "").split(","));
5266
+ const identicalWhitelist = get("whitelist").value.split("\n").map((text) => text.trim()).filter(Boolean);
5267
+ const customRules = Array.from(ruleList.querySelectorAll("fieldset")).map((row) => ({
5268
+ name: row.querySelector("[data-name]").value.trim(),
5269
+ sourceContains: row.querySelector("[data-source]").value,
5270
+ kind: row.querySelector("select").value,
5271
+ value: row.querySelector("[data-value]").value.trim()
5272
+ }));
5273
+ body.append("options", JSON.stringify({ rules, identicalWhitelist, customRules }));
5274
+ run.disabled = true;
5275
+ error.textContent = "";
5276
+ status.textContent = "\u6B63\u5728\u68C0\u67E5\u6587\u4EF6\u2026";
5277
+ results.hidden = true;
5278
+ try {
5279
+ const response = await fetch("/check", { method: "POST", body });
5280
+ const data = await response.json();
5281
+ if (!response.ok) throw new Error(data.error || "\u68C0\u67E5\u5931\u8D25\u3002");
5282
+ show(data);
5283
+ } catch (cause) {
5284
+ status.textContent = "\u68C0\u67E5\u5931\u8D25";
5285
+ error.textContent = cause instanceof Error ? cause.message : "\u8BF7\u91CD\u8BD5\u3002";
5286
+ } finally {
5287
+ run.disabled = false;
5288
+ }
5289
+ });
5290
+ for (const button of document.querySelectorAll("button[data-export]")) {
5291
+ button.addEventListener("click", async () => {
5292
+ const format = button.dataset.export;
5293
+ if (button.disabled) return;
5294
+ button.disabled = true;
5295
+ error.textContent = "";
5296
+ try {
5297
+ const chosen = get("export-scope").value === "all" ? report.rows : filtered;
5298
+ const response = await fetch("/export/" + format, {
5299
+ method: "POST",
5300
+ headers: { "Content-Type": "application/json" },
5301
+ body: JSON.stringify({ rows: chosen, files: report.files })
5302
+ });
5303
+ if (!response.ok) throw new Error((await response.json()).error || "\u5BFC\u51FA\u5931\u8D25\u3002");
5304
+ const url = URL.createObjectURL(await response.blob());
5305
+ const anchor = document.createElement("a");
5306
+ anchor.href = url;
5307
+ anchor.download = "langlink-qa." + format;
5308
+ anchor.click();
5309
+ setTimeout(() => URL.revokeObjectURL(url), 1e3);
5310
+ } catch (cause) {
5311
+ error.textContent = cause instanceof Error ? cause.message : "\u5BFC\u51FA\u5931\u8D25\u3002";
5312
+ } finally {
5313
+ button.disabled = false;
5314
+ }
5315
+ });
5316
+ }
5317
+ }
5318
+ function renderReviewPage(report, nonce = "") {
5319
+ const offline = report !== void 0;
5320
+ const title = offline ? "\u53CC\u8BED QA \u62A5\u544A" : "\u53CC\u8BED\u6587\u4EF6\u68C0\u67E5";
5321
+ const settings = offline ? "" : `<form class="panel" id="settings">
5322
+ <h2>\u672C\u6B21\u68C0\u67E5</h2>
5323
+ <label class="field">\u53CC\u8BED\u6587\u4EF6<input type="file" id="uploads" multiple required accept=".xlf,.xliff,.mqxliff,.mqxlz,.sdlxliff">
5324
+ <small>\u6700\u591A 20 \u4E2A\u6587\u4EF6\uFF0C\u5355\u4E2A 12 MB\uFF0C\u5408\u8BA1 36 MB\u3002</small></label>
5325
+ <fieldset><legend>\u68C0\u67E5\u9879</legend>
5326
+ ${REVIEW_GROUPS.map((group) => `<label class="checkbox"><input type="checkbox" checked data-rules="${group.rules.join(",")}">${group.label}</label>`).join("")}
5327
+ </fieldset>
5328
+ <details><summary>\u5141\u8BB8\u539F\u8BD1\u76F8\u540C\u7684\u5185\u5BB9</summary>
5329
+ <label class="field"><small>\u54C1\u724C\u540D\u6216\u5176\u4ED6\u65E0\u9700\u7FFB\u8BD1\u7684\u6587\u672C\uFF0C\u6BCF\u884C\u4E00\u6761\u3002</small><textarea id="whitelist" rows="3"></textarea></label>
5330
+ </details>
5331
+ <fieldset><legend>\u81EA\u5B9A\u4E49\u68C0\u67E5</legend><div id="custom-rules"></div>
5332
+ <button type="button" id="add-rule">\u6DFB\u52A0\u68C0\u67E5\u9879</button>
5333
+ </fieldset>
5334
+ <div class="run"><button class="primary" id="run" type="submit">\u8FD0\u884C\u68C0\u67E5</button></div>
5335
+ <template id="rule-template"><fieldset class="custom"><legend>\u68C0\u67E5\u9879</legend>
5336
+ <label class="field">\u540D\u79F0<input data-name required maxlength="80"></label>
5337
+ <label class="field">\u539F\u6587\u5305\u542B\uFF08\u53EF\u9009\uFF09<input data-source maxlength="300"></label>
5338
+ <label class="field">\u8981\u6C42<select><option value="required">\u8BD1\u6587\u5FC5\u542B</option><option value="forbidden">\u8BD1\u6587\u7981\u7528</option><option value="maxLength">\u8BD1\u6587\u957F\u5EA6\u4E0A\u9650</option></select></label>
5339
+ <label class="field">\u8BCD\u8BED\u6216\u957F\u5EA6<input data-value required maxlength="300"></label>
5340
+ <button type="button">\u79FB\u9664\u68C0\u67E5\u9879</button>
5341
+ </fieldset></template>
5342
+ </form>`;
5343
+ const exports = offline ? "" : `<div class="exports">
5344
+ <label class="field">\u5BFC\u51FA\u8303\u56F4<select id="export-scope"><option value="filtered">\u5F53\u524D\u7B5B\u9009</option><option value="all">\u5168\u90E8\u7ED3\u679C</option></select></label>
5345
+ <button type="button" data-export="xlsx">Excel</button><button type="button" data-export="csv">CSV</button><button type="button" data-export="html">HTML</button></div>`;
5346
+ const data = offline ? `<script type="application/json" id="report-data">${JSON.stringify(report).replaceAll("<", "\\u003c")}</script>` : "";
5347
+ return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">
5348
+ <meta name="viewport" content="width=device-width, initial-scale=1"><title>${title} \xB7 LangLink</title>
5349
+ <style>${styles}</style></head><body><main><p class="brand">LangLink</p><h1>${title}</h1>
5350
+ <div class="layout${offline ? " offline" : ""}">${settings}
5351
+ <section class="panel" aria-labelledby="results-title"><h2 id="results-title">\u68C0\u67E5\u7ED3\u679C</h2>
5352
+ <p class="muted" id="status" role="status">${offline ? "" : "\u9009\u62E9\u6587\u4EF6\u548C\u68C0\u67E5\u9879\uFF0C\u7136\u540E\u8FD0\u884C\u68C0\u67E5\u3002"}</p>
5353
+ <p id="error" role="alert"></p><ul id="files" hidden></ul>
5354
+ <div id="results" hidden><div class="filters">
5355
+ <label class="field search">\u641C\u7D22<input type="search" id="search" placeholder="\u539F\u6587\u3001\u8BD1\u6587\u3001\u6BB5\u53F7\u6216\u89C4\u5219"></label>
5356
+ <label class="field">\u6587\u4EF6<select id="file-filter"><option value="">\u5168\u90E8\u6587\u4EF6</option></select></label>
5357
+ <div class="field"><label for="category">\u5206\u7C7B</label><select id="category"><option value="">\u5168\u90E8\u5206\u7C7B</option></select></div>
5358
+ <label class="field">\u4E25\u91CD\u7A0B\u5EA6<select id="severity"><option value="">\u5168\u90E8</option><option value="critical">\u4E25\u91CD</option><option value="major">\u4E3B\u8981</option><option value="minor">\u8F7B\u5FAE</option><option value="neutral">\u5F85\u6838\u67E5</option></select></label>
5359
+ <label class="field">\u6392\u5E8F<select id="order"><option value="file">\u6587\u4EF6\u548C\u6BB5\u53F7</option><option value="desc">\u6587\u4EF6\u548C\u6BB5\u53F7\u5012\u5E8F</option><option value="severity">\u4E25\u91CD\u7A0B\u5EA6</option><option value="category">\u95EE\u9898\u5206\u7C7B</option></select></label>
5360
+ </div><p id="no-results" hidden>\u5F53\u524D\u7B5B\u9009\u6CA1\u6709\u95EE\u9898\u8BB0\u5F55\u3002</p>
5361
+ <div class="table" id="table" tabindex="0" role="region" aria-label="\u95EE\u9898\u660E\u7EC6"><table>
5362
+ <thead><tr><th scope="col">\u6587\u4EF6\uFF0F\u6BB5\u53F7</th><th scope="col">\u539F\u6587</th><th scope="col">\u8BD1\u6587</th><th scope="col">\u5206\u7C7B</th><th scope="col">\u95EE\u9898</th></tr></thead>
5363
+ <tbody id="rows"></tbody></table></div>
5364
+ <div class="pager"><span id="pager-text" aria-live="polite"></span><div><button type="button" id="previous">\u4E0A\u4E00\u9875</button> <button type="button" id="next">\u4E0B\u4E00\u9875</button></div></div>
5365
+ ${exports}</div></section></div></main>${data}
5366
+ <script${nonce ? ` nonce="${nonce}"` : ""}>(${reviewClient.toString()})();</script></body></html>`;
5367
+ }
5368
+
5369
+ // packages/l10n-core/src/qa/review-server.ts
5370
+ var HOST = "127.0.0.1";
5371
+ var MAX_BODY = 40 * 1024 * 1024;
5372
+ var IDLE_TIMEOUT_MS = 30 * 60 * 1e3;
5373
+ var columnKeys = Object.keys(REVIEW_COLUMNS);
5374
+ var RequestTooLarge = class extends Error {
5375
+ };
5376
+ async function readBody(request) {
5377
+ const chunks = [];
5378
+ let size = 0;
5379
+ for await (const chunk of request.iterator({ destroyOnReturn: false })) {
5380
+ size += chunk.byteLength;
5381
+ if (size > MAX_BODY) throw new RequestTooLarge("\u672C\u6B21\u8BF7\u6C42\u8D85\u8FC7\u5927\u5C0F\u9650\u5236\u3002\u8BF7\u51CF\u5C11\u6587\u4EF6\u6216\u7F29\u5C0F\u5BFC\u51FA\u8303\u56F4\u3002");
5382
+ chunks.push(chunk);
5383
+ }
5384
+ return new Uint8Array(Buffer.concat(chunks, size));
5385
+ }
5386
+ function reply(response, status, type, body) {
5387
+ response.writeHead(status, { "Content-Type": type, "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" });
5388
+ response.end(body);
5389
+ }
5390
+ function json(response, status, value) {
5391
+ reply(response, status, "application/json; charset=utf-8", JSON.stringify(value));
5392
+ }
5393
+ function readReport(bytes) {
5394
+ const report = JSON.parse(new TextDecoder().decode(bytes));
5395
+ if (!report || !Array.isArray(report.rows) || !Array.isArray(report.files) || report.rows.some((row) => !row || columnKeys.some((key) => typeof row[key] !== "string")) || report.files.some((file) => !file || typeof file.name !== "string" || !Number.isSafeInteger(file.segments) || typeof file.incomplete !== "boolean" || file.error !== void 0 && typeof file.error !== "string")) {
5396
+ throw new Error("\u5BFC\u51FA\u6570\u636E\u65E0\u6548\u3002");
5397
+ }
5398
+ return report;
5399
+ }
5400
+ function csvCell(value) {
5401
+ return escapeCsvField(/^[\s\uFEFF]*[=+@-]|^[\t\r\n]/u.test(value) ? "'" + value : value);
5402
+ }
5403
+ async function exportReport(response, format, report) {
5404
+ response.setHeader("Content-Disposition", `attachment; filename="langlink-qa.${format}"`);
5405
+ if (format === "html") {
5406
+ reply(response, 200, "text/html; charset=utf-8", renderReviewPage(report));
5407
+ return;
5408
+ }
5409
+ const headers = Object.values(REVIEW_COLUMNS);
5410
+ if (format === "csv") {
5411
+ const lines = [headers.join(","), ...report.rows.map((row) => columnKeys.map((key) => csvCell(row[key])).join(","))];
5412
+ reply(response, 200, "text/csv; charset=utf-8", "\uFEFF" + lines.join("\r\n") + "\r\n");
5413
+ return;
5414
+ }
5415
+ if (report.rows.length > 1048575 || report.rows.some((row) => columnKeys.some((key) => row[key].length > 32767))) {
5416
+ throw new Error("\u7ED3\u679C\u8D85\u8FC7 Excel \u7684\u884C\u6570\u6216\u5355\u5143\u683C\u957F\u5EA6\u9650\u5236\uFF0C\u8BF7\u4F7F\u7528 CSV \u6216 HTML\u3002");
5417
+ }
5418
+ const workbook = new ExcelJS3.Workbook();
5419
+ const sheet = workbook.addWorksheet("QA", { views: [{ state: "frozen", ySplit: 1 }] });
5420
+ const widths = [32, 14, 48, 48, 19, 14, 27, 46, 60];
5421
+ sheet.columns = headers.map((header, index) => ({ header, width: widths[index] ?? 24 }));
5422
+ for (const row of report.rows) sheet.addRow(columnKeys.map((key) => row[key]));
5423
+ sheet.getRow(1).font = { bold: true };
5424
+ sheet.autoFilter = { from: { row: 1, column: 1 }, to: { row: Math.max(2, sheet.rowCount), column: headers.length } };
5425
+ const bytes = new Uint8Array(await workbook.xlsx.writeBuffer());
5426
+ reply(response, 200, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", bytes);
5427
+ }
5428
+ async function listenQaReview(onClose) {
5429
+ let origin;
5430
+ let authority;
5431
+ let expiry;
5432
+ const server = createServer((request, response) => {
5433
+ if (request.headers.host !== authority) {
5434
+ json(response, 403, { error: "\u8BF7\u4F7F\u7528\u672C\u673A\u68C0\u67E5\u9875\u9762\u3002" });
5435
+ return;
5436
+ }
5437
+ response.setHeader("Referrer-Policy", "no-referrer");
5438
+ response.setHeader("X-Frame-Options", "DENY");
5439
+ if (request.method === "GET" && request.url === "/") {
5440
+ expiry.refresh();
5441
+ const nonce = randomBytes(16).toString("base64");
5442
+ response.setHeader(
5443
+ "Content-Security-Policy",
5444
+ `default-src 'none'; script-src 'nonce-${nonce}'; style-src 'unsafe-inline'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'`
5445
+ );
5446
+ reply(response, 200, "text/html; charset=utf-8", renderReviewPage(void 0, nonce));
5447
+ return;
5448
+ }
5449
+ if (request.method !== "POST" || request.headers.origin !== origin) {
5450
+ json(response, 403, { error: "\u8BF7\u4ECE\u672C\u673A\u68C0\u67E5\u9875\u9762\u53D1\u8D77\u64CD\u4F5C\u3002" });
5451
+ return;
5452
+ }
5453
+ expiry.refresh();
5454
+ void (async () => {
5455
+ if (request.url === "/check") {
5456
+ const type = request.headers["content-type"] ?? "";
5457
+ if (!type.startsWith("multipart/form-data;")) throw new Error("\u8BF7\u9009\u62E9\u53CC\u8BED\u6587\u4EF6\u3002");
5458
+ const data = await new Response(await readBody(request), { headers: { "Content-Type": type } }).formData();
5459
+ const options = data.get("options");
5460
+ const files = data.getAll("file");
5461
+ if (typeof options !== "string" || !files.every((file) => file instanceof File)) {
5462
+ throw new Error("\u7F3A\u5C11\u6587\u4EF6\u6216\u68C0\u67E5\u8BBE\u7F6E\u3002");
5463
+ }
5464
+ if (files.reduce((size, file) => size + file.size, 0) > 36 * 1024 * 1024) throw new RequestTooLarge("\u6587\u4EF6\u5408\u8BA1\u8D85\u8FC7 36 MB\u3002");
5465
+ json(response, 200, await runQaReview(files, reviewOptions(JSON.parse(options))));
5466
+ return;
5467
+ }
5468
+ const format = /^\/export\/(xlsx|csv|html)$/u.exec(request.url ?? "")?.[1];
5469
+ if (format === void 0) {
5470
+ json(response, 404, { error: "\u627E\u4E0D\u5230\u6B64\u64CD\u4F5C\u3002" });
5471
+ return;
5472
+ }
5473
+ await exportReport(response, format, readReport(await readBody(request)));
5474
+ })().catch((cause) => {
5475
+ response.removeHeader("Content-Disposition");
5476
+ json(response, cause instanceof RequestTooLarge ? 413 : 400, {
5477
+ error: cause instanceof Error ? cause.message : "\u64CD\u4F5C\u5931\u8D25\u3002"
5478
+ });
5479
+ });
5480
+ });
5481
+ await new Promise((resolve6, reject) => {
5482
+ server.once("error", reject);
5483
+ server.listen(0, HOST, () => {
5484
+ server.off("error", reject);
5485
+ resolve6();
5486
+ });
5487
+ });
5488
+ const address = server.address();
5489
+ if (!address || typeof address === "string") throw new Error("\u65E0\u6CD5\u542F\u52A8\u672C\u673A\u68C0\u67E5\u9875\u9762\u3002");
5490
+ authority = `${HOST}:${address.port}`;
5491
+ origin = `http://${authority}`;
5492
+ expiry = setTimeout(() => server.close(), IDLE_TIMEOUT_MS);
5493
+ expiry.unref();
5494
+ server.unref();
5495
+ server.once("close", () => {
5496
+ clearTimeout(expiry);
5497
+ onClose?.();
5498
+ });
5499
+ return { url: origin + "/", close: () => new Promise((resolve6) => server.close(() => resolve6())) };
5500
+ }
5501
+ var activeReview;
5502
+ async function openQaReview() {
5503
+ activeReview ??= listenQaReview(() => {
5504
+ activeReview = void 0;
5505
+ }).catch((cause) => {
5506
+ activeReview = void 0;
5507
+ throw cause;
5508
+ });
5509
+ const { url } = await activeReview;
5510
+ return { url, message: `[\u6253\u5F00\u53CC\u8BED\u6587\u4EF6\u68C0\u67E5](${url})\uFF0C\u9009\u62E9\u6587\u4EF6\u548C\u89C4\u5219\u540E\u70B9\u51FB\u300C\u8FD0\u884C\u68C0\u67E5\u300D\u3002` };
5511
+ }
5512
+
5513
+ // packages/dsh-l10n/src/tools-phase1.ts
4871
5514
  var MAX_BYTES = 200 * 1024 * 1024;
4872
5515
  async function readBytes(path) {
4873
5516
  const absolute = resolve2(path);
@@ -4889,12 +5532,12 @@ async function readGlossary(path) {
4889
5532
  const text = await readUtf8(path);
4890
5533
  return /\.tbx$/iu.test(path) ? parseTbx(text) : parseGlossaryCsv(text);
4891
5534
  }
4892
- async function loadDocumentAssets(document, anchor, options) {
5535
+ async function loadDocumentAssets(document2, anchor, options) {
4893
5536
  const warnings = [];
4894
5537
  const tmEntries = [];
4895
5538
  const glossary = [];
4896
5539
  let used = [];
4897
- const file = document.files[0];
5540
+ const file = document2.files[0];
4898
5541
  let project;
4899
5542
  if (options.project !== void 0) {
4900
5543
  project = await findProject(options.project, anchor);
@@ -4945,8 +5588,8 @@ function createMatchTmTool() {
4945
5588
  },
4946
5589
  async execute(args) {
4947
5590
  const xml = await readUtf8(args.path);
4948
- const document = documentFromXml(xml, args.path);
4949
- const assets = await loadDocumentAssets(document, args.path, {
5591
+ const document2 = documentFromXml(xml, args.path);
5592
+ const assets = await loadDocumentAssets(document2, args.path, {
4950
5593
  ...args.project === void 0 ? {} : { project: args.project },
4951
5594
  ...args.tm === void 0 ? {} : { tm: args.tm }
4952
5595
  });
@@ -4957,8 +5600,8 @@ function createMatchTmTool() {
4957
5600
  const threshold = args.threshold ?? defaults?.threshold ?? 75;
4958
5601
  if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
4959
5602
  const limit = Math.max(1, args.maxSegments ?? 50);
4960
- const matches = document.segments.slice(0, limit).map((segment) => {
4961
- const file = document.files[segment.location.fileIndex];
5603
+ const matches = document2.segments.slice(0, limit).map((segment) => {
5604
+ const file = document2.files[segment.location.fileIndex];
4962
5605
  const candidates = matchTm(assets.tmEntries, segment.sourceText, {
4963
5606
  threshold,
4964
5607
  maxMatches: 1,
@@ -4974,8 +5617,8 @@ function createMatchTmTool() {
4974
5617
  };
4975
5618
  });
4976
5619
  return JSON.stringify({
4977
- format: document.format,
4978
- segments: document.segments.length,
5620
+ format: document2.format,
5621
+ segments: document2.segments.length,
4979
5622
  checked: matches.length,
4980
5623
  threshold,
4981
5624
  tmEntries: assets.tmEntries.length,
@@ -5003,8 +5646,8 @@ function createMatchTermsTool() {
5003
5646
  },
5004
5647
  async execute(args) {
5005
5648
  const xml = await readUtf8(args.path);
5006
- const document = documentFromXml(xml, args.path);
5007
- const assets = await loadDocumentAssets(document, args.path, {
5649
+ const document2 = documentFromXml(xml, args.path);
5650
+ const assets = await loadDocumentAssets(document2, args.path, {
5008
5651
  ...args.project === void 0 ? {} : { project: args.project },
5009
5652
  ...args.glossary === void 0 ? {} : { glossary: args.glossary }
5010
5653
  });
@@ -5015,8 +5658,8 @@ function createMatchTermsTool() {
5015
5658
  const limit = Math.max(1, args.maxSegments ?? 50);
5016
5659
  const hits = [];
5017
5660
  let forbidden = 0;
5018
- for (const segment of document.segments.slice(0, limit)) {
5019
- const file = document.files[segment.location.fileIndex];
5661
+ for (const segment of document2.segments.slice(0, limit)) {
5662
+ const file = document2.files[segment.location.fileIndex];
5020
5663
  for (const side of ["source", "target"]) {
5021
5664
  const result = matchTerms(segment[`${side}Text`], glossary, {
5022
5665
  ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
@@ -5039,9 +5682,9 @@ function createMatchTermsTool() {
5039
5682
  }
5040
5683
  }
5041
5684
  return JSON.stringify({
5042
- format: document.format,
5043
- segments: document.segments.length,
5044
- checked: Math.min(limit, document.segments.length),
5685
+ format: document2.format,
5686
+ segments: document2.segments.length,
5687
+ checked: Math.min(limit, document2.segments.length),
5045
5688
  entries: glossary.length,
5046
5689
  ...assets.project === void 0 ? {} : { project: assets.project.root },
5047
5690
  assetsUsed: assets.used,
@@ -5053,6 +5696,17 @@ function createMatchTermsTool() {
5053
5696
  }
5054
5697
  });
5055
5698
  }
5699
+ function createQaReviewTool() {
5700
+ return defineTool3({
5701
+ name: "l10n_qa_review",
5702
+ description: "Open the local bilingual QA page for selecting files, running deterministic checks, and filtering or exporting findings. Return the clickable page link; files are selected in the browser.",
5703
+ parameters: {},
5704
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
5705
+ async execute() {
5706
+ return (await openQaReview()).message;
5707
+ }
5708
+ });
5709
+ }
5056
5710
  function createCheckTool() {
5057
5711
  return defineTool3({
5058
5712
  name: "l10n_check",
@@ -5083,8 +5737,8 @@ function createCheckTool() {
5083
5737
  },
5084
5738
  async execute(args) {
5085
5739
  const xml = await readUtf8(args.path);
5086
- const document = documentFromXml(xml, args.path);
5087
- const assets = await loadDocumentAssets(document, args.path, {
5740
+ const document2 = documentFromXml(xml, args.path);
5741
+ const assets = await loadDocumentAssets(document2, args.path, {
5088
5742
  ...args.project === void 0 ? {} : { project: args.project },
5089
5743
  ...args.tm === void 0 ? {} : { tm: args.tm },
5090
5744
  ...args.glossary === void 0 ? {} : { glossary: args.glossary }
@@ -5094,7 +5748,7 @@ function createCheckTool() {
5094
5748
  ...args.sourceWhitelist ?? [],
5095
5749
  ...defaults?.untranslatedCopyWhitelist ?? []
5096
5750
  ];
5097
- const checked = checkDocument(document, {
5751
+ const checked = checkDocument(document2, {
5098
5752
  tmEntries: assets.tmEntries,
5099
5753
  glossary: assets.glossary,
5100
5754
  ...whitelist.length === 0 && args.shortSegmentMaxLength === void 0 ? {} : {
@@ -5113,8 +5767,8 @@ function createCheckTool() {
5113
5767
  }
5114
5768
  const limit = Math.max(1, args.maxIssues ?? 100);
5115
5769
  return JSON.stringify({
5116
- format: document.format,
5117
- segments: document.segments.length,
5770
+ format: document2.format,
5771
+ segments: document2.segments.length,
5118
5772
  checkedSegments: checked.qa.checkedSegments,
5119
5773
  coverage: checked.qa.coverage,
5120
5774
  totalIssues: checked.qa.issues.length,
@@ -5182,8 +5836,8 @@ function createTranslateTool() {
5182
5836
  const output = resolve2(args.output);
5183
5837
  if (input === output) throw new Error("refusing to overwrite the input file");
5184
5838
  const xml = await readUtf8(input);
5185
- const document = documentFromXml(xml, input);
5186
- const assets = await loadDocumentAssets(document, input, {
5839
+ const document2 = documentFromXml(xml, input);
5840
+ const assets = await loadDocumentAssets(document2, input, {
5187
5841
  ...args.project === void 0 ? {} : { project: args.project },
5188
5842
  ...args.tm === void 0 ? {} : { tm: args.tm }
5189
5843
  });
@@ -5193,7 +5847,7 @@ function createTranslateTool() {
5193
5847
  const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
5194
5848
  const threshold = args.threshold ?? defaults?.threshold ?? 75;
5195
5849
  if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
5196
- const result = translateDocument(document, assets.tmEntries, {
5850
+ const result = translateDocument(document2, assets.tmEntries, {
5197
5851
  threshold,
5198
5852
  ...args.setSdlConf === void 0 ? {} : { setSdlState: true, sdlConf: args.setSdlConf }
5199
5853
  });
@@ -5260,20 +5914,20 @@ function createValidateTool() {
5260
5914
  },
5261
5915
  async execute(args) {
5262
5916
  const xml = await readUtf8(args.path);
5263
- const document = documentFromXml(xml, args.path);
5917
+ const document2 = documentFromXml(xml, args.path);
5264
5918
  let missingTarget = 0;
5265
5919
  let tagMismatch = 0;
5266
- for (const segment of document.segments) {
5920
+ for (const segment of document2.segments) {
5267
5921
  if (!segment.targetRange) missingTarget += 1;
5268
5922
  if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
5269
5923
  }
5270
5924
  return JSON.stringify({
5271
5925
  valid: true,
5272
- format: document.format,
5273
- segments: document.segments.length,
5926
+ format: document2.format,
5927
+ segments: document2.segments.length,
5274
5928
  missingTarget,
5275
5929
  tagMismatch,
5276
- warnings: document.warnings
5930
+ warnings: document2.warnings
5277
5931
  });
5278
5932
  }
5279
5933
  });
@@ -5437,8 +6091,8 @@ function createPolishTool() {
5437
6091
  });
5438
6092
  }
5439
6093
  const xml = await readText(input);
5440
- const document = parseXliff(xml, { filename: input });
5441
- const result = polishDocument(document, edits);
6094
+ const document2 = parseXliff(xml, { filename: input });
6095
+ const result = polishDocument(document2, edits);
5442
6096
  if (result.applied === 0 && result.warnings.length > 0) {
5443
6097
  throw new Error(result.warnings.join("; "));
5444
6098
  }
@@ -5576,9 +6230,9 @@ function createSemanticCheckTool(llm) {
5576
6230
  async execute(args) {
5577
6231
  if (!llm) throw new Error("dsh LLM service is unavailable; configure a model provider first");
5578
6232
  const xml = await readFile4(resolve4(args.path), "utf8");
5579
- const document = parseXliff(xml, { filename: basename3(args.path) });
6233
+ const document2 = parseXliff(xml, { filename: basename3(args.path) });
5580
6234
  const result = await runSemanticCheck(
5581
- document,
6235
+ document2,
5582
6236
  createDshSemanticProvider(llm, args.provider, args.model),
5583
6237
  { batchSize: args.batchSize ?? 20 }
5584
6238
  );
@@ -5608,11 +6262,11 @@ async function inspectFile(path, signal) {
5608
6262
  const xml = await readFile5(absolute, { encoding: "utf8", signal });
5609
6263
  if (xml.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
5610
6264
  signal?.throwIfAborted();
5611
- const document = parseXliff(xml, { filename: basename4(absolute) });
6265
+ const document2 = parseXliff(xml, { filename: basename4(absolute) });
5612
6266
  const targetTagDistribution = {};
5613
6267
  let missingTarget = 0;
5614
6268
  let tagMismatch = 0;
5615
- for (const segment of document.segments) {
6269
+ for (const segment of document2.segments) {
5616
6270
  const key = String(segment.targetTags.length);
5617
6271
  targetTagDistribution[key] = (targetTagDistribution[key] ?? 0) + 1;
5618
6272
  if (!segment.targetRange) missingTarget += 1;
@@ -5620,17 +6274,17 @@ async function inspectFile(path, signal) {
5620
6274
  }
5621
6275
  const structure = structureSummary(xml);
5622
6276
  return {
5623
- format: document.format,
5624
- version: document.version,
6277
+ format: document2.format,
6278
+ version: document2.version,
5625
6279
  bytes: Buffer.byteLength(xml, "utf8"),
5626
- files: document.files.length,
5627
- languages: document.files.map(
6280
+ files: document2.files.length,
6281
+ languages: document2.files.map(
5628
6282
  (file) => `${file.sourceLanguage}->${file.targetLanguage}`
5629
6283
  ),
5630
- segments: document.segments.length,
6284
+ segments: document2.segments.length,
5631
6285
  missingTarget,
5632
6286
  tagMismatch,
5633
- warnings: document.warnings.length,
6287
+ warnings: document2.warnings.length,
5634
6288
  targetTagDistribution,
5635
6289
  structureElements: structure.count,
5636
6290
  structureHash: structure.hash.slice(0, 16)
@@ -5695,6 +6349,7 @@ function apply(ctx) {
5695
6349
  createMatchTmTool(),
5696
6350
  createMatchTermsTool(),
5697
6351
  createCheckTool(),
6352
+ createQaReviewTool(),
5698
6353
  createTranslateTool(),
5699
6354
  createConvertTool(),
5700
6355
  createValidateTool(),