taskfast_cli/cmd/discover.rs
1// SPDX-License-Identifier: MIT
2//! `taskfast discover` — browse the open-tasks marketplace.
3//!
4//! Wraps `GET /tasks` (operationId `listOpenTasks`). Unauthenticated in the
5//! sense that results aren't scoped to the caller, but an API key is still
6//! required — the server uses it for rate-limiting and relevance biasing.
7//!
8//! Exposes the filter triad workers reach for first: capabilities (title-
9//! keyword-ish match on the poster's side), budget range, and `--status`
10//! to carve out `open` vs. `bidding` listings.
11//!
12//! Mirror of `cmd::task::list --kind=posted` from the poster side, but for
13//! workers looking for inbound work. Kept as a top-level verb (not nested
14//! under `task list --kind=open`) because it matches the skill doc's
15//! mental model: "discover" is a distinct phase from "manage my assigned
16//! workload".
17
18use clap::{Parser, ValueEnum};
19use serde_json::json;
20
21use super::{CmdResult, Ctx};
22use crate::envelope::Envelope;
23
24use taskfast_client::api::types::ListOpenTasksStatus;
25use taskfast_client::map_api_error;
26
27#[derive(Debug, Parser)]
28pub struct Args {
29 /// Filter to `open` (accepting bids) or `bidding` (bids received, not yet
30 /// accepted). Without this, both are returned.
31 #[arg(long)]
32 pub status: Option<DiscoverStatus>,
33
34 /// Required capability tag. Repeat for multiple (AND semantics server-side).
35 #[arg(long = "capability", value_name = "CAP")]
36 pub capabilities: Vec<String>,
37
38 /// Maximum budget ceiling (inclusive). Decimal.
39 #[arg(long)]
40 pub budget_max: Option<f64>,
41
42 /// Minimum budget floor (inclusive). Decimal.
43 #[arg(long)]
44 pub budget_min: Option<f64>,
45
46 #[arg(long)]
47 pub cursor: Option<String>,
48
49 /// Max tasks per page. Defaults to 50 — predictable batch size for
50 /// agent worker loops without accidentally pulling a huge backlog.
51 #[arg(long, default_value_t = 50)]
52 pub limit: i64,
53}
54
55/// clap-friendly mirror of `ListOpenTasksStatus`.
56#[derive(Debug, Clone, Copy, ValueEnum)]
57pub enum DiscoverStatus {
58 Open,
59 Bidding,
60}
61
62impl From<DiscoverStatus> for ListOpenTasksStatus {
63 fn from(s: DiscoverStatus) -> Self {
64 match s {
65 DiscoverStatus::Open => Self::Open,
66 DiscoverStatus::Bidding => Self::Bidding,
67 }
68 }
69}
70
71pub async fn run(ctx: &Ctx, args: Args) -> CmdResult {
72 let client = ctx.client()?;
73 let capabilities_vec: Option<Vec<String>> = if args.capabilities.is_empty() {
74 None
75 } else {
76 Some(args.capabilities.clone())
77 };
78 let resp = match client
79 .inner()
80 .list_open_tasks(
81 None,
82 args.budget_max,
83 args.budget_min,
84 capabilities_vec.as_ref(),
85 args.cursor.as_deref(),
86 taskfast_client::page_limit(args.limit),
87 args.status.map(Into::into),
88 )
89 .await
90 {
91 Ok(v) => v.into_inner(),
92 Err(e) => return Err(map_api_error(e).await.into()),
93 };
94 Ok(Envelope::success(
95 ctx.environment,
96 ctx.dry_run,
97 json!({
98 "tasks": resp.data,
99 "meta": resp.meta,
100 }),
101 ))
102}