1616package io .serverlessworkflow .fluent .agentic ;
1717
1818import dev .langchain4j .agentic .scope .AgenticScope ;
19+ import io .serverlessworkflow .api .types .TaskItem ;
20+ import io .serverlessworkflow .api .types .Workflow ;
1921import io .serverlessworkflow .fluent .func .spi .FuncTransformations ;
2022import io .serverlessworkflow .fluent .spec .BaseWorkflowBuilder ;
23+ import java .util .ArrayList ;
24+ import java .util .Collections ;
25+ import java .util .List ;
2126import java .util .UUID ;
2227import java .util .function .Predicate ;
2328
2429public class AgentWorkflowBuilder
2530 extends BaseWorkflowBuilder <AgentWorkflowBuilder , AgentDoTaskBuilder , AgentTaskItemListBuilder >
2631 implements FuncTransformations <AgentWorkflowBuilder > {
2732
33+ private final List <AgentDoTaskBuilder > agentDoTaskBuilders ;
34+
2835 AgentWorkflowBuilder (final String name , final String namespace , final String version ) {
2936 super (name , namespace , version );
37+ agentDoTaskBuilders = new ArrayList <>();
3038 }
3139
3240 public static AgentWorkflowBuilder workflow () {
@@ -49,7 +57,7 @@ public AgentWorkflowBuilder agent(Object agent) {
4957 public AgentWorkflowBuilder agent (String name , Object agent ) {
5058 final AgentDoTaskBuilder doTaskBuilder = this .newDo ();
5159 doTaskBuilder .agent (name , agent );
52- this . workflow . setDo (doTaskBuilder . build (). getDo () );
60+ agentDoTaskBuilders . add (doTaskBuilder );
5361 return this ;
5462 }
5563
@@ -60,7 +68,7 @@ public AgentWorkflowBuilder sequence(Object... agents) {
6068 public AgentWorkflowBuilder sequence (String name , Object ... agents ) {
6169 final AgentDoTaskBuilder doTaskBuilder = this .newDo ();
6270 doTaskBuilder .sequence (name , agents );
63- this . workflow . setDo (doTaskBuilder . build (). getDo () );
71+ agentDoTaskBuilders . add (doTaskBuilder );
6472 return this ;
6573 }
6674
@@ -71,7 +79,7 @@ public AgentWorkflowBuilder parallel(Object... agents) {
7179 public AgentWorkflowBuilder parallel (String name , Object ... agents ) {
7280 final AgentDoTaskBuilder doTaskBuilder = this .newDo ();
7381 doTaskBuilder .parallel (name , agents );
74- this . workflow . setDo (doTaskBuilder . build (). getDo () );
82+ agentDoTaskBuilders . add (doTaskBuilder );
7583 return this ;
7684 }
7785
@@ -83,7 +91,7 @@ public AgentWorkflowBuilder loop(
8391 String name , Predicate <AgenticScope > exitCondition , Object ... agents ) {
8492 final AgentDoTaskBuilder doTaskBuilder = this .newDo ();
8593 doTaskBuilder .loop (name , loop -> loop .subAgents (agents ).exitCondition (exitCondition ));
86- this . workflow . setDo (doTaskBuilder . build (). getDo () );
94+ agentDoTaskBuilders . add (doTaskBuilder );
8795 return this ;
8896 }
8997
@@ -96,4 +104,15 @@ protected AgentDoTaskBuilder newDo() {
96104 protected AgentWorkflowBuilder self () {
97105 return this ;
98106 }
107+
108+ @ Override
109+ public Workflow build () {
110+ List <TaskItem > items = new ArrayList <>(workflow .getDo ());
111+ this .agentDoTaskBuilders .stream ()
112+ .map (AgentDoTaskBuilder ::build )
113+ .forEach (b -> items .addAll (b .getDo ()));
114+ this .workflow .setDo (Collections .unmodifiableList (items ));
115+ this .agentDoTaskBuilders .clear ();
116+ return this .workflow ;
117+ }
99118}
0 commit comments