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);
|
||||
fileStorage.DeleteMessageFiles(conversationId, deleteMessageIds, messageId, newMessageId);
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
var hooks = _services.GetServices<IConversationHook>();
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
await hook.OnMessageDeleted(conversationId, messageId);
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ public partial class ConversationService : IConversationService
|
|||
states.CleanStates(excludedStates);
|
||||
}
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>()
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToList();
|
||||
var hooks = _services
|
||||
.GetRequiredService<ConversationHookProvider>()
|
||||
.HooksOrderByPriority;
|
||||
|
||||
// Before executing functions
|
||||
foreach (var hook in hooks)
|
||||
|
|
|
|||
|
|
@ -103,7 +103,8 @@ public partial class ConversationService : IConversationService
|
|||
|
||||
db.CreateNewConversation(record);
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
var hooks = _services.GetServices<IConversationHook>();
|
||||
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
// If user connect agent first time
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ public class HumanInterventionNeededFn : IFunctionCallback
|
|||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var hooks = _services.GetServices<IConversationHook>()
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToList();
|
||||
var hooks = _services
|
||||
.GetRequiredService<ConversationHookProvider>()
|
||||
.HooksOrderByPriority;
|
||||
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@ public partial class RoutingService
|
|||
var clonedMessage = RoleDialogModel.From(message);
|
||||
clonedMessage.FunctionName = name;
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>()
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToList();
|
||||
var hooks = _services
|
||||
.GetRequiredService<ConversationHookProvider>()
|
||||
.HooksOrderByPriority;
|
||||
|
||||
var progressService = _services.GetService<IConversationProgressService>();
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ public class ChatHubPlugin : IBotSharpPlugin
|
|||
services.AddScoped<IConversationHook, ChatHubConversationHook>();
|
||||
services.AddScoped<IConversationHook, StreamingLogHook>();
|
||||
services.AddScoped<IConversationHook, WelcomeHook>();
|
||||
services.AddScoped<ConversationHookProvider>();
|
||||
services.AddScoped<IRoutingHook, StreamingLogHook>();
|
||||
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
|
||||
services.AddScoped<ICrontabHook, ChatHubCrontabHook>();
|
||||
|
|
|
|||
Loading…
Reference in a new issue