diff --git a/csml_cli/src/run.rs b/csml_cli/src/run.rs index feb448fd..da8e02b8 100644 --- a/csml_cli/src/run.rs +++ b/csml_cli/src/run.rs @@ -29,6 +29,7 @@ pub fn init_request(string: &str, metadata: Option) -> CsmlRe None => json!({}), }, ttl_duration: None, + step_limit: None, low_data_mode: None, } } @@ -51,6 +52,7 @@ pub fn init_request_flow_trigger(flow_id: &str, step_id: Option<&str>) -> CsmlRe }), metadata: json!({}), ttl_duration: None, + step_limit: None, low_data_mode: None, } } diff --git a/csml_engine/examples/command_line.rs b/csml_engine/examples/command_line.rs index 91cb56fb..6b267092 100644 --- a/csml_engine/examples/command_line.rs +++ b/csml_engine/examples/command_line.rs @@ -35,6 +35,7 @@ fn init_request(string: &str) -> CsmlRequest { }), metadata: json!({"some": "custom-value"}), ttl_duration: None, + step_limit: None, low_data_mode: None, } } diff --git a/csml_engine/src/data.rs b/csml_engine/src/data.rs index 7b126600..7d452029 100644 --- a/csml_engine/src/data.rs +++ b/csml_engine/src/data.rs @@ -343,6 +343,7 @@ pub struct CsmlRequest { pub callback_url: Option, pub payload: serde_json::Value, pub metadata: serde_json::Value, + pub step_limit: Option, pub ttl_duration: Option, pub low_data_mode: Option, } diff --git a/csml_engine/src/lib.rs b/csml_engine/src/lib.rs index 6f9ca758..7830b577 100644 --- a/csml_engine/src/lib.rs +++ b/csml_engine/src/lib.rs @@ -43,7 +43,6 @@ use chrono::prelude::*; use csml_interpreter::data::{ csml_bot::CsmlBot, csml_flow::CsmlFlow, Context, Hold, IndexInfo, Memory, }; -use serde_json::json; use std::{collections::HashMap, env}; /** @@ -65,7 +64,7 @@ pub fn start_conversation( ) -> Result, EngineError> { init_logger(); - let mut formatted_event = format_event(json!(request))?; + let mut formatted_event = format_event(&request)?; let mut db = init_db()?; let mut bot = bot_opt.search_bot(&mut db)?; diff --git a/csml_engine/src/utils.rs b/csml_engine/src/utils.rs index 92ed3d5e..f9778364 100644 --- a/csml_engine/src/utils.rs +++ b/csml_engine/src/utils.rs @@ -1,5 +1,5 @@ use crate::{ - data::{ConversationInfo, Database, EngineError, FlowTrigger}, + data::{ConversationInfo, CsmlRequest, Database, EngineError, FlowTrigger}, db_connectors::state::delete_state_key, send::send_to_callback_url, CsmlBot, CsmlFlow, @@ -122,7 +122,10 @@ pub fn get_event_content(content_type: &str, metadata: &Value) -> Result Result { +pub fn format_event(request: &CsmlRequest) -> Result { + let step_limit = request.step_limit; + let json_event = json!(request); + let content_type = match json_event["payload"]["content_type"].as_str() { Some(content_type) => content_type.to_string(), None => { @@ -141,6 +144,7 @@ pub fn format_event(json_event: serde_json::Value) -> Result content, ttl_duration: json_event["ttl_duration"].as_i64(), low_data_mode: json_event["low_data_mode"].as_bool(), + step_limit, secure: json_event["payload"]["secure"].as_bool().unwrap_or(false), }) } diff --git a/csml_engine/tests/test_bot.rs b/csml_engine/tests/test_bot.rs index b7a6c092..53d2baf9 100644 --- a/csml_engine/tests/test_bot.rs +++ b/csml_engine/tests/test_bot.rs @@ -109,6 +109,7 @@ fn init_request(string: &str, bot_id: String, channel_id: String) -> CsmlRequest }), metadata: json!({"some": "custom-value"}), ttl_duration: None, + step_limit: None, low_data_mode: None, } } diff --git a/csml_interpreter/examples/hello_world.rs b/csml_interpreter/examples/hello_world.rs index 9217768e..ac2793f0 100644 --- a/csml_interpreter/examples/hello_world.rs +++ b/csml_interpreter/examples/hello_world.rs @@ -45,6 +45,7 @@ fn main() { content: serde_json::json!({"payload":"4"}), ttl_duration: None, low_data_mode: None, + step_limit: None, secure: false, }; diff --git a/csml_interpreter/examples/modules.rs b/csml_interpreter/examples/modules.rs index b67c91dc..21f035f3 100644 --- a/csml_interpreter/examples/modules.rs +++ b/csml_interpreter/examples/modules.rs @@ -51,6 +51,7 @@ fn main() { content: serde_json::json!({"payload":"4"}), ttl_duration: None, low_data_mode: None, + step_limit: None, secure: false, }; diff --git a/csml_interpreter/src/data.rs b/csml_interpreter/src/data.rs index 74a11ba4..09535184 100644 --- a/csml_interpreter/src/data.rs +++ b/csml_interpreter/src/data.rs @@ -39,4 +39,4 @@ pub use position::Position; pub use msg::MSG; // limit of steps in a single execution -pub static STEP_LIMIT: i32 = 100; +pub static STEP_LIMIT: usize = 100; diff --git a/csml_interpreter/src/data/data.rs b/csml_interpreter/src/data/data.rs index 0a4419e5..482e5c24 100644 --- a/csml_interpreter/src/data/data.rs +++ b/csml_interpreter/src/data/data.rs @@ -30,7 +30,8 @@ pub struct Data<'a> { pub loop_indexes: Vec, pub loop_index: usize, - pub step_count: &'a mut i32, + pub step_count: &'a mut usize, + pub step_limit: usize, pub step_vars: HashMap, pub previous_info: Option, @@ -70,7 +71,8 @@ impl<'a> Data<'a> { env: &'a Literal, loop_indexes: Vec, loop_index: usize, - step_count: &'a mut i32, + step_count: &'a mut usize, + step_limit: usize, step_vars: HashMap, previous_info: Option, custom_component: &'a serde_json::Map, @@ -90,6 +92,7 @@ impl<'a> Data<'a> { loop_indexes, loop_index, step_count, + step_limit, step_vars, previous_info, custom_component, @@ -109,7 +112,8 @@ impl<'a> Data<'a> { Literal, Vec, usize, - i32, + usize, + usize, HashMap, serde_json::Map, serde_json::Map, @@ -124,7 +128,8 @@ impl<'a> Data<'a> { self.env.clone(), self.loop_indexes.clone(), self.loop_index.clone(), - self.step_count.clone(), + *self.step_count, + self.step_limit, self.step_vars.clone(), self.custom_component.clone(), self.native_component.clone(), @@ -155,7 +160,7 @@ pub fn init_child_context(data: &Data) -> Context { pub fn init_child_scope<'a>( data: &'a Data, context: &'a mut Context, - step_count: &'a mut i32, + step_count: &'a mut usize, ) -> Data<'a> { Data::new( &data.flows, @@ -168,6 +173,7 @@ pub fn init_child_scope<'a>( data.loop_indexes.clone(), data.loop_index, step_count, + data.step_limit, HashMap::new(), data.previous_info.clone(), &data.custom_component, diff --git a/csml_interpreter/src/data/event.rs b/csml_interpreter/src/data/event.rs index c319ce7f..84b172fe 100644 --- a/csml_interpreter/src/data/event.rs +++ b/csml_interpreter/src/data/event.rs @@ -9,7 +9,8 @@ pub struct Event { pub content: serde_json::Value, pub ttl_duration: Option, pub low_data_mode: Option, - pub secure: bool + pub step_limit: Option, + pub secure: bool, } //////////////////////////////////////////////////////////////////////////////// @@ -24,7 +25,8 @@ impl Default for Event { content: serde_json::json!({}), ttl_duration: None, low_data_mode: None, - secure: false + step_limit: None, + secure: false, } } } @@ -41,7 +43,8 @@ impl Event { content, ttl_duration: None, low_data_mode: None, - secure: false + step_limit: None, + secure: false, } } } diff --git a/csml_interpreter/src/interpreter/ast_interpreter/actions.rs b/csml_interpreter/src/interpreter/ast_interpreter/actions.rs index b592743d..ca9a19aa 100644 --- a/csml_interpreter/src/interpreter/ast_interpreter/actions.rs +++ b/csml_interpreter/src/interpreter/ast_interpreter/actions.rs @@ -177,6 +177,7 @@ pub fn match_actions( tmp_loop_indexes, tmp_loop_index, mut tmp_step_count, + tmp_step_limit, tmp_step_vars, tmp_custom_component, tmp_native_component, @@ -193,6 +194,7 @@ pub fn match_actions( tmp_loop_indexes, tmp_loop_index, &mut tmp_step_count, + tmp_step_limit, tmp_step_vars, data.previous_info.clone(), &tmp_custom_component, diff --git a/csml_interpreter/src/interpreter/variable_handler.rs b/csml_interpreter/src/interpreter/variable_handler.rs index b1a8196c..0121894e 100644 --- a/csml_interpreter/src/interpreter/variable_handler.rs +++ b/csml_interpreter/src/interpreter/variable_handler.rs @@ -679,6 +679,7 @@ pub fn get_var( tmp_loop_indexes, tmp_loop_index, mut tmp_step_count, + tmp_step_limit, tmp_step_vars, tmp_custom_component, tmp_native_component, @@ -695,6 +696,7 @@ pub fn get_var( tmp_loop_indexes, tmp_loop_index, &mut tmp_step_count, + tmp_step_limit, tmp_step_vars, data.previous_info.clone(), &tmp_custom_component, diff --git a/csml_interpreter/src/lib.rs b/csml_interpreter/src/lib.rs index 2b724b25..6b450c99 100644 --- a/csml_interpreter/src/lib.rs +++ b/csml_interpreter/src/lib.rs @@ -42,7 +42,7 @@ fn execute_step( sender: &Option>, ) -> MessageData { // stop execution if step_count >= STEP_LIMIT in order to avoid infinite loops - if *data.step_count >= STEP_LIMIT { + if *data.step_count >= data.step_limit { let msg_data = Err(gen_error_info( Position::new( Interval::new_as_u32(0, 0, 0, None, None), @@ -96,6 +96,14 @@ fn execute_step( MessageData::error_to_message(msg_data, sender) } +fn get_step_limit(event: &Event) -> usize { + match (event.step_limit, env::var("STEP_LIMIT").ok()) { + (Some(step_limit), _) => step_limit, + (None, Some(step_limit)) => step_limit.parse::().unwrap_or(STEP_LIMIT), + _ => STEP_LIMIT, + } +} + fn get_flow_ast<'a, 'b>( flows: &'a HashMap, flow: &'b str, @@ -443,6 +451,7 @@ pub fn interpret( let mut step = context.step.to_owned(); let mut step_count = 0; + let step_limit = get_step_limit(&event); let mut step_vars = match &context.hold { Some(hold) => get_hashmap_from_mem(&hold.step_vars, &flow), @@ -499,6 +508,7 @@ pub fn interpret( vec![], 0, &mut step_count, + step_limit, step_vars, previous_info.clone(), &custom,