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.
Files changed (117) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/INSTALL.md +17 -0
  3. package/LICENSE +21 -0
  4. package/NOTICE +46 -0
  5. package/README.md +88 -2
  6. package/docs/contracts/batch.schema.json +839 -0
  7. package/docs/contracts/history.schema.json +126 -0
  8. package/docs/contracts/ops.schema.json +541 -0
  9. package/package.json +54 -4
  10. package/scripts/checker/check.py +411 -0
  11. package/scripts/checker/requirements.txt +5 -0
  12. package/scripts/install-check.mjs +9 -0
  13. package/scripts/requirements.txt +5 -0
  14. package/scripts/rewriter/apple/main.swift +135 -0
  15. package/scripts/rewriter/apple/onionskin-fm +0 -0
  16. package/scripts/rewriter/apple/onionskin-fm.json +6 -0
  17. package/skills/onionskin/SKILL.md +117 -0
  18. package/skills/onionskin/reference/batch.md +94 -0
  19. package/skills/onionskin/reference/formats.md +15 -0
  20. package/skills/onionskin/reference/ops.md +253 -0
  21. package/skills/onionskin/reference/troubleshooting.md +73 -0
  22. package/skills/onionskin/reference/voice-guide.md +86 -0
  23. package/skills/onionskin/voice.example.md +51 -0
  24. package/src/agent-detect.js +149 -0
  25. package/src/agent-events.js +159 -0
  26. package/src/agent-prompt.js +27 -0
  27. package/src/agent-runner.js +593 -0
  28. package/src/analyze.js +17 -0
  29. package/src/anchors.js +190 -0
  30. package/src/apply.js +387 -0
  31. package/src/auth.js +130 -0
  32. package/src/batch.js +322 -0
  33. package/src/check-worker.js +8 -0
  34. package/src/checker.js +293 -0
  35. package/src/cli.js +516 -0
  36. package/src/constants.js +20 -0
  37. package/src/diff.js +267 -0
  38. package/src/doctor.js +247 -0
  39. package/src/docx.js +438 -0
  40. package/src/fmt.js +30 -0
  41. package/src/fsutil.js +348 -0
  42. package/src/history-worker.js +16 -0
  43. package/src/history.js +576 -0
  44. package/src/lease.js +79 -0
  45. package/src/local.js +242 -0
  46. package/src/merge-pptx.js +198 -0
  47. package/src/merge-xlsx.js +453 -0
  48. package/src/merge.js +410 -0
  49. package/src/offload.js +34 -0
  50. package/src/ops/common.js +48 -0
  51. package/src/ops/docx-table.js +323 -0
  52. package/src/ops/docx.js +503 -0
  53. package/src/ops/engine.js +41 -0
  54. package/src/ops/pptx.js +336 -0
  55. package/src/ops/run.js +115 -0
  56. package/src/ops/runs.js +127 -0
  57. package/src/ops/schema.js +109 -0
  58. package/src/ops/xlsx.js +277 -0
  59. package/src/package.js +189 -0
  60. package/src/paths.js +64 -0
  61. package/src/person.js +66 -0
  62. package/src/pptx.js +194 -0
  63. package/src/question.js +47 -0
  64. package/src/quick-plan.js +107 -0
  65. package/src/quick.js +164 -0
  66. package/src/redline-check.js +251 -0
  67. package/src/redline-worker.js +29 -0
  68. package/src/redline.js +438 -0
  69. package/src/reply.js +410 -0
  70. package/src/review.js +274 -0
  71. package/src/rewrite-check.js +1014 -0
  72. package/src/rewriter-cleanup.js +105 -0
  73. package/src/rewriter-setup.js +104 -0
  74. package/src/rewriter.js +396 -0
  75. package/src/sanitize.js +42 -0
  76. package/src/sentence-span.js +25 -0
  77. package/src/server-entry.js +13 -0
  78. package/src/server.js +576 -0
  79. package/src/session.js +1910 -0
  80. package/src/settings.js +161 -0
  81. package/src/setup.js +160 -0
  82. package/src/stamp-words.js +10 -0
  83. package/src/stamps.js +59 -0
  84. package/src/style.js +110 -0
  85. package/src/suggest-reply.js +84 -0
  86. package/src/suggest.js +261 -0
  87. package/src/text.js +31 -0
  88. package/src/tracked-words.js +43 -0
  89. package/src/voice-guide.js +182 -0
  90. package/src/voice-learn.js +314 -0
  91. package/src/voice-sample-worker.js +95 -0
  92. package/src/voice-samples.js +329 -0
  93. package/src/voice-store.js +305 -0
  94. package/src/voice-worker.js +28 -0
  95. package/src/voice.js +297 -0
  96. package/src/xlsx.js +454 -0
  97. package/src/xml-tree.js +118 -0
  98. package/src/xml.js +127 -0
  99. package/src/zip-write.js +74 -0
  100. package/src/zip.js +88 -0
  101. package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
  102. package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
  103. package/vendor/engine/collab-BVynXMqc.js +2591 -0
  104. package/vendor/engine/collab.js +2 -0
  105. package/vendor/engine/core.js +4 -0
  106. package/vendor/engine/default-worker-DogNT09A.js +35625 -0
  107. package/vendor/engine/embed.js +107 -0
  108. package/vendor/engine/index.js +5 -0
  109. package/vendor/engine/office-engine.embed.js +184 -0
  110. package/vendor/engine/office-engine.worker.js +36030 -0
  111. package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
  112. package/vendor/engine-node/LICENSE +21 -0
  113. package/vendor/engine-node/engine-node.mjs +46618 -0
  114. package/vendor/engine.lock.json +26 -0
  115. package/web/dist/assets/index-CFISATkn.js +197 -0
  116. package/web/dist/assets/index-spXE4oTz.css +1 -0
  117. package/web/dist/index.html +14 -0
@@ -0,0 +1,25 @@
1
+ // Quick fixes: which characters of a paragraph count as "the sentence" a selection means. No imports: the server (src/suggest.js) and the
2
+ // page (web/src/stamps.js) both read it, so the two cannot disagree. The server runs it on its own reading of the paragraph, never on text the page sends.
3
+ const SPACE = /\s/u;
4
+ const END_MARK = /[.!?]["')\]”’]*$/u; // a sentence end with the quotes and brackets that close it
5
+ const CLOSE_AFTER_MARK = /^["')\]”’]*(?=\s|$)/u; // closing quotes and brackets right after an end mark
6
+ const MARK_AND_CLOSE = /^[.!?]+["')\]”’]*(?=\s|$)/u; // the end mark and what closes it, right after words that stop short of it
7
+
8
+ /**
9
+ * The span [start, end) of `text` that a selection of [start, end) means: the selection without its leading and trailing spaces, and when it
10
+ * covers a whole sentence except the closing punctuation, closing quote or bracket, with those added. A selection that starts or stops
11
+ * anywhere else comes back trimmed and not extended; the caller's ordinary one-sentence rules then decide. Null when the offsets are not a
12
+ * span of `text`.
13
+ */
14
+ export function sentenceSpan(text, start, end) {
15
+ if (typeof text !== "string" || !Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end > text.length || start >= end) return null;
16
+ while (start < end && SPACE.test(text[start])) start++;
17
+ while (end > start && SPACE.test(text[end - 1])) end--;
18
+ if (start >= end) return null;
19
+ if (end === text.length) return { start, end }; // the paragraph ends here: nothing to add
20
+ const sentenceStart = start === 0 || (SPACE.test(text[start - 1]) && /(?:^|[.!?]["')\]”’]*)\s+$/u.test(text.slice(0, start)));
21
+ if (!sentenceStart) return { start, end };
22
+ const rest = text.slice(end);
23
+ const add = END_MARK.test(text.slice(start, end)) ? CLOSE_AFTER_MARK.exec(rest) : MARK_AND_CLOSE.exec(rest);
24
+ return { start, end: end + (add ? add[0].length : 0) };
25
+ }
@@ -0,0 +1,13 @@
1
+ import { startServer } from "./server.js";
2
+
3
+ try {
4
+ await startServer();
5
+ process.stdout.write("ready\n"); // the CLI waits for this line instead of polling
6
+ } catch (err) {
7
+ if (err?.code === "EALREADY") process.exit(0);
8
+ console.error(err?.message || err);
9
+ process.exit(1);
10
+ }
11
+ process.on("SIGTERM", () => process.exit(0));
12
+ process.on("SIGINT", () => process.exit(0));
13
+ process.stdout.on("error", () => {});
package/src/server.js ADDED
@@ -0,0 +1,576 @@
1
+ import fs from "node:fs";
2
+ import http from "node:http";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { BrowserAuth, CSP } from "./auth.js";
6
+ import { PRODUCT, SERVER_PROTOCOL, env } from "./constants.js";
7
+ import { sha256 } from "./fsutil.js";
8
+ import { ensureStateDir, serverRecordPath, socketPath, stateDir, writeStateFile } from "./paths.js";
9
+ import { setupCheckerStatus, startLateSoffice, startSetupChecker } from "./checker.js";
10
+ import { analyzeOffThread } from "./offload.js";
11
+ import { warm, warmNewest } from "./history.js";
12
+ import { stopRewriter } from "./rewriter.js";
13
+ import { HttpError, Sessions } from "./session.js";
14
+ import { quickPreview } from "./quick-plan.js";
15
+ import { cliStatus, leaseOf, localRunner } from "./local.js"; // leaseOf: the delivery an agent acts on (batch, holder, token), all three required
16
+ import { readStamps, writeStamps } from "./stamps.js";
17
+ import { setupRewriterStatus, startSetupRewriter, suggest, suggestStatus, useSuggestion, warmSuggest } from "./suggest.js";
18
+ import { readStyle, writeStyle } from "./style.js";
19
+ import { GUIDE_BODY_MAX, VOICE_BUILD_MAX_BODY, VOICE_POSTS, buildVoiceRequest, learnFromSend, stopVoice, voicePost, voiceState } from "./voice-learn.js";
20
+ import { agentRunner } from "./agent-runner.js";
21
+ import { isBgHolder } from "./agent-detect.js";
22
+ import { BG_PRESETS, MAX_IMAGE_BYTES, SettingsError, readCustomImage, readSettings, removeCustomImage, saveCustomImage, writeBackground } from "./settings.js";
23
+
24
+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
25
+ const WEB_DIST = env("WEB_DIST", path.join(root, "web", "dist"));
26
+ const MAX_BODY = 300 * 1024 * 1024; // base64 of a 200 MB document; ceiling: whole body buffered in memory. Upgrade path: binary upload route.
27
+ const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png", ".woff2": "font/woff2", ".ico": "image/x-icon", ".map": "application/json" };
28
+
29
+ function secure(res) {
30
+ res.setHeader("Content-Security-Policy", CSP);
31
+ res.setHeader("Referrer-Policy", "no-referrer");
32
+ res.setHeader("X-Content-Type-Options", "nosniff");
33
+ res.setHeader("Cache-Control", "no-store");
34
+ }
35
+ function sendJson(res, status, body) {
36
+ secure(res);
37
+ const raw = JSON.stringify(body);
38
+ res.writeHead(status, { "Content-Type": "application/json; charset=utf-8", "Content-Length": Buffer.byteLength(raw) });
39
+ res.end(raw);
40
+ }
41
+ function sendText(res, status, text) {
42
+ secure(res);
43
+ res.writeHead(status, { "Content-Type": "text/plain; charset=utf-8" });
44
+ res.end(text);
45
+ }
46
+ function readBody(req, max = MAX_BODY) {
47
+ return new Promise((resolve, reject) => {
48
+ const chunks = [];
49
+ let n = 0;
50
+ req.on("data", (c) => {
51
+ n += c.length;
52
+ if (n > max) {
53
+ reject(new HttpError(413, "body too large"));
54
+ req.destroy();
55
+ } else chunks.push(c);
56
+ });
57
+ req.on("end", () => resolve(Buffer.concat(chunks)));
58
+ req.on("error", reject);
59
+ });
60
+ }
61
+ async function readJson(req, max = MAX_BODY) {
62
+ const raw = await readBody(req, max);
63
+ try {
64
+ return raw.length ? JSON.parse(raw.toString("utf8")) : {};
65
+ } catch {
66
+ throw new HttpError(400, "invalid JSON");
67
+ }
68
+ }
69
+ function fail(res, err) {
70
+ if (err instanceof HttpError) return sendJson(res, err.status, { error: err.message, ...err.extra });
71
+ if (err instanceof SettingsError) return sendJson(res, err.status, { error: err.message });
72
+ console.error("internal error:", err.stack || err);
73
+ return sendJson(res, 500, { error: "internal error" });
74
+ }
75
+
76
+ /** index.html, with the launch token in a <meta> when this response is the nonce exchange. The page moves it into memory and sessionStorage and removes the tag. */
77
+ function serveIndex(res, launchToken) {
78
+ let html;
79
+ try {
80
+ html = fs.readFileSync(path.join(WEB_DIST, "index.html"), "utf8");
81
+ } catch {
82
+ return sendText(res, 404, "not found");
83
+ }
84
+ if (launchToken) {
85
+ const meta = `<meta name="onionskin-launch" content="${launchToken}">`; // base64url: no characters that need escaping
86
+ html = html.includes("</head>") ? html.replace("</head>", `${meta}</head>`) : meta + html;
87
+ }
88
+ // the saved background choice, as an allowlisted attribute (the CSP forbids inline script): the first paint already has the right ground
89
+ const bg = readSettings().background;
90
+ html = html.replace("<html ", `<html data-bg="${bg.kind === "custom" ? "custom" : bg.name}" `);
91
+ secure(res);
92
+ const raw = Buffer.from(html);
93
+ res.writeHead(200, { "Content-Type": MIME[".html"], "Content-Length": raw.length });
94
+ res.end(raw);
95
+ }
96
+
97
+ function serveStatic(res, urlPath) {
98
+ const rel = decodeURIComponent(urlPath).replace(/^\/+/, "");
99
+ const file = path.resolve(WEB_DIST, rel);
100
+ if (file !== WEB_DIST && !file.startsWith(WEB_DIST + path.sep)) return sendText(res, 403, "forbidden");
101
+ let st;
102
+ try {
103
+ st = fs.statSync(file);
104
+ } catch {
105
+ return sendText(res, 404, "not found");
106
+ }
107
+ if (!st.isFile()) return sendText(res, 404, "not found");
108
+ secure(res);
109
+ res.writeHead(200, { "Content-Type": MIME[path.extname(file)] || "application/octet-stream", "Content-Length": st.size });
110
+ fs.createReadStream(file).pipe(res);
111
+ }
112
+
113
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
114
+
115
+ /**
116
+ * Runs `fn` while holding <state>/start.lock (created exclusively, holds our pid). The probe-unlink-listen sequence must not interleave
117
+ * between two starting servers: one could unlink the other's live socket. A lock whose pid is gone is stale and taken over.
118
+ * Ceiling: stale-lock takeover is not atomic (two starts finding the same dead lock can both remove it); that needs two crashes in the same millisecond.
119
+ */
120
+ async function withStartLock(fn) {
121
+ const lock = path.join(stateDir(), "start.lock");
122
+ for (let tries = 0; ; tries++) {
123
+ try {
124
+ const fd = fs.openSync(lock, "wx", 0o600);
125
+ fs.writeSync(fd, String(process.pid));
126
+ fs.closeSync(fd);
127
+ break;
128
+ } catch (err) {
129
+ if (err.code !== "EEXIST") throw err;
130
+ let alive = false;
131
+ try {
132
+ process.kill(Number(fs.readFileSync(lock, "utf8")), 0);
133
+ alive = true;
134
+ } catch (e) {
135
+ alive = e.code === "EPERM";
136
+ }
137
+ if (!alive) {
138
+ try {
139
+ fs.unlinkSync(lock);
140
+ } catch {}
141
+ continue;
142
+ }
143
+ if (tries > 150) throw new Error("another server start has held the start lock for too long");
144
+ await sleep(100);
145
+ }
146
+ }
147
+ try {
148
+ return await fn();
149
+ } finally {
150
+ try {
151
+ fs.unlinkSync(lock);
152
+ } catch {}
153
+ }
154
+ }
155
+
156
+ export async function startServer({ port = 0 } = {}) {
157
+ ensureStateDir();
158
+ const sessions = new Sessions();
159
+ const sse = new Map(); // key -> Set<res>
160
+ // LibreOffice checks running after `done` returned (session key -> Set<{cancel}>). Cancelled when the session closes or the server stops.
161
+ const late = new Map();
162
+ function startLate(key, inputs, analysis) {
163
+ let h = null;
164
+ let finished = false; // the callback can run before startLateSoffice returns
165
+ h = startLateSoffice(inputs.pre, inputs.post, inputs.format, (result) => {
166
+ finished = true;
167
+ late.get(key)?.delete(h);
168
+ sessions.recordLateCheck(key, { batchId: analysis.batchId, postSha: analysis.postSha, result });
169
+ });
170
+ if (finished) return;
171
+ if (!late.has(key)) late.set(key, new Set());
172
+ late.get(key).add(h);
173
+ }
174
+ sessions.on("closed", (s) => cancelLate(s.key));
175
+ // learning (stage 1): what a Send can teach is stored on this Mac by src/voice-learn.js; ids and booleans, plus untrusted examples
176
+ sessions.on("voice", (job) => learnFromSend(job));
177
+ function cancelLate(key, why = "the session was closed before the check finished") {
178
+ for (const h of [...(late.get(key) || [])]) h.cancel(why);
179
+ late.delete(key);
180
+ }
181
+ /** The end of a run, for an agent's `done` and for a run on this Mac: the lease check, the checks off the event loop, `done`, the late LibreOffice check. */
182
+ async function finishRun(key, results, claim) {
183
+ const s = sessions.get(key);
184
+ const stageBefore = s.delivered?.stage;
185
+ const delivery = s.delivered;
186
+ const inputs = sessions.analysisInputs(s.key, claim); // checks the lease, reads the bytes, moves the stage to "checking"
187
+ let analysis, done;
188
+ try {
189
+ analysis = inputs ? await analyzeOffThread(inputs) : null; // the server keeps answering while the checks run
190
+ done = sessions.done(s.key, results, claim, analysis);
191
+ } catch (err) {
192
+ if (delivery && s.delivered === delivery && stageBefore) sessions.setStage(s.key, stageBefore); // done did not finish and this delivery is still the current one
193
+ throw err;
194
+ }
195
+ if (analysis && done.checks?.deep?.soffice === "pending") startLate(s.key, inputs, analysis);
196
+ warm(s);
197
+ warmNewest(s);
198
+ return done;
199
+ }
200
+ // a whole Send that Onionskin can do on this Mac is delivered to the local runner instead of an agent (src/local.js)
201
+ const local = localRunner(sessions, { finishRun });
202
+ sessions.local = local;
203
+ sessions.on("local", (s, ticket) => setImmediate(() => local.run(s.key, ticket)));
204
+ // the background agent (src/agent-runner.js): started for a file opened from a person's terminal, or for a Send nobody took
205
+ const agent = agentRunner(sessions, { tabs: (key) => sse.get(key)?.size || 0 });
206
+ sessions.agent = agent;
207
+ let auth;
208
+ let lastActivity = Date.now();
209
+
210
+ // ---- browser side (TCP loopback) -------------------------------------------------------------------------
211
+ async function browser(req, res) {
212
+ lastActivity = Date.now();
213
+ try {
214
+ const url = new URL(req.url, "http://x");
215
+ const p = url.pathname;
216
+ if (!auth.hostOk(req)) return sendText(res, 403, "bad host");
217
+ if (p === "/favicon.ico") {
218
+ secure(res);
219
+ res.writeHead(204);
220
+ return res.end();
221
+ }
222
+ if (req.method === "GET" && (p === "/" || p === "/index.html")) {
223
+ const nonce = url.searchParams.get("n");
224
+ let launchToken = "";
225
+ if (nonce) {
226
+ const ex = auth.exchange(nonce, req);
227
+ if (!ex) return sendText(res, 403, `This launch link was already used or has expired. Run \`${PRODUCT.bin} open <file>\` again.`);
228
+ res.setHeader("Set-Cookie", ex.cookie);
229
+ launchToken = ex.token; // handed over once, inside this page; never again by any route
230
+ } else if (!auth.cookieSession(req)) {
231
+ return sendText(res, 403, `Open documents with \`${PRODUCT.bin} open <file>\`.`);
232
+ }
233
+ return serveIndex(res, launchToken);
234
+ }
235
+ if (req.method === "GET" && !p.startsWith("/api/")) {
236
+ // app assets are public to the cookie holder only
237
+ if (!auth.cookieSession(req)) return sendText(res, 403, "no session");
238
+ return serveStatic(res, p);
239
+ }
240
+ if (!p.startsWith("/api/")) return sendText(res, 404, "not found");
241
+ const mutating = req.method !== "GET";
242
+ const isBootstrap = p === "/api/bootstrap";
243
+ const isEvents = p === "/api/events";
244
+ const bad = auth.check(req, { mutating, queryToken: isEvents ? url.searchParams.get("t") || "" : "" });
245
+ if (bad) return sendJson(res, isBootstrap && bad.code === "token_required" ? 401 : bad.status, { error: bad.error, ...(bad.code ? { code: bad.code, reopen: `${PRODUCT.bin} open <file>` } : {}) });
246
+ const key = url.searchParams.get("s") || "";
247
+ if (isBootstrap) return sendJson(res, 200, { session: sessions.view(sessions.get(key)), product: PRODUCT.name, settings: readSettings(), suggest: suggestStatus() });
248
+ // per-machine settings and the background image: not tied to a session, so any tab (a read-only one too) may use them
249
+ if (p === "/api/settings" || p.startsWith("/api/background")) return await settingsRoute(req, res, p);
250
+ // own stamps are per machine too: a list of {label, words} the person typed, any tab may use it
251
+ if (p === "/api/stamps") return await stampsRoute(req, res);
252
+ // writing preferences: per machine like settings (any tab may use them); a fixed list of ids plus up to 280 typed characters
253
+ if (p === "/api/style") {
254
+ if (req.method === "GET") return sendJson(res, 200, readStyle());
255
+ if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
256
+ return sendJson(res, 200, writeStyle(await readJson(req, 16 * 1024)));
257
+ }
258
+ // learning and the voice guide: per machine like the preferences. GET is ids, numbers and the person's own guide; a POST takes a
259
+ // trait id and an action, the guide, or a switch, never text for a suggestion. Browser only: the CLI socket has no such route.
260
+ if (p === "/api/voice") return req.method === "GET" ? sendJson(res, 200, voiceState()) : sendJson(res, 405, { error: "method not allowed" });
261
+ if (VOICE_POSTS.includes(p)) {
262
+ if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
263
+ return sendJson(res, 200, voicePost(p, await readJson(req, p === "/api/voice/guide" ? GUIDE_BODY_MAX : 16 * 1024)));
264
+ }
265
+ // the deeper-check setup is per machine, like settings: any tab may start it (a read-only one too). It takes no parameters.
266
+ if (p === "/api/setup-checker") return await setupCheckerRoute(req, res);
267
+ // the same for quick fixes (src/suggest.js): turning on Apple's built-in model for quick fixes, only when the person pressed the button
268
+ if (p === "/api/setup-rewriter") return await setupCheckerRoute(req, res, { start: startSetupRewriter, status: setupRewriterStatus });
269
+ const tab = String(req.headers["x-onionskin-tab"] || url.searchParams.get("tab") || "").slice(0, 64);
270
+ if (isEvents) return events(req, res, key, tab);
271
+ const s = sessions.get(key);
272
+ if (req.method === "GET" && p === "/api/file") {
273
+ const { bytes, diskSha } = sessions.loadForPage(key); // records an outside change the user is taking (history)
274
+ warm(s);
275
+ secure(res);
276
+ res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": bytes.length, "X-Sha256": diskSha });
277
+ return res.end(bytes);
278
+ }
279
+ if (req.method === "GET" && p === "/api/stale") return sendJson(res, 200, sessions.staleCheck(key, url.searchParams.get("sha") || ""));
280
+ if (req.method === "GET" && p === "/api/history") return sendJson(res, 200, await sessions.history(key));
281
+ if (req.method === "GET" && p.startsWith("/api/history/")) return sendJson(res, 200, await sessions.historyUpdate(key, decodeURIComponent(p.slice("/api/history/".length)).slice(0, 64)));
282
+ if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
283
+ // the preview of a quick change (src/quick-plan.js): read only, so no editor claim; it answers for the file as it is on disk now
284
+ if (p === "/api/quick") return sendJson(res, 200, await quickPreview(s, String((await readJson(req, 8 * 1024))?.note ?? "")));
285
+ // "Build my voice guide" (src/voice-learn.js): the editing tab only, with its own body cap (five 20 MB files in base64 plus 1 MB)
286
+ if (p === "/api/voice/build") {
287
+ sessions.requireEditor(key, tab);
288
+ return sendJson(res, 200, { ok: true, session: (await buildVoiceRequest(sessions, key, await readJson(req, VOICE_BUILD_MAX_BODY)), sessions.view(s)) });
289
+ }
290
+ // starts the writing helper of quick fixes if it is cold (src/suggest.js); carries no text, changes nothing
291
+ if (p === "/api/suggest/warm") return sendJson(res, 200, (await readJson(req, 1024), warmSuggest()));
292
+ const body = await readJson(req, p === "/api/export-tracked" || p === "/api/agent" ? 1024 : p === "/api/suggest" ? 8 * 1024 : MAX_BODY); // the export and the agent's buttons take ids only
293
+ if (p === "/api/claim" && !/^[\w-]{6,64}$/.test(tab)) return sendJson(res, 400, { error: "a tab id is required" });
294
+ if (p === "/api/claim") return sendJson(res, 200, { ok: sessions.claimEditor(key, tab, { takeover: body.takeover === true }), session: sessions.view(s) });
295
+ if (p !== "/api/resolve") sessions.requireEditor(key, tab);
296
+ // the background agent's buttons: a fixed action, and for `use` a CLI name, for `show` a switch. No path or text is accepted.
297
+ if (p === "/api/agent") {
298
+ const AGENT_ACTIONS = ["start", "stop", "use", "show", "terminal"];
299
+ if (!AGENT_ACTIONS.includes(body.action)) return sendJson(res, 400, { error: `action must be one of ${AGENT_ACTIONS.join(", ")}` });
300
+ if (body.action === "use" && body.cli !== "claude") return sendJson(res, 400, { error: "only Claude Code runs as the background agent in this version" });
301
+ if (body.action === "show" && typeof body.on !== "boolean") return sendJson(res, 400, { error: "on must be true or false" });
302
+ await agent.action(key, { action: body.action, cli: body.cli, on: body.on });
303
+ return sendJson(res, 200, { ok: true, session: sessions.view(s) });
304
+ }
305
+ if (p === "/api/draft") return sendJson(res, 200, { ok: true, session: (sessions.setDraft(key, body), sessions.view(s)) });
306
+ // a tap on a question card: an index (or null for "Something else"), never option text; checked against what the server stored
307
+ if (p === "/api/answer") return sendJson(res, 200, { ok: true, session: (sessions.answer(key, { id: String(body.id || ""), choice: body.choice }), sessions.view(s)) });
308
+ // review first (src/review.js): a button's kind, then taps on its cards (ids and an integer, never text)
309
+ if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (sessions.requestReview(key, body.kind), sessions.view(s)) });
310
+ if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice }), sessions.view(s)) });
311
+ // quick fixes (src/suggest.js): asking writes nothing; Use names the stored suggestion by id and writes it on this Mac
312
+ if (p === "/api/suggest") return sendJson(res, 200, await suggest(sessions, key, body));
313
+ if (p === "/api/suggest/use") return sendJson(res, 200, { ok: true, session: (useSuggestion(sessions, key, body), sessions.view(s)) });
314
+ if (p === "/api/send") {
315
+ const bytes = typeof body.bytesB64 === "string" ? Buffer.from(body.bytesB64, "base64") : null;
316
+ sessions.send(key, { baseSha: String(body.baseSha || ""), bytes, comments: body.comments, note: body.note, edits: body.edits, viewState: body.viewState, quick: body.quick });
317
+ warm(s);
318
+ return sendJson(res, 200, { ok: true, session: sessions.view(s) });
319
+ }
320
+ if (p === "/api/merge") {
321
+ const bytes = typeof body.bytesB64 === "string" ? Buffer.from(body.bytesB64, "base64") : null;
322
+ const r = await sessions.mergeUserEdits(key, { baseSha: String(body.baseSha || ""), bytes, edits: body.edits });
323
+ warm(s);
324
+ return sendJson(res, 200, { ok: true, ...r, session: sessions.view(s) });
325
+ }
326
+ if (p === "/api/takeback") return sendJson(res, 200, { ok: true, session: (sessions.takeBack(key), sessions.view(s)) });
327
+ if (p === "/api/revert") return sendJson(res, 200, { ok: true, session: (sessions.revert(key, String(body.id || "")), warm(s), sessions.view(s)) });
328
+ if (p === "/api/resolve") return sendJson(res, 200, sessions.resolveAll(key));
329
+ if (p === "/api/export-tracked") return sendJson(res, 200, await sessions.exportTracked(key, String(body.id || "").slice(0, 64)));
330
+ if (p === "/api/show-copy") return sendJson(res, 200, sessions.showCopy(String(body.path || "")));
331
+ if (p === "/api/dismiss-conflict") return sendJson(res, 200, { ok: true, session: (sessions.dismissConflict(key), sessions.view(s)) });
332
+ if (p === "/api/close") return sendJson(res, 200, { ok: true, session: (sessions.close(key), sessions.view(s)) });
333
+ return sendJson(res, 404, { error: "not found" });
334
+ } catch (err) {
335
+ fail(res, err);
336
+ }
337
+ }
338
+
339
+ async function settingsRoute(req, res, p) {
340
+ if (p === "/api/settings" && req.method === "GET") return sendJson(res, 200, { ...readSettings(), presets: BG_PRESETS });
341
+ if (p === "/api/background" && req.method === "GET") {
342
+ const img = readCustomImage();
343
+ if (!img) return sendJson(res, 404, { error: "no custom image" });
344
+ secure(res);
345
+ res.writeHead(200, { "Content-Type": img.mime, "Content-Length": img.bytes.length });
346
+ return res.end(img.bytes);
347
+ }
348
+ if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
349
+ if (p === "/api/settings") {
350
+ const body = await readJson(req, 64 * 1024);
351
+ return sendJson(res, 200, writeBackground(body.background));
352
+ }
353
+ if (p === "/api/background") {
354
+ // JSON {bytesB64}: the browser never names a path. The cap is the 15 MB image in base64 plus the envelope.
355
+ const body = await readJson(req, Math.ceil((MAX_IMAGE_BYTES * 4) / 3) + 1024);
356
+ const bytes = typeof body.bytesB64 === "string" ? Buffer.from(body.bytesB64, "base64") : null;
357
+ const saved = saveCustomImage(bytes);
358
+ return sendJson(res, 200, writeBackground({ ...(body.background && typeof body.background === "object" ? body.background : saved.background), kind: "custom" }));
359
+ }
360
+ if (p === "/api/background/remove") return sendJson(res, 200, removeCustomImage());
361
+ return sendJson(res, 404, { error: "not found" });
362
+ }
363
+
364
+ /** GET: the list. POST {stamps}: replace the whole list or refuse it (400); nothing is trimmed to fit. */
365
+ async function stampsRoute(req, res) {
366
+ if (req.method === "GET") return sendJson(res, 200, { stamps: readStamps() });
367
+ if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
368
+ const body = await readJson(req, 16 * 1024);
369
+ if (Object.keys(body).some((k) => k !== "stamps")) return sendJson(res, 400, { error: "only stamps can be sent" });
370
+ return sendJson(res, 200, { stamps: writeStamps(body.stamps) });
371
+ }
372
+
373
+ /** GET: where the setup is. POST: start it (or join the one running) and answer at once; the page polls GET. Nothing from the request is used. */
374
+ async function setupCheckerRoute(req, res, { start = startSetupChecker, status = setupCheckerStatus } = {}) {
375
+ if (req.method === "POST") {
376
+ await readJson(req, 1024); // drained and checked as JSON, never read
377
+ start();
378
+ } else if (req.method !== "GET") return sendJson(res, 405, { error: "method not allowed" });
379
+ return sendJson(res, 200, await status());
380
+ }
381
+
382
+ function events(req, res, key, tab) {
383
+ const s = sessions.get(key);
384
+ if (/^[\w-]{6,64}$/.test(tab)) {
385
+ sessions.claimEditor(key, tab);
386
+ req.on("close", () => sessions.releaseEditor(key, tab));
387
+ }
388
+ secure(res);
389
+ res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-store", Connection: "keep-alive" });
390
+ res.write(`event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
391
+ if (!sse.has(key)) sse.set(key, new Set());
392
+ sse.get(key).add(res);
393
+ req.on("close", () => sse.get(key)?.delete(res));
394
+ }
395
+ sessions.on("change", (s, event) => {
396
+ const set = sse.get(s.key);
397
+ if (!set?.size) return;
398
+ const view = JSON.stringify(sessions.view(s));
399
+ for (const res of set) {
400
+ if (event === "reload") res.write(`event: reload\ndata: ${view}\n\n`);
401
+ res.write(`event: state\ndata: ${view}\n\n`);
402
+ }
403
+ });
404
+
405
+ // ---- CLI side (unix socket) ------------------------------------------------------------------------------
406
+ async function cli(req, res) {
407
+ lastActivity = Date.now();
408
+ try {
409
+ const url = new URL(req.url, "http://x");
410
+ const p = url.pathname;
411
+ if (p === "/health") return sendJson(res, 200, { ok: true, pid: process.pid, protocol: SERVER_PROTOCOL, sessions: sessions.map.size });
412
+ if (req.method === "POST" && p === "/cli/open") {
413
+ const { file, agent: wants } = await readJson(req);
414
+ const s = sessions.open(String(file || ""), { agent: wants === "background" ? "background" : undefined });
415
+ if (wants === "background") agent.ensure(s.key); // a person opened it from a terminal: the background agent works on it
416
+ const nonce = auth.newNonce();
417
+ return sendJson(res, 200, { key: s.key, url: `${auth.origin}/?s=${s.key}&n=${nonce}`, state: sessions.state(s), name: path.basename(s.file) });
418
+ }
419
+ if (req.method === "GET" && p === "/cli/status") {
420
+ return sendJson(res, 200, cliStatus(sessions, sessions.byFile(url.searchParams.get("file") || "")));
421
+ }
422
+ if (req.method === "GET" && p === "/cli/batch-file") {
423
+ // which file a delivered batch belongs to (for `reply --lease <ticket>` without a file); the lease token is still checked at the write
424
+ const id = url.searchParams.get("batch_id") || "";
425
+ const s = id ? [...sessions.map.values()].find((x) => x.delivered?.batch.batch_id === id) : null;
426
+ return sendJson(res, 200, { file: s ? s.file : null });
427
+ }
428
+ if (req.method === "GET" && p === "/cli/batch") {
429
+ // the batch delivered to the agent (anchors for `apply`); null when none is in progress
430
+ const s = sessions.byFile(url.searchParams.get("file") || "");
431
+ return sendJson(res, 200, { state: sessions.state(s), batch: s.delivered?.batch || null });
432
+ }
433
+ if (req.method === "POST" && p === "/cli/agent-write") {
434
+ const body = await readJson(req);
435
+ const s = sessions.byFile(String(body.file || ""));
436
+ const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
437
+ // `ops` is what apply ran to make these bytes: kept for a later merge, which checks it against the bytes before relying on it
438
+ return sendJson(res, 200, 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) }));
439
+ }
440
+ if (req.method === "POST" && p === "/cli/commit") {
441
+ // candidate bytes made elsewhere (a copy edited with python-docx and so on): same lease check, compare-and-swap, snapshot and atomic replace as apply.
442
+ // Without base_sha256 the base is the hash this server last wrote or delivered for the batch.
443
+ const body = await readJson(req);
444
+ const s = sessions.byFile(String(body.file || ""));
445
+ const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
446
+ const baseSha = typeof body.base_sha256 === "string" && body.base_sha256 ? body.base_sha256 : null;
447
+ return sendJson(res, 200, sessions.agentWrite(s.key, { baseSha, bytes, label: "before agent commit", ...leaseOf(body) }));
448
+ }
449
+ if (req.method === "POST" && p === "/cli/apply-report") {
450
+ const body = await readJson(req);
451
+ const s = sessions.byFile(String(body.file || ""));
452
+ return sendJson(res, 200, sessions.noteApply(s.key, {
453
+ ...leaseOf(body), run: typeof body.run === "string" ? body.run : "",
454
+ problems: Array.isArray(body.problems) ? body.problems : [], applied: Array.isArray(body.applied) ? body.applied.filter((k) => typeof k === "string") : [],
455
+ }));
456
+ }
457
+ if (req.method === "GET" && p === "/cli/poll") {
458
+ const f = url.searchParams.get("file") || "";
459
+ let s;
460
+ try {
461
+ s = sessions.byFile(f);
462
+ } catch {
463
+ s = sessions.open(f); // a poller may start before (or after a server restart without) `open`
464
+ }
465
+ const handle = sessions.poll(s.key, { ack: url.searchParams.get("ack") === "1", holder: url.searchParams.get("holder") || "agent", full: url.searchParams.get("full") === "1", compact: url.searchParams.get("compact") === "1" });
466
+ res.on("close", () => handle.cancel());
467
+ const out = await handle.promise;
468
+ if (res.destroyed) return;
469
+ return sendJson(res, 200, out);
470
+ }
471
+ if (req.method === "POST" && p === "/cli/done") {
472
+ const body = await readJson(req);
473
+ const s = sessions.byFile(String(body.file || ""));
474
+ return sendJson(res, 200, await finishRun(s.key, body.results, leaseOf(body)));
475
+ }
476
+ if (req.method === "POST" && p === "/cli/stop") {
477
+ sendJson(res, 200, { ok: true });
478
+ // a deliberate stop ends every waiting poll with "closed" first; otherwise the poll would see a lost connection and start the server again
479
+ for (const s of sessions.map.values()) for (const w of s.pollers.splice(0)) w.resolve({ status: "closed", reason: "the server was stopped" });
480
+ setTimeout(() => shutdown(), 100);
481
+ return;
482
+ }
483
+ return sendJson(res, 404, { error: "not found" });
484
+ } catch (err) {
485
+ fail(res, err);
486
+ }
487
+ }
488
+
489
+ // ---- listen ----------------------------------------------------------------------------------------------
490
+ const sock = socketPath();
491
+ let tcp, unix, tcpPort;
492
+ await withStartLock(async () => {
493
+ if (fs.existsSync(sock)) {
494
+ const health = () =>
495
+ new Promise((resolve) => {
496
+ const r = http.get({ socketPath: sock, path: "/health", timeout: 1000 }, (x) => {
497
+ let t = "";
498
+ x.on("data", (d) => (t += d));
499
+ x.on("end", () => {
500
+ let protocol = null;
501
+ try {
502
+ protocol = JSON.parse(t).protocol;
503
+ } catch {}
504
+ resolve(x.statusCode === 200 ? { protocol } : null);
505
+ });
506
+ });
507
+ r.on("error", () => resolve(null));
508
+ r.on("timeout", () => (r.destroy(), resolve(null)));
509
+ });
510
+ let other = await health();
511
+ if (other && other.protocol !== SERVER_PROTOCOL) {
512
+ // a server left running by another build (an upgrade): it cannot serve this CLI. Ask it to stop, then take its place; its sessions are in the state dir and load here.
513
+ await new Promise((resolve) => {
514
+ const r = http.request({ socketPath: sock, method: "POST", path: "/cli/stop", headers: { "content-type": "application/json" }, timeout: 2000 }, (x) => (x.resume(), x.on("end", resolve)));
515
+ r.on("error", resolve);
516
+ r.on("timeout", () => (r.destroy(), resolve()));
517
+ r.end("{}");
518
+ });
519
+ for (let i = 0; i < 40 && other; i++) {
520
+ await new Promise((r) => setTimeout(r, 100));
521
+ other = fs.existsSync(sock) ? await health() : null;
522
+ }
523
+ if (other) throw new Error("a server from another Onionskin version is running and did not stop; end it and try again");
524
+ } else if (other) throw Object.assign(new Error("a server is already running"), { code: "EALREADY" });
525
+ if (fs.existsSync(sock)) fs.unlinkSync(sock);
526
+ }
527
+ await agent.reapOrphans(); // this server owns the state dir now: agents a crashed predecessor left behind are stopped
528
+ tcp = http.createServer(browser);
529
+ await new Promise((resolve, reject) => (tcp.once("error", reject), tcp.listen(port, "127.0.0.1", resolve)));
530
+ tcpPort = tcp.address().port;
531
+ auth = new BrowserAuth(tcpPort);
532
+ unix = http.createServer(cli);
533
+ await new Promise((resolve, reject) => (unix.once("error", reject), unix.listen(sock, resolve)));
534
+ fs.chmodSync(sock, 0o600);
535
+ writeStateFile(serverRecordPath(), JSON.stringify({ pid: process.pid, port: tcpPort, protocol: SERVER_PROTOCOL }));
536
+ });
537
+
538
+ const idleMs = Number(env("IDLE_MS", 2 * 60 * 60 * 1000));
539
+ const timer = setInterval(() => {
540
+ sessions.tick();
541
+ agent.tick();
542
+ for (const set of sse.values()) for (const r of set) r.write(": ping\n\n");
543
+ // the background agent's own polls do not keep the server up (it stops after 20 minutes without a tab; then the server may stop too)
544
+ const busy = [...sessions.map.values()].some((s) => s.pollers.some((p) => !isBgHolder(p.holder)) || s.delivered);
545
+ if (!busy && ![...sse.values()].some((x) => x.size) && Date.now() - lastActivity > idleMs) shutdown();
546
+ }, Number(env("TICK_MS", 1000)));
547
+
548
+ function shutdown() {
549
+ clearInterval(timer);
550
+ for (const k of [...late.keys()]) cancelLate(k, "the server stopped"); // no soffice outlives the server
551
+ stopRewriter(); // nor the local rewriter's worker
552
+ stopVoice(); // nor a learning worker
553
+ for (const set of sse.values()) for (const r of set) r.end();
554
+ tcp.close();
555
+ // only what is ours: a newer server may have taken over the socket and the record since. Closing a unix server unlinks its path
556
+ // (libuv), so a server that no longer owns the path must not close it; the process exit below ends its connections.
557
+ let ours = false;
558
+ try {
559
+ ours = JSON.parse(fs.readFileSync(serverRecordPath(), "utf8")).pid === process.pid;
560
+ } catch {}
561
+ if (ours) unix.close();
562
+ tcp.closeAllConnections?.();
563
+ unix.closeAllConnections?.();
564
+ if (ours) {
565
+ try {
566
+ fs.unlinkSync(sock);
567
+ } catch {}
568
+ try {
569
+ fs.unlinkSync(serverRecordPath());
570
+ } catch {}
571
+ }
572
+ // no background agent outlives the server: their groups are stopped first (at most about 5 s)
573
+ Promise.race([agent.stopAll(), sleep(5000)]).finally(() => setTimeout(() => process.exit(0), 50).unref());
574
+ }
575
+ return { port: tcpPort, sessions, local, agent, auth, shutdown, tcp, unix };
576
+ }