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,156 @@
|
|
|
1
|
+
const eq = (a, b) => JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
|
|
2
|
+
function fieldChanges(before, after, fields) {
|
|
3
|
+
return fields
|
|
4
|
+
.filter((f) => !eq(before[f], after[f]))
|
|
5
|
+
.map((f) => ({ field: String(f), before: before[f] ?? null, after: after[f] ?? null }));
|
|
6
|
+
}
|
|
7
|
+
function diffItems(before, after, fields) {
|
|
8
|
+
const old = new Map(before.map((x) => [x.id, x]));
|
|
9
|
+
const now = new Set(after.map((x) => x.id));
|
|
10
|
+
const byId = {};
|
|
11
|
+
const order = [];
|
|
12
|
+
for (const a of after) {
|
|
13
|
+
const b = old.get(a.id);
|
|
14
|
+
const changed = b ? fieldChanges(b, a, fields) : [];
|
|
15
|
+
byId[a.id] = { id: a.id, status: !b ? "added" : changed.length ? "amended" : "same", item: a, ...(b ? { before: b } : {}), changed };
|
|
16
|
+
order.push(a);
|
|
17
|
+
}
|
|
18
|
+
for (const b of before) {
|
|
19
|
+
if (now.has(b.id))
|
|
20
|
+
continue;
|
|
21
|
+
byId[b.id] = { id: b.id, status: "deleted", item: b, before: b, changed: [] };
|
|
22
|
+
order.push(b);
|
|
23
|
+
}
|
|
24
|
+
return { order, byId };
|
|
25
|
+
}
|
|
26
|
+
const MODULE_FIELDS = ["name", "description", "x", "y", "lane", "linked_view_id", "files"];
|
|
27
|
+
const CONNECTION_FIELDS = ["from", "to", "label"];
|
|
28
|
+
const MESSAGE_FIELDS = ["from", "to", "text", "kind"];
|
|
29
|
+
/** Diff two versions of a View (null before: a new View; null after: a removed one). */
|
|
30
|
+
export function diffView(before, after) {
|
|
31
|
+
const base = (after ?? before);
|
|
32
|
+
const b = before ?? { ...base, modules: [], connections: [], messages: base.messages ? [] : undefined };
|
|
33
|
+
const a = after ?? { ...base, modules: [], connections: [], messages: base.messages ? [] : undefined };
|
|
34
|
+
const mods = diffItems(b.modules, a.modules, MODULE_FIELDS);
|
|
35
|
+
const cons = diffItems(b.connections, a.connections, CONNECTION_FIELDS);
|
|
36
|
+
const msgs = diffItems(b.messages ?? [], a.messages ?? [], MESSAGE_FIELDS);
|
|
37
|
+
const fields = before && after ? fieldChanges(before, after, ["name", "type", "kind", "lanes", "features", "draft"]) : [];
|
|
38
|
+
const items = [mods, cons, msgs].flatMap((d) => Object.values(d.byId));
|
|
39
|
+
if (!after)
|
|
40
|
+
for (const x of items)
|
|
41
|
+
x.status = "deleted";
|
|
42
|
+
const counts = { added: 0, amended: 0, deleted: 0 };
|
|
43
|
+
for (const x of items)
|
|
44
|
+
if (x.status !== "same")
|
|
45
|
+
counts[x.status]++;
|
|
46
|
+
const status = !before ? "added" : !after ? "deleted" : counts.added + counts.amended + counts.deleted + fields.length ? "amended" : "same";
|
|
47
|
+
const merged = {
|
|
48
|
+
...base,
|
|
49
|
+
modules: mods.order,
|
|
50
|
+
connections: cons.order,
|
|
51
|
+
...(a.messages || b.messages ? { messages: msgs.order } : {}),
|
|
52
|
+
};
|
|
53
|
+
return { status, merged, modules: mods.byId, connections: cons.byId, messages: msgs.byId, fields, counts };
|
|
54
|
+
}
|
|
55
|
+
/** The Gap fields a reviewer reads, before and after. */
|
|
56
|
+
export function diffGap(before, after) {
|
|
57
|
+
if (!before)
|
|
58
|
+
return { status: "added", changed: [] };
|
|
59
|
+
if (!after)
|
|
60
|
+
return { status: "deleted", changed: [] };
|
|
61
|
+
const changed = fieldChanges(before, after, ["title", "intent", "state", "milestone", "module_ids", "view_id", "openspec_change"]);
|
|
62
|
+
return { status: changed.length ? "amended" : "same", changed };
|
|
63
|
+
}
|
|
64
|
+
/** Pins added and removed (a pin has no fields to amend). */
|
|
65
|
+
export function diffPins(before, after) {
|
|
66
|
+
const key = (p) => JSON.stringify(p);
|
|
67
|
+
const old = new Set(before.map(key));
|
|
68
|
+
const now = new Set(after.map(key));
|
|
69
|
+
return [
|
|
70
|
+
...after.filter((p) => !old.has(key(p))).map((pin) => ({ pin, status: "added" })),
|
|
71
|
+
...before.filter((p) => !now.has(key(p))).map((pin) => ({ pin, status: "deleted" })),
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
/** A longest-common-subsequence diff; past `limit` cells it gives up and says "all replaced". */
|
|
75
|
+
export function diffSeq(a, b, same = (x, y) => x === y, limit = 4_000_000) {
|
|
76
|
+
let start = 0;
|
|
77
|
+
while (start < a.length && start < b.length && same(a[start], b[start]))
|
|
78
|
+
start++;
|
|
79
|
+
let endA = a.length;
|
|
80
|
+
let endB = b.length;
|
|
81
|
+
while (endA > start && endB > start && same(a[endA - 1], b[endB - 1]))
|
|
82
|
+
(endA--, endB--);
|
|
83
|
+
const head = a.slice(0, start).map((v) => ({ op: "same", v }));
|
|
84
|
+
const tail = a.slice(endA).map((v) => ({ op: "same", v }));
|
|
85
|
+
const A = a.slice(start, endA);
|
|
86
|
+
const B = b.slice(start, endB);
|
|
87
|
+
const n = A.length;
|
|
88
|
+
const m = B.length;
|
|
89
|
+
let mid;
|
|
90
|
+
if (n * m > limit) {
|
|
91
|
+
mid = [...A.map((v) => ({ op: "del", v })), ...B.map((v) => ({ op: "add", v }))];
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const L = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));
|
|
95
|
+
for (let i = n - 1; i >= 0; i--)
|
|
96
|
+
for (let j = m - 1; j >= 0; j--)
|
|
97
|
+
L[i][j] = same(A[i], B[j]) ? L[i + 1][j + 1] + 1 : Math.max(L[i + 1][j], L[i][j + 1]);
|
|
98
|
+
mid = [];
|
|
99
|
+
let i = 0;
|
|
100
|
+
let j = 0;
|
|
101
|
+
while (i < n && j < m) {
|
|
102
|
+
if (same(A[i], B[j]))
|
|
103
|
+
(mid.push({ op: "same", v: A[i] }), i++, j++);
|
|
104
|
+
else if (L[i + 1][j] >= L[i][j + 1])
|
|
105
|
+
mid.push({ op: "del", v: A[i++] });
|
|
106
|
+
else
|
|
107
|
+
mid.push({ op: "add", v: B[j++] });
|
|
108
|
+
}
|
|
109
|
+
while (i < n)
|
|
110
|
+
mid.push({ op: "del", v: A[i++] });
|
|
111
|
+
while (j < m)
|
|
112
|
+
mid.push({ op: "add", v: B[j++] });
|
|
113
|
+
}
|
|
114
|
+
return [...head, ...mid, ...tail];
|
|
115
|
+
}
|
|
116
|
+
/** A line-by-line doc diff; a removed line followed by an added one is shown as one changed line. */
|
|
117
|
+
export function diffText(before, after) {
|
|
118
|
+
const lines = (s) => (s == null ? [] : s.replace(/\r\n/g, "\n").replace(/\n$/, "").split("\n"));
|
|
119
|
+
const ops = diffSeq(lines(before), lines(after));
|
|
120
|
+
const out = [];
|
|
121
|
+
for (let k = 0; k < ops.length;) {
|
|
122
|
+
if (ops[k].op === "same") {
|
|
123
|
+
out.push({ status: "same", text: ops[k].v });
|
|
124
|
+
k++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const dels = [];
|
|
128
|
+
const adds = [];
|
|
129
|
+
while (k < ops.length && ops[k].op !== "same") {
|
|
130
|
+
const o = ops[k++];
|
|
131
|
+
(o.op === "del" ? dels : adds).push(o.v);
|
|
132
|
+
}
|
|
133
|
+
const pairs = Math.min(dels.length, adds.length);
|
|
134
|
+
for (let p = 0; p < pairs; p++)
|
|
135
|
+
out.push({ status: "amended", text: adds[p], spans: diffWords(dels[p], adds[p]) });
|
|
136
|
+
for (const t of dels.slice(pairs))
|
|
137
|
+
out.push({ status: "deleted", text: t });
|
|
138
|
+
for (const t of adds.slice(pairs))
|
|
139
|
+
out.push({ status: "added", text: t });
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
/** Word-level spans between two versions of a line (whitespace kept with the word before it). */
|
|
144
|
+
export function diffWords(before, after) {
|
|
145
|
+
const words = (s) => s.match(/\S+\s*|\s+/g) ?? [];
|
|
146
|
+
const spans = [];
|
|
147
|
+
for (const o of diffSeq(words(before), words(after))) {
|
|
148
|
+
const status = o.op === "same" ? "same" : o.op === "add" ? "added" : "deleted";
|
|
149
|
+
const last = spans[spans.length - 1];
|
|
150
|
+
if (last && last.status === status)
|
|
151
|
+
last.text += o.v;
|
|
152
|
+
else
|
|
153
|
+
spans.push({ status, text: o.v });
|
|
154
|
+
}
|
|
155
|
+
return spans;
|
|
156
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { UserError } from "./errors.js";
|
|
4
|
+
import { slugId } from "./links.js";
|
|
5
|
+
import { flowToView, isFlowchart, parseFlowchart } from "./mermaid.js";
|
|
6
|
+
import { hasOpenSpec, listChanges, listSpecs } from "./openspec.js";
|
|
7
|
+
import { absPath, repoPath } from "./paths.js";
|
|
8
|
+
import { readDoc, resolveDocPath } from "./project.js";
|
|
9
|
+
import { realInside } from "./scope.js";
|
|
10
|
+
/** Doc folders: docs/ and openspec/ when they exist, plus any import_docs added. */
|
|
11
|
+
export const DEFAULT_SOURCES = ["docs", "openspec"];
|
|
12
|
+
const SKIP_DIRS = new Set(["node_modules", ".git", ".spatial", ".spatial-local", "dist"]);
|
|
13
|
+
const MAX_FILES = 2000;
|
|
14
|
+
const isDir = async (p) => (await stat(p).catch(() => null))?.isDirectory() === true;
|
|
15
|
+
/** Every .md under a repo folder (repo-relative, sorted). */
|
|
16
|
+
export async function walkMarkdown(root, rel) {
|
|
17
|
+
const out = [];
|
|
18
|
+
const walk = async (dir) => {
|
|
19
|
+
if (out.length >= MAX_FILES)
|
|
20
|
+
return;
|
|
21
|
+
const entries = await readdir(join(root, dir), { withFileTypes: true }).catch(() => []);
|
|
22
|
+
for (const e of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
23
|
+
const p = dir === "." ? e.name : `${dir}/${e.name}`;
|
|
24
|
+
if (e.isDirectory() && !SKIP_DIRS.has(e.name))
|
|
25
|
+
await walk(p);
|
|
26
|
+
else if (e.isFile() && e.name.toLowerCase().endsWith(".md") && out.length < MAX_FILES)
|
|
27
|
+
out.push(p);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
await walk(rel);
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
export async function docSources(spatial) {
|
|
34
|
+
const extra = (await spatial.pins.read()).sources;
|
|
35
|
+
const all = [...new Set([...DEFAULT_SOURCES, ...extra])];
|
|
36
|
+
const out = [];
|
|
37
|
+
for (const s of all)
|
|
38
|
+
if (await isDir(join(spatial.root, s)))
|
|
39
|
+
out.push(s);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
export function mermaidBlocks(md) {
|
|
43
|
+
const out = [];
|
|
44
|
+
let heading;
|
|
45
|
+
let inBlock = null;
|
|
46
|
+
for (const line of md.split("\n")) {
|
|
47
|
+
const fence = /^\s*(```|~~~)\s*([\w-]*)/.exec(line);
|
|
48
|
+
if (inBlock) {
|
|
49
|
+
if (fence && !fence[2]) {
|
|
50
|
+
if (inBlock.lang === "mermaid") {
|
|
51
|
+
const code = inBlock.lines.join("\n");
|
|
52
|
+
out.push({ index: out.length + 1, code, flowchart: isFlowchart(code), ...(heading ? { title: heading } : {}) });
|
|
53
|
+
}
|
|
54
|
+
inBlock = null;
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
inBlock.lines.push(line);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (fence) {
|
|
61
|
+
inBlock = { lang: fence[2].toLowerCase(), lines: [] };
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const h = /^#{1,6}\s+(.+?)\s*#*\s*$/.exec(line);
|
|
65
|
+
if (h)
|
|
66
|
+
heading = h[1];
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/** A draft View from the n-th Mermaid block of a doc. */
|
|
71
|
+
export async function draftViewFromDoc(spatial, path, index) {
|
|
72
|
+
const doc = await resolveDocPath(spatial, path);
|
|
73
|
+
const rel = doc.rel;
|
|
74
|
+
const md = await readDoc(spatial, doc);
|
|
75
|
+
if (md === null)
|
|
76
|
+
throw new UserError(`cannot read ${rel}`);
|
|
77
|
+
const block = mermaidBlocks(md).find((b) => b.index === index);
|
|
78
|
+
if (!block)
|
|
79
|
+
throw new UserError(`${rel} has no Mermaid block ${index}`);
|
|
80
|
+
const flow = block.flowchart ? parseFlowchart(block.code) : null;
|
|
81
|
+
if (!flow)
|
|
82
|
+
throw new UserError(`Mermaid block ${index} in ${rel} is not a flowchart`);
|
|
83
|
+
const source = `${rel}#${index}`;
|
|
84
|
+
const views = await spatial.views.listViews();
|
|
85
|
+
// Drafting the same block again opens the View made before; it is never overwritten.
|
|
86
|
+
const existing = views.find((v) => v.source === source);
|
|
87
|
+
if (existing)
|
|
88
|
+
return existing;
|
|
89
|
+
const name = block.title ?? basename(rel, ".md");
|
|
90
|
+
const id = slugId(`${basename(rel, ".md")}-${name}`, views.map((v) => v.id));
|
|
91
|
+
return spatial.views.save(flowToView(flow, { id, name, source }));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Import an existing docs/ or openspec/ folder: it is read as-is (nothing is
|
|
95
|
+
* rewritten) and added to the doc sources. With draftViews, every Mermaid
|
|
96
|
+
* flowchart becomes a draft View (one per block; importing again keeps it).
|
|
97
|
+
*/
|
|
98
|
+
export async function importDocs(spatial, path, draftViews = false) {
|
|
99
|
+
if (spatial.mode === "home")
|
|
100
|
+
throw new UserError("in a project, docs are read where they are: scan_source the docs source, then pin each doc as <source_id>:<path>");
|
|
101
|
+
const rel = repoPath(spatial.root, path);
|
|
102
|
+
await realInside({ id: "repo", abs: spatial.root }, absPath(spatial.root, rel));
|
|
103
|
+
if (!(await isDir(join(spatial.root, rel))))
|
|
104
|
+
throw new UserError(`${rel} is not a folder`);
|
|
105
|
+
if (!DEFAULT_SOURCES.includes(rel) && rel !== ".")
|
|
106
|
+
await spatial.pins.addSource(rel);
|
|
107
|
+
const docs = await walkMarkdown(spatial.root, rel);
|
|
108
|
+
const report = { source: rel, docs, flowcharts: [], draft_views: [] };
|
|
109
|
+
if (rel === "openspec" || (rel === "." && (await hasOpenSpec(spatial.root)))) {
|
|
110
|
+
const changes = await listChanges(spatial.root);
|
|
111
|
+
report.openspec = {
|
|
112
|
+
specs: (await listSpecs(spatial.root)).length,
|
|
113
|
+
changes: changes.filter((c) => !c.archived).length,
|
|
114
|
+
archived: changes.filter((c) => c.archived).length,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const views = await spatial.views.listViews();
|
|
118
|
+
for (const doc of docs) {
|
|
119
|
+
const md = await readFile(join(spatial.root, doc), "utf8");
|
|
120
|
+
for (const b of mermaidBlocks(md).filter((b) => b.flowchart)) {
|
|
121
|
+
const source = `${doc}#${b.index}`;
|
|
122
|
+
const entry = { source, ...(b.title ? { title: b.title } : {}) };
|
|
123
|
+
const have = views.find((v) => v.source === source);
|
|
124
|
+
if (draftViews) {
|
|
125
|
+
try {
|
|
126
|
+
const v = await draftViewFromDoc(spatial, doc, b.index);
|
|
127
|
+
entry.view_id = v.id;
|
|
128
|
+
report.draft_views.push(v.id);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// A flowchart we cannot read is listed without a View.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (have)
|
|
135
|
+
entry.view_id = have.id;
|
|
136
|
+
report.flowcharts.push(entry);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return report;
|
|
140
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { commandLine } from "./launch.js";
|
|
4
|
+
import { configTargets } from "./setup.js";
|
|
5
|
+
import { VERSION } from "./version.js";
|
|
6
|
+
/** Every spatial-views entry in the IDE configs that exist. */
|
|
7
|
+
export async function findTargets(o) {
|
|
8
|
+
const out = [];
|
|
9
|
+
for (const t of o.ides.flatMap((ide) => configTargets(ide, o.home, o.windowsHome, o.wsl === true))) {
|
|
10
|
+
const { ide, file } = t;
|
|
11
|
+
if (!file)
|
|
12
|
+
continue;
|
|
13
|
+
let data;
|
|
14
|
+
try {
|
|
15
|
+
data = JSON.parse(await readFile(file, "utf8"));
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
for (const [name, entry] of Object.entries(data.mcpServers ?? {}))
|
|
21
|
+
if (name.startsWith("spatial-views") && entry && typeof entry.command === "string")
|
|
22
|
+
out.push({ ide, file, name, entry: { command: entry.command, args: Array.isArray(entry.args) ? entry.args : [], env: entry.env } });
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function fixFor(entry, code, platform) {
|
|
27
|
+
const bare = /^npx(\.cmd)?$/i.test(entry.command);
|
|
28
|
+
if (platform === "win32" && (bare || code === "EINVAL"))
|
|
29
|
+
return 'npx is a .cmd on Windows: use "command": "cmd", "args": ["/c", "npx", ...] (spatial-views setup --force writes it)';
|
|
30
|
+
if (entry.command === "wsl" && platform !== "win32" && code === "EACCES")
|
|
31
|
+
return "this `wsl` entry is for a Windows IDE on a Windows folder; in WSL Remote mode use the `spatial-views` entry";
|
|
32
|
+
if (code === "ENOENT" || code === "EACCES")
|
|
33
|
+
return `${entry.command} was not found: write its absolute path (spatial-views setup --force)`;
|
|
34
|
+
return `run \`${commandLine(entry.command, entry.args)}\` in a terminal to see the error, then spatial-views setup --force`;
|
|
35
|
+
}
|
|
36
|
+
/** Stop the server, and on Windows what `cmd /c` started under it. */
|
|
37
|
+
function stop(pid, kill, platform) {
|
|
38
|
+
if (platform === "win32" && pid)
|
|
39
|
+
spawnSync("taskkill", ["/pid", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
|
40
|
+
else
|
|
41
|
+
kill();
|
|
42
|
+
}
|
|
43
|
+
/** Start one entry as written and run initialize, then tools/list. */
|
|
44
|
+
export async function checkEntry(entry, o = {}) {
|
|
45
|
+
if (!(o.wsl && entry.command === "wsl"))
|
|
46
|
+
return run(entry, entry.command, o);
|
|
47
|
+
// From inside WSL, a Windows `wsl` entry is what Windows would run: wsl.exe through interop.
|
|
48
|
+
for (const exe of ["wsl.exe", "/mnt/c/Windows/System32/wsl.exe"]) {
|
|
49
|
+
const r = await run(entry, exe, o);
|
|
50
|
+
if (!/ENOENT/.test(r.error ?? ""))
|
|
51
|
+
return r;
|
|
52
|
+
}
|
|
53
|
+
return { ok: false, skipped: true, error: "wsl.exe is not reachable from here (no WSL interop)", fix: "run `npx spatial-views doctor` in PowerShell to check it" };
|
|
54
|
+
}
|
|
55
|
+
function run(entry, command, o) {
|
|
56
|
+
const platform = o.platform ?? process.platform;
|
|
57
|
+
const timeoutMs = o.timeoutMs ?? 60_000;
|
|
58
|
+
return new Promise((done) => {
|
|
59
|
+
let settled = false;
|
|
60
|
+
let stderr = "";
|
|
61
|
+
let buf = "";
|
|
62
|
+
let child;
|
|
63
|
+
try {
|
|
64
|
+
child = spawn(command, entry.args, {
|
|
65
|
+
env: { ...process.env, ...(entry.env ?? {}) },
|
|
66
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
67
|
+
windowsHide: true,
|
|
68
|
+
shell: false,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
// Windows: a .cmd/.bat without a shell throws EINVAL here instead of emitting "error".
|
|
73
|
+
const e = err;
|
|
74
|
+
return done({ ok: false, error: `could not start ${command}: ${e.code ?? e.message}`, fix: fixFor(entry, e.code, platform) });
|
|
75
|
+
}
|
|
76
|
+
const finish = (r) => {
|
|
77
|
+
if (settled)
|
|
78
|
+
return;
|
|
79
|
+
settled = true;
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
child.stdin?.end();
|
|
82
|
+
stop(child.pid, () => child.kill(), platform);
|
|
83
|
+
done(r);
|
|
84
|
+
};
|
|
85
|
+
const fail = (error, code) => finish({ ok: false, error, fix: fixFor(entry, code, platform) });
|
|
86
|
+
const timer = setTimeout(() => fail(`no answer in ${Math.round(timeoutMs / 1000)}s`), timeoutMs);
|
|
87
|
+
const send = (m) => child.stdin?.write(JSON.stringify(m) + "\n");
|
|
88
|
+
child.on("error", (e) => fail(`could not start ${command}: ${e.code ?? e.message}`, e.code));
|
|
89
|
+
child.stdin?.on("error", () => { });
|
|
90
|
+
child.stderr?.on("data", (d) => (stderr = (stderr + d.toString()).slice(-4000)));
|
|
91
|
+
child.on("exit", (code) => {
|
|
92
|
+
const last = stderr.trim().split(/\r?\n/).filter(Boolean).pop();
|
|
93
|
+
fail(`exited with code ${code}${last ? `: ${last}` : ""}`);
|
|
94
|
+
});
|
|
95
|
+
child.stdout?.on("data", (d) => {
|
|
96
|
+
buf += d.toString();
|
|
97
|
+
let nl;
|
|
98
|
+
while ((nl = buf.indexOf("\n")) >= 0) {
|
|
99
|
+
const line = buf.slice(0, nl).trim();
|
|
100
|
+
buf = buf.slice(nl + 1);
|
|
101
|
+
if (!line.startsWith("{"))
|
|
102
|
+
continue;
|
|
103
|
+
let m;
|
|
104
|
+
try {
|
|
105
|
+
m = JSON.parse(line);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (m.error)
|
|
111
|
+
return fail(`the server said: ${m.error.message ?? "error"}`);
|
|
112
|
+
if (m.id === 1) {
|
|
113
|
+
send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
114
|
+
send({ jsonrpc: "2.0", id: 2, method: "tools/list", params: {} });
|
|
115
|
+
}
|
|
116
|
+
else if (m.id === 2) {
|
|
117
|
+
const n = m.result?.tools?.length ?? 0;
|
|
118
|
+
if (n > 0)
|
|
119
|
+
finish({ ok: true, tools: n });
|
|
120
|
+
else
|
|
121
|
+
fail("tools/list returned no tools");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
send({
|
|
126
|
+
jsonrpc: "2.0",
|
|
127
|
+
id: 1,
|
|
128
|
+
method: "initialize",
|
|
129
|
+
params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "spatial-views-doctor", version: VERSION } },
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
export async function doctor(o) {
|
|
134
|
+
const targets = await findTargets(o);
|
|
135
|
+
if (!targets.length)
|
|
136
|
+
return { ok: false, lines: ["FAIL no spatial-views entry in any IDE config. Fix: run `npx spatial-views setup`"] };
|
|
137
|
+
const lines = [];
|
|
138
|
+
let ok = true;
|
|
139
|
+
for (const t of targets) {
|
|
140
|
+
const r = await checkEntry(t.entry, o);
|
|
141
|
+
const where = `${t.ide} ${t.name} (${t.file})`;
|
|
142
|
+
if (r.ok)
|
|
143
|
+
lines.push(`OK ${where}: ${r.tools} tools`);
|
|
144
|
+
else if (r.skipped)
|
|
145
|
+
lines.push(`SKIP ${where}: ${r.error}`, ` ${r.fix}`);
|
|
146
|
+
else {
|
|
147
|
+
ok = false;
|
|
148
|
+
lines.push(`FAIL ${where}: ${r.error}`, ` fix: ${r.fix}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return { ok, lines };
|
|
152
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { parse } from "yaml";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { UserError } from "./errors.js";
|
|
6
|
+
import { BUILTIN_DIR } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* A Project panel doc type (`toolboxes/types/docs/<id>.yaml`, or the project's
|
|
9
|
+
* `<data>/types/docs/<id>.yaml`, same id replaces): PRD, MDD, Gap, a plain doc.
|
|
10
|
+
* (The Docs toolbox's types, dropped on a module, are toolbox YAML: see types.ts.)
|
|
11
|
+
* A new doc is made from its markdown template ({{title}}, {{date}}) in its folder.
|
|
12
|
+
*/
|
|
13
|
+
export const DocTypeSchema = z.object({
|
|
14
|
+
id: z.string().regex(/^[a-z0-9][a-z0-9-]*$/),
|
|
15
|
+
name: z.string().min(1),
|
|
16
|
+
description: z.string().optional(),
|
|
17
|
+
icon: z.string().default("file-text"),
|
|
18
|
+
/** Where new docs of this type go, under the project (default docs). */
|
|
19
|
+
folder: z
|
|
20
|
+
.string()
|
|
21
|
+
.regex(/^[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)*$/, "a folder is a relative path like docs or specs/prd")
|
|
22
|
+
.default("docs"),
|
|
23
|
+
/** The title a new doc gets until a person renames it. */
|
|
24
|
+
title: z.string().default("New doc"),
|
|
25
|
+
template: z.string().default("# {{title}}\n"),
|
|
26
|
+
});
|
|
27
|
+
export function parseDocType(text, id) {
|
|
28
|
+
const raw = (parse(text) ?? {});
|
|
29
|
+
return DocTypeSchema.parse({ ...raw, id: raw.id ?? id });
|
|
30
|
+
}
|
|
31
|
+
async function readDir(dir) {
|
|
32
|
+
const files = (await readdir(dir).catch(() => [])).filter((f) => /\.ya?ml$/.test(f)).sort();
|
|
33
|
+
const out = [];
|
|
34
|
+
for (const f of files) {
|
|
35
|
+
try {
|
|
36
|
+
out.push(parseDocType(await readFile(join(dir, f), "utf8"), f.replace(/\.ya?ml$/, "")));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// A broken file is left out; the rest still load.
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
/** The order the built-in doc types are offered in. */
|
|
45
|
+
const ORDER = ["prd", "mdd", "gap", "plain"];
|
|
46
|
+
/** Every doc type: the built-in ones, then the project's (same id replaces). */
|
|
47
|
+
export async function loadDocTypes(dataDir, builtinDir = BUILTIN_DIR) {
|
|
48
|
+
const byId = new Map();
|
|
49
|
+
for (const t of await readDir(join(builtinDir, "types", "docs")))
|
|
50
|
+
byId.set(t.id, t);
|
|
51
|
+
if (dataDir)
|
|
52
|
+
for (const t of await readDir(join(dataDir, "types", "docs")))
|
|
53
|
+
byId.set(t.id, t);
|
|
54
|
+
const rank = (id) => (ORDER.includes(id) ? ORDER.indexOf(id) : ORDER.length);
|
|
55
|
+
return [...byId.values()].sort((a, b) => rank(a.id) - rank(b.id) || a.name.localeCompare(b.name));
|
|
56
|
+
}
|
|
57
|
+
export function findDocType(types, ref) {
|
|
58
|
+
const hit = types.find((t) => t.id === ref) ?? types.find((t) => t.name.toLowerCase() === ref.toLowerCase());
|
|
59
|
+
if (!hit)
|
|
60
|
+
throw new UserError(`no doc type "${ref}" (${types.map((t) => t.id).join(", ")})`);
|
|
61
|
+
return hit;
|
|
62
|
+
}
|
|
63
|
+
/** Fill a template's {{title}} and {{date}} (unknown names are left as they are). */
|
|
64
|
+
export function fillTemplate(template, vars) {
|
|
65
|
+
const values = { title: vars.title, date: vars.date ?? new Date().toISOString().slice(0, 10) };
|
|
66
|
+
return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, k) => values[k] ?? all);
|
|
67
|
+
}
|
|
68
|
+
/** A file or id name from a title: lower case, words joined by dashes. */
|
|
69
|
+
export function slugOf(title) {
|
|
70
|
+
return (title
|
|
71
|
+
.toLowerCase()
|
|
72
|
+
.normalize("NFKD")
|
|
73
|
+
.replace(/[^\w\s-]/g, "")
|
|
74
|
+
.trim()
|
|
75
|
+
.replace(/[\s_]+/g, "-")
|
|
76
|
+
.replace(/-+/g, "-")
|
|
77
|
+
.slice(0, 60)
|
|
78
|
+
.replace(/^-|-$/g, "") || "untitled");
|
|
79
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { UserError } from "./errors.js";
|
|
2
|
+
import { slugId } from "./links.js";
|
|
3
|
+
/**
|
|
4
|
+
* Drawing a View: pure functions shared by the MCP tools and the canvas.
|
|
5
|
+
* Every change returns a new View; nothing is mutated.
|
|
6
|
+
*/
|
|
7
|
+
/** A module on this View, by id or else by name (case-insensitive). */
|
|
8
|
+
export function findModule(view, ref) {
|
|
9
|
+
const byId = view.modules.find((m) => m.id === ref);
|
|
10
|
+
if (byId)
|
|
11
|
+
return byId;
|
|
12
|
+
const byName = view.modules.filter((m) => m.name.toLowerCase() === ref.toLowerCase());
|
|
13
|
+
if (byName.length === 1)
|
|
14
|
+
return byName[0];
|
|
15
|
+
if (byName.length > 1)
|
|
16
|
+
throw new UserError(`more than one module on ${view.id} is named "${ref}"; use the id`);
|
|
17
|
+
throw new UserError(`no module "${ref}" on View ${view.id}`);
|
|
18
|
+
}
|
|
19
|
+
/** The next free grid slot, so modules added without a position don't stack. */
|
|
20
|
+
function nextSlot(view) {
|
|
21
|
+
const n = view.modules.length;
|
|
22
|
+
return { x: (n % 4) * 240, y: Math.floor(n / 4) * 140 };
|
|
23
|
+
}
|
|
24
|
+
export function addModule(view, input) {
|
|
25
|
+
const name = input.name.trim();
|
|
26
|
+
if (!name)
|
|
27
|
+
throw new UserError("a module needs a name");
|
|
28
|
+
const taken = view.modules.map((m) => m.id);
|
|
29
|
+
if (input.id && taken.includes(input.id))
|
|
30
|
+
throw new UserError(`module id ${input.id} is taken on ${view.id}`);
|
|
31
|
+
const slot = nextSlot(view);
|
|
32
|
+
const module = {
|
|
33
|
+
id: input.id ?? slugId(name, taken),
|
|
34
|
+
name,
|
|
35
|
+
x: input.x ?? slot.x,
|
|
36
|
+
y: input.y ?? slot.y,
|
|
37
|
+
...(input.description ? { description: input.description } : {}),
|
|
38
|
+
...(input.type ? { type: input.type } : {}),
|
|
39
|
+
...(input.properties && Object.keys(input.properties).length ? { properties: input.properties } : {}),
|
|
40
|
+
};
|
|
41
|
+
return { view: { ...view, modules: [...view.modules, module] }, module };
|
|
42
|
+
}
|
|
43
|
+
export function connect(view, fromRef, toRef, label) {
|
|
44
|
+
const from = findModule(view, fromRef);
|
|
45
|
+
const to = findModule(view, toRef);
|
|
46
|
+
const ids = new Set(view.connections.map((c) => c.id));
|
|
47
|
+
let n = view.connections.length + 1;
|
|
48
|
+
while (ids.has(`c${n}`))
|
|
49
|
+
n++;
|
|
50
|
+
const connection = { id: `c${n}`, from: from.id, to: to.id, ...(label ? { label } : {}) };
|
|
51
|
+
return { view: { ...view, connections: [...view.connections, connection] }, connection };
|
|
52
|
+
}
|
|
53
|
+
function withModule(view, ref, change) {
|
|
54
|
+
const target = findModule(view, ref);
|
|
55
|
+
return { ...view, modules: view.modules.map((m) => (m.id === target.id ? change(m) : m)) };
|
|
56
|
+
}
|
|
57
|
+
export function moveModule(view, ref, x, y) {
|
|
58
|
+
return withModule(view, ref, (m) => ({ ...m, x: Math.round(x), y: Math.round(y) }));
|
|
59
|
+
}
|
|
60
|
+
/** A name not yet used on the View: "Database", then "Database 2", "Database 3". */
|
|
61
|
+
export function uniqueName(view, base) {
|
|
62
|
+
const names = new Set(view.modules.map((m) => m.name.toLowerCase()));
|
|
63
|
+
if (!names.has(base.toLowerCase()))
|
|
64
|
+
return base;
|
|
65
|
+
let n = 2;
|
|
66
|
+
while (names.has(`${base} ${n}`.toLowerCase()))
|
|
67
|
+
n++;
|
|
68
|
+
return `${base} ${n}`;
|
|
69
|
+
}
|
|
70
|
+
export function updateModule(view, ref, patch) {
|
|
71
|
+
return withModule(view, ref, (m) => {
|
|
72
|
+
const next = { ...m };
|
|
73
|
+
if (patch.name !== undefined) {
|
|
74
|
+
if (!patch.name.trim())
|
|
75
|
+
throw new UserError("a module needs a name");
|
|
76
|
+
next.name = patch.name.trim();
|
|
77
|
+
}
|
|
78
|
+
if (patch.description !== undefined) {
|
|
79
|
+
if (patch.description)
|
|
80
|
+
next.description = patch.description;
|
|
81
|
+
else
|
|
82
|
+
delete next.description;
|
|
83
|
+
}
|
|
84
|
+
if (patch.type !== undefined) {
|
|
85
|
+
if (patch.type)
|
|
86
|
+
next.type = patch.type;
|
|
87
|
+
else
|
|
88
|
+
delete next.type;
|
|
89
|
+
}
|
|
90
|
+
if (patch.properties !== undefined) {
|
|
91
|
+
if (patch.properties && Object.keys(patch.properties).length)
|
|
92
|
+
next.properties = patch.properties;
|
|
93
|
+
else
|
|
94
|
+
delete next.properties;
|
|
95
|
+
}
|
|
96
|
+
return next;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** Remove a module, its connections, and its place in any Feature. */
|
|
100
|
+
export function removeModule(view, ref) {
|
|
101
|
+
const target = findModule(view, ref);
|
|
102
|
+
const features = view.features?.map((f) => ({ ...f, module_ids: f.module_ids.filter((id) => id !== target.id) }));
|
|
103
|
+
return {
|
|
104
|
+
...view,
|
|
105
|
+
modules: view.modules.filter((m) => m.id !== target.id),
|
|
106
|
+
connections: view.connections.filter((c) => c.from !== target.id && c.to !== target.id),
|
|
107
|
+
...(features ? { features } : {}),
|
|
108
|
+
...(view.messages ? { messages: view.messages.filter((m) => m.from !== target.id && m.to !== target.id) } : {}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export function removeConnection(view, id) {
|
|
112
|
+
if (!view.connections.some((c) => c.id === id))
|
|
113
|
+
throw new UserError(`no connection ${id} on View ${view.id}`);
|
|
114
|
+
return { ...view, connections: view.connections.filter((c) => c.id !== id) };
|
|
115
|
+
}
|
|
116
|
+
/** Add a source file to a module (repo-relative); adding it again is a no-op. */
|
|
117
|
+
export function addFilePointer(view, ref, path) {
|
|
118
|
+
return withModule(view, ref, (m) => (m.files?.includes(path) ? m : { ...m, files: [...(m.files ?? []), path] }));
|
|
119
|
+
}
|
|
120
|
+
/** A Feature from selected modules (the lasso). */
|
|
121
|
+
export function addFeature(view, name, moduleRefs) {
|
|
122
|
+
if (!name.trim())
|
|
123
|
+
throw new UserError("a Feature needs a name");
|
|
124
|
+
if (moduleRefs.length === 0)
|
|
125
|
+
throw new UserError("select at least one module");
|
|
126
|
+
const module_ids = [...new Set(moduleRefs.map((r) => findModule(view, r).id))];
|
|
127
|
+
const taken = (view.features ?? []).map((f) => f.id);
|
|
128
|
+
const feature = { id: slugId(name, taken), name: name.trim(), module_ids };
|
|
129
|
+
return { view: { ...view, features: [...(view.features ?? []), feature] }, feature };
|
|
130
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experimental features: Gaps, Features (the lasso's Make Gap and Make
|
|
3
|
+
* Feature) and the work-split steps in the Skills. They stay in the code but
|
|
4
|
+
* are hidden unless SPATIAL_VIEWS_EXPERIMENTAL=1 (Direction, 22 Nov 2026: the
|
|
5
|
+
* free version is Docs and Diagrams). Swim lanes are not experimental: they
|
|
6
|
+
* stay in the product (Settings: Lanes on this View), just not in the demo or
|
|
7
|
+
* the default pre-audit.
|
|
8
|
+
*/
|
|
9
|
+
export const EXPERIMENTAL_ENV = "SPATIAL_VIEWS_EXPERIMENTAL";
|
|
10
|
+
export function experimental(env = process.env) {
|
|
11
|
+
return env[EXPERIMENTAL_ENV] === "1";
|
|
12
|
+
}
|
|
13
|
+
export const flags = (env = process.env) => ({ experimental: experimental(env) });
|