158 lines
3.6 KiB
Go
158 lines
3.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/httputil"
|
|
)
|
|
|
|
type reportRequest struct {
|
|
SummaryID string `json:"summary_id"`
|
|
Excerpts []string `json:"excerpts" binding:"required,min=1"`
|
|
Question string `json:"question" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) CreateReport(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
var req reportRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httputil.BadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
var summaryID *string
|
|
if req.SummaryID != "" {
|
|
summaryID = &req.SummaryID
|
|
}
|
|
|
|
// Joindre les extraits avec un séparateur visuel
|
|
excerpt := buildExcerptContext(req.Excerpts)
|
|
|
|
// Créer le rapport en DB avec status=generating, retourner immédiatement
|
|
report, err := h.repo.CreatePendingReport(userID, summaryID, excerpt, req.Question)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
|
|
// Lancer la génération en arrière-plan
|
|
h.pipeline.GenerateReportAsync(report.ID, excerpt, req.Question, h.reportManager)
|
|
|
|
c.JSON(http.StatusCreated, report)
|
|
}
|
|
|
|
func (h *Handler) ListReports(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
reports, err := h.repo.ListReports(userID)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
httputil.OK(c, reports)
|
|
}
|
|
|
|
func (h *Handler) DeleteReport(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
id := c.Param("id")
|
|
// Annuler la goroutine si elle tourne encore
|
|
h.reportManager.Cancel(id)
|
|
if err := h.repo.DeleteReport(id, userID); err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *Handler) GetGeneratingStatus(c *gin.Context) {
|
|
httputil.OK(c, gin.H{"generating": h.pipeline.IsGenerating()})
|
|
}
|
|
|
|
func (h *Handler) ListReportMessages(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
reportID := c.Param("id")
|
|
|
|
report, err := h.repo.GetReport(reportID, userID)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
if report == nil {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
msgs, err := h.repo.ListReportMessages(reportID)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
httputil.OK(c, msgs)
|
|
}
|
|
|
|
func (h *Handler) CreateReportMessage(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
reportID := c.Param("id")
|
|
|
|
var req struct {
|
|
Content string `json:"content" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httputil.BadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
report, err := h.repo.GetReport(reportID, userID)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
if report == nil || report.Status != "done" {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
// Récupérer l'historique pour le contexte IA
|
|
history, err := h.repo.ListReportMessages(reportID)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
|
|
// Persister le message utilisateur
|
|
userMsg, err := h.repo.CreateReportMessage(reportID, "user", req.Content, "done")
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
|
|
// Créer le message assistant en attente
|
|
assistantMsg, err := h.repo.CreateReportMessage(reportID, "assistant", "", "generating")
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
|
|
// Ajouter le message utilisateur à l'historique avant de lancer la génération
|
|
history = append(history, *userMsg)
|
|
|
|
h.pipeline.GenerateReportMessageAsync(assistantMsg.ID, report, history, h.reportManager)
|
|
|
|
c.JSON(http.StatusCreated, assistantMsg)
|
|
}
|
|
|
|
func buildExcerptContext(excerpts []string) string {
|
|
if len(excerpts) == 1 {
|
|
return excerpts[0]
|
|
}
|
|
var sb strings.Builder
|
|
for i, e := range excerpts {
|
|
if i > 0 {
|
|
sb.WriteString("\n\n---\n\n")
|
|
}
|
|
sb.WriteString(e)
|
|
}
|
|
return sb.String()
|
|
}
|