vtcode-llm 0.179.1

LLM provider abstraction, client implementations, and streaming for VT Code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#![allow(
    clippy::collapsible_if,
    reason = "Intentional compatibility, platform, or test-only suppression."
)]

use tracing::warn;

use crate::error_display;
use crate::provider::{LLMError, LLMRequest, Message, MessageRole, ToolChoice};
use crate::providers::common::{chat_completions_url, extract_prompt_cache_settings, override_base_url, resolve_model};
use crate::providers::error_handling::format_network_error;
use reqwest::{Client as HttpClient, Response, StatusCode};
use serde_json::Value;
use std::borrow::Cow;
use std::str::FromStr;
use vtcode_commons::sanitizer::sanitize_provider_diagnostic;
use vtcode_config::TimeoutsConfig;
use vtcode_config::constants::{env_vars, models, urls};
use vtcode_config::core::{
    AnthropicConfig, ModelConfig, OpenRouterAttributionConfig, OpenRouterConfig, OpenRouterPromptCacheSettings,
    PromptCachingConfig,
};
use vtcode_config::models::ModelId;

const OPENROUTER_CATEGORIES: &str = OpenRouterAttributionConfig::CATEGORIES;

/// OpenRouter sticky-routing session identity: body `session_id` (docs: body wins)
/// paired with `x-session-id` at dispatch. Blank lineage omits both.
fn inject_openrouter_session_identity(payload: &mut Value, session_id: Option<&str>) {
    if let Some(session_id) = session_id.filter(|s| !s.trim().is_empty())
        && let Some(map) = payload.as_object_mut()
    {
        map.insert("session_id".to_owned(), Value::String(session_id.to_owned()));
    }
}

mod client_impl;
mod parsing;
mod provider_impl;
#[cfg(test)]
mod tests;

pub struct OpenRouterProvider {
    api_key: String,
    http_client: HttpClient,
    base_url: String,
    model: String,
    prompt_cache_enabled: bool,
    prompt_cache_settings: OpenRouterPromptCacheSettings,
    model_behavior: Option<ModelConfig>,
    attribution: OpenRouterAttributionConfig,
}

impl OpenRouterProvider {
    pub fn new(api_key: String) -> Self {
        Self::with_model_internal(
            api_key,
            models::openrouter::DEFAULT_MODEL.to_string(),
            None,
            None,
            TimeoutsConfig::default(),
            None,
            OpenRouterAttributionConfig::default(),
        )
    }

    fn with_model(api_key: String, model: String) -> Self {
        Self::with_model_internal(
            api_key,
            model,
            None,
            None,
            TimeoutsConfig::default(),
            None,
            OpenRouterAttributionConfig::default(),
        )
    }

    fn new_with_client(
        api_key: String,
        model: String,
        http_client: reqwest::Client,
        base_url: String,
        _timeouts: TimeoutsConfig,
    ) -> Self {
        Self {
            api_key,
            http_client,
            base_url,
            model,
            prompt_cache_enabled: false,
            prompt_cache_settings: OpenRouterPromptCacheSettings::default(),
            model_behavior: None,
            attribution: OpenRouterAttributionConfig::default(),
        }
    }

    pub fn from_config(
        api_key: Option<String>,
        model: Option<String>,
        base_url: Option<String>,
        prompt_cache: Option<PromptCachingConfig>,
        timeouts: Option<TimeoutsConfig>,
        _anthropic: Option<AnthropicConfig>,
        model_behavior: Option<ModelConfig>,
    ) -> Self {
        Self::from_config_with_attribution(
            api_key,
            model,
            base_url,
            prompt_cache,
            timeouts,
            _anthropic,
            model_behavior,
            None,
        )
    }

    pub fn from_config_with_attribution(
        api_key: Option<String>,
        model: Option<String>,
        base_url: Option<String>,
        prompt_cache: Option<PromptCachingConfig>,
        timeouts: Option<TimeoutsConfig>,
        _anthropic: Option<AnthropicConfig>,
        model_behavior: Option<ModelConfig>,
        openrouter: Option<OpenRouterConfig>,
    ) -> Self {
        let api_key_value = api_key.unwrap_or_default();
        let model_value = resolve_model(model, models::openrouter::DEFAULT_MODEL);

        Self::with_model_internal(
            api_key_value,
            model_value,
            prompt_cache,
            base_url,
            timeouts.unwrap_or_default(),
            model_behavior,
            openrouter.map(|c| c.attribution).unwrap_or_default(),
        )
    }

    fn with_model_internal(
        api_key: String,
        model: String,
        prompt_cache: Option<PromptCachingConfig>,
        base_url: Option<String>,
        timeouts: TimeoutsConfig,
        model_behavior: Option<ModelConfig>,
        attribution: OpenRouterAttributionConfig,
    ) -> Self {
        use crate::http_client::HttpClientFactory;
        let (prompt_cache_enabled, prompt_cache_settings) = extract_prompt_cache_settings(
            prompt_cache,
            |p| &p.openrouter,
            |cfg, settings| cfg.enabled && settings.enabled,
        );

        Self {
            api_key,
            http_client: HttpClientFactory::for_llm(&timeouts),
            base_url: override_base_url(urls::OPENROUTER_API_BASE, base_url, Some(env_vars::OPENROUTER_BASE_URL)),
            model,
            prompt_cache_enabled,
            prompt_cache_settings,
            model_behavior,
            attribution,
        }
    }

    pub(super) fn resolve_model<'a>(&'a self, request: &'a LLMRequest) -> &'a str {
        if request.model.trim().is_empty() {
            self.model.as_str()
        } else {
            request.model.as_str()
        }
    }

    fn request_includes_tools(request: &LLMRequest) -> bool {
        request.tools.as_ref().map(|tools| !tools.is_empty()).unwrap_or(false)
    }

    fn enforce_tool_capabilities<'a>(&'a self, request: &'a LLMRequest) -> Cow<'a, LLMRequest> {
        let resolved_model = self.resolve_model(request);
        let tools_requested = Self::request_includes_tools(request);
        let tool_restricted = if let Ok(model_id) = ModelId::from_str(resolved_model) {
            !model_id.supports_tool_calls()
        } else {
            models::openrouter::TOOL_UNAVAILABLE_MODELS.contains(&resolved_model)
        };

        if tools_requested && tool_restricted {
            Cow::Owned(Self::tool_free_request(request))
        } else {
            Cow::Borrowed(request)
        }
    }

    fn tool_free_request(original: &LLMRequest) -> LLMRequest {
        let mut sanitized = original.clone();
        sanitized.tools = None;
        sanitized.tool_choice = Some(ToolChoice::None);
        sanitized.parallel_tool_calls = None;
        sanitized.parallel_tool_config = None;

        let mut normalized_messages: Vec<Message> = Vec::with_capacity(original.messages.len());

        for message in original.messages.iter() {
            match message.role {
                MessageRole::Assistant => {
                    let mut cleaned = message.clone();
                    cleaned.tool_calls = None;
                    cleaned.tool_call_id = None;

                    let content_text = cleaned.content.as_text();
                    let has_content = !content_text.trim().is_empty();
                    if has_content || cleaned.reasoning.is_some() {
                        normalized_messages.push(cleaned);
                    }
                }
                MessageRole::Tool => {
                    let content_text = message.content.as_text();
                    if content_text.trim().is_empty() {
                        continue;
                    }

                    let mut converted = Message::user(content_text.into_owned());
                    converted.reasoning = message.reasoning.clone();
                    normalized_messages.push(converted);
                }
                _ => {
                    normalized_messages.push(message.clone());
                }
            }
        }

        sanitized.messages = std::sync::Arc::new(normalized_messages);
        sanitized
    }

    fn request_includes_images(request: &LLMRequest) -> bool {
        request.messages.iter().any(|msg| msg.content.has_images())
    }

    fn image_free_request(original: &LLMRequest) -> LLMRequest {
        let mut sanitized = original.clone();
        for message in std::sync::Arc::make_mut(&mut sanitized.messages) {
            if let Some(text_only) = message.content.without_images() {
                message.content = text_only;
            }
        }
        sanitized
    }

    /// Retry a request with a fallback payload. Returns `Ok(Some(response))` on
    /// success, `Err` on rate-limit, and `Ok(None)` when the fallback also fails
    /// (caller assembles the combined error).
    async fn retry_with_fallback(
        &self,
        original_status: StatusCode,
        original_error: &str,
        fallback_request: &LLMRequest,
        stream_override: Option<bool>,
        label: &str,
    ) -> Result<Option<Response>, LLMError> {
        let (mut fallback_payload, fallback_url) = self.build_provider_payload(fallback_request)?;
        if let Some(stream_flag) = stream_override {
            fallback_payload["stream"] = Value::Bool(stream_flag);
        }
        let fallback_session_id = crate::providers::shared::session_lineage_from_prompt_cache_key(
            fallback_request.prompt_cache_key.as_deref(),
        );
        inject_openrouter_session_identity(&mut fallback_payload, fallback_session_id.as_deref());

        let fallback_response = self
            .dispatch_request_with_session(&fallback_url, &fallback_payload, fallback_session_id.as_deref())
            .await?;
        if fallback_response.status().is_success() {
            return Ok(Some(fallback_response));
        }

        let fallback_status = fallback_response.status();
        let fallback_text = crate::providers::common::read_provider_error_body(fallback_response).await;

        if fallback_status.as_u16() == 429 || fallback_text.contains("quota") {
            return Err(LLMError::RateLimit { metadata: None });
        }

        let combined_error = format!(
            "HTTP {original_status}: {} | {label} fallback failed with HTTP {fallback_status}: {}",
            sanitize_provider_diagnostic(original_error.as_bytes()),
            sanitize_provider_diagnostic(fallback_text.as_bytes()),
        );
        let formatted_error = error_display::format_llm_error("OpenRouter", &combined_error);
        Err(LLMError::Provider { message: formatted_error, metadata: None })
    }

    /// Attempt a feature-specific fallback when the provider rejects a request.
    ///
    /// Returns `Ok(Some(response))` if the fallback succeeds, `Ok(None)` if the
    /// condition doesn't match (caller should try the next fallback), and `Err`
    /// for rate-limit or combined error failures.
    #[allow(
        clippy::too_many_arguments,
        reason = "Intentional compatibility, platform, or test-only suppression."
    )] // fallback dispatcher, all params needed
    async fn try_feature_fallback(
        &self,
        request: &LLMRequest,
        status: StatusCode,
        error_text: &str,
        stream_override: Option<bool>,
        has_feature: fn(&LLMRequest) -> bool,
        error_match: &str,
        warn_message: &str,
        strip_feature: fn(&LLMRequest) -> LLMRequest,
        label: &str,
    ) -> Result<Option<Response>, LLMError> {
        if has_feature(request) && status == StatusCode::NOT_FOUND && error_text.contains(error_match) {
            warn!("{}", warn_message);
            let fallback_request = strip_feature(request);
            return self
                .retry_with_fallback(status, error_text, &fallback_request, stream_override, label)
                .await;
        }
        Ok(None)
    }

    fn build_provider_payload(&self, request: &LLMRequest) -> Result<(Value, String), LLMError> {
        Ok((self.convert_to_openrouter_format(request)?, chat_completions_url(&self.base_url)))
    }

    async fn dispatch_request(&self, url: &str, payload: &Value) -> Result<Response, LLMError> {
        self.dispatch_request_with_session(url, payload, None).await
    }

    async fn dispatch_request_with_session(
        &self,
        url: &str,
        payload: &Value,
        session_id: Option<&str>,
    ) -> Result<Response, LLMError> {
        let mut req = self.http_client.post(url).bearer_auth(&self.api_key);
        // Opt-in app attribution per https://openrouter.ai/docs/app-attribution.
        // No headers are sent unless explicitly enabled via
        // `[provider.openrouter.attribution]` or `VTCODE_OPENROUTER_ATTRIBUTION=1`.
        if self.attribution.is_enabled() {
            req = req
                .header("HTTP-Referer", self.attribution.resolved_referer().to_string())
                .header("X-OpenRouter-Title", self.attribution.resolved_title().to_string())
                .header("X-OpenRouter-Categories", OPENROUTER_CATEGORIES);
        }
        if let Some(session_id) = session_id.filter(|s| !s.trim().is_empty()) {
            req = req.header("x-session-id", session_id);
        }
        req.json(payload)
            .send()
            .await
            .map_err(|e| format_network_error("OpenRouter", &e))
    }

    async fn send_with_fallback(
        &self,
        request: &LLMRequest,
        stream_override: Option<bool>,
    ) -> Result<Response, LLMError> {
        let adjusted_request = self.enforce_tool_capabilities(request);
        let request_ref = adjusted_request.as_ref();

        let (mut payload, url) = self.build_provider_payload(request_ref)?;
        if let Some(stream_flag) = stream_override {
            payload["stream"] = Value::Bool(stream_flag);
        }
        let session_id =
            crate::providers::shared::session_lineage_from_prompt_cache_key(request_ref.prompt_cache_key.as_deref());
        inject_openrouter_session_identity(&mut payload, session_id.as_deref());

        let response = self
            .dispatch_request_with_session(&url, &payload, session_id.as_deref())
            .await?;
        if response.status().is_success() {
            return Ok(response);
        }

        let status = response.status();
        let error_text = crate::providers::common::read_provider_error_body(response).await;

        if status.as_u16() == 429 || error_text.contains("quota") {
            return Err(LLMError::RateLimit { metadata: None });
        }

        if let Some(resp) = self
            .try_feature_fallback(
                request_ref,
                status,
                &error_text,
                stream_override,
                Self::request_includes_tools,
                "No endpoints found that support tool use",
                "OpenRouter endpoint does not support tool use; retrying without tools",
                Self::tool_free_request,
                "Tool",
            )
            .await?
        {
            return Ok(resp);
        }

        if let Some(resp) = self
            .try_feature_fallback(
                request_ref,
                status,
                &error_text,
                stream_override,
                Self::request_includes_images,
                "No endpoints found that support image input",
                "OpenRouter endpoint does not support image input; retrying without images",
                Self::image_free_request,
                "Image",
            )
            .await?
        {
            return Ok(resp);
        }

        // Use unified error parsing for consistent error categorization
        use crate::providers::error_handling::parse_api_error;
        Err(parse_api_error("OpenRouter", status, &error_text))
    }
}