Skip to content

refine json format in log #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 24, 2024
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 @@ -9,8 +9,18 @@ public class ChatHubConversationHook : ConversationHookBase
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly IUserIdentity _user;
private readonly BotSharpOptions _options;
public ChatHubConversationHook(IServiceProvider services,
private readonly BotSharpOptions _options;

#region Event
private const string INIT_CLIENT_CONVERSATION = "OnConversationInitFromClient";
private const string RECEIVE_CLIENT_MESSAGE = "OnMessageReceivedFromClient";
private const string RECEIVE_ASSISTANT_MESSAGE = "OnMessageReceivedFromAssistant";
private const string GENERATE_SENDER_ACTION = "OnSenderActionGenerated";
private const string DELETE_MESSAGE = "OnMessageDeleted";
#endregion

public ChatHubConversationHook(
IServiceProvider services,
IHubContext<SignalRHub> chatHub,
BotSharpOptions options,
IUserIdentity user)
Expand All @@ -29,8 +39,7 @@ public override async Task OnConversationInitialized(Conversation conversation)
var user = await userService.GetUser(conv.User.Id);
conv.User = UserViewModel.FromUser(user);

await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationInitFromClient", conv);

await InitClientConversation(conv);
await base.OnConversationInitialized(conversation);
}

Expand All @@ -41,35 +50,36 @@ public override async Task OnMessageReceived(RoleDialogModel message)
var sender = await userService.GetMyProfile();

// Update console conversation UI for CSR
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromClient", new ChatResponseModel()

var model = new ChatResponseModel()
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
Sender = UserViewModel.FromUser(sender)
});
};
await ReceiveClientMessage(model);

// Send typing-on to client
await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
var action = new ConversationSenderActionModel
{
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOn
});

};
await GenerateSenderAction(action);
await base.OnMessageReceived(message);
}

public override async Task OnFunctionExecuting(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();

await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
var action = new ConversationSenderActionModel
{
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOn,
Indication = message.Indication
});

};
await GenerateSenderAction(action);
await base.OnFunctionExecuting(message);
}

Expand Down Expand Up @@ -98,23 +108,52 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
}, _options.JsonSerializerOptions);

// Send typing-off to client
await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
var action = new ConversationSenderActionModel
{
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOff
});
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
};

await GenerateSenderAction(action);
await ReceiveAssistantMessage(json);
await base.OnResponseGenerated(message);
}

public override async Task OnMessageDeleted(string conversationId, string messageId)
{
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageDeleted", new ChatResponseModel
var model = new ChatResponseModel
{
ConversationId = conversationId,
MessageId = messageId
});
};
await DeleteMessage(model);
await base.OnMessageDeleted(conversationId, messageId);
}

#region Private methods
private async Task InitClientConversation(ConversationViewModel conversation)
{
await _chatHub.Clients.User(_user.Id).SendAsync(INIT_CLIENT_CONVERSATION, conversation);
}

private async Task ReceiveClientMessage(ChatResponseModel model)
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_CLIENT_MESSAGE, model);
}

private async Task ReceiveAssistantMessage(string? json)
{
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
}

private async Task GenerateSenderAction(ConversationSenderActionModel action)
{
await _chatHub.Clients.User(_user.Id).SendAsync(GENERATE_SENDER_ACTION, action);
}

private async Task DeleteMessage(ChatResponseModel model)
{
await _chatHub.Clients.User(_user.Id).SendAsync(DELETE_MESSAGE, model);
}
#endregion
}
Loading