1use std::{path::Path, rc::Rc};
2
3use crate::AgentServerCommand;
4use acp_thread::AgentConnection;
5use anyhow::Result;
6use gpui::AsyncApp;
7use thiserror::Error;
8
9mod v0;
10mod v1;
11
12#[derive(Debug, Error)]
13#[error("Unsupported version")]
14pub struct UnsupportedVersion;
15
16pub async fn connect(
17 server_name: &'static str,
18 command: AgentServerCommand,
19 root_dir: &Path,
20 cx: &mut AsyncApp,
21) -> Result<Rc<dyn AgentConnection>> {
22 let conn = v1::AcpConnection::stdio(server_name, command.clone(), root_dir, cx).await;
23
24 match conn {
25 Ok(conn) => Ok(Rc::new(conn) as _),
26 Err(err) if err.is::<UnsupportedVersion>() => {
27 // Consider re-using initialize response and subprocess when adding another version here
28 let conn: Rc<dyn AgentConnection> =
29 Rc::new(v0::AcpConnection::stdio(server_name, command, root_dir, cx).await?);
30 Ok(conn)
31 }
32 Err(err) => Err(err),
33 }
34}