eli-cli 0.1.0

CLI binary for eli coding agent
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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Merged TUI+CLI chat interface using alternate screen
//!
//! Combines TUI's beautiful rendering with CLI's full functionality.

use anyhow::Result;
use crossterm::event::{self, DisableBracketedPaste, EnableBracketedPaste, Event, KeyCode, KeyModifiers};
use crossterm::execute;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen};
use ratatui::backend::CrosstermBackend;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph, Wrap};
use ratatui::Frame;
use std::io::{self, Stdout};
use std::time::{Duration, Instant};
use textwrap::{wrap, Options as WrapOptions};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

const SPINNER: [&str; 10] = ["", "", "", "", "", "", "", "", "", ""];

/// A message in the chat history
#[derive(Clone)]
pub struct ChatMessage {
    pub role: String,    // "You", "Eli", "System", "Tool", etc.
    pub content: String,
}

/// Prompt mode for the input
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PromptMode {
    Ask,
    Plan,
    Auto,
}

/// The merged chat UI state
pub struct ChatUi {
    // Messages and history
    pub messages: Vec<ChatMessage>,
    pub history: Vec<String>,
    pub history_cursor: Option<usize>,

    // Input state
    pub input: String,
    pub cursor_pos: usize,

    // UI state
    pub is_processing: bool,
    pub spinner_idx: usize,
    pub last_spinner: Instant,
    pub sources: Vec<String>,
    pub last_tool_ok: Option<bool>,

    // Stats
    pub total_tokens: u32,
    pub elapsed_secs: u64,
    pub prompt_mode: PromptMode,
    pub queue_len: usize,
    pub show_tips: bool,
    pub interrupt_requested: bool,
    last_esc: Option<Instant>,

    // Control
    pub should_quit: bool,

    // Queued prompts (while processing)
    pub queue: Vec<String>,

    // Scrollback
    pub scroll_offset: usize,
    pub follow_tail: bool,
    pub last_total_lines: usize,
    pub last_view_height: usize,
    pub scrollback_max_lines: usize,
}

impl ChatUi {
    pub fn new() -> Self {
        Self {
            messages: vec![ChatMessage {
                role: "System".to_string(),
                content: "Welcome to ELI. Type your question or /help for commands (incl. /model).".to_string(),
            }],
            history: Vec::new(),
            history_cursor: None,
            input: String::new(),
            cursor_pos: 0,
            is_processing: false,
            spinner_idx: 0,
            last_spinner: Instant::now(),
            sources: Vec::new(),
            last_tool_ok: None,
            total_tokens: 0,
            elapsed_secs: 0,
            prompt_mode: PromptMode::Auto,
            queue_len: 0,
            show_tips: true,
            interrupt_requested: false,
            last_esc: None,
            should_quit: false,
            queue: Vec::new(),
            scroll_offset: 0,
            follow_tail: true,
            last_total_lines: 0,
            last_view_height: 0,
            scrollback_max_lines: 10_000,
        }
    }

    /// Add a message to the chat
    pub fn add_message(&mut self, role: &str, content: &str) {
        self.messages.push(ChatMessage {
            role: role.to_string(),
            content: content.to_string(),
        });
    }

    /// Append content to the last message (for streaming)
    pub fn append_to_last(&mut self, content: &str) {
        if let Some(last) = self.messages.last_mut() {
            last.content.push_str(content);
        }
    }

    /// Add a source
    pub fn add_source(&mut self, source: &str) {
        let trimmed = source.trim();
        if !trimmed.is_empty() && !self.sources.iter().any(|s| s.eq_ignore_ascii_case(trimmed)) {
            self.sources.push(trimmed.to_string());
        }
    }

    /// Clear sources for new query
    pub fn clear_sources(&mut self) {
        self.sources.clear();
        self.last_tool_ok = None;
    }

    /// Cycle prompt mode: Ask -> Plan -> Auto -> Ask
    pub fn cycle_mode(&mut self) {
        self.prompt_mode = PromptMode::Auto;
    }

    /// Queue a prompt to run after the current task
    pub fn queue_prompt(&mut self, prompt: String) {
        self.queue.push(prompt);
        self.queue_len = self.queue.len();
    }

    /// Pop the next queued prompt
    pub fn pop_queued(&mut self) -> Option<String> {
        if self.queue.is_empty() {
            None
        } else {
            let next = self.queue.remove(0);
            self.queue_len = self.queue.len();
            Some(next)
        }
    }

    /// Get submitted input and clear buffer
    pub fn submit_input(&mut self) -> Option<String> {
        let trimmed = self.input.trim().to_string();
        if trimmed.is_empty() {
            return None;
        }
        self.follow_tail = true;
        self.history.push(trimmed.clone());
        self.history_cursor = None;
        self.input.clear();
        self.cursor_pos = 0;
        self.last_esc = None;
        Some(trimmed)
    }

    /// Handle a key event, returns Some(submitted_input) if Enter was pressed
    pub fn handle_key(&mut self, code: KeyCode, modifiers: KeyModifiers) -> Option<String> {
        // Ctrl+C = clear input
        if modifiers.contains(KeyModifiers::CONTROL) && code == KeyCode::Char('c') {
            self.history_cursor = None;
            self.input.clear();
            self.cursor_pos = 0;
            self.last_esc = None;
            return None;
        }

        // Ctrl+U = page up, Ctrl+D = page down (or quit if already at bottom)
        if modifiers.contains(KeyModifiers::CONTROL) && code == KeyCode::Char('u') {
            let page = self.page_size();
            self.scroll_by(-(page as isize));
            self.last_esc = None;
            return None;
        }
        if modifiers.contains(KeyModifiers::CONTROL) && code == KeyCode::Char('d') {
            if self.scroll_offset < self.max_scroll_offset() {
                let page = self.page_size();
                self.scroll_by(page as isize);
            } else {
                self.should_quit = true;
            }
            self.last_esc = None;
            return None;
        }

        match code {
            KeyCode::PageUp => {
                let page = self.page_size();
                self.scroll_by(-(page as isize));
                self.last_esc = None;
            }
            KeyCode::PageDown => {
                let page = self.page_size();
                self.scroll_by(page as isize);
                self.last_esc = None;
            }
            KeyCode::Enter => {
                return self.submit_input();
            }
            KeyCode::Char(c) => {
                self.history_cursor = None;
                self.input.insert(self.cursor_pos, c);
                self.cursor_pos += 1;
                self.last_esc = None;
            }
            KeyCode::Backspace => {
                self.history_cursor = None;
                if self.cursor_pos > 0 {
                    self.cursor_pos -= 1;
                    self.input.remove(self.cursor_pos);
                }
                self.last_esc = None;
            }
            KeyCode::Delete => {
                self.history_cursor = None;
                if self.cursor_pos < self.input.len() {
                    self.input.remove(self.cursor_pos);
                }
                self.last_esc = None;
            }
            KeyCode::Left => {
                if self.cursor_pos > 0 {
                    self.cursor_pos -= 1;
                }
                self.last_esc = None;
            }
            KeyCode::Right => {
                if self.cursor_pos < self.input.len() {
                    self.cursor_pos += 1;
                }
                self.last_esc = None;
            }
            KeyCode::Home => {
                self.cursor_pos = 0;
                self.last_esc = None;
            }
            KeyCode::End => {
                self.cursor_pos = self.input.len();
                self.last_esc = None;
            }
            KeyCode::Up => {
                if let Some(last_idx) = self.history.len().checked_sub(1) {
                    let next = match self.history_cursor {
                        None => Some(last_idx),
                        Some(idx) => idx.checked_sub(1),
                    };
                    if let Some(idx) = next {
                        self.history_cursor = Some(idx);
                        self.input = self.history[idx].clone();
                        self.cursor_pos = self.input.len();
                    }
                }
                self.last_esc = None;
            }
            KeyCode::Down => {
                if let Some(idx) = self.history_cursor {
                    let next = idx.saturating_add(1);
                    if next >= self.history.len() {
                        self.history_cursor = None;
                        self.input.clear();
                        self.cursor_pos = 0;
                    } else {
                        self.history_cursor = Some(next);
                        self.input = self.history[next].clone();
                        self.cursor_pos = self.input.len();
                    }
                }
                self.last_esc = None;
            }
            KeyCode::Esc => {
                if self.is_processing {
                    self.interrupt_requested = true;
                    return None;
                }
                let now = Instant::now();
                if let Some(last) = self.last_esc {
                    if now.duration_since(last) <= Duration::from_millis(800) {
                        self.history_cursor = None;
                        self.input.clear();
                        self.cursor_pos = 0;
                        self.last_esc = None;
                        return None;
                    }
                }
                self.last_esc = Some(now);
                return None;
            }
            KeyCode::BackTab => {
                self.last_esc = None;
            }
            _ => {}
        }
        None
    }

    pub fn page_size(&self) -> usize {
        self.last_view_height.saturating_sub(1).max(1)
    }

    pub fn max_scroll_offset(&self) -> usize {
        if self.last_total_lines <= self.last_view_height {
            0
        } else {
            self.last_total_lines.saturating_sub(self.last_view_height)
        }
    }

    pub fn scroll_by(&mut self, delta: isize) {
        let max_offset = self.max_scroll_offset();
        if delta.is_negative() {
            let down = (-delta) as usize;
            self.scroll_offset = self.scroll_offset.saturating_sub(down);
        } else {
            self.scroll_offset = (self.scroll_offset + delta as usize).min(max_offset);
        }
        self.follow_tail = self.scroll_offset >= max_offset;
    }

    pub fn handle_paste(&mut self, text: &str) {
        let normalized = text.replace("\r\n", "\n").replace('\r', "\n");
        self.history_cursor = None;
        self.input.insert_str(self.cursor_pos, &normalized);
        self.cursor_pos += normalized.len();
        self.last_esc = None;
    }

    /// Update spinner animation
    pub fn tick_spinner(&mut self) {
        if self.last_spinner.elapsed() > Duration::from_millis(120) {
            self.spinner_idx = (self.spinner_idx + 1) % SPINNER.len();
            self.last_spinner = Instant::now();
        }
    }

    /// Render the UI
    pub fn ui(&mut self, f: &mut Frame) {
        let area = f.size();
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Min(5),     // Chat body
                Constraint::Length(3),  // Input box
                Constraint::Length(1),  // Sources + hints footer
            ])
            .split(area);

        self.render_chat(f, chunks[0]);
        self.render_input(f, chunks[1]);
        self.render_sources(f, chunks[2]);
    }

    fn render_chat(&mut self, f: &mut Frame, area: Rect) {
        let mut lines: Vec<Line> = Vec::new();
        let width = area.width.max(1) as usize;

        let push_wrapped = |lines: &mut Vec<Line>,
                            content: &str,
                            prefix_first: &str,
                            prefix_rest: &str,
                            prefix_style: Style,
                            content_style: Style| {
            let prefix_width = prefix_first.width();
            let content_width = width.saturating_sub(prefix_width).max(1);
            let options = WrapOptions::new(content_width).break_words(true);
            let wrapped = wrap(content, &options);
            for (idx, seg) in wrapped.iter().enumerate() {
                let prefix = if idx == 0 { prefix_first } else { prefix_rest };
                lines.push(Line::from(vec![
                    Span::styled(prefix.to_string(), prefix_style),
                    Span::styled(seg.to_string(), content_style),
                ]));
            }
        };

        for msg in &self.messages {
            match msg.role.as_str() {
                "You" => {
                    push_wrapped(
                        &mut lines,
                        &msg.content,
                        "",
                        "  ",
                        Style::default().fg(Color::Cyan),
                        Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
                    );
                    lines.push(Line::from(""));
                }
                "Eli" => {
                    for (i, content_line) in msg.content.lines().enumerate() {
                        if content_line.trim().is_empty() {
                            lines.push(Line::from(""));
                            continue;
                        }
                        let prefix_first = if i == 0 { "" } else { "  " };
                        push_wrapped(
                            &mut lines,
                            content_line,
                            prefix_first,
                            "  ",
                            Style::default().fg(Color::Cyan),
                            Style::default().fg(Color::White),
                        );
                    }
                    lines.push(Line::from(""));
                }
                "Tool" => {
                    // Tool output in gray (no truncation)
                    for content_line in msg.content.lines() {
                        push_wrapped(
                            &mut lines,
                            content_line,
                            "  ",
                            "  ",
                            Style::default(),
                            Style::default().fg(Color::DarkGray),
                        );
                    }
                }
                "System" => {
                    push_wrapped(
                        &mut lines,
                        &msg.content,
                        "",
                        "  ",
                        Style::default().fg(Color::Blue),
                        Style::default().fg(Color::DarkGray),
                    );
                    lines.push(Line::from(""));
                }
                "Error" => {
                    push_wrapped(
                        &mut lines,
                        &msg.content,
                        "",
                        "  ",
                        Style::default().fg(Color::Red),
                        Style::default().fg(Color::Red),
                    );
                    lines.push(Line::from(""));
                }
                "Progress" => {
                    // Progress/status updates
                    push_wrapped(
                        &mut lines,
                        &msg.content,
                        "",
                        "  ",
                        Style::default().fg(Color::Yellow),
                        Style::default().fg(Color::DarkGray),
                    );
                }
                _ => {
                    push_wrapped(
                        &mut lines,
                        &msg.content,
                        "",
                        "",
                        Style::default(),
                        Style::default().fg(Color::White),
                    );
                }
            }
        }

        if self.scrollback_max_lines > 0 && lines.len() > self.scrollback_max_lines {
            let drop = lines.len().saturating_sub(self.scrollback_max_lines);
            lines = lines.split_off(drop);
            if self.scroll_offset > drop {
                self.scroll_offset -= drop;
            } else {
                self.scroll_offset = 0;
                self.follow_tail = true;
            }
        }

        let visible_height = area.height as usize;
        let total_lines = lines.len();
        let max_offset = if total_lines > visible_height {
            total_lines - visible_height
        } else {
            0
        };
        if self.follow_tail {
            self.scroll_offset = max_offset;
        } else if self.scroll_offset > max_offset {
            self.scroll_offset = max_offset;
        }
        self.last_total_lines = total_lines;
        self.last_view_height = visible_height;
        if max_offset == 0 {
            self.follow_tail = true;
            self.scroll_offset = 0;
        }

        let chat = Paragraph::new(lines)
            .scroll((self.scroll_offset as u16, 0))
            .wrap(Wrap { trim: false });
        f.render_widget(chat, area);
    }

    fn render_input(&self, f: &mut Frame, area: Rect) {
        f.render_widget(Clear, area);

        let cursor_pos = self.cursor_pos.min(self.input.len());
        let display = self.input.replace('\n', "");
        let cursor_idx = self.input[..cursor_pos].chars().count();
        let prompt = "";
        let width = area.width.max(1) as usize;
        let inner_width = width.saturating_sub(2);
        let content_width = inner_width.saturating_sub(prompt.width()).max(1);

        let window_with_cursor = |text: &str, cursor_idx: usize, max_width: usize| -> (String, usize) {
            let chars: Vec<char> = text.chars().collect();
            if chars.is_empty() {
                return (String::new(), 0);
            }
            let widths: Vec<usize> = chars
                .iter()
                .map(|c| UnicodeWidthChar::width(*c).unwrap_or(0).max(1))
                .collect();
            let total_width: usize = widths.iter().sum();
            let cursor_idx = cursor_idx.min(chars.len());

            if total_width <= max_width {
                return (text.to_string(), cursor_idx);
            }

            let mut end = cursor_idx.min(chars.len());
            if end < chars.len() {
                end = end.saturating_add(1);
            }
            let mut start = end;
            let mut width_acc = 0usize;
            while start > 0 {
                let w = widths[start - 1];
                if width_acc + w > max_width {
                    break;
                }
                width_acc += w;
                start -= 1;
            }
            let visible: String = chars[start..end].iter().collect();
            let cursor_in_window = cursor_idx.saturating_sub(start);
            (visible, cursor_in_window)
        };

        let (visible, cursor_in_window) = window_with_cursor(&display, cursor_idx, content_width);

        let input_spans = if self.is_processing {
            vec![
                Span::styled("", Style::default().fg(Color::DarkGray)),
                Span::styled(visible, Style::default().fg(Color::DarkGray)),
            ]
        } else {
            let visible_chars: Vec<char> = visible.chars().collect();
            let cursor_char = if cursor_in_window < visible_chars.len() {
                visible_chars[cursor_in_window]
            } else {
                ' '
            };
            let before_cursor = visible_chars
                .iter()
                .take(cursor_in_window)
                .collect::<String>();
            let after_cursor = if cursor_in_window < visible_chars.len() {
                visible_chars
                    .iter()
                    .skip(cursor_in_window + 1)
                    .collect::<String>()
            } else {
                String::new()
            };

            vec![
                Span::styled("", Style::default().fg(Color::Cyan)),
                Span::styled(before_cursor, Style::default().fg(Color::White)),
                Span::styled(
                    cursor_char.to_string(),
                    Style::default().fg(Color::Black).bg(Color::White),
                ),
                Span::styled(after_cursor, Style::default().fg(Color::White)),
            ]
        };

        let title = self.input_title();
        let border_color = if self.is_processing {
            Color::DarkGray
        } else {
            Color::Cyan
        };

        let input = Paragraph::new(Line::from(input_spans)).block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(border_color))
                .title(title)
                .title_style(Style::default().fg(border_color)),
        );
        f.render_widget(input, area);
    }

    fn input_title(&self) -> String {
        String::new()
    }

    fn render_sources(&self, f: &mut Frame, area: Rect) {
        f.render_widget(Clear, area);
        let mut spans: Vec<Span> = Vec::new();

        // Tool status icon
        if let Some(ok) = self.last_tool_ok {
            let (symbol, color) = if ok {
                ("", Color::Green)
            } else {
                ("", Color::Red)
            };
            spans.push(Span::styled(format!("{} ", symbol), Style::default().fg(color)));
        }

        // Sources
        if !self.sources.is_empty() {
            spans.push(Span::styled(
                self.sources.join(", "),
                Style::default().fg(Color::Gray),
            ));
            spans.push(Span::styled("", Style::default().fg(Color::DarkGray)));
        }

        // Status: mode, time, tokens
        let mode = "AUTO";
        let phase = if self.is_processing { "working" } else { "ready" };
        let spinner = if self.is_processing {
            format!("{} ", SPINNER[self.spinner_idx % SPINNER.len()])
        } else {
            String::new()
        };
        let queued = if self.queue_len > 0 {
            format!(" · {} queued", self.queue_len)
        } else {
            String::new()
        };
        spans.push(Span::styled(
            format!(
                "{}{} [{}] {}s · {} tokens{}",
                spinner, phase, mode, self.elapsed_secs, self.total_tokens, queued
            ),
            Style::default().fg(Color::DarkGray),
        ));

        let max_offset = self.max_scroll_offset();
        if max_offset > 0 && !self.follow_tail {
            let remaining = max_offset.saturating_sub(self.scroll_offset);
            spans.push(Span::styled(
                format!(" │ scroll: {} lines above", remaining),
                Style::default().fg(Color::DarkGray),
            ));
        }

        // Hints (only when not processing)
        if !self.is_processing && self.show_tips {
            spans.push(Span::styled(
                format!(
                    " │ /compact reduce tokens │ /reset clear tokens │ /tip hide tips"
                ),
                Style::default().fg(Color::DarkGray),
            ));
        }

        let footer = Paragraph::new(Line::from(spans));
        f.render_widget(footer, area);
    }
}

/// Terminal wrapper for alternate screen
pub struct ChatTerminal {
    terminal: ratatui::Terminal<CrosstermBackend<Stdout>>,
}

impl ChatTerminal {
    pub fn new() -> Result<Self> {
        enable_raw_mode()?;
        let mut stdout = io::stdout();
        execute!(stdout, EnterAlternateScreen, EnableBracketedPaste)?;
        let backend = CrosstermBackend::new(stdout);
        let terminal = ratatui::Terminal::new(backend)?;
        Ok(Self { terminal })
    }

    pub fn draw(&mut self, ui: &mut ChatUi) -> Result<()> {
        self.terminal.draw(|f| ui.ui(f))?;
        Ok(())
    }

    /// Poll for events with timeout, returns Some(Event) if available
    pub fn poll_event(&self, timeout: Duration) -> Result<Option<Event>> {
        if event::poll(timeout)? {
            Ok(Some(event::read()?))
        } else {
            Ok(None)
        }
    }
}

impl Drop for ChatTerminal {
    fn drop(&mut self) {
        disable_raw_mode().ok();
        execute!(self.terminal.backend_mut(), DisableBracketedPaste, LeaveAlternateScreen).ok();
        self.terminal.show_cursor().ok();
    }
}