aether-wisp 0.4.20

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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use crate::components::common::render_key_hints;
use crate::components::file_list_panel::{FileListMessage, FileListPanel};
use crate::components::git_diff::git_diff_panel::{GitDiffPanel, GitDiffPanelMessage};
use crate::components::git_diff::{DiffAnchor, PatchAnchor};
use crate::components::review_comments::{CommentAnchor, ReviewComment};
use crate::git_diff::{GitDiffDocument, PatchLineKind, load_git_diff};
use std::collections::HashMap;
use std::path::PathBuf;
use tui::{Component, Either, Event, Frame, KeyCode, Line, SplitLayout, SplitPanel, Style, ViewContext};

pub enum GitDiffViewMessage {
    Close,
    Refresh,
    SubmitPrompt(String),
}

pub enum GitDiffLoadState {
    Loading,
    Ready(GitDiffDocument),
    Empty,
    Error { message: String },
}

#[derive(Debug, Clone)]
pub(crate) struct GitDiffCommentContext {
    pub file_path: String,
    pub line_text: String,
    pub line_number: Option<usize>,
    pub line_kind: PatchLineKind,
}

#[derive(Debug, Clone)]
pub(crate) struct QueuedComment {
    pub review: ReviewComment<PatchAnchor>,
    pub context: GitDiffCommentContext,
}

pub struct GitDiffMode {
    working_dir: PathBuf,
    cached_repo_root: Option<PathBuf>,
    document_revision: usize,
    pub load_state: GitDiffLoadState,
    split: SplitPanel<FileListPanel, GitDiffPanel>,
    comments: ReviewQueue,
    pending_restore: Option<RefreshState>,
}

impl GitDiffMode {
    pub fn new(working_dir: PathBuf) -> Self {
        Self {
            working_dir,
            cached_repo_root: None,
            document_revision: 0,
            load_state: GitDiffLoadState::Empty,
            split: SplitPanel::new(FileListPanel::new(), GitDiffPanel::new(), SplitLayout::fraction(1, 3, 20, 28))
                .with_separator("", Style::default())
                .with_resize_keys(),
            comments: ReviewQueue::default(),
            pending_restore: None,
        }
    }

    pub(crate) fn begin_open(&mut self) {
        self.reset(GitDiffLoadState::Loading);
    }

    pub(crate) fn begin_refresh(&mut self) {
        self.pending_restore = Some(RefreshState {
            selected_path: self.selected_file_path().map(ToOwned::to_owned),
            was_right_focused: !self.split.is_left_focused(),
        });
        self.load_state = GitDiffLoadState::Loading;
        self.split.right_mut().clear_rendered_patches();
    }

    pub(crate) async fn complete_load(&mut self) {
        match load_git_diff(&self.working_dir, self.cached_repo_root.as_deref()).await {
            Ok(doc) => {
                if self.cached_repo_root.is_none() {
                    self.cached_repo_root = Some(doc.repo_root.clone());
                }
                let restore = self.pending_restore.take();
                self.apply_loaded_document(doc, restore);
            }
            Err(error) => {
                self.pending_restore = None;
                self.load_state = GitDiffLoadState::Error { message: error.to_string() };
                self.split.right_mut().clear_rendered_patches();
            }
        }
    }

    pub(crate) fn close(&mut self) {
        self.reset(GitDiffLoadState::Empty);
    }

    fn reset(&mut self, load_state: GitDiffLoadState) {
        self.pending_restore = None;
        self.load_state = load_state;
        *self.split.left_mut() = FileListPanel::new();
        *self.split.right_mut() = GitDiffPanel::new();
        self.comments.clear();
        self.split.focus_left();
    }

    pub fn load_document(&mut self, doc: GitDiffDocument) {
        self.apply_loaded_document(doc, None);
    }

    pub fn set_load_state(&mut self, state: GitDiffLoadState) {
        self.load_state = state;
    }
}

impl Component for GitDiffMode {
    type Message = GitDiffViewMessage;

    async fn on_event(&mut self, event: &Event) -> Option<Vec<GitDiffViewMessage>> {
        if self.split.right().is_in_comment_mode() {
            return Some(self.on_comment_mode_event(event).await);
        }

        if let Event::Key(key) = event {
            match key.code {
                KeyCode::Esc => return Some(vec![GitDiffViewMessage::Close]),
                KeyCode::Char('r') => return Some(vec![GitDiffViewMessage::Refresh]),
                KeyCode::Char('u') => {
                    self.comments.pop();
                    self.sync_comment_state();
                    self.split.right_mut().invalidate_comment_splices();
                    return Some(vec![]);
                }
                KeyCode::Char('s') if !self.split.is_left_focused() => {
                    return Some(self.submit_review());
                }
                KeyCode::Char('h') | KeyCode::Left if !self.split.is_left_focused() => {
                    self.split.focus_left();
                    return Some(vec![]);
                }
                _ => {}
            }
        }

        self.split.on_event(event).await.map(|msgs| self.handle_split_messages(msgs))
    }

    fn render(&mut self, context: &ViewContext) -> Frame {
        let theme = &context.theme;
        if context.size.width < 10 {
            return Frame::new(vec![Line::new("Too narrow")]);
        }

        let body_height = context.size.height.saturating_sub(1);
        let body_context = context.with_size((context.size.width, body_height));

        let status_msg = match &self.load_state {
            GitDiffLoadState::Loading => Some("Loading...".to_string()),
            GitDiffLoadState::Empty => Some("No changes in working tree relative to HEAD".to_string()),
            GitDiffLoadState::Ready(doc) if doc.files.is_empty() => {
                Some("No changes in working tree relative to HEAD".to_string())
            }
            GitDiffLoadState::Error { message } => Some(format!("Git diff unavailable: {message}")),
            GitDiffLoadState::Ready(_) => None,
        };

        let body = if let Some(msg) = status_msg {
            let height = body_height as usize;
            let widths = self.split.widths(context.size.width);
            let left_width = widths.left as usize;
            let mut rows = Vec::with_capacity(height);
            for i in 0..height {
                let mut line = Line::default();
                line.push_text(" ".repeat(left_width));
                line.push_with_style("", Style::fg(theme.muted()));
                if i == 0 {
                    line.push_with_style(&msg, Style::fg(theme.text_secondary()));
                }
                rows.push(line);
            }
            Frame::new(rows)
        } else {
            let left_focused = self.split.is_left_focused();
            self.split.left_mut().set_focused(left_focused);
            self.split.right_mut().set_focused(!left_focused);
            self.prepare_right_panel_layers(&body_context);
            self.split.set_separator_style(Style::fg(theme.muted()));
            self.split.render(&body_context)
        };

        let help_keys: &[(&str, &str)] = if self.split.is_left_focused() { &LEFT_HELP_KEYS } else { &RIGHT_HELP_KEYS };
        Frame::vstack([body, Frame::new(vec![render_key_hints(theme, help_keys)])])
    }
}

impl GitDiffMode {
    fn prepare_right_panel_layers(&mut self, context: &ViewContext) {
        let GitDiffLoadState::Ready(doc) = &self.load_state else {
            return;
        };

        let selected = self.split.left().selected_file_index().unwrap_or(0).min(doc.files.len().saturating_sub(1));
        let file = &doc.files[selected];

        let file_comments = self.comments.for_file(&file.path);

        let right_width = self.split.widths(context.size.width).right;
        self.split.right_mut().ensure_layers(file, &file_comments, right_width, self.document_revision);
    }

    fn on_file_selected(&mut self, idx: usize) {
        self.split.left_mut().select_file_index(idx);
        self.split.right_mut().reset_for_new_file();
    }

    async fn on_comment_mode_event(&mut self, event: &Event) -> Vec<GitDiffViewMessage> {
        if let Some(msgs) = self.split.right_mut().on_event(event).await {
            return self.handle_right_panel_messages(msgs);
        }
        vec![]
    }

    fn handle_split_messages(
        &mut self,
        msgs: Vec<Either<FileListMessage, GitDiffPanelMessage>>,
    ) -> Vec<GitDiffViewMessage> {
        let mut right_msgs = Vec::new();
        for msg in msgs {
            match msg {
                Either::Left(FileListMessage::Selected(idx)) => {
                    self.on_file_selected(idx);
                }
                Either::Left(FileListMessage::FileOpened(idx)) => {
                    self.on_file_selected(idx);
                    self.split.focus_right();
                }
                Either::Right(panel_msg) => right_msgs.push(panel_msg),
            }
        }
        self.handle_right_panel_messages(right_msgs)
    }

    fn handle_right_panel_messages(&mut self, msgs: Vec<GitDiffPanelMessage>) -> Vec<GitDiffViewMessage> {
        for msg in msgs {
            let GitDiffPanelMessage::CommentSubmitted { anchor, text } = msg;
            self.queue_comment(anchor, &text);
        }
        vec![]
    }

    fn queue_comment(&mut self, anchor: DiffAnchor, text: &str) {
        let GitDiffLoadState::Ready(doc) = &self.load_state else {
            return;
        };
        let CommentAnchor(PatchAnchor { hunk: hunk_index, line: line_index }) = anchor;
        let selected = self.split.left().selected_file_index().unwrap_or(0);
        let Some(file) = doc.files.get(selected) else {
            return;
        };
        let Some(hunk) = file.hunks.get(hunk_index) else {
            return;
        };
        let Some(patch_line) = hunk.lines.get(line_index) else {
            return;
        };

        self.comments.push(QueuedComment {
            review: ReviewComment::new(anchor, text),
            context: GitDiffCommentContext {
                file_path: file.path.clone(),
                line_text: patch_line.text.clone(),
                line_number: patch_line.new_line_no.or(patch_line.old_line_no),
                line_kind: patch_line.kind,
            },
        });
        self.sync_comment_state();
        self.split.right_mut().invalidate_comment_splices();
    }

    fn sync_comment_state(&mut self) {
        let counts = match &self.load_state {
            GitDiffLoadState::Ready(doc) => self.comments.counts_for(&doc.files),
            _ => vec![],
        };
        self.split.left_mut().sync_view_state(self.comments.len(), counts);
    }

    fn submit_review(&self) -> Vec<GitDiffViewMessage> {
        if self.comments.is_empty() {
            return vec![];
        }
        vec![GitDiffViewMessage::SubmitPrompt(self.comments.format_prompt())]
    }

    fn selected_file_path(&self) -> Option<&str> {
        let GitDiffLoadState::Ready(doc) = &self.load_state else {
            return None;
        };
        let idx = self.split.left().selected_file_index()?;
        doc.files.get(idx).map(|f| f.path.as_str())
    }

    fn apply_loaded_document(&mut self, doc: GitDiffDocument, restore: Option<RefreshState>) {
        self.document_revision = self.document_revision.saturating_add(1);

        if doc.files.is_empty() {
            self.load_state = GitDiffLoadState::Empty;
            self.split.right_mut().clear_rendered_patches();
            return;
        }

        self.split.left_mut().rebuild_from_files(&doc.files);
        self.split.right_mut().clear_rendered_patches();
        self.split.right_mut().set_repo_root(doc.repo_root.clone());

        if let Some(restore) = restore {
            if restore.was_right_focused {
                self.split.focus_right();
            } else {
                self.split.focus_left();
            }
            self.split.right_mut().reset_scroll();
            if let Some(path) = &restore.selected_path
                && let Some(idx) = doc.files.iter().position(|file| file.path == *path)
            {
                self.split.left_mut().select_file_index(idx);
            }
        }

        self.load_state = GitDiffLoadState::Ready(doc);
        self.sync_comment_state();
    }
}

const LEFT_HELP_KEYS: [(&str, &str); 6] =
    [("j/k", "move"), ("h/l", "fold/open"), ("enter", "view"), ("u", "undo"), ("r", "refresh"), ("Esc", "close")];

const RIGHT_HELP_KEYS: [(&str, &str); 8] = [
    ("j/k", "move"),
    ("h", "back"),
    ("c", "comment"),
    ("s", "submit"),
    ("o", "full file"),
    ("u", "undo"),
    ("r", "refresh"),
    ("Esc", "close"),
];

#[derive(Default)]
struct ReviewQueue {
    comments: Vec<QueuedComment>,
}

impl ReviewQueue {
    fn is_empty(&self) -> bool {
        self.comments.is_empty()
    }

    fn len(&self) -> usize {
        self.comments.len()
    }

    fn clear(&mut self) {
        self.comments.clear();
    }

    fn push(&mut self, comment: QueuedComment) {
        self.comments.push(comment);
    }

    fn pop(&mut self) -> Option<QueuedComment> {
        self.comments.pop()
    }

    fn for_file(&self, path: &str) -> Vec<&QueuedComment> {
        self.comments.iter().filter(|comment| comment.context.file_path == path).collect()
    }

    fn counts_for(&self, files: &[crate::git_diff::FileDiff]) -> Vec<usize> {
        files
            .iter()
            .map(|file| self.comments.iter().filter(|comment| comment.context.file_path == file.path).count())
            .collect()
    }

    fn format_prompt(&self) -> String {
        use std::fmt::Write;

        let mut prompt = String::from("I'm reviewing the working tree diff. Here are my comments:\n");
        let mut file_order: Vec<&str> = Vec::new();
        let mut grouped: HashMap<&str, Vec<&QueuedComment>> = HashMap::new();

        for comment in &self.comments {
            let path = comment.context.file_path.as_str();
            if !grouped.contains_key(path) {
                file_order.push(path);
            }
            grouped.entry(path).or_default().push(comment);
        }

        for file_path in file_order {
            let file_comments = grouped.get(file_path).expect("group exists for ordered path");
            write!(prompt, "\n## `{file_path}`\n").unwrap();

            for comment in file_comments {
                let kind_label = match comment.context.line_kind {
                    PatchLineKind::Added => "added",
                    PatchLineKind::Removed => "removed",
                    PatchLineKind::Context => "context",
                    PatchLineKind::HunkHeader => "header",
                    PatchLineKind::Meta => "meta",
                };
                let line_ref = match comment.context.line_number {
                    Some(n) => format!("Line {n} ({kind_label})"),
                    None => kind_label.to_string(),
                };
                write!(prompt, "\n**{line_ref}:** `{}`\n> {}\n", comment.context.line_text, comment.review.body)
                    .unwrap();
            }
        }

        prompt
    }
}

struct RefreshState {
    selected_path: Option<String>,
    was_right_focused: bool,
}