BotSharp/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs

51 lines
1.8 KiB
C#
Raw Normal View History

2023-09-22 20:38:58 +00:00
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
namespace BotSharp.Core.Routing.Handlers;
public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
public string Name => "route_to_agent";
public string Description => "Route request to appropriate agent.";
2023-10-28 20:59:26 +00:00
public List<ParameterPropertyDef> Parameters => new List<ParameterPropertyDef>
2023-09-22 20:38:58 +00:00
{
2023-10-28 20:59:26 +00:00
new ParameterPropertyDef("reason", "why route to agent"),
new ParameterPropertyDef("next_action_agent", "agent for next action based on user latest response"),
new ParameterPropertyDef("user_goal_agent", "agent who can achieve user original goal"),
new ParameterPropertyDef("args", "useful parameters of next action agent, format: { }")
{
Type = "object"
}
2023-09-22 20:38:58 +00:00
};
public RouteToAgentRoutingHandler(IServiceProvider services, ILogger<RouteToAgentRoutingHandler> logger, RoutingSettings settings)
: base(services, logger, settings)
{
}
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
2023-09-22 20:38:58 +00:00
{
var context = _services.GetRequiredService<RoutingContext>();
2023-09-22 20:38:58 +00:00
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == inst.Function);
message.FunctionArgs = JsonSerializer.Serialize(inst);
2023-09-22 20:38:58 +00:00
var ret = await function.Execute(message);
2023-10-30 16:48:18 +00:00
var agentId = context.GetCurrentAgentId();
2023-11-08 22:27:42 +00:00
if (inst.IsExecutionOnce)
{
message.Content = inst.Question;
}
2023-10-30 16:48:18 +00:00
ret = await routing.InvokeAgent(agentId, _dialogs);
var response = _dialogs.Last();
inst.Response = response.Content;
2023-10-27 15:18:48 +00:00
return true;
2023-09-22 20:38:58 +00:00
}
}