From 4c54a62557764aa6e148c957c874191082cc0450 Mon Sep 17 00:00:00 2001 From: hchen Date: Wed, 25 Oct 2023 10:48:25 -0500 Subject: [PATCH 1/2] Add SelfId in IInstructHook. --- .../BotSharp.Abstraction/Instructs/IInstructHook.cs | 1 + .../BotSharp.Abstraction/Instructs/InstructHookBase.cs | 1 + .../Agents/Services/AgentService.LoadAgent.cs | 4 +--- .../BotSharp.Core/Instructs/InstructService.cs | 10 ++++++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs index 8d5dea5e..9ea5f5de 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs @@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Instructs; public interface IInstructHook { + string SelfId { get; } Task BeforeCompletion(RoleDialogModel message); Task AfterCompletion(InstructResult result); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs index fc1f1dd5..9a973b6e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs @@ -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; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 91c82886..301da3b7 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -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 LoadAgent(string id) { var hooks = _services.GetServices(); diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs index ec64f6af..3718e1fe 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs @@ -21,6 +21,11 @@ public partial class InstructService : IInstructService var hooks = _services.GetServices(); 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); } From a36f3e4b6c5a91e16ba96da1cee44b7f552784e9 Mon Sep 17 00:00:00 2001 From: hchen Date: Wed, 25 Oct 2023 10:55:12 -0500 Subject: [PATCH 2/2] Allow empty SelfId --- src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs index 3718e1fe..bb328753 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs @@ -21,7 +21,7 @@ public partial class InstructService : IInstructService var hooks = _services.GetServices(); foreach (var hook in hooks) { - if (hook.SelfId != agent.Id) + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agent.Id) { continue; } @@ -47,7 +47,7 @@ public partial class InstructService : IInstructService foreach (var hook in hooks) { - if (hook.SelfId != agent.Id) + if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agent.Id) { continue; }