Merge pull request #1039 from ChenGong-lessen/cgong/features/NC-4824
add function util-crontab-task_wait
This commit is contained in:
commit
2ca7cf236f
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -16,6 +16,9 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-task_wait.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
|
|
|||
|
|
@ -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<TaskWaitFn> _logger;
|
||||
public TaskWaitFn(ILogger<TaskWaitFn> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
try
|
||||
{
|
||||
var args = JsonSerializer.Deserialize<TaskWaitArgs>(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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<AgentUtility> utilities)
|
||||
{
|
||||
var items = new List<AgentUtility>
|
||||
|
|
@ -15,7 +16,7 @@ public class CrontabUtilityHook : IAgentUtilityHook
|
|||
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")]
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue