use systemprompt_traits::{AgentInfo, AgentRegistryProvider, RegistryError, ServiceOAuthConfig};
use super::registry::AgentRegistry;
use crate::error::AgentError;
#[derive(Debug, Clone)]
pub struct AgentRegistryProviderService {
registry: AgentRegistry,
}
impl AgentRegistryProviderService {
pub fn new() -> Result<Self, RegistryError> {
let registry = AgentRegistry::new().map_err(|e| RegistryError::Unavailable(e.into()))?;
Ok(Self { registry })
}
pub const fn from_registry(registry: AgentRegistry) -> Self {
Self { registry }
}
}
impl AgentRegistryProvider for AgentRegistryProviderService {
async fn get_agent(&self, name: &str) -> Result<AgentInfo, RegistryError> {
let agent = self
.registry
.get_agent(name)
.await
.map_err(registry_error)?;
Ok(AgentInfo {
name: agent.name,
port: agent.port,
enabled: agent.enabled,
oauth: ServiceOAuthConfig {
required: agent.oauth.required,
scopes: agent.oauth.scopes.iter().map(ToString::to_string).collect(),
audience: agent.oauth.audience.to_string(),
ema: false,
},
})
}
async fn list_enabled_agents(&self) -> Result<Vec<AgentInfo>, RegistryError> {
let agents = self
.registry
.list_enabled_agents()
.await
.map_err(registry_error)?;
Ok(agents
.into_iter()
.map(|agent| AgentInfo {
name: agent.name,
port: agent.port,
enabled: agent.enabled,
oauth: ServiceOAuthConfig {
required: agent.oauth.required,
scopes: agent.oauth.scopes.iter().map(ToString::to_string).collect(),
audience: agent.oauth.audience.to_string(),
ema: false,
},
})
.collect())
}
async fn get_default_agent(&self) -> Result<AgentInfo, RegistryError> {
let agent = self
.registry
.get_default_agent()
.await
.map_err(registry_error)?;
Ok(AgentInfo {
name: agent.name,
port: agent.port,
enabled: agent.enabled,
oauth: ServiceOAuthConfig {
required: agent.oauth.required,
scopes: agent.oauth.scopes.iter().map(ToString::to_string).collect(),
audience: agent.oauth.audience.to_string(),
ema: false,
},
})
}
}
fn registry_error(error: AgentError) -> RegistryError {
match error {
AgentError::NotFound(name) => RegistryError::NotFound(name),
AgentError::EmptyCorsAllowlist
| AgentError::InvalidConfig { .. }
| AgentError::ServicesConfig(_) => RegistryError::Configuration(error.into()),
other => RegistryError::Unavailable(other.into()),
}
}