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            terminal_visible: true,
116            clipboard_5522: Clipboard5522State::default(),
117            render_state: RenderState::new(),
118            transcript_clear_required: true,
119            evicted_message_count: 0,
120            pending_new_messages: 0,
121            last_change_line_idx: None,
122            transcript_flash: None,
123            should_exit: false,
124            last_interrupt_press: None,
125            last_escape_press: None,
126            scroll_cursor_steady_until: None,
127            last_shimmer_active: false,
128            view_rows: resolved_rows,
129            input_height: Self::input_block_height_for_lines(1),
130            transcript_rows: initial_transcript_rows,
131            transcript_width: 0,
132            transcript_view_top: 0,
133            areas: SessionAreas::default(),
134            sticky_prompt_target: None,
135            sticky_prompt_preview_cache: None,
136            leading_user_prompt_truncated: false,
137            transcript_file_link_targets: Vec::new(),
138            modal_link_targets: Vec::new(),
139            hovered_transcript_file_link: None,
140            last_mouse_position: None,
141            last_link_open: None,
142            pending_link_open: None,
143            held_key_modifiers: KeyModifiers::empty(),
144
145            // --- Logging ---
146            log_receiver: None,
147            log_lines: VecDeque::with_capacity(MAX_LOG_LINES),
148            log_cached_text: None,
149            log_evicted: false,
150            show_logs,
151
152            // --- Rendering ---
153            transcript_cache: None,
154            visible_lines_cache: None,
155            queued_inputs: Vec::with_capacity(4),
156            local_agents: Vec::new(),
157            active_plan: None,
158            local_agents_drawer_visible: false,
159            subprocess_entries: Vec::new(),
160            subagent_preview: None,
161            queue_overlay_cache: None,
162            queue_overlay_version: 0,
163            active_overlay: None,
164            overlay_queue: VecDeque::new(),
165            last_overlay_list_selection: None,
166            last_overlay_list_was_last: false,
167            header_rows: initial_header_rows,
168            line_revision_counter: 0,
169            transcript_presentation_revision: 0,
170            first_dirty_line: None,
171            in_tool_code_fence: false,
172
173            // --- Prompt Suggestions ---
174            suggested_prompt_state: SuggestedPromptState::default(),
175            inline_prompt_suggestion: InlinePromptSuggestionState::default(),
176
177            // --- Thinking Indicator ---
178            thinking_spinner: ThinkingSpinner::new(),
179            shimmer_state: ShimmerState::new(),
180            progress: progress::TransientProgress::default(),
181
182            // --- Reverse Search ---
183            reverse_search_state: reverse_search::ReverseSearchState::new(),
184
185            // --- PTY Session Management ---
186            active_pty_sessions: None,
187
188            // --- Background Activity ---
189            background_activity_count: 0,
190            background_finished_count: 0,
191            background_indicator_hits: Vec::new(),
192
193            // --- Keybinding store ---
194            bindings: bindings.unwrap_or_default(),
195
196            // --- Clipboard for yank/paste operations ---
197            clipboard: String::new(),
198            vim_state: VimState::new(vim_mode_enabled),
199
200            // --- Mouse Text Selection ---
201            mouse_selection: MouseSelectionState::new(),
202            mouse_drag_target: MouseDragTarget::None,
203            fullscreen: FullscreenSessionState::default(),
204
205            // --- Performance Caching ---
206            header_lines_cache: None,
207            header_block_title_cache: None,
208            header_height_cache: hashbrown::HashMap::new(),
209            queued_inputs_preview_cache: None,
210            subprocess_entries_preview_cache: None,
211            input_render_cache: None,
212
213            // --- Terminal Title ---
214            app_name,
215            workspace_root: None,
216            terminal_title_items: None,
217            terminal_title_thread_label: None,
218            terminal_title_git_branch: None,
219            terminal_title_task_progress: None,
220            last_terminal_title: None,
221
222            // --- Streaming State ---
223            is_streaming_final_answer: false,
224        };
225        session.ensure_prompt_style_color();
226        session
227    }
228
229    pub(super) fn clear_thinking_spinner_if_active(&mut self, kind: InlineMessageKind) {
230        // Clear spinner when any substantive agent output arrives
231        if matches!(
232            kind,
233            InlineMessageKind::Agent | InlineMessageKind::Policy | InlineMessageKind::Tool | InlineMessageKind::Error
234        ) && self.thinking_spinner.is_active
235        {
236            self.thinking_spinner.stop();
237            self.mark_visual_dirty();
238        }
239    }
240}