From be5f9f247ae1b85c699c7081b623bd9012c73641 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sun, 3 Sep 2023 14:06:40 -0500 Subject: [PATCH] update conversation record --- .../Records/ConversationRecord.cs | 3 - .../Services/ConversationService.cs | 29 ++++++++-- .../Services/ConversationStateService.cs | 57 ++++++++----------- .../Services/ConversationStorage.cs | 44 +++++++------- .../Collections/ConversationCollection.cs | 2 - .../Repository/MongoRepository.cs | 6 -- 6 files changed, 70 insertions(+), 71 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs index 6ad0b25d..6a4dc338 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs @@ -15,9 +15,6 @@ public class ConversationRecord : RecordBase [MaxLength(64)] public string Title { get; set; } = string.Empty; - public string Dialog { get; set; } = string.Empty; - public string State { get; set; } = string.Empty; - [Required] public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 9600bd09..0870cd84 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -1,7 +1,9 @@ using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Conversations.Settings; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Records; using MongoDB.Bson; +using System.IO; namespace BotSharp.Core.Conversations.Services; @@ -54,16 +56,33 @@ public partial class ConversationService : IConversationService public async Task NewConversation(Conversation sess) { var db = _services.GetRequiredService(); + var dbSettings = _services.GetRequiredService(); + var conversationSettings = _services.GetRequiredService(); + var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); + var foundUserId = user?.Id ?? _user.Id; var record = ConversationRecord.FromConversation(sess); - record.Id = sess.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()); - record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id); + record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString()); + record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId); record.Title = "New Conversation"; - db.Transaction(delegate + //db.Transaction(delegate + //{ + // db.Add(record); + //}); + + var dir = Path.Combine(dbSettings.FileRepository, conversationSettings.DataDir, record.Id); + if (!Directory.Exists(dir)) { - db.Add(record); - }); + Directory.CreateDirectory(dir); + } + var path = Path.Combine(dir, "conversation.json"); + File.WriteAllText(path, JsonSerializer.Serialize(record, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + })); _storage.InitStorage(record.Id); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index ccaf2662..81d23528 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -117,16 +117,7 @@ public class ConversationStateService : IConversationStateService, IDisposable public void CleanState() { - //File.Delete(_file); - var conversation = _db.Conversation.FirstOrDefault(x => x.Id == _conversationId); - if (conversation != null) - { - conversation.State = string.Empty; - _db.Transaction(delegate - { - _db.Add(conversation); - }); - } + File.Delete(_file); } private string GetStorageFile(string conversationId) @@ -145,32 +136,32 @@ public class ConversationStateService : IConversationStateService, IDisposable return stateFile; } - private string GetConversationState(string conversationId) - { - var conversation = _db.Conversation.FirstOrDefault(x => x.Id == conversationId); - if (conversation == null) - { - var user = _db.User.FirstOrDefault(x => x.ExternalId == _user.Id); - var record = new ConversationRecord() - { - Id = ObjectId.GenerateNewId().ToString(), - //AgentId = _agentSettings.RouterId, - UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(), - Title = "New Conversation", - Dialog = string.Empty, - State = string.Empty - }; + //private string GetConversationState(string conversationId) + //{ + // var conversation = _db.Conversation.FirstOrDefault(x => x.Id == conversationId); + // if (conversation == null) + // { + // var user = _db.User.FirstOrDefault(x => x.ExternalId == _user.Id); + // var record = new ConversationRecord() + // { + // Id = ObjectId.GenerateNewId().ToString(), + // //AgentId = _agentSettings.RouterId, + // UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(), + // Title = "New Conversation", + // Dialog = string.Empty, + // State = string.Empty + // }; - _db.Transaction(delegate - { - _db.Add(record); - }); + // _db.Transaction(delegate + // { + // _db.Add(record); + // }); - conversation = _db.Conversation.FirstOrDefault(x => x.Id == record.Id); - } + // conversation = _db.Conversation.FirstOrDefault(x => x.Id == record.Id); + // } - return conversation.State ?? string.Empty; - } + // return conversation.State ?? string.Empty; + //} public string GetState(string name) { diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index c55c3265..303e9c7b 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -127,29 +127,29 @@ public class ConversationStorage : IConversationStorage return Path.Combine(dir, "dialogs.txt"); } - private string GetConversationDialogs(string conversationId) - { - var db = _services.GetRequiredService(); - var conversation = db.Conversation.FirstOrDefault(x => x.Id == conversationId); - if (conversation == null) - { - var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); - var record = new ConversationRecord() - { - Id = ObjectId.GenerateNewId().ToString(), - //AgentId = _agentSettings.RouterId, - UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(), - Title = "New Conversation" - }; + //private string GetConversationDialogs(string conversationId) + //{ + // var db = _services.GetRequiredService(); + // var conversation = db.Conversation.FirstOrDefault(x => x.Id == conversationId); + // if (conversation == null) + // { + // var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); + // var record = new ConversationRecord() + // { + // Id = ObjectId.GenerateNewId().ToString(), + // //AgentId = _agentSettings.RouterId, + // UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(), + // Title = "New Conversation" + // }; - db.Transaction(delegate - { - db.Add(record); - }); + // db.Transaction(delegate + // { + // db.Add(record); + // }); - conversation = db.Conversation.FirstOrDefault(x => x.Id == record.Id); - } + // conversation = db.Conversation.FirstOrDefault(x => x.Id == record.Id); + // } - return conversation.Dialog ?? string.Empty; - } + // return conversation.Dialog ?? string.Empty; + //} } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs index 9e1fe51d..283551ef 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs @@ -5,8 +5,6 @@ public class ConversationCollection : MongoBase public Guid AgentId { get; set; } public Guid UserId { get; set; } public string Title { get; set; } - public string Dialog { get; set; } - public string State { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index d8244c7c..06590035 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -114,8 +114,6 @@ public class MongoRepository : IBotSharpRepository AgentId = x.AgentId.ToString(), UserId = x.UserId.ToString(), Title = x.Title, - Dialog = x.Dialog, - State = x.State, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -212,8 +210,6 @@ public class MongoRepository : IBotSharpRepository AgentId = Guid.Parse(x.AgentId), UserId = Guid.Parse(x.UserId), Title = x.Title, - Dialog = x.Dialog, - State = x.State, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -225,8 +221,6 @@ public class MongoRepository : IBotSharpRepository .Set(x => x.AgentId, conversation.AgentId) .Set(x => x.UserId, conversation.UserId) .Set(x => x.Title, conversation.Title) - .Set(x => x.Dialog, conversation.Dialog) - .Set(x => x.State, conversation.State) .Set(x => x.CreatedTime, conversation.CreatedTime) .Set(x => x.UpdatedTime, conversation.UpdatedTime); _dc.Conversations.UpdateOne(filter, update, _options);