fix: use compile_preprocessed for .ast files
All checks were successful
BotServer CI/CD / build (push) Successful in 3m29s

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-12 17:48:41 -03:00
parent af85426ed4
commit 20af25e9e2
2 changed files with 123 additions and 101 deletions

View file

@ -580,6 +580,15 @@ impl ScriptService {
}
}
/// Compile preprocessed script content (from .ast file) - skips preprocessing
pub fn compile_preprocessed(&self, script: &str) -> Result<rhai::AST, Box<EvalAltResult>> {
trace!("Compiling preprocessed script directly");
match self.engine.compile(script) {
Ok(ast) => Ok(ast),
Err(parse_error) => Err(Box::new(parse_error.into())),
}
}
/// Compile a tool script (.bas file with PARAM/DESCRIPTION metadata lines)
/// Filters out tool metadata before compiling
pub fn compile_tool_script(&self, script: &str) -> Result<rhai::AST, Box<EvalAltResult>> {

View file

@ -625,12 +625,12 @@ impl BotOrchestrator {
trace!("Executing start.bas for session {} at: {}", actual_session_id, start_script_path);
// Use pre-compiled .ast if available (avoids recompilation)
// Use pre-compiled .ast if available (avoids preprocessing)
let ast_path = start_script_path.replace(".bas", ".ast");
let script_content = if std::path::Path::new(&ast_path).exists() {
tokio::fs::read_to_string(&ast_path).await.unwrap_or_default()
let (script_content, is_preprocessed) = if std::path::Path::new(&ast_path).exists() {
(tokio::fs::read_to_string(&ast_path).await.unwrap_or_default(), true)
} else {
tokio::fs::read_to_string(&start_script_path).await.unwrap_or_default()
(tokio::fs::read_to_string(&start_script_path).await.unwrap_or_default(), false)
};
if !script_content.is_empty() {
@ -659,7 +659,13 @@ impl BotOrchestrator {
);
script_service.load_bot_config_params(&state_clone, bot_id_clone);
match script_service.compile(&script_content) {
let compile_result = if is_preprocessed {
script_service.compile_preprocessed(&script_content)
} else {
script_service.compile(&script_content)
};
match compile_result {
Ok(ast) => match script_service.run(&ast) {
Ok(_) => Ok(()),
Err(e) => Err(format!("Script execution error: {}", e)),
@ -1408,9 +1414,9 @@ async fn handle_websocket(
info!("Looking for start.bas at: {}", start_script_path);
// Check for pre-compiled .ast file first (avoids recompilation overhead)
// Check for pre-compiled .ast file first (avoids preprocessing overhead)
let ast_path = start_script_path.replace(".bas", ".ast");
let (script_content, _using_ast) = if tokio::fs::metadata(&ast_path).await.is_ok() {
let (script_content, is_preprocessed) = if tokio::fs::metadata(&ast_path).await.is_ok() {
if let Ok(content) = tokio::fs::read_to_string(&ast_path).await {
info!("Using pre-compiled start.ast for {}", bot_name);
(content, true)
@ -1440,6 +1446,7 @@ async fn handle_websocket(
let session_id_str = session_id.to_string();
let mut send_ready_rx = send_ready_rx;
let script_content_owned = script_content.clone();
let is_preprocessed_owned = is_preprocessed;
tokio::spawn(async move {
let _ = send_ready_rx.recv().await;
@ -1476,7 +1483,13 @@ async fn handle_websocket(
);
script_service.load_bot_config_params(&state_for_start, bot_id);
match script_service.compile(&script_content_owned) {
let compile_result = if is_preprocessed_owned {
script_service.compile_preprocessed(&script_content_owned)
} else {
script_service.compile(&script_content_owned)
};
match compile_result {
Ok(ast) => match script_service.run(&ast) {
Ok(_) => Ok(()),
Err(e) => Err(format!("Script execution error: {}", e)),