From 45ed8d6cfe3298b82b2d34bd4c1a4e842a3da3e7 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 7 Sep 2023 17:04:34 -0500 Subject: [PATCH] remove record reference --- .../Agents/Models/Agent.cs | 48 ++++- .../Agents/Models/UserAgent.cs | 10 + .../Conversations/Models/Conversation.cs | 2 +- .../Repositories/IBotSharpRepository.cs | 37 ++-- .../Repositories/Records/AgentRecord.cs | 57 +----- .../Records/ConversationRecord.cs | 37 +--- .../Repositories/Records/RecordBase.cs | 11 +- .../Repositories/Records/RoutingItemRecord.cs | 29 +-- .../Records/RoutingProfileRecord.cs | 18 -- .../Repositories/Records/UserAgentRecord.cs | 22 --- .../Repositories/Records/UserRecord.cs | 29 --- .../Routing/Models/RoutingItem.cs | 2 + .../Routing/Models/RoutingProfile.cs | 4 +- .../BotSharp.Abstraction/Users/Models/User.cs | 1 + .../Utilities/GuidExtensitions.cs | 7 + .../Services/AgentService.CreateAgent.cs | 50 +++-- .../Agents/Services/AgentService.GetAgents.cs | 10 +- .../Services/AgentService.UpdateAgent.cs | 6 +- .../Services/ConversationService.cs | 12 +- .../Services/ConversationStateService.cs | 4 +- .../Services/ConversationStorage.cs | 2 +- .../Repository/BotSharpDbContext.cs | 104 ++++++----- .../Repository/FileRepository.cs | 116 ++++++------ .../BotSharp.Core/Routing/Router.cs | 4 +- .../Routing/Services/RoutingService.cs | 8 +- .../Users/Services/UserService.cs | 12 +- .../Controllers/RoutingController.cs | 1 - .../Routing/RoutingItemViewModel.cs | 2 + .../Routing/RoutingProfileCreationModel.cs | 2 +- .../Routing/RoutingProfileViewModel.cs | 4 +- .../Collections/ConversationCollection.cs | 2 +- .../ConversationDialogCollection.cs | 2 - .../ConversationStatesCollection.cs | 11 -- .../MongoDbContext.cs | 3 - .../Repository/MongoRepository.cs | 172 ++++++++---------- 35 files changed, 354 insertions(+), 487 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Agents/Models/UserAgent.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Utilities/GuidExtensitions.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStatesCollection.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index ca11fb8d..2536e83c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -4,7 +4,7 @@ public class Agent { public string Id { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; - public string? Description { get; set; } + public string Description { get; set; } public DateTime CreatedDateTime { get; set; } public DateTime UpdatedDateTime { get; set; } @@ -35,9 +35,31 @@ public class Agent public bool IsPublic { get; set; } + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } + public override string ToString() => $"{Name} {Id}"; + + public static Agent Clone(Agent agent) + { + return new Agent + { + Id = agent.Id, + Name = agent.Name, + Description = agent.Description, + Instruction = agent.Instruction, + Functions = agent.Functions, + Responses = agent.Responses, + Samples = agent.Samples, + Knowledges = agent.Knowledges, + IsPublic = agent.IsPublic, + CreatedDateTime = agent.CreatedDateTime, + UpdatedDateTime = agent.UpdatedDateTime, + }; + } + public Agent SetInstruction(string instruction) { Instruction = instruction; @@ -55,4 +77,28 @@ public class Agent Responses = responses ?? new List(); ; return this; } + + public Agent SetId(string id) + { + Id = id; + return this; + } + + public Agent SetName(string name) + { + Name = name; + return this; + } + + public Agent SetDescription(string description) + { + Description = description; + return this; + } + + public Agent SetIsPublic(bool isPublic) + { + IsPublic = isPublic; + return this; + } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/UserAgent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/UserAgent.cs new file mode 100644 index 00000000..55033a04 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/UserAgent.cs @@ -0,0 +1,10 @@ +namespace BotSharp.Abstraction.Agents.Models; + +public class UserAgent +{ + public string Id { get; set; } = string.Empty; + public string UserId { get; set; } = string.Empty; + public string AgentId { get; set; } = string.Empty; + public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; + public DateTime CreatedTime { get; set; } = DateTime.UtcNow; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs index 004d94e6..f8f85871 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs @@ -7,7 +7,7 @@ public class Conversation public string UserId { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; public string Dialog { get; set; } = string.Empty; - public ConversationState State { get; set; } + public ConversationState States { get; set; } public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; public DateTime CreatedTime { get; set; } = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index e0b0d4a1..8daa7b48 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -1,41 +1,40 @@ using BotSharp.Abstraction.Repositories.Models; -using BotSharp.Abstraction.Repositories.Records; using BotSharp.Abstraction.Routing.Models; -using System.Linq; +using BotSharp.Abstraction.Users.Models; namespace BotSharp.Abstraction.Repositories; public interface IBotSharpRepository { - IQueryable User { get; } - IQueryable Agent { get; } - IQueryable UserAgent { get; } - IQueryable Conversation { get; } - IQueryable RoutingItem { get; } - IQueryable RoutingProfile { get; } + IQueryable Users { get; } + IQueryable Agents { get; } + IQueryable UserAgents { get; } + IQueryable Conversations { get; } + IQueryable RoutingItems { get; } + IQueryable RoutingProfiles { get; } int Transaction(Action action); void Add(object entity); - UserRecord GetUserByEmail(string email); - void CreateUser(UserRecord user); - void UpdateAgent(AgentRecord agent); + User GetUserByEmail(string email); + void CreateUser(User user); + void UpdateAgent(Agent agent); - List CreateRoutingItems(List routingItems); - List CreateRoutingProfiles(List profiles); + List CreateRoutingItems(List routingItems); + List CreateRoutingProfiles(List profiles); void DeleteRoutingItems(); void DeleteRoutingProfiles(); - AgentRecord GetAgent(string agentId); + Agent GetAgent(string agentId); List GetAgentResponses(string agentId); - void CreateNewConversation(ConversationRecord conversation); + void CreateNewConversation(Conversation conversation); string GetConversationDialog(string conversationId); void UpdateConversationDialog(string conversationId, string dialogs); - List GetConversationState(string conversationId); - void UpdateConversationState(string conversationId, List state); + List GetConversationStates(string conversationId); + void UpdateConversationStates(string conversationId, List states); - ConversationRecord GetConversation(string conversationId); - List GetConversations(string userId); + Conversation GetConversation(string conversationId); + List GetConversations(string userId); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs index 3a460218..3f35bc56 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Utilities; + namespace BotSharp.Abstraction.Repositories.Records; public class AgentRecord : RecordBase @@ -23,59 +25,4 @@ public class AgentRecord : RecordBase [Required] public DateTime UpdatedTime { get; set; } - - public static AgentRecord FromAgent(Agent agent) - { - return new AgentRecord - { - Id = agent.Id, - Name = agent.Name, - Description = agent.Description, - Instruction = agent.Instruction, - Functions = agent.Functions, - Responses = agent.Responses, - IsPublic = agent.IsPublic - }; - } - - public Agent ToAgent() - { - return new Agent - { - Id = Id, - Name = Name, - Description = Description, - Instruction = Instruction, - Functions = Functions, - Responses = Responses, - IsPublic = IsPublic, - CreatedDateTime = CreatedTime, - UpdatedDateTime = UpdatedTime - }; - } - - public AgentRecord SetId(string id) - { - Id = id; - return this; - } - - - public AgentRecord SetInstruction(string instruction) - { - Instruction = instruction; - return this; - } - - public AgentRecord SetFunctions(List functions) - { - Functions = functions; - return this; - } - - public AgentRecord SetResponses(List responses) - { - Responses = responses; - return this; - } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs index ecbcc9cc..5b5c58b8 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Repositories.Models; using System.Text.Json.Serialization; @@ -8,11 +7,11 @@ public class ConversationRecord : RecordBase { [Required] [MaxLength(36)] - public string AgentId { get; set; } = string.Empty; + public Guid AgentId { get; set; } = Guid.Empty; [Required] [MaxLength(36)] - public string UserId { get; set; } = string.Empty; + public Guid UserId { get; set; } = Guid.Empty; [MaxLength(64)] public string Title { get; set; } = string.Empty; @@ -21,41 +20,11 @@ public class ConversationRecord : RecordBase public string Dialog { get; set; } [JsonIgnore] - public List State { get; set; } + public List States { get; set; } [Required] public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; [Required] public DateTime CreatedTime { get; set; } = DateTime.UtcNow; - - public static ConversationRecord FromConversation(Conversation conv) - { - return new ConversationRecord - { - AgentId = conv.AgentId, - UserId = conv.UserId, - Id = conv.Id, - Title = conv.Title, - Dialog = conv.Dialog, - State = conv.State?.Select(x => new KeyValueModel(x.Key, x.Value))?.ToList() ?? new List(), - CreatedTime = conv.CreatedTime, - UpdatedTime = conv.UpdatedTime - }; - } - - public Conversation ToConversation() - { - return new Conversation - { - Id = Id, - Title = Title, - UserId = UserId, - AgentId = AgentId, - Dialog = Dialog, - State = new ConversationState(State), - CreatedTime = CreatedTime, - UpdatedTime = UpdatedTime - }; - } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RecordBase.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RecordBase.cs index 16e2535a..be1bb943 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RecordBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RecordBase.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace BotSharp.Abstraction.Repositories.Records; -namespace BotSharp.Abstraction.Repositories.Records +public class RecordBase { - public class RecordBase - { - public string? Id { get; set; } - } + public Guid Id { get; set; } = Guid.Empty; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs index 77a535d5..b33315f1 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingItemRecord.cs @@ -1,39 +1,14 @@ using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Utilities; namespace BotSharp.Abstraction.Repositories.Records; public class RoutingItemRecord : RecordBase { - public string AgentId { get; set; } + public Guid AgentId { get; set; } public string Name { get; set; } public string Description { get; set; } public List RequiredFields { get; set; } = new List(); public string? RedirectTo { get; set; } public bool Disabled { get; set; } - - public RoutingItem ToRoutingItem() - { - return new RoutingItem - { - AgentId = AgentId, - Name = Name, - Description = Description, - RequiredFields = RequiredFields, - RedirectTo = RedirectTo, - Disabled = Disabled - }; - } - - public static RoutingItemRecord FromRoutingItem(RoutingItem item) - { - return new RoutingItemRecord - { - AgentId = item.AgentId, - Name = item.Name, - Description = item.Description, - RequiredFields = item.RequiredFields, - RedirectTo = item.RedirectTo, - Disabled = item.Disabled - }; - } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingProfileRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingProfileRecord.cs index ef8242cb..5f5f3b21 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingProfileRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/RoutingProfileRecord.cs @@ -6,22 +6,4 @@ public class RoutingProfileRecord : RecordBase { public string Name { get; set; } public List AgentIds { get; set; } - - public RoutingProfile ToRoutingProfile() - { - return new RoutingProfile - { - Name = Name, - AgentIds = AgentIds.ToArray(), - }; - } - - public static RoutingProfileRecord FromRoutingProfile(RoutingProfile profile) - { - return new RoutingProfileRecord - { - Name = profile.Name, - AgentIds = profile.AgentIds.ToList(), - }; - } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserAgentRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserAgentRecord.cs index 6cdf277b..19d9fab6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserAgentRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserAgentRecord.cs @@ -17,26 +17,4 @@ public class UserAgentRecord : RecordBase [Required] public DateTime CreatedTime { get; set; } = DateTime.UtcNow; - - public static UserRecord FromUser(User user) - { - return new UserRecord - { - Id = user.Id, - FirstName = user.FirstName, - LastName = user.LastName, - Email = user.Email, - Password = user.Password - }; - } - - public User ToUser() - { - return new User - { - Id = Id, - CreatedTime = CreatedTime, - UpdatedTime = UpdatedTime - }; - } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserRecord.cs index 6500ba27..db3353e2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/UserRecord.cs @@ -1,5 +1,3 @@ -using BotSharp.Abstraction.Users.Models; - namespace BotSharp.Abstraction.Repositories.Records; public class UserRecord : RecordBase @@ -32,31 +30,4 @@ public class UserRecord : RecordBase [Required] public DateTime CreatedTime { get; set; } = DateTime.UtcNow; - - public static UserRecord FromUser(User user) - { - return new UserRecord - { - Id = user.Id, - FirstName = user.FirstName, - LastName = user.LastName, - Email = user.Email, - Password = user.Password - }; - } - - public User ToUser() - { - return new User - { - Id = Id, - FirstName = FirstName, - LastName = LastName, - Email = Email, - Salt = Salt, - Password = Password, - CreatedTime = CreatedTime, - UpdatedTime = UpdatedTime - }; - } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingItem.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingItem.cs index 1515a05e..2c7bc7d5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingItem.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingItem.cs @@ -4,6 +4,8 @@ namespace BotSharp.Abstraction.Routing.Models; public class RoutingItem { + public string Id { get; set; } + [JsonPropertyName("agent_id")] public string AgentId { get; set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfile.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfile.cs index d92d4d82..c269b5d1 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfile.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfile.cs @@ -4,9 +4,11 @@ namespace BotSharp.Abstraction.Routing.Models; public class RoutingProfile { + public string Id { get; set; } + [JsonPropertyName("name")] public string Name { get; set; } [JsonPropertyName("agent_ids")] - public string[] AgentIds { get; set; } + public List AgentIds { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs index 69810a13..7253f4db 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs @@ -8,6 +8,7 @@ public class User public string Email { get; set; } = string.Empty; public string Salt { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; + public string? ExternalId { get; set; } public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; public DateTime CreatedTime { get; set; } = DateTime.UtcNow; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/GuidExtensitions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/GuidExtensitions.cs new file mode 100644 index 00000000..67c6d2b3 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/GuidExtensitions.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Abstraction.Utilities; + +public static class GuidExtensitions +{ + public static Guid IfNullOrEmptyAsDefault(this string str) + => string.IsNullOrEmpty(str) ? Guid.Empty : Guid.Parse(str); +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index a5762b66..a9ae0243 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -1,13 +1,6 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Repositories.Records; -using BotSharp.Abstraction.Users.Models; -using MongoDB.Bson; using System.IO; -using Tensorflow; -using static Tensorflow.TensorShapeProto.Types; namespace BotSharp.Core.Agents.Services; @@ -17,21 +10,21 @@ public partial class AgentService { var db = _services.GetRequiredService(); - var record = (from a in db.Agent - join ua in db.UserAgent on a.Id equals ua.AgentId - join u in db.User on ua.UserId equals u.Id + var agentRecord = (from a in db.Agents + join ua in db.UserAgents on a.Id equals ua.AgentId + join u in db.Users on ua.UserId equals u.Id where u.ExternalId == _user.Id && a.Name == agent.Name select a).FirstOrDefault(); - if (record != null) + if (agentRecord != null) { - return record.ToAgent(); + return agentRecord; } - record = AgentRecord.FromAgent(agent); - record.Id = Guid.NewGuid().ToString(); - record.CreatedTime = DateTime.UtcNow; - record.UpdatedTime = DateTime.UtcNow; + agentRecord = Agent.Clone(agent); + agentRecord.Id = Guid.NewGuid().ToString(); + agentRecord.CreatedTime = DateTime.UtcNow; + agentRecord.UpdatedTime = DateTime.UtcNow; var dbSettings = _services.GetRequiredService(); var agentSettings = _services.GetRequiredService(); @@ -40,29 +33,32 @@ public partial class AgentService if (foundAgent != null) { - record.SetId(foundAgent.Id) - .SetInstruction(foundAgent.Instruction) - .SetFunctions(foundAgent.Functions) - .SetResponses(foundAgent.Responses); + agentRecord.SetId(foundAgent.Id) + .SetName(foundAgent.Name) + .SetDescription(foundAgent.Description) + .SetIsPublic(foundAgent.IsPublic) + .SetInstruction(foundAgent.Instruction) + .SetFunctions(foundAgent.Functions) + .SetResponses(foundAgent.Responses); } - var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); - var userAgentRecord = new UserAgentRecord + var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id); + var userAgentRecord = new UserAgent { Id = Guid.NewGuid().ToString(), UserId = user.Id, - AgentId = foundAgent?.Id ?? record.Id, + AgentId = foundAgent?.Id ?? agentRecord.Id, CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow }; db.Transaction(delegate { - db.Add(record); + db.Add(agentRecord); db.Add(userAgentRecord); }); - return record.ToAgent(); + return agentRecord; } private JsonSerializerOptions _options = new JsonSerializerOptions @@ -83,7 +79,9 @@ public partial class AgentService var functions = FetchFunctionsFromFile(dir); var instruction = FetchInstructionFromFile(dir); var responses = FetchResponsesFromFile(dir); - return agent.SetInstruction(instruction).SetFunctions(functions).SetResponses(responses); + return agent.SetInstruction(instruction) + .SetFunctions(functions) + .SetResponses(responses); } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 657a85f9..259f126c 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -9,18 +9,18 @@ public partial class AgentService public async Task> GetAgents() { var db = _services.GetRequiredService(); - var query = from a in db.Agent - join ua in db.UserAgent on a.Id equals ua.AgentId - join u in db.User on ua.UserId equals u.Id + var query = from a in db.Agents + join ua in db.UserAgents on a.Id equals ua.AgentId + join u in db.Users on ua.UserId equals u.Id where ua.UserId == _user.Id || u.ExternalId == _user.Id || a.IsPublic - select a.ToAgent(); + select a; return query.ToList(); } public async Task GetAgent(string id) { var db = _services.GetRequiredService(); - var profile = db.GetAgent(id)?.ToAgent(); + var profile = db.GetAgent(id); var instructionFile = profile?.Instruction; if (instructionFile != null) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 737af8d7..6fe9cd48 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -10,9 +10,9 @@ public partial class AgentService { var db = _services.GetRequiredService(); - var record = (from a in db.Agent - join ua in db.UserAgent on a.Id equals ua.AgentId - join u in db.User on ua.UserId equals u.Id + var record = (from a in db.Agents + join ua in db.UserAgents on a.Id equals ua.AgentId + join u in db.Users on ua.UserId equals u.Id where (ua.UserId == _user.Id || u.ExternalId == _user.Id) && a.Id == agent.Id select a).FirstOrDefault(); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 87a1dbb7..50ecfe5c 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -40,15 +40,15 @@ public partial class ConversationService : IConversationService { var db = _services.GetRequiredService(); var conversation = db.GetConversation(id); - return conversation?.ToConversation(); + return conversation; } public async Task> GetConversations() { var db = _services.GetRequiredService(); - var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); + var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id); var conversations = db.GetConversations(user?.Id); - return conversations.Select(x => x.ToConversation()).OrderByDescending(x => x.CreatedTime).ToList(); + return conversations.OrderByDescending(x => x.CreatedTime).ToList(); } public async Task NewConversation(Conversation sess) @@ -56,16 +56,16 @@ public partial class ConversationService : IConversationService var db = _services.GetRequiredService(); var dbSettings = _services.GetRequiredService(); var conversationSettings = _services.GetRequiredService(); - var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); + var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id); var foundUserId = user?.Id ?? _user.Id; - var record = ConversationRecord.FromConversation(sess); + var record = sess; record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString()); record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId); record.Title = "New Conversation"; db.CreateNewConversation(record); - return record.ToConversation(); + return record; } public Task CleanHistory(string agentId) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 2fb397b1..f0edecf6 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -53,7 +53,7 @@ public class ConversationStateService : IConversationStateService, IDisposable return _states; } - _savedStates = _db.GetConversationState(_conversationId); + _savedStates = _db.GetConversationStates(_conversationId); if (!_savedStates.IsNullOrEmpty()) { @@ -88,7 +88,7 @@ public class ConversationStateService : IConversationStateService, IDisposable states.Add(new KeyValueModel(dic.Key, dic.Value)); } - _db.UpdateConversationState(_conversationId, states); + _db.UpdateConversationStates(_conversationId, states); _logger.LogInformation($"Saved state {_conversationId}"); } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index 4041fa83..7a9be970 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -43,7 +43,7 @@ public class ConversationStorage : IConversationStorage } else { - var agent = db.Agent.First(x => x.Id == agentId); + var agent = db.Agents.First(x => x.Id == agentId); sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{agent.Name}|"); var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 453f43a1..66b75ad5 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -1,33 +1,26 @@ +using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Models; -using BotSharp.Abstraction.Repositories.Records; +using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Users.Models; using Microsoft.EntityFrameworkCore.Infrastructure; namespace BotSharp.Core.Repository; public class BotSharpDbContext : Database, IBotSharpRepository { - public IQueryable User => Table(); - public IQueryable Agent => Table(); - public IQueryable UserAgent => Table(); - public IQueryable Conversation => Table(); - public IQueryable RoutingItem => throw new NotImplementedException(); - public IQueryable RoutingProfile => throw new NotImplementedException(); + public IQueryable Users => throw new NotImplementedException(); - public void UpdateAgent(AgentRecord agent) - { - throw new NotImplementedException(); - } + public IQueryable Agents => throw new NotImplementedException(); - public void CreateUser(UserRecord user) - { - throw new NotImplementedException(); - } + public IQueryable UserAgents => throw new NotImplementedException(); + + public IQueryable Conversations => throw new NotImplementedException(); + + public IQueryable RoutingItems => throw new NotImplementedException(); + + public IQueryable RoutingProfiles => throw new NotImplementedException(); - public UserRecord GetUserByEmail(string email) - { - throw new NotImplementedException(); - } public int Transaction(Action action) { @@ -78,6 +71,28 @@ public class BotSharpDbContext : Database, IBotSharpRepository } } + + + public void CreateNewConversation(Conversation conversation) + { + throw new NotImplementedException(); + } + + public List CreateRoutingItems(List routingItems) + { + throw new NotImplementedException(); + } + + public List CreateRoutingProfiles(List profiles) + { + throw new NotImplementedException(); + } + + public void CreateUser(User user) + { + throw new NotImplementedException(); + } + public void DeleteRoutingItems() { throw new NotImplementedException(); @@ -88,12 +103,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } - public List CreateRoutingItems(List routingItems) - { - throw new NotImplementedException(); - } - - public List CreateRoutingProfiles(List profiles) + public Agent GetAgent(string agentId) { throw new NotImplementedException(); } @@ -103,12 +113,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } - public AgentRecord GetAgent(string agentId) - { - throw new NotImplementedException(); - } - - public void CreateNewConversation(ConversationRecord conversation) + public Conversation GetConversation(string conversationId) { throw new NotImplementedException(); } @@ -118,27 +123,32 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } + public List GetConversations(string userId) + { + throw new NotImplementedException(); + } + + public List GetConversationStates(string conversationId) + { + throw new NotImplementedException(); + } + + public User GetUserByEmail(string email) + { + throw new NotImplementedException(); + } + + public void UpdateAgent(Agent agent) + { + throw new NotImplementedException(); + } + public void UpdateConversationDialog(string conversationId, string dialogs) { throw new NotImplementedException(); } - public List GetConversationState(string conversationId) - { - throw new NotImplementedException(); - } - - public void UpdateConversationState(string conversationId, List state) - { - throw new NotImplementedException(); - } - - public ConversationRecord GetConversation(string conversationId) - { - throw new NotImplementedException(); - } - - public List GetConversations(string userId) + 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 82c88c9a..c582c2e2 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -9,6 +9,9 @@ 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; +using BotSharp.Abstraction.Routing.Models; namespace BotSharp.Core.Repository; @@ -39,8 +42,8 @@ public class FileRepository : IBotSharpRepository }; } - private List _users; - public IQueryable User + private List _users; + public IQueryable Users { get { @@ -50,18 +53,18 @@ public class FileRepository : IBotSharpRepository } var dir = Path.Combine(_dbSettings.FileRepository, "users"); - _users = new List(); + _users = new List(); foreach (var d in Directory.GetDirectories(dir)) { var json = File.ReadAllText(Path.Combine(d, "user.json")); - _users.Add(JsonSerializer.Deserialize(json, _options)); + _users.Add(JsonSerializer.Deserialize(json, _options)); } return _users.AsQueryable(); } } - private List _agents; - public IQueryable Agent + private List _agents; + public IQueryable Agents { get { @@ -72,18 +75,18 @@ public class FileRepository : IBotSharpRepository //var agentSettings = _services.GetService(); var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); - _agents = new List(); + _agents = new List(); foreach (var d in Directory.GetDirectories(dir)) { var json = File.ReadAllText(Path.Combine(d, "agent.json")); - _agents.Add(JsonSerializer.Deserialize(json, _options)); + _agents.Add(JsonSerializer.Deserialize(json, _options)); } return _agents.AsQueryable(); } } - private List _userAgents; - public IQueryable UserAgent + private List _userAgents; + public IQueryable UserAgents { get { @@ -93,22 +96,22 @@ public class FileRepository : IBotSharpRepository } var dir = Path.Combine(_dbSettings.FileRepository, "users"); - _userAgents = new List(); + _userAgents = new List(); foreach (var d in Directory.GetDirectories(dir)) { var file = Path.Combine(d, "agents.json"); if (Directory.Exists(d) && File.Exists(file)) { var json = File.ReadAllText(file); - _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); + _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); } } return _userAgents.AsQueryable(); } } - private List _conversations; - public IQueryable Conversation + private List _conversations; + public IQueryable Conversations { get { @@ -119,22 +122,22 @@ public class FileRepository : IBotSharpRepository //var convSettings = _services.GetService(); var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir); - _conversations = new List(); + _conversations = new List(); foreach (var d in Directory.GetDirectories(dir)) { var path = Path.Combine(d, "conversation.json"); if (File.Exists(path)) { var json = File.ReadAllText(path); - _conversations.Add(JsonSerializer.Deserialize(json, _options)); + _conversations.Add(JsonSerializer.Deserialize(json, _options)); } } return _conversations.AsQueryable(); } } - private List _routingItems; - public IQueryable RoutingItem + private List _routingItems; + public IQueryable RoutingItems { get { @@ -143,21 +146,21 @@ public class FileRepository : IBotSharpRepository return _routingItems.AsQueryable(); } - _routingItems = new List(); + _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)) { - _routingItems = JsonSerializer.Deserialize>(File.ReadAllText(filePath)); + _routingItems = JsonSerializer.Deserialize>(File.ReadAllText(filePath)); } return _routingItems.AsQueryable(); } } - private List _routingProfiles; - public IQueryable RoutingProfile + private List _routingProfiles; + public IQueryable RoutingProfiles { get { @@ -166,13 +169,13 @@ public class FileRepository : IBotSharpRepository return _routingProfiles.AsQueryable(); } - _routingProfiles = new List(); + _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)) { - _routingProfiles = JsonSerializer.Deserialize>(File.ReadAllText(filePath)); + _routingProfiles = JsonSerializer.Deserialize>(File.ReadAllText(filePath)); } return _routingProfiles.AsQueryable(); @@ -181,26 +184,25 @@ public class FileRepository : IBotSharpRepository public void Add(object entity) { - _conversations = Conversation.ToList(); - if (entity is ConversationRecord conversation) + if (entity is Conversation conversation) { _conversations.Add(conversation); - _changedTableNames.Add(nameof(ConversationRecord)); + _changedTableNames.Add(nameof(Conversation)); } - else if (entity is AgentRecord agent) + else if (entity is Agent agent) { _agents.Add(agent); - _changedTableNames.Add(nameof(AgentRecord)); + _changedTableNames.Add(nameof(Agent)); } - else if (entity is UserRecord user) + else if (entity is User user) { _users.Add(user); - _changedTableNames.Add(nameof(UserRecord)); + _changedTableNames.Add(nameof(User)); } - else if (entity is UserAgentRecord userAgent) + else if (entity is UserAgent userAgent) { _userAgents.Add(userAgent); - _changedTableNames.Add(nameof(UserAgentRecord)); + _changedTableNames.Add(nameof(UserAgent)); } } @@ -213,7 +215,7 @@ public class FileRepository : IBotSharpRepository // Persist to disk foreach (var table in _changedTableNames) { - if (table == nameof(ConversationRecord)) + if (table == nameof(Conversation)) { //var convSettings = _services.GetService(); @@ -230,7 +232,7 @@ public class FileRepository : IBotSharpRepository File.WriteAllText(path, JsonSerializer.Serialize(conversation, _options)); } } - else if (table == nameof(AgentRecord)) + else if (table == nameof(Agent)) { //var agentSettings = _services.GetService(); @@ -247,7 +249,7 @@ public class FileRepository : IBotSharpRepository File.WriteAllText(path, JsonSerializer.Serialize(agent, _options)); } } - else if (table == nameof(UserRecord)) + else if (table == nameof(User)) { foreach (var user in _users) { @@ -262,7 +264,7 @@ public class FileRepository : IBotSharpRepository File.WriteAllText(path, JsonSerializer.Serialize(user, _options)); } } - else if (table == nameof(UserAgentRecord)) + else if (table == nameof(UserAgent)) { _userAgents.GroupBy(x => x.UserId) .Select(x => x.Key).ToList() @@ -282,12 +284,12 @@ public class FileRepository : IBotSharpRepository return _changedTableNames.Count; } - public UserRecord GetUserByEmail(string email) + public User GetUserByEmail(string email) { - return User.FirstOrDefault(x => x.Email == email); + return Users.FirstOrDefault(x => x.Email == email); } - public void CreateUser(UserRecord user) + public void CreateUser(User user) { var userId = Guid.NewGuid().ToString(); var dir = Path.Combine(_dbSettings.FileRepository, "users", userId); @@ -299,7 +301,7 @@ public class FileRepository : IBotSharpRepository File.WriteAllText(path, JsonSerializer.Serialize(user, _options)); } - public void UpdateAgent(AgentRecord agent) + public void UpdateAgent(Agent agent) { if (agent == null) return; @@ -348,12 +350,12 @@ public class FileRepository : IBotSharpRepository throw new NotImplementedException(); } - public List CreateRoutingItems(List routingItems) + public List CreateRoutingItems(List routingItems) { throw new NotImplementedException(); } - public List CreateRoutingProfiles(List profiles) + public List CreateRoutingProfiles(List profiles) { throw new NotImplementedException(); } @@ -372,13 +374,13 @@ public class FileRepository : IBotSharpRepository return responses; } - public AgentRecord GetAgent(string agentId) + public Agent GetAgent(string agentId) { var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); foreach (var dir in Directory.GetDirectories(agentDir)) { var json = File.ReadAllText(Path.Combine(dir, "agent.json")); - var record = JsonSerializer.Deserialize(json, _options); + var record = JsonSerializer.Deserialize(json, _options); if (record != null && record.Id == agentId) { var instruction = FetchInstruction(dir); @@ -409,7 +411,7 @@ public class FileRepository : IBotSharpRepository return functions; } - public void CreateNewConversation(ConversationRecord conversation) + public void CreateNewConversation(Conversation conversation) { var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir, conversation.Id); if (!Directory.Exists(dir)) @@ -466,7 +468,7 @@ public class FileRepository : IBotSharpRepository return; } - public List GetConversationState(string conversationId) + public List GetConversationStates(string conversationId) { var curStates = new List(); var convDir = FindConversationDirectory(conversationId); @@ -497,7 +499,7 @@ public class FileRepository : IBotSharpRepository if (!File.Exists(path)) continue; var json = File.ReadAllText(path); - var conv = JsonSerializer.Deserialize(json, _options); + var conv = JsonSerializer.Deserialize(json, _options); if (conv != null && conv.Id == conversationId) { return d; @@ -507,7 +509,7 @@ public class FileRepository : IBotSharpRepository return null; } - public void UpdateConversationState(string conversationId, List state) + public void UpdateConversationStates(string conversationId, List states) { var localStates = new List(); var convDir = FindConversationDirectory(conversationId); @@ -516,7 +518,7 @@ public class FileRepository : IBotSharpRepository var stateDir = Path.Combine(convDir, "state.dict"); if (File.Exists(stateDir)) { - foreach (var data in state) + foreach (var data in states) { localStates.Add($"{data.Key}={data.Value}"); } @@ -525,13 +527,13 @@ public class FileRepository : IBotSharpRepository } } - public ConversationRecord GetConversation(string conversationId) + public Conversation GetConversation(string conversationId) { var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { var convFile = Path.Combine(convDir, "conversation.json"); - var record = JsonSerializer.Deserialize(convFile); + var record = JsonSerializer.Deserialize(convFile); var dialogFile = Path.Combine(convDir, "dialogs.txt"); if (record != null && File.Exists(dialogFile)) @@ -543,7 +545,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(); + //record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); // to do } return record; @@ -552,9 +554,9 @@ public class FileRepository : IBotSharpRepository return null; } - public List GetConversations(string userId) + public List GetConversations(string userId) { - var records = new List(); + var records = new List(); var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir); foreach (var d in Directory.GetDirectories(dir)) @@ -563,7 +565,7 @@ public class FileRepository : IBotSharpRepository if (!File.Exists(path)) continue; var json = File.ReadAllText(path); - var record = JsonSerializer.Deserialize(json, _options); + var record = JsonSerializer.Deserialize(json, _options); if (record != null && record.UserId == userId) { var dialogFile = Path.Combine(d, "dialogs.txt"); @@ -576,7 +578,7 @@ public class FileRepository : IBotSharpRepository if (File.Exists(stateFile)) { var states = File.ReadLines(stateFile); - record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); + //record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); // to do } records.Add(record); diff --git a/src/Infrastructure/BotSharp.Core/Routing/Router.cs b/src/Infrastructure/BotSharp.Core/Routing/Router.cs index 779f44be..fb4c67bb 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Router.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Router.cs @@ -33,8 +33,8 @@ public class Router : IAgentRouting { var db = _services.GetRequiredService(); - var records = db.RoutingItem.Select(x => x.ToRoutingItem()).ToArray(); - var profiles = db.RoutingProfile.ToList(); + var records = db.RoutingItems.ToArray(); + var profiles = db.RoutingProfiles.ToList(); if (!profiles.IsNullOrEmpty()) { diff --git a/src/Infrastructure/BotSharp.Core/Routing/Services/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/Services/RoutingService.cs index 6087e1c9..70a74a87 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Services/RoutingService.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Services/RoutingService.cs @@ -16,17 +16,17 @@ public class RoutingService : IRoutingService public async Task> CreateRoutingItems(List routingItems) { var db = _services.GetRequiredService(); - var items = routingItems?.Select(x => RoutingItemRecord.FromRoutingItem(x))?.ToList() ?? new List(); + var items = routingItems?.ToList() ?? new List(); var savedItems = db.CreateRoutingItems(items); - return await Task.FromResult(savedItems.Select(x => x.ToRoutingItem()).ToList()); + return await Task.FromResult(savedItems.ToList()); } public async Task> CreateRoutingProfiles(List routingProfiles) { var db = _services.GetRequiredService(); - var profiles = routingProfiles?.Select(x => RoutingProfileRecord.FromRoutingProfile(x))?.ToList() ?? new List(); + var profiles = routingProfiles?.ToList() ?? new List(); var savedProfiles = db.CreateRoutingProfiles(profiles); - return await Task.FromResult(savedProfiles.Select(x => x.ToRoutingProfile()).ToList()); + return await Task.FromResult(savedProfiles.ToList()); } public async Task DeleteRoutingItems() diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index d81279ca..a34a1a8a 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -25,17 +25,17 @@ public class UserService : IUserService var record = db.GetUserByEmail(user.Email); if (record != null) { - return record.ToUser(); + return record; } - record = UserRecord.FromUser(user); + record = user; record.Email = user.Email.ToLower(); record.Salt = Guid.NewGuid().ToString("N"); record.Password = Utilities.HashText(user.Password, record.Salt); record.ExternalId = _user.Id; db.CreateUser(record); - return record.ToUser(); + return record; } public async Task GetToken(string authorization) @@ -44,7 +44,7 @@ public class UserService : IUserService var (userEmail, password) = base64.SplitAsTuple(":"); var db = _services.GetRequiredService(); - var record = db.User.FirstOrDefault(x => x.Email == userEmail); + var record = db.Users.FirstOrDefault(x => x.Email == userEmail); if (record == null) { return default; @@ -66,7 +66,7 @@ public class UserService : IUserService }; } - private string GenerateJwtToken(UserRecord user) + private string GenerateJwtToken(User user) { var config = _services.GetRequiredService(); var issuer = config["Jwt:Issuer"]; @@ -97,7 +97,7 @@ public class UserService : IUserService public async Task GetMyProfile() { var db = _services.GetRequiredService(); - var user = (from u in db.User + var user = (from u in db.Users where u.ExternalId == _user.Id select new User { diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/RoutingController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/RoutingController.cs index 3253d052..67f7cb98 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/RoutingController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/RoutingController.cs @@ -2,7 +2,6 @@ using BotSharp.Abstraction.ApiAdapters; using BotSharp.Abstraction.Routing; using BotSharp.Abstraction.Routing.Models; using BotSharp.OpenAPI.ViewModels.Routing; -using NetTopologySuite.Index.HPRtree; namespace BotSharp.OpenAPI.Controllers; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingItemViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingItemViewModel.cs index a91169d8..93be7d6e 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingItemViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingItemViewModel.cs @@ -4,6 +4,7 @@ namespace BotSharp.OpenAPI.ViewModels.Routing; public class RoutingItemViewModel { + public string Id { get; set; } public string AgentId { get; set; } public string Name { get; set; } public string Description { get; set; } @@ -15,6 +16,7 @@ public class RoutingItemViewModel { return new RoutingItemViewModel { + Id = routingItem.Id, AgentId = routingItem.AgentId, Name = routingItem.Name, Description = routingItem.Description, diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileCreationModel.cs index 4ea86c9c..cd06fcb0 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileCreationModel.cs @@ -5,7 +5,7 @@ namespace BotSharp.OpenAPI.ViewModels.Routing; public class RoutingProfileCreationModel { public string Name { get; set; } - public string[] AgentIds { get; set; } + public List AgentIds { get; set; } public RoutingProfile ToRoutingProfile() { diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileViewModel.cs index 4f8f7237..b20b5377 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Routing/RoutingProfileViewModel.cs @@ -4,13 +4,15 @@ namespace BotSharp.OpenAPI.ViewModels.Routing; public class RoutingProfileViewModel { + public string Id { get; set; } public string Name { get; set; } - public string[] AgentIds { get; set; } + public List AgentIds { get; set; } public static RoutingProfileViewModel FromRoutingProfile(RoutingProfile profile) { return new RoutingProfileViewModel { + Id = profile.Id, Name = profile.Name, AgentIds = profile.AgentIds, }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs index 709b7f05..4ae900e7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs @@ -7,7 +7,7 @@ public class ConversationCollection : MongoBase public Guid AgentId { get; set; } public Guid UserId { get; set; } public string Title { get; set; } - + public List States { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDialogCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDialogCollection.cs index 889fcd49..802bf21c 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDialogCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDialogCollection.cs @@ -4,6 +4,4 @@ public class ConversationDialogCollection : MongoBase { public Guid ConversationId { get; set; } public string Dialog { get; set; } - public DateTime CreatedTime { get; set; } - public DateTime UpdatedTime { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStatesCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStatesCollection.cs deleted file mode 100644 index 20bb5851..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStatesCollection.cs +++ /dev/null @@ -1,11 +0,0 @@ -using BotSharp.Abstraction.Repositories.Models; - -namespace BotSharp.Plugin.MongoStorage.Collections; - -public class ConversationStatesCollection : MongoBase -{ - public Guid ConversationId { get; set; } - public List State { get; set; } - public DateTime CreatedTime { get; set; } - public DateTime UpdatedTime { get; set; } -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index ee0f7955..7423fed1 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -34,9 +34,6 @@ public class MongoDbContext public IMongoCollection ConversationDialogs => Database.GetCollection("OneBrainConversationDialogs"); - public IMongoCollection ConversationStates - => Database.GetCollection("OneBrainConversationStates"); - public IMongoCollection Users => Database.GetCollection("OneBrainUsers"); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index da20baa6..3aaf8a03 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -1,6 +1,8 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Repositories.Models; +using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Users.Models; using BotSharp.Plugin.MongoStorage.Collections; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -21,8 +23,8 @@ public class MongoRepository : IBotSharpRepository }; } - private List _agents; - public IQueryable Agent + private List _agents; + public IQueryable Agents { get { @@ -32,7 +34,7 @@ public class MongoRepository : IBotSharpRepository } var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List(); - _agents = agentDocs.Select(x => new AgentRecord + _agents = agentDocs.Select(x => new Agent { Id = x.Id.ToString(), Name = x.Name, @@ -49,8 +51,8 @@ public class MongoRepository : IBotSharpRepository } } - private List _users; - public IQueryable User + private List _users; + public IQueryable Users { get { @@ -60,7 +62,7 @@ public class MongoRepository : IBotSharpRepository } var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List(); - _users = userDocs.Select(x => new UserRecord + _users = userDocs.Select(x => new User { Id = x.Id.ToString(), FirstName = x.FirstName, @@ -77,8 +79,8 @@ public class MongoRepository : IBotSharpRepository } } - private List _userAgents; - public IQueryable UserAgent + private List _userAgents; + public IQueryable UserAgents { get { @@ -88,7 +90,7 @@ public class MongoRepository : IBotSharpRepository } var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List(); - _userAgents = userDocs.Select(x => new UserAgentRecord + _userAgents = userDocs.Select(x => new UserAgent { Id = x.Id.ToString(), AgentId = x.AgentId.ToString(), @@ -101,8 +103,8 @@ public class MongoRepository : IBotSharpRepository } } - private List _conversations; - public IQueryable Conversation + private List _conversations; + public IQueryable Conversations { get { @@ -111,22 +113,22 @@ public class MongoRepository : IBotSharpRepository return _conversations.AsQueryable(); } - _conversations = new List(); + _conversations = new List(); var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List(); foreach (var conv in conversationDocs) { var convId = conv.Id.ToString(); var dialog = GetConversationDialog(convId); - var states = GetConversationState(convId); - _conversations.Add(new ConversationRecord + var states = GetConversationStates(convId); + _conversations.Add(new Conversation { Id = convId, AgentId = conv.AgentId.ToString(), UserId = conv.UserId.ToString(), Title = conv.Title, Dialog = dialog, - State = states, + //State = states, to do CreatedTime = conv.CreatedTime, UpdatedTime = conv.UpdatedTime }); @@ -136,8 +138,8 @@ public class MongoRepository : IBotSharpRepository } } - private List _routingItems; - public IQueryable RoutingItem + private List _routingItems; + public IQueryable RoutingItems { get { @@ -147,7 +149,7 @@ public class MongoRepository : IBotSharpRepository } var routingItemDocs = _dc.RoutingItems?.AsQueryable()?.ToList() ?? new List(); - _routingItems = routingItemDocs.Select(x => new RoutingItemRecord + _routingItems = routingItemDocs.Select(x => new RoutingItem { Id = x.Id.ToString(), AgentId = x.AgentId.ToString(), @@ -161,8 +163,8 @@ public class MongoRepository : IBotSharpRepository } } - private List _routingProfiles; - public IQueryable RoutingProfile + private List _routingProfiles; + public IQueryable RoutingProfiles { get { @@ -172,7 +174,7 @@ public class MongoRepository : IBotSharpRepository } var routingProfilDocs = _dc.RoutingProfiles?.AsQueryable()?.ToList() ?? new List(); - _routingProfiles = routingProfilDocs.Select(x => new RoutingProfileRecord + _routingProfiles = routingProfilDocs.Select(x => new RoutingProfile { Id = x.Id.ToString(), Name = x.Name, @@ -187,25 +189,25 @@ public class MongoRepository : IBotSharpRepository List _changedTableNames = new List(); public void Add(object entity) { - if (entity is ConversationRecord conversation) + if (entity is Conversation conversation) { _conversations.Add(conversation); - _changedTableNames.Add(nameof(ConversationRecord)); + _changedTableNames.Add(nameof(Conversation)); } - else if (entity is AgentRecord agent) + else if (entity is Agent agent) { _agents.Add(agent); - _changedTableNames.Add(nameof(AgentRecord)); + _changedTableNames.Add(nameof(Agent)); } - else if (entity is UserRecord user) + else if (entity is User user) { _users.Add(user); - _changedTableNames.Add(nameof(UserRecord)); + _changedTableNames.Add(nameof(User)); } - else if (entity is UserAgentRecord userAgent) + else if (entity is UserAgent userAgent) { _userAgents.Add(userAgent); - _changedTableNames.Add(nameof(UserAgentRecord)); + _changedTableNames.Add(nameof(UserAgent)); } } @@ -216,7 +218,7 @@ public class MongoRepository : IBotSharpRepository foreach (var table in _changedTableNames) { - if (table == nameof(ConversationRecord)) + if (table == nameof(Conversation)) { var conversations = _conversations.Select(x => new ConversationCollection { @@ -240,7 +242,7 @@ public class MongoRepository : IBotSharpRepository _dc.Conversations.UpdateOne(filter, update, _options); } } - else if (table == nameof(AgentRecord)) + else if (table == nameof(Agent)) { var agents = _agents.Select(x => new AgentCollection { @@ -270,7 +272,7 @@ public class MongoRepository : IBotSharpRepository _dc.Agents.UpdateOne(filter, update, _options); } } - else if (table == nameof(UserRecord)) + else if (table == nameof(User)) { var users = _users.Select(x => new UserCollection { @@ -300,7 +302,7 @@ public class MongoRepository : IBotSharpRepository _dc.Users.UpdateOne(filter, update, _options); } } - else if (table == nameof(UserAgentRecord)) + else if (table == nameof(UserAgent)) { var userAgents = _userAgents.Select(x => new UserAgentCollection { @@ -327,10 +329,10 @@ public class MongoRepository : IBotSharpRepository return _changedTableNames.Count; } - public UserRecord GetUserByEmail(string email) + public User GetUserByEmail(string email) { - var user = User.FirstOrDefault(x => x.Email == email); - return user != null ? new UserRecord + var user = Users.FirstOrDefault(x => x.Email == email); + return user != null ? new User { Id = user.Id.ToString(), FirstName = user.FirstName, @@ -344,7 +346,7 @@ public class MongoRepository : IBotSharpRepository } : null; } - public void CreateUser(UserRecord user) + public void CreateUser(User user) { if (user == null) return; @@ -364,7 +366,7 @@ public class MongoRepository : IBotSharpRepository _dc.Users.InsertOne(userCollection); } - public void UpdateAgent(AgentRecord agent) + public void UpdateAgent(Agent agent) { if (agent == null || string.IsNullOrEmpty(agent.Id)) return; @@ -404,7 +406,7 @@ public class MongoRepository : IBotSharpRepository _dc.RoutingProfiles.DeleteMany(Builders.Filter.Empty); } - public List CreateRoutingItems(List routingItems) + public List CreateRoutingItems(List routingItems) { var collections = routingItems?.Select(x => new RoutingItemCollection { @@ -418,7 +420,7 @@ public class MongoRepository : IBotSharpRepository })?.ToList() ?? new List(); _dc.RoutingItems.InsertMany(collections); - return collections.Select(x => new RoutingItemRecord + return collections.Select(x => new RoutingItem { Id = x.Id.ToString(), AgentId = x.AgentId.ToString(), @@ -430,7 +432,7 @@ public class MongoRepository : IBotSharpRepository }).ToList(); } - public List CreateRoutingProfiles(List profiles) + public List CreateRoutingProfiles(List profiles) { var collections = profiles?.Select(x => new RoutingProfileCollection { @@ -440,7 +442,7 @@ public class MongoRepository : IBotSharpRepository })?.ToList() ?? new List(); _dc.RoutingProfiles.InsertMany(collections); - return collections.Select(x => new RoutingProfileRecord + return collections.Select(x => new RoutingProfile { Id = x.Id.ToString(), Name = x.Name, @@ -451,25 +453,25 @@ public class MongoRepository : IBotSharpRepository public List GetAgentResponses(string agentId) { var responses = new List(); - var agent = Agent.FirstOrDefault(x => x.Id == agentId); + var agent = Agents.FirstOrDefault(x => x.Id == agentId); if (agent == null) return responses; return agent.Responses; } - public AgentRecord GetAgent(string agentId) + public Agent GetAgent(string agentId) { - var foundAgent = Agent.FirstOrDefault(x => x.Id == agentId); + var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId); return foundAgent; } - public void CreateNewConversation(ConversationRecord conversation) + public void CreateNewConversation(Conversation conversation) { if (conversation == null) return; var conv = new ConversationCollection { - Id = Guid.Parse(conversation.Id), + Id = !string.IsNullOrEmpty(conversation.Id) ? Guid.Parse(conversation.Id) : Guid.NewGuid(), AgentId = Guid.Parse(conversation.AgentId), UserId = Guid.Parse(conversation.UserId), Title = conversation.Title, @@ -481,23 +483,11 @@ public class MongoRepository : IBotSharpRepository { Id = Guid.NewGuid(), ConversationId = conv.Id, - Dialog = string.Empty, - CreatedTime = DateTime.UtcNow, - UpdatedTime = DateTime.UtcNow, - }; - - var states = new ConversationStatesCollection - { - Id = Guid.NewGuid(), - ConversationId = conv.Id, - State = new List(), - CreatedTime = DateTime.UtcNow, - UpdatedTime = DateTime.UtcNow, + Dialog = string.Empty }; _dc.Conversations.InsertOne(conv); _dc.ConversationDialogs.InsertOne(dialog); - _dc.ConversationStates.InsertOne(states); } public string GetConversationDialog(string conversationId) @@ -515,75 +505,75 @@ public class MongoRepository : IBotSharpRepository { if (string.IsNullOrEmpty(conversationId)) return; - var filter = Builders.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId)); - var foundDialog = _dc.ConversationDialogs.Find(filter).FirstOrDefault(); + var filterConv = Builders.Filter.Eq(x => x.Id, Guid.Parse(conversationId)); + var foundConv = _dc.Conversations.Find(filterConv).FirstOrDefault(); + if (foundConv == null) return; + + var filterDialog = Builders.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId)); + var foundDialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault(); if (foundDialog == null) return; - var update = Builders.Update - .Set(x => x.Dialog, dialogs) - .Set(x => x.UpdatedTime, DateTime.UtcNow); + var updateDialog = Builders.Update.Set(x => x.Dialog, dialogs); + var updateConv = Builders.Update.Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.ConversationDialogs.UpdateOne(filter, update); + _dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog); + _dc.Conversations.UpdateOne(filterConv, updateConv); } - public List GetConversationState(string conversationId) + public List GetConversationStates(string conversationId) { var states = new List(); if (string.IsNullOrEmpty(conversationId)) return states; - var filter = Builders.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId)); - var foundStates = _dc.ConversationStates.Find(filter).FirstOrDefault(); - if (foundStates == null) return states; - - var savedStates = foundStates.State ?? new List(); + var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(conversationId)); + var foundConversation = _dc.Conversations.Find(filter).FirstOrDefault(); + var savedStates = foundConversation?.States ?? new List(); return savedStates; } - public void UpdateConversationState(string conversationId, List state) + public void UpdateConversationStates(string conversationId, List states) { if (string.IsNullOrEmpty(conversationId)) return; - var filter = Builders.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId)); - var foundStates = _dc.ConversationStates.Find(filter).FirstOrDefault(); - if (foundStates == null) return; + var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(conversationId)); + var foundConv = _dc.Conversations.Find(filter).FirstOrDefault(); + if (foundConv == null) return; - var update = Builders.Update - .Set(x => x.State, state) + var update = Builders.Update + .Set(x => x.States, states) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.ConversationStates.UpdateOne(filter, update); + _dc.Conversations.UpdateOne(filter, update); } - public ConversationRecord GetConversation(string conversationId) + public Conversation GetConversation(string conversationId) { if (string.IsNullOrEmpty(conversationId)) return null; - var filterById = Builders.Filter.Eq(x => x.Id, Guid.Parse(conversationId)); + var filterConv = Builders.Filter.Eq(x => x.Id, Guid.Parse(conversationId)); var filterDialog = Builders.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId)); - var filterStates = Builders.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId)); - var conv = _dc.Conversations.Find(filterById).FirstOrDefault(); + var conv = _dc.Conversations.Find(filterConv).FirstOrDefault(); var dialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault(); - var states = _dc.ConversationStates.Find(filterStates).FirstOrDefault(); if (conv == null) return null; - return new ConversationRecord + return new Conversation { Id = conv.Id.ToString(), AgentId = conv.AgentId.ToString(), UserId = conv.UserId.ToString(), Title = conv.Title, Dialog = dialog?.Dialog ?? string.Empty, - State = states?.State ?? new List(), + States = new ConversationState(conv.States ?? new List()), CreatedTime = conv.CreatedTime, UpdatedTime = conv.UpdatedTime }; } - public List GetConversations(string userId) + public List GetConversations(string userId) { - var records = new List(); + var records = new List(); if (string.IsNullOrEmpty(userId)) return records; var filterByUserId = Builders.Filter.Eq(x => x.UserId, Guid.Parse(userId)); @@ -592,16 +582,12 @@ public class MongoRepository : IBotSharpRepository foreach (var conv in conversations) { var convId = conv.Id.ToString(); - var dialog = GetConversationDialog(convId); - var states = GetConversationState(convId); - records.Add(new ConversationRecord + records.Add(new Conversation { Id = convId, AgentId = conv.AgentId.ToString(), UserId = conv.UserId.ToString(), Title = conv.Title, - Dialog = dialog, - State = states, CreatedTime = conv.CreatedTime, UpdatedTime = conv.UpdatedTime });