Skip to main content

mermaid_cli/tui/state/
operation.rs

1//! Operation state management
2//!
3//! Minimal state for tracking active operations.
4
5use std::collections::VecDeque;
6use std::sync::{Arc, Mutex};
7
8use crate::agents::SubagentProgress;
9use crate::utils::MutexExt;
10
11/// Operation state - tracks tool calls and queued messages
12pub struct OperationState {
13    /// Accumulated tool calls during streaming (persists across process_stream_chunks calls)
14    pub accumulated_tool_calls: Vec<crate::models::ToolCall>,
15    /// Queued messages - typed while model is generating, will be sent in order
16    pub queued_messages: VecDeque<String>,
17    /// Shared progress state for active subagents (None when no agents running)
18    pub active_subagents: Option<Arc<Mutex<Vec<SubagentProgress>>>>,
19}
20
21impl OperationState {
22    /// Create a new OperationState with default values
23    pub fn new() -> Self {
24        Self {
25            accumulated_tool_calls: Vec::new(),
26            queued_messages: VecDeque::new(),
27            active_subagents: None,
28        }
29    }
30
31    /// Queue a message to be sent after current generation
32    pub fn queue_message(&mut self, message: String) {
33        self.queued_messages.push_back(message);
34    }
35
36    /// Take the next queued message (removes from front of queue)
37    pub fn take_queued_message(&mut self) -> Option<String> {
38        self.queued_messages.pop_front()
39    }
40
41    /// Check if there are any queued messages
42    pub fn has_queued_message(&self) -> bool {
43        !self.queued_messages.is_empty()
44    }
45
46    /// Get all queued messages for display (doesn't remove them)
47    pub fn get_queued_messages(&self) -> &VecDeque<String> {
48        &self.queued_messages
49    }
50
51    /// Get the count of queued messages
52    pub fn queued_message_count(&self) -> usize {
53        self.queued_messages.len()
54    }
55
56    /// Snapshot the active subagent progress for rendering.
57    /// Locks the mutex briefly and clones the vec (max 10 small entries).
58    pub fn snapshot_subagent_progress(&self) -> Option<Vec<SubagentProgress>> {
59        self.active_subagents
60            .as_ref()
61            .map(|p| p.lock_mut_safe().clone())
62    }
63
64    /// Clear the active subagents state
65    pub fn clear_subagents(&mut self) {
66        self.active_subagents = None;
67    }
68}
69
70impl Default for OperationState {
71    fn default() -> Self {
72        Self::new()
73    }
74}