aether-wisp 0.6.3

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
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
mod view;
pub(crate) use view::ComposerBodyView;

use crate::attachment::{AttachmentKind, PromptAttachment, classify_attachment};
use crate::command::FilesystemCommand;
use crate::file_index::FileEntry;
use crate::surfaces::input::MouseAction;
use crate::surfaces::picker::{CommandEntry, CompletionOverlay};
use crate::surfaces::prompt_search::{self, PromptSearchPicker};
use crate::view::edit_buffer::{EditBuffer, apply_edit_key};
use crate::view::filterable_list::FilterableList;
use crate::request::RequestId;
use crate::view::selection::Direction;
use acp_utils::notifications::PromptSearchResponse;
use crossterm::event::KeyCode;
use ratatui::layout::Position;
use ratatui::text::Line;
use std::collections::HashSet;
use unicode_width::UnicodeWidthStr;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectedFileMention {
    pub path: std::path::PathBuf,
    pub display_name: String,
}

#[derive(Debug, Default)]
pub struct Composer {
    buffer: EditBuffer,
    overlay: Option<Overlay>,
    mentions: Vec<SelectedFileMention>,
    pending_media: Vec<PromptAttachment>,
    history: PromptHistory,
    /// Columns the last layout wrapped the input at. Unset until the composer
    /// has been drawn once, which reads as "wide enough that nothing wraps".
    content_width: Option<usize>,
}

/// The inline picker open around the composer's text.
///
/// At most one, because they compete for the same keys: making that an enum
/// rather than two `Option` fields means the pair can never both be open.
#[derive(Debug)]
enum Overlay {
    /// The `/` command or `@` file list, drawn below the text.
    Completion(CompletionOverlay),
    /// Prompt-history search, drawn above the text. It carries the draft it
    /// replaced, so backing out restores what the user was writing.
    PromptSearch { picker: PromptSearchPicker, draft: String },
}

/// The prompts submitted this session, and where recall has walked back to.
///
/// Navigation stashes the draft it replaced, so stepping past the newest entry
/// puts the user back where they started.
#[derive(Debug, Default)]
struct PromptHistory {
    entries: Vec<String>,
    index: Option<usize>,
    draft: Option<String>,
}

/// Prompts kept for recall. Older ones are dropped rather than growing forever.
const MAX_HISTORY_ENTRIES: usize = 500;

impl PromptHistory {
    fn push(&mut self, prompt: &str) {
        if prompt.trim().is_empty() {
            return;
        }
        self.entries.push(prompt.to_string());
        if self.entries.len() > MAX_HISTORY_ENTRIES {
            self.entries.remove(0);
        }
    }

    /// The previous prompt, stashing `draft` the first time recall starts.
    fn previous(&mut self, draft: &str) -> Option<&str> {
        let index = match self.index {
            Some(0) => return None,
            Some(index) => index - 1,
            None => {
                self.draft = Some(draft.to_string());
                self.entries.len().checked_sub(1)?
            }
        };
        self.index = Some(index);
        self.entries.get(index).map(String::as_str)
    }

    /// The next prompt, or the stashed draft once recall walks past the newest.
    fn next(&mut self) -> Option<String> {
        let index = self.index?;
        if index + 1 < self.entries.len() {
            self.index = Some(index + 1);
            return self.entries.get(index + 1).cloned();
        }
        self.index = None;
        Some(self.draft.take().unwrap_or_default())
    }

    /// Ends navigation, so the recalled prompt becomes the user's own draft.
    fn reset(&mut self) {
        self.index = None;
        self.draft = None;
    }
}

pub struct ComposerLayout {
    pub lines: Vec<Line<'static>>,
    pub cursor: Position,
}

/// What a keystroke or paste the composer's own overlay consumed asks the app
/// to do next.
///
/// The composer owns its overlays and every edit they imply; the app is told
/// only the two things it has to act on outside the composer, so the overlay
/// state itself never has to leave.
#[derive(Debug)]
pub enum ComposerOutcome {
    /// Fully handled inside the composer.
    Handled,
    /// A command was accepted from the `/` list and needs running.
    AcceptedCommand(CommandEntry),
    /// The history-search query changed and needs re-running against the agent.
    Search(String),
}

impl Composer {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn text(&self) -> &str {
        self.buffer.text()
    }

    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty() && self.pending_media.is_empty()
    }

    pub fn selected_mentions(&self) -> Vec<SelectedFileMention> {
        // Match whole whitespace-delimited tokens so that `@foo` does not also match the
        // longer mention `@foobar`. The insertion path always writes `@<display> `, so a
        // complete token comparison is the correct granularity.
        let tokens: HashSet<&str> = self.buffer.text().split_whitespace().collect();
        self.mentions
            .iter()
            .filter(|mention| {
                let needle = format!("@{}", mention.display_name);
                tokens.contains(needle.as_str())
            })
            .cloned()
            .collect()
    }

    pub fn take_submission(&mut self) -> (String, Vec<PromptAttachment>) {
        let text = self.buffer.take();
        let pending_media = std::mem::take(&mut self.pending_media);
        self.history.push(&text);
        self.overlay = None;
        self.mentions.clear();
        self.history.reset();
        (text, pending_media)
    }

    pub fn clear(&mut self) {
        self.buffer.set_text("");
        self.pending_media.clear();
        self.overlay = None;
        self.mentions.clear();
        self.history.reset();
    }

    pub fn add_dropped_media(&mut self, paths: Vec<std::path::PathBuf>) -> bool {
        // Atomic across the whole parsed list: if any path is missing or not a
        // regular file, the paste is ambiguous, so attach nothing and let the
        // caller keep the original payload as composer text.
        if !paths.iter().all(|path| path.is_file()) {
            return false;
        }

        let mut existing: HashSet<std::path::PathBuf> = self.pending_media.iter().map(|a| a.path.clone()).collect();

        let before = self.pending_media.len();

        for path in paths {
            if !matches!(classify_attachment(&path), AttachmentKind::Image | AttachmentKind::Audio) {
                continue;
            }
            if !existing.insert(path.clone()) {
                continue;
            }
            let display_name = path
                .file_name()
                .map_or_else(|| path.to_string_lossy().into_owned(), |n| n.to_string_lossy().into_owned());
            self.pending_media.push(PromptAttachment { path, display_name });
        }

        self.pending_media.len() > before
    }

    pub fn pending_media(&self) -> &[PromptAttachment] {
        &self.pending_media
    }

    /// Applies one shared editing keystroke. Anything that changes the text also
    /// ends history navigation, so the recalled prompt becomes the user's draft.
    pub fn apply_edit_key(&mut self, key: crossterm::event::KeyEvent) -> bool {
        if key.code == KeyCode::Backspace {
            self.backspace();
            return true;
        }
        let before = self.buffer.text().len();
        let handled = apply_edit_key(&mut self.buffer, key);
        if self.buffer.text().len() != before {
            self.history.reset();
        }
        handled
    }

    pub fn insert_char(&mut self, character: char) {
        self.history.reset();
        self.buffer.insert_char(character);
    }

    pub fn insert_str(&mut self, text: &str) {
        self.history.reset();
        self.buffer.insert_str(text);
    }

    pub fn insert_paste(&mut self, text: &str) {
        self.history.reset();
        self.buffer.insert_paste(text);
    }

    pub fn insert_newline(&mut self) {
        self.insert_char('\n');
        self.overlay = None;
    }

    pub fn backspace(&mut self) {
        self.history.reset();
        if self.buffer.is_empty() && !self.pending_media.is_empty() {
            self.pending_media.pop();
            return;
        }
        self.buffer.backspace();
    }

    pub fn move_left(&mut self) {
        self.buffer.move_left();
    }

    pub fn move_line_start(&mut self) {
        self.buffer.move_line_start();
    }

    pub fn move_line_end(&mut self) {
        self.buffer.move_line_end();
    }

    /// Moves the cursor to the visual row above, reporting whether there was
    /// one. A long line the composer soft-wrapped has rows above without any
    /// newline before the cursor, so this follows what is on screen rather than
    /// the newlines in the text.
    pub fn move_up(&mut self) -> bool {
        self.move_visual_row(|row| row.checked_sub(1))
    }

    /// Moves the cursor to the visual row below, reporting whether there was one.
    pub fn move_down(&mut self) -> bool {
        self.move_visual_row(|row| Some(row + 1))
    }

    pub fn recall_previous(&mut self) -> bool {
        let Some(prompt) = self.history.previous(self.buffer.text()).map(str::to_string) else {
            return false;
        };
        self.set_text(prompt);
        self.buffer.set_cursor(0);
        true
    }

    pub fn recall_next(&mut self) -> bool {
        let Some(prompt) = self.history.next() else {
            return false;
        };
        self.set_text(prompt);
        self.buffer.set_cursor(self.buffer.text().len());
        true
    }

    pub fn open_command_picker(&mut self, commands: Vec<CommandEntry>) {
        self.overlay = Some(Overlay::Completion(CompletionOverlay::command(commands)));
    }

    /// Opens the `@` picker and asks for the file index it will show. The walk
    /// runs off the event loop, so opening the picker never stalls the keystroke
    /// that triggered it.
    pub fn open_file_picker(&mut self, root: &std::path::Path) -> FilesystemCommand {
        let request_id = RequestId::next();
        self.overlay = Some(Overlay::Completion(CompletionOverlay::file(request_id)));
        FilesystemCommand::IndexFiles { request_id, root: root.to_path_buf() }
    }

    pub fn on_files_indexed(&mut self, request_id: RequestId, files: Vec<FileEntry>) {
        if let Some(overlay) = self.completion_mut() {
            overlay.set_files(request_id, files);
        }
    }

    pub fn has_completion(&self) -> bool {
        matches!(self.overlay, Some(Overlay::Completion(_)))
    }

    pub fn completion(&self) -> Option<&CompletionOverlay> {
        match self.overlay.as_ref()? {
            Overlay::Completion(overlay) => Some(overlay),
            Overlay::PromptSearch { .. } => None,
        }
    }

    /// The open completion list, for navigation and stateful rendering.
    pub fn completion_mut(&mut self) -> Option<&mut CompletionOverlay> {
        match self.overlay.as_mut()? {
            Overlay::Completion(overlay) => Some(overlay),
            Overlay::PromptSearch { .. } => None,
        }
    }

    pub fn prompt_search(&self) -> Option<&PromptSearchPicker> {
        match self.overlay.as_ref()? {
            Overlay::PromptSearch { picker, .. } => Some(picker),
            Overlay::Completion(_) => None,
        }
    }

    /// The open prompt-history picker, for queries and stateful rendering.
    pub fn prompt_search_mut(&mut self) -> Option<&mut PromptSearchPicker> {
        match self.overlay.as_mut()? {
            Overlay::PromptSearch { picker, .. } => Some(picker),
            Overlay::Completion(_) => None,
        }
    }

    pub fn has_open_overlay(&self) -> bool {
        self.overlay.is_some()
    }

    /// Routes a mouse event to whichever overlay is open. Browsing history
    /// results previews each candidate in the composer, the way the arrow keys
    /// do.
    pub fn on_overlay_mouse(&mut self, action: MouseAction, row: u16) {
        let direction = action.direction();
        match self.overlay.as_mut() {
            Some(Overlay::Completion(overlay)) => navigate_list(overlay.entries_mut(), direction, row),
            Some(Overlay::PromptSearch { picker, .. }) => {
                navigate_list(picker.results_mut(), direction, row);
                self.apply_selected_search_result();
            }
            None => {}
        }
    }

    pub fn has_prompt_search(&self) -> bool {
        matches!(self.overlay, Some(Overlay::PromptSearch { .. }))
    }

    pub fn open_prompt_search(&mut self) {
        let draft = self.buffer.text().to_string();
        self.overlay = Some(Overlay::PromptSearch { picker: PromptSearchPicker::new(), draft });
    }

    /// Closes the search, restoring the draft it replaced unless the user
    /// confirmed one of the results.
    fn close_prompt_search(&mut self, confirmed: bool) {
        let Some(Overlay::PromptSearch { draft, .. }) = self.overlay.take() else {
            return;
        };
        if !confirmed {
            self.buffer.set_text(draft);
        }
    }

    /// Applies a keystroke to the open history search, or reports that there is
    /// none for it to go to.
    pub fn on_prompt_search_key(&mut self, key: crossterm::event::KeyEvent) -> Option<ComposerOutcome> {
        if !self.has_prompt_search() {
            return None;
        }
        let query = self.prompt_search_query_on_key(key);
        Some(self.search_outcome(query))
    }

    /// Applies a paste to the open history search, or reports that there is none
    /// for it to go to.
    pub fn on_prompt_search_paste(&mut self, text: &str) -> Option<ComposerOutcome> {
        let query = self.prompt_search_mut()?.push_str(text);
        Some(self.search_outcome(Some(query)))
    }

    /// Applies a keystroke to the open completion list, or reports that there is
    /// none for it to go to.
    pub fn on_completion_key(&mut self, key: crossterm::event::KeyEvent) -> Option<ComposerOutcome> {
        if !self.has_completion() {
            return None;
        }
        Some(match self.completion_on_key(key) {
            Some(command) => ComposerOutcome::AcceptedCommand(command),
            None => ComposerOutcome::Handled,
        })
    }

    /// A new query goes to the agent; an emptied one puts back the draft the
    /// search replaced, which is the composer's own business.
    fn search_outcome(&mut self, query: Option<String>) -> ComposerOutcome {
        match query {
            Some(query) if !query.trim().is_empty() => ComposerOutcome::Search(query),
            Some(_) => {
                if let Some(Overlay::PromptSearch { draft, .. }) = &self.overlay {
                    self.buffer.set_text(draft.clone());
                }
                ComposerOutcome::Handled
            }
            None => ComposerOutcome::Handled,
        }
    }

    /// Applies a keystroke to the open history search, returning the query to
    /// re-run when the keystroke changed it.
    fn prompt_search_query_on_key(&mut self, key: crossterm::event::KeyEvent) -> Option<String> {
        let picker = self.prompt_search_mut()?;
        match key.code {
            KeyCode::Esc => {
                self.close_prompt_search(false);
                None
            }
            KeyCode::Enter => {
                let confirmed = picker.selected_result().is_some();
                self.close_prompt_search(confirmed);
                None
            }
            KeyCode::Down => {
                picker.results_mut().step(Direction::Forward);
                self.apply_selected_search_result();
                None
            }
            KeyCode::Up => {
                picker.results_mut().step(Direction::Backward);
                self.apply_selected_search_result();
                None
            }
            KeyCode::Backspace => Some(picker.backspace()),
            KeyCode::Char(c)
                if !key
                    .modifiers
                    .intersects(crossterm::event::KeyModifiers::CONTROL | crossterm::event::KeyModifiers::ALT) =>
            {
                Some(picker.push_char(c))
            }
            _ => None,
        }
    }

    pub fn prompt_search_on_results(&mut self, response: PromptSearchResponse) {
        let Some(picker) = self.prompt_search_mut() else {
            return;
        };
        if picker.on_results(response) {
            self.apply_selected_search_result();
        }
    }

    fn apply_selected_search_result(&mut self) {
        let Some(result) = self.prompt_search().and_then(|picker| picker.selected_result()) else {
            return;
        };
        let prompt = result.prompt.clone();
        let cursor = prompt_search::cursor_at_match_end(&prompt, result.match_end);
        self.buffer.set_text(prompt);
        self.buffer.set_cursor(cursor);
    }

    /// Applies a keystroke to the open completion list, returning the command it
    /// accepted.
    ///
    /// The mirror of [`Composer::on_prompt_search_key`]: the composer owns the
    /// list's own keys and the edits they imply, and the app decides what an
    /// accepted command means.
    fn completion_on_key(&mut self, key: crossterm::event::KeyEvent) -> Option<CommandEntry> {
        match key.code {
            KeyCode::Esc => self.close_overlay(),
            KeyCode::Up => self.step_completion(Direction::Backward),
            KeyCode::Down => self.step_completion(Direction::Forward),
            KeyCode::Enter | KeyCode::Tab => {
                let command = self.accept_command();
                if command.is_none() {
                    self.accept_file();
                }
                return command;
            }
            KeyCode::Backspace if self.completion().is_some_and(|overlay| overlay.query().is_empty()) => {
                self.backspace();
                self.close_overlay();
            }
            KeyCode::Backspace => {
                self.backspace();
                self.refresh_overlay_query();
            }
            KeyCode::Char(character)
                if !key
                    .modifiers
                    .intersects(crossterm::event::KeyModifiers::CONTROL | crossterm::event::KeyModifiers::ALT) =>
            {
                self.insert_char(character);
                if character.is_whitespace() {
                    self.close_overlay();
                } else {
                    self.refresh_overlay_query();
                }
            }
            _ => {}
        }
        None
    }

    fn step_completion(&mut self, direction: Direction) {
        if let Some(overlay) = self.completion_mut() {
            overlay.entries_mut().step(direction);
        }
    }

    fn close_overlay(&mut self) {
        self.overlay = None;
    }

    pub fn accept_command(&mut self) -> Option<CommandEntry> {
        let command = self.completion()?.selected_command()?;
        self.replace_token('/', &format!("/{}", command.name));
        self.overlay = None;
        Some(command)
    }

    pub fn accept_file(&mut self) -> Option<FileEntry> {
        let file = self.completion()?.selected_file()?;
        self.replace_token('@', &format!("@{} ", file.display_name));
        self.mentions.push(SelectedFileMention { path: file.path.clone(), display_name: file.display_name.clone() });
        self.overlay = None;
        Some(file)
    }

    pub fn refresh_overlay_query(&mut self) {
        let Some(trigger) = self.completion().map(CompletionOverlay::trigger) else {
            return;
        };
        let query = self
            .active_token(trigger)
            .map_or_else(String::new, |range| self.buffer.text()[range.start + 1..range.end].to_string());
        if let Some(overlay) = self.completion_mut() {
            overlay.set_query(query);
        }
    }

    /// Moves the cursor to the row `target` picks, keeping its column where that
    /// row is long enough to hold it.
    fn move_visual_row(&mut self, target: impl FnOnce(usize) -> Option<usize>) -> bool {
        let content_width = self.content_width.unwrap_or(usize::MAX);
        let layout = view::input_layout(self.buffer.text(), self.buffer.cursor(), content_width);
        let Some(cursor) =
            target(layout.cursor_row).and_then(|row| layout.byte_at(self.buffer.text(), row, layout.cursor_column))
        else {
            return false;
        };
        self.buffer.set_cursor(cursor);
        true
    }

    pub fn cursor_position(&self) -> (usize, usize) {
        let before = &self.buffer.text()[..self.buffer.cursor()];
        let row = before.matches('\n').count();
        let column = before[self.buffer.line_start()..].width();
        (row, column)
    }

    fn active_token(&self, trigger: char) -> Option<std::ops::Range<usize>> {
        let before_cursor = &self.buffer.text()[..self.buffer.cursor()];
        let start = before_cursor.rfind(trigger)?;
        let before_trigger = &before_cursor[..start];
        (trigger == '/' && start == 0
            || trigger == '@' && (before_trigger.is_empty() || before_trigger.ends_with(char::is_whitespace)))
        .then_some(start..self.buffer.cursor())
    }

    fn replace_token(&mut self, trigger: char, replacement: &str) {
        let Some(range) = self.active_token(trigger) else {
            return;
        };
        self.buffer.replace_range(range, replacement);
    }

    fn set_text(&mut self, text: String) {
        self.buffer.set_text(text);
        self.mentions.clear();
        self.overlay = None;
    }
}

fn navigate_list<T>(list: &mut FilterableList<T>, direction: Option<Direction>, row: u16) {
    match direction {
        Some(direction) => list.step(direction),
        None => {
            list.select_at(row);
        }
    }
}