接口 Session

所有已知实现类:
InMemorySession, JsonSession

public interface Session
Session storage interface for AgentScope.

Sessions provide persistent storage for state objects, allowing agents, memories, toolkits, and other stateful components to be saved and restored across application runs or user interactions.

Example usage:


 Session session = new JsonSession(Path.of("sessions"));
 SessionKey sessionKey = SimpleSessionKey.of("user_123");

 // Save state
 session.save(sessionKey, "agent_meta", new AgentMetaState("id", "name", "desc", "prompt"));
 session.save(sessionKey, "memory_messages", messages);  // incremental append

 // Load state
 Optional<AgentMetaState> meta = session.get(sessionKey, "agent_meta", AgentMetaState.class);
 List<Msg> messages = session.getList(sessionKey, "memory_messages", Msg.class);
 
  • 方法详细资料

    • save

      void save(SessionKey sessionKey, String key, State value)
      Save a single state value (full replacement).

      This method saves a single state object, replacing any existing value with the same key.

      参数:
      sessionKey - the session identifier
      key - the state key (e.g., "agent_meta", "toolkit_activeGroups")
      value - the state value to save
    • save

      void save(SessionKey sessionKey, String key, List<? extends State> values)
      Save a list of state values.

      Different implementations may use different storage strategies:

      • JsonSession: Incremental append - only appends new elements not yet persisted
      • InMemorySession: Full replacement - replaces the entire list

      Callers should always pass the full list. The implementation decides the storage strategy.

      参数:
      sessionKey - the session identifier
      key - the state key (e.g., "memory_messages")
      values - the full list of state values
    • get

      <T extends State> Optional<T> get(SessionKey sessionKey, String key, Class<T> type)
      Get a single state value.
      类型参数:
      T - the state type
      参数:
      sessionKey - the session identifier
      key - the state key
      type - the expected state type
      返回:
      the state value, or empty if not found
    • getList

      <T extends State> List<T> getList(SessionKey sessionKey, String key, Class<T> itemType)
      Get a list of state values.
      类型参数:
      T - the item type
      参数:
      sessionKey - the session identifier
      key - the state key
      itemType - the expected item type
      返回:
      the list of state values, or empty list if not found
    • exists

      boolean exists(SessionKey sessionKey)
      Check if a session exists.
      参数:
      sessionKey - the session identifier
      返回:
      true if the session exists
    • delete

      void delete(SessionKey sessionKey)
      Delete a session and all its data.
      参数:
      sessionKey - the session identifier
    • delete

      default void delete(SessionKey sessionKey, String key)
      Delete a single state entry within a session.
      参数:
      sessionKey - the session identifier
      key - the state key to delete
    • listSessionKeys

      Set<SessionKey> listSessionKeys()
      List all session keys.
      返回:
      set of all session keys
    • close

      default void close()
      Clean up any resources used by this session manager. Implementations should override this if they need cleanup.