|
| 1 | +mod agent; |
| 2 | +mod execution; |
| 3 | +mod types; |
| 4 | +mod ui; |
| 5 | + |
| 6 | +// Re-export types for external use |
| 7 | +use std::io::Write; |
| 8 | + |
| 9 | +use agent::{ |
| 10 | + list_available_agents, |
| 11 | + load_agent_execution, |
| 12 | + request_user_approval, |
| 13 | + validate_agent_availability, |
| 14 | +}; |
| 15 | +use execution::{spawn_agent_process, status_agent, status_all_agents}; |
| 16 | +use ui::display_default_agent_warning; |
| 17 | +use eyre::{ |
| 18 | + Result, |
| 19 | + eyre, |
| 20 | +}; |
| 21 | +use serde::{Deserialize, Serialize}; |
| 22 | +use strum::{Display, EnumString}; |
| 23 | + |
| 24 | +use crate::cli::chat::tools::{ |
| 25 | + InvokeOutput, |
| 26 | + OutputKind, |
| 27 | +}; |
| 28 | +use crate::database::settings::Setting; |
| 29 | +use crate::os::Os; |
| 30 | + |
| 31 | +const DEFAULT_AGENT: &str = "default"; |
| 32 | +const ALL_AGENTS: &str = "all"; |
| 33 | + |
| 34 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 35 | +pub struct Delegate { |
| 36 | + /// Operation to perform: launch, status, or list |
| 37 | + pub operation: String, |
| 38 | + /// Agent name to use (optional - uses "default" if not specified) |
| 39 | + #[serde(default)] |
| 40 | + pub agent: Option<String>, |
| 41 | + /// Task description (required for launch operation) |
| 42 | + #[serde(default)] |
| 43 | + pub task: Option<String>, |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Debug, Display, EnumString)] |
| 47 | +#[strum(serialize_all = "lowercase")] |
| 48 | +enum Operation { |
| 49 | + Launch, |
| 50 | + Status, |
| 51 | + List, |
| 52 | +} |
| 53 | + |
| 54 | +#[allow(unused_imports)] |
| 55 | +pub use types::{ |
| 56 | + AgentConfig, |
| 57 | + AgentExecution, |
| 58 | + AgentStatus, |
| 59 | +}; |
| 60 | + |
| 61 | +impl Delegate { |
| 62 | + pub async fn invoke(&self, os: &Os, _output: &mut impl Write) -> Result<InvokeOutput> { |
| 63 | + if !is_enabled(os) { |
| 64 | + return Ok(InvokeOutput { |
| 65 | + output: OutputKind::Text( |
| 66 | + "Delegate tool is experimental and not enabled. Use /experiment to enable it.".to_string(), |
| 67 | + ), |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + // Validate operation first |
| 72 | + let operation = self.operation.parse::<Operation>() |
| 73 | + .map_err(|_| eyre!("Invalid operation. Use: launch, status, or list"))?; |
| 74 | + |
| 75 | + // Validate required fields based on operation |
| 76 | + match operation { |
| 77 | + Operation::Launch => { |
| 78 | + if self.task.is_none() { |
| 79 | + return Err(eyre!("Task description is required for launch operation")); |
| 80 | + } |
| 81 | + if self.agent.is_none() { |
| 82 | + return Err(eyre!("Agent name is required for launch operation. Use 'list' operation to see available agents, then specify agent name.")); |
| 83 | + } |
| 84 | + |
| 85 | + // Validate agent name exists |
| 86 | + let agent_name = self.agent.as_ref().unwrap(); |
| 87 | + if agent_name != DEFAULT_AGENT { |
| 88 | + let available_agents = list_available_agents(os).await?; |
| 89 | + if !available_agents.contains(agent_name) { |
| 90 | + return Err(eyre!( |
| 91 | + "Agent '{}' not found. Available agents: default, {}. Use exact names only.", |
| 92 | + agent_name, |
| 93 | + available_agents.join(", ") |
| 94 | + )); |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + Operation::Status | Operation::List => { |
| 99 | + // No additional validation needed |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + let agent_name = self.get_agent_name(); |
| 104 | + |
| 105 | + let result = match operation { |
| 106 | + Operation::Launch => { |
| 107 | + let task = self.task.as_ref().unwrap(); // Safe due to validation above |
| 108 | + launch_agent(os, agent_name, task).await? |
| 109 | + }, |
| 110 | + Operation::Status => { |
| 111 | + if agent_name == ALL_AGENTS { |
| 112 | + status_all_agents(os).await? |
| 113 | + } else { |
| 114 | + status_agent(os, agent_name).await? |
| 115 | + } |
| 116 | + }, |
| 117 | + Operation::List => { |
| 118 | + list_agents(os).await? |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + Ok(InvokeOutput { |
| 123 | + output: OutputKind::Text(result), |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + pub fn queue_description(&self, output: &mut impl Write) -> Result<()> { |
| 128 | + if let Ok(operation) = self.operation.parse::<Operation>() { |
| 129 | + match operation { |
| 130 | + Operation::Launch => writeln!(output, "Delegating task to agent")?, |
| 131 | + Operation::Status => writeln!(output, "Checking agent status")?, |
| 132 | + Operation::List => writeln!(output, "Listing available agents")?, |
| 133 | + } |
| 134 | + } else { |
| 135 | + writeln!( |
| 136 | + output, |
| 137 | + "Invalid operation '{}'. Use: launch, status, or list", |
| 138 | + self.operation |
| 139 | + )?; |
| 140 | + } |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + |
| 144 | + fn get_agent_name(&self) -> &str { |
| 145 | + if let Ok(operation) = self.operation.parse::<Operation>() { |
| 146 | + match operation { |
| 147 | + Operation::Launch => { |
| 148 | + // Agent is required for launch (validated above) |
| 149 | + self.agent.as_deref().unwrap_or("") |
| 150 | + }, |
| 151 | + Operation::Status => self.agent.as_deref().unwrap_or(ALL_AGENTS), |
| 152 | + Operation::List => "", // Agent name not needed for list operation |
| 153 | + } |
| 154 | + } else { |
| 155 | + self.agent.as_deref().unwrap_or("") |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +async fn list_agents(os: &Os) -> Result<String> { |
| 161 | + let agents = list_available_agents(os).await?; |
| 162 | + if agents.is_empty() { |
| 163 | + Ok("No custom agents configured. Only 'default' agent is available.".to_string()) |
| 164 | + } else { |
| 165 | + Ok(format!("Available agents: default, {}", agents.join(", "))) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +async fn launch_agent(os: &Os, agent: &str, task: &str) -> Result<String> { |
| 170 | + validate_agent_availability(os, agent).await?; |
| 171 | + |
| 172 | + // Check if agent is already running |
| 173 | + if let Some(execution) = load_agent_execution(os, agent).await? { |
| 174 | + if execution.status == AgentStatus::Running { |
| 175 | + return Err(eyre::eyre!("Agent '{}' is already running. Use status operation to check progress or wait for completion.", agent)); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if agent == DEFAULT_AGENT { |
| 180 | + // Show warning for default agent but no approval needed |
| 181 | + display_default_agent_warning()?; |
| 182 | + } else { |
| 183 | + // Show agent info and require approval for specific agents |
| 184 | + request_user_approval(os, agent, task).await?; |
| 185 | + } |
| 186 | + |
| 187 | + let _execution = spawn_agent_process(os, agent, task).await?; |
| 188 | + Ok(format_launch_success(agent, task)) |
| 189 | +} |
| 190 | + |
| 191 | +fn format_launch_success(agent: &str, task: &str) -> String { |
| 192 | + format!( |
| 193 | + "✓ Agent '{}' launched successfully.\nTask: {}\n\nUse 'status' operation to check progress.", |
| 194 | + agent, task |
| 195 | + ) |
| 196 | +} |
| 197 | + |
| 198 | +fn is_enabled(os: &Os) -> bool { |
| 199 | + os.database.settings.get_bool(Setting::EnabledDelegate).unwrap_or(false) |
| 200 | +} |
0 commit comments