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
19 changes: 9 additions & 10 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as e2b from 'e2b'

import { getUserConfig, UserConfig } from './user'
import { asBold, asPrimary } from './utils/format'
import { getRegionDomain, getRegionApiUrl } from './commands/region'
import { getRegionDomain, getRegionApiUrl, DEFAULT_REGION } from './commands/region'

export let apiKey = process.env.AGENTBOX_API_KEY
export let accessToken = process.env.AGENTBOX_ACCESS_TOKEN
Expand Down Expand Up @@ -88,21 +88,20 @@ export function ensureAccessToken() {
}

const userConfig = getUserConfig()
const region = userConfig?.region || DEFAULT_REGION

// Inject region-based domain/apiUrl into env so SDK internal calls (Sandbox.list, etc.) also use them
if (userConfig?.region) {
if (!process.env.UCLOUD_SANDBOX_DOMAIN) {
process.env.UCLOUD_SANDBOX_DOMAIN = getRegionDomain(userConfig.region)
}
if (!process.env.UCLOUD_SANDBOX_API_URL) {
process.env.UCLOUD_SANDBOX_API_URL = getRegionApiUrl(userConfig.region)
}
if (!process.env.UCLOUD_SANDBOX_DOMAIN) {
process.env.UCLOUD_SANDBOX_DOMAIN = getRegionDomain(region)
}
if (!process.env.UCLOUD_SANDBOX_API_URL) {
process.env.UCLOUD_SANDBOX_API_URL = getRegionApiUrl(region)
}

export const connectionConfig = new e2b.ConnectionConfig({
accessToken: process.env.AGENTBOX_ACCESS_TOKEN || userConfig?.accessToken,
apiKey: process.env.AGENTBOX_API_KEY || userConfig?.teamApiKey,
domain: process.env.UCLOUD_SANDBOX_DOMAIN || getRegionDomain(userConfig?.region),
apiUrl: process.env.UCLOUD_SANDBOX_API_URL || getRegionApiUrl(userConfig?.region),
domain: process.env.UCLOUD_SANDBOX_DOMAIN || getRegionDomain(region),
apiUrl: process.env.UCLOUD_SANDBOX_API_URL || getRegionApiUrl(region),
})
export const client = new e2b.ApiClient(connectionConfig)
10 changes: 6 additions & 4 deletions src/commands/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as chalk from 'chalk'

import { getUserConfig, USER_CONFIG_PATH } from 'src/user'

export const DEFAULT_REGION = 'cn-wlcb'

const REGIONS = [
{ id: 'cn-wlcb', name: 'cn-wlcb (China - North)' },
{ id: 'us-ca', name: 'us-ca (US - West)' },
Expand All @@ -13,13 +15,13 @@ const REGIONS = [
type RegionId = (typeof REGIONS)[number]['id']

export function getRegionDomain(region?: string): string {
if (!region) return 'sandbox.ucloudai.com'
return `${region}.sandbox.ucloudai.com`
const r = region || DEFAULT_REGION
return `${r}.sandbox.ucloudai.com`
}

export function getRegionApiUrl(region?: string): string {
if (!region) return 'https://api.sandbox.ucloudai.com'
return `https://api.${region}.sandbox.ucloudai.com`
const r = region || DEFAULT_REGION
return `https://api.${r}.sandbox.ucloudai.com`
}

function switchRegion(region: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface UserConfig {
export const USER_CONFIG_PATH = path.join(os.homedir(), '.ucloud-sandbox-cli', 'config.json')
export const DOCS_BASE =
process.env.UCLOUD_SANDBOX_DOCS_BASE ||
`https://${process.env.UCLOUD_SANDBOX_DOMAIN || 'sandbox.ucloudai.com'}/docs`
`https://${process.env.UCLOUD_SANDBOX_DOMAIN || 'cn-wlcb.sandbox.ucloudai.com'}/docs`

export function getUserConfig(): UserConfig | null {
if (!fs.existsSync(USER_CONFIG_PATH)) return null
Expand Down