Remember last chosen provider/model in HX_HOME/last.json

This commit is contained in:
hermes committed 2026-10-06 11:43:51 +00:00
1 parent 6f951ad7f8
commit 2f1ce2c072
2 files changed
+65 -4

No files matched your search

+19 -4
View File
@@ -99,6 +99,8 @@ struct Config {
explore_model: Option<String>, explore_model: Option<String>,
/// Capture the mouse for wheel scrolling (default true; false = terminal selection works without shift). /// Capture the mouse for wheel scrolling (default true; false = terminal selection works without shift).
mouse: Option<bool>, mouse: Option<bool>,
/// Reuse the last model chosen with /model or /provider (default true).
remember_model: Option<bool>,
/// "ask" (default) or "yolo": whether bash/write/edit prompt in the TUI. /// "ask" (default) or "yolo": whether bash/write/edit prompt in the TUI.
permissions: Option<String>, permissions: Option<String>,
/// Replaces the built-in system prompt (AGENTS.md etc. are still appended). /// 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), max_tokens: o.max_tokens.or(c.max_tokens),
explore_model: o.explore_model.or(c.explore_model), explore_model: o.explore_model.or(c.explore_model),
mouse: o.mouse.or(c.mouse), mouse: o.mouse.or(c.mouse),
remember_model: o.remember_model.or(c.remember_model),
permissions: o.permissions.or(c.permissions), permissions: o.permissions.or(c.permissions),
system_prompt: o.system_prompt.or(c.system_prompt), system_prompt: o.system_prompt.or(c.system_prompt),
lsp: o.lsp.or(c.lsp), lsp: o.lsp.or(c.lsp),
@@ -478,10 +481,18 @@ async fn main() -> Result<()> {
let cwd = std::env::current_dir()?; let cwd = std::env::current_dir()?;
let cfg = Config::load(&cwd)?; let cfg = Config::load(&cwd)?;
let kinds = logged_in(); 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 let kind = cli
.provider .provider
.or(cfg.provider) .or(cfg.provider)
.or(last.as_ref().map(|l| l.provider))
.or(kinds.first().copied()) .or(kinds.first().copied())
.unwrap_or(Kind::Claude); .unwrap_or(Kind::Claude);
let models = catalog(&kinds).await; 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), (None, Some(m)) => hx_ai::models::kind_of(&models, m).unwrap_or(kind),
_ => 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 { match kind {
Kind::Claude => cfg.claude_model.clone(), Kind::Claude => cfg.claude_model.clone(),
Kind::Codex => cfg.codex_model.clone(), Kind::Codex => cfg.codex_model.clone(),
@@ -667,7 +680,8 @@ async fn main() -> Result<()> {
models.clone(), models.clone(),
Box::new(provider), Box::new(provider),
cfg.compact_at.is_none(), cfg.compact_at.is_none(),
), )
.remember_in(last_path.clone()),
}, },
) )
.await; .await;
@@ -685,7 +699,8 @@ async fn main() -> Result<()> {
models.clone(), models.clone(),
Box::new(provider), Box::new(provider),
cfg.compact_at.is_none(), cfg.compact_at.is_none(),
), )
.remember_in(last_path),
pricing, pricing,
specs, specs,
open_sessions, open_sessions,
+46
View File
@@ -23,7 +23,22 @@ pub struct Switched {
pub window: u64, 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<Self> {
serde_json::from_slice(&std::fs::read(path).ok()?).ok()
}
}
pub struct Switcher { pub struct Switcher {
remember: Option<std::path::PathBuf>,
kind: Kind, kind: Kind,
models: Vec<ModelInfo>, models: Vec<ModelInfo>,
make_provider: MakeProvider, make_provider: MakeProvider,
@@ -40,6 +55,7 @@ impl Switcher {
auto_compact: bool, auto_compact: bool,
) -> Self { ) -> Self {
Self { Self {
remember: None,
kind, kind,
models, models,
make_provider, 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. /// The provider the agent currently uses.
pub fn kind(&self) -> Kind { pub fn kind(&self) -> Kind {
self.kind self.kind
@@ -81,6 +103,14 @@ impl Switcher {
} else { } else {
agent.set_model(model.as_str()); 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); let window = models::window(&self.models, &model);
if self.auto_compact { if self.auto_compact {
agent.fit_window(window); 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] #[test]
fn configured_threshold_is_left_alone() { fn configured_threshold_is_left_alone() {
let (mut sw, mut a, _m, _d) = setup(false, false); let (mut sw, mut a, _m, _d) = setup(false, false);