commandagi 0.6.10

The CommandAGI SDK for Rust: the platform client (agent threads, live sessions on a simulated world) and CommandAGI documents (a schematic, a 3D part …) declared as code. No dependencies.
Documentation
//! HOW THE SANDBOX RUNS A FILE — the WebAssembly interface of a compiled `.rs` document.
//!
//! The local host compiles the file into a `cdylib` for `wasm32-unknown-unknown` with this wrapper around it:
//!
//! ```text
//! include!("user.rs");
//! #[no_mangle] pub extern "C" fn commandagi_run() -> u32 { ::commandagi::abi::run(document) }
//! #[no_mangle] pub extern "C" fn commandagi_out_ptr() -> u32 { ::commandagi::abi::out_ptr() }
//! #[no_mangle] pub extern "C" fn commandagi_out_len() -> u32 { ::commandagi::abi::out_len() }
//! #[no_mangle] pub extern "C" fn commandagi_stem_alloc(len: u32) -> u32 { ::commandagi::abi::stem_alloc(len) }
//! #[no_mangle] pub extern "C" fn commandagi_input_alloc(len: u32) -> u32 { ::commandagi::abi::input_alloc(len) }
//! ```
//!
//! Before the run, the sandbox may write the file's stem (UTF-8) where `commandagi_stem_alloc` says: a document that
//! names itself nothing is named after it. It may also write the run's inputs (a JSON object of values by name)
//! where `commandagi_input_alloc` says: a `param(…)` of the file takes its value there ([`design::params`]).
//!
//! The module has no imports. `commandagi_run` returns 1 and leaves the answer as UTF-8 at `commandagi_out_ptr`
//! (`commandagi_out_len` bytes): `{"ok":true,"graph","params","sites"}`, and for a document that is not a graph also
//! `"document": {"format","document","sources"}` beside the empty graph `{"id":"Document","nodes":{}}`. Or it returns
//! 0 and leaves `{"ok":false,"error"}`. A panic traps (a WebAssembly module cannot unwind); its hook leaves the error
//! first, so the sandbox reads it after the trap.

use crate::design::{declare, params, source, Declared, El, Json};
use std::cell::RefCell;

thread_local! {
    static OUT: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
    static STEM_IN: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
    static INPUT_IN: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}

/// Room for the run's inputs, `len` bytes of JSON: where the sandbox writes them before `run`.
pub fn input_alloc(len: u32) -> u32 {
    INPUT_IN.with(|b| {
        let mut b = b.borrow_mut();
        *b = vec![0; len as usize];
        b.as_mut_ptr() as usize as u32
    })
}

/// Room for the file's stem, `len` bytes of UTF-8: where the sandbox writes it before `run`.
pub fn stem_alloc(len: u32) -> u32 {
    STEM_IN.with(|b| {
        let mut b = b.borrow_mut();
        *b = vec![0; len as usize];
        b.as_mut_ptr() as usize as u32
    })
}

fn set_out(text: String) {
    OUT.with(|o| *o.borrow_mut() = text.into_bytes());
}

fn failed(error: String) -> String {
    Json::obj().with("ok", false).with("error", error).text()
}

/// The answer to a declaration: what `run` leaves, as JSON.
pub fn answer(declared: Result<Declared, String>) -> Result<Json, String> {
    let out = Json::obj().with("ok", true);
    Ok(match declared? {
        Declared::Graph(graph) => out.with("graph", graph).with("params", params::declared()).with("sites", source::sites()),
        Declared::Document { format, document, sources } => out
            .with("graph", Json::obj().with("id", "Document").with("nodes", Json::obj()))
            .with("params", params::declared())
            .with("sites", source::sites())
            .with("document", Json::obj().with("format", format).with("document", document).with("sources", sources)),
    })
}

/// Run the file's `document()` and leave the answer.
pub fn run(document: impl FnOnce() -> El) -> u32 {
    std::panic::set_hook(Box::new(|info| {
        let what = info
            .payload()
            .downcast_ref::<&str>()
            .map(|s| s.to_string())
            .or_else(|| info.payload().downcast_ref::<String>().cloned())
            .unwrap_or_else(|| "the file panicked".to_string());
        let at = info.location().map(|l| format!(" (line {})", l.line())).unwrap_or_default();
        set_out(failed(format!("{what}{at}")));
    }));
    set_out(failed("the file did not finish".into()));
    STEM_IN.with(|b| source::set_stem(&String::from_utf8_lossy(&b.borrow())));
    INPUT_IN.with(|b| {
        if !b.borrow().is_empty() {
            params::set_inputs(Json::parse(&String::from_utf8_lossy(&b.borrow())).unwrap_or(Json::Null));
        }
    });
    match answer(declare(document())) {
        Ok(answer) => {
            set_out(answer.text());
            1
        }
        Err(error) => {
            set_out(failed(error));
            0
        }
    }
}

pub fn out_ptr() -> u32 {
    OUT.with(|o| o.borrow().as_ptr() as usize as u32)
}

pub fn out_len() -> u32 {
    OUT.with(|o| o.borrow().len() as u32)
}

/// The answer left by the last `run`, as text (what the sandbox reads; for tests).
pub fn out_text() -> String {
    OUT.with(|o| String::from_utf8_lossy(&o.borrow()).into_owned())
}