// Code scaffolded by goctl. Safe to edit. // goctl 1.10.1 package chat import ( "encoding/json" "io" "net/http" "cursor-api-proxy/internal/logic/chat" "cursor-api-proxy/internal/svc" "cursor-api-proxy/internal/types" "cursor-api-proxy/pkg/infrastructure/httputil" ) func AnthropicMessagesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Read raw body first rawBody, err := io.ReadAll(r.Body) if err != nil { httputil.WriteJSON(w, 400, map[string]interface{}{ "error": map[string]string{"type": "invalid_request_error", "message": "failed to read body"}, }, nil) return } var req types.AnthropicRequest if err := json.Unmarshal(rawBody, &req); err != nil { httputil.WriteJSON(w, 400, map[string]interface{}{ "error": map[string]string{"type": "invalid_request_error", "message": "invalid JSON body"}, }, nil) return } l := chat.NewAnthropicMessagesLogic(r.Context(), svcCtx) if req.Stream { w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") w.Header().Set("X-Accel-Buffering", "no") _ = l.AnthropicMessagesStream(&req, w, r.Method, r.URL.Path) } else { err := l.AnthropicMessages(&req, w, r.Method, r.URL.Path) if err != nil { httputil.WriteJSON(w, 500, map[string]interface{}{ "error": map[string]string{"type": "api_error", "message": err.Error()}, }, nil) } } } }