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/xml.js ADDED
@@ -0,0 +1,127 @@
1
+ // Minimal streaming XML walker for OOXML parts. No regexes over the whole document, no DTDs.
2
+ // Names are kept as written (prefix:local); Word, python-docx and the engine all use the conventional prefixes.
3
+
4
+ const MAX_DEPTH = 256;
5
+ const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
6
+
7
+ export function decodeEntities(s) {
8
+ if (!s.includes("&")) return s;
9
+ let out = "";
10
+ let i = 0;
11
+ while (i < s.length) {
12
+ const amp = s.indexOf("&", i);
13
+ if (amp < 0) {
14
+ out += s.slice(i);
15
+ break;
16
+ }
17
+ out += s.slice(i, amp);
18
+ const semi = s.indexOf(";", amp);
19
+ if (semi < 0 || semi - amp > 10) {
20
+ out += "&";
21
+ i = amp + 1;
22
+ continue;
23
+ }
24
+ const name = s.slice(amp + 1, semi);
25
+ let rep;
26
+ if (name in ENTITIES) rep = ENTITIES[name];
27
+ else if (name[0] === "#") {
28
+ const code = name[1] === "x" || name[1] === "X" ? parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
29
+ if (Number.isInteger(code) && code > 0 && code <= 0x10ffff) rep = String.fromCodePoint(code);
30
+ }
31
+ if (rep === undefined) {
32
+ out += "&";
33
+ i = amp + 1;
34
+ } else {
35
+ out += rep;
36
+ i = semi + 1;
37
+ }
38
+ }
39
+ return out;
40
+ }
41
+
42
+ function parseTag(src, start, end) {
43
+ // src[start..end) is the inside of <...> without the angle brackets
44
+ let i = start;
45
+ let selfClosing = false;
46
+ if (src[end - 1] === "/") {
47
+ selfClosing = true;
48
+ end -= 1;
49
+ }
50
+ while (i < end && !/\s/.test(src[i])) i++;
51
+ const name = src.slice(start, i);
52
+ const attrs = {};
53
+ while (i < end) {
54
+ while (i < end && /\s/.test(src[i])) i++;
55
+ if (i >= end) break;
56
+ const eq = src.indexOf("=", i);
57
+ if (eq < 0 || eq >= end) break;
58
+ const key = src.slice(i, eq).trim();
59
+ i = eq + 1;
60
+ while (i < end && /\s/.test(src[i])) i++;
61
+ const q = src[i];
62
+ if (q !== '"' && q !== "'") break;
63
+ const close = src.indexOf(q, i + 1);
64
+ if (close < 0 || close >= end) break;
65
+ attrs[key] = decodeEntities(src.slice(i + 1, close));
66
+ i = close + 1;
67
+ }
68
+ return { name, attrs, selfClosing };
69
+ }
70
+
71
+ /** Calls onOpen(name, attrs), onText(text), onClose(name) in document order. Throws on DTDs and excess depth. */
72
+ export function walkXml(xml, { onOpen, onClose, onText }) {
73
+ let i = 0;
74
+ let depth = 0;
75
+ const n = xml.length;
76
+ while (i < n) {
77
+ const lt = xml.indexOf("<", i);
78
+ if (lt < 0) {
79
+ if (onText && i < n) onText(decodeEntities(xml.slice(i)));
80
+ break;
81
+ }
82
+ if (lt > i && onText) onText(decodeEntities(xml.slice(i, lt)));
83
+ if (xml.startsWith("<!--", lt)) {
84
+ const e = xml.indexOf("-->", lt + 4);
85
+ if (e < 0) throw new Error("unterminated comment");
86
+ i = e + 3;
87
+ } else if (xml.startsWith("<![CDATA[", lt)) {
88
+ const e = xml.indexOf("]]>", lt + 9);
89
+ if (e < 0) throw new Error("unterminated CDATA");
90
+ if (onText) onText(xml.slice(lt + 9, e));
91
+ i = e + 3;
92
+ } else if (xml.startsWith("<?", lt)) {
93
+ const e = xml.indexOf("?>", lt + 2);
94
+ if (e < 0) throw new Error("unterminated processing instruction");
95
+ i = e + 2;
96
+ } else if (xml.startsWith("<!", lt)) {
97
+ throw new Error("DTD or entity declarations are not allowed");
98
+ } else {
99
+ // find the closing '>' outside quotes
100
+ let j = lt + 1;
101
+ let q = "";
102
+ while (j < n) {
103
+ const c = xml[j];
104
+ if (q) {
105
+ if (c === q) q = "";
106
+ } else if (c === '"' || c === "'") q = c;
107
+ else if (c === ">") break;
108
+ j++;
109
+ }
110
+ if (j >= n) throw new Error("unterminated tag");
111
+ if (xml[lt + 1] === "/") {
112
+ depth--;
113
+ if (onClose) onClose(xml.slice(lt + 2, j).trim());
114
+ } else {
115
+ const tag = parseTag(xml, lt + 1, j);
116
+ if (onOpen) onOpen(tag.name, tag.attrs);
117
+ if (tag.selfClosing) {
118
+ if (onClose) onClose(tag.name);
119
+ } else {
120
+ depth++;
121
+ if (depth > MAX_DEPTH) throw new Error("XML nesting too deep");
122
+ }
123
+ }
124
+ i = j + 1;
125
+ }
126
+ }
127
+ }
@@ -0,0 +1,74 @@
1
+ import zlib from "node:zlib";
2
+ import { openZip, ZipError } from "./zip.js";
3
+
4
+ // Rewrites one entry of a ZIP held in memory and copies every other entry as it is: same order, same compressed bytes, same CRC, same
5
+ // central-directory fields (times, attributes, extra fields, comments). The local headers are written fresh from the central-directory
6
+ // values, so an input that used data descriptors (sizes after the data) comes out without them. No ZIP64, like zip.js.
7
+
8
+ const sig = (n) => {
9
+ const b = Buffer.alloc(4);
10
+ b.writeUInt32LE(n >>> 0, 0);
11
+ return b;
12
+ };
13
+
14
+ /** Returns new archive bytes where `name` holds `data` (deflated). Throws ZipError when `name` is missing or an entry is encrypted. */
15
+ export function replaceEntry(bytes, name, data) {
16
+ const zip = openZip(bytes);
17
+ const buf = zip.buf;
18
+ if (!zip.entries.has(name)) throw new ZipError(`no entry ${JSON.stringify(name)} to replace`);
19
+ const newData = Buffer.from(data);
20
+ const newComp = zlib.deflateRawSync(newData);
21
+ const newCrc = zlib.crc32(newData) >>> 0;
22
+ const parts = [];
23
+ const central = [];
24
+ let offset = 0;
25
+ for (const n of zip.names) {
26
+ const e = zip.entries.get(n);
27
+ if (e.flags & 0x1) throw new ZipError(`encrypted ZIP entry ${JSON.stringify(n)}`);
28
+ const cd = e.cdOffset;
29
+ const nameLen = buf.readUInt16LE(cd + 28);
30
+ const extraLen = buf.readUInt16LE(cd + 30);
31
+ const commentLen = buf.readUInt16LE(cd + 32);
32
+ const record = Buffer.from(buf.subarray(cd, cd + 46 + nameLen + extraLen + commentLen)); // a copy: patched below
33
+ const nameBuf = record.subarray(46, 46 + nameLen);
34
+ const mine = n === name;
35
+ const comp = mine ? newComp : zip.raw(n);
36
+ const method = mine ? 8 : e.method;
37
+ const crc = mine ? newCrc : e.crc;
38
+ const size = mine ? newData.length : e.size;
39
+ const flags = e.flags & ~0x8; // sizes are in the local header now: no data descriptor
40
+ const lh = Buffer.alloc(30);
41
+ lh.writeUInt32LE(0x04034b50, 0);
42
+ lh.writeUInt16LE(record.readUInt16LE(6), 4); // version needed
43
+ lh.writeUInt16LE(flags, 6);
44
+ lh.writeUInt16LE(method, 8);
45
+ lh.writeUInt16LE(record.readUInt16LE(12), 10); // time
46
+ lh.writeUInt16LE(record.readUInt16LE(14), 12); // date
47
+ lh.writeUInt32LE(crc, 14);
48
+ lh.writeUInt32LE(comp.length, 18);
49
+ lh.writeUInt32LE(size, 22);
50
+ lh.writeUInt16LE(nameLen, 26);
51
+ lh.writeUInt16LE(0, 28); // no local extra field
52
+ parts.push(lh, nameBuf, comp);
53
+ record.writeUInt16LE(flags, 8);
54
+ record.writeUInt16LE(method, 10);
55
+ record.writeUInt32LE(crc, 16);
56
+ record.writeUInt32LE(comp.length, 20);
57
+ record.writeUInt32LE(size, 24);
58
+ record.writeUInt32LE(offset, 42);
59
+ central.push(record);
60
+ offset += 30 + nameLen + comp.length;
61
+ }
62
+ const cd = Buffer.concat(central);
63
+ const commentLen = buf.readUInt16LE(zip.eocd + 20);
64
+ const end = Buffer.alloc(22);
65
+ sig(0x06054b50).copy(end, 0);
66
+ end.writeUInt16LE(zip.names.length, 8);
67
+ end.writeUInt16LE(zip.names.length, 10);
68
+ end.writeUInt32LE(cd.length, 12);
69
+ end.writeUInt32LE(offset, 16);
70
+ end.writeUInt16LE(commentLen, 20);
71
+ const comment = buf.subarray(zip.eocd + 22, zip.eocd + 22 + commentLen);
72
+ if (offset + cd.length > 0xfffffffe) throw new ZipError("the archive would need ZIP64");
73
+ return Buffer.concat([...parts, cd, end, comment]);
74
+ }
package/src/zip.js ADDED
@@ -0,0 +1,88 @@
1
+ import zlib from "node:zlib";
2
+
3
+ const MAX_ENTRIES = 20000;
4
+ const MAX_ENTRY_BYTES = 512 * 1024 * 1024;
5
+ const MAX_XML_BYTES = 64 * 1024 * 1024;
6
+ /** Everything one opened archive may inflate or copy out through read(), added up. Real documents read a few parts (types, rels, the main part). */
7
+ export const MAX_TOTAL_EXPANSION = 256 * 1024 * 1024;
8
+
9
+ export class ZipError extends Error {}
10
+
11
+ /** Reads the central directory of a ZIP held in memory. No ZIP64 (files over 4 GB are refused). */
12
+ export function openZip(buf, { budget = MAX_TOTAL_EXPANSION } = {}) {
13
+ if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
14
+ if (buf.length < 22 || buf.readUInt32LE(0) !== 0x04034b50) throw new ZipError("not a ZIP file");
15
+ let eocd = -1;
16
+ for (let i = buf.length - 22; i >= Math.max(0, buf.length - 22 - 65535); i--) {
17
+ if (buf.readUInt32LE(i) === 0x06054b50) {
18
+ eocd = i;
19
+ break;
20
+ }
21
+ }
22
+ if (eocd < 0) throw new ZipError("ZIP end record not found");
23
+ const count = buf.readUInt16LE(eocd + 10);
24
+ const cdSize = buf.readUInt32LE(eocd + 12);
25
+ const cdOffset = buf.readUInt32LE(eocd + 16);
26
+ if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff) throw new ZipError("ZIP64 is not supported");
27
+ if (count > MAX_ENTRIES) throw new ZipError("too many ZIP entries");
28
+ if (cdOffset + cdSize > buf.length) throw new ZipError("ZIP central directory out of range");
29
+ const entries = new Map();
30
+ let p = cdOffset;
31
+ for (let k = 0; k < count; k++) {
32
+ if (p + 46 > buf.length || buf.readUInt32LE(p) !== 0x02014b50) throw new ZipError("bad ZIP central directory");
33
+ const flags = buf.readUInt16LE(p + 8);
34
+ const method = buf.readUInt16LE(p + 10);
35
+ const crc = buf.readUInt32LE(p + 16);
36
+ const compSize = buf.readUInt32LE(p + 20);
37
+ const size = buf.readUInt32LE(p + 24);
38
+ const nameLen = buf.readUInt16LE(p + 28);
39
+ const extraLen = buf.readUInt16LE(p + 30);
40
+ const commentLen = buf.readUInt16LE(p + 32);
41
+ const localOffset = buf.readUInt32LE(p + 42);
42
+ const name = buf.toString("utf8", p + 46, p + 46 + nameLen);
43
+ if (name.startsWith("/") || name.split("/").includes("..") || name.includes("\\") || name.includes("\0")) throw new ZipError(`unsafe ZIP entry name: ${JSON.stringify(name)}`);
44
+ if (entries.has(name)) throw new ZipError(`duplicate ZIP entry: ${JSON.stringify(name)}`);
45
+ if (size > MAX_ENTRY_BYTES) throw new ZipError(`ZIP entry too large: ${JSON.stringify(name)}`);
46
+ entries.set(name, { name, flags, method, crc, compSize, size, localOffset, cdOffset: p }); // cdOffset: where this entry's central record starts (zip-write.js copies it)
47
+ p += 46 + nameLen + extraLen + commentLen;
48
+ }
49
+ let used = 0;
50
+ const spend = (name, n) => {
51
+ used += n;
52
+ if (used > budget) throw new ZipError(`ZIP expansion budget exceeded while reading ${JSON.stringify(name)} (more than ${Math.round(budget / 1024 / 1024)} MB in total)`);
53
+ };
54
+ /** The entry's bytes as stored in the archive (still compressed); null when there is no such entry. Spends no budget: nothing is inflated. */
55
+ function raw(name) {
56
+ const e = entries.get(name);
57
+ if (!e) return null;
58
+ const lo = e.localOffset;
59
+ if (lo + 30 > buf.length || buf.readUInt32LE(lo) !== 0x04034b50) throw new ZipError(`bad local header for ${JSON.stringify(name)}`);
60
+ const dataStart = lo + 30 + buf.readUInt16LE(lo + 26) + buf.readUInt16LE(lo + 28);
61
+ if (dataStart + e.compSize > buf.length) throw new ZipError(`ZIP entry ${JSON.stringify(name)} runs past the end of the file`);
62
+ return buf.subarray(dataStart, dataStart + e.compSize);
63
+ }
64
+ function read(name, cap = MAX_XML_BYTES) {
65
+ const e = entries.get(name);
66
+ if (!e) return null;
67
+ cap = Math.min(cap, MAX_ENTRY_BYTES);
68
+ const data = raw(name);
69
+ if (e.method === 0) {
70
+ // a stored entry is bounded by the same per-entry cap as an inflated one
71
+ if (data.length > cap) throw new ZipError(`ZIP entry ${JSON.stringify(name)} is larger than the ${Math.round(cap / 1024 / 1024)} MB limit for a part that is read`);
72
+ spend(name, data.length);
73
+ return Buffer.from(data);
74
+ }
75
+ if (e.method === 8) {
76
+ let out;
77
+ try {
78
+ out = zlib.inflateRawSync(data, { maxOutputLength: cap });
79
+ } catch (err) {
80
+ throw new ZipError(`cannot inflate ${JSON.stringify(name)}: ${err.message}`);
81
+ }
82
+ spend(name, out.length);
83
+ return out;
84
+ }
85
+ throw new ZipError(`unsupported ZIP method ${e.method} for ${JSON.stringify(name)}`);
86
+ }
87
+ return { names: [...entries.keys()], entries, read, raw, buf, eocd, readText: (name, cap) => read(name, cap)?.toString("utf8") ?? null };
88
+ }