1+ using BotSharp . Abstraction . Hooks ;
12using BotSharp . Abstraction . Routing . Models ;
23using System . Collections . Concurrent ;
34
@@ -15,7 +16,16 @@ public async Task<Agent> LoadAgent(string id, bool loadUtility = true)
1516 return null ;
1617 }
1718
18- HookEmitter . Emit < IAgentHook > ( _services , hook => hook . OnAgentLoading ( ref id ) , id ) ;
19+ var hooks = _services . GetHooks < IAgentHook > ( id ) ;
20+ foreach ( var hook in hooks )
21+ {
22+ var newId = await hook . OnAgentLoading ( id ) ;
23+ if ( ! string . IsNullOrEmpty ( newId ) && newId != id )
24+ {
25+ id = newId ;
26+ break ; // Only the first hook that redirects takes effect
27+ }
28+ }
1929
2030 var agent = await GetAgent ( id ) ;
2131 if ( agent == null )
@@ -35,35 +45,35 @@ public async Task<Agent> LoadAgent(string id, bool loadUtility = true)
3545 PopulateState ( agent ) ;
3646
3747 // After agent is loaded
38- HookEmitter . Emit < IAgentHook > ( _services , hook => {
48+ await HookEmitter . Emit < IAgentHook > ( _services , async hook => {
3949 hook . SetAgent ( agent ) ;
4050
4151 if ( ! string . IsNullOrEmpty ( agent . Instruction ) )
4252 {
43- hook . OnInstructionLoaded ( agent . Instruction , agent . TemplateDict ) ;
53+ await hook . OnInstructionLoaded ( agent . Instruction , agent . TemplateDict ) ;
4454 }
4555
4656 if ( agent . Functions != null )
4757 {
48- hook . OnFunctionsLoaded ( agent . Functions ) ;
58+ await hook . OnFunctionsLoaded ( agent . Functions ) ;
4959 }
5060
5161 if ( agent . Samples != null )
5262 {
53- hook . OnSamplesLoaded ( agent . Samples ) ;
63+ await hook . OnSamplesLoaded ( agent . Samples ) ;
5464 }
5565
5666 if ( loadUtility && ! agent . Utilities . IsNullOrEmpty ( ) )
5767 {
58- hook . OnAgentUtilityLoaded ( agent ) ;
68+ await hook . OnAgentUtilityLoaded ( agent ) ;
5969 }
6070
6171 if ( ! agent . McpTools . IsNullOrEmpty ( ) )
6272 {
63- hook . OnAgentMcpToolLoaded ( agent ) ;
73+ await hook . OnAgentMcpToolLoaded ( agent ) ;
6474 }
6575
66- hook . OnAgentLoaded ( agent ) ;
76+ await hook . OnAgentLoaded ( agent ) ;
6777
6878 } , id ) ;
6979
0 commit comments