@preapexis/piexis 0.1.2

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 (348) hide show
  1. package/.github/workflows/npm-publish.yml +63 -0
  2. package/AGENTS.md +58 -0
  3. package/PERMISSION_MODES_PLAN.md +279 -0
  4. package/README.md +207 -0
  5. package/WHAT_YOU_GET.md +153 -0
  6. package/eval/README.md +72 -0
  7. package/eval/permission-cases.json +28 -0
  8. package/eval/permissions.mjs +103 -0
  9. package/extensions/ask-question.ts +111 -0
  10. package/extensions/background-tasks.ts +433 -0
  11. package/extensions/background-worker.ts +50 -0
  12. package/extensions/glob.ts +32 -0
  13. package/extensions/mode.ts +397 -0
  14. package/extensions/ui.ts +134 -0
  15. package/lib/bash-approvals.ts +113 -0
  16. package/lib/bash-policy.ts +164 -0
  17. package/lib/mode-policy.ts +174 -0
  18. package/lib/path-policy.ts +182 -0
  19. package/lib/permission-classifier.ts +128 -0
  20. package/lib/permission-config.ts +91 -0
  21. package/lib/permission-denials.ts +32 -0
  22. package/lib/permission-intent.ts +128 -0
  23. package/lib/permission-taxonomy.ts +15 -0
  24. package/lib/task-broker.ts +65 -0
  25. package/lib/task-checkout.ts +39 -0
  26. package/lib/task-git.ts +44 -0
  27. package/lib/task-patch.ts +106 -0
  28. package/lib/task-permissions.ts +51 -0
  29. package/lib/task-report.ts +62 -0
  30. package/lib/task-worktree-policy.ts +56 -0
  31. package/package.json +51 -0
  32. package/prompts/commit.md +7 -0
  33. package/prompts/init.md +66 -0
  34. package/prompts/summary.md +14 -0
  35. package/skills/claude-api/LICENSE.txt +202 -0
  36. package/skills/claude-api/SKILL.md +572 -0
  37. package/skills/claude-api/csharp/claude-api/README.md +362 -0
  38. package/skills/claude-api/csharp/claude-api/batches.md +14 -0
  39. package/skills/claude-api/csharp/claude-api/files-api.md +25 -0
  40. package/skills/claude-api/csharp/claude-api/streaming.md +28 -0
  41. package/skills/claude-api/csharp/claude-api/tool-use.md +164 -0
  42. package/skills/claude-api/curl/examples.md +256 -0
  43. package/skills/claude-api/curl/managed-agents.md +371 -0
  44. package/skills/claude-api/go/claude-api/README.md +188 -0
  45. package/skills/claude-api/go/claude-api/files-api.md +23 -0
  46. package/skills/claude-api/go/claude-api/streaming.md +43 -0
  47. package/skills/claude-api/go/claude-api/tool-use.md +220 -0
  48. package/skills/claude-api/go/managed-agents/README.md +564 -0
  49. package/skills/claude-api/java/claude-api/README.md +238 -0
  50. package/skills/claude-api/java/claude-api/files-api.md +27 -0
  51. package/skills/claude-api/java/claude-api/streaming.md +24 -0
  52. package/skills/claude-api/java/claude-api/tool-use.md +228 -0
  53. package/skills/claude-api/java/managed-agents/README.md +443 -0
  54. package/skills/claude-api/php/claude-api/README.md +174 -0
  55. package/skills/claude-api/php/claude-api/batches.md +15 -0
  56. package/skills/claude-api/php/claude-api/files-api.md +13 -0
  57. package/skills/claude-api/php/claude-api/streaming.md +27 -0
  58. package/skills/claude-api/php/claude-api/tool-use.md +253 -0
  59. package/skills/claude-api/php/managed-agents/README.md +443 -0
  60. package/skills/claude-api/python/claude-api/README.md +565 -0
  61. package/skills/claude-api/python/claude-api/batches.md +198 -0
  62. package/skills/claude-api/python/claude-api/files-api.md +170 -0
  63. package/skills/claude-api/python/claude-api/sdk-upgrade.md +286 -0
  64. package/skills/claude-api/python/claude-api/streaming.md +179 -0
  65. package/skills/claude-api/python/claude-api/tool-use.md +627 -0
  66. package/skills/claude-api/python/managed-agents/README.md +335 -0
  67. package/skills/claude-api/ruby/claude-api/README.md +134 -0
  68. package/skills/claude-api/ruby/claude-api/streaming.md +16 -0
  69. package/skills/claude-api/ruby/claude-api/tool-use.md +41 -0
  70. package/skills/claude-api/ruby/managed-agents/README.md +394 -0
  71. package/skills/claude-api/shared/admin-api.md +179 -0
  72. package/skills/claude-api/shared/agent-design.md +101 -0
  73. package/skills/claude-api/shared/anthropic-cli.md +269 -0
  74. package/skills/claude-api/shared/claude-platform-on-aws.md +59 -0
  75. package/skills/claude-api/shared/cost-optimization.md +233 -0
  76. package/skills/claude-api/shared/error-codes.md +262 -0
  77. package/skills/claude-api/shared/live-sources.md +166 -0
  78. package/skills/claude-api/shared/managed-agents-api-reference.md +455 -0
  79. package/skills/claude-api/shared/managed-agents-client-patterns.md +220 -0
  80. package/skills/claude-api/shared/managed-agents-core.md +359 -0
  81. package/skills/claude-api/shared/managed-agents-environments.md +226 -0
  82. package/skills/claude-api/shared/managed-agents-events.md +290 -0
  83. package/skills/claude-api/shared/managed-agents-memory.md +201 -0
  84. package/skills/claude-api/shared/managed-agents-multiagent.md +260 -0
  85. package/skills/claude-api/shared/managed-agents-onboarding.md +82 -0
  86. package/skills/claude-api/shared/managed-agents-outcomes.md +108 -0
  87. package/skills/claude-api/shared/managed-agents-overview.md +77 -0
  88. package/skills/claude-api/shared/managed-agents-scheduled-deployments.md +156 -0
  89. package/skills/claude-api/shared/managed-agents-self-hosted-sandboxes.md +282 -0
  90. package/skills/claude-api/shared/managed-agents-tools.md +504 -0
  91. package/skills/claude-api/shared/managed-agents-webhooks.md +143 -0
  92. package/skills/claude-api/shared/model-migration.md +1872 -0
  93. package/skills/claude-api/shared/models.md +142 -0
  94. package/skills/claude-api/shared/platform-availability.md +53 -0
  95. package/skills/claude-api/shared/prompt-audit.md +223 -0
  96. package/skills/claude-api/shared/prompt-caching.md +292 -0
  97. package/skills/claude-api/shared/token-counting.md +56 -0
  98. package/skills/claude-api/shared/tool-use-concepts.md +526 -0
  99. package/skills/claude-api/typescript/claude-api/README.md +405 -0
  100. package/skills/claude-api/typescript/claude-api/batches.md +106 -0
  101. package/skills/claude-api/typescript/claude-api/files-api.md +98 -0
  102. package/skills/claude-api/typescript/claude-api/streaming.md +178 -0
  103. package/skills/claude-api/typescript/claude-api/tool-use.md +586 -0
  104. package/skills/claude-api/typescript/managed-agents/README.md +359 -0
  105. package/skills/code-review/SKILL.md +34 -0
  106. package/skills/code-review/agents/openai.yaml +3 -0
  107. package/skills/codebase-design/DEEPENING.md +37 -0
  108. package/skills/codebase-design/DESIGN-IT-TWICE.md +44 -0
  109. package/skills/codebase-design/SKILL.md +40 -0
  110. package/skills/diagnosing-bugs/SKILL.md +38 -0
  111. package/skills/diagnosing-bugs/agents/openai.yaml +3 -0
  112. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +44 -0
  113. package/skills/docx/LICENSE.txt +30 -0
  114. package/skills/docx/SKILL.md +93 -0
  115. package/skills/docx/scripts/__init__.py +1 -0
  116. package/skills/docx/scripts/accept_changes.py +135 -0
  117. package/skills/docx/scripts/comment.py +368 -0
  118. package/skills/docx/scripts/merge_runs.py +310 -0
  119. package/skills/docx/scripts/office/helpers/__init__.py +111 -0
  120. package/skills/docx/scripts/office/helpers/pptx_chart.py +170 -0
  121. package/skills/docx/scripts/office/helpers/pptx_slide.py +60 -0
  122. package/skills/docx/scripts/office/helpers/pptx_theme.py +114 -0
  123. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  124. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  125. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  126. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  127. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  128. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  129. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  130. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  131. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  132. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  133. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  134. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  135. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  136. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  137. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  138. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  139. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  140. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  141. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  142. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  143. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  144. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  145. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  146. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  147. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  148. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  149. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  150. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  151. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  152. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  153. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  154. package/skills/docx/scripts/office/schemas/mce/mc.xsd +75 -0
  155. package/skills/docx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  156. package/skills/docx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  157. package/skills/docx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  158. package/skills/docx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  159. package/skills/docx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  160. package/skills/docx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  161. package/skills/docx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  162. package/skills/docx/scripts/office/soffice.py +192 -0
  163. package/skills/docx/scripts/office/validate.py +173 -0
  164. package/skills/docx/scripts/office/validators/__init__.py +15 -0
  165. package/skills/docx/scripts/office/validators/base.py +875 -0
  166. package/skills/docx/scripts/office/validators/docx.py +466 -0
  167. package/skills/docx/scripts/office/validators/pptx.py +441 -0
  168. package/skills/docx/scripts/office/validators/redlining.py +299 -0
  169. package/skills/docx/scripts/templates/comments.xml +3 -0
  170. package/skills/docx/scripts/templates/commentsExtended.xml +3 -0
  171. package/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
  172. package/skills/docx/scripts/templates/commentsIds.xml +3 -0
  173. package/skills/docx/scripts/templates/people.xml +3 -0
  174. package/skills/frontend-design/LICENSE.txt +177 -0
  175. package/skills/frontend-design/SKILL.md +27 -0
  176. package/skills/grilling/SKILL.md +41 -0
  177. package/skills/lean-build/SKILL.md +22 -0
  178. package/skills/lean-build/agents/openai.yaml +4 -0
  179. package/skills/mcp-builder/LICENSE.txt +202 -0
  180. package/skills/mcp-builder/SKILL.md +235 -0
  181. package/skills/mcp-builder/reference/evaluation.md +602 -0
  182. package/skills/mcp-builder/reference/mcp_best_practices.md +249 -0
  183. package/skills/mcp-builder/reference/node_mcp_server.md +970 -0
  184. package/skills/mcp-builder/reference/python_mcp_server.md +719 -0
  185. package/skills/mcp-builder/scripts/connections.py +151 -0
  186. package/skills/mcp-builder/scripts/evaluation.py +373 -0
  187. package/skills/mcp-builder/scripts/example_evaluation.xml +22 -0
  188. package/skills/mcp-builder/scripts/requirements.txt +2 -0
  189. package/skills/migration/SKILL.md +17 -0
  190. package/skills/migration/agents/openai.yaml +4 -0
  191. package/skills/pdf/LICENSE.txt +30 -0
  192. package/skills/pdf/SKILL.md +27 -0
  193. package/skills/pdf/forms.md +294 -0
  194. package/skills/pdf/reference.md +612 -0
  195. package/skills/pdf/scripts/check_bounding_boxes.py +65 -0
  196. package/skills/pdf/scripts/check_fillable_fields.py +11 -0
  197. package/skills/pdf/scripts/convert_pdf_to_images.py +33 -0
  198. package/skills/pdf/scripts/create_validation_image.py +37 -0
  199. package/skills/pdf/scripts/extract_form_field_info.py +122 -0
  200. package/skills/pdf/scripts/extract_form_structure.py +115 -0
  201. package/skills/pdf/scripts/fill_fillable_fields.py +98 -0
  202. package/skills/pdf/scripts/fill_pdf_form_with_annotations.py +107 -0
  203. package/skills/pptx/LICENSE.txt +30 -0
  204. package/skills/pptx/SKILL.md +240 -0
  205. package/skills/pptx/scripts/__init__.py +0 -0
  206. package/skills/pptx/scripts/add_slide.py +367 -0
  207. package/skills/pptx/scripts/clean.py +309 -0
  208. package/skills/pptx/scripts/office/helpers/__init__.py +111 -0
  209. package/skills/pptx/scripts/office/helpers/pptx_chart.py +170 -0
  210. package/skills/pptx/scripts/office/helpers/pptx_slide.py +60 -0
  211. package/skills/pptx/scripts/office/helpers/pptx_theme.py +114 -0
  212. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  213. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  214. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  215. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  216. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  217. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  218. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  219. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  220. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  221. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  222. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  223. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  224. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  225. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  226. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  227. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  228. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  229. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  230. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  231. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  232. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  233. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  234. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  235. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  236. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  237. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  238. package/skills/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  239. package/skills/pptx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  240. package/skills/pptx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  241. package/skills/pptx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  242. package/skills/pptx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  243. package/skills/pptx/scripts/office/schemas/mce/mc.xsd +75 -0
  244. package/skills/pptx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  245. package/skills/pptx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  246. package/skills/pptx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  247. package/skills/pptx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  248. package/skills/pptx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  249. package/skills/pptx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  250. package/skills/pptx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  251. package/skills/pptx/scripts/office/soffice.py +192 -0
  252. package/skills/pptx/scripts/office/validate.py +173 -0
  253. package/skills/pptx/scripts/office/validators/__init__.py +15 -0
  254. package/skills/pptx/scripts/office/validators/base.py +875 -0
  255. package/skills/pptx/scripts/office/validators/docx.py +466 -0
  256. package/skills/pptx/scripts/office/validators/pptx.py +441 -0
  257. package/skills/pptx/scripts/office/validators/redlining.py +299 -0
  258. package/skills/pptx/scripts/thumbnail.py +311 -0
  259. package/skills/safe-refactor/SKILL.md +16 -0
  260. package/skills/safe-refactor/agents/openai.yaml +4 -0
  261. package/skills/tdd/SKILL.md +18 -0
  262. package/skills/tdd/agents/openai.yaml +3 -0
  263. package/skills/tdd/mocking.md +59 -0
  264. package/skills/tdd/tests.md +77 -0
  265. package/skills/verify-and-stop/SKILL.md +16 -0
  266. package/skills/verify-and-stop/agents/openai.yaml +4 -0
  267. package/skills/webapp-testing/LICENSE.txt +202 -0
  268. package/skills/webapp-testing/SKILL.md +96 -0
  269. package/skills/webapp-testing/examples/console_logging.py +35 -0
  270. package/skills/webapp-testing/examples/element_discovery.py +40 -0
  271. package/skills/webapp-testing/examples/static_html_automation.py +33 -0
  272. package/skills/webapp-testing/scripts/with_server.py +106 -0
  273. package/skills/writing-for-agents/SKILL-MECHANICS.md +22 -0
  274. package/skills/writing-for-agents/SKILL.md +28 -0
  275. package/skills/writing-for-agents/agents/openai.yaml +3 -0
  276. package/skills/xlsx/LICENSE.txt +30 -0
  277. package/skills/xlsx/SKILL.md +101 -0
  278. package/skills/xlsx/scripts/office/helpers/__init__.py +111 -0
  279. package/skills/xlsx/scripts/office/helpers/pptx_chart.py +170 -0
  280. package/skills/xlsx/scripts/office/helpers/pptx_slide.py +60 -0
  281. package/skills/xlsx/scripts/office/helpers/pptx_theme.py +114 -0
  282. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  283. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  284. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  285. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  286. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  287. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  288. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  289. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  290. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  291. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  292. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  293. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  294. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  295. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  296. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  297. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  298. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  299. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  300. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  301. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  302. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  303. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  304. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  305. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  306. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  307. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  308. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  309. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  310. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  311. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  312. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  313. package/skills/xlsx/scripts/office/schemas/mce/mc.xsd +75 -0
  314. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  315. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  316. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  317. package/skills/xlsx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  318. package/skills/xlsx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  319. package/skills/xlsx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  320. package/skills/xlsx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  321. package/skills/xlsx/scripts/office/soffice.py +192 -0
  322. package/skills/xlsx/scripts/office/validate.py +173 -0
  323. package/skills/xlsx/scripts/office/validators/__init__.py +15 -0
  324. package/skills/xlsx/scripts/office/validators/base.py +875 -0
  325. package/skills/xlsx/scripts/office/validators/docx.py +466 -0
  326. package/skills/xlsx/scripts/office/validators/pptx.py +441 -0
  327. package/skills/xlsx/scripts/office/validators/redlining.py +299 -0
  328. package/skills/xlsx/scripts/recalc.py +308 -0
  329. package/skills-lock.json +359 -0
  330. package/tests/auto-permissions.test.mjs +315 -0
  331. package/tests/background-tasks.test.mjs +27 -0
  332. package/tests/background-worker.test.mjs +60 -0
  333. package/tests/bash-permissions.test.mjs +276 -0
  334. package/tests/glob.test.mjs +31 -0
  335. package/tests/mode.test.mjs +62 -0
  336. package/tests/permission-evaluation.test.mjs +70 -0
  337. package/tests/permission-foundation.test.mjs +194 -0
  338. package/tests/permission-policy.test.mjs +158 -0
  339. package/tests/runtime/README.md +51 -0
  340. package/tests/runtime/harness.mjs +190 -0
  341. package/tests/runtime/permissions.test.mjs +192 -0
  342. package/tests/runtime/tui.test.mjs +60 -0
  343. package/tests/skills-routing.test.mjs +51 -0
  344. package/tests/task-integration.test.mjs +324 -0
  345. package/tests/task-permissions.test.mjs +178 -0
  346. package/tests/task-worktree-policy.test.mjs +53 -0
  347. package/themes/orange.json +82 -0
  348. package/tsconfig.json +13 -0
@@ -0,0 +1,93 @@
1
+ ---
2
+ name: docx
3
+ description: "Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation."
4
+ license: Proprietary. LICENSE.txt has complete terms
5
+ ---
6
+
7
+ # DOCX creation, editing, and analysis
8
+
9
+ Read only the section for the requested task; creation, editing, comments, and redlining are independent workflows.
10
+
11
+ A `.docx` is a ZIP archive of XML files. Choose your approach by task:
12
+
13
+ | Task | Approach |
14
+ |---|---|
15
+ | **Create** a new document | Write a `docx` (npm) script — see gotchas below |
16
+ | **Edit** an existing document | `unzip` → edit `word/document.xml` → `zip` (docx-js cannot open existing files) |
17
+ | **Read** content | `pandoc -t markdown file.docx` |
18
+
19
+ > Script paths below are relative to this skill's directory.
20
+
21
+ ## Creating with docx-js — gotchas
22
+
23
+ `docx` is preinstalled — do not run `npm install` first; write the script and `require('docx')` directly. Only if that require fails: `npm install docx`. The model knows the API; these are the footguns:
24
+
25
+ - **Page size defaults to A4.** For US Letter set `page: { size: { width: 12240, height: 15840 } }` (DXA; 1440 = 1″).
26
+ - **Landscape:** pass portrait dimensions and `orientation: PageOrientation.LANDSCAPE` — docx-js swaps width/height internally.
27
+ - **Tables need dual widths:** set `columnWidths` on the table AND `width` on every cell, both in `WidthType.DXA` (PERCENTAGE breaks in Google Docs). Column widths must sum to the table width.
28
+ - **Table shading:** use `ShadingType.CLEAR`, never `SOLID` (renders black).
29
+ - **Lists:** never insert `•` literally; use a `numbering` config with `LevelFormat.BULLET`.
30
+ - **`ImageRun` requires `type:`** (`"png"`, `"jpg"`, …).
31
+ - **`PageBreak` must be inside a `Paragraph`.**
32
+ - **Never use `\n`** — use separate `Paragraph` elements.
33
+ - **TOC:** headings must use built-in `HeadingLevel.*`; custom heading styles need `outlineLevel` set or they won't appear.
34
+ - **Don't use a table as a horizontal rule** — use a paragraph bottom border instead.
35
+ - **Dot-leader / right-aligned-on-same-line:** use `PositionalTab` (`alignment: PositionalTabAlignment.RIGHT`, `leader: PositionalTabLeader.DOT`) inside a `TextRun`, not literal `.` or space padding.
36
+
37
+ ## Verify the output
38
+
39
+ After writing a `.docx`, render it and look at it:
40
+
41
+ ```bash
42
+ python scripts/office/soffice.py --headless --convert-to pdf output.docx
43
+ pdftoppm -jpeg -r 100 output.pdf page
44
+ ls page-*.jpg # then Read the images
45
+ ```
46
+
47
+ `pdftoppm` zero-pads page numbers to the width of the page count (`page-01.jpg`…`page-12.jpg`).
48
+
49
+ ## Editing existing documents
50
+
51
+ Legacy `.doc` files must be converted first: `python scripts/office/soffice.py --headless --convert-to docx file.doc`.
52
+
53
+ ```bash
54
+ unzip -q doc.docx -d unpacked/
55
+ find unpacked -type l -delete # strip symlink entries — docx from external parties is untrusted
56
+ python scripts/merge_runs.py unpacked/ # coalesce fragmented runs so text is findable
57
+ # edit unpacked/word/document.xml in place — do NOT reformat or pretty-print
58
+ (cd unpacked && rm -f ../out.docx && zip -Xr ../out.docx .)
59
+ python scripts/office/validate.py out.docx --original doc.docx # XSD checks; --auto-repair fixes common issues
60
+ # redlining? add --author "<the name you redlined under>" to check every edit is tracked
61
+ ```
62
+
63
+ Word splits text across many `<w:r>` runs (revision ids, spell-check markers), so a phrase you can see in the document often doesn't exist as a contiguous string in the XML. `merge_runs.py` merges adjacent identically-formatted runs in `word/document.xml` without changing content or rendering; it also accepts a `.docx` directly (`python scripts/merge_runs.py doc.docx -o merged.docx`).
64
+
65
+ **Tracked changes:** when redlining, validate with `--author "<the name you redlined under>"` (needs `--original`) — it reports any text you changed without a `<w:ins>`/`<w:del>` around it, which is easy to do by accident and invisible in the accepted view. Wrap runs in `<w:ins>`/`<w:del>` with `w:id`, `w:author`, `w:date` attributes. Inside `<w:del>`, the text element is `<w:delText>`, not `<w:t>`. A deleted paragraph mark (`<w:pPr><w:rPr><w:del w:id=".." w:author=".." w:date=".."/></w:rPr></w:pPr>`) means "merge this paragraph into the next" — so deleting a paragraph outright is that plus a `<w:del>` around every run. The `<w:del/>` must come before the rPr's other children; their order is schema-enforced.
66
+
67
+ To produce a clean copy with all tracked changes accepted: `python scripts/accept_changes.py in.docx out.docx`.
68
+
69
+ Accepting a deleted paragraph mark should join that paragraph to the one below it, so a paragraph whose runs are *all* deleted vanishes. Word does this; `accept_changes.py` and `pandoc --track-changes=accept` don't always. Both fail the same way — they strip the deleted text but leave the emptied paragraph behind, which reads as a stray empty bullet when it was auto-numbered:
70
+
71
+ - `pandoc --track-changes=accept` never joins the paragraphs.
72
+ - `accept_changes.py` (LibreOffice) joins them correctly, except when the deleted paragraph is followed by an empty spacer paragraph.
73
+
74
+ An empty bullet in either view is an artifact of that view, not a defect in the document. Check paragraph deletions in the XML.
75
+
76
+ ## Comments
77
+
78
+ Comments require six cross-linked files. Use the helper — directory mode when you'll also be editing `document.xml` (saves an unzip/rezip cycle), `.docx`-direct mode otherwise:
79
+
80
+ ```bash
81
+ # Against an already-unpacked directory (preferred when also placing markers)
82
+ python scripts/comment.py unpacked/ "Fees & expenses cap is too low"
83
+ python scripts/comment.py unpacked/ "Agreed" --parent 0
84
+
85
+ # Against a .docx directly
86
+ python scripts/comment.py contract.docx "This cap is too low" -o annotated.docx
87
+ ```
88
+
89
+ The script writes `comments.xml`, `commentsExtended.xml`, `commentsIds.xml`, `commentsExtensible.xml`, the relationships, and the content-type overrides. Comment IDs are auto-assigned. It then prints the `<w:commentRangeStart>`/`<w:commentRangeEnd>`/`<w:commentReference>` snippet to add to `word/document.xml` so the comment anchors to specific text — until you place those markers, the comment exists but is not visible.
90
+
91
+ ## Dependencies
92
+
93
+ `docx` (npm, preinstalled — install only if `require('docx')` fails) · `pandoc` · LibreOffice (`soffice`) · `pdftoppm` (Poppler)
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,135 @@
1
+ """Accept all tracked changes in a DOCX file using LibreOffice.
2
+
3
+ Requires LibreOffice (soffice) to be installed.
4
+ """
5
+
6
+ import argparse
7
+ import logging
8
+ import shutil
9
+ import subprocess
10
+ from pathlib import Path
11
+
12
+ from office.soffice import get_soffice_env
13
+
14
+ logger = logging.getLogger(__name__)
15
+
16
+ LIBREOFFICE_PROFILE = "/tmp/libreoffice_docx_profile"
17
+ MACRO_DIR = f"{LIBREOFFICE_PROFILE}/user/basic/Standard"
18
+
19
+ ACCEPT_CHANGES_MACRO = """<?xml version="1.0" encoding="UTF-8"?>
20
+ <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
21
+ <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">
22
+ Sub AcceptAllTrackedChanges()
23
+ Dim document As Object
24
+ Dim dispatcher As Object
25
+
26
+ document = ThisComponent.CurrentController.Frame
27
+ dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
28
+
29
+ dispatcher.executeDispatch(document, ".uno:AcceptAllTrackedChanges", "", 0, Array())
30
+ ThisComponent.store()
31
+ ThisComponent.close(True)
32
+ End Sub
33
+ </script:module>"""
34
+
35
+
36
+ def accept_changes(
37
+ input_file: str,
38
+ output_file: str,
39
+ ) -> tuple[None, str]:
40
+ input_path = Path(input_file)
41
+ output_path = Path(output_file)
42
+
43
+ if not input_path.exists():
44
+ return None, f"Error: Input file not found: {input_file}"
45
+
46
+ if not input_path.suffix.lower() == ".docx":
47
+ return None, f"Error: Input file is not a DOCX file: {input_file}"
48
+
49
+ try:
50
+ output_path.parent.mkdir(parents=True, exist_ok=True)
51
+ shutil.copy2(input_path, output_path)
52
+ except Exception as e:
53
+ return None, f"Error: Failed to copy input file to output location: {e}"
54
+
55
+ if not _setup_libreoffice_macro():
56
+ return None, "Error: Failed to setup LibreOffice macro"
57
+
58
+ cmd = [
59
+ "soffice",
60
+ "--headless",
61
+ f"-env:UserInstallation=file://{LIBREOFFICE_PROFILE}",
62
+ "--norestore",
63
+ "vnd.sun.star.script:Standard.Module1.AcceptAllTrackedChanges?language=Basic&location=application",
64
+ str(output_path.absolute()),
65
+ ]
66
+
67
+ try:
68
+ result = subprocess.run(
69
+ cmd,
70
+ capture_output=True,
71
+ text=True,
72
+ timeout=30,
73
+ check=False,
74
+ env=get_soffice_env(),
75
+ )
76
+ except subprocess.TimeoutExpired:
77
+ return (
78
+ None,
79
+ f"Successfully accepted all tracked changes: {input_file} -> {output_file}",
80
+ )
81
+
82
+ if result.returncode != 0:
83
+ return None, f"Error: LibreOffice failed: {result.stderr}"
84
+
85
+ return (
86
+ None,
87
+ f"Successfully accepted all tracked changes: {input_file} -> {output_file}",
88
+ )
89
+
90
+
91
+ def _setup_libreoffice_macro() -> bool:
92
+ macro_dir = Path(MACRO_DIR)
93
+ macro_file = macro_dir / "Module1.xba"
94
+
95
+ if macro_file.exists() and "AcceptAllTrackedChanges" in macro_file.read_text():
96
+ return True
97
+
98
+ if not macro_dir.exists():
99
+ subprocess.run(
100
+ [
101
+ "soffice",
102
+ "--headless",
103
+ f"-env:UserInstallation=file://{LIBREOFFICE_PROFILE}",
104
+ "--terminate_after_init",
105
+ ],
106
+ capture_output=True,
107
+ timeout=10,
108
+ check=False,
109
+ env=get_soffice_env(),
110
+ )
111
+ macro_dir.mkdir(parents=True, exist_ok=True)
112
+
113
+ try:
114
+ macro_file.write_text(ACCEPT_CHANGES_MACRO)
115
+ return True
116
+ except Exception as e:
117
+ logger.warning(f"Failed to setup LibreOffice macro: {e}")
118
+ return False
119
+
120
+
121
+ if __name__ == "__main__":
122
+ parser = argparse.ArgumentParser(
123
+ description="Accept all tracked changes in a DOCX file"
124
+ )
125
+ parser.add_argument("input_file", help="Input DOCX file with tracked changes")
126
+ parser.add_argument(
127
+ "output_file", help="Output DOCX file (clean, no tracked changes)"
128
+ )
129
+ args = parser.parse_args()
130
+
131
+ _, message = accept_changes(args.input_file, args.output_file)
132
+ print(message)
133
+
134
+ if "Error" in message:
135
+ raise SystemExit(1)
@@ -0,0 +1,368 @@
1
+ """Add comments to a DOCX document.
2
+
3
+ Accepts either an unpacked directory OR a .docx/.dotx file directly.
4
+
5
+ Usage:
6
+ # Against an unpacked directory (writes satellite files in place)
7
+ python comment.py unpacked/ "Comment text"
8
+ python comment.py unpacked/ "Reply text" --parent 0
9
+
10
+ # Against a .docx directly (extracts, writes satellite files, rezips)
11
+ python comment.py contract.docx "This cap is too low" -o annotated.docx
12
+ python comment.py contract.docx "Comment" --id 5 # explicit ID
13
+
14
+ The comment ID is auto-assigned (max existing + 1) unless --id is given.
15
+ Plain text is XML-escaped automatically; if you pass already-escaped text
16
+ (e.g. &amp;, &#x2019;) use --raw to skip escaping.
17
+
18
+ After running, add markers to word/document.xml so the comment is visible:
19
+ <w:commentRangeStart w:id="N"/>
20
+ ... commented content ...
21
+ <w:commentRangeEnd w:id="N"/>
22
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="N"/></w:r>
23
+ """
24
+
25
+ import argparse
26
+ import random
27
+ import shutil
28
+ import sys
29
+ import tempfile
30
+ import zipfile
31
+ from datetime import datetime, timezone
32
+ from pathlib import Path
33
+
34
+ import defusedxml.minidom
35
+ from xml.parsers.expat import ExpatError
36
+ from xml.sax.saxutils import escape as xml_escape
37
+
38
+ from office.helpers import opc_target, rezip as _rezip, safe_extract as _safe_extract
39
+
40
+ TEMPLATE_DIR = Path(__file__).parent / "templates"
41
+ NS = {
42
+ "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
43
+ "w14": "http://schemas.microsoft.com/office/word/2010/wordml",
44
+ "w15": "http://schemas.microsoft.com/office/word/2012/wordml",
45
+ "w16cid": "http://schemas.microsoft.com/office/word/2016/wordml/cid",
46
+ "w16cex": "http://schemas.microsoft.com/office/word/2018/wordml/cex",
47
+ }
48
+
49
+ COMMENT_XML = """\
50
+ <w:comment w:id="{id}" w:author="{author}" w:date="{date}" w:initials="{initials}">
51
+ <w:p w14:paraId="{para_id}" w14:textId="77777777">
52
+ <w:r>
53
+ <w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
54
+ <w:annotationRef/>
55
+ </w:r>
56
+ <w:r>
57
+ <w:rPr>
58
+ <w:color w:val="000000"/>
59
+ <w:sz w:val="20"/>
60
+ <w:szCs w:val="20"/>
61
+ </w:rPr>
62
+ <w:t xml:space="preserve">{text}</w:t>
63
+ </w:r>
64
+ </w:p>
65
+ </w:comment>"""
66
+
67
+ COMMENT_MARKER_TEMPLATE = """
68
+ Add to word/document.xml (markers must be direct children of w:p, never inside w:r):
69
+ <w:commentRangeStart w:id="{cid}"/>
70
+ <w:r>...</w:r>
71
+ <w:commentRangeEnd w:id="{cid}"/>
72
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="{cid}"/></w:r>"""
73
+
74
+ REPLY_MARKER_TEMPLATE = """
75
+ Nest markers inside parent {pid}'s markers (direct children of w:p, never inside w:r):
76
+ <w:commentRangeStart w:id="{pid}"/><w:commentRangeStart w:id="{cid}"/>
77
+ <w:r>...</w:r>
78
+ <w:commentRangeEnd w:id="{cid}"/><w:commentRangeEnd w:id="{pid}"/>
79
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="{pid}"/></w:r>
80
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="{cid}"/></w:r>"""
81
+
82
+ SMART_QUOTE_ENTITIES = {
83
+ "“": "&#x201C;",
84
+ "”": "&#x201D;",
85
+ "‘": "&#x2018;",
86
+ "’": "&#x2019;",
87
+ }
88
+
89
+
90
+ def _generate_hex_id() -> str:
91
+ return f"{random.randint(0, 0x7FFFFFFE):08X}"
92
+
93
+
94
+ def _encode_smart_quotes(text: str) -> str:
95
+ for char, entity in SMART_QUOTE_ENTITIES.items():
96
+ text = text.replace(char, entity)
97
+ return text
98
+
99
+
100
+ def _append_xml(xml_path: Path, root_tag: str, content: str) -> None:
101
+ dom = defusedxml.minidom.parseString(xml_path.read_text(encoding="utf-8"))
102
+ root = dom.getElementsByTagName(root_tag)[0]
103
+ ns_attrs = " ".join(f'xmlns:{k}="{v}"' for k, v in NS.items())
104
+ wrapper_dom = defusedxml.minidom.parseString(f"<root {ns_attrs}>{content}</root>")
105
+ for child in wrapper_dom.documentElement.childNodes:
106
+ if child.nodeType == child.ELEMENT_NODE:
107
+ root.appendChild(dom.importNode(child, True))
108
+ output = _encode_smart_quotes(dom.toxml(encoding="UTF-8").decode("utf-8"))
109
+ xml_path.write_text(output, encoding="utf-8")
110
+
111
+
112
+ def _find_para_id(comments_path: Path, comment_id: int) -> str | None:
113
+ dom = defusedxml.minidom.parseString(comments_path.read_text(encoding="utf-8"))
114
+ for c in dom.getElementsByTagName("w:comment"):
115
+ if c.getAttribute("w:id") == str(comment_id):
116
+ for p in c.getElementsByTagName("w:p"):
117
+ if pid := p.getAttribute("w14:paraId"):
118
+ return pid
119
+ return None
120
+
121
+
122
+ def _next_comment_id(comments_path: Path) -> int:
123
+ if not comments_path.exists():
124
+ return 0
125
+ dom = defusedxml.minidom.parseString(comments_path.read_text(encoding="utf-8"))
126
+ ids = []
127
+ for c in dom.getElementsByTagName("w:comment"):
128
+ try:
129
+ ids.append(int(c.getAttribute("w:id")))
130
+ except ValueError:
131
+ pass
132
+ return (max(ids) + 1) if ids else 0
133
+
134
+
135
+ def _get_next_rid(rels_path: Path) -> int:
136
+ dom = defusedxml.minidom.parseString(rels_path.read_text(encoding="utf-8"))
137
+ max_rid = 0
138
+ for rel in dom.getElementsByTagName("Relationship"):
139
+ rid = rel.getAttribute("Id")
140
+ if rid and rid.startswith("rId"):
141
+ try:
142
+ max_rid = max(max_rid, int(rid[3:]))
143
+ except ValueError:
144
+ pass
145
+ return max_rid + 1
146
+
147
+
148
+ def _has_relationship(rels_path: Path, target: str) -> bool:
149
+ dom = defusedxml.minidom.parseString(rels_path.read_text(encoding="utf-8"))
150
+ return any(
151
+ rel.getAttribute("Target") == target
152
+ for rel in dom.getElementsByTagName("Relationship")
153
+ )
154
+
155
+
156
+ def _has_content_type(ct_path: Path, part_name: str) -> bool:
157
+ dom = defusedxml.minidom.parseString(ct_path.read_text(encoding="utf-8"))
158
+ return any(
159
+ o.getAttribute("PartName") == part_name
160
+ for o in dom.getElementsByTagName("Override")
161
+ )
162
+
163
+
164
+ _COMMENT_RELS = [
165
+ ("http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", "comments.xml"),
166
+ ("http://schemas.microsoft.com/office/2011/relationships/commentsExtended", "commentsExtended.xml"),
167
+ ("http://schemas.microsoft.com/office/2016/09/relationships/commentsIds", "commentsIds.xml"),
168
+ ("http://schemas.microsoft.com/office/2018/08/relationships/commentsExtensible", "commentsExtensible.xml"),
169
+ ]
170
+ _COMMENT_OVERRIDES = [
171
+ ("/word/comments.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"),
172
+ ("/word/commentsExtended.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml"),
173
+ ("/word/commentsIds.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsIds+xml"),
174
+ ("/word/commentsExtensible.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtensible+xml"),
175
+ ]
176
+
177
+
178
+ def _ensure_comment_relationships(unpacked_dir: Path) -> None:
179
+ rels_path = unpacked_dir / "word" / "_rels" / "document.xml.rels"
180
+ if not rels_path.exists():
181
+ return
182
+ dom = defusedxml.minidom.parseString(rels_path.read_text(encoding="utf-8"))
183
+ root = dom.documentElement
184
+ comment_types = {rel_type for rel_type, _ in _COMMENT_RELS}
185
+ existing = set()
186
+ for rel in dom.getElementsByTagName("Relationship"):
187
+ if rel.getAttribute("Type") not in comment_types:
188
+ continue
189
+ part = opc_target(
190
+ rel.getAttribute("Target"),
191
+ "word/document.xml",
192
+ rel.getAttribute("TargetMode"),
193
+ )
194
+ if part is not None:
195
+ existing.add(part)
196
+ next_rid = _get_next_rid(rels_path)
197
+ changed = False
198
+ for rel_type, target in _COMMENT_RELS:
199
+ if opc_target(target, "word/document.xml") in existing:
200
+ continue
201
+ rel = dom.createElement("Relationship")
202
+ rel.setAttribute("Id", f"rId{next_rid}")
203
+ rel.setAttribute("Type", rel_type)
204
+ rel.setAttribute("Target", target)
205
+ root.appendChild(rel)
206
+ next_rid += 1
207
+ changed = True
208
+ if changed:
209
+ rels_path.write_bytes(dom.toxml(encoding="UTF-8"))
210
+
211
+
212
+ def _ensure_comment_content_types(unpacked_dir: Path) -> None:
213
+ ct_path = unpacked_dir / "[Content_Types].xml"
214
+ if not ct_path.exists():
215
+ return
216
+ dom = defusedxml.minidom.parseString(ct_path.read_text(encoding="utf-8"))
217
+ root = dom.documentElement
218
+ existing = {
219
+ o.getAttribute("PartName")
220
+ for o in dom.getElementsByTagName("Override")
221
+ }
222
+ changed = False
223
+ for part_name, content_type in _COMMENT_OVERRIDES:
224
+ if part_name in existing:
225
+ continue
226
+ override = dom.createElement("Override")
227
+ override.setAttribute("PartName", part_name)
228
+ override.setAttribute("ContentType", content_type)
229
+ root.appendChild(override)
230
+ changed = True
231
+ if changed:
232
+ ct_path.write_bytes(dom.toxml(encoding="UTF-8"))
233
+
234
+
235
+ def add_comment(
236
+ unpacked_dir: Path | str,
237
+ text: str,
238
+ comment_id: int | None = None,
239
+ author: str = "Claude",
240
+ initials: str = "C",
241
+ parent_id: int | None = None,
242
+ raw: bool = False,
243
+ ) -> tuple[int, str, str]:
244
+ unpacked_dir = Path(unpacked_dir)
245
+ if not raw:
246
+ text = xml_escape(text)
247
+ author = xml_escape(author, {'"': "&quot;"})
248
+ initials = xml_escape(initials, {'"': "&quot;"})
249
+ word = unpacked_dir / "word"
250
+ if not word.exists():
251
+ raise FileNotFoundError(f"{word} not found (not an unpacked .docx?)")
252
+
253
+ comments = word / "comments.xml"
254
+ if comment_id is None:
255
+ comment_id = _next_comment_id(comments)
256
+
257
+ parent_para = None
258
+ if parent_id is not None:
259
+ parent_para = _find_para_id(comments, parent_id) if comments.exists() else None
260
+ if not parent_para:
261
+ raise ValueError(f"parent comment {parent_id} not found")
262
+
263
+ para_id, durable_id = _generate_hex_id(), _generate_hex_id()
264
+ ts = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
265
+
266
+ if not comments.exists():
267
+ shutil.copy(TEMPLATE_DIR / "comments.xml", comments)
268
+ _ensure_comment_relationships(unpacked_dir)
269
+ _ensure_comment_content_types(unpacked_dir)
270
+ _append_xml(
271
+ comments,
272
+ "w:comments",
273
+ COMMENT_XML.format(
274
+ id=comment_id, author=author, date=ts, initials=initials,
275
+ para_id=para_id, text=text,
276
+ ),
277
+ )
278
+
279
+ ext = word / "commentsExtended.xml"
280
+ if not ext.exists():
281
+ shutil.copy(TEMPLATE_DIR / "commentsExtended.xml", ext)
282
+ if parent_para is not None:
283
+ _append_xml(
284
+ ext, "w15:commentsEx",
285
+ f'<w15:commentEx w15:paraId="{para_id}" w15:paraIdParent="{parent_para}" w15:done="0"/>',
286
+ )
287
+ else:
288
+ _append_xml(
289
+ ext, "w15:commentsEx",
290
+ f'<w15:commentEx w15:paraId="{para_id}" w15:done="0"/>',
291
+ )
292
+
293
+ ids = word / "commentsIds.xml"
294
+ if not ids.exists():
295
+ shutil.copy(TEMPLATE_DIR / "commentsIds.xml", ids)
296
+ _append_xml(
297
+ ids, "w16cid:commentsIds",
298
+ f'<w16cid:commentId w16cid:paraId="{para_id}" w16cid:durableId="{durable_id}"/>',
299
+ )
300
+
301
+ extensible = word / "commentsExtensible.xml"
302
+ if not extensible.exists():
303
+ shutil.copy(TEMPLATE_DIR / "commentsExtensible.xml", extensible)
304
+ _append_xml(
305
+ extensible, "w16cex:commentsExtensible",
306
+ f'<w16cex:commentExtensible w16cex:durableId="{durable_id}" w16cex:dateUtc="{ts}"/>',
307
+ )
308
+
309
+ action = "reply" if parent_id is not None else "comment"
310
+ return comment_id, para_id, f"Added {action} id={comment_id} (paraId={para_id})"
311
+
312
+
313
+ def main() -> None:
314
+ p = argparse.ArgumentParser(description="Add a comment to a DOCX (directory or .docx file).")
315
+ p.add_argument("input", help="Unpacked DOCX directory OR a .docx/.dotx file")
316
+ p.add_argument("text", help="Comment text (plain text; XML-escaped automatically)")
317
+ p.add_argument("--raw", action="store_true",
318
+ help="Treat text as pre-escaped XML (skip automatic escaping)")
319
+ p.add_argument("--id", type=int, dest="comment_id",
320
+ help="Comment ID (default: auto-assign as max existing + 1)")
321
+ p.add_argument("--author", default="Claude", help="Author name")
322
+ p.add_argument("--initials", default="C", help="Author initials")
323
+ p.add_argument("--parent", type=int, help="Parent comment ID (makes this a reply)")
324
+ p.add_argument("-o", "--output",
325
+ help="Output .docx path (only used when input is a .docx; default: overwrite input)")
326
+ args = p.parse_args()
327
+
328
+ src = Path(args.input)
329
+
330
+ try:
331
+ if src.is_dir():
332
+ if args.output:
333
+ print("Warning: --output ignored for directory input", file=sys.stderr)
334
+ cid, _, msg = add_comment(
335
+ src, args.text, comment_id=args.comment_id,
336
+ author=args.author, initials=args.initials,
337
+ parent_id=args.parent, raw=args.raw,
338
+ )
339
+ print(msg)
340
+ elif src.is_file() and src.suffix.lower() in (".docx", ".dotx"):
341
+ out = Path(args.output) if args.output else src
342
+ with tempfile.TemporaryDirectory() as tmp:
343
+ tmp_path = Path(tmp)
344
+ with zipfile.ZipFile(src) as zf:
345
+ _safe_extract(zf, tmp_path)
346
+ cid, _, msg = add_comment(
347
+ tmp_path, args.text, comment_id=args.comment_id,
348
+ author=args.author, initials=args.initials,
349
+ parent_id=args.parent, raw=args.raw,
350
+ )
351
+ _rezip(tmp_path, out)
352
+ print(msg)
353
+ print(f"Wrote {out} (comment defined; add markers to word/document.xml to make it visible)")
354
+ else:
355
+ print(f"Error: {src} is neither a directory nor a .docx/.dotx file", file=sys.stderr)
356
+ sys.exit(1)
357
+ except (FileNotFoundError, ValueError, zipfile.BadZipFile, ExpatError) as e:
358
+ print(f"Error: {e}", file=sys.stderr)
359
+ sys.exit(1)
360
+
361
+ if args.parent is not None:
362
+ print(REPLY_MARKER_TEMPLATE.format(pid=args.parent, cid=cid))
363
+ else:
364
+ print(COMMENT_MARKER_TEMPLATE.format(cid=cid))
365
+
366
+
367
+ if __name__ == "__main__":
368
+ main()