Files
memoryweave/go/internal/api/routes/patterns.go
T

108 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 织忆 MemoryWeave — L2 模式 API(t_d32e5f8a)
//
// 设计 §4.1 端点:
// POST /api/v1/patterns/detect 手动触发模式检测(扫 L1 蒸馏记忆 → 工具组合频率 + 路径聚类
// → 写 graph_nodes type='pattern')。短命单发:modernc sqlite
// 开/关不占长连接、不碰 state.db(规避当年 daemon main_loop
// 退役根因)—— 可挂 cron 单发。
// GET /api/v1/patterns 列出检测到的模式(按 source_tasks 长度降序)。
package routes
import (
"encoding/json"
"net/http"
"os"
"strconv"
"github.com/xiaoxue/memoryweave/internal/models"
"github.com/xiaoxue/memoryweave/internal/pattern"
"github.com/xiaoxue/memoryweave/internal/storage"
)
// PatternAPI L2 模式端点。
type PatternAPI struct {
Store *pattern.GraphPatternStore
Provider *pattern.PatternProvider
LDB func() storage.LanceDB // 扫 L1 记忆(distilled)
Cfg pattern.Config
}
// NewPatternAPI 构造 L2 模式端点。
func NewPatternAPI(store *pattern.GraphPatternStore, provider *pattern.PatternProvider, ldb func() storage.LanceDB) *PatternAPI {
cfg := pattern.DefaultConfig()
// cron 单发跑大批量时可放宽扫描上限
if v := os.Getenv("ZHIYI_PATTERN_MAX_RECORDS"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
cfg.MaxRecords = n
}
}
return &PatternAPI{Store: store, Provider: provider, LDB: ldb, Cfg: cfg}
}
// GET /api/v1/patterns — 列出所有检测到的模式(AC1:空库返回 count=0 不报错)。
func (pa *PatternAPI) List(w http.ResponseWriter, r *http.Request) {
limit := pa.Cfg.TopKPatternList
if v := r.URL.Query().Get("limit"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
limit = n
}
}
pats, err := pa.Store.ListPatterns(limit)
if err != nil {
respondError(w, 500, "list patterns: "+err.Error())
return
}
respond(w, 200, map[string]interface{}{
"patterns": pats,
"count": len(pats),
})
}
// POST /api/v1/patterns/detect — 手动触发模式检测。
// body 可选 {"limit": N, "namespace": "hermes-main"}。
// 流程:LanceDB Scan(limit, ns) → pattern.DetectFromRecords → UpsertPatterns(graph_nodes)。
// 空库/无命中 → created/updated=0,不报错(AC1)。
func (pa *PatternAPI) Detect(w http.ResponseWriter, r *http.Request) {
var req struct {
Limit int `json:"limit"`
Namespace string `json:"namespace"`
}
if r.Body != nil {
_ = json.NewDecoder(r.Body).Decode(&req) // 空 body 也合法(用默认)
}
if req.Limit <= 0 {
req.Limit = pa.Cfg.MaxRecords
}
ldb := pa.LDB()
if ldb == nil {
respondError(w, 503, "lancedb unavailable")
return
}
records, err := ldb.Scan(req.Limit, req.Namespace)
if err != nil {
respondError(w, 500, "scan memories: "+err.Error())
return
}
// 只拿蒸馏产物(L1 蒸馏记忆)做检测——原始对话/episode 不是「已验证」的解决路径
// (与召回管线 2.5 步同源判据:raw/episodes 类不参与模式沉淀)。
distilled := make([]models.MemoryRecord, 0, len(records))
for _, rec := range records {
if rec.Category == "distilled" || rec.Category == "" {
distilled = append(distilled, rec)
}
}
pats := pattern.DetectFromRecords(distilled, pa.Cfg)
created, updated, uerr := pa.Store.UpsertPatterns(pats)
if uerr != nil {
respondError(w, 500, "upsert patterns: "+uerr.Error())
return
}
respond(w, 200, map[string]interface{}{
"scanned": len(records),
"created": created,
"updated": updated,
"detected": len(pats),
"patterns": pats,
})
}