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.
- package/LICENSE +21 -0
- package/README.md +245 -2
- package/dist/app/assets/abnfDiagram-S7VZAUFO-CFbb_Sz2.js +1 -0
- package/dist/app/assets/architecture-WOLXFQ4H-CsBD__5a.js +1 -0
- package/dist/app/assets/architectureDiagram-YGSQEFNW-C0ic4ib1.js +36 -0
- package/dist/app/assets/blockDiagram-APKKGZFW-BZDPbzAG.js +140 -0
- package/dist/app/assets/bpmnIcons-APHXDYXF-C0jbCElo.js +1 -0
- package/dist/app/assets/c4Diagram-KJ2TOQU5-BilBWuq5.js +38 -0
- package/dist/app/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/dist/app/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/dist/app/assets/chunk-6MUBBAK2-BBl-4tQr.js +2 -0
- package/dist/app/assets/chunk-AXFCVDMN-BhsttQ-k.js +62 -0
- package/dist/app/assets/chunk-BG4553SI-mWcPcFZr.js +127 -0
- package/dist/app/assets/chunk-DQASFODM-7FKe2gLt.js +10 -0
- package/dist/app/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/dist/app/assets/chunk-HFQBQIA3-nXrQ08BL.js +1 -0
- package/dist/app/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
- package/dist/app/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/dist/app/assets/chunk-KQLB2F66-swukhFmZ.js +2 -0
- package/dist/app/assets/chunk-KS47KZWR-DkDkwO82.js +1 -0
- package/dist/app/assets/chunk-LEVMRMFU-DXH6iXv3.js +1 -0
- package/dist/app/assets/chunk-M57EVK23-B-kEyOJF.js +1 -0
- package/dist/app/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
- package/dist/app/assets/chunk-OY52VR5R-DweG7qYw.js +88 -0
- package/dist/app/assets/chunk-QB3YPZ4I-DyozK3lV.js +213 -0
- package/dist/app/assets/chunk-R3KSYZAL-BEXCUN6J.js +1 -0
- package/dist/app/assets/chunk-STOXU5M4-BaTlty4O.js +72 -0
- package/dist/app/assets/chunk-TU62E6Q5-_rcUP1Fh.js +1 -0
- package/dist/app/assets/chunk-X3CZISLH-NOpCnvgE.js +1 -0
- package/dist/app/assets/chunk-X4PVCEUW-DGxTL3kB.js +1 -0
- package/dist/app/assets/chunk-XXDRQBXY-C02Gpej2.js +1 -0
- package/dist/app/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/dist/app/assets/classDiagram-v2-L25IHNXF-DLEBwpvk.js +217 -0
- package/dist/app/assets/cose-bilkent-JH36ORCC-kNCXOm8k.js +1 -0
- package/dist/app/assets/cynefin-EF2NZ3EQ-6MWNLbuF.js +1 -0
- package/dist/app/assets/cynefinDiagram-L36ESFBY-CLFstukD.js +62 -0
- package/dist/app/assets/cytoscape.esm-nTrd5EVq.js +321 -0
- package/dist/app/assets/d3-scale-CaJQQYkF.js +1 -0
- package/dist/app/assets/dagre-SPEB5RXH-BaSAny1T.js +4 -0
- package/dist/app/assets/diagram-3B7GL73J-DJNlt5Sg.js +200 -0
- package/dist/app/assets/diagram-DNEMPZML-DEHeuILB.js +24 -0
- package/dist/app/assets/diagram-L3MUGCDN-ob5Ka2Xc.js +41 -0
- package/dist/app/assets/diagram-LA4NIPWU-DNESzFAc.js +24 -0
- package/dist/app/assets/diagram-MHCOC4E2-CVi9zKjG.js +3 -0
- package/dist/app/assets/diagram-XPFHEONP-DInT4C7e.js +30 -0
- package/dist/app/assets/dist-CjvHN74u.js +1 -0
- package/dist/app/assets/ebnfDiagram-UZSYDL2S-CRCZNsji.js +1 -0
- package/dist/app/assets/elk-XV6XMXQE-D4fsWI2l.js +2 -0
- package/dist/app/assets/erDiagram-EYBAPDEH-YQNM5StV.js +99 -0
- package/dist/app/assets/eventmodeling-K75KTNOO-Bev08J2F.js +1 -0
- package/dist/app/assets/flowDiagram-MALGAFMO-BhJ554DH.js +1 -0
- package/dist/app/assets/ganttDiagram-23REXITY-CkaBMYM4.js +292 -0
- package/dist/app/assets/gitGraph-VSP46ZUC-CtbZ0sIS.js +1 -0
- package/dist/app/assets/gitGraphDiagram-STJD7YCW-D15ZdQ-Z.js +106 -0
- package/dist/app/assets/graphlib-DS17s2tU.js +1 -0
- package/dist/app/assets/index-Bn6_wHEp.js +98 -0
- package/dist/app/assets/index-H-Z7MFhZ.css +1 -0
- package/dist/app/assets/info-OHQRW6UA-CfUW1XFR.js +1 -0
- package/dist/app/assets/infoDiagram-KBXO4MBG-DT7LbBRQ.js +2 -0
- package/dist/app/assets/ishikawaDiagram-M35NCXHK-CF51lhjB.js +70 -0
- package/dist/app/assets/journeyDiagram-OPLOXDXO-_36FonKk.js +139 -0
- package/dist/app/assets/kanban-definition-BZKZHSQY-tLk_OlQ-.js +89 -0
- package/dist/app/assets/katex-MEuSR36T.js +257 -0
- package/dist/app/assets/mermaid-parser.core-_3Vo8x6b.js +7 -0
- package/dist/app/assets/mermaid.core-y-x1CYxC.js +44 -0
- package/dist/app/assets/mindmap-definition-I2QAS2VK-BCI4HRX3.js +98 -0
- package/dist/app/assets/packet-JDAUHWVQ-Cco3UoaI.js +1 -0
- package/dist/app/assets/pegDiagram-FD4XPPHP-BOzSoNji.js +1 -0
- package/dist/app/assets/pie-XZMESJXO-reOB2GXn.js +1 -0
- package/dist/app/assets/pieDiagram-J4AKIIQI-PlHpwT_c.js +39 -0
- package/dist/app/assets/quadrantDiagram-XTYBLID2-CdxZ0ael.js +7 -0
- package/dist/app/assets/radar-ABXABTNO-DfCl8uV2.js +1 -0
- package/dist/app/assets/railroad-I3PHUGI6-4s14UyW1.js +1 -0
- package/dist/app/assets/railroad-abnf-LNEFI6M7-C49jJE4Y.js +1 -0
- package/dist/app/assets/railroad-ebnf-DWYD2UWJ-Bx-pcPc7.js +1 -0
- package/dist/app/assets/railroad-peg-JR7BVNR7-DjBPSCKV.js +1 -0
- package/dist/app/assets/railroadDiagram-MRQ6NCDL-wGcmIMqq.js +1 -0
- package/dist/app/assets/requirementDiagram-Y7LWUY3S-DprFI03z.js +84 -0
- package/dist/app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/app/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/dist/app/assets/sankeyDiagram-WGJFYUXA-DygYWLTR.js +40 -0
- package/dist/app/assets/sequenceDiagram-5NDRBG5M-BzIr2DBO.js +169 -0
- package/dist/app/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/dist/app/assets/stateDiagram-v2-FJ4FJNEA-MGCqKnm_.js +291 -0
- package/dist/app/assets/swimlanes-UMQ2AFYF-CIEH82Zt.js +1 -0
- package/dist/app/assets/swimlanesDiagram-OMPLPG4O-YWNJjl0g.js +8 -0
- package/dist/app/assets/timeline-definition-SEUHKTQE-BGYSqup2.js +120 -0
- package/dist/app/assets/treeView-D4JQ5SDB-B39o1sJo.js +1 -0
- package/dist/app/assets/treemap-SAJKECNS-BoGxrUH4.js +1 -0
- package/dist/app/assets/usecaseDiagram-AFA6YXN4-dy6oVOke.js +328 -0
- package/dist/app/assets/vennDiagram-NNMJR3RB-CUyuhcAk.js +34 -0
- package/dist/app/assets/wardley-7MLQ67FV-sYPAvQ06.js +1 -0
- package/dist/app/assets/wardleyDiagram-S7YFUSEH-DYtxYUHe.js +78 -0
- package/dist/app/assets/xychartDiagram-GGNKY7IX-ClR06AlB.js +7 -0
- package/dist/app/index.html +14 -0
- package/dist/cli/index.js +338 -0
- package/dist/core/api.js +420 -0
- package/dist/core/approvals.js +426 -0
- package/dist/core/context.js +259 -0
- package/dist/core/diff.js +156 -0
- package/dist/core/docs.js +140 -0
- package/dist/core/doctor.js +152 -0
- package/dist/core/doctypes.js +79 -0
- package/dist/core/edit.js +130 -0
- package/dist/core/errors.js +3 -0
- package/dist/core/flags.js +13 -0
- package/dist/core/gaps.js +119 -0
- package/dist/core/heading.js +4 -0
- package/dist/core/history.js +50 -0
- package/dist/core/home.js +345 -0
- package/dist/core/lanes.js +134 -0
- package/dist/core/launch.js +34 -0
- package/dist/core/layout.js +362 -0
- package/dist/core/links.js +68 -0
- package/dist/core/mermaid.js +175 -0
- package/dist/core/newdoc.js +66 -0
- package/dist/core/open-folder.js +30 -0
- package/dist/core/openspec.js +120 -0
- package/dist/core/paths.js +90 -0
- package/dist/core/project.js +392 -0
- package/dist/core/resolve.js +35 -0
- package/dist/core/route.js +420 -0
- package/dist/core/scan.js +514 -0
- package/dist/core/schema.js +270 -0
- package/dist/core/scope.js +126 -0
- package/dist/core/setup.js +134 -0
- package/dist/core/skills.js +181 -0
- package/dist/core/storage/config.js +74 -0
- package/dist/core/storage/events.js +54 -0
- package/dist/core/storage/folder-watch.js +147 -0
- package/dist/core/storage/folder.js +469 -0
- package/dist/core/storage/index.js +8 -0
- package/dist/core/storage/local.js +22 -0
- package/dist/core/storage/migrate.js +48 -0
- package/dist/core/storage/postgres.js +290 -0
- package/dist/core/storage/types.js +101 -0
- package/dist/core/storage.js +589 -0
- package/dist/core/suggest.js +283 -0
- package/dist/core/tree.js +254 -0
- package/dist/core/types.js +583 -0
- package/dist/core/version.js +1 -0
- package/dist/core/versions.js +245 -0
- package/dist/core/watch.js +2 -0
- package/dist/core/xml.js +68 -0
- package/dist/demo/args.js +58 -0
- package/dist/demo/browser.js +301 -0
- package/dist/demo/platform.js +147 -0
- package/dist/demo/run.js +198 -0
- package/dist/demo/steps.js +328 -0
- package/dist/mcp/project-tools.js +193 -0
- package/dist/mcp/server.js +785 -0
- package/examples/platform/README.md +33 -0
- package/examples/platform/api/package.json +8 -0
- package/examples/platform/api/src/auth-middleware.js +16 -0
- package/examples/platform/api/src/proxy.js +7 -0
- package/examples/platform/api/src/rate-limit.js +13 -0
- package/examples/platform/api/src/server.js +40 -0
- package/examples/platform/auth/package.json +9 -0
- package/examples/platform/auth/src/server.js +24 -0
- package/examples/platform/auth/src/tokens.js +19 -0
- package/examples/platform/auth/src/users.js +16 -0
- package/examples/platform/docs/api/PRD.md +10 -0
- package/examples/platform/docs/api/SDD.md +15 -0
- package/examples/platform/docs/auth/PRD.md +10 -0
- package/examples/platform/docs/auth/SDD.md +8 -0
- package/examples/platform/docs/frontend/PRD.md +18 -0
- package/examples/platform/docs/frontend/SDD.md +13 -0
- package/examples/platform/docs/pricing/PRD.md +9 -0
- package/examples/platform/docs/pricing/SDD.md +8 -0
- package/examples/platform/docs/products/PRD.md +9 -0
- package/examples/platform/docs/products/SDD.md +8 -0
- package/examples/platform/frontend/package.json +8 -0
- package/examples/platform/frontend/src/api-client.js +21 -0
- package/examples/platform/frontend/src/main.js +21 -0
- package/examples/platform/frontend/src/pages/products.js +12 -0
- package/examples/platform/frontend/src/pages/sign-in.js +14 -0
- package/examples/platform/frontend/src/session.js +11 -0
- package/examples/platform/pricing/package.json +9 -0
- package/examples/platform/pricing/src/cache.js +19 -0
- package/examples/platform/pricing/src/product-events.js +17 -0
- package/examples/platform/pricing/src/rules.js +19 -0
- package/examples/platform/pricing/src/server.js +32 -0
- package/examples/platform/products/package.json +9 -0
- package/examples/platform/products/src/catalog.js +20 -0
- package/examples/platform/products/src/events.js +11 -0
- package/examples/platform/products/src/search.js +9 -0
- package/examples/platform/products/src/server.js +31 -0
- package/migrations/postgres/001_init.sql +55 -0
- package/migrations/postgres/002_view_versions.sql +18 -0
- package/package.json +69 -4
- package/schema/approval.schema.json +173 -0
- package/schema/gap.schema.json +56 -0
- package/schema/pins.schema.json +122 -0
- package/schema/project.schema.json +93 -0
- package/schema/settings.schema.json +86 -0
- package/schema/toolbox.schema.json +206 -0
- package/schema/type.schema.json +121 -0
- package/schema/view.schema.json +240 -0
- package/skills/claude/build-gap/SKILL.md +28 -0
- package/skills/claude/check-ui/SKILL.md +40 -0
- package/skills/claude/draw-system/SKILL.md +38 -0
- package/skills/claude/gap-from-view/SKILL.md +29 -0
- package/skills/claude/post-audit/SKILL.md +58 -0
- package/skills/claude/pre-audit/SKILL.md +82 -0
- package/skills/claude/update-state/SKILL.md +56 -0
- package/skills/cursor/check-ui.mdc +40 -0
- package/skills/cursor/draw-system.mdc +38 -0
- package/skills/cursor/post-audit.mdc +44 -0
- package/skills/cursor/pre-audit.mdc +49 -0
- package/skills/cursor/update-state.mdc +45 -0
- package/skills/windsurf/spatial-views.md +212 -0
- package/toolboxes/aws.yaml +43 -0
- package/toolboxes/azure.yaml +41 -0
- package/toolboxes/cloud.yaml +95 -0
- package/toolboxes/docs.yaml +111 -0
- package/toolboxes/software.yaml +114 -0
- package/toolboxes/types/docs/gap.yaml +15 -0
- package/toolboxes/types/docs/mdd.yaml +19 -0
- package/toolboxes/types/docs/plain.yaml +7 -0
- package/toolboxes/types/docs/prd.yaml +17 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { experimental } from "./flags.js";
|
|
5
|
+
/**
|
|
6
|
+
* The Skills (draw-system, gap-from-view, build-gap, and the M1 audit Skills
|
|
7
|
+
* pre-audit, update-state, post-audit, and check-ui) are written once as
|
|
8
|
+
* Claude Code skills (skills/claude/<name>/SKILL.md). The Cursor rules and the
|
|
9
|
+
* Windsurf rules file are made from them, so the three never drift.
|
|
10
|
+
*/
|
|
11
|
+
export const SKILL_NAMES = ["draw-system", "gap-from-view", "build-gap", "pre-audit", "update-state", "post-audit", "check-ui"];
|
|
12
|
+
/** The Skills about Gaps: shipped only with SPATIAL_VIEWS_EXPERIMENTAL=1. */
|
|
13
|
+
export const EXPERIMENTAL_SKILLS = ["gap-from-view", "build-gap"];
|
|
14
|
+
/** The Skills served and installed: without the experimental flag, the Gap Skills are left out. */
|
|
15
|
+
export const skillNames = (exp = experimental()) => SKILL_NAMES.filter((n) => exp || !EXPERIMENTAL_SKILLS.includes(n));
|
|
16
|
+
const block = (tag) => new RegExp(`^[ \\t]*<!-- ${tag} -->[ \\t]*\\n([\\s\\S]*?)^[ \\t]*<!-- /${tag} -->[ \\t]*\\n`, "gm");
|
|
17
|
+
const BLOCK = block("experimental");
|
|
18
|
+
/** The text used instead when the flag is off. */
|
|
19
|
+
const DEFAULT = block("default");
|
|
20
|
+
/** Renumber each ordered list 1, 2, 3 ... (after a step was taken out). A heading or a plain line ends a list. */
|
|
21
|
+
function renumber(md) {
|
|
22
|
+
let n = 0;
|
|
23
|
+
return md
|
|
24
|
+
.split("\n")
|
|
25
|
+
.map((line) => {
|
|
26
|
+
const m = /^(\d+)\. /.exec(line);
|
|
27
|
+
if (m)
|
|
28
|
+
return `${++n}. ${line.slice(m[0].length)}`;
|
|
29
|
+
if (line.trim() && !/^\s/.test(line))
|
|
30
|
+
n = 0;
|
|
31
|
+
return line;
|
|
32
|
+
})
|
|
33
|
+
.join("\n");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A SKILL.md as shipped. Parts between `<!-- experimental -->` and
|
|
37
|
+
* `<!-- /experimental -->` (swim lanes, work splits, Gaps) are kept only with
|
|
38
|
+
* the experimental flag; without it they go, and the steps are renumbered.
|
|
39
|
+
* `description_experimental:` in the frontmatter replaces `description:` when the flag is on.
|
|
40
|
+
*/
|
|
41
|
+
export function skillText(md, exp = experimental()) {
|
|
42
|
+
// A Windows checkout may have CRLF line endings: the Skills ship with LF.
|
|
43
|
+
md = md.replace(/\r\n/g, "\n");
|
|
44
|
+
if (exp) {
|
|
45
|
+
const m = /^description_experimental:\s*(.+)$/m.exec(md);
|
|
46
|
+
let out = md.replace(BLOCK, "$1").replace(DEFAULT, "");
|
|
47
|
+
if (m)
|
|
48
|
+
out = out.replace(/^description:.*$/m, `description: ${m[1]}`);
|
|
49
|
+
return out.replace(/^description_experimental:.*\n/m, "");
|
|
50
|
+
}
|
|
51
|
+
return renumber(md.replace(BLOCK, "").replace(DEFAULT, "$1").replace(/^description_experimental:.*\n/m, ""));
|
|
52
|
+
}
|
|
53
|
+
export function parseSkill(md) {
|
|
54
|
+
const m = /^---\n([\s\S]*?)\n---\n+([\s\S]*)$/.exec(md.replace(/\r\n/g, "\n"));
|
|
55
|
+
if (!m)
|
|
56
|
+
throw new Error("a skill needs --- frontmatter ---");
|
|
57
|
+
const field = (k) => new RegExp(`^${k}:\\s*(.+)$`, "m").exec(m[1])?.[1]?.trim() ?? "";
|
|
58
|
+
return { name: field("name"), description: field("description"), body: m[2].trimEnd() + "\n" };
|
|
59
|
+
}
|
|
60
|
+
/** One SKILL.md as shipped (see skillText). */
|
|
61
|
+
export const readSkillFile = async (skillsDir, name, exp = experimental()) => skillText(await readFile(join(skillsDir, "claude", name, "SKILL.md"), "utf8"), exp);
|
|
62
|
+
export async function readSkills(skillsDir, exp = experimental()) {
|
|
63
|
+
const out = [];
|
|
64
|
+
for (const n of skillNames(exp))
|
|
65
|
+
out.push(parseSkill(await readSkillFile(skillsDir, n, exp)));
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
/** A Cursor rule (.cursor/rules/<name>.mdc): applied by the agent when the description fits. */
|
|
69
|
+
export const cursorRule = (s) => `---\ndescription: ${JSON.stringify(s.description)}\nalwaysApply: false\n---\n\n${s.body}`;
|
|
70
|
+
/** One Windsurf rules file (.windsurf/rules/spatial-views.md) holding all three skills. */
|
|
71
|
+
/** The first line of every Skill: stop when the MCP tools are missing. */
|
|
72
|
+
export const NO_TOOLS = "> If the spatial-views MCP tools are not available, stop and tell the user to enable the spatial-views MCP server " +
|
|
73
|
+
"(and to check the WSL form on Windows). Do not write your own MCP client or call the CLI directly.";
|
|
74
|
+
export function windsurfRules(skills) {
|
|
75
|
+
const gaps = skills.some((s) => s.name === "gap-from-view");
|
|
76
|
+
const desc = gaps
|
|
77
|
+
? "Spatial Views: draw the system as Views (draw-system), scope a change as a Gap (gap-from-view), " +
|
|
78
|
+
"build a Gap from get_context (build-gap), and audit a project of repos and docs (pre-audit, " +
|
|
79
|
+
"update-state, post-audit), and check a built page against its View or spec (check-ui). Use for architecture, " +
|
|
80
|
+
"Gaps, OpenSpec changes, get_context, audits and UI checks."
|
|
81
|
+
: "Spatial Views: draw the system as Views (draw-system) and audit a project of repos and docs (pre-audit, " +
|
|
82
|
+
"update-state, post-audit), and check a built page against its View or spec (check-ui). Use for architecture " +
|
|
83
|
+
"diagrams, the docs pinned to them, audits and UI checks.";
|
|
84
|
+
// Each skill's headings move down one level, under the file's own title.
|
|
85
|
+
const parts = skills.map((s) => s.body.replace(/^(#{1,5}) /gm, "$1# "));
|
|
86
|
+
return `---\ntrigger: model_decision\ndescription: ${JSON.stringify(desc)}\n---\n\n# Spatial Views skills\n\n${NO_TOOLS}\n\nUse the \`spatial-views\` MCP tools. Pick the skill that fits:\n\n${skills
|
|
87
|
+
.map((s) => `- **${s.name}**: ${s.description}`)
|
|
88
|
+
.join("\n")}\n\n${parts.join("\n")}`;
|
|
89
|
+
}
|
|
90
|
+
/** Every generated file, relative to the skills/ folder. */
|
|
91
|
+
export function generated(skills) {
|
|
92
|
+
const files = {};
|
|
93
|
+
for (const s of skills)
|
|
94
|
+
files[`cursor/${s.name}.mdc`] = cursorRule(s);
|
|
95
|
+
files["windsurf/spatial-views.md"] = windsurfRules(skills);
|
|
96
|
+
return files;
|
|
97
|
+
}
|
|
98
|
+
const exists = (p) => stat(p).then(() => true, () => false);
|
|
99
|
+
async function put(file, body, force, log) {
|
|
100
|
+
if (!force && (await exists(file))) {
|
|
101
|
+
log.push(`kept ${file} (exists; --force to replace)`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
await mkdir(dirname(file), { recursive: true });
|
|
105
|
+
await writeFile(file, body, "utf8");
|
|
106
|
+
log.push(`wrote ${file}`);
|
|
107
|
+
}
|
|
108
|
+
/** Add our server to an `mcpServers` JSON file, leaving every other entry as it is. */
|
|
109
|
+
async function mergeMcp(file, entry, log) {
|
|
110
|
+
let data = {};
|
|
111
|
+
if (await exists(file)) {
|
|
112
|
+
try {
|
|
113
|
+
data = JSON.parse(await readFile(file, "utf8"));
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
log.push(`skipped ${file} (not valid JSON; add the server by hand, see docs/install.md)`);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (data.mcpServers?.["spatial-views"]) {
|
|
121
|
+
log.push(`kept ${file} (spatial-views is already there)`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
data.mcpServers = { ...(data.mcpServers ?? {}), "spatial-views": entry };
|
|
125
|
+
await mkdir(dirname(file), { recursive: true });
|
|
126
|
+
await writeFile(file, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
127
|
+
log.push(`updated ${file}`);
|
|
128
|
+
}
|
|
129
|
+
/** True inside WSL: WSL_DISTRO_NAME is set, or /proc/version names Microsoft. */
|
|
130
|
+
export function detectWsl(env = process.env, procVersion) {
|
|
131
|
+
if (env.WSL_DISTRO_NAME)
|
|
132
|
+
return true;
|
|
133
|
+
let v = procVersion;
|
|
134
|
+
if (v === undefined) {
|
|
135
|
+
try {
|
|
136
|
+
v = readFileSync("/proc/version", "utf8");
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return /microsoft/i.test(v);
|
|
143
|
+
}
|
|
144
|
+
/** Quote one word for bash -lc: bare when it is safe, else in single quotes. */
|
|
145
|
+
export const shellQuote = (w) => (/^[A-Za-z0-9_@%+=:,./-]+$/.test(w) ? w : `'${w.replace(/'/g, `'\\''`)}'`);
|
|
146
|
+
/** `npx spatial-views init`: the Skills and the MCP config, for Cursor, Claude Code and Windsurf. */
|
|
147
|
+
export async function init(opts) {
|
|
148
|
+
const log = [];
|
|
149
|
+
const skills = await readSkills(opts.skillsDir);
|
|
150
|
+
const [command, ...pre] = opts.launch;
|
|
151
|
+
const force = opts.force === true;
|
|
152
|
+
if (opts.targets.claude) {
|
|
153
|
+
for (const n of skillNames())
|
|
154
|
+
await put(join(opts.root, ".claude", "skills", n, "SKILL.md"), await readSkillFile(opts.skillsDir, n), force, log);
|
|
155
|
+
await mergeMcp(join(opts.root, ".mcp.json"), { command, args: [...pre, "mcp"] }, log);
|
|
156
|
+
}
|
|
157
|
+
if (opts.targets.cursor) {
|
|
158
|
+
for (const s of skills)
|
|
159
|
+
await put(join(opts.root, ".cursor", "rules", `${s.name}.mdc`), cursorRule(s), force, log);
|
|
160
|
+
const entry = opts.wsl
|
|
161
|
+
? { command: "wsl", args: ["bash", "-lc", [command, ...pre, "mcp", "--dir", opts.root].map(shellQuote).join(" ")] }
|
|
162
|
+
: { command, args: [...pre, "mcp", "--dir", "${workspaceFolder}"] };
|
|
163
|
+
await mergeMcp(join(opts.root, ".cursor", "mcp.json"), entry, log);
|
|
164
|
+
if (opts.wsl)
|
|
165
|
+
log.push("note WSL found: .cursor/mcp.json runs the server through `wsl bash -lc`, because Cursor starts MCP servers on the Windows side.");
|
|
166
|
+
}
|
|
167
|
+
if (opts.targets.windsurf) {
|
|
168
|
+
await put(join(opts.root, ".windsurf", "rules", "spatial-views.md"), windsurfRules(skills), force, log);
|
|
169
|
+
log.push("Windsurf keeps MCP servers in ~/.codeium/windsurf/mcp_config.json (not in the repo). Add:", JSON.stringify({ mcpServers: { "spatial-views": { command, args: [...pre, "mcp", "--dir", opts.root] } } }, null, 2));
|
|
170
|
+
}
|
|
171
|
+
return log;
|
|
172
|
+
}
|
|
173
|
+
/** Rewrite skills/cursor and skills/windsurf from skills/claude (npm run skills), as shipped without the flag. */
|
|
174
|
+
export async function writeGenerated(skillsDir) {
|
|
175
|
+
const files = generated(await readSkills(skillsDir, false));
|
|
176
|
+
for (const [rel, body] of Object.entries(files)) {
|
|
177
|
+
await mkdir(dirname(join(skillsDir, rel)), { recursive: true });
|
|
178
|
+
await writeFile(join(skillsDir, rel), body, "utf8");
|
|
179
|
+
}
|
|
180
|
+
return Object.keys(files);
|
|
181
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { chmod } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { UserError } from "../errors.js";
|
|
5
|
+
import { FolderStore, readText, writeJson } from "./folder.js";
|
|
6
|
+
import { describeUrl, PostgresStore } from "./postgres.js";
|
|
7
|
+
/**
|
|
8
|
+
* Which backend holds the projects (Settings → Storage):
|
|
9
|
+
* <home>/config.json {"storage": {"kind": "folder" | "postgres", "url": "postgres://..."}}
|
|
10
|
+
* or, over it, the environment:
|
|
11
|
+
* SPATIAL_VIEWS_STORAGE=folder|postgres SPATIAL_VIEWS_DATABASE_URL=postgres://...
|
|
12
|
+
*
|
|
13
|
+
* config.json is written owner-only (it may hold a password). The URL is never
|
|
14
|
+
* logged, sent to the canvas, or put in a message: describeUrl gives host/db.
|
|
15
|
+
*/
|
|
16
|
+
export const StorageKindSchema = z.enum(["folder", "postgres"]);
|
|
17
|
+
const ConfigSchema = z.object({
|
|
18
|
+
storage: z.object({ kind: StorageKindSchema.default("folder"), url: z.string().optional() }).default({ kind: "folder" }),
|
|
19
|
+
});
|
|
20
|
+
export const configFile = (home) => join(home, "config.json");
|
|
21
|
+
async function readConfig(home) {
|
|
22
|
+
const raw = await readText(configFile(home));
|
|
23
|
+
if (!raw)
|
|
24
|
+
return ConfigSchema.parse({});
|
|
25
|
+
try {
|
|
26
|
+
return ConfigSchema.parse(JSON.parse(raw));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return ConfigSchema.parse({});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export async function readStorageConfig(home, env = process.env) {
|
|
33
|
+
const file = (await readConfig(home)).storage;
|
|
34
|
+
const envKind = env.SPATIAL_VIEWS_STORAGE?.trim().toLowerCase();
|
|
35
|
+
const envUrl = env.SPATIAL_VIEWS_DATABASE_URL?.trim();
|
|
36
|
+
if (envKind) {
|
|
37
|
+
const kind = StorageKindSchema.safeParse(envKind);
|
|
38
|
+
if (!kind.success)
|
|
39
|
+
throw new UserError(`SPATIAL_VIEWS_STORAGE is folder or postgres`);
|
|
40
|
+
const url = envUrl || (kind.data === "postgres" ? file.url : undefined);
|
|
41
|
+
return { kind: kind.data, ...(url ? { url } : {}), from: "env" };
|
|
42
|
+
}
|
|
43
|
+
if (envUrl && file.kind === "postgres")
|
|
44
|
+
return { kind: "postgres", url: envUrl, from: "env" };
|
|
45
|
+
const from = (await readText(configFile(home))) ? "config" : "default";
|
|
46
|
+
return { kind: file.kind, ...(file.url ? { url: file.url } : {}), from };
|
|
47
|
+
}
|
|
48
|
+
export function storageInfo(c) {
|
|
49
|
+
return { kind: c.kind, from: c.from, url_set: !!c.url, ...(c.url ? { where: describeUrl(c.url) } : {}) };
|
|
50
|
+
}
|
|
51
|
+
/** Save the choice (takes effect when Spatial Views next starts). An omitted URL keeps the saved one. */
|
|
52
|
+
export async function writeStorageConfig(home, input) {
|
|
53
|
+
const cur = await readConfig(home);
|
|
54
|
+
const url = input.url?.trim() || (input.kind === "postgres" ? cur.storage.url : undefined);
|
|
55
|
+
if (input.kind === "postgres") {
|
|
56
|
+
if (!url)
|
|
57
|
+
throw new UserError("Postgres needs a database URL (postgres://host:5432/db, with its user and password)");
|
|
58
|
+
if (!/^postgres(ql)?:\/\//i.test(url))
|
|
59
|
+
throw new UserError("the database URL starts postgres://");
|
|
60
|
+
}
|
|
61
|
+
const next = { ...cur, storage: { kind: input.kind, ...(url ? { url } : {}) } };
|
|
62
|
+
await writeJson(configFile(home), next);
|
|
63
|
+
await chmod(configFile(home), 0o600).catch(() => undefined);
|
|
64
|
+
return { kind: input.kind, ...(url ? { url } : {}), from: "config" };
|
|
65
|
+
}
|
|
66
|
+
/** Open the configured backend. */
|
|
67
|
+
export async function openStorage(home, env = process.env) {
|
|
68
|
+
const c = await readStorageConfig(home, env);
|
|
69
|
+
if (c.kind === "folder")
|
|
70
|
+
return new FolderStore({ home });
|
|
71
|
+
if (!c.url)
|
|
72
|
+
throw new UserError("storage is postgres but no database URL is set (SPATIAL_VIEWS_DATABASE_URL or Settings → Storage)");
|
|
73
|
+
return PostgresStore.connect({ url: c.url });
|
|
74
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { projectToolboxDir, userToolboxDir } from "../types.js";
|
|
2
|
+
import { ViewWatcher, only, yamlId } from "./folder-watch.js";
|
|
3
|
+
/** The kinds the canvas hears about (by the same names as before storage backends). */
|
|
4
|
+
const EVENT = {
|
|
5
|
+
view: "view",
|
|
6
|
+
gap: "gap",
|
|
7
|
+
pins: "pins",
|
|
8
|
+
approval: "approval",
|
|
9
|
+
settings: "settings",
|
|
10
|
+
doc: "doc",
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* One project's live changes for the canvas: the store's change stream (fs
|
|
14
|
+
* watch for folders, LISTEN/NOTIFY for Postgres) plus this machine's open_view
|
|
15
|
+
* hand-off file.
|
|
16
|
+
*/
|
|
17
|
+
export class ProjectEvents {
|
|
18
|
+
listeners = new Set();
|
|
19
|
+
store;
|
|
20
|
+
local;
|
|
21
|
+
/** `dataDir`: the folder whose toolboxes/*.yaml are watched too (toolboxes are files); `userDir`: the user's toolboxes. */
|
|
22
|
+
constructor(storage, project, localDir, pollMs, dataDir, userDir) {
|
|
23
|
+
const emit = (e) => this.listeners.forEach((fn) => fn(e));
|
|
24
|
+
this.store = storage.watch(project, (c) => {
|
|
25
|
+
const type = EVENT[c.kind];
|
|
26
|
+
if (type)
|
|
27
|
+
emit({ type, id: c.id, removed: c.removed });
|
|
28
|
+
});
|
|
29
|
+
this.local = new ViewWatcher([
|
|
30
|
+
{ type: "open", dir: localDir, match: only("open.json", "open") },
|
|
31
|
+
...(dataDir
|
|
32
|
+
? [
|
|
33
|
+
// The project's toolboxes (YAML files, whatever the store).
|
|
34
|
+
{ type: "types", dir: projectToolboxDir(dataDir), match: yamlId },
|
|
35
|
+
]
|
|
36
|
+
: []),
|
|
37
|
+
// The user's toolboxes (~/SpatialViews/toolboxes).
|
|
38
|
+
{ type: "types", dir: userDir ?? userToolboxDir(), match: yamlId },
|
|
39
|
+
], pollMs);
|
|
40
|
+
this.local.subscribe(emit);
|
|
41
|
+
}
|
|
42
|
+
async whenReady() {
|
|
43
|
+
await Promise.all([this.store.ready, this.local.whenReady()]);
|
|
44
|
+
}
|
|
45
|
+
subscribe(fn) {
|
|
46
|
+
this.listeners.add(fn);
|
|
47
|
+
return () => this.listeners.delete(fn);
|
|
48
|
+
}
|
|
49
|
+
close() {
|
|
50
|
+
this.store.close();
|
|
51
|
+
this.local.close();
|
|
52
|
+
this.listeners.clear();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { watch } from "node:fs";
|
|
2
|
+
import { readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { projectToolboxDir, userToolboxDir } from "../types.js";
|
|
5
|
+
export const jsonId = (f) => (f.endsWith(".json") ? f.slice(0, -5) : null);
|
|
6
|
+
export const yamlId = (f) => (/\.ya?ml$/.test(f) ? f.replace(/\.ya?ml$/, "") : null);
|
|
7
|
+
export const only = (name, id) => (f) => (f === name ? id : null);
|
|
8
|
+
/**
|
|
9
|
+
* Watches a project's data folder and reports what changed, so the canvas can
|
|
10
|
+
* show what the AI does through MCP (a separate process) live. This is the
|
|
11
|
+
* folder store's change stream (FolderStore.watch).
|
|
12
|
+
*
|
|
13
|
+
* fs.watch triggers a rescan straight away; a slow poll is the fallback where
|
|
14
|
+
* fs.watch is unreliable (WSL /mnt drives, network file systems) or a folder
|
|
15
|
+
* does not exist yet. A file counts as changed when its mtime, size or inode
|
|
16
|
+
* changes (the stores save by rename, so every save is a new inode).
|
|
17
|
+
*/
|
|
18
|
+
export class ViewWatcher {
|
|
19
|
+
channels;
|
|
20
|
+
pollMs;
|
|
21
|
+
seen = new Map();
|
|
22
|
+
listeners = new Set();
|
|
23
|
+
timer = null;
|
|
24
|
+
debounce = null;
|
|
25
|
+
scanning = null;
|
|
26
|
+
again = false;
|
|
27
|
+
ready;
|
|
28
|
+
constructor(channels, pollMs = 1000) {
|
|
29
|
+
this.channels = channels;
|
|
30
|
+
this.pollMs = pollMs;
|
|
31
|
+
this.ready = this.scan(false);
|
|
32
|
+
this.timer = setInterval(() => void this.rescan(), this.pollMs);
|
|
33
|
+
this.timer.unref?.();
|
|
34
|
+
}
|
|
35
|
+
static forRepo(repoRoot, pollMs) {
|
|
36
|
+
return ViewWatcher.forData(join(repoRoot, ".spatial"), join(repoRoot, ".spatial-local"), pollMs);
|
|
37
|
+
}
|
|
38
|
+
/** Watch one project's data folder (views/, gaps/, pins.json) and its local hand-off folder. */
|
|
39
|
+
static forData(dataDir, localDir, pollMs, userDir) {
|
|
40
|
+
return new ViewWatcher([
|
|
41
|
+
{ type: "view", dir: join(dataDir, "views"), match: jsonId },
|
|
42
|
+
{ type: "gap", dir: join(dataDir, "gaps"), match: jsonId },
|
|
43
|
+
{ type: "pins", dir: dataDir, match: only("pins.json", "pins") },
|
|
44
|
+
{ type: "open", dir: localDir, match: only("open.json", "open") },
|
|
45
|
+
{ type: "approval", dir: join(dataDir, "approvals"), match: jsonId },
|
|
46
|
+
{ type: "settings", dir: dataDir, match: only("settings.json", "settings") },
|
|
47
|
+
// The project's toolboxes and the user's (~/SpatialViews/toolboxes).
|
|
48
|
+
{ type: "types", dir: projectToolboxDir(dataDir), match: yamlId },
|
|
49
|
+
{ type: "types", dir: userDir ?? userToolboxDir(), match: yamlId },
|
|
50
|
+
], pollMs);
|
|
51
|
+
}
|
|
52
|
+
/** Resolves once the first scan (the baseline, which emits nothing) is done. */
|
|
53
|
+
whenReady() {
|
|
54
|
+
return this.ready;
|
|
55
|
+
}
|
|
56
|
+
subscribe(fn) {
|
|
57
|
+
this.listeners.add(fn);
|
|
58
|
+
return () => this.listeners.delete(fn);
|
|
59
|
+
}
|
|
60
|
+
close() {
|
|
61
|
+
if (this.timer)
|
|
62
|
+
clearInterval(this.timer);
|
|
63
|
+
if (this.debounce)
|
|
64
|
+
clearTimeout(this.debounce);
|
|
65
|
+
for (const c of this.channels)
|
|
66
|
+
(c.fsw?.close(), (c.fsw = null));
|
|
67
|
+
this.timer = this.debounce = null;
|
|
68
|
+
this.listeners.clear();
|
|
69
|
+
}
|
|
70
|
+
/** Scan now (serialised: a call during a scan queues one more scan). */
|
|
71
|
+
rescan() {
|
|
72
|
+
if (this.scanning) {
|
|
73
|
+
this.again = true;
|
|
74
|
+
return this.scanning;
|
|
75
|
+
}
|
|
76
|
+
this.scanning = this.scan(true).finally(() => {
|
|
77
|
+
this.scanning = null;
|
|
78
|
+
if (this.again) {
|
|
79
|
+
this.again = false;
|
|
80
|
+
void this.rescan();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return this.scanning;
|
|
84
|
+
}
|
|
85
|
+
ensureFsWatch(c) {
|
|
86
|
+
if (c.fsw || !this.timer)
|
|
87
|
+
return;
|
|
88
|
+
try {
|
|
89
|
+
c.fsw = watch(c.dir, () => {
|
|
90
|
+
if (this.debounce)
|
|
91
|
+
clearTimeout(this.debounce);
|
|
92
|
+
this.debounce = setTimeout(() => void this.rescan(), 30);
|
|
93
|
+
});
|
|
94
|
+
c.fsw.on("error", () => {
|
|
95
|
+
c.fsw?.close();
|
|
96
|
+
c.fsw = null; // the poll carries on, and the next scan tries again
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
c.fsw = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async scan(emit) {
|
|
104
|
+
const now = new Map();
|
|
105
|
+
const meta = new Map();
|
|
106
|
+
for (const c of this.channels) {
|
|
107
|
+
let files = [];
|
|
108
|
+
try {
|
|
109
|
+
files = await readdir(c.dir);
|
|
110
|
+
this.ensureFsWatch(c);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// The folder isn't there (yet): everything we knew in it is gone.
|
|
114
|
+
}
|
|
115
|
+
for (const f of files) {
|
|
116
|
+
const id = c.match(f);
|
|
117
|
+
if (id === null)
|
|
118
|
+
continue;
|
|
119
|
+
try {
|
|
120
|
+
const s = await stat(join(c.dir, f));
|
|
121
|
+
const key = `${c.type}:${id}`;
|
|
122
|
+
now.set(key, `${s.mtimeMs}:${s.size}:${s.ino}`);
|
|
123
|
+
meta.set(key, { type: c.type, id });
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
// Removed between readdir and stat; the next scan settles it.
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const events = [];
|
|
131
|
+
for (const [key, sig] of now)
|
|
132
|
+
if (this.seen.get(key) !== sig)
|
|
133
|
+
events.push({ ...meta.get(key), removed: false });
|
|
134
|
+
for (const key of this.seen.keys()) {
|
|
135
|
+
if (now.has(key))
|
|
136
|
+
continue;
|
|
137
|
+
const at = key.indexOf(":");
|
|
138
|
+
events.push({ type: key.slice(0, at), id: key.slice(at + 1), removed: true });
|
|
139
|
+
}
|
|
140
|
+
this.seen = now;
|
|
141
|
+
if (!emit)
|
|
142
|
+
return;
|
|
143
|
+
for (const e of events)
|
|
144
|
+
for (const fn of this.listeners)
|
|
145
|
+
fn(e);
|
|
146
|
+
}
|
|
147
|
+
}
|