1
1
using BotSharp . Abstraction . Functions ;
2
- using BotSharp . Abstraction . MLTasks ;
3
2
using BotSharp . Abstraction . Routing . Models ;
4
3
5
4
namespace BotSharp . Core . Routing ;
@@ -39,6 +38,8 @@ public async Task<bool> Execute(RoleDialogModel message)
39
38
}
40
39
}
41
40
41
+ // Set default execution data
42
+ message . ExecutionData = JsonSerializer . Deserialize < JsonElement > ( message . FunctionArgs ) ;
42
43
return true ;
43
44
}
44
45
@@ -60,45 +61,65 @@ private bool HasMissingRequiredField(RoleDialogModel message, out string agentId
60
61
}
61
62
62
63
agentId = routingRule . AgentId ;
64
+ // Add routed agent
65
+ message . FunctionArgs = AppendPropertyToArgs ( message . FunctionArgs , "route_to" , agentId ) ;
63
66
64
67
// Check required fields
65
68
var root = JsonSerializer . Deserialize < JsonElement > ( message . FunctionArgs ) ;
66
- bool hasMissingField = false ;
67
- string missingFieldName = "" ;
69
+ var missingFields = new List < string > ( ) ;
68
70
foreach ( var field in routingRule . RequiredFields )
69
71
{
70
72
if ( ! root . EnumerateObject ( ) . Any ( x => x . Name == field ) )
71
73
{
72
- message . ExecutionResult = $ "missing { field } .";
73
- hasMissingField = true ;
74
- missingFieldName = field ;
75
- break ;
74
+ missingFields . Add ( field ) ;
76
75
}
77
76
else if ( root . EnumerateObject ( ) . Any ( x => x . Name == field ) &&
78
77
string . IsNullOrEmpty ( root . EnumerateObject ( ) . FirstOrDefault ( x => x . Name == field ) . Value . ToString ( ) ) )
79
78
{
80
- message . ExecutionResult = $ "missing { field } .";
81
- hasMissingField = true ;
82
- missingFieldName = field ;
83
- break ;
79
+ missingFields . Add ( field ) ;
84
80
}
85
81
}
86
82
87
83
// Check if states contains the field according conversation context.
88
84
var states = _services . GetRequiredService < IConversationStateService > ( ) ;
89
- if ( ! string . IsNullOrEmpty ( states . GetState ( missingFieldName ) ) )
85
+ foreach ( var field in missingFields . ToList ( ) )
90
86
{
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
+ }
95
93
}
96
94
97
- if ( hasMissingField && ! string . IsNullOrEmpty ( routingRule . RedirectTo ) )
95
+ if ( missingFields . Any ( ) )
98
96
{
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
+ }
100
110
}
101
111
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 } ]" + "}" ;
103
124
}
104
125
}
0 commit comments