Skip to main content

artifactize_harness/
api.rs

1//! The wire APIs artifactize speaks, and what each accepts.
2
3use crate::backend::Provider;
4
5/// The API a review's requests use: a built-in provider's, or a custom endpoint's.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Api {
8    OpenaiResponses,
9    OpenaiChat,
10    Anthropic,
11    Gemini,
12    Codex,
13    /// The user's own Claude Code, run as a subprocess (`llm::claude_agent`).
14    ClaudeAgent,
15}
16
17impl From<Provider> for Api {
18    fn from(provider: Provider) -> Self {
19        match provider {
20            Provider::Openai => Self::OpenaiResponses,
21            Provider::Anthropic => Self::Anthropic,
22            Provider::Gemini => Self::Gemini,
23            Provider::Codex => Self::Codex,
24            Provider::ClaudeAgent => Self::ClaudeAgent,
25        }
26    }
27}
28
29pub use artifactize_provider::llm::EndpointApi;
30
31use artifactize_provider::llm::Api as HttpApi;
32
33impl From<HttpApi> for Api {
34    fn from(api: HttpApi) -> Self {
35        match api {
36            HttpApi::OpenaiResponses => Self::OpenaiResponses,
37            HttpApi::OpenaiChat => Self::OpenaiChat,
38            HttpApi::Anthropic => Self::Anthropic,
39            HttpApi::Gemini => Self::Gemini,
40            HttpApi::Codex => Self::Codex,
41        }
42    }
43}
44
45impl From<EndpointApi> for Api {
46    fn from(api: EndpointApi) -> Self {
47        Self::from(HttpApi::from(api))
48    }
49}
50
51impl Api {
52    fn http(self) -> Option<HttpApi> {
53        Some(match self {
54            Self::OpenaiResponses => HttpApi::OpenaiResponses,
55            Self::OpenaiChat => HttpApi::OpenaiChat,
56            Self::Anthropic => HttpApi::Anthropic,
57            Self::Gemini => HttpApi::Gemini,
58            Self::Codex => HttpApi::Codex,
59            Self::ClaudeAgent => return None,
60        })
61    }
62
63    pub fn validate_reasoning(self, reasoning: &str) -> Result<(), String> {
64        match self.http() {
65            Some(api) => api.validate_reasoning(reasoning),
66            None if matches!(reasoning, "low" | "medium" | "high" | "xhigh" | "max") => Ok(()),
67            None => Err(format!(
68                "Unsupported reasoning {reasoning:?} for {self:?}; effort is never remapped."
69            )),
70        }
71    }
72}
73
74/// Checked request parameters, shared by reviews and their follow-ups.
75pub fn parameters(
76    api: Api,
77    reasoning: Option<&str>,
78    session: &str,
79) -> Result<serde_json::Value, String> {
80    if let Some(reasoning) = reasoning {
81        api.validate_reasoning(reasoning)?;
82    }
83    Ok(parameters_unchecked(api, reasoning, session))
84}
85
86pub fn parameters_unchecked(api: Api, reasoning: Option<&str>, session: &str) -> serde_json::Value {
87    use serde_json::json;
88    if let Some(api) = api.http() {
89        return artifactize_provider::llm::Client::parameters_unchecked(api, reasoning, session);
90    }
91    let mut value = json!({"thinking":"adaptive", "thinkingDisplay":"summarized"});
92    if let Some(effort) = reasoning {
93        value["effort"] = json!(effort);
94    }
95    value
96}
97
98pub fn output_format(api: Api, parameters: &mut serde_json::Value, schema: serde_json::Value) {
99    if let Some(api) = api.http() {
100        artifactize_provider::llm::Client::output_format(
101            api,
102            parameters,
103            "artifactize_verdict",
104            schema,
105        );
106    } else {
107        parameters["jsonSchema"] = schema;
108    }
109}