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
@@ -19,6 +19,8 @@ use tokio::sync::Mutex;
|
||||
pub enum Kind {
|
||||
Claude,
|
||||
Codex,
|
||||
/// OpenAI-compatible endpoint from config; no OAuth.
|
||||
Custom,
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Kind {
|
||||
@@ -27,7 +29,8 @@ impl std::str::FromStr for Kind {
|
||||
match s {
|
||||
"claude" | "anthropic" => Ok(Kind::Claude),
|
||||
"codex" | "openai" | "chatgpt" => Ok(Kind::Codex),
|
||||
_ => Err(format!("unknown provider {s:?} (claude|codex)")),
|
||||
"custom" | "openai-compat" | "local" => Ok(Kind::Custom),
|
||||
_ => Err(format!("unknown provider {s:?} (claude|codex|custom)")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,6 +79,7 @@ fn cfg(k: Kind) -> &'static Oauth {
|
||||
match k {
|
||||
Kind::Claude => &CLAUDE,
|
||||
Kind::Codex => &CODEX,
|
||||
Kind::Custom => unreachable!("custom provider has no OAuth"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +141,7 @@ fn save_at(p: &std::path::Path, k: Kind, c: Option<&Credential>) -> Result<()> {
|
||||
/// Pull an existing login from Claude Code / Codex CLI.
|
||||
fn import(k: Kind) -> Option<Credential> {
|
||||
match k {
|
||||
Kind::Custom => None,
|
||||
Kind::Claude => {
|
||||
let v: Value = serde_json::from_slice(
|
||||
&std::fs::read(home().join(".claude/.credentials.json")).ok()?,
|
||||
@@ -211,7 +216,7 @@ async fn token_req(k: Kind, form: &[(&str, &str)]) -> Result<Credential> {
|
||||
// Anthropic wants JSON, OpenAI wants form encoding.
|
||||
let rb = match k {
|
||||
Kind::Claude => rb.json(&form.iter().cloned().collect::<BTreeMap<_, _>>()),
|
||||
Kind::Codex => rb.form(form),
|
||||
Kind::Codex | Kind::Custom => rb.form(form),
|
||||
};
|
||||
let r = rb.send().await.map_err(|e| Error::Http(e.to_string()))?;
|
||||
let status = r.status().as_u16();
|
||||
@@ -368,7 +373,7 @@ pub fn authorize_url(k: Kind, challenge: &str, state: &str) -> String {
|
||||
];
|
||||
match k {
|
||||
Kind::Claude => p.insert(0, ("code", "true")),
|
||||
Kind::Codex => p.extend([
|
||||
Kind::Codex | Kind::Custom => p.extend([
|
||||
("id_token_add_organizations", "true"),
|
||||
("codex_cli_simplified_flow", "true"),
|
||||
("originator", "hx"),
|
||||
@@ -383,12 +388,17 @@ pub fn authorize_url(k: Kind, challenge: &str, state: &str) -> String {
|
||||
|
||||
/// Interactive browser login. Prints the URL to stderr.
|
||||
pub async fn login(k: Kind) -> Result<Credential> {
|
||||
if k == Kind::Custom {
|
||||
return Err(Error::Http(
|
||||
"custom provider has no login: set [openai_compat] in config.toml".into(),
|
||||
));
|
||||
}
|
||||
let c = cfg(k);
|
||||
let (verifier, challenge) = pkce();
|
||||
// Claude uses the verifier as state (matches Claude Code); Codex uses a random state.
|
||||
let state = match k {
|
||||
Kind::Claude => verifier.clone(),
|
||||
Kind::Codex => pkce().0,
|
||||
Kind::Codex | Kind::Custom => pkce().0,
|
||||
};
|
||||
eprintln!(
|
||||
"Open this URL to log in:\n\n{}\n\nWaiting for redirect on {} (or paste the final redirect URL / code here):",
|
||||
|
||||
Reference in new issue
Block a user