Message
One turn of the conversation, the person's or the agent's, with the actions that belong to it.
Installation
pnpm dlx shadcn@latest add @silkware/ai-messageTo copy the pattern by hand, install the components it composes:
pnpm dlx shadcn@latest add @silkware/bubble @silkware/button @silkware/code-block @silkware/message @silkware/tooltipThen install the packages it imports:
pnpm add cn streamdown lucide-reactThe pattern renders the reply with @/components/markdown, which wraps Streamdown and uses Code Block for fenced code. Copy that file and hooks/use-highlighted-code.ts from the repository (the markdown and use-highlighted-code registry items; markdown brings the typeset item with it), then copy the pattern into components/ai-elements/message.tsx:
"use client"
import * as React from "react"import { CheckIcon, CopyIcon, PencilIcon, RefreshCcwIcon, ThumbsDownIcon, ThumbsUpIcon,} from "lucide-react"
import { Markdown } from "@/components/markdown"import { Bubble, BubbleContent } from "@/components/ui/bubble"import { Button } from "@/components/ui/button"import { Message, MessageContent, MessageFooter } from "@/components/ui/message"import { Tooltip, TooltipTrigger } from "@/components/ui/tooltip"
const reply = `Install Dialog, then open it from the gear button in \`site-header.tsx\`.
\`\`\`bashnpx shadcn@latest add dialog\`\`\`
Wrap the button in a \`DialogTrigger\` and put the settings form in \`DialogContent\`. Focus moves into the dialog when it opens, and **Esc** closes it.`
export function MessagePattern() { const [copied, setCopied] = React.useState(false) const [feedback, setFeedback] = React.useState<"like" | "dislike" | null>( null )
React.useEffect(() => { if (!copied) { return }
const timeout = setTimeout(() => setCopied(false), 2000) return () => clearTimeout(timeout) }, [copied])
return ( <div className="mx-auto flex w-full max-w-md flex-col gap-8 py-6 text-sm"> <Message align="end"> <MessageContent className="gap-2"> <Bubble align="end" variant="muted"> <BubbleContent> How do I add a settings dialog? It should open from the gear icon in the header. </BubbleContent> </Bubble> <MessageFooter> <MessageAction label="Copy"> <CopyIcon /> </MessageAction> <MessageAction label="Edit"> <PencilIcon /> </MessageAction> </MessageFooter> </MessageContent> </Message> <Message> <MessageContent className="gap-2"> <Markdown>{reply}</Markdown> <MessageFooter className="px-0"> <MessageAction label={copied ? "Copied" : "Copy"} onPress={() => { navigator.clipboard.writeText(reply) setCopied(true) }} > {copied ? <CheckIcon /> : <CopyIcon />} </MessageAction> <MessageAction label="Good response" aria-pressed={feedback === "like"} onPress={() => setFeedback(feedback === "like" ? null : "like")} > <ThumbsUpIcon className={feedback === "like" ? "fill-current" : undefined} /> </MessageAction> <MessageAction label="Bad response" aria-pressed={feedback === "dislike"} onPress={() => setFeedback(feedback === "dislike" ? null : "dislike") } > <ThumbsDownIcon className={feedback === "dislike" ? "fill-current" : undefined} /> </MessageAction> <MessageAction label="Retry"> <RefreshCcwIcon /> </MessageAction> </MessageFooter> </MessageContent> </Message> </div> )}
function MessageAction({ label, children, ...props}: React.ComponentProps<typeof Button> & { label: string }) { return ( <TooltipTrigger> <Button variant="ghost" size="icon-sm" aria-label={label} {...props}> {children} </Button> <Tooltip> <p>{label}</p> </Tooltip> </TooltipTrigger> )}Replies are set with Typeset
A reply is Markdown, and Markdown is HTML nobody wrote classes for. The Markdown component carries typeset typeset-chat on its root, so Typeset sets its headings, lists, tables, quotes and inline code in the theme's fonts and colours, and the text takes its size from the message around it. Do not style a reply's paragraphs or lists one by one. To set a reply in another preset, pass className="typeset-compact" to Markdown.
Usage
import { Bubble, BubbleContent } from "@/components/ui/bubble"import { Message, MessageContent, MessageFooter } from "@/components/ui/message"<Message align="end"> <MessageContent> <Bubble align="end" variant="muted"> <BubbleContent>Add a settings dialog to the header.</BubbleContent> </Bubble> </MessageContent></Message>
<Message> <MessageContent className="gap-2"> <Markdown>{reply}</Markdown> <MessageFooter className="px-0"> <MessageAction label="Copy"> <CopyIcon /> </MessageAction> </MessageFooter> </MessageContent></Message>The person's turn sits on the end side inside a muted Bubble. The agent's turn has no bubble: it is plain text on the start side, so long answers read like a page. MessageAction is a small helper inside the pattern file, a ghost icon button with a tooltip. Its label becomes both the tooltip and the aria-label.
Examples
Actions
The demo shows both sides. The person's message can be copied or edited. The agent's message can be copied (the icon changes to a check for two seconds), rated up or down, and retried. The rating buttons set aria-pressed, so a screen reader hears whether one is on. In the pattern, only the copy and rating buttons on the agent's turn have behaviour; the others are placeholders for your handlers.
Error
When the agent cannot answer, say what failed and what is safe, and offer a retry on the turn itself. Mark the sentence with role="alert" so it is announced.
AI SDK
A message from useChat has a role and a list of parts. Join the text parts for the reply and keep the other parts for steps, sources and tools. getMessageText in lib/ai.ts does the join:
export function getMessageText(message: Pick<UIMessage, "parts">) { return message.parts.reduce( (text, part) => (part.type === "text" ? text + part.text : text), "" )}const { messages, regenerate, status } = useChat()
{messages.map((message) => message.role === "user" ? ( <Message key={message.id} align="end"> <MessageContent> <Bubble align="end" variant="muted"> <BubbleContent>{getMessageText(message)}</BubbleContent> </Bubble> </MessageContent> </Message> ) : ( <Message key={message.id}> <MessageContent className="gap-2"> <Markdown>{getMessageText(message)}</Markdown> <MessageFooter className="px-0"> <MessageAction label="Retry" isDisabled={status === "streaming"} onPress={() => regenerate({ messageId: message.id })} > <RefreshCcwIcon /> </MessageAction> </MessageFooter> </MessageContent> </Message> ))}regenerate({ messageId }) asks for that reply again. Without a messageId it regenerates the last agent message. When status is error, useChat also returns an error you can show on the turn, and clearError() removes it.
Offline, createChat in lib/ai.ts streams scripted user and assistant messages through the same useChat, so the actions can be built before a route exists.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus to the next action button in the footer. |
| Enter | Presses the focused action. |
| Space | Presses the focused action. |
The composed components provide:
- Real buttons for the actions, with the focus ring and press behaviour of Button.
- A tooltip on hover and keyboard focus for each action.
You provide:
- A label on every action. The icon has no text, so
MessageActionrequireslabeland setsaria-label. aria-pressedon toggles such as the rating buttons.- A live announcement for the result of an action. The copy button changes its label to "Copied", but a screen reader does not reliably announce a changed label, so add a live region when the result matters.
role="alert"on an error sentence, and a retry that does not move focus away.
Guidelines
When to use
- For every turn in a conversation, in a Chat thread.
- Whenever the person needs to act on one reply: copy it, rate it, ask again.
When not to use
- For a notice that is not part of the conversation, such as a failed save. Use an Alert or a toast.
- For the agent's working steps. Use Trace, through Reasoning or Chain of Thought.
Content
- Write the agent's reply the way a colleague would: the answer first, then what changed.
- Show only the actions that work. A button that does nothing is worse than no button.
- Name actions with a verb or a clear noun: "Copy", "Retry", "Good response".
Human in the loop
A reply is a claim the person can check. Keep a rating and a retry on every agent turn, and keep what the agent used (see Sources) next to the answer, so the person can judge it before acting on it.
Keep the footer actions the same on every agent turn so people learn where they are.
Don’t hide a failed reply behind a spinner that never ends. Say what failed and offer a retry.
Composition
| Component | What it does here |
|---|---|
| Message | Lays out one turn: Message aligns it, MessageContent stacks its parts, MessageFooter holds the actions. |
| Bubble | Gives the person's text its muted bubble. |
| Button | The ghost icon buttons for the actions. |
| Tooltip | Shows each action's label on hover and focus. |
| Code Block | Renders fenced code inside the Markdown reply. |
The pattern exports one component, MessagePattern, which holds the two turns and the copy and rating state. MessageAction is internal to the file: lift it into your own module when you reuse it.