vtcode-config 0.173.0

Config loader components shared across VT Code and downstream adopters
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::str::FromStr;

use crate::acp::AgentClientProtocolConfig;
use crate::codex::{FileOpener, HistoryConfig, TuiConfig};
use crate::constants::defaults::DEFAULT_PRIMARY_AGENT_NAME;
use crate::context::ContextFeaturesConfig;
use crate::core::{
    AgentConfig, AgentPermissionsConfig, AnthropicConfig, AuthConfig, AutomationConfig, CommandsConfig,
    CustomProviderConfig, DotfileProtectionConfig, ModelConfig, OpenAIConfig, PermissionsConfig, PromptCachingConfig,
    ProviderOverrideConfig, SandboxConfig, SecurityConfig, SkillsConfig, ToolsConfig,
};
use crate::debug::DebugConfig;
use crate::hooks::HooksConfig;
use crate::mcp::McpClientConfig;
use crate::models::{MiMoAuthMethod, Provider};
use crate::optimization::OptimizationConfig;
use crate::output_styles::OutputStyleConfig;
use crate::root::{ChatConfig, PtyConfig, UiConfig};
use crate::subagents::SubagentRuntimeLimits;
use crate::telemetry::TelemetryConfig;
use crate::timeouts::TimeoutsConfig;
use crate::webmcp::WebmcpConfig;

use crate::loader::syntax_highlighting::SyntaxHighlightingConfig;

/// Provider-specific configuration
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct ProviderConfig {
    /// OpenAI provider configuration
    #[serde(default)]
    pub openai: OpenAIConfig,

    /// Anthropic provider configuration
    #[serde(default)]
    pub anthropic: AnthropicConfig,

    /// Xiaomi MiMo auth method: "payg" or "token-plan"
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mimo_auth_method: Option<MiMoAuthMethod>,
}

/// Codex-compatible top-level feature flags.
///
/// Maps to `[features]` in `vtcode.toml`.
/// When `memories` is true, VT Code can carry useful context from earlier
/// threads into future work via the persistent memory subsystem.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct FeaturesConfig {
    /// Master toggle for the memories subsystem.
    /// When true, VT Code extracts durable context from completed threads
    /// and injects it into future sessions.
    #[serde(default)]
    pub memories: bool,
}

/// Workspace-level configuration controls.
///
/// Maps to `[workspace]` in `vtcode.toml`.
/// When `use_root_config` is true, only the workspace root `vtcode.toml`
/// is used as the active config layer (system, user, project, and
/// dot-dir layers are discarded).
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct WorkspaceConfig {
    /// When true, force the workspace root `vtcode.toml` as the sole
    /// active config layer, discarding system, user, project, and
    /// dot-dir layers.
    #[serde(default)]
    pub(crate) use_root_config: bool,

    /// Include workspace context in messages.
    #[serde(default = "default_true")]
    pub(crate) include_context: bool,

    /// Maximum size of workspace context to include (in bytes).
    #[serde(default)]
    pub(crate) max_context_size: Option<usize>,
}

impl Default for WorkspaceConfig {
    fn default() -> Self {
        Self {
            use_root_config: false,
            include_context: true,
            max_context_size: None,
        }
    }
}

fn default_true() -> bool {
    true
}

/// Main configuration structure for VT Code
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VTCodeConfig {
    /// Primary agent selected at startup when no session override is active.
    #[serde(default = "default_primary_agent")]
    pub default_primary_agent: String,

    /// Codex-compatible top-level feature flags (`[features]` table).
    #[serde(default)]
    pub features: FeaturesConfig,

    /// Codex-compatible clickable citation URI scheme.
    #[serde(default)]
    pub file_opener: FileOpener,

    /// External notification command invoked for supported events.
    #[serde(default)]
    pub notify: Vec<String>,

    /// Codex-compatible local history persistence controls.
    #[serde(default)]
    pub history: HistoryConfig,

    /// Codex-compatible TUI settings.
    #[serde(default)]
    pub tui: TuiConfig,

    /// Agent-wide settings
    #[serde(default)]
    pub agent: AgentConfig,

    /// Authentication configuration for OAuth flows
    #[serde(default)]
    pub auth: AuthConfig,

    /// Tool execution policies
    #[serde(default)]
    pub tools: ToolsConfig,

    /// Unix command permissions
    #[serde(default)]
    pub commands: CommandsConfig,

    /// Permission system settings (resolution, audit logging, caching)
    #[serde(default)]
    pub permissions: PermissionsConfig,

    /// Runtime-only agent permission policy supplied by derived child agents.
    #[serde(skip)]
    #[cfg_attr(feature = "schema", schemars(skip))]
    pub runtime_agent_permissions: Option<AgentPermissionsConfig>,

    /// Runtime-only snapshot of lifecycle hook commands sourced from
    /// workspace-controlled configuration layers, populated by `ConfigManager`
    /// at load time. Workspace-controlled hooks require explicit user approval
    /// before execution; see [`WorkspaceLifecycleHooks`](crate::hooks::WorkspaceLifecycleHooks).
    #[serde(skip)]
    #[cfg_attr(feature = "schema", schemars(skip))]
    pub workspace_lifecycle_hooks: Option<crate::hooks::WorkspaceLifecycleHooks>,

    /// Security settings
    #[serde(default)]
    pub security: SecurityConfig,

    /// Sandbox settings for command execution isolation
    #[serde(default)]
    pub sandbox: SandboxConfig,

    /// UI settings
    #[serde(default)]
    pub ui: UiConfig,

    /// Chat settings
    #[serde(default)]
    pub chat: ChatConfig,

    /// PTY settings
    #[serde(default)]
    pub pty: PtyConfig,

    /// Debug and tracing settings
    #[serde(default)]
    pub debug: DebugConfig,

    /// Context features (e.g., Decision Ledger)
    #[serde(default)]
    pub context: ContextFeaturesConfig,

    /// Telemetry configuration (logging, trajectory)
    #[serde(default)]
    pub telemetry: TelemetryConfig,

    /// Performance optimization settings
    #[serde(default)]
    pub optimization: OptimizationConfig,

    /// Syntax highlighting configuration
    #[serde(default)]
    pub syntax_highlighting: SyntaxHighlightingConfig,

    /// Timeout ceilings and UI warning thresholds
    #[serde(default)]
    pub timeouts: TimeoutsConfig,

    /// Automation configuration
    #[serde(default)]
    pub automation: AutomationConfig,

    /// Subagent runtime configuration
    #[serde(default)]
    pub subagents: SubagentRuntimeLimits,

    /// Prompt cache configuration (local + provider integration)
    #[serde(default)]
    pub prompt_cache: PromptCachingConfig,

    /// Model Context Protocol configuration
    #[serde(default)]
    pub mcp: McpClientConfig,

    /// Authenticated browser editor bridge configuration
    #[serde(default)]
    pub webmcp: WebmcpConfig,

    /// Agent Client Protocol configuration
    #[serde(default)]
    pub acp: AgentClientProtocolConfig,

    /// Lifecycle hooks configuration
    #[serde(default)]
    pub hooks: HooksConfig,

    /// Model-specific behavior configuration
    #[serde(default)]
    pub model: ModelConfig,

    /// Provider-specific configuration
    #[serde(default)]
    pub provider: ProviderConfig,

    /// Skills system configuration (Agent Skills spec)
    #[serde(default)]
    pub skills: SkillsConfig,

    /// Extra OpenAI-compatible endpoints for the model picker.
    /// Define in user or system config only.
    #[serde(default)]
    pub custom_providers: Vec<CustomProviderConfig>,

    /// Built-in provider overrides for model lists and endpoint configuration.
    ///
    /// Maps provider key (e.g., "opencode-zen", "opencode-go") to override
    /// config that extends the provider's hardcoded model list with custom
    /// models, and optionally overrides the base URL or API key env var.
    #[serde(default)]
    pub provider_overrides: BTreeMap<String, ProviderOverrideConfig>,

    /// Restrict which providers may be used.
    ///
    /// When non-empty, only providers listed here are visible in the model
    /// picker, selectable at first-run, and instantiable at runtime.  Empty
    /// (the default) means all built-in and custom providers are available.
    #[serde(default)]
    pub providers_whitelist: Vec<String>,

    /// Output style configuration
    #[serde(default)]
    pub output_style: OutputStyleConfig,

    /// Dotfile protection configuration
    #[serde(default)]
    pub dotfile_protection: DotfileProtectionConfig,

    /// Workspace-level configuration controls
    #[serde(default)]
    pub workspace: WorkspaceConfig,
}

impl Default for VTCodeConfig {
    fn default() -> Self {
        Self {
            default_primary_agent: default_primary_agent(),
            features: FeaturesConfig::default(),
            file_opener: FileOpener::default(),
            notify: Vec::new(),
            history: HistoryConfig::default(),
            tui: TuiConfig::default(),
            agent: AgentConfig::default(),
            auth: AuthConfig::default(),
            tools: ToolsConfig::default(),
            commands: CommandsConfig::default(),
            permissions: PermissionsConfig::default(),
            runtime_agent_permissions: None,
            workspace_lifecycle_hooks: None,
            security: SecurityConfig::default(),
            sandbox: SandboxConfig::default(),
            ui: UiConfig::default(),
            chat: ChatConfig::default(),
            pty: PtyConfig::default(),
            debug: DebugConfig::default(),
            context: ContextFeaturesConfig::default(),
            telemetry: TelemetryConfig::default(),
            optimization: OptimizationConfig::default(),
            syntax_highlighting: SyntaxHighlightingConfig::default(),
            timeouts: TimeoutsConfig::default(),
            automation: AutomationConfig::default(),
            subagents: SubagentRuntimeLimits::default(),
            prompt_cache: PromptCachingConfig::default(),
            mcp: McpClientConfig::default(),
            webmcp: WebmcpConfig::default(),
            acp: AgentClientProtocolConfig::default(),
            hooks: HooksConfig::default(),
            model: ModelConfig::default(),
            provider: ProviderConfig::default(),
            skills: SkillsConfig::default(),
            custom_providers: Vec::new(),
            provider_overrides: BTreeMap::new(),
            providers_whitelist: Vec::new(),
            output_style: OutputStyleConfig::default(),
            dotfile_protection: DotfileProtectionConfig::default(),
            workspace: WorkspaceConfig::default(),
        }
    }
}

impl VTCodeConfig {
    pub fn validate(&self) -> Result<()> {
        self.syntax_highlighting
            .validate()
            .context("Invalid syntax_highlighting configuration")?;

        self.context.validate().context("Invalid context configuration")?;

        self.hooks.validate().context("Invalid hooks configuration")?;

        self.timeouts.validate().context("Invalid timeouts configuration")?;

        self.prompt_cache.validate().context("Invalid prompt_cache configuration")?;

        self.webmcp.validate().context("Invalid webmcp configuration")?;

        self.agent
            .validate_llm_params()
            .map_err(anyhow::Error::msg)
            .context("Invalid agent configuration")?;

        self.ui
            .keyboard_protocol
            .validate()
            .context("Invalid keyboard_protocol configuration")?;

        self.pty.validate().context("Invalid pty configuration")?;

        // Validate custom providers
        let mut seen_names = std::collections::HashSet::new();
        for cp in &self.custom_providers {
            cp.validate()
                .map_err(|msg| anyhow::anyhow!(msg))
                .context("Invalid custom_providers configuration")?;
            if !seen_names.insert(cp.name.to_lowercase()) {
                anyhow::bail!("custom_providers: duplicate name `{}`", cp.name);
            }
        }

        // Validate provider overrides
        for (provider_name, override_config) in &self.provider_overrides {
            override_config
                .validate(provider_name)
                .map_err(|msg| anyhow::anyhow!(msg))
                .context("Invalid provider_overrides configuration")?;
            // Validate that the provider key matches a known provider
            if Provider::from_str(provider_name).is_err() {
                anyhow::bail!(
                    "provider_overrides: unknown provider `{provider_name}`; \
                     must be one of: gemini, openai, anthropic, copilot, deepseek, meta, \
                     openrouter, ollama, lmstudio, llamacpp, moonshot, zai, minimax, \
                     mimo, mistral, huggingface, opencodezen, opencodego, qwen, \
                     stepfun, evolink, poolside, nvidia, merge-gateway, vercel"
                );
            }
        }

        // Validate providers_whitelist entries
        for entry in &self.providers_whitelist {
            let known = Provider::from_str(entry).is_ok() || self.custom_providers.iter().any(|cp| cp.name == *entry);
            if !known {
                anyhow::bail!(
                    "providers_whitelist: unknown provider `{entry}`; \
                     must be a built-in provider or a name from custom_providers"
                );
            }
        }

        Ok(())
    }

    /// Returns true when the persistent memory subsystem is enabled.
    ///
    /// The top-level `features.memories` flag is the global master switch.
    /// `agent.persistent_memory.enabled` then enables repository-scoped storage.
    #[must_use]
    pub fn persistent_memory_enabled(&self) -> bool {
        self.features.memories && self.agent.persistent_memory.enabled
    }

    /// Returns true when the memories subsystem is enabled for injection.
    ///
    /// Memories are active when the subsystem is enabled and `memories.use_memories`
    /// is true.
    #[must_use]
    pub fn memories_enabled(&self) -> bool {
        self.persistent_memory_enabled() && self.agent.persistent_memory.memories.use_memories
    }

    /// Returns true when completed threads should be stored as memory inputs.
    #[must_use]
    pub fn should_generate_memories(&self) -> bool {
        self.persistent_memory_enabled() && self.agent.persistent_memory.memories.generate_memories
    }

    /// Look up a custom provider by its stable key.
    pub fn custom_provider(&self, name: &str) -> Option<&CustomProviderConfig> {
        let lower = name.to_lowercase();
        self.custom_providers.iter().find(|cp| cp.name.to_lowercase() == lower)
    }

    /// Return the explicitly configured credential key for a provider.
    ///
    /// Custom-provider identities take precedence over built-in provider
    /// overrides. A missing result means callers should use the provider's
    /// built-in default or the agent-wide fallback.
    pub fn configured_api_key_env(&self, name: &str) -> Option<String> {
        if let Some(custom_provider) = self.custom_provider(name) {
            return Some(custom_provider.resolved_api_key_env());
        }

        self.provider_overrides
            .iter()
            .find(|(provider, _)| provider.eq_ignore_ascii_case(name))
            .and_then(|(_, provider_override)| provider_override.api_key_env.clone())
            .filter(|api_key_env| !api_key_env.trim().is_empty())
    }

    /// Get the display name for any provider key, falling back to the raw key
    /// if no custom provider matches.
    pub fn provider_display_name(&self, provider_key: &str) -> String {
        if let Some(cp) = self.custom_provider(provider_key) {
            cp.display_name.clone()
        } else if provider_key.eq_ignore_ascii_case("codex") {
            "Codex".to_string()
        } else if let Ok(p) = FromStr::from_str(provider_key) {
            let p: Provider = p;
            p.label().to_string()
        } else {
            provider_key.to_string()
        }
    }
}

fn default_primary_agent() -> String {
    DEFAULT_PRIMARY_AGENT_NAME.to_string()
}