diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingTable.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingTable.cs index 2feedcf8..fd2ba8f7 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingTable.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingTable.cs @@ -13,6 +13,9 @@ public class RoutingTable [JsonPropertyName("required")] public List RequiredFields { get; set; } + [JsonPropertyName("redirect_to")] + public string RedirectTo { get; set; } + public override string ToString() { return AgentName; diff --git a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs index 4c6d1967..c34cc189 100644 --- a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs @@ -28,7 +28,12 @@ public class RouteToAgentFn : IFunctionCallback } else { - if (!HasMissingRequiredField(message, out var agentId)) + var missingfield = HasMissingRequiredField(message, out var agentId); + if (missingfield && message.CurrentAgentId != agentId) + { + message.CurrentAgentId = agentId; + } + else { message.CurrentAgentId = agentId; message.ExecutionResult = $"Routed to {args.AgentName}"; @@ -47,21 +52,21 @@ public class RouteToAgentFn : IFunctionCallback var args = JsonSerializer.Deserialize(message.FunctionArgs); var routes = GetRoutingTable(); - var agent = routes.FirstOrDefault(x => x.AgentName.ToLower() == args.AgentName.ToLower()); + var routingRule = routes.FirstOrDefault(x => x.AgentName.ToLower() == args.AgentName.ToLower()); - if (agent == null) + if (routingRule == null) { agentId = message.CurrentAgentId; message.ExecutionResult = $"Can't find agent {args.AgentName}"; return true; } - agentId = agent.AgentId; + agentId = routingRule.AgentId; // Check required fields var jo = JsonSerializer.Deserialize(message.FunctionArgs); bool hasMissingField = false; - foreach (var field in agent.RequiredFields) + foreach (var field in routingRule.RequiredFields) { if (jo is JsonElement root) { @@ -71,9 +76,21 @@ public class RouteToAgentFn : IFunctionCallback hasMissingField = true; break; } + else if (root.EnumerateObject().Any(x => x.Name == field) && + string.IsNullOrEmpty(root.EnumerateObject().FirstOrDefault(x => x.Name == field).Value.ToString())) + { + message.ExecutionResult = $"missing {field}."; + hasMissingField = true; + break; + } } } + if (hasMissingField && !string.IsNullOrEmpty(routingRule.RedirectTo)) + { + agentId = routingRule.RedirectTo; + } + return hasMissingField; }