Skip to main content

otter_ai/
lib.rs

1//! # otter-ai
2//!
3//! Unified LLM API with provider collections, automatic auth resolution,
4//! token and cost tracking, and simple context persistence and hand-off
5//! to other models mid-session.
6//!
7//! This is a Rust re-implementation of the `@earendil-works/pi-ai` TypeScript package.
8//!
9//! ## Quick Start
10//!
11//! ```rust,no_run
12//! use otter_ai::*;
13//! use otter_ai::types::*;
14//! use otter_ai::models::create_models;
15//! use otter_ai::providers::faux::register_faux_provider;
16//!
17//! # async fn example() -> anyhow::Result<()> {
18//! // 1. Create the models registry
19//! let models = create_models();
20//!
21//! // 2. Register a provider
22//! let reg = register_faux_provider(None);
23//! models.set_provider_arc(reg.provider.clone());
24//!
25//! // 3. Refresh the model catalog
26//! models.refresh_all(false, true).await?;
27//!
28//! // 4. Look up a model
29//! let model = models.get_model("faux", "faux-mini")
30//!     .expect("model not found");
31//!
32//! // 5. Build a conversation context
33//! let context = Context {
34//!     system_prompt: Some("You are a helpful assistant.".into()),
35//!     messages: vec![Message::User {
36//!         content: vec![ContentBlock::Text { text: "Hello!".into(), text_signature: None}],
37//!         timestamp: chrono::Utc::now().timestamp_millis(),
38//!     }],
39//!     ..Default::default()
40//! };
41//!
42//! // 6. Get a complete response
43//! let options = SimpleStreamOptions::default();
44//! let response = models.complete(&model, context, options).await?;
45//! println!("{}", content_text(&match &response {
46//!     Message::Assistant { content, .. } => content,
47//!     _ => panic!("unexpected message type"),
48//! }));
49//! # Ok(())
50//! # }
51//! ```
52
53pub mod auth;
54pub mod models;
55pub mod models_store;
56pub mod providers;
57pub mod types;
58pub mod utils;
59
60// Re-exports from types
61pub use types::{
62    content_text, uuidv7, Api, ApiStreamOptions, AssistantMessage, AssistantMessageEvent,
63    CancellationToken, ContentBlock, Context, ImageContent, JsonSchemaFormat, KnownApi, Message,
64    Model, ModelCostRates, ModelThinkingLevel, ProviderEnv, ProviderHeaders, ProviderId,
65    ProviderRequestOptions, ResponseFormat, SimpleStreamOptions, Tool, ToolChoice, Usage,
66    UsageCost,
67};
68
69// Re-exports from models
70pub use models::{create_models, Models};
71
72// Re-exports from models_store
73pub use models_store::{InMemoryModelsStore, ModelsStore, ModelsStoreEntry};
74
75// Re-exports from auth
76pub use auth::{
77    default_provider_auth_context, parse_loose_credential, AuthContext, Credential,
78    CredentialStore, FileCredentialStore, InMemoryCredentialStore, ModelAuth,
79};
80
81// Re-exports from utils
82pub use utils::{
83    calculate_usage_cost, config_dir, config_path, create_assistant_message_event_stream,
84    ensure_config_dir, parse_partial_json, string_enum_schema, tool_from_schema,
85    validate_tool_arguments, with_retry, AssistantMessageEventStream, EventStream, RetryConfig,
86};
87
88// Re-export async-trait for test/provider impls built on top of otter-ai.
89pub use async_trait::async_trait;
90
91// Re-export schemars for schema generation (replaces TypeBox from TS)
92pub use schemars;
93pub use schemars::schema_for;
94pub use schemars::JsonSchema;
95
96// Re-export serde_json for Value
97pub use serde_json;