Skip to content

Commit 6a2669b

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: Replace Optional builder methods with @nullable
PiperOrigin-RevId: 877347079
1 parent 117fedf commit 6a2669b

1 file changed

Lines changed: 23 additions & 25 deletions

File tree

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ public class InvocationContext {
4343
private final BaseArtifactService artifactService;
4444
private final BaseMemoryService memoryService;
4545
private final Plugin pluginManager;
46-
private final Optional<LiveRequestQueue> liveRequestQueue;
46+
@Nullable private final LiveRequestQueue liveRequestQueue;
4747
private final Map<String, ActiveStreamingTool> activeStreamingTools;
4848
private final String invocationId;
4949
private final Session session;
50-
private final Optional<Content> userContent;
50+
@Nullable private final Content userContent;
5151
private final RunConfig runConfig;
5252
@Nullable private final EventsCompactionConfig eventsCompactionConfig;
5353
@Nullable private final ContextCacheConfig contextCacheConfig;
5454
private final InvocationCostManager invocationCostManager;
5555
private final Map<String, Object> callbackContextData;
5656

57-
private Optional<String> branch;
57+
@Nullable private String branch;
5858
private BaseAgent agent;
5959
private boolean endInvocation;
6060

@@ -153,10 +153,10 @@ public InvocationContext(
153153
+ ".invocationId(invocationId)"
154154
+ ".agent(agent)"
155155
+ ".session(session)"
156-
+ ".userContent(Optional.ofNullable(userContent))"
156+
+ ".userContent(userContent)"
157157
+ ".runConfig(runConfig)"
158158
+ ".build()",
159-
imports = {"com.google.adk.agents.InvocationContext", "java.util.Optional"})
159+
imports = {"com.google.adk.agents.InvocationContext"})
160160
@Deprecated(forRemoval = true)
161161
public static InvocationContext create(
162162
BaseSessionService sessionService,
@@ -172,7 +172,7 @@ public static InvocationContext create(
172172
.invocationId(invocationId)
173173
.agent(agent)
174174
.session(session)
175-
.userContent(Optional.ofNullable(userContent))
175+
.userContent(userContent)
176176
.runConfig(runConfig)
177177
.build();
178178
}
@@ -245,7 +245,7 @@ public Map<String, ActiveStreamingTool> activeStreamingTools() {
245245

246246
/** Returns the queue for managing live requests, if available for this invocation. */
247247
public Optional<LiveRequestQueue> liveRequestQueue() {
248-
return liveRequestQueue;
248+
return Optional.ofNullable(liveRequestQueue);
249249
}
250250

251251
/** Returns the unique ID for this invocation. */
@@ -258,15 +258,15 @@ public String invocationId() {
258258
* history.
259259
*/
260260
public void branch(@Nullable String branch) {
261-
this.branch = Optional.ofNullable(branch);
261+
this.branch = branch;
262262
}
263263

264264
/**
265265
* Returns the branch ID for the current invocation, if one is set. A branch represents a fork in
266266
* the conversation history.
267267
*/
268268
public Optional<String> branch() {
269-
return branch;
269+
return Optional.ofNullable(branch);
270270
}
271271

272272
/** Returns the agent being invoked. */
@@ -291,7 +291,7 @@ public Session session() {
291291

292292
/** Returns the user content that triggered this invocation, if any. */
293293
public Optional<Content> userContent() {
294-
return userContent;
294+
return Optional.ofNullable(userContent);
295295
}
296296

297297
/** Returns the configuration for the current agent run. */
@@ -416,13 +416,13 @@ private Builder(InvocationContext context) {
416416
private BaseArtifactService artifactService;
417417
private BaseMemoryService memoryService;
418418
private Plugin pluginManager = new PluginManager();
419-
private Optional<LiveRequestQueue> liveRequestQueue = Optional.empty();
419+
@Nullable private LiveRequestQueue liveRequestQueue = null;
420420
private Map<String, ActiveStreamingTool> activeStreamingTools = new ConcurrentHashMap<>();
421-
private Optional<String> branch = Optional.empty();
421+
@Nullable private String branch = null;
422422
private String invocationId = newInvocationContextId();
423423
private BaseAgent agent;
424424
private Session session;
425-
private Optional<Content> userContent = Optional.empty();
425+
@Nullable private Content userContent = null;
426426
private RunConfig runConfig = RunConfig.builder().build();
427427
private boolean endInvocation = false;
428428
@Nullable private EventsCompactionConfig eventsCompactionConfig;
@@ -489,7 +489,7 @@ public Builder pluginManager(Plugin pluginManager) {
489489
@Deprecated(forRemoval = true)
490490
@CanIgnoreReturnValue
491491
public Builder liveRequestQueue(Optional<LiveRequestQueue> liveRequestQueue) {
492-
this.liveRequestQueue = liveRequestQueue;
492+
this.liveRequestQueue = liveRequestQueue.orElse(null);
493493
return this;
494494
}
495495

@@ -501,7 +501,7 @@ public Builder liveRequestQueue(Optional<LiveRequestQueue> liveRequestQueue) {
501501
*/
502502
@CanIgnoreReturnValue
503503
public Builder liveRequestQueue(@Nullable LiveRequestQueue liveRequestQueue) {
504-
this.liveRequestQueue = Optional.ofNullable(liveRequestQueue);
504+
this.liveRequestQueue = liveRequestQueue;
505505
return this;
506506
}
507507

@@ -516,7 +516,7 @@ public Builder liveRequestQueue(@Nullable LiveRequestQueue liveRequestQueue) {
516516
@Deprecated(forRemoval = true)
517517
@CanIgnoreReturnValue
518518
public Builder branch(Optional<String> branch) {
519-
this.branch = branch;
519+
this.branch = branch.orElse(null);
520520
return this;
521521
}
522522

@@ -527,8 +527,8 @@ public Builder branch(Optional<String> branch) {
527527
* @return this builder instance for chaining.
528528
*/
529529
@CanIgnoreReturnValue
530-
public Builder branch(String branch) {
531-
this.branch = Optional.of(branch);
530+
public Builder branch(@Nullable String branch) {
531+
this.branch = branch;
532532
return this;
533533
}
534534

@@ -569,14 +569,12 @@ public Builder session(Session session) {
569569
}
570570

571571
/**
572-
* Sets the user content that triggered this invocation.
573-
*
574-
* @param userContent the user content that triggered this invocation.
575-
* @return this builder instance for chaining.
572+
* @deprecated Use {@link #userContent(Content)} instead.
576573
*/
577574
@CanIgnoreReturnValue
575+
@Deprecated
578576
public Builder userContent(Optional<Content> userContent) {
579-
this.userContent = userContent;
577+
this.userContent = userContent.orElse(null);
580578
return this;
581579
}
582580

@@ -587,8 +585,8 @@ public Builder userContent(Optional<Content> userContent) {
587585
* @return this builder instance for chaining.
588586
*/
589587
@CanIgnoreReturnValue
590-
public Builder userContent(Content userContent) {
591-
this.userContent = Optional.of(userContent);
588+
public Builder userContent(@Nullable Content userContent) {
589+
this.userContent = userContent;
592590
return this;
593591
}
594592

0 commit comments

Comments
 (0)