3c65189f30
后端从占位回显变为真实生成:管理员经控制面登记/激活模型,Gateway 经 NATS 下发,Dispatcher 热更新 LLM Pool,Eino 图用 OpenAI 兼容流式真实推理。 - shared: contract.ModelConfig(provider/base_url/api_key/model) + 配置 subjects; bus.RequestModelConfig/ServeModelConfig/Publish/Subscribe ModelConfigUpdated - gateway: store.LLMModel→sundynix_model(AutoMigrate,唯一激活) + admin REST (GET/POST/active/delete/test models, api_key 脱敏) + main ServeModelConfig + 变更广播; 路由 /api/v1/admin/models* - dispatcher: llm.Pool OpenAI 兼容 SSE 流式客户端(ChatStream) + 热更新配置 + 未配置则降级桩; poolModel.Ready()?真实流式:注入记忆的桩; main 取配置+订阅 - 开发期接在线 API 不拉本地模型(见 llm-provider-strategy memory) - 验证: 4 模块 build✓ + e2e PASS; mock OpenAI 服务 live 跑通——登记/测试连接✓/ 激活→NATS 热更新→提交→真实 SSE 流出 mock 回复, mock 日志证明端点被调用且 注入画像(老王)进了模型上下文 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sundynix/sundynix-gateway/internal/store"
|
|
"github.com/sundynix/sundynix-shared/contract"
|
|
)
|
|
|
|
// 控制面(运维管理):LLM 模型配置 CRUD + 测试连接 + 变更广播。
|
|
// 表 sundynix_model 由 Gateway 持有;Dispatcher 经 NATS 取激活配置。
|
|
|
|
type modelBody struct {
|
|
ID uint `json:"id"`
|
|
Provider string `json:"provider"`
|
|
BaseURL string `json:"base_url"`
|
|
APIKey string `json:"api_key"`
|
|
Model string `json:"model"`
|
|
}
|
|
|
|
// ListModels: GET /api/v1/admin/models —— 列出模型(api_key 脱敏)。
|
|
func (h *Handler) ListModels(c *gin.Context) {
|
|
rows, err := h.db.ListModels(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
out := make([]gin.H, 0, len(rows))
|
|
for _, m := range rows {
|
|
out = append(out, gin.H{
|
|
"id": m.ID, "provider": m.Provider, "base_url": m.BaseURL,
|
|
"model": m.Model, "active": m.Active, "api_key": mask(m.APIKey),
|
|
})
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"models": out})
|
|
}
|
|
|
|
// SaveModel: POST /api/v1/admin/models —— 新增/更新一条模型配置。
|
|
func (h *Handler) SaveModel(c *gin.Context) {
|
|
var b modelBody
|
|
if err := c.ShouldBindJSON(&b); err != nil || b.BaseURL == "" || b.Model == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "provider/base_url/model required"})
|
|
return
|
|
}
|
|
provider := b.Provider
|
|
if provider == "" {
|
|
provider = "openai-compatible"
|
|
}
|
|
m := &store.LLMModel{ID: b.ID, Provider: provider, BaseURL: b.BaseURL, APIKey: b.APIKey, Model: b.Model}
|
|
if err := h.db.SaveModel(c.Request.Context(), m); err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
h.broadcastActiveModel(c.Request.Context())
|
|
c.JSON(http.StatusOK, gin.H{"id": m.ID})
|
|
}
|
|
|
|
// SetActiveModel: POST /api/v1/admin/models/:id/active —— 设为激活并广播。
|
|
func (h *Handler) SetActiveModel(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if err := h.db.SetActiveModel(c.Request.Context(), uint(id)); err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
h.broadcastActiveModel(c.Request.Context())
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok", "active": id})
|
|
}
|
|
|
|
// DeleteModel: DELETE /api/v1/admin/models/:id
|
|
func (h *Handler) DeleteModel(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if err := h.db.DeleteModel(c.Request.Context(), uint(id)); err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
h.broadcastActiveModel(c.Request.Context())
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
}
|
|
|
|
// TestModel: POST /api/v1/admin/models/test —— 探测 OpenAI 兼容端点连通性。
|
|
func (h *Handler) TestModel(c *gin.Context) {
|
|
var b modelBody
|
|
if err := c.ShouldBindJSON(&b); err != nil || b.BaseURL == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required"})
|
|
return
|
|
}
|
|
// 若传了已存的 id 但未带 key,用库里的真实 key。
|
|
key := b.APIKey
|
|
if key == "" && b.ID != 0 {
|
|
if rows, _ := h.db.ListModels(c.Request.Context()); rows != nil {
|
|
for _, m := range rows {
|
|
if m.ID == b.ID {
|
|
key = m.APIKey
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 8*time.Second)
|
|
defer cancel()
|
|
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, b.BaseURL+"/models", nil)
|
|
if key != "" {
|
|
req.Header.Set("Authorization", "Bearer "+key)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"ok": false, "message": err.Error()})
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
c.JSON(http.StatusOK, gin.H{"ok": resp.StatusCode < 400, "message": "HTTP " + resp.Status})
|
|
}
|
|
|
|
// broadcastActiveModel 读当前激活配置并经 NATS 广播,触发 Dispatcher 热更新。
|
|
func (h *Handler) broadcastActiveModel(ctx context.Context) {
|
|
row, _ := h.db.GetActiveModel(ctx)
|
|
if row == nil {
|
|
return
|
|
}
|
|
_ = h.bus.PublishModelConfigUpdated(&contract.ModelConfig{
|
|
Provider: row.Provider, BaseURL: row.BaseURL, APIKey: row.APIKey, Model: row.Model,
|
|
})
|
|
}
|
|
|
|
func mask(s string) string {
|
|
if len(s) <= 4 {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
return "••••"
|
|
}
|
|
return "••••" + s[len(s)-4:]
|
|
}
|