| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::collections::{BTreeMap, BTreeSet}; |
| 3 | use std::fmt; |
| 4 | |
| 5 | pub const HOTBAR_SLOT_COUNT: u8 = 8; |
| 6 | |
| 7 | pub const DEFAULT_HOTBAR_ACTIONS: [&str; HOTBAR_SLOT_COUNT as usize] = [ |
| 8 | "slash.workflow", |
| 9 | "slash.goal", |
| 10 | "slash.auto", |
| 11 | "mode.plan", |
| 12 | "mode.agent", |
| 13 | "reasoning.cycle", |
| 14 | "palette.open", |
| 15 | "sidebar.toggle", |
| 16 | ]; |
| 17 | |
| 18 | /// On-disk schema for one `[[hotbar]]` table. |
| 19 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 20 | #[serde(deny_unknown_fields)] |
| 21 | pub struct HotbarBindingToml { |
| 22 | pub slot: u8, |
| 23 | pub action: String, |
| 24 | #[serde(default)] |
| 25 | pub label: Option<String>, |
| 26 | } |
| 27 | |
| 28 | /// Validated hotbar binding used by future render/dispatch layers. |
| 29 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 30 | pub struct HotbarBinding { |
| 31 | pub slot: u8, |
| 32 | pub action: String, |
| 33 | pub label: Option<String>, |
| 34 | } |
| 35 | |
| 36 | /// Non-fatal hotbar config issue. Invalid slots are skipped; duplicate slots |
| 37 | /// use the last binding; unknown actions are kept for UI feedback. |
| 38 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 39 | pub enum HotbarConfigWarning { |
| 40 | SlotOutOfRange { |
| 41 | slot: u8, |
| 42 | action: String, |
| 43 | }, |
| 44 | DuplicateSlot { |
| 45 | slot: u8, |
| 46 | previous_action: String, |
| 47 | replacement_action: String, |
| 48 | }, |
| 49 | UnknownAction { |
| 50 | slot: u8, |
| 51 | action: String, |
| 52 | }, |
| 53 | } |
| 54 | |
| 55 | impl fmt::Display for HotbarConfigWarning { |
| 56 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 57 | match self { |
| 58 | Self::SlotOutOfRange { slot, action } => write!( |
| 59 | f, |
| 60 | "hotbar slot {slot} for action '{action}' is outside 1-{HOTBAR_SLOT_COUNT}; skipped" |
| 61 | ), |
| 62 | Self::DuplicateSlot { |
| 63 | slot, |
| 64 | previous_action, |
| 65 | replacement_action, |
| 66 | } => write!( |
| 67 | f, |
| 68 | "hotbar slot {slot} was bound to '{previous_action}' more than once; using '{replacement_action}'" |
| 69 | ), |
| 70 | Self::UnknownAction { slot, action } => write!( |
| 71 | f, |
| 72 | "hotbar slot {slot} references unknown action '{action}'; keeping binding" |
| 73 | ), |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 79 | pub struct HotbarConfigResolution { |
| 80 | pub bindings: Vec<HotbarBinding>, |
| 81 | pub warnings: Vec<HotbarConfigWarning>, |
| 82 | } |
| 83 | |
| 84 | #[must_use] |
| 85 | pub fn default_hotbar_bindings() -> Vec<HotbarBinding> { |
| 86 | DEFAULT_HOTBAR_ACTIONS |
| 87 | .iter() |
| 88 | .enumerate() |
| 89 | .map(|(idx, action)| HotbarBinding { |
| 90 | slot: u8::try_from(idx + 1).expect("default hotbar slot fits in u8"), |
| 91 | action: (*action).to_string(), |
| 92 | label: None, |
| 93 | }) |
| 94 | .collect() |
| 95 | } |
| 96 | |
| 97 | /// The default hotbar slots in on-disk (`[[hotbar]]`) form. Since #3807 an |
| 98 | /// absent `hotbar` key means "hidden", so `/hotbar on` persists these explicit |
| 99 | /// bindings rather than deleting the key. Kept in terms of |
| 100 | /// [`default_hotbar_bindings`] so `DEFAULT_HOTBAR_ACTIONS` stays the single |
| 101 | /// source of truth. |
| 102 | #[must_use] |
| 103 | pub fn default_hotbar_bindings_toml() -> Vec<HotbarBindingToml> { |
| 104 | default_hotbar_bindings() |
| 105 | .into_iter() |
| 106 | .map(|binding| HotbarBindingToml { |
| 107 | slot: binding.slot, |
| 108 | action: binding.action, |
| 109 | label: binding.label, |
| 110 | }) |
| 111 | .collect() |
| 112 | } |
| 113 | |
| 114 | #[must_use] |
| 115 | pub fn resolve_hotbar_bindings( |
| 116 | configured: Option<&[HotbarBindingToml]>, |
| 117 | known_action_ids: &[&str], |
| 118 | ) -> HotbarConfigResolution { |
| 119 | let known = known_action_ids.iter().copied().collect::<BTreeSet<&str>>(); |
| 120 | let mut warnings = Vec::new(); |
| 121 | |
| 122 | let source = match configured { |
| 123 | Some(bindings) => bindings |
| 124 | .iter() |
| 125 | .map(|binding| HotbarBinding { |
| 126 | slot: binding.slot, |
| 127 | action: binding.action.clone(), |
| 128 | label: binding.label.clone(), |
| 129 | }) |
| 130 | .collect::<Vec<_>>(), |
| 131 | // #3807: an absent `hotbar` key means the Hotbar is hidden until the |
| 132 | // user opts in (via the setup wizard or `/hotbar on`). Only an explicit |
| 133 | // `[[hotbar]]` config produces bindings. `Some([])` stays "disabled". |
| 134 | None => Vec::new(), |
| 135 | }; |
| 136 | |
| 137 | let mut by_slot: BTreeMap<u8, HotbarBinding> = BTreeMap::new(); |
| 138 | for binding in source { |
| 139 | if !(1..=HOTBAR_SLOT_COUNT).contains(&binding.slot) { |
| 140 | warnings.push(HotbarConfigWarning::SlotOutOfRange { |
| 141 | slot: binding.slot, |
| 142 | action: binding.action, |
| 143 | }); |
| 144 | continue; |
| 145 | } |
| 146 | if !known.is_empty() && !known.contains(binding.action.as_str()) { |
| 147 | warnings.push(HotbarConfigWarning::UnknownAction { |
| 148 | slot: binding.slot, |
| 149 | action: binding.action.clone(), |
| 150 | }); |
| 151 | } |
| 152 | if let Some(previous) = by_slot.insert(binding.slot, binding.clone()) { |
| 153 | warnings.push(HotbarConfigWarning::DuplicateSlot { |
| 154 | slot: binding.slot, |
| 155 | previous_action: previous.action, |
| 156 | replacement_action: binding.action, |
| 157 | }); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | HotbarConfigResolution { |
| 162 | bindings: by_slot.into_values().collect(), |
| 163 | warnings, |
| 164 | } |
| 165 | } |
| 166 |