onionskin-cli 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +136 -0
- package/INSTALL.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE +46 -0
- package/README.md +88 -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 +54 -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 +247 -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 +1910 -0
- package/src/settings.js +161 -0
- package/src/setup.js +160 -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-CFISATkn.js +197 -0
- package/web/dist/assets/index-spXE4oTz.css +1 -0
- package/web/dist/index.html +14 -0
package/src/local.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Runs on this Mac a whole Send that Onionskin can do without an agent. A local run is an ordinary delivery to a holder that lives
|
|
2
|
+
// inside the server: the batch, the lease ticket, `reply` and `apply` (validation, bind, expect, protected and package checks), the
|
|
3
|
+
// server's agent-write with its compare-and-swap and snapshot, `done` with its checks, the activity entry and Revert are all the ones an
|
|
4
|
+
// agent's run takes. There is no second write path. When the runner cannot finish before anything is written, the batch goes to the agent
|
|
5
|
+
// as if this had never been tried (Sessions.handBack).
|
|
6
|
+
//
|
|
7
|
+
// What the runner writes comes from a planner (a closed pattern applied to the person's own words: idea 8), from the ops of an agent's
|
|
8
|
+
// review card the person accepted (idea 21), or from a quick fix the person saw and tapped Use on (idea 10, src/suggest.js).
|
|
9
|
+
// Document text and model output only ever fill `find`/`replace`/`text` fields, which the same checks as an agent's reply judge; nothing
|
|
10
|
+
// moves into the batch's instructions, and the batch is never changed.
|
|
11
|
+
//
|
|
12
|
+
// Ceiling: `apply` runs the engine on the server's main thread, as the merge does; a large file pauses other requests for that time.
|
|
13
|
+
// Upgrade path: run applyCommand in a worker with the same in-process port.
|
|
14
|
+
|
|
15
|
+
import { quickPlanner } from "./quick-plan.js";
|
|
16
|
+
|
|
17
|
+
export const LOCAL_HOLDER = "onionskin-local";
|
|
18
|
+
|
|
19
|
+
/** The note every comment of a local run gets when its write was refused because the file changed (the person's words, docs/contracts/routes.md). */
|
|
20
|
+
export const REFUSED_NOTE = "The file changed while this was being done, so nothing was written. Send it again.";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A job that carries `reply` (a quick fix's "Use this", src/suggest.js) skips the planners: the server built that reply from the
|
|
24
|
+
* suggestion it stored, and it goes through the same `reply` call as a planner's.
|
|
25
|
+
*
|
|
26
|
+
* The planners of this build, in order. Each is {name, wants(s, batch) -> boolean (synchronous, pure: reads the person's own words and
|
|
27
|
+
* the session, never the disk), plan(s, batch) -> Promise<{reply} | null>}. `reply` is the JSON `onionskin reply` takes.
|
|
28
|
+
* Idea 8 (quick changes, src/quick-plan.js) is the only one: idea 10 applies nothing at Send.
|
|
29
|
+
*/
|
|
30
|
+
export const PLANNERS = [quickPlanner];
|
|
31
|
+
|
|
32
|
+
const PLAN_MS = 5000; // a planner that has not answered by then hands the batch to the agent
|
|
33
|
+
|
|
34
|
+
/** The delivery a CLI call names: batch, holder, token (all required by Sessions). Shared by the socket routes and the in-process port. */
|
|
35
|
+
export function leaseOf(body) {
|
|
36
|
+
const str = (v) => (typeof v === "string" && v ? v : null);
|
|
37
|
+
return { holder: str(body.holder), token: str(body.lease_token), batchId: str(body.batch_id), strict: true };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** `GET /cli/status`: what `apply`, `commit` and `done` read about a file's session. One function for the socket and the in-process port. */
|
|
41
|
+
export function cliStatus(sessions, s) {
|
|
42
|
+
const v = sessions.view(s);
|
|
43
|
+
return {
|
|
44
|
+
state: v.state, locked: v.locked, pollers: v.pollers, pending_batch: v.pendingBatchId, delivered_batch: v.deliveredBatchId,
|
|
45
|
+
lease_expires_at: v.leaseExpiresAt, comments: { draft: s.comments.filter((c) => c.status === "draft").length, total: s.comments.length },
|
|
46
|
+
unsent_edits: s.edits.length, activity: s.activity.length,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The person reverted a local run of these words: they go to the agent. True when any comment of the batch, or its whole note, is marked. */
|
|
51
|
+
export function skipsLocal(s, batch) {
|
|
52
|
+
const ids = new Set((batch.instructions?.comments || []).map((c) => c.id));
|
|
53
|
+
if (s.comments.some((c) => ids.has(c.id) && c.skipLocal)) return true;
|
|
54
|
+
const note = String(batch.instructions?.overall_note || "").trim();
|
|
55
|
+
return !!s.skipLocalNote && note !== "" && note === String(s.skipLocalNote).trim();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The CLI socket's routes that `reply` and `apply` call, answered in process by the same Sessions calls, for this one session only.
|
|
60
|
+
* An HttpError becomes {status, text} exactly as the socket's fail() writes it, so `apply` takes the same refusal paths.
|
|
61
|
+
*/
|
|
62
|
+
function inProcessPort(sessions, key) {
|
|
63
|
+
const answer = (fn) => {
|
|
64
|
+
try {
|
|
65
|
+
return { status: 200, text: JSON.stringify(fn()) };
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (Number.isInteger(err?.status)) return { status: err.status, text: JSON.stringify({ error: err.message, ...(err.extra || {}) }) };
|
|
68
|
+
return { status: 500, text: JSON.stringify({ error: "internal error" }) };
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const mine = (file) => {
|
|
72
|
+
const s = sessions.byFile(String(file || ""));
|
|
73
|
+
if (s.key !== key) throw Object.assign(new Error("no such session"), { status: 404 });
|
|
74
|
+
return s;
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
healthy: async () => true,
|
|
78
|
+
json: (t) => JSON.parse(t),
|
|
79
|
+
async req({ method, path: p }, body = {}) {
|
|
80
|
+
const u = new URL(p, "http://x");
|
|
81
|
+
const route = `${method} ${u.pathname}`;
|
|
82
|
+
if (route === "GET /cli/status") return answer(() => cliStatus(sessions, mine(u.searchParams.get("file"))));
|
|
83
|
+
if (route === "GET /cli/batch") return answer(() => { const s = mine(u.searchParams.get("file")); return { state: sessions.state(s), batch: s.delivered?.batch || null }; });
|
|
84
|
+
if (route === "POST /cli/agent-write") {
|
|
85
|
+
return answer(() => {
|
|
86
|
+
const s = mine(body.file);
|
|
87
|
+
const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
|
|
88
|
+
return sessions.agentWrite(s.key, { baseSha: String(body.base_sha256 || ""), bytes, route: "apply", ops: Array.isArray(body.ops) ? body.ops : null, partial: body.partial === true, ...leaseOf(body) });
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (route === "POST /cli/apply-report") {
|
|
92
|
+
return answer(() => {
|
|
93
|
+
const s = mine(body.file);
|
|
94
|
+
return sessions.noteApply(s.key, { ...leaseOf(body), run: typeof body.run === "string" ? body.run : "", problems: Array.isArray(body.problems) ? body.problems : [], applied: Array.isArray(body.applied) ? body.applied.filter((k) => typeof k === "string") : [] });
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return { status: 404, text: JSON.stringify({ error: "not found" }) };
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function within(promise, ms) {
|
|
103
|
+
let timer;
|
|
104
|
+
return Promise.race([Promise.resolve(promise).finally(() => clearTimeout(timer)), new Promise((resolve) => (timer = setTimeout(() => resolve(null), ms)))]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The runner. `finishRun(key, results, claim)` is the server's end of a run (the `/cli/done` body). `planners` defaults to PLANNERS;
|
|
109
|
+
* tests add a stub planner to `runner.planners`. Sessions asks `wants` once per batch id, synchronously, before any poller.
|
|
110
|
+
*/
|
|
111
|
+
export function localRunner(sessions, { finishRun, planners = [...PLANNERS] } = {}) {
|
|
112
|
+
const pick = (s, batch) =>
|
|
113
|
+
runner.planners.find((p) => {
|
|
114
|
+
try {
|
|
115
|
+
return p.wants(s, batch) === true;
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const runner = {
|
|
122
|
+
planners,
|
|
123
|
+
planMs: PLAN_MS,
|
|
124
|
+
wants: (s, batch) => !skipsLocal(s, batch) && !!pick(s, batch),
|
|
125
|
+
|
|
126
|
+
/** Runs the delivery named by `ticket` ({batchId, token, job?}, from the `local` event). Never throws. Resolves with what happened, for tests. */
|
|
127
|
+
async run(key, ticket) {
|
|
128
|
+
try {
|
|
129
|
+
return await attempt(key, ticket);
|
|
130
|
+
} catch (err) {
|
|
131
|
+
console.error(`local run failed: ${err?.code || err?.name || "error"}`); // never the message: it can quote the document
|
|
132
|
+
return giveUp(key, ticket);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const claimOf = (ticket) => ({ holder: LOCAL_HOLDER, token: ticket.token, batchId: ticket.batchId, strict: true });
|
|
138
|
+
const finish = (key, ticket, results) => finishRun(key, results, claimOf(ticket));
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Nothing was written: hand the batch to the agent (or drop an accepted suggestion). Refused once a write landed: then the run is left to
|
|
142
|
+
* the lease expiry, which records what is in the file and gives the batch to the agent (as for an agent that stopped responding).
|
|
143
|
+
*/
|
|
144
|
+
async function giveUp(key, ticket) {
|
|
145
|
+
let s;
|
|
146
|
+
try {
|
|
147
|
+
s = sessions.get(key);
|
|
148
|
+
} catch {
|
|
149
|
+
return "gone";
|
|
150
|
+
}
|
|
151
|
+
const accept = !!s.delivered?.accept;
|
|
152
|
+
try {
|
|
153
|
+
if (accept) sessions.dropLocal(key, claimOf(ticket));
|
|
154
|
+
else sessions.handBack(key, claimOf(ticket));
|
|
155
|
+
return accept ? "dropped" : "handed_back";
|
|
156
|
+
} catch (err) {
|
|
157
|
+
if (err?.extra?.code !== "written") return "stale"; // taken back, finished or handed on meanwhile: nothing of ours to settle
|
|
158
|
+
}
|
|
159
|
+
return "left";
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function finishDeclined(key, ticket, batch) {
|
|
163
|
+
const results = { summary: REFUSED_NOTE, results: (batch?.instructions?.comments || []).map((c) => ({ comment_id: c.id, status: "declined", note: REFUSED_NOTE })) };
|
|
164
|
+
try {
|
|
165
|
+
await finish(key, ticket, results);
|
|
166
|
+
return "declined";
|
|
167
|
+
} catch {
|
|
168
|
+
return "left"; // the lease expires and the tick records what is in the file; the batch then goes to the agent
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function attempt(key, ticket) {
|
|
173
|
+
let s;
|
|
174
|
+
try {
|
|
175
|
+
s = sessions.get(key);
|
|
176
|
+
} catch {
|
|
177
|
+
return "gone";
|
|
178
|
+
}
|
|
179
|
+
const d = s.delivered;
|
|
180
|
+
if (!d?.local || d.batch.batch_id !== ticket.batchId || d.lease.token !== ticket.token) return "stale"; // taken back or closed before this ran
|
|
181
|
+
const batch = d.batch;
|
|
182
|
+
const lease = `${ticket.batchId}:${ticket.token}`;
|
|
183
|
+
const server = inProcessPort(sessions, key);
|
|
184
|
+
const [{ ReplyError, replyCommand }, { ApplyError, applyCommand }] = await Promise.all([import("./reply.js"), import("./apply.js")]); // they pull in the engine
|
|
185
|
+
|
|
186
|
+
if (Array.isArray(ticket.job?.ops)) {
|
|
187
|
+
// an accepted suggestion of the agent's review: the stored ops, through apply (--partial) and done
|
|
188
|
+
let out;
|
|
189
|
+
try {
|
|
190
|
+
out = await applyCommand([s.file, "--lease", lease, "--partial", "--holder", LOCAL_HOLDER], server, { ops: { schema: "onionskin.ops/1", ops: ticket.job.ops }, batch });
|
|
191
|
+
} catch (err) {
|
|
192
|
+
if (!(err instanceof ApplyError)) throw err;
|
|
193
|
+
return giveUp(key, ticket);
|
|
194
|
+
}
|
|
195
|
+
if (!out.report.written) return giveUp(key, ticket);
|
|
196
|
+
// which ops landed, by their ids (the card ids): done marks those cards accepted and the rest stale
|
|
197
|
+
const accepted = out.report.results.filter((r) => r.status === "applied" && typeof r.id === "string").map((r) => r.id);
|
|
198
|
+
const results = { summary: typeof ticket.job.summary === "string" ? ticket.job.summary : "", results: [], accepted };
|
|
199
|
+
try {
|
|
200
|
+
await finish(key, ticket, results);
|
|
201
|
+
return "done";
|
|
202
|
+
} catch {
|
|
203
|
+
return retryFinish(key, ticket, results, batch);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// a quick fix the person tapped Use on (src/suggest.js): its reply was built by the server from its stored text, not planned now
|
|
208
|
+
const stored = ticket.job?.reply ? { reply: ticket.job.reply } : null;
|
|
209
|
+
const planner = stored ? null : pick(s, batch);
|
|
210
|
+
const plan = stored ?? (planner ? await within(Promise.resolve().then(() => planner.plan(s, batch)), runner.planMs).catch(() => null) : null);
|
|
211
|
+
if (!plan?.reply) return giveUp(key, ticket);
|
|
212
|
+
if (sessions.get(key).delivered !== d) return "stale"; // taken back while the planner worked
|
|
213
|
+
try {
|
|
214
|
+
await replyCommand([s.file, "--lease", lease, "--json", JSON.stringify(plan.reply)], {
|
|
215
|
+
server,
|
|
216
|
+
holder: LOCAL_HOLDER,
|
|
217
|
+
finish: (_file, results) => finish(key, ticket, results),
|
|
218
|
+
readStdin: async () => "",
|
|
219
|
+
});
|
|
220
|
+
return "done";
|
|
221
|
+
} catch (err) {
|
|
222
|
+
if (!(err instanceof ReplyError)) throw err;
|
|
223
|
+
const code = err.body?.code;
|
|
224
|
+
if (code === "done_failed") return retryFinish(key, ticket, err.body.results, batch);
|
|
225
|
+
// the server refused the write because the file changed: nothing was written, and the agent would meet the same file. Finish with the fixed note.
|
|
226
|
+
if (code === "outside_change" || err.body?.write_error?.code === "conflict") return finishDeclined(key, ticket, batch);
|
|
227
|
+
return giveUp(key, ticket); // no match, expect or protected refusal, a bad plan: nothing written, the agent gets the batch
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** `done` failed after the write: once more, then hand back if nothing was written, else leave it to the lease expiry (as for an agent). */
|
|
232
|
+
async function retryFinish(key, ticket, results, batch) {
|
|
233
|
+
try {
|
|
234
|
+
await finish(key, ticket, results);
|
|
235
|
+
return "done";
|
|
236
|
+
} catch {
|
|
237
|
+
return giveUp(key, ticket);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return runner;
|
|
242
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { minimalReplace } from "./merge.js";
|
|
2
|
+
import { findAll } from "./ops/runs.js";
|
|
3
|
+
import { parseDeck } from "./pptx.js";
|
|
4
|
+
import { untrusted } from "./sanitize.js";
|
|
5
|
+
|
|
6
|
+
// What the user changed in a deck while the agent worked, carried onto the agent's version. The same three versions as the DOCX
|
|
7
|
+
// merge (src/merge.js): `base` (what the page loaded), `mine` (base plus the user's edits) and `theirs` (the file after the agent).
|
|
8
|
+
// The unit is the text of one shape. A slide is matched by its p:sldId id and a shape by its id on that slide, which is how `apply`
|
|
9
|
+
// addresses them: both survive slides being added, removed or reordered, so text on a slide all three versions have is still
|
|
10
|
+
// placed when the agent changed the slide list. A shape only the user changed becomes ops on `theirs` (`replace_text_in_shape`
|
|
11
|
+
// per changed line, or `set_shape_text` when lines were added or removed) with the base text as `expect`, run through the same
|
|
12
|
+
// engine and package check as an agent's `apply`. A shape both changed keeps the agent's text and the user's text is handed back.
|
|
13
|
+
// Slides or shapes the user added, removed or moved are reported, never rebuilt, and the caller keeps a copy of the user's file.
|
|
14
|
+
// Ceilings: text only. Formatting, position, size, fill and slide changes are reported, not carried (the page names them in
|
|
15
|
+
// `edits`; one undone or redone with Undo/Redo is not reported). Table text is handed back. `set_shape_text` gives every line one
|
|
16
|
+
// format, so it is used only where the shape's text has one format; otherwise the text is handed back.
|
|
17
|
+
|
|
18
|
+
const CAP = 2000;
|
|
19
|
+
const cut = (t) => (t.length > CAP ? `${t.slice(0, CAP)}...` : t);
|
|
20
|
+
const sid = (s) => String(s.id ?? `#${s.index}`);
|
|
21
|
+
const key = (sh) => `${sh.id}#${sh.ordinal}`;
|
|
22
|
+
const keys = (sl) => sl.shapes.map((x) => `${key(x)}/${x.kind}`).join(" ");
|
|
23
|
+
const twins = (sl) => sl.shapes.some((x) => x.ordinal > 0);
|
|
24
|
+
const slideText = (sl) => sl.shapes.map((x) => x.text).filter(Boolean).join("\n\n");
|
|
25
|
+
const nameOf = (sl, sh) => untrusted(`${sh.isTitle ? "the title" : sh.name ? `"${sh.name}"` : `shape ${sh.id}`} on slide ${sl.index + 1}`, 100);
|
|
26
|
+
const mixed = (sh) => Object.values(sh.fmt || {}).some((v) => String(v).startsWith("~"));
|
|
27
|
+
|
|
28
|
+
// ---- what the page says the user did (never a reason to write, only to say what was not carried) ----------------------------
|
|
29
|
+
const TEXT = new Set(["text", "undo", "redo"]);
|
|
30
|
+
function readEdits(edits) {
|
|
31
|
+
const out = { format: new Set(), shapes: new Set(), slides: false, other: false };
|
|
32
|
+
for (const e of edits || []) {
|
|
33
|
+
const kind = String(e?.kind ?? "");
|
|
34
|
+
const slide = Number.isInteger(e?.slide) ? e.slide : null;
|
|
35
|
+
if (TEXT.has(kind)) continue;
|
|
36
|
+
if (kind === "format") out.format.add(slide);
|
|
37
|
+
else if (kind.startsWith("shape.")) out.shapes.add(slide);
|
|
38
|
+
else if (kind.startsWith("slide.")) out.slides = true;
|
|
39
|
+
else out.other = true;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The ops that turn the base text of one shape into the user's: one replace per changed line (last line first), else the whole text. */
|
|
45
|
+
function shapeOps(target, old, text) {
|
|
46
|
+
const B = old.text.split("\n");
|
|
47
|
+
const M = text.split("\n");
|
|
48
|
+
const expect = { text_hash: old.hash };
|
|
49
|
+
if (B.length === M.length) {
|
|
50
|
+
const ops = [];
|
|
51
|
+
for (let i = M.length - 1; i >= 0; i--) {
|
|
52
|
+
if (B[i] === M[i]) continue;
|
|
53
|
+
const r = minimalReplace(B[i], M[i]);
|
|
54
|
+
if (!r) return [{ op: "set_shape_text", target, expect, paragraphs: M }];
|
|
55
|
+
// the find text occurs once in its own line; lines before it are counted, and they are untouched when this op runs
|
|
56
|
+
const before = B.slice(0, i).reduce((n, line) => n + findAll(line, r.find).length, 0);
|
|
57
|
+
ops.push({ op: "replace_text_in_shape", target, expect, find: r.find, replace: r.replace, occurrence: before + 1 });
|
|
58
|
+
}
|
|
59
|
+
return ops;
|
|
60
|
+
}
|
|
61
|
+
return [{ op: "set_shape_text", target, expect, paragraphs: M }];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Plans the merge. Arguments are parsed decks (parseDeck) and the page's edit kinds ([{kind, slide}]). Returns
|
|
66
|
+
* {items: [{label, text, slide, key, want, ops}], unplaced: [{label, text, why}], copy}: every item is tried on `theirs`; `copy`
|
|
67
|
+
* asks the caller to keep the user's whole file, because an unplaced item stands for more than it can show.
|
|
68
|
+
*/
|
|
69
|
+
export function planPptxMerge(base, mine, theirs, edits = []) {
|
|
70
|
+
for (const d of [base, mine, theirs]) d.ensure();
|
|
71
|
+
const said = readEdits(edits);
|
|
72
|
+
const items = [];
|
|
73
|
+
const unplaced = [];
|
|
74
|
+
let copy = false;
|
|
75
|
+
const back = (label, why, text = "") => unplaced.push({ label, text: cut(text), why });
|
|
76
|
+
const whole = (label, why, text = "") => (back(label, why, text), (copy = true));
|
|
77
|
+
const inBase = new Map(base.slides.map((s) => [sid(s), s]));
|
|
78
|
+
const inMine = new Map(mine.slides.map((s) => [sid(s), s]));
|
|
79
|
+
const inTheirs = new Map(theirs.slides.map((s) => [sid(s), s]));
|
|
80
|
+
let slidesSaid = false;
|
|
81
|
+
for (const m of mine.slides) {
|
|
82
|
+
if (inBase.has(sid(m))) continue;
|
|
83
|
+
slidesSaid = true;
|
|
84
|
+
whole(`Slide ${m.index + 1}`, `You added slide ${m.index + 1}. Slide changes are not carried over.${slideText(m) ? " This is its text." : ""}`, slideText(m));
|
|
85
|
+
}
|
|
86
|
+
for (const b of base.slides) {
|
|
87
|
+
if (inMine.has(sid(b))) continue;
|
|
88
|
+
slidesSaid = true;
|
|
89
|
+
back(`Slide ${b.index + 1}`, `You removed the slide that was slide ${b.index + 1}. Slide changes are not carried over.`);
|
|
90
|
+
}
|
|
91
|
+
const order = (d) => d.slides.map(sid).filter((x) => inBase.has(x) && inMine.has(x)).join(" ");
|
|
92
|
+
if (order(base) !== order(mine)) {
|
|
93
|
+
slidesSaid = true;
|
|
94
|
+
back("Slides", "You moved slides. Slide changes are not carried over.");
|
|
95
|
+
}
|
|
96
|
+
if (said.slides && !slidesSaid) back("Slides", "You changed slides (hidden, background or order). Slide changes are not carried over.");
|
|
97
|
+
|
|
98
|
+
for (const m of mine.slides) {
|
|
99
|
+
const b = inBase.get(sid(m));
|
|
100
|
+
if (!b) continue;
|
|
101
|
+
const t = inTheirs.get(sid(m));
|
|
102
|
+
const n = m.index + 1;
|
|
103
|
+
// a shape is its id; where two shapes of a slide share one, the id means the same shape only while the shape list is unchanged
|
|
104
|
+
const sure = (![b, m, t].some((sl) => sl && twins(sl))) || (keys(b) === keys(m) && (!t || keys(b) === keys(t)));
|
|
105
|
+
if (!sure) {
|
|
106
|
+
const had = new Set(b.shapes.map((x) => x.text));
|
|
107
|
+
const mineOnly = m.shapes.filter((x) => x.text && !had.has(x.text)).map((x) => x.text);
|
|
108
|
+
if (mineOnly.length || keys(b) !== keys(m)) whole(`Slide ${n}`, `Shapes on slide ${n} share an id and shapes were added or removed there, so your edits could not be matched to the agent's version.${mineOnly.length ? " These are yours." : ""}`, mineOnly.join("\n\n"));
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const wasBy = new Map(b.shapes.map((x) => [key(x), x]));
|
|
112
|
+
const nowBy = new Map((t?.shapes || []).map((x) => [key(x), x]));
|
|
113
|
+
const mineKeys = new Set(m.shapes.map(key));
|
|
114
|
+
for (const old of b.shapes) if (!mineKeys.has(key(old))) back(`Slide ${n}`, `You removed ${nameOf(b, old)}. Shape changes are not carried over.`);
|
|
115
|
+
for (const sh of m.shapes) {
|
|
116
|
+
const old = wasBy.get(key(sh));
|
|
117
|
+
const label = untrusted(`Slide ${n}, ${sh.isTitle ? "Title" : sh.name ? sh.name : `shape ${sh.id}`}`, 100);
|
|
118
|
+
if (!old) {
|
|
119
|
+
whole(label, `You added ${nameOf(m, sh)}. Shape changes are not carried over.${sh.text ? " This is its text." : ""}`, sh.text);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (old.text === sh.text) continue;
|
|
123
|
+
const now = nowBy.get(key(sh));
|
|
124
|
+
if (!t) back(label, `The agent removed slide ${n}, so your edit to ${nameOf(m, sh)} was not carried over. This is yours.`, sh.text);
|
|
125
|
+
else if (!now) back(label, `The agent removed ${nameOf(m, sh)}, so your edit was not carried over. This is yours.`, sh.text);
|
|
126
|
+
else if (now.text === sh.text) continue; // the agent wrote the same text
|
|
127
|
+
else if (now.text !== old.text) back(label, `The agent changed ${nameOf(t, now)} too. Its text is on the slide; this is yours.`, sh.text);
|
|
128
|
+
else if (sh.kind !== "shape") back(label, `You changed ${nameOf(m, sh)}, which is not a plain text box (a table or a group). That is not carried over. This is yours.`, sh.text);
|
|
129
|
+
else {
|
|
130
|
+
const ops = shapeOps({ slide_id: sid(m), shape_id: sh.id }, old, sh.text);
|
|
131
|
+
if (ops[0].op === "set_shape_text" && mixed(now)) back(label, `You added or removed lines in ${nameOf(m, sh)}, whose text mixes formats. That cannot be carried over without losing them. This is yours.`, sh.text);
|
|
132
|
+
else items.push({ label, text: cut(sh.text), name: nameOf(t, now), slide: sid(m), key: key(sh), want: sh.text, ops });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const where = (slide) => (slide == null ? "" : ` on slide ${slide + 1}`);
|
|
137
|
+
for (const slide of said.format) back("Formatting", `You changed text formatting${where(slide)}. Formatting is not carried over.`);
|
|
138
|
+
for (const slide of said.shapes) whole("Shapes", `You added, removed, moved or restyled shapes${where(slide)}. Shape changes are not carried over.`);
|
|
139
|
+
if (said.other) whole("Other changes", "You made a change that is not text. It is not carried over.");
|
|
140
|
+
return { items, unplaced, copy };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** What the result must be: the agent's deck, with nothing different except the carried shapes, each reading as the user's. */
|
|
144
|
+
function verify(theirs, out, applied) {
|
|
145
|
+
out.ensure();
|
|
146
|
+
const bad = new Set();
|
|
147
|
+
if (theirs.slides.map(sid).join(" ") !== out.slides.map(sid).join(" ")) return { foreign: true, bad };
|
|
148
|
+
const carried = new Map(applied.map((it) => [`${it.slide} ${it.key}`, it]));
|
|
149
|
+
for (const [i, sl] of theirs.slides.entries()) {
|
|
150
|
+
const o = out.slides[i];
|
|
151
|
+
if (keys(sl) !== keys(o)) return { foreign: true, bad };
|
|
152
|
+
for (const [j, sh] of sl.shapes.entries()) {
|
|
153
|
+
const it = carried.get(`${sid(sl)} ${key(sh)}`);
|
|
154
|
+
if (it) {
|
|
155
|
+
if (o.shapes[j].text !== it.want) bad.add(it);
|
|
156
|
+
} else if (o.shapes[j].text !== sh.text || JSON.stringify(o.shapes[j].fmt) !== JSON.stringify(sh.fmt)) return { foreign: true, bad };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return { foreign: false, bad };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Runs the plan on the agent's bytes. `runOps` is injected (src/ops/run.js). Returns {bytes: Buffer|null, merged, unplaced, copy}.
|
|
164
|
+
* The proof is the result: bytes are returned only when the saved deck is the agent's deck plus exactly the carried shapes. A shape
|
|
165
|
+
* whose text 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
|
|
166
|
+
* written.
|
|
167
|
+
*/
|
|
168
|
+
export async function mergePptx({ base, mine, theirs, name, runOps, edits = [] }) {
|
|
169
|
+
const T = parseDeck(theirs);
|
|
170
|
+
const plan = planPptxMerge(parseDeck(base), parseDeck(mine), T, edits);
|
|
171
|
+
const unplaced = [...plan.unplaced];
|
|
172
|
+
const fail = (it, why) => unplaced.push({ label: it.label, text: it.text, why });
|
|
173
|
+
const end = (bytes, merged) => ({ bytes, merged, unplaced, copy: plan.copy });
|
|
174
|
+
let items = plan.items;
|
|
175
|
+
for (let pass = 0; pass < 2 && items.length; pass++) {
|
|
176
|
+
const owner = items.flatMap((it) => it.ops.map(() => it));
|
|
177
|
+
const { results, bytes, blocked } = await runOps({ bytes: theirs, format: "pptx", name, ops: items.flatMap((it) => it.ops), partial: true });
|
|
178
|
+
const status = (it) => owner.map((o, n) => (o === it ? results[n]?.status : null)).filter(Boolean);
|
|
179
|
+
const notPlaced = (it) => (status(it).includes("skipped_protected") ? `Your edit to ${it.name} was not carried over: the text box holds content that cannot be rewritten safely (a field or an equation). This is yours.` : `Your edit to ${it.name} could not be placed on the agent's version. This is yours.`);
|
|
180
|
+
if (!bytes) {
|
|
181
|
+
for (const it of items) fail(it, blocked ? "Carrying your edits over would have damaged the file, so none were applied." : notPlaced(it));
|
|
182
|
+
return end(null, 0);
|
|
183
|
+
}
|
|
184
|
+
const applied = items.filter((it) => status(it).every((x) => x === "applied"));
|
|
185
|
+
// a shape with one op applied and another not reads as neither version: these bytes are not written, the rest is run again
|
|
186
|
+
const half = items.some((it) => !applied.includes(it) && status(it).includes("applied"));
|
|
187
|
+
const { foreign, bad } = half ? { foreign: false, bad: new Set() } : verify(T, parseDeck(bytes), applied);
|
|
188
|
+
if (foreign) break;
|
|
189
|
+
for (const it of items) {
|
|
190
|
+
if (!applied.includes(it)) fail(it, notPlaced(it));
|
|
191
|
+
else if (bad.has(it)) fail(it, `Your edit to ${it.name} did not land as typed, so the agent's version was kept there. This is yours.`);
|
|
192
|
+
}
|
|
193
|
+
if (!bad.size && !half) return end(bytes, applied.length);
|
|
194
|
+
items = applied.filter((it) => !bad.has(it));
|
|
195
|
+
}
|
|
196
|
+
for (const it of items) fail(it, "Carrying your edits over would have changed the agent's text, so none were applied.");
|
|
197
|
+
return end(null, 0);
|
|
198
|
+
}
|