Skip to main content

taskfast_cli/cmd/
agent.rs

1// SPDX-License-Identifier: MIT
2//! `taskfast agent` — directory lookups + profile self-service.
3//!
4//! `list` → `GET /agents` (capability-filtered agent directory, for
5//! poster flows that target direct assignment). `get <agent_id>` → fetch
6//! a single public profile. `update-me` → `PUT /agents/me` to edit the
7//! caller's own profile fields (name, description, capabilities, rate,
8//! spend caps). Every field on `update-me` is optional; only supplied
9//! fields are sent.
10
11use clap::{Parser, Subcommand};
12use serde_json::json;
13use uuid::Uuid;
14
15use super::{CmdError, CmdResult, Ctx};
16use crate::envelope::Envelope;
17
18use taskfast_client::api::types::AgentProfileUpdate;
19use taskfast_client::map_api_error;
20
21#[derive(Debug, Subcommand)]
22pub enum Command {
23    /// List agents (optionally filtered by capability).
24    List(ListArgs),
25    /// Get the public profile of an agent by ID.
26    Get(GetArgs),
27    /// Update the caller's own profile.
28    UpdateMe(UpdateMeArgs),
29}
30
31#[derive(Debug, Parser)]
32pub struct ListArgs {
33    /// Filter by capability tag.
34    #[arg(long)]
35    pub capability: Option<String>,
36
37    #[arg(long)]
38    pub cursor: Option<String>,
39
40    #[arg(long)]
41    pub limit: Option<i64>,
42}
43
44#[derive(Debug, Parser)]
45pub struct GetArgs {
46    pub agent_id: String,
47}
48
49#[derive(Debug, Parser)]
50pub struct UpdateMeArgs {
51    #[arg(long)]
52    pub name: Option<String>,
53
54    #[arg(long)]
55    pub description: Option<String>,
56
57    /// Capability tag. Repeat for multiple (replaces the full list).
58    #[arg(long = "capability", value_name = "CAP")]
59    pub capabilities: Vec<String>,
60
61    /// Default price per task (decimal string).
62    #[arg(long)]
63    pub rate: Option<String>,
64
65    /// Maximum budget this agent can post on a task (decimal string).
66    #[arg(long)]
67    pub max_task_budget: Option<String>,
68
69    /// Rolling 24-hour spend cap (decimal string).
70    #[arg(long)]
71    pub daily_spend_limit: Option<String>,
72}
73
74pub async fn run(ctx: &Ctx, cmd: Command) -> CmdResult {
75    match cmd {
76        Command::List(a) => list(ctx, a).await,
77        Command::Get(a) => get(ctx, a).await,
78        Command::UpdateMe(a) => update_me(ctx, a).await,
79    }
80}
81
82async fn list(ctx: &Ctx, args: ListArgs) -> CmdResult {
83    let client = ctx.client()?;
84    let resp = match client
85        .inner()
86        .list_agents(
87            args.capability.as_deref(),
88            args.cursor.as_deref(),
89            args.limit.and_then(taskfast_client::page_limit),
90        )
91        .await
92    {
93        Ok(v) => v.into_inner(),
94        Err(e) => return Err(map_api_error(e).await.into()),
95    };
96    Ok(Envelope::success(
97        ctx.environment,
98        ctx.dry_run,
99        json!({
100            "agents": resp.agents,
101            "next_cursor": resp.next_cursor,
102        }),
103    ))
104}
105
106async fn get(ctx: &Ctx, args: GetArgs) -> CmdResult {
107    let agent_id = Uuid::parse_str(&args.agent_id)
108        .map_err(|e| CmdError::Usage(format!("agent id must be a UUID: {e}")))?;
109    let client = ctx.client()?;
110    let resp = match client.inner().get_agent(&agent_id).await {
111        Ok(v) => v.into_inner(),
112        Err(e) => return Err(map_api_error(e).await.into()),
113    };
114    Ok(Envelope::success(
115        ctx.environment,
116        ctx.dry_run,
117        json!({ "agent": resp }),
118    ))
119}
120
121async fn update_me(ctx: &Ctx, args: UpdateMeArgs) -> CmdResult {
122    // Require at least one intent so we never issue an empty-update PUT.
123    let nothing_set = args.name.is_none()
124        && args.description.is_none()
125        && args.rate.is_none()
126        && args.max_task_budget.is_none()
127        && args.daily_spend_limit.is_none()
128        && args.capabilities.is_empty();
129    if nothing_set {
130        return Err(CmdError::Usage(
131            "update-me requires at least one of: --name --description \
132             --capability --rate --max-task-budget --daily-spend-limit"
133                .into(),
134        ));
135    }
136
137    let body = AgentProfileUpdate {
138        capabilities: args.capabilities.clone(),
139        daily_spend_limit: args.daily_spend_limit.clone(),
140        description: args.description.clone(),
141        max_task_budget: args.max_task_budget.clone(),
142        name: args.name.clone(),
143        rate: args.rate.clone(),
144    };
145
146    if ctx.dry_run {
147        return Ok(Envelope::success(
148            ctx.environment,
149            ctx.dry_run,
150            json!({
151                "action": "would_update_agent_profile",
152                "body": body,
153            }),
154        ));
155    }
156
157    let client = ctx.client()?;
158    let resp = match client.inner().update_agent_profile(&body).await {
159        Ok(v) => v.into_inner(),
160        Err(e) => return Err(map_api_error(e).await.into()),
161    };
162    Ok(Envelope::success(
163        ctx.environment,
164        ctx.dry_run,
165        json!({ "agent": resp }),
166    ))
167}