selfware 0.6.1

Your personal AI workshop — software you own, software that lasts
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Selfware TUI Application
//!
//! State machine for the terminal UI with multi-pane layouts.

// Feature-gated module - dead_code lint disabled at crate level

use super::{wrap_chat_message, CommandPalette, StatusLine, TuiPalette};
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::Span,
    widgets::{Block, Borders, List, ListItem, Paragraph},
    Frame,
};

/// Application state
#[derive(Debug, Clone, PartialEq)]
pub enum AppState {
    /// Normal chat mode
    Chatting,
    /// Running a task with progress
    RunningTask,
    /// Command palette is open
    Palette,
    /// Browsing files
    FileBrowser,
    /// Viewing help
    Help,
    /// Confirming an action
    Confirming(String),
    /// Viewing digital garden
    GardenView,
}

/// A chat message for display
#[derive(Debug, Clone)]
pub struct ChatMessage {
    pub role: MessageRole,
    pub content: String,
    pub timestamp: String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MessageRole {
    User,
    Assistant,
    System,
    Tool,
}

/// Task progress for display
#[derive(Debug, Clone)]
pub struct TaskProgress {
    pub description: String,
    pub current_step: usize,
    pub total_steps: Option<usize>,
    pub current_action: String,
    pub elapsed_secs: u64,
}

/// Animation speed settings
pub const ANIMATION_SPEED_MIN: f64 = 0.25;
pub const ANIMATION_SPEED_MAX: f64 = 4.0;
pub const ANIMATION_SPEED_STEP: f64 = 0.25;
pub const ANIMATION_SPEED_DEFAULT: f64 = 1.0;

/// The main TUI application
pub struct App {
    /// Current state
    pub state: AppState,
    /// Chat messages
    pub messages: Vec<ChatMessage>,
    /// Current input buffer
    pub input: String,
    /// Cursor position in input
    pub cursor: usize,
    /// Command palette
    pub palette: CommandPalette,
    /// Task progress (if running)
    pub task_progress: Option<TaskProgress>,
    /// Status bar message
    pub status: String,
    /// Model name
    pub model: String,
    /// Whether we're connected
    pub connected: bool,
    /// Scroll offset for messages
    pub scroll: usize,
    /// Selected item in lists
    pub selected: usize,
    /// Animation speed multiplier (1.0 = normal, 2.0 = faster, 0.5 = slower)
    pub animation_speed: f64,
    /// Verbose output mode (toggled by /verbose and /compact)
    pub verbose: bool,
    /// Garden view for codebase visualization
    pub garden_view: super::GardenView,
    /// Intelligent status line
    pub status_line: StatusLine,
    /// Discovered skills for slash-command execution
    pub skill_registry: Option<crate::skills::SkillRegistry>,
    /// In-progress assistant response streamed token-by-token. Rendered as the
    /// newest (live) chat line while generating; committed to `messages` and
    /// cleared on completion.
    pub streaming_assistant: Option<String>,
}

impl App {
    /// Create a new app instance
    pub fn new(model: &str) -> Self {
        Self {
            state: AppState::Chatting,
            messages: vec![ChatMessage {
                role: MessageRole::System,
                content: "Welcome to your workshop. How can I help you tend your garden today?"
                    .into(),
                timestamp: chrono::Local::now().format("%H:%M").to_string(),
            }],
            input: String::new(),
            cursor: 0,
            palette: CommandPalette::new(),
            task_progress: None,
            status: "Ready".into(),
            model: model.into(),
            // Not connected until the model actually responds (see
            // append_streaming / add_assistant_message).
            connected: false,
            scroll: 0,
            selected: 0,
            animation_speed: ANIMATION_SPEED_DEFAULT,
            verbose: false,
            garden_view: super::GardenView::new(),
            status_line: StatusLine::with_session(model),
            skill_registry: None,
            streaming_assistant: None,
        }
    }

    /// Add a user message
    pub fn add_user_message(&mut self, content: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::User,
            content: content.into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Append a streamed chunk to the in-progress assistant response.
    pub fn append_streaming(&mut self, text: &str) {
        // Streamed bytes from the model prove the connection is live.
        self.connected = true;
        self.streaming_assistant
            .get_or_insert_with(String::new)
            .push_str(text);
    }

    /// Clear the in-progress streaming buffer (called once the full message is
    /// committed on completion).
    pub fn clear_streaming(&mut self) {
        self.streaming_assistant = None;
    }

    /// Finalize the in-progress streamed turn: commit it as a static assistant
    /// message and clear the live buffer. Called at each turn boundary (tool
    /// start, new step) so a multi-turn run renders as separate messages
    /// instead of one ever-growing run-on block. No-op when the buffer is
    /// empty or whitespace-only.
    pub fn commit_streaming(&mut self) {
        if let Some(text) = self.streaming_assistant.take() {
            let trimmed = text.trim();
            if !trimmed.is_empty() {
                self.add_assistant_message(trimmed);
            }
        }
    }

    /// Add an assistant message
    pub fn add_assistant_message(&mut self, content: &str) {
        // Receiving an assistant message means we reached the model.
        self.connected = true;
        self.messages.push(ChatMessage {
            role: MessageRole::Assistant,
            content: content.into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Add a system message
    pub fn add_system_message(&mut self, content: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::System,
            content: content.into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Add a tool output message
    pub fn add_tool_message(&mut self, tool_name: &str, output: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::Tool,
            content: format!("[{}] {}", tool_name, output),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Clear chat history (keeping a fresh system message).
    ///
    /// Also clears any in-progress streaming buffer so that a partial
    /// assistant response does not persist after the history is wiped.
    pub fn clear_chat(&mut self) {
        self.messages.clear();
        self.clear_streaming();
        // /clear resets the underlying model conversation (see the /clear
        // handler), so a "partial" clear that wiped only the transcript left
        // stale task progress and token/context counters on screen. Reset those
        // too so the cleared view truthfully reflects a fresh conversation.
        self.task_progress = None;
        self.state = AppState::Chatting;
        self.status_line.reset_usage();
        self.messages.push(ChatMessage {
            role: MessageRole::System,
            content: "Chat cleared.".into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
        self.scroll = 0;
    }

    /// Set task progress
    pub fn set_progress(&mut self, progress: TaskProgress) {
        self.task_progress = Some(progress);
        self.state = AppState::RunningTask;
    }

    /// Update just the step counters of the progress gauge from a live
    /// "Step X/Y" signal, so the gauge advances during a run instead of being
    /// frozen at its initial value. Creates the progress entry if a run is
    /// active but none was set yet.
    pub fn update_step_progress(&mut self, current_step: usize, total_steps: usize) {
        match self.task_progress.as_mut() {
            Some(p) => {
                p.current_step = current_step;
                p.total_steps = Some(total_steps);
            }
            None => {
                self.set_progress(TaskProgress {
                    description: "Processing...".into(),
                    current_step,
                    total_steps: Some(total_steps),
                    current_action: "Working".into(),
                    elapsed_secs: 0,
                });
            }
        }
    }

    /// Clear task progress
    pub fn clear_progress(&mut self) {
        self.task_progress = None;
        self.state = AppState::Chatting;
    }

    /// Toggle command palette
    pub fn toggle_palette(&mut self) {
        self.state = if self.state == AppState::Palette {
            AppState::Chatting
        } else {
            AppState::Palette
        };
    }

    /// Render the application
    pub fn render(&mut self, frame: &mut Frame) {
        let area = frame.area();

        // Handle garden view as full-screen overlay
        if self.state == AppState::GardenView {
            self.render_header(
                frame,
                Layout::default()
                    .direction(Direction::Vertical)
                    .constraints([Constraint::Length(3)])
                    .split(area)[0],
            );

            let main_area = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(3), // Header (already rendered)
                    Constraint::Min(10),   // Garden view
                    Constraint::Length(1), // Status bar
                ])
                .split(area)[1];

            self.garden_view.render(frame, main_area);

            let status_area = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(3),
                    Constraint::Min(10),
                    Constraint::Length(1),
                ])
                .split(area)[2];
            self.render_status_bar(frame, status_area);
            return;
        }

        // Create main layout
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Header
                Constraint::Min(10),   // Main content
                Constraint::Length(3), // Input area
                Constraint::Length(1), // Status bar
            ])
            .split(area);

        self.render_header(frame, chunks[0]);
        self.render_messages(frame, chunks[1]);
        self.render_input(frame, chunks[2]);
        self.render_status_bar(frame, chunks[3]);

        // Render overlay if palette is open
        if self.state == AppState::Palette {
            self.render_palette(frame, area);
        }
    }

    /// Build the header title, appending live step progress while a task runs.
    fn header_title(&self) -> String {
        match (&self.state, self.task_progress.as_ref()) {
            (AppState::RunningTask, Some(p)) => {
                let steps = match p.total_steps {
                    Some(total) => format!("{}/{}", p.current_step, total),
                    None => p.current_step.to_string(),
                };
                format!(
                    " 🦊 Selfware Workshop — {} · step {} · {} ",
                    self.model, steps, p.current_action
                )
            }
            _ => format!(" 🦊 Selfware Workshop — {} ", self.model),
        }
    }

    /// Render the header
    fn render_header(&self, frame: &mut Frame, area: Rect) {
        let title = self.header_title();
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(TuiPalette::border_style())
            .title(Span::styled(title, TuiPalette::title_style()));

        frame.render_widget(block, area);
    }

    /// Render chat messages
    fn render_messages(&self, frame: &mut Frame, area: Rect) {
        // Messages pane is "focused" when we're not in palette or other overlay modes
        let is_focused = self.state == AppState::Chatting || self.state == AppState::RunningTask;
        let border_style = if is_focused {
            TuiPalette::border_style()
        } else {
            TuiPalette::muted_style()
        };

        let title = if is_focused {
            " Messages ".into()
        } else {
            format!(" Messages [{}] ", self.state_label())
        };

        let inner = Block::default()
            .borders(Borders::ALL)
            .border_style(border_style)
            .title(Span::styled(title, border_style));

        let inner_area = inner.inner(area);
        frame.render_widget(inner, area);

        // Build message list with line wrapping
        let msg_width = inner_area.width as usize;
        let items: Vec<ListItem> = self
            .messages
            .iter()
            .rev()
            .skip(self.scroll)
            .map(|msg| {
                let style = match msg.role {
                    MessageRole::User => Style::default().fg(TuiPalette::AMBER),
                    MessageRole::Assistant => Style::default().fg(TuiPalette::GARDEN_GREEN),
                    MessageRole::System => TuiPalette::muted_style(),
                    MessageRole::Tool => Style::default().fg(TuiPalette::COPPER),
                };

                let prefix = match msg.role {
                    MessageRole::User => "You",
                    MessageRole::Assistant => "🦊",
                    MessageRole::System => "📋",
                    MessageRole::Tool => "🔧",
                };

                let prefix_str = format!("{} {} ", msg.timestamp, prefix);
                wrap_chat_message(&prefix_str, &msg.content, style, msg_width)
            })
            .collect();

        let messages = List::new(items);
        frame.render_widget(messages, inner_area);
    }

    /// Render input area
    fn render_input(&self, frame: &mut Frame, area: Rect) {
        let is_focused = self.state == AppState::Chatting;
        let border_style = if is_focused {
            Style::default()
                .fg(TuiPalette::AMBER)
                .add_modifier(Modifier::BOLD)
        } else {
            TuiPalette::muted_style()
        };

        let title = if is_focused {
            " Input ".into()
        } else {
            format!(" Input [{}] ", self.state_label())
        };

        let input_block = Block::default()
            .borders(Borders::ALL)
            .border_style(border_style)
            .title(Span::styled(title, border_style));

        let inner = input_block.inner(area);
        frame.render_widget(input_block, area);

        let input_text = if is_focused {
            Paragraph::new(format!("{}", self.input))
                .style(Style::default().fg(TuiPalette::PARCHMENT))
        } else {
            Paragraph::new("".to_string()).style(TuiPalette::muted_style())
        };
        frame.render_widget(input_text, inner);

        // Show cursor only when focused
        if is_focused {
            frame.set_cursor_position((inner.x + 2 + self.cursor as u16, inner.y));
        }
    }

    /// Get a short label for the current state
    fn state_label(&self) -> &'static str {
        match self.state {
            AppState::Chatting => "chat",
            AppState::RunningTask => "task",
            AppState::Palette => "palette",
            AppState::FileBrowser => "files",
            AppState::Help => "help",
            AppState::Confirming(_) => "confirm",
            AppState::GardenView => "garden",
        }
    }

    /// Render status bar
    fn render_status_bar(&self, frame: &mut Frame, area: Rect) {
        self.status_line.render(frame, area);
    }

    /// Update the status line with current app state
    pub fn refresh_status_line(&mut self) {
        self.status_line.connected = self.connected;
        self.status_line.status_message = Some(self.status.clone());
    }

    /// Render command palette overlay
    fn render_palette(&self, frame: &mut Frame, area: Rect) {
        // Center the palette
        let palette_width = 60.min(area.width - 4);
        let palette_height = 15.min(area.height - 4);
        let x = (area.width - palette_width) / 2;
        let y = (area.height - palette_height) / 3;

        let palette_area = Rect::new(x, y, palette_width, palette_height);

        // Clear background
        let clear = Block::default().style(Style::default().bg(TuiPalette::INK));
        frame.render_widget(clear, palette_area);

        // Render palette
        self.palette.render(frame, palette_area, self.selected);
    }

    /// Byte offset in `self.input` for the current char-index cursor.
    fn cursor_byte_offset(&self) -> usize {
        self.input
            .char_indices()
            .nth(self.cursor)
            .map(|(i, _)| i)
            .unwrap_or(self.input.len())
    }

    /// Handle character input
    pub fn on_char(&mut self, c: char) {
        if self.state == AppState::Chatting {
            let byte_idx = self.cursor_byte_offset();
            self.input.insert(byte_idx, c);
            self.cursor += 1;
        } else if self.state == AppState::Palette {
            self.palette.on_char(c);
        }
    }

    /// Handle backspace
    pub fn on_backspace(&mut self) {
        if self.state == AppState::Chatting && self.cursor > 0 {
            self.cursor -= 1;
            let byte_idx = self.cursor_byte_offset();
            self.input.remove(byte_idx);
        } else if self.state == AppState::Palette {
            self.palette.on_backspace();
        }
    }

    /// Handle left arrow
    pub fn on_left(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// Handle right arrow
    pub fn on_right(&mut self) {
        // Bound by CHARACTER count (cursor is a char index), not byte length.
        if self.cursor < self.input.chars().count() {
            self.cursor += 1;
        }
    }

    /// Handle up arrow
    pub fn on_up(&mut self) {
        if self.state == AppState::Palette {
            self.palette.previous();
        } else if self.scroll + 1 < self.messages.len() {
            self.scroll += 1;
        }
    }

    /// Handle down arrow
    pub fn on_down(&mut self) {
        if self.state == AppState::Palette {
            self.palette.next();
        } else if self.scroll > 0 {
            self.scroll -= 1;
        }
    }

    /// Handle enter key
    pub fn on_enter(&mut self) -> Option<String> {
        if self.state == AppState::Palette {
            if let Some(cmd) = self.palette.selected_command() {
                self.state = AppState::Chatting;
                return Some(cmd);
            }
            None
        } else if !self.input.is_empty() {
            let input = std::mem::take(&mut self.input);
            self.cursor = 0;
            Some(input)
        } else {
            None
        }
    }

    /// Handle escape key
    ///
    /// Esc behavior hierarchy:
    /// 1. If palette is open -> close palette and reset it
    /// 2. If confirming -> cancel confirmation
    /// 3. If running task -> cancel/close task view
    /// 4. If file browser -> close file browser
    /// 5. If help -> close help
    /// 6. If chatting with input -> clear input
    /// 7. If chatting (clean state) -> show exit confirmation
    pub fn on_escape(&mut self) {
        match self.state {
            AppState::Palette => {
                self.palette.reset();
                self.state = AppState::Chatting;
                self.status = "Command palette closed".into();
            }
            AppState::Confirming(_) => {
                self.state = AppState::Chatting;
                self.status = "Cancelled".into();
            }
            AppState::RunningTask => {
                self.task_progress = None;
                self.state = AppState::Chatting;
                self.status = "Task view closed".into();
            }
            AppState::FileBrowser => {
                self.state = AppState::Chatting;
                self.status = "File browser closed".into();
            }
            AppState::Help => {
                self.state = AppState::Chatting;
                self.status = "Help closed".into();
            }
            AppState::GardenView => {
                self.state = AppState::Chatting;
                self.status = "Garden view closed".into();
            }
            AppState::Chatting => {
                if !self.input.is_empty() {
                    // First escape clears input
                    self.input.clear();
                    self.cursor = 0;
                    self.status = "Input cleared".into();
                } else {
                    // Clean state - show exit confirmation
                    self.state = AppState::Confirming("Press Enter to exit, Esc to cancel".into());
                    self.status = "Exit?".into();
                }
            }
        }
    }

    /// Increase animation speed (+ key)
    pub fn on_plus(&mut self) {
        self.animation_speed =
            (self.animation_speed + ANIMATION_SPEED_STEP).min(ANIMATION_SPEED_MAX);
        self.status = format!("Animation speed: {:.0}%", self.animation_speed * 100.0);
    }

    /// Decrease animation speed (- key)
    pub fn on_minus(&mut self) {
        self.animation_speed =
            (self.animation_speed - ANIMATION_SPEED_STEP).max(ANIMATION_SPEED_MIN);
        self.status = format!("Animation speed: {:.0}%", self.animation_speed * 100.0);
    }

    /// Get animation delay based on current speed
    /// Returns the delay in milliseconds to use between animation frames
    pub fn animation_delay_ms(&self) -> u64 {
        // Base delay is 100ms, adjusted by speed (faster = shorter delay)
        let base_delay = 100.0;
        (base_delay / self.animation_speed) as u64
    }

    /// Get animation speed as percentage string
    pub fn animation_speed_display(&self) -> String {
        format!("{:.0}%", self.animation_speed * 100.0)
    }
}

#[cfg(test)]
#[path = "../../../tests/unit/ui/tui/app/app_test.rs"]
mod tests;