use crate::cli::command::CommandRequest;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.plugins.run.Request")]
pub struct Request {
pub path_type: Path,
pub name: String,
pub args: Vec<String>,
pub jq: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.plugins.run.Path")]
pub enum Path {
#[serde(rename = "plugins/run")]
PluginsRun,
}
impl CommandRequest for Request {
fn into_command(&self) -> Vec<String> {
let mut argv = vec!["plugins".to_string(), "run".to_string()];
if let Some(jq) = &self.jq {
argv.push("--jq".to_string());
argv.push(jq.clone());
}
argv.push(self.name.clone());
argv.extend(self.args.iter().cloned());
argv
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "cli.command.plugins.run.ResponseItem")]
pub enum ResponseItem {
#[schemars(title = "Mcp")]
Mcp(Mcp),
#[schemars(title = "Error")]
Error(crate::cli::Error),
#[schemars(title = "Notification")]
Notification(serde_json::Value),
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.plugins.run.Mcp")]
pub struct Mcp {
pub r#type: McpType,
pub url: String,
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
schemars::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
#[schemars(rename = "cli.command.plugins.run.McpType")]
pub enum McpType {
Mcp,
}
#[derive(clap::Args)]
pub struct Args {
pub name: String,
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
pub args: Vec<String>,
#[arg(long)]
pub jq: Option<String>,
}
#[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> {
Ok(Self { path_type: Path::PluginsRun,
name: args.name,
args: args.args,
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<E::Stream<ResponseItem>, E::Error> {
request.jq = None;
executor.execute(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<E::Stream<serde_json::Value>, E::Error> {
request.jq = Some(jq);
executor.execute(request, agent_arguments).await
}
#[cfg(feature = "mcp")]
impl crate::cli::command::CommandResponse for ResponseItem {
fn into_mcp(self) -> crate::cli::command::McpResponseItem {
use crate::agent::completions::message::RichContentPart;
use crate::cli::command::McpResponseItem;
match self {
ResponseItem::Mcp(m) => {
McpResponseItem::JSONL(serde_json::to_value(m).unwrap())
}
ResponseItem::Error(e) => e.into_mcp(),
ResponseItem::Notification(value) => {
if let serde_json::Value::String(s) = &value
&& let Some((mime, payload)) = crate::data_url::parse_data_url(s)
{
let part = RichContentPart::from_blob(
mime,
payload.to_string(),
None,
);
return McpResponseItem::Media(part.into());
}
McpResponseItem::JSONL(value)
}
}
}
}
pub mod request_schema;
pub mod response_schema;