Skip to content
2026-09-29 04:20492 字AIOpenClawChannels频道适配器

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(会话绑定)

每一篇文章,都是时间的标本