diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index 8c42b0b90..89e8e3c15 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -791,7 +791,8 @@ protected Flowable runLiveImpl( updatedInvocationContext .agent() .runLive(updatedInvocationContext) - .doOnNext(event -> this.sessionService.appendEvent(session, event))) + .concatMapSingle( + event -> this.sessionService.appendEvent(session, event))) .doOnError( throwable -> { Span span = Span.current(); diff --git a/core/src/test/java/com/google/adk/runner/RunnerTest.java b/core/src/test/java/com/google/adk/runner/RunnerTest.java index 5b1f516d9..d28bd220b 100644 --- a/core/src/test/java/com/google/adk/runner/RunnerTest.java +++ b/core/src/test/java/com/google/adk/runner/RunnerTest.java @@ -1620,6 +1620,44 @@ public void runLive_success() throws Exception { assertThat(simplifyEvents(testSubscriber.values())).containsExactly("test agent: from llm"); } + @Test + public void runLive_asyncSessionService_persistsEvents() throws Exception { + BaseSessionService asyncSessionService = + new AppendDelayingSessionService(new InMemorySessionService(), 10); + Runner runnerWithAsyncSession = + Runner.builder() + .app(App.builder().name("test").rootAgent(agent).build()) + .sessionService(asyncSessionService) + .build(); + Session asyncSession = + runnerWithAsyncSession.sessionService().createSession("test", "user").blockingGet(); + + LiveRequestQueue liveRequestQueue = new LiveRequestQueue(); + TestSubscriber testSubscriber = + runnerWithAsyncSession + .runLive(asyncSession, liveRequestQueue, RunConfig.builder().build()) + .test(); + + liveRequestQueue.content(createContent("from user")); + liveRequestQueue.close(); + + testSubscriber.await(); + testSubscriber.assertComplete(); + assertThat(simplifyEvents(testSubscriber.values())).containsExactly("test agent: from llm"); + + // Verify that the events are successfully persisted to session history. + ImmutableList history = + runnerWithAsyncSession + .sessionService() + .listEvents("test", "user", asyncSession.id()) + .blockingGet() + .events(); + // The history should contain only the agent response event (user messages in liveQueue are not + // persisted). + assertThat(history).hasSize(1); + assertThat(history.get(0).author()).isEqualTo("test agent"); + } + @Test public void runLive_withSessionKey_success() throws Exception { LiveRequestQueue liveRequestQueue = new LiveRequestQueue();