Skip to content

Commit 77e93be

Browse files
tilgalascopybara-github
authored andcommitted
feat!: update LoopAgent's maxIteration field and methods to be @nullable instead of Optional
PiperOrigin-RevId: 881457987
1 parent c6fdb63 commit 77e93be

2 files changed

Lines changed: 11 additions & 18 deletions

File tree

core/src/main/java/com/google/adk/agents/LoopAgent.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2222
import io.reactivex.rxjava3.core.Flowable;
2323
import java.util.List;
24-
import java.util.Optional;
24+
import org.jspecify.annotations.Nullable;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
2727

@@ -34,7 +34,7 @@
3434
public 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
}

core/src/test/java/com/google/adk/agents/LoopAgentTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import io.reactivex.rxjava3.core.Flowable;
3434
import io.reactivex.rxjava3.core.Maybe;
3535
import java.util.List;
36-
import java.util.Optional;
3736
import java.util.concurrent.atomic.AtomicInteger;
3837
import org.junit.Test;
3938
import org.junit.runner.RunWith;
@@ -165,11 +164,7 @@ public void runAsync_withNoMaxIterations_keepsLooping() {
165164
Event event2 = createEvent("event2");
166165
TestBaseAgent subAgent = createSubAgent("subAgent", () -> Flowable.just(event1, event2));
167166
LoopAgent loopAgent =
168-
LoopAgent.builder()
169-
.name("loopAgent")
170-
.subAgents(ImmutableList.of(subAgent))
171-
.maxIterations(Optional.empty())
172-
.build();
167+
LoopAgent.builder().name("loopAgent").subAgents(ImmutableList.of(subAgent)).build();
173168
InvocationContext invocationContext = createInvocationContext(loopAgent);
174169
Iterable<Event> result = loopAgent.runAsync(invocationContext).blockingIterable();
175170

0 commit comments

Comments
 (0)