Skip to content

Commit 12418cc

Browse files
authored
fix: reset 'Working x seconds' timer when new prompt is submitted (#96)
* fix: reset 'Working x seconds' timer when new prompt is submitted Previously, the timer would not reset if a previous conversation turn didn't properly call full_reset(). This change adds a reset_timer parameter to StreamingState::start() that allows callers to explicitly control whether the prompt timer should be reset. - New user prompts (handle_submit_with_provider, handle_submit) now reset the timer by passing reset_timer=true - Tool continuations (send_tool_results_to_llm) preserve the timer by passing reset_timer=false This ensures the 'Working x seconds' indicator always starts from 0 when a user sends a new prompt, while maintaining correct behavior during multi-step tool execution chains. * fix: correct doc comment indentation for Clippy
1 parent f3cceef commit 12418cc

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/cortex-tui/src/app/state.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,14 @@ impl AppState {
460460
// ============================================================================
461461

462462
impl AppState {
463-
/// Start streaming a response
464-
pub fn start_streaming(&mut self, tool: Option<String>) {
465-
self.streaming.start(tool);
463+
/// Start streaming a response.
464+
///
465+
/// # Arguments
466+
/// * `tool` - Optional tool name being executed
467+
/// * `reset_timer` - If true, resets the prompt elapsed timer (use for new user prompts).
468+
/// If false, preserves existing timer (use for tool continuations).
469+
pub fn start_streaming(&mut self, tool: Option<String>, reset_timer: bool) {
470+
self.streaming.start(tool, reset_timer);
466471
// Use typewriter only if streaming animation is enabled
467472
if self.streaming_enabled {
468473
self.typewriter = Some(Typewriter::dynamic(String::new(), 500.0));

src/cortex-tui/src/app/streaming.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ pub struct StreamingState {
2121
}
2222

2323
impl StreamingState {
24-
pub fn start(&mut self, tool: Option<String>) {
24+
/// Start streaming with option to reset the prompt timer.
25+
///
26+
/// # Arguments
27+
/// * `tool` - Optional tool name being executed
28+
/// * `reset_timer` - If true, resets `prompt_started_at` to now (use for new user prompts).
29+
/// If false, preserves existing timer (use for tool continuations).
30+
pub fn start(&mut self, tool: Option<String>, reset_timer: bool) {
2531
self.is_streaming = true;
2632
self.thinking = true;
2733
self.current_tool = tool;
2834
self.task_started_at = Some(Instant::now());
29-
// Only set prompt_started_at if not already set (first call in a turn)
30-
if self.prompt_started_at.is_none() {
35+
// Reset prompt timer only when explicitly requested (new user prompt)
36+
// or when not yet set (first call)
37+
if reset_timer || self.prompt_started_at.is_none() {
3138
self.prompt_started_at = Some(Instant::now());
3239
}
3340
}

src/cortex-tui/src/runner/event_loop/input.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,9 @@ impl EventLoop {
617617
match event {
618618
AppEvent::StreamingStarted => {
619619
self.stream_controller.start_processing();
620-
self.app_state.start_streaming(None);
620+
// Don't reset timer here - this is triggered by backend TaskStarted event
621+
// which could be either a new prompt or a continuation
622+
self.app_state.start_streaming(None, false);
621623
}
622624

623625
AppEvent::StreamingChunk(chunk) => {

src/cortex-tui/src/runner/event_loop/streaming.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl EventLoop {
111111

112112
// Start streaming UI state
113113
self.stream_controller.start_processing();
114-
self.app_state.start_streaming(None);
114+
self.app_state.start_streaming(None, true); // Reset timer for new user prompt
115115

116116
// Reset cancellation flag and stream_done flag for new request
117117
self.streaming_cancelled.store(false, Ordering::SeqCst);
@@ -690,7 +690,7 @@ impl EventLoop {
690690

691691
// Start streaming UI state
692692
self.stream_controller.start_processing();
693-
self.app_state.start_streaming(None);
693+
self.app_state.start_streaming(None, false); // Don't reset timer for tool continuation
694694

695695
// Reset cancellation flag and stream_done flag
696696
self.streaming_cancelled.store(false, Ordering::SeqCst);

src/cortex-tui/src/runner/handlers/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'a> ActionHandler<'a> {
3434
// Send to backend
3535
if let Some(session) = self.session {
3636
session.send_message(text).await?;
37-
self.state.start_streaming(None);
37+
self.state.start_streaming(None, true); // Reset timer for new user prompt
3838
self.stream.start_processing();
3939
}
4040

0 commit comments

Comments
 (0)