Skip to main content

vtcode_core/llm/providers/
mistral.rs

1use async_trait::async_trait;
2use reqwest::Client as HttpClient;
3use serde_json::{Map, Value};
4
5use crate::config::TimeoutsConfig;
6use crate::config::constants::{env_vars, models, urls};
7use crate::config::core::{AnthropicConfig, ModelConfig, PromptCachingConfig};
8use crate::llm::error_display;
9use crate::llm::provider::{LLMError, LLMProvider, LLMRequest, LLMResponse, LLMStream};
10
11use super::{
12    common::{
13        ensure_model, extract_prompt_cache_settings_default, impl_llm_client, override_base_url,
14        parse_json_response, parse_response_openai_format, resolve_model,
15        serialize_messages_openai_format, serialize_tools_openai_format,
16        spawn_openai_compatible_stream, validate_supported_models,
17    },
18    error_handling::handle_openai_http_error,
19};
20
21const PROVIDER_NAME: &str = "Mistral";
22const PROVIDER_KEY: &str = "mistral";
23
24pub struct MistralProvider {
25    api_key: String,
26    http_client: HttpClient,
27    base_url: String,
28    model: String,
29    prompt_cache_enabled: bool,
30    model_behavior: Option<ModelConfig>,
31}
32
33impl MistralProvider {
34    pub fn new(api_key: String) -> Self {
35        Self::with_model_internal(
36            api_key,
37            models::mistral::DEFAULT_MODEL.to_string(),
38            None,
39            None,
40            TimeoutsConfig::default(),
41            None,
42        )
43    }
44
45    pub fn with_model(api_key: String, model: String) -> Self {
46        Self::with_model_internal(api_key, model, None, None, TimeoutsConfig::default(), None)
47    }
48
49    pub fn new_with_client(
50        api_key: String,
51        model: String,
52        http_client: reqwest::Client,
53        base_url: String,
54        _timeouts: TimeoutsConfig,
55    ) -> Self {
56        Self {
57            api_key,
58            http_client,
59            base_url,
60            model,
61            prompt_cache_enabled: false,
62            model_behavior: None,
63        }
64    }
65
66    pub fn from_config(
67        api_key: Option<String>,
68        model: Option<String>,
69        base_url: Option<String>,
70        prompt_cache: Option<PromptCachingConfig>,
71        timeouts: Option<TimeoutsConfig>,
72        _anthropic: Option<AnthropicConfig>,
73        model_behavior: Option<ModelConfig>,
74    ) -> Self {
75        let api_key_value = api_key.unwrap_or_default();
76        let model_value = resolve_model(model, models::mistral::DEFAULT_MODEL);
77
78        Self::with_model_internal(
79            api_key_value,
80            model_value,
81            prompt_cache,
82            base_url,
83            timeouts.unwrap_or_default(),
84            model_behavior,
85        )
86    }
87
88    fn with_model_internal(
89        api_key: String,
90        model: String,
91        prompt_cache: Option<PromptCachingConfig>,
92        base_url: Option<String>,
93        timeouts: TimeoutsConfig,
94        model_behavior: Option<ModelConfig>,
95    ) -> Self {
96        use crate::llm::http_client::HttpClientFactory;
97
98        let (prompt_cache_enabled, _) =
99            extract_prompt_cache_settings_default(prompt_cache, "mistral");
100
101        Self {
102            api_key,
103            http_client: HttpClientFactory::for_llm(&timeouts),
104            base_url: override_base_url(
105                urls::MISTRAL_API_BASE,
106                base_url,
107                Some(env_vars::MISTRAL_BASE_URL),
108            ),
109            model,
110            prompt_cache_enabled,
111            model_behavior,
112        }
113    }
114
115    fn convert_to_mistral_format(&self, request: &LLMRequest) -> Result<Value, LLMError> {
116        let mut payload = Map::with_capacity(12);
117
118        let mut messages = self.serialize_messages(request)?;
119
120        // Mistral API does not support a top-level "system" field.
121        // Inject the system prompt as a system-role message at the start.
122        if let Some(system_prompt) = &request.system_prompt {
123            let trimmed = system_prompt.trim();
124            if !trimmed.is_empty() {
125                messages.insert(0, serde_json::json!({"role": "system", "content": trimmed}));
126            }
127        }
128
129        payload.insert("model".to_owned(), Value::String(request.model.clone()));
130        payload.insert("messages".to_owned(), Value::Array(messages));
131
132        if let Some(max_tokens) = request.max_tokens {
133            payload.insert(
134                "max_tokens".to_owned(),
135                Value::Number(serde_json::Number::from(max_tokens as u64)),
136            );
137        }
138
139        if let Some(temperature) = request.temperature {
140            payload.insert(
141                "temperature".to_owned(),
142                Value::Number(super::common::float_to_json_number(temperature)?),
143            );
144        }
145
146        if let Some(top_p) = request.top_p {
147            payload.insert(
148                "top_p".to_owned(),
149                Value::Number(super::common::float_to_json_number(top_p)?),
150            );
151        }
152
153        if request.stream {
154            payload.insert("stream".to_string(), Value::Bool(true));
155            payload.insert(
156                "stream_options".to_string(),
157                serde_json::json!({"include_usage": true}),
158            );
159        }
160
161        if let Some(tools) = &request.tools
162            && let Some(serialized_tools) = serialize_tools_openai_format(tools)
163        {
164            payload.insert("tools".to_string(), Value::Array(serialized_tools));
165            payload.insert("parallel_tool_calls".to_string(), Value::Bool(false));
166        }
167
168        let has_explicit_choice = request.tool_choice.is_some();
169        if let Some(choice) = &request.tool_choice {
170            payload.insert(
171                "tool_choice".to_string(),
172                choice.to_provider_format(PROVIDER_KEY),
173            );
174        }
175        // Mistral's default "auto" tool_choice sometimes causes the model to
176        // emit tool call arguments as plain text content. Setting it explicitly
177        // when tools are present helps the model use structured tool_calls.
178        if !has_explicit_choice && request.tools.as_ref().is_some_and(|t| !t.is_empty()) {
179            payload.insert("tool_choice".to_string(), Value::String("auto".to_owned()));
180        }
181
182        if let Some(effort) = request.reasoning_effort
183            && effort != crate::config::types::ReasoningEffortLevel::None
184        {
185            payload.insert(
186                "reasoning_effort".to_owned(),
187                Value::String("high".to_owned()),
188            );
189        }
190
191        if let Some(meta) = &request.metadata
192            && let Some(user_id) = meta.get("user_id").and_then(|v| v.as_str())
193        {
194            payload.insert("user_id".to_owned(), Value::String(user_id.to_owned()));
195        }
196
197        Ok(Value::Object(payload))
198    }
199
200    async fn send_request(&self, payload: &Value) -> Result<reqwest::Response, LLMError> {
201        let url = format!("{}/chat/completions", self.base_url.trim_end_matches('/'));
202
203        self.http_client
204            .post(&url)
205            .bearer_auth(&self.api_key)
206            .json(payload)
207            .send()
208            .await
209            .map_err(|e| LLMError::Network {
210                message: error_display::format_llm_error(
211                    PROVIDER_NAME,
212                    &format!("network error: {}", e),
213                ),
214                metadata: None,
215            })
216    }
217
218    fn serialize_messages(&self, request: &LLMRequest) -> Result<Vec<Value>, LLMError> {
219        serialize_messages_openai_format(request, PROVIDER_KEY)
220    }
221
222    fn parse_response(&self, response_json: Value, model: String) -> Result<LLMResponse, LLMError> {
223        parse_response_openai_format(
224            response_json,
225            PROVIDER_NAME,
226            model,
227            self.prompt_cache_enabled,
228            None as Option<fn(&Value, &Value) -> Option<String>>,
229        )
230    }
231}
232
233#[async_trait]
234impl LLMProvider for MistralProvider {
235    fn name(&self) -> &str {
236        PROVIDER_KEY
237    }
238
239    fn supports_streaming(&self) -> bool {
240        true
241    }
242
243    fn supports_tools(&self, _model: &str) -> bool {
244        true
245    }
246
247    fn supports_structured_output(&self, _model: &str) -> bool {
248        true
249    }
250
251    fn supports_vision(&self, _model: &str) -> bool {
252        true
253    }
254
255    fn supports_reasoning(&self, model: &str) -> bool {
256        let requested = if model.trim().is_empty() {
257            &self.model
258        } else {
259            model
260        };
261
262        self.model_behavior
263            .as_ref()
264            .and_then(|b| b.model_supports_reasoning)
265            .unwrap_or(false)
266            || requested == models::mistral::MISTRAL_LARGE_3
267    }
268
269    fn supports_reasoning_effort(&self, _model: &str) -> bool {
270        self.model_behavior
271            .as_ref()
272            .and_then(|b| b.model_supports_reasoning_effort)
273            .unwrap_or(false)
274    }
275
276    fn effective_context_size(&self, _model: &str) -> usize {
277        256_000
278    }
279
280    async fn generate(&self, mut request: LLMRequest) -> Result<LLMResponse, LLMError> {
281        let model = ensure_model(&mut request, &self.model);
282
283        let payload = self.convert_to_mistral_format(&request)?;
284        let response = self.send_request(&payload).await?;
285        let response = handle_openai_http_error(response, PROVIDER_NAME, "MISTRAL_API_KEY").await?;
286
287        let response_json = parse_json_response(response, PROVIDER_NAME).await?;
288        self.parse_response(response_json, model)
289    }
290
291    async fn stream(&self, mut request: LLMRequest) -> Result<LLMStream, LLMError> {
292        ensure_model(&mut request, &self.model);
293        self.validate_request(&request)?;
294        request.stream = true;
295        let model = request.model.clone();
296
297        let payload = self.convert_to_mistral_format(&request)?;
298        let response = self.send_request(&payload).await?;
299        let response = handle_openai_http_error(response, PROVIDER_NAME, "MISTRAL_API_KEY").await?;
300
301        Ok(spawn_openai_compatible_stream(
302            response,
303            PROVIDER_NAME,
304            model,
305            Some("reasoning_content"),
306            super::shared::OpenAiDeltaOrder::ContentFirst,
307        ))
308    }
309
310    fn supported_models(&self) -> Vec<String> {
311        models::mistral::SUPPORTED_MODELS
312            .iter()
313            .map(|model| model.to_string())
314            .collect()
315    }
316
317    fn validate_request(&self, request: &LLMRequest) -> Result<(), LLMError> {
318        validate_supported_models(
319            request,
320            PROVIDER_NAME,
321            PROVIDER_KEY,
322            models::mistral::SUPPORTED_MODELS,
323        )
324    }
325}
326
327impl_llm_client!(MistralProvider);