2023-08-20 19:02:59 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
2023-08-28 03:50:10 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
2023-08-20 19:02:59 +00:00
|
|
|
|
2023-09-08 23:32:57 +00:00
|
|
|
namespace BotSharp.Core.Routing;
|
2023-08-20 19:02:59 +00:00
|
|
|
|
2023-08-23 03:08:14 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Router calls this function to set the Active Agent according to the context
|
|
|
|
|
/// </summary>
|
2024-03-21 22:04:45 +00:00
|
|
|
public partial class RouteToAgentFn : IFunctionCallback
|
2023-08-20 19:02:59 +00:00
|
|
|
{
|
|
|
|
|
public string Name => "route_to_agent";
|
|
|
|
|
private readonly IServiceProvider _services;
|
2024-02-28 16:21:14 +00:00
|
|
|
private readonly IRoutingContext _context;
|
2023-08-20 19:02:59 +00:00
|
|
|
|
2024-02-28 16:21:14 +00:00
|
|
|
public RouteToAgentFn(IServiceProvider services, IRoutingContext context)
|
2023-08-20 19:02:59 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2023-10-02 20:16:35 +00:00
|
|
|
_context = context;
|
2023-08-20 19:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
2023-08-22 04:49:42 +00:00
|
|
|
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
|
2024-02-28 20:40:59 +00:00
|
|
|
var states = _services.GetRequiredService<IConversationStateService>();
|
2023-08-20 19:02:59 +00:00
|
|
|
|
2023-10-12 21:42:39 +00:00
|
|
|
// Push original task agent
|
|
|
|
|
if (!string.IsNullOrEmpty(args.OriginalAgent) && args.OriginalAgent.Length < 32)
|
|
|
|
|
{
|
2024-02-28 20:40:59 +00:00
|
|
|
// Correct user goal agent to keep orignal task
|
|
|
|
|
var goalAgentInState = states.GetState("user_goal_agent", string.Empty);
|
2024-03-01 05:44:57 +00:00
|
|
|
bool correctToOriginalAgent = false;
|
2024-02-28 20:40:59 +00:00
|
|
|
if (goalAgentInState == string.Empty)
|
|
|
|
|
{
|
|
|
|
|
states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true);
|
|
|
|
|
}
|
|
|
|
|
else if (args.OriginalAgent == args.AgentName && args.OriginalAgent != goalAgentInState)
|
|
|
|
|
{
|
|
|
|
|
// Correct to original agent
|
|
|
|
|
args.OriginalAgent = goalAgentInState;
|
2024-03-01 05:44:57 +00:00
|
|
|
correctToOriginalAgent = true;
|
2024-02-28 20:40:59 +00:00
|
|
|
}
|
|
|
|
|
else if (args.OriginalAgent != args.AgentName && args.OriginalAgent != goalAgentInState)
|
|
|
|
|
{
|
|
|
|
|
// Correct to original agent
|
|
|
|
|
states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 21:42:39 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-11-27 22:20:49 +00:00
|
|
|
var filter = new AgentFilter { AgentName = args.OriginalAgent };
|
|
|
|
|
var originalAgent = db.GetAgents(filter).FirstOrDefault();
|
2023-10-12 21:42:39 +00:00
|
|
|
if (originalAgent != null)
|
|
|
|
|
{
|
2024-03-01 05:44:57 +00:00
|
|
|
_context.Push(originalAgent.Id, $"user goal agent{(correctToOriginalAgent ? " & is corrected" : "")}");
|
2023-10-12 21:42:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-28 20:40:59 +00:00
|
|
|
|
|
|
|
|
// Push next action agent
|
|
|
|
|
if (!string.IsNullOrEmpty(args.AgentName) && args.AgentName.Length < 32)
|
2023-10-12 21:42:39 +00:00
|
|
|
{
|
2024-03-05 17:36:48 +00:00
|
|
|
_context.Push(args.AgentName, args.Reason);
|
|
|
|
|
states.SetState("next_action_agent", args.AgentName, isNeedVersion: true);
|
2023-10-12 21:42:39 +00:00
|
|
|
}
|
2023-10-02 20:16:35 +00:00
|
|
|
|
2023-08-22 04:49:42 +00:00
|
|
|
if (string.IsNullOrEmpty(args.AgentName))
|
2023-08-20 19:02:59 +00:00
|
|
|
{
|
2023-10-23 00:31:49 +00:00
|
|
|
message.Content = $"missing agent name";
|
2023-08-20 19:02:59 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-10 21:53:04 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-11-27 22:20:49 +00:00
|
|
|
var filter = new AgentFilter { AgentName = args.AgentName };
|
|
|
|
|
var targetAgent = db.GetAgents(filter).FirstOrDefault();
|
2023-10-10 21:53:04 +00:00
|
|
|
if (targetAgent == null)
|
|
|
|
|
{
|
2023-10-23 00:31:49 +00:00
|
|
|
message.Data = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
|
2023-10-10 21:53:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-13 21:17:40 +00:00
|
|
|
if (targetAgent.Disabled)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 22:04:45 +00:00
|
|
|
var routing = _services.GetRequiredService<IRoutingService>();
|
|
|
|
|
var missingfield = routing.HasMissingRequiredField(message, out var agentId);
|
2023-08-23 18:32:13 +00:00
|
|
|
if (missingfield && message.CurrentAgentId != agentId)
|
|
|
|
|
{
|
2024-03-01 05:44:57 +00:00
|
|
|
// Stack redirection agent
|
2024-03-05 17:36:48 +00:00
|
|
|
_context.Push(agentId, reason: $"REDIRECTION {message.Content}");
|
2023-08-22 04:49:42 +00:00
|
|
|
}
|
2023-08-23 03:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-28 20:40:59 +00:00
|
|
|
message.CurrentAgentId = _context.GetCurrentAgentId();
|
2023-10-02 20:16:35 +00:00
|
|
|
|
2023-08-23 03:08:14 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-20 19:02:59 +00:00
|
|
|
}
|