@langlink/dsh-l10n 0.2.0 → 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
@@ -1,9 +1,171 @@
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";
1
+ // packages/dsh-l10n/src/tools-platform.ts
5
2
  import { defineTool } from "@deepseek-ai/dsh-tools";
6
3
 
4
+ // packages/workbench-sdk/src/index.ts
5
+ var PlatformApiError = class extends Error {
6
+ status;
7
+ code;
8
+ requestId;
9
+ constructor(status, body, requestId) {
10
+ super(typeof body.message === "string" ? body.message : typeof body.detail === "string" ? body.detail : `Platform request failed (${status})`);
11
+ this.name = "PlatformApiError";
12
+ this.status = status;
13
+ this.code = typeof body.code === "string" ? body.code : typeof body.error === "string" ? body.error : "request_failed";
14
+ this.requestId = typeof body.requestId === "string" ? body.requestId : requestId;
15
+ }
16
+ };
17
+ function createPlatformClient(options) {
18
+ const parsed = new URL(options.baseUrl);
19
+ const loopback = ["127.0.0.1", "localhost", "[::1]"].includes(parsed.hostname);
20
+ if (parsed.protocol !== "https:" && !(loopback && parsed.protocol === "http:") || parsed.username || parsed.password || parsed.search || parsed.hash || parsed.pathname !== "/") throw new Error("platform baseUrl must be an HTTPS origin (HTTP only on loopback)");
21
+ const base = parsed.origin;
22
+ const fetcher = options.fetcher ?? fetch;
23
+ const timeoutMs = options.timeoutMs ?? 3e4;
24
+ if (!Number.isInteger(timeoutMs) || timeoutMs < 100 || timeoutMs > 12e4) throw new Error("timeoutMs must be 100..120000");
25
+ const path = (...parts) => `/api/v1/${parts.map(encodeURIComponent).join("/")}`;
26
+ async function response(method, endpoint, body, request = {}) {
27
+ const timeout = AbortSignal.timeout(timeoutMs);
28
+ const signal = request.signal ? AbortSignal.any([request.signal, timeout]) : timeout;
29
+ signal.throwIfAborted();
30
+ const provider = options.token;
31
+ const token = typeof provider === "string" ? provider : await new Promise((resolve6, reject) => {
32
+ const aborted = () => {
33
+ signal.removeEventListener("abort", aborted);
34
+ reject(signal.reason);
35
+ };
36
+ signal.addEventListener("abort", aborted, { once: true });
37
+ Promise.resolve().then(() => {
38
+ signal.throwIfAborted();
39
+ return provider();
40
+ }).then(
41
+ (value) => {
42
+ signal.removeEventListener("abort", aborted);
43
+ resolve6(value);
44
+ },
45
+ (error) => {
46
+ signal.removeEventListener("abort", aborted);
47
+ reject(error);
48
+ }
49
+ );
50
+ if (signal.aborted) aborted();
51
+ });
52
+ signal.throwIfAborted();
53
+ if (!token || /[\r\n]/u.test(token)) throw new Error("a valid platform Token is required");
54
+ const headers = { authorization: `Bearer ${token}`, accept: "application/json" };
55
+ if (body !== void 0) headers["content-type"] = "application/json";
56
+ if (request.idempotencyKey) headers["idempotency-key"] = request.idempotencyKey;
57
+ const result = await fetcher(base + endpoint, { method, headers, redirect: "error", credentials: "omit", ...body === void 0 ? {} : { body: JSON.stringify(body) }, signal });
58
+ if (!result.ok) {
59
+ const parsed2 = await result.json().catch(() => ({}));
60
+ throw new PlatformApiError(result.status, parsed2 && typeof parsed2 === "object" && !Array.isArray(parsed2) ? parsed2 : {}, result.headers.get("x-request-id") ?? void 0);
61
+ }
62
+ return result;
63
+ }
64
+ async function json2(method, endpoint, body, request) {
65
+ return await (await response(method, endpoint, body, request)).json();
66
+ }
67
+ return {
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
+ exportDocument: async (id, request) => {
98
+ const result = await response("POST", path("documents", id, "export"), {}, request);
99
+ const body = await result.json();
100
+ return body;
101
+ },
102
+ async waitForJob(id, settings = {}) {
103
+ const duration = settings.timeoutMs ?? 12e4;
104
+ const deadline = Date.now() + duration;
105
+ const interval = settings.intervalMs ?? 500;
106
+ if (!Number.isInteger(duration) || duration < 1 || duration > 36e5 || !Number.isInteger(interval) || interval < 50 || interval > 5e3) throw new Error("invalid polling bounds");
107
+ const deadlineSignal = AbortSignal.timeout(duration);
108
+ const signal = settings.signal ? AbortSignal.any([settings.signal, deadlineSignal]) : deadlineSignal;
109
+ for (; ; ) {
110
+ settings.signal?.throwIfAborted();
111
+ if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
112
+ let result;
113
+ try {
114
+ result = await json2("GET", path("jobs", id), void 0, { signal });
115
+ } catch (error) {
116
+ if (deadlineSignal.aborted && !settings.signal?.aborted) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
117
+ throw error;
118
+ }
119
+ if (!["queued", "running"].includes(result.status)) return result;
120
+ if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
121
+ await new Promise((resolve6, reject) => {
122
+ const done = () => {
123
+ settings.signal?.removeEventListener("abort", abort);
124
+ resolve6();
125
+ };
126
+ const timer = setTimeout(done, Math.min(interval, Math.max(0, deadline - Date.now())));
127
+ const abort = () => {
128
+ clearTimeout(timer);
129
+ settings.signal?.removeEventListener("abort", abort);
130
+ reject(settings.signal?.reason);
131
+ };
132
+ settings.signal?.addEventListener("abort", abort, { once: true });
133
+ if (settings.signal?.aborted) abort();
134
+ });
135
+ }
136
+ }
137
+ };
138
+ }
139
+
140
+ // packages/dsh-l10n/src/tools-platform.ts
141
+ function createPlatformJobTool(options) {
142
+ const client = createPlatformClient(options);
143
+ return defineTool({
144
+ name: "cat_platform_job",
145
+ description: "Create a revision-safe platform suggestion/QA job for explicitly selected segments. Never auto-applies results. Use a stable idempotency key when repeating a request.",
146
+ parameters: {
147
+ documentId: { type: "string", required: true, description: "Authorized platform document ID" },
148
+ type: { type: "string", required: true, description: "translate, polish, aiqa or check" },
149
+ segmentIds: { type: "array", items: { type: "string" }, required: true, description: "Stable selected IDs" },
150
+ idempotencyKey: { type: "string", required: true, description: "Unique request key; reuse only for identical requests" }
151
+ },
152
+ output: { schema: { type: "object", additionalProperties: false, properties: { jobId: { type: "string" } } }, render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }] },
153
+ async execute(args, exec) {
154
+ if (!["translate", "polish", "aiqa", "check"].includes(args.type)) throw new Error("unknown platform job type");
155
+ return client.createJob(args.documentId, { type: args.type, segmentIds: args.segmentIds }, { idempotencyKey: args.idempotencyKey, signal: exec.signal });
156
+ }
157
+ });
158
+ }
159
+
160
+ // packages/dsh-l10n/src/tools-project.ts
161
+ import { defineTool as defineTool2 } from "@deepseek-ai/dsh-tools";
162
+
163
+ // packages/dsh-l10n/src/project.ts
164
+ import { createHash as createHash2 } from "node:crypto";
165
+ import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
166
+ import { dirname, isAbsolute, join, relative, resolve } from "node:path";
167
+ import ExcelJS2 from "exceljs";
168
+
7
169
  // packages/l10n-core/src/xml.ts
8
170
  import { createHash } from "node:crypto";
9
171
  import { Parser } from "htmlparser2";
@@ -29,6 +191,7 @@ function parseXml(xml) {
29
191
  children: []
30
192
  };
31
193
  const stack = [root];
194
+ let cdata = false;
32
195
  const parser = new Parser(
33
196
  {
34
197
  onopentag(name2, attribs) {
@@ -36,11 +199,19 @@ function parseXml(xml) {
36
199
  if (!parent) return;
37
200
  const start = parser.startIndex;
38
201
  const openEnd = parser.endIndex + 1;
202
+ const namespaces = { xml: "http://www.w3.org/XML/1998/namespace", ...parent.namespaces };
203
+ for (const [key, value] of Object.entries(attribs)) {
204
+ if (key === "xmlns") namespaces[""] = decodeXmlEntities(value);
205
+ else if (key.startsWith("xmlns:")) namespaces[key.slice(6)] = decodeXmlEntities(value);
206
+ }
207
+ const prefix = name2.includes(":") ? name2.slice(0, name2.indexOf(":")) : "";
39
208
  const node = {
40
209
  type: "element",
41
210
  name: name2,
42
211
  local: localName(name2),
43
212
  attribs: { ...attribs },
213
+ namespaceUri: namespaces[prefix] ?? "",
214
+ namespaces,
44
215
  start,
45
216
  openEnd,
46
217
  innerStart: openEnd,
@@ -64,9 +235,16 @@ function parseXml(xml) {
64
235
  parent.children.push({
65
236
  type: "text",
66
237
  text,
238
+ ...cdata ? { cdata: true } : {},
67
239
  start: parser.startIndex,
68
240
  end: parser.endIndex + 1
69
241
  });
242
+ },
243
+ oncdatastart() {
244
+ cdata = true;
245
+ },
246
+ oncdataend() {
247
+ cdata = false;
70
248
  }
71
249
  },
72
250
  {
@@ -81,8 +259,8 @@ function parseXml(xml) {
81
259
  parser.end();
82
260
  return root;
83
261
  }
84
- function validateXml(xml) {
85
- const parser = new SaxesParser({ xmlns: false });
262
+ function validateXml(xml, namespaces = false) {
263
+ const parser = new SaxesParser({ xmlns: namespaces });
86
264
  let error;
87
265
  parser.on("error", (value) => {
88
266
  error = value;
@@ -135,7 +313,7 @@ function decodeXmlEntities(value) {
135
313
  function textOf(node) {
136
314
  let value = "";
137
315
  for (const child of node.children) {
138
- value += child.type === "text" ? decodeXmlEntities(child.text) : textOf(child);
316
+ value += child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : textOf(child);
139
317
  }
140
318
  return value;
141
319
  }
@@ -156,12 +334,9 @@ function collectTextNodes(node) {
156
334
  function parseFragment(content) {
157
335
  validateXml(`<fragment>${content}</fragment>`);
158
336
  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));
337
+ const fragment2 = elementChildren(root)[0];
338
+ if (!fragment2) throw new Error("Fragment root missing");
339
+ return fragment2;
165
340
  }
166
341
  function structureSummary(xml) {
167
342
  const root = parseXml(xml);
@@ -188,7 +363,205 @@ function structureSummary(xml) {
188
363
  };
189
364
  }
190
365
 
366
+ // packages/l10n-core/src/formats/inline.ts
367
+ function container(node, namespace) {
368
+ return (node.namespaceUri ?? "") === namespace && ["g", "pc", "mrk"].includes(node.local) && node.attribs.translate !== "no" && node.attribs.mtype !== "protected";
369
+ }
370
+ function inlineCharacter(node) {
371
+ const read = (candidate) => {
372
+ if (candidate.local !== "ch" || !["MQXliff", "http://www.memoq.com/namespace"].includes(candidate.namespaceUri ?? "")) return void 0;
373
+ const value = candidate.attribs.val;
374
+ return value === void 0 ? void 0 : decodeXmlEntities(value);
375
+ };
376
+ const direct = read(node);
377
+ if (direct !== void 0) return direct;
378
+ if (node.local !== "ph" || !["", "urn:oasis:names:tc:xliff:document:1.2", "urn:oasis:names:tc:xliff:document:2.0"].includes(node.namespaceUri ?? "")) return void 0;
379
+ const elements = elementChildren(node);
380
+ if (elements.length === 1 && node.children.every((child) => child.type === "element" || !child.text.trim())) return read(elements[0]);
381
+ if (elements.length) return void 0;
382
+ const payload = textOf(node);
383
+ if (payload.length > 4096 || !payload.trim().startsWith("<")) return void 0;
384
+ try {
385
+ const context = { ...node, namespaces: { mq: "http://www.memoq.com/namespace", ...node.namespaces } };
386
+ const root = fragment(payload, context).root;
387
+ const codes2 = elementChildren(root);
388
+ if (codes2.length !== 1 || !root.children.every((child) => child.type === "element" || !child.text.trim())) return void 0;
389
+ return read(codes2[0]);
390
+ } catch {
391
+ return void 0;
392
+ }
393
+ }
394
+ function inlineText(root) {
395
+ const namespace = root.namespaceUri ?? "";
396
+ const walk = (node) => node.children.map((child) => child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : container(child, namespace) ? walk(child) : inlineCharacter(child) ?? "").join("");
397
+ return walk(root);
398
+ }
399
+ function editIdentity(code) {
400
+ const bindings = [];
401
+ const walk = (node, children) => {
402
+ bindings.push([node.name, node.namespaceUri ?? "", Object.keys(node.attribs).filter((name2) => name2.includes(":") && !name2.startsWith("xmlns:")).sort().map((name2) => [name2, node.namespaces?.[name2.split(":")[0]] ?? ""])]);
403
+ if (children) for (const child of elementChildren(node)) walk(child, true);
404
+ };
405
+ walk(code.node, code.raw.length === code.node.end - code.node.start);
406
+ return JSON.stringify([code.raw, bindings]);
407
+ }
408
+ function codeIdentity(node, native = true, namespace = node.namespaceUri ?? "") {
409
+ const identity = ["id", "rid", "startRef", "dataRef", "dataRefStart", "dataRefEnd", "pos"].map((key) => [key, node.attribs[key] ?? null]);
410
+ return JSON.stringify([
411
+ node.namespaceUri ?? "",
412
+ node.local,
413
+ identity,
414
+ native && !container(node, namespace) ? inlineCharacter(node) ?? textOf(node) : ""
415
+ ]);
416
+ }
417
+ function inlineCodeSignatures(root) {
418
+ const result = [], namespace = root.namespaceUri ?? "";
419
+ const walk = (parent) => {
420
+ for (const node of elementChildren(parent)) {
421
+ result.push(codeIdentity(node, true, namespace));
422
+ if (container(node, namespace)) walk(node);
423
+ }
424
+ };
425
+ walk(root);
426
+ return result;
427
+ }
428
+ function fragment(content, context) {
429
+ const escape = (value) => value.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;");
430
+ const ns = Object.entries(context.namespaces ?? {}).filter(([prefix2]) => prefix2 !== "xml").map(([prefix2, value]) => ` xmlns${prefix2 ? `:${prefix2}` : ""}="${escape(value)}"`).join("");
431
+ let prefix = "workbench";
432
+ while (Object.hasOwn(context.namespaces ?? {}, prefix)) prefix += "_";
433
+ const uri = context.namespaceUri ?? "";
434
+ const name2 = uri ? `${prefix}:wrapper` : "wrapper";
435
+ const binding = uri ? ` xmlns:${prefix}="${escape(uri)}"` : "";
436
+ const xml = `<${name2}${ns}${binding}>${content}</${name2}>`;
437
+ validateXml(xml, true);
438
+ return { root: elementChildren(parseXml(xml))[0], xml };
439
+ }
440
+ function usesSourceTagInventory(segment) {
441
+ return segment.targetTags.length === 0 && segment.targetInner.trim() === "" && segment.sourceTags.length > 0 && segment.sourceNode !== void 0;
442
+ }
443
+ function codes(root, xml) {
444
+ const result = [];
445
+ const namespace = root.namespaceUri ?? "";
446
+ const walk = (parent) => {
447
+ for (const child of elementChildren(parent)) {
448
+ if (container(child, namespace) && child.openEnd !== child.end) {
449
+ result.push({ raw: xml.slice(child.start, child.openEnd), node: child, close: false });
450
+ walk(child);
451
+ result.push({ raw: xml.slice(child.innerEnd, child.end), node: child, close: true });
452
+ } else result.push({ raw: xml.slice(child.start, child.end), node: child, close: false });
453
+ }
454
+ };
455
+ walk(root);
456
+ return result;
457
+ }
458
+ function validateInlineEdit(segment, content) {
459
+ if (content === segment.targetInner) return;
460
+ if (segment.readOnlyReason) throw new Error(`segment is read-only: ${segment.readOnlyReason}`);
461
+ if (!segment.targetNode) throw new Error("target node not available");
462
+ if (/<!\[CDATA\[|<!--|<\?/.test(content)) throw new Error("XML metadata cannot be introduced by a text edit");
463
+ const seeded = usesSourceTagInventory(segment);
464
+ const old = fragment(seeded ? segment.sourceInner : segment.targetInner, seeded ? segment.sourceNode : segment.targetNode);
465
+ const next = fragment(content, segment.targetNode);
466
+ const original = codes(old.root, old.xml), edited = codes(next.root, next.xml);
467
+ const queues = /* @__PURE__ */ new Map();
468
+ original.forEach((code, i) => {
469
+ const key = editIdentity(code), q = queues.get(key) ?? [];
470
+ q.push(i);
471
+ queues.set(key, q);
472
+ });
473
+ const order = edited.map((code) => {
474
+ const index = queues.get(editIdentity(code))?.shift();
475
+ if (index === void 0) throw new Error("inline code identity, attributes or protected payload changed");
476
+ return index;
477
+ });
478
+ if (order.length !== original.length || [...queues.values()].some((queue) => queue.length)) throw new Error("inline codes cannot be added or removed");
479
+ if (order.every((value, i) => value === i)) return;
480
+ const source = segment.sourceNode ? fragment(segment.sourceInner, segment.sourceNode) : void 0;
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;
505
+ const hintFor = (code) => {
506
+ if ((code.node.namespaceUri ?? "") !== namespace) return void 0;
507
+ const matches = sourceCodes.filter((item) => codeIdentity(item.node, false) === codeIdentity(code.node, false));
508
+ const sourceHint = matches.find((item) => ["no", "firstNo"].includes(item.node.attribs.canReorder ?? ""))?.node.attribs.canReorder;
509
+ if (sourceHint && matches.length !== 1) throw new Error("ambiguous source editing hints require original code order");
510
+ return code.node.attribs.canReorder === "no" || sourceHint === "no" ? "no" : code.node.attribs.canReorder === "firstNo" || sourceHint === "firstNo" ? "firstNo" : void 0;
511
+ };
512
+ const position = new Map(order.map((value, i) => [value, i]));
513
+ const stack = [], pairs = [];
514
+ let fixedOrder = false;
515
+ for (let i = 0; i < original.length; i++) {
516
+ const code = original[i], node = code.node;
517
+ if (code.close || (node.namespaceUri ?? "") !== namespace) continue;
518
+ const name2 = node.local;
519
+ if (node.attribs.isolated === "yes" || node.attribs.isolated === "true") fixedOrder = true;
520
+ if (["bpt", "bx", "sc", "sm"].includes(name2)) stack.push(i);
521
+ if (["ept", "ex", "ec", "em"].includes(name2)) {
522
+ const start = stack.pop(), open = start === void 0 ? void 0 : original[start].node;
523
+ const opener = { ept: "bpt", ex: "bx", ec: "sc", em: "sm" }[name2];
524
+ const matches = open && open.local === opener && (["ec", "em"].includes(name2) ? node.attribs.startRef === open.attribs.id && node.attribs.startRef !== void 0 : node.attribs.rid !== void 0 || open.attribs.rid !== void 0 ? node.attribs.rid === open.attribs.rid : true);
525
+ if (!matches || start === void 0) fixedOrder = true;
526
+ else pairs.push([start, i]);
527
+ }
528
+ }
529
+ if (stack.length) fixedOrder = true;
530
+ if (fixedOrder) throw new Error("isolated, overlapping or incomplete codes must retain their original order");
531
+ const active = [];
532
+ const ends = new Map(pairs.map(([start, end]) => [end, start]));
533
+ const starts = new Set(pairs.map(([start]) => start));
534
+ for (const index of order) {
535
+ if (starts.has(index)) active.push(index);
536
+ if (ends.has(index) && active.pop() !== ends.get(index)) throw new Error("inline code pairs are crossed or reversed");
537
+ }
538
+ const logical = original.map((code, index) => ({ code, index })).filter(({ code }) => !code.close);
539
+ let group = [];
540
+ const groups = [];
541
+ for (const { code, index } of logical) {
542
+ const hint = hintFor(code);
543
+ if (hint === "firstNo") {
544
+ group = [index];
545
+ groups.push(group);
546
+ } else if (hint === "no") {
547
+ if (!group.length) throw new Error("non-reorderable codes must retain their original order");
548
+ group.push(index);
549
+ } else group = [];
550
+ }
551
+ const logicalOrder = order.filter((index) => !original[index].close);
552
+ for (const members of groups) {
553
+ const start = logicalOrder.indexOf(members[0]);
554
+ if (members.some((index, offset) => logicalOrder[start + offset] !== index)) throw new Error("non-reorderable code sequence was split or reordered");
555
+ }
556
+ for (const { code, index } of logical) {
557
+ if (hintFor(code) === "no" && ["g", "pc", "mrk"].includes(code.node.local)) {
558
+ if (position.get(index) !== index) throw new Error("non-reorderable container moved");
559
+ }
560
+ }
561
+ }
562
+
191
563
  // packages/l10n-core/src/formats/xliff12.ts
564
+ var SDL_NAMESPACE = "http://sdl.com/FileTypes/SdlXliff/1.0";
192
565
  var SDL_DATATYPE = "x-sdlfilterframework2";
193
566
  function attr(node, name2) {
194
567
  return node.attribs[name2];
@@ -196,15 +569,15 @@ function attr(node, name2) {
196
569
  function isSegMrk(node) {
197
570
  return node.local === "mrk" && node.attribs.mtype === "seg";
198
571
  }
199
- function sdlElement(segDefs) {
200
- if (!segDefs) return void 0;
201
- return elementChildren(segDefs).find((child) => child.local === "seg");
572
+ function sdlElement(segDefs, mid) {
573
+ const nodes = segDefs ? elementChildren(segDefs).filter((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) : [];
574
+ const matches = mid === void 0 ? nodes : nodes.filter((node) => node.attribs.id === mid);
575
+ return matches.length === 1 ? matches[0] : void 0;
202
576
  }
203
- function sdlState(segDefs) {
204
- const seg = sdlElement(segDefs);
577
+ function sdlState(seg) {
205
578
  if (!seg) return void 0;
206
579
  const state = {};
207
- const mapping = [
580
+ const mapping2 = [
208
581
  ["id", "id"],
209
582
  ["conf", "conf"],
210
583
  ["origin", "origin"],
@@ -212,7 +585,7 @@ function sdlState(segDefs) {
212
585
  ["textMatch", "text-match"],
213
586
  ["locked", "locked"]
214
587
  ];
215
- for (const [key, source] of mapping) {
588
+ for (const [key, source] of mapping2) {
216
589
  const value = seg.attribs[source];
217
590
  if (value !== void 0) state[key] = value;
218
591
  }
@@ -223,7 +596,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
223
596
  const targetTags = targetContainer ? elementSignature(targetContainer) : [];
224
597
  const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
225
598
  if (!targetContainer) warnings.push(`${id}: target missing`);
226
- if (targetContainer && sourceTags.length > 0 && sourceTags.join("|") !== targetTags.join("|")) {
599
+ if (sourceContainer && targetContainer && JSON.stringify(inlineCodeSignatures(sourceContainer).sort()) !== JSON.stringify(inlineCodeSignatures(targetContainer).sort())) {
227
600
  warnings.push(`${id}: source/target inline tag signature differs`);
228
601
  }
229
602
  return {
@@ -232,8 +605,8 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
232
605
  location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
233
606
  sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
234
607
  targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
235
- sourceText: sourceContainer ? textOf(sourceContainer) : "",
236
- targetText: targetContainer ? textOf(targetContainer) : "",
608
+ sourceText: sourceContainer ? inlineText(sourceContainer) : "",
609
+ targetText: targetContainer ? inlineText(targetContainer) : "",
237
610
  sourceTags,
238
611
  targetTags,
239
612
  targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
@@ -244,115 +617,126 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
244
617
  };
245
618
  }
246
619
  function parseXliff(xml, options = {}) {
247
- validateXml(xml);
620
+ validateXml(xml, true);
248
621
  const root = elementChildren(parseXml(xml))[0];
249
- if (!root) throw new Error("No root element");
250
- if (root.local !== "xliff") throw new Error(`Not an XLIFF document: <${root.name}>`);
622
+ if (!root || root.local !== "xliff") throw new Error("Not an XLIFF document");
251
623
  const version = attr(root, "version") ?? "";
252
624
  const isXliff2 = version === "2.0" || version === "2.1";
253
- if (version !== "1.2" && !isXliff2) {
254
- throw new Error(
255
- `Unsupported XLIFF version: ${version || "(missing)"}; supported: 1.2 (SDLXLIFF/MQXLIFF), 2.0 and 2.1`
256
- );
257
- }
258
- const files = [];
259
- const segments = [];
260
- const warnings = [];
261
- const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
262
- const allElements = descendants(root);
263
- const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
264
- const fileNodes = allElements.filter((element) => element.local === "file");
625
+ if (version !== "1.2" && !isXliff2) throw new Error("Unsupported XLIFF version: " + (version || "(missing)") + "; supported: 1.2, 2.0 and 2.1");
626
+ const namespace = root.namespaceUri ?? "";
627
+ const expected = isXliff2 ? "urn:oasis:names:tc:xliff:document:2.0" : "urn:oasis:names:tc:xliff:document:1.2";
628
+ if (namespace !== "" && namespace !== expected) throw new Error("Unsupported XLIFF namespace: " + namespace);
629
+ const core = (node, local) => node.local === local && (node.namespaceUri ?? "") === namespace;
630
+ const children = (node, local) => elementChildren(node).filter((child) => core(child, local));
631
+ const files = [], segments = [], warnings = [];
265
632
  const filename = options.filename?.toLowerCase() ?? "";
266
- let format = version === "2.1" ? "xliff21" : version === "2.0" ? "xliff20" : filename.endsWith(".sdlxliff") ? "sdlxliff" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdlElement ? "sdlxliff" : "xliff12";
267
- fileNodes.forEach((fileNode, fileIndex) => {
633
+ const hasSdl = descendants(root).some((node) => node.namespaceUri === SDL_NAMESPACE);
634
+ let format = version === "2.1" ? "xliff21" : isXliff2 ? "xliff20" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdl || filename.endsWith(".sdlxliff") ? "sdlxliff" : "xliff12";
635
+ const ids = /* @__PURE__ */ new Map();
636
+ children(root, "file").forEach((fileNode, fileIndex) => {
268
637
  const datatype = attr(fileNode, "datatype");
269
- if (datatype === SDL_DATATYPE) format = "sdlxliff";
270
- const file = {
638
+ if (!isXliff2 && datatype === SDL_DATATYPE) format = "sdlxliff";
639
+ files.push({
271
640
  index: fileIndex,
272
- sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(fileNode, "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
273
- targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(fileNode, "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
641
+ sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
642
+ targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
274
643
  ...datatype === void 0 ? {} : { datatype }
275
- };
276
- files.push(file);
277
- const units = descendants(
278
- fileNode,
279
- (candidate) => candidate.local === "trans-unit" || candidate.local === "unit"
280
- );
281
- units.forEach((unit, unitIndex) => {
282
- const unitId = attr(unit, "id") ?? `unit-${unitIndex}`;
283
- if (isXliff2) {
284
- const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
285
- if (segmentNodes.length === 0) {
286
- warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
287
- return;
644
+ });
645
+ let unitIndex = 0;
646
+ const inherited = (node, parent) => node.attribs.translate === "no" ? false : node.attribs.translate === "yes" ? true : parent;
647
+ const walk = (parent, translate) => {
648
+ for (const unit of elementChildren(parent)) {
649
+ if (core(unit, "group") || core(unit, "body")) {
650
+ walk(unit, core(unit, "group") ? inherited(unit, translate) : translate);
651
+ continue;
288
652
  }
289
- segmentNodes.forEach((segmentNode, segmentIndex) => {
290
- const segmentChildren = elementChildren(segmentNode);
291
- const source = segmentChildren.find((child) => child.local === "source");
292
- const target = segmentChildren.find((child) => child.local === "target");
293
- const segmentId = attr(segmentNode, "id") ?? `seg-${segmentIndex}`;
294
- segments.push(
295
- makeSegment(
296
- fileIndex,
297
- unitId,
298
- segmentIndex,
299
- segments.length,
300
- source,
301
- target,
302
- segmentId,
303
- void 0,
304
- void 0,
305
- xml,
306
- warnings
307
- )
308
- );
309
- });
310
- return;
311
- }
312
- const children = elementChildren(unit);
313
- const sourceElement = children.find((child) => child.local === "source");
314
- const targetElement = children.find((child) => child.local === "target");
315
- const segSourceElement = children.find((child) => child.local === "seg-source");
316
- const segDefsElement = children.find((child) => child.local === "seg-defs");
317
- const sdl = sdlState(segDefsElement);
318
- const sdlNode = sdlElement(segDefsElement);
319
- if (segSourceElement) {
320
- const sourceMrks = descendants(segSourceElement, isSegMrk);
321
- const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
322
- sourceMrks.forEach((sourceMrk, segmentIndex) => {
323
- const mid = attr(sourceMrk, "mid");
324
- const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
325
- if (targetMrk === void 0 && targetMrks.length > 0) {
326
- warnings.push(`${fileIndex}:${unitId}:${mid ?? segmentIndex}: target mrk missing`);
653
+ if (!core(unit, isXliff2 ? "unit" : "trans-unit")) continue;
654
+ const unitId = attr(unit, "id") ?? "unit-" + unitIndex;
655
+ unitIndex += 1;
656
+ const mayTranslate = inherited(unit, translate);
657
+ const data = /* @__PURE__ */ Object.create(null);
658
+ const duplicateData = /* @__PURE__ */ new Set();
659
+ for (const original of children(unit, "originalData")) for (const node of children(original, "data")) {
660
+ const id = node.attribs.id;
661
+ if (!id) continue;
662
+ if (Object.hasOwn(data, id)) duplicateData.add(id);
663
+ else data[id] = textOf(node);
664
+ }
665
+ for (const id of duplicateData) {
666
+ delete data[id];
667
+ warnings.push(fileIndex + ":" + unitId + ": ambiguous originalData reference " + id);
668
+ }
669
+ const segDefs = elementChildren(unit).find((node) => node.local === "seg-defs" && node.namespaceUri === SDL_NAMESPACE);
670
+ const push = (source2, target2, mid, index, reason) => {
671
+ const sdlNode = sdlElement(segDefs, mid), sdl = sdlState(sdlNode);
672
+ const segment = makeSegment(fileIndex, unitId, index, segments.length, source2, target2, mid, sdl, sdlNode, xml, warnings);
673
+ if (Object.keys(data).length) segment.originalData = data;
674
+ const markup = target2 && /<!\[CDATA\[|<!--|<\?/.test(segment.targetInner);
675
+ const ambiguousSdl = segDefs && elementChildren(segDefs).some((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) && !sdlNode;
676
+ const readOnly = reason ?? (!mayTranslate ? "translate=no" : ambiguousSdl ? "SDL segment state cannot be mapped uniquely" : ["true", "yes", "1"].includes(sdl?.locked ?? "") ? "SDL segment locked" : markup ? "CDATA, comments or processing instructions require lossless read-only handling" : void 0);
677
+ if (readOnly) {
678
+ segment.readOnlyReason = readOnly;
679
+ warnings.push(segment.id + ": read-only: " + readOnly);
327
680
  }
328
- segments.push(
329
- makeSegment(
330
- fileIndex,
331
- unitId,
332
- segmentIndex,
333
- segments.length,
334
- sourceMrk,
335
- targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
336
- mid,
337
- sdl,
338
- sdlNode,
339
- xml,
340
- warnings
341
- )
342
- );
343
- });
344
- return;
345
- }
346
- if (sourceElement || targetElement) {
347
- segments.push(
348
- makeSegment(fileIndex, unitId, 0, segments.length, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
681
+ const existing = ids.get(segment.id);
682
+ if (existing) {
683
+ existing.readOnlyReason = "ambiguous segment identity";
684
+ segment.readOnlyReason = "ambiguous segment identity";
685
+ do {
686
+ segment.id += ":duplicate:" + segment.index;
687
+ } while (ids.has(segment.id));
688
+ warnings.push(existing.id + ": ambiguous segment identity; read-only");
689
+ }
690
+ ids.set(segment.id, segment);
691
+ segments.push(segment);
692
+ };
693
+ if (isXliff2) {
694
+ const nodes = children(unit, "segment");
695
+ if (!nodes.length) warnings.push(fileIndex + ":" + unitId + ": unit has no supported segment");
696
+ nodes.forEach((node, index) => push(
697
+ children(node, "source")[0],
698
+ children(node, "target")[0],
699
+ attr(node, "id") ?? "seg-" + index,
700
+ index,
701
+ children(node, "source").length > 1 || children(node, "target").length > 1 ? "ambiguous source/target containers" : void 0
702
+ ));
703
+ continue;
704
+ }
705
+ const source = children(unit, "source")[0], target = children(unit, "target")[0], segSource = children(unit, "seg-source")[0];
706
+ const sourceMrks = segSource ? descendants(segSource, (node) => core(node, "mrk") && isSegMrk(node)) : [];
707
+ if (sourceMrks.length) {
708
+ const targetMrks = target ? descendants(target, (node) => core(node, "mrk") && isSegMrk(node)) : [];
709
+ sourceMrks.forEach((mrk, index) => {
710
+ const mid = attr(mrk, "mid");
711
+ const sourceMatches = sourceMrks.filter((node) => node.attribs.mid === mid);
712
+ const matches = mid ? targetMrks.filter((node) => node.attribs.mid === mid) : [];
713
+ const whole = sourceMrks.length === 1 && !targetMrks.length ? target : void 0;
714
+ const unique = mid !== void 0 && sourceMatches.length === 1 && matches.length === 1;
715
+ const mapped = unique ? matches[0] : whole;
716
+ push(mrk, mapped, mid, index, !mapped || sourceMrks.length > 1 && (!mid || sourceMatches.length !== 1) ? "segmented target cannot be mapped uniquely by mid" : void 0);
717
+ });
718
+ } else if (source || target || segSource) push(
719
+ source ?? segSource,
720
+ target,
721
+ void 0,
722
+ 0,
723
+ children(unit, "source").length > 1 || children(unit, "target").length > 1 ? "ambiguous source/target containers" : void 0
349
724
  );
350
- } else {
351
- warnings.push(`${fileIndex}:${unitId}: no source/target`);
725
+ else warnings.push(fileIndex + ":" + unitId + ": no source/target");
352
726
  }
353
- });
727
+ };
728
+ if (isXliff2) walk(fileNode, inherited(fileNode, true));
729
+ else for (const body of children(fileNode, "body")) walk(body, true);
354
730
  });
355
- if (hasSdlNamespace || hasSdlElement) format = "sdlxliff";
731
+ const ranged = segments.filter((segment) => segment.targetRange).sort((a, b) => a.targetRange.start - b.targetRange.start || b.targetRange.end - a.targetRange.end);
732
+ let previous;
733
+ for (const segment of ranged) {
734
+ if (previous && (segment.targetRange.start < previous.targetRange.end || segment.targetRange.start === previous.targetRange.start)) {
735
+ previous.readOnlyReason = segment.readOnlyReason = "overlapping target containers";
736
+ warnings.push(segment.id + ": overlapping target containers; read-only");
737
+ }
738
+ if (!previous || segment.targetRange.end > previous.targetRange.end) previous = segment;
739
+ }
356
740
  return { format, version, files, segments, warnings, xml };
357
741
  }
358
742
 
@@ -361,32 +745,25 @@ function escapeXmlText(value) {
361
745
  return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
362
746
  }
363
747
  function resolveContent(segment, edit) {
748
+ if (segment.readOnlyReason && !(edit.xml && edit.content === segment.targetInner)) throw new Error(`segment is read-only: ${segment.readOnlyReason}`);
364
749
  const hasInlineTags = segment.targetTags.length > 0;
365
750
  if (hasInlineTags) {
366
751
  if (!edit.xml) {
367
752
  throw new Error("target contains inline tags; provide a validated XML fragment with xml=true");
368
753
  }
369
- const signature = fragmentSignature(edit.content);
370
- if (signature.join("|") !== segment.targetTags.join("|")) {
371
- throw new Error(
372
- `inline tag signature mismatch: expected [${segment.targetTags.join(", ")}], got [${signature.join(", ")}]`
373
- );
374
- }
754
+ validateInlineEdit(segment, edit.content);
375
755
  return edit.content;
376
756
  }
377
757
  if (edit.xml) {
378
- const signature = fragmentSignature(edit.content);
379
- if (signature.length > 0) {
380
- throw new Error("target has no inline tags; XML fragment must not add tags");
381
- }
758
+ validateInlineEdit(segment, edit.content);
382
759
  return edit.content;
383
760
  }
384
761
  return escapeXmlText(edit.content);
385
762
  }
386
- function patchDocument(document, edits) {
763
+ function patchDocument(document2, edits) {
387
764
  const errors = [];
388
765
  const prepared = [];
389
- const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
766
+ const byId = new Map(document2.segments.map((segment) => [segment.id, segment]));
390
767
  for (const [segmentId, edit] of edits) {
391
768
  const segment = byId.get(segmentId);
392
769
  if (!segment) {
@@ -398,10 +775,14 @@ function patchDocument(document, edits) {
398
775
  continue;
399
776
  }
400
777
  try {
778
+ const content = resolveContent(segment, edit);
779
+ const target = segment.targetNode;
780
+ const opening = target ? document2.xml.slice(target.start, target.openEnd) : "";
781
+ const expandEmptyTarget = content !== "" && target !== void 0 && target.openEnd === target.end && opening.endsWith("/>");
401
782
  prepared.push({
402
- start: segment.targetRange.start,
403
- end: segment.targetRange.end,
404
- content: resolveContent(segment, edit)
783
+ start: expandEmptyTarget ? target.start : segment.targetRange.start,
784
+ end: expandEmptyTarget ? target.end : segment.targetRange.end,
785
+ content: expandEmptyTarget ? `${opening.slice(0, -2)}>${content}</${target.name}>` : content
405
786
  });
406
787
  } catch (caught) {
407
788
  errors.push({
@@ -411,22 +792,22 @@ function patchDocument(document, edits) {
411
792
  }
412
793
  }
413
794
  if (errors.length > 0) {
414
- return { xml: document.xml, applied: 0, errors };
795
+ return { xml: document2.xml, applied: 0, errors };
415
796
  }
416
797
  prepared.sort((left, right) => right.start - left.start);
417
- let output = document.xml;
798
+ let output = document2.xml;
418
799
  let lastStart = Number.POSITIVE_INFINITY;
419
800
  for (const patch of prepared) {
420
801
  if (patch.end > lastStart) {
421
802
  return {
422
- xml: document.xml,
803
+ xml: document2.xml,
423
804
  applied: 0,
424
805
  errors: [{ segmentId: "overlap", message: "overlapping patch ranges" }]
425
806
  };
426
807
  }
427
808
  if (patch.start < 0 || patch.end > output.length || patch.start > patch.end) {
428
809
  return {
429
- xml: document.xml,
810
+ xml: document2.xml,
430
811
  applied: 0,
431
812
  errors: [{ segmentId: "range", message: "patch range out of bounds" }]
432
813
  };
@@ -436,10 +817,10 @@ function patchDocument(document, edits) {
436
817
  }
437
818
  return { xml: output, applied: prepared.length, errors: [] };
438
819
  }
439
- function textEdits(document, values) {
820
+ function textEdits(document2, values) {
440
821
  const edits = /* @__PURE__ */ new Map();
441
822
  for (const [segmentId, value] of values) {
442
- const segment = document.segments.find((candidate) => candidate.id === segmentId);
823
+ const segment = document2.segments.find((candidate) => candidate.id === segmentId);
443
824
  if (!segment || segment.targetTags.length > 0) continue;
444
825
  edits.set(segmentId, { content: value });
445
826
  }
@@ -491,10 +872,10 @@ function setAttribute(tag, name2, rawValue) {
491
872
  return ` ${name2}="${value}"${selfClosing === "/" ? "/>" : ">"}`;
492
873
  });
493
874
  }
494
- function patchSdlStates(document, updates) {
875
+ function patchSdlStates(document2, updates) {
495
876
  const errors = [];
496
877
  const patches = [];
497
- const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
878
+ const byId = new Map(document2.segments.map((segment) => [segment.id, segment]));
498
879
  for (const [segmentId, update] of updates) {
499
880
  const segment = byId.get(segmentId);
500
881
  if (!segment) {
@@ -505,8 +886,12 @@ function patchSdlStates(document, updates) {
505
886
  errors.push({ segmentId, message: "sdl:seg node missing" });
506
887
  continue;
507
888
  }
889
+ if (segment.readOnlyReason) {
890
+ errors.push({ segmentId, message: `segment is read-only: ${segment.readOnlyReason}` });
891
+ continue;
892
+ }
508
893
  const node = segment.sdlNode;
509
- let tag = document.xml.slice(node.start, node.openEnd);
894
+ let tag = document2.xml.slice(node.start, node.openEnd);
510
895
  for (const [key, attribute] of ATTRIBUTE_NAMES) {
511
896
  const value = update[key];
512
897
  if (value === void 0) continue;
@@ -514,12 +899,12 @@ function patchSdlStates(document, updates) {
514
899
  }
515
900
  patches.push({ start: node.start, end: node.openEnd, content: tag, id: segmentId });
516
901
  }
517
- if (errors.length > 0) return { xml: document.xml, applied: 0, errors };
518
- if (patches.length === 0) return { xml: document.xml, applied: 0, errors: [] };
519
- 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);
520
905
  if (result.errors.length > 0) {
521
906
  return {
522
- xml: document.xml,
907
+ xml: document2.xml,
523
908
  applied: 0,
524
909
  errors: result.errors.map((error) => ({ segmentId: error.id, message: error.message }))
525
910
  };
@@ -528,7 +913,7 @@ function patchSdlStates(document, updates) {
528
913
  validateXml(result.xml);
529
914
  } catch (caught) {
530
915
  return {
531
- xml: document.xml,
916
+ xml: document2.xml,
532
917
  applied: 0,
533
918
  errors: [
534
919
  {
@@ -643,8 +1028,8 @@ function readProperties(props) {
643
1028
  function parseTmx(xml, options = {}) {
644
1029
  const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
645
1030
  validateXml(source);
646
- const document = parseXml(source);
647
- const root = elementChildren(document).find((child) => child.local === "tmx");
1031
+ const document2 = parseXml(source);
1032
+ const root = elementChildren(document2).find((child) => child.local === "tmx");
648
1033
  if (!root) throw new Error("Not a TMX document: missing <tmx> root element");
649
1034
  const warnings = [];
650
1035
  const header = findChild(root, "header");
@@ -702,6 +1087,35 @@ function parseTmx(xml, options = {}) {
702
1087
  }
703
1088
  return { entries, warnings };
704
1089
  }
1090
+ function mergeKey(entry) {
1091
+ const normalize = (value) => value.normalize("NFKC").replace(/\s+/gu, " ").trim();
1092
+ const context = entry.context;
1093
+ return [
1094
+ normalizeLanguage(entry.sourceLanguage),
1095
+ normalizeLanguage(entry.targetLanguage),
1096
+ normalize(entry.source),
1097
+ normalize(entry.target),
1098
+ normalize(context?.previousSource ?? ""),
1099
+ normalize(context?.nextSource ?? "")
1100
+ ].join("\0");
1101
+ }
1102
+ function mergeTmEntries(...inputs) {
1103
+ const entries = [];
1104
+ const seen = /* @__PURE__ */ new Set();
1105
+ let duplicates = 0;
1106
+ for (const input of inputs) {
1107
+ for (const entry of input) {
1108
+ const key = mergeKey(entry);
1109
+ if (seen.has(key)) {
1110
+ duplicates += 1;
1111
+ continue;
1112
+ }
1113
+ seen.add(key);
1114
+ entries.push(entry);
1115
+ }
1116
+ }
1117
+ return { entries, duplicates };
1118
+ }
705
1119
 
706
1120
  // packages/l10n-core/src/assets/glossary.ts
707
1121
  var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
@@ -841,6 +1255,123 @@ function parseGlossaryCsv(text, options = {}) {
841
1255
  return { entries, warnings };
842
1256
  }
843
1257
 
1258
+ // packages/l10n-core/src/assets/tbx.ts
1259
+ function findChild2(parent, local) {
1260
+ return elementChildren(parent).find((child) => child.local === local);
1261
+ }
1262
+ function findDescendant2(parent, local) {
1263
+ for (const child of elementChildren(parent)) {
1264
+ if (child.local === local) return child;
1265
+ const nested = findDescendant2(child, local);
1266
+ if (nested) return nested;
1267
+ }
1268
+ return void 0;
1269
+ }
1270
+ function langSetLanguage(langSet) {
1271
+ return langSet.attribs["xml:lang"] ?? langSet.attribs.lang ?? "";
1272
+ }
1273
+ function findLangSet(langSets, language) {
1274
+ const wanted = normalizeLanguage(language);
1275
+ const exact = langSets.find((langSet) => normalizeLanguage(langSetLanguage(langSet)) === wanted);
1276
+ if (exact) return exact;
1277
+ return langSets.find((langSet) => languageMatches(langSetLanguage(langSet), language));
1278
+ }
1279
+ function termOf(langSet) {
1280
+ const tig = findChild2(langSet, "tig");
1281
+ if (!tig) return void 0;
1282
+ const term = findDescendant2(tig, "term");
1283
+ const text = term ? textOf(term).trim() : "";
1284
+ return text === "" ? void 0 : text;
1285
+ }
1286
+ function isForbiddenLangSet(langSet) {
1287
+ const walk = (parent) => {
1288
+ for (const child of elementChildren(parent)) {
1289
+ if (child.local === "descrip" || child.local === "termNote") {
1290
+ const type = (child.attribs.type ?? child.attribs.name ?? "").trim().toLowerCase();
1291
+ const value = textOf(child).trim().toLowerCase();
1292
+ if (type === "administrativestatus" && /forbidden|deprecated/u.test(value)) return true;
1293
+ if (type === "forbidden" && /^(?:true|yes|1)$/u.test(value)) return true;
1294
+ }
1295
+ if (walk(child)) return true;
1296
+ }
1297
+ return false;
1298
+ };
1299
+ return walk(langSet);
1300
+ }
1301
+ function preferredLanguageOrder(langSets) {
1302
+ const seen = [];
1303
+ for (const langSet of langSets) {
1304
+ const language = langSetLanguage(langSet);
1305
+ if (language !== "" && !seen.some((item) => normalizeLanguage(item) === normalizeLanguage(language))) {
1306
+ seen.push(language);
1307
+ }
1308
+ }
1309
+ return seen;
1310
+ }
1311
+ function parseTbx(xml, options = {}) {
1312
+ const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
1313
+ validateXml(source);
1314
+ const document2 = parseXml(source);
1315
+ const root = elementChildren(document2).find((child) => child.local === "tbx");
1316
+ if (!root) throw new Error("Not a TBX document: missing <tbx> root element");
1317
+ const warnings = [];
1318
+ const body = findChild2(root, "body");
1319
+ if (!body) {
1320
+ warnings.push("TBX: missing <body> element; no entries parsed");
1321
+ return { entries: [], warnings };
1322
+ }
1323
+ const termEntries = elementChildren(body).filter((child) => child.local === "termEntry");
1324
+ if (termEntries.length === 0) {
1325
+ warnings.push("TBX: no <termEntry> elements found");
1326
+ return { entries: [], warnings };
1327
+ }
1328
+ const declared = preferredLanguageOrder(
1329
+ termEntries.flatMap((entry) => elementChildren(entry).filter((child) => child.local === "langSet"))
1330
+ );
1331
+ const sourceLanguage = options.sourceLanguage ?? declared[0];
1332
+ if (sourceLanguage === void 0) {
1333
+ warnings.push("TBX: source language could not be determined; no entries parsed");
1334
+ return { entries: [], warnings };
1335
+ }
1336
+ const targetLanguage = options.targetLanguage ?? declared.find(
1337
+ (language) => !languageMatches(language, sourceLanguage) && normalizeLanguage(language) !== normalizeLanguage(sourceLanguage)
1338
+ );
1339
+ if (targetLanguage === void 0) {
1340
+ warnings.push("TBX: target language could not be determined; no entries parsed");
1341
+ return { entries: [], warnings };
1342
+ }
1343
+ const entries = [];
1344
+ for (const [index, termEntry] of termEntries.entries()) {
1345
+ const label = termEntry.attribs.id ?? `#${index + 1}`;
1346
+ const langSets = elementChildren(termEntry).filter((child) => child.local === "langSet");
1347
+ const sourceLangSet = findLangSet(langSets, sourceLanguage);
1348
+ const targetLangSet = findLangSet(langSets, targetLanguage);
1349
+ if (!sourceLangSet) {
1350
+ warnings.push(`TBX: termEntry ${label} has no langSet for source language "${sourceLanguage}"; skipped`);
1351
+ continue;
1352
+ }
1353
+ if (!targetLangSet) {
1354
+ warnings.push(`TBX: termEntry ${label} has no langSet for target language "${targetLanguage}"; skipped`);
1355
+ continue;
1356
+ }
1357
+ const sourceTerm = termOf(sourceLangSet);
1358
+ const targetTerm = termOf(targetLangSet);
1359
+ if (sourceTerm === void 0 || targetTerm === void 0) {
1360
+ warnings.push(`TBX: termEntry ${label} has no <term> text on one side; skipped`);
1361
+ continue;
1362
+ }
1363
+ entries.push({
1364
+ id: `tbx-${entries.length + 1}`,
1365
+ source: sourceTerm,
1366
+ target: targetTerm,
1367
+ sourceLanguage,
1368
+ targetLanguage,
1369
+ ...isForbiddenLangSet(sourceLangSet) || isForbiddenLangSet(targetLangSet) ? { forbidden: true } : {}
1370
+ });
1371
+ }
1372
+ return { entries, warnings };
1373
+ }
1374
+
844
1375
  // packages/l10n-core/src/assets/tm-matcher.ts
845
1376
  var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
846
1377
  var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
@@ -1396,7 +1927,7 @@ function matchTerms(text, entries, options = {}) {
1396
1927
  return { hits, issues: [], warnings };
1397
1928
  }
1398
1929
 
1399
- // packages/l10n-core/src/qa/rules.ts
1930
+ // packages/l10n-core/src/qa/catalog.ts
1400
1931
  var RULES = [
1401
1932
  // structural class
1402
1933
  { id: "EMPTY_TARGET", severity: "critical" },
@@ -1411,8 +1942,11 @@ var RULES = [
1411
1942
  // content class
1412
1943
  { id: "NUMBER_MISMATCH", severity: "major" },
1413
1944
  { id: "DATE_MISMATCH", severity: "major" },
1945
+ { id: "DATE_FORMAT", severity: "minor", requiresLocale: true },
1414
1946
  { id: "DUPLICATE_WORD", severity: "major" },
1415
1947
  { id: "TERM_MISSING", severity: "major", requiresTerms: true },
1948
+ { id: "TERMINOLOGY_INCONSISTENT", severity: "major", requiresTerms: true, scope: "document" },
1949
+ { id: "SOURCE_TARGET_INCONSISTENT", severity: "major", scope: "document" },
1416
1950
  { id: "SOURCE_LEFTOVER", severity: "major" },
1417
1951
  { id: "PUNCT_DUP", severity: "major" },
1418
1952
  { id: "TRAILING_COLON", severity: "minor" },
@@ -1422,9 +1956,24 @@ var RULES = [
1422
1956
  { id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
1423
1957
  { id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
1424
1958
  ];
1959
+ var QA_RULE_CATALOG = Object.freeze(RULES.map((rule, index) => Object.freeze({
1960
+ ruleId: rule.id,
1961
+ code: `QA-${String(index + 1).padStart(3, "0")}`,
1962
+ severity: rule.severity,
1963
+ defaultEnabled: rule.defaultOff !== true,
1964
+ scope: rule.scope ?? "segment",
1965
+ requiresTerms: rule.requiresTerms === true,
1966
+ requiresLocale: rule.requiresLocale === true
1967
+ })));
1968
+
1969
+ // packages/l10n-core/src/qa/rules.ts
1425
1970
  var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
1426
1971
  var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
1427
1972
  var TERM_RULE_IDS = RULES.filter((rule) => rule.requiresTerms).map((rule) => rule.id);
1973
+ var LOCALE_RULE_IDS = RULES.filter((rule) => rule.requiresLocale).map((rule) => rule.id);
1974
+ var DOCUMENT_RULE_IDS = new Set(
1975
+ RULES.filter((rule) => rule.scope === "document").map((rule) => rule.id)
1976
+ );
1428
1977
  var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
1429
1978
  function normalizeForCompare(value) {
1430
1979
  return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
@@ -1538,15 +2087,16 @@ function checkTags(context) {
1538
2087
  const sourceTags = context.segment.sourceTags;
1539
2088
  const targetTags = context.segment.targetTags;
1540
2089
  if (sourceTags.length === 0 && targetTags.length === 0) return;
1541
- const sourceSignature = sourceTags.join("|");
1542
- const targetSignature = targetTags.join("|");
1543
- if (sourceSignature === targetSignature) return;
1544
- const sameMultiset = sourceTags.length === targetTags.length && JSON.stringify([...sourceTags].sort()) === JSON.stringify([...targetTags].sort());
2090
+ const source = context.segment.sourceNode ? inlineCodeSignatures(context.segment.sourceNode) : sourceTags;
2091
+ const target = context.segment.targetNode ? inlineCodeSignatures(context.segment.targetNode) : targetTags;
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;
1545
2095
  draft(context, {
1546
2096
  ruleId: "TAG_MISMATCH",
1547
- 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(", ")}]`,
1548
- evidence: `source tags: [${sourceTags.join(", ")}], target tags: [${targetTags.join(", ")}]`,
1549
- suggestion: "\u4FDD\u6301 inline tag \u6570\u91CF\u4E0E\u987A\u5E8F\u548C\u6E90\u6587\u4E00\u81F4"
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 } : {} }),
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"
1550
2100
  });
1551
2101
  }
1552
2102
  var URL_PATTERNS = [
@@ -1637,9 +2187,10 @@ function validMonthDay(month, day) {
1637
2187
  var DATE_PATTERNS = [
1638
2188
  /\b(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})\b/gu,
1639
2189
  /\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b/gu,
1640
- /(\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
1641
2191
  ];
1642
2192
  function extractDates(value) {
2193
+ value = normalizeNumericText(value);
1643
2194
  const tokens = [];
1644
2195
  const push = (key, text, start) => {
1645
2196
  tokens.push({ key, text, span: { start, end: start + text.length } });
@@ -1737,7 +2288,7 @@ function extractDates(value) {
1737
2288
  }
1738
2289
  function maskSpans(value, spans) {
1739
2290
  if (spans.length === 0) return value;
1740
- const chars = Array.from(value);
2291
+ const chars = value.split("");
1741
2292
  for (const span of spans) {
1742
2293
  for (let index = span.start; index < span.end && index < chars.length; index += 1) chars[index] = " ";
1743
2294
  }
@@ -1757,10 +2308,14 @@ function normalizeNumber(raw) {
1757
2308
  if (/^\d+\.0+$/u.test(value)) value = value.replace(/\.0+$/u, "");
1758
2309
  return `${sign}${value}`;
1759
2310
  }
2311
+ function normalizeNumericText(value) {
2312
+ return value.replace(/[0-9.,+-]/gu, (character) => character.normalize("NFKC"));
2313
+ }
1760
2314
  function extractNumbers(value) {
2315
+ const normalized = normalizeNumericText(value);
1761
2316
  const masked = maskSpans(
1762
- value,
1763
- extractDates(value).map((token) => token.span)
2317
+ normalized,
2318
+ [...extractDates(normalized).map((token) => token.span), ...collectPlaceholders(normalized)]
1764
2319
  );
1765
2320
  const results = [];
1766
2321
  for (const match of masked.matchAll(NUMBER_RE)) results.push(normalizeNumber(match[0]));
@@ -1799,6 +2354,47 @@ function checkDates(context) {
1799
2354
  ...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
1800
2355
  });
1801
2356
  }
2357
+ function localeDateOrder(locale) {
2358
+ const normalized = locale.trim().toLowerCase().replace(/_/gu, "-");
2359
+ if (/^(?:zh|ja|ko)(?:-|$)/u.test(normalized)) return "ymd";
2360
+ if (/^en-us(?:-|$)/u.test(normalized)) return "mdy";
2361
+ if (/^(?:en-gb|en-au|en-nz|en-ie|de|fr|es|it|pt|nl|ru|pl|tr|sv|da|fi|nb|cs|hu|ro|bg|hr|sk|sl|uk|el)(?:-|$)/u.test(
2362
+ normalized
2363
+ )) {
2364
+ return "dmy";
2365
+ }
2366
+ return void 0;
2367
+ }
2368
+ function numericDateOrder(text) {
2369
+ if (/^(?:\d{4}|[〇零一二三四五六七八九]{4})年/u.test(text)) return "ymd";
2370
+ if (/^\d{4}[./-]\d{1,2}[./-]\d{1,2}$/u.test(text)) return "ymd";
2371
+ const match = /^(\d{1,2})[./-](\d{1,2})[./-]\d{2,4}$/u.exec(text);
2372
+ if (!match) return void 0;
2373
+ const first = Number(match[1]);
2374
+ const second = Number(match[2]);
2375
+ if (first > 12 && second <= 12) return "dmy";
2376
+ if (second > 12 && first <= 12) return "mdy";
2377
+ return void 0;
2378
+ }
2379
+ function checkDateFormat(context) {
2380
+ const targetLocale = context.locale?.targetLocale;
2381
+ if (targetLocale === void 0) return;
2382
+ const expected = localeDateOrder(targetLocale);
2383
+ if (expected === void 0) return;
2384
+ const dates = extractDates(context.target);
2385
+ if (dates.length === 0) return;
2386
+ for (const token of dates) {
2387
+ const actual = numericDateOrder(token.text);
2388
+ if (actual === void 0 || actual === expected) continue;
2389
+ draft(context, {
2390
+ ruleId: "DATE_FORMAT",
2391
+ message: `\u65E5\u671F\u683C\u5F0F\u4E0E\u76EE\u6807\u8BED\u8A00\u4E60\u60EF\u4E0D\u4E00\u81F4: '${token.text}' \u4E3A ${actual.toUpperCase()}\uFF0C${targetLocale} \u671F\u671B ${expected.toUpperCase()}`,
2392
+ evidence: `target date: "${token.text}"`,
2393
+ suggestion: expected === "ymd" ? "\u6539\u4E3A \u5E74-\u6708-\u65E5 \u987A\u5E8F" : expected === "dmy" ? "\u6539\u4E3A \u65E5/\u6708/\u5E74 \u987A\u5E8F" : "\u6539\u4E3A \u6708/\u65E5/\u5E74 \u987A\u5E8F",
2394
+ targetRange: { start: token.span.start, end: token.span.end }
2395
+ });
2396
+ }
2397
+ }
1802
2398
  var BRACKET_PAIRS = /* @__PURE__ */ new Map([
1803
2399
  ["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
1804
2400
  ["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
@@ -2284,6 +2880,91 @@ function checkTermRules(context) {
2284
2880
  });
2285
2881
  }
2286
2882
  }
2883
+ function sourceKey(value) {
2884
+ return value.normalize("NFKC").replace(/\s+/gu, " ").trim();
2885
+ }
2886
+ function sourceTargetInconsistencies(contexts) {
2887
+ const groups = /* @__PURE__ */ new Map();
2888
+ for (const context of contexts) {
2889
+ if (context.source.trim().length === 0 || context.target.trim().length === 0) continue;
2890
+ const key = sourceKey(context.source);
2891
+ const group = groups.get(key);
2892
+ if (group === void 0) groups.set(key, [context]);
2893
+ else group.push(context);
2894
+ }
2895
+ const drafts = [];
2896
+ let checkedSegments = 0;
2897
+ let emitted = 0;
2898
+ for (const group of groups.values()) {
2899
+ if (group.length < 2) continue;
2900
+ checkedSegments += group.length;
2901
+ const targets = group.map((context) => normalizeForCompare(context.target));
2902
+ const variants = Array.from(new Set(targets));
2903
+ if (variants.length < 2) continue;
2904
+ const reference = variants[0];
2905
+ for (let index = 1; index < group.length && emitted < 50; index += 1) {
2906
+ if (targets[index] === reference) continue;
2907
+ const context = group[index];
2908
+ const segment = context?.segment;
2909
+ if (context === void 0 || segment === void 0) continue;
2910
+ drafts.push({
2911
+ segment,
2912
+ draft: {
2913
+ ruleId: "SOURCE_TARGET_INCONSISTENT",
2914
+ message: `\u540C\u4E00\u6E90\u6587\u5B58\u5728\u4E0D\u540C\u8BD1\u6CD5: '${truncate(context.source)}'`,
2915
+ evidence: `variants: [${variants.slice(0, 3).map((value) => truncate(value, 40)).join(" | ")}]`,
2916
+ suggestion: "\u7EDF\u4E00\u8BE5\u53E5\u6BB5\u7684\u8BD1\u6CD5"
2917
+ }
2918
+ });
2919
+ emitted += 1;
2920
+ }
2921
+ }
2922
+ return { drafts, checkedSegments };
2923
+ }
2924
+ function terminologyInconsistencies(contexts) {
2925
+ const perTerm = /* @__PURE__ */ new Map();
2926
+ let checkedSegments = 0;
2927
+ for (const context of contexts) {
2928
+ if (context.termHits === void 0 || context.target.trim().length === 0) continue;
2929
+ const sourceHits = longestHits(context.termHits.source);
2930
+ if (sourceHits.length === 0) continue;
2931
+ checkedSegments += 1;
2932
+ for (const hit of sourceHits) {
2933
+ const entry = hit.entry;
2934
+ if (entry.forbidden === true || !isApproved(entry) || (entry.target ?? "").trim().length === 0) continue;
2935
+ const bucket = perTerm.get(entry.id) ?? { entry, items: [] };
2936
+ if (!bucket.items.some((item) => item.context === context)) {
2937
+ bucket.items.push({
2938
+ context,
2939
+ mentioned: targetMentionsTerm(entry, context.target, context.termHits.target)
2940
+ });
2941
+ }
2942
+ perTerm.set(entry.id, bucket);
2943
+ }
2944
+ }
2945
+ const drafts = [];
2946
+ let emitted = 0;
2947
+ for (const { entry, items } of perTerm.values()) {
2948
+ const mentioned = items.filter((item) => item.mentioned);
2949
+ const missing = items.filter((item) => !item.mentioned);
2950
+ if (mentioned.length === 0 || missing.length === 0) continue;
2951
+ for (const item of missing) {
2952
+ if (emitted >= 25) break;
2953
+ drafts.push({
2954
+ segment: item.context.segment,
2955
+ draft: {
2956
+ ruleId: "TERMINOLOGY_INCONSISTENT",
2957
+ message: `\u672F\u8BED '${entry.source}' \u2192 '${entry.target}' \u5728\u90E8\u5206\u53E5\u6BB5\u5DF2\u4F7F\u7528\u3001\u90E8\u5206\u672A\u4F7F\u7528\uFF08\u4E0D\u4E00\u81F4\uFF09`,
2958
+ evidence: `used in ${mentioned.length} segment(s), missing in ${missing.length}`,
2959
+ suggestion: entry.target,
2960
+ termId: entry.id
2961
+ }
2962
+ });
2963
+ emitted += 1;
2964
+ }
2965
+ }
2966
+ return { drafts, checkedSegments };
2967
+ }
2287
2968
  var CHECK_GROUPS = [
2288
2969
  { ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
2289
2970
  { ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
@@ -2294,6 +2975,7 @@ var CHECK_GROUPS = [
2294
2975
  { ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
2295
2976
  { ids: ["NUMBER_MISMATCH"], run: checkNumbers },
2296
2977
  { ids: ["DATE_MISMATCH"], run: checkDates },
2978
+ { ids: ["DATE_FORMAT"], run: checkDateFormat },
2297
2979
  { ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
2298
2980
  { ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
2299
2981
  { ids: ["PUNCT_DUP"], run: checkPunctDup },
@@ -2308,10 +2990,13 @@ function checkEmptyTargetGroup(context) {
2308
2990
  if (source.trim().length === 0) return;
2309
2991
  const targetMissing = context.segment.targetRange === null && context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2310
2992
  const empty = context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2311
- 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;
2312
2997
  draft(context, {
2313
2998
  ruleId: "EMPTY_TARGET",
2314
- 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",
2315
3000
  evidence: `source: ${truncate(source)}`
2316
3001
  });
2317
3002
  }
@@ -2322,6 +3007,7 @@ function checkUntranslatedCopyGroup(context) {
2322
3007
  if (normalizedSource !== normalizeForCompare(target)) return;
2323
3008
  const policy = context.untranslatedCopy;
2324
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;
2325
3011
  const tokens = wordTokens(source);
2326
3012
  const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
2327
3013
  const maxShort = policy?.shortSegmentMaxLength ?? 4;
@@ -2356,6 +3042,7 @@ function isApplicable(ruleId, segment) {
2356
3042
  case "UNPAIRED_BRACKET":
2357
3043
  case "NUMBER_MISMATCH":
2358
3044
  case "DATE_MISMATCH":
3045
+ case "DATE_FORMAT":
2359
3046
  case "DUPLICATE_WORD":
2360
3047
  case "TERM_MISSING":
2361
3048
  case "SOURCE_LEFTOVER":
@@ -2382,6 +3069,7 @@ function resolveEnabledRules(options) {
2382
3069
  return enabled2;
2383
3070
  }
2384
3071
  const enabled = new Set(DEFAULT_ENABLED);
3072
+ if (options.locale?.targetLocale === void 0) for (const ruleId of LOCALE_RULE_IDS) enabled.delete(ruleId);
2385
3073
  if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
2386
3074
  return enabled;
2387
3075
  }
@@ -2443,6 +3131,14 @@ function runDeterministicChecks(segments, options = {}) {
2443
3131
  const state = /* @__PURE__ */ new Map();
2444
3132
  for (const rule of RULES) {
2445
3133
  if (!enabled.has(rule.id)) continue;
3134
+ if (rule.requiresLocale === true && options.locale?.targetLocale === void 0) {
3135
+ state.set(rule.id, {
3136
+ status: "skipped",
3137
+ reason: "no locale.targetLocale provided; locale rule was not executed",
3138
+ checkedSegments: 0
3139
+ });
3140
+ continue;
3141
+ }
2446
3142
  if (rule.requiresTerms === true && !termAvailable) {
2447
3143
  state.set(rule.id, {
2448
3144
  status: "skipped",
@@ -2459,6 +3155,7 @@ function runDeterministicChecks(segments, options = {}) {
2459
3155
  const applicable = /* @__PURE__ */ new Set();
2460
3156
  for (const ruleId of enabled) {
2461
3157
  if (state.get(ruleId)?.status !== "executed") continue;
3158
+ if (DOCUMENT_RULE_IDS.has(ruleId)) continue;
2462
3159
  if (isApplicable(ruleId, segment)) applicable.add(ruleId);
2463
3160
  }
2464
3161
  if (applicable.size === 0) continue;
@@ -2497,6 +3194,18 @@ function runDeterministicChecks(segments, options = {}) {
2497
3194
  issues.push(toIssue(draft2, segment, draft2.ruleId));
2498
3195
  }
2499
3196
  }
3197
+ const documentRules = RULES.filter(
3198
+ (rule) => rule.scope === "document" && state.get(rule.id)?.status === "executed"
3199
+ );
3200
+ if (documentRules.length > 0) {
3201
+ const contexts = segments.map((segment) => buildContext(segment, options, glossary));
3202
+ for (const rule of documentRules) {
3203
+ const result = rule.id === "TERMINOLOGY_INCONSISTENT" ? terminologyInconsistencies(contexts) : sourceTargetInconsistencies(contexts);
3204
+ for (const item of result.drafts) issues.push(toIssue(item.draft, item.segment, rule.id));
3205
+ const execution = state.get(rule.id);
3206
+ if (execution !== void 0) execution.checkedSegments = result.checkedSegments;
3207
+ }
3208
+ }
2500
3209
  const rules = [];
2501
3210
  for (const rule of RULES) {
2502
3211
  const execution = state.get(rule.id);
@@ -2524,20 +3233,26 @@ function runDeterministicChecks(segments, options = {}) {
2524
3233
  };
2525
3234
  }
2526
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
+
2527
3242
  // packages/l10n-core/src/workflows/check.ts
2528
- function termMatchOptions(document, segment) {
2529
- const file = document.files[segment.location.fileIndex];
3243
+ function termMatchOptions(document2, segment) {
3244
+ const file = document2.files[segment.location.fileIndex];
2530
3245
  const options = {};
2531
3246
  if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
2532
3247
  if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
2533
3248
  return options;
2534
3249
  }
2535
- function checkDocument(document, options = {}) {
3250
+ function checkDocument(document2, options = {}) {
2536
3251
  const glossary = options.glossary ?? [];
2537
3252
  const segmentTermHits = /* @__PURE__ */ new Map();
2538
3253
  if (glossary.length > 0) {
2539
- for (const segment of document.segments) {
2540
- const termOptions = termMatchOptions(document, segment);
3254
+ for (const segment of document2.segments) {
3255
+ const termOptions = termMatchOptions(document2, segment);
2541
3256
  const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
2542
3257
  const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
2543
3258
  segmentTermHits.set(segment.id, {
@@ -2553,7 +3268,7 @@ function checkDocument(document, options = {}) {
2553
3268
  ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
2554
3269
  ...options.locale === void 0 ? {} : { locale: options.locale }
2555
3270
  };
2556
- const qa = runDeterministicChecks(document.segments, qaOptions);
3271
+ const qa = runDeterministicChecks(document2.segments, qaOptions);
2557
3272
  return { qa, segmentTermHits };
2558
3273
  }
2559
3274
 
@@ -2567,13 +3282,18 @@ function bestMatch(matches) {
2567
3282
  }
2568
3283
  return best;
2569
3284
  }
2570
- function translateDocument(document, tmEntries, options = {}) {
3285
+ function translateDocument(document2, tmEntries, options = {}) {
2571
3286
  const threshold = options.threshold ?? DEFAULT_THRESHOLD2;
2572
3287
  const warnings = [];
2573
3288
  const values = /* @__PURE__ */ new Map();
2574
3289
  let skipped = 0;
2575
- for (const segment of document.segments) {
3290
+ for (const segment of document2.segments) {
2576
3291
  if (segment.targetText.trim().length > 0) continue;
3292
+ if (segment.readOnlyReason) {
3293
+ warnings.push(`${segment.id}: read-only: ${segment.readOnlyReason}; skipped`);
3294
+ skipped += 1;
3295
+ continue;
3296
+ }
2577
3297
  if (segment.targetRange === null) {
2578
3298
  warnings.push(`${segment.id}: target missing; cannot fill`);
2579
3299
  skipped += 1;
@@ -2595,7 +3315,7 @@ function translateDocument(document, tmEntries, options = {}) {
2595
3315
  skipped += 1;
2596
3316
  continue;
2597
3317
  }
2598
- const file = document.files[segment.location.fileIndex];
3318
+ const file = document2.files[segment.location.fileIndex];
2599
3319
  const matchOptions = {
2600
3320
  threshold,
2601
3321
  maxMatches: 1,
@@ -2620,30 +3340,30 @@ function translateDocument(document, tmEntries, options = {}) {
2620
3340
  values.set(segment.id, target);
2621
3341
  }
2622
3342
  if (values.size === 0) {
2623
- return { xml: document.xml, applied: 0, skipped, warnings };
3343
+ return { xml: document2.xml, applied: 0, skipped, warnings };
2624
3344
  }
2625
- const patch = patchDocument(document, textEdits(document, values));
3345
+ const patch = patchDocument(document2, textEdits(document2, values));
2626
3346
  if (patch.errors.length > 0) {
2627
3347
  const details = patch.errors.map((error) => `${error.segmentId}: ${error.message}`);
2628
3348
  warnings.push(...details.map((detail) => `patch failed: ${detail}`));
2629
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3349
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
2630
3350
  }
2631
3351
  let originalHash;
2632
3352
  let patchedHash;
2633
3353
  try {
2634
- originalHash = structureSummary(document.xml).hash;
3354
+ originalHash = structureSummary(document2.xml).hash;
2635
3355
  patchedHash = structureSummary(patch.xml).hash;
2636
3356
  } catch (caught) {
2637
3357
  const message = caught instanceof Error ? caught.message : String(caught);
2638
3358
  warnings.push(`structure check failed: ${message}`);
2639
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3359
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
2640
3360
  }
2641
3361
  if (originalHash !== patchedHash) {
2642
3362
  warnings.push("structure summary changed after fill; refusing to write output");
2643
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3363
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
2644
3364
  }
2645
3365
  let xml = patch.xml;
2646
- if (options.setSdlState === true && document.format === "sdlxliff") {
3366
+ if (options.setSdlState === true && document2.format === "sdlxliff") {
2647
3367
  const conf = options.sdlConf ?? "Translated";
2648
3368
  try {
2649
3369
  const patchedDocument = parseXliff(xml);
@@ -2653,13 +3373,13 @@ function translateDocument(document, tmEntries, options = {}) {
2653
3373
  const state = patchSdlStates(patchedDocument, updates);
2654
3374
  if (state.errors.length > 0) {
2655
3375
  warnings.push(...state.errors.map((error) => `state patch failed: ${error.segmentId}: ${error.message}`));
2656
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3376
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
2657
3377
  }
2658
3378
  xml = state.xml;
2659
3379
  } catch (caught) {
2660
3380
  const message = caught instanceof Error ? caught.message : String(caught);
2661
3381
  warnings.push(`state patch failed: ${message}`);
2662
- return { xml: document.xml, applied: 0, skipped: skipped + values.size, warnings };
3382
+ return { xml: document2.xml, applied: 0, skipped: skipped + values.size, warnings };
2663
3383
  }
2664
3384
  }
2665
3385
  return { xml, applied: patch.applied, skipped, warnings };
@@ -2922,11 +3642,11 @@ function parseSemanticResponse(raw, segments, options = {}) {
2922
3642
  }
2923
3643
  return { issues, rejected };
2924
3644
  }
2925
- async function runSemanticCheck(document, provider, options = {}) {
3645
+ async function runSemanticCheck(document2, provider, options = {}) {
2926
3646
  const batchSize = Math.max(1, options.batchSize ?? 20);
2927
3647
  const batches = [];
2928
- for (let index = 0; index < document.segments.length; index += batchSize) {
2929
- 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));
2930
3650
  }
2931
3651
  const issues = [];
2932
3652
  const failures = [];
@@ -3266,9 +3986,9 @@ function backfillOoxml(buffer, map, translations) {
3266
3986
  }
3267
3987
 
3268
3988
  // packages/l10n-core/src/workflows/polish.ts
3269
- function polishDocument(document, edits) {
3989
+ function polishDocument(document2, edits) {
3270
3990
  const normalized = /* @__PURE__ */ new Map();
3271
- const byId = new Map(document.segments.map((segment) => [segment.id, segment]));
3991
+ const byId = new Map(document2.segments.map((segment) => [segment.id, segment]));
3272
3992
  const warnings = [];
3273
3993
  for (const [segmentId, value] of edits) {
3274
3994
  const segment = byId.get(segmentId);
@@ -3279,18 +3999,18 @@ function polishDocument(document, edits) {
3279
3999
  normalized.set(segmentId, typeof value === "string" ? { content: value } : value);
3280
4000
  }
3281
4001
  if (warnings.length > 0) {
3282
- return { xml: document.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
4002
+ return { xml: document2.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
3283
4003
  }
3284
- const before = structureSummary(document.xml).hash;
3285
- const patch = patchDocument(document, normalized);
4004
+ const before = structureSummary(document2.xml).hash;
4005
+ const patch = patchDocument(document2, normalized);
3286
4006
  if (patch.errors.length > 0) {
3287
4007
  warnings.push(...patch.errors.map((error) => `${error.segmentId}: ${error.message}`));
3288
- return { xml: document.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
4008
+ return { xml: document2.xml, applied: 0, skipped: normalized.size, warnings, changes: [] };
3289
4009
  }
3290
4010
  const after = structureSummary(patch.xml).hash;
3291
4011
  if (before !== after) {
3292
4012
  return {
3293
- xml: document.xml,
4013
+ xml: document2.xml,
3294
4014
  applied: 0,
3295
4015
  skipped: normalized.size,
3296
4016
  warnings: ["structure summary changed after polish; refusing to write"],
@@ -3318,81 +4038,1606 @@ function polishDocument(document, edits) {
3318
4038
  };
3319
4039
  }
3320
4040
 
3321
- // packages/dsh-l10n/src/tools-phase1.ts
3322
- var MAX_BYTES = 200 * 1024 * 1024;
3323
- async function readBytes(path) {
3324
- const absolute = resolve(path);
3325
- const info = await stat(absolute);
3326
- if (!info.isFile()) throw new Error("path is not a regular file");
3327
- if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
3328
- return readFile(absolute);
4041
+ // packages/dsh-l10n/src/project.ts
4042
+ var MANIFEST_DIRECTORY = ".langlink";
4043
+ var MANIFEST_FILENAME = "manifest.json";
4044
+ var MANIFEST_VERSION = 1;
4045
+ var MAX_ASSET_BYTES = 200 * 1024 * 1024;
4046
+ var MAX_ASSETS = 64;
4047
+ function manifestPathFor(root) {
4048
+ return join(root, MANIFEST_DIRECTORY, MANIFEST_FILENAME);
4049
+ }
4050
+ function isRecord(value) {
4051
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4052
+ }
4053
+ function requireString(value, field) {
4054
+ if (typeof value !== "string" || value.trim() === "") throw new Error(`project manifest: ${field} must be a non-empty string`);
4055
+ return value;
3329
4056
  }
3330
- async function readUtf8(path) {
3331
- const absolute = resolve(path);
3332
- const info = await stat(absolute);
3333
- if (!info.isFile()) throw new Error("path is not a regular file");
3334
- if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
3335
- const value = await readFile(absolute, { encoding: "utf8" });
3336
- if (value.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
4057
+ function optionalString(value, field) {
4058
+ if (value === void 0 || value === null) return void 0;
4059
+ if (typeof value !== "string" || value.trim() === "") throw new Error(`project manifest: ${field} must be a non-empty string`);
3337
4060
  return value;
3338
4061
  }
3339
- function documentFromXml(xml, path) {
3340
- return parseXliff(xml, { filename: basename(path) });
4062
+ function requireArray(value, field) {
4063
+ if (!Array.isArray(value)) throw new Error(`project manifest: ${field} must be an array`);
4064
+ return value;
3341
4065
  }
3342
- function createMatchTmTool() {
3343
- return defineTool({
3344
- name: "l10n_match_tm",
3345
- description: "Match a local XLIFF/SDLXLIFF file against a TMX 1.4b translation memory. Returns match-rate metadata per segment (no file modification).",
3346
- parameters: {
3347
- path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3348
- tm: { type: "string", required: true, description: "TMX 1.4b file path." },
3349
- threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
3350
- maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
3351
- },
3352
- output: {
3353
- schema: { type: "string" },
3354
- render: (_args, value) => [{ type: "text", text: value }]
3355
- },
3356
- async execute(args) {
3357
- const xml = await readUtf8(args.path);
3358
- const document = documentFromXml(xml, args.path);
3359
- const tm = parseTmx(await readUtf8(args.tm));
3360
- if (tm.entries.length === 0) throw new Error("TMX contains no usable entries");
3361
- const threshold = args.threshold ?? 75;
3362
- const limit = Math.max(1, args.maxSegments ?? 50);
3363
- const matches = document.segments.slice(0, limit).map((segment) => {
3364
- const file = document.files[segment.location.fileIndex];
3365
- const candidates = matchTm(tm.entries, segment.sourceText, {
3366
- threshold,
3367
- maxMatches: 1,
3368
- ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
3369
- ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
3370
- });
3371
- const best = candidates[0];
3372
- return {
3373
- segmentId: segment.id,
3374
- matchRate: best?.matchRate ?? 0,
3375
- similarity: best?.similarity ?? 0,
3376
- matchType: best?.matchType ?? "no_match"
3377
- };
3378
- });
3379
- return JSON.stringify({
3380
- format: document.format,
3381
- segments: document.segments.length,
3382
- checked: matches.length,
3383
- threshold,
3384
- matches
3385
- });
4066
+ function parseMapping(value, field) {
4067
+ if (value === void 0) return void 0;
4068
+ if (!isRecord(value)) throw new Error(`project manifest: ${field} must be an object`);
4069
+ const mapping2 = {};
4070
+ if (value.sheet !== void 0) mapping2.sheet = requireString(value.sheet, `${field}.sheet`);
4071
+ if (value.headerRow !== void 0) {
4072
+ if (typeof value.headerRow !== "number" || !Number.isInteger(value.headerRow) || value.headerRow < 0) {
4073
+ throw new Error(`project manifest: ${field}.headerRow must be a non-negative integer`);
3386
4074
  }
3387
- });
4075
+ mapping2.headerRow = value.headerRow;
4076
+ }
4077
+ for (const key of ["sourceColumn", "targetColumn", "forbiddenColumn"]) {
4078
+ if (value[key] !== void 0) mapping2[key] = requireString(value[key], `${field}.${key}`);
4079
+ }
4080
+ return Object.keys(mapping2).length === 0 ? void 0 : mapping2;
4081
+ }
4082
+ function parseAsset(value, index) {
4083
+ if (!isRecord(value)) throw new Error(`project manifest: assets[${index}] must be an object`);
4084
+ const id = requireString(value.id, `assets[${index}].id`);
4085
+ if (!/^[A-Za-z0-9._-]{1,64}$/u.test(id)) throw new Error(`project manifest: assets[${index}].id "${id}" is not a safe identifier`);
4086
+ const kind = requireString(value.kind, `assets[${index}].kind`);
4087
+ if (kind !== "tm" && kind !== "tb" && kind !== "bilingual") {
4088
+ throw new Error(`project manifest: assets[${index}].kind must be tm, tb or bilingual`);
4089
+ }
4090
+ const format = requireString(value.format, `assets[${index}].format`);
4091
+ if (!["tmx", "tbx", "csv", "tsv", "txt", "xlsx"].includes(format)) {
4092
+ throw new Error(`project manifest: assets[${index}].format "${format}" is not supported`);
4093
+ }
4094
+ const entries = isRecord(value.counts) ? value.counts.entries : 0;
4095
+ if (typeof entries !== "number" || !Number.isInteger(entries) || entries < 0) {
4096
+ throw new Error(`project manifest: assets[${index}].counts.entries must be a non-negative integer`);
4097
+ }
4098
+ const addedAt = requireString(value.addedAt, `assets[${index}].addedAt`);
4099
+ const updatedAt = requireString(value.updatedAt, `assets[${index}].updatedAt`);
4100
+ const sourceLang = optionalString(value.sourceLang, `assets[${index}].sourceLang`);
4101
+ const targetLang = optionalString(value.targetLang, `assets[${index}].targetLang`);
4102
+ const mapping2 = parseMapping(value.mapping, `assets[${index}].mapping`);
4103
+ return {
4104
+ id,
4105
+ kind,
4106
+ format,
4107
+ path: requireString(value.path, `assets[${index}].path`),
4108
+ enabled: value.enabled === void 0 ? true : value.enabled === true,
4109
+ priority: typeof value.priority === "number" && Number.isFinite(value.priority) ? value.priority : 100,
4110
+ ...sourceLang === void 0 ? {} : { sourceLang },
4111
+ ...targetLang === void 0 ? {} : { targetLang },
4112
+ sha256: requireString(value.sha256, `assets[${index}].sha256`),
4113
+ size: typeof value.size === "number" && Number.isFinite(value.size) ? value.size : 0,
4114
+ counts: { entries },
4115
+ ...mapping2 === void 0 ? {} : { mapping: mapping2 },
4116
+ addedAt,
4117
+ updatedAt
4118
+ };
3388
4119
  }
3389
- function createMatchTermsTool() {
3390
- return defineTool({
4120
+ function parseManifest(value, manifestPath) {
4121
+ if (!isRecord(value)) throw new Error(`project manifest is not a JSON object: ${manifestPath}`);
4122
+ if (value.version !== MANIFEST_VERSION) {
4123
+ throw new Error(`project manifest version ${String(value.version)} is not supported (expected ${MANIFEST_VERSION})`);
4124
+ }
4125
+ const qaInput = isRecord(value.qa) ? value.qa : void 0;
4126
+ const outputsInput = isRecord(value.outputs) ? value.outputs : void 0;
4127
+ let qa;
4128
+ if (qaInput !== void 0) {
4129
+ const parsed = {};
4130
+ const locale = optionalString(qaInput.locale, "qa.locale");
4131
+ if (locale !== void 0) parsed.locale = locale;
4132
+ if (typeof qaInput.threshold === "number") parsed.threshold = qaInput.threshold;
4133
+ if (typeof qaInput.maxMatches === "number") parsed.maxMatches = qaInput.maxMatches;
4134
+ if (Array.isArray(qaInput.untranslatedCopyWhitelist)) {
4135
+ parsed.untranslatedCopyWhitelist = qaInput.untranslatedCopyWhitelist.filter(
4136
+ (item) => typeof item === "string"
4137
+ );
4138
+ }
4139
+ qa = parsed;
4140
+ }
4141
+ let outputs;
4142
+ if (outputsInput !== void 0) {
4143
+ const dir = optionalString(outputsInput.dir, "outputs.dir");
4144
+ outputs = dir === void 0 ? {} : { dir };
4145
+ }
4146
+ return {
4147
+ version: MANIFEST_VERSION,
4148
+ name: requireString(value.name, "name"),
4149
+ sourceLang: requireString(value.sourceLang, "sourceLang"),
4150
+ targetLang: requireString(value.targetLang, "targetLang"),
4151
+ createdAt: requireString(value.createdAt, "createdAt"),
4152
+ updatedAt: requireString(value.updatedAt, "updatedAt"),
4153
+ files: requireArray(value.files ?? [], "files").map((entry, index) => {
4154
+ if (!isRecord(entry)) throw new Error(`project manifest: files[${index}] must be an object`);
4155
+ return { path: requireString(entry.path, `files[${index}].path`), sha256: requireString(entry.sha256, `files[${index}].sha256`) };
4156
+ }),
4157
+ assets: requireArray(value.assets ?? [], "assets").map(parseAsset),
4158
+ ...qa === void 0 ? {} : { qa },
4159
+ ...outputs === void 0 ? {} : { outputs }
4160
+ };
4161
+ }
4162
+ async function readManifest(root) {
4163
+ const manifestPath = manifestPathFor(root);
4164
+ let text;
4165
+ try {
4166
+ text = await readFile(manifestPath, "utf8");
4167
+ } catch {
4168
+ throw new Error(`no project manifest at ${manifestPath}; run l10n_project_init first`);
4169
+ }
4170
+ let parsed;
4171
+ try {
4172
+ parsed = JSON.parse(text);
4173
+ } catch (caught) {
4174
+ throw new Error(`project manifest is not valid JSON: ${caught instanceof Error ? caught.message : String(caught)}`);
4175
+ }
4176
+ return { root, manifestPath, manifest: parseManifest(parsed, manifestPath) };
4177
+ }
4178
+ async function writeManifest(handle) {
4179
+ const { manifest, manifestPath } = handle;
4180
+ manifest.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
4181
+ await mkdir(dirname(manifestPath), { recursive: true });
4182
+ const temp = `${manifestPath}.tmp-${process.pid}-${Date.now()}`;
4183
+ await writeFile(temp, `${JSON.stringify(manifest, null, 2)}
4184
+ `, "utf8");
4185
+ await rename(temp, manifestPath);
4186
+ }
4187
+ async function isDirectory(path) {
4188
+ try {
4189
+ return (await stat(path)).isDirectory();
4190
+ } catch {
4191
+ return false;
4192
+ }
4193
+ }
4194
+ async function hasManifest(root) {
4195
+ try {
4196
+ return (await stat(manifestPathFor(root))).isFile();
4197
+ } catch {
4198
+ return false;
4199
+ }
4200
+ }
4201
+ async function findProjectRootFrom(start) {
4202
+ let current = resolve(start);
4203
+ try {
4204
+ if (!await isDirectory(current)) current = dirname(current);
4205
+ } catch {
4206
+ current = dirname(current);
4207
+ }
4208
+ for (; ; ) {
4209
+ if (await hasManifest(current)) return current;
4210
+ const parent = dirname(current);
4211
+ if (parent === current) return void 0;
4212
+ current = parent;
4213
+ }
4214
+ }
4215
+ async function findProject(project, anchor) {
4216
+ if (project !== void 0 && project.trim() !== "") {
4217
+ const candidate = resolve(project);
4218
+ if (candidate.endsWith(MANIFEST_FILENAME)) {
4219
+ const root2 = dirname(dirname(candidate));
4220
+ return readManifest(root2);
4221
+ }
4222
+ if (!await isDirectory(candidate)) throw new Error(`project root is not a directory: ${candidate}`);
4223
+ return await hasManifest(candidate) ? readManifest(candidate) : void 0;
4224
+ }
4225
+ const start = anchor ?? process.cwd();
4226
+ const root = await findProjectRootFrom(start);
4227
+ return root === void 0 ? void 0 : readManifest(root);
4228
+ }
4229
+ async function requireProject(project, anchor) {
4230
+ const handle = await findProject(project, anchor);
4231
+ if (handle !== void 0) return handle;
4232
+ if (project === void 0) {
4233
+ const fromCwd = await findProject(void 0, process.cwd());
4234
+ if (fromCwd !== void 0) return fromCwd;
4235
+ }
4236
+ throw new Error("no project manifest found; run l10n_project_init or pass project (root or .langlink/manifest.json)");
4237
+ }
4238
+ async function initProject(options) {
4239
+ const root = resolve(options.root);
4240
+ if (await isDirectory(root)) {
4241
+ const manifest = await hasManifest(root);
4242
+ if (manifest && options.overwrite !== true) {
4243
+ throw new Error(`project manifest already exists at ${manifestPathFor(root)}; pass overwrite=true to update languages/QA settings`);
4244
+ }
4245
+ }
4246
+ await mkdir(root, { recursive: true });
4247
+ await mkdir(join(root, "in"), { recursive: true });
4248
+ await mkdir(join(root, "assets"), { recursive: true });
4249
+ await mkdir(join(root, "out"), { recursive: true });
4250
+ const now = (/* @__PURE__ */ new Date()).toISOString();
4251
+ let handle;
4252
+ if (await hasManifest(root)) {
4253
+ handle = await readManifest(root);
4254
+ handle.manifest.name = options.name ?? handle.manifest.name;
4255
+ handle.manifest.sourceLang = options.sourceLang;
4256
+ handle.manifest.targetLang = options.targetLang;
4257
+ } else {
4258
+ handle = {
4259
+ root,
4260
+ manifestPath: manifestPathFor(root),
4261
+ manifest: {
4262
+ version: MANIFEST_VERSION,
4263
+ name: options.name ?? "localization",
4264
+ sourceLang: options.sourceLang,
4265
+ targetLang: options.targetLang,
4266
+ createdAt: now,
4267
+ updatedAt: now,
4268
+ files: [],
4269
+ assets: [],
4270
+ outputs: { dir: "out" }
4271
+ }
4272
+ };
4273
+ }
4274
+ const qa = handle.manifest.qa ?? {};
4275
+ if (options.locale !== void 0) qa.locale = options.locale;
4276
+ if (options.threshold !== void 0) qa.threshold = options.threshold;
4277
+ if (options.maxMatches !== void 0) qa.maxMatches = options.maxMatches;
4278
+ if (options.untranslatedCopyWhitelist !== void 0) qa.untranslatedCopyWhitelist = options.untranslatedCopyWhitelist;
4279
+ handle.manifest.qa = qa;
4280
+ await writeManifest(handle);
4281
+ return handle;
4282
+ }
4283
+ function formatOf(path) {
4284
+ const lower = path.toLowerCase();
4285
+ if (lower.endsWith(".tmx")) return "tmx";
4286
+ if (lower.endsWith(".tbx")) return "tbx";
4287
+ if (lower.endsWith(".csv")) return "csv";
4288
+ if (lower.endsWith(".tsv")) return "tsv";
4289
+ if (lower.endsWith(".txt")) return "txt";
4290
+ if (lower.endsWith(".xlsx")) return "xlsx";
4291
+ throw new Error(`unsupported asset format: ${path} (expected .tmx, .tbx, .csv, .tsv, .txt or .xlsx)`);
4292
+ }
4293
+ function normalizeHeader2(value) {
4294
+ return value.trim().toLowerCase().replace(/[\s_-]+/gu, "");
4295
+ }
4296
+ var SOURCE_HEADERS3 = /* @__PURE__ */ new Set(["source", "src", "sourcetext", "\u6E90\u6587", "\u539F\u6587", "\u6E90\u8BED\u8A00"]);
4297
+ var TARGET_HEADERS3 = /* @__PURE__ */ new Set(["target", "tgt", "targettext", "\u8BD1\u6587", "\u76EE\u6A19", "\u76EE\u6807\u8BED\u8A00"]);
4298
+ var ID_HEADERS2 = /* @__PURE__ */ new Set(["id", "segmentid", "\u7F16\u53F7"]);
4299
+ var LOCATION_HEADERS2 = /* @__PURE__ */ new Set(["location", "\u4F4D\u7F6E", "\u6587\u4EF6", "file"]);
4300
+ var FORBIDDEN_HEADERS2 = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
4301
+ function looksLikeHeader(row) {
4302
+ const normalized = row.map(normalizeHeader2);
4303
+ return normalized.some((cell) => SOURCE_HEADERS3.has(cell)) && normalized.some((cell) => TARGET_HEADERS3.has(cell));
4304
+ }
4305
+ function headerColumn(row, aliases) {
4306
+ return row.findIndex((cell) => aliases.has(normalizeHeader2(cell)));
4307
+ }
4308
+ function columnIndex(value, header) {
4309
+ const trimmed = value.trim();
4310
+ if (/^[A-Za-z]{1,3}$/u.test(trimmed)) {
4311
+ let index = 0;
4312
+ for (const char of trimmed.toUpperCase()) index = index * 26 + (char.charCodeAt(0) - 64);
4313
+ return index - 1;
4314
+ }
4315
+ if (/^\d+$/u.test(trimmed)) {
4316
+ const number = Number(trimmed);
4317
+ if (number < 1) throw new Error(`column index must be 1-based: ${value}`);
4318
+ return number - 1;
4319
+ }
4320
+ if (header !== void 0) {
4321
+ const normalized = normalizeHeader2(trimmed);
4322
+ const index = header.findIndex((cell) => normalizeHeader2(cell) === normalized);
4323
+ if (index >= 0) return index;
4324
+ }
4325
+ throw new Error(`cannot resolve column "${value}"; use a 1-based index, spreadsheet letter, or a header name`);
4326
+ }
4327
+ async function readMatrix(path, format, mapping2) {
4328
+ if (format === "xlsx") {
4329
+ const workbook = new ExcelJS2.Workbook();
4330
+ await workbook.xlsx.load(await readFile(path));
4331
+ const sheet = mapping2?.sheet;
4332
+ let worksheet = workbook.worksheets[0];
4333
+ if (sheet !== void 0) {
4334
+ worksheet = /^\d+$/u.test(sheet) ? workbook.worksheets[Number(sheet) - 1] : workbook.getWorksheet(sheet);
4335
+ }
4336
+ if (!worksheet) throw new Error(`XLSX worksheet "${sheet ?? "1"}" was not found`);
4337
+ const rows = [];
4338
+ worksheet.eachRow({ includeEmpty: false }, (row) => {
4339
+ const values = [];
4340
+ for (let index = 1; index <= row.cellCount; index += 1) values.push(row.getCell(index).text ?? "");
4341
+ rows.push(values);
4342
+ });
4343
+ return { rows, warnings: sheet === void 0 && workbook.worksheets.length > 1 ? [`XLSX has ${workbook.worksheets.length} worksheets; using the first`] : [] };
4344
+ }
4345
+ const text = (await readFile(path, "utf8")).replace(/^\uFEFF/u, "");
4346
+ if (text.includes("\uFFFD")) throw new Error("bilingual asset is not valid UTF-8");
4347
+ const warnings = [];
4348
+ if (format === "csv") {
4349
+ const firstLine = text.split(/\r?\n/u)[0] ?? "";
4350
+ const tabs = (firstLine.match(/\t/gu) ?? []).length;
4351
+ const commas = (firstLine.match(/,/gu) ?? []).length;
4352
+ return { rows: parseCsvRows(text, tabs > commas ? " " : ","), warnings };
4353
+ }
4354
+ if (format === "tsv" || format === "txt") {
4355
+ return {
4356
+ rows: text.split(/\r?\n/u).filter((line) => line.trim() !== "").map((line) => line.split(" ")),
4357
+ warnings
4358
+ };
4359
+ }
4360
+ throw new Error(`unsupported bilingual format: ${format}`);
4361
+ }
4362
+ async function parseBilingualAsset(path, format, mapping2) {
4363
+ const matrix = await readMatrix(path, format, mapping2);
4364
+ if (matrix.rows.length === 0) throw new Error("bilingual table is empty");
4365
+ const warnings = [...matrix.warnings];
4366
+ const first = matrix.rows[0] ?? [];
4367
+ const headerRow = mapping2?.headerRow ?? (looksLikeHeader(first) ? 1 : 0);
4368
+ const autoHeader = headerRow === 1 && looksLikeHeader(first);
4369
+ const header = headerRow > 0 ? matrix.rows[headerRow - 1] : void 0;
4370
+ const sourceColumn = mapping2?.sourceColumn !== void 0 ? columnIndex(mapping2.sourceColumn, header) : autoHeader ? headerColumn(first, SOURCE_HEADERS3) : 0;
4371
+ const targetColumn = mapping2?.targetColumn !== void 0 ? columnIndex(mapping2.targetColumn, header) : autoHeader ? headerColumn(first, TARGET_HEADERS3) : 1;
4372
+ if (sourceColumn < 0 || targetColumn < 0) {
4373
+ throw new Error("could not detect source/target columns; pass mapping.sourceColumn/targetColumn");
4374
+ }
4375
+ const idColumn = autoHeader ? headerColumn(first, ID_HEADERS2) : -1;
4376
+ const locationColumn = autoHeader ? headerColumn(first, LOCATION_HEADERS2) : -1;
4377
+ const rows = [];
4378
+ for (let index = headerRow; index < matrix.rows.length; index += 1) {
4379
+ const row = matrix.rows[index] ?? [];
4380
+ const source = (row[sourceColumn] ?? "").trim();
4381
+ const target = (row[targetColumn] ?? "").trim();
4382
+ if (source === "" && target === "") continue;
4383
+ if (source === "") {
4384
+ warnings.push(`row ${index + 1}: empty source`);
4385
+ continue;
4386
+ }
4387
+ const id = (idColumn >= 0 ? (row[idColumn] ?? "").trim() : "") || `row-${index + 1 - headerRow}`;
4388
+ rows.push({
4389
+ id,
4390
+ source,
4391
+ target,
4392
+ ...locationColumn >= 0 && (row[locationColumn] ?? "").trim() !== "" ? { location: (row[locationColumn] ?? "").trim() } : {}
4393
+ });
4394
+ }
4395
+ if (rows.length === 0) throw new Error("bilingual table has no usable rows");
4396
+ return { rows, warnings };
4397
+ }
4398
+ function glossaryFlag(value) {
4399
+ return /^(?:true|1|yes|y|是|禁用)$/iu.test(value.trim());
4400
+ }
4401
+ async function readGlossaryEntries(path, format, sourceLang, targetLang, mapping2) {
4402
+ if (format === "tbx") {
4403
+ if (mapping2 !== void 0) throw new Error("mapping is only supported for CSV/TSV glossaries");
4404
+ const parsed = parseTbx((await readFile(path, "utf8")).replace(/^\uFEFF/u, ""), {
4405
+ ...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
4406
+ ...targetLang === void 0 ? {} : { targetLanguage: targetLang }
4407
+ });
4408
+ return { entries: parsed.entries, warnings: parsed.warnings };
4409
+ }
4410
+ if (mapping2 === void 0) {
4411
+ const text = (await readFile(path, "utf8")).replace(/^\uFEFF/u, "");
4412
+ if (text.includes("\uFFFD")) throw new Error("glossary asset is not valid UTF-8");
4413
+ const parsed = parseGlossaryCsv(text, {
4414
+ ...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
4415
+ ...targetLang === void 0 ? {} : { targetLanguage: targetLang }
4416
+ });
4417
+ return { entries: parsed.entries, warnings: parsed.warnings };
4418
+ }
4419
+ const matrix = await readMatrix(path, format, mapping2);
4420
+ if (matrix.rows.length === 0) throw new Error("glossary table is empty");
4421
+ const warnings = [...matrix.warnings];
4422
+ const first = matrix.rows[0] ?? [];
4423
+ const headerRow = mapping2.headerRow ?? (looksLikeHeader(first) ? 1 : 0);
4424
+ const autoHeader = headerRow === 1 && looksLikeHeader(first);
4425
+ const header = headerRow > 0 ? matrix.rows[headerRow - 1] : void 0;
4426
+ const sourceColumn = mapping2.sourceColumn !== void 0 ? columnIndex(mapping2.sourceColumn, header) : autoHeader ? headerColumn(first, SOURCE_HEADERS3) : 0;
4427
+ const targetColumn = mapping2.targetColumn !== void 0 ? columnIndex(mapping2.targetColumn, header) : autoHeader ? headerColumn(first, TARGET_HEADERS3) : 1;
4428
+ const forbiddenColumn = mapping2.forbiddenColumn !== void 0 ? columnIndex(mapping2.forbiddenColumn, header) : autoHeader ? headerColumn(first, FORBIDDEN_HEADERS2) : -1;
4429
+ if (sourceColumn < 0 || targetColumn < 0) {
4430
+ throw new Error("could not resolve glossary source/target columns; pass mapping.sourceColumn/targetColumn");
4431
+ }
4432
+ const entries = [];
4433
+ for (let index = headerRow; index < matrix.rows.length; index += 1) {
4434
+ const row = matrix.rows[index] ?? [];
4435
+ const source = (row[sourceColumn] ?? "").trim();
4436
+ const target = (row[targetColumn] ?? "").trim();
4437
+ if (source === "" && target === "") continue;
4438
+ if (source === "" || target === "") {
4439
+ warnings.push(`row ${index + 1} is missing ${source === "" ? "source" : "target"}; skipped`);
4440
+ continue;
4441
+ }
4442
+ const forbidden = forbiddenColumn >= 0 ? glossaryFlag(row[forbiddenColumn] ?? "") : false;
4443
+ entries.push({
4444
+ id: `glossary-${entries.length + 1}`,
4445
+ source,
4446
+ target,
4447
+ ...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
4448
+ ...targetLang === void 0 ? {} : { targetLanguage: targetLang },
4449
+ ...forbidden ? { forbidden: true } : {}
4450
+ });
4451
+ }
4452
+ return { entries, warnings };
4453
+ }
4454
+ function bilingualRowsToTm(rows, assetId, sourceLang, targetLang) {
4455
+ return rows.filter((row) => row.target.trim() !== "").map((row) => ({
4456
+ id: `${assetId}:${row.id}`,
4457
+ source: row.source,
4458
+ target: row.target,
4459
+ sourceLanguage: sourceLang,
4460
+ targetLanguage: targetLang,
4461
+ origin: "tm"
4462
+ }));
4463
+ }
4464
+ async function parseAssetFile(absolutePath, kind, format, options) {
4465
+ const warnings = [];
4466
+ if (kind === "tm") {
4467
+ if (format !== "tmx") throw new Error("kind tm requires a .tmx file");
4468
+ const parsed2 = parseTmx((await readFile(absolutePath, "utf8")).replace(/^\uFEFF/u, ""));
4469
+ warnings.push(...parsed2.warnings);
4470
+ const entries2 = parsed2.entries.map((entry) => ({
4471
+ ...entry,
4472
+ id: `${options.id}:${entry.id}`,
4473
+ ...options.sourceLang === void 0 ? {} : { sourceLanguage: options.sourceLang },
4474
+ ...options.targetLang === void 0 ? {} : { targetLanguage: options.targetLang }
4475
+ }));
4476
+ return { counts: { entries: entries2.length }, warnings, tmEntries: entries2, glossary: [] };
4477
+ }
4478
+ if (kind === "tb") {
4479
+ if (format !== "tbx" && format !== "csv" && format !== "tsv") {
4480
+ throw new Error("kind tb requires a .tbx, .csv or .tsv glossary");
4481
+ }
4482
+ const parsed2 = await readGlossaryEntries(
4483
+ absolutePath,
4484
+ format,
4485
+ options.sourceLang,
4486
+ options.targetLang,
4487
+ options.mapping
4488
+ );
4489
+ warnings.push(...parsed2.warnings);
4490
+ const entries2 = parsed2.entries.map((entry) => ({ ...entry, id: `${options.id}:${entry.id}` }));
4491
+ return { counts: { entries: entries2.length }, warnings, tmEntries: [], glossary: entries2 };
4492
+ }
4493
+ if (format !== "csv" && format !== "tsv" && format !== "txt" && format !== "xlsx") {
4494
+ throw new Error("kind bilingual requires a .csv, .tsv, .txt or .xlsx table");
4495
+ }
4496
+ const parsed = await parseBilingualAsset(absolutePath, format, options.mapping);
4497
+ warnings.push(...parsed.warnings);
4498
+ const sourceLang = options.sourceLang;
4499
+ const targetLang = options.targetLang;
4500
+ if (sourceLang === void 0 || targetLang === void 0) {
4501
+ throw new Error("bilingual assets require sourceLang and targetLang (pass them on l10n_asset_add or set them on the project)");
4502
+ }
4503
+ const entries = bilingualRowsToTm(parsed.rows, options.id, sourceLang, targetLang);
4504
+ return { counts: { entries: entries.length }, warnings, tmEntries: entries, glossary: [] };
4505
+ }
4506
+ function kindFromPath(path, format, source) {
4507
+ if (format === "tmx") return { kind: "tm", format };
4508
+ if (format === "tbx") return { kind: "tb", format };
4509
+ if (format === "xlsx") return { kind: "bilingual", format };
4510
+ if (format === "tsv" || format === "txt") return { kind: "bilingual", format };
4511
+ if (format === "csv") {
4512
+ let firstLine = "";
4513
+ try {
4514
+ firstLine = source.split(/\r?\n/u)[0] ?? "";
4515
+ } catch {
4516
+ firstLine = "";
4517
+ }
4518
+ const header = firstLine.split(",").map(normalizeHeader2);
4519
+ if (header.some((cell) => FORBIDDEN_HEADERS2.has(cell))) return { kind: "tb", format };
4520
+ if (header.some((cell) => ID_HEADERS2.has(cell)) || header.some((cell) => LOCATION_HEADERS2.has(cell))) {
4521
+ return { kind: "bilingual", format };
4522
+ }
4523
+ return { kind: "tb", format };
4524
+ }
4525
+ throw new Error(`unsupported asset format: ${path}`);
4526
+ }
4527
+ function absoluteAssetPath(root, path) {
4528
+ return isAbsolute(path) ? resolve(path) : resolve(root, path);
4529
+ }
4530
+ function storedAssetPath(root, absolutePath) {
4531
+ const relativePath = relative(root, absolutePath);
4532
+ if (relativePath === "" || relativePath.startsWith("..") || isAbsolute(relativePath)) return absolutePath;
4533
+ return relativePath.split("\\").join("/");
4534
+ }
4535
+ function nextAssetId(manifest) {
4536
+ const used = new Set(manifest.assets.map((asset) => asset.id));
4537
+ for (let index = 1; index <= MAX_ASSETS + 1; index += 1) {
4538
+ const id = `asset-${index}`;
4539
+ if (!used.has(id)) return id;
4540
+ }
4541
+ throw new Error(`project already has ${MAX_ASSETS} assets`);
4542
+ }
4543
+ async function readAssetBytes(absolutePath) {
4544
+ const info = await stat(absolutePath);
4545
+ if (!info.isFile()) throw new Error(`asset is not a regular file: ${absolutePath}`);
4546
+ if (info.size > MAX_ASSET_BYTES) throw new Error(`asset exceeds ${MAX_ASSET_BYTES} bytes: ${absolutePath}`);
4547
+ const buffer = await readFile(absolutePath);
4548
+ return { size: buffer.byteLength, sha256: createHash2("sha256").update(buffer).digest("hex") };
4549
+ }
4550
+ async function addAsset(handle, options) {
4551
+ const absolutePath = absoluteAssetPath(handle.root, options.path);
4552
+ let info;
4553
+ try {
4554
+ info = await stat(absolutePath);
4555
+ } catch {
4556
+ throw new Error(`asset file does not exist: ${absolutePath}`);
4557
+ }
4558
+ if (!info.isFile()) throw new Error(`asset is not a regular file: ${absolutePath}`);
4559
+ const format = options.format ?? formatOf(absolutePath);
4560
+ let peek = "";
4561
+ if (format === "csv") peek = (await readFile(absolutePath, "utf8")).slice(0, 4096);
4562
+ const inferred = options.kind === void 0 ? kindFromPath(absolutePath, format, peek) : void 0;
4563
+ const kind = options.kind ?? inferred?.kind;
4564
+ if (kind === void 0) throw new Error(`unsupported asset format: ${absolutePath}`);
4565
+ if (kind !== "bilingual" && format !== "tmx" && format !== "tbx" && format !== "csv" && format !== "tsv") {
4566
+ throw new Error(`kind ${kind} does not support format ${format}`);
4567
+ }
4568
+ const sourceLang = options.sourceLang ?? handle.manifest.sourceLang;
4569
+ const targetLang = options.targetLang ?? handle.manifest.targetLang;
4570
+ const id = options.id ?? nextAssetId(handle.manifest);
4571
+ if (!/^[A-Za-z0-9._-]{1,64}$/u.test(id)) throw new Error("asset id must match [A-Za-z0-9._-]{1,64}");
4572
+ if (handle.manifest.assets.some((asset2) => asset2.id === id)) throw new Error(`asset id already exists: ${id}`);
4573
+ if (handle.manifest.assets.some((asset2) => absoluteAssetPath(handle.root, asset2.path) === absolutePath && asset2.kind === kind)) {
4574
+ throw new Error(`asset already registered for ${absolutePath} (${kind}); use l10n_asset_update`);
4575
+ }
4576
+ if (handle.manifest.assets.length >= MAX_ASSETS) throw new Error(`project already has ${MAX_ASSETS} assets`);
4577
+ const { size, sha256 } = await readAssetBytes(absolutePath);
4578
+ const parsed = await parseAssetFile(absolutePath, kind, format, {
4579
+ id,
4580
+ ...sourceLang === void 0 ? {} : { sourceLang },
4581
+ ...targetLang === void 0 ? {} : { targetLang },
4582
+ ...options.mapping === void 0 ? {} : { mapping: options.mapping }
4583
+ });
4584
+ if (parsed.counts.entries === 0) throw new Error("asset contains no usable entries");
4585
+ const now = (/* @__PURE__ */ new Date()).toISOString();
4586
+ const asset = {
4587
+ id,
4588
+ kind,
4589
+ format,
4590
+ path: storedAssetPath(handle.root, absolutePath),
4591
+ enabled: options.enabled !== false,
4592
+ priority: options.priority ?? 100,
4593
+ ...sourceLang === void 0 ? {} : { sourceLang },
4594
+ ...targetLang === void 0 ? {} : { targetLang },
4595
+ sha256,
4596
+ size,
4597
+ counts: parsed.counts,
4598
+ ...options.mapping === void 0 ? {} : { mapping: options.mapping },
4599
+ addedAt: now,
4600
+ updatedAt: now
4601
+ };
4602
+ handle.manifest.assets.push(asset);
4603
+ await writeManifest(handle);
4604
+ return { asset, warnings: parsed.warnings };
4605
+ }
4606
+ async function updateAsset(handle, options) {
4607
+ const asset = handle.manifest.assets.find((item) => item.id === options.id);
4608
+ if (asset === void 0) throw new Error(`unknown asset id: ${options.id}`);
4609
+ const absolutePath = absoluteAssetPath(handle.root, asset.path);
4610
+ const warnings = [];
4611
+ const needsRescan = options.rescan === true || options.mapping !== void 0 || options.sourceLang !== void 0 || options.targetLang !== void 0;
4612
+ if (options.enabled !== void 0) asset.enabled = options.enabled;
4613
+ if (options.priority !== void 0) {
4614
+ if (!Number.isFinite(options.priority)) throw new Error("priority must be a finite number");
4615
+ asset.priority = options.priority;
4616
+ }
4617
+ if (options.sourceLang !== void 0) asset.sourceLang = options.sourceLang;
4618
+ if (options.targetLang !== void 0) asset.targetLang = options.targetLang;
4619
+ if (options.mapping !== void 0) asset.mapping = options.mapping;
4620
+ if (needsRescan) {
4621
+ const parsed = await parseAssetFile(absolutePath, asset.kind, asset.format, {
4622
+ id: asset.id,
4623
+ ...asset.sourceLang === void 0 ? {} : { sourceLang: asset.sourceLang },
4624
+ ...asset.targetLang === void 0 ? {} : { targetLang: asset.targetLang },
4625
+ ...asset.mapping === void 0 ? {} : { mapping: asset.mapping }
4626
+ });
4627
+ if (parsed.counts.entries === 0) throw new Error("asset contains no usable entries after rescan");
4628
+ const { size, sha256 } = await readAssetBytes(absolutePath);
4629
+ asset.counts = parsed.counts;
4630
+ asset.size = size;
4631
+ asset.sha256 = sha256;
4632
+ warnings.push(...parsed.warnings);
4633
+ }
4634
+ asset.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
4635
+ await writeManifest(handle);
4636
+ return { asset, warnings };
4637
+ }
4638
+ async function removeAsset(handle, id) {
4639
+ const index = handle.manifest.assets.findIndex((asset) => asset.id === id);
4640
+ if (index < 0) throw new Error(`unknown asset id: ${id}`);
4641
+ const removed = handle.manifest.assets[index];
4642
+ handle.manifest.assets.splice(index, 1);
4643
+ await writeManifest(handle);
4644
+ return { removed, remaining: handle.manifest.assets.length };
4645
+ }
4646
+ function assetAbsolutePath(handle, asset) {
4647
+ return absoluteAssetPath(handle.root, asset.path);
4648
+ }
4649
+ function normalizeLanguageTag(language) {
4650
+ return language.trim().replace(/_/gu, "-").toLowerCase();
4651
+ }
4652
+ function languageCompatible(actual, wanted) {
4653
+ if (actual === void 0 || wanted === void 0 || wanted.trim() === "") return true;
4654
+ const a = normalizeLanguageTag(actual);
4655
+ const w = normalizeLanguageTag(wanted);
4656
+ if (a === w) return true;
4657
+ return a.split("-")[0] === w.split("-")[0];
4658
+ }
4659
+ async function collectProjectAssets(handle, options = {}) {
4660
+ const tmEntries = [];
4661
+ const glossary = [];
4662
+ const used = [];
4663
+ const warnings = [];
4664
+ const enabled = handle.manifest.assets.filter((asset) => asset.enabled).sort((left, right) => right.priority - left.priority || left.id.localeCompare(right.id));
4665
+ for (const asset of enabled) {
4666
+ if (!languageCompatible(asset.sourceLang, options.sourceLanguage) || !languageCompatible(asset.targetLang, options.targetLanguage)) {
4667
+ continue;
4668
+ }
4669
+ const absolutePath = assetAbsolutePath(handle, asset);
4670
+ let parsed;
4671
+ try {
4672
+ parsed = await parseAssetFile(absolutePath, asset.kind, asset.format, {
4673
+ id: asset.id,
4674
+ ...asset.sourceLang === void 0 ? {} : { sourceLang: asset.sourceLang },
4675
+ ...asset.targetLang === void 0 ? {} : { targetLang: asset.targetLang },
4676
+ ...asset.mapping === void 0 ? {} : { mapping: asset.mapping }
4677
+ });
4678
+ } catch (caught) {
4679
+ warnings.push(`asset ${asset.id} (${asset.path}) could not be parsed: ${caught instanceof Error ? caught.message : String(caught)}`);
4680
+ continue;
4681
+ }
4682
+ tmEntries.push(...parsed.tmEntries);
4683
+ glossary.push(...parsed.glossary);
4684
+ used.push({ id: asset.id, kind: asset.kind, path: asset.path, entries: parsed.counts.entries });
4685
+ }
4686
+ const merged = mergeTmEntries(tmEntries);
4687
+ return { tmEntries: merged.entries, glossary, used, warnings };
4688
+ }
4689
+ async function projectStatus(handle, verify = false) {
4690
+ const assets = [];
4691
+ for (const asset of handle.manifest.assets) {
4692
+ const absolutePath = assetAbsolutePath(handle, asset);
4693
+ let exists = false;
4694
+ let sizeOnDisk;
4695
+ let hashMatch;
4696
+ try {
4697
+ const info = await stat(absolutePath);
4698
+ exists = info.isFile();
4699
+ if (exists) {
4700
+ sizeOnDisk = info.size;
4701
+ if (verify) {
4702
+ const { sha256 } = await readAssetBytes(absolutePath);
4703
+ hashMatch = sha256 === asset.sha256;
4704
+ }
4705
+ }
4706
+ } catch {
4707
+ exists = false;
4708
+ }
4709
+ assets.push({ ...asset, absolutePath, exists, ...sizeOnDisk === void 0 ? {} : { sizeOnDisk }, ...hashMatch === void 0 ? {} : { hashMatch } });
4710
+ }
4711
+ return {
4712
+ root: handle.root,
4713
+ manifestPath: handle.manifestPath,
4714
+ name: handle.manifest.name,
4715
+ sourceLang: handle.manifest.sourceLang,
4716
+ targetLang: handle.manifest.targetLang,
4717
+ createdAt: handle.manifest.createdAt,
4718
+ updatedAt: handle.manifest.updatedAt,
4719
+ qa: handle.manifest.qa,
4720
+ assets,
4721
+ enabledAssets: assets.filter((asset) => asset.enabled).length,
4722
+ trackedFiles: handle.manifest.files.length
4723
+ };
4724
+ }
4725
+ function projectDefaults(handle) {
4726
+ return handle.manifest.qa;
4727
+ }
4728
+
4729
+ // packages/dsh-l10n/src/tools-project.ts
4730
+ var mappingSchema = {
4731
+ type: "object",
4732
+ additionalProperties: false,
4733
+ description: "Bilingual-table layout overrides; omit to auto-detect a source/target header.",
4734
+ properties: {
4735
+ sheet: { type: "string", description: "XLSX worksheet name or 1-based index." },
4736
+ headerRow: { type: "integer", description: "1-based header row; 0 means no header." },
4737
+ sourceColumn: { type: "string", description: "1-based column, spreadsheet letter, or header name." },
4738
+ targetColumn: { type: "string", description: "1-based column, spreadsheet letter, or header name." },
4739
+ forbiddenColumn: { type: "string", description: "Glossary-only forbidden flag column." }
4740
+ }
4741
+ };
4742
+ function mapping(value) {
4743
+ if (value === void 0) return void 0;
4744
+ return value;
4745
+ }
4746
+ function createProjectInitTool() {
4747
+ return defineTool2({
4748
+ name: "l10n_project_init",
4749
+ description: "Create or update a local localization project (.langlink/manifest.json) with language pair and directories. Does not overwrite existing assets unless overwrite=true.",
4750
+ parameters: {
4751
+ root: { type: "string", required: true, description: "Project root directory (created if missing)." },
4752
+ sourceLang: { type: "string", required: true, description: "Source language tag, e.g. en-US." },
4753
+ targetLang: { type: "string", required: true, description: "Target language tag, e.g. zh-CN." },
4754
+ name: { type: "string", description: "Project name; defaults to localization." },
4755
+ locale: { type: "string", description: "Default target locale for QA language-variant rules." },
4756
+ threshold: { type: "integer", description: "Default TM match threshold 50..100 for translate/match tools." },
4757
+ maxMatches: { type: "integer", description: "Default number of TM candidates per segment." },
4758
+ untranslatedCopyWhitelist: {
4759
+ type: "array",
4760
+ items: { type: "string" },
4761
+ description: "Default source texts exempt from UNTRANSLATED_COPY."
4762
+ },
4763
+ overwrite: { type: "boolean", description: "Update language pair/QA settings of an existing manifest; assets are preserved." }
4764
+ },
4765
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4766
+ async execute(args) {
4767
+ if (args.threshold !== void 0 && (args.threshold < 50 || args.threshold > 100)) {
4768
+ throw new Error("threshold must be 50..100");
4769
+ }
4770
+ if (args.maxMatches !== void 0 && args.maxMatches < 1) throw new Error("maxMatches must be >= 1");
4771
+ const handle = await initProject({
4772
+ root: args.root,
4773
+ sourceLang: args.sourceLang,
4774
+ targetLang: args.targetLang,
4775
+ ...args.name === void 0 ? {} : { name: args.name },
4776
+ ...args.locale === void 0 ? {} : { locale: args.locale },
4777
+ ...args.threshold === void 0 ? {} : { threshold: args.threshold },
4778
+ ...args.maxMatches === void 0 ? {} : { maxMatches: args.maxMatches },
4779
+ ...args.untranslatedCopyWhitelist === void 0 ? {} : { untranslatedCopyWhitelist: args.untranslatedCopyWhitelist },
4780
+ ...args.overwrite === void 0 ? {} : { overwrite: args.overwrite }
4781
+ });
4782
+ return JSON.stringify({
4783
+ root: handle.root,
4784
+ manifestPath: handle.manifestPath,
4785
+ name: handle.manifest.name,
4786
+ sourceLang: handle.manifest.sourceLang,
4787
+ targetLang: handle.manifest.targetLang,
4788
+ assets: handle.manifest.assets.length,
4789
+ qa: handle.manifest.qa ?? {}
4790
+ });
4791
+ }
4792
+ });
4793
+ }
4794
+ function createProjectStatusTool() {
4795
+ return defineTool2({
4796
+ name: "l10n_project_status",
4797
+ description: "Read the local project manifest: language pair, QA defaults, tracked files and registered assets (enabled state, counts, existence, optional hash check).",
4798
+ parameters: {
4799
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
4800
+ input: { type: "string", description: "File to discover the project from when project is omitted." },
4801
+ verify: { type: "boolean", description: "Re-hash asset files and report hashMatch; slower for large assets." }
4802
+ },
4803
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4804
+ async execute(args) {
4805
+ const handle = await requireProject(args.project, args.input);
4806
+ const status = await projectStatus(handle, args.verify === true);
4807
+ const warnings = [];
4808
+ for (const asset of status.assets) {
4809
+ if (!asset.exists) warnings.push(`asset ${asset.id} is missing: ${asset.absolutePath}`);
4810
+ else if (asset.hashMatch === false) warnings.push(`asset ${asset.id} changed since import; run l10n_asset_update with rescan=true`);
4811
+ }
4812
+ return JSON.stringify({ ...status, warnings });
4813
+ }
4814
+ });
4815
+ }
4816
+ function createAssetAddTool() {
4817
+ return defineTool2({
4818
+ name: "l10n_asset_add",
4819
+ description: "Register a local asset in the project manifest and parse it now. TMX = translation memory; TBX/CSV glossary = terminology; CSV/TSV/TXT/XLSX bilingual table = TM pairs (source\u2192target). Auto-detects kind from the file, or pass kind/mapping explicitly.",
4820
+ parameters: {
4821
+ path: { type: "string", required: true, description: "Asset file path (relative to the project root or absolute)." },
4822
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered from path when omitted." },
4823
+ kind: { type: "string", enum: ["tm", "tb", "bilingual"], description: "Asset kind; auto-detected when omitted." },
4824
+ format: { type: "string", enum: ["tmx", "tbx", "csv", "tsv", "txt", "xlsx"], description: "File format; inferred from extension when omitted." },
4825
+ id: { type: "string", description: "Asset id; defaults to the next asset-N." },
4826
+ enabled: { type: "boolean", description: "Whether the asset participates in matching/QA; default true." },
4827
+ priority: { type: "integer", description: "Higher priority is used first; default 100." },
4828
+ sourceLang: { type: "string", description: "Asset source language; defaults to the project language pair." },
4829
+ targetLang: { type: "string", description: "Asset target language; defaults to the project language pair." },
4830
+ mapping: mappingSchema
4831
+ },
4832
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4833
+ async execute(args) {
4834
+ const handle = await requireProject(args.project, args.path);
4835
+ const result = await addAsset(handle, {
4836
+ path: args.path,
4837
+ ...args.kind === void 0 ? {} : { kind: args.kind },
4838
+ ...args.format === void 0 ? {} : { format: args.format },
4839
+ ...args.id === void 0 ? {} : { id: args.id },
4840
+ ...args.enabled === void 0 ? {} : { enabled: args.enabled },
4841
+ ...args.priority === void 0 ? {} : { priority: args.priority },
4842
+ ...args.sourceLang === void 0 ? {} : { sourceLang: args.sourceLang },
4843
+ ...args.targetLang === void 0 ? {} : { targetLang: args.targetLang },
4844
+ ...mapping(args.mapping) === void 0 ? {} : { mapping: mapping(args.mapping) }
4845
+ });
4846
+ return JSON.stringify({
4847
+ root: handle.root,
4848
+ asset: result.asset,
4849
+ warnings: result.warnings
4850
+ });
4851
+ }
4852
+ });
4853
+ }
4854
+ function createAssetUpdateTool() {
4855
+ return defineTool2({
4856
+ name: "l10n_asset_update",
4857
+ description: "Update a registered asset: enable/disable for the session, priority, language pair, import mapping, or rescan the file (hash/counts). Changing languages/mapping/rescan re-parses immediately.",
4858
+ parameters: {
4859
+ id: { type: "string", required: true, description: "Asset id from l10n_project_status." },
4860
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
4861
+ input: { type: "string", description: "File to discover the project from when project is omitted." },
4862
+ enabled: { type: "boolean", description: "Whether the asset participates in matching/QA." },
4863
+ priority: { type: "integer", description: "Higher priority is used first." },
4864
+ sourceLang: { type: "string", description: "Asset source language." },
4865
+ targetLang: { type: "string", description: "Asset target language." },
4866
+ mapping: mappingSchema,
4867
+ rescan: { type: "boolean", description: "Re-read the file, recompute hash/counts and report changed warnings." }
4868
+ },
4869
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4870
+ async execute(args) {
4871
+ const handle = await requireProject(args.project, args.input);
4872
+ const result = await updateAsset(handle, {
4873
+ id: args.id,
4874
+ ...args.enabled === void 0 ? {} : { enabled: args.enabled },
4875
+ ...args.priority === void 0 ? {} : { priority: args.priority },
4876
+ ...args.sourceLang === void 0 ? {} : { sourceLang: args.sourceLang },
4877
+ ...args.targetLang === void 0 ? {} : { targetLang: args.targetLang },
4878
+ ...mapping(args.mapping) === void 0 ? {} : { mapping: mapping(args.mapping) },
4879
+ ...args.rescan === void 0 ? {} : { rescan: args.rescan }
4880
+ });
4881
+ return JSON.stringify({ root: handle.root, asset: result.asset, warnings: result.warnings });
4882
+ }
4883
+ });
4884
+ }
4885
+ function createAssetRemoveTool() {
4886
+ return defineTool2({
4887
+ name: "l10n_asset_remove",
4888
+ description: "Remove an asset entry from the project manifest. The asset file on disk is left untouched.",
4889
+ parameters: {
4890
+ id: { type: "string", required: true, description: "Asset id from l10n_project_status." },
4891
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
4892
+ input: { type: "string", description: "File to discover the project from when project is omitted." }
4893
+ },
4894
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4895
+ async execute(args) {
4896
+ const handle = await requireProject(args.project, args.input);
4897
+ const result = await removeAsset(handle, args.id);
4898
+ return JSON.stringify({ root: handle.root, removed: result.removed.id, remaining: result.remaining });
4899
+ }
4900
+ });
4901
+ }
4902
+ async function optionalProject(project, anchor) {
4903
+ return findProject(project, anchor);
4904
+ }
4905
+
4906
+ // packages/dsh-l10n/src/tools-phase1.ts
4907
+ import { createHash as createHash3 } from "node:crypto";
4908
+ import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
4909
+ import { basename, resolve as resolve2 } from "node:path";
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
5514
+ var MAX_BYTES = 200 * 1024 * 1024;
5515
+ async function readBytes(path) {
5516
+ const absolute = resolve2(path);
5517
+ const info = await stat2(absolute);
5518
+ if (!info.isFile()) throw new Error("path is not a regular file");
5519
+ if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
5520
+ return readFile2(absolute);
5521
+ }
5522
+ async function readUtf8(path) {
5523
+ const absolute = resolve2(path);
5524
+ const info = await stat2(absolute);
5525
+ if (!info.isFile()) throw new Error("path is not a regular file");
5526
+ if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
5527
+ const value = await readFile2(absolute, { encoding: "utf8" });
5528
+ if (value.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
5529
+ return value;
5530
+ }
5531
+ async function readGlossary(path) {
5532
+ const text = await readUtf8(path);
5533
+ return /\.tbx$/iu.test(path) ? parseTbx(text) : parseGlossaryCsv(text);
5534
+ }
5535
+ async function loadDocumentAssets(document2, anchor, options) {
5536
+ const warnings = [];
5537
+ const tmEntries = [];
5538
+ const glossary = [];
5539
+ let used = [];
5540
+ const file = document2.files[0];
5541
+ let project;
5542
+ if (options.project !== void 0) {
5543
+ project = await findProject(options.project, anchor);
5544
+ } else {
5545
+ try {
5546
+ project = await findProject(void 0, anchor);
5547
+ } catch (caught) {
5548
+ warnings.push(`project ignored: ${caught instanceof Error ? caught.message : String(caught)}`);
5549
+ }
5550
+ }
5551
+ if (project !== void 0) {
5552
+ const collected = await collectProjectAssets(project, {
5553
+ ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
5554
+ ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
5555
+ });
5556
+ tmEntries.push(...collected.tmEntries);
5557
+ glossary.push(...collected.glossary);
5558
+ used = collected.used;
5559
+ warnings.push(...collected.warnings);
5560
+ }
5561
+ if (options.tm !== void 0) tmEntries.push(...parseTmx(await readUtf8(options.tm)).entries);
5562
+ if (options.glossary !== void 0) glossary.push(...(await readGlossary(options.glossary)).entries);
5563
+ return {
5564
+ ...project === void 0 ? {} : { project },
5565
+ tmEntries: mergeTmEntries(tmEntries).entries,
5566
+ glossary,
5567
+ used,
5568
+ warnings
5569
+ };
5570
+ }
5571
+ function documentFromXml(xml, path) {
5572
+ return parseXliff(xml, { filename: basename(path) });
5573
+ }
5574
+ function createMatchTmTool() {
5575
+ return defineTool3({
5576
+ name: "l10n_match_tm",
5577
+ description: "Match a local XLIFF/SDLXLIFF file against a TMX and/or the enabled TM/bilingual assets of the enclosing project. Returns match-rate metadata per segment (no file modification).",
5578
+ parameters: {
5579
+ path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
5580
+ tm: { type: "string", description: "Optional TMX 1.4b file path; project assets are used too." },
5581
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
5582
+ threshold: { type: "integer", description: "Minimum matchRate 50..100; default from project or 75." },
5583
+ maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
5584
+ },
5585
+ output: {
5586
+ schema: { type: "string" },
5587
+ render: (_args, value) => [{ type: "text", text: value }]
5588
+ },
5589
+ async execute(args) {
5590
+ const xml = await readUtf8(args.path);
5591
+ const document2 = documentFromXml(xml, args.path);
5592
+ const assets = await loadDocumentAssets(document2, args.path, {
5593
+ ...args.project === void 0 ? {} : { project: args.project },
5594
+ ...args.tm === void 0 ? {} : { tm: args.tm }
5595
+ });
5596
+ if (assets.tmEntries.length === 0) {
5597
+ throw new Error("no TM entries: pass tm or register TM/bilingual assets with l10n_asset_add");
5598
+ }
5599
+ const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
5600
+ const threshold = args.threshold ?? defaults?.threshold ?? 75;
5601
+ if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
5602
+ const limit = Math.max(1, args.maxSegments ?? 50);
5603
+ const matches = document2.segments.slice(0, limit).map((segment) => {
5604
+ const file = document2.files[segment.location.fileIndex];
5605
+ const candidates = matchTm(assets.tmEntries, segment.sourceText, {
5606
+ threshold,
5607
+ maxMatches: 1,
5608
+ ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
5609
+ ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
5610
+ });
5611
+ const best = candidates[0];
5612
+ return {
5613
+ segmentId: segment.id,
5614
+ matchRate: best?.matchRate ?? 0,
5615
+ similarity: best?.similarity ?? 0,
5616
+ matchType: best?.matchType ?? "no_match"
5617
+ };
5618
+ });
5619
+ return JSON.stringify({
5620
+ format: document2.format,
5621
+ segments: document2.segments.length,
5622
+ checked: matches.length,
5623
+ threshold,
5624
+ tmEntries: assets.tmEntries.length,
5625
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5626
+ assetsUsed: assets.used,
5627
+ warnings: assets.warnings,
5628
+ matches
5629
+ });
5630
+ }
5631
+ });
5632
+ }
5633
+ function createMatchTermsTool() {
5634
+ return defineTool3({
3391
5635
  name: "l10n_match_terms",
3392
- description: "Match a local XLIFF/SDLXLIFF file against a CSV glossary. Returns term hits and forbidden-term counts (no file modification).",
5636
+ description: "Match a local XLIFF/SDLXLIFF file against a CSV/TBX glossary and/or the enabled terminology assets of the enclosing project. Returns term hits and forbidden-term counts (no file modification).",
3393
5637
  parameters: {
3394
5638
  path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3395
- glossary: { type: "string", required: true, description: "CSV glossary path." },
5639
+ glossary: { type: "string", description: "Optional CSV or TBX-Basic/Minis glossary path; project assets are used too." },
5640
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
3396
5641
  maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
3397
5642
  },
3398
5643
  output: {
@@ -3401,16 +5646,22 @@ function createMatchTermsTool() {
3401
5646
  },
3402
5647
  async execute(args) {
3403
5648
  const xml = await readUtf8(args.path);
3404
- const document = documentFromXml(xml, args.path);
3405
- const glossary = parseGlossaryCsv(await readUtf8(args.glossary));
3406
- if (glossary.entries.length === 0) throw new Error("glossary contains no usable entries");
5649
+ const document2 = documentFromXml(xml, args.path);
5650
+ const assets = await loadDocumentAssets(document2, args.path, {
5651
+ ...args.project === void 0 ? {} : { project: args.project },
5652
+ ...args.glossary === void 0 ? {} : { glossary: args.glossary }
5653
+ });
5654
+ const glossary = assets.glossary;
5655
+ if (glossary.length === 0) {
5656
+ throw new Error("no glossary entries: pass glossary or register TB/CSV assets with l10n_asset_add");
5657
+ }
3407
5658
  const limit = Math.max(1, args.maxSegments ?? 50);
3408
5659
  const hits = [];
3409
5660
  let forbidden = 0;
3410
- for (const segment of document.segments.slice(0, limit)) {
3411
- 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];
3412
5663
  for (const side of ["source", "target"]) {
3413
- const result = matchTerms(segment[`${side}Text`], glossary.entries, {
5664
+ const result = matchTerms(segment[`${side}Text`], glossary, {
3414
5665
  ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
3415
5666
  ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
3416
5667
  });
@@ -3431,10 +5682,13 @@ function createMatchTermsTool() {
3431
5682
  }
3432
5683
  }
3433
5684
  return JSON.stringify({
3434
- format: document.format,
3435
- segments: document.segments.length,
3436
- checked: Math.min(limit, document.segments.length),
3437
- entries: glossary.entries.length,
5685
+ format: document2.format,
5686
+ segments: document2.segments.length,
5687
+ checked: Math.min(limit, document2.segments.length),
5688
+ entries: glossary.length,
5689
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5690
+ assetsUsed: assets.used,
5691
+ warnings: assets.warnings,
3438
5692
  hits: hits.slice(0, 200),
3439
5693
  hitCount: hits.length,
3440
5694
  forbiddenCount: forbidden
@@ -3442,14 +5696,26 @@ function createMatchTermsTool() {
3442
5696
  }
3443
5697
  });
3444
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
+ }
3445
5710
  function createCheckTool() {
3446
- return defineTool({
5711
+ return defineTool3({
3447
5712
  name: "l10n_check",
3448
- 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.",
5713
+ description: "Run deterministic QA on a local XLIFF/SDLXLIFF file, optionally with a TMX/glossary and the enabled assets of the enclosing project. Returns rule/severity counts and a bounded issue list; never modifies files.",
3449
5714
  parameters: {
3450
5715
  path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3451
- tm: { type: "string", description: "Optional TMX 1.4b file path." },
3452
- glossary: { type: "string", description: "Optional CSV glossary path." },
5716
+ tm: { type: "string", description: "Optional TMX 1.4b file path; project TM assets are used too." },
5717
+ glossary: { type: "string", description: "Optional CSV or TBX-Basic/Minis glossary path; project term assets are used too." },
5718
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
3453
5719
  maxIssues: { type: "integer", description: "Max issues returned; default 100." },
3454
5720
  sourceWhitelist: {
3455
5721
  type: "array",
@@ -3462,7 +5728,7 @@ function createCheckTool() {
3462
5728
  },
3463
5729
  targetLocale: {
3464
5730
  type: "string",
3465
- description: "Target locale hint (e.g. zh-CN) used by language-variant rules."
5731
+ description: "Target locale hint (e.g. zh-CN) used by language-variant rules; defaults to project qa.locale."
3466
5732
  }
3467
5733
  },
3468
5734
  output: {
@@ -3471,19 +5737,27 @@ function createCheckTool() {
3471
5737
  },
3472
5738
  async execute(args) {
3473
5739
  const xml = await readUtf8(args.path);
3474
- const document = documentFromXml(xml, args.path);
3475
- const tmEntries = args.tm ? parseTmx(await readUtf8(args.tm)).entries : [];
3476
- const glossary = args.glossary ? parseGlossaryCsv(await readUtf8(args.glossary)).entries : [];
3477
- const checked = checkDocument(document, {
3478
- tmEntries,
3479
- glossary,
3480
- ...args.sourceWhitelist === void 0 && args.shortSegmentMaxLength === void 0 ? {} : {
5740
+ const document2 = documentFromXml(xml, args.path);
5741
+ const assets = await loadDocumentAssets(document2, args.path, {
5742
+ ...args.project === void 0 ? {} : { project: args.project },
5743
+ ...args.tm === void 0 ? {} : { tm: args.tm },
5744
+ ...args.glossary === void 0 ? {} : { glossary: args.glossary }
5745
+ });
5746
+ const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
5747
+ const whitelist = [
5748
+ ...args.sourceWhitelist ?? [],
5749
+ ...defaults?.untranslatedCopyWhitelist ?? []
5750
+ ];
5751
+ const checked = checkDocument(document2, {
5752
+ tmEntries: assets.tmEntries,
5753
+ glossary: assets.glossary,
5754
+ ...whitelist.length === 0 && args.shortSegmentMaxLength === void 0 ? {} : {
3481
5755
  untranslatedCopy: {
3482
- ...args.sourceWhitelist === void 0 ? {} : { whitelist: args.sourceWhitelist },
5756
+ ...whitelist.length === 0 ? {} : { whitelist: Array.from(new Set(whitelist)) },
3483
5757
  ...args.shortSegmentMaxLength === void 0 ? {} : { shortSegmentMaxLength: args.shortSegmentMaxLength }
3484
5758
  }
3485
5759
  },
3486
- ...args.targetLocale === void 0 ? {} : { locale: { targetLocale: args.targetLocale } }
5760
+ ...(args.targetLocale ?? defaults?.locale) === void 0 ? {} : { locale: { targetLocale: args.targetLocale ?? defaults?.locale } }
3487
5761
  });
3488
5762
  const bySeverity = {};
3489
5763
  const byRule = {};
@@ -3493,11 +5767,14 @@ function createCheckTool() {
3493
5767
  }
3494
5768
  const limit = Math.max(1, args.maxIssues ?? 100);
3495
5769
  return JSON.stringify({
3496
- format: document.format,
3497
- segments: document.segments.length,
5770
+ format: document2.format,
5771
+ segments: document2.segments.length,
3498
5772
  checkedSegments: checked.qa.checkedSegments,
3499
5773
  coverage: checked.qa.coverage,
3500
5774
  totalIssues: checked.qa.issues.length,
5775
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5776
+ assetsUsed: assets.used,
5777
+ warnings: assets.warnings,
3501
5778
  bySeverity,
3502
5779
  byRule,
3503
5780
  issues: checked.qa.issues.slice(0, limit).map((issue) => ({
@@ -3515,14 +5792,15 @@ function createCheckTool() {
3515
5792
  });
3516
5793
  }
3517
5794
  function createTranslateTool() {
3518
- return defineTool({
5795
+ return defineTool3({
3519
5796
  name: "l10n_translate",
3520
- 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.",
5797
+ description: "Fill empty targets in a local XLIFF/SDLXLIFF file from a TMX and/or the enabled TM/bilingual project assets, writing a NEW file. Existing targets and tagged segments are never overwritten.",
3521
5798
  parameters: {
3522
5799
  path: { type: "string", required: true, description: "Input XLIFF/SDLXLIFF path." },
3523
- tm: { type: "string", required: true, description: "TMX 1.4b file path." },
5800
+ tm: { type: "string", description: "Optional TMX 1.4b file path; project TM assets are used too." },
5801
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
3524
5802
  output: { type: "string", required: true, description: "New output file path; must differ from input." },
3525
- threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
5803
+ threshold: { type: "integer", description: "Minimum matchRate 50..100; default from project or 75." },
3526
5804
  setSdlConf: { type: "string", description: "SDLXLIFF only: set sdl:seg@conf on filled segments." }
3527
5805
  },
3528
5806
  output: {
@@ -3534,35 +5812,60 @@ function createTranslateTool() {
3534
5812
  applied: { type: "integer" },
3535
5813
  skipped: { type: "integer" },
3536
5814
  warnings: { type: "array", items: { type: "string" } },
3537
- sha256: { type: "string" }
5815
+ sha256: { type: "string" },
5816
+ project: { type: "string" },
5817
+ assetsUsed: {
5818
+ type: "array",
5819
+ items: {
5820
+ type: "object",
5821
+ additionalProperties: false,
5822
+ properties: {
5823
+ id: { type: "string" },
5824
+ kind: { type: "string" },
5825
+ path: { type: "string" },
5826
+ entries: { type: "integer" }
5827
+ }
5828
+ }
5829
+ }
3538
5830
  }
3539
5831
  },
3540
5832
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3541
5833
  },
3542
5834
  async execute(args) {
3543
- const input = resolve(args.path);
3544
- const output = resolve(args.output);
5835
+ const input = resolve2(args.path);
5836
+ const output = resolve2(args.output);
3545
5837
  if (input === output) throw new Error("refusing to overwrite the input file");
3546
5838
  const xml = await readUtf8(input);
3547
- const document = documentFromXml(xml, input);
3548
- const tm = parseTmx(await readUtf8(args.tm));
3549
- const result = translateDocument(document, tm.entries, {
3550
- threshold: args.threshold ?? 75,
5839
+ const document2 = documentFromXml(xml, input);
5840
+ const assets = await loadDocumentAssets(document2, input, {
5841
+ ...args.project === void 0 ? {} : { project: args.project },
5842
+ ...args.tm === void 0 ? {} : { tm: args.tm }
5843
+ });
5844
+ if (assets.tmEntries.length === 0) {
5845
+ throw new Error("no TM entries: pass tm or register TM/bilingual assets with l10n_asset_add");
5846
+ }
5847
+ const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
5848
+ const threshold = args.threshold ?? defaults?.threshold ?? 75;
5849
+ if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
5850
+ const result = translateDocument(document2, assets.tmEntries, {
5851
+ threshold,
3551
5852
  ...args.setSdlConf === void 0 ? {} : { setSdlState: true, sdlConf: args.setSdlConf }
3552
5853
  });
3553
- await writeFile(output, result.xml, "utf8");
5854
+ await writeFile2(output, result.xml, "utf8");
3554
5855
  return {
3555
5856
  output,
3556
5857
  applied: result.applied,
3557
5858
  skipped: result.skipped,
3558
- warnings: result.warnings,
3559
- sha256: createHash2("sha256").update(result.xml, "utf8").digest("hex")
5859
+ warnings: [...assets.warnings, ...result.warnings],
5860
+ sha256: createHash3("sha256").update(result.xml, "utf8").digest("hex"),
5861
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5862
+ assetsUsed: assets.used
3560
5863
  };
3561
5864
  }
3562
5865
  });
3563
5866
  }
3564
5867
  function createConvertTool() {
3565
- return defineTool({
5868
+ return defineTool3({
3566
5869
  name: "l10n_convert",
3567
5870
  description: "Convert a bilingual CSV/TXT/XLSX table between formats. Writes a new file; never modifies the input.",
3568
5871
  parameters: {
@@ -3583,23 +5886,23 @@ function createConvertTool() {
3583
5886
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3584
5887
  },
3585
5888
  async execute(args) {
3586
- const input = resolve(args.input);
3587
- const output = resolve(args.output);
5889
+ const input = resolve2(args.input);
5890
+ const output = resolve2(args.output);
3588
5891
  if (input === output) throw new Error("refusing to overwrite the input file");
3589
5892
  const table = await parseBilingualFileAsync(input, await readBytes(input));
3590
5893
  if (args.to === "xlsx") {
3591
- await writeFile(output, await writeBilingualXlsx(table.rows));
5894
+ await writeFile2(output, await writeBilingualXlsx(table.rows));
3592
5895
  } else if (args.to === "csv") {
3593
- await writeFile(output, writeBilingualCsv(table.rows, { delimiter: "," }), "utf8");
5896
+ await writeFile2(output, writeBilingualCsv(table.rows, { delimiter: "," }), "utf8");
3594
5897
  } else {
3595
- await writeFile(output, writeBilingualCsv(table.rows, { delimiter: " ", header: false }), "utf8");
5898
+ await writeFile2(output, writeBilingualCsv(table.rows, { delimiter: " ", header: false }), "utf8");
3596
5899
  }
3597
5900
  return { output, rows: table.rows.length, warnings: table.warnings };
3598
5901
  }
3599
5902
  });
3600
5903
  }
3601
5904
  function createValidateTool() {
3602
- return defineTool({
5905
+ return defineTool3({
3603
5906
  name: "l10n_validate",
3604
5907
  description: "Validate one local XLIFF 1.2/SDLXLIFF file: XML well-formedness, segment counts, missing targets, inline-tag mismatches. Read-only.",
3605
5908
  parameters: {
@@ -3611,42 +5914,42 @@ function createValidateTool() {
3611
5914
  },
3612
5915
  async execute(args) {
3613
5916
  const xml = await readUtf8(args.path);
3614
- const document = documentFromXml(xml, args.path);
5917
+ const document2 = documentFromXml(xml, args.path);
3615
5918
  let missingTarget = 0;
3616
5919
  let tagMismatch = 0;
3617
- for (const segment of document.segments) {
5920
+ for (const segment of document2.segments) {
3618
5921
  if (!segment.targetRange) missingTarget += 1;
3619
5922
  if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
3620
5923
  }
3621
5924
  return JSON.stringify({
3622
5925
  valid: true,
3623
- format: document.format,
3624
- segments: document.segments.length,
5926
+ format: document2.format,
5927
+ segments: document2.segments.length,
3625
5928
  missingTarget,
3626
5929
  tagMismatch,
3627
- warnings: document.warnings
5930
+ warnings: document2.warnings
3628
5931
  });
3629
5932
  }
3630
5933
  });
3631
5934
  }
3632
5935
 
3633
5936
  // packages/dsh-l10n/src/tools-phase2.ts
3634
- import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
3635
- import { basename as basename2, resolve as resolve2 } from "node:path";
3636
- import { defineTool as defineTool2 } from "@deepseek-ai/dsh-tools";
5937
+ import { readFile as readFile3, stat as stat3, writeFile as writeFile3 } from "node:fs/promises";
5938
+ import { basename as basename2, resolve as resolve3 } from "node:path";
5939
+ import { defineTool as defineTool4 } from "@deepseek-ai/dsh-tools";
3637
5940
  var MAX_BYTES2 = 200 * 1024 * 1024;
3638
5941
  async function readBytes2(path) {
3639
- const absolute = resolve2(path);
3640
- const info = await stat2(absolute);
5942
+ const absolute = resolve3(path);
5943
+ const info = await stat3(absolute);
3641
5944
  if (!info.isFile()) throw new Error("path is not a regular file");
3642
5945
  if (info.size > MAX_BYTES2) throw new Error(`file exceeds ${MAX_BYTES2} bytes`);
3643
- return readFile2(absolute);
5946
+ return readFile3(absolute);
3644
5947
  }
3645
5948
  async function readText(path) {
3646
5949
  return (await readBytes2(path)).toString("utf8");
3647
5950
  }
3648
5951
  function createExtractDocTool() {
3649
- return defineTool2({
5952
+ return defineTool4({
3650
5953
  name: "l10n_extract_doc",
3651
5954
  description: "Extract local DOCX paragraph text, XLSX text cells or PPTX slide text into a bilingual table plus a map.json. Writes new files; the original is never modified.",
3652
5955
  parameters: {
@@ -3670,18 +5973,18 @@ function createExtractDocTool() {
3670
5973
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3671
5974
  },
3672
5975
  async execute(args) {
3673
- const input = resolve2(args.path);
3674
- const rowsOutput = resolve2(args.rowsOutput);
3675
- const mapOutput = resolve2(args.mapOutput);
5976
+ const input = resolve3(args.path);
5977
+ const rowsOutput = resolve3(args.rowsOutput);
5978
+ const mapOutput = resolve3(args.mapOutput);
3676
5979
  if (input === rowsOutput || input === mapOutput) throw new Error("refusing to overwrite the input file");
3677
5980
  const result = extractOoxml(await readBytes2(input), basename2(input));
3678
5981
  const format = args.format ?? (rowsOutput.toLowerCase().endsWith(".xlsx") ? "xlsx" : "csv");
3679
5982
  if (format === "xlsx") {
3680
- await writeFile2(rowsOutput, await writeBilingualXlsx(result.rows));
5983
+ await writeFile3(rowsOutput, await writeBilingualXlsx(result.rows));
3681
5984
  } else {
3682
- await writeFile2(rowsOutput, writeBilingualCsv(result.rows, { delimiter: "," }), "utf8");
5985
+ await writeFile3(rowsOutput, writeBilingualCsv(result.rows, { delimiter: "," }), "utf8");
3683
5986
  }
3684
- await writeFile2(mapOutput, JSON.stringify(result.map, null, 2), "utf8");
5987
+ await writeFile3(mapOutput, JSON.stringify(result.map, null, 2), "utf8");
3685
5988
  return {
3686
5989
  format: result.format,
3687
5990
  rows: result.rows.length,
@@ -3693,7 +5996,7 @@ function createExtractDocTool() {
3693
5996
  });
3694
5997
  }
3695
5998
  function createBackfillTool() {
3696
- return defineTool2({
5999
+ return defineTool4({
3697
6000
  name: "l10n_backfill",
3698
6001
  description: "Backfill translated bilingual rows into a NEW DOCX/XLSX/PPTX file using a previously generated map.json. The original is never modified.",
3699
6002
  parameters: {
@@ -3716,8 +6019,8 @@ function createBackfillTool() {
3716
6019
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3717
6020
  },
3718
6021
  async execute(args) {
3719
- const original = resolve2(args.original);
3720
- const output = resolve2(args.output);
6022
+ const original = resolve3(args.original);
6023
+ const output = resolve3(args.output);
3721
6024
  if (original === output) throw new Error("refusing to overwrite the input file");
3722
6025
  const map = JSON.parse(await readText(args.map));
3723
6026
  if (map.format !== "docx" && map.format !== "xlsx" && map.format !== "pptx") throw new Error("invalid map.json");
@@ -3726,7 +6029,7 @@ function createBackfillTool() {
3726
6029
  table.rows.filter((row) => row.target.trim() !== "").map((row) => [row.id, row.target])
3727
6030
  );
3728
6031
  const result = backfillOoxml(await readBytes2(original), map, translations);
3729
- await writeFile2(output, result.buffer);
6032
+ await writeFile3(output, result.buffer);
3730
6033
  return {
3731
6034
  output,
3732
6035
  applied: result.applied,
@@ -3737,7 +6040,7 @@ function createBackfillTool() {
3737
6040
  });
3738
6041
  }
3739
6042
  function createPolishTool() {
3740
- return defineTool2({
6043
+ return defineTool4({
3741
6044
  name: "l10n_polish",
3742
6045
  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.",
3743
6046
  parameters: {
@@ -3775,8 +6078,8 @@ function createPolishTool() {
3775
6078
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3776
6079
  },
3777
6080
  async execute(args) {
3778
- const input = resolve2(args.path);
3779
- const output = resolve2(args.output);
6081
+ const input = resolve3(args.path);
6082
+ const output = resolve3(args.output);
3780
6083
  if (input === output) throw new Error("refusing to overwrite the input file");
3781
6084
  const parsed = JSON.parse(args.edits);
3782
6085
  const edits = /* @__PURE__ */ new Map();
@@ -3788,12 +6091,12 @@ function createPolishTool() {
3788
6091
  });
3789
6092
  }
3790
6093
  const xml = await readText(input);
3791
- const document = parseXliff(xml, { filename: input });
3792
- const result = polishDocument(document, edits);
6094
+ const document2 = parseXliff(xml, { filename: input });
6095
+ const result = polishDocument(document2, edits);
3793
6096
  if (result.applied === 0 && result.warnings.length > 0) {
3794
6097
  throw new Error(result.warnings.join("; "));
3795
6098
  }
3796
- await writeFile2(output, result.xml, "utf8");
6099
+ await writeFile3(output, result.xml, "utf8");
3797
6100
  return {
3798
6101
  output,
3799
6102
  applied: result.applied,
@@ -3806,7 +6109,7 @@ function createPolishTool() {
3806
6109
  }
3807
6110
 
3808
6111
  // packages/dsh-l10n/src/tools-assets.ts
3809
- import { defineTool as defineTool3 } from "@deepseek-ai/dsh-tools";
6112
+ import { defineTool as defineTool5 } from "@deepseek-ai/dsh-tools";
3810
6113
  function isPrivateOrReservedHost(hostname) {
3811
6114
  const host = hostname.toLowerCase().replace(/^\[/u, "").replace(/\]$/u, "");
3812
6115
  if (host === "localhost" || host.endsWith(".localhost") || host === "::1" || host === "::") return true;
@@ -3859,7 +6162,7 @@ function resolveAssetEndpoint(name2) {
3859
6162
  return new URL(raw);
3860
6163
  }
3861
6164
  function createAssetInfoTool(baseUrl) {
3862
- return defineTool3({
6165
+ return defineTool5({
3863
6166
  name: "l10n_assets",
3864
6167
  description: "Read-only company localization asset service: health, glossary entries, or TM entries. Targets the prod endpoint by default.",
3865
6168
  parameters: {
@@ -3873,7 +6176,10 @@ function createAssetInfoTool(baseUrl) {
3873
6176
  async execute(args) {
3874
6177
  if (!baseUrl) throw new Error("asset endpoint is not configured");
3875
6178
  const path = args.view === "health" ? "health" : args.view === "glossary" ? "glossary" : "tm";
3876
- const response = await fetch(new URL(path, baseUrl), { signal: AbortSignal.timeout(15e3) });
6179
+ const response = await fetch(new URL(path, baseUrl), {
6180
+ signal: AbortSignal.timeout(15e3),
6181
+ redirect: "error"
6182
+ });
3877
6183
  if (!response.ok) throw new Error(`asset service returned ${response.status}`);
3878
6184
  const value = await response.json();
3879
6185
  if (Array.isArray(value.entries) && args.view !== "health") {
@@ -3886,9 +6192,9 @@ function createAssetInfoTool(baseUrl) {
3886
6192
  }
3887
6193
 
3888
6194
  // packages/dsh-l10n/src/tools-semantic.ts
3889
- import { readFile as readFile3 } from "node:fs/promises";
3890
- import { basename as basename3, resolve as resolve3 } from "node:path";
3891
- import { defineTool as defineTool4 } from "@deepseek-ai/dsh-tools";
6195
+ import { readFile as readFile4 } from "node:fs/promises";
6196
+ import { basename as basename3, resolve as resolve4 } from "node:path";
6197
+ import { defineTool as defineTool6 } from "@deepseek-ai/dsh-tools";
3892
6198
  function createDshSemanticProvider(llm, provider, model) {
3893
6199
  return {
3894
6200
  async complete({ prompt }) {
@@ -3908,7 +6214,7 @@ function createDshSemanticProvider(llm, provider, model) {
3908
6214
  };
3909
6215
  }
3910
6216
  function createSemanticCheckTool(llm) {
3911
- return defineTool4({
6217
+ return defineTool6({
3912
6218
  name: "l10n_semantic_check",
3913
6219
  description: "Run semantic localization QA through the dsh LLM service. Requires a configured provider/model; returns validated issues only (evidence must be uniquely locatable).",
3914
6220
  parameters: {
@@ -3923,10 +6229,10 @@ function createSemanticCheckTool(llm) {
3923
6229
  },
3924
6230
  async execute(args) {
3925
6231
  if (!llm) throw new Error("dsh LLM service is unavailable; configure a model provider first");
3926
- const xml = await readFile3(resolve3(args.path), "utf8");
3927
- const document = parseXliff(xml, { filename: basename3(args.path) });
6232
+ const xml = await readFile4(resolve4(args.path), "utf8");
6233
+ const document2 = parseXliff(xml, { filename: basename3(args.path) });
3928
6234
  const result = await runSemanticCheck(
3929
- document,
6235
+ document2,
3930
6236
  createDshSemanticProvider(llm, args.provider, args.model),
3931
6237
  { batchSize: args.batchSize ?? 20 }
3932
6238
  );
@@ -3943,24 +6249,24 @@ function createSemanticCheckTool(llm) {
3943
6249
  }
3944
6250
 
3945
6251
  // packages/dsh-l10n/src/tool.ts
3946
- import { readFile as readFile4, stat as stat3 } from "node:fs/promises";
3947
- import { basename as basename4, resolve as resolve4 } from "node:path";
3948
- import { defineTool as defineTool5 } from "@deepseek-ai/dsh-tools";
6252
+ import { readFile as readFile5, stat as stat4 } from "node:fs/promises";
6253
+ import { basename as basename4, resolve as resolve5 } from "node:path";
6254
+ import { defineTool as defineTool7 } from "@deepseek-ai/dsh-tools";
3949
6255
  var MAX_BYTES3 = 200 * 1024 * 1024;
3950
6256
  async function inspectFile(path, signal) {
3951
6257
  signal?.throwIfAborted();
3952
- const absolute = resolve4(path);
3953
- const info = await stat3(absolute);
6258
+ const absolute = resolve5(path);
6259
+ const info = await stat4(absolute);
3954
6260
  if (!info.isFile()) throw new Error("path is not a regular file");
3955
6261
  if (info.size > MAX_BYTES3) throw new Error(`file exceeds ${MAX_BYTES3} bytes`);
3956
- const xml = await readFile4(absolute, { encoding: "utf8", signal });
6262
+ const xml = await readFile5(absolute, { encoding: "utf8", signal });
3957
6263
  if (xml.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
3958
6264
  signal?.throwIfAborted();
3959
- const document = parseXliff(xml, { filename: basename4(absolute) });
6265
+ const document2 = parseXliff(xml, { filename: basename4(absolute) });
3960
6266
  const targetTagDistribution = {};
3961
6267
  let missingTarget = 0;
3962
6268
  let tagMismatch = 0;
3963
- for (const segment of document.segments) {
6269
+ for (const segment of document2.segments) {
3964
6270
  const key = String(segment.targetTags.length);
3965
6271
  targetTagDistribution[key] = (targetTagDistribution[key] ?? 0) + 1;
3966
6272
  if (!segment.targetRange) missingTarget += 1;
@@ -3968,24 +6274,24 @@ async function inspectFile(path, signal) {
3968
6274
  }
3969
6275
  const structure = structureSummary(xml);
3970
6276
  return {
3971
- format: document.format,
3972
- version: document.version,
6277
+ format: document2.format,
6278
+ version: document2.version,
3973
6279
  bytes: Buffer.byteLength(xml, "utf8"),
3974
- files: document.files.length,
3975
- languages: document.files.map(
6280
+ files: document2.files.length,
6281
+ languages: document2.files.map(
3976
6282
  (file) => `${file.sourceLanguage}->${file.targetLanguage}`
3977
6283
  ),
3978
- segments: document.segments.length,
6284
+ segments: document2.segments.length,
3979
6285
  missingTarget,
3980
6286
  tagMismatch,
3981
- warnings: document.warnings.length,
6287
+ warnings: document2.warnings.length,
3982
6288
  targetTagDistribution,
3983
6289
  structureElements: structure.count,
3984
6290
  structureHash: structure.hash.slice(0, 16)
3985
6291
  };
3986
6292
  }
3987
6293
  function createInspectTool() {
3988
- return defineTool5({
6294
+ return defineTool7({
3989
6295
  name: "l10n_inspect",
3990
6296
  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.",
3991
6297
  parameters: {
@@ -4026,13 +6332,24 @@ function createInspectTool() {
4026
6332
  var name = "langlink-l10n";
4027
6333
  var inject = ["tools"];
4028
6334
  function apply(ctx) {
6335
+ const baseUrl = process.env.WORKBENCH_PLATFORM_URL, token = process.env.WORKBENCH_PLATFORM_TOKEN;
6336
+ if (baseUrl || token) {
6337
+ if (!baseUrl || !token) throw new Error("platform mode requires both URL and Token");
6338
+ ctx.tools.register(createPlatformJobTool({ baseUrl, token }));
6339
+ }
4029
6340
  const llm = ctx.get?.("llm");
4030
6341
  const assetRoot = () => resolveAssetEndpoint("prod");
4031
6342
  for (const tool of [
6343
+ createProjectInitTool(),
6344
+ createProjectStatusTool(),
6345
+ createAssetAddTool(),
6346
+ createAssetUpdateTool(),
6347
+ createAssetRemoveTool(),
4032
6348
  createInspectTool(),
4033
6349
  createMatchTmTool(),
4034
6350
  createMatchTermsTool(),
4035
6351
  createCheckTool(),
6352
+ createQaReviewTool(),
4036
6353
  createTranslateTool(),
4037
6354
  createConvertTool(),
4038
6355
  createValidateTool(),
@@ -4046,9 +6363,15 @@ function apply(ctx) {
4046
6363
  }
4047
6364
  }
4048
6365
  export {
6366
+ addAsset,
4049
6367
  apply,
4050
6368
  assertSafeAssetBaseUrl,
6369
+ assetAbsolutePath,
6370
+ collectProjectAssets,
6371
+ createAssetAddTool,
4051
6372
  createAssetInfoTool,
6373
+ createAssetRemoveTool,
6374
+ createAssetUpdateTool,
4052
6375
  createBackfillTool,
4053
6376
  createCheckTool,
4054
6377
  createConvertTool,
@@ -4057,13 +6380,27 @@ export {
4057
6380
  createInspectTool,
4058
6381
  createMatchTermsTool,
4059
6382
  createMatchTmTool,
6383
+ createPlatformJobTool,
4060
6384
  createPolishTool,
6385
+ createProjectInitTool,
6386
+ createProjectStatusTool,
4061
6387
  createSemanticCheckTool,
4062
6388
  createTranslateTool,
4063
6389
  createValidateTool,
6390
+ findProject,
6391
+ initProject,
4064
6392
  inject,
4065
6393
  inspectFile,
4066
6394
  name,
4067
- resolveAssetEndpoint
6395
+ optionalProject,
6396
+ parseAssetFile,
6397
+ projectDefaults,
6398
+ projectStatus,
6399
+ readManifest,
6400
+ removeAsset,
6401
+ requireProject,
6402
+ resolveAssetEndpoint,
6403
+ updateAsset,
6404
+ writeManifest
4068
6405
  };
4069
6406
  //# sourceMappingURL=index.js.map