first commit

This commit is contained in:
2026-06-14 20:31:10 +08:00
parent c33b143176
commit 1ed3f576fa
51 changed files with 3362 additions and 810 deletions

View File

@@ -1,9 +1,7 @@
package handlers
import (
"encoding/json"
"net/http"
"os"
"github.com/gin-gonic/gin"
"sproutclaw-web/internal/config"
@@ -13,27 +11,23 @@ import (
// RegisterModels registers model-related routes.
func RegisterModels(r *gin.RouterGroup, cfg *config.Config, piClient *rpc.Client) {
r.GET("/models", handleGetModels(cfg, piClient))
r.GET("/models", handleGetModels(piClient))
r.POST("/model", handleSetModel(piClient))
r.POST("/thinking", handleSetThinking(piClient))
}
func handleGetModels(cfg *config.Config, piClient *rpc.Client) gin.HandlerFunc {
func handleGetModels(piClient *rpc.Client) gin.HandlerFunc {
return func(c *gin.Context) {
resp, err := piClient.SendCmd(models.RPCCommand{Type: "list_models"})
resp, err := piClient.SendCmd(models.RPCCommand{Type: "get_available_models"})
if err != nil {
// fallback: read models.json
data, ferr := os.ReadFile(cfg.ModelsConfigFile)
if ferr != nil {
c.JSON(http.StatusOK, models.ModelsResponse{Models: []models.ModelEntry{}})
return
}
var raw any
_ = json.Unmarshal(data, &raw)
c.JSON(http.StatusOK, raw)
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
return
}
c.JSON(http.StatusOK, jsonRaw(resp.Result))
if !resp.Success {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: resp.Error})
return
}
c.JSON(http.StatusOK, jsonRaw(resp.Data))
}
}
@@ -53,7 +47,11 @@ func handleSetModel(piClient *rpc.Client) gin.HandlerFunc {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
return
}
c.JSON(http.StatusOK, jsonRaw(resp.Result))
if !resp.Success {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: resp.Error})
return
}
c.JSON(http.StatusOK, gin.H{"model": jsonRaw(resp.Data)})
}
}
@@ -65,13 +63,17 @@ func handleSetThinking(piClient *rpc.Client) gin.HandlerFunc {
return
}
resp, err := piClient.SendCmd(models.RPCCommand{
Type: "set_thinking",
Budget: req.Budget,
Type: "set_thinking_level",
Level: req.Level,
})
if err != nil {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
return
}
c.JSON(http.StatusOK, jsonRaw(resp.Result))
if !resp.Success {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: resp.Error})
return
}
c.JSON(http.StatusOK, models.OKResponse{OK: true})
}
}