Skip to main content

objectiveai_sdk/cli/command/functions/executions/create/standard/
mod.rs

1//! `functions executions create standard` — async handler stub.
2
3use crate::agent::completions::message::Message;  // unused placeholder to keep imports tidy
4use crate::cli::command::CommandRequest;
5use crate::functions::expression::InputValue;
6use super::{FunctionSpec, ProfileSpec};
7
8#[allow(dead_code)]
9type _UnusedMessage = Message;
10
11#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
12#[schemars(rename = "cli.command.functions.executions.create.standard.Request")]
13pub struct Request {
14    pub path_type: Path,
15    pub function: FunctionSpec,
16    pub profile: ProfileSpec,
17    pub input: RequestInput,
18    pub continuation: Option<String>,
19    pub retry_token: Option<String>,
20    pub seed: Option<i64>,
21    pub split: bool,
22    pub invert: bool,
23    pub dangerous_advanced: Option<RequestDangerousAdvanced>,
24    pub jq: Option<String>,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
28#[schemars(rename = "cli.command.functions.executions.create.standard.Path")]
29pub enum Path {
30    #[serde(rename = "functions/executions/create/standard")]
31    FunctionsExecutionsCreateStandard,
32}
33
34#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
35#[schemars(rename = "cli.command.functions.executions.create.standard.RequestInput")]
36pub enum RequestInput {
37    #[schemars(title = "Inline")]
38    Inline(InputValue),
39    #[schemars(title = "PythonInline")]
40    PythonInline(String),
41    #[schemars(title = "PythonFile")]
42    PythonFile(std::path::PathBuf),
43}
44
45impl RequestInput {
46    fn push_flags(&self, out: &mut Vec<String>) {
47        match self {
48            RequestInput::Inline(v) => {
49                out.push("--input-inline".to_string());
50                out.push(serde_json::to_string(v).expect("input serializes"));
51            }
52            RequestInput::PythonInline(code) => {
53                out.push("--input-python-inline".to_string());
54                out.push(code.clone());
55            }
56            RequestInput::PythonFile(p) => {
57                out.push("--input-python-file".to_string());
58                out.push(p.to_string_lossy().into_owned());
59            }
60        }
61    }
62}
63
64impl CommandRequest for Request {
65    fn into_command(&self) -> Vec<String> {
66        let mut argv = vec![
67            "functions".to_string(),
68            "executions".to_string(),
69            "create".to_string(),
70            "standard".to_string(),
71            "--function-inline".to_string(),
72            serde_json::to_string(&self.function).expect("function serializes"),
73            "--profile-inline".to_string(),
74            serde_json::to_string(&self.profile).expect("profile serializes"),
75        ];
76        self.input.push_flags(&mut argv);
77        if let Some(c) = &self.continuation {
78            argv.push("--continuation".to_string());
79            argv.push(c.clone());
80        }
81        if let Some(t) = &self.retry_token {
82            argv.push("--retry-token".to_string());
83            argv.push(t.clone());
84        }
85        if let Some(seed) = self.seed {
86            argv.push("--seed".to_string());
87            argv.push(seed.to_string());
88        }
89        if self.split {
90            argv.push("--split".to_string());
91        }
92        if self.invert {
93            argv.push("--invert".to_string());
94        }
95        if let Some(advanced) = &self.dangerous_advanced {
96            argv.push("--dangerous-advanced".to_string());
97            argv.push(
98                serde_json::to_string(advanced)
99                    .expect("RequestDangerousAdvanced serializes"),
100            );
101        }
102        if let Some(jq) = &self.jq {
103            argv.push("--jq".to_string());
104            argv.push(jq.clone());
105        }
106        argv
107    }
108}
109
110#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
111#[schemars(rename = "cli.command.functions.executions.create.standard.RequestDangerousAdvanced")]
112pub struct RequestDangerousAdvanced {
113    #[serde(default, skip_serializing_if = "Option::is_none")]
114    #[schemars(extend("omitempty" = true))]
115    pub stream: Option<bool>,
116}
117
118#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
119#[serde(untagged)]
120#[schemars(rename = "cli.command.functions.executions.create.standard.ResponseItem")]
121pub enum ResponseItem {
122    #[schemars(title = "Chunk")]
123    Chunk(crate::functions::executions::response::streaming::FunctionExecutionChunk),
124    #[schemars(title = "Id")]
125    Id(String),
126}
127
128/// Non-chunk variant of [`ResponseItem`]. Returned by the unary `execute`
129/// path (with `dangerous_advanced.stream` cleared) when the cli emits a
130/// single bare id string.
131pub type Response = String;
132
133#[derive(clap::Args)]
134#[group(id = "input", required = true, multiple = false)]
135pub struct Args {
136    /// Inline JSON function definition.
137    #[arg(long)]
138    pub function_inline: String,
139    /// Inline JSON profile definition.
140    #[arg(long)]
141    pub profile_inline: String,
142    /// Inline JSON input value.
143    #[arg(long, group = "input")]
144    pub input_inline: Option<String>,
145    /// Inline Python that produces the input value.
146    #[arg(long, group = "input")]
147    pub input_python_inline: Option<String>,
148    /// Path to a Python file that produces the input value.
149    #[arg(long, group = "input")]
150    pub input_python_file: Option<std::path::PathBuf>,
151    /// Continuation token from a previous response.
152    #[arg(long)]
153    pub continuation: Option<String>,
154    /// Retry token from a previous execution.
155    #[arg(long)]
156    pub retry_token: Option<String>,
157    /// Seed for deterministic mock responses.
158    #[arg(long)]
159    pub seed: Option<i64>,
160    /// Treat input as an array and execute once per element.
161    #[arg(long)]
162    pub split: bool,
163    /// Invert outputs after expressions evaluate.
164    #[arg(long)]
165    pub invert: bool,
166    /// Advanced opt-in flags as inline JSON.
167    #[arg(long)]
168    pub dangerous_advanced: Option<String>,
169    /// jq filter applied to the JSON output.
170    #[arg(long)]
171    pub jq: Option<String>,
172}
173
174#[derive(clap::Args)]
175#[command(args_conflicts_with_subcommands = true)]
176pub struct Command {
177    #[command(flatten)]
178    pub args: Args,
179    #[command(subcommand)]
180    pub schema: Option<Schema>,
181}
182
183#[derive(clap::Subcommand)]
184pub enum Schema {
185    /// Emit the JSON Schema for this leaf's `Request` type and exit.
186    RequestSchema(request_schema::Args),
187    /// Emit the JSON Schema for this leaf's `Response` type and exit.
188    ResponseSchema(response_schema::Args),
189}
190
191impl TryFrom<Args> for Request {
192    type Error = crate::cli::command::FromArgsError;
193    fn try_from(args: Args) -> Result<Self, Self::Error> {
194        let function = {
195            let mut de = serde_json::Deserializer::from_str(&args.function_inline);
196            serde_path_to_error::deserialize(&mut de).map_err(|source| {
197                crate::cli::command::FromArgsError {
198                    field: "function_inline",
199                    source: source.into(),
200                }
201            })?
202        };
203        let profile = {
204            let mut de = serde_json::Deserializer::from_str(&args.profile_inline);
205            serde_path_to_error::deserialize(&mut de).map_err(|source| {
206                crate::cli::command::FromArgsError {
207                    field: "profile_inline",
208                    source: source.into(),
209                }
210            })?
211        };
212        let input = if let Some(s) = args.input_inline {
213            let mut de = serde_json::Deserializer::from_str(&s);
214            let v = serde_path_to_error::deserialize(&mut de).map_err(|source| {
215                crate::cli::command::FromArgsError {
216                    field: "input_inline",
217                    source: source.into(),
218                }
219            })?;
220            RequestInput::Inline(v)
221        } else if let Some(s) = args.input_python_inline {
222            RequestInput::PythonInline(s)
223        } else {
224            RequestInput::PythonFile(args.input_python_file.unwrap())
225        };
226        let dangerous_advanced = if let Some(s) = args.dangerous_advanced {
227            let mut de = serde_json::Deserializer::from_str(&s);
228            let v = serde_path_to_error::deserialize(&mut de).map_err(|source| {
229                crate::cli::command::FromArgsError {
230                    field: "dangerous_advanced",
231                    source: source.into(),
232                }
233            })?;
234            Some(v)
235        } else {
236            None
237        };
238        Ok(Self { path_type: Path::FunctionsExecutionsCreateStandard,
239            function,
240            profile,
241            input,
242            continuation: args.continuation,
243            retry_token: args.retry_token,
244            seed: args.seed,
245            split: args.split,
246            invert: args.invert,
247            dangerous_advanced,
248            jq: args.jq,
249        })
250    }
251}
252
253#[cfg(feature = "cli-executor")]
254pub async fn execute_streaming<E: crate::cli::command::CommandExecutor>(
255    executor: &E,
256    mut request: Request,
257
258        agent_arguments: Option<&crate::cli::command::AgentArguments>,
259    ) -> Result<E::Stream<ResponseItem>, E::Error> {
260    request.jq = None;
261    let mut advanced = request.dangerous_advanced.unwrap_or_default();
262    advanced.stream = Some(true);
263    request.dangerous_advanced = Some(advanced);
264    executor.execute(request, agent_arguments).await
265}
266
267#[cfg(feature = "cli-executor")]
268pub async fn execute_streaming_jq<E: crate::cli::command::CommandExecutor>(
269    executor: &E,
270    mut request: Request,
271    jq: String,
272
273        agent_arguments: Option<&crate::cli::command::AgentArguments>,
274    ) -> Result<E::Stream<serde_json::Value>, E::Error> {
275    request.jq = Some(jq);
276    let mut advanced = request.dangerous_advanced.unwrap_or_default();
277    advanced.stream = Some(true);
278    request.dangerous_advanced = Some(advanced);
279    executor.execute(request, agent_arguments).await
280}
281
282#[cfg(feature = "cli-executor")]
283pub async fn execute<E: crate::cli::command::CommandExecutor>(
284    executor: &E,
285    mut request: Request,
286
287        agent_arguments: Option<&crate::cli::command::AgentArguments>,
288    ) -> Result<Response, E::Error> {
289    request.jq = None;
290    if let Some(advanced) = request.dangerous_advanced.as_mut() {
291        advanced.stream = None;
292    }
293    executor.execute_one(request, agent_arguments).await
294}
295
296#[cfg(feature = "cli-executor")]
297pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
298    executor: &E,
299    mut request: Request,
300    jq: String,
301
302        agent_arguments: Option<&crate::cli::command::AgentArguments>,
303    ) -> Result<serde_json::Value, E::Error> {
304    request.jq = Some(jq);
305    if let Some(advanced) = request.dangerous_advanced.as_mut() {
306        advanced.stream = None;
307    }
308    executor.execute_one(request, agent_arguments).await
309}
310
311#[cfg(feature = "mcp")]
312impl crate::cli::command::CommandResponse for ResponseItem {
313    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
314        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
315    }
316}
317
318pub mod request_schema;
319
320
321pub mod response_schema;