From 8048524a31ffb9c01251777ffa5cc29da8c4bfba Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 20 Feb 2025 11:26:49 -0600 Subject: [PATCH 1/2] refine state --- .../Conversations/IConversationStateService.cs | 2 +- .../Conversations/Services/ConversationStateService.cs | 10 +++++----- .../Controllers/ConversationController.cs | 5 +++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index df1239222..a5563cf30 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -6,7 +6,7 @@ namespace BotSharp.Abstraction.Conversations; /// /// Conversation state service to track the context in the conversation lifecycle /// -public interface IConversationStateService +public interface IConversationStateService : IDisposable { string GetConversationId(); Dictionary Load(string conversationId, bool isReadOnly = false); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 531bf06d1..4d229866f 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -22,11 +22,12 @@ namespace BotSharp.Core.Conversations.Services; /// /// Maintain the conversation state /// -public class ConversationStateService : IConversationStateService, IDisposable +public class ConversationStateService : IConversationStateService { private readonly ILogger _logger; private readonly IServiceProvider _services; private readonly IBotSharpRepository _db; + private readonly IConversationSideCar? _sidecar; private string _conversationId; /// /// States in the current round of conversation @@ -47,6 +48,7 @@ public ConversationStateService( _logger = logger; _curStates = new ConversationState(); _historyStates = new ConversationState(); + _sidecar = services.GetService(); } public string GetConversationId() => _conversationId; @@ -139,9 +141,8 @@ public Dictionary Load(string conversationId, bool isReadOnly = _conversationId = !isReadOnly ? conversationId : null; Reset(); - var sidecar = _services.GetService(); var endNodes = new Dictionary(); - if (sidecar?.IsEnabled() == true) + if (_sidecar?.IsEnabled() == true) { return endNodes; } @@ -217,8 +218,7 @@ public Dictionary Load(string conversationId, bool isReadOnly = public void Save() { - var sidecar = _services.GetService(); - if (_conversationId == null || sidecar?.IsEnabled() == true) + if (_conversationId == null || _sidecar?.IsEnabled() == true) { Reset(); return; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index f41c69fea..75dc11133 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -79,8 +79,9 @@ public async Task> GetConversations([FromBody] [HttpGet("/conversation/{conversationId}/dialogs")] public async Task> GetDialogs([FromRoute] string conversationId) { - var storage = _services.GetRequiredService(); - var history = storage.GetDialogs(conversationId); + var conv = _services.GetRequiredService(); + conv.SetConversationId(conversationId, [], isReadOnly: true); + var history = conv.GetDialogHistory(fromBreakpoint: false); var userService = _services.GetRequiredService(); var agentService = _services.GetRequiredService(); From 816abe9310f27de62ece46f4de6b300b215f87f8 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 20 Feb 2025 11:59:59 -0600 Subject: [PATCH 2/2] clean execution log --- .../Repositories/IBotSharpRepository.cs | 7 ----- .../Evaluations/ExecutionLogger.cs | 13 ++++---- .../Repository/BotSharpDbContext.cs | 12 -------- .../FileRepository/FileRepository.Log.cs | 30 ------------------- .../FileRepository/FileRepository.cs | 3 -- .../Hooks/CommonContentGeneratingHook.cs | 3 +- .../Collections/ExecutionLogDocument.cs | 7 ----- .../MongoDbContext.cs | 3 -- .../MongoRepository.Conversation.cs | 8 ++--- .../Repository/MongoRepository.Log.cs | 26 ---------------- 10 files changed, 10 insertions(+), 102 deletions(-) delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExecutionLogDocument.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 06582d457..235c449b4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -152,13 +152,6 @@ List GetConversationStateSearchKeys(int messageLowerLimit = 2, int convU => throw new NotImplementedException(); #endregion - #region Execution Log - void AddExecutionLogs(string conversationId, List logs) - => throw new NotImplementedException(); - List GetExecutionLogs(string conversationId) - => throw new NotImplementedException(); - #endregion - #region LLM Completion Log void SaveLlmCompletionLog(LlmCompletionLog log) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs b/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs index f70cb59f1..c00ae0989 100644 --- a/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs +++ b/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs @@ -1,26 +1,25 @@ using BotSharp.Abstraction.Evaluations; -using BotSharp.Abstraction.Repositories; using System.Text.RegularExpressions; namespace BotSharp.Core.Evaluations; public class ExecutionLogger : IExecutionLogger { - private readonly BotSharpDatabaseSettings _dbSettings; private readonly IServiceProvider _services; + private readonly ILogger _logger; + public ExecutionLogger( - BotSharpDatabaseSettings dbSettings, - IServiceProvider services) + IServiceProvider services, + ILogger logger) { - _dbSettings = dbSettings; _services = services; + _logger = logger; } public void Append(string conversationId, string content) { content = content.Replace("\r\n", " ").Replace("\n", " "); content = Regex.Replace(content, @"\s+", " "); - var db = _services.GetRequiredService(); - db.AddExecutionLogs(conversationId, new List { content }); + _logger.LogInformation($"Execution Log: {content}"); } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 100bbf169..587de372d 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -135,18 +135,6 @@ public List TruncateConversation(string conversationId, string messageId => throw new NotImplementedException(); #endregion - #region Execution Log - public void AddExecutionLogs(string conversationId, List logs) - { - throw new NotImplementedException(); - } - - public List GetExecutionLogs(string conversationId) - { - throw new NotImplementedException(); - } - #endregion - #region LLM Completion Log public void SaveLlmCompletionLog(LlmCompletionLog log) { diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs index 088988641..f82cb6faa 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs @@ -1,40 +1,10 @@ using BotSharp.Abstraction.Loggers.Models; -using Serilog; using System.IO; namespace BotSharp.Core.Repository { public partial class FileRepository { - #region Execution Log - public void AddExecutionLogs(string conversationId, List logs) - { - if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return; - - var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - - var file = Path.Combine(dir, EXECUTION_LOG_FILE); - File.AppendAllLines(file, logs); - } - - public List GetExecutionLogs(string conversationId) - { - var logs = new List(); - if (string.IsNullOrEmpty(conversationId)) return logs; - - var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId); - if (!Directory.Exists(dir)) return logs; - - var file = Path.Combine(dir, EXECUTION_LOG_FILE); - logs = File.ReadAllLines(file)?.ToList() ?? new List(); - return logs; - } - #endregion - #region LLM Completion Log public void SaveLlmCompletionLog(LlmCompletionLog log) { diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs index a5a2ffef1..78e90e4bc 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs @@ -50,9 +50,6 @@ public partial class FileRepository : IBotSharpRepository private const string KNOWLEDGE_DOC_FOLDER = "document"; private const string KNOWLEDGE_DOC_META_FILE = "meta.json"; - private const string EXECUTION_LOG_FILE = "execution.log"; - private const string PLUGIN_CONFIG_FILE = "config.json"; - private const string STATS_FOLDER = "stats"; private const string STATS_FILE = "stats.json"; diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs index 68d83c87b..d0c9c6e1e 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs @@ -29,7 +29,8 @@ private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel token MessageId = message.MessageId, AgentId = message.CurrentAgentId, Prompt = tokenStats.Prompt, - Response = message.Content + Response = message.Content, + CreateDateTime = DateTime.UtcNow }; db.SaveLlmCompletionLog(completionLog); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExecutionLogDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExecutionLogDocument.cs deleted file mode 100644 index 6b9611412..000000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExecutionLogDocument.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace BotSharp.Plugin.MongoStorage.Collections; - -public class ExecutionLogDocument : MongoBase -{ - public string ConversationId { get; set; } = default!; - public List Logs { get; set; } = []; -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 4d7f76a46..4aa82b2e6 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -154,9 +154,6 @@ public IMongoCollection ConversationDialogs public IMongoCollection ConversationStates => CreateConversationStateIndex(); - public IMongoCollection ExectionLogs - => GetCollectionOrCreate("ExecutionLogs"); - public IMongoCollection LlmCompletionLogs => GetCollectionOrCreate("LlmCompletionLogs"); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index 7d45e9729..8b6dded61 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -56,13 +56,11 @@ public bool DeleteConversations(IEnumerable conversationIds) var filterConv = Builders.Filter.In(x => x.Id, conversationIds); var filterDialog = Builders.Filter.In(x => x.ConversationId, conversationIds); var filterSates = Builders.Filter.In(x => x.ConversationId, conversationIds); - var filterExeLog = Builders.Filter.In(x => x.ConversationId, conversationIds); var filterPromptLog = Builders.Filter.In(x => x.ConversationId, conversationIds); var filterContentLog = Builders.Filter.In(x => x.ConversationId, conversationIds); var filterStateLog = Builders.Filter.In(x => x.ConversationId, conversationIds); var conbTabItems = Builders.Filter.In(x => x.ConversationId, conversationIds); - var exeLogDeleted = _dc.ExectionLogs.DeleteMany(filterExeLog); var promptLogDeleted = _dc.LlmCompletionLogs.DeleteMany(filterPromptLog); var contentLogDeleted = _dc.ContentLogs.DeleteMany(filterContentLog); var stateLogDeleted = _dc.StateLogs.DeleteMany(filterStateLog); @@ -71,10 +69,8 @@ public bool DeleteConversations(IEnumerable conversationIds) var cronDeleted = _dc.CrontabItems.DeleteMany(conbTabItems); var convDeleted = _dc.Conversations.DeleteMany(filterConv); - return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0 - || exeLogDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0 - || contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0 - || convDeleted.DeletedCount > 0; + return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0 + || contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0 || convDeleted.DeletedCount > 0; } [SideCar] diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs index 2316fdabb..769c943f7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs @@ -4,32 +4,6 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { - #region Execution Log - public void AddExecutionLogs(string conversationId, List logs) - { - if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return; - - var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var update = Builders.Update - .SetOnInsert(x => x.Id, Guid.NewGuid().ToString()) - .PushEach(x => x.Logs, logs); - - _dc.ExectionLogs.UpdateOne(filter, update, _options); - } - - public List GetExecutionLogs(string conversationId) - { - List logs = []; - if (string.IsNullOrEmpty(conversationId)) return logs; - - var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault(); - - logs = logCollection?.Logs ?? []; - return logs; - } - #endregion - #region LLM Completion Log public void SaveLlmCompletionLog(LlmCompletionLog log) {