diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/Models/TaskWaitArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/Models/TaskWaitArgs.cs new file mode 100644 index 000000000..9eb728542 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Crontab/Models/TaskWaitArgs.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Abstraction.Crontab.Models; + +public class TaskWaitArgs +{ + + [JsonPropertyName("delay_time")] + public int DelayTime { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj b/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj index ff6c2368a..f406ac977 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj +++ b/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj @@ -16,6 +16,9 @@ + + PreserveNewest + PreserveNewest diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Functions/TaskWaitFn.cs b/src/Infrastructure/BotSharp.Core.Crontab/Functions/TaskWaitFn.cs new file mode 100644 index 000000000..0fb68dc94 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Crontab/Functions/TaskWaitFn.cs @@ -0,0 +1,44 @@ +using BotSharp.Core.Crontab.Hooks; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace BotSharp.Core.Crontab.Functions; + +public class TaskWaitFn : IFunctionCallback +{ + public string Name => $"{CrontabUtilityHook.PREFIX}task_wait"; + + private readonly ILogger _logger; + public TaskWaitFn(ILogger logger) + { + _logger = logger; + } + public async Task Execute(RoleDialogModel message) + { + try + { + var args = JsonSerializer.Deserialize(message.FunctionArgs); + if (args != null && args.DelayTime > 0) + { + await Task.Delay(args.DelayTime * 1000); + } + message.Content = "wait task completed"; + } + catch (JsonException jsonEx) + { + message.Content = "Invalid function arguments format."; + _logger.LogError(jsonEx, "Json deserialization failed."); + } + catch (Exception ex) + { + message.Content = "Unable to perform delay task"; + _logger.LogError(ex, "crontab wait task failed."); + } + return true; + } +} diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs b/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs index ed150861a..7fed754a9 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs @@ -7,7 +7,8 @@ public class CrontabUtilityHook : IAgentUtilityHook { public const string PREFIX = "util-crontab-"; private const string SCHEDULE_TASK_FN = $"{PREFIX}schedule_task"; - + private const string TASK_WAIT_FN = $"{PREFIX}task_wait"; + public void AddUtilities(List utilities) { var items = new List @@ -15,7 +16,7 @@ public void AddUtilities(List utilities) new AgentUtility { Name = UtilityName.ScheduleTask, - Functions = [new(SCHEDULE_TASK_FN)], + Functions = [new(SCHEDULE_TASK_FN), new(TASK_WAIT_FN)], Templates = [new($"{SCHEDULE_TASK_FN}.fn")] } }; diff --git a/src/Infrastructure/BotSharp.Core.Crontab/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-crontab-task_wait.json b/src/Infrastructure/BotSharp.Core.Crontab/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-crontab-task_wait.json new file mode 100644 index 000000000..b3ddd1672 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Crontab/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-crontab-task_wait.json @@ -0,0 +1,16 @@ +{ + "name": "util-crontab-task_wait", + "description": "wait for a peroid of time then process", + "parameters": { + "type": "object", + "properties": { + "delay_time": { + "type": "number", + "description": "delay time in seconds" + } + }, + "required": [ + "delay_time" + ] + } +} \ No newline at end of file