|
| 1 | +mod agent_manager; |
| 2 | +mod agent_paths; |
| 3 | +mod errors; |
| 4 | +mod file_ops; |
| 5 | +mod process; |
| 6 | +mod status; |
| 7 | +mod types; |
| 8 | +mod ui; |
| 9 | + |
| 10 | +// Re-export types for external use |
| 11 | +use std::io::Write; |
| 12 | + |
| 13 | +use agent_manager::{ |
| 14 | + request_user_approval, |
| 15 | + validate_agent_availability, |
| 16 | +}; |
| 17 | +use eyre::{ |
| 18 | + Result, |
| 19 | + eyre, |
| 20 | +}; |
| 21 | +use process::{ |
| 22 | + format_launch_success, |
| 23 | + spawn_agent_process, |
| 24 | + start_monitoring, |
| 25 | +}; |
| 26 | +use serde::{ |
| 27 | + Deserialize, |
| 28 | + Serialize, |
| 29 | +}; |
| 30 | +use status::{ |
| 31 | + status_agent, |
| 32 | + status_all_agents, |
| 33 | +}; |
| 34 | +#[allow(unused_imports)] |
| 35 | +pub use types::{ |
| 36 | + AgentConfig, |
| 37 | + AgentExecution, |
| 38 | + AgentStatus, |
| 39 | +}; |
| 40 | + |
| 41 | +use crate::cli::chat::tools::{ |
| 42 | + InvokeOutput, |
| 43 | + OutputKind, |
| 44 | +}; |
| 45 | +use crate::database::settings::Setting; |
| 46 | +use crate::os::Os; |
| 47 | + |
| 48 | +const OPERATION_LAUNCH: &str = "launch"; |
| 49 | +const OPERATION_STATUS: &str = "status"; |
| 50 | +const DEFAULT_AGENT: &str = "default"; |
| 51 | +const ALL_AGENTS: &str = "all"; |
| 52 | + |
| 53 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 54 | +pub struct Delegate { |
| 55 | + /// Operation to perform: launch or status |
| 56 | + pub operation: String, |
| 57 | + /// Agent name to use (optional - uses "default_agent" if not specified) |
| 58 | + #[serde(default)] |
| 59 | + pub agent: Option<String>, |
| 60 | + /// Task description (required for launch operation) |
| 61 | + #[serde(default)] |
| 62 | + pub task: Option<String>, |
| 63 | +} |
| 64 | + |
| 65 | +impl Delegate { |
| 66 | + pub async fn invoke(&self, os: &Os, _stdout: &mut impl Write) -> Result<InvokeOutput> { |
| 67 | + if !is_enabled(os) { |
| 68 | + return Ok(InvokeOutput { |
| 69 | + output: OutputKind::Text( |
| 70 | + "Delegate tool is experimental and not enabled. Use /experiment to enable it.".to_string(), |
| 71 | + ), |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + let agent_name = self.get_agent_name(); |
| 76 | + let result = match self.operation.as_str() { |
| 77 | + OPERATION_LAUNCH => { |
| 78 | + let task = self |
| 79 | + .task |
| 80 | + .as_ref() |
| 81 | + .ok_or_else(|| eyre!("Task description required for launch operation"))?; |
| 82 | + launch_agent(os, agent_name, task).await? |
| 83 | + }, |
| 84 | + OPERATION_STATUS => { |
| 85 | + if agent_name == ALL_AGENTS { |
| 86 | + status_all_agents(os).await? |
| 87 | + } else { |
| 88 | + status_agent(os, agent_name).await? |
| 89 | + } |
| 90 | + }, |
| 91 | + _ => { |
| 92 | + return Err(eyre!( |
| 93 | + "Invalid operation. Use: {} or {}", |
| 94 | + OPERATION_LAUNCH, |
| 95 | + OPERATION_STATUS |
| 96 | + )); |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + Ok(InvokeOutput { |
| 101 | + output: OutputKind::Text(result), |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + pub fn queue_description(&self, output: &mut impl Write) -> Result<()> { |
| 106 | + let agent_name = self.get_agent_name(); |
| 107 | + match self.operation.as_str() { |
| 108 | + OPERATION_LAUNCH => writeln!(output, "Launching agent '{}'", agent_name)?, |
| 109 | + OPERATION_STATUS => writeln!(output, "Checking status of agent '{}'", agent_name)?, |
| 110 | + _ => writeln!( |
| 111 | + output, |
| 112 | + "Delegate operation '{}' on agent '{}'", |
| 113 | + self.operation, agent_name |
| 114 | + )?, |
| 115 | + } |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | + |
| 119 | + fn get_agent_name(&self) -> &str { |
| 120 | + match self.operation.as_str() { |
| 121 | + OPERATION_LAUNCH => self.agent.as_deref().unwrap_or(DEFAULT_AGENT), |
| 122 | + OPERATION_STATUS => self.agent.as_deref().unwrap_or(ALL_AGENTS), |
| 123 | + _ => self.agent.as_deref().unwrap_or(DEFAULT_AGENT), |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +async fn launch_agent(os: &Os, agent: &str, task: &str) -> Result<String> { |
| 129 | + validate_agent_availability(os, agent).await?; |
| 130 | + request_user_approval(os, agent, task).await?; |
| 131 | + let execution = spawn_agent_process(os, agent, task).await?; |
| 132 | + start_monitoring(execution, os.clone()).await; |
| 133 | + Ok(format_launch_success(agent, task)) |
| 134 | +} |
| 135 | + |
| 136 | +fn is_enabled(os: &Os) -> bool { |
| 137 | + os.database.settings.get_bool(Setting::EnabledDelegate).unwrap_or(false) |
| 138 | +} |
0 commit comments