Skip to main content

vtcode_commons/
reasoning.rs

1//! Reasoning effort level definitions shared across VT Code crates.
2//!
3//! This module provides the [`ReasoningEffortLevel`] enum and associated
4//! constants used for configuring model reasoning depth. These types live
5//! in `vtcode-commons` so that both `vtcode-config` and `vtcode-llm`
6//! can reference them without circular dependencies.
7
8use serde::{Deserialize, Deserializer, Serialize};
9use std::fmt;
10
11/// Reasoning effort level string constants.
12pub mod constants {
13    pub const NONE: &str = "none";
14    pub const MINIMAL: &str = "minimal";
15    pub const LOW: &str = "low";
16    pub const MEDIUM: &str = "medium";
17    pub const HIGH: &str = "high";
18    pub const XHIGH: &str = "xhigh";
19    pub const MAX: &str = "max";
20    pub const ALLOWED_LEVELS: &[&str] = &[MINIMAL, LOW, MEDIUM, HIGH, XHIGH, MAX];
21    pub const LABEL_LOW: &str = "Low";
22    pub const LABEL_MEDIUM: &str = "Medium";
23    pub const LABEL_HIGH: &str = "High";
24    pub const DESCRIPTION_LOW: &str = "Fast responses with lightweight reasoning.";
25    pub const DESCRIPTION_MEDIUM: &str = "Balanced depth and speed for general tasks. (Note: May not be fully available for all models including Gemini 3 Pro)";
26    pub const DESCRIPTION_HIGH: &str = "Maximum reasoning depth for complex problems.";
27}
28
29/// Supported reasoning effort levels configured via vtcode.toml
30/// These map to different provider-specific parameters:
31/// - For Gemini 3 Pro: Maps to thinking_level (low, high) - medium coming soon
32/// - For other models: Maps to provider-specific reasoning parameters
33#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
35#[serde(rename_all = "lowercase")]
36#[derive(Default)]
37pub enum ReasoningEffortLevel {
38    /// No reasoning configuration - for models that don't support configurable reasoning
39    None,
40    /// Minimal reasoning effort - maps to low thinking level for Gemini 3 Pro
41    Minimal,
42    /// Low reasoning effort - maps to low thinking level for Gemini 3 Pro
43    Low,
44    /// Medium reasoning effort - Note: Not fully available for Gemini 3 Pro yet, defaults to high
45    #[default]
46    Medium,
47    /// High reasoning effort - maps to high thinking level for Gemini 3 Pro
48    High,
49    /// Extra high reasoning effort - for GPT-5.4-family and similar long-running tasks
50    XHigh,
51    /// Maximum reasoning effort - for Claude Opus 4.7 adaptive thinking
52    Max,
53}
54
55impl ReasoningEffortLevel {
56    /// Return the textual representation expected by downstream APIs
57    pub fn as_str(self) -> &'static str {
58        match self {
59            Self::None => constants::NONE,
60            Self::Minimal => constants::MINIMAL,
61            Self::Low => constants::LOW,
62            Self::Medium => constants::MEDIUM,
63            Self::High => constants::HIGH,
64            Self::XHigh => constants::XHIGH,
65            Self::Max => constants::MAX,
66        }
67    }
68
69    /// Attempt to parse an effort level from user configuration input
70    pub fn parse(value: &str) -> Option<Self> {
71        let normalized = value.trim();
72        if normalized.eq_ignore_ascii_case(constants::NONE) {
73            Some(Self::None)
74        } else if normalized.eq_ignore_ascii_case(constants::MINIMAL) {
75            Some(Self::Minimal)
76        } else if normalized.eq_ignore_ascii_case(constants::LOW) {
77            Some(Self::Low)
78        } else if normalized.eq_ignore_ascii_case(constants::MEDIUM) {
79            Some(Self::Medium)
80        } else if normalized.eq_ignore_ascii_case(constants::HIGH) {
81            Some(Self::High)
82        } else if normalized.eq_ignore_ascii_case(constants::XHIGH) {
83            Some(Self::XHigh)
84        } else if normalized.eq_ignore_ascii_case(constants::MAX) {
85            Some(Self::Max)
86        } else {
87            None
88        }
89    }
90
91    /// Enumerate the allowed configuration values for validation and messaging
92    pub fn allowed_values() -> &'static [&'static str] {
93        constants::ALLOWED_LEVELS
94    }
95}
96
97impl fmt::Display for ReasoningEffortLevel {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        f.write_str(self.as_str())
100    }
101}
102
103impl<'de> Deserialize<'de> for ReasoningEffortLevel {
104    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
105    where
106        D: Deserializer<'de>,
107    {
108        let raw = String::deserialize(deserializer)?;
109        if let Some(parsed) = Self::parse(&raw) {
110            Ok(parsed)
111        } else {
112            Ok(Self::default())
113        }
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn test_reasoning_effort_parse_and_allowed_values_include_max() {
123        assert_eq!(
124            ReasoningEffortLevel::parse("max"),
125            Some(ReasoningEffortLevel::Max)
126        );
127        assert_eq!(ReasoningEffortLevel::Max.as_str(), "max");
128        assert!(ReasoningEffortLevel::allowed_values().contains(&"max"));
129    }
130}