artifactize-core 0.27.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
use std::path::Path;

use serde::Serialize;

use crate::config::Backend;

pub use artifactize_provider::llm::models::ListedModel;

#[derive(Serialize)]
pub struct Listing {
    pub backend: Backend,
    pub models: Vec<ListedModel>,
}

/// The result a model menu renders. Absence of setup is not a failed listing.
pub enum Availability {
    Models(Vec<ListedModel>),
    NotSetUp {
        reason: String,
    },
    Failed {
        error: String,
    },
    CliMissing {
        reason: String,
    },
    CliTooOld {
        version: semver::Version,
        required_version: semver::Version,
        error: String,
    },
}

impl Availability {
    fn listed(listing: Result<Vec<ListedModel>, String>) -> Self {
        match listing {
            Ok(models) => Self::Models(models),
            Err(error) => Self::Failed { error },
        }
    }
}

/// Discover models and classify setup failures at the native provider boundary.
pub async fn availability(
    backend: Backend,
    state: Option<&Path>,
    repo: Option<&Path>,
) -> Availability {
    use super::Route;
    use crate::auth::{claude_agent, codex};
    use Availability::*;
    let route = match super::route(&backend, state, repo, "to list models") {
        Ok(route) => route,
        Err(failure) if failure.code == crate::agent::error::Code::Authentication => {
            return NotSetUp {
                reason: failure.message,
            };
        }
        Err(failure) => {
            return Failed {
                error: failure.message,
            };
        }
    };
    match route {
        Route::ClaudeAgent => {
            let minimum = super::claude_agent::MINIMUM_VERSION;
            match claude_agent::cli_version().await {
                Ok(None) => {
                    return CliMissing {
                        reason: "Install Claude Code (claude).".into(),
                    };
                }
                Ok(Some(version)) if version < minimum => {
                    return CliTooOld {
                        error: format!("Update Claude Code to {minimum} or newer."),
                        version,
                        required_version: minimum,
                    };
                }
                Err(error) => return Failed { error },
                Ok(Some(_)) => {}
            }
            match claude_agent::status().await {
                claude_agent::Status::Missing => CliMissing {
                    reason: "Install Claude Code (claude).".into(),
                },
                claude_agent::Status::SignedOut => NotSetUp {
                    reason: "Sign in to Claude Code.".into(),
                },
                claude_agent::Status::Failed { error } => Failed { error },
                claude_agent::Status::SignedIn { .. } => {
                    Availability::listed(super::claude_agent::models().await)
                }
            }
        }
        Route::Http(route) => {
            if route.api() == artifactize_provider::llm::Api::Codex {
                match codex::status(state, repo) {
                    Ok(codex::Status::Absent) => {
                        return NotSetUp {
                            reason: "Sign in to Codex.".into(),
                        };
                    }
                    Ok(codex::Status::Refused { reason }) => return Failed { error: reason },
                    Err(error) => return Failed { error },
                    Ok(_) => {}
                }
            }
            let location = model_credentials(&route, state, repo);
            Availability::listed(
                artifactize_provider::llm::models::list(
                    &route,
                    location.as_ref().map_err(String::as_str),
                )
                .await,
            )
        }
    }
}

/// `state` holds saved keys, custom providers and Codex credentials; `repo` keeps them
/// outside the reviewed repository.
pub async fn list(
    backend: Backend,
    state: Option<&Path>,
    repo: Option<&Path>,
) -> Result<Listing, String> {
    let route =
        super::route(&backend, state, repo, "to list models").map_err(|failure| failure.message)?;
    let models = match route {
        super::Route::ClaudeAgent => super::claude_agent::models().await?,
        super::Route::Http(route) => {
            let location = model_credentials(&route, state, repo);
            artifactize_provider::llm::models::list(
                &route,
                location.as_ref().map_err(String::as_str),
            )
            .await?
        }
    };
    Ok(Listing { backend, models })
}

fn model_credentials(
    route: &artifactize_provider::llm::HttpRoute,
    state: Option<&Path>,
    repo: Option<&Path>,
) -> Result<artifactize_provider::auth::Location, String> {
    if route.api() == artifactize_provider::llm::Api::Codex
        && crate::auth::codex::auth_file().is_none()
    {
        super::codex_location(state, repo)
    } else {
        crate::auth::credential_location(state, repo, crate::auth::storage::Tokens::Codex)
    }
}