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

50 lines
1.7 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-09-25 22:46:00 +00:00
new NameDesc("agent", "the name of the agent"),
2023-09-24 16:20:02 +00:00
new NameDesc("reason", "why route to this agent"),
2023-09-25 22:46:00 +00:00
new NameDesc("args", "the agent required parameters")
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 function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == inst.Function);
var message = new RoleDialogModel(AgentRole.Function, inst.Question)
{
FunctionName = inst.Function,
FunctionArgs = JsonSerializer.Serialize(new RoutingArgs
{
2023-09-25 22:46:00 +00:00
AgentName = inst.AgentName
2023-09-22 20:38:58 +00:00
}),
};
var ret = await function.Execute(message);
var result = await routing.InvokeAgent(message.CurrentAgentId);
2023-09-22 20:38:58 +00:00
result.ExecutionData = result.ExecutionData ?? message.ExecutionData;
return result;
}
}