1818import static io .serverlessworkflow .fluent .agentic .AgentWorkflowBuilder .workflow ;
1919import static io .serverlessworkflow .fluent .agentic .dsl .AgenticDSL .conditional ;
2020import static io .serverlessworkflow .fluent .agentic .dsl .AgenticDSL .doTasks ;
21+ import static io .serverlessworkflow .fluent .agentic .dsl .AgenticDSL .loop ;
2122import static io .serverlessworkflow .fluent .spec .dsl .DSL .tasks ;
2223import static org .assertj .core .api .Assertions .assertThat ;
2324import static org .junit .jupiter .api .Assertions .assertEquals ;
2425
2526import dev .langchain4j .agentic .AgenticServices ;
27+ import dev .langchain4j .agentic .scope .AgenticScope ;
2628import dev .langchain4j .agentic .workflow .HumanInTheLoop ;
27- import io .serverlessworkflow .api .types .FlowDirectiveEnum ;
2829import io .serverlessworkflow .api .types .TaskItem ;
2930import io .serverlessworkflow .api .types .Workflow ;
3031import io .serverlessworkflow .api .types .func .CallTaskJava ;
3334import java .util .List ;
3435import java .util .Map ;
3536import java .util .concurrent .atomic .AtomicReference ;
37+ import java .util .function .Predicate ;
3638import org .junit .jupiter .api .DisplayName ;
3739import org .junit .jupiter .api .Test ;
3840
@@ -41,11 +43,14 @@ public class LC4JEquivalenceIT {
4143 @ Test
4244 @ DisplayName ("Sequential agents via DSL.sequence(...)" )
4345 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 ();
4749
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 ();
4954
5055 List <TaskItem > items = wf .getDo ();
5156 assertThat (items ).hasSize (3 );
@@ -113,17 +118,10 @@ public void loopWorkflowWithMaxIterations() {
113118 var scorer = AgentsUtils .newStyleScorer ();
114119 var editor = AgentsUtils .newStyleEditor ();
115120
121+ Predicate <AgenticScope > until = s -> s .readState ("score" , 0 ).doubleValue () >= 0.8 ;
122+
116123 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 ();
127125
128126 List <TaskItem > items = wf .getDo ();
129127 assertThat (items ).hasSize (1 );
@@ -152,10 +150,10 @@ public void loopWorkflowWithMaxIterations() {
152150 @ Test
153151 @ DisplayName ("Parallel agents via DSL.parallel(...)" )
154152 public void parallelWorkflow () {
155- var a1 = AgentsUtils .newFoodExpert ();
156- var a2 = AgentsUtils .newMovieExpert ();
153+ var foodExpert = AgentsUtils .newFoodExpert ();
154+ var movieExpert = AgentsUtils .newMovieExpert ();
157155
158- Workflow wf = workflow ("forkFlow" ).parallel ("fanout" , a1 , a2 ).build ();
156+ Workflow wf = workflow ("forkFlow" ).parallel ("fanout" , foodExpert , movieExpert ).build ();
159157
160158 List <TaskItem > items = wf .getDo ();
161159 assertThat (items ).hasSize (1 );
@@ -183,11 +181,14 @@ public void parallelWorkflow() {
183181 @ Test
184182 @ DisplayName ("Error handling with agents" )
185183 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 ();
189187
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 ();
191192
192193 List <TaskItem > items = wf .getDo ();
193194 assertThat (items ).hasSize (3 );
@@ -218,26 +219,18 @@ public void errorHandling() {
218219 public void conditionalWorkflow () {
219220
220221 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 ();
224225
225226 Workflow wf =
226227 workflow ("conditional" )
227228 .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 ))))
236229 .tasks (
237230 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 )))
241234 .build ();
242235
243236 Map <String , Object > input = Map .of ("question" , "What is the best treatment for a common cold?" );
@@ -267,9 +260,9 @@ public void humanInTheLoop() {
267260 .responseReader (() -> "piscis" )
268261 .build ();
269262
270- var a1 = AgentsUtils .newAstrologyAgent ();
263+ var astrologyAgent = AgentsUtils .newAstrologyAgent ();
271264
272- Workflow wf = workflow ("seqFlow" ).sequence ("process" , a1 , humanInTheLoop ).build ();
265+ Workflow wf = workflow ("seqFlow" ).sequence ("process" , astrologyAgent , humanInTheLoop ).build ();
273266
274267 assertThat (wf .getDo ()).hasSize (2 );
275268
0 commit comments