aether_core/runtime.rs
1//! # Aether Runtime
2//!
3//! Executive environment for dynamic AI code execution.
4//! This module uses the Rhai script engine to execute code generated by AI at runtime.
5
6use crate::{Result, AetherError};
7use rhai::{Engine, Dynamic, Scope};
8use std::collections::HashMap;
9
10/// A runtime environment capable of executing AI-generated scripts in isolation.
11pub struct AetherRuntime {
12 engine: Engine,
13}
14
15impl AetherRuntime {
16 /// Create a new Aether runtime.
17 pub fn new() -> Self {
18 let mut engine = Engine::new();
19 // Disable potentially dangerous operations for security
20 engine.set_max_operations(1000);
21
22 Self { engine }
23 }
24
25 /// Execute AI-generated code as a Rhai script.
26 ///
27 /// # Arguments
28 ///
29 /// * `script` - The raw script generated by AI.
30 /// * `inputs` - A map of input variables to pass to the script.
31 pub fn execute(&self, script: &str, inputs: HashMap<String, Dynamic>) -> Result<Dynamic> {
32 let mut scope = Scope::new();
33
34 // Push inputs into scope
35 for (name, val) in inputs {
36 scope.push(name, val);
37 }
38
39 // Execute script
40 self.engine.eval_with_scope(&mut scope, script)
41 .map_err(|e| AetherError::ConfigError(format!("Runtime execution failed: {}", e)))
42 }
43}
44
45impl Default for AetherRuntime {
46 fn default() -> Self {
47 Self::new()
48 }
49}