Command Palette

Search for a command to run...

Message

shadcn/ui

Turn container supporting message branching, copy controls, actions, and role alignment.

Message
You
How should we model agent tool states so transitions stay type-safe?
Optics Assistant

Model tool execution as a discriminated union keyed by status so every branch carries only its valid payload.

Pair the union with an exhaustive switch in your UI renderer to catch missing states at compile time.

1 of 2

import { ThumbsDownIcon, ThumbsUpIcon } from "lucide-react";import { useState } from "react";import {	AIMessage,	AIMessageAction,	AIMessageActions,	AIMessageBranch,	AIMessageBranchNext,	AIMessageBranchPage,	AIMessageBranchPrevious,	AIMessageContent,	AIMessageCopyButton,} from "@/components/optics/ai-elements/message";interface ResponseDraft {	id: string;	paragraphs: string[];	plainText: string;}const RESPONSE_DRAFTS: ResponseDraft[] = [	{		id: "draft-union",		paragraphs: [			"Model tool execution as a discriminated union keyed by status so every branch carries only its valid payload.",			"Pair the union with an exhaustive switch in your UI renderer to catch missing states at compile time.",		],		plainText:			"Model tool execution as a discriminated union keyed by status so every branch carries only its valid payload. Pair the union with an exhaustive switch in your UI renderer to catch missing states at compile time.",	},	{		id: "draft-reducer",		paragraphs: [			"Keep tool transitions inside a pure reducer that maps each streaming event directly to the next state variant.",			"This avoids scattered boolean flags in JSX and makes retry and approval transitions trivial to unit test.",		],		plainText:			"Keep tool transitions inside a pure reducer that maps each streaming event directly to the next state variant. This avoids scattered boolean flags in JSX and makes retry and approval transitions trivial to unit test.",	},];type FeedbackChoice = "up" | "down";function MessageDemo() {	const [branchIndex, setBranchIndex] = useState(0);	const [feedbackByDraft, setFeedbackByDraft] = useState<		Record<number, FeedbackChoice | undefined>	>({});	const activeDraft = RESPONSE_DRAFTS[branchIndex] ?? RESPONSE_DRAFTS[0];	const currentFeedback = feedbackByDraft[branchIndex];	function handleFeedback(choice: FeedbackChoice) {		setFeedbackByDraft((prev) => ({			...prev,			[branchIndex]: prev[branchIndex] === choice ? undefined : choice,		}));	}	return (		<div className="flex w-full max-w-lg flex-col gap-5">			<AIMessage from="user">				<AIMessageContent header="You">					How should we model agent tool states so transitions stay type-safe?				</AIMessageContent>			</AIMessage>			<AIMessage from="assistant">				<AIMessageContent header="Optics Assistant">					<AIMessageBranch branch={branchIndex} onBranchChange={setBranchIndex}>						{RESPONSE_DRAFTS.map((draft) => (							<div key={draft.id} className="space-y-2 text-sm leading-relaxed">								{draft.paragraphs.map((paragraph) => (									<p key={paragraph}>{paragraph}</p>								))}							</div>						))}						<AIMessageBranchPrevious />						<AIMessageBranchPage />						<AIMessageBranchNext />						<AIMessageActions className="ml-auto">							<AIMessageCopyButton value={activeDraft.plainText} />							<AIMessageAction								tooltip={currentFeedback === "up" ? "Marked helpful" : "Helpful"}								label="Thumbs up"								aria-pressed={currentFeedback === "up"}								className={									currentFeedback === "up" ? "text-emerald-600 dark:text-emerald-400" : undefined								}								onClick={() => handleFeedback("up")}							>								<ThumbsUpIcon />							</AIMessageAction>							<AIMessageAction								tooltip={currentFeedback === "down" ? "Marked not helpful" : "Not helpful"}								label="Thumbs down"								aria-pressed={currentFeedback === "down"}								className={currentFeedback === "down" ? "text-destructive" : undefined}								onClick={() => handleFeedback("down")}							>								<ThumbsDownIcon />							</AIMessageAction>						</AIMessageActions>					</AIMessageBranch>				</AIMessageContent>			</AIMessage>		</div>	);}<MessageDemo />

Installation

pnpm dlx shadcn@latest add @optics/ai-message

Props

<AIMessage />
Name
Type
align
"end" | "start"

<AIMessageContent />
Name
Type
header
React.ReactNode
bubbleVariant
"outline" | "default" | "secondary"(default: muted)
<AIMessageAction />
Name
Type
tooltip
string
label
string
variant
"link" | "outline" | "default"(default: ghost)
render
React.ReactElement
nativeButton
boolean
focusableWhenDisabled
boolean
animation
"none" | "all" | "colors" | "only-scale"
<AIMessageBranch />
Name
Type
defaultBranch
number(default: 0)
branch
number
onBranchChange
((branchIndex: number) => void)
<AIMessageBranchPrevious />
Name
Type
variant
"link" | "outline" | "default"(default: ghost)
render
React.ReactElement
nativeButton
boolean
focusableWhenDisabled
boolean
animation
"none" | "all" | "colors" | "only-scale"
<AIMessageBranchNext />
Name
Type
variant
"link" | "outline" | "default"(default: ghost)
render
React.ReactElement
nativeButton
boolean
focusableWhenDisabled
boolean
animation
"none" | "all" | "colors" | "only-scale"
<AIMessageCopyButton />
Name
Type
timeout
number(default: 2000)
onCopy
(() => void)
onError
((error: Error) => void)
tooltip
string
label
string
render
React.ReactElement
nativeButton
boolean
animation
"none" | "all" | "colors" | "only-scale"
variant
"link" | "outline" | "default"
focusableWhenDisabled
boolean