Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static AgentTaskConfigurer loop(Predicate<AgenticScope> exitCondition, Ob
}

public static AgentTaskConfigurer loop(
Predicate<AgenticScope> exitCondition, int maxIterations, Object... agents) {
int maxIterations, Predicate<AgenticScope> exitCondition, Object... agents) {
return list ->
list.loop(
l -> l.subAgents(agents).exitCondition(exitCondition).maxIterations(maxIterations));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static io.serverlessworkflow.fluent.agentic.AgentWorkflowBuilder.workflow;
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.conditional;
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.doTasks;
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.fn;
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.loop;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -29,6 +31,7 @@
import io.serverlessworkflow.api.types.func.CallTaskJava;
import io.serverlessworkflow.api.types.func.ForTaskFunction;
import io.serverlessworkflow.impl.WorkflowApplication;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -45,10 +48,7 @@ public void sequentialWorkflow() {
var audienceEditor = AgentsUtils.newAudienceEditor();
var styleEditor = AgentsUtils.newStyleEditor();

Workflow wf =
workflow("seqFlow")
.sequence("process", creativeWriter, audienceEditor, styleEditor)
.build();
Workflow wf = workflow("seqFlow").sequence(creativeWriter, audienceEditor, styleEditor).build();

List<TaskItem> items = wf.getDo();
assertThat(items).hasSize(3);
Expand Down Expand Up @@ -77,13 +77,18 @@ public void sequentialWorkflow() {
@Test
@DisplayName("Looping agents via DSL.loop(...)")
public void loopWorkflow() {

var scorer = AgentsUtils.newStyleScorer();
var editor = AgentsUtils.newStyleEditor();
var creativeWriter = AgentsUtils.newCreativeWriter();
var styleScorer = AgentsUtils.newStyleScorer();
var styleEditor = AgentsUtils.newStyleEditor();

Workflow wf =
AgentWorkflowBuilder.workflow("retryFlow")
.loop("reviewLoop", c -> c.readState("score", 0).doubleValue() >= 0.8, scorer, editor)
.agent(creativeWriter)
.loop(
"reviewLoop",
c -> c.readState("score", 0).doubleValue() >= 0.8,
styleScorer,
styleEditor)
.build();

List<TaskItem> items = wf.getDo();
Expand Down Expand Up @@ -118,7 +123,7 @@ public void loopWorkflowWithMaxIterations() {

Predicate<AgenticScope> until = s -> s.readState("score", 0).doubleValue() >= 0.8;

Workflow wf = workflow("retryFlow").loop(until, scorer, 5, editor).build();
Workflow wf = workflow("retryFlow").tasks(loop(5, until, scorer, editor)).build();

List<TaskItem> items = wf.getDo();
assertThat(items).hasSize(1);
Expand All @@ -144,13 +149,53 @@ public void loopWorkflowWithMaxIterations() {
assertThat(result).containsKey("story");
}

public record EveningPlan(String movie, String meal) {}

@Test
@DisplayName("Parallel agents via DSL.parallel(...)")
public void parallelWorkflow() {
var foodExpert = AgentsUtils.newFoodExpert();
var movieExpert = AgentsUtils.newMovieExpert();

Workflow wf = workflow("forkFlow").parallel("fanout", foodExpert, movieExpert).build();
workflow("forkFlow")
.tasks(
d ->
d.parallel(foodExpert, movieExpert)
.callFn(
fn(
f -> {
Map<String, List<String>> asMap = (Map<String, List<String>>) f;
List<EveningPlan> result = new ArrayList<>();
int max =
asMap.values().stream()
.map(List::size)
.min(Integer::compareTo)
.orElse(0);
for (int i = 0; i < max; i++) {
result.add(
new EveningPlan(
asMap.get("movies").get(i), asMap.get("meals").get(i)));
}
return result;
})))
.build();

Workflow wf =
workflow("forkFlow")
.tasks(
d ->
d.parallel("fanout", foodExpert, movieExpert)
.callFn(
fn(
(Map<String, List<String>> m) -> {
var movies = m.getOrDefault("movies", List.of());
var meals = m.getOrDefault("meals", List.of());
return java.util.stream.IntStream.range(
0, Math.min(movies.size(), meals.size()))
.mapToObj(i -> new EveningPlan(movies.get(i), meals.get(i)))
.toList();
})))
.build();

List<TaskItem> items = wf.getDo();
assertThat(items).hasSize(1);
Expand Down
Loading