| 1 | use super::SharedCommandHost; |
| 2 | use crate::tui::app::App; |
| 3 | use codewhale_command_contract::fleet_lane::{ |
| 4 | CommandFleetContext, CommandLaneContext, ControlOperationHelp, ControlReceiptOutcome, |
| 5 | FleetControlVerb, FleetRouteSaveKind, FleetUsageKind, LaneControlVerb, |
| 6 | }; |
| 7 | use codewhale_command_contract::navigation::{CommandNavigationContext, SubagentRosterDisplay}; |
| 8 | use codewhale_engine::fleet::control::{ |
| 9 | execute_fleet_control, execute_project_board_command, execute_project_context_command, |
| 10 | }; |
| 11 | use codewhale_lane::control::{execute_lane_control, operations_for_domain}; |
| 12 | use codewhale_lane::{ControlDomain, ControlOperation, ControlSurface}; |
| 13 | use codewhale_localization::{MessageId, tr}; |
| 14 | |
| 15 | pub(super) struct FleetAdapter<'a> { |
| 16 | pub(super) host: SharedCommandHost<'a>, |
| 17 | } |
| 18 | |
| 19 | pub(super) struct LaneAdapter<'a> { |
| 20 | pub(super) host: SharedCommandHost<'a>, |
| 21 | } |
| 22 | |
| 23 | fn control_operations_for(domain: ControlDomain) -> Vec<ControlOperationHelp> { |
| 24 | operations_for_domain(domain) |
| 25 | .into_iter() |
| 26 | .map(|descriptor| ControlOperationHelp { |
| 27 | slash_invocation: descriptor.slash_invocation(), |
| 28 | authority: descriptor.authority.as_str().to_string(), |
| 29 | summary: descriptor.summary.to_string(), |
| 30 | cli_invocation: descriptor.cli_invocation.to_string(), |
| 31 | }) |
| 32 | .collect() |
| 33 | } |
| 34 | |
| 35 | fn lane_operation(verb: LaneControlVerb) -> ControlOperation { |
| 36 | match verb { |
| 37 | LaneControlVerb::List => ControlOperation::LaneList, |
| 38 | LaneControlVerb::Status => ControlOperation::LaneStatus, |
| 39 | LaneControlVerb::Interrupt => ControlOperation::LaneInterrupt, |
| 40 | LaneControlVerb::Restart => ControlOperation::LaneRestart, |
| 41 | LaneControlVerb::Resume => ControlOperation::LaneResume, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn fleet_operation(verb: FleetControlVerb) -> ControlOperation { |
| 46 | match verb { |
| 47 | FleetControlVerb::List => ControlOperation::FleetList, |
| 48 | FleetControlVerb::Status => ControlOperation::FleetStatus, |
| 49 | FleetControlVerb::Interrupt => ControlOperation::FleetInterrupt, |
| 50 | FleetControlVerb::Restart => ControlOperation::FleetRestart, |
| 51 | FleetControlVerb::Resume => ControlOperation::FleetResume, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn fleet_models_text(app: &App) -> String { |
| 56 | use codewhale_engine::fleet::members::fleet_models; |
| 57 | let locale = app.ui_locale; |
| 58 | let models = match fleet_models(&app.workspace) { |
| 59 | Ok(models) if models.is_empty() => { |
| 60 | return tr(locale, MessageId::FleetModelsEmpty).into_owned(); |
| 61 | } |
| 62 | Ok(models) => models, |
| 63 | Err(error) => { |
| 64 | return tr(locale, MessageId::FleetModelsBroken).replace("{error}", &error.to_string()); |
| 65 | } |
| 66 | }; |
| 67 | let mut lines = vec![ |
| 68 | tr(locale, MessageId::FleetModelsHeader) |
| 69 | .replace("{fleet}", &models[0].fleet) |
| 70 | .replace("{count}", &models.len().to_string()), |
| 71 | ]; |
| 72 | for member in &models { |
| 73 | let provider = codewhale_config::ProviderKind::parse(&member.provider); |
| 74 | let facts = provider |
| 75 | .and_then(|p| { |
| 76 | codewhale_engine::provider_lake::catalog_offering_for_model(p, &member.model) |
| 77 | }) |
| 78 | .map(|row| { |
| 79 | let mut parts = Vec::new(); |
| 80 | if let Some(cost) = row.cost.as_ref() |
| 81 | && let (Some(input), Some(output)) = (cost.input, cost.output) |
| 82 | { |
| 83 | parts.push( |
| 84 | tr(locale, MessageId::FleetModelsFactPrice) |
| 85 | .replace("{input}", &format!("{input:.2}")) |
| 86 | .replace("{output}", &format!("{output:.2}")), |
| 87 | ); |
| 88 | } |
| 89 | if let Some(limit) = row.limit.as_ref() |
| 90 | && let Some(context) = limit.context |
| 91 | { |
| 92 | parts.push( |
| 93 | tr(locale, MessageId::FleetModelsFactContext) |
| 94 | .replace("{context}", &(context / 1000).to_string()), |
| 95 | ); |
| 96 | } |
| 97 | if row.tool_call == Some(true) { |
| 98 | parts.push(tr(locale, MessageId::FleetModelsFactTools).into_owned()); |
| 99 | } |
| 100 | parts.join(" · ") |
| 101 | }) |
| 102 | .filter(|facts| !facts.is_empty()) |
| 103 | .map(|facts| format!(" · {facts}")) |
| 104 | .unwrap_or_default(); |
| 105 | lines.push(format!( |
| 106 | " {}/{} · {}{facts}", |
| 107 | member.provider, |
| 108 | member.model, |
| 109 | member.roles_label() |
| 110 | )); |
| 111 | } |
| 112 | lines.push(tr(locale, MessageId::FleetModelsFooter).into_owned()); |
| 113 | lines.join("\n") |
| 114 | } |
| 115 | |
| 116 | impl CommandLaneContext for LaneAdapter<'_> { |
| 117 | fn control_operations(&self) -> Vec<ControlOperationHelp> { |
| 118 | control_operations_for(ControlDomain::Lane) |
| 119 | } |
| 120 | |
| 121 | fn resolve_control_verb(&self, verb: &str) -> Option<LaneControlVerb> { |
| 122 | match ControlOperation::parse_verb(ControlDomain::Lane, verb)? { |
| 123 | ControlOperation::LaneList => Some(LaneControlVerb::List), |
| 124 | ControlOperation::LaneStatus => Some(LaneControlVerb::Status), |
| 125 | ControlOperation::LaneInterrupt => Some(LaneControlVerb::Interrupt), |
| 126 | ControlOperation::LaneRestart => Some(LaneControlVerb::Restart), |
| 127 | ControlOperation::LaneResume => Some(LaneControlVerb::Resume), |
| 128 | _ => None, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn execute_control( |
| 133 | &mut self, |
| 134 | verb: LaneControlVerb, |
| 135 | target: Option<&str>, |
| 136 | ) -> ControlReceiptOutcome { |
| 137 | let operation = lane_operation(verb); |
| 138 | let receipt = if operation.descriptor().authority.is_write() { |
| 139 | self.host |
| 140 | .app |
| 141 | .borrow_mut() |
| 142 | .lane_control |
| 143 | .submit(operation, target, None) |
| 144 | } else { |
| 145 | execute_lane_control(ControlSurface::Slash, operation, target) |
| 146 | }; |
| 147 | ControlReceiptOutcome { |
| 148 | rendered: receipt.render(), |
| 149 | is_error: receipt.is_error(), |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl CommandFleetContext for FleetAdapter<'_> { |
| 155 | fn control_operations(&self) -> Vec<ControlOperationHelp> { |
| 156 | control_operations_for(ControlDomain::Fleet) |
| 157 | } |
| 158 | |
| 159 | fn resolve_control_verb(&self, verb: &str) -> Option<FleetControlVerb> { |
| 160 | match ControlOperation::parse_verb(ControlDomain::Fleet, verb)? { |
| 161 | ControlOperation::FleetList => Some(FleetControlVerb::List), |
| 162 | ControlOperation::FleetStatus => Some(FleetControlVerb::Status), |
| 163 | ControlOperation::FleetInterrupt => Some(FleetControlVerb::Interrupt), |
| 164 | ControlOperation::FleetRestart => Some(FleetControlVerb::Restart), |
| 165 | ControlOperation::FleetResume => Some(FleetControlVerb::Resume), |
| 166 | _ => None, |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | fn run_control( |
| 171 | &mut self, |
| 172 | verb: FleetControlVerb, |
| 173 | target: Option<&str>, |
| 174 | ) -> ControlReceiptOutcome { |
| 175 | let receipt = execute_fleet_control( |
| 176 | ControlSurface::Slash, |
| 177 | &self.host.app.borrow().workspace, |
| 178 | fleet_operation(verb), |
| 179 | target, |
| 180 | ); |
| 181 | ControlReceiptOutcome { |
| 182 | rendered: receipt.render(), |
| 183 | is_error: receipt.is_error(), |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | fn fleet_models_text(&self) -> String { |
| 188 | fleet_models_text(&self.host.app.borrow()) |
| 189 | } |
| 190 | |
| 191 | fn save_route(&mut self, kind: FleetRouteSaveKind) -> String { |
| 192 | let choice = match kind { |
| 193 | FleetRouteSaveKind::UpdateFleet => { |
| 194 | crate::tui::views::route_save_prompt::RouteSaveChoice::UpdateFleet |
| 195 | } |
| 196 | FleetRouteSaveKind::SaveAsNewFleet => { |
| 197 | crate::tui::views::route_save_prompt::RouteSaveChoice::SaveAsNewFleet |
| 198 | } |
| 199 | }; |
| 200 | self.host.app.borrow_mut().apply_route_save_choice(choice) |
| 201 | } |
| 202 | |
| 203 | fn usage_text(&self, kind: FleetUsageKind) -> String { |
| 204 | let id = match kind { |
| 205 | FleetUsageKind::Add => MessageId::FleetAddUsage, |
| 206 | FleetUsageKind::Remove => MessageId::FleetRemoveUsage, |
| 207 | }; |
| 208 | tr(self.host.app.borrow().ui_locale, id).into_owned() |
| 209 | } |
| 210 | |
| 211 | fn prepare_workers_view(&mut self) { |
| 212 | super::NavigationAdapter { |
| 213 | host: self.host.clone(), |
| 214 | } |
| 215 | .prepare_subagent_roster(SubagentRosterDisplay::Modal); |
| 216 | } |
| 217 | |
| 218 | fn run_project_context(&mut self, args: Option<&str>) -> ControlReceiptOutcome { |
| 219 | let workspace = self.host.app.borrow().workspace.clone(); |
| 220 | match execute_project_context_command(&workspace, args) { |
| 221 | Ok(rendered) => ControlReceiptOutcome { |
| 222 | rendered, |
| 223 | is_error: false, |
| 224 | }, |
| 225 | Err(rendered) => ControlReceiptOutcome { |
| 226 | rendered, |
| 227 | is_error: true, |
| 228 | }, |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | fn run_project_board(&mut self, args: Option<&str>) -> ControlReceiptOutcome { |
| 233 | let workspace = self.host.app.borrow().workspace.clone(); |
| 234 | match execute_project_board_command(&workspace, args) { |
| 235 | Ok(rendered) => ControlReceiptOutcome { |
| 236 | rendered, |
| 237 | is_error: false, |
| 238 | }, |
| 239 | Err(rendered) => ControlReceiptOutcome { |
| 240 | rendered, |
| 241 | is_error: true, |
| 242 | }, |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 |