harness - IM 通道接口
2026年7月4日 下午1:08:59
solon-ai-harness 提供了统一的 IM 通道接口 Channel,用于将 Agent 的能力接入各种即时通讯平台(微信、飞书、钉钉等)。
1、Channel 接口定义
public interface Channel {
/**
* 通道名称标识
*/
String getChannelName();
/**
* 查询指定会话是否已绑定此通道
*/
boolean isBound(String sessionId);
/**
* 向指定会话绑定的 IM 用户发送回复
*/
void sendReply(String sessionId, String text, boolean isFinal);
}
2、核心概念
- 会话绑定:IM 通道通过
sessionId与 Agent 会话关联。一个会话可以同时绑定多个通道。 - 回复推送:Agent 在执行过程中产生的输出,通过
sendReply推送给 IM 用户。isFinal标识是否为最终回复(true表示本轮对话结束)。 - 通道存储:通道绑定关系存储在
{harnessHome}/channels/目录下。
3、实现自定义通道
以实现一个微信通道为例:
public class WeChatChannel implements Channel {
@Override
public String getChannelName() {
return "wechat";
}
@Override
public boolean isBound(String sessionId) {
// 检查该会话是否绑定了微信用户
return WeChatBindingRegistry.isBound(sessionId);
}
@Override
public void sendReply(String sessionId, String text, boolean isFinal) {
// 将 Agent 回复推送到微信
String userOpenId = WeChatBindingRegistry.getOpenId(sessionId);
WeChatApiClient.sendMessage(userOpenId, text);
}
}
4、与流式输出整合
通道通常与 WebStreamBuilder 一起使用,将流式输出同步推送到 IM 平台:
WebStreamBuilder streamBuilder = new WebStreamBuilder(engine);
// 绑定通道
streamBuilder.bind(wechatChannel);
streamBuilder.bind(feishuChannel);
streamBuilder.bind(dingTalkChannel);
// 在 Web 端启动后,Agent 的流式回复会自动推送到已绑定的 IM 通道
5、通道存储目录
通道相关的绑定数据存储在 {harnessHome}/channels/ 子目录下:
{harnessHome}/
└── channels/
└── # IM 通道绑定数据
可通过 engine.getHarnessChannels() 获取该路径。
6、多端架构说明
在 SolonCode CLI 项目中,Channel 接口与 WebSocket、Web Controller 一起构成了多端架构:
- CLI 端:直接在终端输出,不使用 Channel。
- Web 端:通过 WebSocket 推送给前端界面。
- IM 端:通过 Channel 接口推送到微信/飞书/钉钉等平台。
- ACP 端:通过 ACP(Agent Client Protocol)协议推送给 IDE 插件。
各端的回复链路互相独立,通过 WebStreamBuilder.bind() 统一编排。