-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.rs
More file actions
924 lines (824 loc) · 35.3 KB
/
app.rs
File metadata and controls
924 lines (824 loc) · 35.3 KB
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
use std::io::{self, Stdout};
use std::time::{Duration, Instant};
use std::collections::VecDeque;
use anyhow::{Context, Result};
use crossterm::{
event::{Event, EventStream, KeyCode, KeyEvent, KeyModifiers},
execute,
terminal::{disable_raw_mode, enable_raw_mode},
};
use futures::StreamExt;
use ratatui::{
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
Terminal, TerminalOptions, Viewport,
};
use crate::commands::Command;
use crate::config::Config;
use crate::llm::{Agent, AgentStep, RequestMode};
use crate::tools::{ToolDecision, ToolEvent, ToolExecutor, ToolRegistry};
use crate::tool_filter::ToolFilters;
use crate::transcript::{BlockType, Role, Status, TextBlock, Transcript};
use crate::ide::{Diagnostic, Ide, IdeEvent, Nvim};
use crate::ui::{Attachment, ChatView, InputBox};
const MIN_FRAME_TIME: Duration = Duration::from_millis(16);
pub const APP_NAME: &str = "Codey";
pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CODEY_DIR: &str = ".codey";
pub const TRANSCRIPTS_DIR: &str = "transcripts";
const WELCOME_MESSAGE: &str = "Welcome to Codey! I'm your AI coding assistant. How can I help you today?";
const SYSTEM_PROMPT: &str = r#"You are Codey, an AI coding assistant running in a terminal interface.
## Capabilities
You have access to the following tools:
- `read_file`: Read file contents, optionally with line ranges
- `write_file`: Create new files
- `edit_file`: Make precise edits using search/replace
- `shell`: Execute bash commands
- `fetch_url`: Fetch web content
## Guidelines
### Reading Files
- Always read a file before editing it
- Use line ranges for large files: `read_file(path, start_line=100, end_line=200)`
- Use `shell("ls -la")` to explore directories
- When reading files, be careful about reading large files in one-go. Use line ranges,
or check the file stats with `shell("stat <file_path>")` first.
- shell grep is a great way to get a line number to read a targeted section of a file
### Editing Files
- Use `edit_file` for existing files, `write_file` only for new files
- The `old_string` must match EXACTLY, including whitespace and indentation
- If `old_string` appears multiple times, include more context to make it unique
- Apply edits sequentially; each edit sees the result of previous edits
- UYou can do multiple edits at once, but keep it under 1000 lines
### Shell Commands
- Prefer `read_file` over `cat`, `head`, `tail`
- Use `ls` for directory exploration
- Use `grep` or `rg` for searching code
- `pwd` is an easy way to remind yourself of your current directory
- Only prepend a command with cd <some/dir> && if you really need to change directories
### General
- Be concise but thorough
- Explain what you're doing before executing tools
- If a tool fails, explain the error and suggest fixes
- Ask for clarification if the request is ambiguous
- If you feel like backing out of a path, always get confirmation before git resetting the work tree
- Always get confirmation before making destructive changes (this includes building a release)
"#;
const COMPACTION_PROMPT: &str = r#"The conversation context is getting large and needs to be compacted.
Please provide a comprehensive summary of our conversation so far in markdown format. Include:
1. **What was accomplished** - Main tasks and changes completed as a bulleted list
2. **What still needs to be done** - Remaining tasks or open areas of work as a bulleted list
3. **Key project information** - Important facts about the project that the user has shared or that we're not immediately apparent
4. **Relevant files** - Files most relevant to the current work with brief descriptions, line numbers, or method/variable names
5. **Relevant documentation paths or URLs** - Links to docs or resources we will use to continue our work
6. **Quotes and log snippets** - Any important quotes or logs that th euser provided that we'll need later
Be thorough but concise - this summary will seed a fresh conversation context."#;
/// Result of handling an action
enum ActionResult {
NoOp,
Continue,
Interrupt,
}
/// Input modes determine which keybindings are active
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InputMode {
Normal,
Streaming,
ToolApproval,
}
/// Types of messages that can be processed through the event loop
#[derive(Debug, Clone)]
enum MessageRequest {
/// Regular user message (content, turn_id)
User(String, usize),
/// Compaction request (triggered when context exceeds threshold)
Compaction,
/// Command execution (command_name, turn_id)
Command(String, usize),
}
/// Actions that can be triggered by terminal events
#[derive(Debug, Clone, PartialEq, Eq)]
enum Action {
// Text input
InsertChar(char),
InsertNewline,
DeleteBack,
Paste(String),
// Cursor movement
CursorLeft,
CursorRight,
CursorHome,
CursorEnd,
// Input control
Submit,
ClearInput,
HistoryPrev,
HistoryNext,
TabComplete,
// Application control
Interrupt,
Quit,
Resize(u16, u16),
// Tool approval
ApproveTool,
DenyTool,
ApproveToolSession,
}
/// Map a terminal event to an action based on the current input mode
fn map_event(mode: InputMode, event: Event) -> Option<Action> {
match event {
Event::Key(key) => map_key(mode, key),
Event::Paste(content) => Some(Action::Paste(content)),
Event::Resize(w, h) => Some(Action::Resize(w, h)),
_ => None,
}
}
/// Map a key event to an action based on the current input mode
fn map_key(mode: InputMode, key: KeyEvent) -> Option<Action> {
match mode {
InputMode::Normal => map_key_normal(key),
InputMode::Streaming => map_key_streaming(key),
InputMode::ToolApproval => map_key_tool_approval(key),
}
}
/// Keybindings for normal input mode
fn map_key_normal(key: KeyEvent) -> Option<Action> {
let shift = key.modifiers.contains(KeyModifiers::SHIFT);
let alt = key.modifiers.contains(KeyModifiers::ALT);
if key.modifiers.contains(KeyModifiers::CONTROL) {
return match key.code {
KeyCode::Char('c') => Some(Action::Quit),
_ => None,
};
}
match key.code {
KeyCode::Char(c) => Some(Action::InsertChar(c)),
KeyCode::Backspace => Some(Action::DeleteBack),
KeyCode::Left => Some(Action::CursorLeft),
KeyCode::Right => Some(Action::CursorRight),
KeyCode::Home => Some(Action::CursorHome),
KeyCode::End => Some(Action::CursorEnd),
KeyCode::Enter if shift || alt => Some(Action::InsertNewline),
KeyCode::Enter => Some(Action::Submit),
KeyCode::Esc => Some(Action::ClearInput),
KeyCode::Up => Some(Action::HistoryPrev),
KeyCode::Down => Some(Action::HistoryNext),
KeyCode::Tab => Some(Action::TabComplete),
_ => None,
}
}
/// Keybindings for streaming input mode
fn map_key_streaming(key: KeyEvent) -> Option<Action> {
match key.code {
KeyCode::Esc => Some(Action::Interrupt),
_ => map_key_normal(key),
}
}
/// Keybindings for tool approval mode
fn map_key_tool_approval(key: KeyEvent) -> Option<Action> {
match key.code {
KeyCode::Char('y') | KeyCode::Enter => Some(Action::ApproveTool),
KeyCode::Char('n') | KeyCode::Esc => Some(Action::DenyTool),
KeyCode::Char('a') => Some(Action::ApproveToolSession),
_ => None,
}
}
/// Application state
pub struct App {
config: Config,
terminal: Terminal<CrosstermBackend<Stdout>>,
/// Chat view (owns the transcript)
chat: ChatView,
input: InputBox,
/// Flag to indicate a quit request
should_quit: bool,
continue_session: bool,
/// Queue of messages waiting to be processed
message_queue: VecDeque<MessageRequest>,
/// Last render time for frame rate limiting
last_render: Instant,
/// Alert message to display (cleared on next user input)
alert: Option<String>,
/// Compiled tool parameter filters for auto-approve/deny
tool_filters: ToolFilters,
/// IDE connection for editor integration (e.g., Neovim)
ide: Option<Box<dyn Ide>>,
/// LSP diagnostics from the IDE, keyed by file path
diagnostics: std::collections::HashMap<String, Vec<Diagnostic>>,
/// Terminal event stream
events: EventStream,
/// Current input mode
input_mode: InputMode,
/// LLM agent for conversation
agent: Option<Agent>,
/// Tool executor for managing tool approval and execution
tool_executor: ToolExecutor,
}
impl App {
/// Setup terminal for TUI mode
fn setup_terminal(stdout: &mut Stdout) -> Result<()> {
enable_raw_mode().context("Failed to enable raw mode")?;
execute!(
stdout,
crossterm::terminal::SetTitle(format!("{} v{}", APP_NAME, APP_VERSION)),
crossterm::event::EnableBracketedPaste,
crossterm::event::PushKeyboardEnhancementFlags(
crossterm::event::KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES
| crossterm::event::KeyboardEnhancementFlags::REPORT_EVENT_TYPES
)
)
.context("Failed to setup terminal")?;
Ok(())
}
/// Restore terminal to normal mode
fn restore_terminal(&mut self) -> Result<()> {
disable_raw_mode().context("Failed to disable raw mode")?;
execute!(
self.terminal.backend_mut(),
crossterm::event::DisableBracketedPaste,
crossterm::event::PopKeyboardEnhancementFlags
)
.context("Failed to restore terminal")?;
self.terminal
.show_cursor()
.context("Failed to show cursor")?;
Ok(())
}
/// Create a new application
pub async fn new(config: Config, continue_session: bool) -> Result<Self> {
// Okay so in tracing down trying to get the viewport to line up with the
// scroll, it looks like we need to subtract the height of the input from
// the viewport in order to get the height of the chat view
// TODO All of those dimensions are in the draw method
let viewport_height: u16 = 12;
let chat_height = viewport_height.saturating_sub(5) as usize;
let mut stdout = io::stdout();
Self::setup_terminal(&mut stdout)?;
let terminal_size = crossterm::terminal::size()?;
let backend = CrosstermBackend::new(stdout);
// Use inline viewport for native scrollback support
let terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(viewport_height),
}
).context("Failed to create terminal")?;
// Load existing transcript or create new one
let transcript = if continue_session {
Transcript::load()
.context("Failed to load transcript")?
} else {
Transcript::new_numbered()
.context("Failed to create new transcript")?
};
// Compile tool filters from config
let tool_filters = ToolFilters::compile(&config.tools.filters())
.context("Failed to compile tool filters")?;
// Try to connect to neovim if enabled
let ide: Option<Box<dyn Ide>> = if config.ide.nvim.enabled {
match Nvim::discover(&config.ide.nvim).await {
Ok(Some(nvim)) => {
tracing::info!("Connected to {} at {:?}", nvim.name(), nvim.socket_path());
Some(Box::new(nvim))
}
Ok(None) => {
tracing::debug!("No nvim instance found");
None
}
Err(e) => {
tracing::warn!("Failed to connect to nvim: {}", e);
None
}
}
} else {
None
};
Ok(Self {
config,
terminal,
chat: ChatView::new(transcript, terminal_size.0, chat_height),
input: InputBox::new(),
should_quit: false,
continue_session,
message_queue: VecDeque::new(),
last_render: Instant::now(),
alert: None,
tool_filters,
ide,
diagnostics: std::collections::HashMap::new(),
events: EventStream::new(),
input_mode: InputMode::Normal,
agent: None,
tool_executor: ToolExecutor::new(ToolRegistry::new()),
})
}
/// Run the main event loop - purely event-driven rendering
pub async fn run(&mut self) -> Result<()> {
let oauth = crate::auth::OAuthCredentials::load()
.ok()
.flatten();
let mut agent = Agent::new(
self.config.general.clone(),
SYSTEM_PROMPT,
oauth,
);
if self.continue_session {
agent.restore_from_transcript(&self.chat.transcript);
} else {
self.chat.add_turn(Role::Assistant, TextBlock::pending(WELCOME_MESSAGE));
}
self.agent = Some(agent);
// Initial render - populate hot zone from transcript
self.chat.render(&mut self.terminal)?;
self.draw()?;
// CANCEL-SAFETY: When one branch of tokio::select! completes, all other
// futures are dropped (not paused). Any async fn polled here must store
// its state on `self`, not in local variables, so it can resume correctly
// when a new future is created on the next loop iteration.
loop {
tokio::select! {
biased;
// Handle terminal events with highest priority (keystrokes, resize, paste)
Some(term_event) = self.events.next() => {
self.handle_term_event(term_event).await?;
}
// Handle IDE events (selection changes)
Some(ide_event) = async { self.ide.as_mut()?.next().await } => {
self.handle_ide_event(ide_event);
}
// Handle agent steps (streaming responses, tool requests)
Some(agent_step) = async { self.agent.as_mut()?.next().await } => {
self.handle_agent_step(agent_step).await?;
}
// Handle tool executor events (tool output, completion)
Some(tool_event) = self.tool_executor.next() => {
self.handle_tool_event(tool_event).await?;
}
// Handle queued messages when in normal input mode
Some(request) = async { self.message_queue.pop_front() }, if self.input_mode == InputMode::Normal => {
self.handle_message(request).await?;
}
}
if self.should_quit {
break;
}
}
self.restore_terminal()
}
/// Draw the UI
fn draw(&mut self) -> Result<()> {
use ratatui::style::{Color, Style};
use ratatui::widgets::Paragraph;
self.last_render = Instant::now();
// Calculate dimensions
let size = self.terminal.size()?;
let input_height = self.input.required_height(size.width);
let max_input_height = size.height / 2;
let input_height = input_height.min(max_input_height).max(5);
// Draw the viewport (hot zone content + input)
let chat_widget = self.chat.widget();
let input_widget = self.input.widget(
&self.config.general.model,
self.agent.as_ref().map_or(0, |a| a.total_usage().context_tokens),
);
let alert = self.alert.clone();
self.terminal.draw(|frame| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(5), // Chat area (minimum)
Constraint::Length(input_height), // Input area (dynamic)
Constraint::Length(if alert.is_some() { 1 } else { 0 }),
])
.split(frame.area());
frame.render_widget(chat_widget, chunks[0]);
frame.render_widget(input_widget, chunks[1]);
// TODO build as an actual widget on self.alert
if let Some(ref msg) = alert {
let alert_widget = Paragraph::new(msg.as_str())
.style(Style::default().fg(Color::Red));
frame.render_widget(alert_widget, chunks[2]);
}
})?;
Ok(())
}
/// Draw with frame rate limiting - skips if called too frequently
/// Returns true if a draw actually occurred
fn draw_throttled(&mut self) -> Result<bool> {
if self.last_render.elapsed() < MIN_FRAME_TIME {
return Ok(false);
}
self.draw()?;
Ok(true)
}
/// Handle an action. Returns the result indicating what the main loop should do.
async fn handle_action(&mut self, action: Action) -> ActionResult {
// Clear alert on any input action
self.alert = None;
if !matches!(action, Action::InsertChar(_)) {
tracing::debug!("Action received: {:?}", action);
}
match action {
Action::Interrupt => {
return ActionResult::Interrupt;
},
Action::Quit => {
self.should_quit = true;
return ActionResult::Interrupt;
}
Action::ApproveTool => {
self.decide_pending_tool(ToolDecision::Approve).await;
}
Action::DenyTool => {
self.decide_pending_tool(ToolDecision::Deny).await;
}
Action::ApproveToolSession => {
// TODO: implement allow for session
self.decide_pending_tool(ToolDecision::Approve).await;
}
Action::InsertChar(c) => self.input.insert_char(c),
Action::InsertNewline => self.input.insert_newline(),
Action::DeleteBack => self.input.delete_char(),
Action::Paste(content) => {
self.input.add_attachment(Attachment::pasted(content));
}
Action::CursorLeft => self.input.move_cursor_left(),
Action::CursorRight => self.input.move_cursor_right(),
Action::CursorHome => self.input.move_cursor_start(),
Action::CursorEnd => self.input.move_cursor_end(),
Action::Submit => {
let content = self.input.submit();
if !content.trim().is_empty() {
self.queue_message(content);
}
}
Action::ClearInput => self.input.clear(),
Action::HistoryPrev => {
self.input.history_prev();
}
Action::HistoryNext => {
self.input.history_next();
}
Action::TabComplete => {
if let Some(completed) = Command::complete(&self.input.content()) {
self.input.set_content(&completed);
}
}
Action::Resize(_w, _h) => {
// TODO trigger redraw with new dimensions
return ActionResult::NoOp;
}
}
ActionResult::Continue
}
/// Queue a user message or command for processing
fn queue_message(&mut self, content: String) {
let message = match Command::parse(&content) {
Some(command) => {
// Slash command
let name = command.name().to_string();
let turn_id = self.chat.add_turn(
Role::User,
TextBlock::pending(format!("/{}", name)));
MessageRequest::Command(name, turn_id)
},
None => {
// Regular user message
let turn_id = self.chat.add_turn(
Role::User,
TextBlock::pending(&content));
MessageRequest::User(content, turn_id)
},
};
self.message_queue.push_back(message);
self.chat.render(&mut self.terminal)
.expect("Failed to render chat");
self.draw()
.expect("Failed to draw terminal after queuing message");
}
/// Queue a compaction request
pub fn queue_compaction(&mut self) {
// TODO push_front?
self.message_queue.push_back(MessageRequest::Compaction);
}
/// Cancel the current agent request and reset input mode
fn cancel(&mut self) -> Result<()> {
if let Some(agent) = self.agent.as_mut() {
agent.cancel();
}
self.chat.finish_turn(&mut self.terminal)?;
self.input_mode = InputMode::Normal;
Ok(())
}
/// Handle a terminal event
async fn handle_term_event(&mut self, event: std::io::Result<Event>) -> Result<()> {
let event = match event {
Ok(e) => e,
Err(e) => {
tracing::warn!("Event stream error: {}", e);
return Ok(());
}
};
let Some(action) = map_event(self.input_mode, event) else {
return Ok(());
};
match self.handle_action(action).await {
ActionResult::Interrupt => { self.cancel()?; }
ActionResult::Continue => { self.draw_throttled()?; }
ActionResult::NoOp => {}
}
Ok(())
}
/// Handle an IDE event (selection changes, diagnostics, etc.)
fn handle_ide_event(&mut self, event: IdeEvent) {
match event {
IdeEvent::SelectionChanged(selection) => {
let attachment = selection.map(|sel| {
let path = std::env::current_dir()
.ok()
.and_then(|cwd| std::path::Path::new(&sel.path).strip_prefix(cwd).ok())
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or(sel.path);
Attachment::ide_selection(path, sel.content, sel.start_line, sel.end_line)
});
self.input.set_ide_selection(attachment);
let _ = self.draw_throttled();
}
IdeEvent::Diagnostics(diagnostics) => {
// Group diagnostics by path and store them
if let Some(first) = diagnostics.first() {
let path = first.path.clone();
let rel_path = std::env::current_dir()
.ok()
.and_then(|cwd| std::path::Path::new(&path).strip_prefix(cwd).ok())
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or(path);
if diagnostics.is_empty() {
self.diagnostics.remove(&rel_path);
} else {
tracing::debug!(
"Received {} diagnostics for {}",
diagnostics.len(),
rel_path
);
self.diagnostics.insert(rel_path, diagnostics);
}
}
}
}
}
/// Start processing a message request
async fn handle_message(&mut self, request: MessageRequest) -> Result<()> {
if let Some(agent) = self.agent.as_mut() {
if let Err(e) = agent.refresh_oauth_if_needed().await {
tracing::warn!("Failed to refresh OAuth token: {}", e);
}
}
match request {
MessageRequest::Command(name, turn_id) => {
self.chat.mark_last_block_complete(turn_id);
if let Some(command) = Command::get(&name) {
match command.execute(self) {
Ok(None) => {
// Command executed, no output - still need to render
self.chat.render(&mut self.terminal)?;
self.draw()?;
}
Ok(Some(output)) => {
let idx = self.chat.transcript.add_empty(Role::Assistant);
if let Some(turn) = self.chat.transcript.get_mut(idx) {
turn.start_block(Box::new(TextBlock::complete(&output)));
}
self.chat.render(&mut self.terminal)?;
self.draw()?;
}
Err(e) => {
tracing::error!("Command execution error: {}", e);
self.alert = Some(format!("Command error: {}", e));
}
}
}
}
MessageRequest::User(content, turn_id) => {
self.chat.mark_last_block_complete(turn_id);
self.chat.render(&mut self.terminal)?;
self.draw()?;
if let Some(agent) = self.agent.as_mut() {
agent.send_request(&content, RequestMode::Normal);
}
self.chat.begin_turn(Role::Assistant, &mut self.terminal)?;
self.input_mode = InputMode::Streaming;
}
MessageRequest::Compaction => {
if let Some(agent) = self.agent.as_mut() {
agent.send_request(COMPACTION_PROMPT, RequestMode::Compaction);
}
self.chat.begin_turn(Role::Assistant, &mut self.terminal)?;
self.input_mode = InputMode::Streaming;
}
}
Ok(())
}
/// Handle a single agent step during streaming
async fn handle_agent_step(&mut self, step: AgentStep) -> Result<()> {
match step {
AgentStep::TextDelta(text) => {
self.chat.transcript.stream_delta(BlockType::Text, &text);
}
AgentStep::CompactionDelta(text) => {
self.chat.transcript.stream_delta(BlockType::Compaction, &text);
}
AgentStep::ThinkingDelta(text) => {
self.chat.transcript.stream_delta(BlockType::Thinking, &text);
}
AgentStep::ToolRequest(tool_calls) => {
// Just enqueue - the select loop will poll tool_executor.next()
self.tool_executor.enqueue(tool_calls);
}
AgentStep::Retrying { attempt, error } => {
self.alert = Some(format!("Request failed (attempt {}): {}. Retrying...", attempt, error));
tracing::warn!("Retrying request: attempt {}, error: {}", attempt, error);
}
AgentStep::Finished { usage } => {
self.input_mode = InputMode::Normal;
// Handle compaction completion
// TODO something more robust than checking active block type
if self.chat.transcript.is_streaming_block_type(BlockType::Compaction) {
self.chat.transcript.finish_turn();
if let Err(e) = self.chat.transcript.save() {
tracing::error!("Failed to save transcript before compaction: {}", e);
}
match self.chat.transcript.rotate() {
Ok(new_transcript) => {
tracing::info!("Compaction complete, rotating to {:?}", new_transcript.path());
self.chat.reset_transcript(new_transcript, &mut self.terminal)?;
self.draw()?;
}
Err(e) => {
tracing::error!("Failed to rotate transcript: {}", e);
}
}
} else {
// Normal completion
self.chat.transcript.finish_turn();
if let Err(e) = self.chat.transcript.save() {
tracing::error!("Failed to save transcript: {}", e);
}
// Check if compaction is needed
if usage.context_tokens >= self.config.general.compaction_threshold {
self.queue_compaction();
}
}
}
AgentStep::Error(msg) => {
self.chat.transcript.mark_active_block(Status::Error);
self.input_mode = InputMode::Normal;
let alert_msg = if let Some(start) = msg.find('{') {
serde_json::from_str::<serde_json::Value>(&msg[start..])
.ok()
.and_then(|json| json["error"]["message"].as_str().map(String::from))
.unwrap_or_else(|| msg.clone())
} else {
msg.clone()
};
self.alert = Some(alert_msg);
}
}
// Update display
self.chat.render(&mut self.terminal)?;
self.draw_throttled()?;
Ok(())
}
/// Execute a tool decision (approve/deny) for the current tool
async fn decide_pending_tool(&mut self, decision: ToolDecision) {
// Get current tool call_id
// TODO I'd prefer to reference the call_id explictly
let call_id = match self.tool_executor.front() {
Some(tc) => tc.call_id.clone(),
None => {
tracing::warn!("No current tool to execute decision for");
return;
}
};
// Close IDE preview
// TODO: Would be nice to know if the tool actually had an IDE preview open
if let Some(ide) = &self.ide {
if let Err(e) = ide.close_preview().await {
tracing::warn!("Failed to close IDE preview: {}", e);
}
}
// Update block status to Running/Denied
self.chat.transcript.mark_active_block(match decision {
ToolDecision::Approve => Status::Running,
ToolDecision::Deny => Status::Denied,
_ => unreachable!(),
});
let _ = self.chat.render(&mut self.terminal);
let _ = self.draw();
// Mark decision on the tool - next poll will execute it
self.tool_executor.decide(&call_id, decision);
}
/// Handle events from the tool executor
async fn handle_tool_event(&mut self, event: ToolEvent) -> Result<()> {
match event {
ToolEvent::AwaitingApproval(tool_call) => {
// Display tool in transcript
self.draw()?; // there's sometimes a missing token otherwise
let tool = self.tool_executor.tools().get(&tool_call.name);
self.chat.start_block(
tool.create_block(&tool_call.call_id, tool_call.params.clone()),
&mut self.terminal)?;
// Show preview in IDE if the tool provides one
if let Some(preview) = tool.ide_preview(&tool_call.params) {
if let Some(ide) = &self.ide {
if let Err(e) = ide.show_preview(&preview).await {
tracing::warn!("Failed to show IDE preview: {}", e);
}
}
}
self.draw()?;
match self.tool_filters.evaluate(&tool_call.name, &tool_call.params) {
Some(decision) => {
// Auto-approve/deny based on filters
self.decide_pending_tool(decision).await;
}
None => {
// Ask user for approval
self.input_mode = InputMode::ToolApproval;
}
}
}
ToolEvent::OutputDelta { call_id, delta } => {
// Stream output to the active tool block
if let Some(block) = self.chat.transcript.find_tool_block_mut(&call_id) {
block.append_text(&delta);
// Re-render to show the delta
self.chat.render(&mut self.terminal)?;
self.draw()?;
} else {
tracing::warn!("No block found for call_id: {}", call_id);
}
}
ToolEvent::Completed { call_id, content, is_error, ide_post_actions } => {
// Update transcript status (content was already streamed via OutputDelta)
self.chat.transcript.mark_active_block(
if is_error { Status::Error } else { Status::Complete }
);
// Execute post-actions from the tool, collecting diagnostics
let mut final_content = content;
for action in ide_post_actions {
if let Some(ide) = &self.ide {
match &action {
crate::ide::IdeAction::ReloadBuffer(path) => {
// Reload and get LSP diagnostics
match ide.reload_and_get_diagnostics(path).await {
Ok(diagnostics) => {
// Filter to errors and warnings only
let important: Vec<_> = diagnostics.iter()
.filter(|d| matches!(d.severity,
crate::ide::DiagnosticSeverity::Error |
crate::ide::DiagnosticSeverity::Warning))
.collect();
if !important.is_empty() {
final_content.push_str("\n\nLSP Diagnostics:\n");
for d in important {
let source = d.source.as_deref().unwrap_or("lsp");
final_content.push_str(&format!(
" line {}: [{}] {}: {}\n",
d.line, source, d.severity, d.message
));
}
}
}
Err(e) => {
tracing::debug!("Failed to get diagnostics: {}", e);
}
}
}
_ => {
if let Err(e) = ide.execute(&action).await {
tracing::warn!("Failed to execute IDE action: {}", e);
}
}
}
}
}
// Tell agent about the result (with diagnostics appended if any)
if let Some(agent) = self.agent.as_mut() {
agent.submit_tool_result(&call_id, final_content);
}
// Tool is done - go back to streaming mode.
// If there's another tool pending, AwaitingApproval will switch back.
self.input_mode = InputMode::Streaming;
// Render update
self.chat.render(&mut self.terminal)?;
self.draw()?;
}
}
Ok(())
}
}
impl Drop for App {
fn drop(&mut self) {
let _ = self.restore_terminal();
}
}