AI Elements

Message

Use Message for one turn in a thread. The text of the person is a Bubble on a Message with align="end". The reply of the agent is markdown on a Message, with actions under it. A thread of these turns is a Chat.

Installation#

Message is built with the Message, Bubble, and Tooltip components.

npx shadcn@latest add @formance/message @formance/bubble @formance/tooltip

Usage#

import { Bubble, BubbleContent } from '@/components/ui/bubble';import { Button } from '@/components/ui/button';import {  Message,  MessageContent,  MessageFooter,} from '@/components/ui/message';
<Message align="end">  <MessageContent>    <Bubble align="end" variant="muted">      <BubbleContent>How do I accrue daily interest?</BubbleContent>    </Bubble>  </MessageContent></Message><Message>  <MessageContent className="gap-2">    <p>Add a clients:$id:interest account.</p>    <MessageFooter>      <Button variant="ghost" size="icon-sm" aria-label="Copy">        <CopyIcon />      </Button>    </MessageFooter>  </MessageContent></Message>

Actions#

Put the actions in MessageFooter. The footer follows the alignment of the message, so on the turn of the person the actions are on the right. Use a ghost icon button with a tooltip for each action.

  • Copy writes the reply to the clipboard. Its icon changes to a check for two seconds.
  • Good response and Bad response are toggles. Set aria-pressed so that screen readers announce the state.
  • Retry runs the turn again.
  • Edit on the turn of the person puts the text back in the field.

Markdown#

The reply streams as markdown. Render it with the Markdown fragment, in a ghost bubble. Fenced code then shows with the Formance code theme. Keep the messages of the person as plain text.

AI SDK#

Branch on the role of the message. The turn of the person is the bubble. The turn of the agent is the reply. regenerate with the message id runs the turn again.

const { messages, regenerate } = useChat();
if (message.role === 'user') {  return (    <Message align="end">      <MessageContent>        <Bubble align="end" variant="muted">          <BubbleContent>{text}</BubbleContent>        </Bubble>      </MessageContent>    </Message>  );}
return (  <Message>    <MessageContent className="gap-2">      <Markdown>{text}</Markdown>      <MessageFooter>        <Button          variant="ghost"          size="icon-sm"          aria-label="Retry"          onClick={() => regenerate({ messageId: message.id })}        >          <RefreshCcwIcon />        </Button>      </MessageFooter>    </MessageContent>  </Message>);