mirror of
https://github.com/openai/codex.git
synced 2026-06-01 19:02:59 +00:00
Send events to realtime api (#12423)
- Send assistant messages, ExecCommandBegin, and PatchApplyBegin/PatchApplyEnd
This commit is contained in:
@@ -184,6 +184,7 @@ use crate::protocol::ModelRerouteEvent;
|
||||
use crate::protocol::ModelRerouteReason;
|
||||
use crate::protocol::NetworkApprovalContext;
|
||||
use crate::protocol::Op;
|
||||
use crate::protocol::PatchApplyStatus;
|
||||
use crate::protocol::PlanDeltaEvent;
|
||||
use crate::protocol::RateLimitSnapshot;
|
||||
use crate::protocol::ReasoningContentDeltaEvent;
|
||||
@@ -2206,6 +2207,8 @@ impl Session {
|
||||
msg,
|
||||
};
|
||||
self.send_event_raw(event).await;
|
||||
self.maybe_mirror_event_text_to_realtime(&legacy_source)
|
||||
.await;
|
||||
|
||||
let show_raw_agent_reasoning = self.show_raw_agent_reasoning();
|
||||
for legacy in legacy_source.as_legacy_events(show_raw_agent_reasoning) {
|
||||
@@ -2217,6 +2220,18 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
async fn maybe_mirror_event_text_to_realtime(&self, msg: &EventMsg) {
|
||||
let Some(text) = realtime_text_for_event(msg) else {
|
||||
return;
|
||||
};
|
||||
if self.conversation.running_state().await.is_none() {
|
||||
return;
|
||||
}
|
||||
if let Err(err) = self.conversation.text_in(text).await {
|
||||
debug!("failed to mirror event text to realtime conversation: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn send_event_raw(&self, event: Event) {
|
||||
// Record the last known agent status.
|
||||
if let Some(status) = agent_status_from_event(&event.msg) {
|
||||
@@ -5451,6 +5466,56 @@ fn agent_message_text(item: &codex_protocol::items::AgentMessageItem) -> String
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn realtime_text_for_event(msg: &EventMsg) -> Option<String> {
|
||||
match msg {
|
||||
EventMsg::AgentMessage(event) => Some(event.message.clone()),
|
||||
EventMsg::ItemCompleted(event) => match &event.item {
|
||||
TurnItem::AgentMessage(item) => Some(agent_message_text(item)),
|
||||
_ => None,
|
||||
},
|
||||
EventMsg::ExecCommandBegin(event) => {
|
||||
let command = event.command.join(" ");
|
||||
Some(format!(
|
||||
"Exec command started: {command}\nWorking directory: {}",
|
||||
event.cwd.display()
|
||||
))
|
||||
}
|
||||
EventMsg::PatchApplyBegin(event) => {
|
||||
let mut files: Vec<String> = event
|
||||
.changes
|
||||
.keys()
|
||||
.map(|path| path.display().to_string())
|
||||
.collect();
|
||||
files.sort();
|
||||
let file_list = if files.is_empty() {
|
||||
"none".to_string()
|
||||
} else {
|
||||
files.join(", ")
|
||||
};
|
||||
Some(format!(
|
||||
"apply_patch started ({count} file change(s))\nFiles: {file_list}",
|
||||
count = files.len()
|
||||
))
|
||||
}
|
||||
EventMsg::PatchApplyEnd(event) => {
|
||||
let status = match event.status {
|
||||
PatchApplyStatus::Completed => "completed",
|
||||
PatchApplyStatus::Failed => "failed",
|
||||
PatchApplyStatus::Declined => "declined",
|
||||
};
|
||||
let mut text = format!("apply_patch {status}");
|
||||
if !event.stdout.is_empty() {
|
||||
text.push_str(&format!("\nstdout:\n{}", event.stdout));
|
||||
}
|
||||
if !event.stderr.is_empty() {
|
||||
text.push_str(&format!("\nstderr:\n{}", event.stderr));
|
||||
}
|
||||
Some(text)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Split the stream into normal assistant text vs. proposed plan content.
|
||||
/// Normal text becomes AgentMessage deltas; plan content becomes PlanDelta +
|
||||
/// TurnItem::Plan.
|
||||
|
||||
Reference in New Issue
Block a user