-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathhandler.go
More file actions
404 lines (350 loc) · 11 KB
/
Copy pathhandler.go
File metadata and controls
404 lines (350 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package instance
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"math"
"net/http"
"strings"
"copilot-go/anthropic"
"copilot-go/config"
"copilot-go/store"
"github.com/gin-gonic/gin"
sdk "github.com/github/copilot-sdk/go"
)
// DoCompletionsProxy performs the upstream request for completions via the official SDK.
// The caller is responsible for closing resp.Body.
func DoCompletionsProxy(c *gin.Context, _ *config.State, sdkClient *sdk.Client, bodyBytes []byte) (*http.Response, error) {
return SDKDoCompletions(c.Request.Context(), sdkClient, bodyBytes)
}
// ForwardCompletionsResponse writes the upstream response to the client.
func ForwardCompletionsResponse(c *gin.Context, resp *http.Response) {
defer func() { _ = resp.Body.Close() }()
contentType := resp.Header.Get("Content-Type")
isStream := strings.Contains(contentType, "text/event-stream")
if isStream {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
c.Status(resp.StatusCode)
reader := bufio.NewReaderSize(resp.Body, 10*1024*1024)
c.Stream(func(w io.Writer) bool {
line, err := reader.ReadBytes('\n')
if len(line) > 0 {
if _, writeErr := w.Write(line); writeErr != nil {
return false
}
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
}
if err != nil {
if err != io.EOF {
log.Printf("Stream read error: %v", err)
}
return false
}
return true
})
} else {
body, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to read response"})
return
}
c.Data(resp.StatusCode, "application/json", body)
}
}
// ModelsHandler returns cached models with display ID mapping. Models the SDK
// accepts in session.create take precedence over the REST /models list, which
// contains entries that cannot actually be used through this proxy.
func ModelsHandler(c *gin.Context, state *config.State) {
state.RLock()
models := state.SDKModels
if models == nil {
models = state.Models
}
state.RUnlock()
if models == nil {
c.JSON(http.StatusOK, config.ModelsResponse{
Object: "list",
Data: []config.ModelEntry{},
})
return
}
mapped := config.ModelsResponse{
Object: models.Object,
Data: make([]config.ModelEntry, len(models.Data)),
}
for i, m := range models.Data {
mapped.Data[i] = config.ModelEntry{
ID: store.ToDisplayID(m.ID),
Object: m.Object,
Created: m.Created,
OwnedBy: m.OwnedBy,
Name: m.Name,
Version: m.Version,
Vendor: m.Vendor,
Capabilities: m.Capabilities,
}
}
c.JSON(http.StatusOK, mapped)
}
// DoEmbeddingsProxy performs the upstream request for embeddings.
func DoEmbeddingsProxy(state *config.State, bodyBytes []byte) (*http.Response, error) {
var payload map[string]interface{}
if err := json.Unmarshal(bodyBytes, &payload); err == nil {
if model, ok := payload["model"].(string); ok {
payload["model"] = store.ToCopilotID(model)
bodyBytes, _ = json.Marshal(payload)
}
}
return ProxyRequestWithBytes(state, "POST", "/embeddings", bodyBytes, nil, false)
}
// ForwardEmbeddingsResponse writes the upstream embeddings response to the client.
func ForwardEmbeddingsResponse(c *gin.Context, resp *http.Response) {
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to read response"})
return
}
c.Data(resp.StatusCode, "application/json", body)
}
// DoMessagesProxy performs the upstream request for Anthropic messages via the official SDK.
// Returns the raw response. bodyBytes is the original Anthropic payload.
func DoMessagesProxy(c *gin.Context, state *config.State, sdkClient *sdk.Client, bodyBytes []byte) (*http.Response, error) {
var anthropicPayload anthropic.AnthropicMessagesPayload
if err := json.Unmarshal(bodyBytes, &anthropicPayload); err != nil {
return nil, fmt.Errorf("invalid request: %v", err)
}
// Auto-fill max_tokens from model capabilities if not provided
if anthropicPayload.MaxTokens == 0 {
copilotModelID := anthropic.NormalizeAnthropicModel(store.ToCopilotID(anthropicPayload.Model))
if limit := lookupMaxOutputTokens(state, copilotModelID); limit > 0 {
anthropicPayload.MaxTokens = limit
}
}
openaiPayload := anthropic.TranslateToOpenAI(anthropicPayload)
openaiBytes, err := json.Marshal(openaiPayload)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %v", err)
}
return SDKDoCompletions(c.Request.Context(), sdkClient, openaiBytes)
}
// ForwardMessagesResponse writes the upstream response to the client in Anthropic format.
// originalBody is the original Anthropic request (used to determine stream mode).
func ForwardMessagesResponse(c *gin.Context, resp *http.Response, originalBody []byte) {
defer func() { _ = resp.Body.Close() }()
var anthropicPayload anthropic.AnthropicMessagesPayload
if err := json.Unmarshal(originalBody, &anthropicPayload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid request: %v", err)})
return
}
if anthropicPayload.Stream {
handleAnthropicStream(c, resp)
} else {
handleAnthropicNonStream(c, resp)
}
}
func handleAnthropicNonStream(c *gin.Context, resp *http.Response) {
body, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to read response"})
return
}
if resp.StatusCode != 200 {
c.Data(resp.StatusCode, "application/json", body)
return
}
var openaiResp anthropic.ChatCompletionResponse
if err := json.Unmarshal(body, &openaiResp); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to parse upstream response"})
return
}
anthropicResp := anthropic.TranslateToAnthropic(openaiResp)
c.JSON(http.StatusOK, anthropicResp)
}
func handleAnthropicStream(c *gin.Context, resp *http.Response) {
// If upstream returned an error, translate it properly instead of trying to SSE-parse
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log.Printf("[Stream] Upstream returned status %d: %s", resp.StatusCode, string(body))
c.Data(resp.StatusCode, "application/json", body)
return
}
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
c.Header("Transfer-Encoding", "chunked")
c.Status(http.StatusOK)
w := c.Writer
flusher, hasFlusher := w.(http.Flusher)
clientGone := c.Request.Context().Done()
state := anthropic.NewStreamState()
scanner := bufio.NewScanner(resp.Body)
scanner.Buffer(make([]byte, 0, 10*1024*1024), 10*1024*1024)
for scanner.Scan() {
select {
case <-clientGone:
log.Printf("[Stream] Client disconnected, stopping stream")
return
default:
}
line := scanner.Text()
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {
if err := writeSSE(w, "message_stop", map[string]string{"type": "message_stop"}); err != nil {
log.Printf("[Stream] Write error on message_stop: %v", err)
return
}
if hasFlusher {
flusher.Flush()
}
return
}
var chunk anthropic.ChatCompletionResponse
if err := json.Unmarshal([]byte(data), &chunk); err != nil {
log.Printf("[Stream] Failed to parse SSE chunk: %v", err)
continue
}
events := anthropic.TranslateChunkToAnthropicEvents(chunk, state)
for _, event := range events {
if err := writeSSE(w, event.Event, event.Data); err != nil {
log.Printf("[Stream] Write error: %v", err)
return
}
}
if hasFlusher {
flusher.Flush()
}
}
if err := scanner.Err(); err != nil {
log.Printf("[Stream] Scanner error: %v", err)
_ = writeSSE(w, "error", map[string]interface{}{
"type": "error",
"error": map[string]string{
"type": "stream_error",
"message": fmt.Sprintf("upstream stream error: %v", err),
},
})
} else {
log.Printf("[Stream] Upstream closed without [DONE], sending message_stop")
_ = writeSSE(w, "message_stop", map[string]string{"type": "message_stop"})
}
if hasFlusher {
flusher.Flush()
}
}
// lookupMaxOutputTokens finds the max_output_tokens for a model from cached capabilities.
func lookupMaxOutputTokens(state *config.State, modelID string) int {
if state == nil || modelID == "" {
return 0
}
state.RLock()
models := state.Models
state.RUnlock()
if models == nil {
return 0
}
for _, m := range models.Data {
if m.ID == modelID && m.Capabilities != nil && m.Capabilities.Limits.MaxOutputTokens > 0 {
return m.Capabilities.Limits.MaxOutputTokens
}
}
return 0
}
func writeSSE(w io.Writer, event string, data interface{}) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "event: %s\ndata: %s\n\n", event, string(jsonData))
return err
}
// CountTokensHandler provides a simplified token count estimation.
func CountTokensHandler(c *gin.Context, _ *config.State) {
anthropicBeta := c.GetHeader("anthropic-beta")
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
return
}
var payload anthropic.AnthropicMessagesPayload
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid request: %v", err)})
return
}
openaiPayload := anthropic.TranslateToOpenAI(payload)
inputTokens, outputTokens := estimateOpenAITokens(openaiPayload)
if len(payload.Tools) > 0 && !hasClaudeCodeMCPTools(anthropicBeta, payload.Tools) {
switch {
case strings.HasPrefix(payload.Model, "claude"):
inputTokens += 346
case strings.HasPrefix(payload.Model, "grok"):
inputTokens += 480
}
}
finalTokenCount := inputTokens + outputTokens
switch {
case strings.HasPrefix(payload.Model, "claude"):
finalTokenCount = int(math.Round(float64(finalTokenCount) * 1.15))
case strings.HasPrefix(payload.Model, "grok"):
finalTokenCount = int(math.Round(float64(finalTokenCount) * 1.03))
}
finalTokenCount = maxTokenCount(finalTokenCount, 1)
c.JSON(http.StatusOK, gin.H{
"input_tokens": finalTokenCount,
})
}
func estimateOpenAITokens(payload anthropic.ChatCompletionsPayload) (int, int) {
inputTokens := 0
outputTokens := 0
for _, msg := range payload.Messages {
tokens := estimateJSONTokens(msg) + 3
if msg.Role == "assistant" {
outputTokens += tokens
} else {
inputTokens += tokens
}
}
if len(payload.Tools) > 0 {
inputTokens += estimateJSONTokens(payload.Tools)
}
return inputTokens, outputTokens
}
func estimateJSONTokens(v interface{}) int {
data, err := json.Marshal(v)
if err != nil {
return 0
}
tokens := len(data) / 4
if tokens < 1 {
return 1
}
return tokens
}
func maxTokenCount(a, b int) int {
if a > b {
return a
}
return b
}
func hasClaudeCodeMCPTools(anthropicBeta string, tools []anthropic.AnthropicTool) bool {
if !strings.HasPrefix(anthropicBeta, "claude-code") {
return false
}
for _, tool := range tools {
if strings.HasPrefix(tool.Name, "mcp__") {
return true
}
}
return false
}