@@ -663,6 +663,83 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
663663 expect ( result . agentState . output ) . toEqual ( { result : 'success' } )
664664 } )
665665
666+ it ( 'should pass generateN from programmatic step to runAgentStep as n parameter' , async ( ) => {
667+ // Test that when programmatic step returns generateN, it's passed to runAgentStep
668+
669+ let agentStepN : number | undefined
670+
671+ const mockGeneratorFunction = function * ( ) {
672+ // Yield GENERATE_N to trigger n parameter
673+ yield { type : 'GENERATE_N' , n : 5 }
674+ } as ( ) => StepGenerator
675+
676+ mockTemplate . handleSteps = mockGeneratorFunction
677+
678+ const localAgentTemplates = {
679+ 'test-agent' : mockTemplate ,
680+ }
681+
682+ // Mock promptAiSdk to capture the n parameter
683+ loopAgentStepsBaseParams . promptAiSdk = async ( params : any ) => {
684+ agentStepN = params . n
685+ return JSON . stringify ( [
686+ 'Response 1' ,
687+ 'Response 2' ,
688+ 'Response 3' ,
689+ 'Response 4' ,
690+ 'Response 5' ,
691+ ] )
692+ }
693+
694+ await loopAgentSteps ( {
695+ ...loopAgentStepsBaseParams ,
696+ agentType : 'test-agent' ,
697+ localAgentTemplates,
698+ } )
699+
700+ // Verify generateN was passed to runAgentStep as n
701+ expect ( agentStepN ) . toBe ( 5 )
702+ } )
703+
704+ it ( 'should pass nResponses from runAgentStep back to programmatic step' , async ( ) => {
705+ // Test that nResponses returned by runAgentStep are passed to next programmatic step
706+
707+ let receivedNResponses : string [ ] | undefined
708+
709+ const mockGeneratorFunction = function * ( ) {
710+ const { nResponses } = yield { type : 'GENERATE_N' , n : 3 }
711+ receivedNResponses = nResponses
712+ const step = yield {
713+ toolName : 'read_files' ,
714+ input : { paths : [ 'test.txt' ] } ,
715+ }
716+ yield { toolName : 'end_turn' , input : { } }
717+ } as ( ) => StepGenerator
718+
719+ mockTemplate . handleSteps = mockGeneratorFunction
720+
721+ const localAgentTemplates = {
722+ 'test-agent' : mockTemplate ,
723+ }
724+
725+ const expectedResponses = [
726+ 'Implementation A' ,
727+ 'Implementation B' ,
728+ 'Implementation C' ,
729+ ]
730+ loopAgentStepsBaseParams . promptAiSdk = async ( ) => {
731+ return JSON . stringify ( expectedResponses )
732+ }
733+
734+ await loopAgentSteps ( {
735+ ...loopAgentStepsBaseParams ,
736+ agentType : 'test-agent' ,
737+ localAgentTemplates,
738+ } )
739+
740+ expect ( receivedNResponses ) . toEqual ( expectedResponses )
741+ } )
742+
666743 it ( 'should allow agents without outputSchema to end normally' , async ( ) => {
667744 // Test that agents without outputSchema can end without setting output
668745
0 commit comments