@@ -46,12 +46,37 @@ const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
4646const ANTHROPIC_API_KEY = process . env . ANTHROPIC_API_KEY ;
4747const COPILOT_GITHUB_TOKEN = process . env . COPILOT_GITHUB_TOKEN ;
4848
49+ // Configurable Copilot API target host (supports GHES/GHEC / custom endpoints)
50+ // Priority: COPILOT_API_TARGET env var > auto-derive from GITHUB_SERVER_URL > default
51+ function deriveCopilotApiTarget ( ) {
52+ if ( process . env . COPILOT_API_TARGET ) {
53+ return process . env . COPILOT_API_TARGET ;
54+ }
55+ // For GitHub Enterprise Cloud (*.ghe.com) or GitHub Enterprise Server
56+ // (any GITHUB_SERVER_URL that isn't https://github.com), route to the
57+ // enterprise Copilot API endpoint instead of the individual one.
58+ const serverUrl = process . env . GITHUB_SERVER_URL ;
59+ if ( serverUrl ) {
60+ try {
61+ const hostname = new URL ( serverUrl ) . hostname ;
62+ if ( hostname !== 'github.com' ) {
63+ return 'api.enterprise.githubcopilot.com' ;
64+ }
65+ } catch {
66+ // Invalid URL — fall through to default
67+ }
68+ }
69+ return 'api.githubcopilot.com' ;
70+ }
71+ const COPILOT_API_TARGET = deriveCopilotApiTarget ( ) ;
72+
4973// Squid proxy configuration (set via HTTP_PROXY/HTTPS_PROXY in docker-compose)
5074const HTTPS_PROXY = process . env . HTTPS_PROXY || process . env . HTTP_PROXY ;
5175
5276logRequest ( 'info' , 'startup' , {
5377 message : 'Starting AWF API proxy sidecar' ,
5478 squid_proxy : HTTPS_PROXY || 'not configured' ,
79+ copilot_api_target : COPILOT_API_TARGET ,
5580 providers : {
5681 openai : ! ! OPENAI_API_KEY ,
5782 anthropic : ! ! ANTHROPIC_API_KEY ,
@@ -433,7 +458,7 @@ if (COPILOT_GITHUB_TOKEN) {
433458 const contentLength = parseInt ( req . headers [ 'content-length' ] , 10 ) || 0 ;
434459 if ( checkRateLimit ( req , res , 'copilot' , contentLength ) ) return ;
435460
436- proxyRequest ( req , res , 'api.githubcopilot.com' , {
461+ proxyRequest ( req , res , COPILOT_API_TARGET , {
437462 'Authorization' : `Bearer ${ COPILOT_GITHUB_TOKEN } ` ,
438463 } , 'copilot' ) ;
439464 } ) ;
0 commit comments