actor trait improvements and ui implementation

This commit is contained in:
Timur Gordon
2025-08-06 12:48:32 +02:00
parent 9f9149a950
commit dcf0f41bb8
35 changed files with 6582 additions and 15 deletions

View File

@@ -113,6 +113,32 @@ async fn execute_script_and_update_status(
}
}
/// Execute a job with the given engine, setting proper job context
///
/// This function sets up the engine with job context (DB_PATH, CALLER_ID, CONTEXT_ID)
/// and evaluates the script. It returns the result or error without updating Redis.
/// This allows actors to handle Redis updates according to their own patterns.
pub async fn execute_job_with_engine(
engine: &Engine,
job: &Job,
db_path: &str,
) -> Result<Dynamic, Box<rhai::EvalAltResult>> {
// Clone the engine to avoid mutating the original
let mut engine_clone = engine.clone();
// Set up job context in the engine
let mut db_config = rhai::Map::new();
db_config.insert("DB_PATH".into(), db_path.to_string().into());
db_config.insert("CALLER_ID".into(), job.caller_id.clone().into());
db_config.insert("CONTEXT_ID".into(), job.context_id.clone().into());
engine_clone.set_default_tag(Dynamic::from(db_config));
debug!("Actor for Context ID '{}': Evaluating script with Rhai engine (job context set).", job.context_id);
// Execute the script with the configured engine
engine_clone.eval::<Dynamic>(&job.script)
}
/// Clean up job from Redis if preserve_tasks is false
async fn cleanup_job(
redis_conn: &mut redis::aio::MultiplexedConnection,
@@ -236,3 +262,6 @@ pub fn spawn_rhai_actor(
// Re-export the main trait-based interface for convenience
pub use actor_trait::{Actor, ActorConfig, spawn_actor};
// Re-export the shared job execution function
pub use execute_job_with_engine;

38
core/actor/src/main.rs Normal file
View File

@@ -0,0 +1,38 @@
#[cfg(feature = "wasm")]
use baobab_actor::ui::App;
#[cfg(feature = "wasm")]
use yew::prelude::*;
#[cfg(feature = "wasm")]
fn main() {
console_log::init_with_level(log::Level::Debug).expect("Failed to initialize logger");
// Get configuration from URL parameters or local storage
let window = web_sys::window().expect("No global window exists");
let location = window.location();
let search = location.search().unwrap_or_default();
// Parse URL parameters for actor configuration
let url_params = web_sys::UrlSearchParams::new_with_str(&search).unwrap();
let actor_id = url_params.get("id").unwrap_or_else(|| "default_actor".to_string());
let actor_path = url_params.get("path").unwrap_or_else(|| "/path/to/actor".to_string());
let example_dir = url_params.get("example_dir");
let redis_url = url_params.get("redis_url").unwrap_or_else(|| "redis://localhost:6379".to_string());
log::info!("Starting Baobab Actor UI with actor_id: {}", actor_id);
yew::Renderer::<App>::with_props(baobab_actor::ui::app::AppProps {
actor_id,
actor_path,
example_dir,
redis_url,
}).render();
}
#[cfg(not(feature = "wasm"))]
fn main() {
eprintln!("This binary is only available with the 'wasm' feature enabled.");
eprintln!("Please compile with: cargo build --features wasm --target wasm32-unknown-unknown");
std::process::exit(1);
}