- Implemented HelpCommandHandler to handle the /help command. - Added CommandMetadata struct to store command metadata. - Registered new command handlers for GetCurrentSession, ListSessions, LoadSession, and SwitchSession. - Updated existing command handlers to provide metadata for help command. - Removed deprecated SessionQueryCommandHandler. - Added new command handlers for listing sessions and loading sessions.
61 lines
1.6 KiB
Rust
61 lines
1.6 KiB
Rust
use crate::command::context::CommandContext;
|
|
use crate::command::handler::{CommandHandler, CommandMetadata};
|
|
use crate::command::response::{CommandError, CommandResponse, MessageKind};
|
|
use crate::command::Command;
|
|
use async_trait::async_trait;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
/// Help 命令处理器
|
|
///
|
|
/// 显示所有支持的命令列表
|
|
pub struct HelpCommandHandler {
|
|
metadata: Arc<Mutex<Vec<CommandMetadata>>>,
|
|
}
|
|
|
|
impl HelpCommandHandler {
|
|
/// 创建新的 Help 命令处理器
|
|
///
|
|
/// # Arguments
|
|
/// * `metadata` - CommandRouter 的元数据列表引用
|
|
pub fn new(metadata: Arc<Mutex<Vec<CommandMetadata>>>) -> Self {
|
|
Self { metadata }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl CommandHandler for HelpCommandHandler {
|
|
fn can_handle(&self, cmd: &Command) -> bool {
|
|
matches!(cmd, Command::Help)
|
|
}
|
|
|
|
fn metadata(&self) -> Option<CommandMetadata> {
|
|
Some(CommandMetadata {
|
|
name: "help",
|
|
description: "显示所有支持的命令",
|
|
usage: "/help",
|
|
})
|
|
}
|
|
|
|
async fn handle(
|
|
&self,
|
|
_cmd: Command,
|
|
ctx: CommandContext,
|
|
) -> Result<CommandResponse, CommandError> {
|
|
let metadata = self.metadata.lock().unwrap();
|
|
let help_text = format_help(&metadata);
|
|
|
|
Ok(CommandResponse::success(ctx.request_id)
|
|
.with_message(MessageKind::Text, &help_text))
|
|
}
|
|
}
|
|
|
|
/// 格式化帮助文本
|
|
fn format_help(commands: &[CommandMetadata]) -> String {
|
|
let mut output = String::from("# 支持的命令\n\n");
|
|
|
|
for cmd in commands {
|
|
output.push_str(&format!("**{}** - {}\n", cmd.usage, cmd.description));
|
|
}
|
|
|
|
output
|
|
} |