use crate::command::context::CommandContext; use crate::command::handler::{CommandHandler, CommandMetadata}; use crate::command::response::{CommandError, CommandResponse, MessageKind}; use crate::command::Command; use crate::protocol::Channel; use async_trait::async_trait; /// 列出通道命令处理器 pub struct ListChannelsCommandHandler; impl ListChannelsCommandHandler { pub fn new() -> Self { Self } /// 获取默认通道列表(公开供其他模块使用) pub fn get_default_channels() -> Vec { vec![ Channel { id: "websocket".to_string(), name: "WebSocket".to_string(), description: Some("Web 前端通道".to_string()), is_writable: true, }, Channel { id: "cli".to_string(), name: "命令行".to_string(), description: Some("CLI 命令行通道".to_string()), is_writable: false, }, ] } } #[async_trait] impl CommandHandler for ListChannelsCommandHandler { fn can_handle(&self, cmd: &Command) -> bool { matches!(cmd, Command::ListChannels) } fn metadata(&self) -> Option { Some(CommandMetadata { name: "channels", description: "列出所有可用通道", usage: "/channels", }) } async fn handle( &self, cmd: Command, ctx: CommandContext, ) -> Result { match cmd { Command::ListChannels => handle_list_channels(ctx).await, _ => unreachable!(), } } } async fn handle_list_channels( ctx: CommandContext, ) -> Result { let channels = ListChannelsCommandHandler::get_default_channels(); let channels_json = serde_json::to_string(&channels) .map_err(|e| CommandError::new("SERIALIZE_ERROR", e.to_string()))?; let message = format!("Available channels: {}", channels.len()); Ok(CommandResponse::success(ctx.request_id) .with_message(MessageKind::Notification, &message) .with_metadata("channels", &channels_json) .with_metadata("count", &channels.len().to_string())) }