npm i chat 发布:一套代码接入主流聊天平台的 Chat SDK 开源公测

1 分钟阅读
2026 年 2 月 23 日
在多个平台上构建聊天机器人,传统上往往意味着要维护多套代码库,并分别处理各个平台的 API。
今天,我们将全新的 Chat SDK 开源,并以公测(public beta)形式向公众开放。它是一个统一的 TypeScript 库,让团队只需编写一次 bot 逻辑,就能部署到 Slack、Microsoft Teams、Google Chat、Discord、GitHub 和 Linear。
其事件驱动架构内置了类型安全的处理器,可处理提及(mentions)、消息、表情反应(reactions)、按钮点击和斜杠命令(slash commands)。团队还可以使用 JSX 卡片与模态框构建用户界面,并在各平台上以原生方式渲染。
SDK 通过可插拔适配器处理分布式状态管理,支持 Redis、ioredis 和内存存储。
import { Chat } from "chat";import { createSlackAdapter } from "@chat-adapter/slack";import { createRedisState } from "@chat-adapter/state-redis";const bot = new Chat({ userName: "mybot", adapters: { slack: createSlackAdapter(), }, state: createRedisState(),});bot.onNewMention(async (thread) => { await thread.subscribe(); await thread.post("Hello! I am listening to this thread.");});
这是一个简单示例:创建带 Slack adapter 和 Redis state 的 Chat 实例,并在收到新提及时进行响应。
你可以向任意 provider 发送字符串、对象、AST,甚至 JSX 消息。
import { Card, CardText, Actions, Button } from "chat";await thread.post( <Card title="Order #1234"> <CardText>Your order has been received!</CardText> <Actions> <Button id="approve" style="primary">Approve</Button> <Button id="reject" style="danger">Reject</Button> </Actions> </Card>);
Chat SDK 的 post() 函数可直接接收 AI SDK 的文本流,从而将 AI 响应及其他增量内容实时流式推送到聊天平台。
import { ToolLoopAgent } from "ai";const agent = new ToolLoopAgent({ model: "anthropic/claude-4.6-sonnet", instructions: "You are a helpful assistant.",});bot.onNewMention(async (thread, message) => { const result = await agent.stream({ prompt: message.text }); await thread.post(result.textStream);});
该框架以核心 chat 包为起点,并通过模块化平台 adapter 实现扩展。官方已提供多种实践指南,包括:使用 Next.js 与 Redis 构建 Slack bot、使用 Nuxt 构建 Discord 支持 bot、使用 Hono 构建 GitHub bot,以及自动化代码审查 bot。
访问文档了解更多。
在找 chatbot 模板?现在请看这里。

