11using BotSharp . Abstraction . Coding ;
2+ using BotSharp . Abstraction . Coding . Constants ;
23using BotSharp . Abstraction . Conversations ;
34using BotSharp . Abstraction . Models ;
45using BotSharp . Abstraction . Repositories . Filters ;
@@ -39,23 +40,19 @@ public async Task<IEnumerable<string>> Trigger(IRuleTrigger trigger, string text
3940
4041 var isTriggered = true ;
4142
42- // Apply code trigger
43- if ( options != null
44- && ! string . IsNullOrWhiteSpace ( options . CodeProcessor )
45- && ! string . IsNullOrWhiteSpace ( options . AgentId ) )
43+ // Code trigger
44+ if ( options != null )
4645 {
47- var scriptName = options . CodeScriptName ?? $ "{ trigger . Name } _cron.py";
48- isTriggered = await TriggerCodeScript ( options . AgentId , scriptName , options . CodeProcessor , trigger . Name , options . Arguments , options . States ) ;
46+ isTriggered = await TriggerCodeScript ( trigger . Name , options ) ;
4947 }
5048
5149 if ( ! isTriggered )
5250 {
5351 return newConversationIds ;
5452 }
5553
56- var filteredAgents = agents . Items . Where ( x => x . Rules . Exists ( r => r . TriggerName == trigger . Name && ! x . Disabled ) ) . ToList ( ) ;
57-
5854 // Trigger agents
55+ var filteredAgents = agents . Items . Where ( x => x . Rules . Exists ( r => r . TriggerName == trigger . Name && ! x . Disabled ) ) . ToList ( ) ;
5956 foreach ( var agent in filteredAgents )
6057 {
6158 var convService = _services . GetRequiredService < IConversationService > ( ) ;
@@ -93,32 +90,40 @@ await convService.SendMessage(agent.Id,
9390 }
9491
9592 #region Private methods
96- private async Task < bool > TriggerCodeScript ( string agentId , string scriptName , string codeProcessor , string triggerName , JsonDocument ? args = null , IEnumerable < MessageState > ? states = null )
93+ private async Task < bool > TriggerCodeScript ( string triggerName , RuleTriggerOptions options )
9794 {
98- var processor = _services . GetServices < ICodeProcessor > ( ) . FirstOrDefault ( x => x . Provider . IsEqualTo ( codeProcessor ) ) ;
95+ var agentId = options . AgentId ;
96+ if ( string . IsNullOrWhiteSpace ( agentId ) )
97+ {
98+ return false ;
99+ }
100+
101+ var provider = options . CodeProcessor ?? BuiltInCodeProcessor . PyInterpreter ;
102+ var processor = _services . GetServices < ICodeProcessor > ( ) . FirstOrDefault ( x => x . Provider . IsEqualTo ( provider ) ) ;
99103 if ( processor == null )
100104 {
101- _logger . LogWarning ( $ "Unable to find code processor: { codeProcessor } .") ;
105+ _logger . LogWarning ( $ "Unable to find code processor: { provider } .") ;
102106 return false ;
103107 }
104108
105109 var agentService = _services . GetRequiredService < IAgentService > ( ) ;
110+ var scriptName = options . CodeScriptName ?? $ "{ triggerName } _rule.py";
106111 var codeScript = await agentService . GetAgentCodeScript ( agentId , scriptName , scriptType : AgentCodeScriptType . Src ) ;
107112
113+ var msg = $ "rule trigger ({ triggerName } ) code script ({ scriptName } ) in agent ({ agentId } ) => args: { options . Arguments ? . RootElement . GetRawText ( ) } .";
114+
108115 if ( string . IsNullOrWhiteSpace ( codeScript ) )
109116 {
110- _logger . LogWarning ( $ "Unable to find code script ( { scriptName } ) in agent ( { agentId } ) .") ;
117+ _logger . LogWarning ( $ "Unable to find { msg } .") ;
111118 return false ;
112119 }
113120
114- var msg = $ "rule trigger ({ triggerName } ) code script ({ scriptName } ) in agent ({ agentId } ) => args: { args ? . RootElement . GetRawText ( ) } .";
115-
116121 try
117122 {
118123 var response = await processor . RunAsync ( codeScript , options : new ( )
119124 {
120125 ScriptName = scriptName ,
121- Arguments = BuildArguments ( args , states )
126+ Arguments = BuildArguments ( options . Arguments , options . States )
122127 } ) ;
123128
124129 if ( response == null || ! response . Success )
@@ -140,7 +145,7 @@ private async Task<bool> TriggerCodeScript(string agentId, string scriptName, st
140145 result = false ;
141146 }
142147
143- _logger . Log ( logLevel , $ "Code result ({ response . Result } ) from { msg } ") ;
148+ _logger . Log ( logLevel , $ "Code script execution result ({ response . Result } ) from { msg } ") ;
144149 return result ;
145150 }
146151 catch ( Exception ex )
@@ -166,7 +171,7 @@ private IEnumerable<KeyValue> BuildArguments(JsonDocument? args, IEnumerable<Mes
166171
167172 if ( args != null )
168173 {
169- dict [ "trigger_input " ] = args . RootElement . GetRawText ( ) ;
174+ dict [ "trigger_args " ] = args . RootElement . GetRawText ( ) ;
170175 }
171176
172177 return dict . Select ( x => new KeyValue ( x . Key , x . Value ) ) ;
0 commit comments