Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 58 additions & 18 deletions core/src/main/java/com/google/adk/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.adk.utils.CollectionUtils;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.MapMaker;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.types.AudioTranscriptionConfig;
import com.google.genai.types.Content;
Expand All @@ -57,13 +58,15 @@
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.subjects.CompletableSubject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.jspecify.annotations.Nullable;

/** The main class for the GenAI Agents runner. */
Expand All @@ -76,6 +79,8 @@ public class Runner {
private final PluginManager pluginManager;
@Nullable private final EventsCompactionConfig eventsCompactionConfig;
@Nullable private final ContextCacheConfig contextCacheConfig;
private final ConcurrentMap<String, Completable> activeSessionCompletables =
new MapMaker().weakValues().makeMap();

/** Builder for {@link Runner}. */
public static class Builder {
Expand Down Expand Up @@ -380,25 +385,57 @@ public Flowable<Event> runAsync(
Content newMessage,
RunConfig runConfig,
@Nullable Map<String, Object> stateDelta) {
Flowable<Event> result =
Flowable.defer(
() ->
this.sessionService
.getSession(appName, userId, sessionId, Optional.empty())
.switchIfEmpty(
Single.defer(
() -> {
if (runConfig.autoCreateSession()) {
return this.sessionService.createSession(
appName, userId, (Map<String, Object>) null, sessionId);
}
return Single.error(
new IllegalArgumentException(
String.format(
"Session not found: %s for user %s",
sessionId, userId)));
}))
.flatMapPublisher(
session ->
this.runAsyncImpl(session, newMessage, runConfig, stateDelta)))
.compose(Tracing.trace("invocation"));

return Flowable.defer(
() ->
this.sessionService
.getSession(appName, userId, sessionId, Optional.empty())
.switchIfEmpty(
Single.defer(
() -> {
if (runConfig.autoCreateSession()) {
return this.sessionService.createSession(
appName, userId, (Map<String, Object>) null, sessionId);
}
return Single.error(
new IllegalArgumentException(
String.format(
"Session not found: %s for user %s", sessionId, userId)));
}))
.flatMapPublisher(
session -> this.runAsyncImpl(session, newMessage, runConfig, stateDelta)))
.compose(Tracing.trace("invocation"));
() -> {
if (sessionId == null) {
return result;
}

CompletableSubject requestCompletion = CompletableSubject.create();

Completable[] previousHolder = new Completable[1];

activeSessionCompletables.compute(
sessionId,
(key, current) -> {
previousHolder[0] = current;
return requestCompletion;
});

Completable previous = previousHolder[0];

Flowable<Event> sequenced =
(previous == null) ? result : previous.onErrorComplete().andThen(result);

return sequenced.doFinally(
() -> {
requestCompletion.onComplete();
activeSessionCompletables.remove(sessionId, requestCompletion);
});
});
}

/** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */
Expand Down Expand Up @@ -740,6 +777,9 @@ private BaseAgent findAgentToRun(Session session, BaseAgent rootAgent) {

for (Event event : events) {
String author = event.author();
if (author == null) {
continue;
}
if (author.equals("user")) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/adk/sessions/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Builder userId(String userId) {
@CanIgnoreReturnValue
@JsonProperty("events")
public Builder events(List<Event> events) {
this.events = Collections.synchronizedList(events);
this.events = Collections.synchronizedList(new ArrayList<>(events));
return this;
}

Expand Down
Loading
Loading