1mod claude;
2mod gemini;
3mod settings;
4
5#[cfg(test)]
6mod e2e_tests;
7
8pub use claude::*;
9pub use gemini::*;
10pub use settings::*;
11
12use acp_thread::AgentConnection;
13use anyhow::Result;
14use collections::HashMap;
15use gpui::{App, AsyncApp, Entity, SharedString, Task};
16use project::Project;
17use schemars::JsonSchema;
18use serde::{Deserialize, Serialize};
19use std::{
20 path::{Path, PathBuf},
21 rc::Rc,
22 sync::Arc,
23};
24use util::ResultExt as _;
25
26pub fn init(cx: &mut App) {
27 settings::init(cx);
28}
29
30pub trait AgentServer: Send {
31 fn logo(&self) -> ui::IconName;
32 fn name(&self) -> &'static str;
33 fn empty_state_headline(&self) -> &'static str;
34 fn empty_state_message(&self) -> &'static str;
35
36 fn connect(
37 &self,
38 // these will go away when old_acp is fully removed
39 root_dir: &Path,
40 project: &Entity<Project>,
41 cx: &mut App,
42 ) -> Task<Result<Rc<dyn AgentConnection>>>;
43}
44
45impl std::fmt::Debug for AgentServerCommand {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 let filtered_env = self.env.as_ref().map(|env| {
48 env.iter()
49 .map(|(k, v)| {
50 (
51 k,
52 if util::redact::should_redact(k) {
53 "[REDACTED]"
54 } else {
55 v
56 },
57 )
58 })
59 .collect::<Vec<_>>()
60 });
61
62 f.debug_struct("AgentServerCommand")
63 .field("path", &self.path)
64 .field("args", &self.args)
65 .field("env", &filtered_env)
66 .finish()
67 }
68}
69
70pub enum AgentServerVersion {
71 Supported,
72 Unsupported {
73 error_message: SharedString,
74 upgrade_message: SharedString,
75 upgrade_command: String,
76 },
77}
78
79#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, JsonSchema)]
80pub struct AgentServerCommand {
81 #[serde(rename = "command")]
82 pub path: PathBuf,
83 #[serde(default)]
84 pub args: Vec<String>,
85 pub env: Option<HashMap<String, String>>,
86}
87
88impl AgentServerCommand {
89 pub(crate) async fn resolve(
90 path_bin_name: &'static str,
91 extra_args: &[&'static str],
92 settings: Option<AgentServerSettings>,
93 project: &Entity<Project>,
94 cx: &mut AsyncApp,
95 ) -> Option<Self> {
96 if let Some(agent_settings) = settings {
97 return Some(Self {
98 path: agent_settings.command.path,
99 args: agent_settings
100 .command
101 .args
102 .into_iter()
103 .chain(extra_args.iter().map(|arg| arg.to_string()))
104 .collect(),
105 env: agent_settings.command.env,
106 });
107 } else {
108 find_bin_in_path(path_bin_name, project, cx)
109 .await
110 .map(|path| Self {
111 path,
112 args: extra_args.iter().map(|arg| arg.to_string()).collect(),
113 env: None,
114 })
115 }
116 }
117}
118
119async fn find_bin_in_path(
120 bin_name: &'static str,
121 project: &Entity<Project>,
122 cx: &mut AsyncApp,
123) -> Option<PathBuf> {
124 let (env_task, root_dir) = project
125 .update(cx, |project, cx| {
126 let worktree = project.visible_worktrees(cx).next();
127 match worktree {
128 Some(worktree) => {
129 let env_task = project.environment().update(cx, |env, cx| {
130 env.get_worktree_environment(worktree.clone(), cx)
131 });
132
133 let path = worktree.read(cx).abs_path();
134 (env_task, path)
135 }
136 None => {
137 let path: Arc<Path> = paths::home_dir().as_path().into();
138 let env_task = project.environment().update(cx, |env, cx| {
139 env.get_directory_environment(path.clone(), cx)
140 });
141 (env_task, path)
142 }
143 }
144 })
145 .log_err()?;
146
147 cx.background_executor()
148 .spawn(async move {
149 let which_result = if cfg!(windows) {
150 which::which(bin_name)
151 } else {
152 let env = env_task.await.unwrap_or_default();
153 let shell_path = env.get("PATH").cloned();
154 which::which_in(bin_name, shell_path.as_ref(), root_dir.as_ref())
155 };
156
157 if let Err(which::Error::CannotFindBinaryPath) = which_result {
158 return None;
159 }
160
161 which_result.log_err()
162 })
163 .await
164}