AI Message
NewOne turn in an AI chat: the person's bubble, or the assistant's answer with copy, regenerate, ratings and version arrows.
AssistantFast
Use Intl.RelativeTimeFormat. It's built in, so there's nothing to install:
rtf.format(-3, "day")gives "3 days ago"- Pass
numeric: "auto"to get "yesterday" instead of "1 day ago"
Installation
$ pnpm dlx shadcn@latest add @synccn/ai-messageFirst time in this project? Connect synccn with one command.
Usage
import { AIMessage } from "@/components/ui/ai-message";<AIMessage from="assistant" model="Smart" copyText={text} onRegenerate={retry} onFeedback={rate}>…</AIMessage>API reference
Generated from the component's TypeScript types, so it always matches the code.
AIMessage
One turn in an AI conversation: the person's message as a bubble, or the assistant's answer with copy, regenerate, thumbs up/down and version arrows underneath.
- fromrequired
"user" | "assistant"Who wrote it. The person's messages are bubbles on the right; the assistant's are full width.
- name
stringDefault
"Assistant"Name shown above assistant messages.
- model
stringModel name shown after the assistant's name.
- avatar
React.ReactNodeReplaces the assistant's sparkle mark.
- copyText
stringPlain text for the copy button. Without it, there's no copy button.
- onRegenerate
() => voidShows a regenerate button.
- feedback
"up" | "down" | nullCurrent rating, when controlled.
- onFeedback
(value: "up" | "down" | null) => voidShows thumbs up and down. Called with the new rating, or null when it's taken back.
- branch
AIMessageBranchArrows to flip between regenerated versions.
- streaming
booleanDefault
falseHides the actions while the answer is still being written.
Dependencies
Read from the component's imports when the site builds. The CLI installs all of these for you.
Helpers
- lib/clipboardcopyText (copies to the clipboard)
- lib/utilscn (merges class names)
npm packages
- clsx for cn()
- tailwind-merge for cn()
- lucide-react for Check, ChevronLeft, ChevronRight, Copy, RefreshCw, Sparkles, ThumbsDown, ThumbsUp
Used by
Accessibility
- Ratings are toggle buttons with aria-pressed; the version counter is a polite live region.
- Actions stay hidden while streaming, so screen readers aren't offered half-finished answers to copy.
From the source
Read from this component's code when the site builds, so it can't drift.
- ARIA attributes: aria-label, aria-live, aria-pressed
- Announces changes to screen readers