Fix OnPlanningCompleted hook

This commit is contained in:
Haiping Chen 2024-10-12 09:58:42 -05:00
parent 147ec7662e
commit 1c53cb605e
2 changed files with 26 additions and 3 deletions

View file

@ -4,7 +4,7 @@ namespace BotSharp.Core.Infrastructures;
public static class HookEmitter
{
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Action<T> action)
public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> action)
{
var logger = services.GetRequiredService<ILogger<T>>();
var result = new HookEmittedResult();
@ -25,4 +25,26 @@ public static class HookEmitter
return result;
}
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Func<T, Task> action)
{
var logger = services.GetRequiredService<ILogger<T>>();
var result = new HookEmittedResult();
var hooks = services.GetServices<T>();
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;
}
}

View file

@ -67,8 +67,9 @@ public class SummaryPlanFn : IFunctionCallback
var summary = await GetAiResponse(plannerAgent);
message.Content = summary.Content;
await HookEmitter.Emit<IPlanningHook>(_services, x =>
x.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message));
await HookEmitter.Emit<IPlanningHook>(_services, async hook =>
await hook.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message)
);
return true;
}