Skip to main content

objectiveai_sdk/cli/command/functions/execute/swiss_system/
mod.rs

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