|
| 1 | +import { platform } from "os"; |
| 2 | +import { defineCommand, detectOutputFormat, maskToken, type FlagsDef } from "bailian-cli-core"; |
| 3 | +import { emitResult, emitBare } from "bailian-cli-runtime"; |
| 4 | +import { AGENTS, VALID_AGENT_NAMES, type WriteParams } from "./writers.ts"; |
| 5 | + |
| 6 | +const FLAGS = { |
| 7 | + agent: { |
| 8 | + type: "string", |
| 9 | + valueHint: "<name>", |
| 10 | + description: `Target agent: ${VALID_AGENT_NAMES.join(", ")}`, |
| 11 | + required: true, |
| 12 | + choices: VALID_AGENT_NAMES, |
| 13 | + }, |
| 14 | + baseUrl: { type: "string", valueHint: "<url>", description: "API base URL", required: true }, |
| 15 | + apiKey: { type: "string", valueHint: "<key>", description: "API key", required: true }, |
| 16 | + model: { |
| 17 | + type: "string", |
| 18 | + valueHint: "<model>", |
| 19 | + description: "Default model name", |
| 20 | + required: true, |
| 21 | + }, |
| 22 | +} satisfies FlagsDef; |
| 23 | + |
| 24 | +export default defineCommand({ |
| 25 | + description: "Configure a coding agent to use DashScope API", |
| 26 | + auth: "none", |
| 27 | + usageArgs: "--agent <name> --base-url <url> --api-key <key> --model <model>", |
| 28 | + flags: FLAGS, |
| 29 | + exampleArgs: [ |
| 30 | + "--agent claude-code --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3-max", |
| 31 | + "--agent qwen-code --base-url https://dashscope.aliyuncs.com/compatible-mode/v1 --api-key sk-xxxxx --model qwen3-coder-plus", |
| 32 | + "--agent codex --base-url https://dashscope.aliyuncs.com/compatible-mode/v1 --api-key sk-xxxxx --model qwen3-coder-plus", |
| 33 | + ], |
| 34 | + async run(ctx) { |
| 35 | + const { settings, flags } = ctx; |
| 36 | + const agentName = flags.agent; |
| 37 | + const { baseUrl, apiKey, model } = flags; |
| 38 | + const agentDef = AGENTS[agentName]; |
| 39 | + const format = detectOutputFormat(settings.output); |
| 40 | + |
| 41 | + // Hermes has no native Windows support. |
| 42 | + if (agentName === "hermes" && platform() === "win32") { |
| 43 | + process.stderr.write( |
| 44 | + "Warning: Hermes Agent does not support native Windows. Please use WSL2.\n", |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (settings.dryRun) { |
| 49 | + emitResult( |
| 50 | + { |
| 51 | + agent: agentName, |
| 52 | + label: agentDef.label, |
| 53 | + base_url: baseUrl, |
| 54 | + api_key: maskToken(apiKey), |
| 55 | + model, |
| 56 | + }, |
| 57 | + format, |
| 58 | + ); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const params: WriteParams = { baseUrl, apiKey, model }; |
| 63 | + const summary = agentDef.write(params); |
| 64 | + |
| 65 | + if (!settings.quiet) { |
| 66 | + emitBare(`${agentDef.label} configured successfully.`); |
| 67 | + for (const path of summary.paths) emitBare(` Written: ${path}`); |
| 68 | + emitBare(` ${summary.nextStep}`); |
| 69 | + } |
| 70 | + }, |
| 71 | +}); |
0 commit comments