Skip to content

Commit fe6c683

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: subscribe to appendEvent Single in runLive
PiperOrigin-RevId: 943748488
1 parent e6b3517 commit fe6c683

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,8 @@ protected Flowable<Event> runLiveImpl(
791791
updatedInvocationContext
792792
.agent()
793793
.runLive(updatedInvocationContext)
794-
.doOnNext(event -> this.sessionService.appendEvent(session, event)))
794+
.concatMapSingle(
795+
event -> this.sessionService.appendEvent(session, event)))
795796
.doOnError(
796797
throwable -> {
797798
Span span = Span.current();

core/src/test/java/com/google/adk/runner/RunnerTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,44 @@ public void runLive_success() throws Exception {
16201620
assertThat(simplifyEvents(testSubscriber.values())).containsExactly("test agent: from llm");
16211621
}
16221622

1623+
@Test
1624+
public void runLive_asyncSessionService_persistsEvents() throws Exception {
1625+
BaseSessionService asyncSessionService =
1626+
new AppendDelayingSessionService(new InMemorySessionService(), 10);
1627+
Runner runnerWithAsyncSession =
1628+
Runner.builder()
1629+
.app(App.builder().name("test").rootAgent(agent).build())
1630+
.sessionService(asyncSessionService)
1631+
.build();
1632+
Session asyncSession =
1633+
runnerWithAsyncSession.sessionService().createSession("test", "user").blockingGet();
1634+
1635+
LiveRequestQueue liveRequestQueue = new LiveRequestQueue();
1636+
TestSubscriber<Event> testSubscriber =
1637+
runnerWithAsyncSession
1638+
.runLive(asyncSession, liveRequestQueue, RunConfig.builder().build())
1639+
.test();
1640+
1641+
liveRequestQueue.content(createContent("from user"));
1642+
liveRequestQueue.close();
1643+
1644+
testSubscriber.await();
1645+
testSubscriber.assertComplete();
1646+
assertThat(simplifyEvents(testSubscriber.values())).containsExactly("test agent: from llm");
1647+
1648+
// Verify that the events are successfully persisted to session history.
1649+
ImmutableList<Event> history =
1650+
runnerWithAsyncSession
1651+
.sessionService()
1652+
.listEvents("test", "user", asyncSession.id())
1653+
.blockingGet()
1654+
.events();
1655+
// The history should contain only the agent response event (user messages in liveQueue are not
1656+
// persisted).
1657+
assertThat(history).hasSize(1);
1658+
assertThat(history.get(0).author()).isEqualTo("test agent");
1659+
}
1660+
16231661
@Test
16241662
public void runLive_withSessionKey_success() throws Exception {
16251663
LiveRequestQueue liveRequestQueue = new LiveRequestQueue();

0 commit comments

Comments
 (0)