From c8881b77fb35d2b5735d702b40943f79e064c5a1 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Wed, 4 Mar 2026 08:44:32 -0800 Subject: [PATCH] refactor: remove use of Optional params in Contents class PiperOrigin-RevId: 878510506 --- .../google/adk/flows/llmflows/Contents.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Contents.java b/core/src/main/java/com/google/adk/flows/llmflows/Contents.java index a770808d4..13e0cdd0a 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Contents.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Contents.java @@ -25,6 +25,7 @@ import com.google.adk.events.Event; import com.google.adk.events.EventCompaction; import com.google.adk.models.LlmRequest; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -41,6 +42,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import javax.annotation.Nullable; /** {@link RequestProcessor} that populates content in request for LLM flows. */ public final class Contents implements RequestProcessor { @@ -68,7 +70,7 @@ public Single processRequest( request.toBuilder() .contents( getCurrentTurnContents( - context.branch(), + context.branch().orElse(null), context.session().events(), context.agent().name(), modelName)) @@ -78,7 +80,10 @@ public Single processRequest( ImmutableList contents = getContents( - context.branch(), context.session().events(), context.agent().name(), modelName); + context.branch().orElse(null), + context.session().events(), + context.agent().name(), + modelName); return Single.just( RequestProcessor.RequestProcessingResult.create( @@ -87,7 +92,7 @@ public Single processRequest( /** Gets contents for the current turn only (no conversation history). */ private ImmutableList getCurrentTurnContents( - Optional currentBranch, List events, String agentName, String modelName) { + @Nullable String currentBranch, List events, String agentName, String modelName) { // Find the latest event that starts the current turn and process from there. for (int i = events.size() - 1; i >= 0; i--) { Event event = events.get(i); @@ -99,7 +104,7 @@ private ImmutableList getCurrentTurnContents( } private ImmutableList getContents( - Optional currentBranch, List events, String agentName, String modelName) { + @Nullable String currentBranch, List events, String agentName, String modelName) { List filteredEvents = new ArrayList<>(); boolean hasCompactEvent = false; @@ -414,16 +419,12 @@ private static String convertMapToJson(Map struct) { } } - private static boolean isEventBelongsToBranch(Optional invocationBranchOpt, Event event) { - Optional eventBranchOpt = event.branch(); + private static boolean isEventBelongsToBranch(@Nullable String invocationBranch, Event event) { + @Nullable String eventBranch = event.branch().orElse(null); - if (invocationBranchOpt.isEmpty() || invocationBranchOpt.get().isEmpty()) { - return true; - } - if (eventBranchOpt.isEmpty() || eventBranchOpt.get().isEmpty()) { - return true; - } - return invocationBranchOpt.get().startsWith(eventBranchOpt.get()); + return Strings.isNullOrEmpty(invocationBranch) + || Strings.isNullOrEmpty(eventBranch) + || invocationBranch.startsWith(eventBranch); } /**