本文档整理自 2026年3月7日 学习内容,适合有编程基础的读者。文章中有较多 asci 图适合电脑端打开阅读
OpenClaw(原名 Clawdbot / Moltbot)是一款开源的本地优先 AI 代理与自动化平台。
核心定位:不是聊天机器人,而是「AI 执行者」—— 能帮你干活的数字员工。
OpenClaw Gateway │ ├── Agent (一个"人") │ │ │ ├── Channel 1 (Telegram) │ │ │ │ │ ├── Session A (私聊) │ │ │ └── sandbox: off (主会话) │ │ │ │ │ └── Session B (群组) │ │ └── sandbox: non-main │ │ │ ├── Channel 2 (飞书) │ │ │ │ │ └── Session C (私聊) │ │ └── sandbox: off │ │ │ └── Channel 3 (Discord) │ │ │ └── Session D (频道) │ │ │ └── Subagents (子代理) │ ├── Subagent 1 (搜索任务) │ │ └── sandbox: on (隔离) │ │ │ └── Subagent 2 (代码分析) │ └── sandbox: on │ └── Workspace (工作区)routing.bindings | ||
session.dmScope | ||
sandbox.mode |
Session 是 Agent 运行时的对话上下文,不是另一个 Agent。
Agent: main (一个"人") │ ├── 会话 A: Dashboard 私聊 (main:main) ├── 会话 B: Telegram 群组 (main:telegram:group-123) └── 会话 C: 飞书频道 (main:feishu:channel-456)agent:<agentId>:main | ||
agent:<agentId>:<sessionKey> |
dmScope 控制的是:当多个人给你发私信时,OpenClaw 如何管理对话历史。
{"session":{"dmScope":"main"// main/per-peer/per-channel-peer/per-account-channel-peer}}"main" | |||
"per-peer" | |||
"per-channel-peer" | |||
"per-account-channel-peer" |
场景:你有 3 个人在 Telegram 给你发私信
dmScope: "main" (默认)┌─────────────────────────────────────────┐│ 会话: agent:main:main ││ ││ 张三: "帮我查天气" ││ 李四: "今天有什么新闻" ││ 王五: "帮我订外卖" ││ ││ ❌ 张三的对话全混在一起了 ││ AI 会混淆谁是谁 │└────────────────────────────────────────┘dmScope: "per-peer"┌─────────────────────────────────────────┐│ 会话1: agent:main:telegram:user-zhang ││ 张三: "帮我查天气" ││ (独立上下文) │├─────────────────────────────────────────┤│ 会话2: agent:main:telegram:user-li ││ 李四: "今天有什么新闻" ││ (独立上下文) │├─────────────────────────────────────────┤│ 会话3: agent:main:telegram:user-wang ││ 王五: "帮我订外卖" ││ (独立上下文) │└────────────────────────────────────────┘dmScope: "per-channel-peer"┌─────────────────────────────────────────┐│ 会话1: agent:main:telegram:user-zhang ││ 张三 (Telegram): "帮我查天气" │├─────────────────────────────────────────┤│ 会话2: agent:main:feishu:user-zhang ││ 张三 (飞书): "再帮我查下上海天气" ││ (Telegram 和飞书的张三分开) │└────────────────────────────────────────┘dmScope: "per-account-channel-peer"┌─────────────────────────────────────────┐│ 场景:你有 2 个 Telegram 号 + 1 个飞书号 ││ 张三在 3 个平台上都给你发了消息 │└─────────────────────────────────────────┘┌─────────────────────────────────────────┐│ 会话1: agent:main:telegram:acc-001:user-zhang ││ Telegram 号1 + 张三: "帮我查天气" │├─────────────────────────────────────────┤│ 会话2: agent:main:telegram:acc-002:user-zhang ││ Telegram 号2 + 张三: "上次说的那个事" ││ (与 acc-001 的张三分开) │├─────────────────────────────────────────┤│ 会话3: agent:main:feishu:acc-003:user-zhang ││ 飞书号 + 张三: "帮我订外卖" ││ (完全独立,与 Telegram 分开) │└─────────────────────────────────────────┘sandbox: off | ||
sandbox: non-main |
这就是"主会话 vs 群组会话"的区别:
{"sandbox":{"enabled":false,// 是否启用沙箱"workspaceAccess":"rw",// 工作区访问权限: rw/r/none"mode":"non-main"// 模式: on/off/non-main}}"on" | |
"off" | |
"non-main" | (默认) |
// 场景1: 个人助手(完全访问主机){"agents":{"defaults":{"sandbox":{"mode":"off"}}}}// 场景2: 主 Agent 显式配置{"routing":{"agents":{"main":{"workspace":"~/.openclaw/workspace","sandbox":{"mode":"off"}}}}}// 场景3: 安全隔离{"agents":{"defaults":{"sandbox":{"enabled":true,"workspaceAccess":"r","mode":"on"}}}}| 配置文件 | config/routes.rb | routing.bindings |
| 路由规则 | get/post 'path', to: 'controller#action' | { "channel": "telegram", "agent": "main" } |
| 路径参数 | :id:user_id | accountgroup, phone |
| 目标处理 | ||
| 响应格式化 | ||
| 路由名称 | users_path |
# Rails 路由post '/telegram', to:'telegram#main'post '/feishu/dev', to:'feishu#work'post '/whatsapp/:phone', to:'whatsapp#personal'// OpenClaw 路由{"routing":{"bindings":[{"channel":"telegram","agent":"main"},{"channel":"feishu","group":"dev-team","agent":"work"},{"channel":"whatsapp","phone":"+1234567890","agent":"personal"}]}}resources :users | ||
scope '/admin' | ||
constraints |
Accept: text/html | ||
Accept: application/json | ||
Accept: application/xml |
HTTP 世界:浏览器请求 → Rails Controller → 返回 HTMLAPI 请求 → Rails Controller → 返回 JSON移动端请求 → Rails Controller → 返回 XMLOpenClaw 世界:Telegram 消息 → Agent 处理 → Channel → MarkdownV2 格式飞书消息 → Agent 处理 → Channel → 卡片消息格式Discord 消息 → Agent 处理 → Channel → 嵌入式消息格式Channel 的核心职责:把统一的内部消息格式转换为各平台特定的响应格式。
模式1: 直接工具调用(常见)
用户: "帮我搜索新闻" │ ▼Agent: main 直接调用 web_search 工具 │ ▼返回结果模式2: sessions_spawn(复杂任务)
用户: "分析这个代码库" │ ▼Agent: main 调用 sessions_spawn 启动子 Agent │ ├── 创建隔离会话 ├── 子 Agent 执行任务 │ ▼子 Agent 完成任务,通知主会话{"routing":{// 路由层"agents":{// Agent 列表"main":{// 名为 main 的 Agent"workspace":"...",// 工作区路径"sandbox":{// 沙箱设置"mode":"off"}}}},"agents":{// Agent 默认配置"defaults":{"sandbox":{ ... }}},"session":{// 会话配置"dmScope":"main","mainKey":"main"},"channels":{ ... },// 渠道配置"models":{ ... }// 模型配置}本文使用 Claude + Minimax2.5 协助创作