- Add request/response converters (OpenAI <-> Anthropic formats) - Implement SSE streaming conversion (Anthropic events -> OpenAI SSE) - Add /v1/models endpoint with Claude model list - Add /v1/chat/completions endpoint with streaming and non-streaming support - Fix context key type matching bug (sessionIDKey) - Configurable upstream URL via config.yaml - Mimic claude-code CLI headers for upstream requests
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/google/uuid"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func main() {
|
|
// Load config.yaml
|
|
data, err := os.ReadFile("config.yaml")
|
|
if err != nil {
|
|
log.Fatalf("Failed to read config.yaml: %v", err)
|
|
}
|
|
cfg := &Config{}
|
|
if err := yaml.Unmarshal(data, cfg); err != nil {
|
|
log.Fatalf("Failed to parse config.yaml: %v", err)
|
|
}
|
|
config = cfg
|
|
|
|
// Generate session ID (persist across requests)
|
|
sessionID := uuid.New().String()
|
|
|
|
// Register routes
|
|
http.HandleFunc("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
|
|
r = r.WithContext(contextWithSessionIDInContext(r.Context(), sessionID))
|
|
handleChatCompletions(w, r)
|
|
})
|
|
http.HandleFunc("/v1/models", handleModels)
|
|
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprint(w, "OK")
|
|
})
|
|
|
|
addr := fmt.Sprintf(":%d", config.Port)
|
|
log.Printf("Starting proxx on %s, upstream: %s", addr, config.UpstreamURL)
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
log.Fatalf("Server failed: %v", err)
|
|
}
|
|
}
|
|
|
|
// contextKey is a custom type for context keys
|
|
type contextKey string
|
|
|
|
const sessionIDKey contextKey = "sessionID"
|
|
|
|
// contextWithSessionID creates a context with the session ID
|
|
func contextWithSessionID(sessionID string) context.Context {
|
|
return context.WithValue(nil, sessionIDKey, sessionID)
|
|
}
|
|
|
|
// contextWithSessionIDInContext creates a new context with session ID in existing context
|
|
func contextWithSessionIDInContext(parent context.Context, sessionID string) context.Context {
|
|
return context.WithValue(parent, sessionIDKey, sessionID)
|
|
}
|