agcodex-tui 0.1.0

Terminal User Interface for AGCodex with mode switching support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Terminal bell notification system for the TUI
//!
//! Provides comprehensive notification system with terminal bells, visual feedback,
//! and accessibility options for task completion, errors, warnings, and user interactions.

use agcodex_core::config_types::TuiNotifications;
use std::collections::HashSet;
use std::io::Write;
use std::io::{self};
use std::thread;
use std::time::Duration;

/// Different notification levels with distinct bell patterns
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NotificationLevel {
    /// Task successfully completed (single bell)
    TaskComplete,
    /// Error occurred (double bell with delay)
    Error,
    /// Warning or non-critical issue (single bell)
    Warning,
    /// Information or status update (no bell by default)
    Info,
}

/// Comprehensive notification system for TUI with multiple feedback modes
pub struct NotificationSystem {
    /// Base configuration from TUI settings
    config: TuiNotifications,
    /// Which notification levels are enabled
    enabled_levels: HashSet<NotificationLevel>,
    /// Whether sound notifications (terminal bell) are enabled
    sound_enabled: bool,
    /// Whether visual flash notifications are enabled
    visual_enabled: bool,
}

/// Legacy alias for backward compatibility
pub type NotificationManager = NotificationSystem;

impl NotificationSystem {
    /// Create a new notification system with the given configuration
    pub fn new(config: TuiNotifications) -> Self {
        let mut enabled_levels = HashSet::new();

        // Configure enabled levels based on config
        if config.agent_complete {
            enabled_levels.insert(NotificationLevel::TaskComplete);
        }
        if config.agent_failed || config.error_occurred {
            enabled_levels.insert(NotificationLevel::Error);
        }
        if config.user_input_needed || config.warnings {
            enabled_levels.insert(NotificationLevel::Warning);
        }
        if config.info_messages {
            enabled_levels.insert(NotificationLevel::Info);
        }

        let sound_enabled = config.terminal_bell;
        let visual_enabled = config.visual_flash;
        Self {
            config,
            enabled_levels,
            sound_enabled,
            visual_enabled,
        }
    }

    /// Update the notification configuration
    pub fn update_config(&mut self, config: TuiNotifications) {
        // Rebuild enabled levels
        self.enabled_levels.clear();
        if config.agent_complete {
            self.enabled_levels.insert(NotificationLevel::TaskComplete);
        }
        if config.agent_failed || config.error_occurred {
            self.enabled_levels.insert(NotificationLevel::Error);
        }
        if config.user_input_needed || config.warnings {
            self.enabled_levels.insert(NotificationLevel::Warning);
        }
        if config.info_messages {
            self.enabled_levels.insert(NotificationLevel::Info);
        }

        self.sound_enabled = config.terminal_bell;
        self.visual_enabled = config.visual_flash;
        self.config = config;
    }

    /// Enable or disable specific notification levels
    pub fn set_level_enabled(&mut self, level: NotificationLevel, enabled: bool) {
        if enabled {
            self.enabled_levels.insert(level);
        } else {
            self.enabled_levels.remove(&level);
        }
    }

    /// Check if a notification level is enabled
    pub fn is_level_enabled(&self, level: NotificationLevel) -> bool {
        self.enabled_levels.contains(&level)
    }

    /// Enable or disable sound notifications
    pub const fn set_sound_enabled(&mut self, enabled: bool) {
        self.sound_enabled = enabled;
    }

    /// Enable or disable visual notifications  
    pub const fn set_visual_enabled(&mut self, enabled: bool) {
        self.visual_enabled = enabled;
    }

    /// Ring the terminal bell with pattern based on notification level
    fn ring_bell(&self, level: NotificationLevel) -> io::Result<()> {
        if !self.sound_enabled || !self.enabled_levels.contains(&level) {
            return Ok(());
        }

        match level {
            NotificationLevel::TaskComplete => {
                // Single bell for successful task completion
                print!("\x07");
                io::stdout().flush()?;
            }
            NotificationLevel::Error => {
                // Double bell with delay for errors
                print!("\x07");
                io::stdout().flush()?;
                thread::sleep(Duration::from_millis(150));
                print!("\x07");
                io::stdout().flush()?;
            }
            NotificationLevel::Warning => {
                // Single bell for warnings
                print!("\x07");
                io::stdout().flush()?;
            }
            NotificationLevel::Info => {
                // No bell for info by default (visual only)
                // This can be overridden if needed
            }
        }

        Ok(())
    }

    /// Provide visual flash feedback for accessibility
    fn visual_flash(&self, _level: NotificationLevel) -> io::Result<()> {
        if !self.visual_enabled {
            return Ok(());
        }

        // Visual bell using reverse video flash
        // This is handled by the terminal - send the visual bell sequence
        print!("\x1B[?5h"); // Turn on reverse video
        io::stdout().flush()?;
        thread::sleep(Duration::from_millis(50));
        print!("\x1B[?5l"); // Turn off reverse video  
        io::stdout().flush()?;

        Ok(())
    }

    /// Main notification method that handles both sound and visual feedback
    pub fn notify(&self, level: NotificationLevel) -> io::Result<()> {
        if !self.enabled_levels.contains(&level) {
            return Ok(());
        }

        // Ring bell if sound is enabled
        if self.sound_enabled {
            self.ring_bell(level)?;
        }

        // Visual flash for accessibility if sound is disabled or as supplement
        if self.visual_enabled && (!self.sound_enabled || level == NotificationLevel::Error) {
            self.visual_flash(level)?;
        }

        Ok(())
    }

    /// Convenience method to notify with a message for debugging
    pub fn notify_with_message(&self, level: NotificationLevel, message: &str) -> io::Result<()> {
        // Log the notification for debugging (in debug builds)
        #[cfg(debug_assertions)]
        eprintln!("[NOTIFICATION {:?}] {}", level, message);

        self.notify(level)
    }

    // ===== High-level notification methods =====

    /// Notify when an agent completes a task successfully
    pub fn agent_completed(&self) -> io::Result<()> {
        if self.config.agent_complete {
            self.notify(NotificationLevel::TaskComplete)?;
        }
        Ok(())
    }

    /// Notify when an agent completes a task successfully with message
    pub fn agent_completed_with_message(&self, agent_name: &str) -> io::Result<()> {
        if self.config.agent_complete {
            self.notify_with_message(
                NotificationLevel::TaskComplete,
                &format!("Agent '{}' completed successfully", agent_name),
            )?;
        }
        Ok(())
    }

    /// Notify when an agent fails
    pub fn agent_failed(&self) -> io::Result<()> {
        if self.config.agent_failed {
            self.notify(NotificationLevel::Error)?;
        }
        Ok(())
    }

    /// Notify when an agent fails with error message
    pub fn agent_failed_with_message(&self, agent_name: &str, error: &str) -> io::Result<()> {
        if self.config.agent_failed {
            self.notify_with_message(
                NotificationLevel::Error,
                &format!("Agent '{}' failed: {}", agent_name, error),
            )?;
        }
        Ok(())
    }

    /// Notify when a general error occurs
    pub fn error_occurred(&self) -> io::Result<()> {
        if self.config.error_occurred {
            self.notify(NotificationLevel::Error)?;
        }
        Ok(())
    }

    /// Notify when a general error occurs with message
    pub fn error_occurred_with_message(&self, error: &str) -> io::Result<()> {
        if self.config.error_occurred {
            self.notify_with_message(NotificationLevel::Error, error)?;
        }
        Ok(())
    }

    /// Notify when user input is needed (e.g., approval requests)
    pub fn user_input_needed(&self) -> io::Result<()> {
        if self.config.user_input_needed {
            self.notify(NotificationLevel::Warning)?;
        }
        Ok(())
    }

    /// Notify when user input is needed with message
    pub fn user_input_needed_with_message(&self, message: &str) -> io::Result<()> {
        if self.config.user_input_needed {
            self.notify_with_message(NotificationLevel::Warning, message)?;
        }
        Ok(())
    }

    /// Notify with an info message (typically visual only)
    pub fn info(&self, message: &str) -> io::Result<()> {
        self.notify_with_message(NotificationLevel::Info, message)
    }

    /// Notify with a warning (single bell)
    pub fn warning(&self, message: &str) -> io::Result<()> {
        self.notify_with_message(NotificationLevel::Warning, message)
    }

    /// Get current configuration
    pub const fn config(&self) -> &TuiNotifications {
        &self.config
    }

    /// Check if any notifications are enabled
    pub fn has_enabled_notifications(&self) -> bool {
        !self.enabled_levels.is_empty() && (self.sound_enabled || self.visual_enabled)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use agcodex_core::config_types::TuiNotifications;

    #[test]
    fn test_notification_system_creation() {
        let config = TuiNotifications::default();
        let system = NotificationSystem::new(config);

        // All notifications should be enabled by default
        assert!(system.config.terminal_bell);
        assert!(system.config.agent_complete);
        assert!(system.config.agent_failed);
        assert!(system.config.error_occurred);
        assert!(system.config.user_input_needed);
        assert!(system.sound_enabled);
        assert!(system.visual_enabled);
    }

    #[test]
    fn test_disabled_notifications() {
        let config = TuiNotifications {
            terminal_bell: false,
            visual_flash: false,
            agent_complete: false,
            agent_failed: false,
            error_occurred: false,
            user_input_needed: false,
            warnings: false,
            info_messages: false,
        };
        let system = NotificationSystem::new(config);

        assert!(!system.sound_enabled);
        assert!(!system.is_level_enabled(NotificationLevel::TaskComplete));
        assert!(!system.is_level_enabled(NotificationLevel::Error));
        assert!(!system.is_level_enabled(NotificationLevel::Warning));

        // These should not ring the bell (though we can't easily test the actual bell)
        assert!(system.agent_completed().is_ok());
        assert!(system.agent_failed().is_ok());
        assert!(system.error_occurred().is_ok());
        assert!(system.user_input_needed().is_ok());
    }

    #[test]
    fn test_config_update() {
        let initial_config = TuiNotifications::default();
        let mut system = NotificationSystem::new(initial_config);

        let new_config = TuiNotifications {
            terminal_bell: false,
            visual_flash: false,
            agent_complete: false,
            agent_failed: true,
            error_occurred: true,
            user_input_needed: false,
            warnings: false,
            info_messages: false,
        };

        system.update_config(new_config.clone());
        assert_eq!(system.config, new_config);
        assert!(!system.sound_enabled);
        assert!(system.is_level_enabled(NotificationLevel::Error));
        assert!(!system.is_level_enabled(NotificationLevel::TaskComplete));
        assert!(!system.is_level_enabled(NotificationLevel::Warning));
    }

    #[test]
    fn test_level_management() {
        let config = TuiNotifications::default();
        let mut system = NotificationSystem::new(config);

        // Test enabling/disabling levels
        system.set_level_enabled(NotificationLevel::TaskComplete, false);
        assert!(!system.is_level_enabled(NotificationLevel::TaskComplete));

        system.set_level_enabled(NotificationLevel::TaskComplete, true);
        assert!(system.is_level_enabled(NotificationLevel::TaskComplete));
    }

    #[test]
    fn test_sound_and_visual_controls() {
        let config = TuiNotifications::default();
        let mut system = NotificationSystem::new(config);

        system.set_sound_enabled(false);
        assert!(!system.sound_enabled);

        system.set_visual_enabled(false);
        assert!(!system.visual_enabled);

        // Even with everything disabled, has_enabled_notifications should be false
        assert!(!system.has_enabled_notifications());
    }

    #[test]
    fn test_notification_levels() {
        // Test that our enum variants work as expected
        let levels = vec![
            NotificationLevel::TaskComplete,
            NotificationLevel::Error,
            NotificationLevel::Warning,
            NotificationLevel::Info,
        ];

        for level in levels {
            // Test that levels can be created and used
            let mut set = HashSet::new();
            set.insert(level);
            assert!(set.contains(&level));
        }
    }

    // Note: We can't easily test actual bell output without mocking stdout
    // These tests verify the control logic works correctly
    #[test]
    fn test_notification_methods_dont_panic() {
        let config = TuiNotifications::default();
        let system = NotificationSystem::new(config);

        // These should all complete without error
        assert!(system.notify(NotificationLevel::TaskComplete).is_ok());
        assert!(system.notify(NotificationLevel::Error).is_ok());
        assert!(system.notify(NotificationLevel::Warning).is_ok());
        assert!(system.notify(NotificationLevel::Info).is_ok());

        assert!(system.agent_completed_with_message("test-agent").is_ok());
        assert!(
            system
                .agent_failed_with_message("test-agent", "test error")
                .is_ok()
        );
        assert!(system.info("test info").is_ok());
        assert!(system.warning("test warning").is_ok());
    }
}