Skip to main content

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. `--assignment-type` exists
11//! mainly so subcontractor flows can target `direct` offers.
12//!
13//! Mirror of `cmd::task::list --kind=posted` from the poster side, but for
14//! workers looking for inbound work. Kept as a top-level verb (not nested
15//! under `task list --kind=open`) because it matches the skill doc's
16//! mental model: "discover" is a distinct phase from "manage my assigned
17//! workload".
18
19use clap::{Parser, ValueEnum};
20use serde_json::json;
21
22use super::{CmdResult, Ctx};
23use crate::envelope::Envelope;
24
25use taskfast_client::api::types::{ListOpenTasksAssignmentType, ListOpenTasksStatus};
26use taskfast_client::map_api_error;
27
28#[derive(Debug, Parser)]
29pub struct Args {
30    /// Filter to `open` (accepting bids) or `bidding` (bids received, not yet
31    /// accepted). Without this, both are returned.
32    #[arg(long)]
33    pub status: Option<DiscoverStatus>,
34
35    /// Filter to `open` auctions or `direct` invitations.
36    #[arg(long)]
37    pub assignment_type: Option<DiscoverAssignmentType>,
38
39    /// Required capability tag. Repeat for multiple (AND semantics server-side).
40    #[arg(long = "capability", value_name = "CAP")]
41    pub capabilities: Vec<String>,
42
43    /// Maximum budget ceiling (inclusive). Decimal.
44    #[arg(long)]
45    pub budget_max: Option<f64>,
46
47    /// Minimum budget floor (inclusive). Decimal.
48    #[arg(long)]
49    pub budget_min: Option<f64>,
50
51    #[arg(long)]
52    pub cursor: Option<String>,
53
54    /// Max tasks per page. Defaults to 50 — predictable batch size for
55    /// agent worker loops without accidentally pulling a huge backlog.
56    #[arg(long, default_value_t = 50)]
57    pub limit: i64,
58}
59
60/// clap-friendly mirror of `ListOpenTasksStatus`.
61#[derive(Debug, Clone, Copy, ValueEnum)]
62pub enum DiscoverStatus {
63    Open,
64    Bidding,
65}
66
67impl From<DiscoverStatus> for ListOpenTasksStatus {
68    fn from(s: DiscoverStatus) -> Self {
69        match s {
70            DiscoverStatus::Open => Self::Open,
71            DiscoverStatus::Bidding => Self::Bidding,
72        }
73    }
74}
75
76/// clap-friendly mirror of `ListOpenTasksAssignmentType`.
77#[derive(Debug, Clone, Copy, ValueEnum)]
78pub enum DiscoverAssignmentType {
79    Open,
80    Direct,
81}
82
83impl From<DiscoverAssignmentType> for ListOpenTasksAssignmentType {
84    fn from(a: DiscoverAssignmentType) -> Self {
85        match a {
86            DiscoverAssignmentType::Open => Self::Open,
87            DiscoverAssignmentType::Direct => Self::Direct,
88        }
89    }
90}
91
92pub async fn run(ctx: &Ctx, args: Args) -> CmdResult {
93    let client = ctx.client()?;
94    let capabilities_vec: Option<Vec<String>> = if args.capabilities.is_empty() {
95        None
96    } else {
97        Some(args.capabilities.clone())
98    };
99    let resp = match client
100        .inner()
101        .list_open_tasks(
102            args.assignment_type.map(Into::into),
103            args.budget_max,
104            args.budget_min,
105            capabilities_vec.as_ref(),
106            args.cursor.as_deref(),
107            taskfast_client::page_limit(args.limit),
108            args.status.map(Into::into),
109        )
110        .await
111    {
112        Ok(v) => v.into_inner(),
113        Err(e) => return Err(map_api_error(e).await.into()),
114    };
115    Ok(Envelope::success(
116        ctx.environment,
117        ctx.dry_run,
118        json!({
119            "tasks": resp.data,
120            "meta": resp.meta,
121        }),
122    ))
123}