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 OpenAI API (OPENAI_API_KEY or a saved key).
29    Openai,
30    /// The Anthropic API (ANTHROPIC_API_KEY or a saved key).
31    Anthropic,
32    /// The Gemini API (GEMINI_API_KEY or a saved key).
33    Gemini,
34}
35
36impl Provider {
37    /// In the order Settings lists them.
38    pub const ALL: [Self; 5] = [
39        Self::Codex,
40        Self::ClaudeAgent,
41        Self::Openai,
42        Self::Anthropic,
43        Self::Gemini,
44    ];
45
46    pub fn as_str(self) -> &'static str {
47        match self {
48            Self::Anthropic => "anthropic",
49            Self::ClaudeAgent => "claude-agent",
50            Self::Codex => "codex",
51            Self::Gemini => "gemini",
52            Self::Openai => "openai",
53        }
54    }
55
56    fn named(name: &str) -> Option<Self> {
57        Self::ALL
58            .into_iter()
59            .find(|provider| provider.as_str() == name)
60    }
61}
62
63impl std::fmt::Display for Provider {
64    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        formatter.write_str(self.as_str())
66    }
67}
68
69/// Backends that 0.5.0 removed; a declaration naming one fails with its replacements.
70const REMOVED_BACKENDS: [&str; 2] = ["chatgpt", "claude"];
71
72/// What an Agent profile's `backend` names: a built-in [`Provider`], or a custom provider
73/// set up on the machine that runs the eval. The project only declares the name.
74#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
75#[serde(transparent)]
76pub struct Backend(String);
77
78impl Backend {
79    pub fn as_str(&self) -> &str {
80        &self.0
81    }
82
83    /// The built-in provider this names, or `None` for a custom provider.
84    pub fn builtin(&self) -> Option<Provider> {
85        Provider::named(&self.0)
86    }
87}
88
89impl From<Provider> for Backend {
90    fn from(provider: Provider) -> Self {
91        Self(provider.as_str().to_owned())
92    }
93}
94
95impl std::str::FromStr for Backend {
96    type Err = String;
97    fn from_str(name: &str) -> Result<Self, Self::Err> {
98        if REMOVED_BACKENDS.contains(&name) {
99            return Err(format!(
100                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"#
101            ));
102        }
103        identifier(name, "backend")?;
104        Ok(Self(name.to_owned()))
105    }
106}
107
108impl<'de> Deserialize<'de> for Backend {
109    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
110        String::deserialize(deserializer)?
111            .parse()
112            .map_err(serde::de::Error::custom)
113    }
114}
115
116impl std::fmt::Display for Backend {
117    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118        formatter.write_str(&self.0)
119    }
120}