Skip to content

Commit c387ea0

Browse files
authored
Merge branch 'SciSharp:master' into master
2 parents dca40f7 + 8d176b5 commit c387ea0

File tree

27 files changed

+275
-55
lines changed

27 files changed

+275
-55
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,4 @@ public class BuiltInAgentId
5656
/// Translates user-defined natural language rules into programmatic code
5757
/// </summary>
5858
public const string RuleEncoder = "6acfb93c-3412-402e-9ba5-c5d3cd8f0161";
59-
60-
/// <summary>
61-
/// Schedule job
62-
/// </summary>
63-
public const string Crontab = "c2o139da-a62a-4355-8605-fdf0ffaca58e";
6459
}

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
using BotSharp.Abstraction.Functions.Models;
33
using BotSharp.Abstraction.Messaging;
44
using BotSharp.Abstraction.Messaging.Models.RichContent;
5+
using System.Diagnostics;
56

67
namespace BotSharp.Abstraction.Conversations.Models;
78

9+
[DebuggerStepThrough]
810
public class RoleDialogModel : ITrackableMessage
911
{
1012
/// <summary>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<None Remove="data\agents\c2o139da-a62a-4355-8605-fdf0ffaca58e\agent.json" />
10+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json" />
11+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid" />
1112
</ItemGroup>
1213

1314
<ItemGroup>
14-
<Content Include="data\agents\c2o139da-a62a-4355-8605-fdf0ffaca58e\agent.json">
15+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
</Content>
18+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid">
1519
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1620
</Content>
1721
</ItemGroup>

src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using BotSharp.Core.Crontab.Hooks;
18+
1719
namespace BotSharp.Core.Crontab;
1820

1921
/// <summary>
@@ -27,13 +29,9 @@ public class CrontabPlugin : IBotSharpPlugin
2729
public string Description => "Crontab plugin is a time-based job scheduler in agent framework. The cron system is used to trigger AI Agent to do specific task periodically.";
2830
public string IconUrl => "https://icon-library.com/images/stop-watch-icon/stop-watch-icon-10.jpg";
2931

30-
public string[] AgentIds =
31-
[
32-
BuiltInAgentId.Crontab
33-
];
34-
3532
public void RegisterDI(IServiceCollection services, IConfiguration config)
3633
{
34+
services.AddScoped<IAgentUtilityHook, CrontabUtilityHook>();
3735
services.AddScoped<ICrontabService, CrontabService>();
3836
services.AddHostedService<CrontabWatcher>();
3937
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Core.Crontab.Enum;
2+
3+
public class UtilityName
4+
{
5+
public const string ScheduleTask = "crontab.schedule-task";
6+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using BotSharp.Abstraction.Routing;
2+
using BotSharp.Abstraction.Users;
3+
using BotSharp.Core.Crontab.Hooks;
4+
5+
namespace BotSharp.Core.Crontab.Functions;
6+
7+
public class ScheduleTaskFn : IFunctionCallback
8+
{
9+
public string Name => $"{CrontabUtilityHook.PREFIX}schedule_task";
10+
private readonly IServiceProvider _services;
11+
12+
public ScheduleTaskFn(IServiceProvider services)
13+
{
14+
_services = services;
15+
}
16+
17+
public async Task<bool> Execute(RoleDialogModel message)
18+
{
19+
var args = JsonSerializer.Deserialize<ScheduleTaskArgs>(message.FunctionArgs);
20+
21+
var routing = _services.GetRequiredService<IRoutingContext>();
22+
var user = _services.GetRequiredService<IUserIdentity>();
23+
var crontabItem = new CrontabItem
24+
{
25+
Topic = args.Topic,
26+
Description = args.Description,
27+
Cron = args.Cron,
28+
Script = args.Script,
29+
Language = args.Language,
30+
UserId = user.Id,
31+
AgentId = routing.EntryAgentId,
32+
ConversationId = routing.ConversationId
33+
};
34+
35+
return true;
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Core.Crontab.Enum;
3+
4+
namespace BotSharp.Core.Crontab.Hooks;
5+
6+
public class CrontabUtilityHook : IAgentUtilityHook
7+
{
8+
public const string PREFIX = "util-crontab-";
9+
private const string SCHEDULE_TASK_FN = $"{PREFIX}schedule_task";
10+
11+
public void AddUtilities(List<AgentUtility> utilities)
12+
{
13+
var items = new List<AgentUtility>
14+
{
15+
new AgentUtility
16+
{
17+
Name = UtilityName.ScheduleTask,
18+
Functions = [new(SCHEDULE_TASK_FN)],
19+
Templates = [new($"{SCHEDULE_TASK_FN}.fn")]
20+
}
21+
};
22+
23+
utilities.AddRange(items);
24+
}
25+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
namespace BotSharp.Core.Crontab.Models;
22

3-
public class CrontabItem
3+
public class CrontabItem : ScheduleTaskArgs
44
{
55
public string UserId { get; set; } = null!;
66
public string AgentId { get; set; } = null!;
7-
public string Topic { get; set; } = null!;
8-
public string Cron { get; set; } = null!;
7+
public string ConversationId { get; set; } = null!;
8+
public string ExecutionResult { get; set; } = null!;
99

1010
public override string ToString()
1111
{
12-
return $"AgentId: {AgentId}, UserId: {UserId}, Topic: {Topic}";
12+
return $"{Topic}: {Description} [AgentId: {AgentId}, UserId: {UserId}]";
1313
}
1414
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Core.Crontab.Models;
4+
5+
public class ScheduleTaskArgs
6+
{
7+
[JsonPropertyName("cron_expression")]
8+
public string Cron { get; set; } = null!;
9+
10+
[JsonPropertyName("topic")]
11+
public string Topic { get; set; } = null!;
12+
13+
[JsonPropertyName("description")]
14+
public string Description { get; set; } = null!;
15+
16+
[JsonPropertyName("script")]
17+
public string Script { get; set; } = null!;
18+
19+
[JsonPropertyName("language")]
20+
public string Language { get; set; } = null!;
21+
}

src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using BotSharp.Abstraction.Agents.Enums;
18+
using BotSharp.Abstraction.Conversations;
19+
using BotSharp.Abstraction.Routing;
1820
using BotSharp.Core.Crontab.Models;
1921
using BotSharp.Core.Infrastructures;
2022

@@ -56,22 +58,38 @@ public CrontabService(IServiceProvider services, ILogger<CrontabService> logger)
5658

5759
public async Task<List<CrontabItem>> GetCrontable()
5860
{
61+
var convService = _services.GetRequiredService<IConversationService>();
62+
var conv = await convService.GetConversation("73a9ee27-d597-4739-958f-3bd79760ac8e");
63+
if (conv == null || !conv.States.ContainsKey("cron_expression"))
64+
{
65+
return [];
66+
}
67+
5968
return
6069
[
6170
new CrontabItem
6271
{
63-
Cron = "*/30 * * * * *",
64-
AgentId = BuiltInAgentId.AIAssistant,
72+
Topic = conv.States["topic"],
73+
Cron = conv.States["cron_expression"],
74+
Description = conv.States["description"],
75+
Script = conv.States["script"],
76+
Language = conv.States["language"]
6577
}
6678
];
6779
}
6880

6981
public async Task ScheduledTimeArrived(CrontabItem item)
7082
{
71-
_logger.LogInformation("ScheduledTimeArrived");
72-
await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
83+
_logger.LogDebug($"ScheduledTimeArrived {item}");
84+
85+
var hooks = _services.GetServices<ICrontabHook>();
86+
foreach(var hook in hooks)
87+
{
88+
await hook.OnCronTriggered(item);
89+
}
90+
/*await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
7391
await hook.OnCronTriggered(item)
74-
);
92+
);*/
7593
await Task.Delay(1000 * 10);
7694
}
7795
}

0 commit comments

Comments
 (0)