Skip to content
CrewAI 文档版本 跟随上游 源快照 c4853383 更新于 2026-10-18

Channels

借助 CopilotKit Channels SDK 和托管的 Intelligence 平台,把同一个 CrewAI 智能体作为 Slack 或 Teams 机器人运行。

在用户已经所在的地方触达他们

你在概览中构建的 CrewAI 智能体不必只活在 Web 应用背后。同一个 Crew 或 Flow 可以作为机器人运行在消息平台内。无需重建,也无需为智能体逻辑再复制一份:智能体依旧通过 AG-UI 协议暴露,由一个通道从 Slack 或 Microsoft Teams 驱动它。

CopilotKit 的 Channels SDK 提供这样的通道。你在一个小型运行时中声明 createChannel,指向你的 CrewAI 智能体,由 CopilotKit 托管的 Intelligence 平台来撮合与消息服务商之间的连接。

NOTE

与本节的其余内容不同,Channels 不是自托管的。它通过 CopilotKit Intelligence 运行——这是 Channels 在设计上必需的一环(提供免费额度)。Intelligence 持有平台连接和凭据,接收每个平台事件,并把该轮对话投递到你的通道进程;你的进程运行智能体并把回复流式送回。你在 Intelligence 仪表盘中一次性配置 Slack,平台凭据永远不会进入你的进程。你的智能体、工具和状态始终归你所有。

各部分如何衔接

你的 CrewAI 智能体服务器无需任何改动。它照旧通过 AG-UI 提供你的 Crew 或 Flow,与概览中完全一样。你要新增的是一个用 @copilotkit/channels 构建的独立长期运行 Node 进程:它在 CopilotRuntime 上注册一个通道,连接到 Intelligence,并在消息到达时运行你的智能体。

Slack / Teams  ──►  CopilotKit Intelligence  ──►  channel process (Node)  ──►  CrewAI server (AG-UI)  ──►  Crew / Flow

通道进程与 Intelligence 网关保持持久连接,因此它需要一个长期运行的宿主——无服务器的请求处理函数无法持有这条连接。与此同时,你的 CrewAI 服务器可以继续为概览中的 Web 前端提供服务:Web 应用和通道只是一个 AG-UI 端点的两个客户端。

集成指南

Install the Channels packages

Channels SDK 开箱即用——每个平台都包含在同一个包中,无需安装各平台的适配器。把它安装在承载通道的运行时和 CrewAI AG-UI 客户端旁边:

bash
npm install @copilotkit/channels @copilotkit/runtime @ag-ui/crewai

Create a Channel in Intelligence

在 CopilotKit 仪表盘中创建一个通道并连接 Slack——Intelligence 会引导你创建 Slack 应用并保管其凭据。这样你的进程只需要两个环境变量,都来自该仪表盘:

bash
export INTELLIGENCE_API_KEY=...      # authenticates the runtime with Intelligence (free tier available)
export INTELLIGENCE_CHANNEL_ID=...   # the Channel ID, matched by createChannel({ name })

Define the channel

createChannel 声明该通道并挂载你的智能体。以按线程的工厂方式构建智能体,使每个对话都拥有自己的会话,沿用概览中 Web 运行时所用的同一个 CrewAIAgent,并指向你的 AG-UI 端点。identifyUser: "platform" 让 Intelligence 把每个平台用户映射到稳定的身份。

ts
// channel.ts
import { createChannel } from "@copilotkit/channels";
import { CrewAIAgent } from "@ag-ui/crewai";

const channel = createChannel({
  name: process.env.INTELLIGENCE_CHANNEL_ID!, // must match the Channel ID in Intelligence
  identifyUser: "platform",
  // A fresh agent per conversation, pointed at your CrewAI AG-UI endpoint.
  agent: (threadId) => {
    const agent = new CrewAIAgent({ url: "http://localhost:8000/recipe" });
    agent.threadId = threadId;
    return agent;
  },
});

// A mention subscribes the thread and runs the agent; afterwards every message
// in a subscribed thread runs it without needing another mention.
channel.onMention(async ({ thread }) => {
  await thread.subscribe();
  await thread.runAgent();
});

channel.onMessage(async ({ thread }) => {
  if (await thread.isSubscribed()) await thread.runAgent();
});

export { channel };

Register the channel on the runtime

用 Intelligence 网关和你的通道创建 CopilotRuntime,然后用 createCopilotNodeListener 提供服务。agents 映射保持为空——通道自带智能体。等待通道就绪,这样配置有问题时在启动阶段就会明确失败。

ts
// server.ts
import { createServer } from "node:http";
import { CopilotRuntime, CopilotKitIntelligence } from "@copilotkit/runtime/v2";
import { createCopilotNodeListener } from "@copilotkit/runtime/v2/node";
import { channel } from "./channel";

const runtime = new CopilotRuntime({
  agents: {}, // the channel supplies its own agent; no web-facing agents needed
  intelligence: new CopilotKitIntelligence({
    apiKey: process.env.INTELLIGENCE_API_KEY!, // free tier available
  }),
  channels: [channel],
});

const listener = createCopilotNodeListener({ runtime });
await listener.channels?.ready({ timeoutMs: 15_000 });

createServer(listener).listen(3123, () => {
  console.log("Channels runtime listening on port 3123");
});

Run the channel runtime

与你的 CrewAI 智能体服务器一起启动它:

bash
uvicorn server:app --port 8000   # terminal 1 — CrewAI agent server
npx tsx server.ts                 # terminal 2 — Channels runtime

在 Slack 或 Teams 中提及该机器人,它就会运行你的 Crew 或 Flow,并把回复流式送回该线程。线程会保持订阅状态,因此后续消息无需再次提及即可运行。

事件模型

通道用处理函数对平台事件做出反应,每个处理函数会收到一个 thread,你可以用几个方法来驱动它:

  • channel.onMention 在用户 @ 提及机器人时触发。调用 thread.subscribe() 加入线程,然后调用 thread.runAgent() 在该提及上运行你的 CrewAI 智能体。
  • channel.onMessage 在机器人可见线程中的每条消息上触发。用 thread.isSubscribed() 加以限制,使智能体只在其已加入的地方响应,然后调用 thread.runAgent()。
  • thread.runAgent() 为当前轮次运行所关联的 CrewAI 智能体,并将其输出流式输出回频道。传入 { prompt } 可覆盖智能体运行所依据的文本。

你的智能体接收普通的 AG-UI RunAgentInput 并发出普通的 AG-UI 事件;平台机制留在频道之后,因此同一个 Crew 或 Flow 在每个平台上都无需改动即可运行。频道还暴露了用于欢迎、中断、命令、回应和模态框的处理程序 —— 完整接口见 Channel 参考。

平台支持

托管的 Intelligence 路径目前覆盖 Slack 和 Microsoft Teams —— 同一份频道代码在两者上都能运行,message.platform / thread.platform 会报告原生来源。其他平台(Discord、Telegram、WhatsApp)通过开发者自运营的直接适配器接入,而非托管路径 —— 由你自己的进程持有平台凭证和传输。查看 CopilotKit Channels 文档 了解当前平台列表和各平台设置。

相关

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。