mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 05:33:48 +00:00
Added context compression (#1198)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
1 parent
28b88580a9
commit
8a4a26503b
54 files changed
+4582
-56
No files matched your search
@@ -987,18 +987,37 @@ public static partial class WorkspaceBehaviour
|
||||
return await CopyChatAsync(sourceChat, (dialogResult.Data as string)!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a chat in memory, as it would be stored and read back, so that the copy shares nothing with the original.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Keeps the ID and the name; only what is stored with a chat comes along, as when it is loaded.
|
||||
/// Files stay where they are: the copy refers to the same ones.
|
||||
/// </remarks>
|
||||
/// <param name="chat">The chat to copy. It stays untouched.</param>
|
||||
/// <returns>The copy.</returns>
|
||||
internal static ChatThread CloneChat(ChatThread chat)
|
||||
{
|
||||
var serializedChat = JsonSerializer.Serialize(chat, JSON_OPTIONS);
|
||||
return JsonSerializer.Deserialize<ChatThread>(serializedChat, JSON_OPTIONS) ?? throw new InvalidOperationException("The chat could not be copied.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a chat into a new chat of the same workspace, including its managed transcript files.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A caller which wants the copy to differ from the original, such as compressing a chat, see
|
||||
/// ChatCompression.TurnIntoContinuation, changes it before anything of it is written: only the
|
||||
/// transcripts the changed copy still refers to are copied, and the copy reaches the disk only
|
||||
/// once, as it is meant to be. When the change fails, nothing of the copy stays behind.
|
||||
/// </remarks>
|
||||
/// <param name="sourceChat">The chat to copy. Its own files and state stay untouched.</param>
|
||||
/// <param name="name">The name of the copy.</param>
|
||||
/// <param name="reshape">Changes the copy before it is stored, or null to keep it as the original is.</param>
|
||||
/// <returns>The persisted copy.</returns>
|
||||
internal static async Task<ChatThread> CopyChatAsync(ChatThread sourceChat, string name)
|
||||
internal static async Task<ChatThread> CopyChatAsync(ChatThread sourceChat, string name, Action<ChatThread>? reshape = null)
|
||||
{
|
||||
var serializedChat = JsonSerializer.Serialize(sourceChat, JSON_OPTIONS);
|
||||
var copiedChat = JsonSerializer.Deserialize<ChatThread>(serializedChat, JSON_OPTIONS)
|
||||
?? throw new InvalidOperationException("The chat could not be copied.");
|
||||
copiedChat = copiedChat with
|
||||
var copiedChat = CloneChat(sourceChat) with
|
||||
{
|
||||
ChatId = Guid.NewGuid(),
|
||||
Name = name,
|
||||
@@ -1007,6 +1026,7 @@ public static partial class WorkspaceBehaviour
|
||||
var targetDirectory = GetChatDirectory(copiedChat.WorkspaceId, copiedChat.ChatId);
|
||||
try
|
||||
{
|
||||
reshape?.Invoke(copiedChat);
|
||||
CopyManagedTranscriptAttachments(copiedChat, targetDirectory);
|
||||
|
||||
//
|
||||
|
||||
Reference in new issue
Block a user