From 390399dd7e70cd4052a70a0b0ee09c2a7ad42598 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 29 Jan 2024 13:22:37 -0600 Subject: [PATCH] add inherit agent id in mongo --- .../Agents/Enums/AgentField.cs | 1 + .../FileRepository/FileRepository.Agent.cs | 14 ++++++++++++++ .../Collections/AgentDocument.cs | 1 + .../Repository/MongoRepository.Agent.cs | 16 ++++++++++++++++ .../Repository/MongoRepository.Transaction.cs | 2 ++ 5 files changed, 34 insertions(+) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 0fdeffe3..81aacb85 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -8,6 +8,7 @@ public enum AgentField IsPublic, Disabled, Type, + InheritAgentId, Profiles, RoutingRule, Instruction, diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index b2476921..01a75d40 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -31,6 +31,9 @@ namespace BotSharp.Core.Repository case AgentField.Type: UpdateAgentType(agent.Id, agent.Type); break; + case AgentField.InheritAgentId: + UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); + break; case AgentField.Profiles: UpdateAgentProfiles(agent.Id, agent.Profiles); break; @@ -123,6 +126,17 @@ namespace BotSharp.Core.Repository File.WriteAllText(agentFile, json); } + private void UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) + { + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.InheritAgentId = inheritAgentId; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + private void UpdateAgentProfiles(string agentId, List profiles) { if (profiles.IsNullOrEmpty()) return; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs index 603f312e..8e2f4dd4 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs @@ -7,6 +7,7 @@ public class AgentDocument : MongoBase public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } + public string? InheritAgentId { get; set; } public string? IconUrl { get; set; } public string Instruction { get; set; } public List Templates { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 9a0f0cee..58fe44ff 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -32,6 +32,9 @@ public partial class MongoRepository case AgentField.Type: UpdateAgentType(agent.Id, agent.Type); break; + case AgentField.InheritAgentId: + UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); + break; case AgentField.Profiles: UpdateAgentProfiles(agent.Id, agent.Profiles); break; @@ -119,6 +122,16 @@ public partial class MongoRepository _dc.Agents.UpdateOne(filter, update); } + private void UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) + { + var filter = Builders.Filter.Eq(x => x.Id, agentId); + var update = Builders.Update + .Set(x => x.InheritAgentId, inheritAgentId) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + private void UpdateAgentProfiles(string agentId, List profiles) { if (profiles.IsNullOrEmpty()) return; @@ -268,6 +281,7 @@ public partial class MongoRepository IsPublic = agent.IsPublic, Disabled = agent.Disabled, Type = agent.Type, + InheritAgentId = agent.InheritAgentId, Profiles = agent.Profiles, RoutingRules = !agent.RoutingRules.IsNullOrEmpty() ? agent.RoutingRules .Select(r => RoutingRuleMongoElement.ToDomainElement(agent.Id, agent.Name, r)) @@ -329,6 +343,7 @@ public partial class MongoRepository IsPublic = x.IsPublic, Disabled = x.Disabled, Type = x.Type, + InheritAgentId = x.InheritAgentId, Profiles = x.Profiles, RoutingRules = !x.RoutingRules.IsNullOrEmpty() ? x.RoutingRules .Select(r => RoutingRuleMongoElement.ToDomainElement(x.Id, x.Name, r)) @@ -393,6 +408,7 @@ public partial class MongoRepository Samples = x.Samples ?? new List(), IsPublic = x.IsPublic, Type = x.Type, + InheritAgentId = x.InheritAgentId, Disabled = x.Disabled, Profiles = x.Profiles, RoutingRules = x.RoutingRules? diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Transaction.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Transaction.cs index 17e59c2e..470b1fb1 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Transaction.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Transaction.cs @@ -55,6 +55,7 @@ public partial class MongoRepository Samples = x.Samples ?? new List(), IsPublic = x.IsPublic, Type = x.Type, + InheritAgentId = x.InheritAgentId, Disabled = x.Disabled, Profiles = x.Profiles, RoutingRules = x.RoutingRules? @@ -78,6 +79,7 @@ public partial class MongoRepository .Set(x => x.Samples, agent.Samples) .Set(x => x.IsPublic, agent.IsPublic) .Set(x => x.Type, agent.Type) + .Set(x => x.InheritAgentId, agent.InheritAgentId) .Set(x => x.Disabled, agent.Disabled) .Set(x => x.Profiles, agent.Profiles) .Set(x => x.RoutingRules, agent.RoutingRules)