diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 3cc2c5fdb..6b29a72bb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -13,7 +13,7 @@ public interface IConversationService Task> GetConversations(ConversationFilter filter); Task UpdateConversationTitle(string id, string title); Task> GetLastConversations(); - Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours); + Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds); Task DeleteConversations(IEnumerable ids); /// diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs index 9df16428d..bf29f5212 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs @@ -12,8 +12,8 @@ public class ConversationSetting public bool EnableContentLog { get; set; } public bool EnableStateLog { get; set; } public bool EnableTranslationMemory { get; set; } - public CleanConversationSetting CleanSetting { get; set; } = new CleanConversationSetting(); - public RateLimitSetting RateLimit { get; set; } = new RateLimitSetting(); + public CleanConversationSetting CleanSetting { get; set; } = new(); + public RateLimitSetting RateLimit { get; set; } = new(); } public class CleanConversationSetting @@ -22,5 +22,5 @@ public class CleanConversationSetting public int BatchSize { get; set; } public int MessageLimit { get; set; } public int BufferHours { get; set; } - + public IEnumerable ExcludeAgentIds { get; set; } = new List(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 0af440807..ed5d2dab9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -67,7 +67,7 @@ public interface IBotSharpRepository void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint); ConversationBreakpoint? GetConversationBreakpoint(string conversationId); List GetLastConversations(); - List GetIdleConversations(int batchSize, int messageLimit, int bufferHours); + List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds); IEnumerable TruncateConversation(string conversationId, string messageId, bool cleanLog = false); #endregion diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index f2a93b3f0..6e0f97d18 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -70,10 +70,10 @@ public async Task> GetLastConversations() return db.GetLastConversations(); } - public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var db = _services.GetRequiredService(); - return db.GetIdleConversations(batchSize, messageLimit, bufferHours); + return db.GetIdleConversations(batchSize, messageLimit, bufferHours, excludeAgentIds ?? new List()); } public async Task NewConversation(Conversation sess) diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 5ce769012..ebaa8ee11 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -145,7 +145,7 @@ public PagedItems GetConversations(ConversationFilter filter) public List GetLastConversations() => throw new NotImplementedException(); - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) => throw new NotImplementedException(); public List GetConversationDialogs(string conversationId) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index e4bb50980..6f078b619 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -378,7 +378,7 @@ public List GetLastConversations() .ToList(); } - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var ids = new List(); var batchLimit = 100; @@ -423,7 +423,7 @@ public List GetIdleConversations(int batchSize, int messageLimit, int bu continue; } - if (conv.UpdatedTime > utcNow.AddHours(-bufferHours)) + if (excludeAgentIds.Contains(conv.AgentId) || conv.UpdatedTime > utcNow.AddHours(-bufferHours)) { continue; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs b/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs index c1222a4e6..a409ea149 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs @@ -62,7 +62,7 @@ private async Task CleanIdleConversationsAsync() if (cleanSetting == null || !cleanSetting.Enable) return; var conversationService = scope.ServiceProvider.GetRequiredService(); - var conversationIds = await conversationService.GetIdleConversations(cleanSetting.BatchSize, cleanSetting.MessageLimit, cleanSetting.BufferHours); + var conversationIds = await conversationService.GetIdleConversations(cleanSetting.BatchSize, cleanSetting.MessageLimit, cleanSetting.BufferHours, cleanSetting.ExcludeAgentIds); if (!conversationIds.IsNullOrEmpty()) { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index e3f95f278..9aed7265b 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -344,7 +344,7 @@ public List GetLastConversations() }).ToList(); } - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var page = 1; var batchLimit = 100; @@ -370,7 +370,7 @@ public List GetIdleConversations(int batchSize, int messageLimit, int bu { var skip = (page - 1) * batchSize; var candidates = _dc.Conversations.AsQueryable() - .Where(x => x.DialogCount <= messageLimit && x.UpdatedTime <= utcNow.AddHours(-bufferHours)) + .Where(x => !excludeAgentIds.Contains(x.AgentId) && x.DialogCount <= messageLimit && x.UpdatedTime <= utcNow.AddHours(-bufferHours)) .Skip(skip) .Take(batchSize) .Select(x => x.Id)