Skip to main content

artifactize_core/llm/
models.rs

1use std::path::Path;
2
3use serde::Serialize;
4
5use crate::config::Backend;
6
7pub use artifactize_provider::llm::models::ListedModel;
8
9#[derive(Serialize)]
10pub struct Listing {
11    pub backend: Backend,
12    pub models: Vec<ListedModel>,
13}
14
15/// The result a model menu renders. Absence of setup is not a failed listing.
16pub enum Availability {
17    Models(Vec<ListedModel>),
18    NotSetUp {
19        reason: String,
20    },
21    Failed {
22        error: String,
23    },
24    CliMissing {
25        reason: String,
26    },
27    CliTooOld {
28        version: semver::Version,
29        required_version: semver::Version,
30        error: String,
31    },
32}
33
34impl Availability {
35    fn listed(listing: Result<Vec<ListedModel>, String>) -> Self {
36        match listing {
37            Ok(models) => Self::Models(models),
38            Err(error) => Self::Failed { error },
39        }
40    }
41}
42
43/// Discover models and classify setup failures at the native provider boundary.
44pub async fn availability(
45    backend: Backend,
46    state: Option<&Path>,
47    repo: Option<&Path>,
48) -> Availability {
49    use super::Route;
50    use crate::auth::{claude_agent, codex};
51    use Availability::*;
52    let route = match super::route(&backend, state, repo, "to list models") {
53        Ok(route) => route,
54        Err(failure) if failure.code == crate::agent::error::Code::Authentication => {
55            return NotSetUp {
56                reason: failure.message,
57            };
58        }
59        Err(failure) => {
60            return Failed {
61                error: failure.message,
62            };
63        }
64    };
65    match route {
66        Route::ClaudeAgent => {
67            let minimum = super::claude_agent::MINIMUM_VERSION;
68            match claude_agent::cli_version().await {
69                Ok(None) => {
70                    return CliMissing {
71                        reason: "Install Claude Code (claude).".into(),
72                    };
73                }
74                Ok(Some(version)) if version < minimum => {
75                    return CliTooOld {
76                        error: format!("Update Claude Code to {minimum} or newer."),
77                        version,
78                        required_version: minimum,
79                    };
80                }
81                Err(error) => return Failed { error },
82                Ok(Some(_)) => {}
83            }
84            match claude_agent::status().await {
85                claude_agent::Status::Missing => CliMissing {
86                    reason: "Install Claude Code (claude).".into(),
87                },
88                claude_agent::Status::SignedOut => NotSetUp {
89                    reason: "Sign in to Claude Code.".into(),
90                },
91                claude_agent::Status::Failed { error } => Failed { error },
92                claude_agent::Status::SignedIn { .. } => {
93                    Availability::listed(super::claude_agent::models().await)
94                }
95            }
96        }
97        Route::Http(route) => {
98            if route.api() == artifactize_provider::llm::Api::Codex {
99                match codex::status(state, repo) {
100                    Ok(codex::Status::Absent) => {
101                        return NotSetUp {
102                            reason: "Sign in to Codex.".into(),
103                        };
104                    }
105                    Ok(codex::Status::Refused { reason }) => return Failed { error: reason },
106                    Err(error) => return Failed { error },
107                    Ok(_) => {}
108                }
109            }
110            let location = model_credentials(&route, state, repo);
111            Availability::listed(
112                artifactize_provider::llm::models::list(
113                    &route,
114                    location.as_ref().map_err(String::as_str),
115                )
116                .await,
117            )
118        }
119    }
120}
121
122/// `state` holds saved keys, custom providers and Codex credentials; `repo` keeps them
123/// outside the reviewed repository.
124pub async fn list(
125    backend: Backend,
126    state: Option<&Path>,
127    repo: Option<&Path>,
128) -> Result<Listing, String> {
129    let route =
130        super::route(&backend, state, repo, "to list models").map_err(|failure| failure.message)?;
131    let models = match route {
132        super::Route::ClaudeAgent => super::claude_agent::models().await?,
133        super::Route::Http(route) => {
134            let location = model_credentials(&route, state, repo);
135            artifactize_provider::llm::models::list(
136                &route,
137                location.as_ref().map_err(String::as_str),
138            )
139            .await?
140        }
141    };
142    Ok(Listing { backend, models })
143}
144
145fn model_credentials(
146    route: &artifactize_provider::llm::HttpRoute,
147    state: Option<&Path>,
148    repo: Option<&Path>,
149) -> Result<artifactize_provider::auth::Location, String> {
150    if route.api() == artifactize_provider::llm::Api::Codex
151        && crate::auth::codex::auth_file().is_none()
152    {
153        super::codex_location(state, repo)
154    } else {
155        crate::auth::credential_location(state, repo, crate::auth::storage::Tokens::Codex)
156    }
157}