|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +func qwenSettingsPath(home string) string { |
| 14 | + return filepath.Join(home, ".qwen", "settings.json") |
| 15 | +} |
| 16 | + |
| 17 | +func patchQwenConfig(home string, repoRoot string, _ config) (bool, error) { |
| 18 | + configPath := qwenSettingsPath(home) |
| 19 | + raw := map[string]interface{}{} |
| 20 | + data, err := os.ReadFile(configPath) |
| 21 | + if err != nil { |
| 22 | + if !errors.Is(err, fs.ErrNotExist) { |
| 23 | + return false, fmt.Errorf("read %s: %w", configPath, err) |
| 24 | + } |
| 25 | + } else if err := parseJSONConfig(configPath, data, &raw); err != nil { |
| 26 | + return false, fmt.Errorf("parse %s: %w", configPath, err) |
| 27 | + } |
| 28 | + |
| 29 | + skillsValue, skillsExists := raw["skills"] |
| 30 | + skills, skillsValid := skillsValue.(map[string]interface{}) |
| 31 | + if skillsExists && skillsValue != nil && !skillsValid { |
| 32 | + return false, fmt.Errorf("skills key in %s is not an object", configPath) |
| 33 | + } |
| 34 | + if !skillsExists || skillsValue == nil { |
| 35 | + skills = map[string]interface{}{} |
| 36 | + raw["skills"] = skills |
| 37 | + } |
| 38 | + |
| 39 | + target := filepath.Join(repoRoot, "skills") |
| 40 | + directoriesValue, directoriesExists := skills["directories"] |
| 41 | + directories, directoriesValid := directoriesValue.([]interface{}) |
| 42 | + if directoriesExists && directoriesValue != nil && !directoriesValid { |
| 43 | + return false, fmt.Errorf("skills.directories in %s is not an array", configPath) |
| 44 | + } |
| 45 | + for _, value := range directories { |
| 46 | + directory, ok := value.(string) |
| 47 | + if ok && filepath.Clean(expandPath(directory, home)) == filepath.Clean(target) { |
| 48 | + return false, nil |
| 49 | + } |
| 50 | + } |
| 51 | + skills["directories"] = append(directories, hermesExternalDirValue(home, target)) |
| 52 | + |
| 53 | + out, err := json.MarshalIndent(raw, "", " ") |
| 54 | + if err != nil { |
| 55 | + return false, fmt.Errorf("marshal %s: %w", configPath, err) |
| 56 | + } |
| 57 | + out = append(out, '\n') |
| 58 | + if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil { |
| 59 | + return false, fmt.Errorf("create %s: %w", filepath.Dir(configPath), err) |
| 60 | + } |
| 61 | + if err := os.WriteFile(configPath, out, 0o644); err != nil { |
| 62 | + return false, fmt.Errorf("write %s: %w", configPath, err) |
| 63 | + } |
| 64 | + return true, nil |
| 65 | +} |
| 66 | + |
| 67 | +func qwenHasSkillsDirectory(home string, target string) (bool, error) { |
| 68 | + configPath := qwenSettingsPath(home) |
| 69 | + data, err := os.ReadFile(configPath) |
| 70 | + if errors.Is(err, fs.ErrNotExist) { |
| 71 | + return false, nil |
| 72 | + } |
| 73 | + if err != nil { |
| 74 | + return false, fmt.Errorf("read %s: %w", configPath, err) |
| 75 | + } |
| 76 | + var raw map[string]interface{} |
| 77 | + if err := parseJSONConfig(configPath, data, &raw); err != nil { |
| 78 | + return false, fmt.Errorf("parse %s: %w", configPath, err) |
| 79 | + } |
| 80 | + skills, _ := raw["skills"].(map[string]interface{}) |
| 81 | + directories, _ := skills["directories"].([]interface{}) |
| 82 | + for _, value := range directories { |
| 83 | + directory, ok := value.(string) |
| 84 | + if ok && filepath.Clean(expandPath(directory, home)) == filepath.Clean(target) { |
| 85 | + return true, nil |
| 86 | + } |
| 87 | + } |
| 88 | + return false, nil |
| 89 | +} |
| 90 | + |
| 91 | +func inspectQwenAgent(agent agentConfig, expected map[string]string, agentsSkillRoot string, cfg config, home string) (agentReport, error) { |
| 92 | + report := agentReport{ |
| 93 | + Name: agent.Name, |
| 94 | + SkillRoot: agent.SkillRoot, |
| 95 | + AgentRoot: agent.AgentRoot, |
| 96 | + ExpectedSkills: expected, |
| 97 | + Detected: isDetected(agent), |
| 98 | + } |
| 99 | + if !report.Detected { |
| 100 | + return report, nil |
| 101 | + } |
| 102 | + |
| 103 | + entries, err := os.ReadDir(agent.SkillRoot) |
| 104 | + if err == nil { |
| 105 | + for _, entry := range entries { |
| 106 | + if !strings.HasPrefix(entry.Name(), ".") { |
| 107 | + report.External = append(report.External, entry.Name()) |
| 108 | + } |
| 109 | + } |
| 110 | + } else if !errors.Is(err, fs.ErrNotExist) { |
| 111 | + return agentReport{}, fmt.Errorf("read %s: %w", agent.SkillRoot, err) |
| 112 | + } |
| 113 | + |
| 114 | + configured, err := qwenHasSkillsDirectory(home, agentsSkillRoot) |
| 115 | + if err != nil { |
| 116 | + return agentReport{}, err |
| 117 | + } |
| 118 | + if !configured { |
| 119 | + report.Missing = append(report.Missing, "config skills.directories") |
| 120 | + report.Adds = append(report.Adds, "config skills.directories") |
| 121 | + } |
| 122 | + report.Managed = append(report.Managed, sortedKeys(expected)...) |
| 123 | + if err := augmentMCPReport(&report, agent, cfg, home); err != nil { |
| 124 | + return agentReport{}, err |
| 125 | + } |
| 126 | + if err := augmentHookReport(&report, agent, cfg, home); err != nil { |
| 127 | + return agentReport{}, err |
| 128 | + } |
| 129 | + if err := inspectAgentRoles(&report, filepath.Dir(agentsSkillRoot), agent); err != nil { |
| 130 | + return agentReport{}, err |
| 131 | + } |
| 132 | + if h := harnessFor(agent.Name); h != nil && h.RootInstructions != nil { |
| 133 | + if err := inspectRootInstructions(&report, h.RootInstructions, filepath.Dir(agentsSkillRoot), home); err != nil { |
| 134 | + return agentReport{}, err |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + sortReportLists(&report) |
| 139 | + report.Synced = isReportSynced(report) |
| 140 | + return report, nil |
| 141 | +} |
| 142 | + |
| 143 | +var qwenToolMapping = map[string]string{ |
| 144 | + "bash": "run_shell_command", |
| 145 | + "edit": "replace", |
| 146 | + "glob": "glob", |
| 147 | + "grep": "grep_search", |
| 148 | + "read": "read_file", |
| 149 | + "webfetch": "web_fetch", |
| 150 | + "websearch": "web_search", |
| 151 | + "write": "write_file", |
| 152 | +} |
| 153 | + |
| 154 | +func renderQwenAgentRole(role agentRole) string { |
| 155 | + model := strings.TrimSpace(role.Qwen.Model) |
| 156 | + if model == "" { |
| 157 | + model = "inherit" |
| 158 | + } |
| 159 | + tools := role.Qwen.Tools |
| 160 | + if len(tools) == 0 { |
| 161 | + tools = qwenToolsFor(role.Tools) |
| 162 | + } |
| 163 | + |
| 164 | + var b strings.Builder |
| 165 | + b.WriteString("---\n") |
| 166 | + writeYAMLScalar(&b, "name", role.Name) |
| 167 | + writeYAMLScalar(&b, "description", role.Description) |
| 168 | + writeYAMLScalar(&b, "model", model) |
| 169 | + writeYAMLScalar(&b, "approvalMode", role.Qwen.ApprovalMode) |
| 170 | + if len(tools) > 0 { |
| 171 | + b.WriteString("tools:\n") |
| 172 | + for _, tool := range tools { |
| 173 | + writeYAMLListItem(&b, tool) |
| 174 | + } |
| 175 | + } |
| 176 | + b.WriteString("---\n\n") |
| 177 | + b.WriteString("<!-- ") |
| 178 | + b.WriteString(generatedAgentMarker) |
| 179 | + b.WriteString(" from ") |
| 180 | + b.WriteString(agentRoleSourceLabel(role)) |
| 181 | + b.WriteString("; do not edit directly. -->\n\n") |
| 182 | + b.WriteString(role.Instructions) |
| 183 | + b.WriteString("\n") |
| 184 | + return b.String() |
| 185 | +} |
| 186 | + |
| 187 | +func qwenToolsFor(tools []string) []string { |
| 188 | + out := make([]string, 0, len(tools)) |
| 189 | + seen := make(map[string]struct{}, len(tools)) |
| 190 | + for _, tool := range tools { |
| 191 | + mapped := qwenToolMapping[strings.ToLower(strings.TrimSpace(tool))] |
| 192 | + if mapped == "" { |
| 193 | + continue |
| 194 | + } |
| 195 | + if _, ok := seen[mapped]; ok { |
| 196 | + continue |
| 197 | + } |
| 198 | + seen[mapped] = struct{}{} |
| 199 | + out = append(out, mapped) |
| 200 | + } |
| 201 | + return out |
| 202 | +} |
0 commit comments