Files
openmotion/shared/library-skill-catalog.ts
huihui-crypto 492540ffc2 feat(mcp): split library Skills into openmotion_list_skills / openmotion_get_skill (#648)
* feat(mcp): split library Skills into openmotion_list_skills / openmotion_get_skill

Skills no longer ride on the template tools. list_templates returns
templates only (no kind=skill rows, no skillTotal) and get_template rejects
Skill IDs with a pointer to openmotion_get_skill. The new tools reuse the
same repository/hosted readers and content-addressed object store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(studio): point Skill copy prompts and MCP examples to openmotion_get_skill

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: register Skill tools in the MCP handbook and update tool-count tests

- Templates capability lists openmotion_list_skills / openmotion_get_skill;
  technique studio scenarios compose openmotion_get_skill.
- Tool counts +2; the WebMCP tracking integration test flushes between calls
  so 23 tools x 3 events stay within the bounded browser queue (64).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(mcp): ship Skill tools in CMS/package mode, add handbook capability and refresh counts

- CMS mode reads the library/skills shipped with the application and never
  waits on the template publication, so list_skills/get_skill work offline
  and in the packaged phase-one ZIP.
- Phase-one package E2E calls openmotion_list_skills and openmotion_get_skill.
- Handbook gets a Skill-discovery capability (zh-CN copy included).
- Growth teardown Skill reads its scripts via openmotion_get_skill.
- Public counts: 18 MCP names (17 capabilities) and 23 WebMCP page tools.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: zhanghuihua <zhanghuihua@huihuideMacBook-Pro.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: zhanghuihua <zhanghuihua@Mac.lan>
2026-11-19 17:20:49 +08:00

59 lines
3.9 KiB
TypeScript

import { readFile, readdir } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
export type LibrarySkill = { id: string; kind: 'skill'; title: string; description: string; tags: string[]; repository?: string; license?: string; instructions: string; files: string[]; objects?: SkillObject[]; localSource?: string };
export type SkillObject = { path: string; bytes: number; sha256: string };
const root = fileURLToPath(new URL('../', import.meta.url));
export const skillNextStep = 'Follow SKILL.md. When a new project is needed, use openmotion_create_project with projectType=natural_language. Skills are instructions, not copyable projects.';
export const skillUsage = 'This ID is a library Skill, not a template. Read it with openmotion_get_skill(skillId). ' + skillNextStep;
// Cards are build-time Studio sources with media imports, not a Node module.
function fields(source: string): Record<string, string> {
return Object.fromEntries([...source.matchAll(/\b(id|title|useWhen|repository|license|category):\s*'((?:\\.|[^'\\])*)'/g)].map(m => [m[1], m[2].replace(/\\'/g, "'").replace(/\\\\/g, '\\')]));
}
function frontmatter(content: string, key: string): string {
const header = /^---\r?\n([\s\S]*?)\r?\n---/.exec(content)?.[1] ?? '';
const lines = header.split('\n');
const index = lines.findIndex(line => line.startsWith(key + ':'));
let value = index < 0 ? '' : lines[index].slice(key.length + 1).trim();
if (/^[>|][-+]?$/.test(value)) {
value = lines.slice(index + 1).join('\n').split(/\n(?=\S)/)[0].trim();
}
return value.replace(/^[>|][-+]?\s*/, '').replace(/\s*\n\s*/g, ' ').replace(/^(['"])(.*)\1$/, '$2');
}
export async function loadLibrarySkills(appRoot = root, local = true): Promise<LibrarySkill[]> {
const directory = path.join(appRoot, 'library/skills');
let entries;
try { entries = await readdir(directory, { withFileTypes: true }); } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return []; throw error; }
let cards = '';
try { cards = await readFile(path.join(appRoot, 'studio/src/motion/skillCatalog.ts'), 'utf8'); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
const metadata = new Map([...cards.matchAll(/\{ id: '[^\n]+/g)].map(m => { const f = fields(m[0].split('cover:')[0]); return [f.id, f]; }));
const result: LibrarySkill[] = [];
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
if (!entry.isDirectory()) continue;
const base = path.join(directory, entry.name);
let instructions: string;
try { instructions = await readFile(path.join(base, 'SKILL.md'), 'utf8'); } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') continue; throw error; }
const files: string[] = [];
const walk = async (relative: string) => {
for (const child of await readdir(path.join(base, relative), { withFileTypes: true })) {
const name = relative ? relative + '/' + child.name : child.name;
if (child.isDirectory()) await walk(name);
else if (child.isFile()) files.push(name);
}
};
await walk('');
const card = metadata.get(entry.name) ?? {};
result.push({ id: entry.name, kind: 'skill', title: card.title || frontmatter(instructions, 'name') || entry.name,
description: card.useWhen || frontmatter(instructions, 'description'), tags: card.category ? [card.category] : [],
...(card.repository ? { repository: card.repository } : {}), ...(card.license ? { license: card.license } : {}),
instructions, files: files.sort(), ...(local ? { localSource: base } : {}) });
}
return result;
}
export function assertSkillIds(skills: LibrarySkill[], templateIds: string[]) {
const ids = new Set(templateIds);
for (const skill of skills) { if (ids.has(skill.id)) throw new Error('Duplicate template/skill ID: ' + skill.id); ids.add(skill.id); }
}