Skip to main content

codetether_agent/tui/app/state/
mod.rs

1//! TUI application state — the central `AppState` struct and its methods.
2//!
3//! Methods are split into focused submodules by concern:
4//!
5//! - `agent_profile` — spawned agent types and profile lookup
6//! - `profile_defs` — named profile constants
7//! - `slash_commands` — `/command` constant table
8//! - `input_cursor` — cursor movement and text editing
9//! - `slash_suggest` — slash autocomplete suggestion methods
10//! - `scroll` — chat scroll sentinel scheme and tool-preview scroll
11//! - `session_nav` — session list filtering and selection
12//! - `worker_bridge` — A2A worker connection state
13//! - `history` — command history ↑/↓ navigation
14//! - `model_picker` — async model refresh from providers
15//! - `model_picker_nav` — synchronous model picker navigation
16//! - `timing` — request latency tracking
17//! - `steering` — queued steering messages
18//! - `settings_nav` — settings selection and view-mode switching
19//! - `message_cache` — render-line cache for performance
20
21#![allow(dead_code)]
22
23pub mod agent_profile;
24pub mod default_impl;
25pub mod history;
26pub mod input_cursor;
27pub mod input_edit;
28pub mod message_cache;
29pub mod model_picker;
30pub mod model_picker_nav;
31pub mod profile_defs;
32pub mod scroll;
33pub mod session_nav;
34pub mod settings_nav;
35pub mod slash_commands;
36pub mod slash_suggest;
37pub mod steering;
38pub mod timing;
39pub mod worker_bridge;
40
41#[cfg(test)]
42mod tests;
43
44// Re-exports so external `use crate::tui::app::state::X` still works.
45pub use agent_profile::{SpawnedAgent, agent_profile};
46pub use profile_defs::AgentProfile;
47pub use slash_commands::SLASH_COMMANDS;
48
49use std::collections::{HashMap, HashSet, VecDeque};
50use std::sync::Arc;
51use std::time::Instant;
52
53use crate::session::{ImageAttachment, Session, SessionSummary};
54use crate::tui::bus_log::BusLogState;
55use crate::tui::chat::message::ChatMessage;
56use crate::tui::help::HelpScrollState;
57use crate::tui::models::{InputMode, ViewMode};
58use crate::tui::ralph_view::RalphViewState;
59use crate::tui::swarm_view::SwarmViewState;
60use crate::tui::symbol_search::SymbolSearchState;
61use crate::tui::worker_bridge::IncomingTask;
62
63pub use crate::session::SessionEvent;
64
65#[derive(Default)]
66pub struct App {
67    pub state: AppState,
68}
69
70pub struct AppState {
71    pub view_mode: ViewMode,
72    pub input_mode: InputMode,
73    pub messages: Vec<ChatMessage>,
74    pub input: String,
75    pub input_cursor: usize,
76    pub input_scroll: usize,
77    pub chat_scroll: usize,
78    pub chat_last_max_scroll: usize,
79    pub tool_preview_scroll: usize,
80    pub tool_preview_last_max_scroll: usize,
81    pub status: String,
82    pub processing: bool,
83    pub session_id: Option<String>,
84    pub sessions: Vec<SessionSummary>,
85    pub selected_session: usize,
86    pub session_filter: String,
87    pub cwd_display: String,
88    pub bus_log: BusLogState,
89    pub swarm: SwarmViewState,
90    pub ralph: RalphViewState,
91    pub symbol_search: SymbolSearchState,
92    pub slash_suggestions: Vec<String>,
93    pub selected_slash_suggestion: usize,
94    pub command_history: Vec<String>,
95    pub history_index: Option<usize>,
96    pub worker_id: Option<String>,
97    pub worker_name: Option<String>,
98    pub a2a_connected: bool,
99    pub recent_tasks: Vec<String>,
100    pub worker_bridge_registered_agents: HashSet<String>,
101    pub worker_bridge_processing_state: Option<bool>,
102    pub worker_task_queue: VecDeque<IncomingTask>,
103    pub help_scroll: HelpScrollState,
104    pub show_help: bool,
105    pub available_models: Vec<String>,
106    pub selected_model_index: usize,
107    pub model_picker_active: bool,
108    pub model_filter: String,
109    pub streaming_text: String,
110    pub processing_started_at: Option<Instant>,
111    pub current_request_first_token_ms: Option<u64>,
112    pub current_request_last_token_ms: Option<u64>,
113    pub last_request_first_token_ms: Option<u64>,
114    pub last_request_last_token_ms: Option<u64>,
115    pub last_completion_model: Option<String>,
116    pub last_completion_latency_ms: Option<u64>,
117    pub last_completion_prompt_tokens: Option<usize>,
118    pub last_completion_output_tokens: Option<usize>,
119    pub last_tool_name: Option<String>,
120    pub last_tool_latency_ms: Option<u64>,
121    pub last_tool_success: Option<bool>,
122    pub pending_images: Vec<ImageAttachment>,
123    pub queued_steering: Vec<String>,
124    pub auto_apply_edits: bool,
125    pub allow_network: bool,
126    pub slash_autocomplete: bool,
127    pub use_worktree: bool,
128    pub selected_settings_index: usize,
129    pub mcp_registry: Arc<crate::tui::app::mcp::TuiMcpRegistry>,
130    pub spawned_agents: HashMap<String, SpawnedAgent>,
131    pub active_spawned_agent: Option<String>,
132    pub streaming_agent_texts: HashMap<String, String>,
133    pub cached_message_lines: Vec<ratatui::text::Line<'static>>,
134    pub cached_messages_len: usize,
135    pub cached_max_width: usize,
136    pub cached_streaming_snapshot: Option<String>,
137    pub cached_processing: bool,
138    pub cached_frozen_len: usize,
139    pub watchdog_notification: Option<super::watchdog::WatchdogNotification>,
140    pub main_watchdog_root_prompt: Option<String>,
141    pub main_last_event_at: Option<Instant>,
142    pub main_watchdog_restart_count: u32,
143    pub main_inflight_prompt: Option<String>,
144    pub okr_repository: Option<Arc<crate::okr::OkrRepository>>,
145    pub pending_okr_approval: Option<super::okr_gate::PendingOkrApproval>,
146    pub pending_smart_switch_retry: Option<crate::tui::app::smart_switch::PendingSmartSwitchRetry>,
147    pub smart_switch_retry_count: u32,
148    pub smart_switch_attempted_models: Vec<String>,
149    pub chat_sync_rx:
150        Option<tokio::sync::mpsc::UnboundedReceiver<crate::tui::chat::sync::ChatSyncUiEvent>>,
151    pub chat_sync_status: Option<String>,
152    pub chat_sync_last_success: Option<String>,
153    pub chat_sync_last_error: Option<String>,
154    pub chat_sync_uploaded_bytes: u64,
155    pub chat_sync_uploaded_batches: u64,
156    pub autochat: super::autochat::state::AutochatState,
157    pub file_picker_dir: std::path::PathBuf,
158    pub file_picker_entries: Vec<crate::tui::app::file_picker::FilePickerEntry>,
159    pub file_picker_selected: usize,
160    pub file_picker_filter: String,
161    pub file_picker_active: bool,
162    pub workspace: crate::tui::models::WorkspaceSnapshot,
163    pub chat_layout_mode: crate::tui::ui::webview::layout_mode::ChatLayoutMode,
164}