18
18
import static io .serverlessworkflow .fluent .agentic .AgentWorkflowBuilder .workflow ;
19
19
import static io .serverlessworkflow .fluent .agentic .dsl .AgenticDSL .conditional ;
20
20
import static io .serverlessworkflow .fluent .agentic .dsl .AgenticDSL .doTasks ;
21
+ import static io .serverlessworkflow .fluent .agentic .dsl .AgenticDSL .loop ;
21
22
import static io .serverlessworkflow .fluent .spec .dsl .DSL .tasks ;
22
23
import static org .assertj .core .api .Assertions .assertThat ;
23
24
import static org .junit .jupiter .api .Assertions .assertEquals ;
24
25
25
26
import dev .langchain4j .agentic .AgenticServices ;
27
+ import dev .langchain4j .agentic .scope .AgenticScope ;
26
28
import dev .langchain4j .agentic .workflow .HumanInTheLoop ;
27
- import io .serverlessworkflow .api .types .FlowDirectiveEnum ;
28
29
import io .serverlessworkflow .api .types .TaskItem ;
29
30
import io .serverlessworkflow .api .types .Workflow ;
30
31
import io .serverlessworkflow .api .types .func .CallTaskJava ;
33
34
import java .util .List ;
34
35
import java .util .Map ;
35
36
import java .util .concurrent .atomic .AtomicReference ;
37
+ import java .util .function .Predicate ;
36
38
import org .junit .jupiter .api .DisplayName ;
37
39
import org .junit .jupiter .api .Test ;
38
40
@@ -41,11 +43,14 @@ public class LC4JEquivalenceIT {
41
43
@ Test
42
44
@ DisplayName ("Sequential agents via DSL.sequence(...)" )
43
45
public void sequentialWorkflow () {
44
- var a1 = AgentsUtils .newCreativeWriter ();
45
- var a2 = AgentsUtils .newAudienceEditor ();
46
- var a3 = AgentsUtils .newStyleEditor ();
46
+ var creativeWriter = AgentsUtils .newCreativeWriter ();
47
+ var audienceEditor = AgentsUtils .newAudienceEditor ();
48
+ var styleEditor = AgentsUtils .newStyleEditor ();
47
49
48
- Workflow wf = workflow ("seqFlow" ).tasks (tasks -> tasks .sequence ("process" , a1 , a2 , a3 )).build ();
50
+ Workflow wf =
51
+ workflow ("seqFlow" )
52
+ .tasks (tasks -> tasks .sequence ("process" , creativeWriter , audienceEditor , styleEditor ))
53
+ .build ();
49
54
50
55
List <TaskItem > items = wf .getDo ();
51
56
assertThat (items ).hasSize (3 );
@@ -113,17 +118,10 @@ public void loopWorkflowWithMaxIterations() {
113
118
var scorer = AgentsUtils .newStyleScorer ();
114
119
var editor = AgentsUtils .newStyleEditor ();
115
120
121
+ Predicate <AgenticScope > until = s -> s .readState ("score" , 0 ).doubleValue () >= 0.8 ;
122
+
116
123
Workflow wf =
117
- AgentWorkflowBuilder .workflow ("maxFlow" )
118
- .tasks (
119
- d ->
120
- d .loop (
121
- "limit" ,
122
- l ->
123
- l .maxIterations (5 )
124
- .exitCondition (c -> c .readState ("score" , 0 ).doubleValue () >= 0.8 )
125
- .subAgents ("sub" , scorer , editor )))
126
- .build ();
124
+ AgentWorkflowBuilder .workflow ("retryFlow" ).tasks (loop (until , scorer , 5 , editor )).build ();
127
125
128
126
List <TaskItem > items = wf .getDo ();
129
127
assertThat (items ).hasSize (1 );
@@ -152,10 +150,10 @@ public void loopWorkflowWithMaxIterations() {
152
150
@ Test
153
151
@ DisplayName ("Parallel agents via DSL.parallel(...)" )
154
152
public void parallelWorkflow () {
155
- var a1 = AgentsUtils .newFoodExpert ();
156
- var a2 = AgentsUtils .newMovieExpert ();
153
+ var foodExpert = AgentsUtils .newFoodExpert ();
154
+ var movieExpert = AgentsUtils .newMovieExpert ();
157
155
158
- Workflow wf = workflow ("forkFlow" ).parallel ("fanout" , a1 , a2 ).build ();
156
+ Workflow wf = workflow ("forkFlow" ).parallel ("fanout" , foodExpert , movieExpert ).build ();
159
157
160
158
List <TaskItem > items = wf .getDo ();
161
159
assertThat (items ).hasSize (1 );
@@ -183,11 +181,14 @@ public void parallelWorkflow() {
183
181
@ Test
184
182
@ DisplayName ("Error handling with agents" )
185
183
public void errorHandling () {
186
- var a1 = AgentsUtils .newCreativeWriter ();
187
- var a2 = AgentsUtils .newAudienceEditor ();
188
- var a3 = AgentsUtils .newStyleEditor ();
184
+ var creativeWriter = AgentsUtils .newCreativeWriter ();
185
+ var audienceEditor = AgentsUtils .newAudienceEditor ();
186
+ var styleEditor = AgentsUtils .newStyleEditor ();
189
187
190
- Workflow wf = workflow ("seqFlow" ).tasks (tasks -> tasks .sequence ("process" , a1 , a2 , a3 )).build ();
188
+ Workflow wf =
189
+ workflow ("seqFlow" )
190
+ .tasks (tasks -> tasks .sequence ("process" , creativeWriter , audienceEditor , styleEditor ))
191
+ .build ();
191
192
192
193
List <TaskItem > items = wf .getDo ();
193
194
assertThat (items ).hasSize (3 );
@@ -218,26 +219,18 @@ public void errorHandling() {
218
219
public void conditionalWorkflow () {
219
220
220
221
var category = AgentsUtils .newCategoryRouter ();
221
- var a1 = AgentsUtils .newMedicalExpert ();
222
- var a2 = AgentsUtils .newTechnicalExpert ();
223
- var a3 = AgentsUtils .newLegalExpert ();
222
+ var medicalExpert = AgentsUtils .newMedicalExpert ();
223
+ var technicalExpert = AgentsUtils .newTechnicalExpert ();
224
+ var legalExpert = AgentsUtils .newLegalExpert ();
224
225
225
226
Workflow wf =
226
227
workflow ("conditional" )
227
228
.sequence ("process" , category )
228
- .tasks (
229
- t ->
230
- t .switchCase (
231
- p ->
232
- p .onPredicate (
233
- item ->
234
- item .when (Agents .RequestCategory .UNKNOWN ::equals )
235
- .then (FlowDirectiveEnum .END ))))
236
229
.tasks (
237
230
doTasks (
238
- conditional (Agents .RequestCategory .MEDICAL ::equals , a1 ),
239
- conditional (Agents .RequestCategory .TECHNICAL ::equals , a2 ),
240
- conditional (Agents .RequestCategory .LEGAL ::equals , a3 )))
231
+ conditional (Agents .RequestCategory .MEDICAL ::equals , medicalExpert ),
232
+ conditional (Agents .RequestCategory .TECHNICAL ::equals , technicalExpert ),
233
+ conditional (Agents .RequestCategory .LEGAL ::equals , legalExpert )))
241
234
.build ();
242
235
243
236
Map <String , Object > input = Map .of ("question" , "What is the best treatment for a common cold?" );
@@ -267,9 +260,9 @@ public void humanInTheLoop() {
267
260
.responseReader (() -> "piscis" )
268
261
.build ();
269
262
270
- var a1 = AgentsUtils .newAstrologyAgent ();
263
+ var astrologyAgent = AgentsUtils .newAstrologyAgent ();
271
264
272
- Workflow wf = workflow ("seqFlow" ).sequence ("process" , a1 , humanInTheLoop ).build ();
265
+ Workflow wf = workflow ("seqFlow" ).sequence ("process" , astrologyAgent , humanInTheLoop ).build ();
273
266
274
267
assertThat (wf .getDo ()).hasSize (2 );
275
268
0 commit comments