跳转到内容

Rust SDK Overview

此内容尚不支持你的语言。

ai-lib-rust (v0.8.0) is the high-performance Rust runtime for the AI-Protocol specification. It implements a protocol-driven architecture where all provider behavior comes from configuration, not code.

ai-lib-rust v0.8.0 fully implements the V2 protocol specification:

  • V2 Manifest Loading: Three-ring manifest parser (ManifestV2) with V1 auto-promotion
  • ProviderDriver: Box<dyn ProviderDriver> abstraction with OpenAI, Anthropic, and Gemini drivers
  • Capability Registry: Feature-gate based dynamic capability detection and module loading
  • MCP Tool Bridge: McpToolBridge for converting MCP tools to AI-Protocol format with namespace isolation
  • Computer Use: ComputerAction enum + SafetyPolicy for protocol-driven safety enforcement
  • Extended Multimodal: MultimodalCapabilities for modality detection, format validation, and capability checking
  • Standard Error Codes: 13-variant StandardErrorCode enum (E1001–E9999) integrated into all error paths
  • 185+ Tests: Comprehensive coverage including 6 V2 integration tests and 53 CLI validations

The SDK is organized into distinct layers:

The user-facing API:

  • AiClient — Main entry point, created from model identifiers
  • AiClientBuilder — Configuration builder with resilience settings
  • ChatRequestBuilder — Fluent API for building chat requests
  • CallStats — Request/response statistics (tokens, latency)
  • CancelHandle — Graceful stream cancellation

Loads and interprets AI-Protocol manifests:

  • ProtocolLoader — Loads from local files, env vars, or GitHub
  • ProtocolManifest — Parsed provider configuration
  • Validator — JSON Schema validation
  • UnifiedRequest — Standard request format compiled to provider-specific JSON

The heart of streaming processing — an operator-based pipeline:

  • Decoder — Converts byte streams to JSON frames (SSE, JSON Lines)
  • Selector — Filters frames using JSONPath expressions
  • Accumulator — Statefully assembles tool calls from partial chunks
  • FanOut — Expands multi-candidate responses
  • EventMapper — Converts frames to unified StreamingEvent types
  • Retry/Fallback — Pipeline-level retry and fallback operators

HTTP communication:

  • HttpTransport — reqwest-based HTTP client
  • Auth — API key resolution (OS keyring → env vars)
  • Middleware — Transport middleware for logging, metrics

Production reliability patterns:

  • CircuitBreaker — Open/half-open/closed failure isolation
  • RateLimiter — Token bucket algorithm
  • Backpressure — max_inflight semaphore
  • drivers/ProviderDriver trait + OpenAI, Anthropic, Gemini implementations; auto-selected by manifest api_style
  • registry/CapabilityRegistry for runtime module loading based on manifest declarations
  • mcp/McpToolBridge for MCP tool format conversion, namespace isolation (mcp__{server}__{tool}), and provider-specific config extraction
  • computer_use/ComputerAction enum (screenshot, mouse, keyboard, browser, file ops) + SafetyPolicy (domain allowlist, sensitive path protection, max actions per turn)
  • multimodal/MultimodalCapabilities for modality detection, format validation (image, audio, video), and content block validation
  • embeddings/ — EmbeddingClient with vector operations
  • cache/ — Response caching with TTL (MemoryCache)
  • batch/ — BatchCollector and BatchExecutor
  • tokens/ — Token counting and cost estimation
  • plugins/ — Plugin trait, registry, hooks, middleware
  • guardrails/ — Content filtering, PII detection
  • routing/ — Model routing and load balancing (feature-gated)
  • telemetry/ — Feedback sink for user feedback collection
CratePurpose
tokioAsync runtime
reqwestHTTP client
serde / serde_json / serde_yamlSerialization
jsonschemaManifest validation
tracingStructured logging
arc-swapHot-reload support
notifyFile watching
keyringOS keyring integration

Optional features enabled via Cargo (use full to enable all):

FeatureWhat it enables
v2V2 manifest loading, ProviderDriver, Capability Registry
mcpMCP tool bridge, namespace isolation, provider config extraction
computer_useComputer Use actions, safety policy enforcement
multimodalExtended multimodal capabilities, format validation
embeddingsEmbeddingClient, vector operations
batchBatchCollector, BatchExecutor
guardrailsContent filtering, PII detection
tokensToken counting, cost estimation
telemetryAdvanced observability sinks
routing_mvpCustomModelManager, ModelArray, load balancing strategies
interceptorsInterceptorPipeline for logging, metrics, audit
reasoningExtended thinking, reasoning traces
VariablePurpose
AI_PROTOCOL_DIRProtocol manifest directory
<PROVIDER>_API_KEYProvider API key (e.g., OPENAI_API_KEY)
AI_LIB_RPSRate limit (requests per second)
AI_LIB_BREAKER_FAILURE_THRESHOLDCircuit breaker threshold
AI_LIB_MAX_INFLIGHTMax concurrent requests
AI_HTTP_TIMEOUT_SECSHTTP timeout