Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private string GenerateJwtToken(User user)
return tokenHandler.WriteToken(token);
}

[MemoryCache(10 * 60)]
public async Task<User> GetMyProfile()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/web-live-chat/src/lib/helpers/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
32 changes: 20 additions & 12 deletions src/web-live-chat/src/lib/services/signalr-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
});
},

Expand Down