Skip to main content

artifactize_core/llm/
api.rs

1//! The wire APIs artifactize speaks, and what each accepts.
2
3use serde::{Deserialize, Serialize};
4
5use crate::config::Provider;
6
7/// The API a review's requests use: a built-in provider's, or a custom endpoint's.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Api {
10    OpenaiResponses,
11    OpenaiChat,
12    Anthropic,
13    Gemini,
14    Codex,
15    /// The user's own Claude Code, run as a subprocess (`llm::claude_agent`).
16    ClaudeAgent,
17    Antigravity,
18}
19
20impl From<Provider> for Api {
21    fn from(provider: Provider) -> Self {
22        match provider {
23            Provider::Openai => Self::OpenaiResponses,
24            Provider::Anthropic => Self::Anthropic,
25            Provider::Gemini => Self::Gemini,
26            Provider::Codex => Self::Codex,
27            Provider::Antigravity => Self::Antigravity,
28            Provider::ClaudeAgent => Self::ClaudeAgent,
29        }
30    }
31}
32
33impl From<EndpointApi> for Api {
34    fn from(api: EndpointApi) -> Self {
35        match api {
36            EndpointApi::OpenaiResponses => Self::OpenaiResponses,
37            EndpointApi::OpenaiChat => Self::OpenaiChat,
38            EndpointApi::Anthropic => Self::Anthropic,
39            EndpointApi::Gemini => Self::Gemini,
40        }
41    }
42}
43
44impl Api {
45    /// Declared effort is exact: an effort the API does not take fails rather than remaps.
46    pub fn validate_reasoning(self, reasoning: &str) -> Result<(), String> {
47        let supported = match self {
48            Self::Antigravity => return Provider::Antigravity.validate_eval(),
49            // Codex takes the Responses efforts as they are, as Pi's openai-codex does; Chat
50            // Completions' `reasoning_effort` takes the same names.
51            Self::OpenaiResponses | Self::OpenaiChat | Self::Codex => matches!(
52                reasoning,
53                "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max"
54            ),
55            Self::Anthropic => matches!(reasoning, "low" | "medium" | "high" | "max"),
56            // Claude Code's `--effort` levels.
57            Self::ClaudeAgent => matches!(reasoning, "low" | "medium" | "high" | "xhigh" | "max"),
58            // Gemini 3's thinking levels.
59            Self::Gemini => matches!(reasoning, "minimal" | "low" | "medium" | "high"),
60        };
61        if supported {
62            Ok(())
63        } else {
64            Err(format!(
65                "Unsupported reasoning {reasoning:?} for {self:?}; effort is never remapped."
66            ))
67        }
68    }
69}
70
71/// Standard model-list APIs publish names but not the allowable request effort values.
72/// Recognize the API's named families (including dated snapshots); unknown endpoints' model
73/// IDs get no options rather than a guess. `None` leaves the default to the provider.
74pub(super) fn model_efforts(
75    api: Api,
76    model: &str,
77) -> (&'static [&'static str], Option<&'static str>) {
78    const REASONING: &[&str] = &["low", "medium", "high"];
79    const CLAUDE: &[&str] = &["low", "medium", "high", "max"];
80    const OPENAI_NONE: &[&str] = &["none", "low", "medium", "high", "xhigh"];
81    // Match a family or a dated snapshot, without assuming an unlisted variant has
82    // the same capabilities.
83    let named = |name: &str| {
84        model == name
85            || model.strip_prefix(name).is_some_and(|suffix| {
86                suffix
87                    .strip_prefix('-')
88                    .is_some_and(|date| date.starts_with("20"))
89            })
90    };
91    let family = |prefix: &str| model.starts_with(prefix);
92    match api {
93        Api::OpenaiResponses | Api::OpenaiChat
94            if ["o1", "o3", "o3-mini", "o4-mini"].into_iter().any(named) =>
95        {
96            (REASONING, Some("medium"))
97        }
98        Api::OpenaiResponses if named("o3-pro") => (&["high"], Some("high")),
99        Api::OpenaiResponses | Api::OpenaiChat
100            if ["gpt-5", "gpt-5-mini", "gpt-5-nano"].into_iter().any(named) =>
101        {
102            (&["minimal", "low", "medium", "high"], Some("medium"))
103        }
104        Api::OpenaiResponses | Api::OpenaiChat if named("gpt-5.1") => {
105            (&["none", "low", "medium", "high"], Some("none"))
106        }
107        Api::OpenaiResponses | Api::OpenaiChat
108            if [
109                "gpt-5.2",
110                "gpt-5.4",
111                "gpt-5.4-mini",
112                "gpt-5.4-nano",
113                "gpt-5.5",
114            ]
115            .into_iter()
116            .any(named) =>
117        {
118            (OPENAI_NONE, Some("none"))
119        }
120        Api::OpenaiResponses
121            if ["gpt-5.2-pro", "gpt-5.4-pro", "gpt-5.5-pro"]
122                .into_iter()
123                .any(named) =>
124        {
125            (&["medium", "high", "xhigh"], Some("medium"))
126        }
127        Api::Anthropic
128            if ["claude-opus-4-6", "claude-opus-4-7", "claude-opus-4-8"]
129                .into_iter()
130                .any(named) =>
131        {
132            (CLAUDE, Some("high"))
133        }
134        Api::Anthropic if named("claude-sonnet-4-6") => (REASONING, Some("high")),
135        Api::Gemini if family("gemini-3-pro-") => (&["low", "high"], Some("high")),
136        Api::Gemini if family("gemini-3.1-pro-") => (REASONING, Some("high")),
137        Api::Gemini if family("gemini-3-flash-") || family("gemini-3.1-flash-") => {
138            (&["minimal", "low", "medium", "high"], None)
139        }
140        _ => (&[], None),
141    }
142}
143
144/// The API a custom endpoint speaks.
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
146#[serde(rename_all = "kebab-case")]
147pub enum EndpointApi {
148    /// OpenAI Responses (`/responses`).
149    OpenaiResponses,
150    /// OpenAI Chat Completions (`/chat/completions`), which most compatible servers offer.
151    OpenaiChat,
152    /// Anthropic Messages (`/v1/messages`).
153    Anthropic,
154    /// Gemini GenerateContent.
155    Gemini,
156}