This commit is contained in:
Jicheng Lu 2025-07-29 10:32:55 -05:00
parent 4bdde0669a
commit dd024661d5
6 changed files with 23 additions and 73 deletions

View file

@ -3,9 +3,9 @@ using System.Runtime.CompilerServices;
namespace BotSharp.Plugin.ChatHub.Helpers;
public class ChatHubHelper
internal class EventEmitter
{
public static async Task SendChatEvent<T>(
internal static async Task SendChatEvent<T>(
IServiceProvider services,
ILogger logger,
string @event,
@ -21,13 +21,14 @@ public class ChatHubHelper
var settings = services.GetRequiredService<ChatHubSettings>();
var chatHub = services.GetRequiredService<IHubContext<SignalRHub>>();
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)

View file

@ -174,7 +174,7 @@ public class ChatHubConversationHook : ConversationHookBase
private async Task SendEvent<T>(string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
{
var user = _services.GetRequiredService<IUserIdentity>();
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
}

View file

@ -228,7 +228,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conv = _services.GetRequiredService<IConversationService>();
var routingCtx = _services.GetRequiredService<IRoutingContext>();
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<T>(string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
{
var user = _services.GetRequiredService<IUserIdentity>();
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)

View file

@ -89,6 +89,6 @@ public class WelcomeHook : ConversationHookBase
private async Task SendEvent<T>(string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
{
var user = _services.GetRequiredService<IUserIdentity>();
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);
}
}

View file

@ -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<HubObserveData>
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<HubObserveData>
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<HubObserveData>
break;
}
OnReceiveAssistantMessage(value.EventName, model.ConversationId, model);
SendEvent(value.EventName, model.ConversationId, model);
}
private bool AllowSendingMessage()
@ -126,48 +127,12 @@ public class ChatHubObserver : IObserver<HubObserveData>
return sidecar == null || !sidecar.IsEnabled;
}
private void OnReceiveAssistantMessage(string @event, string conversationId, ChatResponseDto model)
#region Private methods
private void SendEvent<T>(string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
{
try
{
var settings = _services.GetRequiredService<ChatHubSettings>();
var chatHub = _services.GetRequiredService<IHubContext<SignalRHub>>();
if (settings.EventDispatchBy == EventDispatchType.Group)
{
chatHub.Clients.Group(conversationId).SendAsync(@event, model).ConfigureAwait(false).GetAwaiter().GetResult();
}
else
{
var user = _services.GetRequiredService<IUserIdentity>();
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<ChatHubSettings>();
var chatHub = _services.GetRequiredService<IHubContext<SignalRHub>>();
if (settings.EventDispatchBy == EventDispatchType.Group)
{
chatHub.Clients.Group(conversationId).SendAsync(ChatEvent.OnSenderActionGenerated, action).ConfigureAwait(false).GetAwaiter().GetResult();
}
else
{
var user = _services.GetRequiredService<IUserIdentity>();
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<IUserIdentity>();
EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, data, nameof(ChatHubObserver), callerName)
.ConfigureAwait(false).GetAwaiter().GetResult();
}
#endregion
}

View file

@ -16,7 +16,7 @@ namespace BotSharp.Plugin.Google.Core
return Task.FromResult(new Agent());
}
public Task<string> RefreshAgents()
public Task<string> RefreshAgents(IEnumerable<string>? agentIds = null)
{
return Task.FromResult("Refreshed successfully");
}