Deepen Agent, compaction, switching and TUI modules
- Agent: make session/usage/provider/model/system/max_tokens/compact_at private; add set_provider, set_model, replace_session, fork, clear, fit_window. Fixes stale context-token reading after resume/new session. - hx-core::compact: ContextMeter, plan/apply and estimate_tokens split out of agent.rs, testable without provider or session. - hx-core::switch: Switcher shared by TUI and RPC for model/provider switches; RPC set_model now also refits the compaction threshold. - hx-tui: status.rs (footer, /usage as pure functions), popup.rs, and Picker::key returning an Act. - tests/tui_smoke.py: resolve the temp dir (macOS /var symlink) and match the resume via the OSC window title instead of split screen text.
This commit is contained in:
1 parent
fe308e0f36
commit
eaa6e22fbd
11 files changed
+1407
-632
No files matched your search
@@ -22,10 +22,22 @@ pub struct Entry {
|
||||
pub pick: Pick,
|
||||
}
|
||||
|
||||
/// What a key press asks the app to do with the picker.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Act {
|
||||
None,
|
||||
/// Dismiss the picker.
|
||||
Close,
|
||||
/// Dismiss the picker and submit this line.
|
||||
Run(String),
|
||||
/// Dismiss the picker and put this into the editor.
|
||||
Insert(String),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Picker {
|
||||
pub query: String,
|
||||
pub sel: usize,
|
||||
query: String,
|
||||
sel: usize,
|
||||
all: Vec<Entry>,
|
||||
}
|
||||
|
||||
@@ -79,11 +91,40 @@ impl Picker {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn selected(&self) -> Option<Pick> {
|
||||
/// Edit the query, move the selection, or pick; everything else is ignored.
|
||||
pub fn key(&mut self, k: crossterm::event::KeyEvent) -> Act {
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
|
||||
match k.code {
|
||||
KeyCode::Esc => return Act::Close,
|
||||
KeyCode::Char('c' | 'p') if ctrl => return Act::Close,
|
||||
KeyCode::Up => self.move_sel(-1),
|
||||
KeyCode::Down | KeyCode::Tab => self.move_sel(1),
|
||||
KeyCode::Backspace => {
|
||||
self.query.pop();
|
||||
self.sel = 0;
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
self.query.push(c);
|
||||
self.sel = 0;
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
return match self.selected() {
|
||||
Some(Pick::Run(l)) => Act::Run(l),
|
||||
Some(Pick::Insert(l)) => Act::Insert(l),
|
||||
None => Act::Close,
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Act::None
|
||||
}
|
||||
|
||||
fn selected(&self) -> Option<Pick> {
|
||||
self.matches().get(self.sel).map(|e| e.pick.clone())
|
||||
}
|
||||
|
||||
pub fn move_sel(&mut self, d: isize) {
|
||||
fn move_sel(&mut self, d: isize) {
|
||||
let n = self.matches().len().max(1) as isize;
|
||||
self.sel = (self.sel as isize + d).rem_euclid(n) as usize;
|
||||
}
|
||||
@@ -207,4 +248,49 @@ mod tests {
|
||||
p.move_sel(-1);
|
||||
assert_eq!(p.sel, p.matches().len() - 1, "wraps to the end");
|
||||
}
|
||||
|
||||
fn press(code: crossterm::event::KeyCode) -> crossterm::event::KeyEvent {
|
||||
crossterm::event::KeyEvent::new(code, crossterm::event::KeyModifiers::NONE)
|
||||
}
|
||||
|
||||
fn type_str(p: &mut Picker, s: &str) {
|
||||
for c in s.chars() {
|
||||
assert_eq!(p.key(press(crossterm::event::KeyCode::Char(c))), Act::None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys_filter_move_and_pick() {
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let mut p = Picker::new(
|
||||
&cmd::builtins(&["opus", "sonnet"]),
|
||||
t.path(),
|
||||
&t.path().join("x"),
|
||||
);
|
||||
// a command without args runs, one with args goes to the editor
|
||||
type_str(&mut p, "comp");
|
||||
assert_eq!(p.key(press(KeyCode::Enter)), Act::Run("/compact".into()));
|
||||
p.key(press(KeyCode::Backspace));
|
||||
p.key(press(KeyCode::Backspace));
|
||||
p.key(press(KeyCode::Backspace));
|
||||
p.key(press(KeyCode::Backspace));
|
||||
type_str(&mut p, "/model");
|
||||
assert_eq!(p.key(press(KeyCode::Enter)), Act::Insert("/model ".into()));
|
||||
// typing resets the selection; down moves it
|
||||
for _ in 0..6 {
|
||||
p.key(press(KeyCode::Backspace));
|
||||
}
|
||||
p.key(press(KeyCode::Down));
|
||||
assert_eq!(p.sel, 1);
|
||||
type_str(&mut p, "m");
|
||||
assert_eq!(p.sel, 0);
|
||||
// no match: enter just closes
|
||||
type_str(&mut p, "zzzzzz");
|
||||
assert_eq!(p.key(press(KeyCode::Enter)), Act::Close);
|
||||
assert_eq!(p.key(press(KeyCode::Esc)), Act::Close);
|
||||
let ctrl = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL);
|
||||
assert_eq!(p.key(ctrl('p')), Act::Close);
|
||||
assert_eq!(p.key(ctrl('c')), Act::Close);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user