@langlink/dsh-l10n 0.1.0 → 0.2.1

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 json(method, endpoint, body, request) {
65
+ return await (await response(method, endpoint, body, request)).json();
66
+ }
67
+ return {
68
+ qaRules: () => json("GET", path("qa-rules")),
69
+ configurations: (resource, offset = 0) => json("GET", `${path(resource)}?offset=${offset}`),
70
+ configurationVersion: (resource, id, version) => json("GET", `${path(resource, id)}?version=${version}`),
71
+ documentConfiguration: (id) => json("GET", path("documents", id, "configuration")),
72
+ setDocumentConfiguration: (id, input) => json("PATCH", path("documents", id, "configuration"), input),
73
+ me: () => json("GET", path("me")),
74
+ plugins: () => json("GET", path("plugins")),
75
+ models: () => json("GET", path("models")),
76
+ createDocument: (input, request) => json("POST", path("documents"), input, request),
77
+ documents: (offset = 0, limit = 50) => json("GET", `${path("documents")}?offset=${offset}&limit=${limit}`),
78
+ documentChanges: (id, after) => json("GET", `${path("documents", id, "changes")}?after=${encodeURIComponent(after)}`),
79
+ document: (id, offset = 0, limit = 50, request) => json("GET", `${path("documents", id)}?segments.offset=${offset}&segments.limit=${limit}`, void 0, request),
80
+ segment: (id, segmentId) => json("GET", path("documents", id, "segments", segmentId)),
81
+ edit: (id, segmentId, input, request) => json("PATCH", path("documents", id, "segments", segmentId), input, request),
82
+ attachAssets: (id, input, request) => json("POST", path("documents", id, "assets"), input, request),
83
+ segmentAssets: (id, segmentId) => json("GET", path("documents", id, "segments", segmentId, "assets")),
84
+ comments: (id, segmentId) => json("GET", path("documents", id, "comments") + (segmentId ? `?segmentId=${encodeURIComponent(segmentId)}` : "")),
85
+ addComment: (id, segmentId, body) => json("POST", path("documents", id, "comments"), { segmentId, body }),
86
+ setStyle: (id, style, customStyleInstruction) => json("PATCH", path("documents", id, "style"), { style, ...customStyleInstruction === void 0 ? {} : { customStyleInstruction } }),
87
+ setModels: (id, input) => json("PATCH", path("documents", id, "models"), input),
88
+ createJob: (id, input, request) => json("POST", path("documents", id, "jobs"), input, request),
89
+ job: (id, request) => json("GET", path("jobs", id), void 0, request),
90
+ cancelJob: (id) => json("POST", path("jobs", id, "cancel"), {}),
91
+ retryJob: (id, request) => json("POST", path("jobs", id, "retry"), {}, request),
92
+ undoJob: (id) => json("POST", path("jobs", id, "undo"), {}),
93
+ jobResults: (id) => json("GET", path("jobs", id, "results")),
94
+ updateSuggestion: (id, segmentId, input) => json("PATCH", path("documents", id, "segments", segmentId, "suggestion"), input),
95
+ discardSuggestion: (id, segmentId, input) => json("DELETE", path("documents", id, "segments", segmentId, "suggestion"), input),
96
+ applySuggestion: (id, segmentId, input) => json("POST", path("documents", id, "segments", segmentId, "apply-suggestion"), input),
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 json("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,182 @@ 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
+ const hintFor = (code) => {
483
+ if ((code.node.namespaceUri ?? "") !== (old.root.namespaceUri ?? "")) return void 0;
484
+ const matches = sourceCodes.filter((item) => codeIdentity(item.node, false) === codeIdentity(code.node, false));
485
+ const sourceHint = matches.find((item) => ["no", "firstNo"].includes(item.node.attribs.canReorder ?? ""))?.node.attribs.canReorder;
486
+ if (sourceHint && matches.length !== 1) throw new Error("ambiguous source editing hints require original code order");
487
+ return code.node.attribs.canReorder === "no" || sourceHint === "no" ? "no" : code.node.attribs.canReorder === "firstNo" || sourceHint === "firstNo" ? "firstNo" : void 0;
488
+ };
489
+ const position = new Map(order.map((value, i) => [value, i]));
490
+ const stack = [], pairs = [];
491
+ let fixedOrder = false;
492
+ for (let i = 0; i < original.length; i++) {
493
+ const code = original[i], node = code.node;
494
+ if (code.close || (node.namespaceUri ?? "") !== (old.root.namespaceUri ?? "")) continue;
495
+ const name2 = node.local;
496
+ if (node.attribs.isolated === "yes" || node.attribs.isolated === "true") fixedOrder = true;
497
+ if (["bpt", "bx", "sc", "sm"].includes(name2)) stack.push(i);
498
+ if (["ept", "ex", "ec", "em"].includes(name2)) {
499
+ const start = stack.pop(), open = start === void 0 ? void 0 : original[start].node;
500
+ const opener = { ept: "bpt", ex: "bx", ec: "sc", em: "sm" }[name2];
501
+ 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);
502
+ if (!matches || start === void 0) fixedOrder = true;
503
+ else pairs.push([start, i]);
504
+ }
505
+ }
506
+ if (stack.length) fixedOrder = true;
507
+ if (fixedOrder) throw new Error("isolated, overlapping or incomplete codes must retain their original order");
508
+ const active = [];
509
+ const ends = new Map(pairs.map(([start, end]) => [end, start]));
510
+ const starts = new Set(pairs.map(([start]) => start));
511
+ for (const index of order) {
512
+ if (starts.has(index)) active.push(index);
513
+ if (ends.has(index) && active.pop() !== ends.get(index)) throw new Error("inline code pairs are crossed or reversed");
514
+ }
515
+ const logical = original.map((code, index) => ({ code, index })).filter(({ code }) => !code.close);
516
+ let group = [];
517
+ const groups = [];
518
+ for (const { code, index } of logical) {
519
+ const hint = hintFor(code);
520
+ if (hint === "firstNo") {
521
+ group = [index];
522
+ groups.push(group);
523
+ } else if (hint === "no") {
524
+ if (!group.length) throw new Error("non-reorderable codes must retain their original order");
525
+ group.push(index);
526
+ } else group = [];
527
+ }
528
+ const logicalOrder = order.filter((index) => !original[index].close);
529
+ for (const members of groups) {
530
+ const start = logicalOrder.indexOf(members[0]);
531
+ if (members.some((index, offset) => logicalOrder[start + offset] !== index)) throw new Error("non-reorderable code sequence was split or reordered");
532
+ }
533
+ for (const { code, index } of logical) {
534
+ if (hintFor(code) === "no" && ["g", "pc", "mrk"].includes(code.node.local)) {
535
+ if (position.get(index) !== index) throw new Error("non-reorderable container moved");
536
+ }
537
+ }
538
+ }
539
+
191
540
  // packages/l10n-core/src/formats/xliff12.ts
541
+ var SDL_NAMESPACE = "http://sdl.com/FileTypes/SdlXliff/1.0";
192
542
  var SDL_DATATYPE = "x-sdlfilterframework2";
193
543
  function attr(node, name2) {
194
544
  return node.attribs[name2];
@@ -196,15 +546,15 @@ function attr(node, name2) {
196
546
  function isSegMrk(node) {
197
547
  return node.local === "mrk" && node.attribs.mtype === "seg";
198
548
  }
199
- function sdlElement(segDefs) {
200
- if (!segDefs) return void 0;
201
- return elementChildren(segDefs).find((child) => child.local === "seg");
549
+ function sdlElement(segDefs, mid) {
550
+ const nodes = segDefs ? elementChildren(segDefs).filter((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) : [];
551
+ const matches = mid === void 0 ? nodes : nodes.filter((node) => node.attribs.id === mid);
552
+ return matches.length === 1 ? matches[0] : void 0;
202
553
  }
203
- function sdlState(segDefs) {
204
- const seg = sdlElement(segDefs);
554
+ function sdlState(seg) {
205
555
  if (!seg) return void 0;
206
556
  const state = {};
207
- const mapping = [
557
+ const mapping2 = [
208
558
  ["id", "id"],
209
559
  ["conf", "conf"],
210
560
  ["origin", "origin"],
@@ -212,144 +562,158 @@ function sdlState(segDefs) {
212
562
  ["textMatch", "text-match"],
213
563
  ["locked", "locked"]
214
564
  ];
215
- for (const [key, source] of mapping) {
565
+ for (const [key, source] of mapping2) {
216
566
  const value = seg.attribs[source];
217
567
  if (value !== void 0) state[key] = value;
218
568
  }
219
569
  return Object.keys(state).length > 0 ? state : void 0;
220
570
  }
221
- function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
571
+ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
222
572
  const sourceTags = sourceContainer ? elementSignature(sourceContainer) : [];
223
573
  const targetTags = targetContainer ? elementSignature(targetContainer) : [];
224
574
  const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
225
575
  if (!targetContainer) warnings.push(`${id}: target missing`);
226
- if (targetContainer && sourceTags.length > 0 && sourceTags.join("|") !== targetTags.join("|")) {
576
+ if (sourceContainer && targetContainer && JSON.stringify(inlineCodeSignatures(sourceContainer).sort()) !== JSON.stringify(inlineCodeSignatures(targetContainer).sort())) {
227
577
  warnings.push(`${id}: source/target inline tag signature differs`);
228
578
  }
229
579
  return {
230
580
  id,
231
- index: segmentIndex,
581
+ index: ordinal,
232
582
  location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
233
583
  sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
234
584
  targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
235
- sourceText: sourceContainer ? textOf(sourceContainer) : "",
236
- targetText: targetContainer ? textOf(targetContainer) : "",
585
+ sourceText: sourceContainer ? inlineText(sourceContainer) : "",
586
+ targetText: targetContainer ? inlineText(targetContainer) : "",
237
587
  sourceTags,
238
588
  targetTags,
239
589
  targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
590
+ ...sourceContainer === void 0 ? {} : { sourceNode: sourceContainer },
240
591
  ...targetContainer === void 0 ? {} : { targetNode: targetContainer },
241
592
  ...sdlNode === void 0 ? {} : { sdlNode },
242
593
  ...sdl === void 0 ? {} : { sdl }
243
594
  };
244
595
  }
245
596
  function parseXliff(xml, options = {}) {
246
- validateXml(xml);
597
+ validateXml(xml, true);
247
598
  const root = elementChildren(parseXml(xml))[0];
248
- if (!root) throw new Error("No root element");
249
- if (root.local !== "xliff") throw new Error(`Not an XLIFF document: <${root.name}>`);
599
+ if (!root || root.local !== "xliff") throw new Error("Not an XLIFF document");
250
600
  const version = attr(root, "version") ?? "";
251
601
  const isXliff2 = version === "2.0" || version === "2.1";
252
- if (version !== "1.2" && !isXliff2) {
253
- throw new Error(
254
- `Unsupported XLIFF version: ${version || "(missing)"}; supported: 1.2 (SDLXLIFF/MQXLIFF), 2.0 and 2.1`
255
- );
256
- }
257
- const files = [];
258
- const segments = [];
259
- const warnings = [];
260
- const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
261
- const allElements = descendants(root);
262
- const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
263
- const fileNodes = allElements.filter((element) => element.local === "file");
602
+ if (version !== "1.2" && !isXliff2) throw new Error("Unsupported XLIFF version: " + (version || "(missing)") + "; supported: 1.2, 2.0 and 2.1");
603
+ const namespace = root.namespaceUri ?? "";
604
+ const expected = isXliff2 ? "urn:oasis:names:tc:xliff:document:2.0" : "urn:oasis:names:tc:xliff:document:1.2";
605
+ if (namespace !== "" && namespace !== expected) throw new Error("Unsupported XLIFF namespace: " + namespace);
606
+ const core = (node, local) => node.local === local && (node.namespaceUri ?? "") === namespace;
607
+ const children = (node, local) => elementChildren(node).filter((child) => core(child, local));
608
+ const files = [], segments = [], warnings = [];
264
609
  const filename = options.filename?.toLowerCase() ?? "";
265
- let format = version === "2.1" ? "xliff21" : version === "2.0" ? "xliff20" : filename.endsWith(".sdlxliff") ? "sdlxliff" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdlElement ? "sdlxliff" : "xliff12";
266
- fileNodes.forEach((fileNode, fileIndex) => {
610
+ const hasSdl = descendants(root).some((node) => node.namespaceUri === SDL_NAMESPACE);
611
+ let format = version === "2.1" ? "xliff21" : isXliff2 ? "xliff20" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdl || filename.endsWith(".sdlxliff") ? "sdlxliff" : "xliff12";
612
+ const ids = /* @__PURE__ */ new Map();
613
+ children(root, "file").forEach((fileNode, fileIndex) => {
267
614
  const datatype = attr(fileNode, "datatype");
268
- if (datatype === SDL_DATATYPE) format = "sdlxliff";
269
- const file = {
615
+ if (!isXliff2 && datatype === SDL_DATATYPE) format = "sdlxliff";
616
+ files.push({
270
617
  index: fileIndex,
271
- sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(fileNode, "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
272
- targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(fileNode, "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
618
+ sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
619
+ targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
273
620
  ...datatype === void 0 ? {} : { datatype }
274
- };
275
- files.push(file);
276
- const units = descendants(
277
- fileNode,
278
- (candidate) => candidate.local === "trans-unit" || candidate.local === "unit"
279
- );
280
- units.forEach((unit, unitIndex) => {
281
- const unitId = attr(unit, "id") ?? `unit-${unitIndex}`;
282
- if (isXliff2) {
283
- const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
284
- if (segmentNodes.length === 0) {
285
- warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
286
- return;
621
+ });
622
+ let unitIndex = 0;
623
+ const inherited = (node, parent) => node.attribs.translate === "no" ? false : node.attribs.translate === "yes" ? true : parent;
624
+ const walk = (parent, translate) => {
625
+ for (const unit of elementChildren(parent)) {
626
+ if (core(unit, "group") || core(unit, "body")) {
627
+ walk(unit, core(unit, "group") ? inherited(unit, translate) : translate);
628
+ continue;
287
629
  }
288
- segmentNodes.forEach((segmentNode, segmentIndex) => {
289
- const segmentChildren = elementChildren(segmentNode);
290
- const source = segmentChildren.find((child) => child.local === "source");
291
- const target = segmentChildren.find((child) => child.local === "target");
292
- const segmentId = attr(segmentNode, "id") ?? `seg-${segmentIndex}`;
293
- segments.push(
294
- makeSegment(
295
- fileIndex,
296
- unitId,
297
- segmentIndex,
298
- source,
299
- target,
300
- segmentId,
301
- void 0,
302
- void 0,
303
- xml,
304
- warnings
305
- )
306
- );
307
- });
308
- return;
309
- }
310
- const children = elementChildren(unit);
311
- const sourceElement = children.find((child) => child.local === "source");
312
- const targetElement = children.find((child) => child.local === "target");
313
- const segSourceElement = children.find((child) => child.local === "seg-source");
314
- const segDefsElement = children.find((child) => child.local === "seg-defs");
315
- const sdl = sdlState(segDefsElement);
316
- const sdlNode = sdlElement(segDefsElement);
317
- if (segSourceElement) {
318
- const sourceMrks = descendants(segSourceElement, isSegMrk);
319
- const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
320
- sourceMrks.forEach((sourceMrk, segmentIndex) => {
321
- const mid = attr(sourceMrk, "mid");
322
- const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
323
- if (targetMrk === void 0 && targetMrks.length > 0) {
324
- warnings.push(`${fileIndex}:${unitId}:${mid ?? segmentIndex}: target mrk missing`);
630
+ if (!core(unit, isXliff2 ? "unit" : "trans-unit")) continue;
631
+ const unitId = attr(unit, "id") ?? "unit-" + unitIndex;
632
+ unitIndex += 1;
633
+ const mayTranslate = inherited(unit, translate);
634
+ const data = /* @__PURE__ */ Object.create(null);
635
+ const duplicateData = /* @__PURE__ */ new Set();
636
+ for (const original of children(unit, "originalData")) for (const node of children(original, "data")) {
637
+ const id = node.attribs.id;
638
+ if (!id) continue;
639
+ if (Object.hasOwn(data, id)) duplicateData.add(id);
640
+ else data[id] = textOf(node);
641
+ }
642
+ for (const id of duplicateData) {
643
+ delete data[id];
644
+ warnings.push(fileIndex + ":" + unitId + ": ambiguous originalData reference " + id);
645
+ }
646
+ const segDefs = elementChildren(unit).find((node) => node.local === "seg-defs" && node.namespaceUri === SDL_NAMESPACE);
647
+ const push = (source2, target2, mid, index, reason) => {
648
+ const sdlNode = sdlElement(segDefs, mid), sdl = sdlState(sdlNode);
649
+ const segment = makeSegment(fileIndex, unitId, index, segments.length, source2, target2, mid, sdl, sdlNode, xml, warnings);
650
+ if (Object.keys(data).length) segment.originalData = data;
651
+ const markup = target2 && /<!\[CDATA\[|<!--|<\?/.test(segment.targetInner);
652
+ const ambiguousSdl = segDefs && elementChildren(segDefs).some((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) && !sdlNode;
653
+ 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);
654
+ if (readOnly) {
655
+ segment.readOnlyReason = readOnly;
656
+ warnings.push(segment.id + ": read-only: " + readOnly);
325
657
  }
326
- segments.push(
327
- makeSegment(
328
- fileIndex,
329
- unitId,
330
- segmentIndex,
331
- sourceMrk,
332
- targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
333
- mid,
334
- sdl,
335
- sdlNode,
336
- xml,
337
- warnings
338
- )
339
- );
340
- });
341
- return;
342
- }
343
- if (sourceElement || targetElement) {
344
- segments.push(
345
- makeSegment(fileIndex, unitId, 0, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
658
+ const existing = ids.get(segment.id);
659
+ if (existing) {
660
+ existing.readOnlyReason = "ambiguous segment identity";
661
+ segment.readOnlyReason = "ambiguous segment identity";
662
+ do {
663
+ segment.id += ":duplicate:" + segment.index;
664
+ } while (ids.has(segment.id));
665
+ warnings.push(existing.id + ": ambiguous segment identity; read-only");
666
+ }
667
+ ids.set(segment.id, segment);
668
+ segments.push(segment);
669
+ };
670
+ if (isXliff2) {
671
+ const nodes = children(unit, "segment");
672
+ if (!nodes.length) warnings.push(fileIndex + ":" + unitId + ": unit has no supported segment");
673
+ nodes.forEach((node, index) => push(
674
+ children(node, "source")[0],
675
+ children(node, "target")[0],
676
+ attr(node, "id") ?? "seg-" + index,
677
+ index,
678
+ children(node, "source").length > 1 || children(node, "target").length > 1 ? "ambiguous source/target containers" : void 0
679
+ ));
680
+ continue;
681
+ }
682
+ const source = children(unit, "source")[0], target = children(unit, "target")[0], segSource = children(unit, "seg-source")[0];
683
+ const sourceMrks = segSource ? descendants(segSource, (node) => core(node, "mrk") && isSegMrk(node)) : [];
684
+ if (sourceMrks.length) {
685
+ const targetMrks = target ? descendants(target, (node) => core(node, "mrk") && isSegMrk(node)) : [];
686
+ sourceMrks.forEach((mrk, index) => {
687
+ const mid = attr(mrk, "mid");
688
+ const sourceMatches = sourceMrks.filter((node) => node.attribs.mid === mid);
689
+ const matches = mid ? targetMrks.filter((node) => node.attribs.mid === mid) : [];
690
+ const whole = sourceMrks.length === 1 && !targetMrks.length ? target : void 0;
691
+ const unique = mid !== void 0 && sourceMatches.length === 1 && matches.length === 1;
692
+ const mapped = unique ? matches[0] : whole;
693
+ push(mrk, mapped, mid, index, !mapped || sourceMrks.length > 1 && (!mid || sourceMatches.length !== 1) ? "segmented target cannot be mapped uniquely by mid" : void 0);
694
+ });
695
+ } else if (source || target || segSource) push(
696
+ source ?? segSource,
697
+ target,
698
+ void 0,
699
+ 0,
700
+ children(unit, "source").length > 1 || children(unit, "target").length > 1 ? "ambiguous source/target containers" : void 0
346
701
  );
347
- } else {
348
- warnings.push(`${fileIndex}:${unitId}: no source/target`);
702
+ else warnings.push(fileIndex + ":" + unitId + ": no source/target");
349
703
  }
350
- });
704
+ };
705
+ if (isXliff2) walk(fileNode, inherited(fileNode, true));
706
+ else for (const body of children(fileNode, "body")) walk(body, true);
351
707
  });
352
- if (hasSdlNamespace || hasSdlElement) format = "sdlxliff";
708
+ const ranged = segments.filter((segment) => segment.targetRange).sort((a, b) => a.targetRange.start - b.targetRange.start || b.targetRange.end - a.targetRange.end);
709
+ let previous;
710
+ for (const segment of ranged) {
711
+ if (previous && (segment.targetRange.start < previous.targetRange.end || segment.targetRange.start === previous.targetRange.start)) {
712
+ previous.readOnlyReason = segment.readOnlyReason = "overlapping target containers";
713
+ warnings.push(segment.id + ": overlapping target containers; read-only");
714
+ }
715
+ if (!previous || segment.targetRange.end > previous.targetRange.end) previous = segment;
716
+ }
353
717
  return { format, version, files, segments, warnings, xml };
354
718
  }
355
719
 
@@ -358,24 +722,17 @@ function escapeXmlText(value) {
358
722
  return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
359
723
  }
360
724
  function resolveContent(segment, edit) {
725
+ if (segment.readOnlyReason && !(edit.xml && edit.content === segment.targetInner)) throw new Error(`segment is read-only: ${segment.readOnlyReason}`);
361
726
  const hasInlineTags = segment.targetTags.length > 0;
362
727
  if (hasInlineTags) {
363
728
  if (!edit.xml) {
364
729
  throw new Error("target contains inline tags; provide a validated XML fragment with xml=true");
365
730
  }
366
- const signature = fragmentSignature(edit.content);
367
- if (signature.join("|") !== segment.targetTags.join("|")) {
368
- throw new Error(
369
- `inline tag signature mismatch: expected [${segment.targetTags.join(", ")}], got [${signature.join(", ")}]`
370
- );
371
- }
731
+ validateInlineEdit(segment, edit.content);
372
732
  return edit.content;
373
733
  }
374
734
  if (edit.xml) {
375
- const signature = fragmentSignature(edit.content);
376
- if (signature.length > 0) {
377
- throw new Error("target has no inline tags; XML fragment must not add tags");
378
- }
735
+ validateInlineEdit(segment, edit.content);
379
736
  return edit.content;
380
737
  }
381
738
  return escapeXmlText(edit.content);
@@ -395,10 +752,14 @@ function patchDocument(document, edits) {
395
752
  continue;
396
753
  }
397
754
  try {
755
+ const content = resolveContent(segment, edit);
756
+ const target = segment.targetNode;
757
+ const opening = target ? document.xml.slice(target.start, target.openEnd) : "";
758
+ const expandEmptyTarget = content !== "" && target !== void 0 && target.openEnd === target.end && opening.endsWith("/>");
398
759
  prepared.push({
399
- start: segment.targetRange.start,
400
- end: segment.targetRange.end,
401
- content: resolveContent(segment, edit)
760
+ start: expandEmptyTarget ? target.start : segment.targetRange.start,
761
+ end: expandEmptyTarget ? target.end : segment.targetRange.end,
762
+ content: expandEmptyTarget ? `${opening.slice(0, -2)}>${content}</${target.name}>` : content
402
763
  });
403
764
  } catch (caught) {
404
765
  errors.push({
@@ -502,6 +863,10 @@ function patchSdlStates(document, updates) {
502
863
  errors.push({ segmentId, message: "sdl:seg node missing" });
503
864
  continue;
504
865
  }
866
+ if (segment.readOnlyReason) {
867
+ errors.push({ segmentId, message: `segment is read-only: ${segment.readOnlyReason}` });
868
+ continue;
869
+ }
505
870
  const node = segment.sdlNode;
506
871
  let tag = document.xml.slice(node.start, node.openEnd);
507
872
  for (const [key, attribute] of ATTRIBUTE_NAMES) {
@@ -699,6 +1064,35 @@ function parseTmx(xml, options = {}) {
699
1064
  }
700
1065
  return { entries, warnings };
701
1066
  }
1067
+ function mergeKey(entry) {
1068
+ const normalize = (value) => value.normalize("NFKC").replace(/\s+/gu, " ").trim();
1069
+ const context = entry.context;
1070
+ return [
1071
+ normalizeLanguage(entry.sourceLanguage),
1072
+ normalizeLanguage(entry.targetLanguage),
1073
+ normalize(entry.source),
1074
+ normalize(entry.target),
1075
+ normalize(context?.previousSource ?? ""),
1076
+ normalize(context?.nextSource ?? "")
1077
+ ].join("\0");
1078
+ }
1079
+ function mergeTmEntries(...inputs) {
1080
+ const entries = [];
1081
+ const seen = /* @__PURE__ */ new Set();
1082
+ let duplicates = 0;
1083
+ for (const input of inputs) {
1084
+ for (const entry of input) {
1085
+ const key = mergeKey(entry);
1086
+ if (seen.has(key)) {
1087
+ duplicates += 1;
1088
+ continue;
1089
+ }
1090
+ seen.add(key);
1091
+ entries.push(entry);
1092
+ }
1093
+ }
1094
+ return { entries, duplicates };
1095
+ }
702
1096
 
703
1097
  // packages/l10n-core/src/assets/glossary.ts
704
1098
  var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
@@ -838,6 +1232,123 @@ function parseGlossaryCsv(text, options = {}) {
838
1232
  return { entries, warnings };
839
1233
  }
840
1234
 
1235
+ // packages/l10n-core/src/assets/tbx.ts
1236
+ function findChild2(parent, local) {
1237
+ return elementChildren(parent).find((child) => child.local === local);
1238
+ }
1239
+ function findDescendant2(parent, local) {
1240
+ for (const child of elementChildren(parent)) {
1241
+ if (child.local === local) return child;
1242
+ const nested = findDescendant2(child, local);
1243
+ if (nested) return nested;
1244
+ }
1245
+ return void 0;
1246
+ }
1247
+ function langSetLanguage(langSet) {
1248
+ return langSet.attribs["xml:lang"] ?? langSet.attribs.lang ?? "";
1249
+ }
1250
+ function findLangSet(langSets, language) {
1251
+ const wanted = normalizeLanguage(language);
1252
+ const exact = langSets.find((langSet) => normalizeLanguage(langSetLanguage(langSet)) === wanted);
1253
+ if (exact) return exact;
1254
+ return langSets.find((langSet) => languageMatches(langSetLanguage(langSet), language));
1255
+ }
1256
+ function termOf(langSet) {
1257
+ const tig = findChild2(langSet, "tig");
1258
+ if (!tig) return void 0;
1259
+ const term = findDescendant2(tig, "term");
1260
+ const text = term ? textOf(term).trim() : "";
1261
+ return text === "" ? void 0 : text;
1262
+ }
1263
+ function isForbiddenLangSet(langSet) {
1264
+ const walk = (parent) => {
1265
+ for (const child of elementChildren(parent)) {
1266
+ if (child.local === "descrip" || child.local === "termNote") {
1267
+ const type = (child.attribs.type ?? child.attribs.name ?? "").trim().toLowerCase();
1268
+ const value = textOf(child).trim().toLowerCase();
1269
+ if (type === "administrativestatus" && /forbidden|deprecated/u.test(value)) return true;
1270
+ if (type === "forbidden" && /^(?:true|yes|1)$/u.test(value)) return true;
1271
+ }
1272
+ if (walk(child)) return true;
1273
+ }
1274
+ return false;
1275
+ };
1276
+ return walk(langSet);
1277
+ }
1278
+ function preferredLanguageOrder(langSets) {
1279
+ const seen = [];
1280
+ for (const langSet of langSets) {
1281
+ const language = langSetLanguage(langSet);
1282
+ if (language !== "" && !seen.some((item) => normalizeLanguage(item) === normalizeLanguage(language))) {
1283
+ seen.push(language);
1284
+ }
1285
+ }
1286
+ return seen;
1287
+ }
1288
+ function parseTbx(xml, options = {}) {
1289
+ const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
1290
+ validateXml(source);
1291
+ const document = parseXml(source);
1292
+ const root = elementChildren(document).find((child) => child.local === "tbx");
1293
+ if (!root) throw new Error("Not a TBX document: missing <tbx> root element");
1294
+ const warnings = [];
1295
+ const body = findChild2(root, "body");
1296
+ if (!body) {
1297
+ warnings.push("TBX: missing <body> element; no entries parsed");
1298
+ return { entries: [], warnings };
1299
+ }
1300
+ const termEntries = elementChildren(body).filter((child) => child.local === "termEntry");
1301
+ if (termEntries.length === 0) {
1302
+ warnings.push("TBX: no <termEntry> elements found");
1303
+ return { entries: [], warnings };
1304
+ }
1305
+ const declared = preferredLanguageOrder(
1306
+ termEntries.flatMap((entry) => elementChildren(entry).filter((child) => child.local === "langSet"))
1307
+ );
1308
+ const sourceLanguage = options.sourceLanguage ?? declared[0];
1309
+ if (sourceLanguage === void 0) {
1310
+ warnings.push("TBX: source language could not be determined; no entries parsed");
1311
+ return { entries: [], warnings };
1312
+ }
1313
+ const targetLanguage = options.targetLanguage ?? declared.find(
1314
+ (language) => !languageMatches(language, sourceLanguage) && normalizeLanguage(language) !== normalizeLanguage(sourceLanguage)
1315
+ );
1316
+ if (targetLanguage === void 0) {
1317
+ warnings.push("TBX: target language could not be determined; no entries parsed");
1318
+ return { entries: [], warnings };
1319
+ }
1320
+ const entries = [];
1321
+ for (const [index, termEntry] of termEntries.entries()) {
1322
+ const label = termEntry.attribs.id ?? `#${index + 1}`;
1323
+ const langSets = elementChildren(termEntry).filter((child) => child.local === "langSet");
1324
+ const sourceLangSet = findLangSet(langSets, sourceLanguage);
1325
+ const targetLangSet = findLangSet(langSets, targetLanguage);
1326
+ if (!sourceLangSet) {
1327
+ warnings.push(`TBX: termEntry ${label} has no langSet for source language "${sourceLanguage}"; skipped`);
1328
+ continue;
1329
+ }
1330
+ if (!targetLangSet) {
1331
+ warnings.push(`TBX: termEntry ${label} has no langSet for target language "${targetLanguage}"; skipped`);
1332
+ continue;
1333
+ }
1334
+ const sourceTerm = termOf(sourceLangSet);
1335
+ const targetTerm = termOf(targetLangSet);
1336
+ if (sourceTerm === void 0 || targetTerm === void 0) {
1337
+ warnings.push(`TBX: termEntry ${label} has no <term> text on one side; skipped`);
1338
+ continue;
1339
+ }
1340
+ entries.push({
1341
+ id: `tbx-${entries.length + 1}`,
1342
+ source: sourceTerm,
1343
+ target: targetTerm,
1344
+ sourceLanguage,
1345
+ targetLanguage,
1346
+ ...isForbiddenLangSet(sourceLangSet) || isForbiddenLangSet(targetLangSet) ? { forbidden: true } : {}
1347
+ });
1348
+ }
1349
+ return { entries, warnings };
1350
+ }
1351
+
841
1352
  // packages/l10n-core/src/assets/tm-matcher.ts
842
1353
  var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
843
1354
  var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
@@ -1393,7 +1904,7 @@ function matchTerms(text, entries, options = {}) {
1393
1904
  return { hits, issues: [], warnings };
1394
1905
  }
1395
1906
 
1396
- // packages/l10n-core/src/qa/rules.ts
1907
+ // packages/l10n-core/src/qa/catalog.ts
1397
1908
  var RULES = [
1398
1909
  // structural class
1399
1910
  { id: "EMPTY_TARGET", severity: "critical" },
@@ -1408,8 +1919,11 @@ var RULES = [
1408
1919
  // content class
1409
1920
  { id: "NUMBER_MISMATCH", severity: "major" },
1410
1921
  { id: "DATE_MISMATCH", severity: "major" },
1922
+ { id: "DATE_FORMAT", severity: "minor", requiresLocale: true },
1411
1923
  { id: "DUPLICATE_WORD", severity: "major" },
1412
1924
  { id: "TERM_MISSING", severity: "major", requiresTerms: true },
1925
+ { id: "TERMINOLOGY_INCONSISTENT", severity: "major", requiresTerms: true, scope: "document" },
1926
+ { id: "SOURCE_TARGET_INCONSISTENT", severity: "major", scope: "document" },
1413
1927
  { id: "SOURCE_LEFTOVER", severity: "major" },
1414
1928
  { id: "PUNCT_DUP", severity: "major" },
1415
1929
  { id: "TRAILING_COLON", severity: "minor" },
@@ -1419,9 +1933,24 @@ var RULES = [
1419
1933
  { id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
1420
1934
  { id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
1421
1935
  ];
1936
+ var QA_RULE_CATALOG = Object.freeze(RULES.map((rule, index) => Object.freeze({
1937
+ ruleId: rule.id,
1938
+ code: `QA-${String(index + 1).padStart(3, "0")}`,
1939
+ severity: rule.severity,
1940
+ defaultEnabled: rule.defaultOff !== true,
1941
+ scope: rule.scope ?? "segment",
1942
+ requiresTerms: rule.requiresTerms === true,
1943
+ requiresLocale: rule.requiresLocale === true
1944
+ })));
1945
+
1946
+ // packages/l10n-core/src/qa/rules.ts
1422
1947
  var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
1423
1948
  var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
1424
1949
  var TERM_RULE_IDS = RULES.filter((rule) => rule.requiresTerms).map((rule) => rule.id);
1950
+ var LOCALE_RULE_IDS = RULES.filter((rule) => rule.requiresLocale).map((rule) => rule.id);
1951
+ var DOCUMENT_RULE_IDS = new Set(
1952
+ RULES.filter((rule) => rule.scope === "document").map((rule) => rule.id)
1953
+ );
1425
1954
  var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
1426
1955
  function normalizeForCompare(value) {
1427
1956
  return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
@@ -1535,15 +2064,14 @@ function checkTags(context) {
1535
2064
  const sourceTags = context.segment.sourceTags;
1536
2065
  const targetTags = context.segment.targetTags;
1537
2066
  if (sourceTags.length === 0 && targetTags.length === 0) return;
1538
- const sourceSignature = sourceTags.join("|");
1539
- const targetSignature = targetTags.join("|");
1540
- if (sourceSignature === targetSignature) return;
1541
- const sameMultiset = sourceTags.length === targetTags.length && JSON.stringify([...sourceTags].sort()) === JSON.stringify([...targetTags].sort());
2067
+ const source = context.segment.sourceNode ? inlineCodeSignatures(context.segment.sourceNode) : sourceTags;
2068
+ const target = context.segment.targetNode ? inlineCodeSignatures(context.segment.targetNode) : targetTags;
2069
+ if (JSON.stringify([...source].sort()) === JSON.stringify([...target].sort())) return;
1542
2070
  draft(context, {
1543
2071
  ruleId: "TAG_MISMATCH",
1544
- message: sameMultiset ? "inline tag \u987A\u5E8F\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4" : `inline tag \u4E0D\u4E00\u81F4: \u6E90\u6587 [${sourceTags.join(", ")}] / \u8BD1\u6587 [${targetTags.join(", ")}]`,
1545
- evidence: `source tags: [${sourceTags.join(", ")}], target tags: [${targetTags.join(", ")}]`,
1546
- suggestion: "\u4FDD\u6301 inline tag \u6570\u91CF\u4E0E\u987A\u5E8F\u548C\u6E90\u6587\u4E00\u81F4"
2072
+ message: `\u6807\u7B7E\u7684\u6570\u91CF\u3001\u6807\u8BC6\u6216\u539F\u751F\u4EE3\u7801\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4`,
2073
+ evidence: JSON.stringify({ source, target }),
2074
+ 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"
1547
2075
  });
1548
2076
  }
1549
2077
  var URL_PATTERNS = [
@@ -1796,6 +2324,47 @@ function checkDates(context) {
1796
2324
  ...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
1797
2325
  });
1798
2326
  }
2327
+ function localeDateOrder(locale) {
2328
+ const normalized = locale.trim().toLowerCase().replace(/_/gu, "-");
2329
+ if (/^(?:zh|ja|ko)(?:-|$)/u.test(normalized)) return "ymd";
2330
+ if (/^en-us(?:-|$)/u.test(normalized)) return "mdy";
2331
+ 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(
2332
+ normalized
2333
+ )) {
2334
+ return "dmy";
2335
+ }
2336
+ return void 0;
2337
+ }
2338
+ function numericDateOrder(text) {
2339
+ if (/^(?:\d{4}|[〇零一二三四五六七八九]{4})年/u.test(text)) return "ymd";
2340
+ if (/^\d{4}[./-]\d{1,2}[./-]\d{1,2}$/u.test(text)) return "ymd";
2341
+ const match = /^(\d{1,2})[./-](\d{1,2})[./-]\d{2,4}$/u.exec(text);
2342
+ if (!match) return void 0;
2343
+ const first = Number(match[1]);
2344
+ const second = Number(match[2]);
2345
+ if (first > 12 && second <= 12) return "dmy";
2346
+ if (second > 12 && first <= 12) return "mdy";
2347
+ return void 0;
2348
+ }
2349
+ function checkDateFormat(context) {
2350
+ const targetLocale = context.locale?.targetLocale;
2351
+ if (targetLocale === void 0) return;
2352
+ const expected = localeDateOrder(targetLocale);
2353
+ if (expected === void 0) return;
2354
+ const dates = extractDates(context.target);
2355
+ if (dates.length === 0) return;
2356
+ for (const token of dates) {
2357
+ const actual = numericDateOrder(token.text);
2358
+ if (actual === void 0 || actual === expected) continue;
2359
+ draft(context, {
2360
+ ruleId: "DATE_FORMAT",
2361
+ 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()}`,
2362
+ evidence: `target date: "${token.text}"`,
2363
+ 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",
2364
+ targetRange: { start: token.span.start, end: token.span.end }
2365
+ });
2366
+ }
2367
+ }
1799
2368
  var BRACKET_PAIRS = /* @__PURE__ */ new Map([
1800
2369
  ["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
1801
2370
  ["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
@@ -2161,18 +2730,23 @@ function checkWhitespace(context) {
2161
2730
  function checkFullwidthHalfwidth(context) {
2162
2731
  const target = context.target;
2163
2732
  if (target.trim().length === 0) return;
2164
- const fullwidth = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/u.exec(target);
2165
- if (fullwidth && fullwidth.index !== void 0) {
2733
+ const exempt = new Set(context.locale?.fullwidthExemptChars ?? []);
2734
+ const fullwidthPattern = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/gu;
2735
+ for (const match of target.matchAll(fullwidthPattern)) {
2736
+ const char = match[0];
2737
+ if (exempt.has(char)) continue;
2738
+ const index = match.index ?? 0;
2166
2739
  draft(context, {
2167
2740
  ruleId: "FULLWIDTH_HALFWIDTH",
2168
2741
  message: "\u8BD1\u6587\u4E2D\u5B57\u6BCD/\u6570\u5B57\u4F7F\u7528\u5168\u89D2\u5F62\u5F0F",
2169
- evidence: `found '${fullwidth[0]}'`,
2742
+ evidence: `found '${char}'`,
2170
2743
  suggestion: "\u5B57\u6BCD\u4E0E\u6570\u5B57\u901A\u5E38\u4F7F\u7528\u534A\u89D2\u5F62\u5F0F",
2171
- targetRange: { start: fullwidth.index, end: fullwidth.index + 1 }
2744
+ targetRange: { start: index, end: index + 1 }
2172
2745
  });
2173
2746
  return;
2174
2747
  }
2175
- if (!context.targetIsCjk) return;
2748
+ const expectFullwidthPunctuation = context.locale?.fullwidthPunctuation ?? context.targetIsCjk;
2749
+ if (!expectFullwidthPunctuation) return;
2176
2750
  const mixed = /[\u4e00-\u9fff][,;:!?]|[,;:!?][\u4e00-\u9fff]/u.exec(target);
2177
2751
  if (mixed === null || mixed.index === void 0) return;
2178
2752
  draft(context, {
@@ -2276,6 +2850,91 @@ function checkTermRules(context) {
2276
2850
  });
2277
2851
  }
2278
2852
  }
2853
+ function sourceKey(value) {
2854
+ return value.normalize("NFKC").replace(/\s+/gu, " ").trim();
2855
+ }
2856
+ function sourceTargetInconsistencies(contexts) {
2857
+ const groups = /* @__PURE__ */ new Map();
2858
+ for (const context of contexts) {
2859
+ if (context.source.trim().length === 0 || context.target.trim().length === 0) continue;
2860
+ const key = sourceKey(context.source);
2861
+ const group = groups.get(key);
2862
+ if (group === void 0) groups.set(key, [context]);
2863
+ else group.push(context);
2864
+ }
2865
+ const drafts = [];
2866
+ let checkedSegments = 0;
2867
+ let emitted = 0;
2868
+ for (const group of groups.values()) {
2869
+ if (group.length < 2) continue;
2870
+ checkedSegments += group.length;
2871
+ const targets = group.map((context) => normalizeForCompare(context.target));
2872
+ const variants = Array.from(new Set(targets));
2873
+ if (variants.length < 2) continue;
2874
+ const reference = variants[0];
2875
+ for (let index = 1; index < group.length && emitted < 50; index += 1) {
2876
+ if (targets[index] === reference) continue;
2877
+ const context = group[index];
2878
+ const segment = context?.segment;
2879
+ if (context === void 0 || segment === void 0) continue;
2880
+ drafts.push({
2881
+ segment,
2882
+ draft: {
2883
+ ruleId: "SOURCE_TARGET_INCONSISTENT",
2884
+ message: `\u540C\u4E00\u6E90\u6587\u5B58\u5728\u4E0D\u540C\u8BD1\u6CD5: '${truncate(context.source)}'`,
2885
+ evidence: `variants: [${variants.slice(0, 3).map((value) => truncate(value, 40)).join(" | ")}]`,
2886
+ suggestion: "\u7EDF\u4E00\u8BE5\u53E5\u6BB5\u7684\u8BD1\u6CD5"
2887
+ }
2888
+ });
2889
+ emitted += 1;
2890
+ }
2891
+ }
2892
+ return { drafts, checkedSegments };
2893
+ }
2894
+ function terminologyInconsistencies(contexts) {
2895
+ const perTerm = /* @__PURE__ */ new Map();
2896
+ let checkedSegments = 0;
2897
+ for (const context of contexts) {
2898
+ if (context.termHits === void 0 || context.target.trim().length === 0) continue;
2899
+ const sourceHits = longestHits(context.termHits.source);
2900
+ if (sourceHits.length === 0) continue;
2901
+ checkedSegments += 1;
2902
+ for (const hit of sourceHits) {
2903
+ const entry = hit.entry;
2904
+ if (entry.forbidden === true || !isApproved(entry) || (entry.target ?? "").trim().length === 0) continue;
2905
+ const bucket = perTerm.get(entry.id) ?? { entry, items: [] };
2906
+ if (!bucket.items.some((item) => item.context === context)) {
2907
+ bucket.items.push({
2908
+ context,
2909
+ mentioned: targetMentionsTerm(entry, context.target, context.termHits.target)
2910
+ });
2911
+ }
2912
+ perTerm.set(entry.id, bucket);
2913
+ }
2914
+ }
2915
+ const drafts = [];
2916
+ let emitted = 0;
2917
+ for (const { entry, items } of perTerm.values()) {
2918
+ const mentioned = items.filter((item) => item.mentioned);
2919
+ const missing = items.filter((item) => !item.mentioned);
2920
+ if (mentioned.length === 0 || missing.length === 0) continue;
2921
+ for (const item of missing) {
2922
+ if (emitted >= 25) break;
2923
+ drafts.push({
2924
+ segment: item.context.segment,
2925
+ draft: {
2926
+ ruleId: "TERMINOLOGY_INCONSISTENT",
2927
+ 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`,
2928
+ evidence: `used in ${mentioned.length} segment(s), missing in ${missing.length}`,
2929
+ suggestion: entry.target,
2930
+ termId: entry.id
2931
+ }
2932
+ });
2933
+ emitted += 1;
2934
+ }
2935
+ }
2936
+ return { drafts, checkedSegments };
2937
+ }
2279
2938
  var CHECK_GROUPS = [
2280
2939
  { ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
2281
2940
  { ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
@@ -2286,6 +2945,7 @@ var CHECK_GROUPS = [
2286
2945
  { ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
2287
2946
  { ids: ["NUMBER_MISMATCH"], run: checkNumbers },
2288
2947
  { ids: ["DATE_MISMATCH"], run: checkDates },
2948
+ { ids: ["DATE_FORMAT"], run: checkDateFormat },
2289
2949
  { ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
2290
2950
  { ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
2291
2951
  { ids: ["PUNCT_DUP"], run: checkPunctDup },
@@ -2310,10 +2970,17 @@ function checkEmptyTargetGroup(context) {
2310
2970
  function checkUntranslatedCopyGroup(context) {
2311
2971
  const { source, target } = context;
2312
2972
  if (source.trim().length === 0 || target.trim().length === 0) return;
2313
- if (normalizeForCompare(source) !== normalizeForCompare(target)) return;
2973
+ const normalizedSource = normalizeForCompare(source);
2974
+ if (normalizedSource !== normalizeForCompare(target)) return;
2975
+ const policy = context.untranslatedCopy;
2976
+ if (policy?.whitelist?.some((entry) => normalizeForCompare(entry) === normalizedSource)) return;
2314
2977
  const tokens = wordTokens(source);
2315
2978
  const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
2316
- if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= 4) return;
2979
+ const maxShort = policy?.shortSegmentMaxLength ?? 4;
2980
+ if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= maxShort) return;
2981
+ if (tokens.length <= 1 && policy?.singleTokenExemptions?.some((entry) => normalizeForCompare(entry) === normalizedSource)) {
2982
+ return;
2983
+ }
2317
2984
  draft(context, {
2318
2985
  ruleId: "UNTRANSLATED_COPY",
2319
2986
  message: "\u8BD1\u6587\u4E0E\u6E90\u6587\u5B8C\u5168\u76F8\u540C\uFF08\u672A\u7FFB\u8BD1\uFF09",
@@ -2341,6 +3008,7 @@ function isApplicable(ruleId, segment) {
2341
3008
  case "UNPAIRED_BRACKET":
2342
3009
  case "NUMBER_MISMATCH":
2343
3010
  case "DATE_MISMATCH":
3011
+ case "DATE_FORMAT":
2344
3012
  case "DUPLICATE_WORD":
2345
3013
  case "TERM_MISSING":
2346
3014
  case "SOURCE_LEFTOVER":
@@ -2367,6 +3035,7 @@ function resolveEnabledRules(options) {
2367
3035
  return enabled2;
2368
3036
  }
2369
3037
  const enabled = new Set(DEFAULT_ENABLED);
3038
+ if (options.locale?.targetLocale === void 0) for (const ruleId of LOCALE_RULE_IDS) enabled.delete(ruleId);
2370
3039
  if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
2371
3040
  return enabled;
2372
3041
  }
@@ -2391,6 +3060,8 @@ function buildContext(segment, options, glossary) {
2391
3060
  targetIsCjk: isCjkHeavy(segment.targetText),
2392
3061
  glossary,
2393
3062
  drafts: [],
3063
+ ...options.locale === void 0 ? {} : { locale: options.locale },
3064
+ ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
2394
3065
  ...termHits === void 0 ? {} : { termHits }
2395
3066
  };
2396
3067
  }
@@ -2426,6 +3097,14 @@ function runDeterministicChecks(segments, options = {}) {
2426
3097
  const state = /* @__PURE__ */ new Map();
2427
3098
  for (const rule of RULES) {
2428
3099
  if (!enabled.has(rule.id)) continue;
3100
+ if (rule.requiresLocale === true && options.locale?.targetLocale === void 0) {
3101
+ state.set(rule.id, {
3102
+ status: "skipped",
3103
+ reason: "no locale.targetLocale provided; locale rule was not executed",
3104
+ checkedSegments: 0
3105
+ });
3106
+ continue;
3107
+ }
2429
3108
  if (rule.requiresTerms === true && !termAvailable) {
2430
3109
  state.set(rule.id, {
2431
3110
  status: "skipped",
@@ -2442,6 +3121,7 @@ function runDeterministicChecks(segments, options = {}) {
2442
3121
  const applicable = /* @__PURE__ */ new Set();
2443
3122
  for (const ruleId of enabled) {
2444
3123
  if (state.get(ruleId)?.status !== "executed") continue;
3124
+ if (DOCUMENT_RULE_IDS.has(ruleId)) continue;
2445
3125
  if (isApplicable(ruleId, segment)) applicable.add(ruleId);
2446
3126
  }
2447
3127
  if (applicable.size === 0) continue;
@@ -2480,6 +3160,18 @@ function runDeterministicChecks(segments, options = {}) {
2480
3160
  issues.push(toIssue(draft2, segment, draft2.ruleId));
2481
3161
  }
2482
3162
  }
3163
+ const documentRules = RULES.filter(
3164
+ (rule) => rule.scope === "document" && state.get(rule.id)?.status === "executed"
3165
+ );
3166
+ if (documentRules.length > 0) {
3167
+ const contexts = segments.map((segment) => buildContext(segment, options, glossary));
3168
+ for (const rule of documentRules) {
3169
+ const result = rule.id === "TERMINOLOGY_INCONSISTENT" ? terminologyInconsistencies(contexts) : sourceTargetInconsistencies(contexts);
3170
+ for (const item of result.drafts) issues.push(toIssue(item.draft, item.segment, rule.id));
3171
+ const execution = state.get(rule.id);
3172
+ if (execution !== void 0) execution.checkedSegments = result.checkedSegments;
3173
+ }
3174
+ }
2483
3175
  const rules = [];
2484
3176
  for (const rule of RULES) {
2485
3177
  const execution = state.get(rule.id);
@@ -2532,7 +3224,9 @@ function checkDocument(document, options = {}) {
2532
3224
  const qaOptions = {
2533
3225
  ...options.glossary === void 0 ? {} : { glossary: options.glossary },
2534
3226
  ...segmentTermHits.size === 0 ? {} : { segmentTermHits },
2535
- ...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules }
3227
+ ...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules },
3228
+ ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
3229
+ ...options.locale === void 0 ? {} : { locale: options.locale }
2536
3230
  };
2537
3231
  const qa = runDeterministicChecks(document.segments, qaOptions);
2538
3232
  return { qa, segmentTermHits };
@@ -2555,6 +3249,11 @@ function translateDocument(document, tmEntries, options = {}) {
2555
3249
  let skipped = 0;
2556
3250
  for (const segment of document.segments) {
2557
3251
  if (segment.targetText.trim().length > 0) continue;
3252
+ if (segment.readOnlyReason) {
3253
+ warnings.push(`${segment.id}: read-only: ${segment.readOnlyReason}; skipped`);
3254
+ skipped += 1;
3255
+ continue;
3256
+ }
2558
3257
  if (segment.targetRange === null) {
2559
3258
  warnings.push(`${segment.id}: target missing; cannot fill`);
2560
3259
  skipped += 1;
@@ -3299,35 +3998,945 @@ function polishDocument(document, edits) {
3299
3998
  };
3300
3999
  }
3301
4000
 
4001
+ // packages/dsh-l10n/src/project.ts
4002
+ var MANIFEST_DIRECTORY = ".langlink";
4003
+ var MANIFEST_FILENAME = "manifest.json";
4004
+ var MANIFEST_VERSION = 1;
4005
+ var MAX_ASSET_BYTES = 200 * 1024 * 1024;
4006
+ var MAX_ASSETS = 64;
4007
+ function manifestPathFor(root) {
4008
+ return join(root, MANIFEST_DIRECTORY, MANIFEST_FILENAME);
4009
+ }
4010
+ function isRecord(value) {
4011
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4012
+ }
4013
+ function requireString(value, field) {
4014
+ if (typeof value !== "string" || value.trim() === "") throw new Error(`project manifest: ${field} must be a non-empty string`);
4015
+ return value;
4016
+ }
4017
+ function optionalString(value, field) {
4018
+ if (value === void 0 || value === null) return void 0;
4019
+ if (typeof value !== "string" || value.trim() === "") throw new Error(`project manifest: ${field} must be a non-empty string`);
4020
+ return value;
4021
+ }
4022
+ function requireArray(value, field) {
4023
+ if (!Array.isArray(value)) throw new Error(`project manifest: ${field} must be an array`);
4024
+ return value;
4025
+ }
4026
+ function parseMapping(value, field) {
4027
+ if (value === void 0) return void 0;
4028
+ if (!isRecord(value)) throw new Error(`project manifest: ${field} must be an object`);
4029
+ const mapping2 = {};
4030
+ if (value.sheet !== void 0) mapping2.sheet = requireString(value.sheet, `${field}.sheet`);
4031
+ if (value.headerRow !== void 0) {
4032
+ if (typeof value.headerRow !== "number" || !Number.isInteger(value.headerRow) || value.headerRow < 0) {
4033
+ throw new Error(`project manifest: ${field}.headerRow must be a non-negative integer`);
4034
+ }
4035
+ mapping2.headerRow = value.headerRow;
4036
+ }
4037
+ for (const key of ["sourceColumn", "targetColumn", "forbiddenColumn"]) {
4038
+ if (value[key] !== void 0) mapping2[key] = requireString(value[key], `${field}.${key}`);
4039
+ }
4040
+ return Object.keys(mapping2).length === 0 ? void 0 : mapping2;
4041
+ }
4042
+ function parseAsset(value, index) {
4043
+ if (!isRecord(value)) throw new Error(`project manifest: assets[${index}] must be an object`);
4044
+ const id = requireString(value.id, `assets[${index}].id`);
4045
+ if (!/^[A-Za-z0-9._-]{1,64}$/u.test(id)) throw new Error(`project manifest: assets[${index}].id "${id}" is not a safe identifier`);
4046
+ const kind = requireString(value.kind, `assets[${index}].kind`);
4047
+ if (kind !== "tm" && kind !== "tb" && kind !== "bilingual") {
4048
+ throw new Error(`project manifest: assets[${index}].kind must be tm, tb or bilingual`);
4049
+ }
4050
+ const format = requireString(value.format, `assets[${index}].format`);
4051
+ if (!["tmx", "tbx", "csv", "tsv", "txt", "xlsx"].includes(format)) {
4052
+ throw new Error(`project manifest: assets[${index}].format "${format}" is not supported`);
4053
+ }
4054
+ const entries = isRecord(value.counts) ? value.counts.entries : 0;
4055
+ if (typeof entries !== "number" || !Number.isInteger(entries) || entries < 0) {
4056
+ throw new Error(`project manifest: assets[${index}].counts.entries must be a non-negative integer`);
4057
+ }
4058
+ const addedAt = requireString(value.addedAt, `assets[${index}].addedAt`);
4059
+ const updatedAt = requireString(value.updatedAt, `assets[${index}].updatedAt`);
4060
+ const sourceLang = optionalString(value.sourceLang, `assets[${index}].sourceLang`);
4061
+ const targetLang = optionalString(value.targetLang, `assets[${index}].targetLang`);
4062
+ const mapping2 = parseMapping(value.mapping, `assets[${index}].mapping`);
4063
+ return {
4064
+ id,
4065
+ kind,
4066
+ format,
4067
+ path: requireString(value.path, `assets[${index}].path`),
4068
+ enabled: value.enabled === void 0 ? true : value.enabled === true,
4069
+ priority: typeof value.priority === "number" && Number.isFinite(value.priority) ? value.priority : 100,
4070
+ ...sourceLang === void 0 ? {} : { sourceLang },
4071
+ ...targetLang === void 0 ? {} : { targetLang },
4072
+ sha256: requireString(value.sha256, `assets[${index}].sha256`),
4073
+ size: typeof value.size === "number" && Number.isFinite(value.size) ? value.size : 0,
4074
+ counts: { entries },
4075
+ ...mapping2 === void 0 ? {} : { mapping: mapping2 },
4076
+ addedAt,
4077
+ updatedAt
4078
+ };
4079
+ }
4080
+ function parseManifest(value, manifestPath) {
4081
+ if (!isRecord(value)) throw new Error(`project manifest is not a JSON object: ${manifestPath}`);
4082
+ if (value.version !== MANIFEST_VERSION) {
4083
+ throw new Error(`project manifest version ${String(value.version)} is not supported (expected ${MANIFEST_VERSION})`);
4084
+ }
4085
+ const qaInput = isRecord(value.qa) ? value.qa : void 0;
4086
+ const outputsInput = isRecord(value.outputs) ? value.outputs : void 0;
4087
+ let qa;
4088
+ if (qaInput !== void 0) {
4089
+ const parsed = {};
4090
+ const locale = optionalString(qaInput.locale, "qa.locale");
4091
+ if (locale !== void 0) parsed.locale = locale;
4092
+ if (typeof qaInput.threshold === "number") parsed.threshold = qaInput.threshold;
4093
+ if (typeof qaInput.maxMatches === "number") parsed.maxMatches = qaInput.maxMatches;
4094
+ if (Array.isArray(qaInput.untranslatedCopyWhitelist)) {
4095
+ parsed.untranslatedCopyWhitelist = qaInput.untranslatedCopyWhitelist.filter(
4096
+ (item) => typeof item === "string"
4097
+ );
4098
+ }
4099
+ qa = parsed;
4100
+ }
4101
+ let outputs;
4102
+ if (outputsInput !== void 0) {
4103
+ const dir = optionalString(outputsInput.dir, "outputs.dir");
4104
+ outputs = dir === void 0 ? {} : { dir };
4105
+ }
4106
+ return {
4107
+ version: MANIFEST_VERSION,
4108
+ name: requireString(value.name, "name"),
4109
+ sourceLang: requireString(value.sourceLang, "sourceLang"),
4110
+ targetLang: requireString(value.targetLang, "targetLang"),
4111
+ createdAt: requireString(value.createdAt, "createdAt"),
4112
+ updatedAt: requireString(value.updatedAt, "updatedAt"),
4113
+ files: requireArray(value.files ?? [], "files").map((entry, index) => {
4114
+ if (!isRecord(entry)) throw new Error(`project manifest: files[${index}] must be an object`);
4115
+ return { path: requireString(entry.path, `files[${index}].path`), sha256: requireString(entry.sha256, `files[${index}].sha256`) };
4116
+ }),
4117
+ assets: requireArray(value.assets ?? [], "assets").map(parseAsset),
4118
+ ...qa === void 0 ? {} : { qa },
4119
+ ...outputs === void 0 ? {} : { outputs }
4120
+ };
4121
+ }
4122
+ async function readManifest(root) {
4123
+ const manifestPath = manifestPathFor(root);
4124
+ let text;
4125
+ try {
4126
+ text = await readFile(manifestPath, "utf8");
4127
+ } catch {
4128
+ throw new Error(`no project manifest at ${manifestPath}; run l10n_project_init first`);
4129
+ }
4130
+ let parsed;
4131
+ try {
4132
+ parsed = JSON.parse(text);
4133
+ } catch (caught) {
4134
+ throw new Error(`project manifest is not valid JSON: ${caught instanceof Error ? caught.message : String(caught)}`);
4135
+ }
4136
+ return { root, manifestPath, manifest: parseManifest(parsed, manifestPath) };
4137
+ }
4138
+ async function writeManifest(handle) {
4139
+ const { manifest, manifestPath } = handle;
4140
+ manifest.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
4141
+ await mkdir(dirname(manifestPath), { recursive: true });
4142
+ const temp = `${manifestPath}.tmp-${process.pid}-${Date.now()}`;
4143
+ await writeFile(temp, `${JSON.stringify(manifest, null, 2)}
4144
+ `, "utf8");
4145
+ await rename(temp, manifestPath);
4146
+ }
4147
+ async function isDirectory(path) {
4148
+ try {
4149
+ return (await stat(path)).isDirectory();
4150
+ } catch {
4151
+ return false;
4152
+ }
4153
+ }
4154
+ async function hasManifest(root) {
4155
+ try {
4156
+ return (await stat(manifestPathFor(root))).isFile();
4157
+ } catch {
4158
+ return false;
4159
+ }
4160
+ }
4161
+ async function findProjectRootFrom(start) {
4162
+ let current = resolve(start);
4163
+ try {
4164
+ if (!await isDirectory(current)) current = dirname(current);
4165
+ } catch {
4166
+ current = dirname(current);
4167
+ }
4168
+ for (; ; ) {
4169
+ if (await hasManifest(current)) return current;
4170
+ const parent = dirname(current);
4171
+ if (parent === current) return void 0;
4172
+ current = parent;
4173
+ }
4174
+ }
4175
+ async function findProject(project, anchor) {
4176
+ if (project !== void 0 && project.trim() !== "") {
4177
+ const candidate = resolve(project);
4178
+ if (candidate.endsWith(MANIFEST_FILENAME)) {
4179
+ const root2 = dirname(dirname(candidate));
4180
+ return readManifest(root2);
4181
+ }
4182
+ if (!await isDirectory(candidate)) throw new Error(`project root is not a directory: ${candidate}`);
4183
+ return await hasManifest(candidate) ? readManifest(candidate) : void 0;
4184
+ }
4185
+ const start = anchor ?? process.cwd();
4186
+ const root = await findProjectRootFrom(start);
4187
+ return root === void 0 ? void 0 : readManifest(root);
4188
+ }
4189
+ async function requireProject(project, anchor) {
4190
+ const handle = await findProject(project, anchor);
4191
+ if (handle !== void 0) return handle;
4192
+ if (project === void 0) {
4193
+ const fromCwd = await findProject(void 0, process.cwd());
4194
+ if (fromCwd !== void 0) return fromCwd;
4195
+ }
4196
+ throw new Error("no project manifest found; run l10n_project_init or pass project (root or .langlink/manifest.json)");
4197
+ }
4198
+ async function initProject(options) {
4199
+ const root = resolve(options.root);
4200
+ if (await isDirectory(root)) {
4201
+ const manifest = await hasManifest(root);
4202
+ if (manifest && options.overwrite !== true) {
4203
+ throw new Error(`project manifest already exists at ${manifestPathFor(root)}; pass overwrite=true to update languages/QA settings`);
4204
+ }
4205
+ }
4206
+ await mkdir(root, { recursive: true });
4207
+ await mkdir(join(root, "in"), { recursive: true });
4208
+ await mkdir(join(root, "assets"), { recursive: true });
4209
+ await mkdir(join(root, "out"), { recursive: true });
4210
+ const now = (/* @__PURE__ */ new Date()).toISOString();
4211
+ let handle;
4212
+ if (await hasManifest(root)) {
4213
+ handle = await readManifest(root);
4214
+ handle.manifest.name = options.name ?? handle.manifest.name;
4215
+ handle.manifest.sourceLang = options.sourceLang;
4216
+ handle.manifest.targetLang = options.targetLang;
4217
+ } else {
4218
+ handle = {
4219
+ root,
4220
+ manifestPath: manifestPathFor(root),
4221
+ manifest: {
4222
+ version: MANIFEST_VERSION,
4223
+ name: options.name ?? "localization",
4224
+ sourceLang: options.sourceLang,
4225
+ targetLang: options.targetLang,
4226
+ createdAt: now,
4227
+ updatedAt: now,
4228
+ files: [],
4229
+ assets: [],
4230
+ outputs: { dir: "out" }
4231
+ }
4232
+ };
4233
+ }
4234
+ const qa = handle.manifest.qa ?? {};
4235
+ if (options.locale !== void 0) qa.locale = options.locale;
4236
+ if (options.threshold !== void 0) qa.threshold = options.threshold;
4237
+ if (options.maxMatches !== void 0) qa.maxMatches = options.maxMatches;
4238
+ if (options.untranslatedCopyWhitelist !== void 0) qa.untranslatedCopyWhitelist = options.untranslatedCopyWhitelist;
4239
+ handle.manifest.qa = qa;
4240
+ await writeManifest(handle);
4241
+ return handle;
4242
+ }
4243
+ function formatOf(path) {
4244
+ const lower = path.toLowerCase();
4245
+ if (lower.endsWith(".tmx")) return "tmx";
4246
+ if (lower.endsWith(".tbx")) return "tbx";
4247
+ if (lower.endsWith(".csv")) return "csv";
4248
+ if (lower.endsWith(".tsv")) return "tsv";
4249
+ if (lower.endsWith(".txt")) return "txt";
4250
+ if (lower.endsWith(".xlsx")) return "xlsx";
4251
+ throw new Error(`unsupported asset format: ${path} (expected .tmx, .tbx, .csv, .tsv, .txt or .xlsx)`);
4252
+ }
4253
+ function normalizeHeader2(value) {
4254
+ return value.trim().toLowerCase().replace(/[\s_-]+/gu, "");
4255
+ }
4256
+ var SOURCE_HEADERS3 = /* @__PURE__ */ new Set(["source", "src", "sourcetext", "\u6E90\u6587", "\u539F\u6587", "\u6E90\u8BED\u8A00"]);
4257
+ var TARGET_HEADERS3 = /* @__PURE__ */ new Set(["target", "tgt", "targettext", "\u8BD1\u6587", "\u76EE\u6A19", "\u76EE\u6807\u8BED\u8A00"]);
4258
+ var ID_HEADERS2 = /* @__PURE__ */ new Set(["id", "segmentid", "\u7F16\u53F7"]);
4259
+ var LOCATION_HEADERS2 = /* @__PURE__ */ new Set(["location", "\u4F4D\u7F6E", "\u6587\u4EF6", "file"]);
4260
+ var FORBIDDEN_HEADERS2 = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
4261
+ function looksLikeHeader(row) {
4262
+ const normalized = row.map(normalizeHeader2);
4263
+ return normalized.some((cell) => SOURCE_HEADERS3.has(cell)) && normalized.some((cell) => TARGET_HEADERS3.has(cell));
4264
+ }
4265
+ function headerColumn(row, aliases) {
4266
+ return row.findIndex((cell) => aliases.has(normalizeHeader2(cell)));
4267
+ }
4268
+ function columnIndex(value, header) {
4269
+ const trimmed = value.trim();
4270
+ if (/^[A-Za-z]{1,3}$/u.test(trimmed)) {
4271
+ let index = 0;
4272
+ for (const char of trimmed.toUpperCase()) index = index * 26 + (char.charCodeAt(0) - 64);
4273
+ return index - 1;
4274
+ }
4275
+ if (/^\d+$/u.test(trimmed)) {
4276
+ const number = Number(trimmed);
4277
+ if (number < 1) throw new Error(`column index must be 1-based: ${value}`);
4278
+ return number - 1;
4279
+ }
4280
+ if (header !== void 0) {
4281
+ const normalized = normalizeHeader2(trimmed);
4282
+ const index = header.findIndex((cell) => normalizeHeader2(cell) === normalized);
4283
+ if (index >= 0) return index;
4284
+ }
4285
+ throw new Error(`cannot resolve column "${value}"; use a 1-based index, spreadsheet letter, or a header name`);
4286
+ }
4287
+ async function readMatrix(path, format, mapping2) {
4288
+ if (format === "xlsx") {
4289
+ const workbook = new ExcelJS2.Workbook();
4290
+ await workbook.xlsx.load(await readFile(path));
4291
+ const sheet = mapping2?.sheet;
4292
+ let worksheet = workbook.worksheets[0];
4293
+ if (sheet !== void 0) {
4294
+ worksheet = /^\d+$/u.test(sheet) ? workbook.worksheets[Number(sheet) - 1] : workbook.getWorksheet(sheet);
4295
+ }
4296
+ if (!worksheet) throw new Error(`XLSX worksheet "${sheet ?? "1"}" was not found`);
4297
+ const rows = [];
4298
+ worksheet.eachRow({ includeEmpty: false }, (row) => {
4299
+ const values = [];
4300
+ for (let index = 1; index <= row.cellCount; index += 1) values.push(row.getCell(index).text ?? "");
4301
+ rows.push(values);
4302
+ });
4303
+ return { rows, warnings: sheet === void 0 && workbook.worksheets.length > 1 ? [`XLSX has ${workbook.worksheets.length} worksheets; using the first`] : [] };
4304
+ }
4305
+ const text = (await readFile(path, "utf8")).replace(/^\uFEFF/u, "");
4306
+ if (text.includes("\uFFFD")) throw new Error("bilingual asset is not valid UTF-8");
4307
+ const warnings = [];
4308
+ if (format === "csv") {
4309
+ const firstLine = text.split(/\r?\n/u)[0] ?? "";
4310
+ const tabs = (firstLine.match(/\t/gu) ?? []).length;
4311
+ const commas = (firstLine.match(/,/gu) ?? []).length;
4312
+ return { rows: parseCsvRows(text, tabs > commas ? " " : ","), warnings };
4313
+ }
4314
+ if (format === "tsv" || format === "txt") {
4315
+ return {
4316
+ rows: text.split(/\r?\n/u).filter((line) => line.trim() !== "").map((line) => line.split(" ")),
4317
+ warnings
4318
+ };
4319
+ }
4320
+ throw new Error(`unsupported bilingual format: ${format}`);
4321
+ }
4322
+ async function parseBilingualAsset(path, format, mapping2) {
4323
+ const matrix = await readMatrix(path, format, mapping2);
4324
+ if (matrix.rows.length === 0) throw new Error("bilingual table is empty");
4325
+ const warnings = [...matrix.warnings];
4326
+ const first = matrix.rows[0] ?? [];
4327
+ const headerRow = mapping2?.headerRow ?? (looksLikeHeader(first) ? 1 : 0);
4328
+ const autoHeader = headerRow === 1 && looksLikeHeader(first);
4329
+ const header = headerRow > 0 ? matrix.rows[headerRow - 1] : void 0;
4330
+ const sourceColumn = mapping2?.sourceColumn !== void 0 ? columnIndex(mapping2.sourceColumn, header) : autoHeader ? headerColumn(first, SOURCE_HEADERS3) : 0;
4331
+ const targetColumn = mapping2?.targetColumn !== void 0 ? columnIndex(mapping2.targetColumn, header) : autoHeader ? headerColumn(first, TARGET_HEADERS3) : 1;
4332
+ if (sourceColumn < 0 || targetColumn < 0) {
4333
+ throw new Error("could not detect source/target columns; pass mapping.sourceColumn/targetColumn");
4334
+ }
4335
+ const idColumn = autoHeader ? headerColumn(first, ID_HEADERS2) : -1;
4336
+ const locationColumn = autoHeader ? headerColumn(first, LOCATION_HEADERS2) : -1;
4337
+ const rows = [];
4338
+ for (let index = headerRow; index < matrix.rows.length; index += 1) {
4339
+ const row = matrix.rows[index] ?? [];
4340
+ const source = (row[sourceColumn] ?? "").trim();
4341
+ const target = (row[targetColumn] ?? "").trim();
4342
+ if (source === "" && target === "") continue;
4343
+ if (source === "") {
4344
+ warnings.push(`row ${index + 1}: empty source`);
4345
+ continue;
4346
+ }
4347
+ const id = (idColumn >= 0 ? (row[idColumn] ?? "").trim() : "") || `row-${index + 1 - headerRow}`;
4348
+ rows.push({
4349
+ id,
4350
+ source,
4351
+ target,
4352
+ ...locationColumn >= 0 && (row[locationColumn] ?? "").trim() !== "" ? { location: (row[locationColumn] ?? "").trim() } : {}
4353
+ });
4354
+ }
4355
+ if (rows.length === 0) throw new Error("bilingual table has no usable rows");
4356
+ return { rows, warnings };
4357
+ }
4358
+ function glossaryFlag(value) {
4359
+ return /^(?:true|1|yes|y|是|禁用)$/iu.test(value.trim());
4360
+ }
4361
+ async function readGlossaryEntries(path, format, sourceLang, targetLang, mapping2) {
4362
+ if (format === "tbx") {
4363
+ if (mapping2 !== void 0) throw new Error("mapping is only supported for CSV/TSV glossaries");
4364
+ const parsed = parseTbx((await readFile(path, "utf8")).replace(/^\uFEFF/u, ""), {
4365
+ ...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
4366
+ ...targetLang === void 0 ? {} : { targetLanguage: targetLang }
4367
+ });
4368
+ return { entries: parsed.entries, warnings: parsed.warnings };
4369
+ }
4370
+ if (mapping2 === void 0) {
4371
+ const text = (await readFile(path, "utf8")).replace(/^\uFEFF/u, "");
4372
+ if (text.includes("\uFFFD")) throw new Error("glossary asset is not valid UTF-8");
4373
+ const parsed = parseGlossaryCsv(text, {
4374
+ ...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
4375
+ ...targetLang === void 0 ? {} : { targetLanguage: targetLang }
4376
+ });
4377
+ return { entries: parsed.entries, warnings: parsed.warnings };
4378
+ }
4379
+ const matrix = await readMatrix(path, format, mapping2);
4380
+ if (matrix.rows.length === 0) throw new Error("glossary table is empty");
4381
+ const warnings = [...matrix.warnings];
4382
+ const first = matrix.rows[0] ?? [];
4383
+ const headerRow = mapping2.headerRow ?? (looksLikeHeader(first) ? 1 : 0);
4384
+ const autoHeader = headerRow === 1 && looksLikeHeader(first);
4385
+ const header = headerRow > 0 ? matrix.rows[headerRow - 1] : void 0;
4386
+ const sourceColumn = mapping2.sourceColumn !== void 0 ? columnIndex(mapping2.sourceColumn, header) : autoHeader ? headerColumn(first, SOURCE_HEADERS3) : 0;
4387
+ const targetColumn = mapping2.targetColumn !== void 0 ? columnIndex(mapping2.targetColumn, header) : autoHeader ? headerColumn(first, TARGET_HEADERS3) : 1;
4388
+ const forbiddenColumn = mapping2.forbiddenColumn !== void 0 ? columnIndex(mapping2.forbiddenColumn, header) : autoHeader ? headerColumn(first, FORBIDDEN_HEADERS2) : -1;
4389
+ if (sourceColumn < 0 || targetColumn < 0) {
4390
+ throw new Error("could not resolve glossary source/target columns; pass mapping.sourceColumn/targetColumn");
4391
+ }
4392
+ const entries = [];
4393
+ for (let index = headerRow; index < matrix.rows.length; index += 1) {
4394
+ const row = matrix.rows[index] ?? [];
4395
+ const source = (row[sourceColumn] ?? "").trim();
4396
+ const target = (row[targetColumn] ?? "").trim();
4397
+ if (source === "" && target === "") continue;
4398
+ if (source === "" || target === "") {
4399
+ warnings.push(`row ${index + 1} is missing ${source === "" ? "source" : "target"}; skipped`);
4400
+ continue;
4401
+ }
4402
+ const forbidden = forbiddenColumn >= 0 ? glossaryFlag(row[forbiddenColumn] ?? "") : false;
4403
+ entries.push({
4404
+ id: `glossary-${entries.length + 1}`,
4405
+ source,
4406
+ target,
4407
+ ...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
4408
+ ...targetLang === void 0 ? {} : { targetLanguage: targetLang },
4409
+ ...forbidden ? { forbidden: true } : {}
4410
+ });
4411
+ }
4412
+ return { entries, warnings };
4413
+ }
4414
+ function bilingualRowsToTm(rows, assetId, sourceLang, targetLang) {
4415
+ return rows.filter((row) => row.target.trim() !== "").map((row) => ({
4416
+ id: `${assetId}:${row.id}`,
4417
+ source: row.source,
4418
+ target: row.target,
4419
+ sourceLanguage: sourceLang,
4420
+ targetLanguage: targetLang,
4421
+ origin: "tm"
4422
+ }));
4423
+ }
4424
+ async function parseAssetFile(absolutePath, kind, format, options) {
4425
+ const warnings = [];
4426
+ if (kind === "tm") {
4427
+ if (format !== "tmx") throw new Error("kind tm requires a .tmx file");
4428
+ const parsed2 = parseTmx((await readFile(absolutePath, "utf8")).replace(/^\uFEFF/u, ""));
4429
+ warnings.push(...parsed2.warnings);
4430
+ const entries2 = parsed2.entries.map((entry) => ({
4431
+ ...entry,
4432
+ id: `${options.id}:${entry.id}`,
4433
+ ...options.sourceLang === void 0 ? {} : { sourceLanguage: options.sourceLang },
4434
+ ...options.targetLang === void 0 ? {} : { targetLanguage: options.targetLang }
4435
+ }));
4436
+ return { counts: { entries: entries2.length }, warnings, tmEntries: entries2, glossary: [] };
4437
+ }
4438
+ if (kind === "tb") {
4439
+ if (format !== "tbx" && format !== "csv" && format !== "tsv") {
4440
+ throw new Error("kind tb requires a .tbx, .csv or .tsv glossary");
4441
+ }
4442
+ const parsed2 = await readGlossaryEntries(
4443
+ absolutePath,
4444
+ format,
4445
+ options.sourceLang,
4446
+ options.targetLang,
4447
+ options.mapping
4448
+ );
4449
+ warnings.push(...parsed2.warnings);
4450
+ const entries2 = parsed2.entries.map((entry) => ({ ...entry, id: `${options.id}:${entry.id}` }));
4451
+ return { counts: { entries: entries2.length }, warnings, tmEntries: [], glossary: entries2 };
4452
+ }
4453
+ if (format !== "csv" && format !== "tsv" && format !== "txt" && format !== "xlsx") {
4454
+ throw new Error("kind bilingual requires a .csv, .tsv, .txt or .xlsx table");
4455
+ }
4456
+ const parsed = await parseBilingualAsset(absolutePath, format, options.mapping);
4457
+ warnings.push(...parsed.warnings);
4458
+ const sourceLang = options.sourceLang;
4459
+ const targetLang = options.targetLang;
4460
+ if (sourceLang === void 0 || targetLang === void 0) {
4461
+ throw new Error("bilingual assets require sourceLang and targetLang (pass them on l10n_asset_add or set them on the project)");
4462
+ }
4463
+ const entries = bilingualRowsToTm(parsed.rows, options.id, sourceLang, targetLang);
4464
+ return { counts: { entries: entries.length }, warnings, tmEntries: entries, glossary: [] };
4465
+ }
4466
+ function kindFromPath(path, format, source) {
4467
+ if (format === "tmx") return { kind: "tm", format };
4468
+ if (format === "tbx") return { kind: "tb", format };
4469
+ if (format === "xlsx") return { kind: "bilingual", format };
4470
+ if (format === "tsv" || format === "txt") return { kind: "bilingual", format };
4471
+ if (format === "csv") {
4472
+ let firstLine = "";
4473
+ try {
4474
+ firstLine = source.split(/\r?\n/u)[0] ?? "";
4475
+ } catch {
4476
+ firstLine = "";
4477
+ }
4478
+ const header = firstLine.split(",").map(normalizeHeader2);
4479
+ if (header.some((cell) => FORBIDDEN_HEADERS2.has(cell))) return { kind: "tb", format };
4480
+ if (header.some((cell) => ID_HEADERS2.has(cell)) || header.some((cell) => LOCATION_HEADERS2.has(cell))) {
4481
+ return { kind: "bilingual", format };
4482
+ }
4483
+ return { kind: "tb", format };
4484
+ }
4485
+ throw new Error(`unsupported asset format: ${path}`);
4486
+ }
4487
+ function absoluteAssetPath(root, path) {
4488
+ return isAbsolute(path) ? resolve(path) : resolve(root, path);
4489
+ }
4490
+ function storedAssetPath(root, absolutePath) {
4491
+ const relativePath = relative(root, absolutePath);
4492
+ if (relativePath === "" || relativePath.startsWith("..") || isAbsolute(relativePath)) return absolutePath;
4493
+ return relativePath.split("\\").join("/");
4494
+ }
4495
+ function nextAssetId(manifest) {
4496
+ const used = new Set(manifest.assets.map((asset) => asset.id));
4497
+ for (let index = 1; index <= MAX_ASSETS + 1; index += 1) {
4498
+ const id = `asset-${index}`;
4499
+ if (!used.has(id)) return id;
4500
+ }
4501
+ throw new Error(`project already has ${MAX_ASSETS} assets`);
4502
+ }
4503
+ async function readAssetBytes(absolutePath) {
4504
+ const info = await stat(absolutePath);
4505
+ if (!info.isFile()) throw new Error(`asset is not a regular file: ${absolutePath}`);
4506
+ if (info.size > MAX_ASSET_BYTES) throw new Error(`asset exceeds ${MAX_ASSET_BYTES} bytes: ${absolutePath}`);
4507
+ const buffer = await readFile(absolutePath);
4508
+ return { size: buffer.byteLength, sha256: createHash2("sha256").update(buffer).digest("hex") };
4509
+ }
4510
+ async function addAsset(handle, options) {
4511
+ const absolutePath = absoluteAssetPath(handle.root, options.path);
4512
+ let info;
4513
+ try {
4514
+ info = await stat(absolutePath);
4515
+ } catch {
4516
+ throw new Error(`asset file does not exist: ${absolutePath}`);
4517
+ }
4518
+ if (!info.isFile()) throw new Error(`asset is not a regular file: ${absolutePath}`);
4519
+ const format = options.format ?? formatOf(absolutePath);
4520
+ let peek = "";
4521
+ if (format === "csv") peek = (await readFile(absolutePath, "utf8")).slice(0, 4096);
4522
+ const inferred = options.kind === void 0 ? kindFromPath(absolutePath, format, peek) : void 0;
4523
+ const kind = options.kind ?? inferred?.kind;
4524
+ if (kind === void 0) throw new Error(`unsupported asset format: ${absolutePath}`);
4525
+ if (kind !== "bilingual" && format !== "tmx" && format !== "tbx" && format !== "csv" && format !== "tsv") {
4526
+ throw new Error(`kind ${kind} does not support format ${format}`);
4527
+ }
4528
+ const sourceLang = options.sourceLang ?? handle.manifest.sourceLang;
4529
+ const targetLang = options.targetLang ?? handle.manifest.targetLang;
4530
+ const id = options.id ?? nextAssetId(handle.manifest);
4531
+ if (!/^[A-Za-z0-9._-]{1,64}$/u.test(id)) throw new Error("asset id must match [A-Za-z0-9._-]{1,64}");
4532
+ if (handle.manifest.assets.some((asset2) => asset2.id === id)) throw new Error(`asset id already exists: ${id}`);
4533
+ if (handle.manifest.assets.some((asset2) => absoluteAssetPath(handle.root, asset2.path) === absolutePath && asset2.kind === kind)) {
4534
+ throw new Error(`asset already registered for ${absolutePath} (${kind}); use l10n_asset_update`);
4535
+ }
4536
+ if (handle.manifest.assets.length >= MAX_ASSETS) throw new Error(`project already has ${MAX_ASSETS} assets`);
4537
+ const { size, sha256 } = await readAssetBytes(absolutePath);
4538
+ const parsed = await parseAssetFile(absolutePath, kind, format, {
4539
+ id,
4540
+ ...sourceLang === void 0 ? {} : { sourceLang },
4541
+ ...targetLang === void 0 ? {} : { targetLang },
4542
+ ...options.mapping === void 0 ? {} : { mapping: options.mapping }
4543
+ });
4544
+ if (parsed.counts.entries === 0) throw new Error("asset contains no usable entries");
4545
+ const now = (/* @__PURE__ */ new Date()).toISOString();
4546
+ const asset = {
4547
+ id,
4548
+ kind,
4549
+ format,
4550
+ path: storedAssetPath(handle.root, absolutePath),
4551
+ enabled: options.enabled !== false,
4552
+ priority: options.priority ?? 100,
4553
+ ...sourceLang === void 0 ? {} : { sourceLang },
4554
+ ...targetLang === void 0 ? {} : { targetLang },
4555
+ sha256,
4556
+ size,
4557
+ counts: parsed.counts,
4558
+ ...options.mapping === void 0 ? {} : { mapping: options.mapping },
4559
+ addedAt: now,
4560
+ updatedAt: now
4561
+ };
4562
+ handle.manifest.assets.push(asset);
4563
+ await writeManifest(handle);
4564
+ return { asset, warnings: parsed.warnings };
4565
+ }
4566
+ async function updateAsset(handle, options) {
4567
+ const asset = handle.manifest.assets.find((item) => item.id === options.id);
4568
+ if (asset === void 0) throw new Error(`unknown asset id: ${options.id}`);
4569
+ const absolutePath = absoluteAssetPath(handle.root, asset.path);
4570
+ const warnings = [];
4571
+ const needsRescan = options.rescan === true || options.mapping !== void 0 || options.sourceLang !== void 0 || options.targetLang !== void 0;
4572
+ if (options.enabled !== void 0) asset.enabled = options.enabled;
4573
+ if (options.priority !== void 0) {
4574
+ if (!Number.isFinite(options.priority)) throw new Error("priority must be a finite number");
4575
+ asset.priority = options.priority;
4576
+ }
4577
+ if (options.sourceLang !== void 0) asset.sourceLang = options.sourceLang;
4578
+ if (options.targetLang !== void 0) asset.targetLang = options.targetLang;
4579
+ if (options.mapping !== void 0) asset.mapping = options.mapping;
4580
+ if (needsRescan) {
4581
+ const parsed = await parseAssetFile(absolutePath, asset.kind, asset.format, {
4582
+ id: asset.id,
4583
+ ...asset.sourceLang === void 0 ? {} : { sourceLang: asset.sourceLang },
4584
+ ...asset.targetLang === void 0 ? {} : { targetLang: asset.targetLang },
4585
+ ...asset.mapping === void 0 ? {} : { mapping: asset.mapping }
4586
+ });
4587
+ if (parsed.counts.entries === 0) throw new Error("asset contains no usable entries after rescan");
4588
+ const { size, sha256 } = await readAssetBytes(absolutePath);
4589
+ asset.counts = parsed.counts;
4590
+ asset.size = size;
4591
+ asset.sha256 = sha256;
4592
+ warnings.push(...parsed.warnings);
4593
+ }
4594
+ asset.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
4595
+ await writeManifest(handle);
4596
+ return { asset, warnings };
4597
+ }
4598
+ async function removeAsset(handle, id) {
4599
+ const index = handle.manifest.assets.findIndex((asset) => asset.id === id);
4600
+ if (index < 0) throw new Error(`unknown asset id: ${id}`);
4601
+ const removed = handle.manifest.assets[index];
4602
+ handle.manifest.assets.splice(index, 1);
4603
+ await writeManifest(handle);
4604
+ return { removed, remaining: handle.manifest.assets.length };
4605
+ }
4606
+ function assetAbsolutePath(handle, asset) {
4607
+ return absoluteAssetPath(handle.root, asset.path);
4608
+ }
4609
+ function normalizeLanguageTag(language) {
4610
+ return language.trim().replace(/_/gu, "-").toLowerCase();
4611
+ }
4612
+ function languageCompatible(actual, wanted) {
4613
+ if (actual === void 0 || wanted === void 0 || wanted.trim() === "") return true;
4614
+ const a = normalizeLanguageTag(actual);
4615
+ const w = normalizeLanguageTag(wanted);
4616
+ if (a === w) return true;
4617
+ return a.split("-")[0] === w.split("-")[0];
4618
+ }
4619
+ async function collectProjectAssets(handle, options = {}) {
4620
+ const tmEntries = [];
4621
+ const glossary = [];
4622
+ const used = [];
4623
+ const warnings = [];
4624
+ const enabled = handle.manifest.assets.filter((asset) => asset.enabled).sort((left, right) => right.priority - left.priority || left.id.localeCompare(right.id));
4625
+ for (const asset of enabled) {
4626
+ if (!languageCompatible(asset.sourceLang, options.sourceLanguage) || !languageCompatible(asset.targetLang, options.targetLanguage)) {
4627
+ continue;
4628
+ }
4629
+ const absolutePath = assetAbsolutePath(handle, asset);
4630
+ let parsed;
4631
+ try {
4632
+ parsed = await parseAssetFile(absolutePath, asset.kind, asset.format, {
4633
+ id: asset.id,
4634
+ ...asset.sourceLang === void 0 ? {} : { sourceLang: asset.sourceLang },
4635
+ ...asset.targetLang === void 0 ? {} : { targetLang: asset.targetLang },
4636
+ ...asset.mapping === void 0 ? {} : { mapping: asset.mapping }
4637
+ });
4638
+ } catch (caught) {
4639
+ warnings.push(`asset ${asset.id} (${asset.path}) could not be parsed: ${caught instanceof Error ? caught.message : String(caught)}`);
4640
+ continue;
4641
+ }
4642
+ tmEntries.push(...parsed.tmEntries);
4643
+ glossary.push(...parsed.glossary);
4644
+ used.push({ id: asset.id, kind: asset.kind, path: asset.path, entries: parsed.counts.entries });
4645
+ }
4646
+ const merged = mergeTmEntries(tmEntries);
4647
+ return { tmEntries: merged.entries, glossary, used, warnings };
4648
+ }
4649
+ async function projectStatus(handle, verify = false) {
4650
+ const assets = [];
4651
+ for (const asset of handle.manifest.assets) {
4652
+ const absolutePath = assetAbsolutePath(handle, asset);
4653
+ let exists = false;
4654
+ let sizeOnDisk;
4655
+ let hashMatch;
4656
+ try {
4657
+ const info = await stat(absolutePath);
4658
+ exists = info.isFile();
4659
+ if (exists) {
4660
+ sizeOnDisk = info.size;
4661
+ if (verify) {
4662
+ const { sha256 } = await readAssetBytes(absolutePath);
4663
+ hashMatch = sha256 === asset.sha256;
4664
+ }
4665
+ }
4666
+ } catch {
4667
+ exists = false;
4668
+ }
4669
+ assets.push({ ...asset, absolutePath, exists, ...sizeOnDisk === void 0 ? {} : { sizeOnDisk }, ...hashMatch === void 0 ? {} : { hashMatch } });
4670
+ }
4671
+ return {
4672
+ root: handle.root,
4673
+ manifestPath: handle.manifestPath,
4674
+ name: handle.manifest.name,
4675
+ sourceLang: handle.manifest.sourceLang,
4676
+ targetLang: handle.manifest.targetLang,
4677
+ createdAt: handle.manifest.createdAt,
4678
+ updatedAt: handle.manifest.updatedAt,
4679
+ qa: handle.manifest.qa,
4680
+ assets,
4681
+ enabledAssets: assets.filter((asset) => asset.enabled).length,
4682
+ trackedFiles: handle.manifest.files.length
4683
+ };
4684
+ }
4685
+ function projectDefaults(handle) {
4686
+ return handle.manifest.qa;
4687
+ }
4688
+
4689
+ // packages/dsh-l10n/src/tools-project.ts
4690
+ var mappingSchema = {
4691
+ type: "object",
4692
+ additionalProperties: false,
4693
+ description: "Bilingual-table layout overrides; omit to auto-detect a source/target header.",
4694
+ properties: {
4695
+ sheet: { type: "string", description: "XLSX worksheet name or 1-based index." },
4696
+ headerRow: { type: "integer", description: "1-based header row; 0 means no header." },
4697
+ sourceColumn: { type: "string", description: "1-based column, spreadsheet letter, or header name." },
4698
+ targetColumn: { type: "string", description: "1-based column, spreadsheet letter, or header name." },
4699
+ forbiddenColumn: { type: "string", description: "Glossary-only forbidden flag column." }
4700
+ }
4701
+ };
4702
+ function mapping(value) {
4703
+ if (value === void 0) return void 0;
4704
+ return value;
4705
+ }
4706
+ function createProjectInitTool() {
4707
+ return defineTool2({
4708
+ name: "l10n_project_init",
4709
+ description: "Create or update a local localization project (.langlink/manifest.json) with language pair and directories. Does not overwrite existing assets unless overwrite=true.",
4710
+ parameters: {
4711
+ root: { type: "string", required: true, description: "Project root directory (created if missing)." },
4712
+ sourceLang: { type: "string", required: true, description: "Source language tag, e.g. en-US." },
4713
+ targetLang: { type: "string", required: true, description: "Target language tag, e.g. zh-CN." },
4714
+ name: { type: "string", description: "Project name; defaults to localization." },
4715
+ locale: { type: "string", description: "Default target locale for QA language-variant rules." },
4716
+ threshold: { type: "integer", description: "Default TM match threshold 50..100 for translate/match tools." },
4717
+ maxMatches: { type: "integer", description: "Default number of TM candidates per segment." },
4718
+ untranslatedCopyWhitelist: {
4719
+ type: "array",
4720
+ items: { type: "string" },
4721
+ description: "Default source texts exempt from UNTRANSLATED_COPY."
4722
+ },
4723
+ overwrite: { type: "boolean", description: "Update language pair/QA settings of an existing manifest; assets are preserved." }
4724
+ },
4725
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4726
+ async execute(args) {
4727
+ if (args.threshold !== void 0 && (args.threshold < 50 || args.threshold > 100)) {
4728
+ throw new Error("threshold must be 50..100");
4729
+ }
4730
+ if (args.maxMatches !== void 0 && args.maxMatches < 1) throw new Error("maxMatches must be >= 1");
4731
+ const handle = await initProject({
4732
+ root: args.root,
4733
+ sourceLang: args.sourceLang,
4734
+ targetLang: args.targetLang,
4735
+ ...args.name === void 0 ? {} : { name: args.name },
4736
+ ...args.locale === void 0 ? {} : { locale: args.locale },
4737
+ ...args.threshold === void 0 ? {} : { threshold: args.threshold },
4738
+ ...args.maxMatches === void 0 ? {} : { maxMatches: args.maxMatches },
4739
+ ...args.untranslatedCopyWhitelist === void 0 ? {} : { untranslatedCopyWhitelist: args.untranslatedCopyWhitelist },
4740
+ ...args.overwrite === void 0 ? {} : { overwrite: args.overwrite }
4741
+ });
4742
+ return JSON.stringify({
4743
+ root: handle.root,
4744
+ manifestPath: handle.manifestPath,
4745
+ name: handle.manifest.name,
4746
+ sourceLang: handle.manifest.sourceLang,
4747
+ targetLang: handle.manifest.targetLang,
4748
+ assets: handle.manifest.assets.length,
4749
+ qa: handle.manifest.qa ?? {}
4750
+ });
4751
+ }
4752
+ });
4753
+ }
4754
+ function createProjectStatusTool() {
4755
+ return defineTool2({
4756
+ name: "l10n_project_status",
4757
+ description: "Read the local project manifest: language pair, QA defaults, tracked files and registered assets (enabled state, counts, existence, optional hash check).",
4758
+ parameters: {
4759
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
4760
+ input: { type: "string", description: "File to discover the project from when project is omitted." },
4761
+ verify: { type: "boolean", description: "Re-hash asset files and report hashMatch; slower for large assets." }
4762
+ },
4763
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4764
+ async execute(args) {
4765
+ const handle = await requireProject(args.project, args.input);
4766
+ const status = await projectStatus(handle, args.verify === true);
4767
+ const warnings = [];
4768
+ for (const asset of status.assets) {
4769
+ if (!asset.exists) warnings.push(`asset ${asset.id} is missing: ${asset.absolutePath}`);
4770
+ else if (asset.hashMatch === false) warnings.push(`asset ${asset.id} changed since import; run l10n_asset_update with rescan=true`);
4771
+ }
4772
+ return JSON.stringify({ ...status, warnings });
4773
+ }
4774
+ });
4775
+ }
4776
+ function createAssetAddTool() {
4777
+ return defineTool2({
4778
+ name: "l10n_asset_add",
4779
+ 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.",
4780
+ parameters: {
4781
+ path: { type: "string", required: true, description: "Asset file path (relative to the project root or absolute)." },
4782
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered from path when omitted." },
4783
+ kind: { type: "string", enum: ["tm", "tb", "bilingual"], description: "Asset kind; auto-detected when omitted." },
4784
+ format: { type: "string", enum: ["tmx", "tbx", "csv", "tsv", "txt", "xlsx"], description: "File format; inferred from extension when omitted." },
4785
+ id: { type: "string", description: "Asset id; defaults to the next asset-N." },
4786
+ enabled: { type: "boolean", description: "Whether the asset participates in matching/QA; default true." },
4787
+ priority: { type: "integer", description: "Higher priority is used first; default 100." },
4788
+ sourceLang: { type: "string", description: "Asset source language; defaults to the project language pair." },
4789
+ targetLang: { type: "string", description: "Asset target language; defaults to the project language pair." },
4790
+ mapping: mappingSchema
4791
+ },
4792
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4793
+ async execute(args) {
4794
+ const handle = await requireProject(args.project, args.path);
4795
+ const result = await addAsset(handle, {
4796
+ path: args.path,
4797
+ ...args.kind === void 0 ? {} : { kind: args.kind },
4798
+ ...args.format === void 0 ? {} : { format: args.format },
4799
+ ...args.id === void 0 ? {} : { id: args.id },
4800
+ ...args.enabled === void 0 ? {} : { enabled: args.enabled },
4801
+ ...args.priority === void 0 ? {} : { priority: args.priority },
4802
+ ...args.sourceLang === void 0 ? {} : { sourceLang: args.sourceLang },
4803
+ ...args.targetLang === void 0 ? {} : { targetLang: args.targetLang },
4804
+ ...mapping(args.mapping) === void 0 ? {} : { mapping: mapping(args.mapping) }
4805
+ });
4806
+ return JSON.stringify({
4807
+ root: handle.root,
4808
+ asset: result.asset,
4809
+ warnings: result.warnings
4810
+ });
4811
+ }
4812
+ });
4813
+ }
4814
+ function createAssetUpdateTool() {
4815
+ return defineTool2({
4816
+ name: "l10n_asset_update",
4817
+ 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.",
4818
+ parameters: {
4819
+ id: { type: "string", required: true, description: "Asset id from l10n_project_status." },
4820
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
4821
+ input: { type: "string", description: "File to discover the project from when project is omitted." },
4822
+ enabled: { type: "boolean", description: "Whether the asset participates in matching/QA." },
4823
+ priority: { type: "integer", description: "Higher priority is used first." },
4824
+ sourceLang: { type: "string", description: "Asset source language." },
4825
+ targetLang: { type: "string", description: "Asset target language." },
4826
+ mapping: mappingSchema,
4827
+ rescan: { type: "boolean", description: "Re-read the file, recompute hash/counts and report changed warnings." }
4828
+ },
4829
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4830
+ async execute(args) {
4831
+ const handle = await requireProject(args.project, args.input);
4832
+ const result = await updateAsset(handle, {
4833
+ id: args.id,
4834
+ ...args.enabled === void 0 ? {} : { enabled: args.enabled },
4835
+ ...args.priority === void 0 ? {} : { priority: args.priority },
4836
+ ...args.sourceLang === void 0 ? {} : { sourceLang: args.sourceLang },
4837
+ ...args.targetLang === void 0 ? {} : { targetLang: args.targetLang },
4838
+ ...mapping(args.mapping) === void 0 ? {} : { mapping: mapping(args.mapping) },
4839
+ ...args.rescan === void 0 ? {} : { rescan: args.rescan }
4840
+ });
4841
+ return JSON.stringify({ root: handle.root, asset: result.asset, warnings: result.warnings });
4842
+ }
4843
+ });
4844
+ }
4845
+ function createAssetRemoveTool() {
4846
+ return defineTool2({
4847
+ name: "l10n_asset_remove",
4848
+ description: "Remove an asset entry from the project manifest. The asset file on disk is left untouched.",
4849
+ parameters: {
4850
+ id: { type: "string", required: true, description: "Asset id from l10n_project_status." },
4851
+ project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
4852
+ input: { type: "string", description: "File to discover the project from when project is omitted." }
4853
+ },
4854
+ output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
4855
+ async execute(args) {
4856
+ const handle = await requireProject(args.project, args.input);
4857
+ const result = await removeAsset(handle, args.id);
4858
+ return JSON.stringify({ root: handle.root, removed: result.removed.id, remaining: result.remaining });
4859
+ }
4860
+ });
4861
+ }
4862
+ async function optionalProject(project, anchor) {
4863
+ return findProject(project, anchor);
4864
+ }
4865
+
3302
4866
  // packages/dsh-l10n/src/tools-phase1.ts
4867
+ import { createHash as createHash3 } from "node:crypto";
4868
+ import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
4869
+ import { basename, resolve as resolve2 } from "node:path";
4870
+ import { defineTool as defineTool3 } from "@deepseek-ai/dsh-tools";
3303
4871
  var MAX_BYTES = 200 * 1024 * 1024;
3304
4872
  async function readBytes(path) {
3305
- const absolute = resolve(path);
3306
- const info = await stat(absolute);
4873
+ const absolute = resolve2(path);
4874
+ const info = await stat2(absolute);
3307
4875
  if (!info.isFile()) throw new Error("path is not a regular file");
3308
4876
  if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
3309
- return readFile(absolute);
4877
+ return readFile2(absolute);
3310
4878
  }
3311
4879
  async function readUtf8(path) {
3312
- const absolute = resolve(path);
3313
- const info = await stat(absolute);
4880
+ const absolute = resolve2(path);
4881
+ const info = await stat2(absolute);
3314
4882
  if (!info.isFile()) throw new Error("path is not a regular file");
3315
4883
  if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
3316
- const value = await readFile(absolute, { encoding: "utf8" });
4884
+ const value = await readFile2(absolute, { encoding: "utf8" });
3317
4885
  if (value.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
3318
4886
  return value;
3319
4887
  }
4888
+ async function readGlossary(path) {
4889
+ const text = await readUtf8(path);
4890
+ return /\.tbx$/iu.test(path) ? parseTbx(text) : parseGlossaryCsv(text);
4891
+ }
4892
+ async function loadDocumentAssets(document, anchor, options) {
4893
+ const warnings = [];
4894
+ const tmEntries = [];
4895
+ const glossary = [];
4896
+ let used = [];
4897
+ const file = document.files[0];
4898
+ let project;
4899
+ if (options.project !== void 0) {
4900
+ project = await findProject(options.project, anchor);
4901
+ } else {
4902
+ try {
4903
+ project = await findProject(void 0, anchor);
4904
+ } catch (caught) {
4905
+ warnings.push(`project ignored: ${caught instanceof Error ? caught.message : String(caught)}`);
4906
+ }
4907
+ }
4908
+ if (project !== void 0) {
4909
+ const collected = await collectProjectAssets(project, {
4910
+ ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
4911
+ ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
4912
+ });
4913
+ tmEntries.push(...collected.tmEntries);
4914
+ glossary.push(...collected.glossary);
4915
+ used = collected.used;
4916
+ warnings.push(...collected.warnings);
4917
+ }
4918
+ if (options.tm !== void 0) tmEntries.push(...parseTmx(await readUtf8(options.tm)).entries);
4919
+ if (options.glossary !== void 0) glossary.push(...(await readGlossary(options.glossary)).entries);
4920
+ return {
4921
+ ...project === void 0 ? {} : { project },
4922
+ tmEntries: mergeTmEntries(tmEntries).entries,
4923
+ glossary,
4924
+ used,
4925
+ warnings
4926
+ };
4927
+ }
3320
4928
  function documentFromXml(xml, path) {
3321
4929
  return parseXliff(xml, { filename: basename(path) });
3322
4930
  }
3323
4931
  function createMatchTmTool() {
3324
- return defineTool({
4932
+ return defineTool3({
3325
4933
  name: "l10n_match_tm",
3326
- description: "Match a local XLIFF/SDLXLIFF file against a TMX 1.4b translation memory. Returns match-rate metadata per segment (no file modification).",
4934
+ 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).",
3327
4935
  parameters: {
3328
4936
  path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3329
- tm: { type: "string", required: true, description: "TMX 1.4b file path." },
3330
- threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
4937
+ tm: { type: "string", description: "Optional TMX 1.4b file path; project assets are used too." },
4938
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
4939
+ threshold: { type: "integer", description: "Minimum matchRate 50..100; default from project or 75." },
3331
4940
  maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
3332
4941
  },
3333
4942
  output: {
@@ -3337,13 +4946,20 @@ function createMatchTmTool() {
3337
4946
  async execute(args) {
3338
4947
  const xml = await readUtf8(args.path);
3339
4948
  const document = documentFromXml(xml, args.path);
3340
- const tm = parseTmx(await readUtf8(args.tm));
3341
- if (tm.entries.length === 0) throw new Error("TMX contains no usable entries");
3342
- const threshold = args.threshold ?? 75;
4949
+ const assets = await loadDocumentAssets(document, args.path, {
4950
+ ...args.project === void 0 ? {} : { project: args.project },
4951
+ ...args.tm === void 0 ? {} : { tm: args.tm }
4952
+ });
4953
+ if (assets.tmEntries.length === 0) {
4954
+ throw new Error("no TM entries: pass tm or register TM/bilingual assets with l10n_asset_add");
4955
+ }
4956
+ const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
4957
+ const threshold = args.threshold ?? defaults?.threshold ?? 75;
4958
+ if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
3343
4959
  const limit = Math.max(1, args.maxSegments ?? 50);
3344
4960
  const matches = document.segments.slice(0, limit).map((segment) => {
3345
4961
  const file = document.files[segment.location.fileIndex];
3346
- const candidates = matchTm(tm.entries, segment.sourceText, {
4962
+ const candidates = matchTm(assets.tmEntries, segment.sourceText, {
3347
4963
  threshold,
3348
4964
  maxMatches: 1,
3349
4965
  ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
@@ -3362,18 +4978,23 @@ function createMatchTmTool() {
3362
4978
  segments: document.segments.length,
3363
4979
  checked: matches.length,
3364
4980
  threshold,
4981
+ tmEntries: assets.tmEntries.length,
4982
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
4983
+ assetsUsed: assets.used,
4984
+ warnings: assets.warnings,
3365
4985
  matches
3366
4986
  });
3367
4987
  }
3368
4988
  });
3369
4989
  }
3370
4990
  function createMatchTermsTool() {
3371
- return defineTool({
4991
+ return defineTool3({
3372
4992
  name: "l10n_match_terms",
3373
- description: "Match a local XLIFF/SDLXLIFF file against a CSV glossary. Returns term hits and forbidden-term counts (no file modification).",
4993
+ 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).",
3374
4994
  parameters: {
3375
4995
  path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3376
- glossary: { type: "string", required: true, description: "CSV glossary path." },
4996
+ glossary: { type: "string", description: "Optional CSV or TBX-Basic/Minis glossary path; project assets are used too." },
4997
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
3377
4998
  maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
3378
4999
  },
3379
5000
  output: {
@@ -3383,15 +5004,21 @@ function createMatchTermsTool() {
3383
5004
  async execute(args) {
3384
5005
  const xml = await readUtf8(args.path);
3385
5006
  const document = documentFromXml(xml, args.path);
3386
- const glossary = parseGlossaryCsv(await readUtf8(args.glossary));
3387
- if (glossary.entries.length === 0) throw new Error("glossary contains no usable entries");
5007
+ const assets = await loadDocumentAssets(document, args.path, {
5008
+ ...args.project === void 0 ? {} : { project: args.project },
5009
+ ...args.glossary === void 0 ? {} : { glossary: args.glossary }
5010
+ });
5011
+ const glossary = assets.glossary;
5012
+ if (glossary.length === 0) {
5013
+ throw new Error("no glossary entries: pass glossary or register TB/CSV assets with l10n_asset_add");
5014
+ }
3388
5015
  const limit = Math.max(1, args.maxSegments ?? 50);
3389
5016
  const hits = [];
3390
5017
  let forbidden = 0;
3391
5018
  for (const segment of document.segments.slice(0, limit)) {
3392
5019
  const file = document.files[segment.location.fileIndex];
3393
5020
  for (const side of ["source", "target"]) {
3394
- const result = matchTerms(segment[`${side}Text`], glossary.entries, {
5021
+ const result = matchTerms(segment[`${side}Text`], glossary, {
3395
5022
  ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
3396
5023
  ...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
3397
5024
  });
@@ -3415,7 +5042,10 @@ function createMatchTermsTool() {
3415
5042
  format: document.format,
3416
5043
  segments: document.segments.length,
3417
5044
  checked: Math.min(limit, document.segments.length),
3418
- entries: glossary.entries.length,
5045
+ entries: glossary.length,
5046
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5047
+ assetsUsed: assets.used,
5048
+ warnings: assets.warnings,
3419
5049
  hits: hits.slice(0, 200),
3420
5050
  hitCount: hits.length,
3421
5051
  forbiddenCount: forbidden
@@ -3424,14 +5054,28 @@ function createMatchTermsTool() {
3424
5054
  });
3425
5055
  }
3426
5056
  function createCheckTool() {
3427
- return defineTool({
5057
+ return defineTool3({
3428
5058
  name: "l10n_check",
3429
- description: "Run deterministic QA on a local XLIFF/SDLXLIFF file, optionally with a TMX and CSV glossary. Returns rule/severity counts and a bounded issue list; never modifies files.",
5059
+ 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.",
3430
5060
  parameters: {
3431
5061
  path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
3432
- tm: { type: "string", description: "Optional TMX 1.4b file path." },
3433
- glossary: { type: "string", description: "Optional CSV glossary path." },
3434
- maxIssues: { type: "integer", description: "Max issues returned; default 100." }
5062
+ tm: { type: "string", description: "Optional TMX 1.4b file path; project TM assets are used too." },
5063
+ glossary: { type: "string", description: "Optional CSV or TBX-Basic/Minis glossary path; project term assets are used too." },
5064
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
5065
+ maxIssues: { type: "integer", description: "Max issues returned; default 100." },
5066
+ sourceWhitelist: {
5067
+ type: "array",
5068
+ items: { type: "string" },
5069
+ description: "Source texts intentionally identical to the target (brand names, codes); exempts UNTRANSLATED_COPY."
5070
+ },
5071
+ shortSegmentMaxLength: {
5072
+ type: "integer",
5073
+ description: "Source length at or below which single-token segments are exempt from UNTRANSLATED_COPY; default 4."
5074
+ },
5075
+ targetLocale: {
5076
+ type: "string",
5077
+ description: "Target locale hint (e.g. zh-CN) used by language-variant rules; defaults to project qa.locale."
5078
+ }
3435
5079
  },
3436
5080
  output: {
3437
5081
  schema: { type: "string" },
@@ -3440,9 +5084,27 @@ function createCheckTool() {
3440
5084
  async execute(args) {
3441
5085
  const xml = await readUtf8(args.path);
3442
5086
  const document = documentFromXml(xml, args.path);
3443
- const tmEntries = args.tm ? parseTmx(await readUtf8(args.tm)).entries : [];
3444
- const glossary = args.glossary ? parseGlossaryCsv(await readUtf8(args.glossary)).entries : [];
3445
- const checked = checkDocument(document, { tmEntries, glossary });
5087
+ const assets = await loadDocumentAssets(document, args.path, {
5088
+ ...args.project === void 0 ? {} : { project: args.project },
5089
+ ...args.tm === void 0 ? {} : { tm: args.tm },
5090
+ ...args.glossary === void 0 ? {} : { glossary: args.glossary }
5091
+ });
5092
+ const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
5093
+ const whitelist = [
5094
+ ...args.sourceWhitelist ?? [],
5095
+ ...defaults?.untranslatedCopyWhitelist ?? []
5096
+ ];
5097
+ const checked = checkDocument(document, {
5098
+ tmEntries: assets.tmEntries,
5099
+ glossary: assets.glossary,
5100
+ ...whitelist.length === 0 && args.shortSegmentMaxLength === void 0 ? {} : {
5101
+ untranslatedCopy: {
5102
+ ...whitelist.length === 0 ? {} : { whitelist: Array.from(new Set(whitelist)) },
5103
+ ...args.shortSegmentMaxLength === void 0 ? {} : { shortSegmentMaxLength: args.shortSegmentMaxLength }
5104
+ }
5105
+ },
5106
+ ...(args.targetLocale ?? defaults?.locale) === void 0 ? {} : { locale: { targetLocale: args.targetLocale ?? defaults?.locale } }
5107
+ });
3446
5108
  const bySeverity = {};
3447
5109
  const byRule = {};
3448
5110
  for (const issue of checked.qa.issues) {
@@ -3456,6 +5118,9 @@ function createCheckTool() {
3456
5118
  checkedSegments: checked.qa.checkedSegments,
3457
5119
  coverage: checked.qa.coverage,
3458
5120
  totalIssues: checked.qa.issues.length,
5121
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5122
+ assetsUsed: assets.used,
5123
+ warnings: assets.warnings,
3459
5124
  bySeverity,
3460
5125
  byRule,
3461
5126
  issues: checked.qa.issues.slice(0, limit).map((issue) => ({
@@ -3473,14 +5138,15 @@ function createCheckTool() {
3473
5138
  });
3474
5139
  }
3475
5140
  function createTranslateTool() {
3476
- return defineTool({
5141
+ return defineTool3({
3477
5142
  name: "l10n_translate",
3478
- description: "Fill empty targets in a local XLIFF/SDLXLIFF file from a TMX, writing a NEW file. Existing targets and tagged segments are never overwritten.",
5143
+ 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.",
3479
5144
  parameters: {
3480
5145
  path: { type: "string", required: true, description: "Input XLIFF/SDLXLIFF path." },
3481
- tm: { type: "string", required: true, description: "TMX 1.4b file path." },
5146
+ tm: { type: "string", description: "Optional TMX 1.4b file path; project TM assets are used too." },
5147
+ project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
3482
5148
  output: { type: "string", required: true, description: "New output file path; must differ from input." },
3483
- threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
5149
+ threshold: { type: "integer", description: "Minimum matchRate 50..100; default from project or 75." },
3484
5150
  setSdlConf: { type: "string", description: "SDLXLIFF only: set sdl:seg@conf on filled segments." }
3485
5151
  },
3486
5152
  output: {
@@ -3492,35 +5158,60 @@ function createTranslateTool() {
3492
5158
  applied: { type: "integer" },
3493
5159
  skipped: { type: "integer" },
3494
5160
  warnings: { type: "array", items: { type: "string" } },
3495
- sha256: { type: "string" }
5161
+ sha256: { type: "string" },
5162
+ project: { type: "string" },
5163
+ assetsUsed: {
5164
+ type: "array",
5165
+ items: {
5166
+ type: "object",
5167
+ additionalProperties: false,
5168
+ properties: {
5169
+ id: { type: "string" },
5170
+ kind: { type: "string" },
5171
+ path: { type: "string" },
5172
+ entries: { type: "integer" }
5173
+ }
5174
+ }
5175
+ }
3496
5176
  }
3497
5177
  },
3498
5178
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3499
5179
  },
3500
5180
  async execute(args) {
3501
- const input = resolve(args.path);
3502
- const output = resolve(args.output);
5181
+ const input = resolve2(args.path);
5182
+ const output = resolve2(args.output);
3503
5183
  if (input === output) throw new Error("refusing to overwrite the input file");
3504
5184
  const xml = await readUtf8(input);
3505
5185
  const document = documentFromXml(xml, input);
3506
- const tm = parseTmx(await readUtf8(args.tm));
3507
- const result = translateDocument(document, tm.entries, {
3508
- threshold: args.threshold ?? 75,
5186
+ const assets = await loadDocumentAssets(document, input, {
5187
+ ...args.project === void 0 ? {} : { project: args.project },
5188
+ ...args.tm === void 0 ? {} : { tm: args.tm }
5189
+ });
5190
+ if (assets.tmEntries.length === 0) {
5191
+ throw new Error("no TM entries: pass tm or register TM/bilingual assets with l10n_asset_add");
5192
+ }
5193
+ const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
5194
+ const threshold = args.threshold ?? defaults?.threshold ?? 75;
5195
+ if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
5196
+ const result = translateDocument(document, assets.tmEntries, {
5197
+ threshold,
3509
5198
  ...args.setSdlConf === void 0 ? {} : { setSdlState: true, sdlConf: args.setSdlConf }
3510
5199
  });
3511
- await writeFile(output, result.xml, "utf8");
5200
+ await writeFile2(output, result.xml, "utf8");
3512
5201
  return {
3513
5202
  output,
3514
5203
  applied: result.applied,
3515
5204
  skipped: result.skipped,
3516
- warnings: result.warnings,
3517
- sha256: createHash2("sha256").update(result.xml, "utf8").digest("hex")
5205
+ warnings: [...assets.warnings, ...result.warnings],
5206
+ sha256: createHash3("sha256").update(result.xml, "utf8").digest("hex"),
5207
+ ...assets.project === void 0 ? {} : { project: assets.project.root },
5208
+ assetsUsed: assets.used
3518
5209
  };
3519
5210
  }
3520
5211
  });
3521
5212
  }
3522
5213
  function createConvertTool() {
3523
- return defineTool({
5214
+ return defineTool3({
3524
5215
  name: "l10n_convert",
3525
5216
  description: "Convert a bilingual CSV/TXT/XLSX table between formats. Writes a new file; never modifies the input.",
3526
5217
  parameters: {
@@ -3541,23 +5232,23 @@ function createConvertTool() {
3541
5232
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3542
5233
  },
3543
5234
  async execute(args) {
3544
- const input = resolve(args.input);
3545
- const output = resolve(args.output);
5235
+ const input = resolve2(args.input);
5236
+ const output = resolve2(args.output);
3546
5237
  if (input === output) throw new Error("refusing to overwrite the input file");
3547
5238
  const table = await parseBilingualFileAsync(input, await readBytes(input));
3548
5239
  if (args.to === "xlsx") {
3549
- await writeFile(output, await writeBilingualXlsx(table.rows));
5240
+ await writeFile2(output, await writeBilingualXlsx(table.rows));
3550
5241
  } else if (args.to === "csv") {
3551
- await writeFile(output, writeBilingualCsv(table.rows, { delimiter: "," }), "utf8");
5242
+ await writeFile2(output, writeBilingualCsv(table.rows, { delimiter: "," }), "utf8");
3552
5243
  } else {
3553
- await writeFile(output, writeBilingualCsv(table.rows, { delimiter: " ", header: false }), "utf8");
5244
+ await writeFile2(output, writeBilingualCsv(table.rows, { delimiter: " ", header: false }), "utf8");
3554
5245
  }
3555
5246
  return { output, rows: table.rows.length, warnings: table.warnings };
3556
5247
  }
3557
5248
  });
3558
5249
  }
3559
5250
  function createValidateTool() {
3560
- return defineTool({
5251
+ return defineTool3({
3561
5252
  name: "l10n_validate",
3562
5253
  description: "Validate one local XLIFF 1.2/SDLXLIFF file: XML well-formedness, segment counts, missing targets, inline-tag mismatches. Read-only.",
3563
5254
  parameters: {
@@ -3589,26 +5280,26 @@ function createValidateTool() {
3589
5280
  }
3590
5281
 
3591
5282
  // packages/dsh-l10n/src/tools-phase2.ts
3592
- import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
3593
- import { basename as basename2, resolve as resolve2 } from "node:path";
3594
- import { defineTool as defineTool2 } from "@deepseek-ai/dsh-tools";
5283
+ import { readFile as readFile3, stat as stat3, writeFile as writeFile3 } from "node:fs/promises";
5284
+ import { basename as basename2, resolve as resolve3 } from "node:path";
5285
+ import { defineTool as defineTool4 } from "@deepseek-ai/dsh-tools";
3595
5286
  var MAX_BYTES2 = 200 * 1024 * 1024;
3596
5287
  async function readBytes2(path) {
3597
- const absolute = resolve2(path);
3598
- const info = await stat2(absolute);
5288
+ const absolute = resolve3(path);
5289
+ const info = await stat3(absolute);
3599
5290
  if (!info.isFile()) throw new Error("path is not a regular file");
3600
5291
  if (info.size > MAX_BYTES2) throw new Error(`file exceeds ${MAX_BYTES2} bytes`);
3601
- return readFile2(absolute);
5292
+ return readFile3(absolute);
3602
5293
  }
3603
5294
  async function readText(path) {
3604
5295
  return (await readBytes2(path)).toString("utf8");
3605
5296
  }
3606
5297
  function createExtractDocTool() {
3607
- return defineTool2({
5298
+ return defineTool4({
3608
5299
  name: "l10n_extract_doc",
3609
- description: "Extract local DOCX paragraph text or XLSX text cells into a bilingual table plus a map.json. Writes new files; the original is never modified.",
5300
+ 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.",
3610
5301
  parameters: {
3611
- path: { type: "string", required: true, description: "DOCX or XLSX file path." },
5302
+ path: { type: "string", required: true, description: "DOCX, XLSX or PPTX file path." },
3612
5303
  rowsOutput: { type: "string", required: true, description: "Bilingual CSV/XLSX output path." },
3613
5304
  mapOutput: { type: "string", required: true, description: "map.json output path." },
3614
5305
  format: { type: "string", enum: ["csv", "xlsx"], description: "Rows output format; default by extension." }
@@ -3628,18 +5319,18 @@ function createExtractDocTool() {
3628
5319
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3629
5320
  },
3630
5321
  async execute(args) {
3631
- const input = resolve2(args.path);
3632
- const rowsOutput = resolve2(args.rowsOutput);
3633
- const mapOutput = resolve2(args.mapOutput);
5322
+ const input = resolve3(args.path);
5323
+ const rowsOutput = resolve3(args.rowsOutput);
5324
+ const mapOutput = resolve3(args.mapOutput);
3634
5325
  if (input === rowsOutput || input === mapOutput) throw new Error("refusing to overwrite the input file");
3635
5326
  const result = extractOoxml(await readBytes2(input), basename2(input));
3636
5327
  const format = args.format ?? (rowsOutput.toLowerCase().endsWith(".xlsx") ? "xlsx" : "csv");
3637
5328
  if (format === "xlsx") {
3638
- await writeFile2(rowsOutput, await writeBilingualXlsx(result.rows));
5329
+ await writeFile3(rowsOutput, await writeBilingualXlsx(result.rows));
3639
5330
  } else {
3640
- await writeFile2(rowsOutput, writeBilingualCsv(result.rows, { delimiter: "," }), "utf8");
5331
+ await writeFile3(rowsOutput, writeBilingualCsv(result.rows, { delimiter: "," }), "utf8");
3641
5332
  }
3642
- await writeFile2(mapOutput, JSON.stringify(result.map, null, 2), "utf8");
5333
+ await writeFile3(mapOutput, JSON.stringify(result.map, null, 2), "utf8");
3643
5334
  return {
3644
5335
  format: result.format,
3645
5336
  rows: result.rows.length,
@@ -3651,14 +5342,14 @@ function createExtractDocTool() {
3651
5342
  });
3652
5343
  }
3653
5344
  function createBackfillTool() {
3654
- return defineTool2({
5345
+ return defineTool4({
3655
5346
  name: "l10n_backfill",
3656
- description: "Backfill translated bilingual rows into a NEW DOCX/XLSX file using a previously generated map.json. The original is never modified.",
5347
+ description: "Backfill translated bilingual rows into a NEW DOCX/XLSX/PPTX file using a previously generated map.json. The original is never modified.",
3657
5348
  parameters: {
3658
- original: { type: "string", required: true, description: "Original DOCX/XLSX path." },
5349
+ original: { type: "string", required: true, description: "Original DOCX/XLSX/PPTX path." },
3659
5350
  map: { type: "string", required: true, description: "map.json path generated by l10n_extract_doc." },
3660
5351
  bilingual: { type: "string", required: true, description: "Translated bilingual CSV/XLSX path." },
3661
- output: { type: "string", required: true, description: "New output DOCX/XLSX path." }
5352
+ output: { type: "string", required: true, description: "New output DOCX/XLSX/PPTX path." }
3662
5353
  },
3663
5354
  output: {
3664
5355
  schema: {
@@ -3674,17 +5365,17 @@ function createBackfillTool() {
3674
5365
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3675
5366
  },
3676
5367
  async execute(args) {
3677
- const original = resolve2(args.original);
3678
- const output = resolve2(args.output);
5368
+ const original = resolve3(args.original);
5369
+ const output = resolve3(args.output);
3679
5370
  if (original === output) throw new Error("refusing to overwrite the input file");
3680
5371
  const map = JSON.parse(await readText(args.map));
3681
- if (map.format !== "docx" && map.format !== "xlsx") throw new Error("invalid map.json");
5372
+ if (map.format !== "docx" && map.format !== "xlsx" && map.format !== "pptx") throw new Error("invalid map.json");
3682
5373
  const table = await parseBilingualFileAsync(args.bilingual, await readBytes2(args.bilingual));
3683
5374
  const translations = new Map(
3684
5375
  table.rows.filter((row) => row.target.trim() !== "").map((row) => [row.id, row.target])
3685
5376
  );
3686
5377
  const result = backfillOoxml(await readBytes2(original), map, translations);
3687
- await writeFile2(output, result.buffer);
5378
+ await writeFile3(output, result.buffer);
3688
5379
  return {
3689
5380
  output,
3690
5381
  applied: result.applied,
@@ -3695,7 +5386,7 @@ function createBackfillTool() {
3695
5386
  });
3696
5387
  }
3697
5388
  function createPolishTool() {
3698
- return defineTool2({
5389
+ return defineTool4({
3699
5390
  name: "l10n_polish",
3700
5391
  description: "Apply reviewed target edits (plain text for untagged segments; inline XML fragments for tagged segments) and write a NEW XLIFF/SDLXLIFF file. Fails closed on tag/structure mismatch.",
3701
5392
  parameters: {
@@ -3733,8 +5424,8 @@ function createPolishTool() {
3733
5424
  render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
3734
5425
  },
3735
5426
  async execute(args) {
3736
- const input = resolve2(args.path);
3737
- const output = resolve2(args.output);
5427
+ const input = resolve3(args.path);
5428
+ const output = resolve3(args.output);
3738
5429
  if (input === output) throw new Error("refusing to overwrite the input file");
3739
5430
  const parsed = JSON.parse(args.edits);
3740
5431
  const edits = /* @__PURE__ */ new Map();
@@ -3751,7 +5442,7 @@ function createPolishTool() {
3751
5442
  if (result.applied === 0 && result.warnings.length > 0) {
3752
5443
  throw new Error(result.warnings.join("; "));
3753
5444
  }
3754
- await writeFile2(output, result.xml, "utf8");
5445
+ await writeFile3(output, result.xml, "utf8");
3755
5446
  return {
3756
5447
  output,
3757
5448
  applied: result.applied,
@@ -3764,11 +5455,62 @@ function createPolishTool() {
3764
5455
  }
3765
5456
 
3766
5457
  // packages/dsh-l10n/src/tools-assets.ts
3767
- import { defineTool as defineTool3 } from "@deepseek-ai/dsh-tools";
5458
+ import { defineTool as defineTool5 } from "@deepseek-ai/dsh-tools";
5459
+ function isPrivateOrReservedHost(hostname) {
5460
+ const host = hostname.toLowerCase().replace(/^\[/u, "").replace(/\]$/u, "");
5461
+ if (host === "localhost" || host.endsWith(".localhost") || host === "::1" || host === "::") return true;
5462
+ if (host.includes(":")) {
5463
+ return /^(fc|fd|fe[89ab])/u.test(host);
5464
+ }
5465
+ const match = host.match(/^\d{1,3}(\.\d{1,3}){3}$/u);
5466
+ if (!match) return false;
5467
+ const octets = match[0].split(".").map(Number);
5468
+ if (octets.some((n) => n > 255)) return true;
5469
+ const a = octets[0];
5470
+ const b = octets[1];
5471
+ const c = octets[2];
5472
+ return a === 0 || a === 10 || a === 127 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || a === 192 && b === 0 && c === 2 || a === 198 && (b === 18 || b === 19) || a >= 224;
5473
+ }
5474
+ function assertSafeAssetBaseUrl(raw, options = {}) {
5475
+ let url;
5476
+ try {
5477
+ url = new URL(raw);
5478
+ } catch {
5479
+ throw new Error("asset base URL is not a valid URL");
5480
+ }
5481
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
5482
+ throw new Error("asset base URL must use http or https");
5483
+ }
5484
+ if (url.pathname !== "/" || url.search !== "" || url.hash !== "") {
5485
+ throw new Error("asset base URL must be a bare origin (no path, query, or fragment)");
5486
+ }
5487
+ if (url.username !== "" || url.password !== "") {
5488
+ throw new Error("asset base URL must not embed credentials");
5489
+ }
5490
+ if (!options.allowPrivateHosts && isPrivateOrReservedHost(url.hostname)) {
5491
+ throw new Error(
5492
+ "asset base URL points to a loopback/private/reserved host; pass allowPrivateHosts (L10N_ASSET_ALLOW_PRIVATE_HOSTS=1) if this is intended"
5493
+ );
5494
+ }
5495
+ return url;
5496
+ }
5497
+ var ASSET_ENDPOINTS = {
5498
+ prod: "https://assets.langlink.tech",
5499
+ staging: "https://assets-staging.langlink.tech",
5500
+ local: "http://127.0.0.1:47160"
5501
+ };
5502
+ function resolveAssetEndpoint(name2) {
5503
+ if (name2 === void 0 || name2 === "") return void 0;
5504
+ const raw = ASSET_ENDPOINTS[name2];
5505
+ if (raw === void 0) {
5506
+ throw new Error(`unknown asset endpoint "${name2}"; expected one of: ${Object.keys(ASSET_ENDPOINTS).join(", ")}`);
5507
+ }
5508
+ return new URL(raw);
5509
+ }
3768
5510
  function createAssetInfoTool(baseUrl) {
3769
- return defineTool3({
5511
+ return defineTool5({
3770
5512
  name: "l10n_assets",
3771
- description: "Read-only company localization asset service: health, glossary entries, or TM entries. Requires L10N_ASSET_BASE_URL.",
5513
+ description: "Read-only company localization asset service: health, glossary entries, or TM entries. Targets the prod endpoint by default.",
3772
5514
  parameters: {
3773
5515
  view: { type: "string", required: true, enum: ["health", "glossary", "tm"], description: "Which asset view to read." },
3774
5516
  maxEntries: { type: "integer", description: "Cap entries returned; default 100." }
@@ -3778,10 +5520,12 @@ function createAssetInfoTool(baseUrl) {
3778
5520
  render: (_args, value) => [{ type: "text", text: value }]
3779
5521
  },
3780
5522
  async execute(args) {
3781
- if (!baseUrl) throw new Error("L10N_ASSET_BASE_URL is not configured");
3782
- const root = baseUrl.replace(/\/$/u, "");
3783
- const path = args.view === "health" ? "/health" : args.view === "glossary" ? "/glossary" : "/tm";
3784
- const response = await fetch(`${root}${path}`, { signal: AbortSignal.timeout(15e3) });
5523
+ if (!baseUrl) throw new Error("asset endpoint is not configured");
5524
+ const path = args.view === "health" ? "health" : args.view === "glossary" ? "glossary" : "tm";
5525
+ const response = await fetch(new URL(path, baseUrl), {
5526
+ signal: AbortSignal.timeout(15e3),
5527
+ redirect: "error"
5528
+ });
3785
5529
  if (!response.ok) throw new Error(`asset service returned ${response.status}`);
3786
5530
  const value = await response.json();
3787
5531
  if (Array.isArray(value.entries) && args.view !== "health") {
@@ -3794,9 +5538,9 @@ function createAssetInfoTool(baseUrl) {
3794
5538
  }
3795
5539
 
3796
5540
  // packages/dsh-l10n/src/tools-semantic.ts
3797
- import { readFile as readFile3 } from "node:fs/promises";
3798
- import { basename as basename3, resolve as resolve3 } from "node:path";
3799
- import { defineTool as defineTool4 } from "@deepseek-ai/dsh-tools";
5541
+ import { readFile as readFile4 } from "node:fs/promises";
5542
+ import { basename as basename3, resolve as resolve4 } from "node:path";
5543
+ import { defineTool as defineTool6 } from "@deepseek-ai/dsh-tools";
3800
5544
  function createDshSemanticProvider(llm, provider, model) {
3801
5545
  return {
3802
5546
  async complete({ prompt }) {
@@ -3816,7 +5560,7 @@ function createDshSemanticProvider(llm, provider, model) {
3816
5560
  };
3817
5561
  }
3818
5562
  function createSemanticCheckTool(llm) {
3819
- return defineTool4({
5563
+ return defineTool6({
3820
5564
  name: "l10n_semantic_check",
3821
5565
  description: "Run semantic localization QA through the dsh LLM service. Requires a configured provider/model; returns validated issues only (evidence must be uniquely locatable).",
3822
5566
  parameters: {
@@ -3831,7 +5575,7 @@ function createSemanticCheckTool(llm) {
3831
5575
  },
3832
5576
  async execute(args) {
3833
5577
  if (!llm) throw new Error("dsh LLM service is unavailable; configure a model provider first");
3834
- const xml = await readFile3(resolve3(args.path), "utf8");
5578
+ const xml = await readFile4(resolve4(args.path), "utf8");
3835
5579
  const document = parseXliff(xml, { filename: basename3(args.path) });
3836
5580
  const result = await runSemanticCheck(
3837
5581
  document,
@@ -3851,17 +5595,17 @@ function createSemanticCheckTool(llm) {
3851
5595
  }
3852
5596
 
3853
5597
  // packages/dsh-l10n/src/tool.ts
3854
- import { readFile as readFile4, stat as stat3 } from "node:fs/promises";
3855
- import { basename as basename4, resolve as resolve4 } from "node:path";
3856
- import { defineTool as defineTool5 } from "@deepseek-ai/dsh-tools";
5598
+ import { readFile as readFile5, stat as stat4 } from "node:fs/promises";
5599
+ import { basename as basename4, resolve as resolve5 } from "node:path";
5600
+ import { defineTool as defineTool7 } from "@deepseek-ai/dsh-tools";
3857
5601
  var MAX_BYTES3 = 200 * 1024 * 1024;
3858
5602
  async function inspectFile(path, signal) {
3859
5603
  signal?.throwIfAborted();
3860
- const absolute = resolve4(path);
3861
- const info = await stat3(absolute);
5604
+ const absolute = resolve5(path);
5605
+ const info = await stat4(absolute);
3862
5606
  if (!info.isFile()) throw new Error("path is not a regular file");
3863
5607
  if (info.size > MAX_BYTES3) throw new Error(`file exceeds ${MAX_BYTES3} bytes`);
3864
- const xml = await readFile4(absolute, { encoding: "utf8", signal });
5608
+ const xml = await readFile5(absolute, { encoding: "utf8", signal });
3865
5609
  if (xml.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
3866
5610
  signal?.throwIfAborted();
3867
5611
  const document = parseXliff(xml, { filename: basename4(absolute) });
@@ -3893,7 +5637,7 @@ async function inspectFile(path, signal) {
3893
5637
  };
3894
5638
  }
3895
5639
  function createInspectTool() {
3896
- return defineTool5({
5640
+ return defineTool7({
3897
5641
  name: "l10n_inspect",
3898
5642
  description: "Inspect a local XLIFF 1.2 or SDLXLIFF file. Returns format, languages, segment count, inline-tag statistics, warnings and a structure hash. Never returns source or target text. Read-only; does not modify the file.",
3899
5643
  parameters: {
@@ -3934,8 +5678,19 @@ function createInspectTool() {
3934
5678
  var name = "langlink-l10n";
3935
5679
  var inject = ["tools"];
3936
5680
  function apply(ctx) {
5681
+ const baseUrl = process.env.WORKBENCH_PLATFORM_URL, token = process.env.WORKBENCH_PLATFORM_TOKEN;
5682
+ if (baseUrl || token) {
5683
+ if (!baseUrl || !token) throw new Error("platform mode requires both URL and Token");
5684
+ ctx.tools.register(createPlatformJobTool({ baseUrl, token }));
5685
+ }
3937
5686
  const llm = ctx.get?.("llm");
5687
+ const assetRoot = () => resolveAssetEndpoint("prod");
3938
5688
  for (const tool of [
5689
+ createProjectInitTool(),
5690
+ createProjectStatusTool(),
5691
+ createAssetAddTool(),
5692
+ createAssetUpdateTool(),
5693
+ createAssetRemoveTool(),
3939
5694
  createInspectTool(),
3940
5695
  createMatchTmTool(),
3941
5696
  createMatchTermsTool(),
@@ -3947,14 +5702,21 @@ function apply(ctx) {
3947
5702
  createBackfillTool(),
3948
5703
  createPolishTool(),
3949
5704
  createSemanticCheckTool(llm),
3950
- createAssetInfoTool(process.env.L10N_ASSET_BASE_URL)
5705
+ createAssetInfoTool(assetRoot())
3951
5706
  ]) {
3952
5707
  ctx.tools.register(tool);
3953
5708
  }
3954
5709
  }
3955
5710
  export {
5711
+ addAsset,
3956
5712
  apply,
5713
+ assertSafeAssetBaseUrl,
5714
+ assetAbsolutePath,
5715
+ collectProjectAssets,
5716
+ createAssetAddTool,
3957
5717
  createAssetInfoTool,
5718
+ createAssetRemoveTool,
5719
+ createAssetUpdateTool,
3958
5720
  createBackfillTool,
3959
5721
  createCheckTool,
3960
5722
  createConvertTool,
@@ -3963,12 +5725,27 @@ export {
3963
5725
  createInspectTool,
3964
5726
  createMatchTermsTool,
3965
5727
  createMatchTmTool,
5728
+ createPlatformJobTool,
3966
5729
  createPolishTool,
5730
+ createProjectInitTool,
5731
+ createProjectStatusTool,
3967
5732
  createSemanticCheckTool,
3968
5733
  createTranslateTool,
3969
5734
  createValidateTool,
5735
+ findProject,
5736
+ initProject,
3970
5737
  inject,
3971
5738
  inspectFile,
3972
- name
5739
+ name,
5740
+ optionalProject,
5741
+ parseAssetFile,
5742
+ projectDefaults,
5743
+ projectStatus,
5744
+ readManifest,
5745
+ removeAsset,
5746
+ requireProject,
5747
+ resolveAssetEndpoint,
5748
+ updateAsset,
5749
+ writeManifest
3973
5750
  };
3974
5751
  //# sourceMappingURL=index.js.map