Update UI dialogs by conversation.

This commit is contained in:
Deep Blue 2023-11-29 09:43:06 -06:00
parent 5bd15f7a6b
commit e71fdc7e65
4 changed files with 39 additions and 14 deletions

View file

@ -94,6 +94,7 @@ public class UserService : IUserService
return tokenHandler.WriteToken(token);
}
[MemoryCache(10 * 60)]
public async Task<User> GetMyProfile()
{
var db = _services.GetRequiredService<IBotSharpRepository>();

View file

@ -61,7 +61,7 @@ public class ChatHubConversationHook : ConversationHookBase
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 class ChatHubConversationHook : ConversationHookBase
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,

View file

@ -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 {};

View file

@ -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);
}
});
},