Skip to content

Commit 067e966

Browse files
lishengzxcclaude
andcommitted
feat(console): resolve gateway URL from region + site, add switchAgent support
Console gateway URL and action are now resolved from a region + site mapping table instead of a single hardcoded config value. Supports cn-beijing and ap-southeast-1 with domestic/international site variants. - Add ConsoleSite type, REGION_GATEWAYS mapping, and resolveGateway() - Add switchAgent to cornerstoneParam for delegated access - Add console_site, console_region, console_switch_agent to config - Remove consoleGatewayUrl from Config (replaced by region+site resolution) - login-console callback now persists baseUrl, site, region, switchAgent - bl console call gains --site and --switch-agent flags - All callers delegate region default to callConsoleGateway (no more hardcoded cn-beijing) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 173e5a7 commit 067e966

19 files changed

Lines changed: 163 additions & 46 deletions

File tree

packages/cli/src/commands/app/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default defineCommand({
4444
const name = (flags.name as string) || "";
4545
const pageNo = (flags.page as number) || 1;
4646
const pageSize = (flags.pageSize as number) || 30;
47-
const region = (flags.region as string) || "cn-beijing";
47+
const region = (flags.region as string) || undefined;
4848
const format = detectOutputFormat(config.output);
4949

5050
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/auth/login-console.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ function parseApiKeyFromRawBody(raw: string, contentType: string): string | null
213213
interface CallbackCredentials {
214214
accessToken: string | null;
215215
apiKey: string | null;
216+
baseUrl: string | null;
217+
consoleSite: string | null;
218+
consoleRegion: string | null;
219+
consoleSwitchAgent: string | null;
216220
}
217221

218222
async function extractCredentialsFromRequest(
@@ -222,12 +226,27 @@ async function extractCredentialsFromRequest(
222226
const accessTokenFromQuery =
223227
u.searchParams.get("access_token") ?? u.searchParams.get("accessToken");
224228
const apiKeyFromQuery = u.searchParams.get("api_key") ?? u.searchParams.get("apiKey");
229+
const baseUrlFromQuery = u.searchParams.get("base_url") ?? u.searchParams.get("baseUrl");
230+
const consoleSiteFromQuery =
231+
u.searchParams.get("console_site") ?? u.searchParams.get("consoleSite");
232+
const consoleRegionFromQuery =
233+
u.searchParams.get("console_region") ?? u.searchParams.get("consoleRegion");
234+
const consoleSwitchAgentFromQuery =
235+
u.searchParams.get("console_switch_agent") ?? u.searchParams.get("consoleSwitchAgent");
236+
237+
const extras = {
238+
baseUrl: baseUrlFromQuery?.trim() || null,
239+
consoleSite: consoleSiteFromQuery?.trim() || null,
240+
consoleRegion: consoleRegionFromQuery?.trim() || null,
241+
consoleSwitchAgent: consoleSwitchAgentFromQuery?.trim() || null,
242+
};
225243

226244
const m = req.method ?? "GET";
227245
if (m !== "POST" && m !== "PUT" && m !== "PATCH") {
228246
return {
229247
accessToken: accessTokenFromQuery?.trim() || null,
230248
apiKey: apiKeyFromQuery?.trim() || null,
249+
...extras,
231250
};
232251
}
233252

@@ -239,12 +258,13 @@ async function extractCredentialsFromRequest(
239258
return {
240259
accessToken: accessTokenFromQuery?.trim() || null,
241260
apiKey: apiKeyFromQuery?.trim() || null,
261+
...extras,
242262
};
243263
}
244264

245265
const accessToken = accessTokenFromQuery?.trim() || parseAccessTokenFromRawBody(raw, contentType);
246266
const apiKey = apiKeyFromQuery?.trim() || parseApiKeyFromRawBody(raw, contentType);
247-
return { accessToken, apiKey };
267+
return { accessToken, apiKey, ...extras };
248268
}
249269

250270
function listenServerOnFreeLocalPort(server: http.Server): Promise<number> {
@@ -301,16 +321,40 @@ export async function runConsoleLogin(
301321
return;
302322
}
303323

304-
const { accessToken, apiKey } = await extractCredentialsFromRequest(req);
324+
const { accessToken, apiKey, baseUrl, consoleSite, consoleRegion, consoleSwitchAgent } =
325+
await extractCredentialsFromRequest(req);
305326

306-
if (accessToken || apiKey) {
327+
if (accessToken || apiKey || baseUrl || consoleSite || consoleRegion || consoleSwitchAgent) {
307328
try {
329+
const existing = readConfigFile() as Record<string, unknown>;
330+
let changed = false;
331+
308332
if (accessToken) {
309-
const existing = readConfigFile() as Record<string, unknown>;
310333
existing.access_token = accessToken;
334+
changed = true;
335+
}
336+
if (baseUrl) {
337+
existing.base_url = baseUrl;
338+
changed = true;
339+
}
340+
if (consoleSite) {
341+
existing.console_site = consoleSite;
342+
changed = true;
343+
}
344+
if (consoleRegion) {
345+
existing.console_region = consoleRegion;
346+
changed = true;
347+
}
348+
if (consoleSwitchAgent) {
349+
existing.console_switch_agent = Number(consoleSwitchAgent);
350+
changed = true;
351+
}
352+
353+
if (changed) {
311354
await writeConfigFile(existing);
312-
process.stderr.write(`access_token saved to ${getConfigPath()}\n`);
355+
process.stderr.write(`Config saved to ${getConfigPath()}\n`);
313356
}
357+
314358
if (apiKey && opts?.onApiKey) {
315359
await opts.onApiKey(apiKey);
316360
}
@@ -329,7 +373,7 @@ export async function runConsoleLogin(
329373
});
330374
res.end("OK\n");
331375

332-
if (accessToken || apiKey) {
376+
if (accessToken || apiKey || baseUrl || consoleSite || consoleRegion || consoleSwitchAgent) {
333377
server.close();
334378
}
335379
} catch {

packages/cli/src/commands/console/call.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
detectOutputFormat,
88
type Config,
99
type GlobalFlags,
10+
type ConsoleSite,
1011
} from "bailian-cli-core";
1112
import { failIfMissing } from "../../output/prompt.ts";
1213
import { emitResult } from "../../output/output.ts";
@@ -28,7 +29,15 @@ export default defineCommand({
2829
},
2930
{
3031
flag: "--region <region>",
31-
description: "API region (default: cn-beijing)",
32+
description: "Console region (e.g. cn-beijing, ap-southeast-1)",
33+
},
34+
{
35+
flag: "--site <site>",
36+
description: "Console site: domestic or international",
37+
},
38+
{
39+
flag: "--switch-agent <uid>",
40+
description: "Switch agent UID for delegated access",
3241
},
3342
],
3443
examples: [
@@ -50,7 +59,9 @@ export default defineCommand({
5059
process.exit(1);
5160
}
5261

53-
const region = (flags.region as string) || "cn-beijing";
62+
const region = (flags.region as string) || undefined;
63+
const site = ((flags.site as string) || undefined) as ConsoleSite | undefined;
64+
const switchAgent = flags.switchAgent ? Number(flags.switchAgent) : undefined;
5465
const format = detectOutputFormat(config.output);
5566

5667
let token: string | undefined;
@@ -71,6 +82,8 @@ export default defineCommand({
7182
api,
7283
data,
7384
region,
85+
site,
86+
switchAgent,
7487
});
7588

7689
emitResult(result, format);

packages/cli/src/commands/mcp/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default defineCommand({
4343
const type = (flags.type as string) || "OFFICIAL";
4444
const pageNo = (flags.page as number) || 1;
4545
const pageSize = (flags.pageSize as number) || 30;
46-
const region = (flags.region as string) || "cn-beijing";
46+
const region = (flags.region as string) || undefined;
4747
const format = detectOutputFormat(config.output);
4848

4949
const data = {

packages/cli/src/commands/quota/check.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function extractResponseData(result: Record<string, unknown>): Record<string, un
9494
async function fetchAllModelsWithQpm(
9595
config: Config,
9696
token: string,
97-
region: string,
97+
region: string | undefined,
9898
): Promise<ModelWithQpm[]> {
9999
const allModels: ModelWithQpm[] = [];
100100
let pageNo = 1;
@@ -130,7 +130,7 @@ async function fetchAllModelsWithQpm(
130130
async function fetchMonitorData(
131131
config: Config,
132132
token: string,
133-
region: string,
133+
region: string | undefined,
134134
modelName: string,
135135
windowMinutes: number,
136136
): Promise<{ rpm: number; tpm: number }> {
@@ -280,7 +280,7 @@ export default defineCommand({
280280
process.exit(1);
281281
}
282282
const windowMinutes = rawPeriod;
283-
const region = (flags.region as string) || "cn-beijing";
283+
const region = (flags.region as string) || undefined;
284284
const format = detectOutputFormat(config.output);
285285

286286
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/quota/history.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export default defineCommand({
130130
const page = Number(flags.page) || 1;
131131
const pageSize = Number(flags.pageSize) || 10;
132132
const modelFilter = (flags.model as string) || undefined;
133-
const region = (flags.region as string) || "cn-beijing";
133+
const region = (flags.region as string) || undefined;
134134
const format = detectOutputFormat(config.output);
135135

136136
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/quota/list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function extractResponseData(result: Record<string, unknown>): Record<string, un
6868
async function fetchAllModelsWithQpm(
6969
config: Config,
7070
token: string,
71-
region: string,
71+
region: string | undefined,
7272
onlySelfService: boolean,
7373
): Promise<ModelWithQpm[]> {
7474
const allModels: ModelWithQpm[] = [];
@@ -186,7 +186,7 @@ export default defineCommand({
186186
async run(config: Config, flags: GlobalFlags) {
187187
const modelFlag = (flags.model as string) || undefined;
188188
const showAll = Boolean(flags.all);
189-
const region = (flags.region as string) || "cn-beijing";
189+
const region = (flags.region as string) || undefined;
190190
const format = detectOutputFormat(config.output);
191191

192192
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/quota/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function extractResponseData(result: Record<string, unknown>): Record<string, un
5353
async function fetchModelQpmInfo(
5454
config: Config,
5555
token: string,
56-
region: string,
56+
region: string | undefined,
5757
modelName: string,
5858
): Promise<{ model: string; qpmInfo: Record<string, QpmInfoItem> } | undefined> {
5959
const raw = await callConsoleGateway(config, token, {
@@ -122,7 +122,7 @@ export default defineCommand({
122122
}
123123

124124
const autoConfirm = Boolean(flags.yes) || config.yes;
125-
const region = (flags.region as string) || "cn-beijing";
125+
const region = (flags.region as string) || undefined;
126126
const format = detectOutputFormat(config.output);
127127

128128
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/usage/free.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default defineCommand({
232232
);
233233
process.exit(1);
234234
}
235-
const region = (flags.region as string) || "cn-beijing";
235+
const region = (flags.region as string) || undefined;
236236
const format = detectOutputFormat(config.output);
237237

238238
const credential = await resolveConsoleGatewayCredential(config);

packages/cli/src/commands/usage/freetier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async function pollUntilDone(
6363
api: string,
6464
requestKey: string,
6565
models: string[],
66-
region: string,
66+
region: string | undefined,
6767
): Promise<unknown> {
6868
let nextTaskId: string | undefined;
6969

@@ -140,7 +140,7 @@ export default defineCommand({
140140
const modelFlag = (flags.model as string) || undefined;
141141
const all = Boolean(flags.all);
142142
const off = Boolean(flags.off);
143-
const region = (flags.region as string) || "cn-beijing";
143+
const region = (flags.region as string) || undefined;
144144
const format = detectOutputFormat(config.output);
145145

146146
if (!modelFlag && !all) {

0 commit comments

Comments
 (0)