包文件→ .shtukbase/packages/blueprint-skilled-agent/lib/skills.ts
lib/skills.ts
99 行, 5.5 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Skills of a project and what an agent gets from them. A skill is `skills/<id>/SKILL.md` in the Agent Skills
- // format (name = id, description = when to use it, the body = the instruction) plus the page fields title and use.
- // An agent page with `skills:` (a list of ids or `all`) gets them through its instruction file
- // `instructions: /agents/prompts/<agent>.md`, which is built here: the page body, then every skill in full.
- // The active skills are also mirrored to `.claude/skills/<id>/SKILL.md` for Claude Code sessions in the project.
- import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- export type Skill = { id: string; name: string; description: string; body: string; active: boolean };
- export type Plan = { files: Map<string, string>; mirrors: string[]; problems: { at: string; what: string; fix: string }[] };
- export const MARK = '<!-- built by blueprint-skilled-agent; edit skills/ and the agent page, then run: shtuk run blueprint-skilled-agent build -->';
- function page(file: string): { fm: any; body: string } {
- const t = readFileSync(file, 'utf8');
- const m = /^---\n([\s\S]*?)\n---\n?/.exec(t);
- if (!m) return { fm: {}, body: t };
- let fm: any = {};
- try {
- fm = (Bun.YAML.parse(m[1]) as any) ?? {};
- } catch {
- fm = { __bad: true };
- }
- return { fm, body: t.slice(m[0].length) };
- }
- const text = (v: unknown): string => (v == null ? '' : String(v)).trim();
- export function skillsOf(root: string, problems: Plan['problems']): Skill[] {
- const dir = join(root, 'skills');
- if (!existsSync(dir)) return [];
- const out: Skill[] = [];
- for (const id of readdirSync(dir).sort()) {
- const f = join(dir, id, 'SKILL.md');
- if (!statSync(join(dir, id)).isDirectory()) continue;
- const at = `skills/${id}/SKILL.md`;
- if (!existsSync(f)) {
- problems.push({ at: `skills/${id}`, what: 'the skill folder has no SKILL.md', fix: 'add SKILL.md or remove the folder' });
- continue;
- }
- const { fm, body } = page(f);
- if (fm.__bad) {
- problems.push({ at, what: 'the head cannot be read as YAML', fix: 'fix the head' });
- continue;
- }
- const name = text(fm.name), description = text(fm.description);
- if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(id) || id.length > 64)
- problems.push({ at, what: `the folder name "${id}" is not a skill name`, fix: 'lowercase Latin letters, digits and a hyphen, up to 64 characters' });
- if (name !== id) problems.push({ at, what: `name "${name || '—'}" does not match the folder "${id}"`, fix: `set name: ${id}` });
- if (!description) problems.push({ at, what: 'no description — it is not said when to apply the skill', fix: 'write description: "Use when …"' });
- else if (description.length > 1024) problems.push({ at, what: 'description is longer than 1024 characters', fix: 'shorten it' });
- if (!body.trim()) problems.push({ at, what: 'no instruction — the page body is empty', fix: 'write what to do' });
- out.push({ id, name, description, body: body.trim(), active: text(fm.use) !== 'paused' });
- }
- return out;
- }
- const q = (s: string) => `"${s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, ' ')}"`;
- /** What the build writes: the prompt of every agent with skills, the Claude Code mirror of the active skills. */
- export function plan(root: string): Plan {
- const problems: Plan['problems'] = [];
- const skills = skillsOf(root, problems);
- const byId = new Map(skills.map((s) => [s.id, s]));
- const active = skills.filter((s) => s.active);
- const files = new Map<string, string>();
- const agents = join(root, 'agents');
- for (const f of existsSync(agents) ? readdirSync(agents).filter((x) => x.endsWith('.md')).sort() : []) {
- const { fm, body } = page(join(agents, f));
- if (fm.skills == null) continue;
- const at = `agents/${f}`;
- const want = fm.skills === 'all' ? active : (Array.isArray(fm.skills) ? fm.skills : [fm.skills]).map((x: unknown) => {
- const id = text(x).replace(/^\/?skills\//, '').replace(/\/(SKILL\.md)?$/, '');
- const s = byId.get(id);
- if (!s) problems.push({ at, what: `the skill "${id}" is not in /skills`, fix: 'fix the skills of the agent or add the skill' });
- else if (!s.active) problems.push({ at, what: `the skill "${id}" is switched off (use: paused)`, fix: 'remove it from the skills of the agent or switch it on' });
- return s && s.active ? s : null;
- }).filter(Boolean) as Skill[];
- const prompt = `/agents/prompts/${f}`;
- if (text(fm.instructions) !== prompt) {
- problems.push({ at, what: `the instruction of the agent is not ${prompt}: the skills will not reach it`, fix: `set instructions: ${prompt}` });
- continue;
- }
- const parts = [MARK, '', body.trim()];
- if (want.length) {
- parts.push('', '## Skills', '', 'Below are your skills. Each one says when to apply it. Apply all that fit the task.');
- for (const s of want) parts.push('', `### Skill ${s.name}`, '', `Use when: ${s.description}`, '', s.body);
- }
- files.set(prompt.slice(1), parts.join('\n') + '\n');
- }
- for (const s of active)
- files.set(`.claude/skills/${s.id}/SKILL.md`, `---\nname: ${s.id}\ndescription: ${q(s.description)}\n---\n\n${MARK}\n\n${s.body}\n`);
- const mirrorDir = join(root, '.claude', 'skills');
- const mirrors = existsSync(mirrorDir) ? readdirSync(mirrorDir).filter((id) => {
- const f = join(mirrorDir, id, 'SKILL.md');
- return existsSync(f) && readFileSync(f, 'utf8').includes(MARK) && !files.has(`.claude/skills/${id}/SKILL.md`);
- }) : [];
- return { files, mirrors, problems };
- }