P1: GapDetector Redis 持久化 - 13个缺口字段序列化为 JSON 写入 zhiyi:gaps HASH - misses 计数器写入 zhiyi:gap_misses - RecordMiss/Close 后自动 persist + 启动时 loadFromRedis - 重启不丢缺口记录 P2: VPropagator Redis 持久化 + useful/not-useful 接入 - 决策记录写入 zhiyi:vprop_decisions HASH - V 值写入 zhiyi:vprop_values HASH - RecordDecision 后自动 persist - feedback/useful → success, feedback/not-useful → failure P3: 中文分类改进 - classifyGap 降级时检测中文同义词标记(又称/别名/aka) - 含标记的 query 归为 GapSynonym(B) 而非 GapUnknown(A) P4: QualityDropMonitor 阈值 5→2 + 蒸馏质量分 - minFeedbacks 从 5 降至 2,更早触发质量监控 P5: CoOccurTracker Redis 持久化 - RedisCoOccurPersist() 写入 zhiyi:cooccur HASH - RecordRecall 中调用 ct.persist() 持久化每条边 - recall pipeline 中独立 goroutine 触发共访追踪 - LoadCoOccurFromRedis() 启动时加载 测试: 全部 5 项重启验证通过
123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
// 织忆 MemoryWeave — 反馈 API
|
|
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/xiaoxue/memoryweave/internal/selfoptimize"
|
|
"github.com/xiaoxue/memoryweave/internal/storage"
|
|
)
|
|
|
|
type FeedbackAPI struct {
|
|
LanceDB storage.LanceDB
|
|
}
|
|
|
|
func NewFeedbackAPI(ldb storage.LanceDB) *FeedbackAPI {
|
|
return &FeedbackAPI{LanceDB: ldb}
|
|
}
|
|
|
|
// POST /api/v1/feedback/useful
|
|
func (fa *FeedbackAPI) MarkUseful(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
MemoryID string `json:"memory_id"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
respondError(w, 400, "invalid body")
|
|
return
|
|
}
|
|
if req.MemoryID == "" {
|
|
respondError(w, 400, "memory_id required")
|
|
return
|
|
}
|
|
|
|
selfoptimize.Dash.RecordFeedback(true)
|
|
// V 值:useful = success
|
|
selfoptimize.VProp.RecordDecision(
|
|
"useful_"+req.MemoryID,
|
|
[]string{req.MemoryID},
|
|
"user_feedback", "success", "",
|
|
)
|
|
fa.LanceDB.IncrementUseful(req.MemoryID)
|
|
respond(w, 200, map[string]string{"status": "ok", "memory_id": req.MemoryID})
|
|
}
|
|
|
|
// POST /api/v1/feedback/not-useful
|
|
func (fa *FeedbackAPI) MarkNotUseful(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
MemoryID string `json:"memory_id"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
respondError(w, 400, "invalid body")
|
|
return
|
|
}
|
|
if req.MemoryID == "" {
|
|
respondError(w, 400, "memory_id required")
|
|
return
|
|
}
|
|
|
|
selfoptimize.Dash.RecordFeedback(false)
|
|
// V 值:not-useful = failure
|
|
selfoptimize.VProp.RecordDecision(
|
|
"notuseful_"+req.MemoryID,
|
|
[]string{req.MemoryID},
|
|
"user_feedback", "failure", "",
|
|
)
|
|
fa.LanceDB.IncrementNotUseful(req.MemoryID)
|
|
respond(w, 200, map[string]string{"status": "ok", "memory_id": req.MemoryID})
|
|
}
|
|
|
|
// POST /api/v1/feedback/deprecate
|
|
func (fa *FeedbackAPI) Deprecate(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
MemoryID string `json:"memory_id"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
respondError(w, 400, "invalid body")
|
|
return
|
|
}
|
|
if req.MemoryID == "" {
|
|
respondError(w, 400, "memory_id required")
|
|
return
|
|
}
|
|
|
|
selfoptimize.Dash.RecordDeprecation()
|
|
fa.LanceDB.SoftDelete(req.MemoryID, req.Reason)
|
|
respond(w, 200, map[string]string{"status": "deprecated", "memory_id": req.MemoryID})
|
|
}
|
|
|
|
// POST /api/v1/feedback/correct
|
|
func (fa *FeedbackAPI) Correct(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
MemoryID string `json:"memory_id"`
|
|
NewContent string `json:"new_content"`
|
|
Source string `json:"source"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
respondError(w, 400, "invalid body")
|
|
return
|
|
}
|
|
if req.MemoryID == "" || req.NewContent == "" {
|
|
respondError(w, 400, "memory_id and new_content required")
|
|
return
|
|
}
|
|
if req.Source == "" {
|
|
req.Source = "muchen_correction"
|
|
}
|
|
|
|
selfoptimize.Dash.RecordCorrection(req.Source)
|
|
// 记录 V 值决策:用户修正 = success
|
|
selfoptimize.VProp.RecordDecision(
|
|
"correct_"+req.MemoryID,
|
|
[]string{req.MemoryID},
|
|
"user_correct", "success", "",
|
|
)
|
|
err := fa.LanceDB.UpdateMemoryContent(req.MemoryID, req.NewContent, req.Source)
|
|
if err != nil {
|
|
respondError(w, 500, "correct failed: "+err.Error())
|
|
return
|
|
}
|
|
respond(w, 200, map[string]string{"status": "corrected", "memory_id": req.MemoryID})
|
|
}
|