Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
44 changes: 44 additions & 0 deletions src/Infrastructure/BotSharp.Core.Crontab/Functions/TaskWaitFn.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ 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>
{
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")]
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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