@@ -52,6 +52,11 @@ pub struct EventProcessorWithJsonOutput {
5252 last_total_token_usage : Option < codex_core:: protocol:: TokenUsage > ,
5353 running_mcp_tool_calls : HashMap < String , RunningMcpToolCall > ,
5454 last_critical_error : Option < ThreadErrorEvent > ,
55+ // Aggregated streaming text for delta events. We synthesize `item.updated` events
56+ // with the full accumulated text so JS clients can diff and print the delta.
57+ running_agent_message : Option < RunningTextItem > ,
58+ running_reasoning_summary : Option < RunningTextItem > ,
59+ running_reasoning_raw : Option < RunningTextItem > ,
5560}
5661
5762#[ derive( Debug , Clone ) ]
@@ -75,6 +80,12 @@ struct RunningMcpToolCall {
7580 arguments : JsonValue ,
7681}
7782
83+ #[ derive( Debug , Clone ) ]
84+ struct RunningTextItem {
85+ item_id : String ,
86+ text : String ,
87+ }
88+
7889impl EventProcessorWithJsonOutput {
7990 pub fn new ( last_message_path : Option < PathBuf > ) -> Self {
8091 Self {
@@ -86,6 +97,9 @@ impl EventProcessorWithJsonOutput {
8697 last_total_token_usage : None ,
8798 running_mcp_tool_calls : HashMap :: new ( ) ,
8899 last_critical_error : None ,
100+ running_agent_message : None ,
101+ running_reasoning_summary : None ,
102+ running_reasoning_raw : None ,
89103 }
90104 }
91105
@@ -94,6 +108,11 @@ impl EventProcessorWithJsonOutput {
94108 protocol:: EventMsg :: SessionConfigured ( ev) => self . handle_session_configured ( ev) ,
95109 protocol:: EventMsg :: AgentMessage ( ev) => self . handle_agent_message ( ev) ,
96110 protocol:: EventMsg :: AgentReasoning ( ev) => self . handle_reasoning_event ( ev) ,
111+ // Forward streaming deltas so JS consumers (Thread.runStreamed / CodexProvider)
112+ // can show incremental output as it is generated.
113+ protocol:: EventMsg :: AgentMessageContentDelta ( ev) => self . handle_agent_message_delta ( ev) ,
114+ protocol:: EventMsg :: ReasoningContentDelta ( ev) => self . handle_reasoning_delta ( ev) ,
115+ protocol:: EventMsg :: ReasoningRawContentDelta ( ev) => self . handle_reasoning_raw_delta ( ev) ,
97116 protocol:: EventMsg :: ExecCommandBegin ( ev) => self . handle_exec_command_begin ( ev) ,
98117 protocol:: EventMsg :: ExecCommandEnd ( ev) => self . handle_exec_command_end ( ev) ,
99118 protocol:: EventMsg :: TerminalInteraction ( ev) => self . handle_terminal_interaction ( ev) ,
@@ -208,6 +227,78 @@ impl EventProcessorWithJsonOutput {
208227
209228 vec ! [ ThreadEvent :: ItemCompleted ( ItemCompletedEvent { item } ) ]
210229 }
230+
231+ fn handle_agent_message_delta (
232+ & mut self ,
233+ ev : & protocol:: AgentMessageContentDeltaEvent ,
234+ ) -> Vec < ThreadEvent > {
235+ if self . running_agent_message . is_none ( ) {
236+ self . running_agent_message = Some ( RunningTextItem {
237+ item_id : self . get_next_item_id ( ) ,
238+ text : String :: new ( ) ,
239+ } ) ;
240+ }
241+ let entry = self
242+ . running_agent_message
243+ . as_mut ( )
244+ . expect ( "running_agent_message initialized above" ) ;
245+ entry. text . push_str ( & ev. delta ) ;
246+ let item = ThreadItem {
247+ id : entry. item_id . clone ( ) ,
248+ details : ThreadItemDetails :: AgentMessage ( AgentMessageItem {
249+ text : entry. text . clone ( ) ,
250+ } ) ,
251+ } ;
252+ vec ! [ ThreadEvent :: ItemUpdated ( ItemUpdatedEvent { item } ) ]
253+ }
254+
255+ fn handle_reasoning_delta (
256+ & mut self ,
257+ ev : & protocol:: ReasoningContentDeltaEvent ,
258+ ) -> Vec < ThreadEvent > {
259+ if self . running_reasoning_summary . is_none ( ) {
260+ self . running_reasoning_summary = Some ( RunningTextItem {
261+ item_id : self . get_next_item_id ( ) ,
262+ text : String :: new ( ) ,
263+ } ) ;
264+ }
265+ let entry = self
266+ . running_reasoning_summary
267+ . as_mut ( )
268+ . expect ( "running_reasoning_summary initialized above" ) ;
269+ entry. text . push_str ( & ev. delta ) ;
270+ let item = ThreadItem {
271+ id : entry. item_id . clone ( ) ,
272+ details : ThreadItemDetails :: Reasoning ( ReasoningItem {
273+ text : entry. text . clone ( ) ,
274+ } ) ,
275+ } ;
276+ vec ! [ ThreadEvent :: ItemUpdated ( ItemUpdatedEvent { item } ) ]
277+ }
278+
279+ fn handle_reasoning_raw_delta (
280+ & mut self ,
281+ ev : & protocol:: ReasoningRawContentDeltaEvent ,
282+ ) -> Vec < ThreadEvent > {
283+ if self . running_reasoning_raw . is_none ( ) {
284+ self . running_reasoning_raw = Some ( RunningTextItem {
285+ item_id : self . get_next_item_id ( ) ,
286+ text : String :: new ( ) ,
287+ } ) ;
288+ }
289+ let entry = self
290+ . running_reasoning_raw
291+ . as_mut ( )
292+ . expect ( "running_reasoning_raw initialized above" ) ;
293+ entry. text . push_str ( & ev. delta ) ;
294+ let item = ThreadItem {
295+ id : entry. item_id . clone ( ) ,
296+ details : ThreadItemDetails :: Reasoning ( ReasoningItem {
297+ text : entry. text . clone ( ) ,
298+ } ) ,
299+ } ;
300+ vec ! [ ThreadEvent :: ItemUpdated ( ItemUpdatedEvent { item } ) ]
301+ }
211302 fn handle_exec_command_begin (
212303 & mut self ,
213304 ev : & protocol:: ExecCommandBeginEvent ,
@@ -461,6 +552,10 @@ impl EventProcessorWithJsonOutput {
461552
462553 fn handle_task_started ( & mut self , _: & protocol:: TurnStartedEvent ) -> Vec < ThreadEvent > {
463554 self . last_critical_error = None ;
555+ // New turn: reset streaming text accumulation.
556+ self . running_agent_message = None ;
557+ self . running_reasoning_summary = None ;
558+ self . running_reasoning_raw = None ;
464559 vec ! [ ThreadEvent :: TurnStarted ( TurnStartedEvent { } ) ]
465560 }
466561
0 commit comments