Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/cli/src/commands/auth/login-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ const CONSOLE_ORIGINS: Record<string, string> = {
};

export function resolveConsoleOrigin(site?: string): string {
return (site && CONSOLE_ORIGINS[site]) || CONSOLE_ORIGINS.domestic!;
const origin = (site && CONSOLE_ORIGINS[site]) || CONSOLE_ORIGINS.domestic!;
if (process.env.BAILIAN_PRE) {
const u = new URL(origin);
u.hostname = `pre-${u.hostname}`;
return u.origin;
}
return origin;
}

function readBodyBounded(req: http.IncomingMessage): Promise<string> {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import quotaList from "./quota/list.ts";
import quotaRequest from "./quota/request.ts";
import quotaHistory from "./quota/history.ts";
import quotaCheck from "./quota/check.ts";
import agentSetup from "./config/agent/index.ts";

/** Command registry map (no dependency on registry.ts — safe for build-time import). */
export const commands: Record<string, Command> = {
Expand Down Expand Up @@ -94,5 +95,6 @@ export const commands: Record<string, Command> = {
"quota request": quotaRequest,
"quota history": quotaHistory,
"quota check": quotaCheck,
"config agent": agentSetup,
update: update,
};
87 changes: 87 additions & 0 deletions packages/cli/src/commands/config/agent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { platform } from "os";
import {
defineCommand,
BailianError,
ExitCode,
type Config,
type GlobalFlags,
} from "bailian-cli-core";
import { AGENTS, VALID_AGENT_NAMES, type WriteParams } from "./writers.ts";

export default defineCommand({
name: "config agent",
description: "Configure a coding agent to use DashScope API",
skipDefaultApiKeySetup: true,
usage: "bl config agent --agent <name> --base-url <url> --api-key <key> --model <model>",
options: [
{
flag: "--agent <name>",
description: `Target agent: ${VALID_AGENT_NAMES.join(", ")}`,
},
{ flag: "--base-url <url>", description: "API base URL" },
{ flag: "--api-key <key>", description: "API key" },
{ flag: "--model <model>", description: "Default model name" },
],
examples: [
"npx bailian-cli config agent --agent claude-code --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3.7-max",
"npx bailian-cli config agent --agent qwen-code --base-url https://dashscope.aliyuncs.com/compatible-mode/v1 --api-key sk-xxxxx --model qwen3.6-plus",
"npx bailian-cli config agent --agent opencode --base-url https://dashscope.aliyuncs.com/apps/anthropic/v1 --api-key sk-xxxxx --model qwen3.7-max",
"npx bailian-cli config agent --agent openclaw --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3.6-plus",
"npx bailian-cli config agent --agent hermes --base-url https://dashscope.aliyuncs.com/apps/anthropic --api-key sk-xxxxx --model qwen3.7-max",
"npx bailian-cli config agent --agent codex --base-url https://dashscope.aliyuncs.com/compatible-mode/v1 --api-key sk-xxxxx --model qwen3.7-max",
],
async run(_config: Config, flags: GlobalFlags) {
const agent = flags.agent as string | undefined;
const baseUrl = flags.baseUrl as string | undefined;
const apiKey = flags.apiKey as string | undefined;
const model = flags.model as string | undefined;

if (!agent || !baseUrl || !apiKey || !model) {
throw new BailianError(
"All flags are required: --agent, --base-url, --api-key, --model",
ExitCode.USAGE,
"bl config agent --agent <name> --base-url <url> --api-key <key> --model <model>",
);
}

const agentDef = AGENTS[agent];
if (!agentDef) {
throw new BailianError(
`Unknown agent "${agent}". Valid agents: ${VALID_AGENT_NAMES.join(", ")}`,
ExitCode.USAGE,
);
}

// Hermes does not support native Windows
if (agent === "hermes" && platform() === "win32") {
process.stderr.write(
"Warning: Hermes Agent does not support native Windows. Please use WSL2.\n",
);
}

const params: WriteParams = { baseUrl, apiKey, model };

if (_config.dryRun) {
process.stdout.write(`[dry-run] Would configure ${agentDef.label} with:\n`);
process.stdout.write(` base-url: ${baseUrl}\n`);
process.stdout.write(
` api-key: ${apiKey.slice(0, 6)}${"*".repeat(Math.max(0, apiKey.length - 6))}\n`,
);
process.stdout.write(` model: ${model}\n`);
return;
}

const summary = agentDef.write(params);

const isTTY = process.stderr.isTTY;
const green = isTTY ? "\x1b[32m" : "";
const cyan = isTTY ? "\x1b[36m" : "";
const reset = isTTY ? "\x1b[0m" : "";

process.stderr.write(`\n${green}✔ ${agentDef.label} configured successfully.${reset}\n\n`);
for (const p of summary.paths) {
process.stderr.write(` Written: ${cyan}${p}${reset}\n`);
}
process.stderr.write(`\n ${summary.nextStep}\n\n`);
},
});
Loading