BotSharp/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs

140 lines
5 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.Functions;
2023-09-18 18:13:49 +00:00
using BotSharp.Abstraction.Repositories;
2023-08-28 03:50:10 +00:00
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Routing;
2023-08-23 03:08:14 +00:00
/// <summary>
/// Router calls this function to set the Active Agent according to the context
/// </summary>
public class RouteToAgentFn : IFunctionCallback
{
public string Name => "route_to_agent";
private readonly IServiceProvider _services;
public RouteToAgentFn(IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
if (string.IsNullOrEmpty(args.AgentName))
{
2023-08-23 03:08:14 +00:00
message.ExecutionResult = $"missing agent name";
}
else
{
2023-09-09 15:37:38 +00:00
var missingfield = HasMissingRequiredField(message, out var agentId);
2023-08-23 18:32:13 +00:00
if (missingfield && message.CurrentAgentId != agentId)
{
message.CurrentAgentId = agentId;
}
else
{
2023-09-18 18:13:49 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.Agents.FirstOrDefault(x => x.Name.ToLower() == args.AgentName.ToLower());
if (record != null)
{
message.CurrentAgentId = record.Id;
message.ExecutionResult = $"Routing to {args.AgentName}";
}
}
2023-08-23 03:08:14 +00:00
}
// Set default execution data
message.ExecutionData = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
2023-08-23 03:08:14 +00:00
return true;
}
/// <summary>
/// If the target agent needs some required fields but the
/// </summary>
/// <returns></returns>
2023-09-09 15:37:38 +00:00
private bool HasMissingRequiredField(RoleDialogModel message, out string agentId)
2023-08-23 03:08:14 +00:00
{
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
var router = _services.GetRequiredService<IAgentRouting>();
2023-08-23 03:08:14 +00:00
2023-09-18 18:13:49 +00:00
var routingRules = router.GetRulesByName(args.AgentName);
if (routingRules == null || !routingRules.Any())
2023-08-23 03:08:14 +00:00
{
agentId = message.CurrentAgentId;
2023-09-18 18:13:49 +00:00
return false;
2023-08-23 03:08:14 +00:00
}
2023-09-18 18:13:49 +00:00
agentId = routingRules.First().AgentId;
// Add routed agent
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "route_to", agentId);
2023-08-23 03:08:14 +00:00
// Check required fields
var root = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
var missingFields = new List<string>();
2023-09-18 18:13:49 +00:00
foreach (var field in routingRules.Where(x => x.Required).Select(x => x.Field))
2023-08-23 03:08:14 +00:00
{
if (!root.EnumerateObject().Any(x => x.Name == field))
2023-08-23 03:08:14 +00:00
{
missingFields.Add(field);
}
else if (root.EnumerateObject().Any(x => x.Name == field) &&
string.IsNullOrEmpty(root.EnumerateObject().FirstOrDefault(x => x.Name == field).Value.ToString()))
{
missingFields.Add(field);
}
}
// Check if states contains the field according conversation context.
var states = _services.GetRequiredService<IConversationStateService>();
foreach (var field in missingFields.ToList())
{
if (!string.IsNullOrEmpty(states.GetState(field)))
{
var value = states.GetState(field);
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, field, value);
missingFields.Remove(field);
}
}
if (missingFields.Any())
2023-08-23 18:32:13 +00:00
{
// Add field to args
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "missing_fields", missingFields);
2023-09-18 18:13:49 +00:00
message.ExecutionResult = $"missing some information: {string.Join(',', missingFields)}";
2023-09-20 22:08:14 +00:00
message.Content = message.ExecutionResult;
// Handle redirect
2023-09-18 18:13:49 +00:00
var routingRule = routingRules.FirstOrDefault(x => missingFields.Contains(x.Field));
if (!string.IsNullOrEmpty(routingRule.RedirectTo))
{
2023-09-18 18:13:49 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.Agents.First(x => x.Id == routingRule.RedirectTo);
// Add redirected agent
2023-09-18 18:13:49 +00:00
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "redirect_to", record.Name);
2023-09-20 22:08:14 +00:00
agentId = routingRule.RedirectTo;
}
2023-09-15 01:19:32 +00:00
else
{
// back to router
agentId = message.CurrentAgentId;
}
2023-08-23 18:32:13 +00:00
}
return missingFields.Any();
}
private string AppendPropertyToArgs(string args, string key, string value)
{
return args.Substring(0, args.Length - 1) + $", \"{key}\": \"{value}\"" + "}";
}
private string AppendPropertyToArgs(string args, string key, IEnumerable<string> values)
{
string fields = string.Join(",", values.Select(x => $"\"{x}\""));
return args.Substring(0, args.Length - 1) + $", \"{key}\": [{fields}]" + "}";
2023-08-23 03:08:14 +00:00
}
}