Add live WebRTC voice conversations to the TUI (#43581)

## What changed

- Add feature-gated `/voice`, `/voice mute`, and `/voice stop` commands with local WebRTC audio and app-server signaling.
- Show live transcripts, conversation status, and microphone and speaker levels. Preserve captions and undelivered answers across thread switches.
- Speak final answers from voice handoffs while keeping delegated reasoning and commentary hidden and typed answers unspoken.
- Retry eligible startup failures once and clean up voice sessions on thread switches and disconnects.
- Stop voice and block late handoffs after a misalignment policy violation. Remove realtime event payloads and spoken text from receipt and debug logs.

## Testing

Add coverage for voice command mapping, rejected starts, disconnect cleanup, delegated final-answer speech, late handoff rejection, and transcript and footer rendering.

GitOrigin-RevId: 11def1f86b0e023b21d0d92072d6636ccad89815
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-07 20:25:30 +00:00
1 parent 4110342321
commit b01c3986fd
70 files changed
+4421 -69

No files matched your search

+117
View File
@@ -13,8 +13,32 @@ use codex_app_server_protocol::TurnInterruptParams;
use codex_app_server_protocol::TurnInterruptResponse;
use codex_app_server_protocol::WarningNotification;
// Leave time for side-thread cleanup and unsubscribe inside the two-second exit budget.
const REALTIME_STOP_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 1);
impl App {
pub(super) async fn stop_realtime_conversation(&mut self, app_server: &mut AppServerSession) {
let Some(thread_id) = self.chat_widget.reset_realtime_conversation() else {
return;
};
match tokio::time::timeout(
REALTIME_STOP_TIMEOUT,
app_server.thread_realtime_stop(thread_id),
)
.await
{
Ok(Ok(())) => {}
Ok(Err(error)) => {
tracing::warn!(%thread_id, %error, "failed to stop voice conversation");
}
Err(_) => {
tracing::warn!(%thread_id, "timed out stopping voice conversation");
}
}
}
pub(super) async fn shutdown_current_thread(&mut self, app_server: &mut AppServerSession) {
self.stop_realtime_conversation(app_server).await;
self.shutdown_side_threads(app_server).await;
if let Some(thread_id) = self.chat_widget.thread_id() {
if let Err(err) = app_server.thread_unsubscribe(thread_id).await {
@@ -446,6 +470,15 @@ impl App {
op: AppCommand,
) -> Result<()> {
let Some(thread_id) = self.active_thread_id else {
if let AppCommand::RealtimeConversationSpeech { delivery_id, .. } = &op {
self.chat_widget
.restore_undelivered_realtime_speech(*delivery_id);
}
if let AppCommand::RealtimeConversationStart { thread_id, .. } = &op
&& self.chat_widget.thread_id() == Some(*thread_id)
{
self.chat_widget.reset_realtime_conversation();
}
self.chat_widget
.add_error_message("No active thread is available.".to_string());
return Ok(());
@@ -461,12 +494,25 @@ impl App {
op: AppCommand,
) -> Result<()> {
if self.thread_unavailable(thread_id) {
if let AppCommand::RealtimeConversationSpeech { delivery_id, .. } = &op {
self.chat_widget
.restore_undelivered_realtime_speech(*delivery_id);
}
if let AppCommand::RealtimeConversationStart { thread_id, .. } = &op
&& self.chat_widget.thread_id() == Some(*thread_id)
{
self.chat_widget.reset_realtime_conversation();
}
self.chat_widget.add_error_message(
"This conversation is read-only or unavailable; no operation was sent.".into(),
);
return Ok(());
}
if self.chat_widget.rejects_misalignment_policy_op(&op) {
if let AppCommand::RealtimeConversationSpeech { delivery_id, .. } = &op {
self.chat_widget
.restore_undelivered_realtime_speech(*delivery_id);
}
return Ok(());
}
@@ -867,6 +913,60 @@ impl App {
.await?;
Ok(true)
}
AppCommand::RealtimeConversationStart {
thread_id: realtime_thread_id,
offer_sdp,
} => {
if *realtime_thread_id != thread_id {
return Ok(true);
}
let model = self
.chat_widget
.config_ref()
.experimental_realtime_ws_model
.clone();
app_server
.thread_realtime_start(
*realtime_thread_id,
String::from(offer_sdp.clone()),
model,
)
.await?;
Ok(true)
}
AppCommand::RealtimeConversationStop {
thread_id: realtime_thread_id,
} => {
app_server.thread_realtime_stop(*realtime_thread_id).await?;
Ok(true)
}
AppCommand::RealtimeConversationSpeech {
thread_id: realtime_thread_id,
attempt_id,
input_generation,
delivery_id,
text,
} => {
if !self.chat_widget.has_pending_realtime_speech(*delivery_id) {
return Ok(true);
}
if *realtime_thread_id != thread_id
|| !self.chat_widget.is_current_realtime_attempt(
*realtime_thread_id,
*attempt_id,
*input_generation,
)
{
self.chat_widget
.restore_undelivered_realtime_speech(*delivery_id);
return Ok(true);
}
app_server
.thread_realtime_append_speech(*realtime_thread_id, text.as_str().to_owned())
.await?;
self.chat_widget.accept_realtime_speech(*delivery_id);
Ok(true)
}
AppCommand::RunUserShellCommand { command } => {
app_server
.thread_shell_command(thread_id, command.to_string())
@@ -1136,6 +1236,7 @@ impl App {
guard.push_notification_ref(&notification);
Some(notification)
} else {
self.retain_inactive_realtime_transcript(thread_id, &notification);
guard.push_notification(notification);
None
};
@@ -1423,6 +1524,8 @@ impl App {
}
}
let should_buffer_initial_replay = !turns.is_empty();
let replayed_final_items = realtime_delivery::completed_agent_items_from_turns(&turns);
let replayed_voice_texts = realtime_delivery::replayed_voice_texts_from_turns(&turns);
if should_buffer_initial_replay {
self.app_event_tx
.send(AppEvent::BeginInitialHistoryReplayBuffer);
@@ -1438,6 +1541,10 @@ impl App {
self.app_event_tx
.send(AppEvent::EndInitialHistoryReplayBuffer);
}
self.restore_realtime_replay_state_after_replay(
&replayed_final_items,
replayed_voice_texts,
);
if matches!(presentation, ThreadAttachPresentation::PromptEdit) {
self.chat_widget.emit_prompt_edit_thread_event();
}
@@ -1651,6 +1758,8 @@ impl App {
mut snapshot: ThreadEventSnapshot,
resume_restored_queue: bool,
) {
let replayed_final_items = realtime_delivery::completed_agent_items(&snapshot);
let replayed_voice_texts = realtime_delivery::replayed_voice_texts(&snapshot);
replay_filter::omit_completed_agent_deltas(&mut snapshot.events);
let request_changes = snapshot
.events
@@ -1694,6 +1803,10 @@ impl App {
self.chat_widget.handle_thread_session(session);
}
}
for turn_id in &snapshot.delegated_turns {
self.chat_widget
.remember_realtime_delegated_reasoning_turn(turn_id);
}
let recovered_input = snapshot
.input_state
.as_ref()
@@ -1728,6 +1841,10 @@ impl App {
if recovered_input.is_some() {
self.chat_widget.restore_reconnected_input(recovered_input);
}
self.restore_realtime_replay_state_after_replay(
&replayed_final_items,
replayed_voice_texts,
);
self.chat_widget
.set_queue_autosend_suppressed(/*suppressed*/ false);
self.chat_widget