mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 04:53: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
@@ -293,6 +293,9 @@ public static class AppIcons
|
||||
/// <summary>Marks the properties of a component in the plugin audit.</summary>
|
||||
public const string COMPONENT_PROPERTIES = LucideIcons.SLIDERS_HORIZONTAL;
|
||||
|
||||
/// <summary>Compresses a chat into a new chat which starts with a summary of it.</summary>
|
||||
public const string COMPRESS_CHAT = LucideIcons.FOLD_VERTICAL;
|
||||
|
||||
/// <summary>Confirms a draft or takes over a suggestion.</summary>
|
||||
public const string CONFIRM = LucideIcons.CHECK;
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
public static class KeyboardEventArgsExtensions
|
||||
{
|
||||
/// <param name="keyEvent">The key pressed.</param>
|
||||
extension(KeyboardEventArgs keyEvent)
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether the key is Enter, of the main keyboard or of the number pad, pressed without Alt, Ctrl, or the Command key.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Confirms a dialog whose text field takes a single line, e.g., a name. The key and its code are both
|
||||
/// asked: the key names what the keyboard layout produces, the code the key on the keyboard. A text
|
||||
/// field which takes several lines uses SendBehavior.IsSendShortcut instead, which follows the setting of
|
||||
/// the user.
|
||||
/// </remarks>
|
||||
public bool IsPlainEnter
|
||||
{
|
||||
get
|
||||
{
|
||||
var key = keyEvent.Key.ToLowerInvariant();
|
||||
var code = keyEvent.Code.ToLowerInvariant();
|
||||
if (key is not "enter" && code is not "enter" and not "numpadenter")
|
||||
return false;
|
||||
|
||||
return keyEvent is { AltKey: false, CtrlKey: false, MetaKey: false };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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