feat: add multi-turn conversation support within a session
Send full conversation history with each API request so the AI maintains context across exchanges. Add "New Chat" button to clear the conversation and start fresh. No persistent storage — session resets on page reload. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -75,8 +75,21 @@
|
||||
</div>
|
||||
|
||||
@* Input area: pinned at the bottom of the chat container.
|
||||
Disabled attribute prevents interaction while the assistant is streaming. *@
|
||||
Disabled attribute prevents interaction while the assistant is streaming.
|
||||
The "New Chat" button clears the conversation to start fresh. *@
|
||||
<div class="input-area">
|
||||
@if (_messages.Count > 0)
|
||||
{
|
||||
<div class="input-actions">
|
||||
<MudButton Variant="Variant.Text"
|
||||
Size="Size.Small"
|
||||
StartIcon="@Icons.Material.Filled.Add"
|
||||
OnClick="NewChat"
|
||||
Disabled="_isStreaming">
|
||||
New Chat
|
||||
</MudButton>
|
||||
</div>
|
||||
}
|
||||
<MudTextField @bind-Value="_userInput"
|
||||
Placeholder="@(_isStreaming ? "Waiting for response..." : "Type a message...")"
|
||||
Variant="Variant.Outlined"
|
||||
@@ -106,6 +119,15 @@
|
||||
// Used to disable input and show the thinking indicator.
|
||||
private bool _isStreaming = false;
|
||||
|
||||
/// <summary>
|
||||
/// Clears all messages to start a new conversation.
|
||||
/// </summary>
|
||||
private void NewChat()
|
||||
{
|
||||
_messages.Clear();
|
||||
_userInput = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the Enter key press to submit the message.
|
||||
/// </summary>
|
||||
@@ -154,14 +176,16 @@
|
||||
|
||||
try
|
||||
{
|
||||
// Build the request with the current user message.
|
||||
// Future phases will include full conversation history for multi-turn.
|
||||
// Build the request with the full conversation history for multi-turn context.
|
||||
// We include all messages except the last one (the empty assistant placeholder
|
||||
// that is waiting to be filled by the stream). This gives the AI the full
|
||||
// conversation so it can reference prior exchanges.
|
||||
var request = new ChatRequest
|
||||
{
|
||||
Messages = new List<ChatMessage>
|
||||
{
|
||||
new ChatMessage { Role = "user", Content = userText }
|
||||
}
|
||||
Messages = _messages
|
||||
.Where(m => !string.IsNullOrEmpty(m.Content))
|
||||
.Select(m => new ChatMessage { Role = m.Role, Content = m.Content, Timestamp = m.Timestamp })
|
||||
.ToList()
|
||||
};
|
||||
|
||||
// Stream tokens from the API. IAsyncEnumerable yields each text delta
|
||||
|
||||
@@ -77,3 +77,10 @@
|
||||
border-top: 1px solid var(--mud-palette-lines-default);
|
||||
background-color: var(--mud-palette-background);
|
||||
}
|
||||
|
||||
/* Action row above the text input (New Chat button) */
|
||||
.input-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user