Chat
Since 0.1.0-alpha.22A conversation: its entries in a log that scrolls, and a box to write the next message in. For an assistant, or for people talking.
An assistant
Put a ChatEntry for each message in a Chat, oldest first, and set Mine on yours. OnSend gets what was typed, or the suggestion chosen; add it, then the reply.
Static
Preview
How can we help?
Ask about an order, a refund or shipping.
Code
@* A help desk assistant. Its answers are canned here; an app streams them from a model.
A static page streams them too, with [StreamRendering], when the post came with script; it
keeps the conversation under an id in the address the form posts to, as a chat app keeps it
under /chat/{id}. *@
System.Collections.Concurrent
NavigationManager Navigation
[StreamRendering]
<Chat Label="Help desk assistant" FormName="help-desk" FormAction="_action" OnSend="SendAsync" OnStop="Stop" Status="_status"
AllowFiles Accept="image/*,.pdf" MaxFileSize="5_000_000"
Suggestions="(_turns.Count == 0 ? Ideas : null)" Placeholder="Ask about an order, a refund or shipping">
<EmptyContent>
<EmptyState Icon="Lucide.MessagesSquare" Title="How can we help?" Description="Ask about an order, a refund or shipping." />
</EmptyContent>
<ChildContent>
foreach (var turn in _turns)
{
if (turn.Mine)
{
<ChatEntry Mine Text="turn.Text" Attachments="turn.Files" Time="turn.Time" />
}
else
{
<ChatEntry Author="Assistant" Initials="IB" Markdown="turn.Text" Time="turn.Time" Pending="turn.Pending" />
}
}
</ChildContent>
</Chat>Static: The form posts without reloading the page. With `[StreamRendering]` on the page, the reply streams as it does interactively: `OnSend`'s `CanStream` is true when the post came with script. There's no Stop: the reply streams in a request a new post can't reach. Keep the conversation yourself between posts, under an id: this example sets `FormAction` to an address with the conversation's id, so each post goes there.
Add this example to a page with the izzy CLI:
izzy add component Chat --at <file>:<line>An assistant's reply is usually Markdown. Set Markdown on its entry, and it's shown as RichText shows it: sanitized, with code highlighted.
While the reply is on its way, set Status to Waiting, then Streaming, and set the entry's Pending. Send becomes Stop (OnStop), and the entry shows dots and then a caret.
Suggestions shows messages to send with one press. EmptyContent fills the log before the first entry.
AllowFiles adds a button to attach files, limited by Accept, MaxFileSize and MaxFiles. The files come to OnSend as Files, checked on the server against those limits. To show an entry's own files, set its Attachments.
Topics
- Streaming from a model: Stream a model's reply into the last entry, with Stop and Retry.
A button of your own
To add a button of your own beside Send, put it in ComposerActions. Here a microphone dictates with the browser's speech recognition: @bind-Draft fills the box with what it hears, and Send sends it as typed text.
Always Interactive
Preview
You said
Book the ferry to Naxos for the 14th.
Code
@* A microphone of your own in the box, dictating into the message with the browser's speech
recognition (wwwroot/examples/dictation.js, a few lines). Chrome, Edge and Safari have it;
elsewhere the button doesn't show. *@
Microsoft.JSInterop
IJSRuntime JS
IAsyncDisposable
<Chat Label="Trip notes" FormName="trip-notes" OnSend="Send" @bind-Draft="_draft" Height="Length.Rem(20)"
Variant="ChatVariant.Conversation" Placeholder="Dictate or type a note">
<ComposerActions>
if (_supported)
{
<IconButton Icon="(_listening ? Lucide.MicOff : Lucide.Mic)" Label="(_listening ? "Stop dictating" : "Dictate")"
Variant="ButtonVariant.Ghost" Accent="Accent.Neutral" Size="ControlSize.Sm" OnClick="ToggleAsync" />
}
</ComposerActions>
<ChildContent>
foreach (var note in _notes)
{
<ChatEntry Mine Text="note" Initials="IB" />
}
</ChildContent>
</Chat>Chrome, Edge and Safari have speech recognition, and Chrome and Edge send the audio to their speech services. Where it's missing, the button doesn't show.
For another service, such as Azure Speech, send the audio there and set Draft from the result.
People talking
For a conversation between people, set Variant to Conversation. Set Mine on your entries and Author on everyone else's.
Static
Preview
Dana Whitfield said
Hi, my order #4821 arrived without the charger.
Dana Whitfield said
It's the 65 W one that comes with the laptop.
You said
Sorry about that, Dana. I've sent a charger by express; it should arrive tomorrow.
Dana Whitfield said
It came today, thanks!
You said
Great to hear.
You said
Is there anything else I can help with?
Code
@* A support thread with a customer, as the agent sees it: everyone's messages in bubbles, yours
at the end with your team's initials, theirs with their name and initials, the day above each
day's first message, and messages in a row grouped. On a static page the thread is kept under
an id in the address the form posts to, as an app keeps it under /threads/{id}. *@
System.Collections.Concurrent
NavigationManager Navigation
<Chat Label="Conversation with Dana Whitfield" FormName="support-thread" FormAction="_action" OnSend="Send"
Variant="ChatVariant.Conversation" Height="Length.Rem(30)" Placeholder="Reply to Dana">
foreach (var message in _messages)
{
<ChatEntry Mine="message.Mine" Author="(message.Mine ? null : "Dana Whitfield")" Text="message.Text"
Initials="(message.Mine ? "IB" : null)" Time="message.Time" Copyable="false" />
}
</Chat>Everyone's messages sit in bubbles, yours at the end. Another person's name sits above their message, with an Avatar: a picture's URL, or initials from the name. Yours shows an avatar when you give it a picture or Initials.
Messages in a row from one person, minutes apart, are grouped, with the name, avatar and time shown once. With Time, each entry shows its time, and the day sits above the first message of each day: "Yesterday", "Today", or the date.
While someone is typing, an entry with their Author and Pending shows three dots. Height sets the chat's height, and the log scrolls inside it.
Details
Variant chooses how others' entries look: across the log for an assistant, or in bubbles for people talking. Entries in a row from the same person, minutes apart, are grouped: their name, avatar and time show once.
The chat is 32rem tall, its log scrolling inside; set Height, or Grow to fill its parent. The app keeps the entries and makes the replies: OnSend gets what was typed, and the app adds it and the reply as ChatEntry items. Status says where the reply is: while it's Waiting or Streaming, Send becomes Stop (OnStop) on an interactive page, and goes on a static one. Suggestions are buttons above the box that send their text. The log starts at its newest entry and stays there as entries arrive, with no script.
On a static page the box is a form that posts (FormName names it, as with Form), so a message sends with JavaScript off too; with script, Enter sends and Shift+Enter adds a line. A reply streams on a static page too, with [StreamRendering] on the page, when the post came with script: OnSend's CanStream says so (with JavaScript off, send the reply whole).
AllowFiles adds a button to attach files (Accept, MaxFileSize, MaxFiles); they come to OnSend with the text, checked against those limits, and post with the form on a static page. Each pick replaces the files chosen before.
Entries are added at the end: a day separator and a static Retry go by the order entries arrived in, so inserting earlier ones (loading older messages) isn't supported yet.
Parts
Written inside Chat. The API page lists their parameters.
ChatEntry: one message in a Chat: yours (Mine), or someone else's, with their name and avatar.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| A log that starts at its newest entry and stays there as entries arrive | Yes | Yes |
| Entries as text, Markdown or HTML, sanitized; avatars, times and days | Yes | Yes |
| Sends in a plain form post, with suggestions and Stop | Yes | Yes |
| Replies that stream in | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, a message posts with the Send button, and Enter adds a line.
Accessibility
- The log is a
lognamed byLabel, which is required. It's quiet (aria-live off): a reply isn't read word by word as it arrives. A status, polite, says "Replying", then "Reply complete", "Stopped" or "Reply failed". - In a conversation between people, an entry from someone else that arrives on an interactive page is announced with its author ("Dana: Sounds good").
- Each entry is an article with a heading screen readers can jump between ("You said", "Dana said"), hidden on screen;
HeadingLevelsets its level (3 by default). - The box is a text area named "Message". Send and Stop are labeled buttons; the suggestions are a group named "Suggestions".
- The log can be scrolled with the keyboard: it's in the Tab order.
- Attach is a label for the file input, named "Attach files"; the files chosen are a list, each with a Remove button on an interactive page. A file over the limits is named in an error under the box, announced as it appears.
Keyboard navigation
In the message box
| Press this key | To do this |
|---|---|
| Enter | Send the message |
| Shift+Enter | Add a line |
Parameters
| Prop | Type | Default |
|---|---|---|
LabelNames the conversation's log, such as "Assistant" or "Messages with Dana". | string | Required |
ChildContentThe entries: ChatEntry items, oldest first. | RenderFragment? | |
EmptyContentShown in the log before the first entry, such as a greeting. | RenderFragment? | |
StatusWhere the reply is. | ChatStatus | ChatStatus.Ready |
SuggestionsMessages a person can send with one press, shown above the box while nothing is on its way. | IReadOnlyList<string>? | |
PlaceholderThe prompt in the empty box, such as "Ask about your order". | string? | |
MaxLengthThe most characters a message can have. | int? | |
FormNameThe form's name on a static page, unique on the page, as a Form's FormName. | string | "chat" |
FormActionThe address the form posts to on a static page; by default, the page's own. | string? | |
@bind-DraftThe message being written, to fill the box or clear it from code (with @bind-Draft, it follows what's typed on an interactive page). | string? | |
ComposerActionsButtons of your own in the box's row of tools, just before Send, such as a microphone that dictates into Draft. | RenderFragment? | |
AllowFilesAdds a button to attach files to a message: they come to OnSend as its Files. | bool | false |
AcceptThe file types that can be attached, as a file input's accept: .pdf,image/*. | string? | |
MaxFileSizeThe largest file that can be attached, in bytes. | long | 10000000 |
MaxFilesThe most files a message can have. | int | 5 |
VariantHow others' entries look: across the log (Assistant, the default) or in bubbles (Conversation, for people talking). | ChatVariant | ChatVariant.Assistant |
HeadingLevelThe level of each entry's hidden heading, 2 to 6. | int | 3 |
OnSendCalled with what a person sent: the message typed, or a suggestion chosen. | EventCallback<ChatSendArgs> | |
OnStopCalled when a person chooses Stop, shown while Status is Waiting or Streaming. | EventCallback |
Plus 27 surface props, 17 item props and 4 common props.
Full APISee also
- ChatEntry: one message in a Chat: yours (
Mine), or someone else's, with their name and avatar. - RichText: displays HTML from an editor or a CMS safely in the page.
- TextArea: lets the user enter several lines of text, and can grow and count them.