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
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { untrusted } from "./sanitize.js";
|
|
2
|
+
import { exactHash } from "./text.js";
|
|
3
|
+
import { a1, ch, parseWorkbook } from "./xlsx.js";
|
|
4
|
+
|
|
5
|
+
// What the user changed in a workbook while the agent worked, carried onto the agent's version. The same three versions as the DOCX
|
|
6
|
+
// merge (src/merge.js): `base` (what the page loaded), `mine` (base plus the user's edits) and `theirs` (the file after the agent).
|
|
7
|
+
// The unit is the cell. A cell only the user changed becomes a `set_cells` op on `theirs` with the base text as `expect`, run through
|
|
8
|
+
// the same engine and package check as an agent's `apply`. A cell both sides changed keeps the agent's value and the user's value is
|
|
9
|
+
// handed back. Ceilings: cell values and formulas only. Formatting, sizes, hidden rows, merged cells and sheet changes are reported,
|
|
10
|
+
// not carried (the page names them in `edits`; one undone or redone with Undo/Redo is not reported). A value the engine reads
|
|
11
|
+
// differently than the file says (text that looks like a number) is handed back. More than MAX_OPS changed cells make their sheet a conflict.
|
|
12
|
+
// Whether an address still names the same cell on the agent's side is never guessed from the cells. Per sheet:
|
|
13
|
+
// 1. the agent's sheet is cell for cell the base: every edit of the user is carried;
|
|
14
|
+
// 2. the server's record of the run (`agentChanges`) shows the agent changed the file only through checked ops, and none that touched
|
|
15
|
+
// this sheet moves cells (inserting or deleting rows or columns, renaming the sheet): merged cell by cell;
|
|
16
|
+
// 3. anything else (an op that moves cells, a write the server cannot name, such as `commit`, no record): all of the user's values on
|
|
17
|
+
// the sheet are handed back in one item and the caller keeps a copy of the user's file.
|
|
18
|
+
// In case 2 the addresses hold, but `set_cells` can still rebuild a shift (each row's content written into the row below, numbers
|
|
19
|
+
// renumbered) that no op records. So a value is placed only on a row the agent left exactly as it was: a value on a row where the
|
|
20
|
+
// agent changed any cell is handed back. Two tests read the cells and can only hand back, never allow a merge: `sheetMoved` (the
|
|
21
|
+
// cells look like rows or columns moved) turns 2 into 3, and `placing` (the cells around a value do not show that its row and
|
|
22
|
+
// column are the same lines, as after a set_cells sort that left the value's own row alone) hands that one value back in case 2.
|
|
23
|
+
// A formula points at cells by address on any sheet: it is carried only when the agent left every sheet as it was (case 1). Rows
|
|
24
|
+
// or columns the user moved (the page says so, or the cells look like it) hand that sheet back too.
|
|
25
|
+
// Ceiling: a set_cells rebuild of a column shift that leaves the value's row reading as before (an empty row, or one whose cells
|
|
26
|
+
// all repeat across the shifted columns) is caught only by `placing`.
|
|
27
|
+
|
|
28
|
+
const CAP = 2000;
|
|
29
|
+
const MAX_OPS = 2000;
|
|
30
|
+
const cut = (t) => (t.length > CAP ? `${t.slice(0, CAP)}...` : t);
|
|
31
|
+
const key = (r, c) => r * 16384 + c;
|
|
32
|
+
const sheetKey = (s) => `${s.sheetId} ${s.name}`;
|
|
33
|
+
const named = (s) => `"${untrusted(s.name, 60)}"`;
|
|
34
|
+
const same = (a, b) => (!a || !b ? !a && !b : (a.shared && b.shared) || (a.text === b.text && a.k === b.k));
|
|
35
|
+
|
|
36
|
+
// ---- do the cells look like rows or columns moved? (a tripwire: it can only hand edits back, never allow a merge) -------------
|
|
37
|
+
// Cell text cannot prove that a row was inserted: it can only show that the differences are also what a shift would leave behind.
|
|
38
|
+
// A line (row or column) is compared by its cells; a formula counts without its cell references, which a shift rewrites.
|
|
39
|
+
const REF = /\$?[A-Za-z]{1,3}\$?\d{1,7}/g;
|
|
40
|
+
const cellSig = (cell) => (cell.shared ? "shared" : cell.k === "formula" ? `f${exactHash(cell.text.replace(REF, "@"))}` : ch(cell));
|
|
41
|
+
|
|
42
|
+
/** The rows (or columns) of a sheet: index -> {sig, cells: Map(position in the line -> cell signature)} */
|
|
43
|
+
function lines(sd, rows) {
|
|
44
|
+
const out = new Map();
|
|
45
|
+
for (const cell of sd.cells.values()) {
|
|
46
|
+
const [i, j] = rows ? [cell.r, cell.c] : [cell.c, cell.r];
|
|
47
|
+
(out.get(i) || out.set(i, { sig: "", cells: new Map() }).get(i)).cells.set(j, cellSig(cell));
|
|
48
|
+
}
|
|
49
|
+
for (const line of out.values()) line.sig = [...line.cells].sort((x, y) => x[0] - y[0]).map(([j, h]) => `${j}:${h}`).join("|");
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const COMMON = 20; // a cell value that this many changed lines share says nothing about which line is which
|
|
54
|
+
/** the share of cells two lines have in common, 0..1 */
|
|
55
|
+
function overlap(x, y) {
|
|
56
|
+
if (!x || !y) return 0;
|
|
57
|
+
let hit = 0;
|
|
58
|
+
for (const [j, h] of x.cells) if (y.cells.get(j) === h) hit++;
|
|
59
|
+
return hit / Math.max(x.cells.size, y.cells.size);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* True when the lines of B could be the lines of A after an insert, delete or move: a changed line now holds what another changed
|
|
64
|
+
* line held (all of it, or at least half and more than of what it held itself: a line that was edited while it moved), a new line
|
|
65
|
+
* repeats an old one, or a line that is gone has a twin left. Errs toward true (then nothing is merged there).
|
|
66
|
+
* Lines that moved and were rewritten by more than half are not seen as moved here; the values on them are held back as values on rows the agent changed.
|
|
67
|
+
*/
|
|
68
|
+
function linesMoved(A, B) {
|
|
69
|
+
const changed = new Set();
|
|
70
|
+
for (const i of new Set([...A.keys(), ...B.keys()])) if (A.get(i)?.sig !== B.get(i)?.sig) changed.add(i);
|
|
71
|
+
const where = (M) => {
|
|
72
|
+
const m = new Map();
|
|
73
|
+
for (const [i, line] of M) (m.get(line.sig) || m.set(line.sig, []).get(line.sig)).push(i);
|
|
74
|
+
return m;
|
|
75
|
+
};
|
|
76
|
+
const inA = where(A);
|
|
77
|
+
const inB = where(B);
|
|
78
|
+
const held = new Map(); // "position:cell signature" -> the changed lines of A that hold it
|
|
79
|
+
for (const q of changed) for (const [j, h] of A.get(q)?.cells || []) (held.get(`${j}:${h}`) || held.set(`${j}:${h}`, []).get(`${j}:${h}`)).push(q);
|
|
80
|
+
for (const i of changed) {
|
|
81
|
+
const a = A.get(i);
|
|
82
|
+
const b = B.get(i);
|
|
83
|
+
const from = b ? (inA.get(b.sig) || []).filter((q) => q !== i) : [];
|
|
84
|
+
if (a ? from.some((q) => changed.has(q)) : from.length) return true;
|
|
85
|
+
if (a && !b && (inB.get(a.sig) || []).some((q) => q !== i)) return true;
|
|
86
|
+
if (!b) continue;
|
|
87
|
+
const votes = new Map();
|
|
88
|
+
for (const [j, h] of b.cells) {
|
|
89
|
+
const list = held.get(`${j}:${h}`);
|
|
90
|
+
if (list && list.length <= COMMON) for (const q of list) if (q !== i) votes.set(q, (votes.get(q) || 0) + 1);
|
|
91
|
+
}
|
|
92
|
+
const best = [...votes].sort((x, y) => y[1] - x[1])[0];
|
|
93
|
+
if (best) {
|
|
94
|
+
const share = overlap(b, A.get(best[0]));
|
|
95
|
+
if (share >= 0.5 && share > overlap(b, a)) return true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Rows or columns of this sheet were added, removed or moved between the two versions (or that cannot be ruled out). */
|
|
102
|
+
export const sheetMoved = (sdA, sdB) => sdA.truncated || sdB.truncated || linesMoved(lines(sdA, true), lines(sdB, true)) || linesMoved(lines(sdA, false), lines(sdB, false));
|
|
103
|
+
|
|
104
|
+
// ---- do the cells look like this is still the same row and the same column? (a tripwire too: it can only hand a value back) ---
|
|
105
|
+
const RARE = 500; // a cell value that more lines than this share is no evidence for which line is which (and bounds the work)
|
|
106
|
+
const WORK = 5e6; // comparisons per axis; past it nothing more counts as the same line
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* One axis (rows or columns) of a sheet in base (A) and in the agent's version (B). `fits(i)`: a value may be placed on line i.
|
|
110
|
+
* A line is the same line (`same`) when it reads the same and no other line reads like it, or when at least half of its cells
|
|
111
|
+
* read the same and it reads as no other line as much (judged both ways: the agent's line among the base lines, the base line
|
|
112
|
+
* among the agent's; cells with a value that more than RARE lines share are left out). A line between two such lines cannot have
|
|
113
|
+
* moved, whatever was rewritten in it (`pinned`), unless it reads as another line more than as itself (`other`: two lines that
|
|
114
|
+
* changed places); neither can the first line before one, the last line after one when the data ends where it did, or a line
|
|
115
|
+
* the agent first wrote on past the end of the base data. A line that reads the same but has a twin, and an empty line, have
|
|
116
|
+
* nothing that tells them apart: they fit only when no line of the axis is in doubt (`steady`), an empty line also when it is
|
|
117
|
+
* past the end of the data in both versions. Errs toward false: the value is handed back.
|
|
118
|
+
*/
|
|
119
|
+
function axis(A, B) {
|
|
120
|
+
const end = (M) => {
|
|
121
|
+
let n = -1;
|
|
122
|
+
for (const i of M.keys()) if (i > n) n = i;
|
|
123
|
+
return n;
|
|
124
|
+
};
|
|
125
|
+
const endA = end(A);
|
|
126
|
+
const endB = end(B);
|
|
127
|
+
const index = (M) => {
|
|
128
|
+
const m = new Map();
|
|
129
|
+
for (const [i, line] of M) for (const [j, h] of line.cells) (m.get(`${j}:${h}`) || m.set(`${j}:${h}`, []).get(`${j}:${h}`)).push(i);
|
|
130
|
+
return m;
|
|
131
|
+
};
|
|
132
|
+
const count = (M) => {
|
|
133
|
+
const m = new Map();
|
|
134
|
+
for (const line of M.values()) m.set(line.sig, (m.get(line.sig) || 0) + 1);
|
|
135
|
+
return m;
|
|
136
|
+
};
|
|
137
|
+
let ix, sigs, steady;
|
|
138
|
+
let work = 0;
|
|
139
|
+
const rare = (key) => (ix.A.get(key)?.length || 0) <= RARE && (ix.B.get(key)?.length || 0) <= RARE;
|
|
140
|
+
const weight = (line) => {
|
|
141
|
+
if (line.w == null) {
|
|
142
|
+
line.w = 0;
|
|
143
|
+
for (const [j, h] of line.cells) if (rare(`${j}:${h}`)) line.w++;
|
|
144
|
+
}
|
|
145
|
+
return line.w;
|
|
146
|
+
};
|
|
147
|
+
/** how much of `line` reads as line i of M (`own`) and as the line of M it is most like apart from i (`other`): the share of the rare cells of the larger of the two */
|
|
148
|
+
const shares = (i, line, M, inM) => {
|
|
149
|
+
const votes = new Map();
|
|
150
|
+
for (const [j, h] of line.cells) {
|
|
151
|
+
const list = rare(`${j}:${h}`) && inM.get(`${j}:${h}`);
|
|
152
|
+
if (!list) continue;
|
|
153
|
+
work += list.length;
|
|
154
|
+
for (const q of list) votes.set(q, (votes.get(q) || 0) + 1);
|
|
155
|
+
}
|
|
156
|
+
let own = 0;
|
|
157
|
+
let other = 0;
|
|
158
|
+
for (const [q, n] of votes) {
|
|
159
|
+
const share = n / Math.max(weight(line), weight(M.get(q)));
|
|
160
|
+
if (q === i) own = share;
|
|
161
|
+
else if (share > other) other = share;
|
|
162
|
+
}
|
|
163
|
+
return { own, other };
|
|
164
|
+
};
|
|
165
|
+
const judged = new Map();
|
|
166
|
+
/** what the cells of line i say: {same, twin: reads the same but so does another line, other: reads as another line more than as itself} */
|
|
167
|
+
const judge = (i) => {
|
|
168
|
+
if (judged.has(i)) return judged.get(i);
|
|
169
|
+
const a = A.get(i);
|
|
170
|
+
const b = B.get(i);
|
|
171
|
+
const v = { same: false, twin: false, other: false };
|
|
172
|
+
if (a && b && a.sig === b.sig) {
|
|
173
|
+
sigs ||= { A: count(A), B: count(B) };
|
|
174
|
+
v.twin = sigs.A.get(a.sig) > 1 || sigs.B.get(b.sig) > 1;
|
|
175
|
+
v.same = !v.twin;
|
|
176
|
+
} else if (work > WORK) v.other = true;
|
|
177
|
+
else if (a || b) {
|
|
178
|
+
ix ||= { A: index(A), B: index(B) };
|
|
179
|
+
const x = b ? shares(i, b, A, ix.A) : { own: 1, other: 0 };
|
|
180
|
+
const y = a ? shares(i, a, B, ix.B) : { own: 1, other: 0 };
|
|
181
|
+
v.same = !!a && !!b && x.own >= 0.5 && y.own >= 0.5 && x.other < x.own && y.other < y.own;
|
|
182
|
+
v.other = (!!b && x.other > x.own) || (!!a && y.other > y.own);
|
|
183
|
+
}
|
|
184
|
+
judged.set(i, v);
|
|
185
|
+
return v;
|
|
186
|
+
};
|
|
187
|
+
const pinned = (i) => !judge(i).other && (i === 0 || judge(i - 1).same) && (judge(i + 1).same || (i === endA && i === endB));
|
|
188
|
+
/** line i, which has cells in one version at least, is not in doubt */
|
|
189
|
+
const held = (i) => judge(i).same || judge(i).twin || pinned(i) || (!A.has(i) && i > endA);
|
|
190
|
+
const isSteady = () => (steady ??= [...new Set([...A.keys(), ...B.keys()])].every(held));
|
|
191
|
+
const fits = (i) => {
|
|
192
|
+
if (!A.has(i)) return !B.has(i) && (i > Math.max(endA, endB) || isSteady());
|
|
193
|
+
return judge(i).same || pinned(i) || (judge(i).twin && isSteady());
|
|
194
|
+
};
|
|
195
|
+
return { fits };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Where on the agent's sheet the cells do not argue against a value of the user: `cell(r, c)`. */
|
|
199
|
+
function placing(sdA, sdB) {
|
|
200
|
+
const rows = axis(lines(sdA, true), lines(sdB, true));
|
|
201
|
+
const cols = axis(lines(sdA, false), lines(sdB, false));
|
|
202
|
+
return { cell: (r, c) => rows.fits(r) && cols.fits(c) };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ---- how the agent changed the file: facts from the server's record of the run --------------------------------------------
|
|
206
|
+
// Ops that leave every cell at its address. Any other op moves cells on the sheet it names or, naming none, on every sheet.
|
|
207
|
+
const ADDRESS_STABLE = new Set(["set_cells", "format_cells", "replace_all_text", "add_sheet"]);
|
|
208
|
+
const lower = (n) => String(n).toLowerCase(); // the engine finds a sheet by its name in any case when that is unambiguous
|
|
209
|
+
const RANK = { stable: 0, unknown: 1, moved: 2 };
|
|
210
|
+
const UNKNOWN = { of: () => "unknown" };
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* What the agent did to each sheet between the page's base and the file on disk. `steps` is the server's record of the writes that
|
|
214
|
+
* lead from one to the other, oldest first (src/session.js): an `apply` write ({route: "apply", from, to: bytes before and after,
|
|
215
|
+
* ops: the validated ops, partial, anchors: the delivered batch's}), or an earlier pass of this merge ({route: "merge", stable: the
|
|
216
|
+
* sheets on which it kept every address}). Any other route (`commit`) or no record at all says nothing. An apply write counts only
|
|
217
|
+
* when its ops, run again here on `from`, give `to` cell for cell: what the agent's process sent is checked, not believed.
|
|
218
|
+
* Returns {of(sheet name)}: "stable" (every cell kept its address), "moved" (an op that changes which address a cell lives at
|
|
219
|
+
* touched the sheet) or "unknown". Ceiling: one engine run per apply write of the run, on every merge.
|
|
220
|
+
*/
|
|
221
|
+
export async function agentChanges(steps, { runOps, name }) {
|
|
222
|
+
if (!Array.isArray(steps) || !steps.length) return UNKNOWN;
|
|
223
|
+
const each = [];
|
|
224
|
+
try {
|
|
225
|
+
for (const st of steps) {
|
|
226
|
+
if (st.route === "merge" && Array.isArray(st.stable)) {
|
|
227
|
+
const kept = new Set(st.stable.map(lower));
|
|
228
|
+
each.push((n) => (kept.has(lower(n)) ? "stable" : "unknown"));
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (st.route !== "apply" || !Array.isArray(st.ops) || !st.ops.length || !st.from || !st.to) return UNKNOWN;
|
|
232
|
+
const anchors = new Map((st.anchors || []).map((a) => [a.id, a]));
|
|
233
|
+
const again = await runOps({ bytes: st.from, format: "xlsx", name, ops: st.ops, partial: st.partial === true, anchors });
|
|
234
|
+
if (!again.bytes) return UNKNOWN;
|
|
235
|
+
const [to, out] = [parseWorkbook(st.to), parseWorkbook(again.bytes)];
|
|
236
|
+
const capped = (wb) => wb.sheets.some((sh) => wb.sheetCells(sh).truncated); // cells past the parse cap cannot be compared
|
|
237
|
+
if (capped(to) || capped(out) || verify(to, out, []).foreign) return UNKNOWN;
|
|
238
|
+
const moved = new Set();
|
|
239
|
+
let all = false;
|
|
240
|
+
for (const op of st.ops) {
|
|
241
|
+
if (ADDRESS_STABLE.has(op.op)) continue;
|
|
242
|
+
if (typeof op.target?.sheet === "string") moved.add(lower(op.target.sheet));
|
|
243
|
+
else all = true;
|
|
244
|
+
}
|
|
245
|
+
each.push((n) => (all || moved.has(lower(n)) ? "moved" : "stable"));
|
|
246
|
+
}
|
|
247
|
+
} catch {
|
|
248
|
+
return UNKNOWN;
|
|
249
|
+
}
|
|
250
|
+
return { of: (n) => each.reduce((worst, f) => (RANK[f(n)] > RANK[worst] ? f(n) : worst), "stable") };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ---- what the page says the user did (never a reason to write, only to hold back or to say what was not carried) ------------
|
|
254
|
+
const LAYOUT = new Set(["sheet.rowHeight", "sheet.colWidth", "sheet.hideRows", "sheet.hideCols", "sheet.gridlines", "sheet.freeze"]);
|
|
255
|
+
const VALUES = new Set(["cell", "replace", "undo", "redo"]);
|
|
256
|
+
|
|
257
|
+
function readEdits(edits) {
|
|
258
|
+
const out = { moved: new Set(), movedAll: false, format: new Set(), layout: new Set(), book: false };
|
|
259
|
+
for (const e of edits || []) {
|
|
260
|
+
const kind = String(e?.kind ?? "");
|
|
261
|
+
const sheet = typeof e?.sheet === "string" ? e.sheet : null;
|
|
262
|
+
if (VALUES.has(kind)) continue;
|
|
263
|
+
if (kind === "format") out.format.add(sheet);
|
|
264
|
+
else if (LAYOUT.has(kind)) out.layout.add(sheet);
|
|
265
|
+
else if (kind.startsWith("book.")) out.book = true;
|
|
266
|
+
else if (sheet == null) out.movedAll = true; // rows, columns, sort, merged cells, an import, or a kind this code does not know
|
|
267
|
+
else out.moved.add(sheet);
|
|
268
|
+
}
|
|
269
|
+
return out;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Plans the merge. Arguments are parsed workbooks (parseWorkbook), the page's edit kinds ([{kind, sheet}]) and what the agent did
|
|
274
|
+
* to each sheet (`agentChanges`; without it only sheets the agent left as they were are merged). Returns
|
|
275
|
+
* {items: [{label, text, sheet, k, want, op}], unplaced: [{label, text, why}], copy, stable}: every item is tried on `theirs`; `copy`
|
|
276
|
+
* asks the caller to keep the user's whole file, because an unplaced item stands for more than it can show; `stable` names the
|
|
277
|
+
* sheets on which the user's version and the agent's still share every address (what a second pass of this merge starts from).
|
|
278
|
+
*/
|
|
279
|
+
export function planXlsxMerge(base, mine, theirs, edits = [], agent = UNKNOWN) {
|
|
280
|
+
const said = readEdits(edits);
|
|
281
|
+
const items = [];
|
|
282
|
+
const unplaced = [];
|
|
283
|
+
let copy = false;
|
|
284
|
+
const whole = (label, why, text = "") => (unplaced.push({ label, text: cut(text), why }), (copy = true));
|
|
285
|
+
const inMine = new Map(mine.sheets.map((s) => [sheetKey(s), s]));
|
|
286
|
+
const inTheirs = new Map(theirs.sheets.map((s) => [sheetKey(s), s]));
|
|
287
|
+
const inBase = new Set(base.sheets.map(sheetKey));
|
|
288
|
+
let sheetsChanged = false;
|
|
289
|
+
const memo = (fn, m = new Map()) => (b) => (m.has(sheetKey(b)) ? m.get(sheetKey(b)) : m.set(sheetKey(b), fn(b)).get(sheetKey(b)));
|
|
290
|
+
const movedMine = memo((b) => said.movedAll || said.moved.has(b.name) || sheetMoved(base.sheetCells(b), mine.sheetCells(inMine.get(sheetKey(b)))));
|
|
291
|
+
const movedTheirs = memo((b) => sheetMoved(base.sheetCells(b), theirs.sheetCells(inTheirs.get(sheetKey(b)))));
|
|
292
|
+
/** the agent's sheet is cell for cell the base */
|
|
293
|
+
const untouched = (b) => {
|
|
294
|
+
const A = base.sheetCells(b);
|
|
295
|
+
const T = theirs.sheetCells(inTheirs.get(sheetKey(b)));
|
|
296
|
+
if (A.truncated || T.truncated || A.cells.size !== T.cells.size) return false;
|
|
297
|
+
for (const [k, cell] of A.cells) if (!same(cell, T.cells.get(k))) return false;
|
|
298
|
+
return true;
|
|
299
|
+
};
|
|
300
|
+
/** on the agent's side, may sheet b be merged by address? "same" | "stable" (cases 1 and 2), else why not: "gone" | "moved" | "unknown" | "looks moved" */
|
|
301
|
+
const place = memo((b) => placing(base.sheetCells(b), theirs.sheetCells(inTheirs.get(sheetKey(b)))));
|
|
302
|
+
const theirSide = memo((b) => {
|
|
303
|
+
if (!inTheirs.has(sheetKey(b))) return "gone";
|
|
304
|
+
if (untouched(b)) return "same";
|
|
305
|
+
const fact = agent.of(b.name);
|
|
306
|
+
if (fact !== "stable") return fact;
|
|
307
|
+
return movedTheirs(b) ? "looks moved" : "stable";
|
|
308
|
+
});
|
|
309
|
+
const byAddress = (b) => ["same", "stable"].includes(theirSide(b));
|
|
310
|
+
/** the rows on which the agent's sheet differs from the base in any cell (a set_cells rebuild of a shift rewrites them) */
|
|
311
|
+
const agentRows = memo((b) => {
|
|
312
|
+
const A = base.sheetCells(b).cells;
|
|
313
|
+
const T = theirs.sheetCells(inTheirs.get(sheetKey(b))).cells;
|
|
314
|
+
const rows = new Set();
|
|
315
|
+
for (const k of new Set([...A.keys(), ...T.keys()])) if (!same(A.get(k), T.get(k))) rows.add(Math.floor(k / 16384));
|
|
316
|
+
return rows;
|
|
317
|
+
});
|
|
318
|
+
// a formula points at cells by address, on any sheet, and any cell the agent set may hold another row's content now: it is
|
|
319
|
+
// carried only when the agent left every sheet as it was
|
|
320
|
+
let still;
|
|
321
|
+
const formulasFit = () => (still ??= base.sheets.every((b) => inMine.has(sheetKey(b)) && !movedMine(b) && theirSide(b) === "same"));
|
|
322
|
+
const theirWhy = {
|
|
323
|
+
gone: (b) => `The agent removed or renamed the sheet ${named(b)}, so your edits there were not carried over. These are yours.`,
|
|
324
|
+
moved: (b) => `The agent added, removed or moved rows or columns on the sheet ${named(b)}, so your edits there were not carried over. These are yours.`,
|
|
325
|
+
unknown: (b) => `The agent changed the sheet ${named(b)} in a way Onionskin cannot check cell by cell, so your edits there were not carried over. These are yours.`,
|
|
326
|
+
"looks moved": (b) => `The agent's changes on the sheet ${named(b)} look like rows or columns moved, so your edits there were not carried over. These are yours.`,
|
|
327
|
+
};
|
|
328
|
+
for (const s of mine.sheets) {
|
|
329
|
+
if (inBase.has(sheetKey(s))) continue;
|
|
330
|
+
sheetsChanged = true;
|
|
331
|
+
whole(`Sheet ${named(s)}`, `You added or renamed the sheet ${named(s)}. Sheet changes are not carried over.`);
|
|
332
|
+
}
|
|
333
|
+
for (const b of base.sheets) {
|
|
334
|
+
const m = inMine.get(sheetKey(b));
|
|
335
|
+
if (!m) {
|
|
336
|
+
sheetsChanged = true;
|
|
337
|
+
unplaced.push({ label: `Sheet ${named(b)}`, text: "", why: `You removed or renamed the sheet ${named(b)}. Sheet changes are not carried over.` });
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const label = (r, c) => `${untrusted(b.name, 60)}!${a1(r, c)}`;
|
|
341
|
+
const sdB = base.sheetCells(b);
|
|
342
|
+
const sdM = mine.sheetCells(m);
|
|
343
|
+
const changed = [];
|
|
344
|
+
for (const k of new Set([...sdB.cells.keys(), ...sdM.cells.keys()])) if (!same(sdB.cells.get(k), sdM.cells.get(k))) changed.push(k);
|
|
345
|
+
changed.sort((x, y) => x - y);
|
|
346
|
+
const mineMoved = movedMine(b);
|
|
347
|
+
if (!changed.length && !mineMoved) continue;
|
|
348
|
+
const values = () => {
|
|
349
|
+
let out = "";
|
|
350
|
+
for (const k of changed) {
|
|
351
|
+
if (out.length > CAP) break; // the rest is in the copy of the user's file
|
|
352
|
+
out += `${out ? "\n" : ""}${a1(Math.floor(k / 16384), k % 16384)}: ${sdM.cells.get(k)?.text ?? "(emptied)"}`;
|
|
353
|
+
}
|
|
354
|
+
return out;
|
|
355
|
+
};
|
|
356
|
+
const t = inTheirs.get(sheetKey(b));
|
|
357
|
+
if (mineMoved) whole(`Sheet ${named(b)}`, `You added, removed or moved rows or columns on the sheet ${named(b)}. That is not carried over. These cells differ in your version.`, values());
|
|
358
|
+
else if (!byAddress(b)) whole(`Sheet ${named(b)}`, theirWhy[theirSide(b)](b), values());
|
|
359
|
+
else if (items.length + changed.length > MAX_OPS) whole(`Sheet ${named(b)}`, `You changed more than ${MAX_OPS} cells while the agent worked. The cells on the sheet ${named(b)} were not carried over. These are yours.`, values());
|
|
360
|
+
else {
|
|
361
|
+
const sdT = theirs.sheetCells(t);
|
|
362
|
+
for (const k of changed) {
|
|
363
|
+
const r = Math.floor(k / 16384);
|
|
364
|
+
const c = k % 16384;
|
|
365
|
+
const want = sdM.cells.get(k);
|
|
366
|
+
const was = sdB.cells.get(k);
|
|
367
|
+
const now = sdT.cells.get(k);
|
|
368
|
+
if (same(now, want)) continue; // the agent wrote the same value
|
|
369
|
+
if (!same(now, was)) {
|
|
370
|
+
unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed ${label(r, c)} too. Its value is in the sheet; this is yours.` : `You emptied ${label(r, c)}, but the agent changed it, so it was kept.` });
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (theirSide(b) === "stable" && agentRows(b).has(r)) {
|
|
374
|
+
unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed other cells in the same row as ${label(r, c)}, so that row may hold other content now. Your value was not carried over. This is yours.` : `You emptied ${label(r, c)}, but the agent changed other cells in the same row, so it was kept.` });
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (theirSide(b) === "stable" && !place(b).cell(r, c)) {
|
|
378
|
+
unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed so much around ${label(r, c)} that it may not be the same cell any more, so your value was not carried over. This is yours.` : `You emptied ${label(r, c)}, but the agent changed so much around it that it may not be the same cell any more, so it was kept.` });
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
if (want?.k === "formula" && !formulasFit()) {
|
|
382
|
+
unplaced.push({ label: label(r, c), text: cut(want.text), why: `Rows or columns may have moved while the agent worked, so your formula for ${label(r, c)} may point at other cells now. It was not carried over. This is yours.` });
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
items.push({ label: label(r, c), text: cut(want?.text ?? ""), sheet: sheetKey(b), k, want, op: { op: "set_cells", target: { sheet: b.name, range: a1(r, c) }, expect: { text_hash: exactHash(was?.text ?? "") }, values: want ? want.text : null } });
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const order = (wb) => wb.sheets.map(sheetKey).filter((x) => inBase.has(x) && inMine.has(x)).join("\n");
|
|
390
|
+
if (!sheetsChanged && (said.book || order(base) !== order(mine))) unplaced.push({ label: "Sheets", text: "", why: "You moved, hid or changed sheets. Sheet changes are not carried over." });
|
|
391
|
+
const where = (sheet) => (sheet == null ? "" : ` on the sheet "${untrusted(sheet, 60)}"`);
|
|
392
|
+
for (const sheet of said.format) unplaced.push({ label: "Formatting", text: "", why: `You changed formatting${where(sheet)}. Formatting is not carried over.` });
|
|
393
|
+
for (const sheet of said.layout) unplaced.push({ label: "Layout", text: "", why: `You changed row or column sizes, hidden rows or columns, or the frozen panes${where(sheet)}. That is not carried over.` });
|
|
394
|
+
const stable = base.sheets.filter((b) => inMine.has(sheetKey(b)) && !movedMine(b) && byAddress(b)).map((b) => b.name);
|
|
395
|
+
return { items, unplaced, copy, stable };
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* What the result must be: the agent's file, with nothing different except the carried cells, each reading as the user's. A cell
|
|
400
|
+
* that was there before keeps its number format (typing a value does not change one), carried or not.
|
|
401
|
+
*/
|
|
402
|
+
function verify(theirs, out, applied) {
|
|
403
|
+
const bad = new Set();
|
|
404
|
+
if (theirs.sheets.map(sheetKey).join("\n") !== out.sheets.map(sheetKey).join("\n")) return { foreign: true, bad };
|
|
405
|
+
const carried = new Map(applied.map((it) => [`${it.sheet}\n${it.k}`, it]));
|
|
406
|
+
for (const [i, s] of theirs.sheets.entries()) {
|
|
407
|
+
const T = theirs.sheetCells(s).cells;
|
|
408
|
+
const O = out.sheetCells(out.sheets[i]).cells;
|
|
409
|
+
for (const k of new Set([...T.keys(), ...O.keys()])) {
|
|
410
|
+
const t = T.get(k);
|
|
411
|
+
const o = O.get(k);
|
|
412
|
+
const it = carried.get(`${sheetKey(s)}\n${k}`);
|
|
413
|
+
const format = !t || !o || theirs.numberFormat(t.s) === out.numberFormat(o.s);
|
|
414
|
+
if (it ? same(o, it.want) && format : same(o, t) && format) continue;
|
|
415
|
+
if (!it) return { foreign: true, bad };
|
|
416
|
+
bad.add(it);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return { foreign: false, bad };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Runs the plan on the agent's bytes. `runOps` is injected (src/ops/run.js); `agent` is the server's record of the run (see
|
|
424
|
+
* `agentChanges`). Returns {bytes: Buffer|null, merged, unplaced, copy, stable}.
|
|
425
|
+
* The proof is the result: bytes are returned only when the saved file is the agent's file plus exactly the carried cells. A cell
|
|
426
|
+
* that did not land as the user had it is taken out and the rest is run again from the agent's bytes (once), so it is never written.
|
|
427
|
+
*/
|
|
428
|
+
export async function mergeXlsx({ base, mine, theirs, name, runOps, edits = [], agent = null }) {
|
|
429
|
+
const T = parseWorkbook(theirs);
|
|
430
|
+
const plan = planXlsxMerge(parseWorkbook(base), parseWorkbook(mine), T, edits, await agentChanges(agent, { runOps, name }));
|
|
431
|
+
const unplaced = [...plan.unplaced];
|
|
432
|
+
const fail = (it, why) => unplaced.push({ label: it.label, text: it.text, why });
|
|
433
|
+
const end = (bytes, merged) => ({ bytes, merged, unplaced, copy: plan.copy, stable: plan.stable });
|
|
434
|
+
let items = plan.items;
|
|
435
|
+
for (let pass = 0; pass < 2 && items.length; pass++) {
|
|
436
|
+
const { results, bytes, blocked } = await runOps({ bytes: theirs, format: "xlsx", name, ops: items.map((x) => x.op), partial: true });
|
|
437
|
+
if (!bytes) {
|
|
438
|
+
for (const it of items) fail(it, blocked ? "Carrying your edits over would have damaged the file, so none were applied." : `Your value for ${it.label} could not be placed on the agent's version.`);
|
|
439
|
+
return end(null, 0);
|
|
440
|
+
}
|
|
441
|
+
const applied = items.filter((_, n) => results[n]?.status === "applied");
|
|
442
|
+
const { foreign, bad } = verify(T, parseWorkbook(bytes), applied);
|
|
443
|
+
if (foreign) break;
|
|
444
|
+
for (const it of items) {
|
|
445
|
+
if (!applied.includes(it)) fail(it, `Your value for ${it.label} could not be placed on the agent's version.`);
|
|
446
|
+
else if (bad.has(it)) fail(it, `Your value for ${it.label} did not land as typed, so the agent's version was kept there.`);
|
|
447
|
+
}
|
|
448
|
+
if (!bad.size) return end(bytes, applied.length);
|
|
449
|
+
items = applied.filter((it) => !bad.has(it));
|
|
450
|
+
}
|
|
451
|
+
for (const it of items) fail(it, "Carrying your edits over would have changed the agent's values, so none were applied.");
|
|
452
|
+
return end(null, 0);
|
|
453
|
+
}
|