@@ -68,7 +68,7 @@ public void sequentialWorkflow() {
68
68
}
69
69
70
70
@ Test
71
- @ DisplayName ("Looping agents via DSL.loop(...)" ) // TODO maxIterations(5)
71
+ @ DisplayName ("Looping agents via DSL.loop(...)" )
72
72
public void loopWorkflow () {
73
73
74
74
var scorer = AgentsUtils .newStyleScorer ();
@@ -103,6 +103,48 @@ public void loopWorkflow() {
103
103
assertThat (result ).containsKey ("story" );
104
104
}
105
105
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
+
106
148
@ Test
107
149
@ DisplayName ("Parallel agents via DSL.parallel(...)" )
108
150
public void parallelWorkflow () {
@@ -115,7 +157,9 @@ public void parallelWorkflow() {
115
157
assertThat (items ).hasSize (1 );
116
158
117
159
var fork = items .get (0 ).getTask ().getForkTask ();
160
+ // two branches created
118
161
assertThat (fork .getFork ().getBranches ()).hasSize (2 );
162
+ // branch names follow "branch-{index}-{name}"
119
163
assertThat (fork .getFork ().getBranches ().get (0 ).getName ()).isEqualTo ("branch-0-fanout" );
120
164
assertThat (fork .getFork ().getBranches ().get (1 ).getName ()).isEqualTo ("branch-1-fanout" );
121
165
@@ -132,6 +176,50 @@ public void parallelWorkflow() {
132
176
assertEquals ("Fake conflict response" , result .get ("movies" ));
133
177
}
134
178
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
+
135
223
@ Test
136
224
@ DisplayName ("Human in the loop" )
137
225
public void humanInTheLoop () {
@@ -149,8 +237,7 @@ public void humanInTheLoop() {
149
237
150
238
var a1 = AgentsUtils .newAstrologyAgent ();
151
239
152
- Workflow wf =
153
- workflow ("seqFlow" ).tasks (tasks -> tasks .sequence ("process" , a1 , humanInTheLoop )).build ();
240
+ Workflow wf = workflow ("seqFlow" ).sequence ("process" , a1 , humanInTheLoop ).build ();
154
241
155
242
assertThat (wf .getDo ()).hasSize (2 );
156
243
0 commit comments