spatial-views 0.0.0-stage → 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 (220) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +245 -2
  3. package/dist/app/assets/abnfDiagram-S7VZAUFO-CFbb_Sz2.js +1 -0
  4. package/dist/app/assets/architecture-WOLXFQ4H-CsBD__5a.js +1 -0
  5. package/dist/app/assets/architectureDiagram-YGSQEFNW-C0ic4ib1.js +36 -0
  6. package/dist/app/assets/blockDiagram-APKKGZFW-BZDPbzAG.js +140 -0
  7. package/dist/app/assets/bpmnIcons-APHXDYXF-C0jbCElo.js +1 -0
  8. package/dist/app/assets/c4Diagram-KJ2TOQU5-BilBWuq5.js +38 -0
  9. package/dist/app/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
  10. package/dist/app/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
  11. package/dist/app/assets/chunk-6MUBBAK2-BBl-4tQr.js +2 -0
  12. package/dist/app/assets/chunk-AXFCVDMN-BhsttQ-k.js +62 -0
  13. package/dist/app/assets/chunk-BG4553SI-mWcPcFZr.js +127 -0
  14. package/dist/app/assets/chunk-DQASFODM-7FKe2gLt.js +10 -0
  15. package/dist/app/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
  16. package/dist/app/assets/chunk-HFQBQIA3-nXrQ08BL.js +1 -0
  17. package/dist/app/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
  18. package/dist/app/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
  19. package/dist/app/assets/chunk-KQLB2F66-swukhFmZ.js +2 -0
  20. package/dist/app/assets/chunk-KS47KZWR-DkDkwO82.js +1 -0
  21. package/dist/app/assets/chunk-LEVMRMFU-DXH6iXv3.js +1 -0
  22. package/dist/app/assets/chunk-M57EVK23-B-kEyOJF.js +1 -0
  23. package/dist/app/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
  24. package/dist/app/assets/chunk-OY52VR5R-DweG7qYw.js +88 -0
  25. package/dist/app/assets/chunk-QB3YPZ4I-DyozK3lV.js +213 -0
  26. package/dist/app/assets/chunk-R3KSYZAL-BEXCUN6J.js +1 -0
  27. package/dist/app/assets/chunk-STOXU5M4-BaTlty4O.js +72 -0
  28. package/dist/app/assets/chunk-TU62E6Q5-_rcUP1Fh.js +1 -0
  29. package/dist/app/assets/chunk-X3CZISLH-NOpCnvgE.js +1 -0
  30. package/dist/app/assets/chunk-X4PVCEUW-DGxTL3kB.js +1 -0
  31. package/dist/app/assets/chunk-XXDRQBXY-C02Gpej2.js +1 -0
  32. package/dist/app/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
  33. package/dist/app/assets/classDiagram-v2-L25IHNXF-DLEBwpvk.js +217 -0
  34. package/dist/app/assets/cose-bilkent-JH36ORCC-kNCXOm8k.js +1 -0
  35. package/dist/app/assets/cynefin-EF2NZ3EQ-6MWNLbuF.js +1 -0
  36. package/dist/app/assets/cynefinDiagram-L36ESFBY-CLFstukD.js +62 -0
  37. package/dist/app/assets/cytoscape.esm-nTrd5EVq.js +321 -0
  38. package/dist/app/assets/d3-scale-CaJQQYkF.js +1 -0
  39. package/dist/app/assets/dagre-SPEB5RXH-BaSAny1T.js +4 -0
  40. package/dist/app/assets/diagram-3B7GL73J-DJNlt5Sg.js +200 -0
  41. package/dist/app/assets/diagram-DNEMPZML-DEHeuILB.js +24 -0
  42. package/dist/app/assets/diagram-L3MUGCDN-ob5Ka2Xc.js +41 -0
  43. package/dist/app/assets/diagram-LA4NIPWU-DNESzFAc.js +24 -0
  44. package/dist/app/assets/diagram-MHCOC4E2-CVi9zKjG.js +3 -0
  45. package/dist/app/assets/diagram-XPFHEONP-DInT4C7e.js +30 -0
  46. package/dist/app/assets/dist-CjvHN74u.js +1 -0
  47. package/dist/app/assets/ebnfDiagram-UZSYDL2S-CRCZNsji.js +1 -0
  48. package/dist/app/assets/elk-XV6XMXQE-D4fsWI2l.js +2 -0
  49. package/dist/app/assets/erDiagram-EYBAPDEH-YQNM5StV.js +99 -0
  50. package/dist/app/assets/eventmodeling-K75KTNOO-Bev08J2F.js +1 -0
  51. package/dist/app/assets/flowDiagram-MALGAFMO-BhJ554DH.js +1 -0
  52. package/dist/app/assets/ganttDiagram-23REXITY-CkaBMYM4.js +292 -0
  53. package/dist/app/assets/gitGraph-VSP46ZUC-CtbZ0sIS.js +1 -0
  54. package/dist/app/assets/gitGraphDiagram-STJD7YCW-D15ZdQ-Z.js +106 -0
  55. package/dist/app/assets/graphlib-DS17s2tU.js +1 -0
  56. package/dist/app/assets/index-Bn6_wHEp.js +98 -0
  57. package/dist/app/assets/index-H-Z7MFhZ.css +1 -0
  58. package/dist/app/assets/info-OHQRW6UA-CfUW1XFR.js +1 -0
  59. package/dist/app/assets/infoDiagram-KBXO4MBG-DT7LbBRQ.js +2 -0
  60. package/dist/app/assets/ishikawaDiagram-M35NCXHK-CF51lhjB.js +70 -0
  61. package/dist/app/assets/journeyDiagram-OPLOXDXO-_36FonKk.js +139 -0
  62. package/dist/app/assets/kanban-definition-BZKZHSQY-tLk_OlQ-.js +89 -0
  63. package/dist/app/assets/katex-MEuSR36T.js +257 -0
  64. package/dist/app/assets/mermaid-parser.core-_3Vo8x6b.js +7 -0
  65. package/dist/app/assets/mermaid.core-y-x1CYxC.js +44 -0
  66. package/dist/app/assets/mindmap-definition-I2QAS2VK-BCI4HRX3.js +98 -0
  67. package/dist/app/assets/packet-JDAUHWVQ-Cco3UoaI.js +1 -0
  68. package/dist/app/assets/pegDiagram-FD4XPPHP-BOzSoNji.js +1 -0
  69. package/dist/app/assets/pie-XZMESJXO-reOB2GXn.js +1 -0
  70. package/dist/app/assets/pieDiagram-J4AKIIQI-PlHpwT_c.js +39 -0
  71. package/dist/app/assets/quadrantDiagram-XTYBLID2-CdxZ0ael.js +7 -0
  72. package/dist/app/assets/radar-ABXABTNO-DfCl8uV2.js +1 -0
  73. package/dist/app/assets/railroad-I3PHUGI6-4s14UyW1.js +1 -0
  74. package/dist/app/assets/railroad-abnf-LNEFI6M7-C49jJE4Y.js +1 -0
  75. package/dist/app/assets/railroad-ebnf-DWYD2UWJ-Bx-pcPc7.js +1 -0
  76. package/dist/app/assets/railroad-peg-JR7BVNR7-DjBPSCKV.js +1 -0
  77. package/dist/app/assets/railroadDiagram-MRQ6NCDL-wGcmIMqq.js +1 -0
  78. package/dist/app/assets/requirementDiagram-Y7LWUY3S-DprFI03z.js +84 -0
  79. package/dist/app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  80. package/dist/app/assets/rough.esm-Dy-Kn_BL.js +1 -0
  81. package/dist/app/assets/sankeyDiagram-WGJFYUXA-DygYWLTR.js +40 -0
  82. package/dist/app/assets/sequenceDiagram-5NDRBG5M-BzIr2DBO.js +169 -0
  83. package/dist/app/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
  84. package/dist/app/assets/stateDiagram-v2-FJ4FJNEA-MGCqKnm_.js +291 -0
  85. package/dist/app/assets/swimlanes-UMQ2AFYF-CIEH82Zt.js +1 -0
  86. package/dist/app/assets/swimlanesDiagram-OMPLPG4O-YWNJjl0g.js +8 -0
  87. package/dist/app/assets/timeline-definition-SEUHKTQE-BGYSqup2.js +120 -0
  88. package/dist/app/assets/treeView-D4JQ5SDB-B39o1sJo.js +1 -0
  89. package/dist/app/assets/treemap-SAJKECNS-BoGxrUH4.js +1 -0
  90. package/dist/app/assets/usecaseDiagram-AFA6YXN4-dy6oVOke.js +328 -0
  91. package/dist/app/assets/vennDiagram-NNMJR3RB-CUyuhcAk.js +34 -0
  92. package/dist/app/assets/wardley-7MLQ67FV-sYPAvQ06.js +1 -0
  93. package/dist/app/assets/wardleyDiagram-S7YFUSEH-DYtxYUHe.js +78 -0
  94. package/dist/app/assets/xychartDiagram-GGNKY7IX-ClR06AlB.js +7 -0
  95. package/dist/app/index.html +14 -0
  96. package/dist/cli/index.js +338 -0
  97. package/dist/core/api.js +420 -0
  98. package/dist/core/approvals.js +426 -0
  99. package/dist/core/context.js +259 -0
  100. package/dist/core/diff.js +156 -0
  101. package/dist/core/docs.js +140 -0
  102. package/dist/core/doctor.js +152 -0
  103. package/dist/core/doctypes.js +79 -0
  104. package/dist/core/edit.js +130 -0
  105. package/dist/core/errors.js +3 -0
  106. package/dist/core/flags.js +13 -0
  107. package/dist/core/gaps.js +119 -0
  108. package/dist/core/heading.js +4 -0
  109. package/dist/core/history.js +50 -0
  110. package/dist/core/home.js +345 -0
  111. package/dist/core/lanes.js +134 -0
  112. package/dist/core/launch.js +34 -0
  113. package/dist/core/layout.js +362 -0
  114. package/dist/core/links.js +68 -0
  115. package/dist/core/mermaid.js +175 -0
  116. package/dist/core/newdoc.js +66 -0
  117. package/dist/core/open-folder.js +30 -0
  118. package/dist/core/openspec.js +120 -0
  119. package/dist/core/paths.js +90 -0
  120. package/dist/core/project.js +392 -0
  121. package/dist/core/resolve.js +35 -0
  122. package/dist/core/route.js +420 -0
  123. package/dist/core/scan.js +514 -0
  124. package/dist/core/schema.js +270 -0
  125. package/dist/core/scope.js +126 -0
  126. package/dist/core/setup.js +134 -0
  127. package/dist/core/skills.js +181 -0
  128. package/dist/core/storage/config.js +74 -0
  129. package/dist/core/storage/events.js +54 -0
  130. package/dist/core/storage/folder-watch.js +147 -0
  131. package/dist/core/storage/folder.js +469 -0
  132. package/dist/core/storage/index.js +8 -0
  133. package/dist/core/storage/local.js +22 -0
  134. package/dist/core/storage/migrate.js +48 -0
  135. package/dist/core/storage/postgres.js +290 -0
  136. package/dist/core/storage/types.js +101 -0
  137. package/dist/core/storage.js +589 -0
  138. package/dist/core/suggest.js +283 -0
  139. package/dist/core/tree.js +254 -0
  140. package/dist/core/types.js +583 -0
  141. package/dist/core/version.js +1 -0
  142. package/dist/core/versions.js +245 -0
  143. package/dist/core/watch.js +2 -0
  144. package/dist/core/xml.js +68 -0
  145. package/dist/demo/args.js +58 -0
  146. package/dist/demo/browser.js +301 -0
  147. package/dist/demo/platform.js +147 -0
  148. package/dist/demo/run.js +198 -0
  149. package/dist/demo/steps.js +328 -0
  150. package/dist/mcp/project-tools.js +193 -0
  151. package/dist/mcp/server.js +785 -0
  152. package/examples/platform/README.md +33 -0
  153. package/examples/platform/api/package.json +8 -0
  154. package/examples/platform/api/src/auth-middleware.js +16 -0
  155. package/examples/platform/api/src/proxy.js +7 -0
  156. package/examples/platform/api/src/rate-limit.js +13 -0
  157. package/examples/platform/api/src/server.js +40 -0
  158. package/examples/platform/auth/package.json +9 -0
  159. package/examples/platform/auth/src/server.js +24 -0
  160. package/examples/platform/auth/src/tokens.js +19 -0
  161. package/examples/platform/auth/src/users.js +16 -0
  162. package/examples/platform/docs/api/PRD.md +10 -0
  163. package/examples/platform/docs/api/SDD.md +15 -0
  164. package/examples/platform/docs/auth/PRD.md +10 -0
  165. package/examples/platform/docs/auth/SDD.md +8 -0
  166. package/examples/platform/docs/frontend/PRD.md +18 -0
  167. package/examples/platform/docs/frontend/SDD.md +13 -0
  168. package/examples/platform/docs/pricing/PRD.md +9 -0
  169. package/examples/platform/docs/pricing/SDD.md +8 -0
  170. package/examples/platform/docs/products/PRD.md +9 -0
  171. package/examples/platform/docs/products/SDD.md +8 -0
  172. package/examples/platform/frontend/package.json +8 -0
  173. package/examples/platform/frontend/src/api-client.js +21 -0
  174. package/examples/platform/frontend/src/main.js +21 -0
  175. package/examples/platform/frontend/src/pages/products.js +12 -0
  176. package/examples/platform/frontend/src/pages/sign-in.js +14 -0
  177. package/examples/platform/frontend/src/session.js +11 -0
  178. package/examples/platform/pricing/package.json +9 -0
  179. package/examples/platform/pricing/src/cache.js +19 -0
  180. package/examples/platform/pricing/src/product-events.js +17 -0
  181. package/examples/platform/pricing/src/rules.js +19 -0
  182. package/examples/platform/pricing/src/server.js +32 -0
  183. package/examples/platform/products/package.json +9 -0
  184. package/examples/platform/products/src/catalog.js +20 -0
  185. package/examples/platform/products/src/events.js +11 -0
  186. package/examples/platform/products/src/search.js +9 -0
  187. package/examples/platform/products/src/server.js +31 -0
  188. package/migrations/postgres/001_init.sql +55 -0
  189. package/migrations/postgres/002_view_versions.sql +18 -0
  190. package/package.json +69 -4
  191. package/schema/approval.schema.json +173 -0
  192. package/schema/gap.schema.json +56 -0
  193. package/schema/pins.schema.json +122 -0
  194. package/schema/project.schema.json +93 -0
  195. package/schema/settings.schema.json +86 -0
  196. package/schema/toolbox.schema.json +206 -0
  197. package/schema/type.schema.json +121 -0
  198. package/schema/view.schema.json +240 -0
  199. package/skills/claude/build-gap/SKILL.md +28 -0
  200. package/skills/claude/check-ui/SKILL.md +40 -0
  201. package/skills/claude/draw-system/SKILL.md +38 -0
  202. package/skills/claude/gap-from-view/SKILL.md +29 -0
  203. package/skills/claude/post-audit/SKILL.md +58 -0
  204. package/skills/claude/pre-audit/SKILL.md +82 -0
  205. package/skills/claude/update-state/SKILL.md +56 -0
  206. package/skills/cursor/check-ui.mdc +40 -0
  207. package/skills/cursor/draw-system.mdc +38 -0
  208. package/skills/cursor/post-audit.mdc +44 -0
  209. package/skills/cursor/pre-audit.mdc +49 -0
  210. package/skills/cursor/update-state.mdc +45 -0
  211. package/skills/windsurf/spatial-views.md +212 -0
  212. package/toolboxes/aws.yaml +43 -0
  213. package/toolboxes/azure.yaml +41 -0
  214. package/toolboxes/cloud.yaml +95 -0
  215. package/toolboxes/docs.yaml +111 -0
  216. package/toolboxes/software.yaml +114 -0
  217. package/toolboxes/types/docs/gap.yaml +15 -0
  218. package/toolboxes/types/docs/mdd.yaml +19 -0
  219. package/toolboxes/types/docs/plain.yaml +7 -0
  220. package/toolboxes/types/docs/prd.yaml +17 -0
@@ -0,0 +1,270 @@
1
+ import { z } from "zod";
2
+ import { TypeEntrySchema, ToolboxFileSchema } from "./types.js";
3
+ /**
4
+ * The data in `.spatial/`, one JSON file per View and per Gap, plus pins.json.
5
+ * React Flow reads the modules and connections directly; the AI is served the
6
+ * same data as XML. `npm run schema` writes these as JSON Schema to schema/.
7
+ */
8
+ const Id = z.string().regex(/^[A-Za-z0-9_-]+$/, "an id is letters, digits, - and _");
9
+ export const ViewTypeSchema = z.enum(["high-level", "repo", "sub-view", "feature"]);
10
+ export const ModuleSchema = z.object({
11
+ id: z.string().min(1),
12
+ name: z.string(),
13
+ x: z.number().default(0),
14
+ y: z.number().default(0),
15
+ description: z.string().optional(),
16
+ /** The sub-view this module drills into (the View hierarchy). */
17
+ linked_view_id: z.string().min(1).optional(),
18
+ /** Source files on the module, repo-relative. */
19
+ files: z.array(z.string().min(1)).optional(),
20
+ /** The swim lane (a lane id on this View) the module sits in: who owns the work. */
21
+ lane: z.string().min(1).optional(),
22
+ /** Its module type (types/<id>.yaml): the icon, colour, size and properties schema. */
23
+ type: z.string().min(1).optional(),
24
+ /** Its property values, by the type's property names. */
25
+ properties: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(),
26
+ });
27
+ /** A point on the canvas (a bend in a connection). */
28
+ export const PointSchema = z.object({ x: z.number(), y: z.number() });
29
+ export const ConnectionSchema = z.object({
30
+ id: z.string().min(1),
31
+ from: z.string().min(1),
32
+ to: z.string().min(1),
33
+ label: z.string().optional(),
34
+ /** Bend points a person placed, in order from `from` to `to`. Left out: routed automatically (orthogonal). */
35
+ bends: z.array(PointSchema).optional(),
36
+ });
37
+ /** A Feature: a named group of modules on one View (made with the lasso). */
38
+ export const FeatureSchema = z.object({
39
+ id: Id,
40
+ name: z.string().min(1),
41
+ module_ids: z.array(z.string().min(1)).default([]),
42
+ });
43
+ /** A swim lane: one per team or person, drawn as a horizontal band. */
44
+ export const LaneSchema = z.object({
45
+ id: Id,
46
+ label: z.string().min(1),
47
+ });
48
+ /** flow (the default): modules and connections. sequence: participants (the modules, left to right) and ordered messages. */
49
+ export const ViewKindSchema = z.enum(["flow", "sequence"]);
50
+ export const MessageKindSchema = z.enum(["sync", "async", "reply"]);
51
+ /** One message in a sequence View, from one participant to another, in order. */
52
+ export const MessageSchema = z.object({
53
+ id: z.string().min(1),
54
+ from: z.string().min(1),
55
+ to: z.string().min(1),
56
+ text: z.string(),
57
+ kind: MessageKindSchema.optional(),
58
+ });
59
+ export const ViewSchema = z.object({
60
+ id: Id,
61
+ name: z.string(),
62
+ type: ViewTypeSchema.default("sub-view"),
63
+ /** sequence: the modules are the participants and `messages` the ordered calls. Left out: flow. */
64
+ kind: ViewKindSchema.optional(),
65
+ /** Swim lanes, top to bottom. */
66
+ lanes: z.array(LaneSchema).optional(),
67
+ /** Sequence Views: the messages, in order. */
68
+ messages: z.array(MessageSchema).optional(),
69
+ modules: z.array(ModuleSchema).default([]),
70
+ connections: z.array(ConnectionSchema).default([]),
71
+ features: z.array(FeatureSchema).optional(),
72
+ /** The positions are still the auto-layout's: the AI may lay the View out again. Cleared once a person moves anything. */
73
+ auto_layout: z.boolean().optional(),
74
+ /** A View drafted by import (from a Mermaid flowchart), not yet reviewed. */
75
+ draft: z.boolean().optional(),
76
+ /** Where a drafted View came from: `<path>#<n>` (the n-th Mermaid block). */
77
+ source: z.string().optional(),
78
+ });
79
+ export const GapStateSchema = z.enum(["open", "building", "done", "cancelled"]);
80
+ /** A Gap, one per file in `.spatial/gaps/<id>.json`. */
81
+ export const GapSchema = z.object({
82
+ id: Id,
83
+ title: z.string().min(1),
84
+ intent: z.string().default(""),
85
+ view_id: z.string().min(1),
86
+ module_ids: z.array(z.string().min(1)).default([]),
87
+ state: GapStateSchema.default("open"),
88
+ /** The OpenSpec change (openspec/changes/<id>/) this Gap follows. */
89
+ openspec_change: z.string().optional(),
90
+ milestone: z.string().optional(),
91
+ /** Raised by an audit (drift, a missing requirement) and not yet confirmed by a person. */
92
+ draft: z.boolean().optional(),
93
+ });
94
+ /** A pin maps a repo path (a spec, a change folder, any .md) to a part of the picture. */
95
+ export const PinSchema = z.discriminatedUnion("kind", [
96
+ z.object({ path: z.string().min(1), kind: z.literal("view"), view_id: z.string().min(1) }),
97
+ z.object({
98
+ path: z.string().min(1),
99
+ kind: z.literal("module"),
100
+ view_id: z.string().min(1),
101
+ module_id: z.string().min(1),
102
+ }),
103
+ z.object({
104
+ path: z.string().min(1),
105
+ kind: z.literal("feature"),
106
+ view_id: z.string().min(1),
107
+ feature_id: z.string().min(1),
108
+ }),
109
+ z.object({ path: z.string().min(1), kind: z.literal("gap"), gap_id: z.string().min(1) }),
110
+ ]);
111
+ /** `.spatial/pins.json`. The docs themselves are never rewritten to hold pins. */
112
+ export const PinsFileSchema = z.object({
113
+ /** Extra doc folders added by import_docs (docs/ and openspec/ are always read). */
114
+ sources: z.array(z.string().min(1)).default([]),
115
+ pins: z.array(PinSchema).default([]),
116
+ });
117
+ // ---- Projects (M1) ------------------------------------------------------
118
+ export const SourceKindSchema = z.enum(["repo", "docs", "diagrams"]);
119
+ export const AuditKindSchema = z.enum(["pre-audit", "update-state", "post-audit"]);
120
+ export const SourceSchema = z.object({
121
+ id: Id,
122
+ /** What people call it (the folder name unless given). */
123
+ name: z.string().optional(),
124
+ kind: SourceKindSchema,
125
+ /** As given: Windows (`c:\git\api`) or POSIX (`/home/me/api`), or relative to the project. */
126
+ path: z.string().min(1),
127
+ added: z.string(),
128
+ /** The commit (repos) or ISO time (docs, diagrams) of the last audit that read this source. */
129
+ last_audit_sha: z.string().optional(),
130
+ });
131
+ export const AuditSchema = z.object({
132
+ kind: AuditKindSchema,
133
+ at: z.string(),
134
+ /** The report, as pinned: `reports/<name>.md` (`.spatial/reports/<name>.md` in a single-repo project). */
135
+ report: z.string().optional(),
136
+ });
137
+ export const ProjectSchema = z.object({
138
+ name: z.string().min(1),
139
+ /** The folder name under `~/SpatialViews/projects/` (home projects). */
140
+ slug: z.string().optional(),
141
+ created: z.string(),
142
+ /** The View the project always opens on (set by create_project and pre-audit; default system-overview). */
143
+ root_view_id: Id.optional(),
144
+ sources: z.array(SourceSchema).default([]),
145
+ audits: z.array(AuditSchema).default([]),
146
+ });
147
+ // ---- Approvals (M1) ---------------------------------------------------------
148
+ /** What one change in an Approval is about: a View, a Gap, the pin map or a doc (repo path). */
149
+ export const ChangeTargetSchema = z.enum(["view", "gap", "pins", "doc"]);
150
+ export const ChangeOpSchema = z.enum(["create", "update", "delete"]);
151
+ export const DecisionSchema = z.enum(["approved", "denied"]);
152
+ export const ApprovalStatusSchema = z.enum(["pending", "approved", "denied"]);
153
+ /**
154
+ * One change in an Approval: the whole before and after of one file, so the
155
+ * review screen can draw both. Every MCP write to the same file in an Approval
156
+ * folds into one change (the first before, the last after).
157
+ */
158
+ export const ApprovalChangeSchema = z.object({
159
+ id: Id,
160
+ target: ChangeTargetSchema,
161
+ /** View id, Gap id, "pins", or the doc's repo path. */
162
+ ref: z.string().min(1),
163
+ op: ChangeOpSchema,
164
+ /** JSON for views, gaps and pins; text for docs. null: did not exist / removed. */
165
+ before: z.unknown().nullable(),
166
+ after: z.unknown().nullable(),
167
+ /** The MCP tools that made it, in order. */
168
+ tools: z.array(z.string()).default([]),
169
+ at: z.string(),
170
+ /** The reviewer's call on this one change (none: goes with the whole Approval). */
171
+ decision: DecisionSchema.optional(),
172
+ });
173
+ /** One amendment the AI made to a pending draft. */
174
+ export const RevisionSchema = z.object({
175
+ /** 1, 2, 3, ... in order. */
176
+ n: z.number().int().positive(),
177
+ at: z.string(),
178
+ /** Who made it (the MCP client's name). */
179
+ by: z.string().min(1),
180
+ /** What it changed, e.g. "1 module added; 1 connection added". */
181
+ summary: z.string(),
182
+ /** The MCP tool that made it. */
183
+ tool: z.string().optional(),
184
+ });
185
+ /**
186
+ * `<data>/approvals/<id>.json`: a draft of one View or Doc (or a Gap, or the
187
+ * pin map), pending for a person and never live until approved. No send
188
+ * step: each AI write lands here at once, and a write to a file that already
189
+ * has a pending Approval folds into it as a new revision. Its one change
190
+ * holds the base (`before`: the live content, at `base_version`) and the head
191
+ * (`after`: the latest draft). Older Approvals may hold several changes.
192
+ */
193
+ export const ApprovalSchema = z.object({
194
+ id: Id,
195
+ /** People's number for it in the project: #12. */
196
+ n: z.number().int().positive().optional(),
197
+ /** Generated from the changes, e.g. "Cursor: amended View Checkout". */
198
+ title: z.string().min(1),
199
+ /** The IDE that made it (the MCP client's name: claude-code, cursor, ...). */
200
+ author: z.string().min(1),
201
+ created: z.string(),
202
+ status: ApprovalStatusSchema.default("pending"),
203
+ /** The agent run (MCP server session) that last amended it. */
204
+ session: z.string().optional(),
205
+ decided: z.string().optional(),
206
+ /** A note from the reviewer, read back by get_approval. */
207
+ note: z.string().optional(),
208
+ /** The version of the live content the draft starts from (Views and Docs; null: none yet). */
209
+ base_version: z.number().int().positive().nullable().optional(),
210
+ /** Each amendment, oldest first. Approve names the one it saw (rev = their count). */
211
+ revisions: z.array(RevisionSchema).default([]),
212
+ changes: z.array(ApprovalChangeSchema).default([]),
213
+ });
214
+ /** `<data>/settings.json`: a project's settings. */
215
+ export const SettingsSchema = z.object({
216
+ /** approval (default): "AI changes need approval", writes land in Approvals. direct: "Apply directly" (M0). */
217
+ ai_changes: z.enum(["approval", "direct"]).default("approval"),
218
+ /** The canvas opens the View the AI is changing and shows the change as it happens. */
219
+ follow: z.boolean().default(true),
220
+ /** Toolbox ids turned off for this project (they leave the canvas's bar; modules of their types still draw). */
221
+ disabled_toolboxes: z.array(z.string()).default([]),
222
+ /** How many unnamed versions each View and Doc keeps (the newest; named ones always stay). Unset or null: all of them. */
223
+ keep_versions: z.number().int().positive().nullable().optional(),
224
+ /**
225
+ * How the project writes its specs. spatial-views (default): PRD, MDD and Gap
226
+ * docs from the doc-type templates. openspec: openspec/specs and
227
+ * openspec/changes in the OpenSpec layout; new docs are OpenSpec changes.
228
+ */
229
+ spec_format: z.enum(["spatial-views", "openspec"]).default("spatial-views"),
230
+ /**
231
+ * The Project panel's folders (Specs, Docs, Diagrams, Logic, Software, Cloud
232
+ * by default; people add, rename and nest them: a folder's parent is the folder it sits in) and where each doc ("doc:<path>")
233
+ * or View ("view:<id>") was put. Anything not placed goes in its default folder.
234
+ */
235
+ tree: z
236
+ .object({
237
+ folders: z.array(z.object({ id: z.string().min(1), name: z.string().min(1).max(60), parent: z.string().min(1).optional() })).optional(),
238
+ place: z.record(z.string(), z.string()).optional(),
239
+ })
240
+ .optional(),
241
+ });
242
+ export function summarize(view) {
243
+ const s = { id: view.id, name: view.name, type: view.type, modules: view.modules.length };
244
+ if (view.draft)
245
+ s.draft = true;
246
+ if (view.kind === "sequence")
247
+ s.kind = "sequence";
248
+ return s;
249
+ }
250
+ /** Where the published JSON Schemas live (schema/*.schema.json in the repo). */
251
+ export const SCHEMA_BASE = "https://raw.githubusercontent.com/Hotchilli-Cloud/spatial-views/main/schema/";
252
+ /** The JSON Schemas for the file kinds in `.spatial/`. */
253
+ export function jsonSchemas() {
254
+ const one = (name, title, schema) => ({
255
+ $schema: "https://json-schema.org/draft/2020-12/schema",
256
+ $id: `${SCHEMA_BASE}${name}.schema.json`,
257
+ title,
258
+ ...z.toJSONSchema(schema, { io: "input", target: "draft-2020-12" }),
259
+ });
260
+ return {
261
+ view: one("view", "Spatial Views: a View (.spatial/views/<id>.json)", ViewSchema),
262
+ gap: one("gap", "Spatial Views: a Gap (.spatial/gaps/<id>.json)", GapSchema),
263
+ pins: one("pins", "Spatial Views: the pin map (.spatial/pins.json)", PinsFileSchema),
264
+ project: one("project", "Spatial Views: a project and its sources (~/SpatialViews/projects/<slug>/project.json)", ProjectSchema),
265
+ approval: one("approval", "Spatial Views: an Approval: a pending AI draft of one View or Doc, with its revisions (<data>/approvals/<id>.json)", ApprovalSchema),
266
+ settings: one("settings", "Spatial Views: a project's settings (<data>/settings.json)", SettingsSchema),
267
+ toolbox: one("toolbox", "Spatial Views: a toolbox (toolboxes/<id>.yaml): its module types and doc types", ToolboxFileSchema),
268
+ type: one("type", "Spatial Views: a module type, as written in a toolbox's types list", TypeEntrySchema),
269
+ };
270
+ }
@@ -0,0 +1,126 @@
1
+ import { lstat, readlink, realpath } from "node:fs/promises";
2
+ import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
3
+ import { UserError } from "./errors.js";
4
+ import { macReal } from "./paths.js";
5
+ /** The refusal when no project is active (PRD §9.1). */
6
+ export const PICK_PROJECT = "pick a project: list_projects, then use_project";
7
+ /** A path outside the active project's sources. The message lists what is allowed. */
8
+ export class ScopeError extends UserError {
9
+ }
10
+ /** No project is active yet. */
11
+ export class NoProjectError extends UserError {
12
+ constructor() {
13
+ super(PICK_PROJECT);
14
+ }
15
+ }
16
+ /**
17
+ * The real path of `p`, following symlinks. Where `p` does not exist yet, the
18
+ * nearest folder above it that does is resolved, and the rest is put back.
19
+ */
20
+ export async function realPath(p) {
21
+ const abs = resolve(p);
22
+ const rest = [];
23
+ let at = abs;
24
+ for (;;) {
25
+ const real = await realpath(at).catch(() => null);
26
+ if (real)
27
+ return rest.length ? join(real, ...rest.reverse()) : real;
28
+ const up = dirname(at);
29
+ if (up === at)
30
+ return abs;
31
+ rest.push(basename(at));
32
+ at = up;
33
+ }
34
+ }
35
+ /** True when `child` is `parent` or inside it (both real, absolute paths). */
36
+ export function isInside(parent, child) {
37
+ const rel = relative(parent, child);
38
+ return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
39
+ }
40
+ /** "api (/src/api), web (/src/web)": for refusals. */
41
+ export function describeRoots(roots) {
42
+ return roots.length ? roots.map((r) => `${r.id} (${r.abs})`).join(", ") : "none yet (add_source)";
43
+ }
44
+ /**
45
+ * `p` relative to a root, when it is written inside it: under the root's real
46
+ * path or as the root was given (macOS: /var is /private/var). Null otherwise.
47
+ * Compared as text: `p` itself is not looked at.
48
+ */
49
+ function relIn(base, given, p) {
50
+ for (const want of [p, macReal(p)])
51
+ for (const from of [base, given])
52
+ if (isInside(from, want))
53
+ return relative(from, want);
54
+ return null;
55
+ }
56
+ /**
57
+ * Follow `rel` down from `base` one name at a time. A symlink is read (it is
58
+ * inside `base`) and followed only if it points inside `base`; a link out is
59
+ * refused without its target being touched. Names that don't exist yet are
60
+ * kept as written. Returns the real path, or null for a link out.
61
+ */
62
+ async function follow(base, given, rel, depth = 0) {
63
+ if (depth > 40)
64
+ return null;
65
+ let cur = base;
66
+ const parts = rel.split(/[\\/]+/).filter((x) => x && x !== ".");
67
+ for (let i = 0; i < parts.length; i++) {
68
+ const next = parts[i] === ".." ? dirname(cur) : join(cur, parts[i]);
69
+ if (!isInside(base, next))
70
+ return null;
71
+ const st = await lstat(next).catch(() => null);
72
+ if (!st)
73
+ return join(next, ...parts.slice(i + 1));
74
+ if (st.isSymbolicLink()) {
75
+ const target = resolve(dirname(next), await readlink(next));
76
+ const to = relIn(base, given, target);
77
+ if (to === null)
78
+ return null;
79
+ const real = await follow(base, given, to, depth + 1);
80
+ if (!real)
81
+ return null;
82
+ cur = real;
83
+ }
84
+ else
85
+ cur = next;
86
+ }
87
+ return cur;
88
+ }
89
+ /**
90
+ * Where `abs` really is, if that is inside `root` (symlinks and `..` resolved).
91
+ * Throws a ScopeError otherwise. Nothing outside the root is ever looked at.
92
+ */
93
+ export async function realInside(root, abs, allowed = [root]) {
94
+ const base = await realPath(root.abs);
95
+ const refuse = () => new ScopeError(`${abs} is outside the project's sources. Allowed: ${describeRoots(allowed)}`);
96
+ const want = resolve(abs);
97
+ const given = resolve(root.abs);
98
+ const rel = relIn(base, given, want);
99
+ // Not even written inside the root: refused without looking at it.
100
+ if (rel === null)
101
+ throw refuse();
102
+ const real = await follow(base, given, rel);
103
+ if (!real || !isInside(base, real))
104
+ throw refuse();
105
+ return real;
106
+ }
107
+ /**
108
+ * The root that really holds `abs`, or null. A path that is not even written
109
+ * inside a root (as given, or under the root's real path) is refused without
110
+ * touching the disk, so a folder outside every source is never so much as
111
+ * looked at. Inside one, symlinks are then resolved and checked again.
112
+ */
113
+ export async function rootOf(roots, abs) {
114
+ const want = resolve(abs);
115
+ for (const r of roots) {
116
+ const base = await realPath(r.abs);
117
+ const given = resolve(r.abs);
118
+ const rel = relIn(base, given, want);
119
+ if (rel === null)
120
+ continue;
121
+ const real = await follow(base, given, rel);
122
+ if (real && isInside(base, real))
123
+ return { root: r, rel: relative(base, real).split(/[\\/]/).join("/") || "." };
124
+ }
125
+ return null;
126
+ }
@@ -0,0 +1,134 @@
1
+ import { execFile } from "node:child_process";
2
+ import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { dirname, join, posix } from "node:path";
5
+ import { promisify } from "node:util";
6
+ import { readSkillFile, shellQuote, skillNames } from "./skills.js";
7
+ export const IDES = ["cursor", "claude", "windsurf"];
8
+ /** The MCP entry: plain, or through `wsl bash -lc` for a Windows IDE talking to WSL. */
9
+ export function mcpEntry(launch, wsl, env) {
10
+ const [command, ...pre] = launch;
11
+ const args = [...pre, "mcp", "--global"];
12
+ const vars = Object.entries(env ?? {});
13
+ if (!wsl)
14
+ return vars.length ? { command: command, args, env: { ...env } } : { command: command, args };
15
+ const line = [...vars.map(([k, v]) => `${k}=${shellQuote(v)}`), ...[command, ...args].map(shellQuote)].join(" ");
16
+ return { command: "wsl", args: ["bash", "-lc", line] };
17
+ }
18
+ /** Where each IDE keeps its global MCP servers. */
19
+ export function configPath(ide, home) {
20
+ switch (ide) {
21
+ case "cursor":
22
+ return join(home, ".cursor", "mcp.json");
23
+ case "claude":
24
+ return join(home, ".claude.json");
25
+ case "windsurf":
26
+ return join(home, ".codeium", "windsurf", "mcp_config.json");
27
+ }
28
+ }
29
+ /**
30
+ * The config files for an IDE. Outside WSL: the one in ~. In WSL, Cursor and
31
+ * Windsurf read the Windows profile; Cursor in WSL Remote mode also reads the
32
+ * WSL-side ~/.cursor/mcp.json, so it gets the Linux entry there as well.
33
+ */
34
+ export function configTargets(ide, home, windowsHome, wsl) {
35
+ if (!wsl || ide === "claude")
36
+ return [{ ide, file: configPath(ide, home), windows: false }];
37
+ const win = { ide, file: windowsHome ? configPath(ide, windowsHome) : null, windows: true };
38
+ return ide === "cursor" ? [win, { ide, file: configPath(ide, home), windows: false }] : [win];
39
+ }
40
+ /** `C:\Users\tony` as WSL sees it: `/mnt/c/Users/tony`. */
41
+ export function wslPath(winPath) {
42
+ const m = /^([A-Za-z]):[\\/](.*)$/.exec(winPath.trim());
43
+ if (!m)
44
+ return null;
45
+ return posix.join("/mnt", m[1].toLowerCase(), ...m[2].split(/[\\/]+/).filter(Boolean));
46
+ }
47
+ /** The Windows profile, from WSL: SPATIAL_VIEWS_WIN_HOME, else what cmd.exe says %USERPROFILE% is. */
48
+ export async function findWindowsHome(env = process.env) {
49
+ if (env.SPATIAL_VIEWS_WIN_HOME?.trim())
50
+ return wslPath(env.SPATIAL_VIEWS_WIN_HOME) ?? env.SPATIAL_VIEWS_WIN_HOME.trim();
51
+ try {
52
+ const { stdout } = await promisify(execFile)("cmd.exe", ["/d", "/c", "echo %USERPROFILE%"], { timeout: 5000, cwd: "/mnt/c" });
53
+ return wslPath(stdout.trim());
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ const exists = (p) => stat(p).then(() => true, () => false);
60
+ async function writeAtomic(file, body) {
61
+ await mkdir(dirname(file), { recursive: true });
62
+ const tmp = `${file}.${process.pid}.${Math.random().toString(36).slice(2, 8)}.tmp`;
63
+ await writeFile(tmp, body, "utf8");
64
+ await rename(tmp, file);
65
+ }
66
+ /** Put our entries in an `mcpServers` JSON file, leaving everything else in it as it was. */
67
+ async function mergeEntries(file, entries, o, log) {
68
+ let data = {};
69
+ if (await exists(file)) {
70
+ try {
71
+ data = JSON.parse(await readFile(file, "utf8"));
72
+ }
73
+ catch {
74
+ log.push(`skipped ${file} (not valid JSON; add this by hand under mcpServers: ${JSON.stringify(entries)})`);
75
+ return;
76
+ }
77
+ }
78
+ const servers = { ...(data.mcpServers ?? {}) };
79
+ let changed = false;
80
+ for (const [name, entry] of Object.entries(entries)) {
81
+ const had = servers[name];
82
+ if (had && JSON.stringify(had) === JSON.stringify(entry)) {
83
+ log.push(`ok ${name} in ${file} (already set up)`);
84
+ continue;
85
+ }
86
+ if (had && !o.force) {
87
+ log.push(`kept ${name} in ${file} (it has another ${name} entry; --force replaces it)`);
88
+ continue;
89
+ }
90
+ servers[name] = entry;
91
+ changed = true;
92
+ log.push(`${o.dryRun ? "would " : ""}${had ? "replace" : "add"} ${name} in ${file}`);
93
+ }
94
+ if (!changed || o.dryRun)
95
+ return;
96
+ data.mcpServers = servers;
97
+ await writeAtomic(file, JSON.stringify(data, null, 2) + "\n");
98
+ }
99
+ export async function setup(opts) {
100
+ const log = [];
101
+ const home = opts.home ?? homedir();
102
+ const o = { force: opts.force === true, dryRun: opts.dryRun === true };
103
+ const wsl = opts.wsl === true;
104
+ if (wsl)
105
+ log.push("WSL found: Cursor and Windsurf get `spatial-views` (absolute node path, for WSL Remote mode) and `spatial-views-win` (`wsl bash -lc`, for a plain Windows folder); Cursor also gets `spatial-views` in the WSL-side ~/.cursor/mcp.json.");
106
+ for (const ide of opts.ides) {
107
+ // Claude Code runs where you run it (inside WSL); Cursor and Windsurf keep their config on Windows,
108
+ // and Cursor in WSL Remote mode also reads the WSL-side ~/.cursor/mcp.json.
109
+ for (const t of configTargets(ide, home, opts.windowsHome, wsl)) {
110
+ const entries = { "spatial-views": mcpEntry(opts.launch, false, opts.env) };
111
+ if (t.windows)
112
+ entries["spatial-views-win"] = mcpEntry(opts.launch, true, opts.env);
113
+ if (!t.file) {
114
+ log.push(`skipped ${ide}: the Windows profile was not found (set SPATIAL_VIEWS_WIN_HOME=C:\\Users\\<you>), add by hand: ${JSON.stringify({ mcpServers: entries })}`);
115
+ continue;
116
+ }
117
+ await mergeEntries(t.file, entries, o, log);
118
+ }
119
+ }
120
+ if (opts.ides.includes("claude") && opts.skillsDir) {
121
+ for (const n of skillNames()) {
122
+ const file = join(home, ".claude", "skills", n, "SKILL.md");
123
+ if ((await exists(file)) && !o.force) {
124
+ log.push(`kept ${file} (exists; --force replaces it)`);
125
+ continue;
126
+ }
127
+ if (!o.dryRun)
128
+ await writeAtomic(file, await readSkillFile(opts.skillsDir, n));
129
+ log.push(`${o.dryRun ? "would write" : "wrote"} ${file}`);
130
+ }
131
+ }
132
+ log.push("", "Restart the IDE. The server starts with no projects: ask for `pre-audit <name> <paths...>`, or", "list_projects, create_project and add_source. The Skills are also served as MCP prompts.", "Check it with `spatial-views doctor`.");
133
+ return log;
134
+ }