From 4f9595f1cd4745057fb0d33226d3fde41d8618da Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Fri, 8 Sep 2023 12:17:54 -0500 Subject: [PATCH] refine code --- .../Agents/Models/Agent.cs | 3 - .../Conversations/Models/ConversationState.cs | 5 ++ .../Services/AgentService.CreateAgent.cs | 4 +- .../Repository/FileRepository.cs | 65 +++++-------------- .../Repository/MongoRepository.cs | 27 ++++---- 5 files changed, 36 insertions(+), 68 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 2536e83c..0e9a33cd 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -35,9 +35,6 @@ public class Agent public bool IsPublic { get; set; } - public DateTime CreatedTime { get; set; } - public DateTime UpdatedTime { get; set; } - public override string ToString() => $"{Name} {Id}"; diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs index 98fcbf0c..8cd753a1 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs @@ -16,4 +16,9 @@ public class ConversationState : Dictionary this[pair.Key] = pair.Value; } } + + public List ToKeyValueList() + { + return this.Select(x => new KeyValueModel(x.Key, x.Value)).ToList(); + } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index a9ae0243..fb120037 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -23,8 +23,8 @@ public partial class AgentService agentRecord = Agent.Clone(agent); agentRecord.Id = Guid.NewGuid().ToString(); - agentRecord.CreatedTime = DateTime.UtcNow; - agentRecord.UpdatedTime = DateTime.UtcNow; + agentRecord.CreatedDateTime = DateTime.UtcNow; + agentRecord.UpdatedDateTime = DateTime.UtcNow; var dbSettings = _services.GetRequiredService(); var agentSettings = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index c582c2e2..32e40cc2 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -1,13 +1,6 @@ -using BotSharp.Abstraction.Conversations.Models; -using BotSharp.Abstraction.Conversations.Settings; -using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Models; -using BotSharp.Abstraction.Repositories.Records; -using MongoDB.Driver.Core.Operations; using System.IO; -using static Tensorflow.TensorShapeProto.Types; -using Tensorflow; using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef; using BotSharp.Abstraction.Users.Models; using BotSharp.Abstraction.Agents.Models; @@ -19,20 +12,17 @@ public class FileRepository : IBotSharpRepository { private readonly BotSharpDatabaseSettings _dbSettings; private readonly AgentSettings _agentSettings; - private readonly ConversationSetting _conversationSetting; - private readonly IServiceProvider _services; + private readonly ConversationSetting _conversationSettings; private JsonSerializerOptions _options; public FileRepository( - IServiceProvider services, BotSharpDatabaseSettings dbSettings, AgentSettings agentSettings, - ConversationSetting conversationSetting) + ConversationSetting conversationSettings) { - _services = services; _dbSettings = dbSettings; _agentSettings = agentSettings; - _conversationSetting = conversationSetting; + _conversationSettings = conversationSettings; _options = new JsonSerializerOptions { @@ -47,7 +37,7 @@ public class FileRepository : IBotSharpRepository { get { - if (_users != null) + if (!_users.IsNullOrEmpty()) { return _users.AsQueryable(); } @@ -68,12 +58,11 @@ public class FileRepository : IBotSharpRepository { get { - if (_agents != null) + if (!_agents.IsNullOrEmpty()) { return _agents.AsQueryable(); } - //var agentSettings = _services.GetService(); var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); _agents = new List(); foreach (var d in Directory.GetDirectories(dir)) @@ -90,7 +79,7 @@ public class FileRepository : IBotSharpRepository { get { - if (_userAgents != null && _userAgents.Count > 0) + if (!_userAgents.IsNullOrEmpty()) { return _userAgents.AsQueryable(); } @@ -115,13 +104,12 @@ public class FileRepository : IBotSharpRepository { get { - if (_conversations != null) + if (!_conversations.IsNullOrEmpty()) { return _conversations.AsQueryable(); } - //var convSettings = _services.GetService(); - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir); + var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); _conversations = new List(); foreach (var d in Directory.GetDirectories(dir)) { @@ -141,14 +129,12 @@ public class FileRepository : IBotSharpRepository { get { - if (_routingItems != null) + if (!_routingItems.IsNullOrEmpty()) { return _routingItems.AsQueryable(); } _routingItems = new List(); - //var agentSettings = _services.GetRequiredService(); - //var dbSettings = _services.GetRequiredService(); var filePath = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, "route.json"); if (File.Exists(filePath)) { @@ -164,14 +150,12 @@ public class FileRepository : IBotSharpRepository { get { - if (_routingProfiles != null) + if (!_routingProfiles.IsNullOrEmpty()) { return _routingProfiles.AsQueryable(); } _routingProfiles = new List(); - //var agentSettings = _services.GetRequiredService(); - //var dbSettings = _services.GetRequiredService(); var filePath = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, "routing-profile.json"); if (File.Exists(filePath)) { @@ -217,12 +201,10 @@ public class FileRepository : IBotSharpRepository { if (table == nameof(Conversation)) { - //var convSettings = _services.GetService(); - foreach (var conversation in _conversations) { var dir = Path.Combine(_dbSettings.FileRepository, - _conversationSetting.DataDir, + _conversationSettings.DataDir, conversation.Id); if (!Directory.Exists(dir)) { @@ -234,8 +216,6 @@ public class FileRepository : IBotSharpRepository } else if (table == nameof(Agent)) { - //var agentSettings = _services.GetService(); - foreach (var agent in _agents) { var dir = Path.Combine(_dbSettings.FileRepository, @@ -330,8 +310,6 @@ public class FileRepository : IBotSharpRepository private string GetAgentDataDir(string agentId) { - //var dbSettings = _services.GetRequiredService(); - //var agentSettings = _services.GetRequiredService(); var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId); if (!Directory.Exists(dir)) { @@ -413,7 +391,7 @@ public class FileRepository : IBotSharpRepository public void CreateNewConversation(Conversation conversation) { - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir, conversation.Id); + var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversation.Id); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); @@ -491,7 +469,7 @@ public class FileRepository : IBotSharpRepository private string? FindConversationDirectory(string conversationId) { - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir); + var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); foreach (var d in Directory.GetDirectories(dir)) { @@ -545,7 +523,7 @@ public class FileRepository : IBotSharpRepository if (record != null && File.Exists(stateFile)) { var states = File.ReadLines(stateFile); - //record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); // to do + record.States = new ConversationState(states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList()); } return record; @@ -557,7 +535,7 @@ public class FileRepository : IBotSharpRepository public List GetConversations(string userId) { var records = new List(); - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir); + var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); foreach (var d in Directory.GetDirectories(dir)) { @@ -568,19 +546,6 @@ public class FileRepository : IBotSharpRepository var record = JsonSerializer.Deserialize(json, _options); if (record != null && record.UserId == userId) { - var dialogFile = Path.Combine(d, "dialogs.txt"); - if (File.Exists(dialogFile)) - { - record.Dialog = File.ReadAllText(dialogFile); - } - - var stateFile = Path.Combine(d, "state.dict"); - if (File.Exists(stateFile)) - { - var states = File.ReadLines(stateFile); - //record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); // to do - } - records.Add(record); } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 3aaf8a03..6402f94c 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -28,7 +28,7 @@ public class MongoRepository : IBotSharpRepository { get { - if (_agents != null) + if (!_agents.IsNullOrEmpty()) { return _agents.AsQueryable(); } @@ -43,8 +43,8 @@ public class MongoRepository : IBotSharpRepository Functions = x.Functions, Responses = x.Responses, IsPublic = x.IsPublic, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime + CreatedDateTime = x.CreatedTime, + UpdatedDateTime = x.UpdatedTime }).ToList(); return _agents.AsQueryable(); @@ -56,7 +56,7 @@ public class MongoRepository : IBotSharpRepository { get { - if (_users != null) + if (!_users.IsNullOrEmpty()) { return _users.AsQueryable(); } @@ -84,7 +84,7 @@ public class MongoRepository : IBotSharpRepository { get { - if (_userAgents != null && _userAgents.Count > 0) + if (!_userAgents.IsNullOrEmpty()) { return _userAgents.AsQueryable(); } @@ -108,7 +108,7 @@ public class MongoRepository : IBotSharpRepository { get { - if (_conversations != null) + if (!_conversations.IsNullOrEmpty()) { return _conversations.AsQueryable(); } @@ -120,7 +120,6 @@ public class MongoRepository : IBotSharpRepository { var convId = conv.Id.ToString(); var dialog = GetConversationDialog(convId); - var states = GetConversationStates(convId); _conversations.Add(new Conversation { Id = convId, @@ -128,7 +127,7 @@ public class MongoRepository : IBotSharpRepository UserId = conv.UserId.ToString(), Title = conv.Title, Dialog = dialog, - //State = states, to do + States = new ConversationState(conv.States), CreatedTime = conv.CreatedTime, UpdatedTime = conv.UpdatedTime }); @@ -143,7 +142,7 @@ public class MongoRepository : IBotSharpRepository { get { - if (_routingItems != null) + if (!_routingItems.IsNullOrEmpty()) { return _routingItems.AsQueryable(); } @@ -168,7 +167,7 @@ public class MongoRepository : IBotSharpRepository { get { - if (_routingProfiles != null) + if (!_routingProfiles.IsNullOrEmpty()) { return _routingProfiles.AsQueryable(); } @@ -226,6 +225,7 @@ public class MongoRepository : IBotSharpRepository AgentId = Guid.Parse(x.AgentId), UserId = Guid.Parse(x.UserId), Title = x.Title, + States = x.States?.ToKeyValueList() ?? new List(), CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -237,6 +237,7 @@ 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.States, conversation.States) .Set(x => x.CreatedTime, conversation.CreatedTime) .Set(x => x.UpdatedTime, conversation.UpdatedTime); _dc.Conversations.UpdateOne(filter, update, _options); @@ -253,8 +254,8 @@ public class MongoRepository : IBotSharpRepository Functions = x.Functions, Responses = x.Responses, IsPublic = x.IsPublic, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime + CreatedTime = x.CreatedDateTime, + UpdatedTime = x.UpdatedDateTime }).ToList(); foreach (var agent in agents) @@ -391,7 +392,7 @@ public class MongoRepository : IBotSharpRepository .Set(x => x.Functions, agent.Functions) .Set(x => x.Responses, agent.Responses) .Set(x => x.IsPublic, agent.IsPublic) - .Set(x => x.UpdatedTime, agent.UpdatedTime); + .Set(x => x.UpdatedTime, agent.UpdatedDateTime); _dc.Agents.UpdateOne(filter, update, _options); }