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.
Files changed (109) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/INSTALL.md +5 -1
  3. package/NOTICE +34 -0
  4. package/README.md +102 -34
  5. package/docs/contracts/batch.schema.json +27 -4
  6. package/docs/contracts/ops.schema.json +117 -5
  7. package/package.json +4 -2
  8. package/scripts/checker/check.py +24 -5
  9. package/scripts/checker/requirements.txt +482 -5
  10. package/scripts/requirements.txt +482 -5
  11. package/scripts/rewriter/apple/main.swift +1 -1
  12. package/scripts/rewriter/apple/onionskin-fm +0 -0
  13. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  14. package/skills/onionskin/SKILL.md +12 -9
  15. package/skills/onionskin/reference/batch.md +5 -3
  16. package/skills/onionskin/reference/formats.md +5 -1
  17. package/skills/onionskin/reference/ops.md +48 -4
  18. package/skills/onionskin/reference/troubleshooting.md +31 -0
  19. package/src/agent-detect.js +28 -2
  20. package/src/agent-prompt.js +12 -1
  21. package/src/agent-runner.js +110 -20
  22. package/src/anchor-work.js +154 -0
  23. package/src/anchor-worker.js +38 -0
  24. package/src/anchors.js +61 -11
  25. package/src/apply.js +119 -47
  26. package/src/batch.js +32 -8
  27. package/src/check-worker.js +8 -5
  28. package/src/checker.js +238 -33
  29. package/src/cli.js +248 -42
  30. package/src/constants.js +34 -1
  31. package/src/diff.js +3 -1
  32. package/src/doctor.js +12 -3
  33. package/src/docx.js +464 -87
  34. package/src/edit-hash.js +19 -0
  35. package/src/fsutil.js +14 -6
  36. package/src/history-worker.js +8 -8
  37. package/src/history.js +144 -31
  38. package/src/html-active.js +384 -0
  39. package/src/html-envelope.js +51 -0
  40. package/src/html-parser.js +8 -0
  41. package/src/html-render-worker.js +25 -0
  42. package/src/html-render.js +939 -0
  43. package/src/html-utf8.js +65 -0
  44. package/src/html-write-worker.js +14 -0
  45. package/src/html-write.js +380 -0
  46. package/src/html.js +1259 -0
  47. package/src/local.js +68 -4
  48. package/src/merge-package.js +1166 -0
  49. package/src/merge-pptx.js +98 -38
  50. package/src/merge-proof.js +1019 -0
  51. package/src/merge-worker.js +60 -0
  52. package/src/merge-xlsx.js +84 -29
  53. package/src/merge.js +286 -83
  54. package/src/nfc.js +46 -0
  55. package/src/notes.js +122 -0
  56. package/src/offload.js +419 -1
  57. package/src/open-worker.js +27 -0
  58. package/src/ops/common.js +2 -1
  59. package/src/ops/docx-table.js +5 -3
  60. package/src/ops/docx.js +58 -29
  61. package/src/ops/html.js +394 -0
  62. package/src/ops/pptx.js +82 -23
  63. package/src/ops/run.js +23 -3
  64. package/src/ops/runs.js +215 -13
  65. package/src/ops/schema.js +8 -1
  66. package/src/ops/xlsx.js +121 -10
  67. package/src/ops-worker.js +17 -0
  68. package/src/package.js +43 -4
  69. package/src/person.js +2 -1
  70. package/src/pptx.js +323 -23
  71. package/src/question.js +10 -3
  72. package/src/quick-plan.js +13 -6
  73. package/src/quick.js +1 -1
  74. package/src/redline-check.js +12 -2
  75. package/src/redline-worker.js +10 -14
  76. package/src/reply.js +46 -19
  77. package/src/retain.js +223 -0
  78. package/src/review.js +32 -18
  79. package/src/rewrite-check.js +2 -2
  80. package/src/rewriter.js +38 -8
  81. package/src/sanitize.js +16 -3
  82. package/src/server-entry.js +4 -3
  83. package/src/server.js +175 -62
  84. package/src/session.js +1151 -210
  85. package/src/setup.js +20 -2
  86. package/src/stop-gate.js +34 -0
  87. package/src/suggest-reply.js +42 -7
  88. package/src/suggest.js +14 -29
  89. package/src/text.js +161 -11
  90. package/src/update.js +517 -0
  91. package/src/voice-guide.js +15 -9
  92. package/src/voice-learn.js +15 -4
  93. package/src/voice-sample-worker.js +7 -6
  94. package/src/voice-store.js +11 -4
  95. package/src/voice-worker.js +4 -6
  96. package/src/voice.js +2 -1
  97. package/src/worker-host.js +23 -0
  98. package/src/xlsx.js +378 -97
  99. package/src/xml-tree.js +6 -3
  100. package/src/xml.js +82 -34
  101. package/src/zip-write.js +10 -5
  102. package/src/zip.js +73 -18
  103. package/vendor/parse5/LICENSE.txt +32 -0
  104. package/vendor/parse5/parse5.mjs +7822 -0
  105. package/vendor/parse5.lock.json +23 -0
  106. package/web/dist/assets/{index-DS-WST1E.js → index-CjsTf5gj.js} +58 -52
  107. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  108. package/web/dist/index.html +2 -2
  109. package/web/dist/assets/index-BCAEKiOG.css +0 -1
package/src/local.js CHANGED
@@ -13,6 +13,10 @@
13
13
  // Upgrade path: run applyCommand in a worker with the same in-process port.
14
14
 
15
15
  import { quickPlanner } from "./quick-plan.js";
16
+ import { NOT_YOUR_FILE, agentMayUse, isAgentHolder } from "./agent-detect.js";
17
+ import { HttpError, readChecked } from "./session.js";
18
+ import { runOpsOffThread } from "./offload.js";
19
+ import { LIMITS } from "./agent-runner.js";
16
20
 
17
21
  export const LOCAL_HOLDER = "onionskin-local";
18
22
 
@@ -37,6 +41,41 @@ export function leaseOf(body) {
37
41
  return { holder: str(body.holder), token: str(body.lease_token), batchId: str(body.batch_id), strict: true };
38
42
  }
39
43
 
44
+ /**
45
+ * The binding of an agent Onionskin started to its one file, checked before a /cli/* route looks the file up: another file, open or not,
46
+ * gets the same 403 `not_your_file`, so the refusal says nothing about it. Every other holder passes (its lease is checked where it acts).
47
+ */
48
+ export function checkBinding(file, holder) {
49
+ if (!agentMayUse(holder, file)) throw new HttpError(403, NOT_YOUR_FILE, { code: "not_your_file" });
50
+ }
51
+
52
+ /** The session a CLI call names, after the holder's binding is checked. */
53
+ export function sessionFor(sessions, file, holder) {
54
+ checkBinding(file, holder);
55
+ return sessions.byFile(String(file || ""));
56
+ }
57
+
58
+ /**
59
+ * `GET /cli/batch`: the delivered batch. With a claim (any of holder-with-token, batch_id, lease_token), and always for an agent Onionskin
60
+ * started, only for the lease holder (Sessions.deliveredBatch), checked before anything of it is returned. A call with no claim and no
61
+ * agent holder (a person's own tools, `apply --dry-run` without a ticket) gets it as before: such a caller can read the file and the state
62
+ * folder anyway; the restricted agent can only run the CLI, which always sends its holder.
63
+ */
64
+ export function cliBatch(sessions, s, { holder, token, batchId }) {
65
+ if (token || batchId || isAgentHolder(holder)) return { state: sessions.state(s), batch: sessions.deliveredBatch(s.key, { holder, token, batchId }) };
66
+ return { state: sessions.state(s), batch: s.delivered?.batch || null };
67
+ }
68
+
69
+ /** `GET /cli/batch-file`: the file of the delivery named by a full ticket (batch id and token) for this holder, or null. Never a file for a wrong token. */
70
+ export function batchFile(sessions, { holder, token, batchId }) {
71
+ if (!batchId || !token) return null;
72
+ const s = [...sessions.map.values()].find((x) => x.delivered?.batch.batch_id === batchId && x.delivered.lease.token === token);
73
+ return s && agentMayUse(holder, s.file) ? s.file : null;
74
+ }
75
+
76
+ /** The claim of a GET route's query (holder, lease_token, batch_id). */
77
+ export const claimOfQuery = (q) => leaseOf({ holder: q.get("holder"), lease_token: q.get("lease_token"), batch_id: q.get("batch_id") });
78
+
40
79
  /** `GET /cli/status`: what `apply`, `commit` and `done` read about a file's session. One function for the socket and the in-process port. */
41
80
  export function cliStatus(sessions, s) {
42
81
  const v = sessions.view(s);
@@ -47,6 +86,29 @@ export function cliStatus(sessions, s) {
47
86
  };
48
87
  }
49
88
 
89
+ // A tab gone this long no longer holds the update back: the same 20 minutes after which the background agent of a file with no tab stops
90
+ // (src/agent-runner.js LIMITS.idleMs). A forgotten session does not block updates forever.
91
+ export const TAB_RECENT_MS = LIMITS.idleMs;
92
+
93
+ /**
94
+ * `GET /cli/open-work`: the sum of cliStatus over every session (what `onionskin update` would cut off). Counts only, never file names.
95
+ * `open_sessions`: sessions not closed with a tab open now (`tabs(key)`) or seen less than TAB_RECENT_MS ago (`seen(key)`, a time). The
96
+ * page keeps typed edits to itself until Send, so `unsent_edits` cannot see them: an open tab is the only sign of them.
97
+ */
98
+ export function openWork(sessions, { tabs = () => 0, seen = () => null, now = Date.now() } = {}) {
99
+ const w = { sessions: 0, open_sessions: 0, unsent_edits: 0, draft_comments: 0, batches: 0 };
100
+ for (const s of sessions.map.values()) {
101
+ const st = cliStatus(sessions, s);
102
+ w.sessions++;
103
+ const t = seen(s.key);
104
+ if (!s.closed && (tabs(s.key) > 0 || (Number.isFinite(t) && now - t < TAB_RECENT_MS))) w.open_sessions++;
105
+ w.unsent_edits += st.unsent_edits;
106
+ w.draft_comments += st.comments.draft;
107
+ if (st.pending_batch || st.delivered_batch) w.batches++;
108
+ }
109
+ return w;
110
+ }
111
+
50
112
  /** The person reverted a local run of these words: they go to the agent. True when any comment of the batch, or its whole note, is marked. */
51
113
  export function skipsLocal(s, batch) {
52
114
  const ids = new Set((batch.instructions?.comments || []).map((c) => c.id));
@@ -60,9 +122,9 @@ export function skipsLocal(s, batch) {
60
122
  * An HttpError becomes {status, text} exactly as the socket's fail() writes it, so `apply` takes the same refusal paths.
61
123
  */
62
124
  function inProcessPort(sessions, key) {
63
- const answer = (fn) => {
125
+ const answer = async (fn) => {
64
126
  try {
65
- return { status: 200, text: JSON.stringify(fn()) };
127
+ return { status: 200, text: JSON.stringify(await fn()) };
66
128
  } catch (err) {
67
129
  if (Number.isInteger(err?.status)) return { status: err.status, text: JSON.stringify({ error: err.message, ...(err.extra || {}) }) };
68
130
  return { status: 500, text: JSON.stringify({ error: "internal error" }) };
@@ -76,16 +138,18 @@ function inProcessPort(sessions, key) {
76
138
  return {
77
139
  healthy: async () => true,
78
140
  json: (t) => JSON.parse(t),
141
+ checkBytes: (bytes, format) => readChecked(format, bytes), // apply reads the file again: within the readers' caps before the engine
142
+ runOps: runOpsOffThread, // and the engine's work happens in a worker thread with a time limit, never on the server's thread
79
143
  async req({ method, path: p }, body = {}) {
80
144
  const u = new URL(p, "http://x");
81
145
  const route = `${method} ${u.pathname}`;
82
146
  if (route === "GET /cli/status") return answer(() => cliStatus(sessions, mine(u.searchParams.get("file"))));
83
- if (route === "GET /cli/batch") return answer(() => { const s = mine(u.searchParams.get("file")); return { state: sessions.state(s), batch: s.delivered?.batch || null }; });
147
+ if (route === "GET /cli/batch") return answer(() => cliBatch(sessions, mine(u.searchParams.get("file")), claimOfQuery(u.searchParams)));
84
148
  if (route === "POST /cli/agent-write") {
85
149
  return answer(() => {
86
150
  const s = mine(body.file);
87
151
  const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
88
- return sessions.agentWrite(s.key, { baseSha: String(body.base_sha256 || ""), bytes, route: "apply", ops: Array.isArray(body.ops) ? body.ops : null, partial: body.partial === true, ...leaseOf(body) });
152
+ return 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) });
89
153
  });
90
154
  }
91
155
  if (route === "POST /cli/apply-report") {