@@ -68,7 +68,7 @@ public void sequentialWorkflow() {
6868 }
6969
7070 @ Test
71- @ DisplayName ("Looping agents via DSL.loop(...)" ) // TODO maxIterations(5)
71+ @ DisplayName ("Looping agents via DSL.loop(...)" )
7272 public void loopWorkflow () {
7373
7474 var scorer = AgentsUtils .newStyleScorer ();
@@ -103,6 +103,48 @@ public void loopWorkflow() {
103103 assertThat (result ).containsKey ("story" );
104104 }
105105
106+ @ Test
107+ @ DisplayName ("Looping agents via DSL.loop(...)" )
108+ public void loopWorkflowWithMaxIterations () {
109+ var scorer = AgentsUtils .newStyleScorer ();
110+ var editor = AgentsUtils .newStyleEditor ();
111+
112+ Workflow wf =
113+ AgentWorkflowBuilder .workflow ("maxFlow" )
114+ .tasks (
115+ d ->
116+ d .loop (
117+ "limit" ,
118+ l ->
119+ l .maxIterations (5 )
120+ .exitCondition (c -> c .readState ("score" , 0 ).doubleValue () >= 0.8 )
121+ .subAgents ("sub" , scorer , editor )))
122+ .build ();
123+
124+ List <TaskItem > items = wf .getDo ();
125+ assertThat (items ).hasSize (1 );
126+
127+ var fn = (ForTaskFunction ) items .get (0 ).getTask ().getForTask ();
128+ assertThat (fn .getDo ()).isNotNull ();
129+ assertThat (fn .getDo ()).hasSize (2 );
130+ fn .getDo ()
131+ .forEach (si -> assertThat (si .getTask ().getCallTask ()).isInstanceOf (CallTaskJava .class ));
132+
133+ Map <String , Object > input =
134+ Map .of (
135+ "story" , "dragons and wizards" ,
136+ "style" , "comedy" );
137+
138+ Map <String , Object > result ;
139+ try (WorkflowApplication app = WorkflowApplication .builder ().build ()) {
140+ result = app .workflowDefinition (wf ).instance (input ).start ().get ().asMap ().orElseThrow ();
141+ } catch (Exception e ) {
142+ throw new RuntimeException ("Workflow execution failed" , e );
143+ }
144+
145+ assertThat (result ).containsKey ("story" );
146+ }
147+
106148 @ Test
107149 @ DisplayName ("Parallel agents via DSL.parallel(...)" )
108150 public void parallelWorkflow () {
@@ -115,7 +157,9 @@ public void parallelWorkflow() {
115157 assertThat (items ).hasSize (1 );
116158
117159 var fork = items .get (0 ).getTask ().getForkTask ();
160+ // two branches created
118161 assertThat (fork .getFork ().getBranches ()).hasSize (2 );
162+ // branch names follow "branch-{index}-{name}"
119163 assertThat (fork .getFork ().getBranches ().get (0 ).getName ()).isEqualTo ("branch-0-fanout" );
120164 assertThat (fork .getFork ().getBranches ().get (1 ).getName ()).isEqualTo ("branch-1-fanout" );
121165
@@ -132,6 +176,50 @@ public void parallelWorkflow() {
132176 assertEquals ("Fake conflict response" , result .get ("movies" ));
133177 }
134178
179+ // TODO
180+ @ Test
181+ @ DisplayName ("Conditional agents via choice(...)" )
182+ public void conditionalWorkflow () {
183+
184+ var category = AgentsUtils .newCategoryRouter ();
185+ var a1 = AgentsUtils .newMedicalExpert ();
186+ var a2 = AgentsUtils .newTechnicalExpert ();
187+ var a3 = AgentsUtils .newLegalExpert ();
188+ }
189+
190+ // TODO
191+ @ Test
192+ @ DisplayName ("Error handling with agents" )
193+ public void errorHandling () {
194+ var a1 = AgentsUtils .newCreativeWriter ();
195+ var a2 = AgentsUtils .newAudienceEditor ();
196+ var a3 = AgentsUtils .newStyleEditor ();
197+
198+ Workflow wf = workflow ("seqFlow" ).tasks (tasks -> tasks .sequence ("process" , a1 , a2 , a3 )).build ();
199+
200+ List <TaskItem > items = wf .getDo ();
201+ assertThat (items ).hasSize (3 );
202+
203+ assertThat (items .get (0 ).getName ()).isEqualTo ("process-0" );
204+ assertThat (items .get (1 ).getName ()).isEqualTo ("process-1" );
205+ assertThat (items .get (2 ).getName ()).isEqualTo ("process-2" );
206+ items .forEach (it -> assertThat (it .getTask ().getCallTask ()).isInstanceOf (CallTaskJava .class ));
207+
208+ Map <String , Object > input =
209+ Map .of (
210+ "style" , "fantasy" ,
211+ "audience" , "young adults" );
212+
213+ Map <String , Object > result ;
214+ try (WorkflowApplication app = WorkflowApplication .builder ().build ()) {
215+ result = app .workflowDefinition (wf ).instance (input ).start ().get ().asMap ().orElseThrow ();
216+ } catch (Exception e ) {
217+ throw new RuntimeException ("Workflow execution failed" , e );
218+ }
219+
220+ assertThat (result ).containsKey ("story" );
221+ }
222+
135223 @ Test
136224 @ DisplayName ("Human in the loop" )
137225 public void humanInTheLoop () {
@@ -149,8 +237,7 @@ public void humanInTheLoop() {
149237
150238 var a1 = AgentsUtils .newAstrologyAgent ();
151239
152- Workflow wf =
153- workflow ("seqFlow" ).tasks (tasks -> tasks .sequence ("process" , a1 , humanInTheLoop )).build ();
240+ Workflow wf = workflow ("seqFlow" ).sequence ("process" , a1 , humanInTheLoop ).build ();
154241
155242 assertThat (wf .getDo ()).hasSize (2 );
156243
0 commit comments