use crate::backend::http::HttpConfig;
use crate::ProserpinaError;
pub mod api_key;
#[cfg(feature = "cli")]
pub mod oauth;
pub mod store;
#[cfg(feature = "cli")]
pub mod tui_ratatui;
pub use store::AuthStore;
#[derive(Debug, Clone)]
pub enum AuthMethod {
ApiKey {
env_var: &'static str,
signup_url: &'static str,
},
#[cfg(feature = "cli")]
OAuth {
client_id: &'static str,
authorize_url: &'static str,
token_url: &'static str,
scope: &'static str,
},
}
#[derive(Debug, Clone)]
pub struct ProviderAuth {
pub name: &'static str,
pub method: AuthMethod,
pub base_url: &'static str,
pub model: &'static str,
}
pub fn auth_registry() -> &'static [ProviderAuth] {
&[
ProviderAuth {
name: "deepseek",
method: AuthMethod::ApiKey {
env_var: "DEEPSEEK_API_KEY",
signup_url: "https://platform.deepseek.com/api_keys",
},
base_url: "https://api.deepseek.com",
model: "deepseek-chat",
},
ProviderAuth {
name: "zai",
method: AuthMethod::ApiKey {
env_var: "ZAI_API_KEY",
signup_url: "https://z.ai/manage-apikey/apikey-list",
},
base_url: "https://api.z.ai/api/coding/paas/v4",
model: "glm-5.2",
},
ProviderAuth {
name: "dashscope",
method: AuthMethod::ApiKey {
env_var: "DASHSCOPE_API_KEY",
signup_url: "https://dashscope.console.aliyun.com/apiKey",
},
base_url: "https://dashscope.aliyuncs.com/compatible-mode/v1",
model: "qwen-plus",
},
ProviderAuth {
name: "google",
method: AuthMethod::ApiKey {
env_var: "GOOGLE_API_KEY",
signup_url: "https://aistudio.google.com/app/apikey",
},
base_url: "https://generativelanguage.googleapis.com/v1beta/openai",
model: "gemini-1.5-pro",
},
ProviderAuth {
name: "moonshot",
method: AuthMethod::ApiKey {
env_var: "MOONSHOT_API_KEY",
signup_url: "https://platform.moonshot.cn/console/api-keys",
},
base_url: "https://api.moonshot.cn/v1",
model: "moonshot-v1-auto",
},
#[cfg(feature = "cli")]
ProviderAuth {
name: "openai",
method: AuthMethod::OAuth {
client_id: "app_EMoamEEZ73f0CkXaXp7hrann",
authorize_url: "https://auth.openai.com/oauth/authorize",
token_url: "https://auth.openai.com/oauth/token",
scope: "openid profile email offline_access",
},
base_url: "https://api.openai.com/v1",
model: "gpt-4o",
},
]
}
pub fn find_provider_auth(name: &str) -> Option<&'static ProviderAuth> {
auth_registry()
.iter()
.find(|p| p.name.eq_ignore_ascii_case(name))
}
#[cfg(feature = "cli")]
pub trait AuthUi {
fn select_provider(&self, providers: &[ProviderAuth]) -> Option<usize>;
fn prompt_api_key(
&self,
provider_name: &str,
signup_url: &str,
) -> Result<String, ProserpinaError>;
fn oauth_flow(&self, url: &str) -> Result<(), ProserpinaError>;
fn show_status(&self, message: &str);
fn show_success(&self, message: &str);
fn show_error(&self, message: &str);
}
pub fn auth_to_http_config(provider: &ProviderAuth, api_key: String) -> HttpConfig {
HttpConfig {
base_url: provider.base_url.to_owned(),
model: provider.model.to_owned(),
api_key,
}
}