cladogram 0.3.0 → 0.4.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/README.md +90 -345
- package/bin/clado.js +99 -17
- package/examples/showcase/README.md +3 -1
- package/examples/showcase/build.sh +3 -3
- package/package.json +2 -2
- package/skills/clado/SKILL.md +50 -773
- package/skills/clado/examples/09-/345/215/217/344/275/234/347/224/273/345/270/203/backend.md +80 -0
- package/skills/clado/examples/09-/345/215/217/344/275/234/347/224/273/345/270/203/data.md +53 -0
- package/skills/clado/examples/09-/345/215/217/344/275/234/347/224/273/345/270/203/interaction.md +70 -0
- package/skills/clado/examples/09-/345/215/217/344/275/234/347/224/273/345/270/203/overview.md +30 -0
- package/skills/clado/examples/09-/345/215/217/344/275/234/347/224/273/345/270/203/verify.md +35 -0
- package/skills/clado/examples/10-/346/235/203/351/231/220/347/224/263/350/257/267/approval.md +69 -0
- package/skills/clado/examples/10-/346/235/203/351/231/220/347/224/263/350/257/267/data-verify.md +49 -0
- package/skills/clado/examples/10-/346/235/203/351/231/220/347/224/263/350/257/267/interaction.md +47 -0
- package/skills/clado/examples/10-/346/235/203/351/231/220/347/224/263/350/257/267/overview.md +39 -0
- package/skills/clado/examples/11-/344/272/213/346/225/205/345/223/215/345/272/224/evidence.md +42 -0
- package/skills/clado/examples/11-/344/272/213/346/225/205/345/223/215/345/272/224/overview.md +26 -0
- package/skills/clado/examples/11-/344/272/213/346/225/205/345/223/215/345/272/224/response.md +64 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/api.md +127 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/caller.md +50 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/calls.md +185 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/code.md +249 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/consumer.md +93 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/contracts.md +89 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/data.md +237 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/overview.md +75 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/publisher.md +175 -0
- package/skills/clado/examples/12-/346/216/245/345/217/243/345/210/260/344/273/243/347/240/201/verify.md +53 -0
- package/skills/clado/examples/13-/346/216/245/345/217/243/346/226/271/346/263/225/345/210/260SQL/01-/347/224/263/350/257/267/350/275/254/345/215/225.md +499 -0
- package/skills/clado/examples/13-/346/216/245/345/217/243/346/226/271/346/263/225/345/210/260SQL/02-/345/267/262/346/234/211/350/256/242/345/215/225/346/216/245/345/217/243.md +46 -0
- package/skills/clado/examples/13-/346/216/245/345/217/243/346/226/271/346/263/225/345/210/260SQL/03-/350/241/250/347/273/223/346/236/204.md +100 -0
- package/skills/clado/examples/13-/346/216/245/345/217/243/346/226/271/346/263/225/345/210/260SQL/overview.md +47 -0
- package/skills/clado/examples/build.sh +4 -1
- package/skills/clado/references/content-patterns.md +61 -0
- package/skills/clado/references/data.md +37 -0
- package/skills/clado/references/delivery.md +57 -0
- package/skills/clado/references/diagnostics.md +20 -104
- package/skills/clado/references/editing.md +49 -0
- package/skills/clado/references/engineering.md +167 -0
- package/skills/clado/references/flows.md +53 -0
- package/skills/clado/references/interactions.md +46 -0
- package/skills/clado/references/operations.md +39 -0
- package/skills/clado/references/requirements.md +37 -0
- package/skills/clado/references/verification.md +27 -0
- package/src/core/parse.js +59 -12
- package/src/html/app-css.js +52 -71
- package/src/html/canvas-card.js +66 -0
- package/src/html/canvas-chain.js +203 -0
- package/src/html/canvas-focus.js +175 -0
- package/src/html/canvas-ui.css +21 -0
- package/src/html/canvas-ui.js +38 -0
- package/src/html/canvas-wires.js +56 -0
- package/src/html/mindmap-connectors.js +57 -0
- package/src/html/mindmap-media.js +39 -0
- package/src/html/mindmap-motion.js +69 -0
- package/src/html/page.js +21 -6
- package/src/html/runtime-api.js +62 -0
- package/src/html/runtime-fixes.js +41 -0
- package/src/html/runtime.js +195 -159
- package/src/server/assets/editor.css +1 -0
- package/src/server/assets/editor.js +79 -0
- package/src/server/assets/library-canvas.js +90 -0
- package/src/server/assets/library.css +9 -0
- package/src/server/assets/library.html +20 -0
- package/src/server/assets/library.js +94 -0
- package/src/server/assets/tree.js +26 -0
- package/src/server/export.js +53 -0
- package/src/server/workspace.js +95 -0
- package/src/skill/targets.js +18 -9
- package/src/workspace/commands.js +42 -0
- package/src/workspace/documents.js +143 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, renameSync, unlinkSync, mkdirSync, existsSync, statSync, readdirSync, realpathSync, openSync, closeSync } from 'node:fs';
|
|
2
|
+
import { resolve, dirname, relative, basename, extname, sep } from 'node:path';
|
|
3
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
4
|
+
import { parseDocument, headingOf, fenceOf, splitFrontMatter } from '../core/parse.js';
|
|
5
|
+
|
|
6
|
+
export class DocumentError extends Error {
|
|
7
|
+
constructor(message, status = 400, diagnostics = []) { super(message); this.status = status; this.diagnostics = diagnostics; }
|
|
8
|
+
}
|
|
9
|
+
export const versionOf = source => createHash('sha256').update(source).digest('hex');
|
|
10
|
+
export function inspectSource(source, file = '') {
|
|
11
|
+
const { doc, diagnostics } = parseDocument(source, { path: file, fileBase: basename(file, extname(file)) });
|
|
12
|
+
const sections = [], clean = source.replace(/^\uFEFF/, ''), fm = splitFrontMatter(clean);
|
|
13
|
+
const lines = source.split(/\r?\n/), bodyLines = fm.body.split(/\r?\n/);
|
|
14
|
+
const offset = clean.slice(0, clean.length - fm.body.length).split(/\r?\n/).length - 1, stack = [];
|
|
15
|
+
let fence = null;
|
|
16
|
+
for (let j = 0; j < bodyLines.length; j++) {
|
|
17
|
+
const i = j + offset, f = fenceOf(bodyLines[j]);
|
|
18
|
+
if (f) { if (!fence) fence = f[1]; else if (f[1][0] === fence[0] && f[1].length >= fence.length) fence = null; continue; }
|
|
19
|
+
if (fence) continue;
|
|
20
|
+
const heading = headingOf(bodyLines[j]); if (!heading) continue;
|
|
21
|
+
const { level, title } = heading;
|
|
22
|
+
while (stack.length && stack.at(-1).level >= level) stack.pop();
|
|
23
|
+
const path = [...stack.map(s => s.title), title].join('.');
|
|
24
|
+
const section = { path, title, level, line: i + 1, start: i, end: lines.length, bodyEnd: lines.length };
|
|
25
|
+
sections.push(section); stack.push(section);
|
|
26
|
+
}
|
|
27
|
+
sections.forEach((s, i) => {
|
|
28
|
+
s.bodyEnd = sections[i + 1]?.start ?? lines.length;
|
|
29
|
+
s.end = sections.slice(i + 1).find(n => n.level <= s.level)?.start ?? lines.length;
|
|
30
|
+
s.content = lines.slice(s.start + 1, s.bodyEnd).join('\n').trim();
|
|
31
|
+
});
|
|
32
|
+
return { source, version: versionOf(source), doc, diagnostics, sections };
|
|
33
|
+
}
|
|
34
|
+
export function readDocument(file) { return inspectSource(readFileSync(file, 'utf8'), file); }
|
|
35
|
+
export function validateSource(source, file) {
|
|
36
|
+
const result = inspectSource(source, file);
|
|
37
|
+
if (result.diagnostics.some(d => d.severity === 'error')) throw new DocumentError('Markdown 解析失败,未写入。', 422, result.diagnostics);
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
export function writeDocument(file, source, { create = false, version } = {}) {
|
|
41
|
+
if (typeof source !== 'string') throw new DocumentError('Markdown 源文必须是字符串。');
|
|
42
|
+
if (!/\.md$/i.test(file)) throw new DocumentError('文档必须使用 .md 扩展名。');
|
|
43
|
+
const initial = existsSync(file) ? versionOf(readFileSync(file,'utf8')) : null;
|
|
44
|
+
const expected = version || initial;
|
|
45
|
+
const result = validateSource(source, file);
|
|
46
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
47
|
+
const lock = resolve(dirname(file), '.' + basename(file) + '.clado-lock');
|
|
48
|
+
let descriptor;
|
|
49
|
+
try { descriptor = openSync(lock,'wx',0o600); }
|
|
50
|
+
catch(error) {
|
|
51
|
+
if (error.code !== 'EEXIST') throw error;
|
|
52
|
+
let stale = false;
|
|
53
|
+
try { const owner=JSON.parse(readFileSync(lock,'utf8')); try { process.kill(owner.pid,0); } catch(e) { stale=e.code==='ESRCH'; } } catch {}
|
|
54
|
+
if (stale) { try { unlinkSync(lock); descriptor=openSync(lock,'wx',0o600); } catch {} }
|
|
55
|
+
if (descriptor === undefined) throw new DocumentError('文档正在被另一个写入者保存,请重新读取后重试。',409);
|
|
56
|
+
}
|
|
57
|
+
const temporary = resolve(dirname(file), '.' + basename(file) + '.' + randomBytes(6).toString('hex') + '.tmp');
|
|
58
|
+
try {
|
|
59
|
+
writeFileSync(descriptor, JSON.stringify({pid:process.pid}));
|
|
60
|
+
if (create && existsSync(file)) throw new DocumentError('文档已经存在,未覆盖。',409);
|
|
61
|
+
if (!create && !existsSync(file)) throw new DocumentError('找不到要修改的文档。',404);
|
|
62
|
+
if (!create && expected !== versionOf(readFileSync(file,'utf8'))) throw new DocumentError('文件已被其他编辑修改,请重新读取后再保存。',409);
|
|
63
|
+
writeFileSync(temporary, source, { flag:'wx', mode:existsSync(file) ? statSync(file).mode & 0o777 : 0o644 });
|
|
64
|
+
if (!create && expected !== versionOf(readFileSync(file,'utf8'))) throw new DocumentError('文件已变化,请重新读取。',409);
|
|
65
|
+
renameSync(temporary,file);
|
|
66
|
+
} finally {
|
|
67
|
+
if (existsSync(temporary)) unlinkSync(temporary);
|
|
68
|
+
closeSync(descriptor); unlinkSync(lock);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function mutateNode(file, action, { parent, target, title, content = '', version } = {}) {
|
|
74
|
+
if (typeof content !== 'string') throw new DocumentError('节点正文必须是字符串。');
|
|
75
|
+
const current = readDocument(file), lines = current.source.split(/\r?\n/);
|
|
76
|
+
if (version && version !== current.version) throw new DocumentError('文件版本冲突,请重新读取。', 409);
|
|
77
|
+
// 节点正文可以含围栏示例,不能偷偷引入其他节点。
|
|
78
|
+
if (inspectSource('# 正文\n\n' + content).sections.length !== 1) throw new DocumentError('节点正文含标题。新增节点请用 node add,改整篇结构请用 doc update。');
|
|
79
|
+
const find = path => {
|
|
80
|
+
const hits = current.sections.filter(s => s.path === path);
|
|
81
|
+
if (hits.length !== 1) throw new DocumentError(hits.length ? '节点路径不唯一,请先调整重名。' : '找不到节点,请用 doc read 查看完整路径。', 404);
|
|
82
|
+
return hits[0];
|
|
83
|
+
};
|
|
84
|
+
if (action === 'add') {
|
|
85
|
+
if (!title || /[\r\n]/.test(title)) throw new DocumentError('新增节点需要单行 --title。');
|
|
86
|
+
const p = parent ? find(parent) : null, level = p ? p.level + 1 : 1;
|
|
87
|
+
if (level > 6) throw new DocumentError('最深为 L6,请新建文档并用 .md 链接关联。');
|
|
88
|
+
const path = p ? p.path + '.' + title : title;
|
|
89
|
+
if (current.sections.some(s => s.path === path)) throw new DocumentError('这个父节点下已有同名标题。', 409);
|
|
90
|
+
lines.splice(p ? p.end : lines.length, 0, '', '#'.repeat(level) + ' ' + title, '', content.trim(), '');
|
|
91
|
+
} else if (action === 'update') {
|
|
92
|
+
const s = find(target);
|
|
93
|
+
lines.splice(s.start + 1, s.bodyEnd - s.start - 1, '', content.trim(), '');
|
|
94
|
+
} else throw new DocumentError('node 只支持 add / update。');
|
|
95
|
+
const candidate = lines.join('\n');
|
|
96
|
+
const next = inspectSource(candidate,file);
|
|
97
|
+
const signature = s => s.path + ':' + s.level;
|
|
98
|
+
const before = current.sections.map(signature);
|
|
99
|
+
const after = next.sections.map(signature);
|
|
100
|
+
if (action === 'add') {
|
|
101
|
+
const addedPath = parent ? parent + '.' + title : title;
|
|
102
|
+
const index = next.sections.findIndex(s=>s.path===addedPath);
|
|
103
|
+
if (index >= 0) after.splice(index,1);
|
|
104
|
+
if (index < 0 || next.sections.length !== current.sections.length + 1) throw new DocumentError('新增内容改变了其他节点结构,请检查标题或未闭合围栏。');
|
|
105
|
+
}
|
|
106
|
+
if (JSON.stringify(before) !== JSON.stringify(after)) throw new DocumentError('正文改变了已有节点结构,可能含未闭合围栏;未写入。');
|
|
107
|
+
// 即使位于最后一个节点,未闭合围栏也不能保存为正文。
|
|
108
|
+
let fence = null;
|
|
109
|
+
for (const line of content.split(/\r?\n/)) { const f=fenceOf(line); if(f) { if(!fence)fence=f[1];else if(f[1][0]===fence[0]&&f[1].length>=fence.length)fence=null; } }
|
|
110
|
+
if (fence) throw new DocumentError('正文代码围栏未闭合,未写入。');
|
|
111
|
+
return writeDocument(file, candidate, { version: current.version });
|
|
112
|
+
}
|
|
113
|
+
export function scanDirectory(root) {
|
|
114
|
+
const documents = [], skipped = [], parsed = new Map();
|
|
115
|
+
const walk = directory => {
|
|
116
|
+
for (const entry of readdirSync(directory, { withFileTypes: true }).sort((a,b) => a.name.localeCompare(b.name))) {
|
|
117
|
+
if (entry.name.startsWith('.') || ['node_modules','dist'].includes(entry.name)) continue;
|
|
118
|
+
const full = resolve(directory, entry.name);
|
|
119
|
+
if (entry.isDirectory()) { walk(full); continue; }
|
|
120
|
+
if (!entry.isFile() || !/\.md$/i.test(entry.name)) continue;
|
|
121
|
+
const file = relative(root, full).split(sep).join('/');
|
|
122
|
+
try {
|
|
123
|
+
const item = readDocument(full);
|
|
124
|
+
const summary = { file, title: item.doc.title, version: item.version, cards: item.doc.cards.length,
|
|
125
|
+
warnings: item.diagnostics.filter(d => d.severity === 'warn').length, modified: statSync(full).mtime.toISOString() };
|
|
126
|
+
if (item.diagnostics.some(d => d.severity === 'error')) { skipped.push({ file, diagnostics: item.diagnostics }); continue; }
|
|
127
|
+
documents.push(summary); parsed.set(file, { ...item, ...summary });
|
|
128
|
+
} catch (error) { skipped.push({ file, diagnostics: [{severity:'error',code:'READ_FAILED',message:error.message}] }); }
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
walk(root); return { documents, skipped, parsed };
|
|
132
|
+
}
|
|
133
|
+
export function workspacePath(root, file, create = false, resource = false) {
|
|
134
|
+
if (typeof file !== 'string' || !file || (!resource && !/\.md$/i.test(file)) || file.includes('\0')) throw new DocumentError('需要目录内的 .md 相对路径。');
|
|
135
|
+
const path = resolve(root, file), prefix = realpathSync(root) + sep;
|
|
136
|
+
if (!path.startsWith(resolve(root) + sep)) throw new DocumentError('路径超出当前目录。', 403);
|
|
137
|
+
let check = path;
|
|
138
|
+
while (!existsSync(check)) check = dirname(check);
|
|
139
|
+
const actual = realpathSync(check);
|
|
140
|
+
if (actual !== realpathSync(root) && !actual.startsWith(prefix)) throw new DocumentError('路径指向当前目录之外。', 403);
|
|
141
|
+
if (!create && !existsSync(path)) throw new DocumentError('文档不存在。', 404);
|
|
142
|
+
return path;
|
|
143
|
+
}
|