|
| 1 | +/** |
| 2 | + * Qwen Code target. Writes: |
| 3 | + * |
| 4 | + * - MCP server entry to `~/.qwen/settings.json` (global) or |
| 5 | + * `./.qwen/settings.json` (local) under the standard |
| 6 | + * `mcpServers.codegraph` key. Qwen Code is Gemini CLI-derived but |
| 7 | + * documents stdio servers as `command` + `args` entries, without a |
| 8 | + * `type: "stdio"` discriminator. |
| 9 | + * - Instructions to `~/.qwen/QWEN.md` (global) or `./QWEN.md` |
| 10 | + * (local). Qwen Code's hierarchical context loader reads QWEN.md, |
| 11 | + * matching Gemini CLI's GEMINI.md behavior. |
| 12 | + * |
| 13 | + * No permissions concept — Qwen Code gates MCP invocations through the |
| 14 | + * per-server `trust` field and UI prompts. We leave `trust` unset so |
| 15 | + * the user controls confirmation prompts. |
| 16 | + * |
| 17 | + * Docs: |
| 18 | + * https://qwenlm.github.io/qwen-code-docs/en/users/configuration/settings/ |
| 19 | + */ |
| 20 | + |
| 21 | +import * as fs from 'fs'; |
| 22 | +import * as path from 'path'; |
| 23 | +import * as os from 'os'; |
| 24 | +import { |
| 25 | + AgentTarget, |
| 26 | + DetectionResult, |
| 27 | + InstallOptions, |
| 28 | + Location, |
| 29 | + WriteResult, |
| 30 | +} from './types'; |
| 31 | +import { |
| 32 | + jsonDeepEqual, |
| 33 | + readJsonFile, |
| 34 | + removeMarkedSection, |
| 35 | + writeJsonFile, |
| 36 | + upsertInstructionsEntry, |
| 37 | +} from './shared'; |
| 38 | +import { |
| 39 | + CODEGRAPH_SECTION_END, |
| 40 | + CODEGRAPH_SECTION_START, |
| 41 | +} from '../instructions-template'; |
| 42 | + |
| 43 | +function configDir(loc: Location): string { |
| 44 | + return loc === 'global' |
| 45 | + ? path.join(os.homedir(), '.qwen') |
| 46 | + : path.join(process.cwd(), '.qwen'); |
| 47 | +} |
| 48 | + |
| 49 | +function settingsJsonPath(loc: Location): string { |
| 50 | + return path.join(configDir(loc), 'settings.json'); |
| 51 | +} |
| 52 | + |
| 53 | +function instructionsPath(loc: Location): string { |
| 54 | + return loc === 'global' |
| 55 | + ? path.join(configDir('global'), 'QWEN.md') |
| 56 | + : path.join(process.cwd(), 'QWEN.md'); |
| 57 | +} |
| 58 | + |
| 59 | +class QwenTarget implements AgentTarget { |
| 60 | + readonly id = 'qwen' as const; |
| 61 | + readonly displayName = 'Qwen Code'; |
| 62 | + readonly docsUrl = 'https://qwenlm.github.io/qwen-code-docs/en/users/configuration/settings/'; |
| 63 | + |
| 64 | + supportsLocation(_loc: Location): boolean { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + detect(loc: Location): DetectionResult { |
| 69 | + const file = settingsJsonPath(loc); |
| 70 | + const config = readJsonFile(file); |
| 71 | + const alreadyConfigured = !!config.mcpServers?.codegraph; |
| 72 | + const installed = loc === 'global' |
| 73 | + ? fs.existsSync(configDir('global')) || fs.existsSync(file) |
| 74 | + : fs.existsSync(file) || fs.existsSync(configDir('local')); |
| 75 | + return { installed, alreadyConfigured, configPath: file }; |
| 76 | + } |
| 77 | + |
| 78 | + install(loc: Location, _opts: InstallOptions): WriteResult { |
| 79 | + const files: WriteResult['files'] = []; |
| 80 | + files.push(writeMcpEntry(loc)); |
| 81 | + files.push(upsertInstructionsEntry(instructionsPath(loc))); |
| 82 | + return { files }; |
| 83 | + } |
| 84 | + |
| 85 | + uninstall(loc: Location): WriteResult { |
| 86 | + const files: WriteResult['files'] = []; |
| 87 | + |
| 88 | + const file = settingsJsonPath(loc); |
| 89 | + const config = readJsonFile(file); |
| 90 | + if (config.mcpServers?.codegraph) { |
| 91 | + delete config.mcpServers.codegraph; |
| 92 | + if (Object.keys(config.mcpServers).length === 0) { |
| 93 | + delete config.mcpServers; |
| 94 | + } |
| 95 | + writeJsonFile(file, config); |
| 96 | + files.push({ path: file, action: 'removed' }); |
| 97 | + } else { |
| 98 | + files.push({ path: file, action: 'not-found' }); |
| 99 | + } |
| 100 | + |
| 101 | + files.push(removeInstructionsEntry(loc)); |
| 102 | + |
| 103 | + return { files }; |
| 104 | + } |
| 105 | + |
| 106 | + printConfig(loc: Location): string { |
| 107 | + const target = settingsJsonPath(loc); |
| 108 | + const snippet = JSON.stringify({ mcpServers: { codegraph: buildQwenMcpEntry() } }, null, 2); |
| 109 | + return `# Add to ${target}\n\n${snippet}\n`; |
| 110 | + } |
| 111 | + |
| 112 | + describePaths(loc: Location): string[] { |
| 113 | + return [settingsJsonPath(loc), instructionsPath(loc)]; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +function writeMcpEntry(loc: Location): WriteResult['files'][number] { |
| 118 | + const file = settingsJsonPath(loc); |
| 119 | + const dir = path.dirname(file); |
| 120 | + if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 121 | + |
| 122 | + const existing = readJsonFile(file); |
| 123 | + const before = existing.mcpServers?.codegraph; |
| 124 | + const after = buildQwenMcpEntry(); |
| 125 | + |
| 126 | + if (jsonDeepEqual(before, after)) { |
| 127 | + return { path: file, action: 'unchanged' }; |
| 128 | + } |
| 129 | + const action: 'created' | 'updated' = |
| 130 | + before ? 'updated' : (fs.existsSync(file) ? 'updated' : 'created'); |
| 131 | + if (!existing.mcpServers) existing.mcpServers = {}; |
| 132 | + existing.mcpServers.codegraph = after; |
| 133 | + writeJsonFile(file, existing); |
| 134 | + return { path: file, action }; |
| 135 | +} |
| 136 | + |
| 137 | +function buildQwenMcpEntry(): { command: string; args: string[] } { |
| 138 | + return { |
| 139 | + command: 'codegraph', |
| 140 | + args: ['serve', '--mcp'], |
| 141 | + }; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Strip the marker-delimited CodeGraph block from QWEN.md if a prior |
| 146 | + * install wrote one. |
| 147 | + */ |
| 148 | +function removeInstructionsEntry(loc: Location): WriteResult['files'][number] { |
| 149 | + const file = instructionsPath(loc); |
| 150 | + const action = removeMarkedSection(file, CODEGRAPH_SECTION_START, CODEGRAPH_SECTION_END); |
| 151 | + return { path: file, action }; |
| 152 | +} |
| 153 | + |
| 154 | +export const qwenTarget: AgentTarget = new QwenTarget(); |
0 commit comments