| 1 | use crate::tui::app::StatusToastLevel; |
| 2 | use std::cmp::Ordering; |
| 3 | use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 4 | use std::sync::Arc; |
| 5 | |
| 6 | use anyhow::Result; |
| 7 | use codewhale_config::AppMode; |
| 8 | |
| 9 | use crate::commands::{self, CommandInfo, CommandResult}; |
| 10 | use crate::tui::app::{App, AppAction}; |
| 11 | use crate::tui::command_palette::{ |
| 12 | CommandPaletteView, build_entries_with_plugins as build_command_palette_entries, |
| 13 | }; |
| 14 | use codewhale_config::ProviderKind; |
| 15 | use codewhale_engine::provider_lake::models_for_provider; |
| 16 | use codewhale_localization::{Locale, MessageId, tr}; |
| 17 | use codewhale_runtime::config::{Config, ProviderIdentity}; |
| 18 | |
| 19 | pub const HOTBAR_COMPACT_LABEL_MAX_WIDTH: usize = 7; |
| 20 | |
| 21 | /// Result of firing a hotbar action. |
| 22 | #[allow(clippy::large_enum_variant)] // AppAction is intentionally large; boxing would force clone churn on the hot path |
| 23 | #[derive(Debug, Clone, PartialEq)] |
| 24 | pub enum HotbarDispatch { |
| 25 | /// The action was fully handled by mutating [`App`]. |
| 26 | Handled, |
| 27 | /// The event loop must handle an existing application action. |
| 28 | AppAction(AppAction), |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 32 | pub enum HotbarActionCategory { |
| 33 | App, |
| 34 | Route, |
| 35 | Slash, |
| 36 | Mcp, |
| 37 | Skill, |
| 38 | Plugin, |
| 39 | } |
| 40 | |
| 41 | impl HotbarActionCategory { |
| 42 | #[must_use] |
| 43 | pub const fn as_str(self) -> &'static str { |
| 44 | match self { |
| 45 | Self::App => "app", |
| 46 | Self::Route => "route", |
| 47 | Self::Slash => "slash", |
| 48 | Self::Mcp => "mcp", |
| 49 | Self::Skill => "skill", |
| 50 | Self::Plugin => "plugin", |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | #[must_use] |
| 56 | pub fn parse(value: &str) -> Option<Self> { |
| 57 | match value { |
| 58 | "app" => Some(Self::App), |
| 59 | "route" => Some(Self::Route), |
| 60 | "slash" => Some(Self::Slash), |
| 61 | "mcp" => Some(Self::Mcp), |
| 62 | "skill" => Some(Self::Skill), |
| 63 | "plugin" => Some(Self::Plugin), |
| 64 | _ => None, |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 70 | pub enum HotbarArgsBehavior { |
| 71 | None, |
| 72 | Optional, |
| 73 | Required, |
| 74 | } |
| 75 | |
| 76 | impl HotbarArgsBehavior { |
| 77 | #[must_use] |
| 78 | fn for_command(info: &CommandInfo) -> Self { |
| 79 | if info.requires_required_argument() { |
| 80 | Self::Required |
| 81 | } else if info.requires_argument() { |
| 82 | Self::Optional |
| 83 | } else { |
| 84 | Self::None |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 90 | pub enum HotbarSafetyClass { |
| 91 | LocalUi, |
| 92 | LocalState, |
| 93 | ConfigChange, |
| 94 | ExternalInput, |
| 95 | ExistingCommand, |
| 96 | RequiresApproval, |
| 97 | } |
| 98 | |
| 99 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 100 | pub enum HotbarRecommendation { |
| 101 | Default, |
| 102 | Eligible, |
| 103 | Advanced, |
| 104 | } |
| 105 | |
| 106 | impl HotbarRecommendation { |
| 107 | #[must_use] |
| 108 | pub const fn is_recommendable(self) -> bool { |
| 109 | matches!(self, Self::Default | Self::Eligible) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 114 | pub struct HotbarActionMetadata { |
| 115 | pub id: String, |
| 116 | pub source_id: String, |
| 117 | pub display_name: String, |
| 118 | pub compact_label: String, |
| 119 | pub description: String, |
| 120 | pub category: HotbarActionCategory, |
| 121 | pub args: HotbarArgsBehavior, |
| 122 | pub safety: HotbarSafetyClass, |
| 123 | pub recommendation: HotbarRecommendation, |
| 124 | } |
| 125 | |
| 126 | impl HotbarActionMetadata { |
| 127 | #[cfg(test)] |
| 128 | #[must_use] |
| 129 | pub fn validation_errors(&self) -> Vec<String> { |
| 130 | let mut errors = Vec::new(); |
| 131 | if self.id.trim().is_empty() { |
| 132 | errors.push("id must not be empty".to_string()); |
| 133 | } |
| 134 | if self.source_id.trim().is_empty() { |
| 135 | errors.push(format!("{} source_id must not be empty", self.id)); |
| 136 | } |
| 137 | if self.display_name.trim().is_empty() { |
| 138 | errors.push(format!("{} display_name must not be empty", self.id)); |
| 139 | } |
| 140 | if self.compact_label.trim().is_empty() { |
| 141 | errors.push(format!("{} compact_label must not be empty", self.id)); |
| 142 | } |
| 143 | if unicode_width::UnicodeWidthStr::width(self.compact_label.as_str()) |
| 144 | > HOTBAR_COMPACT_LABEL_MAX_WIDTH |
| 145 | { |
| 146 | errors.push(format!( |
| 147 | "{} compact_label {:?} exceeds {} display cells", |
| 148 | self.id, self.compact_label, HOTBAR_COMPACT_LABEL_MAX_WIDTH |
| 149 | )); |
| 150 | } |
| 151 | if self.description.trim().is_empty() { |
| 152 | errors.push(format!("{} description must not be empty", self.id)); |
| 153 | } |
| 154 | errors |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 159 | pub struct HotbarRecommendationEntry { |
| 160 | pub metadata: HotbarActionMetadata, |
| 161 | pub disabled_reason: Option<String>, |
| 162 | } |
| 163 | |
| 164 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 165 | pub struct HotbarRecommendationOptions { |
| 166 | pub max_total: usize, |
| 167 | pub max_eligible_per_category: usize, |
| 168 | pub include_required_args: bool, |
| 169 | } |
| 170 | |
| 171 | impl HotbarRecommendationOptions { |
| 172 | #[must_use] |
| 173 | pub const fn for_setup_wizard() -> Self { |
| 174 | Self { |
| 175 | max_total: usize::MAX, |
| 176 | max_eligible_per_category: usize::MAX, |
| 177 | include_required_args: false, |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | impl Default for HotbarRecommendationOptions { |
| 183 | fn default() -> Self { |
| 184 | Self { |
| 185 | max_total: usize::from(codewhale_config::HOTBAR_SLOT_COUNT), |
| 186 | max_eligible_per_category: usize::MAX, |
| 187 | include_required_args: false, |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 193 | pub enum HotbarSourceDispatchBoundary { |
| 194 | /// The action is handled directly by existing in-app state mutation. |
| 195 | DirectApp, |
| 196 | /// The action routes through the existing provider/model picker apply path. |
| 197 | ModelRoute, |
| 198 | /// The action routes through the slash command registry/dispatcher. |
| 199 | SlashCommand, |
| 200 | /// The action only prefills the composer with a reference; nothing |
| 201 | /// executes until the user reviews and sends the message themselves. |
| 202 | ComposerPrefill, |
| 203 | /// The source is visible as a future hotbar source, but binding/dispatch is |
| 204 | /// intentionally deferred until its safety contract is wired. |
| 205 | Deferred, |
| 206 | } |
| 207 | |
| 208 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 209 | pub enum HotbarSourceSafetyMode { |
| 210 | /// Pressing the bound hotbar slot directly fires the existing action path. |
| 211 | DirectFire, |
| 212 | /// Pressing the bound hotbar slot opens/prefills the composer for arguments. |
| 213 | ComposerPrefill, |
| 214 | /// The source must not register bindable actions until its gates are wired. |
| 215 | Disabled, |
| 216 | /// The source may dispatch only through an approval/trust-enforced path. |
| 217 | ApprovalGated, |
| 218 | } |
| 219 | |
| 220 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 221 | pub struct HotbarSourceDescriptor { |
| 222 | pub category: HotbarActionCategory, |
| 223 | pub boundary: HotbarSourceDispatchBoundary, |
| 224 | pub safety_modes: &'static [HotbarSourceSafetyMode], |
| 225 | pub dispatch_path: &'static str, |
| 226 | pub status: &'static str, |
| 227 | } |
| 228 | |
| 229 | impl HotbarSourceDescriptor { |
| 230 | #[must_use] |
| 231 | pub fn registers_dispatchable_actions(self) -> bool { |
| 232 | self.boundary != HotbarSourceDispatchBoundary::Deferred |
| 233 | && !self |
| 234 | .safety_modes |
| 235 | .contains(&HotbarSourceSafetyMode::Disabled) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | const HOTBAR_DIRECT_APP_SAFETY: &[HotbarSourceSafetyMode] = &[HotbarSourceSafetyMode::DirectFire]; |
| 240 | const HOTBAR_ROUTE_SAFETY: &[HotbarSourceSafetyMode] = &[HotbarSourceSafetyMode::DirectFire]; |
| 241 | const HOTBAR_SLASH_SAFETY: &[HotbarSourceSafetyMode] = &[ |
| 242 | HotbarSourceSafetyMode::DirectFire, |
| 243 | HotbarSourceSafetyMode::ComposerPrefill, |
| 244 | ]; |
| 245 | const HOTBAR_MCP_SAFETY: &[HotbarSourceSafetyMode] = &[HotbarSourceSafetyMode::ComposerPrefill]; |
| 246 | const HOTBAR_SKILL_SAFETY: &[HotbarSourceSafetyMode] = &[HotbarSourceSafetyMode::DirectFire]; |
| 247 | const HOTBAR_DEFERRED_SAFETY: &[HotbarSourceSafetyMode] = &[ |
| 248 | HotbarSourceSafetyMode::Disabled, |
| 249 | HotbarSourceSafetyMode::ApprovalGated, |
| 250 | ]; |
| 251 | |
| 252 | const HOTBAR_SOURCE_DESCRIPTORS: &[HotbarSourceDescriptor] = &[ |
| 253 | HotbarSourceDescriptor { |
| 254 | category: HotbarActionCategory::App, |
| 255 | boundary: HotbarSourceDispatchBoundary::DirectApp, |
| 256 | safety_modes: HOTBAR_DIRECT_APP_SAFETY, |
| 257 | dispatch_path: "AppHotbarAction::dispatch", |
| 258 | status: "dispatchable", |
| 259 | }, |
| 260 | HotbarSourceDescriptor { |
| 261 | category: HotbarActionCategory::Route, |
| 262 | boundary: HotbarSourceDispatchBoundary::ModelRoute, |
| 263 | safety_modes: HOTBAR_ROUTE_SAFETY, |
| 264 | dispatch_path: "AppAction::SwitchModelRoute -> apply_model_picker_choice", |
| 265 | status: "dispatchable", |
| 266 | }, |
| 267 | HotbarSourceDescriptor { |
| 268 | category: HotbarActionCategory::Slash, |
| 269 | boundary: HotbarSourceDispatchBoundary::SlashCommand, |
| 270 | safety_modes: HOTBAR_SLASH_SAFETY, |
| 271 | dispatch_path: "commands::execute or composer prefill for required arguments", |
| 272 | status: "dispatchable", |
| 273 | }, |
| 274 | HotbarSourceDescriptor { |
| 275 | category: HotbarActionCategory::Mcp, |
| 276 | boundary: HotbarSourceDispatchBoundary::ComposerPrefill, |
| 277 | safety_modes: HOTBAR_MCP_SAFETY, |
| 278 | dispatch_path: "composer prefill of the MCP tool reference; execution stays behind the \ |
| 279 | existing tool approval flow", |
| 280 | status: "dispatchable", |
| 281 | }, |
| 282 | HotbarSourceDescriptor { |
| 283 | category: HotbarActionCategory::Skill, |
| 284 | boundary: HotbarSourceDispatchBoundary::SlashCommand, |
| 285 | safety_modes: HOTBAR_SKILL_SAFETY, |
| 286 | dispatch_path: "commands::execute via the $<skill> alias (local activation with a \ |
| 287 | visible receipt cell)", |
| 288 | status: "dispatchable", |
| 289 | }, |
| 290 | HotbarSourceDescriptor { |
| 291 | category: HotbarActionCategory::Plugin, |
| 292 | boundary: HotbarSourceDispatchBoundary::Deferred, |
| 293 | safety_modes: HOTBAR_DEFERRED_SAFETY, |
| 294 | dispatch_path: "plugin command/tool registry until plugin approval gates are wired", |
| 295 | status: "exploratory", |
| 296 | }, |
| 297 | ]; |
| 298 | |
| 299 | #[must_use] |
| 300 | pub const fn hotbar_source_descriptors() -> &'static [HotbarSourceDescriptor] { |
| 301 | HOTBAR_SOURCE_DESCRIPTORS |
| 302 | } |
| 303 | |
| 304 | /// Adapter for one source of bindable hotbar actions. |
| 305 | pub trait HotbarActionSource { |
| 306 | fn descriptor(&self) -> HotbarSourceDescriptor; |
| 307 | fn register_actions(&self, registry: &mut HotbarActionRegistry); |
| 308 | } |
| 309 | |
| 310 | /// Uniform interface for actions that can be bound to a hotbar slot. |
| 311 | pub trait HotbarAction: Send + Sync { |
| 312 | /// Stable action id used in config and dispatch. |
| 313 | fn id(&self) -> &str; |
| 314 | |
| 315 | /// Complete metadata used by renderers, setup wizard recommendations, and |
| 316 | /// future source adapters. |
| 317 | fn metadata(&self, locale: Locale) -> HotbarActionMetadata; |
| 318 | |
| 319 | /// Compact cell label. Built-ins keep this at seven characters or less. |
| 320 | #[cfg(test)] |
| 321 | fn short_label(&self) -> &str; |
| 322 | |
| 323 | /// Source category, such as `app`, `route`, `slash`, `mcp`, `skill`, or |
| 324 | /// `plugin`. |
| 325 | fn category(&self) -> &str; |
| 326 | |
| 327 | /// Whether the action is currently active in the supplied app state. |
| 328 | #[cfg(test)] |
| 329 | fn is_active(&self, app: &App) -> bool; |
| 330 | |
| 331 | /// Dynamic unavailable reason. `None` means the action is dispatchable |
| 332 | /// through its normal safety path. |
| 333 | fn disabled_reason(&self, _app: &App) -> Option<String> { |
| 334 | None |
| 335 | } |
| 336 | |
| 337 | /// Fire the action. |
| 338 | fn dispatch(&self, app: &mut App, config: &Config) -> Result<HotbarDispatch>; |
| 339 | } |
| 340 | |
| 341 | #[must_use] |
| 342 | pub fn recommend_hotbar_actions( |
| 343 | app: &App, |
| 344 | options: HotbarRecommendationOptions, |
| 345 | ) -> Vec<HotbarRecommendationEntry> { |
| 346 | let mut entries = app |
| 347 | .hotbar_actions |
| 348 | .iter() |
| 349 | .filter_map(|action| { |
| 350 | let metadata = action.metadata(app.ui_locale); |
| 351 | if !metadata.recommendation.is_recommendable() { |
| 352 | return None; |
| 353 | } |
| 354 | if matches!(metadata.args, HotbarArgsBehavior::Required) |
| 355 | && !options.include_required_args |
| 356 | { |
| 357 | return None; |
| 358 | } |
| 359 | let disabled_reason = action.disabled_reason(app); |
| 360 | if disabled_reason.is_some() { |
| 361 | return None; |
| 362 | } |
| 363 | Some(HotbarRecommendationEntry { |
| 364 | metadata, |
| 365 | disabled_reason, |
| 366 | }) |
| 367 | }) |
| 368 | .collect::<Vec<_>>(); |
| 369 | |
| 370 | entries.sort_by(|a, b| compare_recommendation_metadata(&a.metadata, &b.metadata)); |
| 371 | |
| 372 | let mut selected = Vec::new(); |
| 373 | let mut eligible_by_category: BTreeMap<HotbarActionCategory, usize> = BTreeMap::new(); |
| 374 | for entry in entries { |
| 375 | if selected.len() >= options.max_total { |
| 376 | break; |
| 377 | } |
| 378 | if !matches!(entry.metadata.recommendation, HotbarRecommendation::Default) { |
| 379 | let count = eligible_by_category |
| 380 | .entry(entry.metadata.category) |
| 381 | .or_insert(0); |
| 382 | if *count >= options.max_eligible_per_category { |
| 383 | continue; |
| 384 | } |
| 385 | *count += 1; |
| 386 | } |
| 387 | selected.push(entry); |
| 388 | } |
| 389 | selected |
| 390 | } |
| 391 | |
| 392 | fn default_hotbar_position(action_id: &str) -> Option<usize> { |
| 393 | codewhale_config::DEFAULT_HOTBAR_ACTIONS |
| 394 | .iter() |
| 395 | .position(|default_id| *default_id == action_id) |
| 396 | } |
| 397 | |
| 398 | fn compare_recommendation_metadata(a: &HotbarActionMetadata, b: &HotbarActionMetadata) -> Ordering { |
| 399 | match ( |
| 400 | default_hotbar_position(&a.id), |
| 401 | default_hotbar_position(&b.id), |
| 402 | ) { |
| 403 | (Some(a_pos), Some(b_pos)) => return a_pos.cmp(&b_pos), |
| 404 | (Some(_), None) => return Ordering::Less, |
| 405 | (None, Some(_)) => return Ordering::Greater, |
| 406 | (None, None) => {} |
| 407 | } |
| 408 | |
| 409 | a.category |
| 410 | .cmp(&b.category) |
| 411 | .then_with(|| { |
| 412 | a.display_name |
| 413 | .to_ascii_lowercase() |
| 414 | .cmp(&b.display_name.to_ascii_lowercase()) |
| 415 | }) |
| 416 | .then_with(|| a.id.cmp(&b.id)) |
| 417 | } |
| 418 | |
| 419 | #[derive(Default, Clone)] |
| 420 | pub struct HotbarActionRegistry { |
| 421 | actions: BTreeMap<String, Arc<dyn HotbarAction>>, |
| 422 | } |
| 423 | |
| 424 | impl HotbarActionRegistry { |
| 425 | #[must_use] |
| 426 | pub fn new() -> Self { |
| 427 | Self::default() |
| 428 | } |
| 429 | |
| 430 | #[must_use] |
| 431 | pub fn with_builtins() -> Self { |
| 432 | let mut registry = Self::new(); |
| 433 | registry.register_builtins(); |
| 434 | registry.register_slash_commands(); |
| 435 | registry |
| 436 | } |
| 437 | |
| 438 | #[must_use] |
| 439 | pub fn with_configured_routes( |
| 440 | config: &Config, |
| 441 | active_provider: Option<&ProviderIdentity>, |
| 442 | active_model: &str, |
| 443 | provider_models: &HashMap<String, String>, |
| 444 | ) -> Self { |
| 445 | let mut registry = Self::with_builtins(); |
| 446 | registry.register_configured_routes(config, active_provider, active_model, provider_models); |
| 447 | registry |
| 448 | } |
| 449 | |
| 450 | pub fn register(&mut self, action: impl HotbarAction + 'static) { |
| 451 | let id = action.id().to_string(); |
| 452 | assert!(!id.trim().is_empty(), "hotbar action id must not be empty"); |
| 453 | assert!( |
| 454 | self.actions.insert(id.clone(), Arc::new(action)).is_none(), |
| 455 | "duplicate hotbar action id {id}" |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | pub fn register_source(&mut self, source: &dyn HotbarActionSource) { |
| 460 | let descriptor = source.descriptor(); |
| 461 | debug_assert!( |
| 462 | hotbar_source_descriptors() |
| 463 | .iter() |
| 464 | .any(|registered| registered.category == descriptor.category |
| 465 | && registered.boundary == descriptor.boundary), |
| 466 | "hotbar source descriptor must be registered: {descriptor:?}" |
| 467 | ); |
| 468 | debug_assert!(!descriptor.dispatch_path.trim().is_empty()); |
| 469 | debug_assert!(!descriptor.status.trim().is_empty()); |
| 470 | debug_assert!(!descriptor.safety_modes.is_empty()); |
| 471 | let before = self.actions.len(); |
| 472 | source.register_actions(self); |
| 473 | if !descriptor.registers_dispatchable_actions() { |
| 474 | assert_eq!( |
| 475 | self.actions.len(), |
| 476 | before, |
| 477 | "deferred hotbar source {:?} must not register dispatchable actions before safety gates are wired", |
| 478 | descriptor.category |
| 479 | ); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | pub(crate) fn register_builtins(&mut self) { |
| 484 | self.register_source(&BuiltinHotbarActionSource); |
| 485 | } |
| 486 | |
| 487 | pub(crate) fn register_slash_commands(&mut self) { |
| 488 | self.register_source(&SlashCommandHotbarActionSource); |
| 489 | } |
| 490 | |
| 491 | pub(crate) fn register_configured_routes( |
| 492 | &mut self, |
| 493 | config: &Config, |
| 494 | active_provider: Option<&ProviderIdentity>, |
| 495 | active_model: &str, |
| 496 | provider_models: &HashMap<String, String>, |
| 497 | ) { |
| 498 | let source = ConfiguredRouteHotbarActionSource { |
| 499 | config, |
| 500 | active_provider, |
| 501 | active_model, |
| 502 | provider_models, |
| 503 | }; |
| 504 | self.register_source(&source); |
| 505 | } |
| 506 | |
| 507 | /// Register the already-discovered skills (name, description pairs from |
| 508 | /// `App::cached_skills`) as bindable hotbar actions. No filesystem I/O |
| 509 | /// happens here; the hotbar only lists skills the app already knows. |
| 510 | pub(crate) fn register_skills(&mut self, skills: &[(String, String)]) { |
| 511 | self.register_source(&SkillHotbarActionSource { skills }); |
| 512 | } |
| 513 | |
| 514 | /// Atomically replace the Skill-derived action source while retaining |
| 515 | /// built-ins, configured routes, slash commands, and live MCP actions. |
| 516 | /// Plugin lifecycle changes call this from the same cache refresh that |
| 517 | /// updates command dispatch, preventing stale revoked bindings. |
| 518 | pub(crate) fn replace_skills(&mut self, skills: &[(String, String)]) { |
| 519 | self.actions |
| 520 | .retain(|_, action| action.category() != HotbarActionCategory::Skill.as_str()); |
| 521 | self.register_skills(skills); |
| 522 | } |
| 523 | |
| 524 | /// Replace the MCP-tool hotbar actions with the tools in `snapshot`. |
| 525 | /// |
| 526 | /// Called when a live MCP discovery snapshot lands (or is refreshed) so |
| 527 | /// the hotbar only ever lists tools that are already loaded; the hotbar |
| 528 | /// itself never triggers server connections. |
| 529 | pub fn replace_mcp_tools( |
| 530 | &mut self, |
| 531 | snapshot: Option<&codewhale_engine::mcp::McpManagerSnapshot>, |
| 532 | ) { |
| 533 | self.actions |
| 534 | .retain(|_, action| action.category() != HotbarActionCategory::Mcp.as_str()); |
| 535 | if let Some(snapshot) = snapshot { |
| 536 | self.register_source(&McpToolHotbarActionSource { snapshot }); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | struct BuiltinHotbarActionSource; |
| 542 | |
| 543 | impl HotbarActionSource for BuiltinHotbarActionSource { |
| 544 | fn descriptor(&self) -> HotbarSourceDescriptor { |
| 545 | HOTBAR_SOURCE_DESCRIPTORS |
| 546 | .iter() |
| 547 | .copied() |
| 548 | .find(|descriptor| descriptor.category == HotbarActionCategory::App) |
| 549 | .expect("app hotbar source descriptor exists") |
| 550 | } |
| 551 | |
| 552 | fn register_actions(&self, registry: &mut HotbarActionRegistry) { |
| 553 | registry.register(AppHotbarAction::new( |
| 554 | "voice.toggle", |
| 555 | "voice", |
| 556 | "Voice input", |
| 557 | "Toggle voice capture from the terminal microphone.", |
| 558 | AppHotbarKind::VoiceToggle, |
| 559 | )); |
| 560 | registry.register(AppHotbarAction::new( |
| 561 | "session.compact", |
| 562 | "compact", |
| 563 | "Compact session", |
| 564 | "Shrink this conversation to free context.", |
| 565 | AppHotbarKind::SessionCompact, |
| 566 | )); |
| 567 | registry.register(AppHotbarAction::new( |
| 568 | "mode.plan", |
| 569 | "plan", |
| 570 | "Plan mode", |
| 571 | "Think through a plan before acting.", |
| 572 | AppHotbarKind::Mode(AppMode::Plan), |
| 573 | )); |
| 574 | registry.register(AppHotbarAction::new( |
| 575 | "mode.agent", |
| 576 | "agent", |
| 577 | "Work mode", |
| 578 | "Do direct work in the current session.", |
| 579 | AppHotbarKind::Mode(AppMode::Agent), |
| 580 | )); |
| 581 | registry.register(AppHotbarAction::new( |
| 582 | "reasoning.cycle", |
| 583 | "reason", |
| 584 | "Cycle reasoning", |
| 585 | "Step through reasoning levels for the active provider.", |
| 586 | AppHotbarKind::ReasoningCycle, |
| 587 | )); |
| 588 | registry.register(AppHotbarAction::new( |
| 589 | "sidebar.toggle", |
| 590 | "side", |
| 591 | "Toggle workbar", |
| 592 | "Show or hide the workbar.", |
| 593 | AppHotbarKind::SidebarToggle, |
| 594 | )); |
| 595 | registry.register(AppHotbarAction::new( |
| 596 | "filetree.toggle", |
| 597 | "files", |
| 598 | "Toggle file tree", |
| 599 | "Show or hide the workspace file tree.", |
| 600 | AppHotbarKind::FileTreeToggle, |
| 601 | )); |
| 602 | registry.register(AppHotbarAction::new( |
| 603 | "palette.open", |
| 604 | "palette", |
| 605 | "Command palette", |
| 606 | "Open the command palette.", |
| 607 | AppHotbarKind::PaletteOpen, |
| 608 | )); |
| 609 | registry.register(AppHotbarAction::new( |
| 610 | "trust.toggle", |
| 611 | "trust", |
| 612 | "Toggle trust", |
| 613 | "Turn workspace trust on or off.", |
| 614 | AppHotbarKind::TrustToggle, |
| 615 | )); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | struct SlashCommandHotbarActionSource; |
| 620 | |
| 621 | impl HotbarActionSource for SlashCommandHotbarActionSource { |
| 622 | fn descriptor(&self) -> HotbarSourceDescriptor { |
| 623 | HOTBAR_SOURCE_DESCRIPTORS |
| 624 | .iter() |
| 625 | .copied() |
| 626 | .find(|descriptor| descriptor.category == HotbarActionCategory::Slash) |
| 627 | .expect("slash hotbar source descriptor exists") |
| 628 | } |
| 629 | |
| 630 | fn register_actions(&self, registry: &mut HotbarActionRegistry) { |
| 631 | // Every command registers, including unlisted ones. The hotbar is a |
| 632 | // binding substrate, not a discovery surface: `codewhale-lane`'s |
| 633 | // control-plane descriptors resolve their `slash.<verb>` action id |
| 634 | // through this registry, so dropping an unlisted command here breaks |
| 635 | // a real contract (`control_plane_commands_are_bound_and_bare_dispatch_is_read_only`). |
| 636 | // Unlisted governs what is *advertised* — the slash menu, `/help`, |
| 637 | // and the command palette. |
| 638 | for info in commands::command_infos() { |
| 639 | registry.register(SlashHotbarAction::new(info)); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /// Adapter exposing already-discovered skills as hotbar actions (#2069). |
| 645 | /// |
| 646 | /// Follows the slash-command source pattern: the source only lists entries an |
| 647 | /// existing registry already knows about (`App::cached_skills`), and dispatch |
| 648 | /// reuses the existing `$<skill>` alias through `commands::execute`, which |
| 649 | /// activates the skill locally and posts a visible receipt cell. |
| 650 | struct SkillHotbarActionSource<'a> { |
| 651 | skills: &'a [(String, String)], |
| 652 | } |
| 653 | |
| 654 | impl HotbarActionSource for SkillHotbarActionSource<'_> { |
| 655 | fn descriptor(&self) -> HotbarSourceDescriptor { |
| 656 | HOTBAR_SOURCE_DESCRIPTORS |
| 657 | .iter() |
| 658 | .copied() |
| 659 | .find(|descriptor| descriptor.category == HotbarActionCategory::Skill) |
| 660 | .expect("skill hotbar source descriptor exists") |
| 661 | } |
| 662 | |
| 663 | fn register_actions(&self, registry: &mut HotbarActionRegistry) { |
| 664 | let mut seen = BTreeSet::new(); |
| 665 | for (name, description) in self.skills { |
| 666 | let name = name.trim(); |
| 667 | // Guard against duplicate names across skill roots: the registry |
| 668 | // asserts unique action ids, and the first discovery wins (the |
| 669 | // same shadowing order the skill registry itself uses). |
| 670 | if name.is_empty() || !seen.insert(name.to_string()) { |
| 671 | continue; |
| 672 | } |
| 673 | registry.register(SkillHotbarAction::new(name, description)); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /// Adapter exposing already-discovered MCP tools as hotbar actions (#2068). |
| 679 | /// |
| 680 | /// Deferred-source safety: the source only lists tools from an existing |
| 681 | /// discovery snapshot (enabled servers), and dispatch never executes a tool — |
| 682 | /// it prefills the composer with the tool's model-visible name so the actual |
| 683 | /// call still goes through the agent and the tool approval flow. |
| 684 | struct McpToolHotbarActionSource<'a> { |
| 685 | snapshot: &'a codewhale_engine::mcp::McpManagerSnapshot, |
| 686 | } |
| 687 | |
| 688 | impl HotbarActionSource for McpToolHotbarActionSource<'_> { |
| 689 | fn descriptor(&self) -> HotbarSourceDescriptor { |
| 690 | HOTBAR_SOURCE_DESCRIPTORS |
| 691 | .iter() |
| 692 | .copied() |
| 693 | .find(|descriptor| descriptor.category == HotbarActionCategory::Mcp) |
| 694 | .expect("mcp hotbar source descriptor exists") |
| 695 | } |
| 696 | |
| 697 | fn register_actions(&self, registry: &mut HotbarActionRegistry) { |
| 698 | let mut seen = BTreeSet::new(); |
| 699 | for server in &self.snapshot.servers { |
| 700 | if !server.enabled { |
| 701 | continue; |
| 702 | } |
| 703 | for tool in &server.tools { |
| 704 | if tool.model_name.trim().is_empty() { |
| 705 | continue; |
| 706 | } |
| 707 | let action = McpToolHotbarAction::new(&server.name, tool); |
| 708 | if !seen.insert(action.id.clone()) { |
| 709 | continue; |
| 710 | } |
| 711 | registry.register(action); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | struct ConfiguredRouteHotbarActionSource<'a> { |
| 718 | config: &'a Config, |
| 719 | active_provider: Option<&'a ProviderIdentity>, |
| 720 | active_model: &'a str, |
| 721 | provider_models: &'a HashMap<String, String>, |
| 722 | } |
| 723 | |
| 724 | impl HotbarActionSource for ConfiguredRouteHotbarActionSource<'_> { |
| 725 | fn descriptor(&self) -> HotbarSourceDescriptor { |
| 726 | HOTBAR_SOURCE_DESCRIPTORS |
| 727 | .iter() |
| 728 | .copied() |
| 729 | .find(|descriptor| descriptor.category == HotbarActionCategory::Route) |
| 730 | .expect("route hotbar source descriptor exists") |
| 731 | } |
| 732 | |
| 733 | fn register_actions(&self, registry: &mut HotbarActionRegistry) { |
| 734 | let Some(active) = self |
| 735 | .active_provider |
| 736 | .filter(|identity| self.config.verify_provider_identity(identity).is_ok()) |
| 737 | else { |
| 738 | return; |
| 739 | }; |
| 740 | let mut identities = self.config.provider_identities(); |
| 741 | identities.sort_by(|left, right| { |
| 742 | let left_label = left |
| 743 | .compatibility() |
| 744 | .map_or(left.key.as_str(), |row| row.label); |
| 745 | let right_label = right |
| 746 | .compatibility() |
| 747 | .map_or(right.key.as_str(), |row| row.label); |
| 748 | left_label |
| 749 | .cmp(right_label) |
| 750 | .then_with(|| left.key.as_str().cmp(right.key.as_str())) |
| 751 | }); |
| 752 | for identity in identities { |
| 753 | if identity.provider == ProviderKind::Antigravity { |
| 754 | continue; |
| 755 | } |
| 756 | if !codewhale_engine::provider_readiness::provider_is_configured_for_active( |
| 757 | self.config, |
| 758 | &identity, |
| 759 | active, |
| 760 | ) { |
| 761 | continue; |
| 762 | } |
| 763 | for model in configured_route_models_for_provider( |
| 764 | self.config, |
| 765 | &identity, |
| 766 | active, |
| 767 | self.active_model, |
| 768 | self.provider_models, |
| 769 | ) { |
| 770 | registry.register(RouteHotbarAction::new(identity.clone(), model)); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | impl HotbarActionRegistry { |
| 777 | #[must_use] |
| 778 | pub fn get(&self, id: &str) -> Option<Arc<dyn HotbarAction>> { |
| 779 | self.actions.get(id).cloned() |
| 780 | } |
| 781 | |
| 782 | #[must_use] |
| 783 | pub fn len(&self) -> usize { |
| 784 | self.actions.len() |
| 785 | } |
| 786 | |
| 787 | pub fn iter(&self) -> impl Iterator<Item = &dyn HotbarAction> { |
| 788 | self.actions.values().map(Arc::as_ref) |
| 789 | } |
| 790 | |
| 791 | #[cfg(test)] |
| 792 | #[must_use] |
| 793 | pub fn metadata(&self, locale: Locale) -> Vec<HotbarActionMetadata> { |
| 794 | self.iter().map(|action| action.metadata(locale)).collect() |
| 795 | } |
| 796 | |
| 797 | #[cfg(test)] |
| 798 | #[must_use] |
| 799 | pub fn metadata_validation_errors(&self, locale: Locale) -> Vec<String> { |
| 800 | let mut errors = Vec::new(); |
| 801 | for action in self.iter() { |
| 802 | let metadata = action.metadata(locale); |
| 803 | if metadata.id != action.id() { |
| 804 | errors.push(format!( |
| 805 | "{} metadata id {:?} does not match action id", |
| 806 | action.id(), |
| 807 | metadata.id |
| 808 | )); |
| 809 | } |
| 810 | if metadata.compact_label != action.short_label() { |
| 811 | errors.push(format!( |
| 812 | "{} metadata compact_label {:?} does not match short_label {:?}", |
| 813 | action.id(), |
| 814 | metadata.compact_label, |
| 815 | action.short_label() |
| 816 | )); |
| 817 | } |
| 818 | if metadata.category.as_str() != action.category() { |
| 819 | errors.push(format!( |
| 820 | "{} metadata category {:?} does not match category {:?}", |
| 821 | action.id(), |
| 822 | metadata.category.as_str(), |
| 823 | action.category() |
| 824 | )); |
| 825 | } |
| 826 | errors.extend(metadata.validation_errors()); |
| 827 | } |
| 828 | errors |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | fn dispatch_command_result(app: &mut App, result: CommandResult) -> HotbarDispatch { |
| 833 | if let Some(message) = result.message { |
| 834 | app.show_command_result( |
| 835 | message, |
| 836 | if result.is_error { |
| 837 | StatusToastLevel::Error |
| 838 | } else { |
| 839 | StatusToastLevel::Info |
| 840 | }, |
| 841 | ); |
| 842 | } |
| 843 | result |
| 844 | .action |
| 845 | .map_or(HotbarDispatch::Handled, HotbarDispatch::AppAction) |
| 846 | } |
| 847 | |
| 848 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 849 | enum AppHotbarKind { |
| 850 | VoiceToggle, |
| 851 | SessionCompact, |
| 852 | Mode(AppMode), |
| 853 | ReasoningCycle, |
| 854 | SidebarToggle, |
| 855 | FileTreeToggle, |
| 856 | PaletteOpen, |
| 857 | TrustToggle, |
| 858 | } |
| 859 | |
| 860 | struct AppHotbarAction { |
| 861 | id: &'static str, |
| 862 | short_label: &'static str, |
| 863 | display_name: &'static str, |
| 864 | description: &'static str, |
| 865 | kind: AppHotbarKind, |
| 866 | } |
| 867 | |
| 868 | impl AppHotbarAction { |
| 869 | const fn new( |
| 870 | id: &'static str, |
| 871 | short_label: &'static str, |
| 872 | display_name: &'static str, |
| 873 | description: &'static str, |
| 874 | kind: AppHotbarKind, |
| 875 | ) -> Self { |
| 876 | Self { |
| 877 | id, |
| 878 | short_label, |
| 879 | display_name, |
| 880 | description, |
| 881 | kind, |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | fn safety(&self) -> HotbarSafetyClass { |
| 886 | match self.kind { |
| 887 | AppHotbarKind::VoiceToggle => HotbarSafetyClass::ExternalInput, |
| 888 | AppHotbarKind::TrustToggle => HotbarSafetyClass::LocalState, |
| 889 | AppHotbarKind::SessionCompact | AppHotbarKind::ReasoningCycle => { |
| 890 | HotbarSafetyClass::LocalState |
| 891 | } |
| 892 | AppHotbarKind::Mode(_) |
| 893 | | AppHotbarKind::SidebarToggle |
| 894 | | AppHotbarKind::FileTreeToggle |
| 895 | | AppHotbarKind::PaletteOpen => HotbarSafetyClass::LocalUi, |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | fn recommendation(&self) -> HotbarRecommendation { |
| 900 | if codewhale_config::DEFAULT_HOTBAR_ACTIONS.contains(&self.id) { |
| 901 | HotbarRecommendation::Default |
| 902 | } else { |
| 903 | HotbarRecommendation::Eligible |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | fn localized_display_name(&self, locale: Locale) -> String { |
| 908 | let Some(id) = self.display_name_id() else { |
| 909 | return self.display_name.to_string(); |
| 910 | }; |
| 911 | tr(locale, id).into_owned() |
| 912 | } |
| 913 | |
| 914 | fn localized_description(&self, locale: Locale) -> String { |
| 915 | let Some(id) = self.description_id() else { |
| 916 | return self.description.to_string(); |
| 917 | }; |
| 918 | tr(locale, id).into_owned() |
| 919 | } |
| 920 | |
| 921 | fn display_name_id(&self) -> Option<MessageId> { |
| 922 | Some(match self.kind { |
| 923 | AppHotbarKind::VoiceToggle => MessageId::HotbarActionVoiceToggleName, |
| 924 | AppHotbarKind::SessionCompact => MessageId::HotbarActionSessionCompactName, |
| 925 | AppHotbarKind::Mode(AppMode::Plan) => MessageId::HotbarActionModePlanName, |
| 926 | AppHotbarKind::Mode(AppMode::Agent) => MessageId::HotbarActionModeAgentName, |
| 927 | AppHotbarKind::ReasoningCycle => MessageId::HotbarActionReasoningCycleName, |
| 928 | AppHotbarKind::SidebarToggle => MessageId::HotbarActionSidebarToggleName, |
| 929 | AppHotbarKind::FileTreeToggle => MessageId::HotbarActionFileTreeToggleName, |
| 930 | AppHotbarKind::PaletteOpen => MessageId::HotbarActionPaletteOpenName, |
| 931 | AppHotbarKind::TrustToggle => MessageId::HotbarActionTrustToggleName, |
| 932 | }) |
| 933 | } |
| 934 | |
| 935 | fn description_id(&self) -> Option<MessageId> { |
| 936 | Some(match self.kind { |
| 937 | AppHotbarKind::VoiceToggle => MessageId::HotbarActionVoiceToggleDescription, |
| 938 | AppHotbarKind::SessionCompact => MessageId::HotbarActionSessionCompactDescription, |
| 939 | AppHotbarKind::Mode(AppMode::Plan) => MessageId::HotbarActionModePlanDescription, |
| 940 | AppHotbarKind::Mode(AppMode::Agent) => MessageId::HotbarActionModeAgentDescription, |
| 941 | AppHotbarKind::ReasoningCycle => MessageId::HotbarActionReasoningCycleDescription, |
| 942 | AppHotbarKind::SidebarToggle => MessageId::HotbarActionSidebarToggleDescription, |
| 943 | AppHotbarKind::FileTreeToggle => MessageId::HotbarActionFileTreeToggleDescription, |
| 944 | AppHotbarKind::PaletteOpen => MessageId::HotbarActionPaletteOpenDescription, |
| 945 | AppHotbarKind::TrustToggle => MessageId::HotbarActionTrustToggleDescription, |
| 946 | }) |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | impl HotbarAction for AppHotbarAction { |
| 951 | fn id(&self) -> &str { |
| 952 | self.id |
| 953 | } |
| 954 | |
| 955 | fn metadata(&self, locale: Locale) -> HotbarActionMetadata { |
| 956 | HotbarActionMetadata { |
| 957 | id: self.id.to_string(), |
| 958 | source_id: "builtin".to_string(), |
| 959 | display_name: self.localized_display_name(locale), |
| 960 | compact_label: self.short_label.to_string(), |
| 961 | description: self.localized_description(locale), |
| 962 | category: HotbarActionCategory::App, |
| 963 | args: HotbarArgsBehavior::None, |
| 964 | safety: self.safety(), |
| 965 | recommendation: self.recommendation(), |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | #[cfg(test)] |
| 970 | fn short_label(&self) -> &str { |
| 971 | self.short_label |
| 972 | } |
| 973 | |
| 974 | fn category(&self) -> &str { |
| 975 | "app" |
| 976 | } |
| 977 | |
| 978 | #[cfg(test)] |
| 979 | fn is_active(&self, app: &App) -> bool { |
| 980 | match self.kind { |
| 981 | AppHotbarKind::VoiceToggle => app.voice_enabled, |
| 982 | AppHotbarKind::SessionCompact => app.is_compacting || app.manual_compaction_queued, |
| 983 | AppHotbarKind::Mode(mode) => app.mode == mode, |
| 984 | AppHotbarKind::ReasoningCycle => { |
| 985 | app.reasoning_effort != codewhale_engine::reasoning_preference::ReasoningEffort::Off |
| 986 | } |
| 987 | AppHotbarKind::SidebarToggle => { |
| 988 | app.work_surface.placement != crate::tui::work_surface::WorkSurfacePlacement::Off |
| 989 | } |
| 990 | AppHotbarKind::FileTreeToggle => app.file_tree.is_some(), |
| 991 | AppHotbarKind::PaletteOpen => false, |
| 992 | AppHotbarKind::TrustToggle => app.trust_mode, |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | fn dispatch(&self, app: &mut App, _config: &Config) -> Result<HotbarDispatch> { |
| 997 | match self.kind { |
| 998 | AppHotbarKind::VoiceToggle => { |
| 999 | let result = crate::commands::voice::voice(app); |
| 1000 | Ok(dispatch_command_result(app, result)) |
| 1001 | } |
| 1002 | AppHotbarKind::SessionCompact => { |
| 1003 | Ok(HotbarDispatch::AppAction(AppAction::CompactContext { |
| 1004 | focus: None, |
| 1005 | })) |
| 1006 | } |
| 1007 | AppHotbarKind::Mode(mode) => { |
| 1008 | // User-facing selection: persists the startup default too. |
| 1009 | let outcome = app.select_mode(mode); |
| 1010 | // Only a live change needs an `AppAction`; a persisted-same |
| 1011 | // selection still gets its own receipt so the row does not look |
| 1012 | // inert when it actually wrote the startup default. |
| 1013 | app.report_mode_selection(mode, outcome); |
| 1014 | if outcome.changed_live_state() { |
| 1015 | Ok(HotbarDispatch::AppAction(AppAction::ModeChanged(mode))) |
| 1016 | } else { |
| 1017 | Ok(HotbarDispatch::Handled) |
| 1018 | } |
| 1019 | } |
| 1020 | AppHotbarKind::ReasoningCycle => { |
| 1021 | if app.cycle_effort().changed_live_state() { |
| 1022 | Ok(HotbarDispatch::AppAction(AppAction::UpdateCompaction( |
| 1023 | app.compaction_config(), |
| 1024 | ))) |
| 1025 | } else { |
| 1026 | Ok(HotbarDispatch::Handled) |
| 1027 | } |
| 1028 | } |
| 1029 | AppHotbarKind::SidebarToggle => { |
| 1030 | if app.work_surface.placement == crate::tui::work_surface::WorkSurfacePlacement::Off |
| 1031 | { |
| 1032 | app.work_surface.placement = |
| 1033 | crate::tui::work_surface::WorkSurfacePlacement::Bottom; |
| 1034 | app.show_command_result( |
| 1035 | "Workbar: bottom placement".to_string(), |
| 1036 | StatusToastLevel::Info, |
| 1037 | ); |
| 1038 | } else { |
| 1039 | app.work_surface.placement = |
| 1040 | crate::tui::work_surface::WorkSurfacePlacement::Off; |
| 1041 | app.show_command_result("Workbar is off".to_string(), StatusToastLevel::Info); |
| 1042 | } |
| 1043 | app.needs_redraw = true; |
| 1044 | Ok(HotbarDispatch::Handled) |
| 1045 | } |
| 1046 | AppHotbarKind::FileTreeToggle => { |
| 1047 | crate::tui::key_actions::toggle_file_tree(app); |
| 1048 | Ok(HotbarDispatch::Handled) |
| 1049 | } |
| 1050 | AppHotbarKind::PaletteOpen => { |
| 1051 | app.view_stack.push(CommandPaletteView::new_for_locale( |
| 1052 | app.ui_locale, |
| 1053 | build_command_palette_entries( |
| 1054 | app.ui_locale, |
| 1055 | &app.skills_dir, |
| 1056 | &app.skills_config, |
| 1057 | &app.workspace, |
| 1058 | &app.mcp_config_path, |
| 1059 | app.mcp_snapshot.as_ref(), |
| 1060 | app.extension_plugin_view().as_ref(), |
| 1061 | ), |
| 1062 | )); |
| 1063 | Ok(HotbarDispatch::Handled) |
| 1064 | } |
| 1065 | AppHotbarKind::TrustToggle => { |
| 1066 | app.trust_mode = !app.trust_mode; |
| 1067 | app.show_command_result( |
| 1068 | if app.trust_mode { |
| 1069 | "Workspace trust mode enabled.".to_string() |
| 1070 | } else { |
| 1071 | "Workspace trust mode disabled.".to_string() |
| 1072 | }, |
| 1073 | StatusToastLevel::Info, |
| 1074 | ); |
| 1075 | Ok(HotbarDispatch::Handled) |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | struct SlashHotbarAction { |
| 1082 | info: &'static CommandInfo, |
| 1083 | id: String, |
| 1084 | short_label: String, |
| 1085 | } |
| 1086 | |
| 1087 | impl SlashHotbarAction { |
| 1088 | fn new(info: &'static CommandInfo) -> Self { |
| 1089 | let short_label = match info.name { |
| 1090 | "workflow" => "wf".to_string(), |
| 1091 | other => other.chars().take(7).collect(), |
| 1092 | }; |
| 1093 | Self { |
| 1094 | info, |
| 1095 | id: format!("slash.{}", info.name), |
| 1096 | short_label, |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | fn prefill_composer(&self, app: &mut App) { |
| 1101 | app.clear_input_recoverable(); |
| 1102 | app.input = format!("/{} ", self.info.name); |
| 1103 | app.cursor_position = app.input.chars().count(); |
| 1104 | app.slash_menu_hidden = false; |
| 1105 | app.needs_redraw = true; |
| 1106 | app.show_command_result( |
| 1107 | format!("Command needs arguments; complete {}", app.input.trim_end()), |
| 1108 | StatusToastLevel::Info, |
| 1109 | ); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | impl HotbarAction for SlashHotbarAction { |
| 1114 | fn id(&self) -> &str { |
| 1115 | &self.id |
| 1116 | } |
| 1117 | |
| 1118 | fn metadata(&self, locale: Locale) -> HotbarActionMetadata { |
| 1119 | let recommendation = if codewhale_config::DEFAULT_HOTBAR_ACTIONS.contains(&self.id.as_str()) |
| 1120 | { |
| 1121 | HotbarRecommendation::Default |
| 1122 | } else { |
| 1123 | match self.info.discovery() { |
| 1124 | codewhale_command_contract::CommandDiscovery::Primary => { |
| 1125 | HotbarRecommendation::Eligible |
| 1126 | } |
| 1127 | codewhale_command_contract::CommandDiscovery::Advanced |
| 1128 | | codewhale_command_contract::CommandDiscovery::Compatibility => { |
| 1129 | HotbarRecommendation::Advanced |
| 1130 | } |
| 1131 | } |
| 1132 | }; |
| 1133 | HotbarActionMetadata { |
| 1134 | id: self.id.clone(), |
| 1135 | source_id: format!("command:{}", self.info.name), |
| 1136 | display_name: format!("/{}", self.info.name), |
| 1137 | compact_label: self.short_label.clone(), |
| 1138 | description: self.info.description_for(locale).to_string(), |
| 1139 | category: HotbarActionCategory::Slash, |
| 1140 | args: HotbarArgsBehavior::for_command(self.info), |
| 1141 | safety: HotbarSafetyClass::ExistingCommand, |
| 1142 | recommendation, |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | #[cfg(test)] |
| 1147 | fn short_label(&self) -> &str { |
| 1148 | &self.short_label |
| 1149 | } |
| 1150 | |
| 1151 | fn category(&self) -> &str { |
| 1152 | "slash" |
| 1153 | } |
| 1154 | |
| 1155 | #[cfg(test)] |
| 1156 | fn is_active(&self, _app: &App) -> bool { |
| 1157 | false |
| 1158 | } |
| 1159 | |
| 1160 | fn dispatch(&self, app: &mut App, config: &Config) -> Result<HotbarDispatch> { |
| 1161 | if self.info.requires_required_argument() { |
| 1162 | self.prefill_composer(app); |
| 1163 | return Ok(HotbarDispatch::Handled); |
| 1164 | } |
| 1165 | |
| 1166 | let input = format!("/{}", self.info.name); |
| 1167 | let result = commands::execute_with_config(&input, app, config); |
| 1168 | Ok(dispatch_command_result(app, result)) |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | struct RouteHotbarAction { |
| 1173 | identity: ProviderIdentity, |
| 1174 | model: String, |
| 1175 | id: String, |
| 1176 | short_label: String, |
| 1177 | } |
| 1178 | |
| 1179 | impl RouteHotbarAction { |
| 1180 | fn new(identity: ProviderIdentity, model: String) -> Self { |
| 1181 | let trimmed_model = model.trim().to_string(); |
| 1182 | Self { |
| 1183 | id: route_action_id(&identity, &trimmed_model), |
| 1184 | short_label: codewhale_tui_kit::ui_text::truncate_line_to_width( |
| 1185 | identity.key.as_str(), |
| 1186 | HOTBAR_COMPACT_LABEL_MAX_WIDTH, |
| 1187 | ), |
| 1188 | model: trimmed_model, |
| 1189 | identity, |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | impl HotbarAction for RouteHotbarAction { |
| 1195 | fn id(&self) -> &str { |
| 1196 | &self.id |
| 1197 | } |
| 1198 | |
| 1199 | fn metadata(&self, _locale: Locale) -> HotbarActionMetadata { |
| 1200 | HotbarActionMetadata { |
| 1201 | id: self.id.clone(), |
| 1202 | source_id: format!("route:{}", self.identity.key.as_str()), |
| 1203 | display_name: format!( |
| 1204 | "{} · {}", |
| 1205 | self.identity |
| 1206 | .compatibility() |
| 1207 | .map_or(self.identity.key.as_str(), |row| row.label), |
| 1208 | self.model |
| 1209 | ), |
| 1210 | compact_label: self.short_label.clone(), |
| 1211 | description: format!( |
| 1212 | "Switch to {} on {} through the existing /model route path.", |
| 1213 | self.model, |
| 1214 | self.identity |
| 1215 | .compatibility() |
| 1216 | .map_or(self.identity.key.as_str(), |row| row.label) |
| 1217 | ), |
| 1218 | category: HotbarActionCategory::Route, |
| 1219 | args: HotbarArgsBehavior::None, |
| 1220 | safety: HotbarSafetyClass::ConfigChange, |
| 1221 | recommendation: HotbarRecommendation::Eligible, |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | #[cfg(test)] |
| 1226 | fn short_label(&self) -> &str { |
| 1227 | &self.short_label |
| 1228 | } |
| 1229 | |
| 1230 | fn category(&self) -> &str { |
| 1231 | "route" |
| 1232 | } |
| 1233 | |
| 1234 | #[cfg(test)] |
| 1235 | fn is_active(&self, app: &App) -> bool { |
| 1236 | !app.auto_model |
| 1237 | && app.provider_identity.as_ref() == Some(&self.identity) |
| 1238 | && if self.identity.provider == ProviderKind::Custom { |
| 1239 | app.model.trim() == self.model.trim() |
| 1240 | } else { |
| 1241 | app.model.trim().eq_ignore_ascii_case(self.model.trim()) |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | fn dispatch(&self, _app: &mut App, _config: &Config) -> Result<HotbarDispatch> { |
| 1246 | Ok(HotbarDispatch::AppAction(AppAction::SwitchModelRoute { |
| 1247 | identity: self.identity.clone(), |
| 1248 | model: self.model.clone(), |
| 1249 | })) |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | struct SkillHotbarAction { |
| 1254 | name: String, |
| 1255 | id: String, |
| 1256 | short_label: String, |
| 1257 | description: String, |
| 1258 | } |
| 1259 | |
| 1260 | impl SkillHotbarAction { |
| 1261 | fn new(name: &str, description: &str) -> Self { |
| 1262 | let description = description.trim(); |
| 1263 | Self { |
| 1264 | name: name.to_string(), |
| 1265 | id: format!("skill.{name}"), |
| 1266 | short_label: codewhale_tui_kit::ui_text::truncate_line_to_width( |
| 1267 | name, |
| 1268 | HOTBAR_COMPACT_LABEL_MAX_WIDTH, |
| 1269 | ), |
| 1270 | description: if description.is_empty() { |
| 1271 | format!("Activate the {name} skill for the next message.") |
| 1272 | } else { |
| 1273 | description.to_string() |
| 1274 | }, |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | impl HotbarAction for SkillHotbarAction { |
| 1280 | fn id(&self) -> &str { |
| 1281 | &self.id |
| 1282 | } |
| 1283 | |
| 1284 | fn metadata(&self, _locale: Locale) -> HotbarActionMetadata { |
| 1285 | HotbarActionMetadata { |
| 1286 | id: self.id.clone(), |
| 1287 | source_id: format!("skill:{}", self.name), |
| 1288 | display_name: format!("${}", self.name), |
| 1289 | compact_label: self.short_label.clone(), |
| 1290 | description: self.description.clone(), |
| 1291 | category: HotbarActionCategory::Skill, |
| 1292 | args: HotbarArgsBehavior::None, |
| 1293 | safety: HotbarSafetyClass::ExistingCommand, |
| 1294 | recommendation: HotbarRecommendation::Eligible, |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | #[cfg(test)] |
| 1299 | fn short_label(&self) -> &str { |
| 1300 | &self.short_label |
| 1301 | } |
| 1302 | |
| 1303 | fn category(&self) -> &str { |
| 1304 | "skill" |
| 1305 | } |
| 1306 | |
| 1307 | #[cfg(test)] |
| 1308 | fn is_active(&self, app: &App) -> bool { |
| 1309 | // `activate_skill` stores the full instruction block; the heading line |
| 1310 | // inside it is the stable marker for which skill is armed. |
| 1311 | app.active_skill |
| 1312 | .as_deref() |
| 1313 | .is_some_and(|instruction| instruction.contains(&format!("# Skill: {}\n", self.name))) |
| 1314 | } |
| 1315 | |
| 1316 | fn dispatch(&self, app: &mut App, config: &Config) -> Result<HotbarDispatch> { |
| 1317 | // Same path as typing `$<name>`: activates the skill for the next |
| 1318 | // message (local state plus a visible receipt cell); nothing is sent |
| 1319 | // to the model until the user submits a message. |
| 1320 | let input = format!("${}", self.name); |
| 1321 | let result = commands::execute_with_config(&input, app, config); |
| 1322 | Ok(dispatch_command_result(app, result)) |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | struct McpToolHotbarAction { |
| 1327 | server: String, |
| 1328 | tool_name: String, |
| 1329 | model_name: String, |
| 1330 | description: Option<String>, |
| 1331 | id: String, |
| 1332 | short_label: String, |
| 1333 | } |
| 1334 | |
| 1335 | impl McpToolHotbarAction { |
| 1336 | fn new(server: &str, tool: &codewhale_engine::mcp::McpDiscoveredItem) -> Self { |
| 1337 | Self { |
| 1338 | server: server.to_string(), |
| 1339 | tool_name: tool.name.clone(), |
| 1340 | model_name: tool.model_name.trim().to_string(), |
| 1341 | description: tool.description.clone(), |
| 1342 | id: format!("mcp.{server}.{}", tool.name), |
| 1343 | short_label: codewhale_tui_kit::ui_text::truncate_line_to_width( |
| 1344 | &tool.name, |
| 1345 | HOTBAR_COMPACT_LABEL_MAX_WIDTH, |
| 1346 | ), |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | fn prefill_composer(&self, app: &mut App) { |
| 1351 | app.clear_input_recoverable(); |
| 1352 | app.input = format!("{} ", self.model_name); |
| 1353 | app.cursor_position = app.input.chars().count(); |
| 1354 | app.needs_redraw = true; |
| 1355 | app.show_command_result( |
| 1356 | format!( |
| 1357 | "MCP tool needs a request; complete {}", |
| 1358 | app.input.trim_end() |
| 1359 | ), |
| 1360 | StatusToastLevel::Info, |
| 1361 | ); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | impl HotbarAction for McpToolHotbarAction { |
| 1366 | fn id(&self) -> &str { |
| 1367 | &self.id |
| 1368 | } |
| 1369 | |
| 1370 | fn metadata(&self, _locale: Locale) -> HotbarActionMetadata { |
| 1371 | let description = match self.description.as_deref().map(str::trim) { |
| 1372 | Some(desc) if !desc.is_empty() => { |
| 1373 | format!("Prefill the composer with {} — {desc}", self.model_name) |
| 1374 | } |
| 1375 | _ => format!( |
| 1376 | "Prefill the composer with {}; the call still runs through tool approval.", |
| 1377 | self.model_name |
| 1378 | ), |
| 1379 | }; |
| 1380 | HotbarActionMetadata { |
| 1381 | id: self.id.clone(), |
| 1382 | source_id: format!("mcp:{}", self.server), |
| 1383 | display_name: format!("mcp:{}:{}", self.server, self.tool_name), |
| 1384 | compact_label: self.short_label.clone(), |
| 1385 | description, |
| 1386 | category: HotbarActionCategory::Mcp, |
| 1387 | args: HotbarArgsBehavior::Required, |
| 1388 | safety: HotbarSafetyClass::RequiresApproval, |
| 1389 | recommendation: HotbarRecommendation::Advanced, |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | #[cfg(test)] |
| 1394 | fn short_label(&self) -> &str { |
| 1395 | &self.short_label |
| 1396 | } |
| 1397 | |
| 1398 | fn category(&self) -> &str { |
| 1399 | "mcp" |
| 1400 | } |
| 1401 | |
| 1402 | #[cfg(test)] |
| 1403 | fn is_active(&self, _app: &App) -> bool { |
| 1404 | false |
| 1405 | } |
| 1406 | |
| 1407 | fn dispatch(&self, app: &mut App, _config: &Config) -> Result<HotbarDispatch> { |
| 1408 | // Never execute the tool from the hotbar: prefill the composer with |
| 1409 | // the model-visible tool name (same text the command palette's |
| 1410 | // "> use" entry inserts) and let the user describe the call. The |
| 1411 | // eventual invocation stays behind the normal tool approval flow. |
| 1412 | self.prefill_composer(app); |
| 1413 | Ok(HotbarDispatch::Handled) |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | fn configured_route_models_for_provider( |
| 1418 | config: &Config, |
| 1419 | identity: &ProviderIdentity, |
| 1420 | active: &ProviderIdentity, |
| 1421 | active_model: &str, |
| 1422 | provider_models: &HashMap<String, String>, |
| 1423 | ) -> Vec<String> { |
| 1424 | let mut models = Vec::new(); |
| 1425 | if identity == active { |
| 1426 | push_route_model(&mut models, active_model); |
| 1427 | } |
| 1428 | if let Some(model) = provider_models.get(identity.key.as_str()) { |
| 1429 | push_route_model(&mut models, model); |
| 1430 | } |
| 1431 | if let Some(model) = config |
| 1432 | .provider_config_for(identity) |
| 1433 | .and_then(|entry| entry.model.as_deref()) |
| 1434 | { |
| 1435 | push_route_model(&mut models, model); |
| 1436 | } |
| 1437 | for model in models_for_provider(config, identity) |
| 1438 | .into_iter() |
| 1439 | .filter(|model| !model.trim().eq_ignore_ascii_case("auto")) |
| 1440 | .take(1) |
| 1441 | { |
| 1442 | push_route_model(&mut models, &model); |
| 1443 | } |
| 1444 | models |
| 1445 | } |
| 1446 | |
| 1447 | fn push_route_model(models: &mut Vec<String>, model: &str) { |
| 1448 | let trimmed = model.trim(); |
| 1449 | if trimmed.is_empty() || trimmed.eq_ignore_ascii_case("auto") { |
| 1450 | return; |
| 1451 | } |
| 1452 | if models.iter().any(|existing| existing == trimmed) { |
| 1453 | return; |
| 1454 | } |
| 1455 | models.push(trimmed.to_string()); |
| 1456 | } |
| 1457 | |
| 1458 | fn route_action_id(identity: &ProviderIdentity, model: &str) -> String { |
| 1459 | format!("route.{}.{}", identity.key, model.trim()) |
| 1460 | } |
| 1461 | |
| 1462 | #[cfg(test)] |
| 1463 | mod tests { |
| 1464 | use std::collections::{BTreeSet, HashMap}; |
| 1465 | use std::path::PathBuf; |
| 1466 | |
| 1467 | use crate::tui::app::TuiOptions; |
| 1468 | use crate::tui::views::ModalKind; |
| 1469 | use codewhale_config::ProviderKind; |
| 1470 | use codewhale_engine::reasoning_preference::ReasoningEffort; |
| 1471 | use codewhale_runtime::config::Config; |
| 1472 | |
| 1473 | use super::*; |
| 1474 | |
| 1475 | fn test_app_with_paths_and_config( |
| 1476 | workspace: PathBuf, |
| 1477 | skills_dir: PathBuf, |
| 1478 | config: &Config, |
| 1479 | ) -> App { |
| 1480 | let options = TuiOptions { |
| 1481 | skills_dir, |
| 1482 | start_in_agent_mode: true, |
| 1483 | ..crate::tui::test_support::test_tui_options(workspace) |
| 1484 | }; |
| 1485 | let mut app = App::new(options, config); |
| 1486 | app.ui_locale = codewhale_localization::Locale::En; |
| 1487 | app |
| 1488 | } |
| 1489 | |
| 1490 | fn test_app_with_paths(workspace: PathBuf, skills_dir: PathBuf) -> App { |
| 1491 | test_app_with_paths_and_config(workspace, skills_dir, &Config::default()) |
| 1492 | } |
| 1493 | |
| 1494 | fn test_app() -> App { |
| 1495 | test_app_with_paths(PathBuf::from("."), PathBuf::from(".")) |
| 1496 | } |
| 1497 | |
| 1498 | fn test_mcp_snapshot() -> codewhale_engine::mcp::McpManagerSnapshot { |
| 1499 | use codewhale_engine::mcp::{McpDiscoveredItem, McpManagerSnapshot, McpServerSnapshot}; |
| 1500 | let server = |name: &str, enabled: bool, tools: Vec<McpDiscoveredItem>| McpServerSnapshot { |
| 1501 | name: name.to_string(), |
| 1502 | enabled, |
| 1503 | required: false, |
| 1504 | transport: "stdio".to_string(), |
| 1505 | command_or_url: format!("{name}-server"), |
| 1506 | connect_timeout: 5, |
| 1507 | execute_timeout: 5, |
| 1508 | read_timeout: 5, |
| 1509 | connected: enabled, |
| 1510 | error: None, |
| 1511 | auth_required: false, |
| 1512 | capability_metadata: if enabled { |
| 1513 | codewhale_engine::mcp::McpServerCapabilityMetadata::LegacyFallback |
| 1514 | } else { |
| 1515 | codewhale_engine::mcp::McpServerCapabilityMetadata::NotObserved |
| 1516 | }, |
| 1517 | tools, |
| 1518 | resources: Vec::new(), |
| 1519 | prompts: Vec::new(), |
| 1520 | }; |
| 1521 | McpManagerSnapshot { |
| 1522 | config_path: PathBuf::from("mcp.json"), |
| 1523 | config_exists: true, |
| 1524 | reload_required: false, |
| 1525 | servers: vec![ |
| 1526 | server( |
| 1527 | "search", |
| 1528 | true, |
| 1529 | vec![ |
| 1530 | McpDiscoveredItem { |
| 1531 | name: "web_search".to_string(), |
| 1532 | model_name: "mcp_search_web_search".to_string(), |
| 1533 | description: Some("Search the web".to_string()), |
| 1534 | }, |
| 1535 | McpDiscoveredItem { |
| 1536 | name: "broken".to_string(), |
| 1537 | model_name: " ".to_string(), |
| 1538 | description: None, |
| 1539 | }, |
| 1540 | ], |
| 1541 | ), |
| 1542 | server( |
| 1543 | "offline", |
| 1544 | false, |
| 1545 | vec![McpDiscoveredItem { |
| 1546 | name: "other_tool".to_string(), |
| 1547 | model_name: "mcp_offline_other_tool".to_string(), |
| 1548 | description: None, |
| 1549 | }], |
| 1550 | ), |
| 1551 | ], |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | struct TestHotbarAction { |
| 1556 | id: &'static str, |
| 1557 | } |
| 1558 | |
| 1559 | impl HotbarAction for TestHotbarAction { |
| 1560 | fn id(&self) -> &str { |
| 1561 | self.id |
| 1562 | } |
| 1563 | |
| 1564 | fn metadata(&self, _locale: Locale) -> HotbarActionMetadata { |
| 1565 | HotbarActionMetadata { |
| 1566 | id: self.id.to_string(), |
| 1567 | source_id: "test".to_string(), |
| 1568 | display_name: "Test action".to_string(), |
| 1569 | compact_label: "test".to_string(), |
| 1570 | description: "Test action descriptor".to_string(), |
| 1571 | category: HotbarActionCategory::App, |
| 1572 | args: HotbarArgsBehavior::None, |
| 1573 | safety: HotbarSafetyClass::LocalUi, |
| 1574 | recommendation: HotbarRecommendation::Eligible, |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | fn short_label(&self) -> &str { |
| 1579 | "test" |
| 1580 | } |
| 1581 | |
| 1582 | fn category(&self) -> &str { |
| 1583 | "app" |
| 1584 | } |
| 1585 | |
| 1586 | fn is_active(&self, _app: &App) -> bool { |
| 1587 | false |
| 1588 | } |
| 1589 | |
| 1590 | fn dispatch(&self, _app: &mut App, _config: &Config) -> Result<HotbarDispatch> { |
| 1591 | Ok(HotbarDispatch::Handled) |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | struct DeferredTestHotbarSource; |
| 1596 | |
| 1597 | impl HotbarActionSource for DeferredTestHotbarSource { |
| 1598 | fn descriptor(&self) -> HotbarSourceDescriptor { |
| 1599 | HOTBAR_SOURCE_DESCRIPTORS |
| 1600 | .iter() |
| 1601 | .copied() |
| 1602 | .find(|descriptor| descriptor.category == HotbarActionCategory::Plugin) |
| 1603 | .expect("plugin descriptor exists") |
| 1604 | } |
| 1605 | |
| 1606 | fn register_actions(&self, registry: &mut HotbarActionRegistry) { |
| 1607 | registry.register(TestHotbarAction { |
| 1608 | id: "plugin.deferred-test", |
| 1609 | }); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | #[test] |
| 1614 | #[should_panic(expected = "duplicate hotbar action id duplicate.action")] |
| 1615 | fn registry_rejects_duplicate_action_ids() { |
| 1616 | let mut registry = HotbarActionRegistry::new(); |
| 1617 | registry.register(TestHotbarAction { |
| 1618 | id: "duplicate.action", |
| 1619 | }); |
| 1620 | registry.register(TestHotbarAction { |
| 1621 | id: "duplicate.action", |
| 1622 | }); |
| 1623 | } |
| 1624 | |
| 1625 | #[test] |
| 1626 | fn registry_metadata_contract_covers_registered_actions() { |
| 1627 | let registry = HotbarActionRegistry::with_builtins(); |
| 1628 | let errors = registry.metadata_validation_errors(Locale::En); |
| 1629 | assert!(errors.is_empty(), "metadata validation failed: {errors:?}"); |
| 1630 | |
| 1631 | let metadata = registry.metadata(Locale::En); |
| 1632 | assert_eq!(metadata.len(), registry.len()); |
| 1633 | |
| 1634 | let ids = metadata |
| 1635 | .iter() |
| 1636 | .map(|entry| entry.id.as_str()) |
| 1637 | .collect::<Vec<_>>(); |
| 1638 | let mut sorted_ids = ids.clone(); |
| 1639 | sorted_ids.sort_unstable(); |
| 1640 | assert_eq!( |
| 1641 | ids, sorted_ids, |
| 1642 | "registry metadata should have stable id order" |
| 1643 | ); |
| 1644 | assert_eq!( |
| 1645 | ids.iter().copied().collect::<BTreeSet<_>>().len(), |
| 1646 | ids.len(), |
| 1647 | "metadata ids must be unique" |
| 1648 | ); |
| 1649 | |
| 1650 | for entry in metadata { |
| 1651 | assert_eq!( |
| 1652 | HotbarActionCategory::parse(entry.category.as_str()), |
| 1653 | Some(entry.category) |
| 1654 | ); |
| 1655 | let entry_errors = entry.validation_errors(); |
| 1656 | assert!( |
| 1657 | entry_errors.is_empty(), |
| 1658 | "metadata entry failed validation: {entry_errors:?}" |
| 1659 | ); |
| 1660 | assert!( |
| 1661 | unicode_width::UnicodeWidthStr::width(entry.compact_label.as_str()) |
| 1662 | <= HOTBAR_COMPACT_LABEL_MAX_WIDTH, |
| 1663 | "compact label should be validated: {entry:?}" |
| 1664 | ); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | #[test] |
| 1669 | fn source_descriptors_cover_dispatch_boundaries() { |
| 1670 | let descriptors = hotbar_source_descriptors(); |
| 1671 | let categories = descriptors |
| 1672 | .iter() |
| 1673 | .map(|descriptor| descriptor.category) |
| 1674 | .collect::<BTreeSet<_>>(); |
| 1675 | |
| 1676 | assert_eq!( |
| 1677 | categories, |
| 1678 | BTreeSet::from([ |
| 1679 | HotbarActionCategory::App, |
| 1680 | HotbarActionCategory::Route, |
| 1681 | HotbarActionCategory::Slash, |
| 1682 | HotbarActionCategory::Mcp, |
| 1683 | HotbarActionCategory::Skill, |
| 1684 | HotbarActionCategory::Plugin, |
| 1685 | ]) |
| 1686 | ); |
| 1687 | assert_eq!( |
| 1688 | descriptors |
| 1689 | .iter() |
| 1690 | .find(|descriptor| descriptor.category == HotbarActionCategory::App) |
| 1691 | .map(|descriptor| ( |
| 1692 | descriptor.boundary, |
| 1693 | descriptor.safety_modes, |
| 1694 | descriptor.registers_dispatchable_actions() |
| 1695 | )), |
| 1696 | Some(( |
| 1697 | HotbarSourceDispatchBoundary::DirectApp, |
| 1698 | HOTBAR_DIRECT_APP_SAFETY, |
| 1699 | true |
| 1700 | )) |
| 1701 | ); |
| 1702 | assert_eq!( |
| 1703 | descriptors |
| 1704 | .iter() |
| 1705 | .find(|descriptor| descriptor.category == HotbarActionCategory::Route) |
| 1706 | .map(|descriptor| ( |
| 1707 | descriptor.boundary, |
| 1708 | descriptor.safety_modes, |
| 1709 | descriptor.registers_dispatchable_actions() |
| 1710 | )), |
| 1711 | Some(( |
| 1712 | HotbarSourceDispatchBoundary::ModelRoute, |
| 1713 | HOTBAR_ROUTE_SAFETY, |
| 1714 | true |
| 1715 | )) |
| 1716 | ); |
| 1717 | assert_eq!( |
| 1718 | descriptors |
| 1719 | .iter() |
| 1720 | .find(|descriptor| descriptor.category == HotbarActionCategory::Slash) |
| 1721 | .map(|descriptor| ( |
| 1722 | descriptor.boundary, |
| 1723 | descriptor.safety_modes, |
| 1724 | descriptor.registers_dispatchable_actions() |
| 1725 | )), |
| 1726 | Some(( |
| 1727 | HotbarSourceDispatchBoundary::SlashCommand, |
| 1728 | HOTBAR_SLASH_SAFETY, |
| 1729 | true |
| 1730 | )) |
| 1731 | ); |
| 1732 | assert_eq!( |
| 1733 | descriptors |
| 1734 | .iter() |
| 1735 | .find(|descriptor| descriptor.category == HotbarActionCategory::Mcp) |
| 1736 | .map(|descriptor| ( |
| 1737 | descriptor.boundary, |
| 1738 | descriptor.safety_modes, |
| 1739 | descriptor.registers_dispatchable_actions() |
| 1740 | )), |
| 1741 | Some(( |
| 1742 | HotbarSourceDispatchBoundary::ComposerPrefill, |
| 1743 | HOTBAR_MCP_SAFETY, |
| 1744 | true |
| 1745 | )) |
| 1746 | ); |
| 1747 | assert_eq!( |
| 1748 | descriptors |
| 1749 | .iter() |
| 1750 | .find(|descriptor| descriptor.category == HotbarActionCategory::Skill) |
| 1751 | .map(|descriptor| ( |
| 1752 | descriptor.boundary, |
| 1753 | descriptor.safety_modes, |
| 1754 | descriptor.registers_dispatchable_actions() |
| 1755 | )), |
| 1756 | Some(( |
| 1757 | HotbarSourceDispatchBoundary::SlashCommand, |
| 1758 | HOTBAR_SKILL_SAFETY, |
| 1759 | true |
| 1760 | )) |
| 1761 | ); |
| 1762 | let plugin = descriptors |
| 1763 | .iter() |
| 1764 | .find(|descriptor| descriptor.category == HotbarActionCategory::Plugin) |
| 1765 | .expect("missing descriptor for Plugin"); |
| 1766 | assert_eq!(plugin.boundary, HotbarSourceDispatchBoundary::Deferred); |
| 1767 | assert_eq!(plugin.safety_modes, HOTBAR_DEFERRED_SAFETY); |
| 1768 | assert_eq!(plugin.status, "exploratory"); |
| 1769 | assert!( |
| 1770 | !plugin.registers_dispatchable_actions(), |
| 1771 | "deferred Plugin source must not be dispatchable" |
| 1772 | ); |
| 1773 | } |
| 1774 | |
| 1775 | #[test] |
| 1776 | #[should_panic( |
| 1777 | expected = "deferred hotbar source Plugin must not register dispatchable actions" |
| 1778 | )] |
| 1779 | fn deferred_sources_cannot_register_dispatchable_actions() { |
| 1780 | let mut registry = HotbarActionRegistry::new(); |
| 1781 | registry.register_source(&DeferredTestHotbarSource); |
| 1782 | } |
| 1783 | |
| 1784 | #[test] |
| 1785 | fn source_adapters_register_previous_default_registry_surface() { |
| 1786 | let mut registry = HotbarActionRegistry::new(); |
| 1787 | registry.register_source(&BuiltinHotbarActionSource); |
| 1788 | registry.register_source(&SlashCommandHotbarActionSource); |
| 1789 | |
| 1790 | let adapter_ids = registry |
| 1791 | .iter() |
| 1792 | .map(|action| action.id().to_string()) |
| 1793 | .collect::<Vec<_>>(); |
| 1794 | let default_ids = HotbarActionRegistry::with_builtins() |
| 1795 | .iter() |
| 1796 | .map(|action| action.id().to_string()) |
| 1797 | .collect::<Vec<_>>(); |
| 1798 | |
| 1799 | assert_eq!(adapter_ids, default_ids); |
| 1800 | assert_eq!( |
| 1801 | BuiltinHotbarActionSource.descriptor().category, |
| 1802 | HotbarActionCategory::App |
| 1803 | ); |
| 1804 | assert_eq!( |
| 1805 | SlashCommandHotbarActionSource.descriptor().category, |
| 1806 | HotbarActionCategory::Slash |
| 1807 | ); |
| 1808 | } |
| 1809 | |
| 1810 | #[test] |
| 1811 | fn slash_source_matches_command_palette_command_entries() { |
| 1812 | // Hermetic: the palette builder also reads `~/.claude/skills`, so a |
| 1813 | // host with imported Claude skills would leak entries into one side |
| 1814 | // and fail the comparison. Isolate HOME the way the other |
| 1815 | // environment-reading tests in this file do. |
| 1816 | let _lock = crate::test_support::lock_test_env(); |
| 1817 | let tmp = tempfile::TempDir::new().expect("tempdir"); |
| 1818 | let _home = crate::test_support::EnvVarGuard::set("HOME", tmp.path()); |
| 1819 | let _user_profile = crate::test_support::EnvVarGuard::set("USERPROFILE", tmp.path()); |
| 1820 | let _codewhale_home = |
| 1821 | crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join(".codewhale")); |
| 1822 | let palette_slash_ids = build_command_palette_entries( |
| 1823 | Locale::En, |
| 1824 | tmp.path(), |
| 1825 | &codewhale_runtime::config::SkillsConfig { |
| 1826 | scan_codewhale_only: Some(true), |
| 1827 | ..Default::default() |
| 1828 | }, |
| 1829 | tmp.path(), |
| 1830 | &tmp.path().join("mcp.json"), |
| 1831 | None, |
| 1832 | &codewhale_plugins::PluginRegistry::empty(tmp.path()), |
| 1833 | ) |
| 1834 | .into_iter() |
| 1835 | .filter(|entry| entry.section() == crate::tui::command_palette::PaletteSection::Command) |
| 1836 | .filter_map(|entry| { |
| 1837 | entry |
| 1838 | .label |
| 1839 | .strip_prefix('/') |
| 1840 | .map(|name| format!("slash.{name}")) |
| 1841 | }) |
| 1842 | .collect::<BTreeSet<_>>(); |
| 1843 | |
| 1844 | let mut registry = HotbarActionRegistry::new(); |
| 1845 | registry.register_source(&SlashCommandHotbarActionSource); |
| 1846 | let hotbar_slash_ids = registry |
| 1847 | .iter() |
| 1848 | .map(|action| action.id().to_string()) |
| 1849 | .collect::<BTreeSet<_>>(); |
| 1850 | |
| 1851 | // The hotbar is a binding substrate and registers every command; the |
| 1852 | // palette is a browsing surface and omits the unlisted ones. So the |
| 1853 | // palette is a subset, and the difference is exactly the unlisted set. |
| 1854 | let unlisted_ids = commands::command_infos() |
| 1855 | .iter() |
| 1856 | .filter(|info| info.is_unlisted()) |
| 1857 | .map(|info| format!("slash.{}", info.name)) |
| 1858 | .collect::<BTreeSet<_>>(); |
| 1859 | assert!( |
| 1860 | palette_slash_ids.is_subset(&hotbar_slash_ids), |
| 1861 | "the palette must not offer a command the hotbar cannot bind" |
| 1862 | ); |
| 1863 | assert_eq!( |
| 1864 | hotbar_slash_ids |
| 1865 | .difference(&palette_slash_ids) |
| 1866 | .cloned() |
| 1867 | .collect::<BTreeSet<_>>(), |
| 1868 | unlisted_ids, |
| 1869 | "the only commands the hotbar has and the palette hides are the unlisted ones" |
| 1870 | ); |
| 1871 | } |
| 1872 | |
| 1873 | #[test] |
| 1874 | fn default_hotbar_actions_have_registered_default_metadata() { |
| 1875 | let registry = HotbarActionRegistry::with_builtins(); |
| 1876 | |
| 1877 | for id in codewhale_config::DEFAULT_HOTBAR_ACTIONS { |
| 1878 | let action = registry |
| 1879 | .get(id) |
| 1880 | .unwrap_or_else(|| panic!("missing default hotbar action {id}")); |
| 1881 | let metadata = action.metadata(Locale::En); |
| 1882 | if id.starts_with("slash.") { |
| 1883 | assert_eq!(metadata.category, HotbarActionCategory::Slash); |
| 1884 | } else { |
| 1885 | assert_eq!(metadata.category, HotbarActionCategory::App); |
| 1886 | assert_eq!(metadata.args, HotbarArgsBehavior::None); |
| 1887 | } |
| 1888 | assert_eq!(metadata.recommendation, HotbarRecommendation::Default); |
| 1889 | assert!( |
| 1890 | metadata.recommendation.is_recommendable(), |
| 1891 | "default action must be recommendable: {metadata:?}" |
| 1892 | ); |
| 1893 | assert!(!metadata.display_name.trim().is_empty()); |
| 1894 | assert!(!metadata.description.trim().is_empty()); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | #[test] |
| 1899 | fn slash_action_metadata_describes_args_and_recommendations() { |
| 1900 | let registry = HotbarActionRegistry::with_builtins(); |
| 1901 | |
| 1902 | let compact = registry |
| 1903 | .get("slash.compact") |
| 1904 | .expect("compact slash action") |
| 1905 | .metadata(Locale::En); |
| 1906 | assert_eq!(compact.category, HotbarActionCategory::Slash); |
| 1907 | assert_eq!(compact.source_id, "command:compact"); |
| 1908 | assert_eq!(compact.display_name, "/compact"); |
| 1909 | // `/compact [focus]` takes an optional summary focus (2026-07-23). |
| 1910 | assert_eq!(compact.args, HotbarArgsBehavior::Optional); |
| 1911 | assert_eq!(compact.safety, HotbarSafetyClass::ExistingCommand); |
| 1912 | assert_eq!(compact.recommendation, HotbarRecommendation::Eligible); |
| 1913 | |
| 1914 | let mode = registry |
| 1915 | .get("slash.mode") |
| 1916 | .expect("mode slash action") |
| 1917 | .metadata(Locale::En); |
| 1918 | assert_eq!(mode.args, HotbarArgsBehavior::Optional); |
| 1919 | |
| 1920 | let rename = registry |
| 1921 | .get("slash.rename") |
| 1922 | .expect("rename slash action") |
| 1923 | .metadata(Locale::En); |
| 1924 | assert_eq!(rename.args, HotbarArgsBehavior::Required); |
| 1925 | assert_eq!(rename.recommendation, HotbarRecommendation::Advanced); |
| 1926 | } |
| 1927 | |
| 1928 | #[test] |
| 1929 | fn reasoning_action_remains_available_for_auto_model_routing() { |
| 1930 | let registry = HotbarActionRegistry::with_builtins(); |
| 1931 | let reasoning = registry.get("reasoning.cycle").expect("reasoning action"); |
| 1932 | let mut app = test_app(); |
| 1933 | |
| 1934 | let metadata = reasoning.metadata(Locale::En); |
| 1935 | assert_eq!(metadata.category, HotbarActionCategory::App); |
| 1936 | assert_eq!(metadata.safety, HotbarSafetyClass::LocalState); |
| 1937 | assert_eq!(metadata.recommendation, HotbarRecommendation::Default); |
| 1938 | assert!(reasoning.disabled_reason(&app).is_none()); |
| 1939 | |
| 1940 | app.auto_model = true; |
| 1941 | assert!(reasoning.disabled_reason(&app).is_none()); |
| 1942 | } |
| 1943 | |
| 1944 | #[test] |
| 1945 | fn hotbar_recommendations_default_to_stable_slot_order() { |
| 1946 | let app = test_app(); |
| 1947 | |
| 1948 | let recommendations = |
| 1949 | recommend_hotbar_actions(&app, HotbarRecommendationOptions::default()); |
| 1950 | |
| 1951 | assert_eq!( |
| 1952 | recommendations |
| 1953 | .iter() |
| 1954 | .map(|entry| entry.metadata.id.as_str()) |
| 1955 | .collect::<Vec<_>>(), |
| 1956 | codewhale_config::DEFAULT_HOTBAR_ACTIONS |
| 1957 | ); |
| 1958 | assert!(recommendations.iter().all(|entry| { |
| 1959 | entry.metadata.recommendation == HotbarRecommendation::Default |
| 1960 | && entry.disabled_reason.is_none() |
| 1961 | })); |
| 1962 | } |
| 1963 | |
| 1964 | #[test] |
| 1965 | fn hotbar_recommendations_keep_reasoning_for_auto_model() { |
| 1966 | let mut app = test_app(); |
| 1967 | app.auto_model = true; |
| 1968 | |
| 1969 | let recommendations = |
| 1970 | recommend_hotbar_actions(&app, HotbarRecommendationOptions::for_setup_wizard()); |
| 1971 | |
| 1972 | assert!( |
| 1973 | recommendations |
| 1974 | .iter() |
| 1975 | .any(|entry| entry.metadata.id == "reasoning.cycle") |
| 1976 | ); |
| 1977 | } |
| 1978 | |
| 1979 | #[test] |
| 1980 | fn hotbar_recommendations_exclude_required_args_by_default() { |
| 1981 | let app = test_app(); |
| 1982 | |
| 1983 | let recommendations = |
| 1984 | recommend_hotbar_actions(&app, HotbarRecommendationOptions::for_setup_wizard()); |
| 1985 | |
| 1986 | assert!( |
| 1987 | !recommendations |
| 1988 | .iter() |
| 1989 | .any(|entry| entry.metadata.id == "slash.rename") |
| 1990 | ); |
| 1991 | } |
| 1992 | |
| 1993 | #[test] |
| 1994 | fn hotbar_recommendations_limit_eligible_actions_by_category() { |
| 1995 | let app = test_app(); |
| 1996 | let recommendations = recommend_hotbar_actions( |
| 1997 | &app, |
| 1998 | HotbarRecommendationOptions { |
| 1999 | max_total: usize::MAX, |
| 2000 | max_eligible_per_category: 1, |
| 2001 | include_required_args: false, |
| 2002 | }, |
| 2003 | ); |
| 2004 | |
| 2005 | for default_id in codewhale_config::DEFAULT_HOTBAR_ACTIONS { |
| 2006 | assert!( |
| 2007 | recommendations |
| 2008 | .iter() |
| 2009 | .any(|entry| entry.metadata.id == default_id), |
| 2010 | "default recommendation {default_id} should not be category-capped" |
| 2011 | ); |
| 2012 | } |
| 2013 | let slash_recommendations = recommendations |
| 2014 | .iter() |
| 2015 | .filter(|entry| entry.metadata.category == HotbarActionCategory::Slash) |
| 2016 | .collect::<Vec<_>>(); |
| 2017 | let default_slash = slash_recommendations |
| 2018 | .iter() |
| 2019 | .filter(|entry| entry.metadata.recommendation == HotbarRecommendation::Default) |
| 2020 | .count(); |
| 2021 | let eligible_slash = slash_recommendations |
| 2022 | .iter() |
| 2023 | .filter(|entry| entry.metadata.recommendation == HotbarRecommendation::Eligible) |
| 2024 | .count(); |
| 2025 | assert_eq!(default_slash, 3); |
| 2026 | assert_eq!(eligible_slash, 1); |
| 2027 | } |
| 2028 | |
| 2029 | #[test] |
| 2030 | fn builtins_register_expected_actions() { |
| 2031 | let mut registry = HotbarActionRegistry::new(); |
| 2032 | registry.register_builtins(); |
| 2033 | let ids = registry.iter().map(HotbarAction::id).collect::<Vec<_>>(); |
| 2034 | |
| 2035 | assert_eq!( |
| 2036 | ids, |
| 2037 | vec![ |
| 2038 | "filetree.toggle", |
| 2039 | "mode.agent", |
| 2040 | "mode.plan", |
| 2041 | "palette.open", |
| 2042 | "reasoning.cycle", |
| 2043 | "session.compact", |
| 2044 | "sidebar.toggle", |
| 2045 | "trust.toggle", |
| 2046 | "voice.toggle", |
| 2047 | ] |
| 2048 | ); |
| 2049 | assert!(registry.get("missing.action").is_none()); |
| 2050 | for action in registry.iter() { |
| 2051 | assert_eq!(action.category(), "app"); |
| 2052 | assert!( |
| 2053 | unicode_width::UnicodeWidthStr::width(action.short_label()) |
| 2054 | <= HOTBAR_COMPACT_LABEL_MAX_WIDTH, |
| 2055 | "{} has an overlong short label", |
| 2056 | action.id() |
| 2057 | ); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | #[test] |
| 2062 | fn app_starts_with_builtin_hotbar_registry() { |
| 2063 | let app = test_app(); |
| 2064 | assert!(app.hotbar_actions.len() > HotbarActionRegistry::with_builtins().len()); |
| 2065 | assert!(app.hotbar_actions.get("mode.agent").is_some()); |
| 2066 | assert!(app.hotbar_actions.get("slash.help").is_some()); |
| 2067 | assert!(app.hotbar_actions.get("slash.mode").is_some()); |
| 2068 | assert!( |
| 2069 | app.hotbar_actions |
| 2070 | .iter() |
| 2071 | .any(|action| action.metadata(Locale::En).category == HotbarActionCategory::Route) |
| 2072 | ); |
| 2073 | } |
| 2074 | |
| 2075 | #[test] |
| 2076 | fn configured_routes_register_provider_model_actions() { |
| 2077 | let mut config = Config::default(); |
| 2078 | config |
| 2079 | .provider_config_for_mut( |
| 2080 | &config |
| 2081 | .builtin_provider_identity(ProviderKind::Openrouter) |
| 2082 | .unwrap(), |
| 2083 | ) |
| 2084 | .unwrap() |
| 2085 | .model = Some("anthropic/claude-sonnet-4".to_string()); |
| 2086 | let mut provider_models = HashMap::new(); |
| 2087 | provider_models.insert( |
| 2088 | ProviderKind::Openrouter.as_str().to_string(), |
| 2089 | "openai/gpt-4o".to_string(), |
| 2090 | ); |
| 2091 | let registry = HotbarActionRegistry::with_configured_routes( |
| 2092 | &config, |
| 2093 | Some(&config.active_provider_identity().unwrap()), |
| 2094 | "deepseek-v4-pro", |
| 2095 | &provider_models, |
| 2096 | ); |
| 2097 | |
| 2098 | let active = registry |
| 2099 | .get("route.deepseek.deepseek-v4-pro") |
| 2100 | .expect("active DeepSeek route"); |
| 2101 | assert_eq!(active.category(), "route"); |
| 2102 | assert_eq!( |
| 2103 | active.metadata(Locale::En).safety, |
| 2104 | HotbarSafetyClass::ConfigChange |
| 2105 | ); |
| 2106 | |
| 2107 | let openrouter = registry |
| 2108 | .get("route.openrouter.anthropic/claude-sonnet-4") |
| 2109 | .expect("configured OpenRouter route"); |
| 2110 | let metadata = openrouter.metadata(Locale::En); |
| 2111 | assert_eq!(metadata.category, HotbarActionCategory::Route); |
| 2112 | assert!(metadata.display_name.contains("OpenRouter")); |
| 2113 | assert!(metadata.display_name.contains("anthropic/claude-sonnet-4")); |
| 2114 | |
| 2115 | let mut app = test_app(); |
| 2116 | assert_eq!( |
| 2117 | openrouter |
| 2118 | .dispatch(&mut app, &config) |
| 2119 | .expect("dispatch route"), |
| 2120 | HotbarDispatch::AppAction(AppAction::SwitchModelRoute { |
| 2121 | identity: config |
| 2122 | .builtin_provider_identity(ProviderKind::Openrouter) |
| 2123 | .unwrap(), |
| 2124 | model: "anthropic/claude-sonnet-4".to_string(), |
| 2125 | }) |
| 2126 | ); |
| 2127 | } |
| 2128 | |
| 2129 | #[test] |
| 2130 | fn slash_commands_register_as_hotbar_actions() { |
| 2131 | let registry = HotbarActionRegistry::with_builtins(); |
| 2132 | |
| 2133 | for info in commands::command_infos() { |
| 2134 | let action_id = format!("slash.{}", info.name); |
| 2135 | let action = registry |
| 2136 | .get(&action_id) |
| 2137 | .unwrap_or_else(|| panic!("missing slash hotbar action for /{}", info.name)); |
| 2138 | assert_eq!(action.category(), "slash"); |
| 2139 | assert!(!action.is_active(&test_app())); |
| 2140 | assert!( |
| 2141 | unicode_width::UnicodeWidthStr::width(action.short_label()) |
| 2142 | <= HOTBAR_COMPACT_LABEL_MAX_WIDTH, |
| 2143 | "{action_id} has an overlong short label" |
| 2144 | ); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /// #1888: the hotbar is not a control surface. It binds the owning slash |
| 2149 | /// command and dispatches it through `commands::execute` with no argument, |
| 2150 | /// so what runs is the slash surface — there is no hotbar verb table and |
| 2151 | /// no `ControlSurface::Hotbar` for a test to assert into existence. |
| 2152 | /// |
| 2153 | /// What must hold is narrower and real: every owning command is bound and |
| 2154 | /// directly dispatchable, and a bare press can only reach a verb that |
| 2155 | /// declares `hotbar_bare_dispatch` — which is necessarily targetless and |
| 2156 | /// read-only, because a keypress supplies no id. |
| 2157 | #[test] |
| 2158 | fn control_plane_commands_are_bound_and_bare_dispatch_is_read_only() { |
| 2159 | use codewhale_lane::control::OPERATIONS; |
| 2160 | use codewhale_lane::{ControlAuthority, ControlSurface, TargetKind}; |
| 2161 | |
| 2162 | let registry = HotbarActionRegistry::with_builtins(); |
| 2163 | for descriptor in OPERATIONS { |
| 2164 | let action_id = descriptor.hotbar_action_id(); |
| 2165 | assert_eq!(action_id, format!("slash.{}", descriptor.slash_command)); |
| 2166 | let action = registry |
| 2167 | .get(&action_id) |
| 2168 | .unwrap_or_else(|| panic!("{} has no hotbar action {action_id}", descriptor.id)); |
| 2169 | assert_eq!(action.category(), "slash"); |
| 2170 | // The dispatch runs as the slash surface, which must therefore be |
| 2171 | // one the descriptor actually offers. |
| 2172 | assert!(descriptor.offers(ControlSurface::Slash)); |
| 2173 | |
| 2174 | // A bare hotbar press fires the command with no arguments, so the |
| 2175 | // owning command must never require one — otherwise the slot would |
| 2176 | // silently become a composer prefill instead of the verb. |
| 2177 | let info = commands::get_command_info(descriptor.slash_command) |
| 2178 | .unwrap_or_else(|| panic!("/{} is not registered", descriptor.slash_command)); |
| 2179 | assert!( |
| 2180 | !info.requires_required_argument(), |
| 2181 | "/{} must stay directly dispatchable", |
| 2182 | descriptor.slash_command |
| 2183 | ); |
| 2184 | |
| 2185 | if descriptor.hotbar_bare_dispatch { |
| 2186 | assert_eq!(descriptor.target, TargetKind::None, "{}", descriptor.id); |
| 2187 | assert_eq!( |
| 2188 | descriptor.authority, |
| 2189 | ControlAuthority::Read, |
| 2190 | "{} would mutate durable state from one keypress", |
| 2191 | descriptor.id |
| 2192 | ); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | // Both control domains are bound. |
| 2197 | for id in ["slash.lane", "slash.fleet"] { |
| 2198 | assert!(registry.get(id).is_some(), "{id} must be bindable"); |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | #[test] |
| 2203 | fn retired_slash_pod_binding_stays_unbound() { |
| 2204 | let registry = HotbarActionRegistry::with_builtins(); |
| 2205 | assert!( |
| 2206 | registry.get("slash.pod").is_none(), |
| 2207 | "the retired pod id must not resolve to any action" |
| 2208 | ); |
| 2209 | } |
| 2210 | |
| 2211 | #[test] |
| 2212 | fn slash_hotbar_action_dispatches_argless_command() { |
| 2213 | let registry = HotbarActionRegistry::with_builtins(); |
| 2214 | let mode = registry.get("slash.mode").expect("mode slash action"); |
| 2215 | let mut app = test_app(); |
| 2216 | |
| 2217 | assert_eq!( |
| 2218 | mode.dispatch(&mut app, &Config::default()) |
| 2219 | .expect("dispatch /mode"), |
| 2220 | HotbarDispatch::AppAction(AppAction::OpenModePicker) |
| 2221 | ); |
| 2222 | assert!(app.input.is_empty()); |
| 2223 | } |
| 2224 | |
| 2225 | #[test] |
| 2226 | fn slash_hotbar_action_dispatches_optional_argument_command_with_no_args() { |
| 2227 | let registry = HotbarActionRegistry::with_builtins(); |
| 2228 | let task = registry.get("slash.task").expect("task slash action"); |
| 2229 | let mut app = test_app(); |
| 2230 | |
| 2231 | assert_eq!( |
| 2232 | task.dispatch(&mut app, &Config::default()) |
| 2233 | .expect("dispatch /task"), |
| 2234 | HotbarDispatch::AppAction(AppAction::TaskList) |
| 2235 | ); |
| 2236 | assert!(app.input.is_empty()); |
| 2237 | } |
| 2238 | |
| 2239 | #[test] |
| 2240 | fn slash_hotbar_action_prefills_required_argument_command() { |
| 2241 | let registry = HotbarActionRegistry::with_builtins(); |
| 2242 | let rename = registry.get("slash.rename").expect("rename slash action"); |
| 2243 | let mut app = test_app(); |
| 2244 | app.input = "draft".to_string(); |
| 2245 | app.cursor_position = app.input.chars().count(); |
| 2246 | |
| 2247 | assert_eq!( |
| 2248 | rename |
| 2249 | .dispatch(&mut app, &Config::default()) |
| 2250 | .expect("dispatch /rename"), |
| 2251 | HotbarDispatch::Handled |
| 2252 | ); |
| 2253 | assert_eq!(app.input, "/rename "); |
| 2254 | assert_eq!(app.cursor_position, app.input.chars().count()); |
| 2255 | assert_eq!(app.clear_undo_buffer.as_deref(), Some("draft")); |
| 2256 | assert_eq!( |
| 2257 | app.visible_notice_text().as_deref(), |
| 2258 | Some("Command needs arguments; complete /rename") |
| 2259 | ); |
| 2260 | } |
| 2261 | |
| 2262 | #[test] |
| 2263 | fn skill_source_registers_known_skills_with_dedup() { |
| 2264 | let skills = vec![ |
| 2265 | ("demo".to_string(), "Demo skill".to_string()), |
| 2266 | ("demo".to_string(), "Shadowed duplicate".to_string()), |
| 2267 | (" ".to_string(), "ignored blank name".to_string()), |
| 2268 | ]; |
| 2269 | let mut registry = HotbarActionRegistry::new(); |
| 2270 | registry.register_skills(&skills); |
| 2271 | |
| 2272 | assert_eq!(registry.len(), 1); |
| 2273 | let action = registry.get("skill.demo").expect("skill action"); |
| 2274 | assert_eq!(action.category(), "skill"); |
| 2275 | let metadata = action.metadata(Locale::En); |
| 2276 | assert_eq!(metadata.category, HotbarActionCategory::Skill); |
| 2277 | assert_eq!(metadata.source_id, "skill:demo"); |
| 2278 | assert_eq!(metadata.display_name, "$demo"); |
| 2279 | assert_eq!(metadata.description, "Demo skill"); |
| 2280 | assert_eq!(metadata.args, HotbarArgsBehavior::None); |
| 2281 | assert_eq!(metadata.safety, HotbarSafetyClass::ExistingCommand); |
| 2282 | assert_eq!(metadata.recommendation, HotbarRecommendation::Eligible); |
| 2283 | assert!(registry.metadata_validation_errors(Locale::En).is_empty()); |
| 2284 | } |
| 2285 | |
| 2286 | #[test] |
| 2287 | fn replacing_skills_removes_stale_plugin_actions_atomically() { |
| 2288 | let mut registry = HotbarActionRegistry::with_builtins(); |
| 2289 | registry.register_skills(&[ |
| 2290 | ("native".to_string(), "native Skill".to_string()), |
| 2291 | ( |
| 2292 | "demo:review".to_string(), |
| 2293 | "reviewed plugin Skill".to_string(), |
| 2294 | ), |
| 2295 | ]); |
| 2296 | assert!(registry.get("skill.demo:review").is_some()); |
| 2297 | let builtin_count = registry |
| 2298 | .iter() |
| 2299 | .filter(|action| action.category() != HotbarActionCategory::Skill.as_str()) |
| 2300 | .count(); |
| 2301 | |
| 2302 | registry.replace_skills(&[("native".to_string(), "refreshed".to_string())]); |
| 2303 | |
| 2304 | assert!(registry.get("skill.demo:review").is_none()); |
| 2305 | assert!(registry.get("skill.native").is_some()); |
| 2306 | assert_eq!( |
| 2307 | registry |
| 2308 | .iter() |
| 2309 | .filter(|action| action.category() != HotbarActionCategory::Skill.as_str()) |
| 2310 | .count(), |
| 2311 | builtin_count, |
| 2312 | "refresh must preserve every non-Skill action source" |
| 2313 | ); |
| 2314 | } |
| 2315 | |
| 2316 | #[test] |
| 2317 | fn skill_hotbar_action_activates_skill_through_dollar_alias() { |
| 2318 | let workspace = tempfile::TempDir::new().expect("workspace"); |
| 2319 | let skills_dir = tempfile::TempDir::new().expect("skills dir"); |
| 2320 | let skill_dir = skills_dir.path().join("hotbar-demo-skill"); |
| 2321 | std::fs::create_dir_all(&skill_dir).expect("skill dir"); |
| 2322 | std::fs::write( |
| 2323 | skill_dir.join("SKILL.md"), |
| 2324 | "---\nname: hotbar-demo-skill\ndescription: Demo skill for hotbar tests\n---\n\nFollow the demo instructions.\n", |
| 2325 | ) |
| 2326 | .expect("write SKILL.md"); |
| 2327 | let config = { |
| 2328 | let __cw_config_init_0_0 = Some(skills_dir.path().to_string_lossy().into_owned()); |
| 2329 | let mut __cw_config_init_0_value: Config = Config::default(); |
| 2330 | __cw_config_init_0_value.skills_dir = __cw_config_init_0_0; |
| 2331 | __cw_config_init_0_value |
| 2332 | }; |
| 2333 | let mut app = test_app_with_paths_and_config( |
| 2334 | workspace.path().to_path_buf(), |
| 2335 | skills_dir.path().to_path_buf(), |
| 2336 | &config, |
| 2337 | ); |
| 2338 | |
| 2339 | let action = app |
| 2340 | .hotbar_actions |
| 2341 | .get("skill.hotbar-demo-skill") |
| 2342 | .expect("skill registered from the startup skill cache"); |
| 2343 | assert!(!action.is_active(&app)); |
| 2344 | assert_eq!( |
| 2345 | action.dispatch(&mut app, &config).expect("dispatch skill"), |
| 2346 | HotbarDispatch::Handled |
| 2347 | ); |
| 2348 | assert!(app.active_skill.is_some()); |
| 2349 | assert!(action.is_active(&app)); |
| 2350 | assert!( |
| 2351 | app.visible_notice_text() |
| 2352 | .as_deref() |
| 2353 | .is_some_and(|message| message.contains("activated")) |
| 2354 | ); |
| 2355 | } |
| 2356 | |
| 2357 | #[test] |
| 2358 | fn skill_hotbar_action_reports_unknown_skill() { |
| 2359 | let workspace = tempfile::TempDir::new().expect("workspace"); |
| 2360 | let skills_dir = tempfile::TempDir::new().expect("skills dir"); |
| 2361 | let mut app = test_app_with_paths( |
| 2362 | workspace.path().to_path_buf(), |
| 2363 | skills_dir.path().to_path_buf(), |
| 2364 | ); |
| 2365 | |
| 2366 | let mut registry = HotbarActionRegistry::new(); |
| 2367 | registry.register_skills(&[( |
| 2368 | "hotbar-skill-that-does-not-exist".to_string(), |
| 2369 | "Stale cache entry".to_string(), |
| 2370 | )]); |
| 2371 | let action = registry |
| 2372 | .get("skill.hotbar-skill-that-does-not-exist") |
| 2373 | .expect("stale skill action"); |
| 2374 | |
| 2375 | assert_eq!( |
| 2376 | action |
| 2377 | .dispatch(&mut app, &Config::default()) |
| 2378 | .expect("dispatch stale skill"), |
| 2379 | HotbarDispatch::Handled |
| 2380 | ); |
| 2381 | assert!(app.active_skill.is_none()); |
| 2382 | assert!( |
| 2383 | app.visible_notice_text() |
| 2384 | .as_deref() |
| 2385 | .is_some_and(|message| message.contains("Unknown skill")) |
| 2386 | ); |
| 2387 | } |
| 2388 | |
| 2389 | #[test] |
| 2390 | fn mcp_source_registers_enabled_server_tools_only() { |
| 2391 | let snapshot = test_mcp_snapshot(); |
| 2392 | let mut registry = HotbarActionRegistry::new(); |
| 2393 | registry.replace_mcp_tools(Some(&snapshot)); |
| 2394 | |
| 2395 | // Only the enabled server's tool with a model name registers: the |
| 2396 | // blank-model-name tool and the disabled server's tool never do. |
| 2397 | assert_eq!(registry.len(), 1); |
| 2398 | let action = registry |
| 2399 | .get("mcp.search.web_search") |
| 2400 | .expect("mcp tool action"); |
| 2401 | assert_eq!(action.category(), "mcp"); |
| 2402 | let metadata = action.metadata(Locale::En); |
| 2403 | assert_eq!(metadata.category, HotbarActionCategory::Mcp); |
| 2404 | assert_eq!(metadata.source_id, "mcp:search"); |
| 2405 | assert_eq!(metadata.display_name, "mcp:search:web_search"); |
| 2406 | assert!(metadata.description.contains("mcp_search_web_search")); |
| 2407 | assert!(metadata.description.contains("Search the web")); |
| 2408 | assert_eq!(metadata.args, HotbarArgsBehavior::Required); |
| 2409 | assert_eq!(metadata.safety, HotbarSafetyClass::RequiresApproval); |
| 2410 | assert_eq!(metadata.recommendation, HotbarRecommendation::Advanced); |
| 2411 | assert!(registry.metadata_validation_errors(Locale::En).is_empty()); |
| 2412 | } |
| 2413 | |
| 2414 | #[test] |
| 2415 | fn mcp_hotbar_action_prefills_composer_instead_of_executing() { |
| 2416 | let snapshot = test_mcp_snapshot(); |
| 2417 | let mut registry = HotbarActionRegistry::new(); |
| 2418 | registry.replace_mcp_tools(Some(&snapshot)); |
| 2419 | let action = registry |
| 2420 | .get("mcp.search.web_search") |
| 2421 | .expect("mcp tool action"); |
| 2422 | let mut app = test_app(); |
| 2423 | app.input = "draft".to_string(); |
| 2424 | app.cursor_position = app.input.chars().count(); |
| 2425 | |
| 2426 | assert_eq!( |
| 2427 | action |
| 2428 | .dispatch(&mut app, &Config::default()) |
| 2429 | .expect("dispatch mcp tool"), |
| 2430 | HotbarDispatch::Handled |
| 2431 | ); |
| 2432 | assert_eq!(app.input, "mcp_search_web_search "); |
| 2433 | assert_eq!(app.cursor_position, app.input.chars().count()); |
| 2434 | assert_eq!(app.clear_undo_buffer.as_deref(), Some("draft")); |
| 2435 | assert!( |
| 2436 | app.visible_notice_text() |
| 2437 | .as_deref() |
| 2438 | .is_some_and(|message| message.contains("mcp_search_web_search")) |
| 2439 | ); |
| 2440 | } |
| 2441 | |
| 2442 | #[test] |
| 2443 | fn replace_mcp_tools_refreshes_and_clears_mcp_actions() { |
| 2444 | let mut registry = HotbarActionRegistry::with_builtins(); |
| 2445 | let baseline = registry.len(); |
| 2446 | let snapshot = test_mcp_snapshot(); |
| 2447 | |
| 2448 | registry.replace_mcp_tools(Some(&snapshot)); |
| 2449 | assert_eq!(registry.len(), baseline + 1); |
| 2450 | // Re-applying a refreshed snapshot must not panic on duplicate ids. |
| 2451 | registry.replace_mcp_tools(Some(&snapshot)); |
| 2452 | assert_eq!(registry.len(), baseline + 1); |
| 2453 | |
| 2454 | registry.replace_mcp_tools(None); |
| 2455 | assert_eq!(registry.len(), baseline); |
| 2456 | assert!(registry.get("mcp.search.web_search").is_none()); |
| 2457 | } |
| 2458 | |
| 2459 | #[test] |
| 2460 | fn mode_actions_report_active_state_and_dispatch() { |
| 2461 | let registry = HotbarActionRegistry::with_builtins(); |
| 2462 | let plan = registry.get("mode.plan").expect("plan action"); |
| 2463 | let agent = registry.get("mode.agent").expect("agent action"); |
| 2464 | let mut app = test_app(); |
| 2465 | |
| 2466 | assert!(agent.is_active(&app)); |
| 2467 | assert!(!plan.is_active(&app)); |
| 2468 | assert!(registry.get("mode.yolo").is_none()); |
| 2469 | |
| 2470 | assert_eq!( |
| 2471 | plan.dispatch(&mut app, &Config::default()) |
| 2472 | .expect("dispatch plan"), |
| 2473 | HotbarDispatch::AppAction(AppAction::ModeChanged(AppMode::Plan)) |
| 2474 | ); |
| 2475 | assert_eq!(app.mode, AppMode::Plan); |
| 2476 | assert!(plan.is_active(&app)); |
| 2477 | assert!(!agent.is_active(&app)); |
| 2478 | assert!(registry.get("mode.operate").is_none()); |
| 2479 | } |
| 2480 | |
| 2481 | #[test] |
| 2482 | fn compact_action_emits_existing_app_action() { |
| 2483 | let registry = HotbarActionRegistry::with_builtins(); |
| 2484 | let compact = registry.get("session.compact").expect("compact action"); |
| 2485 | let mut app = test_app(); |
| 2486 | |
| 2487 | assert!(!compact.is_active(&app)); |
| 2488 | assert_eq!( |
| 2489 | compact |
| 2490 | .dispatch(&mut app, &Config::default()) |
| 2491 | .expect("dispatch compact"), |
| 2492 | HotbarDispatch::AppAction(AppAction::CompactContext { focus: None }) |
| 2493 | ); |
| 2494 | app.is_compacting = true; |
| 2495 | assert!(compact.is_active(&app)); |
| 2496 | assert_eq!( |
| 2497 | compact |
| 2498 | .dispatch(&mut app, &Config::default()) |
| 2499 | .expect("dispatch compact while busy"), |
| 2500 | HotbarDispatch::AppAction(AppAction::CompactContext { focus: None }) |
| 2501 | ); |
| 2502 | } |
| 2503 | |
| 2504 | #[test] |
| 2505 | fn reasoning_cycle_updates_effort_and_compaction() { |
| 2506 | let registry = HotbarActionRegistry::with_builtins(); |
| 2507 | let reasoning = registry.get("reasoning.cycle").expect("reasoning action"); |
| 2508 | let mut app = test_app(); |
| 2509 | app.api_provider = ProviderKind::Deepseek; |
| 2510 | app.reasoning_effort = ReasoningEffort::Off; |
| 2511 | |
| 2512 | assert!(!reasoning.is_active(&app)); |
| 2513 | assert!(matches!( |
| 2514 | reasoning |
| 2515 | .dispatch(&mut app, &Config::default()) |
| 2516 | .expect("dispatch reasoning"), |
| 2517 | HotbarDispatch::AppAction(AppAction::UpdateCompaction(_)) |
| 2518 | )); |
| 2519 | assert_eq!(app.reasoning_effort, ReasoningEffort::Low); |
| 2520 | assert!(reasoning.is_active(&app)); |
| 2521 | assert_eq!( |
| 2522 | app.status_toasts.back().map(|notice| notice.text.as_str()), |
| 2523 | Some("Reasoning effort: low") |
| 2524 | ); |
| 2525 | |
| 2526 | app.auto_model = true; |
| 2527 | assert!(reasoning.is_active(&app)); |
| 2528 | assert!(matches!( |
| 2529 | reasoning |
| 2530 | .dispatch(&mut app, &Config::default()) |
| 2531 | .expect("dispatch reasoning under auto model"), |
| 2532 | HotbarDispatch::AppAction(AppAction::UpdateCompaction(_)) |
| 2533 | )); |
| 2534 | assert_eq!(app.reasoning_effort, ReasoningEffort::Medium); |
| 2535 | } |
| 2536 | |
| 2537 | #[test] |
| 2538 | fn reasoning_cycle_is_inert_while_a_turn_is_running() { |
| 2539 | let _lock = crate::test_support::lock_test_env(); |
| 2540 | let tmp = tempfile::TempDir::new().expect("tempdir"); |
| 2541 | let _home = crate::test_support::EnvVarGuard::set("HOME", tmp.path()); |
| 2542 | let _user_profile = crate::test_support::EnvVarGuard::set("USERPROFILE", tmp.path()); |
| 2543 | let _codewhale_home = |
| 2544 | crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join(".codewhale")); |
| 2545 | let _deepseek_config = crate::test_support::EnvVarGuard::remove("DEEPSEEK_CONFIG_PATH"); |
| 2546 | let _codewhale_config = crate::test_support::EnvVarGuard::remove("CODEWHALE_CONFIG_PATH"); |
| 2547 | let _writes = crate::tui::startup_defaults::allow_writes_in_tests(); |
| 2548 | |
| 2549 | codewhale_runtime::settings::Settings::transact(|settings| { |
| 2550 | settings.reasoning_effort = Some("off".to_string()); |
| 2551 | Ok(()) |
| 2552 | }) |
| 2553 | .expect("seed startup reasoning"); |
| 2554 | |
| 2555 | let registry = HotbarActionRegistry::with_builtins(); |
| 2556 | let reasoning = registry.get("reasoning.cycle").expect("reasoning action"); |
| 2557 | let mut app = test_app(); |
| 2558 | app.api_provider = ProviderKind::Deepseek; |
| 2559 | app.auto_model = false; |
| 2560 | app.reasoning_effort = ReasoningEffort::Off; |
| 2561 | app.is_loading = true; |
| 2562 | |
| 2563 | assert_eq!( |
| 2564 | reasoning |
| 2565 | .dispatch(&mut app, &Config::default()) |
| 2566 | .expect("dispatch while busy"), |
| 2567 | HotbarDispatch::Handled |
| 2568 | ); |
| 2569 | assert_eq!(app.reasoning_effort, ReasoningEffort::Off); |
| 2570 | assert_eq!(app.startup_defaults.pending_len(), 0); |
| 2571 | assert_eq!( |
| 2572 | codewhale_runtime::settings::Settings::load() |
| 2573 | .expect("reload settings") |
| 2574 | .reasoning_effort |
| 2575 | .as_deref(), |
| 2576 | Some("off"), |
| 2577 | "a refused hotbar action must not persist a different tier" |
| 2578 | ); |
| 2579 | } |
| 2580 | |
| 2581 | #[test] |
| 2582 | fn reasoning_cycle_uses_codex_effort_tiers() { |
| 2583 | // Codex tiers are now per-model, read from the OAuth roster. Point |
| 2584 | // CODEX_HOME at an empty directory so this exercises the static |
| 2585 | // fallback ladder instead of whatever roster the developer's own |
| 2586 | // machine happens to have cached. |
| 2587 | let _lock = crate::test_support::lock_test_env(); |
| 2588 | let codex_home = tempfile::TempDir::new().expect("codex home"); |
| 2589 | let _codex_home = crate::test_support::EnvVarGuard::set("CODEX_HOME", codex_home.path()); |
| 2590 | let registry = HotbarActionRegistry::with_builtins(); |
| 2591 | let reasoning = registry.get("reasoning.cycle").expect("reasoning action"); |
| 2592 | let mut app = test_app(); |
| 2593 | app.set_provider_identity_record( |
| 2594 | codewhale_runtime::config::Config::default() |
| 2595 | .resolve_provider_identity(ProviderKind::OpenaiCodex.as_str()) |
| 2596 | .expect("captured fixture provider"), |
| 2597 | ); |
| 2598 | app.auto_model = false; |
| 2599 | app.reasoning_effort = ReasoningEffort::Low; |
| 2600 | |
| 2601 | for (expected_effort, expected_label) in [ |
| 2602 | (ReasoningEffort::Medium, "medium"), |
| 2603 | (ReasoningEffort::High, "high"), |
| 2604 | (ReasoningEffort::Max, "max"), |
| 2605 | (ReasoningEffort::Low, "low"), |
| 2606 | ] { |
| 2607 | assert!(matches!( |
| 2608 | reasoning |
| 2609 | .dispatch(&mut app, &Config::default()) |
| 2610 | .expect("dispatch reasoning"), |
| 2611 | HotbarDispatch::AppAction(AppAction::UpdateCompaction(_)) |
| 2612 | )); |
| 2613 | assert_eq!(app.reasoning_effort, expected_effort); |
| 2614 | let expected_message = format!("Reasoning effort: {expected_label}"); |
| 2615 | assert_eq!( |
| 2616 | app.status_toasts.back().map(|notice| notice.text.as_str()), |
| 2617 | Some(expected_message.as_str()) |
| 2618 | ); |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | #[test] |
| 2623 | fn sidebar_toggle_reports_visibility_and_dispatches() { |
| 2624 | let registry = HotbarActionRegistry::with_builtins(); |
| 2625 | let sidebar = registry.get("sidebar.toggle").expect("sidebar action"); |
| 2626 | let mut app = test_app(); |
| 2627 | app.work_surface.placement = crate::tui::work_surface::WorkSurfacePlacement::Top; |
| 2628 | |
| 2629 | assert!(sidebar.is_active(&app)); |
| 2630 | assert_eq!( |
| 2631 | sidebar |
| 2632 | .dispatch(&mut app, &Config::default()) |
| 2633 | .expect("dispatch rail hide"), |
| 2634 | HotbarDispatch::Handled |
| 2635 | ); |
| 2636 | assert_eq!( |
| 2637 | app.work_surface.placement, |
| 2638 | crate::tui::work_surface::WorkSurfacePlacement::Off |
| 2639 | ); |
| 2640 | assert!(!sidebar.is_active(&app)); |
| 2641 | |
| 2642 | sidebar |
| 2643 | .dispatch(&mut app, &Config::default()) |
| 2644 | .expect("dispatch rail show"); |
| 2645 | assert_eq!( |
| 2646 | app.work_surface.placement, |
| 2647 | crate::tui::work_surface::WorkSurfacePlacement::Bottom, |
| 2648 | "toggling the rail back on restores the round-3 default" |
| 2649 | ); |
| 2650 | assert!(sidebar.is_active(&app)); |
| 2651 | } |
| 2652 | |
| 2653 | #[tokio::test] |
| 2654 | async fn filetree_toggle_reports_open_state_and_dispatches() { |
| 2655 | let registry = HotbarActionRegistry::with_builtins(); |
| 2656 | let filetree = registry.get("filetree.toggle").expect("filetree action"); |
| 2657 | let mut app = test_app(); |
| 2658 | |
| 2659 | assert!(!filetree.is_active(&app)); |
| 2660 | assert_eq!( |
| 2661 | filetree |
| 2662 | .dispatch(&mut app, &Config::default()) |
| 2663 | .expect("dispatch filetree open"), |
| 2664 | HotbarDispatch::Handled |
| 2665 | ); |
| 2666 | assert!(app.file_tree.is_some()); |
| 2667 | assert!(filetree.is_active(&app)); |
| 2668 | |
| 2669 | filetree |
| 2670 | .dispatch(&mut app, &Config::default()) |
| 2671 | .expect("dispatch filetree close"); |
| 2672 | assert!(app.file_tree.is_none()); |
| 2673 | assert!(!filetree.is_active(&app)); |
| 2674 | } |
| 2675 | |
| 2676 | #[test] |
| 2677 | fn palette_action_opens_command_palette() { |
| 2678 | let registry = HotbarActionRegistry::with_builtins(); |
| 2679 | let palette = registry.get("palette.open").expect("palette action"); |
| 2680 | let mut app = test_app(); |
| 2681 | |
| 2682 | assert!(!palette.is_active(&app)); |
| 2683 | assert_eq!( |
| 2684 | palette |
| 2685 | .dispatch(&mut app, &Config::default()) |
| 2686 | .expect("dispatch palette"), |
| 2687 | HotbarDispatch::Handled |
| 2688 | ); |
| 2689 | assert_eq!(app.view_stack.top_kind(), Some(ModalKind::CommandPalette)); |
| 2690 | } |
| 2691 | |
| 2692 | #[test] |
| 2693 | fn trust_toggle_reports_trust_state_and_dispatches() { |
| 2694 | let registry = HotbarActionRegistry::with_builtins(); |
| 2695 | let trust = registry.get("trust.toggle").expect("trust action"); |
| 2696 | let mut app = test_app(); |
| 2697 | app.trust_mode = false; |
| 2698 | |
| 2699 | assert!(!trust.is_active(&app)); |
| 2700 | assert_eq!( |
| 2701 | trust |
| 2702 | .dispatch(&mut app, &Config::default()) |
| 2703 | .expect("dispatch trust on"), |
| 2704 | HotbarDispatch::Handled |
| 2705 | ); |
| 2706 | assert!(app.trust_mode); |
| 2707 | assert!(trust.is_active(&app)); |
| 2708 | |
| 2709 | trust |
| 2710 | .dispatch(&mut app, &Config::default()) |
| 2711 | .expect("dispatch trust off"); |
| 2712 | assert!(!app.trust_mode); |
| 2713 | assert!(!trust.is_active(&app)); |
| 2714 | } |
| 2715 | |
| 2716 | #[test] |
| 2717 | fn voice_toggle_dispatches_the_voice_command() { |
| 2718 | let registry = HotbarActionRegistry::with_builtins(); |
| 2719 | let voice = registry.get("voice.toggle").expect("voice action"); |
| 2720 | let mut app = test_app(); |
| 2721 | |
| 2722 | assert!(!voice.is_active(&app)); |
| 2723 | // The toggle is wired to the /voice command. With a recorder on the |
| 2724 | // host it arms voice input and defers capture to the UI event loop; |
| 2725 | // without one it fails gracefully with a localized error. No audio |
| 2726 | // is recorded in either case. |
| 2727 | let result = voice |
| 2728 | .dispatch(&mut app, &Config::default()) |
| 2729 | .expect("dispatch voice"); |
| 2730 | assert!(app.visible_notice_text().is_some()); |
| 2731 | // The old placeholder message must be gone — voice is implemented. |
| 2732 | assert_ne!( |
| 2733 | app.visible_notice_text().as_deref(), |
| 2734 | Some("Voice input is not available in this terminal session yet.") |
| 2735 | ); |
| 2736 | if app.voice_enabled { |
| 2737 | assert_eq!( |
| 2738 | result, |
| 2739 | HotbarDispatch::AppAction(crate::tui::app::AppAction::VoiceCapture) |
| 2740 | ); |
| 2741 | assert!(voice.is_active(&app)); |
| 2742 | // A second press toggles voice input back off. |
| 2743 | let off = voice |
| 2744 | .dispatch(&mut app, &Config::default()) |
| 2745 | .expect("dispatch voice off"); |
| 2746 | assert_eq!(off, HotbarDispatch::Handled); |
| 2747 | assert!(!app.voice_enabled); |
| 2748 | assert!(!voice.is_active(&app)); |
| 2749 | } else { |
| 2750 | assert_eq!(result, HotbarDispatch::Handled); |
| 2751 | } |
| 2752 | } |
| 2753 | } |
| 2754 |