rho-coding-agent 3.4.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
618
619
620
621
622
//! The surfaces in the running TUI: showing what `ui` calls carry, moving
//! each surface from its live card to the transcript, and the keyboard.

use std::{collections::HashMap, sync::Arc};

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use rho_a2ui::{Action, Effect, Pointer, RendererMessage};
use rho_sdk::ToolCallId;
use serde_json::Value;

use super::{
    focus::{is_focus_chord, EditKey, NavKey},
    tree::{Control, FieldKind, Focusable, SurfaceTree, Target},
    Editing, Keyboard, SurfaceEntry, SurfaceFocus, SurfaceView, Surfaces,
};
use crate::tui::{line_editor::LineEditor, tool_call_batch::LiveToolKey, App, ComposerMode, Entry};

impl App {
    /// Shows the surface a `ui` call carries. Every update of the call
    /// repeats it; only the first is applied.
    pub(in crate::tui) fn show_surface(
        &mut self,
        call: ToolCallId,
        surface: String,
        messages: &[Value],
        replayed: bool,
    ) {
        if !self.surfaces.store.apply(&call, &surface, messages) {
            return;
        }
        let entry = SurfaceEntry {
            call,
            surface,
            view: None,
        };
        if self.is_live_call(&entry.call) {
            // A call that shows its surface while still open waits for the
            // user to act on it, so the keyboard goes there unless the user
            // is busy in the composer. A replay shows every call open
            // first, whether or not it waited.
            let composer_free = matches!(self.input_ui.composer(), ComposerMode::Input)
                && !self.input_ui.has_pending_draft();
            if !replayed && composer_free && !self.surfaces.has_keyboard() {
                self.surfaces.focus = SurfaceFocus::Navigating {
                    surface: entry.surface.clone(),
                    index: 0,
                };
            }
            self.surfaces.live.push(entry);
        } else {
            self.insert_entry(&Entry::Surface(entry));
        }
        self.sync_surfaces();
    }

    /// Moves the surface of each call that left the live tool batch to
    /// the transcript, where the call's card just went.
    pub(in crate::tui) fn settle_surfaces(&mut self) {
        let (live, settled) = std::mem::take(&mut self.surfaces.live)
            .into_iter()
            .partition(|entry| self.is_live_call(&entry.call));
        self.surfaces.live = live;
        let settled: Vec<SurfaceEntry> = settled;
        for entry in settled {
            self.insert_entry(&Entry::Surface(entry));
        }
    }

    /// Forgets every surface, as a reset replay starts the session over.
    pub(in crate::tui) fn clear_surfaces(&mut self) {
        self.surfaces = Surfaces::default();
    }

    fn is_live_call(&self, call: &ToolCallId) -> bool {
        self.turn
            .tool_calls()
            .live_cards()
            .any(|(key, _)| matches!(&key, LiveToolKey::Running(running) if running == call))
    }

    /// Takes a new snapshot of each surface for the entry that draws it,
    /// after anything that changes one: messages, an edit, the keyboard
    /// moving. Entries whose snapshot is unchanged keep their lines.
    fn sync_surfaces(&mut self) {
        let mut trees: HashMap<String, Option<Arc<SurfaceTree>>> = HashMap::new();
        let store = &self.surfaces.store;
        let mut tree_of = |surface: &str| {
            trees
                .entry(surface.to_owned())
                .or_insert_with(|| store.tree(surface).map(Arc::new))
                .clone()
        };
        // The keyboard stays on a surface only while it has that control.
        if let Some((surface, index)) = self.surfaces.focus.place() {
            let controls = tree_of(surface).map_or(0, |tree| tree.focusables.len());
            if index >= controls {
                self.surfaces.focus = SurfaceFocus::Idle;
            }
        }
        let reachable = store
            .latest_first()
            .find(|surface| tree_of(surface).is_some_and(|tree| !tree.focusables.is_empty()))
            .map(str::to_owned);
        let focus = &self.surfaces.focus;
        let mut view_of = |entry: &SurfaceEntry| {
            if store.drawn_under(&entry.surface) != Some(&entry.call) {
                return None;
            }
            let keyboard = match focus {
                SurfaceFocus::Navigating { surface, index } if *surface == entry.surface => {
                    Keyboard::On {
                        index: *index,
                        editing: None,
                    }
                }
                SurfaceFocus::Editing {
                    surface,
                    index,
                    editor,
                    invalid,
                } if *surface == entry.surface => Keyboard::On {
                    index: *index,
                    editing: Some(Editing {
                        text: editor.value.clone(),
                        cursor: editor.cursor,
                        invalid: *invalid,
                    }),
                },
                SurfaceFocus::Idle
                | SurfaceFocus::Navigating { .. }
                | SurfaceFocus::Editing { .. } => Keyboard::Away {
                    reachable: reachable.as_deref() == Some(entry.surface.as_str()),
                },
            };
            Some(SurfaceView {
                tree: tree_of(&entry.surface)?,
                keyboard,
            })
        };
        for entry in &mut self.surfaces.live {
            entry.view = view_of(entry);
        }
        let mut redraw = Vec::new();
        let mut rebuild_from = None;
        for (index, entry) in self.history.entries_mut().iter_mut().enumerate() {
            let Entry::Surface(entry) = entry else {
                continue;
            };
            let view = view_of(entry);
            if entry.view == view {
                continue;
            }
            // An entry that starts or stops drawing changes which rows
            // exist, which only a rebuild from there accounts for.
            if entry.view.is_some() == view.is_some() {
                redraw.push(index);
            } else {
                rebuild_from.get_or_insert(index);
            }
            entry.view = view;
        }
        if let Some(index) = rebuild_from {
            self.history.invalidate_from(index);
        }
        self.history.lines_mut().resplice_entries(redraw);
    }

    /// Moves the keyboard from the composer to the latest surface that
    /// has a control, if `key` is the chord for it.
    pub(in crate::tui) fn handle_surface_focus_chord(&mut self, key: KeyEvent) -> bool {
        if !is_focus_chord(key) {
            return false;
        }
        let store = &self.surfaces.store;
        let surface = store
            .latest_first()
            .find(|surface| {
                store
                    .tree(surface)
                    .is_some_and(|tree| !tree.focusables.is_empty())
            })
            .map(str::to_owned);
        match surface {
            Some(surface) => {
                self.surfaces.focus = SurfaceFocus::Navigating { surface, index: 0 };
                self.sync_surfaces();
                // The latest surface is near the end of the transcript.
                self.scroll_history_to_bottom();
            }
            None => self.notify_status("no surface to interact with"),
        }
        true
    }

    /// Pastes `text` into the field being edited, keeping what typing
    /// would keep. While the keyboard is on a surface but not in a field,
    /// the paste is dropped rather than reaching the composer behind it.
    pub(in crate::tui) fn paste_into_surface(&mut self, text: &str) -> bool {
        if !matches!(self.input_ui.composer(), ComposerMode::Input) {
            return false;
        }
        let Some((surface, index)) = self.surfaces.focus.place() else {
            return false;
        };
        let kind = self.surfaces.store.tree(surface).and_then(|tree| {
            match tree.focusables.get(index)?.control {
                Control::Field { kind, .. } => Some(kind),
                _ => None,
            }
        });
        let (Some(kind), SurfaceFocus::Editing { editor, .. }) = (kind, &mut self.surfaces.focus)
        else {
            return true;
        };
        let text: String = text
            .chars()
            .filter(|&ch| match ch {
                '\n' => kind == FieldKind::Long,
                _ => !ch.is_control() && kind.accepts(ch),
            })
            .collect();
        editor.insert_text(&text);
        self.write_edited_field();
        self.sync_surfaces();
        true
    }

    /// Handles `key` while the keyboard is on a surface. Of the keys the
    /// surface has no use for, chords such as Ctrl+C go on to the rest
    /// of the TUI, and the others are dropped, so none edits the
    /// composer's draft behind the surface.
    pub(in crate::tui) async fn handle_surface_key(&mut self, key: KeyEvent) -> bool {
        if !matches!(self.input_ui.composer(), ComposerMode::Input) {
            return false;
        }
        let Some((surface, index)) = self.surfaces.focus.place() else {
            return false;
        };
        let surface = surface.to_owned();
        let Some(Focusable { target, control }) =
            self.surfaces.store.tree(&surface).and_then(|mut tree| {
                (index < tree.focusables.len()).then(|| tree.focusables.remove(index))
            })
        else {
            self.surfaces.focus = SurfaceFocus::Idle;
            self.sync_surfaces();
            return false;
        };
        let handled = if matches!(self.surfaces.focus, SurfaceFocus::Editing { .. }) {
            self.edit_field(key, &control)
        } else {
            match NavKey::of(key) {
                Some(nav) => {
                    self.navigate(nav, &surface, index, &target, control).await;
                    true
                }
                None => false,
            }
        };
        if handled {
            self.input_ui.clear_paste_burst();
            self.ctrl_c_streak = 0;
            self.sync_surfaces();
            return true;
        }
        // The composer is behind the surface, so a key the surface has no
        // use for must not edit its draft. Chords the rest of the TUI owns
        // still pass.
        !self.passes_surface(key)
    }

    /// Whether `key`, which the surface did not use, goes on to the rest
    /// of the TUI: Ctrl+C, or a chord that acts on the app, not on the
    /// composer's draft. An allow-list, so a chord that edits the draft,
    /// such as undo or paste image, cannot reach it by being left off.
    fn passes_surface(&self, key: KeyEvent) -> bool {
        let bindings = &self.info.runtime.keybindings;
        (key.modifiers == KeyModifiers::CONTROL && key.code == KeyCode::Char('c'))
            || bindings.search_transcript.matches(key)
            || bindings.jump_to_bottom.matches(key)
            || bindings.toggle_tool_output.matches(key)
            || bindings.cycle_streaming_mode.matches(key)
            || bindings.cycle_permission_mode_matches(key)
            || bindings.cycle_pinned_model.matches(key)
            || bindings.cycle_pinned_model_back.matches(key)
    }

    fn edit_field(&mut self, key: KeyEvent, control: &Control) -> bool {
        let newline = self.info.runtime.keybindings.insert_newline.matches(key);
        let SurfaceFocus::Editing {
            surface,
            index,
            editor,
            ..
        } = &mut self.surfaces.focus
        else {
            return false;
        };
        let kind = match control {
            Control::Field { kind, .. } => *kind,
            _ => FieldKind::Short,
        };
        let outcome = if newline && kind == FieldKind::Long {
            let at = editor
                .value
                .char_indices()
                .nth(editor.cursor)
                .map_or(editor.value.len(), |(at, _)| at);
            editor.value.insert(at, '\n');
            editor.cursor += 1;
            Some(EditKey::Edited)
        } else {
            EditKey::apply(key, editor, |ch| kind.accepts(ch))
        };
        match outcome {
            None => return false,
            Some(EditKey::Edited) => self.write_edited_field(),
            Some(EditKey::Done) => {
                self.surfaces.focus = SurfaceFocus::Navigating {
                    surface: std::mem::take(surface),
                    index: *index,
                };
            }
            Some(EditKey::DoneThen(nav)) => {
                let (surface, index) = (std::mem::take(surface), *index);
                self.surfaces.focus = SurfaceFocus::Navigating {
                    surface: surface.clone(),
                    index,
                };
                self.move_focus(&surface, index, nav == NavKey::Next);
            }
        }
        true
    }

    /// Writes the text being edited to the field's place in the data
    /// model, so checks and bindings follow each keystroke. Text the
    /// field's kind does not allow is kept out of the model.
    fn write_edited_field(&mut self) {
        let SurfaceFocus::Editing {
            surface,
            index,
            editor,
            invalid,
        } = &mut self.surfaces.focus
        else {
            return;
        };
        let Some(Control::Field { binding, kind }) = self
            .surfaces
            .store
            .tree(surface)
            .and_then(|mut tree| {
                (*index < tree.focusables.len()).then(|| tree.focusables.remove(*index))
            })
            .map(|focusable| focusable.control)
        else {
            return;
        };
        *invalid = kind.problem(&editor.value);
        if invalid.is_none() {
            let value = Value::String(editor.value.clone());
            self.surfaces.store.write(surface, &binding, value);
        }
    }

    async fn navigate(
        &mut self,
        nav: NavKey,
        surface: &str,
        index: usize,
        target: &Target,
        control: Control,
    ) {
        match (nav, control) {
            (NavKey::Leave, _) => self.surfaces.focus = SurfaceFocus::Idle,
            (NavKey::Next, _) => self.move_focus(surface, index, /*forward*/ true),
            (NavKey::Previous, _) => self.move_focus(surface, index, /*forward*/ false),
            (
                NavKey::Left | NavKey::Right,
                Control::Slider {
                    binding,
                    min,
                    max,
                    step,
                },
            ) => {
                let value = self.read_number(surface, &binding).unwrap_or(min);
                let step = if nav == NavKey::Right { step } else { -step };
                let value = (value + step).clamp(min, max);
                self.surfaces
                    .store
                    .write(surface, &binding, number_value(value));
            }
            (NavKey::Left | NavKey::Right | NavKey::Activate, Control::Tabs { count })
                if count > 0 =>
            {
                let active = self.surfaces.store.local(surface, target).min(count - 1);
                let next = if nav == NavKey::Left {
                    (active + count - 1) % count
                } else {
                    (active + 1) % count
                };
                self.surfaces.store.set_local(surface, target, next);
            }
            (NavKey::Left, _) => self.move_focus(surface, index, /*forward*/ false),
            (NavKey::Right, _) => self.move_focus(surface, index, /*forward*/ true),
            (
                NavKey::Activate,
                Control::Button {
                    action: Some(action),
                },
            ) => {
                self.run_surface_action(surface, target, &action).await;
            }
            (NavKey::Activate, Control::Button { action: None }) => {
                self.notify_status("the button is disabled until its checks pass");
            }
            (NavKey::Activate, Control::Modal) => {
                let open = self.surfaces.store.local(surface, target) != 0;
                self.surfaces
                    .store
                    .set_local(surface, target, usize::from(!open));
            }
            (NavKey::Activate, Control::CheckBox { binding }) => {
                let checked =
                    self.surfaces.store.read(surface, &binding) == Some(&Value::Bool(true));
                self.surfaces
                    .store
                    .write(surface, &binding, Value::Bool(!checked));
            }
            (
                NavKey::Activate,
                Control::Choice {
                    binding,
                    value,
                    multiple,
                },
            ) => {
                let mut selected: Vec<Value> = self
                    .surfaces
                    .store
                    .read(surface, &binding)
                    .and_then(Value::as_array)
                    .cloned()
                    .unwrap_or_default();
                let value = Value::String(value);
                let was_selected = selected.contains(&value);
                if !multiple {
                    selected = vec![value];
                } else if was_selected {
                    selected.retain(|other| *other != value);
                } else {
                    selected.push(value);
                }
                self.surfaces
                    .store
                    .write(surface, &binding, Value::Array(selected));
            }
            (NavKey::Activate, Control::Field { binding, .. }) => {
                self.start_editing(surface, index, &binding, None);
            }
            (NavKey::Type(ch), Control::Field { binding, kind }) if kind.accepts(ch) => {
                self.start_editing(surface, index, &binding, Some(ch));
            }
            (NavKey::Activate, Control::Slider { .. } | Control::Tabs { .. })
            | (NavKey::Type(_), _) => {}
        }
    }

    fn move_focus(&mut self, surface: &str, index: usize, forward: bool) {
        let controls = self
            .surfaces
            .store
            .tree(surface)
            .map_or(0, |tree| tree.focusables.len());
        if controls == 0 {
            return;
        }
        let index = if forward {
            (index + 1) % controls
        } else {
            (index + controls - 1) % controls
        };
        self.surfaces.focus = SurfaceFocus::Navigating {
            surface: surface.to_owned(),
            index,
        };
    }

    fn start_editing(
        &mut self,
        surface: &str,
        index: usize,
        binding: &Pointer,
        typed: Option<char>,
    ) {
        let text = match self.surfaces.store.read(surface, binding) {
            Some(Value::String(text)) => text.clone(),
            Some(Value::Null) | None => String::new(),
            Some(other) => other.to_string(),
        };
        let mut editor = LineEditor::new(text);
        if let Some(typed) = typed {
            editor.insert_char(typed);
        }
        self.surfaces.focus = SurfaceFocus::Editing {
            surface: surface.to_owned(),
            index,
            editor,
            invalid: None,
        };
        if typed.is_some() {
            self.write_edited_field();
        }
    }

    fn read_number(&self, surface: &str, binding: &Pointer) -> Option<f64> {
        self.surfaces.store.read(surface, binding)?.as_f64()
    }

    /// Runs the action of the button the user pressed: an event goes to
    /// the session, and the keyboard returns to the composer, where the
    /// model's reply is followed; a function runs here.
    async fn run_surface_action(&mut self, surface: &str, target: &Target, action: &Action) {
        let Some(dispatched) = self.surfaces.store.dispatch(surface, target, action) else {
            return;
        };
        for message in &dispatched.outbound {
            match message {
                RendererMessage::Action(_) => {
                    self.info
                        .session
                        .daemon
                        .surface_action(
                            surface.to_owned(),
                            message.to_json(),
                            dispatched.data_model.clone(),
                        )
                        .await;
                    self.surfaces.focus = SurfaceFocus::Idle;
                }
                RendererMessage::Error(error) => self.insert_entry(&Entry::Error(format!(
                    "could not run the surface action: {}",
                    error.message()
                ))),
                // Only an agent's function call is answered with one.
                RendererMessage::FunctionResponse(_) => {}
            }
        }
        for effect in dispatched.effects {
            match effect {
                // A terminal has no say over which browser opens what, so
                // the link is shown for the user to open.
                Effect::OpenUrl(url) => {
                    self.insert_entry(&Entry::Notice(format!("link from the surface: {url}")));
                }
                // The processor applied it to the data model.
                Effect::SetData { .. } => {}
            }
        }
    }
}

impl FieldKind {
    /// Whether typing `ch` into a field of this kind does anything.
    fn accepts(self, ch: char) -> bool {
        match self {
            Self::Number => ch.is_ascii_digit() || matches!(ch, '.' | '-' | '+' | 'e' | 'E'),
            Self::DateTime { .. } => ch.is_ascii_digit() || matches!(ch, '-' | ':' | 'T' | 'Z'),
            Self::Short | Self::Long | Self::Obscured => true,
        }
    }

    /// Why `text` cannot be the field's value, if it cannot. An empty
    /// field is always allowed: whether it may stay empty is for the
    /// surface's own checks to say.
    pub(super) fn problem(self, text: &str) -> Option<&'static str> {
        use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
        let Self::DateTime { date, time } = self else {
            return None;
        };
        if text.is_empty() {
            return None;
        }
        let seconds = text.matches(':').count() == 2;
        match (date, time) {
            (true, true) | (false, false) => {
                let format = if seconds {
                    "%Y-%m-%dT%H:%M:%S"
                } else {
                    "%Y-%m-%dT%H:%M"
                };
                let padded = text.len() == if seconds { 19 } else { 16 };
                (!padded || NaiveDateTime::parse_from_str(text, format).is_err())
                    .then_some("enter a date and time as YYYY-MM-DDTHH:MM")
            }
            (true, false) => (text.len() != 10
                || NaiveDate::parse_from_str(text, "%Y-%m-%d").is_err())
            .then_some("enter a date as YYYY-MM-DD"),
            (false, true) => {
                let format = if seconds { "%H:%M:%S" } else { "%H:%M" };
                let padded = text.len() == if seconds { 8 } else { 5 };
                (!padded || NaiveTime::parse_from_str(text, format).is_err())
                    .then_some("enter a time as HH:MM")
            }
        }
    }
}

/// A slider's value as JSON: an integer when it is whole.
fn number_value(value: f64) -> Value {
    if value.fract() == 0.0 && value.abs() < 1e15 {
        Value::from(value as i64)
    } else {
        Value::from(value)
    }
}

#[cfg(test)]
#[path = "app_tests.rs"]
mod tests;