BotSharp/src/Infrastructure/BotSharp.Core/Planning/InstructExecutor.cs

39 lines
1.1 KiB
C#

using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Routing;
namespace BotSharp.Core.Planning;
public class InstructExecutor : IExecutor
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public InstructExecutor(IServiceProvider services, ILogger<InstructExecutor> logger)
{
_services = services;
_logger = logger;
}
public async Task<RoleDialogModel> Execute(IRoutingService routing,
FunctionCallFromLlm inst,
RoleDialogModel message,
List<RoleDialogModel> dialogs)
{
message.Instruction = inst;
var handlers = _services.GetServices<IRoutingHandler>();
var handler = handlers.FirstOrDefault(x => x.Name == inst.Function);
handler.SetDialogs(dialogs);
var handled = await handler.Handle(routing, inst, message);
// For client display purpose
var response = dialogs.Last();
response.MessageId = message.MessageId;
response.Instruction = inst;
return response;
}
}