Google Antigravity Rust SDK
https://github.com/user-attachments/assets/93e00393-6fe8-4546-85be-214b91b4de58
The Google Antigravity SDK is a Rust library for building AI agents powered by Antigravity and Gemini. It provides a secure, scalable, and stateful infrastructure layer that abstracts the agentic loop, letting you focus on what your agent does rather than how it runs.
Installation
-
Add the SDK and tokio to your project:
-
Obtain the
localharnessbinary: The SDK relies on a compiled native Go runtime binary (localharness) to orchestrate agent operations. You have two options to install it:-
Option A: Without Python/pip (Recommended for Rust-only environments & web servers) Run the helper installer script to download and extract the binary directly from PyPI (wheels are standard ZIP files):
-
Option B: Using Python/pip If Python is already installed on your development machine, simply run:
The Rust SDK will automatically locate the binary inside the Python package installation directory fallback.
-
Quickstart
Get started by setting your API key and running the hello_world example:
Concepts
Simple Agent
The Agent struct manages the full lifecycle — binary discovery, tool wiring, hook registration, and policy defaults.
use Agent;
async
Advanced Usage with Conversation
For full control over the connection lifecycle, use Conversation with a ConnectionStrategy directly. Conversation is a stateful session that accumulates step history:
use AgentConfig;
use AnyConnection;
use Conversation;
use LocalConnectionStrategy;
use ToolRunner;
use GeminiConfig;
use Arc;
async
Features
Custom Tools
Register Rust functions as custom tools:
use ;
use Tool;
use Value;
;
Hooks and Policies
Control agent behavior with a declarative policy system:
use ;
let policies = vec!;
Web Integration
The SDK provides two patterns for building web applications with AI agents:
Leptos + Axum (Native)
For standard web servers (VPS, Docker, bare metal). The Agent runs in-process with full SDK features.
# Open http://localhost:3000
Leptos + Spin/WASI (Edge)
For edge/serverless deployments on Spin (formerly Fermyon Spin, now under Akamai). Since Spin components cannot make outbound TCP/WebSocket connections directly, this target runs in Sidecar Mode, where the component communicates via HTTP with a native runner process (agent_server):
# Terminal 1: Start the agent sidecar
GEMINI_API_KEY=your-key
# Terminal 2: Start Spin
# Open http://localhost:3000
See
examples/README.mdfor detailed architecture diagrams and configuration options.
WebAssembly (WASM) & Edge Compilation
The SDK supports compiling directly to the wasm32-wasip1 WebAssembly target. This enables running agents inside sandboxed Wasm runtimes (such as Spin, Wasmtime, or Wasmer) that support WASI network sockets.
Wasm Connection Architecture
In standard native environments, the SDK spawns the localharness binary as a local subprocess. Since process spawning is not supported inside WebAssembly sandboxes, when targeting WASM, the SDK switches to Direct Network Connection Mode via WasmConnectionStrategy and WasmConnection:
- Host Server: A host-side
localharnessWebSocket server is run on the machine/VM (configured viaANTIGRAVITY_HARNESS_HOSTandANTIGRAVITY_HARNESS_PORT). - WebSocket Handshake: The
WasmConnectionStrategyopens a TCP socket to the harness server, initiates a WebSocket client handshake with the Gemini API key passed via thex-goog-api-keyheader, and upgrades it to a bi-directional stream. - Session Handshake: The client sends an
InitializeConversationEventcontaining theHarnessConfig(tools, workspaces, and system instructions) to start the stateful agent trajectory. - Asynchronous Event Loop: Two asynchronous tasks are spawned under the WASM event loop:
- Reader Loop: Continuously reads WebSocket frames, parses incoming
OutputEventmessages (includingStepUpdateandTrajectoryStateUpdate), maps them to SDKSteptypes, dispatches lifecycle hooks, and routes tool calls. - Sender Loop: Buffers and sends outgoing
InputEventmessages (user messages, tool responses, question replies, etc.) back to the harness.
- Reader Loop: Continuously reads WebSocket frames, parses incoming
Running the WASM Mock Test Suite
The mock and unit test suite in src/wasm.rs validates WASM-specific functionality (connection lifecycle, event loops, tool call/result extraction, and idle state transitions). Since it uses a standard WebSocket framework, you can run the suite natively on your host machine without a full WebAssembly runner:
# Run all unit tests including the WASM mock connection tests
This mock test suite programmatically binds a local WebSocket server to a dynamically allocated free port, completes the initialization handshake, feeds mock trajectory state updates/step updates, and asserts that WasmConnection correctly receives messages, invokes callback hooks, and cleanly terminates the stream upon transitioning to the idle state.
Rust 2024 and Native Async Traits
The SDK has been fully refactored to use native async traits (stable since Rust 1.75 / Rust 2024), completely removing the dependency on the #[async_trait] macro.
- Native Async Traits: Custom implementations of
Connection,Hook,Tool, andTriggernow use standard async function definitions returningimpl Future<Output = ...> + Sendwhere necessary to maintain compile-time thread safety bounds. - Dynamic Dispatch via Blanket Impls: Because native async traits are not directly object-safe (
dyn Traitcompatible) without manual workarounds, the SDK defines companion traitsDynConnection,DynHook,DynTool, andDynTriggerwhich are object-safe and automatically implemented via blanket implementations for any type implementing the base trait. This allows clean, zero-overhead dynamic dispatch (e.g.Arc<dyn DynHook>).
Local Development
This project uses just to manage development tasks.
- Check Code Quality: Run all style, lint, and test checks.
- Install Local Harness: Download and configure the required
localharnessbinary. - Bump version & Release tag: Bump the package version, update Cargo.lock, commit using
--no-verify(skipping git hooks), and tag the release.# Auto-bump patch version (e.g., 0.1.0 -> 0.1.1) # Or force/override with a specific version - Publish to Crates.io: Manually publish the package to crates.io (runs
just checkfirst).
Architecture
For more information, see ARCHITECTURE.md.
License
This project is licensed under the MIT License.