Add an admin-managed provider model catalog with per-user summary selection, migration backfill, and generation snapshots. Retire report creation and messaging while preserving owner-scoped history. Refs #15 #16 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/httputil"
|
|
)
|
|
|
|
type reportRequest struct {
|
|
SummaryID string `json:"summary_id" binding:"required,uuid"`
|
|
Excerpts []string `json:"excerpts" binding:"required,min=1"`
|
|
Question string `json:"question" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) CreateReport(c *gin.Context) {
|
|
c.JSON(http.StatusGone, gin.H{"error": "reports_read_only"})
|
|
}
|
|
|
|
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
|
|
}
|
|
// Historical generating records have no worker after Q&A retirement.
|
|
// Project an honest interrupted state; never rewrite the stored history.
|
|
for i := range reports {
|
|
if reports[i].Status == "generating" {
|
|
reports[i].Status = "error"
|
|
reports[i].ErrorMsg = "historical_generation_interrupted"
|
|
}
|
|
}
|
|
httputil.OK(c, reports)
|
|
}
|
|
|
|
func (h *Handler) DeleteReport(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
id := c.Param("id")
|
|
err := h.reportManager.WithReport(userID, id, func() error {
|
|
if err := h.repo.DeleteReport(id, userID); err != nil {
|
|
return err
|
|
}
|
|
h.reportManager.CancelReport(userID, id)
|
|
return nil
|
|
})
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
httputil.NotFound(c)
|
|
return
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "report deletion failed"})
|
|
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
|
|
}
|
|
for i := range msgs {
|
|
if msgs[i].Status == "generating" {
|
|
msgs[i].Status = "error"
|
|
}
|
|
}
|
|
httputil.OK(c, msgs)
|
|
}
|
|
|
|
func (h *Handler) CreateReportMessage(c *gin.Context) {
|
|
c.JSON(http.StatusGone, gin.H{"error": "reports_read_only"})
|
|
}
|
|
|
|
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()
|
|
}
|