Skip to main content

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

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