Channels 频道抽象
源码路径:src/channels/(~160 文件:channels/ 80+ + channels/plugins/ 80+)
Channels 是 OpenClaw 的消息渠道抽象层。30+ 消息平台(微信、钉钉、飞书、Discord 等)通过统一的 ChannelPlugin 接口接入。
核心职责
| 职责 | 说明 |
|---|---|
| 渠道注册表 | 管理所有已注册渠道的 ID、元数据、查找 |
| 适配器接口 | 定义 30+ 适配器类型(配置、安全、出站、网关等) |
| 消息处理 | 入站事件分类、消息动作、格式转换 |
| 会话绑定 | 对话线程绑定、会话上下文管理 |
| 流式处理 | 草稿流、分块输出、流适配 |
| 审批流程 | 渠道级审批能力 |
ChannelPlugin 架构
typescript
type ChannelPlugin = {
id: ChannelId;
meta: ChannelMeta;
capabilities: ChannelCapabilities;
defaults: { queue?: { debounceMs } };
// 适配器接口(30+)
config: ChannelConfigAdapter;
setup: ChannelSetupAdapter;
security: ChannelSecurityAdapter;
outbound: ChannelOutboundAdapter;
status: ChannelStatusAdapter;
gateway: ChannelGatewayAdapter;
auth: ChannelAuthAdapter;
streaming: ChannelStreamingAdapter;
threading: ChannelThreadingAdapter;
agentTools: ChannelAgentToolFactory;
// ... 更多
};注册表操作
typescript
normalizeChannelId(id) // 标准化渠道ID
listRegisteredChannelPluginIds() // 列出所有渠道
getRegisteredChannelPluginMeta(id) // 获取渠道元数据消息处理管线
入站事件到达
→ 事件分类 (inbound-event/classification.ts)
→ 上下文绑定 (conversation-binding-context.ts)
→ 消息动作解析 (message-actions.ts)
→ 工具调用处理 (message-tool-api.ts)
→ 回复生成
→ 出站适配 (outbound adapter)适配器模式设计
每个渠道只需实现所需适配器,未实现的适配器使用默认实现:
必须实现:config, meta, id
按需实现:streaming, threading, approval, auth, ...设计模式
| 模式 | 应用 |
|---|---|
| 适配器模式 | 30+ 适配器接口,渠道按需实现 |
| 注册表模式 | 集中管理所有渠道 |
| 能力协商 | capabilities 声明支持的功能 |
| 事件分类 | 入站事件自动分类 |
依赖关系
- 上游:Plugin System(渠道作为插件加载)
- 下游:Gateway Server(渠道运行时管理)、Auto-Reply(入站消息)
- 横切:Session Manager(会话绑定)