objectiveai_sdk/cli/command/tasks/run/mod.rs
1//! `agents tasks run` — fire every pending schedule in the caller's
2//! own subtree.
3//!
4//! Scope is fixed: every schedule whose `agent_instance_hierarchy` is
5//! the caller's own AIH or a descendant of it. Of those, the runner
6//! claims the pending ones (unfired oneshots + interval rows whose
7//! interval has elapsed — newest versions only), minting one
8//! `tasks_runs` row per firing, and dispatches each row's stored argv
9//! through the root `crate::run` in parallel — with the schedule's
10//! captured identity (and the plugin that registered it, if any)
11//! re-installed on the run ctx.
12//!
13//! Two output modes, selected by `stream_all`:
14//! * `--stream-all`: every item every task emits streams back, each
15//! wrapped as a [`ValueResponseItem`].
16//! * default: exactly ONE [`SuccessResponseItem`] per task, emitted
17//! when that task's stream completes — `success` is `false` iff the
18//! task's final item was an error.
19//!
20//! The mode affects only the caller-visible stream: in BOTH modes the
21//! full per-item output is durably written to `tasks_logs`.
22
23use crate::cli::command::CommandRequest;
24
25/// The plugin that registered a schedule — the same shape `tasks list`
26/// surfaces.
27pub use crate::cli::command::tasks::list::Plugin;
28
29#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
30#[schemars(rename = "cli.command.tasks.run.Request")]
31pub struct Request {
32 pub path_type: Path,
33 /// Stream every item every fired task emits (each a
34 /// [`ValueResponseItem`]). When false — the default — each task
35 /// yields exactly one [`SuccessResponseItem`] summary instead; the
36 /// full output still lands in `tasks_logs` either way.
37 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
38 pub stream_all: bool,
39 pub jq: Option<String>,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
43#[schemars(rename = "cli.command.tasks.run.Path")]
44pub enum Path {
45 #[serde(rename = "tasks/run")]
46 AgentsTasksRun,
47}
48
49impl CommandRequest for Request {
50 fn into_command(&self) -> Vec<String> {
51 let mut argv = vec![
52 "tasks".to_string(),
53 "run".to_string(),
54 ];
55 if self.stream_all {
56 argv.push("--stream-all".to_string());
57 }
58 if let Some(jq) = &self.jq {
59 argv.push("--jq".to_string());
60 argv.push(jq.clone());
61 }
62 argv
63 }
64}
65
66/// One stream item from `tasks run`. Untagged — the variants'
67/// required fields (`value` vs `success`) are disjoint, so the wire
68/// shape is just the inner object. Which variant flows is decided by
69/// the request's `stream_all`: `true` streams every emitted item as a
70/// [`ValueResponseItem`]; `false` (default) yields exactly one
71/// [`SuccessResponseItem`] per task when its stream completes.
72#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
73#[serde(untagged)]
74#[schemars(rename = "cli.command.tasks.run.ResponseItem")]
75pub enum ResponseItem {
76 #[schemars(title = "Value")]
77 Value(ValueResponseItem),
78 #[schemars(title = "Success")]
79 Success(SuccessResponseItem),
80}
81
82/// One output item from one fired schedule's in-process stream
83/// (`stream_all` mode). The first four fields identify the source
84/// schedule; `value` is the typed root
85/// [`crate::cli::command::ResponseItem`] emitted by the scheduled cli
86/// leaf — boxed because the root union transitively contains *this*
87/// type (`agents → tasks → run`), and boxing is what makes the
88/// recursion sized.
89///
90/// The `value` field's JSON schema is opaqued to `serde_json::Value`
91/// (renders as bare `{}` aka JsonValue) so the published schema
92/// doesn't inline the entire root union — that's the TS7056 blowup
93/// the root and tier aggregates dodge by being `json_schema_ignore`.
94/// Downstream SDKs see `value: JsonValue` on the typed `execute`
95/// path; consumers that want to peer inside parse it case-by-case.
96#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
97#[schemars(rename = "cli.command.tasks.run.ValueResponseItem")]
98pub struct ValueResponseItem {
99 /// The source schedule's `agent_instance_hierarchy`.
100 pub agent_instance_hierarchy: String,
101 /// The source schedule's `--name`.
102 pub name: String,
103 /// The source schedule's version (`1` on first creation,
104 /// incremented per `schedule --overwrite`).
105 pub version: u64,
106 /// The plugin that registered the source schedule, if any.
107 #[serde(default, skip_serializing_if = "Option::is_none")]
108 #[schemars(extend("omitempty" = true))]
109 pub plugin: Option<Plugin>,
110 /// The typed root item emitted by the scheduled command.
111 #[schemars(with = "serde_json::Value")]
112 pub value: Box<crate::cli::command::ResponseItem>,
113}
114
115/// One per-task completion summary (default mode): the same schedule
116/// identity as [`ValueResponseItem`], with `success` in lieu of
117/// `value`. `success` is `false` iff the task's FINAL emitted item was
118/// an error (a task that emitted nothing is a success). The task's
119/// full output is in `tasks_logs` regardless.
120#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
121#[schemars(rename = "cli.command.tasks.run.SuccessResponseItem")]
122pub struct SuccessResponseItem {
123 /// The source schedule's `agent_instance_hierarchy`.
124 pub agent_instance_hierarchy: String,
125 /// The source schedule's `--name`.
126 pub name: String,
127 /// The source schedule's version (`1` on first creation,
128 /// incremented per `schedule --overwrite`).
129 pub version: u64,
130 /// The plugin that registered the source schedule, if any.
131 #[serde(default, skip_serializing_if = "Option::is_none")]
132 #[schemars(extend("omitempty" = true))]
133 pub plugin: Option<Plugin>,
134 /// Whether the task's stream completed without a trailing error.
135 pub success: bool,
136}
137
138#[derive(clap::Args)]
139pub struct Args {
140 /// Stream every item every fired task emits. Without this flag
141 /// each task yields exactly one `{.., success}` summary when it
142 /// completes; the full output lands in `tasks_logs` either way.
143 #[arg(long)]
144 pub stream_all: bool,
145 #[arg(long)]
146 pub jq: Option<String>,
147}
148
149#[derive(clap::Args)]
150#[command(args_conflicts_with_subcommands = true)]
151pub struct Command {
152 #[command(flatten)]
153 pub args: Args,
154 #[command(subcommand)]
155 pub schema: Option<Schema>,
156}
157
158#[derive(clap::Subcommand)]
159pub enum Schema {
160 /// Emit the JSON Schema for this leaf's `Request` type and exit.
161 RequestSchema(request_schema::Args),
162 /// Emit the JSON Schema for this leaf's `Response` type and exit.
163 ResponseSchema(response_schema::Args),
164}
165
166impl TryFrom<Args> for Request {
167 type Error = crate::cli::command::FromArgsError;
168 fn try_from(args: Args) -> Result<Self, Self::Error> {
169 Ok(Self {
170 path_type: Path::AgentsTasksRun,
171 stream_all: args.stream_all,
172 jq: args.jq,
173 })
174 }
175}
176
177#[cfg(feature = "cli-executor")]
178pub async fn execute<E: crate::cli::command::CommandExecutor>(
179 executor: &E,
180 mut request: Request,
181 agent_arguments: Option<&crate::cli::command::AgentArguments>,
182) -> Result<E::Stream<ResponseItem>, E::Error> {
183 request.jq = None;
184 executor.execute(request, agent_arguments).await
185}
186
187#[cfg(feature = "cli-executor")]
188pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
189 executor: &E,
190 mut request: Request,
191 jq: String,
192 agent_arguments: Option<&crate::cli::command::AgentArguments>,
193) -> Result<E::Stream<serde_json::Value>, E::Error> {
194 request.jq = Some(jq);
195 executor.execute(request, agent_arguments).await
196}
197
198#[cfg(feature = "mcp")]
199impl crate::cli::command::CommandResponse for ResponseItem {
200 fn into_mcp(self) -> crate::cli::command::McpResponseItem {
201 crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
202 }
203}
204
205pub mod request_schema;
206
207pub mod response_schema;