From 9232e27f7195a23bfccc12036ef72f11317d9e15 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 19 Feb 2025 12:06:01 +0100 Subject: [PATCH] Refactored the agent base in preparation for multithreading support --- app/MindWork AI Studio/Agents/AgentBase.cs | 20 ++++++++++--------- .../Agents/AgentDataSourceSelection.cs | 4 ++-- .../Agents/AgentRetrievalContextValidation.cs | 4 ++-- .../Agents/AgentTextContentCleaner.cs | 4 ++-- app/MindWork AI Studio/Agents/UserRequest.cs | 19 ++++++++++++++++++ 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 app/MindWork AI Studio/Agents/UserRequest.cs diff --git a/app/MindWork AI Studio/Agents/AgentBase.cs b/app/MindWork AI Studio/Agents/AgentBase.cs index ef4756f7..66f13146 100644 --- a/app/MindWork AI Studio/Agents/AgentBase.cs +++ b/app/MindWork AI Studio/Agents/AgentBase.cs @@ -32,8 +32,6 @@ public abstract class AgentBase(ILogger logger, SettingsManager setti protected ILogger Logger { get; init; } = logger; - protected IContent? lastUserPrompt; - /// /// Represents the type or category of this agent. /// @@ -80,10 +78,10 @@ public abstract class AgentBase(ILogger logger, SettingsManager setti Blocks = [], }; - protected DateTimeOffset AddUserRequest(ChatThread thread, string request) + protected UserRequest AddUserRequest(ChatThread thread, string request) { var time = DateTimeOffset.Now; - this.lastUserPrompt = new ContentText + var lastUserPrompt = new ContentText { Text = request, }; @@ -93,13 +91,17 @@ public abstract class AgentBase(ILogger logger, SettingsManager setti Time = time, ContentType = ContentType.TEXT, Role = ChatRole.USER, - Content = this.lastUserPrompt, + Content = lastUserPrompt, }); - - return time; + + return new() + { + Time = time, + UserPrompt = lastUserPrompt, + }; } - protected async Task AddAIResponseAsync(ChatThread thread, DateTimeOffset time) + protected async Task AddAIResponseAsync(ChatThread thread, IContent lastUserPrompt, DateTimeOffset time) { if(this.ProviderSettings is null) return; @@ -125,6 +127,6 @@ public abstract class AgentBase(ILogger logger, SettingsManager setti // Use the selected provider to get the AI response. // By awaiting this line, we wait for the entire // content to be streamed. - await aiText.CreateFromProviderAsync(providerSettings.CreateProvider(this.Logger), providerSettings.Model, this.lastUserPrompt, thread); + await aiText.CreateFromProviderAsync(providerSettings.CreateProvider(this.Logger), providerSettings.Model, lastUserPrompt, thread); } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs b/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs index 557c30be..6b1410ce 100644 --- a/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs +++ b/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs @@ -111,8 +111,8 @@ public sealed class AgentDataSourceSelection (ILogger return EMPTY_BLOCK; var thread = this.CreateChatThread(this.SystemPrompt(availableDataSources)); - var time = this.AddUserRequest(thread, text.Text); - await this.AddAIResponseAsync(thread, time); + var userRequest = this.AddUserRequest(thread, text.Text); + await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time); var answer = thread.Blocks[^1]; diff --git a/app/MindWork AI Studio/Agents/AgentRetrievalContextValidation.cs b/app/MindWork AI Studio/Agents/AgentRetrievalContextValidation.cs index a75dc201..7dd226f2 100644 --- a/app/MindWork AI Studio/Agents/AgentRetrievalContextValidation.cs +++ b/app/MindWork AI Studio/Agents/AgentRetrievalContextValidation.cs @@ -96,8 +96,8 @@ public sealed class AgentRetrievalContextValidation (ILogger logger, SettingsM return EMPTY_BLOCK; var thread = this.CreateChatThread(this.SystemPrompt(sourceURL)); - var time = this.AddUserRequest(thread, text.Text); - await this.AddAIResponseAsync(thread, time); + var userRequest = this.AddUserRequest(thread, text.Text); + await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time); var answer = thread.Blocks[^1]; this.answers.Add(answer); diff --git a/app/MindWork AI Studio/Agents/UserRequest.cs b/app/MindWork AI Studio/Agents/UserRequest.cs new file mode 100644 index 00000000..216eebe4 --- /dev/null +++ b/app/MindWork AI Studio/Agents/UserRequest.cs @@ -0,0 +1,19 @@ +using AIStudio.Chat; + +namespace AIStudio.Agents; + +/// +/// The created user request. +/// +public sealed class UserRequest +{ + /// + /// The time when the request was created. + /// + public required DateTimeOffset Time { get; init; } + + /// + /// The user prompt. + /// + public required IContent UserPrompt { get; init; } +} \ No newline at end of file