Skip to main content

taskfast_cli/cmd/
message.rs

1// SPDX-License-Identifier: MIT
2//! `taskfast message` — send + read task-scoped messages.
3//!
4//! Conversations are keyed by task; `list-conversations` returns the task's
5//! distinct participant threads, `list` returns messages in a thread, and
6//! `send` appends a new message. The API is symmetric between poster and
7//! worker — server infers role from the API key.
8
9use clap::{Parser, Subcommand};
10use serde_json::json;
11use uuid::Uuid;
12
13use super::{CmdError, CmdResult, Ctx};
14use crate::envelope::Envelope;
15
16use taskfast_client::api::types::{MessageSendRequest, MessageSendRequestContent};
17use taskfast_client::map_api_error;
18
19#[derive(Debug, Subcommand)]
20pub enum Command {
21    /// Send a message on a task.
22    Send(SendArgs),
23    /// List messages in a task's thread.
24    List(ListArgs),
25    /// List the task's distinct conversations.
26    Conversations(ConversationsArgs),
27}
28
29#[derive(Debug, Parser)]
30pub struct SendArgs {
31    pub task_id: String,
32
33    /// Message body (plain text). Accepts whatever the server allows — no
34    /// local length cap because server-side policy has historically changed.
35    #[arg(long)]
36    pub content: String,
37}
38
39#[derive(Debug, Parser)]
40pub struct ListArgs {
41    pub task_id: String,
42
43    #[arg(long)]
44    pub cursor: Option<String>,
45
46    #[arg(long)]
47    pub limit: Option<i64>,
48}
49
50#[derive(Debug, Parser)]
51pub struct ConversationsArgs {
52    pub task_id: String,
53}
54
55pub async fn run(ctx: &Ctx, cmd: Command) -> CmdResult {
56    match cmd {
57        Command::Send(a) => send(ctx, a).await,
58        Command::List(a) => list(ctx, a).await,
59        Command::Conversations(a) => conversations(ctx, a).await,
60    }
61}
62
63async fn send(ctx: &Ctx, args: SendArgs) -> CmdResult {
64    let task_id = Uuid::parse_str(&args.task_id)
65        .map_err(|e| CmdError::Usage(format!("task id must be a UUID: {e}")))?;
66    if args.content.trim().is_empty() {
67        return Err(CmdError::Usage("--content must not be empty".into()));
68    }
69    let content = MessageSendRequestContent::try_from(args.content.as_str())
70        .map_err(|e| CmdError::Usage(format!("--content rejected by schema: {e}")))?;
71
72    if ctx.dry_run {
73        return Ok(Envelope::success(
74            ctx.environment,
75            ctx.dry_run,
76            json!({
77                "action": "would_send_message",
78                "task_id": task_id.to_string(),
79                "content": args.content,
80            }),
81        ));
82    }
83
84    let body = MessageSendRequest { content };
85    let client = ctx.client()?;
86    let resp = match client.inner().send_message(&task_id, &body).await {
87        Ok(v) => v.into_inner(),
88        Err(e) => return Err(map_api_error(e).await.into()),
89    };
90    Ok(Envelope::success(
91        ctx.environment,
92        ctx.dry_run,
93        json!({ "message": resp }),
94    ))
95}
96
97async fn list(ctx: &Ctx, args: ListArgs) -> CmdResult {
98    let task_id = Uuid::parse_str(&args.task_id)
99        .map_err(|e| CmdError::Usage(format!("task id must be a UUID: {e}")))?;
100    let client = ctx.client()?;
101    let resp = match client
102        .inner()
103        .list_messages(
104            &task_id,
105            args.cursor.as_deref(),
106            args.limit.and_then(taskfast_client::page_limit),
107        )
108        .await
109    {
110        Ok(v) => v.into_inner(),
111        Err(e) => return Err(map_api_error(e).await.into()),
112    };
113    Ok(Envelope::success(
114        ctx.environment,
115        ctx.dry_run,
116        json!({
117            "task_id": task_id.to_string(),
118            "messages": resp.data,
119            "meta": resp.meta,
120        }),
121    ))
122}
123
124async fn conversations(ctx: &Ctx, args: ConversationsArgs) -> CmdResult {
125    let task_id = Uuid::parse_str(&args.task_id)
126        .map_err(|e| CmdError::Usage(format!("task id must be a UUID: {e}")))?;
127    let client = ctx.client()?;
128    let resp = match client.inner().list_conversations(&task_id).await {
129        Ok(v) => v.into_inner(),
130        Err(e) => return Err(map_api_error(e).await.into()),
131    };
132    Ok(Envelope::success(
133        ctx.environment,
134        ctx.dry_run,
135        json!({
136            "task_id": task_id.to_string(),
137            "conversations": resp,
138        }),
139    ))
140}