vtcode-config 0.186.0

Config loader components shared across VT Code and downstream adopters
Documentation
//! Shell prompt profile resolution: profile kind, platform, and WSL-aware
//! resolution for prompt shaping.

use std::env;
use std::fmt;

use serde::{Deserialize, Deserializer, Serialize};

/// Configured shell syntax profile used for model-facing command examples.
///
/// This is prompt guidance only. Command approval, sandboxing, and allow-list
/// policy are enforced elsewhere in the runtime.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum ShellPromptProfile {
    /// Detect from the current platform. Unix-like shells are used for Linux,
    /// macOS, and WSL. Native Windows uses PowerShell.
    #[default]
    Auto,
    /// Use Unix-like shell syntax in prompt examples.
    UnixLike,
    /// Use native PowerShell syntax in prompt examples.
    ///
    /// The canonical wire value is `powershell` (matching `Self::as_str`,
    /// [`Self::allowed_values`], and the docs); `snake_case` would otherwise
    /// render it as the drifting `power_shell`.
    #[serde(rename = "powershell")]
    PowerShell,
}

impl ShellPromptProfile {
    /// Return the textual representation for configuration.
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Auto => "auto",
            Self::UnixLike => "unix_like",
            Self::PowerShell => "powershell",
        }
    }

    /// Parse a shell prompt profile from user configuration.
    pub(crate) fn parse(value: &str) -> Option<Self> {
        let normalized = value.trim();
        if normalized.eq_ignore_ascii_case("auto") {
            Some(Self::Auto)
        } else if normalized.eq_ignore_ascii_case("unix_like")
            || normalized.eq_ignore_ascii_case("unix-like")
            || normalized.eq_ignore_ascii_case("unix")
            || normalized.eq_ignore_ascii_case("posix")
        {
            Some(Self::UnixLike)
        } else if normalized.eq_ignore_ascii_case("powershell")
            || normalized.eq_ignore_ascii_case("power_shell")
            || normalized.eq_ignore_ascii_case("windows")
        {
            Some(Self::PowerShell)
        } else {
            None
        }
    }

    /// Allowed configuration values for validation.
    pub fn allowed_values() -> &'static [&'static str] {
        &["auto", "unix_like", "powershell"]
    }

    /// Resolve this configured value against the current runtime platform.
    pub fn resolve_for_current_platform(self) -> ResolvedShellPromptProfile {
        self.resolve_for_platform(ShellProfilePlatform::current())
    }

    /// Resolve this configured value against an explicit platform.
    pub(crate) fn resolve_for_platform(self, platform: ShellProfilePlatform) -> ResolvedShellPromptProfile {
        match self {
            Self::Auto => match platform {
                ShellProfilePlatform::NativeWindows => ResolvedShellPromptProfile::PowerShell,
                ShellProfilePlatform::UnixLike | ShellProfilePlatform::Wsl => ResolvedShellPromptProfile::UnixLike,
            },
            Self::UnixLike => ResolvedShellPromptProfile::UnixLike,
            Self::PowerShell => ResolvedShellPromptProfile::PowerShell,
        }
    }
}

impl fmt::Display for ShellPromptProfile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for ShellPromptProfile {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = String::deserialize(deserializer)?;
        if let Some(parsed) = Self::parse(&raw) {
            Ok(parsed)
        } else {
            Ok(Self::default())
        }
    }
}

/// Platform category used to resolve [`ShellPromptProfile::Auto`].
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ShellProfilePlatform {
    UnixLike,
    Wsl,
    NativeWindows,
}

impl ShellProfilePlatform {
    fn current() -> Self {
        if cfg!(windows) {
            Self::NativeWindows
        } else if is_wsl_environment() {
            Self::Wsl
        } else {
            Self::UnixLike
        }
    }
}

/// Prompt profile after automatic platform resolution.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolvedShellPromptProfile {
    UnixLike,
    /// Canonical wire value is `powershell` (matching `Self::as_str`); the
    /// derived `snake_case` form would otherwise be the drifting `power_shell`.
    #[serde(rename = "powershell")]
    PowerShell,
}

impl ResolvedShellPromptProfile {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::UnixLike => "unix_like",
            Self::PowerShell => "powershell",
        }
    }
}

impl fmt::Display for ResolvedShellPromptProfile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

fn is_wsl_environment() -> bool {
    if env::var_os("WSL_DISTRO_NAME").is_some() || env::var_os("WSL_INTEROP").is_some() {
        return true;
    }

    for path in ["/proc/sys/kernel/osrelease", "/proc/version"] {
        if let Ok(contents) = std::fs::read_to_string(path)
            && contents.to_ascii_lowercase().contains("microsoft")
        {
            return true;
        }
    }

    false
}