Skip to content

Commit 2ca7cf2

Browse files
authored
Merge pull request #1039 from ChenGong-lessen/cgong/features/NC-4824
add function util-crontab-task_wait
2 parents 83bb000 + 4a220ab commit 2ca7cf2

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace BotSharp.Abstraction.Crontab.Models;
8+
9+
public class TaskWaitArgs
10+
{
11+
12+
[JsonPropertyName("delay_time")]
13+
public int DelayTime { get; set; }
14+
}

src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-task_wait.json">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</Content>
1922
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json">
2023
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2124
</Content>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using BotSharp.Core.Crontab.Hooks;
2+
using Microsoft.Extensions.Logging;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Text.Json.Serialization;
8+
using System.Threading.Tasks;
9+
10+
namespace BotSharp.Core.Crontab.Functions;
11+
12+
public class TaskWaitFn : IFunctionCallback
13+
{
14+
public string Name => $"{CrontabUtilityHook.PREFIX}task_wait";
15+
16+
private readonly ILogger<TaskWaitFn> _logger;
17+
public TaskWaitFn(ILogger<TaskWaitFn> logger)
18+
{
19+
_logger = logger;
20+
}
21+
public async Task<bool> Execute(RoleDialogModel message)
22+
{
23+
try
24+
{
25+
var args = JsonSerializer.Deserialize<TaskWaitArgs>(message.FunctionArgs);
26+
if (args != null && args.DelayTime > 0)
27+
{
28+
await Task.Delay(args.DelayTime * 1000);
29+
}
30+
message.Content = "wait task completed";
31+
}
32+
catch (JsonException jsonEx)
33+
{
34+
message.Content = "Invalid function arguments format.";
35+
_logger.LogError(jsonEx, "Json deserialization failed.");
36+
}
37+
catch (Exception ex)
38+
{
39+
message.Content = "Unable to perform delay task";
40+
_logger.LogError(ex, "crontab wait task failed.");
41+
}
42+
return true;
43+
}
44+
}

src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ public class CrontabUtilityHook : IAgentUtilityHook
77
{
88
public const string PREFIX = "util-crontab-";
99
private const string SCHEDULE_TASK_FN = $"{PREFIX}schedule_task";
10-
10+
private const string TASK_WAIT_FN = $"{PREFIX}task_wait";
11+
1112
public void AddUtilities(List<AgentUtility> utilities)
1213
{
1314
var items = new List<AgentUtility>
1415
{
1516
new AgentUtility
1617
{
1718
Name = UtilityName.ScheduleTask,
18-
Functions = [new(SCHEDULE_TASK_FN)],
19+
Functions = [new(SCHEDULE_TASK_FN), new(TASK_WAIT_FN)],
1920
Templates = [new($"{SCHEDULE_TASK_FN}.fn")]
2021
}
2122
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "util-crontab-task_wait",
3+
"description": "wait for a peroid of time then process",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
"delay_time": {
8+
"type": "number",
9+
"description": "delay time in seconds"
10+
}
11+
},
12+
"required": [
13+
"delay_time"
14+
]
15+
}
16+
}

0 commit comments

Comments
 (0)