Skip to main content

vtcode_llm/
http_client.rs

1//! Centralized HTTP client factory for LLM providers.
2//!
3//! Provides consistent timeout configuration and client creation
4//! across all providers based on the application's TimeoutsConfig.
5
6use reqwest::Client;
7use std::time::Duration;
8
9use vtcode_config::TimeoutsConfig;
10
11/// Factory for creating pre-configured HTTP clients.
12pub struct HttpClientFactory;
13
14impl HttpClientFactory {
15    /// Create an HTTP client for standard LLM API requests.
16    ///
17    /// The request budget comes from `default_ceiling_seconds`; the connect phase
18    /// comes from `http_connect_seconds`, so a slow or high-latency link can be
19    /// tuned independently of how long a completion may run.
20    pub fn for_llm(config: &TimeoutsConfig) -> Client {
21        vtcode_commons::http::create_client_with_timeouts(Self::connect_timeout(config), Self::llm_timeout(config))
22    }
23
24    pub(crate) fn for_llm_without_redirects(config: &TimeoutsConfig) -> Result<Client, reqwest::Error> {
25        vtcode_commons::http::create_client_without_redirects(Self::connect_timeout(config), Self::llm_timeout(config))
26    }
27
28    fn llm_timeout(config: &TimeoutsConfig) -> Duration {
29        let timeout_secs = if config.default_ceiling_seconds > 0 {
30            config.default_ceiling_seconds
31        } else {
32            180 // Fallback if disabled
33        };
34
35        Duration::from_secs(timeout_secs)
36    }
37
38    /// Connect budget from `[timeouts] http_connect_seconds`, falling back to the
39    /// historical 30s when the key is set to `0` (disabled), because reqwest needs
40    /// a concrete duration at this call site.
41    fn connect_timeout(config: &TimeoutsConfig) -> Duration {
42        match config.ceiling_duration(config.http_connect_seconds) {
43            Some(connect) => connect,
44            None => Duration::from_secs(30),
45        }
46    }
47
48    /// Create an HTTP client optimized for streaming requests.
49    ///
50    /// Uses the `streaming_ceiling_seconds` from TimeoutsConfig.
51    /// Streaming requests typically take longer as they wait for incremental output.
52    pub fn for_streaming(config: &TimeoutsConfig) -> Client {
53        let timeout_secs = if config.streaming_ceiling_seconds > 0 {
54            config.streaming_ceiling_seconds
55        } else {
56            600 // 10 minutes fallback for streaming
57        };
58
59        Client::builder()
60            .timeout(Duration::from_secs(timeout_secs))
61            .connect_timeout(Self::connect_timeout(config))
62            .pool_idle_timeout(Some(Duration::from_secs(90)))
63            .pool_max_idle_per_host(1)
64            .build()
65            .unwrap_or_else(|_| Client::new())
66    }
67
68    /// Create an HTTP client with explicit timeout values.
69    ///
70    /// Use this when providers need custom timeout handling.
71    pub(crate) fn with_timeouts(request_timeout: Duration, connect_timeout: Duration) -> Client {
72        vtcode_commons::http::create_client_with_timeouts(connect_timeout, request_timeout)
73    }
74
75    /// Create a default HTTP client with reasonable defaults.
76    ///
77    /// Uses 180s request timeout and 30s connect timeout.
78    pub fn default_client() -> Client {
79        vtcode_commons::http::create_client_with_timeouts(Duration::from_secs(30), Duration::from_secs(180))
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn default_client_is_created() {
89        let client = HttpClientFactory::default_client();
90        // Just verify it doesn't panic
91        drop(client);
92    }
93
94    #[test]
95    fn for_llm_uses_config_timeout() {
96        let config = TimeoutsConfig::default();
97        let client = HttpClientFactory::for_llm(&config);
98        drop(client);
99    }
100
101    #[test]
102    fn for_streaming_uses_longer_timeout() {
103        let config = TimeoutsConfig::default();
104        let client = HttpClientFactory::for_streaming(&config);
105        drop(client);
106    }
107
108    #[test]
109    fn connect_timeout_follows_the_configured_http_key() {
110        let config = TimeoutsConfig { http_connect_seconds: 45, ..Default::default() };
111        assert_eq!(HttpClientFactory::connect_timeout(&config), Duration::from_secs(45));
112    }
113
114    #[test]
115    fn connect_timeout_falls_back_when_disabled() {
116        // `0` means "no limit" in `[timeouts]`, but reqwest still needs a
117        // concrete connect duration here, so the historical 30s applies.
118        let config = TimeoutsConfig { http_connect_seconds: 0, ..Default::default() };
119        assert_eq!(HttpClientFactory::connect_timeout(&config), Duration::from_secs(30));
120    }
121}