Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set variable step limit #427

Merged
merged 3 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions csml_cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn init_request(string: &str, metadata: Option<serde_json::Value>) -> CsmlRe
None => json!({}),
},
ttl_duration: None,
step_limit: None,
low_data_mode: None,
}
}
Expand All @@ -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,
}
}
Expand Down
1 change: 1 addition & 0 deletions csml_engine/examples/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
1 change: 1 addition & 0 deletions csml_engine/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ pub struct CsmlRequest {
pub callback_url: Option<String>,
pub payload: serde_json::Value,
pub metadata: serde_json::Value,
pub step_limit: Option<usize>,
pub ttl_duration: Option<serde_json::Value>,
pub low_data_mode: Option<serde_json::Value>,
}
Expand Down
3 changes: 1 addition & 2 deletions csml_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

/**
Expand All @@ -65,7 +64,7 @@ pub fn start_conversation(
) -> Result<serde_json::Map<String, serde_json::Value>, 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)?;
Expand Down
8 changes: 6 additions & 2 deletions csml_engine/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -122,7 +122,10 @@ pub fn get_event_content(content_type: &str, metadata: &Value) -> Result<String,
/**
* Format the incoming (JSON-formatted) event into an Event struct.
*/
pub fn format_event(json_event: serde_json::Value) -> Result<Event, EngineError> {
pub fn format_event(request: &CsmlRequest) -> Result<Event, EngineError> {
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 => {
Expand All @@ -141,6 +144,7 @@ pub fn format_event(json_event: serde_json::Value) -> Result<Event, EngineError>
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),
})
}
Expand Down
1 change: 1 addition & 0 deletions csml_engine/tests/test_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
1 change: 1 addition & 0 deletions csml_interpreter/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn main() {
content: serde_json::json!({"payload":"4"}),
ttl_duration: None,
low_data_mode: None,
step_limit: None,
secure: false,
};

Expand Down
1 change: 1 addition & 0 deletions csml_interpreter/examples/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn main() {
content: serde_json::json!({"payload":"4"}),
ttl_duration: None,
low_data_mode: None,
step_limit: None,
secure: false,
};

Expand Down
2 changes: 1 addition & 1 deletion csml_interpreter/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 11 additions & 5 deletions csml_interpreter/src/data/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub struct Data<'a> {
pub loop_indexes: Vec<usize>,
pub loop_index: usize,

pub step_count: &'a mut i32,
pub step_count: &'a mut usize,
pub step_limit: usize,

pub step_vars: HashMap<String, Literal>,
pub previous_info: Option<PreviousInfo>,
Expand Down Expand Up @@ -70,7 +71,8 @@ impl<'a> Data<'a> {
env: &'a Literal,
loop_indexes: Vec<usize>,
loop_index: usize,
step_count: &'a mut i32,
step_count: &'a mut usize,
step_limit: usize,
step_vars: HashMap<String, Literal>,
previous_info: Option<PreviousInfo>,
custom_component: &'a serde_json::Map<String, serde_json::Value>,
Expand All @@ -90,6 +92,7 @@ impl<'a> Data<'a> {
loop_indexes,
loop_index,
step_count,
step_limit,
step_vars,
previous_info,
custom_component,
Expand All @@ -109,7 +112,8 @@ impl<'a> Data<'a> {
Literal,
Vec<usize>,
usize,
i32,
usize,
usize,
HashMap<String, Literal>,
serde_json::Map<String, serde_json::Value>,
serde_json::Map<String, serde_json::Value>,
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions csml_interpreter/src/data/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub struct Event {
pub content: serde_json::Value,
pub ttl_duration: Option<i64>,
pub low_data_mode: Option<bool>,
pub secure: bool
pub step_limit: Option<usize>,
pub secure: bool,
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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,
}
}
}
Expand All @@ -41,7 +43,8 @@ impl Event {
content,
ttl_duration: None,
low_data_mode: None,
secure: false
step_limit: None,
secure: false,
}
}
}
2 changes: 2 additions & 0 deletions csml_interpreter/src/interpreter/ast_interpreter/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions csml_interpreter/src/interpreter/variable_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion csml_interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn execute_step(
sender: &Option<mpsc::Sender<MSG>>,
) -> 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),
Expand Down Expand Up @@ -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::<usize>().unwrap_or(STEP_LIMIT),
_ => STEP_LIMIT,
}
}

fn get_flow_ast<'a, 'b>(
flows: &'a HashMap<String, Flow>,
flow: &'b str,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -499,6 +508,7 @@ pub fn interpret(
vec![],
0,
&mut step_count,
step_limit,
step_vars,
previous_info.clone(),
&custom,
Expand Down