@@ -133,7 +133,9 @@ public override async Task<TResult> AssembleResult<TResult>(AiResponse response)
133133 } ) ;
134134
135135 var matches = Regex . Matches ( presetResponse . Messages . Random ( ) . Speech , "\" .*?\" " ) . Cast < Match > ( ) ;
136- var speech = matches . ToList ( ) . Random ( ) ;
136+ var speech = matches . Count ( ) == 0 ? String . Empty : matches . ToList ( ) . Random ( ) . Value ;
137+
138+ var contexts = HandleContexts ( presetResponse ) ;
137139
138140 var aiResponse = new AIResponseResult
139141 {
@@ -143,17 +145,33 @@ public override async Task<TResult> AssembleResult<TResult>(AiResponse response)
143145 {
144146 IntentName = response . Intent
145147 } ,
148+ Intent = response . Intent ,
146149 Fulfillment = new AIResponseFulfillment
147150 {
148151 Messages = presetResponse . Messages . ToList < object > ( ) ,
149- Speech = speech . Value . Substring ( 1 , speech . Length - 2 )
152+ Speech = speech . Length > 1 ? speech . Substring ( 1 , speech . Length - 2 ) : String . Empty
150153 } ,
151154 Score = response . Score ,
152155 Source = response . Source ,
156+ Contexts = contexts . ToArray ( ) ,
153157 Parameters = presetResponse . Parameters . Where ( x => ! String . IsNullOrEmpty ( x . Value ) ) . ToDictionary ( item => item . Name , item => ( object ) item . Value )
154158 } ;
155159
156160 return ( TResult ) ( object ) aiResponse ;
157161 }
162+
163+ private List < AIContext > HandleContexts ( IntentResponse response )
164+ {
165+ var newContexts = response . Contexts . Select ( x => new AIContext
166+ {
167+ Name = x . Name ,
168+ Lifespan = x . Lifespan ,
169+ Parameters = response . Parameters . Select ( p => new KeyValuePair < string , object > ( p . Name , p . Value ) ) . ToDictionary ( d => d . Key , d => d . Value == null ? String . Empty : d . Value )
170+ } ) . ToList ( ) ;
171+
172+ // persist
173+
174+ return newContexts ;
175+ }
158176 }
159177}
0 commit comments