一个 OpenClaw Hook 技能,在用户消息发送给 Agent 之前自动查询 CNB 知识库,将相关的记忆和知识注入到上下文中,帮助 Agent 更好地理解用户需求。
knowledgebase-hook-skill/ ├── handler.ts # OpenClaw Hook 处理器 ├── query.sh # 知识库查询脚本 ├── HOOK.md # Hook 安装和使用文档 ├── SKILL.md # 技能说明文档 └── README.md # 本文件
repo-code:r 权限)# 必须配置
export CNB_TOKEN="your-token-here"
# 可选配置(有默认值)
export KB_REPO="cnbnas/clawd"
export KB_TOP_K="3"
export KB_SCORE_THRESHOLD="0.3"
export KB_HOOK_DEBUG="true"
将 handler.ts 与 HOOK.md 复制到 defaultAgent 的 workspace 目录(通常是 /workspace/clawd/hooks/)。
在 /root/.openclaw/openclaw.json 中添加 Hook 配置:
{
"hooks": {
"internal": {
"enabled": true,
"handlers": [
{
"event": "message:preprocessed",
"module": "./hooks/handler.ts"
}
]
}
}
}
openclaw gateway restart
| 变量名 | 说明 | 默认值 | 是否必须 |
|---|---|---|---|
CNB_TOKEN | CNB API Token | - | ✅ 是 |
CNB_API_ENDPOINT | CNB API 地址 | https://api.cnb.cool | ❌ 否 |
KB_REPO | 知识库仓库 | cnbnas/clawd | ❌ 否 |
KB_TOP_K | 返回结果数量 | 3 | ❌ 否 |
KB_SCORE_THRESHOLD | 相似度阈值 (0-1) | 0.3 | ❌ 否 |
KB_HOOK_ENABLED | 是否启用 Hook | true | ❌ 否 |
KB_HOOK_DEBUG | 是否开启调试日志 | true | ❌ 否 |
直接使用 query.sh 脚本查询知识库:
# 基本查询
./query.sh "记忆系统设计"
# 指定返回数量
./query.sh "如何配置 API" 3
# 指定相似度阈值
./query.sh "定时任务配置" 5 0.7
用户发送消息:
用户:橘子的记忆系统是怎么设计的?
Agent 实际接收到的消息(包含上下文):
📚 知识库上下文: • [MEMORY.md - 橘子的长期记忆] (cloud/MEMORY.md, code) - 相似度:0.966 • [[#1] 🧠 记忆架构设计 - CNB Issue 记忆系] (/cnbnas/clawd/-/issues/1, issue) - 相似度:0.942 • [TOOLS.md - Local Notes] (cloud/TOOLS.md, code) - 相似度:0.895 --- 用户消息: 橘子的记忆系统是怎么设计的?
用户消息 ↓ 媒体理解 ↓ 🔥 message:preprocessed Hook ↓ 查询 CNB 知识库 ↓ 提取 Top-K 相关结果 ↓ 构建上下文摘要 ↓ 注入 bodyForAgent ↓ Agent 处理并回复
调试日志默认开启,可以在 Gateway 控制台看到:
🔥 KB-Hook 被调用了! [KB-Hook] 收到 message:preprocessed 事件 [KB-Hook] 处理消息 (ID=test-123): 测试查询... [KB-Hook] 查询知识库:测试查询... [KB-Hook] 知识库返回 3 条结果 [KB-Hook] 已更新 bodyForAgent (3 条记忆)
export KB_HOOK_DEBUG="false"
CNB_TOKEN 有读取目标仓库的权限MIT License
🍊 Juzi (CNB)
Created: 2026-03-17