spatial-views 0.1.0 → 0.2.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 (223) hide show
  1. package/README.md +201 -19
  2. package/dist/app/assets/{abnfDiagram-S7VZAUFO-BVgtb0Hw.js → abnfDiagram-S7VZAUFO-CFbb_Sz2.js} +1 -1
  3. package/dist/app/assets/{architecture-WOLXFQ4H-DxylGHqT.js → architecture-WOLXFQ4H-CsBD__5a.js} +1 -1
  4. package/dist/app/assets/architectureDiagram-YGSQEFNW-C0ic4ib1.js +36 -0
  5. package/dist/app/assets/{blockDiagram-APKKGZFW-C7fb3MCE.js → blockDiagram-APKKGZFW-BZDPbzAG.js} +1 -1
  6. package/dist/app/assets/{c4Diagram-KJ2TOQU5-Bkb_0fbd.js → c4Diagram-KJ2TOQU5-BilBWuq5.js} +1 -1
  7. package/dist/app/assets/{chunk-6MUBBAK2-DU4vZ03M.js → chunk-6MUBBAK2-BBl-4tQr.js} +1 -1
  8. package/dist/app/assets/{chunk-AXFCVDMN-De96BVTB.js → chunk-AXFCVDMN-BhsttQ-k.js} +1 -1
  9. package/dist/app/assets/{chunk-BG4553SI-J33jsRLV.js → chunk-BG4553SI-mWcPcFZr.js} +2 -2
  10. package/dist/app/assets/{chunk-DQASFODM-Bh85SSGO.js → chunk-DQASFODM-7FKe2gLt.js} +1 -1
  11. package/dist/app/assets/{chunk-HFQBQIA3-CCklgplR.js → chunk-HFQBQIA3-nXrQ08BL.js} +1 -1
  12. package/dist/app/assets/{chunk-KQLB2F66-B04qR8ED.js → chunk-KQLB2F66-swukhFmZ.js} +1 -1
  13. package/dist/app/assets/{chunk-KS47KZWR-BsqDyyAt.js → chunk-KS47KZWR-DkDkwO82.js} +1 -1
  14. package/dist/app/assets/{chunk-LEVMRMFU-DwxrBz2u.js → chunk-LEVMRMFU-DXH6iXv3.js} +1 -1
  15. package/dist/app/assets/{chunk-M57EVK23-D9YIHjps.js → chunk-M57EVK23-B-kEyOJF.js} +1 -1
  16. package/dist/app/assets/{chunk-OY52VR5R-WB63DeTA.js → chunk-OY52VR5R-DweG7qYw.js} +1 -1
  17. package/dist/app/assets/{chunk-QB3YPZ4I-cHzzKdtb.js → chunk-QB3YPZ4I-DyozK3lV.js} +1 -1
  18. package/dist/app/assets/{chunk-R3KSYZAL-C7rDagaa.js → chunk-R3KSYZAL-BEXCUN6J.js} +1 -1
  19. package/dist/app/assets/{chunk-STOXU5M4-CG6pnJtH.js → chunk-STOXU5M4-BaTlty4O.js} +1 -1
  20. package/dist/app/assets/{chunk-TU62E6Q5-BbcGYfy_.js → chunk-TU62E6Q5-_rcUP1Fh.js} +1 -1
  21. package/dist/app/assets/chunk-X3CZISLH-NOpCnvgE.js +1 -0
  22. package/dist/app/assets/{chunk-X4PVCEUW-BUI1Oi3q.js → chunk-X4PVCEUW-DGxTL3kB.js} +1 -1
  23. package/dist/app/assets/{chunk-XXDRQBXY-DYhK5YW-.js → chunk-XXDRQBXY-C02Gpej2.js} +1 -1
  24. package/dist/app/assets/{classDiagram-v2-L25IHNXF-K-k5GTUn.js → classDiagram-v2-L25IHNXF-DLEBwpvk.js} +1 -1
  25. package/dist/app/assets/cose-bilkent-JH36ORCC-kNCXOm8k.js +1 -0
  26. package/dist/app/assets/cynefin-EF2NZ3EQ-6MWNLbuF.js +1 -0
  27. package/dist/app/assets/{cynefinDiagram-L36ESFBY-mkV33_Xx.js → cynefinDiagram-L36ESFBY-CLFstukD.js} +1 -1
  28. package/dist/app/assets/{d3-scale-ygdFCUH8.js → d3-scale-CaJQQYkF.js} +1 -1
  29. package/dist/app/assets/{dagre-SPEB5RXH-BNIymLsu.js → dagre-SPEB5RXH-BaSAny1T.js} +1 -1
  30. package/dist/app/assets/{diagram-3B7GL73J-CGOiolFW.js → diagram-3B7GL73J-DJNlt5Sg.js} +1 -1
  31. package/dist/app/assets/{diagram-DNEMPZML-DZFkHNly.js → diagram-DNEMPZML-DEHeuILB.js} +1 -1
  32. package/dist/app/assets/{diagram-L3MUGCDN-B42GAYLq.js → diagram-L3MUGCDN-ob5Ka2Xc.js} +1 -1
  33. package/dist/app/assets/{diagram-LA4NIPWU-BYuLRYId.js → diagram-LA4NIPWU-DNESzFAc.js} +1 -1
  34. package/dist/app/assets/{diagram-MHCOC4E2-DGzI6TJG.js → diagram-MHCOC4E2-CVi9zKjG.js} +1 -1
  35. package/dist/app/assets/{diagram-XPFHEONP-CTBEgDQ8.js → diagram-XPFHEONP-DInT4C7e.js} +1 -1
  36. package/dist/app/assets/dist-CjvHN74u.js +1 -0
  37. package/dist/app/assets/{ebnfDiagram-UZSYDL2S-GuMdOCvL.js → ebnfDiagram-UZSYDL2S-CRCZNsji.js} +1 -1
  38. package/dist/app/assets/{elk-XV6XMXQE-BpY3loT5.js → elk-XV6XMXQE-D4fsWI2l.js} +1 -1
  39. package/dist/app/assets/{erDiagram-EYBAPDEH-nDZ2PMGl.js → erDiagram-EYBAPDEH-YQNM5StV.js} +1 -1
  40. package/dist/app/assets/{eventmodeling-K75KTNOO-BN42Mgwh.js → eventmodeling-K75KTNOO-Bev08J2F.js} +1 -1
  41. package/dist/app/assets/flowDiagram-MALGAFMO-BhJ554DH.js +1 -0
  42. package/dist/app/assets/ganttDiagram-23REXITY-CkaBMYM4.js +292 -0
  43. package/dist/app/assets/gitGraph-VSP46ZUC-CtbZ0sIS.js +1 -0
  44. package/dist/app/assets/{gitGraphDiagram-STJD7YCW-CDkKLrvO.js → gitGraphDiagram-STJD7YCW-D15ZdQ-Z.js} +1 -1
  45. package/dist/app/assets/index-Bn6_wHEp.js +98 -0
  46. package/dist/app/assets/index-H-Z7MFhZ.css +1 -0
  47. package/dist/app/assets/info-OHQRW6UA-CfUW1XFR.js +1 -0
  48. package/dist/app/assets/{infoDiagram-KBXO4MBG-B92P8pjY.js → infoDiagram-KBXO4MBG-DT7LbBRQ.js} +1 -1
  49. package/dist/app/assets/{ishikawaDiagram-M35NCXHK-BRZsbCvK.js → ishikawaDiagram-M35NCXHK-CF51lhjB.js} +1 -1
  50. package/dist/app/assets/{journeyDiagram-OPLOXDXO-Bppwb1oF.js → journeyDiagram-OPLOXDXO-_36FonKk.js} +1 -1
  51. package/dist/app/assets/{kanban-definition-BZKZHSQY-ByD_x5cm.js → kanban-definition-BZKZHSQY-tLk_OlQ-.js} +1 -1
  52. package/dist/app/assets/{mermaid-parser.core-CKmnV0Md.js → mermaid-parser.core-_3Vo8x6b.js} +3 -3
  53. package/dist/app/assets/mermaid.core-y-x1CYxC.js +44 -0
  54. package/dist/app/assets/{mindmap-definition-I2QAS2VK-fRbUh23C.js → mindmap-definition-I2QAS2VK-BCI4HRX3.js} +1 -1
  55. package/dist/app/assets/packet-JDAUHWVQ-Cco3UoaI.js +1 -0
  56. package/dist/app/assets/{pegDiagram-FD4XPPHP-qIABf6H8.js → pegDiagram-FD4XPPHP-BOzSoNji.js} +1 -1
  57. package/dist/app/assets/pie-XZMESJXO-reOB2GXn.js +1 -0
  58. package/dist/app/assets/{pieDiagram-J4AKIIQI-Bb4RBdrc.js → pieDiagram-J4AKIIQI-PlHpwT_c.js} +1 -1
  59. package/dist/app/assets/{quadrantDiagram-XTYBLID2-B0CpcVWE.js → quadrantDiagram-XTYBLID2-CdxZ0ael.js} +1 -1
  60. package/dist/app/assets/radar-ABXABTNO-DfCl8uV2.js +1 -0
  61. package/dist/app/assets/railroad-I3PHUGI6-4s14UyW1.js +1 -0
  62. package/dist/app/assets/{railroad-abnf-LNEFI6M7-BUhw7Iuo.js → railroad-abnf-LNEFI6M7-C49jJE4Y.js} +1 -1
  63. package/dist/app/assets/{railroad-ebnf-DWYD2UWJ-nQNF56LJ.js → railroad-ebnf-DWYD2UWJ-Bx-pcPc7.js} +1 -1
  64. package/dist/app/assets/{railroad-peg-JR7BVNR7-CwvbVIo6.js → railroad-peg-JR7BVNR7-DjBPSCKV.js} +1 -1
  65. package/dist/app/assets/{railroadDiagram-MRQ6NCDL-Dwr-mPZL.js → railroadDiagram-MRQ6NCDL-wGcmIMqq.js} +1 -1
  66. package/dist/app/assets/{requirementDiagram-Y7LWUY3S-Ctv2DKJM.js → requirementDiagram-Y7LWUY3S-DprFI03z.js} +1 -1
  67. package/dist/app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  68. package/dist/app/assets/{sankeyDiagram-WGJFYUXA-BvKX0eIe.js → sankeyDiagram-WGJFYUXA-DygYWLTR.js} +1 -1
  69. package/dist/app/assets/{sequenceDiagram-5NDRBG5M-Y4YqU2Xt.js → sequenceDiagram-5NDRBG5M-BzIr2DBO.js} +1 -1
  70. package/dist/app/assets/{stateDiagram-v2-FJ4FJNEA-DmjIjn4u.js → stateDiagram-v2-FJ4FJNEA-MGCqKnm_.js} +1 -1
  71. package/dist/app/assets/{swimlanes-UMQ2AFYF-B2O5JCjs.js → swimlanes-UMQ2AFYF-CIEH82Zt.js} +1 -1
  72. package/dist/app/assets/swimlanesDiagram-OMPLPG4O-YWNJjl0g.js +8 -0
  73. package/dist/app/assets/{timeline-definition-SEUHKTQE-DJJdixAI.js → timeline-definition-SEUHKTQE-BGYSqup2.js} +1 -1
  74. package/dist/app/assets/treeView-D4JQ5SDB-B39o1sJo.js +1 -0
  75. package/dist/app/assets/treemap-SAJKECNS-BoGxrUH4.js +1 -0
  76. package/dist/app/assets/{usecaseDiagram-AFA6YXN4-Bp8pXgvR.js → usecaseDiagram-AFA6YXN4-dy6oVOke.js} +1 -1
  77. package/dist/app/assets/{vennDiagram-NNMJR3RB-DYSfZMWT.js → vennDiagram-NNMJR3RB-CUyuhcAk.js} +1 -1
  78. package/dist/app/assets/wardley-7MLQ67FV-sYPAvQ06.js +1 -0
  79. package/dist/app/assets/{wardleyDiagram-S7YFUSEH-CGJ8qg76.js → wardleyDiagram-S7YFUSEH-DYtxYUHe.js} +1 -1
  80. package/dist/app/assets/{xychartDiagram-GGNKY7IX-CRSvraFc.js → xychartDiagram-GGNKY7IX-ClR06AlB.js} +1 -1
  81. package/dist/app/index.html +3 -2
  82. package/dist/cli/index.js +281 -53
  83. package/dist/core/api.js +262 -51
  84. package/dist/core/approvals.js +426 -0
  85. package/dist/core/context.js +68 -3
  86. package/dist/core/diff.js +156 -0
  87. package/dist/core/docs.js +9 -3
  88. package/dist/core/doctor.js +152 -0
  89. package/dist/core/doctypes.js +79 -0
  90. package/dist/core/edit.js +25 -0
  91. package/dist/core/flags.js +13 -0
  92. package/dist/core/gaps.js +7 -2
  93. package/dist/core/heading.js +4 -0
  94. package/dist/core/history.js +36 -5
  95. package/dist/core/home.js +345 -0
  96. package/dist/core/lanes.js +134 -0
  97. package/dist/core/launch.js +34 -0
  98. package/dist/core/layout.js +362 -0
  99. package/dist/core/newdoc.js +66 -0
  100. package/dist/core/open-folder.js +30 -0
  101. package/dist/core/openspec.js +6 -6
  102. package/dist/core/paths.js +71 -4
  103. package/dist/core/project.js +392 -0
  104. package/dist/core/route.js +420 -0
  105. package/dist/core/scan.js +514 -0
  106. package/dist/core/schema.js +170 -1
  107. package/dist/core/scope.js +126 -0
  108. package/dist/core/setup.js +134 -0
  109. package/dist/core/skills.js +92 -14
  110. package/dist/core/storage/config.js +74 -0
  111. package/dist/core/storage/events.js +54 -0
  112. package/dist/core/storage/folder-watch.js +147 -0
  113. package/dist/core/storage/folder.js +469 -0
  114. package/dist/core/storage/index.js +8 -0
  115. package/dist/core/storage/local.js +22 -0
  116. package/dist/core/storage/migrate.js +48 -0
  117. package/dist/core/storage/postgres.js +290 -0
  118. package/dist/core/storage/types.js +101 -0
  119. package/dist/core/storage.js +483 -77
  120. package/dist/core/suggest.js +283 -0
  121. package/dist/core/tree.js +254 -0
  122. package/dist/core/types.js +583 -0
  123. package/dist/core/version.js +1 -1
  124. package/dist/core/versions.js +245 -0
  125. package/dist/core/watch.js +2 -136
  126. package/dist/core/xml.js +23 -5
  127. package/dist/demo/args.js +58 -0
  128. package/dist/demo/browser.js +301 -0
  129. package/dist/demo/platform.js +147 -0
  130. package/dist/demo/run.js +198 -0
  131. package/dist/demo/steps.js +328 -0
  132. package/dist/mcp/project-tools.js +193 -0
  133. package/dist/mcp/server.js +493 -65
  134. package/examples/platform/README.md +33 -0
  135. package/examples/platform/api/package.json +8 -0
  136. package/examples/platform/api/src/auth-middleware.js +16 -0
  137. package/examples/platform/api/src/proxy.js +7 -0
  138. package/examples/platform/api/src/rate-limit.js +13 -0
  139. package/examples/platform/api/src/server.js +40 -0
  140. package/examples/platform/auth/package.json +9 -0
  141. package/examples/platform/auth/src/server.js +24 -0
  142. package/examples/platform/auth/src/tokens.js +19 -0
  143. package/examples/platform/auth/src/users.js +16 -0
  144. package/examples/platform/docs/api/PRD.md +10 -0
  145. package/examples/platform/docs/api/SDD.md +15 -0
  146. package/examples/platform/docs/auth/PRD.md +10 -0
  147. package/examples/platform/docs/auth/SDD.md +8 -0
  148. package/examples/platform/docs/frontend/PRD.md +18 -0
  149. package/examples/platform/docs/frontend/SDD.md +13 -0
  150. package/examples/platform/docs/pricing/PRD.md +9 -0
  151. package/examples/platform/docs/pricing/SDD.md +8 -0
  152. package/examples/platform/docs/products/PRD.md +9 -0
  153. package/examples/platform/docs/products/SDD.md +8 -0
  154. package/examples/platform/frontend/package.json +8 -0
  155. package/examples/platform/frontend/src/api-client.js +21 -0
  156. package/examples/platform/frontend/src/main.js +21 -0
  157. package/examples/platform/frontend/src/pages/products.js +12 -0
  158. package/examples/platform/frontend/src/pages/sign-in.js +14 -0
  159. package/examples/platform/frontend/src/session.js +11 -0
  160. package/examples/platform/pricing/package.json +9 -0
  161. package/examples/platform/pricing/src/cache.js +19 -0
  162. package/examples/platform/pricing/src/product-events.js +17 -0
  163. package/examples/platform/pricing/src/rules.js +19 -0
  164. package/examples/platform/pricing/src/server.js +32 -0
  165. package/examples/platform/products/package.json +9 -0
  166. package/examples/platform/products/src/catalog.js +20 -0
  167. package/examples/platform/products/src/events.js +11 -0
  168. package/examples/platform/products/src/search.js +9 -0
  169. package/examples/platform/products/src/server.js +31 -0
  170. package/migrations/postgres/001_init.sql +55 -0
  171. package/migrations/postgres/002_view_versions.sql +18 -0
  172. package/package.json +18 -2
  173. package/schema/approval.schema.json +173 -0
  174. package/schema/gap.schema.json +3 -0
  175. package/schema/project.schema.json +93 -0
  176. package/schema/settings.schema.json +86 -0
  177. package/schema/toolbox.schema.json +206 -0
  178. package/schema/type.schema.json +121 -0
  179. package/schema/view.schema.json +106 -0
  180. package/skills/claude/build-gap/SKILL.md +7 -0
  181. package/skills/claude/check-ui/SKILL.md +40 -0
  182. package/skills/claude/draw-system/SKILL.md +24 -9
  183. package/skills/claude/gap-from-view/SKILL.md +7 -0
  184. package/skills/claude/post-audit/SKILL.md +58 -0
  185. package/skills/claude/pre-audit/SKILL.md +82 -0
  186. package/skills/claude/update-state/SKILL.md +56 -0
  187. package/skills/cursor/check-ui.mdc +40 -0
  188. package/skills/cursor/draw-system.mdc +24 -9
  189. package/skills/cursor/post-audit.mdc +44 -0
  190. package/skills/cursor/pre-audit.mdc +49 -0
  191. package/skills/cursor/update-state.mdc +45 -0
  192. package/skills/windsurf/spatial-views.md +182 -36
  193. package/toolboxes/aws.yaml +43 -0
  194. package/toolboxes/azure.yaml +41 -0
  195. package/toolboxes/cloud.yaml +95 -0
  196. package/toolboxes/docs.yaml +111 -0
  197. package/toolboxes/software.yaml +114 -0
  198. package/toolboxes/types/docs/gap.yaml +15 -0
  199. package/toolboxes/types/docs/mdd.yaml +19 -0
  200. package/toolboxes/types/docs/plain.yaml +7 -0
  201. package/toolboxes/types/docs/prd.yaml +17 -0
  202. package/dist/app/assets/architectureDiagram-YGSQEFNW-BrBCHqUH.js +0 -36
  203. package/dist/app/assets/chunk-X3CZISLH-FMl4PpWt.js +0 -1
  204. package/dist/app/assets/cose-bilkent-JH36ORCC-BJmLLpaN.js +0 -1
  205. package/dist/app/assets/cynefin-EF2NZ3EQ-DrzwMKdm.js +0 -1
  206. package/dist/app/assets/dist-1Q8ng3tu.js +0 -1
  207. package/dist/app/assets/flowDiagram-MALGAFMO-BCwwiLzb.js +0 -1
  208. package/dist/app/assets/ganttDiagram-23REXITY-xMRHaU3y.js +0 -292
  209. package/dist/app/assets/gitGraph-VSP46ZUC-DyGq5mdd.js +0 -1
  210. package/dist/app/assets/index-BY66fc8Q.js +0 -95
  211. package/dist/app/assets/index-ByetEVbs.css +0 -1
  212. package/dist/app/assets/info-OHQRW6UA-DOBf-fQH.js +0 -1
  213. package/dist/app/assets/mermaid.core-BoRvk1Q-.js +0 -44
  214. package/dist/app/assets/packet-JDAUHWVQ-CgHq03Zj.js +0 -1
  215. package/dist/app/assets/pie-XZMESJXO-DtNh3cDE.js +0 -1
  216. package/dist/app/assets/radar-ABXABTNO-BS0FcsIX.js +0 -1
  217. package/dist/app/assets/railroad-I3PHUGI6-lO8vfGdI.js +0 -1
  218. package/dist/app/assets/swimlanesDiagram-OMPLPG4O-DhCl-Cjg.js +0 -8
  219. package/dist/app/assets/treeView-D4JQ5SDB-Dd1no4eE.js +0 -1
  220. package/dist/app/assets/treemap-SAJKECNS-t6wtsaki.js +0 -1
  221. package/dist/app/assets/wardley-7MLQ67FV-Bx60QTuu.js +0 -1
  222. package/skills/cursor/build-gap.mdc +0 -21
  223. package/skills/cursor/gap-from-view.mdc +0 -22
@@ -0,0 +1,245 @@
1
+ import { z } from "zod";
2
+ import { UserError } from "./errors.js";
3
+ import { diffText, diffView } from "./diff.js";
4
+ import { canonical, ConflictError } from "./storage/types.js";
5
+ /**
6
+ * Versions of Views and Docs. Every save (by a person or the AI) writes a new
7
+ * version, in the same transaction as the save:
8
+ *
9
+ * {n, at, by, note?, name?, restored_from?, data}
10
+ *
11
+ * `by` is "human" for the canvas, or the AI client's name (claude-code, cursor, ...).
12
+ * Folder store: `views/<id>/versions/<n>.json` (Docs: `doc-versions/<path>/<n>.json`);
13
+ * Postgres: the view_versions (doc_versions) table. Restore writes the chosen
14
+ * version as a new one; nothing is deleted, unless the project caps how many
15
+ * unnamed versions each View keeps (settings `keep_versions`; named ones always stay).
16
+ */
17
+ /** Who saves from the canvas. */
18
+ export const HUMAN = "human";
19
+ /** The app itself (a new project's empty System Overview): not a person, so not approved content. */
20
+ export const APP = "spatial-views";
21
+ export const VersionSchema = z.object({
22
+ n: z.number().int().positive(),
23
+ at: z.string(),
24
+ by: z.string().min(1),
25
+ /** What the save was ("restored from v12", "approved approval-3"). */
26
+ note: z.string().optional(),
27
+ /** A name a person or the AI gave it ("before AI tidy"); named versions are never pruned. */
28
+ name: z.string().optional(),
29
+ /** Set on a restore: the version it copied. */
30
+ restored_from: z.number().int().positive().optional(),
31
+ data: z.unknown(),
32
+ });
33
+ const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
34
+ /** "2 modules added, 1 moved, 1 removed; 1 connection added", against the version before (null: the first). */
35
+ export function summarizeView(before, after) {
36
+ if (!before) {
37
+ const parts = [plural(after.modules.length, "module"), plural(after.connections.length, "connection")];
38
+ if (after.messages?.length)
39
+ parts.push(plural(after.messages.length, "message"));
40
+ return parts.join(", ");
41
+ }
42
+ const d = diffView(before, after);
43
+ const mods = { added: 0, changed: 0, moved: 0, removed: 0 };
44
+ for (const m of Object.values(d.modules)) {
45
+ if (m.status === "added")
46
+ mods.added++;
47
+ else if (m.status === "deleted")
48
+ mods.removed++;
49
+ else if (m.status === "amended")
50
+ m.changed.every((c) => c.field === "x" || c.field === "y") ? mods.moved++ : mods.changed++;
51
+ }
52
+ const count = (items) => {
53
+ const c = { added: 0, changed: 0, removed: 0 };
54
+ for (const x of Object.values(items)) {
55
+ if (x.status === "added")
56
+ c.added++;
57
+ else if (x.status === "amended")
58
+ c.changed++;
59
+ else if (x.status === "deleted")
60
+ c.removed++;
61
+ }
62
+ return c;
63
+ };
64
+ const words = (noun, c) => {
65
+ const bits = Object.entries(c).filter(([, n]) => n > 0);
66
+ if (!bits.length)
67
+ return null;
68
+ const [k0, n0] = bits[0];
69
+ return `${plural(n0, noun)} ${k0}${bits.slice(1).map(([k, n]) => `, ${n} ${k}`).join("")}`;
70
+ };
71
+ const parts = [words("module", mods), words("connection", count(d.connections)), words("message", count(d.messages ?? {}))].filter(Boolean);
72
+ if (d.fields.length)
73
+ parts.push(`${d.fields.map((f) => f.field).join(", ")} changed`);
74
+ return parts.length ? parts.join("; ") : "no change";
75
+ }
76
+ /** "2 lines added, 1 changed, 3 removed", against the version before (null: the first). */
77
+ export function summarizeDoc(before, after) {
78
+ if (before === null)
79
+ return plural(after.replace(/\n$/, "").split("\n").length, "line");
80
+ const c = { added: 0, changed: 0, removed: 0 };
81
+ for (const l of diffText(before, after)) {
82
+ if (l.status === "added")
83
+ c.added++;
84
+ else if (l.status === "amended")
85
+ c.changed++;
86
+ else if (l.status === "deleted")
87
+ c.removed++;
88
+ }
89
+ const bits = Object.entries(c).filter(([, n]) => n > 0);
90
+ if (!bits.length)
91
+ return "no change";
92
+ const [k0, n0] = bits[0];
93
+ return `${plural(n0, "line")} ${k0}${bits.slice(1).map(([k, n]) => `, ${n} ${k}`).join("")}`;
94
+ }
95
+ /**
96
+ * The versions of one kind of object (Views or Docs) in one project.
97
+ * Ids are `<object id>@<n>`.
98
+ */
99
+ export class Versions {
100
+ storage;
101
+ project;
102
+ kind;
103
+ parse;
104
+ summarize;
105
+ constructor(storage, project, kind, parse, summarize) {
106
+ this.storage = storage;
107
+ this.project = project;
108
+ this.kind = kind;
109
+ this.parse = parse;
110
+ this.summarize = summarize;
111
+ }
112
+ vid(of, n) {
113
+ return `${of}@${n}`;
114
+ }
115
+ read(x) {
116
+ const v = VersionSchema.parse(x);
117
+ return { ...v, data: this.parse(v.data) };
118
+ }
119
+ /** Every version of an object, oldest first. */
120
+ async list(of) {
121
+ const items = await this.storage.list(this.kind, this.project, { prefix: `${of}@` });
122
+ const out = [];
123
+ for (const it of items) {
124
+ if (it.id.slice(0, it.id.lastIndexOf("@")) !== of)
125
+ continue;
126
+ try {
127
+ out.push(this.read(it.data));
128
+ }
129
+ catch {
130
+ // A broken version is left alone; it must not hide the others.
131
+ }
132
+ }
133
+ return out.sort((a, b) => a.n - b.n);
134
+ }
135
+ /** The history list: newest first, with who, when, the name and a one-line summary. */
136
+ async history(of) {
137
+ const all = await this.list(of);
138
+ const out = [];
139
+ for (const [i, v] of all.entries()) {
140
+ const { data, ...rest } = v;
141
+ const before = i > 0 ? all[i - 1].data : null;
142
+ out.push({ ...rest, summary: v.restored_from ? `restored from v${v.restored_from}` : this.summarize(before, data) });
143
+ }
144
+ return out.reverse();
145
+ }
146
+ async get(of, n) {
147
+ const it = await this.storage.get(this.kind, this.project, this.vid(of, n));
148
+ if (!it)
149
+ throw new UserError(`no version ${n} of ${of}`);
150
+ return this.read(it.data);
151
+ }
152
+ /** Every object that has a version (live or deleted). */
153
+ async objects() {
154
+ const ids = await this.storage.listIds(this.kind, this.project);
155
+ return [...new Set(ids.map((id) => id.slice(0, id.lastIndexOf("@"))))].sort();
156
+ }
157
+ async latest(of) {
158
+ return this.newest(of);
159
+ }
160
+ /** The newest version, reading only the ids and that one version. */
161
+ async newest(of) {
162
+ const ids = await this.storage.listIds(this.kind, this.project, { prefix: `${of}@` });
163
+ let top = 0;
164
+ for (const id of ids) {
165
+ const at = id.lastIndexOf("@");
166
+ if (id.slice(0, at) === of)
167
+ top = Math.max(top, Number(id.slice(at + 1)));
168
+ }
169
+ if (!top)
170
+ return null;
171
+ const it = await this.storage.get(this.kind, this.project, this.vid(of, top));
172
+ return it ? this.read(it.data) : null;
173
+ }
174
+ /**
175
+ * The write that records `data` as the next version of `of` (none when it is
176
+ * the same as the latest version: a save that changed nothing adds nothing).
177
+ */
178
+ async op(of, data, meta = {}) {
179
+ const last = await this.newest(of);
180
+ if (last && !meta.restored_from && canonical(last.data) === canonical(data))
181
+ return null;
182
+ const v = {
183
+ n: (last?.n ?? 0) + 1,
184
+ at: new Date().toISOString(),
185
+ by: meta.by || HUMAN,
186
+ ...(meta.note ? { note: meta.note } : {}),
187
+ ...(meta.restored_from ? { restored_from: meta.restored_from } : {}),
188
+ data,
189
+ };
190
+ return { op: "put", kind: this.kind, project: this.project, id: this.vid(of, v.n), data: v, expected: null };
191
+ }
192
+ /** Name a version ("before AI tidy"), or clear its name with null. */
193
+ async name(of, n, name) {
194
+ const id = this.vid(of, n);
195
+ const it = await this.storage.get(this.kind, this.project, id);
196
+ if (!it)
197
+ throw new UserError(`no version ${n} of ${of}`);
198
+ const v = VersionSchema.parse(it.data);
199
+ const next = { ...v };
200
+ const clean = name?.trim();
201
+ if (clean)
202
+ next.name = clean.slice(0, 120);
203
+ else
204
+ delete next.name;
205
+ await this.storage.put(this.kind, this.project, id, next, { expected: it.version });
206
+ return this.read(next);
207
+ }
208
+ /**
209
+ * Keep the newest `keep` versions of an object, plus every named one (and
210
+ * the newest, always); the rest go. Without a cap, nothing is deleted.
211
+ */
212
+ async prune(of, keep) {
213
+ if (!keep || keep < 1)
214
+ return 0;
215
+ // Under the cap: nothing to read.
216
+ if ((await this.storage.listIds(this.kind, this.project, { prefix: `${of}@` })).length <= keep)
217
+ return 0;
218
+ const all = await this.list(of);
219
+ const cut = all.length - keep;
220
+ let gone = 0;
221
+ for (const v of all.slice(0, Math.max(0, cut))) {
222
+ if (v.name)
223
+ continue;
224
+ if (await this.storage.delete(this.kind, this.project, this.vid(of, v.n)))
225
+ gone++;
226
+ }
227
+ return gone;
228
+ }
229
+ }
230
+ /**
231
+ * Run a save's transaction, retrying when another client took the version
232
+ * number first (the ops are rebuilt each time).
233
+ */
234
+ export async function withVersion(storage, build, by) {
235
+ for (let n = 1;; n++) {
236
+ try {
237
+ return await storage.transact(await build(), by ? { by } : {});
238
+ }
239
+ catch (err) {
240
+ const lost = err instanceof ConflictError && (err.kind === "view_version" || err.kind === "doc_version");
241
+ if (!lost || n >= 5)
242
+ throw err;
243
+ }
244
+ }
245
+ }
@@ -1,136 +1,2 @@
1
- import { watch } from "node:fs";
2
- import { readdir, stat } from "node:fs/promises";
3
- import { join } from "node:path";
4
- const jsonId = (f) => (f.endsWith(".json") ? f.slice(0, -5) : null);
5
- const only = (name, id) => (f) => (f === name ? id : null);
6
- /**
7
- * Watches `.spatial/` and reports what changed, so the canvas can show what
8
- * the AI does through MCP (a separate process) live.
9
- *
10
- * fs.watch triggers a rescan straight away; a slow poll is the fallback where
11
- * fs.watch is unreliable (WSL /mnt drives, network file systems) or a folder
12
- * does not exist yet. A file counts as changed when its mtime, size or inode
13
- * changes (the stores save by rename, so every save is a new inode).
14
- */
15
- export class ViewWatcher {
16
- channels;
17
- pollMs;
18
- seen = new Map();
19
- listeners = new Set();
20
- timer = null;
21
- debounce = null;
22
- scanning = null;
23
- again = false;
24
- ready;
25
- constructor(channels, pollMs = 1000) {
26
- this.channels = channels;
27
- this.pollMs = pollMs;
28
- this.ready = this.scan(false);
29
- this.timer = setInterval(() => void this.rescan(), this.pollMs);
30
- this.timer.unref?.();
31
- }
32
- static forRepo(repoRoot, pollMs) {
33
- const spatial = join(repoRoot, ".spatial");
34
- return new ViewWatcher([
35
- { type: "view", dir: join(spatial, "views"), match: jsonId },
36
- { type: "gap", dir: join(spatial, "gaps"), match: jsonId },
37
- { type: "pins", dir: spatial, match: only("pins.json", "pins") },
38
- { type: "open", dir: join(repoRoot, ".spatial-local"), match: only("open.json", "open") },
39
- ], pollMs);
40
- }
41
- /** Resolves once the first scan (the baseline, which emits nothing) is done. */
42
- whenReady() {
43
- return this.ready;
44
- }
45
- subscribe(fn) {
46
- this.listeners.add(fn);
47
- return () => this.listeners.delete(fn);
48
- }
49
- close() {
50
- if (this.timer)
51
- clearInterval(this.timer);
52
- if (this.debounce)
53
- clearTimeout(this.debounce);
54
- for (const c of this.channels)
55
- (c.fsw?.close(), (c.fsw = null));
56
- this.timer = this.debounce = null;
57
- this.listeners.clear();
58
- }
59
- /** Scan now (serialised: a call during a scan queues one more scan). */
60
- rescan() {
61
- if (this.scanning) {
62
- this.again = true;
63
- return this.scanning;
64
- }
65
- this.scanning = this.scan(true).finally(() => {
66
- this.scanning = null;
67
- if (this.again) {
68
- this.again = false;
69
- void this.rescan();
70
- }
71
- });
72
- return this.scanning;
73
- }
74
- ensureFsWatch(c) {
75
- if (c.fsw || !this.timer)
76
- return;
77
- try {
78
- c.fsw = watch(c.dir, () => {
79
- if (this.debounce)
80
- clearTimeout(this.debounce);
81
- this.debounce = setTimeout(() => void this.rescan(), 30);
82
- });
83
- c.fsw.on("error", () => {
84
- c.fsw?.close();
85
- c.fsw = null; // the poll carries on, and the next scan tries again
86
- });
87
- }
88
- catch {
89
- c.fsw = null;
90
- }
91
- }
92
- async scan(emit) {
93
- const now = new Map();
94
- const meta = new Map();
95
- for (const c of this.channels) {
96
- let files = [];
97
- try {
98
- files = await readdir(c.dir);
99
- this.ensureFsWatch(c);
100
- }
101
- catch {
102
- // The folder isn't there (yet): everything we knew in it is gone.
103
- }
104
- for (const f of files) {
105
- const id = c.match(f);
106
- if (id === null)
107
- continue;
108
- try {
109
- const s = await stat(join(c.dir, f));
110
- const key = `${c.type}:${id}`;
111
- now.set(key, `${s.mtimeMs}:${s.size}:${s.ino}`);
112
- meta.set(key, { type: c.type, id });
113
- }
114
- catch {
115
- // Removed between readdir and stat; the next scan settles it.
116
- }
117
- }
118
- }
119
- const events = [];
120
- for (const [key, sig] of now)
121
- if (this.seen.get(key) !== sig)
122
- events.push({ ...meta.get(key), removed: false });
123
- for (const key of this.seen.keys()) {
124
- if (now.has(key))
125
- continue;
126
- const at = key.indexOf(":");
127
- events.push({ type: key.slice(0, at), id: key.slice(at + 1), removed: true });
128
- }
129
- this.seen = now;
130
- if (!emit)
131
- return;
132
- for (const e of events)
133
- for (const fn of this.listeners)
134
- fn(e);
135
- }
136
- }
1
+ /** The folder store's change stream, under its old path. */
2
+ export { ViewWatcher } from "./storage/folder-watch.js";
package/dist/core/xml.js CHANGED
@@ -19,28 +19,46 @@ function linkAttrs(m, all) {
19
19
  /**
20
20
  * The XML the AI reads for a View. A linked module carries LinkedView (the id)
21
21
  * and LinkedViewName; a link to a View that no longer exists says Missing="true".
22
+ * Lanes come first; a module says its Lane, and a connection across lanes says
23
+ * Dependency="true". A sequence View lists Participants, then its Messages in order.
22
24
  */
23
25
  export function viewToXml(view, all = [], indent = "") {
24
26
  const byId = new Map(view.modules.map((m) => [m.id, m.name]));
27
+ const laneOf = new Map(view.modules.map((m) => [m.id, m.lane]));
28
+ const laneName = new Map((view.lanes ?? []).map((l) => [l.id, l.label]));
29
+ const seq = view.kind === "sequence";
25
30
  const i = indent;
26
31
  const lines = [
27
- `${i}<View${attr("Id", view.id)}${attr("Name", view.name)}${attr("Type", view.type)}${view.draft ? ` Draft="true"` : ""}>`,
32
+ `${i}<View${attr("Id", view.id)}${attr("Name", view.name)}${attr("Type", view.type)}${seq ? ` Kind="sequence"` : ""}${view.draft ? ` Draft="true"` : ""}>`,
28
33
  ];
34
+ for (const l of view.lanes ?? []) {
35
+ const names = view.modules.filter((m) => m.lane === l.id).map((m) => m.name).join(", ");
36
+ lines.push(`${i} <Lane${attr("Id", l.id)}${attr("Label", l.label)}${attr("Modules", names)}/>`);
37
+ }
29
38
  for (const m of view.modules) {
30
39
  const outs = view.connections.filter((c) => c.from === m.id);
31
- const open = `${i} <Module${attr("Id", m.id)}${attr("Name", m.name)}${linkAttrs(m, all)}`;
40
+ const tag = seq ? "Participant" : "Module";
41
+ const open = `${i} <${tag}${attr("Id", m.id)}${attr("Name", m.name)}${attr("Type", m.type)}${attr("Lane", m.lane ? (laneName.get(m.lane) ?? m.lane) : undefined)}${linkAttrs(m, all)}`;
32
42
  const body = [];
33
43
  if (m.description)
34
44
  body.push(`${i} <Description>${esc(m.description)}</Description>`);
35
45
  for (const f of m.files ?? [])
36
46
  body.push(`${i} <File${attr("Path", f)}/>`);
37
- for (const c of outs)
38
- body.push(`${i} <Connection${attr("GoTo", byId.get(c.to) ?? c.to)}${attr("Value", c.label)}/>`);
47
+ for (const c of outs) {
48
+ const a = laneOf.get(c.from);
49
+ const b = laneOf.get(c.to);
50
+ const dep = a && b && a !== b ? ` Dependency="true"${attr("ToLane", laneName.get(b) ?? b)}` : "";
51
+ body.push(`${i} <Connection${attr("GoTo", byId.get(c.to) ?? c.to)}${attr("Value", c.label)}${dep}/>`);
52
+ }
39
53
  if (body.length === 0)
40
54
  lines.push(`${open}/>`);
41
55
  else
42
- lines.push(`${open}>`, ...body, `${i} </Module>`);
56
+ lines.push(`${open}>`, ...body, `${i} </${tag}>`);
43
57
  }
58
+ (view.messages ?? []).forEach((msg, n) => {
59
+ const a = `${attr("Order", String(n + 1))}${attr("From", byId.get(msg.from) ?? msg.from)}${attr("To", byId.get(msg.to) ?? msg.to)}${attr("Kind", msg.kind)}`;
60
+ lines.push(msg.text ? `${i} <Message${a}>${esc(msg.text)}</Message>` : `${i} <Message${a}/>`);
61
+ });
44
62
  for (const f of view.features ?? []) {
45
63
  const names = f.module_ids.map((id) => byId.get(id) ?? id).join(", ");
46
64
  lines.push(`${i} <Feature${attr("Id", f.id)}${attr("Name", f.name)}${attr("Modules", names)}/>`);
@@ -0,0 +1,58 @@
1
+ import { UserError } from "../core/errors.js";
2
+ export const DEFAULT_DELAY = 2500;
3
+ export const DEMO_USAGE = `usage: spatial-views demo [--auto [--delay <ms>]] [--step <n>] [--port <n>]
4
+ space next step (in the browser window, or in this terminal)
5
+ --auto advance on a timer instead (--delay <ms> between steps, default ${DEFAULT_DELAY})
6
+ --step <n> start at step n; the earlier steps' state is set up at once, for retakes
7
+ --port <n> the canvas port (default: a free one)
8
+ q, Ctrl+C stop`;
9
+ /** The value after a flag (`--step 3` or `--step=3`), or an error naming the flag. */
10
+ function valueOf(argv, i, name) {
11
+ const a = argv[i];
12
+ if (a.startsWith(`${name}=`))
13
+ return { value: a.slice(name.length + 1), next: i };
14
+ const v = argv[i + 1];
15
+ if (v === undefined || v.startsWith("--"))
16
+ throw new UserError(`${name} needs a value\n${DEMO_USAGE}`);
17
+ return { value: v, next: i + 1 };
18
+ }
19
+ function int(value, name, min, max) {
20
+ const n = Number(value);
21
+ if (!Number.isInteger(n) || n < min || n > max)
22
+ throw new UserError(`${name} must be a whole number from ${min} to ${max}, not "${value}"`);
23
+ return n;
24
+ }
25
+ /** Parse the arguments after `demo`. `steps` is how many steps there are (for --step). */
26
+ export function parseDemoArgs(argv, steps) {
27
+ const out = { auto: false, delay: DEFAULT_DELAY, step: 1, headless: false };
28
+ let delayGiven = false;
29
+ for (let i = 0; i < argv.length; i++) {
30
+ const a = argv[i];
31
+ const flag = a.split("=")[0];
32
+ if (a === "--auto")
33
+ out.auto = true;
34
+ else if (a === "--headless")
35
+ out.headless = true;
36
+ else if (flag === "--delay") {
37
+ const { value, next } = valueOf(argv, i, "--delay");
38
+ out.delay = int(value, "--delay", 0, 600_000);
39
+ delayGiven = true;
40
+ i = next;
41
+ }
42
+ else if (flag === "--step") {
43
+ const { value, next } = valueOf(argv, i, "--step");
44
+ out.step = int(value, "--step", 1, steps);
45
+ i = next;
46
+ }
47
+ else if (flag === "--port") {
48
+ const { value, next } = valueOf(argv, i, "--port");
49
+ out.port = int(value, "--port", 1, 65535);
50
+ i = next;
51
+ }
52
+ else
53
+ throw new UserError(`spatial-views demo: unknown option "${a}"\n${DEMO_USAGE}`);
54
+ }
55
+ if (delayGiven && !out.auto)
56
+ throw new UserError(`--delay goes with --auto\n${DEMO_USAGE}`);
57
+ return out;
58
+ }