类 SessionManager

java.lang.Object
io.agentscope.core.session.SessionManager

public class SessionManager extends Object
Utility class for managing session state with simplified API.

This class provides a fluent API for both loading and saving session state without requiring manual creation of component maps and manual string keys. It supports different session implementations through dependency injection.

Usage example with JsonSession:


 SessionManager.forSessionId("user123")
     .withSession(new JsonSession(Path.of("sessions")))
     .addComponent(agent)
     .addComponent(memory)
     .loadIfExists();
 

Usage example with saving:


 SessionManager.forSessionId("user123")
     .withSession(new JsonSession(Path.of("sessions")))
     .addComponent(agent)
     .addComponent(memory)
     .saveSession(); // Save current state
 

Usage example with custom session:


 SessionManager.forSessionId("user123")
     .withSession(new DatabaseSession(dbConnection))
     .addComponent(agent)
     .addComponent(memory)
     .saveOrThrow(); // Save with error handling
 
  • 方法详细资料

    • forSessionId

      public static SessionManager forSessionId(String sessionId)
      Create a SessionManager for the given session ID.
      参数:
      sessionId - The session ID to manage
      返回:
      New SessionManager instance
      抛出:
      IllegalArgumentException - if sessionId is null or empty
    • withSession

      public SessionManager withSession(Session session)
      Set the session implementation to use.

      This method allows using any Session implementation, making the manager extensible for different storage backends.

      参数:
      session - session instance
      返回:
      This SessionManager for chaining
      抛出:
      IllegalArgumentException - if session is null
    • addComponent

      public SessionManager addComponent(StateModule component)
      Add a component to be managed.

      Components will be saved and loaded using their saveTo/loadFrom methods.

      参数:
      component - The StateModule component to add
      返回:
      This SessionManager for chaining
      抛出:
      IllegalArgumentException - if component is null
    • loadIfExists

      public void loadIfExists()
      Load session state if the session exists.

      This method will only load the state if a session with the given ID exists. If no session exists, this method does nothing.

      抛出:
      IllegalStateException - if no session has been configured
    • loadOrThrow

      public void loadOrThrow()
      Load session state, throwing an exception if session doesn't exist.
      抛出:
      IllegalStateException - if no session has been configured
      IllegalArgumentException - if session doesn't exist
    • saveSession

      public void saveSession()
      Save current component states to session storage.

      This method saves the current state of all registered components. If the session doesn't exist, it will be created.

      抛出:
      IllegalStateException - if no session has been configured
    • saveOrThrow

      public void saveOrThrow()
      Save current component states to session storage with error handling.

      This method saves the current state of all registered components and throws an exception if the save operation fails.

      抛出:
      IllegalStateException - if no session has been configured
      RuntimeException - if save operation fails
    • saveIfExists

      public void saveIfExists()
      Save session state only if the session already exists.

      This method only saves if a session with the given ID already exists. If no session exists, this method does nothing.

      抛出:
      IllegalStateException - if no session has been configured
    • sessionExists

      public boolean sessionExists()
      Check if the session exists.
      返回:
      true if session exists, false otherwise
      抛出:
      IllegalStateException - if no session has been configured
    • getSession

      public Session getSession()
      Get the configured session for advanced operations.
      返回:
      The configured Session instance
      抛出:
      IllegalStateException - if no session has been configured
    • deleteIfExists

      public boolean deleteIfExists()
      Delete the session if it exists.
      返回:
      true if session was deleted, false if it didn't exist
      抛出:
      IllegalStateException - if no session has been configured
    • deleteOrThrow

      public void deleteOrThrow()
      Delete the session, throwing an exception if it doesn't exist.
      抛出:
      IllegalStateException - if no session has been configured
      IllegalArgumentException - if session doesn't exist