From dd024661d5cdab162edcbd052ffabe9186289e11 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 29 Jul 2025 10:32:55 -0500 Subject: [PATCH] rename --- .../{ChatHubHelper.cs => EventEmitter.cs} | 17 +++--- .../Hooks/ChatHubConversationHook.cs | 2 +- .../Hooks/StreamingLogHook.cs | 18 +----- .../Hooks/WelcomeHook.cs | 2 +- .../Observers/ChatHubObserver.cs | 55 ++++--------------- .../Core/TestAgentService.cs | 2 +- 6 files changed, 23 insertions(+), 73 deletions(-) rename src/Plugins/BotSharp.Plugin.ChatHub/Helpers/{ChatHubHelper.cs => EventEmitter.cs} (61%) diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/ChatHubHelper.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs similarity index 61% rename from src/Plugins/BotSharp.Plugin.ChatHub/Helpers/ChatHubHelper.cs rename to src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs index 94bbc90d..821b173e 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/ChatHubHelper.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs @@ -3,9 +3,9 @@ using System.Runtime.CompilerServices; namespace BotSharp.Plugin.ChatHub.Helpers; -public class ChatHubHelper +internal class EventEmitter { - public static async Task SendChatEvent( + internal static async Task SendChatEvent( IServiceProvider services, ILogger logger, string @event, @@ -21,13 +21,14 @@ public class ChatHubHelper var settings = services.GetRequiredService(); var chatHub = services.GetRequiredService>(); - if (settings.EventDispatchBy == EventDispatchType.Group) + switch (settings.EventDispatchBy) { - await chatHub.Clients.Group(conversationId).SendAsync(@event, data); - } - else - { - await chatHub.Clients.User(userId).SendAsync(@event, data); + case EventDispatchType.Group when !string.IsNullOrEmpty(conversationId): + await chatHub.Clients.Group(conversationId).SendAsync(@event, data); + break; + case EventDispatchType.User when !string.IsNullOrEmpty(userId): + await chatHub.Clients.User(userId).SendAsync(@event, data); + break; } } catch (Exception ex) diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index 5cbc117e..52dd4f7c 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -174,7 +174,7 @@ public class ChatHubConversationHook : ConversationHookBase private async Task SendEvent(string @event, string conversationId, T data, [CallerMemberName] string callerName = "") { var user = _services.GetRequiredService(); - await ChatHubHelper.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(ChatHubConversationHook), callerName); + await EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(ChatHubConversationHook), callerName); } #endregion } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index f036deab..73365115 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -228,7 +228,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR var conv = _services.GetRequiredService(); var routingCtx = _services.GetRequiredService(); var stateLog = BuildStateLog(conv.ConversationId, routingCtx.EntryAgentId, _state.GetStates(), message); - //await SendStateLog(conv.ConversationId, routingCtx.EntryAgentId, _state.GetStates(), message); await SendEvent(ChatEvent.OnConversateStateLogGenerated, conv.ConversationId, stateLog); if (message.Role == AgentRole.Assistant) @@ -248,7 +247,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.AgentResponse, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } } @@ -267,7 +265,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.FunctionCall, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -285,7 +282,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.FunctionCall, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -314,7 +310,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR }, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -325,7 +320,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR if (stateChange == null) return; - //await SendStateChange(conversationId, stateChange); await SendEvent(ChatEvent.OnStateChangeGenerated, conversationId, BuildStateChangeLog(stateChange)); } #endregion @@ -340,7 +334,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR // Agent queue log var log = $"{agent.Name} is enqueued"; - //await SendAgentQueueLog(conversationId, log); await SendEvent(ChatEvent.OnAgentQueueChanged, conversationId, BuildAgentQueueChangedLog(conversationId, log)); // Content log @@ -356,7 +349,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.HardRule, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -370,7 +362,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR // Agent queue log var log = $"{agent.Name} is dequeued"; - //await SendAgentQueueLog(conversationId, log); await SendEvent(ChatEvent.OnAgentQueueChanged, conversationId, BuildAgentQueueChangedLog(conversationId, log)); // Content log @@ -386,7 +377,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.HardRule, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -400,7 +390,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR // Agent queue log var log = $"Agent queue is replaced from {fromAgent.Name} to {toAgent.Name}"; - //await SendAgentQueueLog(conversationId, log); await SendEvent(ChatEvent.OnAgentQueueChanged, conversationId, BuildAgentQueueChangedLog(conversationId, log)); // Content log @@ -416,7 +405,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.HardRule, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -427,7 +415,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR // Agent queue log var log = $"Agent queue is empty"; - //await SendAgentQueueLog(conversationId, log); await SendEvent(ChatEvent.OnAgentQueueChanged, conversationId, BuildAgentQueueChangedLog(conversationId, log)); // Content log @@ -443,7 +430,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.HardRule, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -463,7 +449,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.AgentResponse, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } @@ -482,7 +467,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR Source = ContentLogSource.HardRule, Log = log }; - //await SendContentLog(conversationId, input); await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); } #endregion @@ -492,7 +476,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR private async Task SendEvent(string @event, string conversationId, T data, [CallerMemberName] string callerName = "") { var user = _services.GetRequiredService(); - await ChatHubHelper.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(StreamingLogHook), callerName); + await EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(StreamingLogHook), callerName); } private ContentLogOutputModel BuildContentLog(ContentLogInputModel input) diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs index 3db8017b..8a184568 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs @@ -89,6 +89,6 @@ public class WelcomeHook : ConversationHookBase private async Task SendEvent(string @event, string conversationId, T data, [CallerMemberName] string callerName = "") { var user = _services.GetRequiredService(); - await ChatHubHelper.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(WelcomeHook), callerName); + await EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(WelcomeHook), callerName); } } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs index 71b07b9d..3c1799ea 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs @@ -4,6 +4,7 @@ using BotSharp.Abstraction.MessageHub.Models; using BotSharp.Abstraction.SideCar; using BotSharp.Plugin.ChatHub.Hooks; using Microsoft.AspNetCore.SignalR; +using System.Runtime.CompilerServices; namespace BotSharp.Plugin.ChatHub.Observers; @@ -60,7 +61,7 @@ public class ChatHubObserver : IObserver SenderAction = SenderActionEnum.TypingOn }; - GenerateSenderAction(conv.ConversationId, action); + SendEvent(ChatEvent.OnSenderActionGenerated, conv.ConversationId, action); break; case ChatEvent.OnReceiveLlmStreamMessage: model = new ChatResponseDto() @@ -99,7 +100,7 @@ public class ChatHubObserver : IObserver SenderAction = SenderActionEnum.TypingOff }; - GenerateSenderAction(conv.ConversationId, action); + SendEvent(ChatEvent.OnSenderActionGenerated, conv.ConversationId, action); break; case ChatEvent.OnIndicationReceived: model = new ChatResponseDto @@ -117,7 +118,7 @@ public class ChatHubObserver : IObserver break; } - OnReceiveAssistantMessage(value.EventName, model.ConversationId, model); + SendEvent(value.EventName, model.ConversationId, model); } private bool AllowSendingMessage() @@ -126,48 +127,12 @@ public class ChatHubObserver : IObserver return sidecar == null || !sidecar.IsEnabled; } - private void OnReceiveAssistantMessage(string @event, string conversationId, ChatResponseDto model) + #region Private methods + private void SendEvent(string @event, string conversationId, T data, [CallerMemberName] string callerName = "") { - try - { - var settings = _services.GetRequiredService(); - var chatHub = _services.GetRequiredService>(); - - if (settings.EventDispatchBy == EventDispatchType.Group) - { - chatHub.Clients.Group(conversationId).SendAsync(@event, model).ConfigureAwait(false).GetAwaiter().GetResult(); - } - else - { - var user = _services.GetRequiredService(); - chatHub.Clients.User(user.Id).SendAsync(@event, model).ConfigureAwait(false).GetAwaiter().GetResult(); - } - } - catch (Exception ex) - { - _logger.LogWarning(ex, $"Failed to receive assistant message in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})"); - } - } - - private void GenerateSenderAction(string conversationId, ConversationSenderActionModel action) - { - try - { - var settings = _services.GetRequiredService(); - var chatHub = _services.GetRequiredService>(); - if (settings.EventDispatchBy == EventDispatchType.Group) - { - chatHub.Clients.Group(conversationId).SendAsync(ChatEvent.OnSenderActionGenerated, action).ConfigureAwait(false).GetAwaiter().GetResult(); - } - else - { - var user = _services.GetRequiredService(); - chatHub.Clients.User(user.Id).SendAsync(ChatEvent.OnSenderActionGenerated, action).ConfigureAwait(false).GetAwaiter().GetResult(); - } - } - catch (Exception ex) - { - _logger.LogWarning(ex, $"Failed to generate sender action in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})"); - } + var user = _services.GetRequiredService(); + EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(ChatHubObserver), callerName) + .ConfigureAwait(false).GetAwaiter().GetResult(); } + #endregion } diff --git a/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs b/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs index 50d34701..5d4b07d0 100644 --- a/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs +++ b/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs @@ -16,7 +16,7 @@ namespace BotSharp.Plugin.Google.Core return Task.FromResult(new Agent()); } - public Task RefreshAgents() + public Task RefreshAgents(IEnumerable? agentIds = null) { return Task.FromResult("Refreshed successfully"); }