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)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 12:51:15 +00:00
|
|
|
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
|
2023-09-22 20:38:58 +00:00
|
|
|
{
|
2023-10-20 23:57:45 +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,
|
2023-10-12 21:42:39 +00:00
|
|
|
FunctionArgs = JsonSerializer.Serialize(inst),
|
2023-10-20 23:57:45 +00:00
|
|
|
CurrentAgentId = context.GetCurrentAgentId(),
|
2023-09-22 20:38:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var ret = await function.Execute(message);
|
|
|
|
|
|
2023-10-20 23:57:45 +00:00
|
|
|
var result = await routing.InvokeAgent(context.GetCurrentAgentId());
|
2023-10-12 21:42:39 +00:00
|
|
|
// Keep last message data for debug
|
|
|
|
|
result.ExecutionData = result.ExecutionData ?? message.ExecutionData;
|
2023-10-13 20:15:27 +00:00
|
|
|
result.FunctionName = result.FunctionName ?? message.FunctionName;
|
2023-09-22 20:38:58 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|