| 1 | //! Host adapters for the config command group (FEAT-028 Phase 4). |
| 2 | //! |
| 3 | //! Sole host owner of concrete machinery for the config-group commands. Each |
| 4 | //! delegate calls the existing command implementation and converts its TUI |
| 5 | //! `CommandResult` (message + `AppAction`) into a portable `ConfigReply` |
| 6 | //! (message + `ConfigAction`). The portable handlers depend only on the |
| 7 | //! `CommandConfigContext`/`CommandPetContext`/`CommandTrustContext` facets. |
| 8 | |
| 9 | use super::SharedCommandHost; |
| 10 | use codewhale_command_contract::config::{ |
| 11 | CommandAccountState, CommandClaudeApplyOutcome, CommandClaudeArtifacts, CommandClaudePlan, |
| 12 | CommandConfigContext, CommandLoginContext, CommandLoginStatus, CommandPetContext, |
| 13 | CommandPetLabel, CommandTrustContext, CommandWorkSurfaceContext, CommandWorkflowSummary, |
| 14 | ConfigReply, TrustPathError, |
| 15 | }; |
| 16 | use codewhale_command_contract::types::{CommandRailPanel, CommandWorkSurfacePlacement}; |
| 17 | use std::path::PathBuf; |
| 18 | |
| 19 | pub(crate) struct ConfigAdapter<'a> { |
| 20 | pub(crate) host: SharedCommandHost<'a>, |
| 21 | } |
| 22 | |
| 23 | impl CommandConfigContext for ConfigAdapter<'_> { |
| 24 | fn mcp_config_path(&self) -> PathBuf { |
| 25 | self.host.app.borrow().mcp_config_path.clone() |
| 26 | } |
| 27 | |
| 28 | fn hugging_face_mcp_server_name(&self) -> Result<Option<String>, String> { |
| 29 | let config = codewhale_engine::mcp::load_config(&self.mcp_config_path()) |
| 30 | .map_err(|error| error.to_string())?; |
| 31 | Ok(configured_hf_mcp_server(&config).map(str::to_owned)) |
| 32 | } |
| 33 | |
| 34 | fn translation_enabled(&self) -> bool { |
| 35 | self.host.app.borrow().translation_enabled |
| 36 | } |
| 37 | |
| 38 | fn set_translation_enabled(&mut self, enabled: bool) { |
| 39 | self.host.app.borrow_mut().translation_enabled = enabled; |
| 40 | } |
| 41 | |
| 42 | fn config_path(&self) -> Option<PathBuf> { |
| 43 | self.host.app.borrow().config_path.clone() |
| 44 | } |
| 45 | |
| 46 | fn audit( |
| 47 | &mut self, |
| 48 | ) -> Result<codewhale_command_contract::config::CommandConfigAuditReport, String> { |
| 49 | let app = self.host.app.borrow(); |
| 50 | crate::commands::config_main_host::config_editability_audit(&app) |
| 51 | } |
| 52 | |
| 53 | fn workflow_summary(&mut self) -> CommandWorkflowSummary { |
| 54 | let app = self.host.app.borrow(); |
| 55 | crate::commands::config_main_host::workflow_summary(&app) |
| 56 | } |
| 57 | |
| 58 | fn subagents_status( |
| 59 | &mut self, |
| 60 | ) -> Result<codewhale_command_contract::config::CommandSubagentsReport, String> { |
| 61 | let app = self.host.app.borrow(); |
| 62 | crate::commands::config_main_host::subagents_status(&app) |
| 63 | } |
| 64 | |
| 65 | fn subagent(&mut self, key: &str) -> ConfigReply { |
| 66 | let app = self.host.app.borrow(); |
| 67 | crate::commands::config_main_host::show_subagents_setting(&app, key) |
| 68 | } |
| 69 | |
| 70 | fn set_subagent(&mut self, key: &str, value: &str, persist: bool) -> ConfigReply { |
| 71 | let mut app = self.host.app.borrow_mut(); |
| 72 | crate::commands::config_main_host::set_subagents_config_value(&mut app, key, value, persist) |
| 73 | } |
| 74 | |
| 75 | fn search_provider_status(&mut self) -> ConfigReply { |
| 76 | let app = self.host.app.borrow(); |
| 77 | crate::commands::config_main_host::show_single_setting(&app, "search.provider") |
| 78 | } |
| 79 | |
| 80 | fn search_provider_names(&self) -> &'static str { |
| 81 | codewhale_runtime::config::SearchProvider::names_hint() |
| 82 | } |
| 83 | |
| 84 | fn set_search_provider(&mut self, value: &str, persist: bool) -> ConfigReply { |
| 85 | let mut app = self.host.app.borrow_mut(); |
| 86 | crate::commands::config_main_host::set_search_provider(&mut app, value, persist) |
| 87 | } |
| 88 | |
| 89 | fn notifications_status(&mut self) -> ConfigReply { |
| 90 | let app = self.host.app.borrow(); |
| 91 | crate::commands::config_main_host::show_notifications_status(&app) |
| 92 | } |
| 93 | |
| 94 | fn notification(&mut self, key: &str) -> ConfigReply { |
| 95 | let app = self.host.app.borrow(); |
| 96 | crate::commands::config_main_host::show_notifications_setting(&app, key) |
| 97 | } |
| 98 | |
| 99 | fn set_notification(&mut self, key: &str, value: &str, persist: bool) -> ConfigReply { |
| 100 | let mut app = self.host.app.borrow_mut(); |
| 101 | crate::commands::config_main_host::set_notifications_value(&mut app, key, value, persist) |
| 102 | } |
| 103 | |
| 104 | fn apply_preset(&mut self, name: &str, persist: bool) -> ConfigReply { |
| 105 | let mut app = self.host.app.borrow_mut(); |
| 106 | crate::commands::config_main_host::apply_preset(&mut app, name, persist) |
| 107 | } |
| 108 | |
| 109 | fn get_value(&mut self, key: &str) -> ConfigReply { |
| 110 | let app = self.host.app.borrow(); |
| 111 | crate::commands::config_main_host::show_single_setting(&app, key) |
| 112 | } |
| 113 | |
| 114 | fn set_value(&mut self, key: &str, value: &str, persist: bool) -> ConfigReply { |
| 115 | let mut app = self.host.app.borrow_mut(); |
| 116 | crate::commands::config_main_host::set_config_value(&mut app, key, value, persist) |
| 117 | } |
| 118 | |
| 119 | fn show_settings( |
| 120 | &mut self, |
| 121 | ) -> Result<codewhale_command_contract::config::CommandSettingsReport, String> { |
| 122 | let mut app = self.host.app.borrow_mut(); |
| 123 | crate::commands::config_main_host::show_settings(&mut app) |
| 124 | } |
| 125 | |
| 126 | fn theme_schema(&self) -> String { |
| 127 | codewhale_palette::user_theme_schema_json().to_string() |
| 128 | } |
| 129 | |
| 130 | fn themes_dir(&self) -> Result<String, String> { |
| 131 | codewhale_palette::user_themes_dir().map(|path| path.display().to_string()) |
| 132 | } |
| 133 | |
| 134 | fn verbose_enabled(&self) -> bool { |
| 135 | self.host.app.borrow().verbose_transcript |
| 136 | } |
| 137 | |
| 138 | fn set_verbose_enabled(&mut self, enabled: bool) { |
| 139 | let mut app = self.host.app.borrow_mut(); |
| 140 | app.verbose_transcript = enabled; |
| 141 | app.mark_history_updated(); |
| 142 | } |
| 143 | |
| 144 | fn build_plan(&mut self) -> Option<CommandClaudePlan> { |
| 145 | let mut app = self.host.app.borrow_mut(); |
| 146 | crate::commands::config_main_import_host::build_plan(&mut app) |
| 147 | } |
| 148 | |
| 149 | fn write_artifacts(&mut self, report: &str, bundle: Option<&str>) -> CommandClaudeArtifacts { |
| 150 | crate::commands::config_main_import_host::write_artifacts(report, bundle) |
| 151 | } |
| 152 | |
| 153 | fn apply_import( |
| 154 | &mut self, |
| 155 | plan: &CommandClaudePlan, |
| 156 | report_path: &std::path::Path, |
| 157 | wrote_report: bool, |
| 158 | ) -> CommandClaudeApplyOutcome { |
| 159 | let mut app = self.host.app.borrow_mut(); |
| 160 | crate::commands::config_main_import_host::apply_plan( |
| 161 | &mut app, |
| 162 | plan, |
| 163 | report_path, |
| 164 | wrote_report, |
| 165 | ) |
| 166 | } |
| 167 | |
| 168 | fn display_path(&self, path: &std::path::Path) -> String { |
| 169 | codewhale_util::utils::display_path(path).to_string() |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | pub(crate) struct PetAdapter<'a> { |
| 174 | pub(crate) host: SharedCommandHost<'a>, |
| 175 | } |
| 176 | |
| 177 | impl CommandPetContext for PetAdapter<'_> { |
| 178 | fn enabled(&self) -> bool { |
| 179 | self.host.app.borrow().pet_watch.enabled |
| 180 | } |
| 181 | |
| 182 | fn set_enabled(&mut self, enabled: bool, persist: bool) { |
| 183 | let mut app = self.host.app.borrow_mut(); |
| 184 | crate::tui::pet_watch::set_enabled(&mut app, enabled); |
| 185 | if persist { |
| 186 | app.startup_defaults |
| 187 | .spawn(crate::tui::startup_defaults::StartupDefaults::pet_mode( |
| 188 | enabled, |
| 189 | )); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | fn still(&self) -> bool { |
| 194 | self.host.app.borrow().pet_watch.still() |
| 195 | } |
| 196 | |
| 197 | fn set_still(&mut self, still: bool) { |
| 198 | let mut app = self.host.app.borrow_mut(); |
| 199 | app.pet_watch.set_still(still); |
| 200 | app.needs_redraw = true; |
| 201 | } |
| 202 | |
| 203 | fn set_name(&mut self, name: &str) { |
| 204 | let mut app = self.host.app.borrow_mut(); |
| 205 | app.pet_watch.set_name(Some(name.to_owned())); |
| 206 | app.startup_defaults |
| 207 | .spawn(crate::tui::startup_defaults::StartupDefaults::whale_name( |
| 208 | name, |
| 209 | )); |
| 210 | app.needs_redraw = true; |
| 211 | } |
| 212 | |
| 213 | fn set_color(&mut self, color: Option<&str>) { |
| 214 | let mut app = self.host.app.borrow_mut(); |
| 215 | app.pet_watch.set_color(color); |
| 216 | app.startup_defaults |
| 217 | .spawn(crate::tui::startup_defaults::StartupDefaults::whale_color( |
| 218 | color, |
| 219 | )); |
| 220 | app.needs_redraw = true; |
| 221 | } |
| 222 | |
| 223 | fn label(&self, which: CommandPetLabel) -> String { |
| 224 | use codewhale_localization::MessageId; |
| 225 | let app = self.host.app.borrow(); |
| 226 | let id = match which { |
| 227 | CommandPetLabel::ModeOn => MessageId::PetModeOn, |
| 228 | CommandPetLabel::ModeOff => MessageId::PetModeOff, |
| 229 | CommandPetLabel::StillOn => MessageId::WhaleStillOn, |
| 230 | CommandPetLabel::StillOff => MessageId::WhaleStillOff, |
| 231 | CommandPetLabel::Named => MessageId::WhaleNamed, |
| 232 | CommandPetLabel::Colored => MessageId::WhaleColored, |
| 233 | CommandPetLabel::ColorReset => MessageId::WhaleColorReset, |
| 234 | }; |
| 235 | codewhale_localization::tr(app.ui_locale, id).into_owned() |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | pub(crate) struct TrustAdapter<'a> { |
| 240 | pub(crate) host: SharedCommandHost<'a>, |
| 241 | } |
| 242 | |
| 243 | impl CommandTrustContext for TrustAdapter<'_> { |
| 244 | fn trust_mode(&self) -> bool { |
| 245 | self.host.app.borrow().trust_mode |
| 246 | } |
| 247 | |
| 248 | fn trusted_paths(&self) -> Vec<PathBuf> { |
| 249 | let workspace = self.host.app.borrow().workspace.clone(); |
| 250 | codewhale_runtime::workspace_trust::WorkspaceTrust::load_for(&workspace) |
| 251 | .paths() |
| 252 | .to_vec() |
| 253 | } |
| 254 | |
| 255 | fn add_trusted_path(&mut self, raw: &str) -> Result<PathBuf, TrustPathError> { |
| 256 | let workspace = self.host.app.borrow().workspace.clone(); |
| 257 | let path = PathBuf::from(crate::commands::config_main_host::expand_tilde(raw)); |
| 258 | if !path.exists() { |
| 259 | return Err(TrustPathError::NotFound(path)); |
| 260 | } |
| 261 | codewhale_runtime::workspace_trust::add(&workspace, &path) |
| 262 | .map_err(|err| TrustPathError::UpdateFailed(err.to_string())) |
| 263 | } |
| 264 | |
| 265 | fn remove_trusted_path(&mut self, raw: &str) -> Result<(PathBuf, bool), TrustPathError> { |
| 266 | let workspace = self.host.app.borrow().workspace.clone(); |
| 267 | let path = PathBuf::from(crate::commands::config_main_host::expand_tilde(raw)); |
| 268 | codewhale_runtime::workspace_trust::remove(&workspace, &path) |
| 269 | .map(|removed| (path, removed)) |
| 270 | .map_err(|err| TrustPathError::UpdateFailed(err.to_string())) |
| 271 | } |
| 272 | |
| 273 | fn usage_label(&self) -> String { |
| 274 | let app = self.host.app.borrow(); |
| 275 | codewhale_localization::tr( |
| 276 | app.ui_locale, |
| 277 | codewhale_localization::MessageId::HelpUsageLabel, |
| 278 | ) |
| 279 | .into_owned() |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | pub(crate) struct LoginAdapter<'a> { |
| 284 | pub(crate) host: SharedCommandHost<'a>, |
| 285 | } |
| 286 | |
| 287 | fn is_enabled_plugin_provider(app: &crate::tui::app::App, provider: &str) -> bool { |
| 288 | app.plugin_registry |
| 289 | .active_plugins() |
| 290 | .into_iter() |
| 291 | .any(|plugin| plugin.manifest.providers.contains_key(provider)) |
| 292 | } |
| 293 | |
| 294 | impl CommandLoginContext for LoginAdapter<'_> { |
| 295 | fn login_status(&self) -> CommandLoginStatus { |
| 296 | use codewhale_secrets::account::{ |
| 297 | AccountSessionState, AccountSessionStore, secure_account_session_secrets, |
| 298 | }; |
| 299 | let app = self.host.app.borrow(); |
| 300 | let api_base = match codewhale_account_cli::account_api_base(None) { |
| 301 | Ok(base) => base, |
| 302 | Err(error) => { |
| 303 | return CommandLoginStatus { |
| 304 | account: CommandAccountState::Unavailable(error.to_string()), |
| 305 | api_base: String::new(), |
| 306 | provider: app.provider_identity_for_persistence().to_string(), |
| 307 | sign_in_open: crate::tui::account_login::ACCOUNT_SIGN_IN_ENABLED, |
| 308 | }; |
| 309 | } |
| 310 | }; |
| 311 | let account = match secure_account_session_secrets() { |
| 312 | Ok(secrets) => { |
| 313 | match AccountSessionStore::new(secrets, app.config_profile.as_deref(), &api_base) |
| 314 | .runtime_info_at(chrono::Utc::now()) |
| 315 | { |
| 316 | Ok(info) => match info.state { |
| 317 | AccountSessionState::SignedOut => CommandAccountState::SignedOut, |
| 318 | AccountSessionState::Authenticated => CommandAccountState::Authenticated, |
| 319 | AccountSessionState::OfflineCached => CommandAccountState::OfflineCached, |
| 320 | AccountSessionState::Expired => CommandAccountState::Expired, |
| 321 | AccountSessionState::Revoked => CommandAccountState::Revoked, |
| 322 | }, |
| 323 | Err(error) => CommandAccountState::Unavailable(error.to_string()), |
| 324 | } |
| 325 | } |
| 326 | Err(error) => CommandAccountState::Unavailable(error.to_string()), |
| 327 | }; |
| 328 | CommandLoginStatus { |
| 329 | account, |
| 330 | api_base, |
| 331 | provider: app.provider_identity_for_persistence().to_string(), |
| 332 | sign_in_open: crate::tui::account_login::ACCOUNT_SIGN_IN_ENABLED, |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | fn connect( |
| 337 | &mut self, |
| 338 | provider: Option<&str>, |
| 339 | ) -> codewhale_command_contract::config::ConfigReply { |
| 340 | use codewhale_command_contract::config::ConfigReply; |
| 341 | let app = self.host.app.borrow(); |
| 342 | match provider { |
| 343 | None => ConfigReply::action( |
| 344 | codewhale_command_contract::outcome::ConfigAction::OpenProviderPicker, |
| 345 | ), |
| 346 | Some(provider) if is_enabled_plugin_provider(&app, provider) => ConfigReply::action( |
| 347 | codewhale_command_contract::outcome::ConfigAction::StartPluginLogin { |
| 348 | provider: provider.to_owned(), |
| 349 | }, |
| 350 | ), |
| 351 | Some(other) => ConfigReply::error(format!( |
| 352 | "Usage: /connect [account|<provider>]\nUnknown or inactive plugin provider: {other}{}", |
| 353 | if crate::tui::account_login::ACCOUNT_SIGN_IN_ENABLED { |
| 354 | "\nCodewhale account sign-in is `/login`." |
| 355 | } else { |
| 356 | "" |
| 357 | } |
| 358 | )), |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | fn disconnect(&mut self, provider: &str) -> codewhale_command_contract::config::ConfigReply { |
| 363 | use codewhale_command_contract::config::ConfigReply; |
| 364 | let app = self.host.app.borrow(); |
| 365 | if is_enabled_plugin_provider(&app, provider) { |
| 366 | ConfigReply::action( |
| 367 | codewhale_command_contract::outcome::ConfigAction::StartPluginLogout { |
| 368 | provider: provider.to_owned(), |
| 369 | }, |
| 370 | ) |
| 371 | } else { |
| 372 | ConfigReply::error(format!( |
| 373 | "Usage: /disconnect <provider>\nUnknown or inactive plugin provider: {provider}\nSigning out of Codewhale is `/logout`." |
| 374 | )) |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | pub(crate) struct WorkSurfaceAdapter<'a> { |
| 380 | pub(crate) host: SharedCommandHost<'a>, |
| 381 | } |
| 382 | |
| 383 | fn to_placement( |
| 384 | placement: crate::tui::work_surface::WorkSurfacePlacement, |
| 385 | ) -> CommandWorkSurfacePlacement { |
| 386 | use crate::tui::work_surface::WorkSurfacePlacement as P; |
| 387 | match placement { |
| 388 | P::Bottom => CommandWorkSurfacePlacement::Bottom, |
| 389 | P::Top => CommandWorkSurfacePlacement::Top, |
| 390 | P::Left => CommandWorkSurfacePlacement::Left, |
| 391 | P::Right => CommandWorkSurfacePlacement::Right, |
| 392 | P::Off => CommandWorkSurfacePlacement::Off, |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | fn from_placement( |
| 397 | placement: CommandWorkSurfacePlacement, |
| 398 | ) -> crate::tui::work_surface::WorkSurfacePlacement { |
| 399 | use crate::tui::work_surface::WorkSurfacePlacement as P; |
| 400 | match placement { |
| 401 | CommandWorkSurfacePlacement::Bottom => P::Bottom, |
| 402 | CommandWorkSurfacePlacement::Top => P::Top, |
| 403 | CommandWorkSurfacePlacement::Left => P::Left, |
| 404 | CommandWorkSurfacePlacement::Right => P::Right, |
| 405 | CommandWorkSurfacePlacement::Off => P::Off, |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | fn to_rail_panel(panel: crate::tui::work_surface::RailPanel) -> CommandRailPanel { |
| 410 | use crate::tui::work_surface::RailPanel as P; |
| 411 | match panel { |
| 412 | P::Agents => CommandRailPanel::Agents, |
| 413 | P::Tasks => CommandRailPanel::Tasks, |
| 414 | P::Background => CommandRailPanel::Background, |
| 415 | P::Terminal => CommandRailPanel::Terminal, |
| 416 | P::Files => CommandRailPanel::Files, |
| 417 | P::Notepad => CommandRailPanel::Notepad, |
| 418 | P::Context => CommandRailPanel::Context, |
| 419 | P::Git => CommandRailPanel::Git, |
| 420 | P::Price => CommandRailPanel::Price, |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | fn from_rail_panel(panel: CommandRailPanel) -> crate::tui::work_surface::RailPanel { |
| 425 | use crate::tui::work_surface::RailPanel as P; |
| 426 | match panel { |
| 427 | CommandRailPanel::Agents => P::Agents, |
| 428 | CommandRailPanel::Tasks => P::Tasks, |
| 429 | CommandRailPanel::Background => P::Background, |
| 430 | CommandRailPanel::Terminal => P::Terminal, |
| 431 | CommandRailPanel::Files => P::Files, |
| 432 | CommandRailPanel::Notepad => P::Notepad, |
| 433 | CommandRailPanel::Context => P::Context, |
| 434 | CommandRailPanel::Git => P::Git, |
| 435 | CommandRailPanel::Price => P::Price, |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | impl CommandWorkSurfaceContext for WorkSurfaceAdapter<'_> { |
| 440 | fn placement(&self) -> CommandWorkSurfacePlacement { |
| 441 | to_placement(self.host.app.borrow().work_surface.placement) |
| 442 | } |
| 443 | |
| 444 | fn panel(&self) -> CommandRailPanel { |
| 445 | to_rail_panel(self.host.app.borrow().work_surface.panel) |
| 446 | } |
| 447 | |
| 448 | fn effective_placement(&self) -> CommandWorkSurfacePlacement { |
| 449 | to_placement(self.host.app.borrow().work_surface.effective_placement()) |
| 450 | } |
| 451 | |
| 452 | fn area_present(&self) -> bool { |
| 453 | self.host.app.borrow().work_surface.last_area.is_some() |
| 454 | } |
| 455 | |
| 456 | fn panel_title(&self, panel: CommandRailPanel) -> String { |
| 457 | from_rail_panel(panel).title().to_string() |
| 458 | } |
| 459 | |
| 460 | fn set_placement( |
| 461 | &mut self, |
| 462 | placement: CommandWorkSurfacePlacement, |
| 463 | persist: bool, |
| 464 | ) -> ConfigReply { |
| 465 | let mut app = self.host.app.borrow_mut(); |
| 466 | app.work_surface.placement = from_placement(placement); |
| 467 | app.work_surface.focused = false; |
| 468 | if persist { |
| 469 | let result = crate::commands::config_main_host::set_config_value( |
| 470 | &mut app, |
| 471 | "work_surface_placement", |
| 472 | from_placement(placement).as_setting(), |
| 473 | true, |
| 474 | ); |
| 475 | return result; |
| 476 | } |
| 477 | ConfigReply::ok() |
| 478 | } |
| 479 | |
| 480 | fn set_panel(&mut self, panel: CommandRailPanel, persist: bool) -> ConfigReply { |
| 481 | let mut app = self.host.app.borrow_mut(); |
| 482 | crate::tui::work_surface::select_dock_panel(&mut app, from_rail_panel(panel)); |
| 483 | if persist { |
| 484 | let result = crate::commands::config_main_host::set_config_value( |
| 485 | &mut app, |
| 486 | "rail_panel", |
| 487 | from_rail_panel(panel).as_setting(), |
| 488 | true, |
| 489 | ); |
| 490 | return result; |
| 491 | } |
| 492 | ConfigReply::ok() |
| 493 | } |
| 494 | |
| 495 | fn mark_redraw(&mut self) { |
| 496 | self.host.app.borrow_mut().needs_redraw = true; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | fn configured_hf_mcp_server(config: &codewhale_mcp::config::McpConfig) -> Option<&str> { |
| 501 | config |
| 502 | .servers |
| 503 | .iter() |
| 504 | .find(|(name, server)| looks_like_hf_mcp_server(name, server)) |
| 505 | .map(|(name, _)| name.as_str()) |
| 506 | } |
| 507 | |
| 508 | fn looks_like_hf_mcp_server(name: &str, server: &codewhale_mcp::config::McpServerConfig) -> bool { |
| 509 | let compact_name: String = name |
| 510 | .chars() |
| 511 | .filter(|ch| ch.is_ascii_alphanumeric()) |
| 512 | .flat_map(|ch| ch.to_lowercase()) |
| 513 | .collect(); |
| 514 | if matches!( |
| 515 | compact_name.as_str(), |
| 516 | "huggingface" | "huggingfacemcp" | "hfmcp" | "hfmcpserver" |
| 517 | ) { |
| 518 | return true; |
| 519 | } |
| 520 | |
| 521 | server.url.as_deref().is_some_and(|url| { |
| 522 | let url = url.to_ascii_lowercase(); |
| 523 | url.contains("huggingface.co/mcp") || url.contains("huggingface.co/api/mcp") |
| 524 | }) |
| 525 | } |
| 526 | |
| 527 | #[cfg(test)] |
| 528 | mod hf_tests { |
| 529 | use crate::tui::app::{App, TuiOptions}; |
| 530 | use codewhale_runtime::config::Config; |
| 531 | use std::{fs, path::PathBuf}; |
| 532 | use tempfile::tempdir; |
| 533 | fn app_with_mcp_path(mcp_config_path: PathBuf) -> App { |
| 534 | App::new( |
| 535 | TuiOptions { |
| 536 | max_subagents: 2, |
| 537 | mcp_config_path, |
| 538 | ..crate::tui::test_support::test_tui_options(PathBuf::from(".")) |
| 539 | }, |
| 540 | &Config::default(), |
| 541 | ) |
| 542 | } |
| 543 | |
| 544 | #[test] |
| 545 | fn hf_mcp_status_detects_settings_named_server() { |
| 546 | let dir = tempdir().expect("tempdir"); |
| 547 | let path = dir.path().join("mcp.json"); |
| 548 | fs::write( |
| 549 | &path, |
| 550 | r#"{"mcpServers":{"hf-mcp-server":{"url":"https://huggingface.co/mcp"}}}"#, |
| 551 | ) |
| 552 | .expect("write mcp config"); |
| 553 | let mut app = app_with_mcp_path(path); |
| 554 | |
| 555 | let result = crate::commands::execute("/hf mcp status", &mut app); |
| 556 | |
| 557 | assert!(!result.is_error); |
| 558 | let message = result.message.expect("status message"); |
| 559 | assert!(message.contains("appears configured")); |
| 560 | assert!(message.contains("hf-mcp-server")); |
| 561 | } |
| 562 | |
| 563 | #[test] |
| 564 | fn hf_mcp_status_reports_missing_server_without_network() { |
| 565 | let dir = tempdir().expect("tempdir"); |
| 566 | let path = dir.path().join("mcp.json"); |
| 567 | fs::write(&path, r#"{"servers":{"local":{"command":"node"}}}"#).expect("write mcp config"); |
| 568 | let mut app = app_with_mcp_path(path); |
| 569 | |
| 570 | let result = crate::commands::execute("/hf mcp status", &mut app); |
| 571 | |
| 572 | assert!(!result.is_error); |
| 573 | assert!( |
| 574 | result |
| 575 | .message |
| 576 | .as_deref() |
| 577 | .unwrap_or_default() |
| 578 | .contains("not configured") |
| 579 | ); |
| 580 | } |
| 581 | } |
| 582 |