From a0a62d5026db552d571627345452cdb79b520e7a Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Fri, 17 Jul 2026 17:24:03 +0800 Subject: [PATCH 1/2] Refactor hook base classes to use abstract SelfId --- .../Agents/AgentHookBase.cs | 2 +- .../Conversations/ConversationHookBase.cs | 1 + .../Instructs/InstructHookBase.cs | 4 ++-- .../Hooks/RealtimeConversationHook.cs | 3 +++ .../Evaluations/EvaluationConversationHook.cs | 2 ++ .../Translation/TranslationResponseHook.cs | 2 ++ .../Hooks/RateLimitConversationHook.cs | 3 +++ .../Hooks/ChatHubConversationHook.cs | 2 ++ .../Hooks/StreamingLogHook.cs | 2 ++ .../Hooks/WelcomeHook.cs | 2 ++ .../BotSharp.Plugin.Dashboard.csproj | 4 ++++ .../DashboardPlugin.cs | 5 +---- .../Hooks/StatsConversationHook.cs | 16 ---------------- .../RoutingConversationHook.cs | 18 ++++++++++-------- .../Hooks/SqlDriverConversationHook.cs | 12 ------------ .../SqlDriverPlugin.cs | 1 - .../Hooks/TwilioConversationHook.cs | 3 ++- .../Hooks/WebDriverConversationHook.cs | 3 +++ 18 files changed, 40 insertions(+), 45 deletions(-) delete mode 100644 src/Plugins/BotSharp.Plugin.Dashboard/Hooks/StatsConversationHook.cs delete mode 100644 src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverConversationHook.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs index f735da9e6..d3a87f65b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs @@ -5,7 +5,7 @@ namespace BotSharp.Abstraction.Agents; public abstract class AgentHookBase : IAgentHook { - public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!"); + public abstract string SelfId { get; } protected Agent _agent; public Agent Agent => _agent; diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs index 5f86ba1f9..91ce53188 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs @@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Conversations; public abstract class ConversationHookBase : IConversationHook { + public abstract string SelfId { get; } public Agent Agent { get; private set; } public Conversation Conversation { get; private set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs index 81023135f..8e1d50457 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs @@ -4,9 +4,9 @@ namespace BotSharp.Abstraction.Instructs; -public class InstructHookBase : IInstructHook +public abstract class InstructHookBase : IInstructHook { - public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!"); + public abstract string SelfId { get; } public virtual async Task BeforeCompletion(Agent agent, RoleDialogModel message) { diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs index 369f8b87e..620663f6a 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs @@ -6,6 +6,9 @@ namespace BotSharp.Core.Realtime.Hooks; public class RealtimeConversationHook : ConversationHookBase, IConversationHook { private readonly IServiceProvider _services; + + public override string SelfId => string.Empty; + public RealtimeConversationHook(IServiceProvider services) { _services = services; diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationConversationHook.cs b/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationConversationHook.cs index 80757219e..b0ccb9e34 100644 --- a/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationConversationHook.cs +++ b/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationConversationHook.cs @@ -8,6 +8,8 @@ public class EvaluationConversationHook : ConversationHookBase private readonly IExecutionLogger _logger; private readonly ConversationSetting _convSettings; + public override string SelfId => string.Empty; + public EvaluationConversationHook(IExecutionLogger logger, ConversationSetting convSettings) { _logger = logger; diff --git a/src/Infrastructure/BotSharp.Core/Translation/TranslationResponseHook.cs b/src/Infrastructure/BotSharp.Core/Translation/TranslationResponseHook.cs index d5e61c126..bf6bcbdf9 100644 --- a/src/Infrastructure/BotSharp.Core/Translation/TranslationResponseHook.cs +++ b/src/Infrastructure/BotSharp.Core/Translation/TranslationResponseHook.cs @@ -14,6 +14,8 @@ public class TranslationResponseHook : ConversationHookBase private readonly IConversationStateService _states; private const string AIAssistant = BuiltInAgentId.AIAssistant; + public override string SelfId => string.Empty; + public TranslationResponseHook(IServiceProvider services, IConversationStateService states) { diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs index 02cafbd08..74aa46d56 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs @@ -13,6 +13,9 @@ public class RateLimitConversationHook : ConversationHookBase { private readonly IServiceProvider _services; private readonly ILogger _logger; + + public override string SelfId => string.Empty; + public RateLimitConversationHook(IServiceProvider services, ILogger logger) { _services = services; diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index 18a3990c5..a4fd35d14 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -19,6 +19,8 @@ public class ChatHubConversationHook : ConversationHookBase private readonly BotSharpOptions _options; private readonly ChatHubSettings _settings; + public override string SelfId => string.Empty; + public ChatHubConversationHook( IServiceProvider services, IHubContext chatHub, diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 63f037784..48c5f03b7 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -21,6 +21,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR private readonly IAgentService _agentService; private readonly IRoutingContext _routingCtx; + public override string SelfId => string.Empty; + public StreamingLogHook( ConversationSetting convSettings, BotSharpOptions options, diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs index 92e94f364..a9c0afabc 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs @@ -15,6 +15,8 @@ public class WelcomeHook : ConversationHookBase private readonly BotSharpOptions _options; private readonly ChatHubSettings _settings; + public override string SelfId => string.Empty; + public WelcomeHook( IServiceProvider services, IHubContext chatHub, diff --git a/src/Plugins/BotSharp.Plugin.Dashboard/BotSharp.Plugin.Dashboard.csproj b/src/Plugins/BotSharp.Plugin.Dashboard/BotSharp.Plugin.Dashboard.csproj index fb10312ac..b71451228 100644 --- a/src/Plugins/BotSharp.Plugin.Dashboard/BotSharp.Plugin.Dashboard.csproj +++ b/src/Plugins/BotSharp.Plugin.Dashboard/BotSharp.Plugin.Dashboard.csproj @@ -13,4 +13,8 @@ + + + + diff --git a/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs b/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs index 08ff4e3af..4c2edfe43 100644 --- a/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs @@ -1,9 +1,6 @@ using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Plugins; using BotSharp.Abstraction.Plugins.Models; -using BotSharp.Abstraction.Settings; -using BotSharp.Abstraction.Statistics.Settings; -using BotSharp.Plugin.Dashboard.Hooks; namespace BotSharp.Plugin.Dashboard; @@ -15,7 +12,7 @@ public class DashboardPlugin : IBotSharpPlugin public string IconUrl => "https://cdn0.iconfinder.com/data/icons/octicons/1024/dashboard-512.png"; public void RegisterDI(IServiceCollection services, IConfiguration config) { - services.AddScoped(); + } public bool AttachMenu(List menu) diff --git a/src/Plugins/BotSharp.Plugin.Dashboard/Hooks/StatsConversationHook.cs b/src/Plugins/BotSharp.Plugin.Dashboard/Hooks/StatsConversationHook.cs deleted file mode 100644 index c590318da..000000000 --- a/src/Plugins/BotSharp.Plugin.Dashboard/Hooks/StatsConversationHook.cs +++ /dev/null @@ -1,16 +0,0 @@ -using BotSharp.Abstraction.Conversations; - -namespace BotSharp.Plugin.Dashboard.Hooks; - -public class StatsConversationHook : ConversationHookBase -{ - private readonly IServiceProvider _services; - public StatsConversationHook(IServiceProvider services) - { - _services = services; - } - - public override async Task OnConversationInitialized(Conversation conversation) - { - } -} diff --git a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs index b65e45c56..c68b68777 100644 --- a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs @@ -1,18 +1,17 @@ +using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Routing; +using BotSharp.Abstraction.Routing.Settings; +using BotSharp.Abstraction.Templating; +using BotSharp.Plugin.RoutingSpeeder.Providers; +using BotSharp.Plugin.RoutingSpeeder.Settings; using Microsoft.Extensions.DependencyInjection; using System; +using System.IO; using System.Linq; using System.Threading.Tasks; -using BotSharp.Plugin.RoutingSpeeder.Settings; -using BotSharp.Abstraction.Templating; -using BotSharp.Plugin.RoutingSpeeder.Providers; -using BotSharp.Abstraction.Agents; -using System.IO; -using BotSharp.Abstraction.Routing.Settings; -using BotSharp.Abstraction.Routing.Models; -using BotSharp.Abstraction.Routing; namespace BotSharp.Plugin.RoutingSpeeder; @@ -20,6 +19,9 @@ public class RoutingConversationHook: ConversationHookBase { private readonly IServiceProvider _services; private RouterSpeederSettings _settings; + + public override string SelfId => string.Empty; + public RoutingConversationHook(IServiceProvider service, RouterSpeederSettings settings) { _services = service; diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverConversationHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverConversationHook.cs deleted file mode 100644 index 05749bc57..000000000 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverConversationHook.cs +++ /dev/null @@ -1,12 +0,0 @@ - -namespace BotSharp.Plugin.SqlDriver.Hooks; - -public class SqlDriverConversationHook : ConversationHookBase, IConversationHook -{ - public override Task OnResponseGenerated(RoleDialogModel message) - { - // Render function buttons - - return base.OnResponseGenerated(message); - } -} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs index f85d83874..df9bc0f7e 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs @@ -28,7 +28,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config) services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs index 8762c5649..cb1dad813 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Routing; -using BotSharp.Abstraction.Routing.Enums; using BotSharp.Abstraction.Routing.Models; using BotSharp.Plugin.Twilio.Interfaces; using BotSharp.Plugin.Twilio.Models; @@ -15,6 +14,8 @@ public class TwilioConversationHook : ConversationHookBase, IConversationHook private readonly TwilioSetting _setting; private readonly ILogger _logger; + public override string SelfId => string.Empty; + public TwilioConversationHook(IServiceProvider services, TwilioSetting setting, ILogger logger) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs index 39929abaf..333940e0d 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs @@ -5,6 +5,9 @@ namespace BotSharp.Plugin.WebDriver.Hooks; public class WebDriverConversationHook : ConversationHookBase { private readonly IServiceProvider _services; + + public override string SelfId => string.Empty; + public WebDriverConversationHook(IServiceProvider services) { _services = services; From 4d70daced628b0e9adb447907cf44745c891c416 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Thu, 20 Aug 2026 15:57:56 +0800 Subject: [PATCH 2/2] optimize hook --- .../Conversations/ConversationHookBase.cs | 4 ++++ .../BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs index 91ce53188..f25a411eb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs @@ -5,6 +5,10 @@ namespace BotSharp.Abstraction.Conversations; public abstract class ConversationHookBase : IConversationHook { public abstract string SelfId { get; } + + public virtual bool IsMatch(string agentId) + => string.IsNullOrEmpty(SelfId) || SelfId == agentId; + public Agent Agent { get; private set; } public Conversation Conversation { get; private set; } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs index 620663f6a..bea55c595 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs @@ -14,7 +14,7 @@ public RealtimeConversationHook(IServiceProvider services) _services = services; } - public async Task OnFunctionExecuting(RoleDialogModel message, InvokeFunctionOptions? options = null) + public override async Task OnFunctionExecuting(RoleDialogModel message, InvokeFunctionOptions? options = null) { var hub = _services.GetRequiredService(); if (hub.HubConn == null) @@ -37,7 +37,7 @@ public async Task OnFunctionExecuting(RoleDialogModel message, InvokeFunctionOpt await Task.CompletedTask; } - public async Task OnFunctionExecuted(RoleDialogModel message, InvokeFunctionOptions? options = null) + public override async Task OnFunctionExecuted(RoleDialogModel message, InvokeFunctionOptions? options = null) { var hub = _services.GetRequiredService(); if (options?.From != InvokeSource.Llm || hub.HubConn == null)