ChatMessages
The log of a conversation. It follows new messages while you are at the bottom, stops when you scroll up and offers a button to jump back, shows a typing indicator while an answer is pending, and announces a finished answer once instead of token by token.
Examples
Status and scrolling
Add messages and change the status. Scroll up in the list, add a message, then use Jump to latest.
Question 1: how does the list behave?
Answer 1: it keeps the latest message in view while you are at the bottom.
Question 2: how does the list behave?
Answer 2: it keeps the latest message in view while you are at the bottom.
Question 3: how does the list behave?
Answer 3: it keeps the latest message in view while you are at the bottom.
Question 4: how does the list behave?
Answer 4: it keeps the latest message in view while you are at the bottom.
Question 5: how does the list behave?
Answer 5: it keeps the latest message in view while you are at the bottom.
Question 6: how does the list behave?
Answer 6: it keeps the latest message in view while you are at the bottom.
@* The list follows new messages while you are at the bottom. Scroll up and it stops; "Jump to latest" brings you back.
The buttons need interactivity, since they change the list from .NET. *@
<div class="flex w-full max-w-xl flex-col gap-3">
<div class="flex h-72 flex-col overflow-hidden rounded-xl border border-(--klyroo-border)">
<KlyChatMessages Status="_status" data-testid="chat-messages-demo">
@foreach (var message in _messages)
{
<KlyChatMessage @key="message.Id" Role="message.Role" Text="@message.Text" />
}
</KlyChatMessages>
</div>
<div class="flex flex-wrap items-center gap-2">
<KlyButton Label="Add a message" Size="KlySize.Sm" OnClick="AddUser" data-testid="chat-messages-add" />
<KlyButton Label="Add a reply" Size="KlySize.Sm" Variant="ButtonVariant.Outline" OnClick="AddAssistant" data-testid="chat-messages-reply" />
@foreach (var status in Enum.GetValues<ChatStatus>())
{
var s = status;
<KlyButton Label="@s.ToString()" Size="KlySize.Sm" Variant="ButtonVariant.Soft" Color="KlyColor.Neutral" OnClick="() => _status = s"
data-testid="@($"chat-messages-status-{s.ToString().ToLowerInvariant()}")" />
}
</div>
</div>
@code {
private sealed record Item(int Id, ChatRole Role, string Text);
private readonly List<Item> _messages = [.. Enumerable.Range(1, 12).Select(i => new Item(i, i % 2 == 1 ? ChatRole.User : ChatRole.Assistant, i % 2 == 1 ? $"Question {i / 2 + 1}: how does the list behave?" : $"Answer {i / 2}: it keeps the latest message in view while you are at the bottom."))];
private ChatStatus _status;
private int _next = 13;
private void AddUser() => _messages.Add(new Item(_next++, ChatRole.User, $"New question {_next}"));
private void AddAssistant() => _messages.Add(new Item(_next++, ChatRole.Assistant, $"New answer {_next}"));
}Streaming a reply
A chat panel streaming an IAsyncEnumerable of tokens, with a stop button.
Hi! Ask me how streaming works. Press Enter to send, Shift+Enter for a new line.
@* A chat panel that streams its answer: an IAsyncEnumerable of tokens with a delay stands in for the model.
Interactive: tokens appear as they arrive and the stop button cancels the stream.
Static rendering: the prompt posts, the page waits for the whole answer and renders it. *@
@implements IDisposable
<div class="h-96 w-full max-w-xl overflow-hidden rounded-xl border border-(--klyroo-border)" data-testid="chat-palette-demo">
<KlyChatPalette>
<ChildContent>
<KlyChatMessages Status="_status" data-testid="chat-palette-messages">
@foreach (var message in _messages)
{
<KlyChatMessage @key="message.Id" Role="message.Role" Compact Streaming="message.Streaming" Text="@message.Text"
AvatarText="@(message.Role == ChatRole.Assistant ? "B" : null)" AvatarAlt="@(message.Role == ChatRole.Assistant ? "Klyroo assistant" : null)" />
}
</KlyChatMessages>
</ChildContent>
<Prompt>
<KlyChatPrompt FormName="chat-palette" Status="_status" OnSubmit="Send" OnStop="Stop" OnReload="Retry" Variant="FieldVariant.Subtle"
Placeholder="Ask something..." Label="Message to the assistant" data-testid="chat-palette-prompt" />
</Prompt>
</KlyChatPalette>
</div>
@code {
private sealed class Item(int id, ChatRole role, string text, bool streaming = false)
{
public int Id { get; } = id;
public ChatRole Role { get; } = role;
public string Text { get; set; } = text;
public bool Streaming { get; set; } = streaming;
}
private readonly List<Item> _messages = [new(1, ChatRole.Assistant, "Hi! Ask me how streaming works. Press Enter to send, Shift+Enter for a new line.")];
private ChatStatus _status;
private CancellationTokenSource? _cts;
private string? _lastPrompt;
private int _next = 2;
private async Task Send(string text)
{
_lastPrompt = text;
_messages.Add(new Item(_next++, ChatRole.User, text));
await Answer(text);
}
private async Task Retry()
{
if (_lastPrompt is not null) await Answer(_lastPrompt);
}
private void Stop() => _cts?.Cancel();
private async Task Answer(string prompt)
{
_cts?.Dispose();
_cts = new CancellationTokenSource();
var token = _cts.Token;
var reply = new Item(_next++, ChatRole.Assistant, "", streaming: true);
_messages.Add(reply);
_status = ChatStatus.Submitted;
Refresh();
try
{
await Task.Delay(400, token);
_status = ChatStatus.Streaming;
Refresh();
await foreach (var chunk in ChatDemo.Tokens(ChatDemo.Reply(prompt), 50, token))
{
reply.Text += chunk;
Refresh();
}
_status = ChatStatus.Idle;
}
catch (OperationCanceledException)
{
// Stopped by the user: what arrived stays.
_status = ChatStatus.Idle;
}
finally
{
reply.Streaming = false;
}
}
// Static rendering renders once, after the handler: nothing to refresh before then.
private void Refresh()
{
if (RendererInfo.IsInteractive) StateHasChanged();
}
public void Dispose() => _cts?.Dispose();
}API
KlyChatMessages
Scrolling list of chat messages that keeps the latest one in view, announces new messages to assistive technology and offers a "jump to latest" button.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
AutoScroll |
bool |
true |
Follows the latest message while the user is at the bottom, and shows the "jump to latest" button once they scroll up. Without it the list never scrolls by itself. |
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Label |
string |
Accessible name of the log. Defaults to the built-in text. | |
Status |
ChatStatus |
ChatStatus. |
Where the conversation is. Submitted and Streaming show the typing indicator, Error shows Error . Update it from .NET as the request progresses. Values: Idle, Submitted, Streaming, Error. |
Ui |
ChatMessagesUi |
Per-instance class overrides of the slots of the chat messages, see ChatMessagesUi . They are merged after the theme and the Class parameter wins on the root slot. |
Slots
| Name | Type | Description |
|---|---|---|
Error |
RenderFragment |
Replaces the message shown when Status is Error. |
Indicator |
RenderFragment |
Replaces the typing dots. |
ChildContent |
RenderFragment |
The messages: KlyChatMessage items, in order. |
Ui slots
Ui takes a ChatMessagesUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyChatMessages , after the theme and the app configuration. |
Viewport |
Classes merged into the viewport slot of KlyChatMessages , after the theme and the app configuration. |
Content |
Classes merged into the content slot of KlyChatMessages , after the theme and the app configuration. |
Indicator |
Classes merged into the indicator slot of KlyChatMessages , after the theme and the app configuration. |
Dot |
Classes merged into the dot slot of KlyChatMessages , after the theme and the app configuration. |
Error |
Classes merged into the error slot of KlyChatMessages , after the theme and the app configuration. |
Jump |
Classes merged into the jump slot of KlyChatMessages , after the theme and the app configuration. |
JumpIcon |
Classes merged into the jumpIcon slot of KlyChatMessages , after the theme and the app configuration. |
Status |
Classes merged into the status slot of KlyChatMessages , after the theme and the app configuration. |