BotSharp/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs

107 lines
4 KiB
C#
Raw Normal View History

2024-02-16 21:58:39 +00:00
using BotSharp.Abstraction.Messaging.Enums;
2024-03-18 19:36:41 +00:00
using BotSharp.Abstraction.Options;
2023-11-22 16:27:55 +00:00
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.Plugin.ChatHub.Hooks;
public class ChatHubConversationHook : ConversationHookBase
{
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
2023-11-28 03:19:07 +00:00
private readonly IUserIdentity _user;
2024-03-18 19:36:41 +00:00
private readonly BotSharpOptions _options;
2023-11-22 16:27:55 +00:00
public ChatHubConversationHook(IServiceProvider services,
2023-11-28 03:19:07 +00:00
IHubContext<SignalRHub> chatHub,
2024-03-18 19:36:41 +00:00
BotSharpOptions options,
2023-11-28 03:19:07 +00:00
IUserIdentity user)
2023-11-22 16:27:55 +00:00
{
_services = services;
_chatHub = chatHub;
2023-11-28 03:19:07 +00:00
_user = user;
2024-03-18 19:36:41 +00:00
_options = options;
2023-11-22 16:27:55 +00:00
}
public override async Task OnConversationInitialized(Conversation conversation)
{
var userService = _services.GetRequiredService<IUserService>();
var conv = ConversationViewModel.FromSession(conversation);
var user = await userService.GetUser(conv.User.Id);
conv.User = UserViewModel.FromUser(user);
2023-11-29 15:43:06 +00:00
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationInitFromClient", conv);
2023-11-22 16:27:55 +00:00
await base.OnConversationInitialized(conversation);
}
public override async Task OnMessageReceived(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();
var userService = _services.GetRequiredService<IUserService>();
var sender = await userService.GetMyProfile();
// Update console conversation UI for CSR
2023-11-29 15:43:06 +00:00
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromClient", new ChatResponseModel()
2023-11-22 16:27:55 +00:00
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
2024-04-23 21:27:09 +00:00
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
2023-11-22 16:27:55 +00:00
Sender = UserViewModel.FromUser(sender)
});
2024-02-16 21:58:39 +00:00
// Send typing-on to client
await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
{
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOn
});
2023-11-22 16:27:55 +00:00
await base.OnMessageReceived(message);
}
2024-03-18 19:36:41 +00:00
public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg)
{
await this.OnMessageReceived(message);
}
2023-11-22 16:27:55 +00:00
public override async Task OnResponseGenerated(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();
2023-12-20 02:28:56 +00:00
var json = JsonSerializer.Serialize(new ChatResponseModel()
2023-11-22 16:27:55 +00:00
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
2024-04-23 21:27:09 +00:00
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
2024-02-28 16:16:46 +00:00
Function = message.FunctionName,
2024-04-23 21:27:09 +00:00
RichContent = message.SecondaryRichContent ?? message.RichContent,
2024-02-12 21:49:18 +00:00
Data = message.Data,
2023-11-22 16:27:55 +00:00
Sender = new UserViewModel()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
2024-03-18 19:36:41 +00:00
}, _options.JsonSerializerOptions);
2023-11-22 16:27:55 +00:00
2024-02-16 21:58:39 +00:00
// Send typing-off to client
await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
2024-01-26 21:47:06 +00:00
{
2024-02-16 21:58:39 +00:00
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOff
});
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
2024-02-15 20:43:36 +00:00
2024-02-16 21:58:39 +00:00
await base.OnResponseGenerated(message);
2024-01-26 21:47:06 +00:00
}
2024-03-11 20:32:13 +00:00
public override async Task OnMessageDeleted(string conversationId, string messageId)
{
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageDeleted", new ChatResponseModel
{
ConversationId = conversationId,
MessageId = messageId
});
await base.OnMessageDeleted(conversationId, messageId);
}
2023-11-22 16:27:55 +00:00
}