| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use teams_core::AdaptiveCard; |
| 5 | |
| 6 | use gpui_kit::App; |
| 7 | |
| 8 | use crate::card_inputs::{FieldHandle, InputField}; |
| 9 | |
| 10 | const DIALOG_KEY: &str = "task-dialog"; |
| 11 | pub const DONE_HOLD: Duration = Duration::from_millis(1600); |
| 12 | pub const MAX_REASON_CHARS: usize = 160; |
| 13 | const MAX_REFRESHES_IN_FLIGHT: usize = 3; |
| 14 | |
| 15 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | pub enum ActionPhase { |
| 17 | Busy, |
| 18 | Done, |
| 19 | Failed(String), |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 23 | pub struct BotIdentity { |
| 24 | pub name: String, |
| 25 | pub icon_url: Option<String>, |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Clone, PartialEq)] |
| 29 | pub struct TaskDialogState { |
| 30 | pub title: String, |
| 31 | pub card: AdaptiveCard, |
| 32 | pub scope: CardScope, |
| 33 | pub width: u32, |
| 34 | pub height: u32, |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, PartialEq)] |
| 38 | pub struct CardOverride { |
| 39 | pub basis: String, |
| 40 | pub cards: Vec<AdaptiveCard>, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 44 | pub enum Surface { |
| 45 | Message, |
| 46 | Dialog, |
| 47 | } |
| 48 | |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub struct CardScope { |
| 51 | pub conversation_id: String, |
| 52 | pub message_id: String, |
| 53 | pub card_index: usize, |
| 54 | pub surface: Surface, |
| 55 | } |
| 56 | |
| 57 | impl CardScope { |
| 58 | pub fn message(conversation_id: &str, message_id: &str, card_index: usize) -> Self { |
| 59 | CardScope { |
| 60 | conversation_id: conversation_id.to_owned(), |
| 61 | message_id: message_id.to_owned(), |
| 62 | card_index, |
| 63 | surface: Surface::Message, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub fn card_key(&self) -> String { |
| 68 | match self.surface { |
| 69 | Surface::Message => format!("{}-card-{}", self.message_id, self.card_index), |
| 70 | Surface::Dialog => Self::dialog_key(), |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn input_key(&self, input_id: &str) -> String { |
| 75 | format!("{}/{input_id}", self.card_key()) |
| 76 | } |
| 77 | |
| 78 | pub fn dialog_key() -> String { |
| 79 | DIALOG_KEY.to_owned() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[derive(Default)] |
| 84 | pub struct CardState { |
| 85 | phases: HashMap<String, ActionPhase>, |
| 86 | visibility: HashMap<String, bool>, |
| 87 | open_cards: HashMap<String, usize>, |
| 88 | slots: HashMap<String, usize>, |
| 89 | notes: HashMap<String, String>, |
| 90 | overrides: HashMap<(String, String), CardOverride>, |
| 91 | input_values: HashMap<String, String>, |
| 92 | input_errors: HashMap<String, String>, |
| 93 | input_fields: HashMap<String, FieldHandle>, |
| 94 | refreshed: HashSet<(String, String)>, |
| 95 | refreshes_in_flight: usize, |
| 96 | } |
| 97 | |
| 98 | impl CardState { |
| 99 | pub fn phase(&self, action_key: &str) -> Option<&ActionPhase> { |
| 100 | self.phases.get(action_key) |
| 101 | } |
| 102 | |
| 103 | pub fn is_busy(&self, action_key: &str) -> bool { |
| 104 | self.phases.get(action_key) == Some(&ActionPhase::Busy) |
| 105 | } |
| 106 | |
| 107 | pub fn set_phase(&mut self, action_key: &str, phase: ActionPhase) { |
| 108 | self.phases.insert(action_key.to_owned(), phase); |
| 109 | } |
| 110 | |
| 111 | pub fn clear_done(&mut self, action_key: &str) { |
| 112 | if self.phases.get(action_key) == Some(&ActionPhase::Done) { |
| 113 | self.phases.remove(action_key); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub fn clear_card(&mut self, card_key: &str) { |
| 118 | self.phases.retain(|key, _| !key.starts_with(card_key)); |
| 119 | self.visibility.retain(|key, _| !key.starts_with(card_key)); |
| 120 | self.open_cards.retain(|key, _| !key.starts_with(card_key)); |
| 121 | self.slots.retain(|key, _| !key.starts_with(card_key)); |
| 122 | self.notes.remove(card_key); |
| 123 | self.input_values |
| 124 | .retain(|key, _| !key.starts_with(card_key)); |
| 125 | self.input_errors |
| 126 | .retain(|key, _| !key.starts_with(card_key)); |
| 127 | self.input_fields |
| 128 | .retain(|key, _| !key.starts_with(card_key)); |
| 129 | } |
| 130 | |
| 131 | pub fn has_input(&self, input_key: &str) -> bool { |
| 132 | self.input_values.contains_key(input_key) |
| 133 | } |
| 134 | |
| 135 | pub fn seed_input(&mut self, input_key: String, value: String, field: Option<FieldHandle>) { |
| 136 | if let Some(field) = field { |
| 137 | self.input_fields.insert(input_key.clone(), field); |
| 138 | } |
| 139 | self.input_values.insert(input_key, value); |
| 140 | } |
| 141 | |
| 142 | pub fn input_value(&self, input_key: &str) -> &str { |
| 143 | self.input_values.get(input_key).map_or("", String::as_str) |
| 144 | } |
| 145 | |
| 146 | pub fn input_text(&self, input_key: &str, cx: &App) -> String { |
| 147 | match self.input_fields.get(input_key) { |
| 148 | Some(handle) => handle |
| 149 | .field |
| 150 | .text(cx) |
| 151 | .unwrap_or_else(|| self.input_value(input_key).to_owned()), |
| 152 | None => self.input_value(input_key).to_owned(), |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | pub fn input_field(&self, input_key: &str) -> Option<&InputField> { |
| 157 | self.input_fields.get(input_key).map(|handle| &handle.field) |
| 158 | } |
| 159 | |
| 160 | pub fn set_input_value(&mut self, input_key: &str, value: String) -> bool { |
| 161 | self.input_values.insert(input_key.to_owned(), value); |
| 162 | self.input_errors.remove(input_key).is_some() |
| 163 | } |
| 164 | |
| 165 | pub fn input_error(&self, input_key: &str) -> Option<&str> { |
| 166 | self.input_errors.get(input_key).map(String::as_str) |
| 167 | } |
| 168 | |
| 169 | pub fn set_input_error(&mut self, input_key: &str, message: String) { |
| 170 | self.input_errors.insert(input_key.to_owned(), message); |
| 171 | } |
| 172 | |
| 173 | pub fn clear_input_errors(&mut self, input_keys: &[String]) { |
| 174 | for input_key in input_keys { |
| 175 | self.input_errors.remove(input_key); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | pub fn is_visible(&self, element_key: &str, initial: bool) -> bool { |
| 180 | self.visibility.get(element_key).copied().unwrap_or(initial) |
| 181 | } |
| 182 | |
| 183 | pub fn toggle_visibility(&mut self, element_key: &str, initial: bool, forced: Option<bool>) { |
| 184 | let next = forced.unwrap_or(!self.is_visible(element_key, initial)); |
| 185 | self.visibility.insert(element_key.to_owned(), next); |
| 186 | } |
| 187 | |
| 188 | pub fn open_card(&self, actions_key: &str) -> Option<usize> { |
| 189 | self.open_cards.get(actions_key).copied() |
| 190 | } |
| 191 | |
| 192 | pub fn toggle_open_card(&mut self, actions_key: &str, index: usize) { |
| 193 | if self.open_cards.get(actions_key) == Some(&index) { |
| 194 | self.open_cards.remove(actions_key); |
| 195 | } else { |
| 196 | self.open_cards.insert(actions_key.to_owned(), index); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | pub fn slot(&self, slot_key: &str, default: usize) -> usize { |
| 201 | self.slots.get(slot_key).copied().unwrap_or(default) |
| 202 | } |
| 203 | |
| 204 | pub fn set_slot(&mut self, slot_key: &str, value: usize) { |
| 205 | self.slots.insert(slot_key.to_owned(), value); |
| 206 | } |
| 207 | |
| 208 | pub fn note(&self, card_key: &str) -> Option<&str> { |
| 209 | self.notes.get(card_key).map(String::as_str) |
| 210 | } |
| 211 | |
| 212 | pub fn set_note(&mut self, card_key: &str, text: String) { |
| 213 | self.notes.insert(card_key.to_owned(), text); |
| 214 | } |
| 215 | |
| 216 | pub fn clear_note(&mut self, card_key: &str) { |
| 217 | self.notes.remove(card_key); |
| 218 | } |
| 219 | |
| 220 | pub fn set_override( |
| 221 | &mut self, |
| 222 | conversation_id: &str, |
| 223 | message_id: &str, |
| 224 | replaced: CardOverride, |
| 225 | ) { |
| 226 | self.overrides.insert( |
| 227 | (conversation_id.to_owned(), message_id.to_owned()), |
| 228 | replaced, |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | pub fn begin_refresh(&mut self, conversation_id: &str, message_id: &str, manual: bool) -> bool { |
| 233 | let key = (conversation_id.to_owned(), message_id.to_owned()); |
| 234 | if !manual |
| 235 | && (self.refreshed.contains(&key) |
| 236 | || self.refreshes_in_flight >= MAX_REFRESHES_IN_FLIGHT) |
| 237 | { |
| 238 | return false; |
| 239 | } |
| 240 | self.refreshed.insert(key); |
| 241 | self.refreshes_in_flight += 1; |
| 242 | true |
| 243 | } |
| 244 | |
| 245 | pub fn cancel_refresh(&mut self, conversation_id: &str, message_id: &str) { |
| 246 | self.refreshed |
| 247 | .remove(&(conversation_id.to_owned(), message_id.to_owned())); |
| 248 | self.refreshes_in_flight = self.refreshes_in_flight.saturating_sub(1); |
| 249 | } |
| 250 | |
| 251 | /// True when the throttle was full, so a deferred refresh may start now. |
| 252 | pub fn finish_refresh(&mut self) -> bool { |
| 253 | let was_full = self.refreshes_in_flight >= MAX_REFRESHES_IN_FLIGHT; |
| 254 | self.refreshes_in_flight = self.refreshes_in_flight.saturating_sub(1); |
| 255 | was_full |
| 256 | } |
| 257 | |
| 258 | pub fn overrides_for(&self, conversation_id: &str) -> HashMap<String, CardOverride> { |
| 259 | self.overrides |
| 260 | .iter() |
| 261 | .filter(|((conversation, _), _)| conversation == conversation_id) |
| 262 | .map(|((_, message_id), replaced)| (message_id.clone(), replaced.clone())) |
| 263 | .collect() |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | pub fn short_reason(reason: &str) -> String { |
| 268 | reason.chars().take(MAX_REASON_CHARS).collect() |
| 269 | } |
| 270 | |
| 271 | #[cfg(test)] |
| 272 | mod tests { |
| 273 | use super::*; |
| 274 | |
| 275 | #[test] |
| 276 | fn done_clears_but_busy_and_failed_stay() { |
| 277 | let mut state = CardState::default(); |
| 278 | state.set_phase("a", ActionPhase::Done); |
| 279 | state.set_phase("b", ActionPhase::Busy); |
| 280 | state.set_phase("c", ActionPhase::Failed("nope".into())); |
| 281 | for key in ["a", "b", "c"] { |
| 282 | state.clear_done(key); |
| 283 | } |
| 284 | assert_eq!(state.phase("a"), None); |
| 285 | assert!(state.is_busy("b")); |
| 286 | assert_eq!(state.phase("c"), Some(&ActionPhase::Failed("nope".into()))); |
| 287 | } |
| 288 | |
| 289 | #[test] |
| 290 | fn visibility_toggles_from_the_initial_value_or_is_forced() { |
| 291 | let mut state = CardState::default(); |
| 292 | assert!(!state.is_visible("e", false)); |
| 293 | state.toggle_visibility("e", false, None); |
| 294 | assert!(state.is_visible("e", false)); |
| 295 | state.toggle_visibility("e", false, None); |
| 296 | assert!(!state.is_visible("e", false)); |
| 297 | state.toggle_visibility("e", false, Some(true)); |
| 298 | state.toggle_visibility("e", false, Some(true)); |
| 299 | assert!(state.is_visible("e", false)); |
| 300 | } |
| 301 | |
| 302 | #[test] |
| 303 | fn one_show_card_is_open_per_action_set() { |
| 304 | let mut state = CardState::default(); |
| 305 | state.toggle_open_card("set", 1); |
| 306 | assert_eq!(state.open_card("set"), Some(1)); |
| 307 | state.toggle_open_card("set", 2); |
| 308 | assert_eq!(state.open_card("set"), Some(2)); |
| 309 | state.toggle_open_card("set", 2); |
| 310 | assert_eq!(state.open_card("set"), None); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn slots_default_until_set_and_clear_with_their_card() { |
| 315 | let mut state = CardState::default(); |
| 316 | assert_eq!(state.slot("m-card-0-3", 2), 2); |
| 317 | state.set_slot("m-card-0-3", 5); |
| 318 | assert_eq!(state.slot("m-card-0-3", 2), 5); |
| 319 | state.clear_card("m-card-0"); |
| 320 | assert_eq!(state.slot("m-card-0-3", 2), 2); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn overrides_are_scoped_to_their_conversation() { |
| 325 | let mut state = CardState::default(); |
| 326 | let replaced = CardOverride { |
| 327 | basis: "[]".into(), |
| 328 | cards: Vec::new(), |
| 329 | }; |
| 330 | state.set_override("c1", "m1", replaced.clone()); |
| 331 | assert_eq!(state.overrides_for("c1").get("m1"), Some(&replaced)); |
| 332 | assert!(state.overrides_for("c2").is_empty()); |
| 333 | } |
| 334 | |
| 335 | #[test] |
| 336 | fn a_message_refreshes_once_unless_asked_manually() { |
| 337 | let mut state = CardState::default(); |
| 338 | assert!(state.begin_refresh("c", "m", false)); |
| 339 | assert!(!state.begin_refresh("c", "m", false)); |
| 340 | assert!(state.begin_refresh("c", "m", true)); |
| 341 | assert!(state.begin_refresh("c", "other", false)); |
| 342 | } |
| 343 | |
| 344 | #[test] |
| 345 | fn at_most_three_automatic_refreshes_run_at_once() { |
| 346 | let mut state = CardState::default(); |
| 347 | for message_id in ["a", "b", "c"] { |
| 348 | assert!(state.begin_refresh("c", message_id, false)); |
| 349 | } |
| 350 | assert!(!state.begin_refresh("c", "d", false)); |
| 351 | assert!(state.begin_refresh("c", "d", true)); |
| 352 | assert!(state.finish_refresh()); |
| 353 | assert!(state.finish_refresh()); |
| 354 | assert!(!state.finish_refresh()); |
| 355 | assert!(state.begin_refresh("c", "e", false)); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn a_cancelled_refresh_may_start_again() { |
| 360 | let mut state = CardState::default(); |
| 361 | assert!(state.begin_refresh("c", "m", false)); |
| 362 | state.cancel_refresh("c", "m"); |
| 363 | assert!(state.begin_refresh("c", "m", false)); |
| 364 | } |
| 365 | |
| 366 | #[test] |
| 367 | fn reasons_are_shortened() { |
| 368 | assert_eq!( |
| 369 | short_reason(&"x".repeat(500)).chars().count(), |
| 370 | MAX_REASON_CHARS |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | #[test] |
| 375 | fn card_keys_differ_by_surface_and_index() { |
| 376 | let message = CardScope::message("c", "m", 2); |
| 377 | assert_eq!(message.card_key(), "m-card-2"); |
| 378 | let dialog = CardScope { |
| 379 | surface: Surface::Dialog, |
| 380 | ..message |
| 381 | }; |
| 382 | assert_eq!(dialog.card_key(), "task-dialog"); |
| 383 | } |
| 384 | } |