1010 * Base path: OPENAI_API_BASE_PATH (default: /v1 for the public endpoint)
1111 */
1212
13- const { createBaseAdapterConfig, createAdapterMethods } = require ( '../proxy-utils' ) ;
13+ const {
14+ createBaseAdapterConfig,
15+ createAdapterMethods,
16+ normalizeBasePath,
17+ } = require ( '../proxy-utils' ) ;
1418const { OidcTokenProvider } = require ( '../oidc-token-provider' ) ;
1519
20+ function parseByokBaseUrl ( baseUrl ) {
21+ if ( ! baseUrl ) return { target : undefined , basePath : '' } ;
22+ const trimmed = baseUrl . trim ( ) ;
23+ if ( ! trimmed ) return { target : undefined , basePath : '' } ;
24+
25+ const candidate = / ^ [ a - z A - Z ] [ a - z A - Z \d + \- . ] * : \/ \/ / . test ( trimmed )
26+ ? trimmed
27+ : `https://${ trimmed } ` ;
28+ try {
29+ const parsed = new URL ( candidate ) ;
30+ return {
31+ target : parsed . hostname || undefined ,
32+ basePath : normalizeBasePath ( parsed . pathname ) ,
33+ } ;
34+ } catch {
35+ return { target : undefined , basePath : '' } ;
36+ }
37+ }
38+
1639/**
1740 * Create the OpenAI provider adapter.
1841 *
@@ -21,12 +44,21 @@ const { OidcTokenProvider } = require('../oidc-token-provider');
2144 * @returns {import('./index').ProviderAdapter }
2245 */
2346function createOpenAIAdapter ( env , deps = { } ) {
24- const { apiKey, rawTarget, basePath : explicitBasePath } = createBaseAdapterConfig ( env , {
47+ const { apiKey : openaiApiKey , rawTarget : openaiTarget , basePath : openaiBasePath } = createBaseAdapterConfig ( env , {
2548 keyEnvVar : 'OPENAI_API_KEY' ,
2649 targetEnvVar : 'OPENAI_API_TARGET' ,
2750 basePathEnvVar : 'OPENAI_API_BASE_PATH' ,
2851 defaultTarget : 'api.openai.com' ,
2952 } ) ;
53+ const providerType = ( env . COPILOT_PROVIDER_TYPE || '' ) . trim ( ) . toLowerCase ( ) ;
54+ const copilotAzureByokEnabled = providerType === 'azure' ;
55+ const copilotByokApiKey = ( env . COPILOT_PROVIDER_API_KEY || '' ) . trim ( ) || undefined ;
56+ const { target : copilotByokTarget , basePath : copilotByokBasePath } = parseByokBaseUrl ( env . COPILOT_PROVIDER_BASE_URL ) ;
57+
58+ const apiKey = openaiApiKey || ( copilotAzureByokEnabled ? copilotByokApiKey : undefined ) ;
59+ const explicitOpenAITarget = env . OPENAI_API_TARGET ? openaiTarget : undefined ;
60+ const rawTarget = explicitOpenAITarget || ( copilotAzureByokEnabled ? copilotByokTarget : undefined ) || 'api.openai.com' ;
61+ const explicitBasePath = openaiBasePath || ( copilotAzureByokEnabled ? copilotByokBasePath : '' ) ;
3062
3163 // For the default OpenAI endpoint, unversioned clients (e.g. Codex CLI sending
3264 // /responses) need a /v1 prefix to reach the correct versioned API surface.
@@ -175,7 +207,7 @@ function createOpenAIAdapter(env, deps = {}) {
175207 }
176208 return {
177209 statusCode : 404 ,
178- body : { error : 'OpenAI proxy not configured (no OPENAI_API_KEY or OIDC auth)' } ,
210+ body : { error : 'OpenAI proxy not configured (no OPENAI_API_KEY/COPILOT_PROVIDER_API_KEY or OIDC auth)' } ,
179211 } ;
180212 } ,
181213 } ;
0 commit comments