pub mod delete_topic;
pub mod get_current;
pub mod help;
pub mod list_channels;
pub mod list_memories;
pub mod list_scheduler_jobs;
pub mod list_skills;
pub mod list_todos;
pub mod memory_crud;
pub mod list_sessions;
pub mod list_sessions_by_channel;
pub mod list_topics;
pub mod load_chat_messages;
pub mod load_task_messages;
pub mod load_topic;
pub mod save_session;
pub mod save_topic;
pub mod session;
pub mod stop_execution;
pub mod switch_topic;

// 导出公共函数供其他模块复用
pub use save_session::{
    escape_yaml_string, format_message_content, format_timestamp,
    generate_messages_markdown, generate_system_prompt_markdown,
    generate_subagent_tasks_markdown, load_subagent_data, SubagentTaskData,
};

use crate::bus::ChatMessage;
use crate::command::response::CommandError;
use crate::gateway::session::SessionManager;

/// 从 Session 内存获取消息历史（供命令使用）
pub async fn get_messages_from_session(
    session_manager: &Option<SessionManager>,
    channel_name: &str,
    chat_id: &str,
) -> Result<Vec<ChatMessage>, CommandError> {
    let session_manager = session_manager.as_ref().ok_or_else(|| {
        CommandError::new(
            "SESSION_MANAGER_NOT_SET",
            "Session manager not configured".to_string(),
        )
    })?;

    match session_manager.get(channel_name).await {
        Some(session) => {
            let guard = session.lock().await;
            Ok(guard
                .get_history(chat_id)
                .map(|m| m.clone())
                .unwrap_or_default())
        }
        None => Err(CommandError::new(
            "SESSION_NOT_FOUND",
            format!("Session not found for channel: {}", channel_name),
        )),
    }
}
