diff --git a/packages/agent-runtime/src/tools/handlers/tool/skill.ts b/packages/agent-runtime/src/tools/handlers/tool/skill.ts index 6e8748eaad..5e03864292 100644 --- a/packages/agent-runtime/src/tools/handlers/tool/skill.ts +++ b/packages/agent-runtime/src/tools/handlers/tool/skill.ts @@ -54,19 +54,40 @@ async function loadSkillFromDisk( : []), ] - for (const skillsDir of skillsDirs) { - const skillDir = path.join(skillsDir, skillName) - const skillFilePath = path.join(skillDir, SKILL_FILE_NAME) - + // Walk each skills root (including nested category folders) looking for a + // directory whose name matches skillName and contains a SKILL.md file. + const findSkillFile = (root: string, target: string, depth: number): string | null => { + if (depth > 6) return null + let entries: string[] try { - // Check if the skill directory and file exist - const stat = fs.statSync(skillDir) + entries = fs.readdirSync(root) + } catch { + return null + } + for (const entry of entries) { + if (entry.startsWith('.') ) continue + const full = path.join(root, entry) + let stat + try { stat = fs.statSync(full) } catch { continue } if (!stat.isDirectory()) continue + if (entry === target) { + const candidate = path.join(full, SKILL_FILE_NAME) + try { + if (fs.statSync(candidate).isFile()) return candidate + } catch { /* fall through */ } + } + const found = findSkillFile(full, target, depth + 1) + if (found) return found + } + return null + } - fs.statSync(skillFilePath) // Will throw if file doesn't exist + for (const skillsDir of skillsDirs) { + const skillFilePath = findSkillFile(skillsDir, skillName, 0) + if (!skillFilePath) continue + const skillDir = path.dirname(skillFilePath) - // Same parser as session-start discovery, so a skill created mid-session - // is held to the same rules (YAML-only frontmatter, name matches dir). + try { const skill = parseSkillFileContent( fs.readFileSync(skillFilePath, 'utf8'), { diff --git a/sdk/src/skills/load-skills.ts b/sdk/src/skills/load-skills.ts index d61f2d3d15..0bb1de3d42 100644 --- a/sdk/src/skills/load-skills.ts +++ b/sdk/src/skills/load-skills.ts @@ -41,16 +41,24 @@ function loadSkillFromFile( }) } -/** - * Discovers skills from a skills directory. - * Looks for //SKILL.md files. - */ +const SKIPPED_DIR_NAMES = new Set([ + '.git', + 'node_modules', + '.hg', + '.svn', +]) + +const MAX_SKILL_NESTING_DEPTH = 6 + function discoverSkillsFromDirectory( skillsDir: string, verbose: boolean, + depth: number = 0, ): SkillsMap { const skills: SkillsMap = {} + if (depth > MAX_SKILL_NESTING_DEPTH) return skills + let entries: string[] try { entries = fs.readdirSync(skillsDir) @@ -58,10 +66,13 @@ function discoverSkillsFromDirectory( return skills } + entries = [...entries].sort() + for (const entry of entries) { + if (entry.startsWith('.') || SKIPPED_DIR_NAMES.has(entry)) continue + const skillDir = path.join(skillsDir, entry) - // Skip non-directories and invalid skill names try { const stat = fs.statSync(skillDir) if (!stat.isDirectory()) continue @@ -69,26 +80,32 @@ function discoverSkillsFromDirectory( continue } - if (!isValidSkillName(entry)) { - if (verbose) { - console.warn(`Skipping invalid skill directory name: ${entry}`) - } - continue - } - const skillFilePath = path.join(skillDir, SKILL_FILE_NAME) - // Check if SKILL.md exists + let hasSkillFile = false try { - fs.statSync(skillFilePath) + if (fs.statSync(skillFilePath).isFile()) hasSkillFile = true } catch { - continue + hasSkillFile = false } - const skill = loadSkillFromFile(skillDir, skillFilePath, verbose) - if (skill) { - skills[skill.name] = skill + if (hasSkillFile) { + if (!isValidSkillName(entry)) { + if (verbose) { + console.warn(`Skipping invalid skill directory name: ${entry}`) + } + continue + } + + const skill = loadSkillFromFile(skillDir, skillFilePath, verbose) + if (skill) { + skills[skill.name] = skill + } + continue } + + const nested = discoverSkillsFromDirectory(skillDir, verbose, depth + 1) + Object.assign(skills, nested) } return skills @@ -177,6 +194,10 @@ export type LoadSkillsOptions = { * - `.agents/skills/my-skill/SKILL.md` * - `.claude/skills/my-skill/SKILL.md` * + * Skills may also be nested in category/organizational subdirectories; the + * directory containing SKILL.md determines the skill's identity: + * - `.agents/skills/category/my-skill/SKILL.md` + * * @param options.cwd - Working directory for project skills * @param options.skillsPath - Optional path to a specific skills directory * @param options.verbose - Whether to log errors during loading