clm-kernel 0.0.16

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! RuntimeDispatcher extracted from `runtime_adapter.rs` (Sprint 534 ratchet).

use super::*;

/// Dispatcher that maps runtime names to adapters.
pub struct RuntimeDispatcher {
    adapters: HashMap<String, Box<dyn RuntimeAdapter>>,
}

impl RuntimeDispatcher {
    /// Create a new empty dispatcher.
    pub fn new() -> Self {
        Self {
            adapters: HashMap::new(),
        }
    }

    /// Create a dispatcher pre-populated with all available adapters for the
    /// current target.
    ///
    /// Populates built-in adapters (echo, noop, math) and native subprocess adapters.
    pub fn with_defaults() -> Self {
        let mut d = Self::new();

        // Built-in handlers (always available).
        d.register("echo", Box::new(BuiltinRuntimeAdapter::Echo));
        d.register("noop", Box::new(BuiltinRuntimeAdapter::Noop));
        d.register("math", Box::new(BuiltinRuntimeAdapter::Math));

        // Native-only adapters.
        #[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
    }

    /// Register an adapter under a runtime name.
    pub fn register(&mut self, runtime: &str, adapter: Box<dyn RuntimeAdapter>) {
        self.adapters.insert(runtime.to_string(), adapter);
    }

    /// Dispatch execution to the named runtime.
    ///
    /// Returns an error if the runtime is not registered.
    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)
    }

    /// List all registered runtime names.
    pub fn list_runtimes(&self) -> Vec<&str> {
        self.adapters.keys().map(|s| s.as_str()).collect()
    }

    /// Check if a runtime is registered.
    pub fn has(&self, runtime: &str) -> bool {
        self.adapters.contains_key(runtime)
    }
}

impl Default for RuntimeDispatcher {
    fn default() -> Self {
        Self::with_defaults()
    }
}