98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
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"
|
|
"github.com/tradarr/backend/internal/summarybudget"
|
|
)
|
|
|
|
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 {
|
|
PromptCapacity *summarybudget.Profile `json:"prompt_capacity"`
|
|
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.PromptCapacity, 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)
|
|
}
|