2024-12-09 00:29:04 +00:00
|
|
|
using BotSharp.Abstraction.Crontab.Models;
|
2024-12-05 17:11:16 +00:00
|
|
|
using BotSharp.Core.Crontab.Abstraction;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.ChatHub.Hooks;
|
|
|
|
|
|
|
|
|
|
public class ChatHubCrontabHook : ICrontabHook
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IHubContext<SignalRHub> _chatHub;
|
2025-02-27 22:19:43 +00:00
|
|
|
private readonly ILogger<ChatHubCrontabHook> _logger;
|
2024-12-05 17:11:16 +00:00
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
private readonly IConversationStorage _storage;
|
|
|
|
|
private readonly BotSharpOptions _options;
|
2025-02-13 20:52:46 +00:00
|
|
|
private readonly ChatHubSettings _settings;
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
private const string GENERATE_NOTIFICATION = "OnNotificationGenerated";
|
|
|
|
|
#endregion
|
2024-12-05 17:11:16 +00:00
|
|
|
|
|
|
|
|
public ChatHubCrontabHook(IServiceProvider services,
|
|
|
|
|
IHubContext<SignalRHub> chatHub,
|
2025-02-27 22:19:43 +00:00
|
|
|
ILogger<ChatHubCrontabHook> logger,
|
2024-12-05 17:11:16 +00:00
|
|
|
IUserIdentity user,
|
|
|
|
|
IConversationStorage storage,
|
2025-02-13 20:52:46 +00:00
|
|
|
BotSharpOptions options,
|
|
|
|
|
ChatHubSettings settings)
|
2024-12-05 17:11:16 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_chatHub = chatHub;
|
2025-02-27 22:19:43 +00:00
|
|
|
_logger = logger;
|
2024-12-05 17:11:16 +00:00
|
|
|
_user = user;
|
|
|
|
|
_storage = storage;
|
|
|
|
|
_options = options;
|
2025-02-13 20:52:46 +00:00
|
|
|
_settings = settings;
|
2024-12-05 17:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task OnCronTriggered(CrontabItem item)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonSerializer.Serialize(new ChatResponseModel()
|
|
|
|
|
{
|
|
|
|
|
ConversationId = item.ConversationId,
|
|
|
|
|
MessageId = Guid.NewGuid().ToString(),
|
|
|
|
|
Text = item.ExecutionResult,
|
|
|
|
|
Function = "",
|
|
|
|
|
Sender = new UserViewModel()
|
|
|
|
|
{
|
|
|
|
|
FirstName = "Crontab",
|
|
|
|
|
LastName = "AI",
|
|
|
|
|
Role = AgentRole.Assistant
|
|
|
|
|
}
|
|
|
|
|
}, _options.JsonSerializerOptions);
|
|
|
|
|
|
2025-02-27 22:19:43 +00:00
|
|
|
await SendEvent(item, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SendEvent(CrontabItem item, string json)
|
|
|
|
|
{
|
|
|
|
|
try
|
2025-02-13 20:52:46 +00:00
|
|
|
{
|
2025-02-27 22:19:43 +00:00
|
|
|
if (_settings.EventDispatchBy == EventDispatchType.Group)
|
|
|
|
|
{
|
|
|
|
|
await _chatHub.Clients.Group(item.ConversationId).SendAsync(GENERATE_NOTIFICATION, json);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _chatHub.Clients.User(item.UserId).SendAsync(GENERATE_NOTIFICATION, json);
|
|
|
|
|
}
|
2025-02-13 20:52:46 +00:00
|
|
|
}
|
2025-02-27 22:19:43 +00:00
|
|
|
catch (Exception ex)
|
2025-02-13 20:52:46 +00:00
|
|
|
{
|
2025-02-27 22:19:43 +00:00
|
|
|
_logger.LogWarning($"Failed to send event in {nameof(ChatHubCrontabHook)} (conversation id: {item.ConversationId})." +
|
|
|
|
|
$"\r\n{ex.Message}\r\n{ex.InnerException}");
|
2025-02-13 20:52:46 +00:00
|
|
|
}
|
2024-12-05 17:11:16 +00:00
|
|
|
}
|
|
|
|
|
}
|