mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-11 22:13:48 +00:00
Copy entire chats & Rollback chats to certain message (#992)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
1 parent
c4ba83f0be
commit
e9ebc02a96
14 files changed
+733
-8
No files matched your search
@@ -26,6 +26,8 @@
|
||||
IsSecondToLastBlock="@isSecondLastBlock"
|
||||
RegenerateFunc="@this.RegenerateBlock"
|
||||
RegenerateEnabled="@(() => this.IsProviderSelected && this.ChatThread.IsLLMProviderAllowed(this.Provider))"
|
||||
RollbackFunc="@this.RollbackBlock"
|
||||
RollbackEnabled="@(() => !this.IsCurrentChatStreaming)"
|
||||
EditLastBlockFunc="@this.EditLastBlock"
|
||||
EditLastUserBlockFunc="@this.EditLastUserBlock"/>
|
||||
}
|
||||
@@ -101,6 +103,10 @@
|
||||
<MudTooltip Text="@T("Move the chat to a workspace, or to another if it is already in one.")" Placement="@TOOLBAR_TOOLTIP_PLACEMENT">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.MoveToInbox" Disabled="@(!this.CanThreadBeSaved || this.IsCurrentChatStreaming)" OnClick="@this.MoveChatToWorkspace"/>
|
||||
</MudTooltip>
|
||||
|
||||
<MudTooltip Text="@T("Copy this chat & continue in the copy.")" Placement="@TOOLBAR_TOOLTIP_PLACEMENT">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy" Disabled="@(!this.CanThreadBeCopied)" OnClick="@this.CopyCurrentChat"/>
|
||||
</MudTooltip>
|
||||
}
|
||||
|
||||
<AttachDocuments Name="File Attachments" DocumentPaths="@this.ComposerState.FileAttachments" DocumentPathsChanged="@this.ComposerAttachmentsChanged" CatchAllDocuments="true" UseSmallForm="true" ShowMediaStatus="false" Provider="@this.Provider" OwnerChat="@this.ChatThread" EnsureOwnerChatAsync="@this.EnsureMediaImportChatAsync" Disabled="@this.MediaTranscriptionService.IsBusy(this.CurrentMediaImportOwner)"/>
|
||||
|
||||
@@ -649,7 +649,9 @@ public partial class ChatComponent : MSGComponentBase
|
||||
}
|
||||
|
||||
private bool CanThreadBeSaved => this.ChatThread is not null && this.ChatThread.Blocks.Any(b => !b.HideFromUser);
|
||||
|
||||
|
||||
private bool CanThreadBeCopied => this.CanThreadBeSaved && !this.IsCurrentChatStreaming && !this.MediaTranscriptionService.IsBusy(this.CurrentMediaImportOwner);
|
||||
|
||||
private string TooltipAddChatToWorkspace => string.Format(T("Start new chat in workspace '{0}'"), this.currentWorkspaceName);
|
||||
|
||||
private string UserInputStyle => this.SettingsManager.ConfigurationData.Confidence.ShowProviderConfidence ? this.Provider.UsedLLMProvider.GetConfidence(this.SettingsManager).SetColorStyle(this.SettingsManager) : string.Empty;
|
||||
@@ -1344,7 +1346,48 @@ public partial class ChatComponent : MSGComponentBase
|
||||
|
||||
await this.SyncWorkspaceHeaderWithChatThreadAsync();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Copies the open chat and continues in the copy.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Copied is the chat as it stands on the screen, not the state which was stored last. That is
|
||||
/// why nothing is asked about unsaved changes here, unlike everywhere else a chat is left
|
||||
/// behind: none of them are lost, they move into the copy, while the original keeps what was
|
||||
/// stored.<br/><br/>
|
||||
///
|
||||
/// The same holds for the message being written. The copy is marked as loaded right away,
|
||||
/// because the parameter update which follows would otherwise take it for another chat being
|
||||
/// opened and clear the composer. Only the transcripts attached to that message are exchanged:
|
||||
/// those of the original live in its directory, and the copy got copies of its own.
|
||||
/// </remarks>
|
||||
private async Task CopyCurrentChat()
|
||||
{
|
||||
if (this.ChatThread is null || !this.CanThreadBeCopied)
|
||||
return;
|
||||
|
||||
var sourceChat = this.ChatThread;
|
||||
var copy = this.Workspaces is null
|
||||
? await WorkspaceBehaviour.CopyChatAsync(this.DialogService, sourceChat)
|
||||
: await this.Workspaces.CopyChatAsync(sourceChat);
|
||||
|
||||
if (copy is null)
|
||||
return;
|
||||
|
||||
var transcriptsOfTheOriginal = sourceChat.PendingMediaTranscripts.Select(transcript => transcript.FilePath).ToHashSet(StringComparer.Ordinal);
|
||||
this.ComposerState.FileAttachments.RemoveWhere(attachment => transcriptsOfTheOriginal.Contains(attachment.FilePath));
|
||||
foreach (var transcript in copy.PendingMediaTranscripts)
|
||||
this.ComposerState.FileAttachments.Add(transcript);
|
||||
|
||||
this.ChatThread = copy;
|
||||
this.hasUnsavedChanges = false;
|
||||
|
||||
await this.SyncForegroundChatAsync();
|
||||
this.MarkCurrentChatAsLoadedParameter();
|
||||
await this.SyncWorkspaceHeaderWithChatThreadAsync();
|
||||
await this.ChatThreadChanged.InvokeAsync(this.ChatThread);
|
||||
}
|
||||
|
||||
private async Task LoadedChatChanged(bool notifyParent = true)
|
||||
{
|
||||
this.hasUnsavedChanges = false;
|
||||
@@ -1457,6 +1500,24 @@ public partial class ChatComponent : MSGComponentBase
|
||||
|
||||
await this.SendMessage(reuseLastUserPrompt: true);
|
||||
}
|
||||
|
||||
private async Task RollbackBlock(IContent aiBlock)
|
||||
{
|
||||
if (this.ChatThread is null || this.IsCurrentChatStreaming)
|
||||
return;
|
||||
|
||||
// Which parts of the thread a rollback resets and which it keeps is documented at RollBackTo:
|
||||
if (!this.ChatThread.RollBackTo(aiBlock))
|
||||
return;
|
||||
|
||||
// The rollback reset the AI-selected data sources, which the data source selection still shows:
|
||||
this.dataSourceSelectionComponent?.ChangeOptionWithoutSaving(this.ChatThread.DataSourceOptions, this.ChatThread.AISelectedDataSources);
|
||||
this.hasUnsavedChanges = true;
|
||||
await this.SaveThread();
|
||||
this.tokenTracker?.Nudge();
|
||||
this.StateHasChanged();
|
||||
await this.inputField.FocusAsync();
|
||||
}
|
||||
|
||||
private Task EditLastUserBlock(IContent block)
|
||||
{
|
||||
|
||||
@@ -81,6 +81,10 @@ else
|
||||
<MudIconButton Icon="@Icons.Material.Filled.MoveToInbox" Size="Size.Medium" Color="Color.Inherit" Disabled="@this.IsChatTreeItemBusy(treeItem)" OnClick="@(() => this.MoveChatAsync(treeItem.Path))"/>
|
||||
</MudTooltip>
|
||||
|
||||
<MudTooltip Text="@T("Copy chat")" Placement="@WORKSPACE_ITEM_TOOLTIP_PLACEMENT">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy" Size="Size.Medium" Color="Color.Inherit" Disabled="@this.IsChatTreeItemBusy(treeItem)" OnClick="@(() => this.CopyChatFromTreeAsync(treeItem.Path))"/>
|
||||
</MudTooltip>
|
||||
|
||||
<MudTooltip Text="@T("Rename")" Placement="@WORKSPACE_ITEM_TOOLTIP_PLACEMENT">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Medium" Color="Color.Inherit" Disabled="@this.IsChatTreeItemBusy(treeItem)" OnClick="@(() => this.RenameChatAsync(treeItem.Path))"/>
|
||||
</MudTooltip>
|
||||
|
||||
@@ -660,6 +660,24 @@ public partial class Workspaces : MSGComponentBase
|
||||
await this.LoadTreeItemsAsync(startPrefetch: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the given chat, after asking the user for the name of the copy, and shows the copy in the tree.
|
||||
/// </summary>
|
||||
/// <param name="sourceChat">The chat to copy, as it stands in memory.</param>
|
||||
/// <returns>The persisted copy. Null when the user canceled the question, in which case nothing was copied.</returns>
|
||||
/// <remarks>
|
||||
/// Neither asks about unsaved changes nor opens the copy: which of both a caller needs depends
|
||||
/// on where the copy was asked for.
|
||||
/// </remarks>
|
||||
public async Task<ChatThread?> CopyChatAsync(ChatThread sourceChat)
|
||||
{
|
||||
var copy = await WorkspaceBehaviour.CopyChatAsync(this.DialogService, sourceChat);
|
||||
if (copy is not null)
|
||||
await this.LoadTreeItemsAsync(startPrefetch: false);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
private async Task<ChatThread?> LoadChatAsync(string? chatPath, bool switchToChat)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(chatPath))
|
||||
@@ -781,6 +799,62 @@ public partial class Workspaces : MSGComponentBase
|
||||
await this.LoadTreeItemsAsync(startPrefetch: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the chat behind the copy button of a tree item and opens the copy.
|
||||
/// </summary>
|
||||
/// <param name="chatPath">The directory of the chat to copy.</param>
|
||||
/// <remarks>
|
||||
/// The copy itself is done by CopyChatAsync, which the chat toolbar uses as well. What this
|
||||
/// handler adds is what only the tree needs: it finds the chat by its directory, and because it
|
||||
/// opens the copy afterward, it asks first when the chat open right now has unsaved changes.
|
||||
/// The chat toolbar asks nothing, since it copies the chat on the screen and keeps working in it.
|
||||
/// </remarks>
|
||||
private async Task CopyChatFromTreeAsync(string? chatPath)
|
||||
{
|
||||
var chat = await this.LoadChatAsync(chatPath, false);
|
||||
if (chat is null)
|
||||
return;
|
||||
|
||||
var mediaOwner = MediaImportOwner.ForChat(chat.ChatId);
|
||||
if (this.AIJobService.IsChatGenerationActive(chat.ChatId) || this.MediaTranscriptionService.IsBusy(mediaOwner))
|
||||
return;
|
||||
|
||||
//
|
||||
// Copying the chat which is open right now takes its in-memory state, so whatever the user
|
||||
// has not saved yet ends up in the copy while the original keeps the state it was stored
|
||||
// with. Copying any other chat replaces the open one, so its unsaved changes are gone.
|
||||
// Both outcomes are surprising enough to deserve their own wording.
|
||||
//
|
||||
var openChat = this.CurrentChatThread;
|
||||
var isCopyOfOpenChat = openChat is not null && openChat.ChatId == chat.ChatId;
|
||||
if (await MessageBus.INSTANCE.SendMessageUseFirstResult<bool, bool>(this, Event.HAS_CHAT_UNSAVED_CHANGES))
|
||||
{
|
||||
var dialogParameters = new DialogParameters<ConfirmDialog>
|
||||
{
|
||||
{
|
||||
x => x.Message, isCopyOfOpenChat switch
|
||||
{
|
||||
true => T("Do you want to copy this chat? Your unsaved changes move into the copy, and the original chat keeps the state it was last saved with."),
|
||||
false => T("Do you want to copy this chat? The copy is opened afterwards, so all unsaved changes of the chat you have open right now will be lost."),
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>(T("Copy Chat"), dialogParameters, DialogOptions.FULLSCREEN);
|
||||
var dialogResult = await dialogReference.Result;
|
||||
if (dialogResult is null || dialogResult.Canceled)
|
||||
return;
|
||||
}
|
||||
|
||||
var sourceChat = isCopyOfOpenChat ? openChat! : chat;
|
||||
var copy = await this.CopyChatAsync(sourceChat);
|
||||
if (copy is null)
|
||||
return;
|
||||
|
||||
this.CurrentChatThread = copy;
|
||||
await this.CurrentChatThreadChanged.InvokeAsync(this.CurrentChatThread);
|
||||
}
|
||||
|
||||
private async Task RenameWorkspaceAsync(string? workspacePath)
|
||||
{
|
||||
if (workspacePath is null)
|
||||
|
||||
Reference in new issue
Block a user