From 78893d07cd3391d960b5d945e53ca11c98910f38 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Thu, 26 Feb 2026 11:28:47 -0800 Subject: [PATCH] refactor: Replace Optional builder methods with @Nullable PiperOrigin-RevId: 875812636 --- .../google/adk/agents/InvocationContext.java | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/InvocationContext.java b/core/src/main/java/com/google/adk/agents/InvocationContext.java index 6457a8ca4..89c90b9fe 100644 --- a/core/src/main/java/com/google/adk/agents/InvocationContext.java +++ b/core/src/main/java/com/google/adk/agents/InvocationContext.java @@ -43,18 +43,18 @@ public class InvocationContext { private final BaseArtifactService artifactService; private final BaseMemoryService memoryService; private final Plugin pluginManager; - private final Optional liveRequestQueue; + @Nullable private final LiveRequestQueue liveRequestQueue; private final Map activeStreamingTools; private final String invocationId; private final Session session; - private final Optional userContent; + @Nullable private final Content userContent; private final RunConfig runConfig; @Nullable private final EventsCompactionConfig eventsCompactionConfig; @Nullable private final ContextCacheConfig contextCacheConfig; private final InvocationCostManager invocationCostManager; private final Map callbackContextData; - private Optional branch; + @Nullable private String branch; private BaseAgent agent; private boolean endInvocation; @@ -153,10 +153,10 @@ public InvocationContext( + ".invocationId(invocationId)" + ".agent(agent)" + ".session(session)" - + ".userContent(Optional.ofNullable(userContent))" + + ".userContent(userContent)" + ".runConfig(runConfig)" + ".build()", - imports = {"com.google.adk.agents.InvocationContext", "java.util.Optional"}) + imports = {"com.google.adk.agents.InvocationContext"}) @Deprecated(forRemoval = true) public static InvocationContext create( BaseSessionService sessionService, @@ -172,7 +172,7 @@ public static InvocationContext create( .invocationId(invocationId) .agent(agent) .session(session) - .userContent(Optional.ofNullable(userContent)) + .userContent(userContent) .runConfig(runConfig) .build(); } @@ -245,7 +245,7 @@ public Map activeStreamingTools() { /** Returns the queue for managing live requests, if available for this invocation. */ public Optional liveRequestQueue() { - return liveRequestQueue; + return Optional.ofNullable(liveRequestQueue); } /** Returns the unique ID for this invocation. */ @@ -258,7 +258,7 @@ public String invocationId() { * history. */ public void branch(@Nullable String branch) { - this.branch = Optional.ofNullable(branch); + this.branch = branch; } /** @@ -266,7 +266,7 @@ public void branch(@Nullable String branch) { * the conversation history. */ public Optional branch() { - return branch; + return Optional.ofNullable(branch); } /** Returns the agent being invoked. */ @@ -291,7 +291,7 @@ public Session session() { /** Returns the user content that triggered this invocation, if any. */ public Optional userContent() { - return userContent; + return Optional.ofNullable(userContent); } /** Returns the configuration for the current agent run. */ @@ -416,13 +416,13 @@ private Builder(InvocationContext context) { private BaseArtifactService artifactService; private BaseMemoryService memoryService; private Plugin pluginManager = new PluginManager(); - private Optional liveRequestQueue = Optional.empty(); + @Nullable private LiveRequestQueue liveRequestQueue = null; private Map activeStreamingTools = new ConcurrentHashMap<>(); - private Optional branch = Optional.empty(); + @Nullable private String branch = null; private String invocationId = newInvocationContextId(); private BaseAgent agent; private Session session; - private Optional userContent = Optional.empty(); + @Nullable private Content userContent = null; private RunConfig runConfig = RunConfig.builder().build(); private boolean endInvocation = false; @Nullable private EventsCompactionConfig eventsCompactionConfig; @@ -489,7 +489,7 @@ public Builder pluginManager(Plugin pluginManager) { @Deprecated(forRemoval = true) @CanIgnoreReturnValue public Builder liveRequestQueue(Optional liveRequestQueue) { - this.liveRequestQueue = liveRequestQueue; + this.liveRequestQueue = liveRequestQueue.orElse(null); return this; } @@ -501,7 +501,7 @@ public Builder liveRequestQueue(Optional liveRequestQueue) { */ @CanIgnoreReturnValue public Builder liveRequestQueue(@Nullable LiveRequestQueue liveRequestQueue) { - this.liveRequestQueue = Optional.ofNullable(liveRequestQueue); + this.liveRequestQueue = liveRequestQueue; return this; } @@ -516,7 +516,7 @@ public Builder liveRequestQueue(@Nullable LiveRequestQueue liveRequestQueue) { @Deprecated(forRemoval = true) @CanIgnoreReturnValue public Builder branch(Optional branch) { - this.branch = branch; + this.branch = branch.orElse(null); return this; } @@ -527,8 +527,8 @@ public Builder branch(Optional branch) { * @return this builder instance for chaining. */ @CanIgnoreReturnValue - public Builder branch(String branch) { - this.branch = Optional.of(branch); + public Builder branch(@Nullable String branch) { + this.branch = branch; return this; } @@ -569,14 +569,12 @@ public Builder session(Session session) { } /** - * Sets the user content that triggered this invocation. - * - * @param userContent the user content that triggered this invocation. - * @return this builder instance for chaining. + * @deprecated Use {@link #userContent(Content)} instead. */ @CanIgnoreReturnValue + @Deprecated public Builder userContent(Optional userContent) { - this.userContent = userContent; + this.userContent = userContent.orElse(null); return this; } @@ -587,8 +585,8 @@ public Builder userContent(Optional userContent) { * @return this builder instance for chaining. */ @CanIgnoreReturnValue - public Builder userContent(Content userContent) { - this.userContent = Optional.of(userContent); + public Builder userContent(@Nullable Content userContent) { + this.userContent = userContent; return this; }