高级功能解锁
掌握前九章的基础之后,这些高级功能能让你的 OpenClaw 更加强大。
1. 持久记忆系统
OpenClaw 支持两种记忆机制:
对话记忆(默认)——同一个上下文窗口内的自动历史记录。
持久向量记忆——基于嵌入向量的语义搜索,让 Agent 记住跨对话的持久信息。
启用持久记忆:
{"memory": {"enabled": true,"provider": "sqlite","maxItems": 5000}}
手动向记忆里写入信息
直接在对话里说:
记住:我们公司的品牌色是 #1A73E8,主要面向 B2B 企业客户记住:我的工作邮箱是 wang@company.com,个人邮箱是 me@gmail.com
Agent 会把这些写入 MEMORY.md,之后每次对话都能访问。
查看当前记忆内容
cat ~/.openclaw/workspace/MEMORY.md
清除特定记忆
# 直接编辑 MEMORY.md 删除不需要的条目nano ~/.openclaw/workspace/MEMORY.md
当上下文过长时,/compact 命令将历史压缩并将重要信息存入持久记忆。
2. RAG 知识库
把你的私有文档(API 文档、公司知识库、个人笔记)喜入 OpenClaw,让 Agent 基于它们回答问题。
# 导入本地目录openclaw rag import ./docs/# 导入单个文件openclaw rag import ./wiki.md# 查看已导入的知识库openclaw rag list# 手动测试检索openclaw rag query "如何配置 Telegram Bot?"# 删除知识库openclaw rag clear
支持的文件格式: Markdown、TXT、PDF、JSON、HTML
配置 RAG 自动导入:
{"rag": {"enabled": true,"watchDir": "~/.openclaw/knowledge/","autoImport": true}}
3. 多 Agent 并行执行
一个实例内启动多个 Agent,同时处理不同任务:
{"agents": {"list": [{ "id": "writer", "workspace": "~/.openclaw/workspace-writer", "model": "anthropic/claude-sonnet-4-5" },{ "id": "analyst", "workspace": "~/.openclaw/workspace-analyst", "model": "anthropic/claude-sonnet-4-5" },{ "id": "ops", "workspace": "~/.openclaw/workspace-ops", "model": "openai/gpt-5-mini" }]}}
切换 Agent:
/agent writer # 切到内容写手/agent analyst # 切到数据分析师/agents # 列出所有 Agent
4. Dashboard 控制台
访问 http://localhost:18789/dashboard:
- 实时查看所有 Agent 的运行状态
- 历史对话和任务执行日志
- Cron 任务的调度状况和执行结果
- Token 消耗统计,分 Agent 展示
默认关闭,启用方法:
{ "dashboard": { "enabled": true } }
5. REST API 接口
OpenClaw Gateway 提供 HTTP API,可与任何外部系统集成:
第一步:创建 API Key
openclaw api-key create --name "n8n-integration"# 输出:Bearer eyJhbGci...(保存好,只显示一次)openclaw api-key list # 查看所有 Keyopenclaw api-key revoke <key-id> # 撤销
第二步:向 Agent 发送任务
curl -X POST http://localhost:18789/api/message \-H "Authorization: Bearer <your-api-key>" \-H "Content-Type: application/json" \-d '{"agentId": "writer","message": "帮我把以下内容改写成正式邮件:${content}","channel": "telegram","target": "123456789"}'
异步任务:发送后查询结果
# 发送任务,获取 taskIdTASK_ID=$(curl -s -X POST http://localhost:18789/api/message \-H "Authorization: Bearer <your-api-key>" \-H "Content-Type: application/json" \-d '{"agentId": "analyst", "message": "分析这份数据"}' | jq -r '.taskId')# 轮询结果curl http://localhost:18789/api/task/$TASK_ID \-H "Authorization: Bearer <your-api-key>"
n8n 集成示例
在 n8n 里添加 HTTP Request 节点:
- Method: POST
- URL:
http://你的服务器:18789/api/message - Headers:
Authorization: Bearer <key> - Body:
{"agentId": "ops", "message": "{{ $json.content }}"}
这样 n8n 的任意工作流(收到邮件、表单提交、定时触发)都能驱动 OpenClaw Agent 工作。
Webhook 接收外部触发
配置 OpenClaw 接收来自外部的 Webhook(如 GitHub、Stripe 事件):
{"webhooks": {"enabled": true,"endpoints": [{"path": "/webhook/github","secret": "${GITHUB_WEBHOOK_SECRET}","agentId": "ops","messageTemplate": "GitHub 事件:{{event}},仓库:{{repository.full_name}},详情:{{head_commit.message}}"}]}}
GitHub Webhook 地址设为 http://你的服务器:18789/webhook/github,每次 Push 都会自动通知 ops Agent。
6. /compact 的正确使用方式
当对话历史很长(上下文快满)时,运行:
/compact
Agent 会:
- 把当前对话历史压缩成摘要
- 将关键信息写入
MEMORY.md持久保存 - 清空上下文窗口,释放 Token 额度
什么时候用:对话进行了几十轮、响应变慢、费用明显上涨时。
7. RAG 常见问题
文档导入后 Agent 不引用
- 确认 RAG 已启用:
openclaw rag list看是否有内容 - 手动测试检索:
openclaw rag query "你的问题" - 如果检索无结果,检查文档编码是否为 UTF-8
PDF 导入失败
# 先转成 Markdown 再导入pip install markitdownmarkitdown document.pdf > document.mdopenclaw rag import document.md
知识库内容过期,需要更新
# 删除旧内容,重新导入openclaw rag clearopenclaw rag import ./docs/
配置自动监听目录变化
{"rag": {"enabled": true,"watchDir": "~/.openclaw/knowledge/","autoImport": true,"autoUpdate": true}}
把文件放到 ~/.openclaw/knowledge/ 目录,OpenClaw 会自动检测变化并重新索引。