类 JsonSession

java.lang.Object
io.agentscope.core.session.JsonSession
所有已实现的接口:
Session

public class JsonSession extends Object implements Session
JSON file-based session implementation.

This implementation stores session state as JSON files on the filesystem. Each session is stored in a directory named by the session ID.

Features:

  • Multi-module session support
  • Atomic file operations
  • UTF-8 encoding
  • Graceful handling of missing sessions
  • Configurable storage directory
  • 构造器详细资料

    • JsonSession

      public JsonSession()
      Create a JsonSession with the default session directory.

      Uses the user's home directory with ".agentscope/sessions" as the default storage location for session files.

    • JsonSession

      public JsonSession(Path sessionDirectory)
      Create a JsonSession with a custom session directory.
      参数:
      sessionDirectory - Directory to store session files
  • 方法详细资料

    • save

      public void save(SessionKey sessionKey, String key, State value)
      Save a single state value to a JSON file.

      The state is stored in the session directory as {key}.json with pretty formatting.

      指定者:
      save 在接口中 Session
      参数:
      sessionKey - the session identifier
      key - the state key (e.g., "agent_meta", "toolkit_activeGroups")
      value - the state value to save
    • save

      public void save(SessionKey sessionKey, String key, List<? extends State> values)
      Save a list of state values to a JSONL file with hash-based change detection.

      This method uses hash-based change detection to handle both append-only and mutable lists:

      • If the hash changes (list was modified), the entire file is rewritten
      • If the list shrinks, the entire file is rewritten
      • If the list only grows (append-only), only new items are appended
      • If nothing changes, the operation is skipped
      指定者:
      save 在接口中 Session
      参数:
      sessionKey - the session identifier
      key - the state key (e.g., "memory_messages")
      values - the list of state values to save
    • get

      public <T extends State> Optional<T> get(SessionKey sessionKey, String key, Class<T> type)
      Get a single state value from a JSON file.
      指定者:
      get 在接口中 Session
      类型参数:
      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

      public <T extends State> List<T> getList(SessionKey sessionKey, String key, Class<T> itemType)
      Get a list of state values from a JSONL file.
      指定者:
      getList 在接口中 Session
      类型参数:
      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

      public boolean exists(SessionKey sessionKey)
      Check if a session exists.
      指定者:
      exists 在接口中 Session
      参数:
      sessionKey - the session identifier
      返回:
      true if the session directory exists
    • delete

      public void delete(SessionKey sessionKey)
      Delete a session and all its data.
      指定者:
      delete 在接口中 Session
      参数:
      sessionKey - the session identifier
    • delete

      public void delete(SessionKey sessionKey, String key)
      从接口复制的说明: Session
      Delete a single state entry within a session.
      指定者:
      delete 在接口中 Session
      参数:
      sessionKey - the session identifier
      key - the state key to delete
    • listSessionKeys

      public Set<SessionKey> listSessionKeys()
      List all session keys.
      指定者:
      listSessionKeys 在接口中 Session
      返回:
      set of all session keys
    • getSessionDirectory

      public Path getSessionDirectory()
      Get the session directory path.
      返回:
      Path to the session directory
    • clearAllSessions

      public reactor.core.publisher.Mono<Integer> clearAllSessions()
      Clear all sessions (for testing or cleanup).
      返回:
      Mono that completes when all sessions are deleted