@@ -104,25 +104,22 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
104104 case "response.reasoning_summary_part.done" :
105105 params .ThinkingStopPending = true
106106 case "response.content_part.added" :
107- template = []byte (`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` )
108- template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
109- params .TextBlockOpen = true
110-
111- output = translatorcommon .AppendSSEEventBytes (output , "content_block_start" , template , 2 )
107+ if rootResult .Get ("part.type" ).String () == "output_text" {
108+ output = append (output , startCodexTextBlock (params )... )
109+ }
112110 case "response.output_text.delta" :
113111 params .HasTextDelta = true
112+ output = append (output , finalizeCodexThinkingBlock (params )... )
113+ output = append (output , startCodexTextBlock (params )... )
114114 template = []byte (`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` )
115115 template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
116116 template , _ = sjson .SetBytes (template , "delta.text" , rootResult .Get ("delta" ).String ())
117117
118118 output = translatorcommon .AppendSSEEventBytes (output , "content_block_delta" , template , 2 )
119119 case "response.content_part.done" :
120- template = []byte (`{"type":"content_block_stop","index":0}` )
121- template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
122- params .TextBlockOpen = false
123- params .BlockIndex ++
124-
125- output = translatorcommon .AppendSSEEventBytes (output , "content_block_stop" , template , 2 )
120+ if rootResult .Get ("part.type" ).String () == "output_text" {
121+ output = append (output , stopCodexTextBlock (params )... )
122+ }
126123 case "response.web_search_call.searching" , "response.web_search_call.completed" , "response.web_search_call.in_progress" :
127124 // Wait for populated web_search_call items on output_item.done.
128125 case "response.completed" , "response.incomplete" :
@@ -145,6 +142,7 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
145142 switch itemType {
146143 case "function_call" :
147144 output = append (output , finalizeCodexThinkingBlock (params )... )
145+ output = append (output , stopCodexTextBlock (params )... )
148146 params .HasToolCall = true
149147 params .HasReceivedArgumentsDelta = false
150148 template = []byte (`{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}` )
@@ -199,24 +197,15 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
199197 }
200198
201199 output = append (output , finalizeCodexThinkingBlock (params )... )
202- if ! params .TextBlockOpen {
203- template = []byte (`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` )
204- template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
205- params .TextBlockOpen = true
206- output = translatorcommon .AppendSSEEventBytes (output , "content_block_start" , template , 2 )
207- }
200+ output = append (output , startCodexTextBlock (params )... )
208201
209202 template = []byte (`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` )
210203 template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
211204 template , _ = sjson .SetBytes (template , "delta.text" , text )
212205 output = translatorcommon .AppendSSEEventBytes (output , "content_block_delta" , template , 2 )
213206
214- template = []byte (`{"type":"content_block_stop","index":0}` )
215- template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
216- params .TextBlockOpen = false
217- params .BlockIndex ++
207+ output = append (output , stopCodexTextBlock (params )... )
218208 params .HasTextDelta = true
219- output = translatorcommon .AppendSSEEventBytes (output , "content_block_stop" , template , 2 )
220209 case "function_call" :
221210 template = []byte (`{"type":"content_block_stop","index":0}` )
222211 template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
@@ -517,6 +506,31 @@ func ClaudeTokenCount(_ context.Context, count int64) []byte {
517506 return translatorcommon .ClaudeInputTokensJSON (count )
518507}
519508
509+ func startCodexTextBlock (params * ConvertCodexResponseToClaudeParams ) []byte {
510+ if params .TextBlockOpen {
511+ return nil
512+ }
513+
514+ template := []byte (`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` )
515+ template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
516+ params .TextBlockOpen = true
517+
518+ return translatorcommon .AppendSSEEventBytes (nil , "content_block_start" , template , 2 )
519+ }
520+
521+ func stopCodexTextBlock (params * ConvertCodexResponseToClaudeParams ) []byte {
522+ if ! params .TextBlockOpen {
523+ return nil
524+ }
525+
526+ template := []byte (`{"type":"content_block_stop","index":0}` )
527+ template , _ = sjson .SetBytes (template , "index" , params .BlockIndex )
528+ params .TextBlockOpen = false
529+ params .BlockIndex ++
530+
531+ return translatorcommon .AppendSSEEventBytes (nil , "content_block_stop" , template , 2 )
532+ }
533+
520534func startCodexThinkingBlock (params * ConvertCodexResponseToClaudeParams ) []byte {
521535 if params .ThinkingBlockOpen {
522536 return nil
0 commit comments