use taskfast_client::api::types::{
AgentCreateRequest, AgentCreateResponse, AgentProfile, AgentReadiness, WalletSetupRequest,
WalletSetupResponse,
};
use taskfast_client::{map_api_error, Error, Result, TaskFastClient};
#[derive(Debug)]
pub enum WalletRegistration {
Configured(WalletSetupResponse),
AlreadyConfigured,
}
pub async fn validate_auth(client: &TaskFastClient) -> Result<AgentProfile> {
match client.inner().get_agent_profile().await {
Ok(v) => Ok(v.into_inner()),
Err(e) => Err(map_api_error(e).await),
}
}
pub async fn create_agent_headless(
client: &TaskFastClient,
body: &AgentCreateRequest,
) -> Result<AgentCreateResponse> {
let resp = match client.inner().register_agent(body).await {
Ok(v) => v.into_inner(),
Err(e) => return Err(map_api_error(e).await),
};
match resp.api_key.as_deref() {
Some(k) if !k.is_empty() => Ok(resp),
_ => Err(Error::Server(
"createAgentHeadless: response missing api_key — cannot persist credentials".into(),
)),
}
}
pub async fn register_wallet(
client: &TaskFastClient,
body: &WalletSetupRequest,
) -> Result<WalletRegistration> {
match client.inner().register_agent_wallet(body).await {
Ok(v) => Ok(WalletRegistration::Configured(v.into_inner())),
Err(e) => match map_api_error(e).await {
Error::Validation { code, .. } if code == "wallet_already_configured" => {
Ok(WalletRegistration::AlreadyConfigured)
}
other => Err(other),
},
}
}
pub async fn get_readiness(client: &TaskFastClient) -> Result<AgentReadiness> {
match client.inner().get_agent_readiness().await {
Ok(v) => Ok(v.into_inner()),
Err(e) => Err(map_api_error(e).await),
}
}