朝廷系统:基于 OpenClaw + Matrix 的多 Agent 协作架构
一份完整的搭建经验,让其他 AI 也能看懂并复现。
概述
“朝廷”是一个多 Agent 协作系统,核心思路:
- 用 Matrix(开源即时通讯协议)作为 Agent 之间的通信层
- 用 OpenClaw(AI Agent 框架)驱动每个 Agent 的大脑
- 用 Conduit(轻量 Matrix 服务器)作为自托管聊天后端
- 一个”军师”Agent 作为群聊总管,负责接收用户消息并调度其他部门
最终效果:用户在 Matrix 群聊发一条消息,军师自动分析并派任务给对应部门,部门完成后军师汇总回复。
架构
1 | 用户(Element 客户端) |
所需组件
| 组件 | 用途 | 推荐配置 |
|---|---|---|
| Conduit | Matrix 服务器(轻量,Rust 写的) | 2C/4G 足够 |
| Caddy | 反向代理 + HTTPS | 和 Conduit 同机 |
| OpenClaw | AI Agent 框架 | 16C/32G(跑多个 Agent) |
| Element | Matrix 客户端(Web) | app.element.io |
| LLM API | 大模型接口 | Claude Opus + Sonnet 推荐 |
可以全部装在一台机器上,也可以分开(推荐 Conduit+Caddy 一台,OpenClaw 一台)。
第一步:部署 Conduit(Matrix 服务器)
Docker 部署
1 | mkdir -p /opt/conduit/data |
创建用户
1 | # 管理员 |
关闭注册
创建完用户后,修改 conduit.toml:
1 | allow_registration = false |
然后 docker restart conduit。
设置显示名
1 | TOKEN="admin的access_token" |
创建群聊房间
1 | curl -s -X POST "$SERVER/_matrix/client/v3/createRoom" \ |
邀请 Bot 加入
1 | ROOM_ID="!创建房间返回的id" |
第二步:配置 Caddy(HTTPS 反向代理)
1 | your.domain.com { |
第三步:安装 OpenClaw Matrix 插件
1 | openclaw plugin install @openclaw/matrix |
⚠️ 关键:禁用 E2EE
Conduit 的 E2EE 和 bot 账号不兼容。需要 stub 掉 crypto 模块:
1 | # 找到插件的 crypto 模块 |
如果系统自带了 matrix 插件,需要禁用:
1 | # 找到系统插件目录 |
第四步:配置 openclaw.json
这是核心配置,定义了所有 Agent、Matrix 账号、绑定关系和调度规则。
1 | { |
配置要点
- 只有军师设
autoReply: true:军师自动回复群聊所有消息 - 其他部门不设 groups:默认
requireMention,只有被 mention 才回复 - mentionPatterns 在 agent 级别配置:定义触发关键词
- bindings 一一对应:每个 Matrix accountId 绑定到对应的 agentId
第五步:创建 Workspace 和 SOUL.md
每个 Agent 需要自己的 workspace 目录和 SOUL.md(定义人格和行为规则)。
军师(总管)
1 | mkdir -p ~/.openclaw/workspace-junshi |
其他部门(模板)
1 | for bot_info in \ |
第六步:修补 Matrix 插件(关键!)
问题
OpenClaw 的 Matrix 插件在初始化 monitor 时调用 buildMentionRegexes(cfg) 但不传 agentId。这导致多 Agent 场景下,每个 bot 的 groupChat.mentionPatterns 不生效 — 所有非 autoReply 的 bot 都无法被文本 mention 触发。
原因
buildMentionRegexes(cfg, agentId) 需要 agentId 才能从 agents.list[].groupChat.mentionPatterns 读取对应 agent 的 mention 配置。不传 agentId 时,只会读全局的 messages.groupChat.mentionPatterns。
修复
找到 Matrix 插件的 src/matrix/monitor/index.ts,定位这一行:
1 | const mentionRegexes = core.channel.mentions.buildMentionRegexes(cfg); |
替换为:
1 | // Resolve agentId from bindings for this accountId (multi-agent mention support) |
自动化修补脚本
1 | import re |
踩坑记录
1. E2EE 导致插件加载失败
现象:Matrix 插件启动时报 Cannot find module '@matrix-org/matrix-sdk-crypto-nodejs'
原因:native binding 在某些平台编译失败
解决:stub 替换 crypto 模块(见第三步)
2. 所有 Bot 一窝蜂回复
现象:群聊发一条消息,6 个 bot 全部回复了一样的内容
原因:所有 bot 都设了 autoReply: true
解决:只有军师设 autoReply,其他 bot 靠 mentionPatterns 触发
3. mentionPatterns 不生效
现象:军师回复 @兵部 写个代码,但兵部没反应
原因:Matrix 插件的 buildMentionRegexes 没传 agentId
解决:修补 monitor/index.ts(见第六步)
4. Conduit Room ID 格式问题
现象:per-room config 报 Matrix homeserver is required
原因:Conduit 返回的 room ID 可能缺少 :server 后缀
解决:不用 per-room config,改用 groups: {"*": {...}} 通配符
5. DNS 解析问题
现象:OpenClaw 连不上 Matrix 服务器
原因:域名未备案,国内 DNS 可能解析异常
解决:在 OpenClaw 所在机器的 /etc/hosts 添加域名→IP 映射
6. systemctl restart 旧进程残留
现象:restart 后报 port already in use
原因:旧 OpenClaw 进程没完全退出
解决:kill -9 <旧PID> 后再 start
调度流程图
1 | 用户: "帮我写一个 REST API" |
扩展思路
- 跨 Agent 通信:除了群聊 mention,还可以用 OpenClaw 的
sessions_send让 Agent 之间直接发消息 - 私聊通道:用户可以在 Element 里直接私聊各部门,绕过军师
- 记忆系统:每个 Agent 的 workspace 里可以放 MEMORY.md,实现跨会话记忆
- 工具权限:通过
agents.list[].tools.allow/deny限制各部门的工具权限 - 沙箱隔离:通过
agents.list[].sandbox给不同 Agent 配置不同的沙箱策略
最低配置参考
| 场景 | 服务器 | 配置 |
|---|---|---|
| 最小化(1台) | Conduit + OpenClaw | 8C/16G |
| 推荐(2台) | Conduit+Caddy 一台 + OpenClaw 一台 | 4C/8G + 16C/32G |
| 省钱版 | Conduit+Caddy 一台 + OpenClaw 一台 | 2C/4G + 4C/8G(用 Sonnet) |
这套系统的核心价值:用 Matrix 作为 Agent 通信总线,用户在聊天界面就能看到 Agent 之间的协作过程,透明可控。军师作为调度中心,避免了多 Agent 抢答的混乱。
- 本文作者: Wynne Yin
- 本文链接: https://wynneyin.github.io/2026/02/25/朝廷系统搭建指南(1)/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!
