Merge pull request #903 from iceljc/master

catch chathub error
This commit is contained in:
iceljc 2025-02-27 16:28:24 -06:00 committed by GitHub
commit 48df2f1cfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 189 additions and 60 deletions

View file

@ -7,6 +7,7 @@ public class ChatHubConversationHook : ConversationHookBase
{
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly ILogger<ChatHubConversationHook> _logger;
private readonly IUserIdentity _user;
private readonly BotSharpOptions _options;
private readonly ChatHubSettings _settings;
@ -23,12 +24,14 @@ public class ChatHubConversationHook : ConversationHookBase
public ChatHubConversationHook(
IServiceProvider services,
IHubContext<SignalRHub> chatHub,
ILogger<ChatHubConversationHook> logger,
BotSharpOptions options,
ChatHubSettings settings,
IUserIdentity user)
{
_services = services;
_chatHub = chatHub;
_logger = logger;
_user = user;
_options = options;
_settings = settings;
@ -177,74 +180,122 @@ public class ChatHubConversationHook : ConversationHookBase
private async Task InitClientConversation(string conversationId, ConversationViewModel conversation)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(INIT_CLIENT_CONVERSATION, conversation);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(INIT_CLIENT_CONVERSATION, conversation);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(INIT_CLIENT_CONVERSATION, conversation);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(INIT_CLIENT_CONVERSATION, conversation);
_logger.LogWarning($"Failed to init client conversation in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})" +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task ReceiveClientMessage(string conversationId, ChatResponseModel model)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(RECEIVE_CLIENT_MESSAGE, model);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(RECEIVE_CLIENT_MESSAGE, model);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_CLIENT_MESSAGE, model);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_CLIENT_MESSAGE, model);
_logger.LogWarning($"Failed to receive assistant message in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})" +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task ReceiveAssistantMessage(string conversationId, string? json)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
_logger.LogWarning($"Failed to receive assistant message in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})" +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task GenerateSenderAction(string conversationId, ConversationSenderActionModel action)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(GENERATE_SENDER_ACTION, action);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(GENERATE_SENDER_ACTION, action);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(GENERATE_SENDER_ACTION, action);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(GENERATE_SENDER_ACTION, action);
_logger.LogWarning($"Failed to generate sender action in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})" +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task DeleteMessage(string conversationId, ChatResponseModel model)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(DELETE_MESSAGE, model);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(DELETE_MESSAGE, model);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(DELETE_MESSAGE, model);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(DELETE_MESSAGE, model);
_logger.LogWarning($"Failed to delete message in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})" +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task GenerateNotification(string conversationId, string? json)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(GENERATE_NOTIFICATION, json);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(GENERATE_NOTIFICATION, json);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(GENERATE_NOTIFICATION, json);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(GENERATE_NOTIFICATION, json);
_logger.LogWarning($"Failed to generate notification in {nameof(ChatHubConversationHook)} (conversation id: {conversationId})" +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
#endregion

View file

@ -8,6 +8,7 @@ public class ChatHubCrontabHook : ICrontabHook
{
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly ILogger<ChatHubCrontabHook> _logger;
private readonly IUserIdentity _user;
private readonly IConversationStorage _storage;
private readonly BotSharpOptions _options;
@ -19,6 +20,7 @@ public class ChatHubCrontabHook : ICrontabHook
public ChatHubCrontabHook(IServiceProvider services,
IHubContext<SignalRHub> chatHub,
ILogger<ChatHubCrontabHook> logger,
IUserIdentity user,
IConversationStorage storage,
BotSharpOptions options,
@ -26,6 +28,7 @@ public class ChatHubCrontabHook : ICrontabHook
{
_services = services;
_chatHub = chatHub;
_logger = logger;
_user = user;
_storage = storage;
_options = options;
@ -48,13 +51,26 @@ public class ChatHubCrontabHook : ICrontabHook
}
}, _options.JsonSerializerOptions);
if (_settings.EventDispatchBy == EventDispatchType.Group)
await SendEvent(item, json);
}
private async Task SendEvent(CrontabItem item, string json)
{
try
{
await _chatHub.Clients.Group(item.ConversationId).SendAsync(GENERATE_NOTIFICATION, json);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(item.ConversationId).SendAsync(GENERATE_NOTIFICATION, json);
}
else
{
await _chatHub.Clients.User(item.UserId).SendAsync(GENERATE_NOTIFICATION, json);
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(item.UserId).SendAsync(GENERATE_NOTIFICATION, json);
_logger.LogWarning($"Failed to send event in {nameof(ChatHubCrontabHook)} (conversation id: {item.ConversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
}

View file

@ -12,6 +12,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
private readonly ChatHubSettings _settings;
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly ILogger<StreamingLogHook> _logger;
private readonly IConversationStateService _state;
private readonly IUserIdentity _user;
private readonly IAgentService _agentService;
@ -30,6 +31,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
ChatHubSettings settings,
IServiceProvider serivces,
IHubContext<SignalRHub> chatHub,
ILogger<StreamingLogHook> logger,
IConversationStateService state,
IUserIdentity user,
IAgentService agentService,
@ -40,6 +42,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
_settings = settings;
_services = serivces;
_chatHub = chatHub;
_logger = logger;
_state = state;
_user = user;
_agentService = agentService;
@ -439,54 +442,85 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
#region Private methods
private async Task SendContentLog(string conversationId, ContentLogInputModel input)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(CONTENT_LOG_GENERATED, BuildContentLog(input));
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(CONTENT_LOG_GENERATED, BuildContentLog(input));
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(CONTENT_LOG_GENERATED, BuildContentLog(input));
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(CONTENT_LOG_GENERATED, BuildContentLog(input));
_logger.LogWarning($"Failed to send content log in {nameof(StreamingLogHook)} (conversation id: {conversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task SendStateLog(string conversationId, string agentId, Dictionary<string, string> states, RoleDialogModel message)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(STATE_LOG_GENERATED, BuildStateLog(conversationId, agentId, states, message));
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(STATE_LOG_GENERATED, BuildStateLog(conversationId, agentId, states, message));
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(STATE_LOG_GENERATED, BuildStateLog(conversationId, agentId, states, message));
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(STATE_LOG_GENERATED, BuildStateLog(conversationId, agentId, states, message));
_logger.LogWarning($"Failed to send state log in {nameof(StreamingLogHook)} (conversation id: {conversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task SendAgentQueueLog(string conversationId, string log)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(AGENT_QUEUE_CHANGED, BuildAgentQueueChangedLog(conversationId, log));
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(AGENT_QUEUE_CHANGED, BuildAgentQueueChangedLog(conversationId, log));
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(AGENT_QUEUE_CHANGED, BuildAgentQueueChangedLog(conversationId, log));
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(AGENT_QUEUE_CHANGED, BuildAgentQueueChangedLog(conversationId, log));
_logger.LogWarning($"Failed to send agent queue log in {nameof(StreamingLogHook)} (conversation id: {conversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private async Task SendStateChange(string conversationId, StateChangeModel stateChange)
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
try
{
await _chatHub.Clients.Group(conversationId).SendAsync(STATE_CHANGED, BuildStateChangeLog(stateChange));
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(STATE_CHANGED, BuildStateChangeLog(stateChange));
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(STATE_CHANGED, BuildStateChangeLog(stateChange));
}
}
else
catch (Exception ex)
{
await _chatHub.Clients.User(_user.Id).SendAsync(STATE_CHANGED, BuildStateChangeLog(stateChange));
_logger.LogWarning($"Failed to send state change in {nameof(StreamingLogHook)} (conversation id: {conversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
private string BuildContentLog(ContentLogInputModel input)
{
var output = new ContentLogOutputModel

View file

@ -6,6 +6,7 @@ public class WelcomeHook : ConversationHookBase
{
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly ILogger<WelcomeHook> _logger;
private readonly IUserIdentity _user;
private readonly IConversationStorage _storage;
private readonly BotSharpOptions _options;
@ -17,6 +18,7 @@ public class WelcomeHook : ConversationHookBase
public WelcomeHook(IServiceProvider services,
IHubContext<SignalRHub> chatHub,
ILogger<WelcomeHook> logger,
IUserIdentity user,
IConversationStorage storage,
BotSharpOptions options,
@ -24,6 +26,7 @@ public class WelcomeHook : ConversationHookBase
{
_services = services;
_chatHub = chatHub;
_logger = logger;
_user = user;
_storage = storage;
_options = options;
@ -78,17 +81,30 @@ public class WelcomeHook : ConversationHookBase
_storage.Append(conversation.Id, dialog);
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversation.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}
await SendEvent(conversation.Id, json);
}
}
await base.OnUserAgentConnectedInitially(conversation);
}
private async Task SendEvent(string conversationId, string json)
{
try
{
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(conversationId).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}
else
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}
}
catch (Exception ex)
{
_logger.LogWarning($"Failed to send event in {nameof(WelcomeHook)} (conversation id: {conversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
}

View file

@ -33,12 +33,7 @@ public class SignalRHub : Hub
if (!string.IsNullOrEmpty(conversationId))
{
_logger.LogInformation($"Connection {Context.ConnectionId} is with conversation {conversationId}");
var settings = _services.GetRequiredService<ChatHubSettings>();
if (settings.EventDispatchBy == EventDispatchType.Group)
{
await Groups.AddToGroupAsync(Context.ConnectionId, conversationId);
}
await AddGroup(conversationId);
var conv = await convService.GetConversation(conversationId);
if (conv != null)
@ -56,4 +51,21 @@ public class SignalRHub : Hub
await base.OnConnectedAsync();
}
private async Task AddGroup(string conversationId)
{
try
{
var settings = _services.GetRequiredService<ChatHubSettings>();
if (settings.EventDispatchBy == EventDispatchType.Group)
{
await Groups.AddToGroupAsync(Context.ConnectionId, conversationId);
}
}
catch (Exception ex)
{
_logger.LogWarning($"Failed to add chat group in {nameof(SignalRHub)} (conversation id: {conversationId})." +
$"\r\n{ex.Message}\r\n{ex.InnerException}");
}
}
}