nitpicker-agent 0.1.0

Reusable agentic loop (file/git tools + parallel subagents) extracted from nitpicker
Documentation
#[cfg(feature = "antigravity")]
use crate::config::Config;
use crate::config::{
    AggregatorConfig, ProviderType, ReviewerConfig, is_azure_ad_auth, is_codex_auth,
};
use crate::llm::{LLMClient, LLMClientDyn, LLMProvider, WithRetryExt};
use eyre::Result;
use std::sync::Arc;

// The Antigravity Gemini proxy path is gated behind the `antigravity` feature; these
// predicates (and the proxy branches below) compile out entirely when it is off.
#[cfg(feature = "antigravity")]
pub fn needs_gemini_proxy(provider: &ProviderType, auth: Option<&str>) -> bool {
    provider.is_gemini() && matches!(auth, Some("agy-keyring"))
}

#[cfg(feature = "antigravity")]
pub fn reviewer_needs_gemini_proxy(reviewer: &ReviewerConfig) -> bool {
    needs_gemini_proxy(&reviewer.provider, reviewer.auth.as_deref())
}

#[cfg(feature = "antigravity")]
pub fn aggregator_needs_gemini_proxy(agg: &AggregatorConfig) -> bool {
    needs_gemini_proxy(&agg.provider, agg.auth.as_deref())
}

#[cfg(feature = "antigravity")]
pub fn config_needs_gemini_proxy(config: &Config) -> bool {
    aggregator_needs_gemini_proxy(&config.aggregator)
        || config.reviewer.iter().any(reviewer_needs_gemini_proxy)
}

/// Build a refreshing Azure AD client (feature `azure`). The config validator already rejects
/// `auth = "azure-ad"` when the feature is absent, so the disabled arm is defensive.
fn build_azure_ad_client(
    provider: &ProviderType,
    base_url: Option<&str>,
    scope: Option<&str>,
    credentials: Option<&str>,
) -> Result<Arc<dyn LLMClientDyn>> {
    #[cfg(feature = "azure")]
    {
        Ok(
            crate::azure::build_azure_client(provider, base_url, scope, credentials)?
                .with_retry()
                .into_arc(),
        )
    }
    #[cfg(not(feature = "azure"))]
    {
        let _ = (provider, base_url, scope, credentials);
        eyre::bail!("auth = \"azure-ad\" requires building nitpicker with `--features azure`")
    }
}

pub fn provider_from_config(
    provider: &ProviderType,
    base_url: Option<&str>,
    api_key_env: Option<&str>,
) -> Result<LLMProvider> {
    match provider {
        ProviderType::Anthropic => Ok(LLMProvider::Anthropic {
            base_url: base_url.map(str::to_string),
            api_key_env: api_key_env.map(str::to_string),
        }),
        ProviderType::Gemini => Ok(LLMProvider::Gemini {
            base_url: base_url.map(str::to_string),
            api_key_env: api_key_env.map(str::to_string),
        }),
        ProviderType::OpenAi => Ok(LLMProvider::OpenAi {
            base_url: base_url.map(str::to_string),
            api_key_env: api_key_env.map(str::to_string),
        }),
        ProviderType::OpenRouter => Ok(LLMProvider::OpenRouter {
            api_key_env: api_key_env.unwrap_or("OPENROUTER_API_KEY").to_string(),
        }),
    }
}

pub fn build_reviewer_client(
    reviewer: &ReviewerConfig,
    proxy_url: Option<&str>,
) -> Result<Arc<dyn LLMClientDyn>> {
    #[cfg(feature = "antigravity")]
    if reviewer_needs_gemini_proxy(reviewer) {
        let url =
            proxy_url.ok_or_else(|| eyre::eyre!("Gemini proxy required but not available"))?;
        return crate::llm::create_gemini_client_with_proxy(url);
    }
    #[cfg(not(feature = "antigravity"))]
    let _ = proxy_url;

    if is_azure_ad_auth(reviewer.auth.as_deref()) {
        return build_azure_ad_client(
            &reviewer.provider,
            reviewer.base_url.as_deref(),
            reviewer.azure_scope.as_deref(),
            reviewer.azure_credentials.as_deref(),
        );
    }

    if is_codex_auth(reviewer.auth.as_deref()) {
        return crate::codex::shared_client();
    }

    Ok(provider_from_config(
        &reviewer.provider,
        reviewer.base_url.as_deref(),
        reviewer.api_key_env.as_deref(),
    )?
    .client_from_env()?
    .with_retry()
    .into_arc())
}

pub fn build_aggregator_client(
    agg: &AggregatorConfig,
    proxy_url: Option<&str>,
) -> Result<Arc<dyn LLMClientDyn>> {
    #[cfg(feature = "antigravity")]
    if aggregator_needs_gemini_proxy(agg) {
        let url =
            proxy_url.ok_or_else(|| eyre::eyre!("Gemini proxy required but not available"))?;
        return crate::llm::create_gemini_client_with_proxy(url);
    }
    #[cfg(not(feature = "antigravity"))]
    let _ = proxy_url;

    if is_azure_ad_auth(agg.auth.as_deref()) {
        return build_azure_ad_client(
            &agg.provider,
            agg.base_url.as_deref(),
            agg.azure_scope.as_deref(),
            agg.azure_credentials.as_deref(),
        );
    }

    if is_codex_auth(agg.auth.as_deref()) {
        return crate::codex::shared_client();
    }

    Ok(provider_from_config(
        &agg.provider,
        agg.base_url.as_deref(),
        agg.api_key_env.as_deref(),
    )?
    .client_from_env()?
    .with_retry()
    .into_arc())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn gemini_carries_base_url_and_api_key_env() {
        let provider = provider_from_config(
            &ProviderType::Gemini,
            Some("http://localhost:8080"),
            Some("MY_GEMINI_KEY"),
        )
        .unwrap();
        match provider {
            LLMProvider::Gemini {
                base_url,
                api_key_env,
            } => {
                assert_eq!(base_url.as_deref(), Some("http://localhost:8080"));
                assert_eq!(api_key_env.as_deref(), Some("MY_GEMINI_KEY"));
            }
            _ => panic!("expected Gemini variant"),
        }
    }
}