@@ -15,11 +15,11 @@ import (
1515 "time"
1616)
1717
18- const (
19- copilotAPIBase = "https://api.githubcopilot.com "
20- chatCompletionsPath = "/chat/completions"
18+ var copilotAPIBase = "https://api.githubcopilot.com"
19+ var completionsPath = "/completions "
20+ var chatCompletionsPath = "/chat/completions"
2121
22- // Retry configuration for chat completions
22+ const (
2323 maxChatRetries = 3
2424 baseChatRetryDelay = 1 // seconds
2525
@@ -323,41 +323,41 @@ func (s *ProxyService) processProxyRequest(ctx context.Context, w http.ResponseW
323323 return fmt .Errorf ("bad request: empty request body" )
324324 }
325325
326+ var input struct {
327+ Model string `json:"model"`
328+ }
329+ if jsonErr := json .Unmarshal (body , & input ); jsonErr != nil {
330+ return fmt .Errorf ("bad request: invalid JSON: %w" , jsonErr )
331+ }
326332
327- var input struct {
328- Model string `json:"model"`
329- }
330- if jsonErr := json .Unmarshal (body , & input ); jsonErr != nil {
331- return fmt .Errorf ("bad request: invalid JSON: %w" , jsonErr )
332- }
333-
334- // AllowedModels validation
335- if len (s .config .AllowedModels ) > 0 {
336- allowed := false
337- for _ , m := range s .config .AllowedModels {
338- if input .Model == m {
339- allowed = true
340- break
341- }
342- }
343- if ! allowed {
344- return fmt .Errorf ("bad request: model '%s' is not allowed by allowed_models in config" , input .Model )
345- }
346- }
347-
348- // Ensure we have a valid token before making the request
349- if tokenErr := s .authService .EnsureValidToken (s .config ); tokenErr != nil {
350- Error ("Failed to ensure valid token" , "error" , tokenErr )
351- return NewAuthError ("token validation failed" , tokenErr )
352- }
333+ // AllowedModels validation
334+ if len (s .config .AllowedModels ) > 0 {
335+ allowed := false
336+ for _ , m := range s .config .AllowedModels {
337+ if input .Model == m {
338+ allowed = true
339+ break
340+ }
341+ }
342+ if ! allowed {
343+ return fmt .Errorf ("bad request: model '%s' is not allowed by allowed_models in config" , input .Model )
344+ }
345+ }
346+
347+ // Ensure we have a valid token before making the request
348+ if tokenErr := s .authService .EnsureValidToken (s .config ); tokenErr != nil {
349+ Error ("Failed to ensure valid token" , "error" , tokenErr )
350+ return NewAuthError ("token validation failed" , tokenErr )
351+ }
353352
354353 // Create new request to GitHub Copilot
355354 var targetURL string
355+ base := copilotAPIBase
356356 switch r .URL .Path {
357357 case "/v1/completions" :
358- targetURL = copilotAPIBase + "/completions"
358+ targetURL = base + completionsPath
359359 case "/v1/chat/completions" :
360- targetURL = copilotAPIBase + chatCompletionsPath
360+ targetURL = base + chatCompletionsPath
361361 default :
362362 return fmt .Errorf ("unsupported proxy path: %s" , r .URL .Path )
363363 }
@@ -370,9 +370,21 @@ func (s *ProxyService) processProxyRequest(ctx context.Context, w http.ResponseW
370370 }
371371
372372 // Set headers
373+ // Forward content/negotiation headers from client if present; use defaults if missing
374+ headersToProxy := []string {"Content-Type" , "Accept" , "Accept-Encoding" , "TE" }
375+ defaults := map [string ]string {
376+ "Content-Type" : "application/json" ,
377+ "Accept" : "application/json" ,
378+ }
379+ for _ , h := range headersToProxy {
380+ if v := r .Header .Get (h ); v != "" {
381+ req .Header .Set (h , v )
382+ } else if def , ok := defaults [h ]; ok {
383+ req .Header .Set (h , def )
384+ }
385+ }
386+
373387 req .Header .Set ("Authorization" , "Bearer " + s .config .CopilotToken )
374- req .Header .Set ("Content-Type" , "application/json" )
375- req .Header .Set ("Accept" , "application/json" )
376388 req .Header .Set ("User-Agent" , s .config .Headers .UserAgent )
377389 req .Header .Set ("Editor-Version" , s .config .Headers .EditorVersion )
378390 req .Header .Set ("Editor-Plugin-Version" , s .config .Headers .EditorPluginVersion )
0 commit comments