Support attaching to existing realtime calls (#39876)

## Why

Clients that create and negotiate realtime calls themselves need Codex to join
those calls without creating another call or overwriting the client-owned
session configuration.

## What changed

- Add the `existingCall` transport to `thread/realtime/start`, accepting a
  client-provided `callId` and optional `realtimeSessionId`.
- Attach Codex over the call's sideband WebSocket without SDP negotiation or a
  session update, and reconnect that sideband while preserving transcript
  state.
- Default existing calls to realtime v1, support v1 and v3, and reject v2 or
  options that would reconfigure the client-owned session.
- Encode call IDs as a single URL path segment for v3 sideband connections.

## Testing

Add protocol, app-server, core, reconnection, authentication-header, and URL
encoding coverage for existing-call attachments.

GitOrigin-RevId: 2046b70566f98efa55381bb1e461ea3cc68256c5
This commit is contained in:
Zahan Malkani authored and copyberry committed 2026-08-21 06:41:28 +00:00
1 parent 2aaefa32b0
commit 536f86e5cc
18 files changed
+765 -34

No files matched your search

@@ -0,0 +1,90 @@
use super::RealtimeHandoffState;
use super::RealtimeInputChannels;
use super::RealtimeSessionKind;
use super::RealtimeSidebandSessionInitialization;
use super::RealtimeTranscriptTailFlush;
use super::RealtimeWebrtcSidebandInputTask;
use super::spawn_webrtc_sideband_input_task;
use crate::client::ModelClient;
use async_channel::Sender;
use codex_api::RealtimeEvent;
use codex_api::RealtimeEventParser;
use codex_api::RealtimeSessionConfig;
use codex_api::RealtimeTranscriptState;
use codex_api::RealtimeWebsocketClient;
use codex_api::map_api_error;
use codex_login::default_client::default_headers;
use codex_protocol::error::Result as CodexResult;
use http::HeaderMap;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
pub(super) struct ExistingCallAttachment {
pub(super) client: RealtimeWebsocketClient,
pub(super) model_client: ModelClient,
pub(super) session_config: RealtimeSessionConfig,
pub(super) call_id: String,
pub(super) extra_headers: HeaderMap,
pub(super) input_channels: RealtimeInputChannels,
pub(super) events_tx: Sender<RealtimeEvent>,
pub(super) handoff_state: RealtimeHandoffState,
pub(super) session_kind: RealtimeSessionKind,
pub(super) event_parser: RealtimeEventParser,
pub(super) realtime_active: Arc<AtomicBool>,
pub(super) transcript_tail_flush: RealtimeTranscriptTailFlush,
pub(super) stop_token: CancellationToken,
}
pub(super) async fn attach(attachment: ExistingCallAttachment) -> CodexResult<JoinHandle<()>> {
let ExistingCallAttachment {
client,
model_client,
session_config,
call_id,
extra_headers,
input_channels,
events_tx,
handoff_state,
session_kind,
event_parser,
realtime_active,
transcript_tail_flush,
stop_token,
} = attachment;
let sideband_headers = model_client
.realtime_sideband_headers(extra_headers)
.await?;
let transcript_state = RealtimeTranscriptState::default();
let connection = client
.connect_existing_call_sideband(
session_config.clone(),
&call_id,
sideband_headers.clone(),
default_headers(),
transcript_state,
)
.await
.map_err(map_api_error)?;
Ok(spawn_webrtc_sideband_input_task(
RealtimeWebrtcSidebandInputTask {
client,
session_config,
call_id,
sideband_headers,
session_initialization: RealtimeSidebandSessionInitialization::ExistingCall {
initial_connection: Some(connection),
},
input_channels,
events_tx,
handoff_state,
session_kind,
event_parser,
realtime_active,
transcript_tail_flush,
stop_token,
},
))
}
@@ -1,5 +1,6 @@
use super::RealtimeInputTask;
use super::RealtimeInputTaskExit;
use super::RealtimeSidebandSessionInitialization;
use super::RealtimeWebrtcSidebandInputTask;
use super::flush_realtime_transcript_tail;
use super::report_realtime_transport_loss;
@@ -31,6 +32,7 @@ pub(super) fn spawn_webrtc_sideband_input_task(
session_config,
call_id,
sideband_headers,
mut session_initialization,
input_channels,
events_tx,
handoff_state,
@@ -45,18 +47,44 @@ pub(super) fn spawn_webrtc_sideband_input_task(
let mut reconnecting = false;
let mut rapid_disconnects = 0_u32;
let mut pending_outbound = None;
let transcript_state = RealtimeTranscriptState::default();
let transcript_state = match &session_initialization {
RealtimeSidebandSessionInitialization::ExistingCall {
initial_connection: Some(connection),
} => connection.events().transcript_state(),
RealtimeSidebandSessionInitialization::CoreCreated
| RealtimeSidebandSessionInitialization::ExistingCall {
initial_connection: None,
} => RealtimeTranscriptState::default(),
};
while realtime_active.load(Ordering::Relaxed) {
let connection = match tokio::select! {
biased;
_ = stop_token.cancelled() => break,
connection = client.connect_webrtc_sideband(
session_config.clone(),
&call_id,
sideband_headers.clone(),
default_headers(),
transcript_state.clone(),
) => connection,
connection = async {
match &mut session_initialization {
RealtimeSidebandSessionInitialization::CoreCreated => {
client.connect_webrtc_sideband(
session_config.clone(),
&call_id,
sideband_headers.clone(),
default_headers(),
transcript_state.clone(),
).await
}
RealtimeSidebandSessionInitialization::ExistingCall { initial_connection } => {
match initial_connection.take() {
Some(connection) => Ok(connection),
None => client.connect_existing_call_sideband(
session_config.clone(),
&call_id,
sideband_headers.clone(),
default_headers(),
transcript_state.clone(),
).await,
}
}
}
} => connection,
} {
Ok(connection) => connection,
Err(err) => {