Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
204 lines
6.5 KiB
Go
204 lines
6.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/catalog"
|
|
"git.kmalloc.org/squid/sonda/internal/naming"
|
|
"git.kmalloc.org/squid/sonda/internal/playlists"
|
|
)
|
|
|
|
// namingHandlers suggest a name for a playlist, a mix or a smart playlist of the signed-in user. A suggestion changes
|
|
// nothing: the client decides what to do with it. The name is Claude's when an admin turned that provider on, and a
|
|
// local one otherwise; only a few genres and artist names go to Claude, never the user or a track title.
|
|
type namingHandlers struct {
|
|
svc namingService
|
|
playlists playlistService
|
|
mixes mixService
|
|
smart smartService
|
|
}
|
|
|
|
type nameRequest struct {
|
|
Lang string `json:"lang"` // "en" or "it"; absent or another: English
|
|
Variant int `json:"variant"` // for a mix or a smart playlist: the songs that were on screen
|
|
}
|
|
|
|
type nameJSON struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
// readNameRequest reads the optional body.
|
|
func readNameRequest(c *gin.Context) (nameRequest, bool) {
|
|
var req nameRequest
|
|
if c.Request.ContentLength != 0 && !bindJSON(c, &req) {
|
|
return req, false
|
|
}
|
|
if req.Variant < 0 || req.Variant > 1_000_000 {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "variant must be a whole number from 0 up")
|
|
return req, false
|
|
}
|
|
return req, true
|
|
}
|
|
|
|
// answer makes the suggestion for the songs and writes it.
|
|
func (h *namingHandlers) answer(c *gin.Context, kind string, tracks []catalog.Track, hint, lang string) {
|
|
ctx := c.Request.Context()
|
|
facts, err := h.svc.FactsOf(ctx, kind, tracks, hint)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
s, err := h.svc.Suggest(ctx, facts, lang)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, nameJSON{Name: s.Name, Description: s.Description, Source: s.Source})
|
|
}
|
|
|
|
// playlistName suggests a name for a playlist, from the first songs of it.
|
|
func (h *namingHandlers) playlistName(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
req, ok := readNameRequest(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
l, err := h.playlists.Items(c.Request.Context(), currentUserID(c), id, playlists.Page{Limit: playlists.MaxLimit})
|
|
if err != nil {
|
|
writePlaylistError(c, err)
|
|
return
|
|
}
|
|
tracks := make([]catalog.Track, 0, len(l.Items))
|
|
for _, it := range l.Items {
|
|
if it.Available {
|
|
tracks = append(tracks, it.Track)
|
|
}
|
|
}
|
|
h.answer(c, "playlist", tracks, "", req.Lang)
|
|
}
|
|
|
|
// mixName suggests a name for a mix, from the songs that variant of it has.
|
|
func (h *namingHandlers) mixName(c *gin.Context) {
|
|
req, ok := readNameRequest(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
_, tracks, err := h.mixes.Get(c.Request.Context(), currentUserID(c), c.Param("id"), req.Variant)
|
|
if err != nil {
|
|
writeMixError(c, err)
|
|
return
|
|
}
|
|
h.answer(c, "mix", tracks, "", req.Lang)
|
|
}
|
|
|
|
// smartName suggests a name for a smart playlist, from its rules and the songs that variant of it gives now.
|
|
func (h *namingHandlers) smartName(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
req, ok := readNameRequest(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
p, tracks, err := h.smart.Tracks(c.Request.Context(), currentUserID(c), id, req.Variant)
|
|
if err != nil {
|
|
writeSmartError(c, err)
|
|
return
|
|
}
|
|
h.answer(c, "smart", tracks, p.Rules.Summary(), req.Lang)
|
|
}
|
|
|
|
// status says whether names are made by themselves, so that a page does not ask for a name that Claude will make.
|
|
func (h *namingHandlers) status(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"auto": h.svc.Auto(c.Request.Context())})
|
|
}
|
|
|
|
// automaticName gives the name and the line of description that Claude makes for the songs, when the page asked for it
|
|
// and the provider is on and answered. Otherwise it gives the name the page chose, with no description: a failure of the
|
|
// provider never stops a playlist from being made.
|
|
func automaticName(c *gin.Context, svc namingService, auto bool, lang, kind string, tracks []catalog.Track, hint, fallback string) (name, description string) {
|
|
if !auto || !svc.Auto(c.Request.Context()) {
|
|
return fallback, ""
|
|
}
|
|
facts, err := svc.FactsOf(c.Request.Context(), kind, tracks, hint)
|
|
if err != nil {
|
|
return fallback, ""
|
|
}
|
|
s, err := svc.Suggest(c.Request.Context(), facts, lang)
|
|
if err != nil || s.Source != naming.SourceClaude {
|
|
return fallback, ""
|
|
}
|
|
return s.Name, s.Description
|
|
}
|
|
|
|
type usageTotalsJSON struct {
|
|
NamesMade int `json:"names_made"`
|
|
NamesKept int `json:"names_kept"`
|
|
Failures int `json:"failures"`
|
|
InputTokens int64 `json:"input_tokens"`
|
|
OutputTokens int64 `json:"output_tokens"`
|
|
CacheReadTokens int64 `json:"cache_read_tokens"`
|
|
CacheWriteTokens int64 `json:"cache_write_tokens"`
|
|
CostUSD float64 `json:"cost_usd"`
|
|
}
|
|
|
|
type usageDayJSON struct {
|
|
Date string `json:"date"`
|
|
usageTotalsJSON
|
|
}
|
|
|
|
type usageJSON struct {
|
|
Days int `json:"days"`
|
|
Model string `json:"model"`
|
|
Estimate bool `json:"estimate"`
|
|
Unpriced bool `json:"unpriced"`
|
|
Totals usageTotalsJSON `json:"totals"`
|
|
Daily []usageDayJSON `json:"daily"`
|
|
}
|
|
|
|
func toUsageTotals(t naming.Totals) usageTotalsJSON {
|
|
return usageTotalsJSON{
|
|
NamesMade: t.Made, NamesKept: t.Kept, Failures: t.Failed,
|
|
InputTokens: t.Usage.Input, OutputTokens: t.Usage.Output, CacheReadTokens: t.Usage.CacheRead, CacheWriteTokens: t.Usage.CacheWrite,
|
|
CostUSD: t.CostUSD,
|
|
}
|
|
}
|
|
|
|
// usage says what the calls to Claude cost over the last days (30 by default): the totals, a line for each day that had
|
|
// calls, and an estimate in dollars worked out from the tokens. It is for admins, and it holds nothing of the facts that
|
|
// were sent.
|
|
func (h *namingHandlers) usage(c *gin.Context) {
|
|
days, ok := queryInt(c, "days")
|
|
if !ok {
|
|
return
|
|
}
|
|
if days == 0 {
|
|
days = 30
|
|
}
|
|
r, err := h.svc.Report(c.Request.Context(), clampInt(days))
|
|
if errors.Is(err, naming.ErrInvalidDays) {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "days must be a whole number from 1 to 365")
|
|
return
|
|
}
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
out := usageJSON{Days: r.Days, Model: r.Model, Estimate: r.Estimate, Unpriced: r.Unpriced, Totals: toUsageTotals(r.Totals), Daily: make([]usageDayJSON, len(r.Daily))}
|
|
for i, d := range r.Daily {
|
|
out.Daily[i] = usageDayJSON{Date: d.Date, usageTotalsJSON: toUsageTotals(d.Totals)}
|
|
}
|
|
c.JSON(http.StatusOK, out)
|
|
}
|