vtcode-core 0.103.3

Core library for VT Code - a Rust-based terminal coding agent
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use super::cgp::{CanBuildProvider, CanDescribeProvider, register_builtin_cgp_providers};
use super::model_resolver::{ModelResolver, heuristic_provider_from_model};
use crate::config::TimeoutsConfig;
use crate::config::core::{AnthropicConfig, ModelConfig, OpenAIConfig, PromptCachingConfig};
use crate::config::models::Provider;
use crate::ctx_err;
use crate::llm::provider::{LLMError, LLMProvider};
use crate::llm::providers::OpenAIProvider;
use crate::llm::providers::openai::CustomProviderAuthHandle;
use hashbrown::HashMap;
use std::path::PathBuf;
use vtcode_config::auth::CopilotAuthConfig;
use vtcode_config::auth::OpenAIChatGptAuthHandle;

type ProviderFactory = Box<dyn Fn(ProviderConfig) -> Box<dyn LLMProvider> + Send + Sync>;

const BUILTIN_PROVIDER_KEYS: &[&str] = &[
    "openai",
    "anthropic",
    "gemini",
    "copilot",
    "deepseek",
    "openrouter",
    "ollama",
    "lmstudio",
    "moonshot",
    "zai",
    "minimax",
    "huggingface",
    "openresponses",
    "opencode-zen",
    "opencode-go",
];

/// LLM provider factory and registry
pub struct LLMFactory {
    providers: HashMap<String, ProviderFactory>,
}

#[derive(Debug, Clone)]
pub struct ProviderConfig {
    pub api_key: Option<String>,
    pub openai_chatgpt_auth: Option<OpenAIChatGptAuthHandle>,
    pub copilot_auth: Option<CopilotAuthConfig>,
    pub base_url: Option<String>,
    pub model: Option<String>,
    pub prompt_cache: Option<PromptCachingConfig>,
    pub timeouts: Option<TimeoutsConfig>,
    pub openai: Option<OpenAIConfig>,
    pub anthropic: Option<AnthropicConfig>,
    pub model_behavior: Option<ModelConfig>,
    pub workspace_root: Option<PathBuf>,
}

impl LLMFactory {
    pub fn new() -> Self {
        let mut factory = Self {
            providers: HashMap::new(),
        };

        register_builtin_cgp_providers(&mut factory);

        factory
    }

    pub fn register_cgp_provider<Ctx>(&mut self)
    where
        Ctx: CanDescribeProvider + CanBuildProvider + 'static,
    {
        self.register_provider(Ctx::PROVIDER_KEY, Ctx::build_provider);
    }

    /// Register a new provider
    pub fn register_provider<F>(&mut self, name: &str, factory_fn: F)
    where
        F: Fn(ProviderConfig) -> Box<dyn LLMProvider> + Send + Sync + 'static,
    {
        self.providers
            .insert(name.to_string(), Box::new(factory_fn));
    }

    /// Create provider instance
    #[allow(clippy::result_large_err)]
    pub fn create_provider(
        &self,
        provider_name: &str,
        config: ProviderConfig,
    ) -> Result<Box<dyn LLMProvider>, LLMError> {
        let factory_fn =
            self.providers
                .get(provider_name)
                .ok_or_else(|| LLMError::InvalidRequest {
                    message: format!("Unknown provider: {}", provider_name),
                    metadata: None,
                })?;

        Ok(factory_fn(config))
    }

    /// List available providers
    pub fn list_providers(&self) -> Vec<String> {
        self.providers.keys().cloned().collect()
    }

    /// Remove a provider registration by name.
    pub fn remove_provider(&mut self, name: &str) {
        self.providers.remove(name);
    }

    /// Determine provider name from model string
    pub fn provider_from_model(&self, model: &str) -> Option<String> {
        heuristic_provider_from_model(model).map(|provider| provider.to_string())
    }
}

/// Infer a [`Provider`] from an optional override and model string.
///
/// Attempts, in order:
/// 1. Parse the override if provided.
/// 2. Parse the model into a [`crate::config::models::ModelId`] and return its provider.
/// 3. Fall back to heuristic detection via [`LLMFactory::provider_from_model`].
pub fn infer_provider(override_provider: Option<&str>, model: &str) -> Option<Provider> {
    ModelResolver::resolve_provider(override_provider, model, &[])
}

impl Default for LLMFactory {
    fn default() -> Self {
        Self::new()
    }
}

use std::sync::{LazyLock, Mutex};

use crate::models_manager::ModelsManager;

static FACTORY: LazyLock<Mutex<LLMFactory>> = LazyLock::new(|| Mutex::new(LLMFactory::new()));

static MODELS_MANAGER: LazyLock<ModelsManager> = LazyLock::new(ModelsManager::new);

/// Get global factory instance
pub fn get_factory() -> &'static Mutex<LLMFactory> {
    &FACTORY
}

/// Get global models manager instance
pub fn get_models_manager() -> &'static ModelsManager {
    &MODELS_MANAGER
}

/// Infer provider from model slug using ModelsManager presets.
///
/// This provides a more accurate provider resolution than heuristic-based
/// `provider_from_model` by checking against known model presets first.
pub fn infer_provider_from_model(model: &str) -> Option<Provider> {
    ModelResolver::resolve_provider(None, model, &[]).or_else(|| {
        let family = crate::models_manager::find_family_for_model(model);
        (family.family != "unknown").then_some(family.provider)
    })
}

/// Create provider from model name and API key
#[allow(clippy::result_large_err)]
pub fn create_provider_for_model(
    model: &str,
    api_key: String,
    prompt_cache: Option<PromptCachingConfig>,
    model_behavior: Option<ModelConfig>,
) -> Result<Box<dyn LLMProvider>, LLMError> {
    // Validate model exists in ModelsManager (non-blocking check using local presets)
    if !get_models_manager().model_exists_sync(model) {
        tracing::warn!(
            model = model,
            "Model not found in ModelsManager presets, proceeding with factory heuristics"
        );
    }

    let provider_name = infer_provider_from_model(model)
        .map(|provider| provider.to_string())
        .ok_or_else(|| LLMError::InvalidRequest {
            message: format!("Cannot determine provider for model: {}", model),
            metadata: None,
        })?;
    let factory = get_factory().lock().map_err(|_| LLMError::Provider {
        message: ctx_err!("llm factory", "lock poisoned"),
        metadata: None,
    })?;

    factory.create_provider(
        &provider_name,
        ProviderConfig {
            api_key: Some(api_key),
            openai_chatgpt_auth: None,
            copilot_auth: None,
            base_url: None,
            model: Some(model.to_string()),
            prompt_cache,
            timeouts: None,
            openai: None,
            anthropic: None,
            model_behavior,
            workspace_root: None,
        },
    )
}

/// Create provider with full configuration
#[allow(clippy::result_large_err)]
pub fn create_provider_with_config(
    provider_name: &str,
    config: ProviderConfig,
) -> Result<Box<dyn LLMProvider>, LLMError> {
    let factory = get_factory().lock().map_err(|_| LLMError::Provider {
        message: ctx_err!("llm factory", "lock poisoned"),
        metadata: None,
    })?;
    factory.create_provider(provider_name, config)
}

/// Register custom OpenAI-compatible providers from config into the global factory.
///
/// This performs a sync/replace: previously registered custom providers are
/// removed first, then the new set is registered. Built-in providers are
/// never touched.
pub fn register_custom_providers(custom_providers: &[vtcode_config::core::CustomProviderConfig]) {
    if custom_providers.is_empty() {
        return;
    }

    let Ok(mut factory) = get_factory().lock() else {
        tracing::error!("Failed to lock LLM factory for custom provider registration");
        return;
    };

    // Remove previously registered custom providers (anything not built-in)
    let registered: Vec<String> = factory.list_providers();
    for key in &registered {
        if !BUILTIN_PROVIDER_KEYS.contains(&key.as_str()) {
            factory.remove_provider(key);
        }
    }

    // Register each custom provider
    for cp in custom_providers {
        if let Err(msg) = cp.validate() {
            tracing::warn!("Skipping invalid custom provider: {msg}");
            continue;
        }

        let key = cp.name.to_lowercase();
        let display_name = cp.display_name.clone();
        let default_base_url = cp.base_url.clone();
        let default_model = cp.model.clone();
        let auth_config = cp.auth.clone();
        let api_key_env = cp.resolved_api_key_env();
        let reg_key = key.clone();

        factory.register_provider(&reg_key, move |config: ProviderConfig| {
            let ProviderConfig {
                api_key,
                base_url,
                model,
                prompt_cache,
                timeouts,
                openai,
                model_behavior,
                workspace_root,
                ..
            } = config;

            let api_key = if auth_config.is_some() {
                None
            } else {
                api_key.or_else(|| std::env::var(&api_key_env).ok())
            };

            let model = model
                .filter(|m| !m.trim().is_empty())
                .unwrap_or_else(|| default_model.clone());

            let base_url = base_url
                .clone()
                .filter(|u| !u.trim().is_empty())
                .unwrap_or_else(|| default_base_url.clone());
            let custom_provider_auth = auth_config
                .clone()
                .map(|auth| CustomProviderAuthHandle::new(auth, workspace_root.clone()));

            Box::new(OpenAIProvider::from_custom_config(
                key.clone(),
                display_name.clone(),
                api_key,
                Some(model),
                Some(base_url),
                prompt_cache,
                timeouts,
                openai,
                model_behavior,
                custom_provider_auth,
            ))
        });

        tracing::trace!(
            provider = cp.name,
            display_name = cp.display_name,
            "Registered custom OpenAI-compatible provider"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::core::CustomProviderConfig;
    use crate::config::core::{AnthropicConfig, OpenAIConfig};
    use crate::llm::provider_config::{
        AnthropicProviderConfig, GeminiProviderConfig, OpenAIProviderConfig,
    };
    use crate::llm::providers::OllamaProvider;

    #[test]
    fn builtin_cgp_registration_exposes_expected_provider_keys() {
        let factory = LLMFactory::new();
        let mut providers = factory.list_providers();
        providers.sort();

        assert_eq!(
            providers,
            vec![
                "anthropic",
                "copilot",
                "deepseek",
                "gemini",
                "huggingface",
                "lmstudio",
                "minimax",
                "moonshot",
                "ollama",
                "openai",
                "opencode-go",
                "opencode-zen",
                "openresponses",
                "openrouter",
                "zai",
            ]
        );
    }

    #[test]
    fn standard_provider_builds_through_cgp_registration() {
        let factory = LLMFactory::new();
        let provider = factory
            .create_provider(
                <GeminiProviderConfig as CanDescribeProvider>::PROVIDER_KEY,
                ProviderConfig {
                    api_key: Some("test-key".to_string()),
                    openai_chatgpt_auth: None,
                    copilot_auth: None,
                    base_url: None,
                    model: Some(
                        crate::config::constants::models::google::GEMINI_3_FLASH_PREVIEW
                            .to_string(),
                    ),
                    prompt_cache: None,
                    timeouts: None,
                    openai: None,
                    anthropic: None,
                    model_behavior: None,
                    workspace_root: None,
                },
            )
            .expect("built-in cgp registration should build");

        assert_eq!(provider.name(), "gemini");
    }

    #[test]
    fn openai_build_preserves_provider_specific_config_path() {
        let factory = LLMFactory::new();
        let provider = factory
            .create_provider(
                <OpenAIProviderConfig as CanDescribeProvider>::PROVIDER_KEY,
                ProviderConfig {
                    api_key: Some("test-key".to_string()),
                    openai_chatgpt_auth: None,
                    copilot_auth: None,
                    base_url: None,
                    model: Some(
                        crate::config::constants::models::openai::DEFAULT_MODEL.to_string(),
                    ),
                    prompt_cache: None,
                    timeouts: None,
                    openai: Some(OpenAIConfig {
                        websocket_mode: true,
                        ..OpenAIConfig::default()
                    }),
                    anthropic: Some(AnthropicConfig::default()),
                    model_behavior: None,
                    workspace_root: None,
                },
            )
            .expect("openai cgp registration should build");

        assert_eq!(provider.name(), "openai");
    }

    #[test]
    fn anthropic_build_preserves_provider_specific_config_path() {
        let factory = LLMFactory::new();
        let provider = factory
            .create_provider(
                <AnthropicProviderConfig as CanDescribeProvider>::PROVIDER_KEY,
                ProviderConfig {
                    api_key: Some("test-key".to_string()),
                    openai_chatgpt_auth: None,
                    copilot_auth: None,
                    base_url: None,
                    model: Some(
                        crate::config::constants::models::anthropic::DEFAULT_MODEL.to_string(),
                    ),
                    prompt_cache: None,
                    timeouts: None,
                    openai: None,
                    anthropic: Some(AnthropicConfig {
                        count_tokens_enabled: true,
                        ..AnthropicConfig::default()
                    }),
                    model_behavior: None,
                    workspace_root: None,
                },
            )
            .expect("anthropic cgp registration should build");

        assert_eq!(provider.name(), "anthropic");
    }

    #[test]
    fn custom_provider_registration_still_coexists_with_cgp_builtins() {
        let mut factory = LLMFactory::new();
        factory.register_provider("custom-test", |_config| {
            Box::new(OllamaProvider::from_config(
                None,
                Some("gpt-oss:20b".to_string()),
                Some("http://localhost:11434".to_string()),
                None,
                None,
                None,
                None,
            ))
        });

        let custom = factory
            .create_provider(
                "custom-test",
                ProviderConfig {
                    api_key: None,
                    openai_chatgpt_auth: None,
                    copilot_auth: None,
                    base_url: None,
                    model: None,
                    prompt_cache: None,
                    timeouts: None,
                    openai: None,
                    anthropic: None,
                    model_behavior: None,
                    workspace_root: None,
                },
            )
            .expect("custom provider should still register");
        let builtin = factory
            .create_provider(
                "openai",
                ProviderConfig {
                    api_key: Some("test-key".to_string()),
                    openai_chatgpt_auth: None,
                    copilot_auth: None,
                    base_url: None,
                    model: Some(
                        crate::config::constants::models::openai::DEFAULT_MODEL.to_string(),
                    ),
                    prompt_cache: None,
                    timeouts: None,
                    openai: None,
                    anthropic: None,
                    model_behavior: None,
                    workspace_root: None,
                },
            )
            .expect("builtin provider should still build");

        assert_eq!(custom.name(), "ollama");
        assert_eq!(builtin.name(), "openai");
    }

    #[test]
    fn custom_openai_compatible_provider_uses_configured_display_name() {
        register_custom_providers(&[CustomProviderConfig {
            name: "mycorp".to_string(),
            display_name: "MyCorporateName".to_string(),
            base_url: "https://llm.corp.example/v1".to_string(),
            api_key_env: "MYCORP_API_KEY".to_string(),
            auth: None,
            model: "gpt-5-mini".to_string(),
        }]);

        let provider = create_provider_with_config(
            "mycorp",
            ProviderConfig {
                api_key: None,
                openai_chatgpt_auth: None,
                copilot_auth: None,
                base_url: None,
                model: Some("gpt-5-mini".to_string()),
                prompt_cache: None,
                timeouts: None,
                openai: Some(OpenAIConfig::default()),
                anthropic: None,
                model_behavior: None,
                workspace_root: None,
            },
        )
        .expect("custom provider should register");

        assert_eq!(provider.name(), "mycorp");
        assert_eq!(provider.supported_models(), vec!["gpt-5-mini".to_string()]);

        if let Ok(mut factory) = get_factory().lock() {
            factory.remove_provider("mycorp");
        }
    }

    #[test]
    fn create_provider_for_bare_minimax_model_uses_minimax_provider() {
        let provider =
            create_provider_for_model("MiniMax-M2.5", "test-key".to_string(), None, None)
                .expect("bare minimax model should resolve to minimax provider");

        assert_eq!(provider.name(), "minimax");
    }

    #[test]
    fn create_provider_for_mistral_model_uses_openrouter_provider() {
        let provider =
            create_provider_for_model("mistral-large", "test-key".to_string(), None, None)
                .expect("mistral models should still resolve through openrouter");

        assert_eq!(provider.name(), "openrouter");
    }

    #[test]
    fn create_provider_for_openai_repo_id_uses_openrouter_provider() {
        let provider =
            create_provider_for_model("openai/gpt-oss-20b", "test-key".to_string(), None, None)
                .expect("repo identifiers should preserve openrouter routing");

        assert_eq!(provider.name(), "openrouter");
    }

    #[test]
    fn create_provider_for_unknown_model_returns_error() {
        match create_provider_for_model("totally-unknown-model", "test-key".to_string(), None, None)
        {
            Err(LLMError::InvalidRequest { .. }) => {}
            Err(error) => panic!("expected invalid request error, got {error:?}"),
            Ok(_) => panic!("unknown models should remain rejected"),
        }
    }
}