From 4a219798d98a0bfd4bb9aef4e9468969e42be838 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 18 Sep 2023 18:14:24 -0500 Subject: [PATCH 1/4] sync agent structure changes --- .../Agents/Enums/AgentField.cs | 4 + .../Agents/Models/Agent.cs | 32 +++++++- .../Routing/Models/RoutingRule.cs | 5 ++ .../Services/AgentService.CreateAgent.cs | 4 + .../Services/AgentService.UpdateAgent.cs | 10 +++ .../ViewModels/Agents/AgentCreationModel.cs | 11 ++- .../ViewModels/Agents/AgentUpdateModel.cs | 19 +++++ .../Collections/AgentCollection.cs | 5 ++ .../Models/RoutingRuleElement.cs | 37 +++++++++ .../Repository/MongoRepository.cs | 79 +++++++++++++++++++ 10 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 5f56004a..d82e8602 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -6,6 +6,10 @@ public enum AgentField Name, Description, IsPublic, + Disabled, + AllowRouting, + Profiles, + RoutingRules, Instruction, Function, Template, diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 8b968035..d3551296 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -6,7 +6,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; } = string.Empty; public DateTime CreatedDateTime { get; set; } public DateTime UpdatedDateTime { get; set; } @@ -81,6 +81,10 @@ public class Agent Samples = agent.Samples, Knowledges = agent.Knowledges, IsPublic = agent.IsPublic, + Disabled = agent.Disabled, + AllowRouting = agent.AllowRouting, + Profiles = agent.Profiles, + RoutingRules = agent.RoutingRules, CreatedDateTime = agent.CreatedDateTime, UpdatedDateTime = agent.UpdatedDateTime, }; @@ -94,7 +98,7 @@ public class Agent public Agent SetTemplates(List templates) { - Templates = templates; + Templates = templates ?? new List(); return this; } @@ -133,4 +137,28 @@ public class Agent IsPublic = isPublic; return this; } + + public Agent SetDisabled(bool disabled) + { + Disabled = disabled; + return this; + } + + public Agent SetAllowRouting(bool allowRouting) + { + AllowRouting = allowRouting; + return this; + } + + public Agent SetProfiles(List profiles) + { + Profiles = profiles ?? new List(); + return this; + } + + public Agent SetRoutingRules(List rules) + { + RoutingRules = rules ?? new List(); + return this; + } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs index 40102d4a..ca24ad6b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs @@ -18,4 +18,9 @@ public class RoutingRule { return $"{AgentName} {Field}"; } + + public RoutingRule() + { + + } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 567a3dac..d42e1da0 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -35,6 +35,10 @@ public partial class AgentService .SetName(foundAgent.Name) .SetDescription(foundAgent.Description) .SetIsPublic(foundAgent.IsPublic) + .SetDisabled(foundAgent.Disabled) + .SetAllowRouting(foundAgent.AllowRouting) + .SetProfiles(foundAgent.Profiles) + .SetRoutingRules(foundAgent.RoutingRules) .SetInstruction(foundAgent.Instruction) .SetTemplates(foundAgent.Templates) .SetFunctions(foundAgent.Functions) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 7f6b619b..f91fdef0 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; +using BotSharp.Abstraction.Routing.Models; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -15,6 +16,11 @@ public partial class AgentService record.Name = agent.Name ?? string.Empty; record.Description = agent.Description ?? string.Empty; + record.IsPublic = agent.IsPublic; + record.Disabled = agent.Disabled; + record.AllowRouting = agent.AllowRouting; + record.Profiles = agent.Profiles ?? new List(); + record.RoutingRules = agent.RoutingRules ?? new List(); record.Instruction = agent.Instruction ?? string.Empty; record.Functions = agent.Functions ?? new List(); record.Templates = agent.Templates ?? new List(); @@ -53,6 +59,10 @@ public partial class AgentService .SetName(foundAgent.Name) .SetDescription(foundAgent.Description) .SetIsPublic(foundAgent.IsPublic) + .SetDisabled(foundAgent.Disabled) + .SetAllowRouting(foundAgent.AllowRouting) + .SetProfiles(foundAgent.Profiles) + .SetRoutingRules(foundAgent.RoutingRules) .SetInstruction(foundAgent.Instruction) .SetTemplates(foundAgent.Templates) .SetFunctions(foundAgent.Functions) diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index a8e233fd..277f2d8a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Routing.Models; namespace BotSharp.OpenAPI.ViewModels.Agents; @@ -11,6 +12,10 @@ public class AgentCreationModel public List Functions { get; set; } public List Responses { get; set; } public bool IsPublic { get; set; } + public bool AllowRouting { get; set; } + public bool Disabled { get; set; } + public List Profiles { get; set; } + public List RoutingRules { get; set; } public Agent ToAgent() { @@ -22,7 +27,11 @@ public class AgentCreationModel Templates = Templates, Functions = Functions, Responses = Responses, - IsPublic = IsPublic + IsPublic = IsPublic, + AllowRouting = AllowRouting, + Disabled = Disabled, + Profiles = Profiles, + RoutingRules = RoutingRules }; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index 3052e5b6..bad6e861 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Routing.Models; namespace BotSharp.OpenAPI.ViewModels.Agents; @@ -32,12 +33,30 @@ public class AgentUpdateModel /// public List? Responses { get; set; } + public bool IsPublic { get; set; } + + public bool AllowRouting { get; set; } + + public bool Disabled { get; set; } + + /// + /// Profile by channel + /// + public List Profiles { get; set; } + + public List RoutingRules { get; set; } + public Agent ToAgent() { var agent = new Agent() { Name = Name ?? string.Empty, Description = Description ?? string.Empty, + IsPublic = IsPublic, + Disabled = Disabled, + AllowRouting = AllowRouting, + Profiles = Profiles ?? new List(), + RoutingRules = RoutingRules ?? new List(), Instruction = Instruction ?? string.Empty, Templates = Templates ?? new List(), Functions = Functions ?? new List(), diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs index aa84af03..0f3d5ab8 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Plugin.MongoStorage.Models; namespace BotSharp.Plugin.MongoStorage.Collections; @@ -11,6 +12,10 @@ public class AgentCollection : MongoBase public List Functions { get; set; } public List Responses { get; set; } public bool IsPublic { get; set; } + public bool AllowRouting { get; set; } + public bool Disabled { get; set; } + public List Profiles { get; set; } + public List RoutingRules { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs new file mode 100644 index 00000000..bdb8e1be --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs @@ -0,0 +1,37 @@ +using BotSharp.Abstraction.Routing.Models; + +namespace BotSharp.Plugin.MongoStorage.Models; + +public class RoutingRuleElement +{ + public string Field { get; set; } + public bool Required { get; set; } + public Guid? RedirectTo { get; set; } + + public RoutingRuleElement() + { + + } + + public static RoutingRuleElement ToMongoElement(RoutingRule routingRule) + { + return new RoutingRuleElement + { + Field = routingRule.Field, + Required = routingRule.Required, + RedirectTo = !string.IsNullOrEmpty(routingRule.RedirectTo) ? Guid.Parse(routingRule.RedirectTo) : null + }; + } + + public static RoutingRule ToDomainElement(string agentId, string agentName, RoutingRuleElement rule) + { + return new RoutingRule + { + AgentId = agentId, + AgentName = agentName, + Field = rule.Field, + Required = rule.Required, + RedirectTo = rule.RedirectTo?.ToString() + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 6f3ece02..d436b7b2 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -1,7 +1,9 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Users.Models; using BotSharp.Plugin.MongoStorage.Collections; +using BotSharp.Plugin.MongoStorage.Models; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -42,6 +44,12 @@ public class MongoRepository : IBotSharpRepository Functions = x.Functions, Responses = x.Responses, IsPublic = x.IsPublic, + Disabled = x.Disabled, + AllowRouting = x.AllowRouting, + Profiles = x.Profiles, + RoutingRules = x.RoutingRules? + .Select(r => RoutingRuleElement.ToDomainElement(x.Id.ToString(), x.Name, r))? + .ToList() ?? new List(), CreatedDateTime = x.CreatedTime, UpdatedDateTime = x.UpdatedTime }).ToList(); @@ -206,6 +214,12 @@ public class MongoRepository : IBotSharpRepository Functions = x.Functions, Responses = x.Responses, IsPublic = x.IsPublic, + AllowRouting = x.AllowRouting, + Disabled = x.Disabled, + Profiles = x.Profiles, + RoutingRules = x.RoutingRules? + .Select(r => RoutingRuleElement.ToMongoElement(r))? + .ToList() ?? new List(), CreatedTime = x.CreatedDateTime, UpdatedTime = x.UpdatedDateTime }).ToList(); @@ -221,6 +235,10 @@ public class MongoRepository : IBotSharpRepository .Set(x => x.Functions, agent.Functions) .Set(x => x.Responses, agent.Responses) .Set(x => x.IsPublic, agent.IsPublic) + .Set(x => x.AllowRouting, agent.AllowRouting) + .Set(x => x.Disabled, agent.Disabled) + .Set(x => x.Profiles, agent.Profiles) + .Set(x => x.RoutingRules, agent.RoutingRules) .Set(x => x.CreatedTime, agent.CreatedTime) .Set(x => x.UpdatedTime, agent.UpdatedTime); _dc.Agents.UpdateOne(filter, update, _options); @@ -299,6 +317,18 @@ public class MongoRepository : IBotSharpRepository case AgentField.IsPublic: UpdateAgentIsPublic(agent.Id, agent.IsPublic); break; + case AgentField.Disabled: + UpdateAgentDisabled(agent.Id, agent.Disabled); + break; + case AgentField.AllowRouting: + UpdateAgentAllowRouting(agent.Id, agent.AllowRouting); + break; + case AgentField.Profiles: + UpdateAgentProfiles(agent.Id, agent.Profiles); + break; + case AgentField.RoutingRules: + UpdateAgentRoutingRules(agent.Id, agent.RoutingRules); + break; case AgentField.Instruction: UpdateAgentInstruction(agent.Id, agent.Instruction); break; @@ -354,6 +384,51 @@ public class MongoRepository : IBotSharpRepository _dc.Agents.UpdateOne(filter, update); } + private void UpdateAgentDisabled(string agentId, bool disabled) + { + var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(agentId)); + var update = Builders.Update + .Set(x => x.Disabled, disabled) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + + private void UpdateAgentAllowRouting(string agentId, bool allowRouting) + { + var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(agentId)); + var update = Builders.Update + .Set(x => x.AllowRouting, allowRouting) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + + private void UpdateAgentProfiles(string agentId, List profiles) + { + if (profiles.IsNullOrEmpty()) return; + + var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(agentId)); + var update = Builders.Update + .Set(x => x.Profiles, profiles) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + + private void UpdateAgentRoutingRules(string agentId, List rules) + { + if (rules.IsNullOrEmpty()) return; + + var ruleElements = rules.Select(x => RoutingRuleElement.ToMongoElement(x)).ToList(); + var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(agentId)); + var update = Builders.Update + .Set(x => x.RoutingRules, ruleElements) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + private void UpdateAgentInstruction(string agentId, string instruction) { if (string.IsNullOrEmpty(instruction)) return; @@ -408,6 +483,10 @@ public class MongoRepository : IBotSharpRepository var update = Builders.Update .Set(x => x.Name, agent.Name) .Set(x => x.Description, agent.Description) + .Set(x => x.Disabled, agent.Disabled) + .Set(x => x.AllowRouting, agent.AllowRouting) + .Set(x => x.Profiles, agent.Profiles) + .Set(x => x.RoutingRules, agent.RoutingRules.Select(x => RoutingRuleElement.ToMongoElement(x)).ToList()) .Set(x => x.Instruction, agent.Instruction) .Set(x => x.Templates, agent.Templates) .Set(x => x.Functions, agent.Functions) From 16b6683c01cf14b0c1244f8cc339f8cbb6efe1a8 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 19 Sep 2023 11:38:31 -0500 Subject: [PATCH 2/4] temp save --- .../Repository/FileRepository.cs | 57 +++++++++++++++++++ .../Collections/AgentCollection.cs | 2 +- .../Repository/MongoRepository.cs | 4 +- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 1a6445e7..1544a985 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -4,6 +4,7 @@ using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef; using BotSharp.Abstraction.Users.Models; using BotSharp.Abstraction.Agents.Models; using MongoDB.Driver; +using BotSharp.Abstraction.Routing.Models; namespace BotSharp.Core.Repository; @@ -240,6 +241,18 @@ public class FileRepository : IBotSharpRepository case AgentField.IsPublic: UpdateAgentIsPublic(agent.Id, agent.IsPublic); break; + case AgentField.Disabled: + UpdateAgentDisabled(agent.Id, agent.Disabled); + break; + case AgentField.AllowRouting: + UpdateAgentAllowRouting(agent.Id, agent.AllowRouting); + break; + case AgentField.Profiles: + UpdateAgentProfiles(agent.Id, agent.Profiles); + break; + case AgentField.RoutingRules: + UpdateAgentRoutingRules(agent.Id, agent.RoutingRules); + break; case AgentField.Instruction: UpdateAgentInstruction(agent.Id, agent.Instruction); break; @@ -298,6 +311,50 @@ public class FileRepository : IBotSharpRepository File.WriteAllText(agentFile, json); } + private void UpdateAgentDisabled(string agentId, bool disabled) + { + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.Disabled = disabled; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + + private void UpdateAgentAllowRouting(string agentId, bool allowRouting) + { + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.AllowRouting = allowRouting; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + + private void UpdateAgentProfiles(string agentId, List profiles) + { + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.Profiles = profiles; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + + private void UpdateAgentRoutingRules(string agentId, List rules) + { + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.RoutingRules = rules; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + private void UpdateAgentInstruction(string agentId, string instruction) { if (string.IsNullOrEmpty(instruction)) return; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs index 0f3d5ab8..4b92b87e 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs @@ -15,7 +15,7 @@ public class AgentCollection : MongoBase public bool AllowRouting { get; set; } public bool Disabled { get; set; } public List Profiles { get; set; } - public List RoutingRules { get; set; } + public List RoutingRules { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index d436b7b2..e1616cce 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -218,8 +218,8 @@ public class MongoRepository : IBotSharpRepository Disabled = x.Disabled, Profiles = x.Profiles, RoutingRules = x.RoutingRules? - .Select(r => RoutingRuleElement.ToMongoElement(r))? - .ToList() ?? new List(), + .Select(r => RoutingRuleElement.ToMongoElement(r))? + .ToList() ?? new List(), CreatedTime = x.CreatedDateTime, UpdatedTime = x.UpdatedDateTime }).ToList(); From d238064af964da5efa09371086f5b06947ca150c Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 19 Sep 2023 13:18:29 -0500 Subject: [PATCH 3/4] refine agent and update --- .../Repository/FileRepository.cs | 8 +++++ .../Controllers/AgentController.cs | 34 ++++++++++++++++++- .../ViewModels/Agents/AgentCreationModel.cs | 6 ++-- .../ViewModels/Agents/AgentUpdateModel.cs | 8 +++-- .../ViewModels/Agents/AgentViewModel.cs | 10 ++++++ .../Agents/RoutingRuleUpdateModel.cs | 25 ++++++++++++++ .../Collections/RoutingItemCollection.cs | 11 ------ .../Collections/RoutingProfileCollection.cs | 7 ---- .../MongoDbContext.cs | 6 ---- 9 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 1544a985..ecda379f 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -335,6 +335,8 @@ public class FileRepository : IBotSharpRepository private void UpdateAgentProfiles(string agentId, List profiles) { + if (profiles.IsNullOrEmpty()) return; + var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; @@ -346,6 +348,8 @@ public class FileRepository : IBotSharpRepository private void UpdateAgentRoutingRules(string agentId, List rules) { + if (rules.IsNullOrEmpty()) return; + var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; @@ -453,6 +457,10 @@ public class FileRepository : IBotSharpRepository agent.Name = inputAgent.Name; agent.Description = inputAgent.Description; agent.IsPublic = inputAgent.IsPublic; + agent.Disabled = inputAgent.Disabled; + agent.AllowRouting = inputAgent.AllowRouting; + agent.Profiles = inputAgent.Profiles; + agent.RoutingRules = inputAgent.RoutingRules; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); File.WriteAllText(agentFile, json); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index b5cf73fb..7baaa16a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -66,6 +66,38 @@ public class AgentController : ControllerBase, IApiAdapter await _agentService.UpdateAgent(model, AgentField.IsPublic); } + [HttpPut("/agent/{agentId}/disabled")] + public async Task UpdateAgentDisabled([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.Disabled); + } + + [HttpPut("/agent/{agentId}/allow-routing")] + public async Task UpdateAgentAllowRouting([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.AllowRouting); + } + + [HttpPut("/agent/{agentId}/profiles")] + public async Task UpdateAgentProfiles([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.Profiles); + } + + [HttpPut("/agent/{agentId}/routing-rules")] + public async Task UpdateAgentRoutingRules([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + await _agentService.UpdateAgent(model, AgentField.RoutingRules); + } + [HttpPut("/agent/{agentId}/instruction")] public async Task UpdateAgentInstruction([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) { @@ -83,7 +115,7 @@ public class AgentController : ControllerBase, IApiAdapter } [HttpPut("/agent/{agentId}/templates")] - public async Task UpdateAgenttemplates([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) + public async Task UpdateAgentTemplates([FromRoute] string agentId, [FromBody] AgentUpdateModel agent) { var model = agent.ToAgent(); model.Id = agentId; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index 277f2d8a..3857d82c 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -15,7 +15,7 @@ public class AgentCreationModel public bool AllowRouting { get; set; } public bool Disabled { get; set; } public List Profiles { get; set; } - public List RoutingRules { get; set; } + public List RoutingRules { get; set; } public Agent ToAgent() { @@ -31,7 +31,9 @@ public class AgentCreationModel AllowRouting = AllowRouting, Disabled = Disabled, Profiles = Profiles, - RoutingRules = RoutingRules + RoutingRules = RoutingRules? + .Select(x => RoutingRuleUpdateModel.ToDomainElement(x))? + .ToList() ?? new List() }; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index bad6e861..38452740 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -42,9 +42,9 @@ public class AgentUpdateModel /// /// Profile by channel /// - public List Profiles { get; set; } + public List? Profiles { get; set; } - public List RoutingRules { get; set; } + public List? RoutingRules { get; set; } public Agent ToAgent() { @@ -56,7 +56,9 @@ public class AgentUpdateModel Disabled = Disabled, AllowRouting = AllowRouting, Profiles = Profiles ?? new List(), - RoutingRules = RoutingRules ?? new List(), + RoutingRules = RoutingRules? + .Select(x => RoutingRuleUpdateModel.ToDomainElement(x))? + .ToList() ?? new List(), Instruction = Instruction ?? string.Empty, Templates = Templates ?? new List(), Functions = Functions ?? new List(), diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index ec9c29ad..bf25ec64 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Routing.Models; namespace BotSharp.OpenAPI.ViewModels.Agents; @@ -12,6 +13,11 @@ public class AgentViewModel public List Functions { get; set; } public List Responses { get; set; } public bool IsPublic { get; set; } + public bool AllowRouting { get; set; } + public bool Disabled { get; set; } + public List Profiles { get; set; } + public List RoutingRules { get; set; } + public DateTime CreatedDateTime { get; set; } public DateTime UpdatedDateTime { get; set; } @@ -27,6 +33,10 @@ public class AgentViewModel Functions = agent.Functions, Responses = agent.Responses, IsPublic= agent.IsPublic, + Disabled = agent.Disabled, + AllowRouting = agent.AllowRouting, + Profiles = agent.Profiles, + RoutingRules = agent.RoutingRules, CreatedDateTime = agent.CreatedDateTime, UpdatedDateTime = agent.UpdatedDateTime }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs new file mode 100644 index 00000000..9736db19 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/RoutingRuleUpdateModel.cs @@ -0,0 +1,25 @@ +using BotSharp.Abstraction.Routing.Models; + +namespace BotSharp.OpenAPI.ViewModels.Agents; + +public class RoutingRuleUpdateModel +{ + public string Field { get; set; } + public bool Required { get; set; } + public string? RedirectTo { get; set; } + + public RoutingRuleUpdateModel() + { + + } + + public static RoutingRule ToDomainElement(RoutingRuleUpdateModel model) + { + return new RoutingRule + { + Field = model.Field, + Required = model.Required, + RedirectTo = model.RedirectTo + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs deleted file mode 100644 index 63208306..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingItemCollection.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace BotSharp.Plugin.MongoStorage.Collections; - -public class RoutingItemCollection : MongoBase -{ - public Guid AgentId { get; set; } - public string Name { get; set; } - public string Description { get; set; } - public List RequiredFields { get; set; } - public Guid? RedirectTo { get; set; } - public bool Disabled { get; set; } -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs deleted file mode 100644 index 3b41ab7a..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/RoutingProfileCollection.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace BotSharp.Plugin.MongoStorage.Collections; - -public class RoutingProfileCollection : MongoBase -{ - public string Name { get; set; } - public List AgentIds { get; set; } -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 4782a5b3..4c8479dd 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -42,10 +42,4 @@ public class MongoDbContext public IMongoCollection UserAgents => Database.GetCollection($"{_collectionPrefix}_UserAgents"); - - public IMongoCollection RoutingItems - => Database.GetCollection($"{_collectionPrefix}_RoutingItems"); - - public IMongoCollection RoutingProfiles - => Database.GetCollection($"{_collectionPrefix}_RoutingProfiles"); } From 114bae3c2aa9a98d231deaa4aa882b353445b89f Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 19 Sep 2023 13:20:41 -0500 Subject: [PATCH 4/4] rename mongo routing rule element --- .../Collections/AgentCollection.cs | 2 +- ...outingRuleElement.cs => RoutingRuleMongoElement.cs} | 10 +++++----- .../Repository/MongoRepository.cs | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) rename src/Plugins/BotSharp.Plugin.MongoStorage/Models/{RoutingRuleElement.cs => RoutingRuleMongoElement.cs} (76%) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs index 4b92b87e..ff555750 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs @@ -15,7 +15,7 @@ public class AgentCollection : MongoBase public bool AllowRouting { get; set; } public bool Disabled { get; set; } public List Profiles { get; set; } - public List RoutingRules { get; set; } + public List RoutingRules { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs similarity index 76% rename from src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs rename to src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs index bdb8e1be..c2144870 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs @@ -2,20 +2,20 @@ using BotSharp.Abstraction.Routing.Models; namespace BotSharp.Plugin.MongoStorage.Models; -public class RoutingRuleElement +public class RoutingRuleMongoElement { public string Field { get; set; } public bool Required { get; set; } public Guid? RedirectTo { get; set; } - public RoutingRuleElement() + public RoutingRuleMongoElement() { } - public static RoutingRuleElement ToMongoElement(RoutingRule routingRule) + public static RoutingRuleMongoElement ToMongoElement(RoutingRule routingRule) { - return new RoutingRuleElement + return new RoutingRuleMongoElement { Field = routingRule.Field, Required = routingRule.Required, @@ -23,7 +23,7 @@ public class RoutingRuleElement }; } - public static RoutingRule ToDomainElement(string agentId, string agentName, RoutingRuleElement rule) + public static RoutingRule ToDomainElement(string agentId, string agentName, RoutingRuleMongoElement rule) { return new RoutingRule { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index e1616cce..0e919fcb 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -48,7 +48,7 @@ public class MongoRepository : IBotSharpRepository AllowRouting = x.AllowRouting, Profiles = x.Profiles, RoutingRules = x.RoutingRules? - .Select(r => RoutingRuleElement.ToDomainElement(x.Id.ToString(), x.Name, r))? + .Select(r => RoutingRuleMongoElement.ToDomainElement(x.Id.ToString(), x.Name, r))? .ToList() ?? new List(), CreatedDateTime = x.CreatedTime, UpdatedDateTime = x.UpdatedTime @@ -218,8 +218,8 @@ public class MongoRepository : IBotSharpRepository Disabled = x.Disabled, Profiles = x.Profiles, RoutingRules = x.RoutingRules? - .Select(r => RoutingRuleElement.ToMongoElement(r))? - .ToList() ?? new List(), + .Select(r => RoutingRuleMongoElement.ToMongoElement(r))? + .ToList() ?? new List(), CreatedTime = x.CreatedDateTime, UpdatedTime = x.UpdatedDateTime }).ToList(); @@ -420,7 +420,7 @@ public class MongoRepository : IBotSharpRepository { if (rules.IsNullOrEmpty()) return; - var ruleElements = rules.Select(x => RoutingRuleElement.ToMongoElement(x)).ToList(); + var ruleElements = rules.Select(x => RoutingRuleMongoElement.ToMongoElement(x)).ToList(); var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(agentId)); var update = Builders.Update .Set(x => x.RoutingRules, ruleElements) @@ -486,7 +486,7 @@ public class MongoRepository : IBotSharpRepository .Set(x => x.Disabled, agent.Disabled) .Set(x => x.AllowRouting, agent.AllowRouting) .Set(x => x.Profiles, agent.Profiles) - .Set(x => x.RoutingRules, agent.RoutingRules.Select(x => RoutingRuleElement.ToMongoElement(x)).ToList()) + .Set(x => x.RoutingRules, agent.RoutingRules.Select(x => RoutingRuleMongoElement.ToMongoElement(x)).ToList()) .Set(x => x.Instruction, agent.Instruction) .Set(x => x.Templates, agent.Templates) .Set(x => x.Functions, agent.Functions)