新增路由层 (10 files): - graph.go: GET /graph/stats, POST /graph/query, POST /graph/navigate - feedback.go: POST /feedback/useful|not-useful|deprecate|correct - gaps.go: GET /gaps, POST /gaps/detect, POST /gaps/close - conflicts.go: GET /conflicts, POST /conflicts/resolve - agent.go: POST /agents/register, GET /agents - admin.go: DELETE /distilled, GET /memory/versions, POST /admin/forget|backup, GET /admin/audit - l3.go: GET|POST /l3/worldmodel - triggers.go: GET /triggers, POST /triggers/fire, GET /skills, POST /skills/trial - eval.go: POST /eval/run, GET /eval/history, POST /eval/generate - tuning.go: GET /tuning/status, POST /tuning/run, POST /tuning/analytics - obsidian.go: POST /obsidian/push|pull, GET /obsidian/status 引擎增强: - governance: InMemoryGraph (零外部依赖) + ListActive/Resolve/DB - selfoptimize: RecordDeprecation/Correction/GapClosed/ConflictResolved - storage: IncrementUseful/NotUseful, UpdateMemoryContent, GetVersionHistory, GetCandidatesForForgetting, Backup, GetAuditLog 总计: 17 files, 1915+ lines, 35 endpoints, go build 0 errors
170 lines
5.3 KiB
Go
170 lines
5.3 KiB
Go
// HTTP 服务器 — 路由注册与启动
|
|
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/xiaoxue/memoryweave/internal/api/middleware"
|
|
"github.com/xiaoxue/memoryweave/internal/api/routes"
|
|
"github.com/xiaoxue/memoryweave/internal/governance"
|
|
"github.com/xiaoxue/memoryweave/internal/selfoptimize"
|
|
"github.com/xiaoxue/memoryweave/internal/storage"
|
|
)
|
|
|
|
// NewServer 创建已注册全部路由的 ServeMux
|
|
func NewServer() http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
// ─── 初始化依赖 ──────────────────────────────
|
|
ldb := storage.NewLanceClient()
|
|
emb := storage.NewEmbedder(os.Getenv("VLLM_ENDPOINT"))
|
|
rerank := storage.NewReranker(os.Getenv("RERANK_ENDPOINT"))
|
|
|
|
// 核心 API
|
|
api := routes.NewAPI(ldb, emb, rerank)
|
|
|
|
// 知识图谱(零外部依赖 = 内存实现)
|
|
graphStore := governance.NewInMemoryGraph()
|
|
graphAPI := routes.NewGraphAPI(graphStore)
|
|
|
|
// 冲突检测器
|
|
conflictDetector := governance.NewConflictDetector()
|
|
conflictAPI := routes.NewConflictAPI(conflictDetector)
|
|
|
|
// 自优化
|
|
gapDetector := selfoptimize.NewGapDetector()
|
|
gapAPI := routes.NewGapAPI(gapDetector)
|
|
|
|
// 反馈
|
|
feedbackAPI := routes.NewFeedbackAPI(ldb)
|
|
|
|
// 管理
|
|
forgetter := governance.NewForgetter()
|
|
adminAPI := routes.NewAdminAPI(ldb, forgetter)
|
|
|
|
// Agent 注册
|
|
agentRegistry := routes.NewAgentRegistry(nil)
|
|
|
|
// 评估
|
|
evalAPI := routes.NewEvalAPI(api.Pipeline, ldb)
|
|
|
|
// L3
|
|
l3API := routes.WM
|
|
|
|
// ─── 路由注册 ──────────────────────────────
|
|
|
|
// 公共路由
|
|
mux.HandleFunc("/health", routes.HandleHealth)
|
|
|
|
// M3: 核心 API
|
|
mux.HandleFunc("/api/v1/commit", api.Commit)
|
|
mux.HandleFunc("/api/v1/recall", api.Recall)
|
|
mux.HandleFunc("/api/v1/bootstrap", api.Bootstrap)
|
|
mux.HandleFunc("/api/v1/stats", api.Stats)
|
|
mux.HandleFunc("/api/v1/batch-commit", api.BatchCommit)
|
|
|
|
// SSE 实时推送
|
|
mux.HandleFunc("/api/v1/ws/", routes.SSEBus.SSEHandler)
|
|
|
|
// 知识图谱
|
|
mux.HandleFunc("/api/v1/graph/stats", graphAPI.Stats)
|
|
mux.HandleFunc("/api/v1/graph/query", graphAPI.Query)
|
|
mux.HandleFunc("/api/v1/graph/navigate", graphAPI.Navigate)
|
|
|
|
// 冲突管理
|
|
mux.HandleFunc("/api/v1/conflicts", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" {
|
|
conflictAPI.List(w, r)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
mux.HandleFunc("/api/v1/conflicts/resolve", conflictAPI.Resolve)
|
|
|
|
// 反馈
|
|
mux.HandleFunc("/api/v1/feedback/useful", feedbackAPI.MarkUseful)
|
|
mux.HandleFunc("/api/v1/feedback/not-useful", feedbackAPI.MarkNotUseful)
|
|
mux.HandleFunc("/api/v1/feedback/deprecate", feedbackAPI.Deprecate)
|
|
mux.HandleFunc("/api/v1/feedback/correct", feedbackAPI.Correct)
|
|
|
|
// 知识缺口
|
|
mux.HandleFunc("/api/v1/gaps", gapAPI.List)
|
|
mux.HandleFunc("/api/v1/gaps/detect", gapAPI.Detect)
|
|
mux.HandleFunc("/api/v1/gaps/close/", gapAPI.Close)
|
|
|
|
// Agent 注册
|
|
mux.HandleFunc("/api/v1/agents/register", agentRegistry.Register)
|
|
mux.HandleFunc("/api/v1/agents", agentRegistry.List)
|
|
|
|
// 管理端点
|
|
mux.HandleFunc("/api/v1/admin/consolidate", routes.HandleConsolidate)
|
|
mux.HandleFunc("/api/v1/admin/forget", adminAPI.Forget)
|
|
mux.HandleFunc("/api/v1/admin/backup", adminAPI.Backup)
|
|
mux.HandleFunc("/api/v1/admin/audit", adminAPI.Audit)
|
|
mux.HandleFunc("/api/v1/distilled/", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "DELETE" {
|
|
adminAPI.DeleteDistilled(w, r)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
mux.HandleFunc("/api/v1/memory/", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" {
|
|
adminAPI.Versions(w, r)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
|
|
// L3 世界模型
|
|
mux.HandleFunc("/api/v1/l3/worldmodel", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" {
|
|
l3API.GetHandler(w, r)
|
|
} else if r.Method == "POST" {
|
|
l3API.UpdateHandler(w, r)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
|
|
// 触发器
|
|
mux.HandleFunc("/api/v1/triggers", routes.Triggers.List)
|
|
mux.HandleFunc("/api/v1/triggers/fire", routes.Triggers.Fire)
|
|
|
|
// Skill 结晶
|
|
mux.HandleFunc("/api/v1/skills", routes.Skills.List)
|
|
mux.HandleFunc("/api/v1/skills/", func(w http.ResponseWriter, r *http.Request) {
|
|
routes.Skills.Trial(w, r)
|
|
})
|
|
|
|
// 评估
|
|
mux.HandleFunc("/api/v1/eval/run", evalAPI.Run)
|
|
mux.HandleFunc("/api/v1/eval/history", evalAPI.History)
|
|
mux.HandleFunc("/api/v1/eval/generate", evalAPI.Generate)
|
|
|
|
// 自调参
|
|
mux.HandleFunc("/api/v1/tuning/status", routes.Tuner.StatusHandler)
|
|
mux.HandleFunc("/api/v1/tuning/run", routes.Tuner.RunHandler)
|
|
mux.HandleFunc("/api/v1/tuning/analytics", routes.Tuner.AnalyticsHandler)
|
|
|
|
// 自优化仪表盘
|
|
mux.HandleFunc("/api/v1/metrics/self", func(w http.ResponseWriter, r *http.Request) {
|
|
metrics := selfoptimize.Dash.Metrics()
|
|
data, _ := json.Marshal(metrics)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(data)
|
|
})
|
|
|
|
// Obsidian 同步
|
|
obsidian := routes.NewObsidianSyncer("", ldb)
|
|
mux.HandleFunc("/api/v1/obsidian/push", obsidian.PushHandler)
|
|
mux.HandleFunc("/api/v1/obsidian/pull", obsidian.PullHandler)
|
|
mux.HandleFunc("/api/v1/obsidian/status", obsidian.StatusHandler)
|
|
|
|
log.Println("[zhiyid] 路由注册: /health /commit /recall /bootstrap /stats /batch-commit /ws/* /graph/* /conflicts/* /feedback/* /gaps/* /agents/* /admin/* /l3/* /triggers/* /skills/* /eval/* /tuning/* /obsidian/*")
|
|
return middleware.Auth(mux)
|
|
}
|