@@ -314,6 +314,49 @@ describe("routed Responses custom-tool compatibility", () => {
314314 rewrite . dispose ?.( ) ;
315315 } ) ;
316316
317+ test ( "replays id-less argument deltas once output_item.added resolves the routed item" , ( ) => {
318+ const budget = createTestTranslatorBudget ( ) ;
319+ const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) , budget ) ;
320+
321+ expect ( rewrite ( frame ( "response.function_call_arguments.delta" , {
322+ output_index : 0 ,
323+ delta : "{\"input\":\"echo" ,
324+ } ) ) ) . toEqual ( [ ] ) ;
325+ expect ( budget . snapshot ( ) . currentBytes ) . toBeGreaterThan ( 0 ) ;
326+
327+ const replayed = rewrite ( frame ( "response.output_item.added" , {
328+ output_index : 0 ,
329+ item : {
330+ type : "function_call" ,
331+ id : "fc_exec" ,
332+ call_id : "call_exec" ,
333+ name : "exec" ,
334+ arguments : "" ,
335+ status : "in_progress" ,
336+ } ,
337+ } ) ) ;
338+ expect ( replayed . map ( block => dataPayload ( block ) . type ) ) . toEqual ( [
339+ "response.output_item.added" ,
340+ "response.custom_tool_call_input.delta" ,
341+ ] ) ;
342+ expect ( dataPayload ( replayed [ 0 ] ! ) . item ) . toMatchObject ( { type : "custom_tool_call" , id : "ctc_exec" , name : "exec" } ) ;
343+ expect ( dataPayload ( replayed [ 1 ] ! ) ) . toMatchObject ( { item_id : "ctc_exec" , delta : "echo" } ) ;
344+ expect ( budget . snapshot ( ) . currentBytes ) . toBeGreaterThan ( 0 ) ;
345+
346+ const done = rewrite ( frame ( "response.function_call_arguments.done" , {
347+ output_index : 0 ,
348+ item_id : "fc_exec" ,
349+ arguments : "{\"input\":\"echo ok\"}" ,
350+ } ) ) ;
351+ expect ( dataPayload ( done [ 0 ] ! ) ) . toMatchObject ( {
352+ type : "response.custom_tool_call_input.done" ,
353+ item_id : "ctc_exec" ,
354+ input : "echo ok" ,
355+ } ) ;
356+ rewrite . dispose ?.( ) ;
357+ expect ( budget . snapshot ( ) . currentBytes ) . toBe ( 0 ) ;
358+ } ) ;
359+
317360 test ( "keeps progressive exec input consistent for escaped control characters" , ( ) => {
318361 const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) ) ;
319362 rewrite ( frame ( "response.output_item.added" , {
@@ -343,6 +386,87 @@ describe("routed Responses custom-tool compatibility", () => {
343386 rewrite . dispose ?.( ) ;
344387 } ) ;
345388
389+ test ( "keeps progressive exec input consistent for spaced freeform wrappers" , ( ) => {
390+ const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) ) ;
391+ rewrite ( frame ( "response.output_item.added" , {
392+ output_index : 0 ,
393+ item : { type : "function_call" , id : "fc_exec" , call_id : "call_exec" , name : "exec" , arguments : "" , status : "in_progress" } ,
394+ } ) ) ;
395+
396+ const fragments = [ '{ "input": "' , 'spaced"}' ] ;
397+ let streamedInput = "" ;
398+ for ( const delta of fragments ) {
399+ const blocks = rewrite ( frame ( "response.function_call_arguments.delta" , {
400+ output_index : 0 ,
401+ item_id : "fc_exec" ,
402+ delta,
403+ } ) ) ;
404+ for ( const block of blocks ) streamedInput += String ( dataPayload ( block ) . delta ?? "" ) ;
405+ }
406+
407+ const done = rewrite ( frame ( "response.function_call_arguments.done" , {
408+ output_index : 0 ,
409+ item_id : "fc_exec" ,
410+ arguments : fragments . join ( "" ) ,
411+ } ) ) ;
412+ expect ( streamedInput ) . toBe ( dataPayload ( done [ 0 ] ! ) . input ) ;
413+ expect ( streamedInput ) . toBe ( "spaced" ) ;
414+ rewrite . dispose ?.( ) ;
415+ } ) ;
416+
417+ test ( "suppresses progressive deltas for unrecognized argument shapes until done" , ( ) => {
418+ const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) ) ;
419+ rewrite ( frame ( "response.output_item.added" , {
420+ output_index : 0 ,
421+ item : { type : "function_call" , id : "fc_exec" , call_id : "call_exec" , name : "exec" , arguments : "" , status : "in_progress" } ,
422+ } ) ) ;
423+
424+ expect ( rewrite ( frame ( "response.function_call_arguments.delta" , {
425+ output_index : 0 ,
426+ item_id : "fc_exec" ,
427+ delta : '{"other":"x"' ,
428+ } ) ) ) . toEqual ( [ ] ) ;
429+
430+ const done = rewrite ( frame ( "response.function_call_arguments.done" , {
431+ output_index : 0 ,
432+ item_id : "fc_exec" ,
433+ arguments : '{"input":"authoritative"}' ,
434+ } ) ) ;
435+ expect ( dataPayload ( done [ 0 ] ! ) ) . toMatchObject ( {
436+ type : "response.custom_tool_call_input.done" ,
437+ input : "authoritative" ,
438+ } ) ;
439+ rewrite . dispose ?.( ) ;
440+ } ) ;
441+
442+ test ( "keeps progressive exec input consistent for split unicode escapes" , ( ) => {
443+ const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) ) ;
444+ rewrite ( frame ( "response.output_item.added" , {
445+ output_index : 0 ,
446+ item : { type : "function_call" , id : "fc_exec" , call_id : "call_exec" , name : "exec" , arguments : "" , status : "in_progress" } ,
447+ } ) ) ;
448+
449+ const fragments = [ '{"input":"caf\\u00' , 'e9 \\u0041"}' ] ;
450+ let streamedInput = "" ;
451+ for ( const delta of fragments ) {
452+ const blocks = rewrite ( frame ( "response.function_call_arguments.delta" , {
453+ output_index : 0 ,
454+ item_id : "fc_exec" ,
455+ delta,
456+ } ) ) ;
457+ for ( const block of blocks ) streamedInput += String ( dataPayload ( block ) . delta ?? "" ) ;
458+ }
459+
460+ const done = rewrite ( frame ( "response.function_call_arguments.done" , {
461+ output_index : 0 ,
462+ item_id : "fc_exec" ,
463+ arguments : fragments . join ( "" ) ,
464+ } ) ) ;
465+ expect ( streamedInput ) . toBe ( dataPayload ( done [ 0 ] ! ) . input ) ;
466+ expect ( streamedInput ) . toBe ( "café A" ) ;
467+ rewrite . dispose ?.( ) ;
468+ } ) ;
469+
346470 test ( "handleResponses sends an upstream-safe exec function and restores client SSE" , async ( ) => {
347471 const savedFetch = globalThis . fetch ;
348472 let outboundBody : Record < string , unknown > | undefined ;
@@ -404,6 +528,127 @@ describe("routed Responses custom-tool compatibility", () => {
404528 }
405529 } ) ;
406530
531+ test ( "handleResponses continuation rewrites custom_tool_call_output and keeps call_id ordered" , async ( ) => {
532+ const savedFetch = globalThis . fetch ;
533+ const outboundBodies : Array < Record < string , unknown > > = [ ] ;
534+ const firstUpstreamItem = {
535+ type : "function_call" ,
536+ id : "fc_exec" ,
537+ call_id : "call_exec" ,
538+ name : "exec" ,
539+ arguments : "{\"input\":\"const apps = await sky.list_apps();\"}" ,
540+ status : "completed" ,
541+ } ;
542+ const secondUpstreamMessage = {
543+ type : "message" ,
544+ id : "msg_2" ,
545+ role : "assistant" ,
546+ content : [ { type : "output_text" , text : "27 apps" } ] ,
547+ status : "completed" ,
548+ } ;
549+ let turn = 0 ;
550+ globalThis . fetch = ( async ( _input , init ) => {
551+ outboundBodies . push ( JSON . parse ( String ( init ?. body ) ) as Record < string , unknown > ) ;
552+ turn += 1 ;
553+ if ( turn === 1 ) {
554+ const upstream = [
555+ frame ( "response.output_item.added" , { output_index : 0 , item : { ...firstUpstreamItem , arguments : "" , status : "in_progress" } } ) ,
556+ frame ( "response.function_call_arguments.done" , { output_index : 0 , item_id : "fc_exec" , arguments : firstUpstreamItem . arguments } ) ,
557+ frame ( "response.output_item.done" , { output_index : 0 , item : firstUpstreamItem } ) ,
558+ frame ( "response.completed" , { response : { id : "resp_1" , status : "completed" , output : [ firstUpstreamItem ] } } ) ,
559+ "data: [DONE]" ,
560+ ] . join ( "\n\n" ) + "\n\n" ;
561+ return new Response ( upstream , { headers : { "content-type" : "text/event-stream" } } ) ;
562+ }
563+ const upstream = [
564+ frame ( "response.output_item.added" , { output_index : 0 , item : { ...secondUpstreamMessage , content : [ ] , status : "in_progress" } } ) ,
565+ frame ( "response.output_item.done" , { output_index : 0 , item : secondUpstreamMessage } ) ,
566+ frame ( "response.completed" , { response : { id : "resp_2" , status : "completed" , output : [ secondUpstreamMessage ] } } ) ,
567+ "data: [DONE]" ,
568+ ] . join ( "\n\n" ) + "\n\n" ;
569+ return new Response ( upstream , { headers : { "content-type" : "text/event-stream" } } ) ;
570+ } ) as typeof fetch ;
571+ const config = {
572+ port : 0 ,
573+ defaultProvider : "fixture" ,
574+ providers : {
575+ fixture : {
576+ adapter : "openai-responses" ,
577+ baseUrl : "https://fixture.test/v1" ,
578+ authMode : "key" ,
579+ apiKey : "fixture-key" ,
580+ } ,
581+ } ,
582+ } as OcxConfig ;
583+ const tools = [ { type : "custom" , name : "exec" , description : "Run JavaScript" , format : { type : "grammar" , syntax : "lark" } } ] ;
584+
585+ try {
586+ const first = await handleResponses ( new Request ( "http://localhost/v1/responses" , {
587+ method : "POST" ,
588+ headers : { "content-type" : "application/json" } ,
589+ body : JSON . stringify ( {
590+ model : "fixture/deepseek-v4-flash" ,
591+ stream : true ,
592+ input : [ { role : "user" , content : [ { type : "input_text" , text : "list apps" } ] } ] ,
593+ tools,
594+ } ) ,
595+ } ) , config , { model : "" , provider : "" } ) ;
596+ const firstSse = await first . text ( ) ;
597+ expect ( firstSse ) . toContain ( '"type":"custom_tool_call"' ) ;
598+ expect ( firstSse ) . toContain ( '"call_id":"call_exec"' ) ;
599+ expect ( firstSse ) . not . toContain ( '"type":"function_call"' ) ;
600+
601+ const second = await handleResponses ( new Request ( "http://localhost/v1/responses" , {
602+ method : "POST" ,
603+ headers : { "content-type" : "application/json" } ,
604+ body : JSON . stringify ( {
605+ model : "fixture/deepseek-v4-flash" ,
606+ stream : true ,
607+ input : [
608+ { role : "user" , content : [ { type : "input_text" , text : "list apps" } ] } ,
609+ {
610+ type : "custom_tool_call" ,
611+ id : "ctc_exec" ,
612+ call_id : "call_exec" ,
613+ name : "exec" ,
614+ input : "const apps = await sky.list_apps();" ,
615+ } ,
616+ { type : "custom_tool_call_output" , call_id : "call_exec" , output : "27 apps" } ,
617+ { type : "custom_tool_call_output" , call_id : "call_other" , output : "wrong pairing must stay distinct" } ,
618+ ] ,
619+ tools,
620+ } ) ,
621+ } ) , config , { model : "" , provider : "" } ) ;
622+ const secondSse = await second . text ( ) ;
623+ const continuationInput = outboundBodies [ 1 ] ?. input as Array < Record < string , unknown > > ;
624+ expect ( outboundBodies ) . toHaveLength ( 2 ) ;
625+ expect ( continuationInput ) . toEqual ( expect . arrayContaining ( [
626+ expect . objectContaining ( {
627+ type : "function_call" ,
628+ call_id : "call_exec" ,
629+ name : "exec" ,
630+ arguments : JSON . stringify ( { input : "const apps = await sky.list_apps();" } ) ,
631+ } ) ,
632+ expect . objectContaining ( {
633+ type : "function_call_output" ,
634+ call_id : "call_exec" ,
635+ output : "27 apps" ,
636+ } ) ,
637+ ] ) ) ;
638+ const execOutput = continuationInput . find ( item => item . type === "function_call_output" && item . call_id === "call_exec" ) ;
639+ const otherOutput = continuationInput . find ( item => item . call_id === "call_other" ) ;
640+ expect ( execOutput ) . toMatchObject ( { type : "function_call_output" , output : "27 apps" } ) ;
641+ expect ( otherOutput ) . toMatchObject ( { type : "custom_tool_call_output" , call_id : "call_other" } ) ;
642+ expect ( continuationInput . filter ( item => item . type === "function_call_output" ) ) . toHaveLength ( 1 ) ;
643+ expect ( secondSse ) . toContain ( '"text":"27 apps"' ) ;
644+ expect ( secondSse ) . toContain ( '"id":"resp_2"' ) ;
645+ expect ( secondSse ) . not . toContain ( '"type":"function_call"' ) ;
646+ expect ( secondSse . indexOf ( "resp_2" ) ) . toBeLessThan ( secondSse . indexOf ( "data: [DONE]" ) ) ;
647+ } finally {
648+ globalThis . fetch = savedFetch ;
649+ }
650+ } ) ;
651+
407652 test ( "handleResponses restores routed custom calls in non-streaming JSON" , async ( ) => {
408653 const savedFetch = globalThis . fetch ;
409654 const upstreamItem = {
0 commit comments