-
Notifications
You must be signed in to change notification settings - Fork 0
Add Qwen Code support #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| func qwenSettingsPath(home string) string { | ||
| return filepath.Join(home, ".qwen", "settings.json") | ||
| } | ||
|
|
||
| func patchQwenConfig(home string, repoRoot string, _ config) (bool, error) { | ||
| configPath := qwenSettingsPath(home) | ||
| raw := map[string]interface{}{} | ||
| data, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return false, fmt.Errorf("read %s: %w", configPath, err) | ||
| } | ||
| } else if err := parseJSONConfig(configPath, data, &raw); err != nil { | ||
| return false, fmt.Errorf("parse %s: %w", configPath, err) | ||
| } | ||
|
|
||
| skillsValue, skillsExists := raw["skills"] | ||
| skills, skillsValid := skillsValue.(map[string]interface{}) | ||
| if skillsExists && skillsValue != nil && !skillsValid { | ||
| return false, fmt.Errorf("skills key in %s is not an object", configPath) | ||
| } | ||
| if !skillsExists || skillsValue == nil { | ||
| skills = map[string]interface{}{} | ||
| raw["skills"] = skills | ||
| } | ||
|
|
||
| target := filepath.Join(repoRoot, "skills") | ||
| directoriesValue, directoriesExists := skills["directories"] | ||
| directories, directoriesValid := directoriesValue.([]interface{}) | ||
| if directoriesExists && directoriesValue != nil && !directoriesValid { | ||
| return false, fmt.Errorf("skills.directories in %s is not an array", configPath) | ||
| } | ||
| for _, value := range directories { | ||
| directory, ok := value.(string) | ||
| if ok && filepath.Clean(expandPath(directory, home)) == filepath.Clean(target) { | ||
| return false, nil | ||
| } | ||
| } | ||
| skills["directories"] = append(directories, hermesExternalDirValue(home, target)) | ||
|
|
||
| out, err := json.MarshalIndent(raw, "", " ") | ||
| if err != nil { | ||
| return false, fmt.Errorf("marshal %s: %w", configPath, err) | ||
| } | ||
| out = append(out, '\n') | ||
| if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil { | ||
| return false, fmt.Errorf("create %s: %w", filepath.Dir(configPath), err) | ||
| } | ||
| if err := os.WriteFile(configPath, out, 0o644); err != nil { | ||
| return false, fmt.Errorf("write %s: %w", configPath, err) | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func qwenHasSkillsDirectory(home string, target string) (bool, error) { | ||
| configPath := qwenSettingsPath(home) | ||
| data, err := os.ReadFile(configPath) | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return false, nil | ||
| } | ||
| if err != nil { | ||
| return false, fmt.Errorf("read %s: %w", configPath, err) | ||
| } | ||
| var raw map[string]interface{} | ||
| if err := parseJSONConfig(configPath, data, &raw); err != nil { | ||
| return false, fmt.Errorf("parse %s: %w", configPath, err) | ||
| } | ||
| skills, _ := raw["skills"].(map[string]interface{}) | ||
| directories, _ := skills["directories"].([]interface{}) | ||
| for _, value := range directories { | ||
| directory, ok := value.(string) | ||
| if ok && filepath.Clean(expandPath(directory, home)) == filepath.Clean(target) { | ||
| return true, nil | ||
| } | ||
| } | ||
| return false, nil | ||
| } | ||
|
|
||
| func inspectQwenAgent(agent agentConfig, expected map[string]string, agentsSkillRoot string, cfg config, home string) (agentReport, error) { | ||
| report := agentReport{ | ||
| Name: agent.Name, | ||
| SkillRoot: agent.SkillRoot, | ||
| AgentRoot: agent.AgentRoot, | ||
| ExpectedSkills: expected, | ||
| Detected: isDetected(agent), | ||
| } | ||
| if !report.Detected { | ||
| return report, nil | ||
| } | ||
|
|
||
| entries, err := os.ReadDir(agent.SkillRoot) | ||
| if err == nil { | ||
| for _, entry := range entries { | ||
| if !strings.HasPrefix(entry.Name(), ".") { | ||
| report.External = append(report.External, entry.Name()) | ||
| } | ||
| } | ||
| } else if !errors.Is(err, fs.ErrNotExist) { | ||
| return agentReport{}, fmt.Errorf("read %s: %w", agent.SkillRoot, err) | ||
| } | ||
|
|
||
| configured, err := qwenHasSkillsDirectory(home, agentsSkillRoot) | ||
| if err != nil { | ||
| return agentReport{}, err | ||
| } | ||
| if !configured { | ||
| report.Missing = append(report.Missing, "config skills.directories") | ||
| report.Adds = append(report.Adds, "config skills.directories") | ||
| } | ||
| report.Managed = append(report.Managed, sortedKeys(expected)...) | ||
| if err := augmentMCPReport(&report, agent, cfg, home); err != nil { | ||
| return agentReport{}, err | ||
| } | ||
| if err := augmentHookReport(&report, agent, cfg, home); err != nil { | ||
| return agentReport{}, err | ||
| } | ||
| if err := inspectAgentRoles(&report, filepath.Dir(agentsSkillRoot), agent); err != nil { | ||
| return agentReport{}, err | ||
| } | ||
| if h := harnessFor(agent.Name); h != nil && h.RootInstructions != nil { | ||
| if err := inspectRootInstructions(&report, h.RootInstructions, filepath.Dir(agentsSkillRoot), home); err != nil { | ||
| return agentReport{}, err | ||
| } | ||
| } | ||
|
|
||
| sortReportLists(&report) | ||
| report.Synced = isReportSynced(report) | ||
| return report, nil | ||
| } | ||
|
|
||
| var qwenToolMapping = map[string]string{ | ||
| "bash": "run_shell_command", | ||
| "edit": "replace", | ||
| "glob": "glob", | ||
| "grep": "grep_search", | ||
| "read": "read_file", | ||
| "webfetch": "web_fetch", | ||
| "websearch": "web_search", | ||
| "write": "write_file", | ||
| } | ||
|
|
||
| func renderQwenAgentRole(role agentRole) string { | ||
| model := strings.TrimSpace(role.Qwen.Model) | ||
| if model == "" { | ||
| model = "inherit" | ||
| } | ||
| tools := role.Qwen.Tools | ||
| if len(tools) == 0 { | ||
| tools = qwenToolsFor(role.Tools) | ||
| } | ||
|
|
||
| var b strings.Builder | ||
| b.WriteString("---\n") | ||
| writeYAMLScalar(&b, "name", role.Name) | ||
| writeYAMLScalar(&b, "description", role.Description) | ||
| writeYAMLScalar(&b, "model", model) | ||
| writeYAMLScalar(&b, "approvalMode", role.Qwen.ApprovalMode) | ||
| if len(tools) > 0 { | ||
| b.WriteString("tools:\n") | ||
| for _, tool := range tools { | ||
| writeYAMLListItem(&b, tool) | ||
| } | ||
| } | ||
| b.WriteString("---\n\n") | ||
| b.WriteString("<!-- ") | ||
| b.WriteString(generatedAgentMarker) | ||
| b.WriteString(" from ") | ||
| b.WriteString(agentRoleSourceLabel(role)) | ||
| b.WriteString("; do not edit directly. -->\n\n") | ||
| b.WriteString(role.Instructions) | ||
| b.WriteString("\n") | ||
| return b.String() | ||
| } | ||
|
|
||
| func qwenToolsFor(tools []string) []string { | ||
| out := make([]string, 0, len(tools)) | ||
| seen := make(map[string]struct{}, len(tools)) | ||
| for _, tool := range tools { | ||
| mapped := qwenToolMapping[strings.ToLower(strings.TrimSpace(tool))] | ||
| if mapped == "" { | ||
| continue | ||
| } | ||
| if _, ok := seen[mapped]; ok { | ||
| continue | ||
| } | ||
| seen[mapped] = struct{}{} | ||
| out = append(out, mapped) | ||
| } | ||
| return out | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When first-run
setupshares an existing~/.qwen/agents/<name>.md, this capability causesscanNativeRolesto use the generic Markdown importer, which copies native top-level fields such asmodel,tools, andapprovalModeinto the canonical file.renderQwenAgentRoleinstead expects nestedqwenoptions and adds generated metadata, so the expected output differs from the untouched native source; the first sync then classifies that source as an unmanaged conflict and aborts. Add a Qwen-specific conversion that preserves those native fields in canonicalqwenoptions so importing an existing Qwen role can complete.AGENTS.md reference: AGENTS.md:L17-L19
Useful? React with 👍 / 👎.