model switch also switches provider (gpt models were sent to the Claude API)
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-01 12:15:39 +00:00
1 parent 658ada46f7
commit ad73e8f1c2
7 files changed
+171 -21

No files matched your search

+86 -8
View File
@@ -650,14 +650,31 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
}
"model" if arg.is_empty() => app.t.push(Item::Info(format!("model: {}", app.model))),
"model" => {
let mut a = agent.lock().await;
a.model = arg.into();
set_window(app, &mut a, models, auto_compact);
app.model = arg.into();
app.t.push(Item::Info(format!(
"model → {arg} · context {}k",
hx_ai::models::window(models, arg) / 1000
)));
// a model belongs to one provider: switch provider with it
let k = hx_ai::models::kind_of(models, arg).unwrap_or(app.kind);
let p = if k == app.kind {
Ok(None)
} else {
make_provider(k).map(Some).map_err(|e| e.to_string())
};
match p {
Ok(p) => {
let mut a = agent.lock().await;
if let Some(p) = p {
a.provider = p;
app.kind = k;
app.quota = None;
}
a.model = arg.into();
set_window(app, &mut a, models, auto_compact);
app.model = arg.into();
app.t.push(Item::Info(format!(
"model → {arg} ({k:?}) · context {}k",
hx_ai::models::window(models, arg) / 1000
)));
}
Err(e) => app.t.push(Item::Error(format!("/model {arg}: {e}"))),
}
}
"provider" => match arg
.parse::<Kind>()
@@ -1456,6 +1473,67 @@ mod tests {
assert_eq!(until(10_030, 10_000), "1m");
}
#[tokio::test]
async fn model_switch_follows_provider() {
use std::sync::atomic::{AtomicUsize, Ordering};
static CODEX_CALLS: AtomicUsize = AtomicUsize::new(0);
let t = tempfile::tempdir().unwrap();
let (agent, _) = Agent::new(
Arc::new(NoProvider),
Default::default(),
hx_core::session::Session::open(t.path().join("s.jsonl")).unwrap(),
hx_core::tool::ToolCtx {
cwd: t.path().into(),
state: Default::default(),
},
);
let agent = Arc::new(Mutex::new(agent));
let (tx, _rx) = mpsc::unbounded_channel();
let mut models = hx_ai::models::fallback(Kind::Claude);
models.extend(hx_ai::models::fallback(Kind::Codex));
let ls: LspStatus = Box::new(|| Box::pin(async { vec![] }));
let cmds: Commands = Box::new(|_, _| None);
let mut app = test_app();
app.kind = Kind::Claude;
// codex not logged in: error, nothing changes
let fail: MakeProvider =
Box::new(|_| Err(hx_ai::Error::Http("not logged in to Codex".into())));
let ctx = Ctx {
agent: &agent,
tx: &tx,
make_provider: &fail,
models: &models,
auto_compact: true,
cwd: t.path(),
lsp_status: &ls,
commands: &cmds,
};
submit(&mut app, "/model gpt-5.6-terra".into(), &ctx).await;
assert_eq!((app.kind, app.model.as_str()), (Kind::Claude, "m1"));
assert!(matches!(app.t.items.last(), Some(Item::Error(e)) if e.contains("not logged in")));
let ok: MakeProvider = Box::new(|k| {
assert_eq!(k, Kind::Codex);
CODEX_CALLS.fetch_add(1, Ordering::SeqCst);
Ok(Arc::new(NoProvider))
});
let ctx = Ctx {
make_provider: &ok,
..ctx
};
submit(&mut app, "/model gpt-5.6-terra".into(), &ctx).await;
assert_eq!(
(app.kind, app.model.as_str()),
(Kind::Codex, "gpt-5.6-terra")
);
assert_eq!(agent.lock().await.model, "gpt-5.6-terra");
assert_eq!(CODEX_CALLS.load(Ordering::SeqCst), 1);
// same provider: no new client
submit(&mut app, "/model gpt-5.6-luna".into(), &ctx).await;
assert_eq!(CODEX_CALLS.load(Ordering::SeqCst), 1);
}
struct NoProvider;
#[async_trait::async_trait]
impl Provider for NoProvider {