ai-lib-rust
Protocol Runtime for AI-Protocol β High-performance Rust reference implementation
ai-lib-rust is the Rust runtime implementation for the AI-Protocol specification, embodying the core design principle:
All logic is operators, all configuration is protocol
π― Design Philosophy
Unlike traditional adapter libraries that hardcode provider-specific logic, ai-lib-rust is a protocol-driven runtime:
- Zero Hardcoding β All behavior is driven by protocol manifests (YAML/JSON)
- Operator Pipeline β Decoder β Selector β Accumulator β FanOut β EventMapper
- Hot Reload β Protocol configurations can be updated at runtime without restart
- Unified Interface β Single API for all providers, no provider-specific code needed
ποΈ v0.9 Architecture: E/P Separation
Starting from v0.9.0, ai-lib-rust adopts an Execution/Policy separation architecture, decoupling core execution from policy decisions:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ai-lib-rust (Facade) β
β Backward-compatible entry point, re-exports core β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββ΄ββββββββββββββββββββ
βΌ βΌ
βββββββββββββββββββββββ βββββββββββββββββββββββ
β ai-lib-core β β ai-lib-contact β
β (E: Execution) β β (P: Policy) β
βββββββββββββββββββββββ€ βββββββββββββββββββββββ€
β β’ protocol loader β β β’ routing β
β β’ client β β β’ cache β
β β’ transport β β β’ batch β
β β’ pipeline βββββββββββββββββ β’ plugins β
β β’ drivers β β β’ interceptors β
β β’ types β β β’ guardrails β
β β’ structured output β β β’ telemetry β
β β’ mcp/computer_use β β β’ tokens β
β β’ embeddings β β β’ resilience β
βββββββββββββββββββββββ βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β ai-lib-wasm β
β (WASI Exports) β
βββββββββββββββββββββββ€
β β’ 6 export functionsβ
β β’ wasm32-wasip1 β
β β’ ~1.2 MB binary β
β β’ No P-layer deps β
βββββββββββββββββββββββ
Benefits of E/P Separation
| Aspect | E Layer (ai-lib-core) | P Layer (ai-lib-contact) |
|---|---|---|
| Responsibility | Deterministic execution, protocol loading, type conversion | Policy decisions, caching, routing, telemetry |
| Dependencies | Minimal, stateless | Depends on E layer, may be stateful |
| WASM | Compiles to wasm32-wasip1 | Not supported (policy logic unsuitable) |
| Use Case | Edge devices, browsers, serverless | Server-side, full applications |
Cargo Workspace Structure
| Crate | Path | Role | Published |
|---|---|---|---|
ai-lib-core |
crates/ai-lib-core |
Execution layer: protocol loading, client, transport, pipeline, types | crates.io |
ai-lib-contact |
crates/ai-lib-contact |
Policy layer: cache, batch, routing, plugins, telemetry, resilience | crates.io |
ai-lib-wasm |
crates/ai-lib-wasm |
WASI exports: 6 functions, < 2 MB | Not published |
ai-lib-rust |
crates/ai-lib-rust |
Facade: re-exports core + contact, maintains backward compatibility | crates.io |
ai-lib-wasmtime-harness |
crates/ai-lib-wasmtime-harness |
WASM integration tests (optional, heavy deps) | Not published |
π¦ Installation
Basic Installation (Facade)
[]
= "0.9"
Direct Dependency on Sub-Crates
If you only need execution layer capabilities (smaller dependency graph):
[]
= "0.9"
If you need policy layer capabilities:
[]
= "0.9"
Feature Flags
[]
# Lean core (default)
= "0.9"
# Enable specific capabilities
= { = "0.9", = ["embeddings", "telemetry"] }
# Enable all capabilities
= { = "0.9", = ["full"] }
Execution Layer Features (ai-lib-core):
embeddingsβ Embedding vector generationmcpβ MCP tool bridgingcomputer_useβ Computer Use abstractionmultimodalβ Extended multimodal supportreasoningβ Reasoning/chain-of-thought supportstt/ttsβ Speech recognition/synthesisrerankingβ Re-ranking
Policy Layer Features (ai-lib-contact):
batchβ Batch processingcacheβ Cache managementrouting_mvpβ Model routingguardrailsβ Input/output guardrailstokensβ Token counting and cost estimationtelemetryβ Telemetry and observabilityinterceptorsβ Call interceptors
π Quick Start
Basic Usage
use ;
async
Streaming Response
use ;
use StreamingEvent;
use StreamExt;
async
Sharing Client Across Tasks
AiClient intentionally does not implement Clone (API key compliance). Use Arc to share:
use Arc;
let client = new;
// Pass to multiple async tasks
let handle = spawn;
π§ Configuration
Protocol Manifest Search Path
The runtime searches for protocol configurations in the following order:
- Custom path set via
ProtocolLoader::with_base_path() AI_PROTOCOL_DIR/AI_PROTOCOL_PATHenvironment variable- Common development paths:
ai-protocol/,../ai-protocol/,../../ai-protocol/ - Final fallback: GitHub raw
ailib-official/ai-protocol
API Keys
Recommended (production):
Optional (local development): OS keyring (macOS Keychain / Windows Credential Manager / Linux Secret Service)
Production Configuration
# Proxy
# Timeout
# Concurrency limit
# Rate limiting
# or AI_LIB_RPM=300
# Circuit breaker
π§ͺ Testing
Unit Tests
Compliance Tests (Cross-Runtime Consistency)
# Default run
# Specify compliance directory
COMPLIANCE_DIR=../ai-protocol/tests/compliance
# Run from ai-lib-core (shared test suite)
COMPLIANCE_DIR=../ai-protocol/tests/compliance
WASM Integration Tests
# Build WASM
# Run wasmtime tests
Testing with Mock Server
# Start ai-protocol-mock
# Run tests with mock
MOCK_HTTP_URL=http://localhost:4010
π WASM Support
ai-lib-wasm provides server-side WASM support (wasmtime, Wasmer, etc.):
# Build WASM binary
# Output: target/wasm32-wasip1/release/ai_lib_wasm.wasm (~1.2 MB)
WASM Export Functions:
chat_syncβ Synchronous chatchat_stream_init/chat_stream_poll/chat_stream_endβ Streaming chatembed_syncβ Synchronous embedding
Limitations: WASM version only includes E-layer capabilities, without caching, routing, telemetry, etc.
π Observability
Call Statistics
let = client.call_model_with_stats.await?;
println!;
println!;
Telemetry Feedback (opt-in)
use ;
client.report_feedback.await?;
π Error Codes (V2 Specification)
All provider errors are normalized to 13 standard error codes:
| Code | Name | Retryable | Fallbackable |
|---|---|---|---|
| E1001 | invalid_request |
No | No |
| E1002 | authentication |
No | Yes |
| E1003 | permission_denied |
No | No |
| E1004 | not_found |
No | No |
| E1005 | request_too_large |
No | No |
| E2001 | rate_limited |
Yes | Yes |
| E2002 | quota_exhausted |
No | Yes |
| E3001 | server_error |
Yes | Yes |
| E3002 | overloaded |
Yes | Yes |
| E3003 | timeout |
Yes | Yes |
| E4001 | conflict |
Yes | No |
| E4002 | cancelled |
No | No |
| E9999 | unknown |
No | No |
π€ Community & Contributing
Use Cases
- Server Applications β Use
ai-lib-rust(facade) orai-lib-contactdirectly for full capabilities - Edge/Embedded β Use
ai-lib-corefor minimal dependencies and deterministic execution - Browser/WASM β Use
ai-lib-wasmfor WebAssembly environments
Contributing Guidelines
- All protocol configurations must follow AI-Protocol specification (v1.5 / V2)
- New features must include tests; compliance tests must pass
- Code must pass
cargo clippychecks - Follow Rust API design principles
Code of Conduct
- Respect all contributors
- Welcome participants from all backgrounds
- Focus on technical discussions, avoid personal attacks
- Report issues via GitHub Issues
π Related Projects
| Project | Description |
|---|---|
| AI-Protocol | Protocol specification (v1.5 / V2) |
| ai-lib-python | Python runtime |
| ai-lib-ts | TypeScript runtime |
| ai-lib-go | Go runtime |
| ai-protocol-mock | Mock server |
π License
This project is dual-licensed:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
You may choose either.
ai-lib-rust β Where protocol meets performance π