diff --git a/src/command/handlers/list_sessions.rs b/src/command/handlers/list_sessions.rs index ca3b9f1..ccdbb46 100644 --- a/src/command/handlers/list_sessions.rs +++ b/src/command/handlers/list_sessions.rs @@ -65,42 +65,26 @@ async fn handle_list_sessions( } else { let mut lines = vec![format!("Found {} topic(s):", topics.len())]; lines.push(String::new()); - lines.push("┌────┬─────────────────┬──────────────────────┬──────────┬─────────────────┐".to_string()); - lines.push("│ No │ Topic ID │ Title │ Messages │ Last Active │".to_string()); - lines.push("├────┼─────────────────┼──────────────────────┼──────────┼─────────────────┤".to_string()); for (idx, topic) in topics.iter().enumerate() { - let row_num = idx + 1; + let num = idx + 1; let is_current = topic.id == current_topic_id; - let num_marker = if is_current { " * ".to_string() } else { format!(" {:<2}", row_num) }; + let marker = if is_current { " *" } else { "" }; - let topic_id_display = if topic.id.len() > 15 { - format!("{}...", &topic.id[..12]) - } else { - topic.id.clone() - }; - let title_display = if topic.title.len() > 20 { - format!("{}...", &topic.title[..17]) - } else { - topic.title.clone() - }; - - let last_active = format_time_ago(topic.last_active_at); + // 使用辅助方法获取消息数量 + let msg_count = handler.store + .get_topic_message_count(&topic.id) + .unwrap_or(0); lines.push(format!( - "│{}│ {:<15} │ {:<20} │ {:<8} │ {:<15} │", - num_marker, - topic_id_display, - title_display, - topic.message_count, - last_active + "{}. {}{} ({})", + num, topic.title, marker, msg_count )); } - lines.push("└────┴─────────────────┴──────────────────────┴──────────┴─────────────────┘".to_string()); lines.push(String::new()); lines.push("* = current topic".to_string()); - lines.push("Use /use or /use to switch".to_string()); + lines.push("Use /use to switch".to_string()); lines.join("\n") }; @@ -113,24 +97,4 @@ async fn handle_list_sessions( .with_metadata("topics", &topics_json) .with_metadata("count", &topics.len().to_string()) .with_metadata("current_topic_id", current_topic_id)) -} - -fn format_time_ago(timestamp_ms: i64) -> String { - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_millis() as i64; - - let diff_ms = now - timestamp_ms; - let diff_secs = diff_ms / 1000; - - if diff_secs < 60 { - "just now".to_string() - } else if diff_secs < 3600 { - format!("{} mins ago", diff_secs / 60) - } else if diff_secs < 86400 { - format!("{} hours ago", diff_secs / 3600) - } else { - format!("{} days ago", diff_secs / 86400) - } } \ No newline at end of file diff --git a/src/command/handlers/switch_session.rs b/src/command/handlers/switch_session.rs index c8a75b3..a7629f0 100644 --- a/src/command/handlers/switch_session.rs +++ b/src/command/handlers/switch_session.rs @@ -97,14 +97,19 @@ async fn handle_switch_session( } } + // 使用辅助方法获取消息数量 + let msg_count = handler.store + .get_topic_message_count(&target_topic_id) + .unwrap_or(0); + let message = format!( "✓ Switched to topic: {} ({} messages)", - topic.title, topic.message_count + topic.title, msg_count ); Ok(CommandResponse::success(ctx.request_id) .with_message(MessageKind::Notification, &message) .with_metadata("topic_id", &topic.id) .with_metadata("title", &topic.title) - .with_metadata("message_count", &topic.message_count.to_string())) + .with_metadata("message_count", &msg_count.to_string())) } \ No newline at end of file diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 2d49256..389d83c 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1359,6 +1359,11 @@ impl SessionStore { Ok(messages) } + /// 获取指定话题的消息数量(动态计算,确保准确) + pub fn get_topic_message_count(&self, topic_id: &str) -> Result { + self.load_messages_for_topic(topic_id).map(|msgs| msgs.len()) + } + pub fn load_all_messages(&self, session_id: &str) -> Result, StorageError> { let conn = self.conn.lock().expect("session db mutex poisoned"); load_messages_after(&conn, session_id, 0)