11using BotSharp . Abstraction . Functions ;
2- using BotSharp . Abstraction . MLTasks ;
32using BotSharp . Abstraction . Routing . Models ;
43
54namespace BotSharp . Core . Routing ;
@@ -39,6 +38,8 @@ public async Task<bool> Execute(RoleDialogModel message)
3938 }
4039 }
4140
41+ // Set default execution data
42+ message . ExecutionData = JsonSerializer . Deserialize < JsonElement > ( message . FunctionArgs ) ;
4243 return true ;
4344 }
4445
@@ -60,45 +61,65 @@ private bool HasMissingRequiredField(RoleDialogModel message, out string agentId
6061 }
6162
6263 agentId = routingRule . AgentId ;
64+ // Add routed agent
65+ message . FunctionArgs = AppendPropertyToArgs ( message . FunctionArgs , "route_to" , agentId ) ;
6366
6467 // Check required fields
6568 var root = JsonSerializer . Deserialize < JsonElement > ( message . FunctionArgs ) ;
66- bool hasMissingField = false ;
67- string missingFieldName = "" ;
69+ var missingFields = new List < string > ( ) ;
6870 foreach ( var field in routingRule . RequiredFields )
6971 {
7072 if ( ! root . EnumerateObject ( ) . Any ( x => x . Name == field ) )
7173 {
72- message . ExecutionResult = $ "missing { field } .";
73- hasMissingField = true ;
74- missingFieldName = field ;
75- break ;
74+ missingFields . Add ( field ) ;
7675 }
7776 else if ( root . EnumerateObject ( ) . Any ( x => x . Name == field ) &&
7877 string . IsNullOrEmpty ( root . EnumerateObject ( ) . FirstOrDefault ( x => x . Name == field ) . Value . ToString ( ) ) )
7978 {
80- message . ExecutionResult = $ "missing { field } .";
81- hasMissingField = true ;
82- missingFieldName = field ;
83- break ;
79+ missingFields . Add ( field ) ;
8480 }
8581 }
8682
8783 // Check if states contains the field according conversation context.
8884 var states = _services . GetRequiredService < IConversationStateService > ( ) ;
89- if ( ! string . IsNullOrEmpty ( states . GetState ( missingFieldName ) ) )
85+ foreach ( var field in missingFields . ToList ( ) )
9086 {
91- var value = states . GetState ( missingFieldName ) ;
92- message . FunctionArgs = message . FunctionArgs . Substring ( 0 , message . FunctionArgs . Length - 1 ) + $ ", \" { missingFieldName } \" : \" { value } \" " + "}" ;
93- hasMissingField = false ;
94- missingFieldName = "" ;
87+ if ( ! string . IsNullOrEmpty ( states . GetState ( field ) ) )
88+ {
89+ var value = states . GetState ( field ) ;
90+ message . FunctionArgs = AppendPropertyToArgs ( message . FunctionArgs , field , value ) ;
91+ missingFields . Remove ( field ) ;
92+ }
9593 }
9694
97- if ( hasMissingField && ! string . IsNullOrEmpty ( routingRule . RedirectTo ) )
95+ if ( missingFields . Any ( ) )
9896 {
99- agentId = routingRule . RedirectTo ;
97+ // Add field to args
98+ message . FunctionArgs = AppendPropertyToArgs ( message . FunctionArgs , "missing_fields" , missingFields ) ;
99+ message . ExecutionResult = $ "missing some information";
100+
101+ // Handle redirect
102+ if ( ! string . IsNullOrEmpty ( routingRule . RedirectTo ) )
103+ {
104+ agentId = routingRule . RedirectTo ;
105+ var agent = router . GetRecordByAgentId ( agentId ) ;
106+
107+ // Add redirected agent
108+ message . FunctionArgs = AppendPropertyToArgs ( message . FunctionArgs , "redirect_to" , agent . Name ) ;
109+ }
100110 }
101111
102- return hasMissingField ;
112+ return missingFields . Any ( ) ;
113+ }
114+
115+ private string AppendPropertyToArgs ( string args , string key , string value )
116+ {
117+ return args . Substring ( 0 , args . Length - 1 ) + $ ", \" { key } \" : \" { value } \" " + "}" ;
118+ }
119+
120+ private string AppendPropertyToArgs ( string args , string key , IEnumerable < string > values )
121+ {
122+ string fields = string . Join ( "," , values . Select ( x => $ "\" { x } \" ") ) ;
123+ return args . Substring ( 0 , args . Length - 1 ) + $ ", \" { key } \" : [{ fields } ]" + "}" ;
103124 }
104125}
0 commit comments