2024-01-26 22:23:10 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
|
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Routing.Functions;
|
|
|
|
|
|
|
|
|
|
public class FallbackToRouterFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
public string Name => "fallback_to_router";
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
public FallbackToRouterFn(IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var agents = await agentService.GetAgents(new AgentFilter
|
|
|
|
|
{
|
|
|
|
|
AgentName = args.AgentName
|
|
|
|
|
});
|
|
|
|
|
var targetAgent = agents.Items.FirstOrDefault();
|
|
|
|
|
if (targetAgent == null)
|
|
|
|
|
{
|
|
|
|
|
message.Content = $"Can't find routing agent {args.AgentName}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-24 17:33:41 +00:00
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var dialogs = conv.GetDialogHistory();
|
2024-01-26 22:23:10 +00:00
|
|
|
|
2024-03-24 17:33:41 +00:00
|
|
|
var routing = _services.GetRequiredService<IRoutingService>();
|
|
|
|
|
routing.Context.Replace(targetAgent.Id);
|
2024-01-26 22:23:10 +00:00
|
|
|
message.CurrentAgentId = targetAgent.Id;
|
2024-03-24 17:33:41 +00:00
|
|
|
|
2024-04-30 21:25:58 +00:00
|
|
|
var response = await routing.InstructLoop(message, dialogs, null);
|
2024-01-26 22:23:10 +00:00
|
|
|
|
|
|
|
|
message.Content = response.Content;
|
|
|
|
|
message.StopCompletion = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|