|
16 | 16 | package io.serverlessworkflow.fluent.agentic; |
17 | 17 |
|
18 | 18 | import static io.serverlessworkflow.fluent.agentic.AgentWorkflowBuilder.workflow; |
| 19 | +import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.conditional; |
| 20 | +import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.doTasks; |
| 21 | +import static io.serverlessworkflow.fluent.spec.dsl.DSL.tasks; |
19 | 22 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 23 | import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | 24 |
|
22 | 25 | import dev.langchain4j.agentic.AgenticServices; |
23 | 26 | import dev.langchain4j.agentic.workflow.HumanInTheLoop; |
| 27 | +import io.serverlessworkflow.api.types.FlowDirectiveEnum; |
24 | 28 | import io.serverlessworkflow.api.types.TaskItem; |
25 | 29 | import io.serverlessworkflow.api.types.Workflow; |
26 | 30 | import io.serverlessworkflow.api.types.func.CallTaskJava; |
@@ -176,17 +180,6 @@ public void parallelWorkflow() { |
176 | 180 | assertEquals("Fake conflict response", result.get("movies")); |
177 | 181 | } |
178 | 182 |
|
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 | 183 | // TODO |
191 | 184 | @Test |
192 | 185 | @DisplayName("Error handling with agents") |
@@ -220,6 +213,53 @@ public void errorHandling() { |
220 | 213 | assertThat(result).containsKey("story"); |
221 | 214 | } |
222 | 215 |
|
| 216 | + @SuppressWarnings("unchecked") |
| 217 | + @Test |
| 218 | + @DisplayName("Conditional agents via choice(...)") |
| 219 | + public void conditionalWorkflow() { |
| 220 | + |
| 221 | + var category = AgentsUtils.newCategoryRouter(); |
| 222 | + var a1 = AgentsUtils.newMedicalExpert(); |
| 223 | + var a2 = AgentsUtils.newTechnicalExpert(); |
| 224 | + var a3 = AgentsUtils.newLegalExpert(); |
| 225 | + |
| 226 | + Workflow wf = |
| 227 | + workflow("conditional") |
| 228 | + .sequence("process", category) |
| 229 | + .tasks( |
| 230 | + t -> |
| 231 | + t.switchCase( |
| 232 | + p -> |
| 233 | + p.onPredicate( |
| 234 | + item -> |
| 235 | + item.when( |
| 236 | + m -> |
| 237 | + "unknown" |
| 238 | + .equals( |
| 239 | + ((Map<String, Object>) m).get("category"))) |
| 240 | + .then(FlowDirectiveEnum.END)))) |
| 241 | + .tasks( |
| 242 | + doTasks( |
| 243 | + conditional( |
| 244 | + m -> "medical".equals(((Map<String, Object>) m).get("category")), a1), |
| 245 | + conditional( |
| 246 | + m -> "technical".equals(((Map<String, Object>) m).get("category")), a2), |
| 247 | + conditional( |
| 248 | + m -> "legal".equals(((Map<String, Object>) m).get("category")), a3))) |
| 249 | + .build(); |
| 250 | + |
| 251 | + Map<String, Object> input = Map.of("question", "What is the best treatment for a common cold?"); |
| 252 | + |
| 253 | + Map<String, Object> result; |
| 254 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 255 | + result = app.workflowDefinition(wf).instance(input).start().get().asMap().orElseThrow(); |
| 256 | + } catch (Exception e) { |
| 257 | + throw new RuntimeException("Workflow execution failed", e); |
| 258 | + } |
| 259 | + |
| 260 | + assertThat(result).containsKey("response"); |
| 261 | + } |
| 262 | + |
223 | 263 | @Test |
224 | 264 | @DisplayName("Human in the loop") |
225 | 265 | public void humanInTheLoop() { |
|
0 commit comments