hex/src/live_text.rs
Elliott Hillary 3ae9d10ccd
Live text: show what a dictation has said so far beside the HUD (live-text experiment) (#324)
* Live text: show what a dictation has said so far beside the HUD (live-text experiment)

With the `live-text` experiment on, a hotkey dictation on a streaming model
(Parakeet Unified English on a GPU) shows its text as you speak: one
click-through line just past the recording HUD, growing with the words and
then keeping the newest in view behind "…". It is provisional and only ever
shown; the paste is unchanged.

- Engine: a dictation stream gets its own partial slot and publishes the
  model's stable and tentative text after each feed; a failed or
  interrupted stream clears it. `/streams` shares the same slot type.
- Capture: `DictationCapture::report_live_text`, called by the macOS, Linux,
  and Windows capture loops, shows the recording's text through its
  `live_text::Publisher`. The recording owns it, so every ending (release,
  Escape, a short tap, a lost microphone) hides the line, and an older
  recording never clears a newer one's text.
- UI: one Rune overlay shared by every OS, prewarmed so macOS never reveals
  the previous dictation's words, placed where `rune_hud` last showed the
  HUD: on macOS just past the plate it placed (`hud_placement::beside_plate`)
  on the pointer's screen, on Linux and Windows past its centred plate on
  the primary display, and with the HUD hidden in its place at the top.
- AGENTS.md: "Nothing pastes or reaches the focused app before the final
  text" now says live text may show in Hex's own HUD.

Measured on Apple Silicon (Metal): text arrives in ~1 s steps, about
1–2 s behind speech, with no change to the final text or release latency.
Checked by hand on an M3 Pro (see the feature map); Linux and Windows are
built only by CI.

The feature map also records a pre-existing defect found while testing:
after a pause longer than the model's 5.6 s left context, the stream can
drop the first word of the next sentence. NeMo's reference buffered
streaming drops it on the same audio, so it is the model's behaviour, not
transcribe.cpp's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* live text: the global spot is macOS-only

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Dax Raad <d@ironbay.co>
2026-10-28 21:34:54 -04:00

154 lines
4.3 KiB
Rust

//! What the dictation being recorded has said so far, for the HUDs (the
//! `live-text` experiment).
//!
//! The capture loop publishes through its recording's [`Publisher`], which
//! the recording owns: dropping the recording (release, Escape, a short tap,
//! a lost microphone) clears the text, so no ending can leave it on screen.
//! A HUD registers a [`watch`]er and is told each change from the capture
//! loop's thread; it must hand the text to its own thread and never wait.
//! The text is provisional: it is only ever shown, never pasted or kept.
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use crate::transcription_engine::Partial;
type Watcher = Arc<dyn Fn(Option<&Partial>) + Send + Sync>;
struct Board {
/// The publisher whose text is current, or 0 when none is.
owner: u64,
watchers: Vec<Watcher>,
}
static BOARD: Mutex<Board> = Mutex::new(Board {
owner: 0,
watchers: Vec::new(),
});
static NEXT_PUBLISHER: AtomicU64 = AtomicU64::new(1);
fn board() -> MutexGuard<'static, Board> {
BOARD.lock().unwrap_or_else(|error| error.into_inner())
}
/// Calls `watcher` with each new text, and with `None` once the recording
/// that published it ends.
pub(crate) fn watch(watcher: impl Fn(Option<&Partial>) + Send + Sync + 'static) {
board().watchers.push(Arc::new(watcher));
}
/// One recording's hold on the live text.
pub(crate) struct Publisher {
id: u64,
}
impl Publisher {
pub(crate) fn new() -> Self {
Self {
id: NEXT_PUBLISHER.fetch_add(1, Ordering::Relaxed),
}
}
/// Shows `partial`, replacing any other recording's text.
pub(crate) fn show(&self, partial: &Partial) {
let watchers = {
let mut board = board();
board.owner = self.id;
board.watchers.clone()
};
for watcher in watchers {
watcher(Some(partial));
}
}
}
impl Drop for Publisher {
/// Clears the text if it is still this recording's.
fn drop(&mut self) {
let watchers = {
let mut board = board();
if board.owner != self.id {
return;
}
board.owner = 0;
board.watchers.clone()
};
for watcher in watchers {
watcher(None);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The board is process-wide; these tests take turns with it.
static TURN: Mutex<()> = Mutex::new(());
fn turn() -> MutexGuard<'static, ()> {
TURN.lock().unwrap_or_else(|error| error.into_inner())
}
fn said(stable: &str) -> Partial {
Partial {
stable: stable.into(),
tentative: String::new(),
}
}
/// Records every call a watcher gets, as the stable text or `None`.
fn watched() -> Arc<Mutex<Vec<Option<String>>>> {
let seen = Arc::new(Mutex::new(Vec::new()));
let log = seen.clone();
watch(move |partial| {
log.lock()
.unwrap()
.push(partial.map(|partial| partial.stable.clone()));
});
seen
}
#[test]
fn a_recording_shows_its_text_and_clears_it_when_it_ends() {
let _turn = turn();
let seen = watched();
let recording = Publisher::new();
recording.show(&said("so the plan"));
recording.show(&said("so the plan for today"));
drop(recording);
assert_eq!(
*seen.lock().unwrap(),
[
Some("so the plan".into()),
Some("so the plan for today".into()),
None
]
);
}
#[test]
fn an_older_recording_ending_never_clears_a_newer_ones_text() {
let _turn = turn();
let seen = watched();
let older = Publisher::new();
older.show(&said("old"));
let newer = Publisher::new();
newer.show(&said("new"));
drop(older);
assert_eq!(
*seen.lock().unwrap(),
[Some("old".into()), Some("new".into())]
);
drop(newer);
assert_eq!(seen.lock().unwrap().last(), Some(&None));
}
#[test]
fn a_recording_that_never_showed_text_clears_nothing() {
let _turn = turn();
let seen = watched();
drop(Publisher::new());
assert!(seen.lock().unwrap().is_empty());
}
}