feat(ai): add personal summary models and retire Q&A
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>
This commit is contained in:
@ -172,6 +172,10 @@ func (h *Handler) SetActiveAIProvider(c *gin.Context) {
|
||||
|
||||
func (h *Handler) DeleteAIProvider(c *gin.Context) {
|
||||
if err := h.repo.DeleteAIProvider(c.Param("id")); err != nil {
|
||||
if errors.Is(err, models.ErrCatalogConflict) {
|
||||
c.JSON(409, gin.H{"error": "provider_in_use"})
|
||||
return
|
||||
}
|
||||
httputil.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
@ -359,7 +363,7 @@ func (h *Handler) GetDefaultSystemPrompt(c *gin.Context) {
|
||||
// ── AI Roles ───────────────────────────────────────────────────────────────
|
||||
|
||||
func (h *Handler) GetAIRoles(c *gin.Context) {
|
||||
roles := []string{"summary", "report", "filter"}
|
||||
roles := []string{"summary"}
|
||||
resp := gin.H{}
|
||||
for _, role := range roles {
|
||||
providerID, _ := h.repo.GetSetting("ai_role_" + role + "_provider")
|
||||
@ -376,8 +380,8 @@ type updateAIRoleRequest struct {
|
||||
|
||||
func (h *Handler) UpdateAIRole(c *gin.Context) {
|
||||
role := c.Param("role")
|
||||
if role != "summary" && role != "report" && role != "filter" {
|
||||
httputil.BadRequest(c, fmt.Errorf("invalid role: must be summary, report, or filter"))
|
||||
if role != "summary" {
|
||||
httputil.BadRequest(c, fmt.Errorf("legacy role is not configurable"))
|
||||
return
|
||||
}
|
||||
var req updateAIRoleRequest
|
||||
|
||||
@ -11,6 +11,10 @@ import (
|
||||
)
|
||||
|
||||
func preferencesError(c *gin.Context, err error) {
|
||||
if errors.Is(err, models.ErrSummaryModelUnavailable) {
|
||||
c.JSON(400, gin.H{"error": "summary_model_unavailable"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httputil.NotFound(c)
|
||||
return
|
||||
|
||||
@ -17,44 +17,7 @@ type reportRequest struct {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
summary, err := h.repo.GetOwnedSummary(req.SummaryID, userID)
|
||||
if err != nil {
|
||||
httputil.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
if summary == nil {
|
||||
httputil.NotFound(c)
|
||||
return
|
||||
}
|
||||
for _, excerpt := range req.Excerpts {
|
||||
if strings.TrimSpace(excerpt) == "" || !strings.Contains(summary.Content, excerpt) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "excerpt must come from the summary"})
|
||||
return
|
||||
}
|
||||
}
|
||||
summaryID := &summary.ID
|
||||
|
||||
// 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(userID, report.ID, excerpt, req.Question, h.reportManager)
|
||||
|
||||
c.JSON(http.StatusCreated, report)
|
||||
c.JSON(http.StatusGone, gin.H{"error": "reports_read_only"})
|
||||
}
|
||||
|
||||
func (h *Handler) ListReports(c *gin.Context) {
|
||||
@ -64,6 +27,14 @@ func (h *Handler) ListReports(c *gin.Context) {
|
||||
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)
|
||||
}
|
||||
|
||||
@ -111,58 +82,16 @@ func (h *Handler) ListReportMessages(c *gin.Context) {
|
||||
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) {
|
||||
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)
|
||||
c.JSON(http.StatusGone, gin.H{"error": "reports_read_only"})
|
||||
}
|
||||
|
||||
func buildExcerptContext(excerpts []string) string {
|
||||
|
||||
95
backend/internal/api/handlers/summary_models.go
Normal file
95
backend/internal/api/handlers/summary_models.go
Normal file
@ -0,0 +1,95 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tradarr/backend/internal/httputil"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
)
|
||||
|
||||
func catalogHTTPError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, models.ErrCatalogConflict):
|
||||
c.JSON(409, gin.H{"error": "summary_model_conflict"})
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
httputil.NotFound(c)
|
||||
default:
|
||||
c.JSON(500, gin.H{"error": "summary model operation failed"})
|
||||
}
|
||||
}
|
||||
func (h *Handler) ListMySummaryModels(c *gin.Context) {
|
||||
rows, err := h.repo.ListSummaryModels(true)
|
||||
if err != nil {
|
||||
catalogHTTPError(c, err)
|
||||
return
|
||||
}
|
||||
type option struct {
|
||||
ID string `json:"id"`
|
||||
Provider string `json:"provider"`
|
||||
Model string `json:"model"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
result := []option{}
|
||||
for _, row := range rows {
|
||||
result = append(result, option{row.ID, row.Provider, row.Model, row.Label})
|
||||
}
|
||||
httputil.OK(c, result)
|
||||
}
|
||||
func (h *Handler) ListSummaryModels(c *gin.Context) {
|
||||
rows, err := h.repo.ListSummaryModels(false)
|
||||
if err != nil {
|
||||
catalogHTTPError(c, err)
|
||||
return
|
||||
}
|
||||
httputil.OK(c, rows)
|
||||
}
|
||||
func (h *Handler) CreateSummaryModel(c *gin.Context) {
|
||||
var req struct {
|
||||
ProviderID string `json:"provider_id" binding:"required,uuid"`
|
||||
Model string `json:"model" binding:"required,max=256"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
}
|
||||
if c.ShouldBindJSON(&req) != nil || strings.TrimSpace(req.Model) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid summary model"})
|
||||
return
|
||||
}
|
||||
enabled := true
|
||||
if req.Enabled != nil {
|
||||
enabled = *req.Enabled
|
||||
}
|
||||
row, err := h.repo.CreateSummaryModel(req.ProviderID, req.Model, enabled)
|
||||
if err != nil {
|
||||
catalogHTTPError(c, err)
|
||||
return
|
||||
}
|
||||
httputil.Created(c, row)
|
||||
}
|
||||
func (h *Handler) UpdateSummaryModel(c *gin.Context) {
|
||||
var req struct {
|
||||
Enabled *bool `json:"enabled" binding:"required"`
|
||||
}
|
||||
if c.ShouldBindJSON(&req) != nil || !assetUUID.MatchString(c.Param("id")) {
|
||||
c.JSON(400, gin.H{"error": "invalid summary model"})
|
||||
return
|
||||
}
|
||||
if err := h.repo.UpdateSummaryModel(c.Param("id"), *req.Enabled); err != nil {
|
||||
catalogHTTPError(c, err)
|
||||
return
|
||||
}
|
||||
httputil.OK(c, gin.H{"ok": true})
|
||||
}
|
||||
func (h *Handler) DeleteSummaryModel(c *gin.Context) {
|
||||
if !assetUUID.MatchString(c.Param("id")) {
|
||||
c.JSON(400, gin.H{"error": "invalid summary model"})
|
||||
return
|
||||
}
|
||||
if err := h.repo.DeleteSummaryModel(c.Param("id")); err != nil {
|
||||
catalogHTTPError(c, err)
|
||||
return
|
||||
}
|
||||
httputil.NoContent(c)
|
||||
}
|
||||
Reference in New Issue
Block a user