greentic-llm 1.0.0

Provider-agnostic multi-LLM abstraction for the Greentic platform (rig-core backend, 9 providers).
Documentation
//! Environment-variable-backed [`CredentialSource`].
//!
//! Reads three variables:
//! - `GREENTIC_LLM_PROVIDER` — the active provider id (e.g. `openai`).
//! - `GREENTIC_LLM_API_KEY`  — the API key for that provider.
//! - `GREENTIC_LLM_BASE_URL` — optional override for self-hosted gateways.
//!
//! Any request for a [`ProviderKind`] that does not match
//! `GREENTIC_LLM_PROVIDER` returns [`CredError::Missing`] — the env source
//! intentionally exposes only one provider at a time.

use super::{CredError, Credential, CredentialSource};
use crate::capabilities::ProviderKind;

pub struct EnvCredentialSource;

#[async_trait::async_trait]
impl CredentialSource for EnvCredentialSource {
    async fn get_credential(&self, provider: ProviderKind) -> Result<Credential, CredError> {
        let active = std::env::var("GREENTIC_LLM_PROVIDER")
            .map_err(|_| CredError::Missing(provider))?
            .parse::<ProviderKind>()
            .map_err(|_| CredError::Missing(provider))?;

        if active != provider {
            return Err(CredError::Missing(provider));
        }

        let api_key =
            std::env::var("GREENTIC_LLM_API_KEY").map_err(|_| CredError::Missing(provider))?;

        if api_key.is_empty() {
            return Err(CredError::Missing(provider));
        }

        let base_url = std::env::var("GREENTIC_LLM_BASE_URL").ok();

        Ok(Credential {
            api_key,
            base_url,
            expires_at: None,
        })
    }
}