M11: provider model catalog, per-model context window, subscription usage in footer + /usage
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-01 11:53:27 +00:00
1 parent 3fee36d788
commit 6533dde4ca
11 files changed
+751 -54

No files matched your search

+63 -23
View File
@@ -1,6 +1,7 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
use hx_ai::auth::{self, Kind};
use hx_ai::models::ModelInfo;
use hx_ai::{Provider, StreamEvent, anthropic::Anthropic, codex::Codex};
use hx_core::agent::Agent;
use hx_core::context;
@@ -121,19 +122,25 @@ impl Config {
}
}
/// Offered by `/model` completion; any other id still works.
const MODELS: &[&str] = &[
"claude-sonnet-4-5",
"claude-opus-4-1",
"claude-haiku-4-5",
"gpt-5-codex",
"gpt-5",
];
/// Builtins, MCP prompts (`/server:prompt`), then skills (builtins win name clashes).
fn command_specs(mcp: &hx_mcp::Hub, cwd: &std::path::Path) -> Vec<hx_core::command::CmdSpec> {
/// `models` feeds `/model` completion (any other id still works); `kinds` = logged-in providers.
fn command_specs(
mcp: &hx_mcp::Hub,
cwd: &std::path::Path,
models: &[ModelInfo],
kinds: &[Kind],
) -> Vec<hx_core::command::CmdSpec> {
use hx_core::command::{self as c, ArgKind, CmdSpec, Source};
let mut v = c::builtins(MODELS);
let ids: Vec<&str> = models.iter().map(|m| m.id.as_str()).collect();
let mut v = c::builtins(&ids);
if let Some(p) = v.iter_mut().find(|s| s.name == "provider") {
p.args = ArgKind::Enum(
kinds
.iter()
.map(|k| format!("{k:?}").to_lowercase())
.collect(),
);
}
v.push(CmdSpec::new(
"mcp",
"MCP servers and prompts",
@@ -155,12 +162,24 @@ fn command_specs(mcp: &hx_mcp::Hub, cwd: &std::path::Path) -> Vec<hx_core::comma
v
}
fn default_model(k: Kind) -> String {
match k {
Kind::Claude => "claude-sonnet-4-5",
Kind::Codex => "gpt-5-codex",
/// Providers with stored or importable credentials.
fn logged_in() -> Vec<Kind> {
[Kind::Claude, Kind::Codex]
.into_iter()
.filter(|k| hx_ai::auth::Auth::load(*k).is_ok())
.collect()
}
/// Model catalog of every logged-in provider (live list cached a day, else the built-in list).
async fn catalog(kinds: &[Kind]) -> Vec<ModelInfo> {
let mut v = vec![];
for k in kinds {
match provider(*k) {
Ok(p) => v.extend(hx_ai::models::load(*k, p.as_ref()).await),
Err(_) => v.extend(hx_ai::models::fallback(*k)),
}
}
.into()
v
}
/// USD per million tokens. ponytail: subscription usage isn't billed per token; this is an API-equivalent estimate.
@@ -171,10 +190,18 @@ fn pricing(model: &str) -> Option<hx_ai::Pricing> {
cache_read,
cache_write,
};
// 5.x generation prices first; older ids keep their own
Some(match model {
m if m.contains("fable") => p(10.0, 50.0, 1.0, 12.5),
m if m.contains("opus-5") => p(4.0, 20.0, 0.4, 5.0),
m if m.contains("sonnet-5") => p(2.0, 10.0, 0.2, 2.5),
m if m.contains("opus") => p(15.0, 75.0, 1.5, 18.75),
m if m.contains("sonnet") => p(3.0, 15.0, 0.3, 3.75),
m if m.contains("haiku") => p(1.0, 5.0, 0.1, 1.25),
m if m.contains("sol") || m.starts_with("gpt-5.5") => p(5.0, 30.0, 0.5, 0.0),
m if m.contains("terra") => p(2.5, 15.0, 0.25, 0.0),
m if m.contains("luna") => p(1.0, 6.0, 0.1, 0.0),
m if m.starts_with("gpt-6") => p(10.0, 50.0, 1.0, 0.0),
m if m.starts_with("gpt-5") => p(1.25, 10.0, 0.125, 0.0),
_ => return None,
})
@@ -289,13 +316,20 @@ async fn main() -> Result<()> {
}
let cwd = std::env::current_dir()?;
let cfg = Config::load(&cwd)?;
let kind = cli.provider.or(cfg.provider).unwrap_or(Kind::Claude);
let kinds = logged_in();
// default provider: configured, else the first one logged in
let kind = cli
.provider
.or(cfg.provider)
.or(kinds.first().copied())
.unwrap_or(Kind::Claude);
let models = catalog(&kinds).await;
let model = cli.model.or(cfg.model).unwrap_or_else(|| {
match kind {
Kind::Claude => cfg.claude_model.clone(),
Kind::Codex => cfg.codex_model.clone(),
}
.unwrap_or_else(|| default_model(kind))
.unwrap_or_else(|| hx_ai::models::default_for(kind, &models))
});
let dir = session_dir(&cwd);
let query = cli.resume.as_deref().filter(|q| !q.is_empty());
@@ -349,12 +383,15 @@ async fn main() -> Result<()> {
eprintln!("[mcp] {n}: {e}");
}
}
if let Some(n) = cfg.compact_at {
agent.compact_at = n;
}
if let Some(n) = cfg.max_tokens {
agent.max_tokens = n;
}
agent.compact_at = cfg.compact_at.unwrap_or_else(|| {
hx_ai::models::compact_threshold(
hx_ai::models::window(&models, &agent.model),
agent.max_tokens,
)
});
for e in &cfg.extensions {
let x = hx_core::ext::Extension::start(e, &cwd)
.await
@@ -416,7 +453,7 @@ async fn main() -> Result<()> {
.collect();
mgr.warm(probe).await;
}
let specs = command_specs(&mcp, &cwd);
let specs = command_specs(&mcp, &cwd, &models, &kinds);
if cli.mode == Mode::Rpc {
let r = rpc::serve(agent, abort, specs, hx_tui::project_files(&cwd), dir).await;
mgr.shutdown().await;
@@ -430,10 +467,11 @@ async fn main() -> Result<()> {
agent,
kind,
make_provider: Box::new(provider),
default_model,
pricing,
specs,
open_sessions,
models: models.clone(),
auto_compact: cfg.compact_at.is_none(),
commands: {
let mcp = mcp.clone();
Box::new(move |name: &str, arg: &str| {
@@ -550,5 +588,7 @@ mod tests {
assert_eq!(c.codex_model.as_deref(), Some("gpt-5"));
assert!(toml::from_str::<Config>("typo = 1").is_err());
assert!(pricing("claude-sonnet-4-5").is_some() && pricing("mystery").is_none());
assert_eq!(pricing("claude-sonnet-5-5").unwrap().input, 2.0);
assert_eq!(pricing("gpt-5.6-terra").unwrap().input, 2.5);
}
}