feat(qa): add synthesis conversations with source retention (#52)
This commit is contained in:
99
backend/internal/api/handlers/summary_qa.go
Normal file
99
backend/internal/api/handlers/summary_qa.go
Normal file
@ -0,0 +1,99 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tradarr/backend/internal/httputil"
|
||||
"github.com/tradarr/backend/internal/summaryqa"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (h *Handler) SetQAUseCase(uc summaryqa.UseCase) { h.summaryQA = uc }
|
||||
func qaError(c *gin.Context, err error) {
|
||||
code, status := "qa_unavailable", 503
|
||||
var limited *summaryqa.RateLimitError
|
||||
if errors.Is(err, summaryqa.ErrRateLimited) {
|
||||
seconds := 1
|
||||
if errors.As(err, &limited) {
|
||||
seconds = max(1, limited.RetryAfterSeconds)
|
||||
}
|
||||
c.Header("Retry-After", strconv.Itoa(seconds))
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, summaryqa.ErrRateLimited):
|
||||
code, status = "qa_rate_limited", 429
|
||||
case errors.Is(err, summaryqa.ErrNotFound):
|
||||
code, status = "qa_not_found", 404
|
||||
case errors.Is(err, summaryqa.ErrInvalid):
|
||||
code, status = "qa_invalid_request", 400
|
||||
case errors.Is(err, summaryqa.ErrConflict):
|
||||
code, status = "qa_idempotency_conflict", 409
|
||||
case errors.Is(err, summaryqa.ErrBusy):
|
||||
code, status = "qa_question_in_progress", 409
|
||||
case errors.Is(err, summaryqa.ErrLimit):
|
||||
code, status = "qa_context_limit", 422
|
||||
case errors.Is(err, summaryqa.ErrModel):
|
||||
code, status = "qa_model_unavailable", 409
|
||||
case errors.Is(err, summaryqa.ErrProvider):
|
||||
code, status = "qa_provider_failed", 502
|
||||
}
|
||||
c.JSON(status, gin.H{"error": code})
|
||||
}
|
||||
func (h *Handler) qaReady(c *gin.Context) bool {
|
||||
if !assetUUID.MatchString(c.Param("id")) {
|
||||
qaError(c, summaryqa.ErrNotFound)
|
||||
return false
|
||||
}
|
||||
if h.summaryQA == nil {
|
||||
qaError(c, errors.New("unavailable"))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (h *Handler) GetSummaryConversation(c *gin.Context) {
|
||||
if !h.qaReady(c) {
|
||||
return
|
||||
}
|
||||
result, err := h.summaryQA.Get(c.Request.Context(), c.GetString("userID"), c.Param("id"))
|
||||
if err != nil {
|
||||
qaError(c, err)
|
||||
return
|
||||
}
|
||||
httputil.OK(c, result)
|
||||
}
|
||||
func (h *Handler) AskSummaryQuestion(c *gin.Context) {
|
||||
if !h.qaReady(c) {
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 65536)
|
||||
var q summaryqa.Question
|
||||
decoder := json.NewDecoder(c.Request.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&q) != nil {
|
||||
qaError(c, summaryqa.ErrInvalid)
|
||||
return
|
||||
}
|
||||
if decoder.Decode(new(any)) != io.EOF {
|
||||
qaError(c, summaryqa.ErrInvalid)
|
||||
return
|
||||
}
|
||||
result, err := h.summaryQA.Ask(c.Request.Context(), c.GetString("userID"), c.Param("id"), q)
|
||||
if err != nil {
|
||||
qaError(c, err)
|
||||
return
|
||||
}
|
||||
httputil.OK(c, result)
|
||||
}
|
||||
func (h *Handler) DeleteSummaryConversation(c *gin.Context) {
|
||||
if !h.qaReady(c) {
|
||||
return
|
||||
}
|
||||
if err := h.summaryQA.Delete(c.Request.Context(), c.GetString("userID"), c.Param("id")); err != nil {
|
||||
qaError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(204)
|
||||
}
|
||||
Reference in New Issue
Block a user