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