|
| 1 | +import { formatFrontmatter } from "../utils/frontmatter" |
| 2 | +import type { ClaudeAgent, ClaudeCommand, ClaudeMcpServer, ClaudePlugin } from "../types/claude" |
| 3 | +import type { |
| 4 | + QwenAgentFile, |
| 5 | + QwenBundle, |
| 6 | + QwenCommandFile, |
| 7 | + QwenExtensionConfig, |
| 8 | + QwenMcpServer, |
| 9 | + QwenSetting, |
| 10 | +} from "../types/qwen" |
| 11 | + |
| 12 | +export type ClaudeToQwenOptions = { |
| 13 | + agentMode: "primary" | "subagent" |
| 14 | + inferTemperature: boolean |
| 15 | +} |
| 16 | + |
| 17 | +export function convertClaudeToQwen(plugin: ClaudePlugin, options: ClaudeToQwenOptions): QwenBundle { |
| 18 | + const agentFiles = plugin.agents.map((agent) => convertAgent(agent, options)) |
| 19 | + const cmdFiles = convertCommands(plugin.commands) |
| 20 | + const mcp = plugin.mcpServers ? convertMcp(plugin.mcpServers) : undefined |
| 21 | + const settings = extractSettings(plugin.mcpServers) |
| 22 | + |
| 23 | + const config: QwenExtensionConfig = { |
| 24 | + name: plugin.manifest.name, |
| 25 | + version: plugin.manifest.version || "1.0.0", |
| 26 | + commands: "commands", |
| 27 | + skills: "skills", |
| 28 | + agents: "agents", |
| 29 | + } |
| 30 | + |
| 31 | + if (mcp && Object.keys(mcp).length > 0) { |
| 32 | + config.mcpServers = mcp |
| 33 | + } |
| 34 | + |
| 35 | + if (settings && settings.length > 0) { |
| 36 | + config.settings = settings |
| 37 | + } |
| 38 | + |
| 39 | + const contextFile = generateContextFile(plugin) |
| 40 | + |
| 41 | + return { |
| 42 | + config, |
| 43 | + agents: agentFiles, |
| 44 | + commandFiles: cmdFiles, |
| 45 | + skillDirs: plugin.skills.map((skill) => ({ sourceDir: skill.sourceDir, name: skill.name })), |
| 46 | + contextFile, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function convertAgent(agent: ClaudeAgent, options: ClaudeToQwenOptions): QwenAgentFile { |
| 51 | + const frontmatter: Record<string, unknown> = { |
| 52 | + name: agent.name, |
| 53 | + description: agent.description, |
| 54 | + } |
| 55 | + |
| 56 | + if (agent.model && agent.model !== "inherit") { |
| 57 | + frontmatter.model = normalizeModel(agent.model) |
| 58 | + } |
| 59 | + |
| 60 | + if (options.inferTemperature) { |
| 61 | + const temperature = inferTemperature(agent) |
| 62 | + if (temperature !== undefined) { |
| 63 | + frontmatter.temperature = temperature |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Qwen supports both YAML and Markdown for agents |
| 68 | + // Using YAML format for structured config |
| 69 | + const content = formatFrontmatter(frontmatter, rewriteQwenPaths(agent.body)) |
| 70 | + |
| 71 | + return { |
| 72 | + name: agent.name, |
| 73 | + content, |
| 74 | + format: "yaml", |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function convertCommands(commands: ClaudeCommand[]): QwenCommandFile[] { |
| 79 | + const files: QwenCommandFile[] = [] |
| 80 | + for (const command of commands) { |
| 81 | + if (command.disableModelInvocation) continue |
| 82 | + const frontmatter: Record<string, unknown> = { |
| 83 | + description: command.description, |
| 84 | + } |
| 85 | + if (command.model && command.model !== "inherit") { |
| 86 | + frontmatter.model = normalizeModel(command.model) |
| 87 | + } |
| 88 | + if (command.allowedTools && command.allowedTools.length > 0) { |
| 89 | + frontmatter.allowedTools = command.allowedTools |
| 90 | + } |
| 91 | + const content = formatFrontmatter(frontmatter, rewriteQwenPaths(command.body)) |
| 92 | + files.push({ name: command.name, content }) |
| 93 | + } |
| 94 | + return files |
| 95 | +} |
| 96 | + |
| 97 | +function convertMcp(servers: Record<string, ClaudeMcpServer>): Record<string, QwenMcpServer> { |
| 98 | + const result: Record<string, QwenMcpServer> = {} |
| 99 | + for (const [name, server] of Object.entries(servers)) { |
| 100 | + if (server.command) { |
| 101 | + result[name] = { |
| 102 | + command: server.command, |
| 103 | + args: server.args, |
| 104 | + env: server.env, |
| 105 | + } |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + if (server.url) { |
| 110 | + // Qwen only supports stdio (command-based) MCP servers — skip remote servers |
| 111 | + console.warn( |
| 112 | + `Warning: Remote MCP server '${name}' (URL: ${server.url}) is not supported in Qwen format. Qwen only supports stdio MCP servers. Skipping.`, |
| 113 | + ) |
| 114 | + } |
| 115 | + } |
| 116 | + return result |
| 117 | +} |
| 118 | + |
| 119 | +function extractSettings(mcpServers?: Record<string, ClaudeMcpServer>): QwenSetting[] { |
| 120 | + const settings: QwenSetting[] = [] |
| 121 | + if (!mcpServers) return settings |
| 122 | + |
| 123 | + for (const [name, server] of Object.entries(mcpServers)) { |
| 124 | + if (server.env) { |
| 125 | + for (const [envVar, value] of Object.entries(server.env)) { |
| 126 | + // Only add settings for environment variables that look like placeholders |
| 127 | + if (value.startsWith("${") || value.includes("YOUR_") || value.includes("XXX")) { |
| 128 | + settings.push({ |
| 129 | + name: formatSettingName(envVar), |
| 130 | + description: `Environment variable for ${name} MCP server`, |
| 131 | + envVar, |
| 132 | + sensitive: envVar.toLowerCase().includes("key") || envVar.toLowerCase().includes("token") || envVar.toLowerCase().includes("secret"), |
| 133 | + }) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return settings |
| 140 | +} |
| 141 | + |
| 142 | +function formatSettingName(envVar: string): string { |
| 143 | + return envVar |
| 144 | + .replace(/_/g, " ") |
| 145 | + .toLowerCase() |
| 146 | + .replace(/\b\w/g, (c) => c.toUpperCase()) |
| 147 | +} |
| 148 | + |
| 149 | +function generateContextFile(plugin: ClaudePlugin): string { |
| 150 | + const sections: string[] = [] |
| 151 | + |
| 152 | + // Plugin description |
| 153 | + sections.push(`# ${plugin.manifest.name}`) |
| 154 | + sections.push("") |
| 155 | + if (plugin.manifest.description) { |
| 156 | + sections.push(plugin.manifest.description) |
| 157 | + sections.push("") |
| 158 | + } |
| 159 | + |
| 160 | + // Agents section |
| 161 | + if (plugin.agents.length > 0) { |
| 162 | + sections.push("## Agents") |
| 163 | + sections.push("") |
| 164 | + for (const agent of plugin.agents) { |
| 165 | + sections.push(`- **${agent.name}**: ${agent.description || "No description"}`) |
| 166 | + } |
| 167 | + sections.push("") |
| 168 | + } |
| 169 | + |
| 170 | + // Commands section |
| 171 | + if (plugin.commands.length > 0) { |
| 172 | + sections.push("## Commands") |
| 173 | + sections.push("") |
| 174 | + for (const command of plugin.commands) { |
| 175 | + if (!command.disableModelInvocation) { |
| 176 | + sections.push(`- **/${command.name}**: ${command.description || "No description"}`) |
| 177 | + } |
| 178 | + } |
| 179 | + sections.push("") |
| 180 | + } |
| 181 | + |
| 182 | + // Skills section |
| 183 | + if (plugin.skills.length > 0) { |
| 184 | + sections.push("## Skills") |
| 185 | + sections.push("") |
| 186 | + for (const skill of plugin.skills) { |
| 187 | + sections.push(`- ${skill.name}`) |
| 188 | + } |
| 189 | + sections.push("") |
| 190 | + } |
| 191 | + |
| 192 | + return sections.join("\n") |
| 193 | +} |
| 194 | + |
| 195 | +function rewriteQwenPaths(body: string): string { |
| 196 | + return body |
| 197 | + .replace(/(?<=^|\s|["'`])~\/\.claude\//gm, "~/.qwen/") |
| 198 | + .replace(/(?<=^|\s|["'`])\.claude\//gm, ".qwen/") |
| 199 | +} |
| 200 | + |
| 201 | +const CLAUDE_FAMILY_ALIASES: Record<string, string> = { |
| 202 | + haiku: "claude-haiku", |
| 203 | + sonnet: "claude-sonnet", |
| 204 | + opus: "claude-opus", |
| 205 | +} |
| 206 | + |
| 207 | +function normalizeModel(model: string): string { |
| 208 | + if (model.includes("/")) return model |
| 209 | + if (CLAUDE_FAMILY_ALIASES[model]) { |
| 210 | + const resolved = `anthropic/${CLAUDE_FAMILY_ALIASES[model]}` |
| 211 | + console.warn( |
| 212 | + `Warning: bare model alias "${model}" mapped to "${resolved}".`, |
| 213 | + ) |
| 214 | + return resolved |
| 215 | + } |
| 216 | + if (/^claude-/.test(model)) return `anthropic/${model}` |
| 217 | + if (/^(gpt-|o1-|o3-)/.test(model)) return `openai/${model}` |
| 218 | + if (/^gemini-/.test(model)) return `google/${model}` |
| 219 | + if (/^qwen-/.test(model)) return `qwen/${model}` |
| 220 | + return `anthropic/${model}` |
| 221 | +} |
| 222 | + |
| 223 | +function inferTemperature(agent: ClaudeAgent): number | undefined { |
| 224 | + const sample = `${agent.name} ${agent.description ?? ""}`.toLowerCase() |
| 225 | + if (/(review|audit|security|sentinel|oracle|lint|verification|guardian)/.test(sample)) { |
| 226 | + return 0.1 |
| 227 | + } |
| 228 | + if (/(plan|planning|architecture|strategist|analysis|research)/.test(sample)) { |
| 229 | + return 0.2 |
| 230 | + } |
| 231 | + if (/(doc|readme|changelog|editor|writer)/.test(sample)) { |
| 232 | + return 0.3 |
| 233 | + } |
| 234 | + if (/(brainstorm|creative|ideate|design|concept)/.test(sample)) { |
| 235 | + return 0.6 |
| 236 | + } |
| 237 | + return undefined |
| 238 | +} |
0 commit comments