use crate::agent::completions::message::RichContent;
use crate::cli::command::CommandRequest;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.message.Request")]
pub struct Request {
pub path_type: Path,
pub parent_agent_instance_hierarchy: Option<String>,
pub agent_instance: String,
pub message: RequestMessage,
pub seed: Option<i64>,
pub jq: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.message.Path")]
pub enum Path {
#[serde(rename = "agents/message")]
AgentsMessage,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.message.RequestMessage")]
pub enum RequestMessage {
#[schemars(title = "Inline")]
Inline(RichContent),
#[schemars(title = "Simple")]
Simple(String),
#[schemars(title = "File")]
File(std::path::PathBuf),
#[schemars(title = "PythonInline")]
PythonInline(String),
#[schemars(title = "PythonFile")]
PythonFile(std::path::PathBuf),
}
impl RequestMessage {
fn push_flags(&self, out: &mut Vec<String>) {
match self {
RequestMessage::Inline(rich) => {
out.push("--inline".to_string());
out.push(
serde_json::to_string(rich)
.expect("RichContent serializes to JSON cleanly"),
);
}
RequestMessage::Simple(s) => {
out.push("--simple".to_string());
out.push(s.clone());
}
RequestMessage::File(p) => {
out.push("--file".to_string());
out.push(p.to_string_lossy().into_owned());
}
RequestMessage::PythonInline(code) => {
out.push("--python-inline".to_string());
out.push(code.clone());
}
RequestMessage::PythonFile(p) => {
out.push("--python-file".to_string());
out.push(p.to_string_lossy().into_owned());
}
}
}
}
impl CommandRequest for Request {
fn into_command(&self) -> Vec<String> {
let mut argv = vec![
"agents".to_string(),
"message".to_string(),
self.agent_instance.clone(),
];
if let Some(parent) = &self.parent_agent_instance_hierarchy {
argv.push("--parent-agent-instance-hierarchy".to_string());
argv.push(parent.clone());
}
self.message.push_flags(&mut argv);
if let Some(seed) = self.seed {
argv.push("--seed".to_string());
argv.push(seed.to_string());
}
if let Some(jq) = &self.jq {
argv.push("--jq".to_string());
argv.push(jq.clone());
}
argv
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "cli.command.agents.message.Response")]
pub enum Response {
#[schemars(title = "Queued")]
Queued {
agent_instance_hierarchy: String,
response_id: String,
},
#[schemars(title = "Delivered")]
Delivered {
agent_instance_hierarchy: String,
},
}
#[derive(clap::Args)]
pub struct Args {
pub agent_instance: String,
#[arg(long = "parent-agent-instance-hierarchy")]
pub parent_agent_instance_hierarchy: Option<String>,
#[command(flatten)]
pub message: MessageArgs,
#[arg(long)]
pub seed: Option<i64>,
#[arg(long)]
pub jq: Option<String>,
}
#[derive(clap::Args)]
#[group(required = true, multiple = false)]
pub struct MessageArgs {
#[arg(long)]
pub simple: Option<String>,
#[arg(long)]
pub inline: Option<String>,
#[arg(long)]
pub file: Option<std::path::PathBuf>,
#[arg(long)]
pub python_inline: Option<String>,
#[arg(long)]
pub python_file: Option<std::path::PathBuf>,
}
#[derive(clap::Args)]
#[command(args_conflicts_with_subcommands = true)]
pub struct Command {
#[command(flatten)]
pub args: Args,
#[command(subcommand)]
pub schema: Option<Schema>,
}
#[derive(clap::Subcommand)]
pub enum Schema {
RequestSchema(request_schema::Args),
ResponseSchema(response_schema::Args),
}
impl TryFrom<Args> for Request {
type Error = crate::cli::command::FromArgsError;
fn try_from(args: Args) -> Result<Self, Self::Error> {
let message = if let Some(s) = args.message.simple {
RequestMessage::Simple(s)
} else if let Some(s) = args.message.inline {
let mut de = serde_json::Deserializer::from_str(&s);
let v = serde_path_to_error::deserialize(&mut de).map_err(|source| {
crate::cli::command::FromArgsError {
field: "inline",
source: source.into(),
}
})?;
RequestMessage::Inline(v)
} else if let Some(p) = args.message.file {
RequestMessage::File(p)
} else if let Some(s) = args.message.python_inline {
RequestMessage::PythonInline(s)
} else {
RequestMessage::PythonFile(args.message.python_file.unwrap())
};
Ok(Self {
path_type: Path::AgentsMessage,
parent_agent_instance_hierarchy: args.parent_agent_instance_hierarchy,
agent_instance: args.agent_instance,
message,
seed: args.seed,
jq: args.jq,
})
}
}
#[cfg(feature = "cli-executor")]
pub async fn execute<E: crate::cli::command::CommandExecutor>(
executor: &E,
mut request: Request,
agent_arguments: Option<&crate::cli::command::AgentArguments>,
) -> Result<Response, E::Error> {
request.jq = None;
executor.execute_one(request, agent_arguments).await
}
#[cfg(feature = "cli-executor")]
pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
executor: &E,
mut request: Request,
jq: String,
agent_arguments: Option<&crate::cli::command::AgentArguments>,
) -> Result<serde_json::Value, E::Error> {
request.jq = Some(jq);
executor.execute_one(request, agent_arguments).await
}
#[cfg(feature = "mcp")]
impl crate::cli::command::CommandResponse for Response {
fn into_mcp(self) -> crate::cli::command::McpResponseItem {
crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
}
}
pub mod request_schema;
pub mod response_schema;