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() ?? [] }; } }