Skip to main content

objectiveai_cli/command/
python.rs

1//! `python` — run a Python snippet in the embedded runtime and return
2//! its output as JSON.
3//!
4//! `--code` is executed with the optional `--input` JSON value exposed
5//! as the global `input` (the same harness the `--python` output
6//! transform uses). The script's output — its trailing expression
7//! value, else captured stdout — is returned as a `serde_json::Value`;
8//! a script that produces no output yields JSON `null`.
9
10use objectiveai_sdk::cli::command::python::{Request, Response};
11
12use crate::context::Context;
13use crate::error::Error;
14
15pub async fn execute(ctx: &Context, request: Request) -> Result<Response, Error> {
16    let output: Option<serde_json::Value> = ctx
17        .python()
18        .await?
19        .exec_code(ctx, &request.code, request.input)
20        .await?;
21    Ok(output.unwrap_or(serde_json::Value::Null))
22}
23
24pub mod request_schema {
25    use objectiveai_sdk::cli::command::python as sdk;
26    use objectiveai_sdk::cli::command::python::request_schema::{Request, Response};
27
28    use crate::context::Context;
29    use crate::error::Error;
30
31    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
32        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Request)))
33    }
34}
35
36pub mod response_schema {
37    use objectiveai_sdk::cli::command::python as sdk;
38    use objectiveai_sdk::cli::command::python::response_schema::{Request, Response};
39
40    use crate::context::Context;
41    use crate::error::Error;
42
43    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
44        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Response)))
45    }
46}