2121import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2222import io .reactivex .rxjava3 .core .Flowable ;
2323import java .util .List ;
24- import java . util . Optional ;
24+ import org . jspecify . annotations . Nullable ;
2525import org .slf4j .Logger ;
2626import org .slf4j .LoggerFactory ;
2727
3434public class LoopAgent extends BaseAgent {
3535 private static final Logger logger = LoggerFactory .getLogger (LoopAgent .class );
3636
37- private final Optional < Integer > maxIterations ;
37+ private final @ Nullable Integer maxIterations ;
3838
3939 /**
4040 * Constructor for LoopAgent.
@@ -50,7 +50,7 @@ private LoopAgent(
5050 String name ,
5151 String description ,
5252 List <? extends BaseAgent > subAgents ,
53- Optional < Integer > maxIterations ,
53+ @ Nullable Integer maxIterations ,
5454 List <Callbacks .BeforeAgentCallback > beforeAgentCallback ,
5555 List <Callbacks .AfterAgentCallback > afterAgentCallback ) {
5656
@@ -60,16 +60,10 @@ private LoopAgent(
6060
6161 /** Builder for {@link LoopAgent}. */
6262 public static class Builder extends BaseAgent .Builder <Builder > {
63- private Optional < Integer > maxIterations = Optional . empty () ;
63+ private @ Nullable Integer maxIterations ;
6464
6565 @ CanIgnoreReturnValue
66- public Builder maxIterations (int maxIterations ) {
67- this .maxIterations = Optional .of (maxIterations );
68- return this ;
69- }
70-
71- @ CanIgnoreReturnValue
72- public Builder maxIterations (Optional <Integer > maxIterations ) {
66+ public Builder maxIterations (@ Nullable Integer maxIterations ) {
7367 this .maxIterations = maxIterations ;
7468 return this ;
7569 }
@@ -124,7 +118,7 @@ protected Flowable<Event> runAsyncImpl(InvocationContext invocationContext) {
124118
125119 return Flowable .fromIterable (subAgents )
126120 .concatMap (subAgent -> subAgent .runAsync (invocationContext ))
127- .repeat (maxIterations . orElse ( Integer .MAX_VALUE ) )
121+ .repeat (maxIterations != null ? maxIterations : Integer .MAX_VALUE )
128122 .takeUntil (LoopAgent ::hasEscalateAction );
129123 }
130124
@@ -137,4 +131,8 @@ protected Flowable<Event> runLiveImpl(InvocationContext invocationContext) {
137131 private static boolean hasEscalateAction (Event event ) {
138132 return event .actions ().escalate ().orElse (false );
139133 }
134+
135+ public @ Nullable Integer maxIterations () {
136+ return maxIterations ;
137+ }
140138}
0 commit comments