use std::str::FromStr;
use crate::cli::command::CommandRequest;
use crate::cli::command::path_ref::tokenize;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(tag = "by", rename_all = "snake_case")]
#[schemars(rename = "cli.command.agents.logs.read.all.Target")]
pub enum Target {
#[schemars(title = "Direct")]
Direct {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
parent_agent_instance_hierarchy: Option<String>,
agent_instance: String,
},
#[schemars(title = "Tag")]
Tag { agent_tag: String },
#[schemars(title = "Me")]
Me,
}
impl FromStr for Target {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.trim() == "me" {
return Ok(Target::Me);
}
let mut tag: Option<String> = None;
let mut parent: Option<String> = None;
let mut instance: Option<String> = None;
for (k, v) in tokenize(s)? {
match k {
"tag" => tag = Some(v.to_string()),
"instance" => instance = Some(v.to_string()),
"parent" => parent = Some(v.to_string()),
other => return Err(format!("unknown key: {other}")),
}
}
match (tag, instance, parent) {
(Some(t), None, None) => Ok(Target::Tag { agent_tag: t }),
(Some(_), _, _) => Err(
"tag is mutually exclusive with instance and parent".to_string(),
),
(None, Some(i), p) => Ok(Target::Direct {
parent_agent_instance_hierarchy: p,
agent_instance: i,
}),
(None, None, _) => Err("instance or tag is required".to_string()),
}
}
}
impl Target {
pub fn into_arg_string(&self) -> String {
match self {
Target::Me => "me".to_string(),
Target::Tag { agent_tag } => format!("tag={agent_tag}"),
Target::Direct {
parent_agent_instance_hierarchy: None,
agent_instance,
} => format!("instance={agent_instance}"),
Target::Direct {
parent_agent_instance_hierarchy: Some(p),
agent_instance,
} => format!("instance={agent_instance},parent={p}"),
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.logs.read.all.Request")]
pub struct Request {
pub path_type: Path,
pub targets: Vec<Target>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub after_id: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub limit: Option<i64>,
#[serde(flatten)]
pub base: crate::cli::command::RequestBase,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.logs.read.all.Path")]
pub enum Path {
#[serde(rename = "agents/logs/read/all")]
AgentsLogsReadAll,
}
impl CommandRequest for Request {
fn into_command(&self) -> Vec<String> {
let mut argv = vec![
"agents".to_string(),
"logs".to_string(),
"read".to_string(),
"all".to_string(),
];
for target in &self.targets {
argv.push("--target".to_string());
argv.push(target.into_arg_string());
}
if let Some(after_id) = self.after_id {
argv.push("--after-id".to_string());
argv.push(after_id.to_string());
}
if let Some(limit) = self.limit {
argv.push("--limit".to_string());
argv.push(limit.to_string());
}
self.base.push_flags(&mut argv);
argv
}
fn request_base(&self) -> &crate::cli::command::RequestBase {
&self.base
}
fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
Some(&mut self.base)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[schemars(rename = "cli.command.agents.logs.read.all.ClientNotificationPartType")]
pub enum ClientNotificationPartType {
Text,
Image,
Audio,
Video,
File,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.logs.read.all.ClientNotificationPart")]
pub struct ClientNotificationPart {
pub id: i64,
pub delivered_at: String,
pub r#type: ClientNotificationPartType,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[schemars(rename = "cli.command.agents.logs.read.all.AssistantResponsePart")]
pub enum AssistantResponsePart {
#[schemars(title = "ToolCall")]
ToolCall {
id: i64,
delivered_at: String,
function_name: String,
tool_call_id: String,
tool_call_index: i64,
},
#[schemars(title = "Refusal")]
Refusal { id: i64, delivered_at: String },
#[schemars(title = "Reasoning")]
Reasoning { id: i64, delivered_at: String },
#[schemars(title = "Text")]
Text { id: i64, delivered_at: String },
#[schemars(title = "Image")]
Image { id: i64, delivered_at: String },
#[schemars(title = "Audio")]
Audio { id: i64, delivered_at: String },
#[schemars(title = "Video")]
Video { id: i64, delivered_at: String },
#[schemars(title = "File")]
File { id: i64, delivered_at: String },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[schemars(rename = "cli.command.agents.logs.read.all.ToolResponsePartType")]
pub enum ToolResponsePartType {
Text,
Image,
Audio,
Video,
File,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.logs.read.all.ToolResponsePart")]
pub struct ToolResponsePart {
pub id: i64,
pub delivered_at: String,
pub r#type: ToolResponsePartType,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[schemars(rename = "cli.command.agents.logs.read.all.ResponseItem")]
pub enum ResponseItem {
#[schemars(title = "AgentCompletionRequest")]
AgentCompletionRequest {
id: i64,
agent_instance_hierarchy: String,
sender_agent_instance_hierarchy: String,
delivered_at: String,
response_id: String,
},
#[schemars(title = "VectorCompletionRequest")]
VectorCompletionRequest {
id: i64,
agent_instance_hierarchy: String,
sender_agent_instance_hierarchy: String,
delivered_at: String,
response_id: String,
},
#[schemars(title = "FunctionExecutionRequest")]
FunctionExecutionRequest {
id: i64,
agent_instance_hierarchy: String,
sender_agent_instance_hierarchy: String,
delivered_at: String,
response_id: String,
},
#[schemars(title = "ClientNotification")]
ClientNotification {
agent_instance_hierarchy: String,
sender_agent_instance_hierarchy: String,
response_id: String,
queued_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
key: Option<String>,
parts: Vec<ClientNotificationPart>,
},
#[schemars(title = "AssistantResponse")]
AssistantResponse {
agent_instance_hierarchy: String,
response_id: String,
parts: Vec<AssistantResponsePart>,
},
#[schemars(title = "ToolResponse")]
ToolResponse {
agent_instance_hierarchy: String,
response_id: String,
tool_call_id: String,
parts: Vec<ToolResponsePart>,
},
}
#[derive(clap::Args)]
pub struct Args {
#[arg(long = "target", required = true)]
pub targets: Vec<String>,
#[arg(long)]
pub after_id: Option<i64>,
#[arg(long)]
pub limit: Option<i64>,
#[command(flatten)]
pub base: crate::cli::command::RequestBaseArgs,
}
#[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 targets = args
.targets
.iter()
.map(|s| {
s.parse::<Target>().map_err(|msg| {
crate::cli::command::FromArgsError::path_parse("target", msg)
})
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
path_type: Path::AgentsLogsReadAll,
targets,
after_id: args.after_id,
limit: args.limit,
base: args.base.into(),
})
}
}
#[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.base.clear_transform();
executor.execute(request, agent_arguments).await
}
#[cfg(feature = "cli-executor")]
pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
executor: &E,
mut request: Request,
transform: crate::cli::command::Transform,
agent_arguments: Option<&crate::cli::command::AgentArguments>,
) -> Result<E::Stream<serde_json::Value>, E::Error> {
request.base.set_transform(transform);
executor.execute(request, agent_arguments).await
}
#[cfg(feature = "mcp")]
impl crate::cli::command::CommandResponse for ResponseItem {
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;
#[cfg(test)]
mod tests {
use super::Target;
#[test]
fn me_target_parses_and_round_trips() {
assert_eq!("me".parse::<Target>(), Ok(Target::Me));
assert_eq!(" me ".parse::<Target>(), Ok(Target::Me));
assert_eq!(Target::Me.into_arg_string(), "me");
}
#[test]
fn me_is_mutually_exclusive_with_other_keys() {
assert!("me,instance=x".parse::<Target>().is_err());
}
#[test]
fn json_wire_shape_is_by_me() {
assert_eq!(
serde_json::to_value(Target::Me).unwrap(),
serde_json::json!({ "by": "me" }),
);
}
}