Skip to content

Commit 5e4eaa4

Browse files
tilgalascopybara-github
authored andcommitted
refactor: remove use of Optional params in Contents class
PiperOrigin-RevId: 880784641
1 parent 82ef5ac commit 5e4eaa4

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

core/src/main/java/com/google/adk/flows/llmflows/Contents.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.adk.events.Event;
2626
import com.google.adk.events.EventCompaction;
2727
import com.google.adk.models.LlmRequest;
28+
import com.google.common.base.Strings;
2829
import com.google.common.collect.ImmutableList;
2930
import com.google.common.collect.Iterables;
3031
import com.google.common.collect.Lists;
@@ -41,6 +42,7 @@
4142
import java.util.Map;
4243
import java.util.Optional;
4344
import java.util.Set;
45+
import javax.annotation.Nullable;
4446

4547
/** {@link RequestProcessor} that populates content in request for LLM flows. */
4648
public final class Contents implements RequestProcessor {
@@ -68,7 +70,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
6870
request.toBuilder()
6971
.contents(
7072
getCurrentTurnContents(
71-
context.branch(),
73+
context.branch().orElse(null),
7274
context.session().events(),
7375
context.agent().name(),
7476
modelName))
@@ -78,7 +80,10 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
7880

7981
ImmutableList<Content> contents =
8082
getContents(
81-
context.branch(), context.session().events(), context.agent().name(), modelName);
83+
context.branch().orElse(null),
84+
context.session().events(),
85+
context.agent().name(),
86+
modelName);
8287

8388
return Single.just(
8489
RequestProcessor.RequestProcessingResult.create(
@@ -87,7 +92,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
8792

8893
/** Gets contents for the current turn only (no conversation history). */
8994
private ImmutableList<Content> getCurrentTurnContents(
90-
Optional<String> currentBranch, List<Event> events, String agentName, String modelName) {
95+
@Nullable String currentBranch, List<Event> events, String agentName, String modelName) {
9196
// Find the latest event that starts the current turn and process from there.
9297
for (int i = events.size() - 1; i >= 0; i--) {
9398
Event event = events.get(i);
@@ -99,7 +104,7 @@ private ImmutableList<Content> getCurrentTurnContents(
99104
}
100105

101106
private ImmutableList<Content> getContents(
102-
Optional<String> currentBranch, List<Event> events, String agentName, String modelName) {
107+
@Nullable String currentBranch, List<Event> events, String agentName, String modelName) {
103108
List<Event> filteredEvents = new ArrayList<>();
104109
boolean hasCompactEvent = false;
105110

@@ -414,16 +419,12 @@ private static String convertMapToJson(Map<String, Object> struct) {
414419
}
415420
}
416421

417-
private static boolean isEventBelongsToBranch(Optional<String> invocationBranchOpt, Event event) {
418-
Optional<String> eventBranchOpt = event.branch();
422+
private static boolean isEventBelongsToBranch(@Nullable String invocationBranch, Event event) {
423+
@Nullable String eventBranch = event.branch().orElse(null);
419424

420-
if (invocationBranchOpt.isEmpty() || invocationBranchOpt.get().isEmpty()) {
421-
return true;
422-
}
423-
if (eventBranchOpt.isEmpty() || eventBranchOpt.get().isEmpty()) {
424-
return true;
425-
}
426-
return invocationBranchOpt.get().startsWith(eventBranchOpt.get());
425+
return Strings.isNullOrEmpty(invocationBranch)
426+
|| Strings.isNullOrEmpty(eventBranch)
427+
|| invocationBranch.startsWith(eventBranch);
427428
}
428429

429430
/**

0 commit comments

Comments
 (0)