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
package/src/suggest.js ADDED
@@ -0,0 +1,261 @@
1
+ // Quick fixes (idea 10 option A): the small model on this Mac suggests a rewrite of ONE selected sentence (Shorter, Friendlier, Fix
2
+ // grammar), the person sees both sentences, and only a tap on "Use this" writes it. This file is the server half: the suggestion itself
3
+ // (POST /api/suggest), "Use this" (POST /api/suggest/use, which hands over to Sessions.useSuggestion), warming the model
4
+ // (POST /api/suggest/warm), the status for the page's bootstrap, and the one-button setup (GET|POST /api/setup-rewriter).
5
+ //
6
+ // The trust argument: the selected sentence is document text and the rewrite is model output, both untrusted. Asking writes nothing and
7
+ // changes no session state: the suggestion is kept in this process's memory only (at most 3 per file, for 10 minutes, never in the session
8
+ // file), and the page gets it to show as plain text. A Use names the suggestion by its id; the text written is the stored one, through the
9
+ // local runner's ordinary checked write (src/local.js), never text the page sends back. "Ask the agent instead" is an ordinary Send of the
10
+ // stamp's words: the suggestion never enters a batch.
11
+ //
12
+ // Ceilings: one suggestion at a time per file here, and one at a time for the whole server in the worker (src/rewriter.js answers "busy").
13
+ // The store is lost with the server (a reload after a restart shows no card; the stamp is tapped again).
14
+ import crypto from "node:crypto";
15
+ import { buildDurable, readUnits } from "./anchors.js";
16
+ import { diffText } from "./diff.js";
17
+ import { checkRegularOwned, readDoc } from "./fsutil.js";
18
+ import { REWRITE_TASKS, checkRewrite, inputQualifies } from "./rewrite-check.js";
19
+ import { APPLE_STATES, appleHelperState, appleStateLine, getRewriter, rewriterStatus } from "./rewriter.js";
20
+ import { setupRewriter } from "./rewriter-setup.js";
21
+ import { untrusted } from "./sanitize.js";
22
+ import { HttpError } from "./session.js";
23
+ import { sentenceSpan } from "./sentence-span.js";
24
+ import { placeOf } from "./suggest-reply.js";
25
+ import { normText } from "./text.js";
26
+
27
+ export { USE_NOTE, USE_SUMMARY, buildUseReply, changedMiddle, placeOf } from "./suggest-reply.js";
28
+
29
+ // The stamps that ask the small model first: the jobs it is measurably good at, and nothing else (the owner, 2026-11-12: "only use them for what
30
+ // they're best at"). Today that is Fix grammar. Every other stamp goes to the agent. Widen this list per measured capability; the page's copy
31
+ // (web/src/stamps.js) must match, and test/suggest-view.test.js checks that it does.
32
+ export const SUGGEST_TASKS = ["grammar"];
33
+ // The tasks the page and this server accept: SUGGEST_TASKS, and with the test stand-in back end (src/rewriter.js, backend "stub") all three. A shorter
34
+ // shipped list would be honored end to end (the page names and asks only what is on it; /api/suggest refuses the rest). The page is told this list in the bootstrap (suggestStatus) and has no copy of its own.
35
+ const tasksNow = () => (rewriterStatus().backend === "stub" ? REWRITE_TASKS : SUGGEST_TASKS);
36
+ export const SUGGEST_FORMATS = ["docx", "pptx"];
37
+ const KEEP = 3; // suggestions kept per file
38
+ const KEEP_MS = 10 * 60_000;
39
+ const ASK_MS = 4000; // the model's time limit for one sentence
40
+ const WARM_MS = 8000; // how long a cold model is waited for
41
+ const WARM_RETRY_MS = 250;
42
+ const MAX_QUOTE = 300;
43
+ const CONTEXT = 200; // characters of text kept either side of the sentence in an anchor
44
+ // the line for a suggestion withheld because it changed quoted text (checked before the fact codes: a quoted name is also a name)
45
+ export const QUOTED_LINE = "No suggestion here: the change is inside quoted text.";
46
+ // reasons of the suggest check that mean the writing helper changed a fact (the page says so in those words)
47
+ const FACT_CODES = new Set(["number_changed", "unit_changed", "date_word_changed", "range_changed", "code_changed", "name_changed", "link_changed"]);
48
+
49
+ // ---- the in-memory store ---------------------------------------------------------------------------------------------------
50
+ const stores = new WeakMap(); // Sessions -> Map(session key -> [record], oldest first)
51
+ const asking = new WeakMap(); // Sessions -> Set(session key): one suggestion in flight per file
52
+
53
+ function recordsOf(sessions, key) {
54
+ let m = stores.get(sessions);
55
+ if (!m) stores.set(sessions, (m = new Map()));
56
+ const now = sessions.now();
57
+ const live = (m.get(key) || []).filter((r) => now - r.at < KEEP_MS);
58
+ if (live.length) m.set(key, live);
59
+ else m.delete(key);
60
+ return live;
61
+ }
62
+
63
+ /** Keeps `record` for `key`; the oldest goes when there are more than KEEP. */
64
+ export function keepSuggestion(sessions, key, record) {
65
+ const list = [...recordsOf(sessions, key), record].slice(-KEEP);
66
+ stores.get(sessions).set(key, list);
67
+ }
68
+
69
+ /** The live record with this id for this file, or null (unknown, expired, pushed out, or another file's). */
70
+ export function findSuggestion(sessions, key, id) {
71
+ if (typeof id !== "string" || !id) return null;
72
+ return recordsOf(sessions, key).find((r) => r.id === id) || null;
73
+ }
74
+
75
+ export function forgetSuggestion(sessions, key, id) {
76
+ const m = stores.get(sessions);
77
+ const list = m?.get(key);
78
+ if (list) m.set(key, list.filter((r) => r.id !== id));
79
+ }
80
+
81
+ // ---- POST /api/suggest -----------------------------------------------------------------------------------------------------
82
+ /** The page's selection: a location object and a quote (plain strings only); everything else it may carry is dropped. */
83
+ function cleanAnchor(a) {
84
+ if (!a || typeof a !== "object" || !a.location || typeof a.location !== "object" || Array.isArray(a.location)) return null;
85
+ if (typeof a.quote !== "string" || !a.quote || a.quote.length > MAX_QUOTE) return null;
86
+ const str = (v) => (typeof v === "string" ? v.slice(0, 200) : "");
87
+ return { location: a.location, quote: a.quote, prefix: str(a.prefix), suffix: str(a.suffix) };
88
+ }
89
+
90
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
91
+
92
+ /**
93
+ * One suggestion for the selected sentence. body: {task, anchor: {location, quote, prefix?, suffix?}, wholeParagraph?}. Never writes and
94
+ * never changes the session. Resolves to one of:
95
+ * {state: "shown", id, before, after, diff, warnings, untrusted: true, anchor} diff: diffText(before, after) ops; warnings: suggest-check warnings;
96
+ * anchor: {location, quote, prefix, suffix} of the whole sentence (the selection, completed as src/sentence-span.js says): the page puts its comment there
97
+ * {state: "unchanged"} | {state: "withheld", why: "facts" | "not_shorter" | "not_friendlier" | "not_clean" | "quoted", line?} | {state: "addresses_ai"} | {state: "protected"}
98
+ * {state: "not_eligible", why} | {state: "off", why?, line?} | {state: "busy"} | {state: "timeout"} | {state: "warming"} | {state: "failed", why?: "refused"}
99
+ * "withheld" with why "quoted" carries the line to show (QUOTED_LINE); "off" with a why (Apple's model is not available now: a key of
100
+ * APPLE_STATES) carries its line, and the tap goes to the agent; "failed" with why "refused" is the model declining the sentence.
101
+ * `opts` (tests): rewriter() -> the rewriter or null, warmMs, retryMs.
102
+ */
103
+ export async function suggest(sessions, key, body, opts = {}) {
104
+ const { rewriter = getRewriter, warmMs = WARM_MS, retryMs = WARM_RETRY_MS } = opts;
105
+ const s = sessions.get(key);
106
+ if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
107
+ const task = body?.task;
108
+ if (!tasksNow().includes(task)) throw new HttpError(400, `task must be one of ${tasksNow().join(", ")}`, { code: "bad_task" });
109
+ const no = (why) => ({ state: "not_eligible", why });
110
+ if (!SUGGEST_FORMATS.includes(s.format)) return no("format");
111
+ if (s.delivered || s.pending) return no("working");
112
+ const anchor = cleanAnchor(body.anchor);
113
+ if (!anchor) return no("selection");
114
+ const picked = anchor.quote; // exactly what the page selected; the file is searched for this
115
+ if (untrusted(picked.trim(), MAX_QUOTE + 100) !== picked.trim()) return no("selection"); // control, bidi or zero-width characters, a line break
116
+
117
+ // where it is in the file on disk now, read only. A sentence the page changed and has not sent yet is not found: that is checked again,
118
+ // on the bytes actually written, at Use
119
+ let place = null;
120
+ try {
121
+ checkRegularOwned(s.file);
122
+ const units = readUnits(s.format, readDoc(s.file));
123
+ place = placeOf(units, buildDurable(units, anchor), picked);
124
+ } catch {
125
+ place = null;
126
+ }
127
+ if (place?.code === "protected") return { state: "protected" };
128
+ if (place?.code === "not_eligible") return no("place");
129
+ // The sentence is read from the paragraph itself, never from the page: a selection that is a sentence minus its end mark, closing quote or
130
+ // bracket, or with spaces at its edges, means the whole sentence (src/sentence-span.js). The anchor Use will write on is the sentence's.
131
+ let quote = picked;
132
+ let from = anchor;
133
+ if (place?.ok) {
134
+ const span = Number.isInteger(place.start) && Number.isInteger(place.end) ? sentenceSpan(place.text, place.start, place.end) : null;
135
+ if (span) {
136
+ quote = place.text.slice(span.start, span.end);
137
+ if (untrusted(quote, MAX_QUOTE + 100) !== quote || quote.length > MAX_QUOTE + 10) return no("selection");
138
+ const loc = Number.isInteger(anchor.location.start) && Number.isInteger(anchor.location.end) ? { ...anchor.location, start: span.start, end: span.end } : anchor.location;
139
+ from = { location: loc, quote, prefix: place.text.slice(Math.max(0, span.start - CONTEXT), span.start), suffix: place.text.slice(span.end, span.end + CONTEXT) };
140
+ }
141
+ } else if (untrusted(picked, MAX_QUOTE + 100) !== picked) return no("selection"); // edge spaces: nothing to read the sentence from
142
+ const wholeParagraph = place?.ok ? normText(place.text) === normText(quote) : body.wholeParagraph === true;
143
+ const q = inputQualifies(quote, { wholeParagraph });
144
+ if (!q.ok) return q.reasons.some((r) => r.code === "input_addresses_ai") ? { state: "addresses_ai" } : no("sentence");
145
+
146
+ const rw = rewriter();
147
+ if (!rw) return { state: "off" };
148
+ let mine = asking.get(sessions);
149
+ if (!mine) asking.set(sessions, (mine = new Set()));
150
+ if (mine.has(key)) return { state: "busy" };
151
+ mine.add(key);
152
+ try {
153
+ const until = Date.now() + warmMs;
154
+ let r;
155
+ for (;;) {
156
+ r = await rw.rewrite(task, quote, { timeoutMs: ASK_MS });
157
+ if (r?.code !== "warming_up") break;
158
+ if (Date.now() >= until) return { state: "warming" };
159
+ await sleep(retryMs);
160
+ }
161
+ if (!r?.ok) {
162
+ if (Object.hasOwn(APPLE_STATES, r?.code)) return { state: "off", why: r.code, line: appleStateLine(r.code) };
163
+ if (r?.code === "refused") return { state: "failed", why: "refused" };
164
+ return { state: r?.code === "timeout" ? "timeout" : r?.code === "busy" ? "busy" : "failed" };
165
+ }
166
+ if (s.closed || s.delivered || s.pending) return no("working"); // an agent run started while the model worked
167
+ const c = checkRewrite(task, quote, r.text, { mode: "suggest" });
168
+ if (c.verdict === "unchanged") return { state: "unchanged" };
169
+ if (c.verdict !== "show" && c.verdict !== "warn") {
170
+ const codes = (c.reasons || []).map((x) => x.code);
171
+ if (codes.includes("quoted_text_changed")) return { state: "withheld", why: "quoted", line: QUOTED_LINE };
172
+ return { state: "withheld", why: codes.some((x) => FACT_CODES.has(x)) ? "facts" : task === "shorter" && codes.includes("not_shorter") ? "not_shorter" : task === "friendlier" && codes.includes("formal_only") ? "not_friendlier" : "not_clean" };
173
+ }
174
+ const warnings = (c.warnings || []).map((w) => ({
175
+ code: w.code,
176
+ words: (w.words || []).map((x) => untrusted(x, 30)),
177
+ dropped: (w.dropped || []).map((x) => untrusted(x, 30)),
178
+ added: (w.added || []).map((x) => untrusted(x, 30)),
179
+ ...(w.now ? { now: w.now === "question" ? "question" : "statement" } : {}),
180
+ }));
181
+ const id = `q_${crypto.randomBytes(8).toString("hex")}`;
182
+ keepSuggestion(sessions, key, { id, task, quote, location: from.location, before: quote, after: c.text, warnings, at: sessions.now() });
183
+ const d = diffText(quote, c.text, { context: Infinity });
184
+ return { state: "shown", id, before: quote, after: c.text, diff: d.ops || [], warnings, untrusted: true, anchor: from };
185
+ } finally {
186
+ mine.delete(key);
187
+ }
188
+ }
189
+
190
+ // ---- POST /api/suggest/use ---------------------------------------------------------------------------------------------------
191
+ /**
192
+ * body: {id, commentId, comments, note?, baseSha, bytesB64?, edits?}. The record is looked up here by id; Sessions.useSuggestion checks the
193
+ * rest and starts the local run. The record is used once: it is forgotten as soon as the run has started.
194
+ */
195
+ export function useSuggestion(sessions, key, body) {
196
+ const id = typeof body?.id === "string" ? body.id : "";
197
+ const record = findSuggestion(sessions, key, id);
198
+ if (!record) throw new HttpError(409, "This suggestion is out of date. Tap the stamp again.", { code: "expired" });
199
+ const bytes = typeof body.bytesB64 === "string" ? Buffer.from(body.bytesB64, "base64") : null;
200
+ sessions.useSuggestion(key, { suggestion: record, commentId: body.commentId, comments: body.comments, note: body.note, baseSha: String(body.baseSha || ""), bytes, edits: body.edits });
201
+ forgetSuggestion(sessions, key, id);
202
+ return sessions.get(key);
203
+ }
204
+
205
+ // ---- warm-up and status ------------------------------------------------------------------------------------------------------
206
+ /** POST /api/suggest/warm: starts a cold model in the background. Carries no text. */
207
+ export function warmSuggest() {
208
+ const rw = getRewriter();
209
+ rw?.warm();
210
+ return { on: !!rw };
211
+ }
212
+
213
+ /**
214
+ * The bootstrap's `suggest` field: {on, name, tasks, unavailable}. `unavailable` is {code, line} when Apple's model gives no instant suggestion on
215
+ * this Mac (a key of APPLE_STATES in src/rewriter.js, and its plain line; Fix grammar then goes to the agent), else null. Decided without starting
216
+ * the helper: from the macOS version, the processor, the helper's hash and its quarantine mark, then what the last setup or start was told.
217
+ * `o` (tests) goes to appleHelperState.
218
+ */
219
+ export function suggestStatus(o = {}) {
220
+ const r = rewriterStatus();
221
+ const why = (code) => (Object.hasOwn(APPLE_STATES, code === "helper_changed" ? "helper_missing" : code) ? { code: code === "helper_changed" ? "helper_missing" : code, line: appleStateLine(code) } : null);
222
+ if (r.on) return { on: true, name: r.name, tasks: tasksNow(), unavailable: r.backend === "apple" ? why(getRewriter()?.startCode?.()) : null };
223
+ const h = appleHelperState(o);
224
+ return { on: false, name: null, tasks: tasksNow(), unavailable: why(h.ok ? job?.outcome?.reason : h.reason) ?? why(r.reason) };
225
+ }
226
+
227
+ // ---- the setup, for the browser (GET|POST /api/setup-rewriter) -----------------------------------------------------------------
228
+ // The same pattern as the deeper checks' setup (src/checker.js startSetupChecker): one job for the whole server, a second start joins it,
229
+ // the outcome is kept until the next start so the page can poll. Always Apple's built-in model, from the helper this app copy ships (nothing is
230
+ // downloaded). Nothing starts unless the person pressed the setup button.
231
+ let job = null; // {promise} while running; {outcome: {reason, detail}} after a failure; null when idle or after a success
232
+
233
+ const reasonOf = (err) => (Object.hasOwn(APPLE_STATES, err?.reason) ? err.reason : "failed");
234
+
235
+ /** Starts the setup or joins the running one. `deps` (tests) go to setupRewriter: {probe, probeHelper, apple, clearQuarantine}. */
236
+ export function startSetupRewriter(deps = {}) {
237
+ if (job?.promise) return job.promise;
238
+ const mine = {};
239
+ mine.promise = setupRewriter({ model: "apple" }, () => {}, deps).then(
240
+ (r) => ((job = null), r),
241
+ (err) => {
242
+ job = { outcome: { reason: reasonOf(err), detail: untrusted(err?.message || "", 2000) } };
243
+ throw err;
244
+ },
245
+ );
246
+ mine.promise.catch(() => {}); // a click nobody awaits must not become an unhandled rejection
247
+ job = mine;
248
+ return mine.promise;
249
+ }
250
+
251
+ /** {state: "off" | "working" | "on" | "failed", reason?: a key of APPLE_STATES | "failed", detail?}. */
252
+ export function setupRewriterStatus() {
253
+ if (job?.promise) return { state: "working" };
254
+ if (rewriterStatus().on) return { state: "on" };
255
+ if (job?.outcome) return { state: "failed", ...job.outcome };
256
+ return { state: "off" };
257
+ }
258
+
259
+ export const resetSetupRewriterForTests = () => {
260
+ job = null;
261
+ };
package/src/text.js ADDED
@@ -0,0 +1,31 @@
1
+ import crypto from "node:crypto";
2
+
3
+ /** NFC, whitespace collapsed, trimmed: the text every anchor, hash and comparison is built on. Null and undefined read as "". */
4
+ export const normText = (s) => String(s ?? "").normalize("NFC").replace(/\s+/g, " ").trim();
5
+
6
+ const sha16 = (s) => crypto.createHash("sha256").update(s).digest("hex").slice(0, 16);
7
+ /** 16-hex hash of the normalized text (DOCX paragraphs, PPTX shapes) */
8
+ export const textHash = (s) => sha16(normText(s));
9
+ /** 16-hex hash of the NFC text with whitespace kept (XLSX cells and regions compare character for character) */
10
+ export const exactHash = (s) => sha16(String(s ?? "").normalize("NFC"));
11
+
12
+ function bigrams(s) {
13
+ const m = new Map();
14
+ for (let i = 0; i < s.length - 1; i++) {
15
+ const g = s.slice(i, i + 2);
16
+ m.set(g, (m.get(g) || 0) + 1);
17
+ }
18
+ return m;
19
+ }
20
+ /** Dice coefficient over character bigrams of the normalized, lowercased strings (0..1). */
21
+ export function similarity(a, b) {
22
+ a = normText(a).toLowerCase();
23
+ b = normText(b).toLowerCase();
24
+ if (!a && !b) return 1;
25
+ if (a.length < 2 || b.length < 2) return a === b ? 1 : 0;
26
+ const A = bigrams(a);
27
+ const B = bigrams(b);
28
+ let hit = 0;
29
+ for (const [g, c] of A) hit += Math.min(c, B.get(g) || 0);
30
+ return (2 * hit) / (a.length - 1 + b.length - 1);
31
+ }
@@ -0,0 +1,43 @@
1
+ // What a person reads about the tracked-changes copy (M4 Idea 16). Plain words only; no document or agent text is ever put in here.
2
+
3
+ export const TRACKED = {
4
+ locked: "Wait until the agent finishes, then try again.",
5
+ not_editor: "Save the copy from the tab that holds the file.",
6
+ not_current: "This file has changed since this run. Use What changed to save every change instead.",
7
+ has_revisions: "This file already has tracked changes. Accept or reject them in Word first, then try again.",
8
+ outside: "Someone changed this file outside Onionskin during this time, so these changes cannot all be marked as Onionskin's.",
9
+ check_failed: "The copy did not pass Onionskin's check, so it was not saved. Your file is fine.",
10
+ timeout: "This file is too large to make the copy right now. Nothing was saved.",
11
+ busy: "A copy is already being made.",
12
+ folder: "Onionskin could not save in that folder. Download the copy instead.",
13
+ folder_too_large: "Onionskin could not save in that folder. Move the file to a folder you can save in, then try again.",
14
+ not_docx: "Only Word files can be saved with tracked changes.",
15
+ not_agent: "Only an agent run can be saved with tracked changes.",
16
+ no_changes: "There are no text changes to mark here. Nothing was saved.",
17
+ nothing: "There is nothing in History to save yet.",
18
+ not_set_up: "Onionskin checked the copy. The extra checks are not set up on this Mac.",
19
+ moves: "Moved text shows as removed in one place and added in the other.",
20
+ };
21
+
22
+ /** Refusal codes from src/redline.js (and the span rules) -> the phrase in "Some of these changes cannot be shown ...: {list}." */
23
+ export const UNSUPPORTED = {
24
+ notes_part: "a change in a header, footer or footnote",
25
+ other_part: "a change to the file's styles or settings",
26
+ table_shape: "a change to a table's rows or columns",
27
+ table_para: "a paragraph added or removed inside a table",
28
+ protected: "a change in a paragraph with a picture, link or field",
29
+ at_end: "a paragraph added or removed at the very end",
30
+ next_to_table: "a paragraph added or removed right next to a table",
31
+ section: "a section break",
32
+ sdt: "a change in a text box or content control",
33
+ nested_table: "a change in a table inside another table",
34
+ pruned: "an older change that is no longer stored",
35
+ bad_char: "a character Word cannot store",
36
+ id_overflow: "a file with too many numbered items",
37
+ unreadable: "a part of the file Onionskin cannot read yet",
38
+ };
39
+
40
+ const join = (list) => (list.length < 2 ? list.join("") : `${list.slice(0, -1).join(", ")} and ${list[list.length - 1]}`);
41
+ export const unsupportedText = (codes) => `Some of these changes cannot be shown as tracked changes yet: ${join([...new Set(codes.map((c) => UNSUPPORTED[c] || UNSUPPORTED.unreadable))])}. Nothing was saved.`;
42
+ export const savedText = (name) => `Saved “${name}” in the same folder as your file.`;
43
+ export const formattingText = (n) => (n === 1 ? "1 formatting change is in the copy but not marked, so Reject All in Word keeps it." : `${n} formatting changes are in the copy but not marked, so Reject All in Word keeps them.`);
@@ -0,0 +1,182 @@
1
+ // The voice guide's fixed shape, stage 2 ("Build my voice guide"). Pure: nothing here reads or writes a file.
2
+ //
3
+ // An agent drafts a guide; the person reads every section and saves what they approve (src/voice-store.js keeps it). This module holds
4
+ // the twelve fixed sections and their caps, the strict check of an agent's draft (cleanVoiceDraft: it refuses, it never cuts or
5
+ // repairs), and the parsing of a saved guide into its sections (parseGuide, shortForm).
6
+ //
7
+ // A draft is agent text: untrusted until the person approves it word for word. The check makes sure that what reaches the page is plain
8
+ // text in the fixed layout, with nothing hidden, no link and no markup, so that every word the person approves is a word they could see.
9
+ // Ceiling: it cannot make a persuasive sentence harmless. That is what the person's review is for.
10
+
11
+ /** The twelve sections, in order. `title` is the exact `## ` heading; `max` the cap of its body, in characters (code points). */
12
+ export const SECTIONS = [
13
+ { id: "short", title: "In short", max: 700 },
14
+ { id: "identity", title: "Who I am as a writer", max: 500 },
15
+ { id: "priorities", title: "What matters most", max: 600 },
16
+ { id: "tone", title: "Tone by situation", max: 700 },
17
+ { id: "rhythm", title: "Sentences and rhythm", max: 450 },
18
+ { id: "words", title: "Words I use and avoid", max: 600 },
19
+ { id: "humor", title: "Humor", max: 400 },
20
+ { id: "structure", title: "How I build a piece", max: 500 },
21
+ { id: "punctuation", title: "Punctuation and formatting", max: 400 },
22
+ { id: "rules", title: "Always and never", max: 600 },
23
+ { id: "examples", title: "Before and after", max: 900 },
24
+ { id: "calibration", title: "Sounds like me, does not sound like me", max: 600 },
25
+ ];
26
+ /** The whole saved guide, headings included, in characters (code points). */
27
+ export const GUIDE_MAX = 7000;
28
+ /** The "In short" section, the only part sent when the person picks "Short form only". */
29
+ export const SHORT_MAX = SECTIONS[0].max;
30
+ /** The largest draft read at all, in UTF-8 bytes. Checked before anything else, so a huge body costs nothing. */
31
+ export const DRAFT_MAX_BYTES = 32 * 1024;
32
+ export const DRAFT_MAX_LINES = 160;
33
+ export const LINE_MAX = 600;
34
+ /** The one heading allowed before the first section. */
35
+ export const DRAFT_TITLE = "# My voice guide";
36
+
37
+ /** What the agent is told when a draft is refused (agent-facing; the person sees the page's own wording). */
38
+ export const VOICE_FIX =
39
+ "Fill the template in reference/voice-guide.md exactly: the twelve ## headings in order, each once, nothing before the first except an optional \"# My voice guide\" line, and no other line starting with #. Keep each section within its cap, the whole draft at most 7,000 characters and 160 lines, and every line at most 600 characters. At most two blank lines in a row. No links, web or email addresses, @, HTML, Markdown links or pictures, code fences, tabs or hidden characters. Nothing is cut to fit: correct the draft and reply again.";
40
+
41
+ const cps = (s) => [...s].length;
42
+ const HEADING_LINE = new Map(SECTIONS.map((s, i) => [`## ${s.title}`, i]));
43
+
44
+ // Hidden characters, refused (never stripped): C0 except \n (tabs too), DEL, C1, the bidi marks and controls, the line and paragraph
45
+ // separators, and every default-ignorable, format, private-use, unassigned or surrogate code point. A lone ZWNJ or ZWJ (U+200C, U+200D)
46
+ // is allowed: Persian and several Indic scripts need it to spell a word, and emoji sequences use it. Two or more in a row spell nothing.
47
+ // Built with new RegExp from escapes so no raw control character lives in this file.
48
+ const HIDDEN = new RegExp(
49
+ "[\\u0000-\\u0009\\u000B-\\u001F\\u007F-\\u009F\\u061C\\u200E\\u200F\\u202A-\\u202E\\u2066-\\u2069\\u2028\\u2029]|" +
50
+ "(?![\\u200C\\u200D])[\\p{Default_Ignorable_Code_Point}\\p{Cf}\\p{Co}\\p{Cn}\\p{Cs}]|[\\u200C\\u200D]{2,}",
51
+ "u"
52
+ );
53
+ // Links and markup, checked on the NFKC form so a fullwidth or compatibility look-alike (a fullwidth @ or colon and slashes, a small less-than sign) counts as the real character.
54
+ // Every pattern is a literal or starts at one fixed character, so the check stays linear on a 32 KB draft.
55
+ const MARKUP = [
56
+ /:\/\//u,
57
+ /www\./iu,
58
+ /@/u,
59
+ /&(?:#\d+|#x[0-9a-f]+|[a-z][a-z0-9]*);/iu,
60
+ /mailto:/iu,
61
+ /\]\(/u,
62
+ /!\[/u,
63
+ /<[\p{L}/!?]/u,
64
+ /```/u,
65
+ ];
66
+ const VS = /[\uFE0E\uFE0F]/gu;
67
+ /** A line that draws nothing: spaces of any kind and the braille blank (U+2800, which is neither a space nor hidden). */
68
+ const BLANK = /^[\s\u2800]*$/u;
69
+ const BLANK_RUN_MAX = 2;
70
+ const TLD = /^\p{L}{2,}$/u;
71
+ const NAME_CHAR = /[\p{L}\p{N}-]/u;
72
+ const PORT = /:\d{1,5}$/u;
73
+ /**
74
+ * A domain with a path, at least as strict as the examples' rule (LINK in src/voice.js, a backtracking pattern that is fine on 400
75
+ * characters but not on 32 KB): any part of a word before one of its slashes that ends in a name character, a dot and two or more letters,
76
+ * an optional port after it ("evil.example/x", "//evil.example/login", "x/evil.example/", "evil.example:8080/x"). "4.5/5", "and/or" and
77
+ * "e.g. 3/4" pass. One pass over each word.
78
+ */
79
+ function domainPath(s) {
80
+ for (const word of s.split(/\s+/u)) {
81
+ const parts = word.split("/");
82
+ for (let i = 0; i < parts.length - 1; i++) {
83
+ const name = parts[i].replace(PORT, "");
84
+ const dot = name.lastIndexOf(".");
85
+ if (dot > 0 && NAME_CHAR.test(name[dot - 1]) && TLD.test(name.slice(dot + 1))) return true;
86
+ }
87
+ }
88
+ return false;
89
+ }
90
+
91
+ const refuse = (code, reason) => ({ code, reason });
92
+
93
+ /**
94
+ * The section bodies of `lines`, or a refusal. Every line whose left-trimmed text starts with # must be "## <a fixed title>" exactly
95
+ * (trailing spaces allowed; a fullwidth number sign counts as #); before the first section only blank lines and one optional "# My voice guide". `all` asks for all twelve,
96
+ * in order, once each; otherwise any of them in order, each at most once (a saved guide the person composed from some sections).
97
+ */
98
+ function splitSections(lines, { all }) {
99
+ const found = [];
100
+ let cur = null;
101
+ let titleSeen = false;
102
+ for (const line of lines) {
103
+ const t = line.trimEnd();
104
+ if (/^\s*#/u.test(t.normalize("NFKC"))) { // a fullwidth number sign looks like # on the page
105
+ if (!cur && !titleSeen && t === DRAFT_TITLE) {
106
+ titleSeen = true;
107
+ continue;
108
+ }
109
+ const i = HEADING_LINE.get(t);
110
+ if (i === undefined) return refuse("bad_structure", "a line starts with # but is not one of the twelve section headings");
111
+ if (found.length && i <= found[found.length - 1].index) return refuse("bad_structure", `the heading "${SECTIONS[i].title}" is out of order or repeated`);
112
+ cur = { index: i, lines: [] };
113
+ found.push(cur);
114
+ } else if (cur) cur.lines.push(line);
115
+ else if (t.trim()) return refuse("bad_structure", "there is text before the first section heading");
116
+ }
117
+ if (!found.length) return refuse("bad_structure", "there is no section heading");
118
+ if (all) {
119
+ const missing = SECTIONS.filter((_, i) => !found.some((f) => f.index === i));
120
+ if (missing.length) return refuse("bad_structure", `the heading "${missing[0].title}" is missing`);
121
+ }
122
+ return {
123
+ sections: found.map((f) => ({
124
+ id: SECTIONS[f.index].id,
125
+ title: SECTIONS[f.index].title,
126
+ body: f.lines.join("\n").replace(/^\s*\n/u, "").trimEnd(),
127
+ })),
128
+ };
129
+ }
130
+
131
+ /**
132
+ * An agent's draft of the guide: `{text, sections: [{id, title, body}]}` when it passes, `{code, reason}` when it does not. The only
133
+ * changes: CR LF and CR become LF, NFC, and the emoji presentation selectors U+FE0E and U+FE0F are removed (they only change how an
134
+ * emoji is drawn). Everything else is refused, never cut or repaired. Codes: `too_long`, `hidden_characters`, `link_or_markup`,
135
+ * `bad_structure` (also more than two blank lines in a row). A `reason` holds fixed words, section titles and numbers only, never the draft's own text.
136
+ */
137
+ export function cleanVoiceDraft(value) {
138
+ if (typeof value !== "string") return refuse("bad_structure", "the draft must be text");
139
+ // size first: a huge body is refused before any regex or normalization runs over it
140
+ if (Buffer.byteLength(value, "utf8") > DRAFT_MAX_BYTES) return refuse("too_long", `the draft is over ${DRAFT_MAX_BYTES} bytes`);
141
+ const text = value.replace(/\r\n?/gu, "\n").normalize("NFC").replace(VS, "");
142
+ if (HIDDEN.test(text)) return refuse("hidden_characters", "the draft holds a hidden or control character (tabs included)");
143
+ const nfkc = text.normalize("NFKC");
144
+ if (MARKUP.some((re) => re.test(nfkc)) || domainPath(nfkc)) return refuse("link_or_markup", "the draft holds a link, an address, @, HTML or Markdown markup, or a code fence");
145
+ const lines = text.split("\n");
146
+ // padding: a run of blank lines could push a planted line far below the part of a box the person reads first
147
+ let blank = 0;
148
+ for (const l of lines) {
149
+ blank = BLANK.test(l) ? blank + 1 : 0;
150
+ if (blank > BLANK_RUN_MAX) return refuse("bad_structure", `there are more than ${BLANK_RUN_MAX} blank lines in a row`);
151
+ }
152
+ const split = splitSections(lines, { all: true });
153
+ if (split.code) return split;
154
+ for (const s of split.sections) {
155
+ const max = SECTIONS.find((x) => x.id === s.id).max;
156
+ if (cps(s.body) > max) return refuse("too_long", `the section "${s.title}" is ${cps(s.body)} characters; at most ${max}`);
157
+ }
158
+ if (cps(text) > GUIDE_MAX) return refuse("too_long", `the draft is ${cps(text)} characters; at most ${GUIDE_MAX}`);
159
+ if (lines.length > DRAFT_MAX_LINES) return refuse("too_long", `the draft is ${lines.length} lines; at most ${DRAFT_MAX_LINES}`);
160
+ const long = lines.findIndex((l) => cps(l) > LINE_MAX);
161
+ if (long >= 0) return refuse("too_long", `line ${long + 1} is over ${LINE_MAX} characters`);
162
+ return { text, sections: split.sections };
163
+ }
164
+
165
+ /**
166
+ * A saved guide (the person's own text, already stripped by the store) split into its sections. `structured` is true when every line
167
+ * starting with # is one of the fixed headings, in order, each at most once, with nothing but "# My voice guide" before the first: a
168
+ * guide composed in the editor, possibly from some sections only. A guide imported in stage 1 is usually not structured, and then
169
+ * `sections` is empty. `short` is the "In short" body, or "".
170
+ */
171
+ export function parseGuide(text) {
172
+ if (typeof text !== "string" || !text.trim()) return { structured: false, sections: [], short: "" };
173
+ const split = splitSections(text.replace(/\r\n?/gu, "\n").split("\n"), { all: false });
174
+ if (split.code) return { structured: false, sections: [], short: "" };
175
+ return { structured: true, sections: split.sections, short: split.sections.find((s) => s.id === "short")?.body || "" };
176
+ }
177
+
178
+ /** The "In short" section alone ("## In short" and its body), or "" when the guide has none, it is empty or it is over SHORT_MAX. */
179
+ export function shortForm(text) {
180
+ const { short } = parseGuide(text);
181
+ return short && cps(short) <= SHORT_MAX ? `## ${SECTIONS[0].title}\n${short}` : "";
182
+ }