From e71fdc7e6505f009d6f06e2dfde1641e7b5128be Mon Sep 17 00:00:00 2001 From: Deep Blue Date: Wed, 29 Nov 2023 09:43:06 -0600 Subject: [PATCH] Update UI dialogs by conversation. --- .../Users/Services/UserService.cs | 1 + .../Hooks/ChatHubConversationHook.cs | 4 +-- src/web-live-chat/src/lib/helpers/typedefs.js | 16 ++++++++++ .../src/lib/services/signalr-service.js | 32 ++++++++++++------- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 3ebd27a1c..f6cfd5545 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -94,6 +94,7 @@ private string GenerateJwtToken(User user) return tokenHandler.WriteToken(token); } + [MemoryCache(10 * 60)] public async Task GetMyProfile() { var db = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index a65dbc0f4..939bc20d1 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -61,7 +61,7 @@ public override async Task OnConversationInitialized(Conversation conversation) var user = await userService.GetUser(conv.User.Id); conv.User = UserViewModel.FromUser(user); - await _chatHub.Clients.All.SendAsync("OnConversationInitFromClient", conv); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationInitFromClient", conv); await base.OnConversationInitialized(conversation); } @@ -73,7 +73,7 @@ public override async Task OnMessageReceived(RoleDialogModel message) var sender = await userService.GetMyProfile(); // Update console conversation UI for CSR - await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromClient", new ChatResponseModel() + await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromClient", new ChatResponseModel() { ConversationId = conv.ConversationId, MessageId = message.MessageId, diff --git a/src/web-live-chat/src/lib/helpers/typedefs.js b/src/web-live-chat/src/lib/helpers/typedefs.js index 085987c74..f52bdb02c 100644 --- a/src/web-live-chat/src/lib/helpers/typedefs.js +++ b/src/web-live-chat/src/lib/helpers/typedefs.js @@ -49,6 +49,22 @@ * @property {Date} created_at - The message sent time. */ +/** + * Invoked when a new conersation is created. + * This callback type is called `requestCallback` and is displayed as a global symbol. + * + * @callback OnConversationInitialized + * @param {ConversationModel} conversation + */ + +/** + * Invoked when message is received form chatHub. + * This callback type is called `requestCallback` and is displayed as a global symbol. + * + * @callback OnMessageReceived + * @param {ChatResponseModel} message + */ + // having to export an empty object here is annoying, // but required for vscode to pass on your types. export default {}; \ No newline at end of file diff --git a/src/web-live-chat/src/lib/services/signalr-service.js b/src/web-live-chat/src/lib/services/signalr-service.js index 79da13cc3..573002071 100644 --- a/src/web-live-chat/src/lib/services/signalr-service.js +++ b/src/web-live-chat/src/lib/services/signalr-service.js @@ -9,16 +9,16 @@ let connection; // create a SignalR service object that exposes methods to interact with the hub export const signalr = { - /** @type {function} */ + /** @type {import('$typedefs').OnConversationInitialized} */ onConversationInitFromClient: () => {}, - /** @type {function} */ + /** @type {import('$typedefs').OnMessageReceived} */ onMessageReceivedFromClient: () => {}, - /** @type {function} */ + /** @type {import('$typedefs').OnMessageReceived} */ onMessageReceivedFromCsr: () => {}, - /** @type {function} */ + /** @type {import('$typedefs').OnMessageReceived} */ onMessageReceivedFromAssistant: () => {}, // start the connection @@ -43,27 +43,35 @@ export const signalr = { // register handlers for the hub methods connection.on('OnConversationInitFromClient', (conversation) => { // do something when receiving a message, such as updating the UI or showing a notification - console.log(`[OnConversationInitFromClient] ${conversation.id}: ${conversation.title}`); - this.onConversationInitFromClient(conversation); + if (conversationId === conversation.id) { + console.log(`[OnConversationInitFromClient] ${conversation.id}: ${conversation.title}`); + this.onConversationInitFromClient(conversation); + } }); // register handlers for the hub methods connection.on('OnMessageReceivedFromClient', (message) => { // do something when receiving a message, such as updating the UI or showing a notification - console.log(`[OnMessageReceivedFromClient] ${message.sender.role}: ${message.text}`); - this.onMessageReceivedFromClient(message); + if (conversationId === message.conversation_id) { + console.log(`[OnMessageReceivedFromClient] ${message.sender.role}: ${message.text}`); + this.onMessageReceivedFromClient(message); + } }); connection.on('OnMessageReceivedFromCsr', (message) => { // do something when receiving a message, such as updating the UI or showing a notification - console.log(`[OnMessageReceivedFromCsr] ${message.role}: ${message.content}`); - this.onMessageReceivedFromCsr(message); + if (conversationId === message.conversation_id) { + console.log(`[OnMessageReceivedFromCsr] ${message.role}: ${message.content}`); + this.onMessageReceivedFromCsr(message); + } }); connection.on('OnMessageReceivedFromAssistant', (message) => { // do something when receiving a message, such as updating the UI or showing a notification - console.log(`[OnMessageReceivedFromAssistant] ${message.sender.role}: ${message.text}`); - this.onMessageReceivedFromAssistant(message); + if (conversationId === message.conversation_id) { + console.log(`[OnMessageReceivedFromAssistant] ${message.sender.role}: ${message.text}`); + this.onMessageReceivedFromAssistant(message); + } }); },