# Threaded chat

Threads off any message, with the agent answering inside the thread, so side questions stop burying the main conversation.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Threaded chat. A release check: a side question about one item opens a thread off the answer, and the agent replies inside it. Preview: https://chatcn.dev/preview/threaded-chat-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/threaded-chat
```

## Usage

```tsx
import { ThreadedChat } from "@/components/ui/chatcn/pro/threaded-chat"

<ThreadedChat
  messages={messages}
  status={status}
  onSend={send}
  threads={threads}
  threadStatus={threadStatus}
  onReply={(parentId, text) => askInThread(parentId, text)}
/>
```

A side question about one part of an answer goes into a thread instead of the main conversation. Hovering or focusing an assistant message offers Reply in thread; once a thread has replies, the message shows the count and the last reply, and clicking it opens the thread again. The thread opens beside the conversation with the message it came from pinned at the top, and the agent answers there. Main and thread keep their own status, so the reader can keep using one while the other answers. On a host narrower than 720 px the thread takes the whole view, with a way back. `canThread` decides which messages offer a thread.

## Examples

### States

Example: States. A reply count with the last reply, the thread open beside the conversation, answering in the thread, and a failed reply. Preview: https://chatcn.dev/preview/threaded-chat-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `messages` | `ChatcnMessage[]` | The thread, in the kit's message contract |
| `status`, `error`, `onStop`, `onRetry` | `"ready" | "submitted" | "streaming" | "error"` | Send becomes Stop while it streams; an error offers Try again |
| `value`, `defaultValue`, `onValueChange` | `string` | The draft, controlled or not |
| `onSend` | `(text) => void` | A message in the main conversation |
| `threads` | `Record<string, ChatcnMessage[]>` | Replies, keyed by the parent message id |
| `openThread`, `defaultOpenThread`, `onOpenThreadChange` | `string | null` | The open thread |
| `threadStatus`, `threadError`, `onThreadRetry`, `onThreadStop` |  | The open thread's own state |
| `onReply` | `(parentId, text) => void` | A reply in the open thread |
| `replyValue`, `onReplyValueChange` | `string` | The thread's draft, controlled or not |
| `canThread` | `(message) => boolean` | Default: assistant messages |
| `header` | `ReactNode` | Above the main conversation |
| `disabled` | `boolean` |  |
| `labels` | `Partial<ThreadedChatLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Tab to a message's thread button, Enter | Open its thread, focus in the reply box |
| Escape in the thread | Close it, focus back on the button that opened it |
| Enter, Shift Enter | Send, new line |

## Accessibility

- Thread buttons report whether their thread is open and point at the thread panel.
- The thread is a labelled region; while it covers a narrow screen, the conversation behind it is inert.
- Reply counts are words, not badges.
- Main and thread each announce their own answers politely.

## Motion

The thread enters with a short rise and fade; the reply button fades in on hover. Under reduced motion the thread appears in place.

Source: https://chatcn.dev/docs/components/threaded-chat