From 1c53cb605e2cef97a48ae424f94ee5a81703ec6c Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sat, 12 Oct 2024 09:58:42 -0500 Subject: [PATCH] Fix OnPlanningCompleted hook --- .../Infrastructures/HookEmitter.cs | 24 ++++++++++++++++++- .../Functions/SummaryPlanFn.cs | 5 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs index 2863ff02..cdd8707b 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs @@ -4,7 +4,7 @@ namespace BotSharp.Core.Infrastructures; public static class HookEmitter { - public static async Task Emit(IServiceProvider services, Action action) + public static HookEmittedResult Emit(IServiceProvider services, Action action) { var logger = services.GetRequiredService>(); var result = new HookEmittedResult(); @@ -25,4 +25,26 @@ public static class HookEmitter return result; } + + public static async Task Emit(IServiceProvider services, Func action) + { + var logger = services.GetRequiredService>(); + var result = new HookEmittedResult(); + var hooks = services.GetServices(); + + foreach (var hook in hooks) + { + try + { + logger.LogInformation($"Emit hook action on {action.Method.Name}({hook.GetType().Name})"); + await action(hook); + } + catch (Exception ex) + { + logger.LogError(ex.ToString()); + } + } + + return result; + } } diff --git a/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs b/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs index e9fd3b37..e08e6f6f 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs @@ -67,8 +67,9 @@ public class SummaryPlanFn : IFunctionCallback var summary = await GetAiResponse(plannerAgent); message.Content = summary.Content; - await HookEmitter.Emit(_services, x => - x.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message)); + await HookEmitter.Emit(_services, async hook => + await hook.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message) + ); return true; }