| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | use crate::profile::{AgentProfile, Environment}; |
| 4 | |
| 5 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 6 | pub struct CliHit { |
| 7 | pub profile: AgentProfile, |
| 8 | pub binary: PathBuf, |
| 9 | } |
| 10 | |
| 11 | struct Template { |
| 12 | names: &'static [&'static str], |
| 13 | build: fn() -> AgentProfile, |
| 14 | cursor: bool, |
| 15 | } |
| 16 | |
| 17 | pub fn scan_clis() -> Vec<CliHit> { |
| 18 | let templates = [ |
| 19 | Template { |
| 20 | names: &["claude"], |
| 21 | build: AgentProfile::claude_host, |
| 22 | cursor: false, |
| 23 | }, |
| 24 | Template { |
| 25 | names: &["copilot"], |
| 26 | build: AgentProfile::copilot_host, |
| 27 | cursor: false, |
| 28 | }, |
| 29 | Template { |
| 30 | names: &["codex"], |
| 31 | build: AgentProfile::codex_host, |
| 32 | cursor: false, |
| 33 | }, |
| 34 | Template { |
| 35 | names: &["cursor-agent", "agent"], |
| 36 | build: AgentProfile::cursor_host, |
| 37 | cursor: true, |
| 38 | }, |
| 39 | Template { |
| 40 | names: &["agy"], |
| 41 | build: AgentProfile::agy_host, |
| 42 | cursor: false, |
| 43 | }, |
| 44 | ]; |
| 45 | templates |
| 46 | .into_iter() |
| 47 | .filter_map(|template| { |
| 48 | let binary = find_binary(template.names, template.cursor)?; |
| 49 | let program = binary |
| 50 | .file_name() |
| 51 | .and_then(|name| name.to_str()) |
| 52 | .unwrap_or(template.names[0]); |
| 53 | let mut profile = with_program((template.build)(), program); |
| 54 | if !cfg!(windows) { |
| 55 | profile.open_session_command.clear(); |
| 56 | } |
| 57 | profile.environment = Environment::Host; |
| 58 | Some(CliHit { profile, binary }) |
| 59 | }) |
| 60 | .collect() |
| 61 | } |
| 62 | |
| 63 | /// A bare program name is looked up on `PATH` and in `~/.local/bin`. |
| 64 | /// A desktop session often omits the user bin directory, so `claude` and |
| 65 | /// `cursor-agent` exist for the shell but not for the app. |
| 66 | pub fn resolve_program(program: &str) -> Option<PathBuf> { |
| 67 | let path = Path::new(program); |
| 68 | if path.is_absolute() || program.contains('/') || program.contains('\\') { |
| 69 | return path.is_file().then(|| path.to_path_buf()); |
| 70 | } |
| 71 | for directory in candidate_dirs() { |
| 72 | if let Some(found) = executable(&directory, program) { |
| 73 | return Some(found); |
| 74 | } |
| 75 | } |
| 76 | None |
| 77 | } |
| 78 | |
| 79 | pub fn already_installed(existing: &[AgentProfile], hit: &AgentProfile) -> bool { |
| 80 | let program = hit.command_program(); |
| 81 | existing.iter().any(|agent| { |
| 82 | agent.is_command() |
| 83 | && agent.environment == hit.environment |
| 84 | && agent.command_program() == program |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | fn with_program(mut profile: AgentProfile, program: &str) -> AgentProfile { |
| 89 | let rest = profile.command.split_once(' ').map(|(_, rest)| rest); |
| 90 | profile.command = match rest { |
| 91 | Some(rest) => format!("{program} {rest}"), |
| 92 | None => program.to_owned(), |
| 93 | }; |
| 94 | profile |
| 95 | } |
| 96 | |
| 97 | fn candidate_dirs() -> Vec<PathBuf> { |
| 98 | let mut dirs = Vec::new(); |
| 99 | if let Some(path) = std::env::var_os("PATH") { |
| 100 | dirs.extend(std::env::split_paths(&path)); |
| 101 | } |
| 102 | if let Some(home) = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")) { |
| 103 | let home = PathBuf::from(home); |
| 104 | dirs.push(home.join(".local").join("bin")); |
| 105 | } |
| 106 | dirs |
| 107 | } |
| 108 | |
| 109 | fn find_binary(names: &[&str], cursor: bool) -> Option<PathBuf> { |
| 110 | for directory in candidate_dirs() { |
| 111 | for name in names { |
| 112 | if let Some(path) = executable(&directory, name) { |
| 113 | if cursor && *name == "agent" && !looks_like_cursor(&path) { |
| 114 | continue; |
| 115 | } |
| 116 | return Some(path); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | None |
| 121 | } |
| 122 | |
| 123 | fn executable(directory: &Path, name: &str) -> Option<PathBuf> { |
| 124 | ["", ".exe", ".cmd"].into_iter().find_map(|extension| { |
| 125 | let path = directory.join(format!("{name}{extension}")); |
| 126 | path.is_file().then_some(path) |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | fn looks_like_cursor(path: &Path) -> bool { |
| 131 | let linked = std::fs::read_link(path).unwrap_or_else(|_| path.to_path_buf()); |
| 132 | let text = format!("{} {}", path.display(), linked.display()); |
| 133 | text.contains("cursor-agent") |
| 134 | } |
| 135 | |
| 136 | #[cfg(test)] |
| 137 | mod tests { |
| 138 | use super::*; |
| 139 | |
| 140 | #[test] |
| 141 | fn a_wsl_claude_does_not_hide_the_host_binary() { |
| 142 | let wsl = AgentProfile::claude_wsl(); |
| 143 | let host = AgentProfile::claude_host(); |
| 144 | assert!(!already_installed(&[wsl], &host)); |
| 145 | assert!(already_installed(&[host.clone()], &host)); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn an_absolute_program_is_used_and_a_missing_name_is_not() { |
| 150 | let directory = std::env::temp_dir().join(format!("agents-resolve-{}", std::process::id())); |
| 151 | let _ = std::fs::remove_dir_all(&directory); |
| 152 | std::fs::create_dir_all(&directory).unwrap(); |
| 153 | let binary = directory.join("claude"); |
| 154 | std::fs::write(&binary, b"").unwrap(); |
| 155 | assert_eq!( |
| 156 | resolve_program(binary.to_str().unwrap()).as_deref(), |
| 157 | Some(binary.as_path()) |
| 158 | ); |
| 159 | assert!(resolve_program("definitely-not-a-real-program").is_none()); |
| 160 | let _ = std::fs::remove_dir_all(&directory); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn the_program_token_is_replaced_and_the_rest_stays() { |
| 165 | let profile = with_program(AgentProfile::cursor_host(), "cursor-agent"); |
| 166 | assert_eq!( |
| 167 | profile.command, |
| 168 | "cursor-agent -p --trust --approve-mcps {prompt}" |
| 169 | ); |
| 170 | } |
| 171 | } |