Added option to start new chat

This commit is contained in:
Thorsten Sommer 2024-07-11 09:27:22 +02:00
parent 73b7d81903
commit 2adfdb1014
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5
2 changed files with 60 additions and 4 deletions

View File

@ -44,6 +44,10 @@
</MudTooltip> </MudTooltip>
} }
<MudTooltip Text="Start new chat" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Material.Filled.AddComment" OnClick="() => this.StartNewChat()"/>
</MudTooltip>
<MudTooltip Text="Move chat to workspace" Placement="Placement.Bottom"> <MudTooltip Text="Move chat to workspace" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Material.Filled.MoveToInbox" Disabled="@(!this.CanThreadBeSaved)"/> <MudIconButton Icon="@Icons.Material.Filled.MoveToInbox" Disabled="@(!this.CanThreadBeSaved)"/>
</MudTooltip> </MudTooltip>

View File

@ -1,17 +1,20 @@
using AIStudio.Chat; using AIStudio.Chat;
using AIStudio.Components.Blocks; using AIStudio.Components.Blocks;
using AIStudio.Components.CommonDialogs;
using AIStudio.Provider; using AIStudio.Provider;
using AIStudio.Settings; using AIStudio.Settings;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using DialogOptions = AIStudio.Components.CommonDialogs.DialogOptions;
namespace AIStudio.Components.Pages; namespace AIStudio.Components.Pages;
/// <summary> /// <summary>
/// The chat page. /// The chat page.
/// </summary> /// </summary>
public partial class Chat : ComponentBase public partial class Chat : ComponentBase, IAsyncDisposable
{ {
[Inject] [Inject]
private SettingsManager SettingsManager { get; set; } = null!; private SettingsManager SettingsManager { get; set; } = null!;
@ -22,14 +25,18 @@ public partial class Chat : ComponentBase
[Inject] [Inject]
public Random RNG { get; set; } = null!; public Random RNG { get; set; } = null!;
[Inject]
public IDialogService DialogService { get; set; } = null!;
private static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new(); private static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
private AIStudio.Settings.Provider selectedProvider; private AIStudio.Settings.Provider selectedProvider;
private ChatThread? chatThread; private ChatThread? chatThread;
private bool hasUnsavedChanges;
private bool isStreaming; private bool isStreaming;
private string userInput = string.Empty; private string userInput = string.Empty;
private bool workspacesVisible; private bool workspacesVisible;
private Workspaces? workspaces = null; private Workspaces? workspaces;
// Unfortunately, we need the input field reference to clear it after sending a message. // Unfortunately, we need the input field reference to clear it after sending a message.
// This is necessary because we have to handle the key events ourselves. Otherwise, // This is necessary because we have to handle the key events ourselves. Otherwise,
@ -90,7 +97,11 @@ public partial class Chat : ComponentBase
// Save the chat: // Save the chat:
if (this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY) if (this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY)
{
await this.SaveThread(); await this.SaveThread();
this.hasUnsavedChanges = false;
this.StateHasChanged();
}
// //
// Add the AI response to the thread: // Add the AI response to the thread:
@ -116,6 +127,7 @@ public partial class Chat : ComponentBase
// Enable the stream state for the chat component: // Enable the stream state for the chat component:
this.isStreaming = true; this.isStreaming = true;
this.hasUnsavedChanges = true;
this.StateHasChanged(); this.StateHasChanged();
// Use the selected provider to get the AI response. // Use the selected provider to get the AI response.
@ -125,7 +137,10 @@ public partial class Chat : ComponentBase
// Save the chat: // Save the chat:
if (this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY) if (this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY)
{
await this.SaveThread(); await this.SaveThread();
this.hasUnsavedChanges = false;
}
// Disable the stream state: // Disable the stream state:
this.isStreaming = false; this.isStreaming = false;
@ -134,6 +149,7 @@ public partial class Chat : ComponentBase
private async Task InputKeyEvent(KeyboardEventArgs keyEvent) private async Task InputKeyEvent(KeyboardEventArgs keyEvent)
{ {
this.hasUnsavedChanges = true;
var key = keyEvent.Code.ToLowerInvariant(); var key = keyEvent.Code.ToLowerInvariant();
// Was the enter key (either enter or numpad enter) pressed? // Was the enter key (either enter or numpad enter) pressed?
@ -174,6 +190,7 @@ public partial class Chat : ComponentBase
return; return;
await this.workspaces.StoreChat(this.chatThread); await this.workspaces.StoreChat(this.chatThread);
this.hasUnsavedChanges = false;
} }
private string ExtractThreadName(string firstUserInput) private string ExtractThreadName(string firstUserInput)
@ -188,4 +205,39 @@ public partial class Chat : ComponentBase
return threadName; return threadName;
} }
private async Task StartNewChat()
{
if (this.hasUnsavedChanges)
{
var dialogParameters = new DialogParameters
{
{ "Message", "Are you sure you want to start a new chat? All unsaved changes will be lost." },
};
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Delete Chat", dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
}
this.chatThread = null;
this.isStreaming = false;
this.hasUnsavedChanges = false;
this.userInput = string.Empty;
await this.inputField.Clear();
}
#region Implementation of IAsyncDisposable
public async ValueTask DisposeAsync()
{
if(this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY)
{
await this.SaveThread();
this.hasUnsavedChanges = false;
}
}
#endregion
} }