pub struct AiClient { /* private fields */ }Expand description
Generic AI client for all providers.
Holds HTTP client, API key, and model configuration for reuse across multiple requests. Uses the provider registry to get provider-specific configuration.
Implementations§
Source§impl AiClient
impl AiClient
Sourcepub fn new(provider_name: &str, config: &AiConfig) -> Result<Self>
pub fn new(provider_name: &str, config: &AiConfig) -> Result<Self>
Creates a new AI client from configuration.
Validates the model against cost control settings and fetches the API key from the environment.
§Arguments
provider_name- Name of the provider (e.g., “openrouter”, “gemini”)config- AI configuration with model, timeout, and cost control settings
§Errors
Returns an error if:
- Provider is not found in registry
- Model is not in free tier and
allow_paid_modelsis false (forOpenRouter) - API key environment variable is not set
- HTTP client creation fails
Sourcepub fn with_api_key(
provider_name: &str,
api_key: SecretString,
model_name: &str,
config: &AiConfig,
) -> Result<Self>
pub fn with_api_key( provider_name: &str, api_key: SecretString, model_name: &str, config: &AiConfig, ) -> Result<Self>
Creates a new AI client with a provided API key and validates the model exists.
This constructor validates that the model exists via the runtime model registry before creating the client. It allows callers to provide an API key directly, enabling multi-platform credential resolution (e.g., from iOS keychain via FFI).
§Arguments
provider_name- Name of the provider (e.g., “openrouter”, “gemini”)api_key- API key as aSecretStringmodel_name- Model name to use (e.g., “gemini-3.1-flash-lite-preview”)config- AI configuration with timeout and cost control settings
§Errors
Returns an error if:
- Provider is not found in registry
- Model is not in free tier and
allow_paid_modelsis false (forOpenRouter) - HTTP client creation fails
Sourcepub fn from_claude_credentials(config: &AiConfig) -> Result<Option<Self>>
pub fn from_claude_credentials(config: &AiConfig) -> Result<Option<Self>>
Creates a new AI client from Claude credentials file (~/.claude/credentials.json).
Reads the credentials file, extracts the access token, stores it in the OS keyring,
and returns an AiClient configured for the Anthropic provider.
§Arguments
config- AI configuration with timeout and cost control settings
§Returns
Returns Ok(Some(AiClient)) if credentials are found and valid,
Ok(None) if the credentials file is missing or invalid,
or an error if keyring operations fail.
Sourcepub fn claude_credentials_path() -> Option<PathBuf>
pub fn claude_credentials_path() -> Option<PathBuf>
Returns the path to the Claude credentials file if it exists.
This helper centralizes the path resolution logic for ~/.claude/credentials.json, keeping the CLI command layer thin and avoiding duplicate path construction.
Returns Some(path) if the file exists, None otherwise.
Sourcepub fn from_keyring_oauth(config: &AiConfig) -> Result<Option<Self>>
pub fn from_keyring_oauth(config: &AiConfig) -> Result<Option<Self>>
Attempts to retrieve a Claude OAuth token from the OS keyring.
Returns Ok(Some(AiClient)) if a token is found in the keyring,
Ok(None) if no token is stored, or an error if keyring operations fail.
Sourcepub fn auth_method(&self) -> AuthMethod
pub fn auth_method(&self) -> AuthMethod
Returns the authentication method used by this client.
Sourcepub fn circuit_breaker(&self) -> &CircuitBreaker
pub fn circuit_breaker(&self) -> &CircuitBreaker
Get the circuit breaker for this client.
Trait Implementations§
Source§impl AiProvider for AiClient
impl AiProvider for AiClient
Source§fn api_key_env(&self) -> &str
fn api_key_env(&self) -> &str
Source§fn http_client(&self) -> &Client
fn http_client(&self) -> &Client
Source§fn api_key(&self) -> &SecretString
fn api_key(&self) -> &SecretString
Source§fn max_tokens(&self) -> u32
fn max_tokens(&self) -> u32
Source§fn temperature(&self) -> f32
fn temperature(&self) -> f32
Source§fn max_attempts(&self) -> u32
fn max_attempts(&self) -> u32
Source§fn circuit_breaker(&self) -> Option<&CircuitBreaker>
fn circuit_breaker(&self) -> Option<&CircuitBreaker>
Source§fn custom_guidance(&self) -> Option<&str>
fn custom_guidance(&self) -> Option<&str>
Source§fn build_headers(&self) -> HeaderMap
fn build_headers(&self) -> HeaderMap
Source§fn is_anthropic(&self) -> bool
fn is_anthropic(&self) -> bool
cache_control on message blocks. Read more