Package com.tnsai.agents.async
Class SessionManager
java.lang.Object
com.tnsai.agents.async.SessionManager
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);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSession information container.static enumSession state enum. -
Constructor Summary
ConstructorsConstructorDescriptionSessionManager(Checkpointer checkpointer) Creates a SessionManager with the given checkpointer.SessionManager(Checkpointer checkpointer, Duration autoCheckpointInterval) Creates a SessionManager with auto-checkpoint interval. -
Method Summary
Modifier and TypeMethodDescriptionvoidendSession(String sessionId) Ends a session.intGets the number of active sessions.getSessionInfo(String sessionId) Gets session info.booleanhasSession(String sessionId) Checks if a session exists.pauseSession(String sessionId) Pauses a session and creates a checkpoint.booleanresumeSession(String sessionId, Agent agent) Resumes a paused session.startSession(Agent agent) Starts a new session for an agent.
-
Constructor Details
-
SessionManager
Creates a SessionManager with the given checkpointer. -
SessionManager
Creates a SessionManager with auto-checkpoint interval.
-
-
Method Details
-
startSession
Starts a new session for an agent.- Parameters:
agent- The agent to create a session for- Returns:
- The session ID
-
pauseSession
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
Resumes a paused session. Thread-safe: uses synchronized block to prevent TOCTOU race condition.- Parameters:
sessionId- The session to resumeagent- The agent to restore state to- Returns:
- true if resumed successfully
-
endSession
Ends a session.- Parameters:
sessionId- The session to end
-
getSessionInfo
Gets session info.- Parameters:
sessionId- The session ID- Returns:
- Session info if found
-
getActiveSessionCount
public int getActiveSessionCount()Gets the number of active sessions. -
hasSession
Checks if a session exists.
-