Skip to main content

objectiveai_sdk/cli/command/tasks/delete/
mod.rs

1//! `tasks delete` — remove a task by id, resolved within a plugin
2//! NAMESPACE. By default the namespace is the CALLER's own plugin
3//! identity; `--no-plugin` targets the non-plugin (all-NULL)
4//! namespace explicitly, and the `--plugin-owner`/`--plugin-name`/
5//! `--plugin-version` trio targets a specific plugin's namespace.
6//! This is plain identity resolution, NOT authentication — a caller
7//! may name any namespace's id. Idempotent: deleting an absent id
8//! reports `deleted: false`, never an error. A task deleted mid-run
9//! finishes its in-flight run but records nothing and never fires
10//! again.
11
12use crate::cli::command::CommandRequest;
13
14/// Which plugin namespace `tasks delete` resolves the id within.
15#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
16#[serde(tag = "namespace", rename_all = "snake_case")]
17#[schemars(rename = "cli.command.tasks.delete.DeleteNamespace")]
18pub enum DeleteNamespace {
19    /// Default: the caller's own plugin identity (the caller's trio,
20    /// or the all-NULL namespace when the caller isn't a plugin).
21    #[schemars(title = "Caller")]
22    Caller,
23    /// The non-plugin (all-NULL) namespace, explicitly.
24    #[schemars(title = "NoPlugin")]
25    NoPlugin,
26    /// A specific plugin's namespace.
27    #[schemars(title = "Plugin")]
28    Plugin {
29        owner: String,
30        name: String,
31        version: String,
32    },
33}
34
35impl Default for DeleteNamespace {
36    fn default() -> Self {
37        Self::Caller
38    }
39}
40
41#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
42#[schemars(rename = "cli.command.tasks.delete.Request")]
43pub struct Request {
44    pub path_type: Path,
45    /// The task id.
46    pub id: String,
47    /// The namespace the id resolves within — defaults to the
48    /// caller's own plugin identity.
49    #[serde(default)]
50    pub namespace: DeleteNamespace,
51    #[serde(flatten)]
52    pub base: crate::cli::command::RequestBase,
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
56#[schemars(rename = "cli.command.tasks.delete.Path")]
57pub enum Path {
58    #[serde(rename = "tasks/delete")]
59    TasksDelete,
60}
61
62impl CommandRequest for Request {
63    fn request_base(&self) -> &crate::cli::command::RequestBase {
64        &self.base
65    }
66
67    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
68        Some(&mut self.base)
69    }
70}
71
72#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
73#[schemars(rename = "cli.command.tasks.delete.Response")]
74pub struct Response {
75    /// Whether a task was actually removed (`false` = no such id).
76    pub deleted: bool,
77}
78
79#[derive(clap::Args)]
80#[command(group(clap::ArgGroup::new("id_required").required(true).args(["id"])))]
81pub struct Args {
82    /// The task id.
83    #[arg(long)]
84    pub id: Option<String>,
85    /// Resolve the id in the non-plugin (all-NULL) namespace instead
86    /// of the caller's. Mutually exclusive with the plugin trio.
87    #[arg(long, conflicts_with_all = ["plugin_owner", "plugin_name", "plugin_version"])]
88    pub no_plugin: bool,
89    /// Resolve the id in this plugin's namespace instead of the
90    /// caller's. All three of owner/name/version are required
91    /// together.
92    #[arg(long, requires_all = ["plugin_name", "plugin_version"])]
93    pub plugin_owner: Option<String>,
94    #[arg(long, requires_all = ["plugin_owner", "plugin_version"])]
95    pub plugin_name: Option<String>,
96    #[arg(long, requires_all = ["plugin_owner", "plugin_name"])]
97    pub plugin_version: Option<String>,
98    #[command(flatten)]
99    pub base: crate::cli::command::RequestBaseArgs,
100}
101
102#[derive(clap::Args)]
103#[command(args_conflicts_with_subcommands = true)]
104pub struct Command {
105    #[command(flatten)]
106    pub args: Args,
107    #[command(subcommand)]
108    pub schema: Option<Schema>,
109}
110
111#[derive(clap::Subcommand)]
112pub enum Schema {
113    /// Emit the JSON Schema for this leaf's `Request` type and exit.
114    RequestSchema(request_schema::Args),
115    /// Emit the JSON Schema for this leaf's `Response` type and exit.
116    ResponseSchema(response_schema::Args),
117}
118
119impl TryFrom<Args> for Request {
120    type Error = crate::cli::command::FromArgsError;
121    fn try_from(args: Args) -> Result<Self, Self::Error> {
122        let id = args.id.ok_or_else(|| {
123            crate::cli::command::FromArgsError::path_parse("id", "--id is required".to_string())
124        })?;
125        // clap enforces the trio-together + no_plugin-exclusivity; the
126        // match is exhaustive over the reachable combinations.
127        let namespace = match (args.no_plugin, args.plugin_owner, args.plugin_name, args.plugin_version) {
128            (true, _, _, _) => DeleteNamespace::NoPlugin,
129            (false, Some(owner), Some(name), Some(version)) => {
130                DeleteNamespace::Plugin { owner, name, version }
131            }
132            _ => DeleteNamespace::Caller,
133        };
134        Ok(Self {
135            path_type: Path::TasksDelete,
136            id,
137            namespace,
138            base: args.base.into(),
139        })
140    }
141}
142
143#[cfg(feature = "mcp")]
144impl crate::cli::command::CommandResponse for Response {
145    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
146        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
147    }
148}
149
150#[cfg(feature = "cli-executor")]
151pub async fn execute<E: crate::cli::command::CommandExecutor>(
152    executor: &E,
153    mut request: Request,
154    identity: Option<&crate::identity::Identity>,
155) -> Result<Response, E::Error> {
156    request.base.clear_transform();
157    executor.execute_one(request, identity).await
158}
159
160#[cfg(feature = "cli-executor")]
161pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
162    executor: &E,
163    mut request: Request,
164    transform: crate::cli::command::Transform,
165    identity: Option<&crate::identity::Identity>,
166) -> Result<serde_json::Value, E::Error> {
167    request.base.set_transform(transform);
168    executor.execute_one(request, identity).await
169}
170
171pub mod request_schema;
172
173pub mod response_schema;
174
175/// One `/listen` broadcast run of `tasks delete`: the actual
176/// [`Request`], the producer's
177/// [`Identity`](crate::identity::Identity), and the
178/// unary response future. See [`crate::daemon::command_listener`].
179#[cfg(all(feature = "cli", feature = "daemon"))]
180pub struct ListenerExecution {
181    pub request: Request,
182    pub identity: crate::identity::Identity,
183    pub response: crate::daemon::command_listener::UnaryResponse<Response>,
184}