Skip to main content

mermaid_cli/tui/state/
conversation.rs

1/// Conversation state management
2///
3/// Handles chat messages, history, and persistence.
4
5use std::collections::VecDeque;
6
7use crate::context::Context;
8use crate::models::ChatMessage;
9use crate::session::{ConversationHistory, ConversationManager};
10
11/// Session state - conversation history and persistence
12pub struct ConversationState {
13    /// Current chat messages
14    pub messages: Vec<ChatMessage>,
15    /// Conversation manager for persistence
16    pub conversation_manager: Option<ConversationManager>,
17    /// Current conversation being tracked
18    pub current_conversation: Option<ConversationHistory>,
19    /// Input history for arrow key navigation (loaded from session)
20    pub input_history: VecDeque<String>,
21    /// Current position in history (None = editing current input, Some(i) = viewing history[i])
22    pub history_index: Option<usize>,
23    /// Saved input when navigating away from current draft
24    pub history_buffer: String,
25    /// Context for dynamic file tree reloading
26    pub context: Option<Context>,
27    /// Cumulative token count for the entire conversation
28    pub cumulative_tokens: usize,
29    /// Auto-generated conversation title (like Claude Code)
30    pub conversation_title: Option<String>,
31}
32
33impl ConversationState {
34    /// Create a new ConversationState with default values
35    pub fn new() -> Self {
36        Self {
37            messages: Vec::new(),
38            conversation_manager: None,
39            current_conversation: None,
40            input_history: VecDeque::new(),
41            history_index: None,
42            history_buffer: String::new(),
43            context: None,
44            cumulative_tokens: 0,
45            conversation_title: None,
46        }
47    }
48
49    /// Create ConversationState with conversation management
50    pub fn with_conversation(
51        conversation_manager: Option<ConversationManager>,
52        current_conversation: Option<ConversationHistory>,
53        input_history: VecDeque<String>,
54    ) -> Self {
55        Self {
56            messages: Vec::new(),
57            conversation_manager,
58            current_conversation,
59            input_history,
60            history_index: None,
61            history_buffer: String::new(),
62            context: None,
63            cumulative_tokens: 0,
64            conversation_title: None,
65        }
66    }
67
68    /// Add tokens to the cumulative count
69    pub fn add_tokens(&mut self, count: usize) {
70        self.cumulative_tokens += count;
71    }
72
73    /// Get message count
74    pub fn message_count(&self) -> usize {
75        self.messages.len()
76    }
77
78    /// Check if conversation is empty
79    pub fn is_empty(&self) -> bool {
80        self.messages.is_empty()
81    }
82}
83
84impl Default for ConversationState {
85    fn default() -> Self {
86        Self::new()
87    }
88}