Merge pull request #303 from iceljc/features/add-typing-event
add sender action event
This commit is contained in:
commit
d3f2b5ad44
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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")]
|
||||
|
|
@ -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;
|
||||
|
|
@ -96,14 +97,19 @@ public class ChatHubConversationHook : ConversationHookBase
|
|||
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,
|
||||
|
|
@ -118,29 +124,15 @@ public class ChatHubConversationHook : ConversationHookBase
|
|||
Role = AgentRole.Assistant
|
||||
}
|
||||
}, _serializerOptions);
|
||||
|
||||
// Send typing-off to client
|
||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnSenderActionGenerated", new ConversationSenderActionModel
|
||||
{
|
||||
ConversationId = conv.ConversationId,
|
||||
SenderAction = SenderActionEnum.TypingOff
|
||||
});
|
||||
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
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
|
|||
{
|
||||
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)
|
||||
|
|
@ -65,16 +65,24 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
|
|||
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
|
||||
{
|
||||
|
|
@ -95,4 +103,24 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,3 +20,4 @@ global using BotSharp.Abstraction.Messaging;
|
|||
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;
|
||||
Loading…
Reference in a new issue