vtcode_core/config/
acp.rs

1use crate::config::constants::env::acp::AgentClientProtocolEnvKey;
2use crate::utils::dot_config::WorkspaceTrustLevel;
3use serde::{Deserialize, Serialize};
4
5fn parse_env_bool(key: AgentClientProtocolEnvKey, default: bool) -> bool {
6    std::env::var(key.as_str())
7        .ok()
8        .and_then(|value| {
9            let normalized = value.trim().to_ascii_lowercase();
10            match normalized.as_str() {
11                "1" | "true" | "yes" | "on" => Some(true),
12                "0" | "false" | "no" | "off" => Some(false),
13                _ => None,
14            }
15        })
16        .unwrap_or(default)
17}
18
19fn default_enabled() -> bool {
20    parse_env_bool(AgentClientProtocolEnvKey::Enabled, false)
21}
22
23fn default_zed_enabled() -> bool {
24    parse_env_bool(AgentClientProtocolEnvKey::ZedEnabled, default_enabled())
25}
26
27fn default_zed_tools_read_file_enabled() -> bool {
28    parse_env_bool(AgentClientProtocolEnvKey::ZedToolsReadFileEnabled, true)
29}
30
31fn default_zed_tools_list_files_enabled() -> bool {
32    parse_env_bool(AgentClientProtocolEnvKey::ZedToolsListFilesEnabled, true)
33}
34
35fn parse_env_trust_mode(
36    key: AgentClientProtocolEnvKey,
37    default: AgentClientProtocolZedWorkspaceTrustMode,
38) -> AgentClientProtocolZedWorkspaceTrustMode {
39    std::env::var(key.as_str())
40        .ok()
41        .and_then(|value| AgentClientProtocolZedWorkspaceTrustMode::from_env_value(&value))
42        .unwrap_or(default)
43}
44
45fn default_zed_workspace_trust_mode() -> AgentClientProtocolZedWorkspaceTrustMode {
46    parse_env_trust_mode(
47        AgentClientProtocolEnvKey::ZedWorkspaceTrust,
48        AgentClientProtocolZedWorkspaceTrustMode::FullAuto,
49    )
50}
51
52fn default_transport() -> AgentClientProtocolTransport {
53    AgentClientProtocolTransport::Stdio
54}
55
56/// Agent Client Protocol configuration root
57#[derive(Debug, Clone, Deserialize, Serialize)]
58pub struct AgentClientProtocolConfig {
59    /// Globally enable the ACP bridge
60    #[serde(default = "default_enabled")]
61    pub enabled: bool,
62
63    /// Zed IDE integration settings
64    #[serde(default)]
65    pub zed: AgentClientProtocolZedConfig,
66}
67
68impl Default for AgentClientProtocolConfig {
69    fn default() -> Self {
70        Self {
71            enabled: default_enabled(),
72            zed: AgentClientProtocolZedConfig::default(),
73        }
74    }
75}
76
77/// Transport options supported by the ACP bridge
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
79#[serde(rename_all = "snake_case")]
80pub enum AgentClientProtocolTransport {
81    /// Communicate over stdio (spawned process model)
82    Stdio,
83}
84
85impl Default for AgentClientProtocolTransport {
86    fn default() -> Self {
87        default_transport()
88    }
89}
90
91/// Zed-specific configuration
92#[derive(Debug, Clone, Deserialize, Serialize)]
93pub struct AgentClientProtocolZedConfig {
94    /// Enable Zed integration
95    #[serde(default = "default_zed_enabled")]
96    pub enabled: bool,
97
98    /// Transport used to communicate with the Zed client
99    #[serde(default = "default_transport")]
100    pub transport: AgentClientProtocolTransport,
101
102    /// Tool toggles exposed through the Zed bridge
103    #[serde(default)]
104    pub tools: AgentClientProtocolZedToolsConfig,
105
106    /// Desired workspace trust level when running under ACP
107    #[serde(default = "default_zed_workspace_trust_mode")]
108    pub workspace_trust: AgentClientProtocolZedWorkspaceTrustMode,
109}
110
111impl Default for AgentClientProtocolZedConfig {
112    fn default() -> Self {
113        Self {
114            enabled: default_zed_enabled(),
115            transport: default_transport(),
116            tools: AgentClientProtocolZedToolsConfig::default(),
117            workspace_trust: default_zed_workspace_trust_mode(),
118        }
119    }
120}
121
122/// Zed bridge tool configuration flags
123#[derive(Debug, Clone, Deserialize, Serialize)]
124pub struct AgentClientProtocolZedToolsConfig {
125    /// Toggle the read_file function bridge
126    #[serde(default = "default_zed_tools_read_file_enabled")]
127    pub read_file: bool,
128
129    /// Toggle the list_files function bridge
130    #[serde(default = "default_zed_tools_list_files_enabled")]
131    pub list_files: bool,
132}
133
134impl Default for AgentClientProtocolZedToolsConfig {
135    fn default() -> Self {
136        Self {
137            read_file: default_zed_tools_read_file_enabled(),
138            list_files: default_zed_tools_list_files_enabled(),
139        }
140    }
141}
142
143/// Workspace trust configuration for the Zed ACP bridge
144#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
145#[serde(rename_all = "snake_case")]
146pub enum AgentClientProtocolZedWorkspaceTrustMode {
147    /// Maintain full automation trust
148    FullAuto,
149    /// Restrict to tools policy safeguards
150    ToolsPolicy,
151}
152
153impl AgentClientProtocolZedWorkspaceTrustMode {
154    fn from_env_value(value: &str) -> Option<Self> {
155        let normalized = value.trim().to_ascii_lowercase();
156        match normalized.as_str() {
157            "full_auto" | "full-auto" | "full" => Some(Self::FullAuto),
158            "tools_policy" | "tools-policy" | "tools" => Some(Self::ToolsPolicy),
159            _ => None,
160        }
161    }
162
163    /// Resolve the workspace trust level represented by this configuration.
164    pub fn to_workspace_trust_level(self) -> WorkspaceTrustLevel {
165        match self {
166            Self::FullAuto => WorkspaceTrustLevel::FullAuto,
167            Self::ToolsPolicy => WorkspaceTrustLevel::ToolsPolicy,
168        }
169    }
170}
171
172#[cfg(test)]
173mod tests {
174    use super::*;
175
176    #[test]
177    fn defaults_use_stdio_transport() {
178        let cfg = AgentClientProtocolConfig::default();
179        assert!(matches!(
180            cfg.zed.transport,
181            AgentClientProtocolTransport::Stdio
182        ));
183        assert!(cfg.zed.tools.read_file);
184        assert!(cfg.zed.tools.list_files);
185        assert!(matches!(
186            cfg.zed.workspace_trust,
187            AgentClientProtocolZedWorkspaceTrustMode::FullAuto
188        ));
189    }
190}