@@ -17,7 +17,8 @@ module.exports = {
1717 loadRasaModel : loadRasaModel ,
1818 conversationParseRequest : conversationParseRequest ,
1919 restartRasaCoreConversation : restartRasaCoreConversation ,
20- getConversationStory : getConversationStory
20+ getConversationStory : getConversationStory ,
21+ runActionInConversation : runActionInConversation
2122} ;
2223
2324/* -------------------------------- Util Functions ----------------------------------------------------------------------------------------------------- */
@@ -207,25 +208,16 @@ function modelParseRequest(req, res, next) {
207208function getConversationStory ( req , res , next ) {
208209 try {
209210 logger . winston . info ( "Routing to Model Rasa Story Request -> " + global . rasa_endpoint + "/conversations/" + req . query . conversation_id + "/story" ) ;
210- request ( { method : 'GET' , uri : global . rasa_endpoint + "/conversations/" + req . query . conversation_id + "/story" } ,
211- function ( err , response , body ) {
211+ request ( { method : 'GET' , uri : global . rasa_endpoint + "/conversations/" + req . query . conversation_id + "/story" } , function ( err , response , body ) {
212212 try {
213213 logger . winston . verbose ( 'Rasa Response: ' + body . substring ( 1 , 200 ) + ' ... ' ) ;
214214 logs . logRequest ( req , 'parse' ,
215215 {
216216 server_response : body ,
217217 query : req . body . q
218218 } ) ;
219- //Update conversation table with updated conversation response from rasa ...
220- //TODO: Add story?
221- db . run ( 'update conversations set story = ? where conversation_id = ?' , [ body , req . query . conversation_id ] , function ( err ) {
222- if ( err ) {
223- logger . winston . info ( "Error updating the record" ) ;
224- } else {
225- //http_code, res, body, headers, type
226- sendOutput ( 200 , res , body , { 'Content-Type' : 'plain/text' } , '' ) ;
227- }
228- } ) ;
219+ updateStory ( req . query . conversation_id , body ) ;
220+ sendOutput ( 200 , res , body , { 'Content-Type' : 'plain/text' } , '' ) ;
229221 } catch ( err ) {
230222 logger . winston . error ( err ) ;
231223 sendOutput ( 500 , res , '{"error" : ' + err + "}" ) ;
@@ -236,8 +228,27 @@ function getConversationStory(req, res, next) {
236228 }
237229}
238230
231+ function updateStory ( conversation_id , story ) {
232+ db . run ( 'update conversations set story = ? where conversation_id = ?' , [ story , conversation_id ] , function ( err ) {
233+ if ( err ) {
234+ logger . winston . info ( "Error updating the record" ) ;
235+ } else {
236+ logger . winston . info ( "Updated story" ) ;
237+ }
238+ } ) ;
239+ }
239240
240241
242+ function updateConversation ( conversation_id , conversation ) {
243+ //Update the DB with the latest results
244+ db . run ( 'update conversations set conversation = ? where conversation_id = ?' , [ conversation , conversation_id ] , function ( err ) {
245+ if ( err ) {
246+ logger . winston . error ( "Error updating the record" ) ;
247+ } else {
248+ logger . winston . info ( "Updated conversation" ) ;
249+ }
250+ } ) ;
251+ }
241252
242253function conversationParseRequest ( req , res , next ) {
243254 try {
@@ -251,20 +262,11 @@ function conversationParseRequest(req, res, next) {
251262 server_response : body ,
252263 query : req . body . q
253264 } ) ;
254- //Maybe we should run this before the DB update and save both to the DB at the same time and then return both responses to the client
255- request ( { method : 'POST' , uri : global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/predict" , body : JSON . stringify ( req . body ) } ,
256- function ( err , response , predict_body ) {
257- //console.log(body);
258- //Update conversation table with updated conversation response from rasa ...
259- db . run ( 'update conversations set conversation = ? where conversation_id = ?' , [ predict_body , req . body . conversation_id ] , function ( err ) {
260- if ( err ) {
261- logger . winston . error ( "Error updating the record" ) ;
262- } else {
263-
264- sendOutput ( 200 , res , predict_body ) ;
265- }
266- } ) ;
267- } ) ;
265+
266+ request ( { method : 'POST' , uri : global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/predict" , body : JSON . stringify ( req . body ) } , function ( err , response , predict_body ) {
267+ updateConversation ( req . body . conversation_id , predict_body ) ;
268+ sendOutput ( 200 , res , predict_body ) ;
269+ } ) ;
268270 } catch ( err ) {
269271 logger . winston . error ( err ) ;
270272 sendOutput ( 500 , res , '{"error" : ' + err + "}" ) ;
@@ -275,31 +277,40 @@ function conversationParseRequest(req, res, next) {
275277 }
276278}
277279
280+ function runActionInConversation ( req , res , next ) {
281+ logger . winston . info ( "Rasa Core Run Action Request -> " + global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/execute" ) ;
282+ try {
283+ request ( { method : "POST" , uri : global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/execute" , body : JSON . stringify ( req . body . action ) } , function ( err , response , execute_body ) {
284+ if ( err ) {
285+ logger . winston . error ( err ) ;
286+ sendOutput ( 500 , res , '{"error" : "Exception caught !!"}' ) ;
287+ return ;
288+ }
289+ logger . winston . verbose ( "Run Action Response" + JSON . stringify ( execute_body ) ) ;
290+ updateConversation ( req . body . conversation_id , execute_body ) ;
291+ sendOutput ( 200 , res , execute_body ) ;
292+ } ) ;
293+ } catch ( err ) {
294+ logger . winston . error ( err ) ;
295+ sendOutput ( 500 , res , '{"error" : "Exception caught !!"}' ) ;
296+ return ;
297+ }
298+ }
299+
278300
279301function restartRasaCoreConversation ( req , res , next ) {
280302 logger . winston . info ( "Rasa Core Restart Request -> " + global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/tracker/events" ) ;
281303 try {
282304 var body = JSON . stringify ( { "event" : "restart" } ) ;
283- request ( {
284- method : "POST" ,
285- uri : global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/tracker/events" ,
286- body : body
287- } , function ( err , response , body ) {
305+ request ( { method : "POST" , uri : global . rasa_endpoint + "/conversations/" + req . body . conversation_id + "/tracker/events" , body : body } , function ( err , response , body ) {
288306 if ( err ) {
289307 logger . winston . error ( err ) ;
290308 sendOutput ( 500 , res , '{"error" : "Exception caught !!"}' ) ;
291309 return ;
292310 }
293311 logger . winston . verbose ( "Restart Response" + JSON . stringify ( body ) ) ;
294- //Update conversation table with updated conversation response from rasa ...
295- db . run ( 'update conversations set conversation = ?, story = ? where conversation_id = ?' , [ body , '' , req . body . conversation_id ] , function ( err ) {
296- if ( err ) {
297- logger . winston . error ( "Error updating the record" ) ;
298- } else {
299- //Predict the next action?
300- sendOutput ( 200 , res , body ) ;
301- }
302- } ) ;
312+ updateConversation ( req . body . conversation_id , body ) ;
313+ sendOutput ( 200 , res , body ) ;
303314 } ) ;
304315 } catch ( err ) {
305316 logger . winston . error ( err ) ;
0 commit comments