Skip to content

Commit 209a009

Browse files
authored
Merge pull request #190 from hchen2020/master
Add SelfId in IInstructHook.
2 parents 2bc2367 + a36f3e4 commit 209a009

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Instructs;
44

55
public interface IInstructHook
66
{
7+
string SelfId { get; }
78
Task BeforeCompletion(RoleDialogModel message);
89
Task AfterCompletion(InstructResult result);
910
}

src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Instructs;
44

55
public class InstructHookBase : IInstructHook
66
{
7+
public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!");
78
public virtual async Task AfterCompletion(InstructResult result)
89
{
910
return;

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ namespace BotSharp.Core.Agents.Services;
55

66
public partial class AgentService
77
{
8-
#if !DEBUG
9-
[MemoryCache(10 * 60)]
10-
#endif
8+
[MemoryCache(10 * 60, perInstanceCache: true)]
119
public async Task<Agent> LoadAgent(string id)
1210
{
1311
var hooks = _services.GetServices<IAgentHook>();

src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public async Task<InstructResult> Execute(Agent agent, RoleDialogModel message)
2121
var hooks = _services.GetServices<IInstructHook>();
2222
foreach (var hook in hooks)
2323
{
24+
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agent.Id)
25+
{
26+
continue;
27+
}
28+
2429
await hook.BeforeCompletion(message);
2530

2631
// Interrupted by hook
@@ -42,6 +47,11 @@ public async Task<InstructResult> Execute(Agent agent, RoleDialogModel message)
4247

4348
foreach (var hook in hooks)
4449
{
50+
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agent.Id)
51+
{
52+
continue;
53+
}
54+
4555
await hook.AfterCompletion(response);
4656
}
4757

0 commit comments

Comments
 (0)