use std::future::Future;
use std::pin::Pin;
use crate::context::CommandContext;
use crate::{CommandError, CommandHandler, CommandOutput, SlashCategory};
pub struct AgentCommand;
impl CommandHandler<CommandContext<'_>> for AgentCommand {
fn name(&self) -> &'static str {
"/agent"
}
fn description(&self) -> &'static str {
"Manage sub-agents"
}
fn args_hint(&self) -> &'static str {
"[subcommand]"
}
fn category(&self) -> SlashCategory {
SlashCategory::Integration
}
fn handle<'a>(
&'a self,
ctx: &'a mut CommandContext<'_>,
args: &'a str,
) -> Pin<Box<dyn Future<Output = Result<CommandOutput, CommandError>> + Send + 'a>> {
use tracing::Instrument as _;
let span = tracing::info_span!("commands.agent.handle");
Box::pin(
async move {
let input = if args.is_empty() {
"/agent".to_owned()
} else {
format!("/agent {args}")
};
match ctx.agent.handle_agent_dispatch(&input).await? {
Some(msg) => Ok(CommandOutput::Message(msg)),
None => Ok(CommandOutput::Silent),
}
}
.instrument(span),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::handlers::test_helpers::{MockDebug, MockMessages, MockSession, make_ctx};
use crate::sink::NullSink;
#[test]
fn agent_cmd_name_and_description() {
assert_eq!(AgentCommand.name(), "/agent");
assert!(!AgentCommand.description().is_empty());
}
#[tokio::test]
async fn agent_dispatch_none_returns_silent() {
let mut sink = NullSink;
let mut debug = MockDebug;
let mut messages = MockMessages;
let session = MockSession;
let mut agent = crate::NullAgent;
let mut ctx = make_ctx(&mut sink, &mut debug, &mut messages, &session, &mut agent);
let out = AgentCommand.handle(&mut ctx, "").await.unwrap();
assert!(matches!(out, CommandOutput::Silent));
}
#[tokio::test]
async fn agent_dispatch_with_args_returns_silent() {
let mut sink = NullSink;
let mut debug = MockDebug;
let mut messages = MockMessages;
let session = MockSession;
let mut agent = crate::NullAgent;
let mut ctx = make_ctx(&mut sink, &mut debug, &mut messages, &session, &mut agent);
let out = AgentCommand.handle(&mut ctx, "list").await.unwrap();
assert!(matches!(out, CommandOutput::Silent));
}
}