onionskin-cli 0.0.5 → 0.0.6

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/README.md +4 -4
  3. package/package.json +1 -1
  4. package/scripts/checker/check.py +24 -5
  5. package/scripts/checker/requirements.txt +482 -5
  6. package/scripts/requirements.txt +482 -5
  7. package/scripts/rewriter/apple/main.swift +1 -1
  8. package/scripts/rewriter/apple/onionskin-fm +0 -0
  9. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  10. package/skills/onionskin/SKILL.md +4 -4
  11. package/skills/onionskin/reference/ops.md +1 -1
  12. package/src/agent-runner.js +94 -15
  13. package/src/checker.js +238 -33
  14. package/src/cli.js +173 -33
  15. package/src/doctor.js +12 -3
  16. package/src/docx.js +130 -63
  17. package/src/fsutil.js +13 -5
  18. package/src/history.js +11 -2
  19. package/src/html-render.js +74 -15
  20. package/src/html-utf8.js +1 -1
  21. package/src/html.js +3 -1
  22. package/src/merge-package.js +82 -26
  23. package/src/offload.js +25 -6
  24. package/src/ops/docx-table.js +5 -3
  25. package/src/ops/pptx.js +8 -2
  26. package/src/ops/xlsx.js +114 -8
  27. package/src/package.js +17 -2
  28. package/src/pptx.js +244 -8
  29. package/src/question.js +10 -3
  30. package/src/quick.js +1 -1
  31. package/src/redline-check.js +12 -2
  32. package/src/rewriter.js +38 -8
  33. package/src/server-entry.js +4 -3
  34. package/src/server.js +97 -28
  35. package/src/session.js +13 -2
  36. package/src/setup.js +20 -2
  37. package/src/update.js +248 -19
  38. package/src/voice-guide.js +12 -7
  39. package/src/voice-learn.js +11 -1
  40. package/src/voice-store.js +11 -4
  41. package/src/voice.js +2 -1
  42. package/src/xlsx.js +25 -5
  43. package/src/zip-write.js +8 -4
  44. package/src/zip.js +64 -13
  45. package/web/dist/assets/{index-DytKkMdi.js → index-CjsTf5gj.js} +108 -106
  46. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  47. package/web/dist/index.html +2 -2
  48. package/web/dist/assets/index-DkNx0LSH.css +0 -1
package/src/ops/xlsx.js CHANGED
@@ -2,6 +2,8 @@ import { checkExpect, colIndex, colLetters, engineFailure, fail, parseA1, refuse
2
2
  import { openEngine } from "./engine.js";
3
3
  import { countMatches, findRanges } from "./runs.js";
4
4
  import { a1Range, parseWorkbook } from "../xlsx.js";
5
+ import { openZip } from "../zip.js";
6
+ import { walkXml } from "../xml.js";
5
7
 
6
8
  /**
7
9
  * XLSX driver (headless Engine: xlsx.set, xlsx.format, xlsx.sheetOp, xlsx.bookOp, xlsx.replace). Cell text for `expect` is what a
@@ -90,20 +92,108 @@ async function colText(st, sh, c) {
90
92
 
91
93
  const input = (v) => (v === null ? "" : typeof v === "boolean" ? (v ? "TRUE" : "FALSE") : String(v));
92
94
 
95
+ // Give equal decimal spellings the same signed coefficient and power of ten.
96
+ function normalizedDecimal(text) {
97
+ const [mantissa, exponent = "0"] = text.split(/e/i);
98
+ const [whole, fraction = ""] = mantissa.replace(/^[-+]/, "").split(".");
99
+ const digits = (whole + fraction).replace(/^0+/, "");
100
+ if (!digits) return "0";
101
+ const coefficient = digits.replace(/0+$/, "");
102
+ const power = BigInt(exponent) - BigInt(fraction.length) + BigInt(digits.length - coefficient.length);
103
+ return `${mantissa.startsWith("-") ? "-" : ""}${coefficient}e${power}`;
104
+ }
105
+
106
+ // The numeric and percent patterns mirror parseInput in vendor/engine-node/engine-node.mjs.
107
+ export function numberWriteRefusal(value, r, c) {
108
+ if (typeof value !== "string") return null;
109
+ const text = value.trim();
110
+ const match = /^[-+]?(\d+\.?\d*|\.\d+)(?:e[-+]?\d+|%)?$/i.exec(text);
111
+ if (!match) return null;
112
+ const percent = text.endsWith("%");
113
+ const body = percent ? text.slice(0, -1) : text;
114
+ const number = Number(body);
115
+ let reason;
116
+ if (/^0\d/.test(match[1])) reason = "leading zeros are not kept in this version";
117
+ else if (!Number.isFinite(number) || normalizedDecimal(body) !== normalizedDecimal(String(number))) reason = "the number cannot be stored exactly";
118
+ else return null;
119
+ return refused("number_would_change", `${colLetters(c)}${r + 1} would be saved as the number ${percent ? number / 100 : number} instead of the text ${JSON.stringify(value)}: ${reason}`);
120
+ }
121
+
93
122
  const STRUCTURAL = new Set(["insert_rows", "delete_rows", "insert_columns", "delete_columns"]);
94
123
 
124
+ // Mirror the two dateSerial2 patterns in vendor/engine-node/engine-node.mjs.
125
+ function invalidDatePart(value) {
126
+ const text = value.trim();
127
+ const iso = /^(\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2}))?)?$/.exec(text);
128
+ const m = iso ?? /^(\d{1,2})\/(\d{1,2})\/(\d{4})(?: (\d{1,2}):(\d{2})(?::(\d{2}))?)?$/.exec(text);
129
+ if (!m) return null;
130
+ const month = Number(m[2]), day = Number(m[iso ? 3 : 1]), year = Number(m[iso ? 1 : 3]);
131
+ const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
132
+ if (month < 1 || month > 12 || day < 1 || day > [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]) return "day";
133
+ return Number(m[4]) > 23 || Number(m[5]) > 59 || Number(m[6]) > 59 ? "time" : null;
134
+ }
135
+
136
+ async function setCells(st, sh, cells) {
137
+ await st.h.engine.call("xlsx.set", [sh, cells]);
138
+ // The live format decides whether the engine parses a date. Discard this in-memory edit if it converted an impossible date or time.
139
+ for (const { r, c, input } of cells) {
140
+ const invalid = invalidDatePart(input);
141
+ if (invalid && await cellText(st, sh, r, c) !== input)
142
+ return { ...refused("invalid_date", `${colLetters(c)}${r + 1} has a date format and ${JSON.stringify(input)} names a ${invalid} that does not exist`), dirty: true };
143
+ }
144
+ return null;
145
+ }
146
+
147
+ function addressRefusal(st, sh) {
148
+ const sheet = st.sheets[sh];
149
+ if (sheet.implicitCells === undefined) {
150
+ const xml = (st.zip ??= openZip(st.bytes)).readText(sheet.path);
151
+ if (xml == null) return refused("sheet_unreadable", "the worksheet could not be checked for cell addresses");
152
+ let implicit = false;
153
+ const stack = [];
154
+ walkXml(xml, {
155
+ onOpen(name, attrs) {
156
+ const tag = name.slice(name.indexOf(":") + 1);
157
+ if (tag === "c" && stack.at(-1) === "row" && stack.at(-2) === "sheetData" && !attrs.r) implicit = true;
158
+ stack.push(tag);
159
+ },
160
+ onClose() { stack.pop(); },
161
+ });
162
+ sheet.implicitCells = implicit;
163
+ }
164
+ return sheet.implicitCells ? refused("cells_without_address", `sheet "${sheet.name}" stores cells without their addresses. Edits to it are not supported in this version.`) : null;
165
+ }
166
+
95
167
  /** Phase 1 (before any op ran): sheet and range exist, `expect` holds. Async because it reads cells through the engine. */
96
168
  export async function bind(st, op) {
97
169
  const b = bindShape(st, op);
98
170
  if (!b.ok) return b;
171
+ // Moving addresses or renaming a sheet can also rewrite formulas on other sheets.
172
+ const sheets = STRUCTURAL.has(op.op) || op.op === "rename_sheet" ? st.sheets.keys() : b.sh === undefined ? [] : [b.sh];
173
+ for (const sh of sheets) {
174
+ const bad = addressRefusal(st, sh);
175
+ if (bad) return bad;
176
+ }
99
177
  // Addresses and `expect` are about the file as it is now. After rows or columns moved they would mean something else.
100
178
  if (b.sh !== undefined && st.shifted?.has(b.sh)) return refused("after_structural_edit", "an earlier op of this file inserted or deleted rows/columns on this sheet; send this op in a separate apply call");
179
+ if (op.op === "replace_all_text" && st.shifted?.size > 0) return refused("after_structural_edit", "an earlier op of this file inserted or deleted rows/columns on a sheet; send this op in a separate apply call");
101
180
  const bad = await checkRegion(st, b);
102
181
  if (bad) return bad;
103
182
  if (STRUCTURAL.has(op.op)) (st.shifted ??= new Set()).add(b.sh);
104
183
  if (op.op === "replace_all_text") {
105
184
  const hits = await allMatches(st, op);
185
+ for (const sh of new Set(hits.map((x) => x.sh))) {
186
+ const bad = addressRefusal(st, sh);
187
+ if (bad) return bad;
188
+ }
106
189
  const total = hits.reduce((n, x) => n + x.n, 0);
190
+ // The read-only preview binds with count zero. At execution an earlier op may have changed the cell text.
191
+ if (op.expect.count === 0) {
192
+ for (const x of hits) {
193
+ const cell = replacementCell(x, x.t, op);
194
+ if (cell.code) return cell;
195
+ }
196
+ }
107
197
  // `t` is the formula when a cell has one, so a hit whose text starts with "=" would rewrite a formula
108
198
  const extra = { partial_words: hits.reduce((n, x) => n + x.partial, 0), in_formulas: hits.reduce((n, x) => n + (x.t.startsWith("=") ? x.n : 0), 0) };
109
199
  if (total !== op.expect.count) return fail("expect_mismatch", `found ${total} occurrence(s), expected ${op.expect.count}`, { found: total, ...extra });
@@ -141,6 +231,10 @@ function bindShape(st, op) {
141
231
  const hit = written.find((x) => x.r >= m.r0 && x.r <= m.r1 && x.c >= m.c0 && x.c <= m.c1 && !(x.r === m.r0 && x.c === m.c0));
142
232
  if (hit) return refused("merged_cell", `${colLetters(hit.c)}${hit.r + 1} is inside the merged range ${a1Range(m)}, which shows only its top-left cell ${colLetters(m.c0)}${m.r0 + 1}: write there, or pick a cell outside the range`);
143
233
  }
234
+ for (const { v, r, c } of written) {
235
+ const bad = numberWriteRefusal(v, r, c);
236
+ if (bad) return bad;
237
+ }
144
238
  }
145
239
  return { ok: true, op, sh, rect, phase: "region" };
146
240
  }
@@ -209,6 +303,16 @@ async function allMatches(st, op) {
209
303
  return out;
210
304
  }
211
305
 
306
+ function replacementCell(x, text, op) {
307
+ const ranges = findRanges(text, op.find, op.match_case !== false);
308
+ if (ranges.length !== x.n) return { ...refused("incomplete_replace", `cell ${colLetters(x.c)}${x.r + 1} changed before replace_all_text ran`), dirty: true };
309
+ let next = "";
310
+ let cur = 0;
311
+ for (const { start, end } of ranges) (next += text.slice(cur, start) + op.replace), (cur = end);
312
+ next += text.slice(cur);
313
+ return numberWriteRefusal(next, x.r, x.c) ?? { sh: x.sh, r: x.r, c: x.c, input: next };
314
+ }
315
+
212
316
  export async function exec(st, b) {
213
317
  const { op } = b;
214
318
  try {
@@ -217,14 +321,15 @@ export async function exec(st, b) {
217
321
  const total = b.hits.reduce((n, x) => n + x.n, 0);
218
322
  const byFormula = [];
219
323
  for (const x of b.hits) {
220
- // the same matches the count above came from (findRanges), not a second reading of "case-insensitive" by a RegExp
221
- let next = "";
222
- let cur = 0;
223
- for (const { start, end } of findRanges(x.t, op.find, op.match_case !== false)) (next += x.t.slice(cur, start) + op.replace), (cur = end);
224
- next += x.t.slice(cur);
225
- byFormula.push({ sh: x.sh, r: x.r, c: x.c, input: next });
324
+ const live = await cellText(st, x.sh, x.r, x.c);
325
+ const cell = replacementCell(x, live, op);
326
+ if (cell.code) return cell;
327
+ byFormula.push(cell);
328
+ }
329
+ for (const sh of new Set(byFormula.map((x) => x.sh))) {
330
+ const bad = await setCells(st, sh, byFormula.filter((x) => x.sh === sh));
331
+ if (bad) return bad;
226
332
  }
227
- for (const sh of new Set(byFormula.map((x) => x.sh))) await st.h.engine.call("xlsx.set", [sh, byFormula.filter((x) => x.sh === sh).map(({ r, c, input }) => ({ r, c, input }))]);
228
333
  if (byFormula.length !== b.hits.length) return { ...refused("incomplete_replace", "not every matching cell was rewritten"), dirty: true };
229
334
  return { status: "applied", detail: { replaced: total, cells: byFormula.length } };
230
335
  }
@@ -233,7 +338,8 @@ export async function exec(st, b) {
233
338
  const flat = Array.isArray(op.values) ? op.values : [[op.values]];
234
339
  const cells = [];
235
340
  flat.forEach((row, i) => row.forEach((v, j) => cells.push({ r: b.rect.r0 + i, c: b.rect.c0 + j, input: input(v) })));
236
- await st.h.engine.call("xlsx.set", [b.sh, cells]);
341
+ const bad = await setCells(st, b.sh, cells);
342
+ if (bad) return bad;
237
343
  return { status: "applied", detail: { cells: cells.length } };
238
344
  }
239
345
  case "format_cells": {
package/src/package.js CHANGED
@@ -38,6 +38,15 @@ export function validatePackage(bytes, format) {
38
38
  return zip;
39
39
  }
40
40
 
41
+ function hasZipEntry(zip, clean) {
42
+ if (zip.entries.has(clean)) return true;
43
+ try {
44
+ return zip.entries.has(decodeURIComponent(clean));
45
+ } catch {
46
+ return false;
47
+ }
48
+ }
49
+
41
50
  function relTargets(zip) {
42
51
  const missing = [];
43
52
  let parts = 0;
@@ -56,7 +65,7 @@ function relTargets(zip) {
56
65
  if (tag !== "Relationship" || attrs.TargetMode === "External" || !attrs.Target) return;
57
66
  const target = attrs.Target.startsWith("/") ? attrs.Target.slice(1) : path.posix.normalize(path.posix.join(base, attrs.Target));
58
67
  const clean = target.split("#")[0];
59
- if (clean && !zip.entries.has(clean)) missing.push({ rels: name, target: clean });
68
+ if (clean && !hasZipEntry(zip, clean)) missing.push({ rels: name, target: clean });
60
69
  },
61
70
  });
62
71
  }
@@ -87,7 +96,13 @@ export function partRels(zip, part) {
87
96
  onOpen(tag, a) {
88
97
  if (tag !== "Relationship" || !a.Type || !a.Target) return;
89
98
  const external = a.TargetMode === "External";
90
- const target = external ? a.Target : a.Target.startsWith("/") ? a.Target.slice(1) : path.posix.normalize(path.posix.join(base === "." ? "" : base, a.Target.split("#")[0]));
99
+ let target = external ? a.Target : a.Target.startsWith("/") ? a.Target.slice(1) : path.posix.normalize(path.posix.join(base === "." ? "" : base, a.Target.split("#")[0]));
100
+ if (!external && !zip.entries.has(target)) {
101
+ try {
102
+ const dec = decodeURIComponent(target);
103
+ if (zip.entries.has(dec)) target = dec;
104
+ } catch {}
105
+ }
91
106
  out.push({ id: a.Id, type: a.Type.split("/").pop(), target, external });
92
107
  },
93
108
  });
package/src/pptx.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { attrOf, local, readRels } from "./xlsx.js";
2
2
  import { readBudget, tooMany, walkXml } from "./xml.js";
3
- import { affords, hitsOf, normPaid, normText, searchCost, similarity, spend, textHash, weigh, weightOf } from "./text.js";
3
+ import { OCCURRENCE, affords, hitsOf, normPaid, normText, searchCost, similarity, spend, textHash, weigh, weightOf } from "./text.js";
4
+ import { nfc } from "./nfc.js";
4
5
  import { openZip } from "./zip.js";
5
6
  import { summarizeRuns } from "./fmt.js";
6
7
  import { untrusted } from "./sanitize.js";
@@ -66,8 +67,13 @@ function readSlide(zip, part, count = { shapes: 0, budget: deckBudget() }) {
66
67
  let inT = false;
67
68
  const seen = new Map();
68
69
  const at = []; // open element names, to tell a text color (rPr > solidFill > srgbClr) from any other color in the slide
70
+ let mcFallbackDepth = 0;
69
71
  walkXml(xml, {
70
72
  onOpen(n, a) {
73
+ if (n === "mc:Fallback" || mcFallbackDepth > 0) {
74
+ if (n === "mc:Fallback") mcFallbackDepth++;
75
+ return;
76
+ }
71
77
  const l = local(n);
72
78
  at.push(l);
73
79
  if (SHAPE_TAGS.has(l) && ++count.shapes > MAX_SHAPES) throw tooMany(count.budget, MAX_SHAPES, "shapes");
@@ -79,7 +85,7 @@ function readSlide(zip, part, count = { shapes: 0, budget: deckBudget() }) {
79
85
  else if (top.run && (l === "srgbClr" || l === "schemeClr") && at[at.length - 2] === "solidFill" && at[at.length - 3] === "rPr") top.run.p.color = String(a.val || "").toUpperCase();
80
86
  else if (top.run && l === "latin" && at[at.length - 2] === "rPr") top.run.p.font = a.typeface || "";
81
87
  if (l === "cNvPr" && top && top.id === null) {
82
- top.id = a.id != null ? Number(a.id) : null;
88
+ top.id = /^\d+$/.test(a.id ?? "") ? Number(a.id) : null;
83
89
  top.name = a.name ?? "";
84
90
  } else if (l === "ph" && top) {
85
91
  top.ph = a.type || "object"; // no type = a content placeholder
@@ -91,9 +97,14 @@ function readSlide(zip, part, count = { shapes: 0, budget: deckBudget() }) {
91
97
  else if (l === "tc" && top) top.cellStart = true;
92
98
  },
93
99
  onText(t) {
100
+ if (mcFallbackDepth > 0) return;
94
101
  if (inT && para !== null) para += t;
95
102
  },
96
103
  onClose(n) {
104
+ if (mcFallbackDepth > 0) {
105
+ if (n === "mc:Fallback") mcFallbackDepth--;
106
+ return;
107
+ }
97
108
  const l = local(n);
98
109
  at.pop();
99
110
  if (l === "r" && stack.length) {
@@ -163,20 +174,222 @@ export function pptxDurable(deck, loc, ctx = {}) {
163
174
  // the anchor is the shape, without the text range
164
175
  const q = normPaid(ctx.quote) ?? "";
165
176
  const raw = q ? ctx.quote : "";
166
- const at = q && spend(textCost(sh, raw)) ? sh.text.indexOf(raw) : -1;
177
+ const prefix = (normPaid(ctx.prefix) ?? "").slice(-CTX);
178
+ const suffix = (normPaid(ctx.suffix) ?? "").slice(0, CTX);
179
+ const hintStart = Number.isInteger(loc.start) ? loc.start : null;
180
+ const ctxOpts = { prefix, suffix, nth: ctx.nth ?? ctx.occ ?? null, hintStart };
181
+ let at = -1;
182
+ let len = raw.length;
183
+ let k = null;
184
+ if (q && !ctx.shapeWide) {
185
+ const preferNorm = raw !== q || (q.includes(" ") && HAS_NON_PLAIN_WS.test(sh.text));
186
+ if (preferNorm) {
187
+ const m = findNormMatch(sh, q, ctxOpts);
188
+ if (m) [at, len, k] = [m.start, m.end - m.start, m.k];
189
+ } else {
190
+ const exact = spend(textCost(sh, raw)) ? findExactMatch(sh, raw, ctxOpts) : null;
191
+ if (exact) [at, len, k] = [exact.start, exact.end - exact.start, exact.k];
192
+ else if (exact === undefined) {
193
+ const m = findNormMatch(sh, q, ctxOpts);
194
+ if (m) [at, len, k] = [m.start, m.end - m.start, m.k];
195
+ }
196
+ }
197
+ }
167
198
  return {
168
199
  ...base,
169
200
  shapeId: sh.id, shapeName: sh.name, ordinal: sh.ordinal, kind: sh.kind,
170
201
  text: spend(normCost(sh)) ? normShape(sh).slice(0, KEEP) : "", textHash: sh.hash,
171
202
  // a text range inside the shape: the quote and what surrounds it
172
203
  quote: ctx.shapeWide ? "" : q,
173
- prefix: (normPaid(ctx.prefix) ?? "").slice(-CTX),
174
- suffix: (normPaid(ctx.suffix) ?? "").slice(0, CTX),
204
+ prefix,
205
+ suffix,
175
206
  start: !ctx.shapeWide && at >= 0 ? at : null,
176
- end: !ctx.shapeWide && at >= 0 ? at + raw.length : null,
207
+ end: !ctx.shapeWide && at >= 0 ? at + len : null,
208
+ nth: !ctx.shapeWide && at >= 0 ? k : null,
177
209
  };
178
210
  }
179
211
 
212
+ const MARK = /\p{M}/u;
213
+ const WS_CHAR = /\s/;
214
+ export const HAS_NON_PLAIN_WS = /[\t\r\n\f\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]| /;
215
+ /**
216
+ * May a window start (or end) right before text[i]? Yes below U+0300 (no such character joins the one before it under NFC); never inside a
217
+ * surrogate pair or before a combining mark; otherwise when the character normalizes the same after the three before it as on its own
218
+ * (the longest chain of starters that compose, Hangul L V T and its like, is three: test/parse-limits.test.js checks every code point).
219
+ * Exported for src/docx.js and its test.
220
+ */
221
+ export function cuttable(text, i) {
222
+ if (i <= 0 || i >= text.length) return true;
223
+ const c = text.charCodeAt(i);
224
+ if (c < 0x300) return true;
225
+ if (c >= 0xdc00 && c <= 0xdfff) return false;
226
+ const ch = String.fromCodePoint(text.codePointAt(i));
227
+ if (MARK.test(ch)) return false;
228
+ const before = text.slice(Math.max(0, i - 3), i);
229
+ return (before + ch).normalize("NFC") === before.normalize("NFC") + ch.normalize("NFC");
230
+ }
231
+
232
+ function buildNormMap(text) {
233
+ const parts = [];
234
+ const starts = [];
235
+ const ends = [];
236
+ let seenNonWs = false;
237
+ for (let i = 0; i < text.length; ) {
238
+ if (WS_CHAR.test(text[i])) {
239
+ let j = i + 1;
240
+ while (j < text.length && WS_CHAR.test(text[j])) j++;
241
+ if (seenNonWs && j < text.length) {
242
+ parts.push(" ");
243
+ starts.push(i);
244
+ ends.push(j);
245
+ }
246
+ i = j;
247
+ continue;
248
+ }
249
+ let j = i + (text.codePointAt(i) > 0xffff ? 2 : 1);
250
+ while (j < text.length && !WS_CHAR.test(text[j]) && !cuttable(text, j)) {
251
+ j += text.codePointAt(j) > 0xffff ? 2 : 1;
252
+ }
253
+ const chunk = nfc(text.slice(i, j));
254
+ if (chunk) {
255
+ parts.push(chunk);
256
+ for (let k = 0; k < chunk.length; k++) {
257
+ starts.push(i);
258
+ ends.push(j);
259
+ }
260
+ seenNonWs = true;
261
+ }
262
+ i = j;
263
+ }
264
+ return { norm: parts.join(""), starts: Int32Array.from(starts), ends: Int32Array.from(ends) };
265
+ }
266
+
267
+ const NORM_MAP = new WeakMap();
268
+ const NORM_HITS = new WeakMap();
269
+ const NORM_OCC = new WeakMap();
270
+ const RAW_OCC = new WeakMap();
271
+ const OCC_KEEP = 16;
272
+ export function normMapFor(unit) {
273
+ let map = NORM_MAP.get(unit);
274
+ if (map === undefined) {
275
+ if (!spend(weigh(unit.text))) return null;
276
+ NORM_MAP.set(unit, (map = buildNormMap(unit.text)));
277
+ }
278
+ return map;
279
+ }
280
+ export function normOccurrences(unit, q, max = Infinity) {
281
+ const nq = normText(q);
282
+ if (!nq) return [];
283
+ if (max === Infinity) {
284
+ const cached = NORM_OCC.get(unit)?.get(nq);
285
+ if (cached) return cached;
286
+ }
287
+ const map = normMapFor(unit);
288
+ if (!map) return null;
289
+ if (!spend(searchCost(map.norm.length, nq, hitsOf(NORM_HITS, unit, map.norm, nq)))) return null;
290
+ const out = [];
291
+ for (let i = map.norm.indexOf(nq); i >= 0 && out.length < max; i = map.norm.indexOf(nq, i + nq.length)) {
292
+ if (!spend(OCCURRENCE)) return null;
293
+ out.push({ start: map.starts[i], end: map.ends[i + nq.length - 1], normStart: i, normEnd: i + nq.length });
294
+ }
295
+ if (max === Infinity) {
296
+ let m = NORM_OCC.get(unit);
297
+ if (!m) NORM_OCC.set(unit, (m = new Map()));
298
+ if (m.size >= OCC_KEEP) m.delete(m.keys().next().value);
299
+ m.set(nq, out);
300
+ }
301
+ return out;
302
+ }
303
+
304
+ function exactOccurrencesIn(sh, raw, first, second, ctxPerOcc = 0) {
305
+ let m = RAW_OCC.get(sh);
306
+ const cached = m?.get(raw);
307
+ if (cached) return cached;
308
+ if (!spend(2 * OCCURRENCE)) return null;
309
+ const occ = [first, second];
310
+ for (let i = sh.text.indexOf(raw, second + raw.length); i >= 0; i = sh.text.indexOf(raw, i + raw.length)) {
311
+ if (ctxPerOcc && !affords((occ.length + 1) * ctxPerOcc)) return null;
312
+ if (!spend(OCCURRENCE)) return null;
313
+ occ.push(i);
314
+ }
315
+ if (!m) RAW_OCC.set(sh, (m = new Map()));
316
+ if (m.size >= OCC_KEEP) m.delete(m.keys().next().value);
317
+ m.set(raw, occ);
318
+ return occ;
319
+ }
320
+
321
+ function findExactMatch(sh, raw, { prefix = "", suffix = "", nth = null, hintStart = null } = {}) {
322
+ const first = sh.text.indexOf(raw);
323
+ if (first < 0) return undefined;
324
+ const second = sh.text.indexOf(raw, first + raw.length);
325
+ if (second < 0) return { start: first, end: first + raw.length, k: 0 };
326
+ if (Number.isInteger(hintStart) && hintStart >= 0 && sh.text.slice(hintStart, hintStart + raw.length) === raw) {
327
+ let k = 0;
328
+ let found = false;
329
+ for (let i = first; i >= 0 && i <= hintStart; i = sh.text.indexOf(raw, i + raw.length)) {
330
+ if (!spend(OCCURRENCE)) {
331
+ k = null;
332
+ break;
333
+ }
334
+ if (i === hintStart) {
335
+ found = true;
336
+ break;
337
+ }
338
+ k++;
339
+ }
340
+ return { start: hintStart, end: hintStart + raw.length, k: found ? k : null };
341
+ }
342
+ const pre = normText(prefix).slice(-CTX);
343
+ const suf = normText(suffix).slice(0, CTX);
344
+ const ctxPerOcc = pre || suf ? 2 * (pre.length + suf.length + 16) : 0;
345
+ const occ = exactOccurrencesIn(sh, raw, first, second, ctxPerOcc);
346
+ if (!occ) return null;
347
+ if (ctxPerOcc && !spend(occ.length * ctxPerOcc)) return null;
348
+ const guarded = [];
349
+ for (let i = 0; i < occ.length; i++) {
350
+ const at = occ[i];
351
+ const okPre = !pre || normText(sh.text.slice(Math.max(0, at - 4 * pre.length - 16), at)).endsWith(pre);
352
+ const okSuf = !suf || normText(sh.text.slice(at + raw.length, at + raw.length + 4 * suf.length + 16)).startsWith(suf);
353
+ if (okPre && okSuf) guarded.push(i);
354
+ }
355
+ const validNth = Number.isInteger(nth) && nth >= 0 && nth < occ.length ? nth : null;
356
+ let idx;
357
+ if (guarded.length === 1) idx = guarded[0];
358
+ else if (validNth !== null && guarded.includes(validNth)) idx = validNth;
359
+ else if (Number.isInteger(hintStart) && guarded.length) {
360
+ idx = guarded.reduce((best, i) => (Math.abs(occ[i] - hintStart) < Math.abs(occ[best] - hintStart) ? i : best), guarded[0]);
361
+ } else if (guarded.length) idx = guarded[0];
362
+ else idx = validNth !== null ? validNth : 0;
363
+ return { start: occ[idx], end: occ[idx] + raw.length, k: idx };
364
+ }
365
+
366
+ function findNormMatch(sh, q, { prefix = "", suffix = "", nth = null, hintStart = null } = {}) {
367
+ const pre = normText(prefix).slice(-CTX);
368
+ const suf = normText(suffix).slice(0, CTX);
369
+ const needAll = !!(pre || suf || Number.isInteger(nth) || Number.isInteger(hintStart));
370
+ const occ = normOccurrences(sh, q, needAll ? Infinity : 1);
371
+ if (!occ?.length) return null;
372
+ if (occ.length === 1) return { ...occ[0], k: 0 };
373
+ const validNth = Number.isInteger(nth) && nth >= 0 && nth < occ.length ? nth : null;
374
+ if ((pre || suf) && !spend(occ.length * 2 * (pre.length + suf.length + 16))) return null;
375
+ const map = normMapFor(sh);
376
+ const guarded = [];
377
+ for (let i = 0; i < occ.length; i++) {
378
+ const o = occ[i];
379
+ const okPre = !pre || !map || map.norm.slice(Math.max(0, o.normStart - pre.length - 1), o.normStart).trimEnd().endsWith(pre);
380
+ const okSuf = !suf || !map || map.norm.slice(o.normEnd, o.normEnd + suf.length + 1).trimStart().startsWith(suf);
381
+ if (okPre && okSuf) guarded.push(i);
382
+ }
383
+ let idx;
384
+ if (guarded.length === 1) idx = guarded[0];
385
+ else if (validNth !== null && guarded.includes(validNth)) idx = validNth;
386
+ else if (Number.isInteger(hintStart) && guarded.length) {
387
+ idx = guarded.reduce((best, i) => (Math.abs(occ[i].start - hintStart) < Math.abs(occ[best].start - hintStart) ? i : best), guarded[0]);
388
+ } else if (guarded.length) idx = guarded[0];
389
+ else idx = validNth !== null ? validNth : 0;
390
+ return { ...occ[idx], k: idx };
391
+ }
392
+
180
393
  // A shape's normalized text, kept per shape of a reading: every anchor on a shape normalized all of it again (50 comments on a shape of
181
394
  // 8 million characters held the server's thread for 5.6 s while the page placed them).
182
395
  const NORM = new WeakMap();
@@ -249,8 +462,31 @@ export function resolvePptx(deck, d) {
249
462
  const stext = spend(normCost(sh)) ? normShape(sh) : "";
250
463
  const q = d.quote || "";
251
464
  const searched = !!q && spend(textCost(sh, q)); // a shape-wide comment has no quote, and no search to pay for
252
- const at = searched ? sh.text.indexOf(q) : -1;
253
- return { ...out, method: method === "id" ? how : `${method}+${how}`, shapeId: sh.id, shapeName: sh.name, start: at >= 0 ? at : null, end: at >= 0 ? at + q.length : null, changed: sh.hash !== d.textHash, text: stext.slice(0, 200), quoteLost: searched && at < 0 };
465
+ const ctxOpts = { prefix: d.prefix ?? d.before ?? "", suffix: d.suffix ?? d.after ?? "", nth: d.nth ?? d.occ ?? null };
466
+ let at = -1;
467
+ let len = q.length;
468
+ let k = null;
469
+ if (searched) {
470
+ const preferNorm = q.includes(" ") && HAS_NON_PLAIN_WS.test(sh.text);
471
+ if (preferNorm) {
472
+ if (stext) {
473
+ const m = findNormMatch(sh, q, ctxOpts);
474
+ if (m) [at, len, k] = [m.start, m.end - m.start, m.k];
475
+ }
476
+ } else {
477
+ const exact = findExactMatch(sh, q, ctxOpts);
478
+ if (exact && !(Number.isInteger(ctxOpts.nth) && ctxOpts.nth > 0 && exact.k !== ctxOpts.nth)) {
479
+ [at, len, k] = [exact.start, exact.end - exact.start, exact.k];
480
+ } else if (exact !== null && stext) {
481
+ const m = findNormMatch(sh, q, ctxOpts);
482
+ if (m) [at, len, k] = [m.start, m.end - m.start, m.k];
483
+ else if (exact) [at, len, k] = [exact.start, exact.end - exact.start, exact.k];
484
+ } else if (exact) {
485
+ [at, len, k] = [exact.start, exact.end - exact.start, exact.k];
486
+ }
487
+ }
488
+ }
489
+ return { ...out, method: method === "id" ? how : `${method}+${how}`, shapeId: sh.id, shapeName: sh.name, start: at >= 0 ? at : null, end: at >= 0 ? at + len : null, nth: at >= 0 ? k : null, changed: sh.hash !== d.textHash, text: stext.slice(0, 200), quoteLost: searched && at < 0 };
254
490
  }
255
491
 
256
492
  /** Locator fields for the batch: the names the `apply` targets use. */
package/src/question.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { untrusted } from "./sanitize.js";
2
+ import { foldLinkText } from "./voice-guide.js";
2
3
 
3
4
  /**
4
5
  * A question the agent asks instead of declining: `{"comment":"c1","ask":"Which date?","options":["March 3","March 13"]}`.
@@ -16,7 +17,12 @@ export const QUESTION_FIX = "A question needs 2 to 4 short options (40 character
16
17
  /** The note on the card when `done` received a question it would not show. */
17
18
  export const BAD_QUESTION_NOTE = "The agent asked a question that could not be shown.";
18
19
 
19
- const LINK = /:\/\/|www\./i;
20
+ // Refuse only ://, www., or email addresses (mailto: or a@b.c), after dot-folding and invisible-char stripping.
21
+ // Bare domains without a scheme ("evil.com") are an accepted gap so ordinary text ("Finished.No", "Socket.IO", "main.py:42") always passes.
22
+ // One character before @ proves the local part is nonempty without scanning each suffix.
23
+ const LINK = /:\/\/|www\.|mailto:|[^\s@]@[^\s@.]+\.[^\s@.]/iu;
24
+ // Invisible characters left after untrusted() cleans the displayed text.
25
+ const visibleText = (s) => s.replace(/[\u00AD\u034F\u115F\u1160\u17B4\u17B5\u180E\u2800\u3164\uFE0E\uFE0F\uFFA0]/gu, "");
20
26
  const len = (s) => [...s].length; // code points, as untrusted() and the schema count
21
27
 
22
28
  /** One string: cleaned, then refused when empty, over `max` or a link. Returns {text} or {reason}. */
@@ -25,7 +31,8 @@ function cleanOne(v, max, what) {
25
31
  const text = untrusted(v, Number.MAX_SAFE_INTEGER);
26
32
  if (!text) return { reason: `${what} is empty` };
27
33
  if (len(text) > max) return { reason: `${what} is longer than ${max} characters` };
28
- if (LINK.test(text)) return { reason: `${what} contains a link` };
34
+ const folded = foldLinkText(visibleText(text));
35
+ if (LINK.test(folded)) return { reason: `${what} contains a link` };
29
36
  return { text };
30
37
  }
31
38
 
@@ -42,6 +49,6 @@ export function cleanQuestion({ ask, options } = {}) {
42
49
  if (c.reason) return refuse(c.reason);
43
50
  out.push(c.text);
44
51
  }
45
- if (new Set(out.map((o) => o.toLowerCase())).size !== out.length) return refuse("two options are the same");
52
+ if (new Set(out.map((o) => visibleText(o).toLowerCase())).size !== out.length) return refuse("two options are the same");
46
53
  return { question: { text: q.text, options: out } };
47
54
  }
package/src/quick.js CHANGED
@@ -78,7 +78,7 @@ function parseReplace(t) {
78
78
  let rest;
79
79
  if (OPEN_QUOTES.has(body[0])) {
80
80
  const q = quoted(body);
81
- if (!q || !q.rest.startsWith(" with ")) return null;
81
+ if (!q || !/^ with /i.test(q.rest)) return null;
82
82
  find = { text: q.value, quoted: true };
83
83
  rest = q.rest.slice(" with ".length);
84
84
  } else {
@@ -205,7 +205,11 @@ export function verifyTracked({ before, after, out, counts, author, date }) {
205
205
  const zb = openZip(before);
206
206
  const zo = openZip(out);
207
207
  if (zo.names.join("\0") !== za.names.join("\0")) fail("the copy's parts differ from the agent's version");
208
- for (const n of za.names) if (n !== "word/document.xml" && !zo.read(n).equals(za.read(n))) fail(`a part other than the main text differs: ${n}`);
208
+ for (const n of za.names) {
209
+ if (n === "word/document.xml") continue;
210
+ const sameRaw = zo.entries.get(n)?.method === za.entries.get(n)?.method && zo.raw(n).equals(za.raw(n));
211
+ if (!sameRaw && !zo.read(n).equals(za.read(n))) fail(`a part other than the main text differs: ${n}`);
212
+ }
209
213
  const xa = za.readText("word/document.xml");
210
214
  const xb = zb.readText("word/document.xml");
211
215
  const xo = zo.readText("word/document.xml");
@@ -221,7 +225,13 @@ export function verifyTracked({ before, after, out, counts, author, date }) {
221
225
  return n;
222
226
  };
223
227
  if (revCount(xo) !== ours.size + revCount(xa)) fail("the copy has revisions that are neither ours nor the file's own");
224
- const maxIn = Math.max(0, ...[...inputIds].filter((v) => /^\d+$/.test(v)).map(Number));
228
+ let maxIn = 0;
229
+ for (const v of inputIds) {
230
+ if (/^\d+$/.test(v)) {
231
+ const n = Number(v);
232
+ if (n > maxIn) maxIn = n;
233
+ }
234
+ }
225
235
  for (const id of ours) if (Number(id) <= maxIn) fail("a revision id is not above every id in the file");
226
236
 
227
237
  const A = parseDocxParagraphs(xa);