# AI chat

> Chats, projects, participants and a message tree (or one linear list) in the canonical AI message format or the SDK's own parts, with runs (and the id of a durable engine's run), run steps for progress, a compare-and-set stream claim, harness sessions, tool approvals and policies, pending inputs, cited sources, feedback, hashed share links, a model catalog per plan, moderation events and private Realtime topics per chat and per user.

- Category: AI
- Module: `ai-chat`
- Modes: `managed`, `custom`
- Docs: https://bettersupabase.com/docs/blocks/ai-chat
- Page: https://bettersupabase.com/blocks/ai-chat

## Install

```bash
npx better-supabase add ai-chat
```

The command also installs `updated-at`, `memberships`, `permissions`, `streams`.

## Configure

Add the block to `better-supabase.config.ts`:

```ts
import { defineConfig } from "better-supabase/config";

export default defineConfig({
  blocks: {
    // Required by ai-chat; add installs them too.
    "updated-at": {},
    memberships: {},
    permissions: {},
    streams: {},
    // Modes: "managed", "custom". The default is "managed".
    "ai-chat": { mode: "managed" },
  },
});
```

## Client

The React hooks are the registry item `@better-supabase/use-ai-chat` (`npx shadcn add @better-supabase/use-ai-chat`). They return data and actions and render no UI. Source: https://github.com/ScaleDockHQ/better-supabase/blob/main/packages/registry/items/hooks/use-ai-chat.ts

## Events

- `ai_chat.share_revoked`
- `ai_chat.shared`
- `ai_chat_message.completed`
- `ai_tool_approval.decided`
- `ai_tool_policy.updated`

## Related blocks

Requires: [memberships](https://bettersupabase.com/blocks/memberships.md), [permissions](https://bettersupabase.com/blocks/permissions.md), [streams](https://bettersupabase.com/blocks/streams.md)

Works with: [share-links](https://bettersupabase.com/blocks/share-links.md), [entitlements](https://bettersupabase.com/blocks/entitlements.md), [ai-keys](https://bettersupabase.com/blocks/ai-keys.md), [approvals](https://bettersupabase.com/blocks/approvals.md), [data-lifecycle](https://bettersupabase.com/blocks/data-lifecycle.md), [teams](https://bettersupabase.com/blocks/teams.md), [usage](https://bettersupabase.com/blocks/usage.md)

## Recipes

- [Chat with history](https://bettersupabase.com/recipes/chat-with-history.md): An AI chat that stores every message, resumes a stored conversation and streams the reply.
- [Chat message as Markdown](https://bettersupabase.com/recipes/chat-message-markdown.md): An assistant answer rendered as Markdown with Streamdown: a caret while it streams, static once stored, and only safe elements and links.
