Skip to main content

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

1//! `agents publish` — async handler stub.
2
3use crate::agent::RemoteAgentBaseWithFallbacks;
4use crate::cli::command::CommandRequest;
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
7#[schemars(rename = "cli.command.agents.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.agents.publish.Path")]
20pub enum Path {
21    #[serde(rename = "agents/publish")]
22    AgentsPublish,
23}
24
25#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
26#[schemars(rename = "cli.command.agents.publish.RequestBody")]
27pub enum RequestBody {
28    #[schemars(title = "Inline")]
29    Inline(RemoteAgentBaseWithFallbacks),
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.agents.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 request_base(&self) -> &crate::cli::command::RequestBase {
87        &self.base
88    }
89
90    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
91        Some(&mut self.base)
92    }
93}
94
95#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
96#[schemars(rename = "cli.command.agents.publish.Response")]
97pub struct Response {
98    pub sha: String,
99}
100
101#[derive(clap::Args)]
102#[command(group(clap::ArgGroup::new("repository_required").required(true).args(["repository"])))]
103pub struct Args {
104    /// Target repository.
105    #[arg(long)]
106    pub repository: Option<String>,
107    #[command(flatten)]
108    pub body: BodyArgs,
109    #[command(flatten)]
110    pub message: PublishMessageArgs,
111    /// Overwrite the existing entry if present.
112    #[arg(long)]
113    pub overwrite: bool,
114    #[command(flatten)]
115    pub base: crate::cli::command::RequestBaseArgs,
116}
117
118#[derive(clap::Args)]
119#[group(required = true, multiple = false)]
120pub struct BodyArgs {
121    /// Inline JSON body.
122    #[arg(long)]
123    pub body_inline: Option<String>,
124    /// Path to a JSON file.
125    #[arg(long)]
126    pub body_file: Option<std::path::PathBuf>,
127    /// Inline Python code that produces the JSON body.
128    #[arg(long)]
129    pub body_python_inline: Option<String>,
130    /// Path to a Python file that produces the JSON body.
131    #[arg(long)]
132    pub body_python_file: Option<std::path::PathBuf>,
133}
134
135#[derive(clap::Args)]
136#[group(required = true, multiple = false)]
137pub struct PublishMessageArgs {
138    /// Inline commit message.
139    #[arg(long)]
140    pub message_inline: Option<String>,
141    /// Path to a file containing the commit message.
142    #[arg(long)]
143    pub message_file: Option<std::path::PathBuf>,
144}
145
146#[derive(clap::Args)]
147#[command(args_conflicts_with_subcommands = true)]
148pub struct Command {
149    #[command(flatten)]
150    pub args: Args,
151    #[command(subcommand)]
152    pub schema: Option<Schema>,
153}
154
155#[derive(clap::Subcommand)]
156pub enum Schema {
157    /// Emit the JSON Schema for this leaf's `Request` type and exit.
158    RequestSchema(request_schema::Args),
159    /// Emit the JSON Schema for this leaf's `Response` type and exit.
160    ResponseSchema(response_schema::Args),
161}
162
163impl TryFrom<Args> for Request {
164    type Error = crate::cli::command::FromArgsError;
165    fn try_from(args: Args) -> Result<Self, Self::Error> {
166        let body = if let Some(s) = args.body.body_inline {
167            let mut de = serde_json::Deserializer::from_str(&s);
168            let v = serde_path_to_error::deserialize(&mut de).map_err(|source| {
169                crate::cli::command::FromArgsError {
170                    field: "body_inline",
171                    source: source.into(),
172                }
173            })?;
174            RequestBody::Inline(v)
175        } else if let Some(p) = args.body.body_file {
176            RequestBody::File(p)
177        } else if let Some(s) = args.body.body_python_inline {
178            RequestBody::PythonInline(s)
179        } else {
180            RequestBody::PythonFile(args.body.body_python_file.unwrap())
181        };
182        let message = if let Some(s) = args.message.message_inline {
183            RequestPublishMessage::Inline(s)
184        } else {
185            RequestPublishMessage::File(args.message.message_file.unwrap())
186        };
187        Ok(Self { path_type: Path::AgentsPublish,
188            repository: args.repository.ok_or_else(|| {
189                crate::cli::command::FromArgsError::path_parse(
190                    "repository",
191                    "--repository is required".to_string(),
192                )
193            })?,
194            body,
195            message,
196            overwrite: args.overwrite,
197            base: args.base.into(),
198        })
199    }
200}
201
202#[cfg(feature = "cli-executor")]
203pub async fn execute<E: crate::cli::command::CommandExecutor>(
204    executor: &E,
205    mut request: Request,
206
207        agent_arguments: Option<&crate::cli::command::AgentArguments>,
208    ) -> Result<Response, E::Error> {
209    request.base.clear_transform();
210    executor.execute_one(request, agent_arguments).await
211}
212
213#[cfg(feature = "cli-executor")]
214pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
215    executor: &E,
216    mut request: Request,
217    transform: crate::cli::command::Transform,
218
219        agent_arguments: Option<&crate::cli::command::AgentArguments>,
220    ) -> Result<serde_json::Value, E::Error> {
221    request.base.set_transform(transform);
222    executor.execute_one(request, agent_arguments).await
223}
224
225#[cfg(feature = "mcp")]
226impl crate::cli::command::CommandResponse for Response {
227    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
228        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
229    }
230}
231
232pub mod request_schema;
233
234
235pub mod response_schema;