|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/yeasy/ask/internal/config" |
| 11 | + "github.com/yeasy/ask/internal/skill" |
| 12 | +) |
| 13 | + |
| 14 | +// AvailableSkills represents the XML structure for agent prompt integration |
| 15 | +type AvailableSkills struct { |
| 16 | + XMLName xml.Name `xml:"available_skills"` |
| 17 | + Skills []SkillEntry `xml:"skill"` |
| 18 | +} |
| 19 | + |
| 20 | +// SkillEntry represents a single skill in the XML output |
| 21 | +type SkillEntry struct { |
| 22 | + Name string `xml:"name"` |
| 23 | + Description string `xml:"description"` |
| 24 | + Location string `xml:"location"` |
| 25 | +} |
| 26 | + |
| 27 | +var promptOutputFile string |
| 28 | + |
| 29 | +// promptCmd represents the prompt command |
| 30 | +var promptCmd = &cobra.Command{ |
| 31 | + Use: "prompt [paths...]", |
| 32 | + Short: "Generate XML skill listing for agent prompts", |
| 33 | + Long: `Generate <available_skills> XML block for agent system prompts. |
| 34 | +
|
| 35 | +This format is recommended for Anthropic's models and follows the |
| 36 | +Agent Skills specification at https://agentskills.io/specification. |
| 37 | +
|
| 38 | +If no paths are specified, scans installed skills in default locations. |
| 39 | +
|
| 40 | +Examples: |
| 41 | + ask skill prompt # Scan all installed skills |
| 42 | + ask skill prompt .agent/skills/pdf # Single skill |
| 43 | + ask skill prompt ./skills/a ./skills/b # Multiple skills`, |
| 44 | + Run: runPrompt, |
| 45 | +} |
| 46 | + |
| 47 | +func init() { |
| 48 | + promptCmd.Flags().StringVarP(&promptOutputFile, "output", "o", "", "Write XML to file instead of stdout") |
| 49 | +} |
| 50 | + |
| 51 | +func runPrompt(_ *cobra.Command, args []string) { |
| 52 | + var skillPaths []string |
| 53 | + |
| 54 | + if len(args) > 0 { |
| 55 | + // Use provided paths |
| 56 | + for _, arg := range args { |
| 57 | + absPath, err := filepath.Abs(arg) |
| 58 | + if err != nil { |
| 59 | + fmt.Fprintf(os.Stderr, "Error resolving path %s: %v\n", arg, err) |
| 60 | + continue |
| 61 | + } |
| 62 | + if skill.FindSkillMD(absPath) { |
| 63 | + skillPaths = append(skillPaths, absPath) |
| 64 | + } else { |
| 65 | + fmt.Fprintf(os.Stderr, "Warning: %s is not a valid skill (no SKILL.md found)\n", arg) |
| 66 | + } |
| 67 | + } |
| 68 | + } else { |
| 69 | + // Scan default locations |
| 70 | + skillPaths = discoverInstalledSkills() |
| 71 | + } |
| 72 | + |
| 73 | + if len(skillPaths) == 0 { |
| 74 | + fmt.Fprintln(os.Stderr, "No skills found.") |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + // Build XML structure |
| 79 | + availableSkills := AvailableSkills{} |
| 80 | + for _, path := range skillPaths { |
| 81 | + meta, err := skill.ParseSkillMD(path) |
| 82 | + if err != nil { |
| 83 | + fmt.Fprintf(os.Stderr, "Warning: failed to parse %s: %v\n", path, err) |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + entry := SkillEntry{ |
| 88 | + Name: meta.Name, |
| 89 | + Description: meta.Description, |
| 90 | + Location: filepath.Join(path, "SKILL.md"), |
| 91 | + } |
| 92 | + availableSkills.Skills = append(availableSkills.Skills, entry) |
| 93 | + } |
| 94 | + |
| 95 | + // Generate XML |
| 96 | + output, err := xml.MarshalIndent(availableSkills, "", " ") |
| 97 | + if err != nil { |
| 98 | + fmt.Fprintf(os.Stderr, "Error generating XML: %v\n", err) |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | + |
| 102 | + xmlContent := string(output) |
| 103 | + |
| 104 | + if promptOutputFile != "" { |
| 105 | + if err := os.WriteFile(promptOutputFile, []byte(xmlContent), 0644); err != nil { |
| 106 | + fmt.Fprintf(os.Stderr, "Error writing to %s: %v\n", promptOutputFile, err) |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + fmt.Printf("XML written to %s\n", promptOutputFile) |
| 110 | + } else { |
| 111 | + fmt.Println(xmlContent) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// discoverInstalledSkills finds all installed skills in known locations |
| 116 | +func discoverInstalledSkills() []string { |
| 117 | + var paths []string |
| 118 | + cwd, _ := os.Getwd() |
| 119 | + home, _ := os.UserHomeDir() |
| 120 | + |
| 121 | + // Locations to scan |
| 122 | + searchDirs := []string{ |
| 123 | + filepath.Join(cwd, config.DefaultSkillsDir), |
| 124 | + filepath.Join(cwd, "skills"), |
| 125 | + } |
| 126 | + |
| 127 | + // Add agent-specific directories |
| 128 | + for _, agentConfig := range config.SupportedAgents { |
| 129 | + searchDirs = append(searchDirs, filepath.Join(cwd, agentConfig.ProjectDir)) |
| 130 | + if home != "" { |
| 131 | + searchDirs = append(searchDirs, filepath.Join(home, agentConfig.GlobalDir)) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Add global skills directory |
| 136 | + if home != "" { |
| 137 | + searchDirs = append(searchDirs, filepath.Join(home, ".ask", "skills")) |
| 138 | + } |
| 139 | + |
| 140 | + // Deduplicate and scan |
| 141 | + seen := make(map[string]bool) |
| 142 | + for _, dir := range searchDirs { |
| 143 | + if seen[dir] { |
| 144 | + continue |
| 145 | + } |
| 146 | + seen[dir] = true |
| 147 | + |
| 148 | + entries, err := os.ReadDir(dir) |
| 149 | + if err != nil { |
| 150 | + continue |
| 151 | + } |
| 152 | + |
| 153 | + for _, entry := range entries { |
| 154 | + if !entry.IsDir() { |
| 155 | + continue |
| 156 | + } |
| 157 | + skillPath := filepath.Join(dir, entry.Name()) |
| 158 | + if skill.FindSkillMD(skillPath) { |
| 159 | + // Use absolute path for deduplication |
| 160 | + absPath, _ := filepath.Abs(skillPath) |
| 161 | + if !containsPath(paths, absPath) { |
| 162 | + paths = append(paths, absPath) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return paths |
| 169 | +} |
| 170 | + |
| 171 | +func containsPath(paths []string, target string) bool { |
| 172 | + for _, p := range paths { |
| 173 | + if p == target { |
| 174 | + return true |
| 175 | + } |
| 176 | + } |
| 177 | + return false |
| 178 | +} |
0 commit comments