2023-12-07 03:06:09 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
2025-03-14 16:13:13 +00:00
|
|
|
using BotSharp.Abstraction.Templating;
|
|
|
|
|
|
2023-12-07 03:06:09 +00:00
|
|
|
namespace BotSharp.Core.Routing;
|
|
|
|
|
|
|
|
|
|
public partial class RoutingService
|
|
|
|
|
{
|
2024-09-12 10:49:45 +00:00
|
|
|
public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
|
2023-12-07 03:06:09 +00:00
|
|
|
{
|
|
|
|
|
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == name);
|
2025-03-14 22:04:54 +00:00
|
|
|
|
|
|
|
|
var isFillDummyContent = false;
|
|
|
|
|
var dummyFuncResponse = string.Empty;
|
2024-03-01 05:44:57 +00:00
|
|
|
if (function == null)
|
|
|
|
|
{
|
2025-03-14 22:04:54 +00:00
|
|
|
dummyFuncResponse = await GetDummyFunctionOutput(name, message);
|
|
|
|
|
isFillDummyContent = !string.IsNullOrEmpty(dummyFuncResponse);
|
|
|
|
|
if (!isFillDummyContent)
|
2025-03-14 16:13:13 +00:00
|
|
|
{
|
2025-03-14 22:04:54 +00:00
|
|
|
message.StopCompletion = true;
|
|
|
|
|
message.Content = $"Can't find function implementation of {name}.";
|
|
|
|
|
_logger.LogError(message.Content);
|
|
|
|
|
return false;
|
2025-03-14 16:13:13 +00:00
|
|
|
}
|
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;
|
2024-03-01 05:44:57 +00:00
|
|
|
|
2024-12-07 21:47:17 +00:00
|
|
|
var hooks = _services
|
|
|
|
|
.GetRequiredService<ConversationHookProvider>()
|
|
|
|
|
.HooksOrderByPriority;
|
2024-03-01 05:44:57 +00:00
|
|
|
|
2024-09-12 10:49:45 +00:00
|
|
|
var progressService = _services.GetService<IConversationProgressService>();
|
|
|
|
|
|
2024-03-01 05:44:57 +00:00
|
|
|
// Before executing functions
|
2025-03-14 22:04:54 +00:00
|
|
|
if (!isFillDummyContent)
|
|
|
|
|
{
|
|
|
|
|
clonedMessage.Indication = await function.GetIndication(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
clonedMessage.Indication = "Running";
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 10:49:45 +00:00
|
|
|
if (progressService?.OnFunctionExecuting != null)
|
2024-04-30 21:25:58 +00:00
|
|
|
{
|
2024-09-12 10:49:45 +00:00
|
|
|
await progressService.OnFunctionExecuting(clonedMessage);
|
2024-04-30 21:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2025-03-14 22:04:54 +00:00
|
|
|
if (!isFillDummyContent)
|
|
|
|
|
{
|
|
|
|
|
result = await function.Execute(clonedMessage);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
clonedMessage.Content = dummyFuncResponse;
|
|
|
|
|
result = true;
|
|
|
|
|
}
|
2024-04-06 23:33:49 +00:00
|
|
|
|
|
|
|
|
// After functions have been executed
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.OnFunctionExecuted(clonedMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
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
|
|
|
|
2024-02-27 20:59:08 +00:00
|
|
|
return result;
|
2023-12-07 03:06:09 +00:00
|
|
|
}
|
2025-03-14 16:13:13 +00:00
|
|
|
|
2025-03-14 22:04:54 +00:00
|
|
|
private async Task<string?> GetDummyFunctionOutput(string functionName, RoleDialogModel message)
|
2025-03-14 16:13:13 +00:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(message.CurrentAgentId))
|
|
|
|
|
{
|
2025-03-14 22:04:54 +00:00
|
|
|
return null;
|
2025-03-14 16:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var agent = await agentService.GetAgent(message.CurrentAgentId);
|
|
|
|
|
var found = agent?.Functions?.FirstOrDefault(x => x.Name == functionName);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(found?.Output))
|
|
|
|
|
{
|
2025-03-14 22:04:54 +00:00
|
|
|
return null;
|
2025-03-14 16:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var render = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
|
|
|
|
|
var dict = new Dictionary<string, object>();
|
|
|
|
|
foreach (var item in state.GetStates())
|
|
|
|
|
{
|
|
|
|
|
dict[item.Key] = item.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var text = render.Render(found.Output, dict);
|
2025-03-14 22:04:54 +00:00
|
|
|
return text;
|
2025-03-14 16:13:13 +00:00
|
|
|
}
|
2023-12-07 03:06:09 +00:00
|
|
|
}
|