From 59160bffcb5a2a3b8cdb1aeb4ecd693be2dfd5f4 Mon Sep 17 00:00:00 2001 From: Visagan Guruparan <103048@smsassist.com> Date: Tue, 21 Nov 2023 23:07:19 -0600 Subject: [PATCH] Resolve issue "Update Conversation title dynamically" --- .../Conversations/IConversationService.cs | 1 + .../Repositories/IBotSharpRepository.cs | 1 + .../Services/ConversationService.SendMessage.cs | 2 ++ .../Services/ConversationService.cs | 8 +++++++- .../Repository/BotSharpDbContext.cs | 5 ++++- .../BotSharp.Core/Repository/FileRepository.cs | 17 ++++++++++++++++- .../Repository/MongoRepository.cs | 13 +++++++++++++ 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index ba011cbf..c64bd73b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -8,6 +8,7 @@ public interface IConversationService void SetConversationId(string conversationId, List states); Task GetConversation(string id); Task> GetConversations(); + Task UpdateConversationTitle(string id, string title); Task> GetLastConversations(); Task DeleteConversation(string id); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 95b85e15..d0304f72 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -35,6 +35,7 @@ public interface IBotSharpRepository void UpdateConversationStatus(string conversationId, string status); Conversation GetConversation(string conversationId); List GetConversations(string userId); + void UpdateConversationTitle(string conversationId, string title); List GetLastConversations(); void AddExectionLogs(string conversationId, List logs); List GetExectionLogs(string conversationId); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index b6d2c278..69c21d48 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -128,5 +128,7 @@ public partial class ConversationService // Add to dialog history _storage.Append(_conversationId, response); + var conversation = _services.GetRequiredService(); + var updatedConversation = await conversation.UpdateConversationTitle(_conversationId, response.Instruction.Reason); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 3670c31c..4972d3c5 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -36,7 +36,13 @@ public partial class ConversationService : IConversationService { throw new NotImplementedException(); } - + public async Task UpdateConversationTitle(string id, string title) + { + var db = _services.GetRequiredService(); + db.UpdateConversationTitle(id, title); + var conversation = db.GetConversation(id); + return conversation; + } public async Task GetConversation(string id) { var db = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index eb2540d4..452c04e7 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -150,7 +150,10 @@ public class BotSharpDbContext : Database, IBotSharpRepository { throw new NotImplementedException(); } - + public void UpdateConversationTitle(string conversationId, string title) + { + throw new NotImplementedException(); + } public void UpdateConversationStates(string conversationId, List states) { throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 8df8a1ee..3da66f4f 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -654,7 +654,22 @@ public class FileRepository : IBotSharpRepository return; } - + public void UpdateConversationTitle(string conversationId, string title) + { + var convDir = FindConversationDirectory(conversationId); + if (!string.IsNullOrEmpty(convDir)) + { + var convFile = Path.Combine(convDir, "conversation.json"); + var content = File.ReadAllText(convFile); + var record = JsonSerializer.Deserialize(content, _options); + if (record != null) + { + record.Title = title; + record.UpdatedTime = DateTime.UtcNow; + File.WriteAllText(convFile, JsonSerializer.Serialize(record, _options)); + } + } + } public List GetConversationStates(string conversationId) { var curStates = new List(); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index abdf3479..18d89a5d 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -656,7 +656,20 @@ public class MongoRepository : IBotSharpRepository _dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog); _dc.Conversations.UpdateOne(filterConv, updateConv); } + public void UpdateConversationTitle(string conversationId, string title) + { + if (string.IsNullOrEmpty(conversationId)) return; + var filterConv = Builders.Filter.Eq(x => x.Id, conversationId); + var foundConv = _dc.Conversations.Find(filterConv).FirstOrDefault(); + if (foundConv == null) return; + + var updateConv = Builders.Update + .Set(x => x.UpdatedTime, DateTime.UtcNow) + .Set(x => x.Title, title); + + _dc.Conversations.UpdateOne(filterConv, updateConv); + } public List GetConversationStates(string conversationId) { var states = new List();