use super::*;
pub struct RuntimeDispatcher {
adapters: HashMap<String, Box<dyn RuntimeAdapter>>,
}
impl RuntimeDispatcher {
pub fn new() -> Self {
Self {
adapters: HashMap::new(),
}
}
pub fn with_defaults() -> Self {
let mut d = Self::new();
d.register("echo", Box::new(BuiltinRuntimeAdapter::Echo));
d.register("noop", Box::new(BuiltinRuntimeAdapter::Noop));
d.register("math", Box::new(BuiltinRuntimeAdapter::Math));
#[cfg(feature = "native")]
{
d.register("shell", Box::new(ShellRuntimeAdapter));
d.register("python", Box::new(PythonRuntimeAdapter));
d.register("node", Box::new(NodeRuntimeAdapter));
d.register("llm", Box::new(LlmRuntimeAdapter::new()));
}
d
}
pub fn register(&mut self, runtime: &str, adapter: Box<dyn RuntimeAdapter>) {
self.adapters.insert(runtime.to_string(), adapter);
}
pub fn dispatch(&self, runtime: &str, script: &str, input: &Value) -> Result<Value, String> {
let adapter = self
.adapters
.get(runtime)
.ok_or_else(|| format!("unknown runtime: {} (registered: {:?})", runtime, self.list_runtimes()))?;
adapter.execute(script, input)
}
pub fn list_runtimes(&self) -> Vec<&str> {
self.adapters.keys().map(|s| s.as_str()).collect()
}
pub fn has(&self, runtime: &str) -> bool {
self.adapters.contains_key(runtime)
}
}
impl Default for RuntimeDispatcher {
fn default() -> Self {
Self::with_defaults()
}
}