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

39 lines
1.1 KiB
C#
Raw Normal View History

2023-10-28 20:59:26 +00:00
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;
}
2023-10-30 16:48:18 +00:00
public async Task<RoleDialogModel> Execute(IRoutingService routing,
2023-10-28 20:59:26 +00:00
FunctionCallFromLlm inst,
2023-10-30 16:48:18 +00:00
RoleDialogModel message,
List<RoleDialogModel> dialogs)
2023-10-28 20:59:26 +00:00
{
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);
2023-10-30 16:48:18 +00:00
// For client display purpose
var response = dialogs.Last();
response.MessageId = message.MessageId;
response.Instruction = inst;
2023-10-28 20:59:26 +00:00
2023-10-30 16:48:18 +00:00
return response;
2023-10-28 20:59:26 +00:00
}
}