diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index f5d76c2..2c96c8e 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -99,6 +99,8 @@ struct Config { explore_model: Option, /// Capture the mouse for wheel scrolling (default true; false = terminal selection works without shift). mouse: Option, + /// Reuse the last model chosen with /model or /provider (default true). + remember_model: Option, /// "ask" (default) or "yolo": whether bash/write/edit prompt in the TUI. permissions: Option, /// Replaces the built-in system prompt (AGENTS.md etc. are still appended). @@ -146,6 +148,7 @@ impl Config { max_tokens: o.max_tokens.or(c.max_tokens), explore_model: o.explore_model.or(c.explore_model), mouse: o.mouse.or(c.mouse), + remember_model: o.remember_model.or(c.remember_model), permissions: o.permissions.or(c.permissions), system_prompt: o.system_prompt.or(c.system_prompt), lsp: o.lsp.or(c.lsp), @@ -478,10 +481,18 @@ async fn main() -> Result<()> { let cwd = std::env::current_dir()?; let cfg = Config::load(&cwd)?; let kinds = logged_in(); - // default provider: configured, else the first one logged in + let last_path = context::hx_home().join("last.json"); + let remember = cfg.remember_model != Some(false); + // last /model choice, only while its provider is still logged in + let last = remember + .then(|| hx_core::switch::Last::load(&last_path)) + .flatten() + .filter(|l| kinds.contains(&l.provider)); + // default provider: flag, config, last choice, else the first one logged in let kind = cli .provider .or(cfg.provider) + .or(last.as_ref().map(|l| l.provider)) .or(kinds.first().copied()) .unwrap_or(Kind::Claude); let models = catalog(&kinds).await; @@ -490,7 +501,9 @@ async fn main() -> Result<()> { (None, Some(m)) => hx_ai::models::kind_of(&models, m).unwrap_or(kind), _ => kind, }; - let model = cli.model.or(cfg.model).unwrap_or_else(|| { + // the remembered model applies only to the provider it was chosen on + let last_model = last.filter(|l| l.provider == kind).map(|l| l.model); + let model = cli.model.or(cfg.model).or(last_model).unwrap_or_else(|| { match kind { Kind::Claude => cfg.claude_model.clone(), Kind::Codex => cfg.codex_model.clone(), @@ -667,7 +680,8 @@ async fn main() -> Result<()> { models.clone(), Box::new(provider), cfg.compact_at.is_none(), - ), + ) + .remember_in(last_path.clone()), }, ) .await; @@ -685,7 +699,8 @@ async fn main() -> Result<()> { models.clone(), Box::new(provider), cfg.compact_at.is_none(), - ), + ) + .remember_in(last_path), pricing, specs, open_sessions, diff --git a/crates/hx-core/src/switch.rs b/crates/hx-core/src/switch.rs index 0734a92..6e2c0f4 100644 --- a/crates/hx-core/src/switch.rs +++ b/crates/hx-core/src/switch.rs @@ -23,7 +23,22 @@ pub struct Switched { pub window: u64, } +/// Last chosen provider + model, kept in `$HX_HOME/last.json` so the next start reuses it. +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] +pub struct Last { + pub provider: Kind, + pub model: String, +} + +impl Last { + /// `None` when the file is missing or unreadable. + pub fn load(path: &std::path::Path) -> Option { + serde_json::from_slice(&std::fs::read(path).ok()?).ok() + } +} + pub struct Switcher { + remember: Option, kind: Kind, models: Vec, make_provider: MakeProvider, @@ -40,6 +55,7 @@ impl Switcher { auto_compact: bool, ) -> Self { Self { + remember: None, kind, models, make_provider, @@ -47,6 +63,12 @@ impl Switcher { } } + /// Write every successful switch to `path` (best effort). + pub fn remember_in(mut self, path: std::path::PathBuf) -> Self { + self.remember = Some(path); + self + } + /// The provider the agent currently uses. pub fn kind(&self) -> Kind { self.kind @@ -81,6 +103,14 @@ impl Switcher { } else { agent.set_model(model.as_str()); } + if let Some(p) = &self.remember { + let last = Last { + provider: kind, + model: model.clone(), + }; + // ponytail: failing to save must not fail the switch + let _ = serde_json::to_vec(&last).map(|b| std::fs::write(p, b)); + } let window = models::window(&self.models, &model); if self.auto_compact { agent.fit_window(window); @@ -219,6 +249,22 @@ mod tests { ); } + #[test] + fn switches_are_remembered() { + let (sw, mut a, _m, d) = setup(true, false); + let f = d.path().join("last.json"); + assert_eq!(Last::load(&f), None); + let mut sw = sw.remember_in(f.clone()); + sw.set_model(&mut a, "gpt-x").unwrap(); + let want = Last { + provider: Kind::Codex, + model: "gpt-x".into(), + }; + assert_eq!(Last::load(&f), Some(want)); + std::fs::write(&f, "junk").unwrap(); + assert_eq!(Last::load(&f), None); + } + #[test] fn configured_threshold_is_left_alone() { let (mut sw, mut a, _m, _d) = setup(false, false);