43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.10.1
|
|
|
|
package chat
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"cursor-api-proxy/internal/logic/chat"
|
|
"cursor-api-proxy/internal/svc"
|
|
"cursor-api-proxy/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
)
|
|
|
|
func ChatCompletionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req types.ChatCompletionRequest
|
|
if err := httpx.Parse(r, &req); err != nil {
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
return
|
|
}
|
|
|
|
l := chat.NewChatCompletionsLogic(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")
|
|
err := l.ChatCompletionsStream(&req, w)
|
|
if err != nil {
|
|
w.Write([]byte("event: error\ndata: " + err.Error() + "\n\n"))
|
|
}
|
|
} else {
|
|
resp, err := l.ChatCompletions(&req)
|
|
if err != nil {
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
} else {
|
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
|
}
|
|
}
|
|
}
|
|
}
|