diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index c64bd73b..38e017d6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -10,7 +10,7 @@ public interface IConversationService Task> GetConversations(); Task UpdateConversationTitle(string id, string title); Task> GetLastConversations(); - Task DeleteConversation(string id); + Task DeleteConversation(string id); /// /// Send message to LLM diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 4be0ac80..098fef83 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -28,6 +28,7 @@ public interface IBotSharpRepository #region Conversation void CreateNewConversation(Conversation conversation); + bool DeleteConversation(string conversationId); List GetConversationDialogs(string conversationId); void AppendConversationDialogs(string conversationId, List dialogs); List GetConversationStates(string conversationId); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 4972d3c5..ff64a1e6 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -32,10 +32,13 @@ public partial class ConversationService : IConversationService _logger = logger; } - public Task DeleteConversation(string id) + public async Task DeleteConversation(string id) { - throw new NotImplementedException(); + var db = _services.GetRequiredService(); + var isDeleted = db.DeleteConversation(id); + return await Task.FromResult(isDeleted); } + public async Task UpdateConversationTitle(string id, string title) { var db = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 3d9b53df..f4505d68 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -121,6 +121,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } + public bool DeleteConversation(string conversationId) + { + throw new NotImplementedException(); + } + public Conversation GetConversation(string conversationId) { throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 1c46de7b..1bd787ea 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -5,7 +5,6 @@ using BotSharp.Abstraction.Users.Models; using BotSharp.Abstraction.Agents.Models; using MongoDB.Driver; using BotSharp.Abstraction.Routing.Models; - namespace BotSharp.Core.Repository; public class FileRepository : IBotSharpRepository @@ -625,6 +624,17 @@ public class FileRepository : IBotSharpRepository } } + public bool DeleteConversation(string conversationId) + { + if (string.IsNullOrEmpty(conversationId)) return false; + + var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId); + if (!Directory.Exists(dir)) return false; + + Directory.Delete(dir, true); + return true; + } + public List GetConversationDialogs(string conversationId) { var dialogs = new List(); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 8a178ea1..de82fbf1 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -83,9 +83,11 @@ public class ConversationController : ControllerBase, IApiAdapter } [HttpDelete("/conversation/{conversationId}")] - public async Task DeleteConversation([FromRoute] string conversationId) + public async Task DeleteConversation([FromRoute] string conversationId) { - var service = _services.GetRequiredService(); + var conversationService = _services.GetRequiredService(); + var response = await conversationService.DeleteConversation(conversationId); + return response; } [HttpPost("/conversation/{agentId}/{conversationId}")] diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 54353906..71c59f94 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -627,6 +627,17 @@ public class MongoRepository : IBotSharpRepository _dc.ConversationDialogs.InsertOne(dialog); } + public bool DeleteConversation(string conversationId) + { + if (string.IsNullOrEmpty(conversationId)) return false; + var filterConv = Builders.Filter.Eq(x => x.Id, conversationId); + var filterDialog = Builders.Filter.Eq(x => x.ConversationId, conversationId); + + var dialogDeleted = _dc.ConversationDialogs.DeleteMany(filterDialog); + var convDeleted = _dc.Conversations.DeleteMany(filterConv); + return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0; + } + public List GetConversationDialogs(string conversationId) { var dialogs = new List();