Skip to main content

objectiveai_sdk/cli/command/functions/publish/
mod.rs

1//! `functions publish` — async handler stub.
2
3use crate::functions::FullRemoteFunction;
4use crate::cli::command::CommandRequest;
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
7#[schemars(rename = "cli.command.functions.publish.Request")]
8pub struct Request {
9    pub path_type: Path,
10    pub repository: String,
11    pub body: RequestBody,
12    pub message: RequestPublishMessage,
13    pub overwrite: bool,
14    #[serde(flatten)]
15    pub base: crate::cli::command::RequestBase,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
19#[schemars(rename = "cli.command.functions.publish.Path")]
20pub enum Path {
21    #[serde(rename = "functions/publish")]
22    FunctionsPublish,
23}
24
25#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
26#[schemars(rename = "cli.command.functions.publish.RequestBody")]
27pub enum RequestBody {
28    #[schemars(title = "Inline")]
29    Inline(FullRemoteFunction),
30    #[schemars(title = "File")]
31    File(std::path::PathBuf),
32    #[schemars(title = "PythonInline")]
33    PythonInline(String),
34    #[schemars(title = "PythonFile")]
35    PythonFile(std::path::PathBuf),
36}
37
38#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
39#[schemars(rename = "cli.command.functions.publish.RequestPublishMessage")]
40pub enum RequestPublishMessage {
41    #[schemars(title = "Inline")]
42    Inline(String),
43    #[schemars(title = "File")]
44    File(std::path::PathBuf),
45}
46
47impl RequestBody {
48    fn push_flags(&self, out: &mut Vec<String>) {
49        match self {
50            RequestBody::Inline(v) => {
51                out.push("--body-inline".to_string());
52                out.push(serde_json::to_string(v).expect("body serializes"));
53            }
54            RequestBody::File(p) => {
55                out.push("--body-file".to_string());
56                out.push(p.to_string_lossy().into_owned());
57            }
58            RequestBody::PythonInline(code) => {
59                out.push("--body-python-inline".to_string());
60                out.push(code.clone());
61            }
62            RequestBody::PythonFile(p) => {
63                out.push("--body-python-file".to_string());
64                out.push(p.to_string_lossy().into_owned());
65            }
66        }
67    }
68}
69
70impl RequestPublishMessage {
71    fn push_flags(&self, out: &mut Vec<String>) {
72        match self {
73            RequestPublishMessage::Inline(s) => {
74                out.push("--message-inline".to_string());
75                out.push(s.clone());
76            }
77            RequestPublishMessage::File(p) => {
78                out.push("--message-file".to_string());
79                out.push(p.to_string_lossy().into_owned());
80            }
81        }
82    }
83}
84
85impl CommandRequest for Request {
86    fn into_command(&self) -> Vec<String> {
87        let mut argv = vec![
88            "functions".to_string(),
89            "publish".to_string(),
90            "--repository".to_string(),
91            self.repository.clone(),
92        ];
93        self.body.push_flags(&mut argv);
94        self.message.push_flags(&mut argv);
95        if self.overwrite {
96            argv.push("--overwrite".to_string());
97        }
98        self.base.push_flags(&mut argv);
99        argv
100    }
101
102    fn request_base(&self) -> &crate::cli::command::RequestBase {
103        &self.base
104    }
105
106    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
107        Some(&mut self.base)
108    }
109}
110
111#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
112#[schemars(rename = "cli.command.functions.publish.Response")]
113pub struct Response {
114    pub sha: String,
115}
116
117#[derive(clap::Args)]
118#[group(id = "body", required = true, multiple = false)]
119#[group(id = "message", required = true, multiple = false)]
120pub struct Args {
121    /// Repository name.
122    #[arg(long)]
123    pub repository: String,
124    /// Inline JSON body.
125    #[arg(long, group = "body")]
126    pub body_inline: Option<String>,
127    /// Path to a JSON file.
128    #[arg(long, group = "body")]
129    pub body_file: Option<std::path::PathBuf>,
130    /// Inline Python that produces the JSON body.
131    #[arg(long, group = "body")]
132    pub body_python_inline: Option<String>,
133    /// Path to a Python file that produces the JSON body.
134    #[arg(long, group = "body")]
135    pub body_python_file: Option<std::path::PathBuf>,
136    /// Inline commit message.
137    #[arg(long, group = "message")]
138    pub message_inline: Option<String>,
139    /// Path to a file containing the commit message.
140    #[arg(long, group = "message")]
141    pub message_file: Option<std::path::PathBuf>,
142    /// Overwrite if the entry already exists.
143    #[arg(long)]
144    pub overwrite: bool,
145    #[command(flatten)]
146    pub base: crate::cli::command::RequestBaseArgs,
147}
148
149#[derive(clap::Args)]
150#[command(args_conflicts_with_subcommands = true)]
151pub struct Command {
152    #[command(flatten)]
153    pub args: Args,
154    #[command(subcommand)]
155    pub schema: Option<Schema>,
156}
157
158#[derive(clap::Subcommand)]
159pub enum Schema {
160    /// Emit the JSON Schema for this leaf's `Request` type and exit.
161    RequestSchema(request_schema::Args),
162    /// Emit the JSON Schema for this leaf's `Response` type and exit.
163    ResponseSchema(response_schema::Args),
164}
165
166impl TryFrom<Args> for Request {
167    type Error = crate::cli::command::FromArgsError;
168    fn try_from(args: Args) -> Result<Self, Self::Error> {
169        let body = if let Some(s) = args.body_inline {
170            let mut de = serde_json::Deserializer::from_str(&s);
171            let v = serde_path_to_error::deserialize(&mut de).map_err(|source| {
172                crate::cli::command::FromArgsError {
173                    field: "body_inline",
174                    source: source.into(),
175                }
176            })?;
177            RequestBody::Inline(v)
178        } else if let Some(p) = args.body_file {
179            RequestBody::File(p)
180        } else if let Some(s) = args.body_python_inline {
181            RequestBody::PythonInline(s)
182        } else {
183            RequestBody::PythonFile(args.body_python_file.unwrap())
184        };
185        let message = if let Some(s) = args.message_inline {
186            RequestPublishMessage::Inline(s)
187        } else {
188            RequestPublishMessage::File(args.message_file.unwrap())
189        };
190        Ok(Self { path_type: Path::FunctionsPublish,
191            repository: args.repository,
192            body,
193            message,
194            overwrite: args.overwrite,
195            base: args.base.into(),
196        })
197    }
198}
199
200#[cfg(feature = "cli-executor")]
201pub async fn execute<E: crate::cli::command::CommandExecutor>(
202    executor: &E,
203    mut request: Request,
204
205        agent_arguments: Option<&crate::cli::command::AgentArguments>,
206    ) -> Result<Response, E::Error> {
207    request.base.clear_transform();
208    executor.execute_one(request, agent_arguments).await
209}
210
211#[cfg(feature = "cli-executor")]
212pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
213    executor: &E,
214    mut request: Request,
215    transform: crate::cli::command::Transform,
216
217        agent_arguments: Option<&crate::cli::command::AgentArguments>,
218    ) -> Result<serde_json::Value, E::Error> {
219    request.base.set_transform(transform);
220    executor.execute_one(request, agent_arguments).await
221}
222
223#[cfg(feature = "mcp")]
224impl crate::cli::command::CommandResponse for Response {
225    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
226        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
227    }
228}
229
230pub mod request_schema;
231
232
233pub mod response_schema;