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
use crate::command::{Command, CommandResult, FailedCommand};
use crate::app::keybindings::Keybindings;
use crate::app::message::Message;
use crate::conversation::items::{Conversation, ConversationItem};
use crate::conversation::progress_indicator::{ProgressIndicator, ProgressPhase};
use crate::conversation::status_line::StatusLineModel;
use crate::session::platform::{
    BrowserOpener, ClipboardWriter, default_browser_opener, default_clipboard_writer,
};
use crate::session::session_config_view::LocalConfigOption;
use crate::session::session_model::SessionModel;
use crate::session::workspace_status::WorkspaceStatus;
use crate::settings::{
    ResolvedStatusLineSettings, SettingsModel, UiSettings, resolve_content_padding, resolve_status_line_settings,
};
use crate::surfaces::composer::Composer;
use crate::surfaces::input::RootOutput;
use crate::surfaces::picker::CommandEntry;
use crate::view::generation::Generation;
use crate::theme::Theme;
use acp_utils::client::AcpEvent;
use acp_utils::notifications::AetherCapabilities;
use agent_client_protocol::schema::v1::{self as acp, SessionId};
use std::collections::VecDeque;
use std::path::PathBuf;
use std::time::Instant;
use tokio::sync::mpsc;

pub mod message;
mod navigation;

pub use crate::session::session_model::WorkspaceMoveState;
pub use navigation::{Overlay, Route};

mod acp_reducer;
mod config;
mod input;
mod keybindings;
mod session;
mod submission;
use config::build_theme_entries;
use input::CTRL_C_CONFIRM_WINDOW;
use session::builtin_commands;
use submission::SubmissionState;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ExitState {
    #[default]
    Idle,
    Confirming(Instant),
    Exiting,
}

impl ExitState {
    fn is_confirming(&self) -> bool {
        matches!(self, ExitState::Confirming(_))
    }
}

/// Root UI state: reduces terminal input and ACP events into the canonical
/// conversation, feature state, and composer that the renderer draws each frame.
pub struct App {
    session: SessionModel,
    ui: UiConfig,
    available_commands: Vec<CommandEntry>,
    route: Route,
    overlay: Option<Overlay>,
    conversation: Conversation,
    composer: Composer,
    exit_state: ExitState,
    /// What the event loop still owes the outside world.
    commands: VecDeque<Command>,
    submission: SubmissionState,
    browser_opener: BrowserOpener,
    clipboard_writer: ClipboardWriter,
}

/// How the UI is configured, as opposed to what it is currently showing.
struct UiConfig {
    settings: SettingsModel,
    keybindings: Keybindings,
    content_padding: usize,
    status_line: ResolvedStatusLineSettings,
    theme: Theme,
    theme_generation: Generation,
}

pub struct AppConfig {
    pub session_id: SessionId,
    pub agent_name: String,
    pub workspace_status: WorkspaceStatus,
    pub prompt_capabilities: acp::PromptCapabilities,
    pub session_capabilities: acp::SessionCapabilities,
    pub config_options: Vec<acp::SessionConfigOption>,
    pub auth_methods: Vec<acp::AuthMethod>,
    pub working_dir: PathBuf,
    pub settings: UiSettings,
    /// Host services the UI reaches for; injected so tests observe URL opens
    /// and clipboard writes without spawning anything.
    pub browser_opener: BrowserOpener,
    pub clipboard_writer: ClipboardWriter,
}

impl App {
    /// Build the UI from a freshly connected ACP session.
    ///
    /// Returns the pieces only the process outside the UI needs: the event
    /// channel feeding the event loop and the handle commands are sent through.
    /// Crate-internal entry-point wiring, not part of the public or test API.
    pub(crate) fn from_session(
        session: crate::session::Session,
        settings: UiSettings,
    ) -> (Self, mpsc::UnboundedReceiver<AcpEvent>, acp_utils::client::AcpPromptHandle) {
        let crate::session::Session {
            session_id,
            agent_name,
            prompt_capabilities,
            session_capabilities,
            config_options,
            auth_methods,
            event_rx,
            prompt_handle,
            working_dir,
            workspace_status,
        } = session;
        let mut app = Self::new(AppConfig {
            session_id,
            agent_name,
            workspace_status,
            prompt_capabilities,
            session_capabilities,
            config_options,
            auth_methods,
            working_dir,
            settings,
            browser_opener: default_browser_opener(),
            clipboard_writer: default_clipboard_writer(),
        });
        app.queue(Command::ResolveWorkspace { cwd: app.session.working_dir().to_path_buf() });
        (app, event_rx, prompt_handle)
    }

    pub fn new(config: AppConfig) -> Self {
        let ui = UiConfig {
            content_padding: resolve_content_padding(&config.settings),
            status_line: resolve_status_line_settings(&config.settings),
            keybindings: Keybindings::from_settings(&config.settings),
            theme: Theme::load(&config.settings),
            theme_generation: Generation::default(),
            settings: SettingsModel::new(config.settings.clone()),
        };
        let capabilities = AetherCapabilities::from_meta(config.session_capabilities.meta.as_ref());
        let initial_commands = builtin_commands(&capabilities);
        let browser_opener = config.browser_opener.clone();
        let clipboard_writer = config.clipboard_writer.clone();
        Self {
            session: SessionModel::from_config(config, capabilities),
            ui,
            available_commands: initial_commands,
            route: Route::Conversation,
            overlay: None,
            conversation: Conversation::default(),
            composer: Composer::new(),
            exit_state: ExitState::Idle,
            commands: VecDeque::new(),
            submission: SubmissionState::default(),
            browser_opener,
            clipboard_writer,
        }
    }

    /// Reduce one external input and return its commands.
    ///
    /// This is the synchronous model boundary used by the runtime dispatcher.
    pub fn update(&mut self, message: Message) -> Vec<Command> {
        match message {
            Message::Terminal(event) => self.on_terminal_event(event),
            Message::Agent(event) => self.on_acp_event(*event),
            Message::CommandFinished(result) => self.on_command_result(result),
            Message::Tick(now) => self.on_tick(now),
        }

        self.refresh_progress();
        self.take_commands()
    }

    pub fn take_commands(&mut self) -> Vec<Command> {
        self.commands.drain(..).collect()
    }

    pub fn on_command_result(&mut self, result: CommandResult) {
        match result {
            CommandResult::FilesIndexed { request_id, files } => self.composer.on_files_indexed(request_id, files),
            CommandResult::GitDiff(event) => {
                let Route::GitReview(screen) = &mut self.route else { return };
                let outputs = screen.on_event(event).into_iter().map(RootOutput::GitReview).collect();
                self.dispatch_outputs(outputs);
            }
            CommandResult::SubmissionPrepared(outcome) => self.finish_submission(outcome),
            CommandResult::ThemesListed(files) => {
                let entries = build_theme_entries(self.ui.settings.ui(), &files);
                if let Some(Overlay::Settings(overlay)) = self.overlay.as_mut() {
                    overlay.upsert_local_entries(entries);
                }
            }
            CommandResult::ThemeApplied { settings, theme, error } => self.finish_theme_change(settings, theme, error),
            CommandResult::WorkspaceResolved { cwd, status } => {
                if self.session.working_dir() == cwd {
                    self.session.set_workspace_status(status);
                }
            }
            CommandResult::PromptSearchFailed { query, error } => {
                if let Some(picker) = self.composer.prompt_search_mut() {
                    picker.on_failed(&query, error);
                }
            }
            CommandResult::Failed { command, error } => self.on_command_failed(command, &error),
        }
    }

    fn on_command_failed(&mut self, command: FailedCommand, error: &str) {
        match command {
            FailedCommand::Prompt => {
                self.conversation.turn_mut().set_prompt_in_flight(false);
                self.conversation.progress_indicator_mut().prompt_finished();
                self.submission.reset();
            }
            FailedCommand::LoadSession => {
                self.session.clear_loads();
                self.session.end_workspace_move();
            }
            FailedCommand::ListWorkspaces | FailedCommand::MoveWorkspace => {
                self.session.end_workspace_move();
            }
            FailedCommand::Other(_) => {}
        }
        self.notify(&format!("Failed to {}: {error}", command.describe()));
    }

    fn queue(&mut self, command: Command) {
        self.commands.push_back(command);
    }

    pub fn on_tick(&mut self, now: Instant) {
        if let ExitState::Confirming(armed_at) = self.exit_state
            && now.duration_since(armed_at) > CTRL_C_CONFIRM_WINDOW
        {
            self.exit_state = ExitState::Idle;
        }
        if self.conversation.progress_indicator().is_active() {
            self.conversation.turn_mut().advance_spinner();
        }
        self.conversation.progress_indicator_mut().on_tick(now);
        self.conversation.plan_tracker_mut().on_tick(now);
    }

    pub fn wants_tick(&self) -> bool {
        self.waiting_for_response()
            || self.conversation.any_running()
            || !self.session.workspace_move_state().is_idle()
            || self.conversation.turn().is_compaction_active()
            || self.conversation.progress_indicator().is_active()
            || self.exit_state.is_confirming()
            || self.conversation.plan_tracker().has_completed_in_grace_period()
    }

    pub fn has_navigation(&self) -> bool {
        self.overlay.is_some() || self.route.is_fullscreen()
    }

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

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

    pub fn full_screen_active(&self) -> bool {
        self.route.is_fullscreen()
    }

    pub fn workspace_move_state(&self) -> WorkspaceMoveState {
        self.session.workspace_move_state()
    }

    pub fn exit_requested(&self) -> bool {
        self.exit_state == ExitState::Exiting
    }

    pub fn conversation_items(&self) -> &[ConversationItem] {
        self.conversation.items()
    }

    pub fn conversation_id(&self) -> crate::conversation::ConversationId {
        self.conversation.id()
    }

    pub fn composer(&self) -> &Composer {
        &self.composer
    }

    pub(crate) fn composer_mut(&mut self) -> &mut Composer {
        &mut self.composer
    }

    pub fn config_options(&self) -> &[LocalConfigOption] {
        self.session.config_options()
    }

    pub fn auth_methods(&self) -> &[acp::AuthMethod] {
        self.session.auth_methods()
    }

    pub(crate) fn content_padding(&self) -> usize {
        self.ui.content_padding
    }

    /// Everything the status line reads, gathered for one frame.
    pub fn status_line_model(&self) -> StatusLineModel<'_> {
        StatusLineModel {
            settings: &self.ui.status_line,
            config_options: self.session.config_options(),
            workspace: self.session.workspace_status(),
            agent_name: self.session.agent_name(),
            content_padding: self.ui.content_padding,
            context_usage: self.conversation.turn().context_usage(),
            unhealthy_servers: self.session.unhealthy_server_count(),
            waiting_for_response: self.waiting_for_response(),
            exit_confirmation: self.exit_state.is_confirming(),
        }
    }

    pub fn ui_settings(&self) -> &UiSettings {
        self.ui.settings.ui()
    }

    /// The active theme; the renderer resyncs when `theme_generation` moves.
    pub fn theme(&self) -> &Theme {
        &self.ui.theme
    }

    pub(crate) fn theme_generation(&self) -> Generation {
        self.ui.theme_generation
    }

    /// A prompt is outstanding, so the agent owes us a reply.
    pub fn waiting_for_response(&self) -> bool {
        self.conversation.turn().is_prompt_in_flight()
    }

    /// Either the prompt or one of its tool calls is still running.
    pub fn is_agent_busy(&self) -> bool {
        self.waiting_for_response() || self.conversation.any_running()
    }

    pub fn progress_indicator(&self) -> &ProgressIndicator {
        self.conversation.progress_indicator()
    }

    /// Test seam: the status line reads this through
    /// [`App::status_line_model`] rather than calling it.
    pub fn exit_confirmation_active(&self) -> bool {
        self.exit_state.is_confirming()
    }

    pub(crate) fn spinner_tick(&self) -> usize {
        self.conversation.turn().spinner_tick()
    }

    pub fn plan_entries(&self) -> Vec<acp::PlanEntry> {
        self.conversation.plan_tracker().current_entries()
    }

    /// Reaches past the renderer for the integration tests, which assert on the
    /// state a frame is drawn from rather than on the frame.
    pub fn has_plan(&self) -> bool {
        self.conversation.plan_tracker().has_entries()
    }

    /// Drops all conversation state atomically before starting a new session.
    fn reset_conversation(&mut self) {
        self.reset_turn_state();
        self.submission.reset();
        self.conversation.clear();
    }

    /// Clears the per-turn indicators that must not survive into a different
    /// conversation. Used on its own when a load lands, because the conversation
    /// was already cleared when that load was requested — and may since have
    /// gained notices the user still needs to see.
    fn reset_turn_state(&mut self) {
        // The spinner phase is cosmetic and survives, so a swap does not make
        // the indicator visibly jump.
        self.conversation.reset_feature_state();
    }

    fn refresh_progress(&mut self) {
        let override_phase = match self.session.workspace_move_state() {
            WorkspaceMoveState::Moving => Some(ProgressPhase::MovingWorkspace),
            WorkspaceMoveState::LoadingSession => Some(ProgressPhase::LoadingSession),
            _ if self.conversation.turn().is_compaction_active() => Some(ProgressPhase::Compacting),
            _ => None,
        };
        let interruptible = self.is_agent_busy();
        self.conversation.progress_indicator_mut().refresh(override_phase, interruptible);
    }

    fn return_to_conversation(&mut self) {
        self.close_overlay();
        self.route = Route::Conversation;
    }
}