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

122 lines
4.3 KiB
C#
Raw Normal View History

2023-12-18 22:38:12 +00:00
using BotSharp.Abstraction.Messaging;
2023-12-19 13:16:43 +00:00
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Messaging.Models.RichContent;
2023-11-22 16:27:55 +00:00
using Microsoft.AspNetCore.SignalR;
2023-12-19 13:16:43 +00:00
using System.Text.Json.Serialization.Metadata;
2023-11-22 16:27:55 +00:00
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;
2023-12-19 13:16:43 +00:00
private readonly JsonSerializerOptions _serializerOptions;
2023-11-22 16:27:55 +00:00
public ChatHubConversationHook(IServiceProvider services,
2023-11-28 03:19:07 +00:00
IHubContext<SignalRHub> chatHub,
IUserIdentity user)
2023-11-22 16:27:55 +00:00
{
_services = services;
_chatHub = chatHub;
2023-11-28 03:19:07 +00:00
_user = user;
2023-12-19 13:16:43 +00:00
_serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new RichContentJsonConverter(),
new TemplateMessageJsonConverter(),
}
};
2023-11-22 16:27:55 +00:00
}
2023-11-27 21:00:26 +00:00
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
{
2023-12-19 13:16:43 +00:00
var agentService = _services.GetRequiredService<IAgentService>();
2023-11-27 21:00:26 +00:00
var agent = await agentService.LoadAgent(conversation.AgentId);
// Check if the Welcome template exists.
2023-12-19 13:16:43 +00:00
var welcomeTemplate = agent.Templates?.FirstOrDefault(x => x.Name == "welcome");
2023-11-27 21:00:26 +00:00
if (welcomeTemplate != null)
{
2023-12-18 22:38:12 +00:00
var richContentService = _services.GetRequiredService<IRichContentService>();
var messages = richContentService.ConvertToMessages(welcomeTemplate.Content);
2023-11-27 21:00:26 +00:00
foreach (var message in messages)
{
2023-12-19 13:16:43 +00:00
var json = JsonSerializer.Serialize(new ChatResponseModel()
2023-11-28 03:19:07 +00:00
{
ConversationId = conversation.Id,
Text = message.Text,
2023-12-19 13:16:43 +00:00
RichContent = new RichContent<IRichMessage>(message),
2023-11-28 03:19:07 +00:00
Sender = new UserViewModel()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
2023-12-19 13:16:43 +00:00
}, _serializerOptions);
await Task.Delay(300);
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
2023-11-27 21:00:26 +00:00
}
}
await base.OnUserAgentConnectedInitially(conversation);
}
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,
Text = message.Content,
Sender = UserViewModel.FromUser(sender)
});
await base.OnMessageReceived(message);
}
public override async Task OnResponseGenerated(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();
2023-11-28 03:19:07 +00:00
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", new ChatResponseModel()
2023-11-22 16:27:55 +00:00
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
Text = message.Content,
2023-12-19 13:16:43 +00:00
RichContent = message.RichContent,
2023-11-22 16:27:55 +00:00
Sender = new UserViewModel()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
});
await base.OnResponseGenerated(message);
}
}