BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.CallFunctions.cs

52 lines
1.4 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.Functions;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
2023-09-04 02:45:31 +00:00
public async Task CallFunctions(RoleDialogModel msg)
{
// Invoke functions
var functions = _services.GetServices<IFunctionCallback>()
.Where(x => x.Name == msg.FunctionName)
.ToList();
if (functions.Count == 0)
{
msg.Content = $"Can't find function implementation of {msg.FunctionName}.";
_logger.LogError(msg.Content);
return;
}
var hooks = _services.GetServices<IConversationHook>()
.OrderBy(x => x.Priority)
.ToList();
foreach (var fn in functions)
{
// Before executing functions
foreach (var hook in hooks)
{
await hook.OnFunctionExecuting(msg);
}
2023-08-24 12:12:27 +00:00
try
{
// Execute function
await fn.Execute(msg);
}
catch (Exception ex)
{
msg.ExecutionResult = ex.Message;
_logger.LogError(msg.ExecutionResult);
}
// After functions have been executed
foreach (var hook in hooks)
{
await hook.OnFunctionExecuted(msg);
}
}
}
}