Skip to content

Commit ce1e93a

Browse files
committed
chore: address comments
1 parent 79e2524 commit ce1e93a

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

provider/copilot.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import (
1919
)
2020

2121
const (
22-
copilotBaseURL = "https://api.individual.githubcopilot.com"
22+
copilotBaseURL = "https://api.individual.githubcopilot.com"
23+
24+
// Copilot exposes an OpenAI-compatible API, including for Anthropic models.
2325
routeCopilotChatCompletions = "/copilot/chat/completions"
2426
routeCopilotResponses = "/copilot/responses"
2527
)
@@ -109,11 +111,6 @@ func (p *Copilot) CreateInterceptor(_ http.ResponseWriter, r *http.Request, trac
109111
return nil, fmt.Errorf("missing Copilot authorization: Authorization header not found or invalid")
110112
}
111113

112-
payload, err := io.ReadAll(r.Body)
113-
if err != nil {
114-
return nil, fmt.Errorf("read body: %w", err)
115-
}
116-
117114
id := uuid.New()
118115

119116
// Build config for the interceptor using the per-request key.
@@ -132,7 +129,7 @@ func (p *Copilot) CreateInterceptor(_ http.ResponseWriter, r *http.Request, trac
132129
switch r.URL.Path {
133130
case routeCopilotChatCompletions:
134131
var req chatcompletions.ChatCompletionNewParamsWrapper
135-
if err := json.Unmarshal(payload, &req); err != nil {
132+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
136133
return nil, fmt.Errorf("unmarshal chat completions request body: %w", err)
137134
}
138135

@@ -143,6 +140,10 @@ func (p *Copilot) CreateInterceptor(_ http.ResponseWriter, r *http.Request, trac
143140
}
144141

145142
case routeCopilotResponses:
143+
payload, err := io.ReadAll(r.Body)
144+
if err != nil {
145+
return nil, fmt.Errorf("read body: %w", err)
146+
}
146147
var req responses.ResponsesNewParamsWrapper
147148
if err := json.Unmarshal(payload, &req); err != nil {
148149
return nil, fmt.Errorf("unmarshal responses request body: %w", err)
@@ -177,7 +178,7 @@ func extractBearerToken(auth string) string {
177178
// extractCopilotHeaders extracts headers required by the Copilot API from the
178179
// incoming request. Copilot requires certain client headers to be forwarded.
179180
func extractCopilotHeaders(r *http.Request) map[string]string {
180-
headers := make(map[string]string)
181+
headers := make(map[string]string, len(copilotForwardHeaders))
181182
for _, h := range copilotForwardHeaders {
182183
if v := r.Header.Get(h); v != "" {
183184
headers[h] = v

0 commit comments

Comments
 (0)