@@ -35,7 +35,7 @@ const {
3535 deriveCopilotApiTarget,
3636 copilotTargetRequiresGitHubTokenPrefix,
3737} = require ( './copilot-auth' ) ;
38- const { createProviderOidcAuth } = require ( './cloud-oidc-init' ) ;
38+ const { createProviderOidcAuth, createProviderOidcHeaderResolver } = require ( './cloud-oidc-init' ) ;
3939const { bearerAuthHeaders, withCopilotIntegration } = require ( './auth-headers' ) ;
4040const { URL } = require ( 'url' ) ;
4141const { COPILOT_ENV } = require ( '../provider-env-constants' ) ;
@@ -102,6 +102,16 @@ function createCopilotAdapter(env, deps = {}) {
102102 ? ( body ) => injectByokExtraBodyFields ( body , byokExtraBodyFields )
103103 : null ;
104104 const bodyTransform = composeBodyTransforms ( sanitizedBodyTransform , byokBodyFieldTransform ) ;
105+ const requiresGitHubTokenPrefix = copilotTargetRequiresGitHubTokenPrefix ( rawTarget , env ) ;
106+ const authPrefix = ( requiresGitHubTokenPrefix && ! apiKey ) ? 'token' : 'Bearer' ;
107+ const inferenceAuthHeaderResolver = createProviderOidcHeaderResolver ( {
108+ resolveAuthHeaders,
109+ buildOidcHeaders : ( token ) => withCopilotIntegration ( bearerAuthHeaders ( token ) , integrationId ) ,
110+ buildStaticHeaders : ( ) => withCopilotIntegration ( {
111+ ...( apiKey ? byokExtraHeaders : { } ) ,
112+ 'Authorization' : authPrefix + ' ' + authToken ,
113+ } , integrationId ) ,
114+ } ) ;
105115
106116 // Pre-computed models path used by getModelsFetchConfig and getReflectionInfo.
107117 // For BYOK/custom providers the base path prefix is included (e.g. /api/v1/models
@@ -120,7 +130,7 @@ function createCopilotAdapter(env, deps = {}) {
120130 * @returns {{ url: string, opts: { method: string, headers: Record<string,string> } } & Record<string, unknown> }
121131 */
122132function buildCopilotModelsRequest ( extra = { } ) {
123- const prefix = copilotTargetRequiresGitHubTokenPrefix ( rawTarget , env ) ? 'token' : 'Bearer' ;
133+ const prefix = requiresGitHubTokenPrefix ? 'token' : 'Bearer' ;
124134 return {
125135 url : `https://${ rawTarget } /models` ,
126136 opts : {
@@ -219,24 +229,14 @@ function buildCopilotModelsRequest(extra = {}) {
219229 // Enterprise/Business Copilot API requires 'token <value>' for GitHub OAuth tokens.
220230 // BYOK API keys use 'Bearer' regardless of target.
221231 // Standard api.githubcopilot.com and GHEC (*.ghe.com) also use 'Bearer' for all credentials.
222- const requiresGitHubTokenPrefix = copilotTargetRequiresGitHubTokenPrefix ( rawTarget , env ) ;
223-
224232 const isModelsPath = reqPathname === '/models' || reqPathname . startsWith ( '/models/' ) ;
225233 if ( isModelsPath && req . method === 'GET' && githubToken ) {
226234 // /models always uses the GitHub OAuth token (not BYOK key)
227235 const prefix = requiresGitHubTokenPrefix ? 'token' : 'Bearer' ;
228236 return withCopilotIntegration ( { 'Authorization' : prefix + ' ' + githubToken } , integrationId ) ;
229237 }
230238
231- // For inference: BYOK keys use 'Bearer'; GitHub tokens use 'token' on Enterprise/Business/GHES
232- const authPrefix = ( requiresGitHubTokenPrefix && ! apiKey ) ? 'token' : 'Bearer' ;
233- return resolveAuthHeaders (
234- ( token ) => withCopilotIntegration ( bearerAuthHeaders ( token ) , integrationId ) ,
235- withCopilotIntegration ( {
236- ...( apiKey ? byokExtraHeaders : { } ) ,
237- 'Authorization' : authPrefix + ' ' + authToken ,
238- } , integrationId ) ,
239- ) ;
239+ return inferenceAuthHeaderResolver . resolveHeaders ( ) ;
240240 } ,
241241 bodyTransform,
242242 missingCredentialResponse : {
0 commit comments