File tree Expand file tree Collapse file tree 5 files changed +80
-2
lines changed
BotSharp.Abstraction/Crontab/Models
data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions Expand file tree Collapse file tree 5 files changed +80
-2
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments