From 7570108be5d3a700e8ca305393bc67b794dfe30c Mon Sep 17 00:00:00 2001 From: hchen2020 <101423@smsassist.com> Date: Tue, 22 Aug 2023 22:08:14 -0500 Subject: [PATCH] Remove RoutingResult. --- .../Agents/Models/RoutingResult.cs | 11 --- .../Conversations/Models/RoleDialogModel.cs | 5 -- .../BotSharp.Core/Functions/RouteToAgentFn.cs | 84 +++++++++++-------- 3 files changed, 51 insertions(+), 49 deletions(-) delete mode 100644 src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingResult.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingResult.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingResult.cs deleted file mode 100644 index b8ecdf6b..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/RoutingResult.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace BotSharp.Abstraction.Agents.Models; - -public class RoutingResult -{ - public string Result { get; set; } - - public RoutingResult(string result) - { - Result = result; - } -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index bc460159..b681c2c3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -30,11 +30,6 @@ public class RoleDialogModel /// public object ExecutionData { get; set; } - public bool IsConversationEnd { get; set; } - - public bool NeedReloadAgent { get; set; } - public bool StopPropagate { get; set; } - /// /// Channel name /// diff --git a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs index bae33c7e..4c6d1967 100644 --- a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs @@ -1,12 +1,13 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Functions; -using BotSharp.Abstraction.Functions.Models; -using Microsoft.Extensions.Logging; using System.IO; namespace BotSharp.Core.Functions; +/// +/// Router calls this function to set the Active Agent according to the context +/// public class RouteToAgentFn : IFunctionCallback { public string Name => "route_to_agent"; @@ -20,50 +21,67 @@ public class RouteToAgentFn : IFunctionCallback public async Task Execute(RoleDialogModel message) { var args = JsonSerializer.Deserialize(message.FunctionArgs); - var result = new RoutingResult($"Routed to {args.AgentName}"); if (string.IsNullOrEmpty(args.AgentName)) { - result = new RoutingResult($"Can't find {args.AgentName}"); + message.ExecutionResult = $"missing agent name"; } else { - var agentSettings = _services.GetRequiredService(); - var dbSettings = _services.GetRequiredService(); - var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json"); - var routes = JsonSerializer.Deserialize(File.ReadAllText(filePath)); - - var agent = routes.FirstOrDefault(x => x.AgentName.ToLower() == args.AgentName.ToLower()); - if (agent == null) + if (!HasMissingRequiredField(message, out var agentId)) { - result = new RoutingResult($"Can't find agent {args.AgentName}."); + message.CurrentAgentId = agentId; + message.ExecutionResult = $"Routed to {args.AgentName}"; } - else - { - // Check required fields - var jo = JsonSerializer.Deserialize(message.FunctionArgs); - bool hasMissingField = false; - foreach (var field in agent.RequiredFields) - { - if (jo is JsonElement root) - { - if (!root.EnumerateObject().Any(x => x.Name == field)) - { - result = new RoutingResult($"Please provide {field}."); - hasMissingField = true; - break; - } - } - } + } - if (!hasMissingField) + return true; + } + + /// + /// If the target agent needs some required fields but the + /// + /// + private bool HasMissingRequiredField(RoleDialogModel message, out string agentId) + { + var args = JsonSerializer.Deserialize(message.FunctionArgs); + + var routes = GetRoutingTable(); + var agent = routes.FirstOrDefault(x => x.AgentName.ToLower() == args.AgentName.ToLower()); + + if (agent == null) + { + agentId = message.CurrentAgentId; + message.ExecutionResult = $"Can't find agent {args.AgentName}"; + return true; + } + + agentId = agent.AgentId; + + // Check required fields + var jo = JsonSerializer.Deserialize(message.FunctionArgs); + bool hasMissingField = false; + foreach (var field in agent.RequiredFields) + { + if (jo is JsonElement root) + { + if (!root.EnumerateObject().Any(x => x.Name == field)) { - message.CurrentAgentId = agent.AgentId; + message.ExecutionResult = $"missing {field}."; + hasMissingField = true; + break; } } } - message.ExecutionResult = JsonSerializer.Serialize(result); - return true; + return hasMissingField; + } + + private RoutingTable[] GetRoutingTable() + { + var agentSettings = _services.GetRequiredService(); + var dbSettings = _services.GetRequiredService(); + var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json"); + return JsonSerializer.Deserialize(File.ReadAllText(filePath)); } }