4040public 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