Chat SDK Documentation

repository·main·Indexed 24 days ago

https://github.com/vercel/chat

A unified TypeScript SDK for building chat bots across multiple platforms, including Slack, Discord, Microsoft Teams, Google Chat, GitHub, Linear, and Telegram, using a single codebase. It includes platform-specific adapters like @chat-adapter/discord and supports both serverless HTTP interactions and persistent Gateway WebSocket connections.

Tokens
249.5K
Snippets
643
Records
1.2K
Agent score
80%

What's inside Chat SDK

  1. Features of the @chat-adapter/web adapter

    main

    The @chat-adapter/web adapter provides a web-based messaging interface with the following capabilities and limitations:

    Messaging

    • Post message: Supported
    • Streaming: Native (SSE / UI message stream)
    • Edit message: Not supported (every assistant turn is a fresh streamed response)
    • Delete message: Not supported
    • File uploads: Not supported (deferred to v2)
    • Scheduled messages: Not supported

    Rich Content

    • Tables: Supported (GFM markdown)
    • Card format: Markdown only in v1 (cards/JSX deferred to v2)
    • Buttons: Not supported
    • Modals: Not supported

    Conversations

    • DMs: Supported (isDM: true for every thread)
    • Mentions: N/A (every web message routes as a DM)
    • Add/Remove reactions: Not supported
    • Typing indicator: N/A (useChat derives a status from the SSE response itself)

    Message History

    • Fetch messages: Via state adapter cache (no platform API)
    • Fetch thread info: Supported (synthesized)
    • Fetch channel messages: Via state adapter cache
    • List threads: Not supported
    • Post channel message: Not supported
    • Fetch single message: Not supported
  2. Core usage patterns in Chat SDK

    main

    To build with Chat SDK, you need to master four core patterns:

    1. Creating a Chat Instance: Initialize the Chat class by providing adapters, state management, and configuration options.
    2. Managing Threads, Messages, and Channels: Understand how to work with these abstractions to ensure consistent behavior across different chat platforms.
    3. Handling Events: Register specific handlers to respond to user actions such as mentions, messages, reactions, and other platform-specific events.
    4. Posting Messages: Use thread.post() to render and send various message types back to users.
  3. Explore Telegram Chat SDK features via the Telegram bot example

    main

    The telegram-chat example serves as a smoke-test harness for the @chat-adapter/telegram package, demonstrating several end-to-end SDK capabilities through an interactive menu:

    • Text & Markdown: Demonstrates MarkdownV2 rendering and streaming edits.
    • Cards & Actions: Demonstrates interactive cards with inline-keyboard actions, callback-data handling, and link buttons.
    • Media & Reactions: Demonstrates file uploads (e.g., PNG and PDF) and on-demand reactions.

    Note that this specific example runs in polling mode (no webhooks or public URL required) and is stateless (no persistence or thread subscription).

  4. Use @chat-adapter/shared utilities

    main

    The @chat-adapter/shared package provides several categories of helpers for building Chat SDK adapters:

    Adapter helpers

    Used to extract specific elements from an AdapterPostableMessage:

    • extractCard(message): Extracts a CardElement.
    • extractFiles(message): Extracts FileUpload[].
    • extractPostableAttachments(message): Extracts Attachment[].

    Buffer conversion

    Helpers for handling binary data:

    • toBuffer(input): Converts Buffer, Blob, string, or URL to a Buffer (asynchronous).
    • toBufferSync(input): Synchronous version of toBuffer.
    • bufferToDataUri(buffer, mimeType): Converts a Buffer to a data URI string.

    Card conversion

    Helpers for rendering UI elements across different platforms:

    • cardToFallbackText(card): Renders a CardElement as plain text for platforms without card support.
    • createEmojiConverter(platform): Creates a function to convert emoji placeholders to platform-specific formats.
    • mapButtonStyle(style): Maps generic button styles to platform-specific variants.
    • renderGfmTable(headers, rows): Renders a GitHub Flavored Markdown table.
    • escapeTableCell(text): Escapes pipe characters in table cells.

    Token encryption

    AES-256-GCM helpers for encrypting OAuth tokens at rest. This is recommended for adapters that persist credentials (e.g., Slack or Linear).

    • encryptToken(plaintext, key): Returns an EncryptedTokenData envelope with a random 12-byte IV.
    • decryptToken(data, key): Decrypts an envelope back to the original string.
    • decodeKey(encoded): Decodes a hex-64 or base64-44 encoded 32-byte key. Throws if the length is incorrect.
    • isEncryptedTokenData(value): Type guard to distinguish envelopes from legacy plaintext.
    • EncryptedTokenData: The envelope structure: { data, iv, tag } (all base64).
  5. Overview of chat/ai utilities

    main

    The chat/ai subpath contains the following core utilities:

    • createChatTools: Generates standalone tool factories that allow an agent to perform actions (post messages, send DMs, react, edit, delete, manage subscriptions) across all registered adapters. It supports built-in approval gates and presets.
    • toAiMessages: Converts Chat SDK Message[] into the { role, content }[] shape expected by AI SDK calls. It handles role mapping, attachments, links, sorting, and optional per-message transforms.
    • Types: Exported types for agent message shapes, tool option contracts, presets, approval configurations, and the binding type that connects tools to a Chat instance.
  6. Understand Token subject types in Vercel Connect

    main

    When requesting a token, you must specify a subject which determines how the token acts:

    • app: The application itself uses the connector's installation. Best for bot accounts or tenant-wide admin actions. These skip the user-consent flow.
    • user: Acts on behalf of a specific signed-in user. The user must authorize the connector once before tokens can be minted.
    • jwt-bearer: Used for exchanging an external identity for a provider token (federated identity).

    Note: In the SDK, subject is required. The Vercel CLI requests a user token by default; use --subject app for an app token.

  7. What the Cloudflare Agents state adapter stores

    main

    The adapter implements the full Chat SDK StateAdapter interface using Durable Object SQLite. It persists:

    • Subscriptions: For thread.subscribe() and thread.unsubscribe().
    • Locks: For per-thread or per-channel concurrency.
    • Queues: Pending message queues for queue, debounce, and burst strategies.
    • KV Cache: Generic key-value cache entries (with optional TTL).
    • Lists: Append-only lists with max-length trimming and TTL refresh.

    This enables features like message deduplication, persistent thread history, and cross-platform transcripts.

  8. How Dial threads are structured

    main

    In the Dial adapter, a Chat SDK thread is represented as a unique pair of phone numbers (your Dial-owned number and the peer's number). The thread ID follows this format:

    dial:{yourDialNumber}:{peerNumber}

    Because every distinct pair is a distinct thread, Chat SDK's per-thread state (such as subscriptions, locks, and conversation memory) is automatically scoped to each individual conversation, preventing data leakage between concurrent users.

  9. Understand Adapter metadata and environment specifications

    main

    Adapters in the Chat SDK are categorized into two types: platform (connecting to messaging services) and state (storing SDK state).

    Each adapter in the catalog provides an env spec that defines how to configure it:

    • required: Variables needed regardless of the authentication mode.
    • credentialModes: Groups of mutually exclusive authentication methods (e.g., choosing between a Bot Token or OAuth credentials).
    • optional: Tuning variables that can be omitted.
    • config: Constructor options that cannot be set via environment variables.
  10. Webex adapter capabilities and limitations

    main

    Capabilities

    • Mentions and DMs
    • Rich text (bold, italic, code, links) via Markdown
    • Adaptive Cards (buttons, selects, radio selects, fields, sections)
    • Modals (form cards with submit/close actions)
    • File uploads (single file per message)
    • Thread support via parent IDs
    • Webhook signature verification (HMAC-SHA1)

    Limitations

    • Reactions: addReaction and removeReaction throw NotImplementedError because reactions are not supported by Webex bot tokens.
    • Typing indicators: Not available in the Webex Messaging API.
    • File uploads: Only one file upload per message is supported.
    • Combinations: Cards and file uploads cannot be combined in the same message.
  11. Understand Photon iMessage adapter limitations

    main

    When building with the Photon iMessage adapter, be aware of the following constraints:

    • Group Chat Addressing: Group chats are session-bound. You cannot send a 'cold' message to a group ID that hasn't been received via the gateway/stream in the current session. DMs, however, can be sent 'cold'.
    • No History/Thread Info: fetchMessages, fetchThread, fetchChannelMessages, and other fetch methods are not supported.
    • No Reaction Removal: removeReaction is not supported.
    • Remote-only Features: Reactions, typing indicators, editing, and modals require Cloud or Self-host mode. Local mode only supports sending and receiving.
    • Plain Text Only: iMessage does not support markdown or structured cards. Outbound markdown is stripped to plain text, and inbound text is parsed into the Chat SDK AST.
    • Platform Constraints: Local mode requires macOS.