|
| 1 | +// Package skills discovers local agent skills (SKILL.md files) so the chat |
| 2 | +// TUI can expose them as slash commands. |
| 3 | +package skills |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// Skill is a parsed SKILL.md file. |
| 14 | +type Skill struct { |
| 15 | + // Name is the skill's frontmatter name (falls back to the directory name). |
| 16 | + Name string |
| 17 | + // Description is the frontmatter description (may be empty). |
| 18 | + Description string |
| 19 | + // Body is the markdown content after the frontmatter. |
| 20 | + Body string |
| 21 | + // Path is the absolute path of the SKILL.md file. |
| 22 | + Path string |
| 23 | +} |
| 24 | + |
| 25 | +// Command returns the slash command that invokes the skill. |
| 26 | +func (s Skill) Command() string { |
| 27 | + return "/" + s.Name |
| 28 | +} |
| 29 | + |
| 30 | +// skillsSubdir is the canonical skills directory relative to a base directory. |
| 31 | +var skillsSubdir = filepath.Join(".agents", "skills") |
| 32 | + |
| 33 | +// validName matches skill names that are safe to use as slash commands. |
| 34 | +var validName = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`) |
| 35 | + |
| 36 | +// Discover loads skills from the project (working directory) and the user's |
| 37 | +// home directory. Project skills shadow home skills with the same name. |
| 38 | +// Results are sorted by name. Discovery errors are silently skipped — a |
| 39 | +// malformed skill must not break the chat TUI. |
| 40 | +func Discover() []Skill { |
| 41 | + var bases []string |
| 42 | + if cwd, err := os.Getwd(); err == nil { |
| 43 | + bases = append(bases, cwd) |
| 44 | + } |
| 45 | + if home, err := os.UserHomeDir(); err == nil { |
| 46 | + bases = append(bases, home) |
| 47 | + } |
| 48 | + return discoverFrom(bases) |
| 49 | +} |
| 50 | + |
| 51 | +// discoverFrom loads skills from each base directory in order. Earlier bases |
| 52 | +// shadow later ones on name collisions. |
| 53 | +func discoverFrom(bases []string) []Skill { |
| 54 | + seen := make(map[string]bool) |
| 55 | + var result []Skill |
| 56 | + |
| 57 | + for _, base := range bases { |
| 58 | + dir := filepath.Join(base, skillsSubdir) |
| 59 | + entries, err := os.ReadDir(dir) |
| 60 | + if err != nil { |
| 61 | + continue |
| 62 | + } |
| 63 | + for _, entry := range entries { |
| 64 | + // Allow symlinked skill directories (install-skill creates them). |
| 65 | + info, err := os.Stat(filepath.Join(dir, entry.Name())) |
| 66 | + if err != nil || !info.IsDir() { |
| 67 | + continue |
| 68 | + } |
| 69 | + path := filepath.Join(dir, entry.Name(), "SKILL.md") |
| 70 | + raw, err := os.ReadFile(path) |
| 71 | + if err != nil { |
| 72 | + continue |
| 73 | + } |
| 74 | + skill := Parse(string(raw), entry.Name()) |
| 75 | + skill.Path = path |
| 76 | + if !validName.MatchString(skill.Name) || seen[skill.Name] { |
| 77 | + continue |
| 78 | + } |
| 79 | + seen[skill.Name] = true |
| 80 | + result = append(result, skill) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name }) |
| 85 | + return result |
| 86 | +} |
| 87 | + |
| 88 | +// Parse extracts the frontmatter name/description and the body from SKILL.md |
| 89 | +// content. fallbackName is used when the frontmatter has no name. |
| 90 | +func Parse(content string, fallbackName string) Skill { |
| 91 | + skill := Skill{Name: fallbackName, Body: strings.TrimSpace(content)} |
| 92 | + |
| 93 | + normalized := strings.ReplaceAll(content, "\r\n", "\n") |
| 94 | + if !strings.HasPrefix(normalized, "---\n") { |
| 95 | + return skill |
| 96 | + } |
| 97 | + rest := normalized[len("---\n"):] |
| 98 | + end := strings.Index(rest, "\n---") |
| 99 | + if end < 0 { |
| 100 | + return skill |
| 101 | + } |
| 102 | + frontmatter := rest[:end] |
| 103 | + body := rest[end+len("\n---"):] |
| 104 | + if i := strings.Index(body, "\n"); i >= 0 { |
| 105 | + body = body[i+1:] |
| 106 | + } else { |
| 107 | + body = "" |
| 108 | + } |
| 109 | + skill.Body = strings.TrimSpace(body) |
| 110 | + |
| 111 | + for _, line := range strings.Split(frontmatter, "\n") { |
| 112 | + key, value, ok := strings.Cut(line, ":") |
| 113 | + if !ok { |
| 114 | + continue |
| 115 | + } |
| 116 | + value = strings.TrimSpace(value) |
| 117 | + value = strings.Trim(value, `"'`) |
| 118 | + switch strings.TrimSpace(key) { |
| 119 | + case "name": |
| 120 | + if value != "" { |
| 121 | + skill.Name = value |
| 122 | + } |
| 123 | + case "description": |
| 124 | + skill.Description = value |
| 125 | + } |
| 126 | + } |
| 127 | + return skill |
| 128 | +} |
0 commit comments