Expand description
§Firebase AI Port (Rust)
This module hosts the Rust port of the experimental Firebase AI client. It exposes the same high-level entry points as the JavaScript SDK (getAI, backend configuration helpers) while adopting idiomatic Rust patterns for component registration, backend selection, and error handling. Runtime inference is currently stubbed so the surrounding Firebase app infrastructure can already depend on the public API.
§Porting status
- ai 30%
[### ]
(Status updated October 2025 after porting backend selection helpers, getAI wiring, the shared error surface, and the GenerativeModel skeleton.)
§Quick Start Example
use firebase_rs_sdk::ai::backend::Backend;
use firebase_rs_sdk::ai::public_types::AiOptions;
use firebase_rs_sdk::ai::{get_ai, GenerateTextRequest};
use firebase_rs_sdk::app::api::initialize_app;
use firebase_rs_sdk::app::{FirebaseAppSettings, FirebaseOptions};
let app = initialize_app(
FirebaseOptions {
api_key: Some("fake-key".into()),
project_id: Some("demo-project".into()),
app_id: Some("1:123:web:abc".into()),
..Default::default()
},
Some(FirebaseAppSettings::default()),
)?;
let ai = get_ai(
Some(app),
Some(AiOptions {
backend: Some(Backend::vertex_ai("us-central1")),
use_limited_use_app_check_tokens: Some(false),
}),
)?;
let response = ai.generate_text(GenerateTextRequest {
prompt: "Hello Gemini!".to_owned(),
model: None,
})?;
println!("{}", response.text);
§Implemented
- Component registration for the
aiFirebase namespace with multi-instance support. Backendconfiguration enum plusGoogleAiBackend/VertexAiBackend, matchingpackages/ai/src/backend.ts.- Instance identifier helpers (
encode_instance_identifier,decode_instance_identifier) to keep parity with JS caching semantics. - Public
get_aiAPI that mirrors the JavaScriptgetAI()surface, including backend-aware caching and runtime option updates. - Stubbed
AiService::generate_textmethod with backend-aware diagnostics for integration testing. - Basic unit coverage for backend differentiation, caching behaviour, and prompt validation.
- Rich error surface (
AiError,AiErrorCode,CustomErrorData) aligned withpackages/ai/src/errors.ts, plus helper tests. - HTTP request factory (
RequestOptions,PreparedRequest) mirroringconstructRequestinpackages/ai/src/requests/request.ts, so clients can build REST calls without leaving Rust. GenerativeModelskeleton that normalises model names across backends and preparesgenerateContentrequests on top of the request factory.
§Still to do
- Implement real REST/streaming backends to call Google AI and Vertex AI endpoints.
- Use the shared error taxonomy with real request/response handling once the REST pipeline lands.
- Complete the
GenerativeModelAPI (streaming, chat, token counting) and portImagenModelandLiveGenerativeModelclasses along with their builders and helpers. - Add schema builders, mapper utilities, and browser-specific adapters (
chromeAdapter, WebSocket handler). - Translate the comprehensive TypeScript test suites (backend, helpers, mappers, service) to Rust.
§Next steps - Detailed completion plan
- Streaming groundwork – Add placeholder traits and data structures for streaming responses (mirroring
websocket.ts) so later work can focus on transport implementations without refactoring the public API. - Error mapping integration – Use
CustomErrorDatato map HTTP/provider failures once real network calls are wired up. - Credential attachment – Surface hooks for App Check and Auth token providers so prepared requests can include the relevant headers.
- Generative responses – Introduce response types and helpers so
generate_contentcallers receive structured data rather than raw JSON. - Test migration – Start translating
packages/ai/src/api.test.tsandhelpers.test.tsinto Rust unit tests to lock in the new behaviours.
Modules§
Structs§
- AiService
- Generate
Text Request - Generate
Text Response - Generative
Model - Port of the Firebase JS SDK
GenerativeModelclass. - Prepared
Request - Prepared HTTP request ready to be executed by an HTTP client.
- Request
Options - Additional per-request options.
Enums§
Functions§
- get_ai
- Returns an AI service instance, mirroring the JavaScript
getAI()API. - get_
ai_ service - Convenience wrapper that mirrors the original Rust stub signature.
- register_
ai_ component - Registers the AI component in the global registry.