This blueprint demonstrates how to build browser-native AI agents using WebAssembly (WASM) and WebLLM. It showcases the integration of multiple programming languages (Rust, Go, Python, JavaScript) to create high-performance, browser-based AI applications that run entirely client-side without server dependencies.
Extended with:
- π€ Agent Handoff: Multi-agent coordination with triage routing
- π οΈ Tool Calling: Autonomous tool use with LLM-driven decision making
- π¦ Compositional SDK: functional composition for agent construction
# Build and run with Docker (Recommended)
docker build -t wasm-browser-agents-app .
# For development with hot-reload
docker run -p 5173:5173 \
-v $(pwd)/demos:/app/demos \
-v $(pwd)/src:/app/src \
wasm-browser-agents-appThe Docker setup automatically handles:
- All required language toolchains (Rust, Go, Python)
- WASM compilation tools and dependencies
- Model management and resource allocation
- Development environment configuration
-
Minimum Requirements:
- 4GB RAM
- 10GB disk space
- Docker 20.10.0 or higher
-
Recommended Setup:
- 8GB+ RAM for running larger models
- NVIDIA GPU with CUDA support
- Docker Compose for development
- WSL2 on Windows systems
-
Resource Considerations:
- Rust agent with f32 models: 6GB+ VRAM
- Go agent with balanced models: 4-5GB VRAM
- Python agent: 2-4GB VRAM
- JavaScript agent: 1-2GB VRAM
-
Development Tips:
- Use volume mounts for hot-reload during development
- Monitor Docker stats for resource usage
- Clear Docker cache periodically when switching models
You can quickly run the application using the pre-built Docker image from DockerHub:
# Pull and run the latest version
docker pull hwclass/wasm-browser-agents-blueprint:latest
# Run the container with development configuration
docker run -p 5173:5173 \
-v $(pwd)/demos:/app/demos \
-v $(pwd)/src:/app/src \
hwclass/wasm-browser-agents-blueprint:latest
# Visit http://localhost:5173 in your browserThe DockerHub image includes all necessary dependencies and is pre-configured for development. For production deployment, you can run without volume mounts:
docker run -p 5173:5173 hwclass/wasm-browser-agents-blueprint:latestIf you prefer to run without Docker, you can set up manually:
# Clone the repository
git clone https://github.com/mozilla-ai/wasm-browser-agents-blueprint.git
cd wasm-browser-agents-blueprint
# Install dependencies
npm install
# Build WASM modules
chmod +x build.sh
./build.sh
# Start development server
npm run dev
# Build for production
npm run buildNote: Manual setup requires installing all language toolchains and dependencies separately. See Pre-requisites section for details.
Visit http://localhost:5173 to see the application in action.
Multi-language greeting agents demonstrating WASM integration.
Location: demos/hello-agent/
What it does: Generates greetings in multiple languages, passes to LLM for elaboration
Languages: Rust, Go, Python, JavaScript
Multi-agent coordination with intelligent routing.
Location: demos/handoff/
What it does: Triage agent routes requests to specialized WASM counter agents
Agents: Triage (JS+WebLLM), Rust Counter, Go Counter
Pattern: Closed-world routing with JSON schema validation
Autonomous tool use with LLM-driven decision making.
Location: demos/tool-calling/
What it does: Agent analyzes requests, selects tools, executes them autonomously
Tools: Character counting, webpage visiting, web search
Pattern: Multi-turn tool execution with result feedback
This repository includes a compositional SDK for building browser-native agents:
import { composeAgent, withName, withInstructions, withModel } from './src/sdk'
const agent = composeAgent(
withName("counter_rust"),
withInstructions("Count word occurrences"),
withModel({ provider: "webllm", model: "Qwen2.5-1.5B" })
)Key Principles:
- Composition over classes (functional patterns)
- Agents as decision engines (not environments)
- Clean separation: SDK β Runtime β Demo
The blueprint implements a multi-language WASM architecture that enables:
-
Language-Agnostic WASM Integration
- Rust modules for high-performance computations
- Go modules for efficient concurrent operations
- Python modules via Pyodide for flexible scripting
-
Browser-Native AI Processing
- WebLLM integration for client-side LLM inference
- Agent-specific LLM model selection:
- Rust: High-precision models optimized for performance (e.g., DeepSeek 8B f32)
- Go: Balanced models for concurrent operations (e.g., Qwen2 7B)
- Python: Research and experimental models (e.g., Phi-2)
- JavaScript: Lightweight, responsive models (e.g., TinyLlama)
- Web Workers for non-blocking background processing
- Comlink for seamless Web Worker communication
- Real-time text generation and processing
-
Modern Web Architecture
- Vite-based build system
- ES modules for clean dependency management
- Web Workers with Comlink for type-safe concurrent processing
-
System requirements:
- OS: Windows, macOS, or Linux
- Node.js 18.0 or higher
- Modern web browser with WebAssembly support
- Minimum RAM: 4GB
- Disk space: 1GB for full development setup
-
Development Dependencies:
- Rust toolchain (latest stable)
- Go 1.18 or higher
- Python 3.10 or higher
- npm or yarn package manager
wasm-browser-agents-blueprint/
βββ demos/
β βββ hello-agent/ # Multi-language greeting agents
β β βββ rust/ # Rust WASM implementation
β β βββ go/ # Go WASM implementation
β β βββ python/ # Python/Pyodide implementation
β β βββ js/ # JavaScript implementation
β βββ handoff/ # Multi-agent coordination demo
β β βββ rust/ # Rust counter agent
β β βββ go/ # Go counter agent
β β βββ wasm/ # Compiled WASM binaries
β β βββ triage.worker.js # Triage routing agent
β β βββ *.worker.js # Counter workers
β βββ tool-calling/ # Autonomous tool use demo
β βββ tools.js # Tool registry and implementations
β βββ agent.worker.js # Tool-calling agent
β βββ index.html # Demo UI
βββ src/
β βββ sdk/ # wasm-browser-agent-sdk
β βββ types.ts # Type definitions
β βββ compose.ts # Compositional API
β βββ validation.ts # I/O protocol validation
β βββ index.ts # Public exports
βββ build.sh # Main build script for all demos
βββ package.json # Node.js dependencies
βββ vite.config.mjs # Build configuration
βββ Dockerfile # Complete build environment
The project includes individual build scripts for each demo:
- Rust:
demos/hello-agent/rust/build.shβ Compiles topkg/ - Go:
demos/hello-agent/go/build.shβ Compiles tomain.wasm - Python:
demos/hello-agent/python/build.shβ Prepares for Pyodide - JavaScript:
demos/hello-agent/js/build.shβ No compilation
- Rust Counter:
demos/handoff/rust/build.shβwasm/counter_rust.wasm(~15KB) - Go Counter:
demos/handoff/go/build.shβwasm/counter_go.wasm(~2.4MB)
- No WASM build required (JavaScript tools)
Build All:
./build.sh # Builds all WASM modules for all demosWhen using Docker, all build steps are automatically handled by the Dockerfile.
- WebLLM Integration:
- Run large language models directly in your browser
- Agent-specific model optimization
- Dynamic model switching with automatic resource management
- Multi-Language WASM Support:
- π¦ Rust: High-performance, memory-safe systems programming with f32 precision models
- πΉ Go: Simple and efficient concurrent language with balanced model performance
- π Python: Running via Pyodide for flexible scripting and experimental models
- π JavaScript: Native browser implementation with lightweight models
- Multi-Agent Patterns:
- π€ Handoff: Triage routing to specialized agents
- π οΈ Tool Calling: Autonomous tool selection and execution
- π Multi-Turn Execution: Iterative tool use with result feedback
- Compositional SDK:
- No classes, pure composition (functional patterns)
- Standardized
step(input: string) -> stringagent contract - JSON Schema validation for agent I/O
- Web Workers: Background processing for smooth UI responsiveness
- Comlink Integration: Type-safe and ergonomic Web Worker communication
- Modern UI/UX: Clean, responsive interface with consistent styling
Common issues and solutions:
-
WASM Loading Issues
- Ensure your browser supports WebAssembly
- Check console for detailed error messages
- Verify WASM files are being served with correct MIME types
-
Build Problems
- Verify all required toolchains are installed
- Check Node.js version compatibility
- Clear npm cache and node_modules if needed
-
Performance Issues
- Try different WASM implementations (Rust recommended for best performance)
- Monitor browser console for memory usage
- Check Web Worker initialization status
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.
We welcome contributions! Please see our Contributing Guidelines for details on:
- Code of Conduct
- Development process
- How to submit changes
- How to report issues
- Community guidelines