@@ -12,9 +12,51 @@ import (
1212
1313// registerRoutes sets up the routing for the server.
1414func (s * Server ) registerRoutes (router * http.ServeMux ) {
15+
16+ router .HandleFunc ("/v1/models" , s .modelsHandler ())
17+ router .HandleFunc ("/models" , s .modelsHandler ())
1518 router .HandleFunc ("/" , s .proxyHandler ())
1619}
1720
21+ // modelsHandler forwards models requests to Copilot API
22+ func (s * Server ) modelsHandler () http.HandlerFunc {
23+ return func (w http.ResponseWriter , r * http.Request ) {
24+ s .logger .Info ("Models request" , "method" , r .Method , "path" , r .URL .Path )
25+
26+ r .URL .Path = "/models"
27+
28+ // Forward the request to the Copilot client
29+ upstreamResp , err := s .copilotClient .ForwardRequest (r .Context (), r )
30+ if err != nil {
31+ s .logger .Error ("Models request failed" , "error" , err )
32+ http .Error (w , "Failed to proxy models request" , http .StatusBadGateway )
33+ return
34+ }
35+ defer upstreamResp .Body .Close ()
36+
37+ if upstreamResp .StatusCode != http .StatusOK {
38+ bodyBytes , err := io .ReadAll (upstreamResp .Body )
39+ if err != nil {
40+ s .logger .Error ("Failed to read models upstream error response body" , "error" , err )
41+ http .Error (w , "Failed to read upstream error response" , http .StatusBadGateway )
42+ return
43+ }
44+
45+ s .logger .Error ("Models upstream request returned non-OK status" ,
46+ "status" , upstreamResp .Status ,
47+ "body" , string (bodyBytes ))
48+
49+ // Forward the response to the client
50+ w .WriteHeader (upstreamResp .StatusCode )
51+ w .Write (bodyBytes )
52+ return
53+ }
54+
55+ // Stream the response back to the original client
56+ httpstreaming .StreamResponse (w , upstreamResp , s .logger )
57+ }
58+ }
59+
1860// proxyHandler is the main handler for all incoming requests.
1961func (s * Server ) proxyHandler () http.HandlerFunc {
2062 return func (w http.ResponseWriter , r * http.Request ) {
0 commit comments