oxide-agent 0.1.0

Type-safe, high-performance Rust crate for building agentic systems on Ollama
Documentation
//! WASM-sandboxed tool execution via `WasmTool`.
//!
//! Requires the `wasm-tools` feature:
//!   cargo run --example wasm_tool --features wasm-tools
//!
//! The `.wasm` module must export three symbols — see the ABI contract in
//! `src/wasm/mod.rs` for details.
//!
//! This example loads a hypothetical `sanitize.wasm` module and calls it with
//! a JSON argument object. The tool runs in a wasmtime sandbox with no access
//! to the host filesystem, network, or process state.

use std::path::Path;

use oxide_agent::wasm::{wasm_tool_definition, WasmTool};

fn main() -> anyhow::Result<()> {
    // ── Build the tool definition (JSON Schema) ───────────────────────────────
    let definition = wasm_tool_definition(
        "sanitize_input",
        "Strip dangerous characters from user-provided text",
        serde_json::json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Raw user input to sanitize"
                }
            },
            "required": ["text"]
        }),
    );

    // ── Load the .wasm binary ─────────────────────────────────────────────────
    // Swap this path for your compiled .wasm module.
    let wasm_path = Path::new("tools/sanitize.wasm");

    #[cfg(feature = "wasm-tools")]
    {
        let tool = WasmTool::from_file(wasm_path, definition)?;

        // ── Invoke the tool with JSON arguments ───────────────────────────────
        let args = serde_json::json!({ "text": "hello <script>alert(1)</script>" });
        let result = tool.call(args)?;

        println!("Sanitized: {result}");
        println!("Tool definition: {}", tool.definition().function.name);
    }

    // When the `wasm-tools` feature is disabled, from_file returns a helpful Err.
    #[cfg(not(feature = "wasm-tools"))]
    {
        let err = WasmTool::from_file(wasm_path, definition).unwrap_err();
        println!("Expected error: {err}");
        println!("Enable with: oxide-agent = {{ features = [\"wasm-tools\"] }}");
    }

    Ok(())
}