WASM Target
Sage can compile agents to WebAssembly for browser execution.
Building for WASM
sage build hello.sg --target web
This compiles your Sage program through the full pipeline (parse, type-check, codegen) but targets wasm32-unknown-unknown instead of your native platform.
Output
The build produces a pkg/ directory containing:
pkg/
hello.js # JavaScript glue (wasm-bindgen)
hello_bg.wasm # WebAssembly binary
Prerequisites
The WASM target requires:
wasm32-unknown-unknownRust target:rustup target add wasm32-unknown-unknownwasm-bindgen-cli:cargo install wasm-bindgen-cli- (Optional)
wasm-optfor size optimisation: install via binaryen
Target values
The --target flag accepts:
| Value | Description |
|---|---|
native | Default. Compile to a native binary. |
web or wasm | Compile to WebAssembly for browser use. |
How It Works
When targeting WASM, the codegen layer:
- Generates a
#[wasm_bindgen(start)]entry point instead of#[tokio::main] - Uses
sage-runtime-web— a browser-compatible runtime that replacestokio,reqwest, and native I/O with Web APIs - Produces a
cdylibcrate compiled withwasm-bindgen - Optionally runs
wasm-opt -Ozfor size optimisation
Using in a Web Page
<script type="module">
import init from './pkg/hello.js';
await init();
</script>
The WASM module initialises automatically via the #[wasm_bindgen(start)] entry point.
Limitations
divine(LLM calls) requires a browser-accessible OpenAI-compatible endpointDatabaseandShelltools are not available in WASMFsoperations use browser storage APIs instead of the filesystem- Agent concurrency uses
wasm_bindgen_futures::spawn_local(single-threaded)