From 8403651dc166bd0a61c8ea33cac0383eda970737 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Fri, 16 Feb 2024 15:58:39 -0600 Subject: [PATCH] add sender action event --- .../Models/ConversationSenderActionModel.cs | 11 ++++++ .../Messaging/Enums}/SenderActionEnum.cs | 6 ++- .../Hooks/ChatHubConversationHook.cs | 38 ++++++++----------- .../Hooks/StreamingLogHook.cs | 36 ++++++++++++++++-- .../BotSharp.Plugin.MetaMessenger/Using.cs | 1 + 5 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationSenderActionModel.cs rename src/{Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels => Infrastructure/BotSharp.Abstraction/Messaging/Enums}/SenderActionEnum.cs (63%) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationSenderActionModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationSenderActionModel.cs new file mode 100644 index 00000000..a153472c --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationSenderActionModel.cs @@ -0,0 +1,11 @@ +using BotSharp.Abstraction.Messaging.Enums; + +namespace BotSharp.Abstraction.Conversations.Models; + +public class ConversationSenderActionModel +{ + [JsonPropertyName("conversation_id")] + public string ConversationId { get; set; } + [JsonPropertyName("sender_action")] + public SenderActionEnum SenderAction { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/SenderActionEnum.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/SenderActionEnum.cs similarity index 63% rename from src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/SenderActionEnum.cs rename to src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/SenderActionEnum.cs index 8fcec25e..1b5fe7dd 100644 --- a/src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/SenderActionEnum.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/SenderActionEnum.cs @@ -1,9 +1,11 @@ -namespace BotSharp.Plugin.MetaMessenger.MessagingModels; +using System.Runtime.Serialization; + +namespace BotSharp.Abstraction.Messaging.Enums; public enum SenderActionEnum { [EnumMember(Value = "typing_on")] - TypingOn, + TypingOn = 1, [EnumMember(Value = "typing_off")] TypingOff, [EnumMember(Value = "mark_seen")] diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index 7840517b..1f505eb8 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Messaging; +using BotSharp.Abstraction.Messaging.Enums; using BotSharp.Abstraction.Messaging.JsonConverters; using BotSharp.Abstraction.Messaging.Models.RichContent; using BotSharp.Abstraction.Repositories; @@ -96,14 +97,19 @@ public class ChatHubConversationHook : ConversationHookBase Sender = UserViewModel.FromUser(sender) }); + // Send typing-on to client + await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel + { + ConversationId = conv.ConversationId, + SenderAction = SenderActionEnum.TypingOn + }); + await base.OnMessageReceived(message); } public override async Task OnResponseGenerated(RoleDialogModel message) { var conv = _services.GetRequiredService(); - var state = _services.GetRequiredService(); - var json = JsonSerializer.Serialize(new ChatResponseModel() { ConversationId = conv.ConversationId, @@ -118,29 +124,15 @@ public class ChatHubConversationHook : ConversationHookBase Role = AgentRole.Assistant } }, _serializerOptions); + + // Send typing-off to client + await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel + { + ConversationId = conv.ConversationId, + SenderAction = SenderActionEnum.TypingOff + }); await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json); - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversateStatesGenerated", BuildConversationStates(conv.ConversationId, state.GetStates(), message)); await base.OnResponseGenerated(message); } - - private string BuildConversationStates(string conversationId, Dictionary states, RoleDialogModel message) - { - var log = new ConversationStateLogModel - { - ConversationId = conversationId, - MessageId = message.MessageId, - States = states, - CreateTime = DateTime.UtcNow - }; - - var convSettings = _services.GetRequiredService(); - if (convSettings.EnableStateLog) - { - var db = _services.GetRequiredService(); - db.SaveConversationStateLog(log); - } - - return JsonSerializer.Serialize(log, _serializerOptions); - } } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index c0a90ced..1fe704a3 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -38,7 +38,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook { var conversationId = _state.GetConversationId(); var log = $"MessageId: {message.MessageId} ==>\r\n{message.Role}: {message.Content}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, _user.UserName, log, message)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, _user.UserName, log, message)); } public async Task BeforeGenerating(Agent agent, List conversations) @@ -65,16 +65,24 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook var conversationId = _state.GetConversationId(); var agent = await agentService.LoadAgent(message.CurrentAgentId); - await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, tokenStats.Prompt, message)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, tokenStats.Prompt, message)); var log = message.Role == AgentRole.Function ? $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" : $"[{agent?.Name}]: {message.Content}"; log += $"\r\n<== MessageId: {message.MessageId}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, log, message)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, log, message)); } - private string BuildLog(string conversationId, string? name, string content, RoleDialogModel message) + public override async Task OnResponseGenerated(RoleDialogModel message) + { + var conv = _services.GetRequiredService(); + var state = _services.GetRequiredService(); + + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversateStateLogGenerated", BuildStateLog(conv.ConversationId, state.GetStates(), message)); + } + + private string BuildContentLog(string conversationId, string? name, string content, RoleDialogModel message) { var log = new ConversationContentLogModel { @@ -95,4 +103,24 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook return JsonSerializer.Serialize(log, _serializerOptions); } + + private string BuildStateLog(string conversationId, Dictionary states, RoleDialogModel message) + { + var log = new ConversationStateLogModel + { + ConversationId = conversationId, + MessageId = message.MessageId, + States = states, + CreateTime = DateTime.UtcNow + }; + + var convSettings = _services.GetRequiredService(); + if (convSettings.EnableStateLog) + { + var db = _services.GetRequiredService(); + db.SaveConversationStateLog(log); + } + + return JsonSerializer.Serialize(log, _serializerOptions); + } } diff --git a/src/Plugins/BotSharp.Plugin.MetaMessenger/Using.cs b/src/Plugins/BotSharp.Plugin.MetaMessenger/Using.cs index 5ab5419f..a37bfc99 100644 --- a/src/Plugins/BotSharp.Plugin.MetaMessenger/Using.cs +++ b/src/Plugins/BotSharp.Plugin.MetaMessenger/Using.cs @@ -20,3 +20,4 @@ global using BotSharp.Abstraction.Messaging; global using BotSharp.Abstraction.Conversations.Models; global using BotSharp.Abstraction.Conversations; global using BotSharp.Abstraction.Messaging.Models.RichContent.Template; +global using BotSharp.Abstraction.Messaging.Enums; \ No newline at end of file