|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/1Panel-dev/1Panel/agent/app/dto" |
| 10 | + "github.com/1Panel-dev/1Panel/agent/utils/cmd" |
| 11 | +) |
| 12 | + |
| 13 | +type hermesSkillsListEntry struct { |
| 14 | + Name string `json:"name"` |
| 15 | + Description string `json:"description"` |
| 16 | + Category string `json:"category"` |
| 17 | +} |
| 18 | + |
| 19 | +type hermesSkillsListPayload struct { |
| 20 | + Skills []hermesSkillsListEntry `json:"skills"` |
| 21 | +} |
| 22 | + |
| 23 | +func listHermesSkills(containerName string) ([]dto.AgentSkillItem, error) { |
| 24 | + output, err := runHermesSkillsCommandWithStdout(2*time.Minute, containerName, "list", "--source", "all") |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + items, err := parseHermesSkillsListOutput(output) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + metadata, err := readHermesSkillsListMetadata(containerName) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + return mergeHermesSkillsWithMetadata(items, metadata), nil |
| 37 | +} |
| 38 | + |
| 39 | +func searchHermesSkills(containerName, source, keyword string) ([]dto.AgentSkillSearchItem, error) { |
| 40 | + if source != "official" && source != "skills-sh" { |
| 41 | + return nil, fmt.Errorf("unsupported hermes skill source: %s", source) |
| 42 | + } |
| 43 | + output, err := runHermesSkillsCommandWithStdout( |
| 44 | + 2*time.Minute, |
| 45 | + containerName, |
| 46 | + "search", |
| 47 | + keyword, |
| 48 | + "--source", |
| 49 | + source, |
| 50 | + "--limit", |
| 51 | + "20", |
| 52 | + ) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + return parseHermesSkillSearchOutput(output) |
| 57 | +} |
| 58 | + |
| 59 | +func runHermesSkillsCommandWithStdout(timeout time.Duration, containerName string, hermesArgs ...string) (string, error) { |
| 60 | + args := []string{"exec", "-e", "COLUMNS=240", "-u", "hermes", containerName, "hermes", "skills"} |
| 61 | + args = append(args, hermesArgs...) |
| 62 | + return cmd.NewCommandMgr(cmd.WithTimeout(timeout)).RunWithStdout("docker", args...) |
| 63 | +} |
| 64 | + |
| 65 | +func readHermesSkillsListMetadata(containerName string) (map[string]hermesSkillsListEntry, error) { |
| 66 | + output, err := cmd.NewCommandMgr(cmd.WithTimeout(2*time.Minute)).RunWithStdout( |
| 67 | + "docker", |
| 68 | + "exec", |
| 69 | + "-u", |
| 70 | + "hermes", |
| 71 | + containerName, |
| 72 | + "python", |
| 73 | + "-c", |
| 74 | + "import sys; sys.path.insert(0, '/opt/hermes'); from tools.skills_tool import skills_list; print(skills_list())", |
| 75 | + ) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + return parseHermesSkillsListMetadataOutput(output) |
| 80 | +} |
| 81 | + |
| 82 | +func parseHermesSkillsListOutput(output string) ([]dto.AgentSkillItem, error) { |
| 83 | + headers, rows, err := parseHermesTableOutput(output) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + items := make([]dto.AgentSkillItem, 0, len(rows)) |
| 88 | + for _, row := range rows { |
| 89 | + item := dto.AgentSkillItem{ |
| 90 | + Name: row[headers["Name"]], |
| 91 | + Category: row[headers["Category"]], |
| 92 | + Source: row[headers["Source"]], |
| 93 | + Trust: row[headers["Trust"]], |
| 94 | + Uninstallable: row[headers["Source"]] != "builtin" && row[headers["Source"]] != "local", |
| 95 | + } |
| 96 | + items = append(items, item) |
| 97 | + } |
| 98 | + return items, nil |
| 99 | +} |
| 100 | + |
| 101 | +func parseHermesSkillsListMetadataOutput(output string) (map[string]hermesSkillsListEntry, error) { |
| 102 | + if strings.TrimSpace(output) == "" { |
| 103 | + return map[string]hermesSkillsListEntry{}, nil |
| 104 | + } |
| 105 | + var payload hermesSkillsListPayload |
| 106 | + if err := json.Unmarshal([]byte(output), &payload); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + metadata := make(map[string]hermesSkillsListEntry, len(payload.Skills)) |
| 110 | + for _, skill := range payload.Skills { |
| 111 | + if skill.Name == "" { |
| 112 | + continue |
| 113 | + } |
| 114 | + metadata[skill.Name] = skill |
| 115 | + } |
| 116 | + return metadata, nil |
| 117 | +} |
| 118 | + |
| 119 | +func mergeHermesSkillsWithMetadata(items []dto.AgentSkillItem, metadata map[string]hermesSkillsListEntry) []dto.AgentSkillItem { |
| 120 | + for i := range items { |
| 121 | + entry, ok := metadata[items[i].Name] |
| 122 | + if !ok { |
| 123 | + continue |
| 124 | + } |
| 125 | + items[i].Description = entry.Description |
| 126 | + if items[i].Category == "" && entry.Category != "" { |
| 127 | + items[i].Category = entry.Category |
| 128 | + } |
| 129 | + } |
| 130 | + return items |
| 131 | +} |
| 132 | + |
| 133 | +func parseHermesSkillSearchOutput(output string) ([]dto.AgentSkillSearchItem, error) { |
| 134 | + if strings.Contains(output, "No skills found matching your query.") { |
| 135 | + return []dto.AgentSkillSearchItem{}, nil |
| 136 | + } |
| 137 | + headers, rows, err := parseHermesTableOutput(output) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + items := make([]dto.AgentSkillSearchItem, 0, len(rows)) |
| 142 | + for _, row := range rows { |
| 143 | + identifier := row[headers["Identifier"]] |
| 144 | + items = append(items, dto.AgentSkillSearchItem{ |
| 145 | + Slug: identifier, |
| 146 | + Identifier: identifier, |
| 147 | + Name: row[headers["Name"]], |
| 148 | + Description: row[headers["Description"]], |
| 149 | + Source: row[headers["Source"]], |
| 150 | + Trust: row[headers["Trust"]], |
| 151 | + }) |
| 152 | + } |
| 153 | + return items, nil |
| 154 | +} |
| 155 | + |
| 156 | +func parseHermesTableOutput(output string) (map[string]int, [][]string, error) { |
| 157 | + lines := strings.Split(strings.TrimSpace(ansiEscapePattern.ReplaceAllString(output, "")), "\n") |
| 158 | + var headers []string |
| 159 | + rows := make([][]string, 0) |
| 160 | + var current []string |
| 161 | + |
| 162 | + for _, rawLine := range lines { |
| 163 | + line := strings.TrimSpace(rawLine) |
| 164 | + if (!strings.HasPrefix(line, "│") || !strings.HasSuffix(line, "│")) && |
| 165 | + (!strings.HasPrefix(line, "┃") || !strings.HasSuffix(line, "┃")) { |
| 166 | + continue |
| 167 | + } |
| 168 | + line = strings.ReplaceAll(line, "┃", "│") |
| 169 | + parts := strings.Split(line, "│") |
| 170 | + if len(parts) < 3 { |
| 171 | + continue |
| 172 | + } |
| 173 | + cols := make([]string, 0, len(parts)-2) |
| 174 | + for _, part := range parts[1 : len(parts)-1] { |
| 175 | + cols = append(cols, strings.TrimSpace(part)) |
| 176 | + } |
| 177 | + if len(headers) == 0 { |
| 178 | + headers = cols |
| 179 | + continue |
| 180 | + } |
| 181 | + if cols[0] != "" { |
| 182 | + if current != nil { |
| 183 | + rows = append(rows, current) |
| 184 | + } |
| 185 | + current = cols |
| 186 | + continue |
| 187 | + } |
| 188 | + if current == nil { |
| 189 | + continue |
| 190 | + } |
| 191 | + for i := range cols { |
| 192 | + if cols[i] == "" { |
| 193 | + continue |
| 194 | + } |
| 195 | + if current[i] == "" { |
| 196 | + current[i] = cols[i] |
| 197 | + continue |
| 198 | + } |
| 199 | + current[i] = current[i] + " " + cols[i] |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if current != nil { |
| 204 | + rows = append(rows, current) |
| 205 | + } |
| 206 | + if len(headers) == 0 { |
| 207 | + return nil, nil, fmt.Errorf("hermes skills table not found") |
| 208 | + } |
| 209 | + |
| 210 | + headerIndex := make(map[string]int, len(headers)) |
| 211 | + for i, header := range headers { |
| 212 | + headerIndex[header] = i |
| 213 | + } |
| 214 | + return headerIndex, rows, nil |
| 215 | +} |
0 commit comments