From 5ff5a926b541070b8795bfedfdac4a145d5fd6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peer=20Sch=C3=BCtt?= <20603780+peerschuett@users.noreply.github.com> Date: Fri, 16 May 2025 14:51:24 +0200 Subject: [PATCH] Messages are now stored successfully --- app/MindWork AI Studio/Chat/ChatRoles.cs | 2 +- app/MindWork AI Studio/Chat/ContentBlock.cs | 4 +- .../Settings/SettingsPanelChatTemplates.razor | 8 +- .../SettingsPanelChatTemplates.razor.cs | 4 +- .../Dialogs/ChatTemplateDialog.razor | 52 +++++++----- .../Dialogs/ChatTemplateDialog.razor.cs | 83 +++++++++---------- .../Settings/ChatTemplate.cs | 40 +++------ 7 files changed, 89 insertions(+), 104 deletions(-) diff --git a/app/MindWork AI Studio/Chat/ChatRoles.cs b/app/MindWork AI Studio/Chat/ChatRoles.cs index 6bbed0e9..4c74aa14 100644 --- a/app/MindWork AI Studio/Chat/ChatRoles.cs +++ b/app/MindWork AI Studio/Chat/ChatRoles.cs @@ -4,7 +4,7 @@ public static class ChatRoles { public static IEnumerable ChatTemplateRoles() { - yield return ChatRole.SYSTEM; + yield return ChatRole.USER; yield return ChatRole.AI; } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Chat/ContentBlock.cs b/app/MindWork AI Studio/Chat/ContentBlock.cs index 632d98da..4ac347e9 100644 --- a/app/MindWork AI Studio/Chat/ContentBlock.cs +++ b/app/MindWork AI Studio/Chat/ContentBlock.cs @@ -18,12 +18,12 @@ public class ContentBlock /// /// The content of the block. /// - public IContent? Content { get; init; } + public IContent? Content { get; set; } /// /// The role of the content block in the chat thread, e.g., user, AI, etc. /// - public ChatRole Role { get; init; } = ChatRole.NONE; + public ChatRole Role { get; set; } = ChatRole.NONE; /// /// Should the content block be hidden from the user? diff --git a/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor b/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor index fe5ee59f..5cf040db 100644 --- a/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor +++ b/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor @@ -5,11 +5,11 @@ @T("Your Chat Templates") - @T("Store personal data about yourself in various profiles so that the AIs know your personal context. This saves you from having to explain your context each time, for example, in every chat. When you have different roles, you can create a profile for each role.") + @T("Explain chat Templates TODO") - - - @T("Are you a project manager in a research facility? You might want to create a profile for your project management activities, one for your scientific work, and a profile for when you need to write program code. In these profiles, you can record how much experience you have or which methods you like or dislike using. Later, you can choose when and where you want to use each profile.") + + + @T("Explain chat Templates TODO") diff --git a/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor.cs b/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor.cs index 8db5a18d..409111f7 100644 --- a/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor.cs +++ b/app/MindWork AI Studio/Components/Settings/SettingsPanelChatTemplates.razor.cs @@ -36,10 +36,10 @@ public partial class SettingsPanelChatTemplates : SettingsPanelBase { x => x.DataNum, chatTemplate.Num }, { x => x.DataId, chatTemplate.Id }, { x => x.DataName, chatTemplate.Name }, - { x => x.DataSystemPrompt, chatTemplate.NeedToKnow }, + { x => x.DataSystemPrompt, chatTemplate.SystemPrompt }, // { x => x.DataActions, chatTemplate.Actions }, { x => x.IsEditing, true }, - // {x => x.AdditionalMessages, chatTemplate}, TODO + {x => x.AdditionalMessages, chatTemplate.AdditionalMessages}, }; var dialogReference = await this.DialogService.ShowAsync(T("Edit Chat Template"), dialogParameters, DialogOptions.FULLSCREEN); diff --git a/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor b/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor index b629c1b5..7f2a728d 100644 --- a/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor +++ b/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor @@ -1,3 +1,5 @@ +@using AIStudio.Chat +@using MudBlazor.Extensions @inherits MSGComponentBase @inject ISnackbar Snackbar @@ -51,37 +53,41 @@ HelperText="@T("Tell the AI your system prompt.")" /> + - + What should you know about the additional messages? TODO + + Additional messages - - Start using messages + + + + + Role Entry - Actions + Actions - @context.Role - @context.Entry + @context.Role + @context.Content - - + + - + @foreach (var role in availableRoles) { @role @@ -89,27 +95,29 @@ - + + /> + /> - + - + + @T("Add additional message") + diff --git a/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor.cs b/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor.cs index d481f72b..b17cba59 100644 --- a/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor.cs +++ b/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor.cs @@ -42,7 +42,7 @@ public partial class ChatTemplateDialog : MSGComponentBase public bool IsEditing { get; init; } [Parameter] - public List AdditionalMessages { get; set; } = []; + public List AdditionalMessages { get; set; } = []; [Inject] private ILogger Logger { get; init; } = null!; @@ -58,10 +58,11 @@ public partial class ChatTemplateDialog : MSGComponentBase private string[] dataIssues = []; private string dataEditingPreviousName = string.Empty; - private EntryItem messageEntryBeforeEdit; - private readonly List additionalMessagesEntries = []; - private readonly List availableRoles = ["User", "Assistant"]; - private bool initialAddButtonDisabled = false; + private ContentBlock messageEntryBeforeEdit; + // private readonly List additionalMessagesEntries = []; + // private readonly List availableRoles = ["User", "Assistant"]; + private readonly IEnumerable availableRoles = ChatRoles.ChatTemplateRoles().ToArray(); + private bool allowProfile = true; // We get the form reference from Blazor code to validate it manually: private MudForm form = null!; @@ -72,83 +73,77 @@ public partial class ChatTemplateDialog : MSGComponentBase Id = this.DataId, Name = this.DataName, - NeedToKnow = this.DataSystemPrompt, - // AdditionalMessages = this.additionalMessagesEntries, - Actions = string.Empty, + SystemPrompt = this.DataSystemPrompt, + AdditionalMessages = this.AdditionalMessages, }; - private void RemoveMessage(EntryItem item) + private void RemoveMessage(ContentBlock item) { - this.additionalMessagesEntries.Remove(item); - this.Snackbar.Add("Entry removed", Severity.Info); - this.initialAddButtonDisabled = this.additionalMessagesEntries.Count > 0; - - // ChatRoles.ChatTemplateRoles() // TODO: -> darauf foreach für alle Rollen in der Tabelle + this.AdditionalMessages.Remove(item); + // this.Snackbar.Add("Entry removed", Severity.Info); } - private void AddInitialMessage() + private void AddNewMessageToEnd() { - var newEntry = new EntryItem + var newEntry = new ContentBlock { - Role = availableRoles[0], // Default to first role ("User") - Entry = "Your message" + Role = ChatRole.USER, // Default to User + Content = new ContentText(), + ContentType = ContentType.TEXT, + HideFromUser = true, + Time = DateTimeOffset.Now, }; - this.additionalMessagesEntries.Add(newEntry); - this.Snackbar.Add("Initial entry added", Severity.Success); - this.initialAddButtonDisabled = this.additionalMessagesEntries.Count > 0; + this.AdditionalMessages.Add(newEntry); + // this.Snackbar.Add("Initial entry added", Severity.Success); } - private void AddNewMessageBelow(EntryItem currentItem) + private void AddNewMessageBelow(ContentBlock currentItem) { // Create new entry with a valid role - var newEntry = new EntryItem + var newEntry = new ContentBlock { - Role = availableRoles.FirstOrDefault(role => role != currentItem.Role) ?? availableRoles[0], // Default to role not used in the previous entry - Entry = "Your message" + Role = ChatRole.USER, // Default to User + Content = new ContentText(), + ContentType = ContentType.TEXT, + HideFromUser = true, + Time = DateTimeOffset.Now, }; - + // Rest of the method remains the same - var index = this.additionalMessagesEntries.IndexOf(currentItem); + var index = this.AdditionalMessages.IndexOf(currentItem); if (index >= 0) { - this.additionalMessagesEntries.Insert(index + 1, newEntry); - this.Snackbar.Add("New entry added", Severity.Success); + this.AdditionalMessages.Insert(index + 1, newEntry); + // this.Snackbar.Add("New entry added", Severity.Success); } else { - this.additionalMessagesEntries.Add(newEntry); - this.Snackbar.Add("New entry added", Severity.Success); + this.AdditionalMessages.Add(newEntry); + // this.Snackbar.Add("New entry added", Severity.Success); } - this.initialAddButtonDisabled = this.additionalMessagesEntries.Count > 0; } private void BackupItem(object element) { - this.messageEntryBeforeEdit = new() + this.messageEntryBeforeEdit = new ContentBlock { - Role = ((EntryItem)element).Role, - Entry = ((EntryItem)element).Entry + Role = ((ContentBlock)element).Role, + Content = ((ContentBlock)element).Content, }; } private void ItemHasBeenCommitted(object element) { - this.Snackbar.Add("Changes saved", Severity.Success); + // this.Snackbar.Add("Changes saved", Severity.Success); } private void ResetItemToOriginalValues(object element) { - ((EntryItem)element).Role = this.messageEntryBeforeEdit.Role; - ((EntryItem)element).Entry = this.messageEntryBeforeEdit.Entry; - } - - public class EntryItem - { - public required string Role { get; set; } - public required string Entry { get; set; } + ((ContentBlock)element).Role = this.messageEntryBeforeEdit.Role; + ((ContentBlock)element).Content = this.messageEntryBeforeEdit.Content; } #region Overrides of ComponentBase diff --git a/app/MindWork AI Studio/Settings/ChatTemplate.cs b/app/MindWork AI Studio/Settings/ChatTemplate.cs index b53bf265..72dbf953 100644 --- a/app/MindWork AI Studio/Settings/ChatTemplate.cs +++ b/app/MindWork AI Studio/Settings/ChatTemplate.cs @@ -1,19 +1,19 @@ +using AIStudio.Chat; using AIStudio.Tools.PluginSystem; namespace AIStudio.Settings; -public readonly record struct ChatTemplate(uint Num, string Id, string Name, string NeedToKnow, string Actions) //, CategoryTypes.List AdditionalMessages) +public readonly record struct ChatTemplate(uint Num, string Id, string Name, string SystemPrompt, List AdditionalMessages) { - private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(Profile).Namespace, nameof(Profile)); + private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(ChatTemplate).Namespace, nameof(ChatTemplate)); - public static readonly Profile NO_PROFILE = new() + public static readonly ChatTemplate NO_CHATTEMPLATE = new() { - Name = TB("Use no profile"), - NeedToKnow = string.Empty, - Actions = string.Empty, + Name = TB("Use no chat template"), + SystemPrompt = string.Empty, Id = Guid.Empty.ToString(), Num = uint.MaxValue, - // AdditionalMessages = [], + AdditionalMessages = [], }; #region Overrides of ValueType @@ -31,34 +31,16 @@ public readonly record struct ChatTemplate(uint Num, string Id, string Name, str if(this.Num == uint.MaxValue) return string.Empty; - var needToKnow = + var systemPrompt = $""" - What should you know about the user? - ``` - {this.NeedToKnow} + {this.SystemPrompt} ``` """; - var actions = - $""" - The user wants you to consider the following things. - - ``` - {this.Actions} - ``` - """; - - if (string.IsNullOrWhiteSpace(this.NeedToKnow)) - return actions; - - if (string.IsNullOrWhiteSpace(this.Actions)) - return needToKnow; - return $""" - {needToKnow} - - {actions} + {systemPrompt} """; } + } \ No newline at end of file