Class ThreadSessionManager

java.lang.Object
io.agentscope.spring.boot.agui.common.ThreadSessionManager

public class ThreadSessionManager extends Object
Manages agent sessions by (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();
 
  • Constructor Details

    • ThreadSessionManager

      public ThreadSessionManager(int maxSessions, int sessionTimeoutMinutes)
      Creates a new ThreadSessionManager.
      Parameters:
      maxSessions - Maximum number of sessions to maintain
      sessionTimeoutMinutes - 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 identifier
      agentId - The agent type identifier
      agentFactory - 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 be null for anonymous
      threadId - The thread identifier
      agentId - The agent type identifier
      agentFactory - 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 identifier
      agentId - The agent type identifier
      name - 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 be null for anonymous
      threadId - The thread identifier
      agentId - The agent type identifier
      name - 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() and RuntimeContext.getSessionId(). When the agent is a harness wrapper, the inner ReActAgent is 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

      public Optional<ThreadSessionManager.ThreadSession> getSession(String threadId)
      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

      public Optional<ThreadSessionManager.ThreadSession> getSession(String userId, String threadId)
      Get the session for a user and thread if it exists.
      Parameters:
      userId - The user identifier, may be null for anonymous
      threadId - 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() and ThreadSessionManager.ThreadSession.getThreadId() on each value for tenant-safe identity.

      Returns:
      session snapshot
    • removeSession

      public boolean removeSession(String threadId)
      Remove a session by threadId under the anonymous user slot.
      Parameters:
      threadId - The thread identifier
      Returns:
      true if a session was removed
    • removeSession

      public boolean removeSession(String userId, String threadId)
      Remove a session by user and thread.
      Parameters:
      userId - The user identifier, may be null for anonymous
      threadId - 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.