Files
johannesandClaude Sonnet 5.5 df8d11d8cb feat: pin agent definitions next to skills
A source can install agents (agents/<name>.md files with a frontmatter
name) alongside or instead of skills.

- manifest: optional "agents" selection; lock: "agents" map
- add --agent / --all-agents, remove --agent, list shows the kind
- check/update diff agents like skills (files, not directories)
- dashboard lists and removes agents
- home-manager module: agentTargets (default .claude/agents) links <name>.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-28 13:42:45 +01:00

121 lines
2.9 KiB
Go

// Package skills finds skills inside a fetched source.
package skills
import (
"bufio"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
// Skill is a directory containing SKILL.md.
type Skill struct {
// Name comes from the SKILL.md frontmatter, falling back to the directory name.
// Names aren't unique: plugin monorepos ship the same skill in several places.
Name string
// Path is slash-separated and relative to the source root ("." for a repo
// that is itself one skill).
Path string
}
// Discover returns every skill under root in lexical walk order. Skills don't nest.
func Discover(root string) ([]Skill, error) {
var found []Skill
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if d.Name() == ".git" {
return filepath.SkipDir
}
skillFile := filepath.Join(path, "SKILL.md")
if _, err := os.Stat(skillFile); err != nil {
return nil
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
rel = filepath.ToSlash(rel)
name, err := frontmatterName(skillFile)
if err != nil {
return err
}
if name == "" {
if rel == "." {
return fmt.Errorf("SKILL.md at the repo root has no name in its frontmatter")
}
name = d.Name()
}
found = append(found, Skill{Name: name, Path: rel})
return filepath.SkipDir
})
return found, err
}
// DiscoverAgents returns every agent definition under root in lexical walk order: a
// .md file directly inside a directory named "agents" whose frontmatter has a name.
// Path is the file, slash-separated and relative to root. Files without a name
// (an agents/README.md) are not agents.
func DiscoverAgents(root string) ([]Skill, error) {
var found []Skill
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if d.Name() == ".git" {
return filepath.SkipDir
}
return nil
}
if filepath.Base(filepath.Dir(path)) != "agents" || filepath.Ext(path) != ".md" {
return nil
}
name, err := frontmatterName(path)
if err != nil {
return err
}
if name == "" {
return nil
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
found = append(found, Skill{Name: name, Path: filepath.ToSlash(rel)})
return nil
})
return found, err
}
// frontmatterName reads `name:` from the YAML frontmatter, or "" if absent.
func frontmatterName(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
sc := bufio.NewScanner(f)
if !sc.Scan() || strings.TrimSpace(sc.Text()) != "---" {
return "", sc.Err()
}
for sc.Scan() {
line := sc.Text()
if strings.TrimSpace(line) == "---" {
break
}
if v, ok := strings.CutPrefix(line, "name:"); ok {
return strings.Trim(strings.TrimSpace(v), `"'`), nil
}
}
return "", sc.Err()
}