Class ThreadSessionManager
(userId, threadId) for server-side memory.
When server-side memory is enabled, the same agent instance is reused for requests with the same user and thread, preserving conversation history across requests. Sessions for different users never share a slot, even when they reuse a thread id.
Usage:
ThreadSessionManager manager = new ThreadSessionManager(1000, 30);
Agent agent =
manager.getOrCreateAgent(
"user-1", "thread-123", "default", () -> createAgent());
boolean hasMemory =
manager.hasMemory(
RuntimeContext.builder()
.userId("user-1")
.sessionId("thread-123")
.build());
manager.cleanupExpiredSessions();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a thread session with its agent and metadata. -
Constructor Summary
ConstructorsConstructorDescriptionThreadSessionManager(int maxSessions, int sessionTimeoutMinutes) Creates a new ThreadSessionManager. -
Method Summary
Modifier and TypeMethodDescriptionvoidClean up sessions that have been inactive for longer than the timeout.voidclear()Clear all sessions.ensureSession(String userId, String threadId, String agentId, String name, Supplier<io.agentscope.core.agent.Agent> agentFactory) Ensure a session exists for the given user and thread, creating one if needed.ensureSession(String threadId, String agentId, String name, Supplier<io.agentscope.core.agent.Agent> agentFactory) Ensure a session exists for the given threadId under the anonymous user slot.io.agentscope.core.agent.AgentgetOrCreateAgent(String userId, String threadId, String agentId, Supplier<io.agentscope.core.agent.Agent> agentFactory) Get or create an agent for the given user and thread.io.agentscope.core.agent.AgentgetOrCreateAgent(String threadId, String agentId, Supplier<io.agentscope.core.agent.Agent> agentFactory) Get or create an agent for the given threadId under the anonymous user slot.getSession(String threadId) Get the session for a threadId under the anonymous user slot if it exists.getSession(String userId, String threadId) Get the session for a user and thread if it exists.intGet the current number of active sessions.Returns an unmodifiable snapshot of all sessions keyed by threadId.booleanhasMemory(io.agentscope.core.agent.RuntimeContext runtimeContext) Check if a session exists and has memory for the given runtime context.booleanremoveSession(String threadId) Remove a session by threadId under the anonymous user slot.booleanremoveSession(String userId, String threadId) Remove a session by user and thread.
-
Constructor Details
-
ThreadSessionManager
public ThreadSessionManager(int maxSessions, int sessionTimeoutMinutes) Creates a new ThreadSessionManager.- Parameters:
maxSessions- Maximum number of sessions to maintainsessionTimeoutMinutes- AgentStateStore timeout in minutes (0 = no timeout)
-
-
Method Details
-
getOrCreateAgent
public io.agentscope.core.agent.Agent getOrCreateAgent(String threadId, String agentId, Supplier<io.agentscope.core.agent.Agent> agentFactory) Get or create an agent for the given threadId under the anonymous user slot.- Parameters:
threadId- The thread identifieragentId- The agent type identifieragentFactory- Factory to create new agents if needed- Returns:
- The agent for this thread
-
getOrCreateAgent
public io.agentscope.core.agent.Agent getOrCreateAgent(String userId, String threadId, String agentId, Supplier<io.agentscope.core.agent.Agent> agentFactory) Get or create an agent for the given user and thread.This method is thread-safe. Concurrent requests for the same
(userId, threadId)share the same agent instance.- Parameters:
userId- The user identifier, may benullfor anonymousthreadId- The thread identifieragentId- The agent type identifieragentFactory- Factory to create new agents if needed- Returns:
- The agent for this user and thread
-
ensureSession
public ThreadSessionManager.ThreadSession ensureSession(String threadId, String agentId, String name, Supplier<io.agentscope.core.agent.Agent> agentFactory) Ensure a session exists for the given threadId under the anonymous user slot.- Parameters:
threadId- The thread identifieragentId- The agent type identifiername- Display name for the thread (may be null)agentFactory- Factory to create new agents if needed- Returns:
- The session for this thread
-
ensureSession
public ThreadSessionManager.ThreadSession ensureSession(String userId, String threadId, String agentId, String name, Supplier<io.agentscope.core.agent.Agent> agentFactory) Ensure a session exists for the given user and thread, creating one if needed.- Parameters:
userId- The user identifier, may benullfor anonymousthreadId- The thread identifieragentId- The agent type identifiername- Display name for the thread (may be null)agentFactory- Factory to create new agents if needed- Returns:
- The session for this user and thread
-
hasMemory
public boolean hasMemory(io.agentscope.core.agent.RuntimeContext runtimeContext) Check if a session exists and has memory for the given runtime context.The session is keyed by
RuntimeContext.getUserId()andRuntimeContext.getSessionId(). When the agent is a harness wrapper, the innerReActAgentis inspected without closing it.- Parameters:
runtimeContext- The runtime context identifying the user and thread- Returns:
- true if the session exists and the agent has non-empty memory
-
getSession
Get the session for a threadId under the anonymous user slot if it exists.- Parameters:
threadId- The thread identifier- Returns:
- Optional containing the session, or empty if not found
-
getSession
Get the session for a user and thread if it exists.- Parameters:
userId- The user identifier, may benullfor anonymousthreadId- The thread identifier- Returns:
- Optional containing the session, or empty if not found
-
getSessions
Returns an unmodifiable snapshot of all sessions keyed by threadId.When two users share a thread id, the later entry in iteration order overwrites the snapshot. Use
ThreadSessionManager.ThreadSession.getUserId()andThreadSessionManager.ThreadSession.getThreadId()on each value for tenant-safe identity.- Returns:
- session snapshot
-
removeSession
Remove a session by threadId under the anonymous user slot.- Parameters:
threadId- The thread identifier- Returns:
- true if a session was removed
-
removeSession
Remove a session by user and thread.- Parameters:
userId- The user identifier, may benullfor anonymousthreadId- The thread identifier- Returns:
- true if a session was removed
-
cleanupExpiredSessions
public void cleanupExpiredSessions()Clean up sessions that have been inactive for longer than the timeout. -
getSessionCount
public int getSessionCount()Get the current number of active sessions.- Returns:
- Number of sessions
-
clear
public void clear()Clear all sessions.
-