Skip to content

Commit 8f5d8aa

Browse files
authored
Merge branch 'main' into fix/memory-leak-delete-session
2 parents e8e18ac + d899f6f commit 8f5d8aa

9 files changed

Lines changed: 361 additions & 39 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

core/src/main/java/com/google/adk/artifacts/BaseArtifactService.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.adk.artifacts;
1818

19+
import com.google.adk.sessions.SessionKey;
1920
import com.google.common.collect.ImmutableList;
2021
import com.google.genai.types.Part;
2122
import io.reactivex.rxjava3.core.Completable;
@@ -39,6 +40,12 @@ public interface BaseArtifactService {
3940
Single<Integer> saveArtifact(
4041
String appName, String userId, String sessionId, String filename, Part artifact);
4142

43+
/** Saves an artifact. */
44+
default Single<Integer> saveArtifact(SessionKey sessionKey, String filename, Part artifact) {
45+
return saveArtifact(
46+
sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename, artifact);
47+
}
48+
4249
/**
4350
* Saves an artifact and returns it with fileData if available.
4451
*
@@ -58,18 +65,35 @@ default Single<Part> saveAndReloadArtifact(
5865
.flatMap(version -> loadArtifact(appName, userId, sessionId, filename, version).toSingle());
5966
}
6067

68+
/** Saves an artifact and returns it with fileData if available. */
69+
default Single<Part> saveAndReloadArtifact(
70+
SessionKey sessionKey, String filename, Part artifact) {
71+
return saveAndReloadArtifact(
72+
sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename, artifact);
73+
}
74+
6175
/** Loads the latest version of an artifact from the service. */
6276
default Maybe<Part> loadArtifact(
6377
String appName, String userId, String sessionId, String filename) {
6478
return loadArtifact(appName, userId, sessionId, filename, Optional.empty());
6579
}
6680

81+
/** Loads the latest version of an artifact from the service. */
82+
default Maybe<Part> loadArtifact(SessionKey sessionKey, String filename) {
83+
return loadArtifact(sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename);
84+
}
85+
6786
/** Loads a specific version of an artifact from the service. */
6887
default Maybe<Part> loadArtifact(
6988
String appName, String userId, String sessionId, String filename, int version) {
7089
return loadArtifact(appName, userId, sessionId, filename, Optional.of(version));
7190
}
7291

92+
default Maybe<Part> loadArtifact(SessionKey sessionKey, String filename, int version) {
93+
return loadArtifact(
94+
sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename, version);
95+
}
96+
7397
/**
7498
* @deprecated Use {@link #loadArtifact(String, String, String, String)} or {@link
7599
* #loadArtifact(String, String, String, String, int)} instead.
@@ -88,6 +112,10 @@ Maybe<Part> loadArtifact(
88112
*/
89113
Single<ListArtifactsResponse> listArtifactKeys(String appName, String userId, String sessionId);
90114

115+
default Single<ListArtifactsResponse> listArtifactKeys(SessionKey sessionKey) {
116+
return listArtifactKeys(sessionKey.appName(), sessionKey.userId(), sessionKey.id());
117+
}
118+
91119
/**
92120
* Deletes an artifact.
93121
*
@@ -98,6 +126,10 @@ Maybe<Part> loadArtifact(
98126
*/
99127
Completable deleteArtifact(String appName, String userId, String sessionId, String filename);
100128

129+
default Completable deleteArtifact(SessionKey sessionKey, String filename) {
130+
return deleteArtifact(sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename);
131+
}
132+
101133
/**
102134
* Lists all the versions (as revision IDs) of an artifact.
103135
*
@@ -109,4 +141,8 @@ Maybe<Part> loadArtifact(
109141
*/
110142
Single<ImmutableList<Integer>> listVersions(
111143
String appName, String userId, String sessionId, String filename);
144+
145+
default Single<ImmutableList<Integer>> listVersions(SessionKey sessionKey, String filename) {
146+
return listVersions(sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename);
147+
}
112148
}

core/src/main/java/com/google/adk/runner/Runner.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.adk.sessions.BaseSessionService;
3636
import com.google.adk.sessions.InMemorySessionService;
3737
import com.google.adk.sessions.Session;
38+
import com.google.adk.sessions.SessionKey;
3839
import com.google.adk.summarizer.EventsCompactionConfig;
3940
import com.google.adk.summarizer.LlmEventSummarizer;
4041
import com.google.adk.summarizer.SlidingWindowEventCompactor;
@@ -383,6 +384,25 @@ public Flowable<Event> runAsync(
383384
.flatMapPublisher(session -> this.runAsyncImpl(session, newMessage, runConfig, stateDelta));
384385
}
385386

387+
/** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */
388+
public Flowable<Event> runAsync(
389+
SessionKey sessionKey,
390+
Content newMessage,
391+
RunConfig runConfig,
392+
@Nullable Map<String, Object> stateDelta) {
393+
return runAsync(sessionKey.userId(), sessionKey.id(), newMessage, runConfig, stateDelta);
394+
}
395+
396+
/** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */
397+
public Flowable<Event> runAsync(SessionKey sessionKey, Content newMessage, RunConfig runConfig) {
398+
return runAsync(sessionKey, newMessage, runConfig, /* stateDelta= */ null);
399+
}
400+
401+
/** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */
402+
public Flowable<Event> runAsync(SessionKey sessionKey, Content newMessage) {
403+
return runAsync(sessionKey, newMessage, RunConfig.builder().build());
404+
}
405+
386406
/** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */
387407
public Flowable<Event> runAsync(String userId, String sessionId, Content newMessage) {
388408
return runAsync(userId, sessionId, newMessage, RunConfig.builder().build());
@@ -671,6 +691,17 @@ public Flowable<Event> runLive(
671691
.flatMapPublisher(session -> this.runLive(session, liveRequestQueue, runConfig));
672692
}
673693

694+
/**
695+
* Retrieves the session and runs the agent in live mode.
696+
*
697+
* @return stream of events from the agent.
698+
* @throws IllegalArgumentException if the session is not found.
699+
*/
700+
public Flowable<Event> runLive(
701+
SessionKey sessionKey, LiveRequestQueue liveRequestQueue, RunConfig runConfig) {
702+
return runLive(sessionKey.userId(), sessionKey.id(), liveRequestQueue, runConfig);
703+
}
704+
674705
/**
675706
* Runs the agent asynchronously with a default user ID.
676707
*

0 commit comments

Comments
 (0)