Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions packages/agent-runtime/src/tools/handlers/tool/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
{
Expand Down
57 changes: 39 additions & 18 deletions sdk/src/skills/load-skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,71 @@ function loadSkillFromFile(
})
}

/**
* Discovers skills from a skills directory.
* Looks for <skillsDir>/<skill-name>/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)
} catch {
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
} catch {
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
Expand Down Expand Up @@ -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
Expand Down