diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index 32d2862..de5afca 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -1117,6 +1117,26 @@ async fn main() -> Result<()> { }) as _) }) }, + reload: { + let (mcp, cwd, models, kinds) = + (mcp.clone(), cwd.clone(), models.clone(), kinds.clone()); + Box::new(move || { + let cfg = Config::load(&cwd).map_err(|e| e.to_string())?; + let mut sys = format!( + "{}{}", + cfg.system_prompt + .as_deref() + .unwrap_or(context::DEFAULT_PROMPT), + context::prompt_tail(&context::hx_home(), &cwd) + ); + let idx = mcp.system_prompt(); + if !idx.is_empty() { + sys.push_str("\n\n"); + sys.push_str(&idx); + } + Ok((sys, command_specs(&mcp, &cwd, &models, &kinds))) + }) + }, lsp_status: { let (m, l) = (mgr.clone(), lens_ref.clone()); Box::new(move || { diff --git a/crates/hx-core/src/agent.rs b/crates/hx-core/src/agent.rs index bd51dd2..3144a76 100644 --- a/crates/hx-core/src/agent.rs +++ b/crates/hx-core/src/agent.rs @@ -243,6 +243,10 @@ impl Agent { self.effort = e; } + pub fn system(&self) -> &str { + &self.system + } + pub fn set_system(&mut self, system: impl Into) { self.system = system.into(); self.publish(); diff --git a/crates/hx-core/src/command.rs b/crates/hx-core/src/command.rs index dc67e88..ce21baa 100644 --- a/crates/hx-core/src/command.rs +++ b/crates/hx-core/src/command.rs @@ -98,6 +98,11 @@ pub fn builtins(models: &[&str]) -> Vec { ), b("btw", "side question; no tools, not saved", Free), b("skills", "list skills and where they load from", None), + b( + "reload", + "re-read AGENTS.md, skills, config, MCP prompt index", + None, + ), b( "resume", "resume a session file; no arg opens /sessions", diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index 76777e5..49ea3a9 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -47,6 +47,7 @@ pub struct Setup { pub pricing: fn(&str) -> Option, /// `/lsp` status lines. pub lsp_status: LspStatus, + pub reload: Reload, /// Extra slash commands (MCP prompts, `/mcp`): `(name, arg)` -> None = not mine, /// Some(Ok(Send(text))) = send as a user prompt, Some(Ok(Show(text))) = show, Some(Err) = error. pub commands: Commands, @@ -88,6 +89,9 @@ pub type Commands = Box< + Sync, >; +/// Rebuilds `(system prompt, slash commands)` from disk for `/reload`. +pub type Reload = Box Result<(String, Vec), String> + Send + Sync>; + pub type LspStatus = Box< dyn Fn() -> std::pin::Pin> + Send>> + Send, >; @@ -259,6 +263,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> { switcher, pricing, lsp_status, + reload, commands, specs, open_sessions, @@ -394,6 +399,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> { tx: &tx, cwd: &cwd, lsp_status: &lsp_status, + reload: &reload, commands: &commands, }; let mut term = ratatui::init(); @@ -1290,6 +1296,7 @@ struct Ctx<'a> { tx: &'a mpsc::UnboundedSender, cwd: &'a std::path::Path, lsp_status: &'a LspStatus, + reload: &'a Reload, commands: &'a Commands, } @@ -1299,6 +1306,7 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { tx, cwd, lsp_status, + reload, commands, } = *c; app.scroll.end(); @@ -1376,6 +1384,23 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { let r = status::report(&app.st, status::now_secs()); show(app, "usage", r); } + "reload" => match reload() { + Err(e) => app.t.push(Item::Error(format!("/reload: {e}"))), + Ok((sys, specs)) => { + let mut a = agent.lock().await; + // unchanged prompt: leave it alone, a rewrite would drop the prompt cache + let same = a.system() == sys; + if !same { + a.set_system(sys); + } + let (old, new) = (app.specs.len(), specs.len()); + app.specs = specs; + app.t.push(Item::Info(format!( + "reloaded: prompt {}, commands {old} → {new}", + if same { "unchanged" } else { "updated" } + ))); + } + }, "skills" => { let dirs = hx_core::context::skill_dirs(&hx_core::context::hx_home(), cwd); let mut l = skills_list(&app.specs, &dirs); @@ -2775,16 +2800,27 @@ mod tests { let (tx, _rx) = mpsc::unbounded_channel(); let ls: LspStatus = Box::new(|| Box::pin(async { vec![] })); let cmds: Commands = Box::new(|_, _| None); + let rl: Reload = Box::new(|| Ok(("sys".into(), cmd::builtins(&[])))); let ctx = Ctx { agent: &agent, tx: &tx, cwd: t.path(), lsp_status: &ls, + reload: &rl, commands: &cmds, }; let mut app = test_app(); app.session_path = old.clone(); app.title = Some("first".into()); + // /reload swaps prompt + commands; same prompt twice is reported unchanged + app.specs = vec![]; + submit(&mut app, "/reload".into(), &ctx).await; + assert_eq!(agent.lock().await.system(), "sys"); + assert!(app.specs.iter().any(|s| s.name == "reload")); + submit(&mut app, "/reload".into(), &ctx).await; + assert!( + matches!(app.t.items.last(), Some(Item::Info(m)) if m.contains("prompt unchanged")) + ); submit(&mut app, "/clear".into(), &ctx).await; let new = agent.lock().await.session().path().to_path_buf(); assert_ne!(new, old); @@ -2825,11 +2861,13 @@ mod tests { models.extend(hx_ai::models::fallback(Kind::Codex)); let ls: LspStatus = Box::new(|| Box::pin(async { vec![] })); let cmds: Commands = Box::new(|_, _| None); + let rl: Reload = Box::new(|| Ok(("sys".into(), cmd::builtins(&[])))); let ctx = Ctx { agent: &agent, tx: &tx, cwd: t.path(), lsp_status: &ls, + reload: &rl, commands: &cmds, }; let mut app = test_app();