mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Smooth transitions between TUI reasoning headings (#64202)
## Why Streaming reasoning summaries can replace the status heading before the current line finishes, making fast updates hard to read. ## What changed With shimmer active, sample the latest usable reasoning heading every 3.5 seconds while letting the visible line finish as its text arrives. Fade out the previous heading and reveal the next one across the visible terminal columns. Retry messages still appear immediately, and reduced motion and unsupported terminals keep the static display. Use the same prepared frame for layout and rendering so the status row can reflow when the heading changes. ## Testing Cover chunks that finish one heading and start another before a draw, delayed frames, and split Markdown bold markers. Snapshot the transition in light and dark themes and verify that hook status text reflows at the fade boundary. GitOrigin-RevId: 63c579602f030f461ac81c4841106b29755710b1
This commit is contained in:
@@ -39,7 +39,7 @@ pub(super) fn on_metadata(chat: &mut ChatWidget, notification: TurnModerationMet
|
||||
chat.clear_safety_buffering();
|
||||
chat.bottom_pane.ensure_status_indicator();
|
||||
chat.set_status_header(
|
||||
"I can’t help with this request. Preparing alternative suggestions…".into(),
|
||||
"I can’t help with this request. Preparing alternative suggestions…".to_owned(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ impl ChatWidget {
|
||||
/// requested a redraw.
|
||||
pub(super) fn set_status(
|
||||
&mut self,
|
||||
header: String,
|
||||
header: impl Into<StatusHeader>,
|
||||
details: Option<String>,
|
||||
details_capitalization: StatusDetailsCapitalization,
|
||||
details_max_lines: usize,
|
||||
@@ -27,12 +27,12 @@ impl ChatWidget {
|
||||
&& self.status_state.retry_status_header.is_none()
|
||||
{
|
||||
(
|
||||
compaction::COMPACTION_HEADER.to_string(),
|
||||
compaction::COMPACTION_HEADER.to_string().into(),
|
||||
Some(compaction::COMPACTION_DETAILS.to_string()),
|
||||
STATUS_DETAILS_DEFAULT_MAX_LINES,
|
||||
)
|
||||
} else {
|
||||
(header, details, details_max_lines)
|
||||
(header.into(), details, details_max_lines)
|
||||
};
|
||||
let details = details
|
||||
.filter(|details| !details.is_empty())
|
||||
@@ -46,7 +46,7 @@ impl ChatWidget {
|
||||
}
|
||||
});
|
||||
self.status_state.set_status(StatusIndicatorState {
|
||||
header: header.clone(),
|
||||
header: header.text.clone(),
|
||||
details: details.clone(),
|
||||
details_max_lines,
|
||||
});
|
||||
@@ -75,7 +75,7 @@ impl ChatWidget {
|
||||
/// Convenience wrapper around [`Self::set_status`];
|
||||
/// updates the status indicator header and clears any existing details, returning whether the
|
||||
/// visible status indicator requested a redraw.
|
||||
pub(super) fn set_status_header(&mut self, header: String) -> bool {
|
||||
pub(super) fn set_status_header(&mut self, header: impl Into<StatusHeader>) -> bool {
|
||||
self.set_status(
|
||||
header,
|
||||
/*details*/ None,
|
||||
|
||||
@@ -13,24 +13,23 @@ fn streamed_message_matches_completed(streamed: &str, completed: &str) -> bool {
|
||||
streamed == completed || streamed.strip_suffix('\n') == Some(completed)
|
||||
}
|
||||
|
||||
fn latest_summary_line(text: &str) -> Option<String> {
|
||||
text.lines().rev().find_map(|line| {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.starts_with("<!--") {
|
||||
return None;
|
||||
}
|
||||
let line = line.trim_start_matches('#').trim();
|
||||
let line = if let Some(stripped) = line.strip_prefix("**") {
|
||||
let (bold, trailing) = stripped.split_once("**")?;
|
||||
format!("{bold}{trailing}")
|
||||
} else {
|
||||
line.to_string()
|
||||
};
|
||||
(!line.is_empty()).then_some(line)
|
||||
})
|
||||
}
|
||||
|
||||
impl ChatWidget {
|
||||
/// Finish the visible line and remember the latest one for the next sample.
|
||||
fn refresh_reasoning_heading(&mut self) -> bool {
|
||||
let item_id = self.status_state.reasoning_item_id.as_deref();
|
||||
let part = self.reasoning_summary_parts.len();
|
||||
if let Some(row) = self.bottom_pane.status_widget_mut() {
|
||||
row.extend_reasoning(item_id, part, &self.reasoning_buffer);
|
||||
}
|
||||
if let Some(heading) = StatusHeader::from_summary(item_id, part, &self.reasoning_buffer)
|
||||
&& self.reasoning_header.as_ref() != Some(&heading)
|
||||
{
|
||||
self.reasoning_header = Some(heading);
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub(super) fn on_reasoning_item_started(&mut self, id: String) {
|
||||
if self.status_state.reasoning_resume_turn_id.take().is_some()
|
||||
&& self.status_state.reasoning_item_id.as_ref() != Some(&id)
|
||||
@@ -54,8 +53,7 @@ impl ChatWidget {
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.reasoning_header =
|
||||
latest_summary_line(&self.reasoning_buffer).or(self.reasoning_header.take());
|
||||
self.refresh_reasoning_heading();
|
||||
if let Some(header) = self.reasoning_header.clone() {
|
||||
self.status_state.terminal_title_status_kind = TerminalTitleStatusKind::Thinking;
|
||||
self.set_status_header(header);
|
||||
@@ -457,28 +455,28 @@ impl ChatWidget {
|
||||
return;
|
||||
}
|
||||
|
||||
self.reasoning_header =
|
||||
latest_summary_line(&self.reasoning_buffer).or(self.reasoning_header.take());
|
||||
let Some(header) = self.reasoning_header.as_deref() else {
|
||||
let changed = self.refresh_reasoning_heading();
|
||||
let Some(header) = &self.reasoning_header else {
|
||||
// No usable summary has arrived yet.
|
||||
return;
|
||||
};
|
||||
|
||||
let status = &self.status_state.current_status;
|
||||
if self.status_state.terminal_title_status_kind == TerminalTitleStatusKind::Thinking
|
||||
&& status.header == header
|
||||
if !changed
|
||||
&& self.status_state.terminal_title_status_kind == TerminalTitleStatusKind::Thinking
|
||||
&& status.header == header.text
|
||||
&& status.details.is_none()
|
||||
&& status.details_max_lines == STATUS_DETAILS_DEFAULT_MAX_LINES
|
||||
&& self
|
||||
.bottom_pane
|
||||
.status_widget()
|
||||
.is_none_or(|status| status.header() == header)
|
||||
.is_none_or(|status| status.header() == header.text)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the shimmer header to the extracted reasoning chunk header.
|
||||
let header = header.to_string();
|
||||
let header = header.clone();
|
||||
self.status_state.terminal_title_status_kind = TerminalTitleStatusKind::Thinking;
|
||||
if !self.set_status_header(header) {
|
||||
self.request_redraw();
|
||||
@@ -491,11 +489,13 @@ impl ChatWidget {
|
||||
self.reasoning_summary_parts
|
||||
.push(std::mem::take(&mut self.reasoning_buffer));
|
||||
}
|
||||
let item_id = self.status_state.reasoning_item_id.as_deref();
|
||||
self.reasoning_header = self
|
||||
.reasoning_summary_parts
|
||||
.iter()
|
||||
.enumerate()
|
||||
.rev()
|
||||
.find_map(|part| latest_summary_line(part))
|
||||
.find_map(|(index, part)| StatusHeader::from_summary(item_id, index, part))
|
||||
.or(self.reasoning_header.take());
|
||||
if !self.reasoning_summary_parts.is_empty() {
|
||||
let reasoning_parts = std::mem::take(&mut self.reasoning_summary_parts);
|
||||
|
||||
@@ -33,6 +33,66 @@ fn complete(chat: &mut ChatWidget, id: &str) {
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_heading(chat: &mut ChatWidget, start: Instant, times_ms: &[u64], expected: &str) {
|
||||
let row = chat.bottom_pane.status_widget_mut().unwrap();
|
||||
for &ms in times_ms {
|
||||
let frame = row.render_heading_at(start + Duration::from_millis(ms));
|
||||
assert_eq!(frame, expected, "at {ms} ms");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn finishes_the_visible_heading_when_the_next_heading_arrives_before_a_draw() {
|
||||
for chunks in [
|
||||
&["ing files", "\nRunning tests"][..],
|
||||
&["ing files\nRunning tests"],
|
||||
// A second line with the same prefix is still a distinct heading.
|
||||
&["ing files\nReading files", " again", "\nRunning tests"],
|
||||
] {
|
||||
let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
chat.on_task_started();
|
||||
handle_agent_reasoning_started(&mut chat, "first");
|
||||
delta(&mut chat, "first", "Starting");
|
||||
delta(&mut chat, "first", "\nRead");
|
||||
let start = Instant::now();
|
||||
assert_heading(&mut chat, start, &[0], "Read");
|
||||
for chunk in chunks {
|
||||
delta(&mut chat, "first", chunk);
|
||||
}
|
||||
assert_eq!(
|
||||
chat.bottom_pane.status_widget().unwrap().header(),
|
||||
"Running tests"
|
||||
);
|
||||
// The first draw after the tick is 30 ms late. The second tick is still at 7000 ms.
|
||||
assert_heading(&mut chat, start, &[32, 3499, 3530], "Reading files");
|
||||
delta(&mut chat, "first", "\nEditing");
|
||||
assert_heading(&mut chat, start, &[4270, 6999], "Running tests");
|
||||
delta(&mut chat, "first", "\nTesting");
|
||||
assert_heading(&mut chat, start, &[7000], "Running tests");
|
||||
assert_heading(&mut chat, start, &[7740], "Testing");
|
||||
delta(&mut chat, "first", "\nFinishing");
|
||||
chat.on_stream_error("Reconnecting".into(), /*additional_details*/ None);
|
||||
assert_heading(&mut chat, start, &[7800, 11000], "Reconnecting");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_split_bold_opener_waits_for_the_heading_text() {
|
||||
let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
chat.on_task_started();
|
||||
handle_agent_reasoning_started(&mut chat, "first");
|
||||
let start = Instant::now();
|
||||
for (ms, chunk) in [(0, "*"), (32, "*Reading files*")] {
|
||||
delta(&mut chat, "first", chunk);
|
||||
assert_heading(&mut chat, start, &[ms], "Working");
|
||||
}
|
||||
// Repeating Working between draws should preserve the handoff from that label.
|
||||
chat.restore_reasoning_status_header();
|
||||
delta(&mut chat, "first", "*");
|
||||
assert_heading(&mut chat, start, &[64], "Working");
|
||||
assert_heading(&mut chat, start, &[1000], "Reading files");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reasoning_status_accepts_bold_text_with_a_plain_suffix() {
|
||||
let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
|
||||
@@ -5392,13 +5392,16 @@ async fn reasoning_delta_redraws_when_latest_usable_line_changes() {
|
||||
|
||||
chat.on_agent_reasoning_delta(" files**".to_string());
|
||||
assert!(draw_rx.try_recv().is_ok());
|
||||
assert_eq!(chat.reasoning_header.as_deref(), Some("Checking files"));
|
||||
assert_eq!(
|
||||
chat.reasoning_header.as_ref().unwrap().text,
|
||||
"Checking files"
|
||||
);
|
||||
|
||||
chat.on_agent_reasoning_delta("\nPreparing a response".to_string());
|
||||
assert!(draw_rx.try_recv().is_ok());
|
||||
assert_eq!(
|
||||
chat.reasoning_header.as_deref(),
|
||||
Some("Preparing a response")
|
||||
chat.reasoning_header.as_ref().unwrap().text,
|
||||
"Preparing a response"
|
||||
);
|
||||
chat.on_agent_reasoning_delta("".to_string());
|
||||
assert!(matches!(
|
||||
@@ -5423,7 +5426,10 @@ async fn reasoning_delta_does_not_double_schedule_visible_status_redraw() {
|
||||
|
||||
chat.on_agent_reasoning_delta("**Checking files**".to_string());
|
||||
|
||||
assert_eq!(chat.reasoning_header.as_deref(), Some("Checking files"));
|
||||
assert_eq!(
|
||||
chat.reasoning_header.as_ref().unwrap().text,
|
||||
"Checking files"
|
||||
);
|
||||
assert!(draw_rx.try_recv().is_ok());
|
||||
assert!(matches!(
|
||||
draw_rx.try_recv(),
|
||||
|
||||
Reference in New Issue
Block a user