use rho_protocol::{
AuthKind, ErrorCode, ProviderEntry, ProviderListParams, ProviderListResponse, RpcError,
SignInMethod,
};
use rho_providers::{
model::catalog::{self, LoginGroup},
provider::{self, AuthMode, ProviderAuthKind, ProviderDescriptor, KEYLESS_AUTH},
};
use super::auth::{self, off_workers, repository};
const POPULAR: [&str; 4] = ["anthropic", "openai", "xai", "meta"];
pub(super) async fn list(_: ProviderListParams) -> Result<ProviderListResponse, RpcError> {
let providers = off_workers(|| -> anyhow::Result<_> {
repository().load()?.providers.activate()?;
Ok(entries())
})
.await?
.map_err(|error| {
RpcError::new(
ErrorCode::InternalError,
format!("could not load config: {error:#}"),
)
})?;
Ok(ProviderListResponse::new(providers))
}
fn entries() -> Vec<ProviderEntry> {
let mut entries: Vec<_> = catalog::login_groups()
.into_iter()
.filter_map(entry)
.collect();
entries.sort_by_cached_key(|entry| {
let popular = POPULAR.iter().position(|id| *id == entry.id);
(
popular.unwrap_or(POPULAR.len()),
entry.name.to_lowercase(),
entry.id.clone(),
)
});
entries
}
fn entry(group: LoginGroup) -> Option<ProviderEntry> {
let mut providers: Vec<&'static ProviderDescriptor> = Vec::new();
let mut methods = Vec::new();
for login in &group.methods {
let Some((descriptor, mode)) = provider::resolve_auth_mode(&login.target.auth)
.filter(|(descriptor, _)| descriptor.serves_chat())
else {
continue;
};
if !providers.iter().any(|known| known.name == descriptor.name) {
providers.push(descriptor);
}
methods.push(method(descriptor, mode));
}
for descriptor in providers {
for mode in descriptor
.auth_modes()
.filter(|mode| matches!(mode.auth_kind, ProviderAuthKind::None))
{
debug_assert_eq!(mode.id, KEYLESS_AUTH, "the wire promises `none`");
methods.push(method(descriptor, mode));
}
}
if methods.is_empty() {
return None;
}
methods.sort_by_key(|(leads, _)| !leads);
let methods = methods.into_iter().map(|(_, method)| method).collect();
let entry = ProviderEntry::new(group.id, group.prompt, methods);
Some(match POPULAR.contains(&entry.id.as_str()) {
true => entry.with_popular(),
false => entry,
})
}
fn method(descriptor: &ProviderDescriptor, mode: AuthMode) -> (bool, SignInMethod) {
let offered = offer(mode.auth_kind);
let label = catalog::named_login_method_prompt(mode.id).unwrap_or(offered.label);
let kind = auth::kind(mode.id).unwrap_or(AuthKind::Keyless);
let method = SignInMethod::new(mode.id, descriptor.name, label, kind);
let method = match (
descriptor.collects_login_endpoint(),
descriptor.runtime.default_api_base(),
) {
(true, Some(endpoint)) => method.with_needs_endpoint().with_default_endpoint(endpoint),
(true, None) => method.with_needs_endpoint(),
(false, _) => method,
};
(offered.leads, method)
}
struct Offer {
label: &'static str,
leads: bool,
}
fn offer(auth_kind: ProviderAuthKind) -> Offer {
let (label, leads) = match auth_kind {
ProviderAuthKind::None => ("No sign-in", true),
ProviderAuthKind::ApiKey { .. } => ("API key", false),
ProviderAuthKind::CodexOAuth { .. } => ("ChatGPT account", true),
ProviderAuthKind::GithubCopilotDevice { .. } | ProviderAuthKind::OllamaDeviceKey { .. } => {
("Device login", false)
}
ProviderAuthKind::MetaOAuth { .. } => ("Subscription", false),
ProviderAuthKind::XaiOAuth { .. }
| ProviderAuthKind::KimiOAuth { .. }
| ProviderAuthKind::BearerCredential { .. } => ("Account", false),
};
Offer { label, leads }
}
#[cfg(test)]
#[path = "providers_tests.rs"]
mod tests;