taskfast_client/client.rs
1// SPDX-License-Identifier: MIT
2//! Authenticated TaskFast client.
3//!
4//! [`TaskFastClient`] wraps the progenitor-generated [`crate::api::Client`]
5//! with the cross-cutting concerns the generated code deliberately omits:
6//!
7//! 1. **Auth** — injects `X-API-Key` on every request via reqwest's
8//! `default_headers`, so every generated method call carries it without
9//! per-call plumbing.
10//! 2. **Typed errors** — translates [`crate::api::Error<()>`] into
11//! [`crate::errors::Error`] by reading the response body of an
12//! `UnexpectedResponse` and classifying by status code. The normalizer
13//! strips non-2xx response declarations from the spec (see
14//! `xtask::normalize_spec`), so every TaskFast failure arrives here as
15//! `UnexpectedResponse` — we are the single funnel.
16//! 3. **Retry** — [`TaskFastClient::call_with_retry`] runs a closure
17//! under the configured [`RetryPolicy`], honoring 429 `Retry-After`
18//! and retrying 5xx with exponential backoff.
19//!
20//! Direct access to the generated client is available via [`TaskFastClient::inner`]
21//! so callers can invoke `client.inner().get_agent_profile().await` and then
22//! pipe the result through [`map_api_error`] or the retry wrapper as they prefer.
23
24use std::collections::HashMap;
25use std::future::Future;
26use std::time::Duration;
27
28use progenitor_client::ClientInfo;
29use reqwest::header::{HeaderMap, HeaderValue};
30use serde::{Deserialize, Serialize};
31use tokio::sync::OnceCell;
32
33use crate::api;
34use crate::errors::{Error, Result};
35use crate::retry::{with_backoff, RetryPolicy};
36
37/// Default connect timeout for the underlying reqwest::Client.
38const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
39/// Default total request timeout (connect + body read).
40const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
41/// Hard cap on any single response body we read (16 MiB). TaskFast
42/// responses are JSON envelopes — the largest realistic payload is a
43/// task list page, ~KBs. A 16-MiB ceiling gives generous headroom
44/// while preventing a compromised (or buggy) server from memory-
45/// exhausting the CLI with a gigabyte body.
46const MAX_RESPONSE_BYTES: usize = 16 * 1024 * 1024;
47
48/// Authenticated TaskFast API client.
49pub struct TaskFastClient {
50 inner: api::Client,
51 policy: RetryPolicy,
52 /// Per-client cache of the public `GET /config/network` payload.
53 /// Fetched lazily on first access; immutable per deployment, so a
54 /// process-lifetime cache is sufficient.
55 network_cfg: OnceCell<NetworkConfigResponse>,
56}
57
58impl TaskFastClient {
59 /// Construct a client from an API key. The key is sent as `X-API-Key`
60 /// on every request; the underlying reqwest::Client marks the header
61 /// sensitive so it won't appear in debug traces.
62 ///
63 /// `base_url` is the API host (e.g. `https://api.taskfast.app`). The
64 /// generated client appends the unprefixed path keys from the spec.
65 /// Callers pass hosts, not versioned paths; a trailing `/` is tolerated.
66 pub fn from_api_key(base_url: &str, api_key: &str) -> Result<Self> {
67 let mut value = HeaderValue::from_str(api_key)
68 .map_err(|_| Error::Auth("api key contains invalid header bytes".into()))?;
69 value.set_sensitive(true);
70 let mut headers = HeaderMap::new();
71 headers.insert("X-API-Key", value);
72
73 // F5: pin redirect policy to `none`. reqwest's default follows up
74 // to 10 redirects. `Authorization` is stripped cross-host by
75 // reqwest, but `X-API-Key` is a custom header so it would be
76 // replayed verbatim to whatever 3xx `Location` the server points
77 // at — exfiltrating the PAT on a single malicious redirect. The
78 // real API never 3xx's, so refusing all redirects is safe: a 302
79 // from `api.taskfast.app` is itself the anomaly we want to catch.
80 let http = reqwest::ClientBuilder::new()
81 .default_headers(headers)
82 .connect_timeout(DEFAULT_CONNECT_TIMEOUT)
83 .timeout(DEFAULT_REQUEST_TIMEOUT)
84 .redirect(reqwest::redirect::Policy::none())
85 .build()?;
86
87 let resolved = base_url.trim_end_matches('/').to_owned();
88
89 Ok(Self {
90 inner: api::Client::new_with_client(&resolved, http),
91 policy: RetryPolicy::default(),
92 network_cfg: OnceCell::new(),
93 })
94 }
95
96 /// Underlying reqwest::Client, pre-configured with the `X-API-Key` header.
97 /// Lets callers build requests outside the progenitor-generated surface
98 /// (e.g. to forward a JSON-RPC body to `POST /rpc/{network}` via the
99 /// same connection pool + default headers).
100 pub fn http_client(&self) -> reqwest::Client {
101 self.inner.client().clone()
102 }
103
104 /// Override the retry policy (default: [`RetryPolicy::default`]).
105 pub fn with_retry_policy(mut self, policy: RetryPolicy) -> Self {
106 self.policy = policy;
107 self
108 }
109
110 /// Direct access to the generated typed client. Use to invoke endpoint
111 /// methods; combine with [`map_api_error`] to surface typed errors.
112 pub fn inner(&self) -> &api::Client {
113 &self.inner
114 }
115
116 /// The retry policy in effect for [`Self::call_with_retry`].
117 pub fn retry_policy(&self) -> RetryPolicy {
118 self.policy
119 }
120
121 /// Run `op` under the configured retry policy. `op` receives the attempt
122 /// number (1-indexed) and should produce a typed [`Result`]. Typical use:
123 ///
124 /// ```ignore
125 /// let profile = client.call_with_retry(|_| async {
126 /// client.inner().get_agent_profile().await
127 /// .map(|r| r.into_inner())
128 /// .map_err(map_api_error).await // cannot await inside map_err
129 /// }).await?;
130 /// ```
131 ///
132 /// The callsite ergonomics bite is that [`map_api_error`] is async (it
133 /// reads the response body). Callers should match the Result themselves:
134 ///
135 /// ```ignore
136 /// client.call_with_retry(|_| async {
137 /// match client.inner().get_agent_profile().await {
138 /// Ok(ok) => Ok(ok.into_inner()),
139 /// Err(e) => Err(map_api_error(e).await),
140 /// }
141 /// }).await
142 /// ```
143 pub async fn call_with_retry<T, F, Fut>(&self, op: F) -> Result<T>
144 where
145 F: FnMut(u32) -> Fut,
146 Fut: Future<Output = Result<T>>,
147 {
148 with_backoff(self.policy, op).await
149 }
150
151 /// `GET /users/me` — fetch the human owner's display name + email.
152 ///
153 /// Hand-rolled because the endpoint is not yet in `spec/openapi.yaml`
154 /// (server-side work in flight). 404 is promoted to `Ok(None)` so
155 /// callers can gracefully fall back when running against an older
156 /// server deployment; every other non-2xx is surfaced via the shared
157 /// `classify_response` funnel.
158 pub async fn get_user_profile(&self) -> Result<Option<UserProfile>> {
159 let url = format!("{}/users/me", self.inner.baseurl());
160 let resp = self.inner.client().get(url).send().await?;
161 let status = resp.status();
162 if status.is_success() {
163 let bytes = read_body_capped(resp, MAX_RESPONSE_BYTES).await?;
164 return Ok(Some(serde_json::from_slice::<UserProfile>(&bytes)?));
165 }
166 if status == reqwest::StatusCode::NOT_FOUND {
167 return Ok(None);
168 }
169 Err(classify_response(resp).await)
170 }
171
172 /// Upload a file artifact to `POST /tasks/{task_id}/artifacts`.
173 ///
174 /// Hand-rolled because progenitor's default templates skip `multipart/
175 /// form-data` endpoints. We reuse the inner reqwest::Client (same auth
176 /// headers, same timeouts) and funnel non-2xx responses through the
177 /// same `classify_response` used for generated methods — so upload
178 /// errors map to the same `Error` variants as every other call.
179 pub async fn upload_artifact(
180 &self,
181 task_id: &uuid::Uuid,
182 filename: String,
183 content_type: String,
184 bytes: Vec<u8>,
185 ) -> Result<api::types::Artifact> {
186 let url = format!("{}/tasks/{}/artifacts", self.inner.baseurl(), task_id);
187 let part = reqwest::multipart::Part::bytes(bytes)
188 .file_name(filename)
189 .mime_str(&content_type)
190 .map_err(|e| Error::Server(format!("invalid content-type: {e}")))?;
191 let form = reqwest::multipart::Form::new().part("file", part);
192 let resp = self.inner.client().post(url).multipart(form).send().await?;
193 if resp.status().is_success() {
194 let bytes = read_body_capped(resp, MAX_RESPONSE_BYTES).await?;
195 Ok(serde_json::from_slice::<api::types::Artifact>(&bytes)?)
196 } else {
197 Err(classify_response(resp).await)
198 }
199 }
200
201 /// `GET /config/network` — per-network chain metadata.
202 ///
203 /// Public endpoint, no auth required; the `X-API-Key` header attached by
204 /// `from_api_key` is harmless on the server side. Result is cached on
205 /// this client instance for the rest of the process — the payload is
206 /// immutable per deployment (operator-side config changes require a
207 /// redeploy to take effect).
208 ///
209 /// Each entry's `rpc_url` points at this same deployment's authenticated
210 /// `POST /rpc/{network}` proxy, NOT the upstream Tempo gateway.
211 pub async fn fetch_network_config(&self) -> Result<&NetworkConfigResponse> {
212 self.network_cfg
213 .get_or_try_init(|| async {
214 let url = format!("{}/config/network", self.inner.baseurl());
215 let resp = self.inner.client().get(url).send().await?;
216 if !resp.status().is_success() {
217 return Err(classify_response(resp).await);
218 }
219 let bytes = read_body_capped(resp, MAX_RESPONSE_BYTES).await?;
220 Ok(serde_json::from_slice::<NetworkConfigResponse>(&bytes)?)
221 })
222 .await
223 }
224
225 /// `POST /rpc/{network}` — forward a JSON-RPC call through the
226 /// operator's upstream proxy.
227 ///
228 /// `network` is the key into the `fetch_network_config` map
229 /// (`"testnet"` / `"mainnet"`). Response body is returned verbatim as
230 /// [`serde_json::Value`] — the proxy forwards the upstream JSON-RPC
231 /// envelope without touching it, so callers parse `result` / `error`
232 /// per the JSON-RPC 2.0 spec.
233 ///
234 /// 429 surfaces as [`Error::RateLimited`] with the `Retry-After`
235 /// header honored by `classify_response`; 5xx as [`Error::Server`].
236 pub async fn post_json_rpc(
237 &self,
238 network: &str,
239 request: &serde_json::Value,
240 ) -> Result<serde_json::Value> {
241 let url = format!("{}/rpc/{}", self.inner.baseurl(), network);
242 let resp = self.inner.client().post(url).json(request).send().await?;
243 if !resp.status().is_success() {
244 return Err(classify_response(resp).await);
245 }
246 let bytes = read_body_capped(resp, MAX_RESPONSE_BYTES).await?;
247 Ok(serde_json::from_slice::<serde_json::Value>(&bytes)?)
248 }
249
250 /// `GET /agents/me/events` — raw JSON escape hatch.
251 ///
252 /// Mirrors the generated `list_agent_events` but returns
253 /// [`serde_json::Value`] instead of the strict [`api::types::AgentEventListResponse`]
254 /// so the agent layer can apply per-item tolerant decoding when a
255 /// single malformed event would otherwise fail the whole page.
256 /// Non-2xx responses still funnel through `classify_response`.
257 pub async fn list_agent_events_raw(
258 &self,
259 cursor: Option<&str>,
260 limit: Option<i64>,
261 ) -> Result<serde_json::Value> {
262 let url = format!("{}/agents/me/events", self.inner.baseurl());
263 let mut req = self.inner.client().get(url);
264 let mut query: Vec<(&str, String)> = Vec::new();
265 if let Some(c) = cursor {
266 query.push(("cursor", c.to_string()));
267 }
268 if let Some(l) = limit {
269 query.push(("limit", l.to_string()));
270 }
271 if !query.is_empty() {
272 req = req.query(&query);
273 }
274 let resp = req.send().await?;
275 if !resp.status().is_success() {
276 return Err(classify_response(resp).await);
277 }
278 let bytes = read_body_capped(resp, MAX_RESPONSE_BYTES).await?;
279 Ok(serde_json::from_slice::<serde_json::Value>(&bytes)?)
280 }
281}
282
283/// Stream `resp` body chunks into memory, refusing to allocate past `cap`.
284///
285/// Reqwest offers no native body-size cap on the ClientBuilder. Wrapping
286/// `chunk()` is the cheap route: each call yields whatever reqwest has
287/// buffered, so we can bail as soon as the running total would exceed
288/// the ceiling — no need for a trusted `Content-Length` header (chunked
289/// bodies from a malicious server carry none).
290async fn read_body_capped(mut resp: reqwest::Response, cap: usize) -> Result<Vec<u8>> {
291 let mut buf = Vec::new();
292 while let Some(chunk) = resp.chunk().await? {
293 if buf.len().saturating_add(chunk.len()) > cap {
294 return Err(Error::Server(format!(
295 "response body exceeds {cap}-byte safety cap"
296 )));
297 }
298 buf.extend_from_slice(&chunk);
299 }
300 Ok(buf)
301}
302
303/// Translate a progenitor [`api::Error<()>`] into a typed [`Error`].
304///
305/// Async because the `UnexpectedResponse` path has to consume the response
306/// body to extract TaskFast's `{error, message}` envelope.
307pub async fn map_api_error(err: api::Error<()>) -> Error {
308 use api::Error as AE;
309 match err {
310 AE::InvalidRequest(m) => Error::Server(format!("invalid request: {m}")),
311 AE::CommunicationError(e) => Error::Network(e),
312 AE::InvalidUpgrade(e) => Error::Network(e),
313 AE::ResponseBodyError(e) => Error::Network(e),
314 AE::InvalidResponsePayload(_, e) => Error::Decode(e),
315 AE::UnexpectedResponse(resp) => classify_response(resp).await,
316 AE::ErrorResponse(_) => {
317 // Unreachable with stripped-non-2xx normalization (E = ()), but we
318 // refuse to panic — produce a descriptive Server error instead.
319 Error::Server(
320 "progenitor emitted typed ErrorResponse for E=() — spec normalization regression"
321 .into(),
322 )
323 }
324 // progenitor 0.14 collapsed the former Pre/PostHookError variants
325 // into a single Custom variant for consumer-defined hook failures.
326 AE::Custom(m) => Error::Server(format!("hook: {m}")),
327 }
328}
329
330/// Per-network chain configuration returned by `GET /config/network`.
331///
332/// Keys of `networks` are network names (`"testnet"`, `"mainnet"`);
333/// networks the deployment does not support are simply absent.
334#[derive(Debug, Clone, Deserialize, Serialize)]
335pub struct NetworkConfigResponse {
336 /// Network name → chain metadata.
337 pub networks: HashMap<String, NetworkConfigEntry>,
338}
339
340impl NetworkConfigResponse {
341 /// Return the entry for `network` name or an `Error::Validation` if
342 /// the deployment does not advertise it.
343 pub fn entry(&self, network: &str) -> Result<&NetworkConfigEntry> {
344 self.networks.get(network).ok_or_else(|| Error::Validation {
345 code: "unknown_network".to_string(),
346 message: format!("deployment does not advertise network `{network}`"),
347 })
348 }
349
350 /// Reverse-lookup: find the entry whose `chain_id` matches. Used when
351 /// the caller knows the chain ID (e.g. from escrow params) but not
352 /// the network name. Errors if no entry matches.
353 pub fn entry_by_chain_id(&self, chain_id: i64) -> Result<(&str, &NetworkConfigEntry)> {
354 self.networks
355 .iter()
356 .find(|(_, e)| e.chain_id == chain_id)
357 .map(|(name, entry)| (name.as_str(), entry))
358 .ok_or_else(|| Error::Validation {
359 code: "unknown_chain_id".to_string(),
360 message: format!("no network in /config/network advertises chain_id={chain_id}"),
361 })
362 }
363}
364
365/// Chain metadata for a single Tempo network.
366#[derive(Debug, Clone, Deserialize, Serialize)]
367pub struct NetworkConfigEntry {
368 /// EVM chain ID (e.g. `4217` for Tempo mainnet, `42_431` for moderato testnet).
369 pub chain_id: i64,
370 /// Authenticated JSON-RPC proxy URL on this deployment
371 /// (`{api_base}/rpc/{network}`). NOT the upstream Tempo gateway.
372 pub rpc_url: String,
373 /// WebSocket JSON-RPC URL on the native Tempo gateway (no proxy).
374 pub wss_url: String,
375 /// Block-explorer base URL for this network.
376 pub explorer_url: String,
377 /// Default stablecoin ticker; `None` when the deployment has not
378 /// finalized a token for the network (e.g. mainnet pre-launch).
379 pub default_stablecoin: Option<String>,
380}
381
382/// Owning human's display name + email, returned by `GET /users/me`.
383/// Lives alongside the hand-rolled fetch until the endpoint is added to
384/// `spec/openapi.yaml` and the type can be regenerated under
385/// [`crate::api::types`].
386#[derive(Debug, Clone, Deserialize)]
387pub struct UserProfile {
388 /// Display name for the owning human (e.g. `"Alice Smith"`).
389 pub name: String,
390 /// Owning human's email address.
391 pub email: String,
392}
393
394#[derive(Debug, Deserialize)]
395struct ErrorBody {
396 /// TaskFast's `components/schemas/Error.error` — short code.
397 #[serde(default)]
398 error: String,
399 /// Human-readable detail.
400 #[serde(default)]
401 message: String,
402}
403
404/// Classify a non-2xx response by status code, reading the body to extract
405/// TaskFast's `{error, message}` envelope when relevant.
406///
407/// Body read + JSON parse are best-effort: a transport failure mid-body or a
408/// non-JSON payload collapses to an empty envelope (status line surfaces in
409/// `Display`). Both failure modes emit a `tracing::warn` so silent classifier
410/// drops are observable in logs.
411async fn classify_response(resp: reqwest::Response) -> Error {
412 let status = resp.status();
413 let code = status.as_u16();
414 let retry_after = parse_retry_after(&resp);
415 // Cap the error body read at 64 KiB. TaskFast error envelopes are
416 // `{error, message}` JSON objects — a megabyte-class body on a 4xx
417 // is an abuse signal, not a legitimate payload. Smaller cap than
418 // success bodies: error paths never carry task-list pages.
419 let body = match read_body_capped(resp, 64 * 1024).await {
420 Ok(b) => String::from_utf8_lossy(&b).into_owned(),
421 Err(e) => {
422 tracing::warn!(error = %e.kind(), %code, "classify_response: failed to read body");
423 String::new()
424 }
425 };
426 let parsed: ErrorBody = match serde_json::from_str(&body) {
427 Ok(p) => p,
428 Err(e) => {
429 if !body.is_empty() {
430 tracing::warn!(error = %e, %code, "classify_response: body is not JSON; using raw text");
431 }
432 ErrorBody {
433 error: String::new(),
434 message: body,
435 }
436 }
437 };
438
439 match code {
440 401 | 403 => Error::Auth(format_status(
441 code,
442 display_or_status(&parsed, status.as_str()),
443 )),
444 422 => Error::Validation {
445 code: or_default(&parsed.error, "validation_error"),
446 message: or_default(&parsed.message, "request body failed validation"),
447 },
448 429 => Error::RateLimited {
449 retry_after: retry_after.unwrap_or(Duration::from_secs(1)),
450 },
451 500..=599 => Error::Server(format_status(
452 code,
453 display_or_status(&parsed, status.as_str()),
454 )),
455 // 4xx other than the above (404, 409, etc.) — progenitor would have
456 // surfaced this as UnexpectedResponse because we stripped non-2xx from
457 // the spec. Treat as a validation-ish failure so callers can branch.
458 400..=499 => Error::Validation {
459 code: or_default(&parsed.error, status.as_str()),
460 message: or_default(
461 &parsed.message,
462 status.canonical_reason().unwrap_or("client error"),
463 ),
464 },
465 _ => Error::Server(format_status(
466 code,
467 display_or_status(&parsed, status.as_str()),
468 )),
469 }
470}
471
472fn parse_retry_after(resp: &reqwest::Response) -> Option<Duration> {
473 let v = resp.headers().get(reqwest::header::RETRY_AFTER)?;
474 let s = v.to_str().ok()?.trim();
475 // Only seconds form supported for now; HTTP-date form can land later.
476 s.parse::<u64>().ok().map(Duration::from_secs)
477}
478
479fn or_default(s: &str, fallback: &str) -> String {
480 if s.is_empty() {
481 fallback.to_string()
482 } else {
483 s.to_string()
484 }
485}
486
487fn display_or_status<'a>(body: &'a ErrorBody, status: &'a str) -> &'a str {
488 if !body.message.is_empty() {
489 &body.message
490 } else if !body.error.is_empty() {
491 &body.error
492 } else {
493 status
494 }
495}
496
497fn format_status(code: u16, detail: &str) -> String {
498 format!("HTTP {code}: {detail}")
499}