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    // `--no-objectiveai` gates the in-process `objectiveai.execute` host call.
17    let ctx = ctx.with_no_objectiveai(request.no_objectiveai.unwrap_or(false));
18    let output: Option<serde_json::Value> = ctx
19        .python()
20        .await?
21        .exec_code(&ctx, &request.code, request.input)
22        .await?;
23    Ok(output.unwrap_or(serde_json::Value::Null))
24}
25
26pub mod request_schema {
27    use objectiveai_sdk::cli::command::python as sdk;
28    use objectiveai_sdk::cli::command::python::request_schema::{Request, Response};
29
30    use crate::context::Context;
31    use crate::error::Error;
32
33    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
34        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Request)))
35    }
36}
37
38pub mod response_schema {
39    use objectiveai_sdk::cli::command::python as sdk;
40    use objectiveai_sdk::cli::command::python::response_schema::{Request, Response};
41
42    use crate::context::Context;
43    use crate::error::Error;
44
45    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
46        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Response)))
47    }
48}