系列教程第三篇 | 作者:攀岩者 | 公众号:AI学习日记
引言:一个记忆,两个 Agent
想象一下:
- 你在 Hermes 里讨论了一个项目的技术方案
- 切换到 OpenClaw 继续开发,它自动知道之前的讨论内容
- 两个 Agent 共享同一个”大脑”,无缝协作
这就是 agentmemory 的魔力——跨平台记忆共享。
今天,我将带你实战演示如何打通 Hermes 和 OpenClaw,让它们共享记忆。
一、架构原理
1.1 记忆共享架构
`
┌─────────────┐ ┌─────────────┐
│ Hermes │ │ OpenClaw │
│ (Agent A) │ │ (Agent B) │
└──────┬──────┘ └──────┬──────┘
│ │
│ MCP Protocol │
│ │
└─────────┬─────────┘
│
▼
┌────────────────┐
│ agentmemory │
│ (Memory Hub) │
│ │
│ ┌──────────┐ │
│ │ SQLite │ │
│ │ Database │ │
│ └──────────┘ │
└────────────────┘
`
1.2 工作流程
- Hermes 添加记忆 → 通过 MCP 发送到 agentmemory
- agentmemory 存储 → 保存到 SQLite 数据库
- OpenClaw 搜索记忆 → 通过 MCP 查询 agentmemory
- 返回结果 → OpenClaw 获得 Hermes 添加的记忆
关键点:所有 Agent 连接同一个 agentmemory 实例,记忆自然共享。
二、前置条件
确保你已经完成:
- ✅ 安装了 agentmemory(参考第二篇)
- ✅ agentmemory 服务正在运行(
agentmemory status显示 healthy) - ✅ 配置了 API(小米 MiMo / OpenAI / FreeLLMAPI)
三、配置 Hermes
3.1 添加 MCP 配置
`bash
# 方式 1:使用 hermes config set(推荐)
hermes config set mcp_servers.agentmemory.command npx
hermes config set ‘mcp_servers.agentmemory.args’ ‘[“-y”, “@agentmemory/mcp”]’
hermes config set memory.provider agentmemory
`
`bash
# 方式 2:直接编辑 ~/.hermes/config.yaml
mcp_servers:
agentmemory:
command: npx
args: ‘[“-y”, “@agentmemory/mcp”]’
memory:
provider: agentmemory
`
3.2 验证配置
`bash
# 检查配置是否生效
grep -A 4 “mcp_servers:” ~/.hermes/config.yaml
grep “memory:” ~/.hermes/config.yaml
`
正常输出:
`yaml
mcp_servers:
agentmemory:
command: npx
args: ‘[“-y”, “@agentmemory/mcp”]’
`
3.3 重启 Hermes Gateway
`bash
# 如果使用 systemd
systemctl –user restart hermes-gateway
# 或者重启 hermes 进程
hermes restart
`
四、配置 OpenClaw
4.1 添加 MCP 配置
⚠️ 重要:必须使用 openclaw mcp set 命令,不能直接编辑 JSON 文件!
`bash
openclaw mcp set agentmemory ‘{“command”:”npx”,”args”:[“-y”,”@agentmemory/mcp”]}’
`
4.2 验证配置
`bash
openclaw mcp list
`
正常输出:
`
MCP servers (/home/user/.openclaw/openclaw.json):
- agentmemory
`
4.3 重启 OpenClaw Gateway
`bash
# 如果使用 systemd
systemctl –user restart openclaw-gateway
# 或者手动重启
openclaw gateway restart
`
五、验证记忆桥接
5.1 测试方案
我们将进行双向测试:
- 从 Hermes 添加一条记忆
- 从 OpenClaw 搜索这条记忆
- 从 OpenClaw 添加一条记忆
- 从 Hermes 搜索这条记忆
如果双向都能成功,说明记忆桥接已经打通。
5.2 步骤 1:从 Hermes 添加记忆
在 Hermes 的对话中,让 AI 添加一条记忆:
`
请记住:这是一条从 Hermes 添加的测试记忆,用于验证跨平台记忆共享。
`
或者使用 API:
`bash
curl -s -X POST http://localhost:3111/agentmemory/remember
-H “Content-Type: application/json”
-d ‘{
“hookType”: “hermes-test”,
“sessionId”: “hermes-session-001”,
“project”: “memory-bridge-test”,
“cwd”: “/home/user”,
“timestamp”: “‘$(date -u +%Y-%m-%dT%H:%M:%S.000Z)’”,
“content”: “这是一条从 Hermes 添加的测试记忆。关键词:Hermes 测试成功。”,
“type”: “fact”
}’
`
正常输出:
`json
{
“memory”: {
“id”: “mem_xxx”,
“content”: “这是一条从 Hermes 添加的测试记忆…”
},
“success”: true
}
`
5.3 步骤 2:从 OpenClaw 搜索记忆
在 OpenClaw 的对话中,让 AI 搜索:
`
帮我搜索关于”Hermes 测试”的记忆
`
或者使用 API:
`bash
curl -s -X POST http://localhost:3111/agentmemory/search
-H “Content-Type: application/json”
-d ‘{
“query”: “Hermes 测试”,
“limit”: 5
}’
`
如果能找到刚才添加的记忆,说明 Hermes → OpenClaw 方向打通!
正常输出:
`json
{
“results”: [
{
“observation”: {
“narrative”: “这是一条从 Hermes 添加的测试记忆。关键词:Hermes 测试成功。”
},
“score”: 4.567
}
],
“tokens_used”: 120
}
`
5.4 步骤 3:从 OpenClaw 添加记忆
在 OpenClaw 的对话中,让 AI 添加记忆:
`
请记住:这是一条从 OpenClaw 添加的测试记忆,用于验证跨平台记忆共享。
`
或者使用 API:
`bash
curl -s -X POST http://localhost:3111/agentmemory/remember
-H “Content-Type: application/json”
-d ‘{
“hookType”: “openclaw-test”,
“sessionId”: “openclaw-session-001”,
“project”: “memory-bridge-test”,
“cwd”: “/home/user”,
“timestamp”: “‘$(date -u +%Y-%m-%dT%H:%M:%S.000Z)’”,
“content”: “这是一条从 OpenClaw 添加的测试记忆。关键词:OpenClaw 测试成功。”,
“type”: “fact”
}’
`
5.5 步骤 4:从 Hermes 搜索记忆
在 Hermes 的对话中,让 AI 搜索:
`
帮我搜索关于”OpenClaw 测试”的记忆
`
或者使用 API:
`bash
curl -s -X POST http://localhost:3111/agentmemory/search
-H “Content-Type: application/json”
-d ‘{
“query”: “OpenClaw 测试”,
“limit”: 5
}’
`
如果能找到刚才添加的记忆,说明 OpenClaw → Hermes 方向打通!
六、可视化验证
6.1 访问 Viewer
打开浏览器访问:http://localhost:3113
6.2 查看记忆图谱
在 Viewer 中,你可以看到:
- Sessions 标签:所有会话列表
- Memories 标签:所有记忆条目
- Graph 标签:记忆之间的关联图谱
6.3 验证共享
在 Memories 标签中,你应该能看到:
- 从 Hermes 添加的记忆
- 从 OpenClaw 添加的记忆
- 它们都存储在同一个数据库中
七、实战场景
场景 1:项目讨论
在 Hermes 中:
`
用户:我们来讨论一下新项目的技术方案
AI:好的,您有什么想法?
用户:我想用 Next.js + Prisma + PostgreSQL
AI:明白了,我记住了这个技术栈选择。
(agentmemory 自动捕获:项目使用 Next.js + Prisma + PostgreSQL)
`
切换到 OpenClaw:
`
用户:开始写项目代码
AI:好的,我记得之前讨论过技术方案是 Next.js + Prisma + PostgreSQL,对吗?
用户:是的
AI:那我们从数据库 schema 开始吧。
`
场景 2:编码习惯
在 Hermes 中:
`
用户:我习惯用 TypeScript,喜欢函数式编程风格
AI:明白了,我记住了您的编码偏好。
(agentmemory 自动捕获:用户偏好 TypeScript,喜欢函数式编程)
`
切换到 OpenClaw:
`
用户:帮我写一个工具函数
AI:好的,考虑到您偏好 TypeScript 和函数式风格,我这样写…
(agentmemory 自动召回:用户偏好 TypeScript,喜欢函数式编程)
`
场景 3:错误修复
在 Hermes 中:
`
用户:数据库连接超时了
AI:让我看看… 哦,是连接池配置问题,max_connections 太小了。
(agentmemory 自动捕获:数据库连接超时,原因是连接池配置问题)
`
切换到 OpenClaw:
`
用户:数据库又出问题了
AI:我记得之前遇到过连接超时,是连接池配置问题。让我检查一下 max_connections…
(agentmemory 自动召回:之前连接超时的原因是连接池配置)
`
八、最佳实践
8.1 让 AI 主动记忆
在对话开始时,告诉 AI:
`
请记住我们的对话内容,特别是:
- 技术决策和原因
- 遇到的问题和解决方案
- 我的编码偏好和习惯
`
8.2 定期整理记忆
agentmemory 会自动整理,但你也可以手动触发:
`bash
# 整理记忆(合并相似条目)
curl -s -X POST http://localhost:3111/agentmemory/consolidate
-H “Content-Type: application/json”
-d ‘{“project”: “my-project”}’
`
8.3 搜索技巧
- 使用具体关键词:
数据库连接超时比数据库问题更精准 - 使用多个关键词:
Next.js Prisma 配置比单个词更准确 - 尝试不同表达:如果搜不到,换个说法试试
8.4 隐私保护
如果某些对话不想被记住:
`
这段对话不要记住,涉及敏感信息。
`
或者在 .env 中配置排除规则:
`bash
# 排除特定 Agent
CAPTURE_EXCLUDE_AGENTS=test-agent-*
`
九、故障排查
问题 1:Hermes 找不到记忆
检查:
`bash
# 确认 agentmemory 运行
agentmemory status
# 确认 Hermes 配置
grep -A 4 “mcp_servers:” ~/.hermes/config.yaml
# 确认记忆存在
curl -s http://localhost:3111/agentmemory/status | grep Memories
`
解决:
- 重启 Hermes Gateway
- 检查 MCP 配置格式
问题 2:OpenClaw 找不到记忆
检查:
`bash
# 确认 OpenClaw 配置
openclaw mcp list
# 确认 agentmemory 运行
agentmemory status
`
解决:
- 重新配置:
openclaw mcp set agentmemory '...' - 重启 OpenClaw Gateway
问题 3:双向都搜不到
检查:
`bash
# 直接通过 API 搜索
curl -s -X POST http://localhost:3111/agentmemory/search
-H “Content-Type: application/json”
-d ‘{“query”: “测试”, “limit”: 5}’
`
解决:
- 确认记忆已添加成功
- 尝试不带 project 参数搜索
- 检查 agentmemory 日志
十、总结
通过这篇教程,你已经学会了:
- ✅ 理解记忆共享的架构原理
- ✅ 配置 Hermes 连接 agentmemory
- ✅ 配置 OpenClaw 连接 agentmemory
- ✅ 验证双向记忆桥接
- ✅ 实际应用场景
- ✅ 最佳实践和故障排查
现在,Hermes 和 OpenClaw 已经共享同一个”大脑”,你可以无缝切换使用,它们会越来越懂你。
系列教程回顾
| 篇目 | 内容 |
|---|
|——|——|
| 第一篇 | AI 记忆革命:四大方案终极对决 |
|---|---|
| 第二篇 | 从 0 到 1:agentmemory 部署实战 |
| 第三篇 | 打通 Hermes 和 OpenClaw:跨平台记忆共享 |
下一步
- 尝试在实际工作中使用记忆功能
- 探索 agentmemory 的高级功能(图谱、自动整理)
- 关注我的公众号获取更多 AI 实战教程
作者简介:攀岩者,东华软件技术总监,19 年 IT 经验,PMP+CKA。每天分享 AI 学习笔记,陪你从零基础到 AI 达人。
>
关注公众号:AI学习日记,获取更多 AI 实战教程。
发表回复