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