@@ -67,6 +67,7 @@ pub struct EventProcessorWithJsonOutput {
6767 last_total_token_usage : Option < codex_core:: protocol:: TokenUsage > ,
6868 running_mcp_tool_calls : HashMap < String , RunningMcpToolCall > ,
6969 last_critical_error : Option < ThreadErrorEvent > ,
70+ session_timestamp : Option < String > ,
7071}
7172
7273#[ derive( Debug , Clone ) ]
@@ -101,6 +102,7 @@ impl EventProcessorWithJsonOutput {
101102 last_total_token_usage : None ,
102103 running_mcp_tool_calls : HashMap :: new ( ) ,
103104 last_critical_error : None ,
105+ session_timestamp : None ,
104106 }
105107 }
106108
@@ -167,9 +169,11 @@ impl EventProcessorWithJsonOutput {
167169 )
168170 }
169171
170- fn handle_session_configured ( & self , payload : & SessionConfiguredEvent ) -> Vec < ThreadEvent > {
172+ fn handle_session_configured ( & mut self , payload : & SessionConfiguredEvent ) -> Vec < ThreadEvent > {
173+ self . session_timestamp . clone_from ( & payload. timestamp ) ;
171174 vec ! [ ThreadEvent :: ThreadStarted ( ThreadStartedEvent {
172175 thread_id: payload. session_id. to_string( ) ,
176+ timestamp: payload. timestamp. clone( ) ,
173177 } ) ]
174178 }
175179
@@ -524,14 +528,22 @@ impl EventProcessor for EventProcessorWithJsonOutput {
524528 fn process_event ( & mut self , event : Event ) -> CodexStatus {
525529 let aggregated = self . collect_thread_events ( & event) ;
526530 for conv_event in aggregated {
527- match serde_json:: to_string ( & conv_event) {
528- Ok ( line) => {
529- println ! ( "{line}" ) ;
531+ match serde_json:: to_value ( & conv_event) {
532+ Ok ( value) => {
533+ let value = self . with_timestamp ( value) ;
534+ match serde_json:: to_string ( & value) {
535+ Ok ( line) => {
536+ println ! ( "{line}" ) ;
537+ }
538+ Err ( e) => {
539+ error ! ( "Failed to serialize event: {e:?}" ) ;
540+ }
541+ }
530542 }
531543 Err ( e) => {
532544 error ! ( "Failed to serialize event: {e:?}" ) ;
533545 }
534- }
546+ } ;
535547 }
536548
537549 let Event { msg, .. } = event;
@@ -546,3 +558,18 @@ impl EventProcessor for EventProcessorWithJsonOutput {
546558 }
547559 }
548560}
561+
562+ impl EventProcessorWithJsonOutput {
563+ fn with_timestamp ( & self , mut value : JsonValue ) -> JsonValue {
564+ if let Some ( timestamp) = self . session_timestamp . as_ref ( )
565+ && let Some ( obj) = value. as_object_mut ( )
566+ && !obj. contains_key ( "timestamp" )
567+ {
568+ obj. insert (
569+ "timestamp" . to_string ( ) ,
570+ JsonValue :: String ( timestamp. clone ( ) ) ,
571+ ) ;
572+ }
573+ value
574+ }
575+ }
0 commit comments