use crate::command::adapter::{AdapterError, InputAdapter}; use crate::command::context::AdapterContext; use crate::command::Command; /// Channel 输入适配器 /// /// 将 Channel 消息中的文本命令(如 "/new", "/save")转换为 Command pub struct ChannelInputAdapter; impl ChannelInputAdapter { /// 创建新的 Channel 输入适配器 pub fn new() -> Self { Self } } impl Default for ChannelInputAdapter { fn default() -> Self { Self::new() } } impl InputAdapter for ChannelInputAdapter { fn try_parse( &self, input: &str, _ctx: AdapterContext, ) -> Result, AdapterError> { let trimmed = input.trim(); // 解析 /new 命令 if trimmed == "/new" { return Ok(Some(Command::CreateSession { title: None })); } if let Some(title) = trimmed.strip_prefix("/new ") { let title = title.trim(); return Ok(Some(Command::CreateSession { title: Some(title.to_string()), })); } // 解析 /save 命令 if trimmed == "/save" { return Ok(Some(Command::SaveSession { filepath: None, include_all: false, })); } if let Some(args) = trimmed.strip_prefix("/save ") { let args = args.trim(); // 解析参数:可能是 "all"、路径、或 "all 路径" let (include_all, filepath) = if args == "all" { // /save all - 保存全部消息 (true, None) } else if args.starts_with("all ") { // /save all - 保存全部消息到指定路径 let path = args[4..].trim(); (true, Some(path.to_string())) } else { // /save - 保存活跃消息到指定路径 (false, Some(args.to_string())) }; return Ok(Some(Command::SaveSession { filepath, include_all })); } // 解析 /list 命令 if trimmed == "/list" { return Ok(Some(Command::ListSessions { include_archived: false, })); } if trimmed == "/list all" { return Ok(Some(Command::ListSessions { include_archived: true, })); } // 解析 /use 命令 - 切换会话(支持 session_id 或序号) if let Some(session_id) = trimmed.strip_prefix("/use ") { let session_id = session_id.trim(); return Ok(Some(Command::SwitchSession { session_id: session_id.to_string(), })); } // 解析 /current 命令 - 获取当前会话信息 if trimmed == "/current" { return Ok(Some(Command::GetCurrentSession)); } // 不是命令,返回 None Ok(None) } }