4fd44380aa
所有数据库映射结构体收敛到统一基类,清理混乱。
- store/base.go:BaseModel{ID string(雪花,bwmarrin/snowflake) PK, CreatedAt, UpdatedAt,
DeletedAt gorm软删index} + BeforeCreate 生成 id + NewID()。
- 全模型嵌入 BaseModel:User/Task/LLMModel/KB/Doc/Agent(去掉各自 ID uint/CreatedAt);
Task 业务 id(task_xxx)挪到 TaskID 唯一列,主键统一雪花。
- 模型 id uint→string:admin :id 路由、SetActiveModel/DeleteModel/SaveModel、modelBody.ID。
- 一次性迁移 migrateLegacyIntIDs:检测旧整型 id(AutoMigrate 不改主键类型)→ 备份
sundynix_model 行(唯一不可再生的 API 密钥)→ 删旧表 → 按新规约重建 → 回灌模型(新雪花 id)。
其它表(User/Task/KB/Doc/Agent)为可重建测试数据,重置。
- Doc 预埋 Size/Preview/ObjectKey 字段,DocLink 表(为后续 B/C)。
验证:重启 gateway → 日志"回灌 2 条模型配置";PG sundynix_model.id=varchar、有
created_at/updated_at/deleted_at;DeepSeek/百炼 密钥保留(keylen 35);admin 列表返回
雪花 string id + 脱敏 key;健康五灯全绿。gateway build 通过。
注:mcp-go 的 sundynix_user_profile(Profile) 模型尚未套同规约,待跟进对齐。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
154 lines
4.8 KiB
Go
154 lines
4.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"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 string `json:"id"`
|
|
Kind string `json:"kind"`
|
|
Provider string `json:"provider"`
|
|
BaseURL string `json:"base_url"`
|
|
APIKey string `json:"api_key"`
|
|
Model string `json:"model"`
|
|
}
|
|
|
|
// ListModels: GET /api/v1/admin/models?kind=chat|embedding —— 列出模型(api_key 脱敏)。
|
|
func (h *Handler) ListModels(c *gin.Context) {
|
|
rows, err := h.db.ListModels(c.Request.Context(), c.Query("kind"))
|
|
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, "kind": m.Kind, "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"
|
|
}
|
|
kind := b.Kind
|
|
if kind == "" {
|
|
kind = contract.ConfigKindChat
|
|
}
|
|
m := &store.LLMModel{BaseModel: store.BaseModel{ID: b.ID}, Kind: kind, 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.broadcastActive(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 := c.Param("id")
|
|
if err := h.db.SetActiveModel(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
h.broadcastActive(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 := c.Param("id")
|
|
if err := h.db.DeleteModel(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
h.broadcastActive(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 != "" {
|
|
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(), 10*time.Second)
|
|
defer cancel()
|
|
var req *http.Request
|
|
if b.Kind == contract.ConfigKindEmbedding {
|
|
// embedding 端点多无 /models,发一个最小 /embeddings 探测。
|
|
payload, _ := json.Marshal(map[string]any{"model": b.Model, "input": []string{"ping"}})
|
|
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, b.BaseURL+"/embeddings", bytes.NewReader(payload))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
} else {
|
|
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})
|
|
}
|
|
|
|
// broadcastActive 重新广播各 kind 当前激活配置,触发对应消费方热更新。
|
|
func (h *Handler) broadcastActive(ctx context.Context) {
|
|
for _, kind := range []string{contract.ConfigKindChat, contract.ConfigKindEmbedding} {
|
|
row, _ := h.db.GetActiveModel(ctx, kind)
|
|
if row == nil {
|
|
continue
|
|
}
|
|
_ = h.bus.PublishConfigUpdated(kind, &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:]
|
|
}
|