diff --git a/sundynix-gateway/internal/handler/kb.go b/sundynix-gateway/internal/handler/kb.go
index f742ba9..7938a08 100644
--- a/sundynix-gateway/internal/handler/kb.go
+++ b/sundynix-gateway/internal/handler/kb.go
@@ -19,6 +19,52 @@ import (
"github.com/sundynix/sundynix-shared/contract"
)
+// rawKB 规整知识库名(去空白,空则 default)—— 注册表里的展示名。
+func rawKB(kb string) string {
+ kb = strings.TrimSpace(kb)
+ if kb == "" {
+ return "default"
+ }
+ return kb
+}
+
+// scopedKB 把知识库名锁进当前用户作用域:"owner/name"。
+// owner 来自身份(X-User-ID),客户端只发库名、发不了 owner,故无法越权查到他人的库。
+func scopedKB(c *gin.Context, kb string) string {
+ return userID(c) + "/" + rawKB(kb)
+}
+
+// KbList: GET /api/v1/kb/list —— 当前用户的知识库列表(按 owner 隔离)。
+func (h *Handler) KbList(c *gin.Context) {
+ rows, err := h.db.ListKB(c.Request.Context(), userID(c))
+ if err != nil {
+ c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
+ return
+ }
+ out := make([]gin.H, 0, len(rows))
+ for _, r := range rows {
+ out = append(out, gin.H{"name": r.Name, "kind": r.Kind})
+ }
+ c.JSON(http.StatusOK, gin.H{"kbs": out})
+}
+
+// KbCreate: POST /api/v1/kb/create {name, kind} —— 新建知识库(folder/project/case/general)。
+func (h *Handler) KbCreate(c *gin.Context) {
+ var body struct {
+ Name string `json:"name"`
+ Kind string `json:"kind"`
+ }
+ if err := c.ShouldBindJSON(&body); err != nil || strings.TrimSpace(body.Name) == "" {
+ c.JSON(http.StatusBadRequest, gin.H{"error": "name required"})
+ return
+ }
+ if err := h.db.EnsureKB(c.Request.Context(), userID(c), rawKB(body.Name), body.Kind); err != nil {
+ c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
+ return
+ }
+ c.JSON(http.StatusOK, gin.H{"name": rawKB(body.Name), "kind": body.Kind})
+}
+
// KbIngest: POST /api/v1/kb/ingest —— 文本入库(异步,返回 job_id;进度经 SSE 看)。
func (h *Handler) KbIngest(c *gin.Context) {
var body struct {
@@ -29,8 +75,9 @@ func (h *Handler) KbIngest(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "text required"})
return
}
+ _ = h.db.EnsureKB(c.Request.Context(), userID(c), rawKB(body.KB), "general")
job := newJobID()
- go h.runIngest(job, body.KB, "", nil, body.Text)
+ go h.runIngest(job, scopedKB(c, body.KB), "", nil, body.Text)
c.JSON(http.StatusAccepted, gin.H{"job_id": job})
}
@@ -54,8 +101,9 @@ func (h *Handler) KbIngestFile(c *gin.Context) {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
+ _ = h.db.EnsureKB(c.Request.Context(), userID(c), rawKB(kb), "general")
job := newJobID()
- go h.runIngest(job, kb, fh.Filename, data, "")
+ go h.runIngest(job, scopedKB(c, kb), fh.Filename, data, "")
c.JSON(http.StatusAccepted, gin.H{"job_id": job, "file": fh.Filename})
}
@@ -223,7 +271,7 @@ func (h *Handler) KbSearch(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "q required"})
return
}
- args := map[string]any{"kb": body.KB, "q": body.Q}
+ args := map[string]any{"kb": scopedKB(c, body.KB), "q": body.Q}
if body.TopK > 0 {
args["topK"] = body.TopK
}
@@ -245,7 +293,7 @@ func (h *Handler) KbSearch(c *gin.Context) {
// KbGraph: GET /api/v1/kb/graph?kb= —— 某知识库的图谱三元组(→ mcp-go kb_graph,Neo4j)。
func (h *Handler) KbGraph(c *gin.Context) {
res, err := h.bus.CallTool(c.Request.Context(), contract.ToolSubjectGo("kb_graph"),
- &contract.ToolCall{Tool: "kb_graph", Args: map[string]any{"kb": c.Query("kb"), "limit": 100}})
+ &contract.ToolCall{Tool: "kb_graph", Args: map[string]any{"kb": scopedKB(c, c.Query("kb")), "limit": 100}})
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
diff --git a/sundynix-gateway/internal/router/router.go b/sundynix-gateway/internal/router/router.go
index 7ef0b28..973368d 100644
--- a/sundynix-gateway/internal/router/router.go
+++ b/sundynix-gateway/internal/router/router.go
@@ -24,6 +24,8 @@ func New(db *store.Postgres, cache *store.Redis, bus *nats.Bus) *gin.Engine {
api.GET("/tasks/:id/stream", h.StreamTask) // 4. SSE/WS 回流 Token Stream
api.GET("/tasks/:id/exec", h.StreamExec) // 4b. SSE 回流执行轨迹事件(运行·观测)
api.PUT("/memory", h.SetMemory) // 偏好记忆登记(→ mcp-go memory_upsert)
+ api.GET("/kb/list", h.KbList) // 当前用户的知识库列表(owner 隔离)
+ api.POST("/kb/create", h.KbCreate) // 新建知识库(项目/案件/文件夹/通用)
api.POST("/kb/ingest", h.KbIngest) // 知识库入库(文本,→ mcp-go kb_ingest)
api.POST("/kb/ingest_file", h.KbIngestFile) // 文件入库(docx/xlsx/pdf… 异步)
api.GET("/kb/ingest/:id/stream", h.KbIngestStream) // 入库进度 SSE(实时监控)
diff --git a/sundynix-gateway/internal/store/model.go b/sundynix-gateway/internal/store/model.go
index 1672e6e..fc78e41 100644
--- a/sundynix-gateway/internal/store/model.go
+++ b/sundynix-gateway/internal/store/model.go
@@ -2,10 +2,49 @@ package store
import (
"context"
+ "time"
"gorm.io/gorm"
+ "gorm.io/gorm/clause"
)
+// KB 是一个知识库(按 owner 隔离 + 按 kind 组织:文件夹/项目/案件/通用)。
+// 表名 sundynix_kb。(owner,name) 唯一 —— 同一用户下知识库名不重复。
+// 向量/全文/图谱实际以 "owner/name" 作分区键,保证只有 owner 能查到自己的库。
+type KB struct {
+ ID uint `gorm:"primaryKey"`
+ Owner string `gorm:"size:64;uniqueIndex:idx_kb_owner_name"`
+ Name string `gorm:"size:64;uniqueIndex:idx_kb_owner_name"`
+ Kind string `gorm:"size:16"` // folder / project / case / general
+ CreatedAt time.Time
+}
+
+func (KB) TableName() string { return "sundynix_kb" }
+
+// ListKB 列出某 owner 的全部知识库(按创建时间)。
+func (p *Postgres) ListKB(ctx context.Context, owner string) ([]KB, error) {
+ if p.db == nil {
+ return nil, nil
+ }
+ var rows []KB
+ err := p.db.WithContext(ctx).Where("owner = ?", owner).Order("id").Find(&rows).Error
+ return rows, err
+}
+
+// EnsureKB 幂等登记一个知识库(已存在则保持,不覆盖 kind)。
+func (p *Postgres) EnsureKB(ctx context.Context, owner, name, kind string) error {
+ if p.db == nil {
+ return nil // 降级模式:不持久化注册表,不阻断入库
+ }
+ if kind == "" {
+ kind = "general"
+ }
+ return p.db.WithContext(ctx).Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "owner"}, {Name: "name"}},
+ DoNothing: true,
+ }).Create(&KB{Owner: owner, Name: name, Kind: kind}).Error
+}
+
// LLMModel 是一个模型后端配置(控制面:管理员在此登记可用模型)。
// 表名 sundynix_model(遵守前缀约定)。每个 kind 同一时刻仅一条 Active=true。
type LLMModel struct {
diff --git a/sundynix-gateway/internal/store/pgsql.go b/sundynix-gateway/internal/store/pgsql.go
index 587657e..c3db995 100644
--- a/sundynix-gateway/internal/store/pgsql.go
+++ b/sundynix-gateway/internal/store/pgsql.go
@@ -34,7 +34,7 @@ func OpenPostgres(dsn string) *Postgres {
log.Printf("[store] postgres 不可用,降级运行(不持久化): %v", err)
return &Postgres{}
}
- if err := db.AutoMigrate(&User{}, &Task{}, &LLMModel{}); err != nil {
+ if err := db.AutoMigrate(&User{}, &Task{}, &LLMModel{}, &KB{}); err != nil {
log.Printf("[store] postgres AutoMigrate 失败,降级运行: %v", err)
return &Postgres{}
}