Skip to content

add sender action event #303

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 1 commit into from
Feb 17, 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
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -96,14 +97,19 @@ public override async Task OnMessageReceived(RoleDialogModel message)
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<IConversationService>();
var state = _services.GetRequiredService<IConversationStateService>();

var json = JsonSerializer.Serialize(new ChatResponseModel()
{
ConversationId = conv.ConversationId,
Expand All @@ -118,29 +124,15 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
Role = AgentRole.Assistant
}
}, _serializerOptions);
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<string, string> states, RoleDialogModel message)
{
var log = new ConversationStateLogModel
// Send typing-off to client
await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
{
ConversationId = conversationId,
MessageId = message.MessageId,
States = states,
CreateTime = DateTime.UtcNow
};

var convSettings = _services.GetRequiredService<ConversationSetting>();
if (convSettings.EnableStateLog)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.SaveConversationStateLog(log);
}
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOff
});
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);

return JsonSerializer.Serialize(log, _serializerOptions);
await base.OnResponseGenerated(message);
}
}
36 changes: 32 additions & 4 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override async Task OnMessageReceived(RoleDialogModel message)
{
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<RoleDialogModel> conversations)
Expand All @@ -65,16 +65,24 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
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<IConversationService>();
var state = _services.GetRequiredService<IConversationStateService>();

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
{
Expand All @@ -95,4 +103,24 @@ private string BuildLog(string conversationId, string? name, string content, Rol

return JsonSerializer.Serialize(log, _serializerOptions);
}

private string BuildStateLog(string conversationId, Dictionary<string, string> states, RoleDialogModel message)
{
var log = new ConversationStateLogModel
{
ConversationId = conversationId,
MessageId = message.MessageId,
States = states,
CreateTime = DateTime.UtcNow
};

var convSettings = _services.GetRequiredService<ConversationSetting>();
if (convSettings.EnableStateLog)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.SaveConversationStateLog(log);
}

return JsonSerializer.Serialize(log, _serializerOptions);
}
}
1 change: 1 addition & 0 deletions src/Plugins/BotSharp.Plugin.MetaMessenger/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
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;