// src/commands/skill.ts import { Command } from 'commander'; import { existsSync, readdirSync } from 'fs'; import { join, resolve, dirname } from 'path'; import { fileURLToPath } from 'url'; import { createRequire } from 'module'; import { readSkillName, registerAgentSkill, unregisterAgentSkill, } from '../lib/skill-host.js'; export { readSkillName } from '../lib/skill-host.js'; const require = createRequire(import.meta.url); const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); /** Resolve every bundled skill, independently of the active account's loadout. */ function resolveBuiltinSkills(): string[] { let root = join(__dirname, '..', '..', 'skills'); if (!existsSync(join(root, 'clawclaw', 'SKILL.md'))) { try { const pkgRoot = dirname(require.resolve('@myclaw163/clawclaw-cli/package.json')); root = join(pkgRoot, 'skills'); } catch { try { const pkgRoot = dirname(require.resolve('clawclaw-cli/package.json')); root = join(pkgRoot, 'skills'); } catch { throw new Error('Bundled ClawClaw skills directory not found'); } } } const paths = readdirSync(root, { withFileTypes: true }) .filter(entry => entry.isDirectory() && existsSync(join(root, entry.name, 'SKILL.md'))) .map(entry => join(root, entry.name)).sort(); if (!paths.length) throw new Error('No bundled ClawClaw skills found'); return paths; } export function createSkillCommand(): Command { const cmd = new Command('skill') .description('Install and manage agent skills'); // ─── install ─── cmd.command('install [path]') .description('Register a skill directory with the agent. Use --builtin for all bundled ClawClaw skills.') .option('--builtin', 'install the bundled startup, gameplay and creator skills shipped with this CLI') .option('-g, --global', 'install globally', true) .option('-y, --yes', 'skip confirmation prompts', true) .action(async (rawPath: string | undefined, opts: { builtin?: boolean; global?: boolean; yes?: boolean }) => { let paths: string[]; if (opts.builtin) { paths = resolveBuiltinSkills(); } else if (rawPath) { paths = [resolve(rawPath)]; } else { console.error(JSON.stringify({ error: 'missing_path', message: 'Specify a path or use --builtin for the bundled ClawClaw skills.' }, null, 2)); process.exit(1); } for (const absPath of paths) { if (!existsSync(absPath)) { console.error(JSON.stringify({ error: 'skill_not_found', message: `Skill directory not found: ${absPath}` }, null, 2)); process.exit(1); } const r = registerAgentSkill(absPath); if (!r.ok) { console.error(JSON.stringify({ error: 'skill_install_failed', message: r.error }, null, 2)); process.exit(1); } } }); // ─── uninstall ─── cmd.command('uninstall ') .description('Unregister a skill from the agent (wraps skills remove)') .option('-g, --global', 'remove from global scope', true) .option('-y, --yes', 'skip confirmation prompts', true) .action(async (rawPath: string, opts: { global?: boolean; yes?: boolean }) => { const absPath = resolve(rawPath); const name = readSkillName(absPath); if (!name) { console.error(JSON.stringify({ error: 'skill_name_missing', message: `No valid SKILL.md with a name found in ${absPath}` }, null, 2)); process.exit(1); } const r = unregisterAgentSkill(name); if (!r.ok) { console.error(JSON.stringify({ error: 'skill_uninstall_failed', message: r.error }, null, 2)); process.exit(1); } }); return cmd; }