| 1 | use codewhale_command_contract::fleet_lane::{ |
| 2 | ControlOperationHelp, FleetAction, FleetRouteSaveKind, FleetUsageKind, |
| 3 | }; |
| 4 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 5 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 6 | use codewhale_command_contract::model_setup::resolve_action; |
| 7 | use codewhale_command_contract::outcome::CommandResult; |
| 8 | |
| 9 | const PRIMARY_VERBS: [&str; 7] = [ |
| 10 | "members", "setup", "teams", "workers", "context", "board", "help", |
| 11 | ]; |
| 12 | |
| 13 | const PRIMARY_USAGE: &str = "/fleet [members|setup|teams|workers|context|board|help]"; |
| 14 | |
| 15 | const COMMAND_INFO: CommandInfo = CommandInfo { |
| 16 | name: "fleet", |
| 17 | aliases: &[], |
| 18 | usage: PRIMARY_USAGE, |
| 19 | description_key: "cmd_fleet_description", |
| 20 | }; |
| 21 | |
| 22 | pub struct FleetCmd; |
| 23 | |
| 24 | impl<A: TryFrom<FleetAction>> RegisterCommand<CommandResult<A>> for FleetCmd |
| 25 | where |
| 26 | A::Error: std::fmt::Display, |
| 27 | { |
| 28 | fn info() -> &'static CommandInfo { |
| 29 | &COMMAND_INFO |
| 30 | } |
| 31 | |
| 32 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 33 | CommandHandler::Contextual { |
| 34 | capabilities: CommandCapabilities::FLEET, |
| 35 | handler: fleet, |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn split_verb(arg: Option<&str>) -> Option<(&str, Option<&str>)> { |
| 41 | let rest = arg.map(str::trim).filter(|value| !value.is_empty())?; |
| 42 | Some(match rest.split_once(char::is_whitespace) { |
| 43 | Some((verb, tail)) => (verb, Some(tail.trim())), |
| 44 | None => (rest, None), |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | fn help_text(operations: &[ControlOperationHelp]) -> String { |
| 49 | let mut out = format!( |
| 50 | "Usage: {PRIMARY_USAGE}\n\n\ |
| 51 | The fleet is who is working right now. The primary verbs cover the daily loop:\n\ |
| 52 | /fleet opens the mission view. /fleet members opens the roster — each member's role, model, and access; \ |
| 53 | Enter on a member row opens that member's editor. /fleet setup opens the authoring \ |
| 54 | wizard. /fleet teams (or fleets/saved/manage) switches between named saved teams. \ |
| 55 | /fleet workers (and /subagents) shows sub-agents in the current TUI session only, \ |
| 56 | which is a different set: it does not include durable fleet runs.\n\n\ |
| 57 | Advanced — team model routes (still typed, one level deeper):\n\ |
| 58 | /fleet models lists the selected team's models. \ |
| 59 | /fleet add <provider> <model> [role…] and /fleet remove <provider> <model> edit them.\n\n\ |
| 60 | Advanced — saved routes:\n\ |
| 61 | /fleet save persists the current session route into the selected team's operator. \ |
| 62 | /fleet save-as saves it as a new team.\n\n\ |
| 63 | Shared project context and task board (.codewhale/context/):\n\ |
| 64 | /fleet context [list|read <name>] inspects shared project markdown notes. \ |
| 65 | /fleet board [list|add <title>|claim <id> <owner>|done <id>] manages the local shared task board.\n\n\ |
| 66 | Durable runs — these act on the durable .codewhale/fleet.jsonl ledger for this \ |
| 67 | workspace, the same records `codewhale fleet` reads and writes. the ledger file, \ |
| 68 | saved rosters, and config tables keep the Fleet name:\n", |
| 69 | ); |
| 70 | for descriptor in operations { |
| 71 | out.push_str(&format!( |
| 72 | "\n {:<30} {:<6} {}\n CLI: {}\n", |
| 73 | descriptor.slash_invocation, |
| 74 | descriptor.authority, |
| 75 | descriptor.summary, |
| 76 | descriptor.cli_invocation |
| 77 | )); |
| 78 | } |
| 79 | out |
| 80 | } |
| 81 | |
| 82 | fn fleet<A: TryFrom<FleetAction>>( |
| 83 | mut contexts: CommandContexts<'_>, |
| 84 | arg: Option<&str>, |
| 85 | ) -> CommandResult<A> |
| 86 | where |
| 87 | A::Error: std::fmt::Display, |
| 88 | { |
| 89 | let Some((verb, target)) = split_verb(arg) else { |
| 90 | return resolve_action(FleetAction::OpenMissions, None); |
| 91 | }; |
| 92 | match verb { |
| 93 | "save" | "update" => { |
| 94 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 95 | return CommandResult::error("Command capability unavailable: fleet"); |
| 96 | }; |
| 97 | return CommandResult::message(fleet_ctx.save_route(FleetRouteSaveKind::UpdateFleet)); |
| 98 | } |
| 99 | "save-as" | "saveas" => { |
| 100 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 101 | return CommandResult::error("Command capability unavailable: fleet"); |
| 102 | }; |
| 103 | return CommandResult::message( |
| 104 | fleet_ctx.save_route(FleetRouteSaveKind::SaveAsNewFleet), |
| 105 | ); |
| 106 | } |
| 107 | _ => {} |
| 108 | } |
| 109 | match verb { |
| 110 | "models" | "model" => { |
| 111 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 112 | return CommandResult::error("Command capability unavailable: fleet"); |
| 113 | }; |
| 114 | CommandResult::message(fleet_ctx.fleet_models_text()) |
| 115 | } |
| 116 | "add" => { |
| 117 | let mut words = target.unwrap_or_default().split_whitespace(); |
| 118 | let (Some(provider), Some(model)) = (words.next(), words.next()) else { |
| 119 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 120 | return CommandResult::error("Command capability unavailable: fleet"); |
| 121 | }; |
| 122 | return CommandResult::error(fleet_ctx.usage_text(FleetUsageKind::Add)); |
| 123 | }; |
| 124 | resolve_action( |
| 125 | FleetAction::AddModel { |
| 126 | provider: provider.to_string(), |
| 127 | model: model.to_string(), |
| 128 | roles: words.map(str::to_string).collect(), |
| 129 | }, |
| 130 | None, |
| 131 | ) |
| 132 | } |
| 133 | "remove" | "rm" | "drop" => { |
| 134 | let mut words = target.unwrap_or_default().split_whitespace(); |
| 135 | let (Some(provider), Some(model)) = (words.next(), words.next()) else { |
| 136 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 137 | return CommandResult::error("Command capability unavailable: fleet"); |
| 138 | }; |
| 139 | return CommandResult::error(fleet_ctx.usage_text(FleetUsageKind::Remove)); |
| 140 | }; |
| 141 | resolve_action( |
| 142 | FleetAction::RemoveModel { |
| 143 | provider: provider.to_string(), |
| 144 | model: model.to_string(), |
| 145 | }, |
| 146 | None, |
| 147 | ) |
| 148 | } |
| 149 | "members" | "member" | "roster" | "roles" | "role" | "profiles" | "profile" => { |
| 150 | resolve_action(FleetAction::OpenRoster, None) |
| 151 | } |
| 152 | "setup" | "edit" | "new" => resolve_action(FleetAction::OpenSetup, None), |
| 153 | "teams" | "team" | "fleets" | "saved" | "manage" => { |
| 154 | resolve_action(FleetAction::OpenList, None) |
| 155 | } |
| 156 | "workers" | "worker" | "agents" | "subagents" => { |
| 157 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 158 | return CommandResult::error("Command capability unavailable: fleet"); |
| 159 | }; |
| 160 | match A::try_from(FleetAction::OpenWorkers) { |
| 161 | Ok(action) => { |
| 162 | fleet_ctx.prepare_workers_view(); |
| 163 | CommandResult::action(action) |
| 164 | } |
| 165 | Err(reason) => CommandResult::error(reason.to_string()), |
| 166 | } |
| 167 | } |
| 168 | "help" | "?" => { |
| 169 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 170 | return CommandResult::error("Command capability unavailable: fleet"); |
| 171 | }; |
| 172 | CommandResult::message(help_text(&fleet_ctx.control_operations())) |
| 173 | } |
| 174 | "context" => { |
| 175 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 176 | return CommandResult::error("Command capability unavailable: fleet"); |
| 177 | }; |
| 178 | let outcome = fleet_ctx.run_project_context(target); |
| 179 | if outcome.is_error { |
| 180 | CommandResult::error(outcome.rendered) |
| 181 | } else { |
| 182 | CommandResult::message(outcome.rendered) |
| 183 | } |
| 184 | } |
| 185 | "board" => { |
| 186 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 187 | return CommandResult::error("Command capability unavailable: fleet"); |
| 188 | }; |
| 189 | let outcome = fleet_ctx.run_project_board(target); |
| 190 | if outcome.is_error { |
| 191 | CommandResult::error(outcome.rendered) |
| 192 | } else { |
| 193 | CommandResult::message(outcome.rendered) |
| 194 | } |
| 195 | } |
| 196 | other => { |
| 197 | let Some(fleet_ctx) = contexts.take_fleet() else { |
| 198 | return CommandResult::error("Command capability unavailable: fleet"); |
| 199 | }; |
| 200 | match fleet_ctx.resolve_control_verb(other) { |
| 201 | Some(operation) => { |
| 202 | let receipt = fleet_ctx.run_control(operation, target); |
| 203 | if receipt.is_error { |
| 204 | CommandResult::error(receipt.rendered) |
| 205 | } else { |
| 206 | CommandResult::message(receipt.rendered) |
| 207 | } |
| 208 | } |
| 209 | None => CommandResult::error(format!( |
| 210 | "Unknown /fleet target '{other}'. Use {} — or /fleet help for the full verb list.", |
| 211 | PRIMARY_VERBS[..PRIMARY_VERBS.len() - 1].join(", ") |
| 212 | )), |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 |