| 1 | //! `/agent` command. |
| 2 | |
| 3 | use codewhale_command_contract::handler::CommandHandler; |
| 4 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 5 | use codewhale_command_contract::outcome::{CommandResult, CoreAction}; |
| 6 | |
| 7 | const COMMAND_INFO: CommandInfo = CommandInfo { |
| 8 | name: "agent", |
| 9 | aliases: &["daili"], |
| 10 | usage: "/agent [N] <task>", |
| 11 | description_key: "cmd_agent_description", |
| 12 | }; |
| 13 | |
| 14 | pub(super) struct AgentCmd; |
| 15 | |
| 16 | impl<A: From<CoreAction>> RegisterCommand<CommandResult<A>> for AgentCmd { |
| 17 | fn info() -> &'static CommandInfo { |
| 18 | &COMMAND_INFO |
| 19 | } |
| 20 | |
| 21 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 22 | CommandHandler::Pure(agent) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn agent<A: From<CoreAction>>(arg: Option<&str>) -> CommandResult<A> { |
| 27 | if let Some(action) = parse_agent_control_action(arg) { |
| 28 | if action.action == "cancel" { |
| 29 | return CommandResult::with_message_and_action( |
| 30 | format!("Cancelling agent {}...", action.agent_id), |
| 31 | CoreAction::CancelSubAgent { |
| 32 | agent_id: action.agent_id, |
| 33 | } |
| 34 | .into(), |
| 35 | ); |
| 36 | } |
| 37 | let message = format!( |
| 38 | "Call `agent` with action `{}`, agent_id `{}`, then summarize the returned status for the user. Do not start a new agent.", |
| 39 | action.action, action.agent_id |
| 40 | ); |
| 41 | return CommandResult::with_message_and_action( |
| 42 | format!("Agent {} requested for {}.", action.action, action.agent_id), |
| 43 | CoreAction::SendMessage(message).into(), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | let (max_depth, task) = match super::util::parse_depth_prefixed_arg(arg, 1) { |
| 48 | Ok(parsed) => parsed, |
| 49 | Err(message) => return CommandResult::error(message), |
| 50 | }; |
| 51 | let task = match task { |
| 52 | Some(task) if !task.trim().is_empty() => task.trim().to_string(), |
| 53 | _ => { |
| 54 | return CommandResult::error( |
| 55 | "Usage: /agent [N] <task>\n\n\ |
| 56 | Opens a persistent sub-agent session with recursive agent depth N (0-3, default 1).", |
| 57 | ); |
| 58 | } |
| 59 | }; |
| 60 | CommandResult::with_message_and_action( |
| 61 | format!("Opening persistent sub-agent at depth {max_depth}..."), |
| 62 | CoreAction::LaunchSubAgent { task, max_depth }.into(), |
| 63 | ) |
| 64 | } |
| 65 | |
| 66 | struct AgentControlAction { |
| 67 | action: &'static str, |
| 68 | agent_id: String, |
| 69 | } |
| 70 | |
| 71 | fn parse_agent_control_action(arg: Option<&str>) -> Option<AgentControlAction> { |
| 72 | let arg = arg?.trim(); |
| 73 | let (action, rest) = arg.split_once(char::is_whitespace)?; |
| 74 | let action = match action { |
| 75 | "status" | "inspect" => "status", |
| 76 | "peek" | "progress" => "peek", |
| 77 | "cancel" | "stop" | "abort" => "cancel", |
| 78 | _ => return None, |
| 79 | }; |
| 80 | let agent_id = rest.trim(); |
| 81 | if agent_id.is_empty() || agent_id.contains(char::is_whitespace) { |
| 82 | return None; |
| 83 | } |
| 84 | Some(AgentControlAction { |
| 85 | action, |
| 86 | agent_id: agent_id.to_string(), |
| 87 | }) |
| 88 | } |
| 89 |