mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 17:38:10 +00:00
背景:此前媒体是靠「生成的描述文本」将就进记忆的——写 marker 进正文、 再由正则反解成 media_refs 与图库里的 type=Media 实体。这条链路有三个 致命缺陷:描述由异步模型生成(未生成前媒体等于不存在)、语义检索实质上 只搜描述文字、图库里的「媒体节点」是描述文本的投影而不是媒体本身。 本提交把这条链路整体拆除,媒体改为按自己的原生向量参与记忆: 一、描述链彻底删除(无残留、无兼容分支) - media.Item 去掉 Description/DescribedBy 与对应列; - 删除 Store.Describe / Store.Search / Store.Pending; - 删除 Agent.mediaDescribeLoop / describePendingMedia 与配置项 core.memory.media.describe_on_ingest; - SDK 侧 MediaAttachment 去掉 Description(见 SDK 仓独立提交)。 二、marker 机制删除,媒体归属改为结构化块边 - 删除 mediaMarkerLine/parseMediaMarkers/mediaEntityName/mediaTriplesFromText/ extractMediaDigests/sentenceWithMediaMarkers/docMediaContext; - memory.Triple 新增 MediaDigests 结构化字段;句子文本保持原样, 不再被 marker 污染; - 块以 sentence --contains--> block / document --contains--> block 结构边 挂到承载节点(新增 documents 表与 document 节点种类); - 模型未给原句时用「主谓宾。」拼一句自然语言作落点,不造 marker 文本。 三、旧数据迁移(幂等) - 新增 GraphDB.MigrateLegacyMediaEntities:把 type=Media 的旧实体按短 digest 还原成原生块、挂回原句子、删除旧实体与描述关系;Agent 启动时执行; - CleanupOrphanedSentences 同时看关系引用与块边,避免把只靠块存活的句子 连同块边一起删掉。 四、向量融合:媒体按图本身被召回 - 新增 vector.FuseVectors(逐维求和 + L2 归一化); - Doc.DenseVec = 文本向量 ⊕ 文档块的媒体向量(同 fingerprint 才融合), 新增 Doc.DenseFP,指纹变化触发重算; - ContextEvent.DenseVec 同理融合事件块;事件新增 DenseFP,Prune 只在 同一统一空间内比稠密余弦; - 跨模态视觉路只召回「仍被某层记忆块持有」的媒体,CAS 全库字节不再 直接充当记忆检索结果。 五、同时纳入本分支既有的嵌入基础改造(此前工作区未提交,缺它 HEAD 不可构建) - internal/tfidf 懒回退包、千问三段式多模态 ONNX 空间的 Go 侧 (qwen/embedder.go、image.go、model_input.go)、CLIP 移除、 sdk.NewStore 分词器签名与调用点、embed 侧车 systemd 单元。 验证:go build ./... 、go vet ./...(含 -tags medialive)均通过; 在 HEAD 的独立 worktree 上重放本次暂存集后 go test -short ./internal/... 全部通过(端口冲突类用例在隔离环境中亦通过)。未提交工作区中与本改造 无关的改动(HarmonyOS、waiter、devicebridge、plan.md 等)。
470 lines
11 KiB
Go
470 lines
11 KiB
Go
package document
|
||
|
||
import (
|
||
"os"
|
||
"testing"
|
||
"time"
|
||
|
||
"gitcode.com/JianFeeeee/HomeAgent/internal/memory"
|
||
)
|
||
|
||
func TestInsertAndQuery(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_test_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
if err := s.Start(); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer s.Stop()
|
||
|
||
doc := &Doc{
|
||
Summary: "用户喜欢喝咖啡",
|
||
Content: "用户提到他每天早上都会喝一杯黑咖啡",
|
||
Tags: []string{"咖啡", "习惯"},
|
||
Source: "manual",
|
||
}
|
||
if err := s.Insert(doc); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
if doc.ID == "" {
|
||
t.Error("doc ID should be auto-generated")
|
||
}
|
||
|
||
stats := s.Stats()
|
||
if stats["doc_count"].(int) != 1 {
|
||
t.Errorf("expected 1 doc, got %d", stats["doc_count"])
|
||
}
|
||
}
|
||
|
||
func TestQuery(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_query_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
s.Insert(&Doc{Summary: "咖啡是一种饮品", Content: "咖啡因提神", Source: "manual"})
|
||
s.Insert(&Doc{Summary: "茶叶也有咖啡因", Content: "茶和咖啡都提神", Source: "manual"})
|
||
s.Insert(&Doc{Summary: "今天天气很好", Content: "适合出去散步", Source: "manual"})
|
||
|
||
results := s.Query("咖啡", 5)
|
||
if len(results) == 0 {
|
||
t.Fatal("expected results for '咖啡'")
|
||
}
|
||
|
||
if results[0].AccessCount <= 0 {
|
||
t.Error("access count should be updated on query")
|
||
}
|
||
}
|
||
|
||
func TestContextToDoc(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_ctx_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
entries := []ContextEntry{
|
||
{Timestamp: time.Now(), Source: "user", Content: "我喜欢编程", Response: "很好"},
|
||
{Timestamp: time.Now(), Source: "user", Content: "特别是Go语言", Response: "Go很棒"},
|
||
}
|
||
|
||
doc, err := s.ContextToDoc("test", entries, nil, nil, nil, nil)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if doc == nil {
|
||
t.Fatal("expected non-nil doc")
|
||
}
|
||
if doc.Summary == "" {
|
||
t.Error("summary should not be empty")
|
||
}
|
||
if doc.Content == "" {
|
||
t.Error("content should not be empty")
|
||
}
|
||
}
|
||
|
||
func TestFindColdDocs(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_cold_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
hot := &Doc{Summary: "常用的信息", Content: "经常被查询", Source: "manual"}
|
||
hot.AccessCount = 10
|
||
hot.LastAccess = time.Now()
|
||
s.Insert(hot)
|
||
|
||
cold := &Doc{Summary: "很久没用的信息", Content: "几乎不被访问", Source: "manual"}
|
||
s.Insert(cold)
|
||
// Insert 会重置 LastAccess,手动改为过去的
|
||
cold.LastAccess = time.Now().Add(-100 * time.Hour)
|
||
cold.AccessCount = 1
|
||
|
||
// 应该只找到 cold(72h 前未访问且访问 ≤ 2)
|
||
coldDocs := s.FindColdDocs(72*time.Hour, 2)
|
||
if len(coldDocs) != 1 {
|
||
t.Fatalf("expected 1 cold doc, got %d", len(coldDocs))
|
||
}
|
||
if coldDocs[0].Summary != "很久没用的信息" {
|
||
t.Errorf("expected cold doc, got %s", coldDocs[0].Summary)
|
||
}
|
||
}
|
||
|
||
func TestRecentDocs(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_recent_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
s.Insert(&Doc{Summary: "第一条", Content: "a", Source: "manual"})
|
||
time.Sleep(time.Millisecond)
|
||
s.Insert(&Doc{Summary: "第二条", Content: "b", Source: "manual"})
|
||
|
||
recent := s.RecentDocs(1)
|
||
if len(recent) != 1 {
|
||
t.Fatalf("expected 1 recent doc, got %d", len(recent))
|
||
}
|
||
if recent[0].Summary != "第二条" {
|
||
t.Errorf("expected newest doc, got %s", recent[0].Summary)
|
||
}
|
||
}
|
||
|
||
func TestReindex(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_reindex_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
s.Insert(&Doc{Summary: "测试重索引", Content: "验证索引重建", Source: "manual"})
|
||
s.Reindex()
|
||
|
||
results := s.Query("重索引", 5)
|
||
if len(results) == 0 {
|
||
t.Error("reindex should preserve searchability")
|
||
}
|
||
}
|
||
|
||
func TestSummarizeEntries(t *testing.T) {
|
||
entries := []ContextEntry{
|
||
{Source: "user", Content: "今天天气如何"},
|
||
{Source: "user", Content: "明天会下雨吗"},
|
||
}
|
||
summary := summarizeEntries(entries, func(s string) string { return s }, nil, nil)
|
||
if summary == "" {
|
||
t.Error("summary should not be empty")
|
||
}
|
||
if !contains(summary, "2") {
|
||
t.Errorf("summary should mention count, got: %s", summary)
|
||
}
|
||
}
|
||
|
||
func TestExtractKeywords(t *testing.T) {
|
||
kws := memory.ExtractKeywords("今天天气很好")
|
||
if len(kws) == 0 {
|
||
t.Error("should extract keywords from Chinese text")
|
||
}
|
||
}
|
||
|
||
func TestExtractTags(t *testing.T) {
|
||
entries := []ContextEntry{
|
||
{Content: "我喜欢喝咖啡和编程"},
|
||
}
|
||
tags := extractTags(entries, func(s string) string { return s }, nil, nil)
|
||
if len(tags) == 0 {
|
||
t.Error("should extract tags")
|
||
}
|
||
}
|
||
|
||
func TestInsertEmptyDoc(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_empty_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
doc := &Doc{Summary: "", Content: "", Source: "manual"}
|
||
if err := s.Insert(doc); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if doc.ID == "" {
|
||
t.Error("doc ID should be generated even for empty content")
|
||
}
|
||
}
|
||
|
||
func TestPersistence(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_persist_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
// 写
|
||
s1 := NewStore(dir, memory.TokenizeWords)
|
||
s1.Start()
|
||
s1.Insert(&Doc{Summary: "持久化测试", Content: "应该被保存到磁盘", Source: "manual"})
|
||
s1.Stop()
|
||
|
||
// 读
|
||
s2 := NewStore(dir, memory.TokenizeWords)
|
||
s2.Start()
|
||
defer s2.Stop()
|
||
|
||
stats := s2.Stats()
|
||
if stats["doc_count"].(int) != 1 {
|
||
t.Errorf("expected 1 doc after reload, got %d", stats["doc_count"])
|
||
}
|
||
|
||
results := s2.Query("持久化", 5)
|
||
if len(results) == 0 {
|
||
t.Error("search should work after reload")
|
||
}
|
||
}
|
||
|
||
func contains(s, substr string) bool {
|
||
for i := 0; i <= len(s)-len(substr); i++ {
|
||
if s[i:i+len(substr)] == substr {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func TestFlushNoDirty(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_flush_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
|
||
// 不插任何文档,flush 不应报错
|
||
s.Stop()
|
||
}
|
||
|
||
func TestRemove(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_remove_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
s.Insert(&Doc{Summary: "会被删除", Content: "a", Source: "manual"})
|
||
s.Insert(&Doc{Summary: "会保留", Content: "b", Source: "manual"})
|
||
|
||
// 删除前应该有 2 个
|
||
stats := s.Stats()
|
||
if stats["doc_count"].(int) != 2 {
|
||
t.Fatalf("expected 2 docs before remove, got %d", stats["doc_count"])
|
||
}
|
||
|
||
// 遍历找到 "会被删除" 的 ID
|
||
var rmID string
|
||
for _, d := range s.docs {
|
||
if d.Summary == "会被删除" {
|
||
rmID = d.ID
|
||
break
|
||
}
|
||
}
|
||
if rmID == "" {
|
||
t.Fatal("could not find test doc")
|
||
}
|
||
|
||
s.Remove(rmID)
|
||
|
||
stats = s.Stats()
|
||
if stats["doc_count"].(int) != 1 {
|
||
t.Errorf("expected 1 doc after remove, got %d", stats["doc_count"])
|
||
}
|
||
|
||
// 搜索不应再找到
|
||
results := s.Query("删除", 5)
|
||
if len(results) > 0 {
|
||
t.Error("removed doc should not appear in search results")
|
||
}
|
||
}
|
||
|
||
func TestRemoveNonexistent(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_rm_nonexist_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
s.Insert(&Doc{Summary: "一个文档", Content: "x", Source: "manual"})
|
||
|
||
// 删除不存在的 ID 不应 panic
|
||
s.Remove("nonexistent_id")
|
||
|
||
stats := s.Stats()
|
||
if stats["doc_count"].(int) != 1 {
|
||
t.Errorf("expected 1 doc after remove nonexistent, got %d", stats["doc_count"])
|
||
}
|
||
}
|
||
|
||
func TestSummarizeEntriesWithToolCleanFn(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
toolCleanFn func(name, output string) string
|
||
wantTopics []string
|
||
notTopics []string
|
||
}{
|
||
{
|
||
name: "nil toolCleanFn uses raw output",
|
||
toolCleanFn: nil,
|
||
wantTopics: []string{"手机", "电脑"},
|
||
notTopics: nil,
|
||
},
|
||
{
|
||
name: "NoMemory returns empty skips tool output",
|
||
toolCleanFn: func(name, output string) string {
|
||
return ""
|
||
},
|
||
wantTopics: nil,
|
||
notTopics: []string{"手机", "电脑"},
|
||
},
|
||
{
|
||
name: "Cleaner applies filter",
|
||
toolCleanFn: func(name, output string) string {
|
||
return "电脑 编程"
|
||
},
|
||
wantTopics: []string{"电脑", "编程"},
|
||
notTopics: nil,
|
||
},
|
||
}
|
||
|
||
entry := ContextEntry{
|
||
Content: "今天天气",
|
||
ToolResults: []ToolResultItem{
|
||
{Name: "test_tool", Output: "手机 电脑"},
|
||
},
|
||
}
|
||
|
||
for _, tc := range tests {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
summary := summarizeEntries([]ContextEntry{entry}, func(s string) string { return s }, tc.toolCleanFn, nil)
|
||
for _, w := range tc.wantTopics {
|
||
if !contains(summary, w) {
|
||
t.Errorf("summary should contain %q, got: %s", w, summary)
|
||
}
|
||
}
|
||
for _, n := range tc.notTopics {
|
||
if contains(summary, n) {
|
||
t.Errorf("summary should NOT contain %q, got: %s", n, summary)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestExtractTagsWithToolCleanFn(t *testing.T) {
|
||
entries := []ContextEntry{
|
||
{
|
||
Content: "对话",
|
||
ToolResults: []ToolResultItem{
|
||
{Name: "search", Output: "编程和咖啡"},
|
||
},
|
||
},
|
||
}
|
||
|
||
// toolCleanFn 返回 "" → NoMemory,工具输出被跳过
|
||
tagsSkip := extractTags(entries, func(s string) string { return s }, func(name, output string) string { return "" }, nil)
|
||
for _, tag := range tagsSkip {
|
||
if tag == "编程" || tag == "咖啡" {
|
||
t.Errorf("NoMemory tool should not contribute keywords, got tag: %s", tag)
|
||
}
|
||
}
|
||
|
||
// toolCleanFn 返回清洗文本 → 用清洗后内容提取关键词
|
||
tagsClean := extractTags(entries, func(s string) string { return s }, func(name, output string) string { return "咖啡 编程" }, nil)
|
||
found := false
|
||
for _, tag := range tagsClean {
|
||
if tag == "编程" {
|
||
found = true
|
||
}
|
||
}
|
||
if !found {
|
||
t.Errorf("cleaner output keywords should appear in tags, got: %v", tagsClean)
|
||
}
|
||
}
|
||
|
||
func TestContextToDocContentPreservesRawToolOutput(t *testing.T) {
|
||
dir, err := os.MkdirTemp("", "doc_toolclean_*")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer os.RemoveAll(dir)
|
||
|
||
s := NewStore(dir, memory.TokenizeWords)
|
||
s.Start()
|
||
defer s.Stop()
|
||
|
||
entries := []ContextEntry{
|
||
{
|
||
Timestamp: time.Now(),
|
||
Source: "user",
|
||
Content: "查天气",
|
||
ToolResults: []ToolResultItem{
|
||
{Name: "weather", Output: "{\"temp\": 25}"},
|
||
},
|
||
},
|
||
}
|
||
|
||
// toolCleanFn 返回清洗文本,但 Content 必须保留原始输出
|
||
cleaner := func(name, output string) string {
|
||
return "天气 温度"
|
||
}
|
||
doc, err := s.ContextToDoc("test", entries, nil, nil, cleaner, nil)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !contains(doc.Content, "{\"temp\": 25}") {
|
||
t.Errorf("Content should preserve raw tool output, got: %s", doc.Content)
|
||
}
|
||
if doc.Summary == "" {
|
||
t.Error("summary should not be empty")
|
||
}
|
||
}
|