diff --git a/packages/cli/src/utils/apiPreconnect.test.ts b/packages/cli/src/utils/apiPreconnect.test.ts index df80290a4..de4af8539 100644 --- a/packages/cli/src/utils/apiPreconnect.test.ts +++ b/packages/cli/src/utils/apiPreconnect.test.ts @@ -95,6 +95,39 @@ describe('apiPreconnect', () => { expect(mockFetch).not.toHaveBeenCalled(); }); + it('should accept DashScope regional endpoint (sg-singapore)', () => { + preconnectApi('openai', { + resolvedBaseUrl: + 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1', + }); + expect(mockFetch).toHaveBeenCalledWith( + 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1', + expect.objectContaining({ method: 'HEAD' }), + ); + }); + + it('should accept DashScope regional endpoint (us-virginia)', () => { + preconnectApi('openai', { + resolvedBaseUrl: + 'https://dashscope-us.aliyuncs.com/compatible-mode/v1', + }); + expect(mockFetch).toHaveBeenCalledWith( + 'https://dashscope-us.aliyuncs.com/compatible-mode/v1', + expect.objectContaining({ method: 'HEAD' }), + ); + }); + + it('should accept DashScope regional endpoint (cn-hongkong)', () => { + preconnectApi('openai', { + resolvedBaseUrl: + 'https://cn-hongkong.dashscope.aliyuncs.com/compatible-mode/v1', + }); + expect(mockFetch).toHaveBeenCalledWith( + 'https://cn-hongkong.dashscope.aliyuncs.com/compatible-mode/v1', + expect.objectContaining({ method: 'HEAD' }), + ); + }); + it('should fall back to default URL when resolvedBaseUrl is undefined', () => { preconnectApi('qwen-oauth'); expect(mockFetch).toHaveBeenCalledWith( diff --git a/packages/cli/src/utils/apiPreconnect.ts b/packages/cli/src/utils/apiPreconnect.ts index 9a16e338d..c42f3922e 100644 --- a/packages/cli/src/utils/apiPreconnect.ts +++ b/packages/cli/src/utils/apiPreconnect.ts @@ -21,12 +21,16 @@ import { getOrCreateSharedDispatcher, } from '@qwen-code/qwen-code-core'; +import { ALIBABA_STANDARD_API_KEY_ENDPOINTS } from '../constants/alibabaStandardApiKey.js'; + const debugLogger = createDebugLogger('PRECONNECT'); let preconnectFired = false; /** - * Default API base URLs by AuthType + * Default API base URLs by AuthType. + * DashScope regional endpoints are derived from ALIBABA_STANDARD_API_KEY_ENDPOINTS + * so preconnect covers all supported regions (cn-beijing, sg-singapore, us-virginia, cn-hongkong). */ const DEFAULT_BASE_URLS: Record = { openai: 'https://api.openai.com', @@ -35,6 +39,15 @@ const DEFAULT_BASE_URLS: Record = { dashscope: 'https://dashscope.aliyuncs.com', }; +/** + * All known default base URLs, including DashScope regional endpoints. + * Used by isDefaultBaseUrl() to accept any supported default endpoint. + */ +const ALL_DEFAULT_URLS: string[] = [ + ...Object.values(DEFAULT_BASE_URLS), + ...Object.values(ALIBABA_STANDARD_API_KEY_ENDPOINTS), +]; + /** * Check if preconnect should be skipped due to environment conditions */ @@ -64,7 +77,7 @@ function isDefaultBaseUrl(baseUrl: string): boolean { .toLowerCase() .replace(/^https?:\/\//, '') .replace(/\/+$/, ''); - return Object.values(DEFAULT_BASE_URLS).some((defaultUrl) => { + return ALL_DEFAULT_URLS.some((defaultUrl) => { const normalizedDefault = defaultUrl .toLowerCase() .replace(/^https?:\/\//, '')