Skip to content

Commit 80e157a

Browse files
authored
Merge pull request #217 from iceljc/features/add-delete-conversation
add delete conversation
2 parents a1bc56b + ddd460e commit 80e157a

File tree

7 files changed

+38
-6
lines changed

7 files changed

+38
-6
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IConversationService
1010
Task<List<Conversation>> GetConversations();
1111
Task<Conversation> UpdateConversationTitle(string id, string title);
1212
Task<List<Conversation>> GetLastConversations();
13-
Task DeleteConversation(string id);
13+
Task<bool> DeleteConversation(string id);
1414

1515
/// <summary>
1616
/// Send message to LLM

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRou
2828

2929
#region Conversation
3030
void CreateNewConversation(Conversation conversation);
31+
bool DeleteConversation(string conversationId);
3132
List<DialogElement> GetConversationDialogs(string conversationId);
3233
void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs);
3334
List<StateKeyValue> GetConversationStates(string conversationId);

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ public ConversationService(
3232
_logger = logger;
3333
}
3434

35-
public Task DeleteConversation(string id)
35+
public async Task<bool> DeleteConversation(string id)
3636
{
37-
throw new NotImplementedException();
37+
var db = _services.GetRequiredService<IBotSharpRepository>();
38+
var isDeleted = db.DeleteConversation(id);
39+
return await Task.FromResult(isDeleted);
3840
}
41+
3942
public async Task<Conversation> UpdateConversationTitle(string id, string title)
4043
{
4144
var db = _services.GetRequiredService<IBotSharpRepository>();

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public void CreateNewConversation(Conversation conversation)
121121
throw new NotImplementedException();
122122
}
123123

124+
public bool DeleteConversation(string conversationId)
125+
{
126+
throw new NotImplementedException();
127+
}
128+
124129
public Conversation GetConversation(string conversationId)
125130
{
126131
throw new NotImplementedException();

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using BotSharp.Abstraction.Agents.Models;
66
using MongoDB.Driver;
77
using BotSharp.Abstraction.Routing.Models;
8-
98
namespace BotSharp.Core.Repository;
109

1110
public class FileRepository : IBotSharpRepository
@@ -625,6 +624,17 @@ public void CreateNewConversation(Conversation conversation)
625624
}
626625
}
627626

627+
public bool DeleteConversation(string conversationId)
628+
{
629+
if (string.IsNullOrEmpty(conversationId)) return false;
630+
631+
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId);
632+
if (!Directory.Exists(dir)) return false;
633+
634+
Directory.Delete(dir, true);
635+
return true;
636+
}
637+
628638
public List<DialogElement> GetConversationDialogs(string conversationId)
629639
{
630640
var dialogs = new List<DialogElement>();

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
8383
}
8484

8585
[HttpDelete("/conversation/{conversationId}")]
86-
public async Task DeleteConversation([FromRoute] string conversationId)
86+
public async Task<bool> DeleteConversation([FromRoute] string conversationId)
8787
{
88-
var service = _services.GetRequiredService<IConversationService>();
88+
var conversationService = _services.GetRequiredService<IConversationService>();
89+
var response = await conversationService.DeleteConversation(conversationId);
90+
return response;
8991
}
9092

9193
[HttpPost("/conversation/{agentId}/{conversationId}")]

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,17 @@ public void CreateNewConversation(Conversation conversation)
627627
_dc.ConversationDialogs.InsertOne(dialog);
628628
}
629629

630+
public bool DeleteConversation(string conversationId)
631+
{
632+
if (string.IsNullOrEmpty(conversationId)) return false;
633+
var filterConv = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversationId);
634+
var filterDialog = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
635+
636+
var dialogDeleted = _dc.ConversationDialogs.DeleteMany(filterDialog);
637+
var convDeleted = _dc.Conversations.DeleteMany(filterConv);
638+
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0;
639+
}
640+
630641
public List<DialogElement> GetConversationDialogs(string conversationId)
631642
{
632643
var dialogs = new List<DialogElement>();

0 commit comments

Comments
 (0)