Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/apps/desktop/src/api/acp_client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ pub struct GetAcpSessionOptionsRequest {
pub remote_ssh_host: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ProbeAcpClientRequirementsRequest {
#[serde(default)]
pub remote_connection_id: Option<String>,
#[serde(default)]
pub force_refresh: bool,
}

#[tauri::command]
pub async fn initialize_acp_clients(state: State<'_, AppState>) -> Result<(), String> {
let service = state
Expand All @@ -97,13 +106,17 @@ pub async fn get_acp_clients(state: State<'_, AppState>) -> Result<Vec<AcpClient
#[tauri::command]
pub async fn probe_acp_client_requirements(
state: State<'_, AppState>,
request: ProbeAcpClientRequirementsRequest,
) -> Result<Vec<AcpClientRequirementProbe>, String> {
let service = state
.acp_client_service
.as_ref()
.ok_or_else(|| "ACP client service not initialized".to_string())?;
service
.probe_client_requirements()
.probe_client_requirements(
request.remote_connection_id.as_deref(),
request.force_refresh,
)
.await
.map_err(|e| e.to_string())
}
Expand Down Expand Up @@ -166,7 +179,12 @@ pub async fn create_acp_flow_session(
.await
.map_err(|e| e.to_string())?;
if let Err(error) = service
.start_client_for_session(&request.client_id, &response.session_id)
.start_client_for_session(
&request.client_id,
&response.session_id,
Some(&request.workspace_path),
request.remote_connection_id.as_deref(),
)
.await
{
if let Err(cleanup_error) = service
Expand Down Expand Up @@ -246,14 +264,15 @@ pub async fn start_acp_dialog_turn(
tokio::spawn(async move {
let mut current_round_id: Option<String> = None;
let result = service
.prompt_agent_stream(
&request.client_id,
request.user_input,
request.workspace_path,
Some(request.session_id.clone()),
session_storage_path,
request.timeout_seconds,
|event| {
.prompt_agent_stream(
&request.client_id,
request.user_input,
request.workspace_path,
request.remote_connection_id,
request.session_id.clone(),
session_storage_path,
request.timeout_seconds,
|event| {
match event {
AcpClientStreamEvent::ModelRoundStarted {
round_id,
Expand Down Expand Up @@ -404,7 +423,7 @@ pub async fn cancel_acp_dialog_turn(
.cancel_agent_session(
&request.client_id,
request.workspace_path,
Some(request.session_id),
request.session_id,
)
.await
.map_err(|e| e.to_string())
Expand Down Expand Up @@ -435,8 +454,9 @@ pub async fn get_acp_session_options(
.get_session_options(
&request.client_id,
request.workspace_path,
request.remote_connection_id,
session_storage_path,
Some(request.session_id),
request.session_id,
)
.await
.map_err(|e| e.to_string())
Expand Down
1 change: 1 addition & 0 deletions src/crates/acp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bitfun-events = { path = "../events" }
agent-client-protocol = { version = "=0.11.1", features = ["unstable"] }
tokio = { workspace = true }
tokio-util = { workspace = true, features = ["compat"] }
futures = { workspace = true }
async-trait = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
160 changes: 160 additions & 0 deletions src/crates/acp/src/client/builtin_clients.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
use std::collections::HashMap;

use super::config::{AcpClientConfig, AcpClientPermissionMode};

pub(crate) struct BuiltinAcpClientPreset {
pub(crate) id: &'static str,
pub(crate) command: &'static str,
pub(crate) args: &'static [&'static str],
pub(crate) tool_command: &'static str,
pub(crate) install_package: &'static str,
pub(crate) adapter_package: Option<&'static str>,
pub(crate) adapter_bin: Option<&'static str>,
}

const BUILTIN_ACP_CLIENT_PRESETS: &[BuiltinAcpClientPreset] = &[
BuiltinAcpClientPreset {
id: "opencode",
command: "opencode",
args: &["acp"],
tool_command: "opencode",
install_package: "opencode-ai",
adapter_package: None,
adapter_bin: None,
},
BuiltinAcpClientPreset {
id: "claude-code",
command: "npx",
args: &["--yes", "@zed-industries/claude-code-acp@latest"],
tool_command: "claude",
install_package: "@anthropic-ai/claude-code",
adapter_package: Some("@zed-industries/claude-code-acp"),
adapter_bin: Some("claude-code-acp"),
},
BuiltinAcpClientPreset {
id: "codex",
command: "npx",
args: &["--yes", "@zed-industries/codex-acp@latest"],
tool_command: "codex",
install_package: "@openai/codex",
adapter_package: Some("@zed-industries/codex-acp"),
adapter_bin: Some("codex-acp"),
},
];

pub(crate) fn builtin_client_ids() -> impl Iterator<Item = &'static str> {
BUILTIN_ACP_CLIENT_PRESETS.iter().map(|preset| preset.id)
}

pub(crate) fn builtin_acp_client_preset(
client_id: &str,
) -> Option<&'static BuiltinAcpClientPreset> {
BUILTIN_ACP_CLIENT_PRESETS
.iter()
.find(|preset| preset.id == client_id)
}

pub(crate) fn supported_remote_acp_clients() -> String {
builtin_client_ids().collect::<Vec<_>>().join(", ")
}

pub(crate) fn default_config_for_builtin_client(client_id: &str) -> Option<AcpClientConfig> {
let preset = builtin_acp_client_preset(client_id)?;
Some(AcpClientConfig {
name: None,
command: preset.command.to_string(),
args: preset
.args
.iter()
.map(|value| (*value).to_string())
.collect(),
env: HashMap::new(),
enabled: true,
readonly: false,
permission_mode: AcpClientPermissionMode::Ask,
})
}

pub(crate) fn remote_command_for_builtin_client(client_id: &str) -> Option<String> {
let preset = builtin_acp_client_preset(client_id)?;
Some(render_shell_command(preset.command, preset.args))
}

pub(crate) fn remote_command_for_builtin_client_in_workspace(
client_id: &str,
workspace_path: &str,
) -> Option<String> {
let command = remote_command_for_builtin_client(client_id)?;
let workspace_path = workspace_path.trim();
if workspace_path.is_empty() {
return Some(command);
}
Some(format!(
"cd {} && {}",
shell_escape(workspace_path),
command
))
}

fn render_shell_command(command: &str, args: &[&str]) -> String {
std::iter::once(command)
.chain(args.iter().copied())
.map(shell_escape)
.collect::<Vec<_>>()
.join(" ")
}

fn shell_escape(value: &str) -> String {
if value.chars().all(|ch| {
ch.is_ascii_alphanumeric() || matches!(ch, '/' | '.' | '-' | '_' | ':' | '=' | '@')
}) {
value.to_string()
} else {
format!("'{}'", value.replace('\'', "'\\''"))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn builds_remote_builtin_command_for_opencode() {
assert_eq!(
remote_command_for_builtin_client("opencode").as_deref(),
Some("opencode acp")
);
}

#[test]
fn builds_remote_builtin_command_for_npx_adapter() {
assert_eq!(
remote_command_for_builtin_client("codex").as_deref(),
Some("npx --yes @zed-industries/codex-acp@latest")
);
}

#[test]
fn returns_default_config_for_builtin_client() {
let config = default_config_for_builtin_client("claude-code").expect("builtin config");
assert!(config.enabled);
assert_eq!(config.command, "npx");
assert_eq!(
config.args,
vec!["--yes", "@zed-industries/claude-code-acp@latest"]
);
}

#[test]
fn shell_escape_quotes_spaces() {
assert_eq!(shell_escape("hello world"), "'hello world'");
}

#[test]
fn builds_remote_builtin_command_in_workspace() {
assert_eq!(
remote_command_for_builtin_client_in_workspace("opencode", "/tmp/my repo").as_deref(),
Some("cd '/tmp/my repo' && opencode acp")
);
}
}
9 changes: 9 additions & 0 deletions src/crates/acp/src/client/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ pub struct AcpClientRequirementProbe {
pub notes: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoteAcpClientRequirementSnapshot {
pub connection_id: String,
pub last_probed_at: u64,
#[serde(default)]
pub probes: Vec<AcpClientRequirementProbe>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AcpRequirementProbeItem {
Expand Down
Loading
Loading