Skip to content

Commit b7e47b0

Browse files
google-genai-botcopybara-github
authored andcommitted
test: Fixing the AgentWithMemoryTest
Using the non-deprecated version of runAsync PiperOrigin-RevId: 834291199
1 parent 82fbdac commit b7e47b0

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

core/src/main/java/com/google/adk/memory/InMemoryMemoryService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public Completable addSessionToMemory(Session session) {
7070
session.events().stream()
7171
.filter(
7272
event ->
73-
event.content().isPresent()
74-
&& event.content().get().parts().isPresent()
75-
&& !event.content().get().parts().get().isEmpty())
73+
event
74+
.content()
75+
.flatMap(c -> c.parts())
76+
.filter(parts -> !parts.isEmpty())
77+
.isPresent())
7678
.collect(toImmutableList());
7779
userSessions.put(session.id(), nonEmptyEvents);
7880
});

core/src/test/java/com/google/adk/agents/AgentWithMemoryTest.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
public final class AgentWithMemoryTest {
4141
@Test
4242
public void agentRemembersUserNameWithMemoryTool() throws Exception {
43+
String userId = "test-user";
44+
String agentName = "test-agent";
45+
4346
Part functionCall =
4447
Part.builder()
4548
.functionCall(
@@ -56,13 +59,13 @@ public void agentRemembersUserNameWithMemoryTool() throws Exception {
5659
.content(
5760
Content.builder()
5861
.parts(Part.fromText("OK, I'll remember that."))
59-
.role("test-agent")
62+
.role("model")
6063
.build())
6164
.build(),
6265
LlmResponse.builder()
6366
.content(
6467
Content.builder()
65-
.role("test-agent")
68+
.role("model")
6669
.parts(ImmutableList.of(functionCall))
6770
.build())
6871
.build(),
@@ -72,30 +75,44 @@ public void agentRemembersUserNameWithMemoryTool() throws Exception {
7275
// we won't actually read the name from here since that'd be
7376
// cheating.
7477
.parts(Part.fromText("Your name is James."))
75-
.role("test-agent")
78+
.role("model")
7679
.build())
7780
.build()));
7881

7982
LlmAgent agent =
8083
LlmAgent.builder()
81-
.name("test-agent")
84+
.name(agentName)
8285
.model(testLlm)
8386
.tools(ImmutableList.of(new LoadMemoryTool()))
8487
.build();
8588

8689
InMemoryRunner runner = new InMemoryRunner(agent);
87-
Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet();
90+
String sessionId = runner.sessionService().createSession(agentName, userId).blockingGet().id();
8891

8992
Content firstMessage = Content.fromParts(Part.fromText("My name is James"));
9093

9194
var unused =
92-
runner.runAsync(session, firstMessage, RunConfig.builder().build()).toList().blockingGet();
93-
// Save the session so we can bring it up on the next request.
94-
runner.memoryService().addSessionToMemory(session).blockingAwait();
95+
runner
96+
.runAsync(userId, sessionId, firstMessage, RunConfig.builder().build())
97+
.toList()
98+
.blockingGet();
99+
100+
// Retrieve the updated session after the first runAsync
101+
Session updatedSession =
102+
runner
103+
.sessionService()
104+
.getSession("test-agent", userId, sessionId, Optional.empty())
105+
.blockingGet();
106+
107+
// Save the updated session to memory so we can bring it up on the next request.
108+
runner.memoryService().addSessionToMemory(updatedSession).blockingAwait();
95109

96110
Content secondMessage = Content.fromParts(Part.fromText("what is my name?"));
97111
unused =
98-
runner.runAsync(session, secondMessage, RunConfig.builder().build()).toList().blockingGet();
112+
runner
113+
.runAsync(userId, updatedSession.id(), secondMessage, RunConfig.builder().build())
114+
.toList()
115+
.blockingGet();
99116

100117
// Verify that the tool's response was included in the next LLM call.
101118
LlmRequest lastRequest = testLlm.getLastRequest();

0 commit comments

Comments
 (0)