From d7d0af2c15fc9e6b714be733528868e9976c04de Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 28 Aug 2023 00:28:14 -0500 Subject: [PATCH] add create agent and update agent --- .../Agents/Models/Agent.cs | 5 + .../Repositories/Records/AgentRecord.cs | 14 +- .../BotSharp.Abstraction/Using.cs | 1 + .../Utilities/ListExtenstion.cs | 9 + .../Services/AgentService.CreateAgent.cs | 4 +- .../Agents/Services/AgentService.GetAgents.cs | 2 +- .../Services/AgentService.UpdateAgent.cs | 29 +- .../Users/Services/UserService.cs | 1 - .../ViewModels/Agents/AgentCreationModel.cs | 8 +- .../ViewModels/Agents/AgentUpdateModel.cs | 8 + ...ry.csproj => BotSharp.Plugin.Mongo.csproj} | 0 .../Collections/AgentCollection.cs | 25 +- .../Collections/ConversationCollection.cs | 2 +- .../Collections/UserAgentCollection.cs | 17 +- .../Collections/UserCollection.cs | 27 +- .../MongoBase.cs | 2 +- .../MongoDbContext.cs | 4 +- .../MongoRepositoryPlugin.cs | 27 +- .../Repository/MongoRepository.cs | 438 +++++++++--------- 19 files changed, 342 insertions(+), 281 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Utilities/ListExtenstion.cs rename src/Plugins/BotSharp.Plugin.MongoRepository/{BotSharp.Plugin.MongoRepository.csproj => BotSharp.Plugin.Mongo.csproj} (100%) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 1e22d6f6..1297fca4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -28,6 +28,11 @@ public class Agent /// public string Knowledges { get; set; } + /// + /// Routes + /// + public List Routes { get; set; } + public override string ToString() => $"{Name} {Id}"; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs index d473a206..c3300440 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs @@ -9,6 +9,12 @@ public class AgentRecord : RecordBase [MaxLength(512)] public string? Description { get; set; } + public string Instruction { get; set; } + + public string Functions { get; set; } + + public List Routes { get; set; } + [Required] public DateTime CreatedTime { get; set; } @@ -21,7 +27,10 @@ public class AgentRecord : RecordBase { Id = agent.Id, Name = agent.Name, - Description = agent.Description + Description = agent.Description, + Instruction = agent.Instruction, + Functions = agent.Functions, + Routes = agent.Routes, }; } @@ -32,6 +41,9 @@ public class AgentRecord : RecordBase Id = Id, Name = Name, Description = Description, + Instruction = Instruction, + Functions = Functions, + Routes = Routes, CreatedDateTime = CreatedTime, UpdatedDateTime = UpdatedTime }; diff --git a/src/Infrastructure/BotSharp.Abstraction/Using.cs b/src/Infrastructure/BotSharp.Abstraction/Using.cs index 6a73f3ff..3a545a0a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Using.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Using.cs @@ -1,6 +1,7 @@ global using System; global using System.Collections.Generic; global using System.Text; +global using System.Linq; global using System.Threading.Tasks; global using System.ComponentModel.DataAnnotations; global using BotSharp.Abstraction.Agents.Models; \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/ListExtenstion.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/ListExtenstion.cs new file mode 100644 index 00000000..df68be64 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/ListExtenstion.cs @@ -0,0 +1,9 @@ +namespace BotSharp.Abstraction.Utilities; + +public static class ListExtenstion +{ + public static bool IsEmpty(this IEnumerable strList) + { + return strList == null || !strList.Any(); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index adeefaa3..bdccf367 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Records; +using MongoDB.Bson; namespace BotSharp.Core.Agents.Services; @@ -22,13 +23,12 @@ public partial class AgentService } record = AgentRecord.FromAgent(agent); - record.Id = Guid.NewGuid().ToString(); + record.Id = ObjectId.GenerateNewId().ToString(); record.CreatedTime = DateTime.UtcNow; record.UpdatedTime = DateTime.UtcNow; var userAgentRecord = new UserAgentRecord { - Id = Guid.NewGuid().ToString(), UserId = _user.Id, AgentId = record.Id, CreatedTime = DateTime.UtcNow, diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 9a4030b6..02e96f05 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -44,7 +44,7 @@ public partial class AgentService var functionsFile = Path.Combine(dir, "functions.json"); if (File.Exists(functionsFile)) { - profile.Functions = File.ReadAllText(functionsFile); + //profile.Functions = File.ReadAllText(functionsFile); } return profile; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index c37cd851..cf1f32e8 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -17,19 +17,34 @@ public partial class AgentService join u in db.User on ua.UserId equals u.Id where (ua.UserId == _user.Id || u.ExternalId == _user.Id) && a.Id == agent.Id - select a).First(); + select a).FirstOrDefault(); + + if (record == null) return; record.Name = agent.Name; - record.Description = agent.Description; + + if (!string.IsNullOrEmpty(agent.Description)) + record.Description = agent.Description; + + if (!string.IsNullOrEmpty(agent.Instruction)) + record.Instruction = agent.Instruction; + + if (!string.IsNullOrEmpty(agent.Functions)) + record.Functions = agent.Functions; + + if (!agent.Routes.IsEmpty()) + record.Routes = agent.Routes; + record.UpdatedTime = DateTime.UtcNow; + db.Add(record); }); // Save instruction to file - var dir = GetAgentDataDir(agent.Id); - var instructionFile = Path.Combine(dir, "instruction.txt"); - File.WriteAllText(instructionFile, agent.Instruction); + //var dir = GetAgentDataDir(agent.Id); + //var instructionFile = Path.Combine(dir, "instruction.txt"); + //File.WriteAllText(instructionFile, agent.Instruction); - var samplesFile = Path.Combine(dir, "samples.txt"); - File.WriteAllText(samplesFile, agent.Samples); + //var samplesFile = Path.Combine(dir, "samples.txt"); + //File.WriteAllText(samplesFile, agent.Samples); } } diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 7a3f26fe..3448eda4 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -29,7 +29,6 @@ public class UserService : IUserService } record = UserRecord.FromUser(user); - //record.Id = Guid.NewGuid().ToString(); record.Email = user.Email.ToLower(); record.Salt = Guid.NewGuid().ToString("N"); record.Password = Utilities.HashText(user.Password, record.Salt); diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index f8fead86..1203c4db 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -6,13 +6,19 @@ public class AgentCreationModel { public string Name { get; set; } public string Description { get; set; } + public string Instruction { get; set; } + public string Functions { get; set; } + public List Routes { get; set; } public Agent ToAgent() { return new Agent { Name = Name, - Description = Description + Description = Description, + Instruction = Instruction, + Functions = Functions, + Routes = Routes }; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index 1a87a2d7..263875e7 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -22,6 +22,11 @@ public class AgentUpdateModel /// public string? Functions { get; set; } + /// + /// Routes + /// + public List? Routes { get; set; } + public Agent ToAgent() { var agent = new Agent @@ -41,6 +46,9 @@ public class AgentUpdateModel if (Functions != null) agent.Functions = Functions; + if (!Routes.IsEmpty()) + agent.Routes = Routes; + return agent; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/BotSharp.Plugin.MongoRepository.csproj b/src/Plugins/BotSharp.Plugin.MongoRepository/BotSharp.Plugin.Mongo.csproj similarity index 100% rename from src/Plugins/BotSharp.Plugin.MongoRepository/BotSharp.Plugin.MongoRepository.csproj rename to src/Plugins/BotSharp.Plugin.MongoRepository/BotSharp.Plugin.Mongo.csproj diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/AgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/AgentCollection.cs index 7187faf9..614c43d7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/AgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/AgentCollection.cs @@ -1,14 +1,13 @@ -namespace BotSharp.Plugin.MongoRepository.Collections -{ - public class AgentCollection : MongoBase - { - public string Name { get; set; } - public string Description { get; set; } - public List Functions { get; set; } - public string Instruction { get; set; } - public List Routes { get; set; } +namespace BotSharp.Plugin.Mongo.Collections; - public DateTime CreatedTime { get; set; } - public DateTime UpdatedTime { get; set; } - } -} +public class AgentCollection : MongoBase +{ + public string Name { get; set; } + public string Description { get; set; } + public string Functions { get; set; } + public string Instruction { get; set; } + public List Routes { get; set; } + + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } +} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/ConversationCollection.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/ConversationCollection.cs index 6140f881..63bc9dc4 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/ConversationCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/ConversationCollection.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Plugin.MongoRepository.Collections; +namespace BotSharp.Plugin.Mongo.Collections; public class ConversationCollection : MongoBase { diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserAgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserAgentCollection.cs index 050b5fcf..79822401 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserAgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserAgentCollection.cs @@ -1,11 +1,10 @@ -namespace BotSharp.Plugin.MongoRepository.Collections -{ - public class UserAgentCollection : MongoBase - { - public string UserId { get; set; } - public string AgentId { get; set; } +namespace BotSharp.Plugin.Mongo.Collections; - public DateTime CreatedTime { get; set; } - public DateTime UpdatedTime { get; set; } - } +public class UserAgentCollection : MongoBase +{ + public string UserId { get; set; } + public string AgentId { get; set; } + + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserCollection.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserCollection.cs index a99aed63..59ec8a0b 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/Collections/UserCollection.cs @@ -1,15 +1,14 @@ -namespace BotSharp.Plugin.MongoRepository.Collections -{ - public class UserCollection : MongoBase - { - public string FirstName { get; set; } - public string LastName { get; set; } - public string Email { get; set; } - public string Salt { get; set; } - public string Password { get; set; } - public string? ExternalId { get; set; } +namespace BotSharp.Plugin.Mongo.Collections; - public DateTime CreatedTime { get; set; } - public DateTime UpdatedTime { get; set; } - } -} +public class UserCollection : MongoBase +{ + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public string Salt { get; set; } + public string Password { get; set; } + public string? ExternalId { get; set; } + + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } +} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/MongoBase.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/MongoBase.cs index 98a8f041..995401e8 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/MongoBase.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/MongoBase.cs @@ -1,7 +1,7 @@ using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.IdGenerators; -namespace BotSharp.Plugin.MongoRepository; +namespace BotSharp.Plugin.Mongo; [BsonIgnoreExtraElements(Inherited = true)] public class MongoBase diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/MongoDbContext.cs index 23297bd9..dee02eeb 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/MongoDbContext.cs @@ -1,6 +1,6 @@ -using BotSharp.Plugin.MongoRepository.Collections; +using BotSharp.Plugin.Mongo.Collections; -namespace BotSharp.Core.Repository; +namespace BotSharp.Plugin.Mongo; public class MongoDbContext { diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/MongoRepositoryPlugin.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/MongoRepositoryPlugin.cs index 73517923..75a7cbc9 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/MongoRepositoryPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/MongoRepositoryPlugin.cs @@ -1,16 +1,17 @@ -namespace BotSharp.Core.Repository -{ - public class MongoRepositoryPlugin : IBotSharpPlugin - { - public void RegisterDI(IServiceCollection services, IConfiguration config) - { - services.AddSingleton((IServiceProvider x) => - { - var databaseSettings = x.GetRequiredService(); - return new MongoDbContext(databaseSettings.MongoDb); - }); +using BotSharp.Plugin.Mongo.Repository; - services.AddScoped(); - } +namespace BotSharp.Plugin.Mongo; + +public class MongoRepositoryPlugin : IBotSharpPlugin +{ + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddSingleton((IServiceProvider x) => + { + var databaseSettings = x.GetRequiredService(); + return new MongoDbContext(databaseSettings.MongoDb); + }); + + services.AddScoped(); } } diff --git a/src/Plugins/BotSharp.Plugin.MongoRepository/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoRepository/Repository/MongoRepository.cs index 1cc16c52..4fe5ce96 100644 --- a/src/Plugins/BotSharp.Plugin.MongoRepository/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoRepository/Repository/MongoRepository.cs @@ -1,256 +1,264 @@ -using BotSharp.Plugin.MongoRepository.Collections; +using BotSharp.Plugin.Mongo.Collections; -namespace BotSharp.Core.Repository +namespace BotSharp.Plugin.Mongo.Repository; + +public class MongoRepository : IBotSharpRepository { - public class MongoRepository : IBotSharpRepository + private readonly MongoDbContext _dc; + private readonly IServiceProvider _services; + private UpdateOptions _options; + + public MongoRepository(MongoDbContext dc, IServiceProvider services) { - private readonly MongoDbContext _dc; - private readonly IServiceProvider _services; - private UpdateOptions _options; + _dc = dc; + _services = services; - public MongoRepository(MongoDbContext dc, IServiceProvider services) + _options = new UpdateOptions { - _dc = dc; - _services = services; + IsUpsert = true, + }; + } - _options = new UpdateOptions + private List _agents; + public IQueryable Agent + { + get + { + if (_agents != null) { - IsUpsert = true, - }; + return _agents.AsQueryable(); + } + + var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List(); + _agents = agentDocs.Select(x => new AgentRecord + { + Id = x.Id?.ToString(), + Name = x.Name, + Description = x.Description, + Instruction = x.Instruction, + Functions = x.Functions, + Routes = x.Routes, + CreatedTime = x.CreatedTime, + UpdatedTime = x.UpdatedTime + }).ToList(); + + return _agents.AsQueryable(); } + } - private List _agents; - public IQueryable Agent + private List _users; + public IQueryable User + { + get { - get + if (_users != null) { - if (_agents != null) - { - return _agents.AsQueryable(); - } + return _users.AsQueryable(); + } - var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List(); - _agents = agentDocs.Select(x => new AgentRecord - { - Id = x.Id?.ToString(), - Name = x.Name, - Description = x.Description, + var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List(); + _users = userDocs.Select(x => new UserRecord + { + Id = x.Id?.ToString(), + FirstName = x.FirstName, + LastName = x.LastName, + Email = x.Email, + Password = x.Password, + Salt = x.Salt, + ExternalId = x.ExternalId, + CreatedTime = x.CreatedTime, + UpdatedTime = x.UpdatedTime + }).ToList(); + + return _users.AsQueryable(); + } + } + + private List _userAgents; + public IQueryable UserAgent + { + get + { + if (_userAgents != null && _userAgents.Count > 0) + { + return _userAgents.AsQueryable(); + } + + var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List(); + _userAgents = userDocs.Select(x => new UserAgentRecord + { + Id = x.Id?.ToString(), + AgentId = x.AgentId, + UserId = x.UserId, + CreatedTime = x.CreatedTime, + UpdatedTime = x.UpdatedTime + }).ToList(); + + return _userAgents.AsQueryable(); + } + } + + private List _conversations; + public IQueryable Conversation + { + get + { + if (_conversations != null) + { + return _conversations.AsQueryable(); + } + + var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List(); + _conversations = conversationDocs.Select(x => new ConversationRecord + { + Id = x.Id?.ToString(), + AgentId = x.AgentId, + UserId = x.UserId, + Title = x.Title, + CreatedTime = x.CreatedTime, + UpdatedTime = x.UpdatedTime + }).ToList(); + + return _conversations.AsQueryable(); + } + } + + List _changedTableNames = new List(); + public void Add(object entity) + { + if (entity is ConversationRecord conversation) + { + _conversations.Add(conversation); + _changedTableNames.Add(nameof(ConversationRecord)); + } + else if (entity is AgentRecord agent) + { + _agents.Add(agent); + _changedTableNames.Add(nameof(AgentRecord)); + } + else if (entity is UserRecord user) + { + _users.Add(user); + _changedTableNames.Add(nameof(UserRecord)); + } + else if (entity is UserAgentRecord userAgent) + { + _userAgents.Add(userAgent); + _changedTableNames.Add(nameof(UserAgentRecord)); + } + } + + public int Transaction(Action action) + { + _changedTableNames.Clear(); + action(); + + foreach (var table in _changedTableNames) + { + if (table == nameof(ConversationRecord)) + { + var conversations = _conversations.Select(x => new ConversationCollection + { + Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), + AgentId = x.AgentId, + UserId = x.UserId, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); - return _agents.AsQueryable(); - } - } - - private List _users; - public IQueryable User - { - get - { - if (_users != null) + foreach (var conversation in conversations) { - return _users.AsQueryable(); + var filter = Builders.Filter.Eq(x => x.Id, conversation.Id); + var update = Builders.Update + .Set(x => x.AgentId, conversation.AgentId) + .Set(x => x.UserId, conversation.UserId) + .Set(x => x.CreatedTime, conversation.CreatedTime) + .Set(x => x.UpdatedTime, conversation.UpdatedTime); + _dc.Conversations.UpdateOne(filter, update, _options); } - - var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List(); - _users = userDocs.Select(x => new UserRecord + } + else if (table == nameof(AgentRecord)) + { + var agents = _agents.Select(x => new AgentCollection { - Id = x.Id?.ToString(), + Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), + Name = x.Name, + Description = x.Description, + Instruction = x.Instruction, + Functions = x.Functions, + Routes = x.Routes, + CreatedTime = x.CreatedTime, + UpdatedTime = x.UpdatedTime + }).ToList(); + + foreach (var agent in agents) + { + var filter = Builders.Filter.Eq(x => x.Id, agent.Id); + var update = Builders.Update + .Set(x => x.Name, agent.Name) + .Set(x => x.Description, agent.Description) + .Set(x => x.Instruction, agent.Instruction) + .Set(x => x.Functions, agent.Functions) + .Set(x => x.Routes, agent.Routes) + .Set(x => x.CreatedTime, agent.CreatedTime) + .Set(x => x.UpdatedTime, agent.UpdatedTime); + _dc.Agents.UpdateOne(filter, update, _options); + } + } + else if (table == nameof(UserRecord)) + { + var users = _users.Select(x => new UserCollection + { + Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), FirstName = x.FirstName, LastName = x.LastName, - Email = x.Email, - Password = x.Password, Salt = x.Salt, + Password = x.Password, + Email = x.Email, ExternalId = x.ExternalId, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); - return _users.AsQueryable(); - } - } - - private List _userAgents; - public IQueryable UserAgent - { - get - { - if (_userAgents != null && _userAgents.Count > 0) + foreach (var user in users) { - return _userAgents.AsQueryable(); + var filter = Builders.Filter.Eq(x => x.Id, user.Id); + var update = Builders.Update + .Set(x => x.FirstName, user.FirstName) + .Set(x => x.LastName, user.LastName) + .Set(x => x.Email, user.Email) + .Set(x => x.Salt, user.Salt) + .Set(x => x.Password, user.Password) + .Set(x => x.ExternalId, user.ExternalId) + .Set(x => x.CreatedTime, user.CreatedTime) + .Set(x => x.UpdatedTime, user.UpdatedTime); + _dc.Users.UpdateOne(filter, update, _options); } - - var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List(); - _userAgents = userDocs.Select(x => new UserAgentRecord + } + else if (table == nameof(UserAgentRecord)) + { + var userAgents = _userAgents.Select(x => new UserAgentCollection { - Id = x.Id?.ToString(), + Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), AgentId = x.AgentId, UserId = x.UserId, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); - return _userAgents.AsQueryable(); + foreach (var userAgent in userAgents) + { + var filter = Builders.Filter.Eq(x => x.Id, userAgent.Id); + var update = Builders.Update + .Set(x => x.AgentId, userAgent.AgentId) + .Set(x => x.UserId, userAgent.UserId) + .Set(x => x.CreatedTime, userAgent.CreatedTime) + .Set(x => x.UpdatedTime, userAgent.UpdatedTime); + _dc.UserAgents.UpdateOne(filter, update, _options); + } } } - private List _conversations; - public IQueryable Conversation - { - get - { - if (_conversations != null) - { - return _conversations.AsQueryable(); - } - - var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List(); - _conversations = conversationDocs.Select(x => new ConversationRecord - { - Id = x.Id?.ToString(), - AgentId = x.AgentId, - UserId = x.UserId, - Title = x.Title, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime - }).ToList(); - - return _conversations.AsQueryable(); - } - } - - List _changedTableNames = new List(); - public void Add(object entity) - { - if (entity is ConversationRecord conversation) - { - _conversations.Add(conversation); - _changedTableNames.Add(nameof(ConversationRecord)); - } - else if (entity is AgentRecord agent) - { - _agents.Add(agent); - _changedTableNames.Add(nameof(AgentRecord)); - } - else if (entity is UserRecord user) - { - _users.Add(user); - _changedTableNames.Add(nameof(UserRecord)); - } - else if (entity is UserAgentRecord userAgent) - { - _userAgents.Add(userAgent); - _changedTableNames.Add(nameof(UserAgentRecord)); - } - } - - public int Transaction(Action action) - { - _changedTableNames.Clear(); - action(); - - foreach (var table in _changedTableNames) - { - if (table == nameof(ConversationRecord)) - { - var conversations = _conversations.Select(x => new ConversationCollection - { - Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), - AgentId = x.AgentId, - UserId = x.UserId, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime - }).ToList(); - - foreach (var conversation in conversations) - { - var filter = Builders.Filter.Eq(x => x.Id, conversation.Id); - var update = Builders.Update - .Set(x => x.AgentId, conversation.AgentId) - .Set(x => x.UserId, conversation.UserId) - .Set(x => x.CreatedTime, conversation.CreatedTime) - .Set(x => x.UpdatedTime, conversation.UpdatedTime); - _dc.Conversations.UpdateOne(filter, update, _options); - } - } - else if (table == nameof(AgentRecord)) - { - var agents = _agents.Select(x => new AgentCollection - { - Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), - Name = x.Name, - Description = x.Description, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime - }).ToList(); - - foreach (var agent in agents) - { - var filter = Builders.Filter.Eq(x => x.Id, agent.Id); - var update = Builders.Update - .Set(x => x.Name, agent.Name) - .Set(x => x.Description, agent.Description) - .Set(x => x.CreatedTime, agent.CreatedTime) - .Set(x => x.UpdatedTime, agent.UpdatedTime); - _dc.Agents.UpdateOne(filter, update, _options); - } - } - else if (table == nameof(UserRecord)) - { - var users = _users.Select(x => new UserCollection - { - Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), - FirstName = x.FirstName, - LastName = x.LastName, - Salt = x.Salt, - Password = x.Password, - Email = x.Email, - ExternalId = x.ExternalId, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime - }).ToList(); - - foreach (var user in users) - { - var filter = Builders.Filter.Eq(x => x.Id, user.Id); - var update = Builders.Update - .Set(x => x.FirstName, user.FirstName) - .Set(x => x.LastName, user.LastName) - .Set(x => x.Email, user.Email) - .Set(x => x.Salt, user.Salt) - .Set(x => x.Password, user.Password) - .Set(x => x.ExternalId, user.ExternalId) - .Set(x => x.CreatedTime, user.CreatedTime) - .Set(x => x.UpdatedTime, user.UpdatedTime); - _dc.Users.UpdateOne(filter, update, _options); - } - } - else if (table == nameof(UserAgentRecord)) - { - var userAgents = _userAgents.Select(x => new UserAgentCollection - { - Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()), - AgentId = x.AgentId, - UserId = x.UserId, - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime - }).ToList(); - - foreach (var userAgent in userAgents) - { - var filter = Builders.Filter.Eq(x => x.Id, userAgent.Id); - var update = Builders.Update - .Set(x => x.AgentId, userAgent.AgentId) - .Set(x => x.UserId, userAgent.UserId) - .Set(x => x.CreatedTime, userAgent.CreatedTime) - .Set(x => x.UpdatedTime, userAgent.UpdatedTime); - _dc.UserAgents.UpdateOne(filter, update, _options); - } - } - } - - return _changedTableNames.Count; - } + return _changedTableNames.Count; } }