Class SessionManager

java.lang.Object
com.tnsai.agents.async.SessionManager

public class SessionManager extends Object
Manages agent sessions with automatic persistence.

SessionManager provides:

  • Session lifecycle management (start, pause, resume, end)
  • Automatic checkpointing
  • Session persistence using Checkpointer backends
  • Session metadata tracking

Usage


 SessionManager sessions = new SessionManager(SqliteCheckpointStore.create("sessions.db"));

 // Start a new session
 String sessionId = sessions.startSession(agent);
 agent.chat("First message");
 agent.chat("Second message");

 // Pause (auto-checkpoints)
 sessions.pauseSession(sessionId);

 // Later, resume
 sessions.resumeSession(sessionId, agent);
 agent.chat("Continue from where we left off");

 // End session
 sessions.endSession(sessionId);
 
  • Constructor Details

    • SessionManager

      public SessionManager(Checkpointer checkpointer)
      Creates a SessionManager with the given checkpointer.
    • SessionManager

      public SessionManager(Checkpointer checkpointer, Duration autoCheckpointInterval)
      Creates a SessionManager with auto-checkpoint interval.
  • Method Details

    • startSession

      public String startSession(Agent agent)
      Starts a new session for an agent.
      Parameters:
      agent - The agent to create a session for
      Returns:
      The session ID
    • pauseSession

      public Optional<AgentCheckpoint> pauseSession(String sessionId)
      Pauses a session and creates a checkpoint. Thread-safe: uses synchronized block to prevent TOCTOU race condition.
      Parameters:
      sessionId - The session to pause
      Returns:
      The checkpoint created
    • resumeSession

      public boolean resumeSession(String sessionId, Agent agent)
      Resumes a paused session. Thread-safe: uses synchronized block to prevent TOCTOU race condition.
      Parameters:
      sessionId - The session to resume
      agent - The agent to restore state to
      Returns:
      true if resumed successfully
    • endSession

      public void endSession(String sessionId)
      Ends a session.
      Parameters:
      sessionId - The session to end
    • getSessionInfo

      public Optional<SessionManager.SessionInfo> getSessionInfo(String sessionId)
      Gets session info.
      Parameters:
      sessionId - The session ID
      Returns:
      Session info if found
    • getActiveSessionCount

      public int getActiveSessionCount()
      Gets the number of active sessions.
    • hasSession

      public boolean hasSession(String sessionId)
      Checks if a session exists.