Added option to start new chat in the same workspace

This commit is contained in:
Thorsten Sommer 2024-07-12 20:50:47 +02:00
parent 51c4deed8b
commit 82cda5ee66
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 55 additions and 9 deletions

View File

@ -50,13 +50,23 @@
</MudTooltip> </MudTooltip>
} }
<MudTooltip Text="Start new chat" Placement="@TOOLBAR_TOOLTIP_PLACEMENT"> <MudTooltip Text="Start temporary chat" Placement="@TOOLBAR_TOOLTIP_PLACEMENT">
<MudIconButton Icon="@Icons.Material.Filled.AddComment" OnClick="() => this.StartNewChat()"/> <MudIconButton Icon="@Icons.Material.Filled.AddComment" OnClick="() => this.StartNewChat(useSameWorkspace: false)"/>
</MudTooltip> </MudTooltip>
<MudTooltip Text="Move chat to workspace" Placement="@TOOLBAR_TOOLTIP_PLACEMENT"> @if (!string.IsNullOrWhiteSpace(this.currentWorkspaceName))
<MudIconButton Icon="@Icons.Material.Filled.MoveToInbox" Disabled="@(!this.CanThreadBeSaved)" OnClick="() => this.MoveChatToWorkspace()"/> {
</MudTooltip> <MudTooltip Text="@this.TooltipAddChatToWorkspace" Placement="@TOOLBAR_TOOLTIP_PLACEMENT">
<MudIconButton Icon="@Icons.Material.Filled.CommentBank" OnClick="() => this.StartNewChat(useSameWorkspace: true)"/>
</MudTooltip>
}
@if (this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is not WorkspaceStorageBehavior.DISABLE_WORKSPACES)
{
<MudTooltip Text="Move chat to (another) workspace" Placement="@TOOLBAR_TOOLTIP_PLACEMENT">
<MudIconButton Icon="@Icons.Material.Filled.MoveToInbox" Disabled="@(!this.CanThreadBeSaved)" OnClick="() => this.MoveChatToWorkspace()"/>
</MudTooltip>
}
</MudToolBar> </MudToolBar>
</MudPaper> </MudPaper>
</FooterContent> </FooterContent>

View File

@ -37,6 +37,7 @@ public partial class Chat : ComponentBase, IAsyncDisposable
private bool isStreaming; private bool isStreaming;
private string userInput = string.Empty; private string userInput = string.Empty;
private string currentWorkspaceName = string.Empty; private string currentWorkspaceName = string.Empty;
private Guid currentWorkspaceId = Guid.Empty;
private bool workspacesVisible; private bool workspacesVisible;
private Workspaces? workspaces; private Workspaces? workspaces;
@ -65,6 +66,8 @@ public partial class Chat : ComponentBase, IAsyncDisposable
private bool CanThreadBeSaved => this.chatThread is not null && this.chatThread.Blocks.Count > 0; private bool CanThreadBeSaved => this.chatThread is not null && this.chatThread.Blocks.Count > 0;
private string TooltipAddChatToWorkspace => $"Start new chat in workspace \"{this.currentWorkspaceName}\"";
private async Task SendMessage() private async Task SendMessage()
{ {
if (!this.IsProviderSelected) if (!this.IsProviderSelected)
@ -77,7 +80,7 @@ public partial class Chat : ComponentBase, IAsyncDisposable
{ {
this.chatThread = new() this.chatThread = new()
{ {
WorkspaceId = Guid.Empty, WorkspaceId = this.currentWorkspaceId,
ChatId = Guid.NewGuid(), ChatId = Guid.NewGuid(),
Name = threadName, Name = threadName,
Seed = this.RNG.Next(), Seed = this.RNG.Next(),
@ -218,7 +221,7 @@ public partial class Chat : ComponentBase, IAsyncDisposable
return threadName; return threadName;
} }
private async Task StartNewChat() private async Task StartNewChat(bool useSameWorkspace = false)
{ {
if (this.hasUnsavedChanges) if (this.hasUnsavedChanges)
{ {
@ -233,11 +236,29 @@ public partial class Chat : ComponentBase, IAsyncDisposable
return; return;
} }
this.chatThread = null;
this.isStreaming = false; this.isStreaming = false;
this.hasUnsavedChanges = false; this.hasUnsavedChanges = false;
this.userInput = string.Empty; this.userInput = string.Empty;
this.currentWorkspaceName = string.Empty;
if (!useSameWorkspace)
{
this.chatThread = null;
this.currentWorkspaceId = Guid.Empty;
this.currentWorkspaceName = string.Empty;
}
else
{
this.chatThread = new()
{
WorkspaceId = this.currentWorkspaceId,
ChatId = Guid.NewGuid(),
Name = string.Empty,
Seed = this.RNG.Next(),
SystemPrompt = "You are a helpful assistant!",
Blocks = [],
};
}
await this.inputField.Clear(); await this.inputField.Clear();
} }
@ -249,6 +270,19 @@ public partial class Chat : ComponentBase, IAsyncDisposable
if(this.workspaces is null) if(this.workspaces is null)
return; return;
if (this.hasUnsavedChanges)
{
var confirmationDialogParameters = new DialogParameters
{
{ "Message", "Are you sure you want to move this chat? All unsaved changes will be lost." },
};
var confirmationDialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Unsaved Changes", confirmationDialogParameters, DialogOptions.FULLSCREEN);
var confirmationDialogResult = await confirmationDialogReference.Result;
if (confirmationDialogResult.Canceled)
return;
}
var dialogParameters = new DialogParameters var dialogParameters = new DialogParameters
{ {
{ "Message", "Please select the workspace where you want to move the chat to." }, { "Message", "Please select the workspace where you want to move the chat to." },
@ -280,6 +314,7 @@ public partial class Chat : ComponentBase, IAsyncDisposable
this.chatThread!.WorkspaceId = workspaceId; this.chatThread!.WorkspaceId = workspaceId;
await this.SaveThread(); await this.SaveThread();
this.currentWorkspaceId = this.chatThread.WorkspaceId;
this.currentWorkspaceName = await this.workspaces.LoadWorkspaceName(this.chatThread.WorkspaceId); this.currentWorkspaceName = await this.workspaces.LoadWorkspaceName(this.chatThread.WorkspaceId);
} }
@ -291,6 +326,7 @@ public partial class Chat : ComponentBase, IAsyncDisposable
this.isStreaming = false; this.isStreaming = false;
this.hasUnsavedChanges = false; this.hasUnsavedChanges = false;
this.userInput = string.Empty; this.userInput = string.Empty;
this.currentWorkspaceId = this.chatThread?.WorkspaceId ?? Guid.Empty;
this.currentWorkspaceName = this.chatThread is null ? string.Empty : await this.workspaces.LoadWorkspaceName(this.chatThread.WorkspaceId); this.currentWorkspaceName = this.chatThread is null ? string.Empty : await this.workspaces.LoadWorkspaceName(this.chatThread.WorkspaceId);
await this.inputField.Clear(); await this.inputField.Clear();