vtcode_config/
acp.rs

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