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
@@ -39,7 +39,13 @@
39
39
  { "$ref": "#/$defs/delete_slide" },
40
40
  { "$ref": "#/$defs/move_slide" },
41
41
  { "$ref": "#/$defs/replace_all_text" },
42
- { "$ref": "#/$defs/format_all" }
42
+ { "$ref": "#/$defs/format_all" },
43
+ { "$ref": "#/$defs/set_text" },
44
+ { "$ref": "#/$defs/set_attribute" },
45
+ { "$ref": "#/$defs/remove_attribute" },
46
+ { "$ref": "#/$defs/insert_html" },
47
+ { "$ref": "#/$defs/replace_element" },
48
+ { "$ref": "#/$defs/delete_element" }
43
49
  ]
44
50
  }
45
51
  }
@@ -51,7 +57,7 @@
51
57
  "type": "object",
52
58
  "additionalProperties": false,
53
59
  "minProperties": 1,
54
- "description": "What the agent saw at the target. DOCX/PPTX text targets: `quote` must be contained in the target's text (whitespace collapsed, NFC), `text_hash` must equal the hash of the whole text (the batch `locator.text_hash`). XLSX targets: `quote` must EQUAL the text of the region (cells joined by TAB, rows by LF, each cell as typed: the formula if it has one, else the value; an empty region is the empty string), `text_hash` is the same hash of that text. `absent: true` (add_sheet) means the sheet name must not exist yet.",
60
+ "description": "What the agent saw at the target. HTML elements: `text_hash` must equal the hash of the element's own text (its direct text, the batch `locator.text_hash`), `quote` must be contained in its visible text. DOCX/PPTX text targets: `quote` must be contained in the target's text (whitespace collapsed, NFC), `text_hash` must equal the hash of the whole text (the batch `locator.text_hash`). XLSX targets: `quote` must EQUAL the text of the region (cells joined by TAB, rows by LF, each cell as typed: the formula if it has one, else the value; an empty region is the empty string), `text_hash` is the same hash of that text. `absent: true` (add_sheet) means the sheet name must not exist yet.",
55
61
  "properties": {
56
62
  "quote": { "type": "string", "maxLength": 20000 },
57
63
  "text_hash": { "type": "string", "pattern": "^[0-9a-f]{16}$" },
@@ -84,11 +90,11 @@
84
90
  "type": "object",
85
91
  "additionalProperties": false,
86
92
  "required": ["op", "target", "expect"],
87
- "description": "Inside one paragraph. Either `find` + `replace` (the find text must occur exactly once unless `occurrence` or `all` is given; run formatting is kept) or `text` (the whole paragraph text; refused when the paragraph holds images or other objects).",
93
+ "description": "Inside one paragraph (DOCX) or one element (HTML). Either `find` + `replace` (the find text must occur exactly once unless `occurrence` or `all` is given; run formatting is kept) or `text` (the whole paragraph text; refused when the paragraph holds images or other objects). HTML: each match must lie inside one text of the page (a match across an element's edge, such as a link or bold text, is refused as `spans_markup`); whitespace in `find` matches any run of whitespace in the source; `text` sets the element's text when it holds one text and nothing else.",
88
94
  "properties": {
89
95
  "op": { "const": "replace_text" },
90
96
  "id": { "$ref": "#/$defs/id" },
91
- "target": { "$ref": "#/$defs/docx_target" },
97
+ "target": { "$ref": "#/$defs/text_target" },
92
98
  "expect": { "$ref": "#/$defs/expect" },
93
99
  "find": { "$ref": "#/$defs/find" },
94
100
  "replace": { "$ref": "#/$defs/text" },
@@ -505,11 +511,117 @@
505
511
  "where": { "$ref": "#/$defs/where" }
506
512
  }
507
513
  },
514
+ "html_target": {
515
+ "type": "object",
516
+ "additionalProperties": false,
517
+ "description": "An element of an HTML file: an anchor id from the batch, or the `path` (as /html[1]/body[1]/p[3]) or unique `element_id` the batch's locator or the outline's `at` gives. Exactly one of them. The element must be written in the source (not implied by the parser); script and style elements cannot be targets.",
518
+ "properties": {
519
+ "anchor_id": { "type": "string", "pattern": "^a[0-9]+$" },
520
+ "path": { "$ref": "#/$defs/html_path" },
521
+ "element_id": { "$ref": "#/$defs/html_element_id" }
522
+ }
523
+ },
524
+ "text_target": {
525
+ "type": "object",
526
+ "additionalProperties": false,
527
+ "description": "replace_text's target: a DOCX paragraph (docx_target) or an HTML element (html_target). Which fields go together is checked by the validator: anchor_id alone, or the DOCX locator fields, or one of path and element_id.",
528
+ "properties": {
529
+ "anchor_id": { "type": "string", "pattern": "^a[0-9]+$" },
530
+ "para_id": { "type": "string", "pattern": "^[0-9A-Fa-f]{8}$" },
531
+ "body_path": { "type": "array", "minItems": 1, "maxItems": 4, "items": { "type": "integer", "minimum": 0 } },
532
+ "text_hash": { "type": "string", "pattern": "^[0-9a-f]{16}$" },
533
+ "path": { "$ref": "#/$defs/html_path" },
534
+ "element_id": { "$ref": "#/$defs/html_element_id" }
535
+ }
536
+ },
537
+ "html_path": { "type": "string", "minLength": 2, "maxLength": 20000, "pattern": "^(/[a-z0-9-]{1,40}\\[[1-9][0-9]{0,6}\\])+$" },
538
+ "html_element_id": { "type": "string", "pattern": "^[a-z0-9-]{1,32}$" },
539
+ "markup": { "type": "string", "minLength": 1, "maxLength": 200000 },
540
+ "attribute_name": { "type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_.-]{0,63}$" },
541
+ "set_text": {
542
+ "type": "object",
543
+ "additionalProperties": false,
544
+ "required": ["op", "target", "expect", "text"],
545
+ "description": "HTML only. The whole text of an element that holds one text and nothing else (or nothing at all); an element with other elements inside is refused as `spans_markup` (use replace_element). `expect.text_hash` is the hash of the element's own text (the batch's `locator.text_hash`).",
546
+ "properties": {
547
+ "op": { "const": "set_text" },
548
+ "id": { "$ref": "#/$defs/id" },
549
+ "target": { "$ref": "#/$defs/html_target" },
550
+ "expect": { "$ref": "#/$defs/expect" },
551
+ "text": { "$ref": "#/$defs/text" }
552
+ }
553
+ },
554
+ "set_attribute": {
555
+ "type": "object",
556
+ "additionalProperties": false,
557
+ "required": ["op", "target", "expect", "name", "value"],
558
+ "description": "HTML only. Sets one attribute of an element (its quoting kept), or adds it after the last one. Only attributes that cannot run code are written: an event handler, a data- attribute, a new URL scheme other than http, https, mailto, tel or a relative path, and every attribute outside the allowlist refuse the whole write (`active_content`).",
559
+ "properties": {
560
+ "op": { "const": "set_attribute" },
561
+ "id": { "$ref": "#/$defs/id" },
562
+ "target": { "$ref": "#/$defs/html_target" },
563
+ "expect": { "$ref": "#/$defs/expect" },
564
+ "name": { "$ref": "#/$defs/attribute_name" },
565
+ "value": { "type": "string", "maxLength": 1000000 }
566
+ }
567
+ },
568
+ "remove_attribute": {
569
+ "type": "object",
570
+ "additionalProperties": false,
571
+ "required": ["op", "target", "expect", "name"],
572
+ "description": "HTML only. Removes one attribute. Refused (`not_addressable`) on a start tag that holds an attribute more than once: removing the first would make the next one count.",
573
+ "properties": {
574
+ "op": { "const": "remove_attribute" },
575
+ "id": { "$ref": "#/$defs/id" },
576
+ "target": { "$ref": "#/$defs/html_target" },
577
+ "expect": { "$ref": "#/$defs/expect" },
578
+ "name": { "$ref": "#/$defs/attribute_name" }
579
+ }
580
+ },
581
+ "insert_html": {
582
+ "type": "object",
583
+ "additionalProperties": false,
584
+ "required": ["op", "target", "expect", "where", "html"],
585
+ "description": "HTML only. New markup before or after the target element, or as its first or last child. The markup is parsed in that place and must read back the same (`not_well_formed`) and stay where it is put (`proof_failed`). Code that runs in a browser (a script, an event handler, a frame, a form, a remote image, a stylesheet link, a data- attribute) refuses the whole write (`active_content`).",
586
+ "properties": {
587
+ "op": { "const": "insert_html" },
588
+ "id": { "$ref": "#/$defs/id" },
589
+ "target": { "$ref": "#/$defs/html_target" },
590
+ "expect": { "$ref": "#/$defs/expect" },
591
+ "where": { "enum": ["before", "after", "first_child", "last_child"] },
592
+ "html": { "$ref": "#/$defs/markup" }
593
+ }
594
+ },
595
+ "replace_element": {
596
+ "type": "object",
597
+ "additionalProperties": false,
598
+ "required": ["op", "target", "expect", "html"],
599
+ "description": "HTML only. Replaces the target element (its tags and everything inside) with new markup, parsed in its place under the same rules as insert_html.",
600
+ "properties": {
601
+ "op": { "const": "replace_element" },
602
+ "id": { "$ref": "#/$defs/id" },
603
+ "target": { "$ref": "#/$defs/html_target" },
604
+ "expect": { "$ref": "#/$defs/expect" },
605
+ "html": { "$ref": "#/$defs/markup" }
606
+ }
607
+ },
608
+ "delete_element": {
609
+ "type": "object",
610
+ "additionalProperties": false,
611
+ "required": ["op", "target", "expect"],
612
+ "description": "HTML only. Removes the target element with everything inside it.",
613
+ "properties": {
614
+ "op": { "const": "delete_element" },
615
+ "id": { "$ref": "#/$defs/id" },
616
+ "target": { "$ref": "#/$defs/html_target" },
617
+ "expect": { "$ref": "#/$defs/expect" }
618
+ }
619
+ },
508
620
  "replace_all_text": {
509
621
  "type": "object",
510
622
  "additionalProperties": false,
511
623
  "required": ["op", "find", "replace", "expect"],
512
- "description": "Every occurrence in the document (DOCX paragraphs including table cells, XLSX cells as typed, PPTX shapes). Protected units that contain a match are skipped and reported. `expect.count` is the number of matches including the protected ones.",
624
+ "description": "Every occurrence in the document (DOCX paragraphs including table cells, XLSX cells as typed, PPTX shapes, HTML text: never a script or style body, never an attribute value; a match across an element's edge is not counted, and a count that misses carries `spans_markup`). Protected units that contain a match are skipped and reported. `expect.count` is the number of matches including the protected ones.",
513
625
  "properties": {
514
626
  "op": { "const": "replace_all_text" },
515
627
  "id": { "$ref": "#/$defs/id" },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "onionskin-cli",
3
- "version": "0.0.3",
4
- "description": "Edit Word, Excel and PowerPoint files in the browser and send the batch to a coding agent.",
3
+ "version": "0.0.6",
4
+ "description": "Edit Word, Excel, PowerPoint and HTML files in the browser and send the batch to a coding agent.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "onionskin": "src/cli.js"
@@ -15,6 +15,8 @@
15
15
  "vendor/engine",
16
16
  "vendor/engine-node",
17
17
  "vendor/engine.lock.json",
18
+ "vendor/parse5",
19
+ "vendor/parse5.lock.json",
18
20
  "skills/onionskin",
19
21
  "!skills/onionskin/voice.md",
20
22
  "scripts/checker",
@@ -11,7 +11,7 @@ Environment: ONIONSKIN_SOFFICE (path to soffice), ONIONSKIN_NO_SOFFICE=1, ONIONS
11
11
  Run it with `python -I` so nothing from the current directory is imported. Parts are read with lxml, entity resolution and
12
12
  network access off, DTD and ENTITY declarations refused (defusedxml's rule).
13
13
  """
14
- import collections, json, os, re, shutil, signal, subprocess, sys, tempfile, time, zipfile
14
+ import collections, json, os, pathlib, re, shutil, signal, subprocess, sys, tempfile, time, zipfile
15
15
 
16
16
  try:
17
17
  from lxml import etree
@@ -48,10 +48,16 @@ def read_part(z, name):
48
48
  info = z.getinfo(name)
49
49
  if info.file_size > MAX_PART:
50
50
  raise ValueError(f'{name} is larger than {MAX_PART} bytes')
51
- data = z.read(name)
51
+ with z.open(name) as f:
52
+ data = f.read(MAX_PART + 1)
53
+ if len(data) > MAX_PART:
54
+ raise ValueError(f'{name} is larger than {MAX_PART} bytes')
52
55
  if re.search(rb'<!(DOCTYPE|ENTITY)', data[:65536]):
53
56
  raise ValueError(f'{name} declares a DTD or entity')
54
- return etree.fromstring(data, parser())
57
+ root = etree.fromstring(data, parser())
58
+ if root.getroottree().docinfo.doctype:
59
+ raise ValueError(f'{name} declares a DTD or entity')
60
+ return root
55
61
 
56
62
 
57
63
  def short(qn):
@@ -222,10 +228,18 @@ def soffice_convert(soffice, path, timeout, to='pdf'):
222
228
  tmp = tempfile.mkdtemp(prefix='onionskin-soffice-')
223
229
  try:
224
230
  # private profile so a LibreOffice the user has open is never touched; own process group so a hang can be killed whole
225
- cmd = [soffice, f'-env:UserInstallation=file://{tmp}/profile', '--headless', '--norestore', '--convert-to', to, '--outdir', tmp, path]
226
- p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
231
+ cmd = [soffice, f'-env:UserInstallation={pathlib.Path(os.path.abspath(tmp)).as_uri()}/profile', '--headless', '--norestore', '--convert-to', to, '--outdir', tmp, path]
232
+ pid_file = os.environ.get('ONIONSKIN_SOFFICE_PID_FILE') if os.name == 'posix' else None
233
+ if pid_file:
234
+ # Wait for registration before exec. If the checker dies first, EOF ends the new group.
235
+ cmd = ['/bin/sh', '-c', 'IFS= read -r ready || exit 1; exec "$@"', 'soffice', *cmd]
236
+ p = subprocess.Popen(cmd, stdin=subprocess.PIPE if pid_file else None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
227
237
  CURRENT.append(p)
228
238
  try:
239
+ if pid_file:
240
+ pathlib.Path(pid_file).write_text(f'{p.pid}\n', encoding='ascii')
241
+ p.stdin.write(b'\n')
242
+ p.stdin.flush()
229
243
  deadline = time.monotonic() + timeout
230
244
  while True:
231
245
  try:
@@ -240,6 +254,11 @@ def soffice_convert(soffice, path, timeout, to='pdf'):
240
254
  p.communicate()
241
255
  return {'ok': False, 'timeout': True, 'error': f'timeout after {timeout}s'}
242
256
  finally:
257
+ if p.poll() is None:
258
+ kill_current()
259
+ p.wait()
260
+ if pid_file:
261
+ pathlib.Path(pid_file).unlink(missing_ok=True)
243
262
  CURRENT.clear()
244
263
  pdf = os.path.join(tmp, os.path.splitext(os.path.basename(path))[0] + '.' + to)
245
264
  size = os.path.getsize(pdf) if os.path.exists(pdf) else 0