From 8060fc01ddd8771f4d505b0e3dd6bf262fd542d4 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Fri, 3 Jan 2025 21:58:31 +0100 Subject: [PATCH] Added button to edit last user message (#248) --- .../Chat/ContentBlockComponent.razor | 14 ++++++- .../Chat/ContentBlockComponent.razor.cs | 38 +++++++++++++++++++ .../Components/ChatComponent.razor | 14 ++++++- .../Components/ChatComponent.razor.cs | 38 +++++++++++++++++++ .../wwwroot/changelog/v0.9.24.md | 3 +- 5 files changed, 104 insertions(+), 3 deletions(-) diff --git a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor index 1754ca6..ca61acc 100644 --- a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor +++ b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor @@ -12,10 +12,22 @@ @this.Role.ToName() (@this.Time) + @if (this.IsSecondToLastBlock && this.Role is ChatRole.USER && this.EditLastUserBlockFunc is not null) + { + + + + } + @if (this.IsLastContentBlock && this.Role is ChatRole.USER && this.EditLastBlockFunc is not null) + { + + + + } @if (this.IsLastContentBlock && this.Role is ChatRole.AI && this.RegenerateFunc is not null) { - + } @if (this.RemoveBlockFunc is not null) diff --git a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs index a069e67..5797590 100644 --- a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs +++ b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor.cs @@ -43,6 +43,9 @@ public partial class ContentBlockComponent : ComponentBase [Parameter] public bool IsLastContentBlock { get; set; } = false; + + [Parameter] + public bool IsSecondToLastBlock { get; set; } = false; [Parameter] public Func? RemoveBlockFunc { get; set; } @@ -50,6 +53,12 @@ public partial class ContentBlockComponent : ComponentBase [Parameter] public Func? RegenerateFunc { get; set; } + [Parameter] + public Func? EditLastBlockFunc { get; set; } + + [Parameter] + public Func? EditLastUserBlockFunc { get; set; } + [Parameter] public Func RegenerateEnabled { get; set; } = () => false; @@ -170,4 +179,33 @@ public partial class ContentBlockComponent : ComponentBase if (regenerate.HasValue && regenerate.Value) await this.RegenerateFunc(this.Content); } + + private async Task EditLastBlock() + { + if (this.EditLastBlockFunc is null) + return; + + if(this.Role is not ChatRole.USER) + return; + + await this.EditLastBlockFunc(this.Content); + } + + private async Task EditLastUserBlock() + { + if (this.EditLastUserBlockFunc is null) + return; + + if(this.Role is not ChatRole.USER) + return; + + var edit = await this.DialogService.ShowMessageBox( + "Edit Message", + "Do you really want to edit this message? In order to edit this message, the AI response will be deleted.", + "Yes, remove the AI response and edit it", + "No, keep it"); + + if (edit.HasValue && edit.Value) + await this.EditLastUserBlockFunc(this.Content); + } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/ChatComponent.razor b/app/MindWork AI Studio/Components/ChatComponent.razor index 4e7753d..ccfe1e1 100644 --- a/app/MindWork AI Studio/Components/ChatComponent.razor +++ b/app/MindWork AI Studio/Components/ChatComponent.razor @@ -12,9 +12,21 @@ { var block = blocks[i]; var isLastBlock = i == blocks.Count - 1; + var isSecondLastBlock = i == blocks.Count - 2; @if (!block.HideFromUser) { - + } } } diff --git a/app/MindWork AI Studio/Components/ChatComponent.razor.cs b/app/MindWork AI Studio/Components/ChatComponent.razor.cs index 8cfae55..5017d01 100644 --- a/app/MindWork AI Studio/Components/ChatComponent.razor.cs +++ b/app/MindWork AI Studio/Components/ChatComponent.razor.cs @@ -595,6 +595,44 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable await this.SendMessage(reuseLastUserPrompt: true); } + private Task EditLastUserBlock(IContent block) + { + if(this.ChatThread is null) + return Task.CompletedTask; + + if (block is not ContentText textBlock) + return Task.CompletedTask; + + var lastBlock = this.ChatThread.Blocks.Last(); + var lastBlockContent = lastBlock.Content; + if(lastBlockContent is null) + return Task.CompletedTask; + + this.userInput = textBlock.Text; + this.ChatThread.Remove(block); + this.ChatThread.Remove(lastBlockContent); + this.hasUnsavedChanges = true; + this.StateHasChanged(); + + return Task.CompletedTask; + } + + private Task EditLastBlock(IContent block) + { + if(this.ChatThread is null) + return Task.CompletedTask; + + if (block is not ContentText textBlock) + return Task.CompletedTask; + + this.userInput = textBlock.Text; + this.ChatThread.Remove(block); + this.hasUnsavedChanges = true; + this.StateHasChanged(); + + return Task.CompletedTask; + } + #region Overrides of MSGComponentBase public override async Task ProcessIncomingMessage(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default diff --git a/app/MindWork AI Studio/wwwroot/changelog/v0.9.24.md b/app/MindWork AI Studio/wwwroot/changelog/v0.9.24.md index ce1786e..0c6775c 100644 --- a/app/MindWork AI Studio/wwwroot/changelog/v0.9.24.md +++ b/app/MindWork AI Studio/wwwroot/changelog/v0.9.24.md @@ -1,3 +1,4 @@ # v0.9.24, build 199 (2025-01-xx xx:xx UTC) - Added a button to remove a message from the chat thread. -- Added a button to regenerate the last AI response. \ No newline at end of file +- Added a button to regenerate the last AI response. +- Added a button to edit the last user message. \ No newline at end of file