WeKnora/internal/handler/system_model_catalog.go

55 lines
1.8 KiB
Go
Raw Normal View History

feat(models): add system admin model catalog console (#3662) * feat(models): add system admin model catalog console System administrators can now maintain the model catalog from Settings → System administration → Model catalog instead of editing models.json on every replica. Administrator changes are stored as a versioned overlay in the database (new model_catalog_config table), layered between the deployment file and explicit per-model settings, and synchronised to other instances within about 5 seconds. Backend - GET/POST preview/PUT /system/admin/model-catalog with optimistic version + deployment baseline checks (409 on conflict), 20-version history and audit logging. - Console overlays reject credentials, headers, env interpolation and non-inline icons; those stay deployment-owned. - Catalog views include each chat model's resolved thinking levels and the vendor defaults, so the console never re-implements level inheritance. Frontend - Model list in the shared data-table style with provider/type filters, "modified only" filter and source tags (built-in / deployment / admin). - Edit drawer saves and applies immediately: display name, context window, max output, input modalities, reasoning, thinking levels, embedding dimension and picker visibility, each with its inherited default and a layer comparison table. - Add-model drawer, JSON editor with server-side change check before publishing, and version history with one-step restore. - Model settings gets a system-admin shortcut to the catalog; model editor reloads providers when opened so published catalogs apply. * fix(models): harden the admin model catalog console - Reject console base_url, base_urls, url_patterns and auth: moving a vendor's default URL would send its deployment api_key (and keys of rows without their own base URL) to another host. With every interpolated field now deployment-owned, '$' in names and descriptions is plain text. - Drop deployment base URLs from catalog views; they may carry interpolated tokens. - Remember a stored version this replica cannot compile and skip it until the version changes, instead of re-reading and recompiling every poll. - Resolve vendor default thinking levels with reasoning forced on, like per-model levels. - Compute builtin/deployment views once; preview returns only the candidate catalog and normalized overlay. - Share deployment file lookup via runtime.ReadDeploymentOverlay. - The model editor force-refreshes its own type instead of resetting the provider store for every type; the refreshed list wins in the id index.
2026-09-24 08:31:45 +00:00
package handler
import (
"errors"
"net/http"
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/application/service"
"github.com/gin-gonic/gin"
)
// GetModelCatalog returns the catalog layers, overlay and version history.
func (h *SystemHandler) GetModelCatalog(c *gin.Context) {
state, err := h.modelCatalogSvc.State(c.Request.Context())
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Model catalog is unavailable"})
return
}
c.JSON(http.StatusOK, state)
}
// PreviewModelCatalog validates an overlay and returns the candidate catalog.
func (h *SystemHandler) PreviewModelCatalog(c *gin.Context) { h.changeModelCatalog(c, false) }
// PublishModelCatalog validates, persists and applies an overlay.
func (h *SystemHandler) PublishModelCatalog(c *gin.Context) { h.changeModelCatalog(c, true) }
func (h *SystemHandler) changeModelCatalog(c *gin.Context, publish bool) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, service.MaxCatalogOverlayBytes+4096)
var req service.CatalogUpdate
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid model catalog request (maximum 1 MiB)"})
return
}
var state *service.CatalogState
var err error
if publish {
state, err = h.modelCatalogSvc.Publish(c.Request.Context(), req)
} else {
state, err = h.modelCatalogSvc.Preview(c.Request.Context(), req)
}
if err != nil {
switch {
case errors.Is(err, repository.ErrCatalogVersionConflict):
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
case errors.Is(err, service.ErrInvalidCatalog):
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update model catalog"})
}
return
}
c.JSON(http.StatusOK, state)
}