onionskin-cli 0.0.0-stage → 0.0.2
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/CHANGELOG.md +144 -0
- package/INSTALL.md +25 -0
- package/LICENSE +21 -0
- package/NOTICE +49 -0
- package/README.md +148 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +67 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +288 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1912 -0
- package/src/settings.js +161 -0
- package/src/setup.js +199 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-BUtofdtr.js +197 -0
- package/web/dist/assets/index-Dfizlg0i.css +1 -0
- package/web/dist/index.html +14 -0
package/src/ops/docx.js
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { readDocxParagraphs } from "../docx.js";
|
|
2
|
+
import { openZip } from "../zip.js";
|
|
3
|
+
import { replaceEntry } from "../zip-write.js";
|
|
4
|
+
import { checkExpect, engineFailure, excerpt, fail, textHash, normText, refused } from "./common.js";
|
|
5
|
+
import { TableError, tableEditor } from "./docx-table.js";
|
|
6
|
+
import { loadEngine, openEngine } from "./engine.js";
|
|
7
|
+
import { applySet, findAll, hasObjects, layout, partialWords, pickMatches, replaceRange, setAllText, splitForRange } from "./runs.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* DOCX driver. Two phases per run (see run.js): bind() resolves anchors and checks `expect` against the file as it was before any
|
|
11
|
+
* op ran; exec() then performs the ops in order through the engine's own edit path (docx.edit), the path the editor uses.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const HEADING_CANON = new Set(["title", "subtitle", "h1", "h2", "h3", "h4", "h5", "h6"]);
|
|
15
|
+
const NEW_PID_BASE = 2_000_000;
|
|
16
|
+
|
|
17
|
+
const visibleText = (p) => layout(p.runs).text;
|
|
18
|
+
|
|
19
|
+
export async function open({ bytes, name, anchors }) {
|
|
20
|
+
const h = await openEngine(bytes, name);
|
|
21
|
+
const { paraSpec } = await loadEngine();
|
|
22
|
+
await h.engine.call("docx.start", []);
|
|
23
|
+
const done = h.events.find((e) => e.event === "docx:done")?.data;
|
|
24
|
+
if (done?.error) throw new Error(`the engine could not read the document: ${done.error}`);
|
|
25
|
+
const blocks = h.events.filter((e) => e.event === "docx:blocks").flatMap((e) => e.data);
|
|
26
|
+
const meta = h.events.find((e) => e.event === "docx:meta")?.data || {};
|
|
27
|
+
|
|
28
|
+
// engine paragraphs in document order (table cells included); parts of one paragraph (page breaks) share a pid and merge into one entry
|
|
29
|
+
const eng = [];
|
|
30
|
+
const walk = (bs) => {
|
|
31
|
+
for (const b of bs) {
|
|
32
|
+
if (b.t === "p") {
|
|
33
|
+
const last = eng[eng.length - 1];
|
|
34
|
+
if (last && b.pid !== undefined && last.pid === b.pid) last.parts.push(b);
|
|
35
|
+
else eng.push({ pid: b.pid, parts: [b] });
|
|
36
|
+
} else if (b.t === "tbl") for (const r of b.rows) for (const c of r.cells) walk(c.blocks);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
walk(blocks);
|
|
40
|
+
for (const e of eng) {
|
|
41
|
+
e.ro = e.parts.some((p) => p.ro) || e.pid === undefined;
|
|
42
|
+
e.text = e.parts.map(visibleText).join("");
|
|
43
|
+
e.hash = textHash(e.text);
|
|
44
|
+
}
|
|
45
|
+
const ours = readDocxParagraphs(bytes);
|
|
46
|
+
const group = (list) => {
|
|
47
|
+
const m = new Map();
|
|
48
|
+
for (const x of list) m.set(x.hash, [...(m.get(x.hash) || []), x]);
|
|
49
|
+
return m;
|
|
50
|
+
};
|
|
51
|
+
const ourBy = group(ours);
|
|
52
|
+
const engBy = group(eng);
|
|
53
|
+
const cur = new Map(); // pid -> current single-part Paragraph model
|
|
54
|
+
for (const e of eng) if (e.pid !== undefined && e.parts.length === 1) cur.set(e.pid, e.parts[0]);
|
|
55
|
+
// tableOps: the row and column edits, run on the engine's saved bytes (save); bound: the body paths other ops of this file edit
|
|
56
|
+
return { h, paraSpec, meta, styles: meta.styles || [], ours, eng, ourBy, engBy, cur, anchors: anchors || new Map(), newPid: NEW_PID_BASE, name, bytes, tableOps: [], bound: [], shadow: null };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** the engine paragraph that is `o` (same text, same occurrence number among equal texts); null when the two readings of the file disagree */
|
|
60
|
+
function engineFor(st, o) {
|
|
61
|
+
const a = st.ourBy.get(o.hash);
|
|
62
|
+
const b = st.engBy.get(o.hash);
|
|
63
|
+
if (!b || a.length !== b.length) return null;
|
|
64
|
+
return b[a.indexOf(o)];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function locatorOf(st, target) {
|
|
68
|
+
if (!("anchor_id" in target)) return { loc: target };
|
|
69
|
+
const a = st.anchors.get(target.anchor_id);
|
|
70
|
+
if (!a) return { error: `anchor ${target.anchor_id} is not in the batch (pass --batch batch.json, or run inside the session that delivered it)` };
|
|
71
|
+
if (!a.locator) return { error: `anchor ${target.anchor_id} was not resolved to a paragraph when the batch was built` };
|
|
72
|
+
return { loc: a.locator, quote: a.quote };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** durable locator -> one paragraph of the file. Exact matches only (paraId, path+hash, hash, quote): no fuzzy matching. */
|
|
76
|
+
function resolveParagraph(st, target) {
|
|
77
|
+
const { loc, quote, error } = locatorOf(st, target);
|
|
78
|
+
if (error) return fail("anchor_not_found", error);
|
|
79
|
+
const pid = loc.para_id || loc.paraId;
|
|
80
|
+
const path = loc.body_path || null;
|
|
81
|
+
const hash = loc.text_hash || null;
|
|
82
|
+
const same = (a, b) => a && b && a.length === b.length && a.every((v, i) => v === b[i]);
|
|
83
|
+
const unique = (hits, how) => (hits.length === 1 ? { o: hits[0] } : hits.length > 1 ? fail("anchor_not_found", `ambiguous locator (${how} matches ${hits.length} paragraphs)`) : null);
|
|
84
|
+
if (pid) {
|
|
85
|
+
const r = unique(st.ours.filter((p) => p.paraId === String(pid).toUpperCase()), "para_id");
|
|
86
|
+
if (r) return r;
|
|
87
|
+
}
|
|
88
|
+
if (path && hash) {
|
|
89
|
+
const r = unique(st.ours.filter((p) => same(p.path, path) && p.hash === hash), "body_path with text_hash");
|
|
90
|
+
if (r) return r;
|
|
91
|
+
}
|
|
92
|
+
if (hash) {
|
|
93
|
+
const r = unique(st.ours.filter((p) => p.hash === hash), "text_hash");
|
|
94
|
+
if (r) return r;
|
|
95
|
+
}
|
|
96
|
+
if (path && !hash) {
|
|
97
|
+
const r = unique(st.ours.filter((p) => same(p.path, path)), "body_path");
|
|
98
|
+
if (r) return r;
|
|
99
|
+
}
|
|
100
|
+
const q = normText(quote || "");
|
|
101
|
+
if (q.length >= 8) {
|
|
102
|
+
const r = unique(st.ours.filter((p) => normText(p.text).includes(q)), "the anchor quote");
|
|
103
|
+
if (r) return r;
|
|
104
|
+
}
|
|
105
|
+
return fail("anchor_not_found", "no paragraph in the file matches this locator");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** the paragraph, checked against `expect`, mapped to the engine, and refused when protected */
|
|
109
|
+
function bindParagraph(st, target, expect) {
|
|
110
|
+
const r = resolveParagraph(st, target);
|
|
111
|
+
if (!r.o) return r;
|
|
112
|
+
const o = r.o;
|
|
113
|
+
const bad = checkExpect(expect, o.text, "contains");
|
|
114
|
+
if (bad) return bad;
|
|
115
|
+
if (o.protected) return fail("skipped_protected", "the paragraph holds a field, content control, equation or other construct that cannot be rewritten losslessly");
|
|
116
|
+
const e = engineFor(st, o);
|
|
117
|
+
if (!e) return fail("anchor_not_found", "the engine and the file read this paragraph's text differently (fields or objects?); not edited");
|
|
118
|
+
if (e.ro) return fail("skipped_protected", "the engine marks this paragraph read-only");
|
|
119
|
+
if (e.parts.length > 1) return refused("paragraph_spans_page_break", "paragraphs that contain a page break are not supported");
|
|
120
|
+
if (o.path) st.bound.push(o.path);
|
|
121
|
+
return { ok: true, pid: e.pid, o };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function styleId(st, name) {
|
|
125
|
+
const want = normText(name).toLowerCase();
|
|
126
|
+
return st.styles.find((s) => s.id.toLowerCase() === want || normText(s.name).toLowerCase() === want)?.id;
|
|
127
|
+
}
|
|
128
|
+
async function ensureStyleId(st, name) {
|
|
129
|
+
const known = styleId(st, name);
|
|
130
|
+
if (known) return known;
|
|
131
|
+
const n = normText(name).toLowerCase();
|
|
132
|
+
const canon = n === "normal" ? "normal" : n === "title" ? "title" : n === "subtitle" ? "subtitle" : /^heading [1-6]$/.test(n) ? `h${n.slice(-1)}` : null;
|
|
133
|
+
if (canon) {
|
|
134
|
+
const r = await st.h.engine.call("docx.style", [canon]);
|
|
135
|
+
st.styles = r.styles || st.styles;
|
|
136
|
+
return r.id;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ---- bind --------------------------------------------------------------------------------------------------------
|
|
142
|
+
export function bind(st, op) {
|
|
143
|
+
switch (op.op) {
|
|
144
|
+
case "replace_text":
|
|
145
|
+
case "insert_paragraph_after":
|
|
146
|
+
case "insert_paragraph_before":
|
|
147
|
+
case "delete_paragraph":
|
|
148
|
+
case "set_paragraph_style":
|
|
149
|
+
case "format_text":
|
|
150
|
+
case "format_paragraph": {
|
|
151
|
+
const b = bindParagraph(st, op.target, op.expect);
|
|
152
|
+
return b.ok ? { ...b, op } : b;
|
|
153
|
+
}
|
|
154
|
+
case "move_paragraph": {
|
|
155
|
+
const b = bindParagraph(st, op.target, op.expect);
|
|
156
|
+
if (!b.ok) return b;
|
|
157
|
+
const to = bindParagraph(st, op.to, { text_hash: op.to.text_hash });
|
|
158
|
+
if (!to.ok) return to;
|
|
159
|
+
if (to.pid === b.pid) return refused("move_onto_itself", "a paragraph cannot be moved next to itself");
|
|
160
|
+
// the original XML travels whole: only between places of the same kind, and never with a section break or half a comment or bookmark range
|
|
161
|
+
if (b.o.path?.length !== 1 || to.o.path?.length !== 1) return refused("move_not_in_body", "only paragraphs of the document body (not in a table) can be moved");
|
|
162
|
+
const more = (st.detail ||= readDocxParagraphs(st.bytes, { detail: true }))[b.o.ordinal];
|
|
163
|
+
if (more.sect) return refused("move_section_break", "the paragraph ends a section");
|
|
164
|
+
if (more.loose) return refused("move_open_range", "a comment or bookmark starts or ends inside the paragraph");
|
|
165
|
+
return { ...b, anchorPid: to.pid, op };
|
|
166
|
+
}
|
|
167
|
+
case "set_table_cell_text": {
|
|
168
|
+
const { table, row, col, paragraph = 0 } = op.target;
|
|
169
|
+
const o = st.ours.find((p) => p.path && p.path.length === 4 && p.path[0] === table && p.path[1] === row && p.path[2] === col && p.path[3] === paragraph);
|
|
170
|
+
if (!o) return fail("anchor_not_found", `no paragraph ${paragraph} in cell (${row},${col}) of table ${table}`);
|
|
171
|
+
const r = bindParagraph(st, { text_hash: o.hash, body_path: o.path }, op.expect);
|
|
172
|
+
return r.ok ? { ...r, op } : r;
|
|
173
|
+
}
|
|
174
|
+
case "replace_all_text": {
|
|
175
|
+
const hits = st.ours.map((o) => ({ o, n: findAll(o.text, op.find, op.match_case !== false).length })).filter((x) => x.n);
|
|
176
|
+
const total = hits.reduce((n, x) => n + x.n, 0);
|
|
177
|
+
// what previewAll reports: matches inside a longer word, and matches in protected paragraphs (which the run skips)
|
|
178
|
+
const extra = { partial_words: hits.reduce((n, x) => n + partialWords(x.o.text, op.find, op.match_case !== false), 0), protected: hits.reduce((n, x) => n + (x.o.protected ? x.n : 0), 0) };
|
|
179
|
+
if (total !== op.expect.count) return fail("expect_mismatch", `found ${total} occurrence(s), expected ${op.expect.count}`, { found: total, ...extra });
|
|
180
|
+
return { ok: true, op, hits, total, ...extra };
|
|
181
|
+
}
|
|
182
|
+
case "insert_table_row":
|
|
183
|
+
case "delete_table_row":
|
|
184
|
+
case "insert_table_column":
|
|
185
|
+
case "delete_table_column":
|
|
186
|
+
return bindTable(st, op);
|
|
187
|
+
case "format_all": {
|
|
188
|
+
const inScope = scopeEntries(st, op.scope);
|
|
189
|
+
const prot = protectedEntries(st);
|
|
190
|
+
const skipped = inScope.filter((e) => e.ro || prot.has(e) || e.parts.length > 1).length; // the same test exec skips on
|
|
191
|
+
if (inScope.length !== op.expect.count) return fail("expect_mismatch", `${inScope.length} paragraph(s) with text are in scope "${op.scope}", expected ${op.expect.count}`, { found: inScope.length, protected: skipped });
|
|
192
|
+
return { ok: true, op, found: inScope.length, protected: skipped };
|
|
193
|
+
}
|
|
194
|
+
default:
|
|
195
|
+
return refused("unsupported_op", `${op.op} does not apply to a .docx`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---- table rows and columns (src/ops/docx-table.js) ----------------------------------------------------------------
|
|
200
|
+
const TABLE_KIND = { insert_table_row: "insertRow", delete_table_row: "deleteRow", insert_table_column: "insertColumn", delete_table_column: "deleteColumn" };
|
|
201
|
+
const docXml = (st) => (st.docXml ??= openZip(st.bytes).readText("word/document.xml"));
|
|
202
|
+
/** cell paragraphs -> text: grouped by `key` (path index of the cell) in order, paragraphs joined by LF, groups by `sep` */
|
|
203
|
+
function joined(paras, key, sep) {
|
|
204
|
+
const groups = new Map();
|
|
205
|
+
for (const p of paras) groups.set(p.path[key], [...(groups.get(p.path[key]) || []), p.text]);
|
|
206
|
+
return [...groups.keys()].sort((a, b) => a - b).map((k) => groups.get(k).join("\n")).join(sep);
|
|
207
|
+
}
|
|
208
|
+
const tableSpec = (op, tableNo) => {
|
|
209
|
+
const isRow = "row" in op.target;
|
|
210
|
+
const at = isRow ? op.target.row : op.target.col;
|
|
211
|
+
return { kind: TABLE_KIND[op.op], tableNo, args: op.op.startsWith("insert") ? [at, op.where, op.cells || []] : [at] };
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* A row or column of a top-level table, by `table` (body_path[0]) and `row` or `col`. `expect` is checked against the row's text (cells
|
|
216
|
+
* joined by TAB) or the column's (cells joined by LF), each cell's paragraphs joined by LF. The edit is tried here on a copy of the file as
|
|
217
|
+
* delivered that carries the table edits bound before it, so what the table cannot take (merged cells, the last row, more cell texts
|
|
218
|
+
* than cells) is refused before any op runs.
|
|
219
|
+
*/
|
|
220
|
+
function bindTable(st, op) {
|
|
221
|
+
const { table } = op.target;
|
|
222
|
+
const cells = st.ours.filter((p) => p.path?.length === 4 && p.path[0] === table);
|
|
223
|
+
if (!cells.length) return fail("anchor_not_found", `body item ${table} is not a table (use body_path[0] of a paragraph in the table)`);
|
|
224
|
+
const isRow = "row" in op.target;
|
|
225
|
+
const at = isRow ? op.target.row : op.target.col;
|
|
226
|
+
const sel = cells.filter((p) => p.path[isRow ? 1 : 2] === at);
|
|
227
|
+
if (!sel.length) return fail("anchor_not_found", `the table has no ${isRow ? "row" : "column"} ${at} (0-based)`);
|
|
228
|
+
const bad = checkExpect(op.expect, joined(sel, isRow ? 2 : 1, isRow ? "\t" : "\n"), "contains");
|
|
229
|
+
if (bad) return bad;
|
|
230
|
+
if (op.op.startsWith("delete") && sel.some((p) => p.protected)) return fail("skipped_protected", `the ${isRow ? "row" : "column"} holds a protected paragraph (a field, content control, equation or tracked change); not removed`);
|
|
231
|
+
const spec = tableSpec(op, cells[0].tableNo);
|
|
232
|
+
try {
|
|
233
|
+
(st.bindShadow ??= tableEditor(docXml(st))).run(spec);
|
|
234
|
+
} catch (err) {
|
|
235
|
+
if (err instanceof TableError) return refused(err.code, err.message);
|
|
236
|
+
throw err;
|
|
237
|
+
}
|
|
238
|
+
return { ok: true, op, spec, table, isRow, at };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** The edit on this file's copy of the table (every earlier table edit included); recorded for save when it holds. */
|
|
242
|
+
function execTable(st, b) {
|
|
243
|
+
if (b.op.op.startsWith("delete")) {
|
|
244
|
+
const hit = st.bound.find((p) => p.length === 4 && p[0] === b.table && p[b.isRow ? 1 : 2] === b.at);
|
|
245
|
+
if (hit) return refused("cell_edited_by_another_op", `another op of this file edits a paragraph in this ${b.isRow ? "row" : "column"}; it would be removed with it`);
|
|
246
|
+
}
|
|
247
|
+
st.shadow ??= tableEditor(docXml(st));
|
|
248
|
+
let index;
|
|
249
|
+
try {
|
|
250
|
+
index = st.shadow.run(b.spec);
|
|
251
|
+
} catch (err) {
|
|
252
|
+
if (err instanceof TableError) return refused(err.code, err.message); // nothing changed: the copy stays as it was, the engine was not touched
|
|
253
|
+
throw err;
|
|
254
|
+
}
|
|
255
|
+
st.tableOps.push(b.spec);
|
|
256
|
+
return { status: "applied", ...(index !== undefined ? { detail: { table: b.table, [b.isRow ? "row" : "col"]: index } } : {}) };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function isHeading(st, e) {
|
|
260
|
+
const sid = e.parts[0].sid;
|
|
261
|
+
const info = st.styles.find((s) => s.id === sid);
|
|
262
|
+
return !!info && (HEADING_CANON.has(info.canon) || info.outline != null);
|
|
263
|
+
}
|
|
264
|
+
const protectedEntries = (st) => {
|
|
265
|
+
const set = new Set();
|
|
266
|
+
for (const o of st.ours) if (o.protected) set.add(engineFor(st, o));
|
|
267
|
+
return set;
|
|
268
|
+
};
|
|
269
|
+
function scopeEntries(st, scope) {
|
|
270
|
+
return st.eng.filter((e) => e.text.trim() && (scope === "headings") === isHeading(st, e));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ---- exec --------------------------------------------------------------------------------------------------------
|
|
274
|
+
const wasDeleted = () => fail("anchor_not_found", "the paragraph was deleted by an earlier op of this file");
|
|
275
|
+
|
|
276
|
+
async function editRuns(st, pid, runs) {
|
|
277
|
+
const p = st.cur.get(pid);
|
|
278
|
+
const edited = { ...p, edited: true, runs };
|
|
279
|
+
const res = await st.h.engine.call("docx.edit", [{ anchor: pid, list: [st.paraSpec([edited])], want: true }]);
|
|
280
|
+
const nb = res.blocks[pid];
|
|
281
|
+
st.cur.set(pid, nb && nb.length === 1 && nb[0].t === "p" ? after(p, nb[0], true) : edited); // `edited` stays set: a later spec must not be mistaken for the untouched original
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The paragraph model after an edit. The engine's answer does not carry `pd` (paragraph properties set by an earlier op of this file:
|
|
286
|
+
* style, alignment, list), and every spec is generated from the original paragraph again, so `pd` has to travel with the model or the
|
|
287
|
+
* next op on this paragraph would silently undo it.
|
|
288
|
+
*/
|
|
289
|
+
const after = (p, block, edited) => ({ ...block, edited, ...(p.pd ? { pd: p.pd } : {}) });
|
|
290
|
+
|
|
291
|
+
function matchesIn(p, find, matchCase, pick) {
|
|
292
|
+
const { text } = layout(p.runs);
|
|
293
|
+
return pickMatches(findAll(text, find, matchCase !== false), pick);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export async function exec(st, b) {
|
|
297
|
+
const { op } = b;
|
|
298
|
+
if (TABLE_KIND[op.op]) return execTable(st, b);
|
|
299
|
+
try {
|
|
300
|
+
switch (op.op) {
|
|
301
|
+
case "replace_text":
|
|
302
|
+
case "set_table_cell_text": {
|
|
303
|
+
const p = st.cur.get(b.pid);
|
|
304
|
+
if (!p) return wasDeleted();
|
|
305
|
+
if ("text" in op) {
|
|
306
|
+
if (hasObjects(p.runs)) return refused("paragraph_has_objects", "the paragraph holds an image or other object; use find/replace instead of text");
|
|
307
|
+
await editRuns(st, b.pid, setAllText(p.runs, op.text));
|
|
308
|
+
return { status: "applied" };
|
|
309
|
+
}
|
|
310
|
+
const m = matchesIn(p, op.find, op.match_case, op);
|
|
311
|
+
if (m.error) return fail("anchor_not_found", m.error);
|
|
312
|
+
let runs = p.runs;
|
|
313
|
+
for (const s of [...m.starts].sort((a, c) => c - a)) runs = replaceRange(runs, s, s + op.find.length, op.replace); // right to left keeps offsets valid
|
|
314
|
+
await editRuns(st, b.pid, runs);
|
|
315
|
+
return { status: "applied", detail: { replaced: m.starts.length } };
|
|
316
|
+
}
|
|
317
|
+
case "format_text": {
|
|
318
|
+
const p = st.cur.get(b.pid);
|
|
319
|
+
if (!p) return wasDeleted();
|
|
320
|
+
const m = matchesIn(p, op.find, op.match_case, op);
|
|
321
|
+
if (m.error) return fail("anchor_not_found", m.error);
|
|
322
|
+
const patch = runProps(op.set);
|
|
323
|
+
let runs = p.runs;
|
|
324
|
+
for (const s of [...m.starts].sort((a, c) => c - a)) {
|
|
325
|
+
const sp = splitForRange(runs, s, s + op.find.length);
|
|
326
|
+
runs = applySet(sp.runs, sp.covered, patch);
|
|
327
|
+
}
|
|
328
|
+
await editRuns(st, b.pid, runs);
|
|
329
|
+
return { status: "applied", detail: { ranges: m.starts.length } };
|
|
330
|
+
}
|
|
331
|
+
case "insert_paragraph_after":
|
|
332
|
+
case "insert_paragraph_before": {
|
|
333
|
+
const p = st.cur.get(b.pid);
|
|
334
|
+
if (!p) return wasDeleted();
|
|
335
|
+
let pd;
|
|
336
|
+
if (op.style) {
|
|
337
|
+
const id = await ensureStyleId(st, op.style);
|
|
338
|
+
if (!id) return refused("unknown_style", `no paragraph style "${op.style}" (available: ${st.styles.map((s) => s.name).slice(0, 20).join(", ")})`);
|
|
339
|
+
pd = { style: id };
|
|
340
|
+
}
|
|
341
|
+
const pid = st.newPid++;
|
|
342
|
+
const fresh = { pid, from: b.pid, ...(pd ? { pd } : {}), parts: [[{ text: op.text }]] };
|
|
343
|
+
const anchorSpec = st.paraSpec([p]);
|
|
344
|
+
const res = await st.h.engine.call("docx.edit", [{ anchor: b.pid, list: op.op === "insert_paragraph_after" ? [anchorSpec, fresh] : [fresh, anchorSpec], want: true }]);
|
|
345
|
+
if (res.blocks[b.pid]?.length === 1) st.cur.set(b.pid, after(p, res.blocks[b.pid][0], !!p.edited));
|
|
346
|
+
if (res.blocks[pid]?.length === 1) st.cur.set(pid, { ...res.blocks[pid][0], edited: true });
|
|
347
|
+
return { status: "applied" };
|
|
348
|
+
}
|
|
349
|
+
case "delete_paragraph": {
|
|
350
|
+
if (!st.cur.has(b.pid)) return wasDeleted();
|
|
351
|
+
await st.h.engine.call("docx.edit", [{ anchor: b.pid, list: [], remove: [] }]);
|
|
352
|
+
st.cur.delete(b.pid);
|
|
353
|
+
return { status: "applied" };
|
|
354
|
+
}
|
|
355
|
+
case "set_paragraph_style": {
|
|
356
|
+
const p = st.cur.get(b.pid);
|
|
357
|
+
if (!p) return wasDeleted();
|
|
358
|
+
const id = await ensureStyleId(st, op.style);
|
|
359
|
+
if (!id) return refused("unknown_style", `no paragraph style "${op.style}" (available: ${st.styles.map((s) => s.name).slice(0, 20).join(", ")})`);
|
|
360
|
+
const pd = { ...(p.pd || {}), style: id };
|
|
361
|
+
const spec = { ...st.paraSpec([{ ...p, edited: true }]), pd };
|
|
362
|
+
const res = await st.h.engine.call("docx.edit", [{ anchor: b.pid, list: [spec], want: true }]);
|
|
363
|
+
const nb = res.blocks[b.pid];
|
|
364
|
+
if (!nb || nb[0].sid !== id) return { ...refused("style_not_applied", "the engine did not apply the style"), dirty: true };
|
|
365
|
+
st.cur.set(b.pid, after({ pd }, nb[0], true));
|
|
366
|
+
return { status: "applied" };
|
|
367
|
+
}
|
|
368
|
+
// The two ops below are the merge's own (src/merge.js): they are not in ops.schema.json, so an agent's ops file cannot name them.
|
|
369
|
+
case "format_paragraph": {
|
|
370
|
+
// formatting the user set while the agent worked: paragraph properties (`para`) and run properties by character range (`runs`), in one edit
|
|
371
|
+
const p = st.cur.get(b.pid);
|
|
372
|
+
if (!p) return wasDeleted();
|
|
373
|
+
const pd = { ...(p.pd || {}) };
|
|
374
|
+
const para = op.para || {};
|
|
375
|
+
let id;
|
|
376
|
+
if (para.style === null) pd.style = ""; // back to the default style: the engine takes the style reference out
|
|
377
|
+
else if ("style" in para) {
|
|
378
|
+
id = await ensureStyleId(st, para.style);
|
|
379
|
+
if (!id) return refused("unknown_style", `no paragraph style "${para.style}"`);
|
|
380
|
+
pd.style = id;
|
|
381
|
+
}
|
|
382
|
+
if ("align" in para) pd.align = para.align;
|
|
383
|
+
if ("list" in para) pd.list = para.list;
|
|
384
|
+
if ("ilvl" in para) pd.ilvl = para.ilvl;
|
|
385
|
+
let runs = p.runs;
|
|
386
|
+
if (op.runs?.length) {
|
|
387
|
+
// offsets are those of the paragraph as the file has it: any difference in how the engine reads the text and nothing is formatted
|
|
388
|
+
if (layout(runs).text !== b.o.text) return refused("text_read_differently", "the engine reads this paragraph's text differently; not formatted");
|
|
389
|
+
for (const r of [...op.runs].sort((x, y) => y.start - x.start)) {
|
|
390
|
+
const sp = splitForRange(runs, r.start, r.end);
|
|
391
|
+
runs = applySet(sp.runs, sp.covered, r.set);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const spec = { ...st.paraSpec([{ ...p, edited: true, runs }]), ...(Object.keys(pd).length ? { pd } : {}) };
|
|
395
|
+
const res = await st.h.engine.call("docx.edit", [{ anchor: b.pid, list: [spec], want: true }]);
|
|
396
|
+
const nb = res.blocks[b.pid];
|
|
397
|
+
if (!nb || nb.length !== 1 || nb[0].t !== "p") return { ...refused("format_not_applied", "the engine did not return the paragraph"), dirty: true };
|
|
398
|
+
if (id !== undefined && nb[0].sid !== id) return { ...refused("style_not_applied", "the engine did not apply the style"), dirty: true };
|
|
399
|
+
st.cur.set(b.pid, after({ pd: Object.keys(pd).length ? pd : undefined }, nb[0], true));
|
|
400
|
+
return { status: "applied" };
|
|
401
|
+
}
|
|
402
|
+
case "move_paragraph": {
|
|
403
|
+
// the paragraph itself (its original XML when no op of this file edited it) is taken out of its place and listed next to the anchor
|
|
404
|
+
const p = st.cur.get(b.pid);
|
|
405
|
+
const a = st.cur.get(b.anchorPid);
|
|
406
|
+
if (!p || !a) return wasDeleted();
|
|
407
|
+
const list = [st.paraSpec([a]), st.paraSpec([p])];
|
|
408
|
+
await st.h.engine.call("docx.edit", [{ anchor: b.anchorPid, list: op.where === "before" ? list.reverse() : list }]);
|
|
409
|
+
return { status: "applied" };
|
|
410
|
+
}
|
|
411
|
+
case "replace_all_text": {
|
|
412
|
+
const skipped = [];
|
|
413
|
+
let n = 0;
|
|
414
|
+
let paragraphs = 0;
|
|
415
|
+
let skippedMatches = 0;
|
|
416
|
+
const prot = protectedEntries(st);
|
|
417
|
+
for (const { o, n: expected } of b.hits) {
|
|
418
|
+
const skip = (reason) => {
|
|
419
|
+
skipped.push({ ordinal: o.ordinal, reason, excerpt: excerpt(o.text) });
|
|
420
|
+
skippedMatches += expected;
|
|
421
|
+
};
|
|
422
|
+
const e = engineFor(st, o);
|
|
423
|
+
const p = e && e.pid !== undefined ? st.cur.get(e.pid) : null;
|
|
424
|
+
if (!e || e.ro || prot.has(e) || o.protected || !p) {
|
|
425
|
+
skip(!e ? "text read differently by the engine" : "protected");
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
if (e.parts.length > 1) {
|
|
429
|
+
skip("paragraph spans a page break");
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
const m = matchesIn(p, op.find, op.match_case, { all: true });
|
|
433
|
+
if (m.error || m.starts.length !== expected) {
|
|
434
|
+
// an earlier op of this file changed the paragraph, or the engine reads it differently: never silently under-apply
|
|
435
|
+
skip(m.error ? "the text is no longer in the paragraph (changed by an earlier op of this file?)" : `the engine finds ${m.starts.length} of the ${expected} match(es)`);
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
let runs = p.runs;
|
|
439
|
+
try {
|
|
440
|
+
for (const s of [...m.starts].sort((a, c) => c - a)) runs = replaceRange(runs, s, s + op.find.length, op.replace);
|
|
441
|
+
} catch (err) {
|
|
442
|
+
if (!(err instanceof RangeError)) throw err;
|
|
443
|
+
skip(err.message);
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
await editRuns(st, e.pid, runs);
|
|
447
|
+
n += m.starts.length;
|
|
448
|
+
paragraphs++;
|
|
449
|
+
}
|
|
450
|
+
// every bound match ends up replaced or listed: a count that does not add up is a failure, not an "applied"
|
|
451
|
+
if (n + skippedMatches !== b.total) return { ...refused("incomplete_replace", `replaced ${n} and listed ${skippedMatches} of ${b.total} match(es)`), dirty: true };
|
|
452
|
+
if (!paragraphs && skipped.length) return { status: "skipped_protected", reason: "every paragraph with a match is protected or unreadable for the engine", detail: { skipped_protected: skipped } };
|
|
453
|
+
return { status: "applied", detail: { replaced: n, paragraphs, ...(skipped.length ? { skipped_protected: skipped } : {}) } };
|
|
454
|
+
}
|
|
455
|
+
case "format_all": {
|
|
456
|
+
const prot = protectedEntries(st);
|
|
457
|
+
const patch = runProps(op.set);
|
|
458
|
+
const skipped = [];
|
|
459
|
+
let changed = 0;
|
|
460
|
+
for (const e of scopeEntries(st, op.scope)) {
|
|
461
|
+
const p = e.pid !== undefined ? st.cur.get(e.pid) : null;
|
|
462
|
+
if (!p || e.ro || prot.has(e) || e.parts.length > 1) {
|
|
463
|
+
skipped.push({ reason: "protected", excerpt: excerpt(e.text) });
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const { segs } = layout(p.runs);
|
|
467
|
+
const covered = segs.filter((g) => g.text).map((g) => g.i);
|
|
468
|
+
await editRuns(st, e.pid, applySet(p.runs, covered, patch));
|
|
469
|
+
changed++;
|
|
470
|
+
}
|
|
471
|
+
if (!changed && skipped.length) return { status: "skipped_protected", reason: "every paragraph in scope is protected", detail: { skipped_protected: skipped } };
|
|
472
|
+
return { status: "applied", detail: { paragraphs: changed, ...(skipped.length ? { skipped_protected: skipped } : {}) } };
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
} catch (err) {
|
|
476
|
+
return engineFailure(err);
|
|
477
|
+
}
|
|
478
|
+
return refused("unsupported_op", op.op);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** schema property names -> the engine's direct-formatting names */
|
|
482
|
+
function runProps(set) {
|
|
483
|
+
const d = {};
|
|
484
|
+
if ("bold" in set) d.b = set.bold;
|
|
485
|
+
if ("italic" in set) d.i = set.italic;
|
|
486
|
+
if ("underline" in set) d.u = set.underline ? "single" : "none";
|
|
487
|
+
if ("color" in set) d.color = set.color.toLowerCase();
|
|
488
|
+
if ("size_pt" in set) d.sz = set.size_pt;
|
|
489
|
+
return d;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export async function save(st) {
|
|
493
|
+
const blob = await st.h.engine.call("save", []);
|
|
494
|
+
const out = Buffer.from(await blob.arrayBuffer());
|
|
495
|
+
if (!st.tableOps.length) return out;
|
|
496
|
+
// the row and column edits last, on the engine's document: its paragraph edits never change a table's rows or cells, so each edit
|
|
497
|
+
// meets the table it was checked against in exec (a refusal here throws and nothing is written)
|
|
498
|
+
const ed = tableEditor(openZip(out).readText("word/document.xml"));
|
|
499
|
+
for (const spec of st.tableOps) ed.run(spec);
|
|
500
|
+
return replaceEntry(out, "word/document.xml", ed.xml());
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export const close = (st) => st.h.engine.dispose();
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { env } from "../constants.js";
|
|
6
|
+
|
|
7
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
/** The DOM-free engine core bundled for Node (scripts/build-engine-node.mjs). ONIONSKIN_ENGINE_NODE points tests at a scratch build. */
|
|
9
|
+
export const engineNodePath = () => env("ENGINE_NODE", path.join(here, "..", "..", "vendor", "engine-node", "engine-node.mjs"));
|
|
10
|
+
|
|
11
|
+
let loaded;
|
|
12
|
+
export function loadEngine() {
|
|
13
|
+
return (loaded ??= import(pathToFileURL(engineNodePath()).href));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const LOAD_TIMEOUT_MS = 120_000;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Opens bytes in a headless engine (same Engine class the browser's worker runs, on this thread).
|
|
20
|
+
* Returns {engine, info, events, waitFor(pred)}: waitFor resolves from the engine's own events, no polling.
|
|
21
|
+
*/
|
|
22
|
+
export async function openEngine(bytes, name) {
|
|
23
|
+
const { Engine } = await loadEngine();
|
|
24
|
+
const events = [];
|
|
25
|
+
const waiters = new Set();
|
|
26
|
+
const engine = new Engine((event, data) => {
|
|
27
|
+
events.push({ event, data });
|
|
28
|
+
for (const w of [...waiters]) if (w.pred(event, data)) (waiters.delete(w), w.resolve(data));
|
|
29
|
+
});
|
|
30
|
+
const info = await engine.open(new Blob([bytes]), { name });
|
|
31
|
+
const waitFor = (pred) => {
|
|
32
|
+
const seen = events.find((e) => pred(e.event, e.data));
|
|
33
|
+
if (seen) return Promise.resolve(seen.data);
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const w = { pred, resolve: (d) => (clearTimeout(timer), resolve(d)) };
|
|
36
|
+
const timer = setTimeout(() => (waiters.delete(w), reject(new Error("the engine did not finish loading in time"))), LOAD_TIMEOUT_MS);
|
|
37
|
+
waiters.add(w);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
return { engine, info, events, waitFor };
|
|
41
|
+
}
|