54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/ai"
|
|
"net/http"
|
|
)
|
|
|
|
func (h *Handler) TestSummaryModel(c *gin.Context) {
|
|
// Defence against accidental panic logging by the outer recovery middleware.
|
|
defer func() {
|
|
if recover() != nil {
|
|
c.AbortWithStatusJSON(500, gin.H{"status": "unavailable"})
|
|
}
|
|
}()
|
|
// A no-body action: reject parameters without reading an unbounded/slow body.
|
|
if !assetUUID.MatchString(c.Param("id")) || c.Request.ContentLength != 0 || len(c.Request.TransferEncoding) > 0 || c.Request.URL.RawQuery != "" || c.Request.URL.ForceQuery {
|
|
c.JSON(http.StatusBadRequest, gin.H{"status": "unavailable"})
|
|
return
|
|
}
|
|
if h.modelTester == nil {
|
|
c.JSON(500, gin.H{"status": "unavailable"})
|
|
return
|
|
}
|
|
err := h.modelTester.Test(c.Request.Context(), c.Param("id"))
|
|
code, status := http.StatusOK, "success"
|
|
switch {
|
|
case err == nil:
|
|
case errors.Is(err, ai.ErrModelTestEmpty):
|
|
status = "empty_response"
|
|
case errors.Is(err, ai.ErrModelTestAuth):
|
|
status = "auth_rejected"
|
|
case errors.Is(err, ai.ErrModelTestModelUnavailable):
|
|
status = "model_unavailable"
|
|
case errors.Is(err, ai.ErrModelTestRateLimited):
|
|
status = "rate_limited"
|
|
case errors.Is(err, ai.ErrModelTestTimeout):
|
|
status = "timeout"
|
|
case errors.Is(err, ai.ErrModelTestUnavailable):
|
|
status = "unavailable"
|
|
case errors.Is(err, ai.ErrModelTestNotFound):
|
|
code = 404
|
|
status = "model_unavailable"
|
|
case errors.Is(err, ai.ErrModelTestBusy):
|
|
code = 409
|
|
status = "unavailable"
|
|
default:
|
|
code = 500
|
|
status = "unavailable"
|
|
}
|
|
c.JSON(code, gin.H{"status": status})
|
|
}
|