onionskin-cli 0.0.3 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +198 -0
- package/INSTALL.md +5 -1
- package/NOTICE +34 -0
- package/README.md +102 -34
- package/docs/contracts/batch.schema.json +27 -4
- package/docs/contracts/ops.schema.json +117 -5
- package/package.json +4 -2
- package/scripts/checker/check.py +24 -5
- package/scripts/checker/requirements.txt +482 -5
- package/scripts/requirements.txt +482 -5
- package/scripts/rewriter/apple/main.swift +1 -1
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +1 -1
- package/skills/onionskin/SKILL.md +12 -9
- package/skills/onionskin/reference/batch.md +5 -3
- package/skills/onionskin/reference/formats.md +5 -1
- package/skills/onionskin/reference/ops.md +48 -4
- package/skills/onionskin/reference/troubleshooting.md +31 -0
- package/src/agent-detect.js +28 -2
- package/src/agent-prompt.js +12 -1
- package/src/agent-runner.js +110 -20
- package/src/anchor-work.js +154 -0
- package/src/anchor-worker.js +38 -0
- package/src/anchors.js +61 -11
- package/src/apply.js +119 -47
- package/src/batch.js +32 -8
- package/src/check-worker.js +8 -5
- package/src/checker.js +238 -33
- package/src/cli.js +248 -42
- package/src/constants.js +34 -1
- package/src/diff.js +3 -1
- package/src/doctor.js +12 -3
- package/src/docx.js +464 -87
- package/src/edit-hash.js +19 -0
- package/src/fsutil.js +14 -6
- package/src/history-worker.js +8 -8
- package/src/history.js +144 -31
- package/src/html-active.js +384 -0
- package/src/html-envelope.js +51 -0
- package/src/html-parser.js +8 -0
- package/src/html-render-worker.js +25 -0
- package/src/html-render.js +939 -0
- package/src/html-utf8.js +65 -0
- package/src/html-write-worker.js +14 -0
- package/src/html-write.js +380 -0
- package/src/html.js +1259 -0
- package/src/local.js +68 -4
- package/src/merge-package.js +1166 -0
- package/src/merge-pptx.js +98 -38
- package/src/merge-proof.js +1019 -0
- package/src/merge-worker.js +60 -0
- package/src/merge-xlsx.js +84 -29
- package/src/merge.js +286 -83
- package/src/nfc.js +46 -0
- package/src/notes.js +122 -0
- package/src/offload.js +419 -1
- package/src/open-worker.js +27 -0
- package/src/ops/common.js +2 -1
- package/src/ops/docx-table.js +5 -3
- package/src/ops/docx.js +58 -29
- package/src/ops/html.js +394 -0
- package/src/ops/pptx.js +82 -23
- package/src/ops/run.js +23 -3
- package/src/ops/runs.js +215 -13
- package/src/ops/schema.js +8 -1
- package/src/ops/xlsx.js +121 -10
- package/src/ops-worker.js +17 -0
- package/src/package.js +43 -4
- package/src/person.js +2 -1
- package/src/pptx.js +323 -23
- package/src/question.js +10 -3
- package/src/quick-plan.js +13 -6
- package/src/quick.js +1 -1
- package/src/redline-check.js +12 -2
- package/src/redline-worker.js +10 -14
- package/src/reply.js +46 -19
- package/src/retain.js +223 -0
- package/src/review.js +32 -18
- package/src/rewrite-check.js +2 -2
- package/src/rewriter.js +38 -8
- package/src/sanitize.js +16 -3
- package/src/server-entry.js +4 -3
- package/src/server.js +175 -62
- package/src/session.js +1151 -210
- package/src/setup.js +20 -2
- package/src/stop-gate.js +34 -0
- package/src/suggest-reply.js +42 -7
- package/src/suggest.js +14 -29
- package/src/text.js +161 -11
- package/src/update.js +517 -0
- package/src/voice-guide.js +15 -9
- package/src/voice-learn.js +15 -4
- package/src/voice-sample-worker.js +7 -6
- package/src/voice-store.js +11 -4
- package/src/voice-worker.js +4 -6
- package/src/voice.js +2 -1
- package/src/worker-host.js +23 -0
- package/src/xlsx.js +378 -97
- package/src/xml-tree.js +6 -3
- package/src/xml.js +82 -34
- package/src/zip-write.js +10 -5
- package/src/zip.js +73 -18
- package/vendor/parse5/LICENSE.txt +32 -0
- package/vendor/parse5/parse5.mjs +7822 -0
- package/vendor/parse5.lock.json +23 -0
- package/web/dist/assets/{index-DS-WST1E.js → index-CjsTf5gj.js} +58 -52
- package/web/dist/assets/index-DnbLtHhG.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BCAEKiOG.css +0 -1
package/src/cli.js
CHANGED
|
@@ -4,7 +4,8 @@ import http from "node:http";
|
|
|
4
4
|
import net from "node:net";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import tty from "node:tty";
|
|
7
|
-
import
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
8
9
|
import { fileURLToPath } from "node:url";
|
|
9
10
|
import { PRODUCT, SERVER_PROTOCOL } from "./constants.js";
|
|
10
11
|
import { holderFor, loadHolder, loadTicketRecord, parseTicket, saveHolder } from "./lease.js";
|
|
@@ -14,14 +15,14 @@ import { ReplyError, replyCommand } from "./reply.js";
|
|
|
14
15
|
import { setupChecker } from "./checker.js";
|
|
15
16
|
import { runSetup } from "./setup.js";
|
|
16
17
|
import { expandShortcut, personOpenText, personPollText, pickAudience, shellQuote } from "./person.js";
|
|
17
|
-
import { findClaude, findCodex, isAgentHolder } from "./agent-detect.js";
|
|
18
|
+
import { NOT_YOUR_FILE, agentMayUse, findClaude, findCodex, isAgentHolder } from "./agent-detect.js";
|
|
18
19
|
import { BG_PRESETS, DEFAULT_BACKGROUND, MAX_IMAGE_BYTES, SettingsError, describeBackground, readSettings, saveCustomImage, writeBackground } from "./settings.js";
|
|
19
20
|
|
|
20
21
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
21
22
|
const BIN = PRODUCT.bin;
|
|
22
23
|
const HELP = `${PRODUCT.name}
|
|
23
24
|
|
|
24
|
-
${BIN} open <file.docx|xlsx|pptx> [--no-open] [--no-agent|--agent] Open the file in the browser editor (also: ${BIN} <file>). At a terminal it also starts Claude Code on it in the background; --no-agent only opens the page, --agent prints the agent's output
|
|
25
|
+
${BIN} open <file.docx|xlsx|pptx|html|htm> [--no-open] [--no-agent|--agent] Open the file in the browser editor (also: ${BIN} <file>). At a terminal it also starts Claude Code on it in the background; --no-agent only opens the page, --agent prints the agent's output
|
|
25
26
|
${BIN} poll <file> [--ack] [--full] [--timeout <secs>] [--agent] Block until the user hits Send, print the batch as JSON (--full: the complete batch even when you already have the rules)
|
|
26
27
|
${BIN} status <file> [--ticket] Print session state without blocking (--ticket: re-read your delivery's ticket)
|
|
27
28
|
${BIN} apply <file> --ops <ops.json> --lease <ticket> [--dry-run] [--partial] [--summary] [--batch <batch.json>]
|
|
@@ -35,6 +36,10 @@ const HELP = `${PRODUCT.name}
|
|
|
35
36
|
${BIN} setup-checker Create the Python venv for the deeper checks at \`done\` (uses the network once)
|
|
36
37
|
${BIN} setup-rewriter [--remove] Turn on quick fixes on this Mac: quick spelling and grammar fixes on one sentence, with Apple's built-in model (macOS 26 or later, Apple Intelligence on; nothing is downloaded); --remove turns them off
|
|
37
38
|
${BIN} doctor [--json] [--verbose] Check the install (OK / MISSING / NOTE per line, fix command on each failure); exit 1 if a required check fails
|
|
39
|
+
${BIN} update [--check] [--yes] [--force] Bring this copy to the latest release, the way it was installed (checks for a new version only when you run it)
|
|
40
|
+
--check: print this version and the latest one, change nothing. An npm copy gets the two npm commands (--yes runs them).
|
|
41
|
+
Refuses while a session has unsent edits, draft comments or a batch in flight (--force updates anyway)
|
|
42
|
+
${BIN} utf8-copy <file.html|htm> Write a UTF-8 copy of a page saved as windows-1252 or ISO-8859-1 next to it (<name>.utf8.html, only when that name is free); the file itself is not changed
|
|
38
43
|
${BIN} shapes <file.pptx> [--slide <slide id>] List the shapes of a slide (id, name, placeholder, text preview) as JSON; read-only
|
|
39
44
|
${BIN} config background <${BG_PRESETS.join("|")}|default> Choose the background behind the editing UI (this machine)
|
|
40
45
|
${BIN} config background --image <file> [--strength 0-100] [--no-blur] Use your own PNG, JPEG or WebP (max 15 MB, scaled by the page)
|
|
@@ -56,7 +61,9 @@ export const FLAGS = {
|
|
|
56
61
|
"setup-checker": [],
|
|
57
62
|
"setup-rewriter": ["--model", "--remove", "--clean-old"],
|
|
58
63
|
doctor: ["--json", "--verbose"],
|
|
64
|
+
update: ["--check", "--yes", "--force"],
|
|
59
65
|
shapes: ["--slide"],
|
|
66
|
+
"utf8-copy": [],
|
|
60
67
|
config: ["--image", "--strength", "--no-blur"],
|
|
61
68
|
stop: [],
|
|
62
69
|
};
|
|
@@ -89,11 +96,23 @@ function req(options, body) {
|
|
|
89
96
|
res.on("close", () => !res.complete && reject(new Error("server connection closed")));
|
|
90
97
|
});
|
|
91
98
|
r.on("error", reject);
|
|
99
|
+
if (options.timeout) r.on("timeout", () => r.destroy(new Error("request timed out")));
|
|
92
100
|
if (options.signal) options.signal.addEventListener("abort", () => r.destroy(new Error("aborted")), { once: true });
|
|
101
|
+
// `deadline` (ms): the whole request, answer included, or it ends with `deadlineError`
|
|
102
|
+
if (options.deadline) {
|
|
103
|
+
const t = setTimeout(() => r.destroy(new Error(options.deadlineError || "the server did not answer in time")), options.deadline);
|
|
104
|
+
r.on("close", () => clearTimeout(t));
|
|
105
|
+
}
|
|
93
106
|
if (body) r.write(JSON.stringify(body));
|
|
94
107
|
r.end();
|
|
95
108
|
});
|
|
96
109
|
}
|
|
110
|
+
// Opening reads the file in a worker first: up to 60 s waiting for a free reader and 60 s reading (src/offload.js), so the CLI waits longer.
|
|
111
|
+
// ONIONSKIN_CLI_OPEN_DEADLINE_MS shortens it in tests.
|
|
112
|
+
const openRequest = (body) => {
|
|
113
|
+
const ms = Number(process.env[`${PRODUCT.envPrefix}_CLI_OPEN_DEADLINE_MS`]) || 150_000;
|
|
114
|
+
return req({ method: "POST", path: "/cli/open", deadline: ms, deadlineError: `Onionskin did not finish opening ${path.basename(String(body.file))} within ${Math.round(ms / 1000)} seconds. It may be very large or damaged, or this Mac is very busy. Try again, or open a smaller file` }, body);
|
|
115
|
+
};
|
|
97
116
|
const json = (t) => {
|
|
98
117
|
try {
|
|
99
118
|
return JSON.parse(t);
|
|
@@ -102,11 +121,12 @@ const json = (t) => {
|
|
|
102
121
|
}
|
|
103
122
|
};
|
|
104
123
|
|
|
105
|
-
async function healthy() {
|
|
124
|
+
async function healthy({ reportMismatch = false } = {}) {
|
|
106
125
|
try {
|
|
107
126
|
const r = await req({ method: "GET", path: "/health", timeout: 1500 });
|
|
108
127
|
const j = json(r.text);
|
|
109
|
-
|
|
128
|
+
if (r.status === 200 && j?.protocol === SERVER_PROTOCOL) return true;
|
|
129
|
+
return reportMismatch && r.status === 200 ? "incompatible" : false;
|
|
110
130
|
} catch {
|
|
111
131
|
return false;
|
|
112
132
|
}
|
|
@@ -144,7 +164,14 @@ async function removeStaleSocket() {
|
|
|
144
164
|
continue;
|
|
145
165
|
}
|
|
146
166
|
}
|
|
147
|
-
if (!Number.isInteger(pid))
|
|
167
|
+
if (!Number.isInteger(pid) || pid <= 0) {
|
|
168
|
+
if (f.endsWith("start.lock")) {
|
|
169
|
+
try {
|
|
170
|
+
if (Date.now() - fs.statSync(f).mtimeMs <= 2000) return false;
|
|
171
|
+
} catch {}
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
148
175
|
try {
|
|
149
176
|
process.kill(pid, 0);
|
|
150
177
|
return false; // that process is alive: it may be starting up on this socket
|
|
@@ -160,29 +187,180 @@ async function removeStaleSocket() {
|
|
|
160
187
|
}
|
|
161
188
|
}
|
|
162
189
|
|
|
190
|
+
/** True when `pid` exists (EPERM also means it exists, owned by another user). */
|
|
191
|
+
function pidAlive(pid) {
|
|
192
|
+
try {
|
|
193
|
+
process.kill(pid, 0);
|
|
194
|
+
return true;
|
|
195
|
+
} catch (e) {
|
|
196
|
+
return e.code === "EPERM";
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** True when a Unix socket or TCP endpoint accepts a connection within `timeoutMs`. */
|
|
201
|
+
function canConnect(opts, timeoutMs = 400) {
|
|
202
|
+
return new Promise((resolve) => {
|
|
203
|
+
let done = false;
|
|
204
|
+
const finish = (ok) => {
|
|
205
|
+
if (done) return;
|
|
206
|
+
done = true;
|
|
207
|
+
clearTimeout(t);
|
|
208
|
+
try {
|
|
209
|
+
s.destroy();
|
|
210
|
+
} catch {}
|
|
211
|
+
resolve(ok);
|
|
212
|
+
};
|
|
213
|
+
const s = net.createConnection(opts);
|
|
214
|
+
const t = setTimeout(() => finish(false), timeoutMs);
|
|
215
|
+
t.unref?.();
|
|
216
|
+
s.once("connect", () => finish(true));
|
|
217
|
+
s.once("error", () => finish(false));
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** True when `pid`'s command line looks like an Onionskin server process. */
|
|
222
|
+
function looksLikeOnionskinPid(pid) {
|
|
223
|
+
const r = spawnSync("ps", ["-o", "command=", "-p", String(pid)], { encoding: "utf8", timeout: 1000 });
|
|
224
|
+
if (r.status !== 0) return false;
|
|
225
|
+
return /server-entry\.js|onionskin/i.test((r.stdout || "").trim());
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Inspects server.json / start.lock when /health did not answer.
|
|
230
|
+
* Treats a recorded pid as the server ONLY when identity is confirmed (the socket or recorded port accepts
|
|
231
|
+
* connections, or the process command matches Onionskin); otherwise removes the stale record(s).
|
|
232
|
+
*/
|
|
233
|
+
async function inspectRecordedServer() {
|
|
234
|
+
let staleRemoved = false;
|
|
235
|
+
let sockAccepts = null;
|
|
236
|
+
const checkSock = async () => {
|
|
237
|
+
if (sockAccepts === null) sockAccepts = await canConnect({ path: socketPath() });
|
|
238
|
+
return sockAccepts;
|
|
239
|
+
};
|
|
240
|
+
for (const f of [serverRecordPath(), path.join(stateDir(), "start.lock")]) {
|
|
241
|
+
let raw;
|
|
242
|
+
try {
|
|
243
|
+
raw = fs.readFileSync(f, "utf8");
|
|
244
|
+
} catch {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
let pid = null;
|
|
248
|
+
let port = null;
|
|
249
|
+
try {
|
|
250
|
+
const obj = JSON.parse(raw);
|
|
251
|
+
pid = Number.parseInt(obj?.pid ?? raw, 10);
|
|
252
|
+
if (Number.isInteger(obj?.port) && obj.port > 0 && obj.port <= 65535) port = obj.port;
|
|
253
|
+
} catch {
|
|
254
|
+
pid = Number.parseInt(raw, 10);
|
|
255
|
+
}
|
|
256
|
+
if (Number.isInteger(pid) && pid > 0 && pidAlive(pid)) {
|
|
257
|
+
const confirmed =
|
|
258
|
+
(await checkSock()) ||
|
|
259
|
+
(port !== null && (await canConnect({ host: "127.0.0.1", port }))) ||
|
|
260
|
+
looksLikeOnionskinPid(pid);
|
|
261
|
+
if (confirmed) return { pid, staleRemoved };
|
|
262
|
+
// If a socket file is still present alongside a live PID, leave both untouched (a server may be starting up; test/stop.test.js)
|
|
263
|
+
// while still returning pid: null so `stop` does not wait 5s or report an unrelated process as stuck.
|
|
264
|
+
if (fs.existsSync(socketPath())) continue;
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
if (f.endsWith("start.lock") && Date.now() - fs.statSync(f).mtimeMs <= 2000) continue;
|
|
268
|
+
fs.unlinkSync(f);
|
|
269
|
+
staleRemoved = true;
|
|
270
|
+
} catch {}
|
|
271
|
+
}
|
|
272
|
+
return { pid: null, staleRemoved };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** `stop` (and `update`): asks a running server to stop, waits for its process to end, then removes a leftover socket. */
|
|
276
|
+
async function stopServer({ waitMs = 5000 } = {}) {
|
|
277
|
+
let pid = null;
|
|
278
|
+
let staleRemoved = false;
|
|
279
|
+
let shouldStop = await healthy();
|
|
280
|
+
if (shouldStop) {
|
|
281
|
+
pid = json((await req({ method: "GET", path: "/health", timeout: 1500 }).catch(() => ({ text: "" }))).text).pid;
|
|
282
|
+
} else {
|
|
283
|
+
const r = await req({ method: "GET", path: "/health", timeout: 1500 }).catch(() => null);
|
|
284
|
+
const j = r?.status === 200 ? json(r.text) : null;
|
|
285
|
+
if (j && Number.isInteger(j.pid) && j.pid > 0) {
|
|
286
|
+
pid = j.pid;
|
|
287
|
+
shouldStop = true;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (!Number.isInteger(pid) || pid <= 0) {
|
|
291
|
+
const rec = await inspectRecordedServer();
|
|
292
|
+
staleRemoved = rec.staleRemoved;
|
|
293
|
+
if (rec.pid) {
|
|
294
|
+
pid = rec.pid;
|
|
295
|
+
shouldStop = true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (shouldStop) {
|
|
299
|
+
await req({ method: "POST", path: "/cli/stop", timeout: 2000 }, {}).catch(() => {});
|
|
300
|
+
// the server answers first and exits a moment later: "stopped" is printed only once its process is gone (waits at most waitMs)
|
|
301
|
+
const gone = () => !pidAlive(pid);
|
|
302
|
+
const steps = Math.max(1, Math.ceil(waitMs / 50));
|
|
303
|
+
for (let i = 0; i < steps && Number.isInteger(pid) && pid > 0 && !gone(); i++) await new Promise((r) => setTimeout(r, 50));
|
|
304
|
+
if (Number.isInteger(pid) && pid > 0 && !gone()) {
|
|
305
|
+
throw new Error(`server (pid ${pid}) did not stop within ${Math.round(waitMs / 1000)}s`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
await removeStaleSocket(); // a socket file nothing answers on is no use to anyone, and `doctor` would keep reporting it
|
|
309
|
+
return { staleRemoved, stopped: shouldStop };
|
|
310
|
+
}
|
|
311
|
+
|
|
163
312
|
/** Starts the detached server and waits (event-based) for its "ready" line. */
|
|
164
313
|
async function ensureServer() {
|
|
165
314
|
ensureStateDir();
|
|
166
315
|
if (await healthy()) return;
|
|
167
|
-
const child = spawn(process.execPath, [path.join(here, "server-entry.js")], { detached: true, stdio: ["ignore", "pipe", "
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
316
|
+
const child = spawn(process.execPath, [path.join(here, "server-entry.js")], { detached: true, stdio: ["ignore", "pipe", "pipe"], env: process.env });
|
|
317
|
+
const errChunks = [];
|
|
318
|
+
let errLen = 0;
|
|
319
|
+
child.stderr.on("data", (d) => {
|
|
320
|
+
if (errLen >= 4096) return;
|
|
321
|
+
const slice = d.length > 4096 - errLen ? d.subarray(0, 4096 - errLen) : d;
|
|
322
|
+
errChunks.push(slice);
|
|
323
|
+
errLen += slice.length;
|
|
173
324
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
325
|
+
const detail = () => {
|
|
326
|
+
const s = Buffer.concat(errChunks).toString("utf8").trim();
|
|
327
|
+
return s ? `: ${s}` : "";
|
|
328
|
+
};
|
|
329
|
+
try {
|
|
330
|
+
await new Promise((resolve, reject) => {
|
|
331
|
+
const to = setTimeout(() => reject(new Error(`could not start the ${PRODUCT.name} server${detail()}`)), 15000);
|
|
332
|
+
child.stdout.once("data", () => (clearTimeout(to), resolve()));
|
|
333
|
+
child.once("exit", () => (clearTimeout(to), resolve())); // lost a race to another server: it is healthy now
|
|
334
|
+
child.once("error", (e) => (clearTimeout(to), reject(new Error(`${e.message}${detail()}`))));
|
|
335
|
+
});
|
|
336
|
+
if (!(await healthy())) {
|
|
337
|
+
if (child.exitCode !== null && !child.stderr.readableEnded) {
|
|
338
|
+
await Promise.race([new Promise((r) => child.stderr.once("end", r)), new Promise((r) => setTimeout(r, 50))]);
|
|
339
|
+
}
|
|
340
|
+
throw new Error(`the ${PRODUCT.name} server did not come up${detail()}`);
|
|
341
|
+
}
|
|
342
|
+
} finally {
|
|
343
|
+
child.stdout.destroy();
|
|
344
|
+
child.stderr.destroy();
|
|
345
|
+
child.unref();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function openBrowserArgs(url, platform = process.platform) {
|
|
350
|
+
if (platform === "darwin") return ["open", [url]];
|
|
351
|
+
if (platform === "win32") return ["cmd", ["/c", "start", "", url.replace(/&/g, "^&")]];
|
|
352
|
+
return ["xdg-open", [url]];
|
|
177
353
|
}
|
|
178
354
|
|
|
179
355
|
function openBrowser(url) {
|
|
180
|
-
const
|
|
181
|
-
const c = spawn(
|
|
356
|
+
const [bin, args] = openBrowserArgs(url);
|
|
357
|
+
const c = spawn(bin, args, { detached: true, stdio: "ignore" });
|
|
182
358
|
c.on("error", () => {});
|
|
183
359
|
c.unref();
|
|
184
360
|
}
|
|
185
361
|
|
|
362
|
+
export { req, stopServer, ensureServer };
|
|
363
|
+
|
|
186
364
|
/** `config background <preset|default>`, `config background --image <file> [--strength N] [--no-blur]`, `config get background` */
|
|
187
365
|
export function parseConfigArgs(args) {
|
|
188
366
|
const a = { action: "", key: "", value: "", image: "", strength: null, blur: null };
|
|
@@ -239,7 +417,7 @@ export function configCommand(args) {
|
|
|
239
417
|
return `Background set to your image. It is copied as it is; the page scales it (and the copy keeps its metadata on disk). Reload the page to see it.\n`;
|
|
240
418
|
}
|
|
241
419
|
writeBackground({ kind: "preset", name: a.value, ...keep });
|
|
242
|
-
return `Background set to ${a.value}. Reload the page to see it.\n`;
|
|
420
|
+
return `Background set to ${a.reset ? `the default (${a.value})` : a.value}. Reload the page to see it.\n`;
|
|
243
421
|
}
|
|
244
422
|
|
|
245
423
|
const out = (text) => new Promise((r) => process.stdout.write(text, r));
|
|
@@ -257,6 +435,8 @@ const realOrSame = (file) => {
|
|
|
257
435
|
* a = {ack, full, compact, timeout}; `quiet` skips the human line on STDERR.
|
|
258
436
|
*/
|
|
259
437
|
async function pollFor(file, a, { quiet = false } = {}) {
|
|
438
|
+
// an agent Onionskin started is bound to its one file: refused before anything about another file is asked
|
|
439
|
+
if (!agentMayUse(process.env[`${PRODUCT.envPrefix}_HOLDER`], file)) throw new Error(NOT_YOUR_FILE);
|
|
260
440
|
// an agent Onionskin started (in the background or in Terminal) never starts a server, and never reconnects to one: a server that stopped means its review ended
|
|
261
441
|
const bg = isAgentHolder(process.env[`${PRODUCT.envPrefix}_HOLDER`]); // the background agent, or the same agent opened in Terminal
|
|
262
442
|
const serverGone = { status: "closed", reason: "the server stopped" };
|
|
@@ -286,14 +466,14 @@ async function pollFor(file, a, { quiet = false } = {}) {
|
|
|
286
466
|
} catch (err) {
|
|
287
467
|
clearTimeout(timer);
|
|
288
468
|
if (ac.signal.aborted) continue; // deadline reached: loop returns the timeout
|
|
289
|
-
if (/not found|no such session/i.test(err.message)) throw err;
|
|
469
|
+
if (/not found|no such session/i.test(err.message) || err.message === NOT_YOUR_FILE) throw err;
|
|
290
470
|
if (bg) return serverGone;
|
|
291
471
|
if (++failures > 5) throw err;
|
|
292
472
|
process.stderr.write(`Lost the connection (${err.message}); reconnecting.\n`);
|
|
293
473
|
await new Promise((r) => setTimeout(r, 1000 * failures));
|
|
294
474
|
await ensureServer();
|
|
295
475
|
// the session lives in the server's state dir; re-open it so the poll can register
|
|
296
|
-
await
|
|
476
|
+
await openRequest({ file });
|
|
297
477
|
ack = false;
|
|
298
478
|
}
|
|
299
479
|
}
|
|
@@ -301,13 +481,15 @@ async function pollFor(file, a, { quiet = false } = {}) {
|
|
|
301
481
|
|
|
302
482
|
/** `done`: reports `results` for the delivery named by `lease` (a ticket string) and returns the server's answer. Shared by `done` and `reply`. */
|
|
303
483
|
async function finishBatch(file, results, lease) {
|
|
484
|
+
const envHolder = process.env[`${PRODUCT.envPrefix}_HOLDER`];
|
|
485
|
+
if (!agentMayUse(envHolder, file)) throw new Error(NOT_YOUR_FILE);
|
|
304
486
|
if (!(await healthy())) throw new Error(`the ${PRODUCT.name} server is not running`);
|
|
305
487
|
const realFile = realOrSame(file);
|
|
306
488
|
// the only credential is the ticket printed in the batch's `done` command; a delivery without it is refused, with no per-file fallback
|
|
307
489
|
const ticket = parseTicket(lease);
|
|
308
|
-
const st = json((await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(realFile)}` })).text);
|
|
490
|
+
const st = json((await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(realFile)}${envHolder ? `&holder=${encodeURIComponent(envHolder)}` : ""}` })).text);
|
|
309
491
|
if (!ticket && st.delivered_batch) throw new Error(`a batch is delivered for this file: pass --lease <ticket> (it is printed in the batch's done command; run \`${BIN} status <file> --ticket\` if you lost it, only the agent that polled gets it back)`);
|
|
310
|
-
const claim = ticket ? { holder:
|
|
492
|
+
const claim = ticket ? { holder: envHolder || loadTicketRecord(ticket.batch_id)?.holder, lease_token: ticket.token, batch_id: ticket.batch_id } : envHolder ? { holder: envHolder } : {};
|
|
311
493
|
const r = await req({ method: "POST", path: "/cli/done" }, { file, results, ...claim });
|
|
312
494
|
const j = json(r.text);
|
|
313
495
|
if (r.status !== 200) throw new Error(j.error || "done failed");
|
|
@@ -339,6 +521,7 @@ export function parseArgs(argv) {
|
|
|
339
521
|
}
|
|
340
522
|
else if (x === "--json" || x === "--verbose") a[x.slice(2)] = true; // doctor
|
|
341
523
|
else if (x === "--remove") a.remove = true; // setup-rewriter
|
|
524
|
+
else if (x === "--check" || x === "--yes" || x === "--force") a[x.slice(2)] = true; // update
|
|
342
525
|
else if (x === "--clean-old") a.cleanOld = true; // setup-rewriter, from install.sh: only removes what the older Granite download left
|
|
343
526
|
else if (x === "--model" || x.startsWith("--model=")) a.model = x.includes("=") ? x.split("=")[1] : rest[++i]; // setup-rewriter
|
|
344
527
|
else if (x === "--timeout" || x.startsWith("--timeout=")) {
|
|
@@ -355,7 +538,7 @@ export function parseArgs(argv) {
|
|
|
355
538
|
else if (!a.file) a.file = x;
|
|
356
539
|
else throw new Error(`unexpected argument ${x}`);
|
|
357
540
|
}
|
|
358
|
-
if (["open", "poll", "status", "done", "shapes"].includes(cmd) && !a.file) throw new Error(`Usage: ${BIN} ${cmd} <file>`);
|
|
541
|
+
if (["open", "poll", "status", "done", "shapes", "utf8-copy"].includes(cmd) && !a.file) throw new Error(`Usage: ${BIN} ${cmd} <file>`);
|
|
359
542
|
if (cmd === "done" && !a.results) throw new Error(`Usage: ${BIN} done <file> --results <results.json>`);
|
|
360
543
|
return a;
|
|
361
544
|
}
|
|
@@ -374,7 +557,7 @@ async function main() {
|
|
|
374
557
|
process.exit(r.exit);
|
|
375
558
|
}
|
|
376
559
|
if (argv[0] === "setup-checker") {
|
|
377
|
-
const py = setupChecker();
|
|
560
|
+
const py = await setupChecker();
|
|
378
561
|
return out(`checker ready: ${py}\n`);
|
|
379
562
|
}
|
|
380
563
|
if (argv[0] === "setup-rewriter") {
|
|
@@ -399,6 +582,30 @@ async function main() {
|
|
|
399
582
|
await out(a.json ? JSON.stringify(r, null, 2) + "\n" : formatDoctor(r));
|
|
400
583
|
process.exit(r.ok ? 0 : 1);
|
|
401
584
|
}
|
|
585
|
+
if (argv[0] === "update") {
|
|
586
|
+
const a = parseArgs(argv);
|
|
587
|
+
if (a.file) throw new Error(`unexpected argument ${a.file}`);
|
|
588
|
+
// everything update needs is loaded before the installer runs: install.sh deletes the app folder this process runs from
|
|
589
|
+
const { updateCommand, runGetShDefault } = await import("./update.js");
|
|
590
|
+
const appRoot = path.resolve(here, "..");
|
|
591
|
+
const home = os.homedir();
|
|
592
|
+
const versionOf = (cmd, args) => {
|
|
593
|
+
const r = spawnSync(cmd, args, { encoding: "utf8", timeout: 20000, stdio: ["ignore", "pipe", "ignore"] });
|
|
594
|
+
return r.status === 0 ? r.stdout.trim().split("\n")[0] || null : null;
|
|
595
|
+
};
|
|
596
|
+
const code = await updateCommand(argv.slice(1), {
|
|
597
|
+
version: JSON.parse(fs.readFileSync(path.join(appRoot, "package.json"), "utf8")).version,
|
|
598
|
+
appRoot,
|
|
599
|
+
home,
|
|
600
|
+
env: process.env,
|
|
601
|
+
print: (line) => process.stdout.write(line + "\n"),
|
|
602
|
+
server: { healthy, openWork: async () => { const r = await req({ method: "GET", path: "/cli/open-work", timeout: 3000 }); return { status: r.status, body: json(r.text) }; } },
|
|
603
|
+
stop: stopServer,
|
|
604
|
+
runGetSh: (base, opts) => runGetShDefault(base, process.env, opts),
|
|
605
|
+
launcherVersion: (route, root) => (route === "npm" ? versionOf(process.execPath, [path.join(root, "src", "cli.js"), "--version"]) : versionOf(path.join(home, ".local", "bin", BIN), ["--version"])),
|
|
606
|
+
});
|
|
607
|
+
process.exit(code);
|
|
608
|
+
}
|
|
402
609
|
if (argv[0] === "reply") {
|
|
403
610
|
// JSON on STDOUT only (also for failures), so an agent parses one stream; the exit code says whether the batch was finished
|
|
404
611
|
let r;
|
|
@@ -434,6 +641,12 @@ async function main() {
|
|
|
434
641
|
if (path.extname(real).toLowerCase() !== ".pptx") throw new Error("shapes reads a .pptx file");
|
|
435
642
|
return out(JSON.stringify({ file: path.basename(real), slides: listShapes(parseDeck(readDoc(real)), a.slide ?? null) }, null, 2) + "\n");
|
|
436
643
|
}
|
|
644
|
+
if (a.cmd === "utf8-copy") {
|
|
645
|
+
const { writeUtf8Copy } = await import("./html-utf8.js");
|
|
646
|
+
const r = writeUtf8Copy(file);
|
|
647
|
+
if (!r.ok) throw new Error(r.reason);
|
|
648
|
+
return out(`Wrote a UTF-8 copy of ${a.file}:\n ${r.path}\nThe file itself is not changed. Open the copy with:\n ${BIN} open ${shellQuote(r.path)}\n`);
|
|
649
|
+
}
|
|
437
650
|
if (a.cmd === "poll" && aud.person) {
|
|
438
651
|
await out(personPollText(file));
|
|
439
652
|
process.exit(2);
|
|
@@ -441,8 +654,13 @@ async function main() {
|
|
|
441
654
|
if (a.cmd === "open") {
|
|
442
655
|
await ensureServer();
|
|
443
656
|
// a person at a terminal (not --no-agent): the server starts its background agent on the file
|
|
444
|
-
const r = await
|
|
657
|
+
const r = await openRequest(aud.offer ? { file, agent: "background" } : { file });
|
|
445
658
|
const j = json(r.text);
|
|
659
|
+
if (r.status !== 200 && j.code === "not_utf8" && /\.html?$/i.test(file)) {
|
|
660
|
+
// a page saved as windows-1252 or ISO-8859-1: the fixed sentence (R18) and the command that makes the copy it offers
|
|
661
|
+
const { copyable, openRefusal } = await import("./html-utf8.js");
|
|
662
|
+
if (copyable(fs.readFileSync(file))) throw new Error(openRefusal(BIN, file, shellQuote));
|
|
663
|
+
}
|
|
446
664
|
if (r.status !== 200) throw new Error(j.error || "could not open that file");
|
|
447
665
|
const realPath = fs.realpathSync(file);
|
|
448
666
|
if (!a.noOpen) openBrowser(j.url);
|
|
@@ -452,8 +670,10 @@ async function main() {
|
|
|
452
670
|
} else if (a.cmd === "poll") {
|
|
453
671
|
return out(JSON.stringify(await pollFor(file, a), null, 2) + "\n");
|
|
454
672
|
} else if (a.cmd === "status") {
|
|
673
|
+
const envHolder = process.env[`${PRODUCT.envPrefix}_HOLDER`];
|
|
674
|
+
if (!agentMayUse(envHolder, file)) throw new Error(NOT_YOUR_FILE); // the same words whether that file is open or not
|
|
455
675
|
if (!(await healthy())) return out(JSON.stringify({ state: "no-server" }, null, 2) + "\n");
|
|
456
|
-
const r = await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(file)}` });
|
|
676
|
+
const r = await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(file)}${envHolder ? `&holder=${encodeURIComponent(envHolder)}` : ""}` });
|
|
457
677
|
if (r.status !== 200) throw new Error(json(r.text).error || "no such session");
|
|
458
678
|
const st = json(r.text);
|
|
459
679
|
if (a.ticket) {
|
|
@@ -477,22 +697,8 @@ async function main() {
|
|
|
477
697
|
if (a.summary) return out(JSON.stringify({ ok: !!j.ok, flags: (j.flags || []).map((f) => ({ code: f.code, severity: f.severity })), ...(j.checks?.notice ? { checks_notice: j.checks.notice } : {}) }) + "\n");
|
|
478
698
|
return out(JSON.stringify(j, null, 2) + "\n");
|
|
479
699
|
} else if (a.cmd === "stop") {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
await req({ method: "POST", path: "/cli/stop" }, {});
|
|
483
|
-
// the server answers first and exits a moment later: "stopped" is printed only once its process is gone (waits at most 5 s)
|
|
484
|
-
const gone = () => {
|
|
485
|
-
try {
|
|
486
|
-
process.kill(pid, 0);
|
|
487
|
-
return false;
|
|
488
|
-
} catch {
|
|
489
|
-
return true;
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
for (let i = 0; i < 100 && Number.isInteger(pid) && !gone(); i++) await new Promise((r) => setTimeout(r, 50));
|
|
493
|
-
}
|
|
494
|
-
await removeStaleSocket(); // a socket file nothing answers on is no use to anyone, and `doctor` would keep reporting it
|
|
495
|
-
return out("stopped\n");
|
|
700
|
+
const res = await stopServer();
|
|
701
|
+
return out(res?.staleRemoved ? "no running server (stale record removed)\n" : res?.stopped ? "stopped\n" : "no server was running\n");
|
|
496
702
|
} else throw new Error(`unknown command ${a.cmd}\n${HELP}`);
|
|
497
703
|
}
|
|
498
704
|
|
package/src/constants.js
CHANGED
|
@@ -7,13 +7,46 @@ export const PRODUCT = {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
export const SERVER_PROTOCOL =
|
|
10
|
+
export const SERVER_PROTOCOL = 7; // 2: session.stage, reply, compact polls, /cli/batch-file, stop ends waiting polls; 3: question cards (/api/answer, results "asked"); 4: quick, review and suggestion routes, local history kind, merge in every format (/api/merge edits, /cli/agent-write ops), /api/export-tracked; 5: "Build my voice guide" (/api/voice/build, /api/voice/draft/discard, done takes voice_draft); 6: an agent Onionskin started is bound to its file on every /cli/* route, /cli/batch and /cli/batch-file check the lease first (a CLI never trusts an older server to do that); 7: HTML files (.html, .htm open; /api/html-view; Send takes htmlEdits)
|
|
11
11
|
export const FORMATS = {
|
|
12
12
|
docx: { main: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", part: "/word/document.xml" },
|
|
13
13
|
xlsx: { main: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", part: "/xl/workbook.xml" },
|
|
14
14
|
pptx: { main: "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", part: "/ppt/presentation.xml" },
|
|
15
|
+
// not a zip package: src/package.js validatePackage checks its encoding (src/html.js decodeHtml); the open worker reads it whole
|
|
16
|
+
html: { main: null, part: null, zip: false },
|
|
15
17
|
};
|
|
16
18
|
export const REFUSED_EXTENSIONS = new Set([".docm", ".xlsm", ".pptm"]);
|
|
19
|
+
/**
|
|
20
|
+
* Kinds of web file Onionskin refuses with a fixed sentence (PLAN-html D8, R15), by extension. Checked before the format: these are
|
|
21
|
+
* close to .html but not pages Onionskin can edit by splices.
|
|
22
|
+
*/
|
|
23
|
+
export const REFUSED_PAGES = {
|
|
24
|
+
".mhtml": "Onionskin cannot open this kind of saved page; in Chrome choose Save As, Webpage, Complete.",
|
|
25
|
+
".mht": "Onionskin cannot open this kind of saved page; in Chrome choose Save As, Webpage, Complete.",
|
|
26
|
+
".webarchive": "Onionskin cannot open this kind of saved page; in Chrome choose Save As, Webpage, Complete.",
|
|
27
|
+
".xhtml": "Onionskin cannot open .xhtml pages; save the page as .html and open that.",
|
|
28
|
+
".svg": "Onionskin cannot open .svg pictures; it opens .html and .htm pages.",
|
|
29
|
+
};
|
|
30
|
+
/** The format of a file by its extension (".htm" is "html"), or null when Onionskin does not open that kind of file. */
|
|
31
|
+
export function formatOfExt(ext) {
|
|
32
|
+
const e = String(ext).toLowerCase();
|
|
33
|
+
if (e === ".htm" || e === ".html") return "html";
|
|
34
|
+
const f = e.slice(1);
|
|
35
|
+
return Object.hasOwn(FORMATS, f) ? f : null;
|
|
36
|
+
}
|
|
37
|
+
/** The sentence for a file Onionskin does not open: a refused page kind, or the list of what it opens. */
|
|
38
|
+
export const ONLY_FORMATS = "Only .docx, .xlsx, .pptx, .html and .htm files are supported";
|
|
39
|
+
/**
|
|
40
|
+
* The end of every per-format dispatch chain: a format the chain does not know throws here, so it never runs another format's code (a
|
|
41
|
+
* web page read as a deck). `where` names the chain. The message holds at most 20 characters of the value.
|
|
42
|
+
*/
|
|
43
|
+
export function unknownFormat(format, where) {
|
|
44
|
+
throw new Error(`unknown format ${JSON.stringify(String(format).slice(0, 20))} in ${where}`);
|
|
45
|
+
}
|
|
46
|
+
/** `table[format]` of a per-format table, or unknownFormat (own keys only: "constructor" is not a format). */
|
|
47
|
+
export const byFormat = (table, format, where) => (Object.hasOwn(table, format) ? table[format] : unknownFormat(format, where));
|
|
48
|
+
/** The kind of the entry that ends a list of the page's edits cut short (src/session.js editKinds): more were made than it holds. */
|
|
49
|
+
export const EDITS_OVERFLOW = "edits.overflow";
|
|
17
50
|
export const env = (suffix, fallback) => process.env[`${PRODUCT.envPrefix}_${suffix}`] ?? fallback;
|
|
18
51
|
|
|
19
52
|
/** The largest document that is opened, sent, written or checked. Enforced by size BEFORE the bytes are read. */
|
package/src/diff.js
CHANGED
|
@@ -17,7 +17,9 @@ const WORD_LIMIT = 2000;
|
|
|
17
17
|
export const HARD_CAP_TOKENS = 60_000;
|
|
18
18
|
const MAX_D = 2000;
|
|
19
19
|
const CJK = "\\p{Script=Han}|\\p{Script=Hiragana}|\\p{Script=Katakana}";
|
|
20
|
-
|
|
20
|
+
// a word is at most 4,096 characters a token (a longer one is several): an unbounded repeat over a run of 7.9 million combining marks
|
|
21
|
+
// overflowed the regex stack ("Maximum call stack size exceeded")
|
|
22
|
+
const TOKEN = new RegExp(`(?:${CJK})|(?:(?!${CJK})[\\p{L}\\p{N}\\p{M}]){1,4096}(?:['’](?:(?!${CJK})[\\p{L}\\p{N}\\p{M}]){1,4096})*|\\s+|[^\\s]`, "gu");
|
|
21
23
|
const IS_WORD = new RegExp(`^(?:${CJK}|[\\p{L}\\p{N}\\p{M}])`, "u");
|
|
22
24
|
const IS_CJK = new RegExp(`^(?:${CJK})$`, "u");
|
|
23
25
|
const SENTENCE = /[^.!?。!?\n]*(?:[.!?。!?]+|\n|$)\s*/gu;
|
package/src/doctor.js
CHANGED
|
@@ -90,6 +90,15 @@ const run = (cmd, args, env) => {
|
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
+
/** Parse the target cli.js path from an installer-written launcher (single- or double-quoted). */
|
|
94
|
+
export function parseLauncherTarget(text) {
|
|
95
|
+
const sq = text.match(/exec node '((?:[^']|'\\'')+)'/);
|
|
96
|
+
if (sq) return sq[1].replace(/'\\''/g, "'");
|
|
97
|
+
const dq = text.match(/exec node "([^"]+)"/);
|
|
98
|
+
if (dq) return dq[1];
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
93
102
|
/**
|
|
94
103
|
* @param {object} o home, appRoot, env, verbose: where to look. probes: injectable `defaultBrowser()` and `freeBytes(dir)` for tests.
|
|
95
104
|
* @returns {Promise<{schema:string, ok:boolean, version:string|null, checks:Array<{status:string,name:string,detail:string,fix?:string,required:boolean}>}>}
|
|
@@ -149,8 +158,8 @@ export async function runDoctor(o = {}) {
|
|
|
149
158
|
const found = which(BIN);
|
|
150
159
|
const launcher = found || path.join(home, ".local", "bin", BIN);
|
|
151
160
|
if (fs.existsSync(launcher)) {
|
|
152
|
-
const target = (
|
|
153
|
-
// the installer writes `exec node
|
|
161
|
+
const target = parseLauncherTarget(fs.readFileSync(launcher, "utf8"));
|
|
162
|
+
// the installer writes `exec node '<app>/src/cli.js'` (or double-quoted in older versions); any other launcher (a symlink, npm link) is judged by whether it runs
|
|
154
163
|
add(!target || fs.existsSync(target) ? "OK" : "MISSING", "launcher", `${launcher}${target ? ` -> ${target}` : " (custom launcher)"}`, { required: true, fix: REINSTALL_FIX });
|
|
155
164
|
const v = run(launcher, ["--version"], env);
|
|
156
165
|
add(v ? "OK" : "MISSING", `${BIN} --version`, v || "the launcher did not run", { required: true, fix: REINSTALL_FIX });
|
|
@@ -166,7 +175,7 @@ export async function runDoctor(o = {}) {
|
|
|
166
175
|
};
|
|
167
176
|
const runs = (p) => {
|
|
168
177
|
try {
|
|
169
|
-
const t = (fs.readFileSync(p, "utf8").slice(0, 400)
|
|
178
|
+
const t = parseLauncherTarget(fs.readFileSync(p, "utf8").slice(0, 400)); // the installer's 2-line launcher
|
|
170
179
|
return real(t || p);
|
|
171
180
|
} catch {
|
|
172
181
|
return real(p);
|