Skip to main content

artifactize_core/config/
backend.rs

1//! Built-in providers and names of machine-configured backends.
2
3use super::identifier;
4use serde::{Deserialize, Serialize};
5
6/// A built-in provider: one artifactize reaches with a sign-in or an API key and no other setup.
7#[derive(
8    Debug,
9    Clone,
10    Copy,
11    PartialEq,
12    Eq,
13    PartialOrd,
14    Ord,
15    Hash,
16    Serialize,
17    Deserialize,
18    clap::ValueEnum,
19)]
20#[serde(rename_all = "lowercase")]
21pub enum Provider {
22    /// The ChatGPT/Codex account (artifactize login codex).
23    Codex,
24    /// The user's own Claude Code and its login (claude auth login).
25    #[serde(rename = "claude-agent")]
26    #[value(name = "claude-agent")]
27    ClaudeAgent,
28    /// The user’s own Google Antigravity CLI and its login (Chat only).
29    Antigravity,
30    /// The OpenAI API (OPENAI_API_KEY or a saved key).
31    Openai,
32    /// The Anthropic API (ANTHROPIC_API_KEY or a saved key).
33    Anthropic,
34    /// The Gemini API (GEMINI_API_KEY or a saved key).
35    Gemini,
36}
37
38impl Provider {
39    /// In the order Settings lists them.
40    pub const ALL: [Self; 6] = [
41        Self::Codex,
42        Self::ClaudeAgent,
43        Self::Antigravity,
44        Self::Openai,
45        Self::Anthropic,
46        Self::Gemini,
47    ];
48
49    pub fn as_str(self) -> &'static str {
50        match self {
51            Self::Antigravity => "antigravity",
52            Self::Anthropic => "anthropic",
53            Self::ClaudeAgent => "claude-agent",
54            Self::Codex => "codex",
55            Self::Gemini => "gemini",
56            Self::Openai => "openai",
57        }
58    }
59
60    pub(crate) fn validate_eval(self) -> Result<(), String> {
61        match self {
62            Self::Antigravity => Err("Antigravity is available for Chat only: Google's consumer plans don't cover automated evaluation and the CLI can't be confined to an eval's tools; use the `gemini` API-key backend".into()),
63            _ => Ok(()),
64        }
65    }
66
67    fn named(name: &str) -> Option<Self> {
68        Self::ALL
69            .into_iter()
70            .find(|provider| provider.as_str() == name)
71    }
72}
73
74impl std::fmt::Display for Provider {
75    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        formatter.write_str(self.as_str())
77    }
78}
79
80/// Backends that 0.5.0 removed; a declaration naming one fails with its replacements.
81const REMOVED_BACKENDS: [&str; 2] = ["chatgpt", "claude"];
82
83/// What an Agent profile's `backend` names: a built-in [`Provider`], or a custom provider
84/// set up on the machine that runs the eval. The project only declares the name.
85#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
86#[serde(transparent)]
87pub struct Backend(String);
88
89impl Backend {
90    pub fn as_str(&self) -> &str {
91        &self.0
92    }
93
94    /// The built-in provider this names, or `None` for a custom provider.
95    pub fn builtin(&self) -> Option<Provider> {
96        Provider::named(&self.0)
97    }
98}
99
100impl From<Provider> for Backend {
101    fn from(provider: Provider) -> Self {
102        Self(provider.as_str().to_owned())
103    }
104}
105
106impl std::str::FromStr for Backend {
107    type Err = String;
108    fn from_str(name: &str) -> Result<Self, Self::Err> {
109        if REMOVED_BACKENDS.contains(&name) {
110            return Err(format!(
111                r#"backend "{name}" was removed in 0.5.0; use "openai", "anthropic" or "gemini" with an API key, "codex" with a ChatGPT/Codex sign-in, or "claude-agent" with your Claude Code login"#
112            ));
113        }
114        identifier(name, "backend")?;
115        Ok(Self(name.to_owned()))
116    }
117}
118
119impl<'de> Deserialize<'de> for Backend {
120    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
121        String::deserialize(deserializer)?
122            .parse()
123            .map_err(serde::de::Error::custom)
124    }
125}
126
127impl std::fmt::Display for Backend {
128    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129        formatter.write_str(&self.0)
130    }
131}