@@ -18,6 +18,7 @@ import {
1818import { redactSecretString } from "./lib/redact" ;
1919import { repairFreeformToolInput } from "./responses/apply-patch-envelope" ;
2020import { encodeCompactionSummary } from "./responses/compaction" ;
21+ import { compileCodeModeHelperInput } from "./responses/code-mode-helper-compat" ;
2122import { isTruncatedStopReason , truncationReasonFor } from "./responses/truncated-stop-reason" ;
2223import { encodeReasoningEnvelope , type ReasoningEnvelope } from "./responses/reasoning-envelope" ;
2324import { rememberReasoningForCall } from "./responses/reasoning-replay-cache" ;
@@ -244,9 +245,14 @@ export function bridgeToResponsesSSE(
244245 // Freeform/custom tools (apply_patch, code-mode exec) carry their body in `input`; the
245246 // model is given a function with `{input:string}`, so unwrap it here when relaying back
246247 // as a custom_tool_call. Decorated apply_patch envelopes are repaired at this boundary.
247- const freeformInput = ( args : string , toolName : string , namespace ?: string ) : string => (
248- repairFreeformToolInput ( args , toolName , namespace )
249- ) ;
248+ const freeformInput = (
249+ args : string ,
250+ toolName : string ,
251+ namespace ?: string ,
252+ codeModeHelperName ?: string ,
253+ ) : string => codeModeHelperName
254+ ? compileCodeModeHelperInput ( args , codeModeHelperName )
255+ : repairFreeformToolInput ( args , toolName , namespace ) ;
250256 // Best-effort unwrap of a PARTIAL freeform arg buffer for live input streaming
251257 // (`response.custom_tool_call_input.delta` — codex-rs uses it for UI preview only;
252258 // the completed custom_tool_call item stays authoritative). Compact `{"input":"...`
@@ -530,7 +536,7 @@ export function bridgeToResponsesSSE(
530536 // synthetic compaction item's payload on done.
531537 let compactionText = "" ;
532538 let compactionTextBytes = 0 ;
533- let currentToolCall : { itemId : string ; outputIndex : number ; callId : string ; name : string ; args : string ; argsBytes : number ; namespace ?: string ; freeform ?: boolean ; toolSearch ?: boolean ; inputEmitted ?: string ; providerMetadata ?: OcxProviderOpaqueToolCallMetadata } | null = null ;
539+ let currentToolCall : { itemId : string ; outputIndex : number ; callId : string ; name : string ; args : string ; argsBytes : number ; namespace ?: string ; freeform ?: boolean ; toolSearch ?: boolean ; inputEmitted ?: string ; codeModeHelperName ?: string ; providerMetadata ?: OcxProviderOpaqueToolCallMetadata } | null = null ;
534540 // Open native web-search cell (between begin and end). Holds the output index allocated on
535541 // begin so the matching done reuses it; closed as `failed` if the stream terminates early.
536542 let currentWebSearch : { itemId : string ; eventId : string ; outputIndex : number } | null = null ;
@@ -649,7 +655,7 @@ export function bridgeToResponsesSSE(
649655 emit ( "response.custom_tool_call_input.done" , {
650656 item_id : currentToolCall . itemId , output_index : currentToolCall . outputIndex ,
651657 ...( currentToolCall . namespace ? { namespace : currentToolCall . namespace } : { } ) ,
652- input : freeformInput ( currentToolCall . args , currentToolCall . name , currentToolCall . namespace ) ,
658+ input : freeformInput ( currentToolCall . args , currentToolCall . name , currentToolCall . namespace , currentToolCall . codeModeHelperName ) ,
653659 } ) ;
654660 }
655661 // Freeform tools serialize as custom_tool_call without extra_content; remember the
@@ -666,7 +672,7 @@ export function bridgeToResponsesSSE(
666672 type : "custom_tool_call" , id : currentToolCall . itemId ,
667673 call_id : currentToolCall . callId , name : currentToolCall . name ,
668674 ...( currentToolCall . namespace ? { namespace : currentToolCall . namespace } : { } ) ,
669- input : freeformInput ( currentToolCall . args , currentToolCall . name , currentToolCall . namespace ) , status : "completed" ,
675+ input : freeformInput ( currentToolCall . args , currentToolCall . name , currentToolCall . namespace , currentToolCall . codeModeHelperName ) , status : "completed" ,
670676 }
671677 : {
672678 type : "function_call" , id : currentToolCall . itemId ,
@@ -706,7 +712,7 @@ export function bridgeToResponsesSSE(
706712 type : "custom_tool_call" , id : currentToolCall . itemId ,
707713 call_id : currentToolCall . callId , name : currentToolCall . name ,
708714 ...( currentToolCall . namespace ? { namespace : currentToolCall . namespace } : { } ) ,
709- input : freeformInput ( currentToolCall . args , currentToolCall . name , currentToolCall . namespace ) , status : "incomplete" ,
715+ input : freeformInput ( currentToolCall . args , currentToolCall . name , currentToolCall . namespace , currentToolCall . codeModeHelperName ) , status : "incomplete" ,
710716 }
711717 : {
712718 type : "function_call" , id : currentToolCall . itemId ,
@@ -1052,6 +1058,9 @@ export function bridgeToResponsesSSE(
10521058 }
10531059 if ( currentToolCall ) closeCurrentToolCall ( ) ;
10541060 const effectiveName = normalizeDeclaredToolName ( event . name , options ?. declaredToolNames ) ;
1061+ const codeModeHelperName = effectiveName === "exec" && event . name !== effectiveName
1062+ ? event . name
1063+ : undefined ;
10551064 const mapped = toolNsMap ?. get ( effectiveName ) ;
10561065 const realName = mapped ?. name ?? effectiveName ;
10571066 if ( options ?. declaredToolNames && ! options . declaredToolNames . has ( effectiveName ) ) {
@@ -1083,7 +1092,7 @@ export function bridgeToResponsesSSE(
10831092 ? { type : "custom_tool_call" , id : itemId , call_id : event . id , name : realName , ...( ns ? { namespace : ns } : { } ) , input : "" , status : "in_progress" }
10841093 : { type : "function_call" , id : itemId , call_id : event . id , name : realName , arguments : "" , status : "in_progress" , ...( ns ? { namespace : ns } : { } ) } ;
10851094 emit ( "response.output_item.added" , { output_index : outputIndex , item } ) ;
1086- currentToolCall = { itemId, outputIndex, callId : event . id , name : realName , args : "" , argsBytes : 0 , namespace : ns , freeform, toolSearch, providerMetadata : event . providerMetadata } ;
1095+ currentToolCall = { itemId, outputIndex, callId : event . id , name : realName , args : "" , argsBytes : 0 , namespace : ns , freeform, toolSearch, codeModeHelperName , providerMetadata : event . providerMetadata } ;
10871096 budget ?. openCall ( event . id ) ;
10881097 break ;
10891098 }
@@ -1102,7 +1111,7 @@ export function bridgeToResponsesSSE(
11021111 delta : event . arguments ,
11031112 } ) ;
11041113 }
1105- if ( currentToolCall . freeform ) {
1114+ if ( currentToolCall . freeform && ! currentToolCall . codeModeHelperName ) {
11061115 // Hold while the buffer is still an ambiguous prefix of the JSON wrapper,
11071116 // then stream only the unwrapped input suffix (never rewind on mode flips).
11081117 if ( ! FREEFORM_WRAP_PREFIX . startsWith ( currentToolCall . args ) ) {
@@ -1587,15 +1596,21 @@ function buildResponseJSONWithBudget(
15871596 let batchKiroRedactedBytes = 0 ;
15881597 let currentToolCallId = "" ;
15891598 let currentToolCallName = "" ;
1599+ let currentToolCallCodeModeHelperName : string | undefined ;
15901600 let currentToolCallArgs = "" ;
15911601 let currentToolCallProviderMetadata : OcxProviderOpaqueToolCallMetadata | undefined ;
15921602 let currentToolCallArgsBytes = 0 ;
15931603 // Web-search citations awaiting the next assistant message (attached as url_citation annotations).
15941604 let pendingWebSources : { url : string ; title ?: string } [ ] = [ ] ;
15951605
1596- const freeformInput = ( args : string , toolName : string , namespace ?: string ) : string => (
1597- repairFreeformToolInput ( args , toolName , namespace )
1598- ) ;
1606+ const freeformInput = (
1607+ args : string ,
1608+ toolName : string ,
1609+ namespace ?: string ,
1610+ codeModeHelperName ?: string ,
1611+ ) : string => codeModeHelperName
1612+ ? compileCodeModeHelperInput ( args , codeModeHelperName )
1613+ : repairFreeformToolInput ( args , toolName , namespace ) ;
15991614 const parseArgsObj = ( args : string ) : Record < string , unknown > => {
16001615 try { const o = JSON . parse ( args ) ; return o && typeof o === "object" ? o : { } ; } catch { return { } ; }
16011616 } ;
@@ -1697,7 +1712,7 @@ function buildResponseJSONWithBudget(
16971712 type : "custom_tool_call" , id : `ctc_${ uuid ( ) } ` ,
16981713 call_id : currentToolCallId , name : realName ,
16991714 ...( ns ? { namespace : ns } : { } ) ,
1700- input : freeformInput ( currentToolCallArgs , realName , ns ) , status,
1715+ input : freeformInput ( currentToolCallArgs , realName , ns , currentToolCallCodeModeHelperName ) , status,
17011716 } ) ;
17021717 } else {
17031718 pushOutput ( {
@@ -1711,6 +1726,7 @@ function buildResponseJSONWithBudget(
17111726 budget ?. closeCall ( currentToolCallId ) ;
17121727 currentToolCallId = "" ;
17131728 currentToolCallName = "" ;
1729+ currentToolCallCodeModeHelperName = undefined ;
17141730 currentToolCallProviderMetadata = undefined ;
17151731 currentToolCallArgs = "" ;
17161732 currentToolCallArgsBytes = 0 ;
@@ -1825,6 +1841,9 @@ function buildResponseJSONWithBudget(
18251841 currentToolCallId = e . id ;
18261842 budget ?. openCall ( e . id ) ;
18271843 currentToolCallName = effectiveName ;
1844+ currentToolCallCodeModeHelperName = effectiveName === "exec" && e . name !== effectiveName
1845+ ? e . name
1846+ : undefined ;
18281847 currentToolCallArgs = "" ;
18291848 currentToolCallArgsBytes = 0 ;
18301849 currentToolCallProviderMetadata = e . providerMetadata ;
0 commit comments