Deepen Agent, compaction, switching and TUI modules
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

- 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:
johannes committed 2026-10-02 00:10:41 +02:00
1 parent fe308e0f36
commit eaa6e22fbd
11 files changed
+1407 -632

No files matched your search

+22 -28
View File
@@ -51,8 +51,7 @@ pub struct Ctx {
pub specs: Vec<hx_core::command::CmdSpec>,
pub files: Vec<String>,
pub session_dir: std::path::PathBuf,
pub models: Vec<hx_ai::models::ModelInfo>,
pub make_provider: fn(hx_ai::auth::Kind) -> hx_ai::Result<Arc<dyn hx_ai::Provider>>,
pub switcher: hx_core::switch::Switcher,
}
pub async fn serve(mut agent: Agent, abort: AbortHandle, cx: Ctx) -> Result<()> {
@@ -60,8 +59,7 @@ pub async fn serve(mut agent: Agent, abort: AbortHandle, cx: Ctx) -> Result<()>
specs,
files,
session_dir,
models,
make_provider,
mut switcher,
} = cx;
agent.bus.add(Notify);
let agent = Arc::new(Mutex::new(agent));
@@ -116,7 +114,7 @@ pub async fn serve(mut agent: Agent, abort: AbortHandle, cx: Ctx) -> Result<()>
busy.store(false, std::sync::atomic::Ordering::SeqCst);
reply(
&id,
r.map(|m| json!({"message": m, "usage": a.usage}))
r.map(|m| json!({"message": m, "usage": a.usage()}))
.map_err(|e| e.to_string()),
);
}));
@@ -129,35 +127,31 @@ pub async fn serve(mut agent: Agent, abort: AbortHandle, cx: Ctx) -> Result<()>
let mut a = agent.lock().await;
let r = match method {
"state" => Ok(json!({
"model": a.model, "session": a.session.path(), "head": a.session.head(),
"title": hx_core::session::title(&a.session.messages()),
"messages": a.session.messages().len(), "usage": a.usage,
"model": a.model(), "session": a.session().path(), "head": a.session().head(),
"title": hx_core::session::title(&a.session().messages()),
"messages": a.session().messages().len(), "usage": a.usage(),
"tools": a.tools.specs().into_iter().map(|t| t.name).collect::<Vec<_>>(),
})),
"messages" => Ok(json!(a.session.messages())),
"messages" => Ok(json!(a.session().messages())),
"fork" => match p["id"].as_u64() {
Some(n) if a.session.fork(n) => Ok(json!({"head": a.session.head()})),
Some(n) if a.fork(n) => Ok(json!({"head": a.session().head()})),
_ => Err("unknown entry id".into()),
},
"clear" => a
.session
.rebase(vec![])
.clear()
.map(|_| json!({}))
.map_err(|e| e.to_string()),
"set_model" => match p["model"].as_str() {
// switch provider too when the model belongs to another one
Some(m) => match hx_ai::models::kind_of(&models, m) {
Some(k) => make_provider(k).map(|prov| {
a.provider = prov;
a.model = m.into();
json!({"model": m, "provider": format!("{k:?}").to_lowercase()})
// switches provider too when the model belongs to another one
Some(m) => switcher
.set_model(&mut a, m)
.map(|sw| {
let mut r = json!({"model": sw.model});
if sw.provider_changed {
r["provider"] = json!(format!("{:?}", sw.kind).to_lowercase());
}
r
}),
None => {
a.model = m.into();
Ok(json!({"model": m}))
}
}
.map_err(|e| e.to_string()),
None => Err("params.model required".into()),
},
"compact" => a
@@ -179,15 +173,15 @@ pub async fn serve(mut agent: Agent, abort: AbortHandle, cx: Ctx) -> Result<()>
}
"new" => hx_core::session::Session::open(hx_core::session::new_path(&session_dir))
.map(|s| {
a.session = s;
json!({"session": a.session.path()})
a.replace_session(s);
json!({"session": a.session().path()})
})
.map_err(|e| e.to_string()),
"resume" => match p["path"].as_str() {
Some(path) => hx_core::session::Session::open(path)
.map(|s| {
a.session = s;
json!({"session": a.session.path(), "messages": a.session.messages().len()})
a.replace_session(s);
json!({"session": a.session().path(), "messages": a.session().messages().len()})
})
.map_err(|e| e.to_string()),
None => Err("params.path required".into()),