2025-08-04 19:21:11 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
2025-07-28 22:41:28 +00:00
|
|
|
using BotSharp.Core.MessageHub;
|
2025-05-01 08:47:16 +00:00
|
|
|
using BotSharp.Core.Routing.Executor;
|
2025-03-14 16:13:13 +00:00
|
|
|
|
2023-12-07 03:06:09 +00:00
|
|
|
namespace BotSharp.Core.Routing;
|
|
|
|
|
|
|
|
|
|
public partial class RoutingService
|
|
|
|
|
{
|
2025-08-04 19:21:11 +00:00
|
|
|
public async Task<bool> InvokeFunction(string name, RoleDialogModel message, InvokeFunctionOptions? options = null)
|
2023-12-07 03:06:09 +00:00
|
|
|
{
|
2025-08-04 19:21:11 +00:00
|
|
|
options ??= InvokeFunctionOptions.Default();
|
2025-05-01 08:47:16 +00:00
|
|
|
var currentAgentId = message.CurrentAgentId;
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var agent = await agentService.GetAgent(currentAgentId);
|
|
|
|
|
|
2025-05-15 22:46:36 +00:00
|
|
|
var funcExecutor = FunctionExecutorFactory.Create(_services, name, agent);
|
2025-05-01 08:47:16 +00:00
|
|
|
if (funcExecutor == null)
|
2024-03-01 05:44:57 +00:00
|
|
|
{
|
2025-05-01 08:47:16 +00:00
|
|
|
message.StopCompletion = true;
|
|
|
|
|
message.Content = $"Can't find function implementation of {name}.";
|
|
|
|
|
_logger.LogError(message.Content);
|
|
|
|
|
return false;
|
2024-03-01 05:44:57 +00:00
|
|
|
}
|
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;
|
2025-05-01 08:47:16 +00:00
|
|
|
clonedMessage.Indication = await funcExecutor.GetIndicatorAsync(message);
|
2025-03-14 22:04:54 +00:00
|
|
|
|
2025-07-31 22:48:51 +00:00
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
2025-07-29 18:55:53 +00:00
|
|
|
var messageHub = _services.GetRequiredService<MessageHub<HubObserveData<RoleDialogModel>>>();
|
2025-07-28 22:41:28 +00:00
|
|
|
messageHub.Push(new()
|
2024-04-30 21:25:58 +00:00
|
|
|
{
|
2025-07-28 22:41:28 +00:00
|
|
|
EventName = ChatEvent.OnIndicationReceived,
|
|
|
|
|
Data = clonedMessage,
|
2025-08-01 18:23:46 +00:00
|
|
|
RefId = conv.ConversationId
|
2025-07-28 22:41:28 +00:00
|
|
|
});
|
|
|
|
|
|
2025-05-16 01:19:55 +00:00
|
|
|
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(clonedMessage.CurrentAgentId);
|
2024-03-01 05:44:57 +00:00
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
2025-04-20 23:24:13 +00:00
|
|
|
hook.SetAgent(agent);
|
2025-08-04 19:21:11 +00:00
|
|
|
await hook.OnFunctionExecuting(clonedMessage, options);
|
2024-03-01 05:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-05-01 08:47:16 +00:00
|
|
|
result = await funcExecutor.ExecuteAsync(clonedMessage);
|
2024-04-06 23:33:49 +00:00
|
|
|
|
|
|
|
|
// After functions have been executed
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
2025-08-04 19:21:11 +00:00
|
|
|
await hook.OnFunctionExecuted(clonedMessage, options);
|
2024-04-06 23:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set result to original message
|
2025-02-11 03:51:45 +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;
|
2024-10-22 11:31:21 +00:00
|
|
|
// Don't copy payload
|
|
|
|
|
// message.Payload = clonedMessage.Payload;
|
2024-04-06 23:33:49 +00:00
|
|
|
message.StopCompletion = clonedMessage.StopCompletion;
|
|
|
|
|
message.RichContent = clonedMessage.RichContent;
|
|
|
|
|
message.Data = clonedMessage.Data;
|
2025-09-05 06:17:18 +00:00
|
|
|
message.AdditionalMessageWrapper = clonedMessage.AdditionalMessageWrapper;
|
2024-03-01 05:44:57 +00:00
|
|
|
}
|
2024-03-21 22:04:45 +00:00
|
|
|
catch (JsonException ex)
|
|
|
|
|
{
|
2025-08-01 18:23:46 +00:00
|
|
|
_logger.LogError(ex, $"The input does not contain any JSON tokens:\r\n{message.Content}\r\n{ex.Message}");
|
2024-03-21 22:04:45 +00:00
|
|
|
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;
|
2025-08-01 18:23:46 +00:00
|
|
|
_logger.LogError(ex, ex.ToString());
|
2024-03-01 05:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure content has been populated
|
|
|
|
|
if (string.IsNullOrEmpty(message.Content) && message.Data != null)
|
|
|
|
|
{
|
|
|
|
|
message.Content = JsonSerializer.Serialize(message.Data);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 20:59:08 +00:00
|
|
|
return result;
|
2023-12-07 03:06:09 +00:00
|
|
|
}
|
|
|
|
|
}
|