@@ -55,6 +55,10 @@ import {
5555} from "./CodexToolCallMapper" ;
5656import { stripShellPrefix } from "./CommandUtils" ;
5757import { createTerminalOutputMeta , type TerminalOutputMode } from "./TerminalOutputMode" ;
58+ import {
59+ createAgentTextMessageChunk ,
60+ createAgentTextThoughtChunk ,
61+ } from "./ContentChunks" ;
5862
5963export { stripShellPrefix } ;
6064
@@ -226,44 +230,20 @@ export class CodexEventHandler {
226230 }
227231
228232 private async createTextEvent ( event : AgentMessageDeltaNotification ) : Promise < UpdateSessionEvent > {
229- return {
230- sessionUpdate : "agent_message_chunk" ,
231- content : {
232- type : "text" ,
233- text : event . delta
234- }
235- }
233+ return createAgentTextMessageChunk ( event . delta , event . itemId ) ;
236234 }
237235
238236 private async createConfigWarningEvent ( event : ConfigWarningNotification ) : Promise < UpdateSessionEvent > {
239237 const detailsText = event . details ? `\n\n${ event . details } ` : "" ;
240- return {
241- sessionUpdate : "agent_message_chunk" ,
242- content : {
243- type : "text" ,
244- text : `Config warning: ${ event . summary } ${ detailsText } \n\n`
245- }
246- }
238+ return createAgentTextMessageChunk ( `Config warning: ${ event . summary } ${ detailsText } \n\n` ) ;
247239 }
248240
249241 private createWarningEvent ( event : WarningNotification ) : UpdateSessionEvent {
250- return {
251- sessionUpdate : "agent_message_chunk" ,
252- content : {
253- type : "text" ,
254- text : `Warning: ${ event . message } \n\n`
255- }
256- } ;
242+ return createAgentTextMessageChunk ( `Warning: ${ event . message } \n\n` ) ;
257243 }
258244
259245 private createModelReroutedEvent ( event : ModelReroutedNotification ) : UpdateSessionEvent {
260- return {
261- sessionUpdate : "agent_thought_chunk" ,
262- content : {
263- type : "text" ,
264- text : `Model rerouted from ${ event . fromModel } to ${ event . toModel } (${ event . reason } ).\n\n`
265- }
266- } ;
246+ return createAgentTextThoughtChunk ( `Model rerouted from ${ event . fromModel } to ${ event . toModel } (${ event . reason } ).\n\n` ) ;
267247 }
268248
269249 private createThreadGoalUpdatedEvent ( event : ThreadGoalUpdatedNotification ) : UpdateSessionEvent | null {
@@ -278,13 +258,7 @@ export class CodexEventHandler {
278258 const text = objective . includes ( "\n" )
279259 ? `Goal updated (${ status } ):\n${ objective } `
280260 : `Goal updated (${ status } ): ${ objective } ` ;
281- return {
282- sessionUpdate : "agent_message_chunk" ,
283- content : {
284- type : "text" ,
285- text : `\n\n${ text } \n\n` ,
286- } ,
287- } ;
261+ return createAgentTextMessageChunk ( `\n\n${ text } \n\n` ) ;
288262 }
289263
290264 private formatThreadGoalStatus ( status : ThreadGoalUpdatedNotification [ "goal" ] [ "status" ] ) : string {
@@ -310,13 +284,7 @@ export class CodexEventHandler {
310284 }
311285 this . sessionState . currentGoal = null ;
312286
313- return {
314- sessionUpdate : "agent_message_chunk" ,
315- content : {
316- type : "text" ,
317- text : "\n\nGoal cleared.\n\n" ,
318- } ,
319- } ;
287+ return createAgentTextMessageChunk ( "\n\nGoal cleared.\n\n" ) ;
320288 }
321289
322290 private createThreadGoalSnapshot ( event : ThreadGoalUpdatedNotification ) : ThreadGoalSnapshot {
@@ -342,22 +310,16 @@ export class CodexEventHandler {
342310 event : ReasoningSummaryTextDeltaNotification | ReasoningTextDeltaNotification
343311 ) : UpdateSessionEvent {
344312 this . seenReasoningDeltaItemIds . add ( event . itemId ) ;
345- return this . createAgentThoughtEvent ( event . delta ) ;
313+ return this . createAgentThoughtEvent ( event . delta , event . itemId ) ;
346314 }
347315
348316 private createReasoningSectionBreakEvent ( event : ReasoningSummaryPartAddedNotification ) : UpdateSessionEvent {
349317 this . seenReasoningDeltaItemIds . add ( event . itemId ) ;
350- return this . createAgentThoughtEvent ( "\n\n" ) ;
318+ return this . createAgentThoughtEvent ( "\n\n" , event . itemId ) ;
351319 }
352320
353- private createAgentThoughtEvent ( text : string ) : UpdateSessionEvent {
354- return {
355- sessionUpdate : "agent_thought_chunk" ,
356- content : {
357- type : "text" ,
358- text,
359- }
360- } ;
321+ private createAgentThoughtEvent ( text : string , messageId : string ) : UpdateSessionEvent {
322+ return createAgentTextThoughtChunk ( text , messageId ) ;
361323 }
362324
363325 private async createItemEvent ( event : ItemStartedNotification ) : Promise < UpdateSessionEvent | null > {
@@ -462,31 +424,19 @@ export class CodexEventHandler {
462424 if ( text . length === 0 ) {
463425 return null ;
464426 }
465- return this . createAgentThoughtEvent ( text ) ;
427+ return this . createAgentThoughtEvent ( text , item . id ) ;
466428 }
467429
468430 private createExitedReviewModeEvent ( item : ThreadItem & { type : "exitedReviewMode" } ) : UpdateSessionEvent | null {
469431 const text = item . review . trim ( ) ;
470432 if ( text . length === 0 ) {
471433 return null ;
472434 }
473- return {
474- sessionUpdate : "agent_message_chunk" ,
475- content : {
476- type : "text" ,
477- text,
478- }
479- } ;
435+ return createAgentTextMessageChunk ( text ) ;
480436 }
481437
482438 private createContextCompactedEvent ( ) : UpdateSessionEvent {
483- return {
484- sessionUpdate : "agent_message_chunk" ,
485- content : {
486- type : "text" ,
487- text : "*Context compacted to fit the model's context window.*\n\n"
488- }
489- } ;
439+ return createAgentTextMessageChunk ( "*Context compacted to fit the model's context window.*\n\n" ) ;
490440 }
491441
492442 private createCommandOutputDeltaEvent ( event : CommandExecutionOutputDeltaNotification ) : UpdateSessionEvent {
@@ -629,13 +579,7 @@ export class CodexEventHandler {
629579 ? RequestError . internalError ( this . createTurnErrorData ( params . error ) )
630580 : RequestError . authRequired ( this . createTurnErrorData ( params . error ) , params . error . message ) ;
631581 }
632- return {
633- sessionUpdate : "agent_message_chunk" ,
634- content : {
635- type : "text" ,
636- text : `${ params . error . message } \n\n`
637- }
638- }
582+ return createAgentTextMessageChunk ( `${ params . error . message } \n\n` ) ;
639583 }
640584
641585 private isAuthenticationRequiredError ( error : CodexErrorInfo | null ) : boolean {
0 commit comments