## 问题(实测)
SDK 里 100 个有摘要的符号中 **66 个是英文注释**,例如:
RegisterTool registers a tool that the LLM can call.
于是搜「注册工具」——中文受众最自然的问法——**RegisterTool 得分 0,一条都搜不到**。
更糟的是逐字匹配把噪声顶上来了:搜「注册工具」返回 24 条,排第一的是
`SetToolBlocks`(描述里有「工具」二字),`RegisterTool` 根本不在列表里。
## 修法
不改源码注释(那会让代码与文档脱节),而是在检索索引上加一层**人工标注的
中文功能词**:`tools/apidoc/keywords.json`。
- `rules`:按符号名前缀/子串批量覆盖(`Register*` 全带「注册」,`*Memory*` 带「记忆」)
- `symbols`:逐符号补充(重点 API、或规则覆盖不到的)
词只进 `api-index.json` 的 `g` 字段,**不影响页面展示**;检索结果里会显示
(「为何命中」),读者能理解排序依据。
同时把中文逐字匹配从主信号降为**弱信号**(要求 60% 以上字符命中)——
它正是噪声来源:凡是含「工具」二字的说明都会被「注册工具」匹上。
## 结果
| 查询 | 修改前 | 修改后 |
|---|---|---|
| 注册工具 | 24 条,RegisterTool 缺席 | **7 条,RegisterTool 第一** |
| 崩溃 | 1 条 | 2 条(SetAutoRestart + AutoRestart)|
| InjectText | 7 条(含重复)| 6 条 |
| memory.recall | 1 条 | 1 条(不变)|
顺带修掉索引重复:接口会同时作为 `type` 符号与接口本身被加两次
(`MemoryAPI` 等 11 个各重复一条)。现在接口只走接口那条路径,索引 200 → 189 条。
keywords.json 是**可选**的:读不到只警告不中断,检索退化为原行为。
8.6 KiB
记忆(Memory)
三层记忆的读写接口:图记忆(三元组关系)、文档记忆(带元数据的文档)、文本记忆(事件流水)。以及知识库。
DocMemoryAPI
DocMemoryAPI provides access to the document vector store.
| 方法 | 说明 |
|---|---|
Insert |
|
InsertWithMedia |
InsertWithMedia 写入文档并关联媒体。attachments 里带 Data 的会落进 |
Query |
|
Remove |
|
Stats |
DocMemoryAPI.Insert
Insert(doc *Doc) error
memory.go:76
DocMemoryAPI.InsertWithMedia
InsertWithMedia(doc *Doc, attachments []MediaAttachment) error
InsertWithMedia 写入文档并关联媒体。attachments 里带 Data 的会落进 内容寻址存储(相同字节只存一份),只带 Digest 的直接引用已有内容。 媒体成为文档直接持有的一等记忆块:文档向量会融合它们的原生向量, 因此图片按自己的向量被召回,不依赖任何生成的描述文本。
memory.go:81
DocMemoryAPI.Query
Query(text string, topK int) []*Doc
memory.go:75
DocMemoryAPI.Remove
Remove(id string)
memory.go:82
DocMemoryAPI.Stats
Stats() map[string]interface{}
memory.go:83
KnowledgeAPI
KnowledgeAPI provides access to the knowledge store.
| 方法 | 说明 |
|---|---|
Add |
|
List |
|
Search |
KnowledgeAPI.Add
Add(name, content string) error
knowledge.go:6
KnowledgeAPI.List
List() ([]string, error)
knowledge.go:7
KnowledgeAPI.Search
Search(query string, topK int) ([]*Knowledge, error)
knowledge.go:5
MemoryAPI
MemoryAPI provides access to the graph memory (entity-relation store).
| 方法 | 说明 |
|---|---|
Commit |
|
Introspect |
|
MergeEntities |
|
Purge |
|
Recall |
MemoryAPI.Commit
Commit(triples []Triple) error
memory.go:6
MemoryAPI.Introspect
Introspect() (map[string]interface{}, error)
memory.go:7
MemoryAPI.MergeEntities
MergeEntities(source, target string) (int, error)
memory.go:8
MemoryAPI.Purge
Purge(criteria map[string]string, mode string) (int, error)
memory.go:9
MemoryAPI.Recall
Recall(query []string, depth int) ([]Entity, []Relation, error)
memory.go:5
SocialAPI
SocialAPI provides read-only access to the social graph (person profiles and relationships). External plugins can query person traits and social networks but cannot modify them.
| 方法 | 说明 |
|---|---|
GetNetwork |
|
GetPerson |
|
GetRelations |
|
GetTrait |
|
ListPersons |
SocialAPI.GetNetwork
GetNetwork(name string, depth int) ([]*PersonProfile, error)
memory.go:104
SocialAPI.GetPerson
GetPerson(name string) (*PersonProfile, error)
memory.go:101
SocialAPI.GetRelations
GetRelations(name string) ([]SocialRelation, error)
memory.go:103
SocialAPI.GetTrait
GetTrait(name, trait string) (string, bool)
memory.go:102
SocialAPI.ListPersons
ListPersons() ([]string, error)
memory.go:105
TextMemoryAPI
TextMemoryAPI provides access to chronological text event storage.
| 方法 | 说明 |
|---|---|
Append |
TextMemoryAPI.Append
Append(evt TextEvent) error
memory.go:44
Doc
type Doc struct { ID string `json:"id"` Title string `json:"title"` Content string `json:"content"` Score …
Doc represents a document in the document store.
MediaDigests / Attachments 在 Query 返回时由内核填充(仅元数据,不带字节)。
memory.go:89
PluginSDK.DocMemory
func (s *PluginSDK) DocMemory() DocMemoryAPI
DocMemory returns the document memory API (may be nil if not available).
plugin.go:418
Entity
type Entity struct { Name string `json:"name"` Type string `json:"type"` MentionCount int `json:"mention_count"` }
Entity represents a named entity in the knowledge graph.
memory.go:13
Knowledge
type Knowledge struct { Name string `json:"name"` Content string `json:"content"` }
Knowledge represents a knowledge entry.
示例插件里的真实用法
| 插件 | 位置 | 代码 |
|---|---|---|
recoverydiag |
example/recoverydiag/plugin.go:978 |
if p.sdk != nil && p.sdk.Knowledge() != nil { |
knowledge.go:11
PluginSDK.Knowledge
func (s *PluginSDK) Knowledge() KnowledgeAPI
Knowledge returns the knowledge store API (may be nil if not available).
示例插件里的真实用法
| 插件 | 位置 | 代码 |
|---|---|---|
recoverydiag |
example/recoverydiag/plugin.go:978 |
if p.sdk != nil && p.sdk.Knowledge() != nil { |
plugin.go:425
MediaAttachment
type MediaAttachment struct { Digest string `json:"digest,omitempty"` MIME string `json:"mime,omitempty"` Data []byte `json:"data,omitempty"` Name string `json:"name,omite …
TextEvent represents a single text memory event. MediaAttachment 描述一份与记忆关联的媒体。
两个方向共用一个类型:
- 写入(InsertWithMedia):给 Data + MIME 就是新内容;只给 Digest 则是引用已有内容。
- 读出(Query):内核只填 Digest/MIME,不回 Data—— 一次检索可能命中几十张图,把字节全塞回插件会把 ABI 消息撑爆。 需要字节时拿 Digest 单独取。
刻意没有 Description 字段:媒体不作为文本被索引,也不带任何生成的描述。 它只按自己的原生向量被检索与召回;附加文字请写在文档 / 三元组的文本里。
memory.go:58
PluginSDK.Memory
func (s *PluginSDK) Memory() MemoryAPI
Memory returns the graph memory API (may be nil if not available).
plugin.go:404
PersonProfile
type PersonProfile struct { Name string `json:"name"` Traits map[string]string `json:"traits,omitempty"` Relations []SocialRelation `json:"relations,omitemp …
PersonProfile represents a person's complete profile (traits + social relations).
memory.go:109
Relation
type Relation struct { SourceName string `json:"source_name"` TargetName string `json:"target_name"` RelationType string `json:"relation_type"` Confidence float6 …
Relation represents a relationship between two entities.
memory.go:20
PluginSDK.Social
func (s *PluginSDK) Social() SocialAPI
Social returns the social graph API (may be nil if not available).
plugin.go:439
SocialRelation
type SocialRelation struct { Person string `json:"person"` Relation string `json:"relation"` }
SocialRelation represents a social relationship between two persons.
memory.go:116
TextEvent
type TextEvent struct { Role string `json:"role"` Content string `json:"content"` Timestamp int64 `json:"timestamp"` Channel …
memory.go:65
PluginSDK.TextMemory
func (s *PluginSDK) TextMemory() TextMemoryAPI
TextMemory returns the text memory API (may be nil if not available).
plugin.go:411
Triple
type Triple struct { Subject string `json:"subject"` Relation string `json:"relation"` Object string `json:"object"` Confidence float64 `json:"c …
Triple represents a subject-relation-object triple for the knowledge graph.
SentenceText 是这条三元组的原句,会写进 sentences 表;媒体引用挂在句子上, 所以 MediaDigests 非空时内核会保证句子存在(不给就自动合成一句)。
memory.go:31