ChatPalette
A full chat panel: messages that take the room that is left, and a prompt pinned under them. Put it in a modal, a slideover or any box with a height.
On this page
Examples
Streaming chat panel
Interactive: tokens arrive one by one and Stop cancels. Static rendering: the prompt posts and the page renders the whole answer.
Hi! Ask me how streaming works. Press Enter to send, Shift+Enter for a new line.
Razor
@* 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
KlyChatPalette
Complete chat panel: the messages fill the available height and the prompt stays pinned below them.
Parameters
| Name | Type | Description |
|---|---|---|
Class |
string |
Classes added to the root element, merged last so they win over the theme. |
Ui |
ChatPaletteUi |
Per-instance class overrides of the slots of the chat palette, see ChatPaletteUi . They are merged after the theme and the Class parameter wins on the root slot. |
Slots
| Name | Type | Description |
|---|---|---|
Prompt |
RenderFragment |
The message form, pinned to the bottom: a KlyChatPrompt . |
ChildContent |
RenderFragment |
The conversation: a KlyChatMessages . |
Ui slots
Ui takes a ChatPaletteUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyChatPalette , after the theme and the app configuration. |
Content |
Classes merged into the content slot of KlyChatPalette , after the theme and the app configuration. |
Prompt |
Classes merged into the prompt slot of KlyChatPalette , after the theme and the app configuration. |