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
+28
-20
@@ -7,6 +7,7 @@ use hx_core::agent::Agent;
|
||||
use hx_core::context;
|
||||
use hx_core::hook::{Event, Hook, HookAction};
|
||||
use hx_core::session::Session;
|
||||
use hx_core::switch::Switcher;
|
||||
use hx_core::tool::ToolCtx;
|
||||
use std::io::Write;
|
||||
|
||||
@@ -448,8 +449,12 @@ async fn main() -> Result<()> {
|
||||
Session::open(&path)?,
|
||||
cx,
|
||||
);
|
||||
agent.model = model;
|
||||
agent.system = context::system_prompt(cfg.system_prompt.as_deref(), &context::hx_home(), &cwd);
|
||||
agent.set_model(model);
|
||||
agent.set_system(context::system_prompt(
|
||||
cfg.system_prompt.as_deref(),
|
||||
&context::hx_home(),
|
||||
&cwd,
|
||||
));
|
||||
let mcp = Arc::new(
|
||||
hx_mcp::Hub::start(
|
||||
hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?,
|
||||
@@ -459,8 +464,7 @@ async fn main() -> Result<()> {
|
||||
mcp.register(&mut agent.tools);
|
||||
let idx = mcp.system_prompt();
|
||||
if !idx.is_empty() {
|
||||
agent.system.push_str("\n\n");
|
||||
agent.system.push_str(&idx);
|
||||
agent.append_system(&idx);
|
||||
}
|
||||
for (n, s) in &mcp.servers {
|
||||
if let Some(e) = &s.error {
|
||||
@@ -468,14 +472,12 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
if let Some(n) = cfg.max_tokens {
|
||||
agent.max_tokens = n;
|
||||
agent.set_max_tokens(n);
|
||||
}
|
||||
match cfg.compact_at {
|
||||
Some(n) => agent.set_compact_at(n),
|
||||
None => agent.fit_window(hx_ai::models::window(&models, agent.model())),
|
||||
}
|
||||
agent.compact_at = cfg.compact_at.unwrap_or_else(|| {
|
||||
hx_ai::models::compact_threshold(
|
||||
hx_ai::models::window(&models, &agent.model),
|
||||
agent.max_tokens,
|
||||
)
|
||||
});
|
||||
for e in &cfg.extensions {
|
||||
let x = hx_core::ext::Extension::start(e, &cwd)
|
||||
.await
|
||||
@@ -546,8 +548,12 @@ async fn main() -> Result<()> {
|
||||
specs,
|
||||
files: hx_tui::project_files(&cwd),
|
||||
session_dir: dir,
|
||||
models: models.clone(),
|
||||
make_provider: provider,
|
||||
switcher: Switcher::new(
|
||||
kind,
|
||||
models.clone(),
|
||||
Box::new(provider),
|
||||
cfg.compact_at.is_none(),
|
||||
),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
@@ -560,13 +566,15 @@ async fn main() -> Result<()> {
|
||||
let Some(prompt) = cli.print else {
|
||||
hx_tui::run(hx_tui::Setup {
|
||||
agent,
|
||||
kind,
|
||||
make_provider: Box::new(provider),
|
||||
switcher: Switcher::new(
|
||||
kind,
|
||||
models.clone(),
|
||||
Box::new(provider),
|
||||
cfg.compact_at.is_none(),
|
||||
),
|
||||
pricing,
|
||||
specs,
|
||||
open_sessions,
|
||||
models: models.clone(),
|
||||
auto_compact: cfg.compact_at.is_none(),
|
||||
commands: {
|
||||
let mcp = mcp.clone();
|
||||
Box::new(move |name: &str, arg: &str| {
|
||||
@@ -644,11 +652,11 @@ async fn main() -> Result<()> {
|
||||
if cli.mode == Mode::Json {
|
||||
rpc::emit(
|
||||
&serde_json::json!({"type": "done", "error": r.as_ref().err().map(|e| e.to_string()),
|
||||
"usage": agent.usage, "session": path}),
|
||||
"usage": agent.usage(), "session": path}),
|
||||
);
|
||||
}
|
||||
let u = &agent.usage;
|
||||
let cost = pricing(&agent.model)
|
||||
let u = &agent.usage();
|
||||
let cost = pricing(agent.model())
|
||||
.map(|p| format!(" ~${:.4}", u.cost(&p)))
|
||||
.unwrap_or_default();
|
||||
eprintln!(
|
||||
|
||||
+22
-28
@@ -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()),
|
||||
|
||||
Reference in new issue
Block a user