chat-sdk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseChat SDK
Chat SDK
Unified TypeScript SDK for building chat bots across Slack, Teams, Google Chat, Discord, Telegram, GitHub, Linear, and WhatsApp. Write bot logic once, deploy everywhere.
用于跨Slack、Teams、Google Chat、Discord、Telegram、GitHub、Linear和WhatsApp构建聊天机器人的统一TypeScript SDK,只需编写一次机器人逻辑,即可部署到所有平台。
Start with published sources
从已发布资源入手
When Chat SDK is installed in a user project, inspect the published files that ship in :
node_modulesnode_modules/chat/docs/ # bundled docs
node_modules/chat/dist/index.d.ts # core API types
node_modules/chat/dist/jsx-runtime.d.ts # JSX runtime types
node_modules/chat/docs/contributing/ # adapter-authoring docs
node_modules/chat/docs/guides/ # framework/platform guidesIf one of the paths below does not exist, that package is not installed in the project yet.
Read these before writing code:
- — install and setup
node_modules/chat/docs/getting-started.mdx - —
node_modules/chat/docs/usage.mdxconfig and lifecycleChat - — event routing and handlers
node_modules/chat/docs/handling-events.mdx - — thread/channel/message model
node_modules/chat/docs/threads-messages-channels.mdx - — post, edit, delete, schedule
node_modules/chat/docs/posting-messages.mdx - — AI SDK integration and streaming semantics
node_modules/chat/docs/streaming.mdx - — JSX cards
node_modules/chat/docs/cards.mdx - — button/select interactions
node_modules/chat/docs/actions.mdx - — modal submit/close flows
node_modules/chat/docs/modals.mdx - — slash command routing
node_modules/chat/docs/slash-commands.mdx - — DM behavior and
node_modules/chat/docs/direct-messages.mdxopenDM() - — attachments/uploads
node_modules/chat/docs/files.mdx - — persistence, locking, dedupe
node_modules/chat/docs/state.mdx - — cross-platform feature matrix
node_modules/chat/docs/adapters.mdx - — exact
node_modules/chat/docs/api/chat.mdxAPIChat - — exact
node_modules/chat/docs/api/thread.mdxAPIThread - — exact
node_modules/chat/docs/api/message.mdxAPIMessage - — modal element and event details
node_modules/chat/docs/api/modals.mdx
For the specific adapter or state package you are using, inspect that installed package's export surface in .
dist/index.d.tsnode_modules当用户项目中安装了Chat SDK后,可以查看中附带的已发布文件:
node_modulesnode_modules/chat/docs/ # 打包后的文档
node_modules/chat/dist/index.d.ts # 核心API类型定义
node_modules/chat/dist/jsx-runtime.d.ts # JSX运行时类型定义
node_modules/chat/docs/contributing/ # 适配器开发文档
node_modules/chat/docs/guides/ # 框架/平台指南如果下方的某个路径不存在,说明对应的包尚未安装到项目中。
编写代码前请先阅读以下文档:
- — 安装与配置
node_modules/chat/docs/getting-started.mdx - —
node_modules/chat/docs/usage.mdx配置与生命周期Chat - — 事件路由与处理器
node_modules/chat/docs/handling-events.mdx - — 线程/频道/消息模型
node_modules/chat/docs/threads-messages-channels.mdx - — 发布、编辑、删除、定时发送
node_modules/chat/docs/posting-messages.mdx - — AI SDK集成与流式语义
node_modules/chat/docs/streaming.mdx - — JSX卡片
node_modules/chat/docs/cards.mdx - — 按钮/选择器交互
node_modules/chat/docs/actions.mdx - — 模态框提交/关闭流程
node_modules/chat/docs/modals.mdx - — 斜杠命令路由
node_modules/chat/docs/slash-commands.mdx - — 私信行为与
node_modules/chat/docs/direct-messages.mdx方法openDM() - — 附件/上传
node_modules/chat/docs/files.mdx - — 持久化、锁、去重
node_modules/chat/docs/state.mdx - — 跨平台功能矩阵
node_modules/chat/docs/adapters.mdx - — 完整
node_modules/chat/docs/api/chat.mdxAPI说明Chat - — 完整
node_modules/chat/docs/api/thread.mdxAPI说明Thread - — 完整
node_modules/chat/docs/api/message.mdxAPI说明Message - — 模态框元素与事件详情
node_modules/chat/docs/api/modals.mdx
如果你使用了特定的适配器或状态包,可以查看中对应已安装包的导出内容。
node_modulesdist/index.d.tsQuick start
快速开始
typescript
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(),
dedupeTtlMs: 600_000,
});
bot.onNewMention(async (thread) => {
await thread.subscribe();
await thread.post("Hello! I'm listening to this thread.");
});
bot.onSubscribedMessage(async (thread, message) => {
await thread.post(`You said: ${message.text}`);
});typescript
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(),
dedupeTtlMs: 600_000,
});
bot.onNewMention(async (thread) => {
await thread.subscribe();
await thread.post("Hello! I'm listening to this thread.");
});
bot.onSubscribedMessage(async (thread, message) => {
await thread.post(`You said: ${message.text}`);
});Core concepts
核心概念
- Chat — main entry point; coordinates adapters, routing, locks, and state
- Adapters — platform-specific integrations for Slack, Teams, Google Chat, Discord, Telegram, GitHub, Linear, and WhatsApp
- State adapters — persistence for subscriptions, locks, dedupe, and thread state
- Thread — conversation context with ,
post(),stream(),subscribe(),setState()startTyping() - Message — normalized content with ,
text, attachments, author info, and platformformattedraw - Channel — container for threads and top-level posts
- Chat — 主入口,负责协调适配器、路由、锁和状态
- Adapters — 针对Slack、Teams、Google Chat、Discord、Telegram、GitHub、Linear、WhatsApp的平台专属集成
- State adapters — 用于订阅、锁、去重和线程状态的持久化
- Thread — 会话上下文,提供、
post()、stream()、subscribe()、setState()等方法startTyping() - Message — 标准化内容,包含、
text、附件、作者信息和平台原生formatted数据raw - Channel — 线程和顶层帖子的容器
Event handlers
事件处理器
| Handler | Trigger |
|---|---|
| Bot @-mentioned in an unsubscribed thread |
| New DM in an unsubscribed DM thread |
| Any message in a subscribed thread |
| Regex match in an unsubscribed thread |
| Emoji added or removed |
| Button clicks and select/radio interactions |
| Modal form submitted |
| Modal dismissed/cancelled |
| Slash command invocation |
| Slack assistant thread opened |
| Slack assistant context changed |
| Slack App Home opened |
| Slack member joined channel event |
Read , , , and before wiring handlers. behavior is documented in .
node_modules/chat/docs/handling-events.mdxnode_modules/chat/docs/actions.mdxnode_modules/chat/docs/modals.mdxnode_modules/chat/docs/slash-commands.mdxonDirectMessagenode_modules/chat/docs/direct-messages.mdx| 处理器 | 触发条件 |
|---|---|
| 机器人在未订阅的线程中被@提及 |
| 未订阅的私信线程中收到新消息 |
| 已订阅线程中收到任意消息 |
| 未订阅线程中出现匹配正则表达式的内容 |
| 表情符号被添加或移除 |
| 按钮点击、选择器/单选框交互 |
| 模态框表单提交 |
| 模态框被关闭/取消 |
| 斜杠命令被调用 |
| Slack助理线程被打开 |
| Slack助理上下文变更 |
| Slack应用主页被打开 |
| Slack成员加入频道事件 |
在绑定处理器前请先阅读、、和。的行为说明请参考。
node_modules/chat/docs/handling-events.mdxnode_modules/chat/docs/actions.mdxnode_modules/chat/docs/modals.mdxnode_modules/chat/docs/slash-commands.mdxonDirectMessagenode_modules/chat/docs/direct-messages.mdxStreaming
流式传输
Pass any to or . For AI SDK, prefer over when available so step boundaries are preserved.
AsyncIterable<string>thread.post()thread.stream()result.fullStreamresult.textStreamtypescript
import { ToolLoopAgent } from "ai";
const agent = new ToolLoopAgent({ model: "anthropic/claude-4.5-sonnet" });
bot.onNewMention(async (thread, message) => {
const result = await agent.stream({ prompt: message.text });
await thread.post(result.fullStream);
});Key details:
- controls post+edit fallback cadence
streamingUpdateIntervalMs - defaults to
fallbackStreamingPlaceholderText; set"..."to disablenull - Structured support is Slack-only; other adapters ignore non-text chunks
StreamChunk
你可以将任意传入或。如果使用AI SDK,优先选用而非,这样可以保留步骤边界。
AsyncIterable<string>thread.post()thread.stream()result.fullStreamresult.textStreamtypescript
import { ToolLoopAgent } from "ai";
const agent = new ToolLoopAgent({ model: "anthropic/claude-4.5-sonnet" });
bot.onNewMention(async (thread, message) => {
const result = await agent.stream({ prompt: message.text });
await thread.post(result.fullStream);
});关键细节:
- 控制发布+编辑的 fallback 节奏
streamingUpdateIntervalMs - 默认值为
fallbackStreamingPlaceholderText,设置为"..."可禁用null - 结构化仅支持Slack,其他适配器会忽略非文本块
StreamChunk
Cards and modals (JSX)
卡片与模态框(JSX)
Set in .
jsxImportSource: "chat"tsconfig.jsonCard components:
- ,
Card,CardText,Section,Fields,Field,Button,CardLink,LinkButton,Actions,Select,SelectOption,RadioSelect,Table,ImageDivider
Modal components:
- ,
Modal,TextInput,Select,SelectOptionRadioSelect
tsx
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>
);在中设置。
tsconfig.jsonjsxImportSource: "chat"卡片组件:
- 、
Card、CardText、Section、Fields、Field、Button、CardLink、LinkButton、Actions、Select、SelectOption、RadioSelect、Table、ImageDivider
模态框组件:
- 、
Modal、TextInput、Select、SelectOptionRadioSelect
tsx
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>
);Adapter inventory
适配器清单
Official platform adapters
官方平台适配器
| Platform | Package | Factory |
|---|---|---|
| Slack | | |
| Microsoft Teams | | |
| Google Chat | | |
| Discord | | |
| GitHub | | |
| Linear | | |
| Telegram | | |
| WhatsApp Business Cloud | | |
| 平台 | 包名 | 工厂方法 |
|---|---|---|
| Slack | | |
| Microsoft Teams | | |
| Google Chat | | |
| Discord | | |
| GitHub | | |
| Linear | | |
| Telegram | | |
| WhatsApp Business Cloud | | |
Official state adapters
官方状态适配器
| State backend | Package | Factory |
|---|---|---|
| Redis | | |
| ioredis | | |
| PostgreSQL | | |
| Memory | | |
| 状态后端 | 包名 | 工厂方法 |
|---|---|---|
| Redis | | |
| ioredis | | |
| PostgreSQL | | |
| Memory | | |
Community adapters
社区适配器
chat-state-cloudflare-do@beeper/chat-adapter-matrixchat-adapter-imessage@bitbasti/chat-adapter-webex@resend/chat-sdk-adapterchat-adapter-baileys
chat-state-cloudflare-do@beeper/chat-adapter-matrixchat-adapter-imessage@bitbasti/chat-adapter-webex@resend/chat-sdk-adapterchat-adapter-baileys
Coming-soon platform entries
即将支持的平台
- Signal
- X
- Messenger
- Signal
- X
- Messenger
Building a custom adapter
构建自定义适配器
Read these published docs first:
node_modules/chat/docs/contributing/building.mdxnode_modules/chat/docs/contributing/testing.mdxnode_modules/chat/docs/contributing/publishing.mdx
Also inspect:
- —
node_modules/chat/dist/index.d.tsand related interfacesAdapter - — shared errors and utilities
node_modules/@chat-adapter/shared/dist/index.d.ts - Installed official adapter files — reference implementations for config and APIs
dist/index.d.ts
A custom adapter needs request verification, webhook parsing, message/thread/channel operations, ID encoding/decoding, and a format converter. Use from and shared utilities from .
BaseFormatConverterchat@chat-adapter/shared请先阅读以下已发布文档:
node_modules/chat/docs/contributing/building.mdxnode_modules/chat/docs/contributing/testing.mdxnode_modules/chat/docs/contributing/publishing.mdx
同时可查看:
- —
node_modules/chat/dist/index.d.ts及相关接口定义Adapter - — 通用错误和工具方法
node_modules/@chat-adapter/shared/dist/index.d.ts - 已安装的官方适配器的文件 — 参考配置和API的实现示例
dist/index.d.ts
自定义适配器需要实现请求验证、webhook解析、消息/线程/频道操作、ID编码/解码以及格式转换器功能。你可以使用提供的和的通用工具方法。
chatBaseFormatConverter@chat-adapter/sharedWebhook setup
Webhook配置
Each registered adapter exposes . Wire those directly to your HTTP framework routes. See and for framework-specific route patterns.
bot.webhooks.<name>node_modules/chat/docs/guides/slack-nextjs.mdxnode_modules/chat/docs/guides/discord-nuxt.mdx每个已注册的适配器都会暴露,直接将其绑定到你的HTTP框架路由即可。框架专属的路由模式可参考和。
bot.webhooks.<name>node_modules/chat/docs/guides/slack-nextjs.mdxnode_modules/chat/docs/guides/discord-nuxt.mdx