import { promises as fs } from 'node:fs'; import * as path from 'node:path'; import { moxxyPath } from '@moxxy/sdk/server'; import { skillFrontmatterSchema, asSkillId, type Skill, type SkillScope } from '@moxxy/sdk'; import { parseSkillFile } from './parse.js'; import type { Logger } from '../logger.js'; export interface SkillLoadOptions { readonly projectDir?: string; readonly userDir?: string; readonly pluginDirs?: ReadonlyArray; readonly builtinDir?: string; readonly logger?: Logger; } export interface DiscoveredSkill extends Skill { readonly scope: SkillScope; } export async function discoverSkills(opts: SkillLoadOptions = {}): Promise> { const sources: Array<{ dir: string; scope: SkillScope }> = []; if (opts.builtinDir) sources.push({ dir: opts.builtinDir, scope: 'builtin' }); for (const dir of opts.pluginDirs ?? []) sources.push({ dir, scope: 'plugin' }); const userDir = opts.userDir ?? defaultUserSkillsDir(); sources.push({ dir: userDir, scope: 'user' }); if (opts.projectDir) sources.push({ dir: opts.projectDir, scope: 'project' }); const seenNames = new Map(); for (const source of sources) { const skills = await loadDir(source.dir, source.scope, opts.logger); for (const skill of skills) seenNames.set(skill.frontmatter.name, skill); } return [...seenNames.values()]; } /** Cap directory recursion so a pathologically deep (or hostile) skills tree can't blow the stack at boot. */ const MAX_SKILL_DIR_DEPTH = 8; async function loadDir( dir: string, scope: SkillScope, logger?: Logger, depth = 0, ): Promise> { let entries: import('node:fs').Dirent[]; try { entries = await fs.readdir(dir, { withFileTypes: true }); } catch { return []; } const out: DiscoveredSkill[] = []; for (const entry of entries) { if (entry.isDirectory()) { if (depth >= MAX_SKILL_DIR_DEPTH) { logger?.warn('skill: directory too deep, not recursing', { path: path.join(dir, entry.name), maxDepth: MAX_SKILL_DIR_DEPTH, }); continue; } out.push(...(await loadDir(path.join(dir, entry.name), scope, logger, depth + 1))); continue; } if (!entry.isFile() || !entry.name.endsWith('.md')) continue; const full = path.join(dir, entry.name); let raw: string; try { raw = await fs.readFile(full, 'utf8'); } catch (err) { // One unreadable .md (permission, race-deleted, symlink loop) must not // abort discovery of every remaining skill across every source — degrade // to skipping just this file, like the invalid-frontmatter case below. logger?.warn('skill: unreadable file, skipping', { path: full, error: err instanceof Error ? err.message : String(err), }); continue; } const { frontmatter, body } = parseSkillFile(raw); const parsed = skillFrontmatterSchema.safeParse(frontmatter); if (!parsed.success) { logger?.warn('skill: invalid frontmatter, skipping', { path: full, issues: parsed.error.issues }); continue; } // Auto-derive trigger fallbacks from the skill name when the // frontmatter `triggers:` field is missing/empty. Without this, // user-authored skills (or model-synthesized ones that skipped the // field) have no triggers at all — they're effectively invisible // to the trigger-match router and confusing in /skills listings. // We split the kebab-case name into words and also keep the full // slug as a literal trigger. const fm = parsed.data; const finalTriggers = fm.triggers && fm.triggers.length > 0 ? fm.triggers : deriveTriggers(fm.name); out.push({ id: asSkillId(`${scope}/${fm.name}`), path: full, scope, frontmatter: { ...fm, triggers: finalTriggers }, body: body.trimEnd(), }); } return out; } /** * Derive trigger-like phrases from a kebab-case skill name. Best-effort * fallback for skills whose frontmatter omits the `triggers:` field — * gives the router and `/skills` listing *something* to show. */ function deriveTriggers(name: string): ReadonlyArray { const parts = name.split('-').filter((p) => p.length > 1); const out = new Set(); out.add(name.replace(/-/g, ' ')); for (const p of parts) out.add(p); return [...out]; } export function defaultUserSkillsDir(): string { return moxxyPath('skills'); } export function defaultProjectSkillsDir(cwd: string): string { return path.join(cwd, '.moxxy', 'skills'); }