forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlDriverCrontabHook.cs
More file actions
54 lines (48 loc) · 2.02 KB
/
SqlDriverCrontabHook.cs
File metadata and controls
54 lines (48 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using BotSharp.Abstraction.Crontab.Models;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.SideCar;
using BotSharp.Core.Crontab.Abstraction;
namespace BotSharp.Plugin.SqlDriver.Hooks;
public class SqlDriverCrontabHook : ICrontabHook
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public string[]? Triggers => ["SqlDriverRuleTrigger"];
public SqlDriverCrontabHook(IServiceProvider services, ILogger<SqlDriverCrontabHook> logger)
{
_services = services;
_logger = logger;
}
public async Task OnCronTriggered(CrontabItem item)
{
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(item.ConversationId, []);
_logger.LogWarning($"Crontab item triggered: {item.Title}. {item.Description}");
foreach (var task in item.Tasks)
{
if (task.Language == "text")
{
var sidecar = _services.GetService<IConversationSideCar>();
var response = await sidecar.SendMessage(BuiltInAgentId.AIAssistant, $"{item.ExecutionResult}\r\n{task.Script}", states: new List<MessageState>());
}
else if (task.Language == "sql")
{
var message = new RoleDialogModel(AgentRole.User, $"Run the query")
{
FunctionName = "sql_select",
FunctionArgs = JsonSerializer.Serialize(new SqlStatement
{
Statement = task.Script,
Reason = item.Description
})
};
var routing = _services.GetRequiredService<IRoutingService>();
routing.Context.Push(BuiltInAgentId.SqlDriver);
await routing.InvokeFunction("sql_select", message);
item.AgentId = BuiltInAgentId.SqlDriver;
item.UserId = "41021346";
item.ExecutionResult += message.Content + "\r\n";
}
}
}
}