From d241b21adebd27244bee69a9c58c10f217a11507 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sun, 5 Jan 2025 23:54:48 -0600 Subject: [PATCH 1/4] add agent event rule --- .../Agents/Enums/AgentField.cs | 1 + .../Agents/IAgentService.cs | 2 -- .../Agents/Models/Agent.cs | 12 +++++++ .../Agents/Models/AgentEventRule.cs | 13 ++++++++ .../Hooks/IEventRuleHook.cs | 8 +++++ .../Services/AgentService.UpdateAgent.cs | 2 ++ .../Agents/Services/AgentService.cs | 11 ------- .../FileRepository/FileRepository.Agent.cs | 19 ++++++++++- .../BotSharp.OpenAPI/BotSharp.OpenAPI.csproj | 1 + .../Controllers/AgentController.cs | 22 ++++++++++++- .../ViewModels/Agents/AgentCreationModel.cs | 2 ++ .../ViewModels/Agents/AgentUpdateModel.cs | 18 ++++++---- .../ViewModels/Agents/AgentViewModel.cs | 22 ++++++++----- .../Collections/AgentDocument.cs | 1 + .../Models/AgentEventRuleMongoElement.cs | 33 +++++++++++++++++++ .../Repository/MongoRepository.Agent.cs | 23 +++++++++++-- 16 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs create mode 100644 src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs create mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 346bcf31..613c1778 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -19,6 +19,7 @@ public enum AgentField LlmConfig, Utility, KnowledgeBase, + EventRule, MaxMessageCount } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index 91beea8b..2ec4ff92 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -58,6 +58,4 @@ public interface IAgentService Task> GetUserAgents(string userId); PluginDef GetPlugin(string agentId); - - IEnumerable GetAgentUtilityOptions(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 566df050..83927d9f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -99,6 +99,11 @@ public class Agent /// public List Utilities { get; set; } = new(); + /// + /// Agent rules + /// + public List EventRules { get; set; } = new(); + /// /// Agent knowledge bases /// @@ -154,6 +159,7 @@ public class Agent MaxMessageCount = agent.MaxMessageCount, Profiles = agent.Profiles, RoutingRules = agent.RoutingRules, + EventRules = agent.EventRules, LlmConfig = agent.LlmConfig, KnowledgeBases = agent.KnowledgeBases, CreatedDateTime = agent.CreatedDateTime, @@ -269,6 +275,12 @@ public class Agent return this; } + public Agent SetEventRules(List rules) + { + EventRules = rules ?? []; + return this; + } + public Agent SetLlmConfig(AgentLlmConfig? llmConfig) { LlmConfig = llmConfig; diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs new file mode 100644 index 00000000..c23ef890 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs @@ -0,0 +1,13 @@ +namespace BotSharp.Abstraction.Agents.Models; + +public class AgentEventRule +{ + public string Name { get; set; } + public bool Disabled { get; set; } + + [JsonPropertyName("event_name")] + public string EventName { get; set; } + + [JsonPropertyName("event_type")] + public string EntityType { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs b/src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs new file mode 100644 index 00000000..e65050b8 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs @@ -0,0 +1,8 @@ +using BotSharp.Abstraction.Agents.Models; + +namespace BotSharp.Core.Rules.Hooks; + +public interface IEventRuleHook +{ + void AddRules(List rules); +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 584a4c7e..ea95daa5 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -40,6 +40,7 @@ public partial class AgentService record.Samples = agent.Samples ?? []; record.Utilities = agent.Utilities ?? []; record.KnowledgeBases = agent.KnowledgeBases ?? []; + record.EventRules = agent.EventRules ?? []; if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit) { record.LlmConfig = agent.LlmConfig; @@ -104,6 +105,7 @@ public partial class AgentService .SetSamples(foundAgent.Samples) .SetUtilities(foundAgent.Utilities) .SetKnowledgeBases(foundAgent.KnowledgeBases) + .SetEventRules(foundAgent.EventRules) .SetLlmConfig(foundAgent.LlmConfig); _db.UpdateAgent(clonedAgent, AgentField.All); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index 80cdc56c..c54c4fd8 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -53,15 +53,4 @@ public partial class AgentService : IAgentService var userAgents = _db.GetUserAgents(userId); return userAgents; } - - public IEnumerable GetAgentUtilityOptions() - { - var utilities = new List(); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - hook.AddUtilities(utilities); - } - return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList(); - } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index 0a8d5b65..3b8614f1 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -60,6 +60,9 @@ namespace BotSharp.Core.Repository case AgentField.KnowledgeBase: UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); break; + case AgentField.EventRule: + UpdateAgentEventRules(agent.Id, agent.EventRules); + break; case AgentField.MaxMessageCount: UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); break; @@ -184,6 +187,19 @@ namespace BotSharp.Core.Repository File.WriteAllText(agentFile, json); } + private void UpdateAgentEventRules(string agentId, List rules) + { + if (rules == null) return; + + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.EventRules = rules; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + private void UpdateAgentRoutingRules(string agentId, List rules) { if (rules == null) return; @@ -240,7 +256,7 @@ namespace BotSharp.Core.Repository var text = JsonSerializer.Serialize(func, _options); var file = Path.Combine(functionDir, $"{func.Name}.json"); File.WriteAllText(file, text); - Thread.Sleep(100); + Thread.Sleep(50); } } @@ -328,6 +344,7 @@ namespace BotSharp.Core.Repository agent.Utilities = inputAgent.Utilities; agent.KnowledgeBases = inputAgent.KnowledgeBases; agent.RoutingRules = inputAgent.RoutingRules; + agent.EventRules = inputAgent.EventRules; agent.LlmConfig = inputAgent.LlmConfig; agent.MaxMessageCount = inputAgent.MaxMessageCount; agent.UpdatedDateTime = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj index 28dde755..bacf34e9 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj @@ -48,6 +48,7 @@ + diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 1885365d..b4f1b3e6 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,4 +1,6 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Core.Infrastructures; +using BotSharp.Core.Rules.Hooks; namespace BotSharp.OpenAPI.Controllers; @@ -152,6 +154,24 @@ public class AgentController : ControllerBase [HttpGet("/agent/utility/options")] public IEnumerable GetAgentUtilityOptions() { - return _agentService.GetAgentUtilityOptions(); + var utilities = new List(); + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + hook.AddUtilities(utilities); + } + return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList(); + } + + [HttpGet("/agent/event-rule/options")] + public IEnumerable GetAgentEventRuleOptions() + { + var rules = new List(); + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + hook.AddRules(rules); + } + return rules.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList(); } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index 3caba6ae..9df167b7 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -55,6 +55,7 @@ public class AgentCreationModel public List Utilities { get; set; } = new(); public List RoutingRules { get; set; } = new(); public List KnowledgeBases { get; set; } = new(); + public List EventRules { get; set; } = new(); public AgentLlmConfig? LlmConfig { get; set; } public Agent ToAgent() @@ -78,6 +79,7 @@ public class AgentCreationModel Profiles = Profiles, LlmConfig = LlmConfig, KnowledgeBases = KnowledgeBases, + EventRules = EventRules, RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [], }; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index 66618186..2bfaa52f 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -75,6 +75,9 @@ public class AgentUpdateModel [JsonPropertyName("routing_rules")] public List? RoutingRules { get; set; } + [JsonPropertyName("event_rules")] + public List? EventRules { get; set; } + [JsonPropertyName("llm_config")] public AgentLlmConfig? LlmConfig { get; set; } @@ -89,15 +92,16 @@ public class AgentUpdateModel MergeUtility = MergeUtility, MaxMessageCount = MaxMessageCount, Type = Type, - Profiles = Profiles ?? new List(), - RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List(), + Profiles = Profiles ?? [], + RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [], Instruction = Instruction ?? string.Empty, - ChannelInstructions = ChannelInstructions ?? new List(), - Templates = Templates ?? new List(), - Functions = Functions ?? new List(), - Responses = Responses ?? new List(), - Utilities = Utilities ?? new List(), + ChannelInstructions = ChannelInstructions ?? [], + Templates = Templates ?? [], + Functions = Functions ?? [], + Responses = Responses ?? [], + Utilities = Utilities ?? [], KnowledgeBases = KnowledgeBases ?? [], + EventRules = EventRules ?? [], LlmConfig = LlmConfig }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index e9479109..d2a6f2ca 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -28,6 +28,9 @@ public class AgentViewModel [JsonPropertyName("knowledge_bases")] public List KnowledgeBases { get; set; } + [JsonPropertyName("event_rules")] + public List EventRules { get; set; } + [JsonPropertyName("is_public")] public bool IsPublic { get; set; } @@ -72,20 +75,21 @@ public class AgentViewModel Description = agent.Description, Type = agent.Type, Instruction = agent.Instruction, - ChannelInstructions = agent.ChannelInstructions, - Templates = agent.Templates, - Functions = agent.Functions, - Responses = agent.Responses, - Samples = agent.Samples, - Utilities = agent.Utilities, - KnowledgeBases = agent.KnowledgeBases, + ChannelInstructions = agent.ChannelInstructions ?? [], + Templates = agent.Templates ?? [], + Functions = agent.Functions ?? [], + Responses = agent.Responses ?? [], + Samples = agent.Samples ?? [], + Utilities = agent.Utilities ?? [], + KnowledgeBases = agent.KnowledgeBases ?? [], IsPublic= agent.IsPublic, Disabled = agent.Disabled, MergeUtility = agent.MergeUtility, IconUrl = agent.IconUrl, MaxMessageCount = agent.MaxMessageCount, - Profiles = agent.Profiles ?? new List(), - RoutingRules = agent.RoutingRules, + Profiles = agent.Profiles ?? [], + RoutingRules = agent.RoutingRules ?? [], + EventRules = agent.EventRules ?? [], LlmConfig = agent.LlmConfig, Plugin = agent.Plugin, CreatedDateTime = agent.CreatedDateTime, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs index 19945124..816e0904 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs @@ -21,6 +21,7 @@ public class AgentDocument : MongoBase public List KnowledgeBases { get; set; } public List Profiles { get; set; } public List RoutingRules { get; set; } + public List EventRules { get; set; } public AgentLlmConfigMongoElement? LlmConfig { get; set; } public DateTime CreatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs new file mode 100644 index 00000000..cadb3c45 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs @@ -0,0 +1,33 @@ +using BotSharp.Abstraction.Agents.Models; + +namespace BotSharp.Plugin.MongoStorage.Models; + +public class AgentEventRuleMongoElement +{ + public string Name { get; set; } + public bool Disabled { get; set; } + public string EventName { get; set; } + public string EntityType { get; set; } + + public static AgentEventRuleMongoElement ToMongoElement(AgentEventRule rule) + { + return new AgentEventRuleMongoElement + { + Name = rule.Name, + Disabled = rule.Disabled, + EventName = rule.EventName, + EntityType = rule.EntityType + }; + } + + public static AgentEventRule ToDomainElement(AgentEventRuleMongoElement rule) + { + return new AgentEventRule + { + Name = rule.Name, + Disabled = rule.Disabled, + EventName = rule.EventName, + EntityType = rule.EntityType + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 7f86d144..92d9db60 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -61,6 +61,9 @@ public partial class MongoRepository case AgentField.KnowledgeBase: UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); break; + case AgentField.EventRule: + UpdateAgentEventRules(agent.Id, agent.EventRules); + break; case AgentField.MaxMessageCount: UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); break; @@ -256,6 +259,20 @@ public partial class MongoRepository _dc.Agents.UpdateOne(filter, update); } + private void UpdateAgentEventRules(string agentId, List rules) + { + if (rules == null) return; + + var elements = rules?.Select(x => AgentEventRuleMongoElement.ToMongoElement(x))?.ToList() ?? []; + + var filter = Builders.Filter.Eq(x => x.Id, agentId); + var update = Builders.Update + .Set(x => x.EventRules, elements) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) { var llmConfig = AgentLlmConfigMongoElement.ToMongoElement(config); @@ -297,12 +314,12 @@ public partial class MongoRepository .Set(x => x.Samples, agent.Samples) .Set(x => x.Utilities, agent.Utilities.Select(u => AgentUtilityMongoElement.ToMongoElement(u)).ToList()) .Set(x => x.KnowledgeBases, agent.KnowledgeBases.Select(u => AgentKnowledgeBaseMongoElement.ToMongoElement(u)).ToList()) + .Set(x => x.EventRules, agent.EventRules.Select(e => AgentEventRuleMongoElement.ToMongoElement(e)).ToList()) .Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig)) .Set(x => x.IsPublic, agent.IsPublic) .Set(x => x.UpdatedTime, DateTime.UtcNow); var res = _dc.Agents.UpdateOne(filter, update); - Console.WriteLine(); } #endregion @@ -455,6 +472,7 @@ public partial class MongoRepository RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [], Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [], KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [], + EventRules = x.EventRules?.Select(e => AgentEventRuleMongoElement.ToMongoElement(e))?.ToList() ?? [], CreatedTime = x.CreatedDateTime, UpdatedTime = x.UpdatedDateTime }).ToList(); @@ -546,7 +564,8 @@ public partial class MongoRepository Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [], RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [], Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [], - KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [] + KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [], + EventRules = agentDoc.EventRules?.Select(e => AgentEventRuleMongoElement.ToDomainElement(e))?.ToList() ?? [] }; } } From 8dff3f5ce3a9fb9db14bc3b7edce8b44fc73bbea Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 6 Jan 2025 00:01:14 -0600 Subject: [PATCH 2/4] rename --- .../BotSharp.Abstraction/Agents/Models/AgentEventRule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs index c23ef890..180c83a6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs @@ -8,6 +8,6 @@ public class AgentEventRule [JsonPropertyName("event_name")] public string EventName { get; set; } - [JsonPropertyName("event_type")] + [JsonPropertyName("entity_type")] public string EntityType { get; set; } } From b681b18ac2d0412a22a36fa38ff3b5603adb7e60 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 7 Jan 2025 13:50:50 -0600 Subject: [PATCH 3/4] refine agent rule --- .../Agents/Enums/AgentField.cs | 2 +- .../Agents/IAgentRuleHook.cs | 6 ++++++ .../BotSharp.Abstraction/Agents/Models/Agent.cs | 8 ++++---- .../Models/{AgentEventRule.cs => AgentRule.cs} | 2 +- .../BotSharp.Core.Rules/Hooks/IEventRuleHook.cs | 8 -------- .../Agents/Services/AgentService.UpdateAgent.cs | 4 ++-- .../FileRepository/FileRepository.Agent.cs | 10 +++++----- .../BotSharp.OpenAPI/BotSharp.OpenAPI.csproj | 1 - .../Controllers/AgentController.cs | 10 ++++------ .../ViewModels/Agents/AgentCreationModel.cs | 4 ++-- .../ViewModels/Agents/AgentUpdateModel.cs | 6 +++--- .../ViewModels/Agents/AgentViewModel.cs | 6 +++--- .../Functions/HandleHttpRequestFn.cs | 9 ++++++--- .../Collections/AgentDocument.cs | 2 +- ...eMongoElement.cs => AgentRuleMongoElement.cs} | 10 +++++----- .../Repository/MongoRepository.Agent.cs | 16 ++++++++-------- 16 files changed, 51 insertions(+), 53 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRuleHook.cs rename src/Infrastructure/BotSharp.Abstraction/Agents/Models/{AgentEventRule.cs => AgentRule.cs} (91%) delete mode 100644 src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs rename src/Plugins/BotSharp.Plugin.MongoStorage/Models/{AgentEventRuleMongoElement.cs => AgentRuleMongoElement.cs} (68%) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 613c1778..2a9508d7 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -19,7 +19,7 @@ public enum AgentField LlmConfig, Utility, KnowledgeBase, - EventRule, + Rule, MaxMessageCount } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRuleHook.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRuleHook.cs new file mode 100644 index 00000000..8a19a561 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRuleHook.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Abstraction.Agents; + +public interface IAgentRuleHook +{ + void AddRules(List rules); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 83927d9f..96aa070c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -102,7 +102,7 @@ public class Agent /// /// Agent rules /// - public List EventRules { get; set; } = new(); + public List Rules { get; set; } = new(); /// /// Agent knowledge bases @@ -159,7 +159,7 @@ public class Agent MaxMessageCount = agent.MaxMessageCount, Profiles = agent.Profiles, RoutingRules = agent.RoutingRules, - EventRules = agent.EventRules, + Rules = agent.Rules, LlmConfig = agent.LlmConfig, KnowledgeBases = agent.KnowledgeBases, CreatedDateTime = agent.CreatedDateTime, @@ -275,9 +275,9 @@ public class Agent return this; } - public Agent SetEventRules(List rules) + public Agent SetRules(List rules) { - EventRules = rules ?? []; + Rules = rules ?? []; return this; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentRule.cs similarity index 91% rename from src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs rename to src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentRule.cs index 180c83a6..5c3a276e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentEventRule.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentRule.cs @@ -1,6 +1,6 @@ namespace BotSharp.Abstraction.Agents.Models; -public class AgentEventRule +public class AgentRule { public string Name { get; set; } public bool Disabled { get; set; } diff --git a/src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs b/src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs deleted file mode 100644 index e65050b8..00000000 --- a/src/Infrastructure/BotSharp.Core.Rules/Hooks/IEventRuleHook.cs +++ /dev/null @@ -1,8 +0,0 @@ -using BotSharp.Abstraction.Agents.Models; - -namespace BotSharp.Core.Rules.Hooks; - -public interface IEventRuleHook -{ - void AddRules(List rules); -} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index ea95daa5..6889336a 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -40,7 +40,7 @@ public partial class AgentService record.Samples = agent.Samples ?? []; record.Utilities = agent.Utilities ?? []; record.KnowledgeBases = agent.KnowledgeBases ?? []; - record.EventRules = agent.EventRules ?? []; + record.Rules = agent.Rules ?? []; if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit) { record.LlmConfig = agent.LlmConfig; @@ -105,7 +105,7 @@ public partial class AgentService .SetSamples(foundAgent.Samples) .SetUtilities(foundAgent.Utilities) .SetKnowledgeBases(foundAgent.KnowledgeBases) - .SetEventRules(foundAgent.EventRules) + .SetRules(foundAgent.Rules) .SetLlmConfig(foundAgent.LlmConfig); _db.UpdateAgent(clonedAgent, AgentField.All); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index 3b8614f1..d38b2a89 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -60,8 +60,8 @@ namespace BotSharp.Core.Repository case AgentField.KnowledgeBase: UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); break; - case AgentField.EventRule: - UpdateAgentEventRules(agent.Id, agent.EventRules); + case AgentField.Rule: + UpdateAgentRules(agent.Id, agent.Rules); break; case AgentField.MaxMessageCount: UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); @@ -187,14 +187,14 @@ namespace BotSharp.Core.Repository File.WriteAllText(agentFile, json); } - private void UpdateAgentEventRules(string agentId, List rules) + private void UpdateAgentRules(string agentId, List rules) { if (rules == null) return; var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; - agent.EventRules = rules; + agent.Rules = rules; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); File.WriteAllText(agentFile, json); @@ -344,7 +344,7 @@ namespace BotSharp.Core.Repository agent.Utilities = inputAgent.Utilities; agent.KnowledgeBases = inputAgent.KnowledgeBases; agent.RoutingRules = inputAgent.RoutingRules; - agent.EventRules = inputAgent.EventRules; + agent.Rules = inputAgent.Rules; agent.LlmConfig = inputAgent.LlmConfig; agent.MaxMessageCount = inputAgent.MaxMessageCount; agent.UpdatedDateTime = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj index bacf34e9..28dde755 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj @@ -48,7 +48,6 @@ - diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index b4f1b3e6..6efb7104 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,6 +1,4 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Core.Infrastructures; -using BotSharp.Core.Rules.Hooks; namespace BotSharp.OpenAPI.Controllers; @@ -163,11 +161,11 @@ public class AgentController : ControllerBase return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList(); } - [HttpGet("/agent/event-rule/options")] - public IEnumerable GetAgentEventRuleOptions() + [HttpGet("/agent/rule/options")] + public IEnumerable GetAgentRuleOptions() { - var rules = new List(); - var hooks = _services.GetServices(); + var rules = new List(); + var hooks = _services.GetServices(); foreach (var hook in hooks) { hook.AddRules(rules); diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index 9df167b7..a0e8f790 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -55,7 +55,7 @@ public class AgentCreationModel public List Utilities { get; set; } = new(); public List RoutingRules { get; set; } = new(); public List KnowledgeBases { get; set; } = new(); - public List EventRules { get; set; } = new(); + public List Rules { get; set; } = new(); public AgentLlmConfig? LlmConfig { get; set; } public Agent ToAgent() @@ -79,7 +79,7 @@ public class AgentCreationModel Profiles = Profiles, LlmConfig = LlmConfig, KnowledgeBases = KnowledgeBases, - EventRules = EventRules, + Rules = Rules, RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [], }; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index 2bfaa52f..8e268b97 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -75,8 +75,8 @@ public class AgentUpdateModel [JsonPropertyName("routing_rules")] public List? RoutingRules { get; set; } - [JsonPropertyName("event_rules")] - public List? EventRules { get; set; } + [JsonPropertyName("rules")] + public List? Rules { get; set; } [JsonPropertyName("llm_config")] public AgentLlmConfig? LlmConfig { get; set; } @@ -101,7 +101,7 @@ public class AgentUpdateModel Responses = Responses ?? [], Utilities = Utilities ?? [], KnowledgeBases = KnowledgeBases ?? [], - EventRules = EventRules ?? [], + Rules = Rules ?? [], LlmConfig = LlmConfig }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index d2a6f2ca..de4a13ba 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -28,8 +28,8 @@ public class AgentViewModel [JsonPropertyName("knowledge_bases")] public List KnowledgeBases { get; set; } - [JsonPropertyName("event_rules")] - public List EventRules { get; set; } + [JsonPropertyName("rules")] + public List Rules { get; set; } [JsonPropertyName("is_public")] public bool IsPublic { get; set; } @@ -89,7 +89,7 @@ public class AgentViewModel MaxMessageCount = agent.MaxMessageCount, Profiles = agent.Profiles ?? [], RoutingRules = agent.RoutingRules ?? [], - EventRules = agent.EventRules ?? [], + Rules = agent.Rules ?? [], LlmConfig = agent.LlmConfig, Plugin = agent.Plugin, CreatedDateTime = agent.CreatedDateTime, diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs index d36aafba..f9a4342c 100644 --- a/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs @@ -46,7 +46,7 @@ public class HandleHttpRequestFn : IFunctionCallback catch (Exception ex) { var msg = $"Fail when sending http request. Url: {url}, method: {method}, content: {content}"; - _logger.LogWarning($"{msg}\n(Error: {ex.Message})"); + _logger.LogError($"{msg}\n(Error: {ex.Message}\r\n{ex.InnerException})"); message.Content = msg; return false; } @@ -71,10 +71,13 @@ public class HandleHttpRequestFn : IFunctionCallback private void AddRequestHeaders(HttpClient client) { - client.DefaultRequestHeaders.Add("Authorization", $"{_context.HttpContext.Request.Headers["Authorization"]}"); + var auth = $"{_context.HttpContext.Request.Headers["Authorization"]}"; + var origin = $"{_context.HttpContext.Request.Headers["Origin"]}"; + + client.DefaultRequestHeaders.Add("Authorization", auth); var settings = _services.GetRequiredService(); - var origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : $"{_context.HttpContext.Request.Headers["Origin"]}"; + origin = !string.IsNullOrEmpty(origin) ? origin : settings.Origin; if (!string.IsNullOrEmpty(origin)) { client.DefaultRequestHeaders.Add("Origin", origin); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs index 816e0904..54c9eae7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs @@ -21,7 +21,7 @@ public class AgentDocument : MongoBase public List KnowledgeBases { get; set; } public List Profiles { get; set; } public List RoutingRules { get; set; } - public List EventRules { get; set; } + public List Rules { get; set; } public AgentLlmConfigMongoElement? LlmConfig { get; set; } public DateTime CreatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentRuleMongoElement.cs similarity index 68% rename from src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs rename to src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentRuleMongoElement.cs index cadb3c45..1b346768 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentEventRuleMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentRuleMongoElement.cs @@ -2,16 +2,16 @@ using BotSharp.Abstraction.Agents.Models; namespace BotSharp.Plugin.MongoStorage.Models; -public class AgentEventRuleMongoElement +public class AgentRuleMongoElement { public string Name { get; set; } public bool Disabled { get; set; } public string EventName { get; set; } public string EntityType { get; set; } - public static AgentEventRuleMongoElement ToMongoElement(AgentEventRule rule) + public static AgentRuleMongoElement ToMongoElement(AgentRule rule) { - return new AgentEventRuleMongoElement + return new AgentRuleMongoElement { Name = rule.Name, Disabled = rule.Disabled, @@ -20,9 +20,9 @@ public class AgentEventRuleMongoElement }; } - public static AgentEventRule ToDomainElement(AgentEventRuleMongoElement rule) + public static AgentRule ToDomainElement(AgentRuleMongoElement rule) { - return new AgentEventRule + return new AgentRule { Name = rule.Name, Disabled = rule.Disabled, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 92d9db60..af615298 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -61,8 +61,8 @@ public partial class MongoRepository case AgentField.KnowledgeBase: UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); break; - case AgentField.EventRule: - UpdateAgentEventRules(agent.Id, agent.EventRules); + case AgentField.Rule: + UpdateAgentRules(agent.Id, agent.Rules); break; case AgentField.MaxMessageCount: UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); @@ -259,15 +259,15 @@ public partial class MongoRepository _dc.Agents.UpdateOne(filter, update); } - private void UpdateAgentEventRules(string agentId, List rules) + private void UpdateAgentRules(string agentId, List rules) { if (rules == null) return; - var elements = rules?.Select(x => AgentEventRuleMongoElement.ToMongoElement(x))?.ToList() ?? []; + var elements = rules?.Select(x => AgentRuleMongoElement.ToMongoElement(x))?.ToList() ?? []; var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update - .Set(x => x.EventRules, elements) + .Set(x => x.Rules, elements) .Set(x => x.UpdatedTime, DateTime.UtcNow); _dc.Agents.UpdateOne(filter, update); @@ -314,7 +314,7 @@ public partial class MongoRepository .Set(x => x.Samples, agent.Samples) .Set(x => x.Utilities, agent.Utilities.Select(u => AgentUtilityMongoElement.ToMongoElement(u)).ToList()) .Set(x => x.KnowledgeBases, agent.KnowledgeBases.Select(u => AgentKnowledgeBaseMongoElement.ToMongoElement(u)).ToList()) - .Set(x => x.EventRules, agent.EventRules.Select(e => AgentEventRuleMongoElement.ToMongoElement(e)).ToList()) + .Set(x => x.Rules, agent.Rules.Select(e => AgentRuleMongoElement.ToMongoElement(e)).ToList()) .Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig)) .Set(x => x.IsPublic, agent.IsPublic) .Set(x => x.UpdatedTime, DateTime.UtcNow); @@ -472,7 +472,7 @@ public partial class MongoRepository RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [], Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [], KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [], - EventRules = x.EventRules?.Select(e => AgentEventRuleMongoElement.ToMongoElement(e))?.ToList() ?? [], + Rules = x.Rules?.Select(e => AgentRuleMongoElement.ToMongoElement(e))?.ToList() ?? [], CreatedTime = x.CreatedDateTime, UpdatedTime = x.UpdatedDateTime }).ToList(); @@ -565,7 +565,7 @@ public partial class MongoRepository RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [], Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [], KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [], - EventRules = agentDoc.EventRules?.Select(e => AgentEventRuleMongoElement.ToDomainElement(e))?.ToList() ?? [] + Rules = agentDoc.Rules?.Select(e => AgentRuleMongoElement.ToDomainElement(e))?.ToList() ?? [] }; } } From 3b63b8b720cc330f2a067f88070f5220d5548cfe Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 7 Jan 2025 16:30:47 -0600 Subject: [PATCH 4/4] add http request hook --- .../Http/IHttpRequestHook.cs | 8 ++++ .../Users/IAuthenticationHook.cs | 1 - .../Functions/HandleHttpRequestFn.cs | 19 +++------ .../Hooks/BasicHttpRequestHook.cs | 40 +++++++++++++++++++ .../HttpHandlerPlugin.cs | 2 + 5 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Http/IHttpRequestHook.cs create mode 100644 src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/BasicHttpRequestHook.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Http/IHttpRequestHook.cs b/src/Infrastructure/BotSharp.Abstraction/Http/IHttpRequestHook.cs new file mode 100644 index 00000000..18b96e77 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Http/IHttpRequestHook.cs @@ -0,0 +1,8 @@ +using System.Net.Http.Headers; + +namespace BotSharp.Abstraction.Http; + +public interface IHttpRequestHook +{ + void OnAddHttpHeaders(HttpHeaders headers); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs index d953271f..939d509e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Users.Models; -using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; namespace BotSharp.Abstraction.Users; diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs index f9a4342c..52f5075b 100644 --- a/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs @@ -1,5 +1,6 @@ using System.Net.Http; using System.Net.Mime; +using BotSharp.Abstraction.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; @@ -13,7 +14,6 @@ public class HandleHttpRequestFn : IFunctionCallback private readonly IServiceProvider _services; private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; - private readonly IHttpContextAccessor _context; private readonly BotSharpOptions _options; public HandleHttpRequestFn(IServiceProvider services, @@ -25,7 +25,6 @@ public class HandleHttpRequestFn : IFunctionCallback _services = services; _logger = logger; _httpClientFactory = httpClientFactory; - _context = context; _options = options; } @@ -57,7 +56,7 @@ public class HandleHttpRequestFn : IFunctionCallback if (string.IsNullOrEmpty(url)) return null; using var client = _httpClientFactory.CreateClient(); - AddRequestHeaders(client); + PrepareRequestHeaders(client); var (uri, request) = BuildHttpRequest(url, method, content); var response = await client.SendAsync(request); @@ -69,18 +68,12 @@ public class HandleHttpRequestFn : IFunctionCallback return response; } - private void AddRequestHeaders(HttpClient client) + private void PrepareRequestHeaders(HttpClient client) { - var auth = $"{_context.HttpContext.Request.Headers["Authorization"]}"; - var origin = $"{_context.HttpContext.Request.Headers["Origin"]}"; - - client.DefaultRequestHeaders.Add("Authorization", auth); - - var settings = _services.GetRequiredService(); - origin = !string.IsNullOrEmpty(origin) ? origin : settings.Origin; - if (!string.IsNullOrEmpty(origin)) + var hooks = _services.GetServices(); + foreach (var hook in hooks) { - client.DefaultRequestHeaders.Add("Origin", origin); + hook.OnAddHttpHeaders(client.DefaultRequestHeaders); } } diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/BasicHttpRequestHook.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/BasicHttpRequestHook.cs new file mode 100644 index 00000000..5de858f2 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/BasicHttpRequestHook.cs @@ -0,0 +1,40 @@ +using BotSharp.Abstraction.Http; +using Microsoft.AspNetCore.Http; +using System.Net.Http.Headers; + +namespace BotSharp.Plugin.HttpHandler.Hooks; + +public class BasicHttpRequestHook : IHttpRequestHook +{ + private readonly IServiceProvider _services; + private readonly IHttpContextAccessor _context; + + private const string AUTHORIZATION = "Authorization"; + private const string ORIGIN = "Origin"; + + public BasicHttpRequestHook( + IServiceProvider services, + IHttpContextAccessor context) + { + _services = services; + _context = context; + } + + public void OnAddHttpHeaders(HttpHeaders headers) + { + var settings = _services.GetRequiredService(); + + var auth = $"{_context.HttpContext.Request.Headers[AUTHORIZATION]}"; + if (!string.IsNullOrEmpty(auth)) + { + headers.Add(AUTHORIZATION, auth); + } + + var origin = $"{_context.HttpContext.Request.Headers[ORIGIN]}"; + origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : origin; + if (!string.IsNullOrEmpty(origin)) + { + headers.Add(ORIGIN, origin); + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/HttpHandlerPlugin.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/HttpHandlerPlugin.cs index 9c8cab55..a3c85c0b 100644 --- a/src/Plugins/BotSharp.Plugin.HttpHandler/HttpHandlerPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/HttpHandlerPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents; +using BotSharp.Abstraction.Http; using BotSharp.Abstraction.Settings; using Microsoft.Extensions.Configuration; @@ -21,5 +22,6 @@ public class HttpHandlerPlugin : IBotSharpPlugin }); services.AddScoped(); + services.AddScoped(); } }