introduce ConversationHookProvider to avoid ordering conversartion hooks in runtime
This commit is contained in:
parent
8d176b54f0
commit
ba855bc7cc
|
|
@ -0,0 +1,20 @@
|
||||||
|
namespace BotSharp.Abstraction.Conversations;
|
||||||
|
|
||||||
|
public class ConversationHookProvider
|
||||||
|
{
|
||||||
|
public IEnumerable<IConversationHook> Hooks { get; }
|
||||||
|
|
||||||
|
private readonly Lazy<IEnumerable<IConversationHook>> _hooksOrderByPriority;
|
||||||
|
|
||||||
|
public IEnumerable<IConversationHook> HooksOrderByPriority
|
||||||
|
=> _hooksOrderByPriority.Value;
|
||||||
|
|
||||||
|
public ConversationHookProvider(IEnumerable<IConversationHook> conversationHooks)
|
||||||
|
{
|
||||||
|
Hooks = conversationHooks;
|
||||||
|
_hooksOrderByPriority = new Lazy<IEnumerable<IConversationHook>>(() =>
|
||||||
|
{
|
||||||
|
return conversationHooks.OrderBy(hook => hook.Priority).ToArray();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,7 +9,7 @@ public partial class ConversationService : IConversationService
|
||||||
var deleteMessageIds = db.TruncateConversation(conversationId, messageId, cleanLog: true);
|
var deleteMessageIds = db.TruncateConversation(conversationId, messageId, cleanLog: true);
|
||||||
fileStorage.DeleteMessageFiles(conversationId, deleteMessageIds, messageId, newMessageId);
|
fileStorage.DeleteMessageFiles(conversationId, deleteMessageIds, messageId, newMessageId);
|
||||||
|
|
||||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
var hooks = _services.GetServices<IConversationHook>();
|
||||||
foreach (var hook in hooks)
|
foreach (var hook in hooks)
|
||||||
{
|
{
|
||||||
await hook.OnMessageDeleted(conversationId, messageId);
|
await hook.OnMessageDeleted(conversationId, messageId);
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,9 @@ public partial class ConversationService : IConversationService
|
||||||
states.CleanStates(excludedStates);
|
states.CleanStates(excludedStates);
|
||||||
}
|
}
|
||||||
|
|
||||||
var hooks = _services.GetServices<IConversationHook>()
|
var hooks = _services
|
||||||
.OrderBy(x => x.Priority)
|
.GetRequiredService<ConversationHookProvider>()
|
||||||
.ToList();
|
.HooksOrderByPriority;
|
||||||
|
|
||||||
// Before executing functions
|
// Before executing functions
|
||||||
foreach (var hook in hooks)
|
foreach (var hook in hooks)
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,8 @@ public partial class ConversationService : IConversationService
|
||||||
|
|
||||||
db.CreateNewConversation(record);
|
db.CreateNewConversation(record);
|
||||||
|
|
||||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
var hooks = _services.GetServices<IConversationHook>();
|
||||||
|
|
||||||
foreach (var hook in hooks)
|
foreach (var hook in hooks)
|
||||||
{
|
{
|
||||||
// If user connect agent first time
|
// If user connect agent first time
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ public class HumanInterventionNeededFn : IFunctionCallback
|
||||||
|
|
||||||
public async Task<bool> Execute(RoleDialogModel message)
|
public async Task<bool> Execute(RoleDialogModel message)
|
||||||
{
|
{
|
||||||
var hooks = _services.GetServices<IConversationHook>()
|
var hooks = _services
|
||||||
.OrderBy(x => x.Priority)
|
.GetRequiredService<ConversationHookProvider>()
|
||||||
.ToList();
|
.HooksOrderByPriority;
|
||||||
|
|
||||||
foreach (var hook in hooks)
|
foreach (var hook in hooks)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@ public partial class RoutingService
|
||||||
var clonedMessage = RoleDialogModel.From(message);
|
var clonedMessage = RoleDialogModel.From(message);
|
||||||
clonedMessage.FunctionName = name;
|
clonedMessage.FunctionName = name;
|
||||||
|
|
||||||
var hooks = _services.GetServices<IConversationHook>()
|
var hooks = _services
|
||||||
.OrderBy(x => x.Priority)
|
.GetRequiredService<ConversationHookProvider>()
|
||||||
.ToList();
|
.HooksOrderByPriority;
|
||||||
|
|
||||||
var progressService = _services.GetService<IConversationProgressService>();
|
var progressService = _services.GetService<IConversationProgressService>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ public class ChatHubPlugin : IBotSharpPlugin
|
||||||
services.AddScoped<IConversationHook, ChatHubConversationHook>();
|
services.AddScoped<IConversationHook, ChatHubConversationHook>();
|
||||||
services.AddScoped<IConversationHook, StreamingLogHook>();
|
services.AddScoped<IConversationHook, StreamingLogHook>();
|
||||||
services.AddScoped<IConversationHook, WelcomeHook>();
|
services.AddScoped<IConversationHook, WelcomeHook>();
|
||||||
|
services.AddScoped<ConversationHookProvider>();
|
||||||
services.AddScoped<IRoutingHook, StreamingLogHook>();
|
services.AddScoped<IRoutingHook, StreamingLogHook>();
|
||||||
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
|
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
|
||||||
services.AddScoped<ICrontabHook, ChatHubCrontabHook>();
|
services.AddScoped<ICrontabHook, ChatHubCrontabHook>();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue