aether-wisp 0.5.0

A terminal UI for AI coding agents via the Agent Client Protocol (ACP)
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
use acp_utils::notifications::ElicitationAction;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Layout, Position, Rect};

use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Paragraph, StatefulWidget, Widget};
use utils::plan_review::{PlanReviewDecision, PlanReviewElicitationMeta};

use crate::renderer::DrawContext;
use crate::screens::annotation::{
    AnnotatedRows, Draft, apply_draft_key, comment_body_width, comment_box, draft_body, paste_into_draft,
};
use crate::screens::plan_review::{
    PlanDocument, ReviewComment, SourceMarkdownLine, compile_feedback, render_markdown_source_lines,
};
use crate::screens::review::{DocumentPane, MOUSE_SCROLL_LINES, Pane, body_and_footer, focused_border, focused_title};
use crate::surfaces::elicitation::ElicitationResponder;
use crate::surfaces::input::{MouseAction, PlanReviewOutput, ReviewOutcome, UiEvent, is_composed_char, is_press};
use crate::view::list_view::ListView;
use crate::view::selection::{Direction, SelectionState, step_clamped};
use crate::theme::Theme;
use crate::view::widgets::key_hints;
use crate::view::wrap::{as_u16, wrap_line, wrap_text_char};

const MIN_SPLIT_WIDTH: u16 = 60;
/// Columns the `" │ "` between the line number and the text occupies.
const GUTTER_SEPARATOR_WIDTH: usize = 3;
const OUTLINE_FRACTION: u32 = 1;
const OUTLINE_TOTAL: u32 = 4;

pub struct PlanReviewScreen {
    title: String,
    document: PlanDocument,
    comments: Vec<ReviewComment>,
    plan: DocumentPane<usize>,
    outline_selection: SelectionState,
    draft: Option<Draft<usize>>,
    focus: Pane,
    responder: ElicitationResponder,
}

/// The plan as rendered rows, with review comments woven in.
type PlanRowSet = AnnotatedRows<usize>;

impl PlanReviewScreen {
    pub fn new(meta: PlanReviewElicitationMeta, responder: impl Into<ElicitationResponder>) -> Self {
        let responder = responder.into();
        let document = PlanDocument::parse(&meta.plan_path, &meta.markdown);
        let outline_selection = SelectionState::new(document.outline.len());
        Self {
            title: meta.title,
            document,
            comments: Vec::new(),
            plan: DocumentPane::default(),
            outline_selection,
            draft: None,
            focus: Pane::Document,
            responder,
        }
    }

    fn source_line_count(&self) -> usize {
        self.document.line_count()
    }

    fn source_line_max_index(&self) -> usize {
        self.source_line_count().saturating_sub(1)
    }

    /// Scrolls the plan by rendered rows, or moves the outline when it has
    /// focus instead.
    fn scroll(&mut self, direction: Direction) {
        if self.focus == Pane::Nav {
            self.move_outline(direction);
            return;
        }
        self.plan.scroll_by(direction, MOUSE_SCROLL_LINES);
    }

    fn move_cursor(&mut self, direction: Direction) {
        self.plan.cursor = step_clamped(self.plan.cursor, direction, 1, self.source_line_max_index());
    }

    /// Moves the outline selection, stopping at either end.
    fn move_outline(&mut self, direction: Direction) {
        self.outline_selection.step_clamped(self.document.outline.len(), direction, |_| true);
    }

    /// Focuses whichever pane was clicked and moves its cursor to the clicked
    /// row, hit-testing against the areas the last render recorded.
    fn click(&mut self, row: u16, column: u16) {
        let position = Position::new(column, row);
        if self.outline_selection.rows_area().contains(position) {
            self.focus = Pane::Nav;
            self.outline_selection.select_at(row, self.document.outline.len());
            return;
        }
        if self.plan.contains(position) {
            self.focus = Pane::Document;
            self.plan.focus_at(row);
        }
    }

    fn handle_key(&mut self, key: KeyEvent) -> Vec<PlanReviewOutput> {
        if self.responder.is_answered() {
            return vec![PlanReviewOutput::Outcome(ReviewOutcome::Cancelled)];
        }
        if self.draft.is_some() {
            self.handle_draft_key(key);
            return Vec::new();
        }
        // Draft editing chords remain available; all following bindings are plain keys.
        if is_composed_char(key) {
            return Vec::new();
        }
        if let Some(outcome) = self.decision_key(key) {
            return outcome;
        }
        match self.focus {
            Pane::Document => self.handle_plan_key(key),
            Pane::Nav => self.handle_outline_key(key),
        }
        Vec::new()
    }

    /// Keys that end the review, available from either pane. The decision
    /// itself travels through the elicitation responder; the returned outcome
    /// tells the root how the review ended.
    fn decision_key(&mut self, key: KeyEvent) -> Option<Vec<PlanReviewOutput>> {
        let outcome = match key.code {
            KeyCode::Esc => {
                self.responder.cancel();
                ReviewOutcome::Cancelled
            }
            KeyCode::Char('a') => {
                self.responder
                    .respond(ElicitationAction::Accept, Some(PlanReviewDecision::Approve.response_content(None)));
                ReviewOutcome::Submitted("Approved the plan".to_string())
            }
            KeyCode::Char('r') => {
                let feedback = compile_feedback(&self.document, &self.comments);
                self.responder.respond(
                    ElicitationAction::Accept,
                    Some(PlanReviewDecision::Deny.response_content(Some(&feedback))),
                );
                ReviewOutcome::Submitted("Requested changes to the plan".to_string())
            }
            KeyCode::Char('u') => {
                self.comments.pop();
                return Some(Vec::new());
            }
            _ => return None,
        };
        Some(vec![PlanReviewOutput::Outcome(outcome)])
    }

    fn handle_plan_key(&mut self, key: KeyEvent) {
        let max = self.source_line_max_index();
        match key.code {
            KeyCode::Char('c') => self.draft = Some(Draft::new(self.plan.cursor + 1)),
            KeyCode::Char('j') | KeyCode::Down => self.move_cursor(Direction::Forward),
            KeyCode::Char('k') | KeyCode::Up => self.move_cursor(Direction::Backward),
            KeyCode::Char('g') => self.plan.cursor = 0,
            KeyCode::Char('G') => self.plan.cursor = max,
            KeyCode::Char('n') => self.jump_heading(Direction::Forward),
            KeyCode::Char('p') => self.jump_heading(Direction::Backward),
            KeyCode::Char('h') | KeyCode::Left if !self.document.outline.is_empty() => self.focus = Pane::Nav,
            _ => {}
        }
    }

    fn handle_outline_key(&mut self, key: KeyEvent) {
        let len = self.document.outline.len();
        match key.code {
            KeyCode::Char('j') | KeyCode::Down => self.move_outline(Direction::Forward),
            KeyCode::Char('k') | KeyCode::Up => self.move_outline(Direction::Backward),
            KeyCode::Char('g') => self.outline_selection.select_first(len),
            KeyCode::Char('G') => self.outline_selection.select(Some(len.saturating_sub(1)), len),
            KeyCode::Enter => {
                if let Some(section) =
                    self.outline_selection.selected().and_then(|selected| self.document.outline.get(selected))
                {
                    self.plan.cursor = section.first_line_no.saturating_sub(1).min(self.source_line_max_index());
                    self.focus = Pane::Document;
                }
            }
            KeyCode::Char('l') | KeyCode::Right => self.focus = Pane::Document,
            _ => {}
        }
    }

    fn handle_draft_key(&mut self, key: KeyEvent) {
        if let Some((line_no, body)) = apply_draft_key(&mut self.draft, key) {
            self.comments.push(ReviewComment::new(line_no, body));
        }
    }

    /// Moves the cursor to the next or previous section heading.
    fn jump_heading(&mut self, direction: Direction) {
        let headings = self.document.outline.iter().map(|section| section.first_line_no.saturating_sub(1));
        let target = match direction {
            Direction::Forward => headings.into_iter().find(|&line| line > self.plan.cursor),
            Direction::Backward => headings.into_iter().rfind(|&line| line < self.plan.cursor),
        };
        if let Some(line) = target {
            self.plan.cursor = line.min(self.source_line_max_index());
        }
    }

    fn render_screen(
        &mut self,
        area: Rect,
        buf: &mut Buffer,
        theme: &Theme,
        source_lines: &[SourceMarkdownLine],
    ) -> Option<Position> {
        if area.height < 4 || area.width < 20 {
            Paragraph::new("Plan review view is too small").style(Style::new().fg(theme.error)).render(area, buf);
            return None;
        }

        let (body_area, footer_area) = body_and_footer(area);
        let use_split = area.width >= MIN_SPLIT_WIDTH && !self.document.outline.is_empty();

        let cursor = if use_split {
            let [outline_area, plan_area] = Layout::horizontal([
                Constraint::Ratio(OUTLINE_FRACTION, OUTLINE_TOTAL),
                Constraint::Ratio(OUTLINE_TOTAL - OUTLINE_FRACTION, OUTLINE_TOTAL),
            ])
            .areas(body_area);

            self.render_outline(outline_area, buf, theme);
            self.render_plan(plan_area, buf, theme, source_lines)
        } else {
            self.render_plan(body_area, buf, theme, source_lines)
        };

        self.render_footer(footer_area, buf, theme, use_split);
        cursor
    }

    fn render_outline(&mut self, area: Rect, buf: &mut Buffer, theme: &Theme) {
        let focused = self.focus == Pane::Nav;
        let block = Block::bordered()
            .border_style(focused_border(focused, theme))
            .title(Line::styled(" Outline ", focused_title(focused, theme)));

        let rows = self
            .document
            .outline
            .iter()
            .map(|section| {
                let indent = "  ".repeat(usize::from(section.level.saturating_sub(1)));
                Line::raw(format!("  {indent}{}", section.title))
            })
            .collect();
        let highlight_style = if self.focus == Pane::Nav {
            Style::new().bg(theme.accent).fg(theme.background)
        } else {
            Style::new().fg(theme.accent)
        };
        let view =
            ListView::new(rows, theme).block(block).scrollbar().highlight_symbol("> ").highlight_style(highlight_style);
        StatefulWidget::render(view, area, buf, &mut self.outline_selection);
    }

    fn render_plan(
        &mut self,
        area: Rect,
        buf: &mut Buffer,
        theme: &Theme,
        source_lines: &[SourceMarkdownLine],
    ) -> Option<Position> {
        let focused = self.focus == Pane::Document;
        let block = Block::bordered().border_style(focused_border(focused, theme)).title(format!(" {} ", self.title));

        let inner = block.inner(area);
        block.render(area, buf);

        if inner.height == 0 {
            return None;
        }
        if self.source_line_count() == 0 {
            Paragraph::new("Plan is empty").style(Style::new().fg(theme.muted)).render(inner, buf);
            return None;
        }

        let gutter_width = self
            .source_line_count()
            .checked_ilog10()
            .map_or(0, |digits| usize::try_from(digits).unwrap_or(0))
            + 1
            + GUTTER_SEPARATOR_WIDTH;
        let content_width = PlanRowSet::content_width(inner.width).saturating_sub(as_u16(gutter_width));
        if content_width == 0 {
            return None;
        }

        let mark_cursor = focused && self.draft.is_none();
        let rows = self.build_rows(gutter_width, content_width, theme, source_lines);
        self.plan.render_rows(rows, inner, buf, theme, mark_cursor);
        self.plan.draft_cursor_position()
    }

    /// Lays the plan out as rendered rows, with each comment and the draft woven
    /// in beneath the source line it is anchored to.
    fn build_rows(
        &self,
        gutter_width: usize,
        content_width: u16,
        theme: &Theme,
        source_lines: &[SourceMarkdownLine],
    ) -> PlanRowSet {
        let mut rows = PlanRowSet::default();

        for (index, source) in source_lines.iter().enumerate().take(self.source_line_count()) {
            let line_no = index + 1;

            for (wrap_index, wrapped) in wrap_line(source.line.clone(), content_width).into_iter().enumerate() {
                let mut row = if wrap_index == 0 {
                    let num_width = gutter_width.saturating_sub(GUTTER_SEPARATOR_WIDTH);
                    let mut line = Line::default();
                    line.push_span(Span::styled(format!("{line_no:>num_width$}"), Style::new().fg(theme.text_secondary)));
                    line.push_span(Span::styled("", Style::new().fg(theme.muted)));
                    line
                } else {
                    Line::from(Span::raw(" ".repeat(gutter_width)))
                };
                row.spans.extend(wrapped.spans);
                // Only the first row of a wrapped line takes the cursor, so
                // stepping by one always moves a whole source line.
                if wrap_index == 0 {
                    rows.push(row, index);
                } else {
                    rows.push_anchored(row, index);
                }
            }

            let box_width = content_width.saturating_add(as_u16(gutter_width));
            for comment in self.comments.iter().filter(|comment| comment.line_no == line_no) {
                rows.push_annotation(comment_box(
                    &format!("┌─ Comment on line {line_no}"),
                    &wrap_text_char(&comment.body, comment_body_width(box_width)),
                    theme.info,
                    box_width,
                    theme,
                ));
            }

            if let Some(draft) = self.draft.as_ref().filter(|draft| draft.anchor == line_no) {
                let (body, cursor) = draft_body(draft, comment_body_width(box_width));
                rows.push_draft(comment_box("┌ Draft ─", &body, theme.accent, box_width, theme), cursor);
            }
        }

        rows
    }

    fn render_footer(&self, area: Rect, buf: &mut Buffer, theme: &Theme, has_outline: bool) {
        let mut hints = vec![("j/k", "move")];
        if self.focus == Pane::Document {
            hints.push(("n/p", "heading"));
            if has_outline {
                hints.push(("h", "outline"));
            }
            hints.push(("c", "comment"));
        } else {
            hints.extend([("g/G", "top/end"), ("enter", "jump"), ("l", "plan")]);
        }
        hints.extend([("u", "undo"), ("a", "approve"), ("r", "changes"), ("Esc", "cancel")]);

        Paragraph::new(key_hints(&hints, theme)).style(Style::new().bg(theme.sidebar_bg)).render(area, buf);
    }
}

impl PlanReviewScreen {
    pub(crate) fn on_ui_event(&mut self, event: UiEvent) -> Vec<PlanReviewOutput> {
        match event {
            UiEvent::Key(key) if is_press(key) => self.on_key(key),
            UiEvent::Key(_) => Vec::new(),
            UiEvent::Paste(text) => {
                paste_into_draft(&mut self.draft, &text);
                Vec::new()
            }
            UiEvent::Mouse(action, (column, row)) => self.on_mouse(action, row, column),
        }
    }

    pub fn on_key(&mut self, key: KeyEvent) -> Vec<PlanReviewOutput> {
        self.handle_key(key)
    }

    pub fn on_mouse(&mut self, action: MouseAction, row: u16, column: u16) -> Vec<PlanReviewOutput> {
        match action {
            MouseAction::ScrollUp => self.scroll(Direction::Backward),
            MouseAction::ScrollDown => self.scroll(Direction::Forward),
            MouseAction::Click => self.click(row, column),
        }
        Vec::new()
    }

    pub fn cancel(&mut self) {
        self.responder.cancel();
    }
}

impl PlanReviewScreen {
    pub fn render(&mut self, area: Rect, buf: &mut Buffer, cx: &mut DrawContext<'_>) -> Option<Position> {
        let source_lines = render_markdown_source_lines(&self.document.markdown_text(), cx.theme, cx.highlighter);
        self.render_screen(area, buf, cx.theme, &source_lines)
    }
}