@mseep/open-computer-use 1.0.0

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 (769) hide show
  1. package/.coderabbit.yaml +25 -0
  2. package/.dockerignore +95 -0
  3. package/.env.example +137 -0
  4. package/.githooks/pre-commit +68 -0
  5. package/.github/CODEOWNERS +125 -0
  6. package/.github/ISSUE_TEMPLATE/adr-proposal.md +41 -0
  7. package/.github/ISSUE_TEMPLATE/bug-report.md +49 -0
  8. package/.github/ISSUE_TEMPLATE/component-proposal.md +38 -0
  9. package/.github/ISSUE_TEMPLATE/config.yml +15 -0
  10. package/.github/ISSUE_TEMPLATE/dependency-proposal.md +59 -0
  11. package/.github/ISSUE_TEMPLATE/feature_request.md +15 -0
  12. package/.github/ISSUE_TEMPLATE/nfr-proposal.md +44 -0
  13. package/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  14. package/.github/codeql/codeql-config.yml +11 -0
  15. package/.github/codeql/extensions/security-models/python-sanitizers.model.yml +17 -0
  16. package/.github/codeql/extensions/security-models/qlpack.yml +7 -0
  17. package/.github/dependabot.yml +23 -0
  18. package/.github/security-exceptions.yml +23 -0
  19. package/.github/workflows/build.yml +420 -0
  20. package/.github/workflows/codeql.yml +33 -0
  21. package/.github/workflows/contracts-lint.yml +90 -0
  22. package/.github/workflows/docs-lint.yml +151 -0
  23. package/.github/workflows/helm.yml +131 -0
  24. package/.github/workflows/identity-lint.yml +30 -0
  25. package/.github/workflows/release-chart.yml +177 -0
  26. package/.github/workflows/release.yml +95 -0
  27. package/.github/workflows/security.yml +332 -0
  28. package/.github/workflows/stale.yml +31 -0
  29. package/.github/workflows/supply-chain.yml +242 -0
  30. package/.gitleaks.toml +53 -0
  31. package/.markdownlint.yaml +51 -0
  32. package/.semgrepignore +85 -0
  33. package/.vale/styles/Architecture/ap13-data-class-substrate.yml +12 -0
  34. package/.vale/styles/Architecture/banned-phrases.yml +23 -0
  35. package/.vale/styles/Architecture/banned-vocab.yml +23 -0
  36. package/.vale/styles/Architecture/marketing-tone.yml +19 -0
  37. package/.vale.ini +18 -0
  38. package/CHANGELOG.md +411 -0
  39. package/CLAUDE.md +218 -0
  40. package/CONTRIBUTING.md +82 -0
  41. package/Dockerfile +676 -0
  42. package/LICENSE +98 -0
  43. package/LICENSE-APACHE +202 -0
  44. package/LICENSE-MIT +21 -0
  45. package/NOTICE +36 -0
  46. package/README.md +516 -0
  47. package/SECURITY.md +45 -0
  48. package/THIRD-PARTY-LICENSES.md +14 -0
  49. package/apt-packages.txt +108 -0
  50. package/computer-use-server/.dockerignore +13 -0
  51. package/computer-use-server/Dockerfile +44 -0
  52. package/computer-use-server/README.md +84 -0
  53. package/computer-use-server/app.py +1544 -0
  54. package/computer-use-server/bin/list-subagent-models +449 -0
  55. package/computer-use-server/cli-defaults/README.md +31 -0
  56. package/computer-use-server/cli-defaults/codex.json +7 -0
  57. package/computer-use-server/cli-defaults/opencode.json +18 -0
  58. package/computer-use-server/cli_adapters/__init__.py +46 -0
  59. package/computer-use-server/cli_adapters/claude.py +163 -0
  60. package/computer-use-server/cli_adapters/codex.py +163 -0
  61. package/computer-use-server/cli_adapters/opencode.py +169 -0
  62. package/computer-use-server/cli_adapters/result.py +34 -0
  63. package/computer-use-server/cli_runtime.py +316 -0
  64. package/computer-use-server/context_vars.py +24 -0
  65. package/computer-use-server/docker_manager.py +1100 -0
  66. package/computer-use-server/docs_html.py +12 -0
  67. package/computer-use-server/mcp_resources.py +170 -0
  68. package/computer-use-server/mcp_tools.py +1430 -0
  69. package/computer-use-server/requirements.txt +17 -0
  70. package/computer-use-server/security.py +50 -0
  71. package/computer-use-server/skill_manager.py +664 -0
  72. package/computer-use-server/static/browser-viewer.js +445 -0
  73. package/computer-use-server/static/chart.umd.js +14 -0
  74. package/computer-use-server/static/docs.html +203 -0
  75. package/computer-use-server/static/github-dark.min.css +10 -0
  76. package/computer-use-server/static/github.min.css +10 -0
  77. package/computer-use-server/static/highlight.min.js +1213 -0
  78. package/computer-use-server/static/highlightjs-line-numbers.min.js +1 -0
  79. package/computer-use-server/static/icons.js +74 -0
  80. package/computer-use-server/static/jszip.min.js +13 -0
  81. package/computer-use-server/static/katex/auto-render.min.js +1 -0
  82. package/computer-use-server/static/katex/fonts/KaTeX_AMS-Regular.ttf +0 -0
  83. package/computer-use-server/static/katex/fonts/KaTeX_AMS-Regular.woff +0 -0
  84. package/computer-use-server/static/katex/fonts/KaTeX_AMS-Regular.woff2 +0 -0
  85. package/computer-use-server/static/katex/fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
  86. package/computer-use-server/static/katex/fonts/KaTeX_Caligraphic-Bold.woff +0 -0
  87. package/computer-use-server/static/katex/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
  88. package/computer-use-server/static/katex/fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
  89. package/computer-use-server/static/katex/fonts/KaTeX_Caligraphic-Regular.woff +0 -0
  90. package/computer-use-server/static/katex/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
  91. package/computer-use-server/static/katex/fonts/KaTeX_Fraktur-Bold.ttf +0 -0
  92. package/computer-use-server/static/katex/fonts/KaTeX_Fraktur-Bold.woff +0 -0
  93. package/computer-use-server/static/katex/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
  94. package/computer-use-server/static/katex/fonts/KaTeX_Fraktur-Regular.ttf +0 -0
  95. package/computer-use-server/static/katex/fonts/KaTeX_Fraktur-Regular.woff +0 -0
  96. package/computer-use-server/static/katex/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
  97. package/computer-use-server/static/katex/fonts/KaTeX_Main-Bold.ttf +0 -0
  98. package/computer-use-server/static/katex/fonts/KaTeX_Main-Bold.woff +0 -0
  99. package/computer-use-server/static/katex/fonts/KaTeX_Main-Bold.woff2 +0 -0
  100. package/computer-use-server/static/katex/fonts/KaTeX_Main-BoldItalic.ttf +0 -0
  101. package/computer-use-server/static/katex/fonts/KaTeX_Main-BoldItalic.woff +0 -0
  102. package/computer-use-server/static/katex/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
  103. package/computer-use-server/static/katex/fonts/KaTeX_Main-Italic.ttf +0 -0
  104. package/computer-use-server/static/katex/fonts/KaTeX_Main-Italic.woff +0 -0
  105. package/computer-use-server/static/katex/fonts/KaTeX_Main-Italic.woff2 +0 -0
  106. package/computer-use-server/static/katex/fonts/KaTeX_Main-Regular.ttf +0 -0
  107. package/computer-use-server/static/katex/fonts/KaTeX_Main-Regular.woff +0 -0
  108. package/computer-use-server/static/katex/fonts/KaTeX_Main-Regular.woff2 +0 -0
  109. package/computer-use-server/static/katex/fonts/KaTeX_Math-BoldItalic.ttf +0 -0
  110. package/computer-use-server/static/katex/fonts/KaTeX_Math-BoldItalic.woff +0 -0
  111. package/computer-use-server/static/katex/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
  112. package/computer-use-server/static/katex/fonts/KaTeX_Math-Italic.ttf +0 -0
  113. package/computer-use-server/static/katex/fonts/KaTeX_Math-Italic.woff +0 -0
  114. package/computer-use-server/static/katex/fonts/KaTeX_Math-Italic.woff2 +0 -0
  115. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Bold.ttf +0 -0
  116. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Bold.woff +0 -0
  117. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
  118. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Italic.ttf +0 -0
  119. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Italic.woff +0 -0
  120. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
  121. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Regular.ttf +0 -0
  122. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Regular.woff +0 -0
  123. package/computer-use-server/static/katex/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
  124. package/computer-use-server/static/katex/fonts/KaTeX_Script-Regular.ttf +0 -0
  125. package/computer-use-server/static/katex/fonts/KaTeX_Script-Regular.woff +0 -0
  126. package/computer-use-server/static/katex/fonts/KaTeX_Script-Regular.woff2 +0 -0
  127. package/computer-use-server/static/katex/fonts/KaTeX_Size1-Regular.ttf +0 -0
  128. package/computer-use-server/static/katex/fonts/KaTeX_Size1-Regular.woff +0 -0
  129. package/computer-use-server/static/katex/fonts/KaTeX_Size1-Regular.woff2 +0 -0
  130. package/computer-use-server/static/katex/fonts/KaTeX_Size2-Regular.ttf +0 -0
  131. package/computer-use-server/static/katex/fonts/KaTeX_Size2-Regular.woff +0 -0
  132. package/computer-use-server/static/katex/fonts/KaTeX_Size2-Regular.woff2 +0 -0
  133. package/computer-use-server/static/katex/fonts/KaTeX_Size3-Regular.ttf +0 -0
  134. package/computer-use-server/static/katex/fonts/KaTeX_Size3-Regular.woff +0 -0
  135. package/computer-use-server/static/katex/fonts/KaTeX_Size3-Regular.woff2 +0 -0
  136. package/computer-use-server/static/katex/fonts/KaTeX_Size4-Regular.ttf +0 -0
  137. package/computer-use-server/static/katex/fonts/KaTeX_Size4-Regular.woff +0 -0
  138. package/computer-use-server/static/katex/fonts/KaTeX_Size4-Regular.woff2 +0 -0
  139. package/computer-use-server/static/katex/fonts/KaTeX_Typewriter-Regular.ttf +0 -0
  140. package/computer-use-server/static/katex/fonts/KaTeX_Typewriter-Regular.woff +0 -0
  141. package/computer-use-server/static/katex/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
  142. package/computer-use-server/static/katex/katex.min.css +1 -0
  143. package/computer-use-server/static/katex/katex.min.js +1 -0
  144. package/computer-use-server/static/locale.js +242 -0
  145. package/computer-use-server/static/mammoth.browser.min.js +21 -0
  146. package/computer-use-server/static/marked.min.js +6 -0
  147. package/computer-use-server/static/mermaid.min.js +2811 -0
  148. package/computer-use-server/static/pdf.min.js +22 -0
  149. package/computer-use-server/static/pdf.worker.min.js +22 -0
  150. package/computer-use-server/static/pptxviewjs.min.js +1 -0
  151. package/computer-use-server/static/preact-htm.min.js +1 -0
  152. package/computer-use-server/static/preview.css +1030 -0
  153. package/computer-use-server/static/preview.js +1522 -0
  154. package/computer-use-server/static/xlsx.full.min.js +22 -0
  155. package/computer-use-server/static/xterm-addon-fit.min.js +2 -0
  156. package/computer-use-server/static/xterm-addon-web-links.min.js +2 -0
  157. package/computer-use-server/static/xterm.css +218 -0
  158. package/computer-use-server/static/xterm.min.js +2 -0
  159. package/computer-use-server/system_prompt.py +761 -0
  160. package/computer-use-server/uploads.py +82 -0
  161. package/contracts/README.md +53 -0
  162. package/contracts/audit/audit-fanin.asyncapi.yaml +407 -0
  163. package/contracts/exec/exec-channel.schema.json +240 -0
  164. package/contracts/mcp/2025-06-18/ocu-constraints.schema.json +178 -0
  165. package/contracts/storage/file-artifact-api.schema.json +390 -0
  166. package/contracts/storage/file-ops.schema.json +217 -0
  167. package/contracts/storage/mount-config.schema.json +197 -0
  168. package/cron/Dockerfile +15 -0
  169. package/cron/cleanup-quick.sh +21 -0
  170. package/cron/cleanup.sh +127 -0
  171. package/data/outputs/.gitkeep +0 -0
  172. package/data/uploads/.gitkeep +0 -0
  173. package/docker-compose.test.yml +54 -0
  174. package/docker-compose.webui.yml +77 -0
  175. package/docker-compose.yml +96 -0
  176. package/docs/CLOUD.md +29 -0
  177. package/docs/COMPARISON.md +128 -0
  178. package/docs/DOCKER.md +469 -0
  179. package/docs/DYNAMIC-SKILLS.md +77 -0
  180. package/docs/FEATURES.md +100 -0
  181. package/docs/INSTALL.md +111 -0
  182. package/docs/KNOWN-BUGS.md +86 -0
  183. package/docs/MCP.md +320 -0
  184. package/docs/SCREENSHOTS.md +39 -0
  185. package/docs/SKILLS-USER-GUIDE.md +86 -0
  186. package/docs/SKILLS.md +483 -0
  187. package/docs/TERMINAL-TAB.md +56 -0
  188. package/docs/architecture/02-trust-boundaries.md +224 -0
  189. package/docs/architecture/03-c4-context.md +61 -0
  190. package/docs/architecture/04-bounded-contexts.md +119 -0
  191. package/docs/architecture/05-c4-container.md +88 -0
  192. package/docs/architecture/06-threat-model.md +172 -0
  193. package/docs/architecture/08-contracts.md +105 -0
  194. package/docs/architecture/MANIFESTO.md +38 -0
  195. package/docs/architecture/PROCESS.md +64 -0
  196. package/docs/architecture/README.md +37 -0
  197. package/docs/architecture/adr/0000-template.md +65 -0
  198. package/docs/architecture/adr/0001-layer-0-gate-legacy-exclusion.md +75 -0
  199. package/docs/architecture/adr/0002-session-view-descriptor.md +57 -0
  200. package/docs/architecture/adr/0003-sandbox-runtime-tier-ladder.md +63 -0
  201. package/docs/architecture/adr/0004-operator-authentication-substrate.md +63 -0
  202. package/docs/architecture/adr/0005-egress-credential-delivery-envoy-sds.md +62 -0
  203. package/docs/architecture/adr/0006-egress-forward-proxy-substrate.md +65 -0
  204. package/docs/architecture/adr/0007-egress-auth-mechanism.md +72 -0
  205. package/docs/architecture/adr/0008-session-egress-attribution.md +59 -0
  206. package/docs/architecture/adr/0009-audit-pipeline-pluggable-by-contract.md +76 -0
  207. package/docs/architecture/adr/0010-storage-backend-pluggable-adapter.md +60 -0
  208. package/docs/architecture/adr/0011-storage-egress-lane.md +67 -0
  209. package/docs/architecture/adr/0012-implementation-language.md +67 -0
  210. package/docs/architecture/adr/0020-sandbox-image-provisioning.md +82 -0
  211. package/docs/architecture/adr/README.md +53 -0
  212. package/docs/architecture/compliance/.gitkeep +0 -0
  213. package/docs/architecture/components/00-overview.md +42 -0
  214. package/docs/architecture/components/0000-template.md +50 -0
  215. package/docs/architecture/components/01-mcp-gateway.md +80 -0
  216. package/docs/architecture/components/02-control-operator-api.md +80 -0
  217. package/docs/architecture/components/04-storage-broker.md +104 -0
  218. package/docs/architecture/components/05-session-sandbox.md +93 -0
  219. package/docs/architecture/components/06-egress-trust-edge.md +95 -0
  220. package/docs/architecture/components/07-audit-pipeline.md +110 -0
  221. package/docs/architecture/diagrams/.gitkeep +0 -0
  222. package/docs/architecture/diagrams/02-trust-boundaries.mmd +111 -0
  223. package/docs/architecture/diagrams/06-threat-model.mmd +41 -0
  224. package/docs/architecture/diagrams/08-contracts.mmd +47 -0
  225. package/docs/architecture/diagrams/c4-container.mmd +59 -0
  226. package/docs/architecture/diagrams/c4-context.mmd +46 -0
  227. package/docs/architecture/glossary.md +172 -0
  228. package/docs/architecture/manifesto/.gitkeep +0 -0
  229. package/docs/architecture/manifesto/01-audience-and-buyer.md +57 -0
  230. package/docs/architecture/manifesto/02-nfrs.md +325 -0
  231. package/docs/architecture/manifesto/03-non-negotiables.md +35 -0
  232. package/docs/architecture/manifesto/04-non-goals.md +23 -0
  233. package/docs/architecture/manifesto/05-licensing-posture.md +61 -0
  234. package/docs/architecture/manifesto/06-starter-mode-policy.md +49 -0
  235. package/docs/architecture/manifesto/07-governance.md +60 -0
  236. package/docs/architecture/primitives-backlog.md +51 -0
  237. package/docs/architecture.svg +117 -0
  238. package/docs/claude-code-gateway.md +173 -0
  239. package/docs/cli-config-templates.md +240 -0
  240. package/docs/data-flow.svg +72 -0
  241. package/docs/demo-landing-page.gif +0 -0
  242. package/docs/demo-qwen-trending.gif +0 -0
  243. package/docs/dynamic-skills.svg +77 -0
  244. package/docs/file-flow.svg +126 -0
  245. package/docs/future-architecture/README.md +152 -0
  246. package/docs/future-architecture/adr/0001-control-plane-language-go.md +80 -0
  247. package/docs/future-architecture/adr/0002-guest-agent-language-go.md +84 -0
  248. package/docs/future-architecture/adr/0003-docker-poc-first-then-k8s.md +37 -0
  249. package/docs/future-architecture/adr/0004-pluggable-runtime-via-runtimeclass.md +34 -0
  250. package/docs/future-architecture/adr/0005-mcp-as-control-plane-gateway.md +34 -0
  251. package/docs/future-architecture/adr/0006-no-agpl-no-bsl-dependencies.md +41 -0
  252. package/docs/future-architecture/adr/0007-superseded-by-future-architecture.md +37 -0
  253. package/docs/future-architecture/adr/0008-internal-grpc-external-rest-mcp.md +106 -0
  254. package/docs/future-architecture/adr/0009-external-protocol-dialects.md +94 -0
  255. package/docs/future-architecture/adr/0010-lambda-as-inspiration-not-runtime.md +86 -0
  256. package/docs/future-architecture/adr/0011-kata-as-first-class-dind-runtime.md +84 -0
  257. package/docs/future-architecture/antipatterns.md +552 -0
  258. package/docs/future-architecture/architecture/01-layers.md +109 -0
  259. package/docs/future-architecture/architecture/02-layer4-control-plane.md +122 -0
  260. package/docs/future-architecture/architecture/03-layer3-providers.md +174 -0
  261. package/docs/future-architecture/architecture/04-layer2-runtimes.md +114 -0
  262. package/docs/future-architecture/architecture/04b-credential-broker.md +153 -0
  263. package/docs/future-architecture/architecture/05-layer1-guest-agent.md +138 -0
  264. package/docs/future-architecture/architecture/06-storage.md +134 -0
  265. package/docs/future-architecture/architecture/07-security.md +194 -0
  266. package/docs/future-architecture/architecture/08-networking.md +149 -0
  267. package/docs/future-architecture/architecture/09-templates.md +122 -0
  268. package/docs/future-architecture/architecture/10-observability.md +121 -0
  269. package/docs/future-architecture/design-notes.md +72 -0
  270. package/docs/future-architecture/gaps.md +281 -0
  271. package/docs/future-architecture/phase-template.md +123 -0
  272. package/docs/future-architecture/references.md +225 -0
  273. package/docs/future-architecture/research/01-kata-containers.md +100 -0
  274. package/docs/future-architecture/research/02-e2b-infra.md +133 -0
  275. package/docs/future-architecture/research/03-coder.md +115 -0
  276. package/docs/future-architecture/research/04-cloud-hypervisor.md +99 -0
  277. package/docs/future-architecture/research/05-firecracker.md +114 -0
  278. package/docs/future-architecture/research/06-agent-sandbox.md +142 -0
  279. package/docs/future-architecture/research/07-chromedp.md +78 -0
  280. package/docs/future-architecture/research/08-microsandbox.md +78 -0
  281. package/docs/future-architecture/research/09-agentbox.md +135 -0
  282. package/docs/future-architecture/research/10-sysbox.md +100 -0
  283. package/docs/future-architecture/research/11-firecracker-containerd.md +93 -0
  284. package/docs/future-architecture/research/12-docker-socket-proxy.md +59 -0
  285. package/docs/future-architecture/research/14-e2b-desktop-and-surf.md +107 -0
  286. package/docs/future-architecture/research/18-open-webui-terminals-observed.md +135 -0
  287. package/docs/future-architecture/research/bank-buyer.md +96 -0
  288. package/docs/future-architecture/research/enthusiast-audience.md +106 -0
  289. package/docs/future-architecture/research/proof-uipath-anthropic-2026-05.md +76 -0
  290. package/docs/future-architecture/research/widemoat-thesis-advisor.md +124 -0
  291. package/docs/future-architecture/roadmap.md +438 -0
  292. package/docs/kata-runtime.md +267 -0
  293. package/docs/kubernetes.md +86 -0
  294. package/docs/logo.png +0 -0
  295. package/docs/multi-cli.md +161 -0
  296. package/docs/openwebui-filter.md +134 -0
  297. package/docs/roadmap/implementation-roadmap.md +104 -0
  298. package/docs/sandbox-contents.svg +229 -0
  299. package/docs/screenshots/01-create-document.png +0 -0
  300. package/docs/screenshots/02-file-preview.png +0 -0
  301. package/docs/screenshots/03-browser-viewer.png +0 -0
  302. package/docs/screenshots/04-sub-agent-terminal.png +0 -0
  303. package/docs/screenshots/05-chat-overview.png +0 -0
  304. package/docs/screenshots/06-sub-agent-dashboard.png +0 -0
  305. package/docs/screenshots/07-frontend-design-skill.png +0 -0
  306. package/docs/screenshots/08-pptx-skill.png +0 -0
  307. package/docs/screenshots/09-skill-creator.png +0 -0
  308. package/docs/screenshots/10-data-chart.png +0 -0
  309. package/docs/shared-browser.svg +102 -0
  310. package/docs/system-prompt.md +113 -0
  311. package/docs/terminal-flow.svg +69 -0
  312. package/examples/helm/README.md +20 -0
  313. package/examples/helm/standalone/values.yaml +49 -0
  314. package/examples/helm/with-open-webui/README.md +99 -0
  315. package/examples/helm/with-open-webui/values-computer-use.yaml +32 -0
  316. package/examples/helm/with-open-webui/values-open-webui.yaml +67 -0
  317. package/fonts/NotoEmoji-Regular.ttf +0 -0
  318. package/helm/computer-use-server/.helmignore +17 -0
  319. package/helm/computer-use-server/Chart.yaml +32 -0
  320. package/helm/computer-use-server/README.md +211 -0
  321. package/helm/computer-use-server/templates/NOTES.txt +66 -0
  322. package/helm/computer-use-server/templates/_helpers.tpl +115 -0
  323. package/helm/computer-use-server/templates/configmap-dind-init.yaml +82 -0
  324. package/helm/computer-use-server/templates/configmap.yaml +18 -0
  325. package/helm/computer-use-server/templates/deployment.yaml +248 -0
  326. package/helm/computer-use-server/templates/ingress.yaml +38 -0
  327. package/helm/computer-use-server/templates/networkpolicy.yaml +50 -0
  328. package/helm/computer-use-server/templates/pdb.yaml +16 -0
  329. package/helm/computer-use-server/templates/pvc-data.yaml +20 -0
  330. package/helm/computer-use-server/templates/pvc-skills-cache.yaml +20 -0
  331. package/helm/computer-use-server/templates/pvc-user-data.yaml +20 -0
  332. package/helm/computer-use-server/templates/pvc-var-lib-docker.yaml +27 -0
  333. package/helm/computer-use-server/templates/secret.yaml +23 -0
  334. package/helm/computer-use-server/templates/service.yaml +22 -0
  335. package/helm/computer-use-server/templates/serviceaccount.yaml +15 -0
  336. package/helm/computer-use-server/templates/tests/test-health.yaml +23 -0
  337. package/helm/computer-use-server/values.schema.json +183 -0
  338. package/helm/computer-use-server/values.yaml +297 -0
  339. package/lychee.toml +36 -0
  340. package/openwebui/Dockerfile +52 -0
  341. package/openwebui/README.md +38 -0
  342. package/openwebui/functions/README.md +48 -0
  343. package/openwebui/functions/computer_link_filter.py +487 -0
  344. package/openwebui/init.sh +305 -0
  345. package/openwebui/patches/README.md +44 -0
  346. package/openwebui/patches/fix_artifacts_auto_show.py +441 -0
  347. package/openwebui/patches/fix_attached_files_position.py +87 -0
  348. package/openwebui/patches/fix_large_tool_args.py +156 -0
  349. package/openwebui/patches/fix_large_tool_results.py +289 -0
  350. package/openwebui/patches/fix_preview_url_detection.py +230 -0
  351. package/openwebui/patches/fix_skip_embedding_chat_files.py +229 -0
  352. package/openwebui/patches/fix_skip_rag_files_native_fc.py +100 -0
  353. package/openwebui/patches/fix_tool_loop_errors.py +510 -0
  354. package/package.json +39 -0
  355. package/requirements.txt +112 -0
  356. package/scripts/check-config.sh +141 -0
  357. package/scripts/docs-lint/ai-slop-detector.sh +202 -0
  358. package/scripts/docs-lint/architecture-tree-whitelist.sh +131 -0
  359. package/scripts/docs-lint/ascii-diagram-detector.sh +58 -0
  360. package/scripts/docs-lint/front-matter-validator.sh +97 -0
  361. package/scripts/docs-lint/gitignored-ref-detector.sh +122 -0
  362. package/scripts/docs-lint/identity-email-detector.sh +48 -0
  363. package/scripts/docs-lint/test-linters.sh +354 -0
  364. package/scripts/docs-lint/wc-budget.sh +61 -0
  365. package/scripts/githooks/pre-push +75 -0
  366. package/server.json +13 -0
  367. package/settings-wrapper/Dockerfile +9 -0
  368. package/settings-wrapper/README.md +119 -0
  369. package/settings-wrapper/app.py +113 -0
  370. package/settings-wrapper/requirements.txt +2 -0
  371. package/settings-wrapper/skills.json +25 -0
  372. package/skills/README.md +46 -0
  373. package/skills/examples/algorithmic-art/SKILL.md +405 -0
  374. package/skills/examples/algorithmic-art/templates/generator_template.js +223 -0
  375. package/skills/examples/algorithmic-art/templates/viewer.html +601 -0
  376. package/skills/examples/artifacts-builder/SKILL.md +74 -0
  377. package/skills/examples/artifacts-builder/scripts/bundle-artifact.sh +54 -0
  378. package/skills/examples/artifacts-builder/scripts/init-artifact.sh +322 -0
  379. package/skills/examples/artifacts-builder/scripts/shadcn-components.tar.gz +0 -0
  380. package/skills/examples/canvas-design/LICENSE.txt +202 -0
  381. package/skills/examples/canvas-design/SKILL.md +130 -0
  382. package/skills/examples/canvas-design/canvas-fonts/ArsenalSC-OFL.txt +93 -0
  383. package/skills/examples/canvas-design/canvas-fonts/ArsenalSC-Regular.ttf +0 -0
  384. package/skills/examples/canvas-design/canvas-fonts/BigShoulders-Bold.ttf +0 -0
  385. package/skills/examples/canvas-design/canvas-fonts/BigShoulders-OFL.txt +93 -0
  386. package/skills/examples/canvas-design/canvas-fonts/BigShoulders-Regular.ttf +0 -0
  387. package/skills/examples/canvas-design/canvas-fonts/Boldonse-OFL.txt +93 -0
  388. package/skills/examples/canvas-design/canvas-fonts/Boldonse-Regular.ttf +0 -0
  389. package/skills/examples/canvas-design/canvas-fonts/BricolageGrotesque-Bold.ttf +0 -0
  390. package/skills/examples/canvas-design/canvas-fonts/BricolageGrotesque-OFL.txt +93 -0
  391. package/skills/examples/canvas-design/canvas-fonts/BricolageGrotesque-Regular.ttf +0 -0
  392. package/skills/examples/canvas-design/canvas-fonts/CrimsonPro-Bold.ttf +0 -0
  393. package/skills/examples/canvas-design/canvas-fonts/CrimsonPro-Italic.ttf +0 -0
  394. package/skills/examples/canvas-design/canvas-fonts/CrimsonPro-OFL.txt +93 -0
  395. package/skills/examples/canvas-design/canvas-fonts/CrimsonPro-Regular.ttf +0 -0
  396. package/skills/examples/canvas-design/canvas-fonts/DMMono-OFL.txt +93 -0
  397. package/skills/examples/canvas-design/canvas-fonts/DMMono-Regular.ttf +0 -0
  398. package/skills/examples/canvas-design/canvas-fonts/EricaOne-OFL.txt +94 -0
  399. package/skills/examples/canvas-design/canvas-fonts/EricaOne-Regular.ttf +0 -0
  400. package/skills/examples/canvas-design/canvas-fonts/GeistMono-Bold.ttf +0 -0
  401. package/skills/examples/canvas-design/canvas-fonts/GeistMono-OFL.txt +93 -0
  402. package/skills/examples/canvas-design/canvas-fonts/GeistMono-Regular.ttf +0 -0
  403. package/skills/examples/canvas-design/canvas-fonts/Gloock-OFL.txt +93 -0
  404. package/skills/examples/canvas-design/canvas-fonts/Gloock-Regular.ttf +0 -0
  405. package/skills/examples/canvas-design/canvas-fonts/IBMPlexMono-Bold.ttf +0 -0
  406. package/skills/examples/canvas-design/canvas-fonts/IBMPlexMono-OFL.txt +93 -0
  407. package/skills/examples/canvas-design/canvas-fonts/IBMPlexMono-Regular.ttf +0 -0
  408. package/skills/examples/canvas-design/canvas-fonts/IBMPlexSerif-Bold.ttf +0 -0
  409. package/skills/examples/canvas-design/canvas-fonts/IBMPlexSerif-BoldItalic.ttf +0 -0
  410. package/skills/examples/canvas-design/canvas-fonts/IBMPlexSerif-Italic.ttf +0 -0
  411. package/skills/examples/canvas-design/canvas-fonts/IBMPlexSerif-Regular.ttf +0 -0
  412. package/skills/examples/canvas-design/canvas-fonts/InstrumentSans-Bold.ttf +0 -0
  413. package/skills/examples/canvas-design/canvas-fonts/InstrumentSans-BoldItalic.ttf +0 -0
  414. package/skills/examples/canvas-design/canvas-fonts/InstrumentSans-Italic.ttf +0 -0
  415. package/skills/examples/canvas-design/canvas-fonts/InstrumentSans-OFL.txt +93 -0
  416. package/skills/examples/canvas-design/canvas-fonts/InstrumentSans-Regular.ttf +0 -0
  417. package/skills/examples/canvas-design/canvas-fonts/InstrumentSerif-Italic.ttf +0 -0
  418. package/skills/examples/canvas-design/canvas-fonts/InstrumentSerif-Regular.ttf +0 -0
  419. package/skills/examples/canvas-design/canvas-fonts/Italiana-OFL.txt +93 -0
  420. package/skills/examples/canvas-design/canvas-fonts/Italiana-Regular.ttf +0 -0
  421. package/skills/examples/canvas-design/canvas-fonts/JetBrainsMono-Bold.ttf +0 -0
  422. package/skills/examples/canvas-design/canvas-fonts/JetBrainsMono-OFL.txt +93 -0
  423. package/skills/examples/canvas-design/canvas-fonts/JetBrainsMono-Regular.ttf +0 -0
  424. package/skills/examples/canvas-design/canvas-fonts/Jura-Light.ttf +0 -0
  425. package/skills/examples/canvas-design/canvas-fonts/Jura-Medium.ttf +0 -0
  426. package/skills/examples/canvas-design/canvas-fonts/Jura-OFL.txt +93 -0
  427. package/skills/examples/canvas-design/canvas-fonts/LibreBaskerville-OFL.txt +93 -0
  428. package/skills/examples/canvas-design/canvas-fonts/LibreBaskerville-Regular.ttf +0 -0
  429. package/skills/examples/canvas-design/canvas-fonts/Lora-Bold.ttf +0 -0
  430. package/skills/examples/canvas-design/canvas-fonts/Lora-BoldItalic.ttf +0 -0
  431. package/skills/examples/canvas-design/canvas-fonts/Lora-Italic.ttf +0 -0
  432. package/skills/examples/canvas-design/canvas-fonts/Lora-OFL.txt +93 -0
  433. package/skills/examples/canvas-design/canvas-fonts/Lora-Regular.ttf +0 -0
  434. package/skills/examples/canvas-design/canvas-fonts/NationalPark-Bold.ttf +0 -0
  435. package/skills/examples/canvas-design/canvas-fonts/NationalPark-OFL.txt +93 -0
  436. package/skills/examples/canvas-design/canvas-fonts/NationalPark-Regular.ttf +0 -0
  437. package/skills/examples/canvas-design/canvas-fonts/NothingYouCouldDo-OFL.txt +93 -0
  438. package/skills/examples/canvas-design/canvas-fonts/NothingYouCouldDo-Regular.ttf +0 -0
  439. package/skills/examples/canvas-design/canvas-fonts/Outfit-Bold.ttf +0 -0
  440. package/skills/examples/canvas-design/canvas-fonts/Outfit-OFL.txt +93 -0
  441. package/skills/examples/canvas-design/canvas-fonts/Outfit-Regular.ttf +0 -0
  442. package/skills/examples/canvas-design/canvas-fonts/PixelifySans-Medium.ttf +0 -0
  443. package/skills/examples/canvas-design/canvas-fonts/PixelifySans-OFL.txt +93 -0
  444. package/skills/examples/canvas-design/canvas-fonts/PoiretOne-OFL.txt +93 -0
  445. package/skills/examples/canvas-design/canvas-fonts/PoiretOne-Regular.ttf +0 -0
  446. package/skills/examples/canvas-design/canvas-fonts/RedHatMono-Bold.ttf +0 -0
  447. package/skills/examples/canvas-design/canvas-fonts/RedHatMono-OFL.txt +93 -0
  448. package/skills/examples/canvas-design/canvas-fonts/RedHatMono-Regular.ttf +0 -0
  449. package/skills/examples/canvas-design/canvas-fonts/Silkscreen-OFL.txt +93 -0
  450. package/skills/examples/canvas-design/canvas-fonts/Silkscreen-Regular.ttf +0 -0
  451. package/skills/examples/canvas-design/canvas-fonts/SmoochSans-Medium.ttf +0 -0
  452. package/skills/examples/canvas-design/canvas-fonts/SmoochSans-OFL.txt +93 -0
  453. package/skills/examples/canvas-design/canvas-fonts/Tektur-Medium.ttf +0 -0
  454. package/skills/examples/canvas-design/canvas-fonts/Tektur-OFL.txt +93 -0
  455. package/skills/examples/canvas-design/canvas-fonts/Tektur-Regular.ttf +0 -0
  456. package/skills/examples/canvas-design/canvas-fonts/WorkSans-Bold.ttf +0 -0
  457. package/skills/examples/canvas-design/canvas-fonts/WorkSans-BoldItalic.ttf +0 -0
  458. package/skills/examples/canvas-design/canvas-fonts/WorkSans-Italic.ttf +0 -0
  459. package/skills/examples/canvas-design/canvas-fonts/WorkSans-OFL.txt +93 -0
  460. package/skills/examples/canvas-design/canvas-fonts/WorkSans-Regular.ttf +0 -0
  461. package/skills/examples/canvas-design/canvas-fonts/YoungSerif-OFL.txt +93 -0
  462. package/skills/examples/canvas-design/canvas-fonts/YoungSerif-Regular.ttf +0 -0
  463. package/skills/examples/copy-editing/SKILL.md +447 -0
  464. package/skills/examples/copy-editing/evals/evals.json +89 -0
  465. package/skills/examples/copy-editing/references/plain-english-alternatives.md +394 -0
  466. package/skills/examples/internal-comms/LICENSE.txt +202 -0
  467. package/skills/examples/internal-comms/SKILL.md +32 -0
  468. package/skills/examples/internal-comms/examples/3p-updates.md +47 -0
  469. package/skills/examples/internal-comms/examples/company-newsletter.md +65 -0
  470. package/skills/examples/internal-comms/examples/faq-answers.md +30 -0
  471. package/skills/examples/internal-comms/examples/general-comms.md +16 -0
  472. package/skills/examples/mcp-builder/SKILL.md +328 -0
  473. package/skills/examples/mcp-builder/reference/evaluation.md +602 -0
  474. package/skills/examples/mcp-builder/reference/mcp_best_practices.md +915 -0
  475. package/skills/examples/mcp-builder/reference/node_mcp_server.md +916 -0
  476. package/skills/examples/mcp-builder/reference/python_mcp_server.md +752 -0
  477. package/skills/examples/mcp-builder/scripts/connections.py +151 -0
  478. package/skills/examples/mcp-builder/scripts/evaluation.py +373 -0
  479. package/skills/examples/mcp-builder/scripts/example_evaluation.xml +22 -0
  480. package/skills/examples/mcp-builder/scripts/requirements.txt +2 -0
  481. package/skills/examples/product-marketing-context/SKILL.md +241 -0
  482. package/skills/examples/product-marketing-context/evals/evals.json +85 -0
  483. package/skills/examples/single-cell-rna-qc/SKILL.md +175 -0
  484. package/skills/examples/single-cell-rna-qc/references/scverse_qc_guidelines.md +186 -0
  485. package/skills/examples/single-cell-rna-qc/scripts/qc_analysis.py +232 -0
  486. package/skills/examples/single-cell-rna-qc/scripts/qc_core.py +233 -0
  487. package/skills/examples/single-cell-rna-qc/scripts/qc_plotting.py +235 -0
  488. package/skills/examples/skill-creator/SKILL.md +355 -0
  489. package/skills/examples/skill-creator/references/output-patterns.md +82 -0
  490. package/skills/examples/skill-creator/references/workflows.md +28 -0
  491. package/skills/examples/skill-creator/scripts/init_skill.py +303 -0
  492. package/skills/examples/skill-creator/scripts/package_skill.py +110 -0
  493. package/skills/examples/skill-creator/scripts/quick_validate.py +95 -0
  494. package/skills/examples/slack-gif-creator/SKILL.md +254 -0
  495. package/skills/examples/slack-gif-creator/core/easing.py +234 -0
  496. package/skills/examples/slack-gif-creator/core/frame_composer.py +176 -0
  497. package/skills/examples/slack-gif-creator/core/gif_builder.py +269 -0
  498. package/skills/examples/slack-gif-creator/core/validators.py +136 -0
  499. package/skills/examples/slack-gif-creator/requirements.txt +4 -0
  500. package/skills/examples/social-content/SKILL.md +278 -0
  501. package/skills/examples/social-content/evals/evals.json +92 -0
  502. package/skills/examples/social-content/references/platforms.md +170 -0
  503. package/skills/examples/social-content/references/post-templates.md +177 -0
  504. package/skills/examples/social-content/references/reverse-engineering.md +195 -0
  505. package/skills/examples/theme-factory/SKILL.md +59 -0
  506. package/skills/examples/theme-factory/theme-showcase.pdf +0 -0
  507. package/skills/examples/theme-factory/themes/arctic-frost.md +19 -0
  508. package/skills/examples/theme-factory/themes/botanical-garden.md +19 -0
  509. package/skills/examples/theme-factory/themes/desert-rose.md +19 -0
  510. package/skills/examples/theme-factory/themes/forest-canopy.md +19 -0
  511. package/skills/examples/theme-factory/themes/golden-hour.md +19 -0
  512. package/skills/examples/theme-factory/themes/midnight-galaxy.md +19 -0
  513. package/skills/examples/theme-factory/themes/modern-minimalist.md +19 -0
  514. package/skills/examples/theme-factory/themes/ocean-depths.md +19 -0
  515. package/skills/examples/theme-factory/themes/sunset-boulevard.md +19 -0
  516. package/skills/examples/theme-factory/themes/tech-innovation.md +19 -0
  517. package/skills/examples/web-artifacts-builder/LICENSE.txt +202 -0
  518. package/skills/examples/web-artifacts-builder/SKILL.md +74 -0
  519. package/skills/examples/web-artifacts-builder/scripts/bundle-artifact.sh +54 -0
  520. package/skills/examples/web-artifacts-builder/scripts/init-artifact.sh +322 -0
  521. package/skills/examples/web-artifacts-builder/scripts/shadcn-components.tar.gz +0 -0
  522. package/skills/examples/writing-skills/SKILL.md +655 -0
  523. package/skills/examples/writing-skills/anthropic-best-practices.md +1150 -0
  524. package/skills/examples/writing-skills/examples/CLAUDE_MD_TESTING.md +189 -0
  525. package/skills/examples/writing-skills/graphviz-conventions.dot +172 -0
  526. package/skills/examples/writing-skills/persuasion-principles.md +187 -0
  527. package/skills/examples/writing-skills/render-graphs.js +168 -0
  528. package/skills/examples/writing-skills/testing-skills-with-subagents.md +384 -0
  529. package/skills/public/describe-image/SKILL.md +105 -0
  530. package/skills/public/describe-image/scripts/describe.py +389 -0
  531. package/skills/public/doc-coauthoring/SKILL.md +375 -0
  532. package/skills/public/docx/LICENSE.txt +30 -0
  533. package/skills/public/docx/SKILL.md +199 -0
  534. package/skills/public/docx/docx-js.md +350 -0
  535. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  536. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  537. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  538. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  539. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  540. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  541. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  542. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  543. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  544. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  545. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  546. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  547. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  548. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  549. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  550. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  551. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  552. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  553. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  554. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  555. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  556. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  557. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  558. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  559. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  560. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  561. package/skills/public/docx/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  562. package/skills/public/docx/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  563. package/skills/public/docx/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  564. package/skills/public/docx/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  565. package/skills/public/docx/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  566. package/skills/public/docx/ooxml/schemas/mce/mc.xsd +75 -0
  567. package/skills/public/docx/ooxml/schemas/microsoft/wml-2010.xsd +560 -0
  568. package/skills/public/docx/ooxml/schemas/microsoft/wml-2012.xsd +67 -0
  569. package/skills/public/docx/ooxml/schemas/microsoft/wml-2018.xsd +14 -0
  570. package/skills/public/docx/ooxml/schemas/microsoft/wml-cex-2018.xsd +20 -0
  571. package/skills/public/docx/ooxml/schemas/microsoft/wml-cid-2016.xsd +13 -0
  572. package/skills/public/docx/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  573. package/skills/public/docx/ooxml/schemas/microsoft/wml-symex-2015.xsd +8 -0
  574. package/skills/public/docx/ooxml/scripts/pack.py +159 -0
  575. package/skills/public/docx/ooxml/scripts/unpack.py +29 -0
  576. package/skills/public/docx/ooxml/scripts/validate.py +69 -0
  577. package/skills/public/docx/ooxml/scripts/validation/__init__.py +15 -0
  578. package/skills/public/docx/ooxml/scripts/validation/base.py +951 -0
  579. package/skills/public/docx/ooxml/scripts/validation/docx.py +274 -0
  580. package/skills/public/docx/ooxml/scripts/validation/pptx.py +315 -0
  581. package/skills/public/docx/ooxml/scripts/validation/redlining.py +279 -0
  582. package/skills/public/docx/ooxml.md +632 -0
  583. package/skills/public/docx/scripts/__init__.py +1 -0
  584. package/skills/public/docx/scripts/document.py +1292 -0
  585. package/skills/public/docx/scripts/templates/comments.xml +3 -0
  586. package/skills/public/docx/scripts/templates/commentsExtended.xml +3 -0
  587. package/skills/public/docx/scripts/templates/commentsExtensible.xml +3 -0
  588. package/skills/public/docx/scripts/templates/commentsIds.xml +3 -0
  589. package/skills/public/docx/scripts/templates/people.xml +3 -0
  590. package/skills/public/docx/scripts/utilities.py +374 -0
  591. package/skills/public/file-reading/LICENSE.txt +30 -0
  592. package/skills/public/file-reading/SKILL.md +350 -0
  593. package/skills/public/frontend-design/LICENSE.txt +177 -0
  594. package/skills/public/frontend-design/SKILL.md +42 -0
  595. package/skills/public/gitlab-explorer/SKILL.md +174 -0
  596. package/skills/public/gitlab-explorer/references/git-commands.md +323 -0
  597. package/skills/public/gitlab-explorer/references/glab-commands.md +282 -0
  598. package/skills/public/gitlab-explorer/scripts/check_gitlab_auth.sh +109 -0
  599. package/skills/public/pdf/FORMS.md +205 -0
  600. package/skills/public/pdf/REFERENCE.md +612 -0
  601. package/skills/public/pdf/SKILL.md +364 -0
  602. package/skills/public/pdf/scripts/check_bounding_boxes.py +70 -0
  603. package/skills/public/pdf/scripts/check_bounding_boxes_test.py +226 -0
  604. package/skills/public/pdf/scripts/check_fillable_fields.py +12 -0
  605. package/skills/public/pdf/scripts/convert_pdf_to_images.py +35 -0
  606. package/skills/public/pdf/scripts/create_validation_image.py +41 -0
  607. package/skills/public/pdf/scripts/extract_form_field_info.py +152 -0
  608. package/skills/public/pdf/scripts/fill_fillable_fields.py +114 -0
  609. package/skills/public/pdf/scripts/fill_pdf_form_with_annotations.py +108 -0
  610. package/skills/public/pdf-reading/LICENSE.txt +30 -0
  611. package/skills/public/pdf-reading/REFERENCE.md +196 -0
  612. package/skills/public/pdf-reading/SKILL.md +305 -0
  613. package/skills/public/playwright-cli/SKILL.md +278 -0
  614. package/skills/public/playwright-cli/references/request-mocking.md +87 -0
  615. package/skills/public/playwright-cli/references/running-code.md +232 -0
  616. package/skills/public/playwright-cli/references/session-management.md +169 -0
  617. package/skills/public/playwright-cli/references/storage-state.md +275 -0
  618. package/skills/public/playwright-cli/references/test-generation.md +88 -0
  619. package/skills/public/playwright-cli/references/tracing.md +139 -0
  620. package/skills/public/playwright-cli/references/video-recording.md +43 -0
  621. package/skills/public/pptx/LICENSE.txt +30 -0
  622. package/skills/public/pptx/SKILL.md +484 -0
  623. package/skills/public/pptx/css.md +335 -0
  624. package/skills/public/pptx/html2pptx.md +893 -0
  625. package/skills/public/pptx/html2pptx.tgz +0 -0
  626. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  627. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  628. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  629. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  630. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  631. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  632. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  633. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  634. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  635. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  636. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  637. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  638. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  639. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  640. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  641. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  642. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  643. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  644. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  645. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  646. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  647. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  648. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  649. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  650. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  651. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  652. package/skills/public/pptx/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  653. package/skills/public/pptx/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  654. package/skills/public/pptx/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  655. package/skills/public/pptx/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  656. package/skills/public/pptx/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  657. package/skills/public/pptx/ooxml/schemas/mce/mc.xsd +75 -0
  658. package/skills/public/pptx/ooxml/schemas/microsoft/wml-2010.xsd +560 -0
  659. package/skills/public/pptx/ooxml/schemas/microsoft/wml-2012.xsd +67 -0
  660. package/skills/public/pptx/ooxml/schemas/microsoft/wml-2018.xsd +14 -0
  661. package/skills/public/pptx/ooxml/schemas/microsoft/wml-cex-2018.xsd +20 -0
  662. package/skills/public/pptx/ooxml/schemas/microsoft/wml-cid-2016.xsd +13 -0
  663. package/skills/public/pptx/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  664. package/skills/public/pptx/ooxml/schemas/microsoft/wml-symex-2015.xsd +8 -0
  665. package/skills/public/pptx/ooxml/scripts/pack.py +159 -0
  666. package/skills/public/pptx/ooxml/scripts/unpack.py +29 -0
  667. package/skills/public/pptx/ooxml/scripts/validate.py +69 -0
  668. package/skills/public/pptx/ooxml/scripts/validation/__init__.py +15 -0
  669. package/skills/public/pptx/ooxml/scripts/validation/base.py +951 -0
  670. package/skills/public/pptx/ooxml/scripts/validation/docx.py +274 -0
  671. package/skills/public/pptx/ooxml/scripts/validation/pptx.py +315 -0
  672. package/skills/public/pptx/ooxml/scripts/validation/redlining.py +279 -0
  673. package/skills/public/pptx/ooxml.md +427 -0
  674. package/skills/public/pptx/scripts/inventory.py +1020 -0
  675. package/skills/public/pptx/scripts/rearrange.py +231 -0
  676. package/skills/public/pptx/scripts/replace.py +385 -0
  677. package/skills/public/pptx/scripts/thumbnail.py +450 -0
  678. package/skills/public/skill-creator/SKILL.md +356 -0
  679. package/skills/public/skill-creator/references/output-patterns.md +82 -0
  680. package/skills/public/skill-creator/references/workflows.md +28 -0
  681. package/skills/public/skill-creator/scripts/init_skill.py +303 -0
  682. package/skills/public/skill-creator/scripts/package_skill.py +110 -0
  683. package/skills/public/skill-creator/scripts/quick_validate.py +95 -0
  684. package/skills/public/sub-agent/SKILL.md +186 -0
  685. package/skills/public/sub-agent/references/security-review.md +153 -0
  686. package/skills/public/sub-agent/references/usage.md +207 -0
  687. package/skills/public/sub-agent/scripts/list_subagent_models.sh +22 -0
  688. package/skills/public/test-driven-development/SKILL.md +371 -0
  689. package/skills/public/test-driven-development/testing-anti-patterns.md +299 -0
  690. package/skills/public/webapp-testing/LICENSE.txt +202 -0
  691. package/skills/public/webapp-testing/SKILL.md +96 -0
  692. package/skills/public/webapp-testing/examples/console_logging.py +35 -0
  693. package/skills/public/webapp-testing/examples/element_discovery.py +40 -0
  694. package/skills/public/webapp-testing/examples/static_html_automation.py +33 -0
  695. package/skills/public/webapp-testing/scripts/with_server.py +106 -0
  696. package/skills/public/xlsx/LICENSE.txt +30 -0
  697. package/skills/public/xlsx/SKILL.md +316 -0
  698. package/skills/public/xlsx/preview_data.py +93 -0
  699. package/skills/public/xlsx/recalc.py +178 -0
  700. package/tests/README.md +42 -0
  701. package/tests/fixtures/cli/claude_v0.9.2.0_argv.json +46 -0
  702. package/tests/fixtures/cli/claude_v0.9.2.0_stdout.json +32 -0
  703. package/tests/fixtures/cli/codex_run.jsonl +4 -0
  704. package/tests/fixtures/cli/opencode_run.jsonl +6 -0
  705. package/tests/integration/README.md +56 -0
  706. package/tests/integration/conftest.py +280 -0
  707. package/tests/integration/pytest.ini +13 -0
  708. package/tests/integration/test_mcp_auth.py +85 -0
  709. package/tests/integration/test_mcp_tools.py +101 -0
  710. package/tests/integration/test_workspace_lifecycle.py +125 -0
  711. package/tests/orchestrator/mock_llm_server.py +343 -0
  712. package/tests/orchestrator/test_cli_adapters.py +566 -0
  713. package/tests/orchestrator/test_cli_adapters_live.py +527 -0
  714. package/tests/orchestrator/test_cli_runtime.py +451 -0
  715. package/tests/orchestrator/test_docker_manager.py +302 -0
  716. package/tests/orchestrator/test_dynamic_instructions.py +69 -0
  717. package/tests/orchestrator/test_mcp_resources.py +140 -0
  718. package/tests/orchestrator/test_mcp_tools.py +224 -0
  719. package/tests/orchestrator/test_passthrough_isolation.py +201 -0
  720. package/tests/orchestrator/test_readme_in_container.py +76 -0
  721. package/tests/orchestrator/test_render_cache.py +84 -0
  722. package/tests/orchestrator/test_runtime_cli_endpoint.py +108 -0
  723. package/tests/orchestrator/test_single_user_mode.py +212 -0
  724. package/tests/orchestrator/test_startup_warnings.py +123 -0
  725. package/tests/orchestrator/test_sub_agent_dispatch.py +327 -0
  726. package/tests/orchestrator/test_subagent_claude_compat.py +367 -0
  727. package/tests/orchestrator/test_system_prompt_endpoint.py +191 -0
  728. package/tests/orchestrator/test_tool_descriptions.py +52 -0
  729. package/tests/orchestrator/test_view_image.py +201 -0
  730. package/tests/patches/conftest.py +30 -0
  731. package/tests/patches/fixtures/__init__.py +10 -0
  732. package/tests/patches/fixtures/middleware_v0.9.1.py +5057 -0
  733. package/tests/patches/fixtures/middleware_v0.9.2.py +5120 -0
  734. package/tests/patches/fixtures/retrieval_v0.9.1.py +2684 -0
  735. package/tests/patches/fixtures/retrieval_v0.9.2.py +2700 -0
  736. package/tests/patches/test_fix_attached_files_position.py +118 -0
  737. package/tests/patches/test_fix_large_tool_args.py +130 -0
  738. package/tests/patches/test_fix_large_tool_results.py +531 -0
  739. package/tests/patches/test_fix_skip_embedding_chat_files.py +160 -0
  740. package/tests/patches/test_fix_skip_rag_files_native_fc.py +120 -0
  741. package/tests/patches/test_fix_tool_loop_errors.py +128 -0
  742. package/tests/security/test_path_traversal_app.py +132 -0
  743. package/tests/security/test_path_traversal_docker.py +36 -0
  744. package/tests/security/test_path_traversal_settings.py +87 -0
  745. package/tests/security/test_safe_path_util.py +166 -0
  746. package/tests/security/test_xss_preview.py +46 -0
  747. package/tests/test-default-model-resolution.py +136 -0
  748. package/tests/test-docker-image.sh +358 -0
  749. package/tests/test-list-subagent-models.sh +421 -0
  750. package/tests/test-mcp-endpoint-live.sh +92 -0
  751. package/tests/test-mcp-native-surface.sh +213 -0
  752. package/tests/test-no-cyrillic.sh +135 -0
  753. package/tests/test-opencode-error-mapping.py +130 -0
  754. package/tests/test-pr88-skills.sh +305 -0
  755. package/tests/test-project-structure.sh +202 -0
  756. package/tests/test-single-user-mode.sh +269 -0
  757. package/tests/test-skill-no-hardcoded-models.sh +65 -0
  758. package/tests/test-subagent-cli-surface.py +137 -0
  759. package/tests/test-subagent-runtime.sh +109 -0
  760. package/tests/test_codex_toml_converter.py +204 -0
  761. package/tests/test_default_resolver_no_legacy_global.py +159 -0
  762. package/tests/test_filter.py +648 -0
  763. package/tests/test_init_sh_unchanged.sh +49 -0
  764. package/tests/test_opencode_alias_map_drop.py +144 -0
  765. package/tests/test_requirements.py +91 -0
  766. package/tests/test_subagent_docstring.py +193 -0
  767. package/tests/test_tools.py +34 -0
  768. package/vendor/extract-text/README.md +46 -0
  769. package/vendor/extract-text/extract-text +0 -0
@@ -0,0 +1,1292 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Library for working with Word documents: comments, tracked changes, and editing.
4
+
5
+ Usage:
6
+ from skills.docx.scripts.document import Document
7
+
8
+ # Initialize
9
+ doc = Document('workspace/unpacked')
10
+ doc = Document('workspace/unpacked', author="John Doe", initials="JD")
11
+
12
+ # Find nodes
13
+ node = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "1"})
14
+ node = doc["word/document.xml"].get_node(tag="w:p", line_number=10)
15
+
16
+ # Add comments
17
+ doc.add_comment(start=node, end=node, text="Comment text")
18
+ doc.reply_to_comment(parent_comment_id=0, text="Reply text")
19
+
20
+ # Suggest tracked changes
21
+ doc["word/document.xml"].suggest_deletion(node) # Delete content
22
+ doc["word/document.xml"].revert_insertion(ins_node) # Reject insertion
23
+ doc["word/document.xml"].revert_deletion(del_node) # Reject deletion
24
+
25
+ # Save
26
+ doc.save()
27
+ """
28
+
29
+ import html
30
+ import random
31
+ import shutil
32
+ import tempfile
33
+ from datetime import datetime, timezone
34
+ from pathlib import Path
35
+
36
+ from defusedxml import minidom
37
+ from ooxml.scripts.pack import pack_document
38
+ from ooxml.scripts.validation.docx import DOCXSchemaValidator
39
+ from ooxml.scripts.validation.redlining import RedliningValidator
40
+
41
+ from .utilities import XMLEditor
42
+
43
+ # Path to template files
44
+ TEMPLATE_DIR = Path(__file__).parent / "templates"
45
+
46
+
47
+ class DocxXMLEditor(XMLEditor):
48
+ """XMLEditor that automatically applies RSID, author, and date to new elements.
49
+
50
+ Automatically adds attributes to elements that support them when inserting new content:
51
+ - w:rsidR, w:rsidRDefault, w:rsidP (for w:p and w:r elements)
52
+ - w:author and w:date (for w:ins, w:del, w:comment elements)
53
+ - w:id (for w:ins and w:del elements)
54
+
55
+ Attributes:
56
+ dom (defusedxml.minidom.Document): The DOM document for direct manipulation
57
+ """
58
+
59
+ def __init__(
60
+ self, xml_path, rsid: str, author: str = "Assistant", initials: str = "C"
61
+ ):
62
+ """Initialize with required RSID and optional author.
63
+
64
+ Args:
65
+ xml_path: Path to XML file to edit
66
+ rsid: RSID to automatically apply to new elements
67
+ author: Author name for tracked changes and comments (default: "Assistant")
68
+ initials: Author initials (default: "C")
69
+ """
70
+ super().__init__(xml_path)
71
+ self.rsid = rsid
72
+ self.author = author
73
+ self.initials = initials
74
+
75
+ def _get_next_change_id(self):
76
+ """Get the next available change ID by checking all tracked change elements."""
77
+ max_id = -1
78
+ for tag in ("w:ins", "w:del"):
79
+ elements = self.dom.getElementsByTagName(tag)
80
+ for elem in elements:
81
+ change_id = elem.getAttribute("w:id")
82
+ if change_id:
83
+ try:
84
+ max_id = max(max_id, int(change_id))
85
+ except ValueError:
86
+ pass
87
+ return max_id + 1
88
+
89
+ def _ensure_w16du_namespace(self):
90
+ """Ensure w16du namespace is declared on the root element."""
91
+ root = self.dom.documentElement
92
+ if not root.hasAttribute("xmlns:w16du"): # type: ignore
93
+ root.setAttribute( # type: ignore
94
+ "xmlns:w16du",
95
+ "http://schemas.microsoft.com/office/word/2023/wordml/word16du",
96
+ )
97
+
98
+ def _ensure_w16cex_namespace(self):
99
+ """Ensure w16cex namespace is declared on the root element."""
100
+ root = self.dom.documentElement
101
+ if not root.hasAttribute("xmlns:w16cex"): # type: ignore
102
+ root.setAttribute( # type: ignore
103
+ "xmlns:w16cex",
104
+ "http://schemas.microsoft.com/office/word/2018/wordml/cex",
105
+ )
106
+
107
+ def _ensure_w14_namespace(self):
108
+ """Ensure w14 namespace is declared on the root element."""
109
+ root = self.dom.documentElement
110
+ if not root.hasAttribute("xmlns:w14"): # type: ignore
111
+ root.setAttribute( # type: ignore
112
+ "xmlns:w14",
113
+ "http://schemas.microsoft.com/office/word/2010/wordml",
114
+ )
115
+
116
+ def _inject_attributes_to_nodes(self, nodes):
117
+ """Inject RSID, author, and date attributes into DOM nodes where applicable.
118
+
119
+ Adds attributes to elements that support them:
120
+ - w:r: gets w:rsidR (or w:rsidDel if inside w:del)
121
+ - w:p: gets w:rsidR, w:rsidRDefault, w:rsidP, w14:paraId, w14:textId
122
+ - w:t: gets xml:space="preserve" if text has leading/trailing whitespace
123
+ - w:ins, w:del: get w:id, w:author, w:date, w16du:dateUtc
124
+ - w:comment: gets w:author, w:date, w:initials
125
+ - w16cex:commentExtensible: gets w16cex:dateUtc
126
+
127
+ Args:
128
+ nodes: List of DOM nodes to process
129
+ """
130
+ from datetime import datetime, timezone
131
+
132
+ timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
133
+
134
+ def is_inside_deletion(elem):
135
+ """Check if element is inside a w:del element."""
136
+ parent = elem.parentNode
137
+ while parent:
138
+ if parent.nodeType == parent.ELEMENT_NODE and parent.tagName == "w:del":
139
+ return True
140
+ parent = parent.parentNode
141
+ return False
142
+
143
+ def add_rsid_to_p(elem):
144
+ if not elem.hasAttribute("w:rsidR"):
145
+ elem.setAttribute("w:rsidR", self.rsid)
146
+ if not elem.hasAttribute("w:rsidRDefault"):
147
+ elem.setAttribute("w:rsidRDefault", self.rsid)
148
+ if not elem.hasAttribute("w:rsidP"):
149
+ elem.setAttribute("w:rsidP", self.rsid)
150
+ # Add w14:paraId and w14:textId if not present
151
+ if not elem.hasAttribute("w14:paraId"):
152
+ self._ensure_w14_namespace()
153
+ elem.setAttribute("w14:paraId", _generate_hex_id())
154
+ if not elem.hasAttribute("w14:textId"):
155
+ self._ensure_w14_namespace()
156
+ elem.setAttribute("w14:textId", _generate_hex_id())
157
+
158
+ def add_rsid_to_r(elem):
159
+ # Use w:rsidDel for <w:r> inside <w:del>, otherwise w:rsidR
160
+ if is_inside_deletion(elem):
161
+ if not elem.hasAttribute("w:rsidDel"):
162
+ elem.setAttribute("w:rsidDel", self.rsid)
163
+ else:
164
+ if not elem.hasAttribute("w:rsidR"):
165
+ elem.setAttribute("w:rsidR", self.rsid)
166
+
167
+ def add_tracked_change_attrs(elem):
168
+ # Auto-assign w:id if not present
169
+ if not elem.hasAttribute("w:id"):
170
+ elem.setAttribute("w:id", str(self._get_next_change_id()))
171
+ if not elem.hasAttribute("w:author"):
172
+ elem.setAttribute("w:author", self.author)
173
+ if not elem.hasAttribute("w:date"):
174
+ elem.setAttribute("w:date", timestamp)
175
+ # Add w16du:dateUtc for tracked changes (same as w:date since we generate UTC timestamps)
176
+ if elem.tagName in ("w:ins", "w:del") and not elem.hasAttribute(
177
+ "w16du:dateUtc"
178
+ ):
179
+ self._ensure_w16du_namespace()
180
+ elem.setAttribute("w16du:dateUtc", timestamp)
181
+
182
+ def add_comment_attrs(elem):
183
+ if not elem.hasAttribute("w:author"):
184
+ elem.setAttribute("w:author", self.author)
185
+ if not elem.hasAttribute("w:date"):
186
+ elem.setAttribute("w:date", timestamp)
187
+ if not elem.hasAttribute("w:initials"):
188
+ elem.setAttribute("w:initials", self.initials)
189
+
190
+ def add_comment_extensible_date(elem):
191
+ # Add w16cex:dateUtc for comment extensible elements
192
+ if not elem.hasAttribute("w16cex:dateUtc"):
193
+ self._ensure_w16cex_namespace()
194
+ elem.setAttribute("w16cex:dateUtc", timestamp)
195
+
196
+ def add_xml_space_to_t(elem):
197
+ # Add xml:space="preserve" to w:t if text has leading/trailing whitespace
198
+ if (
199
+ elem.firstChild
200
+ and elem.firstChild.nodeType == elem.firstChild.TEXT_NODE
201
+ ):
202
+ text = elem.firstChild.data
203
+ if text and (text[0].isspace() or text[-1].isspace()):
204
+ if not elem.hasAttribute("xml:space"):
205
+ elem.setAttribute("xml:space", "preserve")
206
+
207
+ for node in nodes:
208
+ if node.nodeType != node.ELEMENT_NODE:
209
+ continue
210
+
211
+ # Handle the node itself
212
+ if node.tagName == "w:p":
213
+ add_rsid_to_p(node)
214
+ elif node.tagName == "w:r":
215
+ add_rsid_to_r(node)
216
+ elif node.tagName == "w:t":
217
+ add_xml_space_to_t(node)
218
+ elif node.tagName in ("w:ins", "w:del"):
219
+ add_tracked_change_attrs(node)
220
+ elif node.tagName == "w:comment":
221
+ add_comment_attrs(node)
222
+ elif node.tagName == "w16cex:commentExtensible":
223
+ add_comment_extensible_date(node)
224
+
225
+ # Process descendants (getElementsByTagName doesn't return the element itself)
226
+ for elem in node.getElementsByTagName("w:p"):
227
+ add_rsid_to_p(elem)
228
+ for elem in node.getElementsByTagName("w:r"):
229
+ add_rsid_to_r(elem)
230
+ for elem in node.getElementsByTagName("w:t"):
231
+ add_xml_space_to_t(elem)
232
+ for tag in ("w:ins", "w:del"):
233
+ for elem in node.getElementsByTagName(tag):
234
+ add_tracked_change_attrs(elem)
235
+ for elem in node.getElementsByTagName("w:comment"):
236
+ add_comment_attrs(elem)
237
+ for elem in node.getElementsByTagName("w16cex:commentExtensible"):
238
+ add_comment_extensible_date(elem)
239
+
240
+ def replace_node(self, elem, new_content):
241
+ """Replace node with automatic attribute injection."""
242
+ nodes = super().replace_node(elem, new_content)
243
+ self._inject_attributes_to_nodes(nodes)
244
+ return nodes
245
+
246
+ def insert_after(self, elem, xml_content):
247
+ """Insert after with automatic attribute injection."""
248
+ nodes = super().insert_after(elem, xml_content)
249
+ self._inject_attributes_to_nodes(nodes)
250
+ return nodes
251
+
252
+ def insert_before(self, elem, xml_content):
253
+ """Insert before with automatic attribute injection."""
254
+ nodes = super().insert_before(elem, xml_content)
255
+ self._inject_attributes_to_nodes(nodes)
256
+ return nodes
257
+
258
+ def append_to(self, elem, xml_content):
259
+ """Append to with automatic attribute injection."""
260
+ nodes = super().append_to(elem, xml_content)
261
+ self._inject_attributes_to_nodes(nodes)
262
+ return nodes
263
+
264
+ def revert_insertion(self, elem):
265
+ """Reject an insertion by wrapping its content in a deletion.
266
+
267
+ Wraps all runs inside w:ins in w:del, converting w:t to w:delText.
268
+ Can process a single w:ins element or a container element with multiple w:ins.
269
+
270
+ Args:
271
+ elem: Element to process (w:ins, w:p, w:body, etc.)
272
+
273
+ Returns:
274
+ list: List containing the processed element(s)
275
+
276
+ Raises:
277
+ ValueError: If the element contains no w:ins elements
278
+
279
+ Example:
280
+ # Reject a single insertion
281
+ ins = doc["word/document.xml"].get_node(tag="w:ins", attrs={"w:id": "5"})
282
+ doc["word/document.xml"].revert_insertion(ins)
283
+
284
+ # Reject all insertions in a paragraph
285
+ para = doc["word/document.xml"].get_node(tag="w:p", line_number=42)
286
+ doc["word/document.xml"].revert_insertion(para)
287
+ """
288
+ # Collect insertions
289
+ ins_elements = []
290
+ if elem.tagName == "w:ins":
291
+ ins_elements.append(elem)
292
+ else:
293
+ ins_elements.extend(elem.getElementsByTagName("w:ins"))
294
+
295
+ # Validate that there are insertions to reject
296
+ if not ins_elements:
297
+ raise ValueError(
298
+ f"revert_insertion requires w:ins elements. "
299
+ f"The provided element <{elem.tagName}> contains no insertions. "
300
+ )
301
+
302
+ # Process all insertions - wrap all children in w:del
303
+ for ins_elem in ins_elements:
304
+ runs = list(ins_elem.getElementsByTagName("w:r"))
305
+ if not runs:
306
+ continue
307
+
308
+ # Create deletion wrapper
309
+ del_wrapper = self.dom.createElement("w:del")
310
+
311
+ # Process each run
312
+ for run in runs:
313
+ # Convert w:t → w:delText and w:rsidR → w:rsidDel
314
+ if run.hasAttribute("w:rsidR"):
315
+ run.setAttribute("w:rsidDel", run.getAttribute("w:rsidR"))
316
+ run.removeAttribute("w:rsidR")
317
+ elif not run.hasAttribute("w:rsidDel"):
318
+ run.setAttribute("w:rsidDel", self.rsid)
319
+
320
+ for t_elem in list(run.getElementsByTagName("w:t")):
321
+ del_text = self.dom.createElement("w:delText")
322
+ # Copy ALL child nodes (not just firstChild) to handle entities
323
+ while t_elem.firstChild:
324
+ del_text.appendChild(t_elem.firstChild)
325
+ for i in range(t_elem.attributes.length):
326
+ attr = t_elem.attributes.item(i)
327
+ del_text.setAttribute(attr.name, attr.value)
328
+ t_elem.parentNode.replaceChild(del_text, t_elem)
329
+
330
+ # Move all children from ins to del wrapper
331
+ while ins_elem.firstChild:
332
+ del_wrapper.appendChild(ins_elem.firstChild)
333
+
334
+ # Add del wrapper back to ins
335
+ ins_elem.appendChild(del_wrapper)
336
+
337
+ # Inject attributes to the deletion wrapper
338
+ self._inject_attributes_to_nodes([del_wrapper])
339
+
340
+ return [elem]
341
+
342
+ def revert_deletion(self, elem):
343
+ """Reject a deletion by re-inserting the deleted content.
344
+
345
+ Creates w:ins elements after each w:del, copying deleted content and
346
+ converting w:delText back to w:t.
347
+ Can process a single w:del element or a container element with multiple w:del.
348
+
349
+ Args:
350
+ elem: Element to process (w:del, w:p, w:body, etc.)
351
+
352
+ Returns:
353
+ list: If elem is w:del, returns [elem, new_ins]. Otherwise returns [elem].
354
+
355
+ Raises:
356
+ ValueError: If the element contains no w:del elements
357
+
358
+ Example:
359
+ # Reject a single deletion - returns [w:del, w:ins]
360
+ del_elem = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "3"})
361
+ nodes = doc["word/document.xml"].revert_deletion(del_elem)
362
+
363
+ # Reject all deletions in a paragraph - returns [para]
364
+ para = doc["word/document.xml"].get_node(tag="w:p", line_number=42)
365
+ nodes = doc["word/document.xml"].revert_deletion(para)
366
+ """
367
+ # Collect deletions FIRST - before we modify the DOM
368
+ del_elements = []
369
+ is_single_del = elem.tagName == "w:del"
370
+
371
+ if is_single_del:
372
+ del_elements.append(elem)
373
+ else:
374
+ del_elements.extend(elem.getElementsByTagName("w:del"))
375
+
376
+ # Validate that there are deletions to reject
377
+ if not del_elements:
378
+ raise ValueError(
379
+ f"revert_deletion requires w:del elements. "
380
+ f"The provided element <{elem.tagName}> contains no deletions. "
381
+ )
382
+
383
+ # Track created insertion (only relevant if elem is a single w:del)
384
+ created_insertion = None
385
+
386
+ # Process all deletions - create insertions that copy the deleted content
387
+ for del_elem in del_elements:
388
+ # Clone the deleted runs and convert them to insertions
389
+ runs = list(del_elem.getElementsByTagName("w:r"))
390
+ if not runs:
391
+ continue
392
+
393
+ # Create insertion wrapper
394
+ ins_elem = self.dom.createElement("w:ins")
395
+
396
+ for run in runs:
397
+ # Clone the run
398
+ new_run = run.cloneNode(True)
399
+
400
+ # Convert w:delText → w:t
401
+ for del_text in list(new_run.getElementsByTagName("w:delText")):
402
+ t_elem = self.dom.createElement("w:t")
403
+ # Copy ALL child nodes (not just firstChild) to handle entities
404
+ while del_text.firstChild:
405
+ t_elem.appendChild(del_text.firstChild)
406
+ for i in range(del_text.attributes.length):
407
+ attr = del_text.attributes.item(i)
408
+ t_elem.setAttribute(attr.name, attr.value)
409
+ del_text.parentNode.replaceChild(t_elem, del_text)
410
+
411
+ # Update run attributes: w:rsidDel → w:rsidR
412
+ if new_run.hasAttribute("w:rsidDel"):
413
+ new_run.setAttribute("w:rsidR", new_run.getAttribute("w:rsidDel"))
414
+ new_run.removeAttribute("w:rsidDel")
415
+ elif not new_run.hasAttribute("w:rsidR"):
416
+ new_run.setAttribute("w:rsidR", self.rsid)
417
+
418
+ ins_elem.appendChild(new_run)
419
+
420
+ # Insert the new insertion after the deletion
421
+ nodes = self.insert_after(del_elem, ins_elem.toxml())
422
+
423
+ # If processing a single w:del, track the created insertion
424
+ if is_single_del and nodes:
425
+ created_insertion = nodes[0]
426
+
427
+ # Return based on input type
428
+ if is_single_del and created_insertion:
429
+ return [elem, created_insertion]
430
+ else:
431
+ return [elem]
432
+
433
+ @staticmethod
434
+ def suggest_paragraph(xml_content: str) -> str:
435
+ """Transform paragraph XML to add tracked change wrapping for insertion.
436
+
437
+ Wraps runs in <w:ins> and adds <w:ins/> to w:rPr in w:pPr for numbered lists.
438
+
439
+ Args:
440
+ xml_content: XML string containing a <w:p> element
441
+
442
+ Returns:
443
+ str: Transformed XML with tracked change wrapping
444
+ """
445
+ wrapper = f'<root xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">{xml_content}</root>'
446
+ doc = minidom.parseString(wrapper)
447
+ para = doc.getElementsByTagName("w:p")[0]
448
+
449
+ # Ensure w:pPr exists
450
+ pPr_list = para.getElementsByTagName("w:pPr")
451
+ if not pPr_list:
452
+ pPr = doc.createElement("w:pPr")
453
+ para.insertBefore(
454
+ pPr, para.firstChild
455
+ ) if para.firstChild else para.appendChild(pPr)
456
+ else:
457
+ pPr = pPr_list[0]
458
+
459
+ # Ensure w:rPr exists in w:pPr
460
+ rPr_list = pPr.getElementsByTagName("w:rPr")
461
+ if not rPr_list:
462
+ rPr = doc.createElement("w:rPr")
463
+ pPr.appendChild(rPr)
464
+ else:
465
+ rPr = rPr_list[0]
466
+
467
+ # Add <w:ins/> to w:rPr
468
+ ins_marker = doc.createElement("w:ins")
469
+ rPr.insertBefore(
470
+ ins_marker, rPr.firstChild
471
+ ) if rPr.firstChild else rPr.appendChild(ins_marker)
472
+
473
+ # Wrap all non-pPr children in <w:ins>
474
+ ins_wrapper = doc.createElement("w:ins")
475
+ for child in [c for c in para.childNodes if c.nodeName != "w:pPr"]:
476
+ para.removeChild(child)
477
+ ins_wrapper.appendChild(child)
478
+ para.appendChild(ins_wrapper)
479
+
480
+ return para.toxml()
481
+
482
+ def suggest_deletion(self, elem):
483
+ """Mark a w:r or w:p element as deleted with tracked changes (in-place DOM manipulation).
484
+
485
+ For w:r: wraps in <w:del>, converts <w:t> to <w:delText>, preserves w:rPr
486
+ For w:p (regular): wraps content in <w:del>, converts <w:t> to <w:delText>
487
+ For w:p (numbered list): adds <w:del/> to w:rPr in w:pPr, wraps content in <w:del>
488
+
489
+ Args:
490
+ elem: A w:r or w:p DOM element without existing tracked changes
491
+
492
+ Returns:
493
+ Element: The modified element
494
+
495
+ Raises:
496
+ ValueError: If element has existing tracked changes or invalid structure
497
+ """
498
+ if elem.nodeName == "w:r":
499
+ # Check for existing w:delText
500
+ if elem.getElementsByTagName("w:delText"):
501
+ raise ValueError("w:r element already contains w:delText")
502
+
503
+ # Convert w:t → w:delText
504
+ for t_elem in list(elem.getElementsByTagName("w:t")):
505
+ del_text = self.dom.createElement("w:delText")
506
+ # Copy ALL child nodes (not just firstChild) to handle entities
507
+ while t_elem.firstChild:
508
+ del_text.appendChild(t_elem.firstChild)
509
+ # Preserve attributes like xml:space
510
+ for i in range(t_elem.attributes.length):
511
+ attr = t_elem.attributes.item(i)
512
+ del_text.setAttribute(attr.name, attr.value)
513
+ t_elem.parentNode.replaceChild(del_text, t_elem)
514
+
515
+ # Update run attributes: w:rsidR → w:rsidDel
516
+ if elem.hasAttribute("w:rsidR"):
517
+ elem.setAttribute("w:rsidDel", elem.getAttribute("w:rsidR"))
518
+ elem.removeAttribute("w:rsidR")
519
+ elif not elem.hasAttribute("w:rsidDel"):
520
+ elem.setAttribute("w:rsidDel", self.rsid)
521
+
522
+ # Wrap in w:del
523
+ del_wrapper = self.dom.createElement("w:del")
524
+ parent = elem.parentNode
525
+ parent.insertBefore(del_wrapper, elem)
526
+ parent.removeChild(elem)
527
+ del_wrapper.appendChild(elem)
528
+
529
+ # Inject attributes to the deletion wrapper
530
+ self._inject_attributes_to_nodes([del_wrapper])
531
+
532
+ return del_wrapper
533
+
534
+ elif elem.nodeName == "w:p":
535
+ # Check for existing tracked changes
536
+ if elem.getElementsByTagName("w:ins") or elem.getElementsByTagName("w:del"):
537
+ raise ValueError("w:p element already contains tracked changes")
538
+
539
+ # Check if it's a numbered list item
540
+ pPr_list = elem.getElementsByTagName("w:pPr")
541
+ is_numbered = pPr_list and pPr_list[0].getElementsByTagName("w:numPr")
542
+
543
+ if is_numbered:
544
+ # Add <w:del/> to w:rPr in w:pPr
545
+ pPr = pPr_list[0]
546
+ rPr_list = pPr.getElementsByTagName("w:rPr")
547
+
548
+ if not rPr_list:
549
+ rPr = self.dom.createElement("w:rPr")
550
+ pPr.appendChild(rPr)
551
+ else:
552
+ rPr = rPr_list[0]
553
+
554
+ # Add <w:del/> marker
555
+ del_marker = self.dom.createElement("w:del")
556
+ rPr.insertBefore(
557
+ del_marker, rPr.firstChild
558
+ ) if rPr.firstChild else rPr.appendChild(del_marker)
559
+
560
+ # Inject attributes into the marker
561
+ self._inject_attributes_to_nodes([del_marker])
562
+
563
+ # Convert w:t → w:delText in all runs
564
+ for t_elem in list(elem.getElementsByTagName("w:t")):
565
+ del_text = self.dom.createElement("w:delText")
566
+ # Copy ALL child nodes (not just firstChild) to handle entities
567
+ while t_elem.firstChild:
568
+ del_text.appendChild(t_elem.firstChild)
569
+ # Preserve attributes like xml:space
570
+ for i in range(t_elem.attributes.length):
571
+ attr = t_elem.attributes.item(i)
572
+ del_text.setAttribute(attr.name, attr.value)
573
+ t_elem.parentNode.replaceChild(del_text, t_elem)
574
+
575
+ # Update run attributes: w:rsidR → w:rsidDel
576
+ for run in elem.getElementsByTagName("w:r"):
577
+ if run.hasAttribute("w:rsidR"):
578
+ run.setAttribute("w:rsidDel", run.getAttribute("w:rsidR"))
579
+ run.removeAttribute("w:rsidR")
580
+ elif not run.hasAttribute("w:rsidDel"):
581
+ run.setAttribute("w:rsidDel", self.rsid)
582
+
583
+ # Wrap all non-pPr children in <w:del>
584
+ del_wrapper = self.dom.createElement("w:del")
585
+ for child in [c for c in elem.childNodes if c.nodeName != "w:pPr"]:
586
+ elem.removeChild(child)
587
+ del_wrapper.appendChild(child)
588
+ elem.appendChild(del_wrapper)
589
+
590
+ # Inject attributes to the deletion wrapper
591
+ self._inject_attributes_to_nodes([del_wrapper])
592
+
593
+ return elem
594
+
595
+ else:
596
+ raise ValueError(f"Element must be w:r or w:p, got {elem.nodeName}")
597
+
598
+
599
+ def _generate_hex_id() -> str:
600
+ """Generate random 8-character hex ID for para/durable IDs.
601
+
602
+ Values are constrained to be less than 0x7FFFFFFF per OOXML spec:
603
+ - paraId must be < 0x80000000
604
+ - durableId must be < 0x7FFFFFFF
605
+ We use the stricter constraint (0x7FFFFFFF) for both.
606
+ """
607
+ return f"{random.randint(1, 0x7FFFFFFE):08X}"
608
+
609
+
610
+ def _generate_rsid() -> str:
611
+ """Generate random 8-character hex RSID."""
612
+ return "".join(random.choices("0123456789ABCDEF", k=8))
613
+
614
+
615
+ class Document:
616
+ """Manages comments in unpacked Word documents."""
617
+
618
+ def __init__(
619
+ self,
620
+ unpacked_dir,
621
+ rsid=None,
622
+ track_revisions=False,
623
+ author="Assistant",
624
+ initials="C",
625
+ ):
626
+ """
627
+ Initialize with path to unpacked Word document directory.
628
+ Automatically sets up comment infrastructure (people.xml, RSIDs).
629
+
630
+ Args:
631
+ unpacked_dir: Path to unpacked DOCX directory (must contain word/ subdirectory)
632
+ rsid: Optional RSID to use for all comment elements. If not provided, one will be generated.
633
+ track_revisions: If True, enables track revisions in settings.xml (default: False)
634
+ author: Default author name for comments (default: "Assistant")
635
+ initials: Default author initials for comments (default: "C")
636
+ """
637
+ self.original_path = Path(unpacked_dir)
638
+
639
+ if not self.original_path.exists() or not self.original_path.is_dir():
640
+ raise ValueError(f"Directory not found: {unpacked_dir}")
641
+
642
+ # Create temporary directory with subdirectories for unpacked content and baseline
643
+ self.temp_dir = tempfile.mkdtemp(prefix="docx_")
644
+ self.unpacked_path = Path(self.temp_dir) / "unpacked"
645
+ shutil.copytree(self.original_path, self.unpacked_path)
646
+
647
+ # Pack original directory into temporary .docx for validation baseline (outside unpacked dir)
648
+ self.original_docx = Path(self.temp_dir) / "original.docx"
649
+ pack_document(self.original_path, self.original_docx, validate=False)
650
+
651
+ self.word_path = self.unpacked_path / "word"
652
+
653
+ # Generate RSID if not provided
654
+ self.rsid = rsid if rsid else _generate_rsid()
655
+ print(f"Using RSID: {self.rsid}")
656
+
657
+ # Set default author and initials
658
+ self.author = author
659
+ self.initials = initials
660
+
661
+ # Cache for lazy-loaded editors
662
+ self._editors = {}
663
+
664
+ # Comment file paths
665
+ self.comments_path = self.word_path / "comments.xml"
666
+ self.comments_extended_path = self.word_path / "commentsExtended.xml"
667
+ self.comments_ids_path = self.word_path / "commentsIds.xml"
668
+ self.comments_extensible_path = self.word_path / "commentsExtensible.xml"
669
+
670
+ # Load existing comments and determine next ID (before setup modifies files)
671
+ self.existing_comments = self._load_existing_comments()
672
+ self.next_comment_id = self._get_next_comment_id()
673
+
674
+ # Convenient access to document.xml editor (semi-private)
675
+ self._document = self["word/document.xml"]
676
+
677
+ # Setup tracked changes infrastructure
678
+ self._setup_tracking(track_revisions=track_revisions)
679
+
680
+ # Add author to people.xml
681
+ self._add_author_to_people(author)
682
+
683
+ def __getitem__(self, xml_path: str) -> DocxXMLEditor:
684
+ """
685
+ Get or create a DocxXMLEditor for the specified XML file.
686
+
687
+ Enables lazy-loaded editors with bracket notation:
688
+ node = doc["word/document.xml"].get_node(tag="w:p", line_number=42)
689
+
690
+ Args:
691
+ xml_path: Relative path to XML file (e.g., "word/document.xml", "word/comments.xml")
692
+
693
+ Returns:
694
+ DocxXMLEditor instance for the specified file
695
+
696
+ Raises:
697
+ ValueError: If the file does not exist
698
+
699
+ Example:
700
+ # Get node from document.xml
701
+ node = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "1"})
702
+
703
+ # Get node from comments.xml
704
+ comment = doc["word/comments.xml"].get_node(tag="w:comment", attrs={"w:id": "0"})
705
+ """
706
+ if xml_path not in self._editors:
707
+ file_path = self.unpacked_path / xml_path
708
+ if not file_path.exists():
709
+ raise ValueError(f"XML file not found: {xml_path}")
710
+ # Use DocxXMLEditor with RSID, author, and initials for all editors
711
+ self._editors[xml_path] = DocxXMLEditor(
712
+ file_path, rsid=self.rsid, author=self.author, initials=self.initials
713
+ )
714
+ return self._editors[xml_path]
715
+
716
+ def add_comment(self, start, end, text: str) -> int:
717
+ """
718
+ Add a comment spanning from one element to another.
719
+
720
+ Args:
721
+ start: DOM element for the starting point
722
+ end: DOM element for the ending point
723
+ text: Comment content
724
+
725
+ Returns:
726
+ The comment ID that was created
727
+
728
+ Example:
729
+ start_node = cm.get_document_node(tag="w:del", id="1")
730
+ end_node = cm.get_document_node(tag="w:ins", id="2")
731
+ cm.add_comment(start=start_node, end=end_node, text="Explanation")
732
+ """
733
+ comment_id = self.next_comment_id
734
+ para_id = _generate_hex_id()
735
+ durable_id = _generate_hex_id()
736
+ timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
737
+
738
+ # Add comment ranges to document.xml immediately
739
+ self._document.insert_before(start, self._comment_range_start_xml(comment_id))
740
+
741
+ # If end node is a paragraph, append comment markup inside it
742
+ # Otherwise insert after it (for run-level anchors)
743
+ if end.tagName == "w:p":
744
+ self._document.append_to(end, self._comment_range_end_xml(comment_id))
745
+ else:
746
+ self._document.insert_after(end, self._comment_range_end_xml(comment_id))
747
+
748
+ # Add to comments.xml immediately
749
+ self._add_to_comments_xml(
750
+ comment_id, para_id, text, self.author, self.initials, timestamp
751
+ )
752
+
753
+ # Add to commentsExtended.xml immediately
754
+ self._add_to_comments_extended_xml(para_id, parent_para_id=None)
755
+
756
+ # Add to commentsIds.xml immediately
757
+ self._add_to_comments_ids_xml(para_id, durable_id)
758
+
759
+ # Add to commentsExtensible.xml immediately
760
+ self._add_to_comments_extensible_xml(durable_id)
761
+
762
+ # Update existing_comments so replies work
763
+ self.existing_comments[comment_id] = {"para_id": para_id}
764
+
765
+ self.next_comment_id += 1
766
+ return comment_id
767
+
768
+ def reply_to_comment(
769
+ self,
770
+ parent_comment_id: int,
771
+ text: str,
772
+ ) -> int:
773
+ """
774
+ Add a reply to an existing comment.
775
+
776
+ Args:
777
+ parent_comment_id: The w:id of the parent comment to reply to
778
+ text: Reply text
779
+
780
+ Returns:
781
+ The comment ID that was created for the reply
782
+
783
+ Example:
784
+ cm.reply_to_comment(parent_comment_id=0, text="I agree with this change")
785
+ """
786
+ if parent_comment_id not in self.existing_comments:
787
+ raise ValueError(f"Parent comment with id={parent_comment_id} not found")
788
+
789
+ parent_info = self.existing_comments[parent_comment_id]
790
+ comment_id = self.next_comment_id
791
+ para_id = _generate_hex_id()
792
+ durable_id = _generate_hex_id()
793
+ timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
794
+
795
+ # Add comment ranges to document.xml immediately
796
+ parent_start_elem = self._document.get_node(
797
+ tag="w:commentRangeStart", attrs={"w:id": str(parent_comment_id)}
798
+ )
799
+ parent_ref_elem = self._document.get_node(
800
+ tag="w:commentReference", attrs={"w:id": str(parent_comment_id)}
801
+ )
802
+
803
+ self._document.insert_after(
804
+ parent_start_elem, self._comment_range_start_xml(comment_id)
805
+ )
806
+ parent_ref_run = parent_ref_elem.parentNode
807
+ self._document.insert_after(
808
+ parent_ref_run, f'<w:commentRangeEnd w:id="{comment_id}"/>'
809
+ )
810
+ self._document.insert_after(
811
+ parent_ref_run, self._comment_ref_run_xml(comment_id)
812
+ )
813
+
814
+ # Add to comments.xml immediately
815
+ self._add_to_comments_xml(
816
+ comment_id, para_id, text, self.author, self.initials, timestamp
817
+ )
818
+
819
+ # Add to commentsExtended.xml immediately (with parent)
820
+ self._add_to_comments_extended_xml(
821
+ para_id, parent_para_id=parent_info["para_id"]
822
+ )
823
+
824
+ # Add to commentsIds.xml immediately
825
+ self._add_to_comments_ids_xml(para_id, durable_id)
826
+
827
+ # Add to commentsExtensible.xml immediately
828
+ self._add_to_comments_extensible_xml(durable_id)
829
+
830
+ # Update existing_comments so replies work
831
+ self.existing_comments[comment_id] = {"para_id": para_id}
832
+
833
+ self.next_comment_id += 1
834
+ return comment_id
835
+
836
+ def suggest_paragraph(self, xml_content: str) -> str:
837
+ """Transform paragraph XML to add tracked change wrapping for insertion.
838
+
839
+ Wraps runs in <w:ins> and adds <w:ins/> to w:rPr in w:pPr for numbered lists.
840
+
841
+ Args:
842
+ xml_content: XML string containing a <w:p> element
843
+
844
+ Returns:
845
+ str: Transformed XML with tracked change wrapping
846
+ """
847
+ return DocxXMLEditor.suggest_paragraph(xml_content)
848
+
849
+ def __del__(self):
850
+ """Clean up temporary directory on deletion."""
851
+ if hasattr(self, "temp_dir") and Path(self.temp_dir).exists():
852
+ shutil.rmtree(self.temp_dir)
853
+
854
+ def validate(self) -> None:
855
+ """
856
+ Validate the document against XSD schema and redlining rules.
857
+
858
+ Raises:
859
+ ValueError: If validation fails.
860
+ """
861
+ # Create validators with current state
862
+ schema_validator = DOCXSchemaValidator(
863
+ self.unpacked_path, self.original_docx, verbose=False
864
+ )
865
+ redlining_validator = RedliningValidator(
866
+ self.unpacked_path, self.original_docx, verbose=False
867
+ )
868
+
869
+ # Run validations
870
+ if not schema_validator.validate():
871
+ raise ValueError("Schema validation failed")
872
+ if not redlining_validator.validate():
873
+ raise ValueError("Redlining validation failed")
874
+
875
+ def save(self, destination=None, validate=True) -> None:
876
+ """
877
+ Save all modified XML files to disk and copy to destination directory.
878
+
879
+ This persists all changes made via add_comment() and reply_to_comment().
880
+
881
+ Args:
882
+ destination: Optional path to save to. If None, saves back to original directory.
883
+ validate: If True, validates document before saving (default: True).
884
+ """
885
+ # Only ensure comment relationships and content types if comment files exist
886
+ if self.comments_path.exists():
887
+ self._ensure_comment_relationships()
888
+ self._ensure_comment_content_types()
889
+
890
+ # Save all modified XML files in temp directory
891
+ for editor in self._editors.values():
892
+ editor.save()
893
+
894
+ # Validate by default
895
+ if validate:
896
+ self.validate()
897
+
898
+ # Copy contents from temp directory to destination (or original directory)
899
+ target_path = Path(destination) if destination else self.original_path
900
+ shutil.copytree(self.unpacked_path, target_path, dirs_exist_ok=True)
901
+
902
+ # ==================== Private: Initialization ====================
903
+
904
+ def _get_next_comment_id(self):
905
+ """Get the next available comment ID."""
906
+ if not self.comments_path.exists():
907
+ return 0
908
+
909
+ editor = self["word/comments.xml"]
910
+ max_id = -1
911
+ for comment_elem in editor.dom.getElementsByTagName("w:comment"):
912
+ comment_id = comment_elem.getAttribute("w:id")
913
+ if comment_id:
914
+ try:
915
+ max_id = max(max_id, int(comment_id))
916
+ except ValueError:
917
+ pass
918
+ return max_id + 1
919
+
920
+ def _load_existing_comments(self):
921
+ """Load existing comments from files to enable replies."""
922
+ if not self.comments_path.exists():
923
+ return {}
924
+
925
+ editor = self["word/comments.xml"]
926
+ existing = {}
927
+
928
+ for comment_elem in editor.dom.getElementsByTagName("w:comment"):
929
+ comment_id = comment_elem.getAttribute("w:id")
930
+ if not comment_id:
931
+ continue
932
+
933
+ # Find para_id from the w:p element within the comment
934
+ para_id = None
935
+ for p_elem in comment_elem.getElementsByTagName("w:p"):
936
+ para_id = p_elem.getAttribute("w14:paraId")
937
+ if para_id:
938
+ break
939
+
940
+ if not para_id:
941
+ continue
942
+
943
+ existing[int(comment_id)] = {"para_id": para_id}
944
+
945
+ return existing
946
+
947
+ # ==================== Private: Setup Methods ====================
948
+
949
+ def _setup_tracking(self, track_revisions=False):
950
+ """Set up comment infrastructure in unpacked directory.
951
+
952
+ Args:
953
+ track_revisions: If True, enables track revisions in settings.xml
954
+ """
955
+ # Create or update word/people.xml
956
+ people_file = self.word_path / "people.xml"
957
+ self._update_people_xml(people_file)
958
+
959
+ # Update XML files
960
+ self._add_content_type_for_people(self.unpacked_path / "[Content_Types].xml")
961
+ self._add_relationship_for_people(
962
+ self.word_path / "_rels" / "document.xml.rels"
963
+ )
964
+
965
+ # Always add RSID to settings.xml, optionally enable trackRevisions
966
+ self._update_settings(
967
+ self.word_path / "settings.xml", track_revisions=track_revisions
968
+ )
969
+
970
+ def _update_people_xml(self, path):
971
+ """Create people.xml if it doesn't exist."""
972
+ if not path.exists():
973
+ # Copy from template
974
+ shutil.copy(TEMPLATE_DIR / "people.xml", path)
975
+
976
+ def _add_content_type_for_people(self, path):
977
+ """Add people.xml content type to [Content_Types].xml if not already present."""
978
+ editor = self["[Content_Types].xml"]
979
+
980
+ if self._has_override(editor, "/word/people.xml"):
981
+ return
982
+
983
+ # Add Override element
984
+ root = editor.dom.documentElement
985
+ override_xml = '<Override PartName="/word/people.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.people+xml"/>'
986
+ editor.append_to(root, override_xml)
987
+
988
+ def _add_relationship_for_people(self, path):
989
+ """Add people.xml relationship to document.xml.rels if not already present."""
990
+ editor = self["word/_rels/document.xml.rels"]
991
+
992
+ if self._has_relationship(editor, "people.xml"):
993
+ return
994
+
995
+ root = editor.dom.documentElement
996
+ root_tag = root.tagName # type: ignore
997
+ prefix = root_tag.split(":")[0] + ":" if ":" in root_tag else ""
998
+ next_rid = editor.get_next_rid()
999
+
1000
+ # Create the relationship entry
1001
+ rel_xml = f'<{prefix}Relationship Id="{next_rid}" Type="http://schemas.microsoft.com/office/2011/relationships/people" Target="people.xml"/>'
1002
+ editor.append_to(root, rel_xml)
1003
+
1004
+ def _update_settings(self, path, track_revisions=False):
1005
+ """Add RSID and optionally enable track revisions in settings.xml.
1006
+
1007
+ Args:
1008
+ path: Path to settings.xml
1009
+ track_revisions: If True, adds trackRevisions element
1010
+
1011
+ Places elements per OOXML schema order:
1012
+ - trackRevisions: early (before defaultTabStop)
1013
+ - rsids: late (after compat)
1014
+ """
1015
+ editor = self["word/settings.xml"]
1016
+ root = editor.get_node(tag="w:settings")
1017
+ prefix = root.tagName.split(":")[0] if ":" in root.tagName else "w"
1018
+
1019
+ # Conditionally add trackRevisions if requested
1020
+ if track_revisions:
1021
+ track_revisions_exists = any(
1022
+ elem.tagName == f"{prefix}:trackRevisions"
1023
+ for elem in editor.dom.getElementsByTagName(f"{prefix}:trackRevisions")
1024
+ )
1025
+
1026
+ if not track_revisions_exists:
1027
+ track_rev_xml = f"<{prefix}:trackRevisions/>"
1028
+ # Try to insert before documentProtection, defaultTabStop, or at start
1029
+ inserted = False
1030
+ for tag in [f"{prefix}:documentProtection", f"{prefix}:defaultTabStop"]:
1031
+ elements = editor.dom.getElementsByTagName(tag)
1032
+ if elements:
1033
+ editor.insert_before(elements[0], track_rev_xml)
1034
+ inserted = True
1035
+ break
1036
+ if not inserted:
1037
+ # Insert as first child of settings
1038
+ if root.firstChild:
1039
+ editor.insert_before(root.firstChild, track_rev_xml)
1040
+ else:
1041
+ editor.append_to(root, track_rev_xml)
1042
+
1043
+ # Always check if rsids section exists
1044
+ rsids_elements = editor.dom.getElementsByTagName(f"{prefix}:rsids")
1045
+
1046
+ if not rsids_elements:
1047
+ # Add new rsids section
1048
+ rsids_xml = f'''<{prefix}:rsids>
1049
+ <{prefix}:rsidRoot {prefix}:val="{self.rsid}"/>
1050
+ <{prefix}:rsid {prefix}:val="{self.rsid}"/>
1051
+ </{prefix}:rsids>'''
1052
+
1053
+ # Try to insert after compat, before clrSchemeMapping, or before closing tag
1054
+ inserted = False
1055
+ compat_elements = editor.dom.getElementsByTagName(f"{prefix}:compat")
1056
+ if compat_elements:
1057
+ editor.insert_after(compat_elements[0], rsids_xml)
1058
+ inserted = True
1059
+
1060
+ if not inserted:
1061
+ clr_elements = editor.dom.getElementsByTagName(
1062
+ f"{prefix}:clrSchemeMapping"
1063
+ )
1064
+ if clr_elements:
1065
+ editor.insert_before(clr_elements[0], rsids_xml)
1066
+ inserted = True
1067
+
1068
+ if not inserted:
1069
+ editor.append_to(root, rsids_xml)
1070
+ else:
1071
+ # Check if this rsid already exists
1072
+ rsids_elem = rsids_elements[0]
1073
+ rsid_exists = any(
1074
+ elem.getAttribute(f"{prefix}:val") == self.rsid
1075
+ for elem in rsids_elem.getElementsByTagName(f"{prefix}:rsid")
1076
+ )
1077
+
1078
+ if not rsid_exists:
1079
+ rsid_xml = f'<{prefix}:rsid {prefix}:val="{self.rsid}"/>'
1080
+ editor.append_to(rsids_elem, rsid_xml)
1081
+
1082
+ # ==================== Private: XML File Creation ====================
1083
+
1084
+ def _add_to_comments_xml(
1085
+ self, comment_id, para_id, text, author, initials, timestamp
1086
+ ):
1087
+ """Add a single comment to comments.xml."""
1088
+ if not self.comments_path.exists():
1089
+ shutil.copy(TEMPLATE_DIR / "comments.xml", self.comments_path)
1090
+
1091
+ editor = self["word/comments.xml"]
1092
+ root = editor.get_node(tag="w:comments")
1093
+
1094
+ escaped_text = (
1095
+ text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
1096
+ )
1097
+ # Note: w:rsidR, w:rsidRDefault, w:rsidP on w:p, w:rsidR on w:r,
1098
+ # and w:author, w:date, w:initials on w:comment are automatically added by DocxXMLEditor
1099
+ comment_xml = f'''<w:comment w:id="{comment_id}">
1100
+ <w:p w14:paraId="{para_id}" w14:textId="77777777">
1101
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:annotationRef/></w:r>
1102
+ <w:r><w:rPr><w:color w:val="000000"/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>{escaped_text}</w:t></w:r>
1103
+ </w:p>
1104
+ </w:comment>'''
1105
+ editor.append_to(root, comment_xml)
1106
+
1107
+ def _add_to_comments_extended_xml(self, para_id, parent_para_id):
1108
+ """Add a single comment to commentsExtended.xml."""
1109
+ if not self.comments_extended_path.exists():
1110
+ shutil.copy(
1111
+ TEMPLATE_DIR / "commentsExtended.xml", self.comments_extended_path
1112
+ )
1113
+
1114
+ editor = self["word/commentsExtended.xml"]
1115
+ root = editor.get_node(tag="w15:commentsEx")
1116
+
1117
+ if parent_para_id:
1118
+ xml = f'<w15:commentEx w15:paraId="{para_id}" w15:paraIdParent="{parent_para_id}" w15:done="0"/>'
1119
+ else:
1120
+ xml = f'<w15:commentEx w15:paraId="{para_id}" w15:done="0"/>'
1121
+ editor.append_to(root, xml)
1122
+
1123
+ def _add_to_comments_ids_xml(self, para_id, durable_id):
1124
+ """Add a single comment to commentsIds.xml."""
1125
+ if not self.comments_ids_path.exists():
1126
+ shutil.copy(TEMPLATE_DIR / "commentsIds.xml", self.comments_ids_path)
1127
+
1128
+ editor = self["word/commentsIds.xml"]
1129
+ root = editor.get_node(tag="w16cid:commentsIds")
1130
+
1131
+ xml = f'<w16cid:commentId w16cid:paraId="{para_id}" w16cid:durableId="{durable_id}"/>'
1132
+ editor.append_to(root, xml)
1133
+
1134
+ def _add_to_comments_extensible_xml(self, durable_id):
1135
+ """Add a single comment to commentsExtensible.xml."""
1136
+ if not self.comments_extensible_path.exists():
1137
+ shutil.copy(
1138
+ TEMPLATE_DIR / "commentsExtensible.xml", self.comments_extensible_path
1139
+ )
1140
+
1141
+ editor = self["word/commentsExtensible.xml"]
1142
+ root = editor.get_node(tag="w16cex:commentsExtensible")
1143
+
1144
+ xml = f'<w16cex:commentExtensible w16cex:durableId="{durable_id}"/>'
1145
+ editor.append_to(root, xml)
1146
+
1147
+ # ==================== Private: XML Fragments ====================
1148
+
1149
+ def _comment_range_start_xml(self, comment_id):
1150
+ """Generate XML for comment range start."""
1151
+ return f'<w:commentRangeStart w:id="{comment_id}"/>'
1152
+
1153
+ def _comment_range_end_xml(self, comment_id):
1154
+ """Generate XML for comment range end with reference run.
1155
+
1156
+ Note: w:rsidR is automatically added by DocxXMLEditor.
1157
+ """
1158
+ return f'''<w:commentRangeEnd w:id="{comment_id}"/>
1159
+ <w:r>
1160
+ <w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
1161
+ <w:commentReference w:id="{comment_id}"/>
1162
+ </w:r>'''
1163
+
1164
+ def _comment_ref_run_xml(self, comment_id):
1165
+ """Generate XML for comment reference run.
1166
+
1167
+ Note: w:rsidR is automatically added by DocxXMLEditor.
1168
+ """
1169
+ return f'''<w:r>
1170
+ <w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
1171
+ <w:commentReference w:id="{comment_id}"/>
1172
+ </w:r>'''
1173
+
1174
+ # ==================== Private: Metadata Updates ====================
1175
+
1176
+ def _has_relationship(self, editor, target):
1177
+ """Check if a relationship with given target exists."""
1178
+ for rel_elem in editor.dom.getElementsByTagName("Relationship"):
1179
+ if rel_elem.getAttribute("Target") == target:
1180
+ return True
1181
+ return False
1182
+
1183
+ def _has_override(self, editor, part_name):
1184
+ """Check if an override with given part name exists."""
1185
+ for override_elem in editor.dom.getElementsByTagName("Override"):
1186
+ if override_elem.getAttribute("PartName") == part_name:
1187
+ return True
1188
+ return False
1189
+
1190
+ def _has_author(self, editor, author):
1191
+ """Check if an author already exists in people.xml."""
1192
+ for person_elem in editor.dom.getElementsByTagName("w15:person"):
1193
+ if person_elem.getAttribute("w15:author") == author:
1194
+ return True
1195
+ return False
1196
+
1197
+ def _add_author_to_people(self, author):
1198
+ """Add author to people.xml (called during initialization)."""
1199
+ people_path = self.word_path / "people.xml"
1200
+
1201
+ # people.xml should already exist from _setup_tracking
1202
+ if not people_path.exists():
1203
+ raise ValueError("people.xml should exist after _setup_tracking")
1204
+
1205
+ editor = self["word/people.xml"]
1206
+ root = editor.get_node(tag="w15:people")
1207
+
1208
+ # Check if author already exists
1209
+ if self._has_author(editor, author):
1210
+ return
1211
+
1212
+ # Add author with proper XML escaping to prevent injection
1213
+ escaped_author = html.escape(author, quote=True)
1214
+ person_xml = f'''<w15:person w15:author="{escaped_author}">
1215
+ <w15:presenceInfo w15:providerId="None" w15:userId="{escaped_author}"/>
1216
+ </w15:person>'''
1217
+ editor.append_to(root, person_xml)
1218
+
1219
+ def _ensure_comment_relationships(self):
1220
+ """Ensure word/_rels/document.xml.rels has comment relationships."""
1221
+ editor = self["word/_rels/document.xml.rels"]
1222
+
1223
+ if self._has_relationship(editor, "comments.xml"):
1224
+ return
1225
+
1226
+ root = editor.dom.documentElement
1227
+ root_tag = root.tagName # type: ignore
1228
+ prefix = root_tag.split(":")[0] + ":" if ":" in root_tag else ""
1229
+ next_rid_num = int(editor.get_next_rid()[3:])
1230
+
1231
+ # Add relationship elements
1232
+ rels = [
1233
+ (
1234
+ next_rid_num,
1235
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
1236
+ "comments.xml",
1237
+ ),
1238
+ (
1239
+ next_rid_num + 1,
1240
+ "http://schemas.microsoft.com/office/2011/relationships/commentsExtended",
1241
+ "commentsExtended.xml",
1242
+ ),
1243
+ (
1244
+ next_rid_num + 2,
1245
+ "http://schemas.microsoft.com/office/2016/09/relationships/commentsIds",
1246
+ "commentsIds.xml",
1247
+ ),
1248
+ (
1249
+ next_rid_num + 3,
1250
+ "http://schemas.microsoft.com/office/2018/08/relationships/commentsExtensible",
1251
+ "commentsExtensible.xml",
1252
+ ),
1253
+ ]
1254
+
1255
+ for rel_id, rel_type, target in rels:
1256
+ rel_xml = f'<{prefix}Relationship Id="rId{rel_id}" Type="{rel_type}" Target="{target}"/>'
1257
+ editor.append_to(root, rel_xml)
1258
+
1259
+ def _ensure_comment_content_types(self):
1260
+ """Ensure [Content_Types].xml has comment content types."""
1261
+ editor = self["[Content_Types].xml"]
1262
+
1263
+ if self._has_override(editor, "/word/comments.xml"):
1264
+ return
1265
+
1266
+ root = editor.dom.documentElement
1267
+
1268
+ # Add Override elements
1269
+ overrides = [
1270
+ (
1271
+ "/word/comments.xml",
1272
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml",
1273
+ ),
1274
+ (
1275
+ "/word/commentsExtended.xml",
1276
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml",
1277
+ ),
1278
+ (
1279
+ "/word/commentsIds.xml",
1280
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsIds+xml",
1281
+ ),
1282
+ (
1283
+ "/word/commentsExtensible.xml",
1284
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtensible+xml",
1285
+ ),
1286
+ ]
1287
+
1288
+ for part_name, content_type in overrides:
1289
+ override_xml = (
1290
+ f'<Override PartName="{part_name}" ContentType="{content_type}"/>'
1291
+ )
1292
+ editor.append_to(root, override_xml)