Add SelfId in IInstructHook.

This commit is contained in:
hchen 2023-10-25 10:48:25 -05:00
parent 2bc2367536
commit 4c54a62557
4 changed files with 13 additions and 3 deletions

View file

@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Instructs;
public interface IInstructHook
{
string SelfId { get; }
Task BeforeCompletion(RoleDialogModel message);
Task AfterCompletion(InstructResult result);
}

View file

@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Instructs;
public class InstructHookBase : IInstructHook
{
public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!");
public virtual async Task AfterCompletion(InstructResult result)
{
return;

View file

@ -5,9 +5,7 @@ namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
#if !DEBUG
[MemoryCache(10 * 60)]
#endif
[MemoryCache(10 * 60, perInstanceCache: true)]
public async Task<Agent> LoadAgent(string id)
{
var hooks = _services.GetServices<IAgentHook>();

View file

@ -21,6 +21,11 @@ public partial class InstructService : IInstructService
var hooks = _services.GetServices<IInstructHook>();
foreach (var hook in hooks)
{
if (hook.SelfId != agent.Id)
{
continue;
}
await hook.BeforeCompletion(message);
// Interrupted by hook
@ -42,6 +47,11 @@ public partial class InstructService : IInstructService
foreach (var hook in hooks)
{
if (hook.SelfId != agent.Id)
{
continue;
}
await hook.AfterCompletion(response);
}