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

52 lines
1.9 KiB
C#
Raw Normal View History

2023-09-22 20:38:58 +00:00
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Functions.Models;
2023-09-24 16:20:02 +00:00
using BotSharp.Abstraction.Models;
2023-09-22 20:38:58 +00:00
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-09-24 16:20:02 +00:00
public List<NameDesc> Parameters => new List<NameDesc>
2023-09-22 20:38:58 +00:00
{
2023-10-20 03:47:14 +00:00
new NameDesc("reason", "why route to agent"),
new NameDesc("next_action_agent", "agent for next action based on user latest response"),
new NameDesc("user_goal_agent", "agent who can achieve user original goal"),
2023-10-13 20:08:59 +00:00
new NameDesc("args", "useful parameters of next action agent")
2023-09-22 20:38:58 +00:00
};
public bool IsReasoning => false;
public RouteToAgentRoutingHandler(IServiceProvider services, ILogger<RouteToAgentRoutingHandler> logger, RoutingSettings settings)
: base(services, logger, settings)
{
}
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
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);
var message = new RoleDialogModel(AgentRole.Function, inst.Question)
{
FunctionName = inst.Function,
FunctionArgs = JsonSerializer.Serialize(inst),
CurrentAgentId = context.GetCurrentAgentId(),
2023-09-22 20:38:58 +00:00
};
var ret = await function.Execute(message);
var result = await routing.InvokeAgent(context.GetCurrentAgentId());
// Keep last message data for debug
2023-10-23 00:31:49 +00:00
result.Data = result.Data ?? message.Data;
2023-10-13 20:15:27 +00:00
result.FunctionName = result.FunctionName ?? message.FunctionName;
2023-09-22 20:38:58 +00:00
return result;
}
}