|
| 1 | +import { platform } from "os"; |
| 2 | +import { |
| 3 | + defineCommand, |
| 4 | + BailianError, |
| 5 | + ExitCode, |
| 6 | + type Config, |
| 7 | + type GlobalFlags, |
| 8 | +} from "bailian-cli-core"; |
| 9 | +import { AGENTS, VALID_AGENT_NAMES, type WriteParams } from "./writers.ts"; |
| 10 | + |
| 11 | +export default defineCommand({ |
| 12 | + name: "agent setup", |
| 13 | + description: "Configure a coding agent to use DashScope API", |
| 14 | + skipDefaultApiKeySetup: true, |
| 15 | + usage: "bl agent setup --agent <name> --base-url <url> --api-key <key> --model <model>", |
| 16 | + options: [ |
| 17 | + { |
| 18 | + flag: "--agent <name>", |
| 19 | + description: `Target agent: ${VALID_AGENT_NAMES.join(", ")}`, |
| 20 | + }, |
| 21 | + { flag: "--base-url <url>", description: "API base URL" }, |
| 22 | + { flag: "--api-key <key>", description: "API key" }, |
| 23 | + { flag: "--model <model>", description: "Default model name" }, |
| 24 | + ], |
| 25 | + examples: [ |
| 26 | + "npx bailian-cli agent setup --agent claude-code --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3.7-max", |
| 27 | + "npx bailian-cli agent setup --agent qwen-code --base-url https://dashscope.aliyuncs.com/compatible-mode/v1 --api-key sk-xxxxx --model qwen3.6-plus", |
| 28 | + "npx bailian-cli agent setup --agent opencode --base-url https://dashscope.aliyuncs.com/apps/anthropic/v1 --api-key sk-xxxxx --model qwen3.7-max", |
| 29 | + "npx bailian-cli agent setup --agent openclaw --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3.6-plus", |
| 30 | + "npx bailian-cli agent setup --agent hermes --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3.7-max", |
| 31 | + "npx bailian-cli agent setup --agent codex --base-url https://dashscope.aliyuncs.com/compatible-mode/v1 --api-key sk-xxxxx --model qwen3.7-max", |
| 32 | + ], |
| 33 | + async run(_config: Config, flags: GlobalFlags) { |
| 34 | + const agent = flags.agent as string | undefined; |
| 35 | + const baseUrl = flags.baseUrl as string | undefined; |
| 36 | + const apiKey = flags.apiKey as string | undefined; |
| 37 | + const model = flags.model as string | undefined; |
| 38 | + |
| 39 | + if (!agent || !baseUrl || !apiKey || !model) { |
| 40 | + throw new BailianError( |
| 41 | + "All flags are required: --agent, --base-url, --api-key, --model", |
| 42 | + ExitCode.USAGE, |
| 43 | + "bl agent setup --agent <name> --base-url <url> --api-key <key> --model <model>", |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + const agentDef = AGENTS[agent]; |
| 48 | + if (!agentDef) { |
| 49 | + throw new BailianError( |
| 50 | + `Unknown agent "${agent}". Valid agents: ${VALID_AGENT_NAMES.join(", ")}`, |
| 51 | + ExitCode.USAGE, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + // Hermes does not support native Windows |
| 56 | + if (agent === "hermes" && platform() === "win32") { |
| 57 | + process.stderr.write( |
| 58 | + "Warning: Hermes Agent does not support native Windows. Please use WSL2.\n", |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + const params: WriteParams = { baseUrl, apiKey, model }; |
| 63 | + |
| 64 | + if (_config.dryRun) { |
| 65 | + process.stdout.write(`[dry-run] Would configure ${agentDef.label} with:\n`); |
| 66 | + process.stdout.write(` base-url: ${baseUrl}\n`); |
| 67 | + process.stdout.write( |
| 68 | + ` api-key: ${apiKey.slice(0, 6)}${"*".repeat(Math.max(0, apiKey.length - 6))}\n`, |
| 69 | + ); |
| 70 | + process.stdout.write(` model: ${model}\n`); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const summary = agentDef.write(params); |
| 75 | + |
| 76 | + const isTTY = process.stderr.isTTY; |
| 77 | + const green = isTTY ? "\x1b[32m" : ""; |
| 78 | + const cyan = isTTY ? "\x1b[36m" : ""; |
| 79 | + const reset = isTTY ? "\x1b[0m" : ""; |
| 80 | + |
| 81 | + process.stderr.write(`\n${green}✔ ${agentDef.label} configured successfully.${reset}\n\n`); |
| 82 | + for (const p of summary.paths) { |
| 83 | + process.stderr.write(` Written: ${cyan}${p}${reset}\n`); |
| 84 | + } |
| 85 | + process.stderr.write(`\n ${summary.nextStep}\n\n`); |
| 86 | + }, |
| 87 | +}); |
0 commit comments