Screens

AI Chat

A complete chat app screen — history sidebar, a thread with tool calls, cited answers and feedback, follow-up chips and a composer with a context meter.

3 variantsMIT

Preview

Full screen

Installation

Copies the screen into components/ and installs every component it uses, with their dependencies and theme tokens.

pnpm dlx shadcn@latest add https://uilab.tmih06.com/r/ai-chat.json

Usage

Pass your own data and wire the callbacks to your agent runtime.

import { AIChat } from "@/components/ai-chat";
import { ToolCallBlock } from "@/components/ui/tool-call-block";
import { SourceList } from "@/components/ui/source-list";
import { StreamingMessage } from "@/components/ui/streaming-message";

export function ChatScreen({ chat, run }: { chat: Chat; run: AgentRun }) {
  return (
    <AIChat
      data={{
        conversations: chat.history,
        activeId: chat.id,
        title: chat.title,
        model: run.model,
        user: { name: chat.user.name },
        starters: chat.starters,
        followUps: run.done ? run.followUps : undefined,
        context: { limit: run.contextLimit, segments: run.contextSegments },
        // Each turn's content is any React node — compose answers from components.
        messages: chat.turns.map((turn) => ({
          id: turn.id,
          role: turn.role,
          at: turn.createdAt,
          content:
            turn.role === "user" ? (
              turn.text
            ) : (
              <>
                {turn.toolCalls.map((call) => (
                  <ToolCallBlock key={call.id} data={call} />
                ))}
                {turn.streaming ? (
                  <StreamingMessage
                    data={{ content: turn.text, status: "streaming" }}
                    showHeader={false}
                  />
                ) : (
                  <SourceList data={{ sources: turn.sources, text: turn.text }} />
                )}
              </>
            ),
        })),
      }}
      status={run.streaming ? "streaming" : "idle"}
      onSend={(text) => run.send(text)}
      onStop={() => run.stop()}
      onNewChat={() => chat.create()}
      onSelectConversation={(c) => chat.open(c.id)}
      onShare={() => chat.share()}
    />
  );
}

// Pass null while the first screen payload is in flight. Keep stale data and
// set loading when refetching; empty messages still mean the intentional new-chat state.
export function ChatScreenState({ chatQuery, run }: { chatQuery: ChatQuery; run: AgentRun }) {
  return (
    <AIChat
      data={chatQuery.data ? toAIChatData(chatQuery.data, run) : null}
      loading={chatQuery.isRefetching}
      skeleton={{ count: 5 }}
      status={run.streaming ? "streaming" : "idle"}
      onSend={(text) => run.send(text)}
      onStop={() => run.stop()}
    />
  );
}

Props

PropTypeDefaultDescription
data*AIChatData | null—History, the open chat (title, model, turns), starter cards, follow-up chips, context usage and the signed-in user. Pass null while the screen loads; empty messages remain the intentional new-chat state once loaded.
loadingbooleanfalseForce the layout-preserving screen skeleton while refetching, even when stale data is available. Stale data contributes shape and cardinality only.
skeleton{ count?: number }—Shape hints for the loading state; count is the number of message placeholders in the thread.
status"idle" | "streaming""idle"While streaming, the composer queues new messages and follow-up chips are hidden.
onSend(text: string) => void—A message from the composer, a starter card or a follow-up chip.
onStop() => void—Stop the current run.
onNewChat() => void—From the history panel. Pass empty `messages` to show the starter cards.
onSelectConversation(conversation: Conversation) => void—A chat picked in the history sidebar or drawer.
onShare() => void—Share button in the header.
sidebarOpenboolean—Controls the history sidebar at wide widths. The header button and mod+\ toggle it; below 56rem the history opens as a drawer instead.
defaultSidebarOpenbooleantrueInitial sidebar state when uncontrolled.
onSidebarOpenChange(open: boolean) => void—Called when the sidebar is shown or hidden.
localestring—BCP 47 locale for message times and token counts.
labelsPartial<AIChatLabels>—Your own UI strings (loading announcement, sidebar, header, placeholders…) plus labels for the inner components (`conversationList`, `chatThread`, `composer`, `contextMeter`, `suggestions`, `keyHint`), for translating. Right-to-left works under `dir="rtl"`: the history sits on the right.
classNamestring—Extra classes for the root.
type AIChatData = {
  conversations: Conversation[];        // from conversation-list
  activeId?: string;
  title: string;
  model: string;
  messages: ThreadMessage[];            // from chat-thread; content is a ReactNode
  followUps?: PromptSuggestion[];       // chips under the last answer
  starters: PromptSuggestion[];         // cards for an empty chat
  context: { limit: number; segments: ContextSegment[] };
  user: { name: string; avatar?: string };
};

// The screen fills its parent and lays out by its own width (container queries):
// a history sidebar from 56rem, a drawer below that. Give the parent a height.