okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-split-agent4 4b5cc08d1 (skills end to end, New skill button, agent skills folder, goal-status race fix) into f-split-int19ea30f6a

main
4.4 KiB74 linesraw
1/**
2 * Skills on disk, for the app and for the agent.
3 *
4 * The app keeps the skills a person writes in its own folder (`<data>/skills/<id>/SKILL.md`). The agent (the Harness) finds skills by scanning
5 * folders of `<name>/SKILL.md` files whose frontmatter has a kebab-case `name` and a `description`. So the app also writes the whole current
6 * list (bundled, written, added, edited, switched on or off) into one folder the Harness is told to scan (`materializeSkills`), every time
7 * anything changes. The Harness watches that folder, so a skill saved in the app is there for the next message without a restart.
8 */
9import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
10import { join } from 'node:path';
11
12/** The kebab-case name the Harness requires, from what a person typed ("Directory opener" -> "directory-opener"). */
13export function skillSlug(name) {
14  return String(name ?? '').toLowerCase().normalize('NFKD').replace(/[̀-ͯ]/g, '').replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60);
15}
16
17const yamlText = value => JSON.stringify(String(value ?? '').replace(/\s+/g, ' ').trim());
18
19/** `SKILL.md` text: the frontmatter the Harness reads, then the instructions. */
20export function skillFileText({ name, title = null, description, body, hidden = false }) {
21  return `---\nname: ${skillSlug(name)}\n${title ? `title: ${yamlText(title)}\n` : ''}description: ${yamlText(description || name)}\n${hidden ? 'disable-model-invocation: true\n' : ''}---\n\n${String(body ?? '').trim()}\n`;
22}
23
24/** Name, description and instructions out of a SKILL.md (frontmatter optional). */
25export function parseSkillFile(text) {
26  const raw = String(text ?? '').replace(/^/, '').replace(/\r\n/g, '\n');
27  const m = /^---\n([\s\S]*?)\n---\n?/.exec(raw);
28  if (!m) return { name: null, title: null, description: null, body: raw.trim() };
29  const field = key => {
30    const line = new RegExp(`^${key}:\\s*(.*)$`, 'mi').exec(m[1])?.[1]?.trim();
31    if (line === undefined) return null;
32    try { if (/^["']/.test(line)) return typeof JSON.parse(line.replace(/^'(.*)'$/, '"$1"')) === 'string' ? JSON.parse(line.replace(/^'(.*)'$/, '"$1"')) : line; } catch { /* plain scalar */ }
33    return line;
34  };
35  return { name: field('name'), title: field('title'), description: field('description'), body: raw.slice(m[0].length).trim() };
36}
37
38/** Where a skill written in the app lives, and writing it there. */
39export function writeCreatedSkill(root, { name, description, body }) {
40  const id = skillSlug(name);
41  if (!id) throw new Error('Give the skill a name with letters or numbers in it.');
42  const dir = join(root, id);
43  if (existsSync(dir)) throw new Error(`You already have a skill called "${id}". Choose another name, or edit the one you have.`);
44  mkdirSync(dir, { recursive: true });
45  writeFileSync(join(dir, 'SKILL.md'), skillFileText({ name: id, title: String(name).trim(), description, body }));
46  return id;
47}
48
49/**
50 * Make `root` hold exactly the skills the agent may use: one folder each, a SKILL.md with frontmatter. A skill switched off is written
51 * with `disable-model-invocation` (the agent does not offer it); folders of skills that are gone are removed. Only this folder is touched.
52 * @param {string} root
53 * @param {Array<{id: string, name: string, description?: string, body?: string, enabled?: boolean}>} skills
54 */
55export function materializeSkills(root, skills) {
56  mkdirSync(root, { recursive: true });
57  const keep = new Set();
58  for (const s of skills) {
59    const slug = skillSlug(s.id.replace(/^(?:ws|my)-/, '') || s.name);
60    if (!slug || !s.body?.trim()) continue;
61    let unique = slug, n = 2;
62    while (keep.has(unique)) unique = `${slug}-${n++}`;
63    keep.add(unique);
64    const dir = join(root, unique);
65    mkdirSync(dir, { recursive: true });
66    const text = skillFileText({ name: unique, description: `${s.name}. ${s.description ?? ''}`.trim(), body: s.body, hidden: s.enabled === false });
67    const file = join(dir, 'SKILL.md');
68    // Only a changed skill is rewritten: the agent watches this folder and treats every write as a change to its list.
69    let same = false; try { same = readFileSync(file, 'utf8') === text; } catch { /* not there yet */ }
70    if (!same) writeFileSync(file, text);
71  }
72  for (const entry of readdirSync(root, { withFileTypes: true })) if (entry.isDirectory() && !keep.has(entry.name)) rmSync(join(root, entry.name), { recursive: true, force: true });
73  return [...keep];
74}