142 lines
5.2 KiB
Go
142 lines
5.2 KiB
Go
// 织忆 MemoryWeave — P3 Peer Representation(t_f8fc97e6,设计 §5 Peer Representation)
|
||
//
|
||
// 借鉴 Honcho「reasoning-first」:记忆 = 实体理解(不是静态文本)。
|
||
// 对一个实体(牧尘/小怡/小雪等)做四层推理画像:
|
||
//
|
||
// explicit 明确陈述 facts / preferences / skills(直接来自 L1 记忆)
|
||
// deductive 演绎推理 inferred_traits / inferred_goals(从具体事实推一般结论)
|
||
// inductive 归纳模式 patterns(跨多条事实归纳出重复模式/趋势)
|
||
// abductive 最佳解释 hypothesized_context(对现状给出最合理解释)
|
||
//
|
||
// 存储沿用 graph.db(与 L2 pattern、L3 skillgen 同库),短命 modernc sqlite 开/关,
|
||
// 不持长连接、不碰 state.db(storage/sqlite.go 打开 memoryweave.db = state.db,
|
||
// 动它违反全仓红线)。表结构见 store.go。
|
||
//
|
||
// 推理引擎接口化:生产复用本地 2B(llama 队列代理 8081,免费腿),测试用确定性
|
||
// fake(零网络 / CGO / 真模型),单测机械可复算。
|
||
package peer
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"github.com/xiaoxue/memoryweave/internal/models"
|
||
)
|
||
|
||
// Peer 对一个实体的综合理解(四层推理画像)。
|
||
// facts/preferences/skills = explicit(明确陈述);inferred_traits/inferred_goals =
|
||
// deductive(演绎);patterns = inductive(归纳);hypothesized_context = abductive(最佳解释)。
|
||
type Peer struct {
|
||
PeerID string `json:"peer_id"`
|
||
Facts []string `json:"facts"`
|
||
Preferences []string `json:"preferences"`
|
||
Skills []string `json:"skills"`
|
||
|
||
// --- Deductive (演绎推理) ---
|
||
InferredTraits []string `json:"inferred_traits"`
|
||
InferredGoals []string `json:"inferred_goals"`
|
||
|
||
// --- Inductive (归纳模式) ---
|
||
Patterns []string `json:"patterns"`
|
||
|
||
// --- Abductive (最佳解释推论) ---
|
||
HypothesizedContext string `json:"hypothesized_context"`
|
||
|
||
UpdatedAt string `json:"updated_at"`
|
||
}
|
||
|
||
// ReasoningOutput 推理引擎产出(deductive/inductive/abductive 三层)。
|
||
// explicit 层由调用方从 L1 记忆直接提取,不经推理引擎。
|
||
type ReasoningOutput struct {
|
||
InferredTraits []string // deductive
|
||
InferredGoals []string // deductive
|
||
Patterns []string // inductive
|
||
HypothesizedContext string // abductive
|
||
}
|
||
|
||
// Reasoner 推理引擎接口。生产用本地 2B,测试用确定性 fake。
|
||
// 输入:explicit 层事实(facts/preferences/skills)+ 图谱上下文(graphContext)。
|
||
type Reasoner interface {
|
||
Reason(facts, preferences, skills, graphContext []string) (*ReasoningOutput, error)
|
||
}
|
||
|
||
// CollectFacts 从 L1 记忆里提取某实体的 explicit 层事实。
|
||
// 纯函数:只读 memories,不改任何状态 —— 单测可机械复算(AC1 输入构造)。
|
||
// 分类判据(确定性):
|
||
// - 含偏好关键词(喜欢/偏好/偏爱/厌恶/倾向/宁/热衷)→ preferences
|
||
// - 含技能关键词(擅长/精通/熟悉/会用/会写/能独立/熟练)→ skills
|
||
// - 其余含实体名的记忆 → facts
|
||
//
|
||
// 返回 matched(实际含实体名的条数),供 AC 判定「造 N 条 → 命中 N 条」。
|
||
func CollectFacts(memories []models.MemoryRecord, peerID string) (facts, prefs, skills []string, matched int) {
|
||
pid := strings.ToLower(strings.TrimSpace(peerID))
|
||
if pid == "" {
|
||
return nil, nil, nil, 0
|
||
}
|
||
var (
|
||
f []string
|
||
pr []string
|
||
sk []string
|
||
)
|
||
for _, m := range memories {
|
||
content := strings.TrimSpace(m.Content)
|
||
if content == "" || !strings.Contains(strings.ToLower(content), pid) {
|
||
continue
|
||
}
|
||
matched++
|
||
switch {
|
||
case isPrefContent(content):
|
||
pr = append(pr, content)
|
||
case isSkillContent(content):
|
||
sk = append(sk, content)
|
||
default:
|
||
f = append(f, content)
|
||
}
|
||
}
|
||
return f, pr, sk, matched
|
||
}
|
||
|
||
// prefKeywords 偏好判据关键词(确定性,宽口径不误伤技能类)。
|
||
var prefKeywords = []string{"喜欢", "偏好", "偏爱", "厌恶", "倾向", "热衷", "宁", "更愿意"}
|
||
|
||
// skillKeywords 技能判据关键词。
|
||
var skillKeywords = []string{"擅长", "精通", "熟悉", "会用", "会写", "能独立", "熟练", "掌握"}
|
||
|
||
func isPrefContent(s string) bool {
|
||
for _, k := range prefKeywords {
|
||
if strings.Contains(s, k) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func isSkillContent(s string) bool {
|
||
for _, k := range skillKeywords {
|
||
if strings.Contains(s, k) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// BuildPeer 核心编排:explicit 层 + 推理引擎 → 四层画像 → 写 peers 表(graph.db)。
|
||
// 非破坏合并语义(non-lossy):
|
||
// - 已存在该 peer → 新 facts/prefs/skills 去重后追加,推理层整体覆盖重算,updated_at 刷新
|
||
// - 不存在 → INSERT
|
||
//
|
||
// 返回写回后的完整 Peer(含四层,可读回校验 AC1)。
|
||
func BuildPeer(store *Store, reasoner Reasoner, peerID string, facts, prefs, skills, graphCtx []string) (*Peer, error) {
|
||
if store == nil || reasoner == nil {
|
||
return nil, fmt.Errorf("peer: store/reasoner 未注入")
|
||
}
|
||
if strings.TrimSpace(peerID) == "" {
|
||
return nil, fmt.Errorf("peer: peer_id 为空")
|
||
}
|
||
out, err := reasoner.Reason(facts, prefs, skills, graphCtx)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("peer: 推理失败: %w", err)
|
||
}
|
||
return store.Upsert(peerID, facts, prefs, skills, out)
|
||
}
|