mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
Add app-server v2 thread realtime API (#12715)
Add experimental `thread/realtime/*` v2 requests and notifications, then route app-server realtime events through that thread-scoped surface with integration coverage. --------- Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -138,6 +138,14 @@ use codex_app_server_protocol::ThreadLoadedListParams;
|
||||
use codex_app_server_protocol::ThreadLoadedListResponse;
|
||||
use codex_app_server_protocol::ThreadReadParams;
|
||||
use codex_app_server_protocol::ThreadReadResponse;
|
||||
use codex_app_server_protocol::ThreadRealtimeAppendAudioParams;
|
||||
use codex_app_server_protocol::ThreadRealtimeAppendAudioResponse;
|
||||
use codex_app_server_protocol::ThreadRealtimeAppendTextParams;
|
||||
use codex_app_server_protocol::ThreadRealtimeAppendTextResponse;
|
||||
use codex_app_server_protocol::ThreadRealtimeStartParams;
|
||||
use codex_app_server_protocol::ThreadRealtimeStartResponse;
|
||||
use codex_app_server_protocol::ThreadRealtimeStopParams;
|
||||
use codex_app_server_protocol::ThreadRealtimeStopResponse;
|
||||
use codex_app_server_protocol::ThreadResumeParams;
|
||||
use codex_app_server_protocol::ThreadResumeResponse;
|
||||
use codex_app_server_protocol::ThreadRollbackParams;
|
||||
@@ -235,6 +243,9 @@ use codex_protocol::dynamic_tools::DynamicToolSpec as CoreDynamicToolSpec;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::AgentStatus;
|
||||
use codex_protocol::protocol::ConversationAudioParams;
|
||||
use codex_protocol::protocol::ConversationStartParams;
|
||||
use codex_protocol::protocol::ConversationTextParams;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::GitInfo as CoreGitInfo;
|
||||
use codex_protocol::protocol::InitialHistory;
|
||||
@@ -625,6 +636,22 @@ impl CodexMessageProcessor {
|
||||
self.turn_interrupt(to_connection_request_id(request_id), params)
|
||||
.await;
|
||||
}
|
||||
ClientRequest::ThreadRealtimeStart { request_id, params } => {
|
||||
self.thread_realtime_start(to_connection_request_id(request_id), params)
|
||||
.await;
|
||||
}
|
||||
ClientRequest::ThreadRealtimeAppendAudio { request_id, params } => {
|
||||
self.thread_realtime_append_audio(to_connection_request_id(request_id), params)
|
||||
.await;
|
||||
}
|
||||
ClientRequest::ThreadRealtimeAppendText { request_id, params } => {
|
||||
self.thread_realtime_append_text(to_connection_request_id(request_id), params)
|
||||
.await;
|
||||
}
|
||||
ClientRequest::ThreadRealtimeStop { request_id, params } => {
|
||||
self.thread_realtime_stop(to_connection_request_id(request_id), params)
|
||||
.await;
|
||||
}
|
||||
ClientRequest::ReviewStart { request_id, params } => {
|
||||
self.review_start(to_connection_request_id(request_id), params)
|
||||
.await;
|
||||
@@ -5518,6 +5545,177 @@ impl CodexMessageProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
async fn prepare_realtime_conversation_thread(
|
||||
&mut self,
|
||||
request_id: ConnectionRequestId,
|
||||
thread_id: &str,
|
||||
) -> Option<(ThreadId, Arc<CodexThread>)> {
|
||||
let (thread_id, thread) = match self.load_thread(thread_id).await {
|
||||
Ok(v) => v,
|
||||
Err(error) => {
|
||||
self.outgoing.send_error(request_id, error).await;
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(error) = self
|
||||
.ensure_conversation_listener(
|
||||
thread_id,
|
||||
request_id.connection_id,
|
||||
false,
|
||||
ApiVersion::V2,
|
||||
)
|
||||
.await
|
||||
{
|
||||
self.outgoing.send_error(request_id, error).await;
|
||||
return None;
|
||||
}
|
||||
|
||||
if !thread.enabled(Feature::RealtimeConversation) {
|
||||
self.send_invalid_request_error(
|
||||
request_id,
|
||||
format!("thread {thread_id} does not support realtime conversation"),
|
||||
)
|
||||
.await;
|
||||
return None;
|
||||
}
|
||||
|
||||
Some((thread_id, thread))
|
||||
}
|
||||
|
||||
async fn thread_realtime_start(
|
||||
&mut self,
|
||||
request_id: ConnectionRequestId,
|
||||
params: ThreadRealtimeStartParams,
|
||||
) {
|
||||
let Some((_, thread)) = self
|
||||
.prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id)
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let submit = thread
|
||||
.submit(Op::RealtimeConversationStart(ConversationStartParams {
|
||||
prompt: params.prompt,
|
||||
session_id: params.session_id,
|
||||
}))
|
||||
.await;
|
||||
|
||||
match submit {
|
||||
Ok(_) => {
|
||||
self.outgoing
|
||||
.send_response(request_id, ThreadRealtimeStartResponse::default())
|
||||
.await;
|
||||
}
|
||||
Err(err) => {
|
||||
self.send_internal_error(
|
||||
request_id,
|
||||
format!("failed to start realtime conversation: {err}"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn thread_realtime_append_audio(
|
||||
&mut self,
|
||||
request_id: ConnectionRequestId,
|
||||
params: ThreadRealtimeAppendAudioParams,
|
||||
) {
|
||||
let Some((_, thread)) = self
|
||||
.prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id)
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let submit = thread
|
||||
.submit(Op::RealtimeConversationAudio(ConversationAudioParams {
|
||||
frame: params.audio.into(),
|
||||
}))
|
||||
.await;
|
||||
|
||||
match submit {
|
||||
Ok(_) => {
|
||||
self.outgoing
|
||||
.send_response(request_id, ThreadRealtimeAppendAudioResponse::default())
|
||||
.await;
|
||||
}
|
||||
Err(err) => {
|
||||
self.send_internal_error(
|
||||
request_id,
|
||||
format!("failed to append realtime conversation audio: {err}"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn thread_realtime_append_text(
|
||||
&mut self,
|
||||
request_id: ConnectionRequestId,
|
||||
params: ThreadRealtimeAppendTextParams,
|
||||
) {
|
||||
let Some((_, thread)) = self
|
||||
.prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id)
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let submit = thread
|
||||
.submit(Op::RealtimeConversationText(ConversationTextParams {
|
||||
text: params.text,
|
||||
}))
|
||||
.await;
|
||||
|
||||
match submit {
|
||||
Ok(_) => {
|
||||
self.outgoing
|
||||
.send_response(request_id, ThreadRealtimeAppendTextResponse::default())
|
||||
.await;
|
||||
}
|
||||
Err(err) => {
|
||||
self.send_internal_error(
|
||||
request_id,
|
||||
format!("failed to append realtime conversation text: {err}"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn thread_realtime_stop(
|
||||
&mut self,
|
||||
request_id: ConnectionRequestId,
|
||||
params: ThreadRealtimeStopParams,
|
||||
) {
|
||||
let Some((_, thread)) = self
|
||||
.prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id)
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let submit = thread.submit(Op::RealtimeConversationClose).await;
|
||||
|
||||
match submit {
|
||||
Ok(_) => {
|
||||
self.outgoing
|
||||
.send_response(request_id, ThreadRealtimeStopResponse::default())
|
||||
.await;
|
||||
}
|
||||
Err(err) => {
|
||||
self.send_internal_error(
|
||||
request_id,
|
||||
format!("failed to stop realtime conversation: {err}"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn build_review_turn(turn_id: String, display_text: &str) -> Turn {
|
||||
let items = if display_text.is_empty() {
|
||||
Vec::new()
|
||||
|
||||
Reference in New Issue
Block a user