onionskin-cli 0.0.2 → 0.0.5
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 +111 -0
- package/INSTALL.md +5 -1
- package/NOTICE +34 -0
- package/README.md +101 -33
- package/docs/contracts/batch.schema.json +27 -4
- package/docs/contracts/ops.schema.json +117 -5
- package/package.json +4 -2
- package/skills/onionskin/SKILL.md +9 -6
- package/skills/onionskin/reference/batch.md +5 -3
- package/skills/onionskin/reference/formats.md +5 -1
- package/skills/onionskin/reference/ops.md +47 -3
- 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 +77 -19
- 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/cli.js +91 -25
- package/src/constants.js +34 -1
- package/src/diff.js +3 -1
- package/src/docx.js +368 -58
- package/src/edit-hash.js +19 -0
- package/src/fsutil.js +1 -1
- package/src/history-worker.js +8 -8
- package/src/history.js +135 -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 +880 -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 +1257 -0
- package/src/local.js +68 -4
- package/src/merge-package.js +1110 -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 +400 -1
- package/src/open-worker.js +27 -0
- package/src/ops/common.js +2 -1
- package/src/ops/docx.js +58 -29
- package/src/ops/html.js +394 -0
- package/src/ops/pptx.js +74 -21
- 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 +12 -7
- package/src/ops-worker.js +17 -0
- package/src/package.js +26 -2
- package/src/person.js +8 -1
- package/src/pptx.js +85 -21
- package/src/quick-plan.js +13 -6
- 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/sanitize.js +16 -3
- package/src/server.js +88 -37
- package/src/session.js +1143 -208
- 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 +288 -0
- package/src/voice-guide.js +4 -3
- package/src/voice-learn.js +4 -3
- package/src/voice-sample-worker.js +7 -6
- package/src/voice-worker.js +4 -6
- package/src/watch.js +188 -0
- package/src/worker-host.js +23 -0
- package/src/xlsx.js +355 -94
- package/src/xml-tree.js +6 -3
- package/src/xml.js +82 -34
- package/src/zip-write.js +2 -1
- package/src/zip.js +9 -5
- 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-DkNx0LSH.css +1 -0
- package/web/dist/assets/{index-BUtofdtr.js → index-DytKkMdi.js} +117 -107
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-Dfizlg0i.css +0 -1
package/src/server.js
CHANGED
|
@@ -7,17 +7,21 @@ import { PRODUCT, SERVER_PROTOCOL, env } from "./constants.js";
|
|
|
7
7
|
import { sha256 } from "./fsutil.js";
|
|
8
8
|
import { ensureStateDir, serverRecordPath, socketPath, stateDir, writeStateFile } from "./paths.js";
|
|
9
9
|
import { setupCheckerStatus, startLateSoffice, startSetupChecker } from "./checker.js";
|
|
10
|
-
import { analyzeOffThread } from "./offload.js";
|
|
10
|
+
import { analyzeOffThread, warmAnchors } from "./offload.js";
|
|
11
|
+
import { REQUEST_MAX, requestSize } from "./retain.js";
|
|
11
12
|
import { warm, warmNewest } from "./history.js";
|
|
12
13
|
import { stopRewriter } from "./rewriter.js";
|
|
13
|
-
import { HttpError, Sessions } from "./session.js";
|
|
14
|
+
import { HttpError, Sessions, tooLarge } from "./session.js";
|
|
15
|
+
import { encodeHtmlView } from "./html-envelope.js";
|
|
16
|
+
import { CapError } from "./xml.js";
|
|
14
17
|
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
|
|
18
|
+
import { batchFile, checkBinding, claimOfQuery, cliBatch, cliStatus, leaseOf, localRunner, openWork, sessionFor } from "./local.js"; // leaseOf: the delivery an agent acts on (batch, holder, token), all three required
|
|
16
19
|
import { readStamps, writeStamps } from "./stamps.js";
|
|
17
20
|
import { setupRewriterStatus, startSetupRewriter, suggest, suggestStatus, useSuggestion, warmSuggest } from "./suggest.js";
|
|
18
21
|
import { readStyle, writeStyle } from "./style.js";
|
|
19
22
|
import { GUIDE_BODY_MAX, VOICE_BUILD_MAX_BODY, VOICE_POSTS, buildVoiceRequest, learnFromSend, stopVoice, voicePost, voiceState } from "./voice-learn.js";
|
|
20
23
|
import { agentRunner } from "./agent-runner.js";
|
|
24
|
+
import { docWatcher } from "./watch.js";
|
|
21
25
|
import { isBgHolder } from "./agent-detect.js";
|
|
22
26
|
import { BG_PRESETS, MAX_IMAGE_BYTES, SettingsError, readCustomImage, readSettings, removeCustomImage, saveCustomImage, writeBackground } from "./settings.js";
|
|
23
27
|
|
|
@@ -31,6 +35,7 @@ function secure(res) {
|
|
|
31
35
|
res.setHeader("Referrer-Policy", "no-referrer");
|
|
32
36
|
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
33
37
|
res.setHeader("Cache-Control", "no-store");
|
|
38
|
+
res.setHeader("X-DNS-Prefetch-Control", "off"); // PLAN-html R3, R21: no DNS prefetch for anything the page or an HTML view names
|
|
34
39
|
}
|
|
35
40
|
function sendJson(res, status, body) {
|
|
36
41
|
secure(res);
|
|
@@ -43,23 +48,32 @@ function sendText(res, status, text) {
|
|
|
43
48
|
res.writeHead(status, { "Content-Type": "text/plain; charset=utf-8" });
|
|
44
49
|
res.end(text);
|
|
45
50
|
}
|
|
46
|
-
|
|
51
|
+
// A body past `max` is refused with a JSON answer the page can show (413, code too_large_request), not a cut connection (the page read
|
|
52
|
+
// that as a failure to retry forever): the rest of it is read and dropped, up to MAX_BODY more, so the browser gets the answer; past
|
|
53
|
+
// that the connection is closed.
|
|
54
|
+
function readBody(req, max = MAX_BODY, refusal = null) {
|
|
47
55
|
return new Promise((resolve, reject) => {
|
|
48
56
|
const chunks = [];
|
|
49
57
|
let n = 0;
|
|
58
|
+
let over = false;
|
|
50
59
|
req.on("data", (c) => {
|
|
51
60
|
n += c.length;
|
|
61
|
+
if (over) {
|
|
62
|
+
if (n > max + MAX_BODY) req.destroy();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
52
65
|
if (n > max) {
|
|
53
|
-
|
|
54
|
-
|
|
66
|
+
over = true;
|
|
67
|
+
chunks.length = 0;
|
|
68
|
+
reject(refusal || new HttpError(413, "body too large", { code: "too_large_request" }));
|
|
55
69
|
} else chunks.push(c);
|
|
56
70
|
});
|
|
57
71
|
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
58
72
|
req.on("error", reject);
|
|
59
73
|
});
|
|
60
74
|
}
|
|
61
|
-
async function readJson(req, max = MAX_BODY) {
|
|
62
|
-
const raw = await readBody(req, max);
|
|
75
|
+
async function readJson(req, max = MAX_BODY, refusal = null) {
|
|
76
|
+
const raw = await readBody(req, max, refusal);
|
|
63
77
|
try {
|
|
64
78
|
return raw.length ? JSON.parse(raw.toString("utf8")) : {};
|
|
65
79
|
} catch {
|
|
@@ -69,6 +83,8 @@ async function readJson(req, max = MAX_BODY) {
|
|
|
69
83
|
function fail(res, err) {
|
|
70
84
|
if (err instanceof HttpError) return sendJson(res, err.status, { error: err.message, ...err.extra });
|
|
71
85
|
if (err instanceof SettingsError) return sendJson(res, err.status, { error: err.message });
|
|
86
|
+
// a document past a reader's cap (src/xml.js): plain words a person can act on, whatever route read it
|
|
87
|
+
if (err instanceof CapError) return sendJson(res, 422, { error: `Onionskin cannot read this file: ${err.message}.`, code: "too_large" });
|
|
72
88
|
console.error("internal error:", err.stack || err);
|
|
73
89
|
return sendJson(res, 500, { error: "internal error" });
|
|
74
90
|
}
|
|
@@ -157,6 +173,10 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
157
173
|
ensureStateDir();
|
|
158
174
|
const sessions = new Sessions();
|
|
159
175
|
const sse = new Map(); // key -> Set<res>
|
|
176
|
+
// key -> when a page of that session last spoke (its event stream opened or ended, any other request): `onionskin update` holds
|
|
177
|
+
// off while a tab is open or was open lately (src/local.js openWork), since edits typed in a tab live only in that tab until Send
|
|
178
|
+
const tabSeen = new Map();
|
|
179
|
+
const tabsOf = (key) => sse.get(key)?.size || 0;
|
|
160
180
|
// LibreOffice checks running after `done` returned (session key -> Set<{cancel}>). Cancelled when the session closes or the server stops.
|
|
161
181
|
const late = new Map();
|
|
162
182
|
function startLate(key, inputs, analysis) {
|
|
@@ -187,7 +207,7 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
187
207
|
let analysis, done;
|
|
188
208
|
try {
|
|
189
209
|
analysis = inputs ? await analyzeOffThread(inputs) : null; // the server keeps answering while the checks run
|
|
190
|
-
done = sessions.
|
|
210
|
+
done = await sessions.doneAsync(s.key, results, claim, analysis, { inline: false }); // the checks and the anchors never run on this thread
|
|
191
211
|
} catch (err) {
|
|
192
212
|
if (delivery && s.delivered === delivery && stageBefore) sessions.setStage(s.key, stageBefore); // done did not finish and this delivery is still the current one
|
|
193
213
|
throw err;
|
|
@@ -202,8 +222,11 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
202
222
|
sessions.local = local;
|
|
203
223
|
sessions.on("local", (s, ticket) => setImmediate(() => local.run(s.key, ticket)));
|
|
204
224
|
// 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:
|
|
225
|
+
const agent = agentRunner(sessions, { tabs: tabsOf });
|
|
206
226
|
sessions.agent = agent;
|
|
227
|
+
// a faster outside-edit notice (src/watch.js); the page's /api/stale poll stays the backstop
|
|
228
|
+
const watcher = docWatcher(sessions);
|
|
229
|
+
sessions.watch = watcher;
|
|
207
230
|
let auth;
|
|
208
231
|
let lastActivity = Date.now();
|
|
209
232
|
|
|
@@ -244,7 +267,8 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
244
267
|
const bad = auth.check(req, { mutating, queryToken: isEvents ? url.searchParams.get("t") || "" : "" });
|
|
245
268
|
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
269
|
const key = url.searchParams.get("s") || "";
|
|
247
|
-
|
|
270
|
+
// a page that loads (or reloads) while a Send of this file is in flight starts from what it committed (Sessions.settled)
|
|
271
|
+
if (isBootstrap) return sendJson(res, 200, { session: (await sessions.settled(key), sessions.view(sessions.get(key))), product: PRODUCT.name, settings: readSettings(), suggest: suggestStatus() });
|
|
248
272
|
// per-machine settings and the background image: not tied to a session, so any tab (a read-only one too) may use them
|
|
249
273
|
if (p === "/api/settings" || p.startsWith("/api/background")) return await settingsRoute(req, res, p);
|
|
250
274
|
// own stamps are per machine too: a list of {label, words} the person typed, any tab may use it
|
|
@@ -269,6 +293,7 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
269
293
|
const tab = String(req.headers["x-onionskin-tab"] || url.searchParams.get("tab") || "").slice(0, 64);
|
|
270
294
|
if (isEvents) return events(req, res, key, tab);
|
|
271
295
|
const s = sessions.get(key);
|
|
296
|
+
tabSeen.set(key, Date.now());
|
|
272
297
|
if (req.method === "GET" && p === "/api/file") {
|
|
273
298
|
const { bytes, diskSha } = sessions.loadForPage(key); // records an outside change the user is taking (history)
|
|
274
299
|
warm(s);
|
|
@@ -276,6 +301,16 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
276
301
|
res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": bytes.length, "X-Sha256": diskSha });
|
|
277
302
|
return res.end(bytes);
|
|
278
303
|
}
|
|
304
|
+
if (req.method === "GET" && p === "/api/html-view") {
|
|
305
|
+
// an HTML file's render copy and what the page needs with it, in one binary body (src/html-envelope.js); never text/html (R12)
|
|
306
|
+
const v = await sessions.htmlView(key, { plain: url.searchParams.get("plain") === "1" });
|
|
307
|
+
warm(s);
|
|
308
|
+
const [prefix, copy] = encodeHtmlView({ version: v.sha, plain: v.plain, reason: v.reason, stats: v.stats, texts: v.texts, editable: v.editable, ident: v.ident, copy: v.bytes });
|
|
309
|
+
secure(res);
|
|
310
|
+
res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": prefix.length + copy.length, "X-Sha256": v.sha });
|
|
311
|
+
res.write(prefix);
|
|
312
|
+
return res.end(copy);
|
|
313
|
+
}
|
|
279
314
|
if (req.method === "GET" && p === "/api/stale") return sendJson(res, 200, sessions.staleCheck(key, url.searchParams.get("sha") || ""));
|
|
280
315
|
if (req.method === "GET" && p === "/api/history") return sendJson(res, 200, await sessions.history(key));
|
|
281
316
|
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)));
|
|
@@ -289,9 +324,14 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
289
324
|
}
|
|
290
325
|
// starts the writing helper of quick fixes if it is cold (src/suggest.js); carries no text, changes nothing
|
|
291
326
|
if (p === "/api/suggest/warm") return sendJson(res, 200, (await readJson(req, 1024), warmSuggest()));
|
|
292
|
-
|
|
327
|
+
// the export and the agent's buttons take ids only; a draft carries no document, so it is held to the cap of a request's own data
|
|
328
|
+
const body = await readJson(req, p === "/api/export-tracked" || p === "/api/agent" ? 1024 : p === "/api/suggest" ? 8 * 1024 : p === "/api/draft" || p === "/api/handover" ? REQUEST_MAX : MAX_BODY, p === "/api/draft" || p === "/api/handover" ? tooLarge("This draft") : null);
|
|
329
|
+
// a Send or a quick fix's Use carries the document too: their comments, note, typed edits and view state are held to the same cap
|
|
330
|
+
if ((p === "/api/send" || p === "/api/suggest/use") && requestSize([body.comments, body.note, body.edits, body.viewState, body.htmlEdits]) > REQUEST_MAX) throw tooLarge(p === "/api/send" ? "This Send" : "This request");
|
|
293
331
|
if (p === "/api/claim" && !/^[\w-]{6,64}$/.test(tab)) return sendJson(res, 400, { error: "a tab id is required" });
|
|
294
332
|
if (p === "/api/claim") return sendJson(res, 200, { ok: sessions.claimEditor(key, tab, { takeover: body.takeover === true }), session: sessions.view(s) });
|
|
333
|
+
// the tab that just lost editing hands over its unsaved words (Sessions.handOver checks that it is that tab), in the file's turn
|
|
334
|
+
if (p === "/api/handover") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.handOver(key, tab, { comments: body.comments, note: body.note, base: body.base, baseSeq: body.baseSeq, noteRev: body.noteRev })), sessions.view(s)) });
|
|
295
335
|
if (p !== "/api/resolve") sessions.requireEditor(key, tab);
|
|
296
336
|
// 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
337
|
if (p === "/api/agent") {
|
|
@@ -299,21 +339,25 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
299
339
|
if (!AGENT_ACTIONS.includes(body.action)) return sendJson(res, 400, { error: `action must be one of ${AGENT_ACTIONS.join(", ")}` });
|
|
300
340
|
if (body.action === "use" && body.cli !== "claude") return sendJson(res, 400, { error: "only Claude Code runs as the background agent in this version" });
|
|
301
341
|
if (body.action === "show" && typeof body.on !== "boolean") return sendJson(res, 400, { error: "on must be true or false" });
|
|
302
|
-
|
|
342
|
+
if (body.takeBack !== undefined && (body.action !== "stop" || typeof body.takeBack !== "boolean")) return sendJson(res, 400, { error: "takeBack goes with stop only, as true or false" });
|
|
343
|
+
// stop with takeBack: the page's Stop. The batch comes back to drafts first, then the agent that held it is stopped (src/agent-runner.js)
|
|
344
|
+
await agent.action(key, { action: body.action, cli: body.cli, on: body.on, takeBack: body.takeBack === true });
|
|
303
345
|
return sendJson(res, 200, { ok: true, session: sessions.view(s) });
|
|
304
346
|
}
|
|
305
|
-
|
|
347
|
+
// a draft save, an answer and Take back run in this file's turn: one that arrives while a Send is in flight waits for it (Sessions.inTurn)
|
|
348
|
+
if (p === "/api/draft") return sendJson(res, 200, { ok: true, session: (await sessions.setDraftAsync(key, { comments: body.comments, note: body.note, rev: body.rev, noteRev: body.noteRev, seen: body.seen, noteSeen: body.noteSeen, tab }), sessions.view(s)) });
|
|
306
349
|
// 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)) });
|
|
350
|
+
if (p === "/api/answer") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.answer(key, { id: String(body.id || ""), choice: body.choice })), sessions.view(s)) });
|
|
308
351
|
// review first (src/review.js): a button's kind, then taps on its cards (ids and an integer, never text)
|
|
309
352
|
if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (sessions.requestReview(key, body.kind), sessions.view(s)) });
|
|
310
353
|
if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice }), sessions.view(s)) });
|
|
311
354
|
// quick fixes (src/suggest.js): asking writes nothing; Use names the stored suggestion by id and writes it on this Mac
|
|
312
355
|
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)) });
|
|
356
|
+
if (p === "/api/suggest/use") return sendJson(res, 200, { ok: true, session: (await useSuggestion(sessions, key, { ...body, tab }), sessions.view(s)) });
|
|
314
357
|
if (p === "/api/send") {
|
|
315
358
|
const bytes = typeof body.bytesB64 === "string" ? Buffer.from(body.bytesB64, "base64") : null;
|
|
316
|
-
|
|
359
|
+
// an HTML file's text edits (PLAN-html R9) come as {nodeId, versionSha, baseTextHash, newText}, never as bytes
|
|
360
|
+
await sessions.sendAsync(key, { baseSha: String(body.baseSha || ""), bytes, comments: body.comments, note: body.note, edits: body.edits, htmlEdits: body.htmlEdits, viewState: body.viewState, quick: body.quick, rev: body.rev, noteRev: body.noteRev, seen: body.seen, noteSeen: body.noteSeen, sends: body.sends, tab });
|
|
317
361
|
warm(s);
|
|
318
362
|
return sendJson(res, 200, { ok: true, session: sessions.view(s) });
|
|
319
363
|
}
|
|
@@ -323,9 +367,9 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
323
367
|
warm(s);
|
|
324
368
|
return sendJson(res, 200, { ok: true, ...r, session: sessions.view(s) });
|
|
325
369
|
}
|
|
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.
|
|
328
|
-
if (p === "/api/resolve") return sendJson(res, 200, sessions.
|
|
370
|
+
if (p === "/api/takeback") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.takeBack(key)), sessions.view(s)) });
|
|
371
|
+
if (p === "/api/revert") return sendJson(res, 200, { ok: true, session: (await sessions.revertAsync(key, String(body.id || "")), warm(s), sessions.view(s)) });
|
|
372
|
+
if (p === "/api/resolve") return sendJson(res, 200, await sessions.resolveAllAsync(key));
|
|
329
373
|
if (p === "/api/export-tracked") return sendJson(res, 200, await sessions.exportTracked(key, String(body.id || "").slice(0, 64)));
|
|
330
374
|
if (p === "/api/show-copy") return sendJson(res, 200, sessions.showCopy(String(body.path || "")));
|
|
331
375
|
if (p === "/api/dismiss-conflict") return sendJson(res, 200, { ok: true, session: (sessions.dismissConflict(key), sessions.view(s)) });
|
|
@@ -390,7 +434,8 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
390
434
|
res.write(`event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
|
|
391
435
|
if (!sse.has(key)) sse.set(key, new Set());
|
|
392
436
|
sse.get(key).add(res);
|
|
393
|
-
|
|
437
|
+
tabSeen.set(key, Date.now());
|
|
438
|
+
req.on("close", () => (sse.get(key)?.delete(res), tabSeen.set(key, Date.now())));
|
|
394
439
|
}
|
|
395
440
|
sessions.on("change", (s, event) => {
|
|
396
441
|
const set = sse.get(s.key);
|
|
@@ -411,44 +456,46 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
411
456
|
if (p === "/health") return sendJson(res, 200, { ok: true, pid: process.pid, protocol: SERVER_PROTOCOL, sessions: sessions.map.size });
|
|
412
457
|
if (req.method === "POST" && p === "/cli/open") {
|
|
413
458
|
const { file, agent: wants } = await readJson(req);
|
|
414
|
-
const s = sessions.
|
|
459
|
+
const s = await sessions.openAsync(String(file || ""), { agent: wants === "background" ? "background" : undefined });
|
|
415
460
|
if (wants === "background") agent.ensure(s.key); // a person opened it from a terminal: the background agent works on it
|
|
416
461
|
const nonce = auth.newNonce();
|
|
417
462
|
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
463
|
}
|
|
419
464
|
if (req.method === "GET" && p === "/cli/status") {
|
|
420
|
-
return sendJson(res, 200, cliStatus(sessions, sessions
|
|
465
|
+
return sendJson(res, 200, cliStatus(sessions, sessionFor(sessions, url.searchParams.get("file") || "", url.searchParams.get("holder"))));
|
|
466
|
+
}
|
|
467
|
+
if (req.method === "GET" && p === "/cli/open-work") {
|
|
468
|
+
// what `onionskin update` would cut off by restarting the server: counts only, no file names
|
|
469
|
+
return sendJson(res, 200, openWork(sessions, { tabs: tabsOf, seen: (key) => tabSeen.get(key) ?? null }));
|
|
421
470
|
}
|
|
422
471
|
if (req.method === "GET" && p === "/cli/batch-file") {
|
|
423
|
-
// which file a delivered batch belongs to (for `reply --lease <ticket>` without a file)
|
|
424
|
-
|
|
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 });
|
|
472
|
+
// which file a delivered batch belongs to (for `reply --lease <ticket>` without a file): only for the whole ticket (batch id and token)
|
|
473
|
+
return sendJson(res, 200, { file: batchFile(sessions, claimOfQuery(url.searchParams)) });
|
|
427
474
|
}
|
|
428
475
|
if (req.method === "GET" && p === "/cli/batch") {
|
|
429
|
-
// the batch delivered to the agent (anchors for `apply`);
|
|
430
|
-
const
|
|
431
|
-
return sendJson(res, 200,
|
|
476
|
+
// the batch delivered to the agent (anchors for `apply`); with a claim, only for the lease holder (src/local.js cliBatch)
|
|
477
|
+
const claim = claimOfQuery(url.searchParams);
|
|
478
|
+
return sendJson(res, 200, cliBatch(sessions, sessionFor(sessions, url.searchParams.get("file") || "", claim.holder), claim));
|
|
432
479
|
}
|
|
433
480
|
if (req.method === "POST" && p === "/cli/agent-write") {
|
|
434
481
|
const body = await readJson(req);
|
|
435
|
-
const s = sessions
|
|
482
|
+
const s = sessionFor(sessions, String(body.file || ""), body.holder);
|
|
436
483
|
const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
|
|
437
484
|
// `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.
|
|
485
|
+
return sendJson(res, 200, await sessions.agentWriteChecked(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
486
|
}
|
|
440
487
|
if (req.method === "POST" && p === "/cli/commit") {
|
|
441
488
|
// 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
489
|
// Without base_sha256 the base is the hash this server last wrote or delivered for the batch.
|
|
443
490
|
const body = await readJson(req);
|
|
444
|
-
const s = sessions
|
|
491
|
+
const s = sessionFor(sessions, String(body.file || ""), body.holder);
|
|
445
492
|
const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
|
|
446
493
|
const baseSha = typeof body.base_sha256 === "string" && body.base_sha256 ? body.base_sha256 : null;
|
|
447
|
-
return sendJson(res, 200, sessions.
|
|
494
|
+
return sendJson(res, 200, await sessions.agentWriteChecked(s.key, { baseSha, bytes, label: "before agent commit", ...leaseOf(body) }));
|
|
448
495
|
}
|
|
449
496
|
if (req.method === "POST" && p === "/cli/apply-report") {
|
|
450
497
|
const body = await readJson(req);
|
|
451
|
-
const s = sessions
|
|
498
|
+
const s = sessionFor(sessions, String(body.file || ""), body.holder);
|
|
452
499
|
return sendJson(res, 200, sessions.noteApply(s.key, {
|
|
453
500
|
...leaseOf(body), run: typeof body.run === "string" ? body.run : "",
|
|
454
501
|
problems: Array.isArray(body.problems) ? body.problems : [], applied: Array.isArray(body.applied) ? body.applied.filter((k) => typeof k === "string") : [],
|
|
@@ -456,12 +503,14 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
456
503
|
}
|
|
457
504
|
if (req.method === "GET" && p === "/cli/poll") {
|
|
458
505
|
const f = url.searchParams.get("file") || "";
|
|
506
|
+
checkBinding(f, url.searchParams.get("holder")); // before the file is looked up or opened
|
|
459
507
|
let s;
|
|
460
508
|
try {
|
|
461
509
|
s = sessions.byFile(f);
|
|
462
510
|
} catch {
|
|
463
|
-
s = sessions.
|
|
511
|
+
s = await sessions.openAsync(f); // a poller may start before (or after a server restart without) `open`
|
|
464
512
|
}
|
|
513
|
+
await sessions.outlineAhead(s.key, url.searchParams.get("holder") || "agent"); // the outline a delivery carries, built off this thread
|
|
465
514
|
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
515
|
res.on("close", () => handle.cancel());
|
|
467
516
|
const out = await handle.promise;
|
|
@@ -470,7 +519,7 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
470
519
|
}
|
|
471
520
|
if (req.method === "POST" && p === "/cli/done") {
|
|
472
521
|
const body = await readJson(req);
|
|
473
|
-
const s = sessions
|
|
522
|
+
const s = sessionFor(sessions, String(body.file || ""), body.holder);
|
|
474
523
|
return sendJson(res, 200, await finishRun(s.key, body.results, leaseOf(body)));
|
|
475
524
|
}
|
|
476
525
|
if (req.method === "POST" && p === "/cli/stop") {
|
|
@@ -533,6 +582,7 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
533
582
|
await new Promise((resolve, reject) => (unix.once("error", reject), unix.listen(sock, resolve)));
|
|
534
583
|
fs.chmodSync(sock, 0o600);
|
|
535
584
|
writeStateFile(serverRecordPath(), JSON.stringify({ pid: process.pid, port: tcpPort, protocol: SERVER_PROTOCOL }));
|
|
585
|
+
warmAnchors(); // the anchor worker starts now, so the first Send does not wait for it
|
|
536
586
|
});
|
|
537
587
|
|
|
538
588
|
const idleMs = Number(env("IDLE_MS", 2 * 60 * 60 * 1000));
|
|
@@ -547,6 +597,7 @@ export async function startServer({ port = 0 } = {}) {
|
|
|
547
597
|
|
|
548
598
|
function shutdown() {
|
|
549
599
|
clearInterval(timer);
|
|
600
|
+
watcher.close();
|
|
550
601
|
for (const k of [...late.keys()]) cancelLate(k, "the server stopped"); // no soffice outlives the server
|
|
551
602
|
stopRewriter(); // nor the local rewriter's worker
|
|
552
603
|
stopVoice(); // nor a learning worker
|