Skip to content
Merged
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 @@ -16,17 +16,25 @@
package io.serverlessworkflow.fluent.agentic;

import dev.langchain4j.agentic.scope.AgenticScope;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.fluent.func.spi.FuncTransformations;
import io.serverlessworkflow.fluent.spec.BaseWorkflowBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;

public class AgentWorkflowBuilder
extends BaseWorkflowBuilder<AgentWorkflowBuilder, AgentDoTaskBuilder, AgentTaskItemListBuilder>
implements FuncTransformations<AgentWorkflowBuilder> {

private final List<AgentDoTaskBuilder> agentDoTaskBuilders;

AgentWorkflowBuilder(final String name, final String namespace, final String version) {
super(name, namespace, version);
agentDoTaskBuilders = new ArrayList<>();
}

public static AgentWorkflowBuilder workflow() {
Expand All @@ -49,7 +57,7 @@ public AgentWorkflowBuilder agent(Object agent) {
public AgentWorkflowBuilder agent(String name, Object agent) {
final AgentDoTaskBuilder doTaskBuilder = this.newDo();
doTaskBuilder.agent(name, agent);
this.workflow.setDo(doTaskBuilder.build().getDo());
agentDoTaskBuilders.add(doTaskBuilder);
return this;
}

Expand All @@ -60,7 +68,7 @@ public AgentWorkflowBuilder sequence(Object... agents) {
public AgentWorkflowBuilder sequence(String name, Object... agents) {
final AgentDoTaskBuilder doTaskBuilder = this.newDo();
doTaskBuilder.sequence(name, agents);
this.workflow.setDo(doTaskBuilder.build().getDo());
agentDoTaskBuilders.add(doTaskBuilder);
return this;
}

Expand All @@ -71,7 +79,7 @@ public AgentWorkflowBuilder parallel(Object... agents) {
public AgentWorkflowBuilder parallel(String name, Object... agents) {
final AgentDoTaskBuilder doTaskBuilder = this.newDo();
doTaskBuilder.parallel(name, agents);
this.workflow.setDo(doTaskBuilder.build().getDo());
agentDoTaskBuilders.add(doTaskBuilder);
return this;
}

Expand All @@ -83,7 +91,7 @@ public AgentWorkflowBuilder loop(
String name, Predicate<AgenticScope> exitCondition, Object... agents) {
final AgentDoTaskBuilder doTaskBuilder = this.newDo();
doTaskBuilder.loop(name, loop -> loop.subAgents(agents).exitCondition(exitCondition));
this.workflow.setDo(doTaskBuilder.build().getDo());
agentDoTaskBuilders.add(doTaskBuilder);
return this;
}

Expand All @@ -96,4 +104,15 @@ protected AgentDoTaskBuilder newDo() {
protected AgentWorkflowBuilder self() {
return this;
}

@Override
public Workflow build() {
List<TaskItem> items = new ArrayList<>(workflow.getDo());
this.agentDoTaskBuilders.stream()
.map(AgentDoTaskBuilder::build)
.forEach(b -> items.addAll(b.getDo()));
this.workflow.setDo(Collections.unmodifiableList(items));
this.agentDoTaskBuilders.clear();
return this.workflow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import io.serverlessworkflow.api.types.Export;
import io.serverlessworkflow.api.types.Input;
import io.serverlessworkflow.api.types.Output;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.fluent.spec.spi.TransformationHandlers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

Expand Down Expand Up @@ -90,7 +94,9 @@ public SELF tasks(Consumer<DBuilder> doTaskConsumer) {
if (this.workflow.getDo() == null) {
this.workflow.setDo(doTaskBuilder.build().getDo());
} else {
this.workflow.getDo().addAll(doTaskBuilder.build().getDo());
List<TaskItem> existingTasks = new ArrayList<>(this.workflow.getDo());
existingTasks.addAll(doTaskBuilder.build().getDo());
this.workflow.setDo(Collections.unmodifiableList(existingTasks));
}
return self();
}
Expand Down