BotSharp/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs

88 lines
2.8 KiB
C#
Raw Normal View History

2023-12-07 03:06:09 +00:00
using BotSharp.Abstraction.Functions;
namespace BotSharp.Core.Routing;
public partial class RoutingService
{
2024-04-30 21:25:58 +00:00
public async Task<bool> InvokeFunction(string name, RoleDialogModel message, Func<RoleDialogModel, Task>? onFunctionExecuting = null)
2023-12-07 03:06:09 +00:00
{
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == name);
2024-03-01 05:44:57 +00:00
if (function == null)
{
message.StopCompletion = true;
2024-05-10 19:29:46 +00:00
message.Content = $"Can't find function implementation of {name}.";
2024-03-01 05:44:57 +00:00
_logger.LogError(message.Content);
return false;
}
2023-12-07 03:06:09 +00:00
2024-04-06 23:33:49 +00:00
// Clone message
var clonedMessage = RoleDialogModel.From(message);
clonedMessage.FunctionName = name;
2024-03-01 05:44:57 +00:00
var hooks = _services.GetServices<IConversationHook>()
.OrderBy(x => x.Priority)
.ToList();
// Before executing functions
2024-04-30 21:25:58 +00:00
clonedMessage.Indication = function.Indication;
if (onFunctionExecuting != null)
{
await onFunctionExecuting(clonedMessage);
}
2024-03-01 05:44:57 +00:00
foreach (var hook in hooks)
{
2024-04-06 23:33:49 +00:00
await hook.OnFunctionExecuting(clonedMessage);
2024-03-01 05:44:57 +00:00
}
bool result = false;
try
{
2024-04-06 23:33:49 +00:00
result = await function.Execute(clonedMessage);
// After functions have been executed
foreach (var hook in hooks)
{
await hook.OnFunctionExecuted(clonedMessage);
}
// Set result to original message
2024-07-11 21:43:03 +00:00
message.Role = clonedMessage.Role;
2024-04-06 23:33:49 +00:00
message.PostbackFunctionName = clonedMessage.PostbackFunctionName;
message.CurrentAgentId = clonedMessage.CurrentAgentId;
message.Content = clonedMessage.Content;
message.StopCompletion = clonedMessage.StopCompletion;
message.RichContent = clonedMessage.RichContent;
message.Data = clonedMessage.Data;
2024-03-01 05:44:57 +00:00
}
2024-03-21 22:04:45 +00:00
catch (JsonException ex)
{
_logger.LogError($"The input does not contain any JSON tokens:\r\n{message.Content}");
message.StopCompletion = true;
message.Content = ex.Message;
}
2024-03-01 05:44:57 +00:00
catch (Exception ex)
{
message.StopCompletion = true;
message.Content = ex.Message;
_logger.LogError(ex.ToString());
}
// Make sure content has been populated
if (string.IsNullOrEmpty(message.Content) && message.Data != null)
{
message.Content = JsonSerializer.Serialize(message.Data);
}
2024-03-28 19:09:13 +00:00
// Save to Storage as well
2024-04-06 23:33:49 +00:00
/*if (!message.StopCompletion && message.FunctionName != "route_to_agent")
2024-03-28 20:38:16 +00:00
{
var storage = _services.GetRequiredService<IConversationStorage>();
storage.Append(Context.ConversationId, message);
2024-04-06 23:33:49 +00:00
}*/
2024-03-28 19:09:13 +00:00
return result;
2023-12-07 03:06:09 +00:00
}
}