feat(memory): add Spaces support, platform/group annotation, and bot name to Nowledge Mem

- Use Nowledge Mem Spaces for per-bot memory isolation (space name: memoh:{botID})
- Auto-ensure space on first use with sync.Map cache
- Add platform/conversation context header to stored text: (Telegram 群组「开发讨论」)
- Replace [我] with bot's actual display name [小助手]
- Thread ConversationType, ConversationName, Platform, BotName through AfterChatRequest
- Add resolveBotDisplayName to resolver for DB lookup
This commit is contained in:
Menci
2026-04-12 15:15:14 +08:00
parent fefbc155c6
commit df1e1fc917
5 changed files with 233 additions and 20 deletions
+22 -5
View File
@@ -60,19 +60,25 @@ Nowledge Mem 是单用户个人知识库,没有 `user_id` 或 `group_id` 概
### 存储文本格式
每条记忆对应一轮对话(用户消息 + bot 回复):
每条记忆对应一轮对话(用户消息 + bot 回复),头部标注来源上下文
```
(Telegram 群组「开发讨论」)
[张三] 我最近在用 Rust 重写后端
[] 很好的选择,Rust 的性能和安全性都很出色
[小助手] 很好的选择,Rust 的性能和安全性都很出色
```
- **头部标注**`({Platform} {会话类型}「{群组名}」)`
- Platform:从 `CurrentChannel` 取值(telegram / feishu / discord 等),首字母大写
- 会话类型映射:`group``群组``private``私聊``thread``话题`
- 群组名:有则加 `「...」`,无则省略(私聊一般没有群组名)
- 示例:`(Telegram 私聊)``(Feishu 群组「开发讨论」)`
- **用户消息**`[{display-name}] {消息内容}`
- display-name 从 YAML front-matter header 中解析(Memoh 的 user_header.go 在每条用户消息中嵌入了 `display-name` 字段)
- 回退链:YAML header → AfterChatRequest.DisplayName → `"用户"`
- **Bot 消息**`[] {消息内容}`
- LLM 读到 `[我]` 时自然理解为"这是我之前说过的话"
- `我` 不是命名实体,不会在知识图谱中产生误导节点
- **Bot 消息**`[{bot-display-name}] {消息内容}`
- 使用 bot 的 display name(从 bots 表的 `display_name` 字段取)
- Nowledge Mem 的实体抽取会将 bot 名识别为 Person 实体
### 为什么不用 `(@username)` 双标识
@@ -87,6 +93,17 @@ AfterChatRequest 中只有 `DisplayName`(人类可读名)和内部 UUID(Us
不需要在查询时拼接发言人信息。
### Spaces 隔离
利用 Nowledge Mem 的 Spaces 功能实现 per-bot 记忆隔离:
- 每个 bot 自动映射到一个 Space,名称为 `memoh:{botID}`botID 是稳定的 UUID
- 首次使用时自动 ensure`GET /spaces` 查找 → 未找到则 `POST /spaces` 创建)
- `sync.Map` 缓存 `botID → spaceID` 映射,避免重复 API 调用
- 所有 `POST /memories``POST /memories/search` 调用带 `space_id` 参数
- 不同 bot 的记忆完全隔离,搜索不互相干扰
- Entity graph 保持全局(Nowledge Mem 设计如此),跨 bot 的实体关联仍可用
### 上下文注入格式
与现有 provider 一致的 `<memory-context>` XML 格式: