spatial-views 0.0.0-stage → 0.1.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 +63 -2
- package/dist/app/assets/abnfDiagram-S7VZAUFO-BVgtb0Hw.js +1 -0
- package/dist/app/assets/architecture-WOLXFQ4H-DxylGHqT.js +1 -0
- package/dist/app/assets/architectureDiagram-YGSQEFNW-BrBCHqUH.js +36 -0
- package/dist/app/assets/blockDiagram-APKKGZFW-C7fb3MCE.js +140 -0
- package/dist/app/assets/bpmnIcons-APHXDYXF-C0jbCElo.js +1 -0
- package/dist/app/assets/c4Diagram-KJ2TOQU5-Bkb_0fbd.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-DU4vZ03M.js +2 -0
- package/dist/app/assets/chunk-AXFCVDMN-De96BVTB.js +62 -0
- package/dist/app/assets/chunk-BG4553SI-J33jsRLV.js +127 -0
- package/dist/app/assets/chunk-DQASFODM-Bh85SSGO.js +10 -0
- package/dist/app/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/dist/app/assets/chunk-HFQBQIA3-CCklgplR.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-B04qR8ED.js +2 -0
- package/dist/app/assets/chunk-KS47KZWR-BsqDyyAt.js +1 -0
- package/dist/app/assets/chunk-LEVMRMFU-DwxrBz2u.js +1 -0
- package/dist/app/assets/chunk-M57EVK23-D9YIHjps.js +1 -0
- package/dist/app/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
- package/dist/app/assets/chunk-OY52VR5R-WB63DeTA.js +88 -0
- package/dist/app/assets/chunk-QB3YPZ4I-cHzzKdtb.js +213 -0
- package/dist/app/assets/chunk-R3KSYZAL-C7rDagaa.js +1 -0
- package/dist/app/assets/chunk-STOXU5M4-CG6pnJtH.js +72 -0
- package/dist/app/assets/chunk-TU62E6Q5-BbcGYfy_.js +1 -0
- package/dist/app/assets/chunk-X3CZISLH-FMl4PpWt.js +1 -0
- package/dist/app/assets/chunk-X4PVCEUW-BUI1Oi3q.js +1 -0
- package/dist/app/assets/chunk-XXDRQBXY-DYhK5YW-.js +1 -0
- package/dist/app/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/dist/app/assets/classDiagram-v2-L25IHNXF-K-k5GTUn.js +217 -0
- package/dist/app/assets/cose-bilkent-JH36ORCC-BJmLLpaN.js +1 -0
- package/dist/app/assets/cynefin-EF2NZ3EQ-DrzwMKdm.js +1 -0
- package/dist/app/assets/cynefinDiagram-L36ESFBY-mkV33_Xx.js +62 -0
- package/dist/app/assets/cytoscape.esm-nTrd5EVq.js +321 -0
- package/dist/app/assets/d3-scale-ygdFCUH8.js +1 -0
- package/dist/app/assets/dagre-SPEB5RXH-BNIymLsu.js +4 -0
- package/dist/app/assets/diagram-3B7GL73J-CGOiolFW.js +200 -0
- package/dist/app/assets/diagram-DNEMPZML-DZFkHNly.js +24 -0
- package/dist/app/assets/diagram-L3MUGCDN-B42GAYLq.js +41 -0
- package/dist/app/assets/diagram-LA4NIPWU-BYuLRYId.js +24 -0
- package/dist/app/assets/diagram-MHCOC4E2-DGzI6TJG.js +3 -0
- package/dist/app/assets/diagram-XPFHEONP-CTBEgDQ8.js +30 -0
- package/dist/app/assets/dist-1Q8ng3tu.js +1 -0
- package/dist/app/assets/ebnfDiagram-UZSYDL2S-GuMdOCvL.js +1 -0
- package/dist/app/assets/elk-XV6XMXQE-BpY3loT5.js +2 -0
- package/dist/app/assets/erDiagram-EYBAPDEH-nDZ2PMGl.js +99 -0
- package/dist/app/assets/eventmodeling-K75KTNOO-BN42Mgwh.js +1 -0
- package/dist/app/assets/flowDiagram-MALGAFMO-BCwwiLzb.js +1 -0
- package/dist/app/assets/ganttDiagram-23REXITY-xMRHaU3y.js +292 -0
- package/dist/app/assets/gitGraph-VSP46ZUC-DyGq5mdd.js +1 -0
- package/dist/app/assets/gitGraphDiagram-STJD7YCW-CDkKLrvO.js +106 -0
- package/dist/app/assets/graphlib-DS17s2tU.js +1 -0
- package/dist/app/assets/index-BY66fc8Q.js +95 -0
- package/dist/app/assets/index-ByetEVbs.css +1 -0
- package/dist/app/assets/info-OHQRW6UA-DOBf-fQH.js +1 -0
- package/dist/app/assets/infoDiagram-KBXO4MBG-B92P8pjY.js +2 -0
- package/dist/app/assets/ishikawaDiagram-M35NCXHK-BRZsbCvK.js +70 -0
- package/dist/app/assets/journeyDiagram-OPLOXDXO-Bppwb1oF.js +139 -0
- package/dist/app/assets/kanban-definition-BZKZHSQY-ByD_x5cm.js +89 -0
- package/dist/app/assets/katex-MEuSR36T.js +257 -0
- package/dist/app/assets/mermaid-parser.core-CKmnV0Md.js +7 -0
- package/dist/app/assets/mermaid.core-BoRvk1Q-.js +44 -0
- package/dist/app/assets/mindmap-definition-I2QAS2VK-fRbUh23C.js +98 -0
- package/dist/app/assets/packet-JDAUHWVQ-CgHq03Zj.js +1 -0
- package/dist/app/assets/pegDiagram-FD4XPPHP-qIABf6H8.js +1 -0
- package/dist/app/assets/pie-XZMESJXO-DtNh3cDE.js +1 -0
- package/dist/app/assets/pieDiagram-J4AKIIQI-Bb4RBdrc.js +39 -0
- package/dist/app/assets/quadrantDiagram-XTYBLID2-B0CpcVWE.js +7 -0
- package/dist/app/assets/radar-ABXABTNO-BS0FcsIX.js +1 -0
- package/dist/app/assets/railroad-I3PHUGI6-lO8vfGdI.js +1 -0
- package/dist/app/assets/railroad-abnf-LNEFI6M7-BUhw7Iuo.js +1 -0
- package/dist/app/assets/railroad-ebnf-DWYD2UWJ-nQNF56LJ.js +1 -0
- package/dist/app/assets/railroad-peg-JR7BVNR7-CwvbVIo6.js +1 -0
- package/dist/app/assets/railroadDiagram-MRQ6NCDL-Dwr-mPZL.js +1 -0
- package/dist/app/assets/requirementDiagram-Y7LWUY3S-Ctv2DKJM.js +84 -0
- package/dist/app/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/dist/app/assets/sankeyDiagram-WGJFYUXA-BvKX0eIe.js +40 -0
- package/dist/app/assets/sequenceDiagram-5NDRBG5M-Y4YqU2Xt.js +169 -0
- package/dist/app/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/dist/app/assets/stateDiagram-v2-FJ4FJNEA-DmjIjn4u.js +291 -0
- package/dist/app/assets/swimlanes-UMQ2AFYF-B2O5JCjs.js +1 -0
- package/dist/app/assets/swimlanesDiagram-OMPLPG4O-DhCl-Cjg.js +8 -0
- package/dist/app/assets/timeline-definition-SEUHKTQE-DJJdixAI.js +120 -0
- package/dist/app/assets/treeView-D4JQ5SDB-Dd1no4eE.js +1 -0
- package/dist/app/assets/treemap-SAJKECNS-t6wtsaki.js +1 -0
- package/dist/app/assets/usecaseDiagram-AFA6YXN4-Bp8pXgvR.js +328 -0
- package/dist/app/assets/vennDiagram-NNMJR3RB-DYSfZMWT.js +34 -0
- package/dist/app/assets/wardley-7MLQ67FV-Bx60QTuu.js +1 -0
- package/dist/app/assets/wardleyDiagram-S7YFUSEH-CGJ8qg76.js +78 -0
- package/dist/app/assets/xychartDiagram-GGNKY7IX-CRSvraFc.js +7 -0
- package/dist/app/index.html +13 -0
- package/dist/cli/index.js +110 -0
- package/dist/core/api.js +209 -0
- package/dist/core/context.js +194 -0
- package/dist/core/docs.js +134 -0
- package/dist/core/edit.js +105 -0
- package/dist/core/errors.js +3 -0
- package/dist/core/gaps.js +114 -0
- package/dist/core/history.js +19 -0
- package/dist/core/links.js +68 -0
- package/dist/core/mermaid.js +175 -0
- package/dist/core/openspec.js +120 -0
- package/dist/core/paths.js +23 -0
- package/dist/core/resolve.js +35 -0
- package/dist/core/schema.js +101 -0
- package/dist/core/skills.js +103 -0
- package/dist/core/storage.js +183 -0
- package/dist/core/version.js +1 -0
- package/dist/core/watch.js +136 -0
- package/dist/core/xml.js +50 -0
- package/dist/mcp/server.js +357 -0
- package/package.json +53 -4
- package/schema/gap.schema.json +53 -0
- package/schema/pins.schema.json +122 -0
- package/schema/view.schema.json +134 -0
- package/skills/claude/build-gap/SKILL.md +21 -0
- package/skills/claude/draw-system/SKILL.md +23 -0
- package/skills/claude/gap-from-view/SKILL.md +22 -0
- package/skills/cursor/build-gap.mdc +21 -0
- package/skills/cursor/draw-system.mdc +23 -0
- package/skills/cursor/gap-from-view.mdc +22 -0
- package/skills/windsurf/spatial-views.md +66 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* The three Skills (draw-system, gap-from-view, build-gap) are written once as
|
|
5
|
+
* Claude Code skills (skills/claude/<name>/SKILL.md). The Cursor rules and the
|
|
6
|
+
* Windsurf rules file are made from them, so the three never drift.
|
|
7
|
+
*/
|
|
8
|
+
export const SKILL_NAMES = ["draw-system", "gap-from-view", "build-gap"];
|
|
9
|
+
export function parseSkill(md) {
|
|
10
|
+
const m = /^---\n([\s\S]*?)\n---\n+([\s\S]*)$/.exec(md);
|
|
11
|
+
if (!m)
|
|
12
|
+
throw new Error("a skill needs --- frontmatter ---");
|
|
13
|
+
const field = (k) => new RegExp(`^${k}:\\s*(.+)$`, "m").exec(m[1])?.[1]?.trim() ?? "";
|
|
14
|
+
return { name: field("name"), description: field("description"), body: m[2].trimEnd() + "\n" };
|
|
15
|
+
}
|
|
16
|
+
export async function readSkills(skillsDir) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const n of SKILL_NAMES)
|
|
19
|
+
out.push(parseSkill(await readFile(join(skillsDir, "claude", n, "SKILL.md"), "utf8")));
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
/** A Cursor rule (.cursor/rules/<name>.mdc): applied by the agent when the description fits. */
|
|
23
|
+
export const cursorRule = (s) => `---\ndescription: ${JSON.stringify(s.description)}\nalwaysApply: false\n---\n\n${s.body}`;
|
|
24
|
+
/** One Windsurf rules file (.windsurf/rules/spatial-views.md) holding all three skills. */
|
|
25
|
+
export function windsurfRules(skills) {
|
|
26
|
+
const desc = "Spatial Views: draw the system as Views (draw-system), scope a change as a Gap (gap-from-view), " +
|
|
27
|
+
"and build a Gap from get_context (build-gap). Use for architecture, Gaps, OpenSpec changes and get_context.";
|
|
28
|
+
// Each skill's headings move down one level, under the file's own title.
|
|
29
|
+
const parts = skills.map((s) => s.body.replace(/^(#{1,5}) /gm, "$1# "));
|
|
30
|
+
return `---\ntrigger: model_decision\ndescription: ${JSON.stringify(desc)}\n---\n\n# Spatial Views skills\n\nUse the \`spatial-views\` MCP tools. Pick the skill that fits:\n\n${skills
|
|
31
|
+
.map((s) => `- **${s.name}**: ${s.description}`)
|
|
32
|
+
.join("\n")}\n\n${parts.join("\n")}`;
|
|
33
|
+
}
|
|
34
|
+
/** Every generated file, relative to the skills/ folder. */
|
|
35
|
+
export function generated(skills) {
|
|
36
|
+
const files = {};
|
|
37
|
+
for (const s of skills)
|
|
38
|
+
files[`cursor/${s.name}.mdc`] = cursorRule(s);
|
|
39
|
+
files["windsurf/spatial-views.md"] = windsurfRules(skills);
|
|
40
|
+
return files;
|
|
41
|
+
}
|
|
42
|
+
const exists = (p) => stat(p).then(() => true, () => false);
|
|
43
|
+
async function put(file, body, force, log) {
|
|
44
|
+
if (!force && (await exists(file))) {
|
|
45
|
+
log.push(`kept ${file} (exists; --force to replace)`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
await mkdir(dirname(file), { recursive: true });
|
|
49
|
+
await writeFile(file, body, "utf8");
|
|
50
|
+
log.push(`wrote ${file}`);
|
|
51
|
+
}
|
|
52
|
+
/** Add our server to an `mcpServers` JSON file, leaving every other entry as it is. */
|
|
53
|
+
async function mergeMcp(file, entry, log) {
|
|
54
|
+
let data = {};
|
|
55
|
+
if (await exists(file)) {
|
|
56
|
+
try {
|
|
57
|
+
data = JSON.parse(await readFile(file, "utf8"));
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
log.push(`skipped ${file} (not valid JSON; add the server by hand, see docs/install.md)`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (data.mcpServers?.["spatial-views"]) {
|
|
65
|
+
log.push(`kept ${file} (spatial-views is already there)`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
data.mcpServers = { ...(data.mcpServers ?? {}), "spatial-views": entry };
|
|
69
|
+
await mkdir(dirname(file), { recursive: true });
|
|
70
|
+
await writeFile(file, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
71
|
+
log.push(`updated ${file}`);
|
|
72
|
+
}
|
|
73
|
+
/** `npx spatial-views init`: the Skills and the MCP config, for Cursor, Claude Code and Windsurf. */
|
|
74
|
+
export async function init(opts) {
|
|
75
|
+
const log = [];
|
|
76
|
+
const skills = await readSkills(opts.skillsDir);
|
|
77
|
+
const [command, ...pre] = opts.launch;
|
|
78
|
+
const force = opts.force === true;
|
|
79
|
+
if (opts.targets.claude) {
|
|
80
|
+
for (const n of SKILL_NAMES)
|
|
81
|
+
await put(join(opts.root, ".claude", "skills", n, "SKILL.md"), await readFile(join(opts.skillsDir, "claude", n, "SKILL.md"), "utf8"), force, log);
|
|
82
|
+
await mergeMcp(join(opts.root, ".mcp.json"), { command, args: [...pre, "mcp"] }, log);
|
|
83
|
+
}
|
|
84
|
+
if (opts.targets.cursor) {
|
|
85
|
+
for (const s of skills)
|
|
86
|
+
await put(join(opts.root, ".cursor", "rules", `${s.name}.mdc`), cursorRule(s), force, log);
|
|
87
|
+
await mergeMcp(join(opts.root, ".cursor", "mcp.json"), { command, args: [...pre, "mcp", "--dir", "${workspaceFolder}"] }, log);
|
|
88
|
+
}
|
|
89
|
+
if (opts.targets.windsurf) {
|
|
90
|
+
await put(join(opts.root, ".windsurf", "rules", "spatial-views.md"), windsurfRules(skills), force, log);
|
|
91
|
+
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));
|
|
92
|
+
}
|
|
93
|
+
return log;
|
|
94
|
+
}
|
|
95
|
+
/** Rewrite skills/cursor and skills/windsurf from skills/claude (npm run skills). */
|
|
96
|
+
export async function writeGenerated(skillsDir) {
|
|
97
|
+
const files = generated(await readSkills(skillsDir));
|
|
98
|
+
for (const [rel, body] of Object.entries(files)) {
|
|
99
|
+
await mkdir(dirname(join(skillsDir, rel)), { recursive: true });
|
|
100
|
+
await writeFile(join(skillsDir, rel), body, "utf8");
|
|
101
|
+
}
|
|
102
|
+
return Object.keys(files);
|
|
103
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { UserError } from "./errors.js";
|
|
4
|
+
import { GapSchema, PinsFileSchema, ViewSchema, summarize, } from "./schema.js";
|
|
5
|
+
export class NotFoundError extends UserError {
|
|
6
|
+
}
|
|
7
|
+
const ID = /^[A-Za-z0-9_-]+$/;
|
|
8
|
+
/** Write through a temp file then rename, 2-space JSON so it diffs cleanly. */
|
|
9
|
+
async function writeJson(file, data) {
|
|
10
|
+
await mkdir(dirname(file), { recursive: true });
|
|
11
|
+
const tmp = `${file}.${process.pid}.${Math.random().toString(36).slice(2, 8)}.tmp`;
|
|
12
|
+
await writeFile(tmp, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
13
|
+
await rename(tmp, file);
|
|
14
|
+
}
|
|
15
|
+
/** One JSON file per item in a folder, validated on read and on write. */
|
|
16
|
+
class JsonDir {
|
|
17
|
+
dir;
|
|
18
|
+
schema;
|
|
19
|
+
noun;
|
|
20
|
+
constructor(dir, schema, noun) {
|
|
21
|
+
this.dir = dir;
|
|
22
|
+
this.schema = schema;
|
|
23
|
+
this.noun = noun;
|
|
24
|
+
}
|
|
25
|
+
path(id) {
|
|
26
|
+
if (!ID.test(id))
|
|
27
|
+
throw new NotFoundError(`no ${this.noun} ${id}`);
|
|
28
|
+
return join(this.dir, `${id}.json`);
|
|
29
|
+
}
|
|
30
|
+
async all() {
|
|
31
|
+
let files;
|
|
32
|
+
try {
|
|
33
|
+
files = await readdir(this.dir);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const items = [];
|
|
39
|
+
for (const f of files.filter((f) => f.endsWith(".json")).sort()) {
|
|
40
|
+
try {
|
|
41
|
+
items.push(await this.get(f.slice(0, -5)));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// A broken file is left alone; it must not hide the others.
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return items;
|
|
48
|
+
}
|
|
49
|
+
async get(id) {
|
|
50
|
+
let raw;
|
|
51
|
+
try {
|
|
52
|
+
raw = await readFile(this.path(id), "utf8");
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
throw new NotFoundError(`no ${this.noun} ${id}`);
|
|
56
|
+
}
|
|
57
|
+
return this.schema.parse(JSON.parse(raw));
|
|
58
|
+
}
|
|
59
|
+
async has(id) {
|
|
60
|
+
try {
|
|
61
|
+
await this.get(id);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async save(item) {
|
|
69
|
+
const clean = this.schema.parse(item);
|
|
70
|
+
await writeJson(this.path(clean.id), clean);
|
|
71
|
+
return clean;
|
|
72
|
+
}
|
|
73
|
+
async remove(id) {
|
|
74
|
+
const file = this.path(id);
|
|
75
|
+
if (!(await this.has(id)))
|
|
76
|
+
throw new NotFoundError(`no ${this.noun} ${id}`);
|
|
77
|
+
await rm(file);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Views live one per file in `<repo>/.spatial/views/<id>.json`. */
|
|
81
|
+
export class ViewStore extends JsonDir {
|
|
82
|
+
constructor(repoRoot) {
|
|
83
|
+
super(join(repoRoot, ".spatial", "views"), ViewSchema, "View");
|
|
84
|
+
}
|
|
85
|
+
listViews() {
|
|
86
|
+
return this.all();
|
|
87
|
+
}
|
|
88
|
+
async list() {
|
|
89
|
+
return (await this.all()).map(summarize);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Gaps live one per file in `<repo>/.spatial/gaps/<id>.json`. */
|
|
93
|
+
export class GapStore extends JsonDir {
|
|
94
|
+
constructor(repoRoot) {
|
|
95
|
+
super(join(repoRoot, ".spatial", "gaps"), GapSchema, "Gap");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export function samePin(a, b) {
|
|
99
|
+
return JSON.stringify(normalPin(a)) === JSON.stringify(normalPin(b));
|
|
100
|
+
}
|
|
101
|
+
function normalPin(p) {
|
|
102
|
+
switch (p.kind) {
|
|
103
|
+
case "view":
|
|
104
|
+
return { path: p.path, kind: p.kind, view_id: p.view_id };
|
|
105
|
+
case "module":
|
|
106
|
+
return { path: p.path, kind: p.kind, view_id: p.view_id, module_id: p.module_id };
|
|
107
|
+
case "feature":
|
|
108
|
+
return { path: p.path, kind: p.kind, view_id: p.view_id, feature_id: p.feature_id };
|
|
109
|
+
case "gap":
|
|
110
|
+
return { path: p.path, kind: p.kind, gap_id: p.gap_id };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** The pin map, `<repo>/.spatial/pins.json`. */
|
|
114
|
+
export class PinStore {
|
|
115
|
+
file;
|
|
116
|
+
constructor(repoRoot) {
|
|
117
|
+
this.file = join(repoRoot, ".spatial", "pins.json");
|
|
118
|
+
}
|
|
119
|
+
async read() {
|
|
120
|
+
let raw;
|
|
121
|
+
try {
|
|
122
|
+
raw = await readFile(this.file, "utf8");
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return { sources: [], pins: [] };
|
|
126
|
+
}
|
|
127
|
+
return PinsFileSchema.parse(JSON.parse(raw));
|
|
128
|
+
}
|
|
129
|
+
async write(data) {
|
|
130
|
+
const clean = PinsFileSchema.parse(data);
|
|
131
|
+
await writeJson(this.file, clean);
|
|
132
|
+
return clean;
|
|
133
|
+
}
|
|
134
|
+
async list() {
|
|
135
|
+
return (await this.read()).pins;
|
|
136
|
+
}
|
|
137
|
+
/** Add a pin; pinning the same path to the same target again is a no-op. Returns false then. */
|
|
138
|
+
async add(pin) {
|
|
139
|
+
const data = await this.read();
|
|
140
|
+
if (data.pins.some((p) => samePin(p, pin)))
|
|
141
|
+
return false;
|
|
142
|
+
data.pins.push(normalPin(pin));
|
|
143
|
+
await this.write(data);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
/** Remove matching pins; returns how many went. */
|
|
147
|
+
async remove(pin) {
|
|
148
|
+
const data = await this.read();
|
|
149
|
+
const kept = data.pins.filter((p) => !samePin(p, pin));
|
|
150
|
+
const gone = data.pins.length - kept.length;
|
|
151
|
+
if (gone)
|
|
152
|
+
await this.write({ ...data, pins: kept });
|
|
153
|
+
return gone;
|
|
154
|
+
}
|
|
155
|
+
async addSource(path) {
|
|
156
|
+
const data = await this.read();
|
|
157
|
+
if (data.sources.includes(path))
|
|
158
|
+
return false;
|
|
159
|
+
data.sources.push(path);
|
|
160
|
+
await this.write(data);
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** Everything in a repo's `.spatial/`. */
|
|
165
|
+
export class Spatial {
|
|
166
|
+
root;
|
|
167
|
+
views;
|
|
168
|
+
gaps;
|
|
169
|
+
pins;
|
|
170
|
+
constructor(root) {
|
|
171
|
+
this.root = root;
|
|
172
|
+
this.views = new ViewStore(root);
|
|
173
|
+
this.gaps = new GapStore(root);
|
|
174
|
+
this.pins = new PinStore(root);
|
|
175
|
+
}
|
|
176
|
+
/** The local, never-committed UI hand-off file (open_view writes it, the canvas follows it). */
|
|
177
|
+
get openFile() {
|
|
178
|
+
return join(this.root, ".spatial-local", "open.json");
|
|
179
|
+
}
|
|
180
|
+
async requestOpen(viewId) {
|
|
181
|
+
await writeJson(this.openFile, { view_id: viewId, at: new Date().toISOString() });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = "0.1.0";
|
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
}
|
package/dist/core/xml.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export function esc(s) {
|
|
2
|
+
return s
|
|
3
|
+
.replace(/&/g, "&")
|
|
4
|
+
.replace(/</g, "<")
|
|
5
|
+
.replace(/>/g, ">")
|
|
6
|
+
.replace(/"/g, """);
|
|
7
|
+
}
|
|
8
|
+
/** Text as CDATA (a `]]>` inside is split so the XML stays well formed). */
|
|
9
|
+
export function cdata(s) {
|
|
10
|
+
return `<![CDATA[${s.replace(/]]>/g, "]]]]><![CDATA[>")}]]>`;
|
|
11
|
+
}
|
|
12
|
+
export const attr = (name, value) => value === undefined || value === "" ? "" : ` ${name}="${esc(value)}"`;
|
|
13
|
+
function linkAttrs(m, all) {
|
|
14
|
+
if (!m.linked_view_id)
|
|
15
|
+
return "";
|
|
16
|
+
const target = all.find((v) => v.id === m.linked_view_id);
|
|
17
|
+
return attr("LinkedView", m.linked_view_id) + (target ? attr("LinkedViewName", target.name) : ` Missing="true"`);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The XML the AI reads for a View. A linked module carries LinkedView (the id)
|
|
21
|
+
* and LinkedViewName; a link to a View that no longer exists says Missing="true".
|
|
22
|
+
*/
|
|
23
|
+
export function viewToXml(view, all = [], indent = "") {
|
|
24
|
+
const byId = new Map(view.modules.map((m) => [m.id, m.name]));
|
|
25
|
+
const i = indent;
|
|
26
|
+
const lines = [
|
|
27
|
+
`${i}<View${attr("Id", view.id)}${attr("Name", view.name)}${attr("Type", view.type)}${view.draft ? ` Draft="true"` : ""}>`,
|
|
28
|
+
];
|
|
29
|
+
for (const m of view.modules) {
|
|
30
|
+
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)}`;
|
|
32
|
+
const body = [];
|
|
33
|
+
if (m.description)
|
|
34
|
+
body.push(`${i} <Description>${esc(m.description)}</Description>`);
|
|
35
|
+
for (const f of m.files ?? [])
|
|
36
|
+
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)}/>`);
|
|
39
|
+
if (body.length === 0)
|
|
40
|
+
lines.push(`${open}/>`);
|
|
41
|
+
else
|
|
42
|
+
lines.push(`${open}>`, ...body, `${i} </Module>`);
|
|
43
|
+
}
|
|
44
|
+
for (const f of view.features ?? []) {
|
|
45
|
+
const names = f.module_ids.map((id) => byId.get(id) ?? id).join(", ");
|
|
46
|
+
lines.push(`${i} <Feature${attr("Id", f.id)}${attr("Name", f.name)}${attr("Modules", names)}/>`);
|
|
47
|
+
}
|
|
48
|
+
lines.push(`${i}</View>`);
|
|
49
|
+
return lines.join("\n");
|
|
50
|
+
}
|