use std::time::{Duration, Instant};
use clap::Parser;
use serde_json::json;
use super::{CmdError, CmdResult, Ctx};
use crate::envelope::Envelope;
use taskfast_client::map_api_error;
use taskfast_client::ClientInfo;
const ANON_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
const ANON_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Debug, Parser)]
pub struct Args;
pub async fn run(ctx: &Ctx, _args: Args) -> CmdResult {
let (latency_ms, endpoint, base_url, authenticated) = match ctx.api_key.as_deref() {
Some(_) => probe_authenticated(ctx).await?,
None => probe_anonymous(ctx).await?,
};
let data = json!({
"pong": true,
"latency_ms": latency_ms,
"endpoint": endpoint,
"base_url": base_url,
"authenticated": authenticated,
});
Ok(Envelope::success(ctx.environment, ctx.dry_run, data))
}
async fn probe_authenticated(ctx: &Ctx) -> Result<(u64, &'static str, String, bool), CmdError> {
let client = ctx.client()?;
let base_url = client.inner().baseurl().to_string();
let started = Instant::now();
let result = client.inner().get_agent_profile().await;
let latency_ms = started.elapsed().as_millis() as u64;
if let Err(e) = result {
return Err(map_probe_error(e, &base_url).await);
}
Ok((latency_ms, "GET /agents/me", base_url, true))
}
async fn map_probe_error(e: taskfast_client::api::Error<()>, base_url: &str) -> CmdError {
if let taskfast_client::api::Error::UnexpectedResponse(resp) = &e {
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return CmdError::Validation {
code: "not_found".into(),
message: format!(
"GET /agents/me returned 404 from {base_url}: the server is reachable \
but did not rewrite this host to its /api routes (it only rewrites \
hosts in its :api_hosts list). Point --api-base/TASKFAST_API at the \
api.<domain> host for this environment (e.g. https://api.taskfast.app), \
or use 127.0.0.1 against a local dev server."
),
};
}
}
map_api_error(e).await.into()
}
async fn probe_anonymous(ctx: &Ctx) -> Result<(u64, &'static str, String, bool), CmdError> {
let base_url = ctx.base_url().to_string();
let http = reqwest::Client::builder()
.connect_timeout(ANON_CONNECT_TIMEOUT)
.timeout(ANON_REQUEST_TIMEOUT)
.build()
.map_err(|e| CmdError::Network(e.to_string()))?;
let started = Instant::now();
let resp = http
.get(&base_url)
.send()
.await
.map_err(|e| CmdError::Network(e.to_string()))?;
let latency_ms = started.elapsed().as_millis() as u64;
let _ = resp.status();
Ok((latency_ms, "GET /", base_url, false))
}