Skip to main content

vtcode_config/types/
shell_profile.rs

1//! Shell prompt profile resolution: profile kind, platform, and WSL-aware
2//! resolution for prompt shaping.
3
4use std::env;
5use std::fmt;
6
7use serde::{Deserialize, Deserializer, Serialize};
8
9/// Configured shell syntax profile used for model-facing command examples.
10///
11/// This is prompt guidance only. Command approval, sandboxing, and allow-list
12/// policy are enforced elsewhere in the runtime.
13#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
15#[serde(rename_all = "snake_case")]
16#[derive(Default)]
17pub enum ShellPromptProfile {
18    /// Detect from the current platform. Unix-like shells are used for Linux,
19    /// macOS, and WSL. Native Windows uses PowerShell.
20    #[default]
21    Auto,
22    /// Use Unix-like shell syntax in prompt examples.
23    UnixLike,
24    /// Use native PowerShell syntax in prompt examples.
25    ///
26    /// The canonical wire value is `powershell` (matching `Self::as_str`,
27    /// [`Self::allowed_values`], and the docs); `snake_case` would otherwise
28    /// render it as the drifting `power_shell`.
29    #[serde(rename = "powershell")]
30    PowerShell,
31}
32
33impl ShellPromptProfile {
34    /// Return the textual representation for configuration.
35    pub(crate) fn as_str(self) -> &'static str {
36        match self {
37            Self::Auto => "auto",
38            Self::UnixLike => "unix_like",
39            Self::PowerShell => "powershell",
40        }
41    }
42
43    /// Parse a shell prompt profile from user configuration.
44    pub(crate) fn parse(value: &str) -> Option<Self> {
45        let normalized = value.trim();
46        if normalized.eq_ignore_ascii_case("auto") {
47            Some(Self::Auto)
48        } else if normalized.eq_ignore_ascii_case("unix_like")
49            || normalized.eq_ignore_ascii_case("unix-like")
50            || normalized.eq_ignore_ascii_case("unix")
51            || normalized.eq_ignore_ascii_case("posix")
52        {
53            Some(Self::UnixLike)
54        } else if normalized.eq_ignore_ascii_case("powershell")
55            || normalized.eq_ignore_ascii_case("power_shell")
56            || normalized.eq_ignore_ascii_case("windows")
57        {
58            Some(Self::PowerShell)
59        } else {
60            None
61        }
62    }
63
64    /// Allowed configuration values for validation.
65    pub fn allowed_values() -> &'static [&'static str] {
66        &["auto", "unix_like", "powershell"]
67    }
68
69    /// Resolve this configured value against the current runtime platform.
70    pub fn resolve_for_current_platform(self) -> ResolvedShellPromptProfile {
71        self.resolve_for_platform(ShellProfilePlatform::current())
72    }
73
74    /// Resolve this configured value against an explicit platform.
75    pub(crate) fn resolve_for_platform(self, platform: ShellProfilePlatform) -> ResolvedShellPromptProfile {
76        match self {
77            Self::Auto => match platform {
78                ShellProfilePlatform::NativeWindows => ResolvedShellPromptProfile::PowerShell,
79                ShellProfilePlatform::UnixLike | ShellProfilePlatform::Wsl => ResolvedShellPromptProfile::UnixLike,
80            },
81            Self::UnixLike => ResolvedShellPromptProfile::UnixLike,
82            Self::PowerShell => ResolvedShellPromptProfile::PowerShell,
83        }
84    }
85}
86
87impl fmt::Display for ShellPromptProfile {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        f.write_str(self.as_str())
90    }
91}
92
93impl<'de> Deserialize<'de> for ShellPromptProfile {
94    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
95    where
96        D: Deserializer<'de>,
97    {
98        let raw = String::deserialize(deserializer)?;
99        if let Some(parsed) = Self::parse(&raw) {
100            Ok(parsed)
101        } else {
102            Ok(Self::default())
103        }
104    }
105}
106
107/// Platform category used to resolve [`ShellPromptProfile::Auto`].
108#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
109#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
110#[serde(rename_all = "snake_case")]
111pub enum ShellProfilePlatform {
112    UnixLike,
113    Wsl,
114    NativeWindows,
115}
116
117impl ShellProfilePlatform {
118    fn current() -> Self {
119        if cfg!(windows) {
120            Self::NativeWindows
121        } else if is_wsl_environment() {
122            Self::Wsl
123        } else {
124            Self::UnixLike
125        }
126    }
127}
128
129/// Prompt profile after automatic platform resolution.
130#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
132#[serde(rename_all = "snake_case")]
133pub enum ResolvedShellPromptProfile {
134    UnixLike,
135    /// Canonical wire value is `powershell` (matching `Self::as_str`); the
136    /// derived `snake_case` form would otherwise be the drifting `power_shell`.
137    #[serde(rename = "powershell")]
138    PowerShell,
139}
140
141impl ResolvedShellPromptProfile {
142    pub(crate) fn as_str(self) -> &'static str {
143        match self {
144            Self::UnixLike => "unix_like",
145            Self::PowerShell => "powershell",
146        }
147    }
148}
149
150impl fmt::Display for ResolvedShellPromptProfile {
151    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152        f.write_str(self.as_str())
153    }
154}
155
156fn is_wsl_environment() -> bool {
157    if env::var_os("WSL_DISTRO_NAME").is_some() || env::var_os("WSL_INTEROP").is_some() {
158        return true;
159    }
160
161    for path in ["/proc/sys/kernel/osrelease", "/proc/version"] {
162        if let Ok(contents) = std::fs::read_to_string(path)
163            && contents.to_ascii_lowercase().contains("microsoft")
164        {
165            return true;
166        }
167    }
168
169    false
170}