OpenAI-compatible provider (Kind::Custom, [openai_compat] config)
This commit is contained in:
1 parent
63baad33a8
commit
b00e5b503f
6 files changed
+617
-10
No files matched your search
@@ -91,6 +91,8 @@ struct Config {
|
||||
provider: Option<Kind>,
|
||||
model: Option<String>,
|
||||
claude_model: Option<String>,
|
||||
/// OpenAI-compatible endpoint (Ollama, vLLM, LM Studio, OpenRouter, ...), provider "custom".
|
||||
openai_compat: Option<hx_ai::openai_compat::Config>,
|
||||
codex_model: Option<String>,
|
||||
/// Auto-compact threshold in estimated tokens.
|
||||
compact_at: Option<usize>,
|
||||
@@ -143,6 +145,7 @@ impl Config {
|
||||
provider: o.provider.or(c.provider),
|
||||
model: o.model.or(c.model),
|
||||
claude_model: o.claude_model.or(c.claude_model),
|
||||
openai_compat: o.openai_compat.or(c.openai_compat),
|
||||
codex_model: o.codex_model.or(c.codex_model),
|
||||
compact_at: o.compact_at.or(c.compact_at),
|
||||
max_tokens: o.max_tokens.or(c.max_tokens),
|
||||
@@ -201,9 +204,12 @@ fn command_specs(
|
||||
|
||||
/// Providers with stored or importable credentials.
|
||||
fn logged_in() -> Vec<Kind> {
|
||||
[Kind::Claude, Kind::Codex]
|
||||
[Kind::Claude, Kind::Codex, Kind::Custom]
|
||||
.into_iter()
|
||||
.filter(|k| hx_ai::auth::Auth::load(*k).is_ok())
|
||||
.filter(|k| match k {
|
||||
Kind::Custom => COMPAT.get().is_some(),
|
||||
k => hx_ai::auth::Auth::load(*k).is_ok(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -267,6 +273,9 @@ fn pricing(model: &str) -> Option<hx_ai::Pricing> {
|
||||
})
|
||||
}
|
||||
|
||||
/// `[openai_compat]` from config, set once at startup.
|
||||
static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new();
|
||||
|
||||
fn provider(k: Kind) -> hx_ai::Result<Arc<dyn Provider>> {
|
||||
Ok(match k {
|
||||
Kind::Claude => {
|
||||
@@ -276,6 +285,17 @@ fn provider(k: Kind) -> hx_ai::Result<Arc<dyn Provider>> {
|
||||
Err(_) => a,
|
||||
})
|
||||
}
|
||||
Kind::Custom => {
|
||||
let cfg = COMPAT
|
||||
.get()
|
||||
.cloned()
|
||||
.ok_or_else(|| hx_ai::Error::Http("no [openai_compat] in config.toml".into()))?;
|
||||
let c = hx_ai::openai_compat::OpenAiCompat::new(cfg)?;
|
||||
Arc::new(match std::env::var("HX_BASE_URL") {
|
||||
Ok(u) => c.with_base_url(u),
|
||||
Err(_) => c,
|
||||
})
|
||||
}
|
||||
Kind::Codex => {
|
||||
let c = Codex::login_default()?;
|
||||
Arc::new(match std::env::var("HX_BASE_URL") {
|
||||
@@ -344,6 +364,11 @@ async fn main() -> Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
Some(Cmd::Models { refresh }) => {
|
||||
if let Ok(cfg) = Config::load(&std::env::current_dir()?)
|
||||
&& let Some(oc) = cfg.openai_compat
|
||||
{
|
||||
let _ = COMPAT.set(oc);
|
||||
}
|
||||
print_models(refresh).await;
|
||||
return Ok(());
|
||||
}
|
||||
@@ -482,6 +507,9 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
let cwd = std::env::current_dir()?;
|
||||
let cfg = Config::load(&cwd)?;
|
||||
if let Some(oc) = cfg.openai_compat.clone() {
|
||||
let _ = COMPAT.set(oc);
|
||||
}
|
||||
let kinds = logged_in();
|
||||
let last_path = context::hx_home().join("last.json");
|
||||
let remember = cfg.remember_model != Some(false);
|
||||
@@ -511,6 +539,7 @@ async fn main() -> Result<()> {
|
||||
match kind {
|
||||
Kind::Claude => cfg.claude_model.clone(),
|
||||
Kind::Codex => cfg.codex_model.clone(),
|
||||
Kind::Custom => None,
|
||||
}
|
||||
.unwrap_or_else(|| hx_ai::models::default_for(kind, &models))
|
||||
});
|
||||
@@ -847,6 +876,12 @@ mod tests {
|
||||
assert_eq!(c.provider, Some(Kind::Codex));
|
||||
assert_eq!(c.codex_model.as_deref(), Some("gpt-5"));
|
||||
assert!(toml::from_str::<Config>("typo = 1").is_err());
|
||||
let c: Config = toml::from_str(
|
||||
"provider = \"custom\"\n[openai_compat]\nbase_url = \"http://x/v1\"\nmodels = [\"q\"]",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(c.provider, Some(Kind::Custom));
|
||||
assert_eq!(c.openai_compat.unwrap().models, ["q"]);
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user