2025-10-12 20:12:49 -03:00
|
|
|
use crate::shared::models::UserSession;
|
|
|
|
|
use crate::shared::state::AppState;
|
|
|
|
|
use log::info;
|
|
|
|
|
use rhai::{Dynamic, Engine, EvalAltResult};
|
|
|
|
|
use std::sync::Arc;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-12 20:12:49 -03:00
|
|
|
pub mod keywords;
|
2025-10-07 10:53:09 -03:00
|
|
|
|
2025-10-06 10:30:17 -03:00
|
|
|
use self::keywords::create_site::create_site_keyword;
|
|
|
|
|
use self::keywords::find::find_keyword;
|
|
|
|
|
use self::keywords::first::first_keyword;
|
|
|
|
|
use self::keywords::for_next::for_keyword;
|
|
|
|
|
use self::keywords::format::format_keyword;
|
|
|
|
|
use self::keywords::get::get_keyword;
|
2025-10-11 12:29:03 -03:00
|
|
|
use self::keywords::hear_talk::{hear_keyword, set_context_keyword, talk_keyword};
|
2025-10-06 10:30:17 -03:00
|
|
|
use self::keywords::last::last_keyword;
|
|
|
|
|
use self::keywords::llm_keyword::llm_keyword;
|
|
|
|
|
use self::keywords::on::on_keyword;
|
|
|
|
|
use self::keywords::print::print_keyword;
|
|
|
|
|
use self::keywords::set::set_keyword;
|
|
|
|
|
use self::keywords::set_schedule::set_schedule_keyword;
|
|
|
|
|
use self::keywords::wait::wait_keyword;
|
2025-10-12 20:12:49 -03:00
|
|
|
|
|
|
|
|
#[cfg(feature = "email")]
|
|
|
|
|
use self::keywords::create_draft_keyword;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "web_automation")]
|
|
|
|
|
use self::keywords::get_website::get_website_keyword;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
pub struct ScriptService {
|
|
|
|
|
engine: Engine,
|
2025-10-12 20:12:49 -03:00
|
|
|
state: Arc<AppState>,
|
|
|
|
|
user: UserSession,
|
2025-10-06 10:30:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScriptService {
|
2025-10-12 20:12:49 -03:00
|
|
|
pub fn new(state: Arc<AppState>, user: UserSession) -> Self {
|
2025-10-06 10:30:17 -03:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
|
|
engine.set_allow_anonymous_fn(true);
|
|
|
|
|
engine.set_allow_looping(true);
|
|
|
|
|
|
2025-10-07 10:53:09 -03:00
|
|
|
#[cfg(feature = "email")]
|
2025-10-12 20:12:49 -03:00
|
|
|
create_draft_keyword(&state, user.clone(), &mut engine);
|
2025-10-07 10:53:09 -03:00
|
|
|
|
2025-10-12 20:12:49 -03:00
|
|
|
create_site_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
find_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
for_keyword(&state, user.clone(), &mut engine);
|
2025-10-06 10:30:17 -03:00
|
|
|
first_keyword(&mut engine);
|
|
|
|
|
last_keyword(&mut engine);
|
|
|
|
|
format_keyword(&mut engine);
|
2025-10-12 20:12:49 -03:00
|
|
|
llm_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
get_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
set_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
wait_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
print_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
on_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
set_schedule_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
hear_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
talk_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
set_context_keyword(&state, user.clone(), &mut engine);
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "web_automation")]
|
|
|
|
|
get_website_keyword(&state, user.clone(), &mut engine);
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-12 20:12:49 -03:00
|
|
|
ScriptService {
|
|
|
|
|
engine,
|
|
|
|
|
state,
|
|
|
|
|
user,
|
|
|
|
|
}
|
2025-10-06 10:30:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn preprocess_basic_script(&self, script: &str) -> String {
|
|
|
|
|
let mut result = String::new();
|
|
|
|
|
let mut for_stack: Vec<usize> = Vec::new();
|
|
|
|
|
let mut current_indent = 0;
|
|
|
|
|
|
|
|
|
|
for line in script.lines() {
|
|
|
|
|
let trimmed = line.trim();
|
|
|
|
|
|
|
|
|
|
if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with("REM") {
|
|
|
|
|
result.push_str(line);
|
|
|
|
|
result.push('\n');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if trimmed.starts_with("FOR EACH") {
|
|
|
|
|
for_stack.push(current_indent);
|
|
|
|
|
result.push_str(&" ".repeat(current_indent));
|
|
|
|
|
result.push_str(trimmed);
|
|
|
|
|
result.push_str("{\n");
|
|
|
|
|
current_indent += 4;
|
|
|
|
|
result.push_str(&" ".repeat(current_indent));
|
|
|
|
|
result.push('\n');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if trimmed.starts_with("NEXT") {
|
|
|
|
|
if let Some(expected_indent) = for_stack.pop() {
|
|
|
|
|
if (current_indent - 4) != expected_indent {
|
|
|
|
|
panic!("NEXT without matching FOR EACH");
|
|
|
|
|
}
|
|
|
|
|
current_indent = current_indent - 4;
|
|
|
|
|
result.push_str(&" ".repeat(current_indent));
|
|
|
|
|
result.push_str("}\n");
|
|
|
|
|
result.push_str(&" ".repeat(current_indent));
|
|
|
|
|
result.push_str(trimmed);
|
|
|
|
|
result.push(';');
|
|
|
|
|
result.push('\n');
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
panic!("NEXT without matching FOR EACH");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if trimmed == "EXIT FOR" {
|
|
|
|
|
result.push_str(&" ".repeat(current_indent));
|
|
|
|
|
result.push_str(trimmed);
|
|
|
|
|
result.push('\n');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.push_str(&" ".repeat(current_indent));
|
|
|
|
|
|
|
|
|
|
let basic_commands = [
|
2025-10-11 12:29:03 -03:00
|
|
|
"SET",
|
|
|
|
|
"CREATE",
|
|
|
|
|
"PRINT",
|
|
|
|
|
"FOR",
|
|
|
|
|
"FIND",
|
|
|
|
|
"GET",
|
|
|
|
|
"EXIT",
|
|
|
|
|
"IF",
|
|
|
|
|
"THEN",
|
|
|
|
|
"ELSE",
|
|
|
|
|
"END IF",
|
|
|
|
|
"WHILE",
|
|
|
|
|
"WEND",
|
|
|
|
|
"DO",
|
|
|
|
|
"LOOP",
|
|
|
|
|
"HEAR",
|
|
|
|
|
"TALK",
|
|
|
|
|
"SET CONTEXT",
|
2025-10-06 10:30:17 -03:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let is_basic_command = basic_commands.iter().any(|&cmd| trimmed.starts_with(cmd));
|
|
|
|
|
let is_control_flow = trimmed.starts_with("IF")
|
|
|
|
|
|| trimmed.starts_with("ELSE")
|
|
|
|
|
|| trimmed.starts_with("END IF");
|
|
|
|
|
|
|
|
|
|
if is_basic_command || !for_stack.is_empty() || is_control_flow {
|
|
|
|
|
result.push_str(trimmed);
|
|
|
|
|
result.push(';');
|
|
|
|
|
} else {
|
|
|
|
|
result.push_str(trimmed);
|
|
|
|
|
if !trimmed.ends_with(';') && !trimmed.ends_with('{') && !trimmed.ends_with('}') {
|
|
|
|
|
result.push(';');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.push('\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !for_stack.is_empty() {
|
|
|
|
|
panic!("Unclosed FOR EACH loop");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn compile(&self, script: &str) -> Result<rhai::AST, Box<EvalAltResult>> {
|
|
|
|
|
let processed_script = self.preprocess_basic_script(script);
|
|
|
|
|
info!("Processed Script:\n{}", processed_script);
|
|
|
|
|
match self.engine.compile(&processed_script) {
|
|
|
|
|
Ok(ast) => Ok(ast),
|
2025-10-11 20:02:14 -03:00
|
|
|
Err(parse_error) => Err(Box::new(parse_error.into())),
|
2025-10-06 10:30:17 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run(&self, ast: &rhai::AST) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
|
|
|
self.engine.eval_ast(ast)
|
|
|
|
|
}
|
|
|
|
|
}
|