Files
codex/codex-rs/tui/src/chatwidget/realtime_split_flap.rs
T
Benjamin Carlsson 3caf9f9586 Style spoken prompts and link workspace files in voice transcripts (#43676)
## What changed

- Render spoken user prompts with a bold red chevron, preserving its color during live transcript animation. Trim leading whitespace for display while retaining the raw transcript text.
- Underline and link file references such as `src/lib.rs:42` in spoken assistant transcripts using terminal hyperlinks. Preserve existing links and support Windows-style relative separators.
- Restrict generated file links to existing files with allowed extensions inside the workspace, rejecting hidden paths, traversal, and symlink escapes.

## Testing

Add rendering and transcript tests for the spoken marker, raw text preservation, file links, Unicode column offsets, and Windows-style paths, plus validation tests for rejected file destinations.

GitOrigin-RevId: 5fa180aa606acdc579a916441520602cb2bf3655
2026-09-08 03:15:37 +00:00

330 lines
11 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! A bounded, presentation-only split-flap board for live voice transcripts.
use super::HistoryCell;
use crate::motion::MotionMode;
use crate::terminal_hyperlinks::HyperlinkLine;
use crate::terminal_hyperlinks::visible_lines;
use crate::tui::FrameRequester;
use ratatui::style::Color;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::text::Span;
use std::time::Duration;
use std::time::Instant;
use unicode_segmentation::UnicodeSegmentation;
#[cfg(test)]
#[path = "realtime_split_flap_tests.rs"]
mod tests;
const FRAME_INTERVAL: Duration = Duration::from_millis(45);
const ANIMATION_DURATION: Duration = Duration::from_millis(675);
const TILE_SETTLE_DURATION: Duration = Duration::from_millis(180);
const TILE_AFTERGLOW_DURATION: Duration = Duration::from_millis(135);
const FLAP_SAMPLE_WINDOW: usize = 24;
/// Four bounded, calibrated amplitude samples; this is not frequency analysis.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct VoiceAmplitudeHistory([usize; 4]);
impl VoiceAmplitudeHistory {
pub(super) fn push(&mut self, level: usize) {
self.0.rotate_left(/*mid*/ 1);
self.0[3] = level.min(/*other*/ 5);
}
pub(super) fn glyphs(&self) -> String {
const LEFT_DOTS: [u32; 6] = [0, 0x40, 0x44, 0x46, 0x47, 0x47];
const RIGHT_DOTS: [u32; 6] = [0, 0x80, 0xa0, 0xb0, 0xb8, 0xb8];
self.0
.chunks_exact(/*chunk_size*/ 2)
.map(|levels| {
char::from_u32(0x2800 | LEFT_DOTS[levels[0]] | RIGHT_DOTS[levels[1]])
.unwrap_or('\u{2800}')
})
.collect()
}
pub(super) fn peak(&self) -> usize {
self.0.iter().copied().max().unwrap_or_default()
}
}
#[derive(Debug)]
pub(super) struct SplitFlapTranscriptCell {
inner: Box<dyn HistoryCell>,
role: String,
target: String,
visible_target: String,
flap_sample: Vec<u8>,
tile_arrivals: Vec<Instant>,
started_at: Instant,
phase_started_at: Instant,
motion_mode: MotionMode,
frame_requester: FrameRequester,
}
impl SplitFlapTranscriptCell {
pub(super) fn new(
inner: Box<dyn HistoryCell>,
role: &str,
target: &str,
previous: Option<&Self>,
motion_mode: MotionMode,
frame_requester: FrameRequester,
) -> Self {
let now = Instant::now();
let visible_target = visible_glyphs(inner.as_ref());
let mut flap_sample = visible_target
.bytes()
.rev()
.filter(u8::is_ascii_alphabetic)
.take(FLAP_SAMPLE_WINDOW)
.collect::<Vec<_>>();
if flap_sample.is_empty() {
flap_sample.extend(visible_target.bytes().rev().take(FLAP_SAMPLE_WINDOW));
}
flap_sample.reverse();
let previous = previous.filter(|cell| {
cell.role == role
&& target.starts_with(&cell.target)
&& visible_target.starts_with(&cell.visible_target)
});
let mut tile_arrivals = previous
.map(|cell| cell.tile_arrivals.clone())
.unwrap_or_default();
let existing_tiles = tile_arrivals.len();
for index in existing_tiles..visible_target.len() {
let offset = u64::try_from(index.saturating_sub(existing_tiles))
.unwrap_or(u64::MAX)
.saturating_mul(/*rhs*/ 8)
.min(/*other*/ 56);
let start = if previous.is_some() {
now.checked_sub(FRAME_INTERVAL).unwrap_or(now)
} else {
now
};
tile_arrivals.push(start + Duration::from_millis(offset));
}
let cell = Self {
inner,
role: role.to_string(),
target: target.to_string(),
visible_target,
flap_sample,
tile_arrivals,
started_at: now,
phase_started_at: previous.map(|cell| cell.phase_started_at).unwrap_or(now),
motion_mode,
frame_requester,
};
if cell.is_animating(Duration::ZERO) {
cell.frame_requester.schedule_frame_in(FRAME_INTERVAL);
}
cell
}
fn is_animating(&self, elapsed: Duration) -> bool {
self.motion_mode == MotionMode::Animated
&& elapsed < ANIMATION_DURATION
&& !self.tile_arrivals.is_empty()
}
fn animate_lines(
&self,
mut lines: Vec<HyperlinkLine>,
width: u16,
elapsed: Duration,
) -> Vec<HyperlinkLine> {
if self.motion_mode == MotionMode::Reduced {
return lines;
}
let board_style = Style::new().bg(Color::Black).fg(Color::Gray);
let mut glyph_index = 0;
let mut word_is_unsettled = false;
let now = self.started_at + elapsed;
let phase_elapsed = now.saturating_duration_since(self.phase_started_at);
let final_line = lines.iter().rposition(|line| {
line.line
.spans
.iter()
.any(|span| !span.content.trim().is_empty())
});
for (line_index, line) in lines.iter_mut().enumerate() {
if line
.line
.spans
.iter()
.all(|span| span.content.trim().is_empty())
{
continue;
}
let original_spans = std::mem::take(&mut line.line.spans);
line.line.style = line.line.style.patch(board_style);
for span in original_spans {
for grapheme in span.content.graphemes(/*is_extended*/ true) {
let mut style = span.style.patch(board_style);
let text = if is_flippable(grapheme) {
let position = glyph_index;
glyph_index += 1;
let arrival = self
.tile_arrivals
.get(position)
.copied()
.unwrap_or(self.started_at);
let tile_elapsed = now.saturating_duration_since(arrival);
let (text, flipping) = flap_glyph(
grapheme,
position,
tile_elapsed,
phase_elapsed,
&self.flap_sample,
);
if flipping {
word_is_unsettled = true;
style = style.fg(Color::DarkGray);
} else if tile_elapsed < TILE_SETTLE_DURATION + TILE_AFTERGLOW_DURATION {
style = style.fg(if self.role == "user" {
Color::Cyan
} else {
Color::Magenta
});
}
text
} else {
if grapheme == "›" {
style = style.fg(span.style.fg.unwrap_or(Color::Cyan));
} else if grapheme == "•" {
style = style.fg(Color::Magenta);
}
if grapheme.chars().all(char::is_whitespace) {
word_is_unsettled = false;
}
if word_is_unsettled && is_punctuation(grapheme) {
" ".to_string()
} else {
grapheme.to_string()
}
};
line.line.spans.push(Span::styled(text, style));
}
}
let remaining = usize::from(width).saturating_sub(line.width());
if Some(line_index) == final_line
&& remaining > 1
&& self.is_animating(elapsed)
&& self.tile_arrivals.first().is_some_and(|arrival| {
now.saturating_duration_since(*arrival) >= TILE_SETTLE_DURATION
})
{
let phase = usize::try_from(phase_elapsed.as_millis() / FRAME_INTERVAL.as_millis())
.unwrap_or(usize::MAX);
let runway = (0..remaining.saturating_sub(/*rhs*/ 1).min(/*other*/ 3))
.map(|index| {
char::from(self.flap_sample[(phase + index) % self.flap_sample.len()])
})
.collect::<String>();
line.line.spans.push(Span::styled(
format!(" {runway}"),
board_style.fg(Color::DarkGray),
));
}
let padding = usize::from(width).saturating_sub(line.width());
if padding > 0 {
line.line
.spans
.push(Span::styled(" ".repeat(padding), board_style));
}
}
if self.is_animating(elapsed) {
self.frame_requester.schedule_frame_in(FRAME_INTERVAL);
}
lines
}
}
impl HistoryCell for SplitFlapTranscriptCell {
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
visible_lines(self.display_hyperlink_lines(width))
}
fn raw_lines(&self) -> Vec<Line<'static>> {
self.inner.raw_lines()
}
fn display_hyperlink_lines(&self, width: u16) -> Vec<HyperlinkLine> {
self.animate_lines(
self.inner.display_hyperlink_lines(width),
width,
self.started_at.elapsed(),
)
}
fn transcript_hyperlink_lines(&self, width: u16) -> Vec<HyperlinkLine> {
self.animate_lines(
self.inner.transcript_hyperlink_lines(width),
width,
self.started_at.elapsed(),
)
}
fn transcript_animation_tick(&self) -> Option<u64> {
let elapsed = self.started_at.elapsed();
self.is_animating(elapsed).then(|| {
u64::try_from(elapsed.as_millis() / FRAME_INTERVAL.as_millis()).unwrap_or(u64::MAX)
})
}
}
fn visible_glyphs(cell: &dyn HistoryCell) -> String {
let mut glyphs = String::new();
for line in cell.display_hyperlink_lines(u16::MAX) {
for span in line.line.spans {
for grapheme in span.content.graphemes(/*is_extended*/ true) {
if is_flippable(grapheme) {
glyphs.push_str(grapheme);
}
}
}
}
glyphs
}
fn is_flippable(grapheme: &str) -> bool {
grapheme.len() == 1 && grapheme.as_bytes()[0].is_ascii_alphanumeric()
}
fn is_punctuation(grapheme: &str) -> bool {
grapheme.len() == 1 && grapheme.as_bytes()[0].is_ascii_punctuation()
}
fn flap_glyph(
grapheme: &str,
index: usize,
elapsed: Duration,
phase_elapsed: Duration,
sample: &[u8],
) -> (String, bool) {
let milliseconds = elapsed.as_millis();
if milliseconds < 40 {
return (" ".to_string(), true);
}
if elapsed >= TILE_SETTLE_DURATION {
return (grapheme.to_string(), false);
}
let frame = usize::try_from(phase_elapsed.as_millis() / FRAME_INTERVAL.as_millis())
.unwrap_or(usize::MAX);
let target = usize::from(grapheme.as_bytes()[0]);
let offset = target
.wrapping_add(index.wrapping_mul(/*rhs*/ 7))
.wrapping_add(frame.wrapping_mul(/*rhs*/ 11));
let tile = char::from(sample[offset % sample.len()]);
(tile.to_string(), true)
}