| 1 | use serde::{Deserialize, Serialize}; |
| 2 | |
| 3 | const CLAUDE_COMMAND: &str = |
| 4 | "claude -p --output-format stream-json --verbose --session-id {session_id}"; |
| 5 | const COPILOT_COMMAND: &str = "copilot -p {prompt} --output-format json --session-id {session_id}"; |
| 6 | pub(crate) const LEGACY_CLAUDE_WSL_OPEN_SESSION: &str = |
| 7 | "wt.exe wsl.exe --cd {cwd} -e claude --resume {session_id}"; |
| 8 | pub(crate) const LEGACY_COPILOT_WSL_OPEN_SESSION: &str = |
| 9 | "wt.exe wsl.exe --cd {cwd} -e copilot --resume {session_id}"; |
| 10 | const CLAUDE_WSL_OPEN_SESSION: &str = |
| 11 | "wt.exe wsl.exe --cd {cwd} -e sh -lc \"exec claude --resume {session_id}\""; |
| 12 | const COPILOT_WSL_OPEN_SESSION: &str = |
| 13 | "wt.exe wsl.exe --cd {cwd} -e sh -lc \"exec copilot --resume {session_id}\""; |
| 14 | |
| 15 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 16 | pub enum Environment { |
| 17 | #[default] |
| 18 | Host, |
| 19 | Wsl { |
| 20 | distro: Option<String>, |
| 21 | }, |
| 22 | } |
| 23 | |
| 24 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 25 | pub enum OutputFormat { |
| 26 | #[default] |
| 27 | ClaudeStreamJson, |
| 28 | CopilotJsonl, |
| 29 | Text, |
| 30 | } |
| 31 | |
| 32 | /// Where a run is answered. Missing JSON stays a command, so older settings keep working. |
| 33 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 34 | #[serde(tag = "type", rename_all = "snake_case")] |
| 35 | pub enum AgentBackend { |
| 36 | #[default] |
| 37 | Command, |
| 38 | OpenAi { |
| 39 | base_url: String, |
| 40 | model: String, |
| 41 | #[serde(default)] |
| 42 | api_key: String, |
| 43 | }, |
| 44 | Anthropic { |
| 45 | model: String, |
| 46 | #[serde(default)] |
| 47 | api_key: String, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 52 | #[serde(default)] |
| 53 | pub struct AgentProfile { |
| 54 | pub id: String, |
| 55 | pub name: String, |
| 56 | pub environment: Environment, |
| 57 | pub command: String, |
| 58 | pub output: OutputFormat, |
| 59 | pub open_session_command: String, |
| 60 | pub mcp_config_flag: String, |
| 61 | pub backend: AgentBackend, |
| 62 | } |
| 63 | |
| 64 | impl AgentProfile { |
| 65 | pub fn is_command(&self) -> bool { |
| 66 | matches!(self.backend, AgentBackend::Command) |
| 67 | } |
| 68 | |
| 69 | pub fn is_chat(&self) -> bool { |
| 70 | !self.is_command() |
| 71 | } |
| 72 | |
| 73 | pub fn backend_label(&self) -> String { |
| 74 | match &self.backend { |
| 75 | AgentBackend::Command => String::new(), |
| 76 | AgentBackend::OpenAi { |
| 77 | base_url, model, .. |
| 78 | } => { |
| 79 | let model = if model.is_empty() { "no model" } else { model }; |
| 80 | format!("{model} · {base_url}") |
| 81 | } |
| 82 | AgentBackend::Anthropic { model, .. } => { |
| 83 | let model = if model.is_empty() { "no model" } else { model }; |
| 84 | format!("Anthropic · {model}") |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn command_program(&self) -> &str { |
| 90 | self.command.split_whitespace().next().unwrap_or("") |
| 91 | } |
| 92 | |
| 93 | pub(crate) fn migrate_open_session_command(&mut self) { |
| 94 | let replacement = match self.open_session_command.as_str() { |
| 95 | LEGACY_CLAUDE_WSL_OPEN_SESSION => CLAUDE_WSL_OPEN_SESSION, |
| 96 | LEGACY_COPILOT_WSL_OPEN_SESSION => COPILOT_WSL_OPEN_SESSION, |
| 97 | _ => return, |
| 98 | }; |
| 99 | self.open_session_command = replacement.to_string(); |
| 100 | } |
| 101 | |
| 102 | pub fn claude_wsl() -> Self { |
| 103 | Self { |
| 104 | id: "claude".to_string(), |
| 105 | name: "Claude Code (WSL)".to_string(), |
| 106 | environment: Environment::Wsl { distro: None }, |
| 107 | command: CLAUDE_COMMAND.to_string(), |
| 108 | output: OutputFormat::ClaudeStreamJson, |
| 109 | open_session_command: CLAUDE_WSL_OPEN_SESSION.to_string(), |
| 110 | mcp_config_flag: "--mcp-config".to_string(), |
| 111 | backend: AgentBackend::Command, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub fn claude_host() -> Self { |
| 116 | Self { |
| 117 | id: "claude-host".to_string(), |
| 118 | name: "Claude Code".to_string(), |
| 119 | environment: Environment::Host, |
| 120 | command: CLAUDE_COMMAND.to_string(), |
| 121 | output: OutputFormat::ClaudeStreamJson, |
| 122 | open_session_command: "wt.exe -d {cwd} claude --resume {session_id}".to_string(), |
| 123 | mcp_config_flag: "--mcp-config".to_string(), |
| 124 | backend: AgentBackend::Command, |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | pub fn copilot_host() -> Self { |
| 129 | Self { |
| 130 | id: "copilot-host".to_string(), |
| 131 | name: "Copilot CLI".to_string(), |
| 132 | environment: Environment::Host, |
| 133 | command: COPILOT_COMMAND.to_string(), |
| 134 | output: OutputFormat::CopilotJsonl, |
| 135 | open_session_command: "wt.exe -d {cwd} copilot --resume {session_id}".to_string(), |
| 136 | mcp_config_flag: "--additional-mcp-config".to_string(), |
| 137 | backend: AgentBackend::Command, |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn codex_host() -> Self { |
| 142 | Self { |
| 143 | id: "codex".to_string(), |
| 144 | name: "Codex CLI".to_string(), |
| 145 | environment: Environment::Host, |
| 146 | command: "codex exec --skip-git-repo-check {prompt}".to_string(), |
| 147 | output: OutputFormat::Text, |
| 148 | ..Self::default() |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | pub fn cursor_host() -> Self { |
| 153 | Self { |
| 154 | id: "cursor".to_string(), |
| 155 | name: "Cursor CLI".to_string(), |
| 156 | environment: Environment::Host, |
| 157 | command: "agent -p --trust --approve-mcps {prompt}".to_string(), |
| 158 | output: OutputFormat::Text, |
| 159 | ..Self::default() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | pub fn agy_host() -> Self { |
| 164 | Self { |
| 165 | id: "agy".to_string(), |
| 166 | name: "Antigravity CLI".to_string(), |
| 167 | environment: Environment::Host, |
| 168 | command: "agy -p {prompt}".to_string(), |
| 169 | output: OutputFormat::Text, |
| 170 | ..Self::default() |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | pub fn ollama() -> Self { |
| 175 | Self::open_ai("ollama", "Ollama", "http://127.0.0.1:11434/v1", "", "") |
| 176 | } |
| 177 | |
| 178 | pub fn openai() -> Self { |
| 179 | Self::open_ai( |
| 180 | "openai", |
| 181 | "OpenAI", |
| 182 | "https://api.openai.com/v1", |
| 183 | "gpt-4o", |
| 184 | "", |
| 185 | ) |
| 186 | } |
| 187 | |
| 188 | pub fn openrouter() -> Self { |
| 189 | Self::open_ai( |
| 190 | "openrouter", |
| 191 | "OpenRouter", |
| 192 | "https://openrouter.ai/api/v1", |
| 193 | "openrouter/auto", |
| 194 | "", |
| 195 | ) |
| 196 | } |
| 197 | |
| 198 | pub fn openai_compatible() -> Self { |
| 199 | Self::open_ai( |
| 200 | "openai-compatible", |
| 201 | "OpenAI-compatible", |
| 202 | "http://127.0.0.1:8080/v1", |
| 203 | "", |
| 204 | "", |
| 205 | ) |
| 206 | } |
| 207 | |
| 208 | pub fn anthropic() -> Self { |
| 209 | Self { |
| 210 | id: "anthropic".to_string(), |
| 211 | name: "Anthropic".to_string(), |
| 212 | backend: AgentBackend::Anthropic { |
| 213 | model: "claude-sonnet-4-5".to_string(), |
| 214 | api_key: String::new(), |
| 215 | }, |
| 216 | ..Self::default() |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | fn open_ai(id: &str, name: &str, base_url: &str, model: &str, api_key: &str) -> Self { |
| 221 | Self { |
| 222 | id: id.to_string(), |
| 223 | name: name.to_string(), |
| 224 | backend: AgentBackend::OpenAi { |
| 225 | base_url: base_url.to_string(), |
| 226 | model: model.to_string(), |
| 227 | api_key: api_key.to_string(), |
| 228 | }, |
| 229 | ..Self::default() |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | pub fn copilot_wsl() -> Self { |
| 234 | Self { |
| 235 | id: "copilot".to_string(), |
| 236 | name: "Copilot CLI (WSL)".to_string(), |
| 237 | environment: Environment::Wsl { distro: None }, |
| 238 | command: COPILOT_COMMAND.to_string(), |
| 239 | output: OutputFormat::CopilotJsonl, |
| 240 | open_session_command: COPILOT_WSL_OPEN_SESSION.to_string(), |
| 241 | mcp_config_flag: "--additional-mcp-config".to_string(), |
| 242 | backend: AgentBackend::Command, |
| 243 | } |
| 244 | } |
| 245 | } |