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 1bf8b0e9..4be0ac80 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 16db5a18..3d9b53df 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 2c596804..1c46de7b 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -653,7 +653,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 d6ad0e7f..54353906 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -659,7 +659,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();