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::SaveTopic { filepath: None, include_subagents: false, })); } if let Some(args) = trimmed.strip_prefix("/save ") { let args = args.trim(); let parts: Vec<&str> = args.split_whitespace().collect(); // 解析参数 let mut include_subagents = false; let mut filepath = None; for part in parts { if part == "+sub" { include_subagents = true; } else if !part.is_empty() { // 非特殊参数视为文件路径 filepath = Some(part.to_string()); } } return Ok(Some(Command::SaveTopic { filepath, include_subagents, })); } // 解析 /save-session 命令 - 保存整个会话 if trimmed == "/save-session" { return Ok(Some(Command::SaveSession { filepath: None, include_all: false, include_subagents: false, })); } if let Some(args) = trimmed.strip_prefix("/save-session ") { let args = args.trim(); let parts: Vec<&str> = args.split_whitespace().collect(); // 解析参数 let mut include_all = false; let mut include_subagents = false; let mut filepath = None; for part in parts { if part == "all" { include_all = true; } else if part == "+sub" { include_subagents = true; } else if !part.is_empty() { // 非特殊参数视为文件路径 filepath = Some(part.to_string()); } } return Ok(Some(Command::SaveSession { filepath, include_all, include_subagents, })); } // 解析 /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 命令 - 切换话题(支持 topic_id 或序号) if let Some(topic_id) = trimmed.strip_prefix("/use ") { let topic_id = topic_id.trim(); return Ok(Some(Command::SwitchTopic { topic_id: topic_id.to_string(), })); } // 解析 /current 命令 - 获取当前会话信息 if trimmed == "/current" { return Ok(Some(Command::GetCurrentSession)); } // 解析 /help 命令 - 显示所有支持的命令 if trimmed == "/help" { return Ok(Some(Command::Help)); } // 不是命令,返回 None Ok(None) } }