Remember thinking level in last.json too
This commit is contained in:
1 parent
ab2af421a4
commit
e1143f0b8b
3 files changed
+45
-15
No files matched your search
@@ -24,10 +24,13 @@ pub struct Switched {
|
||||
}
|
||||
|
||||
/// Last chosen provider + model, kept in `$HX_HOME/last.json` so the next start reuses it.
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct Last {
|
||||
pub provider: Kind,
|
||||
pub model: String,
|
||||
/// Thinking level; absent in files written before it was saved.
|
||||
#[serde(default)]
|
||||
pub effort: Option<hx_ai::Effort>,
|
||||
}
|
||||
|
||||
impl Last {
|
||||
@@ -38,7 +41,7 @@ impl Last {
|
||||
}
|
||||
|
||||
pub struct Switcher {
|
||||
remember: Option<std::path::PathBuf>,
|
||||
remember: Option<(std::path::PathBuf, Last)>,
|
||||
kind: Kind,
|
||||
models: Vec<ModelInfo>,
|
||||
make_provider: MakeProvider,
|
||||
@@ -64,11 +67,20 @@ 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);
|
||||
/// `start` is what the agent starts with.
|
||||
pub fn remember_in(mut self, path: std::path::PathBuf, start: Last) -> Self {
|
||||
self.remember = Some((path, start));
|
||||
self
|
||||
}
|
||||
|
||||
/// Save a new thinking level next to the model (best effort).
|
||||
pub fn remember_effort(&mut self, e: hx_ai::Effort) {
|
||||
if let Some((p, l)) = &mut self.remember {
|
||||
l.effort = Some(e);
|
||||
let _ = serde_json::to_vec(&*l).map(|b| std::fs::write(&*p, b));
|
||||
}
|
||||
}
|
||||
|
||||
/// The provider the agent currently uses.
|
||||
pub fn kind(&self) -> Kind {
|
||||
self.kind
|
||||
@@ -103,13 +115,11 @@ impl Switcher {
|
||||
} else {
|
||||
agent.set_model(model.as_str());
|
||||
}
|
||||
if let Some(p) = &self.remember {
|
||||
let last = Last {
|
||||
provider: kind,
|
||||
model: model.clone(),
|
||||
};
|
||||
if let Some((p, l)) = &mut self.remember {
|
||||
l.provider = kind;
|
||||
l.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 _ = serde_json::to_vec(&*l).map(|b| std::fs::write(&*p, b));
|
||||
}
|
||||
let window = models::window(&self.models, &model);
|
||||
if self.auto_compact {
|
||||
@@ -254,11 +264,18 @@ mod tests {
|
||||
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());
|
||||
let start = Last {
|
||||
provider: Kind::Claude,
|
||||
model: "m".into(),
|
||||
effort: None,
|
||||
};
|
||||
let mut sw = sw.remember_in(f.clone(), start);
|
||||
sw.set_model(&mut a, "gpt-x").unwrap();
|
||||
sw.remember_effort(hx_ai::Effort::High);
|
||||
let want = Last {
|
||||
provider: Kind::Codex,
|
||||
model: "gpt-x".into(),
|
||||
effort: Some(hx_ai::Effort::High),
|
||||
};
|
||||
assert_eq!(Last::load(&f), Some(want));
|
||||
std::fs::write(&f, "junk").unwrap();
|
||||
|
||||
Reference in new issue
Block a user