Skip to main content

vtcode_ui/tui/core_tui/session/
impl_init.rs

1use super::*;
2
3impl Session {
4    pub(super) fn is_error_content(content: &str) -> bool {
5        // Check if message contains common error indicators
6        let lower_content = content.to_lowercase();
7        let error_indicators = [
8            "error:",
9            "error ",
10            "error\n",
11            "failed",
12            "failure",
13            "exception",
14            "invalid",
15            "not found",
16            "couldn't",
17            "can't",
18            "cannot",
19            "denied",
20            "forbidden",
21            "unauthorized",
22            "timeout",
23            "connection refused",
24            "no such",
25            "does not exist",
26        ];
27
28        error_indicators.iter().any(|indicator| lower_content.contains(indicator))
29    }
30
31    pub fn new(theme: InlineTheme, placeholder: Option<String>, view_rows: u16) -> Self {
32        Self::new_with_logs(theme, placeholder, view_rows, true, None, "Agent TUI".to_string())
33    }
34
35    pub(crate) fn new_with_logs(
36        theme: InlineTheme,
37        placeholder: Option<String>,
38        view_rows: u16,
39        show_logs: bool,
40        appearance: Option<AppearanceConfig>,
41        app_name: String,
42    ) -> Self {
43        Self::new_with_options(theme, placeholder, view_rows, show_logs, appearance, app_name, None)
44    }
45
46    pub(crate) fn new_with_bindings(
47        theme: InlineTheme,
48        placeholder: Option<String>,
49        view_rows: u16,
50        show_logs: bool,
51        appearance: Option<AppearanceConfig>,
52        app_name: String,
53        bindings: BindingStore,
54    ) -> Self {
55        Self::new_with_options(theme, placeholder, view_rows, show_logs, appearance, app_name, Some(bindings))
56    }
57
58    fn new_with_options(
59        theme: InlineTheme,
60        placeholder: Option<String>,
61        view_rows: u16,
62        show_logs: bool,
63        appearance: Option<AppearanceConfig>,
64        app_name: String,
65        bindings: Option<BindingStore>,
66    ) -> Self {
67        let resolved_rows = view_rows.max(2);
68        let initial_header_rows = ui::INLINE_HEADER_HEIGHT;
69        let reserved_rows = initial_header_rows + Self::input_block_height_for_lines(1);
70        let initial_transcript_rows = resolved_rows.saturating_sub(reserved_rows).max(1);
71
72        let appearance = appearance.unwrap_or_default();
73        let vim_mode_enabled = appearance.vim_mode;
74
75        let mut session = Self {
76            // --- Managers (Phase 2) ---
77            input_manager: InputManager::new(),
78            scroll_manager: ScrollManager::new(initial_transcript_rows),
79            user_scrolled: false,
80
81            // --- Message Management ---
82            lines: Vec::with_capacity(64),
83            collapsed_pastes: Vec::new(),
84            thinking_runs: ThinkingRunIndex::default(),
85            styles: SessionStyles::new(theme.clone()),
86            theme,
87            appearance,
88            auto_color_scheme: false,
89            header_context: InlineHeaderContext::default(),
90            labels: MessageLabels::default(),
91
92            // --- Prompt/Input Display ---
93            prompt_prefix: USER_PREFIX.to_string(),
94            prompt_style: InlineTextStyle::default(),
95            placeholder,
96            placeholder_style: None,
97            input_status_left: None,
98            footer_context_status: None,
99            footer_context_right: None,
100            footer_context_configured: false,
101            input_status_right: None,
102            copy_notification_until: None,
103            copy_notification_failed: false,
104            copy_notification_chars: 0,
105            drag_auto_scroll: None,
106            input_compact_mode: false,
107
108            // --- UI State ---
109            navigation_state: ListState::default(), // Kept for backward compatibility
110            input_enabled: true,
111            program_status: super::super::program_status::ProgramStatus::default(),
112            activity_state: ActivityState::Idle,
113            image_input_enabled: false,
114            cursor_visible: true,
115            render_state: RenderState::new(),
116            transcript_clear_required: true,
117            evicted_message_count: 0,
118            pending_new_messages: 0,
119            last_change_line_idx: None,
120            transcript_flash: None,
121            should_exit: false,
122            last_interrupt_press: None,
123            last_escape_press: None,
124            scroll_cursor_steady_until: None,
125            last_shimmer_active: false,
126            view_rows: resolved_rows,
127            input_height: Self::input_block_height_for_lines(1),
128            transcript_rows: initial_transcript_rows,
129            transcript_width: 0,
130            transcript_view_top: 0,
131            areas: SessionAreas::default(),
132            sticky_prompt_target: None,
133            sticky_prompt_preview_cache: None,
134            leading_user_prompt_truncated: false,
135            transcript_file_link_targets: Vec::new(),
136            modal_link_targets: Vec::new(),
137            hovered_transcript_file_link: None,
138            last_mouse_position: None,
139            last_link_open: None,
140            pending_link_open: None,
141            held_key_modifiers: KeyModifiers::empty(),
142
143            // --- Logging ---
144            log_receiver: None,
145            log_lines: VecDeque::with_capacity(MAX_LOG_LINES),
146            log_cached_text: None,
147            log_evicted: false,
148            show_logs,
149
150            // --- Rendering ---
151            transcript_cache: None,
152            visible_lines_cache: None,
153            queued_inputs: Vec::with_capacity(4),
154            local_agents: Vec::new(),
155            active_plan: None,
156            local_agents_drawer_visible: false,
157            subprocess_entries: Vec::new(),
158            subagent_preview: None,
159            queue_overlay_cache: None,
160            queue_overlay_version: 0,
161            active_overlay: None,
162            overlay_queue: VecDeque::new(),
163            last_overlay_list_selection: None,
164            last_overlay_list_was_last: false,
165            header_rows: initial_header_rows,
166            line_revision_counter: 0,
167            transcript_presentation_revision: 0,
168            first_dirty_line: None,
169            in_tool_code_fence: false,
170
171            // --- Prompt Suggestions ---
172            suggested_prompt_state: SuggestedPromptState::default(),
173            inline_prompt_suggestion: InlinePromptSuggestionState::default(),
174
175            // --- Thinking Indicator ---
176            thinking_spinner: ThinkingSpinner::new(),
177            shimmer_state: ShimmerState::new(),
178            progress: progress::TransientProgress::default(),
179
180            // --- Reverse Search ---
181            reverse_search_state: reverse_search::ReverseSearchState::new(),
182
183            // --- PTY Session Management ---
184            active_pty_sessions: None,
185
186            // --- Background Activity ---
187            background_activity_count: 0,
188            background_finished_count: 0,
189            background_indicator_hits: Vec::new(),
190
191            // --- Keybinding store ---
192            bindings: bindings.unwrap_or_default(),
193
194            // --- Clipboard for yank/paste operations ---
195            clipboard: String::new(),
196            vim_state: VimState::new(vim_mode_enabled),
197
198            // --- Mouse Text Selection ---
199            mouse_selection: MouseSelectionState::new(),
200            mouse_drag_target: MouseDragTarget::None,
201            fullscreen: FullscreenSessionState::default(),
202
203            // --- Performance Caching ---
204            header_lines_cache: None,
205            header_block_title_cache: None,
206            header_height_cache: hashbrown::HashMap::new(),
207            queued_inputs_preview_cache: None,
208            subprocess_entries_preview_cache: None,
209            input_render_cache: None,
210
211            // --- Terminal Title ---
212            app_name,
213            workspace_root: None,
214            terminal_title_items: None,
215            terminal_title_thread_label: None,
216            terminal_title_git_branch: None,
217            terminal_title_task_progress: None,
218            last_terminal_title: None,
219
220            // --- Streaming State ---
221            is_streaming_final_answer: false,
222        };
223        session.ensure_prompt_style_color();
224        session
225    }
226
227    pub(super) fn clear_thinking_spinner_if_active(&mut self, kind: InlineMessageKind) {
228        // Clear spinner when any substantive agent output arrives
229        if matches!(
230            kind,
231            InlineMessageKind::Agent | InlineMessageKind::Policy | InlineMessageKind::Tool | InlineMessageKind::Error
232        ) && self.thinking_spinner.is_active
233        {
234            self.thinking_spinner.stop();
235            self.mark_visual_dirty();
236        }
237    }
238}